[PATCH v5 4/4] livepatch: Wait for exiting tasks around a transition

From: Binoy Ravindran via B4 Relay

Date: Sat Dec 05 2026 - 13:09:24 EST


From: Binoy Ravindran <binoy@xxxxxx>

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@xxxxxxxxxxxxxx/
Suggested-by: Paul E. McKenney <paulmck@xxxxxxxxxx>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@xxxxxx>
---
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