FreeBSD/Linux Kernel Cross Reference
sys/kern/subr_smp.c
1 /*-
2 * Copyright (c) 2001, John Baldwin <jhb@FreeBSD.org>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * This module holds the global variables and machine independent functions
32 * used for the kernel SMP support.
33 */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/proc.h>
43 #include <sys/bus.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/pcpu.h>
47 #include <sys/smp.h>
48 #include <sys/sysctl.h>
49
50 #include <machine/cpu.h>
51 #include <machine/smp.h>
52
53 #include "opt_sched.h"
54
55 #ifdef SMP
56 volatile cpumask_t stopped_cpus;
57 volatile cpumask_t started_cpus;
58 cpumask_t idle_cpus_mask;
59 cpumask_t hlt_cpus_mask;
60 cpumask_t logical_cpus_mask;
61
62 void (*cpustop_restartfunc)(void);
63 #endif
64 /* This is used in modules that need to work in both SMP and UP. */
65 cpumask_t all_cpus;
66
67 int mp_ncpus;
68 /* export this for libkvm consumers. */
69 int mp_maxcpus = MAXCPU;
70
71 struct cpu_top *smp_topology;
72 volatile int smp_started;
73 u_int mp_maxid;
74
75 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD, NULL, "Kernel SMP");
76
77 SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD, &mp_maxid, 0,
78 "Max CPU ID.");
79
80 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD, &mp_maxcpus, 0,
81 "Max number of CPUs that the system was compiled for.");
82
83 int smp_active = 0; /* are the APs allowed to run? */
84 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0,
85 "Number of Auxillary Processors (APs) that were successfully started");
86
87 int smp_disabled = 0; /* has smp been disabled? */
88 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN, &smp_disabled, 0,
89 "SMP has been disabled from the loader");
90 TUNABLE_INT("kern.smp.disabled", &smp_disabled);
91
92 int smp_cpus = 1; /* how many cpu's running */
93 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD, &smp_cpus, 0,
94 "Number of CPUs online");
95
96 #ifdef SMP
97 /* Enable forwarding of a signal to a process running on a different CPU */
98 static int forward_signal_enabled = 1;
99 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
100 &forward_signal_enabled, 0,
101 "Forwarding of a signal to a process on a different CPU");
102
103 /* Enable forwarding of roundrobin to all other cpus */
104 static int forward_roundrobin_enabled = 1;
105 SYSCTL_INT(_kern_smp, OID_AUTO, forward_roundrobin_enabled, CTLFLAG_RW,
106 &forward_roundrobin_enabled, 0,
107 "Forwarding of roundrobin to all other CPUs");
108
109 /* Variables needed for SMP rendezvous. */
110 static volatile int smp_rv_ncpus;
111 static void (*volatile smp_rv_setup_func)(void *arg);
112 static void (*volatile smp_rv_action_func)(void *arg);
113 static void (*volatile smp_rv_teardown_func)(void *arg);
114 static void * volatile smp_rv_func_arg;
115 static volatile int smp_rv_waiters[3];
116
117 /*
118 * Shared mutex to restrict busywaits between smp_rendezvous() and
119 * smp(_targeted)_tlb_shootdown(). A deadlock occurs if both of these
120 * functions trigger at once and cause multiple CPUs to busywait with
121 * interrupts disabled.
122 */
123 struct mtx smp_ipi_mtx;
124
125 /*
126 * Let the MD SMP code initialize mp_maxid very early if it can.
127 */
128 static void
129 mp_setmaxid(void *dummy)
130 {
131 cpu_mp_setmaxid();
132 }
133 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL);
134
135 /*
136 * Call the MD SMP initialization code.
137 */
138 static void
139 mp_start(void *dummy)
140 {
141
142 /* Probe for MP hardware. */
143 if (smp_disabled != 0 || cpu_mp_probe() == 0) {
144 mp_ncpus = 1;
145 all_cpus = PCPU_GET(cpumask);
146 return;
147 }
148
149 mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
150 cpu_mp_start();
151 printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
152 mp_ncpus);
153 cpu_mp_announce();
154 }
155 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL);
156
157 void
158 forward_signal(struct thread *td)
159 {
160 int id;
161
162 /*
163 * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on
164 * this thread, so all we need to do is poke it if it is currently
165 * executing so that it executes ast().
166 */
167 THREAD_LOCK_ASSERT(td, MA_OWNED);
168 KASSERT(TD_IS_RUNNING(td),
169 ("forward_signal: thread is not TDS_RUNNING"));
170
171 CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
172
173 if (!smp_started || cold || panicstr)
174 return;
175 if (!forward_signal_enabled)
176 return;
177
178 /* No need to IPI ourself. */
179 if (td == curthread)
180 return;
181
182 id = td->td_oncpu;
183 if (id == NOCPU)
184 return;
185 ipi_selected(1 << id, IPI_AST);
186 }
187
188 void
189 forward_roundrobin(void)
190 {
191 struct pcpu *pc;
192 struct thread *td;
193 cpumask_t id, map, me;
194
195 CTR0(KTR_SMP, "forward_roundrobin()");
196
197 if (!smp_started || cold || panicstr)
198 return;
199 if (!forward_roundrobin_enabled)
200 return;
201 map = 0;
202 me = PCPU_GET(cpumask);
203 SLIST_FOREACH(pc, &cpuhead, pc_allcpu) {
204 td = pc->pc_curthread;
205 id = pc->pc_cpumask;
206 if (id != me && (id & stopped_cpus) == 0 &&
207 !TD_IS_IDLETHREAD(td)) {
208 td->td_flags |= TDF_NEEDRESCHED;
209 map |= id;
210 }
211 }
212 ipi_selected(map, IPI_AST);
213 }
214
215 /*
216 * When called the executing CPU will send an IPI to all other CPUs
217 * requesting that they halt execution.
218 *
219 * Usually (but not necessarily) called with 'other_cpus' as its arg.
220 *
221 * - Signals all CPUs in map to stop.
222 * - Waits for each to stop.
223 *
224 * Returns:
225 * -1: error
226 * 0: NA
227 * 1: ok
228 *
229 * XXX FIXME: this is not MP-safe, needs a lock to prevent multiple CPUs
230 * from executing at same time.
231 */
232 int
233 stop_cpus(cpumask_t map)
234 {
235 int i;
236
237 if (!smp_started)
238 return 0;
239
240 CTR1(KTR_SMP, "stop_cpus(%x)", map);
241
242 /* send the stop IPI to all CPUs in map */
243 ipi_selected(map, IPI_STOP);
244
245 i = 0;
246 while ((stopped_cpus & map) != map) {
247 /* spin */
248 cpu_spinwait();
249 i++;
250 #ifdef DIAGNOSTIC
251 if (i == 100000) {
252 printf("timeout stopping cpus\n");
253 break;
254 }
255 #endif
256 }
257
258 return 1;
259 }
260
261 /*
262 * Called by a CPU to restart stopped CPUs.
263 *
264 * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
265 *
266 * - Signals all CPUs in map to restart.
267 * - Waits for each to restart.
268 *
269 * Returns:
270 * -1: error
271 * 0: NA
272 * 1: ok
273 */
274 int
275 restart_cpus(cpumask_t map)
276 {
277
278 if (!smp_started)
279 return 0;
280
281 CTR1(KTR_SMP, "restart_cpus(%x)", map);
282
283 /* signal other cpus to restart */
284 atomic_store_rel_int(&started_cpus, map);
285
286 /* wait for each to clear its bit */
287 while ((stopped_cpus & map) != 0)
288 cpu_spinwait();
289
290 return 1;
291 }
292
293 /*
294 * All-CPU rendezvous. CPUs are signalled, all execute the setup function
295 * (if specified), rendezvous, execute the action function (if specified),
296 * rendezvous again, execute the teardown function (if specified), and then
297 * resume.
298 *
299 * Note that the supplied external functions _must_ be reentrant and aware
300 * that they are running in parallel and in an unknown lock context.
301 */
302 void
303 smp_rendezvous_action(void)
304 {
305 void* local_func_arg = smp_rv_func_arg;
306 void (*local_setup_func)(void*) = smp_rv_setup_func;
307 void (*local_action_func)(void*) = smp_rv_action_func;
308 void (*local_teardown_func)(void*) = smp_rv_teardown_func;
309
310 /* Ensure we have up-to-date values. */
311 atomic_add_acq_int(&smp_rv_waiters[0], 1);
312 while (smp_rv_waiters[0] < smp_rv_ncpus)
313 cpu_spinwait();
314
315 /* setup function */
316 if (local_setup_func != smp_no_rendevous_barrier) {
317 if (smp_rv_setup_func != NULL)
318 smp_rv_setup_func(smp_rv_func_arg);
319
320 /* spin on entry rendezvous */
321 atomic_add_int(&smp_rv_waiters[1], 1);
322 while (smp_rv_waiters[1] < smp_rv_ncpus)
323 cpu_spinwait();
324 }
325
326 /* action function */
327 if (local_action_func != NULL)
328 local_action_func(local_func_arg);
329
330 /* spin on exit rendezvous */
331 atomic_add_int(&smp_rv_waiters[2], 1);
332 if (local_teardown_func == smp_no_rendevous_barrier)
333 return;
334 while (smp_rv_waiters[2] < smp_rv_ncpus)
335 cpu_spinwait();
336
337 /* teardown function */
338 if (local_teardown_func != NULL)
339 local_teardown_func(local_func_arg);
340 }
341
342 void
343 smp_rendezvous_cpus(cpumask_t map,
344 void (* setup_func)(void *),
345 void (* action_func)(void *),
346 void (* teardown_func)(void *),
347 void *arg)
348 {
349 int i, ncpus = 0;
350
351 if (!smp_started) {
352 if (setup_func != NULL)
353 setup_func(arg);
354 if (action_func != NULL)
355 action_func(arg);
356 if (teardown_func != NULL)
357 teardown_func(arg);
358 return;
359 }
360
361 for (i = 0; i <= mp_maxid; i++)
362 if (((1 << i) & map) != 0 && !CPU_ABSENT(i))
363 ncpus++;
364 if (ncpus == 0)
365 panic("ncpus is 0 with map=0x%x", map);
366
367 /* obtain rendezvous lock */
368 mtx_lock_spin(&smp_ipi_mtx);
369
370 /* set static function pointers */
371 smp_rv_ncpus = ncpus;
372 smp_rv_setup_func = setup_func;
373 smp_rv_action_func = action_func;
374 smp_rv_teardown_func = teardown_func;
375 smp_rv_func_arg = arg;
376 smp_rv_waiters[1] = 0;
377 smp_rv_waiters[2] = 0;
378 atomic_store_rel_int(&smp_rv_waiters[0], 0);
379
380 /* signal other processors, which will enter the IPI with interrupts off */
381 ipi_selected(map & ~(1 << curcpu), IPI_RENDEZVOUS);
382
383 /* Check if the current CPU is in the map */
384 if ((map & (1 << curcpu)) != 0)
385 smp_rendezvous_action();
386
387 if (teardown_func == smp_no_rendevous_barrier)
388 while (atomic_load_acq_int(&smp_rv_waiters[2]) < ncpus)
389 cpu_spinwait();
390
391 /* release lock */
392 mtx_unlock_spin(&smp_ipi_mtx);
393 }
394
395 void
396 smp_rendezvous(void (* setup_func)(void *),
397 void (* action_func)(void *),
398 void (* teardown_func)(void *),
399 void *arg)
400 {
401 smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg);
402 }
403 #else /* !SMP */
404
405 /*
406 * Provide dummy SMP support for UP kernels. Modules that need to use SMP
407 * APIs will still work using this dummy support.
408 */
409 static void
410 mp_setvariables_for_up(void *dummy)
411 {
412 mp_ncpus = 1;
413 mp_maxid = PCPU_GET(cpuid);
414 all_cpus = PCPU_GET(cpumask);
415 KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
416 }
417 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
418 mp_setvariables_for_up, NULL);
419
420 void
421 smp_rendezvous_cpus(cpumask_t map,
422 void (*setup_func)(void *),
423 void (*action_func)(void *),
424 void (*teardown_func)(void *),
425 void *arg)
426 {
427 if (setup_func != NULL)
428 setup_func(arg);
429 if (action_func != NULL)
430 action_func(arg);
431 if (teardown_func != NULL)
432 teardown_func(arg);
433 }
434
435 void
436 smp_rendezvous(void (*setup_func)(void *),
437 void (*action_func)(void *),
438 void (*teardown_func)(void *),
439 void *arg)
440 {
441
442 if (setup_func != NULL)
443 setup_func(arg);
444 if (action_func != NULL)
445 action_func(arg);
446 if (teardown_func != NULL)
447 teardown_func(arg);
448 }
449 #endif /* SMP */
450
451 void
452 smp_no_rendevous_barrier(void *dummy)
453 {
454 #ifdef SMP
455 KASSERT((!smp_started),("smp_no_rendevous called and smp is started"));
456 #endif
457 }
Cache object: e52f3dd79038af7ed3995e8eae01dc62
|