]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - kernel/exit.c
Merge branch 'x86-cleanups-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[karo-tx-linux.git] / kernel / exit.c
index fd90195667e1129bf06b96c805e43ba519301be4..84ae830234f8fea6328690ebf977a7063aa91097 100644 (file)
@@ -210,6 +210,82 @@ repeat:
                goto repeat;
 }
 
+/*
+ * Note that if this function returns a valid task_struct pointer (!NULL)
+ * task->usage must remain >0 for the duration of the RCU critical section.
+ */
+struct task_struct *task_rcu_dereference(struct task_struct **ptask)
+{
+       struct sighand_struct *sighand;
+       struct task_struct *task;
+
+       /*
+        * We need to verify that release_task() was not called and thus
+        * delayed_put_task_struct() can't run and drop the last reference
+        * before rcu_read_unlock(). We check task->sighand != NULL,
+        * but we can read the already freed and reused memory.
+        */
+retry:
+       task = rcu_dereference(*ptask);
+       if (!task)
+               return NULL;
+
+       probe_kernel_address(&task->sighand, sighand);
+
+       /*
+        * Pairs with atomic_dec_and_test() in put_task_struct(). If this task
+        * was already freed we can not miss the preceding update of this
+        * pointer.
+        */
+       smp_rmb();
+       if (unlikely(task != READ_ONCE(*ptask)))
+               goto retry;
+
+       /*
+        * We've re-checked that "task == *ptask", now we have two different
+        * cases:
+        *
+        * 1. This is actually the same task/task_struct. In this case
+        *    sighand != NULL tells us it is still alive.
+        *
+        * 2. This is another task which got the same memory for task_struct.
+        *    We can't know this of course, and we can not trust
+        *    sighand != NULL.
+        *
+        *    In this case we actually return a random value, but this is
+        *    correct.
+        *
+        *    If we return NULL - we can pretend that we actually noticed that
+        *    *ptask was updated when the previous task has exited. Or pretend
+        *    that probe_slab_address(&sighand) reads NULL.
+        *
+        *    If we return the new task (because sighand is not NULL for any
+        *    reason) - this is fine too. This (new) task can't go away before
+        *    another gp pass.
+        *
+        *    And note: We could even eliminate the false positive if re-read
+        *    task->sighand once again to avoid the falsely NULL. But this case
+        *    is very unlikely so we don't care.
+        */
+       if (!sighand)
+               return NULL;
+
+       return task;
+}
+
+struct task_struct *try_get_task_struct(struct task_struct **ptask)
+{
+       struct task_struct *task;
+
+       rcu_read_lock();
+       task = task_rcu_dereference(ptask);
+       if (task)
+               get_task_struct(task);
+       rcu_read_unlock();
+
+       return task;
+}
+
 /*
  * Determine if a process group is "orphaned", according to the POSIX
  * definition in 2.2.2.52.  Orphaned process groups are not to be affected
@@ -700,10 +776,14 @@ void do_exit(long code)
 
        exit_signals(tsk);  /* sets PF_EXITING */
        /*
-        * tsk->flags are checked in the futex code to protect against
-        * an exiting task cleaning up the robust pi futexes.
+        * Ensure that all new tsk->pi_lock acquisitions must observe
+        * PF_EXITING. Serializes against futex.c:attach_to_pi_owner().
         */
        smp_mb();
+       /*
+        * Ensure that we must observe the pi_state in exit_mm() ->
+        * mm_release() -> exit_pi_state_list().
+        */
        raw_spin_unlock_wait(&tsk->pi_lock);
 
        if (unlikely(in_atomic())) {
@@ -746,7 +826,7 @@ void do_exit(long code)
                disassociate_ctty(1);
        exit_task_namespaces(tsk);
        exit_task_work(tsk);
-       exit_thread();
+       exit_thread(tsk);
 
        /*
         * Flush inherited counters to the parent - before the parent
@@ -918,17 +998,28 @@ static int eligible_pid(struct wait_opts *wo, struct task_struct *p)
                task_pid_type(p, wo->wo_type) == wo->wo_pid;
 }
 
-static int eligible_child(struct wait_opts *wo, struct task_struct *p)
+static int
+eligible_child(struct wait_opts *wo, bool ptrace, struct task_struct *p)
 {
        if (!eligible_pid(wo, p))
                return 0;
-       /* Wait for all children (clone and not) if __WALL is set;
-        * otherwise, wait for clone children *only* if __WCLONE is
-        * set; otherwise, wait for non-clone children *only*.  (Note:
-        * A "clone" child here is one that reports to its parent
-        * using a signal other than SIGCHLD.) */
-       if (((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
-           && !(wo->wo_flags & __WALL))
+
+       /*
+        * Wait for all children (clone and not) if __WALL is set or
+        * if it is traced by us.
+        */
+       if (ptrace || (wo->wo_flags & __WALL))
+               return 1;
+
+       /*
+        * Otherwise, wait for clone children *only* if __WCLONE is set;
+        * otherwise, wait for non-clone children *only*.
+        *
+        * Note: a "clone" child here is one that reports to its parent
+        * using a signal other than SIGCHLD, or a non-leader thread which
+        * we can only see if it is traced by us.
+        */
+       if ((p->exit_signal != SIGCHLD) ^ !!(wo->wo_flags & __WCLONE))
                return 0;
 
        return 1;
@@ -1300,7 +1391,7 @@ static int wait_consider_task(struct wait_opts *wo, int ptrace,
        if (unlikely(exit_state == EXIT_DEAD))
                return 0;
 
-       ret = eligible_child(wo, p);
+       ret = eligible_child(wo, ptrace, p);
        if (!ret)
                return ret;
 
@@ -1524,7 +1615,8 @@ SYSCALL_DEFINE5(waitid, int, which, pid_t, upid, struct siginfo __user *,
        enum pid_type type;
        long ret;
 
-       if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED))
+       if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED|
+                       __WNOTHREAD|__WCLONE|__WALL))
                return -EINVAL;
        if (!(options & (WEXITED|WSTOPPED|WCONTINUED)))
                return -EINVAL;