From patchwork Tue Feb 11 21:08:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13970707 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E9855C0219B for ; Tue, 11 Feb 2025 21:15:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 834A1280009; Tue, 11 Feb 2025 16:15:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E4D9280004; Tue, 11 Feb 2025 16:15:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6AC0E280009; Tue, 11 Feb 2025 16:15:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4C8AB280004 for ; Tue, 11 Feb 2025 16:15:19 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D8A30120BC3 for ; Tue, 11 Feb 2025 21:15:18 +0000 (UTC) X-FDA: 83108919516.06.A58EAB3 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf10.hostedemail.com (Postfix) with ESMTP id 31690C000F for ; Tue, 11 Feb 2025 21:15:16 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf10.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739308517; a=rsa-sha256; cv=none; b=Xv/+ve0NCaXECzLyTbvR9M9p4rW1k9yjioxwyMpE21p4D+9nE1yxAsUqya0H+NPoAp6ayL uyHVH97OApK69M6RZm/XfOmWNShniymOv3h6GCO7Qqv6UIF0v/qPQIgtgVtlPsNgnPM6vI 8mrnzVrWk+elbfuzndIP1BNtA9fd8f0= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf10.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739308517; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5Wzp6yp4uysyFCtEvGYAZaMq9MCEXT9r2+6OpC3W/Yw=; b=zc7L+5OjVnEZs1mcVUEThXHNYVTYqNhhotdS5zo1veg9/11mW53gQnJysW7F6IKx3ncfit ERp5mck8Jz8qGy6aFURirQTdvJJqlhKQrpv9PMjIrf4lpxNvjhMDP418qSf1TQGk/nZ33q kMZrpEj1dVNtoH5bVZvrohRo9nZX7xU= Received: from fangorn.home.surriel.com ([10.0.13.7]) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1thxUX-000000008HU-3MEu; Tue, 11 Feb 2025 16:08:25 -0500 From: Rik van Riel To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, bp@alien8.de, peterz@infradead.org, dave.hansen@linux.intel.com, zhengqi.arch@bytedance.com, nadav.amit@gmail.com, thomas.lendacky@amd.com, kernel-team@meta.com, linux-mm@kvack.org, akpm@linux-foundation.org, jackmanb@google.com, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel , Manali Shukla Subject: [PATCH v10 10/12] x86/mm: do targeted broadcast flushing from tlbbatch code Date: Tue, 11 Feb 2025 16:08:05 -0500 Message-ID: <20250211210823.242681-11-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250211210823.242681-1-riel@surriel.com> References: <20250211210823.242681-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 31690C000F X-Rspamd-Server: rspam12 X-Stat-Signature: 7534u8auyu9t6zs75uck9txjg8w1ocay X-HE-Tag: 1739308516-287741 X-HE-Meta: U2FsdGVkX18R6XI53Zr79TCtz7apxIlt/7suUg9qCEMFK4pGi1lkg9FRH/w3FVNPBvHkyNKaugTKmjjrI4lMNda55w4G6JocxD9p0GHq0Fcf8kCG+jjXvupyIZ5y2+Lz9nqq8ldHLJB48hdyPKKF0487kSpaVylbymLc+ZBg9svMOQ2whoNWidd9ihGbl94Lqr1e/vp6C2Tg3ThVHZETMHa1cNwCQw2HoQ1jpEJ9KTlBhyG47aqVnWiCBpOrSaE70taBx0EFSrYC8QmJkHw/hiSihgPTz1znViNSAxr3uX9sV/jTIecfVDgdp/0FyPeVh1A57jBAuyrjxSlBXrKrjxJx5Cf1v3W1yynyVzUAdZ1gSCPSDiy00mCvXtKUZtDVovs0NTyJnAy3yPhe2Sjn6AiKL8eMlsL/1LzzzfnI3DUaXo5EP8o5rKv5OLAHaNDR6QfaFEIg53awCM1dzWeeUcv+e/y8Ii+WB7WGP0eJDki1+wwuqzUH0cSawtMgo6Q34Y812mFJWvWei6oCw9uyn4UBgFlqhZrYTTQO96vSTpRglcha/UxQNI4TujaFL4uX7EbrVw/g/HZ2vZbgxA0NtlPfWNXqBcOwpkBs4Xe3ckWRPGa+15d16lTmS3zHPp94bYZcYS6DqpVwknOF78l1PQrQe7Gm7tFex667JPVUOpHsvjVRIwwPcnUzZJ42Qf+xmGAF+bO3Ecxal9tUMBfXWlcl1Tb9H6SBtq4J7XRN7G83feLjxTHheVKVW4ZS2v/QJJHahjWqSysTFKaClMboAqrfo+pEjnJhtt2i8H39Sw+X34zXULLi0PACIiOurpUjLl0hLii1mqlnjbfMpIMIRdWezsftIdfFfmZwJet2MxJCO9HqvgMTqbpnJLVkjT3iClG+w6pE8za4P+dKZ5fN7lbHrbPhlU1yebPfRiDHfPl0XMyBh1REQdMgp3Tz4sT9RvcihcT7QYTT5a46Eeh JL/Uak4J aZETIdH2Kwy2dQxNoyaUk/4QGfIvHsFUnG38fyn+oir14vrifKgHvwrad85GvrJFipR5/EgnApInq66ojb0ES3ziQsjOfdmmH8y04oVMj2/CUFtiCcS6lI/v8i5quoKzsJZq1A71SyeKhUJHgRw6yW+jdifR8suYC4CKfhpAZdCVbeLd7Dn6kxECdI7JBvaDrEPAUvW3VJmRvBt0vGa2mtrLy1qe3m1Ey/eSm7LQCM38JhOD0oWzkrwN7onc7aStSqiINoevNML+H38YtI65eGKrjgAP1OJ7hkJFf9YakoAlpiHuW1HivGd9DUz27IDpX0Gh7VArMGrDgKJyWQKGn1j/w+9iKSWzZi4VKqG5Bw+ksEORmTfsmwajwyix9GyITtbVybnfrSyGxQCaw/zUaZbBZ80Bc/+2Dz7Z6ZqQAsmPhGZlka/+grrnzI2Jq6VCxWTjq1s1qH6e8dx+y8ZwrPmYNS7y+9dejXukK63gBz9W43vgdO3d7o0/ly4BjIzKlp5eSpEyp/kTFfBOJ/5CcEdfdhg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Instead of doing a system-wide TLB flush from arch_tlbbatch_flush, queue up asynchronous, targeted flushes from arch_tlbbatch_add_pending. This also allows us to avoid adding the CPUs of processes using broadcast flushing to the batch->cpumask, and will hopefully further reduce TLB flushing from the reclaim and compaction paths. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/include/asm/invlpgb.h | 20 ++++----- arch/x86/include/asm/tlbflush.h | 19 ++++---- arch/x86/mm/tlb.c | 79 +++++++++++++++++++++++++++++++-- 3 files changed, 96 insertions(+), 22 deletions(-) diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h index a1d5dedd5217..43c331507cc0 100644 --- a/arch/x86/include/asm/invlpgb.h +++ b/arch/x86/include/asm/invlpgb.h @@ -31,7 +31,7 @@ static inline void __invlpgb(unsigned long asid, unsigned long pcid, } /* Wait for INVLPGB originated by this CPU to complete. */ -static inline void tlbsync(void) +static inline void __tlbsync(void) { cant_migrate(); /* TLBSYNC: supported in binutils >= 0.36. */ @@ -61,19 +61,19 @@ static inline void invlpgb_flush_user(unsigned long pcid, unsigned long addr) { __invlpgb(0, pcid, addr, 0, 0, INVLPGB_PCID | INVLPGB_VA); - tlbsync(); + __tlbsync(); } -static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, - unsigned long addr, - u16 nr, - bool pmd_stride) +static inline void __invlpgb_flush_user_nr_nosync(unsigned long pcid, + unsigned long addr, + u16 nr, + bool pmd_stride) { __invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA); } /* Flush all mappings for a given PCID, not including globals. */ -static inline void invlpgb_flush_single_pcid_nosync(unsigned long pcid) +static inline void __invlpgb_flush_single_pcid_nosync(unsigned long pcid) { __invlpgb(0, pcid, 0, 0, 0, INVLPGB_PCID); } @@ -82,11 +82,11 @@ static inline void invlpgb_flush_single_pcid_nosync(unsigned long pcid) static inline void invlpgb_flush_all(void) { __invlpgb(0, 0, 0, 0, 0, INVLPGB_INCLUDE_GLOBAL); - tlbsync(); + __tlbsync(); } /* Flush addr, including globals, for all PCIDs. */ -static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) +static inline void __invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) { __invlpgb(0, 0, addr, nr - 1, 0, INVLPGB_INCLUDE_GLOBAL); } @@ -95,7 +95,7 @@ static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) static inline void invlpgb_flush_all_nonglobals(void) { __invlpgb(0, 0, 0, 0, 0, 0); - tlbsync(); + __tlbsync(); } #endif /* _ASM_X86_INVLPGB */ diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 234277a5ef89..89dddbcd1322 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -106,6 +106,9 @@ struct tlb_state { * need to be invalidated. */ bool invalidate_other; +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH + bool need_tlbsync; +#endif #ifdef CONFIG_ADDRESS_MASKING /* @@ -310,6 +313,10 @@ static inline void broadcast_tlb_flush(struct flush_tlb_info *info) static inline void consider_global_asid(struct mm_struct *mm) { } + +static inline void tlbsync(void) +{ +} #endif #ifdef CONFIG_PARAVIRT @@ -359,21 +366,15 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm) return atomic64_inc_return(&mm->context.tlb_gen); } -static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, - struct mm_struct *mm, - unsigned long uaddr) -{ - inc_mm_tlb_gen(mm); - cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm)); - mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); -} - static inline void arch_flush_tlb_batched_pending(struct mm_struct *mm) { flush_tlb_mm(mm); } extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch); +extern void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, + struct mm_struct *mm, + unsigned long uaddr); static inline bool pte_flags_need_flush(unsigned long oldflags, unsigned long newflags, diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index f3c0d8fafc57..41ac2d121d76 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -492,6 +492,37 @@ static void finish_asid_transition(struct flush_tlb_info *info) WRITE_ONCE(mm->context.asid_transition, false); } +static inline void tlbsync(void) +{ + if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) + return; + __tlbsync(); + this_cpu_write(cpu_tlbstate.need_tlbsync, false); +} + +static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, + unsigned long addr, + u16 nr, bool pmd_stride) +{ + __invlpgb_flush_user_nr_nosync(pcid, addr, nr, pmd_stride); + if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) + this_cpu_write(cpu_tlbstate.need_tlbsync, true); +} + +static inline void invlpgb_flush_single_pcid_nosync(unsigned long pcid) +{ + __invlpgb_flush_single_pcid_nosync(pcid); + if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) + this_cpu_write(cpu_tlbstate.need_tlbsync, true); +} + +static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) +{ + __invlpgb_flush_addr_nosync(addr, nr); + if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) + this_cpu_write(cpu_tlbstate.need_tlbsync, true); +} + static void broadcast_tlb_flush(struct flush_tlb_info *info) { bool pmd = info->stride_shift == PMD_SHIFT; @@ -798,6 +829,8 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, if (IS_ENABLED(CONFIG_PROVE_LOCKING)) WARN_ON_ONCE(!irqs_disabled()); + tlbsync(); + /* * Verify that CR3 is what we think it is. This will catch * hypothetical buggy code that directly switches to swapper_pg_dir @@ -977,6 +1010,8 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, */ void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) { + tlbsync(); + if (this_cpu_read(cpu_tlbstate.loaded_mm) == &init_mm) return; @@ -1640,9 +1675,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) * a local TLB flush is needed. Optimize this use-case by calling * flush_tlb_func_local() directly in this case. */ - if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) { - invlpgb_flush_all_nonglobals(); - } else if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) { + if (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) { flush_tlb_multi(&batch->cpumask, info); } else if (cpumask_test_cpu(cpu, &batch->cpumask)) { lockdep_assert_irqs_enabled(); @@ -1651,12 +1684,52 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) local_irq_enable(); } + /* + * If we issued (asynchronous) INVLPGB flushes, wait for them here. + * The cpumask above contains only CPUs that were running tasks + * not using broadcast TLB flushing. + */ + tlbsync(); + cpumask_clear(&batch->cpumask); put_flush_tlb_info(); put_cpu(); } +void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, + struct mm_struct *mm, + unsigned long uaddr) +{ + u16 asid = mm_global_asid(mm); + + if (asid) { + invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false); + /* Do any CPUs supporting INVLPGB need PTI? */ + if (static_cpu_has(X86_FEATURE_PTI)) + invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false); + + /* + * Some CPUs might still be using a local ASID for this + * process, and require IPIs, while others are using the + * global ASID. + * + * In this corner case we need to do both the broadcast + * TLB invalidation, and send IPIs. The IPIs will help + * stragglers transition to the broadcast ASID. + */ + if (in_asid_transition(mm)) + asid = 0; + } + + if (!asid) { + inc_mm_tlb_gen(mm); + cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm)); + } + + mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); +} + /* * Blindly accessing user memory from NMI context can be dangerous * if we're in the middle of switching the current user task or