[PATCH v3 0/3] livepatch: Do not lose exiting tasks during a transition
From: Binoy Ravindran via B4 Relay
Date: Wed Nov 25 2026 - 18:12:46 EST
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@xxxxxxxxx/
[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@xxxxxx
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@xxxxxx
To: "Paul E. McKenney" <paulmck@xxxxxxxxxx>
To: Frederic Weisbecker <frederic@xxxxxxxxxx>
To: Neeraj Upadhyay <neeraj.upadhyay@xxxxxxxxxx>
To: Joel Fernandes <joelagnelf@xxxxxxxxxx>
To: Josh Triplett <josh@xxxxxxxxxxxxxxxx>
To: Boqun Feng <boqun@xxxxxxxxxx>
To: Uladzislau Rezki <urezki@xxxxxxxxx>
To: Steven Rostedt <rostedt@xxxxxxxxxxx>
To: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
To: Lai Jiangshan <jiangshanlai@xxxxxxxxx>
To: Zqiang <qiang.zhang@xxxxxxxxx>
To: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
To: Miroslav Benes <mbenes@xxxxxxx>
To: Petr Mladek <pmladek@xxxxxxxx>
To: Joe Lawrence <joe.lawrence@xxxxxxxxxx>
To: Song Liu <song@xxxxxxxxxx>
To: Ingo Molnar <mingo@xxxxxxxxxx>
To: Jiri Kosina <jikos@xxxxxxxxxx>
Cc: rcu@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Cc: live-patching@xxxxxxxxxxxxxxx
Cc: Yafang Shao <laoar.shao@xxxxxxxxx>
Cc: Jaidev Shastri <jaidevshastri@xxxxxx>
---
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@xxxxxx>