From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=13.77.154.182 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796099835; b=an8vQNowEKr7j6Vw1hYLQegWwKzVrfi4L4iLf/NM+/g/1En1o8mxyhJu+0TV2FPnd0Ji UNj68TC+X7eH969srPWtNVyXNMYGj3Rm5tM71rk0Y+iX7Hedbsk0PtbCip8TqGjYNyioo 8bmuI2Z9pCCrk/7fsBvnMsy6SUI5R09wTATBEVb5zDneaT1z6OPz4K8L7s8F7nVZDvF6P oHq7Fu1532quIl0cnhQ0GjQB8dBAvXbGcYLXTEItNJVeEfivLeuZrs51nw9WHf9elGP/V mdyMqQrb+A+/BNNywMPQg8MILUsWTTcr/pSjqOUABXh91XRKDnG8F1b/Tu15izKSD8w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796099835; h=DMARC-Filter:Received:DKIM-Filter:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Spam-Status:X-Spam-Checker-Version; bh=ThYSxjfbeCTC7N3aDyvUv33kZWGwL9mTrepsEF50tR4=; b=pZD4y5+7yLjN9ZjgCnwco53x3fyl1E8cewgIC3L15e3+j+1TsV4rTnNGIDBEypa/0bKt dUUsF00Rnk/4bve4ijnA2KeOyUHXnOBQQq+EvkEC/t7gA4g7mVscvjzTYdehCOcLozWUc fOHvlMn3n5OS71JJc5ruUSBAOcvvw9vf+ixje4O9T4Q8BNKp6sMGn4FVoNjPXa3LYOLwj eaDHYYpeVLVYuhSKK9/5D7BMhSCU4xjDeUgElXYvCUOKmn+p+/HvFVNmSrxpLltGfC41x B64Qw8fchpFKd7VTOe53LgNoO5X2h2XJ9ylyLy7AX2nCkKDWCs6WjvyLflXvPzc/D5g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.microsoft.com; spf=pass smtp.mailfrom=linux.microsoft.com; dkim=pass header.d=linux.microsoft.com header.i=@linux.microsoft.com header.a=rsa-sha256 header.s=default header.b=brf+PPCy; arc=none smtp.remote-ip=13.77.154.182 Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8ABEB606A327; Tue, 1 Dec 2026 04:37:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8ABEB606A327 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.microsoft.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.microsoft.com header.i=@linux.microsoft.com header.a=rsa-sha256 header.s=default header.b=brf+PPCy Received: from CPC-namja-026ON.localdomain (unknown [4.213.232.23]) by linux.microsoft.com (Postfix) with ESMTPSA id 502D320B7167; Mon, 30 Nov 2026 20:37:02 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 502D320B7167 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1796099827; bh=ThYSxjfbeCTC7N3aDyvUv33kZWGwL9mTrepsEF50tR4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=brf+PPCyASCupLZr0uEq3q6eWnnDGJ0TK0UrSEI7XGwnEhruFD1YNWxQ/3fWwuCq0 dtAyELKE8nO5jRGgGZibXNKpWVPiSGZtY4ulIBAp9P9fO399Hy1wOp8+jPYo0y7+Zt pGE70Ii9hPY5puZEpGyk/96hDrlsjRFnuITIWumA= From: Naman Jain To: haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org Cc: dave.hansen@intel.com, hpa@zytor.com, riel@surriel.com, mhklinux@outlook.com, schandna@linux.microsoft.com, namjain@linux.microsoft.com, qq570070308@gmail.com, houwenlong.hwl@antgroup.com, zhouchuyi@bytedance.com, jgross@suse.com, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 4/5] x86/hyperv: Filter TLB targets before building hypercalls Date: Tue, 1 Dec 2026 04:36:41 +0000 Message-ID: <20261201043642.1054258-5-namjain@linux.microsoft.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261201043642.1054258-1-namjain@linux.microsoft.com> References: <20261201043642.1054258-1-namjain@linux.microsoft.com> X-Mailing-List: linux-hyperv@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 +#include #include #include @@ -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