Most memory the previous kernel preserved stays reserved until its user restores it through KHO, either to use it again or to free it. If nothing does, it stays reserved for as long as the kernel runs. On a hypervisor host that can be gigabytes, but amusingly nothing really shows it reliably. The host simply has less memory than it should, which is not exactly obvious. All preserved memory is reserved in kho_preserved_memory_reserve(), and everything restored through KHO goes through kho_restore_page(), so KHO can count both easily. Let's add preserved_size and restored_size, in bytes, to /sys/kernel/kho/incoming/. For example, after a live update preserving a 64 MiB memfd: preserved_size restored_size before retrieval 67403776 8192 after finish 67403776 67395584 The 8 KiB left are the incoming KHO FDT and LUO's FLB header. Signed-off-by: Chris Down --- Documentation/ABI/testing/sysfs-kernel-kho | 10 ++++++++++ Documentation/admin-guide/mm/kho.rst | 3 +++ kernel/liveupdate/kexec_handover.c | 15 +++++++++++++++ kernel/liveupdate/kexec_handover_internal.h | 2 ++ kernel/liveupdate/kho_sysfs.c | 16 ++++++++++++++++ 5 files changed, 46 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-kernel-kho b/Documentation/ABI/testing/sysfs-kernel-kho index 21488e35d1f3..a8251d663ce6 100644 --- a/Documentation/ABI/testing/sysfs-kernel-kho +++ b/Documentation/ABI/testing/sysfs-kernel-kho @@ -9,3 +9,13 @@ Description: read only the walk of its preserved memory map. "failed" The kernel rejected the handover. Users: system monitoring + +What: /sys/kernel/kho/incoming/preserved_size +What: /sys/kernel/kho/incoming/restored_size +Date: Nov 2026 +Contact: kexec@lists.infradead.org +Description: read only + Bytes preserved by the previous kernel and bytes restored + through KHO, respectively. Both are 0 unless the incoming + state is "revived". +Users: system monitoring diff --git a/Documentation/admin-guide/mm/kho.rst b/Documentation/admin-guide/mm/kho.rst index 8810813a5d0a..a12c1398eff2 100644 --- a/Documentation/admin-guide/mm/kho.rst +++ b/Documentation/admin-guide/mm/kho.rst @@ -87,6 +87,9 @@ sysfs Interfaces ``/sys/kernel/kho/incoming/state`` Status of the incoming KHO handover. +``/sys/kernel/kho/incoming/preserved_size`` and ``restored_size`` + Bytes preserved by the previous kernel and bytes restored through KHO. + See Documentation/ABI/testing/sysfs-kernel-kho for the sysfs ABI. debugfs Interfaces diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c index aff96aa401f8..5d9b724ab754 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -103,6 +103,8 @@ struct kho_in { char previous_release[__NEW_UTS_LEN + 1]; u32 kexec_count; enum kho_incoming_state state; + unsigned long preserved_pages; + atomic_long_t restored_pages; struct kho_debugfs dbg; struct kho_radix_tree radix_tree; }; @@ -545,6 +547,7 @@ static struct page *kho_restore_page(phys_addr_t phys, bool is_folio, kho_init_pages(page, nr_pages); adjust_managed_page_count(page, nr_pages); + atomic_long_add(nr_pages, &kho_in.restored_pages); return page; } @@ -636,6 +639,7 @@ static int __init kho_preserved_memory_reserve(unsigned long key, void *data) info.magic = KHO_PAGE_MAGIC; info.order = order; page->private = info.page_private; + kho_in.preserved_pages += 1UL << order; return 0; } @@ -1746,6 +1750,7 @@ err: kho_in.fdt_phys = 0; kho_in.radix_tree.root = NULL; kho_in.state = KHO_INCOMING_FAILED; + kho_in.preserved_pages = 0; } static __init int kho_out_fdt_setup(void) @@ -1777,6 +1782,16 @@ enum kho_incoming_state kho_in_state(void) return kho_in.state; } +u64 kho_in_preserved_size(void) +{ + return (u64)kho_in.preserved_pages << PAGE_SHIFT; +} + +u64 kho_in_restored_size(void) +{ + return (u64)atomic_long_read(&kho_in.restored_pages) << PAGE_SHIFT; +} + static void __init kho_in_kexec_metadata(void) { struct kho_kexec_metadata *metadata; diff --git a/kernel/liveupdate/kexec_handover_internal.h b/kernel/liveupdate/kexec_handover_internal.h index b559f5d43116..9e2d1b0ef753 100644 --- a/kernel/liveupdate/kexec_handover_internal.h +++ b/kernel/liveupdate/kexec_handover_internal.h @@ -29,6 +29,8 @@ enum kho_incoming_state { }; enum kho_incoming_state kho_in_state(void); +u64 kho_in_preserved_size(void); +u64 kho_in_restored_size(void); #ifdef CONFIG_KEXEC_HANDOVER_DEBUGFS int kho_debugfs_init(void); diff --git a/kernel/liveupdate/kho_sysfs.c b/kernel/liveupdate/kho_sysfs.c index eb2e4ce72f20..e87a417d9254 100644 --- a/kernel/liveupdate/kho_sysfs.c +++ b/kernel/liveupdate/kho_sysfs.c @@ -21,8 +21,24 @@ static ssize_t state_show(struct kobject *kobj, struct kobj_attribute *attr, } static struct kobj_attribute state_attr = __ATTR_RO(state); +static ssize_t preserved_size_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%llu\n", kho_in_preserved_size()); +} +static struct kobj_attribute preserved_size_attr = __ATTR_RO(preserved_size); + +static ssize_t restored_size_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%llu\n", kho_in_restored_size()); +} +static struct kobj_attribute restored_size_attr = __ATTR_RO(restored_size); + static struct attribute *incoming_attrs[] = { &state_attr.attr, + &preserved_size_attr.attr, + &restored_size_attr.attr, NULL }; -- 2.56.1