Skip to contentSkip to conversation

x86/mm: only do broadcast flush from reclaim if pages were unmapped

PATCH Applied Rik van Riel 2 participants 2 messages Mar 19, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/mm: only do broadcast flush from reclaim if pages were unmapped

Track whether pages were unmapped from any mm (even ones with a currently empty mm_cpumask) by the reclaim code, to figure out whether or not broadcast TLB flush should be done when reclaim finishes.

The reason any mm must be tracked, and not only ones contributing to the tlbbatch cpumask, is that broadcast ASIDs are expected to be kept up to date even on CPUs where the mm is not currently active.

This change allows reclaim to avoid doing TLB flushes when only clean page cache pages and/or slab memory were reclaimed, which is fairly common.

---
This is a simpler alternative to the code that was in my invlpgb series before, and it seems to capture most of the benefit due to how common it is to reclaim only page cache.

 arch/x86/include/asm/tlbbatch.h | 5 +++++
 arch/x86/include/asm/tlbflush.h | 1 +
 arch/x86/mm/tlb.c               | 3 ++-
 3 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/include/asm/tlbbatch.h +5 -0
index 1ad56eb3e8a8..d24f893de4b7 100644
@@ -10,6 +10,11 @@ struct arch_tlbflush_unmap_batch {
 	 * the PFNs being flushed..
 	 */
 	struct cpumask cpumask;
+	/*
+	 * Set if pages were unmapped from any mm, even one that does not
+	 * have active CPUs in its cpumask.
+	 */
+	bool unmapped_pages;
 };
 
 #endif /* _ARCH_X86_TLBBATCH_H */
arch/x86/include/asm/tlbflush.h +1 -0
index 7cad283d502d..a9af8759de34 100644
@@ -353,6 +353,7 @@ static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *b
 {
 	inc_mm_tlb_gen(mm);
 	cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
+	batch->unmapped_pages = true;
 	mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL);
 }
 
arch/x86/mm/tlb.c +2 -1
index 3e2c6e2f5134..e459d97ef397 100644
@@ -1632,8 +1632,9 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 	 * a local TLB flush is needed. Optimize this use-case by calling
 	 * flush_tlb_func_local() directly in this case.
 	 */
-	if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) {
+	if (cpu_feature_enabled(X86_FEATURE_INVLPGB) && batch->unmapped_pages) {
 		invlpgb_flush_all_nonglobals();
+		batch->unmapped_pages = false;
 	} else if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) {
 		flush_tlb_multi(&batch->cpumask, info);
 	} else if (cpumask_test_cpu(cpu, &batch->cpumask)) {
Signature · 1 line
2.48.1
merge bot Applied
#1 lore raw

[tip: x86/core] x86/mm: Only do broadcast flush from reclaim if pages were unmapped

The following commit has been merged into the x86/core branch of tip:

The following commit has been merged into the x86/core branch of tip:

Commit-ID:     0b7eb55cb706e92d6073e4ab63ccd4d219cf2cda
Gitweb:        https://git.kernel.org/tip/0b7eb55cb706e92d6073e4ab63ccd4d219cf2cda
Author:        Rik van Riel <[email protected]>
AuthorDate:    Wed, 19 Mar 2025 13:25:20 -04:00
Committer:     Ingo Molnar <[email protected]>
CommitterDate: Wed, 19 Mar 2025 21:56:42 +01:00

x86/mm: Only do broadcast flush from reclaim if pages were unmapped

Track whether pages were unmapped from any MM (even ones with a currently empty mm_cpumask) by the reclaim code, to figure out whether or not broadcast TLB flush should be done when reclaim finishes.

The reason any MM must be tracked, and not only ones contributing to the tlbbatch cpumask, is that broadcast ASIDs are expected to be kept up to date even on CPUs where the MM is not currently active.

This change allows reclaim to avoid doing TLB flushes when only clean page cache pages and/or slab memory were reclaimed, which is fairly common.

( This is a simpler alternative to the code that was in my INVLPGB series
before, and it seems to capture most of the benefit due to how common it is to reclaim only page cache. )

---
 arch/x86/include/asm/tlbbatch.h | 5 +++++
 arch/x86/include/asm/tlbflush.h | 1 +
 arch/x86/mm/tlb.c               | 3 ++-
 3 files changed, 8 insertions(+), 1 deletion(-)
arch/x86/include/asm/tlbbatch.h +5 -0
index 1ad56eb..80aaf64 100644
@@ -10,6 +10,11 @@ struct arch_tlbflush_unmap_batch {
 	 * the PFNs being flushed..
 	 */
 	struct cpumask cpumask;
+	/*
+	 * Set if pages were unmapped from any MM, even one that does not
+	 * have active CPUs in its cpumask.
+	 */
+	bool unmapped_pages;
 };
 
 #endif /* _ARCH_X86_TLBBATCH_H */
arch/x86/include/asm/tlbflush.h +1 -0
index 7cad283..a9af875 100644
@@ -353,6 +353,7 @@ static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *b
 {
 	inc_mm_tlb_gen(mm);
 	cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm));
+	batch->unmapped_pages = true;
 	mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL);
 }
 
arch/x86/mm/tlb.c +2 -1
index 0efd990..0925768 100644
@@ -1633,8 +1633,9 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch)
 	 * a local TLB flush is needed. Optimize this use-case by calling
 	 * flush_tlb_func_local() directly in this case.
 	 */
-	if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) {
+	if (cpu_feature_enabled(X86_FEATURE_INVLPGB) && batch->unmapped_pages) {
 		invlpgb_flush_all_nonglobals();
+		batch->unmapped_pages = false;
 	} else if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) {
 		flush_tlb_multi(&batch->cpumask, info);
 	} else if (cpumask_test_cpu(cpu, &batch->cpumask)) {