From patchwork Thu Jan 23 04:23:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13947940 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B2F0BC02182 for ; Thu, 23 Jan 2025 04:30:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 45246280007; Wed, 22 Jan 2025 23:30:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 40052280004; Wed, 22 Jan 2025 23:30:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2C82D280007; Wed, 22 Jan 2025 23:30:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0BB25280004 for ; Wed, 22 Jan 2025 23:30:29 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 96A58A0B86 for ; Thu, 23 Jan 2025 04:30:28 +0000 (UTC) X-FDA: 83037440136.09.6A22A42 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf17.hostedemail.com (Postfix) with ESMTP id 15D5640004 for ; Thu, 23 Jan 2025 04:30:26 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf17.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737606627; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7WMYap4DpOjXdXgeMCTpWRI7r70hDYcNVqdkHYtAW+k=; b=mervGdgUcWjj+tC3inYJa7Tv/r73PDl7/yxqK0cuscOZTE3Fhwq207wJHeudC5iPs00V4p 1lc4SxOdUjarJzIWqpfTLMuOJwvpGD6G69nk615L6pEKBYsiJGf2QD7lhUbteNjTJ1nXAj uC3ykxp3e0nVkpb95C0bzXcrI3Vrkko= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737606627; a=rsa-sha256; cv=none; b=4iK+kU8vkWHdTPU10uZC+QOL5OBtt9++8/yaQuvaqYxgdaUnRoKZVm9A77FUMGWxBPpGqy UD/qsYOWOf1md3Z6TmH2rbnVulKqPNlDHHDm7zVue2XwNrjzIr19FOGLib7ROWdAzDIICt kRvpu1/czwsTIMsevHHMtTT+uAbiWf8= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf17.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com Received: from fangorn.home.surriel.com ([10.0.13.7]) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.97.1) (envelope-from ) id 1taols-000000005uH-2qDI; Wed, 22 Jan 2025 23:24:48 -0500 From: Rik van Riel To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, bp@alien8.de, peterz@infradead.org, dave.hansen@linux.intel.com, zhengqi.arch@bytedance.com, nadav.amit@gmail.com, thomas.lendacky@amd.com, kernel-team@meta.com, linux-mm@kvack.org, akpm@linux-foundation.org, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel Subject: [PATCH v7 05/12] x86/mm: add INVLPGB support code Date: Wed, 22 Jan 2025 23:23:24 -0500 Message-ID: <20250123042447.2259648-6-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250123042447.2259648-1-riel@surriel.com> References: <20250123042447.2259648-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 15D5640004 X-Stat-Signature: 8gjzfqhnc8kocoztf7gp9msnp6dzj6x3 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737606626-771662 X-HE-Meta: U2FsdGVkX1+/wdjuzTJ9VmGIsgJkR0Gyeg/hSJJNo+/d6PKA93Y7uv2BnNiQR5Uu5MZGXvUhhPB+qBZy7Yp5sprUT4UyztqWhz4VTS43Y3jcUGwq9zpS4n5CwUayWre/ERfNyyF1wwalm61ZvrWg2A0JbmfGw6mCIvGMZC5xMrj4e6iSLTmMioDYwfRXkqOBQ8QlHFUz5CRxk6KGpfOpk+Pqicq9HC4GxQFaL7TlOmih0oR77fqjZmVOluNmosft+lO5+CRENwTtN6s8jwzhsDJQHYgQSNavUZiZfM9CRDN/H1E/Nl3wkr5mcPf8XgLtCiq/5IJLjzNSx4wsZeNh1/awcmYvaxI4bu00IssA8/EcGuUQFSoqZzucJ9nJ26IIc/Y6RnUHP5R7pPQDI45rx7QwwpvUHFbQFIiveA4sS+BZLLXT5gbm6qPaGGZoC/fscpDLsa6S7/lbckQ6BIBN3V0CYXKJEy/qx5wMihwJxOrE0foEqPCB8h731K8T0l3ZR0YUQp8IN77QDioUAUUjN/rMCScHVLh2WmsOjk5XLCzuv00kU5Lo07eV2Tuj/Of5AtvqZRhd5AA8SNNbKldJJNKQ/OaRIHJ6eGek1Cg9ao/ZPD0gCZfLvMHqpQE0xJqjlUr3hm0Yaz1b9e3/4aU/i3rQdt9O/sr+cyNgQdi7YzZ12ZBRsfvXUyID5LW1O7oPtyi3T/lNqUYIwCK3Z43jc2HMHd5o4xOMbqjs/KtbTC7QeeqyQ4XZTS2n6v1Z+iPseM+qRUeA7mso6AH6bv/50za+3Sot/tL2nED/kftr1uD2/nfP7y9o4DevHc6O0zBSBOlM9WVKs3faHD/w+PxsP3d28e+urK5jU62oh/omA5WHi+tZRV4vJvRFqnzx9U++hL36wndfZuze8gAQeBmqQW61pXDVyggVn3saT5f83FrWyO4vI5QITxt+4mKv+NriJWQetnA3M0E64+dXm34 hNJtyvkm uaLnjchvlHEhRq/Aoif1JaAISIhYFQYOJbWNTN2STOmpQ+RLOpLCWiJ1plJ74RipPI5804Go+7BgGibXDAO0euHIZTAuqZm4GeZvJ1fklZhALH2xlivpUSk8tYaAImcTv+tzqwOMCGdVoSp64AyJ977xcYtunhcIjiJ4917kyRSCdc950raCHwNhED4+jBQTcbymPrARhZTF3mh+Nn5l+7MWaUaDOv/faD2zIPj2W8uHyAonxjUO8S6oVVbA8KrWq1Ok9PXlIyohCH1HZQFJGPKMTbzAC0GlRuSSdoCAA1KznKuoVVIA4d/D5YsFJ75JnYIN5og9H++UDl0DLwkVtLs+7rWObPPpGh1HSH8XHnjKV8OVMuurgSv/1ICyapPD3i6bm X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add invlpgb.h with the helper functions and definitions needed to use broadcast TLB invalidation on AMD EPYC 3 and newer CPUs. Signed-off-by: Rik van Riel --- arch/x86/include/asm/invlpgb.h | 101 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/tlbflush.h | 1 + 2 files changed, 102 insertions(+) create mode 100644 arch/x86/include/asm/invlpgb.h diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h new file mode 100644 index 000000000000..a1d5dedd5217 --- /dev/null +++ b/arch/x86/include/asm/invlpgb.h @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_INVLPGB +#define _ASM_X86_INVLPGB + +#include +#include +#include + +/* + * INVLPGB does broadcast TLB invalidation across all the CPUs in the system. + * + * The INVLPGB instruction is weakly ordered, and a batch of invalidations can + * be done in a parallel fashion. + * + * TLBSYNC is used to ensure that pending INVLPGB invalidations initiated from + * this CPU have completed. + */ +static inline void __invlpgb(unsigned long asid, unsigned long pcid, + unsigned long addr, u16 extra_count, + bool pmd_stride, u8 flags) +{ + u32 edx = (pcid << 16) | asid; + u32 ecx = (pmd_stride << 31) | extra_count; + u64 rax = addr | flags; + + /* The low bits in rax are for flags. Verify addr is clean. */ + VM_WARN_ON_ONCE(addr & ~PAGE_MASK); + + /* INVLPGB; supported in binutils >= 2.36. */ + asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx)); +} + +/* Wait for INVLPGB originated by this CPU to complete. */ +static inline void tlbsync(void) +{ + cant_migrate(); + /* TLBSYNC: supported in binutils >= 0.36. */ + asm volatile(".byte 0x0f, 0x01, 0xff" ::: "memory"); +} + +/* + * INVLPGB can be targeted by virtual address, PCID, ASID, or any combination + * of the three. For example: + * - INVLPGB_VA | INVLPGB_INCLUDE_GLOBAL: invalidate all TLB entries at the address + * - INVLPGB_PCID: invalidate all TLB entries matching the PCID + * + * The first can be used to invalidate (kernel) mappings at a particular + * address across all processes. + * + * The latter invalidates all TLB entries matching a PCID. + */ +#define INVLPGB_VA BIT(0) +#define INVLPGB_PCID BIT(1) +#define INVLPGB_ASID BIT(2) +#define INVLPGB_INCLUDE_GLOBAL BIT(3) +#define INVLPGB_FINAL_ONLY BIT(4) +#define INVLPGB_INCLUDE_NESTED BIT(5) + +/* Flush all mappings for a given pcid and addr, not including globals. */ +static inline void invlpgb_flush_user(unsigned long pcid, + unsigned long addr) +{ + __invlpgb(0, pcid, addr, 0, 0, INVLPGB_PCID | INVLPGB_VA); + tlbsync(); +} + +static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, + unsigned long addr, + u16 nr, + bool pmd_stride) +{ + __invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA); +} + +/* Flush all mappings for a given PCID, not including globals. */ +static inline void invlpgb_flush_single_pcid_nosync(unsigned long pcid) +{ + __invlpgb(0, pcid, 0, 0, 0, INVLPGB_PCID); +} + +/* Flush all mappings, including globals, for all PCIDs. */ +static inline void invlpgb_flush_all(void) +{ + __invlpgb(0, 0, 0, 0, 0, INVLPGB_INCLUDE_GLOBAL); + tlbsync(); +} + +/* Flush addr, including globals, for all PCIDs. */ +static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) +{ + __invlpgb(0, 0, addr, nr - 1, 0, INVLPGB_INCLUDE_GLOBAL); +} + +/* Flush all mappings for all PCIDs except globals. */ +static inline void invlpgb_flush_all_nonglobals(void) +{ + __invlpgb(0, 0, 0, 0, 0, 0); + tlbsync(); +} + +#endif /* _ASM_X86_INVLPGB */ diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index 8fe3b2dda507..dba5caa4a9f4 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include