sched: update delay-accounting to use CFS's precise stats
[safe/jmp/linux-2.6] / kernel / exit.c
index a5bf532..ca6a11b 100644 (file)
@@ -7,7 +7,6 @@
 #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/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>
@@ -42,6 +43,7 @@
 #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>
@@ -82,6 +84,14 @@ static void __exit_signal(struct task_struct *tsk)
        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);
@@ -112,7 +122,9 @@ static void __exit_signal(struct task_struct *tsk)
                sig->maj_flt += tsk->maj_flt;
                sig->nvcsw += tsk->nvcsw;
                sig->nivcsw += tsk->nivcsw;
-               sig->sched_time += tsk->sched_time;
+               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. */
        }
 
@@ -170,7 +182,6 @@ repeat:
                zap_leader = (leader->exit_signal == -1);
        }
 
-       sched_exit(p);
        write_unlock_irq(&tasklist_lock);
        proc_flush_task(p);
        release_thread(p);
@@ -185,21 +196,19 @@ 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 = 0;
-
-       read_lock(&tasklist_lock);
+       struct pid *sid = NULL;
 
-       p = find_task_by_pid_type(PIDTYPE_PGID, pgrp);
+       p = pid_task(pgrp, PIDTYPE_PGID);
        if (p == NULL)
-               p = find_task_by_pid(pgrp);
+               p = pid_task(pgrp, PIDTYPE_PID);
        if (p != NULL)
-               sid = process_session(p);
-
-       read_unlock(&tasklist_lock);
+               sid = task_session(p);
 
        return sid;
 }
@@ -212,78 +221,76 @@ int session_of_pgrp(int pgrp)
  *
  * "I ask you, have you ever known what it is to be an orphan?"
  */
-static int will_become_orphaned_pgrp(int pgrp, struct task_struct *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
                                || is_init(p->real_parent))
                        continue;
-               if (process_group(p->real_parent) != pgrp &&
-                   process_session(p->real_parent) == process_session(p)) {
+               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;
                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
- * of the pid space that the thread belongs to.
+ * 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);
-       current->real_parent = child_reaper(current);
+       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 (!has_rt_policy(current) && (task_nice(current) < 0))
+       if (task_nice(current) < 0)
                set_user_nice(current, 0);
        /* cpus_allowed? */
        /* rt_priority? */
@@ -303,12 +310,12 @@ void __set_special_pids(pid_t session, pid_t pgrp)
        if (process_session(curr) != session) {
                detach_pid(curr, PIDTYPE_SID);
                set_signal_session(curr->signal, session);
-               attach_pid(curr, PIDTYPE_SID, 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));
        }
 }
 
@@ -351,7 +358,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;
@@ -396,7 +403,7 @@ void daemonize(const char *name, ...)
        current->fs = fs;
        atomic_inc(&fs->count);
 
-       put_and_finalize_nsproxy(current->nsproxy);
+       exit_task_namespaces(current);
        current->nsproxy = init_task.nsproxy;
        get_task_namespaces(current);
 
@@ -404,7 +411,7 @@ void daemonize(const char *name, ...)
        current->files = init_task.files;
        atomic_inc(&current->files->count);
 
-       reparent_to_init();
+       reparent_to_kthreadd();
 }
 
 EXPORT_SYMBOL(daemonize);
@@ -431,8 +438,10 @@ static void close_files(struct files_struct * files)
                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;
@@ -649,14 +658,14 @@ reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
         * than we are, and it was the only connection
         * outside, so the child pgrp is now orphaned.
         */
-       if ((process_group(p) != process_group(father)) &&
-           (process_session(p) == process_session(father))) {
-               int pgrp = process_group(p);
+       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_pg_info(SIGHUP, SEND_SIG_PRIV, pgrp);
-                       __kill_pg_info(SIGCONT, SEND_SIG_PRIV, pgrp);
+                       __kill_pgrp_info(SIGHUP, SEND_SIG_PRIV, pgrp);
+                       __kill_pgrp_info(SIGCONT, SEND_SIG_PRIV, pgrp);
                }
        }
 }
@@ -736,6 +745,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)) {
@@ -751,11 +761,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);
        }
@@ -788,12 +795,13 @@ static void exit_notify(struct task_struct *tsk)
         
        t = tsk->real_parent;
        
-       if ((process_group(t) != process_group(tsk)) &&
-           (process_session(t) == process_session(tsk)) &&
-           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 
@@ -853,7 +861,6 @@ static void exit_notify(struct task_struct *tsk)
 fastcall NORET_TYPE void do_exit(long code)
 {
        struct task_struct *tsk = current;
-       struct nsproxy *ns;
        int group_dead;
 
        profile_task_exit(tsk);
@@ -884,13 +891,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 are checked in the futex code to protect against
+        * an exiting task cleaning up the robust pi futexes.
+        */
+       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",
@@ -904,7 +927,7 @@ 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_collect(code, group_dead);
@@ -939,9 +962,8 @@ fastcall NORET_TYPE void do_exit(long code)
 
        tsk->exit_code = code;
        proc_exit_connector(tsk);
-       ns = preexit_task_namespaces(tsk);
+       exit_task_namespaces(tsk);
        exit_notify(tsk);
-       exit_task_namespaces(tsk, ns);
 #ifdef CONFIG_NUMA
        mpol_free(tsk->mempolicy);
        tsk->mempolicy = NULL;
@@ -958,6 +980,12 @@ fastcall NORET_TYPE void do_exit(long code)
         * 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();
@@ -1034,6 +1062,8 @@ asmlinkage void sys_exit_group(int error_code)
 
 static int eligible_child(pid_t pid, int options, struct task_struct *p)
 {
+       int err;
+
        if (pid > 0) {
                if (p->pid != pid)
                        return 0;
@@ -1067,8 +1097,9 @@ static int eligible_child(pid_t pid, int options, struct task_struct *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;
 }
@@ -1192,6 +1223,12 @@ static int wait_task_zombie(struct task_struct *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);
        }
 
@@ -1450,6 +1487,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:
@@ -1458,6 +1496,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;
@@ -1473,6 +1512,12 @@ repeat:
                        if (!ret)
                                continue;
 
+                       if (unlikely(ret < 0)) {
+                               denied = ret;
+                               continue;
+                       }
+                       allowed = 1;
+
                        switch (p->state) {
                        case TASK_TRACED:
                                /*
@@ -1571,6 +1616,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);