[PATCH v2 2/3] livepatch: look up klp_ops in a hash table
From: Zhu, Yi
Date: Thu Dec 03 2026 - 23:51:59 EST
klp_find_ops() walks a global list with one entry per patched function.
It is called for every patched function when a function is patched or
unpatched, and for every patched function of every task in
klp_check_stack_func() during a patching transition. That makes the
per-task stack check O(N^2) in the number of patched functions. With a
synthetic 5,000-function livepatch and ~1,900 tasks, the first
transition pass takes 156 seconds and triggers soft lockups.
Key the klp_ops on the patched function in a hash table, keeping the RCU
rules of the list it replaces.
Signed-off-by: "Zhu, Yi" <chuyee@xxxxxxxxxx>
---
kernel/livepatch/patch.c | 20 +++++++++++---------
kernel/livepatch/patch.h | 4 ++--
2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c
index 7330a189d6d0..10b32edd4a9c 100644
--- a/kernel/livepatch/patch.c
+++ b/kernel/livepatch/patch.c
@@ -12,6 +12,7 @@
#include <linux/livepatch.h>
#include <linux/list.h>
#include <linux/ftrace.h>
+#include <linux/hashtable.h>
#include <linux/rculist.h>
#include <linux/slab.h>
#include <linux/bug.h>
@@ -21,19 +22,20 @@
#include "transition.h"
/*
- * All klp_ops. Updates are serialized by klp_mutex. Lookups normally hold
- * klp_mutex too, but the stack check can also run from __klp_sched_try_switch()
- * with only preemption disabled, so entries are added and removed with the RCU
- * list primitives.
+ * All klp_ops, keyed on the patched function. Updates are serialized by
+ * klp_mutex. Lookups normally hold klp_mutex too, but the stack check can
+ * also run from __klp_sched_try_switch() with only preemption disabled, so
+ * entries are added and removed with the RCU list primitives.
*/
-static LIST_HEAD(klp_ops);
+#define KLP_OPS_HASH_BITS 10
+static DEFINE_HASHTABLE(klp_ops_table, KLP_OPS_HASH_BITS);
struct klp_ops *klp_find_ops(void *old_func)
{
struct klp_ops *ops;
- list_for_each_entry_rcu(ops, &klp_ops, node,
- lockdep_is_held(&klp_mutex)) {
+ hash_for_each_possible_rcu(klp_ops_table, ops, node, (unsigned long)old_func,
+ lockdep_is_held(&klp_mutex)) {
if (ops->old_func == old_func)
return ops;
}
@@ -156,7 +158,7 @@ static void klp_unpatch_func(struct klp_func *func)
* klp_find_ops() readers may still see it until a grace
* period has elapsed, hence kfree_rcu().
*/
- list_del_rcu(&ops->node);
+ hash_del_rcu(&ops->node);
list_del_rcu(&func->stack_node);
kfree_rcu(ops, rcu);
} else {
@@ -224,7 +226,7 @@ static int klp_patch_func(struct klp_func *func)
* is fully set up, so that the error path above can free it
* right away.
*/
- list_add_rcu(&ops->node, &klp_ops);
+ hash_add_rcu(klp_ops_table, &ops->node, (unsigned long)ops->old_func);
} else {
list_add_rcu(&func->stack_node, &ops->func_stack);
}
diff --git a/kernel/livepatch/patch.h b/kernel/livepatch/patch.h
index bf3b9017910a..67570bbf02c6 100644
--- a/kernel/livepatch/patch.h
+++ b/kernel/livepatch/patch.h
@@ -15,14 +15,14 @@
* struct's func_stack list. The winner is the klp_func at the top of the
* func_stack (front of the list).
*
- * @node: node for the global klp_ops list
+ * @node: node in the global klp_ops hash table, keyed on @old_func
* @old_func: the patched function shared by all klp_func's on @func_stack
* @func_stack: list head for the stack of klp_func's (active func is on top)
* @fops: registered ftrace ops struct
* @rcu: for freeing after lockless klp_find_ops() readers are done
*/
struct klp_ops {
- struct list_head node;
+ struct hlist_node node;
void *old_func;
struct list_head func_stack;
struct ftrace_ops fops;
--
2.56.1.987.g2ec0ab4260-goog