Module Name: src Committed By: bouyer Date: Sun Nov 15 20:38:18 UTC 2015
Modified Files: src/sys/kern [netbsd-6-1]: kern_exec.c kern_exit.c kern_synch.c Log Message: Pull up following revision(s) (requested by pgoyette in ticket #1333): sys/kern/kern_exec.c: revision 1.420 sys/kern/kern_synch.c: revision 1.309 sys/kern/kern_exit.c: revision 1.246 sys/kern/kern_exit.c: revision 1.247 sys/kern/kern_exec.c: revision 1.419 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 Pullups will be requested for: NetBSD-7, -6, -6-0, -6-1, -5, -5-0, -5-1, and -5-2 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 Pullups will be requested for: NetBSD-7, -6, -6-0, -6-1, -5, -5-0, -5-1, and -5-2 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 Pullups will be requested for: NetBSD-7, -6, -6-0, -6-1, -5, -5-0, -5-1, and -5-2 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 Pullups will be requested for: NetBSD-7, -6, -6-0, -6-1, -5, -5-0, -5-1, and -5-2 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 Pullups will be requested for NetBSD-7, -6, -6-0, and -6-1 To generate a diff of this commit: cvs rdiff -u -r1.339.2.6.2.2 -r1.339.2.6.2.3 src/sys/kern/kern_exec.c cvs rdiff -u -r1.236.2.2 -r1.236.2.2.4.1 src/sys/kern/kern_exit.c cvs rdiff -u -r1.297.2.1 -r1.297.2.1.6.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.339.2.6.2.2 src/sys/kern/kern_exec.c:1.339.2.6.2.3 --- src/sys/kern/kern_exec.c:1.339.2.6.2.2 Mon Apr 21 10:00:33 2014 +++ src/sys/kern/kern_exec.c Sun Nov 15 20:38:17 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_exec.c,v 1.339.2.6.2.2 2014/04/21 10:00:33 bouyer Exp $ */ +/* $NetBSD: kern_exec.c,v 1.339.2.6.2.3 2015/11/15 20:38:17 bouyer 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.339.2.6.2.2 2014/04/21 10:00:33 bouyer Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.339.2.6.2.3 2015/11/15 20:38:17 bouyer Exp $"); #include "opt_exec.h" #include "opt_ktrace.h" @@ -1408,7 +1408,7 @@ execve_runproc(struct lwp *l, struct exe if (p->p_sflag & PS_STOPEXEC) { 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); @@ -1845,6 +1845,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; @@ -1917,7 +1918,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; @@ -1926,8 +1926,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) { @@ -1940,7 +1950,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 */ @@ -1954,7 +1964,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) { @@ -1962,12 +1972,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 */ @@ -1977,7 +1987,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) { @@ -1988,7 +1998,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 */ @@ -2015,6 +2028,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.236.2.2 src/sys/kern/kern_exit.c:1.236.2.2.4.1 --- src/sys/kern/kern_exit.c:1.236.2.2 Mon Oct 1 23:07:07 2012 +++ src/sys/kern/kern_exit.c Sun Nov 15 20:38:18 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_exit.c,v 1.236.2.2 2012/10/01 23:07:07 riz Exp $ */ +/* $NetBSD: kern_exit.c,v 1.236.2.2.4.1 2015/11/15 20:38:18 bouyer 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.236.2.2 2012/10/01 23:07:07 riz Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_exit.c,v 1.236.2.2.4.1 2015/11/15 20:38:18 bouyer Exp $"); #include "opt_ktrace.h" #include "opt_perfctrs.h" @@ -240,8 +240,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--; @@ -1000,7 +1007,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.297.2.1 src/sys/kern/kern_synch.c:1.297.2.1.6.1 --- src/sys/kern/kern_synch.c:1.297.2.1 Sun Aug 19 17:36:41 2012 +++ src/sys/kern/kern_synch.c Sun Nov 15 20:38:18 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: kern_synch.c,v 1.297.2.1 2012/08/19 17:36:41 riz Exp $ */ +/* $NetBSD: kern_synch.c,v 1.297.2.1.6.1 2015/11/15 20:38:18 bouyer 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.297.2.1 2012/08/19 17:36:41 riz Exp $"); +__KERNEL_RCSID(0, "$NetBSD: kern_synch.c,v 1.297.2.1.6.1 2015/11/15 20:38:18 bouyer Exp $"); #include "opt_kstack.h" #include "opt_perfctrs.h" @@ -1010,7 +1010,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)