Commit 98abed02 authored by Roland McGrath's avatar Roland McGrath

do_wait reorganization

This breaks out the guts of do_wait into three subfunctions.
The control flow is less nonobvious without so much goto.
do_wait_thread and ptrace_do_wait contain the main work of the outer loop.
wait_consider_task contains the main work of the inner loop.
Signed-off-by: default avatarRoland McGrath <roland@redhat.com>
parent 33af79d1
...@@ -1238,7 +1238,7 @@ static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid, ...@@ -1238,7 +1238,7 @@ static int wait_noreap_copyout(struct task_struct *p, pid_t pid, uid_t uid,
* the lock and this task is uninteresting. If we return nonzero, we have * the lock and this task is uninteresting. If we return nonzero, we have
* released the lock and the system call should return. * released the lock and the system call should return.
*/ */
static int wait_task_zombie(struct task_struct *p, int noreap, static int wait_task_zombie(struct task_struct *p, int options,
struct siginfo __user *infop, struct siginfo __user *infop,
int __user *stat_addr, struct rusage __user *ru) int __user *stat_addr, struct rusage __user *ru)
{ {
...@@ -1246,7 +1246,10 @@ static int wait_task_zombie(struct task_struct *p, int noreap, ...@@ -1246,7 +1246,10 @@ static int wait_task_zombie(struct task_struct *p, int noreap,
int retval, status, traced; int retval, status, traced;
pid_t pid = task_pid_vnr(p); pid_t pid = task_pid_vnr(p);
if (unlikely(noreap)) { if (!likely(options & WEXITED))
return 0;
if (unlikely(options & WNOWAIT)) {
uid_t uid = p->uid; uid_t uid = p->uid;
int exit_code = p->exit_code; int exit_code = p->exit_code;
int why, status; int why, status;
...@@ -1397,13 +1400,16 @@ static int wait_task_zombie(struct task_struct *p, int noreap, ...@@ -1397,13 +1400,16 @@ static int wait_task_zombie(struct task_struct *p, int noreap,
* released the lock and the system call should return. * released the lock and the system call should return.
*/ */
static int wait_task_stopped(struct task_struct *p, static int wait_task_stopped(struct task_struct *p,
int noreap, struct siginfo __user *infop, int options, struct siginfo __user *infop,
int __user *stat_addr, struct rusage __user *ru) int __user *stat_addr, struct rusage __user *ru)
{ {
int retval, exit_code, why; int retval, exit_code, why;
uid_t uid = 0; /* unneeded, required by compiler */ uid_t uid = 0; /* unneeded, required by compiler */
pid_t pid; pid_t pid;
if (!(p->ptrace & PT_PTRACED) && !(options & WUNTRACED))
return 0;
exit_code = 0; exit_code = 0;
spin_lock_irq(&p->sighand->siglock); spin_lock_irq(&p->sighand->siglock);
...@@ -1421,7 +1427,7 @@ static int wait_task_stopped(struct task_struct *p, ...@@ -1421,7 +1427,7 @@ static int wait_task_stopped(struct task_struct *p,
if (!exit_code) if (!exit_code)
goto unlock_sig; goto unlock_sig;
if (!noreap) if (!unlikely(options & WNOWAIT))
p->exit_code = 0; p->exit_code = 0;
uid = p->uid; uid = p->uid;
...@@ -1442,7 +1448,7 @@ static int wait_task_stopped(struct task_struct *p, ...@@ -1442,7 +1448,7 @@ static int wait_task_stopped(struct task_struct *p,
why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED; why = (p->ptrace & PT_PTRACED) ? CLD_TRAPPED : CLD_STOPPED;
read_unlock(&tasklist_lock); read_unlock(&tasklist_lock);
if (unlikely(noreap)) if (unlikely(options & WNOWAIT))
return wait_noreap_copyout(p, pid, uid, return wait_noreap_copyout(p, pid, uid,
why, exit_code, why, exit_code,
infop, ru); infop, ru);
...@@ -1476,7 +1482,7 @@ static int wait_task_stopped(struct task_struct *p, ...@@ -1476,7 +1482,7 @@ static int wait_task_stopped(struct task_struct *p,
* the lock and this task is uninteresting. If we return nonzero, we have * the lock and this task is uninteresting. If we return nonzero, we have
* released the lock and the system call should return. * released the lock and the system call should return.
*/ */
static int wait_task_continued(struct task_struct *p, int noreap, static int wait_task_continued(struct task_struct *p, int options,
struct siginfo __user *infop, struct siginfo __user *infop,
int __user *stat_addr, struct rusage __user *ru) int __user *stat_addr, struct rusage __user *ru)
{ {
...@@ -1484,6 +1490,9 @@ static int wait_task_continued(struct task_struct *p, int noreap, ...@@ -1484,6 +1490,9 @@ static int wait_task_continued(struct task_struct *p, int noreap,
pid_t pid; pid_t pid;
uid_t uid; uid_t uid;
if (!unlikely(options & WCONTINUED))
return 0;
if (!(p->signal->flags & SIGNAL_STOP_CONTINUED)) if (!(p->signal->flags & SIGNAL_STOP_CONTINUED))
return 0; return 0;
...@@ -1493,7 +1502,7 @@ static int wait_task_continued(struct task_struct *p, int noreap, ...@@ -1493,7 +1502,7 @@ static int wait_task_continued(struct task_struct *p, int noreap,
spin_unlock_irq(&p->sighand->siglock); spin_unlock_irq(&p->sighand->siglock);
return 0; return 0;
} }
if (!noreap) if (!unlikely(options & WNOWAIT))
p->signal->flags &= ~SIGNAL_STOP_CONTINUED; p->signal->flags &= ~SIGNAL_STOP_CONTINUED;
spin_unlock_irq(&p->sighand->siglock); spin_unlock_irq(&p->sighand->siglock);
...@@ -1519,89 +1528,137 @@ static int wait_task_continued(struct task_struct *p, int noreap, ...@@ -1519,89 +1528,137 @@ static int wait_task_continued(struct task_struct *p, int noreap,
return retval; return retval;
} }
/*
* Consider @p for a wait by @parent.
*
* -ECHILD should be in *@notask_error before the first call.
* Returns nonzero for a final return, when we have unlocked tasklist_lock.
* Returns zero if the search for a child should continue;
* then *@notask_error is 0 if @p is an eligible child, or still -ECHILD.
*/
static int wait_consider_task(struct task_struct *parent,
struct task_struct *p, int *notask_error,
enum pid_type type, struct pid *pid, int options,
struct siginfo __user *infop,
int __user *stat_addr, struct rusage __user *ru)
{
int ret = eligible_child(type, pid, options, p);
if (ret <= 0)
return ret;
if (p->exit_state == EXIT_DEAD)
return 0;
/*
* We don't reap group leaders with subthreads.
*/
if (p->exit_state == EXIT_ZOMBIE && !delay_group_leader(p))
return wait_task_zombie(p, options, infop, stat_addr, ru);
/*
* It's stopped or running now, so it might
* later continue, exit, or stop again.
*/
*notask_error = 0;
if (task_is_stopped_or_traced(p))
return wait_task_stopped(p, options, infop, stat_addr, ru);
return wait_task_continued(p, options, infop, stat_addr, ru);
}
/*
* Do the work of do_wait() for one thread in the group, @tsk.
*
* -ECHILD should be in *@notask_error before the first call.
* Returns nonzero for a final return, when we have unlocked tasklist_lock.
* Returns zero if the search for a child should continue; then
* *@notask_error is 0 if there were any eligible children, or still -ECHILD.
*/
static int do_wait_thread(struct task_struct *tsk, int *notask_error,
enum pid_type type, struct pid *pid, int options,
struct siginfo __user *infop, int __user *stat_addr,
struct rusage __user *ru)
{
struct task_struct *p;
list_for_each_entry(p, &tsk->children, sibling) {
int ret = wait_consider_task(tsk, p, notask_error,
type, pid, options,
infop, stat_addr, ru);
if (ret)
return ret;
}
return 0;
}
static int ptrace_do_wait(struct task_struct *tsk, int *notask_error,
enum pid_type type, struct pid *pid, int options,
struct siginfo __user *infop, int __user *stat_addr,
struct rusage __user *ru)
{
struct task_struct *p;
/*
* If we never saw an eligile child, check for children stolen by
* ptrace. We don't leave -ECHILD in *@notask_error if there are any,
* because we will eventually be allowed to wait for them again.
*/
if (!*notask_error)
return 0;
list_for_each_entry(p, &tsk->ptrace_children, ptrace_list) {
int ret = eligible_child(type, pid, options, p);
if (unlikely(ret < 0))
return ret;
if (ret) {
*notask_error = 0;
return 0;
}
}
return 0;
}
static long do_wait(enum pid_type type, struct pid *pid, int options, static long do_wait(enum pid_type type, struct pid *pid, int options,
struct siginfo __user *infop, int __user *stat_addr, struct siginfo __user *infop, int __user *stat_addr,
struct rusage __user *ru) struct rusage __user *ru)
{ {
DECLARE_WAITQUEUE(wait, current); DECLARE_WAITQUEUE(wait, current);
struct task_struct *tsk; struct task_struct *tsk;
int flag, retval; int retval;
add_wait_queue(&current->signal->wait_chldexit,&wait); add_wait_queue(&current->signal->wait_chldexit,&wait);
repeat: repeat:
/* If there is nothing that can match our critier just get out */ /*
* If there is nothing that can match our critiera just get out.
* We will clear @retval to zero if we see any child that might later
* match our criteria, even if we are not able to reap it yet.
*/
retval = -ECHILD; retval = -ECHILD;
if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type]))) if ((type < PIDTYPE_MAX) && (!pid || hlist_empty(&pid->tasks[type])))
goto end; goto end;
/*
* We will set this flag if we see any child that might later
* match our criteria, even if we are not able to reap it yet.
*/
flag = retval = 0;
current->state = TASK_INTERRUPTIBLE; current->state = TASK_INTERRUPTIBLE;
read_lock(&tasklist_lock); read_lock(&tasklist_lock);
tsk = current; tsk = current;
do { do {
struct task_struct *p; int tsk_result = do_wait_thread(tsk, &retval,
type, pid, options,
list_for_each_entry(p, &tsk->children, sibling) { infop, stat_addr, ru);
int ret = eligible_child(type, pid, options, p); if (!tsk_result)
if (!ret) tsk_result = ptrace_do_wait(tsk, &retval,
continue; type, pid, options,
infop, stat_addr, ru);
if (unlikely(ret < 0)) { if (tsk_result) {
retval = ret; /*
} else if (task_is_stopped_or_traced(p)) { * tasklist_lock is unlocked and we have a final result.
/* */
* It's stopped now, so it might later retval = tsk_result;
* continue, exit, or stop again. goto end;
*/
flag = 1;
if (!(p->ptrace & PT_PTRACED) &&
!(options & WUNTRACED))
continue;
retval = wait_task_stopped(p,
(options & WNOWAIT), infop,
stat_addr, ru);
} else if (p->exit_state == EXIT_ZOMBIE &&
!delay_group_leader(p)) {
/*
* We don't reap group leaders with subthreads.
*/
if (!likely(options & WEXITED))
continue;
retval = wait_task_zombie(p,
(options & WNOWAIT), infop,
stat_addr, ru);
} else if (p->exit_state != EXIT_DEAD) {
/*
* It's running now, so it might later
* exit, stop, or stop and then continue.
*/
flag = 1;
if (!unlikely(options & WCONTINUED))
continue;
retval = wait_task_continued(p,
(options & WNOWAIT), infop,
stat_addr, ru);
}
if (retval != 0) /* tasklist_lock released */
goto end;
}
if (!flag) {
list_for_each_entry(p, &tsk->ptrace_children,
ptrace_list) {
flag = eligible_child(type, pid, options, p);
if (!flag)
continue;
if (likely(flag > 0))
break;
retval = flag;
goto end;
}
} }
if (options & __WNOTHREAD) if (options & __WNOTHREAD)
break; break;
tsk = next_thread(tsk); tsk = next_thread(tsk);
...@@ -1609,16 +1666,14 @@ static long do_wait(enum pid_type type, struct pid *pid, int options, ...@@ -1609,16 +1666,14 @@ static long do_wait(enum pid_type type, struct pid *pid, int options,
} while (tsk != current); } while (tsk != current);
read_unlock(&tasklist_lock); read_unlock(&tasklist_lock);
if (flag) { if (!retval && !(options & WNOHANG)) {
if (options & WNOHANG)
goto end;
retval = -ERESTARTSYS; retval = -ERESTARTSYS;
if (signal_pending(current)) if (!signal_pending(current)) {
goto end; schedule();
schedule(); goto repeat;
goto repeat; }
} }
retval = -ECHILD;
end: end:
current->state = TASK_RUNNING; current->state = TASK_RUNNING;
remove_wait_queue(&current->signal->wait_chldexit,&wait); remove_wait_queue(&current->signal->wait_chldexit,&wait);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment