Skip to contentSkip to conversation

kho: handle failures to reserve preserved memory

PATCH New Chris Down 2 participants 5 messages Dec 6

Subsystems (4)
lore
Share & tools
lore

Chris Down

author
#0 lore raw

v3 cover kho: handle failures to reserve preserved memory

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 3 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 patches 1 and 2 make KHO keep treating the bootmem areas as inherited after a failed handover.

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.

This is based on the next branch of the liveupdate tree, as it needs the CMA accounting from liveupdate-7.5 as well as 0e88c65680e2 ("kho:
don't release incoming bootmem areas when kho_init() fails") from mainline, and only next has both.

v3:

- Check kho_in.bootmem_phys for the CMA accounting and in kho_init()'s error path as well, in a new patch 1
- Keep kho_in.bootmem_phys and kho_bootmem set when kho_memory_init_early() fails, and don't reserve new bootmem areas in that case (Pratyush)
- Retitle what was patch 1 and is now patch 2, as it's no longer only about kho_init()
- Reword the comments in kho_init() and reserve_mem_kho_revive() (Pratyush)
- Say in patch 3 what it takes for the reservation to fail (Mike)
- Show in patch 3 that the kernel still boots after a failed reservation, with a way to trigger one

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)

v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]

Chris Down (3):
kho: don't use is_kho_boot() to check for inherited bootmem
kho: leave inherited bootmem alone if the handover fails
kho: drop the handover if preserved memory can't be reserved

 kernel/liveupdate/kho_core.c | 61 ++++++++++++++++++++++--------------
 mm/memblock.c                | 11 +++++++
 2 files changed, 49 insertions(+), 23 deletions(-)

base-commit: 13278f869c76f01ab1a6f6c694f718d9f487bd68

Signature · 1 line
2.56.1
author
#1 lore raw

PATCH v3 1/3 kho: don't use is_kho_boot() to check for inherited bootmem

On a KHO boot, kho_init() adds the inherited bootmem areas to the CMA counters, as nothing else counts them. If kho_init() fails, its error path frees the bootmem areas that kho_reserve_bootmem() reserved, but leaves inherited ones alone, as the kernel is running out of them.

Both check is_kho_boot() to tell the two cases apart. That doesn't work if the handover fails. is_kho_boot() is then false, but the bootmem areas are still the inherited ones. kho_init() would leave them out of the CMA counters, and its error path would free them with free_reserved_area() while they are in use.

Nothing makes that happen at the moment, but the patch after next does. Test kho_in.bootmem_phys in both places instead. For now the two tests always give the same answer, so this doesn't change behaviour on its own.

That later 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")
---
 kernel/liveupdate/kho_core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/liveupdate/kho_core.c +2 -2
index 15fb892a4412..342099d4dea2 100644
@@ -1901,7 +1901,7 @@ static __init int kho_init(void)
 	const void *fdt = kho_get_fdt();
 	int err = 0;
 
-	if (is_kho_boot())
+	if (kho_in.bootmem_phys)
 		kho_account_incoming_bootmem();
 
 	if (!kho_enable)
@@ -1965,7 +1965,7 @@ err_free_kho_radix_tree:
 	kho_radix_destroy_tree(tree);
 err_free_bootmem:
 	kho_out.fdt = NULL;
-	if (!is_kho_boot()) {
+	if (!kho_in.bootmem_phys) {
 		for (int i = 0; i < kho_bootmem_cnt; i++) {
 			void *start = __va(kho_bootmem[i].addr);
 			void *end = start + kho_bootmem[i].size;
Signature · 1 line
2.56.1
author
#2 lore raw

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

In reply to Chris Down

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")
---
 kernel/liveupdate/kho_core.c | 39 +++++++++++++++++++-----------------
 1 file changed, 21 insertions(+), 18 deletions(-)
kernel/liveupdate/kho_core.c +21 -18
index 342099d4dea2..959413599073 100644
@@ -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();
 }
 
Signature · 1 line
2.56.1
author
#3 lore raw

PATCH v3 3/3 kho: drop the handover if preserved memory can't be reserved

In reply to Chris Down

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. At this point in boot, memblock only allocates from the bootmem areas and from blocks with no preserved memory in them, so for that to happen the bootmem has to be close to full and the preserved memory badly fragmented and spread over most of memory. It's unlikely, but possible. When it happens, the boot 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.

You might wonder, doesn't this not matter because the boot would fail anyway? But, in fact, it can go on, because the memory map is already allocated and nothing else needs memory from memblock between the walk and memblock_free_all(). For example, if one preserves a memfd with 140000 pages interleaved with another file and then uses reserve_mem= to fill most of the next kernel's bootmem, one gets this message for each range that the kernel can't reserve:

memblock: Failed to double reserved array from 131072 to 262144 entries !

That kernel then boots to userspace as usual, where retrieving the memfd fails with -EIO:

    WARNING: kernel/liveupdate/kho_core.c:531 at kho_restore_page+0x157/0x170
    memfd_luo: Unable to restore folio at physical address: 7fe62000

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.

Dropping the handover is only safe with the previous two patches applied. Without them, kho_init() would release the inherited bootmem areas as CMA while the kernel is still running out of them, or free them in its error path.

With this applied, the same boot logs the failure once and comes up without the handover:

memblock: Failed to double reserved array from 131072 to 262144 entries !
KHO: failed to reserve preserved memory: -ENOMEM, disabling KHO revival

Fixes fc33e4b44b27 ("kexec: enable KHO support for memory preservation")
---
 kernel/liveupdate/kho_core.c | 18 +++++++++++++++---
 mm/memblock.c                | 11 +++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)
kernel/liveupdate/kho_core.c +15 -3
index 959413599073..055e0fa3e604 100644
@@ -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.
mm/memblock.c +11 -0
index a450092a80fb..3e356299167c 100644
@@ -2746,6 +2746,17 @@ static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size,
 		return false;
 	}
 
+	/*
+	 * KHO reserves the preserved memory, but that happens later. Handover
+	 * failures can leave it unreserved.
+	 */
+	err = memblock_reserve(*p_start, size);
+	if (err) {
+		pr_warn("KHO reserve-mem '%s' could not be reserved: %pe\n",
+			name, ERR_PTR(err));
+		return false;
+	}
+
 	reserved_mem_add(*p_start, size, name);
 	pr_info("Revived memory reservation '%s' from KHO\n", name);
 
Signature · 1 line
2.56.1