From patchwork Thu Feb 13 16:13: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: 13973653 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 CFC30C021A0 for ; Thu, 13 Feb 2025 16:22:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6938A6B009A; Thu, 13 Feb 2025 11:22:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 669C56B009C; Thu, 13 Feb 2025 11:22:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5312C280001; Thu, 13 Feb 2025 11:22:38 -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 372CA6B009A for ; Thu, 13 Feb 2025 11:22:38 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D638D1411A5 for ; Thu, 13 Feb 2025 16:22:37 +0000 (UTC) X-FDA: 83115439554.30.F0BA992 Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf20.hostedemail.com (Postfix) with ESMTP id 3D75C1C001A for ; Thu, 13 Feb 2025 16:22:36 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf20.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1739463756; 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=v1NWRZNaw4jPjHPHf45p3LReOe6Fwhc0drvpjtQVzWI=; b=YzCLTsgiPfaYW/kSW+j/9sjrttOi2Vnscvk1rxguGJERU1ZKcithW+9cCleH+wHHR5q+za fri96fF3Y7tGSH/RQ2TW3tsRb+74ZTrg29ef5RxIr6rSJVd3/AEzev8bzQWmUw9nbfS0EK lPBp8JCslzHiinhLKQ4tiHWJUHTBVuc= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf20.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1739463756; a=rsa-sha256; cv=none; b=ANIXiPmijS6XZatBGhIDDwWTVPM/zfVaNZEnaR9C33G47GuiqyW56lmRVZLzKD+0C+e4W+ 6vz6QDJ1xuQc+ZBg18PUhcibGCW+bVbhMjxbCo8orZVbLU+uDmtVPQgA3VTPBl+b7GLD3I xqWhwHqkWFr/k/50SEzaaMK/opOkbMA= 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 1tibr7-000000003xx-0kxt; Thu, 13 Feb 2025 11:14:25 -0500 From: Rik van Riel To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, bp@alien8.de, peterz@infradead.org, dave.hansen@linux.intel.com, zhengqi.arch@bytedance.com, nadav.amit@gmail.com, thomas.lendacky@amd.com, kernel-team@meta.com, linux-mm@kvack.org, akpm@linux-foundation.org, jackmanb@google.com, jannh@google.com, mhklinux@outlook.com, andrew.cooper3@citrix.com, Rik van Riel , Manali Shukla Subject: [PATCH v11 05/12] x86/mm: add INVLPGB support code Date: Thu, 13 Feb 2025 11:13:56 -0500 Message-ID: <20250213161423.449435-6-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250213161423.449435-1-riel@surriel.com> References: <20250213161423.449435-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 3D75C1C001A X-Stat-Signature: tgrfrbnbsjjua8cchmjmy1w6kcppgem7 X-HE-Tag: 1739463756-940623 X-HE-Meta: U2FsdGVkX18g8tG4zWAZt+RcPzov26IkduKRst8w3NoOqvqDa73c47FsWawYqIGHsVILZrb1C3rbTZaix3/1rhGvr8CY7nhAwecshJtILHbAc+mgK3MOhlipOt0tMDS9rrxC6AYKL10ZA49jEQg7wkkcwiVhiO09fn0CsbEKSSIlHKcKb9AMLkIACPY20mblozLC/qQUs5fWY+spX3tEoikYlxQynY1xv9RsRzaVCxZxt8/jAkHwWVS4RmWM7H6zk14yq5NJRSLBd1SHkpnGNcjnVTVrE0oFBynKjHBBh3ec/Vfxs+WfJtneX9VoiQqWWf4Ts7VPF62svVQcvwhR0IZgWrJbawvJnX0tHRbHWz+/aEIutKlxGDXs0CtV7AZlfNSOxbAyRad8SflDNKmHpoR11nofKGr6Ra7iIPHukaQaYzm6LBIexHroOqw8qKdBIWWhCpI4uZiGbM74kOJ4nfhS5x7fhfjcLDgznN5JB6gAWfSlyHuKqPMPuJNDanPcld130Ly0aFi7Tg+2r5SI45fXeBUcUymXOfj3yHlVgJwAla827Z01ODGsaRVuL2EO51y58kaluKjc7wWADxfCNoRN057fmwWxDze+zAiXbd88Vd9YP9/kbwrzMECcrl5G85u72Ccd+FtfU4iKjvghfYjVW3qExlfs7aJE4yj1EZpBGF8f1C0rFOik28+szJarGr5QkLXPjx4Soipzwb7gP11O1QHJn546vRsWzf8KNkB1yNfMzpbr/dosZ/hSmV1FjwZ5dC5wbKfr1nPt1zgMuUDYG7ZzY/mqpPYPQQ3m6v+MudmhXpSqbmv0ng3qb3/xXTHoIYqffQJld0KpHPdikNkJ2cHglvjPUzHyq4oi3rlSMgjEzSfT0FNiljLNl2ga5KO/PajsifEuFAJOxnQbtFo88rP+2d8EoFOtu0ynkQLPf4UXSLMcMWX/KBwFQRYMd6gQmf7R11QqWLoPMfS rzV/RHkC NLRLnTMDngzu0Rup2NyT09lNN0gsh9HzP5860lfXT33SIRYf7E8MaUl4AiWkIJsedufAMmEvzFUmvX4u95Wijp2XISc0gl6m729Ot/iFhvZBZ+4pYg+dR6UBPQBwqDUU/FGiQO7G3BfIfYYRtAfwGDU6l1dROgGOMHPTYXfcFqnAseI4TMfFAHMN4DvF8IG14+fN/OZtsQY0QFHQoPoSbsbCJ+YsQ1nx7mthOTe7xI6elCSpFsgsTv7wuhT7DQ1u6qVlT7Bz90MUcYD/lzZwLiygyn0EAd98q7YjEBE3PA3VHZgMP2UjKBxMYZ2J4hC+LEisoPOC25F3sk5N7H7vQXUlnEUHmiYZdYC2FvP15KkPgpc7kvPUvTKeqomZSk/nLX5BC8/d7U5zfPPIIFTHeo9lvxOSPyNWoHw0iOeQRdS5hp+OI1zZ5cecFtvqAYbtdjA/V6dtSxcTbDWQGlUZbLYIA0ChRiNXiCn+WBjLomO/PmNJiuBLrsCBzgLPbxavF/BhD5AEQlwW++Oeg0mKnv/Xhg8CXI/HNqX3l X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add invlpgb.h with the helper functions and definitions needed to use broadcast TLB invalidation on AMD EPYC 3 and newer CPUs. Signed-off-by: Rik van Riel Tested-by: Manali Shukla Tested-by: Brendan Jackman Tested-by: Michael Kelley Acked-by: Dave Hansen --- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/invlpgb.h | 101 +++++++++++++++++++++++ arch/x86/include/asm/tlbflush.h | 1 + 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 arch/x86/include/asm/invlpgb.h diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index c492bdc97b05..625a89259968 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -129,6 +129,12 @@ #define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) #endif +#ifdef CONFIG_X86_BROADCAST_TLB_FLUSH +#define DISABLE_INVLPGB 0 +#else +#define DISABLE_INVLPGB (1 << (X86_FEATURE_INVLPGB & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -146,7 +152,7 @@ #define DISABLED_MASK11 (DISABLE_RETPOLINE|DISABLE_RETHUNK|DISABLE_UNRET| \ DISABLE_CALL_DEPTH_TRACKING|DISABLE_USER_SHSTK) #define DISABLED_MASK12 (DISABLE_FRED|DISABLE_LAM) -#define DISABLED_MASK13 0 +#define DISABLED_MASK13 (DISABLE_INVLPGB) #define DISABLED_MASK14 0 #define DISABLED_MASK15 0 #define DISABLED_MASK16 (DISABLE_PKU|DISABLE_OSPKE|DISABLE_LA57|DISABLE_UMIP| \ diff --git a/arch/x86/include/asm/invlpgb.h b/arch/x86/include/asm/invlpgb.h new file mode 100644 index 000000000000..a1d5dedd5217 --- /dev/null +++ b/arch/x86/include/asm/invlpgb.h @@ -0,0 +1,101 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_INVLPGB +#define _ASM_X86_INVLPGB + +#include +#include +#include + +/* + * INVLPGB does broadcast TLB invalidation across all the CPUs in the system. + * + * The INVLPGB instruction is weakly ordered, and a batch of invalidations can + * be done in a parallel fashion. + * + * TLBSYNC is used to ensure that pending INVLPGB invalidations initiated from + * this CPU have completed. + */ +static inline void __invlpgb(unsigned long asid, unsigned long pcid, + unsigned long addr, u16 extra_count, + bool pmd_stride, u8 flags) +{ + u32 edx = (pcid << 16) | asid; + u32 ecx = (pmd_stride << 31) | extra_count; + u64 rax = addr | flags; + + /* The low bits in rax are for flags. Verify addr is clean. */ + VM_WARN_ON_ONCE(addr & ~PAGE_MASK); + + /* INVLPGB; supported in binutils >= 2.36. */ + asm volatile(".byte 0x0f, 0x01, 0xfe" : : "a" (rax), "c" (ecx), "d" (edx)); +} + +/* Wait for INVLPGB originated by this CPU to complete. */ +static inline void tlbsync(void) +{ + cant_migrate(); + /* TLBSYNC: supported in binutils >= 0.36. */ + asm volatile(".byte 0x0f, 0x01, 0xff" ::: "memory"); +} + +/* + * INVLPGB can be targeted by virtual address, PCID, ASID, or any combination + * of the three. For example: + * - INVLPGB_VA | INVLPGB_INCLUDE_GLOBAL: invalidate all TLB entries at the address + * - INVLPGB_PCID: invalidate all TLB entries matching the PCID + * + * The first can be used to invalidate (kernel) mappings at a particular + * address across all processes. + * + * The latter invalidates all TLB entries matching a PCID. + */ +#define INVLPGB_VA BIT(0) +#define INVLPGB_PCID BIT(1) +#define INVLPGB_ASID BIT(2) +#define INVLPGB_INCLUDE_GLOBAL BIT(3) +#define INVLPGB_FINAL_ONLY BIT(4) +#define INVLPGB_INCLUDE_NESTED BIT(5) + +/* Flush all mappings for a given pcid and addr, not including globals. */ +static inline void invlpgb_flush_user(unsigned long pcid, + unsigned long addr) +{ + __invlpgb(0, pcid, addr, 0, 0, INVLPGB_PCID | INVLPGB_VA); + tlbsync(); +} + +static inline void invlpgb_flush_user_nr_nosync(unsigned long pcid, + unsigned long addr, + u16 nr, + bool pmd_stride) +{ + __invlpgb(0, pcid, addr, nr - 1, pmd_stride, INVLPGB_PCID | INVLPGB_VA); +} + +/* Flush all mappings for a given PCID, not including globals. */ +static inline void invlpgb_flush_single_pcid_nosync(unsigned long pcid) +{ + __invlpgb(0, pcid, 0, 0, 0, INVLPGB_PCID); +} + +/* Flush all mappings, including globals, for all PCIDs. */ +static inline void invlpgb_flush_all(void) +{ + __invlpgb(0, 0, 0, 0, 0, INVLPGB_INCLUDE_GLOBAL); + tlbsync(); +} + +/* Flush addr, including globals, for all PCIDs. */ +static inline void invlpgb_flush_addr_nosync(unsigned long addr, u16 nr) +{ + __invlpgb(0, 0, addr, nr - 1, 0, INVLPGB_INCLUDE_GLOBAL); +} + +/* Flush all mappings for all PCIDs except globals. */ +static inline void invlpgb_flush_all_nonglobals(void) +{ + __invlpgb(0, 0, 0, 0, 0, 0); + tlbsync(); +} + +#endif /* _ASM_X86_INVLPGB */ diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflush.h index e026a5cc388e..bda7080dec83 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