Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[linux-drm-fsl-dcu.git] / kernel / exit.c
index 03e64fe4a14af6b12a1b3638e6d54263a589af47..f132349c032569c94d4b3cd710b0c74fe8d0eec5 100644 (file)
@@ -185,21 +185,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,53 +210,52 @@ 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_init - Reparent the calling kernel thread to the init task of the pid space that the thread belongs to.
  *
  * 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
@@ -425,14 +422,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;
@@ -466,9 +465,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);
        }
@@ -599,10 +596,6 @@ choose_new_parent(struct task_struct *p, struct task_struct *reaper)
 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);
@@ -622,13 +615,7 @@ reparent_thread(struct task_struct *p, struct task_struct *father, int traced)
                p->parent = p->real_parent;
                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
@@ -638,20 +625,37 @@ reparent_thread(struct task_struct *p, struct task_struct *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)) &&
-           (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);
                }
        }
 }
@@ -731,6 +735,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)) {
@@ -783,12 +788,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 
@@ -933,8 +939,8 @@ fastcall NORET_TYPE void do_exit(long code)
 
        tsk->exit_code = code;
        proc_exit_connector(tsk);
-       exit_notify(tsk);
        exit_task_namespaces(tsk);
+       exit_notify(tsk);
 #ifdef CONFIG_NUMA
        mpol_free(tsk->mempolicy);
        tsk->mempolicy = NULL;