[PATCH v2 1/2] kho: don't release incoming bootmem areas when kho_init() fails

From: Chris Down

Date: Sat Nov 28 2026 - 04:21:57 EST


On a non-KHO boot, kho_reserve_bootmem() reserves the bootmem areas in
memblock, and they stay reserved until kho_init() releases them as CMA.

On a KHO boot, the bootmem areas are inherited from the previous kernel.
kexec loads the new kernel into them. kho_populate() adds them to
memblock as free memory, because early allocations are made from them.
Whatever is still free afterwards goes to the buddy allocator like any
other memory.

Unfortunately however, if kho_init() fails, err_free_bootmem currently
frees every area with free_reserved_area() on both kinds of boot. On a
KHO boot this frees pages that are still in use, the kernel image among
them, and frees buddy pages a second time.

With CONFIG_KEXEC_HANDOVER_DEBUGFS=y, kho_init() fails if debugfs is
disabled, so a KHO kexec into a kernel with debugfs=off added to its
command line is enough to hit this. In a VM, the new kernel then oopses
in kho_init() and panics.

BUG: kernel NULL pointer dereference, address: 0000000000000008
RIP: 0010:free_pcppages_bulk+0x10f/0x280
Call Trace:
free_frozen_page_commit+0x1fa/0x430
__free_frozen_pages+0x205/0x600
__free_reserved_area+0x7b/0x120
free_reserved_area+0x89/0xe0
kho_init+0x301/0x3e0
do_one_initcall+0x89/0x260
kernel_init_freeable+0x1e0/0x250
kernel_init+0x1f/0x140
Kernel panic - not syncing: Attempted to kill init! exitcode=0x00000009

To fix this, let's only free the areas when this isn't a KHO boot,
because that's when kho_reserve_bootmem() reserved them.

With this applied, kho_init() still fails after the same kexec, but the
kernel boots and the bootmem areas are left alone. A non-KHO boot with
debugfs=off still frees the areas it reserved.

Fixes: c609c144b0e8 ("kexec: add KHO parsing support")
Cc: <stable@xxxxxxxxxxxxxxx>
Reviewed-by: Pasha Tatashin <pasha.tatashin@xxxxxxxxxx>
Signed-off-by: Chris Down <chris@xxxxxxxxxxxxxx>
---
kernel/liveupdate/kexec_handover.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c
index 42c6609a1766..663dd22e7172 100644
--- a/kernel/liveupdate/kexec_handover.c
+++ b/kernel/liveupdate/kexec_handover.c
@@ -1899,11 +1899,13 @@ err_free_kho_radix_tree:
kho_radix_destroy_tree(tree);
err_free_bootmem:
kho_out.fdt = NULL;
- for (int i = 0; i < kho_bootmem_cnt; i++) {
- void *start = __va(kho_bootmem[i].addr);
- void *end = start + kho_bootmem[i].size;
+ if (!is_kho_boot()) {
+ for (int i = 0; i < kho_bootmem_cnt; i++) {
+ void *start = __va(kho_bootmem[i].addr);
+ void *end = start + kho_bootmem[i].size;

- free_reserved_area(start, end, -1, "");
+ free_reserved_area(start, end, -1, "");
+ }
}
kho_enable = false;
return err;
--
2.56.1