subr_turnstile.c revision 176078
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 176078 2008-02-07 06:55:38Z jeff $");
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;
141131259SjhbSYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0, "turnstile profiling");
142131259SjhbSYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
143131259Sjhb    "turnstile chain stats");
144131259SjhbSYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
145131259Sjhb    &turnstile_max_depth, 0, "maxmimum depth achieved of a single chain");
146131259Sjhb#endif
147122514Sjhbstatic struct mtx td_contested_lock;
148122514Sjhbstatic struct turnstile_chain turnstile_chains[TC_TABLESIZE];
149169666Sjeffstatic uma_zone_t turnstile_zone;
15093702Sjhb
15193702Sjhb/*
15272200Sbmilekic * Prototypes for non-exported routines.
15372200Sbmilekic */
154122514Sjhbstatic void	init_turnstile0(void *dummy);
155131263Sjhb#ifdef TURNSTILE_PROFILING
156131263Sjhbstatic void	init_turnstile_profiling(void *arg);
157131263Sjhb#endif
158139453Sjhbstatic void	propagate_priority(struct thread *td);
159139453Sjhbstatic int	turnstile_adjust_thread(struct turnstile *ts,
160139453Sjhb		    struct thread *td);
161154937Sjhbstatic struct thread *turnstile_first_waiter(struct turnstile *ts);
162122514Sjhbstatic void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
163169666Sjeff#ifdef INVARIANTS
164169666Sjeffstatic void	turnstile_dtor(void *mem, int size, void *arg);
165169666Sjeff#endif
166169666Sjeffstatic int	turnstile_init(void *mem, int size, int flags);
167170295Sjeffstatic void	turnstile_fini(void *mem, int size);
16867352Sjhb
169122514Sjhb/*
170122514Sjhb * Walks the chain of turnstiles and their owners to propagate the priority
171122514Sjhb * of the thread being blocked to all the threads holding locks that have to
172122514Sjhb * release their locks before this thread can run again.
173122514Sjhb */
17467352Sjhbstatic void
17583366Sjulianpropagate_priority(struct thread *td)
17667352Sjhb{
177122514Sjhb	struct turnstile *ts;
178122514Sjhb	int pri;
17967352Sjhb
180170295Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
181122514Sjhb	pri = td->td_priority;
182122514Sjhb	ts = td->td_blocked;
183176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
184170295Sjeff	/*
185170295Sjeff	 * Grab a recursive lock on this turnstile chain so it stays locked
186170295Sjeff	 * for the whole operation.  The caller expects us to return with
187170295Sjeff	 * the original lock held.  We only ever lock down the chain so
188170295Sjeff	 * the lock order is constant.
189170295Sjeff	 */
190170295Sjeff	mtx_lock_spin(&ts->ts_lock);
19167352Sjhb	for (;;) {
192122514Sjhb		td = ts->ts_owner;
19367352Sjhb
19483366Sjulian		if (td == NULL) {
19567352Sjhb			/*
196154937Sjhb			 * This might be a read lock with no owner.  There's
197154937Sjhb			 * not much we can do, so just bail.
19867352Sjhb			 */
199170295Sjeff			mtx_unlock_spin(&ts->ts_lock);
20067352Sjhb			return;
20167352Sjhb		}
20272200Sbmilekic
203170295Sjeff		thread_lock_flags(td, MTX_DUPOK);
204170295Sjeff		mtx_unlock_spin(&ts->ts_lock);
20599072Sjulian		MPASS(td->td_proc != NULL);
20683366Sjulian		MPASS(td->td_proc->p_magic == P_MAGIC);
207122514Sjhb
208122514Sjhb		/*
209157275Sjhb		 * If the thread is asleep, then we are probably about
210157275Sjhb		 * to deadlock.  To make debugging this easier, just
211157275Sjhb		 * panic and tell the user which thread misbehaved so
212157275Sjhb		 * they can hopefully get a stack trace from the truly
213157275Sjhb		 * misbehaving thread.
214122514Sjhb		 */
215157275Sjhb		if (TD_IS_SLEEPING(td)) {
216157275Sjhb			printf(
217157275Sjhb		"Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
218157275Sjhb			    td->td_tid, td->td_proc->p_pid);
219157275Sjhb#ifdef DDB
220157275Sjhb			db_trace_thread(td, -1);
221157275Sjhb#endif
222157275Sjhb			panic("sleeping thread");
223157275Sjhb		}
224122514Sjhb
225122514Sjhb		/*
226122514Sjhb		 * If this thread already has higher priority than the
227122514Sjhb		 * thread that is being blocked, we are finished.
228122514Sjhb		 */
229170295Sjeff		if (td->td_priority <= pri) {
230170295Sjeff			thread_unlock(td);
23167352Sjhb			return;
232170295Sjeff		}
23369376Sjhb
23469376Sjhb		/*
235139453Sjhb		 * Bump this thread's priority.
23667352Sjhb		 */
237139453Sjhb		sched_lend_prio(td, pri);
238139453Sjhb
239139453Sjhb		/*
240139453Sjhb		 * If lock holder is actually running or on the run queue
241139453Sjhb		 * then we are done.
242139453Sjhb		 */
243139453Sjhb		if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
244139453Sjhb			MPASS(td->td_blocked == NULL);
245170295Sjeff			thread_unlock(td);
24667352Sjhb			return;
24767352Sjhb		}
24872376Sjake
24973912Sjhb#ifndef SMP
25067352Sjhb		/*
25183366Sjulian		 * For UP, we check to see if td is curthread (this shouldn't
25273912Sjhb		 * ever happen however as it would mean we are in a deadlock.)
25373912Sjhb		 */
25483366Sjulian		KASSERT(td != curthread, ("Deadlock detected"));
25573912Sjhb#endif
25673912Sjhb
25773912Sjhb		/*
258122514Sjhb		 * If we aren't blocked on a lock, we should be.
25967352Sjhb		 */
260104387Sjhb		KASSERT(TD_ON_LOCK(td), (
261139453Sjhb		    "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
262173600Sjulian		    td->td_tid, td->td_name, td->td_state,
263122514Sjhb		    ts->ts_lockobj->lo_name));
26467352Sjhb
26567352Sjhb		/*
266122514Sjhb		 * Pick up the lock that td is blocked on.
26767352Sjhb		 */
268122514Sjhb		ts = td->td_blocked;
269122514Sjhb		MPASS(ts != NULL);
270176078Sjeff		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
271139453Sjhb		/* Resort td on the list if needed. */
272139453Sjhb		if (!turnstile_adjust_thread(ts, td)) {
273170295Sjeff			mtx_unlock_spin(&ts->ts_lock);
274122590Sjhb			return;
275122590Sjhb		}
276170295Sjeff		/* The thread lock is released as ts lock above. */
277139453Sjhb	}
278139453Sjhb}
279122590Sjhb
280139453Sjhb/*
281139453Sjhb * Adjust the thread's position on a turnstile after its priority has been
282139453Sjhb * changed.
283139453Sjhb */
284139453Sjhbstatic int
285139453Sjhbturnstile_adjust_thread(struct turnstile *ts, struct thread *td)
286139453Sjhb{
287139453Sjhb	struct thread *td1, *td2;
288154937Sjhb	int queue;
28972200Sbmilekic
290170295Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
291139453Sjhb	MPASS(TD_ON_LOCK(td));
29267352Sjhb
293139453Sjhb	/*
294139453Sjhb	 * This thread may not be blocked on this turnstile anymore
295139453Sjhb	 * but instead might already be woken up on another CPU
296170295Sjeff	 * that is waiting on the thread lock in turnstile_unpend() to
297139453Sjhb	 * finish waking this thread up.  We can detect this case
298139453Sjhb	 * by checking to see if this thread has been given a
299139453Sjhb	 * turnstile by either turnstile_signal() or
300139453Sjhb	 * turnstile_broadcast().  In this case, treat the thread as
301139453Sjhb	 * if it was already running.
302139453Sjhb	 */
303139453Sjhb	if (td->td_turnstile != NULL)
304139453Sjhb		return (0);
305139453Sjhb
306139453Sjhb	/*
307139453Sjhb	 * Check if the thread needs to be moved on the blocked chain.
308139453Sjhb	 * It needs to be moved if either its priority is lower than
309139453Sjhb	 * the previous thread or higher than the next thread.
310139453Sjhb	 */
311176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
312139453Sjhb	td1 = TAILQ_PREV(td, threadqueue, td_lockq);
313139453Sjhb	td2 = TAILQ_NEXT(td, td_lockq);
314139453Sjhb	if ((td1 != NULL && td->td_priority < td1->td_priority) ||
315139453Sjhb	    (td2 != NULL && td->td_priority > td2->td_priority)) {
316139453Sjhb
31767352Sjhb		/*
31883366Sjulian		 * Remove thread from blocked chain and determine where
319139453Sjhb		 * it should be moved to.
32067352Sjhb		 */
321154937Sjhb		queue = td->td_tsqueue;
322154937Sjhb		MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
323122514Sjhb		mtx_lock_spin(&td_contested_lock);
324154937Sjhb		TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
325154937Sjhb		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
32683366Sjulian			MPASS(td1->td_proc->p_magic == P_MAGIC);
327139453Sjhb			if (td1->td_priority > td->td_priority)
32867352Sjhb				break;
32967352Sjhb		}
33072200Sbmilekic
331139453Sjhb		if (td1 == NULL)
332154937Sjhb			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
333139453Sjhb		else
334139453Sjhb			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
335122514Sjhb		mtx_unlock_spin(&td_contested_lock);
336139453Sjhb		if (td1 == NULL)
337139453Sjhb			CTR3(KTR_LOCK,
338139453Sjhb		    "turnstile_adjust_thread: td %d put at tail on [%p] %s",
339139453Sjhb			    td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
340139453Sjhb		else
341139453Sjhb			CTR4(KTR_LOCK,
342139453Sjhb		    "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
343139453Sjhb			    td->td_tid, td1->td_tid, ts->ts_lockobj,
344139453Sjhb			    ts->ts_lockobj->lo_name);
34567352Sjhb	}
346139453Sjhb	return (1);
34767352Sjhb}
34867352Sjhb
34971352Sjasone/*
350122514Sjhb * Early initialization of turnstiles.  This is not done via a SYSINIT()
351122514Sjhb * since this needs to be initialized very early when mutexes are first
352122514Sjhb * initialized.
35393609Sdes */
354122514Sjhbvoid
355122514Sjhbinit_turnstiles(void)
35693667Sdes{
357122514Sjhb	int i;
35893667Sdes
359122514Sjhb	for (i = 0; i < TC_TABLESIZE; i++) {
360122514Sjhb		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
361122514Sjhb		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
362122514Sjhb		    NULL, MTX_SPIN);
363131263Sjhb	}
364131263Sjhb	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
365154482Sjhb	LIST_INIT(&thread0.td_contested);
366131263Sjhb	thread0.td_turnstile = NULL;
367131263Sjhb}
368131263Sjhb
369131259Sjhb#ifdef TURNSTILE_PROFILING
370131263Sjhbstatic void
371131263Sjhbinit_turnstile_profiling(void *arg)
372131263Sjhb{
373131263Sjhb	struct sysctl_oid *chain_oid;
374131263Sjhb	char chain_name[10];
375131263Sjhb	int i;
376131263Sjhb
377131263Sjhb	for (i = 0; i < TC_TABLESIZE; i++) {
378131259Sjhb		snprintf(chain_name, sizeof(chain_name), "%d", i);
379131259Sjhb		chain_oid = SYSCTL_ADD_NODE(NULL,
380131259Sjhb		    SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
381131259Sjhb		    chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
382131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
383131259Sjhb		    "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
384131259Sjhb		    NULL);
385131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
386131259Sjhb		    "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
387131259Sjhb		    0, NULL);
388122514Sjhb	}
38993667Sdes}
390131263SjhbSYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
391131263Sjhb    init_turnstile_profiling, NULL);
392131263Sjhb#endif
39393667Sdes
394122514Sjhbstatic void
395122514Sjhbinit_turnstile0(void *dummy)
39693609Sdes{
39793609Sdes
398169666Sjeff	turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
399169666Sjeff#ifdef INVARIANTS
400170295Sjeff	    NULL, turnstile_dtor, turnstile_init, turnstile_fini,
401170295Sjeff	    UMA_ALIGN_CACHE, 0);
402169666Sjeff#else
403170295Sjeff	    NULL, NULL, turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, 0);
404169666Sjeff#endif
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);
687170295Sjeff	if (ts == td->td_turnstile) {
688170295Sjeff	mtx_assert(&tc->tc_lock, MA_OWNED);
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
697170295Sjeff		tc = TC_LOOKUP(ts->ts_lockobj);
698122514Sjhb		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
699122514Sjhb		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
700122514Sjhb		    ("thread's turnstile has pending threads"));
701154937Sjhb		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
702154937Sjhb		    ("thread's turnstile has exclusive waiters"));
703154937Sjhb		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
704154937Sjhb		    ("thread's turnstile has shared waiters"));
705122514Sjhb		KASSERT(LIST_EMPTY(&ts->ts_free),
706122514Sjhb		    ("thread's turnstile has a non-empty free list"));
707170295Sjeff		MPASS(ts->ts_lockobj != NULL);
708122514Sjhb		mtx_lock_spin(&td_contested_lock);
709154937Sjhb		TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
710122514Sjhb		turnstile_setowner(ts, owner);
711122514Sjhb		mtx_unlock_spin(&td_contested_lock);
712122514Sjhb	} else {
713154937Sjhb		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
714122514Sjhb			if (td1->td_priority > td->td_priority)
715122514Sjhb				break;
716122514Sjhb		mtx_lock_spin(&td_contested_lock);
717122514Sjhb		if (td1 != NULL)
718122514Sjhb			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
719122514Sjhb		else
720154937Sjhb			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
721154937Sjhb		MPASS(owner == ts->ts_owner);
722122514Sjhb		mtx_unlock_spin(&td_contested_lock);
723122514Sjhb		MPASS(td->td_turnstile != NULL);
724122514Sjhb		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
72572200Sbmilekic	}
726170295Sjeff	thread_lock(td);
727170295Sjeff	thread_lock_set(td, &ts->ts_lock);
728122514Sjhb	td->td_turnstile = NULL;
72972200Sbmilekic
730122514Sjhb	/* Save who we are blocked on and switch. */
731170295Sjeff	lock = ts->ts_lockobj;
732154937Sjhb	td->td_tsqueue = queue;
733122514Sjhb	td->td_blocked = ts;
734122514Sjhb	td->td_lockname = lock->lo_name;
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);
744170295Sjeff	SCHED_STAT_INC(switch_turnstile);
745131473Sjhb	mi_switch(SW_VOL, NULL);
74672200Sbmilekic
747122514Sjhb	if (LOCK_LOG_TEST(lock, 0))
748139453Sjhb		CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
749139453Sjhb		    __func__, td->td_tid, lock, lock->lo_name);
750170295Sjeff	thread_unlock(td);
75167352Sjhb}
75267352Sjhb
75372200Sbmilekic/*
754122514Sjhb * Pick the highest priority thread on this turnstile and put it on the
755122514Sjhb * pending list.  This must be called with the turnstile chain locked.
75672200Sbmilekic */
757122514Sjhbint
758154937Sjhbturnstile_signal(struct turnstile *ts, int queue)
75971352Sjasone{
760122514Sjhb	struct turnstile_chain *tc;
761122514Sjhb	struct thread *td;
762122514Sjhb	int empty;
76380748Sjhb
764122514Sjhb	MPASS(ts != NULL);
765170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
766122514Sjhb	MPASS(curthread->td_proc->p_magic == P_MAGIC);
767176017Sjeff	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
768154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
76971352Sjasone
770122514Sjhb	/*
771122514Sjhb	 * Pick the highest priority thread blocked on this lock and
772122514Sjhb	 * move it to the pending list.
773122514Sjhb	 */
774154937Sjhb	td = TAILQ_FIRST(&ts->ts_blocked[queue]);
775122514Sjhb	MPASS(td->td_proc->p_magic == P_MAGIC);
776122514Sjhb	mtx_lock_spin(&td_contested_lock);
777154937Sjhb	TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
778122514Sjhb	mtx_unlock_spin(&td_contested_lock);
779122514Sjhb	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
78067352Sjhb
78182304Sbmilekic	/*
782122514Sjhb	 * If the turnstile is now empty, remove it from its chain and
783122514Sjhb	 * give it to the about-to-be-woken thread.  Otherwise take a
784122514Sjhb	 * turnstile from the free list and give it to the thread.
785105782Sdes	 */
786154937Sjhb	empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
787154937Sjhb	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
788131259Sjhb	if (empty) {
789170295Sjeff		tc = TC_LOOKUP(ts->ts_lockobj);
790170295Sjeff		mtx_assert(&tc->tc_lock, MA_OWNED);
791122514Sjhb		MPASS(LIST_EMPTY(&ts->ts_free));
792131259Sjhb#ifdef TURNSTILE_PROFILING
793131259Sjhb		tc->tc_depth--;
794131259Sjhb#endif
795131259Sjhb	} else
796122514Sjhb		ts = LIST_FIRST(&ts->ts_free);
797123363Sjhb	MPASS(ts != NULL);
798122514Sjhb	LIST_REMOVE(ts, ts_hash);
799122514Sjhb	td->td_turnstile = ts;
800122514Sjhb
801122514Sjhb	return (empty);
80267352Sjhb}
803122514Sjhb
80472200Sbmilekic/*
805122514Sjhb * Put all blocked threads on the pending list.  This must be called with
806122514Sjhb * the turnstile chain locked.
80793672Sarr */
80893672Sarrvoid
809154937Sjhbturnstile_broadcast(struct turnstile *ts, int queue)
81093672Sarr{
811122514Sjhb	struct turnstile_chain *tc;
812122514Sjhb	struct turnstile *ts1;
813122514Sjhb	struct thread *td;
81493672Sarr
815122514Sjhb	MPASS(ts != NULL);
816170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
817122514Sjhb	MPASS(curthread->td_proc->p_magic == P_MAGIC);
818176017Sjeff	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
819170295Sjeff	/*
820170295Sjeff	 * We must have the chain locked so that we can remove the empty
821170295Sjeff	 * turnstile from the hash queue.
822170295Sjeff	 */
823122514Sjhb	tc = TC_LOOKUP(ts->ts_lockobj);
824122514Sjhb	mtx_assert(&tc->tc_lock, MA_OWNED);
825154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
826122514Sjhb
827122514Sjhb	/*
828122514Sjhb	 * Transfer the blocked list to the pending list.
829122514Sjhb	 */
830122514Sjhb	mtx_lock_spin(&td_contested_lock);
831154937Sjhb	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
832122514Sjhb	mtx_unlock_spin(&td_contested_lock);
833122514Sjhb
834122514Sjhb	/*
835122514Sjhb	 * Give a turnstile to each thread.  The last thread gets
836154937Sjhb	 * this turnstile if the turnstile is empty.
837122514Sjhb	 */
838122514Sjhb	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
839122514Sjhb		if (LIST_EMPTY(&ts->ts_free)) {
840122514Sjhb			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
841122514Sjhb			ts1 = ts;
842131259Sjhb#ifdef TURNSTILE_PROFILING
843131259Sjhb			tc->tc_depth--;
844131259Sjhb#endif
845122514Sjhb		} else
846122514Sjhb			ts1 = LIST_FIRST(&ts->ts_free);
847123363Sjhb		MPASS(ts1 != NULL);
848122514Sjhb		LIST_REMOVE(ts1, ts_hash);
849122514Sjhb		td->td_turnstile = ts1;
850122514Sjhb	}
85193672Sarr}
852122590Sjhb
85393672Sarr/*
854122514Sjhb * Wakeup all threads on the pending list and adjust the priority of the
855122514Sjhb * current thread appropriately.  This must be called with the turnstile
856122514Sjhb * chain locked.
857105782Sdes */
85867352Sjhbvoid
859154937Sjhbturnstile_unpend(struct turnstile *ts, int owner_type)
86067352Sjhb{
861122514Sjhb	TAILQ_HEAD( ,thread) pending_threads;
862170295Sjeff	struct turnstile *nts;
863122514Sjhb	struct thread *td;
864139453Sjhb	u_char cp, pri;
86572200Sbmilekic
866122514Sjhb	MPASS(ts != NULL);
867170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
868176017Sjeff	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
869122514Sjhb	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
87072200Sbmilekic
871122514Sjhb	/*
872122514Sjhb	 * Move the list of pending threads out of the turnstile and
873122514Sjhb	 * into a local variable.
874122514Sjhb	 */
875122514Sjhb	TAILQ_INIT(&pending_threads);
876122514Sjhb	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
877122514Sjhb#ifdef INVARIANTS
878154937Sjhb	if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
879154937Sjhb	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
880122514Sjhb		ts->ts_lockobj = NULL;
88169429Sjhb#endif
882122514Sjhb	/*
883170295Sjeff	 * Adjust the priority of curthread based on other contested
884170295Sjeff	 * locks it owns.  Don't lower the priority below the base
885170295Sjeff	 * priority however.
886170295Sjeff	 */
887170295Sjeff	td = curthread;
888170295Sjeff	pri = PRI_MAX;
889170295Sjeff	thread_lock(td);
890170295Sjeff	mtx_lock_spin(&td_contested_lock);
891170295Sjeff	/*
892122514Sjhb	 * Remove the turnstile from this thread's list of contested locks
893122514Sjhb	 * since this thread doesn't own it anymore.  New threads will
894122514Sjhb	 * not be blocking on the turnstile until it is claimed by a new
895154937Sjhb	 * owner.  There might not be a current owner if this is a shared
896154937Sjhb	 * lock.
897122514Sjhb	 */
898154937Sjhb	if (ts->ts_owner != NULL) {
899154937Sjhb		ts->ts_owner = NULL;
900154937Sjhb		LIST_REMOVE(ts, ts_link);
901154937Sjhb	}
902170295Sjeff	LIST_FOREACH(nts, &td->td_contested, ts_link) {
903170295Sjeff		cp = turnstile_first_waiter(nts)->td_priority;
904122514Sjhb		if (cp < pri)
905122514Sjhb			pri = cp;
906122514Sjhb	}
907122514Sjhb	mtx_unlock_spin(&td_contested_lock);
908139453Sjhb	sched_unlend_prio(td, pri);
909170295Sjeff	thread_unlock(td);
910122514Sjhb	/*
911122514Sjhb	 * Wake up all the pending threads.  If a thread is not blocked
912122514Sjhb	 * on a lock, then it is currently executing on another CPU in
913123364Sjhb	 * turnstile_wait() or sitting on a run queue waiting to resume
914123364Sjhb	 * in turnstile_wait().  Set a flag to force it to try to acquire
915122514Sjhb	 * the lock again instead of blocking.
916122514Sjhb	 */
917122514Sjhb	while (!TAILQ_EMPTY(&pending_threads)) {
918122514Sjhb		td = TAILQ_FIRST(&pending_threads);
919122514Sjhb		TAILQ_REMOVE(&pending_threads, td, td_lockq);
920170295Sjeff		thread_lock(td);
921176078Sjeff		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
922122514Sjhb		MPASS(td->td_proc->p_magic == P_MAGIC);
923170295Sjeff		MPASS(TD_ON_LOCK(td));
924170295Sjeff		TD_CLR_LOCK(td);
925170295Sjeff		MPASS(TD_CAN_RUN(td));
926170295Sjeff		td->td_blocked = NULL;
927170295Sjeff		td->td_lockname = NULL;
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
1153158031SjhbDB_SHOW_COMMAND(allchains, 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}
1171158031Sjhb
1172161337Sjhb/*
1173161337Sjhb * Show all the threads a particular thread is waiting on based on
1174161337Sjhb * sleepable locks.
1175161337Sjhb */
1176161337Sjhbstatic void
1177161337Sjhbprint_sleepchain(struct thread *td, const char *prefix)
1178161337Sjhb{
1179161337Sjhb	struct thread *owner;
1180161337Sjhb
1181161337Sjhb	/*
1182161337Sjhb	 * Follow the chain.  We keep walking as long as the thread is
1183161337Sjhb	 * blocked on a sleep lock that has an owner.
1184161337Sjhb	 */
1185161337Sjhb	while (!db_pager_quit) {
1186161337Sjhb		db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1187161337Sjhb		    td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name :
1188173600Sjulian		    td->td_name);
1189161337Sjhb		switch (td->td_state) {
1190161337Sjhb		case TDS_INACTIVE:
1191161337Sjhb			db_printf("is inactive\n");
1192161337Sjhb			return;
1193161337Sjhb		case TDS_CAN_RUN:
1194161337Sjhb			db_printf("can run\n");
1195161337Sjhb			return;
1196161337Sjhb		case TDS_RUNQ:
1197161337Sjhb			db_printf("is on a run queue\n");
1198161337Sjhb			return;
1199161337Sjhb		case TDS_RUNNING:
1200161337Sjhb			db_printf("running on CPU %d\n", td->td_oncpu);
1201161337Sjhb			return;
1202161337Sjhb		case TDS_INHIBITED:
1203161337Sjhb			if (TD_ON_SLEEPQ(td)) {
1204161337Sjhb				if (lockmgr_chain(td, &owner) ||
1205161337Sjhb				    sx_chain(td, &owner)) {
1206161337Sjhb					if (owner == NULL)
1207161337Sjhb						return;
1208161337Sjhb					td = owner;
1209161337Sjhb					break;
1210161337Sjhb				}
1211161337Sjhb				db_printf("sleeping on %p \"%s\"\n",
1212161337Sjhb				    td->td_wchan, td->td_wmesg);
1213161337Sjhb				return;
1214161337Sjhb			}
1215161337Sjhb			db_printf("inhibited\n");
1216161337Sjhb			return;
1217161337Sjhb		default:
1218161337Sjhb			db_printf("??? (%#x)\n", td->td_state);
1219161337Sjhb			return;
1220161337Sjhb		}
1221161337Sjhb	}
1222161337Sjhb}
1223161337Sjhb
1224161337SjhbDB_SHOW_COMMAND(sleepchain, db_show_sleepchain)
1225161337Sjhb{
1226161337Sjhb	struct thread *td;
1227161337Sjhb
1228161337Sjhb	/* Figure out which thread to start with. */
1229161337Sjhb	if (have_addr)
1230161337Sjhb		td = db_lookup_thread(addr, TRUE);
1231161337Sjhb	else
1232161337Sjhb		td = kdb_thread;
1233161337Sjhb
1234161337Sjhb	print_sleepchain(td, "");
1235161337Sjhb}
1236161337Sjhb
1237158031Sjhbstatic void	print_waiters(struct turnstile *ts, int indent);
1238158031Sjhb
1239158031Sjhbstatic void
1240158031Sjhbprint_waiter(struct thread *td, int indent)
1241158031Sjhb{
1242158031Sjhb	struct turnstile *ts;
1243158031Sjhb	int i;
1244158031Sjhb
1245160313Sjhb	if (db_pager_quit)
1246160313Sjhb		return;
1247158031Sjhb	for (i = 0; i < indent; i++)
1248158031Sjhb		db_printf(" ");
1249158031Sjhb	print_thread(td, "thread ");
1250158031Sjhb	LIST_FOREACH(ts, &td->td_contested, ts_link)
1251158031Sjhb		print_waiters(ts, indent + 1);
1252158031Sjhb}
1253158031Sjhb
1254158031Sjhbstatic void
1255158031Sjhbprint_waiters(struct turnstile *ts, int indent)
1256158031Sjhb{
1257158031Sjhb	struct lock_object *lock;
1258158031Sjhb	struct lock_class *class;
1259158031Sjhb	struct thread *td;
1260158031Sjhb	int i;
1261158031Sjhb
1262160313Sjhb	if (db_pager_quit)
1263160313Sjhb		return;
1264158031Sjhb	lock = ts->ts_lockobj;
1265158031Sjhb	class = LOCK_CLASS(lock);
1266158031Sjhb	for (i = 0; i < indent; i++)
1267158031Sjhb		db_printf(" ");
1268158031Sjhb	db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
1269158031Sjhb	TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
1270158031Sjhb		print_waiter(td, indent + 1);
1271158031Sjhb	TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
1272158031Sjhb		print_waiter(td, indent + 1);
1273158031Sjhb	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
1274158031Sjhb		print_waiter(td, indent + 1);
1275158031Sjhb}
1276158031Sjhb
1277161324SjhbDB_SHOW_COMMAND(locktree, db_show_locktree)
1278158031Sjhb{
1279158031Sjhb	struct lock_object *lock;
1280158031Sjhb	struct lock_class *class;
1281158031Sjhb	struct turnstile_chain *tc;
1282158031Sjhb	struct turnstile *ts;
1283158031Sjhb
1284158031Sjhb	if (!have_addr)
1285158031Sjhb		return;
1286158031Sjhb	lock = (struct lock_object *)addr;
1287158031Sjhb	tc = TC_LOOKUP(lock);
1288158031Sjhb	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1289158031Sjhb		if (ts->ts_lockobj == lock)
1290158031Sjhb			break;
1291158031Sjhb	if (ts == NULL) {
1292158031Sjhb		class = LOCK_CLASS(lock);
1293158031Sjhb		db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
1294158031Sjhb		    lock->lo_name);
1295158031Sjhb	} else
1296158031Sjhb		print_waiters(ts, 0);
1297158031Sjhb}
1298154937Sjhb#endif
1299