mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists
Date: Thu, 26 Nov 2026 19:52:12 +0100	[thread overview]
Message-ID: <awh_3OkWvI7dIRA-@pathway.suse.cz> (raw)
In-Reply-To: <20261126-klp-series-v3-v4-1-2ad73e9c9254@vt.edu>

On Thu 2026-11-26 11:41:32, Binoy Ravindran via B4 Relay wrote:
> 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. 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

  reply	other threads:[~2026-11-26 18:52 UTC|newest]

Thread overview: 5+ 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 [this message]
2026-11-26 16:41 ` [PATCH v4 2/3] livepatch: Include exiting tasks in the transition Binoy Ravindran via B4 Relay
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=awh_3OkWvI7dIRA-@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

all inboxes | Powered by JetHome®