xref: /freebsd/sys/kern/subr_turnstile.c (revision f5c157d986f052962aae4063ea973a62af59c5d7)
10384fff8SJason Evans /*-
20384fff8SJason Evans  * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
30384fff8SJason Evans  *
40384fff8SJason Evans  * Redistribution and use in source and binary forms, with or without
50384fff8SJason Evans  * modification, are permitted provided that the following conditions
60384fff8SJason Evans  * are met:
70384fff8SJason Evans  * 1. Redistributions of source code must retain the above copyright
80384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer.
90384fff8SJason Evans  * 2. Redistributions in binary form must reproduce the above copyright
100384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer in the
110384fff8SJason Evans  *    documentation and/or other materials provided with the distribution.
120384fff8SJason Evans  * 3. Berkeley Software Design Inc's name may not be used to endorse or
130384fff8SJason Evans  *    promote products derived from this software without specific prior
140384fff8SJason Evans  *    written permission.
150384fff8SJason Evans  *
160384fff8SJason Evans  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
170384fff8SJason Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180384fff8SJason Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190384fff8SJason Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
200384fff8SJason Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210384fff8SJason Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220384fff8SJason Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230384fff8SJason Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240384fff8SJason Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250384fff8SJason Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260384fff8SJason Evans  * SUCH DAMAGE.
270384fff8SJason Evans  *
280384fff8SJason Evans  *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
2936412d79SJohn Baldwin  *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
300384fff8SJason Evans  */
310384fff8SJason Evans 
320384fff8SJason Evans /*
33961a7b24SJohn Baldwin  * Implementation of turnstiles used to hold queue of threads blocked on
34961a7b24SJohn Baldwin  * non-sleepable locks.  Sleepable locks use condition variables to
35961a7b24SJohn Baldwin  * implement their queues.  Turnstiles differ from a sleep queue in that
36961a7b24SJohn Baldwin  * turnstile queue's are assigned to a lock held by an owning thread.  Thus,
37961a7b24SJohn Baldwin  * when one thread is enqueued onto a turnstile, it can lend its priority
38961a7b24SJohn Baldwin  * to the owning thread.
39961a7b24SJohn Baldwin  *
40961a7b24SJohn Baldwin  * We wish to avoid bloating locks with an embedded turnstile and we do not
41961a7b24SJohn Baldwin  * want to use back-pointers in the locks for the same reason.  Thus, we
42961a7b24SJohn Baldwin  * use a similar approach to that of Solaris 7 as described in Solaris
43961a7b24SJohn Baldwin  * Internals by Jim Mauro and Richard McDougall.  Turnstiles are looked up
44961a7b24SJohn Baldwin  * in a hash table based on the address of the lock.  Each entry in the
45961a7b24SJohn Baldwin  * hash table is a linked-lists of turnstiles and is called a turnstile
46961a7b24SJohn Baldwin  * chain.  Each chain contains a spin mutex that protects all of the
47961a7b24SJohn Baldwin  * turnstiles in the chain.
48961a7b24SJohn Baldwin  *
49961a7b24SJohn Baldwin  * Each time a thread is created, a turnstile is malloc'd and attached to
50961a7b24SJohn Baldwin  * that thread.  When a thread blocks on a lock, if it is the first thread
51961a7b24SJohn Baldwin  * to block, it lends its turnstile to the lock.  If the lock already has
52961a7b24SJohn Baldwin  * a turnstile, then it gives its turnstile to the lock's turnstile's free
53861a7db5SJohn Baldwin  * list.  When a thread is woken up, it takes a turnstile from the free list
54961a7b24SJohn Baldwin  * if there are any other waiters.  If it is the only thread blocked on the
55961a7b24SJohn Baldwin  * lock, then it reclaims the turnstile associated with the lock and removes
56961a7b24SJohn Baldwin  * it from the hash table.
570384fff8SJason Evans  */
580384fff8SJason Evans 
59ef0ebfc3SJohn Baldwin #include "opt_turnstile_profiling.h"
60ef0ebfc3SJohn Baldwin 
61677b542eSDavid E. O'Brien #include <sys/cdefs.h>
62677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
63677b542eSDavid E. O'Brien 
640384fff8SJason Evans #include <sys/param.h>
656c35e809SDag-Erling Smørgrav #include <sys/systm.h>
6636412d79SJohn Baldwin #include <sys/kernel.h>
676c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
6819284646SJohn Baldwin #include <sys/lock.h>
69fb919e4dSMark Murray #include <sys/malloc.h>
7019284646SJohn Baldwin #include <sys/mutex.h>
710384fff8SJason Evans #include <sys/proc.h>
72961a7b24SJohn Baldwin #include <sys/queue.h>
73c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
74b43179fbSJeff Roberson #include <sys/sched.h>
75ef0ebfc3SJohn Baldwin #include <sys/sysctl.h>
76ef0ebfc3SJohn Baldwin #include <sys/turnstile.h>
7736412d79SJohn Baldwin 
780cde2e34SJason Evans /*
79961a7b24SJohn Baldwin  * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
80961a7b24SJohn Baldwin  * number chosen because the sleep queue's use the same value for the
81961a7b24SJohn Baldwin  * shift.  Basically, we ignore the lower 8 bits of the address.
82961a7b24SJohn Baldwin  * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
830cde2e34SJason Evans  */
84961a7b24SJohn Baldwin #define	TC_TABLESIZE	128			/* Must be power of 2. */
85961a7b24SJohn Baldwin #define	TC_MASK		(TC_TABLESIZE - 1)
86961a7b24SJohn Baldwin #define	TC_SHIFT	8
87961a7b24SJohn Baldwin #define	TC_HASH(lock)	(((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
88961a7b24SJohn Baldwin #define	TC_LOOKUP(lock)	&turnstile_chains[TC_HASH(lock)]
899ed346baSBosko Milekic 
900cde2e34SJason Evans /*
91961a7b24SJohn Baldwin  * There are three different lists of turnstiles as follows.  The list
92961a7b24SJohn Baldwin  * connected by ts_link entries is a per-thread list of all the turnstiles
93961a7b24SJohn Baldwin  * attached to locks that we own.  This is used to fixup our priority when
94961a7b24SJohn Baldwin  * a lock is released.  The other two lists use the ts_hash entries.  The
955b7de7e1SJohn Baldwin  * first of these two is the turnstile chain list that a turnstile is on
965b7de7e1SJohn Baldwin  * when it is attached to a lock.  The second list to use ts_hash is the
975b7de7e1SJohn Baldwin  * free list hung off of a turnstile that is attached to a lock.
98961a7b24SJohn Baldwin  *
99961a7b24SJohn Baldwin  * Each turnstile contains two lists of threads.  The ts_blocked list is
100961a7b24SJohn Baldwin  * a linked list of threads blocked on the turnstile's lock.  The
101595bc82aSJohn Baldwin  * ts_pending list is a linked list of threads previously awakened by
102961a7b24SJohn Baldwin  * turnstile_signal() or turnstile_wait() that are waiting to be put on
103961a7b24SJohn Baldwin  * the run queue.
104961a7b24SJohn Baldwin  *
105961a7b24SJohn Baldwin  * Locking key:
106961a7b24SJohn Baldwin  *  c - turnstile chain lock
107961a7b24SJohn Baldwin  *  q - td_contested lock
1080cde2e34SJason Evans  */
109961a7b24SJohn Baldwin struct turnstile {
110961a7b24SJohn Baldwin 	TAILQ_HEAD(, thread) ts_blocked;	/* (c + q) Blocked threads. */
111961a7b24SJohn Baldwin 	TAILQ_HEAD(, thread) ts_pending;	/* (c) Pending threads. */
112961a7b24SJohn Baldwin 	LIST_ENTRY(turnstile) ts_hash;		/* (c) Chain and free list. */
113961a7b24SJohn Baldwin 	LIST_ENTRY(turnstile) ts_link;		/* (q) Contested locks. */
114961a7b24SJohn Baldwin 	LIST_HEAD(, turnstile) ts_free;		/* (c) Free turnstiles. */
115961a7b24SJohn Baldwin 	struct lock_object *ts_lockobj;		/* (c) Lock we reference. */
11679a13d01SJohn Baldwin 	struct thread *ts_owner;		/* (c + q) Who owns the lock. */
1178484de75SJohn Baldwin };
1188484de75SJohn Baldwin 
119961a7b24SJohn Baldwin struct turnstile_chain {
120961a7b24SJohn Baldwin 	LIST_HEAD(, turnstile) tc_turnstiles;	/* List of turnstiles. */
121961a7b24SJohn Baldwin 	struct mtx tc_lock;			/* Spin lock for this chain. */
122ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
123ef0ebfc3SJohn Baldwin 	u_int	tc_depth;			/* Length of tc_queues. */
124ef0ebfc3SJohn Baldwin 	u_int	tc_max_depth;			/* Max length of tc_queues. */
125ef0ebfc3SJohn Baldwin #endif
126961a7b24SJohn Baldwin };
127961a7b24SJohn Baldwin 
128ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
129ef0ebfc3SJohn Baldwin u_int turnstile_max_depth;
130ef0ebfc3SJohn Baldwin SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0, "turnstile profiling");
131ef0ebfc3SJohn Baldwin SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
132ef0ebfc3SJohn Baldwin     "turnstile chain stats");
133ef0ebfc3SJohn Baldwin SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
134ef0ebfc3SJohn Baldwin     &turnstile_max_depth, 0, "maxmimum depth achieved of a single chain");
135ef0ebfc3SJohn Baldwin #endif
136961a7b24SJohn Baldwin static struct mtx td_contested_lock;
137961a7b24SJohn Baldwin static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
138961a7b24SJohn Baldwin 
139961a7b24SJohn Baldwin MALLOC_DEFINE(M_TURNSTILE, "turnstiles", "turnstiles");
140c53c013bSJohn Baldwin 
141c53c013bSJohn Baldwin /*
1429ed346baSBosko Milekic  * Prototypes for non-exported routines.
1439ed346baSBosko Milekic  */
144961a7b24SJohn Baldwin static void	init_turnstile0(void *dummy);
14501bd10e1SJohn Baldwin #ifdef TURNSTILE_PROFILING
14601bd10e1SJohn Baldwin static void	init_turnstile_profiling(void *arg);
14701bd10e1SJohn Baldwin #endif
148f5c157d9SJohn Baldwin static void	propagate_priority(struct thread *td);
149f5c157d9SJohn Baldwin static int	turnstile_adjust_thread(struct turnstile *ts,
150f5c157d9SJohn Baldwin 		    struct thread *td);
151961a7b24SJohn Baldwin static void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
15236412d79SJohn Baldwin 
153961a7b24SJohn Baldwin /*
154961a7b24SJohn Baldwin  * Walks the chain of turnstiles and their owners to propagate the priority
155961a7b24SJohn Baldwin  * of the thread being blocked to all the threads holding locks that have to
156961a7b24SJohn Baldwin  * release their locks before this thread can run again.
157961a7b24SJohn Baldwin  */
15836412d79SJohn Baldwin static void
159b40ce416SJulian Elischer propagate_priority(struct thread *td)
16036412d79SJohn Baldwin {
161961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
162961a7b24SJohn Baldwin 	struct turnstile *ts;
163961a7b24SJohn Baldwin 	int pri;
16436412d79SJohn Baldwin 
1651bd0eefbSJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
166961a7b24SJohn Baldwin 	pri = td->td_priority;
167961a7b24SJohn Baldwin 	ts = td->td_blocked;
16836412d79SJohn Baldwin 	for (;;) {
169961a7b24SJohn Baldwin 		td = ts->ts_owner;
17036412d79SJohn Baldwin 
171b40ce416SJulian Elischer 		if (td == NULL) {
17236412d79SJohn Baldwin 			/*
17336412d79SJohn Baldwin 			 * This really isn't quite right. Really
174b40ce416SJulian Elischer 			 * ought to bump priority of thread that
175961a7b24SJohn Baldwin 			 * next acquires the lock.
17636412d79SJohn Baldwin 			 */
17736412d79SJohn Baldwin 			return;
17836412d79SJohn Baldwin 		}
1799ed346baSBosko Milekic 
180e602ba25SJulian Elischer 		MPASS(td->td_proc != NULL);
181b40ce416SJulian Elischer 		MPASS(td->td_proc->p_magic == P_MAGIC);
1821bd0eefbSJohn Baldwin 
183961a7b24SJohn Baldwin 		/*
184961a7b24SJohn Baldwin 		 * XXX: The owner of a turnstile can be stale if it is the
185961a7b24SJohn Baldwin 		 * first thread to grab a slock of a sx lock.  In that case
186961a7b24SJohn Baldwin 		 * it is possible for us to be at SSLEEP or some other
187961a7b24SJohn Baldwin 		 * weird state.  We should probably just return if the state
188961a7b24SJohn Baldwin 		 * isn't SRUN or SLOCK.
189961a7b24SJohn Baldwin 		 */
190961a7b24SJohn Baldwin 		KASSERT(!TD_IS_SLEEPING(td),
191f5c157d9SJohn Baldwin 		    ("sleeping thread (tid %d) owns a non-sleepable lock",
192f5c157d9SJohn Baldwin 		    td->td_tid));
193961a7b24SJohn Baldwin 
194961a7b24SJohn Baldwin 		/*
195961a7b24SJohn Baldwin 		 * If this thread already has higher priority than the
196961a7b24SJohn Baldwin 		 * thread that is being blocked, we are finished.
197961a7b24SJohn Baldwin 		 */
198961a7b24SJohn Baldwin 		if (td->td_priority <= pri)
199961a7b24SJohn Baldwin 			return;
2001bd0eefbSJohn Baldwin 
20136412d79SJohn Baldwin 		/*
202f5c157d9SJohn Baldwin 		 * Bump this thread's priority.
20336412d79SJohn Baldwin 		 */
204f5c157d9SJohn Baldwin 		sched_lend_prio(td, pri);
205f5c157d9SJohn Baldwin 
206f5c157d9SJohn Baldwin 		/*
207f5c157d9SJohn Baldwin 		 * If lock holder is actually running or on the run queue
208f5c157d9SJohn Baldwin 		 * then we are done.
209f5c157d9SJohn Baldwin 		 */
210f5c157d9SJohn Baldwin 		if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
211f5c157d9SJohn Baldwin 			MPASS(td->td_blocked == NULL);
21236412d79SJohn Baldwin 			return;
21336412d79SJohn Baldwin 		}
214d5a08a60SJake Burkholder 
2151b43703bSJohn Baldwin #ifndef SMP
2161b43703bSJohn Baldwin 		/*
217b40ce416SJulian Elischer 		 * For UP, we check to see if td is curthread (this shouldn't
2181b43703bSJohn Baldwin 		 * ever happen however as it would mean we are in a deadlock.)
2191b43703bSJohn Baldwin 		 */
220b40ce416SJulian Elischer 		KASSERT(td != curthread, ("Deadlock detected"));
2211b43703bSJohn Baldwin #endif
2221b43703bSJohn Baldwin 
22336412d79SJohn Baldwin 		/*
224961a7b24SJohn Baldwin 		 * If we aren't blocked on a lock, we should be.
22536412d79SJohn Baldwin 		 */
226551cf4e1SJohn Baldwin 		KASSERT(TD_ON_LOCK(td), (
227f5c157d9SJohn Baldwin 		    "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
228f5c157d9SJohn Baldwin 		    td->td_tid, td->td_proc->p_comm, td->td_state,
229961a7b24SJohn Baldwin 		    ts->ts_lockobj->lo_name));
23036412d79SJohn Baldwin 
23136412d79SJohn Baldwin 		/*
232961a7b24SJohn Baldwin 		 * Pick up the lock that td is blocked on.
23336412d79SJohn Baldwin 		 */
234961a7b24SJohn Baldwin 		ts = td->td_blocked;
235961a7b24SJohn Baldwin 		MPASS(ts != NULL);
236961a7b24SJohn Baldwin 		tc = TC_LOOKUP(ts->ts_lockobj);
237961a7b24SJohn Baldwin 		mtx_lock_spin(&tc->tc_lock);
23836412d79SJohn Baldwin 
239f5c157d9SJohn Baldwin 		/* Resort td on the list if needed. */
240f5c157d9SJohn Baldwin 		if (!turnstile_adjust_thread(ts, td)) {
241f5c157d9SJohn Baldwin 			mtx_unlock_spin(&tc->tc_lock);
242f5c157d9SJohn Baldwin 			return;
243f5c157d9SJohn Baldwin 		}
244f5c157d9SJohn Baldwin 		mtx_unlock_spin(&tc->tc_lock);
245f5c157d9SJohn Baldwin 	}
246f5c157d9SJohn Baldwin }
247f5c157d9SJohn Baldwin 
248f5c157d9SJohn Baldwin /*
249f5c157d9SJohn Baldwin  * Adjust the thread's position on a turnstile after its priority has been
250f5c157d9SJohn Baldwin  * changed.
251f5c157d9SJohn Baldwin  */
252f5c157d9SJohn Baldwin static int
253f5c157d9SJohn Baldwin turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
254f5c157d9SJohn Baldwin {
255f5c157d9SJohn Baldwin 	struct turnstile_chain *tc;
256f5c157d9SJohn Baldwin 	struct thread *td1, *td2;
257f5c157d9SJohn Baldwin 
258f5c157d9SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
259f5c157d9SJohn Baldwin 	MPASS(TD_ON_LOCK(td));
260f5c157d9SJohn Baldwin 
26136412d79SJohn Baldwin 	/*
2626b6bd95eSJohn Baldwin 	 * This thread may not be blocked on this turnstile anymore
2636b6bd95eSJohn Baldwin 	 * but instead might already be woken up on another CPU
2646b6bd95eSJohn Baldwin 	 * that is waiting on sched_lock in turnstile_unpend() to
2656b6bd95eSJohn Baldwin 	 * finish waking this thread up.  We can detect this case
2666b6bd95eSJohn Baldwin 	 * by checking to see if this thread has been given a
2676b6bd95eSJohn Baldwin 	 * turnstile by either turnstile_signal() or
268ef2c0ba7SJohn Baldwin 	 * turnstile_broadcast().  In this case, treat the thread as
2696b6bd95eSJohn Baldwin 	 * if it was already running.
27079a13d01SJohn Baldwin 	 */
271f5c157d9SJohn Baldwin 	if (td->td_turnstile != NULL)
272f5c157d9SJohn Baldwin 		return (0);
27379a13d01SJohn Baldwin 
27479a13d01SJohn Baldwin 	/*
275f5c157d9SJohn Baldwin 	 * Check if the thread needs to be moved on the blocked chain.
276f5c157d9SJohn Baldwin 	 * It needs to be moved if either its priority is lower than
277f5c157d9SJohn Baldwin 	 * the previous thread or higher than the next thread.
27836412d79SJohn Baldwin 	 */
279f5c157d9SJohn Baldwin 	tc = TC_LOOKUP(ts->ts_lockobj);
280f5c157d9SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
281551cf4e1SJohn Baldwin 	td1 = TAILQ_PREV(td, threadqueue, td_lockq);
282f5c157d9SJohn Baldwin 	td2 = TAILQ_NEXT(td, td_lockq);
283f5c157d9SJohn Baldwin 	if ((td1 != NULL && td->td_priority < td1->td_priority) ||
284f5c157d9SJohn Baldwin 	    (td2 != NULL && td->td_priority > td2->td_priority)) {
28536412d79SJohn Baldwin 
28636412d79SJohn Baldwin 		/*
287b40ce416SJulian Elischer 		 * Remove thread from blocked chain and determine where
288f5c157d9SJohn Baldwin 		 * it should be moved to.
28936412d79SJohn Baldwin 		 */
290961a7b24SJohn Baldwin 		mtx_lock_spin(&td_contested_lock);
291961a7b24SJohn Baldwin 		TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
292961a7b24SJohn Baldwin 		TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq) {
293b40ce416SJulian Elischer 			MPASS(td1->td_proc->p_magic == P_MAGIC);
294f5c157d9SJohn Baldwin 			if (td1->td_priority > td->td_priority)
29536412d79SJohn Baldwin 				break;
29636412d79SJohn Baldwin 		}
2979ed346baSBosko Milekic 
298f5c157d9SJohn Baldwin 		if (td1 == NULL)
299f5c157d9SJohn Baldwin 			TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
300f5c157d9SJohn Baldwin 		else
301551cf4e1SJohn Baldwin 			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
302961a7b24SJohn Baldwin 		mtx_unlock_spin(&td_contested_lock);
303f5c157d9SJohn Baldwin 		if (td1 == NULL)
304f5c157d9SJohn Baldwin 			CTR3(KTR_LOCK,
305f5c157d9SJohn Baldwin 		    "turnstile_adjust_thread: td %d put at tail on [%p] %s",
306f5c157d9SJohn Baldwin 			    td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
307f5c157d9SJohn Baldwin 		else
30836412d79SJohn Baldwin 			CTR4(KTR_LOCK,
309f5c157d9SJohn Baldwin 		    "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
310f5c157d9SJohn Baldwin 			    td->td_tid, td1->td_tid, ts->ts_lockobj,
311f5c157d9SJohn Baldwin 			    ts->ts_lockobj->lo_name);
31236412d79SJohn Baldwin 	}
313f5c157d9SJohn Baldwin 	return (1);
31436412d79SJohn Baldwin }
31536412d79SJohn Baldwin 
3166c35e809SDag-Erling Smørgrav /*
317961a7b24SJohn Baldwin  * Early initialization of turnstiles.  This is not done via a SYSINIT()
318961a7b24SJohn Baldwin  * since this needs to be initialized very early when mutexes are first
319961a7b24SJohn Baldwin  * initialized.
3206283b7d0SJohn Baldwin  */
3216283b7d0SJohn Baldwin void
322961a7b24SJohn Baldwin init_turnstiles(void)
3236283b7d0SJohn Baldwin {
324961a7b24SJohn Baldwin 	int i;
3256283b7d0SJohn Baldwin 
326961a7b24SJohn Baldwin 	for (i = 0; i < TC_TABLESIZE; i++) {
327961a7b24SJohn Baldwin 		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
328961a7b24SJohn Baldwin 		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
329961a7b24SJohn Baldwin 		    NULL, MTX_SPIN);
33001bd10e1SJohn Baldwin 	}
33101bd10e1SJohn Baldwin 	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
33201bd10e1SJohn Baldwin 	thread0.td_turnstile = NULL;
33301bd10e1SJohn Baldwin }
33401bd10e1SJohn Baldwin 
335ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
33601bd10e1SJohn Baldwin static void
33701bd10e1SJohn Baldwin init_turnstile_profiling(void *arg)
33801bd10e1SJohn Baldwin {
33901bd10e1SJohn Baldwin 	struct sysctl_oid *chain_oid;
34001bd10e1SJohn Baldwin 	char chain_name[10];
34101bd10e1SJohn Baldwin 	int i;
34201bd10e1SJohn Baldwin 
34301bd10e1SJohn Baldwin 	for (i = 0; i < TC_TABLESIZE; i++) {
344ef0ebfc3SJohn Baldwin 		snprintf(chain_name, sizeof(chain_name), "%d", i);
345ef0ebfc3SJohn Baldwin 		chain_oid = SYSCTL_ADD_NODE(NULL,
346ef0ebfc3SJohn Baldwin 		    SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
347ef0ebfc3SJohn Baldwin 		    chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
348ef0ebfc3SJohn Baldwin 		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
349ef0ebfc3SJohn Baldwin 		    "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
350ef0ebfc3SJohn Baldwin 		    NULL);
351ef0ebfc3SJohn Baldwin 		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
352ef0ebfc3SJohn Baldwin 		    "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
353ef0ebfc3SJohn Baldwin 		    0, NULL);
35401bd10e1SJohn Baldwin 	}
35501bd10e1SJohn Baldwin }
35601bd10e1SJohn Baldwin SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
35701bd10e1SJohn Baldwin     init_turnstile_profiling, NULL);
358ef0ebfc3SJohn Baldwin #endif
3596283b7d0SJohn Baldwin 
360961a7b24SJohn Baldwin static void
361961a7b24SJohn Baldwin init_turnstile0(void *dummy)
3626283b7d0SJohn Baldwin {
3636283b7d0SJohn Baldwin 
364961a7b24SJohn Baldwin 	thread0.td_turnstile = turnstile_alloc();
365961a7b24SJohn Baldwin }
366961a7b24SJohn Baldwin SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
3676c35e809SDag-Erling Smørgrav 
368961a7b24SJohn Baldwin /*
369f5c157d9SJohn Baldwin  * Update a thread on the turnstile list after it's priority has been changed.
370f5c157d9SJohn Baldwin  * The old priority is passed in as an argument.
371f5c157d9SJohn Baldwin  */
372f5c157d9SJohn Baldwin void
373f5c157d9SJohn Baldwin turnstile_adjust(struct thread *td, u_char oldpri)
374f5c157d9SJohn Baldwin {
375f5c157d9SJohn Baldwin 	struct turnstile_chain *tc;
376f5c157d9SJohn Baldwin 	struct turnstile *ts;
377f5c157d9SJohn Baldwin 
378f5c157d9SJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
379f5c157d9SJohn Baldwin 	MPASS(TD_ON_LOCK(td));
380f5c157d9SJohn Baldwin 
381f5c157d9SJohn Baldwin 	/*
382f5c157d9SJohn Baldwin 	 * Pick up the lock that td is blocked on.
383f5c157d9SJohn Baldwin 	 */
384f5c157d9SJohn Baldwin 	ts = td->td_blocked;
385f5c157d9SJohn Baldwin 	MPASS(ts != NULL);
386f5c157d9SJohn Baldwin 	tc = TC_LOOKUP(ts->ts_lockobj);
387f5c157d9SJohn Baldwin 	mtx_lock_spin(&tc->tc_lock);
388f5c157d9SJohn Baldwin 
389f5c157d9SJohn Baldwin 	/* Resort the turnstile on the list. */
390f5c157d9SJohn Baldwin 	if (!turnstile_adjust_thread(ts, td)) {
391f5c157d9SJohn Baldwin 		mtx_unlock_spin(&tc->tc_lock);
392f5c157d9SJohn Baldwin 		return;
393f5c157d9SJohn Baldwin 	}
394f5c157d9SJohn Baldwin 
395f5c157d9SJohn Baldwin 	/*
396f5c157d9SJohn Baldwin 	 * If our priority was lowered and we are at the head of the
397f5c157d9SJohn Baldwin 	 * turnstile, then propagate our new priority up the chain.
398f5c157d9SJohn Baldwin 	 * Note that we currently don't try to revoke lent priorities
399f5c157d9SJohn Baldwin 	 * when our priority goes up.
400f5c157d9SJohn Baldwin 	 */
401f5c157d9SJohn Baldwin 	if (td == TAILQ_FIRST(&ts->ts_blocked) && td->td_priority < oldpri) {
402f5c157d9SJohn Baldwin 		mtx_unlock_spin(&tc->tc_lock);
403f5c157d9SJohn Baldwin 		propagate_priority(td);
404f5c157d9SJohn Baldwin 	} else
405f5c157d9SJohn Baldwin 		mtx_unlock_spin(&tc->tc_lock);
406f5c157d9SJohn Baldwin }
407f5c157d9SJohn Baldwin 
408f5c157d9SJohn Baldwin /*
409961a7b24SJohn Baldwin  * Set the owner of the lock this turnstile is attached to.
410961a7b24SJohn Baldwin  */
411961a7b24SJohn Baldwin static void
412961a7b24SJohn Baldwin turnstile_setowner(struct turnstile *ts, struct thread *owner)
413961a7b24SJohn Baldwin {
414961a7b24SJohn Baldwin 
415961a7b24SJohn Baldwin 	mtx_assert(&td_contested_lock, MA_OWNED);
416961a7b24SJohn Baldwin 	MPASS(owner->td_proc->p_magic == P_MAGIC);
417961a7b24SJohn Baldwin 	MPASS(ts->ts_owner == NULL);
418961a7b24SJohn Baldwin 	ts->ts_owner = owner;
419961a7b24SJohn Baldwin 	LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
420961a7b24SJohn Baldwin }
421961a7b24SJohn Baldwin 
422961a7b24SJohn Baldwin /*
423961a7b24SJohn Baldwin  * Malloc a turnstile for a new thread, initialize it and return it.
424961a7b24SJohn Baldwin  */
425961a7b24SJohn Baldwin struct turnstile *
426961a7b24SJohn Baldwin turnstile_alloc(void)
427961a7b24SJohn Baldwin {
428961a7b24SJohn Baldwin 	struct turnstile *ts;
429961a7b24SJohn Baldwin 
430961a7b24SJohn Baldwin 	ts = malloc(sizeof(struct turnstile), M_TURNSTILE, M_WAITOK | M_ZERO);
431961a7b24SJohn Baldwin 	TAILQ_INIT(&ts->ts_blocked);
432961a7b24SJohn Baldwin 	TAILQ_INIT(&ts->ts_pending);
433961a7b24SJohn Baldwin 	LIST_INIT(&ts->ts_free);
434961a7b24SJohn Baldwin 	return (ts);
435961a7b24SJohn Baldwin }
436961a7b24SJohn Baldwin 
437961a7b24SJohn Baldwin /*
438961a7b24SJohn Baldwin  * Free a turnstile when a thread is destroyed.
439961a7b24SJohn Baldwin  */
440961a7b24SJohn Baldwin void
441961a7b24SJohn Baldwin turnstile_free(struct turnstile *ts)
442961a7b24SJohn Baldwin {
443961a7b24SJohn Baldwin 
444961a7b24SJohn Baldwin 	MPASS(ts != NULL);
445961a7b24SJohn Baldwin 	MPASS(TAILQ_EMPTY(&ts->ts_blocked));
446961a7b24SJohn Baldwin 	MPASS(TAILQ_EMPTY(&ts->ts_pending));
447961a7b24SJohn Baldwin 	free(ts, M_TURNSTILE);
448961a7b24SJohn Baldwin }
449961a7b24SJohn Baldwin 
450961a7b24SJohn Baldwin /*
4512ff0e645SJohn Baldwin  * Lock the turnstile chain associated with the specified lock.
4522ff0e645SJohn Baldwin  */
4532ff0e645SJohn Baldwin void
4542ff0e645SJohn Baldwin turnstile_lock(struct lock_object *lock)
4552ff0e645SJohn Baldwin {
4562ff0e645SJohn Baldwin 	struct turnstile_chain *tc;
4572ff0e645SJohn Baldwin 
4582ff0e645SJohn Baldwin 	tc = TC_LOOKUP(lock);
4592ff0e645SJohn Baldwin 	mtx_lock_spin(&tc->tc_lock);
4602ff0e645SJohn Baldwin }
4612ff0e645SJohn Baldwin 
4622ff0e645SJohn Baldwin /*
463961a7b24SJohn Baldwin  * Look up the turnstile for a lock in the hash table locking the associated
4642ff0e645SJohn Baldwin  * turnstile chain along the way.  If no turnstile is found in the hash
4652ff0e645SJohn Baldwin  * table, NULL is returned.
466961a7b24SJohn Baldwin  */
467961a7b24SJohn Baldwin struct turnstile *
468961a7b24SJohn Baldwin turnstile_lookup(struct lock_object *lock)
469961a7b24SJohn Baldwin {
470961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
471961a7b24SJohn Baldwin 	struct turnstile *ts;
472961a7b24SJohn Baldwin 
473961a7b24SJohn Baldwin 	tc = TC_LOOKUP(lock);
4742ff0e645SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
475961a7b24SJohn Baldwin 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
476961a7b24SJohn Baldwin 		if (ts->ts_lockobj == lock)
477961a7b24SJohn Baldwin 			return (ts);
478961a7b24SJohn Baldwin 	return (NULL);
479961a7b24SJohn Baldwin }
480961a7b24SJohn Baldwin 
481961a7b24SJohn Baldwin /*
482961a7b24SJohn Baldwin  * Unlock the turnstile chain associated with a given lock.
483961a7b24SJohn Baldwin  */
484961a7b24SJohn Baldwin void
485961a7b24SJohn Baldwin turnstile_release(struct lock_object *lock)
486961a7b24SJohn Baldwin {
487961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
488961a7b24SJohn Baldwin 
489961a7b24SJohn Baldwin 	tc = TC_LOOKUP(lock);
490961a7b24SJohn Baldwin 	mtx_unlock_spin(&tc->tc_lock);
491961a7b24SJohn Baldwin }
492961a7b24SJohn Baldwin 
493961a7b24SJohn Baldwin /*
494961a7b24SJohn Baldwin  * Take ownership of a turnstile and adjust the priority of the new
495961a7b24SJohn Baldwin  * owner appropriately.
496961a7b24SJohn Baldwin  */
497961a7b24SJohn Baldwin void
4982ff0e645SJohn Baldwin turnstile_claim(struct lock_object *lock)
499961a7b24SJohn Baldwin {
500961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
5012ff0e645SJohn Baldwin 	struct turnstile *ts;
502961a7b24SJohn Baldwin 	struct thread *td, *owner;
503961a7b24SJohn Baldwin 
5042ff0e645SJohn Baldwin 	tc = TC_LOOKUP(lock);
505961a7b24SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
5062ff0e645SJohn Baldwin 	ts = turnstile_lookup(lock);
5072ff0e645SJohn Baldwin 	MPASS(ts != NULL);
508961a7b24SJohn Baldwin 
509961a7b24SJohn Baldwin 	owner = curthread;
510961a7b24SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
511961a7b24SJohn Baldwin 	turnstile_setowner(ts, owner);
512961a7b24SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
513961a7b24SJohn Baldwin 
514961a7b24SJohn Baldwin 	td = TAILQ_FIRST(&ts->ts_blocked);
515961a7b24SJohn Baldwin 	MPASS(td != NULL);
516961a7b24SJohn Baldwin 	MPASS(td->td_proc->p_magic == P_MAGIC);
517961a7b24SJohn Baldwin 	mtx_unlock_spin(&tc->tc_lock);
518961a7b24SJohn Baldwin 
519961a7b24SJohn Baldwin 	/*
520961a7b24SJohn Baldwin 	 * Update the priority of the new owner if needed.
521961a7b24SJohn Baldwin 	 */
522961a7b24SJohn Baldwin 	mtx_lock_spin(&sched_lock);
523961a7b24SJohn Baldwin 	if (td->td_priority < owner->td_priority)
524f5c157d9SJohn Baldwin 		sched_lend_prio(owner, td->td_priority);
525961a7b24SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
526961a7b24SJohn Baldwin }
527961a7b24SJohn Baldwin 
528961a7b24SJohn Baldwin /*
5292ff0e645SJohn Baldwin  * Block the current thread on the turnstile assicated with 'lock'.  This
5302ff0e645SJohn Baldwin  * function will context switch and not return until this thread has been
5312ff0e645SJohn Baldwin  * woken back up.  This function must be called with the appropriate
5322ff0e645SJohn Baldwin  * turnstile chain locked and will return with it unlocked.
533961a7b24SJohn Baldwin  */
534961a7b24SJohn Baldwin void
5352ff0e645SJohn Baldwin turnstile_wait(struct lock_object *lock, struct thread *owner)
536961a7b24SJohn Baldwin {
537961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
5382ff0e645SJohn Baldwin 	struct turnstile *ts;
539961a7b24SJohn Baldwin 	struct thread *td, *td1;
540961a7b24SJohn Baldwin 
541961a7b24SJohn Baldwin 	td = curthread;
542961a7b24SJohn Baldwin 	tc = TC_LOOKUP(lock);
543961a7b24SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
544961a7b24SJohn Baldwin 	MPASS(td->td_turnstile != NULL);
545961a7b24SJohn Baldwin 	MPASS(owner != NULL);
546961a7b24SJohn Baldwin 	MPASS(owner->td_proc->p_magic == P_MAGIC);
547961a7b24SJohn Baldwin 
5482ff0e645SJohn Baldwin 	/* Look up the turnstile associated with the lock 'lock'. */
5492ff0e645SJohn Baldwin 	ts = turnstile_lookup(lock);
5502ff0e645SJohn Baldwin 
5512ff0e645SJohn Baldwin 	/*
5522ff0e645SJohn Baldwin 	 * If the lock does not already have a turnstile, use this thread's
5532ff0e645SJohn Baldwin 	 * turnstile.  Otherwise insert the current thread into the
5542ff0e645SJohn Baldwin 	 * turnstile already in use by this lock.
5552ff0e645SJohn Baldwin 	 */
556961a7b24SJohn Baldwin 	if (ts == NULL) {
557ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
558ef0ebfc3SJohn Baldwin 		tc->tc_depth++;
559ef0ebfc3SJohn Baldwin 		if (tc->tc_depth > tc->tc_max_depth) {
560ef0ebfc3SJohn Baldwin 			tc->tc_max_depth = tc->tc_depth;
561ef0ebfc3SJohn Baldwin 			if (tc->tc_max_depth > turnstile_max_depth)
562ef0ebfc3SJohn Baldwin 				turnstile_max_depth = tc->tc_max_depth;
563ef0ebfc3SJohn Baldwin 		}
564ef0ebfc3SJohn Baldwin #endif
565961a7b24SJohn Baldwin 		ts = td->td_turnstile;
566961a7b24SJohn Baldwin 		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
567961a7b24SJohn Baldwin 		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
568961a7b24SJohn Baldwin 		    ("thread's turnstile has pending threads"));
569961a7b24SJohn Baldwin 		KASSERT(TAILQ_EMPTY(&ts->ts_blocked),
570961a7b24SJohn Baldwin 		    ("thread's turnstile has a non-empty queue"));
571961a7b24SJohn Baldwin 		KASSERT(LIST_EMPTY(&ts->ts_free),
572961a7b24SJohn Baldwin 		    ("thread's turnstile has a non-empty free list"));
573961a7b24SJohn Baldwin 		KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
574961a7b24SJohn Baldwin 		ts->ts_lockobj = lock;
575961a7b24SJohn Baldwin 		mtx_lock_spin(&td_contested_lock);
576961a7b24SJohn Baldwin 		TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
577961a7b24SJohn Baldwin 		turnstile_setowner(ts, owner);
578961a7b24SJohn Baldwin 		mtx_unlock_spin(&td_contested_lock);
579961a7b24SJohn Baldwin 	} else {
580961a7b24SJohn Baldwin 		TAILQ_FOREACH(td1, &ts->ts_blocked, td_lockq)
581961a7b24SJohn Baldwin 			if (td1->td_priority > td->td_priority)
5826c35e809SDag-Erling Smørgrav 				break;
583961a7b24SJohn Baldwin 		mtx_lock_spin(&td_contested_lock);
584961a7b24SJohn Baldwin 		if (td1 != NULL)
585961a7b24SJohn Baldwin 			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
586961a7b24SJohn Baldwin 		else
587961a7b24SJohn Baldwin 			TAILQ_INSERT_TAIL(&ts->ts_blocked, td, td_lockq);
588961a7b24SJohn Baldwin 		mtx_unlock_spin(&td_contested_lock);
589961a7b24SJohn Baldwin 		MPASS(td->td_turnstile != NULL);
590961a7b24SJohn Baldwin 		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
591961a7b24SJohn Baldwin 		MPASS(owner == ts->ts_owner);
5926c35e809SDag-Erling Smørgrav 	}
593961a7b24SJohn Baldwin 	td->td_turnstile = NULL;
594961a7b24SJohn Baldwin 	mtx_unlock_spin(&tc->tc_lock);
59536412d79SJohn Baldwin 
5969ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
59736412d79SJohn Baldwin 	/*
598961a7b24SJohn Baldwin 	 * Handle race condition where a thread on another CPU that owns
599961a7b24SJohn Baldwin 	 * lock 'lock' could have woken us in between us dropping the
600961a7b24SJohn Baldwin 	 * turnstile chain lock and acquiring the sched_lock.
60136412d79SJohn Baldwin 	 */
602961a7b24SJohn Baldwin 	if (td->td_flags & TDF_TSNOBLOCK) {
603961a7b24SJohn Baldwin 		td->td_flags &= ~TDF_TSNOBLOCK;
6049ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
60536412d79SJohn Baldwin 		return;
60636412d79SJohn Baldwin 	}
6079ed346baSBosko Milekic 
60836412d79SJohn Baldwin #ifdef notyet
60936412d79SJohn Baldwin 	/*
6109ed346baSBosko Milekic 	 * If we're borrowing an interrupted thread's VM context, we
6119ed346baSBosko Milekic 	 * must clean up before going to sleep.
61236412d79SJohn Baldwin 	 */
613b40ce416SJulian Elischer 	if (td->td_ithd != NULL) {
614b40ce416SJulian Elischer 		struct ithd *it = td->td_ithd;
61536412d79SJohn Baldwin 
61636412d79SJohn Baldwin 		if (it->it_interrupted) {
617961a7b24SJohn Baldwin 			if (LOCK_LOG_TEST(lock, 0))
618961a7b24SJohn Baldwin 				CTR3(KTR_LOCK, "%s: %p interrupted %p",
619961a7b24SJohn Baldwin 				    __func__, it, it->it_interrupted);
62036412d79SJohn Baldwin 			intr_thd_fixup(it);
62136412d79SJohn Baldwin 		}
62236412d79SJohn Baldwin 	}
62336412d79SJohn Baldwin #endif
62436412d79SJohn Baldwin 
625961a7b24SJohn Baldwin 	/* Save who we are blocked on and switch. */
626961a7b24SJohn Baldwin 	td->td_blocked = ts;
627961a7b24SJohn Baldwin 	td->td_lockname = lock->lo_name;
628551cf4e1SJohn Baldwin 	TD_SET_LOCK(td);
629b40ce416SJulian Elischer 	propagate_priority(td);
6309ed346baSBosko Milekic 
631961a7b24SJohn Baldwin 	if (LOCK_LOG_TEST(lock, 0))
632f5c157d9SJohn Baldwin 		CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
633f5c157d9SJohn Baldwin 		    td->td_tid, lock, lock->lo_name);
6349ed346baSBosko Milekic 
635bf0acc27SJohn Baldwin 	mi_switch(SW_VOL, NULL);
6369ed346baSBosko Milekic 
637961a7b24SJohn Baldwin 	if (LOCK_LOG_TEST(lock, 0))
638f5c157d9SJohn Baldwin 		CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
639f5c157d9SJohn Baldwin 		    __func__, td->td_tid, lock, lock->lo_name);
6409ed346baSBosko Milekic 
6419ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
64236412d79SJohn Baldwin }
6439ed346baSBosko Milekic 
644961a7b24SJohn Baldwin /*
645961a7b24SJohn Baldwin  * Pick the highest priority thread on this turnstile and put it on the
646961a7b24SJohn Baldwin  * pending list.  This must be called with the turnstile chain locked.
647961a7b24SJohn Baldwin  */
648961a7b24SJohn Baldwin int
649961a7b24SJohn Baldwin turnstile_signal(struct turnstile *ts)
650961a7b24SJohn Baldwin {
651961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
652961a7b24SJohn Baldwin 	struct thread *td;
653961a7b24SJohn Baldwin 	int empty;
654961a7b24SJohn Baldwin 
655961a7b24SJohn Baldwin 	MPASS(ts != NULL);
656961a7b24SJohn Baldwin 	MPASS(curthread->td_proc->p_magic == P_MAGIC);
657961a7b24SJohn Baldwin 	MPASS(ts->ts_owner == curthread);
658961a7b24SJohn Baldwin 	tc = TC_LOOKUP(ts->ts_lockobj);
659961a7b24SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
6609ed346baSBosko Milekic 
6619ed346baSBosko Milekic 	/*
662961a7b24SJohn Baldwin 	 * Pick the highest priority thread blocked on this lock and
663961a7b24SJohn Baldwin 	 * move it to the pending list.
6649ed346baSBosko Milekic 	 */
665961a7b24SJohn Baldwin 	td = TAILQ_FIRST(&ts->ts_blocked);
666b40ce416SJulian Elischer 	MPASS(td->td_proc->p_magic == P_MAGIC);
667961a7b24SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
668961a7b24SJohn Baldwin 	TAILQ_REMOVE(&ts->ts_blocked, td, td_lockq);
669961a7b24SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
670961a7b24SJohn Baldwin 	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
6719ed346baSBosko Milekic 
672961a7b24SJohn Baldwin 	/*
673961a7b24SJohn Baldwin 	 * If the turnstile is now empty, remove it from its chain and
674961a7b24SJohn Baldwin 	 * give it to the about-to-be-woken thread.  Otherwise take a
675961a7b24SJohn Baldwin 	 * turnstile from the free list and give it to the thread.
676961a7b24SJohn Baldwin 	 */
677961a7b24SJohn Baldwin 	empty = TAILQ_EMPTY(&ts->ts_blocked);
678ef0ebfc3SJohn Baldwin 	if (empty) {
679961a7b24SJohn Baldwin 		MPASS(LIST_EMPTY(&ts->ts_free));
680ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
681ef0ebfc3SJohn Baldwin 		tc->tc_depth--;
682ef0ebfc3SJohn Baldwin #endif
683ef0ebfc3SJohn Baldwin 	} else
684961a7b24SJohn Baldwin 		ts = LIST_FIRST(&ts->ts_free);
685da1d503bSJohn Baldwin 	MPASS(ts != NULL);
686961a7b24SJohn Baldwin 	LIST_REMOVE(ts, ts_hash);
687961a7b24SJohn Baldwin 	td->td_turnstile = ts;
6889ed346baSBosko Milekic 
689961a7b24SJohn Baldwin 	return (empty);
690961a7b24SJohn Baldwin }
691961a7b24SJohn Baldwin 
692961a7b24SJohn Baldwin /*
693961a7b24SJohn Baldwin  * Put all blocked threads on the pending list.  This must be called with
694961a7b24SJohn Baldwin  * the turnstile chain locked.
695961a7b24SJohn Baldwin  */
696961a7b24SJohn Baldwin void
697ef2c0ba7SJohn Baldwin turnstile_broadcast(struct turnstile *ts)
698961a7b24SJohn Baldwin {
699961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
700961a7b24SJohn Baldwin 	struct turnstile *ts1;
701961a7b24SJohn Baldwin 	struct thread *td;
702961a7b24SJohn Baldwin 
703961a7b24SJohn Baldwin 	MPASS(ts != NULL);
704961a7b24SJohn Baldwin 	MPASS(curthread->td_proc->p_magic == P_MAGIC);
705961a7b24SJohn Baldwin 	MPASS(ts->ts_owner == curthread);
706961a7b24SJohn Baldwin 	tc = TC_LOOKUP(ts->ts_lockobj);
707961a7b24SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
708961a7b24SJohn Baldwin 
709961a7b24SJohn Baldwin 	/*
710961a7b24SJohn Baldwin 	 * Transfer the blocked list to the pending list.
711961a7b24SJohn Baldwin 	 */
712961a7b24SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
713961a7b24SJohn Baldwin 	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked, td_lockq);
714961a7b24SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
715961a7b24SJohn Baldwin 
716961a7b24SJohn Baldwin 	/*
717961a7b24SJohn Baldwin 	 * Give a turnstile to each thread.  The last thread gets
718961a7b24SJohn Baldwin 	 * this turnstile.
719961a7b24SJohn Baldwin 	 */
720961a7b24SJohn Baldwin 	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
721961a7b24SJohn Baldwin 		if (LIST_EMPTY(&ts->ts_free)) {
722961a7b24SJohn Baldwin 			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
723961a7b24SJohn Baldwin 			ts1 = ts;
724ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING
725ef0ebfc3SJohn Baldwin 			tc->tc_depth--;
726ef0ebfc3SJohn Baldwin #endif
72736412d79SJohn Baldwin 		} else
728961a7b24SJohn Baldwin 			ts1 = LIST_FIRST(&ts->ts_free);
729da1d503bSJohn Baldwin 		MPASS(ts1 != NULL);
730961a7b24SJohn Baldwin 		LIST_REMOVE(ts1, ts_hash);
731961a7b24SJohn Baldwin 		td->td_turnstile = ts1;
732961a7b24SJohn Baldwin 	}
733961a7b24SJohn Baldwin }
7349ed346baSBosko Milekic 
735961a7b24SJohn Baldwin /*
736961a7b24SJohn Baldwin  * Wakeup all threads on the pending list and adjust the priority of the
737961a7b24SJohn Baldwin  * current thread appropriately.  This must be called with the turnstile
738961a7b24SJohn Baldwin  * chain locked.
739961a7b24SJohn Baldwin  */
740961a7b24SJohn Baldwin void
741961a7b24SJohn Baldwin turnstile_unpend(struct turnstile *ts)
742961a7b24SJohn Baldwin {
743961a7b24SJohn Baldwin 	TAILQ_HEAD( ,thread) pending_threads;
744961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
745961a7b24SJohn Baldwin 	struct thread *td;
746f5c157d9SJohn Baldwin 	u_char cp, pri;
747961a7b24SJohn Baldwin 
748961a7b24SJohn Baldwin 	MPASS(ts != NULL);
749961a7b24SJohn Baldwin 	MPASS(ts->ts_owner == curthread);
750961a7b24SJohn Baldwin 	tc = TC_LOOKUP(ts->ts_lockobj);
751961a7b24SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
752961a7b24SJohn Baldwin 	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
753961a7b24SJohn Baldwin 
754961a7b24SJohn Baldwin 	/*
755961a7b24SJohn Baldwin 	 * Move the list of pending threads out of the turnstile and
756961a7b24SJohn Baldwin 	 * into a local variable.
757961a7b24SJohn Baldwin 	 */
758961a7b24SJohn Baldwin 	TAILQ_INIT(&pending_threads);
759961a7b24SJohn Baldwin 	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
760961a7b24SJohn Baldwin #ifdef INVARIANTS
761961a7b24SJohn Baldwin 	if (TAILQ_EMPTY(&ts->ts_blocked))
762961a7b24SJohn Baldwin 		ts->ts_lockobj = NULL;
763961a7b24SJohn Baldwin #endif
764961a7b24SJohn Baldwin 
765961a7b24SJohn Baldwin 	/*
766961a7b24SJohn Baldwin 	 * Remove the turnstile from this thread's list of contested locks
767961a7b24SJohn Baldwin 	 * since this thread doesn't own it anymore.  New threads will
768961a7b24SJohn Baldwin 	 * not be blocking on the turnstile until it is claimed by a new
769961a7b24SJohn Baldwin 	 * owner.
770961a7b24SJohn Baldwin 	 */
771961a7b24SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
772961a7b24SJohn Baldwin 	ts->ts_owner = NULL;
773961a7b24SJohn Baldwin 	LIST_REMOVE(ts, ts_link);
774961a7b24SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
775b8597527SJohn Baldwin 	critical_enter();
776961a7b24SJohn Baldwin 	mtx_unlock_spin(&tc->tc_lock);
777961a7b24SJohn Baldwin 
778961a7b24SJohn Baldwin 	/*
779961a7b24SJohn Baldwin 	 * Adjust the priority of curthread based on other contested
780961a7b24SJohn Baldwin 	 * locks it owns.  Don't lower the priority below the base
781961a7b24SJohn Baldwin 	 * priority however.
782961a7b24SJohn Baldwin 	 */
783961a7b24SJohn Baldwin 	td = curthread;
784d5a08a60SJake Burkholder 	pri = PRI_MAX;
785961a7b24SJohn Baldwin 	mtx_lock_spin(&sched_lock);
786961a7b24SJohn Baldwin 	mtx_lock_spin(&td_contested_lock);
787961a7b24SJohn Baldwin 	LIST_FOREACH(ts, &td->td_contested, ts_link) {
788961a7b24SJohn Baldwin 		cp = TAILQ_FIRST(&ts->ts_blocked)->td_priority;
78936412d79SJohn Baldwin 		if (cp < pri)
79036412d79SJohn Baldwin 			pri = cp;
79136412d79SJohn Baldwin 	}
792961a7b24SJohn Baldwin 	mtx_unlock_spin(&td_contested_lock);
793f5c157d9SJohn Baldwin 	sched_unlend_prio(td, pri);
7949ed346baSBosko Milekic 
795961a7b24SJohn Baldwin 	/*
796961a7b24SJohn Baldwin 	 * Wake up all the pending threads.  If a thread is not blocked
797961a7b24SJohn Baldwin 	 * on a lock, then it is currently executing on another CPU in
79867ba8678SJohn Baldwin 	 * turnstile_wait() or sitting on a run queue waiting to resume
79967ba8678SJohn Baldwin 	 * in turnstile_wait().  Set a flag to force it to try to acquire
800961a7b24SJohn Baldwin 	 * the lock again instead of blocking.
801961a7b24SJohn Baldwin 	 */
802961a7b24SJohn Baldwin 	while (!TAILQ_EMPTY(&pending_threads)) {
803961a7b24SJohn Baldwin 		td = TAILQ_FIRST(&pending_threads);
804961a7b24SJohn Baldwin 		TAILQ_REMOVE(&pending_threads, td, td_lockq);
805961a7b24SJohn Baldwin 		MPASS(td->td_proc->p_magic == P_MAGIC);
806961a7b24SJohn Baldwin 		if (TD_ON_LOCK(td)) {
807961a7b24SJohn Baldwin 			td->td_blocked = NULL;
808961a7b24SJohn Baldwin 			td->td_lockname = NULL;
809961a7b24SJohn Baldwin 			TD_CLR_LOCK(td);
810961a7b24SJohn Baldwin 			MPASS(TD_CAN_RUN(td));
8112630e4c9SJulian Elischer 			setrunqueue(td, SRQ_BORING);
812961a7b24SJohn Baldwin 		} else {
813961a7b24SJohn Baldwin 			td->td_flags |= TDF_TSNOBLOCK;
81467ba8678SJohn Baldwin 			MPASS(TD_IS_RUNNING(td) || TD_ON_RUNQ(td));
815961a7b24SJohn Baldwin 		}
816961a7b24SJohn Baldwin 	}
817b8597527SJohn Baldwin 	critical_exit();
818e0817317SJulian Elischer 	mtx_unlock_spin(&sched_lock);
8199ed346baSBosko Milekic }
8209ed346baSBosko Milekic 
8219ed346baSBosko Milekic /*
822961a7b24SJohn Baldwin  * Return the first thread in a turnstile.
8239ed346baSBosko Milekic  */
824961a7b24SJohn Baldwin struct thread *
825961a7b24SJohn Baldwin turnstile_head(struct turnstile *ts)
8260cde2e34SJason Evans {
827961a7b24SJohn Baldwin #ifdef INVARIANTS
828961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
8295cb0fbe4SJohn Baldwin 
830961a7b24SJohn Baldwin 	MPASS(ts != NULL);
831961a7b24SJohn Baldwin 	tc = TC_LOOKUP(ts->ts_lockobj);
832961a7b24SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
8330cde2e34SJason Evans #endif
834961a7b24SJohn Baldwin 	return (TAILQ_FIRST(&ts->ts_blocked));
835961a7b24SJohn Baldwin }
8360cde2e34SJason Evans 
8379ed346baSBosko Milekic /*
838961a7b24SJohn Baldwin  * Returns true if a turnstile is empty.
8399ed346baSBosko Milekic  */
840961a7b24SJohn Baldwin int
841961a7b24SJohn Baldwin turnstile_empty(struct turnstile *ts)
84236412d79SJohn Baldwin {
843961a7b24SJohn Baldwin #ifdef INVARIANTS
844961a7b24SJohn Baldwin 	struct turnstile_chain *tc;
84536412d79SJohn Baldwin 
846961a7b24SJohn Baldwin 	MPASS(ts != NULL);
847961a7b24SJohn Baldwin 	tc = TC_LOOKUP(ts->ts_lockobj);
848961a7b24SJohn Baldwin 	mtx_assert(&tc->tc_lock, MA_OWNED);
84936412d79SJohn Baldwin #endif
850961a7b24SJohn Baldwin 	return (TAILQ_EMPTY(&ts->ts_blocked));
851c53c013bSJohn Baldwin }
852