subr_sleepqueue.c revision 297466
1139804Simp/*-
2126324Sjhb * Copyright (c) 2004 John Baldwin <jhb@FreeBSD.org>
3126324Sjhb * All rights reserved.
4126324Sjhb *
5126324Sjhb * Redistribution and use in source and binary forms, with or without
6126324Sjhb * modification, are permitted provided that the following conditions
7126324Sjhb * are met:
8126324Sjhb * 1. Redistributions of source code must retain the above copyright
9126324Sjhb *    notice, this list of conditions and the following disclaimer.
10126324Sjhb * 2. Redistributions in binary form must reproduce the above copyright
11126324Sjhb *    notice, this list of conditions and the following disclaimer in the
12126324Sjhb *    documentation and/or other materials provided with the distribution.
13126324Sjhb *
14126324Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15126324Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16126324Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17126324Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18126324Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19126324Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20126324Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21126324Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22126324Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23126324Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24126324Sjhb * SUCH DAMAGE.
25126324Sjhb */
26126324Sjhb
27126324Sjhb/*
28126324Sjhb * Implementation of sleep queues used to hold queue of threads blocked on
29126324Sjhb * a wait channel.  Sleep queues different from turnstiles in that wait
30126324Sjhb * channels are not owned by anyone, so there is no priority propagation.
31126324Sjhb * Sleep queues can also provide a timeout and can also be interrupted by
32126324Sjhb * signals.  That said, there are several similarities between the turnstile
33126324Sjhb * and sleep queue implementations.  (Note: turnstiles were implemented
34126324Sjhb * first.)  For example, both use a hash table of the same size where each
35126324Sjhb * bucket is referred to as a "chain" that contains both a spin lock and
36126324Sjhb * a linked list of queues.  An individual queue is located by using a hash
37126324Sjhb * to pick a chain, locking the chain, and then walking the chain searching
38126324Sjhb * for the queue.  This means that a wait channel object does not need to
39126324Sjhb * embed it's queue head just as locks do not embed their turnstile queue
40126324Sjhb * head.  Threads also carry around a sleep queue that they lend to the
41126324Sjhb * wait channel when blocking.  Just as in turnstiles, the queue includes
42126324Sjhb * a free list of the sleep queues of other threads blocked on the same
43126324Sjhb * wait channel in the case of multiple waiters.
44126324Sjhb *
45126324Sjhb * Some additional functionality provided by sleep queues include the
46126324Sjhb * ability to set a timeout.  The timeout is managed using a per-thread
47126324Sjhb * callout that resumes a thread if it is asleep.  A thread may also
48126324Sjhb * catch signals while it is asleep (aka an interruptible sleep).  The
49126324Sjhb * signal code uses sleepq_abort() to interrupt a sleeping thread.  Finally,
50126324Sjhb * sleep queues also provide some extra assertions.  One is not allowed to
51126324Sjhb * mix the sleep/wakeup and cv APIs for a given wait channel.  Also, one
52126324Sjhb * must consistently use the same lock to synchronize with a wait channel,
53126324Sjhb * though this check is currently only a warning for sleep/wakeup due to
54126324Sjhb * pre-existing abuse of that API.  The same lock must also be held when
55126324Sjhb * awakening threads, though that is currently only enforced for condition
56126324Sjhb * variables.
57126324Sjhb */
58126324Sjhb
59126324Sjhb#include <sys/cdefs.h>
60126324Sjhb__FBSDID("$FreeBSD: head/sys/kern/subr_sleepqueue.c 297466 2016-03-31 18:10:29Z jhb $");
61126324Sjhb
62154936Sjhb#include "opt_sleepqueue_profiling.h"
63154936Sjhb#include "opt_ddb.h"
64170640Sjeff#include "opt_sched.h"
65296973Scem#include "opt_stack.h"
66154936Sjhb
67126324Sjhb#include <sys/param.h>
68126324Sjhb#include <sys/systm.h>
69126324Sjhb#include <sys/lock.h>
70126324Sjhb#include <sys/kernel.h>
71126324Sjhb#include <sys/ktr.h>
72126324Sjhb#include <sys/mutex.h>
73126324Sjhb#include <sys/proc.h>
74177372Sjeff#include <sys/sbuf.h>
75126324Sjhb#include <sys/sched.h>
76235459Srstone#include <sys/sdt.h>
77126324Sjhb#include <sys/signalvar.h>
78126324Sjhb#include <sys/sleepqueue.h>
79296927Scem#include <sys/stack.h>
80131259Sjhb#include <sys/sysctl.h>
81126324Sjhb
82169666Sjeff#include <vm/uma.h>
83169666Sjeff
84154936Sjhb#ifdef DDB
85154936Sjhb#include <ddb/ddb.h>
86154936Sjhb#endif
87154936Sjhb
88296927Scem
89126324Sjhb/*
90248186Smav * Constants for the hash table of sleep queue chains.
91248186Smav * SC_TABLESIZE must be a power of two for SC_MASK to work properly.
92126324Sjhb */
93248186Smav#define	SC_TABLESIZE	256			/* Must be power of 2. */
94126324Sjhb#define	SC_MASK		(SC_TABLESIZE - 1)
95126324Sjhb#define	SC_SHIFT	8
96248186Smav#define	SC_HASH(wc)	((((uintptr_t)(wc) >> SC_SHIFT) ^ (uintptr_t)(wc)) & \
97248186Smav			    SC_MASK)
98126324Sjhb#define	SC_LOOKUP(wc)	&sleepq_chains[SC_HASH(wc)]
99165272Skmacy#define NR_SLEEPQS      2
100126324Sjhb/*
101126324Sjhb * There two different lists of sleep queues.  Both lists are connected
102126324Sjhb * via the sq_hash entries.  The first list is the sleep queue chain list
103126324Sjhb * that a sleep queue is on when it is attached to a wait channel.  The
104126324Sjhb * second list is the free list hung off of a sleep queue that is attached
105126324Sjhb * to a wait channel.
106126324Sjhb *
107126324Sjhb * Each sleep queue also contains the wait channel it is attached to, the
108126324Sjhb * list of threads blocked on that wait channel, flags specific to the
109126324Sjhb * wait channel, and the lock used to synchronize with a wait channel.
110126324Sjhb * The flags are used to catch mismatches between the various consumers
111126324Sjhb * of the sleep queue API (e.g. sleep/wakeup and condition variables).
112126324Sjhb * The lock pointer is only used when invariants are enabled for various
113126324Sjhb * debugging checks.
114126324Sjhb *
115126324Sjhb * Locking key:
116126324Sjhb *  c - sleep queue chain lock
117126324Sjhb */
118126324Sjhbstruct sleepqueue {
119165272Skmacy	TAILQ_HEAD(, thread) sq_blocked[NR_SLEEPQS];	/* (c) Blocked threads. */
120200447Sattilio	u_int sq_blockedcnt[NR_SLEEPQS];	/* (c) N. of blocked threads. */
121126324Sjhb	LIST_ENTRY(sleepqueue) sq_hash;		/* (c) Chain and free list. */
122126324Sjhb	LIST_HEAD(, sleepqueue) sq_free;	/* (c) Free queues. */
123126324Sjhb	void	*sq_wchan;			/* (c) Wait channel. */
124201879Sattilio	int	sq_type;			/* (c) Queue type. */
125136445Sjhb#ifdef INVARIANTS
126164325Spjd	struct lock_object *sq_lock;		/* (c) Associated lock. */
127126324Sjhb#endif
128126324Sjhb};
129126324Sjhb
130126324Sjhbstruct sleepqueue_chain {
131126324Sjhb	LIST_HEAD(, sleepqueue) sc_queues;	/* List of sleep queues. */
132126324Sjhb	struct mtx sc_lock;			/* Spin lock for this chain. */
133131259Sjhb#ifdef SLEEPQUEUE_PROFILING
134131259Sjhb	u_int	sc_depth;			/* Length of sc_queues. */
135131259Sjhb	u_int	sc_max_depth;			/* Max length of sc_queues. */
136131259Sjhb#endif
137126324Sjhb};
138126324Sjhb
139131259Sjhb#ifdef SLEEPQUEUE_PROFILING
140131259Sjhbu_int sleepq_max_depth;
141227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, sleepq, CTLFLAG_RD, 0, "sleepq profiling");
142227309Sedstatic SYSCTL_NODE(_debug_sleepq, OID_AUTO, chains, CTLFLAG_RD, 0,
143131259Sjhb    "sleepq chain stats");
144131259SjhbSYSCTL_UINT(_debug_sleepq, OID_AUTO, max_depth, CTLFLAG_RD, &sleepq_max_depth,
145131259Sjhb    0, "maxmimum depth achieved of a single chain");
146177372Sjeff
147177372Sjeffstatic void	sleepq_profile(const char *wmesg);
148177372Sjeffstatic int	prof_enabled;
149131259Sjhb#endif
150126324Sjhbstatic struct sleepqueue_chain sleepq_chains[SC_TABLESIZE];
151169666Sjeffstatic uma_zone_t sleepq_zone;
152126324Sjhb
153126324Sjhb/*
154126324Sjhb * Prototypes for non-exported routines.
155126324Sjhb */
156177085Sjeffstatic int	sleepq_catch_signals(void *wchan, int pri);
157165272Skmacystatic int	sleepq_check_signals(void);
158277528Shselaskystatic int	sleepq_check_timeout(void);
159169666Sjeff#ifdef INVARIANTS
160169666Sjeffstatic void	sleepq_dtor(void *mem, int size, void *arg);
161169666Sjeff#endif
162169666Sjeffstatic int	sleepq_init(void *mem, int size, int flags);
163181334Sjhbstatic int	sleepq_resume_thread(struct sleepqueue *sq, struct thread *td,
164169666Sjeff		    int pri);
165177085Sjeffstatic void	sleepq_switch(void *wchan, int pri);
166126324Sjhbstatic void	sleepq_timeout(void *arg);
167126324Sjhb
168235459SrstoneSDT_PROBE_DECLARE(sched, , , sleep);
169235459SrstoneSDT_PROBE_DECLARE(sched, , , wakeup);
170235459Srstone
171126324Sjhb/*
172267820Sattilio * Initialize SLEEPQUEUE_PROFILING specific sysctl nodes.
173267820Sattilio * Note that it must happen after sleepinit() has been fully executed, so
174267820Sattilio * it must happen after SI_SUB_KMEM SYSINIT() subsystem setup.
175126324Sjhb */
176267820Sattilio#ifdef SLEEPQUEUE_PROFILING
177267820Sattiliostatic void
178267820Sattilioinit_sleepqueue_profiling(void)
179126324Sjhb{
180267820Sattilio	char chain_name[10];
181131259Sjhb	struct sysctl_oid *chain_oid;
182267820Sattilio	u_int i;
183126324Sjhb
184126324Sjhb	for (i = 0; i < SC_TABLESIZE; i++) {
185267820Sattilio		snprintf(chain_name, sizeof(chain_name), "%u", i);
186131259Sjhb		chain_oid = SYSCTL_ADD_NODE(NULL,
187131259Sjhb		    SYSCTL_STATIC_CHILDREN(_debug_sleepq_chains), OID_AUTO,
188131259Sjhb		    chain_name, CTLFLAG_RD, NULL, "sleepq chain stats");
189131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
190131259Sjhb		    "depth", CTLFLAG_RD, &sleepq_chains[i].sc_depth, 0, NULL);
191131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
192131259Sjhb		    "max_depth", CTLFLAG_RD, &sleepq_chains[i].sc_max_depth, 0,
193131259Sjhb		    NULL);
194267820Sattilio	}
195267820Sattilio}
196267820Sattilio
197267820SattilioSYSINIT(sleepqueue_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
198267820Sattilio    init_sleepqueue_profiling, NULL);
199131259Sjhb#endif
200267820Sattilio
201267820Sattilio/*
202267820Sattilio * Early initialization of sleep queues that is called from the sleepinit()
203267820Sattilio * SYSINIT.
204267820Sattilio */
205267820Sattiliovoid
206267820Sattilioinit_sleepqueues(void)
207267820Sattilio{
208267820Sattilio	int i;
209267820Sattilio
210267820Sattilio	for (i = 0; i < SC_TABLESIZE; i++) {
211267820Sattilio		LIST_INIT(&sleepq_chains[i].sc_queues);
212267820Sattilio		mtx_init(&sleepq_chains[i].sc_lock, "sleepq chain", NULL,
213267820Sattilio		    MTX_SPIN | MTX_RECURSE);
214126324Sjhb	}
215169666Sjeff	sleepq_zone = uma_zcreate("SLEEPQUEUE", sizeof(struct sleepqueue),
216169666Sjeff#ifdef INVARIANTS
217169666Sjeff	    NULL, sleepq_dtor, sleepq_init, NULL, UMA_ALIGN_CACHE, 0);
218169666Sjeff#else
219169666Sjeff	    NULL, NULL, sleepq_init, NULL, UMA_ALIGN_CACHE, 0);
220169666Sjeff#endif
221169666Sjeff
222126324Sjhb	thread0.td_sleepqueue = sleepq_alloc();
223126324Sjhb}
224126324Sjhb
225126324Sjhb/*
226169666Sjeff * Get a sleep queue for a new thread.
227126324Sjhb */
228126324Sjhbstruct sleepqueue *
229126324Sjhbsleepq_alloc(void)
230126324Sjhb{
231126324Sjhb
232169666Sjeff	return (uma_zalloc(sleepq_zone, M_WAITOK));
233126324Sjhb}
234126324Sjhb
235126324Sjhb/*
236126324Sjhb * Free a sleep queue when a thread is destroyed.
237126324Sjhb */
238126324Sjhbvoid
239126324Sjhbsleepq_free(struct sleepqueue *sq)
240126324Sjhb{
241126324Sjhb
242169666Sjeff	uma_zfree(sleepq_zone, sq);
243126324Sjhb}
244126324Sjhb
245126324Sjhb/*
246136445Sjhb * Lock the sleep queue chain associated with the specified wait channel.
247136445Sjhb */
248136445Sjhbvoid
249136445Sjhbsleepq_lock(void *wchan)
250136445Sjhb{
251136445Sjhb	struct sleepqueue_chain *sc;
252136445Sjhb
253136445Sjhb	sc = SC_LOOKUP(wchan);
254136445Sjhb	mtx_lock_spin(&sc->sc_lock);
255136445Sjhb}
256136445Sjhb
257136445Sjhb/*
258126324Sjhb * Look up the sleep queue associated with a given wait channel in the hash
259136445Sjhb * table locking the associated sleep queue chain.  If no queue is found in
260136445Sjhb * the table, NULL is returned.
261126324Sjhb */
262126324Sjhbstruct sleepqueue *
263126324Sjhbsleepq_lookup(void *wchan)
264126324Sjhb{
265126324Sjhb	struct sleepqueue_chain *sc;
266126324Sjhb	struct sleepqueue *sq;
267126324Sjhb
268126324Sjhb	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
269126324Sjhb	sc = SC_LOOKUP(wchan);
270136445Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
271126324Sjhb	LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
272126324Sjhb		if (sq->sq_wchan == wchan)
273126324Sjhb			return (sq);
274126324Sjhb	return (NULL);
275126324Sjhb}
276126324Sjhb
277126324Sjhb/*
278126324Sjhb * Unlock the sleep queue chain associated with a given wait channel.
279126324Sjhb */
280126324Sjhbvoid
281126324Sjhbsleepq_release(void *wchan)
282126324Sjhb{
283126324Sjhb	struct sleepqueue_chain *sc;
284126324Sjhb
285126324Sjhb	sc = SC_LOOKUP(wchan);
286126324Sjhb	mtx_unlock_spin(&sc->sc_lock);
287126324Sjhb}
288126324Sjhb
289126324Sjhb/*
290137277Sjhb * Places the current thread on the sleep queue for the specified wait
291126324Sjhb * channel.  If INVARIANTS is enabled, then it associates the passed in
292126324Sjhb * lock with the sleepq to make sure it is held when that sleep queue is
293126324Sjhb * woken up.
294126324Sjhb */
295126324Sjhbvoid
296165272Skmacysleepq_add(void *wchan, struct lock_object *lock, const char *wmesg, int flags,
297165272Skmacy    int queue)
298126324Sjhb{
299126324Sjhb	struct sleepqueue_chain *sc;
300136445Sjhb	struct sleepqueue *sq;
301137277Sjhb	struct thread *td;
302126324Sjhb
303126324Sjhb	td = curthread;
304126324Sjhb	sc = SC_LOOKUP(wchan);
305126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
306126324Sjhb	MPASS(td->td_sleepqueue != NULL);
307126324Sjhb	MPASS(wchan != NULL);
308165272Skmacy	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
309126324Sjhb
310150177Sjhb	/* If this thread is not allowed to sleep, die a horrible death. */
311247588Sjhb	KASSERT(td->td_no_sleeping == 0,
312247588Sjhb	    ("%s: td %p to sleep on wchan %p with sleeping prohibited",
313240423Sattilio	    __func__, td, wchan));
314150177Sjhb
315136445Sjhb	/* Look up the sleep queue associated with the wait channel 'wchan'. */
316136445Sjhb	sq = sleepq_lookup(wchan);
317136445Sjhb
318136445Sjhb	/*
319136445Sjhb	 * If the wait channel does not already have a sleep queue, use
320136445Sjhb	 * this thread's sleep queue.  Otherwise, insert the current thread
321136445Sjhb	 * into the sleep queue already in use by this wait channel.
322136445Sjhb	 */
323126324Sjhb	if (sq == NULL) {
324165272Skmacy#ifdef INVARIANTS
325165292Skmacy		int i;
326165291Sache
327165292Skmacy		sq = td->td_sleepqueue;
328200447Sattilio		for (i = 0; i < NR_SLEEPQS; i++) {
329165292Skmacy			KASSERT(TAILQ_EMPTY(&sq->sq_blocked[i]),
330200447Sattilio			    ("thread's sleep queue %d is not empty", i));
331200447Sattilio			KASSERT(sq->sq_blockedcnt[i] == 0,
332200447Sattilio			    ("thread's sleep queue %d count mismatches", i));
333200447Sattilio		}
334165272Skmacy		KASSERT(LIST_EMPTY(&sq->sq_free),
335165272Skmacy		    ("thread's sleep queue has a non-empty free list"));
336165272Skmacy		KASSERT(sq->sq_wchan == NULL, ("stale sq_wchan pointer"));
337165292Skmacy		sq->sq_lock = lock;
338165272Skmacy#endif
339131259Sjhb#ifdef SLEEPQUEUE_PROFILING
340131259Sjhb		sc->sc_depth++;
341131259Sjhb		if (sc->sc_depth > sc->sc_max_depth) {
342131259Sjhb			sc->sc_max_depth = sc->sc_depth;
343131259Sjhb			if (sc->sc_max_depth > sleepq_max_depth)
344131259Sjhb				sleepq_max_depth = sc->sc_max_depth;
345131259Sjhb		}
346131259Sjhb#endif
347165292Skmacy		sq = td->td_sleepqueue;
348126324Sjhb		LIST_INSERT_HEAD(&sc->sc_queues, sq, sq_hash);
349126324Sjhb		sq->sq_wchan = wchan;
350201879Sattilio		sq->sq_type = flags & SLEEPQ_TYPE;
351126324Sjhb	} else {
352126324Sjhb		MPASS(wchan == sq->sq_wchan);
353126488Sjhb		MPASS(lock == sq->sq_lock);
354136445Sjhb		MPASS((flags & SLEEPQ_TYPE) == sq->sq_type);
355126324Sjhb		LIST_INSERT_HEAD(&sq->sq_free, td->td_sleepqueue, sq_hash);
356126324Sjhb	}
357172155Sattilio	thread_lock(td);
358165272Skmacy	TAILQ_INSERT_TAIL(&sq->sq_blocked[queue], td, td_slpq);
359200447Sattilio	sq->sq_blockedcnt[queue]++;
360126324Sjhb	td->td_sleepqueue = NULL;
361165272Skmacy	td->td_sqqueue = queue;
362126324Sjhb	td->td_wchan = wchan;
363126324Sjhb	td->td_wmesg = wmesg;
364155741Sdavidxu	if (flags & SLEEPQ_INTERRUPTIBLE) {
365134013Sjhb		td->td_flags |= TDF_SINTR;
366155741Sdavidxu		td->td_flags &= ~TDF_SLEEPABORT;
367155741Sdavidxu	}
368172155Sattilio	thread_unlock(td);
369126324Sjhb}
370126324Sjhb
371126324Sjhb/*
372126324Sjhb * Sets a timeout that will remove the current thread from the specified
373126324Sjhb * sleep queue after timo ticks if the thread has not already been awakened.
374126324Sjhb */
375126324Sjhbvoid
376247783Sdavidesleepq_set_timeout_sbt(void *wchan, sbintime_t sbt, sbintime_t pr,
377247783Sdavide    int flags)
378126324Sjhb{
379277528Shselasky	struct sleepqueue_chain *sc;
380126324Sjhb	struct thread *td;
381126324Sjhb
382126324Sjhb	td = curthread;
383277528Shselasky	sc = SC_LOOKUP(wchan);
384277528Shselasky	mtx_assert(&sc->sc_lock, MA_OWNED);
385277528Shselasky	MPASS(TD_ON_SLEEPQ(td));
386277528Shselasky	MPASS(td->td_sleepqueue == NULL);
387277528Shselasky	MPASS(wchan != NULL);
388297466Sjhb	if (cold)
389297466Sjhb		panic("timed sleep before timers are working");
390247783Sdavide	callout_reset_sbt_on(&td->td_slpcallout, sbt, pr,
391247783Sdavide	    sleepq_timeout, td, PCPU_GET(cpuid), flags | C_DIRECT_EXEC);
392126324Sjhb}
393126324Sjhb
394126324Sjhb/*
395200447Sattilio * Return the number of actual sleepers for the specified queue.
396200447Sattilio */
397200447Sattiliou_int
398200447Sattiliosleepq_sleepcnt(void *wchan, int queue)
399200447Sattilio{
400200447Sattilio	struct sleepqueue *sq;
401200447Sattilio
402200447Sattilio	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
403200447Sattilio	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
404200447Sattilio	sq = sleepq_lookup(wchan);
405200447Sattilio	if (sq == NULL)
406200447Sattilio		return (0);
407200447Sattilio	return (sq->sq_blockedcnt[queue]);
408200447Sattilio}
409200447Sattilio
410200447Sattilio/*
411126324Sjhb * Marks the pending sleep of the current thread as interruptible and
412126324Sjhb * makes an initial check for pending signals before putting a thread
413170294Sjeff * to sleep. Enters and exits with the thread lock held.  Thread lock
414170294Sjeff * may have transitioned from the sleepq lock to a run lock.
415126324Sjhb */
416155741Sdavidxustatic int
417177085Sjeffsleepq_catch_signals(void *wchan, int pri)
418126324Sjhb{
419126324Sjhb	struct sleepqueue_chain *sc;
420126324Sjhb	struct sleepqueue *sq;
421126324Sjhb	struct thread *td;
422126324Sjhb	struct proc *p;
423155741Sdavidxu	struct sigacts *ps;
424248470Sjhb	int sig, ret;
425126324Sjhb
426126324Sjhb	td = curthread;
427155741Sdavidxu	p = curproc;
428126324Sjhb	sc = SC_LOOKUP(wchan);
429126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
430126324Sjhb	MPASS(wchan != NULL);
431211523Sdavidxu	if ((td->td_pflags & TDP_WAKEUP) != 0) {
432211523Sdavidxu		td->td_pflags &= ~TDP_WAKEUP;
433211523Sdavidxu		ret = EINTR;
434211534Sdavidxu		thread_lock(td);
435211523Sdavidxu		goto out;
436211523Sdavidxu	}
437211523Sdavidxu
438177375Sjeff	/*
439177375Sjeff	 * See if there are any pending signals for this thread.  If not
440177375Sjeff	 * we can switch immediately.  Otherwise do the signal processing
441177375Sjeff	 * directly.
442177375Sjeff	 */
443177375Sjeff	thread_lock(td);
444177471Sjeff	if ((td->td_flags & (TDF_NEEDSIGCHK | TDF_NEEDSUSPCHK)) == 0) {
445177375Sjeff		sleepq_switch(wchan, pri);
446177375Sjeff		return (0);
447177375Sjeff	}
448177375Sjeff	thread_unlock(td);
449177375Sjeff	mtx_unlock_spin(&sc->sc_lock);
450129241Sbde	CTR3(KTR_PROC, "sleepq catching signals: thread %p (pid %ld, %s)",
451173601Sjulian		(void *)td, (long)p->p_pid, td->td_name);
452126324Sjhb	PROC_LOCK(p);
453155741Sdavidxu	ps = p->p_sigacts;
454155741Sdavidxu	mtx_lock(&ps->ps_mtx);
455248470Sjhb	sig = cursig(td);
456155741Sdavidxu	if (sig == 0) {
457155741Sdavidxu		mtx_unlock(&ps->ps_mtx);
458155741Sdavidxu		ret = thread_suspend_check(1);
459155741Sdavidxu		MPASS(ret == 0 || ret == EINTR || ret == ERESTART);
460155741Sdavidxu	} else {
461155741Sdavidxu		if (SIGISMEMBER(ps->ps_sigintr, sig))
462155741Sdavidxu			ret = EINTR;
463155741Sdavidxu		else
464155741Sdavidxu			ret = ERESTART;
465155741Sdavidxu		mtx_unlock(&ps->ps_mtx);
466155741Sdavidxu	}
467184667Sdavidxu	/*
468184667Sdavidxu	 * Lock the per-process spinlock prior to dropping the PROC_LOCK
469184667Sdavidxu	 * to avoid a signal delivery race.  PROC_LOCK, PROC_SLOCK, and
470209612Sjhb	 * thread_lock() are currently held in tdsendsignal().
471184667Sdavidxu	 */
472184667Sdavidxu	PROC_SLOCK(p);
473170294Sjeff	mtx_lock_spin(&sc->sc_lock);
474184667Sdavidxu	PROC_UNLOCK(p);
475170294Sjeff	thread_lock(td);
476184667Sdavidxu	PROC_SUNLOCK(p);
477185502Sdavidxu	if (ret == 0) {
478185502Sdavidxu		sleepq_switch(wchan, pri);
479185502Sdavidxu		return (0);
480185502Sdavidxu	}
481211523Sdavidxuout:
482155936Sdavidxu	/*
483155936Sdavidxu	 * There were pending signals and this thread is still
484155936Sdavidxu	 * on the sleep queue, remove it from the sleep queue.
485155936Sdavidxu	 */
486170294Sjeff	if (TD_ON_SLEEPQ(td)) {
487170294Sjeff		sq = sleepq_lookup(wchan);
488181334Sjhb		if (sleepq_resume_thread(sq, td, 0)) {
489181334Sjhb#ifdef INVARIANTS
490181334Sjhb			/*
491181334Sjhb			 * This thread hasn't gone to sleep yet, so it
492181334Sjhb			 * should not be swapped out.
493181334Sjhb			 */
494181334Sjhb			panic("not waking up swapper");
495181334Sjhb#endif
496181334Sjhb		}
497170294Sjeff	}
498170294Sjeff	mtx_unlock_spin(&sc->sc_lock);
499170294Sjeff	MPASS(td->td_lock != &sc->sc_lock);
500155741Sdavidxu	return (ret);
501126324Sjhb}
502126324Sjhb
503126324Sjhb/*
504170294Sjeff * Switches to another thread if we are still asleep on a sleep queue.
505170294Sjeff * Returns with thread lock.
506126324Sjhb */
507126324Sjhbstatic void
508177085Sjeffsleepq_switch(void *wchan, int pri)
509126324Sjhb{
510126324Sjhb	struct sleepqueue_chain *sc;
511175654Sjhb	struct sleepqueue *sq;
512126324Sjhb	struct thread *td;
513126324Sjhb
514126324Sjhb	td = curthread;
515126324Sjhb	sc = SC_LOOKUP(wchan);
516126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
517170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
518175654Sjhb
519175654Sjhb	/*
520175654Sjhb	 * If we have a sleep queue, then we've already been woken up, so
521175654Sjhb	 * just return.
522175654Sjhb	 */
523126324Sjhb	if (td->td_sleepqueue != NULL) {
524126324Sjhb		mtx_unlock_spin(&sc->sc_lock);
525126324Sjhb		return;
526126324Sjhb	}
527175654Sjhb
528175654Sjhb	/*
529175654Sjhb	 * If TDF_TIMEOUT is set, then our sleep has been timed out
530175654Sjhb	 * already but we are still on the sleep queue, so dequeue the
531175654Sjhb	 * thread and return.
532175654Sjhb	 */
533175654Sjhb	if (td->td_flags & TDF_TIMEOUT) {
534175654Sjhb		MPASS(TD_ON_SLEEPQ(td));
535175654Sjhb		sq = sleepq_lookup(wchan);
536181334Sjhb		if (sleepq_resume_thread(sq, td, 0)) {
537181334Sjhb#ifdef INVARIANTS
538181334Sjhb			/*
539181334Sjhb			 * This thread hasn't gone to sleep yet, so it
540181334Sjhb			 * should not be swapped out.
541181334Sjhb			 */
542181334Sjhb			panic("not waking up swapper");
543181334Sjhb#endif
544181334Sjhb		}
545175654Sjhb		mtx_unlock_spin(&sc->sc_lock);
546175654Sjhb		return;
547175654Sjhb	}
548177372Sjeff#ifdef SLEEPQUEUE_PROFILING
549177372Sjeff	if (prof_enabled)
550177372Sjeff		sleepq_profile(td->td_wmesg);
551177372Sjeff#endif
552177085Sjeff	MPASS(td->td_sleepqueue == NULL);
553177085Sjeff	sched_sleep(td, pri);
554170294Sjeff	thread_lock_set(td, &sc->sc_lock);
555235459Srstone	SDT_PROBE0(sched, , , sleep);
556126324Sjhb	TD_SET_SLEEPING(td);
557178272Sjeff	mi_switch(SW_VOL | SWT_SLEEPQ, NULL);
558126324Sjhb	KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING"));
559129241Sbde	CTR3(KTR_PROC, "sleepq resume: thread %p (pid %ld, %s)",
560173600Sjulian	    (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
561126324Sjhb}
562126324Sjhb
563126324Sjhb/*
564126324Sjhb * Check to see if we timed out.
565126324Sjhb */
566126324Sjhbstatic int
567277528Shselaskysleepq_check_timeout(void)
568126324Sjhb{
569277528Shselasky	struct thread *td;
570277528Shselasky
571277528Shselasky	td = curthread;
572170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
573126324Sjhb
574126324Sjhb	/*
575126324Sjhb	 * If TDF_TIMEOUT is set, we timed out.
576126324Sjhb	 */
577126324Sjhb	if (td->td_flags & TDF_TIMEOUT) {
578126324Sjhb		td->td_flags &= ~TDF_TIMEOUT;
579126324Sjhb		return (EWOULDBLOCK);
580126324Sjhb	}
581277528Shselasky
582277528Shselasky	/*
583277528Shselasky	 * If TDF_TIMOFAIL is set, the timeout ran after we had
584277528Shselasky	 * already been woken up.
585277528Shselasky	 */
586277528Shselasky	if (td->td_flags & TDF_TIMOFAIL)
587277528Shselasky		td->td_flags &= ~TDF_TIMOFAIL;
588277528Shselasky
589277528Shselasky	/*
590277528Shselasky	 * If callout_stop() fails, then the timeout is running on
591277528Shselasky	 * another CPU, so synchronize with it to avoid having it
592277528Shselasky	 * accidentally wake up a subsequent sleep.
593277528Shselasky	 */
594296320Skib	else if (_callout_stop_safe(&td->td_slpcallout, CS_MIGRBLOCK, NULL)
595296320Skib	    == 0) {
596277528Shselasky		td->td_flags |= TDF_TIMEOUT;
597277528Shselasky		TD_SET_SLEEPING(td);
598277528Shselasky		mi_switch(SW_INVOL | SWT_SLEEPQTIMO, NULL);
599277528Shselasky	}
600126324Sjhb	return (0);
601126324Sjhb}
602126324Sjhb
603126324Sjhb/*
604126324Sjhb * Check to see if we were awoken by a signal.
605126324Sjhb */
606126324Sjhbstatic int
607126324Sjhbsleepq_check_signals(void)
608126324Sjhb{
609126324Sjhb	struct thread *td;
610126324Sjhb
611126324Sjhb	td = curthread;
612170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
613126324Sjhb
614126324Sjhb	/* We are no longer in an interruptible sleep. */
615155741Sdavidxu	if (td->td_flags & TDF_SINTR)
616246417Sjhb		td->td_flags &= ~TDF_SINTR;
617126324Sjhb
618155741Sdavidxu	if (td->td_flags & TDF_SLEEPABORT) {
619155741Sdavidxu		td->td_flags &= ~TDF_SLEEPABORT;
620155741Sdavidxu		return (td->td_intrval);
621155741Sdavidxu	}
622155741Sdavidxu
623126324Sjhb	return (0);
624126324Sjhb}
625126324Sjhb
626126324Sjhb/*
627126324Sjhb * Block the current thread until it is awakened from its sleep queue.
628126324Sjhb */
629126324Sjhbvoid
630177085Sjeffsleepq_wait(void *wchan, int pri)
631126324Sjhb{
632170294Sjeff	struct thread *td;
633126324Sjhb
634170294Sjeff	td = curthread;
635170294Sjeff	MPASS(!(td->td_flags & TDF_SINTR));
636170294Sjeff	thread_lock(td);
637177085Sjeff	sleepq_switch(wchan, pri);
638170294Sjeff	thread_unlock(td);
639126324Sjhb}
640126324Sjhb
641126324Sjhb/*
642126324Sjhb * Block the current thread until it is awakened from its sleep queue
643126324Sjhb * or it is interrupted by a signal.
644126324Sjhb */
645126324Sjhbint
646177085Sjeffsleepq_wait_sig(void *wchan, int pri)
647126324Sjhb{
648155741Sdavidxu	int rcatch;
649126324Sjhb	int rval;
650126324Sjhb
651177085Sjeff	rcatch = sleepq_catch_signals(wchan, pri);
652126324Sjhb	rval = sleepq_check_signals();
653170294Sjeff	thread_unlock(curthread);
654155741Sdavidxu	if (rcatch)
655155741Sdavidxu		return (rcatch);
656126324Sjhb	return (rval);
657126324Sjhb}
658126324Sjhb
659126324Sjhb/*
660126324Sjhb * Block the current thread until it is awakened from its sleep queue
661126324Sjhb * or it times out while waiting.
662126324Sjhb */
663126324Sjhbint
664177085Sjeffsleepq_timedwait(void *wchan, int pri)
665126324Sjhb{
666170294Sjeff	struct thread *td;
667126324Sjhb	int rval;
668126324Sjhb
669170294Sjeff	td = curthread;
670170294Sjeff	MPASS(!(td->td_flags & TDF_SINTR));
671170294Sjeff	thread_lock(td);
672177085Sjeff	sleepq_switch(wchan, pri);
673277528Shselasky	rval = sleepq_check_timeout();
674170294Sjeff	thread_unlock(td);
675170294Sjeff
676131249Sjhb	return (rval);
677126324Sjhb}
678126324Sjhb
679126324Sjhb/*
680126324Sjhb * Block the current thread until it is awakened from its sleep queue,
681126324Sjhb * it is interrupted by a signal, or it times out waiting to be awakened.
682126324Sjhb */
683126324Sjhbint
684177085Sjeffsleepq_timedwait_sig(void *wchan, int pri)
685126324Sjhb{
686155741Sdavidxu	int rcatch, rvalt, rvals;
687126324Sjhb
688177085Sjeff	rcatch = sleepq_catch_signals(wchan, pri);
689277528Shselasky	rvalt = sleepq_check_timeout();
690126324Sjhb	rvals = sleepq_check_signals();
691277528Shselasky	thread_unlock(curthread);
692155741Sdavidxu	if (rcatch)
693155741Sdavidxu		return (rcatch);
694155741Sdavidxu	if (rvals)
695126324Sjhb		return (rvals);
696155741Sdavidxu	return (rvalt);
697126324Sjhb}
698126324Sjhb
699126324Sjhb/*
700201879Sattilio * Returns the type of sleepqueue given a waitchannel.
701201879Sattilio */
702201879Sattilioint
703201879Sattiliosleepq_type(void *wchan)
704201879Sattilio{
705201879Sattilio	struct sleepqueue *sq;
706201879Sattilio	int type;
707201879Sattilio
708201879Sattilio	MPASS(wchan != NULL);
709201879Sattilio
710201879Sattilio	sleepq_lock(wchan);
711201879Sattilio	sq = sleepq_lookup(wchan);
712201879Sattilio	if (sq == NULL) {
713201879Sattilio		sleepq_release(wchan);
714201879Sattilio		return (-1);
715201879Sattilio	}
716201879Sattilio	type = sq->sq_type;
717201879Sattilio	sleepq_release(wchan);
718201879Sattilio	return (type);
719201879Sattilio}
720201879Sattilio
721201879Sattilio/*
722145056Sjhb * Removes a thread from a sleep queue and makes it
723145056Sjhb * runnable.
724126324Sjhb */
725181334Sjhbstatic int
726145056Sjhbsleepq_resume_thread(struct sleepqueue *sq, struct thread *td, int pri)
727126324Sjhb{
728126324Sjhb	struct sleepqueue_chain *sc;
729126324Sjhb
730126324Sjhb	MPASS(td != NULL);
731126324Sjhb	MPASS(sq->sq_wchan != NULL);
732126324Sjhb	MPASS(td->td_wchan == sq->sq_wchan);
733165272Skmacy	MPASS(td->td_sqqueue < NR_SLEEPQS && td->td_sqqueue >= 0);
734170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
735126324Sjhb	sc = SC_LOOKUP(sq->sq_wchan);
736126324Sjhb	mtx_assert(&sc->sc_lock, MA_OWNED);
737126324Sjhb
738235459Srstone	SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
739235459Srstone
740126324Sjhb	/* Remove the thread from the queue. */
741200447Sattilio	sq->sq_blockedcnt[td->td_sqqueue]--;
742165272Skmacy	TAILQ_REMOVE(&sq->sq_blocked[td->td_sqqueue], td, td_slpq);
743126324Sjhb
744126324Sjhb	/*
745126324Sjhb	 * Get a sleep queue for this thread.  If this is the last waiter,
746126324Sjhb	 * use the queue itself and take it out of the chain, otherwise,
747126324Sjhb	 * remove a queue from the free list.
748126324Sjhb	 */
749126324Sjhb	if (LIST_EMPTY(&sq->sq_free)) {
750126324Sjhb		td->td_sleepqueue = sq;
751126324Sjhb#ifdef INVARIANTS
752126324Sjhb		sq->sq_wchan = NULL;
753126324Sjhb#endif
754131259Sjhb#ifdef SLEEPQUEUE_PROFILING
755131259Sjhb		sc->sc_depth--;
756131259Sjhb#endif
757126324Sjhb	} else
758126324Sjhb		td->td_sleepqueue = LIST_FIRST(&sq->sq_free);
759126324Sjhb	LIST_REMOVE(td->td_sleepqueue, sq_hash);
760126324Sjhb
761129188Sjhb	td->td_wmesg = NULL;
762129188Sjhb	td->td_wchan = NULL;
763246417Sjhb	td->td_flags &= ~TDF_SINTR;
764129188Sjhb
765129241Sbde	CTR3(KTR_PROC, "sleepq_wakeup: thread %p (pid %ld, %s)",
766173600Sjulian	    (void *)td, (long)td->td_proc->p_pid, td->td_name);
767126324Sjhb
768126324Sjhb	/* Adjust priority if requested. */
769177085Sjeff	MPASS(pri == 0 || (pri >= PRI_MIN && pri <= PRI_MAX));
770217410Sjhb	if (pri != 0 && td->td_priority > pri &&
771217410Sjhb	    PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
772136439Sups		sched_prio(td, pri);
773184653Sjhb
774184653Sjhb	/*
775184653Sjhb	 * Note that thread td might not be sleeping if it is running
776184653Sjhb	 * sleepq_catch_signals() on another CPU or is blocked on its
777184653Sjhb	 * proc lock to check signals.  There's no need to mark the
778184653Sjhb	 * thread runnable in that case.
779184653Sjhb	 */
780184653Sjhb	if (TD_IS_SLEEPING(td)) {
781184653Sjhb		TD_CLR_SLEEPING(td);
782184653Sjhb		return (setrunnable(td));
783184653Sjhb	}
784184653Sjhb	return (0);
785126324Sjhb}
786126324Sjhb
787169666Sjeff#ifdef INVARIANTS
788126324Sjhb/*
789169666Sjeff * UMA zone item deallocator.
790169666Sjeff */
791169666Sjeffstatic void
792169666Sjeffsleepq_dtor(void *mem, int size, void *arg)
793169666Sjeff{
794169666Sjeff	struct sleepqueue *sq;
795169666Sjeff	int i;
796169666Sjeff
797169666Sjeff	sq = mem;
798200447Sattilio	for (i = 0; i < NR_SLEEPQS; i++) {
799169666Sjeff		MPASS(TAILQ_EMPTY(&sq->sq_blocked[i]));
800200447Sattilio		MPASS(sq->sq_blockedcnt[i] == 0);
801200447Sattilio	}
802169666Sjeff}
803169666Sjeff#endif
804169666Sjeff
805169666Sjeff/*
806169666Sjeff * UMA zone item initializer.
807169666Sjeff */
808169666Sjeffstatic int
809169666Sjeffsleepq_init(void *mem, int size, int flags)
810169666Sjeff{
811169666Sjeff	struct sleepqueue *sq;
812169666Sjeff	int i;
813169666Sjeff
814169666Sjeff	bzero(mem, size);
815169666Sjeff	sq = mem;
816200447Sattilio	for (i = 0; i < NR_SLEEPQS; i++) {
817169666Sjeff		TAILQ_INIT(&sq->sq_blocked[i]);
818200447Sattilio		sq->sq_blockedcnt[i] = 0;
819200447Sattilio	}
820169666Sjeff	LIST_INIT(&sq->sq_free);
821169666Sjeff	return (0);
822169666Sjeff}
823169666Sjeff
824169666Sjeff/*
825126324Sjhb * Find the highest priority thread sleeping on a wait channel and resume it.
826126324Sjhb */
827181334Sjhbint
828165272Skmacysleepq_signal(void *wchan, int flags, int pri, int queue)
829126324Sjhb{
830126324Sjhb	struct sleepqueue *sq;
831137277Sjhb	struct thread *td, *besttd;
832181334Sjhb	int wakeup_swapper;
833126324Sjhb
834126324Sjhb	CTR2(KTR_PROC, "sleepq_signal(%p, %d)", wchan, flags);
835126324Sjhb	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
836165272Skmacy	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
837126324Sjhb	sq = sleepq_lookup(wchan);
838170294Sjeff	if (sq == NULL)
839181334Sjhb		return (0);
840134013Sjhb	KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
841126324Sjhb	    ("%s: mismatch between sleep/wakeup and cv_*", __func__));
842129188Sjhb
843137277Sjhb	/*
844137277Sjhb	 * Find the highest priority thread on the queue.  If there is a
845137277Sjhb	 * tie, use the thread that first appears in the queue as it has
846137277Sjhb	 * been sleeping the longest since threads are always added to
847137277Sjhb	 * the tail of sleep queues.
848137277Sjhb	 */
849137277Sjhb	besttd = NULL;
850165272Skmacy	TAILQ_FOREACH(td, &sq->sq_blocked[queue], td_slpq) {
851137277Sjhb		if (besttd == NULL || td->td_priority < besttd->td_priority)
852137277Sjhb			besttd = td;
853137277Sjhb	}
854137277Sjhb	MPASS(besttd != NULL);
855170294Sjeff	thread_lock(besttd);
856181334Sjhb	wakeup_swapper = sleepq_resume_thread(sq, besttd, pri);
857170294Sjeff	thread_unlock(besttd);
858181334Sjhb	return (wakeup_swapper);
859126324Sjhb}
860126324Sjhb
861126324Sjhb/*
862126324Sjhb * Resume all threads sleeping on a specified wait channel.
863126324Sjhb */
864181334Sjhbint
865165272Skmacysleepq_broadcast(void *wchan, int flags, int pri, int queue)
866126324Sjhb{
867126324Sjhb	struct sleepqueue *sq;
868182875Sjhb	struct thread *td, *tdn;
869181334Sjhb	int wakeup_swapper;
870126324Sjhb
871126324Sjhb	CTR2(KTR_PROC, "sleepq_broadcast(%p, %d)", wchan, flags);
872126324Sjhb	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
873165272Skmacy	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
874126324Sjhb	sq = sleepq_lookup(wchan);
875177085Sjeff	if (sq == NULL)
876181334Sjhb		return (0);
877134013Sjhb	KASSERT(sq->sq_type == (flags & SLEEPQ_TYPE),
878126324Sjhb	    ("%s: mismatch between sleep/wakeup and cv_*", __func__));
879129188Sjhb
880145056Sjhb	/* Resume all blocked threads on the sleep queue. */
881181334Sjhb	wakeup_swapper = 0;
882182875Sjhb	TAILQ_FOREACH_SAFE(td, &sq->sq_blocked[queue], td_slpq, tdn) {
883170294Sjeff		thread_lock(td);
884181334Sjhb		if (sleepq_resume_thread(sq, td, pri))
885181334Sjhb			wakeup_swapper = 1;
886170294Sjeff		thread_unlock(td);
887170294Sjeff	}
888181334Sjhb	return (wakeup_swapper);
889126324Sjhb}
890126324Sjhb
891126324Sjhb/*
892126324Sjhb * Time sleeping threads out.  When the timeout expires, the thread is
893126324Sjhb * removed from the sleep queue and made runnable if it is still asleep.
894126324Sjhb */
895126324Sjhbstatic void
896126324Sjhbsleepq_timeout(void *arg)
897126324Sjhb{
898277528Shselasky	struct sleepqueue_chain *sc;
899277528Shselasky	struct sleepqueue *sq;
900277528Shselasky	struct thread *td;
901277528Shselasky	void *wchan;
902277528Shselasky	int wakeup_swapper;
903126324Sjhb
904277528Shselasky	td = arg;
905277528Shselasky	wakeup_swapper = 0;
906129241Sbde	CTR3(KTR_PROC, "sleepq_timeout: thread %p (pid %ld, %s)",
907173600Sjulian	    (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
908126324Sjhb
909277528Shselasky	/*
910277528Shselasky	 * First, see if the thread is asleep and get the wait channel if
911277528Shselasky	 * it is.
912277528Shselasky	 */
913277528Shselasky	thread_lock(td);
914277528Shselasky	if (TD_IS_SLEEPING(td) && TD_ON_SLEEPQ(td)) {
915277528Shselasky		wchan = td->td_wchan;
916277528Shselasky		sc = SC_LOOKUP(wchan);
917277528Shselasky		THREAD_LOCKPTR_ASSERT(td, &sc->sc_lock);
918277528Shselasky		sq = sleepq_lookup(wchan);
919277528Shselasky		MPASS(sq != NULL);
920277528Shselasky		td->td_flags |= TDF_TIMEOUT;
921277528Shselasky		wakeup_swapper = sleepq_resume_thread(sq, td, 0);
922277528Shselasky		thread_unlock(td);
923277528Shselasky		if (wakeup_swapper)
924277528Shselasky			kick_proc0();
925277528Shselasky		return;
926277528Shselasky	}
927277213Shselasky
928277528Shselasky	/*
929277528Shselasky	 * If the thread is on the SLEEPQ but isn't sleeping yet, it
930277528Shselasky	 * can either be on another CPU in between sleepq_add() and
931277528Shselasky	 * one of the sleepq_*wait*() routines or it can be in
932277528Shselasky	 * sleepq_catch_signals().
933277528Shselasky	 */
934277213Shselasky	if (TD_ON_SLEEPQ(td)) {
935277528Shselasky		td->td_flags |= TDF_TIMEOUT;
936277528Shselasky		thread_unlock(td);
937277528Shselasky		return;
938277528Shselasky	}
939175654Sjhb
940277528Shselasky	/*
941277528Shselasky	 * Now check for the edge cases.  First, if TDF_TIMEOUT is set,
942277528Shselasky	 * then the other thread has already yielded to us, so clear
943277528Shselasky	 * the flag and resume it.  If TDF_TIMEOUT is not set, then the
944277528Shselasky	 * we know that the other thread is not on a sleep queue, but it
945277528Shselasky	 * hasn't resumed execution yet.  In that case, set TDF_TIMOFAIL
946277528Shselasky	 * to let it know that the timeout has already run and doesn't
947277528Shselasky	 * need to be canceled.
948277528Shselasky	 */
949277528Shselasky	if (td->td_flags & TDF_TIMEOUT) {
950277528Shselasky		MPASS(TD_IS_SLEEPING(td));
951277528Shselasky		td->td_flags &= ~TDF_TIMEOUT;
952277528Shselasky		TD_CLR_SLEEPING(td);
953277528Shselasky		wakeup_swapper = setrunnable(td);
954277528Shselasky	} else
955277528Shselasky		td->td_flags |= TDF_TIMOFAIL;
956170294Sjeff	thread_unlock(td);
957181334Sjhb	if (wakeup_swapper)
958181334Sjhb		kick_proc0();
959126324Sjhb}
960126324Sjhb
961126324Sjhb/*
962126324Sjhb * Resumes a specific thread from the sleep queue associated with a specific
963126324Sjhb * wait channel if it is on that queue.
964126324Sjhb */
965126324Sjhbvoid
966126324Sjhbsleepq_remove(struct thread *td, void *wchan)
967126324Sjhb{
968126324Sjhb	struct sleepqueue *sq;
969181334Sjhb	int wakeup_swapper;
970126324Sjhb
971126324Sjhb	/*
972126324Sjhb	 * Look up the sleep queue for this wait channel, then re-check
973126324Sjhb	 * that the thread is asleep on that channel, if it is not, then
974126324Sjhb	 * bail.
975126324Sjhb	 */
976126324Sjhb	MPASS(wchan != NULL);
977136445Sjhb	sleepq_lock(wchan);
978126324Sjhb	sq = sleepq_lookup(wchan);
979170294Sjeff	/*
980170294Sjeff	 * We can not lock the thread here as it may be sleeping on a
981170294Sjeff	 * different sleepq.  However, holding the sleepq lock for this
982170294Sjeff	 * wchan can guarantee that we do not miss a wakeup for this
983170294Sjeff	 * channel.  The asserts below will catch any false positives.
984170294Sjeff	 */
985126324Sjhb	if (!TD_ON_SLEEPQ(td) || td->td_wchan != wchan) {
986126324Sjhb		sleepq_release(wchan);
987126324Sjhb		return;
988126324Sjhb	}
989170294Sjeff	/* Thread is asleep on sleep queue sq, so wake it up. */
990170294Sjeff	thread_lock(td);
991126324Sjhb	MPASS(sq != NULL);
992170294Sjeff	MPASS(td->td_wchan == wchan);
993181334Sjhb	wakeup_swapper = sleepq_resume_thread(sq, td, 0);
994170294Sjeff	thread_unlock(td);
995126324Sjhb	sleepq_release(wchan);
996181334Sjhb	if (wakeup_swapper)
997181334Sjhb		kick_proc0();
998126324Sjhb}
999126324Sjhb
1000126324Sjhb/*
1001129241Sbde * Abort a thread as if an interrupt had occurred.  Only abort
1002129241Sbde * interruptible waits (unfortunately it isn't safe to abort others).
1003126324Sjhb */
1004181334Sjhbint
1005155741Sdavidxusleepq_abort(struct thread *td, int intrval)
1006126324Sjhb{
1007170294Sjeff	struct sleepqueue *sq;
1008126324Sjhb	void *wchan;
1009126324Sjhb
1010170294Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
1011126324Sjhb	MPASS(TD_ON_SLEEPQ(td));
1012126324Sjhb	MPASS(td->td_flags & TDF_SINTR);
1013155741Sdavidxu	MPASS(intrval == EINTR || intrval == ERESTART);
1014126324Sjhb
1015126324Sjhb	/*
1016126324Sjhb	 * If the TDF_TIMEOUT flag is set, just leave. A
1017126324Sjhb	 * timeout is scheduled anyhow.
1018126324Sjhb	 */
1019126324Sjhb	if (td->td_flags & TDF_TIMEOUT)
1020181334Sjhb		return (0);
1021126324Sjhb
1022129241Sbde	CTR3(KTR_PROC, "sleepq_abort: thread %p (pid %ld, %s)",
1023173600Sjulian	    (void *)td, (long)td->td_proc->p_pid, (void *)td->td_name);
1024170294Sjeff	td->td_intrval = intrval;
1025170294Sjeff	td->td_flags |= TDF_SLEEPABORT;
1026170294Sjeff	/*
1027170294Sjeff	 * If the thread has not slept yet it will find the signal in
1028170294Sjeff	 * sleepq_catch_signals() and call sleepq_resume_thread.  Otherwise
1029170294Sjeff	 * we have to do it here.
1030170294Sjeff	 */
1031170294Sjeff	if (!TD_IS_SLEEPING(td))
1032181334Sjhb		return (0);
1033126324Sjhb	wchan = td->td_wchan;
1034170294Sjeff	MPASS(wchan != NULL);
1035170294Sjeff	sq = sleepq_lookup(wchan);
1036170294Sjeff	MPASS(sq != NULL);
1037170294Sjeff
1038170294Sjeff	/* Thread is asleep on sleep queue sq, so wake it up. */
1039181334Sjhb	return (sleepq_resume_thread(sq, td, 0));
1040126324Sjhb}
1041154936Sjhb
1042296927Scem/*
1043296927Scem * Prints the stacks of all threads presently sleeping on wchan/queue to
1044296927Scem * the sbuf sb.  Sets count_stacks_printed to the number of stacks actually
1045296927Scem * printed.  Typically, this will equal the number of threads sleeping on the
1046296927Scem * queue, but may be less if sb overflowed before all stacks were printed.
1047296927Scem */
1048296973Scem#ifdef STACK
1049296927Scemint
1050296927Scemsleepq_sbuf_print_stacks(struct sbuf *sb, void *wchan, int queue,
1051296927Scem    int *count_stacks_printed)
1052296927Scem{
1053296927Scem	struct thread *td, *td_next;
1054296927Scem	struct sleepqueue *sq;
1055296927Scem	struct stack **st;
1056296927Scem	struct sbuf **td_infos;
1057296927Scem	int i, stack_idx, error, stacks_to_allocate;
1058296927Scem	bool finished, partial_print;
1059296927Scem
1060296927Scem	error = 0;
1061296927Scem	finished = false;
1062296927Scem	partial_print = false;
1063296927Scem
1064296927Scem	KASSERT(wchan != NULL, ("%s: invalid NULL wait channel", __func__));
1065296927Scem	MPASS((queue >= 0) && (queue < NR_SLEEPQS));
1066296927Scem
1067296927Scem	stacks_to_allocate = 10;
1068296927Scem	for (i = 0; i < 3 && !finished ; i++) {
1069296927Scem		/* We cannot malloc while holding the queue's spinlock, so
1070296927Scem		 * we do our mallocs now, and hope it is enough.  If it
1071296927Scem		 * isn't, we will free these, drop the lock, malloc more,
1072296927Scem		 * and try again, up to a point.  After that point we will
1073296927Scem		 * give up and report ENOMEM. We also cannot write to sb
1074296927Scem		 * during this time since the client may have set the
1075296927Scem		 * SBUF_AUTOEXTEND flag on their sbuf, which could cause a
1076296927Scem		 * malloc as we print to it.  So we defer actually printing
1077296927Scem		 * to sb until after we drop the spinlock.
1078296927Scem		 */
1079296927Scem
1080296927Scem		/* Where we will store the stacks. */
1081296927Scem		st = malloc(sizeof(struct stack *) * stacks_to_allocate,
1082296927Scem		    M_TEMP, M_WAITOK);
1083296927Scem		for (stack_idx = 0; stack_idx < stacks_to_allocate;
1084296927Scem		    stack_idx++)
1085296927Scem			st[stack_idx] = stack_create();
1086296927Scem
1087296927Scem		/* Where we will store the td name, tid, etc. */
1088296927Scem		td_infos = malloc(sizeof(struct sbuf *) * stacks_to_allocate,
1089296927Scem		    M_TEMP, M_WAITOK);
1090296927Scem		for (stack_idx = 0; stack_idx < stacks_to_allocate;
1091296927Scem		    stack_idx++)
1092296927Scem			td_infos[stack_idx] = sbuf_new(NULL, NULL,
1093296927Scem			    MAXCOMLEN + sizeof(struct thread *) * 2 + 40,
1094296927Scem			    SBUF_FIXEDLEN);
1095296927Scem
1096296927Scem		sleepq_lock(wchan);
1097296927Scem		sq = sleepq_lookup(wchan);
1098296927Scem		if (sq == NULL) {
1099296927Scem			/* This sleepq does not exist; exit and return ENOENT. */
1100296927Scem			error = ENOENT;
1101296927Scem			finished = true;
1102296927Scem			sleepq_release(wchan);
1103296927Scem			goto loop_end;
1104296927Scem		}
1105296927Scem
1106296927Scem		stack_idx = 0;
1107296927Scem		/* Save thread info */
1108296927Scem		TAILQ_FOREACH_SAFE(td, &sq->sq_blocked[queue], td_slpq,
1109296927Scem		    td_next) {
1110296927Scem			if (stack_idx >= stacks_to_allocate)
1111296927Scem				goto loop_end;
1112296927Scem
1113296927Scem			/* Note the td_lock is equal to the sleepq_lock here. */
1114296927Scem			stack_save_td(st[stack_idx], td);
1115296927Scem
1116296927Scem			sbuf_printf(td_infos[stack_idx], "%d: %s %p",
1117296927Scem			    td->td_tid, td->td_name, td);
1118296927Scem
1119296927Scem			++stack_idx;
1120296927Scem		}
1121296927Scem
1122296927Scem		finished = true;
1123296927Scem		sleepq_release(wchan);
1124296927Scem
1125296927Scem		/* Print the stacks */
1126296927Scem		for (i = 0; i < stack_idx; i++) {
1127296927Scem			sbuf_finish(td_infos[i]);
1128296927Scem			sbuf_printf(sb, "--- thread %s: ---\n", sbuf_data(td_infos[i]));
1129296927Scem			stack_sbuf_print(sb, st[i]);
1130296927Scem			sbuf_printf(sb, "\n");
1131296927Scem
1132296927Scem			error = sbuf_error(sb);
1133296927Scem			if (error == 0)
1134296927Scem				*count_stacks_printed = stack_idx;
1135296927Scem		}
1136296927Scem
1137296927Scemloop_end:
1138296927Scem		if (!finished)
1139296927Scem			sleepq_release(wchan);
1140296927Scem		for (stack_idx = 0; stack_idx < stacks_to_allocate;
1141296927Scem		    stack_idx++)
1142296927Scem			stack_destroy(st[stack_idx]);
1143296927Scem		for (stack_idx = 0; stack_idx < stacks_to_allocate;
1144296927Scem		    stack_idx++)
1145296927Scem			sbuf_delete(td_infos[stack_idx]);
1146296927Scem		free(st, M_TEMP);
1147296927Scem		free(td_infos, M_TEMP);
1148296927Scem		stacks_to_allocate *= 10;
1149296927Scem	}
1150296927Scem
1151296927Scem	if (!finished && error == 0)
1152296927Scem		error = ENOMEM;
1153296927Scem
1154296927Scem	return (error);
1155296927Scem}
1156296973Scem#endif
1157296927Scem
1158177372Sjeff#ifdef SLEEPQUEUE_PROFILING
1159177372Sjeff#define	SLEEPQ_PROF_LOCATIONS	1024
1160212750Smdf#define	SLEEPQ_SBUFSIZE		512
1161177372Sjeffstruct sleepq_prof {
1162177372Sjeff	LIST_ENTRY(sleepq_prof) sp_link;
1163177372Sjeff	const char	*sp_wmesg;
1164177372Sjeff	long		sp_count;
1165177372Sjeff};
1166177372Sjeff
1167177372SjeffLIST_HEAD(sqphead, sleepq_prof);
1168177372Sjeff
1169177372Sjeffstruct sqphead sleepq_prof_free;
1170177372Sjeffstruct sqphead sleepq_hash[SC_TABLESIZE];
1171177372Sjeffstatic struct sleepq_prof sleepq_profent[SLEEPQ_PROF_LOCATIONS];
1172177372Sjeffstatic struct mtx sleepq_prof_lock;
1173177372SjeffMTX_SYSINIT(sleepq_prof_lock, &sleepq_prof_lock, "sleepq_prof", MTX_SPIN);
1174177372Sjeff
1175177372Sjeffstatic void
1176177372Sjeffsleepq_profile(const char *wmesg)
1177177372Sjeff{
1178177372Sjeff	struct sleepq_prof *sp;
1179177372Sjeff
1180177372Sjeff	mtx_lock_spin(&sleepq_prof_lock);
1181177372Sjeff	if (prof_enabled == 0)
1182177372Sjeff		goto unlock;
1183177372Sjeff	LIST_FOREACH(sp, &sleepq_hash[SC_HASH(wmesg)], sp_link)
1184177372Sjeff		if (sp->sp_wmesg == wmesg)
1185177372Sjeff			goto done;
1186177372Sjeff	sp = LIST_FIRST(&sleepq_prof_free);
1187177372Sjeff	if (sp == NULL)
1188177372Sjeff		goto unlock;
1189177372Sjeff	sp->sp_wmesg = wmesg;
1190177372Sjeff	LIST_REMOVE(sp, sp_link);
1191177372Sjeff	LIST_INSERT_HEAD(&sleepq_hash[SC_HASH(wmesg)], sp, sp_link);
1192177372Sjeffdone:
1193177372Sjeff	sp->sp_count++;
1194177372Sjeffunlock:
1195177372Sjeff	mtx_unlock_spin(&sleepq_prof_lock);
1196177372Sjeff	return;
1197177372Sjeff}
1198177372Sjeff
1199177372Sjeffstatic void
1200177372Sjeffsleepq_prof_reset(void)
1201177372Sjeff{
1202177372Sjeff	struct sleepq_prof *sp;
1203177372Sjeff	int enabled;
1204177372Sjeff	int i;
1205177372Sjeff
1206177372Sjeff	mtx_lock_spin(&sleepq_prof_lock);
1207177372Sjeff	enabled = prof_enabled;
1208177372Sjeff	prof_enabled = 0;
1209177372Sjeff	for (i = 0; i < SC_TABLESIZE; i++)
1210177372Sjeff		LIST_INIT(&sleepq_hash[i]);
1211177372Sjeff	LIST_INIT(&sleepq_prof_free);
1212177372Sjeff	for (i = 0; i < SLEEPQ_PROF_LOCATIONS; i++) {
1213177372Sjeff		sp = &sleepq_profent[i];
1214177372Sjeff		sp->sp_wmesg = NULL;
1215177372Sjeff		sp->sp_count = 0;
1216177372Sjeff		LIST_INSERT_HEAD(&sleepq_prof_free, sp, sp_link);
1217177372Sjeff	}
1218177372Sjeff	prof_enabled = enabled;
1219177372Sjeff	mtx_unlock_spin(&sleepq_prof_lock);
1220177372Sjeff}
1221177372Sjeff
1222177372Sjeffstatic int
1223177372Sjeffenable_sleepq_prof(SYSCTL_HANDLER_ARGS)
1224177372Sjeff{
1225177372Sjeff	int error, v;
1226177372Sjeff
1227177372Sjeff	v = prof_enabled;
1228177372Sjeff	error = sysctl_handle_int(oidp, &v, v, req);
1229177372Sjeff	if (error)
1230177372Sjeff		return (error);
1231177372Sjeff	if (req->newptr == NULL)
1232177372Sjeff		return (error);
1233177372Sjeff	if (v == prof_enabled)
1234177372Sjeff		return (0);
1235177372Sjeff	if (v == 1)
1236177372Sjeff		sleepq_prof_reset();
1237177372Sjeff	mtx_lock_spin(&sleepq_prof_lock);
1238177372Sjeff	prof_enabled = !!v;
1239177372Sjeff	mtx_unlock_spin(&sleepq_prof_lock);
1240177372Sjeff
1241177372Sjeff	return (0);
1242177372Sjeff}
1243177372Sjeff
1244177372Sjeffstatic int
1245177372Sjeffreset_sleepq_prof_stats(SYSCTL_HANDLER_ARGS)
1246177372Sjeff{
1247177372Sjeff	int error, v;
1248177372Sjeff
1249177372Sjeff	v = 0;
1250177372Sjeff	error = sysctl_handle_int(oidp, &v, 0, req);
1251177372Sjeff	if (error)
1252177372Sjeff		return (error);
1253177372Sjeff	if (req->newptr == NULL)
1254177372Sjeff		return (error);
1255177372Sjeff	if (v == 0)
1256177372Sjeff		return (0);
1257177372Sjeff	sleepq_prof_reset();
1258177372Sjeff
1259177372Sjeff	return (0);
1260177372Sjeff}
1261177372Sjeff
1262177372Sjeffstatic int
1263177372Sjeffdump_sleepq_prof_stats(SYSCTL_HANDLER_ARGS)
1264177372Sjeff{
1265177372Sjeff	struct sleepq_prof *sp;
1266177372Sjeff	struct sbuf *sb;
1267177372Sjeff	int enabled;
1268177372Sjeff	int error;
1269177372Sjeff	int i;
1270177372Sjeff
1271217916Smdf	error = sysctl_wire_old_buffer(req, 0);
1272217916Smdf	if (error != 0)
1273217916Smdf		return (error);
1274212750Smdf	sb = sbuf_new_for_sysctl(NULL, NULL, SLEEPQ_SBUFSIZE, req);
1275177372Sjeff	sbuf_printf(sb, "\nwmesg\tcount\n");
1276177372Sjeff	enabled = prof_enabled;
1277177372Sjeff	mtx_lock_spin(&sleepq_prof_lock);
1278177372Sjeff	prof_enabled = 0;
1279177372Sjeff	mtx_unlock_spin(&sleepq_prof_lock);
1280177372Sjeff	for (i = 0; i < SC_TABLESIZE; i++) {
1281177372Sjeff		LIST_FOREACH(sp, &sleepq_hash[i], sp_link) {
1282177372Sjeff			sbuf_printf(sb, "%s\t%ld\n",
1283177372Sjeff			    sp->sp_wmesg, sp->sp_count);
1284177372Sjeff		}
1285177372Sjeff	}
1286177372Sjeff	mtx_lock_spin(&sleepq_prof_lock);
1287177372Sjeff	prof_enabled = enabled;
1288177372Sjeff	mtx_unlock_spin(&sleepq_prof_lock);
1289177372Sjeff
1290212750Smdf	error = sbuf_finish(sb);
1291177372Sjeff	sbuf_delete(sb);
1292177372Sjeff	return (error);
1293177372Sjeff}
1294177372Sjeff
1295177372SjeffSYSCTL_PROC(_debug_sleepq, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD,
1296177372Sjeff    NULL, 0, dump_sleepq_prof_stats, "A", "Sleepqueue profiling statistics");
1297177372SjeffSYSCTL_PROC(_debug_sleepq, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW,
1298177372Sjeff    NULL, 0, reset_sleepq_prof_stats, "I",
1299177372Sjeff    "Reset sleepqueue profiling statistics");
1300177372SjeffSYSCTL_PROC(_debug_sleepq, OID_AUTO, enable, CTLTYPE_INT | CTLFLAG_RW,
1301177372Sjeff    NULL, 0, enable_sleepq_prof, "I", "Enable sleepqueue profiling");
1302177372Sjeff#endif
1303177372Sjeff
1304154936Sjhb#ifdef DDB
1305154936SjhbDB_SHOW_COMMAND(sleepq, db_show_sleepqueue)
1306154936Sjhb{
1307154936Sjhb	struct sleepqueue_chain *sc;
1308154936Sjhb	struct sleepqueue *sq;
1309154944Simp#ifdef INVARIANTS
1310154936Sjhb	struct lock_object *lock;
1311154944Simp#endif
1312154936Sjhb	struct thread *td;
1313154936Sjhb	void *wchan;
1314154936Sjhb	int i;
1315154936Sjhb
1316154936Sjhb	if (!have_addr)
1317154936Sjhb		return;
1318154936Sjhb
1319154936Sjhb	/*
1320154936Sjhb	 * First, see if there is an active sleep queue for the wait channel
1321154936Sjhb	 * indicated by the address.
1322154936Sjhb	 */
1323154936Sjhb	wchan = (void *)addr;
1324154936Sjhb	sc = SC_LOOKUP(wchan);
1325154936Sjhb	LIST_FOREACH(sq, &sc->sc_queues, sq_hash)
1326154936Sjhb		if (sq->sq_wchan == wchan)
1327154936Sjhb			goto found;
1328154936Sjhb
1329154936Sjhb	/*
1330154936Sjhb	 * Second, see if there is an active sleep queue at the address
1331154936Sjhb	 * indicated.
1332154936Sjhb	 */
1333154936Sjhb	for (i = 0; i < SC_TABLESIZE; i++)
1334154936Sjhb		LIST_FOREACH(sq, &sleepq_chains[i].sc_queues, sq_hash) {
1335154936Sjhb			if (sq == (struct sleepqueue *)addr)
1336154936Sjhb				goto found;
1337154936Sjhb		}
1338154936Sjhb
1339154936Sjhb	db_printf("Unable to locate a sleep queue via %p\n", (void *)addr);
1340154936Sjhb	return;
1341154936Sjhbfound:
1342154936Sjhb	db_printf("Wait channel: %p\n", sq->sq_wchan);
1343201879Sattilio	db_printf("Queue type: %d\n", sq->sq_type);
1344154936Sjhb#ifdef INVARIANTS
1345154936Sjhb	if (sq->sq_lock) {
1346164325Spjd		lock = sq->sq_lock;
1347154936Sjhb		db_printf("Associated Interlock: %p - (%s) %s\n", lock,
1348154936Sjhb		    LOCK_CLASS(lock)->lc_name, lock->lo_name);
1349154936Sjhb	}
1350154936Sjhb#endif
1351154936Sjhb	db_printf("Blocked threads:\n");
1352165272Skmacy	for (i = 0; i < NR_SLEEPQS; i++) {
1353165272Skmacy		db_printf("\nQueue[%d]:\n", i);
1354165272Skmacy		if (TAILQ_EMPTY(&sq->sq_blocked[i]))
1355165272Skmacy			db_printf("\tempty\n");
1356165272Skmacy		else
1357165272Skmacy			TAILQ_FOREACH(td, &sq->sq_blocked[0],
1358165272Skmacy				      td_slpq) {
1359165272Skmacy				db_printf("\t%p (tid %d, pid %d, \"%s\")\n", td,
1360165272Skmacy					  td->td_tid, td->td_proc->p_pid,
1361180930Sjhb					  td->td_name);
1362165272Skmacy			}
1363200447Sattilio		db_printf("(expected: %u)\n", sq->sq_blockedcnt[i]);
1364165272Skmacy	}
1365154936Sjhb}
1366157823Sjhb
1367157823Sjhb/* Alias 'show sleepqueue' to 'show sleepq'. */
1368183054SsamDB_SHOW_ALIAS(sleepqueue, db_show_sleepqueue);
1369154936Sjhb#endif
1370