From patchwork Thu Jan 23 04:23:20 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: 13947930 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 9AE5EC02182 for ; Thu, 23 Jan 2025 04:27:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 364116B008A; Wed, 22 Jan 2025 23:27:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2ECA26B0093; Wed, 22 Jan 2025 23:27:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 18DAC280002; Wed, 22 Jan 2025 23:27:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id EAE7E6B008A for ; Wed, 22 Jan 2025 23:27:44 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 81FBD1C7EA1 for ; Thu, 23 Jan 2025 04:27:44 +0000 (UTC) X-FDA: 83037433248.22.7EEAEE6 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf29.hostedemail.com (Postfix) with ESMTP id F3262120008 for ; Thu, 23 Jan 2025 04:27:42 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf29.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=1737606463; 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=oq4XWnrfEGNzde2AdVIgZ/kCkzfSi4nq7uDv8bgjwV0=; b=EOkATDb/AYRWT4DEAlPALk5aTx6pUBZMkUZxwsHKIO2Q0h2FkQ9STZRVMrCRRbqxXauadA GoezamK8nsw48VHinaxl2S4xvObDDWjVD5/y0hLgcMmma0Dp6P9SDa4aCswnRYwcdt8l3I tF+fDTBQKapfI6fYhoJ/9da+wLoN1uM= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf29.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=1737606463; a=rsa-sha256; cv=none; b=QgXKLi1vp3ve9sewHXasc1+m07gYmhS7DnTc0Nb9xj2BfkfyrTmBBUMoxBfq+t6grA4hti DdOGytjBaFEaYxVFnbkaGIx6ujEk6VoIqojcIDgJYnXsK2qVEb3I5xAnjyj9B+5ueZ5GSh 4mMpURJrIdo2cxN5/pBoOC+QC66gjys= 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 1taols-000000005uH-2VQQ; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 01/12] x86/mm: make MMU_GATHER_RCU_TABLE_FREE unconditional Date: Wed, 22 Jan 2025 23:23:20 -0500 Message-ID: <20250123042447.2259648-2-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: F3262120008 X-Stat-Signature: cmn6edjuc84g5qmddqr4tq8cwhhntyqj X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737606462-581854 X-HE-Meta: U2FsdGVkX1975Nz33HGT3gUFRKSsG6N06ish/fCzLyy9JEkCiHTj5ntNCqEO1LxMS7/R9cdhooVTRLwJF5XNpR9N9jaMGsVjOYKuzqOKt0DJKxil+2IHSyUFgYPgsGME++WGa++ofzwbpGpBPCN6uWRFvVKXg1kt15vAMdKrsegM9qfRFDV8+E4EkS8G6AXSqqkS0mW8olmXTxpP0BGsFFte2YQjcjuVa/JscBrlye50htXO+aGfj+airU+mSWvdPKyeWeGeEBdz+cMrtor7bhmtDNy+yPKBAUiJenkBex5VkU3nGJZlJcuAYlYj4ZxHiOeJyTdmYzc1skWB7oir8PTG0si2cf12HBDuYfwNG+D2XUaicAMtyZhqJxSJmHIGrtBLNxBW9EBLxJBkvyYSOfyt2ntq0B1vMCX5D0VYrmy1x38j5HgnwawILrIiBjM+ta9FeYRfEZVqD1PFxZAtcS6ww407Bpx7Bc48buUy489Z3j30NDObndV1AObTTTKhr6+ZytVi0OxlcucjgWUAOKCgRJpTh+XFPfDlER3v84VHVe7af5lfMMoIJmxvx4X9AbfSApXcSaX67kI4H/E1iQO8Ad0p4d9EP9u0OCLOOyrOX13j/+KUblWdQNYeDNj7YABidwM7KiWfDJoQjGe4cgMfTQgtURckXnHSSFcAz0GjzlkhNwJw6fWzwO4XovozSo7ovzmo49LIHldDu3GJGtw4ox+frG4GSE2rVz+AbqREvf4EkzjN8Ky7Im5YR9G0QfuZkXaY/QyAvYMyKjGfj+ny9acalkxy94jAYqNaeOTJZUF6xhAAtnVXPDmYfTtwEweIaRP6HnO5W1bczXayTrcyF6gUTnu8mKIM617GrlzX0xzaKgKvEn/OO4l3TpWOJNIhoLQUSq94GfM8JMpb45R5iBw/P2NLG7xn9tlmwX/aWtJNKu8HdIduo5t3mAvZDdGfsoPciKkfEymdDfz gP9t/u6e wBdCO1K1pnuD5CIma70v13ziZDhTjmpIXCqr5Ak06J3PSYhWwr5QD9V3P7hpOR9/9KxPeyRWsLcm2AwEceKGeZ3BSa89MkVA3gfbiH5Zfsx8HCcA8X7bbxTWrIuo/NNsYa6daGdKxOJAALvy5w9AFkaU/MHwLbjuUcFz/d2zS4uTOVv0vZEf0MMm/YeQ75K3TbSEioHm6AZPtxHMhuf+bnM2sZl4VfQL/fnsX4IDPVSQ4wv0L5S3uknS+PPIHgwwjlJXuMlMoCTvFcbuu3vB59yECU1eMGSD74qbFRpEeU0o6e3jmTEVQRaxpBrJSML55G8FAHabSqCPS5iB9STNtBqi+T08g09WukQ7Sb0pelJKx9D3lRZDGKN4bJJsof12BKxFbKp4NLpOXOVve6zQUiZA3OQyQekPdCpHf65jswyPzm3gSOJWRNvoPKDAU/sq+B+3srzGHjtS4LoSG9SAGcIFzLx3I/01XSTjoWmWSlsDxFVE= 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 --- arch/x86/Kconfig | 2 +- arch/x86/kernel/paravirt.c | 7 +------ arch/x86/mm/pgtable.c | 16 ++++------------ 3 files changed, 6 insertions(+), 19 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 */ From patchwork Thu Jan 23 04:23:21 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: 13947927 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 A60C0C0218B for ; Thu, 23 Jan 2025 04:26:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 18DB66B0085; Wed, 22 Jan 2025 23:26:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 118586B0089; Wed, 22 Jan 2025 23:26:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED314280004; Wed, 22 Jan 2025 23:26:11 -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 CA3466B0085 for ; Wed, 22 Jan 2025 23:26:11 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 29D43140B2D for ; Thu, 23 Jan 2025 04:26:11 +0000 (UTC) X-FDA: 83037429342.09.FDD267E Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf17.hostedemail.com (Postfix) with ESMTP id 9A65840013 for ; Thu, 23 Jan 2025 04:26:09 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf17.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=1737606369; 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=PnCoYmo7xgeaCQDZxRDZUVSl9IxMjo0b1OkBBli6rlw=; b=rS356gi+xjzMsetIeuUvYS6nF0mex4T06sXvUUH1Az+vd8ZEFRYaSKoPr1RPlCTIrlGjxP D3jB1o61PtuEgzSzo9JRboMu6391GFRGRgU15iGC1xUWnKFivPo3aamNZwIZpjShnRQHcY pwm8SyPg45tU2Es+t19VDn9lwNRYDE0= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf17.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=1737606369; a=rsa-sha256; cv=none; b=TY05/FJaAU3M67TvcSU9/QOLHQSBKxpAgxjI0em6fSnCmpBYaLBGRpARPUfYvFPC+UOkVX ocy05hedpK5bg12RW615ko/RaBhtkTxpYW/ww9vb5NMdnFU4z/9a9PVxc63/D4SqF0C2bT z/ULlm1QPmff8nB9YztyvQjiRo2eCAM= 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 1taols-000000005uH-2ab2; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 02/12] x86/mm: remove pv_ops.mmu.tlb_remove_table call Date: Wed, 22 Jan 2025 23:23:21 -0500 Message-ID: <20250123042447.2259648-3-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 9A65840013 X-Stat-Signature: eukt9dms1xsw15mmbekxodoyhe7i6o4x X-Rspam-User: X-HE-Tag: 1737606369-333463 X-HE-Meta: U2FsdGVkX19zyFdkUTn9Redl2zyF1IyhI0zV3NrKFLIW28ud6i8HjRmMGp/vxNZTaZit60RNjL5BEGa4r0fpm4LDI2oD0p9DuY1k9Bay5NI9lD6pGL2B8DT7XaMU9reill5JD6DUPqCYntvXSTWIT65EH/9uCo6CLAeQC7Pia7O21/rgmwhv7xsfKYfZ55tdUjU24S4B90JxtgVEB144ZUSPj4fZdFF+Y5Afgaoqboz9L+fNeNEp0z6Tlj66uSAs2lA8FuEOBM+IgTwKzJsvUkhzU64QHRPHZRaeSm9pzh6xGjikwKR4uDj3RTwuZ/0e++WLR9jBdF4gMhnr6MLlSqz8Mc2C+ihUEsDgVVS7017DqWi/UKSJGDdsf4BVQltfsYYGdd2j0oSrOpkjjoK8LXTYj7MBO3RG80ctIBBPgeC0okN+JQAAe2461M04ii+h2NZSVralfdVpItcg1O2WqrnVAkgvw2xrCV24J2a0FhbiNoSzSjIVKBTqSsUjP2Qarv3TFXZqjnUXGPFzobCf6Eu4S/llWMbKd3rpxDVOmD91qGWhpQxZTMpyjC3K8vCIIV9yX6yTaEeAtPMwC171ctyOFQHetWG1PshSaZ5o/l9VYQGiTTGNh/inUT5fjpADuBdQhDxxcZpTplPlALI/0hnoD2rrQUwfEa+m1nn4TVisOWntD9kZYiv6YMgqVDKYFMrNB74X0bDIwBM2pSEQmLPrZnLlpgX886ZLIs4C9sj5UVarOdlA/P3vdxFJUkcORubRaFwCwrVAZRpr2LCUhmLtFcAD68b1FPPfU8es1fX+TPJfYpJiTaqZ0yx8c1ZeUjoYbY9+WHJ8X9JdaHOKCIk5D5cFIE2Z/XsqYr3jBr+yYbfHH4cjiNe/8NUKZKmO534knrFF3O6w9x6303s8eXliZ3i3umVmBUhBbFR0INUQmRi2+ZbtkNla4RKCa7WauN910ybfyQXUkerPpb7 3e6sSpnW tf9PKQ7OL+WUcjh86jfJhxl+EMgnrMy5GWzjTgOEBe8yNlKti/7oGN52ZgEu8NPLQlGBu0WdljXiDBjTWjI9leY5Ez74d/aCR4YRYWo82gj1NAzPh87xr1qc3FDVEASd13vC5JWRUaX8H1tsdcybZKuyYunuvbULMqsl5N/BWMN4qybWCfqYKlKyT3hcsqjLiAk1IrPtO7PfIDsxs5M4fSqHo3U0T4lIuhNc3TA4WTTjSSH+J0A0aPG1OD90VsZJVhNxd1N2092fz1AWhp0Fe3vw8QE1F+ypopxbpC9QNB6uQUX3PmKZjHeUiWv+gbcq9rM5t7WwPu6swdzZX+cuqCm/hA0wblfcJxaEoGCjIyUYY4aY4Xd7urDz1yQH/VkCKkOa6WpRRC3sBMhZ/jhp9N5tLQGQ/B4y4iLGtlEJdwZu4fby1y4AIBUQGVpOvoroND07EYSo/JXyRznU= 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 --- 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 Thu Jan 23 04:23:22 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: 13947932 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 D4B19C02182 for ; Thu, 23 Jan 2025 04:28:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 698646B0096; Wed, 22 Jan 2025 23:28:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 622BE6B0098; Wed, 22 Jan 2025 23:28:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C2DD6B0099; Wed, 22 Jan 2025 23:28:40 -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 2CD2F6B0096 for ; Wed, 22 Jan 2025 23:28:40 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id DC14D140B38 for ; Thu, 23 Jan 2025 04:28:39 +0000 (UTC) X-FDA: 83037435558.20.3C5E056 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf24.hostedemail.com (Postfix) with ESMTP id 50EC8180003 for ; Thu, 23 Jan 2025 04:28:38 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=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; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737606518; 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=Jc2TQoSL3j7v6SJaeBe+lsW7ecYjzyMkuVMBWMOAINM=; b=BXl3pTjxizY8eXot+nXQg84mmC58NZsLJt6HuDP9pAkMBJgD4t0a+SLSRX2uiz9PY+r4hf UYd2djrb21sOlSjDYVvGELvefwN7+cCYdhrp7OXbnbBbpOuXTj2vsDrTvQzZ/DjeGdU2Yp zo+efibts/ZoVK6BezKo/bTB3Zo63HI= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=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; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737606518; a=rsa-sha256; cv=none; b=3gywqNr3MVBXmGgCtV6srS8qaHVXNztKjmEIU2VESIbKzPXLwGZ9+JZh9LFJnQJM3zoble OC8C2uc+hS8O19zYahX9r63v5uun5OMjEV/5eryIyK6Sl9imhZG47HijXR5D25+zqsjLX4 xytN4WheOokvrVrNK9gqhgJuaWxzTtc= 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 1taols-000000005uH-2fkG; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel , Dave Hansen Subject: [PATCH v7 03/12] x86/mm: consolidate full flush threshold decision Date: Wed, 22 Jan 2025 23:23:22 -0500 Message-ID: <20250123042447.2259648-4-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 50EC8180003 X-Stat-Signature: pgek5hf1d1h5syoijmc1wm1y9j78w18w X-HE-Tag: 1737606518-11401 X-HE-Meta: U2FsdGVkX1/UpGTxgKd5fC2bzprq+inWXoPbYlpf5tXUoJxtkTn02W0MReFL9PY7dLvjoixnwW9flnsza273WlA4msrfGHKdnfdKase4w6DwL2xmWI9fdvE7dIhoiUp7CYOueDY06LcD4veERQdAzopr89lCeva8v3f8N7Re0FqoSmWvAYj+QSkR3UI5GpVsOI9KbSS/ptH9YoIk1V0Pk3zb2RSBbfRBwyDNG9fTqiwrx/33rsFo2AqWCFAiMAE75e9Hu2KjVDU8QV9JQQYZj8ow7Lh9X/zNFacfZSG3eAVY5LSbfobv2SSxqlgA5PGFD5LCfUK7MjhCvf1ms7N9vD2KItSNpCv9iHUTYPtGD5ONyGjXposU3ojW/wpDm4iWrio2U8glV8K6TSavMARwcCZjhgvLdHojMTN9uIfbk0wa7BhdNEY2O/qUhbgKeDF3AFkSGKC5LO1mKlCluGm5E9RiBn26so0OPsrmVZKS0zPadPT0130GO59T+m2ElaJlt/vlgTor4i9S+TGw6Ef/Pn/OKdcCZu3BIq14I9LaWHqmN9nZg+n2C4AtbBeuCZIo4VCKcI6po9s23Cuw3QdtLLEd2hqXngEY8xgmq7nXNakucvDhp97M/lJapnmHKQ2mWKrqgRKaZ0bVYquzQtV69pHNVbiylpcE+kmqxl0XJ8urLMl1PF0+ksic4NHuPvEz7ywNAdJ+R9Oz1krxsOa5ROf1+OtBP3m3eDfprWYpiJiJH8GF5kPyaCFUisEQhoqg0Rxuh2bno2Q3NrGqjGD2QXgE5JBscKYnFfK/V0Mdpixz+orEDs2gDY4hOucjKjyi+dTAuxYBS8crJSSAc/35Qw4qRpt6TpT2QQhitP8KUkSumKkcmZcyF/ywJ5Q2NOL79Sj0/mTwf3SeuBZXv3ZUPA1sMqUc1+U2Y106U87o69o/5unt7KQJDbX5yIDdeAwy43NAheY+yXbYkuBISzH KhQQs9Sm 1DJM0CBxzDZg3KXO7BDJZw1P8fQwGzhXf58LH77uC730H/fGYw4oVNJO2VwJZwb8NZatVNaTtlokly5Psf7akByuP8gY2bPC+pOE6mmQzqfDEvWJ236bxXz1eP3vI2kTmwoOvUaZiCBEH78CipC5V8iZEp/u00iR7hE0sBhvAWwvP84AtGI7wrzUl5FyBsOfjPzlrb94SjWQV3RgpXGytlYXKnDhG8Kr2Z+L/NaVADRGSFEhQ5M/ShIyLGiPL+0XPUA50AywN7t1VYDDBs2h+xfC4y4f0T6AFuAHBFNrb612TWrLZriE4v3ZhVFwzOyEPn1AtDmx1f6CKO5SeGUbYd6Qd+8UtcfnDzN/raR/CFVVAWIjNKEa5i0kGb1VGxsmxZdgQ3rVr6FK9lA9rvNxW6NB4Yw== 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 | 43 ++++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 6cf881a942bb..4c2feb7259b1 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(); } /* @@ -1276,7 +1273,7 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) int cpu = get_cpu(); - info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, 0, false, + info = get_flush_tlb_info(NULL, 0, TLB_FLUSH_ALL, PAGE_SHIFT, false, TLB_GENERATION_INVALID); /* * flush_tlb_multi() is not optimized for the common case in which only From patchwork Thu Jan 23 04:23:23 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: 13947941 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 98DDAC0218C for ; Thu, 23 Jan 2025 04:30:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BD66280004; Wed, 22 Jan 2025 23:30:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 84719280008; Wed, 22 Jan 2025 23:30:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 70DB8280004; Wed, 22 Jan 2025 23:30:29 -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 47682280008 for ; Wed, 22 Jan 2025 23:30:29 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id ED0CFB093D for ; Thu, 23 Jan 2025 04:30:28 +0000 (UTC) X-FDA: 83037440136.25.1FD4394 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf20.hostedemail.com (Postfix) with ESMTP id 6C7A61C0006 for ; Thu, 23 Jan 2025 04:30:27 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737606627; 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=Be0cFZNtRXMujQiq/YiC9LCSjjT1Aon5w4ZILw0GV5E=; b=f4zxfXNVdyLiumGHP5kdHgfhp/dNJC5+NoEm7rVLgB0e2mAZsmPEL0ENHkK+QGJ31CRhm3 vE/mP5IaEl3jno9XOD4CTotLESCfVm9AnK+LT9JKjTv126mzTOYn7cwUVwYMX2gdIvQ/uF dxLZ46jSp9OdmhJbSy3UynUL2esHxpk= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737606627; a=rsa-sha256; cv=none; b=YnPZ6t/0ffh2SFpRKrGXkHltXh+LUkKV/eIXvBvkUqzRcBnyIACzHrjprF+ZVbj1FAIGWs +j9zJFjUxT+9CWwSnbMXCd+WafAWlqSlXQ2/B+Dja6y5DO2sloVFab0bTC1mJ+99uPUooT /c7NGR9CVFdMniWCRZcz860brg6baHc= 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 1taols-000000005uH-2kyx; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 04/12] x86/mm: get INVLPGB count max from CPUID Date: Wed, 22 Jan 2025 23:23:23 -0500 Message-ID: <20250123042447.2259648-5-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6C7A61C0006 X-Stat-Signature: 7q6atiuzb716w1pe4q8uk9i6emgo8957 X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737606627-79670 X-HE-Meta: U2FsdGVkX191YWbKLjZpyOltf9xSBL/o51ZqATt+stzg7+j2wa+8LvkYFeC2xyoe42VaXCOCXiPYrzC+7VPRMVUpcshNY7j1Ww86Iid0KAbCfgFcMz0ngESUbu1aCx72jttjQfKFTpOR3tQx2r6CH+LEX9ybyTRL+96aaiDWTZZ0iANDCRH4g4I1vtK7kSWu57V2EYuKMft+5ZcOPWl5Rosk4uNBN63FvgM0e4kutEZqYm33WvNDPHYe6DXmBpebJL3UnwgvPxO43jU2/nim7P7xHpvHIFRavXZKNsfzOxnOTOWGxTy1ZeN+oH4xvIKarfd8PZEv/Ob88/yPhJDvyhDIZffV7023FENze/eA+qH5tD8zPZfxLcrpljQzgsWjr4VWHtG1Li2DiJU8xnRp2dxqBJEiyuA8uuePDKcIazkeAZoWWrRLftisDzS5eGdeueZ2C19/feCTRjCbNOg8Db4NToaFLXX6scYQqoRiXlaLV+rmudAAwR4XxyLzkfd+GF5xSSlzbo1htMxDXGBrjl0/o+QRtmeVHM3fZmfeKH44f2lz0R2i6swX6XdU5tP2hw9I8Q5jgiR7dBS9Bl6JwhTkoHLTuBqK8bbMoFwzxiT2ausUBla8WimB5e6iI6Lnqy49cTogsqG8lijthoWSh/RD8lpSFAHSgcZPc397HdwXA5S3CTyRzz+MPYNhxfwelIEXKBDCp91JIiaUooLCG16Hpf8QEog/jgX/ZG5qyVfmlqUhVjwBt2HUUyRrVbG6HXJw8/BfdgPkDMROWIt6L9L5BF9KlVevyWJ8OPnlpKN94VEWcuP+Y9IdKb/MqipwThqBJ1T2wJPlZvjapwtucLMD3hZ+6Cst18ocmYG2ExNbNg5zfc6QPTOKTWxdBfdMjmlr9/pKDgybsMI2ntSS7fUBOux2nXgFRRiXM3gsQrFR+K147jbGD5d3oP53/pv00R2awCEuMLvHz4L+rlX /lSFFGcy FxbW4sFVGZl6sifaYPNvk7zQKP7091OPkMLlfihm8nwJxScFxXkGpWstq0l6sHIVVQ8GXyAX4fqAHW7hX1gS8NVruI+1DUPA9SiyK2IrIxIcc1fvvg46WwON6dW4DAirLM9a5DPNxY3Ati6HgGVZj047R8ovuOLRKCIV2wVV0rlJ91p/SuM677DXGxj6kxAkpndMRcr2rQmrFQzAG8PNC22utUwXZP3yyU7GbTzqxxMhOEJXz07fCYZlWyGoI1/J6dO9Sbj+svwKiB9m6z9scT2hPDQfjjB+PegZtmw2XkFfaxbmaRxIZK2bUZQ6dfhxgOoltnewedvsnzrTSa9tsRbYpUJq4nlR3lxekdSLSo4giEKVCj3NWwpMKVcVma64UQ1EE 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 --- 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 Thu Jan 23 04:23:24 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: 13947940 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 B2F0BC02182 for ; Thu, 23 Jan 2025 04:30:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45246280007; Wed, 22 Jan 2025 23:30:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 40052280004; Wed, 22 Jan 2025 23:30:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C82D280007; Wed, 22 Jan 2025 23:30:29 -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 0BB25280004 for ; Wed, 22 Jan 2025 23:30:29 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 96A58A0B86 for ; Thu, 23 Jan 2025 04:30:28 +0000 (UTC) X-FDA: 83037440136.09.6A22A42 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf17.hostedemail.com (Postfix) with ESMTP id 15D5640004 for ; Thu, 23 Jan 2025 04:30:26 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf17.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=1737606627; 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=7WMYap4DpOjXdXgeMCTpWRI7r70hDYcNVqdkHYtAW+k=; b=mervGdgUcWjj+tC3inYJa7Tv/r73PDl7/yxqK0cuscOZTE3Fhwq207wJHeudC5iPs00V4p 1lc4SxOdUjarJzIWqpfTLMuOJwvpGD6G69nk615L6pEKBYsiJGf2QD7lhUbteNjTJ1nXAj uC3ykxp3e0nVkpb95C0bzXcrI3Vrkko= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737606627; a=rsa-sha256; cv=none; b=4iK+kU8vkWHdTPU10uZC+QOL5OBtt9++8/yaQuvaqYxgdaUnRoKZVm9A77FUMGWxBPpGqy UD/qsYOWOf1md3Z6TmH2rbnVulKqPNlDHHDm7zVue2XwNrjzIr19FOGLib7ROWdAzDIICt kRvpu1/czwsTIMsevHHMtTT+uAbiWf8= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf17.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com 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 1taols-000000005uH-2qDI; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 05/12] x86/mm: add INVLPGB support code Date: Wed, 22 Jan 2025 23:23:24 -0500 Message-ID: <20250123042447.2259648-6-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 15D5640004 X-Stat-Signature: 8gjzfqhnc8kocoztf7gp9msnp6dzj6x3 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737606626-771662 X-HE-Meta: U2FsdGVkX1+/wdjuzTJ9VmGIsgJkR0Gyeg/hSJJNo+/d6PKA93Y7uv2BnNiQR5Uu5MZGXvUhhPB+qBZy7Yp5sprUT4UyztqWhz4VTS43Y3jcUGwq9zpS4n5CwUayWre/ERfNyyF1wwalm61ZvrWg2A0JbmfGw6mCIvGMZC5xMrj4e6iSLTmMioDYwfRXkqOBQ8QlHFUz5CRxk6KGpfOpk+Pqicq9HC4GxQFaL7TlOmih0oR77fqjZmVOluNmosft+lO5+CRENwTtN6s8jwzhsDJQHYgQSNavUZiZfM9CRDN/H1E/Nl3wkr5mcPf8XgLtCiq/5IJLjzNSx4wsZeNh1/awcmYvaxI4bu00IssA8/EcGuUQFSoqZzucJ9nJ26IIc/Y6RnUHP5R7pPQDI45rx7QwwpvUHFbQFIiveA4sS+BZLLXT5gbm6qPaGGZoC/fscpDLsa6S7/lbckQ6BIBN3V0CYXKJEy/qx5wMihwJxOrE0foEqPCB8h731K8T0l3ZR0YUQp8IN77QDioUAUUjN/rMCScHVLh2WmsOjk5XLCzuv00kU5Lo07eV2Tuj/Of5AtvqZRhd5AA8SNNbKldJJNKQ/OaRIHJ6eGek1Cg9ao/ZPD0gCZfLvMHqpQE0xJqjlUr3hm0Yaz1b9e3/4aU/i3rQdt9O/sr+cyNgQdi7YzZ12ZBRsfvXUyID5LW1O7oPtyi3T/lNqUYIwCK3Z43jc2HMHd5o4xOMbqjs/KtbTC7QeeqyQ4XZTS2n6v1Z+iPseM+qRUeA7mso6AH6bv/50za+3Sot/tL2nED/kftr1uD2/nfP7y9o4DevHc6O0zBSBOlM9WVKs3faHD/w+PxsP3d28e+urK5jU62oh/omA5WHi+tZRV4vJvRFqnzx9U++hL36wndfZuze8gAQeBmqQW61pXDVyggVn3saT5f83FrWyO4vI5QITxt+4mKv+NriJWQetnA3M0E64+dXm34 hNJtyvkm uaLnjchvlHEhRq/Aoif1JaAISIhYFQYOJbWNTN2STOmpQ+RLOpLCWiJ1plJ74RipPI5804Go+7BgGibXDAO0euHIZTAuqZm4GeZvJ1fklZhALH2xlivpUSk8tYaAImcTv+tzqwOMCGdVoSp64AyJ977xcYtunhcIjiJ4917kyRSCdc950raCHwNhED4+jBQTcbymPrARhZTF3mh+Nn5l+7MWaUaDOv/faD2zIPj2W8uHyAonxjUO8S6oVVbA8KrWq1Ok9PXlIyohCH1HZQFJGPKMTbzAC0GlRuSSdoCAA1KznKuoVVIA4d/D5YsFJ75JnYIN5og9H++UDl0DLwkVtLs+7rWObPPpGh1HSH8XHnjKV8OVMuurgSv/1ICyapPD3i6bm 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 --- arch/x86/include/asm/invlpgb.h | 101 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/tlbflush.h | 1 + 2 files changed, 102 insertions(+) create mode 100644 arch/x86/include/asm/invlpgb.h 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 Thu Jan 23 04:23:25 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: 13947942 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 14458C02182 for ; Thu, 23 Jan 2025 04:30:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 86F2D28000B; Wed, 22 Jan 2025 23:30:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 81FC428000A; Wed, 22 Jan 2025 23:30:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6E80928000B; Wed, 22 Jan 2025 23:30:56 -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 4C6F828000A for ; Wed, 22 Jan 2025 23:30:56 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 00BE2120B77 for ; Thu, 23 Jan 2025 04:30:55 +0000 (UTC) X-FDA: 83037441312.19.666B12A Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf30.hostedemail.com (Postfix) with ESMTP id 6861A8000A for ; Thu, 23 Jan 2025 04:30:54 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf30.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=1737606654; 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=g42cjHb2aIEThCXX+573g6QlgGX+6rdSWUpGjtlNPxo=; b=bRbzZ9trvnTJCCGgkskGeBQ7HmjHMVoStgYG2Ibadw3G6iuXF9OKYypqJ1drlELBLu/rmu Q5zujL0lXQIXGFPsHkH9cLprPtkENM+71HUsCOgSzQiu9YDjM+g6TOYQCZSgLkL7/H+wKf Rzu3w6/4EFkfzK3kfl06/3mNZ8/Ixjw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737606654; a=rsa-sha256; cv=none; b=IJ8juLfgxUnfCD9knZbqU02jtkiZgHpwntXF2WGcg1rroMCYQWragDVU4dYhJSISA/VnqG 15/y7SVE0/ME/G5/QkmTsINFOoobOcKIAaK7ucCSfnu555hbCl8Yw+RAGDDODLm++GXBua z7pkdv8BzFCJSqDhHKFV6A4l51Rwjg4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf30.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com 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 1taols-000000005uH-2wHI; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 06/12] x86/mm: use INVLPGB for kernel TLB flushes Date: Wed, 22 Jan 2025 23:23:25 -0500 Message-ID: <20250123042447.2259648-7-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6861A8000A X-Stat-Signature: ky49m58zh6mfpjsk468tig8yzfp41nbf X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737606654-854434 X-HE-Meta: U2FsdGVkX1/rq3iZUzelVh5cKjt9NOTqXOSq+qV7CF3dvC4ji85FKHy+guCFevnJ+/Vrgs4TIbTyBn+AoVB+ErnkjB8TzBpqMgMiXukNv2zDcBVE72E/4kq+5Vs8zuUdav/nruROaEXUI521S0d3yI0puIvp1fk5S9N1/5SB6Yil16VgaMPB+9iNbLzV0Perdzfqc4YoSOoRe+CF1IyRqnZgPHQi0p6eR6eZQIfv+IjqBXa4qouk9Evo0cKY00GiRSI7k4bQmTQL/UIOt7XzN/VsWDVmxxuImwF+Y5E5sbQk0wfo+2rWRyg5p043fffZFScLSimUh8dyepqCHkYlBHu1DdoQfVn4rX113kg90GbuSHfkS33gyEVTkAeTTCHs1K+DYDSc94RQ2siWCZbrmUEdPvkiQ4txiUVyf1xrNua0mA2d+PawJVlGjRjW2eLwGHyoXPw3gUvuqCtQ/oEqWhugYUxqKSAsGDKFwD/kTCx20bEWVhtbYhV6kqSI7zLm7mkHLO2M4BTr7ZgOpBLEV/skaSdqJvo2xJH9l84D8xvBmZ5c/SB9jAXdxmvzhfEgafN/v6QiQz+AY9gkcpcl0hrVfuJ+0dtDAkpmnPBckqE9POnS+OjLcfzHCNv245+GGPrfzinBmKM0dpZPVxMS/VgKyk7HGevpVHuRYEEXtfnZUQqCoJKdYTtGy4wLxczeLWRN44GHunBWQLUq7jLVdYC9DmCSSpH8WUZSze/ij4ymjID8l+iyxWV3hAqXbAKQxzSBZf5RlvUDY+nedvLqTBXbLUK6AuUzyiIAb6u6NhXUQ81WW8cwTymph68ugyO/VOFAgafT8PGnX6hctQqDrLRbeNYuFnt4FJCDd2OBJCMT3Y4+ZK8o+YqzF6gD4YjL2H+4xuM3HjkpzerJHgOM7uEAPddfTT6LiAabiyim/Nwmw7lIBWNpxlYSyhWS9p6UleZyThsFQbXrGFqnXUO uZisjIgp nZ8MRDnSK5zk4/qQl++T8dD0nxtv0TyqIudj2UcCq6ThxraHYArWybQ+mcoNxxOn9epRTdCoE1gMA/pvPp1kNnFTpINsapWt53b/jmKByechsgR0uke8Lc9UBNi2EeadD6kBT1V4z6zd3IeUAa2/QFYf4JlrC43eMkqKOqvGT8gfmAgmRohW/GrlpOjhzNEXvVVcm+aOM+ALAgzoh4BslqfDA+Q20BbpdWxp0ZNWj6Q/APfboILWxQEfXLBBLlftW2nR4JG1IkAJ7KOng3qx4yVp2LNfI5bVfKgeyr6M8n3zuRdFx88habamN5Iw8Q3GNmezQDg915QbGCepGmbrDpTRacqc+9NxsApGjG4jNhSfgBNdTxylykHqrSHY9BU007HRL 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 --- arch/x86/mm/tlb.c | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 4c2feb7259b1..2c9e9b7482dd 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1077,6 +1077,30 @@ 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 (!IS_ENABLED(CONFIG_X86_BROADCAST_TLB_FLUSH)) + return false; + + 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); + 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 +1120,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 Thu Jan 23 04:23:26 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: 13947929 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 05A9DC02182 for ; Thu, 23 Jan 2025 04:27:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 908966B008C; Wed, 22 Jan 2025 23:27:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 892CC280004; Wed, 22 Jan 2025 23:27:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7325F280002; Wed, 22 Jan 2025 23:27:15 -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 570356B008C for ; Wed, 22 Jan 2025 23:27:15 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 0E715B0944 for ; Thu, 23 Jan 2025 04:27:15 +0000 (UTC) X-FDA: 83037432030.25.A605B5E Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf04.hostedemail.com (Postfix) with ESMTP id 8644540005 for ; Thu, 23 Jan 2025 04:27:13 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737606433; a=rsa-sha256; cv=none; b=qnixcHs5sbEm2QuKeXsRhtOOtdExV7wKIYnrTKvcJDYrGR7vvJbHfalf0e8hyj34q6KTt9 WYytHYqgc09yU7DdYYTG6AwUIPtHFaUwraXnHrLL/3rX7+m8krNs8i/vntlJQR4yQmYEyZ SIcg/qva1v+MSmgbLSnNoLrOsgKLnrk= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737606433; 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=FUVHLo8ZQw9HuI2d8HGefUGZmMCOMsVfRQUVMierrfw=; b=Ug+ZbysQtrtvWM8etI21iGbj9yPKSXWKuZ3r+4ZT3K/pszRrWhmWYViHhUC8rMHbHt6qQn RGgjhsbOcPYfrS+KYSUCfnIzyiRp7xbYqG4Sqr5HmLXtoiZgfeMFKwTVVGSkHPfhhsnyII VP5Ei/Mb9eWPjR4la+RCICDUVnpbm+M= 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 1taols-000000005uH-31yY; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 07/12] x86/mm: use INVLPGB in flush_tlb_all Date: Wed, 22 Jan 2025 23:23:26 -0500 Message-ID: <20250123042447.2259648-8-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 8644540005 X-Rspamd-Server: rspam10 X-Stat-Signature: npr8exinsqog5t3ybwj8ddiqrazi1mg6 X-HE-Tag: 1737606433-281975 X-HE-Meta: U2FsdGVkX1+SZDHI3jkV1vR9eMc/Saz0U2X3eFnSaUwsQssqxEN3xHYzd967PUksqk2HLB0vgDF+f1Cnluu+3ndxo8NPVvc/ozxTF2O0SGM4ysKQk32maA/Cu5rQo3nkDvSiP4itHKjZtG3v7oD2FT7ACkmsIp3eHU4XM1osICvafuTyJ88cHmAvBF2VMv2JRcVfBBRnTox3YoVOz0qJYbszRjqLji9A3k2fR4OuIYaxu6A7fLMYMfpZ9UuhLcbp1LNLoALC4p5ayYs1NJL3RMdJtPQmpGJBbyQddlIKRr4IWCPyWF/FsrqoVClFGpvAUS6LSS6vgM1TkASrMDLQhYku2SaEvgXVuf33yAcWpa1IMgb/T5rqrRPIe5QTbkL8JcqcFQLzdzq8k5RvxQfVLAZ5FaKeGAGF1lPUzWqFGA4JPBqDMawJ1y7R+XnXKY+bI5OIcN25CIUZiZlhPl9m5p4+BbplfNoMEc6RWev+VwhMB5U+gh1sYRX9uxR6w4bVizVo5vSvmIvl6PkVcISnSu5Tl1aDErWgfSUaO/FvIFWdo+oe7AeaSyXjsBFNSzcL0yA74zRCQr2ENGPFZZ92O9F/iNTO5pTGsGY0b26FwcT1yHhUsRMJvTWWWtdb728kgSeGopJwJTNp4E0K3MI+NnXQ/3eyiNmyCKAjNHtIQiojjwTAF+hQCuGcuCMzrIlTIHP2KcowpPsPUcf6Z00qpRGvduUZr6yD1ioK5bEs49AgoaYcWUmM2D/p2eE5CskixjizWmUihk9WfAvbTbEaOXL4I6p71slsMzLW7fcaDxELwZD/qG2HExJUkfM1xFyGXe9BbdAlCRjkvO4b/SKf779ATohgo5OqweKxzEUrVVmH1yufLKZbZypSEVPS2+IrUOdseJTLKFDwcUAgi+lMs7NlhkwEH5UihWNNYPD9/2zfghIp6NKPsUh+ywuQvjnnfKun2eB7moSMIlPetZr 0icvdfct Vx5tvGHjDk59NvLK5CRl1TzyawWB2U7+qgzsF5lm+Aj0WsNNoa81sAkgfGgoDROuYI2sorFO8i+rWBm5BsLSjd6qB8OC5QrwvDcQo1T7TJYSnT0kt2ftPaT8QPVrThB61UoX43GP1i6PmaShwpoVZ8a/0UAFjk/coNNjl7i+C+8GvTtvPZ7D9TZMuCbJ38aEd3WKQovUAOFirjwjR1TztPGcVpgP3nTQVL58p4F5xbN5LSqlxLp/Z3lEBzPB/+DE5nNYsOiuI6zncRGkwR98sj1wT41N3tFWaD5MAbzrdRszu7zsZBOIjPq6pv8LTFH5EIu2gZ7Nn/FI7u2QOVWfc6VeZ0vwQLZOyp4v0mvWL2v8PujrOXGA9BSLKEmGl8mP0uCi61N5ZmulQFSjNXsHKhtwKzfuIrlkXhylyjrWFhyOfzTIk5I8X2/MCjw== 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 --- arch/x86/mm/tlb.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 2c9e9b7482dd..e2a0b7fc5fed 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1065,6 +1065,19 @@ void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, } +static bool broadcast_flush_tlb_all(void) +{ + if (!IS_ENABLED(CONFIG_X86_BROADCAST_TLB_FLUSH)) + return false; + + 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 +1086,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 Thu Jan 23 04:23:27 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: 13947928 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 90A79C02182 for ; Thu, 23 Jan 2025 04:26:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2EEC36B008A; Wed, 22 Jan 2025 23:26:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 276946B008C; Wed, 22 Jan 2025 23:26:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 117F5280002; Wed, 22 Jan 2025 23:26:43 -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 E42AE6B008A for ; Wed, 22 Jan 2025 23:26:42 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9642C1A0AC8 for ; Thu, 23 Jan 2025 04:26:42 +0000 (UTC) X-FDA: 83037430644.14.D76EEA7 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf28.hostedemail.com (Postfix) with ESMTP id 1F4F6C0004 for ; Thu, 23 Jan 2025 04:26:40 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf28.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=1737606401; a=rsa-sha256; cv=none; b=308sFyXol9cwMOpxqqsN1QNxzJg6rufKQi4WR6QGpXMQxQosQ48eHW9UwUOl5Z6tr5UDs/ q+uVkSjgQCR99VU3+wvGQwrjZWIL5q0+1Olh2prhXBrDC1y6r/HYohp7U4Ek4xx2oSPiq8 vG+3rvnx6niydFHCFEDMBQhxp0j9d/A= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf28.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=1737606401; 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=hYZKHKN33BrkcN2hT6qEtBNKXUt3RzEMGWzOtIbwDXY=; b=Xpp2qN1D52gCX92Ftq39kqx/1P3EloCZDNR1zM/lYswFF1JuFq/grKILpj6RGEeYKcMYAw jSY+w6dFfz0nfjY5FizD5eXOZZSKfByuAp3FLyVfmwaXHKnjn5V7zG4mN88CuA8W4fTAth QeG0hHFnAwxO2Q+vWmPtSo2MFQcEXqk= 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 1taols-000000005uH-396n; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 08/12] x86/mm: use broadcast TLB flushing for page reclaim TLB flushing Date: Wed, 22 Jan 2025 23:23:27 -0500 Message-ID: <20250123042447.2259648-9-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 1F4F6C0004 X-Rspamd-Server: rspam10 X-Stat-Signature: 8y5zckyd33yayey65oacg5seozacz6f3 X-HE-Tag: 1737606400-486586 X-HE-Meta: U2FsdGVkX19v1g2XeG2tdcsfRCgadlvqG6peFMMbMXPTkrt4JacW+eyRghgN7eX/Q6ymAoTLgkgNUDGysDiQ5YO9wsxcw25bMj0mfwXoEFjMvBsBz0uoAyBwG155pV5JVUom3L1RO3Y5yWbLmm66fgaPqLMI360u7Th5dyyjpWxmR4ufK7Yl5gC6IZwL31m/mY2mhgnEYsz6WDuMoO4DQq9Xb9JpPzl9rvMzu9h4mWdQJZ2jxAgNC7ACTizaGGmTSC+pJzSVod9IS9s7pGejrovyROkU9vWQFImZY7RQK7XU47kBRY3oGwWgciN/SO9d3KqJJ4Yc23HTzJlEeHOFztsx0IBK4rWi5NYOL8LDoroKBS50YrfzHjTmfZlNy0KVTyBbb9X5TzLHkuPReGl6KbEN29PS2syohjAi8pWNXcjUJK85QQOwfMqvb78Ng5C9mwdYdvBNU9Wr3MZWfLogXIt8ct0AysGqExUdc6PgWgEEDJPAeQSroptalUqIv01vcYve4gw1lECsgAOWhs/pWBZjUlCdjCpkpn8ZzNEXh6KDgxCH+eadkuho7jTgz3zgJ/KjJZlV5isEz1FxV6QUkuu3tbcIr9PlLqoocMYnYbrA7kT84+qPmDmlIFDniE31yuWccgwdW6YvxIETqXvyZrt2Enyss4Bzab99qRiWCVJcegqg+HHdyHH4BQtvqIdhlNugxz1N4zoDFgUvVD0iSDqgz26J+h7C2Gzraww8Tm9f22p6paMt1r1/jDIEbMAeRpwxyMKYDdeHdZdJq2LemeDI8GT4ua5icKbyl0fI4FLcwIGlPkmXwWdcFV/cnJ8HF7ErWDMSHdUHnXRPYenWpNoOs8Wzzjafx62AwqOGoGyL0Z7SfG3dFArteY4lG7f3lLENXbh1yuNpntT/j5OMv0iK8eCD/99byMTu0BDSYAjC4BEbqLRGcJzBbxPcHBnjUOmYr6MJdtnEdArIDh2 1AV+kPcD ZhkkeAt13Zb+x8JR6DYci0nvU8S1MiuMxGO1YRkWBxOGzy6IwMs38UdEEtG3+HXxf5iI8cC69DW7vQJAiFFO//h2D9mwgg2w+qjX59dQ15KXaZlX0+HyLxkY9kH+aNNbKSHeCxJEOBysJyF5iwuI/14pjil0iOtqHfYayNDhVds1i5wR9MgshQe9Xeld8tU3vab+J84gY77r3uidVqcTIeH5dTseCnQe9aYShSCkcbhgdbJABEgNFIGpr9XR2m6R4R+AwZsl/TEsBzgISrYW6wm51rGConSZMLoOmXyEbSIf58/7IPkavlLXg4vKiwzJeooH6/XH/c3/yZTpo8B3KeKohRjjZZvO1D4UWlxJgog2hqhePAvLcesEL2RWy202DrXIsr4BR5A0jjR5MAcLUZiQfLqY/an4ox4jZdtl0YjVBhq8= 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 --- 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 e2a0b7fc5fed..9d4864db5720 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1321,7 +1321,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 Thu Jan 23 04:23:28 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: 13947926 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 B7694C02182 for ; Thu, 23 Jan 2025 04:25:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3D75C280004; Wed, 22 Jan 2025 23:25:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 387A46B0085; Wed, 22 Jan 2025 23:25:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2285F280004; Wed, 22 Jan 2025 23:25:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 019C46B0082 for ; Wed, 22 Jan 2025 23:25:39 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 9B1D8A0B7A for ; Thu, 23 Jan 2025 04:25:39 +0000 (UTC) X-FDA: 83037427998.05.70D20C3 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf12.hostedemail.com (Postfix) with ESMTP id 001DC40002 for ; Thu, 23 Jan 2025 04:25:37 +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=1737606338; 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=tenS5SduEBx3wBXhMykphN4RYAJoBuqmRadxVGnGD0k=; b=DhaJ+XVsZlft71wQ2Y7qpksL9yDhc+XumM1b//lL0QYVa4jv2iIAgF2l2L7FxRqS3Bf5r1 FVLrBXoTGlZLK53fzRAuBMTrrErFGy8GmrpnLOq5Zvz2S7jbXNI9FF8uVhLoH/VfRsQG9+ jIXBHZ6gzPzhToizFW3ebFsjTKsXoDo= 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=1737606338; a=rsa-sha256; cv=none; b=W+AGYoEBl9938ByutIw4tgbtfiPdP4r8xsP1rzUDIq+uuXiSyOSjQTIaqWeocYNiRZHsUs Z7LuqvtD2gU6OcZK/ktQ/F3xH+f7Hua6yl0MxATmJMLRtloqVPE3mcxxFpGbCWTE0xMRq/ fLPXkrN/d1kgPOgjUKWyXrOg7QQHHhk= 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 1taols-000000005uH-3G2h; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 09/12] x86/mm: enable broadcast TLB invalidation for multi-threaded processes Date: Wed, 22 Jan 2025 23:23:28 -0500 Message-ID: <20250123042447.2259648-10-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 001DC40002 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: mekb9w6s11f4kkwihk4ynhyes3jgupc5 X-HE-Tag: 1737606337-959286 X-HE-Meta: U2FsdGVkX18XibsOX+vW9m8kend86xUZfE1WewLJsVlJdz6fReCQGJx4zsJVBWq36oXr8E/wEYLI1PnqOy2uE4O0lR+y3lXEPKMoR1bpVgxpgvNCicwUBjdM7jBrLXpzKWfM/T4xh2rxiO9z1ztha5STFGo2eLGBg2xyGAbx57xzOd/dslrfxA+CSMjjPQRSV3jv94YEsXUVkaFDpNcqacrhkDVXzXmgBBqKr5zuGJZzaxcUwFilYr/pg0AqCf6xGCmqDXCL8T2O5HR6BHXRTyWECdIlMtN71XfQgw8d977vqnf0V5SjnfPYGtRbZ0bYg0dyb3tBzTEkDLEENyyePfAyZbU4UC+blErKH8kj3eLEXuso591tf2SIFxqFTQbAJFjNACTtai61LFEnBTmgIxHQRE0eKlS9cQEczzTlLTrzTSUvdQs766E1fowhzVJOnlhDe5B2zXvfoDn7wNRNJN+D3VXrKmwOMRrF6/UdaWVOEQsvgmuQe7k1nGCOgcWV218qDg3VJr8FvxNArn4aw1nzrmlVHlJ9rE8tEqDh/Cv2gqpm0pwUDVAnROHlGmIjSobJ5mS2KEIN+OMfFJKGq1OkS33FGtxWXreY3jUgBoSQriu4dH2GwL7lfL3W+MiPQr2NlwTMDh1r9HWa1Z2vs4YEPV394gG3/2z18uOq2mgTlvY+BjYvA8czSjh9fXSmpxx99tzPGNfDfDTG1YEOxrJY49QLbCJ+yq8O6LIlG77w4aJlLChFzNOT0HuTIZwjSnKauKqjHEg1jZ7HDkuuMD/ujcNKizXASq1yb1VfUQbNrwbg4x9BozXyLwwL2w6Jop5waPIzBYEI1mrLGM0BiUO5WyTLLj8f74xdRmrxbdWUgB87d2P9ULpOhV08Y4pen7OMVRJIhnygypwyEEotWzM2heOgOfu7paYN6LsXJsRXnXIBLDiqy+jtnsZEJVZtk2bpHpCxLubxPBUmYVn mHB7oIKs KuRfn0qulnNss203VtzptsxTjyQb8+4DVoPYsaUF9eyXfxn/lpZ+cwkjMe8ZJ+Rp2zgzalnuKkUcO0YgasxDLCCDDBFfmwnB8NzK9e+3ZkCsgBGK+oAtGd3NfWVuBMfKmxRr1sKvuVBzbhsSleUxHGWux+Pu46wfone0yztpmwKsrB2Zr46PYg7niLOhGsFwM76RF4JKAKMNpo7G6+ArbkdhwY8XkHdZyteRz37jdHgjCNv2WjalFyFGY5+/onH7dmthfGb1DkJm8aBoe4rGE2twdyl8btwxOeIt26aABRcJgrKoneqE0udM3xn7YdAvHePtDmzFJn1jf5M6deLxLRixRG95iCF0No7mETuuzMOI5blYa46BjQHabVg2V0/EkRpdf 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 3 or more CPUs, and gradually increase the threshold as broadcast ASID space is depleted. Signed-off-by: Rik van Riel --- 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 | 344 ++++++++++++++++++++++++++++- 4 files changed, 425 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..7e2f3f7f6455 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(const struct flush_tlb_info *info) +{ + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return false; + + return info->mm && READ_ONCE(info->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(const struct flush_tlb_info *info) +{ + 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 9d4864db5720..b55361fabb89 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 (IS_ENABLED(CONFIG_X86_BROADCAST_TLB_FLUSH) && 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,272 @@ 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); + + if (global_asid && prev_asid != global_asid) + return true; + + 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 (mm->context.global_asid) + return; + + /* The last global ASID was consumed while waiting for the lock. */ + if (!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); +} + +static bool meets_global_asid_threshold(struct mm_struct *mm) +{ + if (!global_asid_available) + return false; + + /* + * Assign a global ASID if the process is active on + * 4 or more CPUs simultaneously. + */ + return mm_active_cpus_exceeds(mm, 3); +} + +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 (meets_global_asid_threshold(mm)) + 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 +838,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 +856,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 +906,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 +927,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 +1051,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 +1074,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 +1101,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 +1270,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)) 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 +1366,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 Thu Jan 23 04:23:29 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: 13947938 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 83A2BC02182 for ; Thu, 23 Jan 2025 04:29:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9E6B280005; Wed, 22 Jan 2025 23:29:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D4E82280004; Wed, 22 Jan 2025 23:29:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF942280005; Wed, 22 Jan 2025 23:29:32 -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 9FFDC280004 for ; Wed, 22 Jan 2025 23:29:32 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 54721160A20 for ; Thu, 23 Jan 2025 04:29:32 +0000 (UTC) X-FDA: 83037437784.14.8B44793 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf25.hostedemail.com (Postfix) with ESMTP id C2A4EA0002 for ; Thu, 23 Jan 2025 04:29:30 +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=1737606570; 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=6I2Tixh2W7UzbM7bw8WMg2w4i3n9YB5wM4arcW3BpeQ=; b=vZs7q2uOhTQXNM1+Stbtxahfgo5ZZ/fwCZQBe4FwxjDJy09t9I68Eu5qGGSceP9rNC7zQO wLkTrkxE4NqcETatzwjBN1RJGpFbA8d5zDRSAVVOkFXYfMJ23RAdhKzKU6qma/dcVjlXYV Mf6QsP9lHUb/gdeNHP1Z5QTcAlXdeYw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737606570; a=rsa-sha256; cv=none; b=axneOkb0Wuv95lKJ9MVYku7lonEHETmqIxiFGxZBk5pD7ymMFVhU6eByYGRBtJcMaIVii6 5tYYaLFvH+WQe6ErrU5ira5LkPr3pGHDzqUiaoPPF63YpiMOvfGUHmTm+4Uus6Nwn0MSIo Hac+NcY4UCrmVMiGK0Xljv0o6rosHDs= 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 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 1taols-000000005uH-3NEp; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 10/12] x86/mm: do targeted broadcast flushing from tlbbatch code Date: Wed, 22 Jan 2025 23:23:29 -0500 Message-ID: <20250123042447.2259648-11-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: C2A4EA0002 X-Stat-Signature: 5zfhhitkbda68f7845rn169gno5xepnb X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737606570-253561 X-HE-Meta: U2FsdGVkX1909B3B2cgloNq/edB1KnwjNWPbos8suqjiuwOcPNKMU2oQUBcWdv+CyPmsRrKxRK121SBUoeXP+ZV5n7Qat35T5KAwqJHednlrJyxIGQjsBc8mz1Q86UdmrRdPv6ZQ5/7AoTHWJfq0zg7PVmCwrGHtXY5a25KGNBsoL7vINZ23EJouM+kHwhrEUwqNoxpL6Ihq0sN6nIs5lPIU2iASfuyhS+BkATR1rxffRdTP5gtDF6s9FSa8NyNOa28x48Rq4Q3zqrFymOYmHM0XBTYFM5RWPsy2jqLBfE27ZbFAt4i0DsMo7l3UKByPTLYDyhtjKVrOFKrQojCAv+q4/Jx2X+XvNihZYmuAmHlPlgqub6hGrRXFLFBxPUZII0bHUsSfLrYn+d0LDlV9XRmyctht9OpSC4lnyN310ngxgSn6TMk+56Y2wgpUHdmAOVMQ9BP3VmfsgUQw0UpX1QyoWT5p9vqWMg40QEIDjLXoLHqujHVh41QLabws9dnkFSGfCQR3T9+PSgX2KCeIVM/JpEKOMSyVsAnmKCwO+CxSwp04SdiMLMcZCb8mKfzjVhjW4pERi+LumT2BeiyXASKgbZ4wGH976MyECnh9RjwYXTnXhMU4SKUrFeJev+0ca4mRzwBjhHSJsz1YnJmj62q9+cEPGzYjd3ocXuBLN6RZ2tSw8vUC/J0f73xKQ21YJMsKvJhJ3JOc4R35jZMikWlPFnK/zz9SSoGZTuycEw/8hfM1dJm4GthJz77zca5Ae2xaK7rE6K/pKrmwJ24FOlQmL7F3skDqDm0vWACE8D6a2MDnIHG67z4s66Gp8vMnTr7WFzTsjvzkNnjGcRE4XxEa76ylVmR2Nj/1wk2/+fBviXZ5/zu/fC12fiAnbysj7AVhaQjnlBi/gOroXkZ65PXcxMe3+i3qvxM89sdoDwoKZai6qXceZ1w5d6ZSlLvBTRKrqyb22mv4PwZfBj+ wxzu5vkx uP3u3WuCX+sUXo01Zlk8aF70xrsVws3KIbeETkEHH1ZOSZPIEJ6hrvQy0ngT+jRiCt91VARgvFbahCe3oBacWoYQl+jNKR498XPNjSguqvbIfgrfrdB4N184N5aHnurTvG776NCkx5dBPp9BXwq20qCQQu/2FpXPTYgJGFT5/rSJ8uLeitdtCvDzhxiQ8EpA3gAM/zB9a7+DhcYeIYj/TXtkRxq176rSBNcg7/BwRr7TRWsCosQeMwDn7VdnXlGX8zoys3q8WsoGErwh/ynQyEhEs5VbfkmJ1B00BlseI73XTh8h29nParHkq4W09mW+OCJr6MPuaosq7W9+/Wo23wvQmlqORInDfPhHLuuXe/DcE+35JrkLptqHJ+iDYvMYjGoLIAqlivRSfLIp2n/ImH/cztec9FH1sk6V1P0axakulsTc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Instead of doing a system-wide TLB flush from arch_tlbbatch_flush, queue up asynchronous, targeted flushes from arch_tlbbatch_add_pending. This also allows us to avoid adding the CPUs of processes using broadcast flushing to the batch->cpumask, and will hopefully further reduce TLB flushing from the reclaim and compaction paths. Signed-off-by: Rik van Riel --- arch/x86/include/asm/tlbbatch.h | 1 + arch/x86/include/asm/tlbflush.h | 12 ++----- arch/x86/mm/tlb.c | 57 +++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/tlbbatch.h b/arch/x86/include/asm/tlbbatch.h index 1ad56eb3e8a8..f9a17edf63ad 100644 --- a/arch/x86/include/asm/tlbbatch.h +++ b/arch/x86/include/asm/tlbbatch.h @@ -10,6 +10,7 @@ struct arch_tlbflush_unmap_batch { * the PFNs being flushed.. */ struct cpumask cpumask; + bool used_invlpgb; }; #endif /* _ARCH_X86_TLBBATCH_H */ diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 7e2f3f7f6455..f8aaa4bcb4d8 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -359,21 +359,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 b55361fabb89..9fee2aff8153 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1641,9 +1641,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(); @@ -1652,12 +1650,65 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) local_irq_enable(); } + /* + * If we issued (asynchronous) INVLPGB flushes, wait for them here. + * The cpumask above contains only CPUs that were running tasks + * not using broadcast TLB flushing. + */ + if (cpu_feature_enabled(X86_FEATURE_INVLPGB) && batch->used_invlpgb) { + tlbsync(); + migrate_enable(); + batch->used_invlpgb = false; + } + cpumask_clear(&batch->cpumask); put_flush_tlb_info(); put_cpu(); } +void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, + struct mm_struct *mm, + unsigned long uaddr) +{ + u16 asid = mm_global_asid(mm); + + if (asid) { + /* + * Queue up an asynchronous invalidation. The corresponding + * TLBSYNC is done in arch_tlbbatch_flush(), and must be done + * on the same CPU. + */ + if (!batch->used_invlpgb) { + batch->used_invlpgb = true; + migrate_disable(); + } + invlpgb_flush_user_nr_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 (READ_ONCE(mm->context.asid_transition)) + 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 Thu Jan 23 04:23:30 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: 13947939 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 6C2C1C02182 for ; Thu, 23 Jan 2025 04:29:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 031CF280006; Wed, 22 Jan 2025 23:29:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F2391280004; Wed, 22 Jan 2025 23:29:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEC37280006; Wed, 22 Jan 2025 23:29:58 -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 C0CF6280004 for ; Wed, 22 Jan 2025 23:29:58 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 4F4C0A0B92 for ; Thu, 23 Jan 2025 04:29:58 +0000 (UTC) X-FDA: 83037438876.17.AF684F0 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf10.hostedemail.com (Postfix) with ESMTP id C8072C0008 for ; Thu, 23 Jan 2025 04:29:56 +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=1737606596; a=rsa-sha256; cv=none; b=MEEvVM5gwICZw0g9ZFb3Ej4jEYeA46wne5+Q2HofgXLzYyzxtAU/IaYc9AkJ6ROQctEM7E eTix/DtU9v7assBkFo6RuzRWJNy/mWefyO+Uc0EZD1j2vqKq/b4e9t6wRJYiIdyohDLXZt Utb+oRA9BfD35pSu2n1lWtFaZi9hKmA= 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=1737606596; 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=E11YoPwP2rmb054DCbWgK0xc/jXlKCd3xTPcG1+CCjs=; b=vIBd8cSqwltQcZj8bqfoQjCZ1hzvTdxRsi1g52tKNIFOA4FzhtGI+cfgF1vPT4HJjCANKu b48dN9rJqrddQbuN8xR8GU92HK7ut7x9RSzid+EX76dsqEl+K9pSipiABvLN82HNq7lL1y UBY7BQQK0XlLsUkJsBKOlponTLmH2JI= 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 1taols-000000005uH-3TDy; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 11/12] x86/mm: enable AMD translation cache extensions Date: Wed, 22 Jan 2025 23:23:30 -0500 Message-ID: <20250123042447.2259648-12-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: C8072C0008 X-Stat-Signature: yss8owikf4ocern91i111or48m8roq37 X-Rspam-User: X-HE-Tag: 1737606596-842885 X-HE-Meta: U2FsdGVkX18EUgnnuS1I0ySPhcKJqYE2pmYaD6yHkZqnKKXC5gPPgoI/LGbRk+YPFDqwUiSXgXc4HViiYKb7mR3Q/rXeAcmCHhAxrN4Ljr5RQxDj+AIGrorZFXQXpA+Az6MK8CwjxiHWfCV6SHxwlPHqERiRni45rIhAOZr/TVLE0QS8shQOGuf0GFdpvYFSp1RqPFGuxbYn3bZ2ONUsDk4cAe4KvcYrdYzb8sWAJrjE9EWkIaGxwrPb/AdEC2/uOiRjXkV5K29V7VYDM8kf1QbZe/YQGIZby01zlJgxA+//1Mcp/u4Tj7CWQN/c0djosMdDuhiYYLvemLnEqdCh8PcumfOva8XdKB8ByhCcUvnlECPnaQO01biZZWuS0UMzO8Rhof9fFKjZyGyeE0uELRAKPA6/ai0vwE4DgYltIiUuX+CH7bHpfTSBDxQeBS861rOY48soIbU+AHMX3UQTXi8bMPktjlR9T365rZadzhLDSviZbVVk0QFqh2e3WrLBRBP0XTvaN0p8meKTJ/+hB7MQSH7jGBHab5eS7KnLTZ1i4GkUz4+0c38scDr2JhBi9TfrwxRTrQVjl7cl24K50N+ASdLyIezJrnNbrnjjmMtrCQJZhJW6kty7MMvMPZ/LVKDzELLT5xQdDIfcVgy9HSQaZsq/MmHYeBgsBr934SqG8k7l5fy1ja7U9xZXCgf8K4ErUaMRBwgFoytD314/4XzURHiLG2uDrPT80CWhvuFujuKZyTueV7eIENXbNkwhc9sDXMOQlcoHWV6mj6qy1hSXbwV1L3BatzPRx+bTMr5Ge1NWPFcJj8NwukLCfMhMAifdW71BLF+wSjUeVb/BaYQGwSImqqHgqUedIHvyYckttCOqnIRvx0m5NryDcenfpxqnQWxLD5zGqNHK6JKG2D9ZJ8WHZjckgDlX6uaDbztZcuzFieFUDGjqm811ZB9CZ2cwo7MrUAUpqXJAHUO r2gCeS5z h+0bN/ugPsLrKuP/5m+vgPnZujQGf+y/K28hoPh2os8UtSPNjfB4Bluu+Bz1tA8w92DmhznTj8ehfms7wDsF3IBAVRPSxGIHKrYI9Ficwj74CTEPmbEq5SMYb7GxdKlAU8fEsVyJz2apXpVkvKR1C28vCWT069RkhFQP/2kH3e3GCbuBa3VOlgJgT2rwlYmuDhes2b1neOBD8no7a+Bno/CHgfuoirqBlkNEVs12xhjrp5nmaf8njah/G8ELXFGqcZanRVFyxXcmve1RDeDADRaef20gm7GKHX3C/9e7MAczkIccC2gbIcVQ8NuDLFyHADs3zRus2IAgP03vlpLbgr4brwOUNDhMyZw0JIPs0aw4tC437cu4WZGm1sDilSP4oZL0T 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 --- 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 Thu Jan 23 04:23:31 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: 13947943 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 381CDC0218C for ; Thu, 23 Jan 2025 04:30:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BFC528000C; Wed, 22 Jan 2025 23:30:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 893F428000A; Wed, 22 Jan 2025 23:30:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C2BA28000C; Wed, 22 Jan 2025 23:30:57 -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 371A728000A for ; Wed, 22 Jan 2025 23:30:57 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EA5F6A0B87 for ; Thu, 23 Jan 2025 04:30:56 +0000 (UTC) X-FDA: 83037441312.27.20D0422 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf15.hostedemail.com (Postfix) with ESMTP id 5D7E5A0004 for ; Thu, 23 Jan 2025 04:30:55 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf15.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=1737606655; a=rsa-sha256; cv=none; b=P7tOiy6Yr1mlAjw4Ojp5vZ5jhOo9zAzG4mpalUM5EbKZgqvh/dRmmuVmSSxlO+0AQtMFjn vLNr8IlrbUG9ogk+zp0bmPXBFhr4TVeUna4zFX16lilwc614WQeWhG3drpM31tE2IcvwpH RfIq2CZ8TvXZbO96BEegoJPu8G3hwFk= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf15.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=1737606655; 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=8C+WD7u9Zqwod1lV9jBGsHtY3wt4lu8RpE4zXut7lR8=; b=lsk8RPRZwK6J9hyCrzpbPgzNBRhRi8TolKQe8HPltD4mMA7Ly/qt8qWXMtWTE0pKOrd5bR YP3a/w4kqIsv2z9z+rnZIyg7o0ZbnOVCPhYZmXPVcfujGkNFmrTBlQn4wooYTJU6rCaW1J FSwUCQ2Nt5+STMmvt8AggU8ezBoav0A= 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 1taols-000000005uH-3Yx9; Wed, 22 Jan 2025 23:24:48 -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, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 12/12] x86/mm: only invalidate final translations with INVLPGB Date: Wed, 22 Jan 2025 23:23:31 -0500 Message-ID: <20250123042447.2259648-13-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 5D7E5A0004 X-Stat-Signature: 5hcqcgrzfj8hbhzagn35431o43fu3o41 X-Rspam-User: X-HE-Tag: 1737606655-168029 X-HE-Meta: U2FsdGVkX19B57ZBNWv91BMEukSadfuQ0lPrTCoDI6/3DvNO4F2y6gFKKobJTZgIrLRoANk/MNe0AV7FX6xkymLPxeAZOiVFqe6RWavExnSuTE8HEg/GEu/evritg5dWaQVoGROMmV9qgnYIf/Zqz9lb2vgZ7xRMBMZDYk0oHyH78UyUgzNam+bgqyTWs8ZScWHSFues0KHq+uStQ6+y0QEoMtF2jNITtCMG81JcjfDaXcn+P4KqpY5T2wo/hOD5jNyrDMIu0iikPlx4YPsLgKZPx69RBcS+crRNTROFErJLZhZ/mMtyjJpKjZEm7dvAcC5vgiQ98DBCX7+PPcpUtZV3KGhsEj4GuRLIKgpTHDTR1vYgQq8UOm5dhAOBxS0XA0jCNrTWTAB7etHldqg2h0hljKf3P0tFIGZPdKWajUZxpetXAwbouUN8pvazjjTG2hackYzE+NwJ+yVbyL2TT1jf2/5UeDgj9g6B4eU8K5Gs43A+vBkV6JVnA/y8RczYVrsnvjjesAkub1aWVovCeG3vp24qDTTzU4vuyA4q4LdvO0xXJaTiiLn8L84Z23tNspxU4H83TrE86Vt7l6teMF5c2hNM1bCM0TotffWvMunajYVsUN+oFF35o405OuNIMRvIyqCfAhFyvC7kYvBIS2k8Vd7NeE7cGLOHNfpkYpAm1B83RDsTQ69dCZoJlryhijPNR/8aHHL23jxYPkc/fJ576rOwluxTu82ZkbyT3aMESnAlFnW6SMJMYd2KmjZLxNH6ZsbV1Kqmp3rJYLXIc3V4a6utzLVVWUZPHi5TqHY5KWS81VilT25Jb37Wp55p0NewyCZKe3RNync79/UU3qCSYsgpN+IWfNc/sxvrRT8aAnQJt7zKgB5N6zAwFJiqq3rwlwpK92NlWKkRKQw6GtRFIfoOM/qdmubYRqx0aHzsYbYLK7i1vs9KbfAMckLKya3pgUX0eOBSZmdLYrP KjsiJqeA 2W6Jr/BREfR3XwYSZMK1F6igFPNaff7tr1zZyJKozN4KX5c8ZAa9kWHerBFtKJrGzwNt2pvXdOvgGHUxuIvvHfvjiPSn9iY7Vs9gn+ZNK1REoG0qZJWJsosYUuHFzmYs4znyLdZb7JbTIJTwclIjGyRwnKSWblo0/4Gim5HHZp05RjbNqR0DzkTe0zoQSajzVZZb2g8ExNTv5MirYh6Z5dpmvjW4rXyrlyt5jCKOYx1Ak7taoEq32GVYM0iamVFQiSEvi/v3w+27mxv2PJhemvqXYShNIuB7kS1yPUg1bRxf2D5zPHJCNChmHH7sWmujffsY+wFBMzJo8hHAEPJ5Y5SRonU/iay8jRfRfuhfSATqKyBhZbGlmnVLgYJR7uhgbQ+D1ES99YznSEV0= 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 --- arch/x86/include/asm/invlpgb.h | 10 ++++++++-- arch/x86/mm/tlb.c | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h index a1d5dedd5217..5fba41671a6d 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); + unsigned long 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 9fee2aff8153..682da8d0d1c9 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -518,10 +518,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); @@ -1683,10 +1683,10 @@ void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, batch->used_invlpgb = true; migrate_disable(); } - 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