From patchwork Fri Jul 12 17:00:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Jackman X-Patchwork-Id: 13731997 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 77AF2C3DA45 for ; Fri, 12 Jul 2024 17:01:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 73EDC6B00A9; Fri, 12 Jul 2024 13:01:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6C64F6B00AA; Fri, 12 Jul 2024 13:01:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4FDED6B00AB; Fri, 12 Jul 2024 13:01:37 -0400 (EDT) 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 2B3986B00A9 for ; Fri, 12 Jul 2024 13:01:37 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id AA52A1A0C61 for ; Fri, 12 Jul 2024 17:01:36 +0000 (UTC) X-FDA: 82331716992.15.F30AA3F Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf14.hostedemail.com (Postfix) with ESMTP id 20B7C100004 for ; Fri, 12 Jul 2024 17:01:33 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jMKbYJR2; spf=pass (imf14.hostedemail.com: domain of 3bWGRZggKCLAZQSacQdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--jackmanb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3bWGRZggKCLAZQSacQdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--jackmanb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720803659; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3PoEMkqequy1KWINMIJVW/1gdvscCRasMo3UATAb/nU=; b=kRgHdHcRvnShGU8etMpOo6OxpCZRr4JIT/1GWDnL7XpRUqiTTPfoDU3psR0D7mroN14R2G 7THPKHhPYXh5R/88VB3xPIgqlL01jrdmwluUwOyPVFTXdWJ1HsvdiPxU6K3+JQKy9j9drY b2ezUdqjhTwGYwm0cCROJkYw7VtsQSg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720803659; a=rsa-sha256; cv=none; b=GtWD3iHi2M5fOrLB8KMqx5LKB+CkSD/Bh3tMX13AZwlUpce//69za/XngeEa+CrIKG20Wy DU8rfSbGFfhP5P7NyITpgqd9jRPjz6Kd2wpgLc0D4ylZ8gmQOZh2BH9A428+P3tv5B7TKX AgFv8To4DGAMJBq9uFH7YxScYKFYW7w= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=jMKbYJR2; spf=pass (imf14.hostedemail.com: domain of 3bWGRZggKCLAZQSacQdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--jackmanb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3bWGRZggKCLAZQSacQdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--jackmanb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-65194ea3d4dso35987957b3.0 for ; Fri, 12 Jul 2024 10:01:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720803693; x=1721408493; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=3PoEMkqequy1KWINMIJVW/1gdvscCRasMo3UATAb/nU=; b=jMKbYJR2TFaMLSP6guMDVo9PdnQGFZnlw5UMcFOCweaP3uxGkNKap905XJaIJ26a0+ Ia23fcbNH3tlx9LDVj03Pk8q/TrpmhUoOeF79qdpNDM/PkohDtTOWSPMHXshoibsQ16O Vt6fR+T+/doplzKiQjLyCMkP0KT99xHUFbxUZ523F/tbanONJwb5oa2A4JuxMJVftvqO Xg4JUzdMP4As6J3tnkxq8F7e/CD1BGqORy/uc0yCoP5TLdvOzaKkTni/njRgVULNOedT MiF071EWw6fxqa4NCS+wZ993SvS+OsU6s8jo+CiJy4pa4i1itj2VEYW66I9F7XFIqJ72 9owg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720803693; x=1721408493; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=3PoEMkqequy1KWINMIJVW/1gdvscCRasMo3UATAb/nU=; b=swRVGjZeur4S8OG03/+D/DmATJPKDLX3RGeMQU/IDadUaRUCdFSN/o3xn3Bpusa2ZN 4I3VfCNk42DWtxaFScm+YXa1lma6p/Tlpcr/pcXL/S/F9x8dFVOybrPrawTIjA9xnrRM Dob6oKnvnxtR3Yx7bmqP6E/AbldUpyLMV9P42poGv4TS+L5/+dvvB3lXEGLr48lRV4B7 sgEPUPW+YouXpDhkut4Cu5FhqlBcgQTIczmya//yyedvkbF+IOKWSthainYGeY+B+rQx wwleVm7saSRQLsH3EUeEt75xRVjBgwGBrWUFfvT9Nkp5keUu6O5UDaAJjwn/k5q6bBuA 3xNg== X-Forwarded-Encrypted: i=1; AJvYcCUSeb/MDe204PRgisb/lMJxbjT4AeGc1FpZfsAaemj4VqUhKnWaTLltweIlbIAAqlmrNp5cs2wUHfQpxzWk4ReZgG4= X-Gm-Message-State: AOJu0YyTIX3fb1Sy78eWCPgSAUroqutT+fzZ69DMAMsDoDZ8NAwgcQnT FHUn7uXFA+ND2XewbkNe170DgUNjMf6JB+fMwulllW8AsnzkvTl2i1gz2bSEM0b11brqCbUD0cL uUzocdRzm3g== X-Google-Smtp-Source: AGHT+IGAsyCLoTSPrJ2N91yIZy7N9XYfi0+vS82VtwZTogujGpm4xok4sUQDdphNDQnjhHnIeN3tEJvaUVoySQ== X-Received: from beeg.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:11db]) (user=jackmanb job=sendgmr) by 2002:a05:690c:488a:b0:62c:ea0b:a447 with SMTP id 00721157ae682-658ee69b8aemr3280537b3.2.1720803693133; Fri, 12 Jul 2024 10:01:33 -0700 (PDT) Date: Fri, 12 Jul 2024 17:00:32 +0000 In-Reply-To: <20240712-asi-rfc-24-v1-0-144b319a40d8@google.com> Mime-Version: 1.0 References: <20240712-asi-rfc-24-v1-0-144b319a40d8@google.com> X-Mailer: b4 0.14-dev Message-ID: <20240712-asi-rfc-24-v1-14-144b319a40d8@google.com> Subject: [PATCH 14/26] mm: asi: Add basic infrastructure for global non-sensitive mappings From: Brendan Jackman To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , Paolo Bonzini , Alexandre Chartre , Liran Alon , Jan Setje-Eilers , Catalin Marinas , Will Deacon , Mark Rutland , Andrew Morton , Mel Gorman , Lorenzo Stoakes , David Hildenbrand , Vlastimil Babka , Michal Hocko , Khalid Aziz , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Valentin Schneider , Paul Turner , Reiji Watanabe , Junaid Shahid , Ofir Weisse , Yosry Ahmed , Patrick Bellasi , KP Singh , Alexandra Sandulescu , Matteo Rizzo , Jann Horn Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, Brendan Jackman X-Stat-Signature: kryfswr7oiinwahwp96rhfkjf5cxqjtc X-Rspamd-Queue-Id: 20B7C100004 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1720803693-488382 X-HE-Meta: U2FsdGVkX1/By7RQwpBDL4Yfexa1vqelsBxAjZd7WiAYWWl83uvBq1TExNqGSUCQyPzxCoCmVT69AOgdGBWWQmJwuqubEZflOOgtT6ErXhxA2mebbElmx8NJFYc327lPMgQEhAcj8dzIBNU0ZkiHe9/f5x2WkGSkw6bN6Z1rqO/ECtdaojvRsHVx3oq1Mkumk7YakrU5w+edFH0ph1oK/gIFzITB25Eu8PU9JF3vpVVvvUW7TOyfrkiDdCksWXFFdyTzJuDGpSIRzMTMKzGZGN+WDBZ9BgJTB5Yzi2MBVfSLRR1Q/yWdzT97UFXD3CbS2VokDUxYTr/5vHDp3BZckscvHfNYqEQIfpDb2cIiPJwFXwtW+0fJWgNeHjks9ZZd2bzczCeqPC1yYyQdUpq4cJ1y7A+Ay4rMZdGzNlYASCpQufSvpOkzikXT/O0miitRr5TPf+6WbVChDTwzr/iFEGTOJTwberiEXlau+HUi05B/iGKaVxle9vyITwDxU5B1ensDbXCCbm/vTnyiW4Co4z83hrh9cLa0t0RDegY6FlXkoLNlpmURO18tZno4oAcBRCyg/CD+v/VxLjQEWzqseRlaby22ykAkNHbF9BadF1+XVTESnzGjCmgNLKjBya1QXDpNlhqcuui61WDas7yXvusbdI3+8e9md/HK4VYm9UekUjiFFffK6UF3EsQU0fwcH8t1Q+zEIEMUZAqEUGvtO/v71B5OiGa4HL9ho1yWTPxvm1+7aYsTRgfXS/P6OJVqPPvpHYhnbH01nXTIF6XZSIPDAQUZVFIItd0WwfYA63/7koW0Pps6fzdKBUKDUor3Z+pgHdTUIne4lHln5OCmIsIdwhSPv7eb/VBOsNlEru4iD1XjfzfcG9L3rg8YOSxsNTH0O4Mg/waDVrshzeUOAaHaN6WMv5ddMEyi/r55moaq8V56qFJPegF/JISzxP4sca+T7e2kk2yoNWh3uoX zWfuZ/yo inKIP1h3sKzpeTw3mxVaoa81hEgKsnEUJ2ZU4XRMQm5nR7lTwHQEezhbkUCVNMG5zOrPi6gz9WcUkfSn8YP6fS4RRQzjqIqJ9WkjoKSsGrM2d8UO+Z+hgiUzSBpNDL8LUn5e5o4Tamaf1tl2iPeltiG33LQlgRQjqqlhPEU0eosQev86+2f2ddceUJk6Ib1pkNenwZmoxV12hD4tFRs9XEsCxoK4+ul6AgNMK+miBprleBXPLbBz5QjmuHNFR3cL5y5Uf+9UIF3K4SKV+/FAWNl7aSBpw3Zdd30AYY8bpMHFMCkvT4I8jBJ98ieDEew+yQzSRrrpAeot8fH9PZ7Mr6egMetBwo/rQwbT2EKapw6X3lGZ5ovVPxECHtB4M+TwHl/5GxjJirfLok6QS3zAlkUCHHZRz/oPsexBahPjDREzVw6ZqpxR/AwKBSEP+uDo/SSBMJIdUPJ3yhl7psUHicz3xosRvRbzVCfExIiQs5UVUV7pLbeCV0OkuyvveY2CQXUUf82Y2npwcICZG/gF6sfxJMwkwro53ITy6 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: From: Junaid Shahid A pseudo-PGD is added to store global non-sensitive ASI mappings. Actual ASI PGDs copy entries from this pseudo-PGD during asi_init(). Memory can be mapped as globally non-sensitive by calling asi_map() with ASI_GLOBAL_NONSENSITIVE. Page tables allocated for global non-sensitive mappings are never freed. While a previous version used init_mm.asi[0] as the special global nonsensitive domain, here we have tried to avoid special-casing index 0. So now we have a special global variable for that. For this to work we need to make sure that nobody assumes that asi is a member of asi->mm->asi (also that nobody assumes a struct asi is embedded in a struct mm - but that seems like a weird assumption to make anyway, when you already have the .mm pointer). I currently believe that this is worth it for the reduced level of magic in the code. Signed-off-by: Junaid Shahid Signed-off-by: Brendan Jackman --- arch/x86/include/asm/asi.h | 3 +++ arch/x86/mm/asi.c | 37 +++++++++++++++++++++++++++++++++++++ arch/x86/mm/init_64.c | 25 ++++++++++++++++--------- arch/x86/mm/mm_internal.h | 3 +++ include/asm-generic/asi.h | 2 ++ 5 files changed, 61 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/asi.h b/arch/x86/include/asm/asi.h index 9aad843eb6df..2d86a5c17f2b 100644 --- a/arch/x86/include/asm/asi.h +++ b/arch/x86/include/asm/asi.h @@ -78,6 +78,9 @@ */ #define ASI_MAX_NUM ((1 << ASI_MAX_NUM_ORDER) - 1) +extern struct asi __asi_global_nonsensitive; +#define ASI_GLOBAL_NONSENSITIVE (&__asi_global_nonsensitive) + struct asi_hooks { /* * Both of these functions MUST be idempotent and re-entrant. They will diff --git a/arch/x86/mm/asi.c b/arch/x86/mm/asi.c index e43b206450ad..807d51497f43 100644 --- a/arch/x86/mm/asi.c +++ b/arch/x86/mm/asi.c @@ -11,6 +11,7 @@ #include #include +#include "mm_internal.h" #include "../../../mm/internal.h" static struct asi_class asi_class[ASI_MAX_NUM]; @@ -19,6 +20,13 @@ static DEFINE_SPINLOCK(asi_class_lock); DEFINE_PER_CPU_ALIGNED(struct asi *, curr_asi); EXPORT_SYMBOL(curr_asi); +static __aligned(PAGE_SIZE) pgd_t asi_global_nonsensitive_pgd[PTRS_PER_PGD]; + +struct asi __asi_global_nonsensitive = { + .pgd = asi_global_nonsensitive_pgd, + .mm = &init_mm, +}; + static inline bool asi_class_registered(int index) { return asi_class[index].name != NULL; @@ -154,6 +162,31 @@ void __init asi_check_boottime_disable(void) pr_info("ASI enablement ignored due to incomplete implementation.\n"); } +static int __init asi_global_init(void) +{ + if (!boot_cpu_has(X86_FEATURE_ASI)) + return 0; + + /* + * Lower-level pagetables for global nonsensitive mappings are shared, + * but the PGD has to be copied into each domain during asi_init. To + * avoid needing to synchronize new mappings into pre-existing domains + * we just pre-allocate all of the relevant level N-1 entries so that + * the global nonsensitive PGD already has pointers that can be copied + * when new domains get asi_init()ed. + */ + preallocate_sub_pgd_pages(asi_global_nonsensitive_pgd, + PAGE_OFFSET, + PAGE_OFFSET + PFN_PHYS(max_pfn) - 1, + "ASI Global Non-sensitive direct map"); + preallocate_sub_pgd_pages(asi_global_nonsensitive_pgd, + VMALLOC_START, VMALLOC_END, + "ASI Global Non-sensitive vmalloc"); + + return 0; +} +subsys_initcall(asi_global_init) + static void __asi_destroy(struct asi *asi) { WARN_ON_ONCE(asi->ref_count <= 0); @@ -168,6 +201,7 @@ int asi_init(struct mm_struct *mm, int asi_index, struct asi **out_asi) { struct asi *asi; int err = 0; + uint i; *out_asi = NULL; @@ -203,6 +237,9 @@ int asi_init(struct mm_struct *mm, int asi_index, struct asi **out_asi) asi->mm = mm; asi->index = asi_index; + for (i = KERNEL_PGD_BOUNDARY; i < PTRS_PER_PGD; i++) + set_pgd(asi->pgd + i, asi_global_nonsensitive_pgd[i]); + exit_unlock: if (err) __asi_destroy(asi); diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index 7e177856ee4f..f67f4637357c 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1278,18 +1278,15 @@ static void __init register_page_bootmem_info(void) #endif } -/* - * Pre-allocates page-table pages for the vmalloc area in the kernel page-table. - * Only the level which needs to be synchronized between all page-tables is - * allocated because the synchronization can be expensive. - */ -static void __init preallocate_vmalloc_pages(void) +/* Initialize empty pagetables at the level below PGD. */ +void __init preallocate_sub_pgd_pages(pgd_t *pgd_table, ulong start, + ulong end, const char *name) { unsigned long addr; const char *lvl; - for (addr = VMALLOC_START; addr <= VMEMORY_END; addr = ALIGN(addr + 1, PGDIR_SIZE)) { - pgd_t *pgd = pgd_offset_k(addr); + for (addr = start; addr <= end; addr = ALIGN(addr + 1, PGDIR_SIZE)) { + pgd_t *pgd = pgd_offset_pgd(pgd_table, addr); p4d_t *p4d; pud_t *pud; @@ -1325,7 +1322,17 @@ static void __init preallocate_vmalloc_pages(void) * The pages have to be there now or they will be missing in * process page-tables later. */ - panic("Failed to pre-allocate %s pages for vmalloc area\n", lvl); + panic("Failed to pre-allocate %s pages for %s area\n", lvl, name); +} + +/* + * Pre-allocates page-table pages for the vmalloc area in the kernel page-table. + * Only the level which needs to be synchronized between all page-tables is + * allocated because the synchronization can be expensive. + */ +static void __init preallocate_vmalloc_pages(void) +{ + preallocate_sub_pgd_pages(init_mm.pgd, VMALLOC_START, VMEMORY_END, "vmalloc"); } void __init mem_init(void) diff --git a/arch/x86/mm/mm_internal.h b/arch/x86/mm/mm_internal.h index 3f37b5c80bb3..1203a977edcd 100644 --- a/arch/x86/mm/mm_internal.h +++ b/arch/x86/mm/mm_internal.h @@ -25,4 +25,7 @@ void update_cache_mode_entry(unsigned entry, enum page_cache_mode cache); extern unsigned long tlb_single_page_flush_ceiling; +extern void preallocate_sub_pgd_pages(pgd_t *pgd_table, ulong start, + ulong end, const char *name); + #endif /* __X86_MM_INTERNAL_H */ diff --git a/include/asm-generic/asi.h b/include/asm-generic/asi.h index 3956f995fe6a..fd5a302e0e09 100644 --- a/include/asm-generic/asi.h +++ b/include/asm-generic/asi.h @@ -9,6 +9,8 @@ #define ASI_MAX_NUM_ORDER 0 #define ASI_MAX_NUM 0 +#define ASI_GLOBAL_NONSENSITIVE NULL + #ifndef _ASSEMBLY_ struct asi_hooks {};