Skip to content

Commit 28319d6

Browse files
Frederic Weisbeckerpaulmckrcu
authored andcommitted
rcu-tasks: Fix synchronize_rcu_tasks() VS zap_pid_ns_processes()
RCU Tasks and PID-namespace unshare can interact in do_exit() in a complicated circular dependency: 1) TASK A calls unshare(CLONE_NEWPID), this creates a new PID namespace that every subsequent child of TASK A will belong to. But TASK A doesn't itself belong to that new PID namespace. 2) TASK A forks() and creates TASK B. TASK A stays attached to its PID namespace (let's say PID_NS1) and TASK B is the first task belonging to the new PID namespace created by unshare() (let's call it PID_NS2). 3) Since TASK B is the first task attached to PID_NS2, it becomes the PID_NS2 child reaper. 4) TASK A forks() again and creates TASK C which get attached to PID_NS2. Note how TASK C has TASK A as a parent (belonging to PID_NS1) but has TASK B (belonging to PID_NS2) as a pid_namespace child_reaper. 5) TASK B exits and since it is the child reaper for PID_NS2, it has to kill all other tasks attached to PID_NS2, and wait for all of them to die before getting reaped itself (zap_pid_ns_process()). 6) TASK A calls synchronize_rcu_tasks() which leads to synchronize_srcu(&tasks_rcu_exit_srcu). 7) TASK B is waiting for TASK C to get reaped. But TASK B is under a tasks_rcu_exit_srcu SRCU critical section (exit_notify() is between exit_tasks_rcu_start() and exit_tasks_rcu_finish()), blocking TASK A. 8) TASK C exits and since TASK A is its parent, it waits for it to reap TASK C, but it can't because TASK A waits for TASK B that waits for TASK C. Pid_namespace semantics can hardly be changed at this point. But the coverage of tasks_rcu_exit_srcu can be reduced instead. The current task is assumed not to be concurrently reapable at this stage of exit_notify() and therefore tasks_rcu_exit_srcu can be temporarily relaxed without breaking its constraints, providing a way out of the deadlock scenario. [ paulmck: Fix build failure by adding additional declaration. ] Fixes: 3f95aa8 ("rcu: Make TASKS_RCU handle tasks that are almost done exiting") Reported-by: Pengfei Xu <[email protected]> Suggested-by: Boqun Feng <[email protected]> Suggested-by: Neeraj Upadhyay <[email protected]> Suggested-by: Paul E. McKenney <[email protected]> Cc: Oleg Nesterov <[email protected]> Cc: Lai Jiangshan <[email protected]> Cc: Eric W . Biederman <[email protected]> Signed-off-by: Frederic Weisbecker <[email protected]> Signed-off-by: Paul E. McKenney <[email protected]>
1 parent 4475709 commit 28319d6

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

include/linux/rcupdate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ void synchronize_rcu_tasks_rude(void);
238238

239239
#define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
240240
void exit_tasks_rcu_start(void);
241+
void exit_tasks_rcu_stop(void);
241242
void exit_tasks_rcu_finish(void);
242243
#else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
243244
#define rcu_tasks_classic_qs(t, preempt) do { } while (0)
@@ -246,6 +247,7 @@ void exit_tasks_rcu_finish(void);
246247
#define call_rcu_tasks call_rcu
247248
#define synchronize_rcu_tasks synchronize_rcu
248249
static inline void exit_tasks_rcu_start(void) { }
250+
static inline void exit_tasks_rcu_stop(void) { }
249251
static inline void exit_tasks_rcu_finish(void) { }
250252
#endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
251253

kernel/pid_namespace.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,24 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns)
244244
set_current_state(TASK_INTERRUPTIBLE);
245245
if (pid_ns->pid_allocated == init_pids)
246246
break;
247+
/*
248+
* Release tasks_rcu_exit_srcu to avoid following deadlock:
249+
*
250+
* 1) TASK A unshare(CLONE_NEWPID)
251+
* 2) TASK A fork() twice -> TASK B (child reaper for new ns)
252+
* and TASK C
253+
* 3) TASK B exits, kills TASK C, waits for TASK A to reap it
254+
* 4) TASK A calls synchronize_rcu_tasks()
255+
* -> synchronize_srcu(tasks_rcu_exit_srcu)
256+
* 5) *DEADLOCK*
257+
*
258+
* It is considered safe to release tasks_rcu_exit_srcu here
259+
* because we assume the current task can not be concurrently
260+
* reaped at this point.
261+
*/
262+
exit_tasks_rcu_stop();
247263
schedule();
264+
exit_tasks_rcu_start();
248265
}
249266
__set_current_state(TASK_RUNNING);
250267

kernel/rcu/tasks.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,16 +1016,27 @@ void exit_tasks_rcu_start(void) __acquires(&tasks_rcu_exit_srcu)
10161016
* task is exiting and may be removed from the tasklist. See
10171017
* corresponding synchronize_srcu() for further details.
10181018
*/
1019-
void exit_tasks_rcu_finish(void) __releases(&tasks_rcu_exit_srcu)
1019+
void exit_tasks_rcu_stop(void) __releases(&tasks_rcu_exit_srcu)
10201020
{
10211021
struct task_struct *t = current;
10221022

10231023
__srcu_read_unlock(&tasks_rcu_exit_srcu, t->rcu_tasks_idx);
1024-
exit_tasks_rcu_finish_trace(t);
1024+
}
1025+
1026+
/*
1027+
* Contribute to protect against tasklist scan blind spot while the
1028+
* task is exiting and may be removed from the tasklist. See
1029+
* corresponding synchronize_srcu() for further details.
1030+
*/
1031+
void exit_tasks_rcu_finish(void)
1032+
{
1033+
exit_tasks_rcu_stop();
1034+
exit_tasks_rcu_finish_trace(current);
10251035
}
10261036

10271037
#else /* #ifdef CONFIG_TASKS_RCU */
10281038
void exit_tasks_rcu_start(void) { }
1039+
void exit_tasks_rcu_stop(void) { }
10291040
void exit_tasks_rcu_finish(void) { exit_tasks_rcu_finish_trace(current); }
10301041
#endif /* #else #ifdef CONFIG_TASKS_RCU */
10311042

0 commit comments

Comments
 (0)