* [PATCH 1/2] rcu-tasks: Add helpers to visit tasks on the exit lists
2026-11-23 20:52 [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
@ 2026-11-23 20:52 ` Binoy Ravindran via B4 Relay
2026-11-23 20:52 ` [PATCH 2/2] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay
2026-11-23 22:40 ` [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition Ravindran, Binoy
2 siblings, 0 replies; 6+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-23 20:52 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. While on the list 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.
Reviewed-by: Jaidev Shastri <jaidevshastri@vt.edu>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
include/linux/rcupdate.h | 9 +++++++
kernel/rcu/tasks.h | 67 +++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 70 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..a231d30ebf81 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -49,6 +49,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 +1069,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 +1081,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 +1333,60 @@ void exit_tasks_rcu_finish(void)
raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
}
+/*
+ * Call fn on every task on the exit lists, in place, with the per-CPU list
+ * lock held and interrupts disabled. fn must not sleep. Does not allocate.
+ * These are the tasks that have passed exit_tasks_rcu_start() but not
+ * exit_tasks_rcu_finish(). Once exit_notify() has run they are no longer on
+ * the task list, but they still run do_exit() and may sleep there.
+ */
+void rcu_tasks_visit_exiting(rcu_tasks_exiting_fn_t fn, void *arg)
+{
+ int cpu;
+ unsigned long flags;
+ struct task_struct *t;
+
+ for_each_possible_cpu(cpu) {
+ struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
+
+ raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
+ list_for_each_entry(t, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) {
+ if (&t->rcu_tasks_exit_list == &rtpcp->rtp_exit_cursor)
+ continue;
+ fn(t, arg);
+ }
+ raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
+ }
+}
+
+/*
+ * Copy up to max tasks on the exit lists into buf, taking a reference on
+ * each. Returns the number of tasks on the lists, which may exceed max.
+ */
+int rcu_tasks_snapshot_exiting(struct task_struct **buf, int max)
+{
+ int cpu, n = 0;
+ unsigned long flags;
+ struct task_struct *t;
+
+ for_each_possible_cpu(cpu) {
+ struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
+
+ raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
+ list_for_each_entry(t, &rtpcp->rtp_exit_list, rcu_tasks_exit_list) {
+ if (&t->rcu_tasks_exit_list == &rtpcp->rtp_exit_cursor)
+ continue;
+ if (n < max) {
+ get_task_struct(t);
+ buf[n] = t;
+ }
+ n++;
+ }
+ raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
+ }
+ return n;
+}
+
#else /* #ifdef CONFIG_TASKS_RCU */
void exit_tasks_rcu_start(void) { }
void exit_tasks_rcu_finish(void) { }
--
2.39.5
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH 2/2] livepatch: Include exiting tasks in the transition
2026-11-23 20:52 [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-11-23 20:52 ` [PATCH 1/2] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay
@ 2026-11-23 20:52 ` Binoy Ravindran via B4 Relay
2026-11-23 22:40 ` [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition Ravindran, Binoy
2 siblings, 0 replies; 6+ messages in thread
From: Binoy Ravindran via B4 Relay @ 2026-11-23 20:52 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.
Make every step of a transition also visit the tasks on the RCU-tasks
exit 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, so they walk the lists in place under the list locks and cannot
fail. 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.
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.
After exit_tasks_rcu_finish() the task runs with preemption disabled
until its final switch, so it cannot sleep or be preempted while it is
off both lists. Kernels without CONFIG_TASKS_RCU have no exit lists, so
this change does not cover them.
Testing was done on v6.12.1, x86-64, under QEMU/KVM; this posting is a
forward port of that change. The test alternates two atomic-replace
livepatches that both replace do_exit() while self-reaping tasks exit
continuously, with a debug-only msleep() after exit_notify() to widen
the window. Without this series, 11 of 12 runs crash in the removed
module; with it, 0 of 12, and 0 of 12 for a control pair that does not
patch do_exit(). With the series, there were also no crashes in 1,200
replace cycles of a /proc mount/unmount stress test, or in 400 cycles
with preempt=full, with and without a real-time task contending for the
CPU of a busy-waiting exiting task. Without the delay, the series did
not change transition time: the median was 0.29 s with it and 0.27 to
0.31 s per run without it. The series does not change the exit path
itself.
Fixes: d83a7cb375ee ("livepatch: change to a per-task consistency model")
Link: https://lore.kernel.org/all/20250127063526.76687-1-laoar.shao@gmail.com/
Reviewed-by: Jaidev Shastri <jaidevshastri@vt.edu>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@vt.edu>
---
kernel/livepatch/transition.c | 72 +++++++++++++++++++++++++++++++++++++++++++
1 file changed, 72 insertions(+)
diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c
index 2351a19ac2a9..cab167417190 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,70 @@ static void klp_synchronize_transition(void)
schedule_on_each_cpu(klp_sync);
}
+static bool klp_try_switch_task(struct task_struct *task);
+
+/*
+ * 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 it works on a snapshot of the exit lists.
+ */
+static bool klp_visit_exiting_tasks(rcu_tasks_exiting_fn_t fn, void *arg)
+{
+ struct task_struct **xt;
+ 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++) {
+ fn(xt[i], arg);
+ put_task_struct(xt[i]);
+ }
+ kvfree(xt);
+ return n <= max;
+}
+
+static void klp_exiting_init(struct task_struct *t, void *arg)
+{
+ t->patch_state = *(int *)arg;
+}
+
+static void klp_exiting_start(struct task_struct *t, void *arg)
+{
+ if (t->patch_state != klp_target_state)
+ set_tsk_thread_flag(t, TIF_PATCH_PENDING);
+}
+
+static void klp_exiting_try(struct task_struct *t, void *arg)
+{
+ if (!klp_try_switch_task(t))
+ *(bool *)arg = false;
+}
+
+static void klp_exiting_complete(struct task_struct *t, void *arg)
+{
+ 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)
+{
+ clear_tsk_thread_flag(t, TIF_PATCH_PENDING);
+}
+
+static void klp_exiting_force(struct task_struct *t, void *arg)
+{
+ klp_update_patch_state(t);
+}
+
/*
* The transition to the target patch state is complete. Clean up the data
* structures.
@@ -124,6 +189,7 @@ static void klp_complete_transition(void)
task->patch_state = KLP_TRANSITION_IDLE;
}
read_unlock(&tasklist_lock);
+ rcu_tasks_visit_exiting(klp_exiting_complete, NULL);
for_each_possible_cpu(cpu) {
task = idle_task(cpu);
@@ -450,6 +516,8 @@ void klp_try_complete_transition(void)
if (!klp_try_switch_task(task))
complete = false;
read_unlock(&tasklist_lock);
+ if (!klp_visit_exiting_tasks(klp_exiting_try, &complete))
+ complete = false;
/*
* Ditto for the idle "swapper" tasks.
@@ -527,6 +595,7 @@ void klp_start_transition(void)
if (task->patch_state != klp_target_state)
set_tsk_thread_flag(task, TIF_PATCH_PENDING);
read_unlock(&tasklist_lock);
+ rcu_tasks_visit_exiting(klp_exiting_start, NULL);
/*
* Mark all idle tasks as needing a patch state update. They'll switch
@@ -580,6 +649,7 @@ void klp_init_transition(struct klp_patch *patch, int state)
task->patch_state = initial_state;
}
read_unlock(&tasklist_lock);
+ rcu_tasks_visit_exiting(klp_exiting_init, &initial_state);
/*
* Ditto for the idle "swapper" tasks.
@@ -643,6 +713,7 @@ void klp_reverse_transition(void)
for_each_process_thread(g, task)
clear_tsk_thread_flag(task, TIF_PATCH_PENDING);
read_unlock(&tasklist_lock);
+ rcu_tasks_visit_exiting(klp_exiting_clear_pending, NULL);
for_each_possible_cpu(cpu)
clear_tsk_thread_flag(idle_task(cpu), TIF_PATCH_PENDING);
@@ -715,6 +786,7 @@ void klp_force_transition(void)
for_each_process_thread(g, task)
klp_update_patch_state(task);
read_unlock(&tasklist_lock);
+ rcu_tasks_visit_exiting(klp_exiting_force, NULL);
for_each_possible_cpu(cpu)
klp_update_patch_state(idle_task(cpu));
--
2.39.5
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition
2026-11-23 20:52 [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-11-23 20:52 ` [PATCH 1/2] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay
2026-11-23 20:52 ` [PATCH 2/2] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay
@ 2026-11-23 22:40 ` Ravindran, Binoy
2026-11-24 4:00 ` Yafang Shao
2 siblings, 1 reply; 6+ messages in thread
From: Ravindran, Binoy @ 2026-11-23 22:40 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, Ravindran, Binoy
Cc: rcu, linux-kernel, live-patching, Yafang Shao, Shastri, Jaidev
The two issues Sashiko reported on patch 2 are valid:
1. An exiting task that has not been unhashed is on both the task list
and an exit list, so klp_start_transition() can visit it twice and
set TIF_PATCH_PENDING again after it has switched.
2. Between exit_tasks_rcu_finish() and its final switch, an exiting
task is on neither list and can still enter patched code with a
stale patch_state.
v2 will walk the exit lists under tasklist_lock and skip tasks that are
still hashed, so each task is visited exactly once. It will also call
klp_synchronize_transition() before func->transition is set and before
completion, to wait out tasks in that last stretch. It will document
rtp_exit_cursor as well.
I'll post v2 after testing it on mainline. Please hold off on
reviewing v1.
Thanks,
Binoy
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition
2026-11-23 22:40 ` [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition Ravindran, Binoy
@ 2026-11-24 4:00 ` Yafang Shao
2026-11-24 4:57 ` Ravindran, Binoy
0 siblings, 1 reply; 6+ messages in thread
From: Yafang Shao @ 2026-11-24 4:00 UTC (permalink / raw)
To: Ravindran, Binoy
Cc: Paul E. McKenney, Frederic Weisbecker, Neeraj Upadhyay,
Joel Fernandes, Josh Triplett, Boqun Feng, Uladzislau Rezki,
Steven Rostedt, Mathieu Desnoyers, Lai Jiangshan, Zqiang,
Josh Poimboeuf, Miroslav Benes, Petr Mladek, Joe Lawrence,
Song Liu, Ingo Molnar, Jiri Kosina, rcu, linux-kernel,
live-patching, Shastri, Jaidev
On Tue, Nov 24, 2026 at 6:40 AM Ravindran, Binoy <binoy@vt.edu> wrote:
>
Hello Ravindran,
Thanks for driving this fix.
>
> The two issues Sashiko reported on patch 2 are valid:
>
> 1. An exiting task that has not been unhashed is on both the task list
> and an exit list, so klp_start_transition() can visit it twice and
> set TIF_PATCH_PENDING again after it has switched.
>
> 2. Between exit_tasks_rcu_finish() and its final switch, an exiting
> task is on neither list and can still enter patched code with a
> stale patch_state.
We also discussed this issue at the thread:
https://lore.kernel.org/live-patching/CALOAHbA9WHPjeZKUcUkwULagQjTMfqAdAg+akqPzbZ7Byc=qrw@mail.gmail.com/
and Josh suggested something along the lines of what you are proposing,
https://lore.kernel.org/live-patching/20250124010848.pdcxokfsk2ngrowl@jpoimboe/
I did not take it for the same reason Sashiko raised in issue 2: the
RCU exit list narrows the race window but does not close it.
I could not see how klp_synchronize_transition(), as you describe
below, completely resolves it. Could you please explain that in
detail?
>
> v2 will walk the exit lists under tasklist_lock and skip tasks that are
> still hashed, so each task is visited exactly once. It will also call
> klp_synchronize_transition() before func->transition is set and before
> completion, to wait out tasks in that last stretch. It will document
> rtp_exit_cursor as well.
>
> I'll post v2 after testing it on mainline. Please hold off on
> reviewing v1.
--
Regards
Yafang
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH 0/2] livepatch: Do not lose exiting tasks during a transition
2026-11-24 4:00 ` Yafang Shao
@ 2026-11-24 4:57 ` Ravindran, Binoy
0 siblings, 0 replies; 6+ messages in thread
From: Ravindran, Binoy @ 2026-11-24 4:57 UTC (permalink / raw)
To: 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, live-patching, Petr Mladek,
Joe Lawrence, Song Liu, Ingo Molnar, Jiri Kosina, rcu,
linux-kernel, live-patching, Shastri, Jaidev
Hi Yafang,
Thanks for the pointers to the earlier thread and to Josh's
suggestion.
> I did not take it for the same reason Sashiko raised in issue 2: the
> RCU exit list narrows the race window but does not close it.
> I could not see how klp_synchronize_transition(), as you describe
> below, completely resolves it. Could you please explain that in
> detail?
The exit list alone does not close it, and v2 does not rely on it
alone. The exit list covers the part of the exit path that can sleep.
The part it misses cannot sleep or be preempted, and
klp_synchronize_transition() waits it out. In detail:
1. Where the gap is. An exiting task is on an exit list from
exit_tasks_rcu_start() to exit_tasks_rcu_finish(). exit_notify()
runs inside that range, and the task is unhashed no earlier than
exit_notify(). So until exit_tasks_rcu_finish() the task is on the
task list, an exit list, or both. After exit_tasks_rcu_finish() it
may be on neither, if it has already been unhashed. That is the
gap in issue 2.
2. What the task can do in the gap. do_exit() calls preempt_disable()
before exit_rcu() and exit_tasks_rcu_finish(). After that the task
runs only lockdep_free_task() and do_task_dead(), whose __schedule()
is its last. From preempt_disable() on, the task stays on its CPU:
it cannot sleep, be preempted, or migrate. So once a task is in the
gap, the next context switch on its CPU is the task's final one.
3. What klp_synchronize_transition() guarantees. It calls
schedule_on_each_cpu() and returns only after a work item has run
on every online CPU, so every CPU has switched context since the
call began. Any task that was in the gap when the call began has
reached its final __schedule() and will never run again.
4. Where v2 calls it.
a. In klp_init_transition(), after patch_state has been set on
every task (the task list and the exit lists, walked under
tasklist_lock) and before func->transition is set. A task that
the walk did not visit had left the exit lists before or during
the walk, so it was in the gap when the sync began, and it is
dead before any function enters the transition. So when
func->transition is set, every task that can still run has a
valid patch_state.
b. In klp_try_complete_transition(), before it calls
klp_complete_transition(). A task in the gap can still be
running code reached through the old function, and the switch
check cannot see it. The sync makes it dead before
func->transition is cleared and before the old patch can be
freed. A task that leaves the exit lists after this sync was
already counted as switched, so it runs the new code.
So there is no point at which a task that can still run patched code
is invisible to the transition: before exit_tasks_rcu_finish() it is
on a list the transition walks, and after it, the sync waits for it to
die.
One limit: the exit lists exist only with CONFIG_TASKS_RCU. Without
it, the helpers are empty and only the final non-preemptible stretch
is covered.
I tested v2 on v7.4-rc2 with two klp-build livepatches that each
replace do_exit() and replace each other ten times per run while
short-lived tasks exit, with a test-only 2 s delay after
exit_notify(). Without the series, 9 of 12 runs crashed: the last
transition finished in about 2 ms, the old patch's module was
unloaded, and the delayed task then faulted executing freed module
memory. With the series, all 12 runs completed their 120 replace
cycles without a fault. I will put this argument in the v2 changelog.
Thanks,
Binoy
^ permalink raw reply [flat|nested] 6+ messages in thread