All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/2] kho: handle failures to reserve preserved memory
@ 2026-12-02  6:32 Chris Down
  2026-12-02  6:32 ` [PATCH v2 1/2] kho: skip bootmem setup in kho_init() if bootmem was inherited Chris Down
  2026-12-02  6:32 ` [PATCH v2 2/2] kho: drop the handover if preserved memory can't be reserved Chris Down
  0 siblings, 2 replies; 5+ messages in thread
From: Chris Down @ 2026-12-02  6:32 UTC (permalink / raw)
  To: rppt, pasha.tatashin, pratyush
  Cc: graf, kexec, linux-mm, linux-kernel, kernel-team

When a kernel boots with a handover, kho_preserved_memory_reserve()
reserves each range of preserved memory in memblock, but it ignores
failures. If one happens, memory that a KHO user still expects to
restore can be released to the page allocator. This needs memblock to
fail to grow its reserved array, so it's unlikely. But if it does
happen, the result is bad enough to be worth handling.

Patch 2 makes the walk stop on such a failure, so that
kho_mem_retrieve() drops the handover and the boot carries on without
it. For that to be safe, reserve_mem_kho_revive() now reserves its own
region, and patch 1 stops kho_init() from releasing inherited bootmem
areas after a failed retrieval.

From a quick check I did with one of the reservations forced to fail,
the kernel logs the failure and boots without the handover. The
reserve_mem region stays reserved, and a later KHO kexec out of that
kernel works.

v2:

- Drop the handover and carry on instead of panicking, with
  reserve_mem_kho_revive() reserving its own region (Mike)
- Stop kho_init() from releasing inherited bootmem areas after a failed
  retrieval, as a separate patch (Mike)

v1: https://lore.kernel.org/r/avQUquBK6cO6tUUI@chrisdown.name

Chris Down (2):
  kho: skip bootmem setup in kho_init() if bootmem was inherited
  kho: drop the handover if preserved memory can't be reserved

 kernel/liveupdate/kho_core.c | 28 +++++++++++++++++++++++-----
 mm/memblock.c                |  8 ++++++++
 2 files changed, 31 insertions(+), 5 deletions(-)


base-commit: ec459d99ed222228b3d601818fd1e24822a450d6
-- 
2.56.1


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] kho: skip bootmem setup in kho_init() if bootmem was inherited
  2026-12-02  6:32 [PATCH v2 0/2] kho: handle failures to reserve preserved memory Chris Down
@ 2026-12-02  6:32 ` Chris Down
  2026-12-02 10:59   ` Pratyush Yadav
  2026-12-02  6:32 ` [PATCH v2 2/2] kho: drop the handover if preserved memory can't be reserved Chris Down
  1 sibling, 1 reply; 5+ messages in thread
From: Chris Down @ 2026-12-02  6:32 UTC (permalink / raw)
  To: rppt, pasha.tatashin, pratyush
  Cc: graf, kexec, linux-mm, linux-kernel, kernel-team

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 what
kho_memory_init() uses to choose between retrieving the handover and
reserving new bootmem areas.

Signed-off-by: Chris Down <chris@chrisdown.name>
---
 kernel/liveupdate/kho_core.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/kernel/liveupdate/kho_core.c b/kernel/liveupdate/kho_core.c
index 15fb892a4412..43ecef317cfd 100644
--- a/kernel/liveupdate/kho_core.c
+++ b/kernel/liveupdate/kho_core.c
@@ -1928,10 +1928,16 @@ static __init int kho_init(void)
 	if (err)
 		goto err_free_fdt;
 
-	if (fdt) {
+	if (fdt)
 		kho_in_debugfs_init(&kho_in.dbg, fdt);
+
+	/*
+	 * We've booted from the inherited bootmem. That means this kernel is
+	 * already using it. As such, leave it alone even if the handover is
+	 * dropped later.
+	 */
+	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);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] kho: drop the handover if preserved memory can't be reserved
  2026-12-02  6:32 [PATCH v2 0/2] kho: handle failures to reserve preserved memory Chris Down
  2026-12-02  6:32 ` [PATCH v2 1/2] kho: skip bootmem setup in kho_init() if bootmem was inherited Chris Down
@ 2026-12-02  6:32 ` Chris Down
  2026-12-02 11:14   ` Pratyush Yadav
  1 sibling, 1 reply; 5+ messages in thread
From: Chris Down @ 2026-12-02  6:32 UTC (permalink / raw)
  To: rppt, pasha.tatashin, pratyush
  Cc: graf, kexec, linux-mm, linux-kernel, kernel-team

kho_preserved_memory_reserve() reserves each range of preserved memory
in memblock and marks it noinit, but ignores both return values. Either
can fail when memblock has to grow its reserved array and can't find
room for the new one, which is unlikely but possible. The boot then
goes on as if the range were reserved, and two things can follow:

- If the reservation failed, memblock_free_all() hands the range to the
  page allocator with the previous kernel's data still in it, while its
  KHO user still expects to restore it. A memfd preserved through LUO
  can then get back pages that belong to someone else. Oh dear!
- If only the noinit marking failed, the struct pages can be
  initialised later, wiping the KHO magic, so restoring the range fails.

Let's return the error instead. The walk then stops, and
kho_mem_retrieve() logs the failure and drops the handover. The boot
carries on without it. Ranges that the walk had already reserved stay
reserved, since freeing them would need the same array growth that
just failed. The rest of the preserved memory is released like any
other free memory.

reserve_mem= revives its regions from the handover before
mm_core_init(), and relies on the walk to reserve them. They have to
stay reserved when the handover is dropped, so make
reserve_mem_kho_revive() reserve its region itself.

Fixes: fc33e4b44b27 ("kexec: enable KHO support for memory preservation")
Signed-off-by: Chris Down <chris@chrisdown.name>
---
 kernel/liveupdate/kho_core.c | 18 +++++++++++++++---
 mm/memblock.c                |  8 ++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/kernel/liveupdate/kho_core.c b/kernel/liveupdate/kho_core.c
index 43ecef317cfd..a0429e85132b 100644
--- a/kernel/liveupdate/kho_core.c
+++ b/kernel/liveupdate/kho_core.c
@@ -623,6 +623,7 @@ static int __init kho_preserved_memory_reserve(unsigned long key, void *data)
 	unsigned int order;
 	phys_addr_t phys;
 	u64 sz;
+	int err;
 
 	phys = kho_decode_radix_key(key, &order);
 
@@ -630,8 +631,14 @@ static int __init kho_preserved_memory_reserve(unsigned long key, void *data)
 	page = kho_get_preserved_page(phys, order);
 
 	/* Reserve the memory preserved in KHO in memblock */
-	memblock_reserve(phys, sz);
-	memblock_reserved_mark_noinit(phys, sz);
+	err = memblock_reserve(phys, sz);
+	if (err)
+		return err;
+
+	err = memblock_reserved_mark_noinit(phys, sz);
+	if (err)
+		return err;
+
 	info.magic = KHO_PAGE_MAGIC;
 	info.order = order;
 	page->private = info.page_private;
@@ -1731,13 +1738,18 @@ static void __init kho_mem_retrieve(void)
 	const struct kho_radix_walk_cb cb = {
 		.leaf = kho_preserved_memory_reserve,
 	};
+	int err;
 
-	if (kho_radix_walk_tree(&kho_in.radix_tree, &cb, NULL))
+	err = kho_radix_walk_tree(&kho_in.radix_tree, &cb, NULL);
+	if (err)
 		goto err;
 
 	return;
 
 err:
+	pr_warn("failed to reserve preserved memory: %pe, disabling KHO revival\n",
+		ERR_PTR(err));
+
 	/*
 	 * Failed to initialize preserved memory. Clear FDT and radix so KHO
 	 * users don't treat it as a KHO boot.
diff --git a/mm/memblock.c b/mm/memblock.c
index a450092a80fb..7b29b657a348 100644
--- a/mm/memblock.c
+++ b/mm/memblock.c
@@ -2746,6 +2746,14 @@ static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
 		return false;
 	}
 
+	/* KHO reserves preserved memory, but not if it drops the handover. */
+	err = memblock_reserve(*p_start, size);
+	if (err) {
+		pr_warn("KHO reserve-mem '%s' could not be reserved: %d\n",
+			name, err);
+		return false;
+	}
+
 	reserved_mem_add(*p_start, size, name);
 	pr_info("Revived memory reservation '%s' from KHO\n", name);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] kho: skip bootmem setup in kho_init() if bootmem was inherited
  2026-12-02  6:32 ` [PATCH v2 1/2] kho: skip bootmem setup in kho_init() if bootmem was inherited Chris Down
@ 2026-12-02 10:59   ` Pratyush Yadav
  0 siblings, 0 replies; 5+ messages in thread
From: Pratyush Yadav @ 2026-12-02 10:59 UTC (permalink / raw)
  To: Chris Down
  Cc: rppt, pasha.tatashin, pratyush, graf, kexec, linux-mm,
	linux-kernel, kernel-team

On Tue, Dec 01 2026, Chris Down wrote:

> 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 what
> kho_memory_init() uses to choose between retrieving the handover and
> reserving new bootmem areas.
>
> Signed-off-by: Chris Down <chris@chrisdown.name>
> ---
>  kernel/liveupdate/kho_core.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/kernel/liveupdate/kho_core.c b/kernel/liveupdate/kho_core.c
> index 15fb892a4412..43ecef317cfd 100644
> --- a/kernel/liveupdate/kho_core.c
> +++ b/kernel/liveupdate/kho_core.c
> @@ -1928,10 +1928,16 @@ static __init int kho_init(void)
>  	if (err)
>  		goto err_free_fdt;
>  
> -	if (fdt) {
> +	if (fdt)
>  		kho_in_debugfs_init(&kho_in.dbg, fdt);
> +
> +	/*
> +	 * We've booted from the inherited bootmem. That means this kernel is
> +	 * already using it. As such, leave it alone even if the handover is
> +	 * dropped later.

Nit: I don't understand what "even if handover is dropped later" means.
Doesn't the handover get dropped _before_ kho_init()?

Also, perhaps "even if the handover fails" is better?

> +	 */
> +	if (kho_in.bootmem_phys)

kho_memory_init_early() clears kho_in.bootmem_phys on failure. So this
doesn't work. It clears it because kho_memory_init() uses to check if it
should even try to retrieve preserved memory.

Perhaps it should only clear fdt_phys and kho_memory_init() should check
fdt_phys instead?

>  		return 0;
> -	}
>  
>  	for (int i = 0; i < kho_bootmem_cnt; i++) {
>  		unsigned long base_pfn = PHYS_PFN(kho_bootmem[i].addr);

-- 
Regards,
Pratyush Yadav


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 2/2] kho: drop the handover if preserved memory can't be reserved
  2026-12-02  6:32 ` [PATCH v2 2/2] kho: drop the handover if preserved memory can't be reserved Chris Down
@ 2026-12-02 11:14   ` Pratyush Yadav
  0 siblings, 0 replies; 5+ messages in thread
From: Pratyush Yadav @ 2026-12-02 11:14 UTC (permalink / raw)
  To: Chris Down
  Cc: rppt, pasha.tatashin, pratyush, graf, kexec, linux-mm,
	linux-kernel, kernel-team

On Tue, Dec 01 2026, Chris Down wrote:

> kho_preserved_memory_reserve() reserves each range of preserved memory
> in memblock and marks it noinit, but ignores both return values. Either
> can fail when memblock has to grow its reserved array and can't find
> room for the new one, which is unlikely but possible. The boot then
> goes on as if the range were reserved, and two things can follow:
>
> - If the reservation failed, memblock_free_all() hands the range to the
>   page allocator with the previous kernel's data still in it, while its
>   KHO user still expects to restore it. A memfd preserved through LUO
>   can then get back pages that belong to someone else. Oh dear!
> - If only the noinit marking failed, the struct pages can be
>   initialised later, wiping the KHO magic, so restoring the range fails.
>
> Let's return the error instead. The walk then stops, and
> kho_mem_retrieve() logs the failure and drops the handover. The boot
> carries on without it. Ranges that the walk had already reserved stay
> reserved, since freeing them would need the same array growth that
> just failed. The rest of the preserved memory is released like any
> other free memory.
>
> reserve_mem= revives its regions from the handover before
> mm_core_init(), and relies on the walk to reserve them. They have to
> stay reserved when the handover is dropped, so make
> reserve_mem_kho_revive() reserve its region itself.
>
> Fixes: fc33e4b44b27 ("kexec: enable KHO support for memory preservation")
> Signed-off-by: Chris Down <chris@chrisdown.name>
> ---
>  kernel/liveupdate/kho_core.c | 18 +++++++++++++++---
>  mm/memblock.c                |  8 ++++++++
>  2 files changed, 23 insertions(+), 3 deletions(-)
>
[...]
> diff --git a/mm/memblock.c b/mm/memblock.c
> index a450092a80fb..7b29b657a348 100644
> --- a/mm/memblock.c
> +++ b/mm/memblock.c
> @@ -2746,6 +2746,14 @@ static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
>  		return false;
>  	}
>  
> +	/* KHO reserves preserved memory, but not if it drops the handover. */

Nit: I find this comment hard to read. 

Perhaps:

    KHO reserves the preserved memory, but that happens later. Handover
    failures can leave it unreserved.

Or something to the tune of this?

Other than this,

Reviewed-by: Pratyush Yadav <pratyush@kernel.org>

> +	err = memblock_reserve(*p_start, size);
> +	if (err) {
> +		pr_warn("KHO reserve-mem '%s' could not be reserved: %d\n",
> +			name, err);
> +		return false;
> +	}
> +
>  	reserved_mem_add(*p_start, size, name);
>  	pr_info("Revived memory reservation '%s' from KHO\n", name);

-- 
Regards,
Pratyush Yadav


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-02 11:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02  6:32 [PATCH v2 0/2] kho: handle failures to reserve preserved memory Chris Down
2026-12-02  6:32 ` [PATCH v2 1/2] kho: skip bootmem setup in kho_init() if bootmem was inherited Chris Down
2026-12-02 10:59   ` Pratyush Yadav
2026-12-02  6:32 ` [PATCH v2 2/2] kho: drop the handover if preserved memory can't be reserved Chris Down
2026-12-02 11:14   ` Pratyush Yadav

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.