Skip to contentSkip to conversation

x86/hyperv: Account for lazy mm_cpumask maintenance

PATCH New Naman Jain 2 participants 8 messages Dec 1

Subsystems (4)
lore
Share & tools
lore

Naman Jain

author
#0 lore raw

v3 cover x86/hyperv: Account for lazy mm_cpumask maintenance

Lazy mm_cpumask maintenance leaves a CPU's bit set after it switches to another address space. Linux removes stale bits by running flush_tlb_func() on the target CPUs. A Hyper-V TLB flush hypercall invalidates translations but does not run this function. Stale bits can therefore remain and cause unnecessary remote invalidations.

hyperv_flush_tlb_multi() can also select HV_FLUSH_ALL_PROCESSORS from a mask containing stale bits before reaching its per-CPU lazy-TLB checks.

The regression was found in SuperBench's eight-GPU PyTorch LSTM training on Azure MI300X VMs when comparing Linux 6.8 and 7.0. Recreating DataLoader workers each epoch caused host-to-GPU copy stalls and timeouts. Bisection identified commit 209954cbc7d0 ("x86/mm/tlb: Update mm_cpumask lazily") as the cause of the regression.

Patches 1-3 prepare for target filtering in patch 4 while patch 5 adds immediate cleanup.

Patch 1 falls back to native flushing if VP-set conversion returns zero. This can happen when the maximum VP index is at least 4096. The check is defensive; the issue has not been seen in practice.

Patch 2 lets the Hyper-V PV TLB flush functions call should_flush_tlb(). Its argument is const because the function only reads the request. A wrapper keeps the void * argument required by on_each_cpu_cond_mask().

Patch 3 uses native flushing when mask trimming is requested or a global ASID transition is in progress. This lets flush_tlb_func() run on the target CPUs to clear stale mask bits or switch to the global ASID.

Patch 4 uses should_flush_tlb() to decide which CPUs to include in a Hyper-V flush before choosing the hypercall format or the all-processors flag. When freeing page tables, it keeps the full mask to include CPUs in lazy TLB mode. Stale bits can still cause all-processor flushes in that case.

Patch 5 requests native cleanup if filtering leaves no remote CPU that needs a flush. Removing stale mm_cpumask() bits lets later flushes take the local-only path when possible. Lazy CPUs are still skipped, so some bits may remain.

The earlier mask-weight > mm_users experiment on a 32-vCPU Hyper-V VM found that lazy bits could keep the comparison true across 10,000 flush requests. With three remote readers, mm_users was 4 and mask weights were 26-32. Each reader received 10,000 native callbacks; the corresponding run using cleanup only when no remote target remained used hypercalls.
A doubled threshold and a rate-limited policy have not been tested.

On a 32-vCPU Hyper-V VM, after a single-threaded process visited all CPUs and returned to one CPU, the reproducer's median time per iteration (two mprotect() calls and a write) dropped from 17.235 us to 1.622 us with the fix. On the reporter's 96-vCPU VM, the application hit the 600-second timeout without the fix and completed in 424 and 436 seconds with it.

Changes since v2:
https://lore.kernel.org/all/[email protected]/
* Replace vague descriptions with function names in commit messages and comments (Dave, Michael).
* Describe the VP-set limit as theoretical and remove the confusing explanation about filtering an all-present mask (Michael).
* Use the agreed wording about successfully taking the Hyper-V hypercall path, and the accepted patch 3 commit message (Michael).
* Make the cleanup comment conditional and name mm_cpumask() explicitly in its commit message (Michael).
* Clarify that page-table-freeing flushes can still target stale members (Sahil).

Changes since v1:
https://lore.kernel.org/all/[email protected]/
* Split the change into five patches with separate explanations (Dave).
* Add the workload and regression context (Michael).
* Explain ASID handling and filtering exceptions separately (Dave).
* Test the mm_users heuristic and keep immediate cleanup separate for policy review (Dave).

Naman Jain (5):
x86/hyperv: Reject unrepresentable TLB flush VP sets x86/tlb: Make should_flush_tlb() available to Hyper-V x86/hyperv: Use native flushes for mask trimming and ASID transitions x86/hyperv: Filter TLB targets before building hypercalls x86/hyperv: Request mask cleanup when no remote target remains

 arch/x86/hyperv/mmu.c           | 71 +++++++++++++++++++++++----------
 arch/x86/include/asm/tlbflush.h |  1 +
 arch/x86/mm/tlb.c               | 17 ++++++--
 3 files changed, 64 insertions(+), 25 deletions(-)

base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v3 1/5 x86/hyperv: Reject unrepresentable TLB flush VP sets

cpumask_to_vpset_skip() returns zero if hv_max_vp_index is at least 4096, beyond the range of its sparse VP set. Treat zero as an error instead of issuing an extended flush with an empty target set.

This makes hyperv_flush_tlb_multi() fall back to native flushing, matching the extended IPI path.

This is a defensive check and the issue is not seen in practice.

---
 arch/x86/hyperv/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/hyperv/mmu.c +1 -1
index cfcb60468b01c..1ea57d642faa4 100644
@@ -199,7 +199,7 @@ static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
 	flush->hv_vp_set.format = HV_GENERIC_SET_SPARSE_4K;
 	nr_bank = cpumask_to_vpset_skip(&flush->hv_vp_set, cpus,
 			info->freed_tables ? NULL : cpu_is_lazy);
-	if (nr_bank < 0)
+	if (nr_bank <= 0)
 		return HV_STATUS_INVALID_PARAMETER;
 
 	/*

base-commit: e5aad2bdbbee783611d32599f89cea3f12f9593d

Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v3 2/5 x86/tlb: Make should_flush_tlb() available to Hyper-V

In reply to Naman Jain

Make should_flush_tlb() available to the Hyper-V PV TLB flush functions so they can use the same checks as native_flush_tlb_multi().

Use a const struct flush_tlb_info pointer for direct callers. This preserves the read-only request passed by hyperv_flush_tlb_multi() without a cast and lets the compiler check the argument type. Keep a small wrapper for on_each_cpu_cond_mask(), which takes void *.

No functional changes.

---
 arch/x86/include/asm/tlbflush.h |  1 +
 arch/x86/mm/tlb.c               | 17 +++++++++++++----
 2 files changed, 14 insertions(+), 4 deletions(-)
arch/x86/include/asm/tlbflush.h +1 -0
index 70098d448e999..1d1852ecaf084 100644
@@ -263,6 +263,7 @@ void flush_tlb_one_user(unsigned long addr);
 void flush_tlb_one_kernel(unsigned long addr);
 void flush_tlb_multi(const struct cpumask *cpumask,
 		      const struct flush_tlb_info *info);
+bool should_flush_tlb(int cpu, const struct flush_tlb_info *info);
 
 static inline bool is_dyn_asid(u16 asid)
 {
arch/x86/mm/tlb.c +13 -4
index 53dcab39566f2..2990aab1f8ca3 100644
@@ -1279,10 +1279,14 @@ static void flush_tlb_func(void *info)
 			nr_invalidate);
 }
 
-static bool should_flush_tlb(int cpu, void *data)
+/*
+ * Callers must bypass should_flush_tlb() when freeing page tables or
+ * during a global ASID transition, so CPUs in lazy TLB mode also
+ * receive callbacks.
+ */
+bool should_flush_tlb(int cpu, const struct flush_tlb_info *info)
 {
 	struct mm_struct *loaded_mm = per_cpu(cpu_tlbstate.loaded_mm, cpu);
-	struct flush_tlb_info *info = data;
 
 	/*
 	 * Order the 'loaded_mm' and 'is_lazy' against their
@@ -1317,6 +1321,11 @@ static bool should_flush_tlb(int cpu, void *data)
 	return false;
 }
 
+static bool should_flush_tlb_wrapper(int cpu, void *data)
+{
+	return should_flush_tlb(cpu, data);
+}
+
 static bool should_trim_cpumask(struct mm_struct *mm)
 {
 	if (time_after(jiffies, READ_ONCE(mm->context.next_trim_cpumask))) {
@@ -1357,8 +1366,8 @@ STATIC_NOPV void native_flush_tlb_multi(const struct cpumask *cpumask,
 	if (info->freed_tables || mm_in_asid_transition(info->mm))
 		on_each_cpu_mask(cpumask, flush_tlb_func, (void *)info, true);
 	else
-		on_each_cpu_cond_mask(should_flush_tlb, flush_tlb_func,
-				(void *)info, 1, cpumask);
+		on_each_cpu_cond_mask(should_flush_tlb_wrapper, flush_tlb_func,
+				      (void *)info, 1, cpumask);
 }
 
 void flush_tlb_multi(const struct cpumask *cpumask,
Signature · 1 line
2.43.0
author
#3 lore raw

PATCH v3 3/5 x86/hyperv: Use native flushes for mask trimming and ASID transitions

In reply to Naman Jain

When trimming mm_cpumask(), Linux needs to run flush_tlb_func() on remote CPUs so they can clear their stale bits. A Hyper-V TLB flush hypercall invalidates translations but does not run this function.

The same applies during a global ASID transition. CPUs still using local ASIDs need to run flush_tlb_func() to switch to the global ASID.

Call native_flush_tlb_multi() in both cases so the required work runs on the target CPUs.

---
 arch/x86/hyperv/mmu.c | 8 ++++++++
 1 file changed, 8 insertions(+)
arch/x86/hyperv/mmu.c +8 -0
index 1ea57d642faa4..375f23b157ab3 100644
@@ -70,6 +70,14 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
 	if (!hv_hypercall_pg)
 		goto do_native;
 
+	/*
+	 * Trimming needs callbacks on stale members. Global ASID transitions
+	 * need callbacks to switch CPUs still using local ASIDs. A Hyper-V
+	 * flush hypercall does neither.
+	 */
+	if (info->trim_cpumask || mm_in_asid_transition(info->mm))
+		goto do_native;
+
 	local_irq_save(flags);
 
 	flush = *this_cpu_ptr(hyperv_pcpu_input_arg);
Signature · 1 line
2.43.0
author
#4 lore raw

PATCH v3 4/5 x86/hyperv: Filter TLB targets before building hypercalls

In reply to Naman Jain

SuperBench's eight-GPU PyTorch LSTM training on Azure MI300X VMs regressed between Linux 6.8 and 7.0. Recreating DataLoader workers each epoch caused host-to-GPU copy stalls and timeouts. Bisection identified commit 209954cbc7d0 ("x86/mm/tlb: Update mm_cpumask lazily") as the cause of the regression.

That change leaves a CPU's bit set in mm_cpumask() even after the CPU switches to another mm. Native flush callbacks remove these stale bits, but successfully taking the Hyper-V hypercall path does not run those callbacks. A CPU running another mm need not be in lazy TLB mode, so the existing lazy-TLB check does not exclude it. If the mask includes all present CPUs, hyperv_flush_tlb_multi() selects HV_FLUSH_ALL_PROCESSORS before checking individual CPUs.

Use should_flush_tlb() to build a per-CPU target mask before selecting HV_FLUSH_ALL_PROCESSORS or either hypercall format. Keep the full mask when freeing page tables, since should_flush_tlb() excludes lazy CPUs that still need invalidation in that case. Retain the empty basic-mask check because filtering can leave no targets.

This patch does not affect when trimming is requested.

---
 arch/x86/hyperv/mmu.c | 40 ++++++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 14 deletions(-)
arch/x86/hyperv/mmu.c +26 -14
index 375f23b157ab3..15809bb4a5ebb 100644
@@ -1,6 +1,7 @@
 #define pr_fmt(fmt)  "Hyper-V: " fmt
 
 #include <linux/log2.h>
+#include <linux/percpu.h>
 #include <linux/slab.h>
 #include <linux/types.h>
 
@@ -16,6 +17,8 @@
 /* Each gva in gva_list encodes up to 4096 pages to flush */
 #define HV_TLB_FLUSH_UNIT (4096 * PAGE_SIZE)
 
+static DEFINE_PER_CPU(struct cpumask, hyperv_tlb_flush_mask);
+
 static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
 				      const struct flush_tlb_info *info);
 
@@ -51,19 +54,14 @@ static inline int fill_gva_list(u64 gva_list[], int offset,
 	return gva_n - offset;
 }
 
-static bool cpu_is_lazy(int cpu)
-{
-	return per_cpu(cpu_tlbstate_shared.is_lazy, cpu);
-}
-
 static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
 				   const struct flush_tlb_info *info)
 {
 	int cpu, vcpu, gva_n, max_gvas;
 	struct hv_tlb_flush *flush;
+	struct cpumask *flush_cpus;
 	u64 status;
 	unsigned long flags;
-	bool do_lazy = !info->freed_tables;
 
 	trace_hyperv_mmu_flush_tlb_multi(cpus, info);
 
@@ -87,6 +85,23 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
 		goto do_native;
 	}
 
+	flush_cpus = this_cpu_ptr(&hyperv_tlb_flush_mask);
+	/*
+	 * mm_cpumask() may include CPUs that have switched to another mm.
+	 * Use should_flush_tlb() to check which CPUs need this flush.
+	 * When freeing page tables, keep the full mask because CPUs in
+	 * lazy TLB mode must also be flushed.
+	 */
+	if (info->freed_tables) {
+		cpumask_copy(flush_cpus, cpus);
+	} else {
+		cpumask_clear(flush_cpus);
+		for_each_cpu(cpu, cpus) {
+			if (should_flush_tlb(cpu, info))
+				__cpumask_set_cpu(cpu, flush_cpus);
+		}
+	}
+
 	if (info->mm) {
 		/*
 		 * AddressSpace argument must match the CR3 with PCID bits
@@ -101,7 +116,7 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
 	}
 
 	flush->processor_mask = 0;
-	if (cpumask_equal(cpus, cpu_present_mask)) {
+	if (cpumask_equal(flush_cpus, cpu_present_mask)) {
 		flush->flags |= HV_FLUSH_ALL_PROCESSORS;
 	} else {
 		/*
@@ -115,14 +130,12 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
 		 * must. We will also check all VP numbers when walking the
 		 * supplied CPU set to remain correct in all cases.
 		 */
-		cpu = cpumask_last(cpus);
+		cpu = cpumask_last(flush_cpus);
 
 		if (cpu < nr_cpumask_bits && hv_cpu_number_to_vp_number(cpu) >= 64)
 			goto do_ex_hypercall;
 
-		for_each_cpu(cpu, cpus) {
-			if (do_lazy && cpu_is_lazy(cpu))
-				continue;
+		for_each_cpu(cpu, flush_cpus) {
 			vcpu = hv_cpu_number_to_vp_number(cpu);
 			if (vcpu == VP_INVAL) {
 				local_irq_restore(flags);
@@ -166,7 +179,7 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus,
 	goto check_status;
 
 do_ex_hypercall:
-	status = hyperv_flush_tlb_others_ex(cpus, info);
+	status = hyperv_flush_tlb_others_ex(flush_cpus, info);
 
 check_status:
 	local_irq_restore(flags);
@@ -205,8 +218,7 @@ static u64 hyperv_flush_tlb_others_ex(const struct cpumask *cpus,
 	flush->hv_vp_set.valid_bank_mask = 0;
 
 	flush->hv_vp_set.format = HV_GENERIC_SET_SPARSE_4K;
-	nr_bank = cpumask_to_vpset_skip(&flush->hv_vp_set, cpus,
-			info->freed_tables ? NULL : cpu_is_lazy);
+	nr_bank = cpumask_to_vpset(&flush->hv_vp_set, cpus);
 	if (nr_bank <= 0)
 		return HV_STATUS_INVALID_PARAMETER;
 
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v3 4/5] x86/hyperv: Filter TLB targets before building hypercalls

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] x86/hyperv: missing Fixes tag for commit 209954cbc7d0 --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] x86/hyperv: missing Fixes tag for commit 209954cbc7d0 --

commit ffd1be8a9bff8ab7dcfc38d55cedf5571c8c7235
Author: Naman Jain <[email protected]>

Quoted 22 lines

x86/hyperv: Filter TLB targets before building hypercalls

SuperBench's eight-GPU PyTorch LSTM training on Azure MI300X VMs regressed between Linux 6.8 and 7.0. Recreating DataLoader workers each epoch caused host-to-GPU copy stalls and timeouts. Bisection identified commit 209954cbc7d0 ("x86/mm/tlb: Update mm_cpumask lazily") as the cause of the regression.

That change leaves a CPU's bit set in mm_cpumask() even after the CPU switches to another mm. Native flush callbacks remove these stale bits, but successfully taking the Hyper-V hypercall path does not run those callbacks. A CPU running another mm need not be in lazy TLB mode, so the existing lazy-TLB check does not exclude it. If the mask includes all present CPUs, hyperv_flush_tlb_multi() selects HV_FLUSH_ALL_PROCESSORS before checking individual CPUs.

Use should_flush_tlb() to build a per-CPU target mask before selecting HV_FLUSH_ALL_PROCESSORS or either hypercall format. Keep the full mask when freeing page tables, since should_flush_tlb() excludes lazy CPUs that still need invalidation in that case. Retain the empty basic-mask check because filtering can leave no targets.

Quoted 3 lines

This patch does not affect when trimming is requested.

[Severity: Low]
Since this fixes a regression caused by lazy mm_cpumask updates, should the commit message include:

Fixes 209954cbc7d0 ("x86/mm/tlb: Update mm_cpumask lazily")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#6 lore raw

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

In reply to Naman Jain

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.

---
 arch/x86/hyperv/mmu.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)
arch/x86/hyperv/mmu.c +15 -6
index 15809bb4a5ebb..c74d3f109aa13 100644
@@ -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;
-		}
 	}
 
 	/*
Signature · 1 line
2.43.0
#7 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] x86/hyperv: unthrottled mm_cpumask trimming in hyperv_flush_tlb_multi() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] x86/hyperv: unthrottled mm_cpumask trimming in hyperv_flush_tlb_multi()
--

commit 23e911529adea55b9d5f4467fb5c265f2dce940f
Author: Naman Jain <[email protected]>

x86/hyperv: Request mask cleanup when no remote target remains

This commit falls back to native_flush_tlb_multi() with trim_cpumask enabled when target filtering leaves no remote CPUs in flush_cpus, and removes the redundant empty processor_mask check.

Quoted 12 lines
arch/x86/hyperv/mmu.c +4 -0
index 15809bb4a5ebb..c74d3f109aa13 100644
@@ -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.

arch/x86/hyperv/mmu.c static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · line 118

Quoted 10 lines
arch/x86/hyperv/mmu.c +10 -0
+	 */
+	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;
+	}

[Severity: Medium]
Does forcing trim_info.trim_cpumask = 1 and passing the unfiltered cpus mask here bypass should_trim_cpumask()'s 1 Hz rate limit?

hyperv_flush_tlb_multi() already branches to do_native when info->trim_cpumask is set by flush_tlb_mm_range(), so this block only runs when should_trim_cpumask() throttled trimming. In workloads where sibling threads frequently run on remote CPUs and switch away between TLB flushes, forcing trim_cpumask = 1 on the unfiltered cpus mask causes should_flush_tlb() to match every stale non-lazy CPU in mm_cpumask(mm), sending synchronous trim IPIs and contending on mm_cpumask(mm) on every local-only flush instead of at most once per second.

When the stale remote CPUs in mm_cpumask(mm) are in lazy TLB mode, should_flush_tlb() returns false even with trim_cpumask = 1, so passing cpus rather than flush_cpus does not clear them from mm_cpumask(mm) and instead makes smp_call_function_many_cond() rescan all remote CPUs in cpus and re-run should_flush_tlb() on every flush.

Passing flush_cpus and info without overriding trim_cpumask (native_flush_tlb_multi(flush_cpus, info)) would flush only the local CPU without IPIs, hypercalls or remote CPU rescans while leaving mm_cpumask trimming to the 1 Hz should_trim_cpumask() path.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5