* [PATCH 0/2] livepatch: make the transition stack check scale with patch size
@ 2026-12-04 0:35 Zhu, Yi
2026-12-04 0:35 ` [PATCH 1/2] livepatch: look up klp_ops in a hash table Zhu, Yi
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Zhu, Yi @ 2026-12-04 0:35 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Wilson Felipe Pereira, Dylan Hatch
Cc: live-patching, linux-kernel
During a patching transition, every task's stack is checked against
every patched function. Two things make that expensive for large
cumulative livepatches:
- klp_check_stack_func() calls klp_find_ops(), a walk of a global list
with one entry per patched function, for every patched function.
The per-task check is therefore O(N^2) in the number of patched
functions N.
- Even without that lookup, each task repeats the same O(N * D) scan
(D = stack depth), although the forbidden address ranges are the
same for all tasks.
The first transition pass runs under read_lock(&tasklist_lock), so on
large patches this stalls fork() and exit() machine-wide for seconds to
minutes.
Patch 1 keys klp_ops on the patched function in an RCU hash table.
Patch 2 builds a sorted, RCU-published table of the patched address
ranges once per transition state change, and looks up each stack entry
with bsearch(). The per-task cost drops to O(D log N). If the table
can't be built, the existing per-function check is used.
Testing, on livepatching/for-next (2c3787447317) with and without the
series, in a 32-vCPU QEMU/KVM guest (Intel Xeon @ 2.60GHz host,
CONFIG_PREEMPT_LAZY):
- livepatch kselftests, with lockdep, PROVE_RCU and DEBUG_LIST: 39
passed, 4 skipped (legacy replace attribute not present), 0 failed on
both kernels; no lockdep, RCU or WARN splats.
- A synthetic target module with N noinline functions and a livepatch
replacing all of them; idle threads added to reach the task counts
below. The first klp_try_complete_transition() pass, timed with a
kprobe, and the time from insmod until the transition completes:
first pass insmod -> patched
N tasks base patched base patched
100 ~1.9k 19.6 ms 1.7 ms 1.9 s 1.9 s
1,000 ~1.9k 3.41 s 1.9 ms 4.9 s 2.9 s
2,000 ~1.9k 14.5 s 1.7 ms 17.9 s 4.8 s
3,000 ~1.9k 30.7 s 2.2 ms 36.1 s 6.9 s
5,000 ~1.9k 156 s 1.8 ms 167 s 11.8 s
10,000 ~1.9k 1,069 s 1.8 ms 1,096 s 27.2 s
2,000 ~3.4k 25.9 s 3.0 ms 29.3 s 5.0 s
2,000 ~50.4k - 35.5 ms - 5.5 s
10,000 ~50.4k - 37.5 ms - 27.8 s
On base, the first pass triggers soft lockups from 3,000 functions
on; with ~3.4k tasks, udev-worker tasks on other CPUs report soft
lockups too. The patched kernel shows no soft lockups or RCU stalls
in any run. Its remaining insmod time is module loading, symbol
resolution and ftrace registration, which this series does not
change.
Disabling a large patch has a separate cost: two synchronize_rcu()
calls per function in the ftrace unregister path. That will be
addressed separately.
Zhu, Yi (2):
livepatch: look up klp_ops in a hash table
livepatch: check task stacks against a sorted range table
kernel/livepatch/core.c | 10 ++
kernel/livepatch/patch.c | 38 ++++---
kernel/livepatch/patch.h | 8 +-
kernel/livepatch/transition.c | 190 +++++++++++++++++++++++++++++++---
kernel/livepatch/transition.h | 1 +
5 files changed, 221 insertions(+), 26 deletions(-)
base-commit: 2c37874473176739f4107c70739a930501d619c8
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/2] livepatch: look up klp_ops in a hash table
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
2026-12-04 0:35 ` [PATCH 2/2] livepatch: check task stacks against a sorted range table Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 0/3] livepatch: make the transition stack check scale with patch size Zhu, Yi
2 siblings, 0 replies; 7+ messages in thread
From: Zhu, Yi @ 2026-12-04 0:35 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Wilson Felipe Pereira, Dylan Hatch
Cc: live-patching, linux-kernel
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] livepatch: check task stacks against a sorted range table
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 ` [PATCH 1/2] livepatch: look up klp_ops in a hash table Zhu, Yi
@ 2026-12-04 0:35 ` Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 0/3] livepatch: make the transition stack check scale with patch size Zhu, Yi
2 siblings, 0 replies; 7+ messages in thread
From: Zhu, Yi @ 2026-12-04 0:35 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Wilson Felipe Pereira, Dylan Hatch
Cc: live-patching, linux-kernel
During a transition, klp_check_stack() computes, for every task and every
patched function, the code range that must not be on the task's stack, and
scans the whole stack for it. The ranges are the same for every task, so
this repeats the same work T times and costs O(N * D) per task even without
the old O(N) klp_find_ops() walk. With thousands of patched functions and
thousands of tasks, the first transition pass runs for seconds to minutes
under read_lock(&tasklist_lock), which stalls fork() and exit()
machine-wide.
Build a table of those ranges, sorted by start address, and look up each
stack entry with bsearch(), making the per-task cost O(D log N).
klp_check_stack() also runs from __klp_sched_try_switch() on any CPU
without klp_mutex, so the table is never changed in place. A new table is
built under klp_mutex whenever the patched functions or the target state
change while a transition is in progress: when the transition starts
(including a reversed one), when a module is loaded or unloaded, and when
it completes. It's published with RCU and the old one is freed with
kvfree_rcu(). Readers already run with preemption disabled. If the table
can't be allocated, or two ranges overlap, the existing per-function check
is used.
The table may briefly be stale around module load and unload. A module
that is being loaded has not run yet, so no task can be in one of its
functions; a module that is going away only leaves extra ranges behind,
which makes the check more conservative.
Signed-off-by: "Zhu, Yi" <chuyee@google.com>
---
kernel/livepatch/core.c | 10 ++
kernel/livepatch/transition.c | 190 +++++++++++++++++++++++++++++++---
kernel/livepatch/transition.h | 1 +
3 files changed, 189 insertions(+), 12 deletions(-)
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index b83715272e8c..355ba2170554 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -1366,6 +1366,10 @@ int klp_module_coming(struct module *mod)
}
}
+ /* The transition patch may now patch functions in this module. */
+ if (klp_transition_patch)
+ klp_update_stack_ranges();
+
mutex_unlock(&klp_mutex);
return 0;
@@ -1380,6 +1384,8 @@ int klp_module_coming(struct module *mod)
mod->klp_alive = false;
obj->mod = NULL;
klp_cleanup_module_patches_limited(mod, patch);
+ if (klp_transition_patch)
+ klp_update_stack_ranges();
mutex_unlock(&klp_mutex);
return ret;
@@ -1401,6 +1407,10 @@ void klp_module_going(struct module *mod)
klp_cleanup_module_patches_limited(mod, NULL);
+ /* Drop this module's functions from the transition range table. */
+ if (klp_transition_patch)
+ klp_update_stack_ranges();
+
mutex_unlock(&klp_mutex);
}
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index ef70ff582da1..fc35e09eff69 100644
--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -7,7 +7,11 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <linux/bsearch.h>
#include <linux/cpu.h>
+#include <linux/rcupdate.h>
+#include <linux/slab.h>
+#include <linux/sort.h>
#include <linux/stacktrace.h>
#include <linux/static_call.h>
#include "core.h"
@@ -27,6 +31,31 @@ static int klp_target_state = KLP_TRANSITION_IDLE;
static unsigned int klp_signals_cnt;
+/*
+ * The code ranges which must not be on a task's stack for the task to switch
+ * to klp_target_state, one per patched function of klp_transition_patch,
+ * sorted by start address so that each stack entry needs one bsearch().
+ *
+ * klp_check_stack() reads the table with preemption disabled, both from
+ * transition passes (under klp_mutex) and from __klp_sched_try_switch()
+ * (without klp_mutex). So the table is never modified in place: a new one is
+ * built under klp_mutex whenever the set of patched functions or the target
+ * state changes, and published with RCU. A NULL table means the slow path.
+ */
+struct klp_stack_range {
+ unsigned long start;
+ unsigned long end;
+ const char *name;
+};
+
+struct klp_stack_ranges {
+ struct rcu_head rcu;
+ unsigned int nr;
+ struct klp_stack_range range[] __counted_by(nr);
+};
+
+static struct klp_stack_ranges __rcu *klp_stack_ranges;
+
/*
* When a livepatch is in progress, enable klp stack checking in
* schedule(). This helps CPU-bound kthreads get patched.
@@ -145,6 +174,8 @@ static void klp_complete_transition(void)
klp_target_state = KLP_TRANSITION_IDLE;
klp_transition_patch = NULL;
+
+ klp_update_stack_ranges();
}
/*
@@ -199,23 +230,22 @@ void klp_update_patch_state(struct task_struct *task)
}
/*
- * Determine whether the given stack trace includes any references to a
- * to-be-patched or to-be-unpatched function.
+ * Get the code range of a to-be-patched or to-be-unpatched function, which a
+ * task must not be running or sleeping in to switch to klp_target_state.
*/
-static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
- unsigned int nr_entries)
+static void klp_func_stack_range(struct klp_func *func,
+ unsigned long *func_addr,
+ unsigned long *func_size)
{
- unsigned long func_addr, func_size, address;
struct klp_ops *ops;
- int i;
if (klp_target_state == KLP_TRANSITION_UNPATCHED) {
/*
* Check for the to-be-unpatched function
* (the func itself).
*/
- func_addr = (unsigned long)func->new_func;
- func_size = func->new_size;
+ *func_addr = (unsigned long)func->new_func;
+ *func_size = func->new_size;
} else {
/*
* Check for the to-be-patched function
@@ -225,17 +255,109 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
if (list_is_singular(&ops->func_stack)) {
/* original function */
- func_addr = (unsigned long)func->old_func;
- func_size = func->old_size;
+ *func_addr = (unsigned long)func->old_func;
+ *func_size = func->old_size;
} else {
/* previously patched function */
struct klp_func *prev;
prev = list_next_entry(func, stack_node);
- func_addr = (unsigned long)prev->new_func;
- func_size = prev->new_size;
+ *func_addr = (unsigned long)prev->new_func;
+ *func_size = prev->new_size;
+ }
+ }
+}
+
+static int klp_stack_range_cmp(const void *a, const void *b)
+{
+ const struct klp_stack_range *ra = a, *rb = b;
+
+ if (ra->start < rb->start)
+ return -1;
+ return ra->start > rb->start;
+}
+
+/*
+ * Rebuild the sorted range table for the patched functions of
+ * klp_transition_patch, or clear it if there is no transition. Must be called
+ * whenever the set of patched functions or klp_target_state changes while a
+ * transition is in progress. If the table can't be built, it's cleared and
+ * klp_check_stack() falls back to checking each function.
+ */
+void klp_update_stack_ranges(void)
+{
+ struct klp_stack_ranges *new = NULL, *old;
+ struct klp_object *obj;
+ struct klp_func *func;
+ unsigned int nr = 0, i = 0;
+
+ lockdep_assert_held(&klp_mutex);
+
+ if (!klp_transition_patch)
+ goto publish;
+
+ klp_for_each_object(klp_transition_patch, obj) {
+ if (!obj->patched)
+ continue;
+ klp_for_each_func(obj, func)
+ nr++;
+ }
+
+ new = kvmalloc(struct_size(new, range, nr), GFP_KERNEL);
+ if (!new)
+ goto publish;
+ new->nr = nr;
+
+ klp_for_each_object(klp_transition_patch, obj) {
+ if (!obj->patched)
+ continue;
+ klp_for_each_func(obj, func) {
+ unsigned long func_addr, func_size;
+
+ klp_func_stack_range(func, &func_addr, &func_size);
+ if (!func_size)
+ continue;
+ new->range[i].start = func_addr;
+ new->range[i].end = func_addr + func_size;
+ new->range[i].name = func->old_name;
+ i++;
}
}
+ new->nr = i;
+
+ sort(new->range, new->nr, sizeof(new->range[0]), klp_stack_range_cmp,
+ NULL);
+
+ /* klp_stack_range_match() needs disjoint ranges. */
+ for (i = 1; i < new->nr; i++) {
+ if (new->range[i].start < new->range[i - 1].end) {
+ pr_debug("'%s': overlapping functions '%s' and '%s', not using range table\n",
+ klp_transition_patch->mod->name,
+ new->range[i - 1].name, new->range[i].name);
+ kvfree(new);
+ new = NULL;
+ goto publish;
+ }
+ }
+
+publish:
+ old = rcu_replace_pointer(klp_stack_ranges, new,
+ lockdep_is_held(&klp_mutex));
+ if (old)
+ kvfree_rcu(old, rcu);
+}
+
+/*
+ * Determine whether the given stack trace includes any references to a
+ * to-be-patched or to-be-unpatched function.
+ */
+static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
+ unsigned int nr_entries)
+{
+ unsigned long func_addr, func_size, address;
+ int i;
+
+ klp_func_stack_range(func, &func_addr, &func_size);
for (i = 0; i < nr_entries; i++) {
address = entries[i];
@@ -247,6 +369,37 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
return 0;
}
+/* bsearch() comparator: is the address before, inside or after the range? */
+static int klp_stack_range_match(const void *key, const void *elt)
+{
+ unsigned long address = *(const unsigned long *)key;
+ const struct klp_stack_range *range = elt;
+
+ if (address < range->start)
+ return -1;
+ return address >= range->end;
+}
+
+/* Look up each stack entry in the sorted, disjoint range table. */
+static int klp_check_stack_ranges(const struct klp_stack_ranges *ranges,
+ const unsigned long *entries,
+ unsigned int nr_entries, const char **oldname)
+{
+ const struct klp_stack_range *range;
+ unsigned int i;
+
+ for (i = 0; i < nr_entries; i++) {
+ range = bsearch(&entries[i], ranges->range, ranges->nr,
+ sizeof(ranges->range[0]), klp_stack_range_match);
+ if (range) {
+ *oldname = range->name;
+ return -EADDRINUSE;
+ }
+ }
+
+ return 0;
+}
+
/*
* Determine whether it's safe to transition the task to the target patch state
* by looking for any to-be-patched or to-be-unpatched functions on its stack.
@@ -254,6 +407,7 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
static int klp_check_stack(struct task_struct *task, const char **oldname)
{
unsigned long *entries = this_cpu_ptr(klp_stack_entries);
+ const struct klp_stack_ranges *ranges;
struct klp_object *obj;
struct klp_func *func;
int ret, nr_entries;
@@ -266,6 +420,11 @@ static int klp_check_stack(struct task_struct *task, const char **oldname)
return -EINVAL;
nr_entries = ret;
+ ranges = rcu_dereference_sched(klp_stack_ranges);
+ if (ranges)
+ return klp_check_stack_ranges(ranges, entries, nr_entries,
+ oldname);
+
klp_for_each_object(klp_transition_patch, obj) {
if (!obj->patched)
continue;
@@ -517,6 +676,13 @@ void klp_start_transition(void)
klp_transition_patch->mod->name,
klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
+ /*
+ * Build the range table for klp_check_stack() before any task can be
+ * marked TIF_PATCH_PENDING and check its stack. This also covers a
+ * reversed transition, whose target state just changed.
+ */
+ klp_update_stack_ranges();
+
/*
* Mark all normal tasks as needing a patch state update. They'll
* switch either in klp_try_complete_transition() or as they exit the
diff --git a/kernel/livepatch/transition.h b/kernel/livepatch/transition.h
index 322db16233de..49f8fd44c9bc 100644
--- a/kernel/livepatch/transition.h
+++ b/kernel/livepatch/transition.h
@@ -9,6 +9,7 @@ extern struct klp_patch *klp_transition_patch;
void klp_init_transition(struct klp_patch *patch, int state);
void klp_cancel_transition(void);
void klp_start_transition(void);
+void klp_update_stack_ranges(void);
void klp_try_complete_transition(void);
void klp_reverse_transition(void);
void klp_force_transition(void);
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 0/3] livepatch: make the transition stack check scale with patch size
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 ` [PATCH 1/2] livepatch: look up klp_ops in a hash table Zhu, Yi
2026-12-04 0:35 ` [PATCH 2/2] livepatch: check task stacks against a sorted range table Zhu, Yi
@ 2026-12-04 4:51 ` Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 1/3] livepatch: fix racy klp_ops lookup in klp_check_stack_func() Zhu, Yi
` (2 more replies)
2 siblings, 3 replies; 7+ messages in thread
From: Zhu, Yi @ 2026-12-04 4:51 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Wilson Felipe Pereira, Dylan Hatch
Cc: live-patching, linux-kernel
During a patching transition, every task's stack is checked against
every patched function. Two things make that expensive for large
cumulative livepatches:
- klp_check_stack_func() calls klp_find_ops(), a walk of a global list
with one entry per patched function, for every patched function.
The per-task check is therefore O(N^2) in the number of patched
functions N.
- Even without that lookup, each task repeats the same O(N * D) scan
(D = stack depth), although the forbidden address ranges are the
same for all tasks.
The first transition pass runs under read_lock(&tasklist_lock), so on
large patches this stalls fork() and exit() machine-wide for seconds to
minutes.
Patch 1 fixes an existing race: since klp_check_stack() can run from
__klp_sched_try_switch() without klp_mutex, its klp_ops lookup can race
with klp_module_coming() and klp_module_going() patching and unpatching
an object of the transition patch, and dereference a NULL or freed
klp_ops or an emptied func_stack. It makes klp_ops RCU-managed and the
lookup tolerate a missing klp_ops. Patches 2 and 3 build on it.
Patch 2 keys klp_ops on the patched function in an RCU hash table.
Patch 3 builds a sorted, RCU-published table of the patched address
ranges once per transition state change, and looks up each stack entry
with bsearch(). The per-task cost drops to O(D log N). If the table
can't be built, the existing per-function check is used.
Testing, on livepatching/for-next (2c3787447317) with and without the
series, in a 32-vCPU QEMU/KVM guest (Intel Xeon @ 2.60GHz host,
CONFIG_PREEMPT_LAZY):
- livepatch kselftests, with lockdep, PROVE_RCU and DEBUG_LIST: 39
passed, 4 skipped (legacy replace attribute not present), 0 failed on
both kernels; no lockdep, RCU or WARN splats.
- With the same debug options, 300 load/unload cycles of a target module
while a transition is held pending by test_klp_callbacks_busy, with 32
extra tasks scheduling continuously: no splats.
- A synthetic target module with N noinline functions and a livepatch
replacing all of them; idle threads added to reach the task counts
below (v1 numbers; a v2 rerun at 1,000, 5,000 and 10,000 functions
matched them). The first klp_try_complete_transition() pass, timed
with a kprobe, and the time from insmod until the transition
completes:
first pass insmod -> patched
N tasks base patched base patched
100 ~1.9k 19.6 ms 1.7 ms 1.9 s 1.9 s
1,000 ~1.9k 3.41 s 1.9 ms 4.9 s 2.9 s
2,000 ~1.9k 14.5 s 1.7 ms 17.9 s 4.8 s
3,000 ~1.9k 30.7 s 2.2 ms 36.1 s 6.9 s
5,000 ~1.9k 156 s 1.8 ms 167 s 11.8 s
10,000 ~1.9k 1,069 s 1.8 ms 1,096 s 27.2 s
2,000 ~3.4k 25.9 s 3.0 ms 29.3 s 5.0 s
2,000 ~50.4k - 35.5 ms - 5.5 s
10,000 ~50.4k - 37.5 ms - 27.8 s
On base, the first pass triggers soft lockups from 3,000 functions
on; with ~3.4k tasks, udev-worker tasks on other CPUs report soft
lockups too. The patched kernel shows no soft lockups or RCU stalls
in any run. Its remaining insmod time is module loading, symbol
resolution and ftrace registration, which this series does not
change.
Disabling a large patch has a separate cost: two synchronize_rcu()
calls per function in the ftrace unregister path. That will be
addressed separately.
Changes in v2:
- New patch 1, fixing the klp_ops lookup race above. It predates this
series, but v1 patch 1 widened the window by unpublishing a klp_ops
before its sleeping ftrace unregistration (Sashiko).
- Patch 2: keep the unregister-then-unpublish order; the RCU conversion
and old_func now come from patch 1.
- Patch 3: add smp_wmb() between the range table update and the
TIF_PATCH_PENDING writes, for reversed transitions (Sashiko).
- Patch 3: rebuild the table on module load/unload only if the
transition patch applies to the module, and not on the
klp_module_coming() error path, where it never changed (Sashiko).
- Patch 3: use rcu_dereference_check() with klp_mutex or a sched RCU
read-side section as the condition (Sashiko).
- Patch 1: set obj->patched with smp_store_release() and read it with
smp_load_acquire() in the stack check (Sashiko).
- Patch 1: mark the lockless func->old_func/new_func accesses against
klp_free_object_loaded() with READ_ONCE()/WRITE_ONCE(), and use the
immutable ops->old_func after the lookup.
v1: https://lore.kernel.org/all/cover.1796344045.git.chuyee@google.com/
Zhu, Yi (3):
livepatch: fix racy klp_ops lookup in klp_check_stack_func()
livepatch: look up klp_ops in a hash table
livepatch: check task stacks against a sorted range table
kernel/livepatch/core.c | 29 +++-
kernel/livepatch/patch.c | 47 +++++--
kernel/livepatch/patch.h | 8 +-
kernel/livepatch/transition.c | 240 +++++++++++++++++++++++++++++++---
kernel/livepatch/transition.h | 1 +
5 files changed, 288 insertions(+), 37 deletions(-)
base-commit: 2c37874473176739f4107c70739a930501d619c8
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 1/3] livepatch: fix racy klp_ops lookup in klp_check_stack_func()
2026-12-04 4:51 ` [PATCH v2 0/3] livepatch: make the transition stack check scale with patch size Zhu, Yi
@ 2026-12-04 4:51 ` Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 2/3] livepatch: look up klp_ops in a hash table Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 3/3] livepatch: check task stacks against a sorted range table Zhu, Yi
2 siblings, 0 replies; 7+ messages in thread
From: Zhu, Yi @ 2026-12-04 4:51 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Wilson Felipe Pereira, Dylan Hatch
Cc: live-patching, linux-kernel
Since commit e3ff7c609f39 ("livepatch,sched: Add livepatch task switching
to cond_resched()"), klp_check_stack() can run from
__klp_sched_try_switch() with only preemption disabled and without
klp_mutex. klp_module_coming() and klp_module_going() can patch and
unpatch an object of the transition patch at the same time, under
klp_mutex, and the stack check's klp_find_ops() lookup isn't safe
against that:
- klp_find_ops() walks the global klp_ops list with plain list
primitives while klp_patch_func() adds to it and klp_unpatch_func()
removes from it and frees the klp_ops with kfree() right away.
- klp_unpatch_object() only clears obj->patched after all funcs are
unpatched, and unregister_ftrace_function() sleeps for each of them.
Meanwhile the stack check still visits the object, and
klp_find_ops() returns NULL for funcs that are already unpatched,
which klp_check_stack_func() dereferences.
- Removing the last func from a func_stack leaves it empty, so both
klp_find_ops() and the list_is_singular()/list_next_entry() logic in
klp_check_stack_func() can treat the list head as a klp_func.
- klp_patch_object() sets obj->patched without ordering it after the
func_stack and klp_ops updates.
Store old_func in struct klp_ops and match on it, so a lookup doesn't
depend on the func_stack contents. Add and remove klp_ops with the RCU
list primitives, publish one only once its ftrace ops is registered,
unpublish it before emptying its func_stack and free it with
kfree_rcu(). Readers already run with preemption disabled.
In klp_check_stack_func(), skip a func whose klp_ops is gone, and find
the previous function through func->stack_node.next, which
list_del_rcu() leaves intact. Such a func belongs to a module that is
coming or going, so none of its code can be on a stack, and the core
doesn't check stacks when it patches or unpatches such an object anyway.
Set obj->patched with smp_store_release() and read it with
smp_load_acquire() in klp_check_stack(). klp_free_object_loaded() also
clears func->old_func and, for NOPs, func->new_func right after
unpatching, so mark those accesses with WRITE_ONCE()/READ_ONCE() and use
the immutable ops->old_func once the lookup succeeds.
Fixes: e3ff7c609f39 ("livepatch,sched: Add livepatch task switching to cond_resched()")
Signed-off-by: "Zhu, Yi" <chuyee@google.com>
---
kernel/livepatch/core.c | 5 ++--
kernel/livepatch/patch.c | 43 ++++++++++++++++++++++++-----------
kernel/livepatch/patch.h | 4 ++++
kernel/livepatch/transition.c | 37 ++++++++++++++++++++++++------
4 files changed, 67 insertions(+), 22 deletions(-)
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index b83715272e8c..92fbb524165a 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -724,11 +724,12 @@ static void klp_free_object_loaded(struct klp_object *obj)
obj->mod = NULL;
+ /* klp_check_stack() may still read these locklessly. */
klp_for_each_func(obj, func) {
- func->old_func = NULL;
+ WRITE_ONCE(func->old_func, NULL);
if (func->nop)
- func->new_func = NULL;
+ WRITE_ONCE(func->new_func, NULL);
}
}
diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c
index 3f54a017bbf6..7330a189d6d0 100644
--- a/kernel/livepatch/patch.c
+++ b/kernel/livepatch/patch.c
@@ -20,17 +20,21 @@
#include "patch.h"
#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.
+ */
static LIST_HEAD(klp_ops);
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)
+ list_for_each_entry_rcu(ops, &klp_ops, node,
+ lockdep_is_held(&klp_mutex)) {
+ if (ops->old_func == old_func)
return ops;
}
@@ -147,9 +151,14 @@ static void klp_unpatch_func(struct klp_func *func)
WARN_ON(unregister_ftrace_function(&ops->fops));
WARN_ON(ftrace_set_filter_ip(&ops->fops, ftrace_loc, 1, 0));
+ /*
+ * Unpublish ops before emptying its func_stack. Lockless
+ * klp_find_ops() readers may still see it until a grace
+ * period has elapsed, hence kfree_rcu().
+ */
+ list_del_rcu(&ops->node);
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 +200,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 +219,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.
+ */
+ list_add_rcu(&ops->node, &klp_ops);
} else {
list_add_rcu(&func->stack_node, &ops->func_stack);
}
@@ -222,7 +235,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;
}
@@ -240,7 +252,7 @@ static void __klp_unpatch_object(struct klp_object *obj, bool nops_only)
}
if (obj->dynamic || !nops_only)
- obj->patched = false;
+ WRITE_ONCE(obj->patched, false);
}
@@ -264,7 +276,12 @@ int klp_patch_object(struct klp_object *obj)
return ret;
}
}
- obj->patched = true;
+
+ /*
+ * Publish the object to klp_check_stack() only after all of its funcs
+ * are on their func_stacks. Pairs with smp_load_acquire() there.
+ */
+ smp_store_release(&obj->patched, true);
return 0;
}
diff --git a/kernel/livepatch/patch.h b/kernel/livepatch/patch.h
index d5f2fbe373e0..bf3b9017910a 100644
--- a/kernel/livepatch/patch.h
+++ b/kernel/livepatch/patch.h
@@ -16,13 +16,17 @@
* func_stack (front of the list).
*
* @node: node for the global klp_ops list
+ * @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;
+ 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);
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index ef70ff582da1..07e786c2d812 100644
--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -214,25 +214,47 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
* Check for the to-be-unpatched function
* (the func itself).
*/
- func_addr = (unsigned long)func->new_func;
+ func_addr = (unsigned long)READ_ONCE(func->new_func);
func_size = func->new_size;
} else {
+ struct list_head *next;
+
/*
* Check for the to-be-patched function
* (the previous func).
*/
- ops = klp_find_ops(func->old_func);
+ ops = klp_find_ops(READ_ONCE(func->old_func));
+
+ /*
+ * klp_module_coming() and klp_module_going() can patch and
+ * unpatch an object of the transition patch while this runs
+ * from __klp_sched_try_switch() without klp_mutex. Then ops
+ * may already be gone, or func may already be off its
+ * func_stack. The module's code can't be on any stack at that
+ * point, and the core doesn't check stacks when it patches or
+ * unpatches such an object anyway, so there is nothing to check.
+ * For the same reason, func->old_func and func->new_func may be
+ * cleared by klp_free_object_loaded() meanwhile; use the
+ * immutable ops->old_func once ops is found.
+ *
+ * list_del_rcu() leaves func->stack_node.next pointing to the
+ * previous func or to the func_stack head, so use it rather than
+ * list_is_singular(), which an emptied func_stack would defeat.
+ */
+ if (!ops)
+ return 0;
- if (list_is_singular(&ops->func_stack)) {
+ next = READ_ONCE(func->stack_node.next);
+ if (next == &ops->func_stack) {
/* original function */
- func_addr = (unsigned long)func->old_func;
+ func_addr = (unsigned long)ops->old_func;
func_size = func->old_size;
} else {
/* previously patched function */
struct klp_func *prev;
- prev = list_next_entry(func, stack_node);
- func_addr = (unsigned long)prev->new_func;
+ prev = list_entry(next, struct klp_func, stack_node);
+ func_addr = (unsigned long)READ_ONCE(prev->new_func);
func_size = prev->new_size;
}
}
@@ -267,7 +289,8 @@ static int klp_check_stack(struct task_struct *task, const char **oldname)
nr_entries = ret;
klp_for_each_object(klp_transition_patch, obj) {
- if (!obj->patched)
+ /* Pairs with smp_store_release() in klp_patch_object(). */
+ if (!smp_load_acquire(&obj->patched))
continue;
klp_for_each_func(obj, func) {
ret = klp_check_stack_func(func, entries, nr_entries);
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 2/3] livepatch: look up klp_ops in a hash table
2026-12-04 4:51 ` [PATCH v2 0/3] livepatch: make the transition stack check scale with patch size Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 1/3] livepatch: fix racy klp_ops lookup in klp_check_stack_func() Zhu, Yi
@ 2026-12-04 4:51 ` Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 3/3] livepatch: check task stacks against a sorted range table Zhu, Yi
2 siblings, 0 replies; 7+ messages in thread
From: Zhu, Yi @ 2026-12-04 4:51 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Wilson Felipe Pereira, Dylan Hatch
Cc: live-patching, linux-kernel
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@google.com>
---
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
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 3/3] livepatch: check task stacks against a sorted range table
2026-12-04 4:51 ` [PATCH v2 0/3] livepatch: make the transition stack check scale with patch size Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 1/3] livepatch: fix racy klp_ops lookup in klp_check_stack_func() Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 2/3] livepatch: look up klp_ops in a hash table Zhu, Yi
@ 2026-12-04 4:51 ` Zhu, Yi
2 siblings, 0 replies; 7+ messages in thread
From: Zhu, Yi @ 2026-12-04 4:51 UTC (permalink / raw)
To: Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Wilson Felipe Pereira, Dylan Hatch
Cc: live-patching, linux-kernel
During a transition, klp_check_stack() computes, for every task and every
patched function, the code range that must not be on the task's stack, and
scans the whole stack for it. The ranges are the same for every task, so
this repeats the same work T times and costs O(N * D) per task even without
the old O(N) klp_find_ops() walk. With thousands of patched functions and
thousands of tasks, the first transition pass runs for seconds to minutes
under read_lock(&tasklist_lock), which stalls fork() and exit()
machine-wide.
Build a table of those ranges, sorted by start address, and look up each
stack entry with bsearch(), making the per-task cost O(D log N).
klp_check_stack() also runs from __klp_sched_try_switch() on any CPU
without klp_mutex, so the table is never changed in place. A new table is
built under klp_mutex whenever the patched functions or the target state
change while a transition is in progress: when the transition starts
(including a reversed one), when a module that the transition patch
applies to is loaded or unloaded, and when it completes. It's published
with RCU and the old one is freed with kvfree_rcu(). Readers already run
with preemption disabled. A write barrier orders the update before the
TIF_PATCH_PENDING writes, pairing with the read barrier in
__klp_sched_try_switch(). If the table can't be allocated, or two ranges
overlap, the existing per-function check is used.
The table may briefly be stale around module load and unload. A module
that is being loaded has not run yet, so no task can be in one of its
functions; a module that is going away only leaves extra ranges behind,
which makes the check more conservative.
Signed-off-by: "Zhu, Yi" <chuyee@google.com>
---
kernel/livepatch/core.c | 24 ++++
kernel/livepatch/transition.c | 213 +++++++++++++++++++++++++++++++---
kernel/livepatch/transition.h | 1 +
3 files changed, 221 insertions(+), 17 deletions(-)
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 92fbb524165a..1246ad28526e 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -1305,6 +1305,22 @@ static void klp_cleanup_module_patches_limited(struct module *mod,
}
}
+/* Does the transition patch, if any, patch the given module? */
+static bool klp_transition_patch_has_module(struct module *mod)
+{
+ struct klp_object *obj;
+
+ if (!klp_transition_patch)
+ return false;
+
+ klp_for_each_object(klp_transition_patch, obj) {
+ if (klp_is_module(obj) && !strcmp(obj->name, mod->name))
+ return true;
+ }
+
+ return false;
+}
+
int klp_module_coming(struct module *mod)
{
int ret;
@@ -1367,6 +1383,10 @@ int klp_module_coming(struct module *mod)
}
}
+ /* The transition patch may now patch functions in this module. */
+ if (klp_transition_patch_has_module(mod))
+ klp_update_stack_ranges();
+
mutex_unlock(&klp_mutex);
return 0;
@@ -1402,6 +1422,10 @@ void klp_module_going(struct module *mod)
klp_cleanup_module_patches_limited(mod, NULL);
+ /* Drop this module's functions from the transition range table. */
+ if (klp_transition_patch_has_module(mod))
+ klp_update_stack_ranges();
+
mutex_unlock(&klp_mutex);
}
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index 07e786c2d812..6756d5ccdea8 100644
--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -7,7 +7,11 @@
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+#include <linux/bsearch.h>
#include <linux/cpu.h>
+#include <linux/rcupdate.h>
+#include <linux/slab.h>
+#include <linux/sort.h>
#include <linux/stacktrace.h>
#include <linux/static_call.h>
#include "core.h"
@@ -27,6 +31,31 @@ static int klp_target_state = KLP_TRANSITION_IDLE;
static unsigned int klp_signals_cnt;
+/*
+ * The code ranges which must not be on a task's stack for the task to switch
+ * to klp_target_state, one per patched function of klp_transition_patch,
+ * sorted by start address so that each stack entry needs one bsearch().
+ *
+ * klp_check_stack() reads the table with preemption disabled, both from
+ * transition passes (under klp_mutex) and from __klp_sched_try_switch()
+ * (without klp_mutex). So the table is never modified in place: a new one is
+ * built under klp_mutex whenever the set of patched functions or the target
+ * state changes, and published with RCU. A NULL table means the slow path.
+ */
+struct klp_stack_range {
+ unsigned long start;
+ unsigned long end;
+ const char *name;
+};
+
+struct klp_stack_ranges {
+ struct rcu_head rcu;
+ unsigned int nr;
+ struct klp_stack_range range[] __counted_by(nr);
+};
+
+static struct klp_stack_ranges __rcu *klp_stack_ranges;
+
/*
* When a livepatch is in progress, enable klp stack checking in
* schedule(). This helps CPU-bound kthreads get patched.
@@ -145,6 +174,8 @@ static void klp_complete_transition(void)
klp_target_state = KLP_TRANSITION_IDLE;
klp_transition_patch = NULL;
+
+ klp_update_stack_ranges();
}
/*
@@ -199,23 +230,23 @@ void klp_update_patch_state(struct task_struct *task)
}
/*
- * Determine whether the given stack trace includes any references to a
- * to-be-patched or to-be-unpatched function.
+ * Get the code range of a to-be-patched or to-be-unpatched function, which a
+ * task must not be running or sleeping in to switch to klp_target_state. A
+ * size of zero means there is nothing to check.
*/
-static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
- unsigned int nr_entries)
+static void klp_func_stack_range(struct klp_func *func,
+ unsigned long *func_addr,
+ unsigned long *func_size)
{
- unsigned long func_addr, func_size, address;
struct klp_ops *ops;
- int i;
if (klp_target_state == KLP_TRANSITION_UNPATCHED) {
/*
* Check for the to-be-unpatched function
* (the func itself).
*/
- func_addr = (unsigned long)READ_ONCE(func->new_func);
- func_size = func->new_size;
+ *func_addr = (unsigned long)READ_ONCE(func->new_func);
+ *func_size = func->new_size;
} else {
struct list_head *next;
@@ -241,23 +272,118 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
* previous func or to the func_stack head, so use it rather than
* list_is_singular(), which an emptied func_stack would defeat.
*/
- if (!ops)
- return 0;
+ if (!ops) {
+ *func_addr = 0;
+ *func_size = 0;
+ return;
+ }
next = READ_ONCE(func->stack_node.next);
if (next == &ops->func_stack) {
/* original function */
- func_addr = (unsigned long)ops->old_func;
- func_size = func->old_size;
+ *func_addr = (unsigned long)ops->old_func;
+ *func_size = func->old_size;
} else {
/* previously patched function */
struct klp_func *prev;
prev = list_entry(next, struct klp_func, stack_node);
- func_addr = (unsigned long)READ_ONCE(prev->new_func);
- func_size = prev->new_size;
+ *func_addr = (unsigned long)READ_ONCE(prev->new_func);
+ *func_size = prev->new_size;
}
}
+}
+
+static int klp_stack_range_cmp(const void *a, const void *b)
+{
+ const struct klp_stack_range *ra = a, *rb = b;
+
+ if (ra->start < rb->start)
+ return -1;
+ return ra->start > rb->start;
+}
+
+/*
+ * Rebuild the sorted range table for the patched functions of
+ * klp_transition_patch, or clear it if there is no transition. Must be called
+ * whenever the set of patched functions or klp_target_state changes while a
+ * transition is in progress. If the table can't be built, it's cleared and
+ * klp_check_stack() falls back to checking each function.
+ */
+void klp_update_stack_ranges(void)
+{
+ struct klp_stack_ranges *new = NULL, *old;
+ struct klp_object *obj;
+ struct klp_func *func;
+ unsigned int nr = 0, i = 0;
+
+ lockdep_assert_held(&klp_mutex);
+
+ if (!klp_transition_patch)
+ goto publish;
+
+ klp_for_each_object(klp_transition_patch, obj) {
+ if (!obj->patched)
+ continue;
+ klp_for_each_func(obj, func)
+ nr++;
+ }
+
+ new = kvmalloc(struct_size(new, range, nr), GFP_KERNEL);
+ if (!new)
+ goto publish;
+ new->nr = nr;
+
+ klp_for_each_object(klp_transition_patch, obj) {
+ if (!obj->patched)
+ continue;
+ klp_for_each_func(obj, func) {
+ unsigned long func_addr, func_size;
+
+ klp_func_stack_range(func, &func_addr, &func_size);
+ if (!func_size)
+ continue;
+ new->range[i].start = func_addr;
+ new->range[i].end = func_addr + func_size;
+ new->range[i].name = func->old_name;
+ i++;
+ }
+ }
+ new->nr = i;
+
+ sort(new->range, new->nr, sizeof(new->range[0]), klp_stack_range_cmp,
+ NULL);
+
+ /* klp_stack_range_match() needs disjoint ranges. */
+ for (i = 1; i < new->nr; i++) {
+ if (new->range[i].start < new->range[i - 1].end) {
+ pr_debug("'%s': overlapping functions '%s' and '%s', not using range table\n",
+ klp_transition_patch->mod->name,
+ new->range[i - 1].name, new->range[i].name);
+ kvfree(new);
+ new = NULL;
+ goto publish;
+ }
+ }
+
+publish:
+ old = rcu_replace_pointer(klp_stack_ranges, new,
+ lockdep_is_held(&klp_mutex));
+ if (old)
+ kvfree_rcu(old, rcu);
+}
+
+/*
+ * Determine whether the given stack trace includes any references to a
+ * to-be-patched or to-be-unpatched function.
+ */
+static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
+ unsigned int nr_entries)
+{
+ unsigned long func_addr, func_size, address;
+ int i;
+
+ klp_func_stack_range(func, &func_addr, &func_size);
for (i = 0; i < nr_entries; i++) {
address = entries[i];
@@ -269,6 +395,37 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
return 0;
}
+/* bsearch() comparator: is the address before, inside or after the range? */
+static int klp_stack_range_match(const void *key, const void *elt)
+{
+ unsigned long address = *(const unsigned long *)key;
+ const struct klp_stack_range *range = elt;
+
+ if (address < range->start)
+ return -1;
+ return address >= range->end;
+}
+
+/* Look up each stack entry in the sorted, disjoint range table. */
+static int klp_check_stack_ranges(const struct klp_stack_ranges *ranges,
+ const unsigned long *entries,
+ unsigned int nr_entries, const char **oldname)
+{
+ const struct klp_stack_range *range;
+ unsigned int i;
+
+ for (i = 0; i < nr_entries; i++) {
+ range = bsearch(&entries[i], ranges->range, ranges->nr,
+ sizeof(ranges->range[0]), klp_stack_range_match);
+ if (range) {
+ *oldname = range->name;
+ return -EADDRINUSE;
+ }
+ }
+
+ return 0;
+}
+
/*
* Determine whether it's safe to transition the task to the target patch state
* by looking for any to-be-patched or to-be-unpatched functions on its stack.
@@ -276,6 +433,7 @@ static int klp_check_stack_func(struct klp_func *func, unsigned long *entries,
static int klp_check_stack(struct task_struct *task, const char **oldname)
{
unsigned long *entries = this_cpu_ptr(klp_stack_entries);
+ const struct klp_stack_ranges *ranges;
struct klp_object *obj;
struct klp_func *func;
int ret, nr_entries;
@@ -288,6 +446,13 @@ static int klp_check_stack(struct task_struct *task, const char **oldname)
return -EINVAL;
nr_entries = ret;
+ ranges = rcu_dereference_check(klp_stack_ranges,
+ lockdep_is_held(&klp_mutex) ||
+ rcu_read_lock_sched_held());
+ if (ranges)
+ return klp_check_stack_ranges(ranges, entries, nr_entries,
+ oldname);
+
klp_for_each_object(klp_transition_patch, obj) {
/* Pairs with smp_store_release() in klp_patch_object(). */
if (!smp_load_acquire(&obj->patched))
@@ -394,9 +559,9 @@ void __klp_sched_try_switch(void)
/*
* Enforce the order of the TIF_PATCH_PENDING read above and the
- * klp_target_state read in klp_try_switch_task(). The corresponding
- * write barriers are in klp_init_transition() and
- * klp_reverse_transition().
+ * klp_target_state and klp_stack_ranges reads in klp_try_switch_task().
+ * The corresponding write barriers are in klp_init_transition(),
+ * klp_reverse_transition() and klp_start_transition().
*/
smp_rmb();
@@ -540,6 +705,20 @@ void klp_start_transition(void)
klp_transition_patch->mod->name,
klp_target_state == KLP_TRANSITION_PATCHED ? "patching" : "unpatching");
+ /*
+ * Build the range table for klp_check_stack() before any task can be
+ * marked TIF_PATCH_PENDING and check its stack. This also covers a
+ * reversed transition, whose target state just changed.
+ */
+ klp_update_stack_ranges();
+
+ /*
+ * Enforce the order of the table update and the TIF_PATCH_PENDING
+ * writes below, so that __klp_sched_try_switch() doesn't check a task
+ * against the table of the reversed transition.
+ */
+ smp_wmb();
+
/*
* Mark all normal tasks as needing a patch state update. They'll
* switch either in klp_try_complete_transition() or as they exit the
diff --git a/kernel/livepatch/transition.h b/kernel/livepatch/transition.h
index 322db16233de..49f8fd44c9bc 100644
--- a/kernel/livepatch/transition.h
+++ b/kernel/livepatch/transition.h
@@ -9,6 +9,7 @@ extern struct klp_patch *klp_transition_patch;
void klp_init_transition(struct klp_patch *patch, int state);
void klp_cancel_transition(void);
void klp_start_transition(void);
+void klp_update_stack_ranges(void);
void klp_try_complete_transition(void);
void klp_reverse_transition(void);
void klp_force_transition(void);
--
2.56.1.987.g2ec0ab4260-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-12-04 4:51 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH 1/2] livepatch: look up klp_ops in a hash table Zhu, Yi
2026-12-04 0:35 ` [PATCH 2/2] livepatch: check task stacks against a sorted range table Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 0/3] livepatch: make the transition stack check scale with patch size Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 1/3] livepatch: fix racy klp_ops lookup in klp_check_stack_func() Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 2/3] livepatch: look up klp_ops in a hash table Zhu, Yi
2026-12-04 4:51 ` [PATCH v2 3/3] livepatch: check task stacks against a sorted range table Zhu, Yi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®