skip empty PUD subtrees during aging with a PUD-level Bloom filter
Subsystems (5)
Baoquan He
reviewercover 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.
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, medians of 3 runs.
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, per pass, both nodes aged:
base filter change
node 1 leaf entries scanned 1,214 100 -92%
walks with leaves 17 1 -94%
PUDs skipped 0 52
node 0 leaf entries scanned 12,559 12,288 -2.2%
leaf entries assoc. 9,164 9,175 +0.1%
worker RSS delta 0% 0%
forced pass, node 1 leaf/walk
before the filtered passes 1,363 1,362
after them 1,363 1,369
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. Node 0 keeps its young leaves (+0.1%), so the coarser filter does not starve the node that owns them.
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) 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 1132, leaf_associated 513
This is what v4 adds. In v3 the range stayed skipped, because folio_referenced_one() takes the pmdp_clear_flush_young_notify() branch for huge pages and never reaches lru_gen_look_around().
4) Reclaim pressure. Four 512 MiB workers in a memory.max=2G/swap.max=1G memcg, i.e. exactly at the limit, so reclaim is always active; each touches one 256 MiB half and flips it every second, so a half is cold for a few aging generations and then hot again. Aging runs on both nodes every 0.5 s, for 600 s. The base ran twice, the filter three times:
oom_kill 0 vs 0
hot half resident, min 262,051 vs 262,120 (of 262,144 pages)
pgsteal_anon 2.12M vs 2.24M
pswpout 2.12M vs 2.24M
workingset_refault_anon 2.12M vs 2.24M
pgscan_anon 14.2M vs 17.6M
node 1 walks with leaves 4,532 vs 4 -99.9%
No OOM and no residency loss. Evictions, swap-out and refaults overlap between the two kernels (base 2.12-2.61M, filter 2.03-2.26M), so this is not a measurable difference. pgscan_anon is 15% higher; note that it counts folios examined by the LRU scanner on both nodes, not page-table walks, so it is not a measure of the filter's own work. PUD subtrees skipped on node 0 are 0.16-0.20% of its associated leaves.
5) Low memory. Aging cost on a single node, CPU time per pass taken from the utime+stime of the task that writes to debugfs, 200 passes:
shape base filter
2 procs x 32 GiB VA, 0.2% touched 3,707 us 3,191 us -14%
20 procs x 3 GiB VA, 2% touched 11,008 us 10,573 us
20 procs x 64 MiB, fully touched 10,962 us 10,783 usThe leaves scanned, the leaves associated and the number of walks with leaves are the same in all three rows. If a mm has only a few 1 GiB subtrees the filter has nothing to save and it spends nothing. On the wide shape it saves about 14%, i.e. what 512 PMD-level Bloom tests cost per skipped subtree.
Change log
==========v3->v4:
- Mark a PUD young only when a huge page is really young for this lruvec. walk_pmd_range_locked() returns that now, and the accessed bit of a non-leaf PMD no longer marks anything. (Sashiko)
- Mark the PUD from folio_referenced_one() too, so a PUD that holds only PMD-mapped huge pages can be re-marked. (Sashiko)
- Statistics: count the batched entries of a large folio in MM_LEAF_ASSOCIATED, move the tracepoint inside #ifdef CONFIG_LRU_GEN, and word the comment so MM_LEAF_ASSOCIATED and MM_WALK_EMPTY clearly mean "young". (Sashiko)
- Bloom filter helpers: fold the __ functions into test/update/reset_bloom_filter(), and describe both levels in the comment, including why they keep the same m and k and that two levels of double buffering take 16 KiB per lruvec. (Kairui)
v2 -> v3:
- Use (mm, PUD-aligned address) as the PUD filter key instead of the PUD page-table pointer. This removes the pud_offset(p4d_offset(pgd_offset())) page-table walk that the v2 lru_gen_look_around() hunk had to do to obtain the key with withc Barry is very worried about adding cost to a hot paths. This is suggested by Kairui.
- Store both levels in one array, filters[BLOOM_FILTER_LEVELS] [NR_BLOOM_FILTERS], instead of separate pmd_filters/pud_filters (Kairui's idea).
- Count skipped subtrees (MM_PUD_EMPTY_SKIPPED, debugfs column "S" of TYFALWEES, and a tracepoint field) so the avoided cost is measurable.
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 | 21 ++++- mm/rmap.c | 5 +- mm/trace_vmscan.h | 30 ++++++++ mm/vmscan.c | 171 +++++++++++++++++++++++++++++++++-------- 4 files changed, 192 insertions(+), 35 deletions(-)
base-commit: 9f004372ebeb18fdeb699f9880a0e472df90135b
Signature · 1 line
2.52.0
Baoquan He
reviewerPATCH 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 folio 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_LEAF_ASSOCIATED - leaf entries whose folio is in this lruvec MM_WALK_TOTAL - page-table walks completed MM_WALK_EMPTY - walks that found no folio in this lruvec MM_LEAF_EMPTY_WALKS - leaf entries scanned during empty walks
A new tracepoint, mm_vmscan_lru_gen_walk(), fires after each walk, and the debugfs lru_gen output ("TYFALWEE") exposes the new counters.
--- include/linux/mmzone.h | 4 ++++ mm/trace_vmscan.h | 28 ++++++++++++++++++++++++++++ mm/vmscan.c | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 62 insertions(+), 4 deletions(-)
include/linux/mmzone.h +4 -0
@@ -579,6 +579,10 @@ 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_LEAF_ASSOCIATED ,/* young leaf entries whose folio is in this lruvec */ +MM_WALK_TOTAL ,/* page-table walks completed */ +MM_WALK_EMPTY ,/* walks that found nothing young for this lruvec */ +MM_LEAF_EMPTY_WALKS ,/* leaf entries scanned during empty walks */ NR_MM_STATS };
mm/trace_vmscan.h +28 -0
@@ -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_associated, bool empty), + +TP_ARGS (nid, seq, leaf_total, leaf_associated, empty), + +TP_STRUCT__entry ( + __field(int, nid) + __field(unsigned long, seq) + __field(int, leaf_total) + __field(int, leaf_associated) +__field (bool, empty) + ), + +TP_fast_assign ( + __entry->nid = nid; + __entry->seq = seq; + __entry->leaf_total = leaf_total; + __entry->leaf_associated = leaf_associated; + __entry->empty = empty; + ), + +TP_printk ("nid=%d seq=%lu leaf_total=%d leaf_associated=%d empty=%d" , + __entry->nid, __entry->seq, __entry->leaf_total, + __entry->leaf_associated, __entry->empty) +); +#endif /* CONFIG_LRU_GEN */ TRACE_EVENT(mm_vmscan_write_folio,
mm/vmscan.c +30 -4
@@ -3584,6 +3584,8 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,if (!folio)continue ; + walk->mm_stats[MM_LEAF_ASSOCIATED ]++; +if (folio_test_large (folio)) {const unsigned int max_nr = (end - addr) >>PAGE_SHIFT ;
@@ -3591,6 +3593,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, max_nr, FPB_MERGE_YOUNG_DIRTY); total += nr -1 ; walk->mm_stats[MM_LEAF_TOTAL ] += nr -1 ; + walk->mm_stats[MM_LEAF_ASSOCIATED ] += nr -1 ; }if (!test_and_clear_young_ptes_notify (args->vma, addr, cur_pte, nr))
@@ -3682,6 +3685,8 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_areaif (!folio) goto next; + walk->mm_stats[MM_LEAF_ASSOCIATED ]++; +if (!pmdp_test_and_clear_young_notify (vma, addr, pmd + i)) goto next;
@@ -4158,8 +4163,29 @@ 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 nothing +* young for this lruvec (node+memcg) is pure waste: +* the PUD-level filter will skip it next generation. +*/ +if (walk->mm_stats[MM_LEAF_TOTAL ]) { + walk->mm_stats[MM_WALK_TOTAL ]++; +if (walk->mm_stats[MM_LEAF_ASSOCIATED ] ==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_ASSOCIATED ], empty); + } }while (mm); done:if (success) {
@@ -5804,14 +5830,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 ="TYFALWEE" ; n =READ_ONCE (mm_state->stats[hist][i]); }else if (seq != max_seq &&NR_HIST_GENS >1 ) { - s ="tyfa" ; + s ="tyfalwee" ; n =READ_ONCE (mm_state->stats[hist][i]); }
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH 1/4] mm/mglru: add MM_WALK_EMPTY stats and tracepoint
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/mglru: inconsistent MM_LEAF_ASSOCIATED accounting across mm_has_notifiers() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/mglru: inconsistent MM_LEAF_ASSOCIATED accounting across mm_has_notifiers()
--
commit 4f0dadfe0435dfb7f058c5e9c459aef67be5dff2
Author: Baoquan He <[email protected]>
mm/mglru: add MM_WALK_EMPTY stats and tracepoint
This commit adds MGLRU aging statistics (MM_LEAF_ASSOCIATED, MM_WALK_TOTAL, MM_WALK_EMPTY, and MM_LEAF_EMPTY_WALKS) and the mm_vmscan_lru_gen_walk tracepoint to track page-table walks that find no matching folios in the target lruvec.
Quoted 37 lines
include/linux/mmzone.h +4
-0@@ -579,6 +579,10 @@ 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_LEAF_ASSOCIATED ,/* young leaf entries whose folio is in this lruvec */ +MM_WALK_TOTAL ,/* page-table walks completed */ +MM_WALK_EMPTY ,/* walks that found nothing young for this lruvec */ +MM_LEAF_EMPTY_WALKS ,/* leaf entries scanned during empty walks */ NR_MM_STATS };mm/vmscan.c +3
-0@@ -3584,6 +3584,8 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,if (!folio)continue ; + walk->mm_stats[MM_LEAF_ASSOCIATED ]++; +if (folio_test_large (folio)) {const unsigned int max_nr = (end - addr) >>PAGE_SHIFT ;@@ -3591,6 +3593,7 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, max_nr, FPB_MERGE_YOUNG_DIRTY); total += nr -1 ; walk->mm_stats[MM_LEAF_TOTAL ] += nr -1 ; + walk->mm_stats[MM_LEAF_ASSOCIATED ] += nr -1 ; }if (!test_and_clear_young_ptes_notify (args->vma, addr, cur_pte, nr))
mm/vmscan.c static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area · line 3692
Quoted 8 lines
mm/vmscan.c +2
-0@@ -3682,6 +3685,8 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_areaif (!folio) goto next; + walk->mm_stats[MM_LEAF_ASSOCIATED ]++; +if (!pmdp_test_and_clear_young_notify (vma, addr, pmd + i)) goto next;
[Severity: Medium]
Does MM_LEAF_ASSOCIATED count all leaf entries belonging to the target lruvec (as described in the commit message), or only young leaf entries in the target lruvec (as described in the header comments)?
In walk_pte_range() and walk_pmd_range_locked(), get_pte_pfn() and get_pmd_pfn() filter out non-young entries before get_pfn_folio() is reached whenever !mm_has_notifiers(vma->vm_mm):
mm/vmscan.c:get_pte_pfn() {
...
if (!pte_young(pte) && !mm_has_notifiers(vma->vm_mm))
return -1;
...
}When !mm_has_notifiers(vma->vm_mm), every entry that reaches get_pfn_folio(pfn, memcg, pgdat) is already young, so MM_LEAF_ASSOCIATED is identical to MM_LEAF_YOUNG (which also only counts young entries after get_pfn_folio() verifies the folio belongs to this lruvec), and an mm with old folios in the lruvec is still counted as MM_WALK_EMPTY.
Conversely, when mm_has_notifiers(vma->vm_mm) is true (such as KVM guests), get_pte_pfn() and get_pmd_pfn() let non-young entries through so secondary MMU notifiers can be checked by test_and_clear_young_ptes_notify() and pmdp_test_and_clear_young_notify(). Because MM_LEAF_ASSOCIATED is incremented before those notify calls, it counts both old and young entries when mm_has_notifiers(vma->vm_mm) is true, giving MM_LEAF_ASSOCIATED and MM_WALK_EMPTY different meanings depending on whether the mm has MMU notifiers registered.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Baoquan He
reviewerRe: [PATCH 1/4] mm/mglru: add MM_WALK_EMPTY stats and tracepoint
Quoted 6 lines · On 11/21/26 at 08:59am, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/mglru: inconsistentMM_LEAF_ASSOCIATEDaccounting acrossmm_has_notifiers()
--
...snip...
Quoted 4 lines
Quoted 4 lines
mm/vmscan.c +0
-0@@ -3682,6 +3685,8 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_areaif (!folio) goto next;
Quoted 10 lines
mm/vmscan.c static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_area · line 3692
Quoted 5 lines
mm/vmscan.c +2
-0+ walk->mm_stats[MM_LEAF_ASSOCIATED ]++; +if (!pmdp_test_and_clear_young_notify (vma, addr, pmd + i)) goto next;[Severity: Medium]
DoesMM_LEAF_ASSOCIATEDcount all leaf entries belonging to the target lruvec (as described in the commit message), or only young leaf entries in the target lruvec (as described in the header comments)?
Agreed, and the counter is not worth keeping. MM_LEAF_YOUNG already counts the leaves we care about: it is incremented after get_pfn_folio() confirms the folio belongs to this lruvec. So v5 drops MM_LEAF_ASSOCIATED.
Quoted 29 lines
In
walk_pte_range()andwalk_pmd_range_locked(),get_pte_pfn()andget_pmd_pfn()filter out non-young entries beforeget_pfn_folio()is reached whenever !mm_has_notifiers(vma->vm_mm):mm/vmscan.c:get_pte_pfn() { ... if (!pte_young(pte) && !mm_has_notifiers(vma->vm_mm)) return -1; ... }When !mm_has_notifiers(vma->vm_mm), every entry that reaches
get_pfn_folio(pfn, memcg, pgdat)is already young, soMM_LEAF_ASSOCIATEDis identical toMM_LEAF_YOUNG(which also only counts young entries afterget_pfn_folio()verifies the folio belongs to this lruvec), and an mm with old folios in the lruvec is still counted asMM_WALK_EMPTY.Conversely, when
mm_has_notifiers(vma->vm_mm)is true (such as KVM guests),get_pte_pfn()andget_pmd_pfn()let non-young entries through so secondary MMU notifiers can be checked bytest_and_clear_young_ptes_notify()andpmdp_test_and_clear_young_notify(). BecauseMM_LEAF_ASSOCIATEDis incremented before those notify calls, it counts both old and young entries whenmm_has_notifiers(vma->vm_mm)is true, givingMM_LEAF_ASSOCIATEDandMM_WALK_EMPTYdifferent meanings depending on whether the mm has MMU notifiers registered.Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Baoquan He
reviewerPATCH 2/4 mm/mglru: add PUD-level Bloom filter state and generic helpers
In reply to Baoquan HeFrom: 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 operate at PUD granularity and generalize the suppression 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 most visible case: a foreign mm's PUDs have no young entries for this lruvec, so the entire PMD iteration is pure waste.
test_bloom_filter(), update_bloom_filter() and reset_bloom_filter() take the level now and use mm_state->filters[level][gen] directly.
No behavior change yet. No PUD-level bitmap is allocated in this patch; the next patch fills and uses them for the first time.
--- include/linux/mmzone.h | 10 ++++++++-- mm/vmscan.c | 39 ++++++++++++++++++++++----------------- 2 files changed, 30 insertions(+), 19 deletions(-)
include/linux/mmzone.h +8 -2
@@ -589,6 +589,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;
@@ -596,8 +602,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
@@ -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 sreturn 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 longset_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 **itespin_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);
@@ -3766,7 +3767,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 ]++;
@@ -3777,7 +3779,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);
@@ -4436,8 +4438,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);
@@ -6153,6 +6155,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);
@@ -6170,9 +6173,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
Baoquan He
reviewerPATCH 3/4 mm/mglru: skip cold PUD subtrees during aging
In reply to Baoquan HeFrom: 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.
Use (mm, PUD-aligned address) as the filter key rather than the PUD page table pointer. It is a stable identity for the range being walked, is available without walking the page tables in lru_gen_look_around(), and does not depend on the lifetime of a page table page.
The PUD level keeps the same m and k as the PMD level. It holds far fewer keys, but a false positive there costs a walk over 512 PMDs, so the same bitmap size is not an over-allocation.
The double-buffered filter flips with each new iteration, so newly hot or migrated-in pages are re-checked promptly rather than suppressed indefinitely. force_scan walks bypass the PUD test, so manual aging and newly added mms always rescan and re-populate the filter.
To keep hot regions marked, also report the covering PUD from the rmap feedback path: lru_gen_look_around() for PTE-mapped folios, and folio_referenced_one() for PMD-mapped ones, which never reaches lru_gen_look_around(). Without the second path 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
@@ -678,6 +678,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);
@@ -703,6 +704,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
@@ -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
[email protected]
AI reviewRe: [PATCH 3/4] mm/mglru: skip cold PUD subtrees during aging
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/mglru: walk_pmd_range() drops young status of THPs flushed at non-leaf PMD --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/mglru: walk_pmd_range() drops young status of THPs flushed at non-leaf PMD
--
commit 807f1bc14298f872f3bf880156eb7f9c34a7f570
Author: Baoquan He <[email protected]>
mm/mglru: skip cold PUD subtrees during aging
This commit introduces a PUD-level Bloom filter to MGLRU page table aging to skip 1 GiB PUD subtrees that had no young pages in the previous generation, while updating the filter from the page table walk and rmap feedback paths.
Quoted 4 lines
mm/vmscan.c +0
-0
[ ... ]
Quoted 28 lines
mm/vmscan.c +4
-3@@ -3626,11 +3637,12 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,return suitable_to_scan (total, young); } -static void walk_pmd_range_locked (pud_t *pud,unsigned long addr,struct vm_area_struct *vma, +static bool walk_pmd_range_locked (pud_t *pud,unsigned long addr,struct vm_area_struct *vma,struct mm_walk *args,unsigned long *bitmap,unsigned long *first) {int i;bool dirty; +bool young = false;pmd_t *pmd;spinlock_t *ptl;struct folio *last =NULL ;@@ -3644,13 +3656,13 @@ static void walk_pmd_range_locked(pud_t *pud, unsigned long addr, struct vm_areaif (*first ==-1 ) { *first = addr;bitmap_zero (bitmap,MIN_LRU_BATCH ); -return ; +return false; } i = addr ==-1 ?0 :pmd_index (addr) -pmd_index (*first);if (i && i <=MIN_LRU_BATCH ) {__set_bit (i -1 , bitmap); -return ; +return false; }
[ ... ]
Quoted 12 lines
mm/vmscan.c +2
-1@@ -3755,7 +3771,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 ; }@@ -3777,15 +3794,18 @@ static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end,continue ;
mm/vmscan.c static void walk_pmd_range(pud_t *pud, unsigned long start, unsigned long end, · line 3803
Quoted 10 lines
mm/vmscan.c +2
-1walk->mm_stats[MM_NONLEAF_ADDED ]++; + young = true;/* carry over to the next generation */ update_bloom_filter (mm_state,BLOOM_FILTER_PMD , walk->seq +1 , pmd + i); } -walk_pmd_range_locked (pud,-1 , vma, args, bitmap, &first); + young |=walk_pmd_range_locked (pud,-1 , vma, args, bitmap, &first);
[Severity: Medium]
Can walk_pmd_range() drop the young return value when batched transparent huge pages are flushed at 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);
}
...
}walk_pmd_range_locked() buffers up to 1 + MIN_LRU_BATCH entries in first and bitmap across loop iterations, returning false while buffering. If earlier iterations queued PMD-mapped THPs and this non-leaf PMD call lies more than MIN_LRU_BATCH entries past first, it flushes those THPs and resets first to -1, but its return value is ignored. 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, skipping the 1 GiB subtree on the next aging pass.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Baoquan He
reviewerPATCH 4/4 mm/mglru: count PUD subtrees skipped by the PUD-level filter
In reply to Baoquan HeFrom: Baoquan He <[email protected]>
Add MM_PUD_EMPTY_SKIPPED, incremented in walk_pud_range() whenever a PUD subtree is skipped by the filter. Like other counters accumulated per walk, shown in debugfs lru_gen output (the "S" column of TYFALWEES) and via the mm_vmscan_lru_gen_walk tracepoint, so the avoided cost by the filter 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
@@ -583,6 +583,7 @@ enum {MM_WALK_TOTAL ,/* page-table walks completed */ MM_WALK_EMPTY ,/* walks that found nothing young 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
@@ -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_associated, bool empty), + int leaf_associated, bool empty, int pud_skipped), - TP_ARGS(nid, seq, leaf_total, leaf_associated, empty), + TP_ARGS(nid, seq, leaf_total, leaf_associated, 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_associated) __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_associated = leaf_associated; __entry->empty = empty; + __entry->pud_skipped = pud_skipped; ), -TP_printk ("nid=%d seq=%lu leaf_total=%d leaf_associated=%d empty=%d" , +TP_printk ("nid=%d seq=%lu leaf_total=%d leaf_associated=%d empty=%d pud_skipped=%d" , __entry->nid, __entry->seq, __entry->leaf_total, - __entry->leaf_associated, __entry->empty) + __entry->leaf_associated, __entry->empty, __entry->pud_skipped) );#endif /* CONFIG_LRU_GEN */
mm/vmscan.c +8 -5
@@ -3841,8 +3841,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,
@@ -4223,7 +4225,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_ASSOCIATED ], empty); + walk->mm_stats[MM_LEAF_ASSOCIATED ], empty, + walk->mm_stats[MM_PUD_EMPTY_SKIPPED ]); } }while (mm); done:
@@ -5905,14 +5908,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 ="TYFALWEE" ; + s ="TYFALWEES" ; n =READ_ONCE (mm_state->stats[hist][i]); }else if (seq != max_seq &&NR_HIST_GENS >1 ) { - s ="tyfalwee" ; + s ="tyfalwees" ; n =READ_ONCE (mm_state->stats[hist][i]); }
Signature · 1 line
2.52.0
Baoquan He
reviewerQuoted 10 lines · Baoquan He
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()setsmm->lru_gen.bitmapto -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.
This is v4 series, I forgot adding v4 mark in subject.
...