xref: /freebsd-13-stable/sys/kern/kern_synch.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1990, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * (c) UNIX System Laboratories, Inc.
7  * All or some portions of this file are derived from material licensed
8  * to the University of California by American Telephone and Telegraph
9  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10  * the permission of UNIX System Laboratories, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
37  */
38 
39 #include <sys/cdefs.h>
40 #include "opt_ktrace.h"
41 #include "opt_sched.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/blockcount.h>
46 #include <sys/condvar.h>
47 #include <sys/kdb.h>
48 #include <sys/kernel.h>
49 #include <sys/ktr.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.h>
53 #include <sys/resourcevar.h>
54 #include <sys/sched.h>
55 #include <sys/sdt.h>
56 #include <sys/signalvar.h>
57 #include <sys/sleepqueue.h>
58 #include <sys/smp.h>
59 #include <sys/sx.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysproto.h>
62 #include <sys/vmmeter.h>
63 #ifdef KTRACE
64 #include <sys/uio.h>
65 #include <sys/ktrace.h>
66 #endif
67 #ifdef EPOCH_TRACE
68 #include <sys/epoch.h>
69 #endif
70 
71 #include <machine/cpu.h>
72 
73 static void synch_setup(void *dummy);
74 SYSINIT(synch_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, synch_setup,
75     NULL);
76 
77 int	hogticks;
78 static const char pause_wchan[MAXCPU];
79 
80 static struct callout loadav_callout;
81 
82 struct loadavg averunnable =
83 	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
84 /*
85  * Constants for averages over 1, 5, and 15 minutes
86  * when sampling at 5 second intervals.
87  */
88 static uint64_t cexp[3] = {
89 	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
90 	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
91 	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
92 };
93 
94 /* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
95 SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, FSCALE,
96     "Fixed-point scale factor used for calculating load average values");
97 
98 static void	loadav(void *arg);
99 
100 SDT_PROVIDER_DECLARE(sched);
101 SDT_PROBE_DEFINE(sched, , , preempt);
102 
103 static void
sleepinit(void * unused)104 sleepinit(void *unused)
105 {
106 
107 	hogticks = (hz / 10) * 2;	/* Default only. */
108 	init_sleepqueues();
109 }
110 
111 /*
112  * vmem tries to lock the sleepq mutexes when free'ing kva, so make sure
113  * it is available.
114  */
115 SYSINIT(sleepinit, SI_SUB_KMEM, SI_ORDER_ANY, sleepinit, NULL);
116 
117 /*
118  * General sleep call.  Suspends the current thread until a wakeup is
119  * performed on the specified identifier.  The thread will then be made
120  * runnable with the specified priority.  Sleeps at most sbt units of time
121  * (0 means no timeout).  If pri includes the PCATCH flag, let signals
122  * interrupt the sleep, otherwise ignore them while sleeping.  Returns 0 if
123  * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
124  * signal becomes pending, ERESTART is returned if the current system
125  * call should be restarted if possible, and EINTR is returned if the system
126  * call should be interrupted by the signal (return EINTR).
127  *
128  * The lock argument is unlocked before the caller is suspended, and
129  * re-locked before _sleep() returns.  If priority includes the PDROP
130  * flag the lock is not re-locked before returning.
131  */
132 int
_sleep(const void * ident,struct lock_object * lock,int priority,const char * wmesg,sbintime_t sbt,sbintime_t pr,int flags)133 _sleep(const void *ident, struct lock_object *lock, int priority,
134     const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags)
135 {
136 	struct thread *td;
137 	struct lock_class *class;
138 	uintptr_t lock_state;
139 	int catch, pri, rval, sleepq_flags;
140 	WITNESS_SAVE_DECL(lock_witness);
141 
142 	TSENTER();
143 	td = curthread;
144 #ifdef KTRACE
145 	if (KTRPOINT(td, KTR_CSW))
146 		ktrcsw(1, 0, wmesg);
147 #endif
148 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
149 	    "Sleeping on \"%s\"", wmesg);
150 	KASSERT(sbt != 0 || mtx_owned(&Giant) || lock != NULL,
151 	    ("sleeping without a lock"));
152 	KASSERT(ident != NULL, ("_sleep: NULL ident"));
153 	KASSERT(TD_IS_RUNNING(td), ("_sleep: curthread not running"));
154 	if (priority & PDROP)
155 		KASSERT(lock != NULL && lock != &Giant.lock_object,
156 		    ("PDROP requires a non-Giant lock"));
157 	if (lock != NULL)
158 		class = LOCK_CLASS(lock);
159 	else
160 		class = NULL;
161 
162 	if (SCHEDULER_STOPPED_TD(td)) {
163 		if (lock != NULL && priority & PDROP)
164 			class->lc_unlock(lock);
165 		return (0);
166 	}
167 	catch = priority & PCATCH;
168 	pri = priority & PRIMASK;
169 
170 	KASSERT(!TD_ON_SLEEPQ(td), ("recursive sleep"));
171 
172 	if ((uintptr_t)ident >= (uintptr_t)&pause_wchan[0] &&
173 	    (uintptr_t)ident <= (uintptr_t)&pause_wchan[MAXCPU - 1])
174 		sleepq_flags = SLEEPQ_PAUSE;
175 	else
176 		sleepq_flags = SLEEPQ_SLEEP;
177 	if (catch)
178 		sleepq_flags |= SLEEPQ_INTERRUPTIBLE;
179 
180 	sleepq_lock(ident);
181 	CTR5(KTR_PROC, "sleep: thread %ld (pid %ld, %s) on %s (%p)",
182 	    td->td_tid, td->td_proc->p_pid, td->td_name, wmesg, ident);
183 
184 	if (lock == &Giant.lock_object)
185 		mtx_assert(&Giant, MA_OWNED);
186 	DROP_GIANT();
187 	if (lock != NULL && lock != &Giant.lock_object &&
188 	    !(class->lc_flags & LC_SLEEPABLE)) {
189 		KASSERT(!(class->lc_flags & LC_SPINLOCK),
190 		    ("spin locks can only use msleep_spin"));
191 		WITNESS_SAVE(lock, lock_witness);
192 		lock_state = class->lc_unlock(lock);
193 	} else
194 		/* GCC needs to follow the Yellow Brick Road */
195 		lock_state = -1;
196 
197 	/*
198 	 * We put ourselves on the sleep queue and start our timeout
199 	 * before calling thread_suspend_check, as we could stop there,
200 	 * and a wakeup or a SIGCONT (or both) could occur while we were
201 	 * stopped without resuming us.  Thus, we must be ready for sleep
202 	 * when cursig() is called.  If the wakeup happens while we're
203 	 * stopped, then td will no longer be on a sleep queue upon
204 	 * return from cursig().
205 	 */
206 	sleepq_add(ident, lock, wmesg, sleepq_flags, 0);
207 	if (sbt != 0)
208 		sleepq_set_timeout_sbt(ident, sbt, pr, flags);
209 	if (lock != NULL && class->lc_flags & LC_SLEEPABLE) {
210 		sleepq_release(ident);
211 		WITNESS_SAVE(lock, lock_witness);
212 		lock_state = class->lc_unlock(lock);
213 		sleepq_lock(ident);
214 	}
215 	if (sbt != 0 && catch)
216 		rval = sleepq_timedwait_sig(ident, pri);
217 	else if (sbt != 0)
218 		rval = sleepq_timedwait(ident, pri);
219 	else if (catch)
220 		rval = sleepq_wait_sig(ident, pri);
221 	else {
222 		sleepq_wait(ident, pri);
223 		rval = 0;
224 	}
225 #ifdef KTRACE
226 	if (KTRPOINT(td, KTR_CSW))
227 		ktrcsw(0, 0, wmesg);
228 #endif
229 	PICKUP_GIANT();
230 	if (lock != NULL && lock != &Giant.lock_object && !(priority & PDROP)) {
231 		class->lc_lock(lock, lock_state);
232 		WITNESS_RESTORE(lock, lock_witness);
233 	}
234 	TSEXIT();
235 	return (rval);
236 }
237 
238 int
msleep_spin_sbt(const void * ident,struct mtx * mtx,const char * wmesg,sbintime_t sbt,sbintime_t pr,int flags)239 msleep_spin_sbt(const void *ident, struct mtx *mtx, const char *wmesg,
240     sbintime_t sbt, sbintime_t pr, int flags)
241 {
242 	struct thread *td;
243 	int rval;
244 	WITNESS_SAVE_DECL(mtx);
245 
246 	td = curthread;
247 	KASSERT(mtx != NULL, ("sleeping without a mutex"));
248 	KASSERT(ident != NULL, ("msleep_spin_sbt: NULL ident"));
249 	KASSERT(TD_IS_RUNNING(td), ("msleep_spin_sbt: curthread not running"));
250 
251 	if (SCHEDULER_STOPPED_TD(td))
252 		return (0);
253 
254 	sleepq_lock(ident);
255 	CTR5(KTR_PROC, "msleep_spin: thread %ld (pid %ld, %s) on %s (%p)",
256 	    td->td_tid, td->td_proc->p_pid, td->td_name, wmesg, ident);
257 
258 	DROP_GIANT();
259 	mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
260 	WITNESS_SAVE(&mtx->lock_object, mtx);
261 	mtx_unlock_spin(mtx);
262 
263 	/*
264 	 * We put ourselves on the sleep queue and start our timeout.
265 	 */
266 	sleepq_add(ident, &mtx->lock_object, wmesg, SLEEPQ_SLEEP, 0);
267 	if (sbt != 0)
268 		sleepq_set_timeout_sbt(ident, sbt, pr, flags);
269 
270 	/*
271 	 * Can't call ktrace with any spin locks held so it can lock the
272 	 * ktrace_mtx lock, and WITNESS_WARN considers it an error to hold
273 	 * any spin lock.  Thus, we have to drop the sleepq spin lock while
274 	 * we handle those requests.  This is safe since we have placed our
275 	 * thread on the sleep queue already.
276 	 */
277 #ifdef KTRACE
278 	if (KTRPOINT(td, KTR_CSW)) {
279 		sleepq_release(ident);
280 		ktrcsw(1, 0, wmesg);
281 		sleepq_lock(ident);
282 	}
283 #endif
284 #ifdef WITNESS
285 	sleepq_release(ident);
286 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "Sleeping on \"%s\"",
287 	    wmesg);
288 	sleepq_lock(ident);
289 #endif
290 	if (sbt != 0)
291 		rval = sleepq_timedwait(ident, 0);
292 	else {
293 		sleepq_wait(ident, 0);
294 		rval = 0;
295 	}
296 #ifdef KTRACE
297 	if (KTRPOINT(td, KTR_CSW))
298 		ktrcsw(0, 0, wmesg);
299 #endif
300 	PICKUP_GIANT();
301 	mtx_lock_spin(mtx);
302 	WITNESS_RESTORE(&mtx->lock_object, mtx);
303 	return (rval);
304 }
305 
306 /*
307  * pause_sbt() delays the calling thread by the given signed binary
308  * time. During cold bootup, pause_sbt() uses the DELAY() function
309  * instead of the _sleep() function to do the waiting. The "sbt"
310  * argument must be greater than or equal to zero. A "sbt" value of
311  * zero is equivalent to a "sbt" value of one tick.
312  */
313 int
pause_sbt(const char * wmesg,sbintime_t sbt,sbintime_t pr,int flags)314 pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags)
315 {
316 	KASSERT(sbt >= 0, ("pause_sbt: timeout must be >= 0"));
317 
318 	/* silently convert invalid timeouts */
319 	if (sbt == 0)
320 		sbt = tick_sbt;
321 
322 	if ((cold && curthread == &thread0) || kdb_active ||
323 	    SCHEDULER_STOPPED()) {
324 		/*
325 		 * We delay one second at a time to avoid overflowing the
326 		 * system specific DELAY() function(s):
327 		 */
328 		while (sbt >= SBT_1S) {
329 			DELAY(1000000);
330 			sbt -= SBT_1S;
331 		}
332 		/* Do the delay remainder, if any */
333 		sbt = howmany(sbt, SBT_1US);
334 		if (sbt > 0)
335 			DELAY(sbt);
336 		return (EWOULDBLOCK);
337 	}
338 	return (_sleep(&pause_wchan[curcpu], NULL,
339 	    (flags & C_CATCH) ? PCATCH : 0, wmesg, sbt, pr, flags));
340 }
341 
342 /*
343  * Make all threads sleeping on the specified identifier runnable.
344  */
345 void
wakeup(const void * ident)346 wakeup(const void *ident)
347 {
348 	int wakeup_swapper;
349 
350 	sleepq_lock(ident);
351 	wakeup_swapper = sleepq_broadcast(ident, SLEEPQ_SLEEP, 0, 0);
352 	sleepq_release(ident);
353 	if (wakeup_swapper) {
354 		KASSERT(ident != &proc0,
355 		    ("wakeup and wakeup_swapper and proc0"));
356 		kick_proc0();
357 	}
358 }
359 
360 /*
361  * Make a thread sleeping on the specified identifier runnable.
362  * May wake more than one thread if a target thread is currently
363  * swapped out.
364  */
365 void
wakeup_one(const void * ident)366 wakeup_one(const void *ident)
367 {
368 	int wakeup_swapper;
369 
370 	sleepq_lock(ident);
371 	wakeup_swapper = sleepq_signal(ident, SLEEPQ_SLEEP | SLEEPQ_DROP, 0, 0);
372 	if (wakeup_swapper)
373 		kick_proc0();
374 }
375 
376 void
wakeup_any(const void * ident)377 wakeup_any(const void *ident)
378 {
379 	int wakeup_swapper;
380 
381 	sleepq_lock(ident);
382 	wakeup_swapper = sleepq_signal(ident, SLEEPQ_SLEEP | SLEEPQ_UNFAIR |
383 	    SLEEPQ_DROP, 0, 0);
384 	if (wakeup_swapper)
385 		kick_proc0();
386 }
387 
388 /*
389  * Signal sleeping waiters after the counter has reached zero.
390  */
391 void
_blockcount_wakeup(blockcount_t * bc,u_int old)392 _blockcount_wakeup(blockcount_t *bc, u_int old)
393 {
394 
395 	KASSERT(_BLOCKCOUNT_WAITERS(old),
396 	    ("%s: no waiters on %p", __func__, bc));
397 
398 	if (atomic_cmpset_int(&bc->__count, _BLOCKCOUNT_WAITERS_FLAG, 0))
399 		wakeup(bc);
400 }
401 
402 /*
403  * Wait for a wakeup or a signal.  This does not guarantee that the count is
404  * still zero on return.  Callers wanting a precise answer should use
405  * blockcount_wait() with an interlock.
406  *
407  * If there is no work to wait for, return 0.  If the sleep was interrupted by a
408  * signal, return EINTR or ERESTART, and return EAGAIN otherwise.
409  */
410 int
_blockcount_sleep(blockcount_t * bc,struct lock_object * lock,const char * wmesg,int prio)411 _blockcount_sleep(blockcount_t *bc, struct lock_object *lock, const char *wmesg,
412     int prio)
413 {
414 	void *wchan;
415 	uintptr_t lock_state;
416 	u_int old;
417 	int ret;
418 	bool catch, drop;
419 
420 	KASSERT(lock != &Giant.lock_object,
421 	    ("%s: cannot use Giant as the interlock", __func__));
422 
423 	catch = (prio & PCATCH) != 0;
424 	drop = (prio & PDROP) != 0;
425 	prio &= PRIMASK;
426 
427 	/*
428 	 * Synchronize with the fence in blockcount_release().  If we end up
429 	 * waiting, the sleepqueue lock acquisition will provide the required
430 	 * side effects.
431 	 *
432 	 * If there is no work to wait for, but waiters are present, try to put
433 	 * ourselves to sleep to avoid jumping ahead.
434 	 */
435 	if (atomic_load_acq_int(&bc->__count) == 0) {
436 		if (lock != NULL && drop)
437 			LOCK_CLASS(lock)->lc_unlock(lock);
438 		return (0);
439 	}
440 	lock_state = 0;
441 	wchan = bc;
442 	sleepq_lock(wchan);
443 	DROP_GIANT();
444 	if (lock != NULL)
445 		lock_state = LOCK_CLASS(lock)->lc_unlock(lock);
446 	old = blockcount_read(bc);
447 	ret = 0;
448 	do {
449 		if (_BLOCKCOUNT_COUNT(old) == 0) {
450 			sleepq_release(wchan);
451 			goto out;
452 		}
453 		if (_BLOCKCOUNT_WAITERS(old))
454 			break;
455 	} while (!atomic_fcmpset_int(&bc->__count, &old,
456 	    old | _BLOCKCOUNT_WAITERS_FLAG));
457 	sleepq_add(wchan, NULL, wmesg, catch ? SLEEPQ_INTERRUPTIBLE : 0, 0);
458 	if (catch)
459 		ret = sleepq_wait_sig(wchan, prio);
460 	else
461 		sleepq_wait(wchan, prio);
462 	if (ret == 0)
463 		ret = EAGAIN;
464 
465 out:
466 	PICKUP_GIANT();
467 	if (lock != NULL && !drop)
468 		LOCK_CLASS(lock)->lc_lock(lock, lock_state);
469 
470 	return (ret);
471 }
472 
473 static void
kdb_switch(void)474 kdb_switch(void)
475 {
476 	thread_unlock(curthread);
477 	kdb_backtrace();
478 	kdb_reenter();
479 	panic("%s: did not reenter debugger", __func__);
480 }
481 
482 /*
483  * The machine independent parts of context switching.
484  *
485  * The thread lock is required on entry and is no longer held on return.
486  */
487 void
mi_switch(int flags)488 mi_switch(int flags)
489 {
490 	uint64_t runtime, new_switchtime;
491 	struct thread *td;
492 
493 	td = curthread;			/* XXX */
494 	THREAD_LOCK_ASSERT(td, MA_OWNED | MA_NOTRECURSED);
495 	KASSERT(!TD_ON_RUNQ(td), ("mi_switch: called by old code"));
496 #ifdef INVARIANTS
497 	if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td))
498 		mtx_assert(&Giant, MA_NOTOWNED);
499 #endif
500 	KASSERT(td->td_critnest == 1 || KERNEL_PANICKED(),
501 		("mi_switch: switch in a critical section"));
502 	KASSERT((flags & (SW_INVOL | SW_VOL)) != 0,
503 	    ("mi_switch: switch must be voluntary or involuntary"));
504 
505 	/*
506 	 * Don't perform context switches from the debugger.
507 	 */
508 	if (kdb_active)
509 		kdb_switch();
510 	if (SCHEDULER_STOPPED_TD(td))
511 		return;
512 	if (flags & SW_VOL) {
513 		td->td_ru.ru_nvcsw++;
514 		td->td_swvoltick = ticks;
515 	} else {
516 		td->td_ru.ru_nivcsw++;
517 		td->td_swinvoltick = ticks;
518 	}
519 #ifdef SCHED_STATS
520 	SCHED_STAT_INC(sched_switch_stats[flags & SW_TYPE_MASK]);
521 #endif
522 	/*
523 	 * Compute the amount of time during which the current
524 	 * thread was running, and add that to its total so far.
525 	 */
526 	new_switchtime = cpu_ticks();
527 	runtime = new_switchtime - PCPU_GET(switchtime);
528 	td->td_runtime += runtime;
529 	td->td_incruntime += runtime;
530 	PCPU_SET(switchtime, new_switchtime);
531 	td->td_generation++;	/* bump preempt-detect counter */
532 	VM_CNT_INC(v_swtch);
533 	PCPU_SET(switchticks, ticks);
534 	CTR4(KTR_PROC, "mi_switch: old thread %ld (td_sched %p, pid %ld, %s)",
535 	    td->td_tid, td_get_sched(td), td->td_proc->p_pid, td->td_name);
536 #ifdef KDTRACE_HOOKS
537 	if (SDT_PROBES_ENABLED() &&
538 	    ((flags & SW_PREEMPT) != 0 || ((flags & SW_INVOL) != 0 &&
539 	    (flags & SW_TYPE_MASK) == SWT_NEEDRESCHED)))
540 		SDT_PROBE0(sched, , , preempt);
541 #endif
542 	sched_switch(td, flags);
543 	CTR4(KTR_PROC, "mi_switch: new thread %ld (td_sched %p, pid %ld, %s)",
544 	    td->td_tid, td_get_sched(td), td->td_proc->p_pid, td->td_name);
545 
546 	/*
547 	 * If the last thread was exiting, finish cleaning it up.
548 	 */
549 	if ((td = PCPU_GET(deadthread))) {
550 		PCPU_SET(deadthread, NULL);
551 		thread_stash(td);
552 	}
553 	spinlock_exit();
554 }
555 
556 /*
557  * Change thread state to be runnable, placing it on the run queue if
558  * it is in memory.  If it is swapped out, return true so our caller
559  * will know to awaken the swapper.
560  *
561  * Requires the thread lock on entry, drops on exit.
562  */
563 int
setrunnable(struct thread * td,int srqflags)564 setrunnable(struct thread *td, int srqflags)
565 {
566 	int swapin;
567 
568 	THREAD_LOCK_ASSERT(td, MA_OWNED);
569 	KASSERT(td->td_proc->p_state != PRS_ZOMBIE,
570 	    ("setrunnable: pid %d is a zombie", td->td_proc->p_pid));
571 
572 	swapin = 0;
573 	switch (td->td_state) {
574 	case TDS_RUNNING:
575 	case TDS_RUNQ:
576 		break;
577 	case TDS_CAN_RUN:
578 		KASSERT((td->td_flags & TDF_INMEM) != 0,
579 		    ("setrunnable: td %p not in mem, flags 0x%X inhibit 0x%X",
580 		    td, td->td_flags, td->td_inhibitors));
581 		/* unlocks thread lock according to flags */
582 		sched_wakeup(td, srqflags);
583 		return (0);
584 	case TDS_INHIBITED:
585 		/*
586 		 * If we are only inhibited because we are swapped out
587 		 * arrange to swap in this process.
588 		 */
589 		if (td->td_inhibitors == TDI_SWAPPED &&
590 		    (td->td_flags & TDF_SWAPINREQ) == 0) {
591 			td->td_flags |= TDF_SWAPINREQ;
592 			swapin = 1;
593 		}
594 		break;
595 	default:
596 		panic("setrunnable: state 0x%x", td->td_state);
597 	}
598 	if ((srqflags & (SRQ_HOLD | SRQ_HOLDTD)) == 0)
599 		thread_unlock(td);
600 
601 	return (swapin);
602 }
603 
604 /*
605  * Compute a tenex style load average of a quantity on
606  * 1, 5 and 15 minute intervals.
607  */
608 static void
loadav(void * arg)609 loadav(void *arg)
610 {
611 	int i;
612 	uint64_t nrun;
613 	struct loadavg *avg;
614 
615 	nrun = (uint64_t)sched_load();
616 	avg = &averunnable;
617 
618 	for (i = 0; i < 3; i++)
619 		avg->ldavg[i] = (cexp[i] * (uint64_t)avg->ldavg[i] +
620 		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
621 
622 	/*
623 	 * Schedule the next update to occur after 5 seconds, but add a
624 	 * random variation to avoid synchronisation with processes that
625 	 * run at regular intervals.
626 	 */
627 	callout_reset_sbt(&loadav_callout,
628 	    SBT_1US * (4000000 + (int)(random() % 2000001)), SBT_1US,
629 	    loadav, NULL, C_DIRECT_EXEC | C_PREL(32));
630 }
631 
632 /* ARGSUSED */
633 static void
synch_setup(void * dummy)634 synch_setup(void *dummy)
635 {
636 	callout_init(&loadav_callout, 1);
637 
638 	/* Kick off timeout driven events by calling first time. */
639 	loadav(NULL);
640 }
641 
642 bool
should_yield(void)643 should_yield(void)
644 {
645 
646 	return ((u_int)ticks - (u_int)curthread->td_swvoltick >= hogticks);
647 }
648 
649 void
maybe_yield(void)650 maybe_yield(void)
651 {
652 
653 	if (should_yield())
654 		kern_yield(PRI_USER);
655 }
656 
657 void
kern_yield(int prio)658 kern_yield(int prio)
659 {
660 	struct thread *td;
661 
662 	td = curthread;
663 	DROP_GIANT();
664 	thread_lock(td);
665 	if (prio == PRI_USER)
666 		prio = td->td_user_pri;
667 	if (prio >= 0)
668 		sched_prio(td, prio);
669 	mi_switch(SW_VOL | SWT_RELINQUISH);
670 	PICKUP_GIANT();
671 }
672 
673 /*
674  * General purpose yield system call.
675  */
676 int
sys_yield(struct thread * td,struct yield_args * uap)677 sys_yield(struct thread *td, struct yield_args *uap)
678 {
679 
680 	thread_lock(td);
681 	if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
682 		sched_prio(td, PRI_MAX_TIMESHARE);
683 	mi_switch(SW_VOL | SWT_RELINQUISH);
684 	td->td_retval[0] = 0;
685 	return (0);
686 }
687 
688 int
sys_sched_getcpu(struct thread * td,struct sched_getcpu_args * uap)689 sys_sched_getcpu(struct thread *td, struct sched_getcpu_args *uap)
690 {
691 	td->td_retval[0] = td->td_oncpu;
692 	return (0);
693 }
694