Skip to content

Commit 3f95aa8

Browse files
committed
rcu: Make TASKS_RCU handle tasks that are almost done exiting
Once a task has passed exit_notify() in the do_exit() code path, it is no longer on the task lists, and is therefore no longer visible to rcu_tasks_kthread(). This means that an almost-exited task might be preempted while within a trampoline, and this task won't be waited on by rcu_tasks_kthread(). This commit fixes this bug by adding an srcu_struct. An exiting task does srcu_read_lock() just before calling exit_notify(), and does the corresponding srcu_read_unlock() after doing the final preempt_disable(). This means that rcu_tasks_kthread() can do synchronize_srcu() to wait for all mostly-exited tasks to reach their final preempt_disable() region, and then use synchronize_sched() to wait for those tasks to finish exiting. Reported-by: Oleg Nesterov <oleg@redhat.com> Suggested-by: Lai Jiangshan <laijs@cn.fujitsu.com> Signed-off-by: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
1 parent 53c6d4e commit 3f95aa8

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎include/linux/rcupdate.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ static inline void rcu_user_hooks_switch(struct task_struct *prev,
321321
* macro rather than an inline function to avoid #include hell.
322322
*/
323323
#ifdef CONFIG_TASKS_RCU
324+
#define TASKS_RCU(x) x
325+
extern struct srcu_struct tasks_rcu_exit_srcu;
324326
#define rcu_note_voluntary_context_switch(t) \
325327
do { \
326328
preempt_disable(); /* Exclude synchronize_sched(); */ \
@@ -329,6 +331,7 @@ static inline void rcu_user_hooks_switch(struct task_struct *prev,
329331
preempt_enable(); \
330332
} while (0)
331333
#else /* #ifdef CONFIG_TASKS_RCU */
334+
#define TASKS_RCU(x) do { } while (0)
332335
#define rcu_note_voluntary_context_switch(t) do { } while (0)
333336
#endif /* #else #ifdef CONFIG_TASKS_RCU */
334337

‎kernel/exit.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,7 @@ void do_exit(long code)
667667
{
668668
struct task_struct *tsk = current;
669669
int group_dead;
670+
TASKS_RCU(int tasks_rcu_i);
670671

671672
profile_task_exit(tsk);
672673

@@ -775,6 +776,7 @@ void do_exit(long code)
775776
*/
776777
flush_ptrace_hw_breakpoint(tsk);
777778

779+
TASKS_RCU(tasks_rcu_i = __srcu_read_lock(&tasks_rcu_exit_srcu));
778780
exit_notify(tsk, group_dead);
779781
proc_exit_connector(tsk);
780782
#ifdef CONFIG_NUMA
@@ -814,6 +816,7 @@ void do_exit(long code)
814816
if (tsk->nr_dirtied)
815817
__this_cpu_add(dirty_throttle_leaks, tsk->nr_dirtied);
816818
exit_rcu();
819+
TASKS_RCU(__srcu_read_unlock(&tasks_rcu_exit_srcu, tasks_rcu_i));
817820

818821
/*
819822
* The setting of TASK_RUNNING by try_to_wake_up() may be delayed

‎kernel/rcu/update.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,13 @@ static struct rcu_head *rcu_tasks_cbs_head;
367367
static struct rcu_head **rcu_tasks_cbs_tail = &rcu_tasks_cbs_head;
368368
static DEFINE_RAW_SPINLOCK(rcu_tasks_cbs_lock);
369369

370+
/* Track exiting tasks in order to allow them to be waited for. */
371+
DEFINE_SRCU(tasks_rcu_exit_srcu);
372+
373+
/* Control stall timeouts. Disable with <= 0, otherwise jiffies till stall. */
374+
static int rcu_task_stall_timeout __read_mostly = HZ * 60 * 3;
375+
module_param(rcu_task_stall_timeout, int, 0644);
376+
370377
/* Post an RCU-tasks callback. */
371378
void call_rcu_tasks(struct rcu_head *rhp, void (*func)(struct rcu_head *rhp))
372379
{
@@ -517,6 +524,15 @@ static int __noreturn rcu_tasks_kthread(void *arg)
517524
}
518525
rcu_read_unlock();
519526

527+
/*
528+
* Wait for tasks that are in the process of exiting.
529+
* This does only part of the job, ensuring that all
530+
* tasks that were previously exiting reach the point
531+
* where they have disabled preemption, allowing the
532+
* later synchronize_sched() to finish the job.
533+
*/
534+
synchronize_srcu(&tasks_rcu_exit_srcu);
535+
520536
/*
521537
* Each pass through the following loop scans the list
522538
* of holdout tasks, removing any that are no longer
@@ -546,6 +562,11 @@ static int __noreturn rcu_tasks_kthread(void *arg)
546562
* ->rcu_tasks_holdout accesses to be within the grace
547563
* period, avoiding the need for memory barriers for
548564
* ->rcu_tasks_holdout accesses.
565+
*
566+
* In addition, this synchronize_sched() waits for exiting
567+
* tasks to complete their final preempt_disable() region
568+
* of execution, cleaning up after the synchronize_srcu()
569+
* above.
549570
*/
550571
synchronize_sched();
551572

0 commit comments

Comments
 (0)