From patchwork Wed Feb 5 01:39:50 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: 13960409 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 38B5BC02193 for ; Wed, 5 Feb 2025 01:42:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A94628002C; Tue, 4 Feb 2025 20:42:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 959F3280029; Tue, 4 Feb 2025 20:42:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7D1ED28002C; Tue, 4 Feb 2025 20:42:00 -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 5A9E4280029 for ; Tue, 4 Feb 2025 20:42:00 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 017AA47CD6 for ; Wed, 5 Feb 2025 01:42:00 +0000 (UTC) X-FDA: 83084190000.24.D2BF021 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf07.hostedemail.com (Postfix) with ESMTP id 7A3C740007 for ; Wed, 5 Feb 2025 01:41:58 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; spf=pass (imf07.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=1738719718; 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=ip6IZe3oo5uqxg8ZjhyfYmkwoxfDeIFpv/FoRZTLf+sjr2xD9Ir/ou1aJVc6UtFLZQWHX5 iH5rbGKghBoAbJZyflorOT5iTtj+varYMsohmUO0BaT75ggnjdrjBwvHV4a1Q8xZKLxVuj Z6oR9mniygvEYrahgzOqXo1ldBWnTJE= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; spf=pass (imf07.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=1738719718; a=rsa-sha256; cv=none; b=x8unks3ILedGpvXWO2hkgTzRP4Nww0obUi/2cx3JwR3r5D/ClYM4IVmbkfxa7e+0W0IOTU nqEOk4KooKtw5pzgFvLAVjlH8V84y1ETyBiQkV0JyocjAYHXnmcfcSgVLia0DUK+D45zzL cYQcmtqqrQ1U6gvLfTWZG+2/v8Avit4= 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 1tfUP4-000000004Cs-2T4Y; Tue, 04 Feb 2025 20:40:34 -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 v8 01/12] x86/mm: make MMU_GATHER_RCU_TABLE_FREE unconditional Date: Tue, 4 Feb 2025 20:39:50 -0500 Message-ID: <20250205014033.3626204-2-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7A3C740007 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 5drrzqgg9yp9xtti5mq6f19c14x5uo4h X-HE-Tag: 1738719718-45288 X-HE-Meta: U2FsdGVkX1+7u6SWX/B0uTqy2FZs0gQ+Ioc5B7giO4v4nsQyhCn8tSzGg5vdISfJhpBLLWJ9J7X2pPcIXJrkrjNiDl+Rbto3fD8Z1cmrPEI25YNSTZdalCr15gEJPW9D6c3k5qhj6/jfy7PQbKN0+N+TtwlsfVriy5ci9nlfHfnZITQDUixvba/47qRxztTV48quLgbHd/PFEe8Ouu/+1tCi8NVXwYCv8wRIlB0L/ZjQIg1KItdY1z7if6K/c4C0USNau8cYv3QKVXAQD0Wvb3gPzFtS3La3S/RxFjqN6qxZAXxHg13PSVJeVcth3mMzSqRMAdhefLek2CxMoPL8NHXSygeYE9YRECck9Ljr8s+0cIcvGoJ0JEkJMMvP8T9O4AKboYmTTIZlrfYS2LxuCfacChGT3XUTD2hJYAMqFWQqBAGBeQh1I6qY4cfX88L8G9aq6PktOy+qXNQ1d1FyHLgVH8kNZy9aDsss9/nON2vjcAiBp2+HN+qvv+2kY0aqBj0cXcTe5e0FnCXluGJU+9x5ctu2HMXBXmERh98BDLX0eKSAXVwIf/JY9X4jxu+VAmz3rNLbjwDby5XJES/mZP6FDDC2zBgbDCpwSZ//x6sNvijXMTCSot846QJu2KgV3H7Vfu+LaTJkrGN8cScBJNIv2J6r5B2vJ9S3nMAfaMD6tOVZ1nlkropCUex3uQIxhXI3tXKfsEqxLwLBD38KRFvIStXvH37zv1r9V02sdT1xufTWfT0f3sPAQlWTCYe7fhRA1Z9vUpOovBaX7zJpUBCx0FujyhJquHjOaIyiSelmIGas7T0HKxlixWbgns4UfjZYCDFA8gQj8Quh8H8gsDilqQV07nKS+K6ZpLorDW9cPU6bDY8k/+r01U5JN7vUfvZ2DLNy7oCvLQjZiqoT6XWHWw2WRJU/hyckU0WawvyRn8ub9ytAuJKwI0l3wKKaERKJUuBNK2FTIK4aBJq v6/Uf0S+ 1hXSe/32rwBOUiqIBopXvoYmcnGOaNUs9T1yp55NLDQXEI6C1K/bCU67TTq1XlcTF1TO33I8fN0CbG35ii1h5CVZlWdD7dP9x17Qu4H6+jOudBKjb62ilUVGkdsVxLwEWuCuq3v4mHIhSKiweoKRXP/Z5vKETf6Mu1nMPNwhli6oT5Fe+uzwb5lawHqdYMJYOYInaS/OLyy1fAd2K61zBUnHLkWgimyK+yDfvrclUTrBBU1Kf0bgLjLav/FXVkdaZZhpf79We2ufgMIcU/JIUX1LdwDlssC0lW53ISWPYjZ/rMs/KhflqdI6wQD37zBpXNqNG5QI1yGN3v+xF//YM7ETnb52d/AWTCIJ6pb44eI4TZTjVFugYXqy0QHDpV4TN4eGQmfg/NMUu3hleiBvt+io4/Cycz1zNoUghG5WpU7BySMRe9wlMorMfPJpuNcb+FVgWkdu5ktROMuiST1gNEZrhjDeIllPmeBeVh/FrAB/S3o+3IKeFofx6oKzlHOB1FSGBiu0giB5wOV9lM/7RtcVCUHRIjEnDMMBEn4jgTnbAfCXKfbOQ6hWFIvlN3XxY6sBwF9s2gxGG6k9K2qu/y2fVhU1lWX4eENeZUnCQ5D+oweI9xZBKeV6/pEFRJ0GhXOtw 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 Wed Feb 5 01:39:51 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: 13960412 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 61600C02196 for ; Wed, 5 Feb 2025 01:42:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 209DB280030; Tue, 4 Feb 2025 20:42:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B9B4280029; Tue, 4 Feb 2025 20:42:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 01029280030; Tue, 4 Feb 2025 20:42:01 -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 D27B0280029 for ; Tue, 4 Feb 2025 20:42:01 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 891B012045F for ; Wed, 5 Feb 2025 01:42:01 +0000 (UTC) X-FDA: 83084190042.09.9D38859 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf22.hostedemail.com (Postfix) with ESMTP id 1437DC000E for ; Wed, 5 Feb 2025 01:41:59 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738719720; 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=DqsS1OfyO9fnKom+RFOnfy44TgnZK3vtZShDJfAlyKNFv7KAv+NoBJt06Mze9APinr6UTk VN+Guv5iqV4GlTCXhz4ueVRV1i/ogSPw7R1u2Uw8Kk3kSa9x/H6yo1phnNsTKgweqAULbS 6WiI2sM9w6egk6z0OpkSZYkfZ9xLcHA= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=none; spf=pass (imf22.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738719720; a=rsa-sha256; cv=none; b=J3sx5Jcfr/kWDYQcrptQW1oRrn5zvRE+vH3isx0Z2a2euka/H8b8iYWWR3WsyuNpuq2iaB jW65PcOlrI0rgmxRzcnJaz9D8LK/LGokq8axm5cKTMIEuxQIsE8jOPCIm6U75mv0Lfakfl HQ/zncvBTxZzVhbRk/1u3oxZL1CrdsE= 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 1tfUP4-000000004Cs-2ZIj; Tue, 04 Feb 2025 20:40:34 -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 v8 02/12] x86/mm: remove pv_ops.mmu.tlb_remove_table call Date: Tue, 4 Feb 2025 20:39:51 -0500 Message-ID: <20250205014033.3626204-3-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 1437DC000E X-Stat-Signature: pkf5krgkfcs1bxuuj3pejnntn89r7j4k X-Rspam-User: X-HE-Tag: 1738719719-168204 X-HE-Meta: U2FsdGVkX19E/JfijbtQWludGHoZ3aB7BrfD54Of40uNQJB5by8TG91rQlkg9NDzSSBekxnS6M1r3rr2V08qwwLX00YNhqTh9K3bmwZ8/S6YWJg7xoUxuBSeY+wzYIp/ljTi5d7F2tn26RvemorCWpmkKlxry+HZGv8RL37ZMgyb+VFN/K6vHM2Q+fydjf6IH1b9OvY9YRqTWMUTSVDEQDsVdv/2Xvj0+tf8bNqstOLzgHtJ0X1Bv7O5Ha+8GZS3Y0aQUIrqfvLtJEhlRu1p/rS09a2g+S83lLX3yanIaThiwH+xmLWIrdlnamONGeZe4MXlES+NvpoEVf2Yq7WCWM9U3QpQ9bdFKzo4IhsY5eBIzacJw3zoN33Eecj2fOowI9CrVGXjIG5NsuqfwaP4xPHq2DDaT3u4K7l4Gs/5NvHoGxrjMw0g4fr+0rXffnIgSfiY+v3otWvB5m7M/mwAquWL6fGMZIDltwUzrVyOXZ4POaWktPAR5ZjDTuVYxHcDf5GwvA+Ioj0Txevo9wT2CY9mRcJpG8Vb1IyNQ4Yto9yI3XneLrMboc30tJxY299L7//ZSLK+gv3m8Rp16hAosZb0DWUf0BI5mUSdzc6uQvym5sQsoAGmfPre/YHTs8sdTZAeDEnQvORsBbGYcu2ovmRsDYBBP9L4F5zseTQbXU5ZnPqUUo/iT9CE398DO2t7cBxffgvLsNf3R+M1qYnF3dXefr1T0k24UT/fzwZW7N3RH1qUA0NLBzfppEP33tt3rw+8SLFFBvodt/SIqI965DGvcpg9f70S8azTuOhDhPhgxyMVRzt60PUP7u7ijfvhQ3uUJtF+puh1jFTV0c1ZxTBjcctmtMWCdWdCyXkOCrUto7bTHuM3hDSlfRKXRVGGm3wkHN1CAH4qOLJx6cph+UdqDzqcQ8gCHA5JU83CIHEWoozaOg7m/hNG/zURNlDciT3uFzvhDvFDo9Ati4l 0/9Gw2vA VbJAEiPIWORgQkAuDKXQxVylyeApWT+3eSJF6EAFkhHM4YspxIJ+taxwsPhecmXNambUqczsw1C7CTHSvVvI+vqLSZzc1RizLeMd++OsRL6aMUjrPQZl71ednaxJ+yqt11R/oxGUh1eQzgnn+G9iVyUtd2OZWxeUrpvm4pCM64mFfbz0fV7Bh6NkcyFss0d2TbrdhQ4UI9Lx3uifCsmTDKZEWp1M27msfMUTacrpktoGCw8uBwRJOgpKwVbGTdEHLNdVCR89Bo8VWiqkZIKRjnnVc9pDXWftCri5VXo22WMj+LdvVBfJGvCDotsjqU+aUrwdxo2YmdzNDOiDOpgl/pS83OkHCciM2PR56KSApAakq0gQVHa/r31vUUJq8PzUP9jV+1bOWP3MvzXl/AQkhgHm9WMQZSu92ll1QHuhFbEUP5UT4AEEm56McbtdcOptiGE8i/tRDgdy6nj5xjIB3SCg8cwrNjmj5K/1p7pGmtZUluBIjSoOZBvRREH2OJO7dl357Woa4ZJQ8XP6+9So7bZ9M+06OityyETgLZlxTtJ+D5OxL9woNqTRE8zsSxUGoa7qUkcnmMjZxW+hb1ULeFKX53A== 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 Wed Feb 5 01:39:52 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: 13960415 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 073C7C02196 for ; Wed, 5 Feb 2025 01:42:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 31C3A280033; Tue, 4 Feb 2025 20:42:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2CCBA280029; Tue, 4 Feb 2025 20:42:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 16C6E280033; Tue, 4 Feb 2025 20:42:05 -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 EB8C7280029 for ; Tue, 4 Feb 2025 20:42:04 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id AB3E9140431 for ; Wed, 5 Feb 2025 01:42:04 +0000 (UTC) X-FDA: 83084190168.05.511F46E Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf20.hostedemail.com (Postfix) with ESMTP id 260D51C000A for ; Wed, 5 Feb 2025 01:42:02 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738719723; 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=iAmQmZCZMVLJeE+FEOFdmABFJ0iOT1gtVlpDsFG+APg=; b=1ki6TA5LBir0FHSPtxXQF/vs6GDG4I7OzZqGJRx2tL1of6lyR8khwZdKCRY0vGbrf/ZFhq ZX+RqoR2Ka++hG3FUv0k/77+c8mW+IRKMwhB3Y5NGTLH5RaUVtC6w8/1IFrFffZRy868As NDKeuNdEOdjbISl2ogwgEem+8P5vj+U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738719723; a=rsa-sha256; cv=none; b=FcNJSDxQyvP4xxumX0XUxt1gEoaKLYX6+0P6H/5J0UujfzZK5lbvDmYLCe74NorH7cT4gh /USyvUGwXkDZ5qBXTfTHa9P99emq0bciXn6tV3Mcr0HDgiX1IuX+d5wHADR0y/ENY34+1W j/vIvH39kLMq94hQ6u+SuoBuwdnMUE0= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; spf=pass (imf20.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none 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 1tfUP4-000000004Cs-2eJa; Tue, 04 Feb 2025 20:40:34 -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 v8 03/12] x86/mm: consolidate full flush threshold decision Date: Tue, 4 Feb 2025 20:39:52 -0500 Message-ID: <20250205014033.3626204-4-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Stat-Signature: rwmabkwsghkbetkfeqit3qp3jadkqrzc X-Rspamd-Queue-Id: 260D51C000A X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1738719722-735571 X-HE-Meta: U2FsdGVkX18DrqDoZmShxNfypi88P2W2rKXF3AgAMP6BCjGQK3NQs79xAVrbXBgjK5gUczqk6TT51WIeXiynsmwIrm/SebtTdEtavbH63cqmRiOUwp7blOnO5AKjKGZpK+cHI1RECwEa3BmmiMSEKmlYIiS5gszVWp6nns9ZPDudMB9SWL6OZpuYIfQUuH54wo6jmkBb4xNKwsjTi9vdoP5/AByPp6kcrN27Cd/QrnciwhJghT3R6DjkiUxIVE/9ZuPny9+4XEfdLTXezZhvz+pG43NhtaRTevOUOmaPIpY5E/cYmCppr0SwYKQzzlis3mWT+7RsWdovBba40Vz8ottish0Z/WdkRPnIIWnj0a8h8clyxFDLx+ZUsWt9GoBY72vK5PdOvLtmiyVwGCg6L2zHiQC6TU9cV+kVbKMH0N5PZm38C6+tPd4O+F8g4aagJxnmAlVuxz7jVhXGIYWF7WF2lBUNLF1XK4JpAfIPROrn//VbacDg8PPbCayhejS1EvbW/OYDsCTZTNXlEbnbL2jtX0eOG5/5RoYRi3Or9VH967kWOKcRYYE/cWuytw+w3IEvVyH/81gr5CsYdm2D2znlrPq3K6xisj92iafGgn4rJE2Gg1viGbE+TwljbULH4LUgCbztOwCw7XknU66TStVa9lMjp18Q5Yts0Uv5VdNAUGlUJNXu8vN72+8mKFTwtxD85fV4y2wWrdiuGwLCpgP2B3vsqZc8JAQHDzOgZDcMUu39JQe1BnVfzECeSX33QtRENM9KcQw2+TEmBOxOiHiH+NnnQ2aULav7TAIsmK9WpvYrgxr1jlyfmDEmtY8s3KnP6P5w/DUsfsBHG273zKtbNmcNFFU5sGM82AfS5tcJsIlrJpHqxUIVrlhYElScLzOtBpFWtk4D6L9iL54osK/MwMjrULx/NXNR+ltwkQxGYw1gw659Bwk3GJZ1IRg/GhA6Vk2FZZ1sVtfJtyu mOC0N6i5 K3RaK+uu79SCdFBlilEcjBsJzUNB1I5GBBTp13KCaFOuvj6h0w1HA61cmReKdKyHPymXyRvD/7Z9i4Z2LBs+v4BS+Z/WLebULj3kJ2Rldn1YrGUmkQUGsVsY0NfJtzFRvvyuiZ5HVT1plPQIfeFyE773+klnXPufrWtWCrb7DvqgPLf0WmRnuoVySMi107aBXRb5vCXmTTY5i+3XeLCzFslVSiGZ1Gqz2ZEkRGbP/3YBgmRjL+Ja8P1FFr+zwptEthwV7ABb54WK5xv89tBbd3qr1CepSY4u06eok7Zily7WfsC3sOCGi095iThcU/frBqo3es4G3nsPfwBQIWhKVBV0iyiyMwqcF/nD3wanCvbwBJ8E7RJmjzJ2r7z/LXpnupX9ke/GLY0ugBqEH/zhSA4x/1Qth6I8wZQ/9cS97z+H2dYQdoJzyqinQEHoWyuBqzZemZ6Ze5bhOt8TyOsdzZolKlpa5kINutAeXugHaMm9bKyPAHelIm2YNlA== 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 | 52 ++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 6cf881a942bb..02e1f5c5bca3 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,15 @@ static struct flush_tlb_info *get_flush_tlb_info(struct mm_struct *mm, info->initiating_cpu = smp_processor_id(); info->trim_cpumask = 0; + /* + * If the number of flushes is so large that a full flush + * would be faster, do a full flush. + */ + if ((end - start) >> stride_shift > tlb_single_page_flush_ceiling) { + info->start = 0; + info->end = TLB_FLUSH_ALL; + } + return info; } @@ -1026,17 +1040,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 +1094,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 +1278,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 Wed Feb 5 01:39:53 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: 13960416 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 7114BC02197 for ; Wed, 5 Feb 2025 01:42:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9FA84280034; Tue, 4 Feb 2025 20:42:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 95E4C280029; Tue, 4 Feb 2025 20:42:05 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 82240280034; Tue, 4 Feb 2025 20:42:05 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 5B915280029 for ; Tue, 4 Feb 2025 20:42:05 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1A156B0871 for ; Wed, 5 Feb 2025 01:42:05 +0000 (UTC) X-FDA: 83084190210.28.9852456 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf29.hostedemail.com (Postfix) with ESMTP id 8EEB812000D for ; Wed, 5 Feb 2025 01:42:03 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf29.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738719723; a=rsa-sha256; cv=none; b=43ljzocPSrMDomr7js/4zL7tMP5MYDa/CU2XpleZgx/M3fWRPbaOZ8qBEhwB5wSAgxQN4a FmnJ7Z1Bv+9U38M0sclkLB7DusiyKz0QpxBl/Vjm2QgAkwmOFDJ0wYT8C51nyjok6OkxFg E7ZRt3PlwWyWvS0hwdZ9zQR2ft2TiVs= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf29.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738719723; 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=HiV8dGAJdlvQ4MWzeEl8GDv5AH4aiqkl40IuX2/ZLHOS4ezF5GGmy0rFPZSRsJbbmmA4VG /oxIcx7hiGCMruauUQoJR7+LOQLEwMdnOiAYyRJaA3+Qk0SuotBzd37Uyyqryuz642w6ye yKsLtaBfbvpo0mMy2kQ1ZyiaYpOQC/Q= 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 1tfUP4-000000004Cs-2jje; Tue, 04 Feb 2025 20:40:34 -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 v8 04/12] x86/mm: get INVLPGB count max from CPUID Date: Tue, 4 Feb 2025 20:39:53 -0500 Message-ID: <20250205014033.3626204-5-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 8EEB812000D X-Stat-Signature: s5m17umhngmgkb1x4r814kyg4a31r816 X-HE-Tag: 1738719723-463647 X-HE-Meta: U2FsdGVkX1/3gEeAciviiUHf2bh1yLWSB9iikFCE8l8cN2lPW2jkYNtYxEhJG6G14iGIo4FwrIbk9Do+BeQRhfLvMm2Jkk7psMWmeounJtSbETLPxQ/yVTOtBqloGsYhYpd/wgZfWlvrk0/+ZH4EKEX+rcfl6t+5wTV8Sx+AgEsu2xt2Rf3mBT0FgGm3VUtqjOPadpDNHBU+YgEgRfO7ofeoTYc0AZIu5dt6QPpeFMOx3GNix4a3W20XJ/sLeSRzih57ktqm6D+ygVYOyjgbdoQqwQeHAmwfvCQF7T1uuPu0YITmdtpPmOYRZQAZdXz+M+N48WItDYjI6Y9c5ra5hNHYMzrV+Qrsa43StfCTha9sRSHLuDli4G4Gsnl27zlRBwrz1xAJBhaMA/0VrV0iWBpIKZ6pz5AhZ7YoTt8p1kSiLJLgM5aHuWabV2rbvp+QVgR2LvXOz35tfLlww7+NR0gl3sQrZXqUGyq/XKRKcbaz7BupcF0bnGk3TPKdDf3kkS8yH0K4MHln6mTCpw6J/XGcFMNS4+bwovRf5S+iGMvTHrhgWrwq4skf8oXxmZzyIhuJrP/zCdMm2Mz3ZiKplcigIayDcoHghDCPO2OoqhAHCgw/j14PMYhbOzPkfuhY9x+I54tSxfD5codgC2LslyBTGiVh+soEk+WrO3O80g2odjXXnbUnsI5Y6IE2vqH9FsMxzVRvXAWQ99HYH6rzCTCe71YxW2bldN+BoFTjwegkke1Rn1Ndy9dFyC/JDICccmLTpxfnGoYZet99OPojp7Wyp7le5AQW9apvP+BolaUHCQ+RBvDMHdh5oytjkVuEiv7PmHU9KgDuaHO+wnEquWz6oOLedg0df8tsuhSRIv0GUA9HMS/rGToQ25p/nSG37dJodyD4dbWFGURVFeqdTQmCXTHReT+BWoaLbPZkjtYanzym/Cy4h6KYtllHY6e5UiRwEovpVnvrCDkQSlO 2J+wi+2U uwQbFgKADJGzABPCacpZX3LsUtdXiIL7kFL8Wi3al5PmAqSavofrWN0H4RUxeqA6+D6Gt6VbA8eedcxdqK2uHY/ygTTNybPl1LejeFWp2whDHVdAtnXMusVpgs8PB00OyrB21CzLsM12mgs/Mb/VCvLtzG6VyiqjHSsoYDgMownypoQeJdzRbFvko30GV3mKrl+TQL7Yz81526GdVBo1151BViZFDy8KlrQg9Fpc0m78p5D0O4katnojnm4gJHgXiWbxjiHRi35TbGlE0VZU8vMYb4GsiWrBUykVQOhfmm7k2lRsfGVK6UA8l8jbHp71Mcj6NOgywjt6dpt1btp9B4WT2cvlDy6W5Go4gfcmvDV9c96sBMNxItMYdKvzfhmBpnu02O3Q2DVqURYgIBp1nICl6ayzjFBNZCVwXZB7hq08W36Fdqf64vszfHxjOyTPsAvfW8LHfRkuNTKqMdSsY3t1CYInbpg8iiEB1xyPxo3NEq2yBt+teWBpRHw== 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 Wed Feb 5 01:39:54 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: 13960414 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 8A4C8C02193 for ; Wed, 5 Feb 2025 01:42:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AEF9280032; Tue, 4 Feb 2025 20:42:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 75EF5280029; Tue, 4 Feb 2025 20:42:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B08E280032; Tue, 4 Feb 2025 20:42:03 -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 33DC0280029 for ; Tue, 4 Feb 2025 20:42:03 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EAF8E1C783F for ; Wed, 5 Feb 2025 01:42:02 +0000 (UTC) X-FDA: 83084190084.13.A826BF5 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf18.hostedemail.com (Postfix) with ESMTP id 65B721C0005 for ; Wed, 5 Feb 2025 01:42:01 +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=1738719721; 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=gGkuDiBDS4ihmNFZkFNOYw2RY+F3FANpHOmoSeyaCYLK5yMJs9XZXTycyDc5aMOfSNkSWC KTrb01dd1VChOe52TX4uwCY59TyU6jeKJH+1uhq8le5bITNmMP/CBVSalv0L8/IRR4u862 eSjjFTfFLLKKC3F2hudw5T5RTpc25JQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738719721; a=rsa-sha256; cv=none; b=xwOfIt/6+ADPy+x8f1GUNB/IhmjZiA8yblztXTOG7Z1UdWYliMzIdD8QZEPRVQK33LoURk wo4eER7lhl6pqtJSb3WisBhw9jICOVrDdZg8mVvR4xlu3qH7iVvysCZoNdPw5hzzTD7MvD JLRhAI+y9cBa1L1E+YX36KsqnD4m4tk= 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 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 1tfUP4-000000004Cs-2p3a; Tue, 04 Feb 2025 20:40:34 -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 v8 05/12] x86/mm: add INVLPGB support code Date: Tue, 4 Feb 2025 20:39:54 -0500 Message-ID: <20250205014033.3626204-6-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Stat-Signature: ef4n9awdgx6i6yaaw84nwnw3zfijy8yk X-Rspamd-Queue-Id: 65B721C0005 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1738719721-895408 X-HE-Meta: U2FsdGVkX181dRusCatOb+y5+9SBBz07poA6RPkgGQDY6Roh97638/rZB/Q7vMhv9MQcuhsF3GenU014Wsbb2viTBxvweYF0tfEpVVcUREQrqmx9TAaoyMh+dsEraii0zKEgCq6qxqq/h/zWttXAbfpzVFOhq8Khso0JxPfzl1SHHMwwlGWYujaOPzepG2yEFUPYL2KUZw58s3d74RgQb5YY6xOlsNc6kQfhn6/bUS2LXOjQ2l7ZRKTeSbCmUI2LorL3akFuHrBMG75sTldy01CwZOs3NfejypIihnVEu024Xxyj30wRHj2lyx7Z/fFlZoTPXGTNSMoUygeAgZJQThM68tQ6QLIhu/KfBJn1TzIay/L8FckuV+XYiJVH4LnM4HTP51PPLZpvNtkkYZ8O8ENcYo+XtfvnxcdSwVzCwdyINN1rao2CQM7491rR8uOUSNuoA/nY3mOMK8XKI641j61rY4m4w/u4NNZ5pRGbO2NjN5QMRyo28JucwJQGKrGIg0rLpn1wnM9h8tM8OktXFydFA5OwmF9ySZqoR9su3V1paaTRCqyGdmJ2WczZfLSFFltHYtr2v5ptRcyQ2H8z8SPjpRrCVd+Q6ZzVM/m+D/omhl9fP5NnqI3p+cEOJ9DT38dcdPkNXFJ9UetymEpBI4IeFZTnysnD8Ap777De2KIHmL7YCHSvKb44chiTkn8Kied/LITU5Pd2e02FAH0GBVG5mlXTuV/5zaPQK5gIJuumCQ5r5Iwa/g2pUnj7BSSOPQFAxdCs5zkSPUAbSVmm+w8xS1ZnbY1McKNavF14S7m3mpb3dHA1wp7dfWKPtZax+5xrqBLCjNIpK/0vJemPWK59mZ+ss7IIjlr57fpZUok5LxYo1jjq/XADQSJ+YakxqJKyEaSB2Gc9qR/ey1rKyaFrB/OsyOsi0YiWQUkYtASk1FoTjzXwzvBtwfFLySRIEKKnZMR5Q6FLcWHPajV yK373Z6e gUplkAULtyLVkYhLvBuOfsOGSgJWim+I+/V5Ewg9mktzyzvt9aqZwdgdAN0VHDWcHl4mnwvJPTIWwJkKZY9aaFTzyMbkXI21ddOE9vQkplsG2yLZFr8miKWkTCisaitEpIYHaA1ECpWvfoGadgm6eCqyMGmH6IRGwvjvrJMnme5FMoZGkjqIpbmw7s2lFbI+zguZx2/xRVwl2+z0ynj2/vv0TTo8I9dYb1MolrcmUkYNHFz14XW0QVIYpgLxcD2CtIShka7GsnVPbY+m9HdrDuOnOTdvced3Nb3BjJouIQSSyQPaxvRMu+ORi5HmX6q1MweIqSXiRAPIMFQWS+l3ZGjnsaTUr2hqu6m5GSa+icw4N2wHaIr1WE3i0P5Sc3ip7M35D1Jc0SPdUswt34jipEd9SVLx6vCXgM8QJVHKKI0gB3nxPxNwTll3eaBuQjsfDkKR+6ojlveI0TACNq4LZvWI6nDGYspC2cHNAjC+5O22RySZMkxDo0qT3Yg== 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 Wed Feb 5 01:39:55 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: 13960406 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 9EE76C02197 for ; Wed, 5 Feb 2025 01:42:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DC9B5280009; Tue, 4 Feb 2025 20:41:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D797E280029; Tue, 4 Feb 2025 20:41:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5518280009; Tue, 4 Feb 2025 20:41:58 -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 8F477280029 for ; Tue, 4 Feb 2025 20:41:58 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 4549647CD9 for ; Wed, 5 Feb 2025 01:41:58 +0000 (UTC) X-FDA: 83084189916.06.E14BC88 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf02.hostedemail.com (Postfix) with ESMTP id A67C08000C for ; Wed, 5 Feb 2025 01:41:56 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; spf=pass (imf02.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=1738719716; 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=HRG2Bbm1sWj6J9i73OhV5GaNFdEzDwFt59ntbbNT8+4=; b=A0JAYaqeBtWW7izZOGCy7VyoEmmgXIoFQwb+6FZQ5s1AlbI3H6qnsYxuOtS2SL1IVFCfav /iTm5o6nJgPVGfMusd3poanQFZN1pTjiIEQau/NQ6NTWE23vzn0Fr4QJm+rOOJBtcQxjeK g8Oy+46DsBWo+WGs4D8/F8hsKV8dNsk= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=pass (imf02.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=1738719716; a=rsa-sha256; cv=none; b=3mPEY6+DCTKQ8RsBImpqevbN+ncY6GJeYb475FTs1aXxFmYoXTjRj9AXe5w8fNTwnn/0J3 ssfz31mUWwMvSbDcgLwCV7m2nuVB8rsj/AUGX4qlESIC5l0LssDAN6Vgzasm60iDVbptXK 5XilptHLOCoARD7AHF+O41Ti4xrS2CQ= 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 1tfUP4-000000004Cs-2ubK; Tue, 04 Feb 2025 20:40:34 -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 v8 06/12] x86/mm: use INVLPGB for kernel TLB flushes Date: Tue, 4 Feb 2025 20:39:55 -0500 Message-ID: <20250205014033.3626204-7-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: A67C08000C X-Stat-Signature: dpw9jz3pz6dcojie5ogpz4tit93byncz X-HE-Tag: 1738719716-354838 X-HE-Meta: U2FsdGVkX1+65bYy7VqxSroT1xgK6g0gq0qjMkjMFSC/Q2x9gUCle3iLM5AEscUjnOYonyXbXo3ZpPzWu68kA2ghpCF5fhEtVqMWsTzsazBrto16uU2mR3JyZRfY4jgSG4UV+zaBd4rmZ1gKMvHVKjzURowoUWGG6c/nKrLfhh0++u1Zw2vakqVwZOD+8hWBgVHpwyGpibo7qSe1AzQyongEFM7+dIgYM6AjcQWz801s9X4ZVnCzZxfgk9IpEF7L2bpM3M4isQolU9GcegsIH7s+cwOVHZEb17piHY0HRwK0nL6g7MY+n7zU2mz44HCAD1pLA+YLnUJ1JH/W0xR1WALQxlVQD8ZsNCsBFbSGG28TxdEn2MXihQk1xKEVwUQTryHK7AYnJsoEYawWMKV25uyIH+uMstInlFR1hftcmNATnJ2lrcT+dlzoDqCKzrlcuONwol8zM7nEqkT0/fJ3JAXds6Zg2XhPOCqEnf973zsocMfhbZT+EvjCWIZGhnsfuMaE/c/WE/ugaTQwL9D3DaSAsptYoJlDqsAiLCpdO9pw8n2FL8SLI/AK+jAVw6bBIF5VLNqe+lo5Z0wFGHNXmhMjgFdcZ62NcjeuNBMwZJSLFYQ0rcJ/sw3eqljJUKfFl2izlbugvE8d3QnEOBSYvJXB27ktzzd08Pv/3Z4bAujrsFfSaRgpAtgLqfjIzQBFsbNP5GYyO6U6Eueqc+VG0ZifXFg6al2lo1pWAi6RDeSaTALGKfIEoEfqGsVMYHozScofJmdc7LwiFw0axm8dbx6UCX/ycfruh5RHC1dw+KZey7lLjCqBKATWUkrSSw/ucRTR7P7ylRYBtFyShRE5niG0jUaHIa/XjFXalY0QqL73AMfGy+e/K1RaUhJSAepND9nBEqMv8WMGOFlMSxNZqGNN2v6I+TY8b9OWSQw4n/s/OlrkR8VzhCC7v5d6HRlXqeQ68q/C/JdZRRU4Id7 xQ9syHvY WcYElPUlAdVb/QoMnBMxel9KHWpyBxoVFXhYvJQ0vSIBWNa3vFAie+tJsm8ygQgsB5bfqhno2N1XJKSFLLB4NN+28guXVMbrod/wy2r7tTIXf5JEL4l/SDrfbm0yUuwB0zKRPgBwgtW3hhLlmXsxwje+MApu2MijX7ll5nR0W1pZJDelo/kwiIyUues6EQPV2qd5krpa67GUXu1n1m4vgmD1l5CkMKZJp61HNn5/2l8E5VJh4gWeRMBEWnqc3XTbx+jyB/KUHH8M/PFbB0I9P8/lmilIeIHAJLHwoQovIqubRjp/s8D+aAm+/wLvjpvvpurYJHxx9N7k4yIr+Y1LeTHjL7NJXp9+D6FUQlk9jnWPFG1wnzUkFB5RP4NmRGSVDr3lqrlICFRJNkhvrI6T3eCE8czea6dxsodpy6PGDq87efstWcV59KGYQ0vmyVHW8ZhPHk29kpZVSevvOaPrXR02QXogpQEipd2qNgrt04PsBT6BfUuCzAfZnJQ== 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 02e1f5c5bca3..7945624afc98 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1082,6 +1082,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; @@ -1101,7 +1125,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 Wed Feb 5 01:39:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13960408 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 73EF6C02193 for ; Wed, 5 Feb 2025 01:42:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B85A28002B; Tue, 4 Feb 2025 20:42:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 36581280029; Tue, 4 Feb 2025 20:42:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1757B28002C; Tue, 4 Feb 2025 20:42:00 -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 DF3FF280029 for ; Tue, 4 Feb 2025 20:41:59 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 8D717C028D for ; Wed, 5 Feb 2025 01:41:59 +0000 (UTC) X-FDA: 83084189958.24.B90EE33 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf25.hostedemail.com (Postfix) with ESMTP id 13231A000F for ; Wed, 5 Feb 2025 01:41:57 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738719718; 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=3hJjQhaxjZoUnrI/0h4ZXxYnfjAoAeVV3kyo/NKjz+8=; b=1pIzwpjpWvV9qEQeBCZUfP0i4Izelng3Uzv7zwjKNlBlvLUcghx+xbinjOjvVivPqmq0XR 6wznKZXNQyYraklF45gIwLiK2DtWUMghm8ZnIjpWoVsUrJlsiXvzv1jtf3zwMcgOQThA45 k/LPwwqgjW47+1QgSP3d5e4wAh/VCR0= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=none; spf=pass (imf25.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738719718; a=rsa-sha256; cv=none; b=RS6fc0URtbNgqdi30PWWkhJg7KGNPrgTQ7kDqNh2j26BGJUaO15ChIr65/D4zS0Cq/kd02 p6lHmz0/qCnlQ2w66+2Ing4kY+r35649RxDMVc8C+dRJ3bWLNjpEYbODYxEOdxTxNSZ/jl tS4sss2TfqkaPdAxp6lglmPLUvbxqHQ= 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 1tfUP4-000000004Cs-2zwB; Tue, 04 Feb 2025 20:40:34 -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 v8 07/12] x86/mm: use INVLPGB in flush_tlb_all Date: Tue, 4 Feb 2025 20:39:56 -0500 Message-ID: <20250205014033.3626204-8-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 13231A000F X-Stat-Signature: q3nun8rmynjandh5s76sfqe6e9jmn7iw X-HE-Tag: 1738719717-244411 X-HE-Meta: U2FsdGVkX1+JMDJ9h1UCImWhJKJ6tDbzLfqml3ecxKeUyG5lMNIClWj6xZW/fFGSxU+gkv3ZcmufdqqGq83WD/D4jZWL3E/gQoeFgQA7hE5J31nutu+3kTU7ofZsDm29ut55vxePrxpicZAhDUKPpQW7Tl9thOj8tryLBjb2X7koALX5NDmezKMaxFChbPl4p8CKZ8Aj5bGZvHGFjEftEQf96cZRK1pr0GLjY8vmx5V0hq71qpE3DkR3L89uXXPCd8Uj1OkI1aAYDr/EKeMjoWBaGGqcLXSdK3MDFlX0DJGOMbt0gjrrPv+9L3EkNtKbMn337DXUdKxBHMH9m/71dq7QIxMNJkgvq0Wz2FOsI2ikbV0XnjoBKGcjFICH0A3sPR4R0E/VUT+720gqsUcy043YHfMMbkDWZVgkwQDY3GJeSFiBSH6JbC9uBF2WwGgdySyMNH2CHql9SioYsxWJvBNZNVWVzQxFFpiQqQkytYumKGHA6jqFu85ilXLckzQ+GIMrwJNoSdEoKBMM6wL4V1QDhLvZTlAOLlp3HtmoXmGRVLIUtbP8g18jQjbHGgr1rfdRIWdWgNf9/W7YMLboK6gAdwlfzVy47vemcpM6rmzLYk4Wh2j0tr/rn3mu7nl8etrdGGbpxkpJoQE90p8Zz44Vexetc5sYxnxrXjGxEQbmHJFmS0IJOFx+8OchoBLy/85pXKD35l+ud38xega8iGwkrDGcEVXIAKZ3HusK4RY1o4ZqvmK5AWbFheOHjt3OqtmJ8s91PYSdlfbkBEztnVBo3HVVVtkZggHiOCBPUaRfxQLA7LtxCbRFOF8Fk59Ovyj6VEko/lA6VIGA1xB3kmFq1KxFHySeWsvwe0F5Z3eaMv1nv66WbZbXnkS7yd6ymz2QThfWd7PNOCLpJPFNkT+U6g73OJe07uWk69NEMF2XSjjtw+7WhZRFuWPqDGVIjpb9hy78aCPfPT5gfmx VHt+0lgP oQF3Dai5+S1GK5KJsn7J2vXfhD0TNoTSLq93YBtGPfCD3qN9ds/p41nW4SXv8ImT5zBll9mYa4kFuCT04SM9m1esClKjcfEodjy+AMZWh8hDFo8BbdEeSqS4Az+qPRD5qft86dU5AXRyLZoDFPwlX39g1ww/S8ZVWvbi5FLfnssSfOyLRowyygUXgfJ2boQlyKF4npKcMJmJZr6s+Q5cUotdq3qgJ4mlmMw5DEkHSzn8STzTznRG3lTdnyPS6impquE0wkPmbx5CwU0nLeZECPSGm0J7Vvu5zm/QyfkpdB6fjJDFaloaO3YCIL6nL+vOm0kjJRnct0o2M9FmcGeqSYeeSmOR54KBOYduFRf+LnKax3qqlanQDDF402WRgYzcKTg4FOoDUFgLy8wsihTsbpX21T1sIadH5n38WaCcTMFhAluTSn0AH1GC3gqc20kJqneJguyZi9q3mVyDZECImV2Um7L0x6of6B3O7bPHKdCNbSLNA9lImottDLgnBOpPvTPdx6Hl1pQvB6WFUdn651SFcJehVdkb/CO5nsV8Gc1c+m8hhDVNtD3BqeAdSyAfrC4jx 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 7945624afc98..43e1abd4bcb0 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1070,6 +1070,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); @@ -1078,6 +1091,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 Wed Feb 5 01:39:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13960404 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 5C642C02196 for ; Wed, 5 Feb 2025 01:41:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DEE8D280016; Tue, 4 Feb 2025 20:41:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DA106280009; Tue, 4 Feb 2025 20:41:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C673D280016; Tue, 4 Feb 2025 20:41:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id A5EF1280009 for ; Tue, 4 Feb 2025 20:41:57 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 529BDC05F0 for ; Wed, 5 Feb 2025 01:41:57 +0000 (UTC) X-FDA: 83084189874.08.88AEBE8 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf27.hostedemail.com (Postfix) with ESMTP id 963FE40003 for ; Wed, 5 Feb 2025 01:41:55 +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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738719715; a=rsa-sha256; cv=none; b=LLK31Mj5hxojVyo3J1xeGfHThLa66WkYCXve0qeo1EXNNXM1G8CBo7n7KU9L9hsW22OYII OI1iqYxfCEpZ8k//B9k1VOJ2IAUyYAGPnrU6Q3YvKLPMse/xI36P86kFPaaglllFQWdU6c EBpdHueRWJhTsp4qQvwKADdUvOOEg5M= 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738719715; 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=RJvH0dEjzsESgMcFRzd9556aYq/w5MJOy0P94uuYdRE=; b=4Lzm32WJ3Z6xuiJvZpPft0+wSIU692gx/qLZBsHcppp/4OmhDSHvfiZqQ4cSJ/dKjXizF7 BvQX7lMCKzsEZPdRvFuMk6v7E7kx5hSUCQ1gM9P5OdUC904qwNg8E5RNBIfyx0/BBl7YSP lM5z7bYlpTMIe1QEz3/CGIFuVeTfu+g= 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 1tfUP4-000000004Cs-35hO; Tue, 04 Feb 2025 20:40:34 -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 v8 08/12] x86/mm: use broadcast TLB flushing for page reclaim TLB flushing Date: Tue, 4 Feb 2025 20:39:57 -0500 Message-ID: <20250205014033.3626204-9-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 963FE40003 X-Stat-Signature: qjrsemwyxmtus8yyn3p9mkstmqxmpeos X-HE-Tag: 1738719715-206273 X-HE-Meta: U2FsdGVkX1/ogxMZ93titD7ECU5bgPW/4ntfKuAI0AIvKM6T5Q4+sG6WRVlcs5+QnJ+C+R1naFLx7aSJJnkOdDX28ANrHUQy1H0vhjTiUD8WgOfEmJxc0LlRuINc57c2fKQl0FKOBMHD7YvGKrmrR/aX3GVhqTWNCOD4WL0mexVeTZSw4rj59nsIBcw1wJdHCJbgcR+fII5qkvDMUiXL45dATwIw8buqTZwKGEIshZkDXPYUThM+q9XbAqqQnGvMElDlnfhooKvdCoWhn1R+kMjl8uLHtxLkeAFImlkanaajEQ3UFmzWdL5xEf219l94wV+EsVvyUsZUG0or75yCMmEosyvHvJHG1VjEp6Vs9nu4hUDgXn4tjjlwW6p2r1Poy7x+ZjN65+GQB3sCCmrTu39Q9pG0MDsmtCZoRx5MhW72zxfdEM95yJAkTUYf7Mdj3+c0etZ/izSe2dYsJKtbCPDLqOJQR5fV35MSEYvLQG20t58V7Rax2Yvi5J26PbDUAlN28BW3JSODtCgHR/NcfXbJVxiOH25AU3pbbMVo7nVSu7R15PjTNrVugcgcVT9vwLvvmZxqYgQ7LJ4UluilSWAD3k2Dx6a+dC8HNVZ0txHQleNe599Mx/W8RuWBnyS289MVLXLIURQvhhAHC56VeRpSswR0kgW93WdsCJRadol2BgoNklc4hxedSwR6pddFXVgYKqMZbAdd+1GanaeuDp8j/4HjWFICQjaU/OOqzmBkpuek2uXG9B3zZAG7v5gPNhBxjtHuBG2M20xt9dVtKKBY3qx2uvFz6XY/4leKFgPcfF7Wa4XaJ614uipsESkOVmpYdpkuS4ClCAJ0TGiar6l6CWEclVWypZjHruXGXxu+SZIvSXwJuCBmQqzmbDEdPHoxBHYckPPhMdSPL4lCbr3YSbtHy/h4apk1XNrfE0IbLhiZMU8TKtn5h5s0Y/Lkl8UelobHJrUEn6SpmFp da+uh7GE d6wiA9m1OwCMhC7kPuyC50W7/EAUZYZlE+RwUWzYsVGEqrLIiZPEl9KzB4cBY2QrcTceITytVnmDN+2sNXkyiPnoKTGRsMtOwY8Ptqg3eVyWQucGcGEJr54JW+mn0jXvhmWMxTS/zYLput0Ep4hrhxmSuomxbcy6vuwHSRjNxNvFyT77Pxa95buvQH4TOvwvQDtz4CvPkEjG9uPFq9MKX4jSsGV5onN7ZwjQNMpZ+uBH+QQhBUfUdLV8LKiDKwXsWeKybfFJmhBRREc1qGkqfop45dcWI6ygKcTPvrlq5jg/CiT2kUoi9Ct7WQH/iLem+EGGZuJoZeQiPERBKrEB/PViSu85Z6Tm0YNJT1qQJ8vQ3dVwg2hBaTsTtYA7VsHooEcici+vx7uiKjSQ1YzQlJKusORIZNQlo042H9Lzekeq9AD82y/SoiHp+OPYoBqNA6OsmYqzo++Xbkk1PPYR80yoU0pBAx8ciz0+Jinh+TwlZ3i5SYpASuBt1iw== 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 43e1abd4bcb0..25c13416a0e8 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1326,7 +1326,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 Wed Feb 5 01:39:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13960407 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 ED5E7C02199 for ; Wed, 5 Feb 2025 01:42:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E8DD728002A; Tue, 4 Feb 2025 20:41:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E3BE028002B; Tue, 4 Feb 2025 20:41:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF39528002A; Tue, 4 Feb 2025 20:41:59 -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 996DE280029 for ; Tue, 4 Feb 2025 20:41:59 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3442C140426 for ; Wed, 5 Feb 2025 01:41:59 +0000 (UTC) X-FDA: 83084189958.22.45C4EC0 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf26.hostedemail.com (Postfix) with ESMTP id 8983B140002 for ; Wed, 5 Feb 2025 01:41:57 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf26.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=1738719717; 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=BTB89YEY7WmLo7rYyf+OvQuwEfDHTecKqCiNDEW8ITI=; b=2I4v2pTTiNsBUAuFk5YbiuJr6A2V0gw0t4HImhXbObJ8Mj4VNVuKPuFH81LGrICkS2j5Mm OBC/7R5+DIQYzXUYxngziV9ntSLyllXMuJtcRhpyGVTnOq8O6WNsmTfIjhDC6MdCYZ+NIf UZ6YTb3veS/On+JyIGsr/soWO4pZ2Jc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738719717; a=rsa-sha256; cv=none; b=hneh94CRJmB9mbs3zGnquj7MnlXvZWYm3TcwWxsTKUk7QiA59n7lXC6vn2MFOx0mcOCdC6 ND+vlvV0XQCpmhOGO9R+qIOYstHc5KOQrY+drK/X4cp99Jk5wxNOhZYAPuf35gYzD9kzTY MzQQJZ/3JJkCzcVhGIac6oUGVWqpN7c= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf26.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 1tfUP4-000000004Cs-3Bjg; Tue, 04 Feb 2025 20:40:34 -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 v8 09/12] x86/mm: enable broadcast TLB invalidation for multi-threaded processes Date: Tue, 4 Feb 2025 20:39:58 -0500 Message-ID: <20250205014033.3626204-10-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8983B140002 X-Stat-Signature: bfqmak3jfnay6n5tuwci8u6mijz1dmx6 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1738719717-250567 X-HE-Meta: U2FsdGVkX19bMC6azzPq7+7ZqVoFV5MfVb7OYL40pvjusoIOE/qmfE8dUlNBbvYnaTq1E0HjCQtHE9xGR8QG9m5sKA+yon6K63iR7x15YbsYbLqJRG+KLdcDOrEkUnciOwUw3MD1+HblNQSU8sp5WJ6gdEngDKat6Qn9Va01gq/xKF82sl04CIy6qmN6drJuUl58crTZq5vCWL6NhoOl9wyLzqQsCNJoKUG34JjqAm4BZE7On8QaqNAYA4T0msq28AYA/gfbFqj9xiOdIlc1qvtVHn2ULeKrUtnkNPQAV8spgIWC1rgh1bOKqc/NWpcbedT1iGkeo1nTnRhLgkpbdNpGwazvjJv39OWnS7dyu/oDr/ywVpj3ZluQKD20JNMQJag0zwdtXCS7aFC6sruN9KclnqFWMsAm75BxEKBkAh6EPWdtuwUuzuxZj/cuwwEwHEQuzvABJy+VUtbQgOMnpsgxyB+foaChQ0KmSp2P3y7AEt3q8WvWv1jytZo9MIb8GmV0YJCnriUcVECQm4ZkbUeU+ZoPYOz13AJ15GIlum6Uu1OX0FxRyQe3VHBxU5wbi5wHdYdO8bnl74YCbf9YPl6E9tTamlZYSXST8L2BiiM0PynAYHEE3ffBRrQ0smWa0FHIDFHEnISJujJkNzyEC/KffWwccaMi3j1eomsHYcDkAmp9uF+iDjm+ytupeayQyKChu3tKSexCabbc3jAMhGib+cYr1t0UmpyDW27CivpKnVGuFjFPYRiQG8jUv4ZZfcfrbgrrU5BybGmBwgXp6XqfYRf1EtPZ60kc2vmUSeE/i7YutU3o4fits1AcsB4zIKJRyNg21DtDGsuYfShVFlxdBLakn8TFQ7ZLJs2KePiHoU6gY0zfZSm91OnYih6YZvd4VLuJHEdrGgb6mjGmJKMUY82lFBOxWF7MN5qAfFgrGzrrcK/ofyWty9CE/AWF5XIptu+t58KZhGwYebs OPi8/SYj +71mS77EigKoKuuCNfRhXha4kqHU8/WPmN5dUKBp6hRqGUvDccvcIK0a0XFuTNFhIDfoM/UTCt7XF3nSL50QmOPkT5MfErul1pUAuWFflz+SzWlj2/N49wUgaAcHEw4RLwuIs8GwY/SEBc+SDRy55WFbLLXOwafcERhgkoIkkTmoUfljmHnCVCHRyS2z2J/VWHbcf+FemI2B03h0AZPAqGwrJUMe/bAbKeSByQ4kdepcqQsvrAvioducUVFjNyg6Z8GQlnQp5sQm9W0k6qjG1AAQSYMZhMfRE+LwMcLK3DtjibDM11UVfbZn05kstBcFQ3MogH31BdopMcCXyjCLJ1qU/NpUun0z04PxCKgxV/4tcNeJF90Zaw69OO5zUrW33RvHOaN3OFZCXXNt17AJritqz2aH8kPlrMBtQty61mUcS0emih0a4EM+xmQPzzwKDVM6nc0jO8+c41GmilUGMfub/oojVN3SXjb7HwAf54EFL06IEQijdHJp6YQ== 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..7e2f3f7f6455 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -239,6 +240,78 @@ void flush_tlb_one_kernel(unsigned long addr); void flush_tlb_multi(const struct cpumask *cpumask, const struct flush_tlb_info *info); +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH +static inline bool is_dyn_asid(u16 asid) +{ + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return true; + + return asid < TLB_NR_DYN_ASIDS; +} + +static inline bool is_global_asid(u16 asid) +{ + return !is_dyn_asid(asid); +} + +static inline bool in_asid_transition(const struct flush_tlb_info *info) +{ + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return false; + + return info->mm && READ_ONCE(info->mm->context.asid_transition); +} + +static inline u16 mm_global_asid(struct mm_struct *mm) +{ + u16 asid; + + if (!cpu_feature_enabled(X86_FEATURE_INVLPGB)) + return 0; + + asid = smp_load_acquire(&mm->context.global_asid); + + /* mm->context.global_asid is either 0, or a global ASID */ + VM_WARN_ON_ONCE(asid && is_dyn_asid(asid)); + + return asid; +} +#else +static inline bool is_dyn_asid(u16 asid) +{ + return true; +} + +static inline bool is_global_asid(u16 asid) +{ + return false; +} + +static inline bool in_asid_transition(const struct flush_tlb_info *info) +{ + return false; +} + +static inline u16 mm_global_asid(struct mm_struct *mm) +{ + return 0; +} + +static inline bool needs_global_asid_reload(struct mm_struct *next, u16 prev_asid) +{ + return false; +} + +static inline void broadcast_tlb_flush(struct flush_tlb_info *info) +{ + VM_WARN_ON_ONCE(1); +} + +static inline void consider_global_asid(struct mm_struct *mm) +{ +} +#endif + #ifdef CONFIG_PARAVIRT #include #endif diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 25c13416a0e8..7b363ae1569b 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -74,13 +74,15 @@ * use different names for each of them: * * ASID - [0, TLB_NR_DYN_ASIDS-1] - * the canonical identifier for an mm + * the canonical identifier for an mm, dynamically allocated on each CPU + * [TLB_NR_DYN_ASIDS, MAX_ASID_AVAILABLE-1] + * the canonical, global identifier for an mm, identical across all CPUs * - * kPCID - [1, TLB_NR_DYN_ASIDS] + * kPCID - [1, MAX_ASID_AVAILABLE] * the value we write into the PCID part of CR3; corresponds to the * ASID+1, because PCID 0 is special. * - * uPCID - [2048 + 1, 2048 + TLB_NR_DYN_ASIDS] + * uPCID - [2048 + 1, 2048 + MAX_ASID_AVAILABLE] * for KPTI each mm has two address spaces and thus needs two * PCID values, but we can still do with a single ASID denomination * for each mm. Corresponds to kPCID + 2048. @@ -225,6 +227,20 @@ static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, return; } + /* + * TLB consistency for global ASIDs is maintained with broadcast TLB + * flushing. The TLB is never outdated, and does not need flushing. + */ + if (IS_ENABLED(CONFIG_X86_BROADCAST_TLB_FLUSH) && static_cpu_has(X86_FEATURE_INVLPGB)) { + u16 global_asid = mm_global_asid(next); + + if (global_asid) { + *new_asid = global_asid; + *need_flush = false; + return; + } + } + if (this_cpu_read(cpu_tlbstate.invalidate_other)) clear_asid_other(); @@ -251,6 +267,272 @@ static void choose_new_asid(struct mm_struct *next, u64 next_tlb_gen, *need_flush = true; } +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH +/* + * Logic for broadcast TLB invalidation. + */ +static DEFINE_RAW_SPINLOCK(global_asid_lock); +static u16 last_global_asid = MAX_ASID_AVAILABLE; +static DECLARE_BITMAP(global_asid_used, MAX_ASID_AVAILABLE) = { 0 }; +static DECLARE_BITMAP(global_asid_freed, MAX_ASID_AVAILABLE) = { 0 }; +static int global_asid_available = MAX_ASID_AVAILABLE - TLB_NR_DYN_ASIDS - 1; + +static void reset_global_asid_space(void) +{ + lockdep_assert_held(&global_asid_lock); + + /* + * A global TLB flush guarantees that any stale entries from + * previously freed global ASIDs get flushed from the TLB + * everywhere, making these global ASIDs safe to reuse. + */ + invlpgb_flush_all_nonglobals(); + + /* + * Clear all the previously freed global ASIDs from the + * broadcast_asid_used bitmap, now that the global TLB flush + * has made them actually available for re-use. + */ + bitmap_andnot(global_asid_used, global_asid_used, + global_asid_freed, MAX_ASID_AVAILABLE); + bitmap_clear(global_asid_freed, 0, MAX_ASID_AVAILABLE); + + /* + * ASIDs 0-TLB_NR_DYN_ASIDS are used for CPU-local ASID + * assignments, for tasks doing IPI based TLB shootdowns. + * Restart the search from the start of the global ASID space. + */ + last_global_asid = TLB_NR_DYN_ASIDS; +} + +static u16 get_global_asid(void) +{ + + u16 asid; + + lockdep_assert_held(&global_asid_lock); + + /* The previous allocated ASID is at the top of the address space. */ + if (last_global_asid >= MAX_ASID_AVAILABLE - 1) + reset_global_asid_space(); + + asid = find_next_zero_bit(global_asid_used, MAX_ASID_AVAILABLE, last_global_asid); + + if (asid >= MAX_ASID_AVAILABLE) { + /* This should never happen. */ + VM_WARN_ONCE(1, "Unable to allocate global ASID despite %d available\n", global_asid_available); + return 0; + } + + /* Claim this global ASID. */ + __set_bit(asid, global_asid_used); + last_global_asid = asid; + global_asid_available--; + return asid; +} + +/* + * Returns true if the mm is transitioning from a CPU-local ASID to a global + * (INVLPGB) ASID, or the other way around. + */ +static bool needs_global_asid_reload(struct mm_struct *next, u16 prev_asid) +{ + u16 global_asid = mm_global_asid(next); + + if (global_asid && prev_asid != global_asid) + return true; + + if (!global_asid && is_global_asid(prev_asid)) + return true; + + return false; +} + +void destroy_context_free_global_asid(struct mm_struct *mm) +{ + if (!mm->context.global_asid) + return; + + guard(raw_spinlock_irqsave)(&global_asid_lock); + + /* The global ASID can be re-used only after flush at wrap-around. */ + __set_bit(mm->context.global_asid, global_asid_freed); + + mm->context.global_asid = 0; + global_asid_available++; +} + +/* + * Check whether a process is currently active on more than "threshold" CPUs. + * This is a cheap estimation on whether or not it may make sense to assign + * a global ASID to this process, and use broadcast TLB invalidation. + */ +static bool mm_active_cpus_exceeds(struct mm_struct *mm, int threshold) +{ + int count = 0; + int cpu; + + /* This quick check should eliminate most single threaded programs. */ + if (cpumask_weight(mm_cpumask(mm)) <= threshold) + return false; + + /* Slower check to make sure. */ + for_each_cpu(cpu, mm_cpumask(mm)) { + /* Skip the CPUs that aren't really running this process. */ + if (per_cpu(cpu_tlbstate.loaded_mm, cpu) != mm) + continue; + + if (per_cpu(cpu_tlbstate_shared.is_lazy, cpu)) + continue; + + if (++count > threshold) + return true; + } + return false; +} + +/* + * Assign a global ASID to the current process, protecting against + * races between multiple threads in the process. + */ +static void use_global_asid(struct mm_struct *mm) +{ + u16 asid; + + guard(raw_spinlock_irqsave)(&global_asid_lock); + + /* This process is already using broadcast TLB invalidation. */ + if (mm->context.global_asid) + return; + + /* The last global ASID was consumed while waiting for the lock. */ + if (!global_asid_available) { + VM_WARN_ONCE(1, "Ran out of global ASIDs\n"); + return; + } + + asid = get_global_asid(); + if (!asid) + return; + + /* + * Notably flush_tlb_mm_range() -> broadcast_tlb_flush() -> + * finish_asid_transition() needs to observe asid_transition = true + * once it observes global_asid. + */ + mm->context.asid_transition = true; + smp_store_release(&mm->context.global_asid, asid); +} + +static bool meets_global_asid_threshold(struct mm_struct *mm) +{ + if (!global_asid_available) + return false; + + /* + * Assign a global ASID if the process is active on + * 4 or more CPUs simultaneously. + */ + return mm_active_cpus_exceeds(mm, 3); +} + +static void consider_global_asid(struct mm_struct *mm) +{ + if (!static_cpu_has(X86_FEATURE_INVLPGB)) + return; + + /* Check every once in a while. */ + if ((current->pid & 0x1f) != (jiffies & 0x1f)) + return; + + if (meets_global_asid_threshold(mm)) + use_global_asid(mm); +} + +static void finish_asid_transition(struct flush_tlb_info *info) +{ + struct mm_struct *mm = info->mm; + int bc_asid = mm_global_asid(mm); + int cpu; + + if (!READ_ONCE(mm->context.asid_transition)) + return; + + for_each_cpu(cpu, mm_cpumask(mm)) { + /* + * The remote CPU is context switching. Wait for that to + * finish, to catch the unlikely case of it switching to + * the target mm with an out of date ASID. + */ + while (READ_ONCE(per_cpu(cpu_tlbstate.loaded_mm, cpu)) == LOADED_MM_SWITCHING) + cpu_relax(); + + if (READ_ONCE(per_cpu(cpu_tlbstate.loaded_mm, cpu)) != mm) + continue; + + /* + * If at least one CPU is not using the global ASID yet, + * send a TLB flush IPI. The IPI should cause stragglers + * to transition soon. + * + * This can race with the CPU switching to another task; + * that results in a (harmless) extra IPI. + */ + if (READ_ONCE(per_cpu(cpu_tlbstate.loaded_mm_asid, cpu)) != bc_asid) { + flush_tlb_multi(mm_cpumask(info->mm), info); + return; + } + } + + /* All the CPUs running this process are using the global ASID. */ + WRITE_ONCE(mm->context.asid_transition, false); +} + +static void broadcast_tlb_flush(struct flush_tlb_info *info) +{ + bool pmd = info->stride_shift == PMD_SHIFT; + unsigned long maxnr = invlpgb_count_max; + unsigned long asid = info->mm->context.global_asid; + unsigned long addr = info->start; + unsigned long nr; + + /* Flushing multiple pages at once is not supported with 1GB pages. */ + if (info->stride_shift > PMD_SHIFT) + maxnr = 1; + + /* + * TLB flushes with INVLPGB are kicked off asynchronously. + * The inc_mm_tlb_gen() guarantees page table updates are done + * before these TLB flushes happen. + */ + if (info->end == TLB_FLUSH_ALL) { + invlpgb_flush_single_pcid_nosync(kern_pcid(asid)); + /* Do any CPUs supporting INVLPGB need PTI? */ + if (static_cpu_has(X86_FEATURE_PTI)) + invlpgb_flush_single_pcid_nosync(user_pcid(asid)); + } else do { + /* + * Calculate how many pages can be flushed at once; if the + * remainder of the range is less than one page, flush one. + */ + nr = min(maxnr, (info->end - addr) >> info->stride_shift); + nr = max(nr, 1); + + invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd); + /* Do any CPUs supporting INVLPGB need PTI? */ + if (static_cpu_has(X86_FEATURE_PTI)) + invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd); + + addr += nr << info->stride_shift; + } while (addr < info->end); + + finish_asid_transition(info); + + /* Wait for the INVLPGBs kicked off above to finish. */ + tlbsync(); +} +#endif /* CONFIG_X86_BROADCAST_TLB_FLUSH */ + /* * Given an ASID, flush the corresponding user ASID. We can delay this * until the next time we switch to it. @@ -556,8 +838,9 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, */ if (prev == next) { /* Not actually switching mm's */ - VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) != - next->context.ctx_id); + VM_WARN_ON(is_dyn_asid(prev_asid) && + this_cpu_read(cpu_tlbstate.ctxs[prev_asid].ctx_id) != + next->context.ctx_id); /* * If this races with another thread that enables lam, 'new_lam' @@ -573,6 +856,23 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, !cpumask_test_cpu(cpu, mm_cpumask(next)))) cpumask_set_cpu(cpu, mm_cpumask(next)); + /* + * Check if the current mm is transitioning to a new ASID. + */ + if (needs_global_asid_reload(next, prev_asid)) { + next_tlb_gen = atomic64_read(&next->context.tlb_gen); + + choose_new_asid(next, next_tlb_gen, &new_asid, &need_flush); + goto reload_tlb; + } + + /* + * Broadcast TLB invalidation keeps this PCID up to date + * all the time. + */ + if (is_global_asid(prev_asid)) + return; + /* * If the CPU is not in lazy TLB mode, we are just switching * from one thread in a process to another thread in the same @@ -606,6 +906,13 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, */ cond_mitigation(tsk); + /* + * Let nmi_uaccess_okay() and finish_asid_transition() + * know that we're changing CR3. + */ + this_cpu_write(cpu_tlbstate.loaded_mm, LOADED_MM_SWITCHING); + barrier(); + /* * Leave this CPU in prev's mm_cpumask. Atomic writes to * mm_cpumask can be expensive under contention. The CPU @@ -620,14 +927,12 @@ void switch_mm_irqs_off(struct mm_struct *unused, struct mm_struct *next, next_tlb_gen = atomic64_read(&next->context.tlb_gen); choose_new_asid(next, next_tlb_gen, &new_asid, &need_flush); - - /* Let nmi_uaccess_okay() know that we're changing CR3. */ - this_cpu_write(cpu_tlbstate.loaded_mm, LOADED_MM_SWITCHING); - barrier(); } +reload_tlb: new_lam = mm_lam_cr3_mask(next); if (need_flush) { + VM_WARN_ON_ONCE(is_global_asid(new_asid)); this_cpu_write(cpu_tlbstate.ctxs[new_asid].ctx_id, next->context.ctx_id); this_cpu_write(cpu_tlbstate.ctxs[new_asid].tlb_gen, next_tlb_gen); load_new_mm_cr3(next->pgd, new_asid, new_lam, true); @@ -746,7 +1051,7 @@ static void flush_tlb_func(void *info) const struct flush_tlb_info *f = info; struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm); u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid); - u64 local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen); + u64 local_tlb_gen; bool local = smp_processor_id() == f->initiating_cpu; unsigned long nr_invalidate = 0; u64 mm_tlb_gen; @@ -769,6 +1074,16 @@ static void flush_tlb_func(void *info) if (unlikely(loaded_mm == &init_mm)) return; + /* Reload the ASID if transitioning into or out of a global ASID */ + if (needs_global_asid_reload(loaded_mm, loaded_mm_asid)) { + switch_mm_irqs_off(NULL, loaded_mm, NULL); + loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid); + } + + /* Broadcast ASIDs are always kept up to date with INVLPGB. */ + if (is_global_asid(loaded_mm_asid)) + return; + VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].ctx_id) != loaded_mm->context.ctx_id); @@ -786,6 +1101,8 @@ static void flush_tlb_func(void *info) return; } + local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen); + if (unlikely(f->new_tlb_gen != TLB_GENERATION_INVALID && f->new_tlb_gen <= local_tlb_gen)) { /* @@ -953,7 +1270,7 @@ STATIC_NOPV void native_flush_tlb_multi(const struct cpumask *cpumask, * up on the new contents of what used to be page tables, while * doing a speculative memory access. */ - if (info->freed_tables) + if (info->freed_tables || in_asid_transition(info)) on_each_cpu_mask(cpumask, flush_tlb_func, (void *)info, true); else on_each_cpu_cond_mask(should_flush_tlb, flush_tlb_func, @@ -1054,9 +1371,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 Wed Feb 5 01:39:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13960413 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 B1E02C02193 for ; Wed, 5 Feb 2025 01:42:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2F86280031; Tue, 4 Feb 2025 20:42:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CB645280029; Tue, 4 Feb 2025 20:42:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B5573280031; Tue, 4 Feb 2025 20:42:02 -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 979B9280029 for ; Tue, 4 Feb 2025 20:42:02 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 5123AC05DF for ; Wed, 5 Feb 2025 01:42:02 +0000 (UTC) X-FDA: 83084190084.25.9E06255 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf04.hostedemail.com (Postfix) with ESMTP id CDFA140004 for ; Wed, 5 Feb 2025 01:42:00 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf04.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738719720; 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=jF+03qR1ZAoTeFkcz4dgiKIgM8Ua8410RffZlkziHnQ=; b=Jl890UrhunwB20tNM7f4TI9giAVAaO/gxG5inqLA+juPqHY3wmq1nOU9cz/4nkqWMY/6Hg kJOfSUjpxhM/jwvJ9v8pbXy/HKT8gfWSPOOH6hQnU+5FyxJY4UJq01XwtDTD9lUeh81D6W 2PzDbJmBirsiGSs+I1M6Epjt/oBJV4M= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738719720; a=rsa-sha256; cv=none; b=G8l4JPH6j34xJ6ti3dRc3TSHSi7D9pX2HvLTtos/yK3gwe7tzWAvXEXRFXwwqnuWPkyoas /4LQcwhB5uaP2F/IfWaHlyqMoKbY2ouagryD1r8Q9/rLTMmf6wfUvOvrVZd79g68zylaQG D1M/eLyPGjieDqwJz6JDeMAjd6C+dHU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf04.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com 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 1tfUP4-000000004Cs-3JEC; Tue, 04 Feb 2025 20:40:34 -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 v8 10/12] x86/mm: do targeted broadcast flushing from tlbbatch code Date: Tue, 4 Feb 2025 20:39:59 -0500 Message-ID: <20250205014033.3626204-11-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: CDFA140004 X-Stat-Signature: mhg9aqdt7ehrk1bwnt5h4c65k51oiues X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1738719720-36247 X-HE-Meta: U2FsdGVkX18JIEWQXJdUrw1N1gi7gHbat4y3an0Pp4kTOsl8w1uLgTNrqvfbxobD/QjjxBUzBscuZGYGQg07p1poV/EusUgMEe1XY9SkGYNl+l7XpMg7yrjpv651Ev4Mjqs6Ou45IJIWesfNTYT7cPXmA6CoNR8stzFd1ZDd9WwGlbE0C34dm5lY6luTRRLfy4CvXXruJJjcuVIJKA4PE9aAB6cOfHEWqfIBzcthrsDwh4K1+xDO/w2cX/Y3m3cBnqWgDz+5pijypWnDGy8o7st+CsuAAIIGl7c2tJ4K76dJhnRJmcr0i/MOAROqWKqRzZ/Tb3Q+N/w2hxtvRotjX4hc4f8Jw2VMt7bt7nr0FWdR9tnQkBRYPdOf/zke2284P57VuOeuLS6hcnqTzAdUXjHC96Co4zY2tEXejjKuqaiUegT2s9SzytvPa+cEvOLm/vjZ2eWDTqD4HEBla8i87C921SFTPjYq2lZudSUIRn+/Gov3A73kMMNWG6aZ7nb7E4jJR0JBx72W1p4Wbw48afEYLAkky5/dPXJ9d/l+znfF7yHZJ7/imSwFomJd7iNnzlPDyXafYW+mKMcZKDLK+8Ekeh6prFQU0l8wYyJjzfrvfpUx0v2fZ3vPz1UOpLcZFh+3eSqr8DztXRASguDvlMGXZ8a7dnoQXL0Bd9/jwkS/vhnuEoBj5ySQlqB0VGReAY1ddEXo3um/faMYPGwv5CyX043tfUIMyxkJdII8RUglM0ro1UeK6Se8MaQ3t5D/AHoXISR5sOWkUgVAx6gfat6sybuv9yFCesEi8PFO1nwAQnNNSGnMLvCENd4TB0s6jcvorHytm9Eim9PHbzijFNclUuKElT3WZAGAtoyJDtjpBv3Q7PsoaUV37yL8RVRyy7/KM6kMmU3W0NFqXlfuez4k+dLcV6FBN6vShi+o4BEVk+pP1X41OF5kQgox/0SX8WUjobfIKNIaN2jbK3Z dRL811Sn fqPzdl/hyGbanlYVkYIRcU9/Wgfu8bRZR4xBu+r/uKK9vRRFK4dajBrSNJ3PbErM+0JrBmNyGRaaxcxBdSiTOHgJlO1rlh8HtiA1fX7bm+9fUpJf4nNgzNRlraG3SmeFII79plCs1wM8B4n0lPwn9Q0BF6A2D+TOquMP0nwMp8N+CgQQ+tTQN2SCC556C8oajdDU+obFWJbD5Q64IpeuZamm/y9c3b7FqXcK+BsbOTp5yaquqBGtShgj4kvtIZo9P2ip3u3FWKpyErrUqrgkCnvg3JAXVgl/I1nN9KLGjzwGXaepVdqn17iNlME1KgVL8EATrwyHLtuy6jan7sN/G4sx8b9JgvRX/3kmUOnIb5kc9JOGnHtHh7eh9B0AUy7cAL9/AZ6exqy2t5HgNuMx+ZPXbqIVMPJSoWvfC3cRAKlvLGEysMZzQWKGwETJvkguuoxPKvprOoyB0hYDi7d/H8TCyVflp8WQTJxpocSveuopVI7n4+oIP/2xlQw== 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/tlbbatch.h | 1 + arch/x86/include/asm/tlbflush.h | 12 ++----- arch/x86/mm/tlb.c | 57 +++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/tlbbatch.h b/arch/x86/include/asm/tlbbatch.h index 1ad56eb3e8a8..f9a17edf63ad 100644 --- a/arch/x86/include/asm/tlbbatch.h +++ b/arch/x86/include/asm/tlbbatch.h @@ -10,6 +10,7 @@ struct arch_tlbflush_unmap_batch { * the PFNs being flushed.. */ struct cpumask cpumask; + bool used_invlpgb; }; #endif /* _ARCH_X86_TLBBATCH_H */ diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 7e2f3f7f6455..f8aaa4bcb4d8 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -359,21 +359,15 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm) return atomic64_inc_return(&mm->context.tlb_gen); } -static inline void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, - struct mm_struct *mm, - unsigned long uaddr) -{ - inc_mm_tlb_gen(mm); - cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm)); - mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); -} - static inline void arch_flush_tlb_batched_pending(struct mm_struct *mm) { flush_tlb_mm(mm); } extern void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch); +extern void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, + struct mm_struct *mm, + unsigned long uaddr); static inline bool pte_flags_need_flush(unsigned long oldflags, unsigned long newflags, diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 7b363ae1569b..c064e27df1f3 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1646,9 +1646,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(); @@ -1657,12 +1655,65 @@ void arch_tlbbatch_flush(struct arch_tlbflush_unmap_batch *batch) local_irq_enable(); } + /* + * If we issued (asynchronous) INVLPGB flushes, wait for them here. + * The cpumask above contains only CPUs that were running tasks + * not using broadcast TLB flushing. + */ + if (cpu_feature_enabled(X86_FEATURE_INVLPGB) && batch->used_invlpgb) { + tlbsync(); + migrate_enable(); + batch->used_invlpgb = false; + } + cpumask_clear(&batch->cpumask); put_flush_tlb_info(); put_cpu(); } +void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, + struct mm_struct *mm, + unsigned long uaddr) +{ + u16 asid = mm_global_asid(mm); + + if (asid) { + /* + * Queue up an asynchronous invalidation. The corresponding + * TLBSYNC is done in arch_tlbbatch_flush(), and must be done + * on the same CPU. + */ + if (!batch->used_invlpgb) { + batch->used_invlpgb = true; + migrate_disable(); + } + invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false); + /* Do any CPUs supporting INVLPGB need PTI? */ + if (static_cpu_has(X86_FEATURE_PTI)) + invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false); + + /* + * Some CPUs might still be using a local ASID for this + * process, and require IPIs, while others are using the + * global ASID. + * + * In this corner case we need to do both the broadcast + * TLB invalidation, and send IPIs. The IPIs will help + * stragglers transition to the broadcast ASID. + */ + if (READ_ONCE(mm->context.asid_transition)) + asid = 0; + } + + if (!asid) { + inc_mm_tlb_gen(mm); + cpumask_or(&batch->cpumask, &batch->cpumask, mm_cpumask(mm)); + } + + mmu_notifier_arch_invalidate_secondary_tlbs(mm, 0, -1UL); +} + /* * Blindly accessing user memory from NMI context can be dangerous * if we're in the middle of switching the current user task or From patchwork Wed Feb 5 01:40:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13960405 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 28357C02193 for ; Wed, 5 Feb 2025 01:42:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 84DED280028; Tue, 4 Feb 2025 20:41:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7F977280009; Tue, 4 Feb 2025 20:41:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6737E280028; Tue, 4 Feb 2025 20:41:58 -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 44569280009 for ; Tue, 4 Feb 2025 20:41:58 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id E4E0114041D for ; Wed, 5 Feb 2025 01:41:57 +0000 (UTC) X-FDA: 83084189874.29.E85B377 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf13.hostedemail.com (Postfix) with ESMTP id 6071520007 for ; Wed, 5 Feb 2025 01:41:56 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=pass (imf13.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=1738719716; 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=JXVQP0OwIsip1IDNIgeDYW9CH/oVY1PdsdFiT6pDbgNUr/jza+YyL1zeRp73Yj6CQYOJnq llewxfYEAWIvcVg4Mx87Ko08V12+6jxEuHH8jXBwMgKgFeqHzlUy+LRSBG623+nE4HvekZ E9gJhaKXhYEM0vSZk1PvSllBRureyD0= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=pass (imf13.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=1738719716; a=rsa-sha256; cv=none; b=Dx1KyCKpwbJMths07szX2yFzQqilhx6fV/SNu3RrJjjy+1jL2bieyscEpdaE0ZSxdHjhnN IH+TQMadTQTpPs1vM2md6mT0VOagSyLHi6na9AdNS14zFffQuXfSWWRPx4is5UfnjTU5QZ +OfrXLhL0sAvPNJPH/32M+FwHohhcRY= 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 1tfUP4-000000004Cs-3P3F; Tue, 04 Feb 2025 20:40:34 -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 v8 11/12] x86/mm: enable AMD translation cache extensions Date: Tue, 4 Feb 2025 20:40:00 -0500 Message-ID: <20250205014033.3626204-12-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6071520007 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: wyme47iyrc5ci7mpemqcsh5rg8sm4pz1 X-HE-Tag: 1738719716-524801 X-HE-Meta: U2FsdGVkX18oHgBW4kUl3johChy22y5W1o+i5g2UpifiZRcxq4f9D5P3xG3/QmewqS+2eU8lzpp7Kfvg3qkth33+DMaXWuyDdxpzihS/Em9c3Y9LOMZSAccQvQNfQv0WGpPi9QeWBRD4oHlz2z5se9/kDqiEtIxYNo0v2LJrQ9qDtZk5SwL9xJ/Gffjr/RdYvwNV1008umSsK/7ZxABZOGW8w1vI8rj0+/D86epYDVjjhdnbUuahBaDQ4up4M5vNq00GsxnADyPIhY9iCSSuOeS49UilUpxDMEoxEssE6C+1LYxtIY/meMRLswqagQZ7qrGf0AvifFwNgaJaMco9r/PxVf+XAFQy/etHu2nvp24NAk7hxprSIMPwcb/vjFDDqwhkJXnxhovu1jEJwIcnaX4E4nCgJUelhsz8+er07DbgR4QS3A5MkX9zosRC5iDDpUpCLPgPz7VNxteviZW/VfKScskgx51DUm6Qs7QM4hAX9p/TojCPSLwT66O6JBNVA6y2c/SntNVVdGg4yWL5/vvm1SXKA8GnqBw32LogQbilAt2lqT8NOCznhjy6/KsOZRRTNWdJg7Ljy+H0aVSUpNhtcSMd1xr3TSq+By+++1zT5mCn90a4w7NXfu4oLJtvjs4Zu5aypTl/uyo/TOpdAqTaLy6iUR1doOWGaVha12LhjpuznC+VQdfNuEm7WEyeS7jFIbYQlhOfRmpOHlKWANV1Yx9HrLDifwrbkrgEAu8EBO1+rdK0GIW2gD8tVGuihrPgMX4Z8QAgSIuXZAopRM1GN7GAKlPJb/51gyvf1TZVLqTMLWFgJ+NQg/65c8SNJGIEnlZiNb5lTDm5L40ZjanfJYDW55Ljgb6UPRlFxgYYnKBsuJ+flL00/ShY9kRUAL5kjDdXCcwCVdapKxVRTKIDVaeQsRXNKp1m1zxfjAvn57wPeuvJ+VHGZi9/ePoLSroBha0hyT+CxnOMWPc eoVfDapt siZVTYtEYZ0uwYm3jIZ/WLMIQbW5gt6gvtjbu+rIhCCvXC2tjlCyxnX4Mol78m3LL7U6psZZlWrrDvmge5lWhfZ/lVqVeY34Gjt1YSPTpavGlYsHgl3gR9a4ZucDGKLwWxjODgUYaXH7NE7pHFwmDrhk69nn+uubtc6sb6GjtUBbqnMGRvtRgc9Bud/QAEGI9pRVSDS5t8lXPA4mdVdJbjQgfyBbN5d5DLiX6rVhn/ZtGf1QDdqJ+DIynlAhaWwkkPCIhea8sS6g3jv3D/QUhiA/HEszt4morNrM84cQcs73/kTPP2tBDtJvOcbP09mW2q5y74KvV782V57ap/f0KLQ1ZD8OjxXuTLKlvGzxmQyk0VTk4GKfXGqA572CYCq3voFOoVp8KKIetWqRUJHhXlEx/ZpkxEBghaHixiCR+Gbwnb1hhM/FMdhJHARvLjqLYd/dhntYXCQ4F4wFAY9kE3KrQTQ85EyzZFLEjaSnxnFh2b7cRCexHaHKn2Q== 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 Wed Feb 5 01:40:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13960411 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 AA8B8C02196 for ; Wed, 5 Feb 2025 01:42:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 64EB528002E; Tue, 4 Feb 2025 20:42:01 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5FD5F280029; Tue, 4 Feb 2025 20:42:01 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D7F2280030; Tue, 4 Feb 2025 20:42:01 -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 00B4A28002E for ; Tue, 4 Feb 2025 20:42:00 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B03EAC049E for ; Wed, 5 Feb 2025 01:42:00 +0000 (UTC) X-FDA: 83084190000.30.9D4BD38 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf03.hostedemail.com (Postfix) with ESMTP id 1D0AE20004 for ; Wed, 5 Feb 2025 01:41:58 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=none; spf=pass (imf03.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=1738719719; 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=wRSak9HASRXfARy40PYBB0OHspKG9XBr6pShL4AyQA0=; b=7um5wZLcbrSNvh3wM88M6XKSOtY5dt8f/7ay29WuJUxf5o6v49T91tL6h5YYXMKBQl+GpE xS31CQMn0D5D1F6pRbANoAv36TvOGuYhBFIILdbYxSVA7ef8pZz2QUhyTT7wZ6716soLz5 OtSHe9zn9Nmy+KR6cofASRSWo4spFtc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=none; spf=pass (imf03.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=1738719719; a=rsa-sha256; cv=none; b=ffA10TLa2OHZaza1jr+7MfmFj0FRht0QV1gM0O0hlL4GxBqAvVXIIgdEVB41tJsivLiPW4 QlBBFZZF2o8A4sZgOfLmNSGF+DYITy4B8qEHIUdwqzXN98XOzSKcYcuHUyPoo8A+qlhioY 4UgZXTMugvoQcjMpwI1JJA7ZCjKrsE0= 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 1tfUP4-000000004Cs-3UtC; Tue, 04 Feb 2025 20:40:34 -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 v8 12/12] x86/mm: only invalidate final translations with INVLPGB Date: Tue, 4 Feb 2025 20:40:01 -0500 Message-ID: <20250205014033.3626204-13-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250205014033.3626204-1-riel@surriel.com> References: <20250205014033.3626204-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 1D0AE20004 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: rftx3b5bzuc9ufs391mbhxdc3r6q6mud X-HE-Tag: 1738719718-504096 X-HE-Meta: U2FsdGVkX1+qluUWPjx7l9t0tLx+b2HADZaDoidubaFt+hkRvR1vNNhpZ4rsbz70RcdoZC+8l5tAFNy+wEA21FEE9Qoi+AeB7nFym1TGwbG4rBIZwXGvjsWkGdJQeJlqCPL1rtVB1xEYlk60Sd7XUYcqoMgawkcLXs0kCkydAGSYULh4DIkVIIDFczDbMsXS0PwHA0t2GxF2vIClNziqSh1/yI9WDaE6jM3mWY9ZKxeZ4l7rv+EGFyOZUnrk0Ed5KeEWoYJxVzBN8qu+TBo4ST2FKFGkVv/WcgQJdApNy/LqXg0JUBidg9WSuTZyJA2y25V/Bp4y76z9wSjC2HDXxzNMR2ZOAU/TNOQxC8h/gpjKHlXRa88DHdpH1Kqta4T99Nkos0T/usWWyOPLTuZbvzshDxMCbjonUYQ7iOgNLd/JHpTcDuL+hGSaK05qlG7gdnEyrCN+HhsXNjcHRroyCoezgqsfuUAN1l/A7kXAIcpR3I6r/Kjki5uIgDaijFFcVF3u6MCY0ZsIVudnwO0oW+Y0kkBjCi8d5pNb1SOg8h5FAhAr6By4YbmvHRC8V3z8VlHTcJTOJ4ZT3IrhXJAfDAIkRUerotZ562YEtT9MDcvmqdqJFimiwoK2rqJmCr/euBdlNZskgasTu2qbx9XXOvE4KL9nl14oRiyQLdc/bUTGOQdoLt9CDmz4LFO3huVLmDzQ0eWWzH3T++zRSMEZvIb1zQtwm21cRL8V1/gPeMUVXOgVOonPCylyhEo2VVdugeP9IIDghuT67lXmBcoUAFQ2Cni9/mNfnOyxWNLnnhb2czR5O5ftjAxCaxLlLkdLI08Ijgq2mYzSq2o44zAQcc04NIrZEj7a67St//zio7O1cAGWfuslva8WNCQIJrIDNnkrxVBeT6kvBPk6urpOZOfu43WsJ+SbMCkNm08B9y8TAJ5UUuO4OEzlezMlX3sThm+Tp1YM3wFTl4Ghhap Vvsywsut lAnN9k1580mJo5GED8hK9yoiDcelG4wvEy4QKGCUeGj2ZEdikrsPavsuXr+5wpKZb4L1asGN2v0uFEYL57LVMFWxKLtZrVkqmml1O4+4u6f7c0MQMQ1UdoqcpNpyOSRpPyfu4fvSbPPDyXG3jpH0JVodCJ2ITAMJ/DoE5XHoZYJ5Wi7sRibiArvOAtLWU2B5t4kRT65LPWPkbE+GLr1V9ykZpVYFB9xbljgBvWsux4GFCrWzfqIP+IyniMF4BOAxwhGLPe6Kkm+lsUEs+/aP9z2+X2XVg4LFzIIcOvkQFVt7Qibegj1rilCaHIGKeNVB2UdSrTVqyl6zOouu4sXz6yghY4QddNHr4ZNgu+asf7ilLetMnkVwvz03W1sv4nX9ErWNasE+t88yMRn4ZCYwSanoBZmJiZLdpi4QBy17uvFnyi7qY6mUj/xl4zlTc2StdutzURxRJeaExUqNQ9hsPoxbmGuG2FBq+1CCJcyV5/AkMNnErnJYN7IRtfw== 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 | 8 ++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h index a1d5dedd5217..5fba41671a6d 100644 --- a/arch/x86/include/asm/invlpgb.h +++ b/arch/x86/include/asm/invlpgb.h @@ -67,9 +67,15 @@ static inline void invlpgb_flush_user(unsigned long pcid, static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, unsigned long addr, u16 nr, - bool pmd_stride) + bool pmd_stride, + bool freed_tables) { - __invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA); + unsigned long flags = INVLPGB_PCID | INVLPGB_VA; + + if (!freed_tables) + flags |= INVLPGB_FINAL_ONLY; + + __invlpgb(0, pcid, addr, nr - 1, pmd_stride, flags); } /* Flush all mappings for a given PCID, not including globals. */ diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index c064e27df1f3..7b116898c547 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -518,10 +518,10 @@ static void broadcast_tlb_flush(struct flush_tlb_info *info) nr = min(maxnr, (info->end - addr) >> info->stride_shift); nr = max(nr, 1); - invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd); + invlpgb_flush_user_nr_nosync(kern_pcid(asid), addr, nr, pmd, info->freed_tables); /* Do any CPUs supporting INVLPGB need PTI? */ if (static_cpu_has(X86_FEATURE_PTI)) - invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd); + invlpgb_flush_user_nr_nosync(user_pcid(asid), addr, nr, pmd, info->freed_tables); addr += nr << info->stride_shift; } while (addr < info->end); @@ -1688,10 +1688,10 @@ void arch_tlbbatch_add_pending(struct arch_tlbflush_unmap_batch *batch, batch->used_invlpgb = true; migrate_disable(); } - invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false); + invlpgb_flush_user_nr_nosync(kern_pcid(asid), uaddr, 1, false, false); /* Do any CPUs supporting INVLPGB need PTI? */ if (static_cpu_has(X86_FEATURE_PTI)) - invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false); + invlpgb_flush_user_nr_nosync(user_pcid(asid), uaddr, 1, false, false); /* * Some CPUs might still be using a local ASID for this