[PATCH 19/26] wait: Simpler code for clearing notask_error in wait_consider_task

From: Eric W. Biederman
Date: Tue Jun 06 2017 - 15:16:25 EST


Start with reaping pending zombies and then return if there is nothing
wait_task_consider can ever do with the task.

What remains are living tasks and delayed child zombie thread group
leaders waiting for their thread group to exit. Leaving something for
WEXITED to find later.

As long as WEXITED can happen the code is guaranteed not to block
indefinitely, as at least the exit event will wake it up. The only
reason not to clear notask_error is if it is impossible for wait
to find something to report. For zombie thread group leaders it
is possible that living threads will report group wide continued
or stopped states.

Which means we can now safely and practically always clear notask_error.

Signed-off-by: "Eric W. Biederman" <ebiederm@xxxxxxxxxxxx>
---
kernel/exit.c | 62 ++++++++++++++++++++++-------------------------------------
1 file changed, 23 insertions(+), 39 deletions(-)

diff --git a/kernel/exit.c b/kernel/exit.c
index 4e2d2b6f5581..8f3825b22de5 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -1359,47 +1359,31 @@ static int wait_consider_task(struct wait_opts *wo, int ptrace,
if ((exit_state == EXIT_TRACEE) && ptrace)
return wait_task_zombie(wo, exit_state, p);

- if (unlikely(exit_state == EXIT_TRACED)) {
- /*
- * ptrace == 0 means we are the natural parent. In this case
- * we should clear notask_error, debugger will notify us.
- */
- if (likely(!ptrace))
- wo->notask_error = 0;
+ /* Is this task past the point where ptrace cares? */
+ if (unlikely((exit_state == EXIT_TRACED) && ptrace))
return 0;
- }

- if (exit_state == EXIT_ZOMBIE) {
- /*
- * Allow access to stopped/continued state via zombie by
- * falling through. Clearing of notask_error is complex.
- *
- * When !@ptrace:
- *
- * If WEXITED is set, notask_error should naturally be
- * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
- * so, if there are live subthreads, there are events to
- * wait for. If all subthreads are dead, it's still safe
- * to clear - this function will be called again in finite
- * amount time once all the subthreads are released and
- * will then return without clearing.
- *
- * When @ptrace:
- *
- * Stopped state is per-task and thus can't change once the
- * target task dies. Only continued and exited can happen.
- * Clear notask_error if WCONTINUED | WEXITED.
- */
- if ((!ptrace && (!p->ptrace || ptrace_reparented(p))) ||
- (wo->wo_flags & (WCONTINUED | WEXITED)))
- wo->notask_error = 0;
- } else {
- /*
- * @p is alive and it's gonna stop, continue or exit, so
- * there always is something to wait for.
- */
- wo->notask_error = 0;
- }
+ /*
+ * A this point @p is alive or the zombie of a delayed
+ * child thread group leader that has not been reaped yet.
+ *
+ * Allow access to stopped/continued state via zombie by
+ * falling through. Clearing of notask_error is logically complex.
+ *
+ * When @p is alive and it's gonna stop, continue or exit,
+ * so there always is something to wait for.
+ *
+ * When @p is a zombie
+ *
+ * If WEXITED is set, notask_error should naturally be
+ * cleared. If not, subset of WSTOPPED|WCONTINUED is set,
+ * so, if there are live subthreads, there are events to
+ * wait for. If all subthreads are dead, it's still safe
+ * to clear - this function will be called again in finite
+ * amount time once all the subthreads are released and
+ * will then return without clearing.
+ */
+ wo->notask_error = 0;

/*
* Wait for stopped.
--
2.10.1