From patchwork Tue Feb 11 21:07:56 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: 13970698 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 81683C0219B for ; Tue, 11 Feb 2025 21:09:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 577B7280002; Tue, 11 Feb 2025 16:09:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D80C6B0098; Tue, 11 Feb 2025 16:09:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 109EF280001; Tue, 11 Feb 2025 16:09:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id BA0376B0093 for ; Tue, 11 Feb 2025 16:09:07 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6036E4A646 for ; Tue, 11 Feb 2025 21:09:07 +0000 (UTC) X-FDA: 83108903934.19.4A63AA7 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf01.hostedemail.com (Postfix) with ESMTP id C36534000D for ; Tue, 11 Feb 2025 21:09:05 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf01.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=1739308145; a=rsa-sha256; cv=none; b=hg31YL8Gv9Nq/4CUz/8F7h7ayAc48ogfR/Net9m/KRd9JK2dmDeJwHz7/luvz0Ngf7tFGy 9VHsxg6Q9Y7XNGOOWRaDBnChcFRVfAUK0WVzO9HJmIAJhfck1sE68FV9YWnU4Q67eVBgl6 F8yfLHufjsk6RaihR3CwBQHPCYCQKMA= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf01.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=1739308145; 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=P5knolMVwP0R2zovaoahPRToZT6slJoi959foZfSsM0=; b=K9/AaKhZxS4pgaoxqNkmSJB3P7yLfjMsCnawbpSLIvorWCgXklzenCMELDtlAq8PF+qz+S IEpiWLwboF0Jc6JoI2LauWjJyKjqlXhZzNZLD2qZp/IimVQZdvMkVNcxa5hwf2zRcPsGGV HV+vt3gGaneRyO4dXcNtmnjaYMTOvSU= 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-2WcT; 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 01/12] x86/mm: make MMU_GATHER_RCU_TABLE_FREE unconditional Date: Tue, 11 Feb 2025 16:07:56 -0500 Message-ID: <20250211210823.242681-2-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-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: C36534000D X-Stat-Signature: je1xciswe5jr9da4bk77df9cxm5oztf7 X-Rspam-User: X-HE-Tag: 1739308145-653243 X-HE-Meta: U2FsdGVkX1+VrINe16k1AwPyMhW7KI8MBQMEN3e9J/Kp9RDchbA1x8XTVKK7m+17+lC3o75jk5/1yHMzUZoyag9RD+1Ug8HtCcTxsVPY5XK10jPnA2P4UHbjQnJFvF1YBvI31U0P6mpcfqei0Xnu1FpL4TY+3NsFHDkWnxC2abHTyTZTZgQ7O6v2pw80tRa8+dbTneDY9fYqJzcZE57laIwQFnQAwWUgOVBdxTazOnEhYoHePZ0OJ3sRmlqeoUMm8WnctRGKr1Xz1pLKLrivrsVPHMRerUxIe1loS8uZzCoCR3XBeHh0bM/+OtrspQ/qfKSpsySJbE37bvVzgsWCbYStfk3FGUf9zA7NILkHmuT7q7nSJfFM3e3Fjqn/NPyfzWXKv5l+f9bOpMLFoZlqWDfTsgqQ8urGd6XWVlKNzIbAI+0Gd9VigLL37z273g4dYQBqOL/ItBgKxUmI2RHi/T2cD0ftzpVnAR5q9K0tvwWYkTiH0I4YmziDs+xtIhojuTaoznlTenN/oxo5D8q2C0HPw/7uW7/EZ/DWfFNyG+C2juNi+Ro5yDyy8M6nhxrHneKKTP6bk3ocDI7aRm8MjRpTqKs2aXodvGBhh6sZuzACihsgmQb8oSED68sVPGcp/eDb6r9NiCG7IAIz2AK6lwWioLFTinv3lcU4wYGyyv0Gmky0aNaYoxL/zsrpqCI+MF5VvxgZyweVfkg6Fp6l0vPUYWG7NBZBlHyONN1xHavZ2eRY0mI3OUZvazsBV5ELNuSWFJ+BmHBjo6WFbc29FIVHrRPWtD51dcTlUvk45dAXfTu4n5sUHpNo1bAFOo1oNMFsQZo/jN4wM4BRCYmyYo4U4je3E2V63ePeoGUg12pCxXUvt97JAxB11M1jKm0vF+qT8/PaoQHa2/k2pctcC9YkLtI6W8re3OFUPFTm9SYqS5smxrZJUxLIeAW3/HTqwGFypufA5Jy0olsmjjI KIaMHeSe 2AEXZcPyufNLA2lXdvd5l+deO1pv992TYwBT1s8kYsvNLApEgGjpOqrYbu6XqY06sg0eUx3b0g2Hi0PO9cP3eQ8BGklXoQ3nnB7dHO8knaOe4CND4BxaOo/xMgmZsDozoU2pWCtBgu20dO/9IspEodTxHQun/Pjlfko+hYtogNx/GOF+noGn9R8wqI/pS4t+fe1191yNrVVCMCN9GSwcFD6fbU2oI54M6GccXMxUUXz48W25RXIKcpIdnkEJZfvPkGKVRXhs1WoVO05XDlQl79gRyXBSGZHn/34K2vYkU0FitElY+CUwHJ7Mo+K6sC885ssjNJQmxw0LbUkrwUyPHt1EL1kSleDNbG472C6OTvXEaI973nT53XdEno+l9RO+FPPqPyUYrrutiIOmncGpAIoVuvzCBrUJz2OI+dzJONgOUxMghhsdx2eSTgxykVScX89A67zYd7bgubxXCEJhaEC9Tig2we8dM9kh/GS37n18U1EzjNBvSRLRqd1jQQmhWl1iwAKAbXjn69z0Oqts6SZwT664ArlTHTUeGYOD0chlublfqW2MqqoQjnCVUJKN9EXJ/9BU/a8+9oVyesmbw/6DkDQ4MfcV52fksS+jb10H/Djy/Wh+9/A71AnV9YIiUf9hXtxxO1Pj7kyAhH2DpaC1nIQ== 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: Currently x86 uses CONFIG_MMU_GATHER_TABLE_FREE when using paravirt, and not when running on bare metal. There is no real good reason to do things differently for each setup. Make them all the same. Currently get_user_pages_fast synchronizes against page table freeing in two different ways: - on bare metal, by blocking IRQs, which block TLB flush IPIs - on paravirt, with MMU_GATHER_RCU_TABLE_FREE This is done because some paravirt TLB flush implementations handle the TLB flush in the hypervisor, and will do the flush even when the target CPU has interrupts disabled. Always handle page table freeing with MMU_GATHER_RCU_TABLE_FREE. Using RCU synchronization between page table freeing and get_user_pages_fast() allows bare metal to also do TLB flushing while interrupts are disabled. Various places in the mm do still block IRQs or disable preemption as an implicit way to block RCU frees. That makes it safe to use INVLPGB on AMD CPUs. Signed-off-by: Rik van Riel Suggested-by: Peter Zijlstra Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/Kconfig | 2 +- arch/x86/kernel/paravirt.c | 7 +------ arch/x86/mm/pgtable.c | 16 ++++------------ mm/mmu_gather.c | 4 ++-- 4 files changed, 8 insertions(+), 21 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 9d7bd0ae48c4..e8743f8c9fd0 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -274,7 +274,7 @@ config X86 select HAVE_PCI select HAVE_PERF_REGS select HAVE_PERF_USER_STACK_DUMP - select MMU_GATHER_RCU_TABLE_FREE if PARAVIRT + select MMU_GATHER_RCU_TABLE_FREE select MMU_GATHER_MERGE_VMAS select HAVE_POSIX_CPU_TIMERS_TASK_WORK select HAVE_REGS_AND_STACK_ACCESS_API diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c index fec381533555..2b78a6b466ed 100644 --- a/arch/x86/kernel/paravirt.c +++ b/arch/x86/kernel/paravirt.c @@ -59,11 +59,6 @@ void __init native_pv_lock_init(void) static_branch_enable(&virt_spin_lock_key); } -static void native_tlb_remove_table(struct mmu_gather *tlb, void *table) -{ - tlb_remove_page(tlb, table); -} - struct static_key paravirt_steal_enabled; struct static_key paravirt_steal_rq_enabled; @@ -191,7 +186,7 @@ struct paravirt_patch_template pv_ops = { .mmu.flush_tlb_kernel = native_flush_tlb_global, .mmu.flush_tlb_one_user = native_flush_tlb_one_user, .mmu.flush_tlb_multi = native_flush_tlb_multi, - .mmu.tlb_remove_table = native_tlb_remove_table, + .mmu.tlb_remove_table = tlb_remove_table, .mmu.exit_mmap = paravirt_nop, .mmu.notify_page_enc_status_changed = paravirt_nop, diff --git a/arch/x86/mm/pgtable.c b/arch/x86/mm/pgtable.c index 5745a354a241..3dc4af1f7868 100644 --- a/arch/x86/mm/pgtable.c +++ b/arch/x86/mm/pgtable.c @@ -18,14 +18,6 @@ EXPORT_SYMBOL(physical_mask); #define PGTABLE_HIGHMEM 0 #endif -#ifndef CONFIG_PARAVIRT -static inline -void paravirt_tlb_remove_table(struct mmu_gather *tlb, void *table) -{ - tlb_remove_page(tlb, table); -} -#endif - gfp_t __userpte_alloc_gfp = GFP_PGTABLE_USER | PGTABLE_HIGHMEM; pgtable_t pte_alloc_one(struct mm_struct *mm) @@ -54,7 +46,7 @@ void ___pte_free_tlb(struct mmu_gather *tlb, struct page *pte) { pagetable_pte_dtor(page_ptdesc(pte)); paravirt_release_pte(page_to_pfn(pte)); - paravirt_tlb_remove_table(tlb, pte); + tlb_remove_table(tlb, pte); } #if CONFIG_PGTABLE_LEVELS > 2 @@ -70,7 +62,7 @@ void ___pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd) tlb->need_flush_all = 1; #endif pagetable_pmd_dtor(ptdesc); - paravirt_tlb_remove_table(tlb, ptdesc_page(ptdesc)); + tlb_remove_table(tlb, ptdesc_page(ptdesc)); } #if CONFIG_PGTABLE_LEVELS > 3 @@ -80,14 +72,14 @@ void ___pud_free_tlb(struct mmu_gather *tlb, pud_t *pud) pagetable_pud_dtor(ptdesc); paravirt_release_pud(__pa(pud) >> PAGE_SHIFT); - paravirt_tlb_remove_table(tlb, virt_to_page(pud)); + tlb_remove_table(tlb, virt_to_page(pud)); } #if CONFIG_PGTABLE_LEVELS > 4 void ___p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d) { paravirt_release_p4d(__pa(p4d) >> PAGE_SHIFT); - paravirt_tlb_remove_table(tlb, virt_to_page(p4d)); + tlb_remove_table(tlb, virt_to_page(p4d)); } #endif /* CONFIG_PGTABLE_LEVELS > 4 */ #endif /* CONFIG_PGTABLE_LEVELS > 3 */ diff --git a/mm/mmu_gather.c b/mm/mmu_gather.c index 99b3e9408aa0..59fd0137af63 100644 --- a/mm/mmu_gather.c +++ b/mm/mmu_gather.c @@ -246,8 +246,8 @@ static void __tlb_remove_table_free(struct mmu_table_batch *batch) * IRQs delays the completion of the TLB flush we can never observe an already * freed page. * - * Architectures that do not have this (PPC) need to delay the freeing by some - * other means, this is that means. + * Architectures that do not use IPIs (PPC, x86 paravirt, AMD INVLPGB, ARM64) + * need to delay the freeing by some other means, this is that means. * * What we do is batch the freed directory pages (tables) and RCU free them. * We use the sched RCU variant, as that guarantees that IRQ/preempt disabling From patchwork Tue Feb 11 21:07:57 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: 13970697 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 EAF04C0219D for ; Tue, 11 Feb 2025 21:09:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2DC3E6B008C; Tue, 11 Feb 2025 16:09:07 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 15B7B280004; Tue, 11 Feb 2025 16:09:07 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E721B280002; Tue, 11 Feb 2025 16:09:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id B58FF6B008C for ; Tue, 11 Feb 2025 16:09:06 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5F73E120B9E for ; Tue, 11 Feb 2025 21:09:06 +0000 (UTC) X-FDA: 83108903892.30.D10FB54 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf23.hostedemail.com (Postfix) with ESMTP id C76CE140016 for ; Tue, 11 Feb 2025 21:09:04 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf23.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=1739308144; a=rsa-sha256; cv=none; b=vlQyoMPjnZG55SOB2ecDWoP30XNN/dKqpPPxlzCUF1wGvmJvV+OLeHzZUG6Nzm2VcGPMpG 5shf6hDVUQXd2+7BPxKJqKmpzNd0BajivQ8hA4lsBWjtRTmCSMO6gNmflLLjpTCiLrF+Dm tEoU+rFpg10pdmUV2w7mfQi2odGTvs4= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf23.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=1739308144; 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=a8Lc0/l5LZ9sGfCvyre++KeBAz2TLECIF2bnpXvR3D8=; b=IIUia6Q2XGCVkUzlDsmowJCaT7a0zGyKjrLqwhark74hp9AGmvRXKxQuQROICd+mueQhVd twO1E0Nc4hH0VrUyoET7cm2a05fePcT72nV+iiEvcGO0B4lJ5Mp77GmSPAzHvgKsA+42Bg mLRMWoKJEofS5Gy1UMhmHiNB0If9Q40= 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-2dg1; 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 02/12] x86/mm: remove pv_ops.mmu.tlb_remove_table call Date: Tue, 11 Feb 2025 16:07:57 -0500 Message-ID: <20250211210823.242681-3-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-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: C76CE140016 X-Stat-Signature: z3kmcaa9w6xyqkmx1q5eaodbihszj4ct X-Rspam-User: X-HE-Tag: 1739308144-835886 X-HE-Meta: U2FsdGVkX18s6DyJkO6nHssjLx+IvCBDN+NpXZyV2tdV0htZO6IKIrsovJaJD8W3dbFeq6C2sL2CLUiQQAwbK1LbgwOib2sMRsQdecQo1JDhWVQLwYSlHGk0aKaBQo3k1MnnYPo23hkHYMU8JK2G/G+ehH0SnE1usge+WIMS8LZb0cyul+UbiCWGVlaJ2eFWHiC7EgL/Yyh3Ik/RkupMC1ZSnrpbeYLzzftLGihw6/kn3IW82QSJcjqnymQopefI2z/RlLPtl1ofuUViWtCIoXz9I8l1zVDXsuJ0Dil9DcZuAaWVMIwbzx+8SSdj9maxFEeIFyKg23EP9OhTrqMpmgNmiZMUPG/05EFOVJssqMzT/Ar/ut0veKD5VZ4//p2ExNnBLedfWDwA+jZjf0L6iuQDjgOcnWplj2w5iSFm1z52B66D/4Hu5lIKbMrvuWmMBJZccvWPDbQqN7rcL8R2s2IEHN1rPJFDJ9atoNTN1OKTVXFaLVdvJ3tzS0OJIl1LV1rYsLggVFLCQMjyly/hBmes6AdWsWRoZyHHJDU5MLqOXRg8MadJR5SHvFm11HuBpk2MhCkJGXFahNr6CQUwj2eg5/fH6PyP5X5OjssxRHbGkUVT1iuQcZfCDGPgAAVOvM7y4FPK61x9e3ggkulmrC+EDoogkhM+3a6+fa/IqGcmRckv8T7+X33cNB5INYLUQk86JvP4U9mAcEkfi3FkybhUtRATlUtt4iI6bfK1Y7U31h6bKGMwGu7b9xL1shIYsGPgMz7QZ11isApaVJPNunxmzPsrOH8h4Er1dT9IfZhqjzeBpq1O2tw9lLt+rYweciPqY1kYlZO4jOnV7JbuRiBLVGLKOsaWkLx9CzO7XaqjGE6xsEVbulr4RHxlmKi9usg6K+mmksdKySn1BU4fnWGNUKvl3cLuUk6+7uzACQKNU2maCIYXJPghA16uuyVFEbBLrqwmPdxoat36OBm k5P2ghYc /FI2neHKUlEvV3N9wzFaEEddVDb0x2EduLpMM9JM4LEKQI3oHuA7VhTNqjrwRsPNxbduKv4Z2ejtIvmvqgYx3TLahx/S5LSuTlKu4Z8msjn1U3Df4wA6SkgosbXuZVTG1L/N813qSokkbi3e4BArlLdum0XPo4BKjxtxbv6IQkFK73LptKheMfEBTXC60FPstLya4cjUVajYOBPEofQ2UboaQljD9e5ab8zAHaZvQzufycsaDP6yPg+m6b9TO7L89OnX/T5LhCEQMIwAPcJ741pSCduodcgrITIBV000kCmTNFnahfZyqhojpLuWA/hnO58xQOfHzH/XD+klpi9Ag2VJ/13+N+gjH+BsXs7pTybANIQeYkhau+T7Rlsr0wIFdCOUgKdeOj88fEBfgi45amwYG8fKemlHD9Z37ZEJlhnFNfQSQTqGsx+Xz7hcB1jtk1XEw9+JraX9XxHP0yz4VWnqDgtlu5h7P8D7bAlMw2nMOnvyveeh/h9rj//3cYDr7Du4F1QnFS8kL+7xXPDWY9/Y3yYrT6rm2Y/W29BOFbS+R3mTb2n7XJ0Czx1+8G74eT5I5g5C23nmavn5oIVemiQ9cIiBHt7T1aWZHyvU+o3CuQPkt9h3u0W8+7w== 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: Every pv_ops.mmu.tlb_remove_table call ends up calling tlb_remove_table. Get rid of the indirection by simply calling tlb_remove_table directly, and not going through the paravirt function pointers. Signed-off-by: Rik van Riel Suggested-by: Qi Zheng Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/hyperv/mmu.c | 1 - arch/x86/include/asm/paravirt.h | 5 ----- arch/x86/include/asm/paravirt_types.h | 2 -- arch/x86/kernel/kvm.c | 1 - arch/x86/kernel/paravirt.c | 1 - arch/x86/xen/mmu_pv.c | 1 - 6 files changed, 11 deletions(-) diff --git a/arch/x86/hyperv/mmu.c b/arch/x86/hyperv/mmu.c index 1cc113200ff5..cbe6c71e17c1 100644 --- a/arch/x86/hyperv/mmu.c +++ b/arch/x86/hyperv/mmu.c @@ -240,5 +240,4 @@ void hyperv_setup_mmu_ops(void) pr_info("Using hypercall for remote TLB flush\n"); pv_ops.mmu.flush_tlb_multi = hyperv_flush_tlb_multi; - pv_ops.mmu.tlb_remove_table = tlb_remove_table; } diff --git a/arch/x86/include/asm/paravirt.h b/arch/x86/include/asm/paravirt.h index d4eb9e1d61b8..794ba3647c6c 100644 --- a/arch/x86/include/asm/paravirt.h +++ b/arch/x86/include/asm/paravirt.h @@ -91,11 +91,6 @@ static inline void __flush_tlb_multi(const struct cpumask *cpumask, PVOP_VCALL2(mmu.flush_tlb_multi, cpumask, info); } -static inline void paravirt_tlb_remove_table(struct mmu_gather *tlb, void *table) -{ - PVOP_VCALL2(mmu.tlb_remove_table, tlb, table); -} - static inline void paravirt_arch_exit_mmap(struct mm_struct *mm) { PVOP_VCALL1(mmu.exit_mmap, mm); diff --git a/arch/x86/include/asm/paravirt_types.h b/arch/x86/include/asm/paravirt_types.h index 8d4fbe1be489..13405959e4db 100644 --- a/arch/x86/include/asm/paravirt_types.h +++ b/arch/x86/include/asm/paravirt_types.h @@ -136,8 +136,6 @@ struct pv_mmu_ops { void (*flush_tlb_multi)(const struct cpumask *cpus, const struct flush_tlb_info *info); - void (*tlb_remove_table)(struct mmu_gather *tlb, void *table); - /* Hook for intercepting the destruction of an mm_struct. */ void (*exit_mmap)(struct mm_struct *mm); void (*notify_page_enc_status_changed)(unsigned long pfn, int npages, bool enc); diff --git a/arch/x86/kernel/kvm.c b/arch/x86/kernel/kvm.c index 7a422a6c5983..3be9b3342c67 100644 --- a/arch/x86/kernel/kvm.c +++ b/arch/x86/kernel/kvm.c @@ -838,7 +838,6 @@ static void __init kvm_guest_init(void) #ifdef CONFIG_SMP if (pv_tlb_flush_supported()) { pv_ops.mmu.flush_tlb_multi = kvm_flush_tlb_multi; - pv_ops.mmu.tlb_remove_table = tlb_remove_table; pr_info("KVM setup pv remote TLB flush\n"); } diff --git a/arch/x86/kernel/paravirt.c b/arch/x86/kernel/paravirt.c index 2b78a6b466ed..c019771e0123 100644 --- a/arch/x86/kernel/paravirt.c +++ b/arch/x86/kernel/paravirt.c @@ -186,7 +186,6 @@ struct paravirt_patch_template pv_ops = { .mmu.flush_tlb_kernel = native_flush_tlb_global, .mmu.flush_tlb_one_user = native_flush_tlb_one_user, .mmu.flush_tlb_multi = native_flush_tlb_multi, - .mmu.tlb_remove_table = tlb_remove_table, .mmu.exit_mmap = paravirt_nop, .mmu.notify_page_enc_status_changed = paravirt_nop, diff --git a/arch/x86/xen/mmu_pv.c b/arch/x86/xen/mmu_pv.c index 55a4996d0c04..041e17282af0 100644 --- a/arch/x86/xen/mmu_pv.c +++ b/arch/x86/xen/mmu_pv.c @@ -2137,7 +2137,6 @@ static const typeof(pv_ops) xen_mmu_ops __initconst = { .flush_tlb_kernel = xen_flush_tlb, .flush_tlb_one_user = xen_flush_tlb_one_user, .flush_tlb_multi = xen_flush_tlb_multi, - .tlb_remove_table = tlb_remove_table, .pgd_alloc = xen_pgd_alloc, .pgd_free = xen_pgd_free, From patchwork Tue Feb 11 21:07:58 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: 13970696 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 95D09C021A1 for ; Tue, 11 Feb 2025 21:09:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E7558280003; Tue, 11 Feb 2025 16:09:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D6255280001; Tue, 11 Feb 2025 16:09:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3D2E6B0093; Tue, 11 Feb 2025 16:09:06 -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 955056B008C for ; Tue, 11 Feb 2025 16:09:06 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4D83E80B64 for ; Tue, 11 Feb 2025 21:09:06 +0000 (UTC) X-FDA: 83108903892.07.D197D1E Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf25.hostedemail.com (Postfix) with ESMTP id B7430A0010 for ; Tue, 11 Feb 2025 21:09:04 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.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=1739308144; a=rsa-sha256; cv=none; b=CRrDbXgaa5gTwLThI9mXDLK2s6ggVPig8n5biTsQZw4o24YroY8EOPltJ/H8PplM6xMYPH Dqa6zkvZePFlSQvMUKPbADQ/zZp65/0RmAOvAwXH/CxVLpoHO+zKEyt5Bl2nNhG+XVInvl E2aMe1wl4C3XB8+O/JDURtp3mcDs4SI= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.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=1739308144; 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=UlPy0p14IsQWBCIsvN3dzDIKE7Ry1N2cej3bhiK/m74=; b=voRo9thwtkOoX3eaoVyok7IxYMsKvNwjCtmeMA9DhdLqHrVSoD5CNUCe/4175pyibf4UK8 n4Fi47OR8taKzBZAyCv5Zmm90cg0vPoRHnk1Y+zfWwLjOow3jEuar4sh4mdSL8qz8SMnuJ inzwWQwh81KYGxHGi02CN6/l0zg4VNo= 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-2jR8; 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 , Dave Hansen Subject: [PATCH v10 03/12] x86/mm: consolidate full flush threshold decision Date: Tue, 11 Feb 2025 16:07:58 -0500 Message-ID: <20250211210823.242681-4-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-Stat-Signature: 6t31yrfijib6desum3aph4t3mkef9ngx X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: B7430A0010 X-Rspam-User: X-HE-Tag: 1739308144-652362 X-HE-Meta: U2FsdGVkX19u3vh13FntgkMfnFMyWzIRassxm4hqtvn8Cm9fY49JWkFhMNjRKtrCJyVJQpYBzU7/goV72Fp16wPG6t11wvET0ZjFawiRBYRdnrHN4HEK7CL+/yFGAL263G2pgsbJHGFPLQNYXZbF/M/Mmx5L6EOf+UjK/oDgWT7Z9Zk0ZdSlhabAgwk/s0Smo+4PYO8a9Jhvtpjp+Le+Pup8xSVgA4iwZMpcrGh8/Dp2hHbYjkmsjvHFwuAAvm11RXLwvTbFBOa0BvitU7SO+3BtC9xlfIiiXJVMnfQpVCp4sDIX98uK3K4sK7vh0P+nVbRyN2PMMEz/qznF1Ybil51sUxJd1yaf28iCmtqqxBSvritmfALrzF87Gt8o7RgEqh2Iew5Hsc1pQ85IOEKca9E2HDNeMryq2VTW9o3iktdfX+jiPvEAvKw/a7mHrsqAYVfHrVgORPBBSwUTNG1kvnCQYR6x3VRvGIdWJAhf+2dRkEjngtS5doY8pk0a5gPOiQaH9BcAX//QF4AQ446vBelJcUpNBZDgFXii+0pQ9oK0f5FYk6v4XGjMR63zpytCXrcgQjnmyZp5DhCU2qKuUd5o8HllNvuY74GOXGo8dT28igzukVAxLAXHByKX04yAKMEgohXEz+KFWWZz3+v/jMI5lNDosXkuUhExkkYvihBvTeA8HfRvPQ7HMyPPF0MMhR6g//70q6di8yvLE3cT0L+W3PiPANIBSwBYVe26rdPtU+PTOHmWVFsZatj1eFmxcpMjxRwBiDYAQyi9xcYX8SDEfqSi0PLamNBgFhknAv9c8XprqiSvQX83tEegp4z9exjjNp1ixBvjaViyPFPV7b1rwrBuaZZXjNT+bt9DSC0TudUaRdGg50IuRRbfltNWL5ITBLsMWRv/QOdX3xA9C+BuI+VtAdBa4CkSh4OjsNNVh5R5fvx2Y1OvGmrumL6jvAhJoTNcX9m13vf+UwW hp7h6+px nxaHkgWP0d2w9pAjYLPaYrD81cUFRtcsT783vV7+lm6Soeh3Vmpo4iSh5t1T23g3RCpKojnqfXchmOlvMepIyPi1+HEt3Gz+3Vkcj6GBqs8nPye5Vsh8VZnIgNDjkcAgcsvP1vwmrzVyp5/zHpGAALfMgi3ajlLbtdgqOLXoNrzTvM1CgcFhPERxWZy4wYeH8uHG77PtFIZbcqrTORH0VtDz3IZ7K/o9ELOZq7DkOo8ZMlALf7x6Xp1tNKdWd3lgp1cJNa3jo4dDnqHQsbd6GiQOkwzomXxMZqu7Mwmz/DCMXsKmxj4YMqORHRuQPWNnvQ893OKhy/flGyXH+VVtIJOCEPwWJCYHly6n7RNlgeIwOj/z6HaXGUHv/UiekhpR0Unmvpjk4pdx84HXhw2jBE+BveTnYPZvibumB5aaYP6g/YvwixwRASOo0fK1y1MlvuvYTYmW2l4ihnjI3NX8lM0XR4EIDJZ+CROzYnu0Nbt3c/t3c/y2teIAKZA== 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: Reduce code duplication by consolidating the decision point for whether to do individual invalidations or a full flush inside get_flush_tlb_info. Signed-off-by: Rik van Riel Suggested-by: Dave Hansen --- arch/x86/mm/tlb.c | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 6cf881a942bb..58c8a6a94e65 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1009,6 +1009,15 @@ static struct flush_tlb_info *get_flush_tlb_info(struct mm_struct *mm, info->initiating_cpu = smp_processor_id(); info->trim_cpumask = 0; + /* + * If the number of flushes is so large that a full flush + * would be faster, do a full flush. + */ + if ((end - start) >> stride_shift > tlb_single_page_flush_ceiling) { + info->start = 0; + info->end = TLB_FLUSH_ALL; + } + return info; } @@ -1026,17 +1035,8 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, bool freed_tables) { struct flush_tlb_info *info; + int cpu = get_cpu(); u64 new_tlb_gen; - int cpu; - - cpu = get_cpu(); - - /* Should we flush just the requested range? */ - if ((end == TLB_FLUSH_ALL) || - ((end - start) >> stride_shift) > tlb_single_page_flush_ceiling) { - start = 0; - end = TLB_FLUSH_ALL; - } /* This is also a barrier that synchronizes with switch_mm(). */ new_tlb_gen = inc_mm_tlb_gen(mm); @@ -1089,22 +1089,19 @@ static void do_kernel_range_flush(void *info) void flush_tlb_kernel_range(unsigned long start, unsigned long end) { - /* Balance as user space task's flush, a bit conservative */ - if (end == TLB_FLUSH_ALL || - (end - start) > tlb_single_page_flush_ceiling << PAGE_SHIFT) { - on_each_cpu(do_flush_tlb_all, NULL, 1); - } else { - struct flush_tlb_info *info; + struct flush_tlb_info *info; - preempt_disable(); - info = get_flush_tlb_info(NULL, start, end, 0, false, - TLB_GENERATION_INVALID); + guard(preempt)(); + info = get_flush_tlb_info(NULL, start, end, PAGE_SHIFT, false, + TLB_GENERATION_INVALID); + + if (info->end == TLB_FLUSH_ALL) + on_each_cpu(do_flush_tlb_all, NULL, 1); + else on_each_cpu(do_kernel_range_flush, info, 1); - put_flush_tlb_info(); - preempt_enable(); - } + put_flush_tlb_info(); } /* From patchwork Tue Feb 11 21:07:59 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: 13970695 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 5C569C0219B for ; Tue, 11 Feb 2025 21:09:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 04D156B008A; Tue, 11 Feb 2025 16:09:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DADD8280001; Tue, 11 Feb 2025 16:09:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC8FE6B0092; Tue, 11 Feb 2025 16:09:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 792126B008C for ; Tue, 11 Feb 2025 16:09:05 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 33FDBA0B5C for ; Tue, 11 Feb 2025 21:09:05 +0000 (UTC) X-FDA: 83108903850.18.79A30BB Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf14.hostedemail.com (Postfix) with ESMTP id A6AFF10000B for ; Tue, 11 Feb 2025 21:09:03 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf14.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=1739308143; 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=PyhXFUBDGnnCh+Ky8qsTy+jYofYsUxnCj7+3iJ6jxmU=; b=Sa2J9WvPHPh6WTYPvN6uswx2MdRwrbbE64Y1850ks9HIBAar0y3tN+/jDnhbeemn6h6xlq j9Luh+PquskLrZ7pYFB0qzsW1pmZbaYkYLFvfU8MMmnTValTF5DSMagbwpKkUamZrBLBHO fdTOoxr/Hadd2XrB6/s1NknkRSbBVek= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf14.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=1739308143; a=rsa-sha256; cv=none; b=du2sUyU2LSCUCNcPveAJyuZ5SdGVcQ+mI1w/lJk3vcvSXdTSd5rb31Iqa4345jXw/VOC2C Nv4W+VMNL+17FtIp21xzNgoOLuglGA7ME7gYcF/jqBJ7F9z6Qo8xpb6FlqtANF+v35T0/J Iu08KPzY4Q6QIRF0FFXQR8Zx1TJ1VF8= 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-2paU; 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 04/12] x86/mm: get INVLPGB count max from CPUID Date: Tue, 11 Feb 2025 16:07:59 -0500 Message-ID: <20250211210823.242681-5-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-Server: rspam08 X-Rspamd-Queue-Id: A6AFF10000B X-Stat-Signature: bg9bpzyiwefzxznbqh4xfp9rws4uj9f4 X-HE-Tag: 1739308143-164087 X-HE-Meta: U2FsdGVkX19zbayFS1cPparQVuCrMsdv4kbTqdZNKgRNrJCeFtWAd6nLxV2Nko29vlnqQKTXSNH62ealFfDBeAOjcOOZ4anPlBakNR7HLW0/F6ZnrCRD1TIv3+OnnF2QRol8zzcZaFxHCOVVRRzDPM+vKwUS9Waq4XsjP4YRoNEFxUei400ualGWW02XUMTjWscDX3lSp1RxNr2CoTliK0iqq4ITWBb3fYYdpLK9Xi2HuNUk2v8x1gKx+nyM3lux9HNy2NKwRfxdnsBVXBUQWiJXVI3FPOUK5ZiY+/97sD3nj+bx3Gm1/mC0XYlbhbNNniH/3XErtH5h7jF0D9PXSzkQyPu09AbaF2GItFVHQQXQaA7M22REO6qkdISKQjPwWc1OsI8wYQIztHPGimI0H3TpPNNtFqTa3kj+EHsqrInz3lgh3vGnlCMljyXcVp1JEGKzDnR4Jxxkb0aS2Z2+a0LxX/zcb+z0iWcjBzlsLFBvqxtb3Ht34p6I8R58boOixvsByhQfhBn5SIm5FqtS/+iec088f4ohMHrdJsPr/2UG5+AIegEF1f+METZqayQ6o037XJ3UP7DXZs8bYeDLVZrtYWtr5nuJ2yiNOo88UU/1dCc632cvuTQUec9jwfBv+R+mxKMS1xVrdZzseXKvNcAYbJ+/EkfsxzacEL3DFMRZUpvhZWRoNP4Caw8XSufIoeZ6JWiPgigiuZ05Wo7c0K4IcyPA/EwEzb+cXSMnCiwdCPhIuLQ1Yf4clCyccztWCUMmGx7t35F0ap7/w1JHl02Ju4cTibK6PMMNV/JsRLFFBrcezAfOAu00qNXj4J7Rsi8601OFEOVJV9B7dXSuwRBKDpxpwUIDUd9IjAeNqLDc1+yj90xsOEhUT3BuITGr/OBz3tkhcq/F1sz7WNfBrwE6wYPG8So70x992mPHwWYVSabIUedinTf+9PhICgO+Cd+8e7RMFtLXG6QjOvW 7WiadsD8 9K5zzgyNGfi5c/fS/9ojv4z9exNKgIX9uGgcE/jbYMpzHu0WREuA3t1I3L6TkirAw7Kq+ldViSPplw5mZrQP0mc4Wu1TM0t6TUopKYA5rBqMOvcIe/R73ZSMLk/Kqx4E8G+9yYnR0d7L/gNu4qJ51ZzJJunUsu2h17SfKkPb0v2RHEK5YQrqvovPyruilH0Xqb7GQfuudMkEUAfWwCEcjs85XS17vBbm+J7MJnKIEt2hOg5PM7oyE/ET3N2Ur0HO6ov3NBcJ8tjno7TmV/KH+z66muiNo2avpbQCz4wd9wfNjS508kj8h2Fk9u37auyHA+I7Fp7vuu70LhW53VNcs2RfsxlvC2JirzkrL3pPzkxfwUESmo5kckG0oiZFLy7HaK++0yp1lyu3g/ksm91y0idh5b/sB4xzrTYxs6EoU0hXWRI7th6kxFlXwMrKTqgD/xyUwkxAU62jwKm0G0xfr2NFmvr+yWQjEEdnyxhYnBbdjhnvEc1WYZzOg1IRAH/3YCUGmziaqTjgJyms= 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: The CPU advertises the maximum number of pages that can be shot down with one INVLPGB instruction in the CPUID data. Save that information for later use. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/Kconfig.cpu | 5 +++++ arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/tlbflush.h | 7 +++++++ arch/x86/kernel/cpu/amd.c | 8 ++++++++ 4 files changed, 21 insertions(+) diff --git a/arch/x86/Kconfig.cpu b/arch/x86/Kconfig.cpu index 2a7279d80460..abe013a1b076 100644 --- a/arch/x86/Kconfig.cpu +++ b/arch/x86/Kconfig.cpu @@ -395,6 +395,10 @@ config X86_VMX_FEATURE_NAMES def_bool y depends on IA32_FEAT_CTL +config X86_BROADCAST_TLB_FLUSH + def_bool y + depends on CPU_SUP_AMD && 64BIT + menuconfig PROCESSOR_SELECT bool "Supported processor vendors" if EXPERT help @@ -431,6 +435,7 @@ config CPU_SUP_CYRIX_32 config CPU_SUP_AMD default y bool "Support AMD processors" if PROCESSOR_SELECT + select X86_BROADCAST_TLB_FLUSH help This enables detection, tunings and quirks for AMD processors diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index 17b6590748c0..f9b832e971c5 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -338,6 +338,7 @@ #define X86_FEATURE_CLZERO (13*32+ 0) /* "clzero" CLZERO instruction */ #define X86_FEATURE_IRPERF (13*32+ 1) /* "irperf" Instructions Retired Count */ #define X86_FEATURE_XSAVEERPTR (13*32+ 2) /* "xsaveerptr" Always save/restore FP error pointers */ +#define X86_FEATURE_INVLPGB (13*32+ 3) /* INVLPGB and TLBSYNC instruction supported. */ #define X86_FEATURE_RDPRU (13*32+ 4) /* "rdpru" Read processor register at user level */ #define X86_FEATURE_WBNOINVD (13*32+ 9) /* "wbnoinvd" WBNOINVD instruction */ #define X86_FEATURE_AMD_IBPB (13*32+12) /* Indirect Branch Prediction Barrier */ diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 02fc2aa06e9e..8fe3b2dda507 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -183,6 +183,13 @@ static inline void cr4_init_shadow(void) extern unsigned long mmu_cr4_features; extern u32 *trampoline_cr4_features; +/* How many pages can we invalidate with one INVLPGB. */ +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH +extern u16 invlpgb_count_max; +#else +#define invlpgb_count_max 1 +#endif + extern void initialize_tlbstate_and_flush(void); /* diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 79d2e17f6582..bcf73775b4f8 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -29,6 +29,8 @@ #include "cpu.h" +u16 invlpgb_count_max __ro_after_init; + static inline int rdmsrl_amd_safe(unsigned msr, unsigned long long *p) { u32 gprs[8] = { 0 }; @@ -1135,6 +1137,12 @@ static void cpu_detect_tlb_amd(struct cpuinfo_x86 *c) tlb_lli_2m[ENTRIES] = eax & mask; tlb_lli_4m[ENTRIES] = tlb_lli_2m[ENTRIES] >> 1; + + /* Max number of pages INVLPGB can invalidate in one shot */ + if (boot_cpu_has(X86_FEATURE_INVLPGB)) { + cpuid(0x80000008, &eax, &ebx, &ecx, &edx); + invlpgb_count_max = (edx & 0xffff) + 1; + } } static const struct cpu_dev amd_cpu_dev = { From patchwork Tue Feb 11 21:08:00 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: 13970701 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 C4919C021A1 for ; Tue, 11 Feb 2025 21:09:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 09392280001; Tue, 11 Feb 2025 16:09:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F1017280005; Tue, 11 Feb 2025 16:09:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3747280001; Tue, 11 Feb 2025 16:09:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 897926B0096 for ; Tue, 11 Feb 2025 16:09:08 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 457F44A53D for ; Tue, 11 Feb 2025 21:09:08 +0000 (UTC) X-FDA: 83108903976.30.B18BF5B Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf25.hostedemail.com (Postfix) with ESMTP id BC66EA000F for ; Tue, 11 Feb 2025 21:09:06 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf25.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=1739308146; 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=6qz9gCbUXEYLUIUcJSjSc2JYCHkXZ37KXS7QGRocORI=; b=TRMCyrD4X5HWlL4SQKMv/3zWZRGAavV5k5UnRPdBE/mIgY/fc52NTvH64wbtYDdBv7ID2D zhAZP3YiYXML+PeL0ehCQRnkrBYSV/ekn3zaI+WyDEpYyfZm4Q0Avq2BF7b6muyoYPcFnp ldhBfDd0vXWzVDDVwv6S7VPaIJ08CGA= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf25.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=1739308146; a=rsa-sha256; cv=none; b=tqG28DRW6gOuHn9gZizOEGCNTe/BPanUuI2vjtr1MBrQLBV6gNT9AORFo9tj8aHD3lOU7v lkKEdaTxbMQ4JqBbDAaS2b2OMItESkDkbbPcY5A+NkoLoPl5Zx7Zoec0WrSFO7FWhxB5ul HvPUudeHqiGIb/hHMbrIXaFXdDV+V9w= 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-2xFb; 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 05/12] x86/mm: add INVLPGB support code Date: Tue, 11 Feb 2025 16:08:00 -0500 Message-ID: <20250211210823.242681-6-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-Server: rspam09 X-Rspamd-Queue-Id: BC66EA000F X-Stat-Signature: mmo3y389cmuqcuzazxifjfubpzbce6tp X-HE-Tag: 1739308146-826126 X-HE-Meta: U2FsdGVkX189Xv7uKHi3NjnGufDtZ+sdG6tcqPUSsrn4UYJaL4rx7bhFv2Bsw349U+U5DbYNlhBz5AFXmfByQ7xtviW2vDLkJhdxlnLQk7L9ao7T0935wcCLrIisdsRKvwdao4cNngGXtzDRob/SRKuysSQZGNA3sTjYIVutmiYeFePfIgL7ovljotaLLB0yugI3hb4gFB6ViM5OJkUYu21PM3piCwxxu4+ie5UeUJ+nS1yRoLCOOdC+J9njJK2TMQ+7t56Z43LQkYmJExWoWjRED0ZXGph11vefpJrQdCh/SU40zA1TdpdkvO+pzP96ogmrYJF78/yaqxaGTWp0/jFx+SBpI5QR28LwtmWMLJ16oGKSrQO5Db4wqQSlZO6bHpp/uxU0PKLtr7P2mbU3q65IPcQj4Kuz7tScxfxKqjgEP5LdbQ5XIzcWP/99oagnZz3L/l37XUpLgfeVcx3GVl/IcrO6kOVRBcdZeBw5kMZ/YplbPrlZzvhLyoNQyN9u/KfTvbkLnYW2T1z+GOMjQP/bdLl5v2Sd33vNg6ll/BKY8oRVcM1FLMZR0nR3fC3XYM38AQm7tr1LPTmQeuOfggNQmJmVY2RJkJHeMXnEb+Xxu8v6PYlMSfXUfYRO1/CJmrmZlOXQnz2/fzk6Aaksb7b3lkHl7tbVrOFX/8C8gWqevFLo2mdMckZW+EMyFCY23LyhLEQAVzIHGFpqWCAgLGooanZeXc6ifrKHZp7OwLoatx5oryLYSqRieEPGPUj6TMnv1tnAcPfXUh+SdiOWwuAbEPk25xSLcKIi4OWhCt3iJQA7GWYNFveQhigu6sKZ3+UmDPaV677/aGTE3Pzu1zLom9B2nbVwPy7NCQH08MkcVVs5yvKY1kUHoGF5GLF+ecDtRbmitZRPG2NYbvnHpTLpm5y5p8Xg+16otbUuttwYCJPMmd6XKh+38WJrBMkNNAi8yC+JnnqFpr/0Ylc AEX87Els OC8yMk+VUB5PyCOZitCP2KkONsyatb5juf3iG8fUcJSbeV//bkILTpMhEXFefUqdlfG20tQbAtNhh8m7O8gBXUnUyx4NSRtQv5Sa+7ZhNYGBMvLTEbjoKHuxNXBtv1MTmKtj/DJQnlZ24iUZvCz88N2esSJqmOXZXr1M4Z7iEYliUtBh8CKFMEmgtY51i4cNTo5Ut7GbcKHekNie8cqpMII9BTL0KBx1t/bim0S9dBhcht9iiaGq6Hcl0YTTQoQkSo+cbCMpDRq9+UJ1lvm700DQo6dpJubbzS2roUyMadYuFTX3RkoTAtpmOGCskdw+Ny0dewbX8pX49UY3wjJ78eLvwnxA2ehXDs+eNiFZkiCoqHGb1Dzzo1odZ1XpAVhceeYN5hqaFzfohG5Fbh7RtsNQP+GiUOVL5Kc4TYpGz/vmxYPZKt9ERTc4fQb19fcMIr7ZGzdbCRsQBJc0yY/X+0FxqNJKDr73rLr55ZXO044f8z6PtsOZW8Uwloy404Jdha3ftnNo1z90n7NU= 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: Add invlpgb.h with the helper functions and definitions needed to use broadcast TLB invalidation on AMD EPYC 3 and newer CPUs. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/invlpgb.h | 101 +++++++++++++++++++++++ arch/x86/include/asm/tlbflush.h | 1 + 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/invlpgb.h diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index c492bdc97b05..625a89259968 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -129,6 +129,12 @@ #define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) #endif +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH +#define DISABLE_INVLPGB 0 +#else +#define DISABLE_INVLPGB (1 << (X86_FEATURE_INVLPGB & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -146,7 +152,7 @@ #define DISABLED_MASK11 (DISABLE_RETPOLINE|DISABLE_RETHUNK|DISABLE_UNRET| \ DISABLE_CALL_DEPTH_TRACKING|DISABLE_USER_SHSTK) #define DISABLED_MASK12 (DISABLE_FRED|DISABLE_LAM) -#define DISABLED_MASK13 0 +#define DISABLED_MASK13 (DISABLE_INVLPGB) #define DISABLED_MASK14 0 #define DISABLED_MASK15 0 #define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h new file mode 100644 index 000000000000..a1d5dedd5217 --- /dev/null +++ b/arch/x86/include/asm/invlpgb.h @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_INVLPGB +#define _ASM_X86_INVLPGB + +#include +#include +#include + +/* + * INVLPGB does broadcast TLB invalidation across all the CPUs in the system. + * + * The INVLPGB instruction is weakly ordered, and a batch of invalidations can + * be done in a parallel fashion. + * + * TLBSYNC is used to ensure that pending INVLPGB invalidations initiated from + * this CPU have completed. + */ +static inline void __invlpgb(unsigned long asid, unsigned long pcid, + unsigned long addr, u16 extra_count, + bool pmd_stride, u8 flags) +{ + u32 edx = (pcid << 16) | asid; + u32 ecx = (pmd_stride << 31) | extra_count; + u64 rax = addr | flags; + + /* The low bits in rax are for flags. Verify addr is clean. */ + VM_WARN_ON_ONCE(addr & ~PAGE_MASK); + + /* INVLPGB; supported in binutils >= 2.36. */ + asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx)); +} + +/* Wait for INVLPGB originated by this CPU to complete. */ +static inline void tlbsync(void) +{ + cant_migrate(); + /* TLBSYNC: supported in binutils >= 0.36. */ + asm volatile(".byte 0x0f, 0x01, 0xff" ::: "memory"); +} + +/* + * INVLPGB can be targeted by virtual address, PCID, ASID, or any combination + * of the three. For example: + * - INVLPGB_VA | INVLPGB_INCLUDE_GLOBAL: invalidate all TLB entries at the address + * - INVLPGB_PCID: invalidate all TLB entries matching the PCID + * + * The first can be used to invalidate (kernel) mappings at a particular + * address across all processes. + * + * The latter invalidates all TLB entries matching a PCID. + */ +#define INVLPGB_VA BIT(0) +#define INVLPGB_PCID BIT(1) +#define INVLPGB_ASID BIT(2) +#define INVLPGB_INCLUDE_GLOBAL BIT(3) +#define INVLPGB_FINAL_ONLY BIT(4) +#define INVLPGB_INCLUDE_NESTED BIT(5) + +/* Flush all mappings for a given pcid and addr, not including globals. */ +static inline void invlpgb_flush_user(unsigned long pcid, + unsigned long addr) +{ + __invlpgb(0, pcid, addr, 0, 0, INVLPGB_PCID | INVLPGB_VA); + tlbsync(); +} + +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) +{ + __invlpgb(0, pcid, 0, 0, 0, INVLPGB_PCID); +} + +/* Flush all mappings, including globals, for all PCIDs. */ +static inline void invlpgb_flush_all(void) +{ + __invlpgb(0, 0, 0, 0, 0, INVLPGB_INCLUDE_GLOBAL); + tlbsync(); +} + +/* Flush addr, including globals, for all PCIDs. */ +static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) +{ + __invlpgb(0, 0, addr, nr - 1, 0, INVLPGB_INCLUDE_GLOBAL); +} + +/* Flush all mappings for all PCIDs except globals. */ +static inline void invlpgb_flush_all_nonglobals(void) +{ + __invlpgb(0, 0, 0, 0, 0, 0); + tlbsync(); +} + +#endif /* _ASM_X86_INVLPGB */ diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 8fe3b2dda507..dba5caa4a9f4 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include From patchwork Tue Feb 11 21:08:01 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: 13970699 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 472F8C0219B for ; Tue, 11 Feb 2025 21:09:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 85EBD6B0095; Tue, 11 Feb 2025 16:09:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 57391280001; Tue, 11 Feb 2025 16:09:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F62B6B0093; Tue, 11 Feb 2025 16:09:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D58436B0095 for ; Tue, 11 Feb 2025 16:09:07 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 91BBD120BA0 for ; Tue, 11 Feb 2025 21:09:07 +0000 (UTC) X-FDA: 83108903934.23.0094F10 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf04.hostedemail.com (Postfix) with ESMTP id E09104000C for ; Tue, 11 Feb 2025 21:09:05 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; spf=pass (imf04.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=1739308146; 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=vnXS272Wjg7KQ1wY+9yT30v+pSEs7yyG8KzaUN+cYd8=; b=VA6j2ALIbR3hq9zHbfcy7mfNadSa7SNijFJTiLYj/Ff7+7DuPGXFKnpwDRY3FWE6RDzeHC d+sKSpbqHw5A3eJoi4UzSDLF7g/Ze7F/vLAYJrhqoPfr5rVNES7tqhcYkGbMtXyFFpwIoo LWa6GAO3eBBwqqXdDytXZFueL6llsec= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; spf=pass (imf04.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=1739308146; a=rsa-sha256; cv=none; b=ewhdHDaxgyVemqpxf5PAnexv3JS6sGdr7aEdlM8BZ9yBAa+uWmchXssQGyjK+T1/e6Fnid 8u5LQAF5bfQ6riu8bI4+MOZ6obh45lPr2jhJW4arr4/N/u7wxaMWHSfWhtaIN0KSw5DTEI 6WyR+XXQsRWm8S0rZitxuGr0NE8fp7I= 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-32ms; 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 06/12] x86/mm: use INVLPGB for kernel TLB flushes Date: Tue, 11 Feb 2025 16:08:01 -0500 Message-ID: <20250211210823.242681-7-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: E09104000C X-Stat-Signature: bwej6oom99zdgeisxxn9wjkdp3a88q7y X-Rspamd-Server: rspam03 X-HE-Tag: 1739308145-398394 X-HE-Meta: U2FsdGVkX18hqFxiO62QhvAsWGPgFJzBpexLoaRLbEtulsXMFo30uEKqh2eyWIr6ytRf5kd+EDnVwKktJOyAX0evuFM1jBb1TIJbvKRoOFWNCI7aQNrupZBqJPca66m/ppOb0LV/xW3hw3/26X+nakfnpkn3HLJ0c4IdqA1Fd/j5OYD1uDTfNBKZfGnAX9PK64vXpondx6+bjpfSLUcO6DgqLuSlQrQ5F3MwlbeRtPcoTQMpCLpDUjUvZZrWZVk/fjsOdoaa3TFZFeOORRLNqqlB8+FRJMM5uz4wTdTZJZeETDclXG+WpSuVQxJGOJ/ieThPAzY/gpK1kuzUeLS6dCuAgrzARCOpOxe8dNdV3Y8j3xdRlZddGa712/fVgS7XK+uA6gEJIiYV0LWu25z+mD/KW239Tc1gVgUucD32MqT8GTSZBLTV8lEubJadslr5ux5vd7OxrfG9T0eHnK8qyTuJNY6ZsW/Zg3jppwn7u+EDKXU2vGO0BI8hbmdtEuLdZAH5Rh5ee/hTEP4ITH7vyj7UI/zK+/n7pxorPvNlAkZAv+O0mD4RGyhuFgsEFK0WMGNcQUvPuWDoOXXVxr7C1Jrp5fmc2GorLhBMK+Z9T6mihI4pBVRxo3nmsGWRzsibRxB6bAhNGoSMBohQIWStrC486B+HnqdngSMGNjjyq1Kj1VRnSdyOpmsvTLU/YK/wu9I1/XDLW79hF93eK+ppapQbQOtk0zcYSMusqaUPGoT/A9uRb7Pk8QhCvEFqQZV6y0/NRkZv/V5Ro8Wzh3gVslwfvcI+vUwaKvRQyi0hSA3SbbTfyzpLYZYCvYl8n6fGY+J9vzlciAP+rCATlFHMTuNH3ew3AyVu0L6902qIwy+glb4stIa88KsHZhHROtE7i1h4B+/sIN/MLQSufNaqkPFdMKspDip6D2h4ECEAXmqWqVnOPpqAs1bGLOHeyoOuvk2lpVGo+SebOXKMGHW lMoCl67O 1neJgrggGyzS+mDtj0ujN9K8gczHfX9oFaqCrRws1lCEfrAqr2620R6CnY9W/fpDbUF7DEXEGYB//nH4Urf2RZA8Kv1+5ixX1r8fCBeapmM4OZuYXC5Lrzpm0/Q61gyUB4S3WcuzmSU43vnd/HmTOPapuckUfwuDZgExVVhbK+Qa0+4Hkuo62/lp1BUqQ+zDMURxNsquQiaOmngbKMK8O36R96stVWFF05IyqUYTuMyEBUwGYQQzTBzOI77oXkKS2qZsBS9ZidCX4LwvIYKQqKOI/lKqEiXX6fyiDzLx32vYa0r1/TXcv7xi82o8id9VzHMuF38VgsUtpFCclwjUAHmRUx+9JKRxGF9eGt4zMMOZT1WjjEJ9h/H701vcJrVM2g9u44IU6zbRDl1LN7V2CIDYyfkWcxzQmoToH1GTZhMkSB70x61R4Mrz/N4nWadWmnJqBsHv8IRu2wnKsgykiKuL/Ba0YoedG2vAbA1xU/Yb/Jkr2u2R6OzmzMhDFYnwcWGupJgMaWbEptFA= 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: Use broadcast TLB invalidation for kernel addresses when available. Remove the need to send IPIs for kernel TLB flushes. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/mm/tlb.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 58c8a6a94e65..54a118b22e59 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1077,6 +1077,28 @@ void flush_tlb_all(void) on_each_cpu(do_flush_tlb_all, NULL, 1); } +static bool broadcast_kernel_range_flush(struct flush_tlb_info *info) +{ + unsigned long addr; + unsigned long nr; + + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return false; + + if (info->end == TLB_FLUSH_ALL) { + invlpgb_flush_all(); + return true; + } + + for (addr = info->start; addr < info->end; addr += nr << PAGE_SHIFT) { + nr = min((info->end - addr) >> PAGE_SHIFT, invlpgb_count_max); + nr = max(nr, 1); /* Round up if the last page is partial. */ + invlpgb_flush_addr_nosync(addr, nr); + } + tlbsync(); + return true; +} + static void do_kernel_range_flush(void *info) { struct flush_tlb_info *f = info; @@ -1096,7 +1118,9 @@ void flush_tlb_kernel_range(unsigned long start, unsigned long end) info = get_flush_tlb_info(NULL, start, end, PAGE_SHIFT, false, TLB_GENERATION_INVALID); - if (info->end == TLB_FLUSH_ALL) + if (broadcast_kernel_range_flush(info)) + ; /* Fall through. */ + else if (info->end == TLB_FLUSH_ALL) on_each_cpu(do_flush_tlb_all, NULL, 1); else on_each_cpu(do_kernel_range_flush, info, 1); From patchwork Tue Feb 11 21:08:02 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: 13970693 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 200BFC0219B for ; Tue, 11 Feb 2025 21:09:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C5F86B0088; Tue, 11 Feb 2025 16:09:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9440A6B008A; Tue, 11 Feb 2025 16:09:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7ED4F6B0092; Tue, 11 Feb 2025 16:09:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5A2966B0085 for ; Tue, 11 Feb 2025 16:09:05 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0CD9A4A6AD for ; Tue, 11 Feb 2025 21:09:05 +0000 (UTC) X-FDA: 83108903850.15.DF0543B Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf20.hostedemail.com (Postfix) with ESMTP id 702491C000A for ; Tue, 11 Feb 2025 21:09:03 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; spf=pass (imf20.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=1739308143; 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=g+hApyUzLn1llTtRX7GGfjcLtS0C5r3Mv1JciDqV8o8=; b=bS/iaha1SpQ14Y5vuKfLMePd2TOl7W0J5t1BM6aceMBDoRNKZ/1Su7a6gCC7wWeKz1UGkn G5cVude62MOm71Ez1WjwbXW1+n1L9XEO7D3vNJRE3gjHl0TZ+AhLPtks/tr8+BFxnQXOH0 c7B2BKVKUXjRmqK9XChypV55oLC3rtg= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; spf=pass (imf20.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=1739308143; a=rsa-sha256; cv=none; b=HY+UWH+83p6TYZCw45YL6jlhfwUls7k4QHiitjBwX7WkLzf6zeWrJmyHG0nbOsNbLcCcuf 9nXFUugq709F8VKkXY5QsefBmT7x9F4/GlkiAuv9cNBZeDH4krxwC1CXX4EAD8eh9JkGXg QSTj0wby1oAp6rYpcPnRuicx1Nhlo4M= 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-36py; 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 07/12] x86/mm: use INVLPGB in flush_tlb_all Date: Tue, 11 Feb 2025 16:08:02 -0500 Message-ID: <20250211210823.242681-8-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-Rspamd-Queue-Id: 702491C000A X-Stat-Signature: o4jj9ab9m1byugb5p719e5bzuq6xfwjw X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1739308143-175248 X-HE-Meta: U2FsdGVkX18cByEtktbBCMndSPS24WvlMRNXJ8P0odPQrnadmiGxKnJtUfvfMZaPOIYttnJlbgc6mdeWYmY2MRFR9TPHTj05IBTBCVRop74RF73SdfUauHoPZOnqzD+xQIbkYApJbfiHxSQiMmtfxFQI1ofo2bGYx+mxJm3JoLBfVl2UFu4gPF2tg+kNfgNtUa5kSzexbqAkvZ+CrWtowwCcRW3xrs08bE/fAkYNbiIGOwzlRFhZJml2CslHmT5qxuAMDHOb0oKCA4yv5FfxzFTtgfFkohWzcnZJbK/9pod3j5Tc9/hEuae8HXj57lPv0IuLuYbqe5t4B3s7zyVrOKJ8ohACJDJ1ZeKKJ/UGZ6+Dj89agoaXmTzxSw2MTYXzEr0MdTFfCo+bXA+p6gckmSNaJgHUJo3HRk/I2N+x54cVAMZEd3gTRkysLnFLJYqoqmhaeFt0zyD/EqyOPDIxBhXr9BfLXxH/NmqIIeP/WoxShpjk3S08coHJPJrz2AcArBKNDuFioSCY+a+/ibmIt43XKlOZBgQVarpuB/hrPj1jAbs+mLQNUU7Sw3LveWnS2LSCnKvlfvuxOOkVcVearkmmOJgECjantmZIJzTePconfhzTLyF2iR3PnbCUZiMGS5PnlxfmMZe3KMnSGrLIBU59iwlCR+56GwfDUeMWnOiWwcWGZqmVUEdsj3vrfn7/Av7aqOqMD8Ra64Fe/C2/24uE1F0m1AhsTulZM4K35eyzZuDj9vzrY4N1kNBtyi5ARr377LvLZVehVY52PrgMT8RXQ78WGcSoYQh0CKucHfT3rRuMMdPRl6q/IonQR5nyueUVkj7HkYqpVuQgdLMfmCgHIqnbDm4Q+GhZNIG1GdPFJ0u98MVWRaB+gjPwnRVm2Acj0Ffjgm5OD3WOwAC0ZYi60TpJyRiaf60kcupn2q/bUul7I/LvBHQ9J2lJgr+JdQ5fFGXcBoXNEft4adb VRu0nbfT m2qNWat3yYxcfCJSNX6itFUtvbPldVVx1d/dgCaygs/QgCXuHrsySC2vuXDqyLOMS+4YfvIGYW5X0zGWWekx0ZufNY1T35O0Qo0sjRgI600AlPpZX/GawjhqxKj4IbW1l1d6Unz+goNX/IpZX9nU9gWsFsHNAWrQ+6mNxZKaGLnPf7gYJTUWrkvbUel3z7jRS2lN4b8miOZQqgqdVN+Yb1qY0hC3q2LotvE8ifOTeUyhnhoG6Zo/wjWmlSxyjJWhThgO5vI2TjNHG/Ewfsgp3UHgnbhK0QjluoCjdCNtgZrrUh0jIhig1tTPJEZZcMN6OtKea3pdOTSocLrhZ0if1DZHHCEBgyl7TCtQIrduHhoKS1VivgFMwOhfG2NC/7CmnpVsZODO1SWCW6c/+u1xITD8/Aofm2pZiV8mvRIG6X3SgBgKkT82O2F9O/ytq5uXe8bEap+v2pdoEYrz8Z8OGMmbfSuva9f6nx90ATkYPXYnY0JCpsJ6H50B8iFopaeigyqOOuGYXtVWhNC+nthOjgsfDYB+pqp9MTpIYIoUxiWrA0PBidk6OE+etwsZEzoO3BPu04zlWdhGcPMHRbTTUULmy5w== 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: The flush_tlb_all() function is not used a whole lot, but we might as well use broadcast TLB flushing there, too. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/mm/tlb.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 54a118b22e59..62b4eaf4e851 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1065,6 +1065,16 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, } +static bool broadcast_flush_tlb_all(void) +{ + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return false; + + guard(preempt)(); + invlpgb_flush_all(); + return true; +} + static void do_flush_tlb_all(void *info) { count_vm_tlb_event(NR_TLB_REMOTE_FLUSH_RECEIVED); @@ -1073,6 +1083,8 @@ static void do_flush_tlb_all(void *info) void flush_tlb_all(void) { + if (broadcast_flush_tlb_all()) + return; count_vm_tlb_event(NR_TLB_REMOTE_FLUSH); on_each_cpu(do_flush_tlb_all, NULL, 1); } From patchwork Tue Feb 11 21:08:03 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: 13970694 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 361A4C0219D for ; Tue, 11 Feb 2025 21:09:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C3AE16B0085; Tue, 11 Feb 2025 16:09:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BB2526B0093; Tue, 11 Feb 2025 16:09:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9B6D86B0085; Tue, 11 Feb 2025 16:09:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6F9A66B0088 for ; Tue, 11 Feb 2025 16:09:05 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 08A31140279 for ; Tue, 11 Feb 2025 21:09:05 +0000 (UTC) X-FDA: 83108903850.17.0CC513C Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf24.hostedemail.com (Postfix) with ESMTP id 7C06218000D for ; Tue, 11 Feb 2025 21:09:03 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.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=1739308143; a=rsa-sha256; cv=none; b=q05bT9A4p8ZUOioXAvYtz1f/OJYj8enVF12WSyweeONNWszY1GoQHprJRZItxTlrlF3TCY e/HonW4qk5PhUKw809JPNROvyuzQrnFgmHISXNx32SkxqZ6z1K000q/tV6+YW0FX5Rj+Ar 4lkc16HMdp4VT1qXOa6xF44nRdTL6P8= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.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=1739308143; 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=YwhRUul0Ead0E8FstHFdRR/pLtGJxHIOsTVrzdrugr4=; b=dgupN0TWFCYsH1vaBSDTu7GFmYadsEyqGbAPGttot32CcSxhJKjw1GPR+2Fbz/COHjgJup skEuSJvB0on+290aJh58JY61CCtarclNwm8tZ/nWv6Yc1GFm+XROToGjNQZfEuQVa5s/R2 tjfUVMYBMNTbJN2BV2Ddsz8Dw2cYlpE= 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-3BaY; 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 08/12] x86/mm: use broadcast TLB flushing for page reclaim TLB flushing Date: Tue, 11 Feb 2025 16:08:03 -0500 Message-ID: <20250211210823.242681-9-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-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 7C06218000D X-Stat-Signature: ikfucfjmox856tx1ekha1ptpeckugsfb X-Rspam-User: X-HE-Tag: 1739308143-757165 X-HE-Meta: U2FsdGVkX19lDrGL8VX385O4JjmlqBZhCyfLtgH/fQTNZmpTBMg3Mm5GnJf6wIzo7etpgZQ5ADVY9NCI6PIejxgqokRXdkE6OMpFzW91dwvMghsA493geR5LGwu1x1utmlr/y9stDtzGYrI+4LtYaz5HYLDnqLTlEIWnSg9pD573HFjec1Zhsq5VdTOS/4XgVWVIsOIw5aAcf6pIgBYseX4rCU+S+XBjqmkmYDK3wq2HwV2j55S1SF22fS4F5FakSXF/l1cQUe69b21QKk9YPLgMb7Vsj96V8FDc5HMkLdJtL6r2iPsmdzlAvnI8CHorKcQ/HkPv+hlWC98pgaUgk8mgMhBwzUcuOg7h1INzNH+ZPThjt/kuynDXR8MUpvOneAh5KdqKyfbkwlbr/91fDrRs0cDX5ZP/g++TZj03onPTKjC8i0Ffv3Xftgj39JvDZ58inFCE5t0yDBfQuiLQRXckm2Pog33Yi2QNreOC1aZJryWRDg+ah+kxdhY15Oe+oY5FyZFr/Q3Y9atiwxxhijmQgI+/ZQeH6ifChVi7s/UBevZ98W5okxZWu2mEDiSt9p06OyOwOXKfcKleDch/qGCmhJXkKv3shU8xvQjkbCAIF1NXbFwZo66jhYVNwOqEPO0QSQO4UahQST2K5bn6HALSVloDpHHTk7n4yO6auWVyoRshLb1e4CdG0anQfzFGz+mF2qS4jJgwJbUgpcvOInuG4X03vRRvsQk8LGEWr+B3hXxTQix7hyPfhU+ImpL2iDGhZ9MIbUJCo795CVfq/yd5LkrRdlRzbcm3EqhTywDSRX6yYjje+JNipArgu0qNDHI3gvh5q18uRhQIOu+E7lqc/HmZs2T190lJDPnRPpUqMeDUDF6ZJaUhQW/iV2vcCIt9WYY+21PZPOhAlHHECvGxG0SVxd8wZTZT5ybqGDvIkRCnqywiRUdKNQhzdoMmUsvjYOhY6rkf5VSM1dj RHQ2wZ4e w8pJjC2k0W0TmtYGGSkwMHAnvrKeZnlyABdNTwQ78wbkyR4g9ARgDHiSd+8/UiocSDJiRpos49/I3OTDj9DOMH4UK8KD5/1ImJhzajK56H5hU6PjPtcAgCX9bIvhmC9FTL5psmtmF0pQgMiLZ9kSUWh/y3+ViBITy/xa0UIClAU3/K4cL4t07Yabtty6aEg9/EJ049I+31fy7rNjcO0GhQKCDK57idTFeSvfsXiCy+Dy+8fxO4X1avSiq2AgLEZIChhz0lr3e+CCkYfZ8d9zIfSAdMuJfP7wZAI/X1WefSBvu6VyXw1PDeDLolfdIiN38mNguKVapbjzM2rsY8NkUF/PHbTb0Ney2YeNKHsl8+gJvLohQaXsB8w61uOV9koCbSBgpxroUxMPeUGLz2crVB2WD5nYVaRudiSJwawRJq/q9DBHmSQOQ94484o2cIRfSJDcYM6dy0kZbWoWHonB/An/lhP6H7iPigHLZWNvskHEsPcCBkP2ucJMgbVKnaVXgj0FptMLy2C31v0g= 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: In the page reclaim code, we only track the CPU(s) where the TLB needs to be flushed, rather than all the individual mappings that may be getting invalidated. Use broadcast TLB flushing when that is available. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/mm/tlb.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 62b4eaf4e851..55c922083aa2 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1316,7 +1316,9 @@ 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 (cpumask_any_but(&batch->cpumask, cpu) < nr_cpu_ids) { + if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) { + invlpgb_flush_all_nonglobals(); + } else 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(); From patchwork Tue Feb 11 21:08:04 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: 13970700 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 EC515C0219B for ; Tue, 11 Feb 2025 21:09:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B53726B0098; Tue, 11 Feb 2025 16:09:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 76F796B0093; Tue, 11 Feb 2025 16:09:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 378246B0099; Tue, 11 Feb 2025 16:09:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E86436B0096 for ; Tue, 11 Feb 2025 16:09:07 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id AB157140ADC for ; Tue, 11 Feb 2025 21:09:07 +0000 (UTC) X-FDA: 83108903934.07.FACC402 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf22.hostedemail.com (Postfix) with ESMTP id 192E6C0002 for ; Tue, 11 Feb 2025 21:09:05 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf22.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=1739308146; 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=SpyXQVGBdZWh+hl6LJ48H/njwFp9ohY0WuRxbIxuKcs=; b=bp0HSf2fAn2XAR13fUrzSf0UD7g+UBS4kGkrDs0HWfGauMie3AKirDkvh40Ud5UsHXvtSs sEHljH2J1AlgROtyhlrumhksWUkH2qLEuZ6ADWqUCUqW7mXGoyXp5uWmrKZ8xReBD1Iw48 uSwMaP9fjBgi0jhvjV/Zl7tdA7hB7JI= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf22.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=1739308146; a=rsa-sha256; cv=none; b=cy3dKK4gxXI6+PyxphD4VNDi2xaW0EjQy/eXFwOcQNCz1Za47O8SO74xGJoZ0Eplu2toQF uMTmURY29a+Ks4OJYULni1DsDXQC9Sfl3EWqHTN6sw7y6CGeefUKEkluZF+STNu2JI/DXL DtArjgLyTyad4rVGfoS+Wg3aMjxdF34= 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-3Gkh; 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 09/12] x86/mm: enable broadcast TLB invalidation for multi-threaded processes Date: Tue, 11 Feb 2025 16:08:04 -0500 Message-ID: <20250211210823.242681-10-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-Server: rspam08 X-Rspamd-Queue-Id: 192E6C0002 X-Stat-Signature: 5gbzhf31ntngz5tbydqgo15pccr9bb4j X-HE-Tag: 1739308145-923043 X-HE-Meta: U2FsdGVkX1/bSgB7P54SVeIeTr8QNRCoxpErVTS27xzSKliDiuQGgQ4WA+IyEslg8MmM56utPANAKa2lqXt4ModJWGIeQHP08KZKnFANanOGVMMKc4YALeKWF4ruHx5/IOKwtTyxyD1m9rE7sz+CKeNx/g+5mV2kd3UjRH/45eFPad2e6sslwbb6ZMj/WhGN+O1Q1J16RbGF1KZjwBfAR697FZtX6+K/GJbUYOxs2YVCj2Y2ykUdDNrNX94rTWNcEA2bXdmq2wQMKJw/T76FcSs7TmEb42bUVYxWotFC8890GzWkyqHMNKGmXlt3VX/+MNRTKAYEqJAiLbDmMJ1pTKy9li26R/GmOYJctYahQIT16GBwZQVpaKsd4tcyKxVX2bXT1rDqh8GR5sF2mParqlmpLZ12+Tw5GVqvGXVzSM3Yu8WIAifEQ6vMM+sB/DLDBRl3ttie7egMyntHhDSeaymjHPlg9JS7oxFX5FRvNf4h0Z9LYNxHsd37FM0NeZgpEAe9QvWbQ/UTrc8Fi1tiYGElAlfVSq93BeFXlLpMD6u4FfKlqBEu1Xy6o8HTBYG3tdPkpZ3nM+oJOPhtBIACTFIbDUDrBk8KbJqDABrsXC3GcrdrHg2/BjFm1KVkYNp+HagZZnvH61TuMgmTjIWehq4nl6SvZF1o5Q8Z8NbNylQpg5xoQdHa6kwvS5gNCmNzkR41xp8QfPCgg4rDrm3NTfWX5OykfDnummH03oxngsmegiDg3js2LY1ct1KM7dq1f71s2sPlAqvdPIYBmOV0XjPbzkOQwRREM+qOZXYP7IKcU2/AeUybEai9E5RUema+/yrpXMY+b+M8UTR9OOMqAgIwIUH+FXXZNLTD/dnaEE0cYoEIeB8aM+wlGAVgyW1vb1foL3WGPzVhrJm8YN1L1q0SnoNLA0zCfVEwAkR4/wEvhVlToIZR/mg7Po9HdUTZuuD8DwGilDVz6F251Gr /7yQCwYo WzDAfSCUJqFhvIeJ5iE1c8/j8JevUjdd0ueA+2ONsprFWpRU+wHvtDhTwY1h8lhHDwR3QZMMmPWCBNvQm3YAikhk1XIC6jN1nSNlaT1vlsi46A5ZmLT2nzS0tgCgBvZj414ehBHXLO9/Aw5etgyllg0Sfi4S1aJqwjkvgeu1NnEBrYb5xRSzu91h7hI/5wbX6XT36Vg2RHzEBgy5/FrxrlaqpD3AftivPaOKaCRiDY48ACC2Gsa529k/97oQlE3TyRrEVdmQQh036SDBuwm0+W1AmuIB77PVV/FCJDC+xNhBYGhsGYlMFIQf5r7Ul21fzP0A2POcWsYCu6wsa5IpmLwdgv3+C9rNX4H9jzLq8oO3FhanmxmzeooMBkxQPLgGI4dZvUSxXIxKL/kG3ben8uMLE3KGjGYn/8wcMt45pUa2TpLiANkykV/CO1Pvw7g5zPkkuMsMALEUN5jICzHRdeYpJBS1KMRPggzDYNQZxsk8W5VCRGN4iCTujRhdUuQkSh9jKf/3A0BFyzz8= 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: Use broadcast TLB invalidation, using the INVPLGB instruction, on AMD EPYC 3 and newer CPUs. In order to not exhaust PCID space, and keep TLB flushes local for single threaded processes, we only hand out broadcast ASIDs to processes active on 4 or more CPUs. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman Reviewed-by: Nadav Amit > --- arch/x86/include/asm/mmu.h | 6 + arch/x86/include/asm/mmu_context.h | 14 ++ arch/x86/include/asm/tlbflush.h | 73 ++++++ arch/x86/mm/tlb.c | 348 ++++++++++++++++++++++++++++- 4 files changed, 429 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/mmu.h b/arch/x86/include/asm/mmu.h index 3b496cdcb74b..d71cd599fec4 100644 --- a/arch/x86/include/asm/mmu.h +++ b/arch/x86/include/asm/mmu.h @@ -69,6 +69,12 @@ typedef struct { u16 pkey_allocation_map; s16 execute_only_pkey; #endif + +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH + u16 global_asid; + bool asid_transition; +#endif + } mm_context_t; #define INIT_MM_CONTEXT(mm) \ diff --git a/arch/x86/include/asm/mmu_context.h b/arch/x86/include/asm/mmu_context.h index 795fdd53bd0a..d670699d32c2 100644 --- a/arch/x86/include/asm/mmu_context.h +++ b/arch/x86/include/asm/mmu_context.h @@ -139,6 +139,8 @@ static inline void mm_reset_untag_mask(struct mm_struct *mm) #define enter_lazy_tlb enter_lazy_tlb extern void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk); +extern void destroy_context_free_global_asid(struct mm_struct *mm); + /* * Init a new mm. Used on mm copies, like at fork() * and on mm's that are brand-new, like at execve(). @@ -161,6 +163,14 @@ static inline int init_new_context(struct task_struct *tsk, mm->context.execute_only_pkey = -1; } #endif + +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH + if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) { + mm->context.global_asid = 0; + mm->context.asid_transition = false; + } +#endif + mm_reset_untag_mask(mm); init_new_context_ldt(mm); return 0; @@ -170,6 +180,10 @@ static inline int init_new_context(struct task_struct *tsk, static inline void destroy_context(struct mm_struct *mm) { destroy_context_ldt(mm); +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH + if (cpu_feature_enabled(X86_FEATURE_INVLPGB)) + destroy_context_free_global_asid(mm); +#endif } extern void switch_mm(struct mm_struct *prev, struct mm_struct *next, diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index dba5caa4a9f4..234277a5ef89 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -239,6 +240,78 @@ void flush_tlb_one_kernel(unsigned long addr); void flush_tlb_multi(const struct cpumask *cpumask, const struct flush_tlb_info *info); +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH +static inline bool is_dyn_asid(u16 asid) +{ + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return true; + + return asid < TLB_NR_DYN_ASIDS; +} + +static inline bool is_global_asid(u16 asid) +{ + return !is_dyn_asid(asid); +} + +static inline bool in_asid_transition(struct mm_struct *mm) +{ + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return false; + + return mm && READ_ONCE(mm->context.asid_transition); +} + +static inline u16 mm_global_asid(struct mm_struct *mm) +{ + u16 asid; + + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return 0; + + asid = smp_load_acquire(&mm->context.global_asid); + + /* mm->context.global_asid is either 0, or a global ASID */ + VM_WARN_ON_ONCE(asid && is_dyn_asid(asid)); + + return asid; +} +#else +static inline bool is_dyn_asid(u16 asid) +{ + return true; +} + +static inline bool is_global_asid(u16 asid) +{ + return false; +} + +static inline bool in_asid_transition(struct mm_struct *mm) +{ + return false; +} + +static inline u16 mm_global_asid(struct mm_struct *mm) +{ + return 0; +} + +static inline bool needs_global_asid_reload(struct mm_struct *next, u16 prev_asid) +{ + return false; +} + +static inline void broadcast_tlb_flush(struct flush_tlb_info *info) +{ + VM_WARN_ON_ONCE(1); +} + +static inline void consider_global_asid(struct mm_struct *mm) +{ +} +#endif + #ifdef CONFIG_PARAVIRT #include #endif diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 55c922083aa2..f3c0d8fafc57 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -74,13 +74,15 @@ * use different names for each of them: * * ASID - [0, TLB_NR_DYN_ASIDS-1] - * the canonical identifier for an mm + * the canonical identifier for an mm, dynamically allocated on each CPU + * [TLB_NR_DYN_ASIDS, MAX_ASID_AVAILABLE-1] + * the canonical, global identifier for an mm, identical across all CPUs * - * kPCID - [1, TLB_NR_DYN_ASIDS] + * kPCID - [1, MAX_ASID_AVAILABLE] * the value we write into the PCID part of CR3; corresponds to the * ASID+1, because PCID 0 is special. * - * uPCID - [2048 + 1, 2048 + TLB_NR_DYN_ASIDS] + * uPCID - [2048 + 1, 2048 + MAX_ASID_AVAILABLE] * for KPTI each mm has two address spaces and thus needs two * PCID values, but we can still do with a single ASID denomination * for each mm. Corresponds to kPCID + 2048. @@ -225,6 +227,20 @@ static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, return; } + /* + * TLB consistency for global ASIDs is maintained with broadcast TLB + * flushing. The TLB is never outdated, and does not need flushing. + */ + if (static_cpu_has(X86_FEATURE_INVLPGB)) { + u16 global_asid = mm_global_asid(next); + + if (global_asid) { + *new_asid = global_asid; + *need_flush = false; + return; + } + } + if (this_cpu_read(cpu_tlbstate.invalidate_other)) clear_asid_other(); @@ -251,6 +267,276 @@ static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, *need_flush = true; } +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH +/* + * Logic for broadcast TLB invalidation. + */ +static DEFINE_RAW_SPINLOCK(global_asid_lock); +static u16 last_global_asid = MAX_ASID_AVAILABLE; +static DECLARE_BITMAP(global_asid_used, MAX_ASID_AVAILABLE) = { 0 }; +static DECLARE_BITMAP(global_asid_freed, MAX_ASID_AVAILABLE) = { 0 }; +static int global_asid_available = MAX_ASID_AVAILABLE - TLB_NR_DYN_ASIDS - 1; + +static void reset_global_asid_space(void) +{ + lockdep_assert_held(&global_asid_lock); + + /* + * A global TLB flush guarantees that any stale entries from + * previously freed global ASIDs get flushed from the TLB + * everywhere, making these global ASIDs safe to reuse. + */ + invlpgb_flush_all_nonglobals(); + + /* + * Clear all the previously freed global ASIDs from the + * broadcast_asid_used bitmap, now that the global TLB flush + * has made them actually available for re-use. + */ + bitmap_andnot(global_asid_used, global_asid_used, + global_asid_freed, MAX_ASID_AVAILABLE); + bitmap_clear(global_asid_freed, 0, MAX_ASID_AVAILABLE); + + /* + * ASIDs 0-TLB_NR_DYN_ASIDS are used for CPU-local ASID + * assignments, for tasks doing IPI based TLB shootdowns. + * Restart the search from the start of the global ASID space. + */ + last_global_asid = TLB_NR_DYN_ASIDS; +} + +static u16 get_global_asid(void) +{ + + u16 asid; + + lockdep_assert_held(&global_asid_lock); + + /* The previous allocated ASID is at the top of the address space. */ + if (last_global_asid >= MAX_ASID_AVAILABLE - 1) + reset_global_asid_space(); + + asid = find_next_zero_bit(global_asid_used, MAX_ASID_AVAILABLE, last_global_asid); + + if (asid >= MAX_ASID_AVAILABLE) { + /* This should never happen. */ + VM_WARN_ONCE(1, "Unable to allocate global ASID despite %d available\n", + global_asid_available); + return 0; + } + + /* Claim this global ASID. */ + __set_bit(asid, global_asid_used); + last_global_asid = asid; + global_asid_available--; + return asid; +} + +/* + * Returns true if the mm is transitioning from a CPU-local ASID to a global + * (INVLPGB) ASID, or the other way around. + */ +static bool needs_global_asid_reload(struct mm_struct *next, u16 prev_asid) +{ + u16 global_asid = mm_global_asid(next); + + /* Process is transitioning to a global ASID */ + if (global_asid && prev_asid != global_asid) + return true; + + /* Transition from global->local ASID does not currently happen. */ + if (!global_asid && is_global_asid(prev_asid)) + return true; + + return false; +} + +void destroy_context_free_global_asid(struct mm_struct *mm) +{ + if (!mm->context.global_asid) + return; + + guard(raw_spinlock_irqsave)(&global_asid_lock); + + /* The global ASID can be re-used only after flush at wrap-around. */ + __set_bit(mm->context.global_asid, global_asid_freed); + + mm->context.global_asid = 0; + global_asid_available++; +} + +/* + * Check whether a process is currently active on more than "threshold" CPUs. + * This is a cheap estimation on whether or not it may make sense to assign + * a global ASID to this process, and use broadcast TLB invalidation. + */ +static bool mm_active_cpus_exceeds(struct mm_struct *mm, int threshold) +{ + int count = 0; + int cpu; + + /* This quick check should eliminate most single threaded programs. */ + if (cpumask_weight(mm_cpumask(mm)) <= threshold) + return false; + + /* Slower check to make sure. */ + for_each_cpu(cpu, mm_cpumask(mm)) { + /* Skip the CPUs that aren't really running this process. */ + if (per_cpu(cpu_tlbstate.loaded_mm, cpu) != mm) + continue; + + if (per_cpu(cpu_tlbstate_shared.is_lazy, cpu)) + continue; + + if (++count > threshold) + return true; + } + return false; +} + +/* + * Assign a global ASID to the current process, protecting against + * races between multiple threads in the process. + */ +static void use_global_asid(struct mm_struct *mm) +{ + u16 asid; + + guard(raw_spinlock_irqsave)(&global_asid_lock); + + /* This process is already using broadcast TLB invalidation. */ + if (READ_ONCE(mm->context.global_asid)) + return; + + /* The last global ASID was consumed while waiting for the lock. */ + if (!READ_ONCE(global_asid_available)) { + VM_WARN_ONCE(1, "Ran out of global ASIDs\n"); + return; + } + + asid = get_global_asid(); + if (!asid) + return; + + /* + * Notably flush_tlb_mm_range() -> broadcast_tlb_flush() -> + * finish_asid_transition() needs to observe asid_transition = true + * once it observes global_asid. + */ + mm->context.asid_transition = true; + smp_store_release(&mm->context.global_asid, asid); +} + +/* + * x86 has 4k ASIDs (2k when compiled with KPTI), but the largest + * x86 systems have over 8k CPUs. Because of this potential ASID + * shortage, global ASIDs are handed out to processes that have + * frequent TLB flushes and are active on 4 or more CPUs simultaneously. + */ +static void consider_global_asid(struct mm_struct *mm) +{ + if (!static_cpu_has(X86_FEATURE_INVLPGB)) + return; + + /* Check every once in a while. */ + if ((current->pid & 0x1f) != (jiffies & 0x1f)) + return; + + if (!READ_ONCE(global_asid_available)) + return; + + /* + * Assign a global ASID if the process is active on + * 4 or more CPUs simultaneously. + */ + if (mm_active_cpus_exceeds(mm, 3)) + use_global_asid(mm); +} + +static void finish_asid_transition(struct flush_tlb_info *info) +{ + struct mm_struct *mm = info->mm; + int bc_asid = mm_global_asid(mm); + int cpu; + + if (!READ_ONCE(mm->context.asid_transition)) + return; + + for_each_cpu(cpu, mm_cpumask(mm)) { + /* + * The remote CPU is context switching. Wait for that to + * finish, to catch the unlikely case of it switching to + * the target mm with an out of date ASID. + */ + while (READ_ONCE(per_cpu(cpu_tlbstate.loaded_mm, cpu)) == LOADED_MM_SWITCHING) + cpu_relax(); + + if (READ_ONCE(per_cpu(cpu_tlbstate.loaded_mm, cpu)) != mm) + continue; + + /* + * If at least one CPU is not using the global ASID yet, + * send a TLB flush IPI. The IPI should cause stragglers + * to transition soon. + * + * This can race with the CPU switching to another task; + * that results in a (harmless) extra IPI. + */ + if (READ_ONCE(per_cpu(cpu_tlbstate.loaded_mm_asid, cpu)) != bc_asid) { + flush_tlb_multi(mm_cpumask(info->mm), info); + return; + } + } + + /* All the CPUs running this process are using the global ASID. */ + WRITE_ONCE(mm->context.asid_transition, false); +} + +static void broadcast_tlb_flush(struct flush_tlb_info *info) +{ + bool pmd = info->stride_shift == PMD_SHIFT; + unsigned long maxnr = invlpgb_count_max; + unsigned long asid = info->mm->context.global_asid; + unsigned long addr = info->start; + unsigned long nr; + + /* Flushing multiple pages at once is not supported with 1GB pages. */ + if (info->stride_shift > PMD_SHIFT) + maxnr = 1; + + /* + * TLB flushes with INVLPGB are kicked off asynchronously. + * The inc_mm_tlb_gen() guarantees page table updates are done + * before these TLB flushes happen. + */ + if (info->end == TLB_FLUSH_ALL) { + invlpgb_flush_single_pcid_nosync(kern_pcid(asid)); + /* Do any CPUs supporting INVLPGB need PTI? */ + if (static_cpu_has(X86_FEATURE_PTI)) + invlpgb_flush_single_pcid_nosync(user_pcid(asid)); + } else do { + /* + * Calculate how many pages can be flushed at once; if the + * remainder of the range is less than one page, flush one. + */ + nr = min(maxnr, (info->end - addr) >> info->stride_shift); + nr = max(nr, 1); + + invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd); + /* Do any CPUs supporting INVLPGB need PTI? */ + if (static_cpu_has(X86_FEATURE_PTI)) + invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd); + + addr += nr << info->stride_shift; + } while (addr < info->end); + + finish_asid_transition(info); + + /* Wait for the INVLPGBs kicked off above to finish. */ + tlbsync(); +} +#endif /* CONFIG_X86_BROADCAST_TLB_FLUSH */ + /* * Given an ASID, flush the corresponding user ASID. We can delay this * until the next time we switch to it. @@ -556,8 +842,9 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, */ if (prev == next) { /* Not actually switching mm's */ - VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) != - next->context.ctx_id); + VM_WARN_ON(is_dyn_asid(prev_asid) && + this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) != + next->context.ctx_id); /* * If this races with another thread that enables lam, 'new_lam' @@ -573,6 +860,23 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, !cpumask_test_cpu(cpu, mm_cpumask(next)))) cpumask_set_cpu(cpu, mm_cpumask(next)); + /* + * Check if the current mm is transitioning to a new ASID. + */ + if (needs_global_asid_reload(next, prev_asid)) { + next_tlb_gen = atomic64_read(&next->context.tlb_gen); + + choose_new_asid(next, next_tlb_gen, &new_asid, &need_flush); + goto reload_tlb; + } + + /* + * Broadcast TLB invalidation keeps this PCID up to date + * all the time. + */ + if (is_global_asid(prev_asid)) + return; + /* * If the CPU is not in lazy TLB mode, we are just switching * from one thread in a process to another thread in the same @@ -606,6 +910,13 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, */ cond_mitigation(tsk); + /* + * Let nmi_uaccess_okay() and finish_asid_transition() + * know that we're changing CR3. + */ + this_cpu_write(cpu_tlbstate.loaded_mm, LOADED_MM_SWITCHING); + barrier(); + /* * Leave this CPU in prev's mm_cpumask. Atomic writes to * mm_cpumask can be expensive under contention. The CPU @@ -620,14 +931,12 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, next_tlb_gen = atomic64_read(&next->context.tlb_gen); choose_new_asid(next, next_tlb_gen, &new_asid, &need_flush); - - /* Let nmi_uaccess_okay() know that we're changing CR3. */ - this_cpu_write(cpu_tlbstate.loaded_mm, LOADED_MM_SWITCHING); - barrier(); } +reload_tlb: new_lam = mm_lam_cr3_mask(next); if (need_flush) { + VM_WARN_ON_ONCE(is_global_asid(new_asid)); this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id); this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen); load_new_mm_cr3(next->pgd, new_asid, new_lam, true); @@ -746,7 +1055,7 @@ static void flush_tlb_func(void *info) const struct flush_tlb_info *f = info; struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm); u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid); - u64 local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen); + u64 local_tlb_gen; bool local = smp_processor_id() == f->initiating_cpu; unsigned long nr_invalidate = 0; u64 mm_tlb_gen; @@ -769,6 +1078,16 @@ static void flush_tlb_func(void *info) if (unlikely(loaded_mm == &init_mm)) return; + /* Reload the ASID if transitioning into or out of a global ASID */ + if (needs_global_asid_reload(loaded_mm, loaded_mm_asid)) { + switch_mm_irqs_off(NULL, loaded_mm, NULL); + loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid); + } + + /* Broadcast ASIDs are always kept up to date with INVLPGB. */ + if (is_global_asid(loaded_mm_asid)) + return; + VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].ctx_id) != loaded_mm->context.ctx_id); @@ -786,6 +1105,8 @@ static void flush_tlb_func(void *info) return; } + local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen); + if (unlikely(f->new_tlb_gen != TLB_GENERATION_INVALID && f->new_tlb_gen <= local_tlb_gen)) { /* @@ -953,7 +1274,7 @@ STATIC_NOPV void native_flush_tlb_multi(const struct cpumask *cpumask, * up on the new contents of what used to be page tables, while * doing a speculative memory access. */ - if (info->freed_tables) + if (info->freed_tables || 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, @@ -1049,9 +1370,12 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, * a local TLB flush is needed. Optimize this use-case by calling * flush_tlb_func_local() directly in this case. */ - if (cpumask_any_but(mm_cpumask(mm), cpu) < nr_cpu_ids) { + if (mm_global_asid(mm)) { + broadcast_tlb_flush(info); + } else if (cpumask_any_but(mm_cpumask(mm), cpu) < nr_cpu_ids) { info->trim_cpumask = should_trim_cpumask(mm); flush_tlb_multi(mm_cpumask(mm), info); + consider_global_asid(mm); } else if (mm == this_cpu_read(cpu_tlbstate.loaded_mm)) { lockdep_assert_irqs_enabled(); local_irq_disable(); 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 From patchwork Tue Feb 11 21:08:06 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: 13970703 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 80DBFC0219D for ; Tue, 11 Feb 2025 21:09:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5751F280006; Tue, 11 Feb 2025 16:09:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4FCA8280004; Tue, 11 Feb 2025 16:09:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2E734280006; Tue, 11 Feb 2025 16:09:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 02EB0280004 for ; Tue, 11 Feb 2025 16:09:09 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B5CB9B137C for ; Tue, 11 Feb 2025 21:09:09 +0000 (UTC) X-FDA: 83108904018.26.1538276 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf10.hostedemail.com (Postfix) with ESMTP id 3600DC0013 for ; Tue, 11 Feb 2025 21:09:08 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=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; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739308148; 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=5jyuLSMzlUYDWRskKJb6Xc0QSC8f69gZO1sgMf59NT0=; b=f91Bklavo9O1S/7c9JZ/490iNKOVEShZpSFbP+7v9+5VWfTC544ANGBVZqLlHFehmwXUj7 DDpWQyaPE/dmfkFsxtRu8gpfS3nYLphC8B6m1v+ksjeTHCbj84D0r+N9aTsAarfe2f2XuV C8fzeA+JQ0T/MiaxI7+U7omn8f5bXmI= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=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; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739308148; a=rsa-sha256; cv=none; b=xHIJYPWeQwz0IfivfW/KJSUYHBbQIMmW7eCOVZaRDsTvXaxVQDS/8WTdCU2IoqMh+shBVa 2F5ZaQdLCOx0hzn8HzqMHmcjawqtiH9y9Oh6LpLoLtuDtfCp95vFd/BxaG0KNeVMJ5abQt tFNTCR0916ISIyXAe+qhEbdfxc1YTqo= 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-3Re4; 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 11/12] x86/mm: enable AMD translation cache extensions Date: Tue, 11 Feb 2025 16:08:06 -0500 Message-ID: <20250211210823.242681-12-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-Server: rspam11 X-Rspamd-Queue-Id: 3600DC0013 X-Stat-Signature: k8wihdqopebgwp9ettnrn6at7o7czpg9 X-HE-Tag: 1739308148-849864 X-HE-Meta: U2FsdGVkX18+PcaWUgCa1ddWRwsMSdY+luQ9WYHuLVdzvJqyxdzPOUZOI5nYCu0nZdUyeYsO6UsejOzsDa4vW0nAggEOr53CRvReMEuDklxUXFcORqhp3NVIP0fj+fT1OENnUTpCJNxoIwA5HBFGoFyEaTV7jjZNI1CXTs9mTVyeWlHJEgLl/WNuiTLImoRtZLCjOHUBjqUqSGmjFELazOM6EihV0dWYkZXUj1LAoOFLcEZpw0exBWac3C4rYqrvmCths+zmY2PML68ZcW9oBmOhdWtRBDGeKhQgLLdPK/FudUb/8p5avmNQQTYnPkR8kCX4fMs7kVqT0JW17XAUAGjVgVAtf7TGOCtoJff+TwkRrP5FVCK6z0Q/Bl6Vhz9A1cHnpVibQbfbgIKd7yqaAJhKuasgo5TgNQKO92JY/bnxVx0fyrT+9rGLRC1oMiD7kFS2LP65H+rWKpdXC9x7D9tboXQLIZ2nEhQFmt8oPUF6viYyc3vHpWoLKw5WS8szRTdn2+X4K+fHn5pcu550e/wHuPIOidrvg5cBl9NfzHn633tJqFQAJtAAqSRiViV9OdWWxfxfY8NUhajnupdACvYTaak7npaMVxq8F64Waj8wb7jSXeSsQTH67AbAQqh3/0fWGQWnQyx8iWSKniGaylUd9nI0lZipgxXSlqrLWoWohwWkH6jn/bQs40r1+xg0YsariwkbunPplKLdmlz5naNN+l9HZlwLJDlKSEKlzLvLl6ukcj2Vn9wyWesziGOGHy3sXFbyIRqjyrQ6sKDoi2XP8g+jYcfyRI42Wkx3s/28ie3h8RsrpWbCbZH50XxxfdI7LhaF6ha5KpXBXz/+GsE9gwPTkecf6etPlycanwWmt65LYjiJLKP91DcDx1yUPNtw0q58u+9AhfGGzMRJ1zK1yJQwl1bKZPyVBEUybLATZjHIfTPZqT70HLBR2NF3Xi6MZg2AdSzSvauALkY RPlQR2EN yDfVQjYfB+Z3oWxfiJCSk9vwxInKYETjypZpQsXZH2aXnIq+M6RRoMNKgh308d0yQ9xp0coMt9E2B8glgWMqDv0NSPUUH40E5Q5YxM1cIJAzXebvs7uQ1156cspLH1OnTQnIXJDr+wOHpdzYjE+nc7ZmEl3Eo/1AzRoybNlzL2eOufrkSKVQHVx/VoOJkQibtpbQ36KtVT//BWxcMK6oWTsVOVL+7tBXi32WQIhH2mfa7l33MHte2AqZqVmGHGlKLT9nXhpuVEDoz0E/ZD2Jn8Dpe3UXarUwq/xjUoxW78VtXsJ420/XUYy5QJgI0gusjxJ66y+ynXXW5ajxne014rHfrYF47tPcO2q6XQdbR9FE7wgPBORyIJgqyJUupGCgsV1x/uWFNr1urMFBeNvBR1kWfs/vMi4X5eAqLaCn5+xuB9kYQUpJWY+96+xX3T7Rs2/DyZIe1fYfQurFaK5tKEf2Ge1VJAwWU16Xh8Z1p4JaaaTSuLj9xGro8GIrCroIVWNq/b/mDYoMxtZE= 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: With AMD TCE (translation cache extensions) only the intermediate mappings that cover the address range zapped by INVLPG / INVLPGB get invalidated, rather than all intermediate mappings getting zapped at every TLB invalidation. This can help reduce the TLB miss rate, by keeping more intermediate mappings in the cache. From the AMD manual: Translation Cache Extension (TCE) Bit. Bit 15, read/write. Setting this bit to 1 changes how the INVLPG, INVLPGB, and INVPCID instructions operate on TLB entries. When this bit is 0, these instructions remove the target PTE from the TLB as well as all upper-level table entries that are cached in the TLB, whether or not they are associated with the target PTE. When this bit is set, these instructions will remove the target PTE and only those upper-level entries that lead to the target PTE in the page table hierarchy, leaving unrelated upper-level entries intact. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/kernel/cpu/amd.c | 4 ++++ tools/arch/x86/include/asm/msr-index.h | 2 ++ 3 files changed, 8 insertions(+) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index 3ae84c3b8e6d..dc1c1057f26e 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -25,6 +25,7 @@ #define _EFER_SVME 12 /* Enable virtualization */ #define _EFER_LMSLE 13 /* Long Mode Segment Limit Enable */ #define _EFER_FFXSR 14 /* Enable Fast FXSAVE/FXRSTOR */ +#define _EFER_TCE 15 /* Enable Translation Cache Extensions */ #define _EFER_AUTOIBRS 21 /* Enable Automatic IBRS */ #define EFER_SCE (1<<_EFER_SCE) @@ -34,6 +35,7 @@ #define EFER_SVME (1<<_EFER_SVME) #define EFER_LMSLE (1<<_EFER_LMSLE) #define EFER_FFXSR (1<<_EFER_FFXSR) +#define EFER_TCE (1<<_EFER_TCE) #define EFER_AUTOIBRS (1<<_EFER_AUTOIBRS) /* diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index bcf73775b4f8..21076252a491 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -1071,6 +1071,10 @@ static void init_amd(struct cpuinfo_x86 *c) /* AMD CPUs don't need fencing after x2APIC/TSC_DEADLINE MSR writes. */ clear_cpu_cap(c, X86_FEATURE_APIC_MSRS_FENCE); + + /* Enable Translation Cache Extension */ + if (cpu_feature_enabled(X86_FEATURE_TCE)) + msr_set_bit(MSR_EFER, _EFER_TCE); } #ifdef CONFIG_X86_32 diff --git a/tools/arch/x86/include/asm/msr-index.h b/tools/arch/x86/include/asm/msr-index.h index 3ae84c3b8e6d..dc1c1057f26e 100644 --- a/tools/arch/x86/include/asm/msr-index.h +++ b/tools/arch/x86/include/asm/msr-index.h @@ -25,6 +25,7 @@ #define _EFER_SVME 12 /* Enable virtualization */ #define _EFER_LMSLE 13 /* Long Mode Segment Limit Enable */ #define _EFER_FFXSR 14 /* Enable Fast FXSAVE/FXRSTOR */ +#define _EFER_TCE 15 /* Enable Translation Cache Extensions */ #define _EFER_AUTOIBRS 21 /* Enable Automatic IBRS */ #define EFER_SCE (1<<_EFER_SCE) @@ -34,6 +35,7 @@ #define EFER_SVME (1<<_EFER_SVME) #define EFER_LMSLE (1<<_EFER_LMSLE) #define EFER_FFXSR (1<<_EFER_FFXSR) +#define EFER_TCE (1<<_EFER_TCE) #define EFER_AUTOIBRS (1<<_EFER_AUTOIBRS) /* From patchwork Tue Feb 11 21:08:07 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: 13970702 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 103F4C0219B for ; Tue, 11 Feb 2025 21:09:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 54D66280005; Tue, 11 Feb 2025 16:09:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D828280004; Tue, 11 Feb 2025 16:09:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2054F280006; Tue, 11 Feb 2025 16:09:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id ED852280004 for ; Tue, 11 Feb 2025 16:09:08 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A7D19140ABF for ; Tue, 11 Feb 2025 21:09:08 +0000 (UTC) X-FDA: 83108903976.21.91373CD Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf12.hostedemail.com (Postfix) with ESMTP id 1F70640003 for ; Tue, 11 Feb 2025 21:09:06 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; spf=pass (imf12.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=1739308147; 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=3DNTI2DQKwqgHBk5qLmMNVCGYTRMRarISEI9fPhmoWI=; b=pdYybMvgg3y+TNbYT/UHpPnPmTX1knlNSXXtsTjK5qpR6Ek+HYlvkzVIIwCqOl1K58SomT EF9ibTTR2kWM7KIpsGom4l6ywmHcMI5bsHy3+fmsYLBntB0REnTakicgLGMgprx8JDIZk7 UhZQ0XwA5Eg5L0YTQYv4opRTMBBFPYw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; spf=pass (imf12.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=1739308147; a=rsa-sha256; cv=none; b=Ik/UKSEl9dd28pNf9r7sy5Ax+5egqxcBs+5xvL7mEijZf0iF1h4FWmownltOBWYvDlIe5R LKEGs00S+fXillFMwYEm8yYoR6kX1jC1KZaJgmGKUQPMaOXvSTjiY4FvORcO+xSN2d09zX yPZXgNTOaibLhuZXa9GPWNRPFqckP0Q= 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-3Y2i; 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 12/12] x86/mm: only invalidate final translations with INVLPGB Date: Tue, 11 Feb 2025 16:08:07 -0500 Message-ID: <20250211210823.242681-13-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-Server: rspam11 X-Rspamd-Queue-Id: 1F70640003 X-Stat-Signature: yhbig3nset9k53zjy6843k3osa4b85w5 X-HE-Tag: 1739308146-438987 X-HE-Meta: U2FsdGVkX187MAozRRkWuzpQM4PfY+SkGc3agO075Mqj9Kp97WpdGWaYLirUk25KcSQp79zO5hl6GR44CJXVSdSgrOK/Gn3Db7df6Qbs6zz2xWW1DB+CAbAlazrIHTzzHY4yFJ3igeW053Al/HjortEvREK7Rv0xW2t2SP+V0B6a7vrVEH6w57BU6ADPSEuYug56Ho1bDjE8PSlD4FVnW9tq4QZk9ELb/3sbQdeWfpxTyRd0YFlkrXgVG/Tqxvv3KpgXSc6UfIV38JusfFVPV7RRG1opfxPhCr0WSJRRWcbpQFWUqjfD0gjBfislksESCZRra95F1m1BY3jGTSJQl0S3oT85ibu2h007S6GJiMtsYV38GJgbifOTYw6NMPUy4FqkbcZLoLz/75WfGPAIk7dMg5l/NhW+XRJCCNbVqH9GSwctT8jcsIOtEM5WvHBXN8rMuATKCzsAfirSfxIaXHjPxcw+WCpt/L6ecMWH+VnjwgkOVmhc7fOo3LwQa/DVR35u9SoEHa/7mBWIYVf+gAk5cxlEeGmlsX88C/8KqvmTaI4Mhcin9P7Rk6XsSI0lztvnTcG0kyBggMU68aA0BcKwho7rk2Hvg8NAg+SzvtxK8MuYOPeM32E9mAM4yU2Pz9zYMbjVfiD7ojhUqRqcAhKnNUPRb5LGrffXftQ1JDq4aCtSsCkSgPgNpXVTfKA9lbGNTOuYmcy1JVG1khU8tr+P7yqulbM2KAmzolTJ7B2CmMXKbdYrh72V7zHwBcXyMaT0Y9w2ODVtHKQTnWrKG9FiAz5HpQma/n3fBcEYJjK/vMiN76yuH+aMnyuRrcYjIzxiLylD0A8/4jtaZxqSIh/Cr+Z8MiV25VkXeGbVQrhTByUJHu4dZS/XU+XlvngI+K8OZkDdsmR3o/yi0Gf7VlPwrBUiObD/Ydo8Nt3h1o5Lfbi4BJwjanN2wJvvj95wFfXZU8r9DoB9/Oowm07 B95YZh1O Jrx5O2igullgomzX5ZDQJhfTeNU9Wuvv7S7MZROhDLTOMdtevWtMG6rR0MdMm6+wpRxDXyjxjGx+/cSnOFDN4nyKa2fvvh3AQ/ibjadUMxRmE3IAes8OcQpBN379EF+2vY/SSoLvddj+GXmTXc4rJSnqO0iALJfK17aT3Uik6+xfYDJcXyLktW7BFfcAI+lQYY/OvOE21t66KxRwggbfv/0KoVaYrUyNrY2J7FF7JknHOs0X0qP9wLA22Ls1X0NPB6o6OUlPmyouZHjPMdY29B5sq+qArBl1MDjxevVk5LK+mHab+hwnNIZkVx41MCNv9MvgUG71OUBEHsiwjBAIInCi7UIdc+MxgwNtL4mKhTxh9FMHsRRQmXmsNHtdlVrEg2uSyizl7KI5iE4sp95kkRYCbnPvxF+123ySGb5YTqgL8mTvvvGkcmBQnwImLDCwFwRvvTEhK5i2ua0YNqBgYj2q0IkZk8xBkXxiq9isTTmfa/GpUp7GvmOjtYm+hrBXL6LlMZbrbXMv1rpY= 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: Use the INVLPGB_FINAL_ONLY flag when invalidating mappings with INVPLGB. This way only leaf mappings get removed from the TLB, leaving intermediate translations cached. On the (rare) occasions where we free page tables we do a full flush, ensuring intermediate translations get flushed from the TLB. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman --- arch/x86/include/asm/invlpgb.h | 10 ++++++++-- arch/x86/mm/tlb.c | 13 +++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h index 43c331507cc0..220aba708b72 100644 --- a/arch/x86/include/asm/invlpgb.h +++ b/arch/x86/include/asm/invlpgb.h @@ -67,9 +67,15 @@ static inline void invlpgb_flush_user(unsigned long pcid, static inline void __invlpgb_flush_user_nr_nosync(unsigned long pcid, unsigned long addr, u16 nr, - bool pmd_stride) + bool pmd_stride, + bool freed_tables) { - __invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA); + u8 flags = INVLPGB_PCID | INVLPGB_VA; + + if (!freed_tables) + flags |= INVLPGB_FINAL_ONLY; + + __invlpgb(0, pcid, addr, nr - 1, pmd_stride, flags); } /* Flush all mappings for a given PCID, not including globals. */ diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 41ac2d121d76..bef0811dfa27 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -502,9 +502,10 @@ static inline void tlbsync(void) static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, unsigned long addr, - u16 nr, bool pmd_stride) + u16 nr, bool pmd_stride, + bool freed_tables) { - __invlpgb_flush_user_nr_nosync(pcid, addr, nr, pmd_stride); + __invlpgb_flush_user_nr_nosync(pcid, addr, nr, pmd_stride, freed_tables); if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) this_cpu_write(cpu_tlbstate.need_tlbsync, true); } @@ -553,10 +554,10 @@ static void broadcast_tlb_flush(struct flush_tlb_info *info) nr = min(maxnr, (info->end - addr) >> info->stride_shift); nr = max(nr, 1); - invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd); + invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd, info->freed_tables); /* Do any CPUs supporting INVLPGB need PTI? */ if (static_cpu_has(X86_FEATURE_PTI)) - invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd); + invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd, info->freed_tables); addr += nr << info->stride_shift; } while (addr < info->end); @@ -1704,10 +1705,10 @@ void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, u16 asid = mm_global_asid(mm); if (asid) { - invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false); + invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false, 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); + invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false, false); /* * Some CPUs might still be using a local ASID for this