Freezer: make kernel threads nonfreezable by default
[safe/jmp/linux-2.6] / kernel / exit.c
index a94e1c3..e8af8d0 100644 (file)
@@ -4,35 +4,47 @@
  *  Copyright (C) 1991, 1992  Linus Torvalds
  */
 
-#include <linux/config.h>
 #include <linux/mm.h>
 #include <linux/slab.h>
 #include <linux/interrupt.h>
-#include <linux/smp_lock.h>
 #include <linux/module.h>
 #include <linux/capability.h>
 #include <linux/completion.h>
 #include <linux/personality.h>
 #include <linux/tty.h>
-#include <linux/namespace.h>
+#include <linux/mnt_namespace.h>
 #include <linux/key.h>
 #include <linux/security.h>
 #include <linux/cpu.h>
 #include <linux/acct.h>
+#include <linux/tsacct_kern.h>
 #include <linux/file.h>
 #include <linux/binfmts.h>
+#include <linux/nsproxy.h>
+#include <linux/pid_namespace.h>
 #include <linux/ptrace.h>
 #include <linux/profile.h>
+#include <linux/signalfd.h>
 #include <linux/mount.h>
 #include <linux/proc_fs.h>
+#include <linux/kthread.h>
 #include <linux/mempolicy.h>
+#include <linux/taskstats_kern.h>
+#include <linux/delayacct.h>
+#include <linux/freezer.h>
 #include <linux/cpuset.h>
 #include <linux/syscalls.h>
 #include <linux/signal.h>
+#include <linux/posix-timers.h>
 #include <linux/cn_proc.h>
 #include <linux/mutex.h>
 #include <linux/futex.h>
 #include <linux/compat.h>
+#include <linux/pipe_fs_i.h>
+#include <linux/audit.h> /* for audit_free() */
+#include <linux/resource.h>
+#include <linux/blkdev.h>
+#include <linux/task_io_accounting_ops.h>
 
 #include <asm/uaccess.h>
 #include <asm/unistd.h>
@@ -40,9 +52,6 @@
 #include <asm/mmu_context.h>
 
 extern void sem_exit (void);
-extern struct task_struct *child_reaper;
-
-int getrusage(struct task_struct *, int, struct rusage __user *);
 
 static void exit_mm(struct task_struct * tsk);
 
@@ -50,39 +59,108 @@ static void __unhash_process(struct task_struct *p)
 {
        nr_threads--;
        detach_pid(p, PIDTYPE_PID);
-       detach_pid(p, PIDTYPE_TGID);
        if (thread_group_leader(p)) {
                detach_pid(p, PIDTYPE_PGID);
                detach_pid(p, PIDTYPE_SID);
 
-               list_del_init(&p->tasks);
+               list_del_rcu(&p->tasks);
                __get_cpu_var(process_counts)--;
        }
-
+       list_del_rcu(&p->thread_group);
        remove_parent(p);
 }
 
+/*
+ * This function expects the tasklist_lock write-locked.
+ */
+static void __exit_signal(struct task_struct *tsk)
+{
+       struct signal_struct *sig = tsk->signal;
+       struct sighand_struct *sighand;
+
+       BUG_ON(!sig);
+       BUG_ON(!atomic_read(&sig->count));
+
+       rcu_read_lock();
+       sighand = rcu_dereference(tsk->sighand);
+       spin_lock(&sighand->siglock);
+
+       /*
+        * Notify that this sighand has been detached. This must
+        * be called with the tsk->sighand lock held. Also, this
+        * access tsk->sighand internally, so it must be called
+        * before tsk->sighand is reset.
+        */
+       signalfd_detach_locked(tsk);
+
+       posix_cpu_timers_exit(tsk);
+       if (atomic_dec_and_test(&sig->count))
+               posix_cpu_timers_exit_group(tsk);
+       else {
+               /*
+                * If there is any task waiting for the group exit
+                * then notify it:
+                */
+               if (sig->group_exit_task && atomic_read(&sig->count) == sig->notify_count) {
+                       wake_up_process(sig->group_exit_task);
+                       sig->group_exit_task = NULL;
+               }
+               if (tsk == sig->curr_target)
+                       sig->curr_target = next_thread(tsk);
+               /*
+                * Accumulate here the counters for all threads but the
+                * group leader as they die, so they can be added into
+                * the process-wide totals when those are taken.
+                * The group leader stays around as a zombie as long
+                * as there are other threads.  When it gets reaped,
+                * the exit.c code will add its counts into these totals.
+                * We won't ever get here for the group leader, since it
+                * will have been the last reference on the signal_struct.
+                */
+               sig->utime = cputime_add(sig->utime, tsk->utime);
+               sig->stime = cputime_add(sig->stime, tsk->stime);
+               sig->min_flt += tsk->min_flt;
+               sig->maj_flt += tsk->maj_flt;
+               sig->nvcsw += tsk->nvcsw;
+               sig->nivcsw += tsk->nivcsw;
+               sig->inblock += task_io_get_inblock(tsk);
+               sig->oublock += task_io_get_oublock(tsk);
+               sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
+               sig = NULL; /* Marker for below. */
+       }
+
+       __unhash_process(tsk);
+
+       tsk->signal = NULL;
+       tsk->sighand = NULL;
+       spin_unlock(&sighand->siglock);
+       rcu_read_unlock();
+
+       __cleanup_sighand(sighand);
+       clear_tsk_thread_flag(tsk,TIF_SIGPENDING);
+       flush_sigqueue(&tsk->pending);
+       if (sig) {
+               flush_sigqueue(&sig->shared_pending);
+               taskstats_tgid_free(sig);
+               __cleanup_signal(sig);
+       }
+}
+
+static void delayed_put_task_struct(struct rcu_head *rhp)
+{
+       put_task_struct(container_of(rhp, struct task_struct, rcu));
+}
+
 void release_task(struct task_struct * p)
 {
+       struct task_struct *leader;
        int zap_leader;
-       task_t *leader;
-       struct dentry *proc_dentry;
-
-repeat: 
+repeat:
        atomic_dec(&p->user->processes);
-       spin_lock(&p->proc_lock);
-       proc_dentry = proc_pid_unhash(p);
        write_lock_irq(&tasklist_lock);
-       if (unlikely(p->ptrace))
-               __ptrace_unlink(p);
+       ptrace_unlink(p);
        BUG_ON(!list_empty(&p->ptrace_list) || !list_empty(&p->ptrace_children));
        __exit_signal(p);
-       /*
-        * Note that the fastpath in sys_times depends on __exit_signal having
-        * updated the counters before a task is removed from the tasklist of
-        * the process by __unhash_process.
-        */
-       __unhash_process(p);
 
        /*
         * If we are the last non-leader member of the thread
@@ -105,12 +183,10 @@ repeat:
                zap_leader = (leader->exit_signal == -1);
        }
 
-       sched_exit(p);
        write_unlock_irq(&tasklist_lock);
-       spin_unlock(&p->proc_lock);
-       proc_pid_flush(proc_dentry);
+       proc_flush_task(p);
        release_thread(p);
-       put_task_struct(p);
+       call_rcu(&p->rcu, delayed_put_task_struct);
 
        p = leader;
        if (unlikely(zap_leader))
@@ -121,25 +197,20 @@ repeat:
  * This checks not only the pgrp, but falls back on the pid if no
  * satisfactory pgrp is found. I dunno - gdb doesn't work correctly
  * without this...
+ *
+ * The caller must hold rcu lock or the tasklist lock.
  */
-int session_of_pgrp(int pgrp)
+struct pid *session_of_pgrp(struct pid *pgrp)
 {
        struct task_struct *p;
-       int sid = -1;
+       struct pid *sid = NULL;
+
+       p = pid_task(pgrp, PIDTYPE_PGID);
+       if (p == NULL)
+               p = pid_task(pgrp, PIDTYPE_PID);
+       if (p != NULL)
+               sid = task_session(p);
 
-       read_lock(&tasklist_lock);
-       do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
-               if (p->signal->session > 0) {
-                       sid = p->signal->session;
-                       goto out;
-               }
-       } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
-       p = find_task_by_pid(pgrp);
-       if (p)
-               sid = p->signal->session;
-out:
-       read_unlock(&tasklist_lock);
-       
        return sid;
 }
 
@@ -151,90 +222,76 @@ out:
  *
  * "I ask you, have you ever known what it is to be an orphan?"
  */
-static int will_become_orphaned_pgrp(int pgrp, task_t *ignored_task)
+static int will_become_orphaned_pgrp(struct pid *pgrp, struct task_struct *ignored_task)
 {
        struct task_struct *p;
        int ret = 1;
 
-       do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
+       do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
                if (p == ignored_task
                                || p->exit_state
-                               || p->real_parent->pid == 1)
+                               || is_init(p->real_parent))
                        continue;
-               if (process_group(p->real_parent) != pgrp
-                           && p->real_parent->signal->session == p->signal->session) {
+               if (task_pgrp(p->real_parent) != pgrp &&
+                   task_session(p->real_parent) == task_session(p)) {
                        ret = 0;
                        break;
                }
-       } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
+       } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
        return ret;     /* (sighing) "Often!" */
 }
 
-int is_orphaned_pgrp(int pgrp)
+int is_current_pgrp_orphaned(void)
 {
        int retval;
 
        read_lock(&tasklist_lock);
-       retval = will_become_orphaned_pgrp(pgrp, NULL);
+       retval = will_become_orphaned_pgrp(task_pgrp(current), NULL);
        read_unlock(&tasklist_lock);
 
        return retval;
 }
 
-static int has_stopped_jobs(int pgrp)
+static int has_stopped_jobs(struct pid *pgrp)
 {
        int retval = 0;
        struct task_struct *p;
 
-       do_each_task_pid(pgrp, PIDTYPE_PGID, p) {
+       do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
                if (p->state != TASK_STOPPED)
                        continue;
-
-               /* If p is stopped by a debugger on a signal that won't
-                  stop it, then don't count p as stopped.  This isn't
-                  perfect but it's a good approximation.  */
-               if (unlikely (p->ptrace)
-                   && p->exit_code != SIGSTOP
-                   && p->exit_code != SIGTSTP
-                   && p->exit_code != SIGTTOU
-                   && p->exit_code != SIGTTIN)
-                       continue;
-
                retval = 1;
                break;
-       } while_each_task_pid(pgrp, PIDTYPE_PGID, p);
+       } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
        return retval;
 }
 
 /**
- * reparent_to_init - Reparent the calling kernel thread to the init task.
+ * reparent_to_kthreadd - Reparent the calling kernel thread to kthreadd
  *
  * If a kernel thread is launched as a result of a system call, or if
- * it ever exits, it should generally reparent itself to init so that
- * it is correctly cleaned up on exit.
+ * it ever exits, it should generally reparent itself to kthreadd so it
+ * isn't in the way of other processes and is correctly cleaned up on exit.
  *
  * The various task state such as scheduling policy and priority may have
  * been inherited from a user process, so we reset them to sane values here.
  *
- * NOTE that reparent_to_init() gives the caller full capabilities.
+ * NOTE that reparent_to_kthreadd() gives the caller full capabilities.
  */
-static void reparent_to_init(void)
+static void reparent_to_kthreadd(void)
 {
        write_lock_irq(&tasklist_lock);
 
        ptrace_unlink(current);
        /* Reparent to init */
        remove_parent(current);
-       current->parent = child_reaper;
-       current->real_parent = child_reaper;
+       current->real_parent = current->parent = kthreadd_task;
        add_parent(current);
 
        /* Set the exit signal to SIGCHLD so we signal init on exit */
        current->exit_signal = SIGCHLD;
 
-       if ((current->policy == SCHED_NORMAL ||
-                       current->policy == SCHED_BATCH)
-                               && (task_nice(current) < 0))
+       if (task_nice(current) < 0)
                set_user_nice(current, 0);
        /* cpus_allowed? */
        /* rt_priority? */
@@ -251,19 +308,19 @@ void __set_special_pids(pid_t session, pid_t pgrp)
 {
        struct task_struct *curr = current->group_leader;
 
-       if (curr->signal->session != session) {
+       if (process_session(curr) != session) {
                detach_pid(curr, PIDTYPE_SID);
-               curr->signal->session = session;
-               attach_pid(curr, PIDTYPE_SID, session);
+               set_signal_session(curr->signal, session);
+               attach_pid(curr, PIDTYPE_SID, find_pid(session));
        }
        if (process_group(curr) != pgrp) {
                detach_pid(curr, PIDTYPE_PGID);
                curr->signal->pgrp = pgrp;
-               attach_pid(curr, PIDTYPE_PGID, pgrp);
+               attach_pid(curr, PIDTYPE_PGID, find_pid(pgrp));
        }
 }
 
-void set_special_pids(pid_t session, pid_t pgrp)
+static void set_special_pids(pid_t session, pid_t pgrp)
 {
        write_lock_irq(&tasklist_lock);
        __set_special_pids(session, pgrp);
@@ -302,7 +359,7 @@ int disallow_signal(int sig)
                return -EINVAL;
 
        spin_lock_irq(&current->sighand->siglock);
-       sigaddset(&current->blocked, sig);
+       current->sighand->action[(sig)-1].sa.sa_handler = SIG_IGN;
        recalc_sigpending();
        spin_unlock_irq(&current->sighand->siglock);
        return 0;
@@ -331,11 +388,14 @@ void daemonize(const char *name, ...)
         * they would be locked into memory.
         */
        exit_mm(current);
+       /*
+        * We don't want to have TIF_FREEZE set if the system-wide hibernation
+        * or suspend transition begins right now.
+        */
+       current->flags |= PF_NOFREEZE;
 
        set_special_pids(1, 1);
-       mutex_lock(&tty_mutex);
-       current->signal->tty = NULL;
-       mutex_unlock(&tty_mutex);
+       proc_clear_tty(current);
 
        /* Block and flush all signals */
        sigfillset(&blocked);
@@ -348,14 +408,16 @@ void daemonize(const char *name, ...)
        fs = init_task.fs;
        current->fs = fs;
        atomic_inc(&fs->count);
-       exit_namespace(current);
-       current->namespace = init_task.namespace;
-       get_namespace(current->namespace);
+
+       exit_task_namespaces(current);
+       current->nsproxy = init_task.nsproxy;
+       get_task_namespaces(current);
+
        exit_files(current);
        current->files = init_task.files;
        atomic_inc(&current->files->count);
 
-       reparent_to_init();
+       reparent_to_kthreadd();
 }
 
 EXPORT_SYMBOL(daemonize);
@@ -376,14 +438,16 @@ static void close_files(struct files_struct * files)
        for (;;) {
                unsigned long set;
                i = j * __NFDBITS;
-               if (i >= fdt->max_fdset || i >= fdt->max_fds)
+               if (i >= fdt->max_fds)
                        break;
                set = fdt->open_fds->fds_bits[j++];
                while (set) {
                        if (set & 1) {
                                struct file * file = xchg(&fdt->fd[i], NULL);
-                               if (file)
+                               if (file) {
                                        filp_close(file, files);
+                                       cond_resched();
+                               }
                        }
                        i++;
                        set >>= 1;
@@ -417,9 +481,7 @@ void fastcall put_files_struct(struct files_struct *files)
                 * you can free files immediately.
                 */
                fdt = files_fdtable(files);
-               if (fdt == &files->fdtab)
-                       fdt->free_files = files;
-               else
+               if (fdt != &files->fdtab)
                        kmem_cache_free(files_cachep, files);
                free_fdtable(fdt);
        }
@@ -427,6 +489,18 @@ void fastcall put_files_struct(struct files_struct *files)
 
 EXPORT_SYMBOL(put_files_struct);
 
+void reset_files_struct(struct task_struct *tsk, struct files_struct *files)
+{
+       struct files_struct *old;
+
+       old = tsk->files;
+       task_lock(tsk);
+       tsk->files = files;
+       task_unlock(tsk);
+       put_files_struct(old);
+}
+EXPORT_SYMBOL(reset_files_struct);
+
 static inline void __exit_files(struct task_struct *tsk)
 {
        struct files_struct * files = tsk->files;
@@ -514,7 +588,7 @@ static void exit_mm(struct task_struct * tsk)
                down_read(&mm->mmap_sem);
        }
        atomic_inc(&mm->mm_count);
-       if (mm != tsk->active_mm) BUG();
+       BUG_ON(mm != tsk->active_mm);
        /* more a memory barrier than a real lock */
        task_lock(tsk);
        tsk->mm = NULL;
@@ -524,7 +598,8 @@ static void exit_mm(struct task_struct * tsk)
        mmput(mm);
 }
 
-static inline void choose_new_parent(task_t *p, task_t *reaper)
+static inline void
+choose_new_parent(struct task_struct *p, struct task_struct *reaper)
 {
        /*
         * Make sure we're not reparenting to ourselves and that
@@ -534,12 +609,9 @@ static inline void choose_new_parent(task_t *p, task_t *reaper)
        p->real_parent = reaper;
 }
 
-static void reparent_thread(task_t *p, task_t *father, int traced)
+static void
+reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
 {
-       /* We don't want people slaying init.  */
-       if (p->exit_signal != -1)
-               p->exit_signal = SIGCHLD;
-
        if (p->pdeath_signal)
                /* We already hold the tasklist_lock here.  */
                group_send_sig_info(p->pdeath_signal, SEND_SIG_NOINFO, p);
@@ -555,17 +627,11 @@ static void reparent_thread(task_t *p, task_t *father, int traced)
                 * anyway, so let go of it.
                 */
                p->ptrace = 0;
-               list_del_init(&p->sibling);
+               remove_parent(p);
                p->parent = p->real_parent;
-               list_add_tail(&p->sibling, &p->parent->children);
+               add_parent(p);
 
-               /* If we'd notified the old parent about this child's death,
-                * also notify the new parent.
-                */
-               if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
-                   thread_group_empty(p))
-                       do_notify_parent(p, p->exit_signal);
-               else if (p->state == TASK_TRACED) {
+               if (p->state == TASK_TRACED) {
                        /*
                         * If it was at a trace stop, turn it into
                         * a normal stop since it's no longer being
@@ -575,19 +641,37 @@ static void reparent_thread(task_t *p, task_t *father, int traced)
                }
        }
 
+       /* If this is a threaded reparent there is no need to
+        * notify anyone anything has happened.
+        */
+       if (p->real_parent->group_leader == father->group_leader)
+               return;
+
+       /* We don't want people slaying init.  */
+       if (p->exit_signal != -1)
+               p->exit_signal = SIGCHLD;
+
+       /* If we'd notified the old parent about this child's death,
+        * also notify the new parent.
+        */
+       if (!traced && p->exit_state == EXIT_ZOMBIE &&
+           p->exit_signal != -1 && thread_group_empty(p))
+               do_notify_parent(p, p->exit_signal);
+
        /*
         * process group orphan check
         * Case ii: Our child is in a different pgrp
         * than we are, and it was the only connection
         * outside, so the child pgrp is now orphaned.
         */
-       if ((process_group(p) != process_group(father)) &&
-           (p->signal->session == father->signal->session)) {
-               int pgrp = process_group(p);
-
-               if (will_become_orphaned_pgrp(pgrp, NULL) && has_stopped_jobs(pgrp)) {
-                       __kill_pg_info(SIGHUP, SEND_SIG_PRIV, pgrp);
-                       __kill_pg_info(SIGCONT, SEND_SIG_PRIV, pgrp);
+       if ((task_pgrp(p) != task_pgrp(father)) &&
+           (task_session(p) == task_session(father))) {
+               struct pid *pgrp = task_pgrp(p);
+
+               if (will_become_orphaned_pgrp(pgrp, NULL) &&
+                   has_stopped_jobs(pgrp)) {
+                       __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
+                       __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
                }
        }
 }
@@ -596,10 +680,11 @@ static void reparent_thread(task_t *p, task_t *father, int traced)
  * When we die, we re-parent all our children.
  * Try to give them to another thread in our thread
  * group, and if no such member exists, give it to
- * the global child reaper process (ie "init")
+ * the child reaper process (ie "init") in our pid
+ * space.
  */
-static void forget_original_parent(struct task_struct * father,
-                                         struct list_head *to_release)
+static void
+forget_original_parent(struct task_struct *father, struct list_head *to_release)
 {
        struct task_struct *p, *reaper = father;
        struct list_head *_p, *_n;
@@ -607,7 +692,7 @@ static void forget_original_parent(struct task_struct * father,
        do {
                reaper = next_thread(reaper);
                if (reaper == father) {
-                       reaper = child_reaper;
+                       reaper = child_reaper(father);
                        break;
                }
        } while (reaper->exit_state);
@@ -622,7 +707,7 @@ static void forget_original_parent(struct task_struct * father,
         */
        list_for_each_safe(_p, _n, &father->children) {
                int ptrace;
-               p = list_entry(_p,struct task_struct,sibling);
+               p = list_entry(_p, struct task_struct, sibling);
 
                ptrace = p->ptrace;
 
@@ -651,7 +736,7 @@ static void forget_original_parent(struct task_struct * father,
                        list_add(&p->ptrace_list, to_release);
        }
        list_for_each_safe(_p, _n, &father->ptrace_children) {
-               p = list_entry(_p,struct task_struct,ptrace_list);
+               p = list_entry(_p, struct task_struct, ptrace_list);
                choose_new_parent(p, reaper);
                reparent_thread(p, father, 1);
        }
@@ -666,6 +751,7 @@ static void exit_notify(struct task_struct *tsk)
        int state;
        struct task_struct *t;
        struct list_head ptrace_dead, *_p, *_n;
+       struct pid *pgrp;
 
        if (signal_pending(tsk) && !(tsk->signal->flags & SIGNAL_GROUP_EXIT)
            && !thread_group_empty(tsk)) {
@@ -681,11 +767,8 @@ static void exit_notify(struct task_struct *tsk)
                read_lock(&tasklist_lock);
                spin_lock_irq(&tsk->sighand->siglock);
                for (t = next_thread(tsk); t != tsk; t = next_thread(t))
-                       if (!signal_pending(t) && !(t->flags & PF_EXITING)) {
-                               recalc_sigpending_tsk(t);
-                               if (signal_pending(t))
-                                       signal_wake_up(t, 0);
-                       }
+                       if (!signal_pending(t) && !(t->flags & PF_EXITING))
+                               recalc_sigpending_and_wake(t);
                spin_unlock_irq(&tsk->sighand->siglock);
                read_unlock(&tasklist_lock);
        }
@@ -718,12 +801,13 @@ static void exit_notify(struct task_struct *tsk)
         
        t = tsk->real_parent;
        
-       if ((process_group(t) != process_group(tsk)) &&
-           (t->signal->session == tsk->signal->session) &&
-           will_become_orphaned_pgrp(process_group(tsk), tsk) &&
-           has_stopped_jobs(process_group(tsk))) {
-               __kill_pg_info(SIGHUP, SEND_SIG_PRIV, process_group(tsk));
-               __kill_pg_info(SIGCONT, SEND_SIG_PRIV, process_group(tsk));
+       pgrp = task_pgrp(tsk);
+       if ((task_pgrp(t) != pgrp) &&
+           (task_session(t) == task_session(tsk)) &&
+           will_become_orphaned_pgrp(pgrp, tsk) &&
+           has_stopped_jobs(pgrp)) {
+               __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
+               __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
        }
 
        /* Let father know we died 
@@ -771,7 +855,7 @@ static void exit_notify(struct task_struct *tsk)
 
        list_for_each_safe(_p, _n, &ptrace_dead) {
                list_del_init(_p);
-               t = list_entry(_p,struct task_struct,ptrace_list);
+               t = list_entry(_p, struct task_struct, ptrace_list);
                release_task(t);
        }
 
@@ -780,6 +864,34 @@ static void exit_notify(struct task_struct *tsk)
                release_task(tsk);
 }
 
+#ifdef CONFIG_DEBUG_STACK_USAGE
+static void check_stack_usage(void)
+{
+       static DEFINE_SPINLOCK(low_water_lock);
+       static int lowest_to_date = THREAD_SIZE;
+       unsigned long *n = end_of_stack(current);
+       unsigned long free;
+
+       while (*n == 0)
+               n++;
+       free = (unsigned long)n - (unsigned long)end_of_stack(current);
+
+       if (free >= lowest_to_date)
+               return;
+
+       spin_lock(&low_water_lock);
+       if (free < lowest_to_date) {
+               printk(KERN_WARNING "%s used greatest stack depth: %lu bytes "
+                               "left\n",
+                               current->comm, free);
+               lowest_to_date = free;
+       }
+       spin_unlock(&low_water_lock);
+}
+#else
+static inline void check_stack_usage(void) {}
+#endif
+
 fastcall NORET_TYPE void do_exit(long code)
 {
        struct task_struct *tsk = current;
@@ -793,8 +905,13 @@ fastcall NORET_TYPE void do_exit(long code)
                panic("Aiee, killing interrupt handler!");
        if (unlikely(!tsk->pid))
                panic("Attempted to kill the idle task!");
-       if (unlikely(tsk == child_reaper))
-               panic("Attempted to kill init!");
+       if (unlikely(tsk == child_reaper(tsk))) {
+               if (tsk->nsproxy->pid_ns != &init_pid_ns)
+                       tsk->nsproxy->pid_ns->child_reaper = init_pid_ns.child_reaper;
+               else
+                       panic("Attempted to kill init!");
+       }
+
 
        if (unlikely(current->ptrace & PT_TRACE_EXIT)) {
                current->ptrace_message = code;
@@ -808,21 +925,29 @@ fastcall NORET_TYPE void do_exit(long code)
        if (unlikely(tsk->flags & PF_EXITING)) {
                printk(KERN_ALERT
                        "Fixing recursive fault but reboot is needed!\n");
+               /*
+                * We can do this unlocked here. The futex code uses
+                * this flag just to verify whether the pi state
+                * cleanup has been done or not. In the worst case it
+                * loops once more. We pretend that the cleanup was
+                * done as there is no way to return. Either the
+                * OWNER_DIED bit is set by now or we push the blocked
+                * task into the wait for ever nirwana as well.
+                */
+               tsk->flags |= PF_EXITPIDONE;
                if (tsk->io_context)
                        exit_io_context();
                set_current_state(TASK_UNINTERRUPTIBLE);
                schedule();
        }
 
-       tsk->flags |= PF_EXITING;
-
        /*
-        * Make sure we don't try to process any timer firings
-        * while we are already exiting.
+        * tsk->flags are checked in the futex code to protect against
+        * an exiting task cleaning up the robust pi futexes.
         */
-       tsk->it_virt_expires = cputime_zero;
-       tsk->it_prof_expires = cputime_zero;
-       tsk->it_sched_expires = 0;
+       spin_lock_irq(&tsk->pi_lock);
+       tsk->flags |= PF_EXITING;
+       spin_unlock_irq(&tsk->pi_lock);
 
        if (unlikely(in_atomic()))
                printk(KERN_INFO "note: %s[%d] exited with preempt_count %d\n",
@@ -836,22 +961,31 @@ fastcall NORET_TYPE void do_exit(long code)
        }
        group_dead = atomic_dec_and_test(&tsk->signal->live);
        if (group_dead) {
-               hrtimer_cancel(&tsk->signal->real_timer);
+               hrtimer_cancel(&tsk->signal->real_timer);
                exit_itimers(tsk->signal);
-               acct_process(code);
        }
+       acct_collect(code, group_dead);
        if (unlikely(tsk->robust_list))
                exit_robust_list(tsk);
-#ifdef CONFIG_COMPAT
+#if defined(CONFIG_FUTEX) && defined(CONFIG_COMPAT)
        if (unlikely(tsk->compat_robust_list))
                compat_exit_robust_list(tsk);
 #endif
+       if (group_dead)
+               tty_audit_exit();
+       if (unlikely(tsk->audit_context))
+               audit_free(tsk);
+
+       taskstats_exit(tsk, group_dead);
+
        exit_mm(tsk);
 
+       if (group_dead)
+               acct_process();
        exit_sem(tsk);
        __exit_files(tsk);
        __exit_fs(tsk);
-       exit_namespace(tsk);
+       check_stack_usage();
        exit_thread();
        cpuset_exit(tsk);
        exit_keys(tsk);
@@ -865,28 +999,46 @@ fastcall NORET_TYPE void do_exit(long code)
 
        tsk->exit_code = code;
        proc_exit_connector(tsk);
+       exit_task_namespaces(tsk);
        exit_notify(tsk);
 #ifdef CONFIG_NUMA
        mpol_free(tsk->mempolicy);
        tsk->mempolicy = NULL;
 #endif
        /*
-        * If DEBUG_MUTEXES is on, make sure we are holding no locks:
+        * This must happen late, after the PID is not
+        * hashed anymore:
         */
-       mutex_debug_check_no_locks_held(tsk);
+       if (unlikely(!list_empty(&tsk->pi_state_list)))
+               exit_pi_state_list(tsk);
+       if (unlikely(current->pi_state_cache))
+               kfree(current->pi_state_cache);
+       /*
+        * Make sure we are holding no locks:
+        */
+       debug_check_no_locks_held(tsk);
+       /*
+        * We can do this unlocked here. The futex code uses this flag
+        * just to verify whether the pi state cleanup has been done
+        * or not. In the worst case it loops once more.
+        */
+       tsk->flags |= PF_EXITPIDONE;
 
        if (tsk->io_context)
                exit_io_context();
 
-       /* PF_DEAD causes final put_task_struct after we schedule. */
+       if (tsk->splice_pipe)
+               __free_pipe_info(tsk->splice_pipe);
+
        preempt_disable();
-       BUG_ON(tsk->flags & PF_DEAD);
-       tsk->flags |= PF_DEAD;
+       /* causes final put_task_struct in finish_task_switch(). */
+       tsk->state = TASK_DEAD;
 
        schedule();
        BUG();
        /* Avoid "noreturn function does return".  */
-       for (;;) ;
+       for (;;)
+               cpu_relax();    /* For when BUG is null */
 }
 
 EXPORT_SYMBOL_GPL(do_exit);
@@ -895,7 +1047,7 @@ NORET_TYPE void complete_and_exit(struct completion *comp, long code)
 {
        if (comp)
                complete(comp);
-       
+
        do_exit(code);
 }
 
@@ -906,13 +1058,6 @@ asmlinkage long sys_exit(int error_code)
        do_exit((error_code&0xff)<<8);
 }
 
-task_t fastcall *next_thread(const task_t *p)
-{
-       return pid_task(p->pids[PIDTYPE_TGID].pid_list.next, PIDTYPE_TGID);
-}
-
-EXPORT_SYMBOL(next_thread);
-
 /*
  * Take down every thread in the group.  This is called by fatal signals
  * as well as by sys_exit_group (below).
@@ -927,7 +1072,6 @@ do_group_exit(int exit_code)
        else if (!thread_group_empty(current)) {
                struct signal_struct *const sig = current->signal;
                struct sighand_struct *const sighand = current->sighand;
-               read_lock(&tasklist_lock);
                spin_lock_irq(&sighand->siglock);
                if (sig->flags & SIGNAL_GROUP_EXIT)
                        /* Another thread got here before we took the lock.  */
@@ -937,7 +1081,6 @@ do_group_exit(int exit_code)
                        zap_other_threads(current);
                }
                spin_unlock_irq(&sighand->siglock);
-               read_unlock(&tasklist_lock);
        }
 
        do_exit(exit_code);
@@ -954,8 +1097,10 @@ asmlinkage void sys_exit_group(int error_code)
        do_group_exit((error_code & 0xff) << 8);
 }
 
-static int eligible_child(pid_t pid, int options, task_t *p)
+static int eligible_child(pid_t pid, int options, struct task_struct *p)
 {
+       int err;
+
        if (pid > 0) {
                if (p->pid != pid)
                        return 0;
@@ -986,21 +1131,23 @@ static int eligible_child(pid_t pid, int options, task_t *p)
         * Do not consider thread group leaders that are
         * in a non-empty thread group:
         */
-       if (current->tgid != p->tgid && delay_group_leader(p))
+       if (delay_group_leader(p))
                return 2;
 
-       if (security_task_wait(p))
-               return 0;
+       err = security_task_wait(p);
+       if (err)
+               return err;
 
        return 1;
 }
 
-static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
+static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
                               int why, int status,
                               struct siginfo __user *infop,
                               struct rusage __user *rusagep)
 {
        int retval = rusagep ? getrusage(p, RUSAGE_BOTH, rusagep) : 0;
+
        put_task_struct(p);
        if (!retval)
                retval = put_user(SIGCHLD, &infop->si_signo);
@@ -1025,7 +1172,7 @@ static int wait_noreap_copyout(task_t *p, pid_t pid, uid_t uid,
  * the lock and this task is uninteresting.  If we return nonzero, we have
  * released the lock and the system call should return.
  */
-static int wait_task_zombie(task_t *p, int noreap,
+static int wait_task_zombie(struct task_struct *p, int noreap,
                            struct siginfo __user *infop,
                            int __user *stat_addr, struct rusage __user *ru)
 {
@@ -1113,6 +1260,12 @@ static int wait_task_zombie(task_t *p, int noreap,
                        p->nvcsw + sig->nvcsw + sig->cnvcsw;
                psig->cnivcsw +=
                        p->nivcsw + sig->nivcsw + sig->cnivcsw;
+               psig->cinblock +=
+                       task_io_get_inblock(p) +
+                       sig->inblock + sig->cinblock;
+               psig->coublock +=
+                       task_io_get_oublock(p) +
+                       sig->oublock + sig->coublock;
                spin_unlock_irq(&p->parent->sighand->siglock);
        }
 
@@ -1187,8 +1340,8 @@ static int wait_task_zombie(task_t *p, int noreap,
  * the lock and this task is uninteresting.  If we return nonzero, we have
  * released the lock and the system call should return.
  */
-static int wait_task_stopped(task_t *p, int delayed_group_leader, int noreap,
-                            struct siginfo __user *infop,
+static int wait_task_stopped(struct task_struct *p, int delayed_group_leader,
+                            int noreap, struct siginfo __user *infop,
                             int __user *stat_addr, struct rusage __user *ru)
 {
        int retval, exit_code;
@@ -1302,7 +1455,7 @@ bail_ref:
  * the lock and this task is uninteresting.  If we return nonzero, we have
  * released the lock and the system call should return.
  */
-static int wait_task_continued(task_t *p, int noreap,
+static int wait_task_continued(struct task_struct *p, int noreap,
                               struct siginfo __user *infop,
                               int __user *stat_addr, struct rusage __user *ru)
 {
@@ -1371,6 +1524,7 @@ static long do_wait(pid_t pid, int options, struct siginfo __user *infop,
        DECLARE_WAITQUEUE(wait, current);
        struct task_struct *tsk;
        int flag, retval;
+       int allowed, denied;
 
        add_wait_queue(&current->signal->wait_chldexit,&wait);
 repeat:
@@ -1379,6 +1533,7 @@ repeat:
         * match our criteria, even if we are not able to reap it yet.
         */
        flag = 0;
+       allowed = denied = 0;
        current->state = TASK_INTERRUPTIBLE;
        read_lock(&tasklist_lock);
        tsk = current;
@@ -1388,12 +1543,18 @@ repeat:
                int ret;
 
                list_for_each(_p,&tsk->children) {
-                       p = list_entry(_p,struct task_struct,sibling);
+                       p = list_entry(_p, struct task_struct, sibling);
 
                        ret = eligible_child(pid, options, p);
                        if (!ret)
                                continue;
 
+                       if (unlikely(ret < 0)) {
+                               denied = ret;
+                               continue;
+                       }
+                       allowed = 1;
+
                        switch (p->state) {
                        case TASK_TRACED:
                                /*
@@ -1477,8 +1638,7 @@ check_continued:
                if (options & __WNOTHREAD)
                        break;
                tsk = next_thread(tsk);
-               if (tsk->signal != current->signal)
-                       BUG();
+               BUG_ON(tsk->signal != current->signal);
        } while (tsk != current);
 
        read_unlock(&tasklist_lock);
@@ -1493,6 +1653,8 @@ check_continued:
                goto repeat;
        }
        retval = -ECHILD;
+       if (unlikely(denied) && !allowed)
+               retval = denied;
 end:
        current->state = TASK_RUNNING;
        remove_wait_queue(&current->signal->wait_chldexit,&wait);