Module Name:    src
Committed By:   ad
Date:           Sun Dec  1 14:52:14 UTC 2019

Modified Files:
        src/sys/arch/arm/arm: arm_machdep.c
        src/sys/arch/mips/mips: cpu_subr.c
        src/sys/arch/sparc/sparc: intr.c
        src/sys/arch/sparc64/sparc64: machdep.c
        src/sys/arch/usermode/dev: cpu.c
        src/sys/arch/x86/x86: x86_machdep.c

Log Message:
Make cpu_intr_p() safe to use anywhere, i.e. outside assertions:

Don't call kpreempt_disable() / kpreempt_enable() to make sure we're not
preempted while using the value of curcpu().  Instead, observe the value of
l_ncsw before and after the check to see if we have been preempted.  If
we have been preempted, then we need to retry the read.


To generate a diff of this commit:
cvs rdiff -u -r1.56 -r1.57 src/sys/arch/arm/arm/arm_machdep.c
cvs rdiff -u -r1.37 -r1.38 src/sys/arch/mips/mips/cpu_subr.c
cvs rdiff -u -r1.121 -r1.122 src/sys/arch/sparc/sparc/intr.c
cvs rdiff -u -r1.293 -r1.294 src/sys/arch/sparc64/sparc64/machdep.c
cvs rdiff -u -r1.81 -r1.82 src/sys/arch/usermode/dev/cpu.c
cvs rdiff -u -r1.129 -r1.130 src/sys/arch/x86/x86/x86_machdep.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/arch/arm/arm/arm_machdep.c
diff -u src/sys/arch/arm/arm/arm_machdep.c:1.56 src/sys/arch/arm/arm/arm_machdep.c:1.57
--- src/sys/arch/arm/arm/arm_machdep.c:1.56	Sat Nov 23 19:40:34 2019
+++ src/sys/arch/arm/arm/arm_machdep.c	Sun Dec  1 14:52:13 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: arm_machdep.c,v 1.56 2019/11/23 19:40:34 ad Exp $	*/
+/*	$NetBSD: arm_machdep.c,v 1.57 2019/12/01 14:52:13 ad Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -80,7 +80,7 @@
 
 #include <sys/param.h>
 
-__KERNEL_RCSID(0, "$NetBSD: arm_machdep.c,v 1.56 2019/11/23 19:40:34 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: arm_machdep.c,v 1.57 2019/12/01 14:52:13 ad Exp $");
 
 #include <sys/exec.h>
 #include <sys/proc.h>
@@ -260,12 +260,27 @@ cpu_need_resched(struct cpu_info *ci, st
 bool
 cpu_intr_p(void)
 {
-	struct cpu_info * const ci = curcpu();
 #ifdef __HAVE_PIC_FAST_SOFTINTS
-	if (ci->ci_cpl < IPL_VM)
+	int cpl;
+#endif
+	uint64_t ncsw;
+	int idepth;
+	lwp_t *l;
+
+	l = curlwp;
+	do {
+		ncsw = l->l_ncsw;
+		idepth = l->l_cpu->ci_intr_depth;
+#ifdef __HAVE_PIC_FAST_SOFTINTS
+		cpl = ci->ci_cpl;
+#endif
+	} while (__predict_false(ncsw != l->l_ncsw));
+
+#ifdef __HAVE_PIC_FAST_SOFTINTS
+	if (cpl < IPL_VM)
 		return false;
 #endif
-	return ci->ci_intr_depth != 0;
+	return idepth != 0;
 }
 
 #ifdef MODULAR

Index: src/sys/arch/mips/mips/cpu_subr.c
diff -u src/sys/arch/mips/mips/cpu_subr.c:1.37 src/sys/arch/mips/mips/cpu_subr.c:1.38
--- src/sys/arch/mips/mips/cpu_subr.c:1.37	Sun Nov 24 15:37:39 2019
+++ src/sys/arch/mips/mips/cpu_subr.c	Sun Dec  1 14:52:13 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu_subr.c,v 1.37 2019/11/24 15:37:39 ad Exp $	*/
+/*	$NetBSD: cpu_subr.c,v 1.38 2019/12/01 14:52:13 ad Exp $	*/
 
 /*-
  * Copyright (c) 2010, 2019 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cpu_subr.c,v 1.37 2019/11/24 15:37:39 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu_subr.c,v 1.38 2019/12/01 14:52:13 ad Exp $");
 
 #include "opt_cputype.h"
 #include "opt_ddb.h"
@@ -604,11 +604,17 @@ cpu_idle(void)
 bool
 cpu_intr_p(void)
 {
-	bool rv;
-	kpreempt_disable();
-	rv = (curcpu()->ci_idepth != 0);
-	kpreempt_enable();
-	return rv;
+	uint64_t ncsw;
+	int idepth;
+	lwp_t *l;
+
+	l = curlwp;
+	do {
+		ncsw = l->l_ncsw;
+		idepth = l->l_cpu->ci_idepth;
+	} while (__predict_false(ncsw != l->l_ncsw));
+
+	return idepth != 0;
 }
 
 #ifdef MULTIPROCESSOR

Index: src/sys/arch/sparc/sparc/intr.c
diff -u src/sys/arch/sparc/sparc/intr.c:1.121 src/sys/arch/sparc/sparc/intr.c:1.122
--- src/sys/arch/sparc/sparc/intr.c:1.121	Fri Mar  1 02:33:55 2019
+++ src/sys/arch/sparc/sparc/intr.c	Sun Dec  1 14:52:14 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: intr.c,v 1.121 2019/03/01 02:33:55 macallan Exp $ */
+/*	$NetBSD: intr.c,v 1.122 2019/12/01 14:52:14 ad Exp $ */
 
 /*
  * Copyright (c) 1992, 1993
@@ -41,7 +41,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.121 2019/03/01 02:33:55 macallan Exp $");
+__KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.122 2019/12/01 14:52:14 ad Exp $");
 
 #include "opt_multiprocessor.h"
 #include "opt_sparc_arch.h"
@@ -889,11 +889,15 @@ intr_biglock_wrapper(void *vp)
 bool
 cpu_intr_p(void)
 {
+	uint64_t ncsw;
 	int idepth;
+	lwp_t *l;
 
-	kpreempt_disable();
-	idepth = curcpu()->ci_idepth;
-	kpreempt_enable();
+	l = curlwp;
+	do {
+		ncsw = l->l_ncsw;
+		idepth = l->l_cpu->ci_idepth;
+	} while (__predict_false(ncsw != l->l_ncsw));
 
 	return idepth != 0;
 }

Index: src/sys/arch/sparc64/sparc64/machdep.c
diff -u src/sys/arch/sparc64/sparc64/machdep.c:1.293 src/sys/arch/sparc64/sparc64/machdep.c:1.294
--- src/sys/arch/sparc64/sparc64/machdep.c:1.293	Sat Nov 23 19:40:37 2019
+++ src/sys/arch/sparc64/sparc64/machdep.c	Sun Dec  1 14:52:14 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.293 2019/11/23 19:40:37 ad Exp $ */
+/*	$NetBSD: machdep.c,v 1.294 2019/12/01 14:52:14 ad Exp $ */
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2019 The NetBSD Foundation, Inc.
@@ -71,7 +71,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.293 2019/11/23 19:40:37 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.294 2019/12/01 14:52:14 ad Exp $");
 
 #include "opt_ddb.h"
 #include "opt_multiprocessor.h"
@@ -2654,8 +2654,17 @@ cpu_signotify(struct lwp *l)
 bool
 cpu_intr_p(void)
 {
+	uint64_t ncsw;
+	int idepth;
+	lwp_t *l;
 
-	return curcpu()->ci_idepth >= 0;
+	l = curlwp;
+	do {
+		ncsw = l->l_ncsw;
+		idepth = l->l_cpu->ci_idepth;
+	} while (__predict_false(ncsw != l->l_ncsw));
+
+	return idepth >= 0;
 }
 
 #ifdef MODULAR

Index: src/sys/arch/usermode/dev/cpu.c
diff -u src/sys/arch/usermode/dev/cpu.c:1.81 src/sys/arch/usermode/dev/cpu.c:1.82
--- src/sys/arch/usermode/dev/cpu.c:1.81	Sat Nov 23 19:40:37 2019
+++ src/sys/arch/usermode/dev/cpu.c	Sun Dec  1 14:52:14 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: cpu.c,v 1.81 2019/11/23 19:40:37 ad Exp $ */
+/* $NetBSD: cpu.c,v 1.82 2019/12/01 14:52:14 ad Exp $ */
 
 /*-
  * Copyright (c) 2007 Jared D. McNeill <jmcne...@invisible.ca>
@@ -30,7 +30,7 @@
 #include "opt_hz.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.81 2019/11/23 19:40:37 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.82 2019/12/01 14:52:14 ad Exp $");
 
 #include <sys/param.h>
 #include <sys/conf.h>
@@ -528,11 +528,15 @@ cpu_rootconf(void)
 bool
 cpu_intr_p(void)
 {
+	uint64_t ncsw;
 	int idepth;
+	lwp_t *l;
 
-	kpreempt_disable();
-	idepth = curcpu()->ci_idepth;
-	kpreempt_enable();
+	l = curlwp;
+	do {
+		ncsw = l->l_ncsw;
+		idepth = l->l_cpu->ci_idepth;
+	} while (__predict_false(ncsw != l->l_ncsw));
 
-	return (idepth >= 0);
+	return idepth >= 0;
 }

Index: src/sys/arch/x86/x86/x86_machdep.c
diff -u src/sys/arch/x86/x86/x86_machdep.c:1.129 src/sys/arch/x86/x86/x86_machdep.c:1.130
--- src/sys/arch/x86/x86/x86_machdep.c:1.129	Sat Nov 23 19:40:37 2019
+++ src/sys/arch/x86/x86/x86_machdep.c	Sun Dec  1 14:52:14 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: x86_machdep.c,v 1.129 2019/11/23 19:40:37 ad Exp $	*/
+/*	$NetBSD: x86_machdep.c,v 1.130 2019/12/01 14:52:14 ad Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2006, 2007 YAMAMOTO Takashi,
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.129 2019/11/23 19:40:37 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: x86_machdep.c,v 1.130 2019/12/01 14:52:14 ad Exp $");
 
 #include "opt_modular.h"
 #include "opt_physmem.h"
@@ -349,12 +349,17 @@ cpu_need_proftick(struct lwp *l)
 bool
 cpu_intr_p(void)
 {
+	uint64_t ncsw;
 	int idepth;
+	lwp_t *l;
+
+	l = curlwp;
+	do {
+		ncsw = l->l_ncsw;
+		idepth = l->l_cpu->ci_idepth;
+	} while (__predict_false(ncsw != l->l_ncsw));
 
-	kpreempt_disable();
-	idepth = curcpu()->ci_idepth;
-	kpreempt_enable();
-	return (idepth >= 0);
+	return idepth >= 0;
 }
 
 #ifdef __HAVE_PREEMPTION

Reply via email to