From: Ridong Chen 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 --- 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