Make KHO Stateless
Subsystems (6)
v4 cover Make KHO Stateless
This series transitions KHO from an xarray-based metadata tracking system with serialization to a radix tree data structure that can be passed directly to the next kernel.
The key motivations for this change are to:
- Eliminate the need for data serialization before kexec.
- Remove the KHO finalize state.
- Pass preservation metadata more directly to the next kernel via the FDT.
The new approach uses a radix tree to mark preserved pages. A page's physical address and its order are encoded into a single value. The tree is composed of multiple levels of page-sized tables, with leaf nodes being bitmaps where each set bit represents a preserved page. The physical address of the radix tree's root is passed in the FDT, allowing the next kernel to reconstruct the preserved memory map.
This series is broken down into the following patches:
1. kho: Adopt radix tree for preserved memory tracking:
Replaces the xarray-based tracker with the new radix tree implementation and increments the ABI version.
2. kho: Remove finalize state and clients:
Removes the now-obsolete kho_finalize() function and its usage from client code and debugfs.
---
Changelog since v3 [1]:
- The patches introducing the KHO FDT ABI header [2] and relocating the vmalloc preservation structure to the KHO ABI header [3] were merged into another series [4].
- Use struct kho_radix_tree to encapsulate the tree structure and expose it to the public radix tree APIs as an argument.
- Public radix tree APIs now manage the tree structure's lock internally.
- Protect the radix tree with a mutex lock instead of a read-write semaphore.
- Radix tree root pointer validation and warnings are now centralized in kho_radix_add_page() and kho_radix_del_page().
- Updates to the KHO finalization logic are grouped into the second patch.
- kho_radix_encode_key() and kho_radix_decode_key() were removed from the public APIs.
- KHO radix tree callback function takes physical address and order of a page as inputs instead of a radix key.
- Refactored kho_radix_get_index() to use kho_radix_get_bitmap_index().
- Updated the documentation.
[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
---
Jason Miu (2):
kho: Adopt radix tree for preserved memory tracking
kho: Remove finalize state and clients
Documentation/admin-guide/mm/kho.rst | 52 +- Documentation/core-api/kho/abi.rst | 6 + Documentation/core-api/kho/index.rst | 23 +- include/linux/kho/abi/kexec_handover.h | 141 +++- include/linux/kho_radix_tree.h | 72 ++ kernel/liveupdate/kexec_handover.c | 703 ++++++++++---------- kernel/liveupdate/kexec_handover_debugfs.c | 23 - kernel/liveupdate/kexec_handover_internal.h | 3 - kernel/liveupdate/luo_core.c | 12 +- kernel/liveupdate/luo_flb.c | 2 +- tools/testing/selftests/kho/init.c | 20 - 11 files changed, 573 insertions(+), 484 deletions(-) create mode 100644 include/linux/kho_radix_tree.h
base-commit: f96074c6d01d8a5e9e2fccd0bba5f2ed654c1f2d
Signature · 1 line
2.52.0.457.g6b5491de43-goog
PATCH v4 1/2 kho: Adopt radix tree for preserved memory tracking
Introduce a radix tree implementation for tracking preserved memory pages and switch the KHO memory tracking mechanism to use it. This lays the groundwork for a stateless KHO implementation that eliminates the need for serialization and the associated "finalize" state.
This patch introduces the core radix tree data structures and constants to the KHO ABI. It adds the radix tree node and leaf structures, along with documentation for the radix tree key encoding scheme that combines a page's physical address and order.
To support broader use by other kernel subsystems, such as hugetlb preservation, the core radix tree manipulation functions are exported as a public API.
The xarray-based memory tracking is replaced with this new radix tree implementation. The core KHO preservation and unpreservation functions are wired up to use the radix tree helpers. On boot, the second kernel restores the preserved memory map by walking the radix tree whose root physical address is passed via the FDT.
The ABI compatible version is bumped to "kho-v2" to reflect the structural changes in the preserved memory map and sub-FDT property names. This includes renaming "fdt" to "preserved-data" to better reflect that preserved state may use formats other than FDT.
--- Documentation/core-api/kho/abi.rst | 6 + Documentation/core-api/kho/index.rst | 16 + include/linux/kho/abi/kexec_handover.h | 141 ++++- include/linux/kho_radix_tree.h | 72 +++ kernel/liveupdate/kexec_handover.c | 722 +++++++++++++------------ 5 files changed, 583 insertions(+), 374 deletions(-) create mode 100644 include/linux/kho_radix_tree.h
Documentation/core-api/kho/abi.rst +6 -0
@@ -22,6 +22,12 @@ memblock preservation ABI .. kernel-doc:: include/linux/kho/abi/memblock.h :doc: memblock kexec handover ABI +KHO persistent memory tracker ABI +================================= + +.. kernel-doc:: include/linux/kho/abi/kexec_handover.h + :doc: KHO persistent memory tracker + See Also ========
Documentation/core-api/kho/index.rst +16 -0
@@ -4,6 +4,8 @@ Kexec Handover Subsystem ======================== +.. _kho-concepts: + Overview ========
@@ -72,6 +74,8 @@ the next KHO, because kexec can overwrite even the original kernel. KHO finalization phase ====================== +.. _kho-finalization-phase: + To enable user space based kexec file loader, the kernel needs to be able to provide the FDT that describes the current kernel's state before performing the actual kexec. The process of generating that FDT is
@@ -79,6 +83,18 @@ called serialization. When the FDT is generated, some properties of the system may become immutable because they are already written down in the FDT. That state is called the KHO finalization phase. +Kexec Handover Radix Tree +========================= + +.. kernel-doc:: include/linux/kho_radix_tree.h + :doc: Kexec Handover Radix Tree + +Public API +========== + +.. kernel-doc:: kernel/liveupdate/kexec_handover.c + :export: + See Also ========
include/linux/kho/abi/kexec_handover.h +126 -15
188 lines not on this page · raw message
include/linux/kho_radix_tree.h new file +72 -0
72 lines not on this page · raw message
kernel/liveupdate/kexec_handover.c +363 -359
942 lines not on this page · raw message
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Mike Rapoport
maintainerRe: [PATCH v4 1/2] kho: Adopt radix tree for preserved memory tracking
Quoted 34 lines · Jason Miu
Introduce a radix tree implementation for tracking preserved memory pages and switch the KHO memory tracking mechanism to use it. This lays the groundwork for a stateless KHO implementation that eliminates the need for serialization and the associated "finalize" state.
This patch introduces the core radix tree data structures and constants to the KHO ABI. It adds the radix tree node and leaf structures, along with documentation for the radix tree key encoding scheme that combines a page's physical address and order.
To support broader use by other kernel subsystems, such as hugetlb preservation, the core radix tree manipulation functions are exported as a public API.
The xarray-based memory tracking is replaced with this new radix tree implementation. The core KHO preservation and unpreservation functions are wired up to use the radix tree helpers. On boot, the second kernel restores the preserved memory map by walking the radix tree whose root physical address is passed via the FDT.
The ABI
compatibleversion is bumped to "kho-v2" to reflect the structural changes in the preserved memory map and sub-FDT property names. This includes renaming "fdt" to "preserved-data" to better reflect that preserved state may use formats other than FDT.Signed-off-by Jason Miu <[email protected]>--- Documentation/core-api/kho/abi.rst | 6 + Documentation/core-api/kho/index.rst | 16 + include/linux/kho/abi/kexec_handover.h | 141 ++++- include/linux/kho_radix_tree.h | 72 +++ kernel/liveupdate/kexec_handover.c | 722 +++++++++++++------------ 5 files changed, 583 insertions(+), 374 deletions(-) create mode 100644 include/linux/kho_radix_tree.h
...
Quoted 40 lines
+/**
+ * DOC: KHO persistent memory tracker
+ *
+ * KHO tracks preserved memory using a radix tree data structure. Each node of
+ * the tree is exactly a single page. The leaf nodes are bitmaps where each set
+ * bit is a preserved page of any order. The intermediate nodes are tables of
+ * physical addresses that point to a lower level node.
+ *
+ * The tree hierarchy is shown below::
+ *+ * root + * +-------------------+ + * | Level 5 | (struct kho_radix_node) + * +-------------------+ + * | + * v + * +-------------------+ + * | Level 4 | (struct kho_radix_node) + * +-------------------+ + * | + * | ... (intermediate levels) + * | + * v + * +-------------------+ + * | Level 0 | (struct kho_radix_leaf) + * +-------------------++ *
+ * The tree is traversed using a key that encodes the page's physical address
+ * (pa) and its order into a single unsigned long value. The encoded key value
+ * is composed of two parts: the 'order bit' in the upper part and the 'page
+ * offset' in the lower part.::
+ *+ * +------------+-----------------------------+--------------------------+ + * | Page Order | Order Bit | Page Offset | + * +------------+-----------------------------+--------------------------+ + * | 0 | ...000100 ... (at bit 52) | pa >> (PAGE_SHIFT + 0) | + * | 1 | ...000010 ... (at bit 51) | pa >> (PAGE_SHIFT + 1) | + * | 2 | ...000001 ... (at bit 50) | pa >> (PAGE_SHIFT + 2) | + * | ... | ... | ... | + * +------------+-----------------------------+--------------------------+
Quoted 2 lines
+ *
+ * Page Offset:
To me "page offset" reads as offset from somewhere and here it's rather pfn on steroids :)
Also in many places in the kernel "page offset" refers to the offset inside a page.
Can't say I can think of a better name, but it feels that it should express that this is an address more explicitly.
Quoted 16 lines
+ * The 'page offset' is the physical address normalized for its order. It
+ * effectively represents the page offset for the given order.
+ *
+ * Order Bit:
+ * The 'order bit' encodes the page order by setting a single bit at a
+ * specific position. The position of this bit itself represents the order.
+ *
+ * For instance, on a 64-bit system with 4KB pages (PAGE_SHIFT= 12), the
+ * maximum range for a page offset (for order 0) is 52 bits (64 - 12). This
+ * offset occupies bits [0-51]. For order 0, the order bit is set at
+ * position 52.
+ *
+ * The following diagram illustrates how the encoded key value is split into
+ * indices for the tree levels, withPAGE_SIZEof 4KB::
+ *
+ * 63:60 59:51 50:42 41:33 32:24 23:15 14:0
Quoted 5 lines
+ * +---------+--------+--------+--------+--------+--------+-----------------+ + * | 0 | Lv 5 | Lv 4 | Lv 3 | Lv 2 | Lv 1 | Lv 0 (bitmap) | + * +---------+--------+--------+--------+--------+--------+-----------------++ *
+ * The radix tree stores pages of all sizes (orders) in a single 6-level
"sizes" can be misleading here because "all sizes" can mean non power-of-2 sizes as well. I'd just use "pages of all orders".
Quoted 1 line
+ * hierarchy. It efficiently shares lower table levels, especially due to
Don't we share the higher levels? Also using "tree" instead of "table" seems clearer to me.
Quoted 25 lines
+ * common zero top address bits, allowing a single, efficient algorithm to
+ * manage all pages. This bitmap approach also offers memory efficiency; for
+ * example, a 512KB bitmap can cover a 16GB memory range for 0-order pages with
+ *PAGE_SIZE= 4KB.
+ *
+ * The data structures defined here are part of the KHO ABI. Any modification
+ * to these structures that breaks backward compatibility must be accompanied by
+ * an update to the "compatible" string. This ensures that a newer kernel can
+ * correctly interpret the data passed by an older kernel.
+ */include/linux/kho/abi/kexec_handover.h +15
-0+ +/* +* Defines constants for the KHO radix tree structure, used to track preserved +* memory. These constants govern the indexing, sizing, and depth of the tree. +*/ +enum kho_radix_consts { +/* +* The bit position of the order bit (and also the length of the +* page offset) for an order-0 page. +*/ +KHO_ORDER_0_LOG2 =64 -PAGE_SHIFT , + +/* Size of the table in kho_radix_node, in log2 */ +KHO_TABLE_SIZE_LOG2 =const_ilog2 (PAGE_SIZE /sizeof (phys_addr_t)), +
include/linux/kho/abi/kexec_handover.h struct kho_vmalloc { · line 263
Quoted 10 lines
include/linux/kho/abi/kexec_handover.h +10
-0+/* Number of bits in the kho_radix_leaf bitmap, in log2 */ +KHO_BITMAP_SIZE_LOG2 =PAGE_SHIFT +const_ilog2 (BITS_PER_BYTE ), + +/* +* The total tree depth is the number of intermediate levels +* and 1 bitmap level. +*/ +KHO_TREE_MAX_DEPTH = +DIV_ROUND_UP (KHO_ORDER_0_LOG2 -KHO_BITMAP_SIZE_LOG2 , + KHO_TABLE_SIZE_LOG2) + 1,
Extra tab in indentation of DIV_ROUND_UP would make it more readable IMHO.
Quoted 17 lines
include/linux/kho/abi/kexec_handover.h +10
-0+}; + +struct kho_radix_node { +u64 table[1 <<KHO_TABLE_SIZE_LOG2 ]; +}; + +struct kho_radix_leaf { + DECLARE_BITMAP(bitmap, 1 << KHO_BITMAP_SIZE_LOG2); +}; +#endif /* _LINUX_KHO_ABI_KEXEC_HANDOVER_H */ include/linux/kho_radix_tree.h new file +0
-0
include/linux/kho_radix_tree.h line 4
Quoted 4 lines
include/linux/kho_radix_tree.h new file +4
-0@@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_KHO_ABI_RADIX_TREE_H +#define _LINUX_KHO_ABI_RADIX_TREE_H
I misinterpreted the file name during v3 review, no need for _ABI here :)
Quoted 4 lines
include/linux/kho_radix_tree.h new file +4
-0+#include <linux/err.h> +#include <linux/errno.h> +#include <linux/mutex_types.h> +#include <linux/types.h>
...
include/linux/kho_radix_tree.h line 48
Quoted 9 lines
include/linux/kho_radix_tree.h new file +9
-0+#ifdef CONFIG_KEXEC_HANDOVER + +int kho_radix_add_page (struct kho_radix_tree *tree,unsigned long pfn, +unsigned int order); + +void kho_radix_del_page (struct kho_radix_tree *tree,unsigned long pfn, +unsigned int order); + +int kho_radix_walk_tree(struct kho_radix_tree *tree, unsigned int level,
I think 'level' and 'start' should not be a part of public API. We don't want to expose walking the tree from arbitrary place.
Quoted 3 lines
include/linux/kho_radix_tree.h new file +3
-0+ unsigned long start, kho_radix_tree_walk_callback_t cb); + +#else /* #ifdef CONFIG_KEXEC_HANDOVER */
...
include/linux/kho_radix_tree.h
Quoted 13 lines
include/linux/kho_radix_tree.h new file +13
-0+/** +* kho_radix_decode_key - Decodes a radix key back into a physical address and order. +* @key: The unsigned long key to decode. +* @order: An output parameter, a pointer to an unsigned int where the decoded +* page order will be stored. +* +* This function reverses the encoding performed by kho_radix_encode_key(), +* extracting the original physical address and page order from a given key. +* +* Return: The decoded physical address. +*/ +static phys_addr_t kho_radix_decode_key (unsigned long key, +unsigned int *order)
Nit: *order can be on the same line as key
Quoted 1 line
{
...
include/linux/kho_radix_tree.h
Quoted 7 lines
include/linux/kho_radix_tree.h new file +7
-0+static unsigned long kho_radix_get_index (unsigned long key, +unsigned int level) +{ +int s; + +if (level ==0 ) +return kho_radix_get_bitmap_index (key);
I'd drop this and use get_bitmap_index() explicitly for level-0. Maybe also rename _get_index() to _get_table_index().
Quoted 4 lines
include/linux/kho_radix_tree.h new file +4
-0+ + s = ((level -1 ) *KHO_TABLE_SIZE_LOG2 ) +KHO_BITMAP_SIZE_LOG2 ; +return (key >> s) % (1 <<KHO_TABLE_SIZE_LOG2 ); +}
...
Quoted 17 lines
include/linux/kho_radix_tree.h new file +17
-0+void kho_radix_del_page (struct kho_radix_tree *tree,unsigned long pfn, +unsigned int order) +{ +unsigned long key =kho_radix_encode_key (PFN_PHYS (pfn), order); +struct kho_radix_node *node = tree->root; +struct kho_radix_leaf *leaf; +unsigned int i, idx; + +if (WARN_ON_ONCE (!tree->root)) +return ; + +might_sleep (); + +guard (mutex)(&tree->lock); + +/* Go from high levels to low levels */ +for (i =KHO_TREE_MAX_DEPTH -1 ; i >0 ; i--) {
include/linux/kho_radix_tree.h
Quoted 10 lines
include/linux/kho_radix_tree.h new file +10
-0+ idx =kho_radix_get_index (key, i); + +/* +* Attempting to delete a page that has not been preserved, +* return with a warning. +*/ +if (WARN_ON (!node->table[idx])) +return ; + +if (node->table[idx])
There's already WARN_ON(!node->table) and return, no need for another if here.
include/linux/kho_radix_tree.h
Quoted 7 lines
include/linux/kho_radix_tree.h new file +7
-0+ node =phys_to_virt ((phys_addr_t )node->table[idx]); + } + +/* Handle the leaf level bitmap (level 0) */ + leaf = (struct kho_radix_leaf *)node; + idx =kho_radix_get_index (key,0 ); +__clear_bit (idx, leaf->bitmap);
I think I already mentioned it in earlier reviews, but I don't remember any response.
How do we approach freeing empty bitmaps and intermediate nodes? If we do a few preserve/uppreserve cycles for memory that can be allocated and freed in between we might get many unused bitmaps.
My view is that we should free the empty bitmaps, maybe asynchronously. The intermediate nodes probably don't take that much memory to bother with them.
Quoted 2 lines
include/linux/kho_radix_tree.h new file +2
-0+} +EXPORT_SYMBOL_GPL (kho_radix_del_page);
...
Quoted 16 lines
include/linux/kho_radix_tree.h new file +16
-0+/** +* kho_radix_walk_tree - Traverses the radix tree and calls a callback for each preserved page. +* @tree: A pointer to the KHO radix tree to walk. +* @level: The starting level for the walk (typically KHO_TREE_MAX_DEPTH - 1). +* @start: The initial key prefix for the walk (typically 0). +* @cb: A callback function of type kho_radix_tree_walk_callback_t that will be +* invoked for each preserved page found in the tree. The callback receives +* the physical address and order of the preserved page. +* +* This function walks the radix tree, searching from the specified top level +* (@level) down to the lowest level (level 0). For each preserved page found, +* it invokes the provided callback, passing the page's physical address and +* order. +* +* Return: 0 if the walk completed the specified tree, or the non-zero return +* value from the callback that stopped the walk.
include/linux/kho_radix_tree.h
Quoted 10 lines
include/linux/kho_radix_tree.h new file +10
-0+*/ +int kho_radix_walk_tree (struct kho_radix_tree *tree,unsigned int level, +unsigned long start,kho_radix_tree_walk_callback_t cb) +{ +if (WARN_ON_ONCE (!tree->root)) +return -EINVAL ; + +guard (mutex)(&tree->lock); + +return __kho_radix_walk_tree (tree->root, level, start, cb);
Like I said, let's make it
return __kho_radix_walk_tree(tree->root, KHO_TREE_MAX_DEPTH - 1, 0, cb);
and drop level and start parameters.
Quoted 2 lines
include/linux/kho_radix_tree.h new file +2
-0+} +EXPORT_SYMBOL_GPL (kho_radix_walk_tree);
...
Quoted 12 lines
include/linux/kho_radix_tree.h new file +3
-4@@ -260,11 +388,20 @@ static struct page *kho_restore_page(phys_addr_t phys, bool is_folio)/* Clear private to make sure later restores on this page error out. */ page->private =0 ; +/* Head page gets refcount of 1. */ +set_page_count (page,1 ); -if (is_folio) -kho_init_folio (page, info.order); -else -kho_init_pages (page, nr_pages); +/*
include/linux/kho_radix_tree.h static struct page *kho_restore_page(phys_addr_t phys, bool is_folio) · line 404
Quoted 10 lines
include/linux/kho_radix_tree.h new file +10
-0+* For higher order folios, tail pages get a page count of zero. +* For physically contiguous order-0 pages every pages gets a page +* count of 1 +*/ + ref_cnt = is_folio ?0 :1 ; +for (unsigned int i =1 ; i < nr_pages; i++) +set_page_count (page + i, ref_cnt); + +if (is_folio && info.order) +prep_compound_page (page, info.order);
It looks like this reverts latest Pratyush's changes to kho_restore_page(), please rebase on a newer version of mm.git/linux-next.git.
Quoted 3 lines
include/linux/kho_radix_tree.h new file +0
-0adjust_managed_page_count (page, nr_pages);return page;
...
include/linux/kho_radix_tree.h
Quoted 2 lines
include/linux/kho_radix_tree.h new file +2
-0+static int __init kho_radix_memblock_reserve (phys_addr_t phys, +unsigned int order)
I don't think _radix should be mentioned in the callback at all. How about
kho_preserved_memory_reserve()?
Quoted 1 line
{
...
include/linux/kho_radix_tree.h
Quoted 1 line
include/linux/kho_radix_tree.h new file +1
-0+/* Reserve the memory preserved in KHO radix tree in memblock */
/* Reserve memory preserved by KHO in memblock */
What data structure is used to track the preserved memory is not important here.
Quoted 8 lines
include/linux/kho_radix_tree.h new file +5
-0+memblock_reserve (phys, sz); +memblock_reserved_mark_noinit (phys, sz); + info.magic =KHO_PAGE_MAGIC ; + info.order = order; + page->private = info.page_private;return 0 ; }
...
Quoted 6 lines
static
__initintkho_out_fdt_setup(void)
{include/linux/kho_radix_tree.h new file +2
-1+struct kho_radix_tree *tree = &kho_out.radix_tree;void *root = kho_out.fdt; -u64 empty_mem_map =0 ; +u64 preserved_mem_tree_pa;
include/linux/kho_radix_tree.h
Quoted 10 lines
include/linux/kho_radix_tree.h new file +2
-2int err; err =fdt_create (root,PAGE_SIZE ); err |=fdt_finish_reservemap (root); err |=fdt_begin_node (root,"" ); err |=fdt_property_string (root,"compatible" ,KHO_FDT_COMPATIBLE ); - err |=fdt_property (root,KHO_FDT_MEMORY_MAP_PROP_NAME , &empty_mem_map, -sizeof (empty_mem_map)); + + scoped_guard(mutex, &tree->lock) {
This runs exactly one time on boot and there's no place in the code that can concurrently change tree->root.
include/linux/kho_radix_tree.h
Quoted 1 line
include/linux/kho_radix_tree.h new file +1
-0+ preserved_mem_tree_pa = (u64 )virt_to_phys (tree->root);
I don't think we should cast phys_addr_t to u64.
Quoted 11 lines
include/linux/kho_radix_tree.h new file +6
-0+ } + + err |=fdt_property (root,KHO_FDT_MEMORY_MAP_PROP_NAME , + &preserved_mem_tree_pa, +sizeof (preserved_mem_tree_pa)); + err |=fdt_end_node (root); err |=fdt_finish (root);@@ -1332,16 +1329,26 @@ static __init int kho_out_fdt_setup(void)
include/linux/kho_radix_tree.h static __init int kho_out_fdt_setup(void) · line 1339
Quoted 10 lines
include/linux/kho_radix_tree.h new file +2
-0static __init int kho_init (void ) { +struct kho_radix_tree *tree = &kho_out.radix_tree;const void *fdt =kho_get_fdt ();int err =0 ;if (!kho_enable)return 0 ; + scoped_guard(mutex, &tree->lock) {
No need for lock here. If anything tries to access the tree before the root is allocated we are anyway doomed.
include/linux/kho_radix_tree.h static __init int kho_out_fdt_setup(void) · line 1341
Quoted 2 lines
include/linux/kho_radix_tree.h new file +2
-0+ tree->root = (struct kho_radix_node *) +kzalloc (PAGE_SIZE ,GFP_KERNEL );
No need for casting from void *.
Quoted 5 lines
include/linux/kho_radix_tree.h new file +5
-0+if (!tree->root) { + err = -ENOMEM ; + goto err_free_scratch; + } + }
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 1/2] kho: Adopt radix tree for preserved memory tracking
Quoted 16 lines · Mike Rapoport
Quoted 13 lines
+ * The tree is traversed using a key that encodes the page's physical address
+ * (pa) and its order into a single unsigned long value. The encoded key value
+ * is composed of two parts: the 'order bit' in the upper part and the 'page
+ * offset' in the lower part.::
+ *+ * +------------+-----------------------------+--------------------------+ + * | Page Order | Order Bit | Page Offset | + * +------------+-----------------------------+--------------------------+ + * | 0 | ...000100 ... (at bit 52) | pa >> (PAGE_SHIFT + 0) | + * | 1 | ...000010 ... (at bit 51) | pa >> (PAGE_SHIFT + 1) | + * | 2 | ...000001 ... (at bit 50) | pa >> (PAGE_SHIFT + 2) | + * | ... | ... | ... | + * +------------+-----------------------------+--------------------------+Quoted 2 lines
+ *
+ * Page Offset:
Quoted 7 lines
To me "page offset" reads as offset from somewhere and here it's rather pfn on steroids :)
Also in many places in the kernel "page offset" refers to the offset inside a page.Can't say I can think of a better name, but it feels that it should express that this is an address more explicitly.
It is "Shifted Physical Address"
Quoted 11 lines
kernel/liveupdate/kexec_handover.c static int __init kho_parse_enable(char *p) · line 264
Quoted 7 lines
kernel/liveupdate/kexec_handover.c +7
-0+ node =phys_to_virt ((phys_addr_t )node->table[idx]); + } + +/* Handle the leaf level bitmap (level 0) */ + leaf = (struct kho_radix_leaf *)node; + idx =kho_radix_get_index (key,0 ); +__clear_bit (idx, leaf->bitmap);I think I already mentioned it in earlier reviews, but I don't remember any response.
Quoted 3 lines
How do we approach freeing empty bitmaps and intermediate nodes? If we do a few preserve/uppreserve cycles for memory that can be allocated and freed in between we might get many unused bitmaps.
Surely this is an error case??
We shouldn't be unpreserving at all in a normal flow?
Quoted 3 lines
My view is that we should free the empty bitmaps, maybe asynchronously. The intermediate nodes probably don't take that much memory to bother with them.
Telling they are empty would be quite expensive. I think we should not attempt to clean the tree unless there is a really good reason why we should have good flows with alot of unpreserving activity.
I think we would be better served to treat the root cause and fixup what ever is doing preserving/unpreserving loops.
Jason
Mike Rapoport
maintainerRe: [PATCH v4 1/2] kho: Adopt radix tree for preserved memory tracking
Quoted 38 lines · Jason Gunthorpe
Quoted 16 lines · Mike Rapoport
Quoted 13 lines
+ * The tree is traversed using a key that encodes the page's physical address
+ * (pa) and its order into a single unsigned long value. The encoded key value
+ * is composed of two parts: the 'order bit' in the upper part and the 'page
+ * offset' in the lower part.::
+ *+ * +------------+-----------------------------+--------------------------+ + * | Page Order | Order Bit | Page Offset | + * +------------+-----------------------------+--------------------------+ + * | 0 | ...000100 ... (at bit 52) | pa >> (PAGE_SHIFT + 0) | + * | 1 | ...000010 ... (at bit 51) | pa >> (PAGE_SHIFT + 1) | + * | 2 | ...000001 ... (at bit 50) | pa >> (PAGE_SHIFT + 2) | + * | ... | ... | ... | + * +------------+-----------------------------+--------------------------+Quoted 2 lines
+ *
+ * Page Offset:Quoted 7 lines
To me "page offset" reads as offset from somewhere and here it's rather pfn on steroids :)
Also in many places in the kernel "page offset" refers to the offset inside a page.Can't say I can think of a better name, but it feels that it should express that this is an address more explicitly.
It is "Shifted Physical Address"
Quoted 11 lines
kernel/liveupdate/kexec_handover.c static int __init kho_parse_enable(char *p) · line 264
Quoted 7 lines
kernel/liveupdate/kexec_handover.c +7
-0+ node =phys_to_virt ((phys_addr_t )node->table[idx]); + } + +/* Handle the leaf level bitmap (level 0) */ + leaf = (struct kho_radix_leaf *)node; + idx =kho_radix_get_index (key,0 ); +__clear_bit (idx, leaf->bitmap);I think I already mentioned it in earlier reviews, but I don't remember any response.
Quoted 7 lines
Quoted 3 lines
How do we approach freeing empty bitmaps and intermediate nodes? If we do a few preserve/uppreserve cycles for memory that can be allocated and freed in between we might get many unused bitmaps.
Surely this is an error case??
We shouldn't be unpreserving at all in a normal flow?
That's an error case for KHO/LUO, but might not be an error case for other users of the kho_radix_tree.
For example mshv intends to use kho_radix_tree to track the hypervisor memory and there unpreserving will be a part of the normal flow.
I'm not saying we must implement freeing of empty bitmaps from day 1, but at least there should be a comment about it.
Quoted 1 line
Jason
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 1/2] kho: Adopt radix tree for preserved memory tracking
Quoted 2 lines · Mike Rapoport
For example mshv intends to use
kho_radix_treeto track the hypervisor memory and there unpreserving will be a part of the normal flow.
I do not think this is a good idea.
Nothing should be touching KHO until a kexec sequence is started. KHO calls should WARN_ON prior to this point. If a kexec sequence aborts then the entire radix tree should be discarded and it should go back to WARN_ON'ing KHO calls.
Jason
Mike Rapoport
maintainerRe: [PATCH v4 1/2] kho: Adopt radix tree for preserved memory tracking
Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Mike Rapoport
For example mshv intends to use
kho_radix_treeto track the hypervisor memory and there unpreserving will be a part of the normal flow.I do not think this is a good idea.
Sorry I wasn't clear, mshv is not going to use KHO memory tracker but another instance of kho_radix_tree data structure.
I don't see a problem with that.
Quoted 4 lines
Nothing should be touching KHO until a kexec sequence is started. KHO calls should
WARN_ONprior to this point. If a kexec sequence aborts then the entire radix tree should be discarded and it should go back to WARN_ON'ing KHO calls.
The whole point of making KHO stateless was to decouple it from kexec sequence and let userspace control when preservation happens and to allow preserving as much as possible ahead of time to save cycles at kexec-reboot.
Quoted 1 line
Jason
Signature · 2 lines
Sincerely yours, Mike.
Re: [PATCH v4 1/2] kho: Adopt radix tree for preserved memory tracking
Quoted 11 lines · Mike Rapoport
Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Mike Rapoport
For example mshv intends to use
kho_radix_treeto track the hypervisor memory and there unpreserving will be a part of the normal flow.I do not think this is a good idea.
Sorry I wasn't clear, mshv is not going to use KHO memory tracker but another instance of
kho_radix_treedata structure.
I don't see a problem with that.
Oh.. That's.. Interesting but sure if it is made into a library then it makes some sense that it would need freeing support somehow.
I'm surprised there isn't a better data structure for what mshv needs though?
Quoted 9 lines
Quoted 4 lines
Nothing should be touching KHO until a kexec sequence is started. KHO calls should
WARN_ONprior to this point. If a kexec sequence aborts then the entire radix tree should be discarded and it should go back to WARN_ON'ing KHO calls.The whole point of making KHO stateless was to decouple it from kexec sequence and let userspace control when preservation happens and to allow preserving as much as possible ahead of time to save cycles at kexec-reboot.
Sure, but also the data structures should not have a life of their own, it needs to start from an empty slate every time luo starts the sequence. There cannot be cruft left over from previous failed attempts or something
Jason
Pasha Tatashin
maintainerRe: [PATCH v4 1/2] kho: Adopt radix tree for preserved memory tracking
Quoted 14 lines · Jason Gunthorpe
Quoted 11 lines · Mike Rapoport
Quoted 6 lines · Jason Gunthorpe
Quoted 2 lines · Mike Rapoport
For example mshv intends to use
kho_radix_treeto track the hypervisor memory and there unpreserving will be a part of the normal flow.I do not think this is a good idea.
Sorry I wasn't clear, mshv is not going to use KHO memory tracker but another instance of
kho_radix_treedata structure.
I don't see a problem with that.
Quoted 5 lines
Oh.. That's.. Interesting but sure if it is made into a library then it makes some sense that it would need freeing support somehow.
I'm surprised there isn't a better data structure for what mshv needs though?
It seems both MSHV and kho_radix_tree have an ABI requirement for data to be parsable by the next kernel. Even if other libraries might work better, they likely won't need to meet the stability and versioning requirements for cross-kernel interfaces.
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines
Nothing should be touching KHO until a kexec sequence is started. KHO calls should
WARN_ONprior to this point. If a kexec sequence aborts then the entire radix tree should be discarded and it should go back to WARN_ON'ing KHO calls.The whole point of making KHO stateless was to decouple it from kexec sequence and let userspace control when preservation happens and to allow preserving as much as possible ahead of time to save cycles at kexec-reboot.
Quoted 4 lines
Sure, but also the data structures should not have a life of their own, it needs to start from an empty slate every time luo starts the sequence. There cannot be cruft left over from previous failed attempts or something
Even if a kexec fails, as long as someone still holds LUO session references, we might successfully kexec the next time. KHO unpreserving will only occur once the LUO session FDs are closed.
Pasha
Re: [PATCH v4 1/2] kho: Adopt radix tree for preserved memory tracking
In reply to Mike RapoportQuoted 7 lines · Mike Rapoport
Quoted 5 lines · Jason Miu
Introduce a radix tree implementation for tracking preserved memory pages and switch the KHO memory tracking mechanism to use it. This lays the groundwork for a stateless KHO implementation that eliminates the need for serialization and the associated "finalize" state.
...
Quoted 46 lines
...
Quoted 40 lines
+/**
+ * DOC: KHO persistent memory tracker
+ *
+ * KHO tracks preserved memory using a radix tree data structure. Each node of
+ * the tree is exactly a single page. The leaf nodes are bitmaps where each set
+ * bit is a preserved page of any order. The intermediate nodes are tables of
+ * physical addresses that point to a lower level node.
+ *
+ * The tree hierarchy is shown below::
+ *+ * root + * +-------------------+ + * | Level 5 | (struct kho_radix_node) + * +-------------------+ + * | + * v + * +-------------------+ + * | Level 4 | (struct kho_radix_node) + * +-------------------+ + * | + * | ... (intermediate levels) + * | + * v + * +-------------------+ + * | Level 0 | (struct kho_radix_leaf) + * +-------------------++ *
+ * The tree is traversed using a key that encodes the page's physical address
+ * (pa) and its order into a single unsigned long value. The encoded key value
+ * is composed of two parts: the 'order bit' in the upper part and the 'page
+ * offset' in the lower part.::
+ *+ * +------------+-----------------------------+--------------------------+ + * | Page Order | Order Bit | Page Offset | + * +------------+-----------------------------+--------------------------+ + * | 0 | ...000100 ... (at bit 52) | pa >> (PAGE_SHIFT + 0) | + * | 1 | ...000010 ... (at bit 51) | pa >> (PAGE_SHIFT + 1) | + * | 2 | ...000001 ... (at bit 50) | pa >> (PAGE_SHIFT + 2) | + * | ... | ... | ... | + * +------------+-----------------------------+--------------------------+Quoted 2 lines
+ *
+ * Page Offset:
Quoted 8 lines
To me "page offset" reads as offset from somewhere and here it's rather pfn on steroids :)
Also in many places in the kernel "page offset" refers to the offset inside a page.Can't say I can think of a better name, but it feels that it should express that this is an address more explicitly.
I updated this to "Shifted Physical Address," borrowing the idea from Jason Gunthorpe. (Thank you =)
Quoted 22 lines
Quoted 16 lines
+ * The 'page offset' is the physical address normalized for its order. It
+ * effectively represents the page offset for the given order.
+ *
+ * Order Bit:
+ * The 'order bit' encodes the page order by setting a single bit at a
+ * specific position. The position of this bit itself represents the order.
+ *
+ * For instance, on a 64-bit system with 4KB pages (PAGE_SHIFT= 12), the
+ * maximum range for a page offset (for order 0) is 52 bits (64 - 12). This
+ * offset occupies bits [0-51]. For order 0, the order bit is set at
+ * position 52.
+ *
+ * The following diagram illustrates how the encoded key value is split into
+ * indices for the tree levels, withPAGE_SIZEof 4KB::
+ *
+ * 63:60 59:51 50:42 41:33 32:24 23:15 14:0Quoted 5 lines
+ * +---------+--------+--------+--------+--------+--------+-----------------+ + * | 0 | Lv 5 | Lv 4 | Lv 3 | Lv 2 | Lv 1 | Lv 0 (bitmap) | + * +---------+--------+--------+--------+--------+--------+-----------------++ *
+ * The radix tree stores pages of all sizes (orders) in a single 6-level
Quoted 8 lines
"sizes" can be misleading here because "all sizes" can mean non power-of-2 sizes as well. I'd just use "pages of all orders".
Quoted 1 line
+ * hierarchy. It efficiently shares lower table levels, especially due to
Don't we share the higher levels? Also using "tree" instead of "table" seems clearer to me.
yes, updated.
Quoted 69 lines
Quoted 25 lines
+ * common zero top address bits, allowing a single, efficient algorithm to
+ * manage all pages. This bitmap approach also offers memory efficiency; for
+ * example, a 512KB bitmap can cover a 16GB memory range for 0-order pages with
+ *PAGE_SIZE= 4KB.
+ *
+ * The data structures defined here are part of the KHO ABI. Any modification
+ * to these structures that breaks backward compatibility must be accompanied by
+ * an update to the "compatible" string. This ensures that a newer kernel can
+ * correctly interpret the data passed by an older kernel.
+ */include/linux/kho/abi/kexec_handover.h +15
-0+ +/* +* Defines constants for the KHO radix tree structure, used to track preserved +* memory. These constants govern the indexing, sizing, and depth of the tree. +*/ +enum kho_radix_consts { +/* +* The bit position of the order bit (and also the length of the +* page offset) for an order-0 page. +*/ +KHO_ORDER_0_LOG2 =64 -PAGE_SHIFT , + +/* Size of the table in kho_radix_node, in log2 */ +KHO_TABLE_SIZE_LOG2 =const_ilog2 (PAGE_SIZE /sizeof (phys_addr_t)), +include/linux/kho/abi/kexec_handover.h struct kho_vmalloc { · line 263
Quoted 10 lines
include/linux/kho/abi/kexec_handover.h +10
-0+/* Number of bits in the kho_radix_leaf bitmap, in log2 */ +KHO_BITMAP_SIZE_LOG2 =PAGE_SHIFT +const_ilog2 (BITS_PER_BYTE ), + +/* +* The total tree depth is the number of intermediate levels +* and 1 bitmap level. +*/ +KHO_TREE_MAX_DEPTH = +DIV_ROUND_UP (KHO_ORDER_0_LOG2 -KHO_BITMAP_SIZE_LOG2 , + KHO_TABLE_SIZE_LOG2) + 1,Extra tab in indentation of
DIV_ROUND_UPwould make it more readable IMHO.Quoted 17 lines
include/linux/kho/abi/kexec_handover.h +10
-0+}; + +struct kho_radix_node { +u64 table[1 <<KHO_TABLE_SIZE_LOG2 ]; +}; + +struct kho_radix_leaf { + DECLARE_BITMAP(bitmap, 1 << KHO_BITMAP_SIZE_LOG2); +}; +#endif /* _LINUX_KHO_ABI_KEXEC_HANDOVER_H */ include/linux/kho_radix_tree.h new file +0
-0include/linux/kho_radix_tree.h line 4
Quoted 4 lines
include/linux/kho_radix_tree.h new file +4
-0@@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_KHO_ABI_RADIX_TREE_H +#define _LINUX_KHO_ABI_RADIX_TREE_HI misinterpreted the file name during v3 review, no need for
_ABIhere :)Quoted 4 lines
include/linux/kho_radix_tree.h new file +4
-0+#include <linux/err.h> +#include <linux/errno.h> +#include <linux/mutex_types.h> +#include <linux/types.h> ...
Quoted 13 lines
include/linux/kho_radix_tree.h line 48
Quoted 9 lines
include/linux/kho_radix_tree.h new file +9
-0+#ifdef CONFIG_KEXEC_HANDOVER + +int kho_radix_add_page (struct kho_radix_tree *tree,unsigned long pfn, +unsigned int order); + +void kho_radix_del_page (struct kho_radix_tree *tree,unsigned long pfn, +unsigned int order); + +int kho_radix_walk_tree(struct kho_radix_tree *tree, unsigned int level,I think 'level' and 'start' should not be a part of public API. We don't want to expose walking the tree from arbitrary place.
Sure, update the function implementation as well.
Quoted 26 lines
Quoted 3 lines
include/linux/kho_radix_tree.h new file +3
-0+ unsigned long start, kho_radix_tree_walk_callback_t cb); + +#else /* #ifdef CONFIG_KEXEC_HANDOVER */ ...
include/linux/kho_radix_tree.h
Quoted 13 lines
include/linux/kho_radix_tree.h new file +13
-0+/** +* kho_radix_decode_key - Decodes a radix key back into a physical address and order. +* @key: The unsigned long key to decode. +* @order: An output parameter, a pointer to an unsigned int where the decoded +* page order will be stored. +* +* This function reverses the encoding performed by kho_radix_encode_key(), +* extracting the original physical address and page order from a given key. +* +* Return: The decoded physical address. +*/ +static phys_addr_t kho_radix_decode_key (unsigned long key, +unsigned int *order)Nit: *order can be on the same line as key
Quoted 1 line
{
...
Quoted 11 lines
include/linux/kho_radix_tree.h
Quoted 7 lines
include/linux/kho_radix_tree.h new file +7
-0+static unsigned long kho_radix_get_index (unsigned long key, +unsigned int level) +{ +int s; + +if (level ==0 ) +return kho_radix_get_bitmap_index (key);I'd drop this and use
get_bitmap_index()explicitly for level-0. Maybe also rename_get_index()to_get_table_index().
The function name is udpated and the caller will call kho_radix_get_bitmap_index() for bitmap level explicitly.
Quoted 49 lines
Quoted 4 lines
include/linux/kho_radix_tree.h new file +4
-0+ + s = ((level -1 ) *KHO_TABLE_SIZE_LOG2 ) +KHO_BITMAP_SIZE_LOG2 ; +return (key >> s) % (1 <<KHO_TABLE_SIZE_LOG2 ); +}...
Quoted 17 lines
include/linux/kho_radix_tree.h new file +17
-0+void kho_radix_del_page (struct kho_radix_tree *tree,unsigned long pfn, +unsigned int order) +{ +unsigned long key =kho_radix_encode_key (PFN_PHYS (pfn), order); +struct kho_radix_node *node = tree->root; +struct kho_radix_leaf *leaf; +unsigned int i, idx; + +if (WARN_ON_ONCE (!tree->root)) +return ; + +might_sleep (); + +guard (mutex)(&tree->lock); + +/* Go from high levels to low levels */ +for (i =KHO_TREE_MAX_DEPTH -1 ; i >0 ; i--) {include/linux/kho_radix_tree.h
Quoted 10 lines
include/linux/kho_radix_tree.h new file +10
-0+ idx =kho_radix_get_index (key, i); + +/* +* Attempting to delete a page that has not been preserved, +* return with a warning. +*/ +if (WARN_ON (!node->table[idx])) +return ; + +if (node->table[idx])There's already
WARN_ON(!node->table)and return, no need for another if here.include/linux/kho_radix_tree.h
Quoted 7 lines
include/linux/kho_radix_tree.h new file +7
-0+ node =phys_to_virt ((phys_addr_t )node->table[idx]); + } + +/* Handle the leaf level bitmap (level 0) */ + leaf = (struct kho_radix_leaf *)node; + idx =kho_radix_get_index (key,0 ); +__clear_bit (idx, leaf->bitmap);I think I already mentioned it in earlier reviews, but I don't remember any response.
Quoted 8 lines
How do we approach freeing empty bitmaps and intermediate nodes? If we do a few preserve/uppreserve cycles for memory that can be allocated and freed in between we might get many unused bitmaps.
My view is that we should free the empty bitmaps, maybe asynchronously. The intermediate nodes probably don't take that much memory to bother with them.
I think memory preserving and than unpreserving is not a common use case. I suggest keeping the current simpler implementation instead of doing the cleanup, we can address this memory usage optimization later. If we implement this, I tend to clean up all the empty bitmaps and intermediate nodes to avoid incomplete tree traversing paths.
Quoted 60 lines
Quoted 2 lines
include/linux/kho_radix_tree.h new file +2
-0+} +EXPORT_SYMBOL_GPL (kho_radix_del_page);...
Quoted 16 lines
include/linux/kho_radix_tree.h new file +16
-0+/** +* kho_radix_walk_tree - Traverses the radix tree and calls a callback for each preserved page. +* @tree: A pointer to the KHO radix tree to walk. +* @level: The starting level for the walk (typically KHO_TREE_MAX_DEPTH - 1). +* @start: The initial key prefix for the walk (typically 0). +* @cb: A callback function of type kho_radix_tree_walk_callback_t that will be +* invoked for each preserved page found in the tree. The callback receives +* the physical address and order of the preserved page. +* +* This function walks the radix tree, searching from the specified top level +* (@level) down to the lowest level (level 0). For each preserved page found, +* it invokes the provided callback, passing the page's physical address and +* order. +* +* Return: 0 if the walk completed the specified tree, or the non-zero return +* value from the callback that stopped the walk. include/linux/kho_radix_tree.h
Quoted 10 lines
include/linux/kho_radix_tree.h new file +10
-0+*/ +int kho_radix_walk_tree (struct kho_radix_tree *tree,unsigned int level, +unsigned long start,kho_radix_tree_walk_callback_t cb) +{ +if (WARN_ON_ONCE (!tree->root)) +return -EINVAL ; + +guard (mutex)(&tree->lock); + +return __kho_radix_walk_tree (tree->root, level, start, cb);Like I said, let's make it
return __kho_radix_walk_tree(tree->root, KHO_TREE_MAX_DEPTH - 1, 0, cb);and drop level and start parameters.
Quoted 2 lines
include/linux/kho_radix_tree.h new file +2
-0+} +EXPORT_SYMBOL_GPL (kho_radix_walk_tree);...
Quoted 17 lines
include/linux/kho_radix_tree.h new file +8
-4@@ -260,11 +388,20 @@ static struct page *kho_restore_page(phys_addr_t phys, bool is_folio)/* Clear private to make sure later restores on this page error out. */ page->private =0 ; +/* Head page gets refcount of 1. */ +set_page_count (page,1 ); -if (is_folio) -kho_init_folio (page, info.order); -else -kho_init_pages (page, nr_pages); +/* +* For higher order folios, tail pages get a page count of zero. +* For physically contiguous order-0 pages every pages gets a page +* count of 1 +*/ + ref_cnt = is_folio ?0 :1 ;
Quoted 9 lines
include/linux/kho_radix_tree.h static struct page *kho_restore_page(phys_addr_t phys, bool is_folio) · line 404
Quoted 5 lines
include/linux/kho_radix_tree.h new file +5
-0+for (unsigned int i =1 ; i < nr_pages; i++) +set_page_count (page + i, ref_cnt); + +if (is_folio && info.order) +prep_compound_page (page, info.order);It looks like this reverts latest Pratyush's changes to
kho_restore_page(), please rebase on a newer version of mm.git/linux-next.git.
Yes, sync-ed again.
Quoted 53 lines
Quoted 3 lines
include/linux/kho_radix_tree.h new file +0
-0adjust_managed_page_count (page, nr_pages);return page;...
include/linux/kho_radix_tree.h
Quoted 2 lines
include/linux/kho_radix_tree.h new file +2
-0+static int __init kho_radix_memblock_reserve (phys_addr_t phys, +unsigned int order)I don't think
_radixshould be mentioned in the callback at all. How about
kho_preserved_memory_reserve()?Quoted 1 line
{
...
Quoted 1 line
+ /* Reserve the memory preserved in KHO radix tree in memblock */
/* Reserve memory preserved by KHO in memblock */What data structure is used to track the preserved memory is not important here.
Quoted 8 lines
include/linux/kho_radix_tree.h new file +5
-0+memblock_reserve (phys, sz); +memblock_reserved_mark_noinit (phys, sz); + info.magic =KHO_PAGE_MAGIC ; + info.order = order; + page->private = info.page_private;return 0 ; }...
Quoted 6 lines
static
__initintkho_out_fdt_setup(void)
{include/linux/kho_radix_tree.h new file +2
-1+struct kho_radix_tree *tree = &kho_out.radix_tree;void *root = kho_out.fdt; -u64 empty_mem_map =0 ; +u64 preserved_mem_tree_pa;include/linux/kho_radix_tree.h
Quoted 10 lines
include/linux/kho_radix_tree.h new file +2
-2int err; err =fdt_create (root,PAGE_SIZE ); err |=fdt_finish_reservemap (root); err |=fdt_begin_node (root,"" ); err |=fdt_property_string (root,"compatible" ,KHO_FDT_COMPATIBLE ); - err |=fdt_property (root,KHO_FDT_MEMORY_MAP_PROP_NAME , &empty_mem_map, -sizeof (empty_mem_map)); + + scoped_guard(mutex, &tree->lock) {
Quoted 3 lines
This runs exactly one time on boot and there's no place in the code that can concurrently change
tree->root.
Updated and I understand the idea. Personally I think having a mutex lock always when accessing the tree makes the logic symmetric, but this change is certainly more optimal. =)
Quoted 40 lines
Quoted 1 line
+
preserved_mem_tree_pa= (u64)virt_to_phys(tree->root);I don't think we should cast
phys_addr_tto u64.Quoted 11 lines
include/linux/kho_radix_tree.h new file +6
-0+ } + + err |=fdt_property (root,KHO_FDT_MEMORY_MAP_PROP_NAME , + &preserved_mem_tree_pa, +sizeof (preserved_mem_tree_pa)); + err |=fdt_end_node (root); err |=fdt_finish (root);@@ -1332,16 +1329,26 @@ static __init int kho_out_fdt_setup(void)include/linux/kho_radix_tree.h static __init int kho_out_fdt_setup(void) · line 1339
Quoted 10 lines
include/linux/kho_radix_tree.h new file +2
-0static __init int kho_init (void ) { +struct kho_radix_tree *tree = &kho_out.radix_tree;const void *fdt =kho_get_fdt ();int err =0 ;if (!kho_enable)return 0 ; + scoped_guard(mutex, &tree->lock) {No need for lock here. If anything tries to access the tree before the root is allocated we are anyway doomed.
include/linux/kho_radix_tree.h static __init int kho_out_fdt_setup(void) · line 1341
Quoted 2 lines
include/linux/kho_radix_tree.h new file +2
-0+ tree->root = (struct kho_radix_node *) +kzalloc (PAGE_SIZE ,GFP_KERNEL );No need for casting from void *.
include/linux/kho_radix_tree.h static __init int kho_out_fdt_setup(void) · line 1346
Quoted 5 lines
include/linux/kho_radix_tree.h new file +5
-0+if (!tree->root) { + err = -ENOMEM ; + goto err_free_scratch; + } + }
Quoted 3 lines
--
Sincerely yours,
Mike.
Thanks again, sending out the v5 patches.
--
Jason Miu
PATCH v4 2/2 kho: Remove finalize state and clients
In reply to Jason MiuEliminate the kho_finalize() function and its associated state from the KHO subsystem. The transition to a radix tree for memory tracking makes the explicit "finalize" state and its serialization step obsolete.
Remove the kho_finalize() and kho_finalized() APIs and their stub implementations. Update KHO client code and the debugfs interface to no longer call or depend on the kho_finalize() mechanism.
Complete the move towards a stateless KHO, simplifying the overall design by removing unnecessary state management.
--- Documentation/admin-guide/mm/kho.rst | 52 ++++----------------- Documentation/core-api/kho/index.rst | 17 ++----- kernel/liveupdate/kexec_handover.c | 21 +-------- kernel/liveupdate/kexec_handover_debugfs.c | 23 --------- kernel/liveupdate/kexec_handover_internal.h | 3 -- kernel/liveupdate/luo_core.c | 12 +---- kernel/liveupdate/luo_flb.c | 2 +- tools/testing/selftests/kho/init.c | 20 -------- 8 files changed, 16 insertions(+), 134 deletions(-)
Documentation/admin-guide/mm/kho.rst +10 -42
@@ -28,20 +28,10 @@ per NUMA node scratch regions on boot. Perform a KHO kexec =================== -First, before you perform a KHO kexec, you need to move the system into -the :ref:`KHO finalization phase <kho-finalization-phase>` :: - - $ echo 1 > /sys/kernel/debug/kho/out/finalize - -After this command, the KHO FDT is available in -``/sys/kernel/debug/kho/out/fdt``. Other subsystems may also register -their own preserved sub FDTs under -``/sys/kernel/debug/kho/out/sub_fdts/``. - -Next, load the target payload and kexec into it. It is important that you -use the ``-s`` parameter to use the in-kernel kexec file loader, as user -space kexec tooling currently has no support for KHO with the user space -based file loader :: +To perform a KHO kexec, load the target payload and kexec into it. It +is important that you use the ``-s`` parameter to use the in-kernel +kexec file loader, as user space kexec tooling currently has no +support for KHO with the user space based file loader :: # kexec -l /path/to/bzImage --initrd /path/to/initrd -s # kexec -e
@@ -52,40 +42,19 @@ For example, if you used ``reserve_mem`` command line parameter to create an early memory reservation, the new kernel will have that memory at the same physical address as the old kernel. -Abort a KHO exec -================ - -You can move the system out of KHO finalization phase again by calling :: - - $ echo 0 > /sys/kernel/debug/kho/out/active - -After this command, the KHO FDT is no longer available in -``/sys/kernel/debug/kho/out/fdt``. - debugfs Interfaces ================== +These debugfs interfaces are available when the kernel is compiled with +``CONFIG_KEXEC_HANDOVER_DEBUGFS`` set to y. + Currently KHO creates the following debugfs interfaces. Notice that these interfaces may change in the future. They will be moved to sysfs once KHO is stabilized. -``/sys/kernel/debug/kho/out/finalize`` - Kexec HandOver (KHO) allows Linux to transition the state of - compatible drivers into the next kexec'ed kernel. To do so, - device drivers will instruct KHO to preserve memory regions, - which could contain serialized kernel state. - While the state is serialized, they are unable to perform - any modifications to state that was serialized, such as - handed over memory allocations. - - When this file contains "1", the system is in the transition - state. When contains "0", it is not. To switch between the - two states, echo the respective number into this file. - ``/sys/kernel/debug/kho/out/fdt`` - When KHO state tree is finalized, the kernel exposes the - flattened device tree blob that carries its current KHO - state in this file. Kexec user space tooling can use this + The kernel exposes the flattened device tree blob that carries its + current KHO state in this file. Kexec user space tooling can use this as input file for the KHO payload image. ``/sys/kernel/debug/kho/out/scratch_len``
@@ -100,8 +69,7 @@ stabilized. it should place its payload images. ``/sys/kernel/debug/kho/out/sub_fdts/`` - In the KHO finalization phase, KHO producers register their own - FDT blob under this directory. + KHO producers can register their own FDT blob under this directory. ``/sys/kernel/debug/kho/in/fdt`` When the kernel was booted with Kexec HandOver (KHO),
Documentation/core-api/kho/index.rst +3 -14
@@ -9,8 +9,9 @@ Kexec Handover Subsystem Overview ======== -Kexec HandOver (KHO) is a mechanism that allows Linux to preserve memory -regions, which could contain serialized system states, across kexec. +Kexec HandOver (KHO) is a mechanism that allows Linux to preserve +memory regions, containing kernel data structures in their live, +in-memory format, across kexec. KHO uses :ref:`flattened device tree (FDT) <kho_fdt>` to pass information about the preserved state from pre-exec kernel to post-kexec kernel and :ref:`scratch
@@ -71,18 +72,6 @@ for boot memory allocations and as target memory for kexec blobs, some parts of that memory region may be reserved. These reservations are irrelevant for the next KHO, because kexec can overwrite even the original kernel. -KHO finalization phase -====================== - -.. _kho-finalization-phase: - -To enable user space based kexec file loader, the kernel needs to be able to -provide the FDT that describes the current kernel's state before -performing the actual kexec. The process of generating that FDT is -called serialization. When the FDT is generated, some properties -of the system may become immutable because they are already written down -in the FDT. That state is called the KHO finalization phase. - Kexec Handover Radix Tree =========================
kernel/liveupdate/kexec_handover.c +1 -20
@@ -69,8 +69,7 @@ early_param("kho", kho_parse_enable);struct kho_out {void *fdt; -bool finalized; -struct mutex lock;/* protects KHO FDT finalization */ +struct mutex lock;/* protects KHO FDT */ struct kho_radix_tree radix_tree;struct kho_debugfs dbg;
@@ -81,7 +80,6 @@ static struct kho_out kho_out = { .radix_tree = { .lock = __MUTEX_INITIALIZER(kho_out.radix_tree.lock), }, - .finalized = false, };/**
@@ -1191,23 +1189,6 @@ void kho_restore_free(void *mem) }EXPORT_SYMBOL_GPL (kho_restore_free); -int kho_finalize (void ) -{ -if (!kho_enable) -return -EOPNOTSUPP ; - -guard (mutex)(&kho_out.lock); - kho_out.finalized = true; - -return 0 ; -} - -bool kho_finalized (void ) -{ -guard (mutex)(&kho_out.lock); -return kho_out.finalized; -} -struct kho_in {phys_addr_t fdt_phys;phys_addr_t scratch_phys;
kernel/liveupdate/kexec_handover_debugfs.c +0 -23
@@ -75,24 +75,6 @@ void kho_debugfs_fdt_remove(struct kho_debugfs *dbg, void *fdt) } } -static int kho_out_finalize_get (void *data,u64 *val) -{ - *val =kho_finalized (); - -return 0 ; -} - -static int kho_out_finalize_set (void *data,u64 val) -{ -if (val) -return kho_finalize (); -else -return -EINVAL ; -} - -DEFINE_DEBUGFS_ATTRIBUTE (kho_out_finalize_fops, kho_out_finalize_get, - kho_out_finalize_set,"%llu\n" ); -static int scratch_phys_show (struct seq_file *m,void *v) {for (int i =0 ; i < kho_scratch_cnt; i++)
@@ -198,11 +180,6 @@ __init int kho_out_debugfs_init(struct kho_debugfs *dbg)if (IS_ERR (f)) goto err_rmdir; - f =debugfs_create_file ("finalize" ,0600 , dir,NULL , - &kho_out_finalize_fops); -if (IS_ERR (f)) - goto err_rmdir; - dbg->dir = dir; dbg->sub_fdt_dir = sub_fdt_dir;return 0 ;
kernel/liveupdate/kexec_handover_internal.h +0 -3
@@ -22,9 +22,6 @@ struct kho_debugfs {};extern struct kho_scratch *kho_scratch;extern unsigned int kho_scratch_cnt; -bool kho_finalized (void ); -int kho_finalize (void ); -#ifdef CONFIG_KEXEC_HANDOVER_DEBUGFS int kho_debugfs_init (void );void kho_in_debugfs_init (struct kho_debugfs *dbg,const void *fdt);
kernel/liveupdate/luo_core.c +1 -11
@@ -231,17 +231,7 @@ int liveupdate_reboot(void)luo_flb_serialize (); - err =kho_finalize (); -if (err) { -pr_err ("kho_finalize failed %d\n" , err); -/* -* kho_finalize() may return libfdt errors, to aboid passing to -* userspace unknown errors, change this to EAGAIN. -*/ - err = -EAGAIN ; - } - -return err; +return 0 ; }/**
kernel/liveupdate/luo_flb.c +1 -1
@@ -630,7 +630,7 @@ int __init luo_flb_setup_incoming(void *fdt_in)* data handle, and the final reference count. This allows the new kernel to * find the appropriate handler and reconstruct the FLB's state. * -* Context: Called from liveupdate_reboot() just before kho_finalize(). +* Context: Called from liveupdate_reboot() just before return. */ void luo_flb_serialize (void ) {
tools/testing/selftests/kho/init.c +0 -20
@@ -11,7 +11,6 @@/* from arch/x86/include/asm/setup.h */ #define COMMAND_LINE_SIZE 2048 -#define KHO_FINALIZE "/debugfs/kho/out/finalize"#define KERNEL_IMAGE "/kernel"static int mount_filesystems (void )
@@ -22,22 +21,6 @@ static int mount_filesystems(void)return mount ("proc" ,"/proc" ,"proc" ,0 ,NULL ); } -static int kho_enable (void ) -{ -const char enable[] ="1" ; -int fd; - - fd =open (KHO_FINALIZE ,O_RDWR ); -if (fd <0 ) -return -1 ; - -if (write (fd, enable,sizeof (enable)) !=sizeof (enable)) -return 1 ; - -close (fd); -return 0 ; -} -static long kexec_file_load (int kernel_fd,int initrd_fd,unsigned long cmdline_len,const char *cmdline,unsigned long flags)
@@ -78,9 +61,6 @@ int main(int argc, char *argv[])if (mount_filesystems ()) goto err_reboot; -if (kho_enable ()) - goto err_reboot; -if (kexec_load ()) goto err_reboot;
Signature · 1 line
2.52.0.457.g6b5491de43-goog
Mike Rapoport
maintainerRe: [PATCH v4 2/2] kho: Remove finalize state and clients
Quoted 62 lines · Jason Miu
Eliminate the
kho_finalize()function and its associated state from the KHO subsystem. The transition to a radix tree for memory tracking makes the explicit "finalize" state and its serialization step obsolete.Remove the
kho_finalize()andkho_finalized()APIs and their stub implementations. Update KHO client code and the debugfs interface to no longer call or depend on thekho_finalize()mechanism.Complete the move towards a stateless KHO, simplifying the overall design by removing unnecessary state management.
Signed-off-by Jason Miu <[email protected]>--- Documentation/admin-guide/mm/kho.rst | 52 ++++----------------- Documentation/core-api/kho/index.rst | 17 ++----- kernel/liveupdate/kexec_handover.c | 21 +-------- kernel/liveupdate/kexec_handover_debugfs.c | 23 --------- kernel/liveupdate/kexec_handover_internal.h | 3 -- kernel/liveupdate/luo_core.c | 12 +---- kernel/liveupdate/luo_flb.c | 2 +- tools/testing/selftests/kho/init.c | 20 -------- 8 files changed, 16 insertions(+), 134 deletions(-)Documentation/admin-guide/mm/kho.rst +4
-19@@ -28,20 +28,10 @@ per NUMA node scratch regions on boot. Perform a KHO kexec =================== -First, before you perform a KHO kexec, you need to move the system into -the :ref:`KHO finalization phase <kho-finalization-phase>` :: - - $ echo 1 > /sys/kernel/debug/kho/out/finalize - -After this command, the KHO FDT is available in -``/sys/kernel/debug/kho/out/fdt``. Other subsystems may also register -their own preserved sub FDTs under -``/sys/kernel/debug/kho/out/sub_fdts/``. - -Next, load the target payload and kexec into it. It is important that you -use the ``-s`` parameter to use the in-kernel kexec file loader, as user -space kexec tooling currently has no support for KHO with the user space -based file loader :: +To perform a KHO kexec, load the target payload and kexec into it. It +is important that you use the ``-s`` parameter to use the in-kernel +kexec file loader, as user space kexec tooling currently has no +support for KHO with the user space based file loader :: # kexec -l /path/to/bzImage --initrd /path/to/initrd -s # kexec -e@@ -52,40 +42,19 @@ For example, if you used ``reserve_mem`` command line parameter to create an early memory reservation, the new kernel will have that memory at the same physical address as the old kernel. -Abort a KHO exec -================ - -You can move the system out of KHO finalization phase again by calling :: -
Documentation/admin-guide/mm/kho.rst For example, if you used ``reserve_mem`` command line parameter to create · line 49
Quoted 10 lines
Documentation/admin-guide/mm/kho.rst +2
-5- $ echo 0 > /sys/kernel/debug/kho/out/active - -After this command, the KHO FDT is no longer available in -``/sys/kernel/debug/kho/out/fdt``. - debugfs Interfaces ================== +These debugfs interfaces are available when the kernel is compiled with +``CONFIG_KEXEC_HANDOVER_DEBUGFS`` set to y.
s/set to y/enabled/
Documentation/admin-guide/mm/kho.rst stabilized. · line 72
Quoted 4 lines
Documentation/admin-guide/mm/kho.rst +1
-2``/sys/kernel/debug/kho/out/sub_fdts/`` - In the KHO finalization phase, KHO producers register their own - FDT blob under this directory. + KHO producers can register their own FDT blob under this directory.
"own FDT or another binary blob ..."
Documentation/core-api/kho/index.rst Kexec Handover Subsystem · line 14
Quoted 13 lines
Documentation/core-api/kho/index.rst +3
-2@@ -9,8 +9,9 @@ Kexec Handover Subsystem Overview ======== -Kexec HandOver (KHO) is a mechanism that allows Linux to preserve memory -regions, which could contain serialized system states, across kexec. +Kexec HandOver (KHO) is a mechanism that allows Linux to preserve +memory regions, containing kernel data structures in their live, +in-memory format, across kexec.
I'm not sure about "live, in-memory format". memfd does preserve some serialized state rather than "live memfd".
The existing phrasing is general enough and I'd leave it as is.
kernel/liveupdate/luo_flb.c int __init luo_flb_setup_incoming(void *fdt_in) · line 633
Quoted 10 lines
kernel/liveupdate/luo_flb.c +1
-1@@ -630,7 +630,7 @@ int __init luo_flb_setup_incoming(void *fdt_in)* data handle, and the final reference count. This allows the new kernel to * find the appropriate handler and reconstruct the FLB's state. * -* Context: Called from liveupdate_reboot() just before kho_finalize(). +* Context: Called from liveupdate_reboot() just before return.
Just
Called from liveupdate_reboot()
is enough IMO.
Quoted 1 line
kernel/liveupdate/luo_flb.c +0
-0*/
Signature · 2 lines
Sincerely yours, Mike.