xref: /dflybsd-src/sys/kern/kern_synch.c (revision b0a405db534ead54c6e9811b89d9b262c3280011)
1 /*-
2  * Copyright (c) 1982, 1986, 1990, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
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  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
35  * $FreeBSD: src/sys/kern/kern_synch.c,v 1.87.2.6 2002/10/13 07:29:53 kbyanc Exp $
36  */
37 
38 #include "opt_ktrace.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/proc.h>
43 #include <sys/kernel.h>
44 #include <sys/signalvar.h>
45 #include <sys/resourcevar.h>
46 #include <sys/vmmeter.h>
47 #include <sys/sysctl.h>
48 #include <sys/lock.h>
49 #include <sys/uio.h>
50 #include <sys/kcollect.h>
51 #ifdef KTRACE
52 #include <sys/ktrace.h>
53 #endif
54 #include <sys/ktr.h>
55 #include <sys/serialize.h>
56 
57 #include <sys/signal2.h>
58 #include <sys/thread2.h>
59 #include <sys/spinlock2.h>
60 #include <sys/mutex2.h>
61 
62 #include <machine/cpu.h>
63 #include <machine/smp.h>
64 
65 #include <vm/vm_extern.h>
66 
67 struct tslpque {
68 	TAILQ_HEAD(, thread)	queue;
69 	const volatile void	*ident0;
70 	const volatile void	*ident1;
71 	const volatile void	*ident2;
72 	const volatile void	*ident3;
73 };
74 
75 static void sched_setup (void *dummy);
76 SYSINIT(sched_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, sched_setup, NULL);
77 static void sched_dyninit (void *dummy);
78 SYSINIT(sched_dyninit, SI_BOOT1_DYNALLOC, SI_ORDER_FIRST, sched_dyninit, NULL);
79 
80 int	lbolt;
81 void	*lbolt_syncer;
82 int	ncpus;
83 int	ncpus2, ncpus2_shift, ncpus2_mask;	/* note: mask not cpumask_t */
84 int	ncpus_fit, ncpus_fit_mask;		/* note: mask not cpumask_t */
85 int	safepri;
86 int	tsleep_now_works;
87 int	tsleep_crypto_dump = 0;
88 
89 MALLOC_DEFINE(M_TSLEEP, "tslpque", "tsleep queues");
90 
91 #define __DEALL(ident)	__DEQUALIFY(void *, ident)
92 
93 #if !defined(KTR_TSLEEP)
94 #define KTR_TSLEEP	KTR_ALL
95 #endif
96 KTR_INFO_MASTER(tsleep);
97 KTR_INFO(KTR_TSLEEP, tsleep, tsleep_beg, 0, "tsleep enter %p", const volatile void *ident);
98 KTR_INFO(KTR_TSLEEP, tsleep, tsleep_end, 1, "tsleep exit");
99 KTR_INFO(KTR_TSLEEP, tsleep, wakeup_beg, 2, "wakeup enter %p", const volatile void *ident);
100 KTR_INFO(KTR_TSLEEP, tsleep, wakeup_end, 3, "wakeup exit");
101 KTR_INFO(KTR_TSLEEP, tsleep, ilockfail,  4, "interlock failed %p", const volatile void *ident);
102 
103 #define logtsleep1(name)	KTR_LOG(tsleep_ ## name)
104 #define logtsleep2(name, val)	KTR_LOG(tsleep_ ## name, val)
105 
106 struct loadavg averunnable =
107 	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
108 /*
109  * Constants for averages over 1, 5, and 15 minutes
110  * when sampling at 5 second intervals.
111  */
112 static fixpt_t cexp[3] = {
113 	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
114 	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
115 	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
116 };
117 
118 static void	endtsleep (void *);
119 static void	loadav (void *arg);
120 static void	schedcpu (void *arg);
121 
122 static int pctcpu_decay = 10;
123 SYSCTL_INT(_kern, OID_AUTO, pctcpu_decay, CTLFLAG_RW, &pctcpu_decay, 0, "");
124 
125 /*
126  * kernel uses `FSCALE', userland (SHOULD) use kern.fscale
127  */
128 int     fscale __unused = FSCALE;	/* exported to systat */
129 SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, "");
130 
131 /*
132  * Recompute process priorities, once a second.
133  *
134  * Since the userland schedulers are typically event oriented, if the
135  * estcpu calculation at wakeup() time is not sufficient to make a
136  * process runnable relative to other processes in the system we have
137  * a 1-second recalc to help out.
138  *
139  * This code also allows us to store sysclock_t data in the process structure
140  * without fear of an overrun, since sysclock_t are guarenteed to hold
141  * several seconds worth of count.
142  *
143  * WARNING!  callouts can preempt normal threads.  However, they will not
144  * preempt a thread holding a spinlock so we *can* safely use spinlocks.
145  */
146 static int schedcpu_stats(struct proc *p, void *data __unused);
147 static int schedcpu_resource(struct proc *p, void *data __unused);
148 
149 static void
150 schedcpu(void *arg)
151 {
152 	allproc_scan(schedcpu_stats, NULL, 1);
153 	allproc_scan(schedcpu_resource, NULL, 1);
154 	if (mycpu->gd_cpuid == 0) {
155 		wakeup((caddr_t)&lbolt);
156 		wakeup(lbolt_syncer);
157 	}
158 	callout_reset(&mycpu->gd_schedcpu_callout, hz, schedcpu, NULL);
159 }
160 
161 /*
162  * General process statistics once a second
163  */
164 static int
165 schedcpu_stats(struct proc *p, void *data __unused)
166 {
167 	struct lwp *lp;
168 
169 	/*
170 	 * Threads may not be completely set up if process in SIDL state.
171 	 */
172 	if (p->p_stat == SIDL)
173 		return(0);
174 
175 	PHOLD(p);
176 	if (lwkt_trytoken(&p->p_token) == FALSE) {
177 		PRELE(p);
178 		return(0);
179 	}
180 
181 	p->p_swtime++;
182 	FOREACH_LWP_IN_PROC(lp, p) {
183 		if (lp->lwp_stat == LSSLEEP) {
184 			++lp->lwp_slptime;
185 			if (lp->lwp_slptime == 1)
186 				p->p_usched->uload_update(lp);
187 		}
188 
189 		/*
190 		 * Only recalculate processes that are active or have slept
191 		 * less then 2 seconds.  The schedulers understand this.
192 		 * Otherwise decay by 50% per second.
193 		 */
194 		if (lp->lwp_slptime <= 1) {
195 			p->p_usched->recalculate(lp);
196 		} else {
197 			int decay;
198 
199 			decay = pctcpu_decay;
200 			cpu_ccfence();
201 			if (decay <= 1)
202 				decay = 1;
203 			if (decay > 100)
204 				decay = 100;
205 			lp->lwp_pctcpu = (lp->lwp_pctcpu * (decay - 1)) / decay;
206 		}
207 	}
208 	lwkt_reltoken(&p->p_token);
209 	lwkt_yield();
210 	PRELE(p);
211 	return(0);
212 }
213 
214 /*
215  * Resource checks.  XXX break out since ksignal/killproc can block,
216  * limiting us to one process killed per second.  There is probably
217  * a better way.
218  */
219 static int
220 schedcpu_resource(struct proc *p, void *data __unused)
221 {
222 	u_int64_t ttime;
223 	struct lwp *lp;
224 
225 	if (p->p_stat == SIDL)
226 		return(0);
227 
228 	PHOLD(p);
229 	if (lwkt_trytoken(&p->p_token) == FALSE) {
230 		PRELE(p);
231 		return(0);
232 	}
233 
234 	if (p->p_stat == SZOMB || p->p_limit == NULL) {
235 		lwkt_reltoken(&p->p_token);
236 		PRELE(p);
237 		return(0);
238 	}
239 
240 	ttime = 0;
241 	FOREACH_LWP_IN_PROC(lp, p) {
242 		/*
243 		 * We may have caught an lp in the middle of being
244 		 * created, lwp_thread can be NULL.
245 		 */
246 		if (lp->lwp_thread) {
247 			ttime += lp->lwp_thread->td_sticks;
248 			ttime += lp->lwp_thread->td_uticks;
249 		}
250 	}
251 
252 	switch(plimit_testcpulimit(p->p_limit, ttime)) {
253 	case PLIMIT_TESTCPU_KILL:
254 		killproc(p, "exceeded maximum CPU limit");
255 		break;
256 	case PLIMIT_TESTCPU_XCPU:
257 		if ((p->p_flags & P_XCPU) == 0) {
258 			p->p_flags |= P_XCPU;
259 			ksignal(p, SIGXCPU);
260 		}
261 		break;
262 	default:
263 		break;
264 	}
265 	lwkt_reltoken(&p->p_token);
266 	lwkt_yield();
267 	PRELE(p);
268 	return(0);
269 }
270 
271 /*
272  * This is only used by ps.  Generate a cpu percentage use over
273  * a period of one second.
274  */
275 void
276 updatepcpu(struct lwp *lp, int cpticks, int ttlticks)
277 {
278 	fixpt_t acc;
279 	int remticks;
280 
281 	acc = (cpticks << FSHIFT) / ttlticks;
282 	if (ttlticks >= ESTCPUFREQ) {
283 		lp->lwp_pctcpu = acc;
284 	} else {
285 		remticks = ESTCPUFREQ - ttlticks;
286 		lp->lwp_pctcpu = (acc * ttlticks + lp->lwp_pctcpu * remticks) /
287 				ESTCPUFREQ;
288 	}
289 }
290 
291 /*
292  * Handy macros to calculate hash indices.  LOOKUP() calculates the
293  * global cpumask hash index, TCHASHSHIFT() converts that into the
294  * pcpu hash index.
295  *
296  * By making the pcpu hash arrays smaller we save a significant amount
297  * of memory at very low cost.  The real cost is in IPIs, which are handled
298  * by the much larger global cpumask hash table.
299  */
300 #define LOOKUP_PRIME	66555444443333333ULL
301 #define LOOKUP(x)	((((uintptr_t)(x) + ((uintptr_t)(x) >> 18)) ^	\
302 			  LOOKUP_PRIME) % slpque_tablesize)
303 #define TCHASHSHIFT(x)	((x) >> 4)
304 
305 static uint32_t	slpque_tablesize;
306 static cpumask_t *slpque_cpumasks;
307 
308 SYSCTL_UINT(_kern, OID_AUTO, slpque_tablesize, CTLFLAG_RD, &slpque_tablesize,
309     0, "");
310 
311 /*
312  * This is a dandy function that allows us to interlock tsleep/wakeup
313  * operations with unspecified upper level locks, such as lockmgr locks,
314  * simply by holding a critical section.  The sequence is:
315  *
316  *	(acquire upper level lock)
317  *	tsleep_interlock(blah)
318  *	(release upper level lock)
319  *	tsleep(blah, ...)
320  *
321  * Basically this functions queues us on the tsleep queue without actually
322  * descheduling us.  When tsleep() is later called with PINTERLOCK it
323  * assumes the thread was already queued, otherwise it queues it there.
324  *
325  * Thus it is possible to receive the wakeup prior to going to sleep and
326  * the race conditions are covered.
327  */
328 static __inline void
329 _tsleep_interlock(globaldata_t gd, const volatile void *ident, int flags)
330 {
331 	thread_t td = gd->gd_curthread;
332 	struct tslpque *qp;
333 	uint32_t cid;
334 	uint32_t gid;
335 
336 	crit_enter_quick(td);
337 	if (td->td_flags & TDF_TSLEEPQ) {
338 		cid = LOOKUP(td->td_wchan);
339 		gid = TCHASHSHIFT(cid);
340 		qp = &gd->gd_tsleep_hash[gid];
341 		TAILQ_REMOVE(&qp->queue, td, td_sleepq);
342 		if (TAILQ_FIRST(&qp->queue) == NULL) {
343 			ATOMIC_CPUMASK_NANDBIT(slpque_cpumasks[cid],
344 					       gd->gd_cpuid);
345 			qp->ident0 = NULL;
346 			qp->ident1 = NULL;
347 			qp->ident2 = NULL;
348 			qp->ident3 = NULL;
349 		}
350 	} else {
351 		td->td_flags |= TDF_TSLEEPQ;
352 	}
353 	cid = LOOKUP(ident);
354 	gid = TCHASHSHIFT(cid);
355 	qp = &gd->gd_tsleep_hash[gid];
356 	TAILQ_INSERT_TAIL(&qp->queue, td, td_sleepq);
357 	if (qp->ident0 != ident && qp->ident1 != ident &&
358 	    qp->ident2 != ident && qp->ident3 != ident) {
359 		if (qp->ident0 == NULL)
360 			qp->ident0 = ident;
361 		else if (qp->ident1 == NULL)
362 			qp->ident1 = ident;
363 		else if (qp->ident2 == NULL)
364 			qp->ident2 = ident;
365 		else if (qp->ident3 == NULL)
366 			qp->ident3 = ident;
367 		else
368 			qp->ident0 = (void *)(intptr_t)-1;
369 	}
370 	ATOMIC_CPUMASK_ORBIT(slpque_cpumasks[cid], gd->gd_cpuid);
371 	td->td_wchan = ident;
372 	td->td_wdomain = flags & PDOMAIN_MASK;
373 	crit_exit_quick(td);
374 }
375 
376 void
377 tsleep_interlock(const volatile void *ident, int flags)
378 {
379 	_tsleep_interlock(mycpu, ident, flags);
380 }
381 
382 /*
383  * Remove thread from sleepq.  Must be called with a critical section held.
384  * The thread must not be migrating.
385  */
386 static __inline void
387 _tsleep_remove(thread_t td)
388 {
389 	globaldata_t gd = mycpu;
390 	struct tslpque *qp;
391 	uint32_t cid;
392 	uint32_t gid;
393 
394 	KKASSERT(td->td_gd == gd && IN_CRITICAL_SECT(td));
395 	KKASSERT((td->td_flags & TDF_MIGRATING) == 0);
396 	if (td->td_flags & TDF_TSLEEPQ) {
397 		td->td_flags &= ~TDF_TSLEEPQ;
398 		cid = LOOKUP(td->td_wchan);
399 		gid = TCHASHSHIFT(cid);
400 		qp = &gd->gd_tsleep_hash[gid];
401 		TAILQ_REMOVE(&qp->queue, td, td_sleepq);
402 		if (TAILQ_FIRST(&qp->queue) == NULL) {
403 			ATOMIC_CPUMASK_NANDBIT(slpque_cpumasks[cid],
404 					       gd->gd_cpuid);
405 		}
406 		td->td_wchan = NULL;
407 		td->td_wdomain = 0;
408 	}
409 }
410 
411 void
412 tsleep_remove(thread_t td)
413 {
414 	_tsleep_remove(td);
415 }
416 
417 /*
418  * General sleep call.  Suspends the current process until a wakeup is
419  * performed on the specified identifier.  The process will then be made
420  * runnable with the specified priority.  Sleeps at most timo/hz seconds
421  * (0 means no timeout).  If flags includes PCATCH flag, signals are checked
422  * before and after sleeping, else signals are not checked.  Returns 0 if
423  * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
424  * signal needs to be delivered, ERESTART is returned if the current system
425  * call should be restarted if possible, and EINTR is returned if the system
426  * call should be interrupted by the signal (return EINTR).
427  *
428  * Note that if we are a process, we release_curproc() before messing with
429  * the LWKT scheduler.
430  *
431  * During autoconfiguration or after a panic, a sleep will simply
432  * lower the priority briefly to allow interrupts, then return.
433  *
434  * WARNING!  This code can't block (short of switching away), or bad things
435  *           will happen.  No getting tokens, no blocking locks, etc.
436  */
437 int
438 tsleep(const volatile void *ident, int flags, const char *wmesg, int timo)
439 {
440 	struct thread *td = curthread;
441 	struct lwp *lp = td->td_lwp;
442 	struct proc *p = td->td_proc;		/* may be NULL */
443 	globaldata_t gd;
444 	int sig;
445 	int catch;
446 	int error;
447 	int oldpri;
448 	struct callout thandle;
449 
450 	/*
451 	 * Currently a severe hack.  Make sure any delayed wakeups
452 	 * are flushed before we sleep or we might deadlock on whatever
453 	 * event we are sleeping on.
454 	 */
455 	if (td->td_flags & TDF_DELAYED_WAKEUP)
456 		wakeup_end_delayed();
457 
458 	/*
459 	 * NOTE: removed KTRPOINT, it could cause races due to blocking
460 	 * even in stable.  Just scrap it for now.
461 	 */
462 	if (!tsleep_crypto_dump && (tsleep_now_works == 0 || panicstr)) {
463 		/*
464 		 * After a panic, or before we actually have an operational
465 		 * softclock, just give interrupts a chance, then just return;
466 		 *
467 		 * don't run any other procs or panic below,
468 		 * in case this is the idle process and already asleep.
469 		 */
470 		splz();
471 		oldpri = td->td_pri;
472 		lwkt_setpri_self(safepri);
473 		lwkt_switch();
474 		lwkt_setpri_self(oldpri);
475 		return (0);
476 	}
477 	logtsleep2(tsleep_beg, ident);
478 	gd = td->td_gd;
479 	KKASSERT(td != &gd->gd_idlethread);	/* you must be kidding! */
480 	td->td_wakefromcpu = -1;		/* overwritten by _wakeup */
481 
482 	/*
483 	 * NOTE: all of this occurs on the current cpu, including any
484 	 * callout-based wakeups, so a critical section is a sufficient
485 	 * interlock.
486 	 *
487 	 * The entire sequence through to where we actually sleep must
488 	 * run without breaking the critical section.
489 	 */
490 	catch = flags & PCATCH;
491 	error = 0;
492 	sig = 0;
493 
494 	crit_enter_quick(td);
495 
496 	KASSERT(ident != NULL, ("tsleep: no ident"));
497 	KASSERT(lp == NULL ||
498 		lp->lwp_stat == LSRUN ||	/* Obvious */
499 		lp->lwp_stat == LSSTOP,		/* Set in tstop */
500 		("tsleep %p %s %d",
501 			ident, wmesg, lp->lwp_stat));
502 
503 	/*
504 	 * We interlock the sleep queue if the caller has not already done
505 	 * it for us.  This must be done before we potentially acquire any
506 	 * tokens or we can loose the wakeup.
507 	 */
508 	if ((flags & PINTERLOCKED) == 0) {
509 		_tsleep_interlock(gd, ident, flags);
510 	}
511 
512 	/*
513 	 * Setup for the current process (if this is a process).  We must
514 	 * interlock with lwp_token to avoid remote wakeup races via
515 	 * setrunnable()
516 	 */
517 	if (lp) {
518 		lwkt_gettoken(&lp->lwp_token);
519 
520 		/*
521 		 * If the umbrella process is in the SCORE state then
522 		 * make sure that the thread is flagged going into a
523 		 * normal sleep to allow the core dump to proceed, otherwise
524 		 * the coredump can end up waiting forever.  If the normal
525 		 * sleep is woken up, the thread will enter a stopped state
526 		 * upon return to userland.
527 		 *
528 		 * We do not want to interrupt or cause a thread exist at
529 		 * this juncture because that will mess-up the state the
530 		 * coredump is trying to save.
531 		 */
532 		if (p->p_stat == SCORE &&
533 		    (lp->lwp_mpflags & LWP_MP_WSTOP) == 0) {
534 			atomic_set_int(&lp->lwp_mpflags, LWP_MP_WSTOP);
535 			++p->p_nstopped;
536 		}
537 
538 		/*
539 		 * PCATCH requested.
540 		 */
541 		if (catch) {
542 			/*
543 			 * Early termination if PCATCH was set and a
544 			 * signal is pending, interlocked with the
545 			 * critical section.
546 			 *
547 			 * Early termination only occurs when tsleep() is
548 			 * entered while in a normal LSRUN state.
549 			 */
550 			if ((sig = CURSIG(lp)) != 0)
551 				goto resume;
552 
553 			/*
554 			 * Causes ksignal to wake us up if a signal is
555 			 * received (interlocked with lp->lwp_token).
556 			 */
557 			lp->lwp_flags |= LWP_SINTR;
558 		}
559 	} else {
560 		KKASSERT(p == NULL);
561 	}
562 
563 	/*
564 	 * Make sure the current process has been untangled from
565 	 * the userland scheduler and initialize slptime to start
566 	 * counting.
567 	 *
568 	 * NOTE: td->td_wakefromcpu is pre-set by the release function
569 	 *	 for the dfly scheduler, and then adjusted by _wakeup()
570 	 */
571 	if (lp) {
572 		p->p_usched->release_curproc(lp);
573 		lp->lwp_slptime = 0;
574 	}
575 
576 	/*
577 	 * If the interlocked flag is set but our cpu bit in the slpqueue
578 	 * is no longer set, then a wakeup was processed inbetween the
579 	 * tsleep_interlock() (ours or the callers), and here.  This can
580 	 * occur under numerous circumstances including when we release the
581 	 * current process.
582 	 *
583 	 * Extreme loads can cause the sending of an IPI (e.g. wakeup()'s)
584 	 * to process incoming IPIs, thus draining incoming wakeups.
585 	 */
586 	if ((td->td_flags & TDF_TSLEEPQ) == 0) {
587 		logtsleep2(ilockfail, ident);
588 		goto resume;
589 	}
590 
591 	/*
592 	 * scheduling is blocked while in a critical section.  Coincide
593 	 * the descheduled-by-tsleep flag with the descheduling of the
594 	 * lwkt.
595 	 *
596 	 * The timer callout is localized on our cpu and interlocked by
597 	 * our critical section.
598 	 */
599 	lwkt_deschedule_self(td);
600 	td->td_flags |= TDF_TSLEEP_DESCHEDULED;
601 	td->td_wmesg = wmesg;
602 
603 	/*
604 	 * Setup the timeout, if any.  The timeout is only operable while
605 	 * the thread is flagged descheduled.
606 	 */
607 	KKASSERT((td->td_flags & TDF_TIMEOUT) == 0);
608 	if (timo) {
609 		callout_init_mp(&thandle);
610 		callout_reset(&thandle, timo, endtsleep, td);
611 	}
612 
613 	/*
614 	 * Beddy bye bye.
615 	 */
616 	if (lp) {
617 		/*
618 		 * Ok, we are sleeping.  Place us in the SSLEEP state.
619 		 */
620 		KKASSERT((lp->lwp_mpflags & LWP_MP_ONRUNQ) == 0);
621 
622 		/*
623 		 * tstop() sets LSSTOP, so don't fiddle with that.
624 		 */
625 		if (lp->lwp_stat != LSSTOP)
626 			lp->lwp_stat = LSSLEEP;
627 		lp->lwp_ru.ru_nvcsw++;
628 		p->p_usched->uload_update(lp);
629 		lwkt_switch();
630 
631 		/*
632 		 * And when we are woken up, put us back in LSRUN.  If we
633 		 * slept for over a second, recalculate our estcpu.
634 		 */
635 		lp->lwp_stat = LSRUN;
636 		if (lp->lwp_slptime) {
637 			p->p_usched->uload_update(lp);
638 			p->p_usched->recalculate(lp);
639 		}
640 		lp->lwp_slptime = 0;
641 	} else {
642 		lwkt_switch();
643 	}
644 
645 	/*
646 	 * Make sure we haven't switched cpus while we were asleep.  It's
647 	 * not supposed to happen.  Cleanup our temporary flags.
648 	 */
649 	KKASSERT(gd == td->td_gd);
650 
651 	/*
652 	 * Cleanup the timeout.  If the timeout has already occured thandle
653 	 * has already been stopped, otherwise stop thandle.  If the timeout
654 	 * is running (the callout thread must be blocked trying to get
655 	 * lwp_token) then wait for us to get scheduled.
656 	 */
657 	if (timo) {
658 		while (td->td_flags & TDF_TIMEOUT_RUNNING) {
659 			/* else we won't get rescheduled! */
660 			if (lp->lwp_stat != LSSTOP)
661 				lp->lwp_stat = LSSLEEP;
662 			lwkt_deschedule_self(td);
663 			td->td_wmesg = "tsrace";
664 			lwkt_switch();
665 			kprintf("td %p %s: timeout race\n", td, td->td_comm);
666 		}
667 		if (td->td_flags & TDF_TIMEOUT) {
668 			td->td_flags &= ~TDF_TIMEOUT;
669 			error = EWOULDBLOCK;
670 		} else {
671 			/* does not block when on same cpu */
672 			callout_stop(&thandle);
673 		}
674 	}
675 	td->td_flags &= ~TDF_TSLEEP_DESCHEDULED;
676 
677 	/*
678 	 * Make sure we have been removed from the sleepq.  In most
679 	 * cases this will have been done for us already but it is
680 	 * possible for a scheduling IPI to be in-flight from a
681 	 * previous tsleep/tsleep_interlock() or due to a straight-out
682 	 * call to lwkt_schedule() (in the case of an interrupt thread),
683 	 * causing a spurious wakeup.
684 	 */
685 	_tsleep_remove(td);
686 	td->td_wmesg = NULL;
687 
688 	/*
689 	 * Figure out the correct error return.  If interrupted by a
690 	 * signal we want to return EINTR or ERESTART.
691 	 */
692 resume:
693 	if (lp) {
694 		if (catch && error == 0) {
695 			if (sig != 0 || (sig = CURSIG(lp))) {
696 				if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
697 					error = EINTR;
698 				else
699 					error = ERESTART;
700 			}
701 		}
702 
703 		lp->lwp_flags &= ~LWP_SINTR;
704 
705 		/*
706 		 * Unconditionally set us to LSRUN on resume.  lwp_stat could
707 		 * be in a weird state due to the goto resume, particularly
708 		 * when tsleep() is called from tstop().
709 		 */
710 		lp->lwp_stat = LSRUN;
711 		lwkt_reltoken(&lp->lwp_token);
712 	}
713 	logtsleep1(tsleep_end);
714 	crit_exit_quick(td);
715 	return (error);
716 }
717 
718 /*
719  * Interlocked spinlock sleep.  An exclusively held spinlock must
720  * be passed to ssleep().  The function will atomically release the
721  * spinlock and tsleep on the ident, then reacquire the spinlock and
722  * return.
723  *
724  * This routine is fairly important along the critical path, so optimize it
725  * heavily.
726  */
727 int
728 ssleep(const volatile void *ident, struct spinlock *spin, int flags,
729        const char *wmesg, int timo)
730 {
731 	globaldata_t gd = mycpu;
732 	int error;
733 
734 	_tsleep_interlock(gd, ident, flags);
735 	spin_unlock_quick(gd, spin);
736 	error = tsleep(ident, flags | PINTERLOCKED, wmesg, timo);
737 	_spin_lock_quick(gd, spin, wmesg);
738 
739 	return (error);
740 }
741 
742 int
743 lksleep(const volatile void *ident, struct lock *lock, int flags,
744 	const char *wmesg, int timo)
745 {
746 	globaldata_t gd = mycpu;
747 	int error;
748 
749 	_tsleep_interlock(gd, ident, flags);
750 	lockmgr(lock, LK_RELEASE);
751 	error = tsleep(ident, flags | PINTERLOCKED, wmesg, timo);
752 	lockmgr(lock, LK_EXCLUSIVE);
753 
754 	return (error);
755 }
756 
757 /*
758  * Interlocked mutex sleep.  An exclusively held mutex must be passed
759  * to mtxsleep().  The function will atomically release the mutex
760  * and tsleep on the ident, then reacquire the mutex and return.
761  */
762 int
763 mtxsleep(const volatile void *ident, struct mtx *mtx, int flags,
764 	 const char *wmesg, int timo)
765 {
766 	globaldata_t gd = mycpu;
767 	int error;
768 
769 	_tsleep_interlock(gd, ident, flags);
770 	mtx_unlock(mtx);
771 	error = tsleep(ident, flags | PINTERLOCKED, wmesg, timo);
772 	mtx_lock_ex_quick(mtx);
773 
774 	return (error);
775 }
776 
777 /*
778  * Interlocked serializer sleep.  An exclusively held serializer must
779  * be passed to zsleep().  The function will atomically release
780  * the serializer and tsleep on the ident, then reacquire the serializer
781  * and return.
782  */
783 int
784 zsleep(const volatile void *ident, struct lwkt_serialize *slz, int flags,
785        const char *wmesg, int timo)
786 {
787 	globaldata_t gd = mycpu;
788 	int ret;
789 
790 	ASSERT_SERIALIZED(slz);
791 
792 	_tsleep_interlock(gd, ident, flags);
793 	lwkt_serialize_exit(slz);
794 	ret = tsleep(ident, flags | PINTERLOCKED, wmesg, timo);
795 	lwkt_serialize_enter(slz);
796 
797 	return ret;
798 }
799 
800 /*
801  * Directly block on the LWKT thread by descheduling it.  This
802  * is much faster then tsleep(), but the only legal way to wake
803  * us up is to directly schedule the thread.
804  *
805  * Setting TDF_SINTR will cause new signals to directly schedule us.
806  *
807  * This routine must be called while in a critical section.
808  */
809 int
810 lwkt_sleep(const char *wmesg, int flags)
811 {
812 	thread_t td = curthread;
813 	int sig;
814 
815 	if ((flags & PCATCH) == 0 || td->td_lwp == NULL) {
816 		td->td_flags |= TDF_BLOCKED;
817 		td->td_wmesg = wmesg;
818 		lwkt_deschedule_self(td);
819 		lwkt_switch();
820 		td->td_wmesg = NULL;
821 		td->td_flags &= ~TDF_BLOCKED;
822 		return(0);
823 	}
824 	if ((sig = CURSIG(td->td_lwp)) != 0) {
825 		if (SIGISMEMBER(td->td_proc->p_sigacts->ps_sigintr, sig))
826 			return(EINTR);
827 		else
828 			return(ERESTART);
829 
830 	}
831 	td->td_flags |= TDF_BLOCKED | TDF_SINTR;
832 	td->td_wmesg = wmesg;
833 	lwkt_deschedule_self(td);
834 	lwkt_switch();
835 	td->td_flags &= ~(TDF_BLOCKED | TDF_SINTR);
836 	td->td_wmesg = NULL;
837 	return(0);
838 }
839 
840 /*
841  * Implement the timeout for tsleep.
842  *
843  * This type of callout timeout is scheduled on the same cpu the process
844  * is sleeping on.  Also, at the moment, the MP lock is held.
845  */
846 static void
847 endtsleep(void *arg)
848 {
849 	thread_t td = arg;
850 	struct lwp *lp;
851 
852 	/*
853 	 * We are going to have to get the lwp_token, which means we might
854 	 * block.  This can race a tsleep getting woken up by other means
855 	 * so set TDF_TIMEOUT_RUNNING to force the tsleep to wait for our
856 	 * processing to complete (sorry tsleep!).
857 	 *
858 	 * We can safely set td_flags because td MUST be on the same cpu
859 	 * as we are.
860 	 */
861 	KKASSERT(td->td_gd == mycpu);
862 	crit_enter();
863 	td->td_flags |= TDF_TIMEOUT_RUNNING | TDF_TIMEOUT;
864 
865 	/*
866 	 * This can block but TDF_TIMEOUT_RUNNING will prevent the thread
867 	 * from exiting the tsleep on us.  The flag is interlocked by virtue
868 	 * of lp being on the same cpu as we are.
869 	 */
870 	if ((lp = td->td_lwp) != NULL)
871 		lwkt_gettoken(&lp->lwp_token);
872 
873 	KKASSERT(td->td_flags & TDF_TSLEEP_DESCHEDULED);
874 
875 	if (lp) {
876 		/*
877 		 * callout timer should normally never be set in tstop()
878 		 * because it passes a timeout of 0.  However, there is a
879 		 * case during thread exit (which SSTOP's all the threads)
880 		 * for which tstop() must break out and can (properly) leave
881 		 * the thread in LSSTOP.
882 		 */
883 		KKASSERT(lp->lwp_stat != LSSTOP ||
884 			 (lp->lwp_mpflags & LWP_MP_WEXIT));
885 		setrunnable(lp);
886 		lwkt_reltoken(&lp->lwp_token);
887 	} else {
888 		_tsleep_remove(td);
889 		lwkt_schedule(td);
890 	}
891 	KKASSERT(td->td_gd == mycpu);
892 	td->td_flags &= ~TDF_TIMEOUT_RUNNING;
893 	crit_exit();
894 }
895 
896 /*
897  * Make all processes sleeping on the specified identifier runnable.
898  * count may be zero or one only.
899  *
900  * The domain encodes the sleep/wakeup domain, flags, plus the originating
901  * cpu.
902  *
903  * This call may run without the MP lock held.  We can only manipulate thread
904  * state on the cpu owning the thread.  We CANNOT manipulate process state
905  * at all.
906  *
907  * _wakeup() can be passed to an IPI so we can't use (const volatile
908  * void *ident).
909  */
910 static void
911 _wakeup(void *ident, int domain)
912 {
913 	struct tslpque *qp;
914 	struct thread *td;
915 	struct thread *ntd;
916 	globaldata_t gd;
917 	cpumask_t mask;
918 	uint32_t cid;
919 	uint32_t gid;
920 	int wids = 0;
921 
922 	crit_enter();
923 	logtsleep2(wakeup_beg, ident);
924 	gd = mycpu;
925 	cid = LOOKUP(ident);
926 	gid = TCHASHSHIFT(cid);
927 	qp = &gd->gd_tsleep_hash[gid];
928 restart:
929 	for (td = TAILQ_FIRST(&qp->queue); td != NULL; td = ntd) {
930 		ntd = TAILQ_NEXT(td, td_sleepq);
931 		if (td->td_wchan == ident &&
932 		    td->td_wdomain == (domain & PDOMAIN_MASK)
933 		) {
934 			KKASSERT(td->td_gd == gd);
935 			_tsleep_remove(td);
936 			td->td_wakefromcpu = PWAKEUP_DECODE(domain);
937 			if (td->td_flags & TDF_TSLEEP_DESCHEDULED) {
938 				lwkt_schedule(td);
939 				if (domain & PWAKEUP_ONE)
940 					goto done;
941 			}
942 			goto restart;
943 		}
944 		if (td->td_wchan == qp->ident0)
945 			wids |= 1;
946 		else if (td->td_wchan == qp->ident1)
947 			wids |= 2;
948 		else if (td->td_wchan == qp->ident2)
949 			wids |= 4;
950 		else if (td->td_wchan == qp->ident3)
951 			wids |= 8;
952 		else
953 			wids |= 16;	/* force ident0 to be retained (-1) */
954 	}
955 
956 	/*
957 	 * Because a bunch of cpumask array entries cover the same queue, it
958 	 * is possible for our bit to remain set in some of them and cause
959 	 * spurious wakeup IPIs later on.  Make sure that the bit is cleared
960 	 * when a spurious IPI occurs to prevent further spurious IPIs.
961 	 */
962 	if (TAILQ_FIRST(&qp->queue) == NULL) {
963 		ATOMIC_CPUMASK_NANDBIT(slpque_cpumasks[cid], gd->gd_cpuid);
964 		qp->ident0 = NULL;
965 		qp->ident1 = NULL;
966 		qp->ident2 = NULL;
967 		qp->ident3 = NULL;
968 	} else {
969 		if ((wids & 1) == 0) {
970 			if ((wids & 16) == 0)
971 				qp->ident0 = NULL;
972 		}
973 		if ((wids & 2) == 0)
974 			qp->ident1 = NULL;
975 		if ((wids & 4) == 0)
976 			qp->ident2 = NULL;
977 		if ((wids & 8) == 0)
978 			qp->ident3 = NULL;
979 	}
980 
981 	/*
982 	 * We finished checking the current cpu but there still may be
983 	 * more work to do.  Either wakeup_one was requested and no matching
984 	 * thread was found, or a normal wakeup was requested and we have
985 	 * to continue checking cpus.
986 	 *
987 	 * It should be noted that this scheme is actually less expensive then
988 	 * the old scheme when waking up multiple threads, since we send
989 	 * only one IPI message per target candidate which may then schedule
990 	 * multiple threads.  Before we could have wound up sending an IPI
991 	 * message for each thread on the target cpu (!= current cpu) that
992 	 * needed to be woken up.
993 	 *
994 	 * NOTE: Wakeups occuring on remote cpus are asynchronous.  This
995 	 *	 should be ok since we are passing idents in the IPI rather
996 	 *	 then thread pointers.
997 	 *
998 	 * NOTE: We MUST mfence (or use an atomic op) prior to reading
999 	 *	 the cpumask, as another cpu may have written to it in
1000 	 *	 a fashion interlocked with whatever the caller did before
1001 	 *	 calling wakeup().  Otherwise we might miss the interaction
1002 	 *	 (kern_mutex.c can cause this problem).
1003 	 *
1004 	 *	 lfence is insufficient as it may allow a written state to
1005 	 *	 reorder around the cpumask load.
1006 	 */
1007 	if ((domain & PWAKEUP_MYCPU) == 0) {
1008 		globaldata_t tgd;
1009 		int n;
1010 
1011 		cpu_mfence();
1012 		mask = slpque_cpumasks[cid];
1013 		CPUMASK_ANDMASK(mask, gd->gd_other_cpus);
1014 		while (CPUMASK_TESTNZERO(mask)) {
1015 			n = BSRCPUMASK(mask);
1016 			CPUMASK_NANDBIT(mask, n);
1017 			tgd = globaldata_find(n);
1018 			qp = &tgd->gd_tsleep_hash[gid];
1019 			if (qp->ident0 == (void *)(intptr_t)-1)
1020 				++tgd->gd_cnt.v_wakeup_colls;
1021 			if (qp->ident0 == (void *)(intptr_t)-1 ||
1022 			    qp->ident0 == ident ||
1023 			    qp->ident1 == ident ||
1024 			    qp->ident2 == ident ||
1025 			    qp->ident3 == ident) {
1026 				lwkt_send_ipiq2(tgd, _wakeup, ident,
1027 						domain | PWAKEUP_MYCPU);
1028 			}
1029 #if 0
1030 			lwkt_send_ipiq2_mask(mask, _wakeup, ident,
1031 					     domain | PWAKEUP_MYCPU);
1032 #endif
1033 		}
1034 	}
1035 done:
1036 	logtsleep1(wakeup_end);
1037 	crit_exit();
1038 }
1039 
1040 /*
1041  * Wakeup all threads tsleep()ing on the specified ident, on all cpus
1042  */
1043 void
1044 wakeup(const volatile void *ident)
1045 {
1046     globaldata_t gd = mycpu;
1047     thread_t td = gd->gd_curthread;
1048 
1049     if (td && (td->td_flags & TDF_DELAYED_WAKEUP)) {
1050 	/*
1051 	 * If we are in a delayed wakeup section, record up to two wakeups in
1052 	 * a per-CPU queue and issue them when we block or exit the delayed
1053 	 * wakeup section.
1054 	 */
1055 	if (atomic_cmpset_ptr(&gd->gd_delayed_wakeup[0], NULL, ident))
1056 		return;
1057 	if (atomic_cmpset_ptr(&gd->gd_delayed_wakeup[1], NULL, ident))
1058 		return;
1059 
1060 	ident = atomic_swap_ptr(__DEQUALIFY(volatile void **, &gd->gd_delayed_wakeup[1]),
1061 				__DEALL(ident));
1062 	ident = atomic_swap_ptr(__DEQUALIFY(volatile void **, &gd->gd_delayed_wakeup[0]),
1063 				__DEALL(ident));
1064     }
1065 
1066     _wakeup(__DEALL(ident), PWAKEUP_ENCODE(0, gd->gd_cpuid));
1067 }
1068 
1069 /*
1070  * Wakeup one thread tsleep()ing on the specified ident, on any cpu.
1071  */
1072 void
1073 wakeup_one(const volatile void *ident)
1074 {
1075     /* XXX potentially round-robin the first responding cpu */
1076     _wakeup(__DEALL(ident), PWAKEUP_ENCODE(0, mycpu->gd_cpuid) |
1077 			    PWAKEUP_ONE);
1078 }
1079 
1080 /*
1081  * Wakeup threads tsleep()ing on the specified ident on the current cpu
1082  * only.
1083  */
1084 void
1085 wakeup_mycpu(const volatile void *ident)
1086 {
1087     _wakeup(__DEALL(ident), PWAKEUP_ENCODE(0, mycpu->gd_cpuid) |
1088 			    PWAKEUP_MYCPU);
1089 }
1090 
1091 /*
1092  * Wakeup one thread tsleep()ing on the specified ident on the current cpu
1093  * only.
1094  */
1095 void
1096 wakeup_mycpu_one(const volatile void *ident)
1097 {
1098     /* XXX potentially round-robin the first responding cpu */
1099     _wakeup(__DEALL(ident), PWAKEUP_ENCODE(0, mycpu->gd_cpuid) |
1100 			    PWAKEUP_MYCPU | PWAKEUP_ONE);
1101 }
1102 
1103 /*
1104  * Wakeup all thread tsleep()ing on the specified ident on the specified cpu
1105  * only.
1106  */
1107 void
1108 wakeup_oncpu(globaldata_t gd, const volatile void *ident)
1109 {
1110     globaldata_t mygd = mycpu;
1111     if (gd == mycpu) {
1112 	_wakeup(__DEALL(ident), PWAKEUP_ENCODE(0, mygd->gd_cpuid) |
1113 				PWAKEUP_MYCPU);
1114     } else {
1115 	lwkt_send_ipiq2(gd, _wakeup, __DEALL(ident),
1116 			PWAKEUP_ENCODE(0, mygd->gd_cpuid) |
1117 			PWAKEUP_MYCPU);
1118     }
1119 }
1120 
1121 /*
1122  * Wakeup one thread tsleep()ing on the specified ident on the specified cpu
1123  * only.
1124  */
1125 void
1126 wakeup_oncpu_one(globaldata_t gd, const volatile void *ident)
1127 {
1128     globaldata_t mygd = mycpu;
1129     if (gd == mygd) {
1130 	_wakeup(__DEALL(ident), PWAKEUP_ENCODE(0, mygd->gd_cpuid) |
1131 				PWAKEUP_MYCPU | PWAKEUP_ONE);
1132     } else {
1133 	lwkt_send_ipiq2(gd, _wakeup, __DEALL(ident),
1134 			PWAKEUP_ENCODE(0, mygd->gd_cpuid) |
1135 			PWAKEUP_MYCPU | PWAKEUP_ONE);
1136     }
1137 }
1138 
1139 /*
1140  * Wakeup all threads waiting on the specified ident that slept using
1141  * the specified domain, on all cpus.
1142  */
1143 void
1144 wakeup_domain(const volatile void *ident, int domain)
1145 {
1146     _wakeup(__DEALL(ident), PWAKEUP_ENCODE(domain, mycpu->gd_cpuid));
1147 }
1148 
1149 /*
1150  * Wakeup one thread waiting on the specified ident that slept using
1151  * the specified  domain, on any cpu.
1152  */
1153 void
1154 wakeup_domain_one(const volatile void *ident, int domain)
1155 {
1156     /* XXX potentially round-robin the first responding cpu */
1157     _wakeup(__DEALL(ident),
1158 	    PWAKEUP_ENCODE(domain, mycpu->gd_cpuid) | PWAKEUP_ONE);
1159 }
1160 
1161 void
1162 wakeup_start_delayed(void)
1163 {
1164     globaldata_t gd = mycpu;
1165 
1166     crit_enter();
1167     gd->gd_curthread->td_flags |= TDF_DELAYED_WAKEUP;
1168     crit_exit();
1169 }
1170 
1171 void
1172 wakeup_end_delayed(void)
1173 {
1174     globaldata_t gd = mycpu;
1175 
1176     if (gd->gd_curthread->td_flags & TDF_DELAYED_WAKEUP) {
1177 	crit_enter();
1178 	gd->gd_curthread->td_flags &= ~TDF_DELAYED_WAKEUP;
1179 	if (gd->gd_delayed_wakeup[0] || gd->gd_delayed_wakeup[1]) {
1180 	    if (gd->gd_delayed_wakeup[0]) {
1181 		    wakeup(gd->gd_delayed_wakeup[0]);
1182 		    gd->gd_delayed_wakeup[0] = NULL;
1183 	    }
1184 	    if (gd->gd_delayed_wakeup[1]) {
1185 		    wakeup(gd->gd_delayed_wakeup[1]);
1186 		    gd->gd_delayed_wakeup[1] = NULL;
1187 	    }
1188 	}
1189 	crit_exit();
1190     }
1191 }
1192 
1193 /*
1194  * setrunnable()
1195  *
1196  * Make a process runnable.  lp->lwp_token must be held on call and this
1197  * function must be called from the cpu owning lp.
1198  *
1199  * This only has an effect if we are in LSSTOP or LSSLEEP.
1200  */
1201 void
1202 setrunnable(struct lwp *lp)
1203 {
1204 	thread_t td = lp->lwp_thread;
1205 
1206 	ASSERT_LWKT_TOKEN_HELD(&lp->lwp_token);
1207 	KKASSERT(td->td_gd == mycpu);
1208 	crit_enter();
1209 	if (lp->lwp_stat == LSSTOP)
1210 		lp->lwp_stat = LSSLEEP;
1211 	if (lp->lwp_stat == LSSLEEP) {
1212 		_tsleep_remove(td);
1213 		lwkt_schedule(td);
1214 	} else if (td->td_flags & TDF_SINTR) {
1215 		lwkt_schedule(td);
1216 	}
1217 	crit_exit();
1218 }
1219 
1220 /*
1221  * The process is stopped due to some condition, usually because p_stat is
1222  * set to SSTOP, but also possibly due to being traced.
1223  *
1224  * Caller must hold p->p_token
1225  *
1226  * NOTE!  If the caller sets SSTOP, the caller must also clear P_WAITED
1227  * because the parent may check the child's status before the child actually
1228  * gets to this routine.
1229  *
1230  * This routine is called with the current lwp only, typically just
1231  * before returning to userland if the process state is detected as
1232  * possibly being in a stopped state.
1233  */
1234 void
1235 tstop(void)
1236 {
1237 	struct lwp *lp = curthread->td_lwp;
1238 	struct proc *p = lp->lwp_proc;
1239 	struct proc *q;
1240 
1241 	lwkt_gettoken(&lp->lwp_token);
1242 	crit_enter();
1243 
1244 	/*
1245 	 * If LWP_MP_WSTOP is set, we were sleeping
1246 	 * while our process was stopped.  At this point
1247 	 * we were already counted as stopped.
1248 	 */
1249 	if ((lp->lwp_mpflags & LWP_MP_WSTOP) == 0) {
1250 		/*
1251 		 * If we're the last thread to stop, signal
1252 		 * our parent.
1253 		 */
1254 		p->p_nstopped++;
1255 		atomic_set_int(&lp->lwp_mpflags, LWP_MP_WSTOP);
1256 		wakeup(&p->p_nstopped);
1257 		if (p->p_nstopped == p->p_nthreads) {
1258 			/*
1259 			 * Token required to interlock kern_wait()
1260 			 */
1261 			q = p->p_pptr;
1262 			PHOLD(q);
1263 			lwkt_gettoken(&q->p_token);
1264 			p->p_flags &= ~P_WAITED;
1265 			wakeup(p->p_pptr);
1266 			if ((q->p_sigacts->ps_flag & PS_NOCLDSTOP) == 0)
1267 				ksignal(q, SIGCHLD);
1268 			lwkt_reltoken(&q->p_token);
1269 			PRELE(q);
1270 		}
1271 	}
1272 
1273 	/*
1274 	 * Wait here while in a stopped state, interlocked with lwp_token.
1275 	 * We must break-out if the whole process is trying to exit.
1276 	 */
1277 	while (STOPLWP(p, lp)) {
1278 		lp->lwp_stat = LSSTOP;
1279 		tsleep(p, 0, "stop", 0);
1280 	}
1281 	p->p_nstopped--;
1282 	atomic_clear_int(&lp->lwp_mpflags, LWP_MP_WSTOP);
1283 	crit_exit();
1284 	lwkt_reltoken(&lp->lwp_token);
1285 }
1286 
1287 /*
1288  * Compute a tenex style load average of a quantity on
1289  * 1, 5 and 15 minute intervals.  This is a pcpu callout.
1290  *
1291  * We segment the lwp scan on a pcpu basis.  This does NOT
1292  * mean the associated lwps are on this cpu, it is done
1293  * just to break the work up.
1294  *
1295  * The callout on cpu0 rolls up the stats from the other
1296  * cpus.
1297  */
1298 static int loadav_count_runnable(struct lwp *p, void *data);
1299 
1300 static void
1301 loadav(void *arg)
1302 {
1303 	globaldata_t gd = mycpu;
1304 	struct loadavg *avg;
1305 	int i, nrun;
1306 
1307 	nrun = 0;
1308 	alllwp_scan(loadav_count_runnable, &nrun, 1);
1309 	gd->gd_loadav_nrunnable = nrun;
1310 	if (gd->gd_cpuid == 0) {
1311 		avg = &averunnable;
1312 		nrun = 0;
1313 		for (i = 0; i < ncpus; ++i)
1314 			nrun += globaldata_find(i)->gd_loadav_nrunnable;
1315 		for (i = 0; i < 3; i++) {
1316 			avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
1317 			    (long)nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
1318 		}
1319 	}
1320 
1321 	/*
1322 	 * Schedule the next update to occur after 5 seconds, but add a
1323 	 * random variation to avoid synchronisation with processes that
1324 	 * run at regular intervals.
1325 	 */
1326 	callout_reset(&gd->gd_loadav_callout,
1327 		      hz * 4 + (int)(krandom() % (hz * 2 + 1)),
1328 		      loadav, NULL);
1329 }
1330 
1331 static int
1332 loadav_count_runnable(struct lwp *lp, void *data)
1333 {
1334 	int *nrunp = data;
1335 	thread_t td;
1336 
1337 	switch (lp->lwp_stat) {
1338 	case LSRUN:
1339 		if ((td = lp->lwp_thread) == NULL)
1340 			break;
1341 		if (td->td_flags & TDF_BLOCKED)
1342 			break;
1343 		++*nrunp;
1344 		break;
1345 	default:
1346 		break;
1347 	}
1348 	lwkt_yield();
1349 	return(0);
1350 }
1351 
1352 /*
1353  * Regular data collection
1354  */
1355 static uint64_t
1356 collect_load_callback(int n)
1357 {
1358 	int fscale = averunnable.fscale;
1359 
1360 	return ((averunnable.ldavg[0] * 100 + (fscale >> 1)) / fscale);
1361 }
1362 
1363 static void
1364 sched_setup(void *dummy __unused)
1365 {
1366 	globaldata_t save_gd = mycpu;
1367 	globaldata_t gd;
1368 	int n;
1369 
1370 	kcollect_register(KCOLLECT_LOAD, "load", collect_load_callback,
1371 			  KCOLLECT_SCALE(KCOLLECT_LOAD_FORMAT, 0));
1372 
1373 	/*
1374 	 * Kick off timeout driven events by calling first time.  We
1375 	 * split the work across available cpus to help scale it,
1376 	 * it can eat a lot of cpu when there are a lot of processes
1377 	 * on the system.
1378 	 */
1379 	for (n = 0; n < ncpus; ++n) {
1380 		gd = globaldata_find(n);
1381 		lwkt_setcpu_self(gd);
1382 		callout_init_mp(&gd->gd_loadav_callout);
1383 		callout_init_mp(&gd->gd_schedcpu_callout);
1384 		schedcpu(NULL);
1385 		loadav(NULL);
1386 	}
1387 	lwkt_setcpu_self(save_gd);
1388 }
1389 
1390 /*
1391  * Extremely early initialization, dummy-up the tables so we don't have
1392  * to conditionalize for NULL in _wakeup() and tsleep_interlock().  Even
1393  * though the system isn't blocking this early, these functions still
1394  * try to access the hash table.
1395  *
1396  * This setup will be overridden once sched_dyninit() -> sleep_gdinit()
1397  * is called.
1398  */
1399 void
1400 sleep_early_gdinit(globaldata_t gd)
1401 {
1402 	static struct tslpque	dummy_slpque;
1403 	static cpumask_t dummy_cpumasks;
1404 
1405 	slpque_tablesize = 1;
1406 	gd->gd_tsleep_hash = &dummy_slpque;
1407 	slpque_cpumasks = &dummy_cpumasks;
1408 	TAILQ_INIT(&dummy_slpque.queue);
1409 }
1410 
1411 /*
1412  * PCPU initialization.  Called after KMALLOC is operational, by
1413  * sched_dyninit() for cpu 0, and by mi_gdinit() for other cpus later.
1414  *
1415  * WARNING! The pcpu hash table is smaller than the global cpumask
1416  *	    hash table, which can save us a lot of memory when maxproc
1417  *	    is set high.
1418  */
1419 void
1420 sleep_gdinit(globaldata_t gd)
1421 {
1422 	struct thread *td;
1423 	size_t hash_size;
1424 	uint32_t n;
1425 	uint32_t i;
1426 
1427 	/*
1428 	 * This shouldn't happen, that is there shouldn't be any threads
1429 	 * waiting on the dummy tsleep queue this early in the boot.
1430 	 */
1431 	if (gd->gd_cpuid == 0) {
1432 		struct tslpque *qp = &gd->gd_tsleep_hash[0];
1433 		TAILQ_FOREACH(td, &qp->queue, td_sleepq) {
1434 			kprintf("SLEEP_GDINIT SWITCH %s\n", td->td_comm);
1435 		}
1436 	}
1437 
1438 	/*
1439 	 * Note that we have to allocate one extra slot because we are
1440 	 * shifting a modulo value.  TCHASHSHIFT(slpque_tablesize - 1) can
1441 	 * return the same value as TCHASHSHIFT(slpque_tablesize).
1442 	 */
1443 	n = TCHASHSHIFT(slpque_tablesize) + 1;
1444 
1445 	hash_size = sizeof(struct tslpque) * n;
1446 	gd->gd_tsleep_hash = (void *)kmem_alloc3(&kernel_map, hash_size,
1447 						 VM_SUBSYS_GD,
1448 						 KM_CPU(gd->gd_cpuid));
1449 	memset(gd->gd_tsleep_hash, 0, hash_size);
1450 	for (i = 0; i < n; ++i)
1451 		TAILQ_INIT(&gd->gd_tsleep_hash[i].queue);
1452 }
1453 
1454 /*
1455  * Dynamic initialization after the memory system is operational.
1456  */
1457 static void
1458 sched_dyninit(void *dummy __unused)
1459 {
1460 	int tblsize;
1461 	int tblsize2;
1462 	int n;
1463 
1464 	/*
1465 	 * Calculate table size for slpque hash.  We want a prime number
1466 	 * large enough to avoid overloading slpque_cpumasks when the
1467 	 * system has a large number of sleeping processes, which will
1468 	 * spam IPIs on wakeup().
1469 	 *
1470 	 * While it is true this is really a per-lwp factor, generally
1471 	 * speaking the maxproc limit is a good metric to go by.
1472 	 */
1473 	for (tblsize = maxproc | 1; ; tblsize += 2) {
1474 		if (tblsize % 3 == 0)
1475 			continue;
1476 		if (tblsize % 5 == 0)
1477 			continue;
1478 		tblsize2 = (tblsize / 2) | 1;
1479 		for (n = 7; n < tblsize2; n += 2) {
1480 			if (tblsize % n == 0)
1481 				break;
1482 		}
1483 		if (n == tblsize2)
1484 			break;
1485 	}
1486 
1487 	/*
1488 	 * PIDs are currently limited to 6 digits.  Cap the table size
1489 	 * at double this.
1490 	 */
1491 	if (tblsize > 2000003)
1492 		tblsize = 2000003;
1493 
1494 	slpque_tablesize = tblsize;
1495 	slpque_cpumasks = kmalloc(sizeof(*slpque_cpumasks) * slpque_tablesize,
1496 				  M_TSLEEP, M_WAITOK | M_ZERO);
1497 	sleep_gdinit(mycpu);
1498 }
1499