Re: [PATCH v4 1/3] rcu-tasks: Add helpers to visit tasks on the exit lists

From: Paul E. McKenney

Date: Mon Nov 30 2026 - 20:24:24 EST


On Tue, Dec 01, 2026 at 12:29:12AM +0000, Ravindran, Binoy wrote:
>
> On Mon, Nov 30, 2026, Paul E. McKenney wrote:
> > I instead suggest keeping your overall algorithm, including your walk
> > of the tasks list, and replacing the call to your Tasks-RCU helper
> > function with synchronize_rcu_tasks(). This does not wait for tasks
> > with preemption disabled that are working towards their last call
> > to schedule(), but those can be handled with a subsequent call to
> > synchronize_rcu_tasks_rude().
>
> Thanks, and sorry for misreading your first question. I see what you
> mean now, and I tried the core of it.
>
> The test kernel is v7.4-rc2 with that change and nothing else from our
> series. The transition walks the task list as before, and it does not
> walk the exit lists. Once every task it finds has switched,
> klp_try_complete_transition() calls synchronize_rcu_tasks() and then
> synchronize_rcu_tasks_rude(), just before the cleanup.
>
> To trigger the race, a test-only hook makes exiting tasks sleep after
> exit_notify(). Two livepatches that both replace do_exit() replace
> each other in a loop, and the old module is removed each time.
>
> With a 2 s sleep, all 12 runs crashed. In every run, an exiting task
> woke up and went back into memory that the removed module had used.
> That memory was unmapped, filled with int3 after the free, or already
> holding the next module's code.
>
> With a 20 ms sleep, all 12 runs finished their cycles, 120 in all,
> without a fault. Without the two waits, the same 20 ms test crashed in
> 11 of 12 runs.
>
> So the waits only delay the cleanup. A short sleep can end in time,
> but a long one does not. The reason is that a task asleep in do_exit()
> after exit_notify() is in an RCU Tasks quiescent state, so the grace
> period does not wait for it. The rude flavor does not help either,
> because it does not cover a sleeping task.
>
> That is why the series has the transition find these tasks on the
> exit lists and check their stacks, as it does for every other task.

Thank you for trying this out.

And part of my problem was that I was remembering the old versions
of exit_tasks_rcu_start() and exit_tasks_rcu_finish(), for example,
from Linux kernel v6.0. In these old versions, exit_tasks_rcu_start()
entered an SRCU read-side critical section, exit_tasks_rcu_finish()
exited it, and rcu_tasks_postscan() did synchronize_srcu().

Apologies for my confusion, but so many RCU versions! ;-)

Would it work to add those __srcu_read_lock() and __srcu_read_unlock()
calls to exit_tasks_rcu_start() and exit_tasks_rcu_finish(),
respectively, then have your code do synchronize_srcu() instead of
synchronize_rcu_tasks(), presumably by invoking a helper function in
kernel/rcu/tasks.h?

Some thought and/or experiment maybe required to come up with the optimal
variant of __srcu_read_lock() and __srcu_read_unlock(), but those are
good places to start.

This assumes that your code would not be holding mutexes across my
proposed synchronize_srcu() that are also acquired by do_exit() between
its calls to exit_tasks_rcu_start() and exit_tasks_rcu_finish(). See
here for more details:

1612160b9127 ("rcu-tasks: Eliminate deadlocks involving do_exit() and RCU tasks")

If your code does need to hold such mutexes, back to the drawing board.

Thanx, Paul