Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys/arch Make cpu_intr_p() safe to use anywhere, i.e. outsid...



details:   https://anonhg.NetBSD.org/src/rev/de2621e64bc5
branches:  trunk
changeset: 465657:de2621e64bc5
user:      ad <ad%NetBSD.org@localhost>
date:      Sun Dec 01 14:52:13 2019 +0000

description:
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.

diffstat:

 sys/arch/arm/arm/arm_machdep.c     |  25 ++++++++++++++++++++-----
 sys/arch/mips/mips/cpu_subr.c      |  20 +++++++++++++-------
 sys/arch/sparc/sparc/intr.c        |  14 +++++++++-----
 sys/arch/sparc64/sparc64/machdep.c |  15 ++++++++++++---
 sys/arch/usermode/dev/cpu.c        |  16 ++++++++++------
 sys/arch/x86/x86/x86_machdep.c     |  17 +++++++++++------
 6 files changed, 75 insertions(+), 32 deletions(-)

diffs (242 lines):

diff -r e78f1a3f1e67 -r de2621e64bc5 sys/arch/arm/arm/arm_machdep.c
--- a/sys/arch/arm/arm/arm_machdep.c    Sun Dec 01 14:43:26 2019 +0000
+++ b/sys/arch/arm/arm/arm_machdep.c    Sun Dec 01 14:52:13 2019 +0000
@@ -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 @@
 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
diff -r e78f1a3f1e67 -r de2621e64bc5 sys/arch/mips/mips/cpu_subr.c
--- a/sys/arch/mips/mips/cpu_subr.c     Sun Dec 01 14:43:26 2019 +0000
+++ b/sys/arch/mips/mips/cpu_subr.c     Sun Dec 01 14:52:13 2019 +0000
@@ -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 @@
 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
diff -r e78f1a3f1e67 -r de2621e64bc5 sys/arch/sparc/sparc/intr.c
--- a/sys/arch/sparc/sparc/intr.c       Sun Dec 01 14:43:26 2019 +0000
+++ b/sys/arch/sparc/sparc/intr.c       Sun Dec 01 14:52:13 2019 +0000
@@ -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 @@
 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;
 }
diff -r e78f1a3f1e67 -r de2621e64bc5 sys/arch/sparc64/sparc64/machdep.c
--- a/sys/arch/sparc64/sparc64/machdep.c        Sun Dec 01 14:43:26 2019 +0000
+++ b/sys/arch/sparc64/sparc64/machdep.c        Sun Dec 01 14:52:13 2019 +0000
@@ -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 @@
 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
diff -r e78f1a3f1e67 -r de2621e64bc5 sys/arch/usermode/dev/cpu.c
--- a/sys/arch/usermode/dev/cpu.c       Sun Dec 01 14:43:26 2019 +0000
+++ b/sys/arch/usermode/dev/cpu.c       Sun Dec 01 14:52:13 2019 +0000
@@ -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 <jmcneill%invisible.ca@localhost>
@@ -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 @@
 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;
 }
diff -r e78f1a3f1e67 -r de2621e64bc5 sys/arch/x86/x86/x86_machdep.c
--- a/sys/arch/x86/x86/x86_machdep.c    Sun Dec 01 14:43:26 2019 +0000
+++ b/sys/arch/x86/x86/x86_machdep.c    Sun Dec 01 14:52:13 2019 +0000
@@ -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 @@
 bool
 cpu_intr_p(void)
 {
+       uint64_t ncsw;
        int idepth;
+       lwp_t *l;
 
-       kpreempt_disable();
-       idepth = curcpu()->ci_idepth;
-       kpreempt_enable();
-       return (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 __HAVE_PREEMPTION



Home | Main Index | Thread Index | Old Index