[RFC PATCH 10/11] x86/kaslr: avoid placing kernel in carvable high memory when extmem= is set

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


Cold-boot extmem= switches early memblock allocations bottom-up, placing
direct-map page tables right above the uncompressed kernel image.
Physical KASLR scans all RAM without checking extmem=, so placing the
kernel above 4 GiB lands the image and page tables inside carvable high
RAM -- breaking 1 GiB contiguity, preventing a clean max carve on
secondary nodes, or causing fixed-size carving to fail with -ENOMEM.

Prefer physical KASLR slots below SZ_4G on cold boot when CONFIG_EXTMEM
is enabled and extmem= is set, falling back to mem_limit if no slot
exists below 4 GiB. KHO bootmem placement and virtual KASLR are
unchanged.

Signed-off-by: Suseendran Bhaskaran <suseendranb at google.com>
---
 arch/x86/boot/compressed/kaslr.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
index 290153818d0f..73c672d6f2ae 100644
--- a/arch/x86/boot/compressed/kaslr.c
+++ b/arch/x86/boot/compressed/kaslr.c
@@ -33,6 +33,7 @@
 #undef _SETUP
 
 #include <asm/kexec_handover.h>
+#include <linux/sizes.h>
 
 extern unsigned long get_cmd_line_ptr(void);
 
@@ -74,6 +75,7 @@ static unsigned long get_boot_seed(void)
 #define MAX_MEMMAP_REGIONS	4
 
 static bool memmap_too_large;
+static bool extmem_cmdline;
 
 
 /*
@@ -271,6 +273,9 @@ static void handle_mem_options(void)
 
 			if (mem_size < mem_limit)
 				mem_limit = mem_size;
+		} else if (IS_ENABLED(CONFIG_EXTMEM) &&
+			   !strcmp(param, "extmem") && val && *val) {
+			extmem_cmdline = true;
 		}
 	}
 
@@ -823,11 +828,28 @@ static unsigned long find_random_phys_addr(unsigned long minimum,
 
 	/*
 	 * During kexec handover only process KHO bootmem areas that are known
-	 * not to contain any data that must be preserved.
+	 * not to contain any data that must be preserved. On cold boot with
+	 * extmem=, prefer slots below 4 GiB so neither the kernel image nor
+	 * bottom-up direct-map page tables land in carvable high RAM, falling
+	 * back to mem_limit if no usable slot exists below 4 GiB.
 	 */
-	if (!process_kho_entries(minimum, image_size) &&
-	    !process_efi_entries(minimum, image_size))
-		process_e820_entries(minimum, image_size);
+	if (!process_kho_entries(minimum, image_size)) {
+		unsigned long orig_gb_huge_pages = max_gb_huge_pages;
+		u64 orig_mem_limit = mem_limit;
+
+		if (extmem_cmdline && (u64)minimum + image_size <= SZ_4G)
+			mem_limit = min_t(u64, mem_limit, SZ_4G);
+
+		if (!process_efi_entries(minimum, image_size))
+			process_e820_entries(minimum, image_size);
+
+		if (!slot_max && mem_limit != orig_mem_limit) {
+			mem_limit = orig_mem_limit;
+			max_gb_huge_pages = orig_gb_huge_pages;
+			if (!process_efi_entries(minimum, image_size))
+				process_e820_entries(minimum, image_size);
+		}
+	}
 
 	phys_addr = slots_fetch_random();
 



More information about the linux-arm-kernel mailing list