[PATCH v5 1/4] rcu-tasks: Make the exit window an SRCU read-side critical section

From: Binoy Ravindran via B4 Relay

Date: Sat Dec 05 2026 - 13:09:24 EST


From: Binoy Ravindran <binoy@xxxxxx>

Tasks between exit_tasks_rcu_start() and exit_tasks_rcu_finish() are no
longer on the task list, but they still run kernel code, and they can
sleep there for a long time. Livepatch needs to wait for them before it
starts a transition and before it removes code they may still run. An
RCU Tasks grace period does not do that: a task asleep in do_exit() is
in an RCU Tasks quiescent state.

Make the exit window an SRCU read-side critical section, as it was before
the exit lists replaced the SRCU read side. The SRCU index is kept in
the task_struct. Add synchronize_rcu_tasks_exit() and a polled pair,
start_poll_synchronize_rcu_tasks_exit() and
poll_state_synchronize_rcu_tasks_exit(), for callers that must not block.

The exit lists are not changed and RCU Tasks does not use the new domain.

Suggested-by: Paul E. McKenney <paulmck@xxxxxxxxxx>
Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Binoy Ravindran <binoy@xxxxxx>
---
include/linux/rcupdate.h | 6 ++++++
include/linux/sched.h | 1 +
kernel/rcu/tasks.h | 28 ++++++++++++++++++++++++++++
3 files changed, 35 insertions(+)

diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
index 3f74ae6d6e1f2..4442fbe9e9ac8 100644
--- a/include/linux/rcupdate.h
+++ b/include/linux/rcupdate.h
@@ -204,6 +204,9 @@ void rcu_tasks_rude_torture_stats_print(char *tt, char *tf);
#define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
void exit_tasks_rcu_start(void);
void exit_tasks_rcu_finish(void);
+void synchronize_rcu_tasks_exit(void);
+unsigned long start_poll_synchronize_rcu_tasks_exit(void);
+bool poll_state_synchronize_rcu_tasks_exit(unsigned long cookie);
#else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
#define rcu_tasks_classic_qs(t, preempt) do { } while (0)
#define rcu_tasks_qs(t, preempt) do { } while (0)
@@ -212,6 +215,9 @@ void exit_tasks_rcu_finish(void);
#define synchronize_rcu_tasks synchronize_rcu
static inline void exit_tasks_rcu_start(void) { }
static inline void exit_tasks_rcu_finish(void) { }
+static inline void synchronize_rcu_tasks_exit(void) { }
+static inline unsigned long start_poll_synchronize_rcu_tasks_exit(void) { return 0; }
+static inline bool poll_state_synchronize_rcu_tasks_exit(unsigned long cookie) { return true; }
#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */

/**
diff --git a/include/linux/sched.h b/include/linux/sched.h
index e4f6323d78555..0fe18976d1b40 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -963,6 +963,7 @@ struct task_struct {
struct list_head rcu_tasks_holdout_list;
int rcu_tasks_exit_cpu;
struct list_head rcu_tasks_exit_list;
+ int rcu_tasks_exit_srcu_idx;
#endif /* #ifdef CONFIG_TASKS_RCU */

#ifdef CONFIG_TASKS_TRACE_RCU
diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index fcac7361ec51e..57a16d7c33122 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -1288,6 +1288,32 @@ void rcu_tasks_get_gp_data(int *flags, unsigned long *gp_seq)
}
EXPORT_SYMBOL_GPL(rcu_tasks_get_gp_data);

+/*
+ * Tasks between exit_tasks_rcu_start() and exit_tasks_rcu_finish() are
+ * no longer on the task list, but they still run kernel code. This SRCU
+ * domain lets other subsystems wait for every task in that window to
+ * leave it.
+ */
+DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
+
+/* Wait for all tasks currently in the exit window to leave it. */
+void synchronize_rcu_tasks_exit(void)
+{
+ synchronize_srcu(&tasks_rcu_exit_srcu);
+}
+
+/* Start waiting for the tasks in the exit window, without blocking. */
+unsigned long start_poll_synchronize_rcu_tasks_exit(void)
+{
+ return start_poll_synchronize_srcu(&tasks_rcu_exit_srcu);
+}
+
+/* Have all tasks that were in the exit window at @cookie left it? */
+bool poll_state_synchronize_rcu_tasks_exit(unsigned long cookie)
+{
+ return poll_state_synchronize_srcu(&tasks_rcu_exit_srcu, cookie);
+}
+
/*
* Protect against tasklist scan blind spot while the task is exiting and
* may be removed from the tasklist. Do this by adding the task to yet
@@ -1313,6 +1339,7 @@ void exit_tasks_rcu_start(void)
list_add(&t->rcu_tasks_exit_list, &rtpcp->rtp_exit_list);
raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
preempt_enable();
+ current->rcu_tasks_exit_srcu_idx = __srcu_read_lock(&tasks_rcu_exit_srcu);
}

/*
@@ -1330,6 +1357,7 @@ void exit_tasks_rcu_finish(void)
raw_spin_lock_irqsave_rcu_node(rtpcp, flags);
list_del_init(&t->rcu_tasks_exit_list);
raw_spin_unlock_irqrestore_rcu_node(rtpcp, flags);
+ __srcu_read_unlock(&tasks_rcu_exit_srcu, current->rcu_tasks_exit_srcu_idx);
}

#else /* #ifdef CONFIG_TASKS_RCU */

--
2.39.5