Patch "rcu-tasks: Fix synchronize_rcu_tasks() VS zap_pid_ns_processes()" has been added to the 5.15-stable tree

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This is a note to let you know that I've just added the patch titled

    rcu-tasks: Fix synchronize_rcu_tasks() VS zap_pid_ns_processes()

to the 5.15-stable tree which can be found at:
    http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary

The filename of the patch is:
     rcu-tasks-fix-synchronize_rcu_tasks-vs-zap_pid_ns_pr.patch
and it can be found in the queue-5.15 subdirectory.

If you, or anyone else, feels it should not be added to the stable tree,
please let <stable@xxxxxxxxxxxxxxx> know about it.



commit 714e9855a4f036a4190c80024bb19870a371d9ed
Author: Frederic Weisbecker <frederic@xxxxxxxxxx>
Date:   Fri Nov 25 14:55:00 2022 +0100

    rcu-tasks: Fix synchronize_rcu_tasks() VS zap_pid_ns_processes()
    
    [ Upstream commit 28319d6dc5e2ffefa452c2377dd0f71621b5bff0 ]
    
    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: 3f95aa81d265 ("rcu: Make TASKS_RCU handle tasks that are almost done exiting")
    Reported-by: Pengfei Xu <pengfei.xu@xxxxxxxxx>
    Suggested-by: Boqun Feng <boqun.feng@xxxxxxxxx>
    Suggested-by: Neeraj Upadhyay <quic_neeraju@xxxxxxxxxxx>
    Suggested-by: Paul E. McKenney <paulmck@xxxxxxxxxx>
    Cc: Oleg Nesterov <oleg@xxxxxxxxxx>
    Cc: Lai Jiangshan <jiangshanlai@xxxxxxxxx>
    Cc: Eric W . Biederman <ebiederm@xxxxxxxxxxxx>
    Signed-off-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
    Signed-off-by: Paul E. McKenney <paulmck@xxxxxxxxxx>
    Signed-off-by: Sasha Levin <sashal@xxxxxxxxxx>

diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h
index 434d12fe2d4f5..1e937a32da33d 100644
--- a/include/linux/rcupdate.h
+++ b/include/linux/rcupdate.h
@@ -193,6 +193,7 @@ void synchronize_rcu_tasks_rude(void);
 
 #define rcu_note_voluntary_context_switch(t) rcu_tasks_qs(t, false)
 void exit_tasks_rcu_start(void);
+void exit_tasks_rcu_stop(void);
 void exit_tasks_rcu_finish(void);
 #else /* #ifdef CONFIG_TASKS_RCU_GENERIC */
 #define rcu_tasks_qs(t, preempt) do { } while (0)
@@ -200,6 +201,7 @@ void exit_tasks_rcu_finish(void);
 #define call_rcu_tasks call_rcu
 #define synchronize_rcu_tasks synchronize_rcu
 static inline void exit_tasks_rcu_start(void) { }
+static inline void exit_tasks_rcu_stop(void) { }
 static inline void exit_tasks_rcu_finish(void) { }
 #endif /* #else #ifdef CONFIG_TASKS_RCU_GENERIC */
 
diff --git a/kernel/pid_namespace.c b/kernel/pid_namespace.c
index a46a3723bc662..259fc4ca0d9cc 100644
--- a/kernel/pid_namespace.c
+++ b/kernel/pid_namespace.c
@@ -244,7 +244,24 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns)
 		set_current_state(TASK_INTERRUPTIBLE);
 		if (pid_ns->pid_allocated == init_pids)
 			break;
+		/*
+		 * Release tasks_rcu_exit_srcu to avoid following deadlock:
+		 *
+		 * 1) TASK A unshare(CLONE_NEWPID)
+		 * 2) TASK A fork() twice -> TASK B (child reaper for new ns)
+		 *    and TASK C
+		 * 3) TASK B exits, kills TASK C, waits for TASK A to reap it
+		 * 4) TASK A calls synchronize_rcu_tasks()
+		 *                   -> synchronize_srcu(tasks_rcu_exit_srcu)
+		 * 5) *DEADLOCK*
+		 *
+		 * It is considered safe to release tasks_rcu_exit_srcu here
+		 * because we assume the current task can not be concurrently
+		 * reaped at this point.
+		 */
+		exit_tasks_rcu_stop();
 		schedule();
+		exit_tasks_rcu_start();
 	}
 	__set_current_state(TASK_RUNNING);
 
diff --git a/kernel/rcu/tasks.h b/kernel/rcu/tasks.h
index 2408ca633872a..5533e3106ba01 100644
--- a/kernel/rcu/tasks.h
+++ b/kernel/rcu/tasks.h
@@ -640,16 +640,27 @@ void exit_tasks_rcu_start(void) __acquires(&tasks_rcu_exit_srcu)
  * task is exiting and may be removed from the tasklist. See
  * corresponding synchronize_srcu() for further details.
  */
-void exit_tasks_rcu_finish(void) __releases(&tasks_rcu_exit_srcu)
+void exit_tasks_rcu_stop(void) __releases(&tasks_rcu_exit_srcu)
 {
 	struct task_struct *t = current;
 
 	__srcu_read_unlock(&tasks_rcu_exit_srcu, t->rcu_tasks_idx);
-	exit_tasks_rcu_finish_trace(t);
+}
+
+/*
+ * Contribute to protect against tasklist scan blind spot while the
+ * task is exiting and may be removed from the tasklist. See
+ * corresponding synchronize_srcu() for further details.
+ */
+void exit_tasks_rcu_finish(void)
+{
+	exit_tasks_rcu_stop();
+	exit_tasks_rcu_finish_trace(current);
 }
 
 #else /* #ifdef CONFIG_TASKS_RCU */
 void exit_tasks_rcu_start(void) { }
+void exit_tasks_rcu_stop(void) { }
 void exit_tasks_rcu_finish(void) { exit_tasks_rcu_finish_trace(current); }
 #endif /* #else #ifdef CONFIG_TASKS_RCU */
 



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux