From patchwork Fri Mar 31 07:08:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13195317 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 CBD49C76196 for ; Fri, 31 Mar 2023 07:08:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 583006B0078; Fri, 31 Mar 2023 03:08:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 534376B007D; Fri, 31 Mar 2023 03:08:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 35E536B007E; Fri, 31 Mar 2023 03:08:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 26D876B0078 for ; Fri, 31 Mar 2023 03:08:28 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 01D981403EC for ; Fri, 31 Mar 2023 07:08:27 +0000 (UTC) X-FDA: 80628315096.26.1D03429 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf15.hostedemail.com (Postfix) with ESMTP id 35854A0007 for ; Fri, 31 Mar 2023 07:08:26 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Qvh1jjxM; spf=pass (imf15.hostedemail.com: domain of 36YYmZAoKCLgwmqpwYfkcbemmejc.amkjglsv-kkitYai.mpe@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=36YYmZAoKCLgwmqpwYfkcbemmejc.amkjglsv-kkitYai.mpe@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1680246506; 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=EVZwB8yF2h7dd4fmRqNpsJbSVI4UBUJ0tNYnQMZkjKk=; b=qaJqgUdODS4S6cyp3sqbp+aaLmhEMWhnHm/v7ODWgqTjV2ZfF+kIAFj2QlNnQjFT7IZ43m kQpcGVLVZvbOjinV7T1wM5LdrOdzyepmW+VyoNHwSNWki5DRUQnzfYLIMS1F7J9IpyDw/f k47GSLOcYeKzrlQhH7DzufJy1Jn+Q5Q= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=Qvh1jjxM; spf=pass (imf15.hostedemail.com: domain of 36YYmZAoKCLgwmqpwYfkcbemmejc.amkjglsv-kkitYai.mpe@flex--yosryahmed.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=36YYmZAoKCLgwmqpwYfkcbemmejc.amkjglsv-kkitYai.mpe@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680246506; a=rsa-sha256; cv=none; b=4P21oTsvTXCg0d1aJ1z8xQDvqjdRWCrfVY/KMwxzE1O691MEj2N+I8SadNiNsZofVTMkrQ 379uAFS82atjvqE6/AIk5TiKOf+pMuYwZg/VWFO4gC6pRWBGcqd+mqoLLbwwKQ4XQE7mda Co9KwUH1PZQTchUQjGhuLmjxn9RcATo= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5463f2d3223so38248387b3.14 for ; Fri, 31 Mar 2023 00:08:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680246505; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EVZwB8yF2h7dd4fmRqNpsJbSVI4UBUJ0tNYnQMZkjKk=; b=Qvh1jjxMNNvin3fUDDF8N+zZ8dqqA9V78pIPC9J4eiet7qDkXN9KmmYYGJUPeJYaHs MKLH6pNXsD05xRQcVpFEEfGwpCvmj99CxoROc64tCepIXuXgVcLXkcyqrZDmsVbfc/un 7sfS8XWr9MjceXgEsPIOeI1S+8cmvT09eKc373lVeEeUsWXJ702WCHk+gyMUxa0d13mH DpL6cgiWqKJVgtXn1VwPDG4acoSGVNKXwkCh7EQWTotbaEfnfpggZPHjxNETB9GJ4f25 tJPNvYQSCiMrvuPAqeNaMz0m/mU2f6x/6ZvXVSUmCM0sHbOErJEOuuXNLHg8qMP+5e4a yxRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680246505; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EVZwB8yF2h7dd4fmRqNpsJbSVI4UBUJ0tNYnQMZkjKk=; b=NuRBTTA3zXEAhm8DE0FWPp74REG1keDyhviRG3Oh9UigrZR3DaN8N8uxXYM/FyZilw ypywb1AwjdXVY294ylkm60TOL3aTrue8NsDLogTsXADS1RMAgb51Wh3aWdxNzieR/Vr0 fKboIi04+OMWGQoJmSoXsyiobN82IlFOT7vLDJYYMbWeHx0jfcqr6B7HfcbQF8zU3Qdr WjNTNUdDsLg+2bJgPzUtJPUd69EvM6KKJKdWX/9j3PF/JTtJcvwzzsO4N4Y/NzBQMGdo h+pO0xIRp3eTKV9dtOhbnBAVP1Yyyip5MX/PS5EUFNuq0Sas1C8cQRLo3p1I+ZIp2fhc JEYw== X-Gm-Message-State: AAQBX9eHPCkk+Mm24fNhJdvZy0rC1Rmp50CSA8JRsWxXQvWbQJzLEOBX 38Eh2/zmDN+i+vCMnq3oIzQtF/bek0flzoja X-Google-Smtp-Source: AKy350acSxAChLfmqKO9826eoY8acajFCu3SAUOtEMLxv1trgvamSCxttKg1ftNmirCAT6DUp5LYAYXEe4TbOfrd X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a05:6902:188f:b0:b78:bced:2e3d with SMTP id cj15-20020a056902188f00b00b78bced2e3dmr15320634ybb.3.1680246505338; Fri, 31 Mar 2023 00:08:25 -0700 (PDT) Date: Fri, 31 Mar 2023 07:08:17 +0000 In-Reply-To: <20230331070818.2792558-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230331070818.2792558-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230331070818.2792558-3-yosryahmed@google.com> Subject: [PATCH v3 2/3] mm: vmscan: refactor updating reclaimed pages in reclaim_state From: Yosry Ahmed To: 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 Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-xfs@vger.kernel.org, linux-mm@kvack.org, Yosry Ahmed X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 35854A0007 X-Stat-Signature: p9jdojzo6w9qnkxn1umpasjmts7n4ypb X-HE-Tag: 1680246506-998104 X-HE-Meta: U2FsdGVkX1+Z5MUZrF1Bs0UoqboA4CKYfyzVsqIRASU+UkNnjdj+KRXTWuu17GOchTgQhzlblc0u5fH06mY92ZvZnY1C9imDdhGHm+dxI13KoOc4LUbVO1fgFkDZI04+9/09H1OB/ATqQgdm4YA5b5idEA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KloM5LQUGKgTrjLOv1SbDhC4Da5t62QlGwqWhDgX7CiN+rEU49BIIYMIff89KE0EIbKLkls8mfFisiA4gxY0Qzn5OLYp9diQmk61Bs0qc8Rf8g0BNlI/U05pShwwF4dyQm8j+vAdU3qW94sHlW44/fed+s0SvsZQ7IFZcMnYtYBoNuJF51e1f93JyzW/FK2nxn0Jws+oyrp0B4qZp6ejIU+34hZgGMLWVvvNP2N5yU0/5K8ScCWlYhRtLWQyLv8/yQ+jlNQaihI3WcXNXXxeqV17az1N62N0/zV0o0nEJb4Vh+FMxs9HdwG0ZanOMA8WGJpISxcRXJbKIxYfKC92qciWMBoMmpUr8Cy/ehKnTbCBCUGiItdrz26ojfT3QMiihdUXov1LnUXFrXaxZsl1Q20SMOKD//9+2xBbmgqC1RsoINkWXHF+kn0/hvZt6TEewlxCg4vF6WnjniDeOQPXADfaJlNnGtxOWnp+4RnagD3NAEdDWNPHg0rpJ7h5OjS4Yoy4tUas2qzNjyoyDBbjQX6m1shHlw/GT8Q== 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: 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 future changes to this logic are contained within mm/vmscan.c. Signed-off-by: Yosry Ahmed --- fs/inode.c | 3 +-- fs/xfs/xfs_buf.c | 3 +-- include/linux/swap.h | 5 ++++- mm/slab.c | 3 +-- mm/slob.c | 6 ++---- mm/slub.c | 5 ++--- mm/vmscan.c | 36 ++++++++++++++++++++++++++++++------ 7 files changed, 41 insertions(+), 20 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 list_head *item, __count_vm_events(KSWAPD_INODESTEAL, reap); else __count_vm_events(PGINODESTEAL, reap); - if (current->reclaim_state) - current->reclaim_state->reclaimed_slab += 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 += bp->b_page_count; + mm_account_reclaimed_pages(bp->b_page_count); if (bp->b_pages != bp->b_page_array) kmem_free(bp->b_pages); diff --git a/include/linux/swap.h b/include/linux/swap.h index 209a425739a9f..589ea2731931f 100644 --- a/include/linux/swap.h +++ b/include/linux/swap.h @@ -153,13 +153,16 @@ 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 }; +void mm_account_reclaimed_pages(unsigned long 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 *cachep, struct slab *slab) smp_wmb(); __folio_clear_slab(folio); - if (current->reclaim_state) - current->reclaim_state->reclaimed_slab += 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 = virt_to_page(b); - if (current->reclaim_state) - current->reclaim_state->reclaimed_slab += 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, struct 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 += 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 fef7d1c0f82b2..a3e38851b34ac 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -511,6 +511,34 @@ static void set_task_reclaim_state(struct task_struct *task, task->reclaim_state = rs; } +/* + * mm_account_reclaimed_pages(): account reclaimed pages outside of LRU-based + * reclaim + * @pages: number of pages reclaimed + * + * If the current process is undergoing a reclaim operation, increment the + * number of reclaimed pages by @pages. + */ +void mm_account_reclaimed_pages(unsigned long pages) +{ + if (current->reclaim_state) + current->reclaim_state->reclaimed += pages; +} +EXPORT_SYMBOL(mm_account_reclaimed_pages); + +/* + * flush_reclaim_state(): add pages reclaimed outside of LRU-based reclaim to + * scan_control->nr_reclaimed. + */ +static void flush_reclaim_state(struct scan_control *sc, + struct reclaim_state *rs) +{ + if (rs) { + sc->nr_reclaimed += rs->reclaimed; + rs->reclaimed = 0; + } +} + static long xchg_nr_deferred(struct shrinker *shrinker, struct shrink_control *sc) { @@ -5346,8 +5374,7 @@ static int shrink_one(struct lruvec *lruvec, struct scan_control *sc) vmpressure(sc->gfp_mask, memcg, false, sc->nr_scanned - scanned, sc->nr_reclaimed - reclaimed); - sc->nr_reclaimed += current->reclaim_state->reclaimed_slab; - current->reclaim_state->reclaimed_slab = 0; + flush_reclaim_state(sc, current->reclaim_state); return success ? MEMCG_LRU_YOUNG : 0; } @@ -6472,10 +6499,7 @@ static void shrink_node(pg_data_t *pgdat, struct scan_control *sc) shrink_node_memcgs(pgdat, sc); - if (reclaim_state) { - sc->nr_reclaimed += reclaim_state->reclaimed_slab; - reclaim_state->reclaimed_slab = 0; - } + flush_reclaim_state(sc, reclaim_state); /* Record the subtree's reclaim efficiency */ if (!sc->proactive)