From: Petr Mladek <pmladek@suse.com>
To: binoy@vt.edu
Cc: "Paul E. McKenney" <paulmck@kernel.org>,
Frederic Weisbecker <frederic@kernel.org>,
Neeraj Upadhyay <neeraj.upadhyay@kernel.org>,
Joel Fernandes <joelagnelf@nvidia.com>,
Josh Triplett <josh@joshtriplett.org>,
Boqun Feng <boqun@kernel.org>,
Uladzislau Rezki <urezki@gmail.com>,
Steven Rostedt <rostedt@goodmis.org>,
Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
Lai Jiangshan <jiangshanlai@gmail.com>,
Zqiang <qiang.zhang@linux.dev>,
Josh Poimboeuf <jpoimboe@kernel.org>,
Miroslav Benes <mbenes@suse.cz>,
Joe Lawrence <joe.lawrence@redhat.com>,
Song Liu <song@kernel.org>, Ingo Molnar <mingo@kernel.org>,
Jiri Kosina <jikos@kernel.org>,
rcu@vger.kernel.org, linux-kernel@vger.kernel.org,
live-patching@vger.kernel.org, Yafang Shao <laoar.shao@gmail.com>,
Jaidev Shastri <jaidevshastri@vt.edu>
Subject: Re: [PATCH v4 2/3] livepatch: Include exiting tasks in the transition
Date: Fri, 27 Nov 2026 17:10:38 +0100 [thread overview]
Message-ID: <awmrfuzTlVWgGgEM@pathway.suse.cz> (raw)
In-Reply-To: <20261126-klp-series-v3-v4-2-2ad73e9c9254@vt.edu>
On Thu 2026-11-26 11:41:33, Binoy Ravindran via B4 Relay wrote:
> 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.
I would use [1] to reference the discussion, see 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.
I would start another paragraph here.
> 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.
I was confused by the meaning of "tasks still hashed".
Later I realized that it pointed to __unhash_process() function.
I heard about this function for the first time. I have never
met a term "hashed process" before. I never needed to study pids this
deeply.
Honestly, I think that "hashed task" is a term invented by LLMS.
It does not mean much. It is just a "poorly" chosen helper
function name in kernel/exit.c. There is no counter part
in kernel/fork.c...
Instead, I would write something like: "skip tasks which are still
accessible via the for_each_process_thread() walk, so that each
task is visited only once."
BTW: Would it hurt to process the same task twice?
I think that it should not be a big deal. It looks like an
optimization. But the dependence between pid_alive(),
for_each_process_thread(), and task_list lock is pretty
tricky. I am not sure if it is worth relying on it.
Also I would start another paragraph here.
> 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.
>
> Fixes: d83a7cb375ee ("livepatch: change to a per-task consistency model")
> Link: https://lore.kernel.org/all/20250127063526.76687-1-laoar.shao@gmail.com/
This points to the cover letter of a patchset. Many things were
discussed there. It might be better to mention here the mail
where the discussion about livepatching do_exit() started.
Also I would use:
[1] https://lore.kernel.org/...
instead of "Link: " so that [1] can be used as a reference in
the text above.
> Assisted-by: Claude:claude-opus-5-5
This is not needed.
> Signed-off-by: Binoy Ravindran <binoy@vt.edu>
> --- a/kernel/livepatch/transition.c
> +++ b/kernel/livepatch/transition.c
> @@ -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.
I would be more explicit here.
* Important: Both for_each_process_thread() and rcu_tasks_exit_list
* must be traversed under tasklist_lock. It prevents calling
* __unhash_process() so that pid_alive() can be used to detect
* whether the task has already been proceed in
* for_each_process_thread() cycle.
But as I have said, I am not sure of it is worth 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)
The name of other callbacks seems to follow the name of the function
where they are called. This one is called in klp_reverse_transition()
so I would call it:
klp_exiting_reverse();
> +{
> + 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);
> +}
That said, the names look a bit confusing. It feels like these
callbacks are initializing, starting, completing, ... some exit.
I would use:
+ prefix: "klp_exit_task_*" to make it more clear
that it is manipulating a task in some exit state.
+ same suffix as the caller, .e.g "init_transition" to
make it clear which stage of the transition it
manipulates.
So, rename:
+ klp_exiting_init -> klp_exit_task_init_transition
+ klp_exiting_start -> klp_exit_task_start_transition
+ klp_exiting_complete -> klp_exit_task_complete_transition
...
> +
> /*
> * 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;
> +}
I did not check this function. I think that we will do it another way,
see my reply to the 1st patch.
> /*
> * 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,13 @@ 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 wait until
> + * every CPU has switched context.
Just imagine that you see this comment 2 years from now without
the fresh context. "exit list" sounds a bit vague. Also "the snapshot"
is a term that was not mentioned anywhere else.
I would be more precise. I suggest something like:
* There might still be tasks which were already removed from
* rcu_tasks_exit_list and were not checked above. They run with
* the preemption disabled until they finally stop in TASK_DEAD state.
* Make sure that they stopped running and will never run again.
Now the question is why we need to call it here and not,
for example, in klp_start_transition() or klp_reverse_transition().
IMHO, we need it here to avoid removing the ftrace trampolines and
the livepatch module when the code still might be in use.
BTW: Do we need it in all situations, for example, when the 1st
livepatch gets enabled? In this case, nothing is removed.
Anyway, we should explain in the comment why exactly we need
the synchronization here.
> synchronize_rcu() is not enough:
> + * __schedule() reports the quiescent state before switch_to().
I would say that synchronize_rcu() is not enough by definition.
We are waiting for tasks which are not longer visible for RCU.
But I agree that the comment above rcu_tasks_postgp() might be confusing.
I guess that RCU does not care about tracking the rest of __schedule().
So, it makes sense to keep the comment here. I would just add it into
a separate paragraph (after an empty line).
> + */
> + klp_synchronize_transition();
Hmm, I would move this up, above the comment: "/* Done! Now cleanup
the data structures. */". Let's be on the safe side and
make sure that all tasks are safe before we start the clean up...
Or is there any reason why it is done here?
> klp_complete_transition();
>
> /*
> @@ -526,6 +614,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 +668,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 +680,16 @@ 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, which it makes
> + * with preemption disabled. This returns only after every CPU has
> + * switched context, so every such task is gone. synchronize_rcu() is
> + * not enough: __schedule() reports the quiescent state before
> + * switch_to().
> + */
> + klp_synchronize_transition();
Why exactly do we need the synchronization here, please?
We should not need this for livepatching of do_exit(). We do not care
which do_exit() implementation is used by the about-to-die processes
at this stage.
The only problem might be if we want to livepatch a function which
is called in do_exit() after the process gets removed from the
rcu_tasks_exit_list. It might be only:
+ raw_spin_unlock_irqrestore_rcu_node()
+ lockdep_free_task()
+ do_task_dead()
+ set_special_state()
+ __schedule()
They all might start calling klp_ftrace_handler() and could need to
see consistent state current->patch_state.
But wait! If this is the problem then we might need to call
klp_synchronize_transition() in each stage which calls
rcu_tasks_visit_exiting(). Otherwise, the klp_ftrace_handler()
might see some inconsistence and trigger a WARN() or
use an inconsistent task->patch_state.
> /*
> * Enforce the order of the task->patch_state initializations and the
> * func->transition updates to ensure that klp_ftrace_handler() doesn't
> @@ -642,6 +742,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 +815,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)
Best Regards,
Petr
PS: It is an amazing work for a newcomer.
next prev parent reply other threads:[~2026-11-27 16:10 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-26 16:41 [PATCH v4 0/3] livepatch: Do not lose exiting tasks during a transition Binoy Ravindran via B4 Relay
2026-11-26 16:41 ` [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists Binoy Ravindran via B4 Relay
2026-11-26 18:52 ` Petr Mladek
2026-11-27 18:56 ` Ravindran, Binoy
2026-11-26 16:41 ` [PATCH v4 2/3] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay
2026-11-27 16:10 ` Petr Mladek [this message]
2026-11-27 19:30 ` Ravindran, Binoy
2026-11-26 16:41 ` [PATCH v4 3/3] livepatch: Select TASKS_RCU Binoy Ravindran via B4 Relay
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=awmrfuzTlVWgGgEM@pathway.suse.cz \
--to=pmladek@suse.com \
--cc=binoy@vt.edu \
--cc=boqun@kernel.org \
--cc=frederic@kernel.org \
--cc=jaidevshastri@vt.edu \
--cc=jiangshanlai@gmail.com \
--cc=jikos@kernel.org \
--cc=joe.lawrence@redhat.com \
--cc=joelagnelf@nvidia.com \
--cc=josh@joshtriplett.org \
--cc=jpoimboe@kernel.org \
--cc=laoar.shao@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=live-patching@vger.kernel.org \
--cc=mathieu.desnoyers@efficios.com \
--cc=mbenes@suse.cz \
--cc=mingo@kernel.org \
--cc=neeraj.upadhyay@kernel.org \
--cc=paulmck@kernel.org \
--cc=qiang.zhang@linux.dev \
--cc=rcu@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=song@kernel.org \
--cc=urezki@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox