From: "Zhu, Yi" <chuyee@google.com>
To: Josh Poimboeuf <jpoimboe@kernel.org>,
Miroslav Benes <mbenes@suse.cz>, Petr Mladek <pmladek@suse.com>,
Joe Lawrence <joe.lawrence@redhat.com>,
Song Liu <song@kernel.org>,
Wilson Felipe Pereira <wfelipe@google.com>,
Dylan Hatch <dylanbhatch@google.com>
Cc: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 1/2] livepatch: look up klp_ops in a hash table
Date: Fri, 4 Dec 2026 00:35:06 +0000 [thread overview]
Message-ID: <3f1027446e0fb57f76fca545840f99f19fa0237c.1796344045.git.chuyee@google.com> (raw)
In-Reply-To: <cover.1796344045.git.chuyee@google.com>
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. Store old_func
in struct klp_ops so a lookup no longer has to dereference the top of
func_stack. The stack check can also run from __klp_sched_try_switch()
with only preemption disabled, so use the RCU hash list primitives:
publish a klp_ops only after its ftrace ops is registered, unpublish it
before tearing it down, and free it with kfree_rcu().
Signed-off-by: "Zhu, Yi" <chuyee@google.com>
---
kernel/livepatch/patch.c | 38 ++++++++++++++++++++++++++------------
kernel/livepatch/patch.h | 8 ++++++--
2 files changed, 32 insertions(+), 14 deletions(-)
diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c
index 3f54a017bbf6..e93105187bc4 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>
@@ -20,17 +21,22 @@
#include "patch.h"
#include "transition.h"
-static LIST_HEAD(klp_ops);
+/*
+ * 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.
+ */
+#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;
- struct klp_func *func;
- list_for_each_entry(ops, &klp_ops, node) {
- func = list_first_entry(&ops->func_stack, struct klp_func,
- stack_node);
- if (func->old_func == old_func)
+ 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;
}
@@ -144,12 +150,17 @@ static void klp_unpatch_func(struct klp_func *func)
if (WARN_ON(!ftrace_loc))
return;
+ /*
+ * Unpublish ops before tearing it down. Lockless
+ * klp_find_ops() readers may still see it until a grace
+ * period has elapsed, hence kfree_rcu().
+ */
+ hash_del_rcu(&ops->node);
WARN_ON(unregister_ftrace_function(&ops->fops));
WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0));
list_del_rcu(&func->stack_node);
- list_del(&ops->node);
- kfree(ops);
+ kfree_rcu(ops, rcu);
} else {
list_del_rcu(&func->stack_node);
}
@@ -191,8 +202,7 @@ static int klp_patch_func(struct klp_func *func)
FTRACE_OPS_FL_IPMODIFY |
FTRACE_OPS_FL_PERMANENT;
- list_add(&ops->node, &klp_ops);
-
+ ops->old_func = func->old_func;
INIT_LIST_HEAD(&ops->func_stack);
list_add_rcu(&func->stack_node, &ops->func_stack);
@@ -211,7 +221,12 @@ static int klp_patch_func(struct klp_func *func)
goto err;
}
-
+ /*
+ * Publish ops for lockless klp_find_ops() readers only once it
+ * is fully set up, so that the error path above can free it
+ * right away.
+ */
+ hash_add_rcu(klp_ops_table, &ops->node, (unsigned long)ops->old_func);
} else {
list_add_rcu(&func->stack_node, &ops->func_stack);
}
@@ -222,7 +237,6 @@ static int klp_patch_func(struct klp_func *func)
err:
list_del_rcu(&func->stack_node);
- list_del(&ops->node);
kfree(ops);
return ret;
}
diff --git a/kernel/livepatch/patch.h b/kernel/livepatch/patch.h
index d5f2fbe373e0..67570bbf02c6 100644
--- a/kernel/livepatch/patch.h
+++ b/kernel/livepatch/patch.h
@@ -15,14 +15,18 @@
* 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;
+ struct rcu_head rcu;
};
struct klp_ops *klp_find_ops(void *old_func);
--
2.56.1.987.g2ec0ab4260-goog
next prev parent reply other threads:[~2026-12-04 0:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 0:35 [PATCH 0/2] livepatch: make the transition stack check scale with patch size Zhu, Yi
2026-12-04 0:35 ` Zhu, Yi [this message]
2026-12-04 0:51 ` [PATCH 1/2] livepatch: look up klp_ops in a hash table sashiko-bot
2026-12-04 0:35 ` [PATCH 2/2] livepatch: check task stacks against a sorted range table Zhu, Yi
2026-12-04 0:59 ` sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3f1027446e0fb57f76fca545840f99f19fa0237c.1796344045.git.chuyee@google.com \
--to=chuyee@google.com \
--cc=dylanbhatch@google.com \
--cc=joe.lawrence@redhat.com \
--cc=jpoimboe@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mbenes@suse.cz \
--cc=pmladek@suse.com \
--cc=song@kernel.org \
--cc=wfelipe@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox