[RFC PATCH 03/11] x86/mm: add early direct-map unmap helpers for boot carveouts

Suseendran Bhaskaran suseendranb at google.com
Tue Dec 1 16:19:38 PST 2026


init_mem_mapping() maps all memblock.memory ranges (populated from
E820_TYPE_RAM) into the direct map and records them in pfn_mapped[]
before NUMA discovery. Leaving memory carved out after SRAT setup in
pfn_mapped[] and the direct map causes three problems:

 - With CONFIG_KASAN=y, kasan_init() allocates writable shadow RAM (1/8
   of the carved size) from remaining System RAM and conflicts with
   kasan_add_zero_shadow() on ZONE_DEVICE or memmap_on_memory hotplug.
 - arch_add_memory() ignores non-PAGE_KERNEL attributes (such as
   pgprot_decrypted() or pgprot_noncached()) when direct-map entries are
   already present.
 - Unbound ranges stay mapped in the host direct map until a driver binds
   them.

Add arch_boot_carveout_early() to trim carved PFNs from pfn_mapped[]
before kasan_init(), arch_boot_carveout_unmap() to split straddling
1 GiB PUD leaves and unmap PMD-aligned carveouts before SMP bringup, and
an empty arch_boot_carveout_enable() stub for architecture parity.
Runtime arch_add_memory() and arch_remove_memory() remain unchanged.

Signed-off-by: Suseendran Bhaskaran <suseendranb at google.com>
---
 arch/x86/mm/init_64.c          | 85 ++++++++++++++++++++++++++++++++++
 include/linux/memory_hotplug.h |  4 ++
 2 files changed, 89 insertions(+)

diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c
index 2a635b7f78ea..d027438c6bc8 100644
--- a/arch/x86/mm/init_64.c
+++ b/arch/x86/mm/init_64.c
@@ -31,6 +31,8 @@
 #include <linux/memory_hotplug.h>
 #include <linux/memremap.h>
 #include <linux/nmi.h>
+#include <linux/overflow.h>
+#include <linux/range.h>
 #include <linux/gfp.h>
 #include <linux/kcore.h>
 
@@ -1281,6 +1283,89 @@ void __ref arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap)
 	__remove_pages(start_pfn, nr_pages, altmap);
 	kernel_physical_mapping_remove(start, start + size);
 }
+
+static int __init split_direct_map_pud_boundary(unsigned long vaddr)
+{
+	unsigned long base_pfn;
+	unsigned int i, level;
+	pmd_t *pmd_table;
+	pgprot_t prot;
+	pud_t *pud;
+
+	if (IS_ALIGNED(vaddr, PUD_SIZE))
+		return 0;
+
+	pud = (pud_t *)lookup_address(vaddr, &level);
+	if (!pud || level != PG_LEVEL_1G || !pud_present(*pud))
+		return 0;
+
+	base_pfn = pud_pfn(*pud);
+	prot = pud_pgprot(*pud);
+	pmd_table = pmd_alloc_one(&init_mm, vaddr & PUD_MASK);
+	if (!pmd_table)
+		return -ENOMEM;
+
+	for (i = 0; i < PTRS_PER_PMD; i++)
+		set_pmd(&pmd_table[i],
+			pfn_pmd(base_pfn + i * PTRS_PER_PTE, prot));
+
+	spin_lock(&init_mm.page_table_lock);
+	paravirt_alloc_pmd(&init_mm, __pa(pmd_table) >> PAGE_SHIFT);
+	set_pud(pud, __pud(__pa(pmd_table) | _KERNPG_TABLE));
+	spin_unlock(&init_mm.page_table_lock);
+
+	update_page_count(PG_LEVEL_1G, -1);
+	update_page_count(PG_LEVEL_2M, PTRS_PER_PMD);
+	flush_tlb_all();
+
+	return 0;
+}
+
+void __init arch_boot_carveout_enable(void)
+{
+}
+
+void __init arch_boot_carveout_early(phys_addr_t start, phys_addr_t size)
+{
+	unsigned long start_pfn = PFN_DOWN(start);
+	unsigned long end_pfn = PFN_UP(start + size);
+	unsigned long low = 1UL << (32 - PAGE_SHIFT);
+	int i;
+
+	if (start_pfn >= end_pfn)
+		return;
+
+	subtract_range(pfn_mapped, E820_MAX_ENTRIES, start_pfn, end_pfn);
+	nr_pfn_mapped = clean_sort_range(pfn_mapped, E820_MAX_ENTRIES);
+	max_pfn_mapped = nr_pfn_mapped ? pfn_mapped[nr_pfn_mapped - 1].end : 0;
+
+	if (start_pfn < low) {
+		max_low_pfn_mapped = 0;
+		for (i = 0; i < nr_pfn_mapped && pfn_mapped[i].start < low; i++)
+			max_low_pfn_mapped = min_t(u64, pfn_mapped[i].end, low);
+	}
+}
+
+int __init arch_boot_carveout_unmap(phys_addr_t start, phys_addr_t size)
+{
+	phys_addr_t end;
+	int err;
+
+	if (!size || check_add_overflow(start, size, &end) ||
+	    !IS_ALIGNED(start | size, PMD_SIZE))
+		return -EINVAL;
+	if (WARN_ON_ONCE(!after_bootmem || num_online_cpus() > 1))
+		return -EBUSY;
+
+	err = split_direct_map_pud_boundary((unsigned long)__va(start));
+	if (!err)
+		err = split_direct_map_pud_boundary((unsigned long)__va(end));
+	if (err)
+		return err;
+
+	kernel_physical_mapping_remove(start, end);
+	return 0;
+}
 #endif /* CONFIG_MEMORY_HOTPLUG */
 
 static struct kcore_list kcore_vsyscall;
diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h
index ec71debc0673..10527722057a 100644
--- a/include/linux/memory_hotplug.h
+++ b/include/linux/memory_hotplug.h
@@ -140,6 +140,10 @@ extern void arch_remove_memory(u64 start, u64 size, struct vmem_altmap *altmap);
 extern void __remove_pages(unsigned long start_pfn, unsigned long nr_pages,
 			   struct vmem_altmap *altmap);
 
+void arch_boot_carveout_enable(void);
+void arch_boot_carveout_early(phys_addr_t start, phys_addr_t size);
+int arch_boot_carveout_unmap(phys_addr_t start, phys_addr_t size);
+
 /* reasonably generic interface to expand the physical pages */
 extern int __add_pages(int nid, unsigned long start_pfn, unsigned long nr_pages,
 		       struct mhp_params *params);



More information about the linux-arm-kernel mailing list