Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796170816; b=NHnrqbwou/ULR/aCVC/ZxbzZFG57YceO6wOJZdDFZ+QE4YTFBP4GB0qgvm8Eg9mWqvk5 7PENcs2SdLIIXc+IHOle/rots9mBq9VCOyd7w3Y6JD76Ng41s3fxoxz7MTEZnspQhxSHZ 3NJMJUjf0Z//ISW5VTe28i+QeXGHR9G6bxZEOFZXq8rnvtY2mAhVXPr/tWbSgW36AdbA2 U16TyjctDzsGcCqaX4Vtc9+zB4dF9LpjDdBOrKlqDYzmNsFh1o/Gsa2VR+MGtQcbFVfyW 7BwsyrM59xdlMfDOlEOpYft8VCN+LYRbZNBoufZ0uvvI57gJXobhpJ1BYVq+t141MFA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796170816; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=za4k9WouZoBXY56dabbGXCHfp8GbiOOVrJ9DVYP55sg=; b=wLGC8EmbfX5yL07+xeyfg7Oxh1uBR9Tzlg4oi4HEEmF9P7urNDiPGZ/JEEp0XfIcU5o/ vJHYXa+ZrERhSHqhH1dcnW1oeQganK7s4vmrsHe8JBbahXxmSZotXumL1AvWV2BqheL+b lFRejOyX0mOQOhxhpW20GhTyJys/HKKtorUItDg3U9RVevI6eRT5Nzu9yuqihk2nrCmEK fHytk5zZgRAMeH/0LL1nQOjgJfd6h5l5emIB3bBTcPM0F0tP1RP5peifXQKy4XXZfc2ny XdfVufnBZv0U7/lrUuII+cXeExJ+Hu5y/tQ/bfBdHadwWcom1LAjQMbtXwYn+p7gbnw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--suseendranb.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=EExQ8sNU; arc=none smtp.remote-ip=209.85.215.197 Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4CD05606A691 for ; Wed, 2 Dec 2026 00:20:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4CD05606A691 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--suseendranb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=EExQ8sNU Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-cc73895c7d5so6996506a12.1 for ; Tue, 01 Dec 2026 16:20:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796170815; x=1796775615; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=za4k9WouZoBXY56dabbGXCHfp8GbiOOVrJ9DVYP55sg=; b=EExQ8sNUu+57oUBsb/v8Q5is0fKYDwW1S0Bmleiocb+p1ogwiJMqzFMWv0mTLB5J0L 6yIlxmlDFNG3vNpCpt6gBCeUznwxFa6PQn5Z9LU0xPoonN3hz1J+Xf+QQ+jPZK5mUtGQ ToWA81KGEjINxvEBLUb9WA2324SdQkBki8QMaVWSjjFaS/WjxxqOk0OKM3YHab/KQXNZ Y7rXOhtFvfSw12dSXB7ABQELaAPKG0JeOPb4ZamfASNYGobdBaYk1T4P04Co2Ih46HeC 493SFNhFx/bQcKPKtUXzE0BoQ4ASBRg2FTVKzfGbClgMGJgkjuBR9VKUQvY9o/fUcQ0m iWzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796170815; x=1796775615; h=content-type: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:content-type; bh=za4k9WouZoBXY56dabbGXCHfp8GbiOOVrJ9DVYP55sg=; b=KWGZcZddkDL0dxzTjXbpXAohrXbEJflJnpKq73FIf6jh7ttBvtRnnKctT6H4TKtRW/ vv3wYdr0wyweT5Mt3CWSiOg5fDcQvu8tPcWxRndQSDmLyODhwgAM6ao3Y+ICxzrG97hs 1sxTPNGekeFSXyvd+PZNvH16KO98cI1b3CW3/PT3qFHUmfESo4fFjQt3d/bw59NCc+xz YZyd3zK+wrbJIae32EsYBmEr9DySa5Y1/NE9flIs2FgS0vgVEjbEMNeiMYN7HTkuP/yZ NrchtlXURHsx+qXAi6xjbmo6IihBghqTPu7xp+NjljAD8IN+r2sGX571WHmgbQknFipP vO6Q== X-Forwarded-Encrypted: i=1; AL+hErGdu8fRWg7ijXJLICfV6AKM02Cglnuk2RVWlb12/T04Zt7osucj+PYxmruGBM5tKOFCAA2OB/J5jHQ=@vger.kernel.org X-Gm-Message-State: APuU4Z81gYBZRPpEyfyNfjd7a2BL63mG1SyW/OCK6L7IaULIkH37DlPf bwgdp6psdxl8nAtwjEeW1Umvp2PI9LgMMO4dXOpT0ZNJ0OacWk2jRWJycJT9X9kutk9GWnzaZca KbABF1MuF6ZBcN98hMVgwUGKXZA== X-Received: from pglu2.prod.google.com ([2002:a63:1402:0:b0:cc7:5400:3218]) (user=suseendranb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a21:3b8e:b0:3de:4c08:174 with SMTP id adf61e73a8af0-4014d457f6emr43960032637.33.1796170815069; Tue, 01 Dec 2026 16:20:15 -0800 (PST) Date: Wed, 2 Dec 2026 00:19:40 +0000 In-Reply-To: <20261202001946.1226273-1-suseendranb@google.com> X-Mailing-List: linux-cxl@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261202001946.1226273-1-suseendranb@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261202001946.1226273-6-suseendranb@google.com> Subject: [RFC PATCH 05/11] mm/extmem: add architecture-neutral external memory subsystem From: Suseendran Bhaskaran To: Andrew Morton , Mike Rapoport , Dan Williams , David Hildenbrand Cc: Alexander Graf , Alison Schofield , Andy Lutomirski , Borislav Petkov , Catalin Marinas , Changyuan Lyu , Dave Hansen , Dave Jiang , David Rientjes , Frank van der Linden , "H. Peter Anvin" , Ingo Molnar , Ira Weiny , Jonathan Corbet , "Liam R. Howlett" , Mark Rutland , Michal Hocko , Oscar Salvador , Pasha Tatashin , Peter Zijlstra , Pratyush Yadav , Randy Dunlap , Shuah Khan , Thomas Gleixner , Vishal Verma , Vlastimil Babka , Will Deacon , kexec@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-cxl@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, nvdimm@lists.linux.dev, x86@kernel.org, Suseendran Bhaskaran Content-Type: text/plain; charset="UTF-8" Some virtualization and storage hosts keep only a small slice of RAM per NUMA node for the kernel and expose the remaining DRAM as ZONE_DEVICE DAX or PMEM devices. Existing boot options do not fit this model across x86_64 and arm64: - EFI_MEMORY_SP ("Soft Reserved") requires per-machine firmware changes and defaults in dax_hmem to routing memory to dax_kmem for hot-adding back into System RAM. - memmap=nn!ss is x86-only and blind to firmware reserved holes and NUMA node boundaries. - cma=, hugetlb_cma=, and reserve_mem= leave memory in memblock.memory and System RAM, paying ~16 MiB/GiB (on 4 KiB pages) in boot struct page arrays upfront and blocking ZONE_DEVICE and driver-managed hotplug consumers. - Runtime offline_and_remove_memory() cannot remove boot RAM on arm64 and cannot guarantee 1 GiB contiguity after early allocations. Add CONFIG_EXTMEM and the early boot parameter: extmem=[|all:][:][:keep=][,...] to carve section-aligned physical memory ranges above 4 GiB out of memblock after NUMA setup and before NODE_DATA, sparse_init(), or kasan_init() run: - Switch pre-NUMA memblock allocations bottom-up so early page tables stay out of the top of carvable nodes, then restore top-down order. - Include MEMBLOCK_HOTPLUG so movable_node does not hide secondary nodes, reuse memblock_reserve_contig_multi() across firmware holes, and advise memory_block_advise_max_size() for hotplug consumers. - Trim x86_64 pfn_mapped[] before kasan_init() and unmap carved ranges from the direct/linear map before SMP bringup. - Add per-region alignment to struct memregion_info, release carved ranges from System RAM in /proc/iomem (needed on x86_64 where e820__reserve_resources() populates iomem from e820_table rather than memblock.memory), and register "extmem" platform devices with IORESOURCE_MEM resources and region metadata. Signed-off-by: Suseendran Bhaskaran --- .../admin-guide/kernel-parameters.txt | 15 + MAINTAINERS | 7 + arch/arm64/mm/init.c | 3 + arch/x86/mm/init_64.c | 2 + arch/x86/mm/numa.c | 3 + include/linux/extmem.h | 13 + include/linux/memregion.h | 1 + mm/Kconfig | 11 + mm/Makefile | 1 + mm/arch_numa.c | 3 + mm/extmem.c | 419 ++++++++++++++++++ mm/mm_init.c | 2 + 12 files changed, 480 insertions(+) create mode 100644 include/linux/extmem.h create mode 100644 mm/extmem.c diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index dce9b8f3ae98..c20667240080 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -1693,6 +1693,21 @@ Kernel parameters might take longer, especially on systems with a lot of memory. Available with CONFIG_PAGE_EXTENSION=y. + extmem= [KNL,MM,X86-64,ARM64,EARLY] + Format: [|all:][:][:keep=][,...] + Carve NUMA-aware, aligned physical memory regions above + 4 GiB out of memblock after NUMA discovery and before + struct page allocation, unmap them from the kernel + direct/linear map, and expose them as "extmem" platform + devices (up to 8 contiguous ranges per node and 64 + regions total). + defaults to and must be at least the sparsemem + section size (128 MiB on 4K/16K pages, 512 MiB on 64K + pages), and must be a multiple of . + "max" carves all aligned memory above and + 4 GiB (or all memory above 4 GiB on an explicit + secondary node > 0). + failslab= fail_usercopy= fail_page_alloc= diff --git a/MAINTAINERS b/MAINTAINERS index 895c33a99acb..306707fdfd68 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -17562,6 +17562,13 @@ S: Maintained F: include/linux/execmem.h F: mm/execmem.c +MEMORY MANAGEMENT - EXTMEM +M: Suseendran Bhaskaran +L: linux-mm@kvack.org +S: Maintained +F: include/linux/extmem.h +F: mm/extmem.c + MEMORY MANAGEMENT - GUP (GET USER PAGES) M: David Hildenbrand R: Jason Gunthorpe diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 4e7682dfe366..802738e6c9d6 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -314,6 +315,8 @@ void __init bootmem_init(void) min_low_pfn = min; arch_numa_init(); + if (!IS_ENABLED(CONFIG_NUMA)) + extmem_init(); kvm_hyp_reserve(); dma_limits_init(); diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index d027438c6bc8..d9f54c1dea4f 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -824,6 +825,7 @@ kernel_physical_mapping_change(unsigned long paddr_start, static __always_inline void x86_numa_init(void) { memblock_set_node(0, PHYS_ADDR_MAX, &memblock.memory, 0); + extmem_init(); } #endif diff --git a/arch/x86/mm/numa.c b/arch/x86/mm/numa.c index ced66e68a68e..6d2943fd0b2e 100644 --- a/arch/x86/mm/numa.c +++ b/arch/x86/mm/numa.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -130,6 +131,8 @@ static int __init numa_register_nodes(void) if (!memblock_validate_numa_coverage(SZ_1M)) return -EINVAL; + extmem_init(); + /* Finally register nodes. */ for_each_node_mask(nid, node_possible_map) { unsigned long start_pfn, end_pfn; diff --git a/include/linux/extmem.h b/include/linux/extmem.h new file mode 100644 index 000000000000..88fde6f26e64 --- /dev/null +++ b/include/linux/extmem.h @@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _LINUX_EXTMEM_H +#define _LINUX_EXTMEM_H + +#ifdef CONFIG_EXTMEM +void extmem_init(void); +void extmem_unmap_regions(void); +#else +static inline void extmem_init(void) { } +static inline void extmem_unmap_regions(void) { } +#endif + +#endif /* _LINUX_EXTMEM_H */ diff --git a/include/linux/memregion.h b/include/linux/memregion.h index a55f62cc5266..b725948496ab 100644 --- a/include/linux/memregion.h +++ b/include/linux/memregion.h @@ -9,6 +9,7 @@ struct memregion_info { int target_node; struct range range; + unsigned int align; }; #ifdef CONFIG_MEMREGION diff --git a/mm/Kconfig b/mm/Kconfig index 18925ccb4adc..95dfdd192909 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -1537,4 +1537,15 @@ config PTDUMP source "mm/damon/Kconfig" +config EXTMEM + bool "Architecture-neutral external memory carving (extmem)" + depends on MEMORY_HOTREMOVE && (X86_64 || ARM64) + select MEMREGION + select NUMA_KEEP_MEMINFO if NUMA_MEMBLKS + help + Allow carving out NUMA-aware, section- or huge-page-aligned + physical memory regions at boot via the "extmem=" kernel command + line parameter and exposing them as "extmem" platform devices + carrying memory-region metadata and IORESOURCE_MEM resources. + endmenu diff --git a/mm/Makefile b/mm/Makefile index f49994e61b03..0542953db072 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -151,3 +151,4 @@ obj-$(CONFIG_TMPFS_QUOTA) += shmem_quota.o obj-$(CONFIG_LAZY_MMU_MODE_KUNIT_TEST) += tests/lazy_mmu_mode_kunit.o obj-$(CONFIG_VMSTAT_KUNIT_TEST) += tests/vmstat_kunit.o obj-$(CONFIG_MEM_ALLOC_PROFILING) += alloc_tag.o +obj-$(CONFIG_EXTMEM) += extmem.o diff --git a/mm/arch_numa.c b/mm/arch_numa.c index 8125e9cf07b6..be434cf69d44 100644 --- a/mm/arch_numa.c +++ b/mm/arch_numa.c @@ -9,6 +9,7 @@ #define pr_fmt(fmt) "NUMA: " fmt #include +#include #include #include #include @@ -224,6 +225,8 @@ static int __init numa_register_nodes(void) if (!memblock_validate_numa_coverage(0)) return -EINVAL; + extmem_init(); + /* Finally register nodes. */ for_each_node_mask(nid, numa_nodes_parsed) { unsigned long start_pfn, end_pfn; diff --git a/mm/extmem.c b/mm/extmem.c new file mode 100644 index 000000000000..e5be9fcb6bbd --- /dev/null +++ b/mm/extmem.c @@ -0,0 +1,419 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Architecture-neutral external memory (extmem) boot carving. + */ + +#define pr_fmt(fmt) "extmem: " fmt + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mm_init.h" + +#define EXTMEM_MAX_RANGES 8 +#define EXTMEM_MAX_REGIONS 64 +#define EXTMEM_MIN_ALIGN BIT_ULL(SECTION_SIZE_BITS) +#define EXTMEM_ALL_NODES MAX_NUMNODES + +struct extmem_rule { + int nid; + phys_addr_t size; + phys_addr_t align; + phys_addr_t keep; +}; + +struct extmem_region { + phys_addr_t base; + phys_addr_t size; + phys_addr_t align; + int nid; +}; + +static struct extmem_rule extmem_rules[EXTMEM_MAX_REGIONS] __initdata; +static unsigned int extmem_nr_rules __initdata; + +static struct extmem_region extmem_regions[EXTMEM_MAX_REGIONS] __initdata; +static unsigned int extmem_nr_regions __initdata; + +/* + * Parse [|all:][:][:keep=] + */ +static int __init extmem_parse_one(char *s) +{ + struct extmem_rule rule = { + .nid = NUMA_NO_NODE, + .align = EXTMEM_MIN_ALIGN, + }; + char *colon, *endp; + + if (!*s) + return -EINVAL; + if (extmem_nr_rules >= ARRAY_SIZE(extmem_rules)) + return -ENOSPC; + + colon = isdigit(*s) ? strchr(s, ':') : NULL; + if (str_has_prefix(s, "all:")) { + rule.nid = EXTMEM_ALL_NODES; + s += 4; + } else if (colon) { + unsigned int nid; + int ret; + + *colon = '\0'; + ret = kstrtouint(s, 10, &nid); + *colon = ':'; + if (!ret) { + if (nid < MAX_NUMNODES) { + rule.nid = (int)nid; + s = colon + 1; + } else if (nid < EXTMEM_MIN_ALIGN) { + return -EINVAL; + } + } + } + + if (str_has_prefix(s, "max") && (s[3] == ':' || s[3] == '\0')) { + rule.size = PHYS_ADDR_MAX; + s += 3; + } else { + rule.size = memparse(s, &endp); + if (endp == s || !rule.size || rule.size == PHYS_ADDR_MAX) + return -EINVAL; + s = endp; + } + + if (*s == ':' && !str_has_prefix(s + 1, "keep=")) { + s++; + rule.align = memparse(s, &endp); + if (endp == s || !is_power_of_2(rule.align) || + rule.align < EXTMEM_MIN_ALIGN) + return -EINVAL; + s = endp; + } + + if (str_has_prefix(s, ":keep=")) { + s += 6; + rule.keep = memparse(s, &endp); + if (endp == s || !rule.keep) + return -EINVAL; + s = endp; + } + + if (*s != '\0') + return -EINVAL; + if (rule.size == PHYS_ADDR_MAX) { + if (!rule.keep && + (rule.nid <= 0 || rule.nid == EXTMEM_ALL_NODES)) + return -EINVAL; + } else if (!IS_ALIGNED(rule.size, rule.align)) { + return -EINVAL; + } + + extmem_rules[extmem_nr_rules++] = rule; + return 0; +} + +static int __init early_extmem(char *p) +{ + unsigned int orig_nr_rules = extmem_nr_rules; + char *tok; + int err; + + if (!p) + return -EINVAL; + + while ((tok = strsep(&p, ",")) != NULL) { + err = extmem_parse_one(tok); + if (err) { + extmem_nr_rules = orig_nr_rules; + pr_err("invalid extmem option '%s' (%pe)\n", + tok, ERR_PTR(err)); + return err; + } + } + + /* + * Place pre-SRAT memblock allocations bottom-up (like movable_node) + * so early page tables stay in low RAM rather than fragmenting the + * top of a NUMA node before extmem_init() runs. + */ + if (extmem_nr_rules) { + memblock_set_bottom_up(true); + arch_boot_carveout_enable(); + } + + return 0; +} +early_param("extmem", early_extmem); + +static int __init extmem_find_keep_end(int nid, phys_addr_t keep, + phys_addr_t *keep_end) +{ + phys_addr_t start, end, rem = keep; + u64 i; + + __for_each_mem_range(i, &memblock.memory, NULL, nid, MEMBLOCK_HOTPLUG, + &start, &end, NULL) { + if (rem <= end - start) { + *keep_end = start + rem; + return 0; + } + rem -= end - start; + } + + return -ENOSPC; +} + +static int __init extmem_commit_regions(unsigned int nr, const char *verb) +{ + struct extmem_region *regs = &extmem_regions[extmem_nr_regions]; + struct memblock_region *r; + unsigned int i; + u64 hotplug = 0; + + BUILD_BUG_ON(EXTMEM_MAX_REGIONS > BITS_PER_LONG_LONG); + + for (i = 0; i < nr; i++) { + if (!numa_valid_node(regs[i].nid) || + !node_possible(regs[i].nid)) + regs[i].nid = max(phys_to_target_node(regs[i].base), 0); + + for_each_mem_region(r) { + if (memblock_addrs_overlap(regs[i].base, regs[i].size, + r->base, r->size) && + memblock_is_hotpluggable(r)) { + hotplug |= BIT_ULL(i); + break; + } + } + + if (memblock_remove(regs[i].base, regs[i].size)) { + while (i--) + memblock_add_node(regs[i].base, regs[i].size, + regs[i].nid, + (hotplug & BIT_ULL(i)) ? + MEMBLOCK_HOTPLUG : + MEMBLOCK_NONE); + return -ENOMEM; + } + } + + for (i = 0; i < nr; i++) { + phys_addr_t end = regs[i].base + regs[i].size - 1; + + arch_boot_carveout_early(regs[i].base, regs[i].size); + memory_block_advise_max_size(BIT_ULL(__ffs64(regs[i].base | + regs[i].size))); + pr_info("%s [%pa-%pa] (%llu MiB) on node %d\n", + verb, ®s[i].base, &end, + (u64)(regs[i].size / SZ_1M), regs[i].nid); + } + extmem_nr_regions += nr; + + return 0; +} + +static void __init extmem_carve_node(int nid, const struct extmem_rule *rule) +{ + unsigned int flags = MEMBLOCK_CONTIG_TOP_DOWN | + MEMBLOCK_CONTIG_INCLUDE_HOTPLUG; + struct memblock_contig_range cands[EXTMEM_MAX_RANGES]; + phys_addr_t high_min = SZ_4G, keep_end; + int max_ranges, nr, k; + char scope[16]; + + if (extmem_nr_regions >= EXTMEM_MAX_REGIONS) + return; + if (rule->size == PHYS_ADDR_MAX) + flags |= MEMBLOCK_CONTIG_ALLOW_PARTIAL; + max_ranges = min_t(unsigned int, EXTMEM_MAX_RANGES, + EXTMEM_MAX_REGIONS - extmem_nr_regions); + + if (nid == NUMA_NO_NODE) + strscpy(scope, "system", sizeof(scope)); + else + snprintf(scope, sizeof(scope), "node %d", nid); + + if (rule->keep) { + if (extmem_find_keep_end(nid, rule->keep, &keep_end)) { + pr_info("%s has less than %llu MiB, skipping\n", + scope, (u64)(rule->keep / SZ_1M)); + return; + } + high_min = max(high_min, keep_end); + } + + nr = memblock_reserve_contig_multi(rule->size, rule->align, rule->align, + high_min, nid, flags, cands, + max_ranges); + if (nr < 0) { + pr_err("failed to carve on %s above %pa (%pe)\n", + scope, &high_min, ERR_PTR(nr)); + return; + } + if (!nr) { + pr_info("%s has no carvable memory above %pa\n", + scope, &high_min); + return; + } + + for (k = 0; k < nr; k++) { + memblock_phys_free(cands[k].base, cands[k].size); + extmem_regions[extmem_nr_regions + k] = (struct extmem_region) { + .base = cands[k].base, + .size = cands[k].size, + .align = rule->align, + .nid = cands[k].nid, + }; + } + + if (extmem_commit_regions(nr, "carved")) + pr_err("failed to remove carved range on %s\n", scope); +} + +void __init extmem_init(void) +{ + unsigned long orig_end_pfn, new_end_pfn; + unsigned int i, j; + int nid; + + if (!extmem_nr_rules) + return; + + orig_end_pfn = PFN_DOWN(memblock_end_of_DRAM()); + memblock_set_bottom_up(false); + + for (i = 0; i < extmem_nr_rules; i++) { + struct extmem_rule *rule = &extmem_rules[i]; + + if (rule->nid != EXTMEM_ALL_NODES) { + if (rule->nid == NUMA_NO_NODE || + node_possible(rule->nid)) + extmem_carve_node(rule->nid, rule); + continue; + } + + for_each_node_mask(nid, node_possible_map) { + unsigned long start_pfn, end_pfn; + + get_pfn_range_for_nid(nid, &start_pfn, &end_pfn); + if (start_pfn >= end_pfn) + continue; + + for (j = 0; j < extmem_nr_rules; j++) + if (extmem_rules[j].nid == nid) + break; + if (j == extmem_nr_rules) + extmem_carve_node(nid, rule); + } + } + + if (!extmem_nr_regions) + return; + new_end_pfn = PFN_DOWN(memblock_end_of_DRAM()); + if (new_end_pfn < orig_end_pfn) { + max_pfn = new_end_pfn; + max_low_pfn = new_end_pfn; + } +} + +void __init extmem_unmap_regions(void) +{ + unsigned int i, dst = 0; + + for (i = 0; i < extmem_nr_regions; i++) { + struct extmem_region *reg = &extmem_regions[i]; + phys_addr_t end = reg->base + reg->size - 1; + int err; + + err = arch_boot_carveout_unmap(reg->base, reg->size); + if (err) { + pr_err("failed to unmap [%pa-%pa]: %pe\n", + ®->base, &end, ERR_PTR(err)); + continue; + } + + extmem_regions[dst++] = *reg; + } + extmem_nr_regions = dst; +} + +static unsigned int __init extmem_region_align(phys_addr_t align) +{ + if (!has_transparent_hugepage()) + return PAGE_SIZE; + if (IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD) && + align >= PUD_SIZE && (u64)PUD_SIZE <= UINT_MAX) + return (unsigned int)PUD_SIZE; + return PMD_SIZE; +} + +static int __init extmem_publish_regions(void) +{ + unsigned int i; + + for (i = 0; i < extmem_nr_regions; i++) { + struct extmem_region *reg = &extmem_regions[i]; + struct resource res = DEFINE_RES_MEM_NAMED(reg->base, reg->size, + "External Memory"); + struct memregion_info mri = { + .target_node = reg->nid, + .range = { .start = res.start, .end = res.end }, + .align = extmem_region_align(reg->align), + }; + struct platform_device *pdev; + int id, err; + + release_mem_region_adjustable(reg->base, reg->size); + + id = memregion_alloc(GFP_KERNEL); + if (id < 0) { + pr_err("failed to allocate memregion ID for %pr: %pe\n", + &res, ERR_PTR(id)); + continue; + } + + pdev = platform_device_alloc("extmem", id); + if (!pdev) { + memregion_free(id); + continue; + } + + set_dev_node(&pdev->dev, numa_map_to_online_node(reg->nid)); + err = platform_device_add_resources(pdev, &res, 1); + if (!err) + err = platform_device_add_data(pdev, &mri, sizeof(mri)); + if (!err) + err = platform_device_add(pdev); + if (err) { + pr_err("failed to register extmem.%d for %pr: %pe\n", + id, &res, ERR_PTR(err)); + platform_device_put(pdev); + memregion_free(id); + } + } + + return 0; +} +core_initcall(extmem_publish_regions); diff --git a/mm/mm_init.c b/mm/mm_init.c index e66d3850e128..a0cc18d9d7c5 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -2675,4 +2676,5 @@ void __init mm_core_init(void) kmsan_init_runtime(); mm_cache_init(); execmem_init(); + extmem_unmap_regions(); }