subr_xcall.c revision 1.13 1 /* $NetBSD: subr_xcall.c,v 1.13 2011/05/13 22:16:44 rmind Exp $ */
2
3 /*-
4 * Copyright (c) 2007-2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran and Mindaugas Rasiukevicius.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Cross call support
34 *
35 * Background
36 *
37 * Sometimes it is necessary to modify hardware state that is tied
38 * directly to individual CPUs (such as a CPU's local timer), and
39 * these updates can not be done remotely by another CPU. The LWP
40 * requesting the update may be unable to guarantee that it will be
41 * running on the CPU where the update must occur, when the update
42 * occurs.
43 *
44 * Additionally, it's sometimes necessary to modify per-CPU software
45 * state from a remote CPU. Where these update operations are so
46 * rare or the access to the per-CPU data so frequent that the cost
47 * of using locking or atomic operations to provide coherency is
48 * prohibitive, another way must be found.
49 *
50 * Cross calls help to solve these types of problem by allowing
51 * any CPU in the system to request that an arbitrary function be
52 * executed on any other CPU.
53 *
54 * Implementation
55 *
56 * A slow mechanism for making 'low priority' cross calls is
57 * provided. The function to be executed runs on the remote CPU
58 * within a bound kthread. No queueing is provided, and the
59 * implementation uses global state. The function being called may
60 * block briefly on locks, but in doing so must be careful to not
61 * interfere with other cross calls in the system. The function is
62 * called with thread context and not from a soft interrupt, so it
63 * can ensure that it is not interrupting other code running on the
64 * CPU, and so has exclusive access to the CPU. Since this facility
65 * is heavyweight, it's expected that it will not be used often.
66 *
67 * Cross calls must not allocate memory, as the pagedaemon uses
68 * them (and memory allocation may need to wait on the pagedaemon).
69 *
70 * A low-overhead mechanism for high priority calls (XC_HIGHPRI) is
71 * also provided. The function to be executed runs on a software
72 * interrupt context, at SOFTINT_CLOCK level, and is expected to be
73 * very lightweight, e.g. avoid blocking.
74 */
75
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: subr_xcall.c,v 1.13 2011/05/13 22:16:44 rmind Exp $");
78
79 #include <sys/types.h>
80 #include <sys/param.h>
81 #include <sys/xcall.h>
82 #include <sys/mutex.h>
83 #include <sys/condvar.h>
84 #include <sys/evcnt.h>
85 #include <sys/kthread.h>
86 #include <sys/cpu.h>
87
88 /* Cross-call state box. */
89 typedef struct {
90 kmutex_t xc_lock;
91 kcondvar_t xc_busy;
92 xcfunc_t xc_func;
93 void * xc_arg1;
94 void * xc_arg2;
95 uint64_t xc_headp;
96 uint64_t xc_donep;
97 } xc_state_t;
98
99 /* Bit indicating high (1) or low (0) priority. */
100 #define XC_PRI_BIT (1ULL << 63)
101
102 /* Low priority xcall structures. */
103 static xc_state_t xc_low_pri __cacheline_aligned;
104 static uint64_t xc_tailp __cacheline_aligned;
105
106 /* High priority xcall structures. */
107 static xc_state_t xc_high_pri __cacheline_aligned;
108 static void * xc_sih __cacheline_aligned;
109
110 /* Event counters. */
111 static struct evcnt xc_unicast_ev __cacheline_aligned;
112 static struct evcnt xc_broadcast_ev __cacheline_aligned;
113
114 static void xc_init(void);
115 static void xc_thread(void *);
116 static void xc_highpri_intr(void *);
117
118 static inline uint64_t xc_highpri(xcfunc_t, void *, void *, struct cpu_info *);
119 static inline uint64_t xc_lowpri(xcfunc_t, void *, void *, struct cpu_info *);
120
121 /*
122 * xc_init:
123 *
124 * Initialize low and high priority cross-call structures.
125 */
126 static void
127 xc_init(void)
128 {
129 xc_state_t *xclo = &xc_low_pri, *xchi = &xc_high_pri;
130
131 memset(xclo, 0, sizeof(xc_state_t));
132 mutex_init(&xclo->xc_lock, MUTEX_DEFAULT, IPL_NONE);
133 cv_init(&xclo->xc_busy, "xclocv");
134 xc_tailp = 0;
135
136 memset(xchi, 0, sizeof(xc_state_t));
137 mutex_init(&xchi->xc_lock, MUTEX_DEFAULT, IPL_SOFTCLOCK);
138 cv_init(&xchi->xc_busy, "xchicv");
139 xc_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
140 xc_highpri_intr, NULL);
141 KASSERT(xc_sih != NULL);
142
143 evcnt_attach_dynamic(&xc_unicast_ev, EVCNT_TYPE_MISC, NULL,
144 "crosscall", "unicast");
145 evcnt_attach_dynamic(&xc_broadcast_ev, EVCNT_TYPE_MISC, NULL,
146 "crosscall", "broadcast");
147 }
148
149 /*
150 * xc_init_cpu:
151 *
152 * Initialize the cross-call subsystem. Called once for each CPU
153 * in the system as they are attached.
154 */
155 void
156 xc_init_cpu(struct cpu_info *ci)
157 {
158 static bool again = false;
159 int error;
160
161 if (!again) {
162 /* Autoconfiguration will prevent re-entry. */
163 xc_init();
164 again = true;
165 }
166 cv_init(&ci->ci_data.cpu_xcall, "xcall");
167 error = kthread_create(PRI_XCALL, KTHREAD_MPSAFE, ci, xc_thread,
168 NULL, NULL, "xcall/%u", ci->ci_index);
169 KASSERT(error == 0);
170 }
171
172 /*
173 * xc_broadcast:
174 *
175 * Trigger a call on all CPUs in the system.
176 */
177 uint64_t
178 xc_broadcast(u_int flags, xcfunc_t func, void *arg1, void *arg2)
179 {
180
181 KASSERT(!cpu_intr_p() && !cpu_softintr_p());
182
183 if ((flags & XC_HIGHPRI) != 0) {
184 return xc_highpri(func, arg1, arg2, NULL);
185 } else {
186 return xc_lowpri(func, arg1, arg2, NULL);
187 }
188 }
189
190 /*
191 * xc_unicast:
192 *
193 * Trigger a call on one CPU.
194 */
195 uint64_t
196 xc_unicast(u_int flags, xcfunc_t func, void *arg1, void *arg2,
197 struct cpu_info *ci)
198 {
199
200 KASSERT(ci != NULL);
201 KASSERT(!cpu_intr_p() && !cpu_softintr_p());
202
203 if ((flags & XC_HIGHPRI) != 0) {
204 return xc_highpri(func, arg1, arg2, ci);
205 } else {
206 return xc_lowpri(func, arg1, arg2, ci);
207 }
208 }
209
210 /*
211 * xc_wait:
212 *
213 * Wait for a cross call to complete.
214 */
215 void
216 xc_wait(uint64_t where)
217 {
218 xc_state_t *xc;
219
220 KASSERT(!cpu_intr_p() && !cpu_softintr_p());
221
222 /* Determine whether it is high or low priority cross-call. */
223 if ((where & XC_PRI_BIT) != 0) {
224 xc = &xc_high_pri;
225 where &= ~XC_PRI_BIT;
226 } else {
227 xc = &xc_low_pri;
228 }
229
230 /* Fast path, if already done. */
231 if (xc->xc_donep >= where) {
232 return;
233 }
234
235 /* Slow path: block until awoken. */
236 mutex_enter(&xc->xc_lock);
237 while (xc->xc_donep < where) {
238 cv_wait(&xc->xc_busy, &xc->xc_lock);
239 }
240 mutex_exit(&xc->xc_lock);
241 }
242
243 /*
244 * xc_lowpri:
245 *
246 * Trigger a low priority call on one or more CPUs.
247 */
248 static inline uint64_t
249 xc_lowpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci)
250 {
251 xc_state_t *xc = &xc_low_pri;
252 CPU_INFO_ITERATOR cii;
253 uint64_t where;
254
255 mutex_enter(&xc->xc_lock);
256 while (xc->xc_headp != xc_tailp) {
257 cv_wait(&xc->xc_busy, &xc->xc_lock);
258 }
259 xc->xc_arg1 = arg1;
260 xc->xc_arg2 = arg2;
261 xc->xc_func = func;
262 if (ci == NULL) {
263 xc_broadcast_ev.ev_count++;
264 for (CPU_INFO_FOREACH(cii, ci)) {
265 if ((ci->ci_schedstate.spc_flags & SPCF_RUNNING) == 0)
266 continue;
267 xc->xc_headp += 1;
268 ci->ci_data.cpu_xcall_pending = true;
269 cv_signal(&ci->ci_data.cpu_xcall);
270 }
271 } else {
272 xc_unicast_ev.ev_count++;
273 xc->xc_headp += 1;
274 ci->ci_data.cpu_xcall_pending = true;
275 cv_signal(&ci->ci_data.cpu_xcall);
276 }
277 KASSERT(xc_tailp < xc->xc_headp);
278 where = xc->xc_headp;
279 mutex_exit(&xc->xc_lock);
280
281 /* Return a low priority ticket. */
282 KASSERT((where & XC_PRI_BIT) == 0);
283 return where;
284 }
285
286 /*
287 * xc_thread:
288 *
289 * One thread per-CPU to dispatch low priority calls.
290 */
291 static void
292 xc_thread(void *cookie)
293 {
294 struct cpu_info *ci = curcpu();
295 xc_state_t *xc = &xc_low_pri;
296 void *arg1, *arg2;
297 xcfunc_t func;
298
299 mutex_enter(&xc->xc_lock);
300 for (;;) {
301 while (!ci->ci_data.cpu_xcall_pending) {
302 if (xc->xc_headp == xc_tailp) {
303 cv_broadcast(&xc->xc_busy);
304 }
305 cv_wait(&ci->ci_data.cpu_xcall, &xc->xc_lock);
306 KASSERT(ci == curcpu());
307 }
308 ci->ci_data.cpu_xcall_pending = false;
309 func = xc->xc_func;
310 arg1 = xc->xc_arg1;
311 arg2 = xc->xc_arg2;
312 xc_tailp++;
313 mutex_exit(&xc->xc_lock);
314
315 KASSERT(func != NULL);
316 (*func)(arg1, arg2);
317
318 mutex_enter(&xc->xc_lock);
319 xc->xc_donep++;
320 }
321 /* NOTREACHED */
322 }
323
324 /*
325 * xc_ipi_handler:
326 *
327 * Handler of cross-call IPI.
328 */
329 void
330 xc_ipi_handler(void)
331 {
332 /* Executes xc_highpri_intr() via software interrupt. */
333 softint_schedule(xc_sih);
334 }
335
336 /*
337 * xc_highpri_intr:
338 *
339 * A software interrupt handler for high priority calls.
340 */
341 static void
342 xc_highpri_intr(void *dummy)
343 {
344 xc_state_t *xc = &xc_high_pri;
345 void *arg1, *arg2;
346 xcfunc_t func;
347
348 KASSERT(cpu_softintr_p());
349 /*
350 * Lock-less fetch of function and its arguments.
351 * Safe since it cannot change at this point.
352 */
353 KASSERT(xc->xc_donep < xc->xc_headp);
354 func = xc->xc_func;
355 arg1 = xc->xc_arg1;
356 arg2 = xc->xc_arg2;
357
358 KASSERT(func != NULL);
359 (*func)(arg1, arg2);
360
361 /*
362 * Note the request as done, and if we have reached the head,
363 * cross-call has been processed - notify waiters, if any.
364 */
365 mutex_enter(&xc->xc_lock);
366 if (++xc->xc_donep == xc->xc_headp) {
367 cv_broadcast(&xc->xc_busy);
368 }
369 mutex_exit(&xc->xc_lock);
370 }
371
372 /*
373 * xc_highpri:
374 *
375 * Trigger a high priority call on one or more CPUs.
376 */
377 static inline uint64_t
378 xc_highpri(xcfunc_t func, void *arg1, void *arg2, struct cpu_info *ci)
379 {
380 xc_state_t *xc = &xc_high_pri;
381 uint64_t where;
382
383 mutex_enter(&xc->xc_lock);
384 while (xc->xc_headp != xc->xc_donep) {
385 cv_wait(&xc->xc_busy, &xc->xc_lock);
386 }
387 xc->xc_func = func;
388 xc->xc_arg1 = arg1;
389 xc->xc_arg2 = arg2;
390 xc->xc_headp += (ci ? 1 : ncpu);
391 where = xc->xc_headp;
392 mutex_exit(&xc->xc_lock);
393
394 /*
395 * Send the IPI once lock is released.
396 * Note: it will handle the local CPU case.
397 */
398
399 #ifdef MULTIPROCESSOR
400 kpreempt_disable();
401 if (curcpu() == ci) {
402 /* Unicast: local CPU. */
403 xc_ipi_handler();
404 } else if (ci) {
405 /* Unicast: remote CPU. */
406 xc_send_ipi(ci);
407 } else {
408 /* Broadcast: all, including local. */
409 xc_send_ipi(NULL);
410 xc_ipi_handler();
411 }
412 kpreempt_enable();
413 #else
414 KASSERT(curcpu() == ci);
415 xc_ipi_handler();
416 #endif
417
418 /* Indicate a high priority ticket. */
419 return (where | XC_PRI_BIT);
420 }
421