* [PATCH v3 0/3] livepatch: Do not lose exiting tasks during a transition
@ 2026-11-25 23:11 Binoy Ravindran via B4 Relay
2026-11-25 23:11 ` [PATCH v3 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; 10+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-25 23:11 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 synchronize_rcu() 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. Compared with v7.4-rc2 alone (about 30,000 transitions per
mode), the median transition time rose from 8 to 12 ms in the default
mode and from 8 ms to 0.8 s with preempt=full, while the 90th and 99th
percentiles stayed at about 1.6 s and 1.8 s. v2, which waited with
klp_synchronize_transition(), kept the median at 7 to 8 ms in both
modes. The wait is the only functional change from v2, so we attribute
the increase to the synchronize_rcu() calls. 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 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 | 99 +++++++++++++++++++++++++++++++++++++++++++
kernel/rcu/tasks.h | 69 +++++++++++++++++++++++++++---
4 files changed, 172 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] 10+ messages in thread* [PATCH v3 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists 2026-11-25 23:11 [PATCH v3 0/3] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay @ 2026-11-25 23:11 ` Binoy Ravindran via B4 Relay 2026-11-25 23:11 ` [PATCH v3 2/3] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay 2026-11-25 23:11 ` [PATCH v3 3/3] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay 2 siblings, 0 replies; 10+ messages in thread From: Binoy Ravindran via B4 Relay @ 2026-11-25 23:11 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 related [flat|nested] 10+ messages in thread
* [PATCH v3 2/3] livepatch: Include exiting tasks in the transition 2026-11-25 23:11 [PATCH v3 0/3] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay 2026-11-25 23:11 ` [PATCH v3 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay @ 2026-11-25 23:11 ` Binoy Ravindran via B4 Relay 2026-11-25 23:33 ` sashiko-bot 2026-11-25 23:11 ` [PATCH v3 3/3] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay 2 siblings, 1 reply; 10+ messages in thread From: Binoy Ravindran via B4 Relay @ 2026-11-25 23:11 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. This stretch is an RCU read-side critical section, so synchronize_rcu() waits it out, as rcu_tasks_postgp() already relies on. 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 | 99 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c index 2351a19ac2a9..0edcb9f9c663 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,12 @@ 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 + * synchronize_rcu() waits it out, as in rcu_tasks_postgp(). + */ + synchronize_rcu(); klp_complete_transition(); /* @@ -526,6 +613,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 +667,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 +679,14 @@ 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. It runs with + * preemption disabled, so synchronize_rcu() returns only after every + * such task is gone, as in rcu_tasks_postgp(). + */ + synchronize_rcu(); + /* * Enforce the order of the task->patch_state initializations and the * func->transition updates to ensure that klp_ftrace_handler() doesn't @@ -642,6 +739,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 +812,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 related [flat|nested] 10+ messages in thread
* Re: [PATCH v3 2/3] livepatch: Include exiting tasks in the transition 2026-11-25 23:11 ` [PATCH v3 2/3] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay @ 2026-11-25 23:33 ` sashiko-bot 2026-11-26 0:58 ` Ravindran, Binoy 0 siblings, 1 reply; 10+ messages in thread From: sashiko-bot @ 2026-11-25 23:33 UTC (permalink / raw) To: Binoy Ravindran, 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: live-patching, Yafang Shao, Jaidev Shastri Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] livepatch: synchronize_rcu() returns before dying task finishes __schedule() -- commit 92ba6cc4a8d2e1bcb38ce05b46cd777e11fa7ede Author: Binoy Ravindran <binoy@vt.edu> livepatch: Include exiting tasks in the transition This commit extends livepatch transitions to visit exiting tasks on the RCU-tasks exit lists and uses synchronize_rcu() to wait for tasks that have already left the exit lists to finish their final context switch. Link: https://lore.kernel.org/all/20250127063526.76687-1-laoar.shao@gmail.com/ > diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c > index 2351a19ac2a9e..0edcb9f9c663b 100644 > --- a/kernel/livepatch/transition.c > +++ b/kernel/livepatch/transition.c [ ... ] > @@ -489,6 +570,12 @@ 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 > + * synchronize_rcu() waits it out, as in rcu_tasks_postgp(). > + */ > + synchronize_rcu(); > klp_complete_transition(); > > /* [ ... ] > @@ -590,6 +679,14 @@ 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. It runs with > + * preemption disabled, so synchronize_rcu() returns only after every > + * such task is gone, as in rcu_tasks_postgp(). > + */ > + synchronize_rcu(); > + > /* > * Enforce the order of the task->patch_state initializations and the > * func->transition updates to ensure that klp_ftrace_handler() doesn't [Severity: Medium] Can synchronize_rcu() in klp_try_complete_transition() and klp_init_transition() return while a dying task is still executing __schedule() before switch_to()? When do_exit() reaches do_task_dead() -> __schedule(), klp_sched_try_switch() is a no-op because curr->__state is TASK_DEAD rather than TASK_FREEZABLE, and rcu_note_context_switch() reports the RCU quiescent state near the start of __schedule() while current is still the dying task: CPU0 (dying task) CPU1 (livepatch transition) do_exit() preempt_disable() exit_tasks_rcu_finish() klp_init_transition() /* walks miss CPU0's task */ synchronize_rcu() do_task_dead() __schedule() klp_sched_try_switch(prev) rcu_note_context_switch() /* synchronize_rcu() returns */ func->transition = true pick_next_task() klp_ftrace_handler() <-- sees patch_state == KLP_TRANSITION_IDLE context_switch() switch_to() If a livepatch patches any function called in __schedule() between rcu_note_context_switch() and switch_to() (such as update_rq_clock(), try_to_block_task(), pick_next_task(), or psi_sched_switch()), klp_ftrace_handler() can hit WARN_ON_ONCE(patch_state == KLP_TRANSITION_IDLE) on the uninitialized task during klp_init_transition(), or klp_complete_transition() can unpatch replaced patches and clear func->transition while an unswitched dying task is still running inside __schedule(). -- Sashiko AI review · https://sashiko.dev/#/patchset/20261125-klp-series-v3-v3-0-b1e0f79af331@vt.edu?part=2 ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v3 2/3] livepatch: Include exiting tasks in the transition 2026-11-25 23:33 ` sashiko-bot @ 2026-11-26 0:58 ` Ravindran, Binoy 2026-11-26 3:12 ` Yafang Shao 0 siblings, 1 reply; 10+ messages in thread From: Ravindran, Binoy @ 2026-11-26 0:58 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, sashiko-reviews@lists.linux.dev Cc: live-patching@vger.kernel.org, Yafang Shao, Shastri, Jaidev, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Thanks, this is right. A dying task reports its quiescent state in rcu_note_context_switch(), early in __schedule(), and then still runs traceable code, such as update_rq_clock() and pick_next_task(), until switch_to(). RCU does not promise to wait for code after that point, and with expedited grace periods the quiescent state is reported right there, so synchronize_rcu() can return too early. klp_synchronize_transition(), which v2 used, does not have this problem: schedule_on_each_cpu() completes only after the work item has run on every CPU, which on the dying task's CPU happens only after switch_to(). v4 goes back to klp_synchronize_transition() for both waits and keeps klp_try_switch_exiting_tasks() from v3. I will send it after rerunning the tests. Yafang, this reverses your second suggestion; I hope the reason above makes sense. It also brings back the v2 transition times. Binoy ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v3 2/3] livepatch: Include exiting tasks in the transition 2026-11-26 0:58 ` Ravindran, Binoy @ 2026-11-26 3:12 ` Yafang Shao 2026-11-27 14:39 ` Petr Mladek 0 siblings, 1 reply; 10+ messages in thread From: Yafang Shao @ 2026-11-26 3:12 UTC (permalink / raw) To: Ravindran, Binoy, Paul E. McKenney Cc: 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, sashiko-reviews@lists.linux.dev, live-patching@vger.kernel.org, Shastri, Jaidev, linux-kernel@vger.kernel.org, rcu@vger.kernel.org On Thu, Nov 26, 2026 at 8:58 AM Ravindran, Binoy <binoy@vt.edu> wrote: > > > Thanks, this is right. A dying task reports its quiescent state in > rcu_note_context_switch(), early in __schedule(), and then still runs > traceable code, such as update_rq_clock() and pick_next_task(), until > switch_to(). RCU does not promise to wait for code after that point, and > with expedited grace periods the quiescent state is reported right > there, so synchronize_rcu() can return too early. > klp_synchronize_transition(), which v2 used, does not have this > problem: schedule_on_each_cpu() completes only after the work item has > run on every CPU, which on the dying task's CPU happens only after > switch_to(). > > v4 goes back to klp_synchronize_transition() for both waits and keeps > klp_try_switch_exiting_tasks() from v3. I will send it after rerunning > the tests. > > Yafang, this reverses your second suggestion; I hope the reason above > makes sense. It also brings back the v2 transition times. Agreed. I missed the point that the quiescent state is reported early in __schedule(), and that the code from there until switch_to() is not covered by synchronize_rcu(). Thanks for pointing that out. -- Regards Yafang ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v3 2/3] livepatch: Include exiting tasks in the transition 2026-11-26 3:12 ` Yafang Shao @ 2026-11-27 14:39 ` Petr Mladek 2026-11-27 15:10 ` Petr Mladek 0 siblings, 1 reply; 10+ messages in thread From: Petr Mladek @ 2026-11-27 14:39 UTC (permalink / raw) To: Yafang Shao Cc: Ravindran, Binoy, 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, sashiko-reviews@lists.linux.dev, live-patching@vger.kernel.org, Shastri, Jaidev, linux-kernel@vger.kernel.org, rcu@vger.kernel.org On Thu 2026-11-26 11:12:27, Yafang Shao wrote: > On Thu, Nov 26, 2026 at 8:58 AM Ravindran, Binoy <binoy@vt.edu> wrote: > > > > > > Thanks, this is right. A dying task reports its quiescent state in > > rcu_note_context_switch(), early in __schedule(), and then still runs > > traceable code, such as update_rq_clock() and pick_next_task(), until > > switch_to(). RCU does not promise to wait for code after that point, and > > with expedited grace periods the quiescent state is reported right > > there, so synchronize_rcu() can return too early. > > klp_synchronize_transition(), which v2 used, does not have this > > problem: schedule_on_each_cpu() completes only after the work item has > > run on every CPU, which on the dying task's CPU happens only after > > switch_to(). > > > > v4 goes back to klp_synchronize_transition() for both waits and keeps > > klp_try_switch_exiting_tasks() from v3. I will send it after rerunning > > the tests. > > > > Yafang, this reverses your second suggestion; I hope the reason above > > makes sense. It also brings back the v2 transition times. > > Agreed. I missed the point that the quiescent state is reported early > in __schedule(), and that the code from there until switch_to() is not > covered by synchronize_rcu(). Thanks for pointing that out. Honestly, I think that synchronize_srcu() should work. Otherwise, the comment in rcu_tasks_postgp() would be wrong. IMHO, the trick is that there are two interlacing rcu_read() periods in do_exit() and synchronize_rcu(), see the comment in rcu_tasks_postscan() and the commit e4e1e8089c5fd9 ("rcu-tasks: Improve comments explaining tasks_rcu_exit_srcu purpose"). Best Regards, Petr ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v3 2/3] livepatch: Include exiting tasks in the transition 2026-11-27 14:39 ` Petr Mladek @ 2026-11-27 15:10 ` Petr Mladek 2026-11-27 19:13 ` Ravindran, Binoy 0 siblings, 1 reply; 10+ messages in thread From: Petr Mladek @ 2026-11-27 15:10 UTC (permalink / raw) To: Yafang Shao Cc: Ravindran, Binoy, 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, sashiko-reviews@lists.linux.dev, live-patching@vger.kernel.org, Shastri, Jaidev, linux-kernel@vger.kernel.org, rcu@vger.kernel.org On Fri 2026-11-27 15:39:15, Petr Mladek wrote: > On Thu 2026-11-26 11:12:27, Yafang Shao wrote: > > On Thu, Nov 26, 2026 at 8:58 AM Ravindran, Binoy <binoy@vt.edu> wrote: > > > > > > > > > Thanks, this is right. A dying task reports its quiescent state in > > > rcu_note_context_switch(), early in __schedule(), and then still runs > > > traceable code, such as update_rq_clock() and pick_next_task(), until > > > switch_to(). RCU does not promise to wait for code after that point, and > > > with expedited grace periods the quiescent state is reported right > > > there, so synchronize_rcu() can return too early. > > > klp_synchronize_transition(), which v2 used, does not have this > > > problem: schedule_on_each_cpu() completes only after the work item has > > > run on every CPU, which on the dying task's CPU happens only after > > > switch_to(). > > > > > > v4 goes back to klp_synchronize_transition() for both waits and keeps > > > klp_try_switch_exiting_tasks() from v3. I will send it after rerunning > > > the tests. > > > > > > Yafang, this reverses your second suggestion; I hope the reason above > > > makes sense. It also brings back the v2 transition times. > > > > Agreed. I missed the point that the quiescent state is reported early > > in __schedule(), and that the code from there until switch_to() is not > > covered by synchronize_rcu(). Thanks for pointing that out. > > Honestly, I think that synchronize_srcu() should work. Otherwise, > the comment in rcu_tasks_postgp() would be wrong. > > IMHO, the trick is that there are two interlacing rcu_read() periods > in do_exit() and synchronize_rcu(), see the comment in > rcu_tasks_postscan() and the commit e4e1e8089c5fd9 ("rcu-tasks: > Improve comments explaining tasks_rcu_exit_srcu purpose"). Wait! I might be wrong. I think that it does not work because the 1st rcu_read section ends in exit_rcu() which is called before exit_tasks_rcu_finish() where the task is removed from rcu_tasks_exit_list. By other words, the task is removed from the rcu_tasks_exit_list in the later 2nd RCU period. So, if synchronize_rcu() is going to wait for this 2nd period then it might end prematurely because the quiescent state is reported by rcu_note_context_switch() early in __schedule(). But I might miss something. I am not sure which period synchronize_rcu() is waiting for. Best Regards, Petr ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v3 2/3] livepatch: Include exiting tasks in the transition 2026-11-27 15:10 ` Petr Mladek @ 2026-11-27 19:13 ` Ravindran, Binoy 0 siblings, 0 replies; 10+ messages in thread From: Ravindran, Binoy @ 2026-11-27 19:13 UTC (permalink / raw) To: Petr Mladek, Yafang Shao 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, sashiko-reviews@lists.linux.dev, live-patching@vger.kernel.org, Shastri, Jaidev, linux-kernel@vger.kernel.org, rcu@vger.kernel.org Hi Petr, > By other words, the task is removed from the rcu_tasks_exit_list > in the later 2nd RCU period. So, if synchronize_rcu() is going > to wait for this 2nd period then it might end prematurely because > the quiescent state is reported by rcu_note_context_switch() > early in __schedule(). Yes, that matches my reading. > But I might miss something. I am not sure which period > synchronize_rcu() is waiting for. synchronize_rcu() waits for RCU readers that started before it. Of the two sections that the comment above rcu_tasks_postscan() describes, only the second is an RCU reader: the stretch from the final preempt_disable() in do_exit() to the final schedule(). rcu_note_context_switch() notes the quiescent state early in __schedule(), before switch_to(). With an expedited grace period in progress, it also reports it right there, through rcu_report_exp_rdp() (kernel/rcu/tree_plugin.h). The grace period can then end while the task still runs traceable code in the rest of __schedule(). RCU does not need to cover that code, but livepatch does. klp_synchronize_transition() covers it, because the work item can run on that CPU only after the task has switched out. It was also cheaper in my tests. With synchronize_rcu() (v3), the median transition time went from 8 to 12 ms in the default mode and from 8 ms to 0.8 s with preempt=full. With klp_synchronize_transition() (v4), the medians were 7 ms and 11 ms. So v5 keeps klp_synchronize_transition(), with the comment reworded as you suggested. Thanks, Binoy ^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v3 3/3] livepatch: Select TASKS_RCU 2026-11-25 23:11 [PATCH v3 0/3] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay 2026-11-25 23:11 ` [PATCH v3 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay 2026-11-25 23:11 ` [PATCH v3 2/3] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay @ 2026-11-25 23:11 ` Binoy Ravindran via B4 Relay 2 siblings, 0 replies; 10+ messages in thread From: Binoy Ravindran via B4 Relay @ 2026-11-25 23:11 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 related [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-11-27 19:13 UTC | newest] Thread overview: 10+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-25 23:11 [PATCH v3 0/3] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay 2026-11-25 23:11 ` [PATCH v3 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay 2026-11-25 23:11 ` [PATCH v3 2/3] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay 2026-11-25 23:33 ` sashiko-bot 2026-11-26 0:58 ` Ravindran, Binoy 2026-11-26 3:12 ` Yafang Shao 2026-11-27 14:39 ` Petr Mladek 2026-11-27 15:10 ` Petr Mladek 2026-11-27 19:13 ` Ravindran, Binoy 2026-11-25 23:11 ` [PATCH v3 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