From patchwork Thu Feb 6 04:43: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: 13962186 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 2441DC02196 for ; Thu, 6 Feb 2025 04:45:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6B4F6B008C; Wed, 5 Feb 2025 23:45:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 855EC6B0096; Wed, 5 Feb 2025 23:45:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5BBE56B008A; Wed, 5 Feb 2025 23:45:13 -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 E4F576B008C for ; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 9AFF7C0E9D for ; Thu, 6 Feb 2025 04:45:12 +0000 (UTC) X-FDA: 83088280464.14.4DE630F Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf24.hostedemail.com (Postfix) with ESMTP id 166DD180009 for ; Thu, 6 Feb 2025 04:45:10 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738817111; a=rsa-sha256; cv=none; b=igH3IhBkkRgveG3N5oo/pORJh888Vz5HJuXafntJeoJS3mgKv+5VLFToyNQb/WMt9v+RDF 7u6zYVm5mdPviZNkR3PSTSzIv96IqPMkY9SoVQ3JxDkyP8C8KFcuFKD0mH8CgRrKsfOslu UNcsBO2/5rJm3kae5x/cPz9mpYCjY0U= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf24.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738817111; 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=POY3K8VJw3a1uUU3SOCTreZCz+W5UIji1O9L3bkG7jk=; b=w0YflwjGql29LcIQ3eU17Fj2YJbDZzNXfEfeschtbSo6u3zHTbf+AOTm6YffqvYtEdfduG yyahUa4Jt5r7K9fAPAIC1xCcDUTRnZtnY+IShJYHT/SJ0NclLcfeMTOXE+GtwPSwa24cmd yelrB5FkPbc6srkdtP6wc6sxy5XgBxU= 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 1tftjw-000000004tQ-1ilY; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 01/12] x86/mm: make MMU_GATHER_RCU_TABLE_FREE unconditional Date: Wed, 5 Feb 2025 23:43:20 -0500 Message-ID: <20250206044346.3810242-2-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 166DD180009 X-Rspamd-Server: rspam10 X-Stat-Signature: 1z5ko8xk6ysqurjj6rdcdbkkdczpszk8 X-HE-Tag: 1738817110-493984 X-HE-Meta: U2FsdGVkX1936rDV7fljzDd8v8ypriIYSPvPsIAsVH7z5yEM6V0bF7nyMY8xIzHO11y1XDj1BR02JJz5Ou7KyMzBYuPK7peWoxPKJjDhrcvEnx4ZuWFLnkJ/cBioUrJg6gVLDKg0quZ7qWHJ8sXDyCUsZR/XaDH1Lprfp3XB4UePAzVf6sbDYiqRie3Ywu9QW1vywxNK6zpJo2Y28tOAqwkaGkFY+rc7k/Jlw64F7ab31JzeczQ7Ze/Bp7m2mV99LcYIxqT5GCTJwia8tuOa/D2l46FEqB5tXXAjulsbQtEL7nXUbExs5ErT2Q7awIpAt376Ghuj2P1OgVV45n2885Ry5H5FXQNzMrGmNVYsFr9ThvK8+oUdReXTnv5WdLysNfvWy4R4NKQMIMACX9x4lShNGDfvqMUtS9jbw3B0l10t3Q5s4iKy+8zfoU+/k+dYPMULk5SsSlaBSubFhaIJF7RiCEB9/Ix7+eqotQVxcj4cNJLU7oWoZbEMLm+8cBsDeXz9SDcLoNCXchQWirOhvwYsURGSQKsCqiXnsxcuT/3nOpKZhq2XQIXtHlAJv/j5cZIFBKg5eQZYcyBUSl67AzieorAPnWeCPKGZ8o0DeVHgK5z0867RLPJb/2DqTur6l2aACdAtEbzpgBjvj9mMbtUsAgHDnoS9MRon1yhBBIlNm/FyklBwlGuxWdFnk7CohIgvl5z2ggWUWYCYMaGu9mQxfITHB31bx5ujz3I5P+bOxDy8fj65y5SG+hEMagAuq8/pzxSq90P92R8H1E6tClgIiVBKofvjDZp76FJsUJFVW+L3PrVjW9imD7E0Flmi04oSladTveyTB3okK1+a6Rq8LuPcEG3zBIvS4Obi1VUEPgVPkr4MnHMnDadm6G/FbHxkOX9PWKMLRNgdn6wIAmAi0XGXmr7LnPfgL7DbAQlP/cK8iJA298CbQYzOCb4sTq2YtILA7lYKuYkwKGn 8yuOeEsT SK5wjQNzHrZLZvWK3tpB00vwZUZhCVzNnEACAkJE1ReF7Icr8DHz/fvf2AvcBF/IVuRo8qU8M1oeTZUQa5VFRYJUIskz4/1PHficX+qJgnOVOBWMPYA+M2Jc3tCqvoYWLeuPDuq0dTnezKm9sdCo6W/7w/hwgqGxHyhGfsei6QB7H52mOU0ZQEeRyAhOQSvgx5he4W2aqdytSDp+4syzjnmTfcD4wAkQjRzN6yXejypCg36s5O45xpsYlLwoJb9VRH+7exkfzys02PHer38NG2POQkn2I72Z6wIQJ00bWuLU5T711HRw+FM5JV2rYLeYalpaHJqEtPBHZraj+cOk9WoJympwauoiMLjTfV1VIhe7/24EttUVTXt02S8qQ7FU91RyEV71RHuzi+RJh4xEgMLh65CyKdt4PnECDCkFC4qp59OTDHykKpklvR4ghQAQeMH+hRs0NTq0HIr1wBFMvs+rXIvYSjwsTspBnLqFiCZt6ejFenj7yNoH+Yo7DLyMYvSpf5CNYC5avmjZ2YL64Lb6mJ6Vsv/D08GZUQ5iV2bL8eTYrBLxzQClyC4UpgcGsaSjAW+CWSsgJn615Lckcs1VzterUhpQYdluQZs6B890j/J3gRs/8l/LNyRSmNJdmNjNv X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Currently x86 uses CONFIG_MMU_GATHER_TABLE_FREE when using paravirt, and not when running on bare metal. There is no real good reason to do things differently for each setup. Make them all the same. Currently get_user_pages_fast synchronizes against page table freeing in two different ways: - on bare metal, by blocking IRQs, which block TLB flush IPIs - on paravirt, with MMU_GATHER_RCU_TABLE_FREE This is done because some paravirt TLB flush implementations handle the TLB flush in the hypervisor, and will do the flush even when the target CPU has interrupts disabled. Always handle page table freeing with MMU_GATHER_RCU_TABLE_FREE. Using RCU synchronization between page table freeing and get_user_pages_fast() allows bare metal to also do TLB flushing while interrupts are disabled. Various places in the mm do still block IRQs or disable preemption as an implicit way to block RCU frees. That makes it safe to use INVLPGB on AMD CPUs. Signed-off-by: Rik van Riel Suggested-by: Peter Zijlstra Tested-by: Manali Shukla --- 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 Feb 6 04:43: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: 13962184 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 F1B7AC02198 for ; Thu, 6 Feb 2025 04:45:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 64BB86B0082; Wed, 5 Feb 2025 23:45:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 000B16B0099; Wed, 5 Feb 2025 23:45:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C46546B0089; Wed, 5 Feb 2025 23:45:12 -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 4BF0D6B0085 for ; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 038BE1A0E65 for ; Thu, 6 Feb 2025 04:45:11 +0000 (UTC) X-FDA: 83088280464.18.391C8AE Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf27.hostedemail.com (Postfix) with ESMTP id 703CE40012 for ; Thu, 6 Feb 2025 04:45:10 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf27.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=1738817110; 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=7SMC/pD10vzfyOvE9rsu6eXTFlWmc349QpZ60zv8sKo=; b=laFXc0AcV7xfqKYmdNr2xly2oiYDeZny/xSgBplStGQZ2vMk9T+JSTOJF6mLXnMVMo8JA2 N9BXFzaY8L1ZtW6me3SvAbn6hcAzcJZPumByEjxT1XoPywifM9Rh7Q/QPv60Cev2YwROZi QCrH6HC1N5cgdTaoL6K4BmCtHKpRxeE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738817110; a=rsa-sha256; cv=none; b=AAAXbV7HTk9mkKT56NoLWEfT4rbcdMfwSkgxRdhN8oreb1HyPPGZagRPnPWkwOQNO1w1qY ENh/CAHIcyig7ByM9ND+Ru0BZLE4CC4ox0jW9bYHi7rXOcduxbqtFI2WyWgLBUvRJjWPmJ /QCRjuKJE8Sw9RPYO6WFuzO5wbf5cxE= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf27.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 1tftjw-000000004tQ-1pX5; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 02/12] x86/mm: remove pv_ops.mmu.tlb_remove_table call Date: Wed, 5 Feb 2025 23:43:21 -0500 Message-ID: <20250206044346.3810242-3-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Stat-Signature: 1u4nz9p54it61qq6s89chtra7cs4bq17 X-Rspam-User: X-Rspamd-Queue-Id: 703CE40012 X-Rspamd-Server: rspam03 X-HE-Tag: 1738817110-901477 X-HE-Meta: U2FsdGVkX1/1FPCxM+KFSnlR8RsNt8Hh898HbPvpvAL6q22TT8U7h6rURcXB5yJc4y6Ms/jeESZ1KkML1awJDYygqiaz4iWyAZKqzMAOAsWAUQl0pvWu7/pVn5/YIKpxXwQ1p5qOYOPLoEGPVjC9ONcONTaQt5YlOajoWMpxON32tMnZB1WUfUWdWmDqXICqxRHxdsgpgk6tjmGqc+nEwU3fNylDJzNaALiOQ+GHOPia2qb9LN5yKlX9PENlfgagkEDqU2eIFEwF8aRsy6dMYHCkcAJajD0Q1x01vibftf/tqNFD/49zS7UDxl0qusStr8RuVUYAzR5od5d4ABzbJ5kNmgASzM2GBIw8mtn213f/fMOAdd+nEwXuC7bInRv8WXOZV+tMp4/iVpnkN0TZMcxcy/biGi/FmkUWRhYLqrl/Lq1lBYxhOpxqzarKNoh0NJ+Jq4LqECeJbuaK+tOq7QxwOY7Ecos6UXpEuecOiuSs1w0+QHZ+ushcEba4+c1Ub9P08qJv61m3CdPiUDQrQTYg3kz2f4oW18r+aeAZmyY+wCM7ak2uIuU782BDfLjt4bIpTBiw8rkvXCdZHKVHiFSiunWfTPJY+w0LSiK+AADurVAVi+IWeTbwitmNUWZR1noSjswvlC9PLLTTUzm0fkgkulJ8+VIDyoPZm05x0TXM5BC25xlsbvY4qWYTAGSALWrNToffeGMZJwOs1cRKEto/cllMIDcwt3/Ru9ZRkueBPZOEkcVG23KpQ2CRgy9r5aFmDE5fJHuh4ClYzUr9amRH1YN3QyPkqKGZtoTmGTCAVHLgnsa3EsT47f4p1wjEjMAwFlMiAu30XY9z04K9J478Bb1vzOBPGuVM02I1QSTTekkbVo35O3Ph43wlZA9DQioy6SVi47nSqaCt+9+rwy6XsSJueqJ/4YfTQeNY4Mm5GgtoezHI/gB99ev9jdd4ZPALPE5Rt1yyvn9BbqP VcDjKGF+ GG5t+bJrr9EePtR7kfaWRUFgCmTBdV1LkD058N+cl/thnilF4B2ns8+gPRnCT/aBG8ifiEaTp+MAjpYzs/pckUvcApDk4EF0gowPXZz3j1y0Sd6Uxpt+QuMxyGeOTSwnUxcUZL98Rhgy/t9PPNoOm9XK2xxJUcwRfZvPJk9AD4jbE4K2zrtdCEt4rbV2QXnLNo9R11/VOovGZd4wfzr+AzUqVrW/XIBRfnt7apBt2ysmeImxGblIOweKL+mk2+Ze6G3U8va41LoKgSSveZ99i4CS9LsmSY6IPYDh+9GGzuSVmq3VCFXzoJQeCxcD4mGH7Nqv5APbFE9x5MG6iB9qo4i7euR1+H8T9NSfEmcCoAgyUjtVDSoiZZGOgorOe82AiGlFY5GLw/BK0rdXjpqWza5jw2xrI/0J5rcMnGC9Eb5DLwSLdbRXJm99KR3PrV7Qn0WI07uHLaHwapa9UkhXMj33cSQe+L95cUomv9IEGUiMZN9i7j+wPCxtQXk4zFeqvqKXVqy5eQGzxtQhiMHOreFy9J51cMYGlWlDteQnf6gVsxMEDDHKrDZv10688NOvXn4//KWhcbK6Kf/dbB2KryVPhr4ddh/zuqFG0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Every pv_ops.mmu.tlb_remove_table call ends up calling tlb_remove_table. Get rid of the indirection by simply calling tlb_remove_table directly, and not going through the paravirt function pointers. Signed-off-by: Rik van Riel Suggested-by: Qi Zheng Tested-by: Manali Shukla --- 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 Feb 6 04:43: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: 13962185 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 B925CC0219B for ; Thu, 6 Feb 2025 04:45:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8CBC06B008A; Wed, 5 Feb 2025 23:45:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3BBD16B0098; Wed, 5 Feb 2025 23:45:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E6406B0089; Wed, 5 Feb 2025 23:45:12 -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 C5A336B0095 for ; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 810841C86DB for ; Thu, 6 Feb 2025 04:45:12 +0000 (UTC) X-FDA: 83088280464.18.58CF0BD Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf18.hostedemail.com (Postfix) with ESMTP id ED1041C000B for ; Thu, 6 Feb 2025 04:45:10 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.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=1738817111; 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=RQuwox4NCDjZmuGODsKLFwmLqtB/IejGr4b5qSu3yYE=; b=VaNdUoi4sm/Lw/JzzUtDF+m/jnc5Y0MGRmXQlMrB9FDwza0TQ20BUvPNTGshG2o3W36ZhI 5tcAeVs+vtaLdyqIavl/mxi1rNPKCyZ1+KWIYE5iYoY5eCDd6c1zT+oq2VeOq4RjBlgEju mMGY2VNWAe0xB6gqjOLfw0IcfWAN7NE= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=pass (imf18.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=1738817111; a=rsa-sha256; cv=none; b=8IGGiQcXqiCVkt4MNadvp1MwMDjtuKLgnNnnDpLE/+Muw9H0CPc7jfV+ROCUlYpdmj/iq4 VvvuQUqJLR+46/DVsszpwtOqmPVRoQi+35VnONXvJ+d6ucOwjWKB4R/g/JGHPuLKeDOIYp 1h05tM8rL+JT9LF2TUQH/rLcfBkG2NA= 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 1tftjw-000000004tQ-1w6p; Wed, 05 Feb 2025 23:43: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 v9 03/12] x86/mm: consolidate full flush threshold decision Date: Wed, 5 Feb 2025 23:43:22 -0500 Message-ID: <20250206044346.3810242-4-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: ED1041C000B X-Stat-Signature: s9wyihdmm9ofwwpt7y463ar5ouc5doj4 X-Rspam-User: X-HE-Tag: 1738817110-476141 X-HE-Meta: U2FsdGVkX1/SRxNdTBpLEPdWEcOrs8kwDgAEt6S7OrKQJC1NZjlh+krYxUlqQgUoCMNtuuYWE9XrYuKYxSybCSrUN7UywqDUiMiCF5Sx4IyglQ+4wfqnOd5BNesN/XO2A0JsRqBGA1XQ6vgL1qSW/5nLT3x1OdH+Rwel/GTjjh18mZIgBCF4lVvdRPaHxGeMs0CIUHQROUPz2mc9VW3/ydG9LtAmTf7DiKGU+avbhSUxYvTUy1BR+0ceGOKBsXGtWQi74MgCN9TQb46i3x80QoBFV0N6urHgo6n9WQ7UCsnRNxve6FDUY8DZILrn+IhH+cerXOr9hKaOlz3vp+G35iALnNwPiBC7kUp3no2OEBsRJlPIYBdLFEWLl7/0X/lZ2J+Zi7FVeoiCnbINo1belCsomVUGHmKdjCDjSqdCNf7KUttoyR1D3K7qhAmNKuiZaQuTsSz0KDdWWuieEMz9hYFRDhMwAv8KF3X5XBTYZgZFYPIm0Sfsl+Op1vEZ728PY5D1Mmj4pG/RvOE1Z5mEcr6TY+ZbE6wggtGUXb/rHEuOSb0X26t88vCTUw0PwWhlafQgZYJVjG9TetkxiehLRRsv7988mAs7y1ioImlZebJqVj4zggwhHpJvTkXppRzrZuz8pnl7Z2Z8XK3Kvcg96SLnXeUREPhXf+AwPd+se/R57KGixWlSP6r/T/T+mb6ADv7inT9k5FExGguBFPfp3QiFhvZ1IvTgw4YyvGuNBaJ55ZoJ7T+vMbAaKUopYEvs9rDtHblGVXPBaEuBGrBv5yH9GR75tNpz3mbqlUORdJzrELFVMOWK5O06VhrofVlY73I99tiUH2n0Ai9ElxZRdAfDARTNfP9/P/GAndGZWlXZDyGlfEOHOG+J6R7cncssmGRvf8h9o+B0Hn4PVWt6YOSbBnkYJC4QGiPxYm4nIzYKi2oPcvxEDSxanfpR/ox/1RV5/JP1mFpX9QlxfO6 VeaUHF4h p6QvDf3ZTf30dj0syl+++aC5GU8GFwc4mJX5plxuUSVllguP7BdzseR2PH0pbK0v3I6SoD163izJh+wLB6zHj8PDkNe4jEdlqVXg/JrLJYte7vx3IRVLvmXNajyIhGtpNCArgrouChGDU3inEe9UO6Izf+vxX1VKNqWWV3qDldHk8zOI6tU3+rnokL9XX9Gah3RPMcxMA2K0yoa038KJODO2VFRE1iJFdNazO3l4bliwnsXPiIIE1Hgl2RvXZB3oV9sOdFsECob/uQkNiGK1aNy7tSYhYZSjfv9m5RKSZUcpAxtzvx0bZ/EdRObKpBGfMto3QBX5mB2LKZt5Ht5EVMTL73MgN2/xArHSUeazzujskt/S8CJrXrngkIF400W/GDEDY1CH6Y7rilTQC17XMbFeLvU9MaA8u8IYTQZevALz1aGgyVKerQMtu0u1Aum1UzsqDJQPR0kz0IgVFDnPYVvI/Ve77wR9Yw64lJAyshLfJAzi7vROdSsTgRQ== 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 | 56 ++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 6cf881a942bb..36939b104561 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1000,8 +1000,13 @@ static struct flush_tlb_info *get_flush_tlb_info(struct mm_struct *mm, BUG_ON(this_cpu_inc_return(flush_tlb_info_idx) != 1); #endif - info->start = start; - info->end = end; + /* + * Round the start and end addresses to the page size specified + * by the stride shift. This ensures partial pages at the end of + * a range get fully invalidated. + */ + info->start = round_down(start, 1 << stride_shift); + info->end = round_up(end, 1 << stride_shift); info->mm = mm; info->stride_shift = stride_shift; info->freed_tables = freed_tables; @@ -1009,6 +1014,19 @@ static struct flush_tlb_info *get_flush_tlb_info(struct mm_struct *mm, info->initiating_cpu = smp_processor_id(); info->trim_cpumask = 0; + WARN_ONCE(start != info->start || end != info->end, + "TLB flush not stride %x aligned. Start %lx, end %lx\n", + 1 << stride_shift, start, end); + + /* + * 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 +1044,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 +1098,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 +1282,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 Feb 6 04:43: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: 13962187 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 94ED5C0219B for ; Thu, 6 Feb 2025 04:45:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F2F076B0095; Wed, 5 Feb 2025 23:45:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EB77C6B0096; Wed, 5 Feb 2025 23:45:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0AD46B0099; Wed, 5 Feb 2025 23:45:13 -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 84D116B0095 for ; Wed, 5 Feb 2025 23:45:13 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 410CEA0E97 for ; Thu, 6 Feb 2025 04:45:13 +0000 (UTC) X-FDA: 83088280506.12.9344653 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf12.hostedemail.com (Postfix) with ESMTP id B2EA540009 for ; Thu, 6 Feb 2025 04:45:11 +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=1738817111; 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=MJZfFPcR2XnyePdMtSV8xYvGCnHjzZ83M+Symc/4Er0=; b=MsxWlkTF9HDCKYXDmMLpezWyuyIAk0eoUxrn47W0FFCcm5GFyDAKNC0VzKoKJwclpid+ns ptuRq5rebJGV8MRm0+HLmTwHH0T/RfyU6OPj6JpAZXhhy3A3VUNvFQKu0sB1JvmTP8JWj7 jKZRERLzCja09Xh1wTmoV2zoiP+SUMo= 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=1738817111; a=rsa-sha256; cv=none; b=e34Vrxj37/Q+H8OgsMEuWQpPC4j1Ho05cZApiZmkrzbN/H+1L6/C5I2CLRKgYZiRDC6NJd n9nIuA1fxsQ7GM54QtU4EHdrwbQkeENLyw3cjtdfiweYesEi9ntLBjmbzcX6DaeQ571gdL bp4qH4aFvJPJzU5oX22hmV9h+FqvbVk= 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 1tftjw-000000004tQ-225I; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 04/12] x86/mm: get INVLPGB count max from CPUID Date: Wed, 5 Feb 2025 23:43:23 -0500 Message-ID: <20250206044346.3810242-5-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: B2EA540009 X-Stat-Signature: nx1tpxhdgxr9t7hp4epkwzr44zez3o9h X-Rspam-User: X-HE-Tag: 1738817111-411771 X-HE-Meta: U2FsdGVkX18sa/a1RUaQuIGFQgKDTWuotdRHB1EDpOYUNXBpiKnfJ1A5zbbhvxBSZA0h1Caq9b//rOPu24r6CYvGpgyVR7WXQpbzzrbovfqZs/5hSdmFRi/9CTxRVQLJkas2v6LLpAFav4o4S2xLxDxm6OOasVuLLupAjKnW0TbqdxinhQtkkZyOs/gSht1wisreBxCZDeWuFI9O/NHVrFo4gc38UGy91SARgIVq/THejUhFdaSTNFQDlOfuvVFZ5xlBQWNf954Of9zZZ8CJr4H0z6t153M1v8RJwj5yMXWAvI3eGAnMUjJCJVR7uzcYLmvlGJ/grhop1DfV5cfdz40uYPXTGzpUTpHRTgSJfZvaYPcHp/qifVAOoWfZhMPknJK7vMCY9m6Hm+KG9qAdcWh9o96fuHT9T+wH+ANl4UNysuuz1TYj33lf9ws7FeArfEKRxe9fIz/eM+cEQJXxJlp4hGzktZDeKo93eaLDgW8XNAbigQl1Qt+HsWgvp51/BqgxysAXf1kolXXVBGT4USGOPDmPSl8Jsx+NBTvAwgz01N7bhKvlEo1+/hZe8Vii1uE/UjvIIpejqLkJW7t0EgDrV+Xe+Uf9RD5xdaBzRBbndgwlISZmhu75LfXk+nWNxUdfRJz5Jz9s13HqeL7ehvsX6baVYWXN/eOexGxhtqUmjoyRboXlE81YHwfNjjwKZEHV6Vz/6u7qc8rprw1pEKtmrS2Mfm3LOV5nwRZ3/RWvtnIbAmx0J09SPBmuEhe7Lzu/gQYTu9oYtknwzhe3ZVLnnvlsCY3nIQhmnEQ6d7PUh2NTCEMmG8diApsZ6YdQx8ivZB09xw+Zj1YCQi9lyO9ArjislZK/Iw6E7Utd4P7m9hkd2vZb1WPCRUAS1zuegAniZG4c1/jzMsQWCQU2L42cNsKdCS5IhypenXuqoFKMUAgr6F3WWpTAWZljRsgjCi67He8hxrUWy3aZutf O+2jgOsP twCOFIsPmPBVPjGNH434m5wdMDE/sKlLz/wD+iRB96UnMXOpr6HVH2l4LRYcLDPAg4uqhNR8r2JbS86eSSCJU7LiUPQ9dR8b60q2xta7IINOdhwC6fcJZp8/xuh/ee+bhlAx9mEkQF38IAAV75s/xNqTuuNTY++fjr23RTvlln3H/WOwq/OVqTLLRfNT+mn6cYrC8Uw5ILMZTjhy0YY1OwJiZ/V1jRGmSEoYm1T5KRHL6HYSj0JhCoszPzOvqsPJFipEltASkKlucpzwBoHRphRYcvzpsJXjxE4/FuA/Bs9/RkftoxzV+gNHFaKzsbJxYEUgg0D+41oaMqFB8sc1ALMCUTbIRfHxUXCPvaf9feeP0qGmLcBI6eFwl6T5balyt+RBsPGlj7UfFgSEKSsR6TF8lBCz1kbDs62sAHu3N+VXRPSdsir2sF1lNxRaNFzAzzddT90YElae1ZjKFGx7FZjdlQxxiOQm6oxn6FI+l5RoH+A5jiOCgA7c3aw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The CPU advertises the maximum number of pages that can be shot down with one INVLPGB instruction in the CPUID data. Save that information for later use. Signed-off-by: Rik van Riel Tested-by: Manali Shukla --- 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 Feb 6 04:43: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: 13962182 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 EAB36C02196 for ; Thu, 6 Feb 2025 04:45:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1EB4A6B0092; Wed, 5 Feb 2025 23:45:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B89C86B008A; Wed, 5 Feb 2025 23:45:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9D7256B0082; Wed, 5 Feb 2025 23:45:12 -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 1EE826B0089 for ; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C2FEAC0E9D for ; Thu, 6 Feb 2025 04:45:11 +0000 (UTC) X-FDA: 83088280422.27.D33C2E7 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf19.hostedemail.com (Postfix) with ESMTP id 43A3F1A0006 for ; Thu, 6 Feb 2025 04:45:10 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf19.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738817110; 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=3+aEHRMJF/9osCoj8pjOdZfOr9CM4ctqqZt/YSTbXNA=; b=mJTBo5L4WVJLoatdz0oc08m19vF1+peO4ycM+VDfwtfdlH9GAzwM4ekBSMj0EVYtv67Yds 1wPHZa6zgFHPwXeTKxDUauTztV4bKzSU2GcrRtbQ2VCcAnLDvZLjKCKIeIm+uCGTI5Hz4/ XEUVtHbZEZ615VpmfzDsfkflT4mSWp0= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf19.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738817110; a=rsa-sha256; cv=none; b=OlDXmB31BMCH/t7izrUMwAYKKPwH07xjxXhEVOtb1zWBA07U95Clx0iAOwC+asJ7H0HcGE gzTYQOkQt8A0AM+u+QhdrThZ0vfDQorxcUB0z8qj4Z/BK+F3CyFxNgxwYVS6sxw1Ztw8eq mmNeVdnFexEhkc+uKsCIFaXA+W7KImI= 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 1tftjw-000000004tQ-27vB; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 05/12] x86/mm: add INVLPGB support code Date: Wed, 5 Feb 2025 23:43:24 -0500 Message-ID: <20250206044346.3810242-6-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 43A3F1A0006 X-Stat-Signature: dybcyxod8rbynq1opp1yawmxtdmqdoky X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1738817110-875427 X-HE-Meta: U2FsdGVkX18bSmP5DTShCG8XGDooNf03EWR/YtOqOu2Hmpysq5GMd9pBZGoYjcvwhGNLTkxNk2UKn0mHv9mQLY6/4rRvTrJKDr7E8KcwGUGWyB6Ar7r6zEfyUWDw2vgaFUmyg6KHHdVNgD9sCDeKAT/JHBraxMj5N/x1TM8mYIEx6wsWGR1rRYFi/KkabsCepm7Ho7cnJuM1NY6JiggIZ++Tpe+jQI+kjZgSmwetM85xoGjxN5SCSVyo/KTiUPs2Ge+XVX8NWYQ8HbaGdpmCLL6HVfwj1FazwdMjFpUI3jvyt0dMfnOGxkhzey0W4KOitzs/okKesWX6pe/S2nCAK88wBY446ifjj8idosYm56eQhXAfBH10MfxpONfb0hposwCP5TlgVqKnAbaficE+wxUMpvPKJyGmgxmtnvDdEgIR/s+1Srf2QdD4e1iAvI7rDgAdjQLX1HCt9YRnjwAt+HfmDGYxKX7Z/J+0gDtPoj1jYggJqRHaNuIfE2wWzKgRJhYEebu7liyy5Q12GZiASdEWSHngdwkcTpTWoLrfYAtdzjY/1kibh2KANG5TwGhEfpQ9EAcngE8lPfnyt0syheJAv3zYXnH48oabZDU4ozfzQekD1b5VSPYTeCalJH7ZJvgumfFIzRFBBmoCXwS85i+CqYqkZdCIyN4f9r9b/hQCZktJdD63reKu+rgeeoPttOFaSeEDHm0+727hwUUndI4B8oJTadggr+5YlDTf6VmIHrG2LYosATSBoOQxyYVFGgpovJBFISmb4CHix+xWmG0FX82dytvXrYtrTxot+ko5wbY6OPhSJoVLgBCBK1j4/Jgo0HOArB17rdhfh0Qql39XEz2DALToC7InmeVFvllMyr48vxcZyuOcIaP7VHCTg6HUxwT62LhQb+PVjL3RCQgVmVjV5FvuV4tuJEkTlWK91RaRW9GI7Uz8ht8vURFL7e2Xjq7U9CJr+5ie6VS m1+mSguH YJVo6l0oLR9GAlMLAUl8avIOOVFS4n00bHMMF+IKQP8hPY1wyah5dWT5ex94ypg81oDqhLWa8yFJiGrIOTb3NLiRSrhLje3h79vH/7+gdED6bdo4a0pZWWEutON6PgnJkpzPn1Jq+CulxG2NFxOoFi840kYWlCiOuj90ciYldCNfOu5h6UtNepVIX5Aozv6jSEQP+NKZoLYlmvFKJ2Mxb25FuqapHVsuWX+HRzKVwhyOcZq3V7boC4tLNu/4iEO4uSiZGTPY1iO1E+V2Pgl5VBB/I53kcKIibu5W03MS/LRWNlovfW0mU4NJuHIAHOkMP/Uw/EWMeEUXoZFUlV+HFJBHzB6DEx6cl+UYBuIRRC9+P+WxR0rCA7lwXDq9swK2tT34GAT1LizYAX8atwHFG1QOyswECCnIPEht5gknHATPhV5kQOxCLuCtxjCW+C4XA2lv8ka8a/H21pBqd4/IIhOgRwXzrPgWM897L2fKKKAmrxQCn6KYPPCfwIQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add invlpgb.h with the helper functions and definitions needed to use broadcast TLB invalidation on AMD EPYC 3 and newer CPUs. Signed-off-by: Rik van Riel Tested-by: Manali Shukla --- 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 Feb 6 04:43: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: 13962178 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 08E00C02196 for ; Thu, 6 Feb 2025 04:45:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 434A46B0093; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3969C6B0092; Wed, 5 Feb 2025 23:45:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 266266B0082; Wed, 5 Feb 2025 23:45:12 -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 086D26B0082 for ; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A44C7B18D9 for ; Thu, 6 Feb 2025 04:45:11 +0000 (UTC) X-FDA: 83088280422.09.04912C5 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf15.hostedemail.com (Postfix) with ESMTP id 26767A0004 for ; Thu, 6 Feb 2025 04:45:09 +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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738817110; 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=LwsWehzYjkF1dDnuvGvtM8fVrbVxjj6sQ0wzVHF9uqw=; b=M002Mv1v824hpnyKlkKpKmgb30zkLdIOxUxBTX3/TYP032m0OCR3y2N08JADWxXESuZsO6 gOQIaoH7jY6D5azTKhELETBjfbj9m39FjQUiMK+lamq0Un1wAfLs4qscG3ldjs0o/0vyHd buD9WhE0l5OA0dSX8PLes6Jhqc5Zml4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738817110; a=rsa-sha256; cv=none; b=lNgFE31qq5NAtPVrRXRapuLyUrxhUx7Rvr6ovOgW6xAIsqciptAVZr42NBC4n3oxJlVn/q ZqdsZ5xWmRBi4eEgCvS5NWooUN3s4+ZFmPtW1Qlh6eVkfOJsJbiLU1VkYOasx1I08vM4Br eXpIJnEEvRjhRdX79RRG8jXbfjeDlO4= 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 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 1tftjw-000000004tQ-2FVL; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 06/12] x86/mm: use INVLPGB for kernel TLB flushes Date: Wed, 5 Feb 2025 23:43:25 -0500 Message-ID: <20250206044346.3810242-7-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Stat-Signature: 54wbx8ipxkhedyp3y1hn6ciqgho6z6cg X-Rspam-User: X-Rspamd-Queue-Id: 26767A0004 X-Rspamd-Server: rspam03 X-HE-Tag: 1738817109-932806 X-HE-Meta: U2FsdGVkX18/UNQIKHtgyL2DNqK7uutlPuMM4MCQ73VjpMu8pxlnwELOXYByBcaPOh+M7pDLNlhWGjkRSGpwya/qoizUTocvxFr93uVNVHuThvARFrwI39edDHmRvf7WgoDbk5U4wwTLBOWzMMo1Sn79qaomF+HIguPJTtCKZEVK+9Hp65K2p0ULasmcTcHjlvXM9xXU03KZnhEdJZxfvJwX7ZTgatqyvNcnda9/kNv8kod3d4sLHTtQhyUIsaWKcd+quE+hx3j2MRfKqD0ncYtl6ekSdCPsPlNgArhKai7VF64WoDEp9EfYRgR6anXLz0DXB9c0C1onwCAETo3qLnlgVE2HSP/Y7wAQ+4gyL44ZTX94sjus5L0JbenaLrJnOFmt34pEDmg9gGpBkkYlm6sCyz/Ff/S9WcjFBU0mnuKnxwjAL9luIalnRveQTP3YhSOqe1z/ru4cXch0tZdbQ0AJhXrrW7vUbNWzMN3YYEwKIwHSSd3IhI3FPycwz56RcgzK51uDF8WA+oGB2+CT4efuk6ImsJnR1P0vDeN2jnVD2ERPVDRVzGflEsiS9PQJmKeIlu4N/Kb3manFIWyKwEIMpXbw1qmuHT1SsNdvsvTgDlXvYto8HBkE+zqUmcJRcytM1QJf9TZTFXLToqvwIUlicifWB01bA9EsV4GeUEVmLIy21MSOAEWRQ9/UYEWbYWnnwFhB62lbvTkfdrlAWrvUwIiyC0fNBN4RejSUcxSJz0113yK+GjNQUOLZcN3exWVTEpxys4ztR4XX8UCisIx93NmpP6tXWNmGNEWAxuRrw5CGBbcf9OOQohkiJeyt44RcVAbBScWquUc0q1mD8Lpg/WFCCRewe3PozhdLg8FdnKoxEvhDRm11l3luwQoQiz/blUWCXfNMRmz5+nIX4Z1AYKYYikn7CAlKEHku6qZ2qneCjtIjnEbO+kkYCwQ7YYZbqgacKALJpRGmO5S KMLTIuLu QfRiI3uiPYWVo+GhmMqu+ubAdM9DjHqdpVkW2XYu9VrHF0d/4Ykx8ZouL59dy48YXcomp1DxKoB3cTehTzD6qUrKeeSDg9ut57dFN0EETYaA2VDaKAEjXUdqvIhm3A5c3Po8RfINDnmuC5EGvoG7Zf08onJlwbSdx297r+x37vvr/3S2nounkD1vrxUzSzaWTww/FXUhNuewDrh9hQIS7Mj1S27OKkGwBNmVqHTDzso7rTxJiVXL76qEFByE2t5nJAhLDs5Stpmx0MLP7R3hMVXtafWD6/djzV/Sr5kT7yL/UXAPFOwsfGEgFPtT85MMt1Jq3Qfr4axUZPus376i7zNz7Myl2/LaNytjZYDNV+kIe6OzLS1hq1BPHXCDV9geZ9VoYm3nVslcuZFDXRTg+GzGVt4VdUzu2/ZowoFwg7aoRs8rer6fUsar/t6rSVKvKD8VL6rC79/Qy6CDI+A/IOQgBaI+0d7GSsKkszWQl1Noj5ErnVZhf4a1YkQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Use broadcast TLB invalidation for kernel addresses when available. Remove the need to send IPIs for kernel TLB flushes. Signed-off-by: Rik van Riel Tested-by: Manali Shukla --- 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 36939b104561..227e972b6fbc 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1086,6 +1086,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; @@ -1105,7 +1129,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 Feb 6 04:43: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: 13962183 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 96546C0219B for ; Thu, 6 Feb 2025 04:45:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 426326B0089; Wed, 5 Feb 2025 23:45:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D5A496B0082; Wed, 5 Feb 2025 23:45:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A4AA16B008C; Wed, 5 Feb 2025 23:45:12 -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 3157C6B008A for ; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D9AB74B2B9 for ; Thu, 6 Feb 2025 04:45:11 +0000 (UTC) X-FDA: 83088280422.23.0E35442 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf06.hostedemail.com (Postfix) with ESMTP id 58C1418000B for ; Thu, 6 Feb 2025 04:45:10 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=none; spf=pass (imf06.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=1738817110; 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=xvL+JboQGVeD7hbghaG8a20gYRkE19VBetmrAcsqGz4=; b=ejrSXvg6qMW/jeBIBev4JhTDF7LazIlcTkaSGNvd3/GhS5CxAVbr586688/Q/vYS0ESAaa gprvSttntaYQVXttbQVFujyE+yblGcubpeNLEVWLvPTmPMZdb4Xy+nUP4gjk/ov85e8nBA qMs4Rti5IOqgxx5bBO9eQSfsiyU87d0= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738817110; a=rsa-sha256; cv=none; b=VtfAHvoTBsJzBFerjfpXeIzKgmeqaJmzH63Yo/uyGuMgdeCnExjHQSMtvUsNWnJvaUSNgT Md+7HcCZfytKTBXIq7Mr+Dq0aEY6O1liEFuuZ4B8DFAim4qawD61wQRjqLVi/RWFryTRH8 7/Knp+4Dix5Mzkur/RzrNBwYKoRZyoo= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=none; spf=pass (imf06.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none 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 1tftjw-000000004tQ-2Ndp; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 07/12] x86/mm: use INVLPGB in flush_tlb_all Date: Wed, 5 Feb 2025 23:43:26 -0500 Message-ID: <20250206044346.3810242-8-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Stat-Signature: p7akxoim3bn48quczyajci4afzphb7ue X-Rspamd-Queue-Id: 58C1418000B X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1738817110-925619 X-HE-Meta: U2FsdGVkX19htZ2Uq8PBhoiwp/NoTXXAMJ6wOm4QCyg+8K53g0OSq8gffOxf3syv9M9uEKWra4FpFM7F9FPsLoQkxfR7z2I5Vzxe154sMz2HfOlGhHPzXluPHmv5pxPDZdGEdkrDlbFQ7VfyAvjZn29+fIk6pnIEYlMznZ7krCkNgIomHZpossyQFYiGvfw0uR1PdR03zFcd7q7iyeQoLgcZTLNmBsWyKnKSes5g+uUKfJPbLv8wzFY2tGBiXhCtRkqHW9sLmhY3ZvFMyq/VHCGSDRYPr3tiP2dzT8MWRv5a8C2EmT30M6lbj2uinX6YVnX+kP3dZOCYPvj1UsRFPx7eXJaF5VCjGamTEre70X8uA+5SOynY23NPX5qCEwEK93ZX/Fj0WGZSjqv5l+cqOAU2aRY66Nhi3fyZt1Y410mBpH5qgEIQYx4fmuNF4oWQqQ4yi9rfuSHkQEzLxBjj+Y8VLSQMznNPx7kp8Wjz/5i32C/tMjVP1g/77kpssRi7p21Zi28PQaZ6yTOlwGqxS0l1w31jnSdI5tnY1w3oPIO875aDLrEyOGHLezSPt3oG9C/jtAnsdGYHBGeuF8XrTByupzzJsqqMi+GlveSmzCuWDRk4o9ZyusbSAcaDb/ZstDegL2trmyWmNPo691u0f+8UIxbMnLjZ6z1sDOqpB9LgYjElQz8LJFIY0JKZ3dEjn1SxlCVyfUrJgOEp7A8kw0VRpZC9pd7io17vX/Fb1QOprzUxytuL9eg+2mY9F2cWRvO2JREDCCcOXYNgwAAkDaT6X19pWYG8WF0XP+FbFoSyQapd+tk6ZUUiq8dE7e5+wCYwKh+jGLifhOs0yEYApx3IPyq/sX391IwDXWG+zsGYQbt/0Tx/8BkFNFgHM41oWzW0tWXLE++frN6df8ay23AkzAagfiT53ejcH2iaufRDKuOy2loL7Bg93S4S8kGMfusZ1X5/y64AjLvb/f6 u+9xGgtZ 4SY6taB/tvVwRGXZiwwnpOmn+zEe9rhc4NTpAdYN2SjiOeeP5EDmpuLR5zbBtecuX6DmSh908A358PrMj5I1zMnuJs+3ng08naIRt8xIGezgHpeybutqolHV3sBD96sB5jfL9bk8Tx4C0JRsJxcXlJsj6tM4HZHc4rr0k7x/l2GLPgzp8Aa1eSbO91ahQepX8witn1x5LwafDA5zhC1Fd7OCry81ZaO+yN461wIpvIMjOQtp5Gm0ocWk8OfZ3iG53Exvtk3VGylzrwgqHI/FNVLcOKc2qwNyUE+gOR/hfKRqbO08/qtlFXmqjD1NNLcYNw0lI85uaagaJapGVf2k88f1aJsftKai5AzjYrpg8Ts1JZ2zEQ95kGbExadX5g+ADqbYGHQcES9jrXg9IIHJh9bFTd0ceH055wuqPuYgjPit9R3FYWJmCHXj4qrqDAGIo2O9cfu7AzsU/8i2hPzoHC9n5vZfgfSzD+VM90ZjUPZLflYRXrAYTVN+fHfgnB3/RYnjhQN4y8gyL72iuW/NR6yOTWEg4ysmG+iG7pORi3d3PxHfloiKnqYA6M/PtYfFo45Ei X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The flush_tlb_all() function is not used a whole lot, but we might as well use broadcast TLB flushing there, too. Signed-off-by: Rik van Riel Tested-by: Manali Shukla --- 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 227e972b6fbc..d225a3df9aa7 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1074,6 +1074,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); @@ -1082,6 +1095,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 Feb 6 04:43: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: 13962192 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 348EEC02196 for ; Thu, 6 Feb 2025 05:05:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BED356B0085; Thu, 6 Feb 2025 00:05:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B74FD6B0088; Thu, 6 Feb 2025 00:05:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A14F36B0089; Thu, 6 Feb 2025 00:05:04 -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 836C16B0085 for ; Thu, 6 Feb 2025 00:05:04 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 3775A1A0E75 for ; Thu, 6 Feb 2025 05:05:04 +0000 (UTC) X-FDA: 83088330528.15.8614C83 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf21.hostedemail.com (Postfix) with ESMTP id A80F41C0003 for ; Thu, 6 Feb 2025 05:05:02 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf21.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=1738818302; a=rsa-sha256; cv=none; b=ixD0BqhJ7APBRMZii61u0sf3jk6/hIjgkwAW+ktTmrUC6njzjUg5yxQ5ifq3uVJ4/vUt8M DFYpRt8TdsJAvqIUJ1plCqpcuOAzyoVsZZVjqknKBjHIShwuyfQ4pkrVZpb8ngL03Ntsfp YIUmDYpA6fESVZD/vUaNs3nCge36Fso= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf21.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=1738818302; 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=WowDIwnpgIEpwOsuWS97CLarZHxjKmlMZS46pitOn9s=; b=7pN+P8bT9SjxT8TOiqPpkOGRZuehedLCqVZzjHQqU6Mjiv1EDoP56yXxnxs3UEZsX2BeZA nZu2UCBZRHbzMF7DPQJSbLmBznVqWkwNTgISD5pgU4Mvt+a+a7ap8+u6+I7ovMpGzxvgXe /c/8O1T7qH/xzy9bXLJ4QV/TNSR7l1Y= 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 1tftjw-000000004tQ-2Tcd; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 08/12] x86/mm: use broadcast TLB flushing for page reclaim TLB flushing Date: Wed, 5 Feb 2025 23:43:27 -0500 Message-ID: <20250206044346.3810242-9-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: A80F41C0003 X-Stat-Signature: ocqsnjs7gp6156jn7b55bysdwuitozng X-HE-Tag: 1738818302-328216 X-HE-Meta: U2FsdGVkX181DLWPRTPtAVZKFZNP30sSpS3T0OMQG82nQLsLUCIS3pWKsr+QV8O4i5WMcS48W5DBvOL3wuHa94w61rMyC61RVw00jjaECsGs1g8ucbp3wJu8Sh8OGAox8IjMbOaASzUUk/W8k5pdbbA4fzux9qwUfTYicAzI4Tk/y/4jw4OWGqWtQhjcPT7e+ObwDkfv5pWIiYmuiKMcJX0qQSqR6ttUf+N32fNoIxyz+iIqNLxRqwXY6TqaBlYMjrr5DrlwwhGoUONU4wPlnCX9MwJ/+QK7mL823oJoPoyBw3aUvEJ6ZV/JxIhgNUgrgpC9u74Sse/Ix23d1pv+BZ+bDzXIGuLi3/L3YHNzxiQAjMV9JmaK49jI/jP36emEzfxVroKQEPq+//+hZNdhDtYPBT3K0/WSHMpd7FoRjxanWtfa/9ZzssP5DA0IGo3Jqd8MhZ03hfGwHEyM64f5IpuAfAxDURKJq+WUvGb4mUpr92K/aCaJqs/FrExgMbdbnMraDJaGhbmCaVebxtQ+YLj9pUZHljOK64uhDL87ZJfz6trlER6EsC05n/Ts/G1a8tA4b+lbJG2SVTvG9WUUxYaR5DgzmVITug3KhMPezIXEX3Q53rhtRdgUg7mQxMf9MrfS2U6Pt9PS6ig45T/NN2Z3FGwF5EDLRkGG1jdFOq+vtpgjXoXiIsvHmDNt7pyyZLPeyOzTH0ojLHMYVCyww/9MAG/j0OdY3Munli5mzTEjAgPI+kllNMQ1fZE4pNnfKx+efSyXHzVQYFvmX71Ge7rSuMSlnRBOexQvadlExUJU062hGj74TLkqcorbx4cy7htaTERRIsBYeXvyR5ezb+i4PCsBgdo+lLORvD5Hpj1snCBy0IxZ/NCfNdQ48/iq4//fu51fLcRnBqIUijBRlDW+J0ZBYdFvZDcDT+L4VRsXRuud96TumGFvai88+7Aqw/NPCarUlpfP5EL5/XX uBIjP8Gc oaj9nEH+lFdacslaXdfWLD9hho42ksXrbDqjSOX+q+N5vPjeF9LrtKfC7dUT0f9GIX3NOyg5HcFxkF5gqa7kUlg5qnSMW5NHhsg0wk/h1YKToV1i9YP23d6Rsn4ukKnk26nLQA2LQKHnKFaGxgTJYwS3pCt1dg27v1WAHtxOiTtjl88oKZecCKmXkZOm3hM6jvS7Y6Txt8hbGd5R6zy2c/RWV2qEbZTGKOpBIDQ3IwM+kwkJi8DIeEqtaPI0VIgGiGSzFR/5t5XsWjYwaTjFkwbq7ZeclbMtv4WK8sJ1CDv8tCYl8i6BmpBdWOrWUclpyX15SW+QAz+tsoAMFCL1FobCPhRWgbxKd8eXv/J3O26sRxx7fk9rvCOcLM4ldoy9BGrFEGsRBxyPVIHIAFWSAhTkSM1cuo3GakiFs1rekwJFPB5DgOUKLNVPW1TQY2BKaXTrqzU3x+sairfRsTbOEs/hkoByVIT13z6GZAe1V/JegQRA7B7MkHF49uQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: In the page reclaim code, we only track the CPU(s) where the TLB needs to be flushed, rather than all the individual mappings that may be getting invalidated. Use broadcast TLB flushing when that is available. Signed-off-by: Rik van Riel Tested-by: Manali Shukla --- 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 d225a3df9aa7..f2d18f16d76f 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1330,7 +1330,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 Feb 6 04:43: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: 13962198 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 35106C02196 for ; Thu, 6 Feb 2025 05:15:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A21F06B0082; Thu, 6 Feb 2025 00:15:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D2156B0083; Thu, 6 Feb 2025 00:15:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 873666B0085; Thu, 6 Feb 2025 00:15:10 -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 63B336B0082 for ; Thu, 6 Feb 2025 00:15:10 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 0D2501A0E88 for ; Thu, 6 Feb 2025 05:15:10 +0000 (UTC) X-FDA: 83088355980.14.4CE3F14 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf15.hostedemail.com (Postfix) with ESMTP id 64C9FA0009 for ; Thu, 6 Feb 2025 05:15:08 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=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; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738818908; 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=Aqd+z96bD10YIiPJgKo8zOH8Bc3sMIAmXrTLeVT4lM0=; b=7kBhlgkYWYGsWUzp5Lbr+LpyMctvu63VOMueW7T+coRiiPrHaZaPs8LTPtmdBc16BbPy8C JYFDGnMDsJLCF43rHBrmcxla35h9K6IdqqFvaJ9Z6kNDZSWGujLFKDHp2gyA/GRJr+Q2i4 pdPEXXQEm0rmzM30rqwvr+p2c6QFn64= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=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; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738818908; a=rsa-sha256; cv=none; b=ak6iiWnYK1DYLEFSJddPWNJerUNPa7WWDdUlbLoCl0Y4IIuHJDwnv5fbHhVhcLMld3/TlJ HBmqmLpqHIk5/OpbsSXsNaMX/Owj6bXD/AnqHf5K1WaQoIhfTWuF5/AJ33VdDe1pcURljJ SbYo22HFIU2B0gKBjuRKO0IEZMsd9eY= 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 1tftjw-000000004tQ-2aZg; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 09/12] x86/mm: enable broadcast TLB invalidation for multi-threaded processes Date: Wed, 5 Feb 2025 23:43:28 -0500 Message-ID: <20250206044346.3810242-10-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 64C9FA0009 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: nqxfa4dwqn5agjers3mpjn91unmennko X-HE-Tag: 1738818908-506978 X-HE-Meta: U2FsdGVkX19wjVHppTdqskm5q7F02pb0PIiI9kMC3JSDiw51j/DWnPdfHeQm54wmjK2II0mLv7sRDnZy+NN1nmAQYbiShP161OiUEmZbgzMNVE3PmQW5xcjz/3hI8/yS9GfoGjX9M4w6EPT+8WUXILKkE//EjSmfv+EBf0GztA0HXEPIKODaISgcvtytqo8br7IZ6sVVLCnE0keMn8Arh1WWb4BKeWp3ES78+ICAYoSP4D9CWmWiH3YflglDuvJhnp/dgDFM4MdSudPS6LEIb2tiZZf5RRfBkw3KXT8qZxhHybsFMLRKC//gbHm152T7FrMPYEncm4EPMio8uPeJaJrY4hQ1qUCr20cFMkLjm5YoyYYm2+QC7HSF3KLcmBS54c3Mw2z9nHUmkuFKS61LksTZ2uakeSsuANxf+hGqibSdDhyUIl41gamzRvn86BSZliSNMMYs9mOYw9phaOhf36D4IatNOFkDMaivMbJvbGwykEegZVIPTc3Q4AY37AM5+aBsDS4T00Rr26uxbdlXj1tiJbOjy5+AOIBR7jf9x9cji+4GzS9Dn5KymYwZIA0C8Ef19pHHTn3iTBjiaX/QAQR4nPa29X2S/PeKzufFS/o6LrpWrcmtP61DN+PxUYjMXiYTDmjUEAQuKypI6dtsLwQymwJOSz/fArvKg5PPfDgJ8GK5wyl/tQ8wucO061HYfPpcpnWE373S8p1j8/kQ1dLutgtondFvFJhxVixwxsJafJCDzypL5zJhvj9q6qgkdd4CCHV2cmAhQ7TCx9jEA80T66sLpE1ebOqUKTG4gvJJBI8OT9EPwDfPffyRAgXy/a8lH2cGvxNmGkVUov9z1WMugvkkapTg2zJB1IZShoPdj1Zm1BGUrVata3w4vupBTlBnhiMpPjNO4gl58YFOdV/vEPJpOg8Wt/oX9yiIxxpfQ7QTcXpDVXGmoeF0NyfF1TDFVVfOpNx924yLrAg sZRPTTkK Y6yhTULfa5aA/JKb5bjdrxH6CS4I/Lhl+iap7R5C8wn3mR32a4uig0nf9ySoH8cEFzXj9dIq8pxE/XfhUZfVCIgLGfi8N4LIbAIE/dzAd7vKHTJ7z8FprNRgk69uJvSKxPRe6jPv8vIM4n42JbBROu1PN0wxHLDrVKX7GHFLUf11wSqo7Xe4Q3q5Fa5v2+ugDCJwP7h38noE3JPu/NyEb6Roqh4MrUoELPkVcxoe8cDuIrZjWEw9JTwk2zBmjR36IZRwYfhssa2WyDs83o+u1YWxvq5SyHqYefm2jU1G4ZaYtOOtwbB5XZBqmFfwISEg7vc3UGnVSIDIsWUq+YgtYlvgiY6KfxEUr5rw6U/7BNEoMTKdiBiyIT7ESjttEjhsVIQsXb39X9IZjnhUk0eqf6RW2XDqCBEMx7n044eA54HfLyW5WEI1ODrgNpP8Vg99FTbKCDR6jfGW/l1zl6corB2+VuiLNyzVDj48CgrskUPVwZknH0KLstLS1lg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Use broadcast TLB invalidation, using the INVPLGB instruction, on AMD EPYC 3 and newer CPUs. In order to not exhaust PCID space, and keep TLB flushes local for single threaded processes, we only hand out broadcast ASIDs to processes active on 4 or more CPUs. Signed-off-by: Rik van Riel Tested-by: Manali Shukla --- 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..234277a5ef89 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -239,6 +240,78 @@ void flush_tlb_one_kernel(unsigned long addr); void flush_tlb_multi(const struct cpumask *cpumask, const struct flush_tlb_info *info); +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH +static inline bool is_dyn_asid(u16 asid) +{ + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return true; + + return asid < TLB_NR_DYN_ASIDS; +} + +static inline bool is_global_asid(u16 asid) +{ + return !is_dyn_asid(asid); +} + +static inline bool in_asid_transition(struct mm_struct *mm) +{ + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return false; + + return mm && READ_ONCE(mm->context.asid_transition); +} + +static inline u16 mm_global_asid(struct mm_struct *mm) +{ + u16 asid; + + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return 0; + + asid = smp_load_acquire(&mm->context.global_asid); + + /* mm->context.global_asid is either 0, or a global ASID */ + VM_WARN_ON_ONCE(asid && is_dyn_asid(asid)); + + return asid; +} +#else +static inline bool is_dyn_asid(u16 asid) +{ + return true; +} + +static inline bool is_global_asid(u16 asid) +{ + return false; +} + +static inline bool in_asid_transition(struct mm_struct *mm) +{ + return false; +} + +static inline u16 mm_global_asid(struct mm_struct *mm) +{ + return 0; +} + +static inline bool needs_global_asid_reload(struct mm_struct *next, u16 prev_asid) +{ + return false; +} + +static inline void broadcast_tlb_flush(struct flush_tlb_info *info) +{ + VM_WARN_ON_ONCE(1); +} + +static inline void consider_global_asid(struct mm_struct *mm) +{ +} +#endif + #ifdef CONFIG_PARAVIRT #include #endif diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index f2d18f16d76f..05390f0e6cb0 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->mm)) on_each_cpu_mask(cpumask, flush_tlb_func, (void *)info, true); else on_each_cpu_cond_mask(should_flush_tlb, flush_tlb_func, @@ -1058,9 +1375,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 Feb 6 04:43: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: 13962188 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 3BFA2C02196 for ; Thu, 6 Feb 2025 04:55:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A555A6B0083; Wed, 5 Feb 2025 23:55:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A052C6B0085; Wed, 5 Feb 2025 23:55:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8CC3F6B0088; Wed, 5 Feb 2025 23:55:13 -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 6F4336B0083 for ; Wed, 5 Feb 2025 23:55:13 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 18F47160E74 for ; Thu, 6 Feb 2025 04:55:13 +0000 (UTC) X-FDA: 83088305706.04.0B23A9F Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf18.hostedemail.com (Postfix) with ESMTP id 7C9411C0007 for ; Thu, 6 Feb 2025 04:55:11 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf18.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=1738817711; 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=0UcZbWosEpa3X2E55QBH/yWmXX34hID/6W2F8SaA1mk=; b=6dhjLIVGu7W/c5NPwU/yOfNN9rrWSsw4QcSEhedX3kKg67YbZb502Hnvwwf/KPL8aQ4wBH 8KZcqu84fk48hRuhZp3TlPyCeyOBQhv0rBhzMIl2IYNMy8LIzo5ZWHlDwqQUPZYb5dZrcC onniP0iah7sIeiqQTMzZtOYL0u3m0ro= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf18.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=1738817711; a=rsa-sha256; cv=none; b=rzAb9akHyaO1EAaHjVwv7/ZKhQMHSE97N+ksJxwTyslMIexOSjMtqEmQofnutHt5d53vA2 IP4nMPwj4cjMeXI2KYgf4e0NwqM92lvYJq6vvva+qJNg+ooAYtSuWnUjHQqB2Nxva1voIX Y+g7rDB/SDqPptC1usaN2Tf5vqJrQis= 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 1tftjw-000000004tQ-2igC; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 10/12] x86/mm: do targeted broadcast flushing from tlbbatch code Date: Wed, 5 Feb 2025 23:43:29 -0500 Message-ID: <20250206044346.3810242-11-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 7C9411C0007 X-Stat-Signature: yzn6k69omq18rnexadpb4bqniqi8wni4 X-Rspam-User: X-HE-Tag: 1738817711-497525 X-HE-Meta: U2FsdGVkX19wd+HH21MTMeUqon9xh+FWHG0yFBNWBeBu5tvi6SZQpoL5jkdVSqxAxmqYnvduV6uIRe3BWtl9HcRFg2kK/fLeh159tfYoM6ZpJdCM1aJTnj5N93jA3bEl3pZfdjIZCSQl+n8oeLKFCEYOaefUTFigDvNKo6t7rYPqBSpvEF91w5vsZ9DOtU4N9KOKVYCJX0oYrSndQDpB/ztDknG+i3RV70h55beVJTjETmp6iZhmn5Ql5d8DKXu4YIGns3Fv5dDNCwLsH6uHb6km7Gt8pAZcNm5gV5r8mZjs5GTotpb20R/imSjWstJcmSjMvjAMXJeYxiCWweEngpxXWdAV+yIard32p28uJXTWiIjoIFOW9ciAAbu3AMrrmqJgZgLKc0PHPgh1Y4RnAqNWmz28sswOpQ9/EqHXWfRtivSCac7Tp7f+OfN10txHDefhG3iZ3x9ZC8MEsZ/7UutbNJdWnPLCB1Q4VVifJZRwjTjuluCWbPjSC7c5kwIqG7lvqpaqwYurAXkq4aFVvIYse2vikwBqZkHbU/goiCyn9PDqg17VzY5S7QrQdBtDeALqJpjAnixjXun664JYGTjIMBZZgaoBY8Oeyo23T/STDL26sNIszZwTkXtwUVHMvP4pnb4o5KWuHBPauflXcV7CjjETQb++2oAotwNL6cu5bufWqV5pab5l9nwGHC+I0cldePU4X4do1KJ936fmWr02lPkVb9JUvL//X2JRKr/jZaHiWDnteWR8SSc5dR4jZ68X7IS4BgmhWhUw4F2zbkgWsPsem9Z5vWliR3R4sslkyRThLeUMovcvwvDu5ROY2NTuulm1ipK7I9Diyf73Xs/HHHfnEGJKiqoWZrGg+TMCtxixI5917iMYnkF3mkk65MHS0EO2XzXYuF+sTnjJGidbhtBnjR5reqJnh0O7wybOaF/+KMuKb7tYW57F3QjIaLe5csQD5PXrnUBW5vg nNi1jACG 5XvNDLqjlW9djKEAvpkoKGJrhGvydokg5zDuIYThLEMpebJABUR05kFHk1CATukab2rY/5197GaPp54m9Frz/5W0BHweAgdToPlRYruvFZLb5sE7xhwtT1mUH42PFtqbixKKcq5b49ot9UrDUwEGxFwFe4zpBI3ODNn2K8MchDsbrolYmgIoK1OBHYC66uUp7fTkEQUZ4UNiUwfdJLCGBoNBhIdrQDJ2l138/hr/i+LcDpp4MEMD6Voz/2TDEccKeLeqoyPumjCHVwJBBUom93B8ZTLJxZGlJjBAmNB5dNGpDbVLrMZujeixdY3UwJ9Q8+KVqUI8XrsupCpVt44ihBAK8jHcjzKK25Iyoz0sp+GDVvCWv4P3k4OOiNKaD+28NT1gY5fVc9rj2/RojuVcZt6eWvpvIRdqK5V+7u3cGS3/3ZzAAiDwIr8ckhy4WqP+4deoSkHyWbCTdxNxWyV/oWvVEdqGShQOgohBxba0HL7mx9nrl705HVxQ/bw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Instead of doing a system-wide TLB flush from arch_tlbbatch_flush, queue up asynchronous, targeted flushes from arch_tlbbatch_add_pending. This also allows us to avoid adding the CPUs of processes using broadcast flushing to the batch->cpumask, and will hopefully further reduce TLB flushing from the reclaim and compaction paths. Signed-off-by: Rik van Riel Tested-by: Manali Shukla --- arch/x86/include/asm/invlpgb.h | 21 +++++---- arch/x86/include/asm/tlbflush.h | 17 ++++--- arch/x86/mm/tlb.c | 80 +++++++++++++++++++++++++++++++-- 3 files changed, 95 insertions(+), 23 deletions(-) diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h index a1d5dedd5217..357e3cc417e4 100644 --- a/arch/x86/include/asm/invlpgb.h +++ b/arch/x86/include/asm/invlpgb.h @@ -31,9 +31,8 @@ static inline void __invlpgb(unsigned long asid, unsigned long pcid, } /* Wait for INVLPGB originated by this CPU to complete. */ -static inline void tlbsync(void) +static inline void __tlbsync(void) { - cant_migrate(); /* TLBSYNC: supported in binutils >= 0.36. */ asm volatile(".byte 0x0f, 0x01, 0xff" ::: "memory"); } @@ -61,19 +60,19 @@ static inline void invlpgb_flush_user(unsigned long pcid, unsigned long addr) { __invlpgb(0, pcid, addr, 0, 0, INVLPGB_PCID | INVLPGB_VA); - tlbsync(); + __tlbsync(); } -static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, - unsigned long addr, - u16 nr, - bool pmd_stride) +static inline void __invlpgb_flush_user_nr_nosync(unsigned long pcid, + unsigned long addr, + u16 nr, + bool pmd_stride) { __invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA); } /* Flush all mappings for a given PCID, not including globals. */ -static inline void invlpgb_flush_single_pcid_nosync(unsigned long pcid) +static inline void __invlpgb_flush_single_pcid_nosync(unsigned long pcid) { __invlpgb(0, pcid, 0, 0, 0, INVLPGB_PCID); } @@ -82,11 +81,11 @@ static inline void invlpgb_flush_single_pcid_nosync(unsigned long pcid) static inline void invlpgb_flush_all(void) { __invlpgb(0, 0, 0, 0, 0, INVLPGB_INCLUDE_GLOBAL); - tlbsync(); + __tlbsync(); } /* Flush addr, including globals, for all PCIDs. */ -static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) +static inline void __invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) { __invlpgb(0, 0, addr, nr - 1, 0, INVLPGB_INCLUDE_GLOBAL); } @@ -95,7 +94,7 @@ static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) static inline void invlpgb_flush_all_nonglobals(void) { __invlpgb(0, 0, 0, 0, 0, 0); - tlbsync(); + __tlbsync(); } #endif /* _ASM_X86_INVLPGB */ diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 234277a5ef89..bf167e215e8e 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -106,6 +106,7 @@ struct tlb_state { * need to be invalidated. */ bool invalidate_other; + bool need_tlbsync; #ifdef CONFIG_ADDRESS_MASKING /* @@ -310,6 +311,10 @@ static inline void broadcast_tlb_flush(struct flush_tlb_info *info) static inline void consider_global_asid(struct mm_struct *mm) { } + +static inline void tlbsync(void) +{ +} #endif #ifdef CONFIG_PARAVIRT @@ -359,21 +364,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 05390f0e6cb0..4253c3efd7e4 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -488,6 +488,37 @@ static void finish_asid_transition(struct flush_tlb_info *info) WRITE_ONCE(mm->context.asid_transition, false); } +static inline void tlbsync(void) +{ + if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) + return; + __tlbsync(); + this_cpu_write(cpu_tlbstate.need_tlbsync, false); +} + +static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, + unsigned long addr, + u16 nr, bool pmd_stride) +{ + __invlpgb_flush_user_nr_nosync(pcid, addr, nr, pmd_stride); + if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) + this_cpu_write(cpu_tlbstate.need_tlbsync, true); +} + +static inline void invlpgb_flush_single_pcid_nosync(unsigned long pcid) +{ + __invlpgb_flush_single_pcid_nosync(pcid); + if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) + this_cpu_write(cpu_tlbstate.need_tlbsync, true); +} + +static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) +{ + __invlpgb_flush_addr_nosync(addr, nr); + if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) + this_cpu_write(cpu_tlbstate.need_tlbsync, true); +} + static void broadcast_tlb_flush(struct flush_tlb_info *info) { bool pmd = info->stride_shift == PMD_SHIFT; @@ -794,6 +825,8 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, if (IS_ENABLED(CONFIG_PROVE_LOCKING)) WARN_ON_ONCE(!irqs_disabled()); + tlbsync(); + /* * Verify that CR3 is what we think it is. This will catch * hypothetical buggy code that directly switches to swapper_pg_dir @@ -973,6 +1006,8 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, */ void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk) { + tlbsync(); + if (this_cpu_read(cpu_tlbstate.loaded_mm) == &init_mm) return; @@ -1650,9 +1685,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(); @@ -1661,12 +1694,53 @@ 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)) + tlbsync(); + cpumask_clear(&batch->cpumask); put_flush_tlb_info(); put_cpu(); } +void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, + struct mm_struct *mm, + unsigned long uaddr) +{ + u16 asid = mm_global_asid(mm); + + if (asid) { + invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false); + /* Do any CPUs supporting INVLPGB need PTI? */ + if (static_cpu_has(X86_FEATURE_PTI)) + invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false); + + /* + * Some CPUs might still be using a local ASID for this + * process, and require IPIs, while others are using the + * global ASID. + * + * In this corner case we need to do both the broadcast + * TLB invalidation, and send IPIs. The IPIs will help + * stragglers transition to the broadcast ASID. + */ + if (in_asid_transition(mm)) + asid = 0; + } + + if (!asid) { + inc_mm_tlb_gen(mm); + cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm)); + } + + mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); +} + /* * Blindly accessing user memory from NMI context can be dangerous * if we're in the middle of switching the current user task or From patchwork Thu Feb 6 04:43: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: 13962181 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 AB167C02198 for ; Thu, 6 Feb 2025 04:45:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E40C66B0085; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 986F46B0096; Wed, 5 Feb 2025 23:45:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5DCC26B0082; Wed, 5 Feb 2025 23:45:12 -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 0D4A26B0083 for ; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7ECAE80459 for ; Thu, 6 Feb 2025 04:45:11 +0000 (UTC) X-FDA: 83088280422.19.8987AD8 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf01.hostedemail.com (Postfix) with ESMTP id E47CF40006 for ; Thu, 6 Feb 2025 04:45:08 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738817110; 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=OzOWNXCbdygkBtJqfnE6pzP7kkpaLLgmCvcR9CDWv5c=; b=qnEVuR9aPZypqDo0zIKO/q2dwUBINmB9IevYk37/M55OCI7Mk3trA/D2CWl+NwfTJM8P1a Vn+mGQSQKx/c9xSNZ0TtR4DoamrTLrN0h/ZETGS5gEd4c8JwGrP9KBSuE+ryHlXwwOaChw n1kJ7VLEZaqHGGp9S1i4JaGOQBO9ECQ= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=none; spf=pass (imf01.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738817110; a=rsa-sha256; cv=none; b=0oiSnivISChdWriSq/kkW3GC09GRbaPepwPZ+VdcyvuCZIrvRj+l0ETRfqv7DZzb2M7XI4 1vroPmkMxnOgeWB8g+KNNTNoeyoLwIOpgVIgr6HbK1iEDtZCbpMVLJt/+ePrTQxnNmTCJt Cvoun4va87inihCEn2Hj8ekQoTagt2A= 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 1tftjw-000000004tQ-2pH3; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 11/12] x86/mm: enable AMD translation cache extensions Date: Wed, 5 Feb 2025 23:43:30 -0500 Message-ID: <20250206044346.3810242-12-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: E47CF40006 X-Stat-Signature: sd8itgb956ba48adisk7j7zsfcbbunp7 X-HE-Tag: 1738817108-752376 X-HE-Meta: U2FsdGVkX1+mNqJFn/xoVp2D7a8FjLU8S9Sb15RlA/d8rm3oD0r5vZ9awZ3vB1SKPX+DnTZGRKq2Es98bgCCqbJGitkZekWOxbRg+Z8zVTETJgey/w3mhlppw0njjR/9qZ+caCOZUS7M7JIupIno10qRztIwRf6aldldbZ1nWCmjhXOZxAmA4mOmMhKd7YHQImbImz+yBZ64JyyjiqrZnRHNUwz24NIbuqpUSJqFTJP8COeiVGVGyNCmCr98QCO+ZxeWaEYy8S8Zmx5IX/JCFi4iYb0XkHlQJO+/6N8PZaiRhg/KXYLBMGGvih9AlTxNICvLQOq67gD1my7ZhcYXdR3mrD4/5DWcWOHjmKprD4Hq+It4Nc13cJ9GpGup78DdkyWHeQ41NIKuE21NtghOTtpQ+XEF1c5uQAbO4IEYdXi7/12sn4c2Cg7kXtdGWuRHJNuesqNJAfRCUkMG4Z9JQEOCK+Vdqk4MzdJxUYP83AJMR5Zv8PGrgAyqhRv0ec1fMMvyxPjrv5klOUUe31cgeh2VdsPur+ncfCeGin2Hq05XyfI7Is5C9Jqy8oewSfXJplQICGk+3EDNeFORlF3NewZM3aSNdKOkpjN1itayR/AFFIhJUaVPFeL1pguFDX0xPRpJgIrU5ch6RPSpVgscCjTi//GOPBF1vEJxAHeZT7u2k5hwN4i9D2sIB+eYyIpCJyYnKBwq0OeSkK9rd0GtfcUcbmhcAEwK5tiM3msBVA/EvsSUQG4tgOCS5HzOWVw3EqqHLNAxZyRYlqNIq7EPSKKS+uG0+EKuZKVSFsErjM0tV5epwelEVh1iAe6oj2stXQiYE1+9tOUE5Qh6Tjvox9h5f8jVgrDOcn0Q3iKSHBNALwUaAbFSeln8Jab61iFdRmH9uKmPOOEXro3IP2R7vw02xW2CJPaO2c7U4Ij5NMQYrla+mQJ5igJ2MfgtB1WyakNcivCsv3nI9qXvCLX Lwk+zaFS epG+YwTw7pJ/ScKrKZ0RRBC5TFOdLdwtcEfPvEZ5zw8JH89TDW8eQXB5ZMnV0TjKfPAkRq76ndWLqaWfHXRZdg0fC5NFLNZno//0tlUQR2hVUJNtNrcthVeoZ0AVw+FAwE/u9m/3D0WHS2lLCrhI+r8M2AklH3h4L11LN6b2WrWmDa3deuMePaSfH8szqsgO6/gzj59qPvg0gTbg4qTMW7ndG6V/0xpiM5pN0FeRGnL5MlzJM1QCjNsqGxHH+KlTWG5GlRhU/rXWVfCC/WkZl+D1EyujDhzpzGFpwNwymF26bf6k4ky2h3JqlDERWF7U3NdFrvJLxsqqPEO+YgEnCb2dfP77If40R0JA9LEW1Un45/ykekP+0HWKfirbuZPo0VEQ0GsVNST6AH8whxWSLe8GtZUDQNaXbTQ/14qJoyktF/hCsUj394+pVRJTb1He0bs8S0cggzxQ1Kxk6e/s5VbxUZxwrRPeMZBFJCN7QjGMWbNF905TIXOLPig== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: With AMD TCE (translation cache extensions) only the intermediate mappings that cover the address range zapped by INVLPG / INVLPGB get invalidated, rather than all intermediate mappings getting zapped at every TLB invalidation. This can help reduce the TLB miss rate, by keeping more intermediate mappings in the cache. From the AMD manual: Translation Cache Extension (TCE) Bit. Bit 15, read/write. Setting this bit to 1 changes how the INVLPG, INVLPGB, and INVPCID instructions operate on TLB entries. When this bit is 0, these instructions remove the target PTE from the TLB as well as all upper-level table entries that are cached in the TLB, whether or not they are associated with the target PTE. When this bit is set, these instructions will remove the target PTE and only those upper-level entries that lead to the target PTE in the page table hierarchy, leaving unrelated upper-level entries intact. Signed-off-by: Rik van Riel Tested-by: Manali Shukla --- 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 Feb 6 04:43: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: 13962179 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 F318BC0219B for ; Thu, 6 Feb 2025 04:45:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7209D6B0088; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 679116B0083; Wed, 5 Feb 2025 23:45:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42F566B008C; Wed, 5 Feb 2025 23:45:12 -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 112A46B0085 for ; Wed, 5 Feb 2025 23:45:12 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A96054B2BA for ; Thu, 6 Feb 2025 04:45:11 +0000 (UTC) X-FDA: 83088280422.08.CEC0862 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf25.hostedemail.com (Postfix) with ESMTP id EC89DA0006 for ; Thu, 6 Feb 2025 04:45:09 +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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738817110; a=rsa-sha256; cv=none; b=rOqbWW+BiWsicbFN+uqWzFZzxJx3FQIkWMMGlrzcrS1iVRfRNyFy5BwsCiMFQxfhXV8Lx0 984696IYig0I4wa+BZM6+GK+wZI08eS5dCFrkHwpflQL23LG2xmYKjq+Zu0Vn7kRQVr+IT 1BR6dbMo9D5LhC5NQnAHf1fq18JNdAE= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf25.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738817110; 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=Q4bAwfsuDmxWS9pEuNJnAJuyAkhvxVxPhIXeu22xxqo=; b=KHktb5SBWE19I8p1rNj+8c2gRnF88OgvdPytMrQvElVot/eltnR4OzRpU+9+riopa/2lri 8Vno5X7wrmbb2wFc/H6+ZHmDehdY6Kkw+zj2O4G0eBZQczUUvFEAJQ/EIXfvgvLTyqQIqa F35ur458RsgfwoGa/EA6EvmWRvCgm/A= 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 1tftjw-000000004tQ-2vlx; Wed, 05 Feb 2025 23:43: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 , Manali Shukla Subject: [PATCH v9 12/12] x86/mm: only invalidate final translations with INVLPGB Date: Wed, 5 Feb 2025 23:43:31 -0500 Message-ID: <20250206044346.3810242-13-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250206044346.3810242-1-riel@surriel.com> References: <20250206044346.3810242-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: EC89DA0006 X-Stat-Signature: 763cdm5c1oui6ux5w6kpkhhe7hfx6zuh X-HE-Tag: 1738817109-400251 X-HE-Meta: U2FsdGVkX1/j8WYS2fJBxm4ksACMB1peFFPu5ITE4ICwr/hPaiKFk8E5zK1O/ROv7JF2disAELPvwNqZO7Nzqw8rZj4RR1fgtrThFzB3S95VRMf9sSCHnOlNGxvvTUq1nWtefexie5rgAl3MZkEC40Nk+ZSXRdQJHEbkTF5QjBfxgN3YbTmGm6v/o/2uihkU0n6ATYJkeYcWu40R1QY92Hth6VWucKx1SCkrG/wWoyL+2liAZmuUTTY8hhgkl5bVVTQVYvBSFXDn18cyqvGBvJL8R8sszvW4b8MsBIbuDU5YoLZJ3DU9K+zMW+Wx0LKyWCIt+FX/rDs9m6oL8760kFFgCYzF/VXxgsUuKaDNX0gU46YR3sT6fl2BwOIVIEbY7EXLDbtT/0YlIe8La7q8Z5hubhi7IXDCiwOxTJfN65NTLPJS+fEiQaMqQGRBEcinE1nQTPWBH1hPa/REASox0Rwa1gacCsNri4DCbMpZKu/DS+5jnp8wH7vz2oBqWVnEntEiu6km0E1R7YoRy4XW8OV4d3OwM3Paukv+NbW4FEEiGApRFiRW7KZqK1wrE97Sn0JG1kZ66pwhMqB4Q5lcrGJB+XSB6HkwUxR0lIDSLs3w3al3ZYhaAWv8sQydczgW+LdViSQOf4z5tp/xX09P4ToFy0TLP6Yc06SeHZupXOqwR56SiC6hJ3aWYLBCodl2rTjT7pjHjnJ2E9vVJlQQlViAFWucIhg1axsB6ss80mxWqPSfvmxssMAITIsYXqzCuy02gPpn/V4C24xAPY6RHLAVGZT0dbQ30D6TC/pWEg/uCW6789bwPSNA8ZXw9ucJvjT1MO5MdERHSTegrxIxTQRyEmLwNVj4dHAEUzwQJpvID6kvZCPrjMPG4FPM+OoUgO1L1+pDPZGlyYGhO5klZI5suMZeDfWpC9aZ0ZrXzbtrkczKL8+gmfE3nP/QDWdg7wpTpR4xoVTEAHtBUWD wpw97wLR LQtTsQO508J1RqAPDvYqQrB+o2P1daFRhUnv0XiPGyYltSTPwJJH3S+KojSNX078NoRHTsaQ5H5efW+4cWr8k534nXYYmUSXPdzgjeDsEWW/ir0Y8TR2sgM39LRajkMbyezKfg1p7DYrm6hkXA6ge6iNdMbyoCTY3zBBfo/SBMRsUsknuomB5DUvCG9wMoxe1NeBFfsbqDk9w2PUuPd6+X4kkym6rQ5pm/KxzfWoNm/WFqcVSon2mus0CEhitTd9JIfAwBDikWsnODBazEMb63AYF5C0XC/Qk49O8wRV+DQZzoQu/yF0zAXRjitFerjpujfzhtrn8atp4UYU6sUuO4xvat59JV2wywhotAtiWrN051BH4gcuyjdP2MkxbF3G7XyWoAFoOR942naYdAUm4R8ZO8K94r+S0lg+x41JbhC27/3HgH37CU0ft3Ia7WaNAQ5jnIHuql8NLQXjG1hx3/ZQORTqjiwImLvH4bse0cj2aU6FN3ktobYeOog== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Use the INVLPGB_FINAL_ONLY flag when invalidating mappings with INVPLGB. This way only leaf mappings get removed from the TLB, leaving intermediate translations cached. On the (rare) occasions where we free page tables we do a full flush, ensuring intermediate translations get flushed from the TLB. Signed-off-by: Rik van Riel Tested-by: Manali Shukla --- arch/x86/include/asm/invlpgb.h | 10 ++++++++-- arch/x86/mm/tlb.c | 13 +++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h index 357e3cc417e4..9df559974f78 100644 --- a/arch/x86/include/asm/invlpgb.h +++ b/arch/x86/include/asm/invlpgb.h @@ -66,9 +66,15 @@ static inline void invlpgb_flush_user(unsigned long pcid, static inline void __invlpgb_flush_user_nr_nosync(unsigned long pcid, unsigned long addr, u16 nr, - bool pmd_stride) + bool pmd_stride, + bool freed_tables) { - __invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA); + u8 flags = INVLPGB_PCID | INVLPGB_VA; + + if (!freed_tables) + flags |= INVLPGB_FINAL_ONLY; + + __invlpgb(0, pcid, addr, nr - 1, pmd_stride, flags); } /* Flush all mappings for a given PCID, not including globals. */ diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 4253c3efd7e4..b9aa5ab1b1af 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -498,9 +498,10 @@ static inline void tlbsync(void) static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, unsigned long addr, - u16 nr, bool pmd_stride) + u16 nr, bool pmd_stride, + bool freed_tables) { - __invlpgb_flush_user_nr_nosync(pcid, addr, nr, pmd_stride); + __invlpgb_flush_user_nr_nosync(pcid, addr, nr, pmd_stride, freed_tables); if (!this_cpu_read(cpu_tlbstate.need_tlbsync)) this_cpu_write(cpu_tlbstate.need_tlbsync, true); } @@ -549,10 +550,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); @@ -1715,10 +1716,10 @@ void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, u16 asid = mm_global_asid(mm); if (asid) { - invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false); + invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false, false); /* Do any CPUs supporting INVLPGB need PTI? */ if (static_cpu_has(X86_FEATURE_PTI)) - invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false); + invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false, false); /* * Some CPUs might still be using a local ASID for this