* [PATCH] mm/mglru: skip redundant bitmap store in lru_gen_use_mm()
@ 2026-11-25 11:25 Ridong Chen
0 siblings, 0 replies; only message in thread
From: Ridong Chen @ 2026-11-25 11:25 UTC (permalink / raw)
To: Andrew Morton, David Hildenbrand, Johannes Weiner
Cc: Peter Zijlstra, Lorenzo Stoakes, Liam R . Howlett,
Vlastimil Babka, Mike Rapoport, Suren Baghdasaryan, Michal Hocko,
Kairui Song, Qi Zheng, Shakeel Butt, Barry Song, Axel Rasmussen,
Yuanchu Xie, Wei Xu, Baoquan He, Baolin Wang, Joanne Koong,
linux-mm, linux-kernel, Ridong Chen
From: Ridong Chen <chenridong@xiaomi.com>
lru_gen_use_mm() is called from context_switch() on every switch to a
user task and unconditionally sets mm->lru_gen.bitmap to -1. The bitmap
is only cleared by the page table walkers in get_next_mm() during aging,
so between aging passes it is already all ones and the store is
redundant.
The store is not free when the mm_struct is shared: every CPU running a
thread of the same process must take the cacheline in exclusive state,
so the line bounces between CPUs on every context switch. On x86,
switch_mm_irqs_off() returns early when switching between threads of
the same mm, which can make this the only shared write on that path.
Read the bitmap first and only write it when it is not already set.
The semantics are unchanged: the plain WRITE_ONCE() had no ordering
guarantees, and a walker clearing a bit concurrently can be missed in
either version.
Tested on an i7-13700HX (8P+8E cores, 24 threads), with turbo disabled
and the performance governor. On each CPU two tasks sched_yield() to
each other, either as threads of one process (shared mm) or as one
process per CPU (private mm, control). Context switches per second per
CPU, median of 5 runs, averaged over 2 boots of each kernel:
CPUs shared mm private mm
base patched base patched
1 1114766 1106870 -0.7% 1104512 1109284 +0.4%
2 1086513 1110204 +2.2% 1110334 1112209 +0.2%
4 1085872 1112258 +2.4% 1113146 1112467 -0.1%
8 1085945 1111371 +2.3% 1111662 1111876 +0.0%
16 660870 666748 +0.9% 665470 667058 +0.2%
24 579382 596472 +2.9% 596177 598500 +0.4%
The 16 CPU case uses both SMT siblings of each P-core, which share the
L1/L2, so less of the traffic crosses cores. With the patch, the shared
mm case is within noise of the private mm case.
will-it-scale context_switch1 and sched_yield do not show a difference
(within 1% for both threads and processes): sched_yield runs one task
per CPU and never switches, and context_switch1 leaves the pipe peer
unpinned, so most switches stay on one CPU and the line does not move.
Signed-off-by: Ridong Chen <chenridong@xiaomi.com>
---
Changes since RFC [1]:
- Split out of the RFC series and sent on its own; it is independent of
the other two patches. Patch 2 of the RFC is in mm-new as part of
MGLRU-FG, patch 3 will be reworked separately.
- Rebased onto mm-new, no code change. The numbers above were measured
on next-20261023; lru_gen_use_mm() and its callers have not changed
since.
The test program is a small C file; I can post it if that helps.
[1] https://lore.kernel.org/20261105132220.2233677-2-ridong.chen@linux.dev
include/linux/mm_types.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
index 4c576e24ce42..7812b36fefff 100644
--- a/include/linux/mm_types.h
+++ b/include/linux/mm_types.h
@@ -1541,7 +1541,8 @@ static inline void lru_gen_use_mm(struct mm_struct *mm)
* used since the last time it cleared the bitmap. So it might be worth
* walking the page tables of this mm_struct to clear the accessed bit.
*/
- WRITE_ONCE(mm->lru_gen.bitmap, -1);
+ if (READ_ONCE(mm->lru_gen.bitmap) != -1UL)
+ WRITE_ONCE(mm->lru_gen.bitmap, -1UL);
}
#else /* !CONFIG_LRU_GEN_WALKS_MMU */
base-commit: a036649714c3446abfdc36d0ae483046432b16c5
--
2.34.1
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-25 11:25 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 11:25 [PATCH] mm/mglru: skip redundant bitmap store in lru_gen_use_mm() Ridong Chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®