* [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time in scan_folios()
@ 2026-11-27 6:03 Ridong Chen
2026-11-27 6:03 ` [RFC PATCH 1/2] mm/mglru: update LRU sizes once per zone when isolating folios Ridong Chen
` (2 more replies)
0 siblings, 3 replies; 10+ 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>
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(-)
base-commit: 3d27e49eb905a5119e78fbe3d3890f376d15c728
--
2.34.1
^ permalink raw reply [flat|nested] 10+ messages in thread* [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; 10+ 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] 10+ 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; 10+ 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] 10+ 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 2026-11-28 1:00 ` Ridong Chen 2 siblings, 1 reply; 10+ 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] 10+ messages in thread
* Re: [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time in scan_folios() 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 0 siblings, 1 reply; 10+ messages in thread From: Ridong Chen @ 2026-11-28 1:00 UTC (permalink / raw) To: Kairui Song 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 11/28/2026 3:34 AM, Kairui Song wrote: > 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. Hi Kairui, Thanks for the pointers. I'm fine with handling the batching together with your batch struct work later, and happy to fold the nr_taken[] part of patch 1 into it when the time comes. Patch 2 (prefetch) is independent of that, though. It is the one-liner Chris suggested for your v3 1/3, which he said he would ack if resent on its own [1]; the inc_min_seq() half of it has since been done by commit 5d3664f070f0 ("mm/mglru: move folios from oldest gen to second-oldest gen from head to tail"). It is a no-op on archs without prefetch, the same thing isolate_lru_folios() already does for the classical LRU, and it gave +16.5% alone with 24 CPUs in my test. Would you be OK with taking the prefetch patch now, with batching left for later? I can resend it as a standalone patch. -- Best regards Ridong ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time in scan_folios() 2026-11-28 1:00 ` Ridong Chen @ 2026-11-28 8:30 ` Kairui Song 2026-12-04 10:36 ` Ridong Chen 0 siblings, 1 reply; 10+ messages in thread From: Kairui Song @ 2026-11-28 8:30 UTC (permalink / raw) To: Ridong Chen Cc: Andrew Morton, 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 Sat, Nov 28, 2026 at 9:00 AM Ridong Chen <ridong.chen@linux.dev> wrote: > > > > On 11/28/2026 3:34 AM, Kairui Song wrote: > > 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. > > Hi Kairui, > > Thanks for the pointers. > > I'm fine with handling the batching together with your batch struct > work later, and happy to fold the nr_taken[] part of patch 1 into it > when the time comes. > > Patch 2 (prefetch) is independent of that, though. It is the > one-liner Chris suggested for your v3 1/3, which he said he would ack > if resent on its own [1]; the inc_min_seq() half of it has since been > done by commit 5d3664f070f0 ("mm/mglru: move folios from oldest gen to > second-oldest gen from head to tail"). It is a no-op on archs without > prefetch, the same thing isolate_lru_folios() already does for the > classical LRU, and it gave +16.5% alone with 24 CPUs in my test. I'm actually a bit skeptical about the real effect of this, and the previous work for inc_min_seq... for example if you read: https://lwn.net/Articles/444336/ In my previous attempt 2 years ago, I did more than just synthetic microbenchmarks, on three different CPUs, 5900HX, i7-9700, 8255C and 7K62. The results changed dramatically; it's not a x86 vs ARM thing, but is strongly affected by the actual microarchitecture design (which is not surprising). So my conclusion was that it isn't worth the complexity, especially considering that modern hardware branch prediction is much smarter and memory pressure is mostly random. In the longterm we should work on getting rid of the LRU "as a double linked list". The branch check may even bring negative effects, IMO. > > Would you be OK with taking the prefetch patch now, with batching left > for later? I can resend it as a standalone patch. But that sounds ok to me, it wasn't too ugly now considering we already have all the wheels. If that solves the problem for you, let's have it. For the batching part my concern is that how should we balance the stack usage and batching size, vmscan have a bad history of causing too many stack using, with a batch on stack it will get worse, perhaps we should put it somewhere else, but I think this can be done later too, after FG maybe. ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time in scan_folios() 2026-11-28 8:30 ` Kairui Song @ 2026-12-04 10:36 ` Ridong Chen 2026-12-04 10:51 ` Barry Song 0 siblings, 1 reply; 10+ messages in thread From: Ridong Chen @ 2026-12-04 10:36 UTC (permalink / raw) To: Kairui Song Cc: Andrew Morton, 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 11/28/2026 4:30 PM, Kairui Song wrote: > On Sat, Nov 28, 2026 at 9:00 AM Ridong Chen <ridong.chen@linux.dev> wrote: >> >> >> >> On 11/28/2026 3:34 AM, Kairui Song wrote: >>> 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. >> >> Hi Kairui, >> >> Thanks for the pointers. >> >> I'm fine with handling the batching together with your batch struct >> work later, and happy to fold the nr_taken[] part of patch 1 into it >> when the time comes. >> >> Patch 2 (prefetch) is independent of that, though. It is the >> one-liner Chris suggested for your v3 1/3, which he said he would ack >> if resent on its own [1]; the inc_min_seq() half of it has since been >> done by commit 5d3664f070f0 ("mm/mglru: move folios from oldest gen to >> second-oldest gen from head to tail"). It is a no-op on archs without >> prefetch, the same thing isolate_lru_folios() already does for the >> classical LRU, and it gave +16.5% alone with 24 CPUs in my test. > > I'm actually a bit skeptical about the real effect of this, and the > previous work for inc_min_seq... for example if you read: > https://lwn.net/Articles/444336/ > > In my previous attempt 2 years ago, I did more than just synthetic > microbenchmarks, on three different CPUs, 5900HX, i7-9700, 8255C and > 7K62. The results changed dramatically; it's not a x86 vs ARM thing, > but is strongly affected by the actual microarchitecture design (which > is not surprising). So my conclusion was that it isn't worth the > complexity, especially considering that modern hardware branch > prediction is much smarter and memory pressure is mostly random. In > the longterm we should work on getting rid of the LRU "as a double > linked list". The branch check may even bring negative effects, IMO. > >> >> Would you be OK with taking the prefetch patch now, with batching left >> for later? I can resend it as a standalone patch. > > But that sounds ok to me, it wasn't too ugly now considering we > already have all the wheels. If that solves the problem for you, let's > have it. > Hi Kairui, Sorry for the late reply. I've been looking at MGLRU direct reclaim tail latency on Android. When many tasks enter direct reclaim at the same time they pile up on the same memcgs and slow each other down [1], so the time spent under lru_lock matters there. To check the prefetch on arm64, I ran the same kind of microbenchmark on an 8-CPU tablet: random 4K pread() on files 3x the memcg limit, one thread per CPU, all threads in one memcg, base and patched interleaved in one boot, 5 rounds each. Reads per second per thread: threads base prefetch 1 180688 +1.5% 2 189663 +2.0% 4 181787 +3.2% 6 168018 +3.8% 8 146523 +6.1% From 4 threads on, every round was faster, and there was no regression with 1 thread. So the gain is there on arm64 too, and the change is a single line, the same as isolate_lru_folios() already does. I'll resend the prefetch patch on its own and leave the batching for your batch struct work. [1] https://lore.kernel.org/linux-mm/20261119065043.398-1-ridong.chen@linux.dev/ > For the batching part my concern is that how should we balance the > stack usage and batching size, vmscan have a bad history of causing > too many stack using, with a batch on stack it will get worse, perhaps > we should put it somewhere else, but I think this can be done later > too, after FG maybe. -- Best regards Ridong ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time in scan_folios() 2026-12-04 10:36 ` Ridong Chen @ 2026-12-04 10:51 ` Barry Song 2026-12-04 11:00 ` Ridong Chen 0 siblings, 1 reply; 10+ messages in thread From: Barry Song @ 2026-12-04 10:51 UTC (permalink / raw) To: Ridong Chen Cc: Kairui Song, Andrew Morton, David Hildenbrand, Johannes Weiner, 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, Dec 4, 2026 at 6:36 PM Ridong Chen <ridong.chen@linux.dev> wrote: [...] > > > > Hi Kairui, > > Sorry for the late reply. > > I've been looking at MGLRU direct reclaim tail latency on Android. > When many tasks enter direct reclaim at the same time they pile up on > the same memcgs and slow each other down [1], so the time spent under > lru_lock matters there. > > To check the prefetch on arm64, I ran the same kind of microbenchmark > on an 8-CPU tablet: random 4K pread() on files 3x the memcg limit, > one thread per CPU, all threads in one memcg, base and patched > interleaved in one boot, 5 rounds each. Reads per second per thread: > > threads base prefetch > 1 180688 +1.5% > 2 189663 +2.0% > 4 181787 +3.2% > 6 168018 +3.8% > 8 146523 +6.1% > > From 4 threads on, every round was faster, and there was no regression > with 1 thread. I think this one-line change is fine if the data supports it. Usually, though, this kind of optimization warrants an explanation at the microarchitectural level, for example, using the top-down analysis model to identify where the CPU stalls. I assume prefetching reduces memory-bound stalls, but what I don't quite understand is why it provides a greater benefit in multithreaded workloads. Could this be due to increased cache contention and more frequent cache evictions? Would it be possible to perform a top-down analysis [1], at least on x86 if that's easier, to gain a deeper understanding of the performance improvement? [1] https://github.com/andikleen/pmu-tools Thanks, Barry ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time in scan_folios() 2026-12-04 10:51 ` Barry Song @ 2026-12-04 11:00 ` Ridong Chen 2026-12-04 11:12 ` Barry Song 0 siblings, 1 reply; 10+ messages in thread From: Ridong Chen @ 2026-12-04 11:00 UTC (permalink / raw) To: Barry Song Cc: Kairui Song, Andrew Morton, David Hildenbrand, Johannes Weiner, 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 12/4/2026 6:51 PM, Barry Song wrote: > On Fri, Dec 4, 2026 at 6:36 PM Ridong Chen <ridong.chen@linux.dev> wrote: > [...] >>> >> >> Hi Kairui, >> >> Sorry for the late reply. >> >> I've been looking at MGLRU direct reclaim tail latency on Android. >> When many tasks enter direct reclaim at the same time they pile up on >> the same memcgs and slow each other down [1], so the time spent under >> lru_lock matters there. >> >> To check the prefetch on arm64, I ran the same kind of microbenchmark >> on an 8-CPU tablet: random 4K pread() on files 3x the memcg limit, >> one thread per CPU, all threads in one memcg, base and patched >> interleaved in one boot, 5 rounds each. Reads per second per thread: >> >> threads base prefetch >> 1 180688 +1.5% >> 2 189663 +2.0% >> 4 181787 +3.2% >> 6 168018 +3.8% >> 8 146523 +6.1% >> >> From 4 threads on, every round was faster, and there was no regression >> with 1 thread. > > I think this one-line change is fine if the data supports it. > Usually, though, this kind of optimization warrants an explanation > at the microarchitectural level, for example, using the top-down > analysis model to identify where the CPU stalls. > > I assume prefetching reduces memory-bound stalls, but what I don't > quite understand is why it provides a greater benefit in > multithreaded workloads. Could this be due to increased cache > contention and more frequent cache evictions? > The benchmark is concurrent reclaim on one memcg: all threads reclaim from the same lruvec and serialize on lruvec_lock_irq(). The prefetch shortens the critical section in scan_folios(), so the gain is in how long the other reclaimers wait for the lock, not in the reclaimer itself. With one thread nobody waits, so there is no change. With more threads, the more of them wait, the bigger the gain. > Would it be possible to perform a top-down analysis [1], at least > on x86 if that's easier, to gain a deeper understanding of the > performance improvement? > > [1] https://github.com/andikleen/pmu-tools > > Thanks, > Barry -- Best regards Ridong ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [RFC PATCH 0/2] mm/mglru: shorten the lru_lock hold time in scan_folios() 2026-12-04 11:00 ` Ridong Chen @ 2026-12-04 11:12 ` Barry Song 0 siblings, 0 replies; 10+ messages in thread From: Barry Song @ 2026-12-04 11:12 UTC (permalink / raw) To: Ridong Chen Cc: Kairui Song, Andrew Morton, David Hildenbrand, Johannes Weiner, 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, Dec 4, 2026 at 7:00 PM Ridong Chen <ridong.chen@linux.dev> wrote: > > > > On 12/4/2026 6:51 PM, Barry Song wrote: > > On Fri, Dec 4, 2026 at 6:36 PM Ridong Chen <ridong.chen@linux.dev> wrote: > > [...] > >>> > >> > >> Hi Kairui, > >> > >> Sorry for the late reply. > >> > >> I've been looking at MGLRU direct reclaim tail latency on Android. > >> When many tasks enter direct reclaim at the same time they pile up on > >> the same memcgs and slow each other down [1], so the time spent under > >> lru_lock matters there. > >> > >> To check the prefetch on arm64, I ran the same kind of microbenchmark > >> on an 8-CPU tablet: random 4K pread() on files 3x the memcg limit, > >> one thread per CPU, all threads in one memcg, base and patched > >> interleaved in one boot, 5 rounds each. Reads per second per thread: > >> > >> threads base prefetch > >> 1 180688 +1.5% > >> 2 189663 +2.0% > >> 4 181787 +3.2% > >> 6 168018 +3.8% > >> 8 146523 +6.1% > >> > >> From 4 threads on, every round was faster, and there was no regression > >> with 1 thread. > > > > I think this one-line change is fine if the data supports it. > > Usually, though, this kind of optimization warrants an explanation > > at the microarchitectural level, for example, using the top-down > > analysis model to identify where the CPU stalls. > > > > I assume prefetching reduces memory-bound stalls, but what I don't > > quite understand is why it provides a greater benefit in > > multithreaded workloads. Could this be due to increased cache > > contention and more frequent cache evictions? > > > > The benchmark is concurrent reclaim on one memcg: all threads reclaim > from the same lruvec and serialize on lruvec_lock_irq(). The prefetch > shortens the critical section in scan_folios(), so the gain is in how > long the other reclaimers wait for the lock, not in the reclaimer > itself. With one thread nobody waits, so there is no change. With > more threads, the more of them wait, the bigger the gain. Makes sense to me. So, in the single-threaded case, prefetching reduces memory-bound stalls, while in the multithreaded case, shorter lock hold times also help reduce lock contention. Perhaps you could use toplev.py to measure memory-bound stalls in the single-threaded case and perf lock contention to quantify lock contention in the multithreaded case. I think people would appreciate seeing these results in the changelog. Personally, I'm quite interested in microarchitectural performance analysis using the top-down model. If collecting these data doesn't take too much time (and I suspect it won't), I'd appreciate it if you could include the results in the changelog when posting this as a standalone patch :-) Thanks, Barry ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-12-04 11:12 UTC | newest] Thread overview: 10+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 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 ` [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 2026-12-04 10:36 ` Ridong Chen 2026-12-04 10:51 ` Barry Song 2026-12-04 11:00 ` Ridong Chen 2026-12-04 11:12 ` Barry Song
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox