subr_pserialize.c revision 1.15 1 1.15 martin /* $NetBSD: subr_pserialize.c,v 1.15 2019/12/03 13:30:52 martin 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.15 martin __KERNEL_RCSID(0, "$NetBSD: subr_pserialize.c,v 1.15 2019/12/03 13:30:52 martin 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.2 he #include <sys/evcnt.h>
40 1.1 christos #include <sys/kmem.h>
41 1.1 christos #include <sys/pserialize.h>
42 1.1 christos #include <sys/xcall.h>
43 1.1 christos
44 1.1 christos struct pserialize {
45 1.1 christos lwp_t * psz_owner;
46 1.1 christos };
47 1.1 christos
48 1.1 christos static struct evcnt psz_ev_excl __cacheline_aligned;
49 1.1 christos
50 1.1 christos /*
51 1.1 christos * pserialize_init:
52 1.1 christos *
53 1.1 christos * Initialize passive serialization structures.
54 1.1 christos */
55 1.1 christos void
56 1.1 christos pserialize_init(void)
57 1.1 christos {
58 1.1 christos
59 1.1 christos evcnt_attach_dynamic(&psz_ev_excl, EVCNT_TYPE_MISC, NULL,
60 1.1 christos "pserialize", "exclusive access");
61 1.1 christos }
62 1.1 christos
63 1.1 christos /*
64 1.1 christos * pserialize_create:
65 1.1 christos *
66 1.1 christos * Create and initialize a passive serialization object.
67 1.1 christos */
68 1.1 christos pserialize_t
69 1.1 christos pserialize_create(void)
70 1.1 christos {
71 1.1 christos pserialize_t psz;
72 1.1 christos
73 1.14 riastrad psz = kmem_zalloc(sizeof(*psz), KM_SLEEP);
74 1.1 christos return psz;
75 1.1 christos }
76 1.1 christos
77 1.1 christos /*
78 1.1 christos * pserialize_destroy:
79 1.1 christos *
80 1.1 christos * Destroy a passive serialization object.
81 1.1 christos */
82 1.1 christos void
83 1.1 christos pserialize_destroy(pserialize_t psz)
84 1.1 christos {
85 1.1 christos
86 1.1 christos KASSERT(psz->psz_owner == NULL);
87 1.14 riastrad kmem_free(psz, sizeof(*psz));
88 1.1 christos }
89 1.1 christos
90 1.1 christos /*
91 1.1 christos * pserialize_perform:
92 1.1 christos *
93 1.14 riastrad * Perform the write side of passive serialization. This operation
94 1.14 riastrad * MUST be serialized at a caller level (e.g. with a mutex or by a
95 1.14 riastrad * single-threaded use).
96 1.1 christos */
97 1.1 christos void
98 1.1 christos pserialize_perform(pserialize_t psz)
99 1.1 christos {
100 1.1 christos
101 1.1 christos KASSERT(!cpu_intr_p());
102 1.1 christos KASSERT(!cpu_softintr_p());
103 1.1 christos
104 1.1 christos if (__predict_false(panicstr != NULL)) {
105 1.1 christos return;
106 1.1 christos }
107 1.1 christos KASSERT(psz->psz_owner == NULL);
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.14 riastrad psz->psz_owner = curlwp;
115 1.14 riastrad
116 1.1 christos /*
117 1.14 riastrad * Broadcast a NOP to all CPUs and wait until all of them complete.
118 1.1 christos */
119 1.14 riastrad xc_barrier(XC_HIGHPRI);
120 1.1 christos
121 1.14 riastrad KASSERT(psz->psz_owner == curlwp);
122 1.1 christos psz->psz_owner = NULL;
123 1.15 martin #ifdef __HAVE_ATOMIC64_LOADSTORE
124 1.14 riastrad atomic_store_relaxed(&psz_ev_excl.ev_count,
125 1.14 riastrad 1 + atomic_load_relaxed(&psz_ev_excl.ev_count));
126 1.15 martin #endif
127 1.1 christos }
128 1.1 christos
129 1.1 christos int
130 1.1 christos pserialize_read_enter(void)
131 1.1 christos {
132 1.9 ozaki int s;
133 1.1 christos
134 1.9 ozaki s = splsoftserial();
135 1.14 riastrad curcpu()->ci_psz_read_depth++;
136 1.14 riastrad __insn_barrier();
137 1.9 ozaki return s;
138 1.1 christos }
139 1.1 christos
140 1.1 christos void
141 1.1 christos pserialize_read_exit(int s)
142 1.1 christos {
143 1.1 christos
144 1.14 riastrad KASSERT(kpreempt_disabled());
145 1.14 riastrad
146 1.14 riastrad __insn_barrier();
147 1.14 riastrad if (__predict_false(curcpu()->ci_psz_read_depth-- == 0))
148 1.14 riastrad panic("mismatching pserialize_read_exit()");
149 1.1 christos splx(s);
150 1.1 christos }
151 1.1 christos
152 1.1 christos /*
153 1.9 ozaki * pserialize_in_read_section:
154 1.9 ozaki *
155 1.14 riastrad * True if the caller is in a pserialize read section. To be used
156 1.14 riastrad * only for diagnostic assertions where we want to guarantee the
157 1.14 riastrad * condition like:
158 1.9 ozaki *
159 1.14 riastrad * KASSERT(pserialize_in_read_section());
160 1.9 ozaki */
161 1.9 ozaki bool
162 1.9 ozaki pserialize_in_read_section(void)
163 1.9 ozaki {
164 1.14 riastrad
165 1.14 riastrad return kpreempt_disabled() && curcpu()->ci_psz_read_depth > 0;
166 1.9 ozaki }
167 1.9 ozaki
168 1.9 ozaki /*
169 1.9 ozaki * pserialize_not_in_read_section:
170 1.9 ozaki *
171 1.14 riastrad * True if the caller is not in a pserialize read section. To be
172 1.14 riastrad * used only for diagnostic assertions where we want to guarantee
173 1.14 riastrad * the condition like:
174 1.9 ozaki *
175 1.14 riastrad * KASSERT(pserialize_not_in_read_section());
176 1.9 ozaki */
177 1.9 ozaki bool
178 1.9 ozaki pserialize_not_in_read_section(void)
179 1.9 ozaki {
180 1.9 ozaki bool notin;
181 1.9 ozaki
182 1.14 riastrad kpreempt_disable();
183 1.14 riastrad notin = (curcpu()->ci_psz_read_depth == 0);
184 1.14 riastrad kpreempt_enable();
185 1.9 ozaki
186 1.9 ozaki return notin;
187 1.9 ozaki }
188