subr_turnstile.c revision 234190
165557Sjasone/*-
265557Sjasone * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
365557Sjasone *
465557Sjasone * Redistribution and use in source and binary forms, with or without
565557Sjasone * modification, are permitted provided that the following conditions
665557Sjasone * are met:
765557Sjasone * 1. Redistributions of source code must retain the above copyright
865557Sjasone *    notice, this list of conditions and the following disclaimer.
965557Sjasone * 2. Redistributions in binary form must reproduce the above copyright
1065557Sjasone *    notice, this list of conditions and the following disclaimer in the
1165557Sjasone *    documentation and/or other materials provided with the distribution.
1265557Sjasone * 3. Berkeley Software Design Inc's name may not be used to endorse or
1365557Sjasone *    promote products derived from this software without specific prior
1465557Sjasone *    written permission.
1565557Sjasone *
1665557Sjasone * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
1765557Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1865557Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1965557Sjasone * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
2065557Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2165557Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2265557Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2365557Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2465557Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2565557Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2665557Sjasone * SUCH DAMAGE.
2765557Sjasone *
2865557Sjasone *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
2967352Sjhb *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
3065557Sjasone */
3165557Sjasone
3265557Sjasone/*
33122514Sjhb * Implementation of turnstiles used to hold queue of threads blocked on
34122514Sjhb * non-sleepable locks.  Sleepable locks use condition variables to
35122514Sjhb * implement their queues.  Turnstiles differ from a sleep queue in that
36122514Sjhb * turnstile queue's are assigned to a lock held by an owning thread.  Thus,
37122514Sjhb * when one thread is enqueued onto a turnstile, it can lend its priority
38122514Sjhb * to the owning thread.
39122514Sjhb *
40122514Sjhb * We wish to avoid bloating locks with an embedded turnstile and we do not
41122514Sjhb * want to use back-pointers in the locks for the same reason.  Thus, we
42122514Sjhb * use a similar approach to that of Solaris 7 as described in Solaris
43122514Sjhb * Internals by Jim Mauro and Richard McDougall.  Turnstiles are looked up
44122514Sjhb * in a hash table based on the address of the lock.  Each entry in the
45122514Sjhb * hash table is a linked-lists of turnstiles and is called a turnstile
46122514Sjhb * chain.  Each chain contains a spin mutex that protects all of the
47122514Sjhb * turnstiles in the chain.
48122514Sjhb *
49169666Sjeff * Each time a thread is created, a turnstile is allocated from a UMA zone
50169666Sjeff * and attached to that thread.  When a thread blocks on a lock, if it is the
51169666Sjeff * first thread to block, it lends its turnstile to the lock.  If the lock
52169666Sjeff * already has a turnstile, then it gives its turnstile to the lock's
53169666Sjeff * turnstile's free list.  When a thread is woken up, it takes a turnstile from
54169666Sjeff * the free list if there are any other waiters.  If it is the only thread
55169666Sjeff * blocked on the lock, then it reclaims the turnstile associated with the lock
56169666Sjeff * and removes it from the hash table.
5772200Sbmilekic */
5872200Sbmilekic
59116182Sobrien#include <sys/cdefs.h>
60116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_turnstile.c 234190 2012-04-12 17:43:59Z jhb $");
61116182Sobrien
62154937Sjhb#include "opt_ddb.h"
63154937Sjhb#include "opt_turnstile_profiling.h"
64170640Sjeff#include "opt_sched.h"
65154937Sjhb
6665557Sjasone#include <sys/param.h>
6793609Sdes#include <sys/systm.h>
6867352Sjhb#include <sys/kernel.h>
6993609Sdes#include <sys/ktr.h>
7076166Smarkm#include <sys/lock.h>
7174912Sjhb#include <sys/mutex.h>
7265557Sjasone#include <sys/proc.h>
73122514Sjhb#include <sys/queue.h>
74131259Sjhb#include <sys/sched.h>
75131259Sjhb#include <sys/sysctl.h>
76122514Sjhb#include <sys/turnstile.h>
7765557Sjasone
78169666Sjeff#include <vm/uma.h>
79169666Sjeff
80154937Sjhb#ifdef DDB
81158031Sjhb#include <sys/kdb.h>
82154937Sjhb#include <ddb/ddb.h>
83161337Sjhb#include <sys/lockmgr.h>
84161337Sjhb#include <sys/sx.h>
85154937Sjhb#endif
86154937Sjhb
8765557Sjasone/*
88122514Sjhb * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
89122514Sjhb * number chosen because the sleep queue's use the same value for the
90122514Sjhb * shift.  Basically, we ignore the lower 8 bits of the address.
91122514Sjhb * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
9271352Sjasone */
93122514Sjhb#define	TC_TABLESIZE	128			/* Must be power of 2. */
94122514Sjhb#define	TC_MASK		(TC_TABLESIZE - 1)
95122514Sjhb#define	TC_SHIFT	8
96122514Sjhb#define	TC_HASH(lock)	(((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
97122514Sjhb#define	TC_LOOKUP(lock)	&turnstile_chains[TC_HASH(lock)]
9871352Sjasone
9971352Sjasone/*
100122514Sjhb * There are three different lists of turnstiles as follows.  The list
101122514Sjhb * connected by ts_link entries is a per-thread list of all the turnstiles
102122514Sjhb * attached to locks that we own.  This is used to fixup our priority when
103122514Sjhb * a lock is released.  The other two lists use the ts_hash entries.  The
104126317Sjhb * first of these two is the turnstile chain list that a turnstile is on
105126317Sjhb * when it is attached to a lock.  The second list to use ts_hash is the
106126317Sjhb * free list hung off of a turnstile that is attached to a lock.
107122514Sjhb *
108154937Sjhb * Each turnstile contains three lists of threads.  The two ts_blocked lists
109154937Sjhb * are linked list of threads blocked on the turnstile's lock.  One list is
110154937Sjhb * for exclusive waiters, and the other is for shared waiters.  The
111126884Sjhb * ts_pending list is a linked list of threads previously awakened by
112122514Sjhb * turnstile_signal() or turnstile_wait() that are waiting to be put on
113122514Sjhb * the run queue.
114122514Sjhb *
115122514Sjhb * Locking key:
116122514Sjhb *  c - turnstile chain lock
117122514Sjhb *  q - td_contested lock
11871352Sjasone */
119122514Sjhbstruct turnstile {
120170295Sjeff	struct mtx ts_lock;			/* Spin lock for self. */
121154937Sjhb	struct threadqueue ts_blocked[2];	/* (c + q) Blocked threads. */
122154937Sjhb	struct threadqueue ts_pending;		/* (c) Pending threads. */
123122514Sjhb	LIST_ENTRY(turnstile) ts_hash;		/* (c) Chain and free list. */
124122514Sjhb	LIST_ENTRY(turnstile) ts_link;		/* (q) Contested locks. */
125122514Sjhb	LIST_HEAD(, turnstile) ts_free;		/* (c) Free turnstiles. */
126122514Sjhb	struct lock_object *ts_lockobj;		/* (c) Lock we reference. */
127122590Sjhb	struct thread *ts_owner;		/* (c + q) Who owns the lock. */
12874912Sjhb};
129122514Sjhb
130122514Sjhbstruct turnstile_chain {
131122514Sjhb	LIST_HEAD(, turnstile) tc_turnstiles;	/* List of turnstiles. */
132122514Sjhb	struct mtx tc_lock;			/* Spin lock for this chain. */
133131259Sjhb#ifdef TURNSTILE_PROFILING
134131259Sjhb	u_int	tc_depth;			/* Length of tc_queues. */
135131259Sjhb	u_int	tc_max_depth;			/* Max length of tc_queues. */
136131259Sjhb#endif
13774912Sjhb};
13871352Sjasone
139131259Sjhb#ifdef TURNSTILE_PROFILING
140131259Sjhbu_int turnstile_max_depth;
141227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0,
142227309Sed    "turnstile profiling");
143227309Sedstatic SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
144131259Sjhb    "turnstile chain stats");
145131259SjhbSYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
146131259Sjhb    &turnstile_max_depth, 0, "maxmimum depth achieved of a single chain");
147131259Sjhb#endif
148122514Sjhbstatic struct mtx td_contested_lock;
149122514Sjhbstatic struct turnstile_chain turnstile_chains[TC_TABLESIZE];
150169666Sjeffstatic uma_zone_t turnstile_zone;
15193702Sjhb
15293702Sjhb/*
15372200Sbmilekic * Prototypes for non-exported routines.
15472200Sbmilekic */
155122514Sjhbstatic void	init_turnstile0(void *dummy);
156131263Sjhb#ifdef TURNSTILE_PROFILING
157131263Sjhbstatic void	init_turnstile_profiling(void *arg);
158131263Sjhb#endif
159139453Sjhbstatic void	propagate_priority(struct thread *td);
160139453Sjhbstatic int	turnstile_adjust_thread(struct turnstile *ts,
161139453Sjhb		    struct thread *td);
162154937Sjhbstatic struct thread *turnstile_first_waiter(struct turnstile *ts);
163122514Sjhbstatic void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
164169666Sjeff#ifdef INVARIANTS
165169666Sjeffstatic void	turnstile_dtor(void *mem, int size, void *arg);
166169666Sjeff#endif
167169666Sjeffstatic int	turnstile_init(void *mem, int size, int flags);
168170295Sjeffstatic void	turnstile_fini(void *mem, int size);
16967352Sjhb
170122514Sjhb/*
171122514Sjhb * Walks the chain of turnstiles and their owners to propagate the priority
172122514Sjhb * of the thread being blocked to all the threads holding locks that have to
173122514Sjhb * release their locks before this thread can run again.
174122514Sjhb */
17567352Sjhbstatic void
17683366Sjulianpropagate_priority(struct thread *td)
17767352Sjhb{
178122514Sjhb	struct turnstile *ts;
179122514Sjhb	int pri;
18067352Sjhb
181170295Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
182122514Sjhb	pri = td->td_priority;
183122514Sjhb	ts = td->td_blocked;
184176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
185170295Sjeff	/*
186170295Sjeff	 * Grab a recursive lock on this turnstile chain so it stays locked
187170295Sjeff	 * for the whole operation.  The caller expects us to return with
188170295Sjeff	 * the original lock held.  We only ever lock down the chain so
189170295Sjeff	 * the lock order is constant.
190170295Sjeff	 */
191170295Sjeff	mtx_lock_spin(&ts->ts_lock);
19267352Sjhb	for (;;) {
193122514Sjhb		td = ts->ts_owner;
19467352Sjhb
19583366Sjulian		if (td == NULL) {
19667352Sjhb			/*
197154937Sjhb			 * This might be a read lock with no owner.  There's
198154937Sjhb			 * not much we can do, so just bail.
19967352Sjhb			 */
200170295Sjeff			mtx_unlock_spin(&ts->ts_lock);
20167352Sjhb			return;
20267352Sjhb		}
20372200Sbmilekic
204170295Sjeff		thread_lock_flags(td, MTX_DUPOK);
205170295Sjeff		mtx_unlock_spin(&ts->ts_lock);
20699072Sjulian		MPASS(td->td_proc != NULL);
20783366Sjulian		MPASS(td->td_proc->p_magic == P_MAGIC);
208122514Sjhb
209122514Sjhb		/*
210157275Sjhb		 * If the thread is asleep, then we are probably about
211157275Sjhb		 * to deadlock.  To make debugging this easier, just
212157275Sjhb		 * panic and tell the user which thread misbehaved so
213157275Sjhb		 * they can hopefully get a stack trace from the truly
214157275Sjhb		 * misbehaving thread.
215122514Sjhb		 */
216157275Sjhb		if (TD_IS_SLEEPING(td)) {
217157275Sjhb			printf(
218157275Sjhb		"Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
219157275Sjhb			    td->td_tid, td->td_proc->p_pid);
220234190Sjhb			kdb_backtrace_thread(td);
221157275Sjhb			panic("sleeping thread");
222157275Sjhb		}
223122514Sjhb
224122514Sjhb		/*
225122514Sjhb		 * If this thread already has higher priority than the
226122514Sjhb		 * thread that is being blocked, we are finished.
227122514Sjhb		 */
228170295Sjeff		if (td->td_priority <= pri) {
229170295Sjeff			thread_unlock(td);
23067352Sjhb			return;
231170295Sjeff		}
23269376Sjhb
23369376Sjhb		/*
234139453Sjhb		 * Bump this thread's priority.
23567352Sjhb		 */
236139453Sjhb		sched_lend_prio(td, pri);
237139453Sjhb
238139453Sjhb		/*
239139453Sjhb		 * If lock holder is actually running or on the run queue
240139453Sjhb		 * then we are done.
241139453Sjhb		 */
242139453Sjhb		if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
243139453Sjhb			MPASS(td->td_blocked == NULL);
244170295Sjeff			thread_unlock(td);
24567352Sjhb			return;
24667352Sjhb		}
24772376Sjake
24873912Sjhb#ifndef SMP
24967352Sjhb		/*
25083366Sjulian		 * For UP, we check to see if td is curthread (this shouldn't
25173912Sjhb		 * ever happen however as it would mean we are in a deadlock.)
25273912Sjhb		 */
25383366Sjulian		KASSERT(td != curthread, ("Deadlock detected"));
25473912Sjhb#endif
25573912Sjhb
25673912Sjhb		/*
257122514Sjhb		 * If we aren't blocked on a lock, we should be.
25867352Sjhb		 */
259104387Sjhb		KASSERT(TD_ON_LOCK(td), (
260139453Sjhb		    "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
261173600Sjulian		    td->td_tid, td->td_name, td->td_state,
262122514Sjhb		    ts->ts_lockobj->lo_name));
26367352Sjhb
26467352Sjhb		/*
265122514Sjhb		 * Pick up the lock that td is blocked on.
26667352Sjhb		 */
267122514Sjhb		ts = td->td_blocked;
268122514Sjhb		MPASS(ts != NULL);
269176078Sjeff		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
270139453Sjhb		/* Resort td on the list if needed. */
271139453Sjhb		if (!turnstile_adjust_thread(ts, td)) {
272170295Sjeff			mtx_unlock_spin(&ts->ts_lock);
273122590Sjhb			return;
274122590Sjhb		}
275170295Sjeff		/* The thread lock is released as ts lock above. */
276139453Sjhb	}
277139453Sjhb}
278122590Sjhb
279139453Sjhb/*
280139453Sjhb * Adjust the thread's position on a turnstile after its priority has been
281139453Sjhb * changed.
282139453Sjhb */
283139453Sjhbstatic int
284139453Sjhbturnstile_adjust_thread(struct turnstile *ts, struct thread *td)
285139453Sjhb{
286139453Sjhb	struct thread *td1, *td2;
287154937Sjhb	int queue;
28872200Sbmilekic
289170295Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
290139453Sjhb	MPASS(TD_ON_LOCK(td));
29167352Sjhb
292139453Sjhb	/*
293139453Sjhb	 * This thread may not be blocked on this turnstile anymore
294139453Sjhb	 * but instead might already be woken up on another CPU
295170295Sjeff	 * that is waiting on the thread lock in turnstile_unpend() to
296139453Sjhb	 * finish waking this thread up.  We can detect this case
297139453Sjhb	 * by checking to see if this thread has been given a
298139453Sjhb	 * turnstile by either turnstile_signal() or
299139453Sjhb	 * turnstile_broadcast().  In this case, treat the thread as
300139453Sjhb	 * if it was already running.
301139453Sjhb	 */
302139453Sjhb	if (td->td_turnstile != NULL)
303139453Sjhb		return (0);
304139453Sjhb
305139453Sjhb	/*
306139453Sjhb	 * Check if the thread needs to be moved on the blocked chain.
307139453Sjhb	 * It needs to be moved if either its priority is lower than
308139453Sjhb	 * the previous thread or higher than the next thread.
309139453Sjhb	 */
310176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
311139453Sjhb	td1 = TAILQ_PREV(td, threadqueue, td_lockq);
312139453Sjhb	td2 = TAILQ_NEXT(td, td_lockq);
313139453Sjhb	if ((td1 != NULL && td->td_priority < td1->td_priority) ||
314139453Sjhb	    (td2 != NULL && td->td_priority > td2->td_priority)) {
315139453Sjhb
31667352Sjhb		/*
31783366Sjulian		 * Remove thread from blocked chain and determine where
318139453Sjhb		 * it should be moved to.
31967352Sjhb		 */
320154937Sjhb		queue = td->td_tsqueue;
321154937Sjhb		MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
322122514Sjhb		mtx_lock_spin(&td_contested_lock);
323154937Sjhb		TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
324154937Sjhb		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
32583366Sjulian			MPASS(td1->td_proc->p_magic == P_MAGIC);
326139453Sjhb			if (td1->td_priority > td->td_priority)
32767352Sjhb				break;
32867352Sjhb		}
32972200Sbmilekic
330139453Sjhb		if (td1 == NULL)
331154937Sjhb			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
332139453Sjhb		else
333139453Sjhb			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
334122514Sjhb		mtx_unlock_spin(&td_contested_lock);
335139453Sjhb		if (td1 == NULL)
336139453Sjhb			CTR3(KTR_LOCK,
337139453Sjhb		    "turnstile_adjust_thread: td %d put at tail on [%p] %s",
338139453Sjhb			    td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
339139453Sjhb		else
340139453Sjhb			CTR4(KTR_LOCK,
341139453Sjhb		    "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
342139453Sjhb			    td->td_tid, td1->td_tid, ts->ts_lockobj,
343139453Sjhb			    ts->ts_lockobj->lo_name);
34467352Sjhb	}
345139453Sjhb	return (1);
34667352Sjhb}
34767352Sjhb
34871352Sjasone/*
349122514Sjhb * Early initialization of turnstiles.  This is not done via a SYSINIT()
350122514Sjhb * since this needs to be initialized very early when mutexes are first
351122514Sjhb * initialized.
35293609Sdes */
353122514Sjhbvoid
354122514Sjhbinit_turnstiles(void)
35593667Sdes{
356122514Sjhb	int i;
35793667Sdes
358122514Sjhb	for (i = 0; i < TC_TABLESIZE; i++) {
359122514Sjhb		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
360122514Sjhb		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
361122514Sjhb		    NULL, MTX_SPIN);
362131263Sjhb	}
363131263Sjhb	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
364154482Sjhb	LIST_INIT(&thread0.td_contested);
365131263Sjhb	thread0.td_turnstile = NULL;
366131263Sjhb}
367131263Sjhb
368131259Sjhb#ifdef TURNSTILE_PROFILING
369131263Sjhbstatic void
370131263Sjhbinit_turnstile_profiling(void *arg)
371131263Sjhb{
372131263Sjhb	struct sysctl_oid *chain_oid;
373131263Sjhb	char chain_name[10];
374131263Sjhb	int i;
375131263Sjhb
376131263Sjhb	for (i = 0; i < TC_TABLESIZE; i++) {
377131259Sjhb		snprintf(chain_name, sizeof(chain_name), "%d", i);
378131259Sjhb		chain_oid = SYSCTL_ADD_NODE(NULL,
379131259Sjhb		    SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
380131259Sjhb		    chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
381131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
382131259Sjhb		    "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
383131259Sjhb		    NULL);
384131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
385131259Sjhb		    "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
386131259Sjhb		    0, NULL);
387122514Sjhb	}
38893667Sdes}
389131263SjhbSYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
390131263Sjhb    init_turnstile_profiling, NULL);
391131263Sjhb#endif
39293667Sdes
393122514Sjhbstatic void
394122514Sjhbinit_turnstile0(void *dummy)
39593609Sdes{
39693609Sdes
397169666Sjeff	turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
398182879Sjhb	    NULL,
399169666Sjeff#ifdef INVARIANTS
400182879Sjhb	    turnstile_dtor,
401169666Sjeff#else
402182879Sjhb	    NULL,
403169666Sjeff#endif
404182879Sjhb	    turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
405122514Sjhb	thread0.td_turnstile = turnstile_alloc();
40693609Sdes}
407122514SjhbSYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
40893609Sdes
40993609Sdes/*
410139453Sjhb * Update a thread on the turnstile list after it's priority has been changed.
411139453Sjhb * The old priority is passed in as an argument.
412139453Sjhb */
413139453Sjhbvoid
414139453Sjhbturnstile_adjust(struct thread *td, u_char oldpri)
415139453Sjhb{
416139453Sjhb	struct turnstile *ts;
417139453Sjhb
418139453Sjhb	MPASS(TD_ON_LOCK(td));
419139453Sjhb
420139453Sjhb	/*
421139453Sjhb	 * Pick up the lock that td is blocked on.
422139453Sjhb	 */
423139453Sjhb	ts = td->td_blocked;
424139453Sjhb	MPASS(ts != NULL);
425176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
426170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
427139453Sjhb
428139453Sjhb	/* Resort the turnstile on the list. */
429170295Sjeff	if (!turnstile_adjust_thread(ts, td))
430139453Sjhb		return;
431139453Sjhb	/*
432139453Sjhb	 * If our priority was lowered and we are at the head of the
433139453Sjhb	 * turnstile, then propagate our new priority up the chain.
434139453Sjhb	 * Note that we currently don't try to revoke lent priorities
435139453Sjhb	 * when our priority goes up.
436139453Sjhb	 */
437154937Sjhb	MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
438154937Sjhb	    td->td_tsqueue == TS_SHARED_QUEUE);
439154937Sjhb	if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
440154937Sjhb	    td->td_priority < oldpri) {
441139453Sjhb		propagate_priority(td);
442170295Sjeff	}
443139453Sjhb}
444139453Sjhb
445139453Sjhb/*
446122514Sjhb * Set the owner of the lock this turnstile is attached to.
44774900Sjhb */
448122514Sjhbstatic void
449122514Sjhbturnstile_setowner(struct turnstile *ts, struct thread *owner)
45074900Sjhb{
45174900Sjhb
452122514Sjhb	mtx_assert(&td_contested_lock, MA_OWNED);
453154937Sjhb	MPASS(ts->ts_owner == NULL);
454154937Sjhb
455154937Sjhb	/* A shared lock might not have an owner. */
456154937Sjhb	if (owner == NULL)
457154937Sjhb		return;
458154937Sjhb
459122514Sjhb	MPASS(owner->td_proc->p_magic == P_MAGIC);
460122514Sjhb	ts->ts_owner = owner;
461122514Sjhb	LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
46274900Sjhb}
46374900Sjhb
464169666Sjeff#ifdef INVARIANTS
465122514Sjhb/*
466169666Sjeff * UMA zone item deallocator.
467122514Sjhb */
468169666Sjeffstatic void
469169666Sjeffturnstile_dtor(void *mem, int size, void *arg)
47074900Sjhb{
471122514Sjhb	struct turnstile *ts;
47274900Sjhb
473169666Sjeff	ts = mem;
474169666Sjeff	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
475169666Sjeff	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
476169666Sjeff	MPASS(TAILQ_EMPTY(&ts->ts_pending));
477169666Sjeff}
478169666Sjeff#endif
479169666Sjeff
480169666Sjeff/*
481169666Sjeff * UMA zone item initializer.
482169666Sjeff */
483169666Sjeffstatic int
484169666Sjeffturnstile_init(void *mem, int size, int flags)
485169666Sjeff{
486169666Sjeff	struct turnstile *ts;
487169666Sjeff
488169666Sjeff	bzero(mem, size);
489169666Sjeff	ts = mem;
490154937Sjhb	TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
491154937Sjhb	TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
492122514Sjhb	TAILQ_INIT(&ts->ts_pending);
493122514Sjhb	LIST_INIT(&ts->ts_free);
494170295Sjeff	mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN | MTX_RECURSE);
495169666Sjeff	return (0);
49674900Sjhb}
49774900Sjhb
498170295Sjeffstatic void
499170295Sjeffturnstile_fini(void *mem, int size)
500170295Sjeff{
501170295Sjeff	struct turnstile *ts;
502170295Sjeff
503170295Sjeff	ts = mem;
504170295Sjeff	mtx_destroy(&ts->ts_lock);
505170295Sjeff}
506170295Sjeff
507122514Sjhb/*
508169666Sjeff * Get a turnstile for a new thread.
509169666Sjeff */
510169666Sjeffstruct turnstile *
511169666Sjeffturnstile_alloc(void)
512169666Sjeff{
513169666Sjeff
514169666Sjeff	return (uma_zalloc(turnstile_zone, M_WAITOK));
515169666Sjeff}
516169666Sjeff
517169666Sjeff/*
518122514Sjhb * Free a turnstile when a thread is destroyed.
519122514Sjhb */
52074900Sjhbvoid
521122514Sjhbturnstile_free(struct turnstile *ts)
52274900Sjhb{
52374900Sjhb
524169666Sjeff	uma_zfree(turnstile_zone, ts);
52574900Sjhb}
52674900Sjhb
52774900Sjhb/*
528136445Sjhb * Lock the turnstile chain associated with the specified lock.
529136445Sjhb */
530136445Sjhbvoid
531170295Sjeffturnstile_chain_lock(struct lock_object *lock)
532136445Sjhb{
533136445Sjhb	struct turnstile_chain *tc;
534136445Sjhb
535136445Sjhb	tc = TC_LOOKUP(lock);
536136445Sjhb	mtx_lock_spin(&tc->tc_lock);
537136445Sjhb}
538136445Sjhb
539170295Sjeffstruct turnstile *
540170295Sjeffturnstile_trywait(struct lock_object *lock)
541170295Sjeff{
542170295Sjeff	struct turnstile_chain *tc;
543170295Sjeff	struct turnstile *ts;
544170295Sjeff
545170295Sjeff	tc = TC_LOOKUP(lock);
546170295Sjeff	mtx_lock_spin(&tc->tc_lock);
547170295Sjeff	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
548170295Sjeff		if (ts->ts_lockobj == lock) {
549170295Sjeff			mtx_lock_spin(&ts->ts_lock);
550170295Sjeff			return (ts);
551170295Sjeff		}
552170295Sjeff
553170295Sjeff	ts = curthread->td_turnstile;
554170295Sjeff	MPASS(ts != NULL);
555170295Sjeff	mtx_lock_spin(&ts->ts_lock);
556170295Sjeff	KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
557170295Sjeff	ts->ts_lockobj = lock;
558170295Sjeff
559170295Sjeff	return (ts);
560170295Sjeff}
561170295Sjeff
562170295Sjeffvoid
563170295Sjeffturnstile_cancel(struct turnstile *ts)
564170295Sjeff{
565170295Sjeff	struct turnstile_chain *tc;
566170295Sjeff	struct lock_object *lock;
567170295Sjeff
568170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
569170295Sjeff
570170295Sjeff	mtx_unlock_spin(&ts->ts_lock);
571170295Sjeff	lock = ts->ts_lockobj;
572170295Sjeff	if (ts == curthread->td_turnstile)
573170295Sjeff		ts->ts_lockobj = NULL;
574170295Sjeff	tc = TC_LOOKUP(lock);
575170295Sjeff	mtx_unlock_spin(&tc->tc_lock);
576170295Sjeff}
577170295Sjeff
578136445Sjhb/*
579122514Sjhb * Look up the turnstile for a lock in the hash table locking the associated
580136445Sjhb * turnstile chain along the way.  If no turnstile is found in the hash
581136445Sjhb * table, NULL is returned.
58271352Sjasone */
583122514Sjhbstruct turnstile *
584122514Sjhbturnstile_lookup(struct lock_object *lock)
58571352Sjasone{
586122514Sjhb	struct turnstile_chain *tc;
587122514Sjhb	struct turnstile *ts;
58871352Sjasone
589122514Sjhb	tc = TC_LOOKUP(lock);
590136445Sjhb	mtx_assert(&tc->tc_lock, MA_OWNED);
591122514Sjhb	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
592170295Sjeff		if (ts->ts_lockobj == lock) {
593170295Sjeff			mtx_lock_spin(&ts->ts_lock);
594122514Sjhb			return (ts);
595170295Sjeff		}
596122514Sjhb	return (NULL);
59771352Sjasone}
59871352Sjasone
59971352Sjasone/*
600122514Sjhb * Unlock the turnstile chain associated with a given lock.
60171352Sjasone */
60272200Sbmilekicvoid
603170295Sjeffturnstile_chain_unlock(struct lock_object *lock)
60471352Sjasone{
605122514Sjhb	struct turnstile_chain *tc;
60671352Sjasone
607122514Sjhb	tc = TC_LOOKUP(lock);
608122514Sjhb	mtx_unlock_spin(&tc->tc_lock);
60972200Sbmilekic}
61072200Sbmilekic
61172200Sbmilekic/*
612154937Sjhb * Return a pointer to the thread waiting on this turnstile with the
613154937Sjhb * most important priority or NULL if the turnstile has no waiters.
614154937Sjhb */
615154937Sjhbstatic struct thread *
616154937Sjhbturnstile_first_waiter(struct turnstile *ts)
617154937Sjhb{
618154937Sjhb	struct thread *std, *xtd;
619154937Sjhb
620154937Sjhb	std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]);
621154937Sjhb	xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
622154937Sjhb	if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority))
623154937Sjhb		return (std);
624154937Sjhb	return (xtd);
625154937Sjhb}
626154937Sjhb
627154937Sjhb/*
628122514Sjhb * Take ownership of a turnstile and adjust the priority of the new
629122514Sjhb * owner appropriately.
63072200Sbmilekic */
63172200Sbmilekicvoid
632170295Sjeffturnstile_claim(struct turnstile *ts)
63372200Sbmilekic{
634170295Sjeff	struct thread *td, *owner;
635122514Sjhb	struct turnstile_chain *tc;
63672200Sbmilekic
637170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
638170295Sjeff	MPASS(ts != curthread->td_turnstile);
63972200Sbmilekic
640122514Sjhb	owner = curthread;
641122514Sjhb	mtx_lock_spin(&td_contested_lock);
642122514Sjhb	turnstile_setowner(ts, owner);
643122514Sjhb	mtx_unlock_spin(&td_contested_lock);
64472200Sbmilekic
645154937Sjhb	td = turnstile_first_waiter(ts);
646122514Sjhb	MPASS(td != NULL);
647122514Sjhb	MPASS(td->td_proc->p_magic == P_MAGIC);
648176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
64972200Sbmilekic
650122514Sjhb	/*
651122514Sjhb	 * Update the priority of the new owner if needed.
652122514Sjhb	 */
653170295Sjeff	thread_lock(owner);
654122514Sjhb	if (td->td_priority < owner->td_priority)
655139453Sjhb		sched_lend_prio(owner, td->td_priority);
656170295Sjeff	thread_unlock(owner);
657170295Sjeff	tc = TC_LOOKUP(ts->ts_lockobj);
658170295Sjeff	mtx_unlock_spin(&ts->ts_lock);
659170295Sjeff	mtx_unlock_spin(&tc->tc_lock);
66067352Sjhb}
66167352Sjhb
66272200Sbmilekic/*
663136445Sjhb * Block the current thread on the turnstile assicated with 'lock'.  This
664136445Sjhb * function will context switch and not return until this thread has been
665136445Sjhb * woken back up.  This function must be called with the appropriate
666136445Sjhb * turnstile chain locked and will return with it unlocked.
66772200Sbmilekic */
66867352Sjhbvoid
669170295Sjeffturnstile_wait(struct turnstile *ts, struct thread *owner, int queue)
67067352Sjhb{
671122514Sjhb	struct turnstile_chain *tc;
67283366Sjulian	struct thread *td, *td1;
673170295Sjeff	struct lock_object *lock;
67467352Sjhb
67583366Sjulian	td = curthread;
676170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
677154937Sjhb	if (owner)
678154937Sjhb		MPASS(owner->td_proc->p_magic == P_MAGIC);
679154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
68072200Sbmilekic
681136445Sjhb	/*
682136445Sjhb	 * If the lock does not already have a turnstile, use this thread's
683136445Sjhb	 * turnstile.  Otherwise insert the current thread into the
684136445Sjhb	 * turnstile already in use by this lock.
685136445Sjhb	 */
686170295Sjeff	tc = TC_LOOKUP(ts->ts_lockobj);
687218272Sjhb	mtx_assert(&tc->tc_lock, MA_OWNED);
688170295Sjeff	if (ts == td->td_turnstile) {
689131259Sjhb#ifdef TURNSTILE_PROFILING
690131259Sjhb		tc->tc_depth++;
691131259Sjhb		if (tc->tc_depth > tc->tc_max_depth) {
692131259Sjhb			tc->tc_max_depth = tc->tc_depth;
693131259Sjhb			if (tc->tc_max_depth > turnstile_max_depth)
694131259Sjhb				turnstile_max_depth = tc->tc_max_depth;
695131259Sjhb		}
696131259Sjhb#endif
697122514Sjhb		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
698122514Sjhb		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
699122514Sjhb		    ("thread's turnstile has pending threads"));
700154937Sjhb		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
701154937Sjhb		    ("thread's turnstile has exclusive waiters"));
702154937Sjhb		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
703154937Sjhb		    ("thread's turnstile has shared waiters"));
704122514Sjhb		KASSERT(LIST_EMPTY(&ts->ts_free),
705122514Sjhb		    ("thread's turnstile has a non-empty free list"));
706170295Sjeff		MPASS(ts->ts_lockobj != NULL);
707122514Sjhb		mtx_lock_spin(&td_contested_lock);
708154937Sjhb		TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
709122514Sjhb		turnstile_setowner(ts, owner);
710122514Sjhb		mtx_unlock_spin(&td_contested_lock);
711122514Sjhb	} else {
712154937Sjhb		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
713122514Sjhb			if (td1->td_priority > td->td_priority)
714122514Sjhb				break;
715122514Sjhb		mtx_lock_spin(&td_contested_lock);
716122514Sjhb		if (td1 != NULL)
717122514Sjhb			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
718122514Sjhb		else
719154937Sjhb			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
720154937Sjhb		MPASS(owner == ts->ts_owner);
721122514Sjhb		mtx_unlock_spin(&td_contested_lock);
722122514Sjhb		MPASS(td->td_turnstile != NULL);
723122514Sjhb		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
72472200Sbmilekic	}
725170295Sjeff	thread_lock(td);
726170295Sjeff	thread_lock_set(td, &ts->ts_lock);
727122514Sjhb	td->td_turnstile = NULL;
72872200Sbmilekic
729122514Sjhb	/* Save who we are blocked on and switch. */
730170295Sjeff	lock = ts->ts_lockobj;
731154937Sjhb	td->td_tsqueue = queue;
732122514Sjhb	td->td_blocked = ts;
733122514Sjhb	td->td_lockname = lock->lo_name;
734201879Sattilio	td->td_blktick = ticks;
735122514Sjhb	TD_SET_LOCK(td);
736170295Sjeff	mtx_unlock_spin(&tc->tc_lock);
737122514Sjhb	propagate_priority(td);
73872200Sbmilekic
739122514Sjhb	if (LOCK_LOG_TEST(lock, 0))
740139453Sjhb		CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
741139453Sjhb		    td->td_tid, lock, lock->lo_name);
74272200Sbmilekic
743176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
744178272Sjeff	mi_switch(SW_VOL | SWT_TURNSTILE, NULL);
74572200Sbmilekic
746122514Sjhb	if (LOCK_LOG_TEST(lock, 0))
747139453Sjhb		CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
748139453Sjhb		    __func__, td->td_tid, lock, lock->lo_name);
749170295Sjeff	thread_unlock(td);
75067352Sjhb}
75167352Sjhb
75272200Sbmilekic/*
753122514Sjhb * Pick the highest priority thread on this turnstile and put it on the
754122514Sjhb * pending list.  This must be called with the turnstile chain locked.
75572200Sbmilekic */
756122514Sjhbint
757154937Sjhbturnstile_signal(struct turnstile *ts, int queue)
75871352Sjasone{
759122514Sjhb	struct turnstile_chain *tc;
760122514Sjhb	struct thread *td;
761122514Sjhb	int empty;
76280748Sjhb
763122514Sjhb	MPASS(ts != NULL);
764170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
765122514Sjhb	MPASS(curthread->td_proc->p_magic == P_MAGIC);
766176017Sjeff	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
767154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
76871352Sjasone
769122514Sjhb	/*
770122514Sjhb	 * Pick the highest priority thread blocked on this lock and
771122514Sjhb	 * move it to the pending list.
772122514Sjhb	 */
773154937Sjhb	td = TAILQ_FIRST(&ts->ts_blocked[queue]);
774122514Sjhb	MPASS(td->td_proc->p_magic == P_MAGIC);
775122514Sjhb	mtx_lock_spin(&td_contested_lock);
776154937Sjhb	TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
777122514Sjhb	mtx_unlock_spin(&td_contested_lock);
778122514Sjhb	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
77967352Sjhb
78082304Sbmilekic	/*
781122514Sjhb	 * If the turnstile is now empty, remove it from its chain and
782122514Sjhb	 * give it to the about-to-be-woken thread.  Otherwise take a
783122514Sjhb	 * turnstile from the free list and give it to the thread.
784105782Sdes	 */
785154937Sjhb	empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
786154937Sjhb	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
787131259Sjhb	if (empty) {
788170295Sjeff		tc = TC_LOOKUP(ts->ts_lockobj);
789170295Sjeff		mtx_assert(&tc->tc_lock, MA_OWNED);
790122514Sjhb		MPASS(LIST_EMPTY(&ts->ts_free));
791131259Sjhb#ifdef TURNSTILE_PROFILING
792131259Sjhb		tc->tc_depth--;
793131259Sjhb#endif
794131259Sjhb	} else
795122514Sjhb		ts = LIST_FIRST(&ts->ts_free);
796123363Sjhb	MPASS(ts != NULL);
797122514Sjhb	LIST_REMOVE(ts, ts_hash);
798122514Sjhb	td->td_turnstile = ts;
799122514Sjhb
800122514Sjhb	return (empty);
80167352Sjhb}
802122514Sjhb
80372200Sbmilekic/*
804122514Sjhb * Put all blocked threads on the pending list.  This must be called with
805122514Sjhb * the turnstile chain locked.
80693672Sarr */
80793672Sarrvoid
808154937Sjhbturnstile_broadcast(struct turnstile *ts, int queue)
80993672Sarr{
810122514Sjhb	struct turnstile_chain *tc;
811122514Sjhb	struct turnstile *ts1;
812122514Sjhb	struct thread *td;
81393672Sarr
814122514Sjhb	MPASS(ts != NULL);
815170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
816122514Sjhb	MPASS(curthread->td_proc->p_magic == P_MAGIC);
817176017Sjeff	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
818170295Sjeff	/*
819170295Sjeff	 * We must have the chain locked so that we can remove the empty
820170295Sjeff	 * turnstile from the hash queue.
821170295Sjeff	 */
822122514Sjhb	tc = TC_LOOKUP(ts->ts_lockobj);
823122514Sjhb	mtx_assert(&tc->tc_lock, MA_OWNED);
824154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
825122514Sjhb
826122514Sjhb	/*
827122514Sjhb	 * Transfer the blocked list to the pending list.
828122514Sjhb	 */
829122514Sjhb	mtx_lock_spin(&td_contested_lock);
830154937Sjhb	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
831122514Sjhb	mtx_unlock_spin(&td_contested_lock);
832122514Sjhb
833122514Sjhb	/*
834122514Sjhb	 * Give a turnstile to each thread.  The last thread gets
835154937Sjhb	 * this turnstile if the turnstile is empty.
836122514Sjhb	 */
837122514Sjhb	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
838122514Sjhb		if (LIST_EMPTY(&ts->ts_free)) {
839122514Sjhb			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
840122514Sjhb			ts1 = ts;
841131259Sjhb#ifdef TURNSTILE_PROFILING
842131259Sjhb			tc->tc_depth--;
843131259Sjhb#endif
844122514Sjhb		} else
845122514Sjhb			ts1 = LIST_FIRST(&ts->ts_free);
846123363Sjhb		MPASS(ts1 != NULL);
847122514Sjhb		LIST_REMOVE(ts1, ts_hash);
848122514Sjhb		td->td_turnstile = ts1;
849122514Sjhb	}
85093672Sarr}
851122590Sjhb
85293672Sarr/*
853122514Sjhb * Wakeup all threads on the pending list and adjust the priority of the
854122514Sjhb * current thread appropriately.  This must be called with the turnstile
855122514Sjhb * chain locked.
856105782Sdes */
85767352Sjhbvoid
858154937Sjhbturnstile_unpend(struct turnstile *ts, int owner_type)
85967352Sjhb{
860122514Sjhb	TAILQ_HEAD( ,thread) pending_threads;
861170295Sjeff	struct turnstile *nts;
862122514Sjhb	struct thread *td;
863139453Sjhb	u_char cp, pri;
86472200Sbmilekic
865122514Sjhb	MPASS(ts != NULL);
866170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
867176017Sjeff	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
868122514Sjhb	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
86972200Sbmilekic
870122514Sjhb	/*
871122514Sjhb	 * Move the list of pending threads out of the turnstile and
872122514Sjhb	 * into a local variable.
873122514Sjhb	 */
874122514Sjhb	TAILQ_INIT(&pending_threads);
875122514Sjhb	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
876122514Sjhb#ifdef INVARIANTS
877154937Sjhb	if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
878154937Sjhb	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
879122514Sjhb		ts->ts_lockobj = NULL;
88069429Sjhb#endif
881122514Sjhb	/*
882170295Sjeff	 * Adjust the priority of curthread based on other contested
883170295Sjeff	 * locks it owns.  Don't lower the priority below the base
884170295Sjeff	 * priority however.
885170295Sjeff	 */
886170295Sjeff	td = curthread;
887170295Sjeff	pri = PRI_MAX;
888170295Sjeff	thread_lock(td);
889170295Sjeff	mtx_lock_spin(&td_contested_lock);
890170295Sjeff	/*
891122514Sjhb	 * Remove the turnstile from this thread's list of contested locks
892122514Sjhb	 * since this thread doesn't own it anymore.  New threads will
893122514Sjhb	 * not be blocking on the turnstile until it is claimed by a new
894154937Sjhb	 * owner.  There might not be a current owner if this is a shared
895154937Sjhb	 * lock.
896122514Sjhb	 */
897154937Sjhb	if (ts->ts_owner != NULL) {
898154937Sjhb		ts->ts_owner = NULL;
899154937Sjhb		LIST_REMOVE(ts, ts_link);
900154937Sjhb	}
901170295Sjeff	LIST_FOREACH(nts, &td->td_contested, ts_link) {
902170295Sjeff		cp = turnstile_first_waiter(nts)->td_priority;
903122514Sjhb		if (cp < pri)
904122514Sjhb			pri = cp;
905122514Sjhb	}
906122514Sjhb	mtx_unlock_spin(&td_contested_lock);
907139453Sjhb	sched_unlend_prio(td, pri);
908170295Sjeff	thread_unlock(td);
909122514Sjhb	/*
910122514Sjhb	 * Wake up all the pending threads.  If a thread is not blocked
911122514Sjhb	 * on a lock, then it is currently executing on another CPU in
912123364Sjhb	 * turnstile_wait() or sitting on a run queue waiting to resume
913123364Sjhb	 * in turnstile_wait().  Set a flag to force it to try to acquire
914122514Sjhb	 * the lock again instead of blocking.
915122514Sjhb	 */
916122514Sjhb	while (!TAILQ_EMPTY(&pending_threads)) {
917122514Sjhb		td = TAILQ_FIRST(&pending_threads);
918122514Sjhb		TAILQ_REMOVE(&pending_threads, td, td_lockq);
919170295Sjeff		thread_lock(td);
920176078Sjeff		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
921122514Sjhb		MPASS(td->td_proc->p_magic == P_MAGIC);
922170295Sjeff		MPASS(TD_ON_LOCK(td));
923170295Sjeff		TD_CLR_LOCK(td);
924170295Sjeff		MPASS(TD_CAN_RUN(td));
925170295Sjeff		td->td_blocked = NULL;
926170295Sjeff		td->td_lockname = NULL;
927201879Sattilio		td->td_blktick = 0;
928154937Sjhb#ifdef INVARIANTS
929170295Sjeff		td->td_tsqueue = 0xff;
930154937Sjhb#endif
931170295Sjeff		sched_add(td, SRQ_BORING);
932170295Sjeff		thread_unlock(td);
933122514Sjhb	}
934170295Sjeff	mtx_unlock_spin(&ts->ts_lock);
93567352Sjhb}
93667352Sjhb
93772200Sbmilekic/*
938157844Sjhb * Give up ownership of a turnstile.  This must be called with the
939157844Sjhb * turnstile chain locked.
940157844Sjhb */
941157844Sjhbvoid
942157844Sjhbturnstile_disown(struct turnstile *ts)
943157844Sjhb{
944157844Sjhb	struct thread *td;
945157844Sjhb	u_char cp, pri;
946157844Sjhb
947157844Sjhb	MPASS(ts != NULL);
948170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
949157844Sjhb	MPASS(ts->ts_owner == curthread);
950157844Sjhb	MPASS(TAILQ_EMPTY(&ts->ts_pending));
951157844Sjhb	MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
952157844Sjhb	    !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
953157844Sjhb
954157844Sjhb	/*
955157844Sjhb	 * Remove the turnstile from this thread's list of contested locks
956157844Sjhb	 * since this thread doesn't own it anymore.  New threads will
957157844Sjhb	 * not be blocking on the turnstile until it is claimed by a new
958157844Sjhb	 * owner.
959157844Sjhb	 */
960157844Sjhb	mtx_lock_spin(&td_contested_lock);
961157844Sjhb	ts->ts_owner = NULL;
962157844Sjhb	LIST_REMOVE(ts, ts_link);
963157844Sjhb	mtx_unlock_spin(&td_contested_lock);
964157844Sjhb
965157844Sjhb	/*
966157844Sjhb	 * Adjust the priority of curthread based on other contested
967157844Sjhb	 * locks it owns.  Don't lower the priority below the base
968157844Sjhb	 * priority however.
969157844Sjhb	 */
970157844Sjhb	td = curthread;
971157844Sjhb	pri = PRI_MAX;
972170295Sjeff	thread_lock(td);
973170295Sjeff	mtx_unlock_spin(&ts->ts_lock);
974157844Sjhb	mtx_lock_spin(&td_contested_lock);
975157844Sjhb	LIST_FOREACH(ts, &td->td_contested, ts_link) {
976157844Sjhb		cp = turnstile_first_waiter(ts)->td_priority;
977157844Sjhb		if (cp < pri)
978157844Sjhb			pri = cp;
979157844Sjhb	}
980157844Sjhb	mtx_unlock_spin(&td_contested_lock);
981157844Sjhb	sched_unlend_prio(td, pri);
982170295Sjeff	thread_unlock(td);
983157844Sjhb}
984157844Sjhb
985157844Sjhb/*
986122514Sjhb * Return the first thread in a turnstile.
98772200Sbmilekic */
988122514Sjhbstruct thread *
989154937Sjhbturnstile_head(struct turnstile *ts, int queue)
99067352Sjhb{
991122514Sjhb#ifdef INVARIANTS
99267352Sjhb
993122514Sjhb	MPASS(ts != NULL);
994154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
995170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
996122514Sjhb#endif
997154937Sjhb	return (TAILQ_FIRST(&ts->ts_blocked[queue]));
99871320Sjasone}
999154937Sjhb
1000157844Sjhb/*
1001157844Sjhb * Returns true if a sub-queue of a turnstile is empty.
1002157844Sjhb */
1003157844Sjhbint
1004157844Sjhbturnstile_empty(struct turnstile *ts, int queue)
1005157844Sjhb{
1006157844Sjhb#ifdef INVARIANTS
1007157844Sjhb
1008157844Sjhb	MPASS(ts != NULL);
1009157844Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1010170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
1011157844Sjhb#endif
1012157844Sjhb	return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
1013157844Sjhb}
1014157844Sjhb
1015154937Sjhb#ifdef DDB
1016154937Sjhbstatic void
1017154937Sjhbprint_thread(struct thread *td, const char *prefix)
1018154937Sjhb{
1019154937Sjhb
1020154937Sjhb	db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid,
1021157952Sjhb	    td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
1022173600Sjulian	    td->td_name);
1023154937Sjhb}
1024154937Sjhb
1025154937Sjhbstatic void
1026154937Sjhbprint_queue(struct threadqueue *queue, const char *header, const char *prefix)
1027154937Sjhb{
1028154937Sjhb	struct thread *td;
1029154937Sjhb
1030154937Sjhb	db_printf("%s:\n", header);
1031154937Sjhb	if (TAILQ_EMPTY(queue)) {
1032154937Sjhb		db_printf("%sempty\n", prefix);
1033154937Sjhb		return;
1034154937Sjhb	}
1035154937Sjhb	TAILQ_FOREACH(td, queue, td_lockq) {
1036154937Sjhb		print_thread(td, prefix);
1037154937Sjhb	}
1038154937Sjhb}
1039154937Sjhb
1040154937SjhbDB_SHOW_COMMAND(turnstile, db_show_turnstile)
1041154937Sjhb{
1042154937Sjhb	struct turnstile_chain *tc;
1043154937Sjhb	struct turnstile *ts;
1044154937Sjhb	struct lock_object *lock;
1045154937Sjhb	int i;
1046154937Sjhb
1047154937Sjhb	if (!have_addr)
1048154937Sjhb		return;
1049154937Sjhb
1050154937Sjhb	/*
1051154937Sjhb	 * First, see if there is an active turnstile for the lock indicated
1052154937Sjhb	 * by the address.
1053154937Sjhb	 */
1054154937Sjhb	lock = (struct lock_object *)addr;
1055154937Sjhb	tc = TC_LOOKUP(lock);
1056154937Sjhb	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1057154937Sjhb		if (ts->ts_lockobj == lock)
1058154937Sjhb			goto found;
1059154937Sjhb
1060154937Sjhb	/*
1061154937Sjhb	 * Second, see if there is an active turnstile at the address
1062154937Sjhb	 * indicated.
1063154937Sjhb	 */
1064154937Sjhb	for (i = 0; i < TC_TABLESIZE; i++)
1065154937Sjhb		LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) {
1066154937Sjhb			if (ts == (struct turnstile *)addr)
1067154937Sjhb				goto found;
1068154937Sjhb		}
1069154937Sjhb
1070154937Sjhb	db_printf("Unable to locate a turnstile via %p\n", (void *)addr);
1071154937Sjhb	return;
1072154937Sjhbfound:
1073154937Sjhb	lock = ts->ts_lockobj;
1074154937Sjhb	db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name,
1075154937Sjhb	    lock->lo_name);
1076154937Sjhb	if (ts->ts_owner)
1077154937Sjhb		print_thread(ts->ts_owner, "Lock Owner: ");
1078154937Sjhb	else
1079154937Sjhb		db_printf("Lock Owner: none\n");
1080154937Sjhb	print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t");
1081154937Sjhb	print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters",
1082154937Sjhb	    "\t");
1083154937Sjhb	print_queue(&ts->ts_pending, "Pending Threads", "\t");
1084154937Sjhb
1085154937Sjhb}
1086158031Sjhb
1087161324Sjhb/*
1088161324Sjhb * Show all the threads a particular thread is waiting on based on
1089161324Sjhb * non-sleepable and non-spin locks.
1090161324Sjhb */
1091158031Sjhbstatic void
1092161324Sjhbprint_lockchain(struct thread *td, const char *prefix)
1093158031Sjhb{
1094158031Sjhb	struct lock_object *lock;
1095158031Sjhb	struct lock_class *class;
1096158031Sjhb	struct turnstile *ts;
1097158031Sjhb
1098158031Sjhb	/*
1099158031Sjhb	 * Follow the chain.  We keep walking as long as the thread is
1100158031Sjhb	 * blocked on a turnstile that has an owner.
1101158031Sjhb	 */
1102160313Sjhb	while (!db_pager_quit) {
1103158031Sjhb		db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1104158031Sjhb		    td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
1105173600Sjulian		    td->td_name);
1106158031Sjhb		switch (td->td_state) {
1107158031Sjhb		case TDS_INACTIVE:
1108158031Sjhb			db_printf("is inactive\n");
1109158031Sjhb			return;
1110158031Sjhb		case TDS_CAN_RUN:
1111158031Sjhb			db_printf("can run\n");
1112158031Sjhb			return;
1113158031Sjhb		case TDS_RUNQ:
1114158031Sjhb			db_printf("is on a run queue\n");
1115158031Sjhb			return;
1116158031Sjhb		case TDS_RUNNING:
1117158031Sjhb			db_printf("running on CPU %d\n", td->td_oncpu);
1118158031Sjhb			return;
1119158031Sjhb		case TDS_INHIBITED:
1120158031Sjhb			if (TD_ON_LOCK(td)) {
1121158031Sjhb				ts = td->td_blocked;
1122158031Sjhb				lock = ts->ts_lockobj;
1123158031Sjhb				class = LOCK_CLASS(lock);
1124158031Sjhb				db_printf("blocked on lock %p (%s) \"%s\"\n",
1125158031Sjhb				    lock, class->lc_name, lock->lo_name);
1126158031Sjhb				if (ts->ts_owner == NULL)
1127158031Sjhb					return;
1128158031Sjhb				td = ts->ts_owner;
1129158031Sjhb				break;
1130158031Sjhb			}
1131158031Sjhb			db_printf("inhibited\n");
1132158031Sjhb			return;
1133158031Sjhb		default:
1134158031Sjhb			db_printf("??? (%#x)\n", td->td_state);
1135158031Sjhb			return;
1136158031Sjhb		}
1137158031Sjhb	}
1138158031Sjhb}
1139158031Sjhb
1140161324SjhbDB_SHOW_COMMAND(lockchain, db_show_lockchain)
1141158031Sjhb{
1142158031Sjhb	struct thread *td;
1143158031Sjhb
1144158031Sjhb	/* Figure out which thread to start with. */
1145158031Sjhb	if (have_addr)
1146158031Sjhb		td = db_lookup_thread(addr, TRUE);
1147158031Sjhb	else
1148158031Sjhb		td = kdb_thread;
1149158031Sjhb
1150161324Sjhb	print_lockchain(td, "");
1151158031Sjhb}
1152158031Sjhb
1153183054SsamDB_SHOW_ALL_COMMAND(chains, db_show_allchains)
1154158031Sjhb{
1155158031Sjhb	struct thread *td;
1156158031Sjhb	struct proc *p;
1157158031Sjhb	int i;
1158158031Sjhb
1159158031Sjhb	i = 1;
1160166073Sdelphij	FOREACH_PROC_IN_SYSTEM(p) {
1161158031Sjhb		FOREACH_THREAD_IN_PROC(p, td) {
1162158031Sjhb			if (TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested)) {
1163158031Sjhb				db_printf("chain %d:\n", i++);
1164161324Sjhb				print_lockchain(td, " ");
1165158031Sjhb			}
1166160313Sjhb			if (db_pager_quit)
1167160313Sjhb				return;
1168158031Sjhb		}
1169158031Sjhb	}
1170158031Sjhb}
1171183054SsamDB_SHOW_ALIAS(allchains, db_show_allchains)
1172158031Sjhb
1173161337Sjhb/*
1174161337Sjhb * Show all the threads a particular thread is waiting on based on
1175161337Sjhb * sleepable locks.
1176161337Sjhb */
1177161337Sjhbstatic void
1178161337Sjhbprint_sleepchain(struct thread *td, const char *prefix)
1179161337Sjhb{
1180161337Sjhb	struct thread *owner;
1181161337Sjhb
1182161337Sjhb	/*
1183161337Sjhb	 * Follow the chain.  We keep walking as long as the thread is
1184161337Sjhb	 * blocked on a sleep lock that has an owner.
1185161337Sjhb	 */
1186161337Sjhb	while (!db_pager_quit) {
1187161337Sjhb		db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1188161337Sjhb		    td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
1189173600Sjulian		    td->td_name);
1190161337Sjhb		switch (td->td_state) {
1191161337Sjhb		case TDS_INACTIVE:
1192161337Sjhb			db_printf("is inactive\n");
1193161337Sjhb			return;
1194161337Sjhb		case TDS_CAN_RUN:
1195161337Sjhb			db_printf("can run\n");
1196161337Sjhb			return;
1197161337Sjhb		case TDS_RUNQ:
1198161337Sjhb			db_printf("is on a run queue\n");
1199161337Sjhb			return;
1200161337Sjhb		case TDS_RUNNING:
1201161337Sjhb			db_printf("running on CPU %d\n", td->td_oncpu);
1202161337Sjhb			return;
1203161337Sjhb		case TDS_INHIBITED:
1204161337Sjhb			if (TD_ON_SLEEPQ(td)) {
1205161337Sjhb				if (lockmgr_chain(td, &owner) ||
1206161337Sjhb				    sx_chain(td, &owner)) {
1207161337Sjhb					if (owner == NULL)
1208161337Sjhb						return;
1209161337Sjhb					td = owner;
1210161337Sjhb					break;
1211161337Sjhb				}
1212161337Sjhb				db_printf("sleeping on %p \"%s\"\n",
1213161337Sjhb				    td->td_wchan, td->td_wmesg);
1214161337Sjhb				return;
1215161337Sjhb			}
1216161337Sjhb			db_printf("inhibited\n");
1217161337Sjhb			return;
1218161337Sjhb		default:
1219161337Sjhb			db_printf("??? (%#x)\n", td->td_state);
1220161337Sjhb			return;
1221161337Sjhb		}
1222161337Sjhb	}
1223161337Sjhb}
1224161337Sjhb
1225161337SjhbDB_SHOW_COMMAND(sleepchain, db_show_sleepchain)
1226161337Sjhb{
1227161337Sjhb	struct thread *td;
1228161337Sjhb
1229161337Sjhb	/* Figure out which thread to start with. */
1230161337Sjhb	if (have_addr)
1231161337Sjhb		td = db_lookup_thread(addr, TRUE);
1232161337Sjhb	else
1233161337Sjhb		td = kdb_thread;
1234161337Sjhb
1235161337Sjhb	print_sleepchain(td, "");
1236161337Sjhb}
1237161337Sjhb
1238158031Sjhbstatic void	print_waiters(struct turnstile *ts, int indent);
1239158031Sjhb
1240158031Sjhbstatic void
1241158031Sjhbprint_waiter(struct thread *td, int indent)
1242158031Sjhb{
1243158031Sjhb	struct turnstile *ts;
1244158031Sjhb	int i;
1245158031Sjhb
1246160313Sjhb	if (db_pager_quit)
1247160313Sjhb		return;
1248158031Sjhb	for (i = 0; i < indent; i++)
1249158031Sjhb		db_printf(" ");
1250158031Sjhb	print_thread(td, "thread ");
1251158031Sjhb	LIST_FOREACH(ts, &td->td_contested, ts_link)
1252158031Sjhb		print_waiters(ts, indent + 1);
1253158031Sjhb}
1254158031Sjhb
1255158031Sjhbstatic void
1256158031Sjhbprint_waiters(struct turnstile *ts, int indent)
1257158031Sjhb{
1258158031Sjhb	struct lock_object *lock;
1259158031Sjhb	struct lock_class *class;
1260158031Sjhb	struct thread *td;
1261158031Sjhb	int i;
1262158031Sjhb
1263160313Sjhb	if (db_pager_quit)
1264160313Sjhb		return;
1265158031Sjhb	lock = ts->ts_lockobj;
1266158031Sjhb	class = LOCK_CLASS(lock);
1267158031Sjhb	for (i = 0; i < indent; i++)
1268158031Sjhb		db_printf(" ");
1269158031Sjhb	db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
1270158031Sjhb	TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
1271158031Sjhb		print_waiter(td, indent + 1);
1272158031Sjhb	TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
1273158031Sjhb		print_waiter(td, indent + 1);
1274158031Sjhb	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
1275158031Sjhb		print_waiter(td, indent + 1);
1276158031Sjhb}
1277158031Sjhb
1278161324SjhbDB_SHOW_COMMAND(locktree, db_show_locktree)
1279158031Sjhb{
1280158031Sjhb	struct lock_object *lock;
1281158031Sjhb	struct lock_class *class;
1282158031Sjhb	struct turnstile_chain *tc;
1283158031Sjhb	struct turnstile *ts;
1284158031Sjhb
1285158031Sjhb	if (!have_addr)
1286158031Sjhb		return;
1287158031Sjhb	lock = (struct lock_object *)addr;
1288158031Sjhb	tc = TC_LOOKUP(lock);
1289158031Sjhb	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1290158031Sjhb		if (ts->ts_lockobj == lock)
1291158031Sjhb			break;
1292158031Sjhb	if (ts == NULL) {
1293158031Sjhb		class = LOCK_CLASS(lock);
1294158031Sjhb		db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
1295158031Sjhb		    lock->lo_name);
1296158031Sjhb	} else
1297158031Sjhb		print_waiters(ts, 0);
1298158031Sjhb}
1299154937Sjhb#endif
1300