2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Implementation of sleep queues used to hold queue of threads blocked on
30 * a wait channel. Sleep queues are different from turnstiles in that wait
31 * channels are not owned by anyone, so there is no priority propagation.
32 * Sleep queues can also provide a timeout and can also be interrupted by
33 * signals. That said, there are several similarities between the turnstile
34 * and sleep queue implementations. (Note: turnstiles were implemented
35 * first.) For example, both use a hash table of the same size where each
36 * bucket is referred to as a "chain" that contains both a spin lock and
37 * a linked list of queues. An individual queue is located by using a hash
38 * to pick a chain, locking the chain, and then walking the chain searching
39 * for the queue. This means that a wait channel object does not need to
40 * embed its queue head just as locks do not embed their turnstile queue
41 * head. Threads also carry around a sleep queue that they lend to the
42 * wait channel when blocking. Just as in turnstiles, the queue includes
43 * a free list of the sleep queues of other threads blocked on the same
44 * wait channel in the case of multiple waiters.
46 * Some additional functionality provided by sleep queues include the
47 * ability to set a timeout. The timeout is managed using a per-thread
48 * callout that resumes a thread if it is asleep. A thread may also
49 * catch signals while it is asleep (aka an interruptible sleep). The
50 * signal code uses sleepq_abort() to interrupt a sleeping thread. Finally,
51 * sleep queues also provide some extra assertions. One is not allowed to
52 * mix the sleep/wakeup and cv APIs for a given wait channel. Also, one
53 * must consistently use the same lock to synchronize with a wait channel,
54 * though this check is currently only a warning for sleep/wakeup due to
55 * pre-existing abuse of that API. The same lock must also be held when
56 * awakening threads, though that is currently only enforced for condition
60 #include <sys/cdefs.h>
61 __FBSDID("$FreeBSD$");
63 #include "opt_sleepqueue_profiling.h"
65 #include "opt_sched.h"
66 #include "opt_stack.h"
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/kernel.h>
73 #include <sys/mutex.h>
76 #include <sys/sched.h>
78 #include <sys/signalvar.h>
79 #include <sys/sleepqueue.h>
80 #include <sys/stack.h>
81 #include <sys/sysctl.h>
84 #include <sys/epoch.h>
87 #include <machine/atomic.h>
96 * Constants for the hash table of sleep queue chains.
97 * SC_TABLESIZE must be a power of two for SC_MASK to work properly.
100 #define SC_TABLESIZE 256
102 CTASSERT(powerof2(SC_TABLESIZE));
103 #define SC_MASK (SC_TABLESIZE - 1)
105 #define SC_HASH(wc) ((((uintptr_t)(wc) >> SC_SHIFT) ^ (uintptr_t)(wc)) & \
107 #define SC_LOOKUP(wc) &sleepq_chains[SC_HASH(wc)]
110 * There are two different lists of sleep queues. Both lists are connected
111 * via the sq_hash entries. The first list is the sleep queue chain list
112 * that a sleep queue is on when it is attached to a wait channel. The
113 * second list is the free list hung off of a sleep queue that is attached
116 * Each sleep queue also contains the wait channel it is attached to, the
117 * list of threads blocked on that wait channel, flags specific to the
118 * wait channel, and the lock used to synchronize with a wait channel.
119 * The flags are used to catch mismatches between the various consumers
120 * of the sleep queue API (e.g. sleep/wakeup and condition variables).
121 * The lock pointer is only used when invariants are enabled for various
125 * c - sleep queue chain lock
128 struct threadqueue sq_blocked[NR_SLEEPQS]; /* (c) Blocked threads. */
129 u_int sq_blockedcnt[NR_SLEEPQS]; /* (c) N. of blocked threads. */
130 LIST_ENTRY(sleepqueue) sq_hash; /* (c) Chain and free list. */
131 LIST_HEAD(, sleepqueue) sq_free; /* (c) Free queues. */
132 const void *sq_wchan; /* (c) Wait channel. */
133 int sq_type; /* (c) Queue type. */
135 struct lock_object *sq_lock; /* (c) Associated lock. */
139 struct sleepqueue_chain {
140 LIST_HEAD(, sleepqueue) sc_queues; /* List of sleep queues. */
141 struct mtx sc_lock; /* Spin lock for this chain. */
142 #ifdef SLEEPQUEUE_PROFILING
143 u_int sc_depth; /* Length of sc_queues. */
144 u_int sc_max_depth; /* Max length of sc_queues. */
146 } __aligned(CACHE_LINE_SIZE);
148 #ifdef SLEEPQUEUE_PROFILING
149 u_int sleepq_max_depth;
150 static SYSCTL_NODE(_debug, OID_AUTO, sleepq, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
152 static SYSCTL_NODE(_debug_sleepq, OID_AUTO, chains,
153 CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
154 "sleepq chain stats");
155 SYSCTL_UINT(_debug_sleepq, OID_AUTO, max_depth, CTLFLAG_RD, &sleepq_max_depth,
156 0, "maxmimum depth achieved of a single chain");
158 static void sleepq_profile(const char *wmesg);
159 static int prof_enabled;
161 static struct sleepqueue_chain sleepq_chains[SC_TABLESIZE];
162 static uma_zone_t sleepq_zone;
165 * Prototypes for non-exported routines.
167 static int sleepq_catch_signals(const void *wchan, int pri);
168 static inline int sleepq_check_signals(void);
169 static inline int sleepq_check_timeout(void);
171 static void sleepq_dtor(void *mem, int size, void *arg);
173 static int sleepq_init(void *mem, int size, int flags);
174 static int sleepq_resume_thread(struct sleepqueue *sq, struct thread *td,
175 int pri, int srqflags);
176 static void sleepq_remove_thread(struct sleepqueue *sq, struct thread *td);
177 static void sleepq_switch(const void *wchan, int pri);
178 static void sleepq_timeout(void *arg);
180 SDT_PROBE_DECLARE(sched, , , sleep);
181 SDT_PROBE_DECLARE(sched, , , wakeup);
184 * Initialize SLEEPQUEUE_PROFILING specific sysctl nodes.
185 * Note that it must happen after sleepinit() has been fully executed, so
186 * it must happen after SI_SUB_KMEM SYSINIT() subsystem setup.
188 #ifdef SLEEPQUEUE_PROFILING
190 init_sleepqueue_profiling(void)
193 struct sysctl_oid *chain_oid;
196 for (i = 0; i < SC_TABLESIZE; i++) {
197 snprintf(chain_name, sizeof(chain_name), "%u", i);
198 chain_oid = SYSCTL_ADD_NODE(NULL,
199 SYSCTL_STATIC_CHILDREN(_debug_sleepq_chains), OID_AUTO,
200 chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
201 "sleepq chain stats");
202 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
203 "depth", CTLFLAG_RD, &sleepq_chains[i].sc_depth, 0, NULL);
204 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
205 "max_depth", CTLFLAG_RD, &sleepq_chains[i].sc_max_depth, 0,
210 SYSINIT(sleepqueue_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
211 init_sleepqueue_profiling, NULL);
215 * Early initialization of sleep queues that is called from the sleepinit()
219 init_sleepqueues(void)
223 for (i = 0; i < SC_TABLESIZE; i++) {
224 LIST_INIT(&sleepq_chains[i].sc_queues);
225 mtx_init(&sleepq_chains[i].sc_lock, "sleepq chain", NULL,
228 sleepq_zone = uma_zcreate("SLEEPQUEUE", sizeof(struct sleepqueue),
230 NULL, sleepq_dtor, sleepq_init, NULL, UMA_ALIGN_CACHE, 0);
232 NULL, NULL, sleepq_init, NULL, UMA_ALIGN_CACHE, 0);
235 thread0.td_sleepqueue = sleepq_alloc();
239 * Get a sleep queue for a new thread.
245 return (uma_zalloc(sleepq_zone, M_WAITOK));
249 * Free a sleep queue when a thread is destroyed.
252 sleepq_free(struct sleepqueue *sq)
255 uma_zfree(sleepq_zone, sq);
259 * Lock the sleep queue chain associated with the specified wait channel.
262 sleepq_lock(const void *wchan)
264 struct sleepqueue_chain *sc;
266 sc = SC_LOOKUP(wchan);
267 mtx_lock_spin(&sc->sc_lock);
271 * Look up the sleep queue associated with a given wait channel in the hash
272 * table locking the associated sleep queue chain. If no queue is found in
273 * the table, NULL is returned.
276 sleepq_lookup(const void *wchan)
278 struct sleepqueue_chain *sc;
279 struct sleepqueue *sq;
281 KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
282 sc = SC_LOOKUP(wchan);
283 mtx_assert(&sc->sc_lock, MA_OWNED);
284 LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
285 if (sq->sq_wchan == wchan)
291 * Unlock the sleep queue chain associated with a given wait channel.
294 sleepq_release(const void *wchan)
296 struct sleepqueue_chain *sc;
298 sc = SC_LOOKUP(wchan);
299 mtx_unlock_spin(&sc->sc_lock);
303 * Places the current thread on the sleep queue for the specified wait
304 * channel. If INVARIANTS is enabled, then it associates the passed in
305 * lock with the sleepq to make sure it is held when that sleep queue is
309 sleepq_add(const void *wchan, struct lock_object *lock, const char *wmesg,
310 int flags, int queue)
312 struct sleepqueue_chain *sc;
313 struct sleepqueue *sq;
317 sc = SC_LOOKUP(wchan);
318 mtx_assert(&sc->sc_lock, MA_OWNED);
319 MPASS(td->td_sleepqueue != NULL);
320 MPASS(wchan != NULL);
321 MPASS((queue >= 0) && (queue < NR_SLEEPQS));
323 /* If this thread is not allowed to sleep, die a horrible death. */
324 if (__predict_false(!THREAD_CAN_SLEEP())) {
326 epoch_trace_list(curthread);
329 ("%s: td %p to sleep on wchan %p with sleeping prohibited",
330 __func__, td, wchan));
333 /* Look up the sleep queue associated with the wait channel 'wchan'. */
334 sq = sleepq_lookup(wchan);
337 * If the wait channel does not already have a sleep queue, use
338 * this thread's sleep queue. Otherwise, insert the current thread
339 * into the sleep queue already in use by this wait channel.
345 sq = td->td_sleepqueue;
346 for (i = 0; i < NR_SLEEPQS; i++) {
347 KASSERT(TAILQ_EMPTY(&sq->sq_blocked[i]),
348 ("thread's sleep queue %d is not empty", i));
349 KASSERT(sq->sq_blockedcnt[i] == 0,
350 ("thread's sleep queue %d count mismatches", i));
352 KASSERT(LIST_EMPTY(&sq->sq_free),
353 ("thread's sleep queue has a non-empty free list"));
354 KASSERT(sq->sq_wchan == NULL, ("stale sq_wchan pointer"));
357 #ifdef SLEEPQUEUE_PROFILING
359 if (sc->sc_depth > sc->sc_max_depth) {
360 sc->sc_max_depth = sc->sc_depth;
361 if (sc->sc_max_depth > sleepq_max_depth)
362 sleepq_max_depth = sc->sc_max_depth;
365 sq = td->td_sleepqueue;
366 LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
367 sq->sq_wchan = wchan;
368 sq->sq_type = flags & SLEEPQ_TYPE;
370 MPASS(wchan == sq->sq_wchan);
371 MPASS(lock == sq->sq_lock);
372 MPASS((flags & SLEEPQ_TYPE) == sq->sq_type);
373 LIST_INSERT_HEAD(&sq->sq_free, td->td_sleepqueue, sq_hash);
376 TAILQ_INSERT_TAIL(&sq->sq_blocked[queue], td, td_slpq);
377 sq->sq_blockedcnt[queue]++;
378 td->td_sleepqueue = NULL;
379 td->td_sqqueue = queue;
380 td->td_wchan = wchan;
381 td->td_wmesg = wmesg;
382 if (flags & SLEEPQ_INTERRUPTIBLE) {
384 td->td_flags |= TDF_SINTR;
386 td->td_flags &= ~TDF_TIMEOUT;
391 * Sets a timeout that will remove the current thread from the specified
392 * sleep queue after timo ticks if the thread has not already been awakened.
395 sleepq_set_timeout_sbt(const void *wchan, sbintime_t sbt, sbintime_t pr,
398 struct sleepqueue_chain *sc __unused;
403 sc = SC_LOOKUP(wchan);
404 mtx_assert(&sc->sc_lock, MA_OWNED);
405 MPASS(TD_ON_SLEEPQ(td));
406 MPASS(td->td_sleepqueue == NULL);
407 MPASS(wchan != NULL);
408 if (cold && td == &thread0)
409 panic("timed sleep before timers are working");
410 KASSERT(td->td_sleeptimo == 0, ("td %d %p td_sleeptimo %jx",
411 td->td_tid, td, (uintmax_t)td->td_sleeptimo));
413 callout_when(sbt, pr, flags, &td->td_sleeptimo, &pr1);
415 callout_reset_sbt_on(&td->td_slpcallout, td->td_sleeptimo, pr1,
416 sleepq_timeout, td, PCPU_GET(cpuid), flags | C_PRECALC |
421 * Return the number of actual sleepers for the specified queue.
424 sleepq_sleepcnt(const void *wchan, int queue)
426 struct sleepqueue *sq;
428 KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
429 MPASS((queue >= 0) && (queue < NR_SLEEPQS));
430 sq = sleepq_lookup(wchan);
433 return (sq->sq_blockedcnt[queue]);
437 sleepq_check_ast_sc_locked(struct thread *td, struct sleepqueue_chain *sc)
442 mtx_assert(&sc->sc_lock, MA_OWNED);
444 if ((td->td_pflags & TDP_WAKEUP) != 0) {
445 td->td_pflags &= ~TDP_WAKEUP;
451 * See if there are any pending signals or suspension requests for this
452 * thread. If not, we can switch immediately.
455 if ((td->td_flags & (TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK)) == 0)
459 mtx_unlock_spin(&sc->sc_lock);
462 CTR3(KTR_PROC, "sleepq catching signals: thread %p (pid %ld, %s)",
463 (void *)td, (long)p->p_pid, td->td_name);
467 * Check for suspension first. Checking for signals and then
468 * suspending could result in a missed signal, since a signal
469 * can be delivered while this thread is suspended.
471 ret = sig_ast_checksusp(td);
474 mtx_lock_spin(&sc->sc_lock);
479 ret = sig_ast_needsigchk(td);
482 * Lock the per-process spinlock prior to dropping the
483 * PROC_LOCK to avoid a signal delivery race.
484 * PROC_LOCK, PROC_SLOCK, and thread_lock() are
485 * currently held in tdsendsignal().
488 mtx_lock_spin(&sc->sc_lock);
497 * Marks the pending sleep of the current thread as interruptible and
498 * makes an initial check for pending signals before putting a thread
499 * to sleep. Enters and exits with the thread lock held. Thread lock
500 * may have transitioned from the sleepq lock to a run lock.
503 sleepq_catch_signals(const void *wchan, int pri)
506 struct sleepqueue_chain *sc;
507 struct sleepqueue *sq;
510 sc = SC_LOOKUP(wchan);
511 mtx_assert(&sc->sc_lock, MA_OWNED);
512 MPASS(wchan != NULL);
515 ret = sleepq_check_ast_sc_locked(td, sc);
516 THREAD_LOCK_ASSERT(td, MA_OWNED);
517 mtx_assert(&sc->sc_lock, MA_OWNED);
521 * No pending signals and no suspension requests found.
522 * Switch the thread off the cpu.
524 sleepq_switch(wchan, pri);
527 * There were pending signals and this thread is still
528 * on the sleep queue, remove it from the sleep queue.
530 if (TD_ON_SLEEPQ(td)) {
531 sq = sleepq_lookup(wchan);
532 sleepq_remove_thread(sq, td);
534 MPASS(td->td_lock != &sc->sc_lock);
535 mtx_unlock_spin(&sc->sc_lock);
542 * Switches to another thread if we are still asleep on a sleep queue.
543 * Returns with thread lock.
546 sleepq_switch(const void *wchan, int pri)
548 struct sleepqueue_chain *sc;
549 struct sleepqueue *sq;
554 sc = SC_LOOKUP(wchan);
555 mtx_assert(&sc->sc_lock, MA_OWNED);
556 THREAD_LOCK_ASSERT(td, MA_OWNED);
559 * If we have a sleep queue, then we've already been woken up, so
562 if (td->td_sleepqueue != NULL) {
563 mtx_unlock_spin(&sc->sc_lock);
569 * If TDF_TIMEOUT is set, then our sleep has been timed out
570 * already but we are still on the sleep queue, so dequeue the
573 * Do the same if the real-time clock has been adjusted since this
574 * thread calculated its timeout based on that clock. This handles
575 * the following race:
576 * - The Ts thread needs to sleep until an absolute real-clock time.
577 * It copies the global rtc_generation into curthread->td_rtcgen,
578 * reads the RTC, and calculates a sleep duration based on that time.
579 * See umtxq_sleep() for an example.
580 * - The Tc thread adjusts the RTC, bumps rtc_generation, and wakes
581 * threads that are sleeping until an absolute real-clock time.
582 * See tc_setclock() and the POSIX specification of clock_settime().
583 * - Ts reaches the code below. It holds the sleepqueue chain lock,
584 * so Tc has finished waking, so this thread must test td_rtcgen.
585 * (The declaration of td_rtcgen refers to this comment.)
587 rtc_changed = td->td_rtcgen != 0 && td->td_rtcgen != rtc_generation;
588 if ((td->td_flags & TDF_TIMEOUT) || rtc_changed) {
592 MPASS(TD_ON_SLEEPQ(td));
593 sq = sleepq_lookup(wchan);
594 sleepq_remove_thread(sq, td);
595 mtx_unlock_spin(&sc->sc_lock);
599 #ifdef SLEEPQUEUE_PROFILING
601 sleepq_profile(td->td_wmesg);
603 MPASS(td->td_sleepqueue == NULL);
604 sched_sleep(td, pri);
605 thread_lock_set(td, &sc->sc_lock);
606 SDT_PROBE0(sched, , , sleep);
608 mi_switch(SW_VOL | SWT_SLEEPQ);
609 KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING"));
610 CTR3(KTR_PROC, "sleepq resume: thread %p (pid %ld, %s)",
611 (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
615 * Check to see if we timed out.
618 sleepq_check_timeout(void)
625 if (td->td_sleeptimo != 0) {
626 if (td->td_sleeptimo <= sbinuptime())
628 td->td_sleeptimo = 0;
634 * Check to see if we were awoken by a signal.
637 sleepq_check_signals(void)
642 KASSERT((td->td_flags & TDF_SINTR) == 0,
643 ("thread %p still in interruptible sleep?", td));
645 return (td->td_intrval);
649 * Block the current thread until it is awakened from its sleep queue.
652 sleepq_wait(const void *wchan, int pri)
657 MPASS(!(td->td_flags & TDF_SINTR));
659 sleepq_switch(wchan, pri);
663 * Block the current thread until it is awakened from its sleep queue
664 * or it is interrupted by a signal.
667 sleepq_wait_sig(const void *wchan, int pri)
671 rcatch = sleepq_catch_signals(wchan, pri);
674 return (sleepq_check_signals());
678 * Block the current thread until it is awakened from its sleep queue
679 * or it times out while waiting.
682 sleepq_timedwait(const void *wchan, int pri)
687 MPASS(!(td->td_flags & TDF_SINTR));
690 sleepq_switch(wchan, pri);
692 return (sleepq_check_timeout());
696 * Block the current thread until it is awakened from its sleep queue,
697 * it is interrupted by a signal, or it times out waiting to be awakened.
700 sleepq_timedwait_sig(const void *wchan, int pri)
702 int rcatch, rvalt, rvals;
704 rcatch = sleepq_catch_signals(wchan, pri);
705 /* We must always call check_timeout() to clear sleeptimo. */
706 rvalt = sleepq_check_timeout();
707 rvals = sleepq_check_signals();
716 * Returns the type of sleepqueue given a waitchannel.
719 sleepq_type(const void *wchan)
721 struct sleepqueue *sq;
724 MPASS(wchan != NULL);
726 sq = sleepq_lookup(wchan);
735 * Removes a thread from a sleep queue and makes it
738 * Requires the sc chain locked on entry. If SRQ_HOLD is specified it will
739 * be locked on return. Returns without the thread lock held.
742 sleepq_resume_thread(struct sleepqueue *sq, struct thread *td, int pri,
745 struct sleepqueue_chain *sc;
749 MPASS(sq->sq_wchan != NULL);
750 MPASS(td->td_wchan == sq->sq_wchan);
752 sc = SC_LOOKUP(sq->sq_wchan);
753 mtx_assert(&sc->sc_lock, MA_OWNED);
756 * Avoid recursing on the chain lock. If the locks don't match we
757 * need to acquire the thread lock which setrunnable will drop for
758 * us. In this case we need to drop the chain lock afterwards.
760 * There is no race that will make td_lock equal to sc_lock because
764 if (!TD_IS_SLEEPING(td)) {
768 thread_lock_block_wait(td);
770 /* Remove thread from the sleepq. */
771 sleepq_remove_thread(sq, td);
773 /* If we're done with the sleepqueue release it. */
774 if ((srqflags & SRQ_HOLD) == 0 && drop)
775 mtx_unlock_spin(&sc->sc_lock);
777 /* Adjust priority if requested. */
778 MPASS(pri == 0 || (pri >= PRI_MIN && pri <= PRI_MAX));
779 if (pri != 0 && td->td_priority > pri &&
780 PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
784 * Note that thread td might not be sleeping if it is running
785 * sleepq_catch_signals() on another CPU or is blocked on its
786 * proc lock to check signals. There's no need to mark the
787 * thread runnable in that case.
789 if (TD_IS_SLEEPING(td)) {
792 return (setrunnable(td, srqflags));
801 sleepq_remove_thread(struct sleepqueue *sq, struct thread *td)
803 struct sleepqueue_chain *sc __unused;
806 MPASS(sq->sq_wchan != NULL);
807 MPASS(td->td_wchan == sq->sq_wchan);
808 MPASS(td->td_sqqueue < NR_SLEEPQS && td->td_sqqueue >= 0);
809 THREAD_LOCK_ASSERT(td, MA_OWNED);
810 sc = SC_LOOKUP(sq->sq_wchan);
811 mtx_assert(&sc->sc_lock, MA_OWNED);
813 SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
815 /* Remove the thread from the queue. */
816 sq->sq_blockedcnt[td->td_sqqueue]--;
817 TAILQ_REMOVE(&sq->sq_blocked[td->td_sqqueue], td, td_slpq);
820 * Get a sleep queue for this thread. If this is the last waiter,
821 * use the queue itself and take it out of the chain, otherwise,
822 * remove a queue from the free list.
824 if (LIST_EMPTY(&sq->sq_free)) {
825 td->td_sleepqueue = sq;
829 #ifdef SLEEPQUEUE_PROFILING
833 td->td_sleepqueue = LIST_FIRST(&sq->sq_free);
834 LIST_REMOVE(td->td_sleepqueue, sq_hash);
836 if ((td->td_flags & TDF_TIMEOUT) == 0 && td->td_sleeptimo != 0)
838 * We ignore the situation where timeout subsystem was
839 * unable to stop our callout. The struct thread is
840 * type-stable, the callout will use the correct
841 * memory when running. The checks of the
842 * td_sleeptimo value in this function and in
843 * sleepq_timeout() ensure that the thread does not
844 * get spurious wakeups, even if the callout was reset
847 callout_stop(&td->td_slpcallout);
851 td->td_flags &= ~(TDF_SINTR | TDF_TIMEOUT);
853 CTR3(KTR_PROC, "sleepq_wakeup: thread %p (pid %ld, %s)",
854 (void *)td, (long)td->td_proc->p_pid, td->td_name);
858 sleepq_remove_nested(struct thread *td)
860 struct sleepqueue_chain *sc;
861 struct sleepqueue *sq;
864 MPASS(TD_ON_SLEEPQ(td));
866 wchan = td->td_wchan;
867 sc = SC_LOOKUP(wchan);
868 mtx_lock_spin(&sc->sc_lock);
869 sq = sleepq_lookup(wchan);
872 sleepq_remove_thread(sq, td);
873 mtx_unlock_spin(&sc->sc_lock);
874 /* Returns with the thread lock owned. */
879 * UMA zone item deallocator.
882 sleepq_dtor(void *mem, int size, void *arg)
884 struct sleepqueue *sq;
888 for (i = 0; i < NR_SLEEPQS; i++) {
889 MPASS(TAILQ_EMPTY(&sq->sq_blocked[i]));
890 MPASS(sq->sq_blockedcnt[i] == 0);
896 * UMA zone item initializer.
899 sleepq_init(void *mem, int size, int flags)
901 struct sleepqueue *sq;
906 for (i = 0; i < NR_SLEEPQS; i++) {
907 TAILQ_INIT(&sq->sq_blocked[i]);
908 sq->sq_blockedcnt[i] = 0;
910 LIST_INIT(&sq->sq_free);
915 * Find thread sleeping on a wait channel and resume it.
918 sleepq_signal(const void *wchan, int flags, int pri, int queue)
920 struct sleepqueue_chain *sc;
921 struct sleepqueue *sq;
922 struct threadqueue *head;
923 struct thread *td, *besttd;
926 CTR2(KTR_PROC, "sleepq_signal(%p, %d)", wchan, flags);
927 KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
928 MPASS((queue >= 0) && (queue < NR_SLEEPQS));
929 sq = sleepq_lookup(wchan);
931 if (flags & SLEEPQ_DROP)
932 sleepq_release(wchan);
935 KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
936 ("%s: mismatch between sleep/wakeup and cv_*", __func__));
938 head = &sq->sq_blocked[queue];
939 if (flags & SLEEPQ_UNFAIR) {
941 * Find the most recently sleeping thread, but try to
942 * skip threads still in process of context switch to
943 * avoid spinning on the thread lock.
945 sc = SC_LOOKUP(wchan);
946 besttd = TAILQ_LAST_FAST(head, thread, td_slpq);
947 while (besttd->td_lock != &sc->sc_lock) {
948 td = TAILQ_PREV_FAST(besttd, head, thread, td_slpq);
955 * Find the highest priority thread on the queue. If there
956 * is a tie, use the thread that first appears in the queue
957 * as it has been sleeping the longest since threads are
958 * always added to the tail of sleep queues.
960 besttd = td = TAILQ_FIRST(head);
961 while ((td = TAILQ_NEXT(td, td_slpq)) != NULL) {
962 if (td->td_priority < besttd->td_priority)
966 MPASS(besttd != NULL);
967 wakeup_swapper = sleepq_resume_thread(sq, besttd, pri,
968 (flags & SLEEPQ_DROP) ? 0 : SRQ_HOLD);
969 return (wakeup_swapper);
973 match_any(struct thread *td __unused)
980 * Resume all threads sleeping on a specified wait channel.
983 sleepq_broadcast(const void *wchan, int flags, int pri, int queue)
985 struct sleepqueue *sq;
987 CTR2(KTR_PROC, "sleepq_broadcast(%p, %d)", wchan, flags);
988 KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
989 MPASS((queue >= 0) && (queue < NR_SLEEPQS));
990 sq = sleepq_lookup(wchan);
993 KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
994 ("%s: mismatch between sleep/wakeup and cv_*", __func__));
996 return (sleepq_remove_matching(sq, queue, match_any, pri));
1000 * Resume threads on the sleep queue that match the given predicate.
1003 sleepq_remove_matching(struct sleepqueue *sq, int queue,
1004 bool (*matches)(struct thread *), int pri)
1006 struct thread *td, *tdn;
1010 * The last thread will be given ownership of sq and may
1011 * re-enqueue itself before sleepq_resume_thread() returns,
1012 * so we must cache the "next" queue item at the beginning
1013 * of the final iteration.
1016 TAILQ_FOREACH_SAFE(td, &sq->sq_blocked[queue], td_slpq, tdn) {
1018 wakeup_swapper |= sleepq_resume_thread(sq, td, pri,
1022 return (wakeup_swapper);
1026 * Time sleeping threads out. When the timeout expires, the thread is
1027 * removed from the sleep queue and made runnable if it is still asleep.
1030 sleepq_timeout(void *arg)
1032 struct sleepqueue_chain *sc __unused;
1033 struct sleepqueue *sq;
1039 CTR3(KTR_PROC, "sleepq_timeout: thread %p (pid %ld, %s)",
1040 (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
1043 if (td->td_sleeptimo == 0 ||
1044 td->td_sleeptimo > td->td_slpcallout.c_time) {
1046 * The thread does not want a timeout (yet).
1048 } else if (TD_IS_SLEEPING(td) && TD_ON_SLEEPQ(td)) {
1050 * See if the thread is asleep and get the wait
1053 wchan = td->td_wchan;
1054 sc = SC_LOOKUP(wchan);
1055 THREAD_LOCKPTR_ASSERT(td, &sc->sc_lock);
1056 sq = sleepq_lookup(wchan);
1058 td->td_flags |= TDF_TIMEOUT;
1059 wakeup_swapper = sleepq_resume_thread(sq, td, 0, 0);
1063 } else if (TD_ON_SLEEPQ(td)) {
1065 * If the thread is on the SLEEPQ but isn't sleeping
1066 * yet, it can either be on another CPU in between
1067 * sleepq_add() and one of the sleepq_*wait*()
1068 * routines or it can be in sleepq_catch_signals().
1070 td->td_flags |= TDF_TIMEOUT;
1076 * Resumes a specific thread from the sleep queue associated with a specific
1077 * wait channel if it is on that queue.
1080 sleepq_remove(struct thread *td, const void *wchan)
1082 struct sleepqueue_chain *sc;
1083 struct sleepqueue *sq;
1087 * Look up the sleep queue for this wait channel, then re-check
1088 * that the thread is asleep on that channel, if it is not, then
1091 MPASS(wchan != NULL);
1092 sc = SC_LOOKUP(wchan);
1093 mtx_lock_spin(&sc->sc_lock);
1095 * We can not lock the thread here as it may be sleeping on a
1096 * different sleepq. However, holding the sleepq lock for this
1097 * wchan can guarantee that we do not miss a wakeup for this
1098 * channel. The asserts below will catch any false positives.
1100 if (!TD_ON_SLEEPQ(td) || td->td_wchan != wchan) {
1101 mtx_unlock_spin(&sc->sc_lock);
1105 /* Thread is asleep on sleep queue sq, so wake it up. */
1106 sq = sleepq_lookup(wchan);
1108 MPASS(td->td_wchan == wchan);
1109 wakeup_swapper = sleepq_resume_thread(sq, td, 0, 0);
1115 * Abort a thread as if an interrupt had occurred. Only abort
1116 * interruptible waits (unfortunately it isn't safe to abort others).
1118 * Requires thread lock on entry, releases on return.
1121 sleepq_abort(struct thread *td, int intrval)
1123 struct sleepqueue *sq;
1126 THREAD_LOCK_ASSERT(td, MA_OWNED);
1127 MPASS(TD_ON_SLEEPQ(td));
1128 MPASS(td->td_flags & TDF_SINTR);
1129 MPASS((intrval == 0 && (td->td_flags & TDF_SIGWAIT) != 0) ||
1130 intrval == EINTR || intrval == ERESTART);
1133 * If the TDF_TIMEOUT flag is set, just leave. A
1134 * timeout is scheduled anyhow.
1136 if (td->td_flags & TDF_TIMEOUT) {
1141 CTR3(KTR_PROC, "sleepq_abort: thread %p (pid %ld, %s)",
1142 (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
1143 td->td_intrval = intrval;
1146 * If the thread has not slept yet it will find the signal in
1147 * sleepq_catch_signals() and call sleepq_resume_thread. Otherwise
1148 * we have to do it here.
1150 if (!TD_IS_SLEEPING(td)) {
1154 wchan = td->td_wchan;
1155 MPASS(wchan != NULL);
1156 sq = sleepq_lookup(wchan);
1159 /* Thread is asleep on sleep queue sq, so wake it up. */
1160 return (sleepq_resume_thread(sq, td, 0, 0));
1164 sleepq_chains_remove_matching(bool (*matches)(struct thread *))
1166 struct sleepqueue_chain *sc;
1167 struct sleepqueue *sq, *sq1;
1168 int i, wakeup_swapper;
1171 for (sc = &sleepq_chains[0]; sc < sleepq_chains + SC_TABLESIZE; ++sc) {
1172 if (LIST_EMPTY(&sc->sc_queues)) {
1175 mtx_lock_spin(&sc->sc_lock);
1176 LIST_FOREACH_SAFE(sq, &sc->sc_queues, sq_hash, sq1) {
1177 for (i = 0; i < NR_SLEEPQS; ++i) {
1178 wakeup_swapper |= sleepq_remove_matching(sq, i,
1182 mtx_unlock_spin(&sc->sc_lock);
1184 if (wakeup_swapper) {
1190 * Prints the stacks of all threads presently sleeping on wchan/queue to
1191 * the sbuf sb. Sets count_stacks_printed to the number of stacks actually
1192 * printed. Typically, this will equal the number of threads sleeping on the
1193 * queue, but may be less if sb overflowed before all stacks were printed.
1197 sleepq_sbuf_print_stacks(struct sbuf *sb, const void *wchan, int queue,
1198 int *count_stacks_printed)
1200 struct thread *td, *td_next;
1201 struct sleepqueue *sq;
1203 struct sbuf **td_infos;
1204 int i, stack_idx, error, stacks_to_allocate;
1210 KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
1211 MPASS((queue >= 0) && (queue < NR_SLEEPQS));
1213 stacks_to_allocate = 10;
1214 for (i = 0; i < 3 && !finished ; i++) {
1215 /* We cannot malloc while holding the queue's spinlock, so
1216 * we do our mallocs now, and hope it is enough. If it
1217 * isn't, we will free these, drop the lock, malloc more,
1218 * and try again, up to a point. After that point we will
1219 * give up and report ENOMEM. We also cannot write to sb
1220 * during this time since the client may have set the
1221 * SBUF_AUTOEXTEND flag on their sbuf, which could cause a
1222 * malloc as we print to it. So we defer actually printing
1223 * to sb until after we drop the spinlock.
1226 /* Where we will store the stacks. */
1227 st = malloc(sizeof(struct stack *) * stacks_to_allocate,
1229 for (stack_idx = 0; stack_idx < stacks_to_allocate;
1231 st[stack_idx] = stack_create(M_WAITOK);
1233 /* Where we will store the td name, tid, etc. */
1234 td_infos = malloc(sizeof(struct sbuf *) * stacks_to_allocate,
1236 for (stack_idx = 0; stack_idx < stacks_to_allocate;
1238 td_infos[stack_idx] = sbuf_new(NULL, NULL,
1239 MAXCOMLEN + sizeof(struct thread *) * 2 + 40,
1243 sq = sleepq_lookup(wchan);
1245 /* This sleepq does not exist; exit and return ENOENT. */
1248 sleepq_release(wchan);
1253 /* Save thread info */
1254 TAILQ_FOREACH_SAFE(td, &sq->sq_blocked[queue], td_slpq,
1256 if (stack_idx >= stacks_to_allocate)
1259 /* Note the td_lock is equal to the sleepq_lock here. */
1260 (void)stack_save_td(st[stack_idx], td);
1262 sbuf_printf(td_infos[stack_idx], "%d: %s %p",
1263 td->td_tid, td->td_name, td);
1269 sleepq_release(wchan);
1271 /* Print the stacks */
1272 for (i = 0; i < stack_idx; i++) {
1273 sbuf_finish(td_infos[i]);
1274 sbuf_printf(sb, "--- thread %s: ---\n", sbuf_data(td_infos[i]));
1275 stack_sbuf_print(sb, st[i]);
1276 sbuf_printf(sb, "\n");
1278 error = sbuf_error(sb);
1280 *count_stacks_printed = stack_idx;
1285 sleepq_release(wchan);
1286 for (stack_idx = 0; stack_idx < stacks_to_allocate;
1288 stack_destroy(st[stack_idx]);
1289 for (stack_idx = 0; stack_idx < stacks_to_allocate;
1291 sbuf_delete(td_infos[stack_idx]);
1293 free(td_infos, M_TEMP);
1294 stacks_to_allocate *= 10;
1297 if (!finished && error == 0)
1304 #ifdef SLEEPQUEUE_PROFILING
1305 #define SLEEPQ_PROF_LOCATIONS 1024
1306 #define SLEEPQ_SBUFSIZE 512
1307 struct sleepq_prof {
1308 LIST_ENTRY(sleepq_prof) sp_link;
1309 const char *sp_wmesg;
1313 LIST_HEAD(sqphead, sleepq_prof);
1315 struct sqphead sleepq_prof_free;
1316 struct sqphead sleepq_hash[SC_TABLESIZE];
1317 static struct sleepq_prof sleepq_profent[SLEEPQ_PROF_LOCATIONS];
1318 static struct mtx sleepq_prof_lock;
1319 MTX_SYSINIT(sleepq_prof_lock, &sleepq_prof_lock, "sleepq_prof", MTX_SPIN);
1322 sleepq_profile(const char *wmesg)
1324 struct sleepq_prof *sp;
1326 mtx_lock_spin(&sleepq_prof_lock);
1327 if (prof_enabled == 0)
1329 LIST_FOREACH(sp, &sleepq_hash[SC_HASH(wmesg)], sp_link)
1330 if (sp->sp_wmesg == wmesg)
1332 sp = LIST_FIRST(&sleepq_prof_free);
1335 sp->sp_wmesg = wmesg;
1336 LIST_REMOVE(sp, sp_link);
1337 LIST_INSERT_HEAD(&sleepq_hash[SC_HASH(wmesg)], sp, sp_link);
1341 mtx_unlock_spin(&sleepq_prof_lock);
1346 sleepq_prof_reset(void)
1348 struct sleepq_prof *sp;
1352 mtx_lock_spin(&sleepq_prof_lock);
1353 enabled = prof_enabled;
1355 for (i = 0; i < SC_TABLESIZE; i++)
1356 LIST_INIT(&sleepq_hash[i]);
1357 LIST_INIT(&sleepq_prof_free);
1358 for (i = 0; i < SLEEPQ_PROF_LOCATIONS; i++) {
1359 sp = &sleepq_profent[i];
1360 sp->sp_wmesg = NULL;
1362 LIST_INSERT_HEAD(&sleepq_prof_free, sp, sp_link);
1364 prof_enabled = enabled;
1365 mtx_unlock_spin(&sleepq_prof_lock);
1369 enable_sleepq_prof(SYSCTL_HANDLER_ARGS)
1374 error = sysctl_handle_int(oidp, &v, v, req);
1377 if (req->newptr == NULL)
1379 if (v == prof_enabled)
1382 sleepq_prof_reset();
1383 mtx_lock_spin(&sleepq_prof_lock);
1385 mtx_unlock_spin(&sleepq_prof_lock);
1391 reset_sleepq_prof_stats(SYSCTL_HANDLER_ARGS)
1396 error = sysctl_handle_int(oidp, &v, 0, req);
1399 if (req->newptr == NULL)
1403 sleepq_prof_reset();
1409 dump_sleepq_prof_stats(SYSCTL_HANDLER_ARGS)
1411 struct sleepq_prof *sp;
1417 error = sysctl_wire_old_buffer(req, 0);
1420 sb = sbuf_new_for_sysctl(NULL, NULL, SLEEPQ_SBUFSIZE, req);
1421 sbuf_printf(sb, "\nwmesg\tcount\n");
1422 enabled = prof_enabled;
1423 mtx_lock_spin(&sleepq_prof_lock);
1425 mtx_unlock_spin(&sleepq_prof_lock);
1426 for (i = 0; i < SC_TABLESIZE; i++) {
1427 LIST_FOREACH(sp, &sleepq_hash[i], sp_link) {
1428 sbuf_printf(sb, "%s\t%ld\n",
1429 sp->sp_wmesg, sp->sp_count);
1432 mtx_lock_spin(&sleepq_prof_lock);
1433 prof_enabled = enabled;
1434 mtx_unlock_spin(&sleepq_prof_lock);
1436 error = sbuf_finish(sb);
1441 SYSCTL_PROC(_debug_sleepq, OID_AUTO, stats,
1442 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0,
1443 dump_sleepq_prof_stats, "A",
1444 "Sleepqueue profiling statistics");
1445 SYSCTL_PROC(_debug_sleepq, OID_AUTO, reset,
1446 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
1447 reset_sleepq_prof_stats, "I",
1448 "Reset sleepqueue profiling statistics");
1449 SYSCTL_PROC(_debug_sleepq, OID_AUTO, enable,
1450 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, 0,
1451 enable_sleepq_prof, "I",
1452 "Enable sleepqueue profiling");
1456 DB_SHOW_COMMAND(sleepq, db_show_sleepqueue)
1458 struct sleepqueue_chain *sc;
1459 struct sleepqueue *sq;
1461 struct lock_object *lock;
1471 * First, see if there is an active sleep queue for the wait channel
1472 * indicated by the address.
1474 wchan = (void *)addr;
1475 sc = SC_LOOKUP(wchan);
1476 LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
1477 if (sq->sq_wchan == wchan)
1481 * Second, see if there is an active sleep queue at the address
1484 for (i = 0; i < SC_TABLESIZE; i++)
1485 LIST_FOREACH(sq, &sleepq_chains[i].sc_queues, sq_hash) {
1486 if (sq == (struct sleepqueue *)addr)
1490 db_printf("Unable to locate a sleep queue via %p\n", (void *)addr);
1493 db_printf("Wait channel: %p\n", sq->sq_wchan);
1494 db_printf("Queue type: %d\n", sq->sq_type);
1498 db_printf("Associated Interlock: %p - (%s) %s\n", lock,
1499 LOCK_CLASS(lock)->lc_name, lock->lo_name);
1502 db_printf("Blocked threads:\n");
1503 for (i = 0; i < NR_SLEEPQS; i++) {
1504 db_printf("\nQueue[%d]:\n", i);
1505 if (TAILQ_EMPTY(&sq->sq_blocked[i]))
1506 db_printf("\tempty\n");
1508 TAILQ_FOREACH(td, &sq->sq_blocked[i],
1510 db_printf("\t%p (tid %d, pid %d, \"%s\")\n", td,
1511 td->td_tid, td->td_proc->p_pid,
1514 db_printf("(expected: %u)\n", sq->sq_blockedcnt[i]);
1518 /* Alias 'show sleepqueue' to 'show sleepq'. */
1519 DB_SHOW_ALIAS(sleepqueue, db_show_sleepqueue);