* [PATCH v5 0/4] livepatch: Do not lose exiting tasks during a transition
@ 2026-12-05 18:08 Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section Binoy Ravindran via B4 Relay
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-12-05 18:08 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, Ingo Molnar, Peter Zijlstra,
Juri Lelli, Vincent Guittot, Dietmar Eggemann, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak
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
reported this race in [1] and discussed it there with Petr Mladek, and
Josh Poimboeuf suggested building on the exit tracking of
exit_tasks_rcu_start() and exit_tasks_rcu_finish() in [2]. A task that
was already in this exit window when a transition starts is also never
initialized, so if it then calls a function in transition,
klp_ftrace_handler() warns or picks the wrong version of the function.
That second problem predates the RCU-tasks exit lists.
v5 follows Paul McKenney's suggestion in the v4 thread. Patch 1 makes
the exit window, from exit_tasks_rcu_start() to exit_tasks_rcu_finish(),
an SRCU read-side critical section, as it was before the exit lists, and
adds blocking and polled waits for it. Patch 4 uses them:
- klp_init_transition() waits for the window before func->transition
is set, so that no task in it runs a patched function uninitialized.
The wait is killable: on a fatal signal, klp_init_transition()
undoes its setup and returns -EINTR, and enabling or disabling the
patch fails.
- klp_try_complete_transition() starts a polled grace period once
every other task has switched. Tasks do not switch back, so the
retries only poll it, every 10 ms, without walking the tasks again.
klp_mutex is not held across this wait, and a forced transition
does not wait.
As in v4, klp_synchronize_transition() waits out the tasks past
exit_tasks_rcu_finish(), which run with preemption disabled until their
final switch, in both places. The exit-list walks of v4 are gone, and so
is the walker interface on the RCU side. Patch 2 makes
klp_synchronize_transition() retry if schedule_on_each_cpu() cannot
allocate its work items. Patch 3, suggested by Yafang, makes LIVEPATCH
select TASKS_RCU, which every livepatch kernel already builds.
The SRCU wait covers every task in the exit window, whether or not it
runs patched code, so a slow exit delays the transition by its length.
The wait at init holds klp_mutex, and a task stuck in the window could
make it last indefinitely. It could also deadlock: a task that loads a
livepatch inside a PID namespace whose init is exiting waits for that
init, which waits in zap_pid_ns_processes() for the task. Because the
wait is killable, the loading task can be killed, and in the namespace
case the exiting init sends it SIGKILL. The wait at completion does not
hold klp_mutex, and a forced transition does not wait.
Tested on v7.4-rc2 under KVM, with two livepatches that replace each
other in a loop and a debug-only sleep after exit_notify(). On x86-64,
with klp-build:
- do_exit() replaced, with no sleep and with sleeps of 20 ms and 2 s:
30 runs and 420 replace cycles without a fault. Without the series,
6 of 6 runs crashed with a 2 s sleep.
- A function called after the sleep, inside the exit window, replaced
instead: no livepatch warning in 24 runs. Without the series, all 24
warned.
- With lockdep, PROVE_RCU and DEBUG_ATOMIC_SLEEP: 10 runs and 180
replace cycles with no reports.
- With CPUs going offline and online throughout: 12 runs and 120
replace cycles without a fault.
- 12 runs that force a transition all finished.
- With exiting tasks sleeping 15 s in the window, insmod was killed
5 s into the wait at init. In 6 of 6 runs it exited within 0.03 s
and left nothing loaded, and a later insmod enabled the patch.
Without the killable wait, insmod exited only after about 10 s, and
the patch was enabled anyway.
On arm64, with kpatch-build, do_exit() replaced with no sleep and with
sleeps of 20 ms, 200 ms and 2 s: 42 runs and 540 replace cycles without
a fault.
Each commit builds without warnings with and without CONFIG_LIVEPATCH,
and with W=1 for the files it touches.
Transition time, from "starting patching transition" to "patching
complete", as p90 / p99, for a livepatch that does not touch the exit
path, with 36 runs per exit sleep (18 for no sleep):
no sleep: exit-list walk 1.68 / 1.79 s, v5 1.63 / 1.78 s
500 ms: exit-list walk 1.73 / 1.84 s, v5 2.11 / 2.31 s
1 s: exit-list walk 1.75 / 1.91 s, v5 2.69 / 2.83 s
2 s: exit-list walk 1.84 / 2.03 s, v5 3.70 / 3.85 s
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/live-patching/CALOAHbA9WHPjeZKUcUkwULagQjTMfqAdAg+akqPzbZ7Byc=qrw@mail.gmail.com/
[2] https://lore.kernel.org/live-patching/20250124010848.pdcxokfsk2ngrowl@jpoimboe/
---
Changes in v5:
- Replace the exit-list walks with an SRCU read side over the exit
window and waits for it (Paul McKenney). This replaces
"rcu-tasks: Add helpers to visit tasks on the exit lists" with
"rcu-tasks: Make the exit window an SRCU read-side critical section",
and "livepatch: Include exiting tasks in the transition" with
"livepatch: Wait for exiting tasks around a transition".
- Wait for the exit window in klp_init_transition() too, which also
fixes the stale patch_state of tasks that were in the window when the
transition started. Make that wait killable.
- Add "livepatch: Retry klp_synchronize_transition() if it fails".
- Point [1] and the Link: tag of patch 4 at the original crash report.
- Link to v4: https://patch.msgid.link/20261126-klp-series-v3-v4-0-2ad73e9c9254@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>
To: Ingo Molnar <mingo@redhat.com>
To: Peter Zijlstra <peterz@infradead.org>
To: Juri Lelli <juri.lelli@redhat.com>
To: Vincent Guittot <vincent.guittot@linaro.org>
To: Dietmar Eggemann <dietmar.eggemann@arm.com>
To: Ben Segall <bsegall@google.com>
To: Mel Gorman <mgorman@suse.de>
To: Valentin Schneider <vschneid@redhat.com>
To: K Prateek Nayak <kprateek.nayak@amd.com>
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 (4):
rcu-tasks: Make the exit window an SRCU read-side critical section
livepatch: Retry klp_synchronize_transition() if it fails
livepatch: Select TASKS_RCU
livepatch: Wait for exiting tasks around a transition
include/linux/rcupdate.h | 6 ++
include/linux/sched.h | 1 +
kernel/livepatch/Kconfig | 1 +
kernel/livepatch/core.c | 9 ++-
kernel/livepatch/transition.c | 131 +++++++++++++++++++++++++++++++++++++++++-
kernel/livepatch/transition.h | 2 +-
kernel/rcu/tasks.h | 28 +++++++++
7 files changed, 173 insertions(+), 5 deletions(-)
---
base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb
change-id: 20261123-klp-series-v3-d52796c0590f
Best regards,
--
Binoy Ravindran <binoy@vt.edu>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section
2026-12-05 18:08 [PATCH v5 0/4] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
@ 2026-12-05 18:08 ` Binoy Ravindran via B4 Relay
2026-12-05 18:28 ` sashiko-bot
2026-12-05 18:08 ` [PATCH v5 2/4] livepatch: Retry klp_synchronize_transition() if it fails Binoy Ravindran via B4 Relay
` (2 subsequent siblings)
3 siblings, 1 reply; 8+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-12-05 18:08 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, Ingo Molnar, Peter Zijlstra,
Juri Lelli, Vincent Guittot, Dietmar Eggemann, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak
Cc: rcu, linux-kernel, live-patching, Yafang Shao, Jaidev Shastri,
Binoy Ravindran
From: Binoy Ravindran <binoy@vt.edu>
Tasks between exit_tasks_rcu_start() and exit_tasks_rcu_finish() are no
longer on the task list, but they still run kernel code, and they can
sleep there for a long time. Livepatch needs to wait for them before it
starts a transition and before it removes code they may still run. An
RCU Tasks grace period does not do that: a task asleep in do_exit() is
in an RCU Tasks quiescent state.
Make the exit window an SRCU read-side critical section, as it was before
the exit lists replaced the SRCU read side. The SRCU index is kept in
the task_struct. Add synchronize_rcu_tasks_exit() and a polled pair,
start_poll_synchronize_rcu_tasks_exit() and
poll_state_synchronize_rcu_tasks_exit(), for callers that must not block.
The exit lists are not changed and RCU Tasks does not use the new domain.
Suggested-by: Paul E. McKenney <paulmck@kernel.org>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
include/linux/rcupdate.h | 6 ++++++
include/linux/sched.h | 1 +
kernel/rcu/tasks.h | 28 ++++++++++++++++++++++++++++
3 files changed, 35 insertions(+)
diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
index 3f74ae6d6e1f2..4442fbe9e9ac8 100644
--- a/include/linux/rcupdate.h
+++ b/include/linux/rcupdate.h
@@ -204,6 +204,9 @@ void rcu_tasks_rude_torture_stats_print(char *tt, char *tf);
#define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
void exit_tasks_rcu_start(void);
void exit_tasks_rcu_finish(void);
+void synchronize_rcu_tasks_exit(void);
+unsigned long start_poll_synchronize_rcu_tasks_exit(void);
+bool poll_state_synchronize_rcu_tasks_exit(unsigned long cookie);
#else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
#define rcu_tasks_classic_qs(t, preempt) do { } while (0)
#define rcu_tasks_qs(t, preempt) do { } while (0)
@@ -212,6 +215,9 @@ void exit_tasks_rcu_finish(void);
#define synchronize_rcu_tasks synchronize_rcu
static inline void exit_tasks_rcu_start(void) { }
static inline void exit_tasks_rcu_finish(void) { }
+static inline void synchronize_rcu_tasks_exit(void) { }
+static inline unsigned long start_poll_synchronize_rcu_tasks_exit(void) { return 0; }
+static inline bool poll_state_synchronize_rcu_tasks_exit(unsigned long cookie) { return true; }
#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
/**
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e4f6323d78555..0fe18976d1b40 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -963,6 +963,7 @@ struct task_struct {
struct list_head rcu_tasks_holdout_list;
int rcu_tasks_exit_cpu;
struct list_head rcu_tasks_exit_list;
+ int rcu_tasks_exit_srcu_idx;
#endif /* #ifdef CONFIG_TASKS_RCU */
#ifdef CONFIG_TASKS_TRACE_RCU
diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index fcac7361ec51e..57a16d7c33122 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -1288,6 +1288,32 @@ void rcu_tasks_get_gp_data(int *flags, unsigned long *gp_seq)
}
EXPORT_SYMBOL_GPL(rcu_tasks_get_gp_data);
+/*
+ * Tasks between exit_tasks_rcu_start() and exit_tasks_rcu_finish() are
+ * no longer on the task list, but they still run kernel code. This SRCU
+ * domain lets other subsystems wait for every task in that window to
+ * leave it.
+ */
+DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
+
+/* Wait for all tasks currently in the exit window to leave it. */
+void synchronize_rcu_tasks_exit(void)
+{
+ synchronize_srcu(&tasks_rcu_exit_srcu);
+}
+
+/* Start waiting for the tasks in the exit window, without blocking. */
+unsigned long start_poll_synchronize_rcu_tasks_exit(void)
+{
+ return start_poll_synchronize_srcu(&tasks_rcu_exit_srcu);
+}
+
+/* Have all tasks that were in the exit window at @cookie left it? */
+bool poll_state_synchronize_rcu_tasks_exit(unsigned long cookie)
+{
+ return poll_state_synchronize_srcu(&tasks_rcu_exit_srcu, cookie);
+}
+
/*
* Protect against tasklist scan blind spot while the task is exiting and
* may be removed from the tasklist. Do this by adding the task to yet
@@ -1313,6 +1339,7 @@ void exit_tasks_rcu_start(void)
list_add(&t->rcu_tasks_exit_list, &rtpcp->rtp_exit_list);
raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
preempt_enable();
+ current->rcu_tasks_exit_srcu_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
}
/*
@@ -1330,6 +1357,7 @@ void exit_tasks_rcu_finish(void)
raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
list_del_init(&t->rcu_tasks_exit_list);
raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
+ __srcu_read_unlock(&tasks_rcu_exit_srcu, current->rcu_tasks_exit_srcu_idx);
}
#else /* #ifdef CONFIG_TASKS_RCU */
--
2.39.5
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v5 2/4] livepatch: Retry klp_synchronize_transition() if it fails
2026-12-05 18:08 [PATCH v5 0/4] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section Binoy Ravindran via B4 Relay
@ 2026-12-05 18:08 ` Binoy Ravindran via B4 Relay
2026-12-05 18:22 ` sashiko-bot
2026-12-05 18:08 ` [PATCH v5 3/4] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition Binoy Ravindran via B4 Relay
3 siblings, 1 reply; 8+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-12-05 18:08 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, Ingo Molnar, Peter Zijlstra,
Juri Lelli, Vincent Guittot, Dietmar Eggemann, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak
Cc: rcu, linux-kernel, live-patching, Yafang Shao, Jaidev Shastri,
Binoy Ravindran
From: Binoy Ravindran <binoy@vt.edu>
schedule_on_each_cpu() returns -ENOMEM when it cannot allocate its work
items, before it queues any of them. klp_synchronize_transition() ignores
the error, so it can return without waiting. The transition relies on the
wait in several places, including the two that the following patches add
before func->transition is set and before the transition completes.
Retry until the wait succeeds. The callers can sleep, since
schedule_on_each_cpu() itself sleeps.
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
kernel/livepatch/transition.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index 2351a19ac2a9e..33095e888e10c 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/delay.h>
#include "core.h"
#include "patch.h"
#include "transition.h"
@@ -71,7 +72,13 @@ static void klp_sync(struct work_struct *work)
*/
static void klp_synchronize_transition(void)
{
- schedule_on_each_cpu(klp_sync);
+ /*
+ * schedule_on_each_cpu() fails only when it cannot allocate its work
+ * items, before it queues any of them. The callers rely on the wait,
+ * so retry until it succeeds.
+ */
+ while (schedule_on_each_cpu(klp_sync))
+ msleep(20);
}
/*
--
2.39.5
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH v5 3/4] livepatch: Select TASKS_RCU
2026-12-05 18:08 [PATCH v5 0/4] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 2/4] livepatch: Retry klp_synchronize_transition() if it fails Binoy Ravindran via B4 Relay
@ 2026-12-05 18:08 ` Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition Binoy Ravindran via B4 Relay
3 siblings, 0 replies; 8+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-12-05 18:08 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, Ingo Molnar, Peter Zijlstra,
Juri Lelli, Vincent Guittot, Dietmar Eggemann, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak
Cc: rcu, linux-kernel, live-patching, Yafang Shao, Jaidev Shastri,
Binoy Ravindran
From: Binoy Ravindran <binoy@vt.edu>
The transition waits for exiting tasks through the SRCU read side of
exit_tasks_rcu_start() and exit_tasks_rcu_finish(), which exists 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 4c0a9c18d0b25..683d6993e207c 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] 8+ messages in thread
* [PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition
2026-12-05 18:08 [PATCH v5 0/4] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
` (2 preceding siblings ...)
2026-12-05 18:08 ` [PATCH v5 3/4] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay
@ 2026-12-05 18:08 ` Binoy Ravindran via B4 Relay
2026-12-05 18:27 ` sashiko-bot
3 siblings, 1 reply; 8+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-12-05 18:08 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, Ingo Molnar, Peter Zijlstra,
Juri Lelli, Vincent Guittot, Dietmar Eggemann, Ben Segall,
Mel Gorman, Valentin Schneider, K Prateek Nayak
Cc: rcu, linux-kernel, live-patching, Yafang Shao, Jaidev Shastri,
Binoy Ravindran
From: Binoy Ravindran <binoy@vt.edu>
A task that is past exit_notify() is no longer on the task list, but it
still runs do_exit() and its callees. The transition neither checks its
stack nor sets its patch_state. Two things can go wrong:
1. When the transition completes, it can remove code that such a task
still runs. The task faults when it wakes up and returns into the
freed module memory.
2. A task that was already in the exit window when the transition
started keeps a stale patch_state. If it then calls a function in
transition, klp_ftrace_handler() warns, or picks the wrong version
of the function.
Both can be reproduced by making exiting tasks sleep after exit_notify()
with a test-only hook, while two livepatches replace each other in a
loop. With a 2 s sleep, 6 of 6 runs crash from (1). With the patched
function called after the sleep, 24 of 24 runs warn from (2).
Wait for the exit window, using its SRCU read side:
- In klp_init_transition(), before func->transition is set, wait for
the tasks in the window to leave it. Tasks that enter it later were
on the task list and are initialized as usual.
- In klp_try_complete_transition(), once every other task has
switched, start a polled SRCU grace period. Tasks do not switch
back, so the retries only poll it, every 10 ms, without walking the
tasks again. klp_mutex is not held across a long wait, and a task
stuck in the window, for example after a recursive fault, delays the
transition like any other task that cannot switch. A forced
transition does not wait for it.
The wait in klp_init_transition() holds klp_mutex, and a task stuck in
the exit window can make it last indefinitely. It could even deadlock:
a task that loads a livepatch inside a PID namespace whose init is
exiting waits for that init, which waits in zap_pid_ns_processes() for
the task. So the wait is killable. klp_init_transition() returns
-EINTR if the enabling or disabling task gets a fatal signal, after
undoing its setup, and its callers pass the error on. In the example
above, the init sends the loading task SIGKILL, so it gives up and exits.
Tasks that already left the window run with preemption disabled until
their final context switch, and they can still call patched functions
such as __schedule(). Wait for them with klp_synchronize_transition() in
both places. synchronize_rcu() is not enough: __schedule() reports the
RCU quiescent state in rcu_note_context_switch(), before switch_to().
Fixes: d83a7cb375ee ("livepatch: change to a per-task consistency model")
Link: https://lore.kernel.org/live-patching/CALOAHbA9WHPjeZKUcUkwULagQjTMfqAdAg+akqPzbZ7Byc=qrw@mail.gmail.com/
Suggested-by: Paul E. McKenney <paulmck@kernel.org>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
kernel/livepatch/core.c | 9 +++-
kernel/livepatch/transition.c | 122 +++++++++++++++++++++++++++++++++++++++++-
kernel/livepatch/transition.h | 2 +-
3 files changed, 129 insertions(+), 4 deletions(-)
diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c
index 517fe427ff92d..d5ef8cab4e3d6 100644
--- a/kernel/livepatch/core.c
+++ b/kernel/livepatch/core.c
@@ -1015,6 +1015,7 @@ static int klp_init_patch(struct klp_patch *patch)
static int __klp_disable_patch(struct klp_patch *patch)
{
struct klp_object *obj;
+ int ret;
if (WARN_ON(!patch->enabled))
return -EINVAL;
@@ -1022,7 +1023,9 @@ static int __klp_disable_patch(struct klp_patch *patch)
if (klp_transition_patch)
return -EBUSY;
- klp_init_transition(patch, KLP_TRANSITION_UNPATCHED);
+ ret = klp_init_transition(patch, KLP_TRANSITION_UNPATCHED);
+ if (ret)
+ return ret;
klp_for_each_object(patch, obj)
if (obj->patched)
@@ -1057,7 +1060,9 @@ static int __klp_enable_patch(struct klp_patch *patch)
pr_notice("enabling patch '%s'\n", patch->mod->name);
- klp_init_transition(patch, KLP_TRANSITION_PATCHED);
+ ret = klp_init_transition(patch, KLP_TRANSITION_PATCHED);
+ if (ret)
+ return ret;
/*
* Enforce the order of the func->transition writes in
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index 33095e888e10c..fb033483d02dd 100644
--- a/kernel/livepatch/transition.c
+++ b/kernel/livepatch/transition.c
@@ -426,6 +426,36 @@ static void klp_send_signals(void)
read_unlock(&tasklist_lock);
}
+/*
+ * Tasks in the exit window, between exit_tasks_rcu_start() and
+ * exit_tasks_rcu_finish(), are no longer on the task list, so the stack
+ * checks above do not see them. They may still run code that the cleanup
+ * is about to remove. Once every other task has switched, start an SRCU
+ * grace period for the window and poll it. Tasks do not switch back, so
+ * the retries only poll, every 10 ms, without walking the tasks again.
+ * klp_mutex is not held across the wait, a task stuck in the window delays
+ * the transition like any other task that cannot switch, and a forced
+ * transition does not wait for it.
+ *
+ * Return true if the transition must wait; it has then been rescheduled.
+ */
+static unsigned long klp_exit_cookie;
+static bool klp_exit_cookie_valid;
+
+static bool klp_wait_exiting_tasks(void)
+{
+ if (klp_transition_patch->forced)
+ return false;
+ if (!klp_exit_cookie_valid) {
+ klp_exit_cookie = start_poll_synchronize_rcu_tasks_exit();
+ klp_exit_cookie_valid = true;
+ }
+ if (poll_state_synchronize_rcu_tasks_exit(klp_exit_cookie))
+ return false;
+ schedule_delayed_work(&klp_transition_work, msecs_to_jiffies(10));
+ return true;
+}
+
/*
* 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
@@ -443,6 +473,16 @@ void klp_try_complete_transition(void)
WARN_ON_ONCE(klp_target_state == KLP_TRANSITION_IDLE);
+ /*
+ * Every task already switched: only the tasks in the exit window are
+ * left. Tasks do not switch back, so do not walk them again.
+ */
+ if (klp_exit_cookie_valid) {
+ if (klp_wait_exiting_tasks())
+ return;
+ goto exiting_done;
+ }
+
/*
* Try to switch the tasks to the target patch state by walking their
* stacks and looking for any to-be-patched or to-be-unpatched
@@ -478,6 +518,9 @@ void klp_try_complete_transition(void)
}
cpus_read_unlock();
+ if (complete && klp_wait_exiting_tasks())
+ return;
+
if (!complete) {
if (klp_signals_cnt && !(klp_signals_cnt % SIGNALS_TIMEOUT))
klp_send_signals();
@@ -493,6 +536,18 @@ void klp_try_complete_transition(void)
return;
}
+exiting_done:
+ /*
+ * Tasks that already left the exit window run with preemption disabled
+ * until they finally stop in TASK_DEAD state, and they may still call
+ * patched functions such as __schedule(). Make sure that they stopped
+ * running before the code they may run is removed.
+ *
+ * synchronize_rcu() is not enough: __schedule() reports the RCU
+ * quiescent state before switch_to().
+ */
+ klp_synchronize_transition();
+
/* Done! Now cleanup the data structures. */
klp_resched_disable();
patch = klp_transition_patch;
@@ -551,12 +606,52 @@ void klp_start_transition(void)
klp_signals_cnt = 0;
}
+/*
+ * Wait for the tasks in the exit window to leave it. Return -EINTR if the
+ * current task gets a fatal signal first.
+ */
+static int klp_wait_exiting_tasks_killable(void)
+{
+ unsigned long cookie = start_poll_synchronize_rcu_tasks_exit();
+
+ while (!poll_state_synchronize_rcu_tasks_exit(cookie)) {
+ if (fatal_signal_pending(current))
+ return -EINTR;
+ schedule_timeout_killable(msecs_to_jiffies(10));
+ }
+ return 0;
+}
+
+/*
+ * Undo klp_init_transition() before any func->transition was set. Like
+ * klp_complete_transition(), this only resets the tasks on the task list
+ * and the idle tasks.
+ */
+static void klp_undo_init_transition(void)
+{
+ struct task_struct *g, *task;
+ unsigned int cpu;
+
+ read_lock(&tasklist_lock);
+ for_each_process_thread(g, task)
+ task->patch_state = KLP_TRANSITION_IDLE;
+ read_unlock(&tasklist_lock);
+
+ for_each_possible_cpu(cpu) {
+ task = idle_task(cpu);
+ task->patch_state = KLP_TRANSITION_IDLE;
+ }
+
+ klp_target_state = KLP_TRANSITION_IDLE;
+ klp_transition_patch = NULL;
+}
+
/*
* Initialize the global target patch state and all tasks to the initial patch
* state, and initialize all function transition states to true in preparation
* for patching or unpatching.
*/
-void klp_init_transition(struct klp_patch *patch, int state)
+int klp_init_transition(struct klp_patch *patch, int state)
{
struct task_struct *g, *task;
unsigned int cpu;
@@ -597,6 +692,28 @@ void klp_init_transition(struct klp_patch *patch, int state)
task->patch_state = initial_state;
}
+ /*
+ * Tasks in the exit window were not visited above, so their patch_state
+ * is stale. Once func->transition is set, klp_ftrace_handler() would
+ * warn about them, or run the wrong version of a function for them.
+ * Wait for them to leave the window. Tasks that enter it later were on
+ * the task list above. The wait can be long, or never end if a task is
+ * stuck in the window, so the caller can interrupt it with a fatal
+ * signal.
+ *
+ * Tasks that already left the window run with preemption disabled until
+ * they finally stop, and may still call functions such as __schedule().
+ * Make sure that they stopped running as well. synchronize_rcu() is not
+ * enough: __schedule() reports the RCU quiescent state before
+ * switch_to().
+ */
+ if (klp_wait_exiting_tasks_killable()) {
+ klp_undo_init_transition();
+ return -EINTR;
+ }
+ klp_synchronize_transition();
+ klp_exit_cookie_valid = false;
+
/*
* Enforce the order of the task->patch_state initializations and the
* func->transition updates to ensure that klp_ftrace_handler() doesn't
@@ -623,6 +740,8 @@ void klp_init_transition(struct klp_patch *patch, int state)
klp_for_each_object(patch, obj)
klp_for_each_func(obj, func)
func->transition = true;
+
+ return 0;
}
/*
@@ -666,6 +785,7 @@ void klp_reverse_transition(void)
* prepare for the reverse transition.
*/
klp_transition_patch->enabled = !klp_transition_patch->enabled;
+ klp_exit_cookie_valid = false;
klp_target_state = !klp_target_state;
/*
diff --git a/kernel/livepatch/transition.h b/kernel/livepatch/transition.h
index 322db16233de1..1070ac65abf56 100644
--- a/kernel/livepatch/transition.h
+++ b/kernel/livepatch/transition.h
@@ -6,7 +6,7 @@
extern struct klp_patch *klp_transition_patch;
-void klp_init_transition(struct klp_patch *patch, int state);
+int klp_init_transition(struct klp_patch *patch, int state);
void klp_cancel_transition(void);
void klp_start_transition(void);
void klp_try_complete_transition(void);
--
2.39.5
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v5 2/4] livepatch: Retry klp_synchronize_transition() if it fails
2026-12-05 18:08 ` [PATCH v5 2/4] livepatch: Retry klp_synchronize_transition() if it fails Binoy Ravindran via B4 Relay
@ 2026-12-05 18:22 ` sashiko-bot
0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-05 18:22 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,
Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Ben Segall, Mel Gorman, Valentin Schneider,
K Prateek Nayak
Cc: live-patching, Yafang Shao, Jaidev Shastri
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] livepatch: unbounded uninterruptible retry in
klp_synchronize_transition()
- [Low] livepatch: missing Fixes tag for klp_synchronize_transition()
bug fix
--
commit f48adcf8ba6e941661b90ee57576b67c0772ba78
Author: Binoy Ravindran <binoy@vt.edu>
> livepatch: Retry klp_synchronize_transition() if it fails
>
> schedule_on_each_cpu() returns -ENOMEM when it cannot allocate its work
> items, before it queues any of them. klp_synchronize_transition() ignores
> the error, so it can return without waiting. The transition relies on the
> wait in several places, including the two that the following patches add
> before func->transition is set and before the transition completes.
>
> Retry until the wait succeeds. The callers can sleep, since
> schedule_on_each_cpu() itself sleeps.
>
> Assisted-by: Claude:claude-opus-5-5
> Signed-off-by: Binoy Ravindran <binoy@vt.edu>
[Severity: Low]
This fixes the unchecked schedule_on_each_cpu() call introduced in
klp_synchronize_transition(). Should this include:
Fixes: 842c08846420 ("livepatch: Fix stacking of patches with respect to RCU")
> diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
> index 2351a19ac2a9e..33095e888e10c 100644
> --- a/kernel/livepatch/transition.c
> +++ b/kernel/livepatch/transition.c
[ ... ]
> @@ -71,7 +72,13 @@ static void klp_sync(struct work_struct *work)
> */
> static void klp_synchronize_transition(void)
> {
> - schedule_on_each_cpu(klp_sync);
> + /*
> + * schedule_on_each_cpu() fails only when it cannot allocate its work
> + * items, before it queues any of them. The callers rely on the wait,
> + * so retry until it succeeds.
> + */
> + while (schedule_on_each_cpu(klp_sync))
> + msleep(20);
> }
[Severity: Medium]
Can this loop livelock forever while holding klp_mutex?
schedule_on_each_cpu() calls alloc_percpu(), which takes
pcpu_alloc_mutex with mutex_lock_killable(). Under OOM, another caller
holding pcpu_alloc_mutex can loop in GFP_NOIO page allocation, while a
killed livepatch caller gets -ENOMEM from alloc_percpu() on every retry.
Even without contention, an OOM victim that cannot satisfy
pcpu_alloc_pages() from OOM reserves fails with -ENOMEM and loops
forever. Because msleep(20) is uninterruptible, the task cannot exit to
free memory and holds klp_mutex indefinitely, stalling module loading.
Since every caller of klp_synchronize_transition() already holds
klp_mutex, a static DEFINE_PER_CPU(struct work_struct, ...) driven with
cpus_read_lock(), schedule_work_on(), and flush_work() would avoid
dynamic allocation and the retry loop altogether.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-klp-series-v3-v5-0-3d60c8ea9316@vt.edu?part=2
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition
2026-12-05 18:08 ` [PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition Binoy Ravindran via B4 Relay
@ 2026-12-05 18:27 ` sashiko-bot
0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-05 18:27 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,
Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Ben Segall, Mel Gorman, Valentin Schneider,
K Prateek Nayak
Cc: live-patching, Yafang Shao, Jaidev Shastri
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] livepatch: klp_wait_exiting_tasks() checks per-patch forced
flag
- [High] livepatch: AB-BA deadlock between klp_mutex and
tasks_rcu_exit_srcu
- [High] livepatch: unprivileged PID namespace can pin
tasks_rcu_exit_srcu indefinitely
- [Medium] livepatch: unbacked-off 10 ms exit polling keeps
klp_sched_try_switch active
--
commit fe2e50e0784ef2725574105892343b959dcc0320
Author: Binoy Ravindran <binoy@vt.edu>
livepatch: Wait for exiting tasks around a transition
This commit waits for tasks in the do_exit() SRCU exit window before
starting and before completing a livepatch transition, using a killable
wait in klp_init_transition() and a polled wait in
klp_try_complete_transition().
Link: https://lore.kernel.org/live-patching/CALOAHbA9WHPjeZKUcUkwULagQjTMfqAdAg+akqPzbZ7Byc=qrw@mail.gmail.com/
> diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
> index 33095e888e10c..fb033483d02dd 100644
> --- a/kernel/livepatch/transition.c
> +++ b/kernel/livepatch/transition.c
> @@ -426,6 +426,36 @@ static void klp_send_signals(void)
> read_unlock(&tasklist_lock);
> }
>
> +/*
> + * Tasks in the exit window, between exit_tasks_rcu_start() and
> + * exit_tasks_rcu_finish(), are no longer on the task list, so the stack
> + * checks above do not see them. They may still run code that the cleanup
> + * is about to remove. Once every other task has switched, start an SRCU
> + * grace period for the window and poll it. Tasks do not switch back, so
> + * the retries only poll, every 10 ms, without walking the tasks again.
> + * klp_mutex is not held across the wait, a task stuck in the window delays
> + * the transition like any other task that cannot switch, and a forced
> + * transition does not wait for it.
> + *
> + * Return true if the transition must wait; it has then been rescheduled.
> + */
> +static unsigned long klp_exit_cookie;
> +static bool klp_exit_cookie_valid;
> +
> +static bool klp_wait_exiting_tasks(void)
> +{
> + if (klp_transition_patch->forced)
> + return false;
[Severity: High]
Does checking klp_transition_patch->forced work for forced enabling or
replace transitions?
klp_force_transition() only sets klp_transition_patch->forced when
klp_target_state == KLP_TRANSITION_UNPATCHED. For KLP_TRANSITION_PATCHED
transitions it stays false, so forcing an enable or replace transition
still loops here every 10 ms forever if a task is stuck in the exit window.
Conversely, klp_reverse_transition() leaves klp_transition_patch->forced
set. Reversing a replace patch to unpatching, forcing it, and reversing
back skips the wait while prior patches remain unforced, letting
klp_free_patch_finish() cause a use-after-free on exiting tasks.
Using a separate transition-scoped flag set in klp_force_transition() and
cleared in klp_init_transition() and klp_reverse_transition() would avoid
both problems.
> + if (!klp_exit_cookie_valid) {
> + klp_exit_cookie = start_poll_synchronize_rcu_tasks_exit();
> + klp_exit_cookie_valid = true;
> + }
> + if (poll_state_synchronize_rcu_tasks_exit(klp_exit_cookie))
> + return false;
> + schedule_delayed_work(&klp_transition_work, msecs_to_jiffies(10));
> + return true;
> +}
[Severity: Medium]
Should this polling back off instead of firing every 10 ms indefinitely?
If a task lingers in the exit window, rescheduling klp_transition_work
every unrounded 10 ms wakes a CPU and acquires klp_mutex 100 times per
second, preventing tickless idle and deep C-states (unlike the existing
round_jiffies_relative(HZ) retry).
In addition, klp_resched_disable() is not called until exiting_done, which
keeps klp_sched_try_switch_key enabled in __schedule() across all CPUs for
the entire wait even though all live and idle tasks have already switched.
[ ... ]
> @@ -551,12 +606,52 @@ void klp_start_transition(void)
> klp_signals_cnt = 0;
> }
>
> +/*
> + * Wait for the tasks in the exit window to leave it. Return -EINTR if the
> + * current task gets a fatal signal first.
> + */
> +static int klp_wait_exiting_tasks_killable(void)
> +{
> + unsigned long cookie = start_poll_synchronize_rcu_tasks_exit();
> +
> + while (!poll_state_synchronize_rcu_tasks_exit(cookie)) {
> + if (fatal_signal_pending(current))
> + return -EINTR;
> + schedule_timeout_killable(msecs_to_jiffies(10));
> + }
> + return 0;
> +}
[Severity: High]
Can an unprivileged user pin tasks_rcu_exit_srcu here indefinitely?
With unshare(CLONE_NEWUSER | CLONE_NEWPID), a parent can fork PID 1 and
PID 2 into the child namespace, let PID 1 exit, and never reap PID 2.
do_exit() calls exit_tasks_rcu_start() before zap_pid_ns_processes(), so
PID 1 blocks in TASK_IDLE while holding the tasks_rcu_exit_srcu read lock.
Any host livepatch enable or disable then hangs here holding klp_mutex,
blocking all module loads and unloads in klp_module_coming() and
klp_module_going() until killed, and any active transition stalls
indefinitely in klp_wait_exiting_tasks().
[ ... ]
> @@ -597,6 +692,28 @@ void klp_init_transition(struct klp_patch *patch, int state)
> task->patch_state = initial_state;
> }
>
> + /*
> + * Tasks in the exit window were not visited above, so their patch_state
> + * is stale. Once func->transition is set, klp_ftrace_handler() would
> + * warn about them, or run the wrong version of a function for them.
> + * Wait for them to leave the window. Tasks that enter it later were on
> + * the task list above. The wait can be long, or never end if a task is
> + * stuck in the window, so the caller can interrupt it with a fatal
> + * signal.
> + *
> + * Tasks that already left the window run with preemption disabled until
> + * they finally stop, and may still call functions such as __schedule().
> + * Make sure that they stopped running as well. synchronize_rcu() is not
> + * enough: __schedule() reports the RCU quiescent state before
> + * switch_to().
> + */
> + if (klp_wait_exiting_tasks_killable()) {
> + klp_undo_init_transition();
> + return -EINTR;
> + }
[Severity: High]
Can waiting for tasks_rcu_exit_srcu here under klp_mutex cause an ABBA
deadlock?
Task A (root ns) Task I (child init) Task B (child ns)
---------------- ------------------- -----------------
lock(&klp_mutex)
exit_tasks_rcu_start()
zap_pid_ns_processes()
SIGKILL -> Task B
stack_order_show()
lock(&klp_mutex)
<-- uninterruptible
wait for Task B
klp_wait_exiting_tasks_killable()
<-- waits for tasks_rcu_exit_srcu
*** DEADLOCK ***
Task B blocks uninterruptibly in stack_order_show() (world-readable sysfs)
or klp_module_coming()/klp_module_going() and ignores Task I's SIGKILL.
Task A is outside the child namespace so Task I never signals it; all three
tasks deadlock and module loading stalls on klp_mutex until Task A is killed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-klp-series-v3-v5-0-3d60c8ea9316@vt.edu?part=4
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section
2026-12-05 18:08 ` [PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section Binoy Ravindran via B4 Relay
@ 2026-12-05 18:28 ` sashiko-bot
0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-05 18:28 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,
Ingo Molnar, Peter Zijlstra, Juri Lelli, Vincent Guittot,
Dietmar Eggemann, Ben Segall, Mel Gorman, Valentin Schneider,
K Prateek Nayak
Cc: live-patching, Yafang Shao, Jaidev Shastri
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Low] rcu-tasks: exit SRCU wait APIs undefined if TASKS_RCU_GENERIC &&
!TASKS_RCU
- [Low] rcu-tasks: duplicate rcu_tasks_exit_srcu_idx field bloats
task_struct
- [Low] rcu-tasks: new exit wait APIs lack locking and exit-tail
documentation
--
commit 609ca5b3cbc8a384c9da27d490a20f664c23f2de
Author: Binoy Ravindran <binoy@vt.edu>
rcu-tasks: Make the exit window an SRCU read-side critical section
This commit wraps the do_exit() window between exit_tasks_rcu_start() and
exit_tasks_rcu_finish() in a new tasks_rcu_exit_srcu read-side critical
section and adds blocking and polled wait helpers for callers that need to
wait for exiting tasks to leave that window.
> diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
> index 3f74ae6d6e1f2..4442fbe9e9ac8 100644
> --- a/include/linux/rcupdate.h
> +++ b/include/linux/rcupdate.h
> @@ -204,6 +204,9 @@ void rcu_tasks_rude_torture_stats_print(char *tt, char *tf);
> #define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
> void exit_tasks_rcu_start(void);
> void exit_tasks_rcu_finish(void);
> +void synchronize_rcu_tasks_exit(void);
> +unsigned long start_poll_synchronize_rcu_tasks_exit(void);
> +bool poll_state_synchronize_rcu_tasks_exit(unsigned long cookie);
> #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
> #define rcu_tasks_classic_qs(t, preempt) do { } while (0)
> #define rcu_tasks_qs(t, preempt) do { } while (0)
> @@ -212,6 +215,9 @@ void exit_tasks_rcu_finish(void);
> #define synchronize_rcu_tasks synchronize_rcu
> static inline void exit_tasks_rcu_start(void) { }
> static inline void exit_tasks_rcu_finish(void) { }
> +static inline void synchronize_rcu_tasks_exit(void) { }
> +static inline unsigned long start_poll_synchronize_rcu_tasks_exit(void) { return 0; }
> +static inline bool poll_state_synchronize_rcu_tasks_exit(unsigned long cookie) { return true; }
> #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
[Severity: Low]
Can calling these three helpers fail to link when CONFIG_TASKS_RCU_GENERIC
is enabled without CONFIG_TASKS_RCU?
CONFIG_TASKS_RCU_GENERIC is enabled whenever CONFIG_TASKS_RUDE_RCU is set,
so these declarations are exposed as external symbols even when
CONFIG_TASKS_RCU is disabled. In kernel/rcu/tasks.h, however, they are only
defined under #ifdef CONFIG_TASKS_RCU, with no stubs in the #else branch.
Commit "livepatch: Select TASKS_RCU" later in this series avoids the link
error for livepatch, but the guard mismatch remains. Guarding the
declarations and stubs with #ifdef CONFIG_TASKS_RCU (or adding matching
definitions in kernel/rcu/tasks.h) would keep all configs linkable.
> diff --git a/include/linux/sched.h b/include/linux/sched.h
> index e4f6323d78555..0fe18976d1b40 100644
> --- a/include/linux/sched.h
> +++ b/include/linux/sched.h
> @@ -963,6 +963,7 @@ struct task_struct {
> struct list_head rcu_tasks_holdout_list;
> int rcu_tasks_exit_cpu;
> struct list_head rcu_tasks_exit_list;
> + int rcu_tasks_exit_srcu_idx;
> #endif /* #ifdef CONFIG_TASKS_RCU */
[Severity: Low]
Could this reuse the existing u8 rcu_tasks_idx field in the same
#ifdef CONFIG_TASKS_RCU block instead of adding rcu_tasks_exit_srcu_idx?
rcu_tasks_idx has had no remaining users since commit 6b70399f9ef3
("rcu-tasks: Maintain lists to eliminate RCU-tasks/do_exit() deadlocks"),
and __srcu_read_lock() only returns 0 or 1. Appending an int after
rcu_tasks_exit_list needlessly grows struct task_struct by 8 bytes (with
padding) on 64-bit builds without CONFIG_TASKS_TRACE_RCU and
CONFIG_TRIVIAL_PREEMPT_RCU.
> diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
> index fcac7361ec51e..57a16d7c33122 100644
> --- a/kernel/rcu/tasks.h
> +++ b/kernel/rcu/tasks.h
> @@ -1288,6 +1288,32 @@ void rcu_tasks_get_gp_data(int *flags, unsigned long *gp_seq)
> }
> EXPORT_SYMBOL_GPL(rcu_tasks_get_gp_data);
>
> +/*
> + * Tasks between exit_tasks_rcu_start() and exit_tasks_rcu_finish() are
> + * no longer on the task list, but they still run kernel code. This SRCU
> + * domain lets other subsystems wait for every task in that window to
> + * leave it.
> + */
> +DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
> +
> +/* Wait for all tasks currently in the exit window to leave it. */
> +void synchronize_rcu_tasks_exit(void)
> +{
> + synchronize_srcu(&tasks_rcu_exit_srcu);
> +}
> +
> +/* Start waiting for the tasks in the exit window, without blocking. */
> +unsigned long start_poll_synchronize_rcu_tasks_exit(void)
> +{
> + return start_poll_synchronize_srcu(&tasks_rcu_exit_srcu);
> +}
> +
> +/* Have all tasks that were in the exit window at @cookie left it? */
> +bool poll_state_synchronize_rcu_tasks_exit(unsigned long cookie)
> +{
> + return poll_state_synchronize_srcu(&tasks_rcu_exit_srcu, cookie);
> +}
[Severity: Low]
Should these new public helpers carry kernel-doc covering their calling
contexts, locking constraints, and exit-tail semantics, like
synchronize_rcu_tasks()?
exit_tasks_rcu_start() calls __srcu_read_lock() directly to bypass lockdep
(do_exit() checks debug_check_no_locks_held() inside the window). Holding
any lock acquired in late do_exit() across synchronize_rcu_tasks_exit() will
deadlock silently, as in commit 1612160b9127 ("rcu-tasks: Eliminate
deadlocks involving do_exit() and RCU tasks").
In addition, exit_tasks_rcu_finish() runs inside the final preempt_disable()
section before lockdep_free_task() and do_task_dead(), so completing this
SRCU grace period alone does not guarantee exiting tasks are off the CPU;
callers needing that still require a follow-up CPU synchronization step.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261205-klp-series-v3-v5-0-3d60c8ea9316@vt.edu?part=1
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-05 18:28 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 18:08 [PATCH v5 0/4] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section Binoy Ravindran via B4 Relay
2026-12-05 18:28 ` sashiko-bot
2026-12-05 18:08 ` [PATCH v5 2/4] livepatch: Retry klp_synchronize_transition() if it fails Binoy Ravindran via B4 Relay
2026-12-05 18:22 ` sashiko-bot
2026-12-05 18:08 ` [PATCH v5 3/4] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay
2026-12-05 18:08 ` [PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition Binoy Ravindran via B4 Relay
2026-12-05 18:27 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox