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