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