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