subr_pserialize.c revision 1.1 1 1.1 christos /* $NetBSD: subr_pserialize.c,v 1.1 2011/07/30 17:01:04 christos 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 * Implementation accurately matches the lapsed US patent 4809168, therefore
33 1.1 christos * code is patent-free in the United States. Your use of this code is at
34 1.1 christos * your own risk.
35 1.1 christos *
36 1.1 christos * Note for NetBSD developers: all changes to this source file must be
37 1.1 christos * approved by the <core>.
38 1.1 christos */
39 1.1 christos
40 1.1 christos #include <sys/cdefs.h>
41 1.1 christos __KERNEL_RCSID(0, "$NetBSD: subr_pserialize.c,v 1.1 2011/07/30 17:01:04 christos Exp $");
42 1.1 christos
43 1.1 christos #include <sys/param.h>
44 1.1 christos
45 1.1 christos #include <sys/condvar.h>
46 1.1 christos #include <sys/cpu.h>
47 1.1 christos #include <sys/kmem.h>
48 1.1 christos #include <sys/mutex.h>
49 1.1 christos #include <sys/pserialize.h>
50 1.1 christos #include <sys/queue.h>
51 1.1 christos #include <sys/xcall.h>
52 1.1 christos
53 1.1 christos struct pserialize {
54 1.1 christos TAILQ_ENTRY(pserialize) psz_chain;
55 1.1 christos lwp_t * psz_owner;
56 1.1 christos kcondvar_t psz_notifier;
57 1.1 christos kcpuset_t * psz_target;
58 1.1 christos kcpuset_t * psz_pass;
59 1.1 christos };
60 1.1 christos
61 1.1 christos static u_int psz_work_todo __cacheline_aligned;
62 1.1 christos static kmutex_t psz_lock __cacheline_aligned;
63 1.1 christos static struct evcnt psz_ev_excl __cacheline_aligned;
64 1.1 christos
65 1.1 christos /*
66 1.1 christos * As defined in "Method 1":
67 1.1 christos * q0: "0 MP checkpoints have occured".
68 1.1 christos * q1: "1 MP checkpoint has occured".
69 1.1 christos * q2: "2 MP checkpoints have occured".
70 1.1 christos */
71 1.1 christos static TAILQ_HEAD(, pserialize) psz_queue0 __cacheline_aligned;
72 1.1 christos static TAILQ_HEAD(, pserialize) psz_queue1 __cacheline_aligned;
73 1.1 christos static TAILQ_HEAD(, pserialize) psz_queue2 __cacheline_aligned;
74 1.1 christos
75 1.1 christos /*
76 1.1 christos * pserialize_init:
77 1.1 christos *
78 1.1 christos * Initialize passive serialization structures.
79 1.1 christos */
80 1.1 christos void
81 1.1 christos pserialize_init(void)
82 1.1 christos {
83 1.1 christos
84 1.1 christos psz_work_todo = 0;
85 1.1 christos TAILQ_INIT(&psz_queue0);
86 1.1 christos TAILQ_INIT(&psz_queue1);
87 1.1 christos TAILQ_INIT(&psz_queue2);
88 1.1 christos mutex_init(&psz_lock, MUTEX_DEFAULT, IPL_SCHED);
89 1.1 christos evcnt_attach_dynamic(&psz_ev_excl, EVCNT_TYPE_MISC, NULL,
90 1.1 christos "pserialize", "exclusive access");
91 1.1 christos }
92 1.1 christos
93 1.1 christos /*
94 1.1 christos * pserialize_create:
95 1.1 christos *
96 1.1 christos * Create and initialize a passive serialization object.
97 1.1 christos */
98 1.1 christos pserialize_t
99 1.1 christos pserialize_create(void)
100 1.1 christos {
101 1.1 christos pserialize_t psz;
102 1.1 christos
103 1.1 christos psz = kmem_zalloc(sizeof(struct pserialize), KM_SLEEP);
104 1.1 christos cv_init(&psz->psz_notifier, "psrlz");
105 1.1 christos psz->psz_target = kcpuset_create();
106 1.1 christos psz->psz_pass = kcpuset_create();
107 1.1 christos psz->psz_owner = NULL;
108 1.1 christos
109 1.1 christos return psz;
110 1.1 christos }
111 1.1 christos
112 1.1 christos /*
113 1.1 christos * pserialize_destroy:
114 1.1 christos *
115 1.1 christos * Destroy a passive serialization object.
116 1.1 christos */
117 1.1 christos void
118 1.1 christos pserialize_destroy(pserialize_t psz)
119 1.1 christos {
120 1.1 christos
121 1.1 christos KASSERT(psz->psz_owner == NULL);
122 1.1 christos
123 1.1 christos cv_destroy(&psz->psz_notifier);
124 1.1 christos kcpuset_destroy(psz->psz_target);
125 1.1 christos kcpuset_destroy(psz->psz_pass);
126 1.1 christos kmem_free(psz, sizeof(struct pserialize));
127 1.1 christos }
128 1.1 christos
129 1.1 christos /*
130 1.1 christos * pserialize_perform:
131 1.1 christos *
132 1.1 christos * Perform the write side of passive serialization. The calling
133 1.1 christos * thread holds an exclusive lock on the data object(s) being updated.
134 1.1 christos * We wait until every processor in the system has made at least two
135 1.1 christos * passes through cpu_swichto(). The wait is made with the caller's
136 1.1 christos * update lock held, but is short term.
137 1.1 christos */
138 1.1 christos void
139 1.1 christos pserialize_perform(pserialize_t psz)
140 1.1 christos {
141 1.1 christos
142 1.1 christos KASSERT(!cpu_intr_p());
143 1.1 christos KASSERT(!cpu_softintr_p());
144 1.1 christos
145 1.1 christos if (__predict_false(panicstr != NULL)) {
146 1.1 christos return;
147 1.1 christos }
148 1.1 christos KASSERT(psz->psz_owner == NULL);
149 1.1 christos KASSERT(kcpuset_iszero(psz->psz_target));
150 1.1 christos KASSERT(ncpu > 0);
151 1.1 christos
152 1.1 christos /*
153 1.1 christos * Set up the object and put it onto the queue. The lock
154 1.1 christos * activity here provides the necessary memory barrier to
155 1.1 christos * make the caller's data update completely visible to
156 1.1 christos * other processors.
157 1.1 christos */
158 1.1 christos psz->psz_owner = curlwp;
159 1.1 christos kcpuset_fill(psz->psz_target);
160 1.1 christos kcpuset_zero(psz->psz_pass);
161 1.1 christos
162 1.1 christos mutex_spin_enter(&psz_lock);
163 1.1 christos TAILQ_INSERT_TAIL(&psz_queue0, psz, psz_chain);
164 1.1 christos psz_work_todo++;
165 1.1 christos mutex_spin_exit(&psz_lock);
166 1.1 christos
167 1.1 christos /*
168 1.1 christos * Force some context switch activity on every CPU, as the system
169 1.1 christos * may not be busy. Note: should pass the point twice.
170 1.1 christos */
171 1.1 christos xc_broadcast(XC_HIGHPRI, (xcfunc_t)nullop, NULL, NULL);
172 1.1 christos xc_broadcast(XC_HIGHPRI, (xcfunc_t)nullop, NULL, NULL);
173 1.1 christos
174 1.1 christos /*
175 1.1 christos * Wait for all CPUs to cycle through mi_switch() twice.
176 1.1 christos * The last one through will remove our update from the
177 1.1 christos * queue and awaken us.
178 1.1 christos */
179 1.1 christos mutex_spin_enter(&psz_lock);
180 1.1 christos while (!kcpuset_iszero(psz->psz_target)) {
181 1.1 christos cv_wait(&psz->psz_notifier, &psz_lock);
182 1.1 christos }
183 1.1 christos psz_ev_excl.ev_count++;
184 1.1 christos mutex_spin_exit(&psz_lock);
185 1.1 christos
186 1.1 christos psz->psz_owner = NULL;
187 1.1 christos }
188 1.1 christos
189 1.1 christos int
190 1.1 christos pserialize_read_enter(void)
191 1.1 christos {
192 1.1 christos
193 1.1 christos KASSERT(!cpu_intr_p());
194 1.1 christos return splsoftclock();
195 1.1 christos }
196 1.1 christos
197 1.1 christos void
198 1.1 christos pserialize_read_exit(int s)
199 1.1 christos {
200 1.1 christos
201 1.1 christos splx(s);
202 1.1 christos }
203 1.1 christos
204 1.1 christos /*
205 1.1 christos * pserialize_switchpoint:
206 1.1 christos *
207 1.1 christos * Monitor system context switch activity. Called from machine
208 1.1 christos * independent code after mi_switch() returns.
209 1.1 christos */
210 1.1 christos void
211 1.1 christos pserialize_switchpoint(void)
212 1.1 christos {
213 1.1 christos pserialize_t psz, next;
214 1.1 christos cpuid_t cid;
215 1.1 christos
216 1.1 christos /*
217 1.1 christos * If no updates pending, bail out. No need to lock in order to
218 1.1 christos * test psz_work_todo; the only ill effect of missing an update
219 1.1 christos * would be to delay LWPs waiting in pserialize_perform(). That
220 1.1 christos * will not happen because updates are on the queue before an
221 1.1 christos * xcall is generated (serialization) to tickle every CPU.
222 1.1 christos */
223 1.1 christos if (__predict_true(psz_work_todo == 0)) {
224 1.1 christos return;
225 1.1 christos }
226 1.1 christos mutex_spin_enter(&psz_lock);
227 1.1 christos cid = cpu_index(curcpu());
228 1.1 christos
229 1.1 christos /*
230 1.1 christos * At first, scan through the second queue and update each request,
231 1.1 christos * if passed all processors, then transfer to the third queue.
232 1.1 christos */
233 1.1 christos for (psz = TAILQ_FIRST(&psz_queue1); psz != NULL; psz = next) {
234 1.1 christos next = TAILQ_NEXT(psz, psz_chain);
235 1.1 christos if (!kcpuset_match(psz->psz_pass, psz->psz_target)) {
236 1.1 christos kcpuset_set(cid, psz->psz_pass);
237 1.1 christos continue;
238 1.1 christos }
239 1.1 christos kcpuset_zero(psz->psz_pass);
240 1.1 christos TAILQ_REMOVE(&psz_queue1, psz, psz_chain);
241 1.1 christos TAILQ_INSERT_TAIL(&psz_queue2, psz, psz_chain);
242 1.1 christos }
243 1.1 christos /*
244 1.1 christos * Scan through the first queue and update each request,
245 1.1 christos * if passed all processors, then move to the second queue.
246 1.1 christos */
247 1.1 christos for (psz = TAILQ_FIRST(&psz_queue0); psz != NULL; psz = next) {
248 1.1 christos next = TAILQ_NEXT(psz, psz_chain);
249 1.1 christos if (!kcpuset_match(psz->psz_pass, psz->psz_target)) {
250 1.1 christos kcpuset_set(cid, psz->psz_pass);
251 1.1 christos continue;
252 1.1 christos }
253 1.1 christos kcpuset_zero(psz->psz_pass);
254 1.1 christos TAILQ_REMOVE(&psz_queue0, psz, psz_chain);
255 1.1 christos TAILQ_INSERT_TAIL(&psz_queue1, psz, psz_chain);
256 1.1 christos }
257 1.1 christos /*
258 1.1 christos * Process the third queue: entries have been seen twice on every
259 1.1 christos * processor, remove from the queue and notify the updating thread.
260 1.1 christos */
261 1.1 christos while ((psz = TAILQ_FIRST(&psz_queue2)) != NULL) {
262 1.1 christos TAILQ_REMOVE(&psz_queue2, psz, psz_chain);
263 1.1 christos kcpuset_zero(psz->psz_target);
264 1.1 christos cv_signal(&psz->psz_notifier);
265 1.1 christos psz_work_todo--;
266 1.1 christos }
267 1.1 christos mutex_spin_exit(&psz_lock);
268 1.1 christos }
269