x86/hyperv: Account for lazy mm_cpumask maintenance
Subsystems (4)
v2 cover x86/hyperv: Account for lazy mm_cpumask maintenance
Lazy mm_cpumask maintenance leaves CPU bits set after switching to another address space, with stale memberships removed by native flush callbacks. Successful Hyper-V TLB flush hypercalls do not run those callbacks, so historical memberships can persist and cause unnecessary remote flushes. If the mask covers all present CPUs, the backend selects an all-processor flush before reaching its per-CPU lazy-TLB checks.
This was found while investigating a regression between Linux 6.8 and 7.0 in SuperBench's eight-GPU PyTorch LSTM training on Azure MI300X VMs. 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"). This is distinct from lazy TLB
mode: a CPU actively running another address space need not be lazy.
Patches 1-3 prepare for target filtering in patch 4 while patch 5 adds immediate cleanup.
Patch 1 rejects VP-set conversion failures that return zero, allowing native fallback rather than issuing an empty-target extended hypercall.
Patch 2 makes the native target predicate reusable, retaining its context-switch ordering and adding a clearly named callback wrapper.
Patch 3 uses native callbacks for requested mask trimming and global ASID transitions. The latter need guest callbacks to switch CPUs still using local ASIDs; a Hyper-V invalidation does not do that.
Patch 4 filters targets before selecting either hypercall format or the all-processors shortcut. Page-table-freeing flushes remain unfiltered.
Patch 5 separately requests best-effort cleanup when no remote target remains after filtering. Lazy memberships may survive this cleanup. The cleanup policy remains open for discussion.
The suggested mask-weight > mm_users heuristic was tested as a replacement for patch 5's approach on a 32-vCPU Hyper-V VM. With three remote readers, mm_users was 4 while lazy memberships kept mask weights at 26-32. The heuristic requested trimming for all 10,000 flushes, and each reader received 10,000 remote callbacks. The scoped approach used Hyper-V hypercalls instead. A doubled threshold or rate-limited policy has not been tested.
Earlier testing of the monolithic patch reduced the 32-vCPU all-history reproducer from 17.235 us to 1.622 us per iteration (two mprotect() calls and a write). The reporter's application hit the 600-second timeout on the unpatched 96-vCPU VM and completed in 424 and 436 seconds with the patch.
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 the ASID fallback separately from the reported regression (Dave).
* Keep related predicate/wrapper names and comment the definition rather than the header declaration (Dave).
* Explain target filtering, page-table freeing, and the no-remote-target cleanup condition. Document the const copy and lazy-membership limit (Dave).
* Test the mm_users heuristic and retain scoped cleanup as a separate policy discussion (Dave).
* Omit Fixes tags and stable requests (Dave).
* Rebase onto next-20261116.
Naman Jain (5):
x86/hyperv: Reject unrepresentable TLB flush VP sets x86/tlb: Make the conditional flush predicate reusable x86/hyperv: Use native callbacks for 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 | 18 +++++++-- 3 files changed, 65 insertions(+), 25 deletions(-)
base-commit: 749a4de370409464cf8501ec5b2451df45c84eb4
Signature · 1 line
2.43.0
PATCH v2 1/5 x86/hyperv: Reject unrepresentable TLB flush VP sets
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.
--- arch/x86/hyperv/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/hyperv/mmu.c +1 -1
@@ -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 ;/*
Signature · 1 line
2.43.0
RE: [PATCH v2 1/5] x86/hyperv: Reject unrepresentable TLB flush VP sets
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 3 lines
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 is more a theoretical issue than a real issue, right? The max VP index would need to be >= 4096 for this error to occur, and to my knowledge the max vCPU count in a Hyper-V VM is something smaller than 4096.
Quoted 5 lines
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.
I don't understand the last sentence. Is it a leftover or a bad copy/paste?
Michael
Quoted 23 lines
Signed-off-by Naman Jain <[email protected]>--- arch/x86/hyperv/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/hyperv/mmu.c +1
-1@@ -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
Re: [PATCH v2 1/5] x86/hyperv: Reject unrepresentable TLB flush VP sets
Quoted 10 lines · Michael Kelley
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 3 lines
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 is more a theoretical issue than a real issue, right? The max VP index would need to be >= 4096 for this error to occur, and to my knowledge the max vCPU count in a Hyper-V VM is something smaller than 4096.
Right, this is more of a defensive solution. I can remove this if at all, you feel this is not required at the moment. Or else, I can make it clear in the commit message.
Quoted 9 lines
Quoted 5 lines
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.
I don't understand the last sentence. Is it a leftover or a bad copy/paste?
Michael
I meant that removing stale CPUs from an all-present mask can make the flush use an explicit VP set instead of HV_FLUSH_ALL_PROCESSORS. The control flow would then reach this condition more often after these changes. But that doesn't establish that the conversion limit can actually be reached. The sentence adds confusion, so I'll remove it.
Regards,
Naman
Quoted 24 lines
Quoted 24 lines
Signed-off-by Naman Jain <[email protected]>--- arch/x86/hyperv/mmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)arch/x86/hyperv/mmu.c +1
-1@@ -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
PATCH v2 2/5 x86/tlb: Make the conditional flush predicate reusable
In reply to Naman JainMake 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.
--- arch/x86/include/asm/tlbflush.h | 1 + arch/x86/mm/tlb.c | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-)
arch/x86/include/asm/tlbflush.h +1 -0
@@ -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 +14 -4
@@ -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,
Signature · 1 line
2.43.0
Re: [PATCH v2 2/5] x86/tlb: Make the conditional flush predicate reusable
arch/x86/mm/tlb.c static bool should_flush_tlb(int cpu, void *data) · line 1328
Quoted 5 lines · Naman Jain
arch/x86/mm/tlb.c +5
-0+/* 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); +}
Could we please fix up the LLM-generated comments like this before sending them for review? This is not the way humans write comments.
Re: [PATCH v2 2/5] x86/tlb: Make the conditional flush predicate reusable
Quoted 9 lines · Dave Hansen
arch/x86/mm/tlb.c static bool should_flush_tlb(int cpu, void *data) · line 1328
Quoted 5 lines · Naman Jain
arch/x86/mm/tlb.c +5
-0+/* 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); +}Could we please fix up the LLM-generated comments like this before sending them for review? This is not the way humans write comments.
Sorry, I agree. I'll remove/fix them. Thanks for your patience.
Regards,
Naman
RE: [PATCH v2 2/5] x86/tlb: Make the conditional flush predicate reusable
In reply to Naman JainFrom: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 1 line
In the patch Subject, avoid the "flush predicate" terminology (see my comment on Patch 4).
Quoted 1 line
Make
should_flush_tlb()available to the Hyper-V backend and give it a
What is the Hyper-V "backend"? Maybe more specific would be "PV tlb flush functions"?
Quoted 2 lines
const struct
flush_tlb_infopointer instead of void *. Keep ashould_flush_tlb_wrapper()adapter for the SMP callback interface.
Is there any particular reason for needing a specific type instead of keeping the void *? I'm just wondering what value that change adds other than the compiler being able to typecheck the call as made from the Hyper-V code.
Michael
Quoted 70 lines
No functional changes.
Signed-off-by Naman Jain <[email protected]>--- arch/x86/include/asm/tlbflush.h | 1 + arch/x86/mm/tlb.c | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-)arch/x86/include/asm/tlbflush.h +1
-0@@ -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 +14
-4@@ -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
Re: [PATCH v2 2/5] x86/tlb: Make the conditional flush predicate reusable
Quoted 5 lines · Michael Kelley
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 1 line
In the patch Subject, avoid the "flush predicate" terminology (see my comment on Patch 4).
Acked. I can use this:
[PATCH v3 2/5] x86/tlb: Make should_flush_tlb() available to Hyper-V
Quoted 5 lines
Quoted 2 lines
Make
should_flush_tlb()available to the Hyper-V backend and give it aWhat is the Hyper-V "backend"? Maybe more specific would be "PV tlb flush functions"?
yes, I should change it to "Hyper-V PV TLB flush functions".
Quoted 10 lines
Quoted 3 lines
const struct
flush_tlb_infopointer instead of void *. Keep ashould_flush_tlb_wrapper()adapter for the SMP callback interface.Is there any particular reason for needing a specific type instead of keeping the void *? I'm just wondering what value that change adds other than the compiler being able to typecheck the call as made from the Hyper-V code.
Michael
No functional reason. I changed the type because the Hyper-V caller passes a "const flush_tlb_info *" while should_flush_tlb() expects "void *". The original function does not modify the request, although its local pointer is not declared const.
Keeping the void * signature would also work, with an explicit cast at the Hyper-V call site. I can keep the existing signature if you prefer a smaller change.
Regards,
Naman
Quoted 71 lines
Quoted 71 lines
No functional changes.
Signed-off-by Naman Jain <[email protected]>--- arch/x86/include/asm/tlbflush.h | 1 + arch/x86/mm/tlb.c | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-)arch/x86/include/asm/tlbflush.h +1
-0@@ -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 +14
-4@@ -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
PATCH v2 3/5 x86/hyperv: Use native callbacks for trimming and ASID transitions
In reply to Naman JainSuccessful 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.
--- arch/x86/hyperv/mmu.c | 8 ++++++++ 1 file changed, 8 insertions(+)
arch/x86/hyperv/mmu.c +8 -0
@@ -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
RE: [PATCH v2 3/5] x86/hyperv: Use native callbacks for trimming and ASID transitions
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 2 lines
Successful Hyper-V flush hypercalls do not run the guest callbacks that
How should I interpret the "Successful" qualifier on this statement? It seems a bit spurious since unsuccessful flush hypercalls don't run the callbacks either. But maybe there's something I'm not understanding ....
Michael
Quoted 35 lines
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 throughflush_tlb_func(). Use the native path for these requests too, without filtering the supplied CPU mask.Signed-off-by Naman Jain <[email protected]>--- arch/x86/hyperv/mmu.c | 8 ++++++++ 1 file changed, 8 insertions(+)arch/x86/hyperv/mmu.c +8
-1@@ -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
Re: [PATCH v2 3/5] x86/hyperv: Use native callbacks for trimming and ASID transitions
Quoted 10 lines · Michael Kelley
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 2 lines
Successful Hyper-V flush hypercalls do not run the guest callbacks that
How should I interpret the "Successful" qualifier on this statement? It seems a bit spurious since unsuccessful flush hypercalls don't run the callbacks either. But maybe there's something I'm not understanding ....
Michael
I meant that a failed hypercall leads to native fallback, which can run the callbacks, whereas a successful call returns without doing so. But the hypercall itself never runs those callbacks.
I can use this commit msg the next time.
x86/hyperv: Use native flushes for mask trimming and ASID transitions
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.
Regards,
Naman
PATCH v2 4/5 x86/hyperv: Filter TLB targets before building hypercalls
In reply to Naman JainSuperBench'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.
--- arch/x86/hyperv/mmu.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-)
arch/x86/hyperv/mmu.c +26 -14
@@ -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 ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 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 b708a994017fe50fc481b9fcff6a341c5d843b13
Author: Naman Jain <[email protected]>
Quoted 14 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. The reported bisection identified commit
209954cbc7d0("x86/mm/tlb: Updatemm_cpumasklazily").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.
Quoted 8 lines
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 <[email protected]>
[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?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Re: [PATCH v2 4/5] x86/hyperv: Filter TLB targets before building hypercalls
In reply to Naman JainQuoted 69 lines · 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. The reported bisection identified commit
209954cbc7d0("x86/mm/tlb: Updatemm_cpumasklazily").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 <[email protected]>--- arch/x86/hyperv/mmu.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-)arch/x86/hyperv/mmu.c +4
-6@@ -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; }
arch/x86/hyperv/mmu.c static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · line 96
Quoted 10 lines
arch/x86/hyperv/mmu.c +9
-0+ 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);
Hi Naman,
In VM, for a process that has ran lets say on multiple CPUs (worst case
- on all CPUS), and now running on a single cpu while other cpu run different processes. Until the next trim, if this process calls munmap() or any flush_tlb_range() which has freed_tables set to true, this would copy all stale CPUs in flush_cpus and patch 5 will skip native path, and can cause HV_FLUSH_ALL_PROCESSORS (for all cpu scenario) as in original issue ?
Regards,
Sahil
Quoted 56 lines
arch/x86/hyperv/mmu.c +13
-8+ }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 ;
Re: [PATCH v2 4/5] x86/hyperv: Filter TLB targets before building hypercalls
Quoted 86 lines · Sahil Chandna
Quoted 63 lines · 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. The reported bisection identified commit
209954cbc7d0("x86/mm/tlb: Updatemm_cpumasklazily").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 <[email protected]>---
arch/x86/hyperv/mmu.c| 40 ++++++++++++++++++++++++++--------------1 file changed, 26 insertions(+), 14 deletions(-)arch/x86/hyperv/mmu.c +4
-6@@ -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 structcpumask *cpus, goto do_native;arch/x86/hyperv/mmu.c static void hyperv_flush_tlb_multi(const struct · line 96
Quoted 10 lines
arch/x86/hyperv/mmu.c +9
-0} + 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);Hi Naman,
In VM, for a process that has ran lets say on multiple CPUs (worst case
- on all CPUS), and now running on a single cpu while other cpu run different processes. Until the next trim, if this process callsmunmap()or anyflush_tlb_range()which hasfreed_tablesset to true, this would copy all stale CPUs inflush_cpusand patch 5 will skip native path, and can causeHV_FLUSH_ALL_PROCESSORS(for all cpu scenario) as in original issue ?
Quoted 2 lines
Regards,
Sahil
Hi,
Yes, your understanding is correct. With freed_tables set, we keep the original CPU mask. If it contains all present CPUs, we can still select HV_FLUSH_ALL_PROCESSORS, and patch 5's no-remote-target cleanup will not run.
We keep the full mask because freeing page tables requires including lazy CPUs. The filter we reuse skips lazy CPUs, so we cannot safely apply it unchanged here. The native path also bypasses this filter when freed_tables is set.
This does not mean every stale CPU needs the flush. Safely excluding those CPUs while retaining the required lazy CPUs would need separate logic.
So this series improves ordinary flushes, but the overhead you describe can remain for page-table-freeing flushes until stale entries are removed by native cleanup.
Regards,
Naman
RE: [PATCH v2 4/5] x86/hyperv: Filter TLB targets before building hypercalls
In reply to Naman JainFrom: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 7 lines
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: Updatemm_cpumasklazily").That change leaves a CPU's bit set after it switches to another mm.
To be more explicit,
"... leaves a CPU's bit set in the mm_cpumask even after the CPU switches to another mm".
Quoted 1 line
Native flush callbacks remove these stale bits, but successful Hyper-V
Again, I'm wondering about the "successful" qualifier.
Quoted 1 line
hypercalls do not run those callbacks. A CPU actively running another
The word "actively" seems redundant/superfluous here.
Quoted 2 lines
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
Be more explicit than just "backend".
Quoted 3 lines
the all-processors shortcut before checking individual CPUs.
Use the native predicate to build a per-CPU target mask before selecting
Be more explicit than "native predicate" (which sounds like AI-speak). Just say should_flush_tlb().
Quoted 5 lines
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.
I had a little trouble parsing this sentence. I first read it as "When trimming is requested, this patch does not change.", which didn't make any sense. Maybe reword as "This patch does not affect when trimming is requested."
Quoted 49 lines
Signed-off-by Naman Jain <[email protected]>--- arch/x86/hyperv/mmu.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-)arch/x86/hyperv/mmu.c +4
-6@@ -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);
arch/x86/hyperv/mmu.c static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · line 91
Quoted 7 lines
arch/x86/hyperv/mmu.c +4
-0@@ -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
Again, say should_flush_tlb() instead of "native predicate". And I don't really know what "context-switch ordering" is trying to get at.
Quoted 64 lines
arch/x86/hyperv/mmu.c +18
-9+* 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
Re: [PATCH v2 4/5] x86/hyperv: Filter TLB targets before building hypercalls
Quoted 7 lines · Michael Kelley
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 6 lines
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: Updatemm_cpumasklazily").
Quoted 7 lines
Quoted 1 line
That change leaves a CPU's bit set after it switches to another mm.
To be more explicit,
"... leaves a CPU's bit set in the
mm_cpumaskeven after the CPU switches to another mm".
Acked.
Quoted 4 lines
Quoted 1 line
Native flush callbacks remove these stale bits, but successful Hyper-V
Again, I'm wondering about the "successful" qualifier.
Discussed in patch 3 comments. Please let me know if I can retain this.
Quoted 3 lines
Quoted 1 line
hypercalls do not run those callbacks. A CPU actively running another
The word "actively" seems redundant/superfluous here.
Acked. I'll remove it.
Quoted 5 lines
Quoted 3 lines
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
Be more explicit than just "backend".
Acked.
Quoted 7 lines
Quoted 4 lines
the all-processors shortcut before checking individual CPUs.
Use the native predicate to build a per-CPU target mask before selecting
Be more explicit than "native predicate" (which sounds like AI-speak). Just say
should_flush_tlb().
Acked.
Quoted 11 lines
Quoted 6 lines
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.
I had a little trouble parsing this sentence. I first read it as "When trimming is requested, this patch does not change.", which didn't make any sense. Maybe reword as "This patch does not affect when trimming is requested."
Agreed.
Quoted 7 lines
Quoted 7 lines
Signed-off-by Naman Jain <[email protected]>--- arch/x86/hyperv/mmu.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-)
<snip>
Quoted 17 lines
Quoted 17 lines
arch/x86/hyperv/mmu.c +1
-2-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; }
Quoted 7 lines
arch/x86/hyperv/mmu.c static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · line 91
Quoted 4 lines
arch/x86/hyperv/mmu.c +4
-0+ 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 Again, say
should_flush_tlb()instead of "native predicate". And I don't really know what "context-switch ordering" is trying to get at.
Please allow me to rewrite this whole comment as follows:
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.
Quoted 14 lines
Quoted 4 lines
arch/x86/hyperv/mmu.c +3
-0+* TLB exclusion. Page-table freeing must include lazy CPUs, so keep +* the full candidate mask in that case. +*/ arch/x86/hyperv/mmu.c static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · line 104
Quoted 10 lines
arch/x86/hyperv/mmu.c +10
-0+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); + } + } +
Regards,
Naman
PATCH v2 5/5 x86/hyperv: Request mask cleanup when no remote target remains
In reply to Naman JainFiltering 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.
--- arch/x86/hyperv/mmu.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
arch/x86/hyperv/mmu.c +15 -6
@@ -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 ; - } }/*
Signature · 1 line
2.43.0
RE: [PATCH v2 5/5] x86/hyperv: Request mask cleanup when no remote target remains
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 4 lines
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.
This needs some rewording. Something like:
Filtering avoids flushing on CPUs that don't need it, but the filtering doesn't remove any stale entries from the mm_cpumask. These stale entries can keep later flushes on the multi-CPU path even when only the local CPU needs to be flushed.
Quoted 3 lines
When no remote target remains after filtering, request native trimming of the original candidate mask instead of waiting for the periodic trim.
And maybe substitute mm_cpumask instead of "original candidate mask" (if that's actually correct).
Quoted 3 lines
Copy the const request so only this call requests cleanup.
The native predicate still skips lazy CPUs, so cleanup may not leave a
Again, be explicit instead of "native predicate".
Quoted 13 lines
local-only mask. The new early return also makes the empty basic processor-mask check unnecessary.
Signed-off-by Naman Jain <[email protected]>--- arch/x86/hyperv/mmu.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)arch/x86/hyperv/mmu.c +0
-0
arch/x86/hyperv/mmu.c static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · line 108
Quoted 7 lines
arch/x86/hyperv/mmu.c +4
-0@@ -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.
This comment reads like an assertion of the state of things at this point. But it's only true if the below "if" statement is true. Reword as "If no remote target remains after filtering, request native cleanup ..."
Quoted 30 lines
arch/x86/hyperv/mmu.c +11
-7+*/ +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
Re: [PATCH v2 5/5] x86/hyperv: Request mask cleanup when no remote target remains
Quoted 6 lines · Michael Kelley
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 4 lines
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.
Quoted 7 lines
This needs some rewording. Something like:
Filtering avoids flushing on CPUs that don't need it, but the filtering doesn't remove any stale entries from the
mm_cpumask. These stale entries can keep later flushes on the multi-CPU path even when only the local CPU needs to be flushed.
Acked. Thanks.
Quoted 7 lines
Quoted 3 lines
When no remote target remains after filtering, request native trimming of the original candidate mask instead of waiting for the periodic trim.
And maybe substitute
mm_cpumaskinstead of "original candidate mask" (if that's actually correct).
Acked. Yes, that's correct.
Quoted 6 lines
Quoted 3 lines
Copy the const request so only this call requests cleanup.
The native predicate still skips lazy CPUs, so cleanup may not leave a
Again, be explicit instead of "native predicate".
Acked.
Quoted 16 lines
Quoted 16 lines
local-only mask. The new early return also makes the empty basic processor-mask check unnecessary.
Signed-off-by Naman Jain <[email protected]>--- arch/x86/hyperv/mmu.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)arch/x86/hyperv/mmu.c +0
-0@@ -102,6 +102,21 @@ static void hyperv_flush_tlb_multi(const struct cpumask *cpus, } }
Quoted 8 lines
arch/x86/hyperv/mmu.c static void hyperv_flush_tlb_multi(const struct cpumask *cpus, · line 108
Quoted 4 lines
arch/x86/hyperv/mmu.c +4
-0+/* +* 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. This comment reads like an assertion of the state of things at this point. But it's only true if the below "if" statement is true. Reword as "If no remote target remains after filtering, request native cleanup ..."
Acked. I will change it.
Regards,
Naman
Quoted 31 lines
Quoted 31 lines
arch/x86/hyperv/mmu.c +11
-7+*/ +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
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 4 lines
Lazy
mm_cpumaskmaintenance leaves CPU bits set after switching to another address space, with stale memberships removed by native flush callbacks. Successful Hyper-V TLB flush hypercalls do not run those callbacks, so
I'm not sure what the "Successful" qualifier means here. See comments in later patches of the series.
Quoted 3 lines
historical memberships can persist and cause unnecessary remote flushes. If the mask covers all present CPUs, the backend selects an all-processor flush before reaching its per-CPU lazy-TLB checks.
As noted in subsequent patches, "backend" is very vague.
Quoted 41 lines
This was found while investigating a regression between Linux 6.8 and 7.0 in SuperBench's eight-GPU PyTorch LSTM training on Azure MI300X VMs. Recreating DataLoader workers each epoch caused host-to-GPU copy stalls and timeouts. The reported bisection identified commit
209954cbc7d0("x86/mm/tlb: Updatemm_cpumasklazily"). This is distinct from lazy TLB
mode: a CPU actively running another address space need not be lazy.Patches 1-3 prepare for target filtering in patch 4 while patch 5 adds immediate cleanup.
Patch 1 rejects VP-set conversion failures that return zero, allowing native fallback rather than issuing an empty-target extended hypercall.
Patch 2 makes the native target predicate reusable, retaining its context-switch ordering and adding a clearly named callback wrapper.
Patch 3 uses native callbacks for requested mask trimming and global ASID transitions. The latter need guest callbacks to switch CPUs still using local ASIDs; a Hyper-V invalidation does not do that.
Patch 4 filters targets before selecting either hypercall format or the all-processors shortcut. Page-table-freeing flushes remain unfiltered.
Patch 5 separately requests best-effort cleanup when no remote target remains after filtering. Lazy memberships may survive this cleanup. The cleanup policy remains open for discussion.
The suggested mask-weight >
mm_usersheuristic was tested as a replacement for patch 5's approach on a 32-vCPU Hyper-V VM. With three remote readers,mm_userswas 4 while lazy memberships kept mask weights at 26-32. The heuristic requested trimming for all 10,000 flushes, and each reader received 10,000 remote callbacks. The scoped approach used Hyper-V hypercalls instead. A doubled threshold or rate-limited policy has not been tested.Earlier testing of the monolithic patch reduced the 32-vCPU all-history reproducer from 17.235 us to 1.622 us per iteration (two
mprotect()calls and a write). The reporter's application hit the 600-second timeout on the unpatched 96-vCPU VM and completed in 424 and 436 seconds with the patch.
Quoted 14 lines
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 the ASID fallback separately from the reported regression (Dave).
* Keep related predicate/wrapper names and comment the definition rather than the header declaration (Dave).
* Explain target filtering, page-table freeing, and the no-remote-target cleanup condition. Document the const copy and lazy-membership limit (Dave).
* Test themm_usersheuristic and retain scoped cleanup as a separate policy discussion (Dave).
* Omit Fixes tags and stable requests (Dave).
* Rebase onto next-20261116.
I've provided comments on the remaining patches in the series, mostly around the AI-speak in the commit messages and code comments. I still intend to look more closely at how/when to initiate trimming, and may follow with additional comments on that topic.
Michael
Quoted 17 lines
Naman Jain (5):
x86/hyperv: Reject unrepresentable TLB flush VP sets x86/tlb: Make the conditional flush predicate reusable x86/hyperv: Use native callbacks for trimming and ASID transitions x86/hyperv: Filter TLB targets before building hypercalls x86/hyperv: Request mask cleanup when no remote target remainsarch/x86/hyperv/mmu.c | 71 +++++++++++++++++++++++---------- arch/x86/include/asm/tlbflush.h | 1 + arch/x86/mm/tlb.c | 18 +++++++-- 3 files changed, 65 insertions(+), 25 deletions(-)base-commit:
749a4de370409464cf8501ec5b2451df45c84eb4--
2.43.0
Quoted 6 lines · Michael Kelley
From: Naman Jain <[email protected]> Sent: Tuesday, November 17, 2026 1:15 AM
Quoted 4 lines
Lazy
mm_cpumaskmaintenance leaves CPU bits set after switching to another address space, with stale memberships removed by native flush callbacks. Successful Hyper-V TLB flush hypercalls do not run those callbacks, so
Quoted 8 lines
I'm not sure what the "Successful" qualifier means here. See comments in later patches of the series.
Quoted 3 lines
historical memberships can persist and cause unnecessary remote flushes. If the mask covers all present CPUs, the backend selects an all-processor flush before reaching its per-CPU lazy-TLB checks.
As noted in subsequent patches, "backend" is very vague.
Acked.
Quoted 57 lines
Quoted 42 lines
This was found while investigating a regression between Linux 6.8 and 7.0 in SuperBench's eight-GPU PyTorch LSTM training on Azure MI300X VMs. Recreating DataLoader workers each epoch caused host-to-GPU copy stalls and timeouts. The reported bisection identified commit
209954cbc7d0("x86/mm/tlb: Updatemm_cpumasklazily"). This is distinct from lazy TLB
mode: a CPU actively running another address space need not be lazy.Patches 1-3 prepare for target filtering in patch 4 while patch 5 adds immediate cleanup.
Patch 1 rejects VP-set conversion failures that return zero, allowing native fallback rather than issuing an empty-target extended hypercall.
Patch 2 makes the native target predicate reusable, retaining its context-switch ordering and adding a clearly named callback wrapper.
Patch 3 uses native callbacks for requested mask trimming and global ASID transitions. The latter need guest callbacks to switch CPUs still using local ASIDs; a Hyper-V invalidation does not do that.
Patch 4 filters targets before selecting either hypercall format or the all-processors shortcut. Page-table-freeing flushes remain unfiltered.
Patch 5 separately requests best-effort cleanup when no remote target remains after filtering. Lazy memberships may survive this cleanup. The cleanup policy remains open for discussion.
The suggested mask-weight >
mm_usersheuristic was tested as a replacement for patch 5's approach on a 32-vCPU Hyper-V VM. With three remote readers,mm_userswas 4 while lazy memberships kept mask weights at 26-32. The heuristic requested trimming for all 10,000 flushes, and each reader received 10,000 remote callbacks. The scoped approach used Hyper-V hypercalls instead. A doubled threshold or rate-limited policy has not been tested.Earlier testing of the monolithic patch reduced the 32-vCPU all-history reproducer from 17.235 us to 1.622 us per iteration (two
mprotect()calls and a write). The reporter's application hit the 600-second timeout on the unpatched 96-vCPU VM and completed in 424 and 436 seconds with the patch.Quoted 14 lines
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 the ASID fallback separately from the reported regression (Dave).
* Keep related predicate/wrapper names and comment the definition rather than the header declaration (Dave).
* Explain target filtering, page-table freeing, and the no-remote-target cleanup condition. Document the const copy and lazy-membership limit (Dave).
* Test themm_usersheuristic and retain scoped cleanup as a separate policy discussion (Dave).
* Omit Fixes tags and stable requests (Dave).
* Rebase onto next-20261116.
Quoted 4 lines
I've provided comments on the remaining patches in the series, mostly around the AI-speak in the commit messages and code comments. I still intend to look more closely at how/when to initiate trimming, and may follow with additional comments on that topic.
Acked. Thanks for reviewing. I'll wait a couple more days before sending the next version with the changes.
Regards,
Naman
Quoted 20 lines
Michael
Quoted 17 lines
Naman Jain (5):
x86/hyperv: Reject unrepresentable TLB flush VP sets x86/tlb: Make the conditional flush predicate reusable x86/hyperv: Use native callbacks for trimming and ASID transitions x86/hyperv: Filter TLB targets before building hypercalls x86/hyperv: Request mask cleanup when no remote target remainsarch/x86/hyperv/mmu.c | 71 +++++++++++++++++++++++---------- arch/x86/include/asm/tlbflush.h | 1 + arch/x86/mm/tlb.c | 18 +++++++-- 3 files changed, 65 insertions(+), 25 deletions(-)base-commit:
749a4de370409464cf8501ec5b2451df45c84eb4--
2.43.0