mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Suseendran Bhaskaran <suseendranb@google.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Mike Rapoport <rppt@kernel.org>,  Dan Williams <djbw@kernel.org>,
	David Hildenbrand <david@kernel.org>
Cc: Alexander Graf <graf@amazon.com>,
	Alison Schofield <alison.schofield@intel.com>,
	 Andy Lutomirski <luto@kernel.org>,
	Borislav Petkov <bp@alien8.de>,
	 Catalin Marinas <catalin.marinas@arm.com>,
	Changyuan Lyu <changyuanl@google.com>,
	 Dave Hansen <dave.hansen@linux.intel.com>,
	Dave Jiang <dave.jiang@intel.com>,
	 David Rientjes <rientjes@google.com>,
	Frank van der Linden <fvdl@google.com>,
	"H. Peter Anvin" <hpa@zytor.com>,  Ingo Molnar <mingo@redhat.com>,
	Ira Weiny <iweiny@kernel.org>, Jonathan Corbet <corbet@lwn.net>,
	 "Liam R. Howlett" <liam@infradead.org>,
	Mark Rutland <mark.rutland@arm.com>,
	 Michal Hocko <mhocko@suse.com>,
	Oscar Salvador <osalvador@suse.de>,
	 Pasha Tatashin <pasha.tatashin@soleen.com>,
	Peter Zijlstra <peterz@infradead.org>,
	 Pratyush Yadav <pratyush@kernel.org>,
	Randy Dunlap <rdunlap@infradead.org>,
	 Shuah Khan <skhan@linuxfoundation.org>,
	Thomas Gleixner <tglx@kernel.org>,
	 Vishal Verma <vishal.l.verma@intel.com>,
	Vlastimil Babka <vbabka@kernel.org>,
	 Will Deacon <will@kernel.org>,
	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 <suseendranb@google.com>
Subject: [RFC PATCH 05/11] mm/extmem: add architecture-neutral external memory subsystem
Date: Wed,  2 Dec 2026 00:19:40 +0000	[thread overview]
Message-ID: <20261202001946.1226273-6-suseendranb@google.com> (raw)
In-Reply-To: <20261202001946.1226273-1-suseendranb@google.com>

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=[<nid>|all:]<size|max>[:<align>][:keep=<size>][,...]

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 <suseendranb@google.com>
---
 .../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: [<nid>|all:]<size|max>[:<align>][:keep=<keep_size>][,...]
+			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).
+			<align> defaults to and must be at least the sparsemem
+			section size (128 MiB on 4K/16K pages, 512 MiB on 64K
+			pages), and <size> must be a multiple of <align>.
+			"max" carves all aligned memory above <keep_size> and
+			4 GiB (or all memory above 4 GiB on an explicit
+			secondary node <nid> > 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 <suseendranb@google.com>
+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 <david@kernel.org>
 R:	Jason Gunthorpe <jgg@nvidia.com>
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 <linux/dma-direct.h>
 #include <linux/dma-map-ops.h>
 #include <linux/efi.h>
+#include <linux/extmem.h>
 #include <linux/swiotlb.h>
 #include <linux/vmalloc.h>
 #include <linux/mm.h>
@@ -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 <linux/pfn.h>
 #include <linux/poison.h>
 #include <linux/dma-mapping.h>
+#include <linux/extmem.h>
 #include <linux/memory.h>
 #include <linux/memory_hotplug.h>
 #include <linux/memremap.h>
@@ -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 <linux/topology.h>
 #include <linux/sort.h>
 #include <linux/numa_memblks.h>
+#include <linux/extmem.h>
 
 #include <asm/e820/api.h>
 #include <asm/proto.h>
@@ -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 <linux/acpi.h>
+#include <linux/extmem.h>
 #include <linux/memblock.h>
 #include <linux/module.h>
 #include <linux/of.h>
@@ -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 <linux/align.h>
+#include <linux/bitops.h>
+#include <linux/build_bug.h>
+#include <linux/ctype.h>
+#include <linux/err.h>
+#include <linux/extmem.h>
+#include <linux/init.h>
+#include <linux/ioport.h>
+#include <linux/kernel.h>
+#include <linux/limits.h>
+#include <linux/log2.h>
+#include <linux/memblock.h>
+#include <linux/memory.h>
+#include <linux/memory_hotplug.h>
+#include <linux/memregion.h>
+#include <linux/mm.h>
+#include <linux/mmzone.h>
+#include <linux/nodemask.h>
+#include <linux/numa.h>
+#include <linux/pgtable.h>
+#include <linux/platform_device.h>
+#include <linux/sizes.h>
+#include <linux/string.h>
+
+#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 [<nid>|all:]<size|max>[:<align>][:keep=<keep_size>]
+ */
+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, &regs[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",
+			       &reg->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 <linux/cma.h>
 #include <linux/crash_dump.h>
 #include <linux/execmem.h>
+#include <linux/extmem.h>
 #include <linux/sizes.h>
 #include <linux/vmstat.h>
 #include <linux/kexec_handover.h>
@@ -2675,4 +2676,5 @@ void __init mm_core_init(void)
 	kmsan_init_runtime();
 	mm_cache_init();
 	execmem_init();
+	extmem_unmap_regions();
 }

  parent reply	other threads:[~2026-12-02  0:20 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  0:19 [RFC PATCH 00/11] mm: NUMA-aware boot memory carving (extmem) Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 01/11] mm/cma: clamp multi-range search to 4G and honor memblock flags Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 02/11] mm/memblock: factor out multi-range contiguous reservation helper from CMA Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 03/11] x86/mm: add early direct-map unmap helpers for boot carveouts Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 04/11] arm64/mm: support unmapping boot carveouts from the linear map Suseendran Bhaskaran
2026-12-02  0:19 ` Suseendran Bhaskaran [this message]
2026-12-02  0:19 ` [RFC PATCH 06/11] dax/hmem: bind extmem platform devices with per-region alignment Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 07/11] nvdimm/ramdax: manage bus and DIMM allocations with devres Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 08/11] nvdimm/ramdax: harden probe checks and select NUMA_KEEP_MEMINFO Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 09/11] nvdimm/ramdax: support binding extmem platform devices Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 10/11] x86/kaslr: avoid placing kernel in carvable high memory when extmem= is set Suseendran Bhaskaran
2026-12-02  0:19 ` [RFC PATCH 11/11] mm/extmem: preserve carved regions across KHO kexec Suseendran Bhaskaran

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202001946.1226273-6-suseendranb@google.com \
    --to=suseendranb@google.com \
    --cc=akpm@linux-foundation.org \
    --cc=alison.schofield@intel.com \
    --cc=bp@alien8.de \
    --cc=catalin.marinas@arm.com \
    --cc=changyuanl@google.com \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=dave.jiang@intel.com \
    --cc=david@kernel.org \
    --cc=djbw@kernel.org \
    --cc=fvdl@google.com \
    --cc=graf@amazon.com \
    --cc=hpa@zytor.com \
    --cc=iweiny@kernel.org \
    --cc=kexec@lists.infradead.org \
    --cc=liam@infradead.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=luto@kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mhocko@suse.com \
    --cc=mingo@redhat.com \
    --cc=nvdimm@lists.linux.dev \
    --cc=osalvador@suse.de \
    --cc=pasha.tatashin@soleen.com \
    --cc=peterz@infradead.org \
    --cc=pratyush@kernel.org \
    --cc=rdunlap@infradead.org \
    --cc=rientjes@google.com \
    --cc=rppt@kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tglx@kernel.org \
    --cc=vbabka@kernel.org \
    --cc=vishal.l.verma@intel.com \
    --cc=will@kernel.org \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®