[PATCH v3 2/3] kho: leave inherited bootmem alone if the handover fails

From: Chris Down

Date: Sat Dec 05 2026 - 23:15:20 EST


On a cold boot, kho_reserve_bootmem() reserves the bootmem areas in
memblock, and kho_init() later releases them to the page allocator as
CMA. A kernel that was handed its bootmem areas by the previous kernel
boots out of them instead. kho_init() must leave them alone.

kho_init() tells the two cases apart by whether there is a handover
FDT. That doesn't work if kho_mem_retrieve() fails. It clears
kho_in.fdt_phys so that KHO users don't treat the boot as a KHO boot,
but the bootmem areas are still the inherited ones. kho_init() would
then run init_cma_reserved_pageblock() over memory that is in use.

Nothing makes kho_mem_retrieve() fail at the moment, but the next
patch does. Test kho_in.bootmem_phys instead, which is only set if the
bootmem areas were inherited.

It doesn't always stay set, though. kho_memory_init_early() has an
error path that drops the handover as well, and it clears
kho_in.bootmem_phys too, since kho_memory_init() uses that to choose
between retrieving the handover and reserving new bootmem areas.
Nothing should reach that path either, but let's leave
kho_in.bootmem_phys set there, and set kho_bootmem before anything in
kho_memory_init_early() can fail, as kho_bootmem_overlap() still reads
it afterwards. kho_memory_init() now retrieves the handover if
is_kho_boot() says there is one, and otherwise only reserves new
bootmem areas if none were inherited, as the kernel is still
allocating out of the inherited ones.

The next patch is only safe with this one applied, so this has the
same Fixes: tag to make sure that a backport doesn't go out without it.

Fixes: fc33e4b44b27 ("kexec: enable KHO support for memory preservation")
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
kernel/liveupdate/kho_core.c | 39 +++++++++++++++++++-----------------
1 file changed, 21 insertions(+), 18 deletions(-)

diff --git a/kernel/liveupdate/kho_core.c b/kernel/liveupdate/kho_core.c
index 342099d4dea2..959413599073 100644
--- a/kernel/liveupdate/kho_core.c
+++ b/kernel/liveupdate/kho_core.c
@@ -1928,10 +1928,15 @@ static __init int kho_init(void)
if (err)
goto err_free_fdt;

- if (fdt) {
+ if (fdt)
kho_in_debugfs_init(&kho_in.dbg, fdt);
+
+ /*
+ * If we booted from the inherited bootmem, this kernel is already
+ * using it, so leave it alone even if the handover failed.
+ */
+ if (kho_in.bootmem_phys)
return 0;
- }

for (int i = 0; i < kho_bootmem_cnt; i++) {
unsigned long base_pfn = PHYS_PFN(kho_bootmem[i].addr);
@@ -1986,21 +1991,12 @@ void __init kho_memory_init_early(void)
if (!is_kho_boot())
return;

- /*
- * kho_get_mem_map() should always succeed. If it fails, kho_populate()
- * catches that and never sets kho_in.bootmem_phys, which stops memory
- * retrieval.
- */
+ kho_bootmem = phys_to_virt(kho_in.bootmem_phys);
+
mem_map = kho_get_mem_map(fdt);
if (WARN_ON(!mem_map))
goto err;

- /*
- * kho_bootmem_overlap() needs kho_bootmem to be initialized. It is used
- * by free_area_init() on KHO boots, so initialize it early.
- */
- kho_bootmem = phys_to_virt(kho_in.bootmem_phys);
-
if (kho_radix_init_tree(&kho_in.radix_tree, mem_map))
goto err;

@@ -2010,11 +2006,11 @@ void __init kho_memory_init_early(void)

err:
/*
- * Failed to initialize preserved memory radix tree. Clear FDT
- * and bootmem so KHO users don't treat it as a KHO boot.
+ * Failed to set up preserved memory. Clear FDT so KHO users don't treat
+ * it as a KHO boot. kho_in.bootmem_phys stays set, as this kernel is
+ * still running out of the inherited bootmem.
*/
kho_in.fdt_phys = 0;
- kho_in.bootmem_phys = 0;
}

void __init kho_memory_init(void)
@@ -2025,9 +2021,16 @@ void __init kho_memory_init(void)
return;
}

- if (kho_in.bootmem_phys)
+ if (is_kho_boot()) {
kho_mem_retrieve();
- else
+ return;
+ }
+
+ /*
+ * If the handover failed, this kernel is still running out of the
+ * inherited bootmem. Only reserve new areas if there is none.
+ */
+ if (!kho_in.bootmem_phys)
kho_reserve_bootmem();
}

--
2.56.1