Refactor sigswitch() trunk
authorkamil <kamil@NetBSD.org>
Sat, 12 Oct 2019 19:57:09 +0000
branchtrunk
changeset 455278 fd8e3d06d020
parent 455277 c610775a4cc7
child 455279 cf45951ccca7
Refactor sigswitch() Make the function static as it is now local to kern_sig.c. Rename the 'relock' argument to 'proc_lock_held' as it is more verbose. This was suggested by mjg@freebsd. While there this flips the users between true<->false. Add additional KASSERT(9) calls here to validate whethe proc_lock is used accordingly.
sys/kern/kern_sig.c
sys/sys/signalvar.h
--- a/sys/kern/kern_sig.c	Sat Oct 12 19:38:57 2019 +0000
+++ b/sys/kern/kern_sig.c	Sat Oct 12 19:57:09 2019 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_sig.c,v 1.368 2019/10/12 10:55:23 kamil Exp $	*/
+/*	$NetBSD: kern_sig.c,v 1.369 2019/10/12 19:57:09 kamil Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -70,7 +70,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.368 2019/10/12 10:55:23 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_sig.c,v 1.369 2019/10/12 19:57:09 kamil Exp $");
 
 #include "opt_ptrace.h"
 #include "opt_dtrace.h"
@@ -125,6 +125,7 @@
 static int	sigpost(struct lwp *, sig_t, int, int);
 static int	sigput(sigpend_t *, struct proc *, ksiginfo_t *);
 static int	sigunwait(struct proc *, const ksiginfo_t *);
+static void	sigswitch(int, int, bool);
 
 static void	sigacts_poolpage_free(struct pool *, void *);
 static void	*sigacts_poolpage_alloc(struct pool *, int);
@@ -931,7 +932,7 @@
 	 * The process is already stopping.
 	 */
 	if ((p->p_sflag & PS_STOPPING) != 0) {
-		sigswitch(0, p->p_xsig, false);
+		sigswitch(0, p->p_xsig, true);
 		mutex_enter(proc_lock);
 		mutex_enter(p->p_lock);
 		goto repeat; /* XXX */
@@ -949,7 +950,7 @@
 		p->p_sigctx.ps_faked = true;
 		p->p_sigctx.ps_lwp = ksi->ksi_lid;
 		p->p_sigctx.ps_info = ksi->ksi_info;
-		sigswitch(0, signo, false);
+		sigswitch(0, signo, true);
 
 		if (ktrpoint(KTR_PSIG)) {
 			if (p->p_emul->e_ktrpsig)
@@ -1641,7 +1642,7 @@
 	 * The process is already stopping.
 	 */
 	if ((p->p_sflag & PS_STOPPING) != 0) {
-		sigswitch(0, p->p_xsig, false);
+		sigswitch(0, p->p_xsig, true);
 		mutex_enter(proc_lock);
 		mutex_enter(p->p_lock);
 		goto repeat; /* XXX */
@@ -1666,7 +1667,7 @@
 	p->p_sigctx.ps_lwp = ksi.ksi_lid;
 	p->p_sigctx.ps_info = ksi.ksi_info;
 
-	sigswitch(0, signo, false);
+	sigswitch(0, signo, true);
 
 	if (code == TRAP_CHLD) {
 		mutex_enter(proc_lock);
@@ -1686,8 +1687,8 @@
 /*
  * Stop the current process and switch away when being stopped or traced.
  */
-void
-sigswitch(int ppmask, int signo, bool relock)
+static void
+sigswitch(int ppmask, int signo, bool proc_lock_held)
 {
 	struct lwp *l = curlwp;
 	struct proc *p = l->l_proc;
@@ -1697,6 +1698,12 @@
 	KASSERT(l->l_stat == LSONPROC);
 	KASSERT(p->p_nrlwps > 0);
 
+	if (proc_lock_held) {
+		KASSERT(mutex_owned(proc_lock));
+	} else {
+		KASSERT(!mutex_owned(proc_lock));
+	}
+
 	/*
 	 * If we are exiting, demise now.
 	 *
@@ -1704,7 +1711,7 @@
 	 */
 	if (__predict_false(ISSET(p->p_sflag, PS_WEXIT))) {
 		mutex_exit(p->p_lock);
-		if (!relock) {
+		if (proc_lock_held) {
 			mutex_exit(proc_lock);
 		}
 		lwp_exit(l);
@@ -1728,7 +1735,7 @@
 	 * a new signal, then signal the parent.
 	 */
 	if ((p->p_sflag & PS_STOPPING) != 0) {
-		if (relock && !mutex_tryenter(proc_lock)) {
+		if (!proc_lock_held && !mutex_tryenter(proc_lock)) {
 			mutex_exit(p->p_lock);
 			mutex_enter(proc_lock);
 			mutex_enter(p->p_lock);
@@ -1748,6 +1755,7 @@
 	/*
 	 * Unlock and switch away.
 	 */
+	KASSERT(!mutex_owned(proc_lock));
 	KERNEL_UNLOCK_ALL(l, &biglocks);
 	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
 		p->p_nrlwps--;
@@ -1837,7 +1845,7 @@
 		 * we awaken, check for a signal from the debugger.
 		 */
 		if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
-			sigswitch(PS_NOCLDSTOP, 0, true);
+			sigswitch(PS_NOCLDSTOP, 0, false);
 			mutex_enter(p->p_lock);
 			signo = sigchecktrace();
 		} else if (p->p_stat == SACTIVE)
@@ -1909,7 +1917,7 @@
 			p->p_xsig = signo;
 
 			/* Handling of signal trace */
-			sigswitch(0, signo, true);
+			sigswitch(0, signo, false);
 			mutex_enter(p->p_lock);
 
 			/* Check for a signal from the debugger. */
@@ -1966,7 +1974,7 @@
 				p->p_xsig = signo;
 				p->p_sflag &= ~PS_CONTINUED;
 				signo = 0;
-				sigswitch(PS_NOCLDSTOP, p->p_xsig, true);
+				sigswitch(PS_NOCLDSTOP, p->p_xsig, false);
 				mutex_enter(p->p_lock);
 			} else if (prop & SA_IGNORE) {
 				/*
@@ -2519,7 +2527,7 @@
 	 * The process is already stopping.
 	 */
 	if ((p->p_sflag & PS_STOPPING) != 0) {
-		sigswitch(0, p->p_xsig, true);
+		sigswitch(0, p->p_xsig, false);
 		mutex_enter(p->p_lock);
 		goto repeat; /* XXX */
 	}
@@ -2532,7 +2540,7 @@
 	p->p_xsig = signo;
 	p->p_sigctx.ps_lwp = ksi.ksi_lid;
 	p->p_sigctx.ps_info = ksi.ksi_info;
-	sigswitch(0, signo, true);
+	sigswitch(0, signo, false);
 
 	if (ktrpoint(KTR_PSIG)) {
 		if (p->p_emul->e_ktrpsig)
--- a/sys/sys/signalvar.h	Sat Oct 12 19:38:57 2019 +0000
+++ b/sys/sys/signalvar.h	Sat Oct 12 19:57:09 2019 +0000
@@ -1,4 +1,4 @@
-/*	$NetBSD: signalvar.h,v 1.96 2019/09/30 21:13:33 kamil Exp $	*/
+/*	$NetBSD: signalvar.h,v 1.97 2019/10/12 19:57:09 kamil Exp $	*/
 
 /*
  * Copyright (c) 1991, 1993
@@ -153,7 +153,6 @@
 int	killpg1(struct lwp *, struct ksiginfo *, int, int);
 void	proc_unstop(struct proc *p);
 void	eventswitch(int, int, int);
-void	sigswitch(int, int, bool);
 
 
 int	sigaction1(struct lwp *, int, const struct sigaction *,