From patchwork Thu Jan 16 02:30:28 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13941158 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 EB9A5C02183 for ; Thu, 16 Jan 2025 02:32:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C96A46B0085; Wed, 15 Jan 2025 21:32:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C46D86B0089; Wed, 15 Jan 2025 21:32:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B0DFD6B008A; Wed, 15 Jan 2025 21:32:47 -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 943C46B0085 for ; Wed, 15 Jan 2025 21:32:47 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4D59CC0548 for ; Thu, 16 Jan 2025 02:32:47 +0000 (UTC) X-FDA: 83011741974.04.E05AACD Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf09.hostedemail.com (Postfix) with ESMTP id B2D8B140007 for ; Thu, 16 Jan 2025 02:32:45 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf09.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=1736994765; 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=1K8kB1I4+9t9TIYqSgJN8D2rNOhCjXKJjmT94NTU88I=; b=6Zk0JWkiuOLg/8QYU46H3rHH55fwpbFWbd2asnYgRCUQ2Yo8rtKxCrqbK3NdZWIrRUy7cC qeOYSfpmPj2tcr4wOc4SbutAyjUvvhZrceIHX3kWXfIPUw6gOYR6+8o5gvPq1LumLvr1cG Mif7hXOBZaBziE/+4rBvSxiSBmrVXrk= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf09.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=1736994765; a=rsa-sha256; cv=none; b=4GCDrBBqztdMXJLKVhJdzusH3RqOHGqoINnF1FEZj7ev6mCCXHzYAtBjIhmcPg2aG2/Lbr E8TlkZFmeSLBjNR3M2xE0krt6mkC+3jUS+5ZLIV2fMm+d5VDexGL3JQu5bpFFllWn+VEDQ 6l2z0ea4wa21aATg4ifEPdQXEtfh0L4= 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 1tYFfO-000000003nd-3L0b; Wed, 15 Jan 2025 21:31:30 -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 v5 05/12] x86/mm: add INVLPGB support code Date: Wed, 15 Jan 2025 21:30:28 -0500 Message-ID: <20250116023127.1531583-6-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250116023127.1531583-1-riel@surriel.com> References: <20250116023127.1531583-1-riel@surriel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: B2D8B140007 X-Stat-Signature: 1eqj46cwghihky78wbbto77zresyfj7q X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1736994765-413159 X-HE-Meta: U2FsdGVkX191vrwWhsLpuDpL5Q5qX7H1AQITK626NUxelO5vQS1ZenfrSNJ/IXNJbjfl2+YoB1ZWy1nwtF97AjQd4HfbfcZdUOKaILURJJ7UJNAzd6zFCitCvRYfxIwkatlZodvlX1li5WtSJ55Z62SauwLZQYOWk8N7beesD2OM/PTXh1kxwyf9YZY7Odoq7N89EcBV14RQolcNZPn4lMQVcLIPkEFHM15Cx+K43Gfi3NowQKavWiKvyCO3Usw2UMFtgiU6OS2RL+3jJtOMRPWNf12VTcJz5uZ4Z3hfxszu2yFpeFla/p4Fxg2EvryAeVhJV5DOjlzcO/gmo/H22Bn2iyqm2DmR/URGZ9KNwOxv0bL5Xv//CyIQJUQSon3YC/bFjNkDpQrt7FYKVC/jc88i1tPjIMBCTmjFdZsFB0mT/jZ1FjlI/tlNanNxuls3VjoG9S3KshrADhIMq48rfJ2yDfx4NqpPvmRgudbmhJlEkHIPfGEvAClP44nhvPzddy7NC/FSUYXodB1aiUiQ27/IcLzvwyGvMi5y9MLK/xmlTOu2MHyixGuracjcONnfHBu8RTEx7fkyXSxbQUgnlGkHM1Ng1xiZGgBJ3k7kmrEZoJ7OlZPhLMPk4h3vkJ0dQ2lxeypY8dra1RIyU1Bu8N8zJOypKEQvh8j/KK2V4InslRrcNKfwRlcJlaoKjvfTnvE1xuVwZselyAoRdTW///s5nAxHCAagTof8ClvJcZQUDZqD80hSWPiylYlmWYxwj9xAzVycLYvDFo40Pyb23EgKUvQWvVoVisdaFdZWHyWuMVYTH/hLvN46TS78MPJDQoBMZgWYPYk9LmFmpcJvdO7oTnPIxidva5ajCn5kRsrun78aacLSkjjRQWDWHfxRYftNtl5HpNp46lxFvVdRAC/1LO2Ub5DO6u3XuFaZc21nN2C5i8LgLPMbyn7zU2UCtAPAYXSuDFj6TD38cEB jOA9W8Rx HOM7lJMm5uT8Cs3cBnbiw6wn6QI3HjTyaUS3hTv4Q3gKlhNhosnAR/B9dr1eZEux3CpMFTUOyq7a5+M5bPoNkLjImT5sBz7k02k1ZMR1TlqLPe8v6f+QJyGYk0xZ80u+2mk1s6KnZIQ5/0Gt/s9VhXA3i9Ng6HsCNZkCpjWrABnL+jKlAKEbHRnhyBMOem7yM9vfR7cgLu36WtXFLHQo+Yu/8Rq7hJdJxZwqnhTD5Qfn9kbP1drJttVRWs5S3FuNj4kgyvMzk76ueOVho3+csR3eUbDaK1McuopHqtD30JiDgWy66Jjj0IfBBjk/8iWgSpXEuF2wkzGfLRoFK7NHNPR7ibBF9xDLwYLiri3+LGZtPAPHJ7QdicYAAvRpYKLFd0vFG 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 | 97 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/tlbflush.h | 1 + 2 files changed, 98 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..4dfd09e65fa6 --- /dev/null +++ b/arch/x86/include/asm/invlpgb.h @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_INVLPGB +#define _ASM_X86_INVLPGB + +#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, unsigned long flags) +{ + u32 edx = (pcid << 16) | asid; + u32 ecx = (pmd_stride << 31) | extra_count; + u64 rax = addr | flags; + + /* 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