2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 * promote products derived from this software without specific prior
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
29 * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
33 * Implementation of turnstiles used to hold queue of threads blocked on
34 * non-sleepable locks. Sleepable locks use condition variables to
35 * implement their queues. Turnstiles differ from a sleep queue in that
36 * turnstile queue's are assigned to a lock held by an owning thread. Thus,
37 * when one thread is enqueued onto a turnstile, it can lend its priority
38 * to the owning thread.
40 * We wish to avoid bloating locks with an embedded turnstile and we do not
41 * want to use back-pointers in the locks for the same reason. Thus, we
42 * use a similar approach to that of Solaris 7 as described in Solaris
43 * Internals by Jim Mauro and Richard McDougall. Turnstiles are looked up
44 * in a hash table based on the address of the lock. Each entry in the
45 * hash table is a linked-lists of turnstiles and is called a turnstile
46 * chain. Each chain contains a spin mutex that protects all of the
47 * turnstiles in the chain.
49 * Each time a thread is created, a turnstile is allocated from a UMA zone
50 * and attached to that thread. When a thread blocks on a lock, if it is the
51 * first thread to block, it lends its turnstile to the lock. If the lock
52 * already has a turnstile, then it gives its turnstile to the lock's
53 * turnstile's free list. When a thread is woken up, it takes a turnstile from
54 * the free list if there are any other waiters. If it is the only thread
55 * blocked on the lock, then it reclaims the turnstile associated with the lock
56 * and removes it from the hash table.
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
63 #include "opt_turnstile_profiling.h"
64 #include "opt_sched.h"
66 #include <sys/param.h>
67 #include <sys/systm.h>
69 #include <sys/kernel.h>
72 #include <sys/mutex.h>
74 #include <sys/queue.h>
75 #include <sys/sched.h>
77 #include <sys/sysctl.h>
78 #include <sys/turnstile.h>
84 #include <sys/lockmgr.h>
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.
94 #define TC_TABLESIZE 128 /* Must be power of 2. */
95 #define TC_MASK (TC_TABLESIZE - 1)
97 #define TC_HASH(lock) (((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK)
98 #define TC_LOOKUP(lock) &turnstile_chains[TC_HASH(lock)]
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.
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
117 * c - turnstile chain lock
118 * q - td_contested lock
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. */
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. */
140 #ifdef TURNSTILE_PROFILING
141 u_int turnstile_max_depth;
142 static SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0,
143 "turnstile profiling");
144 static SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0,
145 "turnstile chain stats");
146 SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD,
147 &turnstile_max_depth, 0, "maximum depth achieved of a single chain");
149 static struct mtx td_contested_lock;
150 static struct turnstile_chain turnstile_chains[TC_TABLESIZE];
151 static uma_zone_t turnstile_zone;
154 * Prototypes for non-exported routines.
156 static void init_turnstile0(void *dummy);
157 #ifdef TURNSTILE_PROFILING
158 static void init_turnstile_profiling(void *arg);
161 static void print_sleepchain(struct thread *td, const char *prefix);
163 static void propagate_priority(struct thread *td);
164 static int turnstile_adjust_thread(struct turnstile *ts,
166 static struct thread *turnstile_first_waiter(struct turnstile *ts);
167 static void turnstile_setowner(struct turnstile *ts, struct thread *owner);
169 static void turnstile_dtor(void *mem, int size, void *arg);
171 static int turnstile_init(void *mem, int size, int flags);
172 static void turnstile_fini(void *mem, int size);
174 SDT_PROVIDER_DECLARE(sched);
175 SDT_PROBE_DEFINE(sched, , , sleep);
176 SDT_PROBE_DEFINE2(sched, , , wakeup, "struct thread *",
180 * Walks the chain of turnstiles and their owners to propagate the priority
181 * of the thread being blocked to all the threads holding locks that have to
182 * release their locks before this thread can run again.
185 propagate_priority(struct thread *td)
187 struct turnstile *ts;
190 THREAD_LOCK_ASSERT(td, MA_OWNED);
191 pri = td->td_priority;
193 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
195 * Grab a recursive lock on this turnstile chain so it stays locked
196 * for the whole operation. The caller expects us to return with
197 * the original lock held. We only ever lock down the chain so
198 * the lock order is constant.
200 mtx_lock_spin(&ts->ts_lock);
206 * This might be a read lock with no owner. There's
207 * not much we can do, so just bail.
209 mtx_unlock_spin(&ts->ts_lock);
213 thread_lock_flags(td, MTX_DUPOK);
214 mtx_unlock_spin(&ts->ts_lock);
215 MPASS(td->td_proc != NULL);
216 MPASS(td->td_proc->p_magic == P_MAGIC);
219 * If the thread is asleep, then we are probably about
220 * to deadlock. To make debugging this easier, show
221 * backtrace of misbehaving thread and panic to not
222 * leave the kernel deadlocked.
224 if (TD_IS_SLEEPING(td)) {
226 "Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n",
227 td->td_tid, td->td_proc->p_pid);
228 kdb_backtrace_thread(td);
229 panic("sleeping thread");
233 * If this thread already has higher priority than the
234 * thread that is being blocked, we are finished.
236 if (td->td_priority <= pri) {
242 * Bump this thread's priority.
244 sched_lend_prio(td, pri);
247 * If lock holder is actually running or on the run queue
250 if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) {
251 MPASS(td->td_blocked == NULL);
258 * For UP, we check to see if td is curthread (this shouldn't
259 * ever happen however as it would mean we are in a deadlock.)
261 KASSERT(td != curthread, ("Deadlock detected"));
265 * If we aren't blocked on a lock, we should be.
267 KASSERT(TD_ON_LOCK(td), (
268 "thread %d(%s):%d holds %s but isn't blocked on a lock\n",
269 td->td_tid, td->td_name, td->td_state,
270 ts->ts_lockobj->lo_name));
273 * Pick up the lock that td is blocked on.
277 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
278 /* Resort td on the list if needed. */
279 if (!turnstile_adjust_thread(ts, td)) {
280 mtx_unlock_spin(&ts->ts_lock);
283 /* The thread lock is released as ts lock above. */
288 * Adjust the thread's position on a turnstile after its priority has been
292 turnstile_adjust_thread(struct turnstile *ts, struct thread *td)
294 struct thread *td1, *td2;
297 THREAD_LOCK_ASSERT(td, MA_OWNED);
298 MPASS(TD_ON_LOCK(td));
301 * This thread may not be blocked on this turnstile anymore
302 * but instead might already be woken up on another CPU
303 * that is waiting on the thread lock in turnstile_unpend() to
304 * finish waking this thread up. We can detect this case
305 * by checking to see if this thread has been given a
306 * turnstile by either turnstile_signal() or
307 * turnstile_broadcast(). In this case, treat the thread as
308 * if it was already running.
310 if (td->td_turnstile != NULL)
314 * Check if the thread needs to be moved on the blocked chain.
315 * It needs to be moved if either its priority is lower than
316 * the previous thread or higher than the next thread.
318 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
319 td1 = TAILQ_PREV(td, threadqueue, td_lockq);
320 td2 = TAILQ_NEXT(td, td_lockq);
321 if ((td1 != NULL && td->td_priority < td1->td_priority) ||
322 (td2 != NULL && td->td_priority > td2->td_priority)) {
325 * Remove thread from blocked chain and determine where
326 * it should be moved to.
328 queue = td->td_tsqueue;
329 MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE);
330 mtx_lock_spin(&td_contested_lock);
331 TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
332 TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) {
333 MPASS(td1->td_proc->p_magic == P_MAGIC);
334 if (td1->td_priority > td->td_priority)
339 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
341 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
342 mtx_unlock_spin(&td_contested_lock);
345 "turnstile_adjust_thread: td %d put at tail on [%p] %s",
346 td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name);
349 "turnstile_adjust_thread: td %d moved before %d on [%p] %s",
350 td->td_tid, td1->td_tid, ts->ts_lockobj,
351 ts->ts_lockobj->lo_name);
357 * Early initialization of turnstiles. This is not done via a SYSINIT()
358 * since this needs to be initialized very early when mutexes are first
362 init_turnstiles(void)
366 for (i = 0; i < TC_TABLESIZE; i++) {
367 LIST_INIT(&turnstile_chains[i].tc_turnstiles);
368 mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain",
371 mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN);
372 LIST_INIT(&thread0.td_contested);
373 thread0.td_turnstile = NULL;
376 #ifdef TURNSTILE_PROFILING
378 init_turnstile_profiling(void *arg)
380 struct sysctl_oid *chain_oid;
384 for (i = 0; i < TC_TABLESIZE; i++) {
385 snprintf(chain_name, sizeof(chain_name), "%d", i);
386 chain_oid = SYSCTL_ADD_NODE(NULL,
387 SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO,
388 chain_name, CTLFLAG_RD, NULL, "turnstile chain stats");
389 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
390 "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0,
392 SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO,
393 "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth,
397 SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY,
398 init_turnstile_profiling, NULL);
402 init_turnstile0(void *dummy)
405 turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile),
412 turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE);
413 thread0.td_turnstile = turnstile_alloc();
415 SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL);
418 * Update a thread on the turnstile list after it's priority has been changed.
419 * The old priority is passed in as an argument.
422 turnstile_adjust(struct thread *td, u_char oldpri)
424 struct turnstile *ts;
426 MPASS(TD_ON_LOCK(td));
429 * Pick up the lock that td is blocked on.
433 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
434 mtx_assert(&ts->ts_lock, MA_OWNED);
436 /* Resort the turnstile on the list. */
437 if (!turnstile_adjust_thread(ts, td))
440 * If our priority was lowered and we are at the head of the
441 * turnstile, then propagate our new priority up the chain.
442 * Note that we currently don't try to revoke lent priorities
443 * when our priority goes up.
445 MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE ||
446 td->td_tsqueue == TS_SHARED_QUEUE);
447 if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) &&
448 td->td_priority < oldpri) {
449 propagate_priority(td);
454 * Set the owner of the lock this turnstile is attached to.
457 turnstile_setowner(struct turnstile *ts, struct thread *owner)
460 mtx_assert(&td_contested_lock, MA_OWNED);
461 MPASS(ts->ts_owner == NULL);
463 /* A shared lock might not have an owner. */
467 MPASS(owner->td_proc->p_magic == P_MAGIC);
468 ts->ts_owner = owner;
469 LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link);
474 * UMA zone item deallocator.
477 turnstile_dtor(void *mem, int size, void *arg)
479 struct turnstile *ts;
482 MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]));
483 MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
484 MPASS(TAILQ_EMPTY(&ts->ts_pending));
489 * UMA zone item initializer.
492 turnstile_init(void *mem, int size, int flags)
494 struct turnstile *ts;
498 TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
499 TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]);
500 TAILQ_INIT(&ts->ts_pending);
501 LIST_INIT(&ts->ts_free);
502 mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN | MTX_RECURSE);
507 turnstile_fini(void *mem, int size)
509 struct turnstile *ts;
512 mtx_destroy(&ts->ts_lock);
516 * Get a turnstile for a new thread.
519 turnstile_alloc(void)
522 return (uma_zalloc(turnstile_zone, M_WAITOK));
526 * Free a turnstile when a thread is destroyed.
529 turnstile_free(struct turnstile *ts)
532 uma_zfree(turnstile_zone, ts);
536 * Lock the turnstile chain associated with the specified lock.
539 turnstile_chain_lock(struct lock_object *lock)
541 struct turnstile_chain *tc;
543 tc = TC_LOOKUP(lock);
544 mtx_lock_spin(&tc->tc_lock);
548 turnstile_trywait(struct lock_object *lock)
550 struct turnstile_chain *tc;
551 struct turnstile *ts;
553 tc = TC_LOOKUP(lock);
554 mtx_lock_spin(&tc->tc_lock);
555 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
556 if (ts->ts_lockobj == lock) {
557 mtx_lock_spin(&ts->ts_lock);
561 ts = curthread->td_turnstile;
563 mtx_lock_spin(&ts->ts_lock);
564 KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer"));
565 ts->ts_lockobj = lock;
571 turnstile_cancel(struct turnstile *ts)
573 struct turnstile_chain *tc;
574 struct lock_object *lock;
576 mtx_assert(&ts->ts_lock, MA_OWNED);
578 mtx_unlock_spin(&ts->ts_lock);
579 lock = ts->ts_lockobj;
580 if (ts == curthread->td_turnstile)
581 ts->ts_lockobj = NULL;
582 tc = TC_LOOKUP(lock);
583 mtx_unlock_spin(&tc->tc_lock);
587 * Look up the turnstile for a lock in the hash table locking the associated
588 * turnstile chain along the way. If no turnstile is found in the hash
589 * table, NULL is returned.
592 turnstile_lookup(struct lock_object *lock)
594 struct turnstile_chain *tc;
595 struct turnstile *ts;
597 tc = TC_LOOKUP(lock);
598 mtx_assert(&tc->tc_lock, MA_OWNED);
599 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
600 if (ts->ts_lockobj == lock) {
601 mtx_lock_spin(&ts->ts_lock);
608 * Unlock the turnstile chain associated with a given lock.
611 turnstile_chain_unlock(struct lock_object *lock)
613 struct turnstile_chain *tc;
615 tc = TC_LOOKUP(lock);
616 mtx_unlock_spin(&tc->tc_lock);
620 * Return a pointer to the thread waiting on this turnstile with the
621 * most important priority or NULL if the turnstile has no waiters.
623 static struct thread *
624 turnstile_first_waiter(struct turnstile *ts)
626 struct thread *std, *xtd;
628 std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]);
629 xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]);
630 if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority))
636 * Take ownership of a turnstile and adjust the priority of the new
637 * owner appropriately.
640 turnstile_claim(struct turnstile *ts)
642 struct thread *td, *owner;
643 struct turnstile_chain *tc;
645 mtx_assert(&ts->ts_lock, MA_OWNED);
646 MPASS(ts != curthread->td_turnstile);
649 mtx_lock_spin(&td_contested_lock);
650 turnstile_setowner(ts, owner);
651 mtx_unlock_spin(&td_contested_lock);
653 td = turnstile_first_waiter(ts);
655 MPASS(td->td_proc->p_magic == P_MAGIC);
656 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
659 * Update the priority of the new owner if needed.
662 if (td->td_priority < owner->td_priority)
663 sched_lend_prio(owner, td->td_priority);
664 thread_unlock(owner);
665 tc = TC_LOOKUP(ts->ts_lockobj);
666 mtx_unlock_spin(&ts->ts_lock);
667 mtx_unlock_spin(&tc->tc_lock);
671 * Block the current thread on the turnstile assicated with 'lock'. This
672 * function will context switch and not return until this thread has been
673 * woken back up. This function must be called with the appropriate
674 * turnstile chain locked and will return with it unlocked.
677 turnstile_wait(struct turnstile *ts, struct thread *owner, int queue)
679 struct turnstile_chain *tc;
680 struct thread *td, *td1;
681 struct lock_object *lock;
684 mtx_assert(&ts->ts_lock, MA_OWNED);
686 MPASS(owner->td_proc->p_magic == P_MAGIC);
687 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
690 * If the lock does not already have a turnstile, use this thread's
691 * turnstile. Otherwise insert the current thread into the
692 * turnstile already in use by this lock.
694 tc = TC_LOOKUP(ts->ts_lockobj);
695 mtx_assert(&tc->tc_lock, MA_OWNED);
696 if (ts == td->td_turnstile) {
697 #ifdef TURNSTILE_PROFILING
699 if (tc->tc_depth > tc->tc_max_depth) {
700 tc->tc_max_depth = tc->tc_depth;
701 if (tc->tc_max_depth > turnstile_max_depth)
702 turnstile_max_depth = tc->tc_max_depth;
705 LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash);
706 KASSERT(TAILQ_EMPTY(&ts->ts_pending),
707 ("thread's turnstile has pending threads"));
708 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]),
709 ("thread's turnstile has exclusive waiters"));
710 KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]),
711 ("thread's turnstile has shared waiters"));
712 KASSERT(LIST_EMPTY(&ts->ts_free),
713 ("thread's turnstile has a non-empty free list"));
714 MPASS(ts->ts_lockobj != NULL);
715 mtx_lock_spin(&td_contested_lock);
716 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
717 turnstile_setowner(ts, owner);
718 mtx_unlock_spin(&td_contested_lock);
720 TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq)
721 if (td1->td_priority > td->td_priority)
723 mtx_lock_spin(&td_contested_lock);
725 TAILQ_INSERT_BEFORE(td1, td, td_lockq);
727 TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq);
728 MPASS(owner == ts->ts_owner);
729 mtx_unlock_spin(&td_contested_lock);
730 MPASS(td->td_turnstile != NULL);
731 LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash);
734 thread_lock_set(td, &ts->ts_lock);
735 td->td_turnstile = NULL;
737 /* Save who we are blocked on and switch. */
738 lock = ts->ts_lockobj;
739 td->td_tsqueue = queue;
741 td->td_lockname = lock->lo_name;
742 td->td_blktick = ticks;
744 mtx_unlock_spin(&tc->tc_lock);
745 propagate_priority(td);
747 if (LOCK_LOG_TEST(lock, 0))
748 CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__,
749 td->td_tid, lock, lock->lo_name);
751 SDT_PROBE0(sched, , , sleep);
753 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
754 mi_switch(SW_VOL | SWT_TURNSTILE, NULL);
756 if (LOCK_LOG_TEST(lock, 0))
757 CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s",
758 __func__, td->td_tid, lock, lock->lo_name);
763 * Pick the highest priority thread on this turnstile and put it on the
764 * pending list. This must be called with the turnstile chain locked.
767 turnstile_signal(struct turnstile *ts, int queue)
769 struct turnstile_chain *tc;
774 mtx_assert(&ts->ts_lock, MA_OWNED);
775 MPASS(curthread->td_proc->p_magic == P_MAGIC);
776 MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
777 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
780 * Pick the highest priority thread blocked on this lock and
781 * move it to the pending list.
783 td = TAILQ_FIRST(&ts->ts_blocked[queue]);
784 MPASS(td->td_proc->p_magic == P_MAGIC);
785 mtx_lock_spin(&td_contested_lock);
786 TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq);
787 mtx_unlock_spin(&td_contested_lock);
788 TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq);
791 * If the turnstile is now empty, remove it from its chain and
792 * give it to the about-to-be-woken thread. Otherwise take a
793 * turnstile from the free list and give it to the thread.
795 empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
796 TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]);
798 tc = TC_LOOKUP(ts->ts_lockobj);
799 mtx_assert(&tc->tc_lock, MA_OWNED);
800 MPASS(LIST_EMPTY(&ts->ts_free));
801 #ifdef TURNSTILE_PROFILING
805 ts = LIST_FIRST(&ts->ts_free);
807 LIST_REMOVE(ts, ts_hash);
808 td->td_turnstile = ts;
814 * Put all blocked threads on the pending list. This must be called with
815 * the turnstile chain locked.
818 turnstile_broadcast(struct turnstile *ts, int queue)
820 struct turnstile_chain *tc;
821 struct turnstile *ts1;
825 mtx_assert(&ts->ts_lock, MA_OWNED);
826 MPASS(curthread->td_proc->p_magic == P_MAGIC);
827 MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
829 * We must have the chain locked so that we can remove the empty
830 * turnstile from the hash queue.
832 tc = TC_LOOKUP(ts->ts_lockobj);
833 mtx_assert(&tc->tc_lock, MA_OWNED);
834 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
837 * Transfer the blocked list to the pending list.
839 mtx_lock_spin(&td_contested_lock);
840 TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq);
841 mtx_unlock_spin(&td_contested_lock);
844 * Give a turnstile to each thread. The last thread gets
845 * this turnstile if the turnstile is empty.
847 TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) {
848 if (LIST_EMPTY(&ts->ts_free)) {
849 MPASS(TAILQ_NEXT(td, td_lockq) == NULL);
851 #ifdef TURNSTILE_PROFILING
855 ts1 = LIST_FIRST(&ts->ts_free);
857 LIST_REMOVE(ts1, ts_hash);
858 td->td_turnstile = ts1;
863 * Wakeup all threads on the pending list and adjust the priority of the
864 * current thread appropriately. This must be called with the turnstile
868 turnstile_unpend(struct turnstile *ts, int owner_type)
870 TAILQ_HEAD( ,thread) pending_threads;
871 struct turnstile *nts;
876 mtx_assert(&ts->ts_lock, MA_OWNED);
877 MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL);
878 MPASS(!TAILQ_EMPTY(&ts->ts_pending));
881 * Move the list of pending threads out of the turnstile and
882 * into a local variable.
884 TAILQ_INIT(&pending_threads);
885 TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq);
887 if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) &&
888 TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]))
889 ts->ts_lockobj = NULL;
892 * Adjust the priority of curthread based on other contested
893 * locks it owns. Don't lower the priority below the base
899 mtx_lock_spin(&td_contested_lock);
901 * Remove the turnstile from this thread's list of contested locks
902 * since this thread doesn't own it anymore. New threads will
903 * not be blocking on the turnstile until it is claimed by a new
904 * owner. There might not be a current owner if this is a shared
907 if (ts->ts_owner != NULL) {
909 LIST_REMOVE(ts, ts_link);
911 LIST_FOREACH(nts, &td->td_contested, ts_link) {
912 cp = turnstile_first_waiter(nts)->td_priority;
916 mtx_unlock_spin(&td_contested_lock);
917 sched_unlend_prio(td, pri);
920 * Wake up all the pending threads. If a thread is not blocked
921 * on a lock, then it is currently executing on another CPU in
922 * turnstile_wait() or sitting on a run queue waiting to resume
923 * in turnstile_wait(). Set a flag to force it to try to acquire
924 * the lock again instead of blocking.
926 while (!TAILQ_EMPTY(&pending_threads)) {
927 td = TAILQ_FIRST(&pending_threads);
928 TAILQ_REMOVE(&pending_threads, td, td_lockq);
929 SDT_PROBE2(sched, , , wakeup, td, td->td_proc);
931 THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock);
932 MPASS(td->td_proc->p_magic == P_MAGIC);
933 MPASS(TD_ON_LOCK(td));
935 MPASS(TD_CAN_RUN(td));
936 td->td_blocked = NULL;
937 td->td_lockname = NULL;
940 td->td_tsqueue = 0xff;
942 sched_add(td, SRQ_BORING);
945 mtx_unlock_spin(&ts->ts_lock);
949 * Give up ownership of a turnstile. This must be called with the
950 * turnstile chain locked.
953 turnstile_disown(struct turnstile *ts)
959 mtx_assert(&ts->ts_lock, MA_OWNED);
960 MPASS(ts->ts_owner == curthread);
961 MPASS(TAILQ_EMPTY(&ts->ts_pending));
962 MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) ||
963 !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]));
966 * Remove the turnstile from this thread's list of contested locks
967 * since this thread doesn't own it anymore. New threads will
968 * not be blocking on the turnstile until it is claimed by a new
971 mtx_lock_spin(&td_contested_lock);
973 LIST_REMOVE(ts, ts_link);
974 mtx_unlock_spin(&td_contested_lock);
977 * Adjust the priority of curthread based on other contested
978 * locks it owns. Don't lower the priority below the base
984 mtx_unlock_spin(&ts->ts_lock);
985 mtx_lock_spin(&td_contested_lock);
986 LIST_FOREACH(ts, &td->td_contested, ts_link) {
987 cp = turnstile_first_waiter(ts)->td_priority;
991 mtx_unlock_spin(&td_contested_lock);
992 sched_unlend_prio(td, pri);
997 * Return the first thread in a turnstile.
1000 turnstile_head(struct turnstile *ts, int queue)
1005 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1006 mtx_assert(&ts->ts_lock, MA_OWNED);
1008 return (TAILQ_FIRST(&ts->ts_blocked[queue]));
1012 * Returns true if a sub-queue of a turnstile is empty.
1015 turnstile_empty(struct turnstile *ts, int queue)
1020 MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE);
1021 mtx_assert(&ts->ts_lock, MA_OWNED);
1023 return (TAILQ_EMPTY(&ts->ts_blocked[queue]));
1028 print_thread(struct thread *td, const char *prefix)
1031 db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid,
1032 td->td_proc->p_pid, td->td_name);
1036 print_queue(struct threadqueue *queue, const char *header, const char *prefix)
1040 db_printf("%s:\n", header);
1041 if (TAILQ_EMPTY(queue)) {
1042 db_printf("%sempty\n", prefix);
1045 TAILQ_FOREACH(td, queue, td_lockq) {
1046 print_thread(td, prefix);
1050 DB_SHOW_COMMAND(turnstile, db_show_turnstile)
1052 struct turnstile_chain *tc;
1053 struct turnstile *ts;
1054 struct lock_object *lock;
1061 * First, see if there is an active turnstile for the lock indicated
1064 lock = (struct lock_object *)addr;
1065 tc = TC_LOOKUP(lock);
1066 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1067 if (ts->ts_lockobj == lock)
1071 * Second, see if there is an active turnstile at the address
1074 for (i = 0; i < TC_TABLESIZE; i++)
1075 LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) {
1076 if (ts == (struct turnstile *)addr)
1080 db_printf("Unable to locate a turnstile via %p\n", (void *)addr);
1083 lock = ts->ts_lockobj;
1084 db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name,
1087 print_thread(ts->ts_owner, "Lock Owner: ");
1089 db_printf("Lock Owner: none\n");
1090 print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t");
1091 print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters",
1093 print_queue(&ts->ts_pending, "Pending Threads", "\t");
1098 * Show all the threads a particular thread is waiting on based on
1099 * non-sleepable and non-spin locks.
1102 print_lockchain(struct thread *td, const char *prefix)
1104 struct lock_object *lock;
1105 struct lock_class *class;
1106 struct turnstile *ts;
1109 * Follow the chain. We keep walking as long as the thread is
1110 * blocked on a turnstile that has an owner.
1112 while (!db_pager_quit) {
1113 db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1114 td->td_proc->p_pid, td->td_name);
1115 switch (td->td_state) {
1117 db_printf("is inactive\n");
1120 db_printf("can run\n");
1123 db_printf("is on a run queue\n");
1126 db_printf("running on CPU %d\n", td->td_oncpu);
1129 if (TD_ON_LOCK(td)) {
1130 ts = td->td_blocked;
1131 lock = ts->ts_lockobj;
1132 class = LOCK_CLASS(lock);
1133 db_printf("blocked on lock %p (%s) \"%s\"\n",
1134 lock, class->lc_name, lock->lo_name);
1135 if (ts->ts_owner == NULL)
1140 db_printf("inhibited\n");
1143 db_printf("??? (%#x)\n", td->td_state);
1149 DB_SHOW_COMMAND(lockchain, db_show_lockchain)
1153 /* Figure out which thread to start with. */
1155 td = db_lookup_thread(addr, true);
1159 print_lockchain(td, "");
1162 DB_SHOW_ALL_COMMAND(chains, db_show_allchains)
1169 FOREACH_PROC_IN_SYSTEM(p) {
1170 FOREACH_THREAD_IN_PROC(p, td) {
1171 if (TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested)) {
1172 db_printf("chain %d:\n", i++);
1173 print_lockchain(td, " ");
1175 if (TD_IS_INHIBITED(td) && TD_ON_SLEEPQ(td)) {
1176 db_printf("chain %d:\n", i++);
1177 print_sleepchain(td, " ");
1184 DB_SHOW_ALIAS(allchains, db_show_allchains)
1187 * Show all the threads a particular thread is waiting on based on
1191 print_sleepchain(struct thread *td, const char *prefix)
1193 struct thread *owner;
1196 * Follow the chain. We keep walking as long as the thread is
1197 * blocked on a sleep lock that has an owner.
1199 while (!db_pager_quit) {
1200 db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid,
1201 td->td_proc->p_pid, td->td_name);
1202 switch (td->td_state) {
1204 db_printf("is inactive\n");
1207 db_printf("can run\n");
1210 db_printf("is on a run queue\n");
1213 db_printf("running on CPU %d\n", td->td_oncpu);
1216 if (TD_ON_SLEEPQ(td)) {
1217 if (lockmgr_chain(td, &owner) ||
1218 sx_chain(td, &owner)) {
1224 db_printf("sleeping on %p \"%s\"\n",
1225 td->td_wchan, td->td_wmesg);
1228 db_printf("inhibited\n");
1231 db_printf("??? (%#x)\n", td->td_state);
1237 DB_SHOW_COMMAND(sleepchain, db_show_sleepchain)
1241 /* Figure out which thread to start with. */
1243 td = db_lookup_thread(addr, true);
1247 print_sleepchain(td, "");
1250 static void print_waiters(struct turnstile *ts, int indent);
1253 print_waiter(struct thread *td, int indent)
1255 struct turnstile *ts;
1260 for (i = 0; i < indent; i++)
1262 print_thread(td, "thread ");
1263 LIST_FOREACH(ts, &td->td_contested, ts_link)
1264 print_waiters(ts, indent + 1);
1268 print_waiters(struct turnstile *ts, int indent)
1270 struct lock_object *lock;
1271 struct lock_class *class;
1277 lock = ts->ts_lockobj;
1278 class = LOCK_CLASS(lock);
1279 for (i = 0; i < indent; i++)
1281 db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name);
1282 TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq)
1283 print_waiter(td, indent + 1);
1284 TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq)
1285 print_waiter(td, indent + 1);
1286 TAILQ_FOREACH(td, &ts->ts_pending, td_lockq)
1287 print_waiter(td, indent + 1);
1290 DB_SHOW_COMMAND(locktree, db_show_locktree)
1292 struct lock_object *lock;
1293 struct lock_class *class;
1294 struct turnstile_chain *tc;
1295 struct turnstile *ts;
1299 lock = (struct lock_object *)addr;
1300 tc = TC_LOOKUP(lock);
1301 LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash)
1302 if (ts->ts_lockobj == lock)
1305 class = LOCK_CLASS(lock);
1306 db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name,
1309 print_waiters(ts, 0);