* [PATCH v4 0/3] livepatch: Do not lose exiting tasks during a transition
@ 2026-11-26 16:41 Binoy Ravindran via B4 Relay
2026-11-26 16:41 ` [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-26 16:41 UTC (permalink / raw)
To: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
Joel Fernandes, Josh Triplett, Boqun Feng, Uladzislau Rezki,
Steven Rostedt, Mathieu Desnoyers, Lai Jiangshan, Zqiang,
Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Ingo Molnar, Jiri Kosina
Cc: rcu, linux-kernel, live-patching, Yafang Shao, Jaidev Shastri,
Binoy Ravindran
After exit_notify(), a self-reaping task is unhashed from the task list
but keeps running the rest of do_exit(). The livepatch transition walks
the task list, so it can complete while such a task still runs code from
a patch module that atomic replace is about to free. Yafang Shao and
Petr Mladek discussed this race in [1], and Josh Poimboeuf suggested a
similar approach in [2]. The race is hard to hit, but any livepatch that
replaces a function on the exit path can reach it, for example a fix to
do_exit() itself.
Every exiting task is on an RCU-tasks exit list from
exit_tasks_rcu_start(), before exit_notify(), until
exit_tasks_rcu_finish(). Patch 1 adds helpers to visit the tasks on
these lists. Patch 2 makes every transition step, including reverse and
force, visit them too. After exit_tasks_rcu_finish(), do_exit() runs
with preemption disabled until the task's final switch. Patch 2 waits
that stretch out with klp_synchronize_transition() before
func->transition is set and before the transition completes. The exit
path itself is unchanged. Patch 3, suggested by Yafang, makes LIVEPATCH
select TASKS_RCU, which every livepatch kernel already builds, so that
the exit lists are always there.
Tested on v7.4-rc2, x86-64, under QEMU/KVM, with two klp-build
livepatches that each replace do_exit() and replace each other ten times
per run, and a debug-only 2 s sleep after exit_notify(). Without the
series, 9 of 12 runs crashed; with it, all 12 completed their 120
replace cycles. With lockdep, PROVE_RCU and DEBUG_ATOMIC_SLEEP enabled,
the series ran 180 replace cycles, 120 of them with the sleep, with no
reports. Without the sleep, 20,000 replace cycles with the series, in
both the default and preempt=full modes, completed without a fault.
Transition times matched v7.4-rc2 alone: medians of 7 ms (default) and
11 ms (preempt=full) against 8 ms, and 90th and 99th percentiles of
about 1.6 s and 1.8 s in both. Each commit builds without new warnings
with and without CONFIG_LIVEPATCH. A reproducer and the logs are
available on request.
checkpatch.pl in v7.4-rc2 reports the Assisted-by: trailer as
non-standard. The trailer follows
Documentation/process/coding-assistants.rst.
[1] https://lore.kernel.org/all/20250127063526.76687-1-laoar.shao@gmail.com/
[2] https://lore.kernel.org/live-patching/20250124010848.pdcxokfsk2ngrowl@jpoimboe/
---
Changes in v4:
- Wait out the do_exit() tail with klp_synchronize_transition() again,
as in v2. synchronize_rcu() can return while the dying task still runs
traceable code in __schedule(), because the quiescent state is
reported before switch_to() (Sashiko). This also restores the v2
transition times.
- Keep klp_try_switch_exiting_tasks() from v3 (Yafang Shao).
- Link to v3: https://patch.msgid.link/20261125-klp-series-v3-v3-0-b1e0f79af331@vt.edu
Changes in v3:
- Wait out the do_exit() tail with synchronize_rcu() instead of
klp_synchronize_transition(), as rcu_tasks_postgp() does
(Yafang Shao). With preempt=full this raises the median transition
time in our test from 8 ms to 0.8 s; the 90th and 99th
percentiles stay about the same. See the test notes above.
- Replace the callback-based snapshot walk, which had a single user,
with klp_try_switch_exiting_tasks() (Yafang Shao).
- Link to v2: https://patch.msgid.link/20261124-klp-series-v3-v2-0-50d20829a337@vt.edu
Changes in v2:
- Walk the exit lists under tasklist_lock and skip tasks that are
still hashed, so that each task is visited exactly once (Sashiko).
- Call klp_synchronize_transition() after initializing the transition,
before func->transition is set, and before completing it, to wait
out tasks past exit_tasks_rcu_finish() (Sashiko).
- Document rtp_exit_cursor in the kernel-doc comment (Sashiko).
- Add patch 3, which makes LIVEPATCH select TASKS_RCU (Yafang Shao).
- Test on v7.4-rc2 instead of v6.12.1, including lockdep and
natural-timing runs.
- Link to v1: https://patch.msgid.link/20261123-klp-series-v3-v1-0-427c95811d9b@vt.edu
To: "Paul E. McKenney" <paulmck@kernel.org>
To: Frederic Weisbecker <frederic@kernel.org>
To: Neeraj Upadhyay <neeraj.upadhyay@kernel.org>
To: Joel Fernandes <joelagnelf@nvidia.com>
To: Josh Triplett <josh@joshtriplett.org>
To: Boqun Feng <boqun@kernel.org>
To: Uladzislau Rezki <urezki@gmail.com>
To: Steven Rostedt <rostedt@goodmis.org>
To: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
To: Lai Jiangshan <jiangshanlai@gmail.com>
To: Zqiang <qiang.zhang@linux.dev>
To: Josh Poimboeuf <jpoimboe@kernel.org>
To: Miroslav Benes <mbenes@suse.cz>
To: Petr Mladek <pmladek@suse.com>
To: Joe Lawrence <joe.lawrence@redhat.com>
To: Song Liu <song@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
To: Jiri Kosina <jikos@kernel.org>
Cc: rcu@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Cc: live-patching@vger.kernel.org
Cc: Yafang Shao <laoar.shao@gmail.com>
Cc: Jaidev Shastri <jaidevshastri@vt.edu>
---
Binoy Ravindran (3):
rcu-tasks: Add helpers to visit tasks on the exit lists
livepatch: Include exiting tasks in the transition
livepatch: Select TASKS_RCU
include/linux/rcupdate.h | 9 ++++
kernel/livepatch/Kconfig | 1 +
kernel/livepatch/transition.c | 102 ++++++++++++++++++++++++++++++++++++++++++
kernel/rcu/tasks.h | 69 +++++++++++++++++++++++++---
4 files changed, 175 insertions(+), 6 deletions(-)
---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261123-klp-series-v3-d52796c0590f
Best regards,
--
Binoy Ravindran <binoy@vt.edu>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists
2026-11-26 16:41 [PATCH v4 0/3] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
@ 2026-11-26 16:41 ` Binoy Ravindran via B4 Relay
2026-11-26 18:52 ` Petr Mladek
2026-11-26 16:41 ` [PATCH v4 2/3] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay
2026-11-26 16:41 ` [PATCH v4 3/3] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay
2 siblings, 1 reply; 5+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-26 16:41 UTC (permalink / raw)
To: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
Joel Fernandes, Josh Triplett, Boqun Feng, Uladzislau Rezki,
Steven Rostedt, Mathieu Desnoyers, Lai Jiangshan, Zqiang,
Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Ingo Molnar, Jiri Kosina
Cc: rcu, linux-kernel, live-patching, Yafang Shao, Jaidev Shastri,
Binoy Ravindran
From: Binoy Ravindran <binoy@vt.edu>
A task joins its CPU's RCU-tasks exit list in exit_tasks_rcu_start(),
before exit_notify() unhashes it from the task list, and leaves it in
exit_tasks_rcu_finish(), after do_exit() has disabled preemption for the
final switch. Once exit_notify() has unhashed it, the task is no longer
found by for_each_process_thread(), but it still runs do_exit() and may
sleep there. Code that must reach every task that can still be
descheduled, such as the livepatch transition, has no way to find it.
Add two helpers. rcu_tasks_visit_exiting() calls a function on each
task on the exit lists under the list locks, without allocating.
rcu_tasks_snapshot_exiting() copies the tasks into a caller-supplied
array with a reference held on each, for callers that must sleep or take
other locks while examining them. Without CONFIG_TASKS_RCU there are no
exit lists, and the helpers do nothing.
rcu_tasks_postscan() keeps its place in an exit list with a bare
list_head while it drops the lock to reschedule, relying on nobody else
traversing the list. Move that cursor into struct rcu_tasks_percpu,
next to the list it marks, so that the new helpers can recognize and
skip it. Only the grace-period kthread runs postscan, one CPU's list at
a time, so one cursor per CPU suffices.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
include/linux/rcupdate.h | 9 +++++++
kernel/rcu/tasks.h | 69 +++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 72 insertions(+), 6 deletions(-)
diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
index 3f74ae6d6e1f..55b8b4a0fa94 100644
--- a/include/linux/rcupdate.h
+++ b/include/linux/rcupdate.h
@@ -214,6 +214,15 @@ static inline void exit_tasks_rcu_start(void) { }
static inline void exit_tasks_rcu_finish(void) { }
#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
+typedef void (*rcu_tasks_exiting_fn_t)(struct task_struct *t, void *arg);
+#ifdef CONFIG_TASKS_RCU
+void rcu_tasks_visit_exiting(rcu_tasks_exiting_fn_t fn, void *arg);
+int rcu_tasks_snapshot_exiting(struct task_struct **buf, int max);
+#else
+static inline void rcu_tasks_visit_exiting(rcu_tasks_exiting_fn_t fn, void *arg) { }
+static inline int rcu_tasks_snapshot_exiting(struct task_struct **buf, int max) { return 0; }
+#endif
+
/**
* cond_resched_tasks_rcu_qs - Report potential quiescent states to RCU
*
diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index fcac7361ec51..b5fa5a3a8503 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -33,6 +33,8 @@ typedef void (*postgp_func_t)(struct rcu_tasks *rtp);
* @barrier_q_head: RCU callback for barrier operation.
* @rtp_blkd_tasks: List of tasks blocked as readers.
* @rtp_exit_list: List of tasks in the latter portion of do_exit().
+ * @rtp_exit_cursor: Placeholder that rcu_tasks_postscan() parks on @rtp_exit_list
+ * while it drops the lock, so that exit-list walkers can skip it.
* @cpu: CPU number corresponding to this entry.
* @index: Index of this CPU in rtpcp_array of the rcu_tasks structure.
* @rtpp: Pointer to the rcu_tasks structure.
@@ -49,6 +51,7 @@ struct rcu_tasks_percpu {
struct rcu_head barrier_q_head;
struct list_head rtp_blkd_tasks;
struct list_head rtp_exit_list;
+ struct list_head rtp_exit_cursor;
int cpu;
int index;
struct rcu_tasks *rtpp;
@@ -1068,7 +1071,6 @@ static void rcu_tasks_postscan(struct list_head *hop)
struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
struct task_struct *t;
struct task_struct *t1;
- struct list_head tmp;
raw_spin_lock_irq_rcu_node(rtpcp);
list_for_each_entry_safe(t, t1, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) {
@@ -1081,14 +1083,15 @@ static void rcu_tasks_postscan(struct list_head *hop)
continue;
// Keep our place in the list while pausing.
- // Nothing else traverses this list, so adding a
- // bare list_head is OK.
- list_add(&tmp, &t->rcu_tasks_exit_list);
+ // The cursor lives in rtpcp so that the exit-list
+ // visitors can recognize and skip it.
+ list_add(&rtpcp->rtp_exit_cursor, &t->rcu_tasks_exit_list);
raw_spin_unlock_irq_rcu_node(rtpcp);
cond_resched(); // For CONFIG_PREEMPT=n kernels
raw_spin_lock_irq_rcu_node(rtpcp);
- t1 = list_entry(tmp.next, struct task_struct, rcu_tasks_exit_list);
- list_del(&tmp);
+ t1 = list_entry(rtpcp->rtp_exit_cursor.next, struct task_struct,
+ rcu_tasks_exit_list);
+ list_del(&rtpcp->rtp_exit_cursor);
j = jiffies + 1;
}
raw_spin_unlock_irq_rcu_node(rtpcp);
@@ -1332,6 +1335,60 @@ void exit_tasks_rcu_finish(void)
raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
}
+/*
+ * Call fn on every task on the exit lists, in place, with the per-CPU list
+ * lock held and interrupts disabled. fn must not sleep. Does not allocate.
+ * These are the tasks that have passed exit_tasks_rcu_start() but not
+ * exit_tasks_rcu_finish(). Once exit_notify() has run they are no longer on
+ * the task list, but they still run do_exit() and may sleep there.
+ */
+void rcu_tasks_visit_exiting(rcu_tasks_exiting_fn_t fn, void *arg)
+{
+ int cpu;
+ unsigned long flags;
+ struct task_struct *t;
+
+ for_each_possible_cpu(cpu) {
+ struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
+
+ raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
+ list_for_each_entry(t, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) {
+ if (&t->rcu_tasks_exit_list == &rtpcp->rtp_exit_cursor)
+ continue;
+ fn(t, arg);
+ }
+ raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
+ }
+}
+
+/*
+ * Copy up to max tasks on the exit lists into buf, taking a reference on
+ * each. Returns the number of tasks on the lists, which may exceed max.
+ */
+int rcu_tasks_snapshot_exiting(struct task_struct **buf, int max)
+{
+ int cpu, n = 0;
+ unsigned long flags;
+ struct task_struct *t;
+
+ for_each_possible_cpu(cpu) {
+ struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
+
+ raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
+ list_for_each_entry(t, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) {
+ if (&t->rcu_tasks_exit_list == &rtpcp->rtp_exit_cursor)
+ continue;
+ if (n < max) {
+ get_task_struct(t);
+ buf[n] = t;
+ }
+ n++;
+ }
+ raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
+ }
+ return n;
+}
+
#else /* #ifdef CONFIG_TASKS_RCU */
void exit_tasks_rcu_start(void) { }
void exit_tasks_rcu_finish(void) { }
--
2.39.5
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 2/3] livepatch: Include exiting tasks in the transition
2026-11-26 16:41 [PATCH v4 0/3] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-11-26 16:41 ` [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay
@ 2026-11-26 16:41 ` Binoy Ravindran via B4 Relay
2026-11-26 16:41 ` [PATCH v4 3/3] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay
2 siblings, 0 replies; 5+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-26 16:41 UTC (permalink / raw)
To: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
Joel Fernandes, Josh Triplett, Boqun Feng, Uladzislau Rezki,
Steven Rostedt, Mathieu Desnoyers, Lai Jiangshan, Zqiang,
Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Ingo Molnar, Jiri Kosina
Cc: rcu, linux-kernel, live-patching, Yafang Shao, Jaidev Shastri,
Binoy Ravindran
From: Binoy Ravindran <binoy@vt.edu>
The transition walks tasks with for_each_process_thread(). A
self-reaping task is unhashed in exit_notify() but keeps running the rest
of do_exit(), so the walk never sees it. The transition can then
complete, and with atomic replace the previous patch module can be
freed, while that task is still executing, or about to return into, the
previous module's copy of a patched function. This is the race discussed
in the thread linked below.
An exiting task is on an RCU-tasks exit list from exit_tasks_rcu_start(),
before exit_notify(), until exit_tasks_rcu_finish(). Make every step of
a transition also visit the tasks on these lists, using the helpers from
the previous patch. klp_init_transition(), klp_start_transition(),
klp_complete_transition(), klp_reverse_transition() and
klp_force_transition() only set fields and flags. They walk the exit
lists in place, under tasklist_lock, and skip tasks that are still
hashed, so that each task is visited exactly once.
klp_try_complete_transition() may inspect a task's stack, so it takes a
snapshot with a reference on each task. If the snapshot cannot be
allocated, or more tasks start exiting than it has room for, the
transition is treated as incomplete and retried. An exiting task must
therefore switch, like any other task, before the transition can
complete.
After exit_tasks_rcu_finish() the task may be on neither list, but it
can still run patched code until its final switch. do_exit() disables
preemption before exit_tasks_rcu_finish(), so from then on the task
cannot sleep, be preempted or migrate, and the next context switch on
its CPU is its last. klp_synchronize_transition() returns only after
every online CPU has switched context, so it waits out every task in this
stretch. synchronize_rcu() is not enough here: __schedule() reports the
RCU quiescent state in rcu_note_context_switch(), before switch_to(), so
it can return while the task still runs traceable code in __schedule().
Call it in two places:
- In klp_init_transition(), after patch_state has been set on every
task and before func->transition is set. An exiting task that the
walks did not visit had left the exit lists before or during them,
so it is gone before any function enters the transition.
- In klp_try_complete_transition(), before klp_complete_transition().
A task that left the exit lists unswitched after the snapshot is gone
before func->transition is cleared and before the previous patch can
be freed.
Covering the initialization and completion steps matters as well:
otherwise an exiting task keeps a stale patch_state from an earlier
transition, which can equal the new target. Likewise, the reverse path
clears TIF_PATCH_PENDING on exiting tasks and the force path updates
their patch state, so neither leaves an exiting task behind.
klp_send_signals() is not extended: a task that has passed exit_notify()
never returns to user space, so a fake signal cannot help it switch.
The exit lists exist only with CONFIG_TASKS_RCU, which every kernel
that can enable LIVEPATCH builds today. A following patch makes
LIVEPATCH select it.
Tested on v7.4-rc2, x86-64, under QEMU/KVM. Two klp-build livepatches
that each replace do_exit() replace each other ten times per run while
self-reaping tasks exit continuously, with a debug-only msleep() after
exit_notify() to widen the window. Without this series, 9 of 12 runs
crashed executing freed module memory after the previous patch was
removed. With it, all 12 runs completed their 120 replace cycles
without a fault. Without the delay, both kernels completed 60 cycles
without a fault.
Fixes: d83a7cb375ee ("livepatch: change to a per-task consistency model")
Link: https://lore.kernel.org/all/20250127063526.76687-1-laoar.shao@gmail.com/
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
kernel/livepatch/transition.c | 102 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 102 insertions(+)
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index 2351a19ac2a9..2b13b0b04eeb 100644
--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -10,6 +10,7 @@
#include <linux/cpu.h>
#include <linux/stacktrace.h>
#include <linux/static_call.h>
+#include <linux/slab.h>
#include "core.h"
#include "patch.h"
#include "transition.h"
@@ -74,6 +75,54 @@ static void klp_synchronize_transition(void)
schedule_on_each_cpu(klp_sync);
}
+/*
+ * Tasks past exit_notify() are off the task list but still run do_exit() and its
+ * callees, possibly from this patch's or a replaced patch's code, so the
+ * transition must cover them too. The try step inspects stacks, which needs a
+ * reference on each task, so klp_try_switch_exiting_tasks() works on a
+ * snapshot of the exit lists.
+ *
+ * The other steps run with tasklist_lock held and skip tasks that are still
+ * hashed, so every task is visited exactly once: an exiting task can be on
+ * both lists until release_task() unhashes it.
+ */
+static void klp_exiting_init(struct task_struct *t, void *arg)
+{
+ if (pid_alive(t))
+ return; /* still on the task list: already visited */
+ t->patch_state = *(int *)arg;
+}
+
+static void klp_exiting_start(struct task_struct *t, void *arg)
+{
+ if (pid_alive(t))
+ return; /* still on the task list: already visited */
+ if (t->patch_state != klp_target_state)
+ set_tsk_thread_flag(t, TIF_PATCH_PENDING);
+}
+
+static void klp_exiting_complete(struct task_struct *t, void *arg)
+{
+ if (pid_alive(t))
+ return; /* still on the task list: already visited */
+ clear_tsk_thread_flag(t, TIF_PATCH_PENDING);
+ t->patch_state = KLP_TRANSITION_IDLE;
+}
+
+static void klp_exiting_clear_pending(struct task_struct *t, void *arg)
+{
+ if (pid_alive(t))
+ return; /* still on the task list: already visited */
+ clear_tsk_thread_flag(t, TIF_PATCH_PENDING);
+}
+
+static void klp_exiting_force(struct task_struct *t, void *arg)
+{
+ if (pid_alive(t))
+ return; /* still on the task list: already visited */
+ klp_update_patch_state(t);
+}
+
/*
* The transition to the target patch state is complete. Clean up the data
* structures.
@@ -123,6 +172,7 @@ static void klp_complete_transition(void)
WARN_ON_ONCE(test_tsk_thread_flag(task, TIF_PATCH_PENDING));
task->patch_state = KLP_TRANSITION_IDLE;
}
+ rcu_tasks_visit_exiting(klp_exiting_complete, NULL);
read_unlock(&tasklist_lock);
for_each_possible_cpu(cpu) {
@@ -419,6 +469,35 @@ static void klp_send_signals(void)
read_unlock(&tasklist_lock);
}
+/*
+ * Try to switch every task on the exit lists. Returns false if one of them
+ * could not be switched yet, or if the snapshot could not be taken in full.
+ */
+static bool klp_try_switch_exiting_tasks(void)
+{
+ struct task_struct **xt;
+ bool complete = true;
+ int i, n, max;
+
+ /*
+ * Size the array from a count taken now, plus slack for tasks that
+ * start exiting before the copy is made. If even more arrive, report
+ * an incomplete walk: klp_try_complete_transition() runs again later.
+ */
+ max = rcu_tasks_snapshot_exiting(NULL, 0) + 64;
+ xt = kvmalloc_array(max, sizeof(*xt), GFP_KERNEL);
+ if (!xt)
+ return false;
+ n = rcu_tasks_snapshot_exiting(xt, max);
+ for (i = 0; i < min(n, max); i++) {
+ if (!klp_try_switch_task(xt[i]))
+ complete = false;
+ put_task_struct(xt[i]);
+ }
+ kvfree(xt);
+ return complete && n <= max;
+}
+
/*
* Try to switch all remaining tasks to the target patch state by walking the
* stacks of sleeping tasks and looking for any to-be-patched or
@@ -450,6 +529,8 @@ void klp_try_complete_transition(void)
if (!klp_try_switch_task(task))
complete = false;
read_unlock(&tasklist_lock);
+ if (!klp_try_switch_exiting_tasks())
+ complete = false;
/*
* Ditto for the idle "swapper" tasks.
@@ -489,6 +570,13 @@ void klp_try_complete_transition(void)
/* Done! Now cleanup the data structures. */
klp_resched_disable();
patch = klp_transition_patch;
+ /*
+ * A task can leave the exit lists unswitched after the snapshot. It
+ * runs with preemption disabled until its final switch, so wait until
+ * every CPU has switched context. synchronize_rcu() is not enough:
+ * __schedule() reports the quiescent state before switch_to().
+ */
+ klp_synchronize_transition();
klp_complete_transition();
/*
@@ -526,6 +614,7 @@ void klp_start_transition(void)
for_each_process_thread(g, task)
if (task->patch_state != klp_target_state)
set_tsk_thread_flag(task, TIF_PATCH_PENDING);
+ rcu_tasks_visit_exiting(klp_exiting_start, NULL);
read_unlock(&tasklist_lock);
/*
@@ -579,6 +668,7 @@ void klp_init_transition(struct klp_patch *patch, int state)
WARN_ON_ONCE(task->patch_state != KLP_TRANSITION_IDLE);
task->patch_state = initial_state;
}
+ rcu_tasks_visit_exiting(klp_exiting_init, &initial_state);
read_unlock(&tasklist_lock);
/*
@@ -590,6 +680,16 @@ void klp_init_transition(struct klp_patch *patch, int state)
task->patch_state = initial_state;
}
+ /*
+ * A task that has passed exit_tasks_rcu_finish() is on neither list but
+ * may still call patched code until its final switch, which it makes
+ * with preemption disabled. This returns only after every CPU has
+ * switched context, so every such task is gone. synchronize_rcu() is
+ * not enough: __schedule() reports the quiescent state before
+ * switch_to().
+ */
+ klp_synchronize_transition();
+
/*
* Enforce the order of the task->patch_state initializations and the
* func->transition updates to ensure that klp_ftrace_handler() doesn't
@@ -642,6 +742,7 @@ void klp_reverse_transition(void)
read_lock(&tasklist_lock);
for_each_process_thread(g, task)
clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
+ rcu_tasks_visit_exiting(klp_exiting_clear_pending, NULL);
read_unlock(&tasklist_lock);
for_each_possible_cpu(cpu)
@@ -714,6 +815,7 @@ void klp_force_transition(void)
read_lock(&tasklist_lock);
for_each_process_thread(g, task)
klp_update_patch_state(task);
+ rcu_tasks_visit_exiting(klp_exiting_force, NULL);
read_unlock(&tasklist_lock);
for_each_possible_cpu(cpu)
--
2.39.5
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v4 3/3] livepatch: Select TASKS_RCU
2026-11-26 16:41 [PATCH v4 0/3] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-11-26 16:41 ` [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay
2026-11-26 16:41 ` [PATCH v4 2/3] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay
@ 2026-11-26 16:41 ` Binoy Ravindran via B4 Relay
2 siblings, 0 replies; 5+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-26 16:41 UTC (permalink / raw)
To: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
Joel Fernandes, Josh Triplett, Boqun Feng, Uladzislau Rezki,
Steven Rostedt, Mathieu Desnoyers, Lai Jiangshan, Zqiang,
Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Ingo Molnar, Jiri Kosina
Cc: rcu, linux-kernel, live-patching, Yafang Shao, Jaidev Shastri,
Binoy Ravindran
From: Binoy Ravindran <binoy@vt.edu>
The transition finds exiting tasks through the RCU-tasks exit lists,
which exist only with CONFIG_TASKS_RCU. Every kernel that can enable
LIVEPATCH already builds TASKS_RCU. TASKS_RCU defaults to
NEED_TASKS_RCU && PREEMPTION. LIVEPATCH depends on dynamic ftrace, so
FUNCTION_TRACER is set, and it selects NEED_TASKS_RCU. Every
architecture that selects HAVE_LIVEPATCH also selects
ARCH_HAS_PREEMPT_LAZY, which rules out PREEMPT_VOLUNTARY, and none
selects ARCH_NO_PREEMPT, which PREEMPT_NONE requires, so PREEMPTION is
always set.
Select TASKS_RCU explicitly, so that the dependency is documented and
holds even if those defaults change. This changes no configuration
today.
Suggested-by: Yafang Shao <laoar.shao@gmail.com>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
kernel/livepatch/Kconfig | 1 +
1 file changed, 1 insertion(+)
diff --git a/kernel/livepatch/Kconfig b/kernel/livepatch/Kconfig
index 4c0a9c18d0b2..683d6993e207 100644
--- a/kernel/livepatch/Kconfig
+++ b/kernel/livepatch/Kconfig
@@ -12,6 +12,7 @@ config LIVEPATCH
depends on KALLSYMS_ALL
depends on HAVE_LIVEPATCH
depends on !TRIM_UNUSED_KSYMS
+ select TASKS_RCU
help
Say Y here if you want to support kernel live patching.
This option has no runtime impact until a kernel "patch"
--
2.39.5
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists
2026-11-26 16:41 ` [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay
@ 2026-11-26 18:52 ` Petr Mladek
0 siblings, 0 replies; 5+ messages in thread
From: Petr Mladek @ 2026-11-26 18:52 UTC (permalink / raw)
To: binoy
Cc: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
Joel Fernandes, Josh Triplett, Boqun Feng, Uladzislau Rezki,
Steven Rostedt, Mathieu Desnoyers, Lai Jiangshan, Zqiang,
Josh Poimboeuf, Miroslav Benes, Joe Lawrence, Song Liu,
Ingo Molnar, Jiri Kosina, rcu, linux-kernel, live-patching,
Yafang Shao, Jaidev Shastri
On Thu 2026-11-26 11:41:32, Binoy Ravindran via B4 Relay wrote:
> From: Binoy Ravindran <binoy@vt.edu>
>
> A task joins its CPU's RCU-tasks exit list in exit_tasks_rcu_start(),
> before exit_notify() unhashes it from the task list, and leaves it in
> exit_tasks_rcu_finish(), after do_exit() has disabled preemption for the
> final switch. Once exit_notify() has unhashed it, the task is no longer
> found by for_each_process_thread(), but it still runs do_exit() and may
> sleep there. Code that must reach every task that can still be
> descheduled, such as the livepatch transition, has no way to find it.
>
> Add two helpers. rcu_tasks_visit_exiting() calls a function on each
> task on the exit lists under the list locks, without allocating.
> rcu_tasks_snapshot_exiting() copies the tasks into a caller-supplied
> array with a reference held on each, for callers that must sleep or take
> other locks while examining them. Without CONFIG_TASKS_RCU there are no
> exit lists, and the helpers do nothing.
Nit: I would be nice to make the commit messages better human readable.
The dense paragraphs generated by AI are not easy to read. I mean
something like:
Add two helpers:
1. rcu_tasks_visit_exiting() allows to do a quick operation on
all exiting processes. It will be needed, for example, for...
2. rcu_tasks_snapshot_exiting() ...
> rcu_tasks_postscan() keeps its place in an exit list with a bare
> list_head while it drops the lock to reschedule, relying on nobody else
> traversing the list. Move that cursor into struct rcu_tasks_percpu,
> next to the list it marks, so that the new helpers can recognize and
> skip it. Only the grace-period kthread runs postscan, one CPU's list at
> a time, so one cursor per CPU suffices.
>
> --- a/kernel/rcu/tasks.h
> +++ b/kernel/rcu/tasks.h
> @@ -33,6 +33,8 @@ typedef void (*postgp_func_t)(struct rcu_tasks *rtp);
> * @barrier_q_head: RCU callback for barrier operation.
> * @rtp_blkd_tasks: List of tasks blocked as readers.
> * @rtp_exit_list: List of tasks in the latter portion of do_exit().
> + * @rtp_exit_cursor: Placeholder that rcu_tasks_postscan() parks on @rtp_exit_list
> + * while it drops the lock, so that exit-list walkers can skip it.
> * @cpu: CPU number corresponding to this entry.
> * @index: Index of this CPU in rtpcp_array of the rcu_tasks structure.
> * @rtpp: Pointer to the rcu_tasks structure.
> @@ -49,6 +51,7 @@ struct rcu_tasks_percpu {
> struct rcu_head barrier_q_head;
> struct list_head rtp_blkd_tasks;
> struct list_head rtp_exit_list;
> + struct list_head rtp_exit_cursor;
> int cpu;
> int index;
> struct rcu_tasks *rtpp;
> @@ -1068,7 +1071,6 @@ static void rcu_tasks_postscan(struct list_head *hop)
> struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
> struct task_struct *t;
> struct task_struct *t1;
> - struct list_head tmp;
>
> raw_spin_lock_irq_rcu_node(rtpcp);
> list_for_each_entry_safe(t, t1, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) {
> @@ -1081,14 +1083,15 @@ static void rcu_tasks_postscan(struct list_head *hop)
> continue;
>
> // Keep our place in the list while pausing.
> - // Nothing else traverses this list, so adding a
> - // bare list_head is OK.
> - list_add(&tmp, &t->rcu_tasks_exit_list);
> + // The cursor lives in rtpcp so that the exit-list
> + // visitors can recognize and skip it.
> + list_add(&rtpcp->rtp_exit_cursor, &t->rcu_tasks_exit_list);
> raw_spin_unlock_irq_rcu_node(rtpcp);
> cond_resched(); // For CONFIG_PREEMPT=n kernels
> raw_spin_lock_irq_rcu_node(rtpcp);
> - t1 = list_entry(tmp.next, struct task_struct, rcu_tasks_exit_list);
> - list_del(&tmp);
> + t1 = list_entry(rtpcp->rtp_exit_cursor.next, struct task_struct,
> + rcu_tasks_exit_list);
> + list_del(&rtpcp->rtp_exit_cursor);
> j = jiffies + 1;
> }
> raw_spin_unlock_irq_rcu_node(rtpcp);
> @@ -1332,6 +1335,60 @@ void exit_tasks_rcu_finish(void)
> raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
> }
>
> +/*
> + * Call fn on every task on the exit lists, in place, with the per-CPU list
> + * lock held and interrupts disabled. fn must not sleep. Does not allocate.
> + * These are the tasks that have passed exit_tasks_rcu_start() but not
> + * exit_tasks_rcu_finish(). Once exit_notify() has run they are no longer on
> + * the task list, but they still run do_exit() and may sleep there.
> + */
> +void rcu_tasks_visit_exiting(rcu_tasks_exiting_fn_t fn, void *arg)
> +{
> + int cpu;
> + unsigned long flags;
> + struct task_struct *t;
> +
> + for_each_possible_cpu(cpu) {
> + struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
> +
> + raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
> + list_for_each_entry(t, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) {
> + if (&t->rcu_tasks_exit_list == &rtpcp->rtp_exit_cursor)
> + continue;
> + fn(t, arg);
Is it acceptable to process all entries while keeping the raw_spin_lock()?
rcu_tasks_postscan() contains the trick with the @tmp/@rtp_exit_cursor
list_head to be able to release the spin lock while walking the list.
You might argue that our fn() is short. But rcu_tasks_postscan() calls
rcu_tasks_pertask() in the loop and it seems to be quite simple as well.
IMHO, a better approach would be to make rcu_tasks_postscan() generic
function to do an operation on the rcu_tasks_exit_list and release
the spin lock regularly. The operation should be defined via a *fn
passed as a parameter.
I mean to allow to use the same RT-friendly for-cycle in both RCU
and livepatching code paths.
We might need more cursors. One for the RCU code path and
another for the livepatching code path.
It will be tricky. But it will remove the hack with copying the list
of exiting processes into a temporary allocated array. The allocation
and copying is problematic on its own. IMHO, it might cause a livelock
when many processes are exiting...
Best Regards,
Petr
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-26 18:52 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-26 16:41 [PATCH v4 0/3] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-11-26 16:41 ` [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay
2026-11-26 18:52 ` Petr Mladek
2026-11-26 16:41 ` [PATCH v4 2/3] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay
2026-11-26 16:41 ` [PATCH v4 3/3] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay
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®