mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Chris Down <chris@chrisdown.name>
To: rppt@kernel.org, pasha.tatashin@soleen.com, pratyush@kernel.org
Cc: graf@amazon.com, kexec@lists.infradead.org, linux-mm@kvack.org,
	linux-kernel@vger.kernel.org, kernel-team@meta.com
Subject: [PATCH v2 2/2] kho: drop the handover if preserved memory can't be reserved
Date: Tue, 01 Dec 2026 22:32:11 -0800	[thread overview]
Message-ID: <58bd4ca045b33b71cec8509e893f4ff81e372dc7.1796126791.git.chris@chrisdown.name> (raw)
In-Reply-To: <cover.1796126791.git.chris@chrisdown.name>

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


      parent reply	other threads:[~2026-12-02  6:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=58bd4ca045b33b71cec8509e893f4ff81e372dc7.1796126791.git.chris@chrisdown.name \
    --to=chris@chrisdown.name \
    --cc=graf@amazon.com \
    --cc=kernel-team@meta.com \
    --cc=kexec@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=pasha.tatashin@soleen.com \
    --cc=pratyush@kernel.org \
    --cc=rppt@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®