kho: handle failures to reserve preserved memory
Subsystems (4)
v2 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 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/[email protected]
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
Signature · 1 line
2.56.1
PATCH v2 1/2 kho: skip bootmem setup in kho_init() if bootmem was inherited
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.
--- kernel/liveupdate/kho_core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)
kernel/liveupdate/kho_core.c +8 -2
@@ -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);
Signature · 1 line
2.56.1
Pratyush Yadav
maintainerRe: [PATCH v2 1/2] kho: skip bootmem setup in kho_init() if bootmem was inherited
Quoted 27 lines · Chris Down
On a cold boot,
kho_reserve_bootmem()reserves the bootmem areas in memblock, andkho_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 ifkho_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 runinit_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 whatkho_memory_init()uses to choose between retrieving the handover and reserving new bootmem areas.Signed-off-by Chris Down <[email protected]>--- kernel/liveupdate/kho_core.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-)kernel/liveupdate/kho_core.c +0
-0@@ -1928,10 +1928,16 @@ static __init int kho_init(void)if (err)
kernel/liveupdate/kho_core.c static __init int kho_init(void) · line 1937
Quoted 10 lines
kernel/liveupdate/kho_core.c +6
-1goto 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?
kernel/liveupdate/kho_core.c static __init int kho_init(void) · line 1939
Quoted 2 lines
kernel/liveupdate/kho_core.c +2
-0+*/ +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?
Quoted 5 lines
kernel/liveupdate/kho_core.c +0
-1return 0 ; - }for (int i =0 ; i < kho_bootmem_cnt; i++) {unsigned long base_pfn =PHYS_PFN (kho_bootmem[i].addr);
Signature · 2 lines
Regards, Pratyush Yadav
PATCH v2 2/2 kho: drop the handover if preserved memory can't be reserved
In reply to Chris Downkho_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.
--- kernel/liveupdate/kho_core.c | 18 +++++++++++++++--- mm/memblock.c | 8 ++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)
kernel/liveupdate/kho_core.c +15 -3
@@ -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 +8 -0
@@ -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);
Signature · 1 line
2.56.1
Re: [PATCH v2 2/2] kho: drop the handover if preserved memory can't be reserved
Quoted 32 lines · 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, 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 makereserve_mem_kho_revive()reserve its region itself.Fixes fc33e4b44b27 ("kexec: enable KHO support for memory preservation")Signed-off-by Chris Down <[email protected]>--- kernel/liveupdate/kho_core.c | 18 +++++++++++++++--- mm/memblock.c | 8 ++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)
[...]
mm/memblock.c static bool __init reserve_mem_kho_revive(const char *name, phys_addr_t size, · line 2749
Quoted 9 lines
mm/memblock.c +1
-0@@ -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,
Quoted 9 lines
mm/memblock.c +7
-0+ 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);
Signature · 2 lines
Regards, Pratyush Yadav
Mike Rapoport
maintainerQuoted 6 lines · Chris Down
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.
If memblock_reserve() fails we are in a very unhappy place. Do you think we really should worry about KHO state when we won't pass through mm_core_init() anyway?
Quoted 33 lines
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 stopskho_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_memregion 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)
- Stopkho_init()from releasing inherited bootmem areas after a failed retrieval, as a separate patch (Mike)v1: https://lore.kernel.org/r/[email protected]
Chris Down (2):
kho: skip bootmem setup inkho_init()if bootmem was inherited
kho: drop the handover if preserved memory can't be reservedkernel/liveupdate/kho_core.c | 28 +++++++++++++++++++++++----- mm/memblock.c | 8 ++++++++ 2 files changed, 31 insertions(+), 5 deletions(-)base-commit:
ec459d99ed222228b3d601818fd1e24822a450d6Signature · 1 line
2.56.1
Signature · 2 lines
Sincerely yours, Mike.