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