From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B837EE7AD47 for ; Tue, 3 Oct 2023 13:28:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 413CA8D0076; Tue, 3 Oct 2023 09:28:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3C2A88D0003; Tue, 3 Oct 2023 09:28:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 264608D0076; Tue, 3 Oct 2023 09:28:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1605B8D0003 for ; Tue, 3 Oct 2023 09:28:47 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C6602140314 for ; Tue, 3 Oct 2023 13:28:46 +0000 (UTC) X-FDA: 81304230252.16.E2C2DB4 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by imf27.hostedemail.com (Postfix) with ESMTP id 9F9D940038 for ; Tue, 3 Oct 2023 13:28:44 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=d4lrfI5Y; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=SFIfmLmE; dmarc=none; spf=pass (imf27.hostedemail.com: domain of jack@suse.cz designates 195.135.220.28 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1696339724; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Y06AtrFsQ3PjPoBLSLe2+hpm0kIXInuEKE0T9W7n+MI=; b=SbMgr4zSuowq0TZjQWKirt4seMUy24QAsmD2YNHJvsH26rH2VtsZVncvXJoBh4egAJjaeQ 2Zi0QD3KWt/UIYev5miAkGCh8kCmkdsX585dEavZyoIsH52esuGVT6aweSjYmNY5a8tH9g pQEomsRvlwmZv0rAOabztuykLabmnEw= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=suse.cz header.s=susede2_rsa header.b=d4lrfI5Y; dkim=pass header.d=suse.cz header.s=susede2_ed25519 header.b=SFIfmLmE; dmarc=none; spf=pass (imf27.hostedemail.com: domain of jack@suse.cz designates 195.135.220.28 as permitted sender) smtp.mailfrom=jack@suse.cz ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696339724; a=rsa-sha256; cv=none; b=5uUDDSg31P/EWTWok7VKqjvv1tHoZsyKbHhRErJLv8G1btP7cCKnwpfEipRQBUnNtIp86D aONOjUPOK4Fpih09uGXTkJVEQOVTUuEgN9B3L3RvR8Om1t7lb5Pd1uqpISaWMXtqGwmXNF FCS603XB29oHfoBvOlFBll8zl72PJs4= Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id D63D621892; Tue, 3 Oct 2023 13:28:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1696339722; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Y06AtrFsQ3PjPoBLSLe2+hpm0kIXInuEKE0T9W7n+MI=; b=d4lrfI5YfWhF4HGLALTHxj2phyE3d22SjIV8qBLYRgtYWs+T7gaOaC9s4AU0qtwfJrktxb e9Yf3qVSBRPBfl98BsAvLFTh7dqoODbx6K8CiIJIor8zyJ/h2imkya+vdaeHrEBwSJb785 wf0c1MmCxQqdCoWuRh5TPX5iGDaQJEI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1696339722; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=Y06AtrFsQ3PjPoBLSLe2+hpm0kIXInuEKE0T9W7n+MI=; b=SFIfmLmERxs2L2TdDSrpBEYP8v9SfC74lSa5GQ5WViAF02HYA7kKdKmfUEYBKMTbgR4MkY peN3nYUPd+AW71AA== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id C6A11139F9; Tue, 3 Oct 2023 13:28:42 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id QX5yMAoXHGXpOAAAMHmgww (envelope-from ); Tue, 03 Oct 2023 13:28:42 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 456D1A07CC; Tue, 3 Oct 2023 15:28:42 +0200 (CEST) Date: Tue, 3 Oct 2023 15:28:42 +0200 From: Jan Kara To: Hugh Dickins Cc: Andrew Morton , Christian Brauner , Carlos Maiolino , Chuck Lever , Jan Kara , Matthew Wilcox , Johannes Weiner , Axel Rasmussen , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH 6/8] shmem: move memcg charge out of shmem_add_to_page_cache() Message-ID: <20231003132842.lxniwpknqfxan5px@quack3> References: <4b2143c5-bf32-64f0-841-81a81158dac@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4b2143c5-bf32-64f0-841-81a81158dac@google.com> X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 9F9D940038 X-Stat-Signature: wxcx6n4z6sd5919y3abg9x4y3x8ng69n X-HE-Tag: 1696339724-103935 X-HE-Meta: U2FsdGVkX18j6GzPnvwUtfflsoU5f80R1oLBz5PoUDI1zn3hgsWygHmztIl8WY9q0LwD2P82YL6tHUaAc/jSh2g03IIhuBKyCFMJ6ZwoXK+t4h2r7xo4qNv6+DsAx/pSMkc78mVIi3Je79JMasKStp2AJrIh9XLavV0kJYRfLqQ7a8cgBjJaMeGJ0QbuZeJ/woa6tiG12J8MWMRKFPCl+XEJ4/Xcoz2PCYClYxZhL/H+3fKtc/nCHaC9ICqpNpsvdshdFK577fAGcjZoransqhrK/pLd8fED8R5FKKf4UCNvNd6JjtpxuPm6KmbGvbkCG7uRHb4u8Ykx/qlu/FnUYYI8Szmiwdgt4cl2c688P0xmbV6PgNisRd5amrgqnjM2GLIrV5ysFlrLEuybahODyiMFsBdaKae4+7fla2LZ+iY+WESnRnmVNErKloe7/5T8tkInsgramCfhdiZrmiDWeXh3DxzOBJz/0PdSIoxfvYBZGmrDInikr3JA/wdeWJ/lpMkRO7o1VX8DjGrXGk82D9NCvNPT8O6IDoP3EoCBY86uHyd/c+qkSObJSvKV0KXUOHuIQy/jdhBpkZTgQvfsgrcl1ZVeUyyUSkQ2Xe8AzXKoeqCSlSJZtFl+mzFFfYKORtRNzigA/x4VCCBP/T9pFjaVSnj226I2NWaHdkjsqtfKBagstMClH2BvTjMa+cxUEy6viLDF/w/qu7mMmNxnWt8j3mV7fVBnmvUcHR113EvCudhpcHprw1WMHcjVM2d8fyYnh9GYn73XuXzCkxkg2qKN8+lODtrwRw9t+bDyxvt/3oYnSDVnj1QrnTyNdfhQ3OU21UZXEw6SODKos1eNxQWn9Mvn8etOzngqKKBDbfSwlj2yflk4yoLIBhYOlUWlHH2L6WIyhWWRba4byTCUaFiimOreZqaSK7WiS45veNqhm3HPDljTFRYQSMRaK2UrE/MJM1KSAGY4NrY9iXr 9F0/Zkh1 2qkUskr6N8iEqStCcM550ZRpgG3Usf6G/PbUIwCnLgpH2dpwBl5tjg9EK12SPETOOv3NMsSQ3pByneC23etaFflQoWmmaYQEcfic7T1ie/mvXyBoAHt7x7wMoLQrGlV19iQZiK57lFLSKHVXumXSafk2orA1Vz0c3ZdE5YNIhBIAIiwYaoIcDml0yA/PQggftD3IzjZyK7eTUiMysFVFbGMrhacHxIcdCh2bO4eAKQrxRikA+0h3ZLEbMTJ59j7QMa9RL0XiSvbvH0e9+V8xRgL/0gg2lgP5ZhIcftXiAUy8I1LPCT3wlXCfCIR8/KW1gX54M X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri 29-09-23 20:31:27, Hugh Dickins wrote: > Extract shmem's memcg charging out of shmem_add_to_page_cache(): it's > misleading done there, because many calls are dealing with a swapcache > page, whose memcg is nowadays always remembered while swapped out, then > the charge re-levied when it's brought back into swapcache. > > Temporarily move it back up to the shmem_get_folio_gfp() level, where > the memcg was charged before v5.8; but the next commit goes on to move > it back down to a new home. > > In making this change, it becomes clear that shmem_swapin_folio() does > not need to know the vma, just the fault mm (if any): call it fault_mm > rather than charge_mm - let mem_cgroup_charge() decide whom to charge. > > Signed-off-by: Hugh Dickins Looks good. Feel free to add: Reviewed-by: Jan Kara Honza > --- > mm/shmem.c | 68 +++++++++++++++++++++++------------------------------- > 1 file changed, 29 insertions(+), 39 deletions(-) > > diff --git a/mm/shmem.c b/mm/shmem.c > index 63ba6037b23a..0a7f7b567b80 100644 > --- a/mm/shmem.c > +++ b/mm/shmem.c > @@ -146,9 +146,8 @@ static unsigned long shmem_default_max_inodes(void) > #endif > > static int shmem_swapin_folio(struct inode *inode, pgoff_t index, > - struct folio **foliop, enum sgp_type sgp, > - gfp_t gfp, struct vm_area_struct *vma, > - vm_fault_t *fault_type); > + struct folio **foliop, enum sgp_type sgp, gfp_t gfp, > + struct mm_struct *fault_mm, vm_fault_t *fault_type); > > static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) > { > @@ -760,12 +759,10 @@ static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, > */ > static int shmem_add_to_page_cache(struct folio *folio, > struct address_space *mapping, > - pgoff_t index, void *expected, gfp_t gfp, > - struct mm_struct *charge_mm) > + pgoff_t index, void *expected, gfp_t gfp) > { > XA_STATE_ORDER(xas, &mapping->i_pages, index, folio_order(folio)); > long nr = folio_nr_pages(folio); > - int error; > > VM_BUG_ON_FOLIO(index != round_down(index, nr), folio); > VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); > @@ -776,16 +773,7 @@ static int shmem_add_to_page_cache(struct folio *folio, > folio->mapping = mapping; > folio->index = index; > > - if (!folio_test_swapcache(folio)) { > - error = mem_cgroup_charge(folio, charge_mm, gfp); > - if (error) { > - if (folio_test_pmd_mappable(folio)) { > - count_vm_event(THP_FILE_FALLBACK); > - count_vm_event(THP_FILE_FALLBACK_CHARGE); > - } > - goto error; > - } > - } > + gfp &= GFP_RECLAIM_MASK; > folio_throttle_swaprate(folio, gfp); > > do { > @@ -813,15 +801,12 @@ static int shmem_add_to_page_cache(struct folio *folio, > } while (xas_nomem(&xas, gfp)); > > if (xas_error(&xas)) { > - error = xas_error(&xas); > - goto error; > + folio->mapping = NULL; > + folio_ref_sub(folio, nr); > + return xas_error(&xas); > } > > return 0; > -error: > - folio->mapping = NULL; > - folio_ref_sub(folio, nr); > - return error; > } > > /* > @@ -1324,10 +1309,8 @@ static int shmem_unuse_swap_entries(struct inode *inode, > > if (!xa_is_value(folio)) > continue; > - error = shmem_swapin_folio(inode, indices[i], > - &folio, SGP_CACHE, > - mapping_gfp_mask(mapping), > - NULL, NULL); > + error = shmem_swapin_folio(inode, indices[i], &folio, SGP_CACHE, > + mapping_gfp_mask(mapping), NULL, NULL); > if (error == 0) { > folio_unlock(folio); > folio_put(folio); > @@ -1810,12 +1793,11 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, > */ > static int shmem_swapin_folio(struct inode *inode, pgoff_t index, > struct folio **foliop, enum sgp_type sgp, > - gfp_t gfp, struct vm_area_struct *vma, > + gfp_t gfp, struct mm_struct *fault_mm, > vm_fault_t *fault_type) > { > struct address_space *mapping = inode->i_mapping; > struct shmem_inode_info *info = SHMEM_I(inode); > - struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL; > struct swap_info_struct *si; > struct folio *folio = NULL; > swp_entry_t swap; > @@ -1843,7 +1825,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, > if (fault_type) { > *fault_type |= VM_FAULT_MAJOR; > count_vm_event(PGMAJFAULT); > - count_memcg_event_mm(charge_mm, PGMAJFAULT); > + count_memcg_event_mm(fault_mm, PGMAJFAULT); > } > /* Here we actually start the io */ > folio = shmem_swapin(swap, gfp, info, index); > @@ -1880,8 +1862,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, > } > > error = shmem_add_to_page_cache(folio, mapping, index, > - swp_to_radix_entry(swap), gfp, > - charge_mm); > + swp_to_radix_entry(swap), gfp); > if (error) > goto failed; > > @@ -1929,7 +1910,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, > struct address_space *mapping = inode->i_mapping; > struct shmem_inode_info *info = SHMEM_I(inode); > struct shmem_sb_info *sbinfo; > - struct mm_struct *charge_mm; > + struct mm_struct *fault_mm; > struct folio *folio; > pgoff_t hindex; > gfp_t huge_gfp; > @@ -1946,7 +1927,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, > } > > sbinfo = SHMEM_SB(inode->i_sb); > - charge_mm = vma ? vma->vm_mm : NULL; > + fault_mm = vma ? vma->vm_mm : NULL; > > folio = filemap_get_entry(mapping, index); > if (folio && vma && userfaultfd_minor(vma)) { > @@ -1958,7 +1939,7 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, > > if (xa_is_value(folio)) { > error = shmem_swapin_folio(inode, index, &folio, > - sgp, gfp, vma, fault_type); > + sgp, gfp, fault_mm, fault_type); > if (error == -EEXIST) > goto repeat; > > @@ -2044,9 +2025,16 @@ static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, > if (sgp == SGP_WRITE) > __folio_set_referenced(folio); > > - error = shmem_add_to_page_cache(folio, mapping, hindex, > - NULL, gfp & GFP_RECLAIM_MASK, > - charge_mm); > + error = mem_cgroup_charge(folio, fault_mm, gfp); > + if (error) { > + if (folio_test_pmd_mappable(folio)) { > + count_vm_event(THP_FILE_FALLBACK); > + count_vm_event(THP_FILE_FALLBACK_CHARGE); > + } > + goto unacct; > + } > + > + error = shmem_add_to_page_cache(folio, mapping, hindex, NULL, gfp); > if (error) > goto unacct; > > @@ -2644,8 +2632,10 @@ int shmem_mfill_atomic_pte(pmd_t *dst_pmd, > if (unlikely(pgoff >= max_off)) > goto out_release; > > - ret = shmem_add_to_page_cache(folio, mapping, pgoff, NULL, > - gfp & GFP_RECLAIM_MASK, dst_vma->vm_mm); > + ret = mem_cgroup_charge(folio, dst_vma->vm_mm, gfp); > + if (ret) > + goto out_release; > + ret = shmem_add_to_page_cache(folio, mapping, pgoff, NULL, gfp); > if (ret) > goto out_release; > > -- > 2.35.3 > -- Jan Kara SUSE Labs, CR