* [PATCH v2 1/5] x86/hyperv: Reject unrepresentable TLB flush VP sets
2026-11-17 9:15 [PATCH v2 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
@ 2026-11-17 9:15 ` Naman Jain
2026-11-17 9:15 ` [PATCH v2 2/5] x86/tlb: Make the conditional flush predicate reusable Naman Jain
` (3 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Naman Jain @ 2026-11-17 9:15 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86, luto,
peterz
Cc: hpa, riel, mhklinux, bigeasy, zhouchuyi, houwenlong.hwl,
qq570070308, bastien.curutchet, namjain, dianders, jgross, hawk,
linux-hyperv, linux-kernel
The VP-set conversion helper returns zero when it cannot represent the
maximum VP index. Treat zero as an error, just like negative results,
rather than issuing an extended flush with an empty target set.
This lets the caller fall back to native flushing, as the extended IPI
path already does. Filtering a mask that initially includes all present
CPUs can make it use this conversion instead of the all-processors
shortcut.
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;
/*
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH v2 2/5] x86/tlb: Make the conditional flush predicate reusable
2026-11-17 9:15 [PATCH v2 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
2026-11-17 9:15 ` [PATCH v2 1/5] x86/hyperv: Reject unrepresentable TLB flush VP sets Naman Jain
@ 2026-11-17 9:15 ` Naman Jain
2026-11-17 9:15 ` [PATCH v2 3/5] x86/hyperv: Use native callbacks for trimming and ASID transitions Naman Jain
` (2 subsequent siblings)
4 siblings, 0 replies; 7+ messages in thread
From: Naman Jain @ 2026-11-17 9:15 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86, luto,
peterz
Cc: hpa, riel, mhklinux, bigeasy, zhouchuyi, houwenlong.hwl,
qq570070308, bastien.curutchet, namjain, dianders, jgross, hawk,
linux-hyperv, linux-kernel
Make should_flush_tlb() available to the Hyper-V backend and give it a
const struct flush_tlb_info pointer instead of void *. Keep a
should_flush_tlb_wrapper() adapter for the SMP callback interface.
No functional changes.
Signed-off-by: Naman Jain <namjain@linux.microsoft.com>
---
arch/x86/include/asm/tlbflush.h | 1 +
arch/x86/mm/tlb.c | 18 ++++++++++++++----
2 files changed, 15 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..9b1aea2b3a1d5 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)
+/*
+ * Select ordinary flush targets using state ordered against context switches.
+ * Callers must bypass this filtering for page-table freeing and global ASID
+ * transitions, as native_flush_tlb_multi() does.
+ */
+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,12 @@ static bool should_flush_tlb(int cpu, void *data)
return false;
}
+/* Adapt the typed predicate to the SMP conditional-call interface. */
+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 +1367,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 related [flat|nested] 7+ messages in thread* [PATCH v2 3/5] x86/hyperv: Use native callbacks for trimming and ASID transitions
2026-11-17 9:15 [PATCH v2 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
2026-11-17 9:15 ` [PATCH v2 1/5] x86/hyperv: Reject unrepresentable TLB flush VP sets Naman Jain
2026-11-17 9:15 ` [PATCH v2 2/5] x86/tlb: Make the conditional flush predicate reusable Naman Jain
@ 2026-11-17 9:15 ` Naman Jain
2026-11-17 9:15 ` [PATCH v2 4/5] x86/hyperv: Filter TLB targets before building hypercalls Naman Jain
2026-11-17 9:15 ` [PATCH v2 5/5] x86/hyperv: Request mask cleanup when no remote target remains Naman Jain
4 siblings, 0 replies; 7+ messages in thread
From: Naman Jain @ 2026-11-17 9:15 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86, luto,
peterz
Cc: hpa, riel, mhklinux, bigeasy, zhouchuyi, houwenlong.hwl,
qq570070308, bastien.curutchet, namjain, dianders, jgross, hawk,
linux-hyperv, linux-kernel
Successful Hyper-V flush hypercalls do not run the guest callbacks that
remove stale CPU-mask entries. Use the native flush path when the common
x86 code requests trimming.
Global ASID transitions also need these callbacks.
finish_asid_transition() requests a flush so that CPUs still using local
ASIDs switch to the global ASID through flush_tlb_func(). Use the native
path for these requests too, without filtering the supplied CPU mask.
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 related [flat|nested] 7+ messages in thread
* [PATCH v2 4/5] x86/hyperv: Filter TLB targets before building hypercalls
2026-11-17 9:15 [PATCH v2 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
` (2 preceding siblings ...)
2026-11-17 9:15 ` [PATCH v2 3/5] x86/hyperv: Use native callbacks for trimming and ASID transitions Naman Jain
@ 2026-11-17 9:15 ` Naman Jain
2026-11-17 9:24 ` sashiko-bot
2026-11-17 9:15 ` [PATCH v2 5/5] x86/hyperv: Request mask cleanup when no remote target remains Naman Jain
4 siblings, 1 reply; 7+ messages in thread
From: Naman Jain @ 2026-11-17 9:15 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86, luto,
peterz
Cc: hpa, riel, mhklinux, bigeasy, zhouchuyi, houwenlong.hwl,
qq570070308, bastien.curutchet, namjain, dianders, jgross, hawk,
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. The reported bisection identified
commit 209954cbc7d0 ("x86/mm/tlb: Update mm_cpumask lazily").
That change leaves a CPU's bit set after it switches to another mm.
Native flush callbacks remove these stale bits, but successful Hyper-V
hypercalls do not run those callbacks. A CPU actively 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, the backend selects
the all-processors shortcut before checking individual CPUs.
Use the native predicate to build a per-CPU target mask before selecting
the all-processors shortcut or either hypercall format. Keep the full
candidate mask when freeing page tables. Retain the empty basic-mask
check because filtering can leave no targets.
This patch does not change 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..833eaea1d3fa8 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 retain CPUs that have switched to another mm.
+ * Use the native predicate for its context-switch ordering and lazy
+ * TLB exclusion. Page-table freeing must include lazy CPUs, so keep
+ * the full candidate mask in that case.
+ */
+ 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 related [flat|nested] 7+ messages in thread* Re: [PATCH v2 4/5] x86/hyperv: Filter TLB targets before building hypercalls
2026-11-17 9:15 ` [PATCH v2 4/5] x86/hyperv: Filter TLB targets before building hypercalls Naman Jain
@ 2026-11-17 9:24 ` sashiko-bot
0 siblings, 0 replies; 7+ messages in thread
From: sashiko-bot @ 2026-11-17 9:24 UTC (permalink / raw)
To: Naman Jain; +Cc: linux-hyperv
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] x86/hyperv: missing Fixes tag for commit 209954cbc7d0
--
commit b708a994017fe50fc481b9fcff6a341c5d843b13
Author: Naman Jain <namjain@linux.microsoft.com>
> 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. The reported bisection identified
> commit 209954cbc7d0 ("x86/mm/tlb: Update mm_cpumask lazily").
>
> That change leaves a CPU's bit set after it switches to another mm.
> Native flush callbacks remove these stale bits, but successful Hyper-V
> hypercalls do not run those callbacks. A CPU actively 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, the backend selects
> the all-processors shortcut before checking individual CPUs.
>
> Use the native predicate to build a per-CPU target mask before selecting
> the all-processors shortcut or either hypercall format. Keep the full
> candidate mask when freeing page tables. Retain the empty basic-mask
> check because filtering can leave no targets.
>
> This patch does not change when trimming is requested.
>
> Signed-off-by: Naman Jain <namjain@linux.microsoft.com>
[Severity: Low]
Since this fixes the regression introduced by 209954cbc7d0 ("x86/mm/tlb:
Update mm_cpumask lazily"), should the commit message include a Fixes tag?
Fixes: 209954cbc7d0 ("x86/mm/tlb: Update mm_cpumask lazily")
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261117091507.2088642-1-namjain@linux.microsoft.com?part=4
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 5/5] x86/hyperv: Request mask cleanup when no remote target remains
2026-11-17 9:15 [PATCH v2 0/5] x86/hyperv: Account for lazy mm_cpumask maintenance Naman Jain
` (3 preceding siblings ...)
2026-11-17 9:15 ` [PATCH v2 4/5] x86/hyperv: Filter TLB targets before building hypercalls Naman Jain
@ 2026-11-17 9:15 ` Naman Jain
4 siblings, 0 replies; 7+ messages in thread
From: Naman Jain @ 2026-11-17 9:15 UTC (permalink / raw)
To: haiyangz, wei.liu, decui, tglx, mingo, bp, dave.hansen, x86, luto,
peterz
Cc: hpa, riel, mhklinux, bigeasy, zhouchuyi, houwenlong.hwl,
qq570070308, bastien.curutchet, namjain, dianders, jgross, hawk,
linux-hyperv, linux-kernel
Filtering avoids unnecessary hypercalls but leaves historical CPU-mask
entries in place. These entries can keep later flushes on the multi-CPU
path even when only local work is needed.
When no remote target remains after filtering, request native trimming
of the original candidate mask instead of waiting for the periodic trim.
Copy the const request so only this call requests cleanup.
The native predicate 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 833eaea1d3fa8..545a25fcf6c34 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,
}
}
+ /*
+ * 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 related [flat|nested] 7+ messages in thread