From patchwork Sun Jan 12 15:53:49 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: 13936449 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 4969FE7719C for ; Sun, 12 Jan 2025 15:56:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6F70E6B009E; Sun, 12 Jan 2025 10:55:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6797A6B00A0; Sun, 12 Jan 2025 10:55:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 31E386B009E; Sun, 12 Jan 2025 10:55:44 -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 DB03C6B00A0 for ; Sun, 12 Jan 2025 10:55:43 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 98C9A161898 for ; Sun, 12 Jan 2025 15:55:43 +0000 (UTC) X-FDA: 82999250166.12.782FBCE Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf12.hostedemail.com (Postfix) with ESMTP id 0845340003 for ; Sun, 12 Jan 2025 15:55:41 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf12.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736697342; a=rsa-sha256; cv=none; b=8i2r2PUeVVzylNAqWcGYfhQqe3aDJBW2lRim345K1NNKnJ4y2Gf455f0jm0FjOtH/lZ2Hn 9oRZmrpiFTfabGgpUjHP0LwadBqmmtITmpigi5IdbTiU9ALamxUqzGRwjLYR8Ub6zHvuXb QW0uav821KRvLMVXbOpD8khlPrWa5Q0= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; dmarc=none; spf=pass (imf12.hostedemail.com: domain of riel@shelob.surriel.com designates 96.67.55.147 as permitted sender) smtp.mailfrom=riel@shelob.surriel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736697342; 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=KTwHDX9ZwNE0qLpg8jL7jj/Bv2rokHfmlVRjvUotn08=; b=5RM4CyRHg1hhCpXQDrhl3q6vluBJ4d86RhBMDLLS3ndh/ZVqv0aE25wG83/b4O08cubwae b71GM4+M2smwuy7IMzRYp9wm1K2Cnhc8NdfJK+MO5MArrzpxfdEd7pWt3mG2ISs1UdeNl+ ikHNI7gLRNbEceOkX1Op/BMuw7s5Wc8= 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 1tX0Ii-0000000010W-30FD; Sun, 12 Jan 2025 10:54:56 -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, Rik van Riel Subject: [PATCH v4 05/12] x86/mm: add INVLPGB support code Date: Sun, 12 Jan 2025 10:53:49 -0500 Message-ID: <20250112155453.1104139-6-riel@surriel.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250112155453.1104139-1-riel@surriel.com> References: <20250112155453.1104139-1-riel@surriel.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 0845340003 X-Stat-Signature: pj5ggsu13gm1pge9w1buohtatuq9dw1n X-HE-Tag: 1736697341-255506 X-HE-Meta: U2FsdGVkX1/NetAFGoU7dTpN2UvxkISfd2Z4ZduOqbutCaCfiSRWWnfdvt+hAglX37sRK0qvih7A0NHgHdvUvhmZ6thtvYkasjFHaiEX3kZa6NgMMdBjA/jyZtQ1m0nGGr1Fk5fAFpwGriTna+Rrvu4OT6KfONkHjPzV7r4/kUhtGG2pPvICw4ZHFLxys+Ej7ZD1lssMLWWbxA4CtsYyijPNomJ8zsk60sMX+bQ0KEDlQK7GmVb4uTUEfY5zqRWdc6kBOMPqiXNmJe1XvI5znVgfpEpUKEVzex+NBNthPaTDql8I4Lk2stMNyMvJsRblsNy8VdZx/xyzFZQ0ueIc29P9t92ISmo6rTSPMWKKr6hDzift7ABw+R7YbfgbueCqpu3dIWgMSZKonoptTc55F/XJuSjFubMZd+gSKDbp9LSU0bxN+3rOD0m+vAc5ZC+yUuHDyC4/djKvLRFCPqsWlpDyUJ3jt3RYCkUqy24iJ7VKyU09ryM59hiD5eU04O2JXc6FxUpza/Ov3PpeycxRbShUliklUhnp/bbcxZ0WtdrN+A+IyBCKkOVzyLczt5+IglnCk9POZSMZaMymJo87n8tXZeErPsbOTIwbgdCQJbnWLisC1Eqr7K6/CbpMTJF6yhs45L7/AOR3fKjO97O9R4AghP5CIYsQ3mnMSbAnG8eptJPNsXvUUuQ1S3jGG2ct9yZHmE/rVuiPGPzxnTotb6qvPxbm7ei8Qp91rG7RAJVJRxVEmxC22QvwU5EjDYgTdfxxlio4P8iHKhrXVrbp21a9CSDKaDF4IAg18l6BkSo2d/ia8ec6PBNlDvoIxpupAM30eu5hJ/492VzVBTwJqhreWYMUNcLNgT6uBh0LHiVzYBongFpj/+t12D+Fs4WWumUBL4kBU5zY3WaLvV0P8TmEtYUKK7xbT6qCs+hf7w6vtDyejF/+/VKKq70y2C9L1K6C8eojhn/WxtXwYMt Neno3x0/ YYvxC/B6jv31LNygHIP4IUcZJuUhpi4Zp5xdcMLHd/vcrUkjPPtcMv2ja2kU4jOvodmip1sKo8IvnfzqTppMu0okWJBd2xxue5yDn4t9y8kUT2Vv8v7HLw4yt3+0tGFmRUyFjwa1m9d1Xrp3XAUcJP/siklMCpv43RT1GoK5ARVQfv/2kqm2aADj1F64feFXNWJ8tSNA8sYfjka0MiINATWdJrxcmDTYeZTMDsoU3bPZkna6TeQUaZQojdtsq12tsa2AfeUL2X1iGTJR04oCwdmnMkXoeSDJIO4flneuYQdwHchFBGQ3BJKtNUgt+8oPzzvlqCn+MJZiwwCHkwB+HQBL++HL8dvEszOy/K0MNXBl4qQINUcrYfqIxjLYgUPpDPLGVxQJmn+6CzFg= 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 | 95 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/tlbflush.h | 1 + 2 files changed, 96 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..d62e3733a1ab --- /dev/null +++ b/arch/x86/include/asm/invlpgb.h @@ -0,0 +1,95 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_INVLPGB +#define _ASM_X86_INVLPGB + +#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, + int extra_count, bool pmd_stride, unsigned long flags) +{ + u32 edx = (pcid << 16) | asid; + u32 ecx = (pmd_stride << 31); + u64 rax = addr | flags; + + /* Protect against negative numbers. */ + extra_count = max(extra_count, 0); + ecx |= extra_count; + + asm volatile("invlpgb" : : "a" (rax), "c" (ecx), "d" (edx)); +} + +/* Wait for INVLPGB originated by this CPU to complete. */ +static inline void tlbsync(void) +{ + asm volatile("tlbsync"); +} + +/* + * 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, + int 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, int 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