mm/mglru: shorten the lru_lock hold time in scan_folios()
Subsystems (4)
cover mm/mglru: shorten the lru_lock hold time in scan_folios()
From: Ridong Chen <[email protected]>
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
Signature · 1 line
2.34.1
RFC PATCH 1/2 mm/mglru: update LRU sizes once per zone when isolating folios
From: Ridong Chen <[email protected]>
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.
--- include/linux/mm_inline.h | 26 ++++++++++++++++++++++++-- mm/vmscan.c | 16 +++++++++++++--- 2 files changed, 37 insertions(+), 5 deletions(-)
include/linux/mm_inline.h +24 -2
@@ -376,7 +376,13 @@ static inline bool lru_gen_add_rejected_folio(struct lruvec *lruvec, struct folireturn __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
mm/vmscan.c +13 -3
@@ -4732,7 +4732,8 @@ static bool sort_folio(struct lruvec *lruvec, struct folio *folio, struct scan_creturn 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 scaif (!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);
Signature · 1 line
2.34.1
RFC PATCH 2/2 mm/mglru: prefetch the previous folio in scan_folios()
In reply to Ridong ChenFrom: Ridong Chen <[email protected]>
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%--- mm/vmscan.c | 2 ++ 1 file changed, 2 insertions(+)
mm/vmscan.c +2 -0
@@ -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);
Signature · 1 line
2.34.1
Quoted 36 lines · Ridong Chen
From: Ridong Chen <[email protected]>
When many CPUs reclaim from the same lruvec, they spend most of their time spinning on
lru_lock, and the timescan_folios()holds the lock limits reclaim throughput. With 24 threads refaulting file pages in one memcg, about 40% of all cycles went to waiting forlru_lockhere.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.
Quoted 7 lines
Ridong Chen (2):
mm/mglru: update LRU sizes once per zone when isolating folios mm/mglru: prefetch the previous folio inscan_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/[email protected]/
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.
Quoted 55 lines · Kairui Song
Quoted 36 lines · Ridong Chen
From: Ridong Chen <[email protected]>
When many CPUs reclaim from the same lruvec, they spend most of their time spinning on
lru_lock, and the timescan_folios()holds the lock limits reclaim throughput. With 24 threads refaulting file pages in one memcg, about 40% of all cycles went to waiting forlru_lockhere.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.
Quoted 7 lines
Ridong Chen (2):
mm/mglru: update LRU sizes once per zone when isolating folios mm/mglru: prefetch the previous folio inscan_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/[email protected]/
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.
Quoted 8 lines
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.
Signature · 2 lines
Best regards Ridong
Quoted 76 lines · Ridong Chen
Quoted 55 lines · Kairui Song
Quoted 36 lines · Ridong Chen
From: Ridong Chen <[email protected]>
When many CPUs reclaim from the same lruvec, they spend most of their time spinning on
lru_lock, and the timescan_folios()holds the lock limits reclaim throughput. With 24 threads refaulting file pages in one memcg, about 40% of all cycles went to waiting forlru_lockhere.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.
Quoted 7 lines
Ridong Chen (2):
mm/mglru: update LRU sizes once per zone when isolating folios mm/mglru: prefetch the previous folio inscan_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/[email protected]/
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.
Quoted 8 lines
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.
Quoted 7 lines
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 commit5d3664f070f0("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 thingisolate_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.
Quoted 3 lines
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.