Received: by 2002:ac8:156:0:b0:3e0:cd10:60c8 with SMTP id f22csp15452qtg; Thu, 6 Apr 2023 10:45:27 -0700 (PDT) X-Google-Smtp-Source: AKy350be8wHxlHqeAJmbIpax9gcJ0V+gLVKdeicJ1GCufVQql/YJrBl0SCCeARgko/PT6edtAQUs X-Received: by 2002:a17:906:198e:b0:92f:de0d:104b with SMTP id g14-20020a170906198e00b0092fde0d104bmr7485936ejd.9.1680803127720; Thu, 06 Apr 2023 10:45:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680803127; cv=none; d=google.com; s=arc-20160816; b=uS2+FpvNtRqTXw3HgjAOzo1XR9KyeiQTxW4WTz3nZ445oSwk6AMJTOu2dJ4szCHmh1 GvCOxgTFFNGaCzPIjO9J0wIggOntd6GS6qNJ2tnJhXe4HsC1twJ4c7TK4UNGFHOmJuEA mcuV6n557ZtSCr/F01D2wh/DsQpupHMnbnjO2c1HGb72ErtDcV8LF8hfS5YmQ0KbWhIH eO9pbT7Hk4Ckg9wEAt64S/1+kU+s1gPPnhV6+wELkxK3/Sen+A6lR+O2UI10QgW5mjiF 4j6mJZPMTk+EtINW4SKfDpGXzTSEJwTt7WT6PD5CnmOU6oxYPDc3uZk4KpzDglnN3f3k +PcA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=XgunQKgjebnF9/8Pl+g6YNmqfUk6jNisGsID9O/PX0w=; b=q/+yTAUiqizPfARXhIn4JWR4PSrxdo3kpa1jWekduAg3KWUo+ZpwEMnGmdUQ9hMn0c B8aPdpFaX6QnxQmkryFNf/TuZOLLmWwNldZfrofBRDAoMeIxciG+s4DKgjR36qJ4a8ll GM7VoZqNlxIHU2wTxvGX+zKVQBKXBjPYPQPCpLtgCr3hvLBWN68/8DZ/ouwWx4zOnN6+ MB/M1098eM+2XwrTkc8vd9X4gSKBAoLaDJf2H5aVG/MAcAyb1T1Fmt/KAM0uTo/IJUqW gyryc8+doz4jV5U0FcDUV36zlXjK3t1w8RTGe3gDzCNknMrmjE80a4LA0mBcpM3cVu0E aMSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ccSPSMRU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f20-20020a1709062c5400b008b178bc310csi1508067ejh.189.2023.04.06.10.45.01; Thu, 06 Apr 2023 10:45:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ccSPSMRU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239935AbjDFRoJ (ORCPT + 99 others); Thu, 6 Apr 2023 13:44:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37422 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239900AbjDFRoH (ORCPT ); Thu, 6 Apr 2023 13:44:07 -0400 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 105001AC for ; Thu, 6 Apr 2023 10:44:05 -0700 (PDT) Received: by mail-ej1-x630.google.com with SMTP id sg7so3261722ejc.9 for ; Thu, 06 Apr 2023 10:44:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680803043; x=1683395043; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=XgunQKgjebnF9/8Pl+g6YNmqfUk6jNisGsID9O/PX0w=; b=ccSPSMRUDSc0bpLAhvDCPyES07oKwyIPZNwuQ/IwZoOgNBsMXGbH+jhpzcy2CVs6qe gf9q7XlZX6ZhtppOGmeAjo6xywnsdB5dv2HzOpUbMN2fJXWuqZGuAWO9sgExTpbJehI8 TQunqGBOG6XHxog87yLj7Wv+VJD0njH/KctbHt8uMNWNiqLCpf6zzT6ZRYdVL6JeTJfG /y/8SaqX1OEijhY6eIOBA47tJsskssjQsL7B5zdEouMlCtKuog897UwiSIWk8QHAEyy+ /vEQSxwnWA+KRfv4bkA1zaiaNt33qeMlHVQTQ99RhLlotsKTTZjVtOa9Uh8XO8Ni/IN4 F//g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680803043; x=1683395043; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XgunQKgjebnF9/8Pl+g6YNmqfUk6jNisGsID9O/PX0w=; b=7DN0p9SwxtUIL0FJNdkXA2d3NvaqNLZJKUIjenbQAFw7udrnh158Qw6+A32akQ+hWu i1xMcYxew8ZX+eNqj4baYvdKv0vnPnedsoDuWIjD8n2KWe3J9AVxYPBqUsVPl+waITnl QhLEUgYwBwtOGfudwsnjGnF4zDQ7kxYywZxuBU9TozJ1tgQjxjD1LAgQgfGaaToTFYhB OFx1vhR3rTayvrrk9qLSXHje34AU5NbyDNR+jYxl1fMekNVZdy6ttWD/nQQofGU3BDON 7QgQgEdlajGIiCEG8p9AxOrQ9eFQXPTnKGbLokXm4naNVRgNqqoO7yRLVNvEGf4cYAoQ wdoA== X-Gm-Message-State: AAQBX9cuLIjP1S1p7hh+6oSP2n0T3yQkSYTDl6eCE7ltlJ0abCTLre4H Ain+ZO4sDKG0J4ZECpgdE1iuTtboWl9jQQVWslC25g== X-Received: by 2002:a17:906:3b07:b0:935:3085:303b with SMTP id g7-20020a1709063b0700b009353085303bmr3722546ejf.15.1680803043263; Thu, 06 Apr 2023 10:44:03 -0700 (PDT) MIME-Version: 1.0 References: <20230405185427.1246289-1-yosryahmed@google.com> <20230405185427.1246289-3-yosryahmed@google.com> <7ce03e4323b95c1e8fd3faed32c9b285162fe5a8.camel@linux.intel.com> In-Reply-To: <7ce03e4323b95c1e8fd3faed32c9b285162fe5a8.camel@linux.intel.com> From: Yosry Ahmed Date: Thu, 6 Apr 2023 10:43:26 -0700 Message-ID: Subject: Re: [PATCH v5 2/2] mm: vmscan: refactor reclaim_state helpers To: Tim Chen Cc: Andrew Morton , Alexander Viro , "Darrick J. Wong" , Christoph Lameter , David Rientjes , Joonsoo Kim , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, "Matthew Wilcox (Oracle)" , Miaohe Lin , David Hildenbrand , Johannes Weiner , Peter Xu , NeilBrown , Shakeel Butt , Michal Hocko , Yu Zhao , Dave Chinner , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-15.7 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,ENV_AND_HDR_SPF_MATCH, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL, USER_IN_DEF_SPF_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 6, 2023 at 10:32=E2=80=AFAM Tim Chen wrote: > > On Wed, 2023-04-05 at 18:54 +0000, Yosry Ahmed wrote: > > During reclaim, we keep track of pages reclaimed from other means than > > LRU-based reclaim through scan_control->reclaim_state->reclaimed_slab, > > which we stash a pointer to in current task_struct. > > > > However, we keep track of more than just reclaimed slab pages through > > this. We also use it for clean file pages dropped through pruned inodes= , > > and xfs buffer pages freed. Rename reclaimed_slab to reclaimed, and add > > a helper function that wraps updating it through current, so that futur= e > > changes to this logic are contained within mm/vmscan.c. > > > > Additionally, add a flush_reclaim_state() helper to wrap using > > reclaim_state->reclaimed to updated sc->nr_reclaimed, and use that > > helper to add an elaborate comment about why we only do the update for > > global reclaim. > > > > Finally, move set_task_reclaim_state() next to flush_reclaim_state() so > > that all reclaim_state helpers are in close proximity for easier > > readability. > > > > Signed-off-by: Yosry Ahmed > > --- > > fs/inode.c | 3 +- > > fs/xfs/xfs_buf.c | 3 +- > > include/linux/swap.h | 17 +++++++++- > > mm/slab.c | 3 +- > > mm/slob.c | 6 ++-- > > mm/slub.c | 5 ++- > > mm/vmscan.c | 75 ++++++++++++++++++++++++++++++++------------ > > 7 files changed, 78 insertions(+), 34 deletions(-) > > > > diff --git a/fs/inode.c b/fs/inode.c > > index 4558dc2f13557..e60fcc41faf17 100644 > > --- a/fs/inode.c > > +++ b/fs/inode.c > > @@ -864,8 +864,7 @@ static enum lru_status inode_lru_isolate(struct lis= t_head *item, > > __count_vm_events(KSWAPD_INODESTEAL, reap= ); > > else > > __count_vm_events(PGINODESTEAL, reap); > > - if (current->reclaim_state) > > - current->reclaim_state->reclaimed_slab += =3D reap; > > + mm_account_reclaimed_pages(reap); > > } > > iput(inode); > > spin_lock(lru_lock); > > diff --git a/fs/xfs/xfs_buf.c b/fs/xfs/xfs_buf.c > > index 54c774af6e1c6..15d1e5a7c2d34 100644 > > --- a/fs/xfs/xfs_buf.c > > +++ b/fs/xfs/xfs_buf.c > > @@ -286,8 +286,7 @@ xfs_buf_free_pages( > > if (bp->b_pages[i]) > > __free_page(bp->b_pages[i]); > > } > > - if (current->reclaim_state) > > - current->reclaim_state->reclaimed_slab +=3D bp->b_page_co= unt; > > + mm_account_reclaimed_pages(bp->b_page_count); > > > > if (bp->b_pages !=3D bp->b_page_array) > > kmem_free(bp->b_pages); > > diff --git a/include/linux/swap.h b/include/linux/swap.h > > index 209a425739a9f..e131ac155fb95 100644 > > --- a/include/linux/swap.h > > +++ b/include/linux/swap.h > > @@ -153,13 +153,28 @@ union swap_header { > > * memory reclaim > > */ > > struct reclaim_state { > > - unsigned long reclaimed_slab; > > + /* pages reclaimed outside of LRU-based reclaim */ > > + unsigned long reclaimed; > > #ifdef CONFIG_LRU_GEN > > /* per-thread mm walk data */ > > struct lru_gen_mm_walk *mm_walk; > > #endif > > }; > > > > +/* > > + * mm_account_reclaimed_pages(): account reclaimed pages outside of LR= U-based > > + * reclaim > > + * @pages: number of pages reclaimed > > + * > > + * If the current process is undergoing a reclaim operation, increment= the > > + * number of reclaimed pages by @pages. > > + */ > > +static inline void mm_account_reclaimed_pages(unsigned long pages) > > +{ > > + if (current->reclaim_state) > > + current->reclaim_state->reclaimed +=3D pages; > > +} > > + > > #ifdef __KERNEL__ > > > > struct address_space; > > diff --git a/mm/slab.c b/mm/slab.c > > index dabc2a671fc6f..64bf1de817b24 100644 > > --- a/mm/slab.c > > +++ b/mm/slab.c > > @@ -1392,8 +1392,7 @@ static void kmem_freepages(struct kmem_cache *cac= hep, struct slab *slab) > > smp_wmb(); > > __folio_clear_slab(folio); > > > > - if (current->reclaim_state) > > - current->reclaim_state->reclaimed_slab +=3D 1 << order; > > + mm_account_reclaimed_pages(1 << order); > > unaccount_slab(slab, order, cachep); > > __free_pages(&folio->page, order); > > } > > diff --git a/mm/slob.c b/mm/slob.c > > index fe567fcfa3a39..79cc8680c973c 100644 > > --- a/mm/slob.c > > +++ b/mm/slob.c > > @@ -61,7 +61,7 @@ > > #include > > > > #include > > -#include /* struct reclaim_state */ > > +#include /* mm_account_reclaimed_pages() */ > > #include > > #include > > #include > > @@ -211,9 +211,7 @@ static void slob_free_pages(void *b, int order) > > { > > struct page *sp =3D virt_to_page(b); > > > > - if (current->reclaim_state) > > - current->reclaim_state->reclaimed_slab +=3D 1 << order; > > - > > + mm_account_reclaimed_pages(1 << order); > > mod_node_page_state(page_pgdat(sp), NR_SLAB_UNRECLAIMABLE_B, > > -(PAGE_SIZE << order)); > > __free_pages(sp, order); > > diff --git a/mm/slub.c b/mm/slub.c > > index 39327e98fce34..7aa30eef82350 100644 > > --- a/mm/slub.c > > +++ b/mm/slub.c > > @@ -11,7 +11,7 @@ > > */ > > > > #include > > -#include /* struct reclaim_state */ > > +#include /* mm_account_reclaimed_pages() */ > > #include > > #include > > #include > > @@ -2063,8 +2063,7 @@ static void __free_slab(struct kmem_cache *s, str= uct slab *slab) > > /* Make the mapping reset visible before clearing the flag */ > > smp_wmb(); > > __folio_clear_slab(folio); > > - if (current->reclaim_state) > > - current->reclaim_state->reclaimed_slab +=3D pages; > > + mm_account_reclaimed_pages(pages); > > unaccount_slab(slab, order, s); > > __free_pages(&folio->page, order); > > } > > diff --git a/mm/vmscan.c b/mm/vmscan.c > > index c82bd89f90364..049e39202e6ce 100644 > > --- a/mm/vmscan.c > > +++ b/mm/vmscan.c > > @@ -188,18 +188,6 @@ struct scan_control { > > */ > > int vm_swappiness =3D 60; > > > > -static void set_task_reclaim_state(struct task_struct *task, > > - struct reclaim_state *rs) > > -{ > > - /* Check for an overwrite */ > > - WARN_ON_ONCE(rs && task->reclaim_state); > > - > > - /* Check for the nulling of an already-nulled member */ > > - WARN_ON_ONCE(!rs && !task->reclaim_state); > > - > > - task->reclaim_state =3D rs; > > -} > > - > > LIST_HEAD(shrinker_list); > > DECLARE_RWSEM(shrinker_rwsem); > > > > @@ -511,6 +499,59 @@ static bool writeback_throttling_sane(struct scan_= control *sc) > > } > > #endif > > > > +static void set_task_reclaim_state(struct task_struct *task, > > + struct reclaim_state *rs) > > +{ > > + /* Check for an overwrite */ > > + WARN_ON_ONCE(rs && task->reclaim_state); > > + > > + /* Check for the nulling of an already-nulled member */ > > + WARN_ON_ONCE(!rs && !task->reclaim_state); > > + > > + task->reclaim_state =3D rs; > > +} > > + > > +/* > > + * flush_reclaim_state(): add pages reclaimed outside of LRU-based rec= laim to > > + * scan_control->nr_reclaimed. > > + */ > > +static void flush_reclaim_state(struct scan_control *sc, > > + struct reclaim_state *rs) > > +{ > > + /* > > + * Currently, reclaim_state->reclaimed includes three types of pa= ges > > + * freed outside of vmscan: > > + * (1) Slab pages. > > + * (2) Clean file pages from pruned inodes. > > + * (3) XFS freed buffer pages. > > + * > > + * For all of these cases, we have no way of finding out whether = these > > + * pages were related to the memcg under reclaim. For example, a = freed > > + * slab page could have had only a single object charged to the m= emcg > > Minor nits: > s/could have had/could have > > > + * under reclaim. Also, populated inodes are not on shrinker LRUs > > + * anymore except on highmem systems. > > + * > > + * Instead of over-reporting the reclaimed pages in a memcg recla= im, > > + * only count such pages in global reclaim. This prevents unneces= sary > > May be clearer to say: > This prevents under-reclaimaing the target memcg, and unnecessary Thanks, will rephrase for the next version! > > > + * retries during memcg charging and false positive from proactiv= e > > + * reclaim (memory.reclaim). > > + * > > Tim >