Skip to contentSkip to conversation

skip empty PUD subtrees during aging with a PUD-level Bloom filter

PATCH In review Baoquan He 3 participants 15 messages Nov 23 – Nov 25

Subsystems (5)
lore
Share & tools
lore

Baoquan He

author reviewer
#0 lore raw

v5 cover skip empty PUD subtrees during aging with a PUD-level Bloom filter

Problem
=======

MGLRU's aging walks every PUD of every mm in the mm_list. For a PUD whose 512 PMDs all fail the existing PMD-level Bloom filter, the walker still descends into the PUD and iterates all 512 PMD entries. When an mm has no pages on a given node this is structural waste: lru_gen_use_mm() sets mm->lru_gen.bitmap to -1 (all nodes) on every context switch, so the aging walker of *every* node, e.g kswapd, direct reclaim, proactive aging walks the same mm.

On a 2-node KVM guest with the workload pinned to node 0, node 1's aging walks of those mms scan 1,362 leaf entries per walk while finding no young folio for node 1's lruvec, and 94% of those walks are entirely empty.

Approach
========

Add a PUD-level Bloom filter, one level above the existing PMD filter. The walker now:

1. Tests the PUD filter before descending into a PUD subtree.
2. If the PUD had nothing young for this lruvec last generation, skips the whole 1 GiB region -- avoiding 512 PMD lookups.
3. walk_pmd_range() reports whether it found anything young; walk_pud_range() records that in the double-buffered PUD filter.
4. The rmap feedback path marks hot PUDs, so regions whose hotness is only observed by eviction are re-examined promptly. This is done for both mapping types: lru_gen_look_around() covers PTE-mapped folios, and folio_referenced_one() covers PMD-mapped ones, which never reach lru_gen_look_around().
5. force_scan walks bypass the PUD test, so manual aging and newly added mms always rescan and re-populate the filter.

The filter key is (mm, PUD-aligned address), as suggested by Kairui. It is a stable identity of the range on both sides of the interface: the aging walk computes it from args->mm and addr, the rmap path from vma->vm_mm and the address. So the feedback path needs no page-table walk at all.

The double-buffered filter flips each generation and force_scan bypasses it, so a missed re-mark costs at most one generation, and reclaim is still safe for a wrongly skipped subtree because folio_referenced() consults the PTE young bits, not the filter.

Change log
==========
v4 -> v5:

- Rebased onto the latest mm-new commit 7d1a16abe4cc.

- Drop MM_LEAF_ASSOCIATED: an empty walk is now "MM_LEAF_TOTAL > 0 and MM_LEAF_YOUNG == 0". MM_LEAF_YOUNG already means the young leaves of this lruvec, which is what the PUD filter needs, so the second counter added nothing. The debugfs columns and the tracepoint field are renamed to match. (Sashiko)

Test
====

8 GiB KVM guest, 4 vCPU, two 4 GiB NUMA nodes, 7.4.0-rc3 with the series. "base" below is the same kernel with the filter disabled (patch 1 only):
same .config, same workload. Each result directory records its own cmdline and per-node meminfo, so a wrong configuration would show.

1) Cross-node empty walks. 16 workers x 128 MiB pinned to node 0 (numactl --membind=0), so node 1 owns none of these pages. 100 aging passes, both nodes aged, 3 runs, median:

base filter change
node 1 leaf entries scanned 1,243 107 -91%
walks with leaves 17 0.67 -96%
PUDs skipped 0 51
node 0 leaf entries scanned 12,926 13,155 +1.8%
young leaves 9,228 9,167 -0.7%
worker RSS delta 0% 0%

The base's own three runs spread 9,133 to 9,248 young leaves on node 0, so node 0 keeps its young leaves. The filter marks young leaves in exactly the same place it did before; what it drops is the walk into PUDs that had nothing young.

     forced pass, node 1 leaf/walk
       before the filtered passes   1,359-1,371  1,363-1,370
       after them                   1,361-1,371  1,363-1,370

A forced pass bypasses the filter, so it costs the same on both kernels and the same before and after 100 filtered passes: the filter is a cache, not a state.

2) Correctness, 11 checks, all pass: a cold PUD is skipped; touching it does not revive it; force_scan re-marks it and the next pass walks it again; a munmap + mmap at the same address shows no stale suppression; a 1 GiB range migrated from node 0 to node 1 is re-scanned by node 1 after one forced pass; no premature reclaim in any of them; dmesg clean.

3) A PUD that holds only PMD-mapped huge pages. 1 GiB is mapped with THP=always (AnonHugePages = 1 GiB, 262,144 of 262,144 pages resident) and aged until its PUD is skipped (S +4 over 3 passes). The memcg is then put under reclaim pressure (memory.max below the current usage) and the range is touched again, so folio_referenced() runs on PMD-mapped folios. The next normal aging pass walks the range again:

     leaf_total 1560,  leaf_young 387

4) Reclaim pressure. Four 512 MiB workers in a memory.max=2G/swap.max=1G memcg, at the limit so reclaim never stops; each flips a 256 MiB half every second. Both nodes are aged every 0.5 s for 600 s. Rates per second, because some runs stopped early on both kernels:

                             base                filter
     pgscan_anon          23.7-27.4k/s (3)    34.6-35.3k/s (2)
     pgsteal_anon          3.95-4.10k/s        3.74-4.63k/s
     pswpout               3.94-4.10k/s        3.74-4.61k/s
     refault_anon          3.95-4.10k/s        3.74-4.63k/s
     oom_kill              0                   0
     hot half resident     262,140-262,144     262,144
     node 1 walks w/ leaves  2,812-4,544          4

Evictions, swap-out, refaults and residency are the same. The scanner examines 26-49% more folios with the filter: a folio under a skipped PUD is not promoted that generation, so it sits in an older generation a little longer and takes more scanning to reach. Extra scanning, not extra reclaim. The per-walk statistics cannot cause it, nothing outside lru_gen_seq_show() reads them.

5) Aging cost on a single node. Per pass, 200 passes, perf stat and the median wall time around the same fork-free debugfs write:

shape base filter change
2 procs x 32 GiB VA, 0.2% touched
time per pass 1,577-1,596 1,278-1,392 us -15%
instructions per pass 5.79M 4.27M -26%
20 procs x 3 GiB VA, 2% touched
time per pass 9,889-10,097 9,563-9,775 us -4%
instructions per pass 29.6M 28.1M -5%
20 procs x 64 MiB, fully touched
time per pass 10,294-10,579 10,167-10,411 us -2%
instructions per pass 30.06M 30.05M 0%

The leaf counters are the same in all three rows; only the skips differ. The wide shape has 34 PUD regions per mm and skips all of them, so it is 1.5M instructions lighter per pass, about 22,000 per skipped PUD, which is the 512 PMD-level Bloom tests it no longer runs. With 5 and 1 PUD regions per mm there is little to skip, so the other two cost nothing.

Baoquan He (4):
mm/mglru: add MM_WALK_EMPTY stats and tracepoint mm/mglru: add PUD-level Bloom filter state and generic helpers mm/mglru: skip cold PUD subtrees during aging mm/mglru: count PUD subtrees skipped by the PUD-level filter

 include/linux/mmzone.h |  20 ++++-
 mm/rmap.c              |   5 +-
 mm/trace_vmscan.h      |  30 ++++++++
 mm/vmscan.c            | 169 +++++++++++++++++++++++++++++++++--------
 4 files changed, 189 insertions(+), 35 deletions(-)

base-commit: 7d1a16abe4cc5dccd21e3d6262e44433a5885d27

Signature · 1 line
2.52.0
author reviewer
#1 lore raw

PATCH v5 1/4 mm/mglru: add MM_WALK_EMPTY stats and tracepoint

From: Baoquan He <[email protected]>

Add per-walk counters to measure empty aging walks, which traverse an mm's page tables but find no young leaf for the current lruvec (node+memcg). These are common on multi-NUMA node systems because lru_gen_use_mm() marks an mm for all nodes at every context switch.

New counters (accumulated in mm_state->stats[]):

  MM_WALK_TOTAL        - page-table walks completed
  MM_WALK_EMPTY        - walks that found no young leaf for this lruvec
  MM_LEAF_EMPTY_WALKS  - leaf entries scanned during empty walks

A walk is empty when MM_LEAF_TOTAL > 0 and MM_LEAF_YOUNG == 0. MM_LEAF_YOUNG already counts only the young leaves whose folio is in this lruvec, which is the condition the PUD-level filter of the following patches uses.

The new tracepoint mm_vmscan_lru_gen_walk() fires after each walk, and the debugfs lru_gen output ("TYFAWEE") exposes the counters.

---
 include/linux/mmzone.h |  3 +++
 mm/trace_vmscan.h      | 28 ++++++++++++++++++++++++++++
 mm/vmscan.c            | 32 ++++++++++++++++++++++++++++----
 3 files changed, 59 insertions(+), 4 deletions(-)
include/linux/mmzone.h +3 -0
index 6a2ad0cff795..8aabe0f23e60 100644
@@ -579,6 +579,9 @@ enum {
 	MM_LEAF_YOUNG,		/* young leaf entries */
 	MM_NONLEAF_FOUND,	/* non-leaf entries found in Bloom filters */
 	MM_NONLEAF_ADDED,	/* non-leaf entries added to Bloom filters */
+	MM_WALK_TOTAL,		/* page-table walks completed */
+	MM_WALK_EMPTY,		/* walks that found no young leaf for this lruvec */
+	MM_LEAF_EMPTY_WALKS,	/* leaf entries scanned during empty walks */
 	NR_MM_STATS
 };
 
mm/trace_vmscan.h +28 -0
index 9dafdb5bd3ce..b0c2af65c1ea 100644
@@ -508,6 +508,34 @@ TRACE_EVENT(mm_mglru_aging,
 		__print_array(__entry->nr_anon, MAX_NR_GENS, sizeof(unsigned long)),
 		__print_array(__entry->nr_file, MAX_NR_GENS, sizeof(unsigned long)))
 );
+TRACE_EVENT(mm_vmscan_lru_gen_walk,
+
+	TP_PROTO(int nid, unsigned long seq, int leaf_total,
+		 int leaf_young, bool empty),
+
+	TP_ARGS(nid, seq, leaf_total, leaf_young, empty),
+
+	TP_STRUCT__entry(
+		__field(int, nid)
+		__field(unsigned long, seq)
+		__field(int, leaf_total)
+		__field(int, leaf_young)
+		__field(bool, empty)
+	),
+
+	TP_fast_assign(
+		__entry->nid = nid;
+		__entry->seq = seq;
+		__entry->leaf_total = leaf_total;
+		__entry->leaf_young = leaf_young;
+		__entry->empty = empty;
+	),
+
+	TP_printk("nid=%d seq=%lu leaf_total=%d leaf_young=%d empty=%d",
+		__entry->nid, __entry->seq, __entry->leaf_total,
+		__entry->leaf_young, __entry->empty)
+);
+
 #endif /* CONFIG_LRU_GEN */
 
 TRACE_EVENT(mm_vmscan_write_folio,
mm/vmscan.c +28 -4
index bc7579d47f9b..a0cbbbb2ff00 100644
@@ -4158,8 +4158,32 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq,
 
 	do {
 		success = iterate_mm_list(walk, &mm);
-		if (mm)
+		if (mm) {
+			bool empty = false;
+
 			walk_mm(mm, walk);
+			/*
+			 * A walk that traversed page tables but found no young
+			 * leaf for this lruvec (node+memcg) is pure waste: the
+			 * PUD-level filter will skip it next generation.
+			 * MM_LEAF_YOUNG only counts leaves whose folio belongs
+			 * to this lruvec, so it is the condition the filter
+			 * uses as well.
+			 */
+			if (walk->mm_stats[MM_LEAF_TOTAL]) {
+				walk->mm_stats[MM_WALK_TOTAL]++;
+				if (walk->mm_stats[MM_LEAF_YOUNG] == 0) {
+					walk->mm_stats[MM_WALK_EMPTY]++;
+					walk->mm_stats[MM_LEAF_EMPTY_WALKS] +=
+						walk->mm_stats[MM_LEAF_TOTAL];
+					empty = true;
+				}
+			}
+			trace_mm_vmscan_lru_gen_walk(
+					lruvec_pgdat(lruvec)->node_id, walk->seq,
+					walk->mm_stats[MM_LEAF_TOTAL],
+					walk->mm_stats[MM_LEAF_YOUNG], empty);
+		}
 	} while (mm);
 done:
 	if (success) {
@@ -5804,14 +5828,14 @@ static void lru_gen_seq_show_full(struct seq_file *m, struct lruvec *lruvec,
 
 	seq_puts(m, "                      ");
 	for (i = 0; i < NR_MM_STATS; i++) {
-		const char *s = "xxxx";
+		const char *s = "xxxxxxxx";
 		unsigned long n = 0;
 
 		if (seq == max_seq && NR_HIST_GENS == 1) {
-			s = "TYFA";
+			s = "TYFAWEE";
 			n = READ_ONCE(mm_state->stats[hist][i]);
 		} else if (seq != max_seq && NR_HIST_GENS > 1) {
-			s = "tyfa";
+			s = "tyfawee";
 			n = READ_ONCE(mm_state->stats[hist][i]);
 		}
 
Signature · 1 line
2.52.0
author reviewer
#2 lore raw

PATCH v5 2/4 mm/mglru: add PUD-level Bloom filter state and generic helpers

In reply to Baoquan He

From: Baoquan He <[email protected]>

Generalize the Bloom filter state and helpers so that another filter level can reuse the same hash, double-buffering and reset logic.

Replace the PMD-level filter array with a two-dimensional filters array indexed by level, and add BLOOM_FILTER_PUD and BLOOM_FILTER_PMD. The PUD-level filters work one level up: when all PMDs under a PUD fail the PMD-level filter, the whole PMD iteration can be skipped. Cross-node empty walks are the case where this wastes the most time.

test_bloom_filter(), update_bloom_filter() and reset_bloom_filter() take the level and use mm_state->filters[level][gen] directly.

No behavior change yet, and no PUD-level bitmap is allocated.

---
 include/linux/mmzone.h | 10 ++++++++--
 mm/vmscan.c            | 39 ++++++++++++++++++++++-----------------
 2 files changed, 30 insertions(+), 19 deletions(-)
include/linux/mmzone.h +8 -2
index 8aabe0f23e60..f057c98d1f97 100644
@@ -588,6 +588,12 @@ enum {
 /* double-buffering Bloom filters */
 #define NR_BLOOM_FILTERS	2
 
+enum bloom_filter_level {
+	BLOOM_FILTER_PUD,
+	BLOOM_FILTER_PMD,
+	BLOOM_FILTER_LEVELS,
+};
+
 struct lru_gen_mm_state {
 	/* synced with max_seq after each iteration */
 	unsigned long seq;
@@ -595,8 +601,8 @@ struct lru_gen_mm_state {
 	struct list_head *head;
 	/* where the last iteration ended before */
 	struct list_head *tail;
-	/* Bloom filters flip after each iteration */
-	unsigned long *filters[NR_BLOOM_FILTERS];
+	/* Bloom filters flip after each iteration, indexed by level */
+	unsigned long *filters[BLOOM_FILTER_LEVELS][NR_BLOOM_FILTERS];
 	/* the mm stats for debugging */
 	unsigned long stats[NR_HIST_GENS][NR_MM_STATS];
 };
mm/vmscan.c +22 -17
index a0cbbbb2ff00..7d20ee1b6c10 100644
@@ -2819,14 +2819,14 @@ static void get_item_key(void *item, int *key)
 	key[1] = hash >> BLOOM_FILTER_SHIFT;
 }
 
-static bool test_bloom_filter(struct lru_gen_mm_state *mm_state, unsigned long seq,
-			      void *item)
+static bool test_bloom_filter(struct lru_gen_mm_state *mm_state, int level,
+			      unsigned long seq, void *item)
 {
 	int key[2];
 	unsigned long *filter;
 	int gen = filter_gen_from_seq(seq);
 
-	filter = READ_ONCE(mm_state->filters[gen]);
+	filter = READ_ONCE(mm_state->filters[level][gen]);
 	if (!filter)
 		return true;
 
@@ -2835,14 +2835,14 @@ static bool test_bloom_filter(struct lru_gen_mm_state *mm_state, unsigned long s
 	return test_bit(key[0], filter) && test_bit(key[1], filter);
 }
 
-static void update_bloom_filter(struct lru_gen_mm_state *mm_state, unsigned long seq,
-				void *item)
+static void update_bloom_filter(struct lru_gen_mm_state *mm_state, int level,
+				unsigned long seq, void *item)
 {
 	int key[2];
 	unsigned long *filter;
 	int gen = filter_gen_from_seq(seq);
 
-	filter = READ_ONCE(mm_state->filters[gen]);
+	filter = READ_ONCE(mm_state->filters[level][gen]);
 	if (!filter)
 		return;
 
@@ -2854,12 +2854,13 @@ static void update_bloom_filter(struct lru_gen_mm_state *mm_state, unsigned long
 		set_bit(key[1], filter);
 }
 
-static void reset_bloom_filter(struct lru_gen_mm_state *mm_state, unsigned long seq)
+static void reset_bloom_filter(struct lru_gen_mm_state *mm_state, int level,
+			       unsigned long seq)
 {
 	unsigned long *filter;
 	int gen = filter_gen_from_seq(seq);
 
-	filter = mm_state->filters[gen];
+	filter = mm_state->filters[level][gen];
 	if (filter) {
 		bitmap_clear(filter, 0, BIT(BLOOM_FILTER_SHIFT));
 		return;
@@ -2867,7 +2868,7 @@ static void reset_bloom_filter(struct lru_gen_mm_state *mm_state, unsigned long
 
 	filter = bitmap_zalloc(BIT(BLOOM_FILTER_SHIFT),
 			       __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);
-	WRITE_ONCE(mm_state->filters[gen], filter);
+	WRITE_ONCE(mm_state->filters[level][gen], filter);
 }
 
 /******************************************************************************
@@ -3110,7 +3111,7 @@ static bool iterate_mm_list(struct lru_gen_mm_walk *walk, struct mm_struct **ite
 	spin_unlock(&mm_list->lock);
 
 	if (mm && first)
-		reset_bloom_filter(mm_state, walk->seq + 1);
+		reset_bloom_filter(mm_state, BLOOM_FILTER_PMD, walk->seq + 1);
 
 	if (*iter)
 		mmdrop(*iter);
@@ -3761,7 +3762,8 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
 			walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);
 		}
 
-		if (!walk->force_scan && !test_bloom_filter(mm_state, walk->seq, pmd + i))
+		if (!walk->force_scan &&
+		    !test_bloom_filter(mm_state, BLOOM_FILTER_PMD, walk->seq, pmd + i))
 			continue;
 
 		walk->mm_stats[MM_NONLEAF_FOUND]++;
@@ -3772,7 +3774,7 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
 		walk->mm_stats[MM_NONLEAF_ADDED]++;
 
 		/* carry over to the next generation */
-		update_bloom_filter(mm_state, walk->seq + 1, pmd + i);
+		update_bloom_filter(mm_state, BLOOM_FILTER_PMD, walk->seq + 1, pmd + i);
 	}
 
 	walk_pmd_range_locked(pud, -1, vma, args, bitmap, &first);
@@ -4434,8 +4436,8 @@ bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr)
 
 	/* feedback from rmap walkers to page table walkers */
 	if (mm_state && suitable_to_scan(i, young))
-		update_bloom_filter(mm_state, READ_ONCE(lruvec->lrugen.max_seq),
-				    pvmw->pmd);
+		update_bloom_filter(mm_state, BLOOM_FILTER_PMD,
+				    READ_ONCE(lruvec->lrugen.max_seq), pvmw->pmd);
 
 	mem_cgroup_put(memcg);
 
@@ -6151,6 +6153,7 @@ void lru_gen_init_memcg(struct mem_cgroup *memcg)
 void lru_gen_exit_memcg(struct mem_cgroup *memcg)
 {
 	int i;
+	int level;
 	int nid;
 	struct lru_gen_mm_list *mm_list = get_mm_list(memcg);
 
@@ -6168,9 +6171,11 @@ void lru_gen_exit_memcg(struct mem_cgroup *memcg)
 		if (!mm_state)
 			continue;
 
-		for (i = 0; i < NR_BLOOM_FILTERS; i++) {
-			bitmap_free(mm_state->filters[i]);
-			mm_state->filters[i] = NULL;
+		for (level = 0; level < BLOOM_FILTER_LEVELS; level++) {
+			for (i = 0; i < NR_BLOOM_FILTERS; i++) {
+				bitmap_free(mm_state->filters[level][i]);
+				mm_state->filters[level][i] = NULL;
+			}
 		}
 	}
 }
Signature · 1 line
2.52.0
author reviewer
#3 lore raw

PATCH v5 3/4 mm/mglru: skip cold PUD subtrees during aging

In reply to Baoquan He

From: Baoquan He <[email protected]>

The aging walks into every PUD and runs the PMD-level Bloom filter on each PMD. Add a coarser PUD-level filter one level up:

- walk_pmd_range() reports whether the subtree held anything young for this lruvec. That is the same condition which already carries a PMD over into the next generation: a young PMD entry, a huge page owned by this lruvec, or PTEs with enough young ones to pass suitable_to_scan().
- walk_pud_range() records that in the PUD filter, and
- on subsequent generations, skips the whole PUD subtree when the filter says it had nothing young last generation.

The filter key is (mm, PUD-aligned address): a stable identity for the range that does not depend on the lifetime of a page table page, and that the rmap path can compute without walking page tables.

The filter flips each generation and force_scan bypasses it, so a missed re-mark costs at most one generation. To keep hot regions marked, the rmap feedback path reports the covering PUD as well: lru_gen_look_around() for PTE-mapped folios, folio_referenced_one() for PMD-mapped ones. Without the second one a PUD that holds only transparent huge pages stays skipped after one cold generation.

---
 include/linux/mmzone.h |  6 +++
 mm/rmap.c              |  5 ++-
 mm/vmscan.c            | 99 ++++++++++++++++++++++++++++++++++++------
 3 files changed, 96 insertions(+), 14 deletions(-)
include/linux/mmzone.h +6 -0
index f057c98d1f97..07e6c887e21a 100644
@@ -677,6 +677,7 @@ struct lru_gen_memcg {
 void lru_gen_init_pgdat(struct pglist_data *pgdat);
 void lru_gen_init_lruvec(struct lruvec *lruvec);
 bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw, unsigned int nr);
+void lru_gen_mark_pud(struct mm_struct *mm, struct folio *folio, unsigned long addr);
 
 void lru_gen_init_memcg(struct mem_cgroup *memcg);
 void lru_gen_exit_memcg(struct mem_cgroup *memcg);
@@ -702,6 +703,11 @@ static inline bool lru_gen_look_around(struct page_vma_mapped_walk *pvmw,
 	return false;
 }
 
+static inline void lru_gen_mark_pud(struct mm_struct *mm, struct folio *folio,
+				    unsigned long addr)
+{
+}
+
 static inline void lru_gen_init_memcg(struct mem_cgroup *memcg)
 {
 }
mm/rmap.c +4 -1
index 65646ed6fc45..ea5ada45c09f 100644
@@ -1004,8 +1004,11 @@ static bool folio_referenced_one(struct folio *folio,
 				referenced++;
 		} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
 			if (pmdp_clear_flush_young_notify(vma, address,
-						pvmw.pmd))
+						pvmw.pmd)) {
 				referenced++;
+				if (lru_gen_enabled() && !lru_gen_switching())
+					lru_gen_mark_pud(vma->vm_mm, folio, address);
+			}
 		} else {
 			/* unexpected pmd-mapped folio? */
 			WARN_ON_ONCE(1);
mm/vmscan.c +86 -13

201 lines not on this page · raw message

Signature · 1 line
2.52.0
#4 lore raw

Re: [PATCH v5 3/4] mm/mglru: skip cold PUD subtrees during aging

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 1009

Quoted 12 lines · Baoquan He
mm/rmap.c +2 -1
index 65646ed6fc45..ea5ada45c09f 100644
@@ -1004,8 +1004,11 @@ static bool folio_referenced_one(struct folio *folio,
 				referenced++;
 		} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
 			if (pmdp_clear_flush_young_notify(vma, address,
-						pvmw.pmd))
+						pvmw.pmd)) {
 				referenced++;
+				if (lru_gen_enabled() && !lru_gen_switching())

Could this be abstracted a little bette? This looks to be embedding an expression specific to lrugen in rmap?

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 1010

Quoted 1 line
mm/rmap.c +1 -0
+					lru_gen_mark_pud(vma->vm_mm, folio, address);

--
Cheers, Lorenzo

author reviewer
#5 lore raw

Re: [PATCH v5 3/4] mm/mglru: skip cold PUD subtrees during aging

Quoted 16 lines · Lorenzo Stoakes (ARM)

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 1009

Quoted 12 lines · Baoquan He
mm/rmap.c +2 -1
index 65646ed6fc45..ea5ada45c09f 100644
@@ -1004,8 +1004,11 @@ static bool folio_referenced_one(struct folio *folio,
 				referenced++;
 		} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
 			if (pmdp_clear_flush_young_notify(vma, address,
-						pvmw.pmd))
+						pvmw.pmd)) {
 				referenced++;
+				if (lru_gen_enabled() && !lru_gen_switching())

Could this be abstracted a little bette? This looks to be embedding an expression specific to lrugen in rmap?

You are right, the "is MGLRU in use" test should be kept inside lru_gen_mark_pud(), so the rmap side is one plain call and rmap does not have to carry an MGLRU expression. Will update this in v6. Thanks for careful reviewing and great suggestion.

@@ -1004,8 +1004,10 @@ static bool folio_referenced_one(struct folio *folio,
referenced++;
} else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
if (pmdp_clear_flush_young_notify(vma, address,
- pvmw.pmd))
+ pvmw.pmd)) {
referenced++;
+ lru_gen_mark_pud(vma->vm_mm, folio, address);
+ }
} else {
/* unexpected pmd-mapped folio? */ WARN_ON_ONCE(1);
......
+void lru_gen_mark_pud(struct mm_struct *mm, struct folio *folio, unsigned long addr) +{
+ struct mem_cgroup *memcg;
+
+ if (!lru_gen_enabled() || lru_gen_switching())
+ return;
+
+ memcg = get_mem_cgroup_from_folio(folio);
+ lru_gen_update_pud_filter(mem_cgroup_lruvec(memcg, folio_pgdat(folio)),
+ mm, addr);
+ mem_cgroup_put(memcg);
+}
+

Quoted 5 lines

mm/rmap.c static bool folio_referenced_one(struct folio *folio, · line 1010

Quoted 2 lines
mm/rmap.c +1 -0
+					lru_gen_mark_pud(vma->vm_mm, folio, address);

--
Cheers, Lorenzo

#6 lore raw

Re: [PATCH v5 3/4] mm/mglru: skip cold PUD subtrees during aging

In reply to Baoquan He
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/mglru: lost young status in walk_pmd_range() when flushing THP batch --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/mglru: lost young status in walk_pmd_range() when flushing THP batch
--

commit a67d5ce39733f49dd06a525e19b6621ddfe22558
Author: Baoquan He <[email protected]>

mm/mglru: skip cold PUD subtrees during aging

This commit introduces a PUD-level Bloom filter keyed by (mm, PUD-aligned address) to skip PUD subtrees that held no young entries during the previous MGLRU aging generation, and updates it from both the page table aging walk and the rmap feedback path.

Quoted 4 lines
mm/vmscan.c +0 -0
index 7d20ee1b6c10a..517f5fbecdbf7 100644

[ ... ]

mm/vmscan.c static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end, · line 3772

Quoted 9 lines
mm/vmscan.c +2 -1
@@ -3750,7 +3766,8 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
 			walk->mm_stats[MM_LEAF_TOTAL]++;
 
 			if (pfn != -1)
-				walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);
+				young |= walk_pmd_range_locked(pud, addr, vma, args,
+							       bitmap, &first);
 			continue;
 		}

[Severity: Medium]
Does walk_pmd_range() lose the young return value when a queued THP batch is flushed by the second walk_pmd_range_locked() call on a non-leaf PMD?

mm/vmscan.c:walk_pmd_range() {
    ...
		if (!walk->force_scan && should_clear_pmd_young() &&
		    !mm_has_notifiers(args->mm)) {
			if (!pmd_young(val))
				continue;

			walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);
		}
    ...
}

Both PMD-mapped THPs and young non-leaf PMDs share the same (first, bitmap) batch. walk_pmd_range_locked() queues entries within MIN_LRU_BATCH of *first and returns false, only scanning the queued entries and returning whether any THP was young when a later call exceeds MIN_LRU_BATCH (or passes addr == -1). If a young THP is queued first and a young non-leaf PMD more than MIN_LRU_BATCH entries later flushes that batch, the non-leaf call discards the return value; if no later PMD in the PUD sets young, walk_pmd_range() returns false and walk_pud_range() leaves the PUD out of BLOOM_FILTER_PUD for the next generation.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#7 lore raw

Re: [PATCH v5 3/4] mm/mglru: skip cold PUD subtrees during aging

Quoted 35 lines · On 11/24/26 at 08:59am, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/mglru: lost young status in walk_pmd_range() when flushing THP batch
--

commit a67d5ce39733f49dd06a525e19b6621ddfe22558
Author: Baoquan He <[email protected]>

mm/mglru: skip cold PUD subtrees during aging

This commit introduces a PUD-level Bloom filter keyed by (mm, PUD-aligned address) to skip PUD subtrees that held no young entries during the previous MGLRU aging generation, and updates it from both the page table aging walk and the rmap feedback path.

Quoted 4 lines
mm/vmscan.c +0 -0
index 7d20ee1b6c10a..517f5fbecdbf7 100644

[ ... ]

mm/vmscan.c static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end, · line 3772

Quoted 9 lines
mm/vmscan.c +2 -1
@@ -3750,7 +3766,8 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,
 			walk->mm_stats[MM_LEAF_TOTAL]++;
 
 			if (pfn != -1)
-				walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);
+				young |= walk_pmd_range_locked(pud, addr, vma, args,
+							       bitmap, &first);
 			continue;
 		}

[Severity: Medium]
Does walk_pmd_range() lose the young return value when a queued THP batch is flushed by the second walk_pmd_range_locked() call on a non-leaf PMD?

Quoted 8 lines
mm/vmscan.c:walk_pmd_range() {
    ...
		if (!walk->force_scan && should_clear_pmd_young() &&
		    !mm_has_notifiers(args->mm)) {
			if (!pmd_young(val))
				continue;

			walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);

Yes, this is a corner case and could happen to lose the PMD-mapped young leaf count. The detailed analysis has been replied to v4 patch 3:

https://lore.kernel.org/all/awVGycKHzXzW5OF1@bogon/T/#u

This will be fixed in v6.

Quoted 16 lines
		}
    ...
}

Both PMD-mapped THPs and young non-leaf PMDs share the same (first, bitmap) batch. walk_pmd_range_locked() queues entries within MIN_LRU_BATCH of *first and returns false, only scanning the queued entries and returning whether any THP was young when a later call exceeds MIN_LRU_BATCH (or passes addr == -1). If a young THP is queued first and a young non-leaf PMD more than MIN_LRU_BATCH entries later flushes that batch, the non-leaf call discards the return value; if no later PMD in the PUD sets young, walk_pmd_range() returns false and walk_pud_range() leaves the PUD out of BLOOM_FILTER_PUD for the next generation.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author reviewer
#8 lore raw

PATCH v5 4/4 mm/mglru: count PUD subtrees skipped by the PUD-level filter

In reply to Baoquan He

From: Baoquan He <[email protected]>

Add MM_PUD_EMPTY_SKIPPED, incremented in walk_pud_range() whenever a PUD subtree is skipped by the filter. Like the other per-walk counters it is shown in the debugfs lru_gen output (the "S" column of TYFAWEES) and in the mm_vmscan_lru_gen_walk tracepoint, so the avoided cost can be measured directly.

---
 include/linux/mmzone.h |  1 +
 mm/trace_vmscan.h      | 10 ++++++----
 mm/vmscan.c            | 13 ++++++++-----
 3 files changed, 15 insertions(+), 9 deletions(-)
include/linux/mmzone.h +1 -0
index 07e6c887e21a..a1437728180d 100644
@@ -582,6 +582,7 @@ enum {
 	MM_WALK_TOTAL,		/* page-table walks completed */
 	MM_WALK_EMPTY,		/* walks that found no young leaf for this lruvec */
 	MM_LEAF_EMPTY_WALKS,	/* leaf entries scanned during empty walks */
+	MM_PUD_EMPTY_SKIPPED,	/* PUD subtrees skipped by the PUD-level filter */
 	NR_MM_STATS
 };
 
mm/trace_vmscan.h +6 -4
index b0c2af65c1ea..5a78b8ca3dac 100644
@@ -511,9 +511,9 @@ TRACE_EVENT(mm_mglru_aging,
 TRACE_EVENT(mm_vmscan_lru_gen_walk,
 
 	TP_PROTO(int nid, unsigned long seq, int leaf_total,
-		 int leaf_young, bool empty),
+		 int leaf_young, bool empty, int pud_skipped),
 
-	TP_ARGS(nid, seq, leaf_total, leaf_young, empty),
+	TP_ARGS(nid, seq, leaf_total, leaf_young, empty, pud_skipped),
 
 	TP_STRUCT__entry(
 		__field(int, nid)
@@ -521,6 +521,7 @@ TRACE_EVENT(mm_vmscan_lru_gen_walk,
 		__field(int, leaf_total)
 		__field(int, leaf_young)
 		__field(bool, empty)
+		__field(int, pud_skipped)
 	),
 
 	TP_fast_assign(
@@ -529,11 +530,12 @@ TRACE_EVENT(mm_vmscan_lru_gen_walk,
 		__entry->leaf_total = leaf_total;
 		__entry->leaf_young = leaf_young;
 		__entry->empty = empty;
+		__entry->pud_skipped = pud_skipped;
 	),
 
-	TP_printk("nid=%d seq=%lu leaf_total=%d leaf_young=%d empty=%d",
+	TP_printk("nid=%d seq=%lu leaf_total=%d leaf_young=%d empty=%d pud_skipped=%d",
 		__entry->nid, __entry->seq, __entry->leaf_total,
-		__entry->leaf_young, __entry->empty)
+		__entry->leaf_young, __entry->empty, __entry->pud_skipped)
 );
 
 #endif /* CONFIG_LRU_GEN */
mm/vmscan.c +8 -5
index 517f5fbecdbf..56e18ffd3383 100644
@@ -3836,8 +3836,10 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
 
 		/* Skip a subtree that had nothing young for this lruvec last gen */
 		if (!walk->force_scan &&
-		    !test_bloom_filter(mm_state, BLOOM_FILTER_PUD, walk->seq, pud_key))
+		    !test_bloom_filter(mm_state, BLOOM_FILTER_PUD, walk->seq, pud_key)) {
+			walk->mm_stats[MM_PUD_EMPTY_SKIPPED]++;
 			continue;
+		}
 
 		if (walk_pmd_range(pud + i, addr, next, args))
 			update_bloom_filter(mm_state, BLOOM_FILTER_PUD, walk->seq + 1,
@@ -4221,7 +4223,8 @@ static bool try_to_inc_max_seq(struct lruvec *lruvec, unsigned long seq,
 			trace_mm_vmscan_lru_gen_walk(
 					lruvec_pgdat(lruvec)->node_id, walk->seq,
 					walk->mm_stats[MM_LEAF_TOTAL],
-					walk->mm_stats[MM_LEAF_YOUNG], empty);
+					walk->mm_stats[MM_LEAF_YOUNG], empty,
+					walk->mm_stats[MM_PUD_EMPTY_SKIPPED]);
 		}
 	} while (mm);
 done:
@@ -5903,14 +5906,14 @@ static void lru_gen_seq_show_full(struct seq_file *m, struct lruvec *lruvec,
 
 	seq_puts(m, "                      ");
 	for (i = 0; i < NR_MM_STATS; i++) {
-		const char *s = "xxxxxxxx";
+		const char *s = "xxxxxxxxx";
 		unsigned long n = 0;
 
 		if (seq == max_seq && NR_HIST_GENS == 1) {
-			s = "TYFAWEE";
+			s = "TYFAWEES";
 			n = READ_ONCE(mm_state->stats[hist][i]);
 		} else if (seq != max_seq && NR_HIST_GENS > 1) {
-			s = "tyfawee";
+			s = "tyfawees";
 			n = READ_ONCE(mm_state->stats[hist][i]);
 		}
 
Signature · 1 line
2.52.0
#9 lore raw

Re: [PATCH v5 4/4] mm/mglru: count PUD subtrees skipped by the PUD-level filter

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/mglru: lost young return from walk_pmd_range_locked() in walk_pmd_range() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/mglru: lost young return from walk_pmd_range_locked() in walk_pmd_range()
--

commit 3dc8080e8c434c4239bc40252f11504dbbdb29f3
Author: Baoquan He <[email protected]>

mm/mglru: count PUD subtrees skipped by the PUD-level filter

This commit adds MM_PUD_EMPTY_SKIPPED to count PUD subtrees skipped by the PUD-level Bloom filter and exposes it in debugfs and the mm_vmscan_lru_gen_walk tracepoint.

Quoted 6 lines
mm/vmscan.c +0 -0
index 517f5fbecdbf7..56e18ffd3383e 100644
@@ -3836,8 +3836,10 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
 

mm/vmscan.c static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end, · line 3845

Quoted 10 lines
mm/vmscan.c +3 -1
 		/* Skip a subtree that had nothing young for this lruvec last gen */
 		if (!walk->force_scan &&
-		    !test_bloom_filter(mm_state, BLOOM_FILTER_PUD, walk->seq, pud_key))
+		    !test_bloom_filter(mm_state, BLOOM_FILTER_PUD, walk->seq, pud_key)) {
+			walk->mm_stats[MM_PUD_EMPTY_SKIPPED]++;
 			continue;
+		}
 
 		if (walk_pmd_range(pud + i, addr, next, args))
 			update_bloom_filter(mm_state, BLOOM_FILTER_PUD, walk->seq + 1,

[Severity: Medium]
Can walk_pmd_range() drop the true return value from a flushed THP batch and leave BLOOM_FILTER_PUD unset for a PUD containing young THP entries?

When walk_pmd_range() encounters a PMD-mapped THP, walk_pmd_range_locked() batches it into first/bitmap and initially returns false. If a young non-leaf PMD appears more than MIN_LRU_BATCH entries past first, the second walk_pmd_range_locked() call flushes that queued batch and discards its return value:

mm/vmscan.c:walk_pmd_range() {
	...
		if (!walk->force_scan && should_clear_pmd_young() &&
		    !mm_has_notifiers(args->mm)) {
			if (!pmd_young(val))
				continue;

			walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);
		}
	...
}

Because that flush resets first to -1, the final walk_pmd_range_locked() call at the end of walk_pmd_range() returns false. If no subsequent PMD in the PUD sets young, walk_pmd_range() returns false to walk_pud_range(), which skips update_bloom_filter() for BLOOM_FILTER_PUD and causes the next generation's walk to skip the PUD subtree and increment MM_PUD_EMPTY_SKIPPED even though it contained young THP entries. OR-ing that return value into young avoids losing the result of the flushed batch.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author reviewer
#10 lore raw

Re: [PATCH v5 4/4] mm/mglru: count PUD subtrees skipped by the PUD-level filter

Quoted 42 lines · On 11/24/26 at 08:59am, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/mglru: lost young return from walk_pmd_range_locked() in walk_pmd_range()
--

commit 3dc8080e8c434c4239bc40252f11504dbbdb29f3
Author: Baoquan He <[email protected]>

mm/mglru: count PUD subtrees skipped by the PUD-level filter

This commit adds MM_PUD_EMPTY_SKIPPED to count PUD subtrees skipped by the PUD-level Bloom filter and exposes it in debugfs and the mm_vmscan_lru_gen_walk tracepoint.

Quoted 6 lines
mm/vmscan.c +0 -0
index 517f5fbecdbf7..56e18ffd3383e 100644
@@ -3836,8 +3836,10 @@ static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end,
 

mm/vmscan.c static int walk_pud_range(p4d_t *p4d, unsigned long start, unsigned long end, · line 3845

Quoted 10 lines
mm/vmscan.c +3 -1
 		/* Skip a subtree that had nothing young for this lruvec last gen */
 		if (!walk->force_scan &&
-		    !test_bloom_filter(mm_state, BLOOM_FILTER_PUD, walk->seq, pud_key))
+		    !test_bloom_filter(mm_state, BLOOM_FILTER_PUD, walk->seq, pud_key)) {
+			walk->mm_stats[MM_PUD_EMPTY_SKIPPED]++;
 			continue;
+		}
 
 		if (walk_pmd_range(pud + i, addr, next, args))
 			update_bloom_filter(mm_state, BLOOM_FILTER_PUD, walk->seq + 1,

[Severity: Medium]
Can walk_pmd_range() drop the true return value from a flushed THP batch and leave BLOOM_FILTER_PUD unset for a PUD containing young THP entries?

When walk_pmd_range() encounters a PMD-mapped THP, walk_pmd_range_locked() batches it into first/bitmap and initially returns false. If a young non-leaf PMD appears more than MIN_LRU_BATCH entries past first, the second walk_pmd_range_locked() call flushes that queued batch and discards its return value:

Quoted 8 lines
mm/vmscan.c:walk_pmd_range() {
	...
		if (!walk->force_scan && should_clear_pmd_young() &&
		    !mm_has_notifiers(args->mm)) {
			if (!pmd_young(val))
				continue;

			walk_pmd_range_locked(pud, addr, vma, args, bitmap, &first);

This has been reported at patch 3 sashiko report, a repeatedly reported issue. Will fix in v6 soon. Thanks.

Quoted 14 lines
		}
	...
}

Because that flush resets first to -1, the final walk_pmd_range_locked() call at the end of walk_pmd_range() returns false. If no subsequent PMD in the PUD sets young, walk_pmd_range() returns false to walk_pud_range(), which skips update_bloom_filter() for BLOOM_FILTER_PUD and causes the next generation's walk to skip the PUD subtree and increment MM_PUD_EMPTY_SKIPPED even though it contained young THP entries. OR-ing that return value into young avoids losing the result of the flushed batch.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author reviewer
#12 lore raw
Quoted 10 lines · Lorenzo Stoakes (ARM)

(Got a script set up to detect things not cc'd now)

Quoted 1 line · Baoquan He
 mm/rmap.c              |   5 +-

MEMORY MANAGEMENT - RMAP (REVERSE MAPPING)
...
M: Lorenzo Stoakes <[email protected]>

Please cc- me on rmap stuff, thanks ;)

Sure, will do next time. I should have used scripts/get_maintainer.pl to collect mail address, but not copying from the old thread. Thanks for reminding.

#13 lore raw
Quoted 10 lines · Baoquan He
Quoted 9 lines · Lorenzo Stoakes (ARM)

(Got a script set up to detect things not cc'd now)

Quoted 1 line · Baoquan He
 mm/rmap.c              |   5 +-

MEMORY MANAGEMENT - RMAP (REVERSE MAPPING)
...
M: Lorenzo Stoakes <[email protected]>

Quoted 6 lines
Quoted 1 line

Please cc- me on rmap stuff, thanks ;)

Sure, will do next time. I should have used scripts/get_maintainer.pl to collect mail address, but not copying from the old thread. Thanks for reminding.

Thanks, email-for-development continues to be... fun :) though I do very much recommend b4 (i.e. b4 prep, b4 send), I resisted it for far too long, but can't live without it now, it just makes everything super easy.

Like:

$ b4 prep -n name-of-series
$ ... do work ...
$ b4 prep --edit-cover # Write cover letter (b4 adds a dummy commit in tree for it)
$ b4 prep --check # do checkpatch etc.
$ b4 prep --auto-to-cc # automatically figures out who to send it to
$ b4 send # and off it goes

--
Cheers, Lorenzo

author reviewer
#14 lore raw
Quoted 22 lines · Lorenzo Stoakes (ARM)
Quoted 10 lines · Baoquan He
Quoted 9 lines · Lorenzo Stoakes (ARM)

(Got a script set up to detect things not cc'd now)

Quoted 1 line · Baoquan He
 mm/rmap.c              |   5 +-

MEMORY MANAGEMENT - RMAP (REVERSE MAPPING)
...
M: Lorenzo Stoakes <[email protected]>

Quoted 6 lines
Quoted 1 line

Please cc- me on rmap stuff, thanks ;)

Sure, will do next time. I should have used scripts/get_maintainer.pl to collect mail address, but not copying from the old thread. Thanks for reminding.

Thanks, email-for-development continues to be... fun :) though I do very much recommend b4 (i.e. b4 prep, b4 send), I resisted it for far too long, but can't live without it now, it just makes everything super easy.

Quoted 8 lines

Like:

$ b4 prep -n name-of-series
$ ... do work ...
$ b4 prep --edit-cover # Write cover letter (b4 adds a dummy commit in tree for it)
$ b4 prep --check # do checkpatch etc.
$ b4 prep --auto-to-cc # automatically figures out who to send it to
$ b4 send # and off it goes

Ah, it looks super cool. I only use b4 am to download patchset sometime, can't wait to use it. Thanks a lot for the sharing.