Module Name: src Committed By: snj Date: Thu Nov 5 09:04:55 UTC 2015
Modified Files: src/sys/kern [netbsd-7]: kern_exec.c kern_exit.c kern_synch.c Log Message: Pull up following revision(s) (requested by pgoyette in ticket #996): sys/kern/kern_exec.c: revisions 1.419, 1.420 sys/kern/kern_exit.c: revisions 1.246, 1.247 sys/kern/kern_synch.c: revision 1.309 In execve_runproc(), update the p_waited entry for the process being moved to SSTOP state, not for its parent. (It is correct to update the parent's p_nstopchild count.) If the value is not already zero, it could prevent its parent from waiting for the process. Fixes PR kern/50298 -- When clearing out the scheduler queues during system shutdown, we move all processes to the SSTOP state. Make sure we update each process's p_waited and the parents' p_nstopchild counters to maintain consistent values. Should not make any real difference this late in the shutdown process, but we should still be consistent just in case. Fixes PR kern/50318 -- Currently, if a process is exiting and its parent has indicated no intent of reaping the process (nor any other children), the process wil get reparented to init. Since the state of the exiting process at this point is SDEAD, proc_reparent() will not update either the old or new parent's p_nstopchild counters. This change causes both old and new parents to be properly updated. Fixes PR kern/50300 -- For processes marked with PS_STOPEXIT, update the process's p_waited value, and update its parent's p_nstopchild value when marking the process's p_stat to SSTOP. The process needed to be SACTIVE to get here, so this transition represents an additional process for which the parent needs to wait. Fixes PR kern/50308 -- In spawn_return() we temporarily move the process state to SSTOP, but without updating its p_waited value or its parent's p_nstopchild counter. Later, we restore the original state, again without any adjustment of the related values. This leaves a relatively short window when the values are inconsistent and could interfere with the proper operation of sys_wait() for the parent (if it manages to be scheduled; it's not totally clear what, if anything, prevents scheduling/execution of the parent). If during this window, any of the checks being made result in an error, we call exit1() which will eventually migrate the process's state to SDEAD (with an intermediate transition to SDYING). At this point the other variables get updated, and we finally restore a consistent state. This change updates the p_waited and parent's p_nstopchild at each step to eliminate any windows during which the values could lead to incorrect decisions. Fixes PR kern/50330 To generate a diff of this commit: cvs rdiff -u -r1.408.2.3 -r1.408.2.4 src/sys/kern/kern_exec.c cvs rdiff -u -r1.244 -r1.244.2.1 src/sys/kern/kern_exit.c cvs rdiff -u -r1.308 -r1.308.4.1 src/sys/kern/kern_synch.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/kern/kern_exec.c diff -u src/sys/kern/kern_exec.c:1.408.2.3 src/sys/kern/kern_exec.c:1.408.2.4 --- src/sys/kern/kern_exec.c:1.408.2.3 Tue Apr 14 05:12:17 2015 +++ src/sys/kern/kern_exec.c Thu Nov 5 09:04:55 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_exec.c,v 1.408.2.3 2015/04/14 05:12:17 snj Exp $ */ +/* $NetBSD: kern_exec.c,v 1.408.2.4 2015/11/05 09:04:55 snj Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -59,7 +59,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.408.2.3 2015/04/14 05:12:17 snj Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.408.2.4 2015/11/05 09:04:55 snj Exp $"); #include "opt_exec.h" #include "opt_execfmt.h" @@ -1260,7 +1260,7 @@ execve_runproc(struct lwp *l, struct exe KERNEL_UNLOCK_ALL(l, &l->l_biglocks); p->p_pptr->p_nstopchild++; - p->p_pptr->p_waited = 0; + p->p_waited = 0; mutex_enter(p->p_lock); ksiginfo_queue_init(&kq); sigclearall(p, &contsigmask, &kq); @@ -1961,6 +1961,7 @@ spawn_return(void *arg) struct spawn_exec_data *spawn_data = arg; struct lwp *l = curlwp; int error, newfd; + int ostat; size_t i; const struct posix_spawn_file_actions_entry *fae; pid_t ppid; @@ -2033,7 +2034,6 @@ spawn_return(void *arg) /* handle posix_spawnattr */ if (spawn_data->sed_attrs != NULL) { - int ostat; struct sigaction sigact; sigact._sa_u._sa_handler = SIG_DFL; sigact.sa_flags = 0; @@ -2042,8 +2042,18 @@ spawn_return(void *arg) * set state to SSTOP so that this proc can be found by pid. * see proc_enterprp, do_sched_setparam below */ + mutex_enter(proc_lock); + /* + * p_stat should be SACTIVE, so we need to adjust the + * parent's p_nstopchild here. For safety, just make + * we're on the good side of SDEAD before we adjust. + */ ostat = l->l_proc->p_stat; + KASSERT(ostat < SSTOP); l->l_proc->p_stat = SSTOP; + l->l_proc->p_waited = 0; + l->l_proc->p_pptr->p_nstopchild++; + mutex_exit(proc_lock); /* Set process group */ if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETPGROUP) { @@ -2056,7 +2066,7 @@ spawn_return(void *arg) error = proc_enterpgrp(spawn_data->sed_parent, mypid, pgrp, false); if (error) - goto report_error; + goto report_error_stopped; } /* Set scheduler policy */ @@ -2070,7 +2080,7 @@ spawn_return(void *arg) SCHED_NONE, &spawn_data->sed_attrs->sa_schedparam); } if (error) - goto report_error; + goto report_error_stopped; /* Reset user ID's */ if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_RESETIDS) { @@ -2078,12 +2088,12 @@ spawn_return(void *arg) kauth_cred_getgid(l->l_cred), -1, ID_E_EQ_R | ID_E_EQ_S); if (error) - goto report_error; + goto report_error_stopped; error = do_setresuid(l, -1, kauth_cred_getuid(l->l_cred), -1, ID_E_EQ_R | ID_E_EQ_S); if (error) - goto report_error; + goto report_error_stopped; } /* Set signal masks/defaults */ @@ -2093,7 +2103,7 @@ spawn_return(void *arg) &spawn_data->sed_attrs->sa_sigmask, NULL); mutex_exit(l->l_proc->p_lock); if (error) - goto report_error; + goto report_error_stopped; } if (spawn_data->sed_attrs->sa_flags & POSIX_SPAWN_SETSIGDEF) { @@ -2114,7 +2124,10 @@ spawn_return(void *arg) 0); } } + mutex_enter(proc_lock); l->l_proc->p_stat = ostat; + l->l_proc->p_pptr->p_nstopchild--; + mutex_exit(proc_lock); } /* now do the real exec */ @@ -2141,6 +2154,11 @@ spawn_return(void *arg) /* NOTREACHED */ return; + report_error_stopped: + mutex_enter(proc_lock); + l->l_proc->p_stat = ostat; + l->l_proc->p_pptr->p_nstopchild--; + mutex_exit(proc_lock); report_error: if (have_reflock) { /* Index: src/sys/kern/kern_exit.c diff -u src/sys/kern/kern_exit.c:1.244 src/sys/kern/kern_exit.c:1.244.2.1 --- src/sys/kern/kern_exit.c:1.244 Mon May 5 15:45:32 2014 +++ src/sys/kern/kern_exit.c Thu Nov 5 09:04:55 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_exit.c,v 1.244 2014/05/05 15:45:32 christos Exp $ */ +/* $NetBSD: kern_exit.c,v 1.244.2.1 2015/11/05 09:04:55 snj Exp $ */ /*- * Copyright (c) 1998, 1999, 2006, 2007, 2008 The NetBSD Foundation, Inc. @@ -67,7 +67,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.244 2014/05/05 15:45:32 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.244.2.1 2015/11/05 09:04:55 snj Exp $"); #include "opt_ktrace.h" #include "opt_perfctrs.h" @@ -227,8 +227,15 @@ exit1(struct lwp *l, int rv) if (__predict_false(p->p_sflag & PS_STOPEXIT)) { KERNEL_UNLOCK_ALL(l, &l->l_biglocks); sigclearall(p, &contsigmask, &kq); + + if (!mutex_tryenter(proc_lock)) { + mutex_exit(p->p_lock); + mutex_enter(proc_lock); + mutex_enter(p->p_lock); + } p->p_waited = 0; - membar_producer(); + p->p_pptr->p_nstopchild++; + mutex_exit(proc_lock); p->p_stat = SSTOP; lwp_lock(l); p->p_nrlwps--; @@ -959,7 +966,7 @@ proc_reparent(struct proc *child, struct if (child->p_pptr == parent) return; - if (child->p_stat == SZOMB || + if (child->p_stat == SZOMB || child->p_stat == SDEAD || (child->p_stat == SSTOP && !child->p_waited)) { child->p_pptr->p_nstopchild--; parent->p_nstopchild++; Index: src/sys/kern/kern_synch.c diff -u src/sys/kern/kern_synch.c:1.308 src/sys/kern/kern_synch.c:1.308.4.1 --- src/sys/kern/kern_synch.c:1.308 Fri Feb 28 10:16:51 2014 +++ src/sys/kern/kern_synch.c Thu Nov 5 09:04:55 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_synch.c,v 1.308 2014/02/28 10:16:51 skrll Exp $ */ +/* $NetBSD: kern_synch.c,v 1.308.4.1 2015/11/05 09:04:55 snj Exp $ */ /*- * Copyright (c) 1999, 2000, 2004, 2006, 2007, 2008, 2009 @@ -69,7 +69,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.308 2014/02/28 10:16:51 skrll Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.308.4.1 2015/11/05 09:04:55 snj Exp $"); #include "opt_kstack.h" #include "opt_perfctrs.h" @@ -985,7 +985,13 @@ suspendsched(void) continue; } - p->p_stat = SSTOP; + if (p->p_stat != SSTOP) { + if (p->p_stat != SZOMB && p->p_stat != SDEAD) { + p->p_pptr->p_nstopchild++; + p->p_waited = 0; + } + p->p_stat = SSTOP; + } LIST_FOREACH(l, &p->p_lwps, l_sibling) { if (l == curlwp)