[PATCH 5/5] riscv: mm: Reserve the DTB before scanning reserved memory

Han Gao via B4 Relay devnull+gaohan.iscas.ac.cn at kernel.org
Fri Oct 23 00:33:18 PDT 2026


From: Han Gao <gaohan at iscas.ac.cn>

setup_bootmem() reserves the DTB only after
early_init_fdt_scan_reserved_mem() has allocated the dynamically placed
reserved-memory regions. A CMA pool described with size and alloc-ranges
can therefore be placed over the DTB, which is later handed to the page
allocator and overwritten.

This happens after kexec_file has put the DTB of the next kernel in CMA:
the next kernel reuses that range for its own pool, and a further
kexec_file_load() fails with -FDT_ERR_BADMAGIC.

Reserve the DTB first.

Fixes: ef69d2559fe9 ("riscv: Move early dtb mapping into the fixmap region")
Signed-off-by: Han Gao <gaohan at iscas.ac.cn>
---
 arch/riscv/mm/init.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c
index fb37b0b67efe..1ca908bcc4c0 100644
--- a/arch/riscv/mm/init.c
+++ b/arch/riscv/mm/init.c
@@ -290,21 +290,23 @@ static void __init setup_bootmem(void)
 
 	reserve_initrd_mem();
 
-	/*
-	 * No allocation should be done before reserving the memory as defined
-	 * in the device tree, otherwise the allocation could end up in a
-	 * reserved region.
-	 */
-	early_init_fdt_scan_reserved_mem();
-
 	/*
 	 * If DTB is built in, no need to reserve its memblock.
 	 * Otherwise, do reserve it but avoid using
 	 * early_init_fdt_reserve_self() since __pa() does
-	 * not work for DTB pointers that are fixmap addresses
+	 * not work for DTB pointers that are fixmap addresses.
+	 * Reserve it before dynamically placed reserved-memory
+	 * regions, such as a CMA pool, are allocated.
 	 */
 	if (!IS_ENABLED(CONFIG_BUILTIN_DTB))
 		memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
+
+	/*
+	 * No allocation should be done before reserving the memory as defined
+	 * in the device tree, otherwise the allocation could end up in a
+	 * reserved region.
+	 */
+	early_init_fdt_scan_reserved_mem();
 }
 
 #ifdef CONFIG_RELOCATABLE

-- 
2.55.0





More information about the kexec mailing list