[PATCH v3 5/5] x86/hyperv: Request mask cleanup when no remote target remains

From: Naman Jain

Date: Mon Nov 30 2026 - 23:37:24 EST


Filtering avoids flushing CPUs that do not need it, but does not remove
stale entries from mm_cpumask(). These entries can keep later flushes on
the multi-CPU path even when only the local CPU needs to be flushed.

If no remote CPU needs a flush after filtering, call
native_flush_tlb_multi() with the original CPU mask and request trimming
of mm_cpumask() instead of waiting for the periodic trim. Copy the const
request so only this call requests cleanup.

should_flush_tlb() still skips lazy CPUs, so cleanup may not leave a
local-only mask. The new early return also makes the empty basic
processor-mask check unnecessary.

Signed-off-by: Naman Jain <namjain@xxxxxxxxxxxxxxxxxxx>
---
arch/x86/hyperv/mmu.c | 21 +++++++++++++++------
1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/arch/x86/hyperv/mmu.c b/arch/x86/hyperv/mmu.c
index 15809bb4a5ebb..c74d3f109aa13 100644
--- a/arch/x86/hyperv/mmu.c
+++ b/arch/x86/hyperv/mmu.c
@@ -102,6 +102,21 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
}
}

+ /*
+ * If no remote target remains after filtering, request native cleanup
+ * of stale, non-lazy memberships so later flushes can take the local
+ * fast path when possible. Lazy memberships may remain.
+ */
+ if (cpumask_any_but(flush_cpus, smp_processor_id()) >= nr_cpu_ids) {
+ /* Keep the caller's const request unchanged. */
+ struct flush_tlb_info trim_info = *info;
+
+ trim_info.trim_cpumask = 1;
+ local_irq_restore(flags);
+ native_flush_tlb_multi(cpus, &trim_info);
+ return;
+ }
+
if (info->mm) {
/*
* AddressSpace argument must match the CR3 with PCID bits
@@ -148,12 +163,6 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
__set_bit(vcpu, (unsigned long *)
&flush->processor_mask);
}
-
- /* nothing to flush if 'processor_mask' ends up being empty */
- if (!flush->processor_mask) {
- local_irq_restore(flags);
- return;
- }
}

/*
--
2.43.0