subr_turnstile.c revision 239585
1/*-
2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 *    promote products derived from this software without specific prior
14 *    written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
29 *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
30 */
31
32/*
33 * Implementation of turnstiles used to hold queue of threads blocked on
34 * non-sleepable locks.  Sleepable locks use condition variables to
35 * implement their queues.  Turnstiles differ from a sleep queue in that
36 * turnstile queue's are assigned to a lock held by an owning thread.  Thus,
37 * when one thread is enqueued onto a turnstile, it can lend its priority
38 * to the owning thread.
39 *
40 * We wish to avoid bloating locks with an embedded turnstile and we do not
41 * want to use back-pointers in the locks for the same reason.  Thus, we
42 * use a similar approach to that of Solaris 7 as described in Solaris
43 * Internals by Jim Mauro and Richard McDougall.  Turnstiles are looked up
44 * in a hash table based on the address of the lock.  Each entry in the
45 * hash table is a linked-lists of turnstiles and is called a turnstile
46 * chain.  Each chain contains a spin mutex that protects all of the
47 * turnstiles in the chain.
48 *
49 * Each time a thread is created, a turnstile is allocated from a UMA zone
50 * and attached to that thread.  When a thread blocks on a lock, if it is the
51 * first thread to block, it lends its turnstile to the lock.  If the lock
52 * already has a turnstile, then it gives its turnstile to the lock's
53 * turnstile's free list.  When a thread is woken up, it takes a turnstile from
54 * the free list if there are any other waiters.  If it is the only thread
55 * blocked on the lock, then it reclaims the turnstile associated with the lock
56 * and removes it from the hash table.
57 */
58
59#include <sys/cdefs.h>
60__FBSDID("$FreeBSD: head/sys/kern/subr_turnstile.c 239585 2012-08-22 20:01:38Z jhb $");
61
62#include "opt_ddb.h"
63#include "opt_kdtrace.h"
64#include "opt_turnstile_profiling.h"
65#include "opt_sched.h"
66
67#include <sys/param.h>
68#include <sys/systm.h>
69#include <sys/kdb.h>
70#include <sys/kernel.h>
71#include <sys/ktr.h>
72#include <sys/lock.h>
73#include <sys/mutex.h>
74#include <sys/proc.h>
75#include <sys/queue.h>
76#include <sys/sched.h>
77#include <sys/sdt.h>
78#include <sys/sysctl.h>
79#include <sys/turnstile.h>
80
81#include <vm/uma.h>
82
83#ifdef DDB
84#include <ddb/ddb.h>
85#include <sys/lockmgr.h>
86#include <sys/sx.h>
87#endif
88
89/*
90 * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
91 * number chosen because the sleep queue's use the same value for the
92 * shift.  Basically, we ignore the lower 8 bits of the address.
93 * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
94 */
95#define	TC_TABLESIZE	128			/* Must be power of 2. */
96#define	TC_MASK		(TC_TABLESIZE - 1)
97#define	TC_SHIFT	8
98#define	TC_HASH(lock)	(((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
99#define	TC_LOOKUP(lock)	&turnstile_chains[TC_HASH(lock)]
100
101/*
102 * There are three different lists of turnstiles as follows.  The list
103 * connected by ts_link entries is a per-thread list of all the turnstiles
104 * attached to locks that we own.  This is used to fixup our priority when
105 * a lock is released.  The other two lists use the ts_hash entries.  The
106 * first of these two is the turnstile chain list that a turnstile is on
107 * when it is attached to a lock.  The second list to use ts_hash is the
108 * free list hung off of a turnstile that is attached to a lock.
109 *
110 * Each turnstile contains three lists of threads.  The two ts_blocked lists
111 * are linked list of threads blocked on the turnstile's lock.  One list is
112 * for exclusive waiters, and the other is for shared waiters.  The
113 * ts_pending list is a linked list of threads previously awakened by
114 * turnstile_signal() or turnstile_wait() that are waiting to be put on
115 * the run queue.
116 *
117 * Locking key:
118 *  c - turnstile chain lock
119 *  q - td_contested lock
120 */
121struct turnstile {
122	struct mtx ts_lock;			/* Spin lock for self. */
123	struct threadqueue ts_blocked[2];	/* (c + q) Blocked threads. */
124	struct threadqueue ts_pending;		/* (c) Pending threads. */
125	LIST_ENTRY(turnstile) ts_hash;		/* (c) Chain and free list. */
126	LIST_ENTRY(turnstile) ts_link;		/* (q) Contested locks. */
127	LIST_HEAD(, turnstile) ts_free;		/* (c) Free turnstiles. */
128	struct lock_object *ts_lockobj;		/* (c) Lock we reference. */
129	struct thread *ts_owner;		/* (c + q) Who owns the lock. */
130};
131
132struct turnstile_chain {
133	LIST_HEAD(, turnstile) tc_turnstiles;	/* List of turnstiles. */
134	struct mtx tc_lock;			/* Spin lock for this chain. */
135#ifdef TURNSTILE_PROFILING
136	u_int	tc_depth;			/* Length of tc_queues. */
137	u_int	tc_max_depth;			/* Max length of tc_queues. */
138#endif
139};
140
141#ifdef TURNSTILE_PROFILING
142u_int turnstile_max_depth;
143static SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0,
144    "turnstile profiling");
145static SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
146    "turnstile chain stats");
147SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
148    &turnstile_max_depth, 0, "maximum depth achieved of a single chain");
149#endif
150static struct mtx td_contested_lock;
151static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
152static uma_zone_t turnstile_zone;
153
154/*
155 * Prototypes for non-exported routines.
156 */
157static void	init_turnstile0(void *dummy);
158#ifdef TURNSTILE_PROFILING
159static void	init_turnstile_profiling(void *arg);
160#endif
161static void	propagate_priority(struct thread *td);
162static int	turnstile_adjust_thread(struct turnstile *ts,
163		    struct thread *td);
164static struct thread *turnstile_first_waiter(struct turnstile *ts);
165static void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
166#ifdef INVARIANTS
167static void	turnstile_dtor(void *mem, int size, void *arg);
168#endif
169static int	turnstile_init(void *mem, int size, int flags);
170static void	turnstile_fini(void *mem, int size);
171
172SDT_PROVIDER_DECLARE(sched);
173SDT_PROBE_DEFINE(sched, , , sleep, sleep);
174SDT_PROBE_DEFINE2(sched, , , wakeup, wakeup, "struct thread *",
175    "struct proc *");
176
177/*
178 * Walks the chain of turnstiles and their owners to propagate the priority
179 * of the thread being blocked to all the threads holding locks that have to
180 * release their locks before this thread can run again.
181 */
182static void
183propagate_priority(struct thread *td)
184{
185	struct turnstile *ts;
186	int pri;
187
188	THREAD_LOCK_ASSERT(td, MA_OWNED);
189	pri = td->td_priority;
190	ts = td->td_blocked;
191	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
192	/*
193	 * Grab a recursive lock on this turnstile chain so it stays locked
194	 * for the whole operation.  The caller expects us to return with
195	 * the original lock held.  We only ever lock down the chain so
196	 * the lock order is constant.
197	 */
198	mtx_lock_spin(&ts->ts_lock);
199	for (;;) {
200		td = ts->ts_owner;
201
202		if (td == NULL) {
203			/*
204			 * This might be a read lock with no owner.  There's
205			 * not much we can do, so just bail.
206			 */
207			mtx_unlock_spin(&ts->ts_lock);
208			return;
209		}
210
211		thread_lock_flags(td, MTX_DUPOK);
212		mtx_unlock_spin(&ts->ts_lock);
213		MPASS(td->td_proc != NULL);
214		MPASS(td->td_proc->p_magic == P_MAGIC);
215
216		/*
217		 * If the thread is asleep, then we are probably about
218		 * to deadlock.  To make debugging this easier, just
219		 * panic and tell the user which thread misbehaved so
220		 * they can hopefully get a stack trace from the truly
221		 * misbehaving thread.
222		 */
223		if (TD_IS_SLEEPING(td)) {
224			printf(
225		"Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
226			    td->td_tid, td->td_proc->p_pid);
227			kdb_backtrace_thread(td);
228			panic("sleeping thread");
229		}
230
231		/*
232		 * If this thread already has higher priority than the
233		 * thread that is being blocked, we are finished.
234		 */
235		if (td->td_priority <= pri) {
236			thread_unlock(td);
237			return;
238		}
239
240		/*
241		 * Bump this thread's priority.
242		 */
243		sched_lend_prio(td, pri);
244
245		/*
246		 * If lock holder is actually running or on the run queue
247		 * then we are done.
248		 */
249		if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
250			MPASS(td->td_blocked == NULL);
251			thread_unlock(td);
252			return;
253		}
254
255#ifndef SMP
256		/*
257		 * For UP, we check to see if td is curthread (this shouldn't
258		 * ever happen however as it would mean we are in a deadlock.)
259		 */
260		KASSERT(td != curthread, ("Deadlock detected"));
261#endif
262
263		/*
264		 * If we aren't blocked on a lock, we should be.
265		 */
266		KASSERT(TD_ON_LOCK(td), (
267		    "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
268		    td->td_tid, td->td_name, td->td_state,
269		    ts->ts_lockobj->lo_name));
270
271		/*
272		 * Pick up the lock that td is blocked on.
273		 */
274		ts = td->td_blocked;
275		MPASS(ts != NULL);
276		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
277		/* Resort td on the list if needed. */
278		if (!turnstile_adjust_thread(ts, td)) {
279			mtx_unlock_spin(&ts->ts_lock);
280			return;
281		}
282		/* The thread lock is released as ts lock above. */
283	}
284}
285
286/*
287 * Adjust the thread's position on a turnstile after its priority has been
288 * changed.
289 */
290static int
291turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
292{
293	struct thread *td1, *td2;
294	int queue;
295
296	THREAD_LOCK_ASSERT(td, MA_OWNED);
297	MPASS(TD_ON_LOCK(td));
298
299	/*
300	 * This thread may not be blocked on this turnstile anymore
301	 * but instead might already be woken up on another CPU
302	 * that is waiting on the thread lock in turnstile_unpend() to
303	 * finish waking this thread up.  We can detect this case
304	 * by checking to see if this thread has been given a
305	 * turnstile by either turnstile_signal() or
306	 * turnstile_broadcast().  In this case, treat the thread as
307	 * if it was already running.
308	 */
309	if (td->td_turnstile != NULL)
310		return (0);
311
312	/*
313	 * Check if the thread needs to be moved on the blocked chain.
314	 * It needs to be moved if either its priority is lower than
315	 * the previous thread or higher than the next thread.
316	 */
317	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
318	td1 = TAILQ_PREV(td, threadqueue, td_lockq);
319	td2 = TAILQ_NEXT(td, td_lockq);
320	if ((td1 != NULL && td->td_priority < td1->td_priority) ||
321	    (td2 != NULL && td->td_priority > td2->td_priority)) {
322
323		/*
324		 * Remove thread from blocked chain and determine where
325		 * it should be moved to.
326		 */
327		queue = td->td_tsqueue;
328		MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
329		mtx_lock_spin(&td_contested_lock);
330		TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
331		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
332			MPASS(td1->td_proc->p_magic == P_MAGIC);
333			if (td1->td_priority > td->td_priority)
334				break;
335		}
336
337		if (td1 == NULL)
338			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
339		else
340			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
341		mtx_unlock_spin(&td_contested_lock);
342		if (td1 == NULL)
343			CTR3(KTR_LOCK,
344		    "turnstile_adjust_thread: td %d put at tail on [%p] %s",
345			    td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
346		else
347			CTR4(KTR_LOCK,
348		    "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
349			    td->td_tid, td1->td_tid, ts->ts_lockobj,
350			    ts->ts_lockobj->lo_name);
351	}
352	return (1);
353}
354
355/*
356 * Early initialization of turnstiles.  This is not done via a SYSINIT()
357 * since this needs to be initialized very early when mutexes are first
358 * initialized.
359 */
360void
361init_turnstiles(void)
362{
363	int i;
364
365	for (i = 0; i < TC_TABLESIZE; i++) {
366		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
367		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
368		    NULL, MTX_SPIN);
369	}
370	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
371	LIST_INIT(&thread0.td_contested);
372	thread0.td_turnstile = NULL;
373}
374
375#ifdef TURNSTILE_PROFILING
376static void
377init_turnstile_profiling(void *arg)
378{
379	struct sysctl_oid *chain_oid;
380	char chain_name[10];
381	int i;
382
383	for (i = 0; i < TC_TABLESIZE; i++) {
384		snprintf(chain_name, sizeof(chain_name), "%d", i);
385		chain_oid = SYSCTL_ADD_NODE(NULL,
386		    SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
387		    chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
388		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
389		    "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
390		    NULL);
391		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
392		    "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
393		    0, NULL);
394	}
395}
396SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
397    init_turnstile_profiling, NULL);
398#endif
399
400static void
401init_turnstile0(void *dummy)
402{
403
404	turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
405	    NULL,
406#ifdef INVARIANTS
407	    turnstile_dtor,
408#else
409	    NULL,
410#endif
411	    turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
412	thread0.td_turnstile = turnstile_alloc();
413}
414SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
415
416/*
417 * Update a thread on the turnstile list after it's priority has been changed.
418 * The old priority is passed in as an argument.
419 */
420void
421turnstile_adjust(struct thread *td, u_char oldpri)
422{
423	struct turnstile *ts;
424
425	MPASS(TD_ON_LOCK(td));
426
427	/*
428	 * Pick up the lock that td is blocked on.
429	 */
430	ts = td->td_blocked;
431	MPASS(ts != NULL);
432	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
433	mtx_assert(&ts->ts_lock, MA_OWNED);
434
435	/* Resort the turnstile on the list. */
436	if (!turnstile_adjust_thread(ts, td))
437		return;
438	/*
439	 * If our priority was lowered and we are at the head of the
440	 * turnstile, then propagate our new priority up the chain.
441	 * Note that we currently don't try to revoke lent priorities
442	 * when our priority goes up.
443	 */
444	MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
445	    td->td_tsqueue == TS_SHARED_QUEUE);
446	if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
447	    td->td_priority < oldpri) {
448		propagate_priority(td);
449	}
450}
451
452/*
453 * Set the owner of the lock this turnstile is attached to.
454 */
455static void
456turnstile_setowner(struct turnstile *ts, struct thread *owner)
457{
458
459	mtx_assert(&td_contested_lock, MA_OWNED);
460	MPASS(ts->ts_owner == NULL);
461
462	/* A shared lock might not have an owner. */
463	if (owner == NULL)
464		return;
465
466	MPASS(owner->td_proc->p_magic == P_MAGIC);
467	ts->ts_owner = owner;
468	LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
469}
470
471#ifdef INVARIANTS
472/*
473 * UMA zone item deallocator.
474 */
475static void
476turnstile_dtor(void *mem, int size, void *arg)
477{
478	struct turnstile *ts;
479
480	ts = mem;
481	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
482	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
483	MPASS(TAILQ_EMPTY(&ts->ts_pending));
484}
485#endif
486
487/*
488 * UMA zone item initializer.
489 */
490static int
491turnstile_init(void *mem, int size, int flags)
492{
493	struct turnstile *ts;
494
495	bzero(mem, size);
496	ts = mem;
497	TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
498	TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
499	TAILQ_INIT(&ts->ts_pending);
500	LIST_INIT(&ts->ts_free);
501	mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN | MTX_RECURSE);
502	return (0);
503}
504
505static void
506turnstile_fini(void *mem, int size)
507{
508	struct turnstile *ts;
509
510	ts = mem;
511	mtx_destroy(&ts->ts_lock);
512}
513
514/*
515 * Get a turnstile for a new thread.
516 */
517struct turnstile *
518turnstile_alloc(void)
519{
520
521	return (uma_zalloc(turnstile_zone, M_WAITOK));
522}
523
524/*
525 * Free a turnstile when a thread is destroyed.
526 */
527void
528turnstile_free(struct turnstile *ts)
529{
530
531	uma_zfree(turnstile_zone, ts);
532}
533
534/*
535 * Lock the turnstile chain associated with the specified lock.
536 */
537void
538turnstile_chain_lock(struct lock_object *lock)
539{
540	struct turnstile_chain *tc;
541
542	tc = TC_LOOKUP(lock);
543	mtx_lock_spin(&tc->tc_lock);
544}
545
546struct turnstile *
547turnstile_trywait(struct lock_object *lock)
548{
549	struct turnstile_chain *tc;
550	struct turnstile *ts;
551
552	tc = TC_LOOKUP(lock);
553	mtx_lock_spin(&tc->tc_lock);
554	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
555		if (ts->ts_lockobj == lock) {
556			mtx_lock_spin(&ts->ts_lock);
557			return (ts);
558		}
559
560	ts = curthread->td_turnstile;
561	MPASS(ts != NULL);
562	mtx_lock_spin(&ts->ts_lock);
563	KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
564	ts->ts_lockobj = lock;
565
566	return (ts);
567}
568
569void
570turnstile_cancel(struct turnstile *ts)
571{
572	struct turnstile_chain *tc;
573	struct lock_object *lock;
574
575	mtx_assert(&ts->ts_lock, MA_OWNED);
576
577	mtx_unlock_spin(&ts->ts_lock);
578	lock = ts->ts_lockobj;
579	if (ts == curthread->td_turnstile)
580		ts->ts_lockobj = NULL;
581	tc = TC_LOOKUP(lock);
582	mtx_unlock_spin(&tc->tc_lock);
583}
584
585/*
586 * Look up the turnstile for a lock in the hash table locking the associated
587 * turnstile chain along the way.  If no turnstile is found in the hash
588 * table, NULL is returned.
589 */
590struct turnstile *
591turnstile_lookup(struct lock_object *lock)
592{
593	struct turnstile_chain *tc;
594	struct turnstile *ts;
595
596	tc = TC_LOOKUP(lock);
597	mtx_assert(&tc->tc_lock, MA_OWNED);
598	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
599		if (ts->ts_lockobj == lock) {
600			mtx_lock_spin(&ts->ts_lock);
601			return (ts);
602		}
603	return (NULL);
604}
605
606/*
607 * Unlock the turnstile chain associated with a given lock.
608 */
609void
610turnstile_chain_unlock(struct lock_object *lock)
611{
612	struct turnstile_chain *tc;
613
614	tc = TC_LOOKUP(lock);
615	mtx_unlock_spin(&tc->tc_lock);
616}
617
618/*
619 * Return a pointer to the thread waiting on this turnstile with the
620 * most important priority or NULL if the turnstile has no waiters.
621 */
622static struct thread *
623turnstile_first_waiter(struct turnstile *ts)
624{
625	struct thread *std, *xtd;
626
627	std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]);
628	xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
629	if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority))
630		return (std);
631	return (xtd);
632}
633
634/*
635 * Take ownership of a turnstile and adjust the priority of the new
636 * owner appropriately.
637 */
638void
639turnstile_claim(struct turnstile *ts)
640{
641	struct thread *td, *owner;
642	struct turnstile_chain *tc;
643
644	mtx_assert(&ts->ts_lock, MA_OWNED);
645	MPASS(ts != curthread->td_turnstile);
646
647	owner = curthread;
648	mtx_lock_spin(&td_contested_lock);
649	turnstile_setowner(ts, owner);
650	mtx_unlock_spin(&td_contested_lock);
651
652	td = turnstile_first_waiter(ts);
653	MPASS(td != NULL);
654	MPASS(td->td_proc->p_magic == P_MAGIC);
655	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
656
657	/*
658	 * Update the priority of the new owner if needed.
659	 */
660	thread_lock(owner);
661	if (td->td_priority < owner->td_priority)
662		sched_lend_prio(owner, td->td_priority);
663	thread_unlock(owner);
664	tc = TC_LOOKUP(ts->ts_lockobj);
665	mtx_unlock_spin(&ts->ts_lock);
666	mtx_unlock_spin(&tc->tc_lock);
667}
668
669/*
670 * Block the current thread on the turnstile assicated with 'lock'.  This
671 * function will context switch and not return until this thread has been
672 * woken back up.  This function must be called with the appropriate
673 * turnstile chain locked and will return with it unlocked.
674 */
675void
676turnstile_wait(struct turnstile *ts, struct thread *owner, int queue)
677{
678	struct turnstile_chain *tc;
679	struct thread *td, *td1;
680	struct lock_object *lock;
681
682	td = curthread;
683	mtx_assert(&ts->ts_lock, MA_OWNED);
684	if (owner)
685		MPASS(owner->td_proc->p_magic == P_MAGIC);
686	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
687	KASSERT(!TD_IS_IDLETHREAD(td), ("idle threads cannot block on locks"));
688
689	/*
690	 * If the lock does not already have a turnstile, use this thread's
691	 * turnstile.  Otherwise insert the current thread into the
692	 * turnstile already in use by this lock.
693	 */
694	tc = TC_LOOKUP(ts->ts_lockobj);
695	mtx_assert(&tc->tc_lock, MA_OWNED);
696	if (ts == td->td_turnstile) {
697#ifdef TURNSTILE_PROFILING
698		tc->tc_depth++;
699		if (tc->tc_depth > tc->tc_max_depth) {
700			tc->tc_max_depth = tc->tc_depth;
701			if (tc->tc_max_depth > turnstile_max_depth)
702				turnstile_max_depth = tc->tc_max_depth;
703		}
704#endif
705		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
706		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
707		    ("thread's turnstile has pending threads"));
708		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
709		    ("thread's turnstile has exclusive waiters"));
710		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
711		    ("thread's turnstile has shared waiters"));
712		KASSERT(LIST_EMPTY(&ts->ts_free),
713		    ("thread's turnstile has a non-empty free list"));
714		MPASS(ts->ts_lockobj != NULL);
715		mtx_lock_spin(&td_contested_lock);
716		TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
717		turnstile_setowner(ts, owner);
718		mtx_unlock_spin(&td_contested_lock);
719	} else {
720		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
721			if (td1->td_priority > td->td_priority)
722				break;
723		mtx_lock_spin(&td_contested_lock);
724		if (td1 != NULL)
725			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
726		else
727			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
728		MPASS(owner == ts->ts_owner);
729		mtx_unlock_spin(&td_contested_lock);
730		MPASS(td->td_turnstile != NULL);
731		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
732	}
733	thread_lock(td);
734	thread_lock_set(td, &ts->ts_lock);
735	td->td_turnstile = NULL;
736
737	/* Save who we are blocked on and switch. */
738	lock = ts->ts_lockobj;
739	td->td_tsqueue = queue;
740	td->td_blocked = ts;
741	td->td_lockname = lock->lo_name;
742	td->td_blktick = ticks;
743	TD_SET_LOCK(td);
744	mtx_unlock_spin(&tc->tc_lock);
745	propagate_priority(td);
746
747	if (LOCK_LOG_TEST(lock, 0))
748		CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
749		    td->td_tid, lock, lock->lo_name);
750
751	SDT_PROBE0(sched, , , sleep);
752
753	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
754	mi_switch(SW_VOL | SWT_TURNSTILE, NULL);
755
756	if (LOCK_LOG_TEST(lock, 0))
757		CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
758		    __func__, td->td_tid, lock, lock->lo_name);
759	thread_unlock(td);
760}
761
762/*
763 * Pick the highest priority thread on this turnstile and put it on the
764 * pending list.  This must be called with the turnstile chain locked.
765 */
766int
767turnstile_signal(struct turnstile *ts, int queue)
768{
769	struct turnstile_chain *tc;
770	struct thread *td;
771	int empty;
772
773	MPASS(ts != NULL);
774	mtx_assert(&ts->ts_lock, MA_OWNED);
775	MPASS(curthread->td_proc->p_magic == P_MAGIC);
776	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
777	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
778
779	/*
780	 * Pick the highest priority thread blocked on this lock and
781	 * move it to the pending list.
782	 */
783	td = TAILQ_FIRST(&ts->ts_blocked[queue]);
784	MPASS(td->td_proc->p_magic == P_MAGIC);
785	mtx_lock_spin(&td_contested_lock);
786	TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
787	mtx_unlock_spin(&td_contested_lock);
788	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
789
790	/*
791	 * If the turnstile is now empty, remove it from its chain and
792	 * give it to the about-to-be-woken thread.  Otherwise take a
793	 * turnstile from the free list and give it to the thread.
794	 */
795	empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
796	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
797	if (empty) {
798		tc = TC_LOOKUP(ts->ts_lockobj);
799		mtx_assert(&tc->tc_lock, MA_OWNED);
800		MPASS(LIST_EMPTY(&ts->ts_free));
801#ifdef TURNSTILE_PROFILING
802		tc->tc_depth--;
803#endif
804	} else
805		ts = LIST_FIRST(&ts->ts_free);
806	MPASS(ts != NULL);
807	LIST_REMOVE(ts, ts_hash);
808	td->td_turnstile = ts;
809
810	return (empty);
811}
812
813/*
814 * Put all blocked threads on the pending list.  This must be called with
815 * the turnstile chain locked.
816 */
817void
818turnstile_broadcast(struct turnstile *ts, int queue)
819{
820	struct turnstile_chain *tc;
821	struct turnstile *ts1;
822	struct thread *td;
823
824	MPASS(ts != NULL);
825	mtx_assert(&ts->ts_lock, MA_OWNED);
826	MPASS(curthread->td_proc->p_magic == P_MAGIC);
827	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
828	/*
829	 * We must have the chain locked so that we can remove the empty
830	 * turnstile from the hash queue.
831	 */
832	tc = TC_LOOKUP(ts->ts_lockobj);
833	mtx_assert(&tc->tc_lock, MA_OWNED);
834	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
835
836	/*
837	 * Transfer the blocked list to the pending list.
838	 */
839	mtx_lock_spin(&td_contested_lock);
840	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
841	mtx_unlock_spin(&td_contested_lock);
842
843	/*
844	 * Give a turnstile to each thread.  The last thread gets
845	 * this turnstile if the turnstile is empty.
846	 */
847	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
848		if (LIST_EMPTY(&ts->ts_free)) {
849			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
850			ts1 = ts;
851#ifdef TURNSTILE_PROFILING
852			tc->tc_depth--;
853#endif
854		} else
855			ts1 = LIST_FIRST(&ts->ts_free);
856		MPASS(ts1 != NULL);
857		LIST_REMOVE(ts1, ts_hash);
858		td->td_turnstile = ts1;
859	}
860}
861
862/*
863 * Wakeup all threads on the pending list and adjust the priority of the
864 * current thread appropriately.  This must be called with the turnstile
865 * chain locked.
866 */
867void
868turnstile_unpend(struct turnstile *ts, int owner_type)
869{
870	TAILQ_HEAD( ,thread) pending_threads;
871	struct turnstile *nts;
872	struct thread *td;
873	u_char cp, pri;
874
875	MPASS(ts != NULL);
876	mtx_assert(&ts->ts_lock, MA_OWNED);
877	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
878	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
879
880	/*
881	 * Move the list of pending threads out of the turnstile and
882	 * into a local variable.
883	 */
884	TAILQ_INIT(&pending_threads);
885	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
886#ifdef INVARIANTS
887	if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
888	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
889		ts->ts_lockobj = NULL;
890#endif
891	/*
892	 * Adjust the priority of curthread based on other contested
893	 * locks it owns.  Don't lower the priority below the base
894	 * priority however.
895	 */
896	td = curthread;
897	pri = PRI_MAX;
898	thread_lock(td);
899	mtx_lock_spin(&td_contested_lock);
900	/*
901	 * Remove the turnstile from this thread's list of contested locks
902	 * since this thread doesn't own it anymore.  New threads will
903	 * not be blocking on the turnstile until it is claimed by a new
904	 * owner.  There might not be a current owner if this is a shared
905	 * lock.
906	 */
907	if (ts->ts_owner != NULL) {
908		ts->ts_owner = NULL;
909		LIST_REMOVE(ts, ts_link);
910	}
911	LIST_FOREACH(nts, &td->td_contested, ts_link) {
912		cp = turnstile_first_waiter(nts)->td_priority;
913		if (cp < pri)
914			pri = cp;
915	}
916	mtx_unlock_spin(&td_contested_lock);
917	sched_unlend_prio(td, pri);
918	thread_unlock(td);
919	/*
920	 * Wake up all the pending threads.  If a thread is not blocked
921	 * on a lock, then it is currently executing on another CPU in
922	 * turnstile_wait() or sitting on a run queue waiting to resume
923	 * in turnstile_wait().  Set a flag to force it to try to acquire
924	 * the lock again instead of blocking.
925	 */
926	while (!TAILQ_EMPTY(&pending_threads)) {
927		td = TAILQ_FIRST(&pending_threads);
928		TAILQ_REMOVE(&pending_threads, td, td_lockq);
929		SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
930		thread_lock(td);
931		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
932		MPASS(td->td_proc->p_magic == P_MAGIC);
933		MPASS(TD_ON_LOCK(td));
934		TD_CLR_LOCK(td);
935		MPASS(TD_CAN_RUN(td));
936		td->td_blocked = NULL;
937		td->td_lockname = NULL;
938		td->td_blktick = 0;
939#ifdef INVARIANTS
940		td->td_tsqueue = 0xff;
941#endif
942		sched_add(td, SRQ_BORING);
943		thread_unlock(td);
944	}
945	mtx_unlock_spin(&ts->ts_lock);
946}
947
948/*
949 * Give up ownership of a turnstile.  This must be called with the
950 * turnstile chain locked.
951 */
952void
953turnstile_disown(struct turnstile *ts)
954{
955	struct thread *td;
956	u_char cp, pri;
957
958	MPASS(ts != NULL);
959	mtx_assert(&ts->ts_lock, MA_OWNED);
960	MPASS(ts->ts_owner == curthread);
961	MPASS(TAILQ_EMPTY(&ts->ts_pending));
962	MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
963	    !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
964
965	/*
966	 * Remove the turnstile from this thread's list of contested locks
967	 * since this thread doesn't own it anymore.  New threads will
968	 * not be blocking on the turnstile until it is claimed by a new
969	 * owner.
970	 */
971	mtx_lock_spin(&td_contested_lock);
972	ts->ts_owner = NULL;
973	LIST_REMOVE(ts, ts_link);
974	mtx_unlock_spin(&td_contested_lock);
975
976	/*
977	 * Adjust the priority of curthread based on other contested
978	 * locks it owns.  Don't lower the priority below the base
979	 * priority however.
980	 */
981	td = curthread;
982	pri = PRI_MAX;
983	thread_lock(td);
984	mtx_unlock_spin(&ts->ts_lock);
985	mtx_lock_spin(&td_contested_lock);
986	LIST_FOREACH(ts, &td->td_contested, ts_link) {
987		cp = turnstile_first_waiter(ts)->td_priority;
988		if (cp < pri)
989			pri = cp;
990	}
991	mtx_unlock_spin(&td_contested_lock);
992	sched_unlend_prio(td, pri);
993	thread_unlock(td);
994}
995
996/*
997 * Return the first thread in a turnstile.
998 */
999struct thread *
1000turnstile_head(struct turnstile *ts, int queue)
1001{
1002#ifdef INVARIANTS
1003
1004	MPASS(ts != NULL);
1005	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1006	mtx_assert(&ts->ts_lock, MA_OWNED);
1007#endif
1008	return (TAILQ_FIRST(&ts->ts_blocked[queue]));
1009}
1010
1011/*
1012 * Returns true if a sub-queue of a turnstile is empty.
1013 */
1014int
1015turnstile_empty(struct turnstile *ts, int queue)
1016{
1017#ifdef INVARIANTS
1018
1019	MPASS(ts != NULL);
1020	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1021	mtx_assert(&ts->ts_lock, MA_OWNED);
1022#endif
1023	return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
1024}
1025
1026#ifdef DDB
1027static void
1028print_thread(struct thread *td, const char *prefix)
1029{
1030
1031	db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid,
1032	    td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
1033	    td->td_name);
1034}
1035
1036static void
1037print_queue(struct threadqueue *queue, const char *header, const char *prefix)
1038{
1039	struct thread *td;
1040
1041	db_printf("%s:\n", header);
1042	if (TAILQ_EMPTY(queue)) {
1043		db_printf("%sempty\n", prefix);
1044		return;
1045	}
1046	TAILQ_FOREACH(td, queue, td_lockq) {
1047		print_thread(td, prefix);
1048	}
1049}
1050
1051DB_SHOW_COMMAND(turnstile, db_show_turnstile)
1052{
1053	struct turnstile_chain *tc;
1054	struct turnstile *ts;
1055	struct lock_object *lock;
1056	int i;
1057
1058	if (!have_addr)
1059		return;
1060
1061	/*
1062	 * First, see if there is an active turnstile for the lock indicated
1063	 * by the address.
1064	 */
1065	lock = (struct lock_object *)addr;
1066	tc = TC_LOOKUP(lock);
1067	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1068		if (ts->ts_lockobj == lock)
1069			goto found;
1070
1071	/*
1072	 * Second, see if there is an active turnstile at the address
1073	 * indicated.
1074	 */
1075	for (i = 0; i < TC_TABLESIZE; i++)
1076		LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) {
1077			if (ts == (struct turnstile *)addr)
1078				goto found;
1079		}
1080
1081	db_printf("Unable to locate a turnstile via %p\n", (void *)addr);
1082	return;
1083found:
1084	lock = ts->ts_lockobj;
1085	db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name,
1086	    lock->lo_name);
1087	if (ts->ts_owner)
1088		print_thread(ts->ts_owner, "Lock Owner: ");
1089	else
1090		db_printf("Lock Owner: none\n");
1091	print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t");
1092	print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters",
1093	    "\t");
1094	print_queue(&ts->ts_pending, "Pending Threads", "\t");
1095
1096}
1097
1098/*
1099 * Show all the threads a particular thread is waiting on based on
1100 * non-sleepable and non-spin locks.
1101 */
1102static void
1103print_lockchain(struct thread *td, const char *prefix)
1104{
1105	struct lock_object *lock;
1106	struct lock_class *class;
1107	struct turnstile *ts;
1108
1109	/*
1110	 * Follow the chain.  We keep walking as long as the thread is
1111	 * blocked on a turnstile that has an owner.
1112	 */
1113	while (!db_pager_quit) {
1114		db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1115		    td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
1116		    td->td_name);
1117		switch (td->td_state) {
1118		case TDS_INACTIVE:
1119			db_printf("is inactive\n");
1120			return;
1121		case TDS_CAN_RUN:
1122			db_printf("can run\n");
1123			return;
1124		case TDS_RUNQ:
1125			db_printf("is on a run queue\n");
1126			return;
1127		case TDS_RUNNING:
1128			db_printf("running on CPU %d\n", td->td_oncpu);
1129			return;
1130		case TDS_INHIBITED:
1131			if (TD_ON_LOCK(td)) {
1132				ts = td->td_blocked;
1133				lock = ts->ts_lockobj;
1134				class = LOCK_CLASS(lock);
1135				db_printf("blocked on lock %p (%s) \"%s\"\n",
1136				    lock, class->lc_name, lock->lo_name);
1137				if (ts->ts_owner == NULL)
1138					return;
1139				td = ts->ts_owner;
1140				break;
1141			}
1142			db_printf("inhibited\n");
1143			return;
1144		default:
1145			db_printf("??? (%#x)\n", td->td_state);
1146			return;
1147		}
1148	}
1149}
1150
1151DB_SHOW_COMMAND(lockchain, db_show_lockchain)
1152{
1153	struct thread *td;
1154
1155	/* Figure out which thread to start with. */
1156	if (have_addr)
1157		td = db_lookup_thread(addr, TRUE);
1158	else
1159		td = kdb_thread;
1160
1161	print_lockchain(td, "");
1162}
1163
1164DB_SHOW_ALL_COMMAND(chains, db_show_allchains)
1165{
1166	struct thread *td;
1167	struct proc *p;
1168	int i;
1169
1170	i = 1;
1171	FOREACH_PROC_IN_SYSTEM(p) {
1172		FOREACH_THREAD_IN_PROC(p, td) {
1173			if (TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested)) {
1174				db_printf("chain %d:\n", i++);
1175				print_lockchain(td, " ");
1176			}
1177			if (db_pager_quit)
1178				return;
1179		}
1180	}
1181}
1182DB_SHOW_ALIAS(allchains, db_show_allchains)
1183
1184/*
1185 * Show all the threads a particular thread is waiting on based on
1186 * sleepable locks.
1187 */
1188static void
1189print_sleepchain(struct thread *td, const char *prefix)
1190{
1191	struct thread *owner;
1192
1193	/*
1194	 * Follow the chain.  We keep walking as long as the thread is
1195	 * blocked on a sleep lock that has an owner.
1196	 */
1197	while (!db_pager_quit) {
1198		db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1199		    td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
1200		    td->td_name);
1201		switch (td->td_state) {
1202		case TDS_INACTIVE:
1203			db_printf("is inactive\n");
1204			return;
1205		case TDS_CAN_RUN:
1206			db_printf("can run\n");
1207			return;
1208		case TDS_RUNQ:
1209			db_printf("is on a run queue\n");
1210			return;
1211		case TDS_RUNNING:
1212			db_printf("running on CPU %d\n", td->td_oncpu);
1213			return;
1214		case TDS_INHIBITED:
1215			if (TD_ON_SLEEPQ(td)) {
1216				if (lockmgr_chain(td, &owner) ||
1217				    sx_chain(td, &owner)) {
1218					if (owner == NULL)
1219						return;
1220					td = owner;
1221					break;
1222				}
1223				db_printf("sleeping on %p \"%s\"\n",
1224				    td->td_wchan, td->td_wmesg);
1225				return;
1226			}
1227			db_printf("inhibited\n");
1228			return;
1229		default:
1230			db_printf("??? (%#x)\n", td->td_state);
1231			return;
1232		}
1233	}
1234}
1235
1236DB_SHOW_COMMAND(sleepchain, db_show_sleepchain)
1237{
1238	struct thread *td;
1239
1240	/* Figure out which thread to start with. */
1241	if (have_addr)
1242		td = db_lookup_thread(addr, TRUE);
1243	else
1244		td = kdb_thread;
1245
1246	print_sleepchain(td, "");
1247}
1248
1249static void	print_waiters(struct turnstile *ts, int indent);
1250
1251static void
1252print_waiter(struct thread *td, int indent)
1253{
1254	struct turnstile *ts;
1255	int i;
1256
1257	if (db_pager_quit)
1258		return;
1259	for (i = 0; i < indent; i++)
1260		db_printf(" ");
1261	print_thread(td, "thread ");
1262	LIST_FOREACH(ts, &td->td_contested, ts_link)
1263		print_waiters(ts, indent + 1);
1264}
1265
1266static void
1267print_waiters(struct turnstile *ts, int indent)
1268{
1269	struct lock_object *lock;
1270	struct lock_class *class;
1271	struct thread *td;
1272	int i;
1273
1274	if (db_pager_quit)
1275		return;
1276	lock = ts->ts_lockobj;
1277	class = LOCK_CLASS(lock);
1278	for (i = 0; i < indent; i++)
1279		db_printf(" ");
1280	db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
1281	TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
1282		print_waiter(td, indent + 1);
1283	TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
1284		print_waiter(td, indent + 1);
1285	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
1286		print_waiter(td, indent + 1);
1287}
1288
1289DB_SHOW_COMMAND(locktree, db_show_locktree)
1290{
1291	struct lock_object *lock;
1292	struct lock_class *class;
1293	struct turnstile_chain *tc;
1294	struct turnstile *ts;
1295
1296	if (!have_addr)
1297		return;
1298	lock = (struct lock_object *)addr;
1299	tc = TC_LOOKUP(lock);
1300	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1301		if (ts->ts_lockobj == lock)
1302			break;
1303	if (ts == NULL) {
1304		class = LOCK_CLASS(lock);
1305		db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
1306		    lock->lo_name);
1307	} else
1308		print_waiters(ts, 0);
1309}
1310#endif
1311