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

From: Naman Jain

Date: Mon Nov 23 2026 - 05:17:10 EST




On 11/23/2026 3:27 PM, Sahil Chandna wrote:


On 17/11/2026 14:45, Naman Jain wrote:
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@xxxxxxxxxxxxxxxxxxx>
---
  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);
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


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