Add kho_radix_tree_freeze() to prevent further modifications to a KHO radix tree. After freezing, kho_radix_add_key() and kho_radix_del_key() return -EBUSY. This is used by the MSHV page preservation code to lock the tree before serializing it for kexec.
Also change kho_radix_del_key() from void to int so it can report -EBUSY (frozen) and -ENOENT (key not present). Signed-off-by: Jork Loeser <[email protected]> --- include/linux/kho_radix_tree.h | 24 ++++++++++---- kernel/liveupdate/kexec_handover.c | 51 +++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/include/linux/kho_radix_tree.h b/include/linux/kho_radix_tree.h index c0840ecb230c..4fe2238e1e30 100644 --- a/include/linux/kho_radix_tree.h +++ b/include/linux/kho_radix_tree.h @@ -21,10 +21,10 @@ * scheme. Each key is an unsigned long that combines a page's physical * address and its order. * - * Client code is responsible for allocating the root node of the tree, - * initializing the mutex lock, and managing its lifecycle. It must use the - * tree data structures defined in the KHO ABI, - * `include/linux/kho/abi/kexec_handover.h`. + * Client code must initialize the tree using kho_radix_tree_init(). Pass + * a physical address to restore a tree preserved across kexec, or 0 to + * allocate a fresh empty tree. The tree uses data structures defined in + * the KHO ABI, `include/linux/kho/abi/kexec_handover.h`. */ struct kho_radix_node; @@ -32,6 +32,7 @@ struct kho_radix_node; struct kho_radix_tree { struct kho_radix_node *root; struct mutex lock; /* protects the tree's structure and root pointer */ + bool frozen; }; /** @@ -51,11 +52,12 @@ struct kho_radix_walk_cb { #ifdef CONFIG_KEXEC_HANDOVER int kho_radix_add_key(struct kho_radix_tree *tree, unsigned long key); -void kho_radix_del_key(struct kho_radix_tree *tree, unsigned long key); +int kho_radix_del_key(struct kho_radix_tree *tree, unsigned long key); int kho_radix_walk_tree(struct kho_radix_tree *tree, const struct kho_radix_walk_cb *cb, void *data); int kho_radix_init_tree(struct kho_radix_tree *tree, struct kho_radix_node *root); void kho_radix_destroy_tree(struct kho_radix_tree *tree); +int kho_radix_tree_freeze(struct kho_radix_tree *tree); #else /* #ifdef CONFIG_KEXEC_HANDOVER */ @@ -64,8 +66,11 @@ static inline int kho_radix_add_key(struct kho_radix_tree *tree, unsigned long k return -EOPNOTSUPP; } -static inline void kho_radix_del_key(struct kho_radix_tree *tree, - unsigned long key) { } +static inline int kho_radix_del_key(struct kho_radix_tree *tree, + unsigned long key) +{ + return -EOPNOTSUPP; +} static inline int kho_radix_walk_tree(struct kho_radix_tree *tree, const struct kho_radix_walk_cb *cb, void *data) @@ -81,6 +86,11 @@ static inline int kho_radix_init_tree(struct kho_radix_tree *tree, static inline void kho_radix_destroy_tree(struct kho_radix_tree *tree) { } +static inline int kho_radix_tree_freeze(struct kho_radix_tree *tree) +{ + return -EOPNOTSUPP; +} + #endif /* #ifdef CONFIG_KEXEC_HANDOVER */ #endif /* _LINUX_KHO_RADIX_TREE_H */ diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_handover.c index 797ec285b698..2e2b4e73f00d 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -79,9 +79,6 @@ struct kho_out { static struct kho_out kho_out = { .lock = __MUTEX_INITIALIZER(kho_out.lock), - .radix_tree = { - .lock = __MUTEX_INITIALIZER(kho_out.radix_tree.lock), - }, }; struct kho_in { @@ -180,6 +177,28 @@ static void __ref kho_radix_free_node(struct kho_radix_node *node) memblock_free(node, PAGE_SIZE); } +/** + * kho_radix_tree_freeze - Freeze the tree, preventing further modifications. + * @tree: The KHO radix tree to freeze. + * + * After freezing, kho_radix_add_key() and kho_radix_del_key() will return + * -EBUSY. The check is performed under the tree's mutex, so there is no + * race between a concurrent add/del and the freeze. + * + * Return: 0 on success, -EBUSY if the tree is already frozen. + */ +int kho_radix_tree_freeze(struct kho_radix_tree *tree) +{ + guard(mutex)(&tree->lock); + + if (tree->frozen) + return -EBUSY; + + tree->frozen = true; + return 0; +} +EXPORT_SYMBOL_GPL(kho_radix_tree_freeze); + /** * kho_radix_add_key - Add a key to the radix tree. * @tree: The KHO radix tree. @@ -210,6 +229,9 @@ int kho_radix_add_key(struct kho_radix_tree *tree, unsigned long key) guard(mutex)(&tree->lock); + if (tree->frozen) + return -EBUSY; + /* Go from high levels to low levels */ for (i = KHO_TREE_MAX_DEPTH - 1; i > 0; i--) { idx = kho_radix_get_table_index(key, i); @@ -268,20 +290,26 @@ EXPORT_SYMBOL_GPL(kho_radix_add_key); * This function traverses the radix tree and clears the bit corresponding to * the key, effectively removing it from the tree. It does not free the tree's * intermediate nodes, even if they become empty. + * + * Return: 0 on success, -EINVAL if the tree is uninitialized, -EBUSY if + * frozen, -ENOENT if the key was not present. */ -void kho_radix_del_key(struct kho_radix_tree *tree, unsigned long key) +int kho_radix_del_key(struct kho_radix_tree *tree, unsigned long key) { struct kho_radix_node *node = tree->root; struct kho_radix_leaf *leaf; unsigned int i, idx; if (WARN_ON_ONCE(!tree->root)) - return; + return -EINVAL; might_sleep(); guard(mutex)(&tree->lock); + if (WARN_ON_ONCE(tree->frozen)) + return -EBUSY; + /* Go from high levels to low levels */ for (i = KHO_TREE_MAX_DEPTH - 1; i > 0; i--) { idx = kho_radix_get_table_index(key, i); @@ -291,7 +319,7 @@ void kho_radix_del_key(struct kho_radix_tree *tree, unsigned long key) * return with a warning. */ if (WARN_ON(!node->table[idx])) - return; + return -ENOENT; node = phys_to_virt(node->table[idx]); } @@ -300,6 +328,8 @@ void kho_radix_del_key(struct kho_radix_tree *tree, unsigned long key) leaf = (struct kho_radix_leaf *)node; idx = kho_radix_get_bitmap_index(key); __clear_bit(idx, leaf->bitmap); + + return 0; } EXPORT_SYMBOL_GPL(kho_radix_del_key); @@ -346,6 +376,7 @@ int kho_radix_init_tree(struct kho_radix_tree *tree, struct kho_radix_node *root tree->root = root; mutex_init(&tree->lock); + tree->frozen = false; return 0; } EXPORT_SYMBOL_GPL(kho_radix_init_tree); @@ -1746,11 +1777,9 @@ static __init int kho_init(void) if (!kho_enable) return 0; - tree->root = kzalloc(PAGE_SIZE, GFP_KERNEL); - if (!tree->root) { - err = -ENOMEM; + err = kho_radix_init_tree(tree, NULL); + if (err) goto err_free_scratch; - } kho_out.fdt = kho_alloc_preserve(PAGE_SIZE); if (IS_ERR(kho_out.fdt)) { @@ -1807,7 +1836,7 @@ static __init int kho_init(void) err_free_fdt: kho_unpreserve_free(kho_out.fdt); err_free_kho_radix_tree_root: - kfree(tree->root); + free_page((unsigned long)tree->root); tree->root = NULL; err_free_scratch: kho_out.fdt = NULL; -- 2.43.0

