xref: /freebsd/sys/kern/subr_smp.c (revision a02aba5f3c73d7ed377f88327fedd11f70f23353)
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 cpuset_t stopped_cpus;
57 volatile cpuset_t started_cpus;
58 cpuset_t hlt_cpus_mask;
59 cpuset_t logical_cpus_mask;
60 
61 void (*cpustop_restartfunc)(void);
62 #endif
63 /* This is used in modules that need to work in both SMP and UP. */
64 cpuset_t all_cpus;
65 
66 int mp_ncpus;
67 /* export this for libkvm consumers. */
68 int mp_maxcpus = MAXCPU;
69 
70 volatile int smp_started;
71 u_int mp_maxid;
72 
73 SYSCTL_NODE(_kern, OID_AUTO, smp, CTLFLAG_RD|CTLFLAG_CAPRD, NULL, "Kernel SMP");
74 
75 SYSCTL_INT(_kern_smp, OID_AUTO, maxid, CTLFLAG_RD|CTLFLAG_CAPRD, &mp_maxid, 0,
76     "Max CPU ID.");
77 
78 SYSCTL_INT(_kern_smp, OID_AUTO, maxcpus, CTLFLAG_RD|CTLFLAG_CAPRD, &mp_maxcpus,
79     0, "Max number of CPUs that the system was compiled for.");
80 
81 int smp_active = 0;	/* are the APs allowed to run? */
82 SYSCTL_INT(_kern_smp, OID_AUTO, active, CTLFLAG_RW, &smp_active, 0,
83     "Number of Auxillary Processors (APs) that were successfully started");
84 
85 int smp_disabled = 0;	/* has smp been disabled? */
86 SYSCTL_INT(_kern_smp, OID_AUTO, disabled, CTLFLAG_RDTUN|CTLFLAG_CAPRD,
87     &smp_disabled, 0, "SMP has been disabled from the loader");
88 TUNABLE_INT("kern.smp.disabled", &smp_disabled);
89 
90 int smp_cpus = 1;	/* how many cpu's running */
91 SYSCTL_INT(_kern_smp, OID_AUTO, cpus, CTLFLAG_RD|CTLFLAG_CAPRD, &smp_cpus, 0,
92     "Number of CPUs online");
93 
94 int smp_topology = 0;	/* Which topology we're using. */
95 SYSCTL_INT(_kern_smp, OID_AUTO, topology, CTLFLAG_RD, &smp_topology, 0,
96     "Topology override setting; 0 is default provided by hardware.");
97 TUNABLE_INT("kern.smp.topology", &smp_topology);
98 
99 #ifdef SMP
100 /* Enable forwarding of a signal to a process running on a different CPU */
101 static int forward_signal_enabled = 1;
102 SYSCTL_INT(_kern_smp, OID_AUTO, forward_signal_enabled, CTLFLAG_RW,
103 	   &forward_signal_enabled, 0,
104 	   "Forwarding of a signal to a process on a different CPU");
105 
106 /* Variables needed for SMP rendezvous. */
107 static volatile int smp_rv_ncpus;
108 static void (*volatile smp_rv_setup_func)(void *arg);
109 static void (*volatile smp_rv_action_func)(void *arg);
110 static void (*volatile smp_rv_teardown_func)(void *arg);
111 static void *volatile smp_rv_func_arg;
112 static volatile int smp_rv_waiters[4];
113 
114 /*
115  * Shared mutex to restrict busywaits between smp_rendezvous() and
116  * smp(_targeted)_tlb_shootdown().  A deadlock occurs if both of these
117  * functions trigger at once and cause multiple CPUs to busywait with
118  * interrupts disabled.
119  */
120 struct mtx smp_ipi_mtx;
121 
122 /*
123  * Let the MD SMP code initialize mp_maxid very early if it can.
124  */
125 static void
126 mp_setmaxid(void *dummy)
127 {
128 	cpu_mp_setmaxid();
129 }
130 SYSINIT(cpu_mp_setmaxid, SI_SUB_TUNABLES, SI_ORDER_FIRST, mp_setmaxid, NULL);
131 
132 /*
133  * Call the MD SMP initialization code.
134  */
135 static void
136 mp_start(void *dummy)
137 {
138 
139 	mtx_init(&smp_ipi_mtx, "smp rendezvous", NULL, MTX_SPIN);
140 
141 	/* Probe for MP hardware. */
142 	if (smp_disabled != 0 || cpu_mp_probe() == 0) {
143 		mp_ncpus = 1;
144 		CPU_SETOF(PCPU_GET(cpuid), &all_cpus);
145 		return;
146 	}
147 
148 	cpu_mp_start();
149 	printf("FreeBSD/SMP: Multiprocessor System Detected: %d CPUs\n",
150 	    mp_ncpus);
151 	cpu_mp_announce();
152 }
153 SYSINIT(cpu_mp, SI_SUB_CPU, SI_ORDER_THIRD, mp_start, NULL);
154 
155 void
156 forward_signal(struct thread *td)
157 {
158 	int id;
159 
160 	/*
161 	 * signotify() has already set TDF_ASTPENDING and TDF_NEEDSIGCHECK on
162 	 * this thread, so all we need to do is poke it if it is currently
163 	 * executing so that it executes ast().
164 	 */
165 	THREAD_LOCK_ASSERT(td, MA_OWNED);
166 	KASSERT(TD_IS_RUNNING(td),
167 	    ("forward_signal: thread is not TDS_RUNNING"));
168 
169 	CTR1(KTR_SMP, "forward_signal(%p)", td->td_proc);
170 
171 	if (!smp_started || cold || panicstr)
172 		return;
173 	if (!forward_signal_enabled)
174 		return;
175 
176 	/* No need to IPI ourself. */
177 	if (td == curthread)
178 		return;
179 
180 	id = td->td_oncpu;
181 	if (id == NOCPU)
182 		return;
183 	ipi_cpu(id, IPI_AST);
184 }
185 
186 /*
187  * When called the executing CPU will send an IPI to all other CPUs
188  *  requesting that they halt execution.
189  *
190  * Usually (but not necessarily) called with 'other_cpus' as its arg.
191  *
192  *  - Signals all CPUs in map to stop.
193  *  - Waits for each to stop.
194  *
195  * Returns:
196  *  -1: error
197  *   0: NA
198  *   1: ok
199  *
200  */
201 static int
202 generic_stop_cpus(cpuset_t map, u_int type)
203 {
204 #ifdef KTR
205 	char cpusetbuf[CPUSETBUFSIZ];
206 #endif
207 	static volatile u_int stopping_cpu = NOCPU;
208 	int i;
209 
210 	KASSERT(
211 #if defined(__amd64__)
212 	    type == IPI_STOP || type == IPI_STOP_HARD || type == IPI_SUSPEND,
213 #else
214 	    type == IPI_STOP || type == IPI_STOP_HARD,
215 #endif
216 	    ("%s: invalid stop type", __func__));
217 
218 	if (!smp_started)
219 		return (0);
220 
221 	CTR2(KTR_SMP, "stop_cpus(%s) with %u type",
222 	    cpusetobj_strprint(cpusetbuf, &map), type);
223 
224 	if (stopping_cpu != PCPU_GET(cpuid))
225 		while (atomic_cmpset_int(&stopping_cpu, NOCPU,
226 		    PCPU_GET(cpuid)) == 0)
227 			while (stopping_cpu != NOCPU)
228 				cpu_spinwait(); /* spin */
229 
230 	/* send the stop IPI to all CPUs in map */
231 	ipi_selected(map, type);
232 
233 	i = 0;
234 	while (!CPU_SUBSET(&stopped_cpus, &map)) {
235 		/* spin */
236 		cpu_spinwait();
237 		i++;
238 		if (i == 100000000) {
239 			printf("timeout stopping cpus\n");
240 			break;
241 		}
242 	}
243 
244 	stopping_cpu = NOCPU;
245 	return (1);
246 }
247 
248 int
249 stop_cpus(cpuset_t map)
250 {
251 
252 	return (generic_stop_cpus(map, IPI_STOP));
253 }
254 
255 int
256 stop_cpus_hard(cpuset_t map)
257 {
258 
259 	return (generic_stop_cpus(map, IPI_STOP_HARD));
260 }
261 
262 #if defined(__amd64__)
263 int
264 suspend_cpus(cpuset_t map)
265 {
266 
267 	return (generic_stop_cpus(map, IPI_SUSPEND));
268 }
269 #endif
270 
271 /*
272  * Called by a CPU to restart stopped CPUs.
273  *
274  * Usually (but not necessarily) called with 'stopped_cpus' as its arg.
275  *
276  *  - Signals all CPUs in map to restart.
277  *  - Waits for each to restart.
278  *
279  * Returns:
280  *  -1: error
281  *   0: NA
282  *   1: ok
283  */
284 int
285 restart_cpus(cpuset_t map)
286 {
287 #ifdef KTR
288 	char cpusetbuf[CPUSETBUFSIZ];
289 #endif
290 
291 	if (!smp_started)
292 		return 0;
293 
294 	CTR1(KTR_SMP, "restart_cpus(%s)", cpusetobj_strprint(cpusetbuf, &map));
295 
296 	/* signal other cpus to restart */
297 	CPU_COPY_STORE_REL(&map, &started_cpus);
298 
299 	/* wait for each to clear its bit */
300 	while (CPU_OVERLAP(&stopped_cpus, &map))
301 		cpu_spinwait();
302 
303 	return 1;
304 }
305 
306 /*
307  * All-CPU rendezvous.  CPUs are signalled, all execute the setup function
308  * (if specified), rendezvous, execute the action function (if specified),
309  * rendezvous again, execute the teardown function (if specified), and then
310  * resume.
311  *
312  * Note that the supplied external functions _must_ be reentrant and aware
313  * that they are running in parallel and in an unknown lock context.
314  */
315 void
316 smp_rendezvous_action(void)
317 {
318 	struct thread *td;
319 	void *local_func_arg;
320 	void (*local_setup_func)(void*);
321 	void (*local_action_func)(void*);
322 	void (*local_teardown_func)(void*);
323 #ifdef INVARIANTS
324 	int owepreempt;
325 #endif
326 
327 	/* Ensure we have up-to-date values. */
328 	atomic_add_acq_int(&smp_rv_waiters[0], 1);
329 	while (smp_rv_waiters[0] < smp_rv_ncpus)
330 		cpu_spinwait();
331 
332 	/* Fetch rendezvous parameters after acquire barrier. */
333 	local_func_arg = smp_rv_func_arg;
334 	local_setup_func = smp_rv_setup_func;
335 	local_action_func = smp_rv_action_func;
336 	local_teardown_func = smp_rv_teardown_func;
337 
338 	/*
339 	 * Use a nested critical section to prevent any preemptions
340 	 * from occurring during a rendezvous action routine.
341 	 * Specifically, if a rendezvous handler is invoked via an IPI
342 	 * and the interrupted thread was in the critical_exit()
343 	 * function after setting td_critnest to 0 but before
344 	 * performing a deferred preemption, this routine can be
345 	 * invoked with td_critnest set to 0 and td_owepreempt true.
346 	 * In that case, a critical_exit() during the rendezvous
347 	 * action would trigger a preemption which is not permitted in
348 	 * a rendezvous action.  To fix this, wrap all of the
349 	 * rendezvous action handlers in a critical section.  We
350 	 * cannot use a regular critical section however as having
351 	 * critical_exit() preempt from this routine would also be
352 	 * problematic (the preemption must not occur before the IPI
353 	 * has been acknowledged via an EOI).  Instead, we
354 	 * intentionally ignore td_owepreempt when leaving the
355 	 * critical section.  This should be harmless because we do
356 	 * not permit rendezvous action routines to schedule threads,
357 	 * and thus td_owepreempt should never transition from 0 to 1
358 	 * during this routine.
359 	 */
360 	td = curthread;
361 	td->td_critnest++;
362 #ifdef INVARIANTS
363 	owepreempt = td->td_owepreempt;
364 #endif
365 
366 	/*
367 	 * If requested, run a setup function before the main action
368 	 * function.  Ensure all CPUs have completed the setup
369 	 * function before moving on to the action function.
370 	 */
371 	if (local_setup_func != smp_no_rendevous_barrier) {
372 		if (smp_rv_setup_func != NULL)
373 			smp_rv_setup_func(smp_rv_func_arg);
374 		atomic_add_int(&smp_rv_waiters[1], 1);
375 		while (smp_rv_waiters[1] < smp_rv_ncpus)
376                 	cpu_spinwait();
377 	}
378 
379 	if (local_action_func != NULL)
380 		local_action_func(local_func_arg);
381 
382 	if (local_teardown_func != smp_no_rendevous_barrier) {
383 		/*
384 		 * Signal that the main action has been completed.  If a
385 		 * full exit rendezvous is requested, then all CPUs will
386 		 * wait here until all CPUs have finished the main action.
387 		 */
388 		atomic_add_int(&smp_rv_waiters[2], 1);
389 		while (smp_rv_waiters[2] < smp_rv_ncpus)
390 			cpu_spinwait();
391 
392 		if (local_teardown_func != NULL)
393 			local_teardown_func(local_func_arg);
394 	}
395 
396 	/*
397 	 * Signal that the rendezvous is fully completed by this CPU.
398 	 * This means that no member of smp_rv_* pseudo-structure will be
399 	 * accessed by this target CPU after this point; in particular,
400 	 * memory pointed by smp_rv_func_arg.
401 	 */
402 	atomic_add_int(&smp_rv_waiters[3], 1);
403 
404 	td->td_critnest--;
405 	KASSERT(owepreempt == td->td_owepreempt,
406 	    ("rendezvous action changed td_owepreempt"));
407 }
408 
409 void
410 smp_rendezvous_cpus(cpuset_t map,
411 	void (* setup_func)(void *),
412 	void (* action_func)(void *),
413 	void (* teardown_func)(void *),
414 	void *arg)
415 {
416 	int curcpumap, i, ncpus = 0;
417 
418 	if (!smp_started) {
419 		if (setup_func != NULL)
420 			setup_func(arg);
421 		if (action_func != NULL)
422 			action_func(arg);
423 		if (teardown_func != NULL)
424 			teardown_func(arg);
425 		return;
426 	}
427 
428 	CPU_FOREACH(i) {
429 		if (CPU_ISSET(i, &map))
430 			ncpus++;
431 	}
432 	if (ncpus == 0)
433 		panic("ncpus is 0 with non-zero map");
434 
435 	mtx_lock_spin(&smp_ipi_mtx);
436 
437 	/* Pass rendezvous parameters via global variables. */
438 	smp_rv_ncpus = ncpus;
439 	smp_rv_setup_func = setup_func;
440 	smp_rv_action_func = action_func;
441 	smp_rv_teardown_func = teardown_func;
442 	smp_rv_func_arg = arg;
443 	smp_rv_waiters[1] = 0;
444 	smp_rv_waiters[2] = 0;
445 	smp_rv_waiters[3] = 0;
446 	atomic_store_rel_int(&smp_rv_waiters[0], 0);
447 
448 	/*
449 	 * Signal other processors, which will enter the IPI with
450 	 * interrupts off.
451 	 */
452 	curcpumap = CPU_ISSET(curcpu, &map);
453 	CPU_CLR(curcpu, &map);
454 	ipi_selected(map, IPI_RENDEZVOUS);
455 
456 	/* Check if the current CPU is in the map */
457 	if (curcpumap != 0)
458 		smp_rendezvous_action();
459 
460 	/*
461 	 * Ensure that the master CPU waits for all the other
462 	 * CPUs to finish the rendezvous, so that smp_rv_*
463 	 * pseudo-structure and the arg are guaranteed to not
464 	 * be in use.
465 	 */
466 	while (atomic_load_acq_int(&smp_rv_waiters[3]) < ncpus)
467 		cpu_spinwait();
468 
469 	mtx_unlock_spin(&smp_ipi_mtx);
470 }
471 
472 void
473 smp_rendezvous(void (* setup_func)(void *),
474 	       void (* action_func)(void *),
475 	       void (* teardown_func)(void *),
476 	       void *arg)
477 {
478 	smp_rendezvous_cpus(all_cpus, setup_func, action_func, teardown_func, arg);
479 }
480 
481 static struct cpu_group group[MAXCPU];
482 
483 struct cpu_group *
484 smp_topo(void)
485 {
486 	char cpusetbuf[CPUSETBUFSIZ], cpusetbuf2[CPUSETBUFSIZ];
487 	struct cpu_group *top;
488 
489 	/*
490 	 * Check for a fake topology request for debugging purposes.
491 	 */
492 	switch (smp_topology) {
493 	case 1:
494 		/* Dual core with no sharing.  */
495 		top = smp_topo_1level(CG_SHARE_NONE, 2, 0);
496 		break;
497 	case 2:
498 		/* No topology, all cpus are equal. */
499 		top = smp_topo_none();
500 		break;
501 	case 3:
502 		/* Dual core with shared L2.  */
503 		top = smp_topo_1level(CG_SHARE_L2, 2, 0);
504 		break;
505 	case 4:
506 		/* quad core, shared l3 among each package, private l2.  */
507 		top = smp_topo_1level(CG_SHARE_L3, 4, 0);
508 		break;
509 	case 5:
510 		/* quad core,  2 dualcore parts on each package share l2.  */
511 		top = smp_topo_2level(CG_SHARE_NONE, 2, CG_SHARE_L2, 2, 0);
512 		break;
513 	case 6:
514 		/* Single-core 2xHTT */
515 		top = smp_topo_1level(CG_SHARE_L1, 2, CG_FLAG_HTT);
516 		break;
517 	case 7:
518 		/* quad core with a shared l3, 8 threads sharing L2.  */
519 		top = smp_topo_2level(CG_SHARE_L3, 4, CG_SHARE_L2, 8,
520 		    CG_FLAG_SMT);
521 		break;
522 	default:
523 		/* Default, ask the system what it wants. */
524 		top = cpu_topo();
525 		break;
526 	}
527 	/*
528 	 * Verify the returned topology.
529 	 */
530 	if (top->cg_count != mp_ncpus)
531 		panic("Built bad topology at %p.  CPU count %d != %d",
532 		    top, top->cg_count, mp_ncpus);
533 	if (CPU_CMP(&top->cg_mask, &all_cpus))
534 		panic("Built bad topology at %p.  CPU mask (%s) != (%s)",
535 		    top, cpusetobj_strprint(cpusetbuf, &top->cg_mask),
536 		    cpusetobj_strprint(cpusetbuf2, &all_cpus));
537 	return (top);
538 }
539 
540 struct cpu_group *
541 smp_topo_none(void)
542 {
543 	struct cpu_group *top;
544 
545 	top = &group[0];
546 	top->cg_parent = NULL;
547 	top->cg_child = NULL;
548 	top->cg_mask = all_cpus;
549 	top->cg_count = mp_ncpus;
550 	top->cg_children = 0;
551 	top->cg_level = CG_SHARE_NONE;
552 	top->cg_flags = 0;
553 
554 	return (top);
555 }
556 
557 static int
558 smp_topo_addleaf(struct cpu_group *parent, struct cpu_group *child, int share,
559     int count, int flags, int start)
560 {
561 	char cpusetbuf[CPUSETBUFSIZ], cpusetbuf2[CPUSETBUFSIZ];
562 	cpuset_t mask;
563 	int i;
564 
565 	CPU_ZERO(&mask);
566 	for (i = 0; i < count; i++, start++)
567 		CPU_SET(start, &mask);
568 	child->cg_parent = parent;
569 	child->cg_child = NULL;
570 	child->cg_children = 0;
571 	child->cg_level = share;
572 	child->cg_count = count;
573 	child->cg_flags = flags;
574 	child->cg_mask = mask;
575 	parent->cg_children++;
576 	for (; parent != NULL; parent = parent->cg_parent) {
577 		if (CPU_OVERLAP(&parent->cg_mask, &child->cg_mask))
578 			panic("Duplicate children in %p.  mask (%s) child (%s)",
579 			    parent,
580 			    cpusetobj_strprint(cpusetbuf, &parent->cg_mask),
581 			    cpusetobj_strprint(cpusetbuf2, &child->cg_mask));
582 		CPU_OR(&parent->cg_mask, &child->cg_mask);
583 		parent->cg_count += child->cg_count;
584 	}
585 
586 	return (start);
587 }
588 
589 struct cpu_group *
590 smp_topo_1level(int share, int count, int flags)
591 {
592 	struct cpu_group *child;
593 	struct cpu_group *top;
594 	int packages;
595 	int cpu;
596 	int i;
597 
598 	cpu = 0;
599 	top = &group[0];
600 	packages = mp_ncpus / count;
601 	top->cg_child = child = &group[1];
602 	top->cg_level = CG_SHARE_NONE;
603 	for (i = 0; i < packages; i++, child++)
604 		cpu = smp_topo_addleaf(top, child, share, count, flags, cpu);
605 	return (top);
606 }
607 
608 struct cpu_group *
609 smp_topo_2level(int l2share, int l2count, int l1share, int l1count,
610     int l1flags)
611 {
612 	struct cpu_group *top;
613 	struct cpu_group *l1g;
614 	struct cpu_group *l2g;
615 	int cpu;
616 	int i;
617 	int j;
618 
619 	cpu = 0;
620 	top = &group[0];
621 	l2g = &group[1];
622 	top->cg_child = l2g;
623 	top->cg_level = CG_SHARE_NONE;
624 	top->cg_children = mp_ncpus / (l2count * l1count);
625 	l1g = l2g + top->cg_children;
626 	for (i = 0; i < top->cg_children; i++, l2g++) {
627 		l2g->cg_parent = top;
628 		l2g->cg_child = l1g;
629 		l2g->cg_level = l2share;
630 		for (j = 0; j < l2count; j++, l1g++)
631 			cpu = smp_topo_addleaf(l2g, l1g, l1share, l1count,
632 			    l1flags, cpu);
633 	}
634 	return (top);
635 }
636 
637 
638 struct cpu_group *
639 smp_topo_find(struct cpu_group *top, int cpu)
640 {
641 	struct cpu_group *cg;
642 	cpuset_t mask;
643 	int children;
644 	int i;
645 
646 	CPU_SETOF(cpu, &mask);
647 	cg = top;
648 	for (;;) {
649 		if (!CPU_OVERLAP(&cg->cg_mask, &mask))
650 			return (NULL);
651 		if (cg->cg_children == 0)
652 			return (cg);
653 		children = cg->cg_children;
654 		for (i = 0, cg = cg->cg_child; i < children; cg++, i++)
655 			if (CPU_OVERLAP(&cg->cg_mask, &mask))
656 				break;
657 	}
658 	return (NULL);
659 }
660 #else /* !SMP */
661 
662 void
663 smp_rendezvous_cpus(cpuset_t map,
664 	void (*setup_func)(void *),
665 	void (*action_func)(void *),
666 	void (*teardown_func)(void *),
667 	void *arg)
668 {
669 	if (setup_func != NULL)
670 		setup_func(arg);
671 	if (action_func != NULL)
672 		action_func(arg);
673 	if (teardown_func != NULL)
674 		teardown_func(arg);
675 }
676 
677 void
678 smp_rendezvous(void (*setup_func)(void *),
679 	       void (*action_func)(void *),
680 	       void (*teardown_func)(void *),
681 	       void *arg)
682 {
683 
684 	if (setup_func != NULL)
685 		setup_func(arg);
686 	if (action_func != NULL)
687 		action_func(arg);
688 	if (teardown_func != NULL)
689 		teardown_func(arg);
690 }
691 
692 /*
693  * Provide dummy SMP support for UP kernels.  Modules that need to use SMP
694  * APIs will still work using this dummy support.
695  */
696 static void
697 mp_setvariables_for_up(void *dummy)
698 {
699 	mp_ncpus = 1;
700 	mp_maxid = PCPU_GET(cpuid);
701 	CPU_SETOF(mp_maxid, &all_cpus);
702 	KASSERT(PCPU_GET(cpuid) == 0, ("UP must have a CPU ID of zero"));
703 }
704 SYSINIT(cpu_mp_setvariables, SI_SUB_TUNABLES, SI_ORDER_FIRST,
705     mp_setvariables_for_up, NULL);
706 #endif /* SMP */
707 
708 void
709 smp_no_rendevous_barrier(void *dummy)
710 {
711 #ifdef SMP
712 	KASSERT((!smp_started),("smp_no_rendevous called and smp is started"));
713 #endif
714 }
715