xref: /freebsd/sys/kern/subr_turnstile.c (revision 685dc743)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Berkeley Software Design Inc's name may not be used to endorse or
15  *    promote products derived from this software without specific prior
16  *    written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
31  *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
32  */
33 
34 /*
35  * Implementation of turnstiles used to hold queue of threads blocked on
36  * non-sleepable locks.  Sleepable locks use condition variables to
37  * implement their queues.  Turnstiles differ from a sleep queue in that
38  * turnstile queue's are assigned to a lock held by an owning thread.  Thus,
39  * when one thread is enqueued onto a turnstile, it can lend its priority
40  * to the owning thread.
41  *
42  * We wish to avoid bloating locks with an embedded turnstile and we do not
43  * want to use back-pointers in the locks for the same reason.  Thus, we
44  * use a similar approach to that of Solaris 7 as described in Solaris
45  * Internals by Jim Mauro and Richard McDougall.  Turnstiles are looked up
46  * in a hash table based on the address of the lock.  Each entry in the
47  * hash table is a linked-lists of turnstiles and is called a turnstile
48  * chain.  Each chain contains a spin mutex that protects all of the
49  * turnstiles in the chain.
50  *
51  * Each time a thread is created, a turnstile is allocated from a UMA zone
52  * and attached to that thread.  When a thread blocks on a lock, if it is the
53  * first thread to block, it lends its turnstile to the lock.  If the lock
54  * already has a turnstile, then it gives its turnstile to the lock's
55  * turnstile's free list.  When a thread is woken up, it takes a turnstile from
56  * the free list if there are any other waiters.  If it is the only thread
57  * blocked on the lock, then it reclaims the turnstile associated with the lock
58  * and removes it from the hash table.
59  */
60 
61 #include <sys/cdefs.h>
62 #include "opt_ddb.h"
63 #include "opt_turnstile_profiling.h"
64 #include "opt_sched.h"
65 
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kdb.h>
69 #include <sys/kernel.h>
70 #include <sys/ktr.h>
71 #include <sys/lock.h>
72 #include <sys/mutex.h>
73 #include <sys/proc.h>
74 #include <sys/queue.h>
75 #include <sys/sched.h>
76 #include <sys/sdt.h>
77 #include <sys/sysctl.h>
78 #include <sys/turnstile.h>
79 
80 #include <vm/uma.h>
81 
82 #ifdef DDB
83 #include <ddb/ddb.h>
84 #include <sys/lockmgr.h>
85 #include <sys/sx.h>
86 #endif
87 
88 /*
89  * Constants for the hash table of turnstile chains.  TC_SHIFT is a magic
90  * number chosen because the sleep queue's use the same value for the
91  * shift.  Basically, we ignore the lower 8 bits of the address.
92  * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
93  */
94 #define	TC_TABLESIZE	128			/* Must be power of 2. */
95 #define	TC_MASK		(TC_TABLESIZE - 1)
96 #define	TC_SHIFT	8
97 #define	TC_HASH(lock)	(((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
98 #define	TC_LOOKUP(lock)	&turnstile_chains[TC_HASH(lock)]
99 
100 /*
101  * There are three different lists of turnstiles as follows.  The list
102  * connected by ts_link entries is a per-thread list of all the turnstiles
103  * attached to locks that we own.  This is used to fixup our priority when
104  * a lock is released.  The other two lists use the ts_hash entries.  The
105  * first of these two is the turnstile chain list that a turnstile is on
106  * when it is attached to a lock.  The second list to use ts_hash is the
107  * free list hung off of a turnstile that is attached to a lock.
108  *
109  * Each turnstile contains three lists of threads.  The two ts_blocked lists
110  * are linked list of threads blocked on the turnstile's lock.  One list is
111  * for exclusive waiters, and the other is for shared waiters.  The
112  * ts_pending list is a linked list of threads previously awakened by
113  * turnstile_signal() or turnstile_wait() that are waiting to be put on
114  * the run queue.
115  *
116  * Locking key:
117  *  c - turnstile chain lock
118  *  q - td_contested lock
119  */
120 struct turnstile {
121 	struct mtx ts_lock;			/* Spin lock for self. */
122 	struct threadqueue ts_blocked[2];	/* (c + q) Blocked threads. */
123 	struct threadqueue ts_pending;		/* (c) Pending threads. */
124 	LIST_ENTRY(turnstile) ts_hash;		/* (c) Chain and free list. */
125 	LIST_ENTRY(turnstile) ts_link;		/* (q) Contested locks. */
126 	LIST_HEAD(, turnstile) ts_free;		/* (c) Free turnstiles. */
127 	struct lock_object *ts_lockobj;		/* (c) Lock we reference. */
128 	struct thread *ts_owner;		/* (c + q) Who owns the lock. */
129 };
130 
131 struct turnstile_chain {
132 	LIST_HEAD(, turnstile) tc_turnstiles;	/* List of turnstiles. */
133 	struct mtx tc_lock;			/* Spin lock for this chain. */
134 #ifdef TURNSTILE_PROFILING
135 	u_int	tc_depth;			/* Length of tc_queues. */
136 	u_int	tc_max_depth;			/* Max length of tc_queues. */
137 #endif
138 };
139 
140 #ifdef TURNSTILE_PROFILING
141 u_int turnstile_max_depth;
142 static SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
143     "turnstile profiling");
144 static SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains,
145     CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
146     "turnstile chain stats");
147 SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
148     &turnstile_max_depth, 0, "maximum depth achieved of a single chain");
149 #endif
150 static struct mtx td_contested_lock;
151 static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
152 static uma_zone_t turnstile_zone;
153 
154 /*
155  * Prototypes for non-exported routines.
156  */
157 static void	init_turnstile0(void *dummy);
158 #ifdef TURNSTILE_PROFILING
159 static void	init_turnstile_profiling(void *arg);
160 #endif
161 static void	propagate_priority(struct thread *td);
162 static int	turnstile_adjust_thread(struct turnstile *ts,
163 		    struct thread *td);
164 static struct thread *turnstile_first_waiter(struct turnstile *ts);
165 static void	turnstile_setowner(struct turnstile *ts, struct thread *owner);
166 #ifdef INVARIANTS
167 static void	turnstile_dtor(void *mem, int size, void *arg);
168 #endif
169 static int	turnstile_init(void *mem, int size, int flags);
170 static void	turnstile_fini(void *mem, int size);
171 
172 SDT_PROVIDER_DECLARE(sched);
173 SDT_PROBE_DEFINE(sched, , , sleep);
174 SDT_PROBE_DEFINE2(sched, , , wakeup, "struct thread *",
175     "struct proc *");
176 
177 static inline void
propagate_unlock_ts(struct turnstile * top,struct turnstile * ts)178 propagate_unlock_ts(struct turnstile *top, struct turnstile *ts)
179 {
180 
181 	if (ts != top)
182 		mtx_unlock_spin(&ts->ts_lock);
183 }
184 
185 static inline void
propagate_unlock_td(struct turnstile * top,struct thread * td)186 propagate_unlock_td(struct turnstile *top, struct thread *td)
187 {
188 
189 	if (td->td_lock != &top->ts_lock)
190 		thread_unlock(td);
191 }
192 
193 /*
194  * Walks the chain of turnstiles and their owners to propagate the priority
195  * of the thread being blocked to all the threads holding locks that have to
196  * release their locks before this thread can run again.
197  */
198 static void
propagate_priority(struct thread * td)199 propagate_priority(struct thread *td)
200 {
201 	struct turnstile *ts, *top;
202 	int pri;
203 
204 	THREAD_LOCK_ASSERT(td, MA_OWNED);
205 	pri = td->td_priority;
206 	top = ts = td->td_blocked;
207 	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
208 
209 	/*
210 	 * The original turnstile lock is held across the entire
211 	 * operation.  We only ever lock down the chain so the lock
212 	 * order is constant.
213 	 */
214 	for (;;) {
215 		td = ts->ts_owner;
216 
217 		if (td == NULL) {
218 			/*
219 			 * This might be a read lock with no owner.  There's
220 			 * not much we can do, so just bail.
221 			 */
222 			propagate_unlock_ts(top, ts);
223 			return;
224 		}
225 
226 		/*
227 		 * Wait for the thread lock to be stable and then only
228 		 * acquire if it is not the turnstile lock.
229 		 */
230 		thread_lock_block_wait(td);
231 		if (td->td_lock != &ts->ts_lock) {
232 			thread_lock_flags(td, MTX_DUPOK);
233 			propagate_unlock_ts(top, ts);
234 		}
235 		MPASS(td->td_proc != NULL);
236 		MPASS(td->td_proc->p_magic == P_MAGIC);
237 
238 		/*
239 		 * If the thread is asleep, then we are probably about
240 		 * to deadlock.  To make debugging this easier, show
241 		 * backtrace of misbehaving thread and panic to not
242 		 * leave the kernel deadlocked.
243 		 */
244 		if (TD_IS_SLEEPING(td)) {
245 			printf(
246 		"Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
247 			    td->td_tid, td->td_proc->p_pid);
248 			kdb_backtrace_thread(td);
249 			panic("sleeping thread");
250 		}
251 
252 		/*
253 		 * If this thread already has higher priority than the
254 		 * thread that is being blocked, we are finished.
255 		 */
256 		if (td->td_priority <= pri) {
257 			propagate_unlock_td(top, td);
258 			return;
259 		}
260 
261 		/*
262 		 * Bump this thread's priority.
263 		 */
264 		sched_lend_prio(td, pri);
265 
266 		/*
267 		 * If lock holder is actually running or on the run queue
268 		 * then we are done.
269 		 */
270 		if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
271 			MPASS(td->td_blocked == NULL);
272 			propagate_unlock_td(top, td);
273 			return;
274 		}
275 
276 #ifndef SMP
277 		/*
278 		 * For UP, we check to see if td is curthread (this shouldn't
279 		 * ever happen however as it would mean we are in a deadlock.)
280 		 */
281 		KASSERT(td != curthread, ("Deadlock detected"));
282 #endif
283 
284 		/*
285 		 * If we aren't blocked on a lock, we should be.
286 		 */
287 		KASSERT(TD_ON_LOCK(td), (
288 		    "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
289 		    td->td_tid, td->td_name, TD_GET_STATE(td),
290 		    ts->ts_lockobj->lo_name));
291 
292 		/*
293 		 * Pick up the lock that td is blocked on.
294 		 */
295 		ts = td->td_blocked;
296 		MPASS(ts != NULL);
297 		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
298 		/* Resort td on the list if needed. */
299 		if (!turnstile_adjust_thread(ts, td)) {
300 			propagate_unlock_ts(top, ts);
301 			return;
302 		}
303 		/* The thread lock is released as ts lock above. */
304 	}
305 }
306 
307 /*
308  * Adjust the thread's position on a turnstile after its priority has been
309  * changed.
310  */
311 static int
turnstile_adjust_thread(struct turnstile * ts,struct thread * td)312 turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
313 {
314 	struct thread *td1, *td2;
315 	int queue;
316 
317 	THREAD_LOCK_ASSERT(td, MA_OWNED);
318 	MPASS(TD_ON_LOCK(td));
319 
320 	/*
321 	 * This thread may not be blocked on this turnstile anymore
322 	 * but instead might already be woken up on another CPU
323 	 * that is waiting on the thread lock in turnstile_unpend() to
324 	 * finish waking this thread up.  We can detect this case
325 	 * by checking to see if this thread has been given a
326 	 * turnstile by either turnstile_signal() or
327 	 * turnstile_broadcast().  In this case, treat the thread as
328 	 * if it was already running.
329 	 */
330 	if (td->td_turnstile != NULL)
331 		return (0);
332 
333 	/*
334 	 * Check if the thread needs to be moved on the blocked chain.
335 	 * It needs to be moved if either its priority is lower than
336 	 * the previous thread or higher than the next thread.
337 	 */
338 	THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock);
339 	td1 = TAILQ_PREV(td, threadqueue, td_lockq);
340 	td2 = TAILQ_NEXT(td, td_lockq);
341 	if ((td1 != NULL && td->td_priority < td1->td_priority) ||
342 	    (td2 != NULL && td->td_priority > td2->td_priority)) {
343 		/*
344 		 * Remove thread from blocked chain and determine where
345 		 * it should be moved to.
346 		 */
347 		queue = td->td_tsqueue;
348 		MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
349 		mtx_lock_spin(&td_contested_lock);
350 		TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
351 		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
352 			MPASS(td1->td_proc->p_magic == P_MAGIC);
353 			if (td1->td_priority > td->td_priority)
354 				break;
355 		}
356 
357 		if (td1 == NULL)
358 			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
359 		else
360 			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
361 		mtx_unlock_spin(&td_contested_lock);
362 		if (td1 == NULL)
363 			CTR3(KTR_LOCK,
364 		    "turnstile_adjust_thread: td %d put at tail on [%p] %s",
365 			    td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
366 		else
367 			CTR4(KTR_LOCK,
368 		    "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
369 			    td->td_tid, td1->td_tid, ts->ts_lockobj,
370 			    ts->ts_lockobj->lo_name);
371 	}
372 	return (1);
373 }
374 
375 /*
376  * Early initialization of turnstiles.  This is not done via a SYSINIT()
377  * since this needs to be initialized very early when mutexes are first
378  * initialized.
379  */
380 void
init_turnstiles(void)381 init_turnstiles(void)
382 {
383 	int i;
384 
385 	for (i = 0; i < TC_TABLESIZE; i++) {
386 		LIST_INIT(&turnstile_chains[i].tc_turnstiles);
387 		mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
388 		    NULL, MTX_SPIN);
389 	}
390 	mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
391 	LIST_INIT(&thread0.td_contested);
392 	thread0.td_turnstile = NULL;
393 }
394 
395 #ifdef TURNSTILE_PROFILING
396 static void
init_turnstile_profiling(void * arg)397 init_turnstile_profiling(void *arg)
398 {
399 	struct sysctl_oid *chain_oid;
400 	char chain_name[10];
401 	int i;
402 
403 	for (i = 0; i < TC_TABLESIZE; i++) {
404 		snprintf(chain_name, sizeof(chain_name), "%d", i);
405 		chain_oid = SYSCTL_ADD_NODE(NULL,
406 		    SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
407 		    chain_name, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
408 		    "turnstile chain stats");
409 		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
410 		    "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
411 		    NULL);
412 		SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
413 		    "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
414 		    0, NULL);
415 	}
416 }
417 SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
418     init_turnstile_profiling, NULL);
419 #endif
420 
421 static void
init_turnstile0(void * dummy)422 init_turnstile0(void *dummy)
423 {
424 
425 	turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
426 	    NULL,
427 #ifdef INVARIANTS
428 	    turnstile_dtor,
429 #else
430 	    NULL,
431 #endif
432 	    turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
433 	thread0.td_turnstile = turnstile_alloc();
434 }
435 SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
436 
437 /*
438  * Update a thread on the turnstile list after it's priority has been changed.
439  * The old priority is passed in as an argument.
440  */
441 void
turnstile_adjust(struct thread * td,u_char oldpri)442 turnstile_adjust(struct thread *td, u_char oldpri)
443 {
444 	struct turnstile *ts;
445 
446 	MPASS(TD_ON_LOCK(td));
447 
448 	/*
449 	 * Pick up the lock that td is blocked on.
450 	 */
451 	ts = td->td_blocked;
452 	MPASS(ts != NULL);
453 	THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock);
454 	mtx_assert(&ts->ts_lock, MA_OWNED);
455 
456 	/* Resort the turnstile on the list. */
457 	if (!turnstile_adjust_thread(ts, td))
458 		return;
459 	/*
460 	 * If our priority was lowered and we are at the head of the
461 	 * turnstile, then propagate our new priority up the chain.
462 	 * Note that we currently don't try to revoke lent priorities
463 	 * when our priority goes up.
464 	 */
465 	MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
466 	    td->td_tsqueue == TS_SHARED_QUEUE);
467 	if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
468 	    td->td_priority < oldpri) {
469 		propagate_priority(td);
470 	}
471 }
472 
473 /*
474  * Set the owner of the lock this turnstile is attached to.
475  */
476 static void
turnstile_setowner(struct turnstile * ts,struct thread * owner)477 turnstile_setowner(struct turnstile *ts, struct thread *owner)
478 {
479 
480 	mtx_assert(&td_contested_lock, MA_OWNED);
481 	MPASS(ts->ts_owner == NULL);
482 
483 	/* A shared lock might not have an owner. */
484 	if (owner == NULL)
485 		return;
486 
487 	MPASS(owner->td_proc->p_magic == P_MAGIC);
488 	ts->ts_owner = owner;
489 	LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
490 }
491 
492 #ifdef INVARIANTS
493 /*
494  * UMA zone item deallocator.
495  */
496 static void
turnstile_dtor(void * mem,int size,void * arg)497 turnstile_dtor(void *mem, int size, void *arg)
498 {
499 	struct turnstile *ts;
500 
501 	ts = mem;
502 	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
503 	MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
504 	MPASS(TAILQ_EMPTY(&ts->ts_pending));
505 }
506 #endif
507 
508 /*
509  * UMA zone item initializer.
510  */
511 static int
turnstile_init(void * mem,int size,int flags)512 turnstile_init(void *mem, int size, int flags)
513 {
514 	struct turnstile *ts;
515 
516 	bzero(mem, size);
517 	ts = mem;
518 	TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
519 	TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
520 	TAILQ_INIT(&ts->ts_pending);
521 	LIST_INIT(&ts->ts_free);
522 	mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN);
523 	return (0);
524 }
525 
526 static void
turnstile_fini(void * mem,int size)527 turnstile_fini(void *mem, int size)
528 {
529 	struct turnstile *ts;
530 
531 	ts = mem;
532 	mtx_destroy(&ts->ts_lock);
533 }
534 
535 /*
536  * Get a turnstile for a new thread.
537  */
538 struct turnstile *
turnstile_alloc(void)539 turnstile_alloc(void)
540 {
541 
542 	return (uma_zalloc(turnstile_zone, M_WAITOK));
543 }
544 
545 /*
546  * Free a turnstile when a thread is destroyed.
547  */
548 void
turnstile_free(struct turnstile * ts)549 turnstile_free(struct turnstile *ts)
550 {
551 
552 	uma_zfree(turnstile_zone, ts);
553 }
554 
555 /*
556  * Lock the turnstile chain associated with the specified lock.
557  */
558 void
turnstile_chain_lock(struct lock_object * lock)559 turnstile_chain_lock(struct lock_object *lock)
560 {
561 	struct turnstile_chain *tc;
562 
563 	tc = TC_LOOKUP(lock);
564 	mtx_lock_spin(&tc->tc_lock);
565 }
566 
567 struct turnstile *
turnstile_trywait(struct lock_object * lock)568 turnstile_trywait(struct lock_object *lock)
569 {
570 	struct turnstile_chain *tc;
571 	struct turnstile *ts;
572 
573 	tc = TC_LOOKUP(lock);
574 	mtx_lock_spin(&tc->tc_lock);
575 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
576 		if (ts->ts_lockobj == lock) {
577 			mtx_lock_spin(&ts->ts_lock);
578 			return (ts);
579 		}
580 
581 	ts = curthread->td_turnstile;
582 	MPASS(ts != NULL);
583 	mtx_lock_spin(&ts->ts_lock);
584 	KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
585 	ts->ts_lockobj = lock;
586 
587 	return (ts);
588 }
589 
590 bool
turnstile_lock(struct turnstile * ts,struct lock_object ** lockp,struct thread ** tdp)591 turnstile_lock(struct turnstile *ts, struct lock_object **lockp,
592     struct thread **tdp)
593 {
594 	struct turnstile_chain *tc;
595 	struct lock_object *lock;
596 
597 	if ((lock = ts->ts_lockobj) == NULL)
598 		return (false);
599 	tc = TC_LOOKUP(lock);
600 	mtx_lock_spin(&tc->tc_lock);
601 	mtx_lock_spin(&ts->ts_lock);
602 	if (__predict_false(lock != ts->ts_lockobj)) {
603 		mtx_unlock_spin(&tc->tc_lock);
604 		mtx_unlock_spin(&ts->ts_lock);
605 		return (false);
606 	}
607 	*lockp = lock;
608 	*tdp = ts->ts_owner;
609 	return (true);
610 }
611 
612 void
turnstile_unlock(struct turnstile * ts,struct lock_object * lock)613 turnstile_unlock(struct turnstile *ts, struct lock_object *lock)
614 {
615 	struct turnstile_chain *tc;
616 
617 	mtx_assert(&ts->ts_lock, MA_OWNED);
618 	mtx_unlock_spin(&ts->ts_lock);
619 	if (ts == curthread->td_turnstile)
620 		ts->ts_lockobj = NULL;
621 	tc = TC_LOOKUP(lock);
622 	mtx_unlock_spin(&tc->tc_lock);
623 }
624 
625 void
turnstile_assert(struct turnstile * ts)626 turnstile_assert(struct turnstile *ts)
627 {
628 	MPASS(ts->ts_lockobj == NULL);
629 }
630 
631 void
turnstile_cancel(struct turnstile * ts)632 turnstile_cancel(struct turnstile *ts)
633 {
634 	struct turnstile_chain *tc;
635 	struct lock_object *lock;
636 
637 	mtx_assert(&ts->ts_lock, MA_OWNED);
638 
639 	mtx_unlock_spin(&ts->ts_lock);
640 	lock = ts->ts_lockobj;
641 	if (ts == curthread->td_turnstile)
642 		ts->ts_lockobj = NULL;
643 	tc = TC_LOOKUP(lock);
644 	mtx_unlock_spin(&tc->tc_lock);
645 }
646 
647 /*
648  * Look up the turnstile for a lock in the hash table locking the associated
649  * turnstile chain along the way.  If no turnstile is found in the hash
650  * table, NULL is returned.
651  */
652 struct turnstile *
turnstile_lookup(struct lock_object * lock)653 turnstile_lookup(struct lock_object *lock)
654 {
655 	struct turnstile_chain *tc;
656 	struct turnstile *ts;
657 
658 	tc = TC_LOOKUP(lock);
659 	mtx_assert(&tc->tc_lock, MA_OWNED);
660 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
661 		if (ts->ts_lockobj == lock) {
662 			mtx_lock_spin(&ts->ts_lock);
663 			return (ts);
664 		}
665 	return (NULL);
666 }
667 
668 /*
669  * Unlock the turnstile chain associated with a given lock.
670  */
671 void
turnstile_chain_unlock(struct lock_object * lock)672 turnstile_chain_unlock(struct lock_object *lock)
673 {
674 	struct turnstile_chain *tc;
675 
676 	tc = TC_LOOKUP(lock);
677 	mtx_unlock_spin(&tc->tc_lock);
678 }
679 
680 /*
681  * Return a pointer to the thread waiting on this turnstile with the
682  * most important priority or NULL if the turnstile has no waiters.
683  */
684 static struct thread *
turnstile_first_waiter(struct turnstile * ts)685 turnstile_first_waiter(struct turnstile *ts)
686 {
687 	struct thread *std, *xtd;
688 
689 	std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]);
690 	xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
691 	if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority))
692 		return (std);
693 	return (xtd);
694 }
695 
696 /*
697  * Take ownership of a turnstile and adjust the priority of the new
698  * owner appropriately.
699  */
700 void
turnstile_claim(struct turnstile * ts)701 turnstile_claim(struct turnstile *ts)
702 {
703 	struct thread *td, *owner;
704 	struct turnstile_chain *tc;
705 
706 	mtx_assert(&ts->ts_lock, MA_OWNED);
707 	MPASS(ts != curthread->td_turnstile);
708 
709 	owner = curthread;
710 	mtx_lock_spin(&td_contested_lock);
711 	turnstile_setowner(ts, owner);
712 	mtx_unlock_spin(&td_contested_lock);
713 
714 	td = turnstile_first_waiter(ts);
715 	MPASS(td != NULL);
716 	MPASS(td->td_proc->p_magic == P_MAGIC);
717 	THREAD_LOCKPTR_BLOCKED_ASSERT(td, &ts->ts_lock);
718 
719 	/*
720 	 * Update the priority of the new owner if needed.
721 	 */
722 	thread_lock(owner);
723 	if (td->td_priority < owner->td_priority)
724 		sched_lend_prio(owner, td->td_priority);
725 	thread_unlock(owner);
726 	tc = TC_LOOKUP(ts->ts_lockobj);
727 	mtx_unlock_spin(&ts->ts_lock);
728 	mtx_unlock_spin(&tc->tc_lock);
729 }
730 
731 /*
732  * Block the current thread on the turnstile assicated with 'lock'.  This
733  * function will context switch and not return until this thread has been
734  * woken back up.  This function must be called with the appropriate
735  * turnstile chain locked and will return with it unlocked.
736  */
737 void
turnstile_wait(struct turnstile * ts,struct thread * owner,int queue)738 turnstile_wait(struct turnstile *ts, struct thread *owner, int queue)
739 {
740 	struct turnstile_chain *tc;
741 	struct thread *td, *td1;
742 	struct lock_object *lock;
743 
744 	td = curthread;
745 	mtx_assert(&ts->ts_lock, MA_OWNED);
746 	if (owner)
747 		MPASS(owner->td_proc->p_magic == P_MAGIC);
748 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
749 
750 	/*
751 	 * If the lock does not already have a turnstile, use this thread's
752 	 * turnstile.  Otherwise insert the current thread into the
753 	 * turnstile already in use by this lock.
754 	 */
755 	tc = TC_LOOKUP(ts->ts_lockobj);
756 	mtx_assert(&tc->tc_lock, MA_OWNED);
757 	if (ts == td->td_turnstile) {
758 #ifdef TURNSTILE_PROFILING
759 		tc->tc_depth++;
760 		if (tc->tc_depth > tc->tc_max_depth) {
761 			tc->tc_max_depth = tc->tc_depth;
762 			if (tc->tc_max_depth > turnstile_max_depth)
763 				turnstile_max_depth = tc->tc_max_depth;
764 		}
765 #endif
766 		LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
767 		KASSERT(TAILQ_EMPTY(&ts->ts_pending),
768 		    ("thread's turnstile has pending threads"));
769 		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
770 		    ("thread's turnstile has exclusive waiters"));
771 		KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
772 		    ("thread's turnstile has shared waiters"));
773 		KASSERT(LIST_EMPTY(&ts->ts_free),
774 		    ("thread's turnstile has a non-empty free list"));
775 		MPASS(ts->ts_lockobj != NULL);
776 		mtx_lock_spin(&td_contested_lock);
777 		TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
778 		turnstile_setowner(ts, owner);
779 		mtx_unlock_spin(&td_contested_lock);
780 	} else {
781 		TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
782 			if (td1->td_priority > td->td_priority)
783 				break;
784 		mtx_lock_spin(&td_contested_lock);
785 		if (td1 != NULL)
786 			TAILQ_INSERT_BEFORE(td1, td, td_lockq);
787 		else
788 			TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
789 		MPASS(owner == ts->ts_owner);
790 		mtx_unlock_spin(&td_contested_lock);
791 		MPASS(td->td_turnstile != NULL);
792 		LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
793 	}
794 	thread_lock(td);
795 	thread_lock_set(td, &ts->ts_lock);
796 	td->td_turnstile = NULL;
797 
798 	/* Save who we are blocked on and switch. */
799 	lock = ts->ts_lockobj;
800 	td->td_tsqueue = queue;
801 	td->td_blocked = ts;
802 	td->td_lockname = lock->lo_name;
803 	td->td_blktick = ticks;
804 	TD_SET_LOCK(td);
805 	mtx_unlock_spin(&tc->tc_lock);
806 	propagate_priority(td);
807 
808 	if (LOCK_LOG_TEST(lock, 0))
809 		CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
810 		    td->td_tid, lock, lock->lo_name);
811 
812 	SDT_PROBE0(sched, , , sleep);
813 
814 	THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
815 	mi_switch(SW_VOL | SWT_TURNSTILE);
816 
817 	if (LOCK_LOG_TEST(lock, 0))
818 		CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
819 		    __func__, td->td_tid, lock, lock->lo_name);
820 }
821 
822 /*
823  * Pick the highest priority thread on this turnstile and put it on the
824  * pending list.  This must be called with the turnstile chain locked.
825  */
826 int
turnstile_signal(struct turnstile * ts,int queue)827 turnstile_signal(struct turnstile *ts, int queue)
828 {
829 	struct turnstile_chain *tc __unused;
830 	struct thread *td;
831 	int empty;
832 
833 	MPASS(ts != NULL);
834 	mtx_assert(&ts->ts_lock, MA_OWNED);
835 	MPASS(curthread->td_proc->p_magic == P_MAGIC);
836 	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
837 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
838 
839 	/*
840 	 * Pick the highest priority thread blocked on this lock and
841 	 * move it to the pending list.
842 	 */
843 	td = TAILQ_FIRST(&ts->ts_blocked[queue]);
844 	MPASS(td->td_proc->p_magic == P_MAGIC);
845 	mtx_lock_spin(&td_contested_lock);
846 	TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
847 	mtx_unlock_spin(&td_contested_lock);
848 	TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
849 
850 	/*
851 	 * If the turnstile is now empty, remove it from its chain and
852 	 * give it to the about-to-be-woken thread.  Otherwise take a
853 	 * turnstile from the free list and give it to the thread.
854 	 */
855 	empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
856 	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
857 	if (empty) {
858 		tc = TC_LOOKUP(ts->ts_lockobj);
859 		mtx_assert(&tc->tc_lock, MA_OWNED);
860 		MPASS(LIST_EMPTY(&ts->ts_free));
861 #ifdef TURNSTILE_PROFILING
862 		tc->tc_depth--;
863 #endif
864 	} else
865 		ts = LIST_FIRST(&ts->ts_free);
866 	MPASS(ts != NULL);
867 	LIST_REMOVE(ts, ts_hash);
868 	td->td_turnstile = ts;
869 
870 	return (empty);
871 }
872 
873 /*
874  * Put all blocked threads on the pending list.  This must be called with
875  * the turnstile chain locked.
876  */
877 void
turnstile_broadcast(struct turnstile * ts,int queue)878 turnstile_broadcast(struct turnstile *ts, int queue)
879 {
880 	struct turnstile_chain *tc __unused;
881 	struct turnstile *ts1;
882 	struct thread *td;
883 
884 	MPASS(ts != NULL);
885 	mtx_assert(&ts->ts_lock, MA_OWNED);
886 	MPASS(curthread->td_proc->p_magic == P_MAGIC);
887 	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
888 	/*
889 	 * We must have the chain locked so that we can remove the empty
890 	 * turnstile from the hash queue.
891 	 */
892 	tc = TC_LOOKUP(ts->ts_lockobj);
893 	mtx_assert(&tc->tc_lock, MA_OWNED);
894 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
895 
896 	/*
897 	 * Transfer the blocked list to the pending list.
898 	 */
899 	mtx_lock_spin(&td_contested_lock);
900 	TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
901 	mtx_unlock_spin(&td_contested_lock);
902 
903 	/*
904 	 * Give a turnstile to each thread.  The last thread gets
905 	 * this turnstile if the turnstile is empty.
906 	 */
907 	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
908 		if (LIST_EMPTY(&ts->ts_free)) {
909 			MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
910 			ts1 = ts;
911 #ifdef TURNSTILE_PROFILING
912 			tc->tc_depth--;
913 #endif
914 		} else
915 			ts1 = LIST_FIRST(&ts->ts_free);
916 		MPASS(ts1 != NULL);
917 		LIST_REMOVE(ts1, ts_hash);
918 		td->td_turnstile = ts1;
919 	}
920 }
921 
922 static u_char
turnstile_calc_unlend_prio_locked(struct thread * td)923 turnstile_calc_unlend_prio_locked(struct thread *td)
924 {
925 	struct turnstile *nts;
926 	u_char cp, pri;
927 
928 	THREAD_LOCK_ASSERT(td, MA_OWNED);
929 	mtx_assert(&td_contested_lock, MA_OWNED);
930 
931 	pri = PRI_MAX;
932 	LIST_FOREACH(nts, &td->td_contested, ts_link) {
933 		cp = turnstile_first_waiter(nts)->td_priority;
934 		if (cp < pri)
935 			pri = cp;
936 	}
937 	return (pri);
938 }
939 
940 /*
941  * Wakeup all threads on the pending list and adjust the priority of the
942  * current thread appropriately.  This must be called with the turnstile
943  * chain locked.
944  */
945 void
turnstile_unpend(struct turnstile * ts)946 turnstile_unpend(struct turnstile *ts)
947 {
948 	TAILQ_HEAD( ,thread) pending_threads;
949 	struct thread *td;
950 	u_char pri;
951 
952 	MPASS(ts != NULL);
953 	mtx_assert(&ts->ts_lock, MA_OWNED);
954 	MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
955 	MPASS(!TAILQ_EMPTY(&ts->ts_pending));
956 
957 	/*
958 	 * Move the list of pending threads out of the turnstile and
959 	 * into a local variable.
960 	 */
961 	TAILQ_INIT(&pending_threads);
962 	TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
963 #ifdef INVARIANTS
964 	if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
965 	    TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
966 		ts->ts_lockobj = NULL;
967 #endif
968 	/*
969 	 * Adjust the priority of curthread based on other contested
970 	 * locks it owns.  Don't lower the priority below the base
971 	 * priority however.
972 	 */
973 	td = curthread;
974 	thread_lock(td);
975 	mtx_lock_spin(&td_contested_lock);
976 	/*
977 	 * Remove the turnstile from this thread's list of contested locks
978 	 * since this thread doesn't own it anymore.  New threads will
979 	 * not be blocking on the turnstile until it is claimed by a new
980 	 * owner.  There might not be a current owner if this is a shared
981 	 * lock.
982 	 */
983 	if (ts->ts_owner != NULL) {
984 		ts->ts_owner = NULL;
985 		LIST_REMOVE(ts, ts_link);
986 	}
987 	pri = turnstile_calc_unlend_prio_locked(td);
988 	mtx_unlock_spin(&td_contested_lock);
989 	sched_unlend_prio(td, pri);
990 	thread_unlock(td);
991 	/*
992 	 * Wake up all the pending threads.  If a thread is not blocked
993 	 * on a lock, then it is currently executing on another CPU in
994 	 * turnstile_wait() or sitting on a run queue waiting to resume
995 	 * in turnstile_wait().  Set a flag to force it to try to acquire
996 	 * the lock again instead of blocking.
997 	 */
998 	while (!TAILQ_EMPTY(&pending_threads)) {
999 		td = TAILQ_FIRST(&pending_threads);
1000 		TAILQ_REMOVE(&pending_threads, td, td_lockq);
1001 		SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
1002 		thread_lock_block_wait(td);
1003 		THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
1004 		MPASS(td->td_proc->p_magic == P_MAGIC);
1005 		MPASS(TD_ON_LOCK(td));
1006 		TD_CLR_LOCK(td);
1007 		MPASS(TD_CAN_RUN(td));
1008 		td->td_blocked = NULL;
1009 		td->td_lockname = NULL;
1010 		td->td_blktick = 0;
1011 #ifdef INVARIANTS
1012 		td->td_tsqueue = 0xff;
1013 #endif
1014 		sched_add(td, SRQ_HOLD | SRQ_BORING);
1015 	}
1016 	mtx_unlock_spin(&ts->ts_lock);
1017 }
1018 
1019 /*
1020  * Give up ownership of a turnstile.  This must be called with the
1021  * turnstile chain locked.
1022  */
1023 void
turnstile_disown(struct turnstile * ts)1024 turnstile_disown(struct turnstile *ts)
1025 {
1026 	struct thread *td;
1027 	u_char pri;
1028 
1029 	MPASS(ts != NULL);
1030 	mtx_assert(&ts->ts_lock, MA_OWNED);
1031 	MPASS(ts->ts_owner == curthread);
1032 	MPASS(TAILQ_EMPTY(&ts->ts_pending));
1033 	MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
1034 	    !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
1035 
1036 	/*
1037 	 * Remove the turnstile from this thread's list of contested locks
1038 	 * since this thread doesn't own it anymore.  New threads will
1039 	 * not be blocking on the turnstile until it is claimed by a new
1040 	 * owner.
1041 	 */
1042 	mtx_lock_spin(&td_contested_lock);
1043 	ts->ts_owner = NULL;
1044 	LIST_REMOVE(ts, ts_link);
1045 	mtx_unlock_spin(&td_contested_lock);
1046 
1047 	/*
1048 	 * Adjust the priority of curthread based on other contested
1049 	 * locks it owns.  Don't lower the priority below the base
1050 	 * priority however.
1051 	 */
1052 	td = curthread;
1053 	thread_lock(td);
1054 	mtx_unlock_spin(&ts->ts_lock);
1055 	mtx_lock_spin(&td_contested_lock);
1056 	pri = turnstile_calc_unlend_prio_locked(td);
1057 	mtx_unlock_spin(&td_contested_lock);
1058 	sched_unlend_prio(td, pri);
1059 	thread_unlock(td);
1060 }
1061 
1062 /*
1063  * Return the first thread in a turnstile.
1064  */
1065 struct thread *
turnstile_head(struct turnstile * ts,int queue)1066 turnstile_head(struct turnstile *ts, int queue)
1067 {
1068 #ifdef INVARIANTS
1069 
1070 	MPASS(ts != NULL);
1071 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1072 	mtx_assert(&ts->ts_lock, MA_OWNED);
1073 #endif
1074 	return (TAILQ_FIRST(&ts->ts_blocked[queue]));
1075 }
1076 
1077 /*
1078  * Returns true if a sub-queue of a turnstile is empty.
1079  */
1080 int
turnstile_empty(struct turnstile * ts,int queue)1081 turnstile_empty(struct turnstile *ts, int queue)
1082 {
1083 #ifdef INVARIANTS
1084 
1085 	MPASS(ts != NULL);
1086 	MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1087 	mtx_assert(&ts->ts_lock, MA_OWNED);
1088 #endif
1089 	return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
1090 }
1091 
1092 #ifdef DDB
1093 static void
print_thread(struct thread * td,const char * prefix)1094 print_thread(struct thread *td, const char *prefix)
1095 {
1096 
1097 	db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid,
1098 	    td->td_proc->p_pid, td->td_name);
1099 }
1100 
1101 static void
print_queue(struct threadqueue * queue,const char * header,const char * prefix)1102 print_queue(struct threadqueue *queue, const char *header, const char *prefix)
1103 {
1104 	struct thread *td;
1105 
1106 	db_printf("%s:\n", header);
1107 	if (TAILQ_EMPTY(queue)) {
1108 		db_printf("%sempty\n", prefix);
1109 		return;
1110 	}
1111 	TAILQ_FOREACH(td, queue, td_lockq) {
1112 		print_thread(td, prefix);
1113 	}
1114 }
1115 
DB_SHOW_COMMAND(turnstile,db_show_turnstile)1116 DB_SHOW_COMMAND(turnstile, db_show_turnstile)
1117 {
1118 	struct turnstile_chain *tc;
1119 	struct turnstile *ts;
1120 	struct lock_object *lock;
1121 	int i;
1122 
1123 	if (!have_addr)
1124 		return;
1125 
1126 	/*
1127 	 * First, see if there is an active turnstile for the lock indicated
1128 	 * by the address.
1129 	 */
1130 	lock = (struct lock_object *)addr;
1131 	tc = TC_LOOKUP(lock);
1132 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1133 		if (ts->ts_lockobj == lock)
1134 			goto found;
1135 
1136 	/*
1137 	 * Second, see if there is an active turnstile at the address
1138 	 * indicated.
1139 	 */
1140 	for (i = 0; i < TC_TABLESIZE; i++)
1141 		LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) {
1142 			if (ts == (struct turnstile *)addr)
1143 				goto found;
1144 		}
1145 
1146 	db_printf("Unable to locate a turnstile via %p\n", (void *)addr);
1147 	return;
1148 found:
1149 	lock = ts->ts_lockobj;
1150 	db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name,
1151 	    lock->lo_name);
1152 	if (ts->ts_owner)
1153 		print_thread(ts->ts_owner, "Lock Owner: ");
1154 	else
1155 		db_printf("Lock Owner: none\n");
1156 	print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t");
1157 	print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters",
1158 	    "\t");
1159 	print_queue(&ts->ts_pending, "Pending Threads", "\t");
1160 
1161 }
1162 
1163 /*
1164  * Show all the threads a particular thread is waiting on based on
1165  * non-spin locks.
1166  */
1167 static void
print_lockchain(struct thread * td,const char * prefix)1168 print_lockchain(struct thread *td, const char *prefix)
1169 {
1170 	struct lock_object *lock;
1171 	struct lock_class *class;
1172 	struct turnstile *ts;
1173 	struct thread *owner;
1174 
1175 	/*
1176 	 * Follow the chain.  We keep walking as long as the thread is
1177 	 * blocked on a lock that has an owner.
1178 	 */
1179 	while (!db_pager_quit) {
1180 		if (td == (void *)LK_KERNPROC) {
1181 			db_printf("%sdisowned (LK_KERNPROC)\n", prefix);
1182 			return;
1183 		}
1184 		db_printf("%sthread %d (pid %d, %s) is ", prefix, td->td_tid,
1185 		    td->td_proc->p_pid, td->td_name);
1186 		switch (TD_GET_STATE(td)) {
1187 		case TDS_INACTIVE:
1188 			db_printf("inactive\n");
1189 			return;
1190 		case TDS_CAN_RUN:
1191 			db_printf("runnable\n");
1192 			return;
1193 		case TDS_RUNQ:
1194 			db_printf("on a run queue\n");
1195 			return;
1196 		case TDS_RUNNING:
1197 			db_printf("running on CPU %d\n", td->td_oncpu);
1198 			return;
1199 		case TDS_INHIBITED:
1200 			if (TD_ON_LOCK(td)) {
1201 				ts = td->td_blocked;
1202 				lock = ts->ts_lockobj;
1203 				class = LOCK_CLASS(lock);
1204 				db_printf("blocked on lock %p (%s) \"%s\"\n",
1205 				    lock, class->lc_name, lock->lo_name);
1206 				if (ts->ts_owner == NULL)
1207 					return;
1208 				td = ts->ts_owner;
1209 				break;
1210 			} else if (TD_ON_SLEEPQ(td)) {
1211 				if (!lockmgr_chain(td, &owner) &&
1212 				    !sx_chain(td, &owner)) {
1213 					db_printf("sleeping on %p \"%s\"\n",
1214 					    td->td_wchan, td->td_wmesg);
1215 					return;
1216 				}
1217 				if (owner == NULL)
1218 					return;
1219 				td = owner;
1220 				break;
1221 			}
1222 			db_printf("inhibited: %s\n", KTDSTATE(td));
1223 			return;
1224 		default:
1225 			db_printf("??? (%#x)\n", TD_GET_STATE(td));
1226 			return;
1227 		}
1228 	}
1229 }
1230 
DB_SHOW_COMMAND(lockchain,db_show_lockchain)1231 DB_SHOW_COMMAND(lockchain, db_show_lockchain)
1232 {
1233 	struct thread *td;
1234 
1235 	/* Figure out which thread to start with. */
1236 	if (have_addr)
1237 		td = db_lookup_thread(addr, true);
1238 	else
1239 		td = kdb_thread;
1240 
1241 	print_lockchain(td, "");
1242 }
1243 DB_SHOW_ALIAS(sleepchain, db_show_lockchain);
1244 
DB_SHOW_ALL_COMMAND(chains,db_show_allchains)1245 DB_SHOW_ALL_COMMAND(chains, db_show_allchains)
1246 {
1247 	struct thread *td;
1248 	struct proc *p;
1249 	int i;
1250 
1251 	i = 1;
1252 	FOREACH_PROC_IN_SYSTEM(p) {
1253 		FOREACH_THREAD_IN_PROC(p, td) {
1254 			if ((TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested))
1255 			    || (TD_IS_INHIBITED(td) && TD_ON_SLEEPQ(td))) {
1256 				db_printf("chain %d:\n", i++);
1257 				print_lockchain(td, " ");
1258 			}
1259 			if (db_pager_quit)
1260 				return;
1261 		}
1262 	}
1263 }
1264 DB_SHOW_ALIAS_FLAGS(allchains, db_show_allchains, DB_CMD_MEMSAFE);
1265 
1266 static void	print_waiters(struct turnstile *ts, int indent);
1267 
1268 static void
print_waiter(struct thread * td,int indent)1269 print_waiter(struct thread *td, int indent)
1270 {
1271 	struct turnstile *ts;
1272 	int i;
1273 
1274 	if (db_pager_quit)
1275 		return;
1276 	for (i = 0; i < indent; i++)
1277 		db_printf(" ");
1278 	print_thread(td, "thread ");
1279 	LIST_FOREACH(ts, &td->td_contested, ts_link)
1280 		print_waiters(ts, indent + 1);
1281 }
1282 
1283 static void
print_waiters(struct turnstile * ts,int indent)1284 print_waiters(struct turnstile *ts, int indent)
1285 {
1286 	struct lock_object *lock;
1287 	struct lock_class *class;
1288 	struct thread *td;
1289 	int i;
1290 
1291 	if (db_pager_quit)
1292 		return;
1293 	lock = ts->ts_lockobj;
1294 	class = LOCK_CLASS(lock);
1295 	for (i = 0; i < indent; i++)
1296 		db_printf(" ");
1297 	db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
1298 	TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
1299 		print_waiter(td, indent + 1);
1300 	TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
1301 		print_waiter(td, indent + 1);
1302 	TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
1303 		print_waiter(td, indent + 1);
1304 }
1305 
DB_SHOW_COMMAND(locktree,db_show_locktree)1306 DB_SHOW_COMMAND(locktree, db_show_locktree)
1307 {
1308 	struct lock_object *lock;
1309 	struct lock_class *class;
1310 	struct turnstile_chain *tc;
1311 	struct turnstile *ts;
1312 
1313 	if (!have_addr)
1314 		return;
1315 	lock = (struct lock_object *)addr;
1316 	tc = TC_LOOKUP(lock);
1317 	LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1318 		if (ts->ts_lockobj == lock)
1319 			break;
1320 	if (ts == NULL) {
1321 		class = LOCK_CLASS(lock);
1322 		db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
1323 		    lock->lo_name);
1324 	} else
1325 		print_waiters(ts, 0);
1326 }
1327 #endif
1328