xref: /freebsd/sys/kern/subr_turnstile.c (revision aea9dba46b81abc5c0fd4c42d3ac762d3bc1de37)
10384fff8SJason Evans /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a36da99SPedro F. Giffuni  *
40384fff8SJason Evans  * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
50384fff8SJason Evans  *
60384fff8SJason Evans  * Redistribution and use in source and binary forms, with or without
70384fff8SJason Evans  * modification, are permitted provided that the following conditions
80384fff8SJason Evans  * are met:
90384fff8SJason Evans  * 1. Redistributions of source code must retain the above copyright
100384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer.
110384fff8SJason Evans  * 2. Redistributions in binary form must reproduce the above copyright
120384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer in the
130384fff8SJason Evans  *    documentation and/or other materials provided with the distribution.
140384fff8SJason Evans  * 3. Berkeley Software Design Inc's name may not be used to endorse or
150384fff8SJason Evans  *    promote products derived from this software without specific prior
160384fff8SJason Evans  *    written permission.
170384fff8SJason Evans  *
180384fff8SJason Evans  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
190384fff8SJason Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
200384fff8SJason Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
210384fff8SJason Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
220384fff8SJason Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
230384fff8SJason Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
240384fff8SJason Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
250384fff8SJason Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
260384fff8SJason Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
270384fff8SJason Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
280384fff8SJason Evans  * SUCH DAMAGE.
290384fff8SJason Evans  *
300384fff8SJason Evans  *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
3136412d79SJohn Baldwin  *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
320384fff8SJason Evans  */
330384fff8SJason Evans 
340384fff8SJason Evans /*
35961a7b24SJohn Baldwin  * Implementation of turnstiles used to hold queue of threads blocked on
36961a7b24SJohn Baldwin  * non-sleepable locks.  Sleepable locks use condition variables to
37961a7b24SJohn Baldwin  * implement their queues.  Turnstiles differ from a sleep queue in that
38961a7b24SJohn Baldwin  * turnstile queue's are assigned to a lock held by an owning thread.  Thus,
39961a7b24SJohn Baldwin  * when one thread is enqueued onto a turnstile, it can lend its priority
40961a7b24SJohn Baldwin  * to the owning thread.
41961a7b24SJohn Baldwin  *
42961a7b24SJohn Baldwin  * We wish to avoid bloating locks with an embedded turnstile and we do not
43961a7b24SJohn Baldwin  * want to use back-pointers in the locks for the same reason.  Thus, we
44961a7b24SJohn Baldwin  * use a similar approach to that of Solaris 7 as described in Solaris
45961a7b24SJohn Baldwin  * Internals by Jim Mauro and Richard McDougall.  Turnstiles are looked up
46961a7b24SJohn Baldwin  * in a hash table based on the address of the lock.  Each entry in the
47961a7b24SJohn Baldwin  * hash table is a linked-lists of turnstiles and is called a turnstile
48961a7b24SJohn Baldwin  * chain.  Each chain contains a spin mutex that protects all of the
49961a7b24SJohn Baldwin  * turnstiles in the chain.
50961a7b24SJohn Baldwin  *
512b7e2ee7SJeff Roberson  * Each time a thread is created, a turnstile is allocated from a UMA zone
522b7e2ee7SJeff Roberson  * and attached to that thread.  When a thread blocks on a lock, if it is the
532b7e2ee7SJeff Roberson  * first thread to block, it lends its turnstile to the lock.  If the lock
542b7e2ee7SJeff Roberson  * already has a turnstile, then it gives its turnstile to the lock's
552b7e2ee7SJeff Roberson  * turnstile's free list.  When a thread is woken up, it takes a turnstile from
562b7e2ee7SJeff Roberson  * the free list if there are any other waiters.  If it is the only thread
572b7e2ee7SJeff Roberson  * blocked on the lock, then it reclaims the turnstile associated with the lock
582b7e2ee7SJeff Roberson  * and removes it from the hash table.
590384fff8SJason Evans  */
600384fff8SJason Evans 
61677b542eSDavid E. O'Brien #include <sys/cdefs.h>
627aa4f685SJohn Baldwin #include "opt_ddb.h"
637aa4f685SJohn Baldwin #include "opt_turnstile_profiling.h"
643036ab79SJeff Roberson #include "opt_sched.h"
657aa4f685SJohn Baldwin 
660384fff8SJason Evans #include <sys/param.h>
676c35e809SDag-Erling Smørgrav #include <sys/systm.h>
6891849f34SMarius Strobl #include <sys/kdb.h>
6936412d79SJohn Baldwin #include <sys/kernel.h>
706c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
7119284646SJohn Baldwin #include <sys/lock.h>
7219284646SJohn Baldwin #include <sys/mutex.h>
730384fff8SJason Evans #include <sys/proc.h>
74961a7b24SJohn Baldwin #include <sys/queue.h>
75b43179fbSJeff Roberson #include <sys/sched.h>
76b3e9e682SRyan Stone #include <sys/sdt.h>
77ef0ebfc3SJohn Baldwin #include <sys/sysctl.h>
78ef0ebfc3SJohn Baldwin #include <sys/turnstile.h>
7936412d79SJohn Baldwin 
802b7e2ee7SJeff Roberson #include <vm/uma.h>
812b7e2ee7SJeff Roberson 
827aa4f685SJohn Baldwin #ifdef DDB
837aa4f685SJohn Baldwin #include <ddb/ddb.h>
84462a7addSJohn Baldwin #include <sys/lockmgr.h>
85462a7addSJohn Baldwin #include <sys/sx.h>
867aa4f685SJohn Baldwin #endif
877aa4f685SJohn Baldwin 
880cde2e34SJason Evans /*
89961a7b24SJohn Baldwin  * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
90961a7b24SJohn Baldwin  * number chosen because the sleep queue's use the same value for the
91961a7b24SJohn Baldwin  * shift.  Basically, we ignore the lower 8 bits of the address.
92961a7b24SJohn Baldwin  * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
930cde2e34SJason Evans  */
94961a7b24SJohn Baldwin #define	TC_TABLESIZE	128			/* Must be power of 2. */
95961a7b24SJohn Baldwin #define	TC_MASK		(TC_TABLESIZE - 1)
96961a7b24SJohn Baldwin #define	TC_SHIFT	8
97961a7b24SJohn Baldwin #define	TC_HASH(lock)	(((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
98961a7b24SJohn Baldwin #define	TC_LOOKUP(lock)	&turnstile_chains[TC_HASH(lock)]
999ed346baSBosko Milekic 
1000cde2e34SJason Evans /*
101961a7b24SJohn Baldwin  * There are three different lists of turnstiles as follows.  The list
102961a7b24SJohn Baldwin  * connected by ts_link entries is a per-thread list of all the turnstiles
103961a7b24SJohn Baldwin  * attached to locks that we own.  This is used to fixup our priority when
104961a7b24SJohn Baldwin  * a lock is released.  The other two lists use the ts_hash entries.  The
1055b7de7e1SJohn Baldwin  * first of these two is the turnstile chain list that a turnstile is on
1065b7de7e1SJohn Baldwin  * when it is attached to a lock.  The second list to use ts_hash is the
1075b7de7e1SJohn Baldwin  * free list hung off of a turnstile that is attached to a lock.
108961a7b24SJohn Baldwin  *
1097aa4f685SJohn Baldwin  * Each turnstile contains three lists of threads.  The two ts_blocked lists
1107aa4f685SJohn Baldwin  * are linked list of threads blocked on the turnstile's lock.  One list is
1117aa4f685SJohn Baldwin  * for exclusive waiters, and the other is for shared waiters.  The
112595bc82aSJohn Baldwin  * ts_pending list is a linked list of threads previously awakened by
113961a7b24SJohn Baldwin  * turnstile_signal() or turnstile_wait() that are waiting to be put on
114961a7b24SJohn Baldwin  * the run queue.
115961a7b24SJohn Baldwin  *
116961a7b24SJohn Baldwin  * Locking key:
117961a7b24SJohn Baldwin  *  c - turnstile chain lock
118961a7b24SJohn Baldwin  *  q - td_contested lock
1190cde2e34SJason Evans  */
120961a7b24SJohn Baldwin struct turnstile {
1212502c107SJeff Roberson 	struct mtx ts_lock;			/* Spin lock for self. */
1227aa4f685SJohn Baldwin 	struct threadqueue ts_blocked[2];	/* (c + q) Blocked threads. */
1237aa4f685SJohn Baldwin 	struct threadqueue ts_pending;		/* (c) Pending threads. */
124961a7b24SJohn Baldwin 	LIST_ENTRY(turnstile) ts_hash;		/* (c) Chain and free list. */
125961a7b24SJohn Baldwin 	LIST_ENTRY(turnstile) ts_link;		/* (q) Contested locks. */
126961a7b24SJohn Baldwin 	LIST_HEAD(, turnstile) ts_free;		/* (c) Free turnstiles. */
127961a7b24SJohn Baldwin 	struct lock_object *ts_lockobj;		/* (c) Lock we reference. */
12879a13d01SJohn Baldwin 	struct thread *ts_owner;		/* (c + q) Who owns the lock. */
1298484de75SJohn Baldwin };
1308484de75SJohn Baldwin 
131961a7b24SJohn Baldwin struct turnstile_chain {
132961a7b24SJohn Baldwin 	LIST_HEAD(, turnstile) tc_turnstiles;	/* List of turnstiles. */
133961a7b24SJohn Baldwin 	struct mtx tc_lock;			/* Spin lock for this chain. */
134ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
135ef0ebfc3SJohn Baldwin 	u_int	tc_depth;			/* Length of tc_queues. */
136ef0ebfc3SJohn Baldwin 	u_int	tc_max_depth;			/* Max length of tc_queues. */
137ef0ebfc3SJohn Baldwin #endif
138961a7b24SJohn Baldwin };
139961a7b24SJohn Baldwin 
140ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
141ef0ebfc3SJohn Baldwin u_int turnstile_max_depth;
1427029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
1436472ac3dSEd Schouten     "turnstile profiling");
1447029da5cSPawel Biernacki static SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains,
1457029da5cSPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
146ef0ebfc3SJohn Baldwin     "turnstile chain stats");
147ef0ebfc3SJohn Baldwin SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
14899006d44SDavide Italiano     &turnstile_max_depth, 0, "maximum depth achieved of a single chain");
149ef0ebfc3SJohn Baldwin #endif
150961a7b24SJohn Baldwin static struct mtx td_contested_lock;
151961a7b24SJohn Baldwin static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
1522b7e2ee7SJeff Roberson static uma_zone_t turnstile_zone;
153c53c013bSJohn Baldwin 
154c53c013bSJohn Baldwin /*
1559ed346baSBosko Milekic  * Prototypes for non-exported routines.
1569ed346baSBosko Milekic  */
157961a7b24SJohn Baldwin static void	init_turnstile0(void *dummy);
15801bd10e1SJohn Baldwin #ifdef TURNSTILE_PROFILING
15901bd10e1SJohn Baldwin static void	init_turnstile_profiling(void *arg);
16001bd10e1SJohn Baldwin #endif
161f5c157d9SJohn Baldwin static void	propagate_priority(struct thread *td);
162f5c157d9SJohn Baldwin static int	turnstile_adjust_thread(struct turnstile *ts,
163f5c157d9SJohn Baldwin 		    struct thread *td);
1647aa4f685SJohn Baldwin static struct thread *turnstile_first_waiter(struct turnstile *ts);
165961a7b24SJohn Baldwin static void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
1662b7e2ee7SJeff Roberson #ifdef INVARIANTS
1672b7e2ee7SJeff Roberson static void	turnstile_dtor(void *mem, int size, void *arg);
1682b7e2ee7SJeff Roberson #endif
1692b7e2ee7SJeff Roberson static int	turnstile_init(void *mem, int size, int flags);
1702502c107SJeff Roberson static void	turnstile_fini(void *mem, int size);
17136412d79SJohn Baldwin 
172b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(sched);
173d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(sched, , , sleep);
174d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , wakeup, "struct thread *",
175b3e9e682SRyan Stone     "struct proc *");
176b3e9e682SRyan Stone 
1771c81a87eSJeff Roberson static inline void
propagate_unlock_ts(struct turnstile * top,struct turnstile * ts)1781c81a87eSJeff Roberson propagate_unlock_ts(struct turnstile *top, struct turnstile *ts)
1791c81a87eSJeff Roberson {
1801c81a87eSJeff Roberson 
1811c81a87eSJeff Roberson 	if (ts != top)
1821c81a87eSJeff Roberson 		mtx_unlock_spin(&ts->ts_lock);
1831c81a87eSJeff Roberson }
1841c81a87eSJeff Roberson 
1851c81a87eSJeff Roberson static inline void
propagate_unlock_td(struct turnstile * top,struct thread * td)1861c81a87eSJeff Roberson propagate_unlock_td(struct turnstile *top, struct thread *td)
1871c81a87eSJeff Roberson {
1881c81a87eSJeff Roberson 
1891c81a87eSJeff Roberson 	if (td->td_lock != &top->ts_lock)
1901c81a87eSJeff Roberson 		thread_unlock(td);
1911c81a87eSJeff Roberson }
1921c81a87eSJeff Roberson 
193961a7b24SJohn Baldwin /*
194961a7b24SJohn Baldwin  * Walks the chain of turnstiles and their owners to propagate the priority
195961a7b24SJohn Baldwin  * of the thread being blocked to all the threads holding locks that have to
196961a7b24SJohn Baldwin  * release their locks before this thread can run again.
197961a7b24SJohn Baldwin  */
19836412d79SJohn Baldwin static void
propagate_priority(struct thread * td)199b40ce416SJulian Elischer propagate_priority(struct thread *td)
20036412d79SJohn Baldwin {
2011c81a87eSJeff Roberson 	struct turnstile *ts, *top;
202961a7b24SJohn Baldwin 	int pri;
20336412d79SJohn Baldwin 
2042502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
205961a7b24SJohn Baldwin 	pri = td->td_priority;
2061c81a87eSJeff Roberson 	top = ts = td->td_blocked;
207626ac252SJeff Roberson 	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
2081c81a87eSJeff Roberson 
2092502c107SJeff Roberson 	/*
2101c81a87eSJeff Roberson 	 * The original turnstile lock is held across the entire
2111c81a87eSJeff Roberson 	 * operation.  We only ever lock down the chain so the lock
2121c81a87eSJeff Roberson 	 * order is constant.
2132502c107SJeff Roberson 	 */
21436412d79SJohn Baldwin 	for (;;) {
215961a7b24SJohn Baldwin 		td = ts->ts_owner;
21636412d79SJohn Baldwin 
217b40ce416SJulian Elischer 		if (td == NULL) {
21836412d79SJohn Baldwin 			/*
2197aa4f685SJohn Baldwin 			 * This might be a read lock with no owner.  There's
2207aa4f685SJohn Baldwin 			 * not much we can do, so just bail.
22136412d79SJohn Baldwin 			 */
2221c81a87eSJeff Roberson 			propagate_unlock_ts(top, ts);
22336412d79SJohn Baldwin 			return;
22436412d79SJohn Baldwin 		}
2259ed346baSBosko Milekic 
2261c81a87eSJeff Roberson 		/*
2271c81a87eSJeff Roberson 		 * Wait for the thread lock to be stable and then only
2281c81a87eSJeff Roberson 		 * acquire if it is not the turnstile lock.
2291c81a87eSJeff Roberson 		 */
2301c81a87eSJeff Roberson 		thread_lock_block_wait(td);
2311c81a87eSJeff Roberson 		if (td->td_lock != &ts->ts_lock) {
2322502c107SJeff Roberson 			thread_lock_flags(td, MTX_DUPOK);
2331c81a87eSJeff Roberson 			propagate_unlock_ts(top, ts);
2341c81a87eSJeff Roberson 		}
235e602ba25SJulian Elischer 		MPASS(td->td_proc != NULL);
236b40ce416SJulian Elischer 		MPASS(td->td_proc->p_magic == P_MAGIC);
2371bd0eefbSJohn Baldwin 
238961a7b24SJohn Baldwin 		/*
2394b3b0413SJohn Baldwin 		 * If the thread is asleep, then we are probably about
240b2e054b0SPawel Jakub Dawidek 		 * to deadlock.  To make debugging this easier, show
241b2e054b0SPawel Jakub Dawidek 		 * backtrace of misbehaving thread and panic to not
242b2e054b0SPawel Jakub Dawidek 		 * leave the kernel deadlocked.
243961a7b24SJohn Baldwin 		 */
2444b3b0413SJohn Baldwin 		if (TD_IS_SLEEPING(td)) {
2454b3b0413SJohn Baldwin 			printf(
2464b3b0413SJohn Baldwin 		"Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
2474b3b0413SJohn Baldwin 			    td->td_tid, td->td_proc->p_pid);
2480cc457b0SJohn Baldwin 			kdb_backtrace_thread(td);
249*aea9dba4SMark Johnston 			panic("sleeping thread holds %s",
250*aea9dba4SMark Johnston 			    ts->ts_lockobj->lo_name);
2514b3b0413SJohn Baldwin 		}
252961a7b24SJohn Baldwin 
253961a7b24SJohn Baldwin 		/*
254961a7b24SJohn Baldwin 		 * If this thread already has higher priority than the
255961a7b24SJohn Baldwin 		 * thread that is being blocked, we are finished.
256961a7b24SJohn Baldwin 		 */
2572502c107SJeff Roberson 		if (td->td_priority <= pri) {
2581c81a87eSJeff Roberson 			propagate_unlock_td(top, td);
259961a7b24SJohn Baldwin 			return;
2602502c107SJeff Roberson 		}
2611bd0eefbSJohn Baldwin 
26236412d79SJohn Baldwin 		/*
263f5c157d9SJohn Baldwin 		 * Bump this thread's priority.
26436412d79SJohn Baldwin 		 */
265f5c157d9SJohn Baldwin 		sched_lend_prio(td, pri);
266f5c157d9SJohn Baldwin 
267f5c157d9SJohn Baldwin 		/*
268f5c157d9SJohn Baldwin 		 * If lock holder is actually running or on the run queue
269f5c157d9SJohn Baldwin 		 * then we are done.
270f5c157d9SJohn Baldwin 		 */
271f5c157d9SJohn Baldwin 		if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
272f5c157d9SJohn Baldwin 			MPASS(td->td_blocked == NULL);
2731c81a87eSJeff Roberson 			propagate_unlock_td(top, td);
27436412d79SJohn Baldwin 			return;
27536412d79SJohn Baldwin 		}
276d5a08a60SJake Burkholder 
2771b43703bSJohn Baldwin #ifndef SMP
2781b43703bSJohn Baldwin 		/*
279b40ce416SJulian Elischer 		 * For UP, we check to see if td is curthread (this shouldn't
2801b43703bSJohn Baldwin 		 * ever happen however as it would mean we are in a deadlock.)
2811b43703bSJohn Baldwin 		 */
282b40ce416SJulian Elischer 		KASSERT(td != curthread, ("Deadlock detected"));
2831b43703bSJohn Baldwin #endif
2841b43703bSJohn Baldwin 
28536412d79SJohn Baldwin 		/*
286961a7b24SJohn Baldwin 		 * If we aren't blocked on a lock, we should be.
28736412d79SJohn Baldwin 		 */
288551cf4e1SJohn Baldwin 		KASSERT(TD_ON_LOCK(td), (
289f5c157d9SJohn Baldwin 		    "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
290fa2528acSAlex Richardson 		    td->td_tid, td->td_name, TD_GET_STATE(td),
291961a7b24SJohn Baldwin 		    ts->ts_lockobj->lo_name));
29236412d79SJohn Baldwin 
29336412d79SJohn Baldwin 		/*
294961a7b24SJohn Baldwin 		 * Pick up the lock that td is blocked on.
29536412d79SJohn Baldwin 		 */
296961a7b24SJohn Baldwin 		ts = td->td_blocked;
297961a7b24SJohn Baldwin 		MPASS(ts != NULL);
298626ac252SJeff Roberson 		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
299f5c157d9SJohn Baldwin 		/* Resort td on the list if needed. */
300f5c157d9SJohn Baldwin 		if (!turnstile_adjust_thread(ts, td)) {
3011c81a87eSJeff Roberson 			propagate_unlock_ts(top, ts);
302f5c157d9SJohn Baldwin 			return;
303f5c157d9SJohn Baldwin 		}
3042502c107SJeff Roberson 		/* The thread lock is released as ts lock above. */
305f5c157d9SJohn Baldwin 	}
306f5c157d9SJohn Baldwin }
307f5c157d9SJohn Baldwin 
308f5c157d9SJohn Baldwin /*
309f5c157d9SJohn Baldwin  * Adjust the thread's position on a turnstile after its priority has been
310f5c157d9SJohn Baldwin  * changed.
311f5c157d9SJohn Baldwin  */
312f5c157d9SJohn Baldwin static int
turnstile_adjust_thread(struct turnstile * ts,struct thread * td)313f5c157d9SJohn Baldwin turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
314f5c157d9SJohn Baldwin {
315f5c157d9SJohn Baldwin 	struct thread *td1, *td2;
3167aa4f685SJohn Baldwin 	int queue;
317f5c157d9SJohn Baldwin 
3182502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
319f5c157d9SJohn Baldwin 	MPASS(TD_ON_LOCK(td));
320f5c157d9SJohn Baldwin 
32136412d79SJohn Baldwin 	/*
3226b6bd95eSJohn Baldwin 	 * This thread may not be blocked on this turnstile anymore
3236b6bd95eSJohn Baldwin 	 * but instead might already be woken up on another CPU
3242502c107SJeff Roberson 	 * that is waiting on the thread lock in turnstile_unpend() to
3256b6bd95eSJohn Baldwin 	 * finish waking this thread up.  We can detect this case
3266b6bd95eSJohn Baldwin 	 * by checking to see if this thread has been given a
3276b6bd95eSJohn Baldwin 	 * turnstile by either turnstile_signal() or
328ef2c0ba7SJohn Baldwin 	 * turnstile_broadcast().  In this case, treat the thread as
3296b6bd95eSJohn Baldwin 	 * if it was already running.
33079a13d01SJohn Baldwin 	 */
331f5c157d9SJohn Baldwin 	if (td->td_turnstile != NULL)
332f5c157d9SJohn Baldwin 		return (0);
33379a13d01SJohn Baldwin 
33479a13d01SJohn Baldwin 	/*
335f5c157d9SJohn Baldwin 	 * Check if the thread needs to be moved on the blocked chain.
336f5c157d9SJohn Baldwin 	 * It needs to be moved if either its priority is lower than
337f5c157d9SJohn Baldwin 	 * the previous thread or higher than the next thread.
33836412d79SJohn Baldwin 	 */
33961a74c5cSJeff Roberson 	THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock);
340551cf4e1SJohn Baldwin 	td1 = TAILQ_PREV(td, threadqueue, td_lockq);
341f5c157d9SJohn Baldwin 	td2 = TAILQ_NEXT(td, td_lockq);
342f5c157d9SJohn Baldwin 	if ((td1 != NULL && td->td_priority < td1->td_priority) ||
343f5c157d9SJohn Baldwin 	    (td2 != NULL && td->td_priority > td2->td_priority)) {
34436412d79SJohn Baldwin 		/*
345b40ce416SJulian Elischer 		 * Remove thread from blocked chain and determine where
346f5c157d9SJohn Baldwin 		 * it should be moved to.
34736412d79SJohn Baldwin 		 */
3487aa4f685SJohn Baldwin 		queue = td->td_tsqueue;
3497aa4f685SJohn Baldwin 		MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
350961a7b24SJohn Baldwin 		mtx_lock_spin(&td_contested_lock);
3517aa4f685SJohn Baldwin 		TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
3527aa4f685SJohn Baldwin 		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
353b40ce416SJulian Elischer 			MPASS(td1->td_proc->p_magic == P_MAGIC);
354f5c157d9SJohn Baldwin 			if (td1->td_priority > td->td_priority)
35536412d79SJohn Baldwin 				break;
35636412d79SJohn Baldwin 		}
3579ed346baSBosko Milekic 
358f5c157d9SJohn Baldwin 		if (td1 == NULL)
3597aa4f685SJohn Baldwin 			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
360f5c157d9SJohn Baldwin 		else
361551cf4e1SJohn Baldwin 			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
362961a7b24SJohn Baldwin 		mtx_unlock_spin(&td_contested_lock);
363f5c157d9SJohn Baldwin 		if (td1 == NULL)
364f5c157d9SJohn Baldwin 			CTR3(KTR_LOCK,
365f5c157d9SJohn Baldwin 		    "turnstile_adjust_thread: td %d put at tail on [%p] %s",
366f5c157d9SJohn Baldwin 			    td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
367f5c157d9SJohn Baldwin 		else
36836412d79SJohn Baldwin 			CTR4(KTR_LOCK,
369f5c157d9SJohn Baldwin 		    "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
370f5c157d9SJohn Baldwin 			    td->td_tid, td1->td_tid, ts->ts_lockobj,
371f5c157d9SJohn Baldwin 			    ts->ts_lockobj->lo_name);
37236412d79SJohn Baldwin 	}
373f5c157d9SJohn Baldwin 	return (1);
37436412d79SJohn Baldwin }
37536412d79SJohn Baldwin 
3766c35e809SDag-Erling Smørgrav /*
377961a7b24SJohn Baldwin  * Early initialization of turnstiles.  This is not done via a SYSINIT()
378961a7b24SJohn Baldwin  * since this needs to be initialized very early when mutexes are first
379961a7b24SJohn Baldwin  * initialized.
3806283b7d0SJohn Baldwin  */
3816283b7d0SJohn Baldwin void
init_turnstiles(void)382961a7b24SJohn Baldwin init_turnstiles(void)
3836283b7d0SJohn Baldwin {
384961a7b24SJohn Baldwin 	int i;
3856283b7d0SJohn Baldwin 
386961a7b24SJohn Baldwin 	for (i = 0; i < TC_TABLESIZE; i++) {
387961a7b24SJohn Baldwin 		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
388961a7b24SJohn Baldwin 		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
389961a7b24SJohn Baldwin 		    NULL, MTX_SPIN);
39001bd10e1SJohn Baldwin 	}
39101bd10e1SJohn Baldwin 	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
392550d1c93SJohn Baldwin 	LIST_INIT(&thread0.td_contested);
39301bd10e1SJohn Baldwin 	thread0.td_turnstile = NULL;
39401bd10e1SJohn Baldwin }
39501bd10e1SJohn Baldwin 
396ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
39701bd10e1SJohn Baldwin static void
init_turnstile_profiling(void * arg)39801bd10e1SJohn Baldwin init_turnstile_profiling(void *arg)
39901bd10e1SJohn Baldwin {
40001bd10e1SJohn Baldwin 	struct sysctl_oid *chain_oid;
40101bd10e1SJohn Baldwin 	char chain_name[10];
40201bd10e1SJohn Baldwin 	int i;
40301bd10e1SJohn Baldwin 
40401bd10e1SJohn Baldwin 	for (i = 0; i < TC_TABLESIZE; i++) {
405ef0ebfc3SJohn Baldwin 		snprintf(chain_name, sizeof(chain_name), "%d", i);
406ef0ebfc3SJohn Baldwin 		chain_oid = SYSCTL_ADD_NODE(NULL,
407ef0ebfc3SJohn Baldwin 		    SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
4087029da5cSPawel Biernacki 		    chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
4097029da5cSPawel Biernacki 		    "turnstile chain stats");
410ef0ebfc3SJohn Baldwin 		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
411ef0ebfc3SJohn Baldwin 		    "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
412ef0ebfc3SJohn Baldwin 		    NULL);
413ef0ebfc3SJohn Baldwin 		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
414ef0ebfc3SJohn Baldwin 		    "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
415ef0ebfc3SJohn Baldwin 		    0, NULL);
41601bd10e1SJohn Baldwin 	}
41701bd10e1SJohn Baldwin }
41801bd10e1SJohn Baldwin SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
41901bd10e1SJohn Baldwin     init_turnstile_profiling, NULL);
420ef0ebfc3SJohn Baldwin #endif
4216283b7d0SJohn Baldwin 
422961a7b24SJohn Baldwin static void
init_turnstile0(void * dummy)423961a7b24SJohn Baldwin init_turnstile0(void *dummy)
4246283b7d0SJohn Baldwin {
4256283b7d0SJohn Baldwin 
4262b7e2ee7SJeff Roberson 	turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
4278c68f75aSJohn Baldwin 	    NULL,
4282b7e2ee7SJeff Roberson #ifdef INVARIANTS
4298c68f75aSJohn Baldwin 	    turnstile_dtor,
4302b7e2ee7SJeff Roberson #else
4318c68f75aSJohn Baldwin 	    NULL,
4322b7e2ee7SJeff Roberson #endif
4338c68f75aSJohn Baldwin 	    turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
434961a7b24SJohn Baldwin 	thread0.td_turnstile = turnstile_alloc();
435961a7b24SJohn Baldwin }
436961a7b24SJohn Baldwin SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
4376c35e809SDag-Erling Smørgrav 
438961a7b24SJohn Baldwin /*
439f5c157d9SJohn Baldwin  * Update a thread on the turnstile list after it's priority has been changed.
440f5c157d9SJohn Baldwin  * The old priority is passed in as an argument.
441f5c157d9SJohn Baldwin  */
442f5c157d9SJohn Baldwin void
turnstile_adjust(struct thread * td,u_char oldpri)443f5c157d9SJohn Baldwin turnstile_adjust(struct thread *td, u_char oldpri)
444f5c157d9SJohn Baldwin {
445f5c157d9SJohn Baldwin 	struct turnstile *ts;
446f5c157d9SJohn Baldwin 
447f5c157d9SJohn Baldwin 	MPASS(TD_ON_LOCK(td));
448f5c157d9SJohn Baldwin 
449f5c157d9SJohn Baldwin 	/*
450f5c157d9SJohn Baldwin 	 * Pick up the lock that td is blocked on.
451f5c157d9SJohn Baldwin 	 */
452f5c157d9SJohn Baldwin 	ts = td->td_blocked;
453f5c157d9SJohn Baldwin 	MPASS(ts != NULL);
45461a74c5cSJeff Roberson 	THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock);
4552502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
456f5c157d9SJohn Baldwin 
457f5c157d9SJohn Baldwin 	/* Resort the turnstile on the list. */
4582502c107SJeff Roberson 	if (!turnstile_adjust_thread(ts, td))
459f5c157d9SJohn Baldwin 		return;
460f5c157d9SJohn Baldwin 	/*
461f5c157d9SJohn Baldwin 	 * If our priority was lowered and we are at the head of the
462f5c157d9SJohn Baldwin 	 * turnstile, then propagate our new priority up the chain.
463f5c157d9SJohn Baldwin 	 * Note that we currently don't try to revoke lent priorities
464f5c157d9SJohn Baldwin 	 * when our priority goes up.
465f5c157d9SJohn Baldwin 	 */
4667aa4f685SJohn Baldwin 	MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
4677aa4f685SJohn Baldwin 	    td->td_tsqueue == TS_SHARED_QUEUE);
4687aa4f685SJohn Baldwin 	if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
4697aa4f685SJohn Baldwin 	    td->td_priority < oldpri) {
470f5c157d9SJohn Baldwin 		propagate_priority(td);
4712502c107SJeff Roberson 	}
472f5c157d9SJohn Baldwin }
473f5c157d9SJohn Baldwin 
474f5c157d9SJohn Baldwin /*
475961a7b24SJohn Baldwin  * Set the owner of the lock this turnstile is attached to.
476961a7b24SJohn Baldwin  */
477961a7b24SJohn Baldwin static void
turnstile_setowner(struct turnstile * ts,struct thread * owner)478961a7b24SJohn Baldwin turnstile_setowner(struct turnstile *ts, struct thread *owner)
479961a7b24SJohn Baldwin {
480961a7b24SJohn Baldwin 
481961a7b24SJohn Baldwin 	mtx_assert(&td_contested_lock, MA_OWNED);
482961a7b24SJohn Baldwin 	MPASS(ts->ts_owner == NULL);
4837aa4f685SJohn Baldwin 
4847aa4f685SJohn Baldwin 	/* A shared lock might not have an owner. */
4857aa4f685SJohn Baldwin 	if (owner == NULL)
4867aa4f685SJohn Baldwin 		return;
4877aa4f685SJohn Baldwin 
4887aa4f685SJohn Baldwin 	MPASS(owner->td_proc->p_magic == P_MAGIC);
489961a7b24SJohn Baldwin 	ts->ts_owner = owner;
490961a7b24SJohn Baldwin 	LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
491961a7b24SJohn Baldwin }
492961a7b24SJohn Baldwin 
4932b7e2ee7SJeff Roberson #ifdef INVARIANTS
494961a7b24SJohn Baldwin /*
4952b7e2ee7SJeff Roberson  * UMA zone item deallocator.
496961a7b24SJohn Baldwin  */
4972b7e2ee7SJeff Roberson static void
turnstile_dtor(void * mem,int size,void * arg)4982b7e2ee7SJeff Roberson turnstile_dtor(void *mem, int size, void *arg)
499961a7b24SJohn Baldwin {
500961a7b24SJohn Baldwin 	struct turnstile *ts;
501961a7b24SJohn Baldwin 
5022b7e2ee7SJeff Roberson 	ts = mem;
5032b7e2ee7SJeff Roberson 	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
5042b7e2ee7SJeff Roberson 	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
5052b7e2ee7SJeff Roberson 	MPASS(TAILQ_EMPTY(&ts->ts_pending));
5062b7e2ee7SJeff Roberson }
5072b7e2ee7SJeff Roberson #endif
5082b7e2ee7SJeff Roberson 
5092b7e2ee7SJeff Roberson /*
5102b7e2ee7SJeff Roberson  * UMA zone item initializer.
5112b7e2ee7SJeff Roberson  */
5122b7e2ee7SJeff Roberson static int
turnstile_init(void * mem,int size,int flags)5132b7e2ee7SJeff Roberson turnstile_init(void *mem, int size, int flags)
5142b7e2ee7SJeff Roberson {
5152b7e2ee7SJeff Roberson 	struct turnstile *ts;
5162b7e2ee7SJeff Roberson 
5172b7e2ee7SJeff Roberson 	bzero(mem, size);
5182b7e2ee7SJeff Roberson 	ts = mem;
5197aa4f685SJohn Baldwin 	TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
5207aa4f685SJohn Baldwin 	TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
521961a7b24SJohn Baldwin 	TAILQ_INIT(&ts->ts_pending);
522961a7b24SJohn Baldwin 	LIST_INIT(&ts->ts_free);
5231c81a87eSJeff Roberson 	mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN);
5242b7e2ee7SJeff Roberson 	return (0);
5252b7e2ee7SJeff Roberson }
5262b7e2ee7SJeff Roberson 
5272502c107SJeff Roberson static void
turnstile_fini(void * mem,int size)5282502c107SJeff Roberson turnstile_fini(void *mem, int size)
5292502c107SJeff Roberson {
5302502c107SJeff Roberson 	struct turnstile *ts;
5312502c107SJeff Roberson 
5322502c107SJeff Roberson 	ts = mem;
5332502c107SJeff Roberson 	mtx_destroy(&ts->ts_lock);
5342502c107SJeff Roberson }
5352502c107SJeff Roberson 
5362b7e2ee7SJeff Roberson /*
5372b7e2ee7SJeff Roberson  * Get a turnstile for a new thread.
5382b7e2ee7SJeff Roberson  */
5392b7e2ee7SJeff Roberson struct turnstile *
turnstile_alloc(void)5402b7e2ee7SJeff Roberson turnstile_alloc(void)
5412b7e2ee7SJeff Roberson {
5422b7e2ee7SJeff Roberson 
5432b7e2ee7SJeff Roberson 	return (uma_zalloc(turnstile_zone, M_WAITOK));
544961a7b24SJohn Baldwin }
545961a7b24SJohn Baldwin 
546961a7b24SJohn Baldwin /*
547961a7b24SJohn Baldwin  * Free a turnstile when a thread is destroyed.
548961a7b24SJohn Baldwin  */
549961a7b24SJohn Baldwin void
turnstile_free(struct turnstile * ts)550961a7b24SJohn Baldwin turnstile_free(struct turnstile *ts)
551961a7b24SJohn Baldwin {
552961a7b24SJohn Baldwin 
5532b7e2ee7SJeff Roberson 	uma_zfree(turnstile_zone, ts);
554961a7b24SJohn Baldwin }
555961a7b24SJohn Baldwin 
556961a7b24SJohn Baldwin /*
5572ff0e645SJohn Baldwin  * Lock the turnstile chain associated with the specified lock.
5582ff0e645SJohn Baldwin  */
5592ff0e645SJohn Baldwin void
turnstile_chain_lock(struct lock_object * lock)5602502c107SJeff Roberson turnstile_chain_lock(struct lock_object *lock)
5612ff0e645SJohn Baldwin {
5622ff0e645SJohn Baldwin 	struct turnstile_chain *tc;
5632ff0e645SJohn Baldwin 
5642ff0e645SJohn Baldwin 	tc = TC_LOOKUP(lock);
5652ff0e645SJohn Baldwin 	mtx_lock_spin(&tc->tc_lock);
5662ff0e645SJohn Baldwin }
5672ff0e645SJohn Baldwin 
5682502c107SJeff Roberson struct turnstile *
turnstile_trywait(struct lock_object * lock)5692502c107SJeff Roberson turnstile_trywait(struct lock_object *lock)
5702502c107SJeff Roberson {
5712502c107SJeff Roberson 	struct turnstile_chain *tc;
5722502c107SJeff Roberson 	struct turnstile *ts;
5732502c107SJeff Roberson 
5742502c107SJeff Roberson 	tc = TC_LOOKUP(lock);
5752502c107SJeff Roberson 	mtx_lock_spin(&tc->tc_lock);
5762502c107SJeff Roberson 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
5772502c107SJeff Roberson 		if (ts->ts_lockobj == lock) {
5782502c107SJeff Roberson 			mtx_lock_spin(&ts->ts_lock);
5792502c107SJeff Roberson 			return (ts);
5802502c107SJeff Roberson 		}
5812502c107SJeff Roberson 
5822502c107SJeff Roberson 	ts = curthread->td_turnstile;
5832502c107SJeff Roberson 	MPASS(ts != NULL);
5842502c107SJeff Roberson 	mtx_lock_spin(&ts->ts_lock);
5852502c107SJeff Roberson 	KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
5862502c107SJeff Roberson 	ts->ts_lockobj = lock;
5872502c107SJeff Roberson 
5882502c107SJeff Roberson 	return (ts);
5892502c107SJeff Roberson }
5902502c107SJeff Roberson 
5912fb62b1aSMark Johnston bool
turnstile_lock(struct turnstile * ts,struct lock_object ** lockp,struct thread ** tdp)5922fb62b1aSMark Johnston turnstile_lock(struct turnstile *ts, struct lock_object **lockp,
5932fb62b1aSMark Johnston     struct thread **tdp)
59406bf2a6aSMatt Macy {
59506bf2a6aSMatt Macy 	struct turnstile_chain *tc;
59606bf2a6aSMatt Macy 	struct lock_object *lock;
59706bf2a6aSMatt Macy 
59806bf2a6aSMatt Macy 	if ((lock = ts->ts_lockobj) == NULL)
5992fb62b1aSMark Johnston 		return (false);
60006bf2a6aSMatt Macy 	tc = TC_LOOKUP(lock);
60106bf2a6aSMatt Macy 	mtx_lock_spin(&tc->tc_lock);
60206bf2a6aSMatt Macy 	mtx_lock_spin(&ts->ts_lock);
60306bf2a6aSMatt Macy 	if (__predict_false(lock != ts->ts_lockobj)) {
60406bf2a6aSMatt Macy 		mtx_unlock_spin(&tc->tc_lock);
60506bf2a6aSMatt Macy 		mtx_unlock_spin(&ts->ts_lock);
6062fb62b1aSMark Johnston 		return (false);
60706bf2a6aSMatt Macy 	}
60806bf2a6aSMatt Macy 	*lockp = lock;
6092fb62b1aSMark Johnston 	*tdp = ts->ts_owner;
6102fb62b1aSMark Johnston 	return (true);
61106bf2a6aSMatt Macy }
61206bf2a6aSMatt Macy 
61306bf2a6aSMatt Macy void
turnstile_unlock(struct turnstile * ts,struct lock_object * lock)61406bf2a6aSMatt Macy turnstile_unlock(struct turnstile *ts, struct lock_object *lock)
61506bf2a6aSMatt Macy {
61606bf2a6aSMatt Macy 	struct turnstile_chain *tc;
61706bf2a6aSMatt Macy 
61806bf2a6aSMatt Macy 	mtx_assert(&ts->ts_lock, MA_OWNED);
61906bf2a6aSMatt Macy 	mtx_unlock_spin(&ts->ts_lock);
62006bf2a6aSMatt Macy 	if (ts == curthread->td_turnstile)
62106bf2a6aSMatt Macy 		ts->ts_lockobj = NULL;
62206bf2a6aSMatt Macy 	tc = TC_LOOKUP(lock);
62306bf2a6aSMatt Macy 	mtx_unlock_spin(&tc->tc_lock);
62406bf2a6aSMatt Macy }
62506bf2a6aSMatt Macy 
62606bf2a6aSMatt Macy void
turnstile_assert(struct turnstile * ts)62706bf2a6aSMatt Macy turnstile_assert(struct turnstile *ts)
62806bf2a6aSMatt Macy {
62906bf2a6aSMatt Macy 	MPASS(ts->ts_lockobj == NULL);
63006bf2a6aSMatt Macy }
63106bf2a6aSMatt Macy 
6322502c107SJeff Roberson void
turnstile_cancel(struct turnstile * ts)6332502c107SJeff Roberson turnstile_cancel(struct turnstile *ts)
6342502c107SJeff Roberson {
6352502c107SJeff Roberson 	struct turnstile_chain *tc;
6362502c107SJeff Roberson 	struct lock_object *lock;
6372502c107SJeff Roberson 
6382502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
6392502c107SJeff Roberson 
6402502c107SJeff Roberson 	mtx_unlock_spin(&ts->ts_lock);
6412502c107SJeff Roberson 	lock = ts->ts_lockobj;
6422502c107SJeff Roberson 	if (ts == curthread->td_turnstile)
6432502c107SJeff Roberson 		ts->ts_lockobj = NULL;
6442502c107SJeff Roberson 	tc = TC_LOOKUP(lock);
6452502c107SJeff Roberson 	mtx_unlock_spin(&tc->tc_lock);
6462502c107SJeff Roberson }
6472502c107SJeff Roberson 
6482ff0e645SJohn Baldwin /*
649961a7b24SJohn Baldwin  * Look up the turnstile for a lock in the hash table locking the associated
6502ff0e645SJohn Baldwin  * turnstile chain along the way.  If no turnstile is found in the hash
6512ff0e645SJohn Baldwin  * table, NULL is returned.
652961a7b24SJohn Baldwin  */
653961a7b24SJohn Baldwin struct turnstile *
turnstile_lookup(struct lock_object * lock)654961a7b24SJohn Baldwin turnstile_lookup(struct lock_object *lock)
655961a7b24SJohn Baldwin {
656961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
657961a7b24SJohn Baldwin 	struct turnstile *ts;
658961a7b24SJohn Baldwin 
659961a7b24SJohn Baldwin 	tc = TC_LOOKUP(lock);
6602ff0e645SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
661961a7b24SJohn Baldwin 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
6622502c107SJeff Roberson 		if (ts->ts_lockobj == lock) {
6632502c107SJeff Roberson 			mtx_lock_spin(&ts->ts_lock);
664961a7b24SJohn Baldwin 			return (ts);
6652502c107SJeff Roberson 		}
666961a7b24SJohn Baldwin 	return (NULL);
667961a7b24SJohn Baldwin }
668961a7b24SJohn Baldwin 
669961a7b24SJohn Baldwin /*
670961a7b24SJohn Baldwin  * Unlock the turnstile chain associated with a given lock.
671961a7b24SJohn Baldwin  */
672961a7b24SJohn Baldwin void
turnstile_chain_unlock(struct lock_object * lock)6732502c107SJeff Roberson turnstile_chain_unlock(struct lock_object *lock)
674961a7b24SJohn Baldwin {
675961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
676961a7b24SJohn Baldwin 
677961a7b24SJohn Baldwin 	tc = TC_LOOKUP(lock);
678961a7b24SJohn Baldwin 	mtx_unlock_spin(&tc->tc_lock);
679961a7b24SJohn Baldwin }
680961a7b24SJohn Baldwin 
681961a7b24SJohn Baldwin /*
6827aa4f685SJohn Baldwin  * Return a pointer to the thread waiting on this turnstile with the
6837aa4f685SJohn Baldwin  * most important priority or NULL if the turnstile has no waiters.
6847aa4f685SJohn Baldwin  */
6857aa4f685SJohn Baldwin static struct thread *
turnstile_first_waiter(struct turnstile * ts)6867aa4f685SJohn Baldwin turnstile_first_waiter(struct turnstile *ts)
6877aa4f685SJohn Baldwin {
6887aa4f685SJohn Baldwin 	struct thread *std, *xtd;
6897aa4f685SJohn Baldwin 
6907aa4f685SJohn Baldwin 	std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]);
6917aa4f685SJohn Baldwin 	xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
6927aa4f685SJohn Baldwin 	if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority))
6937aa4f685SJohn Baldwin 		return (std);
6947aa4f685SJohn Baldwin 	return (xtd);
6957aa4f685SJohn Baldwin }
6967aa4f685SJohn Baldwin 
6977aa4f685SJohn Baldwin /*
698961a7b24SJohn Baldwin  * Take ownership of a turnstile and adjust the priority of the new
699961a7b24SJohn Baldwin  * owner appropriately.
700961a7b24SJohn Baldwin  */
701961a7b24SJohn Baldwin void
turnstile_claim(struct turnstile * ts)7022502c107SJeff Roberson turnstile_claim(struct turnstile *ts)
703961a7b24SJohn Baldwin {
704961a7b24SJohn Baldwin 	struct thread *td, *owner;
7052502c107SJeff Roberson 	struct turnstile_chain *tc;
706961a7b24SJohn Baldwin 
7072502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
7082502c107SJeff Roberson 	MPASS(ts != curthread->td_turnstile);
709961a7b24SJohn Baldwin 
710961a7b24SJohn Baldwin 	owner = curthread;
711961a7b24SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
712961a7b24SJohn Baldwin 	turnstile_setowner(ts, owner);
713961a7b24SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
714961a7b24SJohn Baldwin 
7157aa4f685SJohn Baldwin 	td = turnstile_first_waiter(ts);
716961a7b24SJohn Baldwin 	MPASS(td != NULL);
717961a7b24SJohn Baldwin 	MPASS(td->td_proc->p_magic == P_MAGIC);
71861a74c5cSJeff Roberson 	THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock);
719961a7b24SJohn Baldwin 
720961a7b24SJohn Baldwin 	/*
721961a7b24SJohn Baldwin 	 * Update the priority of the new owner if needed.
722961a7b24SJohn Baldwin 	 */
7232502c107SJeff Roberson 	thread_lock(owner);
724961a7b24SJohn Baldwin 	if (td->td_priority < owner->td_priority)
725f5c157d9SJohn Baldwin 		sched_lend_prio(owner, td->td_priority);
7262502c107SJeff Roberson 	thread_unlock(owner);
7272502c107SJeff Roberson 	tc = TC_LOOKUP(ts->ts_lockobj);
7282502c107SJeff Roberson 	mtx_unlock_spin(&ts->ts_lock);
7292502c107SJeff Roberson 	mtx_unlock_spin(&tc->tc_lock);
730961a7b24SJohn Baldwin }
731961a7b24SJohn Baldwin 
732961a7b24SJohn Baldwin /*
7332ff0e645SJohn Baldwin  * Block the current thread on the turnstile assicated with 'lock'.  This
7342ff0e645SJohn Baldwin  * function will context switch and not return until this thread has been
7352ff0e645SJohn Baldwin  * woken back up.  This function must be called with the appropriate
7362ff0e645SJohn Baldwin  * turnstile chain locked and will return with it unlocked.
737961a7b24SJohn Baldwin  */
738961a7b24SJohn Baldwin void
turnstile_wait(struct turnstile * ts,struct thread * owner,int queue)7392502c107SJeff Roberson turnstile_wait(struct turnstile *ts, struct thread *owner, int queue)
740961a7b24SJohn Baldwin {
741961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
742961a7b24SJohn Baldwin 	struct thread *td, *td1;
7432502c107SJeff Roberson 	struct lock_object *lock;
744961a7b24SJohn Baldwin 
745961a7b24SJohn Baldwin 	td = curthread;
7462502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
7477aa4f685SJohn Baldwin 	if (owner)
748961a7b24SJohn Baldwin 		MPASS(owner->td_proc->p_magic == P_MAGIC);
7497aa4f685SJohn Baldwin 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
750961a7b24SJohn Baldwin 
7512ff0e645SJohn Baldwin 	/*
7522ff0e645SJohn Baldwin 	 * If the lock does not already have a turnstile, use this thread's
7532ff0e645SJohn Baldwin 	 * turnstile.  Otherwise insert the current thread into the
7542ff0e645SJohn Baldwin 	 * turnstile already in use by this lock.
7552ff0e645SJohn Baldwin 	 */
7562502c107SJeff Roberson 	tc = TC_LOOKUP(ts->ts_lockobj);
7572502c107SJeff Roberson 	mtx_assert(&tc->tc_lock, MA_OWNED);
758f7488600SJohn Baldwin 	if (ts == td->td_turnstile) {
759ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
760ef0ebfc3SJohn Baldwin 		tc->tc_depth++;
761ef0ebfc3SJohn Baldwin 		if (tc->tc_depth > tc->tc_max_depth) {
762ef0ebfc3SJohn Baldwin 			tc->tc_max_depth = tc->tc_depth;
763ef0ebfc3SJohn Baldwin 			if (tc->tc_max_depth > turnstile_max_depth)
764ef0ebfc3SJohn Baldwin 				turnstile_max_depth = tc->tc_max_depth;
765ef0ebfc3SJohn Baldwin 		}
766ef0ebfc3SJohn Baldwin #endif
767961a7b24SJohn Baldwin 		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
768961a7b24SJohn Baldwin 		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
769961a7b24SJohn Baldwin 		    ("thread's turnstile has pending threads"));
7707aa4f685SJohn Baldwin 		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
7717aa4f685SJohn Baldwin 		    ("thread's turnstile has exclusive waiters"));
7727aa4f685SJohn Baldwin 		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
7737aa4f685SJohn Baldwin 		    ("thread's turnstile has shared waiters"));
774961a7b24SJohn Baldwin 		KASSERT(LIST_EMPTY(&ts->ts_free),
775961a7b24SJohn Baldwin 		    ("thread's turnstile has a non-empty free list"));
7762502c107SJeff Roberson 		MPASS(ts->ts_lockobj != NULL);
777961a7b24SJohn Baldwin 		mtx_lock_spin(&td_contested_lock);
7787aa4f685SJohn Baldwin 		TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
779961a7b24SJohn Baldwin 		turnstile_setowner(ts, owner);
780961a7b24SJohn Baldwin 		mtx_unlock_spin(&td_contested_lock);
781961a7b24SJohn Baldwin 	} else {
7827aa4f685SJohn Baldwin 		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
783961a7b24SJohn Baldwin 			if (td1->td_priority > td->td_priority)
7846c35e809SDag-Erling Smørgrav 				break;
785961a7b24SJohn Baldwin 		mtx_lock_spin(&td_contested_lock);
786961a7b24SJohn Baldwin 		if (td1 != NULL)
787961a7b24SJohn Baldwin 			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
788961a7b24SJohn Baldwin 		else
7897aa4f685SJohn Baldwin 			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
7907aa4f685SJohn Baldwin 		MPASS(owner == ts->ts_owner);
791961a7b24SJohn Baldwin 		mtx_unlock_spin(&td_contested_lock);
792961a7b24SJohn Baldwin 		MPASS(td->td_turnstile != NULL);
793961a7b24SJohn Baldwin 		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
7946c35e809SDag-Erling Smørgrav 	}
7952502c107SJeff Roberson 	thread_lock(td);
7962502c107SJeff Roberson 	thread_lock_set(td, &ts->ts_lock);
797961a7b24SJohn Baldwin 	td->td_turnstile = NULL;
79836412d79SJohn Baldwin 
799961a7b24SJohn Baldwin 	/* Save who we are blocked on and switch. */
8002502c107SJeff Roberson 	lock = ts->ts_lockobj;
8017aa4f685SJohn Baldwin 	td->td_tsqueue = queue;
802961a7b24SJohn Baldwin 	td->td_blocked = ts;
803961a7b24SJohn Baldwin 	td->td_lockname = lock->lo_name;
804f7829d0dSAttilio Rao 	td->td_blktick = ticks;
805551cf4e1SJohn Baldwin 	TD_SET_LOCK(td);
8062502c107SJeff Roberson 	mtx_unlock_spin(&tc->tc_lock);
807b40ce416SJulian Elischer 	propagate_priority(td);
8089ed346baSBosko Milekic 
809961a7b24SJohn Baldwin 	if (LOCK_LOG_TEST(lock, 0))
810f5c157d9SJohn Baldwin 		CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
811f5c157d9SJohn Baldwin 		    td->td_tid, lock, lock->lo_name);
8129ed346baSBosko Milekic 
813b3e9e682SRyan Stone 	SDT_PROBE0(sched, , , sleep);
814b3e9e682SRyan Stone 
815626ac252SJeff Roberson 	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
816686bcb5cSJeff Roberson 	mi_switch(SW_VOL | SWT_TURNSTILE);
8179ed346baSBosko Milekic 
818961a7b24SJohn Baldwin 	if (LOCK_LOG_TEST(lock, 0))
819f5c157d9SJohn Baldwin 		CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
820f5c157d9SJohn Baldwin 		    __func__, td->td_tid, lock, lock->lo_name);
82136412d79SJohn Baldwin }
8229ed346baSBosko Milekic 
823961a7b24SJohn Baldwin /*
824961a7b24SJohn Baldwin  * Pick the highest priority thread on this turnstile and put it on the
825961a7b24SJohn Baldwin  * pending list.  This must be called with the turnstile chain locked.
826961a7b24SJohn Baldwin  */
827961a7b24SJohn Baldwin int
turnstile_signal(struct turnstile * ts,int queue)8287aa4f685SJohn Baldwin turnstile_signal(struct turnstile *ts, int queue)
829961a7b24SJohn Baldwin {
8303adccf38SMatt Macy 	struct turnstile_chain *tc __unused;
831961a7b24SJohn Baldwin 	struct thread *td;
832961a7b24SJohn Baldwin 	int empty;
833961a7b24SJohn Baldwin 
834961a7b24SJohn Baldwin 	MPASS(ts != NULL);
8352502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
836961a7b24SJohn Baldwin 	MPASS(curthread->td_proc->p_magic == P_MAGIC);
8375dff04c3SJeff Roberson 	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
8387aa4f685SJohn Baldwin 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
8399ed346baSBosko Milekic 
8409ed346baSBosko Milekic 	/*
841961a7b24SJohn Baldwin 	 * Pick the highest priority thread blocked on this lock and
842961a7b24SJohn Baldwin 	 * move it to the pending list.
8439ed346baSBosko Milekic 	 */
8447aa4f685SJohn Baldwin 	td = TAILQ_FIRST(&ts->ts_blocked[queue]);
845b40ce416SJulian Elischer 	MPASS(td->td_proc->p_magic == P_MAGIC);
846961a7b24SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
8477aa4f685SJohn Baldwin 	TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
848961a7b24SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
849961a7b24SJohn Baldwin 	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
8509ed346baSBosko Milekic 
851961a7b24SJohn Baldwin 	/*
852961a7b24SJohn Baldwin 	 * If the turnstile is now empty, remove it from its chain and
853961a7b24SJohn Baldwin 	 * give it to the about-to-be-woken thread.  Otherwise take a
854961a7b24SJohn Baldwin 	 * turnstile from the free list and give it to the thread.
855961a7b24SJohn Baldwin 	 */
8567aa4f685SJohn Baldwin 	empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
8577aa4f685SJohn Baldwin 	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
858ef0ebfc3SJohn Baldwin 	if (empty) {
8592502c107SJeff Roberson 		tc = TC_LOOKUP(ts->ts_lockobj);
8602502c107SJeff Roberson 		mtx_assert(&tc->tc_lock, MA_OWNED);
861961a7b24SJohn Baldwin 		MPASS(LIST_EMPTY(&ts->ts_free));
862ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
863ef0ebfc3SJohn Baldwin 		tc->tc_depth--;
864ef0ebfc3SJohn Baldwin #endif
865ef0ebfc3SJohn Baldwin 	} else
866961a7b24SJohn Baldwin 		ts = LIST_FIRST(&ts->ts_free);
867da1d503bSJohn Baldwin 	MPASS(ts != NULL);
868961a7b24SJohn Baldwin 	LIST_REMOVE(ts, ts_hash);
869961a7b24SJohn Baldwin 	td->td_turnstile = ts;
8709ed346baSBosko Milekic 
871961a7b24SJohn Baldwin 	return (empty);
872961a7b24SJohn Baldwin }
873961a7b24SJohn Baldwin 
874961a7b24SJohn Baldwin /*
875961a7b24SJohn Baldwin  * Put all blocked threads on the pending list.  This must be called with
876961a7b24SJohn Baldwin  * the turnstile chain locked.
877961a7b24SJohn Baldwin  */
878961a7b24SJohn Baldwin void
turnstile_broadcast(struct turnstile * ts,int queue)8797aa4f685SJohn Baldwin turnstile_broadcast(struct turnstile *ts, int queue)
880961a7b24SJohn Baldwin {
8813adccf38SMatt Macy 	struct turnstile_chain *tc __unused;
882961a7b24SJohn Baldwin 	struct turnstile *ts1;
883961a7b24SJohn Baldwin 	struct thread *td;
884961a7b24SJohn Baldwin 
885961a7b24SJohn Baldwin 	MPASS(ts != NULL);
8862502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
887961a7b24SJohn Baldwin 	MPASS(curthread->td_proc->p_magic == P_MAGIC);
8885dff04c3SJeff Roberson 	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
8892502c107SJeff Roberson 	/*
8902502c107SJeff Roberson 	 * We must have the chain locked so that we can remove the empty
8912502c107SJeff Roberson 	 * turnstile from the hash queue.
8922502c107SJeff Roberson 	 */
893961a7b24SJohn Baldwin 	tc = TC_LOOKUP(ts->ts_lockobj);
894961a7b24SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
8957aa4f685SJohn Baldwin 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
896961a7b24SJohn Baldwin 
897961a7b24SJohn Baldwin 	/*
898961a7b24SJohn Baldwin 	 * Transfer the blocked list to the pending list.
899961a7b24SJohn Baldwin 	 */
900961a7b24SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
9017aa4f685SJohn Baldwin 	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
902961a7b24SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
903961a7b24SJohn Baldwin 
904961a7b24SJohn Baldwin 	/*
905961a7b24SJohn Baldwin 	 * Give a turnstile to each thread.  The last thread gets
9067aa4f685SJohn Baldwin 	 * this turnstile if the turnstile is empty.
907961a7b24SJohn Baldwin 	 */
908961a7b24SJohn Baldwin 	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
909961a7b24SJohn Baldwin 		if (LIST_EMPTY(&ts->ts_free)) {
910961a7b24SJohn Baldwin 			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
911961a7b24SJohn Baldwin 			ts1 = ts;
912ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
913ef0ebfc3SJohn Baldwin 			tc->tc_depth--;
914ef0ebfc3SJohn Baldwin #endif
91536412d79SJohn Baldwin 		} else
916961a7b24SJohn Baldwin 			ts1 = LIST_FIRST(&ts->ts_free);
917da1d503bSJohn Baldwin 		MPASS(ts1 != NULL);
918961a7b24SJohn Baldwin 		LIST_REMOVE(ts1, ts_hash);
919961a7b24SJohn Baldwin 		td->td_turnstile = ts1;
920961a7b24SJohn Baldwin 	}
921961a7b24SJohn Baldwin }
9229ed346baSBosko Milekic 
923a9fd669bSKonstantin Belousov static u_char
turnstile_calc_unlend_prio_locked(struct thread * td)924a9fd669bSKonstantin Belousov turnstile_calc_unlend_prio_locked(struct thread *td)
925a9fd669bSKonstantin Belousov {
926a9fd669bSKonstantin Belousov 	struct turnstile *nts;
927a9fd669bSKonstantin Belousov 	u_char cp, pri;
928a9fd669bSKonstantin Belousov 
929a9fd669bSKonstantin Belousov 	THREAD_LOCK_ASSERT(td, MA_OWNED);
930a9fd669bSKonstantin Belousov 	mtx_assert(&td_contested_lock, MA_OWNED);
931a9fd669bSKonstantin Belousov 
932a9fd669bSKonstantin Belousov 	pri = PRI_MAX;
933a9fd669bSKonstantin Belousov 	LIST_FOREACH(nts, &td->td_contested, ts_link) {
934a9fd669bSKonstantin Belousov 		cp = turnstile_first_waiter(nts)->td_priority;
935a9fd669bSKonstantin Belousov 		if (cp < pri)
936a9fd669bSKonstantin Belousov 			pri = cp;
937a9fd669bSKonstantin Belousov 	}
938a9fd669bSKonstantin Belousov 	return (pri);
939a9fd669bSKonstantin Belousov }
940a9fd669bSKonstantin Belousov 
941961a7b24SJohn Baldwin /*
942961a7b24SJohn Baldwin  * Wakeup all threads on the pending list and adjust the priority of the
943961a7b24SJohn Baldwin  * current thread appropriately.  This must be called with the turnstile
944961a7b24SJohn Baldwin  * chain locked.
945961a7b24SJohn Baldwin  */
946961a7b24SJohn Baldwin void
turnstile_unpend(struct turnstile * ts)947d0a22279SMateusz Guzik turnstile_unpend(struct turnstile *ts)
948961a7b24SJohn Baldwin {
949961a7b24SJohn Baldwin 	TAILQ_HEAD( ,thread) pending_threads;
950961a7b24SJohn Baldwin 	struct thread *td;
951a9fd669bSKonstantin Belousov 	u_char pri;
952961a7b24SJohn Baldwin 
953961a7b24SJohn Baldwin 	MPASS(ts != NULL);
9542502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
9555dff04c3SJeff Roberson 	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
956961a7b24SJohn Baldwin 	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
957961a7b24SJohn Baldwin 
958961a7b24SJohn Baldwin 	/*
959961a7b24SJohn Baldwin 	 * Move the list of pending threads out of the turnstile and
960961a7b24SJohn Baldwin 	 * into a local variable.
961961a7b24SJohn Baldwin 	 */
962961a7b24SJohn Baldwin 	TAILQ_INIT(&pending_threads);
963961a7b24SJohn Baldwin 	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
964961a7b24SJohn Baldwin #ifdef INVARIANTS
9657aa4f685SJohn Baldwin 	if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
9667aa4f685SJohn Baldwin 	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
967961a7b24SJohn Baldwin 		ts->ts_lockobj = NULL;
968961a7b24SJohn Baldwin #endif
9692502c107SJeff Roberson 	/*
9702502c107SJeff Roberson 	 * Adjust the priority of curthread based on other contested
9712502c107SJeff Roberson 	 * locks it owns.  Don't lower the priority below the base
9722502c107SJeff Roberson 	 * priority however.
9732502c107SJeff Roberson 	 */
9742502c107SJeff Roberson 	td = curthread;
9752502c107SJeff Roberson 	thread_lock(td);
9762502c107SJeff Roberson 	mtx_lock_spin(&td_contested_lock);
977961a7b24SJohn Baldwin 	/*
978961a7b24SJohn Baldwin 	 * Remove the turnstile from this thread's list of contested locks
979961a7b24SJohn Baldwin 	 * since this thread doesn't own it anymore.  New threads will
980961a7b24SJohn Baldwin 	 * not be blocking on the turnstile until it is claimed by a new
9817aa4f685SJohn Baldwin 	 * owner.  There might not be a current owner if this is a shared
9827aa4f685SJohn Baldwin 	 * lock.
983961a7b24SJohn Baldwin 	 */
9847aa4f685SJohn Baldwin 	if (ts->ts_owner != NULL) {
985961a7b24SJohn Baldwin 		ts->ts_owner = NULL;
986961a7b24SJohn Baldwin 		LIST_REMOVE(ts, ts_link);
9877aa4f685SJohn Baldwin 	}
988a9fd669bSKonstantin Belousov 	pri = turnstile_calc_unlend_prio_locked(td);
989961a7b24SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
990f5c157d9SJohn Baldwin 	sched_unlend_prio(td, pri);
9912502c107SJeff Roberson 	thread_unlock(td);
992961a7b24SJohn Baldwin 	/*
993961a7b24SJohn Baldwin 	 * Wake up all the pending threads.  If a thread is not blocked
994961a7b24SJohn Baldwin 	 * on a lock, then it is currently executing on another CPU in
99567ba8678SJohn Baldwin 	 * turnstile_wait() or sitting on a run queue waiting to resume
99667ba8678SJohn Baldwin 	 * in turnstile_wait().  Set a flag to force it to try to acquire
997961a7b24SJohn Baldwin 	 * the lock again instead of blocking.
998961a7b24SJohn Baldwin 	 */
999961a7b24SJohn Baldwin 	while (!TAILQ_EMPTY(&pending_threads)) {
1000961a7b24SJohn Baldwin 		td = TAILQ_FIRST(&pending_threads);
1001961a7b24SJohn Baldwin 		TAILQ_REMOVE(&pending_threads, td, td_lockq);
1002b3e9e682SRyan Stone 		SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
100361a74c5cSJeff Roberson 		thread_lock_block_wait(td);
1004626ac252SJeff Roberson 		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
1005961a7b24SJohn Baldwin 		MPASS(td->td_proc->p_magic == P_MAGIC);
10062502c107SJeff Roberson 		MPASS(TD_ON_LOCK(td));
10072502c107SJeff Roberson 		TD_CLR_LOCK(td);
10082502c107SJeff Roberson 		MPASS(TD_CAN_RUN(td));
1009961a7b24SJohn Baldwin 		td->td_blocked = NULL;
1010961a7b24SJohn Baldwin 		td->td_lockname = NULL;
1011f7829d0dSAttilio Rao 		td->td_blktick = 0;
10127aa4f685SJohn Baldwin #ifdef INVARIANTS
10137aa4f685SJohn Baldwin 		td->td_tsqueue = 0xff;
10147aa4f685SJohn Baldwin #endif
101561a74c5cSJeff Roberson 		sched_add(td, SRQ_HOLD | SRQ_BORING);
1016961a7b24SJohn Baldwin 	}
10172502c107SJeff Roberson 	mtx_unlock_spin(&ts->ts_lock);
10189ed346baSBosko Milekic }
10199ed346baSBosko Milekic 
10209ed346baSBosko Milekic /*
1021f1a4b852SJohn Baldwin  * Give up ownership of a turnstile.  This must be called with the
1022f1a4b852SJohn Baldwin  * turnstile chain locked.
1023f1a4b852SJohn Baldwin  */
1024f1a4b852SJohn Baldwin void
turnstile_disown(struct turnstile * ts)1025f1a4b852SJohn Baldwin turnstile_disown(struct turnstile *ts)
1026f1a4b852SJohn Baldwin {
1027f1a4b852SJohn Baldwin 	struct thread *td;
1028a9fd669bSKonstantin Belousov 	u_char pri;
1029f1a4b852SJohn Baldwin 
1030f1a4b852SJohn Baldwin 	MPASS(ts != NULL);
10312502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
1032f1a4b852SJohn Baldwin 	MPASS(ts->ts_owner == curthread);
1033f1a4b852SJohn Baldwin 	MPASS(TAILQ_EMPTY(&ts->ts_pending));
1034f1a4b852SJohn Baldwin 	MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
1035f1a4b852SJohn Baldwin 	    !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
1036f1a4b852SJohn Baldwin 
1037f1a4b852SJohn Baldwin 	/*
1038f1a4b852SJohn Baldwin 	 * Remove the turnstile from this thread's list of contested locks
1039f1a4b852SJohn Baldwin 	 * since this thread doesn't own it anymore.  New threads will
1040f1a4b852SJohn Baldwin 	 * not be blocking on the turnstile until it is claimed by a new
1041f1a4b852SJohn Baldwin 	 * owner.
1042f1a4b852SJohn Baldwin 	 */
1043f1a4b852SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
1044f1a4b852SJohn Baldwin 	ts->ts_owner = NULL;
1045f1a4b852SJohn Baldwin 	LIST_REMOVE(ts, ts_link);
1046f1a4b852SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
1047f1a4b852SJohn Baldwin 
1048f1a4b852SJohn Baldwin 	/*
1049f1a4b852SJohn Baldwin 	 * Adjust the priority of curthread based on other contested
1050f1a4b852SJohn Baldwin 	 * locks it owns.  Don't lower the priority below the base
1051f1a4b852SJohn Baldwin 	 * priority however.
1052f1a4b852SJohn Baldwin 	 */
1053f1a4b852SJohn Baldwin 	td = curthread;
10542502c107SJeff Roberson 	thread_lock(td);
10552502c107SJeff Roberson 	mtx_unlock_spin(&ts->ts_lock);
1056f1a4b852SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
1057a9fd669bSKonstantin Belousov 	pri = turnstile_calc_unlend_prio_locked(td);
1058f1a4b852SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
1059f1a4b852SJohn Baldwin 	sched_unlend_prio(td, pri);
10602502c107SJeff Roberson 	thread_unlock(td);
1061f1a4b852SJohn Baldwin }
1062f1a4b852SJohn Baldwin 
1063f1a4b852SJohn Baldwin /*
1064961a7b24SJohn Baldwin  * Return the first thread in a turnstile.
10659ed346baSBosko Milekic  */
1066961a7b24SJohn Baldwin struct thread *
turnstile_head(struct turnstile * ts,int queue)10677aa4f685SJohn Baldwin turnstile_head(struct turnstile *ts, int queue)
10680cde2e34SJason Evans {
1069961a7b24SJohn Baldwin #ifdef INVARIANTS
10705cb0fbe4SJohn Baldwin 
1071961a7b24SJohn Baldwin 	MPASS(ts != NULL);
10727aa4f685SJohn Baldwin 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
10732502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
10740cde2e34SJason Evans #endif
10757aa4f685SJohn Baldwin 	return (TAILQ_FIRST(&ts->ts_blocked[queue]));
1076961a7b24SJohn Baldwin }
10777aa4f685SJohn Baldwin 
1078f1a4b852SJohn Baldwin /*
1079f1a4b852SJohn Baldwin  * Returns true if a sub-queue of a turnstile is empty.
1080f1a4b852SJohn Baldwin  */
1081f1a4b852SJohn Baldwin int
turnstile_empty(struct turnstile * ts,int queue)1082f1a4b852SJohn Baldwin turnstile_empty(struct turnstile *ts, int queue)
1083f1a4b852SJohn Baldwin {
1084f1a4b852SJohn Baldwin #ifdef INVARIANTS
1085f1a4b852SJohn Baldwin 
1086f1a4b852SJohn Baldwin 	MPASS(ts != NULL);
1087f1a4b852SJohn Baldwin 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
10882502c107SJeff Roberson 	mtx_assert(&ts->ts_lock, MA_OWNED);
1089f1a4b852SJohn Baldwin #endif
1090f1a4b852SJohn Baldwin 	return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
1091f1a4b852SJohn Baldwin }
1092f1a4b852SJohn Baldwin 
10937aa4f685SJohn Baldwin #ifdef DDB
10947aa4f685SJohn Baldwin static void
print_thread(struct thread * td,const char * prefix)10957aa4f685SJohn Baldwin print_thread(struct thread *td, const char *prefix)
10967aa4f685SJohn Baldwin {
10977aa4f685SJohn Baldwin 
10987aa4f685SJohn Baldwin 	db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid,
109986a448c3SKonstantin Belousov 	    td->td_proc->p_pid, td->td_name);
11007aa4f685SJohn Baldwin }
11017aa4f685SJohn Baldwin 
11027aa4f685SJohn Baldwin static void
print_queue(struct threadqueue * queue,const char * header,const char * prefix)11037aa4f685SJohn Baldwin print_queue(struct threadqueue *queue, const char *header, const char *prefix)
11047aa4f685SJohn Baldwin {
11057aa4f685SJohn Baldwin 	struct thread *td;
11067aa4f685SJohn Baldwin 
11077aa4f685SJohn Baldwin 	db_printf("%s:\n", header);
11087aa4f685SJohn Baldwin 	if (TAILQ_EMPTY(queue)) {
11097aa4f685SJohn Baldwin 		db_printf("%sempty\n", prefix);
11107aa4f685SJohn Baldwin 		return;
11117aa4f685SJohn Baldwin 	}
11127aa4f685SJohn Baldwin 	TAILQ_FOREACH(td, queue, td_lockq) {
11137aa4f685SJohn Baldwin 		print_thread(td, prefix);
11147aa4f685SJohn Baldwin 	}
11157aa4f685SJohn Baldwin }
11167aa4f685SJohn Baldwin 
DB_SHOW_COMMAND(turnstile,db_show_turnstile)11177aa4f685SJohn Baldwin DB_SHOW_COMMAND(turnstile, db_show_turnstile)
11187aa4f685SJohn Baldwin {
11197aa4f685SJohn Baldwin 	struct turnstile_chain *tc;
11207aa4f685SJohn Baldwin 	struct turnstile *ts;
11217aa4f685SJohn Baldwin 	struct lock_object *lock;
11227aa4f685SJohn Baldwin 	int i;
11237aa4f685SJohn Baldwin 
11247aa4f685SJohn Baldwin 	if (!have_addr)
11257aa4f685SJohn Baldwin 		return;
11267aa4f685SJohn Baldwin 
11277aa4f685SJohn Baldwin 	/*
11287aa4f685SJohn Baldwin 	 * First, see if there is an active turnstile for the lock indicated
11297aa4f685SJohn Baldwin 	 * by the address.
11307aa4f685SJohn Baldwin 	 */
11317aa4f685SJohn Baldwin 	lock = (struct lock_object *)addr;
11327aa4f685SJohn Baldwin 	tc = TC_LOOKUP(lock);
11337aa4f685SJohn Baldwin 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
11347aa4f685SJohn Baldwin 		if (ts->ts_lockobj == lock)
11357aa4f685SJohn Baldwin 			goto found;
11367aa4f685SJohn Baldwin 
11377aa4f685SJohn Baldwin 	/*
11387aa4f685SJohn Baldwin 	 * Second, see if there is an active turnstile at the address
11397aa4f685SJohn Baldwin 	 * indicated.
11407aa4f685SJohn Baldwin 	 */
11417aa4f685SJohn Baldwin 	for (i = 0; i < TC_TABLESIZE; i++)
11427aa4f685SJohn Baldwin 		LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) {
11437aa4f685SJohn Baldwin 			if (ts == (struct turnstile *)addr)
11447aa4f685SJohn Baldwin 				goto found;
11457aa4f685SJohn Baldwin 		}
11467aa4f685SJohn Baldwin 
11477aa4f685SJohn Baldwin 	db_printf("Unable to locate a turnstile via %p\n", (void *)addr);
11487aa4f685SJohn Baldwin 	return;
11497aa4f685SJohn Baldwin found:
11507aa4f685SJohn Baldwin 	lock = ts->ts_lockobj;
11517aa4f685SJohn Baldwin 	db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name,
11527aa4f685SJohn Baldwin 	    lock->lo_name);
11537aa4f685SJohn Baldwin 	if (ts->ts_owner)
11547aa4f685SJohn Baldwin 		print_thread(ts->ts_owner, "Lock Owner: ");
11557aa4f685SJohn Baldwin 	else
11567aa4f685SJohn Baldwin 		db_printf("Lock Owner: none\n");
11577aa4f685SJohn Baldwin 	print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t");
11587aa4f685SJohn Baldwin 	print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters",
11597aa4f685SJohn Baldwin 	    "\t");
11607aa4f685SJohn Baldwin 	print_queue(&ts->ts_pending, "Pending Threads", "\t");
11617aa4f685SJohn Baldwin 
11627aa4f685SJohn Baldwin }
1163ae110b53SJohn Baldwin 
116477e66268SJohn Baldwin /*
116577e66268SJohn Baldwin  * Show all the threads a particular thread is waiting on based on
11660c1d923eSConrad Meyer  * non-spin locks.
116777e66268SJohn Baldwin  */
1168ae110b53SJohn Baldwin static void
print_lockchain(struct thread * td,const char * prefix)116977e66268SJohn Baldwin print_lockchain(struct thread *td, const char *prefix)
1170ae110b53SJohn Baldwin {
1171ae110b53SJohn Baldwin 	struct lock_object *lock;
1172ae110b53SJohn Baldwin 	struct lock_class *class;
1173ae110b53SJohn Baldwin 	struct turnstile *ts;
11740c1d923eSConrad Meyer 	struct thread *owner;
1175ae110b53SJohn Baldwin 
1176ae110b53SJohn Baldwin 	/*
1177ae110b53SJohn Baldwin 	 * Follow the chain.  We keep walking as long as the thread is
11780c1d923eSConrad Meyer 	 * blocked on a lock that has an owner.
1179ae110b53SJohn Baldwin 	 */
1180fed79884SJohn Baldwin 	while (!db_pager_quit) {
11811e9ee2b5SConrad Meyer 		if (td == (void *)LK_KERNPROC) {
11821e9ee2b5SConrad Meyer 			db_printf("%sdisowned (LK_KERNPROC)\n", prefix);
11831e9ee2b5SConrad Meyer 			return;
11841e9ee2b5SConrad Meyer 		}
11851e9ee2b5SConrad Meyer 		db_printf("%sthread %d (pid %d, %s) is ", prefix, td->td_tid,
118686a448c3SKonstantin Belousov 		    td->td_proc->p_pid, td->td_name);
1187fa2528acSAlex Richardson 		switch (TD_GET_STATE(td)) {
1188ae110b53SJohn Baldwin 		case TDS_INACTIVE:
11891e9ee2b5SConrad Meyer 			db_printf("inactive\n");
1190ae110b53SJohn Baldwin 			return;
1191ae110b53SJohn Baldwin 		case TDS_CAN_RUN:
11921e9ee2b5SConrad Meyer 			db_printf("runnable\n");
1193ae110b53SJohn Baldwin 			return;
1194ae110b53SJohn Baldwin 		case TDS_RUNQ:
11951e9ee2b5SConrad Meyer 			db_printf("on a run queue\n");
1196ae110b53SJohn Baldwin 			return;
1197ae110b53SJohn Baldwin 		case TDS_RUNNING:
1198ae110b53SJohn Baldwin 			db_printf("running on CPU %d\n", td->td_oncpu);
1199ae110b53SJohn Baldwin 			return;
1200ae110b53SJohn Baldwin 		case TDS_INHIBITED:
1201ae110b53SJohn Baldwin 			if (TD_ON_LOCK(td)) {
1202ae110b53SJohn Baldwin 				ts = td->td_blocked;
1203ae110b53SJohn Baldwin 				lock = ts->ts_lockobj;
1204ae110b53SJohn Baldwin 				class = LOCK_CLASS(lock);
1205ae110b53SJohn Baldwin 				db_printf("blocked on lock %p (%s) \"%s\"\n",
1206ae110b53SJohn Baldwin 				    lock, class->lc_name, lock->lo_name);
1207ae110b53SJohn Baldwin 				if (ts->ts_owner == NULL)
1208ae110b53SJohn Baldwin 					return;
1209ae110b53SJohn Baldwin 				td = ts->ts_owner;
1210ae110b53SJohn Baldwin 				break;
12110c1d923eSConrad Meyer 			} else if (TD_ON_SLEEPQ(td)) {
12120c1d923eSConrad Meyer 				if (!lockmgr_chain(td, &owner) &&
12130c1d923eSConrad Meyer 				    !sx_chain(td, &owner)) {
12140c1d923eSConrad Meyer 					db_printf("sleeping on %p \"%s\"\n",
12150c1d923eSConrad Meyer 					    td->td_wchan, td->td_wmesg);
12160c1d923eSConrad Meyer 					return;
12170c1d923eSConrad Meyer 				}
12180c1d923eSConrad Meyer 				if (owner == NULL)
12190c1d923eSConrad Meyer 					return;
12200c1d923eSConrad Meyer 				td = owner;
12210c1d923eSConrad Meyer 				break;
1222ae110b53SJohn Baldwin 			}
12231e9ee2b5SConrad Meyer 			db_printf("inhibited: %s\n", KTDSTATE(td));
1224ae110b53SJohn Baldwin 			return;
1225ae110b53SJohn Baldwin 		default:
1226fa2528acSAlex Richardson 			db_printf("??? (%#x)\n", TD_GET_STATE(td));
1227ae110b53SJohn Baldwin 			return;
1228ae110b53SJohn Baldwin 		}
1229ae110b53SJohn Baldwin 	}
1230ae110b53SJohn Baldwin }
1231ae110b53SJohn Baldwin 
DB_SHOW_COMMAND(lockchain,db_show_lockchain)123277e66268SJohn Baldwin DB_SHOW_COMMAND(lockchain, db_show_lockchain)
1233ae110b53SJohn Baldwin {
1234ae110b53SJohn Baldwin 	struct thread *td;
1235ae110b53SJohn Baldwin 
1236ae110b53SJohn Baldwin 	/* Figure out which thread to start with. */
1237ae110b53SJohn Baldwin 	if (have_addr)
1238cd508278SPedro F. Giffuni 		td = db_lookup_thread(addr, true);
1239ae110b53SJohn Baldwin 	else
1240ae110b53SJohn Baldwin 		td = kdb_thread;
1241ae110b53SJohn Baldwin 
124277e66268SJohn Baldwin 	print_lockchain(td, "");
1243ae110b53SJohn Baldwin }
12440c1d923eSConrad Meyer DB_SHOW_ALIAS(sleepchain, db_show_lockchain);
1245ae110b53SJohn Baldwin 
DB_SHOW_ALL_COMMAND(chains,db_show_allchains)124639297ba4SSam Leffler DB_SHOW_ALL_COMMAND(chains, db_show_allchains)
1247ae110b53SJohn Baldwin {
1248ae110b53SJohn Baldwin 	struct thread *td;
1249ae110b53SJohn Baldwin 	struct proc *p;
1250ae110b53SJohn Baldwin 	int i;
1251ae110b53SJohn Baldwin 
1252ae110b53SJohn Baldwin 	i = 1;
12534f506694SXin LI 	FOREACH_PROC_IN_SYSTEM(p) {
1254ae110b53SJohn Baldwin 		FOREACH_THREAD_IN_PROC(p, td) {
12550c1d923eSConrad Meyer 			if ((TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested))
12560c1d923eSConrad Meyer 			    || (TD_IS_INHIBITED(td) && TD_ON_SLEEPQ(td))) {
1257ae110b53SJohn Baldwin 				db_printf("chain %d:\n", i++);
125877e66268SJohn Baldwin 				print_lockchain(td, " ");
1259ae110b53SJohn Baldwin 			}
1260fed79884SJohn Baldwin 			if (db_pager_quit)
1261fed79884SJohn Baldwin 				return;
1262ae110b53SJohn Baldwin 		}
1263ae110b53SJohn Baldwin 	}
1264ae110b53SJohn Baldwin }
12653a9e3ed6SJohn Baldwin DB_SHOW_ALIAS_FLAGS(allchains, db_show_allchains, DB_CMD_MEMSAFE);
1266ae110b53SJohn Baldwin 
1267ae110b53SJohn Baldwin static void	print_waiters(struct turnstile *ts, int indent);
1268ae110b53SJohn Baldwin 
1269ae110b53SJohn Baldwin static void
print_waiter(struct thread * td,int indent)1270ae110b53SJohn Baldwin print_waiter(struct thread *td, int indent)
1271ae110b53SJohn Baldwin {
1272ae110b53SJohn Baldwin 	struct turnstile *ts;
1273ae110b53SJohn Baldwin 	int i;
1274ae110b53SJohn Baldwin 
1275fed79884SJohn Baldwin 	if (db_pager_quit)
1276fed79884SJohn Baldwin 		return;
1277ae110b53SJohn Baldwin 	for (i = 0; i < indent; i++)
1278ae110b53SJohn Baldwin 		db_printf(" ");
1279ae110b53SJohn Baldwin 	print_thread(td, "thread ");
1280ae110b53SJohn Baldwin 	LIST_FOREACH(ts, &td->td_contested, ts_link)
1281ae110b53SJohn Baldwin 		print_waiters(ts, indent + 1);
1282ae110b53SJohn Baldwin }
1283ae110b53SJohn Baldwin 
1284ae110b53SJohn Baldwin static void
print_waiters(struct turnstile * ts,int indent)1285ae110b53SJohn Baldwin print_waiters(struct turnstile *ts, int indent)
1286ae110b53SJohn Baldwin {
1287ae110b53SJohn Baldwin 	struct lock_object *lock;
1288ae110b53SJohn Baldwin 	struct lock_class *class;
1289ae110b53SJohn Baldwin 	struct thread *td;
1290ae110b53SJohn Baldwin 	int i;
1291ae110b53SJohn Baldwin 
1292fed79884SJohn Baldwin 	if (db_pager_quit)
1293fed79884SJohn Baldwin 		return;
1294ae110b53SJohn Baldwin 	lock = ts->ts_lockobj;
1295ae110b53SJohn Baldwin 	class = LOCK_CLASS(lock);
1296ae110b53SJohn Baldwin 	for (i = 0; i < indent; i++)
1297ae110b53SJohn Baldwin 		db_printf(" ");
1298ae110b53SJohn Baldwin 	db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
1299ae110b53SJohn Baldwin 	TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
1300ae110b53SJohn Baldwin 		print_waiter(td, indent + 1);
1301ae110b53SJohn Baldwin 	TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
1302ae110b53SJohn Baldwin 		print_waiter(td, indent + 1);
1303ae110b53SJohn Baldwin 	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
1304ae110b53SJohn Baldwin 		print_waiter(td, indent + 1);
1305ae110b53SJohn Baldwin }
1306ae110b53SJohn Baldwin 
DB_SHOW_COMMAND(locktree,db_show_locktree)130777e66268SJohn Baldwin DB_SHOW_COMMAND(locktree, db_show_locktree)
1308ae110b53SJohn Baldwin {
1309ae110b53SJohn Baldwin 	struct lock_object *lock;
1310ae110b53SJohn Baldwin 	struct lock_class *class;
1311ae110b53SJohn Baldwin 	struct turnstile_chain *tc;
1312ae110b53SJohn Baldwin 	struct turnstile *ts;
1313ae110b53SJohn Baldwin 
1314ae110b53SJohn Baldwin 	if (!have_addr)
1315ae110b53SJohn Baldwin 		return;
1316ae110b53SJohn Baldwin 	lock = (struct lock_object *)addr;
1317ae110b53SJohn Baldwin 	tc = TC_LOOKUP(lock);
1318ae110b53SJohn Baldwin 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1319ae110b53SJohn Baldwin 		if (ts->ts_lockobj == lock)
1320ae110b53SJohn Baldwin 			break;
1321ae110b53SJohn Baldwin 	if (ts == NULL) {
1322ae110b53SJohn Baldwin 		class = LOCK_CLASS(lock);
1323ae110b53SJohn Baldwin 		db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
1324ae110b53SJohn Baldwin 		    lock->lo_name);
1325ae110b53SJohn Baldwin 	} else
1326ae110b53SJohn Baldwin 		print_waiters(ts, 0);
1327ae110b53SJohn Baldwin }
13287aa4f685SJohn Baldwin #endif
1329