From patchwork Fri Jan 10 18:40:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Jackman X-Patchwork-Id: 13935241 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 0CDD3E77188 for ; Fri, 10 Jan 2025 18:41:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 11A346B00CC; Fri, 10 Jan 2025 13:41:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 024AB6B00CD; Fri, 10 Jan 2025 13:41:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D6EEE6B00CE; Fri, 10 Jan 2025 13:41:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B6FA66B00CC for ; Fri, 10 Jan 2025 13:41:16 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 60482120DDE for ; Fri, 10 Jan 2025 18:41:16 +0000 (UTC) X-FDA: 82992409752.12.2589D9B Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf03.hostedemail.com (Postfix) with ESMTP id 63D702000C for ; Fri, 10 Jan 2025 18:41:14 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Q+Pjg+r2; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3yGmBZwgKCOMOFHPRFSGLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--jackmanb.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3yGmBZwgKCOMOFHPRFSGLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--jackmanb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736534474; a=rsa-sha256; cv=none; b=DFLW4rQqTnoVL+DeGz3jU4tbHemLTYQWdfIGcOzX7ojV5UN6DGmmQyfI4yeIpU73WFNTSm Cw5gqKRw1YYrEUG70o2NmK3/VyaSNTx1ROA6owZKs5RKWJCi5nXKEvMOlevgfSUfQMaljA 7zWUzEBmg2MjmkjqpXbFKoUp9qjyoTg= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Q+Pjg+r2; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf03.hostedemail.com: domain of 3yGmBZwgKCOMOFHPRFSGLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--jackmanb.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3yGmBZwgKCOMOFHPRFSGLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--jackmanb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1736534474; 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=UzXM9zi9X6couWShNCb4EkjJb05AI+Oo7qUi6Mvjsww=; b=nWCDUDo9Rz7oy6hG76oPdkKQbbHUoLtWQ+jG6WzZYmiCmuVN+/YzocFeK/lxY8DccC/hrW T5tGO63wx+s1oFfbqOKoFqfoeaHD4dcspRc6YxnBM/IuyNv0xgFuREXE5iAZSdC83Z1xkE RO0U1GqaXwlyPAE/HHz4cTM/Tpjonjg= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43624b08181so12084445e9.0 for ; Fri, 10 Jan 2025 10:41:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736534473; x=1737139273; 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=UzXM9zi9X6couWShNCb4EkjJb05AI+Oo7qUi6Mvjsww=; b=Q+Pjg+r2zEtZVY21124bQ4M8fkfs3ACL47HgYdBynh2vc2QaNjEOLVlCISRvwPD2j+ oDIRDA+Es3WhYXQdV5Qvw+Niuz/fELVfpRpzoOZZCHavhM6C/9inap6lorvWr3X8KQZp ucix+8YU7Mo/3lbV3OCu5Ub1WHWEl2UcosaUBsJX+ScbPdy8PgxDVAit+Bl0xp6beYul nmX/Cm+e+czaG6onB300yabKYLVZxAxSjEhitttR4yJMcU7IbmNsXpVLvT2HZiofBQUw ftQMGyCQaZDyHNx2sqq6RDlXScsMrGHb1ijvgVtvg+C5Ez0D8UEm7m66sHWd8G7Nu1JD hTAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736534473; x=1737139273; 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=UzXM9zi9X6couWShNCb4EkjJb05AI+Oo7qUi6Mvjsww=; b=b6fczufyKqd0uWfZY4K4a2L6nfmB78g0CN1qfEEcCSrCK1IAiiCNxY7kYNC3OUl8XX 7V0n6CU2O1nuTv4SQQbb4902i0ohnQgYSpjDMSKItgRm1/tW3nf7c1T7Ugh9RDZjsahw 3Nantxz/Sje2nAwzfgp1Bcab4g485aKqWVNQVFjdLczmUDtZQUxhlmDug5XZ1lvc/j/R wIZGW6BYU8waiOWl+EvqRODPIwNoWMmZjAFCvjbMBSO/9emtZ4nkA+CtillXoD+6irXL UV5qeYlc7MKQv7u6IfsoN8sR6+fRxrRsngxiV/lC1ZFYFTAOO0o0ayKD9FxImnjJOAL+ NJYw== X-Forwarded-Encrypted: i=1; AJvYcCXesWQ4o2dG4JLicr67Dz0xcFQYB7t9YkU0wnjWt9/nTgAnT1aIirZmKwIKdbhMw791Nok2v2oESA==@kvack.org X-Gm-Message-State: AOJu0YwM6eBtUDqPpsIUygZ7H3Os4auJpjXrOd4WNbwedOrKbvDQURTB Gb+jzk87//iJjGGsR5EnNHcpCO1CdHm08wKB4q8bvsUxyEEFAYqVlLWlsk+puy6bWDM9HVBKd/S xPtXweFd+Cw== X-Google-Smtp-Source: AGHT+IFDRv4zNENk63KYcLxYmQdIUcLs7zTtS/Sd8xMbR3tJqcykEbsngD0F8tGcq40UK8q2XIPTmnyPYnp+hA== X-Received: from wmqe5.prod.google.com ([2002:a05:600c:4e45:b0:435:21e:7bec]) (user=jackmanb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d2a:b0:435:edb0:5d27 with SMTP id 5b1f17b1804b1-436e8827fbcmr75837805e9.9.1736534472863; Fri, 10 Jan 2025 10:41:12 -0800 (PST) Date: Fri, 10 Jan 2025 18:40:38 +0000 In-Reply-To: <20250110-asi-rfc-v2-v2-0-8419288bc805@google.com> Mime-Version: 1.0 References: <20250110-asi-rfc-v2-v2-0-8419288bc805@google.com> X-Mailer: b4 0.15-dev Message-ID: <20250110-asi-rfc-v2-v2-12-8419288bc805@google.com> Subject: [PATCH RFC v2 12/29] 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 , Richard Henderson , Matt Turner , Vineet Gupta , Russell King , Catalin Marinas , Will Deacon , Guo Ren , Brian Cain , Huacai Chen , WANG Xuerui , Geert Uytterhoeven , Michal Simek , Thomas Bogendoerfer , Dinh Nguyen , Jonas Bonn , Stefan Kristiansson , Stafford Horne , "James E.J. Bottomley" , Helge Deller , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Naveen N Rao , Madhavan Srinivasan , Paul Walmsley , Palmer Dabbelt , Albert Ou , Heiko Carstens , Vasily Gorbik , Alexander Gordeev , Christian Borntraeger , Sven Schnelle , Yoshinori Sato , Rich Felker , John Paul Adrian Glaubitz , "David S. Miller" , Andreas Larsson , Richard Weinberger , Anton Ivanov , Johannes Berg , Chris Zankel , Max Filippov , Arnd Bergmann , Andrew Morton , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Uladzislau Rezki , Christoph Hellwig , Masami Hiramatsu , Mathieu Desnoyers , Mike Rapoport , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Dennis Zhou , Tejun Heo , Christoph Lameter , Sean Christopherson , Paolo Bonzini , Ard Biesheuvel , Josh Poimboeuf , Pawan Gupta Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-alpha@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, loongarch@lists.linux.dev, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-openrisc@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, linux-um@lists.infradead.org, linux-arch@vger.kernel.org, linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, Brendan Jackman , Junaid Shahid X-Stat-Signature: 9yamd54c96i8imok4cy3yenh3o7g1np9 X-Rspam-User: X-Rspamd-Queue-Id: 63D702000C X-Rspamd-Server: rspam08 X-HE-Tag: 1736534474-498965 X-HE-Meta: U2FsdGVkX1/uhKExAJ8HSOxb8k9Ct9co+sD28UK79l7MB+obqy6PrMyWPxNUo7YUlD9al6bLoUlkc51WnNxFDhoCNEm3OGrVASqGqssFjBbcmNH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0NqaS/guGGTKIjPavg465ysR8X9RA0I+Azu2sEFcWqmbflAfK8uA1ucA4puLCGJmPy1HNa51ghJfTx+cEUqa1BK9Tdlb08Niqe2EZWm5sCdPKj5mGg7BF2uCCw/iW9sUnF+03n7LV7G2I/48ppf74mNvVDpupj2HbgMLFDoS5zMdPnKPp7XHnOkOjG47eKjkV+uTCBn7d/P7/C44Y5lvgNPunEsk7QEYY6o/axlKRvaLP73plhq7TF+ofCWK30U/67uyaIz3qF/DLudZQq5fDbRMUQABxBJYW9oAf5C4YpaWjk/ggJoogvm+lqTrtw6qCqoCJ4pi/8AV941yuisdT5haaogaaV2NCF4YAO/Myjy+6/YjZblsB8A== 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. These page tables are shared between all domains and init_mm, so they don't need special synchronization. RFC note: A refactoring/prep commit should be split out of this patch. 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 33f18be0e268b3a6725196619cbb8d847c21e197..555edb5f292e4d6baba782f51d014aa48dc850b6 100644 --- a/arch/x86/include/asm/asi.h +++ b/arch/x86/include/asm/asi.h @@ -120,6 +120,9 @@ struct asi_taint_policy { asi_taints_t set; }; +extern struct asi __asi_global_nonsensitive; +#define ASI_GLOBAL_NONSENSITIVE (&__asi_global_nonsensitive) + /* * An ASI domain (struct asi) represents a restricted address space. The * unrestricted address space (and user address space under PTI) are not diff --git a/arch/x86/mm/asi.c b/arch/x86/mm/asi.c index f2d8fbc0366c289891903e1c2ac6c59b9476d95f..17391ec8b22e3c0903cd5ca29cbb03fcc4cbacce 100644 --- a/arch/x86/mm/asi.c +++ b/arch/x86/mm/asi.c @@ -13,6 +13,7 @@ #include #include +#include "mm_internal.h" #include "../../../mm/internal.h" static struct asi_taint_policy *taint_policies[ASI_MAX_NUM_CLASSES]; @@ -26,6 +27,13 @@ const char *asi_class_names[] = { 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_id_valid(enum asi_class_id class_id) { return class_id >= 0 && class_id < ASI_MAX_NUM_CLASSES; @@ -156,6 +164,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); @@ -170,6 +203,7 @@ int asi_init(struct mm_struct *mm, enum asi_class_id class_id, struct asi **out_ { struct asi *asi; int err = 0; + uint i; *out_asi = NULL; @@ -203,6 +237,9 @@ int asi_init(struct mm_struct *mm, enum asi_class_id class_id, struct asi **out_ asi->mm = mm; asi->class_id = class_id; + 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 ff253648706fa9cd49169a54882014a72ad540cf..9d358a05c4e18ac6d5e115de111758ea6cdd37f2 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -1288,18 +1288,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; @@ -1335,7 +1332,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 3f37b5c80bb32ff34656a20789449da92e853eb6..1203a977edcd523589ad88a37aab01398a10a129 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 5be8f7d657ba0bc2196e333f62b084d0c9eef7b6..7867b8c23449058a1dd06308ab5351e0d210a489 100644 --- a/include/asm-generic/asi.h +++ b/include/asm-generic/asi.h @@ -23,6 +23,8 @@ typedef u8 asi_taints_t; #ifndef CONFIG_MITIGATION_ADDRESS_SPACE_ISOLATION +#define ASI_GLOBAL_NONSENSITIVE NULL + struct asi_hooks {}; struct asi {};