All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ridong Chen <ridong.chen@linux.dev>
To: Andrew Morton <akpm@linux-foundation.org>,
	Kairui Song <kasong@tencent.com>,
	David Hildenbrand <david@kernel.org>,
	Johannes Weiner <hannes@cmpxchg.org>
Cc: Barry Song <baohua@kernel.org>, Qi Zheng <qi.zheng@linux.dev>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	Axel Rasmussen <axelrasmussen@google.com>,
	Yuanchu Xie <yuanchu@google.com>, Wei Xu <weixugc@google.com>,
	Baoquan He <baoquan.he@linux.dev>,
	Baolin Wang <baolin.wang@linux.alibaba.com>,
	"Liam R. Howlett" <liam@infradead.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
	linux-mm@kvack.org (open list:MEMORY MANAGEMENT - MGLRU
	(MULTI-GEN LRU)), Ridong Chen <ridong.chen@linux.dev>,
	Ridong Chen <chenridong@xiaomi.com>
Subject: [RFC PATCH 1/2] mm/mglru: update LRU sizes once per zone when isolating folios
Date: Fri, 27 Nov 2026 14:03:14 +0800	[thread overview]
Message-ID: <20261127060315.1119672-2-ridong.chen@linux.dev> (raw)
In-Reply-To: <20261127060315.1119672-1-ridong.chen@linux.dev>

From: Ridong Chen <chenridong@xiaomi.com>

scan_folios() isolates up to MIN_LRU_BATCH folios under lru_lock, and
lru_gen_del_folio() updates the LRU sizes for each of them. Each update
is a mod_lruvec_state() for the node and memcg counters plus a zone
counter, so a batch makes up to 64 such calls while other CPUs spin on
the lock. isolate_lru_folios() avoids that for the classical LRU by
counting the isolated pages per zone in nr_zone_taken[] and calling
update_lru_sizes() once at the end.

Do the same for MGLRU: let isolate_folio() add the pages it isolates to
a per-zone count, split by whether their gen is active, and update the
LRU sizes once per zone after the scan. lrugen->nr_pages is still
updated per folio, for the gen each folio was taken off. The active
split can't change during the scan because max_seq only advances under
lru_lock, which scan_folios() holds.

Tested on an i7-13700HX (8P+8E cores, 24 threads), turbo off and the
performance governor. 24 files of 128MiB on ext4 on brd, all threads in
one memcg with memory.max=1G, random 4K pread(), so nearly every read
refaults and every thread ends up in direct reclaim. Reads per second
per CPU, mean of 2 runs, with and without this change in one kernel and
the runs interleaved:

  CPUs      base   patched
     1    426681    424975   -0.4%
     2    387442    390272   +0.7%
     4    360312    363840   +1.0%
     8    314549    322322   +2.5%
    12    235058    242638   +3.2%
    16    184086    201351   +9.4%
    24    124478    143734  +15.5%

The gain comes from the time lru_lock is held, so it only shows when
many CPUs reclaim from the same lruvec.

Signed-off-by: Ridong Chen <chenridong@xiaomi.com>
---
 include/linux/mm_inline.h | 26 ++++++++++++++++++++++++--
 mm/vmscan.c               | 16 +++++++++++++---
 2 files changed, 37 insertions(+), 5 deletions(-)

diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h
index 0a055a3fe210..84a1abee3728 100644
--- a/include/linux/mm_inline.h
+++ b/include/linux/mm_inline.h
@@ -376,7 +376,13 @@ static inline bool lru_gen_add_rejected_folio(struct lruvec *lruvec, struct foli
 	return __lru_gen_add_folio(lruvec, folio, scanned_seq, false);
 }
 
-static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
+/*
+ * If @nr_taken is not NULL, the caller updates the node, memcg and zone LRU
+ * sizes for the folios it isolates: this updates lrugen->nr_pages and adds
+ * @folio's pages to @nr_taken[], indexed by whether its gen is active.
+ */
+static inline bool __lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
+				       bool reclaiming, long *nr_taken)
 {
 	unsigned long flags;
 	int gen = folio_lru_gen(folio);
@@ -392,12 +398,28 @@ static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio,
 	flags = set_mask_bits(folio_flags(folio, 0), LRU_GEN_MASK, flags);
 	gen = ((flags & LRU_GEN_MASK) >> LRU_GEN_PGOFF) - 1;
 
-	lru_gen_update_size(lruvec, folio, gen, -1);
+	if (nr_taken) {
+		struct lru_gen_folio *lrugen = &lruvec->lrugen;
+		int type = folio_is_file_lru(folio);
+		int zone = folio_zonenum(folio);
+		int delta = folio_nr_pages(folio);
+
+		WRITE_ONCE(lrugen->nr_pages[gen][type][zone],
+			   lrugen->nr_pages[gen][type][zone] - delta);
+		nr_taken[lru_gen_is_active(lruvec, gen)] += delta;
+	} else {
+		lru_gen_update_size(lruvec, folio, gen, -1);
+	}
 	list_del(&folio->lru);
 
 	return true;
 }
 
+static inline bool lru_gen_del_folio(struct lruvec *lruvec, struct folio *folio, bool reclaiming)
+{
+	return __lru_gen_del_folio(lruvec, folio, reclaiming, NULL);
+}
+
 /**
  * folio_migrate_lru_refs - copy the reference state to a new folio
  * @new: the destination folio
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 621214442f14..9dbdcda04f13 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -4732,7 +4732,8 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_c
 	return false;
 }
 
-static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct scan_control *sc)
+static bool isolate_folio(struct lruvec *lruvec, struct folio *folio,
+			  struct scan_control *sc, long *nr_taken)
 {
 	bool success;
 
@@ -4750,7 +4751,7 @@ static bool isolate_folio(struct lruvec *lruvec, struct folio *folio, struct sca
 	if (!folio_test_referenced(folio))
 		folio_set_lru_refs(folio, 0);
 
-	success = lru_gen_del_folio(lruvec, folio, true);
+	success = __lru_gen_del_folio(lruvec, folio, true, nr_taken);
 	VM_WARN_ON_ONCE_FOLIO(!success, folio);
 
 	return true;
@@ -4801,6 +4802,8 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 	for (zone_idx = sc->reclaim_idx; zone_idx >= 0; zone_idx--) {
 		LIST_HEAD(moved);
 		int skipped_zone = 0;
+		/* isolated pages, indexed by lru_gen_is_active() */
+		long nr_taken[2] = {};
 		struct zone *zone = &pgdat->node_zones[zone_idx];
 		struct list_head *head = &lrugen->folios[gen][type][zone_idx];
 
@@ -4819,7 +4822,7 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 			scanned += delta;
 			if (sort_folio(lruvec, folio, sc, gen, max_gen, tier))
 				sorted += delta;
-			else if (isolate_folio(lruvec, folio, sc)) {
+			else if (isolate_folio(lruvec, folio, sc, nr_taken)) {
 				list_add(&folio->lru, list);
 				isolated += delta;
 			} else {
@@ -4831,6 +4834,13 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
 				break;
 		}
 
+		/* Like update_lru_sizes(), once per zone instead of per folio */
+		if (nr_taken[0])
+			__update_lru_size(lruvec, type * LRU_FILE, zone_idx, -nr_taken[0]);
+		if (nr_taken[1])
+			__update_lru_size(lruvec, type * LRU_FILE + LRU_ACTIVE, zone_idx,
+					  -nr_taken[1]);
+
 		if (skipped_zone) {
 			list_splice(&moved, head);
 			__count_zid_vm_events(PGSCAN_SKIP, zone_idx, skipped_zone);
-- 
2.34.1



  reply	other threads:[~2026-11-27  6:03 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27  6:03 [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time in scan_folios() Ridong Chen
2026-11-27  6:03 ` Ridong Chen [this message]
2026-11-27  6:03 ` [RFC PATCH 2/2] mm/mglru: prefetch the previous folio " Ridong Chen
2026-11-27 19:34 ` [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time " Kairui Song
2026-11-28  1:00   ` Ridong Chen
2026-11-28  8:30     ` Kairui Song

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127060315.1119672-2-ridong.chen@linux.dev \
    --to=ridong.chen@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=axelrasmussen@google.com \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=baoquan.he@linux.dev \
    --cc=chenridong@xiaomi.com \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kasong@tencent.com \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=qi.zheng@linux.dev \
    --cc=rppt@kernel.org \
    --cc=shakeel.butt@linux.dev \
    --cc=vbabka@kernel.org \
    --cc=weixugc@google.com \
    --cc=yuanchu@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.