Re: [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists
From: Petr Mladek
Date: Thu Nov 26 2026 - 13:52:20 EST
On Thu 2026-11-26 11:41:32, Binoy Ravindran via B4 Relay wrote:
> From: Binoy Ravindran <binoy@xxxxxx>
>
> 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. Once exit_notify() has unhashed it, 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.
Nit: I would be nice to make the commit messages better human readable.
The dense paragraphs generated by AI are not easy to read. I mean
something like:
Add two helpers:
1. rcu_tasks_visit_exiting() allows to do a quick operation on
all exiting processes. It will be needed, for example, for...
2. rcu_tasks_snapshot_exiting() ...
> 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.
>
> --- a/kernel/rcu/tasks.h
> +++ b/kernel/rcu/tasks.h
> @@ -33,6 +33,8 @@ typedef void (*postgp_func_t)(struct rcu_tasks *rtp);
> * @barrier_q_head: RCU callback for barrier operation.
> * @rtp_blkd_tasks: List of tasks blocked as readers.
> * @rtp_exit_list: List of tasks in the latter portion of do_exit().
> + * @rtp_exit_cursor: Placeholder that rcu_tasks_postscan() parks on @rtp_exit_list
> + * while it drops the lock, so that exit-list walkers can skip it.
> * @cpu: CPU number corresponding to this entry.
> * @index: Index of this CPU in rtpcp_array of the rcu_tasks structure.
> * @rtpp: Pointer to the rcu_tasks structure.
> @@ -49,6 +51,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 +1071,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 +1083,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 +1335,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);
Is it acceptable to process all entries while keeping the raw_spin_lock()?
rcu_tasks_postscan() contains the trick with the @tmp/@rtp_exit_cursor
list_head to be able to release the spin lock while walking the list.
You might argue that our fn() is short. But rcu_tasks_postscan() calls
rcu_tasks_pertask() in the loop and it seems to be quite simple as well.
IMHO, a better approach would be to make rcu_tasks_postscan() generic
function to do an operation on the rcu_tasks_exit_list and release
the spin lock regularly. The operation should be defined via a *fn
passed as a parameter.
I mean to allow to use the same RT-friendly for-cycle in both RCU
and livepatching code paths.
We might need more cursors. One for the RCU code path and
another for the livepatching code path.
It will be tricky. But it will remove the hack with copying the list
of exiting processes into a temporary allocated array. The allocation
and copying is problematic on its own. IMHO, it might cause a livelock
when many processes are exiting...
Best Regards,
Petr