Sun Apr 9 08:17:45 2023 UTC ()
pserialize(9): Micro-optimize pserialize_not_in_read_section_p.

Load l_ncsw to test whether we have been preempted, rather than
loading and storing l_nopreempt (via function call) to prevent it.


(riastradh)
diff -r1.19 -r1.20 src/sys/kern/subr_pserialize.c

cvs diff -r1.19 -r1.20 src/sys/kern/subr_pserialize.c (switch to unified diff)

--- src/sys/kern/subr_pserialize.c 2022/11/15 10:29:56 1.19
+++ src/sys/kern/subr_pserialize.c 2023/04/09 08:17:45 1.20
@@ -1,182 +1,197 @@ @@ -1,182 +1,197 @@
1/* $NetBSD: subr_pserialize.c,v 1.19 2022/11/15 10:29:56 macallan Exp $ */ 1/* $NetBSD: subr_pserialize.c,v 1.20 2023/04/09 08:17:45 riastradh Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc. 4 * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 */ 27 */
28 28
29/* 29/*
30 * Passive serialization. 30 * Passive serialization.
31 */ 31 */
32 32
33#include <sys/cdefs.h> 33#include <sys/cdefs.h>
34__KERNEL_RCSID(0, "$NetBSD: subr_pserialize.c,v 1.19 2022/11/15 10:29:56 macallan Exp $"); 34__KERNEL_RCSID(0, "$NetBSD: subr_pserialize.c,v 1.20 2023/04/09 08:17:45 riastradh Exp $");
35 35
36#include <sys/param.h> 36#include <sys/param.h>
 37
37#include <sys/atomic.h> 38#include <sys/atomic.h>
38#include <sys/cpu.h> 39#include <sys/cpu.h>
39#include <sys/kernel.h> 
40#include <sys/evcnt.h> 40#include <sys/evcnt.h>
 41#include <sys/kernel.h>
41#include <sys/kmem.h> 42#include <sys/kmem.h>
 43#include <sys/lwp.h>
42#include <sys/mutex.h> 44#include <sys/mutex.h>
43#include <sys/pserialize.h> 45#include <sys/pserialize.h>
44#include <sys/xcall.h> 46#include <sys/xcall.h>
45 47
46struct pserialize { 48struct pserialize {
47 char psz_dummy; 49 char psz_dummy;
48}; 50};
49 51
50static kmutex_t psz_lock __cacheline_aligned; 52static kmutex_t psz_lock __cacheline_aligned;
51static struct evcnt psz_ev_excl __cacheline_aligned = 53static struct evcnt psz_ev_excl __cacheline_aligned =
52 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pserialize", "exclusive access"); 54 EVCNT_INITIALIZER(EVCNT_TYPE_MISC, NULL, "pserialize", "exclusive access");
53EVCNT_ATTACH_STATIC(psz_ev_excl); 55EVCNT_ATTACH_STATIC(psz_ev_excl);
54 56
55/* 57/*
56 * pserialize_init: 58 * pserialize_init:
57 * 59 *
58 * Initialize passive serialization structures. 60 * Initialize passive serialization structures.
59 */ 61 */
60void 62void
61pserialize_init(void) 63pserialize_init(void)
62{ 64{
63 65
64 mutex_init(&psz_lock, MUTEX_DEFAULT, IPL_NONE); 66 mutex_init(&psz_lock, MUTEX_DEFAULT, IPL_NONE);
65} 67}
66 68
67/* 69/*
68 * pserialize_create: 70 * pserialize_create:
69 * 71 *
70 * Create and initialize a passive serialization object. 72 * Create and initialize a passive serialization object.
71 */ 73 */
72pserialize_t 74pserialize_t
73pserialize_create(void) 75pserialize_create(void)
74{ 76{
75 pserialize_t psz; 77 pserialize_t psz;
76 78
77 psz = kmem_zalloc(sizeof(*psz), KM_SLEEP); 79 psz = kmem_zalloc(sizeof(*psz), KM_SLEEP);
78 return psz; 80 return psz;
79} 81}
80 82
81/* 83/*
82 * pserialize_destroy: 84 * pserialize_destroy:
83 * 85 *
84 * Destroy a passive serialization object. 86 * Destroy a passive serialization object.
85 */ 87 */
86void 88void
87pserialize_destroy(pserialize_t psz) 89pserialize_destroy(pserialize_t psz)
88{ 90{
89 91
90 kmem_free(psz, sizeof(*psz)); 92 kmem_free(psz, sizeof(*psz));
91} 93}
92 94
93/* 95/*
94 * pserialize_perform: 96 * pserialize_perform:
95 * 97 *
96 * Perform the write side of passive serialization. 98 * Perform the write side of passive serialization.
97 */ 99 */
98void 100void
99pserialize_perform(pserialize_t psz) 101pserialize_perform(pserialize_t psz)
100{ 102{
101 103
102 KASSERT(!cpu_intr_p()); 104 KASSERT(!cpu_intr_p());
103 KASSERT(!cpu_softintr_p()); 105 KASSERT(!cpu_softintr_p());
104 106
105 if (__predict_false(panicstr != NULL)) { 107 if (__predict_false(panicstr != NULL)) {
106 return; 108 return;
107 } 109 }
108 110
109 if (__predict_false(mp_online == false)) { 111 if (__predict_false(mp_online == false)) {
110 psz_ev_excl.ev_count++; 112 psz_ev_excl.ev_count++;
111 return; 113 return;
112 } 114 }
113 115
114 /* 116 /*
115 * Broadcast a NOP to all CPUs and wait until all of them complete. 117 * Broadcast a NOP to all CPUs and wait until all of them complete.
116 */ 118 */
117 xc_barrier(XC_HIGHPRI); 119 xc_barrier(XC_HIGHPRI);
118 120
119 mutex_enter(&psz_lock); 121 mutex_enter(&psz_lock);
120 psz_ev_excl.ev_count++; 122 psz_ev_excl.ev_count++;
121 mutex_exit(&psz_lock); 123 mutex_exit(&psz_lock);
122} 124}
123 125
124int 126int
125pserialize_read_enter(void) 127pserialize_read_enter(void)
126{ 128{
127 int s; 129 int s;
128 130
129 s = splsoftserial(); 131 s = splsoftserial();
130 curcpu()->ci_psz_read_depth++; 132 curcpu()->ci_psz_read_depth++;
131 __insn_barrier(); 133 __insn_barrier();
132 return s; 134 return s;
133} 135}
134 136
135void 137void
136pserialize_read_exit(int s) 138pserialize_read_exit(int s)
137{ 139{
138 140
139 KASSERT((cold || kpreempt_disabled())); 141 KASSERT((cold || kpreempt_disabled()));
140 142
141 __insn_barrier(); 143 __insn_barrier();
142 if (__predict_false(curcpu()->ci_psz_read_depth-- == 0)) 144 if (__predict_false(curcpu()->ci_psz_read_depth-- == 0))
143 panic("mismatching pserialize_read_exit()"); 145 panic("mismatching pserialize_read_exit()");
144 splx(s); 146 splx(s);
145} 147}
146 148
147/* 149/*
148 * pserialize_in_read_section: 150 * pserialize_in_read_section:
149 * 151 *
150 * True if the caller is in a pserialize read section. To be used 152 * True if the caller is in a pserialize read section. To be used
151 * only for diagnostic assertions where we want to guarantee the 153 * only for diagnostic assertions where we want to guarantee the
152 * condition like: 154 * condition like:
153 * 155 *
154 * KASSERT(pserialize_in_read_section()); 156 * KASSERT(pserialize_in_read_section());
155 */ 157 */
156bool 158bool
157pserialize_in_read_section(void) 159pserialize_in_read_section(void)
158{ 160{
159 161
160 return kpreempt_disabled() && curcpu()->ci_psz_read_depth > 0; 162 return kpreempt_disabled() && curcpu()->ci_psz_read_depth > 0;
161} 163}
162 164
163/* 165/*
164 * pserialize_not_in_read_section: 166 * pserialize_not_in_read_section:
165 * 167 *
166 * True if the caller is not in a pserialize read section. To be 168 * True if the caller is not in a pserialize read section. To be
167 * used only for diagnostic assertions where we want to guarantee 169 * used only for diagnostic assertions where we want to guarantee
168 * the condition like: 170 * the condition like:
169 * 171 *
170 * KASSERT(pserialize_not_in_read_section()); 172 * KASSERT(pserialize_not_in_read_section());
171 */ 173 */
172bool 174bool
173pserialize_not_in_read_section(void) 175pserialize_not_in_read_section(void)
174{ 176{
 177 struct lwp *l = curlwp;
 178 uint64_t ncsw;
175 bool notin; 179 bool notin;
176 180
177 kpreempt_disable(); 181 ncsw = l->l_ncsw;
178 notin = (curcpu()->ci_psz_read_depth == 0); 182 __insn_barrier();
179 kpreempt_enable(); 183 notin = __predict_true(l->l_cpu->ci_psz_read_depth == 0);
 184 __insn_barrier();
 185
 186 /*
 187 * If we had a context switch, we're definitely not in a
 188 * pserialize read section because pserialize read sections
 189 * block preemption.
 190 */
 191 if (__predict_false(ncsw != l->l_ncsw)) {
 192 KDASSERT(notin);
 193 notin = true;
 194 }
180 195
181 return notin; 196 return notin;
182} 197}