From patchwork Mon Dec 23 02:55:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13918427 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 BB0E8E77188 for ; Mon, 23 Dec 2024 03:06:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 49E756B008A; Sun, 22 Dec 2024 22:06:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 44F576B0093; Sun, 22 Dec 2024 22:06:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 316596B0095; Sun, 22 Dec 2024 22:06:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 1549C6B008A for ; Sun, 22 Dec 2024 22:06:40 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C78D4438F9 for ; Mon, 23 Dec 2024 03:06:39 +0000 (UTC) X-FDA: 82924734606.27.EC7144C Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf19.hostedemail.com (Postfix) with ESMTP id 3E8E81A0008 for ; Mon, 23 Dec 2024 03:05:59 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734923159; 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=xffulrALW7b77+K4s7iaL5hn9lV4yu3SHiwRwdYz3+g=; b=0jzIZrVUBVL8ePGDDhwh0G8ZHqGkgoUlPYXPea8G61SZzase6PpjuXWakAh5ZoialRoP91 Hbukhchi8Ro/RBnSlm3tYaYHH0euAlfYLfl1sNgYm8aVZVsgyuy8/OmrNX41nrnklvb68K StRNUPhzodzVtasLFTEpuhBF8WMgOw0= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; spf=pass (imf19.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734923159; a=rsa-sha256; cv=none; b=wilUIFu+Fd+JP+fv9w99hFbL3NCUMJTdR5/tCWdvAm3T1nQJJT8bi4gmIEUkKo3NvrychV EB+YNHz5pes9sudicOgy/QR4V+bPgJIiMZncojZwhY83Lcg5HpIGPqFUVLgejLzxzZhi41 n00aITSpFMEqzin2n+MiJ794tnb2G8c= 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 1tPYdq-000000001Ih-00QW; Sun, 22 Dec 2024 21:57:58 -0500 From: Rik van Riel To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@meta.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, hpa@zytor.com, akpm@linux-foundation.org, linux-mm@kvack.org, Rik van Riel Subject: [PATCH 09/11] x86,tlb: do targeted broadcast flushing from tlbbatch code Date: Sun, 22 Dec 2024 21:55:15 -0500 Message-ID: <20241223025751.3268975-10-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20241223025751.3268975-1-riel@surriel.com> References: <20241223025751.3268975-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 3E8E81A0008 X-Rspam-User: X-Stat-Signature: sywu95nsg1bo4dpafrx1ntfzmomtckr1 X-HE-Tag: 1734923159-857383 X-HE-Meta: U2FsdGVkX1/ZFpgM2A4aPnzIlV2CQlUjIK3r8/HgQEhFAwE4LPbxVFwAzVOEv86+Ii2YlYNoy8gEZf67f5ztiiiHdNLYP9xgYIi8iUWFvhrl5bBvGbgGJc/GOCCt7T9d4vWAkMinfxf3ormUxxld2OTatsJNF9uQwo27cPDZ1pXdAP9/zB/y3UPqdM9+xHGSfeznAeDJUaSIIkJ1vMG74WSgG/F8BI61uELj2gmh8J6hepv4tRzDa3WTLzvxYm8sCYm5cXyXF6+DiMhRm+LSjAdGX2UTg0NMz4BdXTBmgjVOCKDh52dr6niIOEudQEPry3aygY/Flbw9yH30Fry08AoAXCIyZZdEGR2r1MYkKuBYDITiIqoWrsIj2YcDIX7d0CJFql0CgAxPau9xbjCmSs8Zrr46sXsfsGqqVMFYl1cW4dcsf6QHc8oq846z4aCnaQJ++Q8tWsTBV+u2OJStXJJ9SBAUtwZIYKVTnCe/jJ1dxV44aAo9a/HvYt+5dzs7YvhjY1h5zNwoZswV+VRPgt9xhOPGIl4yzi215EDnVk5Of+aFLXCHy8BY9X1XqsUR5rmfpYnWlzuD2c+SAOAU4li0roR9Ir96X+ExPUdVyVWhnn8QcIgp4VJkMZNU0OseFl36P8+gD5LRwuXYdx5OfqD6KS/g52H2tRzVDIRqhsJlds+DCBtgyjnDqOeULuIWDNkiwlMfdKAEFjO+KaNXBu3m19dkzIdt3qtkFdlT93gzYuajD5NSyTXQ8/mZ/IMTOxz3ynQ+Yh1cOdDjw9uhoXYBeAMsyr7lGsQ1jPKB6enMzmIOn3mczoViyxuMkcGC2savdYjXZawU2DpQKkQWBaVIa4j9E6K19ssUNUFvDfLM+K3I6sDtABr+OHgqIxoBtsoFDg15H3GbKrDhc/L/dvLIT2etcSX7J1oqM76bKhgPA3vsSj6S1RGA6N6NlhWVPbLBFSjJINg/NTGalOP IqYZxl7z l9QMKSc789s8tsvmWsqAf3mfUkNoTpvOImduQ9Xm768S0fm/MMgimUXRKsyoyXKu2eSZvVUQHxX8++caxbDN6xssMkL9mVh6Wa/QD9QYw29wCEYlNDC6T5kLheucWhPko8SDKE9BVmNdN2pAIOKVlF9gpbQzJuhmRjLvGsqHpJR55eieNdkef0Y2BYNbFlfASY70Yxq8bwkQvWRN+CbSncBC4O8bO2o2k82XdVJ/nwIlN92IJBxmp88M0DCDfPZqjQRcOH3rYYFl76kyTPUCz2AMZnAosadPCT7eJqts3qGz7TXQwniGkrYHsPJjfJ8QYDaHY 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 --- arch/x86/include/asm/tlbbatch.h | 1 + arch/x86/include/asm/tlbflush.h | 12 +++------ arch/x86/mm/tlb.c | 48 ++++++++++++++++++++++++++------- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/arch/x86/include/asm/tlbbatch.h b/arch/x86/include/asm/tlbbatch.h index 1ad56eb3e8a8..f9a17edf63ad 100644 --- a/arch/x86/include/asm/tlbbatch.h +++ b/arch/x86/include/asm/tlbbatch.h @@ -10,6 +10,7 @@ struct arch_tlbflush_unmap_batch { * the PFNs being flushed.. */ struct cpumask cpumask; + bool used_invlpgb; }; #endif /* _ARCH_X86_TLBBATCH_H */ diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 074f46b74b92..71d094841356 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -295,21 +295,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 29a64f8c4c94..c5459516a72e 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1605,16 +1605,7 @@ EXPORT_SYMBOL_GPL(__flush_tlb_all); void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) { struct flush_tlb_info *info; - int cpu; - - if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) { - guard(preempt)(); - invlpgb_flush_all_nonglobals(); - tlbsync(); - return; - } - - cpu = get_cpu(); + int cpu = get_cpu(); info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, 0, false, TLB_GENERATION_INVALID); @@ -1632,12 +1623,49 @@ 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. + */ + if (cpu_feature_enabled(X86_FEATURE_INVLPGB) && batch->used_invlpgb) { + tlbsync(); + migrate_enable(); + batch->used_invlpgb = false; + } + 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) +{ + if (static_cpu_has(X86_FEATURE_INVLPGB) && mm->context.broadcast_asid) { + u16 asid = mm->context.broadcast_asid; + /* + * Queue up an asynchronous invalidation. The corresponding + * TLBSYNC is done in arch_tlbbatch_flush(), and must be done + * on the same CPU. + */ + if (!batch->used_invlpgb) { + batch->used_invlpgb = true; + migrate_disable(); + } + invlpgb_flush_user_nr(kern_pcid(asid), uaddr, 1, 0); + /* Do any CPUs supporting INVLPGB need PTI? */ + if (static_cpu_has(X86_FEATURE_PTI)) + invlpgb_flush_user_nr(user_pcid(asid), uaddr, 1, 0); + } else { + 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