pidns: Wait in zap_pid_ns_processes until pid_ns->nr_hashed == 1
authorEric W. Biederman <ebiederm@xmission.com>
Wed, 1 Aug 2012 22:03:42 +0000 (15:03 -0700)
committerEric W. Biederman <ebiederm@xmission.com>
Mon, 19 Nov 2012 13:59:12 +0000 (05:59 -0800)
Looking at pid_ns->nr_hashed is a bit simpler and it works for
disjoint process trees that an unshare or a join of a pid_namespace
may create.

Acked-by: "Serge E. Hallyn" <serge@hallyn.com>
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
kernel/exit.c
kernel/pid.c
kernel/pid_namespace.c

index 346616c0092cfe3993fbcfb9b7f36d0d02986fb2..d7fe58db452709444a42e622c964e604ddd42302 100644 (file)
@@ -72,18 +72,6 @@ static void __unhash_process(struct task_struct *p, bool group_dead)
                list_del_rcu(&p->tasks);
                list_del_init(&p->sibling);
                __this_cpu_dec(process_counts);
-               /*
-                * If we are the last child process in a pid namespace to be
-                * reaped, notify the reaper sleeping zap_pid_ns_processes().
-                */
-               if (IS_ENABLED(CONFIG_PID_NS)) {
-                       struct task_struct *parent = p->real_parent;
-
-                       if ((task_active_pid_ns(parent)->child_reaper == parent) &&
-                           list_empty(&parent->children) &&
-                           (parent->flags & PF_EXITING))
-                               wake_up_process(parent);
-               }
        }
        list_del_rcu(&p->thread_group);
 }
index 9c219117af362e8199eede8d2ee12dbdcb9c56cc..6e8da291de492c7e174d161d65af4349d35a163b 100644 (file)
@@ -273,10 +273,20 @@ void free_pid(struct pid *pid)
        spin_lock_irqsave(&pidmap_lock, flags);
        for (i = 0; i <= pid->level; i++) {
                struct upid *upid = pid->numbers + i;
+               struct pid_namespace *ns = upid->ns;
                hlist_del_rcu(&upid->pid_chain);
-               if (--upid->ns->nr_hashed == 0) {
-                       upid->ns->nr_hashed = -1;
-                       schedule_work(&upid->ns->proc_work);
+               switch(--ns->nr_hashed) {
+               case 1:
+                       /* When all that is left in the pid namespace
+                        * is the reaper wake up the reaper.  The reaper
+                        * may be sleeping in zap_pid_ns_processes().
+                        */
+                       wake_up_process(ns->child_reaper);
+                       break;
+               case 0:
+                       ns->nr_hashed = -1;
+                       schedule_work(&ns->proc_work);
+                       break;
                }
        }
        spin_unlock_irqrestore(&pidmap_lock, flags);
index 84591cfeefc1b1165ca047d549d36ad9edc1beaf..3cc29b830e9ed8743bd9705c0a26f44275e9fc1c 100644 (file)
@@ -217,22 +217,15 @@ void zap_pid_ns_processes(struct pid_namespace *pid_ns)
 
        /*
         * sys_wait4() above can't reap the TASK_DEAD children.
-        * Make sure they all go away, see __unhash_process().
+        * Make sure they all go away, see free_pid().
         */
        for (;;) {
-               bool need_wait = false;
-
-               read_lock(&tasklist_lock);
-               if (!list_empty(&current->children)) {
-                       __set_current_state(TASK_UNINTERRUPTIBLE);
-                       need_wait = true;
-               }
-               read_unlock(&tasklist_lock);
-
-               if (!need_wait)
+               set_current_state(TASK_UNINTERRUPTIBLE);
+               if (pid_ns->nr_hashed == 1)
                        break;
                schedule();
        }
+       __set_current_state(TASK_RUNNING);
 
        if (pid_ns->reboot)
                current->signal->group_exit_code = pid_ns->reboot;