From patchwork Tue Apr 4 00:13:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yosry Ahmed X-Patchwork-Id: 13198932 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 79B35C77B6C for ; Tue, 4 Apr 2023 00:14:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6237900003; Mon, 3 Apr 2023 20:14:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CEB6A900002; Mon, 3 Apr 2023 20:14:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AF03E900003; Mon, 3 Apr 2023 20:14:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9347E900002 for ; Mon, 3 Apr 2023 20:14:02 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 65177160D95 for ; Tue, 4 Apr 2023 00:14:02 +0000 (UTC) X-FDA: 80641785924.01.F7F1954 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf12.hostedemail.com (Postfix) with ESMTP id 944B340011 for ; Tue, 4 Apr 2023 00:14:00 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LtYxh8Yf; spf=pass (imf12.hostedemail.com: domain of 3x2srZAoKCHQqgkjqSZeWVYggYdW.Ugedafmp-eecnSUc.gjY@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3x2srZAoKCHQqgkjqSZeWVYggYdW.Ugedafmp-eecnSUc.gjY@flex--yosryahmed.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1680567240; a=rsa-sha256; cv=none; b=3AzKrs/8qMQ+ZNLnIRP2xM++Z0i9YM5BGcFpUufRG3ghjzDalcP8JQLN/AzbivNezZ3M/T 5NKKry8/I+52wJKLsT59P5nLP938uzNVDhksaFqt0aBobvOrxo5zzL3bNrHWG1Elf/EJrH f5Wci+xuefW9lXmOzAuufuvDazzVqN0= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=LtYxh8Yf; spf=pass (imf12.hostedemail.com: domain of 3x2srZAoKCHQqgkjqSZeWVYggYdW.Ugedafmp-eecnSUc.gjY@flex--yosryahmed.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3x2srZAoKCHQqgkjqSZeWVYggYdW.Ugedafmp-eecnSUc.gjY@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=1680567240; 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=KttjkMRwWn+raVJD4pdmRR27zCHPUdxyxHHqMjZ4zso=; b=nKgd4KxcDlaRf4U3Z4SWHrh4ejyF9/J/jMuAfOdrJ+IYggMBSnCichzyF49NMTpRjq7kAb Bc1AiRInggb9yUNoh1AfzMIEzPsGXKQ1/JU2RFUWqDYB2sfG9UhLUgVXz81SuV4GyMSoyZ AZlAVFDcuRlSfwhCsgtkNljmaYu4A7U= Received: by mail-yb1-f202.google.com with SMTP id x15-20020a25accf000000b00b3b4535c48dso30226966ybd.7 for ; Mon, 03 Apr 2023 17:14:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; t=1680567240; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=KttjkMRwWn+raVJD4pdmRR27zCHPUdxyxHHqMjZ4zso=; b=LtYxh8YfdBOctNDOeOLcBvNxi7r6sZaIjGKGM0dSm4YAneG6TwPyrn6ciNpFpw7o05 agIDp8r5g1zsrBgbrkIAj0istxDrIlFVjEGnPtfdwxO+fsBcrB9rTcH6hnSDhjeSGcI9 DNWZ+jLmPaIMNrbRgwWvkKyl3KncBlpUe/ySEPbtedlDu2Al/e4kMdIq0CbOxcu1yZTM p4hiQCl8bRdPl3Qg3dXxTrvvyXCgzXh5Si+jrlA5hVfiT0NfHjRVSIoyov4AHA/OntzE adXxHHHwpx/tQEJ5noRB+DDraQeehxLQoOxrCnjR60/cYEdTl8ijh1e5DVzefIts+Ack bxFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680567240; 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=KttjkMRwWn+raVJD4pdmRR27zCHPUdxyxHHqMjZ4zso=; b=Bvxh9WVuz8f4rnywx//bYZ67fq8vP3oJcYdzQI8q113ZKqfwbjkoclbE728jagBpuo pgPEVRXOgE8mDs5rNJDBqsFFb+pupRIlGMlcHBcQyYDdjjQBQ/+GG5UtTzYgT22ZvxDf MABmfi6K/76M01dYpGcE6SZ5vZU+Ok50EyKlb1eKTaNuD0z3racPW27CNM0nsL7CtYMO bo0iq2x1EHp1m8B9kTAF4pl0BQCr0aGJa9O2N35WeEROqj+icZWqRn6j9pooI07XsWDR 6XGnC1iW4ge56yAcsFEtHTrnt30oRHBQjEwqoIMHWhu6iCbpymB7fFkHmlO5OvAYFdXC ahCQ== X-Gm-Message-State: AAQBX9fVTAi2fUZN/Uqf4cbbIlReT3oUTnA9eiYYMRyeun358x2ktWC9 jRMrcDWqWNgpaGhDMttCWslkiKT5ZyjKVEpi X-Google-Smtp-Source: AKy350aw2fB9OoYuquCgQGUwaaV+x9FcDRGjxrCFXXINr0QvKI17cucoPmiPUl1lnvL0dNkgmm2U7bqUVOt3NxkG X-Received: from yosry.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:2327]) (user=yosryahmed job=sendgmr) by 2002:a25:d950:0:b0:b7d:87f:ce3e with SMTP id q77-20020a25d950000000b00b7d087fce3emr663961ybg.6.1680567239851; Mon, 03 Apr 2023 17:13:59 -0700 (PDT) Date: Tue, 4 Apr 2023 00:13:52 +0000 In-Reply-To: <20230404001353.468224-1-yosryahmed@google.com> Mime-Version: 1.0 References: <20230404001353.468224-1-yosryahmed@google.com> X-Mailer: git-send-email 2.40.0.348.gf938b09366-goog Message-ID: <20230404001353.468224-3-yosryahmed@google.com> Subject: [PATCH v4 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-Queue-Id: 944B340011 X-Rspamd-Server: rspam01 X-Stat-Signature: coe7jzc9t7de9ifdsh5afuah4b5ip7hj X-HE-Tag: 1680567240-390718 X-HE-Meta: U2FsdGVkX18jJvk4Blarn4ayohgWfHHRHJX+etVwazMLRFDmoFG+6G7lfV53WcDEOPzPjU2ZGMSGeq/jI4ruIQC76RT5FijQno76wgY6Lvq3/jvY6khW8zTkmzFyYXlPb7Jk0fyklXSvaT1DgbTco22RlbQi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5CqM/EpTTZPdpXKCHgvdfN12oohTzEJQROgb3Sbc5GcaPUX5Ki1a9MC4d56V8JNXrHOUiiUGX2UPgIGImf6fH6ns6ab9E8vTXL0ylA8dmLLwjpUBrvO9VjwGTQJZaDkPwhzLuSt2cRMMjaAkimu837BnH/GG1jurSeuhsiA+0qegdcuOdCGwlVHR3isEvLwV3htO7BM6UaKw4ADmb6SETJJIiRqX6cyMU8E9DrBlQZv7fuHhn+ZJ7KE+1RB52jgzltXtfJsxyVbCZQ6UHINXxt1o+a/tTIBEgTVUuf+5ZVKfNjupyvfkXuDs1TtN8M7qTuB4WcK7unjx7RFwGyV9fSebi8Pamy4sZG8Bw6+voq/A+wSDWoQjp5yKa6USEA+AzqwwDHSNItoczH56jbjsI3idsM2eRrRindzh1rCxX01l/79ZLob9dLNAa0gxaFAHOE4BvjVXutGKZmjf0r1rUcm6mm5szJLhHzSDmh5ckGKUXrdKc3I0MEC9D2YyWLi2pT+/bGaphCtwbsSzNSp6O6w== 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 | 17 ++++++++++++++++- mm/slab.c | 3 +-- mm/slob.c | 6 ++---- mm/slub.c | 5 ++--- mm/vmscan.c | 21 +++++++++++++++------ 7 files changed, 38 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..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 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. + */ +static inline void mm_account_reclaimed_pages(unsigned long pages) +{ + if (current->reclaim_state) + current->reclaim_state->reclaimed += 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..8f0e7c4e91ae3 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -511,6 +511,19 @@ static void set_task_reclaim_state(struct task_struct *task, task->reclaim_state = rs; } +/* + * 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 +5359,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 +6484,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)