* [RFC PATCH 1/2] mm/mglru: update LRU sizes once per zone when isolating folios
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
2026-11-27 6:03 ` [RFC PATCH 2/2] mm/mglru: prefetch the previous folio in scan_folios() Ridong Chen
2026-11-27 19:34 ` [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time " Kairui Song
2 siblings, 0 replies; 4+ messages in thread
From: Ridong Chen @ 2026-11-27 6:03 UTC (permalink / raw)
To: Andrew Morton, Kairui Song, David Hildenbrand, Johannes Weiner
Cc: Barry Song, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie,
Wei Xu, Baoquan He, Baolin Wang, Liam R. Howlett, Vlastimil Babka,
Mike Rapoport, Michal Hocko,
open list:MEMORY MANAGEMENT - MGLRU (MULTI-GEN LRU), Ridong Chen,
Ridong Chen
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
^ permalink raw reply related [flat|nested] 4+ messages in thread* [RFC PATCH 2/2] mm/mglru: prefetch the previous folio in scan_folios()
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 ` [RFC PATCH 1/2] mm/mglru: update LRU sizes once per zone when isolating folios Ridong Chen
@ 2026-11-27 6:03 ` Ridong Chen
2026-11-27 19:34 ` [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time " Kairui Song
2 siblings, 0 replies; 4+ messages in thread
From: Ridong Chen @ 2026-11-27 6:03 UTC (permalink / raw)
To: Andrew Morton, Kairui Song, David Hildenbrand, Johannes Weiner
Cc: Barry Song, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie,
Wei Xu, Baoquan He, Baolin Wang, Liam R. Howlett, Vlastimil Babka,
Mike Rapoport, Michal Hocko,
open list:MEMORY MANAGEMENT - MGLRU (MULTI-GEN LRU), Ridong Chen,
Ridong Chen
From: Ridong Chen <chenridong@xiaomi.com>
scan_folios() walks a gen list from the tail under lru_lock. Isolating
a folio unlinks it, which writes the lru pointers of its neighbour, and
the next iteration then reads that neighbour's flags. Both miss in the
cache when many CPUs reclaim from the same lruvec, and the misses are
taken while other CPUs wait for the lock.
isolate_lru_folios() prefetches the previous folio for the same
reason, and inc_min_seq() prefetches the next one as it walks its list
from the head. Do the same in scan_folios(), which walks from the tail
like isolate_lru_folios().
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 425912 -0.2%
2 387442 392010 +1.2%
4 360312 365538 +1.5%
8 314549 322524 +2.5%
12 235058 245057 +4.3%
16 184086 200956 +9.2%
24 124478 145011 +16.5%
Signed-off-by: Ridong Chen <chenridong@xiaomi.com>
---
mm/vmscan.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 9dbdcda04f13..14f75c6fc4a8 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -4814,6 +4814,8 @@ static int scan_folios(unsigned long nr_to_scan, struct lruvec *lruvec,
struct folio *folio = lru_to_folio(head);
int delta = folio_nr_pages(folio);
+ prefetchw_prev_lru_folio(folio, head);
+
VM_WARN_ON_ONCE_FOLIO(folio_test_unevictable(folio), folio);
VM_WARN_ON_ONCE_FOLIO(folio_test_active(folio), folio);
VM_WARN_ON_ONCE_FOLIO(folio_is_file_lru(folio) != type, folio);
--
2.34.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time in scan_folios()
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 ` [RFC PATCH 1/2] mm/mglru: update LRU sizes once per zone when isolating folios Ridong Chen
2026-11-27 6:03 ` [RFC PATCH 2/2] mm/mglru: prefetch the previous folio in scan_folios() Ridong Chen
@ 2026-11-27 19:34 ` Kairui Song
2 siblings, 0 replies; 4+ messages in thread
From: Kairui Song @ 2026-11-27 19:34 UTC (permalink / raw)
To: Ridong Chen
Cc: Andrew Morton, Kairui Song, David Hildenbrand, Johannes Weiner,
Barry Song, Qi Zheng, Shakeel Butt, Axel Rasmussen, Yuanchu Xie,
Wei Xu, Baoquan He, Baolin Wang, Liam R. Howlett, Vlastimil Babka,
Mike Rapoport, Michal Hocko,
open list:MEMORY MANAGEMENT - MGLRU (MULTI-GEN LRU), Ridong Chen
On Fri, Nov 27, 2026 at 02:03:13PM +0800, Ridong Chen wrote:
> From: Ridong Chen <chenridong@xiaomi.com>
>
> When many CPUs reclaim from the same lruvec, they spend most of their
> time spinning on lru_lock, and the time scan_folios() holds the lock
> limits reclaim throughput. With 24 threads refaulting file pages in one
> memcg, about 40% of all cycles went to waiting for lru_lock here.
>
> This series takes per-folio work out of that critical section, along
> the lines of what isolate_lru_folios() already does for the classical
> LRU:
>
> 1. Update the node, memcg and zone LRU sizes once per zone instead of
> once per isolated folio.
>
> 2. Prefetch the previous folio, so the cache miss on its flags and list
> pointers is not taken while holding the lock.
>
> Tested on an i7-13700HX (8P+8E, 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 each patch in one
> kernel and the runs interleaved:
>
> CPUs base patch 1 patch 2 both
> 1 426681 -0.4% -0.2% +0.3%
> 2 387442 +0.7% +1.2% +1.4%
> 4 360312 +1.0% +1.5% +2.1%
> 8 314549 +2.5% +2.5% +3.3%
> 12 235058 +3.2% +4.3% +5.6%
> 16 184086 +9.4% +9.2% +11.2%
> 24 124478 +15.5% +16.5% +21.6%
>
> The gain grows with the number of CPUs reclaiming from one lruvec;
> with a few CPUs there is little contention and no measurable change.
>
> Ridong Chen (2):
> mm/mglru: update LRU sizes once per zone when isolating folios
> mm/mglru: prefetch the previous folio in scan_folios()
>
> include/linux/mm_inline.h | 26 ++++++++++++++++++++++++--
> mm/vmscan.c | 18 +++++++++++++++---
> 2 files changed, 39 insertions(+), 5 deletions(-)
Hi Ridong
I have a different approach proposed about two years ago:
https://lore.kernel.org/all/20240123184552.59758-1-ryncsn@gmail.com/
I hesitated to push that as the end to end gain was not
that large (5-10%) fio and it makes the code too complex.
But I think that one may work very well for microbench.
I tried to resurrect that 2 weeks ago, but realized it's not
the best time at the moment, there are too many things in flight
for MGLRU:
https://github.com/ryncsn/linux/commits/b4/mglru-isolation/
Perhaps we can discuss and see if using a "batch struct" could
fit better later? It should also unify the aging batch too.
^ permalink raw reply [flat|nested] 4+ messages in thread