2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
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
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 $
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.
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.
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.
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
65 #include "opt_turnstile_profiling.h"
66 #include "opt_sched.h"
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/kernel.h>
74 #include <sys/mutex.h>
76 #include <sys/queue.h>
77 #include <sys/sched.h>
79 #include <sys/sysctl.h>
80 #include <sys/turnstile.h>
86 #include <sys/lockmgr.h>
91 * Constants for the hash table of turnstile chains. TC_SHIFT is a magic
92 * number chosen because the sleep queue's use the same value for the
93 * shift. Basically, we ignore the lower 8 bits of the address.
94 * TC_TABLESIZE must be a power of two for TC_MASK to work properly.
96 #define TC_TABLESIZE 128 /* Must be power of 2. */
97 #define TC_MASK (TC_TABLESIZE - 1)
99 #define TC_HASH(lock) (((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
100 #define TC_LOOKUP(lock) &turnstile_chains[TC_HASH(lock)]
103 * There are three different lists of turnstiles as follows. The list
104 * connected by ts_link entries is a per-thread list of all the turnstiles
105 * attached to locks that we own. This is used to fixup our priority when
106 * a lock is released. The other two lists use the ts_hash entries. The
107 * first of these two is the turnstile chain list that a turnstile is on
108 * when it is attached to a lock. The second list to use ts_hash is the
109 * free list hung off of a turnstile that is attached to a lock.
111 * Each turnstile contains three lists of threads. The two ts_blocked lists
112 * are linked list of threads blocked on the turnstile's lock. One list is
113 * for exclusive waiters, and the other is for shared waiters. The
114 * ts_pending list is a linked list of threads previously awakened by
115 * turnstile_signal() or turnstile_wait() that are waiting to be put on
119 * c - turnstile chain lock
120 * q - td_contested lock
123 struct mtx ts_lock; /* Spin lock for self. */
124 struct threadqueue ts_blocked[2]; /* (c + q) Blocked threads. */
125 struct threadqueue ts_pending; /* (c) Pending threads. */
126 LIST_ENTRY(turnstile) ts_hash; /* (c) Chain and free list. */
127 LIST_ENTRY(turnstile) ts_link; /* (q) Contested locks. */
128 LIST_HEAD(, turnstile) ts_free; /* (c) Free turnstiles. */
129 struct lock_object *ts_lockobj; /* (c) Lock we reference. */
130 struct thread *ts_owner; /* (c + q) Who owns the lock. */
133 struct turnstile_chain {
134 LIST_HEAD(, turnstile) tc_turnstiles; /* List of turnstiles. */
135 struct mtx tc_lock; /* Spin lock for this chain. */
136 #ifdef TURNSTILE_PROFILING
137 u_int tc_depth; /* Length of tc_queues. */
138 u_int tc_max_depth; /* Max length of tc_queues. */
142 #ifdef TURNSTILE_PROFILING
143 u_int turnstile_max_depth;
144 static SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0,
145 "turnstile profiling");
146 static SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
147 "turnstile chain stats");
148 SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
149 &turnstile_max_depth, 0, "maximum depth achieved of a single chain");
151 static struct mtx td_contested_lock;
152 static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
153 static uma_zone_t turnstile_zone;
156 * Prototypes for non-exported routines.
158 static void init_turnstile0(void *dummy);
159 #ifdef TURNSTILE_PROFILING
160 static void init_turnstile_profiling(void *arg);
162 static void propagate_priority(struct thread *td);
163 static int turnstile_adjust_thread(struct turnstile *ts,
165 static struct thread *turnstile_first_waiter(struct turnstile *ts);
166 static void turnstile_setowner(struct turnstile *ts, struct thread *owner);
168 static void turnstile_dtor(void *mem, int size, void *arg);
170 static int turnstile_init(void *mem, int size, int flags);
171 static void turnstile_fini(void *mem, int size);
173 SDT_PROVIDER_DECLARE(sched);
174 SDT_PROBE_DEFINE(sched, , , sleep);
175 SDT_PROBE_DEFINE2(sched, , , wakeup, "struct thread *",
179 * Walks the chain of turnstiles and their owners to propagate the priority
180 * of the thread being blocked to all the threads holding locks that have to
181 * release their locks before this thread can run again.
184 propagate_priority(struct thread *td)
186 struct turnstile *ts;
189 THREAD_LOCK_ASSERT(td, MA_OWNED);
190 pri = td->td_priority;
192 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
194 * Grab a recursive lock on this turnstile chain so it stays locked
195 * for the whole operation. The caller expects us to return with
196 * the original lock held. We only ever lock down the chain so
197 * the lock order is constant.
199 mtx_lock_spin(&ts->ts_lock);
205 * This might be a read lock with no owner. There's
206 * not much we can do, so just bail.
208 mtx_unlock_spin(&ts->ts_lock);
212 thread_lock_flags(td, MTX_DUPOK);
213 mtx_unlock_spin(&ts->ts_lock);
214 MPASS(td->td_proc != NULL);
215 MPASS(td->td_proc->p_magic == P_MAGIC);
218 * If the thread is asleep, then we are probably about
219 * to deadlock. To make debugging this easier, show
220 * backtrace of misbehaving thread and panic to not
221 * leave the kernel deadlocked.
223 if (TD_IS_SLEEPING(td)) {
225 "Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
226 td->td_tid, td->td_proc->p_pid);
227 kdb_backtrace_thread(td);
228 panic("sleeping thread");
232 * If this thread already has higher priority than the
233 * thread that is being blocked, we are finished.
235 if (td->td_priority <= pri) {
241 * Bump this thread's priority.
243 sched_lend_prio(td, pri);
246 * If lock holder is actually running or on the run queue
249 if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
250 MPASS(td->td_blocked == NULL);
257 * For UP, we check to see if td is curthread (this shouldn't
258 * ever happen however as it would mean we are in a deadlock.)
260 KASSERT(td != curthread, ("Deadlock detected"));
264 * If we aren't blocked on a lock, we should be.
266 KASSERT(TD_ON_LOCK(td), (
267 "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
268 td->td_tid, td->td_name, td->td_state,
269 ts->ts_lockobj->lo_name));
272 * Pick up the lock that td is blocked on.
276 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
277 /* Resort td on the list if needed. */
278 if (!turnstile_adjust_thread(ts, td)) {
279 mtx_unlock_spin(&ts->ts_lock);
282 /* The thread lock is released as ts lock above. */
287 * Adjust the thread's position on a turnstile after its priority has been
291 turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
293 struct thread *td1, *td2;
296 THREAD_LOCK_ASSERT(td, MA_OWNED);
297 MPASS(TD_ON_LOCK(td));
300 * This thread may not be blocked on this turnstile anymore
301 * but instead might already be woken up on another CPU
302 * that is waiting on the thread lock in turnstile_unpend() to
303 * finish waking this thread up. We can detect this case
304 * by checking to see if this thread has been given a
305 * turnstile by either turnstile_signal() or
306 * turnstile_broadcast(). In this case, treat the thread as
307 * if it was already running.
309 if (td->td_turnstile != NULL)
313 * Check if the thread needs to be moved on the blocked chain.
314 * It needs to be moved if either its priority is lower than
315 * the previous thread or higher than the next thread.
317 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
318 td1 = TAILQ_PREV(td, threadqueue, td_lockq);
319 td2 = TAILQ_NEXT(td, td_lockq);
320 if ((td1 != NULL && td->td_priority < td1->td_priority) ||
321 (td2 != NULL && td->td_priority > td2->td_priority)) {
324 * Remove thread from blocked chain and determine where
325 * it should be moved to.
327 queue = td->td_tsqueue;
328 MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
329 mtx_lock_spin(&td_contested_lock);
330 TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
331 TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
332 MPASS(td1->td_proc->p_magic == P_MAGIC);
333 if (td1->td_priority > td->td_priority)
338 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
340 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
341 mtx_unlock_spin(&td_contested_lock);
344 "turnstile_adjust_thread: td %d put at tail on [%p] %s",
345 td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
348 "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
349 td->td_tid, td1->td_tid, ts->ts_lockobj,
350 ts->ts_lockobj->lo_name);
356 * Early initialization of turnstiles. This is not done via a SYSINIT()
357 * since this needs to be initialized very early when mutexes are first
361 init_turnstiles(void)
365 for (i = 0; i < TC_TABLESIZE; i++) {
366 LIST_INIT(&turnstile_chains[i].tc_turnstiles);
367 mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
370 mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
371 LIST_INIT(&thread0.td_contested);
372 thread0.td_turnstile = NULL;
375 #ifdef TURNSTILE_PROFILING
377 init_turnstile_profiling(void *arg)
379 struct sysctl_oid *chain_oid;
383 for (i = 0; i < TC_TABLESIZE; i++) {
384 snprintf(chain_name, sizeof(chain_name), "%d", i);
385 chain_oid = SYSCTL_ADD_NODE(NULL,
386 SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
387 chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
388 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
389 "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
391 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
392 "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
396 SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
397 init_turnstile_profiling, NULL);
401 init_turnstile0(void *dummy)
404 turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
411 turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
412 thread0.td_turnstile = turnstile_alloc();
414 SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
417 * Update a thread on the turnstile list after it's priority has been changed.
418 * The old priority is passed in as an argument.
421 turnstile_adjust(struct thread *td, u_char oldpri)
423 struct turnstile *ts;
425 MPASS(TD_ON_LOCK(td));
428 * Pick up the lock that td is blocked on.
432 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
433 mtx_assert(&ts->ts_lock, MA_OWNED);
435 /* Resort the turnstile on the list. */
436 if (!turnstile_adjust_thread(ts, td))
439 * If our priority was lowered and we are at the head of the
440 * turnstile, then propagate our new priority up the chain.
441 * Note that we currently don't try to revoke lent priorities
442 * when our priority goes up.
444 MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
445 td->td_tsqueue == TS_SHARED_QUEUE);
446 if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
447 td->td_priority < oldpri) {
448 propagate_priority(td);
453 * Set the owner of the lock this turnstile is attached to.
456 turnstile_setowner(struct turnstile *ts, struct thread *owner)
459 mtx_assert(&td_contested_lock, MA_OWNED);
460 MPASS(ts->ts_owner == NULL);
462 /* A shared lock might not have an owner. */
466 MPASS(owner->td_proc->p_magic == P_MAGIC);
467 ts->ts_owner = owner;
468 LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
473 * UMA zone item deallocator.
476 turnstile_dtor(void *mem, int size, void *arg)
478 struct turnstile *ts;
481 MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
482 MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
483 MPASS(TAILQ_EMPTY(&ts->ts_pending));
488 * UMA zone item initializer.
491 turnstile_init(void *mem, int size, int flags)
493 struct turnstile *ts;
497 TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
498 TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
499 TAILQ_INIT(&ts->ts_pending);
500 LIST_INIT(&ts->ts_free);
501 mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN | MTX_RECURSE);
506 turnstile_fini(void *mem, int size)
508 struct turnstile *ts;
511 mtx_destroy(&ts->ts_lock);
515 * Get a turnstile for a new thread.
518 turnstile_alloc(void)
521 return (uma_zalloc(turnstile_zone, M_WAITOK));
525 * Free a turnstile when a thread is destroyed.
528 turnstile_free(struct turnstile *ts)
531 uma_zfree(turnstile_zone, ts);
535 * Lock the turnstile chain associated with the specified lock.
538 turnstile_chain_lock(struct lock_object *lock)
540 struct turnstile_chain *tc;
542 tc = TC_LOOKUP(lock);
543 mtx_lock_spin(&tc->tc_lock);
547 turnstile_trywait(struct lock_object *lock)
549 struct turnstile_chain *tc;
550 struct turnstile *ts;
552 tc = TC_LOOKUP(lock);
553 mtx_lock_spin(&tc->tc_lock);
554 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
555 if (ts->ts_lockobj == lock) {
556 mtx_lock_spin(&ts->ts_lock);
560 ts = curthread->td_turnstile;
562 mtx_lock_spin(&ts->ts_lock);
563 KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
564 ts->ts_lockobj = lock;
570 turnstile_lock(struct turnstile *ts, struct lock_object **lockp,
573 struct turnstile_chain *tc;
574 struct lock_object *lock;
576 if ((lock = ts->ts_lockobj) == NULL)
578 tc = TC_LOOKUP(lock);
579 mtx_lock_spin(&tc->tc_lock);
580 mtx_lock_spin(&ts->ts_lock);
581 if (__predict_false(lock != ts->ts_lockobj)) {
582 mtx_unlock_spin(&tc->tc_lock);
583 mtx_unlock_spin(&ts->ts_lock);
592 turnstile_unlock(struct turnstile *ts, struct lock_object *lock)
594 struct turnstile_chain *tc;
596 mtx_assert(&ts->ts_lock, MA_OWNED);
597 mtx_unlock_spin(&ts->ts_lock);
598 if (ts == curthread->td_turnstile)
599 ts->ts_lockobj = NULL;
600 tc = TC_LOOKUP(lock);
601 mtx_unlock_spin(&tc->tc_lock);
605 turnstile_assert(struct turnstile *ts)
607 MPASS(ts->ts_lockobj == NULL);
611 turnstile_cancel(struct turnstile *ts)
613 struct turnstile_chain *tc;
614 struct lock_object *lock;
616 mtx_assert(&ts->ts_lock, MA_OWNED);
618 mtx_unlock_spin(&ts->ts_lock);
619 lock = ts->ts_lockobj;
620 if (ts == curthread->td_turnstile)
621 ts->ts_lockobj = NULL;
622 tc = TC_LOOKUP(lock);
623 mtx_unlock_spin(&tc->tc_lock);
627 * Look up the turnstile for a lock in the hash table locking the associated
628 * turnstile chain along the way. If no turnstile is found in the hash
629 * table, NULL is returned.
632 turnstile_lookup(struct lock_object *lock)
634 struct turnstile_chain *tc;
635 struct turnstile *ts;
637 tc = TC_LOOKUP(lock);
638 mtx_assert(&tc->tc_lock, MA_OWNED);
639 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
640 if (ts->ts_lockobj == lock) {
641 mtx_lock_spin(&ts->ts_lock);
648 * Unlock the turnstile chain associated with a given lock.
651 turnstile_chain_unlock(struct lock_object *lock)
653 struct turnstile_chain *tc;
655 tc = TC_LOOKUP(lock);
656 mtx_unlock_spin(&tc->tc_lock);
660 * Return a pointer to the thread waiting on this turnstile with the
661 * most important priority or NULL if the turnstile has no waiters.
663 static struct thread *
664 turnstile_first_waiter(struct turnstile *ts)
666 struct thread *std, *xtd;
668 std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]);
669 xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
670 if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority))
676 * Take ownership of a turnstile and adjust the priority of the new
677 * owner appropriately.
680 turnstile_claim(struct turnstile *ts)
682 struct thread *td, *owner;
683 struct turnstile_chain *tc;
685 mtx_assert(&ts->ts_lock, MA_OWNED);
686 MPASS(ts != curthread->td_turnstile);
689 mtx_lock_spin(&td_contested_lock);
690 turnstile_setowner(ts, owner);
691 mtx_unlock_spin(&td_contested_lock);
693 td = turnstile_first_waiter(ts);
695 MPASS(td->td_proc->p_magic == P_MAGIC);
696 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
699 * Update the priority of the new owner if needed.
702 if (td->td_priority < owner->td_priority)
703 sched_lend_prio(owner, td->td_priority);
704 thread_unlock(owner);
705 tc = TC_LOOKUP(ts->ts_lockobj);
706 mtx_unlock_spin(&ts->ts_lock);
707 mtx_unlock_spin(&tc->tc_lock);
711 * Block the current thread on the turnstile assicated with 'lock'. This
712 * function will context switch and not return until this thread has been
713 * woken back up. This function must be called with the appropriate
714 * turnstile chain locked and will return with it unlocked.
717 turnstile_wait(struct turnstile *ts, struct thread *owner, int queue)
719 struct turnstile_chain *tc;
720 struct thread *td, *td1;
721 struct lock_object *lock;
724 mtx_assert(&ts->ts_lock, MA_OWNED);
726 MPASS(owner->td_proc->p_magic == P_MAGIC);
727 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
730 * If the lock does not already have a turnstile, use this thread's
731 * turnstile. Otherwise insert the current thread into the
732 * turnstile already in use by this lock.
734 tc = TC_LOOKUP(ts->ts_lockobj);
735 mtx_assert(&tc->tc_lock, MA_OWNED);
736 if (ts == td->td_turnstile) {
737 #ifdef TURNSTILE_PROFILING
739 if (tc->tc_depth > tc->tc_max_depth) {
740 tc->tc_max_depth = tc->tc_depth;
741 if (tc->tc_max_depth > turnstile_max_depth)
742 turnstile_max_depth = tc->tc_max_depth;
745 LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
746 KASSERT(TAILQ_EMPTY(&ts->ts_pending),
747 ("thread's turnstile has pending threads"));
748 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
749 ("thread's turnstile has exclusive waiters"));
750 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
751 ("thread's turnstile has shared waiters"));
752 KASSERT(LIST_EMPTY(&ts->ts_free),
753 ("thread's turnstile has a non-empty free list"));
754 MPASS(ts->ts_lockobj != NULL);
755 mtx_lock_spin(&td_contested_lock);
756 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
757 turnstile_setowner(ts, owner);
758 mtx_unlock_spin(&td_contested_lock);
760 TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
761 if (td1->td_priority > td->td_priority)
763 mtx_lock_spin(&td_contested_lock);
765 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
767 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
768 MPASS(owner == ts->ts_owner);
769 mtx_unlock_spin(&td_contested_lock);
770 MPASS(td->td_turnstile != NULL);
771 LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
774 thread_lock_set(td, &ts->ts_lock);
775 td->td_turnstile = NULL;
777 /* Save who we are blocked on and switch. */
778 lock = ts->ts_lockobj;
779 td->td_tsqueue = queue;
781 td->td_lockname = lock->lo_name;
782 td->td_blktick = ticks;
784 mtx_unlock_spin(&tc->tc_lock);
785 propagate_priority(td);
787 if (LOCK_LOG_TEST(lock, 0))
788 CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
789 td->td_tid, lock, lock->lo_name);
791 SDT_PROBE0(sched, , , sleep);
793 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
794 mi_switch(SW_VOL | SWT_TURNSTILE, NULL);
796 if (LOCK_LOG_TEST(lock, 0))
797 CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
798 __func__, td->td_tid, lock, lock->lo_name);
803 * Pick the highest priority thread on this turnstile and put it on the
804 * pending list. This must be called with the turnstile chain locked.
807 turnstile_signal(struct turnstile *ts, int queue)
809 struct turnstile_chain *tc __unused;
814 mtx_assert(&ts->ts_lock, MA_OWNED);
815 MPASS(curthread->td_proc->p_magic == P_MAGIC);
816 MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
817 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
820 * Pick the highest priority thread blocked on this lock and
821 * move it to the pending list.
823 td = TAILQ_FIRST(&ts->ts_blocked[queue]);
824 MPASS(td->td_proc->p_magic == P_MAGIC);
825 mtx_lock_spin(&td_contested_lock);
826 TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
827 mtx_unlock_spin(&td_contested_lock);
828 TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
831 * If the turnstile is now empty, remove it from its chain and
832 * give it to the about-to-be-woken thread. Otherwise take a
833 * turnstile from the free list and give it to the thread.
835 empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
836 TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
838 tc = TC_LOOKUP(ts->ts_lockobj);
839 mtx_assert(&tc->tc_lock, MA_OWNED);
840 MPASS(LIST_EMPTY(&ts->ts_free));
841 #ifdef TURNSTILE_PROFILING
845 ts = LIST_FIRST(&ts->ts_free);
847 LIST_REMOVE(ts, ts_hash);
848 td->td_turnstile = ts;
854 * Put all blocked threads on the pending list. This must be called with
855 * the turnstile chain locked.
858 turnstile_broadcast(struct turnstile *ts, int queue)
860 struct turnstile_chain *tc __unused;
861 struct turnstile *ts1;
865 mtx_assert(&ts->ts_lock, MA_OWNED);
866 MPASS(curthread->td_proc->p_magic == P_MAGIC);
867 MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
869 * We must have the chain locked so that we can remove the empty
870 * turnstile from the hash queue.
872 tc = TC_LOOKUP(ts->ts_lockobj);
873 mtx_assert(&tc->tc_lock, MA_OWNED);
874 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
877 * Transfer the blocked list to the pending list.
879 mtx_lock_spin(&td_contested_lock);
880 TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
881 mtx_unlock_spin(&td_contested_lock);
884 * Give a turnstile to each thread. The last thread gets
885 * this turnstile if the turnstile is empty.
887 TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
888 if (LIST_EMPTY(&ts->ts_free)) {
889 MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
891 #ifdef TURNSTILE_PROFILING
895 ts1 = LIST_FIRST(&ts->ts_free);
897 LIST_REMOVE(ts1, ts_hash);
898 td->td_turnstile = ts1;
903 turnstile_calc_unlend_prio_locked(struct thread *td)
905 struct turnstile *nts;
908 THREAD_LOCK_ASSERT(td, MA_OWNED);
909 mtx_assert(&td_contested_lock, MA_OWNED);
912 LIST_FOREACH(nts, &td->td_contested, ts_link) {
913 cp = turnstile_first_waiter(nts)->td_priority;
921 * Wakeup all threads on the pending list and adjust the priority of the
922 * current thread appropriately. This must be called with the turnstile
926 turnstile_unpend(struct turnstile *ts)
928 TAILQ_HEAD( ,thread) pending_threads;
933 mtx_assert(&ts->ts_lock, MA_OWNED);
934 MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
935 MPASS(!TAILQ_EMPTY(&ts->ts_pending));
938 * Move the list of pending threads out of the turnstile and
939 * into a local variable.
941 TAILQ_INIT(&pending_threads);
942 TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
944 if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
945 TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
946 ts->ts_lockobj = NULL;
949 * Adjust the priority of curthread based on other contested
950 * locks it owns. Don't lower the priority below the base
955 mtx_lock_spin(&td_contested_lock);
957 * Remove the turnstile from this thread's list of contested locks
958 * since this thread doesn't own it anymore. New threads will
959 * not be blocking on the turnstile until it is claimed by a new
960 * owner. There might not be a current owner if this is a shared
963 if (ts->ts_owner != NULL) {
965 LIST_REMOVE(ts, ts_link);
967 pri = turnstile_calc_unlend_prio_locked(td);
968 mtx_unlock_spin(&td_contested_lock);
969 sched_unlend_prio(td, pri);
972 * Wake up all the pending threads. If a thread is not blocked
973 * on a lock, then it is currently executing on another CPU in
974 * turnstile_wait() or sitting on a run queue waiting to resume
975 * in turnstile_wait(). Set a flag to force it to try to acquire
976 * the lock again instead of blocking.
978 while (!TAILQ_EMPTY(&pending_threads)) {
979 td = TAILQ_FIRST(&pending_threads);
980 TAILQ_REMOVE(&pending_threads, td, td_lockq);
981 SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
983 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
984 MPASS(td->td_proc->p_magic == P_MAGIC);
985 MPASS(TD_ON_LOCK(td));
987 MPASS(TD_CAN_RUN(td));
988 td->td_blocked = NULL;
989 td->td_lockname = NULL;
992 td->td_tsqueue = 0xff;
994 sched_add(td, SRQ_BORING);
997 mtx_unlock_spin(&ts->ts_lock);
1001 * Give up ownership of a turnstile. This must be called with the
1002 * turnstile chain locked.
1005 turnstile_disown(struct turnstile *ts)
1011 mtx_assert(&ts->ts_lock, MA_OWNED);
1012 MPASS(ts->ts_owner == curthread);
1013 MPASS(TAILQ_EMPTY(&ts->ts_pending));
1014 MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
1015 !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
1018 * Remove the turnstile from this thread's list of contested locks
1019 * since this thread doesn't own it anymore. New threads will
1020 * not be blocking on the turnstile until it is claimed by a new
1023 mtx_lock_spin(&td_contested_lock);
1024 ts->ts_owner = NULL;
1025 LIST_REMOVE(ts, ts_link);
1026 mtx_unlock_spin(&td_contested_lock);
1029 * Adjust the priority of curthread based on other contested
1030 * locks it owns. Don't lower the priority below the base
1035 mtx_unlock_spin(&ts->ts_lock);
1036 mtx_lock_spin(&td_contested_lock);
1037 pri = turnstile_calc_unlend_prio_locked(td);
1038 mtx_unlock_spin(&td_contested_lock);
1039 sched_unlend_prio(td, pri);
1044 * Return the first thread in a turnstile.
1047 turnstile_head(struct turnstile *ts, int queue)
1052 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1053 mtx_assert(&ts->ts_lock, MA_OWNED);
1055 return (TAILQ_FIRST(&ts->ts_blocked[queue]));
1059 * Returns true if a sub-queue of a turnstile is empty.
1062 turnstile_empty(struct turnstile *ts, int queue)
1067 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1068 mtx_assert(&ts->ts_lock, MA_OWNED);
1070 return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
1075 print_thread(struct thread *td, const char *prefix)
1078 db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid,
1079 td->td_proc->p_pid, td->td_name);
1083 print_queue(struct threadqueue *queue, const char *header, const char *prefix)
1087 db_printf("%s:\n", header);
1088 if (TAILQ_EMPTY(queue)) {
1089 db_printf("%sempty\n", prefix);
1092 TAILQ_FOREACH(td, queue, td_lockq) {
1093 print_thread(td, prefix);
1097 DB_SHOW_COMMAND(turnstile, db_show_turnstile)
1099 struct turnstile_chain *tc;
1100 struct turnstile *ts;
1101 struct lock_object *lock;
1108 * First, see if there is an active turnstile for the lock indicated
1111 lock = (struct lock_object *)addr;
1112 tc = TC_LOOKUP(lock);
1113 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1114 if (ts->ts_lockobj == lock)
1118 * Second, see if there is an active turnstile at the address
1121 for (i = 0; i < TC_TABLESIZE; i++)
1122 LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) {
1123 if (ts == (struct turnstile *)addr)
1127 db_printf("Unable to locate a turnstile via %p\n", (void *)addr);
1130 lock = ts->ts_lockobj;
1131 db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name,
1134 print_thread(ts->ts_owner, "Lock Owner: ");
1136 db_printf("Lock Owner: none\n");
1137 print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t");
1138 print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters",
1140 print_queue(&ts->ts_pending, "Pending Threads", "\t");
1145 * Show all the threads a particular thread is waiting on based on
1149 print_lockchain(struct thread *td, const char *prefix)
1151 struct lock_object *lock;
1152 struct lock_class *class;
1153 struct turnstile *ts;
1154 struct thread *owner;
1157 * Follow the chain. We keep walking as long as the thread is
1158 * blocked on a lock that has an owner.
1160 while (!db_pager_quit) {
1161 db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1162 td->td_proc->p_pid, td->td_name);
1163 switch (td->td_state) {
1165 db_printf("is inactive\n");
1168 db_printf("can run\n");
1171 db_printf("is on a run queue\n");
1174 db_printf("running on CPU %d\n", td->td_oncpu);
1177 if (TD_ON_LOCK(td)) {
1178 ts = td->td_blocked;
1179 lock = ts->ts_lockobj;
1180 class = LOCK_CLASS(lock);
1181 db_printf("blocked on lock %p (%s) \"%s\"\n",
1182 lock, class->lc_name, lock->lo_name);
1183 if (ts->ts_owner == NULL)
1187 } else if (TD_ON_SLEEPQ(td)) {
1188 if (!lockmgr_chain(td, &owner) &&
1189 !sx_chain(td, &owner)) {
1190 db_printf("sleeping on %p \"%s\"\n",
1191 td->td_wchan, td->td_wmesg);
1199 db_printf("inhibited\n");
1202 db_printf("??? (%#x)\n", td->td_state);
1208 DB_SHOW_COMMAND(lockchain, db_show_lockchain)
1212 /* Figure out which thread to start with. */
1214 td = db_lookup_thread(addr, true);
1218 print_lockchain(td, "");
1220 DB_SHOW_ALIAS(sleepchain, db_show_lockchain);
1222 DB_SHOW_ALL_COMMAND(chains, db_show_allchains)
1229 FOREACH_PROC_IN_SYSTEM(p) {
1230 FOREACH_THREAD_IN_PROC(p, td) {
1231 if ((TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested))
1232 || (TD_IS_INHIBITED(td) && TD_ON_SLEEPQ(td))) {
1233 db_printf("chain %d:\n", i++);
1234 print_lockchain(td, " ");
1241 DB_SHOW_ALIAS(allchains, db_show_allchains)
1243 static void print_waiters(struct turnstile *ts, int indent);
1246 print_waiter(struct thread *td, int indent)
1248 struct turnstile *ts;
1253 for (i = 0; i < indent; i++)
1255 print_thread(td, "thread ");
1256 LIST_FOREACH(ts, &td->td_contested, ts_link)
1257 print_waiters(ts, indent + 1);
1261 print_waiters(struct turnstile *ts, int indent)
1263 struct lock_object *lock;
1264 struct lock_class *class;
1270 lock = ts->ts_lockobj;
1271 class = LOCK_CLASS(lock);
1272 for (i = 0; i < indent; i++)
1274 db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
1275 TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
1276 print_waiter(td, indent + 1);
1277 TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
1278 print_waiter(td, indent + 1);
1279 TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
1280 print_waiter(td, indent + 1);
1283 DB_SHOW_COMMAND(locktree, db_show_locktree)
1285 struct lock_object *lock;
1286 struct lock_class *class;
1287 struct turnstile_chain *tc;
1288 struct turnstile *ts;
1292 lock = (struct lock_object *)addr;
1293 tc = TC_LOOKUP(lock);
1294 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1295 if (ts->ts_lockobj == lock)
1298 class = LOCK_CLASS(lock);
1299 db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
1302 print_waiters(ts, 0);