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$");
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>
68234280Smarius#include <sys/kdb.h>
6967352Sjhb#include <sys/kernel.h>
7093609Sdes#include <sys/ktr.h>
7176166Smarkm#include <sys/lock.h>
7274912Sjhb#include <sys/mutex.h>
7365557Sjasone#include <sys/proc.h>
74122514Sjhb#include <sys/queue.h>
75131259Sjhb#include <sys/sched.h>
76235459Srstone#include <sys/sdt.h>
77131259Sjhb#include <sys/sysctl.h>
78122514Sjhb#include <sys/turnstile.h>
7965557Sjasone
80169666Sjeff#include <vm/uma.h>
81169666Sjeff
82154937Sjhb#ifdef DDB
83154937Sjhb#include <ddb/ddb.h>
84161337Sjhb#include <sys/lockmgr.h>
85161337Sjhb#include <sys/sx.h>
86154937Sjhb#endif
87154937Sjhb
8865557Sjasone/*
89122514Sjhb * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
90122514Sjhb * number chosen because the sleep queue's use the same value for the
91122514Sjhb * shift.  Basically, we ignore the lower 8 bits of the address.
92122514Sjhb * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
9371352Sjasone */
94122514Sjhb#define	TC_TABLESIZE	128			/* Must be power of 2. */
95122514Sjhb#define	TC_MASK		(TC_TABLESIZE - 1)
96122514Sjhb#define	TC_SHIFT	8
97122514Sjhb#define	TC_HASH(lock)	(((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
98122514Sjhb#define	TC_LOOKUP(lock)	&turnstile_chains[TC_HASH(lock)]
9971352Sjasone
10071352Sjasone/*
101122514Sjhb * There are three different lists of turnstiles as follows.  The list
102122514Sjhb * connected by ts_link entries is a per-thread list of all the turnstiles
103122514Sjhb * attached to locks that we own.  This is used to fixup our priority when
104122514Sjhb * a lock is released.  The other two lists use the ts_hash entries.  The
105126317Sjhb * first of these two is the turnstile chain list that a turnstile is on
106126317Sjhb * when it is attached to a lock.  The second list to use ts_hash is the
107126317Sjhb * free list hung off of a turnstile that is attached to a lock.
108122514Sjhb *
109154937Sjhb * Each turnstile contains three lists of threads.  The two ts_blocked lists
110154937Sjhb * are linked list of threads blocked on the turnstile's lock.  One list is
111154937Sjhb * for exclusive waiters, and the other is for shared waiters.  The
112126884Sjhb * ts_pending list is a linked list of threads previously awakened by
113122514Sjhb * turnstile_signal() or turnstile_wait() that are waiting to be put on
114122514Sjhb * the run queue.
115122514Sjhb *
116122514Sjhb * Locking key:
117122514Sjhb *  c - turnstile chain lock
118122514Sjhb *  q - td_contested lock
11971352Sjasone */
120122514Sjhbstruct turnstile {
121170295Sjeff	struct mtx ts_lock;			/* Spin lock for self. */
122154937Sjhb	struct threadqueue ts_blocked[2];	/* (c + q) Blocked threads. */
123154937Sjhb	struct threadqueue ts_pending;		/* (c) Pending threads. */
124122514Sjhb	LIST_ENTRY(turnstile) ts_hash;		/* (c) Chain and free list. */
125122514Sjhb	LIST_ENTRY(turnstile) ts_link;		/* (q) Contested locks. */
126122514Sjhb	LIST_HEAD(, turnstile) ts_free;		/* (c) Free turnstiles. */
127122514Sjhb	struct lock_object *ts_lockobj;		/* (c) Lock we reference. */
128122590Sjhb	struct thread *ts_owner;		/* (c + q) Who owns the lock. */
12974912Sjhb};
130122514Sjhb
131122514Sjhbstruct turnstile_chain {
132122514Sjhb	LIST_HEAD(, turnstile) tc_turnstiles;	/* List of turnstiles. */
133122514Sjhb	struct mtx tc_lock;			/* Spin lock for this chain. */
134131259Sjhb#ifdef TURNSTILE_PROFILING
135131259Sjhb	u_int	tc_depth;			/* Length of tc_queues. */
136131259Sjhb	u_int	tc_max_depth;			/* Max length of tc_queues. */
137131259Sjhb#endif
13874912Sjhb};
13971352Sjasone
140131259Sjhb#ifdef TURNSTILE_PROFILING
141131259Sjhbu_int turnstile_max_depth;
142227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0,
143227309Sed    "turnstile profiling");
144227309Sedstatic SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
145131259Sjhb    "turnstile chain stats");
146131259SjhbSYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
147234303Sdavide    &turnstile_max_depth, 0, "maximum depth achieved of a single chain");
148131259Sjhb#endif
149122514Sjhbstatic struct mtx td_contested_lock;
150122514Sjhbstatic struct turnstile_chain turnstile_chains[TC_TABLESIZE];
151169666Sjeffstatic uma_zone_t turnstile_zone;
15293702Sjhb
15393702Sjhb/*
15472200Sbmilekic * Prototypes for non-exported routines.
15572200Sbmilekic */
156122514Sjhbstatic void	init_turnstile0(void *dummy);
157131263Sjhb#ifdef TURNSTILE_PROFILING
158131263Sjhbstatic void	init_turnstile_profiling(void *arg);
159131263Sjhb#endif
160139453Sjhbstatic void	propagate_priority(struct thread *td);
161139453Sjhbstatic int	turnstile_adjust_thread(struct turnstile *ts,
162139453Sjhb		    struct thread *td);
163154937Sjhbstatic struct thread *turnstile_first_waiter(struct turnstile *ts);
164122514Sjhbstatic void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
165169666Sjeff#ifdef INVARIANTS
166169666Sjeffstatic void	turnstile_dtor(void *mem, int size, void *arg);
167169666Sjeff#endif
168169666Sjeffstatic int	turnstile_init(void *mem, int size, int flags);
169170295Sjeffstatic void	turnstile_fini(void *mem, int size);
17067352Sjhb
171235459SrstoneSDT_PROVIDER_DECLARE(sched);
172258622SavgSDT_PROBE_DEFINE(sched, , , sleep);
173258622SavgSDT_PROBE_DEFINE2(sched, , , wakeup, "struct thread *",
174235459Srstone    "struct proc *");
175235459Srstone
176122514Sjhb/*
177122514Sjhb * Walks the chain of turnstiles and their owners to propagate the priority
178122514Sjhb * of the thread being blocked to all the threads holding locks that have to
179122514Sjhb * release their locks before this thread can run again.
180122514Sjhb */
18167352Sjhbstatic void
18283366Sjulianpropagate_priority(struct thread *td)
18367352Sjhb{
184122514Sjhb	struct turnstile *ts;
185122514Sjhb	int pri;
18667352Sjhb
187170295Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
188122514Sjhb	pri = td->td_priority;
189122514Sjhb	ts = td->td_blocked;
190176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
191170295Sjeff	/*
192170295Sjeff	 * Grab a recursive lock on this turnstile chain so it stays locked
193170295Sjeff	 * for the whole operation.  The caller expects us to return with
194170295Sjeff	 * the original lock held.  We only ever lock down the chain so
195170295Sjeff	 * the lock order is constant.
196170295Sjeff	 */
197170295Sjeff	mtx_lock_spin(&ts->ts_lock);
19867352Sjhb	for (;;) {
199122514Sjhb		td = ts->ts_owner;
20067352Sjhb
20183366Sjulian		if (td == NULL) {
20267352Sjhb			/*
203154937Sjhb			 * This might be a read lock with no owner.  There's
204154937Sjhb			 * not much we can do, so just bail.
20567352Sjhb			 */
206170295Sjeff			mtx_unlock_spin(&ts->ts_lock);
20767352Sjhb			return;
20867352Sjhb		}
20972200Sbmilekic
210170295Sjeff		thread_lock_flags(td, MTX_DUPOK);
211170295Sjeff		mtx_unlock_spin(&ts->ts_lock);
21299072Sjulian		MPASS(td->td_proc != NULL);
21383366Sjulian		MPASS(td->td_proc->p_magic == P_MAGIC);
214122514Sjhb
215122514Sjhb		/*
216157275Sjhb		 * If the thread is asleep, then we are probably about
217246923Spjd		 * to deadlock.  To make debugging this easier, show
218246923Spjd		 * backtrace of misbehaving thread and panic to not
219246923Spjd		 * leave the kernel deadlocked.
220122514Sjhb		 */
221157275Sjhb		if (TD_IS_SLEEPING(td)) {
222157275Sjhb			printf(
223157275Sjhb		"Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
224157275Sjhb			    td->td_tid, td->td_proc->p_pid);
225234190Sjhb			kdb_backtrace_thread(td);
226157275Sjhb			panic("sleeping thread");
227157275Sjhb		}
228122514Sjhb
229122514Sjhb		/*
230122514Sjhb		 * If this thread already has higher priority than the
231122514Sjhb		 * thread that is being blocked, we are finished.
232122514Sjhb		 */
233170295Sjeff		if (td->td_priority <= pri) {
234170295Sjeff			thread_unlock(td);
23567352Sjhb			return;
236170295Sjeff		}
23769376Sjhb
23869376Sjhb		/*
239139453Sjhb		 * Bump this thread's priority.
24067352Sjhb		 */
241139453Sjhb		sched_lend_prio(td, pri);
242139453Sjhb
243139453Sjhb		/*
244139453Sjhb		 * If lock holder is actually running or on the run queue
245139453Sjhb		 * then we are done.
246139453Sjhb		 */
247139453Sjhb		if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
248139453Sjhb			MPASS(td->td_blocked == NULL);
249170295Sjeff			thread_unlock(td);
25067352Sjhb			return;
25167352Sjhb		}
25272376Sjake
25373912Sjhb#ifndef SMP
25467352Sjhb		/*
25583366Sjulian		 * For UP, we check to see if td is curthread (this shouldn't
25673912Sjhb		 * ever happen however as it would mean we are in a deadlock.)
25773912Sjhb		 */
25883366Sjulian		KASSERT(td != curthread, ("Deadlock detected"));
25973912Sjhb#endif
26073912Sjhb
26173912Sjhb		/*
262122514Sjhb		 * If we aren't blocked on a lock, we should be.
26367352Sjhb		 */
264104387Sjhb		KASSERT(TD_ON_LOCK(td), (
265139453Sjhb		    "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
266173600Sjulian		    td->td_tid, td->td_name, td->td_state,
267122514Sjhb		    ts->ts_lockobj->lo_name));
26867352Sjhb
26967352Sjhb		/*
270122514Sjhb		 * Pick up the lock that td is blocked on.
27167352Sjhb		 */
272122514Sjhb		ts = td->td_blocked;
273122514Sjhb		MPASS(ts != NULL);
274176078Sjeff		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
275139453Sjhb		/* Resort td on the list if needed. */
276139453Sjhb		if (!turnstile_adjust_thread(ts, td)) {
277170295Sjeff			mtx_unlock_spin(&ts->ts_lock);
278122590Sjhb			return;
279122590Sjhb		}
280170295Sjeff		/* The thread lock is released as ts lock above. */
281139453Sjhb	}
282139453Sjhb}
283122590Sjhb
284139453Sjhb/*
285139453Sjhb * Adjust the thread's position on a turnstile after its priority has been
286139453Sjhb * changed.
287139453Sjhb */
288139453Sjhbstatic int
289139453Sjhbturnstile_adjust_thread(struct turnstile *ts, struct thread *td)
290139453Sjhb{
291139453Sjhb	struct thread *td1, *td2;
292154937Sjhb	int queue;
29372200Sbmilekic
294170295Sjeff	THREAD_LOCK_ASSERT(td, MA_OWNED);
295139453Sjhb	MPASS(TD_ON_LOCK(td));
29667352Sjhb
297139453Sjhb	/*
298139453Sjhb	 * This thread may not be blocked on this turnstile anymore
299139453Sjhb	 * but instead might already be woken up on another CPU
300170295Sjeff	 * that is waiting on the thread lock in turnstile_unpend() to
301139453Sjhb	 * finish waking this thread up.  We can detect this case
302139453Sjhb	 * by checking to see if this thread has been given a
303139453Sjhb	 * turnstile by either turnstile_signal() or
304139453Sjhb	 * turnstile_broadcast().  In this case, treat the thread as
305139453Sjhb	 * if it was already running.
306139453Sjhb	 */
307139453Sjhb	if (td->td_turnstile != NULL)
308139453Sjhb		return (0);
309139453Sjhb
310139453Sjhb	/*
311139453Sjhb	 * Check if the thread needs to be moved on the blocked chain.
312139453Sjhb	 * It needs to be moved if either its priority is lower than
313139453Sjhb	 * the previous thread or higher than the next thread.
314139453Sjhb	 */
315176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
316139453Sjhb	td1 = TAILQ_PREV(td, threadqueue, td_lockq);
317139453Sjhb	td2 = TAILQ_NEXT(td, td_lockq);
318139453Sjhb	if ((td1 != NULL && td->td_priority < td1->td_priority) ||
319139453Sjhb	    (td2 != NULL && td->td_priority > td2->td_priority)) {
320139453Sjhb
32167352Sjhb		/*
32283366Sjulian		 * Remove thread from blocked chain and determine where
323139453Sjhb		 * it should be moved to.
32467352Sjhb		 */
325154937Sjhb		queue = td->td_tsqueue;
326154937Sjhb		MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
327122514Sjhb		mtx_lock_spin(&td_contested_lock);
328154937Sjhb		TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
329154937Sjhb		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
33083366Sjulian			MPASS(td1->td_proc->p_magic == P_MAGIC);
331139453Sjhb			if (td1->td_priority > td->td_priority)
33267352Sjhb				break;
33367352Sjhb		}
33472200Sbmilekic
335139453Sjhb		if (td1 == NULL)
336154937Sjhb			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
337139453Sjhb		else
338139453Sjhb			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
339122514Sjhb		mtx_unlock_spin(&td_contested_lock);
340139453Sjhb		if (td1 == NULL)
341139453Sjhb			CTR3(KTR_LOCK,
342139453Sjhb		    "turnstile_adjust_thread: td %d put at tail on [%p] %s",
343139453Sjhb			    td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
344139453Sjhb		else
345139453Sjhb			CTR4(KTR_LOCK,
346139453Sjhb		    "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
347139453Sjhb			    td->td_tid, td1->td_tid, ts->ts_lockobj,
348139453Sjhb			    ts->ts_lockobj->lo_name);
34967352Sjhb	}
350139453Sjhb	return (1);
35167352Sjhb}
35267352Sjhb
35371352Sjasone/*
354122514Sjhb * Early initialization of turnstiles.  This is not done via a SYSINIT()
355122514Sjhb * since this needs to be initialized very early when mutexes are first
356122514Sjhb * initialized.
35793609Sdes */
358122514Sjhbvoid
359122514Sjhbinit_turnstiles(void)
36093667Sdes{
361122514Sjhb	int i;
36293667Sdes
363122514Sjhb	for (i = 0; i < TC_TABLESIZE; i++) {
364122514Sjhb		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
365122514Sjhb		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
366122514Sjhb		    NULL, MTX_SPIN);
367131263Sjhb	}
368131263Sjhb	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
369154482Sjhb	LIST_INIT(&thread0.td_contested);
370131263Sjhb	thread0.td_turnstile = NULL;
371131263Sjhb}
372131263Sjhb
373131259Sjhb#ifdef TURNSTILE_PROFILING
374131263Sjhbstatic void
375131263Sjhbinit_turnstile_profiling(void *arg)
376131263Sjhb{
377131263Sjhb	struct sysctl_oid *chain_oid;
378131263Sjhb	char chain_name[10];
379131263Sjhb	int i;
380131263Sjhb
381131263Sjhb	for (i = 0; i < TC_TABLESIZE; i++) {
382131259Sjhb		snprintf(chain_name, sizeof(chain_name), "%d", i);
383131259Sjhb		chain_oid = SYSCTL_ADD_NODE(NULL,
384131259Sjhb		    SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
385131259Sjhb		    chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
386131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
387131259Sjhb		    "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
388131259Sjhb		    NULL);
389131259Sjhb		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
390131259Sjhb		    "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
391131259Sjhb		    0, NULL);
392122514Sjhb	}
39393667Sdes}
394131263SjhbSYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
395131263Sjhb    init_turnstile_profiling, NULL);
396131263Sjhb#endif
39793667Sdes
398122514Sjhbstatic void
399122514Sjhbinit_turnstile0(void *dummy)
40093609Sdes{
40193609Sdes
402169666Sjeff	turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
403182879Sjhb	    NULL,
404169666Sjeff#ifdef INVARIANTS
405182879Sjhb	    turnstile_dtor,
406169666Sjeff#else
407182879Sjhb	    NULL,
408169666Sjeff#endif
409182879Sjhb	    turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
410122514Sjhb	thread0.td_turnstile = turnstile_alloc();
41193609Sdes}
412122514SjhbSYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
41393609Sdes
41493609Sdes/*
415139453Sjhb * Update a thread on the turnstile list after it's priority has been changed.
416139453Sjhb * The old priority is passed in as an argument.
417139453Sjhb */
418139453Sjhbvoid
419139453Sjhbturnstile_adjust(struct thread *td, u_char oldpri)
420139453Sjhb{
421139453Sjhb	struct turnstile *ts;
422139453Sjhb
423139453Sjhb	MPASS(TD_ON_LOCK(td));
424139453Sjhb
425139453Sjhb	/*
426139453Sjhb	 * Pick up the lock that td is blocked on.
427139453Sjhb	 */
428139453Sjhb	ts = td->td_blocked;
429139453Sjhb	MPASS(ts != NULL);
430176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
431170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
432139453Sjhb
433139453Sjhb	/* Resort the turnstile on the list. */
434170295Sjeff	if (!turnstile_adjust_thread(ts, td))
435139453Sjhb		return;
436139453Sjhb	/*
437139453Sjhb	 * If our priority was lowered and we are at the head of the
438139453Sjhb	 * turnstile, then propagate our new priority up the chain.
439139453Sjhb	 * Note that we currently don't try to revoke lent priorities
440139453Sjhb	 * when our priority goes up.
441139453Sjhb	 */
442154937Sjhb	MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
443154937Sjhb	    td->td_tsqueue == TS_SHARED_QUEUE);
444154937Sjhb	if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
445154937Sjhb	    td->td_priority < oldpri) {
446139453Sjhb		propagate_priority(td);
447170295Sjeff	}
448139453Sjhb}
449139453Sjhb
450139453Sjhb/*
451122514Sjhb * Set the owner of the lock this turnstile is attached to.
45274900Sjhb */
453122514Sjhbstatic void
454122514Sjhbturnstile_setowner(struct turnstile *ts, struct thread *owner)
45574900Sjhb{
45674900Sjhb
457122514Sjhb	mtx_assert(&td_contested_lock, MA_OWNED);
458154937Sjhb	MPASS(ts->ts_owner == NULL);
459154937Sjhb
460154937Sjhb	/* A shared lock might not have an owner. */
461154937Sjhb	if (owner == NULL)
462154937Sjhb		return;
463154937Sjhb
464122514Sjhb	MPASS(owner->td_proc->p_magic == P_MAGIC);
465122514Sjhb	ts->ts_owner = owner;
466122514Sjhb	LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
46774900Sjhb}
46874900Sjhb
469169666Sjeff#ifdef INVARIANTS
470122514Sjhb/*
471169666Sjeff * UMA zone item deallocator.
472122514Sjhb */
473169666Sjeffstatic void
474169666Sjeffturnstile_dtor(void *mem, int size, void *arg)
47574900Sjhb{
476122514Sjhb	struct turnstile *ts;
47774900Sjhb
478169666Sjeff	ts = mem;
479169666Sjeff	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
480169666Sjeff	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
481169666Sjeff	MPASS(TAILQ_EMPTY(&ts->ts_pending));
482169666Sjeff}
483169666Sjeff#endif
484169666Sjeff
485169666Sjeff/*
486169666Sjeff * UMA zone item initializer.
487169666Sjeff */
488169666Sjeffstatic int
489169666Sjeffturnstile_init(void *mem, int size, int flags)
490169666Sjeff{
491169666Sjeff	struct turnstile *ts;
492169666Sjeff
493169666Sjeff	bzero(mem, size);
494169666Sjeff	ts = mem;
495154937Sjhb	TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
496154937Sjhb	TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
497122514Sjhb	TAILQ_INIT(&ts->ts_pending);
498122514Sjhb	LIST_INIT(&ts->ts_free);
499170295Sjeff	mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN | MTX_RECURSE);
500169666Sjeff	return (0);
50174900Sjhb}
50274900Sjhb
503170295Sjeffstatic void
504170295Sjeffturnstile_fini(void *mem, int size)
505170295Sjeff{
506170295Sjeff	struct turnstile *ts;
507170295Sjeff
508170295Sjeff	ts = mem;
509170295Sjeff	mtx_destroy(&ts->ts_lock);
510170295Sjeff}
511170295Sjeff
512122514Sjhb/*
513169666Sjeff * Get a turnstile for a new thread.
514169666Sjeff */
515169666Sjeffstruct turnstile *
516169666Sjeffturnstile_alloc(void)
517169666Sjeff{
518169666Sjeff
519169666Sjeff	return (uma_zalloc(turnstile_zone, M_WAITOK));
520169666Sjeff}
521169666Sjeff
522169666Sjeff/*
523122514Sjhb * Free a turnstile when a thread is destroyed.
524122514Sjhb */
52574900Sjhbvoid
526122514Sjhbturnstile_free(struct turnstile *ts)
52774900Sjhb{
52874900Sjhb
529169666Sjeff	uma_zfree(turnstile_zone, ts);
53074900Sjhb}
53174900Sjhb
53274900Sjhb/*
533136445Sjhb * Lock the turnstile chain associated with the specified lock.
534136445Sjhb */
535136445Sjhbvoid
536170295Sjeffturnstile_chain_lock(struct lock_object *lock)
537136445Sjhb{
538136445Sjhb	struct turnstile_chain *tc;
539136445Sjhb
540136445Sjhb	tc = TC_LOOKUP(lock);
541136445Sjhb	mtx_lock_spin(&tc->tc_lock);
542136445Sjhb}
543136445Sjhb
544170295Sjeffstruct turnstile *
545170295Sjeffturnstile_trywait(struct lock_object *lock)
546170295Sjeff{
547170295Sjeff	struct turnstile_chain *tc;
548170295Sjeff	struct turnstile *ts;
549170295Sjeff
550170295Sjeff	tc = TC_LOOKUP(lock);
551170295Sjeff	mtx_lock_spin(&tc->tc_lock);
552170295Sjeff	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
553170295Sjeff		if (ts->ts_lockobj == lock) {
554170295Sjeff			mtx_lock_spin(&ts->ts_lock);
555170295Sjeff			return (ts);
556170295Sjeff		}
557170295Sjeff
558170295Sjeff	ts = curthread->td_turnstile;
559170295Sjeff	MPASS(ts != NULL);
560170295Sjeff	mtx_lock_spin(&ts->ts_lock);
561170295Sjeff	KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
562170295Sjeff	ts->ts_lockobj = lock;
563170295Sjeff
564170295Sjeff	return (ts);
565170295Sjeff}
566170295Sjeff
567170295Sjeffvoid
568170295Sjeffturnstile_cancel(struct turnstile *ts)
569170295Sjeff{
570170295Sjeff	struct turnstile_chain *tc;
571170295Sjeff	struct lock_object *lock;
572170295Sjeff
573170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
574170295Sjeff
575170295Sjeff	mtx_unlock_spin(&ts->ts_lock);
576170295Sjeff	lock = ts->ts_lockobj;
577170295Sjeff	if (ts == curthread->td_turnstile)
578170295Sjeff		ts->ts_lockobj = NULL;
579170295Sjeff	tc = TC_LOOKUP(lock);
580170295Sjeff	mtx_unlock_spin(&tc->tc_lock);
581170295Sjeff}
582170295Sjeff
583136445Sjhb/*
584122514Sjhb * Look up the turnstile for a lock in the hash table locking the associated
585136445Sjhb * turnstile chain along the way.  If no turnstile is found in the hash
586136445Sjhb * table, NULL is returned.
58771352Sjasone */
588122514Sjhbstruct turnstile *
589122514Sjhbturnstile_lookup(struct lock_object *lock)
59071352Sjasone{
591122514Sjhb	struct turnstile_chain *tc;
592122514Sjhb	struct turnstile *ts;
59371352Sjasone
594122514Sjhb	tc = TC_LOOKUP(lock);
595136445Sjhb	mtx_assert(&tc->tc_lock, MA_OWNED);
596122514Sjhb	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
597170295Sjeff		if (ts->ts_lockobj == lock) {
598170295Sjeff			mtx_lock_spin(&ts->ts_lock);
599122514Sjhb			return (ts);
600170295Sjeff		}
601122514Sjhb	return (NULL);
60271352Sjasone}
60371352Sjasone
60471352Sjasone/*
605122514Sjhb * Unlock the turnstile chain associated with a given lock.
60671352Sjasone */
60772200Sbmilekicvoid
608170295Sjeffturnstile_chain_unlock(struct lock_object *lock)
60971352Sjasone{
610122514Sjhb	struct turnstile_chain *tc;
61171352Sjasone
612122514Sjhb	tc = TC_LOOKUP(lock);
613122514Sjhb	mtx_unlock_spin(&tc->tc_lock);
61472200Sbmilekic}
61572200Sbmilekic
61672200Sbmilekic/*
617154937Sjhb * Return a pointer to the thread waiting on this turnstile with the
618154937Sjhb * most important priority or NULL if the turnstile has no waiters.
619154937Sjhb */
620154937Sjhbstatic struct thread *
621154937Sjhbturnstile_first_waiter(struct turnstile *ts)
622154937Sjhb{
623154937Sjhb	struct thread *std, *xtd;
624154937Sjhb
625154937Sjhb	std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]);
626154937Sjhb	xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
627154937Sjhb	if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority))
628154937Sjhb		return (std);
629154937Sjhb	return (xtd);
630154937Sjhb}
631154937Sjhb
632154937Sjhb/*
633122514Sjhb * Take ownership of a turnstile and adjust the priority of the new
634122514Sjhb * owner appropriately.
63572200Sbmilekic */
63672200Sbmilekicvoid
637170295Sjeffturnstile_claim(struct turnstile *ts)
63872200Sbmilekic{
639170295Sjeff	struct thread *td, *owner;
640122514Sjhb	struct turnstile_chain *tc;
64172200Sbmilekic
642170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
643170295Sjeff	MPASS(ts != curthread->td_turnstile);
64472200Sbmilekic
645122514Sjhb	owner = curthread;
646122514Sjhb	mtx_lock_spin(&td_contested_lock);
647122514Sjhb	turnstile_setowner(ts, owner);
648122514Sjhb	mtx_unlock_spin(&td_contested_lock);
64972200Sbmilekic
650154937Sjhb	td = turnstile_first_waiter(ts);
651122514Sjhb	MPASS(td != NULL);
652122514Sjhb	MPASS(td->td_proc->p_magic == P_MAGIC);
653176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
65472200Sbmilekic
655122514Sjhb	/*
656122514Sjhb	 * Update the priority of the new owner if needed.
657122514Sjhb	 */
658170295Sjeff	thread_lock(owner);
659122514Sjhb	if (td->td_priority < owner->td_priority)
660139453Sjhb		sched_lend_prio(owner, td->td_priority);
661170295Sjeff	thread_unlock(owner);
662170295Sjeff	tc = TC_LOOKUP(ts->ts_lockobj);
663170295Sjeff	mtx_unlock_spin(&ts->ts_lock);
664170295Sjeff	mtx_unlock_spin(&tc->tc_lock);
66567352Sjhb}
66667352Sjhb
66772200Sbmilekic/*
668136445Sjhb * Block the current thread on the turnstile assicated with 'lock'.  This
669136445Sjhb * function will context switch and not return until this thread has been
670136445Sjhb * woken back up.  This function must be called with the appropriate
671136445Sjhb * turnstile chain locked and will return with it unlocked.
67272200Sbmilekic */
67367352Sjhbvoid
674170295Sjeffturnstile_wait(struct turnstile *ts, struct thread *owner, int queue)
67567352Sjhb{
676122514Sjhb	struct turnstile_chain *tc;
67783366Sjulian	struct thread *td, *td1;
678170295Sjeff	struct lock_object *lock;
67967352Sjhb
68083366Sjulian	td = curthread;
681170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
682154937Sjhb	if (owner)
683154937Sjhb		MPASS(owner->td_proc->p_magic == P_MAGIC);
684154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
68572200Sbmilekic
686136445Sjhb	/*
687136445Sjhb	 * If the lock does not already have a turnstile, use this thread's
688136445Sjhb	 * turnstile.  Otherwise insert the current thread into the
689136445Sjhb	 * turnstile already in use by this lock.
690136445Sjhb	 */
691170295Sjeff	tc = TC_LOOKUP(ts->ts_lockobj);
692218272Sjhb	mtx_assert(&tc->tc_lock, MA_OWNED);
693170295Sjeff	if (ts == td->td_turnstile) {
694131259Sjhb#ifdef TURNSTILE_PROFILING
695131259Sjhb		tc->tc_depth++;
696131259Sjhb		if (tc->tc_depth > tc->tc_max_depth) {
697131259Sjhb			tc->tc_max_depth = tc->tc_depth;
698131259Sjhb			if (tc->tc_max_depth > turnstile_max_depth)
699131259Sjhb				turnstile_max_depth = tc->tc_max_depth;
700131259Sjhb		}
701131259Sjhb#endif
702122514Sjhb		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
703122514Sjhb		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
704122514Sjhb		    ("thread's turnstile has pending threads"));
705154937Sjhb		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
706154937Sjhb		    ("thread's turnstile has exclusive waiters"));
707154937Sjhb		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
708154937Sjhb		    ("thread's turnstile has shared waiters"));
709122514Sjhb		KASSERT(LIST_EMPTY(&ts->ts_free),
710122514Sjhb		    ("thread's turnstile has a non-empty free list"));
711170295Sjeff		MPASS(ts->ts_lockobj != NULL);
712122514Sjhb		mtx_lock_spin(&td_contested_lock);
713154937Sjhb		TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
714122514Sjhb		turnstile_setowner(ts, owner);
715122514Sjhb		mtx_unlock_spin(&td_contested_lock);
716122514Sjhb	} else {
717154937Sjhb		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
718122514Sjhb			if (td1->td_priority > td->td_priority)
719122514Sjhb				break;
720122514Sjhb		mtx_lock_spin(&td_contested_lock);
721122514Sjhb		if (td1 != NULL)
722122514Sjhb			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
723122514Sjhb		else
724154937Sjhb			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
725154937Sjhb		MPASS(owner == ts->ts_owner);
726122514Sjhb		mtx_unlock_spin(&td_contested_lock);
727122514Sjhb		MPASS(td->td_turnstile != NULL);
728122514Sjhb		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
72972200Sbmilekic	}
730170295Sjeff	thread_lock(td);
731170295Sjeff	thread_lock_set(td, &ts->ts_lock);
732122514Sjhb	td->td_turnstile = NULL;
73372200Sbmilekic
734122514Sjhb	/* Save who we are blocked on and switch. */
735170295Sjeff	lock = ts->ts_lockobj;
736154937Sjhb	td->td_tsqueue = queue;
737122514Sjhb	td->td_blocked = ts;
738122514Sjhb	td->td_lockname = lock->lo_name;
739201879Sattilio	td->td_blktick = ticks;
740122514Sjhb	TD_SET_LOCK(td);
741170295Sjeff	mtx_unlock_spin(&tc->tc_lock);
742122514Sjhb	propagate_priority(td);
74372200Sbmilekic
744122514Sjhb	if (LOCK_LOG_TEST(lock, 0))
745139453Sjhb		CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
746139453Sjhb		    td->td_tid, lock, lock->lo_name);
74772200Sbmilekic
748235459Srstone	SDT_PROBE0(sched, , , sleep);
749235459Srstone
750176078Sjeff	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
751178272Sjeff	mi_switch(SW_VOL | SWT_TURNSTILE, NULL);
75272200Sbmilekic
753122514Sjhb	if (LOCK_LOG_TEST(lock, 0))
754139453Sjhb		CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
755139453Sjhb		    __func__, td->td_tid, lock, lock->lo_name);
756170295Sjeff	thread_unlock(td);
75767352Sjhb}
75867352Sjhb
75972200Sbmilekic/*
760122514Sjhb * Pick the highest priority thread on this turnstile and put it on the
761122514Sjhb * pending list.  This must be called with the turnstile chain locked.
76272200Sbmilekic */
763122514Sjhbint
764154937Sjhbturnstile_signal(struct turnstile *ts, int queue)
76571352Sjasone{
766122514Sjhb	struct turnstile_chain *tc;
767122514Sjhb	struct thread *td;
768122514Sjhb	int empty;
76980748Sjhb
770122514Sjhb	MPASS(ts != NULL);
771170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
772122514Sjhb	MPASS(curthread->td_proc->p_magic == P_MAGIC);
773176017Sjeff	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
774154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
77571352Sjasone
776122514Sjhb	/*
777122514Sjhb	 * Pick the highest priority thread blocked on this lock and
778122514Sjhb	 * move it to the pending list.
779122514Sjhb	 */
780154937Sjhb	td = TAILQ_FIRST(&ts->ts_blocked[queue]);
781122514Sjhb	MPASS(td->td_proc->p_magic == P_MAGIC);
782122514Sjhb	mtx_lock_spin(&td_contested_lock);
783154937Sjhb	TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
784122514Sjhb	mtx_unlock_spin(&td_contested_lock);
785122514Sjhb	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
78667352Sjhb
78782304Sbmilekic	/*
788122514Sjhb	 * If the turnstile is now empty, remove it from its chain and
789122514Sjhb	 * give it to the about-to-be-woken thread.  Otherwise take a
790122514Sjhb	 * turnstile from the free list and give it to the thread.
791105782Sdes	 */
792154937Sjhb	empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
793154937Sjhb	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
794131259Sjhb	if (empty) {
795170295Sjeff		tc = TC_LOOKUP(ts->ts_lockobj);
796170295Sjeff		mtx_assert(&tc->tc_lock, MA_OWNED);
797122514Sjhb		MPASS(LIST_EMPTY(&ts->ts_free));
798131259Sjhb#ifdef TURNSTILE_PROFILING
799131259Sjhb		tc->tc_depth--;
800131259Sjhb#endif
801131259Sjhb	} else
802122514Sjhb		ts = LIST_FIRST(&ts->ts_free);
803123363Sjhb	MPASS(ts != NULL);
804122514Sjhb	LIST_REMOVE(ts, ts_hash);
805122514Sjhb	td->td_turnstile = ts;
806122514Sjhb
807122514Sjhb	return (empty);
80867352Sjhb}
809122514Sjhb
81072200Sbmilekic/*
811122514Sjhb * Put all blocked threads on the pending list.  This must be called with
812122514Sjhb * the turnstile chain locked.
81393672Sarr */
81493672Sarrvoid
815154937Sjhbturnstile_broadcast(struct turnstile *ts, int queue)
81693672Sarr{
817122514Sjhb	struct turnstile_chain *tc;
818122514Sjhb	struct turnstile *ts1;
819122514Sjhb	struct thread *td;
82093672Sarr
821122514Sjhb	MPASS(ts != NULL);
822170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
823122514Sjhb	MPASS(curthread->td_proc->p_magic == P_MAGIC);
824176017Sjeff	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
825170295Sjeff	/*
826170295Sjeff	 * We must have the chain locked so that we can remove the empty
827170295Sjeff	 * turnstile from the hash queue.
828170295Sjeff	 */
829122514Sjhb	tc = TC_LOOKUP(ts->ts_lockobj);
830122514Sjhb	mtx_assert(&tc->tc_lock, MA_OWNED);
831154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
832122514Sjhb
833122514Sjhb	/*
834122514Sjhb	 * Transfer the blocked list to the pending list.
835122514Sjhb	 */
836122514Sjhb	mtx_lock_spin(&td_contested_lock);
837154937Sjhb	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
838122514Sjhb	mtx_unlock_spin(&td_contested_lock);
839122514Sjhb
840122514Sjhb	/*
841122514Sjhb	 * Give a turnstile to each thread.  The last thread gets
842154937Sjhb	 * this turnstile if the turnstile is empty.
843122514Sjhb	 */
844122514Sjhb	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
845122514Sjhb		if (LIST_EMPTY(&ts->ts_free)) {
846122514Sjhb			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
847122514Sjhb			ts1 = ts;
848131259Sjhb#ifdef TURNSTILE_PROFILING
849131259Sjhb			tc->tc_depth--;
850131259Sjhb#endif
851122514Sjhb		} else
852122514Sjhb			ts1 = LIST_FIRST(&ts->ts_free);
853123363Sjhb		MPASS(ts1 != NULL);
854122514Sjhb		LIST_REMOVE(ts1, ts_hash);
855122514Sjhb		td->td_turnstile = ts1;
856122514Sjhb	}
85793672Sarr}
858122590Sjhb
85993672Sarr/*
860122514Sjhb * Wakeup all threads on the pending list and adjust the priority of the
861122514Sjhb * current thread appropriately.  This must be called with the turnstile
862122514Sjhb * chain locked.
863105782Sdes */
86467352Sjhbvoid
865154937Sjhbturnstile_unpend(struct turnstile *ts, int owner_type)
86667352Sjhb{
867122514Sjhb	TAILQ_HEAD( ,thread) pending_threads;
868170295Sjeff	struct turnstile *nts;
869122514Sjhb	struct thread *td;
870139453Sjhb	u_char cp, pri;
87172200Sbmilekic
872122514Sjhb	MPASS(ts != NULL);
873170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
874176017Sjeff	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
875122514Sjhb	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
87672200Sbmilekic
877122514Sjhb	/*
878122514Sjhb	 * Move the list of pending threads out of the turnstile and
879122514Sjhb	 * into a local variable.
880122514Sjhb	 */
881122514Sjhb	TAILQ_INIT(&pending_threads);
882122514Sjhb	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
883122514Sjhb#ifdef INVARIANTS
884154937Sjhb	if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
885154937Sjhb	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
886122514Sjhb		ts->ts_lockobj = NULL;
88769429Sjhb#endif
888122514Sjhb	/*
889170295Sjeff	 * Adjust the priority of curthread based on other contested
890170295Sjeff	 * locks it owns.  Don't lower the priority below the base
891170295Sjeff	 * priority however.
892170295Sjeff	 */
893170295Sjeff	td = curthread;
894170295Sjeff	pri = PRI_MAX;
895170295Sjeff	thread_lock(td);
896170295Sjeff	mtx_lock_spin(&td_contested_lock);
897170295Sjeff	/*
898122514Sjhb	 * Remove the turnstile from this thread's list of contested locks
899122514Sjhb	 * since this thread doesn't own it anymore.  New threads will
900122514Sjhb	 * not be blocking on the turnstile until it is claimed by a new
901154937Sjhb	 * owner.  There might not be a current owner if this is a shared
902154937Sjhb	 * lock.
903122514Sjhb	 */
904154937Sjhb	if (ts->ts_owner != NULL) {
905154937Sjhb		ts->ts_owner = NULL;
906154937Sjhb		LIST_REMOVE(ts, ts_link);
907154937Sjhb	}
908170295Sjeff	LIST_FOREACH(nts, &td->td_contested, ts_link) {
909170295Sjeff		cp = turnstile_first_waiter(nts)->td_priority;
910122514Sjhb		if (cp < pri)
911122514Sjhb			pri = cp;
912122514Sjhb	}
913122514Sjhb	mtx_unlock_spin(&td_contested_lock);
914139453Sjhb	sched_unlend_prio(td, pri);
915170295Sjeff	thread_unlock(td);
916122514Sjhb	/*
917122514Sjhb	 * Wake up all the pending threads.  If a thread is not blocked
918122514Sjhb	 * on a lock, then it is currently executing on another CPU in
919123364Sjhb	 * turnstile_wait() or sitting on a run queue waiting to resume
920123364Sjhb	 * in turnstile_wait().  Set a flag to force it to try to acquire
921122514Sjhb	 * the lock again instead of blocking.
922122514Sjhb	 */
923122514Sjhb	while (!TAILQ_EMPTY(&pending_threads)) {
924122514Sjhb		td = TAILQ_FIRST(&pending_threads);
925122514Sjhb		TAILQ_REMOVE(&pending_threads, td, td_lockq);
926235459Srstone		SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
927170295Sjeff		thread_lock(td);
928176078Sjeff		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
929122514Sjhb		MPASS(td->td_proc->p_magic == P_MAGIC);
930170295Sjeff		MPASS(TD_ON_LOCK(td));
931170295Sjeff		TD_CLR_LOCK(td);
932170295Sjeff		MPASS(TD_CAN_RUN(td));
933170295Sjeff		td->td_blocked = NULL;
934170295Sjeff		td->td_lockname = NULL;
935201879Sattilio		td->td_blktick = 0;
936154937Sjhb#ifdef INVARIANTS
937170295Sjeff		td->td_tsqueue = 0xff;
938154937Sjhb#endif
939170295Sjeff		sched_add(td, SRQ_BORING);
940170295Sjeff		thread_unlock(td);
941122514Sjhb	}
942170295Sjeff	mtx_unlock_spin(&ts->ts_lock);
94367352Sjhb}
94467352Sjhb
94572200Sbmilekic/*
946157844Sjhb * Give up ownership of a turnstile.  This must be called with the
947157844Sjhb * turnstile chain locked.
948157844Sjhb */
949157844Sjhbvoid
950157844Sjhbturnstile_disown(struct turnstile *ts)
951157844Sjhb{
952157844Sjhb	struct thread *td;
953157844Sjhb	u_char cp, pri;
954157844Sjhb
955157844Sjhb	MPASS(ts != NULL);
956170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
957157844Sjhb	MPASS(ts->ts_owner == curthread);
958157844Sjhb	MPASS(TAILQ_EMPTY(&ts->ts_pending));
959157844Sjhb	MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
960157844Sjhb	    !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
961157844Sjhb
962157844Sjhb	/*
963157844Sjhb	 * Remove the turnstile from this thread's list of contested locks
964157844Sjhb	 * since this thread doesn't own it anymore.  New threads will
965157844Sjhb	 * not be blocking on the turnstile until it is claimed by a new
966157844Sjhb	 * owner.
967157844Sjhb	 */
968157844Sjhb	mtx_lock_spin(&td_contested_lock);
969157844Sjhb	ts->ts_owner = NULL;
970157844Sjhb	LIST_REMOVE(ts, ts_link);
971157844Sjhb	mtx_unlock_spin(&td_contested_lock);
972157844Sjhb
973157844Sjhb	/*
974157844Sjhb	 * Adjust the priority of curthread based on other contested
975157844Sjhb	 * locks it owns.  Don't lower the priority below the base
976157844Sjhb	 * priority however.
977157844Sjhb	 */
978157844Sjhb	td = curthread;
979157844Sjhb	pri = PRI_MAX;
980170295Sjeff	thread_lock(td);
981170295Sjeff	mtx_unlock_spin(&ts->ts_lock);
982157844Sjhb	mtx_lock_spin(&td_contested_lock);
983157844Sjhb	LIST_FOREACH(ts, &td->td_contested, ts_link) {
984157844Sjhb		cp = turnstile_first_waiter(ts)->td_priority;
985157844Sjhb		if (cp < pri)
986157844Sjhb			pri = cp;
987157844Sjhb	}
988157844Sjhb	mtx_unlock_spin(&td_contested_lock);
989157844Sjhb	sched_unlend_prio(td, pri);
990170295Sjeff	thread_unlock(td);
991157844Sjhb}
992157844Sjhb
993157844Sjhb/*
994122514Sjhb * Return the first thread in a turnstile.
99572200Sbmilekic */
996122514Sjhbstruct thread *
997154937Sjhbturnstile_head(struct turnstile *ts, int queue)
99867352Sjhb{
999122514Sjhb#ifdef INVARIANTS
100067352Sjhb
1001122514Sjhb	MPASS(ts != NULL);
1002154937Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1003170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
1004122514Sjhb#endif
1005154937Sjhb	return (TAILQ_FIRST(&ts->ts_blocked[queue]));
100671320Sjasone}
1007154937Sjhb
1008157844Sjhb/*
1009157844Sjhb * Returns true if a sub-queue of a turnstile is empty.
1010157844Sjhb */
1011157844Sjhbint
1012157844Sjhbturnstile_empty(struct turnstile *ts, int queue)
1013157844Sjhb{
1014157844Sjhb#ifdef INVARIANTS
1015157844Sjhb
1016157844Sjhb	MPASS(ts != NULL);
1017157844Sjhb	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1018170295Sjeff	mtx_assert(&ts->ts_lock, MA_OWNED);
1019157844Sjhb#endif
1020157844Sjhb	return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
1021157844Sjhb}
1022157844Sjhb
1023154937Sjhb#ifdef DDB
1024154937Sjhbstatic void
1025154937Sjhbprint_thread(struct thread *td, const char *prefix)
1026154937Sjhb{
1027154937Sjhb
1028154937Sjhb	db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid,
1029295488Skib	    td->td_proc->p_pid, td->td_name);
1030154937Sjhb}
1031154937Sjhb
1032154937Sjhbstatic void
1033154937Sjhbprint_queue(struct threadqueue *queue, const char *header, const char *prefix)
1034154937Sjhb{
1035154937Sjhb	struct thread *td;
1036154937Sjhb
1037154937Sjhb	db_printf("%s:\n", header);
1038154937Sjhb	if (TAILQ_EMPTY(queue)) {
1039154937Sjhb		db_printf("%sempty\n", prefix);
1040154937Sjhb		return;
1041154937Sjhb	}
1042154937Sjhb	TAILQ_FOREACH(td, queue, td_lockq) {
1043154937Sjhb		print_thread(td, prefix);
1044154937Sjhb	}
1045154937Sjhb}
1046154937Sjhb
1047154937SjhbDB_SHOW_COMMAND(turnstile, db_show_turnstile)
1048154937Sjhb{
1049154937Sjhb	struct turnstile_chain *tc;
1050154937Sjhb	struct turnstile *ts;
1051154937Sjhb	struct lock_object *lock;
1052154937Sjhb	int i;
1053154937Sjhb
1054154937Sjhb	if (!have_addr)
1055154937Sjhb		return;
1056154937Sjhb
1057154937Sjhb	/*
1058154937Sjhb	 * First, see if there is an active turnstile for the lock indicated
1059154937Sjhb	 * by the address.
1060154937Sjhb	 */
1061154937Sjhb	lock = (struct lock_object *)addr;
1062154937Sjhb	tc = TC_LOOKUP(lock);
1063154937Sjhb	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1064154937Sjhb		if (ts->ts_lockobj == lock)
1065154937Sjhb			goto found;
1066154937Sjhb
1067154937Sjhb	/*
1068154937Sjhb	 * Second, see if there is an active turnstile at the address
1069154937Sjhb	 * indicated.
1070154937Sjhb	 */
1071154937Sjhb	for (i = 0; i < TC_TABLESIZE; i++)
1072154937Sjhb		LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) {
1073154937Sjhb			if (ts == (struct turnstile *)addr)
1074154937Sjhb				goto found;
1075154937Sjhb		}
1076154937Sjhb
1077154937Sjhb	db_printf("Unable to locate a turnstile via %p\n", (void *)addr);
1078154937Sjhb	return;
1079154937Sjhbfound:
1080154937Sjhb	lock = ts->ts_lockobj;
1081154937Sjhb	db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name,
1082154937Sjhb	    lock->lo_name);
1083154937Sjhb	if (ts->ts_owner)
1084154937Sjhb		print_thread(ts->ts_owner, "Lock Owner: ");
1085154937Sjhb	else
1086154937Sjhb		db_printf("Lock Owner: none\n");
1087154937Sjhb	print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t");
1088154937Sjhb	print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters",
1089154937Sjhb	    "\t");
1090154937Sjhb	print_queue(&ts->ts_pending, "Pending Threads", "\t");
1091154937Sjhb
1092154937Sjhb}
1093158031Sjhb
1094161324Sjhb/*
1095161324Sjhb * Show all the threads a particular thread is waiting on based on
1096161324Sjhb * non-sleepable and non-spin locks.
1097161324Sjhb */
1098158031Sjhbstatic void
1099161324Sjhbprint_lockchain(struct thread *td, const char *prefix)
1100158031Sjhb{
1101158031Sjhb	struct lock_object *lock;
1102158031Sjhb	struct lock_class *class;
1103158031Sjhb	struct turnstile *ts;
1104158031Sjhb
1105158031Sjhb	/*
1106158031Sjhb	 * Follow the chain.  We keep walking as long as the thread is
1107158031Sjhb	 * blocked on a turnstile that has an owner.
1108158031Sjhb	 */
1109160313Sjhb	while (!db_pager_quit) {
1110158031Sjhb		db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1111295488Skib		    td->td_proc->p_pid, td->td_name);
1112158031Sjhb		switch (td->td_state) {
1113158031Sjhb		case TDS_INACTIVE:
1114158031Sjhb			db_printf("is inactive\n");
1115158031Sjhb			return;
1116158031Sjhb		case TDS_CAN_RUN:
1117158031Sjhb			db_printf("can run\n");
1118158031Sjhb			return;
1119158031Sjhb		case TDS_RUNQ:
1120158031Sjhb			db_printf("is on a run queue\n");
1121158031Sjhb			return;
1122158031Sjhb		case TDS_RUNNING:
1123158031Sjhb			db_printf("running on CPU %d\n", td->td_oncpu);
1124158031Sjhb			return;
1125158031Sjhb		case TDS_INHIBITED:
1126158031Sjhb			if (TD_ON_LOCK(td)) {
1127158031Sjhb				ts = td->td_blocked;
1128158031Sjhb				lock = ts->ts_lockobj;
1129158031Sjhb				class = LOCK_CLASS(lock);
1130158031Sjhb				db_printf("blocked on lock %p (%s) \"%s\"\n",
1131158031Sjhb				    lock, class->lc_name, lock->lo_name);
1132158031Sjhb				if (ts->ts_owner == NULL)
1133158031Sjhb					return;
1134158031Sjhb				td = ts->ts_owner;
1135158031Sjhb				break;
1136158031Sjhb			}
1137158031Sjhb			db_printf("inhibited\n");
1138158031Sjhb			return;
1139158031Sjhb		default:
1140158031Sjhb			db_printf("??? (%#x)\n", td->td_state);
1141158031Sjhb			return;
1142158031Sjhb		}
1143158031Sjhb	}
1144158031Sjhb}
1145158031Sjhb
1146161324SjhbDB_SHOW_COMMAND(lockchain, db_show_lockchain)
1147158031Sjhb{
1148158031Sjhb	struct thread *td;
1149158031Sjhb
1150158031Sjhb	/* Figure out which thread to start with. */
1151158031Sjhb	if (have_addr)
1152283248Spfg		td = db_lookup_thread(addr, true);
1153158031Sjhb	else
1154158031Sjhb		td = kdb_thread;
1155158031Sjhb
1156161324Sjhb	print_lockchain(td, "");
1157158031Sjhb}
1158158031Sjhb
1159183054SsamDB_SHOW_ALL_COMMAND(chains, db_show_allchains)
1160158031Sjhb{
1161158031Sjhb	struct thread *td;
1162158031Sjhb	struct proc *p;
1163158031Sjhb	int i;
1164158031Sjhb
1165158031Sjhb	i = 1;
1166166073Sdelphij	FOREACH_PROC_IN_SYSTEM(p) {
1167158031Sjhb		FOREACH_THREAD_IN_PROC(p, td) {
1168158031Sjhb			if (TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested)) {
1169158031Sjhb				db_printf("chain %d:\n", i++);
1170161324Sjhb				print_lockchain(td, " ");
1171158031Sjhb			}
1172160313Sjhb			if (db_pager_quit)
1173160313Sjhb				return;
1174158031Sjhb		}
1175158031Sjhb	}
1176158031Sjhb}
1177183054SsamDB_SHOW_ALIAS(allchains, db_show_allchains)
1178158031Sjhb
1179161337Sjhb/*
1180161337Sjhb * Show all the threads a particular thread is waiting on based on
1181161337Sjhb * sleepable locks.
1182161337Sjhb */
1183161337Sjhbstatic void
1184161337Sjhbprint_sleepchain(struct thread *td, const char *prefix)
1185161337Sjhb{
1186161337Sjhb	struct thread *owner;
1187161337Sjhb
1188161337Sjhb	/*
1189161337Sjhb	 * Follow the chain.  We keep walking as long as the thread is
1190161337Sjhb	 * blocked on a sleep lock that has an owner.
1191161337Sjhb	 */
1192161337Sjhb	while (!db_pager_quit) {
1193161337Sjhb		db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1194295488Skib		    td->td_proc->p_pid, td->td_name);
1195161337Sjhb		switch (td->td_state) {
1196161337Sjhb		case TDS_INACTIVE:
1197161337Sjhb			db_printf("is inactive\n");
1198161337Sjhb			return;
1199161337Sjhb		case TDS_CAN_RUN:
1200161337Sjhb			db_printf("can run\n");
1201161337Sjhb			return;
1202161337Sjhb		case TDS_RUNQ:
1203161337Sjhb			db_printf("is on a run queue\n");
1204161337Sjhb			return;
1205161337Sjhb		case TDS_RUNNING:
1206161337Sjhb			db_printf("running on CPU %d\n", td->td_oncpu);
1207161337Sjhb			return;
1208161337Sjhb		case TDS_INHIBITED:
1209161337Sjhb			if (TD_ON_SLEEPQ(td)) {
1210161337Sjhb				if (lockmgr_chain(td, &owner) ||
1211161337Sjhb				    sx_chain(td, &owner)) {
1212161337Sjhb					if (owner == NULL)
1213161337Sjhb						return;
1214161337Sjhb					td = owner;
1215161337Sjhb					break;
1216161337Sjhb				}
1217161337Sjhb				db_printf("sleeping on %p \"%s\"\n",
1218161337Sjhb				    td->td_wchan, td->td_wmesg);
1219161337Sjhb				return;
1220161337Sjhb			}
1221161337Sjhb			db_printf("inhibited\n");
1222161337Sjhb			return;
1223161337Sjhb		default:
1224161337Sjhb			db_printf("??? (%#x)\n", td->td_state);
1225161337Sjhb			return;
1226161337Sjhb		}
1227161337Sjhb	}
1228161337Sjhb}
1229161337Sjhb
1230161337SjhbDB_SHOW_COMMAND(sleepchain, db_show_sleepchain)
1231161337Sjhb{
1232161337Sjhb	struct thread *td;
1233161337Sjhb
1234161337Sjhb	/* Figure out which thread to start with. */
1235161337Sjhb	if (have_addr)
1236283248Spfg		td = db_lookup_thread(addr, true);
1237161337Sjhb	else
1238161337Sjhb		td = kdb_thread;
1239161337Sjhb
1240161337Sjhb	print_sleepchain(td, "");
1241161337Sjhb}
1242161337Sjhb
1243158031Sjhbstatic void	print_waiters(struct turnstile *ts, int indent);
1244158031Sjhb
1245158031Sjhbstatic void
1246158031Sjhbprint_waiter(struct thread *td, int indent)
1247158031Sjhb{
1248158031Sjhb	struct turnstile *ts;
1249158031Sjhb	int i;
1250158031Sjhb
1251160313Sjhb	if (db_pager_quit)
1252160313Sjhb		return;
1253158031Sjhb	for (i = 0; i < indent; i++)
1254158031Sjhb		db_printf(" ");
1255158031Sjhb	print_thread(td, "thread ");
1256158031Sjhb	LIST_FOREACH(ts, &td->td_contested, ts_link)
1257158031Sjhb		print_waiters(ts, indent + 1);
1258158031Sjhb}
1259158031Sjhb
1260158031Sjhbstatic void
1261158031Sjhbprint_waiters(struct turnstile *ts, int indent)
1262158031Sjhb{
1263158031Sjhb	struct lock_object *lock;
1264158031Sjhb	struct lock_class *class;
1265158031Sjhb	struct thread *td;
1266158031Sjhb	int i;
1267158031Sjhb
1268160313Sjhb	if (db_pager_quit)
1269160313Sjhb		return;
1270158031Sjhb	lock = ts->ts_lockobj;
1271158031Sjhb	class = LOCK_CLASS(lock);
1272158031Sjhb	for (i = 0; i < indent; i++)
1273158031Sjhb		db_printf(" ");
1274158031Sjhb	db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
1275158031Sjhb	TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
1276158031Sjhb		print_waiter(td, indent + 1);
1277158031Sjhb	TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
1278158031Sjhb		print_waiter(td, indent + 1);
1279158031Sjhb	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
1280158031Sjhb		print_waiter(td, indent + 1);
1281158031Sjhb}
1282158031Sjhb
1283161324SjhbDB_SHOW_COMMAND(locktree, db_show_locktree)
1284158031Sjhb{
1285158031Sjhb	struct lock_object *lock;
1286158031Sjhb	struct lock_class *class;
1287158031Sjhb	struct turnstile_chain *tc;
1288158031Sjhb	struct turnstile *ts;
1289158031Sjhb
1290158031Sjhb	if (!have_addr)
1291158031Sjhb		return;
1292158031Sjhb	lock = (struct lock_object *)addr;
1293158031Sjhb	tc = TC_LOOKUP(lock);
1294158031Sjhb	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1295158031Sjhb		if (ts->ts_lockobj == lock)
1296158031Sjhb			break;
1297158031Sjhb	if (ts == NULL) {
1298158031Sjhb		class = LOCK_CLASS(lock);
1299158031Sjhb		db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
1300158031Sjhb		    lock->lo_name);
1301158031Sjhb	} else
1302158031Sjhb		print_waiters(ts, 0);
1303158031Sjhb}
1304154937Sjhb#endif
1305