Skip to content

Commit 1612160

Browse files
paulmckrcufbq
authored andcommitted
rcu-tasks: Eliminate deadlocks involving do_exit() and RCU tasks
Holding a mutex across synchronize_rcu_tasks() and acquiring that same mutex in code called from do_exit() after its call to exit_tasks_rcu_start() but before its call to exit_tasks_rcu_stop() results in deadlock. This is by design, because tasks that are far enough into do_exit() are no longer present on the tasks list, making it a bit difficult for RCU Tasks to find them, let alone wait on them to do a voluntary context switch. However, such deadlocks are becoming more frequent. In addition, lockdep currently does not detect such deadlocks and they can be difficult to reproduce. In addition, if a task voluntarily context switches during that time (for example, if it blocks acquiring a mutex), then this task is in an RCU Tasks quiescent state. And with some adjustments, RCU Tasks could just as well take advantage of that fact. This commit therefore eliminates these deadlock by replacing the SRCU-based wait for do_exit() completion with per-CPU lists of tasks currently exiting. A given task will be on one of these per-CPU lists for the same period of time that this task would previously have been in the previous SRCU read-side critical section. These lists enable RCU Tasks to find the tasks that have already been removed from the tasks list, but that must nevertheless be waited upon. The RCU Tasks grace period gathers any of these do_exit() tasks that it must wait on, and adds them to the list of holdouts. Per-CPU locking and get_task_struct() are used to synchronize addition to and removal from these lists. Link: https://lore.kernel.org/all/20240118021842.290665-1-chenzhongjin@huawei.com/ Reported-by: Chen Zhongjin <chenzhongjin@huawei.com> Reported-by: Yang Jihong <yangjihong1@huawei.com> Signed-off-by: Paul E. McKenney <paulmck@kernel.org> Tested-by: Yang Jihong <yangjihong1@huawei.com> Tested-by: Chen Zhongjin <chenzhongjin@huawei.com> Reviewed-by: Frederic Weisbecker <frederic@kernel.org> Signed-off-by: Boqun Feng <boqun.feng@gmail.com>
1 parent 6b70399 commit 1612160

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

kernel/rcu/tasks.h

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ static struct rcu_tasks rt_name = \
146146
}
147147

148148
#ifdef CONFIG_TASKS_RCU
149-
/* Track exiting tasks in order to allow them to be waited for. */
150-
DEFINE_STATIC_SRCU(tasks_rcu_exit_srcu);
151149

152150
/* Report delay in synchronize_srcu() completion in rcu_tasks_postscan(). */
153151
static void tasks_rcu_exit_srcu_stall(struct timer_list *unused);
@@ -852,10 +850,12 @@ static void rcu_tasks_wait_gp(struct rcu_tasks *rtp)
852850
// number of voluntary context switches, and add that task to the
853851
// holdout list.
854852
// rcu_tasks_postscan():
855-
// Invoke synchronize_srcu() to ensure that all tasks that were
856-
// in the process of exiting (and which thus might not know to
857-
// synchronize with this RCU Tasks grace period) have completed
858-
// exiting.
853+
// Gather per-CPU lists of tasks in do_exit() to ensure that all
854+
// tasks that were in the process of exiting (and which thus might
855+
// not know to synchronize with this RCU Tasks grace period) have
856+
// completed exiting. The synchronize_rcu() in rcu_tasks_postgp()
857+
// will take care of any tasks stuck in the non-preemptible region
858+
// of do_exit() following its call to exit_tasks_rcu_stop().
859859
// check_all_holdout_tasks(), repeatedly until holdout list is empty:
860860
// Scans the holdout list, attempting to identify a quiescent state
861861
// for each task on the list. If there is a quiescent state, the
@@ -868,8 +868,10 @@ static void rcu_tasks_wait_gp(struct rcu_tasks *rtp)
868868
// with interrupts disabled.
869869
//
870870
// For each exiting task, the exit_tasks_rcu_start() and
871-
// exit_tasks_rcu_finish() functions begin and end, respectively, the SRCU
872-
// read-side critical sections waited for by rcu_tasks_postscan().
871+
// exit_tasks_rcu_finish() functions add and remove, respectively, the
872+
// current task to a per-CPU list of tasks that rcu_tasks_postscan() must
873+
// wait on. This is necessary because rcu_tasks_postscan() must wait on
874+
// tasks that have already been removed from the global list of tasks.
873875
//
874876
// Pre-grace-period update-side code is ordered before the grace
875877
// via the raw_spin_lock.*rcu_node(). Pre-grace-period read-side code
@@ -933,9 +935,13 @@ static void rcu_tasks_pertask(struct task_struct *t, struct list_head *hop)
933935
}
934936
}
935937

938+
void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func);
939+
DEFINE_RCU_TASKS(rcu_tasks, rcu_tasks_wait_gp, call_rcu_tasks, "RCU Tasks");
940+
936941
/* Processing between scanning taskslist and draining the holdout list. */
937942
static void rcu_tasks_postscan(struct list_head *hop)
938943
{
944+
int cpu;
939945
int rtsi = READ_ONCE(rcu_task_stall_info);
940946

941947
if (!IS_ENABLED(CONFIG_TINY_RCU)) {
@@ -949,9 +955,9 @@ static void rcu_tasks_postscan(struct list_head *hop)
949955
* this, divide the fragile exit path part in two intersecting
950956
* read side critical sections:
951957
*
952-
* 1) An _SRCU_ read side starting before calling exit_notify(),
953-
* which may remove the task from the tasklist, and ending after
954-
* the final preempt_disable() call in do_exit().
958+
* 1) A task_struct list addition before calling exit_notify(),
959+
* which may remove the task from the tasklist, with the
960+
* removal after the final preempt_disable() call in do_exit().
955961
*
956962
* 2) An _RCU_ read side starting with the final preempt_disable()
957963
* call in do_exit() and ending with the final call to schedule()
@@ -960,7 +966,17 @@ static void rcu_tasks_postscan(struct list_head *hop)
960966
* This handles the part 1). And postgp will handle part 2) with a
961967
* call to synchronize_rcu().
962968
*/
963-
synchronize_srcu(&tasks_rcu_exit_srcu);
969+
970+
for_each_possible_cpu(cpu) {
971+
struct rcu_tasks_percpu *rtpcp = per_cpu_ptr(rcu_tasks.rtpcpu, cpu);
972+
struct task_struct *t;
973+
974+
raw_spin_lock_irq_rcu_node(rtpcp);
975+
list_for_each_entry(t, &rtpcp->rtp_exit_list, rcu_tasks_exit_list)
976+
if (list_empty(&t->rcu_tasks_holdout_list))
977+
rcu_tasks_pertask(t, hop);
978+
raw_spin_unlock_irq_rcu_node(rtpcp);
979+
}
964980

965981
if (!IS_ENABLED(CONFIG_TINY_RCU))
966982
del_timer_sync(&tasks_rcu_exit_srcu_stall_timer);
@@ -1028,17 +1044,13 @@ static void rcu_tasks_postgp(struct rcu_tasks *rtp)
10281044
*
10291045
* In addition, this synchronize_rcu() waits for exiting tasks
10301046
* to complete their final preempt_disable() region of execution,
1031-
* cleaning up after synchronize_srcu(&tasks_rcu_exit_srcu),
10321047
* enforcing the whole region before tasklist removal until
10331048
* the final schedule() with TASK_DEAD state to be an RCU TASKS
10341049
* read side critical section.
10351050
*/
10361051
synchronize_rcu();
10371052
}
10381053

1039-
void call_rcu_tasks(struct rcu_head *rhp, rcu_callback_t func);
1040-
DEFINE_RCU_TASKS(rcu_tasks, rcu_tasks_wait_gp, call_rcu_tasks, "RCU Tasks");
1041-
10421054
static void tasks_rcu_exit_srcu_stall(struct timer_list *unused)
10431055
{
10441056
#ifndef CONFIG_TINY_RCU

0 commit comments

Comments
 (0)