* [PATCH v3 1/5] x86/hyperv: Reject unrepresentable TLB flush VP sets
2026-12-01 4:36 [PATCH v3 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
@ 2026-12-01 4:36 ` Naman Jain
2026-12-01 4:36 ` [PATCH v3 2/5] x86/tlb: Make should_flush_tlb() available to Hyper-V Naman Jain
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Naman Jain @ 2026-12-01 4:36 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86,
luto, peterz
Cc: dave.hansen, hpa, riel, mhklinux, schandna, namjain, qq570070308,
houwenlong.hwl, zhouchuyi, jgross, linux-hyperv, linux-kernel
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.
Signed-off-by: Naman Jain <namjain@linux.microsoft.com>
---
arch/x86/hyperv/mmu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/x86/hyperv/mmu.c b/arch/x86/hyperv/mmu.c
index cfcb60468b01c..1ea57d642faa4 100644
--- a/arch/x86/hyperv/mmu.c
+++ b/arch/x86/hyperv/mmu.c
@@ -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
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v3 2/5] x86/tlb: Make should_flush_tlb() available to Hyper-V
2026-12-01 4:36 [PATCH v3 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
2026-12-01 4:36 ` [PATCH v3 1/5] x86/hyperv: Reject unrepresentable TLB flush VP sets Naman Jain
@ 2026-12-01 4:36 ` Naman Jain
2026-12-01 4:36 ` [PATCH v3 3/5] x86/hyperv: Use native flushes for mask trimming and ASID transitions Naman Jain
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Naman Jain @ 2026-12-01 4:36 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86,
luto, peterz
Cc: dave.hansen, hpa, riel, mhklinux, schandna, namjain, qq570070308,
houwenlong.hwl, zhouchuyi, jgross, linux-hyperv, linux-kernel
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.
Signed-off-by: Naman Jain <namjain@linux.microsoft.com>
---
arch/x86/include/asm/tlbflush.h | 1 +
arch/x86/mm/tlb.c | 17 +++++++++++++----
2 files changed, 14 insertions(+), 4 deletions(-)
diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h
index 70098d448e999..1d1852ecaf084 100644
--- a/arch/x86/include/asm/tlbflush.h
+++ b/arch/x86/include/asm/tlbflush.h
@@ -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)
{
diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c
index 53dcab39566f2..2990aab1f8ca3 100644
--- a/arch/x86/mm/tlb.c
+++ b/arch/x86/mm/tlb.c
@@ -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,
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v3 3/5] x86/hyperv: Use native flushes for mask trimming and ASID transitions
2026-12-01 4:36 [PATCH v3 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
2026-12-01 4:36 ` [PATCH v3 1/5] x86/hyperv: Reject unrepresentable TLB flush VP sets Naman Jain
2026-12-01 4:36 ` [PATCH v3 2/5] x86/tlb: Make should_flush_tlb() available to Hyper-V Naman Jain
@ 2026-12-01 4:36 ` Naman Jain
2026-12-01 4:36 ` [PATCH v3 4/5] x86/hyperv: Filter TLB targets before building hypercalls Naman Jain
2026-12-01 4:36 ` [PATCH v3 5/5] x86/hyperv: Request mask cleanup when no remote target remains Naman Jain
4 siblings, 0 replies; 6+ messages in thread
From: Naman Jain @ 2026-12-01 4:36 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86,
luto, peterz
Cc: dave.hansen, hpa, riel, mhklinux, schandna, namjain, qq570070308,
houwenlong.hwl, zhouchuyi, jgross, linux-hyperv, linux-kernel
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.
Signed-off-by: Naman Jain <namjain@linux.microsoft.com>
---
arch/x86/hyperv/mmu.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/arch/x86/hyperv/mmu.c b/arch/x86/hyperv/mmu.c
index 1ea57d642faa4..375f23b157ab3 100644
--- a/arch/x86/hyperv/mmu.c
+++ b/arch/x86/hyperv/mmu.c
@@ -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);
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v3 4/5] x86/hyperv: Filter TLB targets before building hypercalls
2026-12-01 4:36 [PATCH v3 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
` (2 preceding siblings ...)
2026-12-01 4:36 ` [PATCH v3 3/5] x86/hyperv: Use native flushes for mask trimming and ASID transitions Naman Jain
@ 2026-12-01 4:36 ` Naman Jain
2026-12-01 4:36 ` [PATCH v3 5/5] x86/hyperv: Request mask cleanup when no remote target remains Naman Jain
4 siblings, 0 replies; 6+ messages in thread
From: Naman Jain @ 2026-12-01 4:36 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86,
luto, peterz
Cc: dave.hansen, hpa, riel, mhklinux, schandna, namjain, qq570070308,
houwenlong.hwl, zhouchuyi, jgross, linux-hyperv, linux-kernel
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.
Signed-off-by: Naman Jain <namjain@linux.microsoft.com>
---
arch/x86/hyperv/mmu.c | 40 ++++++++++++++++++++++++++--------------
1 file changed, 26 insertions(+), 14 deletions(-)
diff --git a/arch/x86/hyperv/mmu.c b/arch/x86/hyperv/mmu.c
index 375f23b157ab3..15809bb4a5ebb 100644
--- a/arch/x86/hyperv/mmu.c
+++ b/arch/x86/hyperv/mmu.c
@@ -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;
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v3 5/5] x86/hyperv: Request mask cleanup when no remote target remains
2026-12-01 4:36 [PATCH v3 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
` (3 preceding siblings ...)
2026-12-01 4:36 ` [PATCH v3 4/5] x86/hyperv: Filter TLB targets before building hypercalls Naman Jain
@ 2026-12-01 4:36 ` Naman Jain
4 siblings, 0 replies; 6+ messages in thread
From: Naman Jain @ 2026-12-01 4:36 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86,
luto, peterz
Cc: dave.hansen, hpa, riel, mhklinux, schandna, namjain, qq570070308,
houwenlong.hwl, zhouchuyi, jgross, linux-hyperv, linux-kernel
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@linux.microsoft.com>
---
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
^ permalink raw reply [flat|nested] 6+ messages in thread