10384fff8SJason Evans /*- 20384fff8SJason Evans * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 30384fff8SJason Evans * 40384fff8SJason Evans * Redistribution and use in source and binary forms, with or without 50384fff8SJason Evans * modification, are permitted provided that the following conditions 60384fff8SJason Evans * are met: 70384fff8SJason Evans * 1. Redistributions of source code must retain the above copyright 80384fff8SJason Evans * notice, this list of conditions and the following disclaimer. 90384fff8SJason Evans * 2. Redistributions in binary form must reproduce the above copyright 100384fff8SJason Evans * notice, this list of conditions and the following disclaimer in the 110384fff8SJason Evans * documentation and/or other materials provided with the distribution. 120384fff8SJason Evans * 3. Berkeley Software Design Inc's name may not be used to endorse or 130384fff8SJason Evans * promote products derived from this software without specific prior 140384fff8SJason Evans * written permission. 150384fff8SJason Evans * 160384fff8SJason Evans * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 170384fff8SJason Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 180384fff8SJason Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 190384fff8SJason Evans * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 200384fff8SJason Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 210384fff8SJason Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 220384fff8SJason Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 230384fff8SJason Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 240384fff8SJason Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 250384fff8SJason Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 260384fff8SJason Evans * SUCH DAMAGE. 270384fff8SJason Evans * 280384fff8SJason Evans * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 2936412d79SJohn Baldwin * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 300384fff8SJason Evans */ 310384fff8SJason Evans 320384fff8SJason Evans /* 33961a7b24SJohn Baldwin * Implementation of turnstiles used to hold queue of threads blocked on 34961a7b24SJohn Baldwin * non-sleepable locks. Sleepable locks use condition variables to 35961a7b24SJohn Baldwin * implement their queues. Turnstiles differ from a sleep queue in that 36961a7b24SJohn Baldwin * turnstile queue's are assigned to a lock held by an owning thread. Thus, 37961a7b24SJohn Baldwin * when one thread is enqueued onto a turnstile, it can lend its priority 38961a7b24SJohn Baldwin * to the owning thread. 39961a7b24SJohn Baldwin * 40961a7b24SJohn Baldwin * We wish to avoid bloating locks with an embedded turnstile and we do not 41961a7b24SJohn Baldwin * want to use back-pointers in the locks for the same reason. Thus, we 42961a7b24SJohn Baldwin * use a similar approach to that of Solaris 7 as described in Solaris 43961a7b24SJohn Baldwin * Internals by Jim Mauro and Richard McDougall. Turnstiles are looked up 44961a7b24SJohn Baldwin * in a hash table based on the address of the lock. Each entry in the 45961a7b24SJohn Baldwin * hash table is a linked-lists of turnstiles and is called a turnstile 46961a7b24SJohn Baldwin * chain. Each chain contains a spin mutex that protects all of the 47961a7b24SJohn Baldwin * turnstiles in the chain. 48961a7b24SJohn Baldwin * 492b7e2ee7SJeff Roberson * Each time a thread is created, a turnstile is allocated from a UMA zone 502b7e2ee7SJeff Roberson * and attached to that thread. When a thread blocks on a lock, if it is the 512b7e2ee7SJeff Roberson * first thread to block, it lends its turnstile to the lock. If the lock 522b7e2ee7SJeff Roberson * already has a turnstile, then it gives its turnstile to the lock's 532b7e2ee7SJeff Roberson * turnstile's free list. When a thread is woken up, it takes a turnstile from 542b7e2ee7SJeff Roberson * the free list if there are any other waiters. If it is the only thread 552b7e2ee7SJeff Roberson * blocked on the lock, then it reclaims the turnstile associated with the lock 562b7e2ee7SJeff Roberson * and removes it from the hash table. 570384fff8SJason Evans */ 580384fff8SJason Evans 59677b542eSDavid E. O'Brien #include <sys/cdefs.h> 60677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 61677b542eSDavid E. O'Brien 627aa4f685SJohn Baldwin #include "opt_ddb.h" 637aa4f685SJohn Baldwin #include "opt_turnstile_profiling.h" 643036ab79SJeff Roberson #include "opt_sched.h" 657aa4f685SJohn Baldwin 660384fff8SJason Evans #include <sys/param.h> 676c35e809SDag-Erling Smørgrav #include <sys/systm.h> 6836412d79SJohn Baldwin #include <sys/kernel.h> 696c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 7019284646SJohn Baldwin #include <sys/lock.h> 7119284646SJohn Baldwin #include <sys/mutex.h> 720384fff8SJason Evans #include <sys/proc.h> 73961a7b24SJohn Baldwin #include <sys/queue.h> 74b43179fbSJeff Roberson #include <sys/sched.h> 75ef0ebfc3SJohn Baldwin #include <sys/sysctl.h> 76ef0ebfc3SJohn Baldwin #include <sys/turnstile.h> 7736412d79SJohn Baldwin 782b7e2ee7SJeff Roberson #include <vm/uma.h> 792b7e2ee7SJeff Roberson 807aa4f685SJohn Baldwin #ifdef DDB 81ae110b53SJohn Baldwin #include <sys/kdb.h> 827aa4f685SJohn Baldwin #include <ddb/ddb.h> 83462a7addSJohn Baldwin #include <sys/lockmgr.h> 84462a7addSJohn Baldwin #include <sys/sx.h> 857aa4f685SJohn Baldwin #endif 867aa4f685SJohn Baldwin 870cde2e34SJason Evans /* 88961a7b24SJohn Baldwin * Constants for the hash table of turnstile chains. TC_SHIFT is a magic 89961a7b24SJohn Baldwin * number chosen because the sleep queue's use the same value for the 90961a7b24SJohn Baldwin * shift. Basically, we ignore the lower 8 bits of the address. 91961a7b24SJohn Baldwin * TC_TABLESIZE must be a power of two for TC_MASK to work properly. 920cde2e34SJason Evans */ 93961a7b24SJohn Baldwin #define TC_TABLESIZE 128 /* Must be power of 2. */ 94961a7b24SJohn Baldwin #define TC_MASK (TC_TABLESIZE - 1) 95961a7b24SJohn Baldwin #define TC_SHIFT 8 96961a7b24SJohn Baldwin #define TC_HASH(lock) (((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK) 97961a7b24SJohn Baldwin #define TC_LOOKUP(lock) &turnstile_chains[TC_HASH(lock)] 989ed346baSBosko Milekic 990cde2e34SJason Evans /* 100961a7b24SJohn Baldwin * There are three different lists of turnstiles as follows. The list 101961a7b24SJohn Baldwin * connected by ts_link entries is a per-thread list of all the turnstiles 102961a7b24SJohn Baldwin * attached to locks that we own. This is used to fixup our priority when 103961a7b24SJohn Baldwin * a lock is released. The other two lists use the ts_hash entries. The 1045b7de7e1SJohn Baldwin * first of these two is the turnstile chain list that a turnstile is on 1055b7de7e1SJohn Baldwin * when it is attached to a lock. The second list to use ts_hash is the 1065b7de7e1SJohn Baldwin * free list hung off of a turnstile that is attached to a lock. 107961a7b24SJohn Baldwin * 1087aa4f685SJohn Baldwin * Each turnstile contains three lists of threads. The two ts_blocked lists 1097aa4f685SJohn Baldwin * are linked list of threads blocked on the turnstile's lock. One list is 1107aa4f685SJohn Baldwin * for exclusive waiters, and the other is for shared waiters. The 111595bc82aSJohn Baldwin * ts_pending list is a linked list of threads previously awakened by 112961a7b24SJohn Baldwin * turnstile_signal() or turnstile_wait() that are waiting to be put on 113961a7b24SJohn Baldwin * the run queue. 114961a7b24SJohn Baldwin * 115961a7b24SJohn Baldwin * Locking key: 116961a7b24SJohn Baldwin * c - turnstile chain lock 117961a7b24SJohn Baldwin * q - td_contested lock 1180cde2e34SJason Evans */ 119961a7b24SJohn Baldwin struct turnstile { 1202502c107SJeff Roberson struct mtx ts_lock; /* Spin lock for self. */ 1217aa4f685SJohn Baldwin struct threadqueue ts_blocked[2]; /* (c + q) Blocked threads. */ 1227aa4f685SJohn Baldwin struct threadqueue ts_pending; /* (c) Pending threads. */ 123961a7b24SJohn Baldwin LIST_ENTRY(turnstile) ts_hash; /* (c) Chain and free list. */ 124961a7b24SJohn Baldwin LIST_ENTRY(turnstile) ts_link; /* (q) Contested locks. */ 125961a7b24SJohn Baldwin LIST_HEAD(, turnstile) ts_free; /* (c) Free turnstiles. */ 126961a7b24SJohn Baldwin struct lock_object *ts_lockobj; /* (c) Lock we reference. */ 12779a13d01SJohn Baldwin struct thread *ts_owner; /* (c + q) Who owns the lock. */ 1288484de75SJohn Baldwin }; 1298484de75SJohn Baldwin 130961a7b24SJohn Baldwin struct turnstile_chain { 131961a7b24SJohn Baldwin LIST_HEAD(, turnstile) tc_turnstiles; /* List of turnstiles. */ 132961a7b24SJohn Baldwin struct mtx tc_lock; /* Spin lock for this chain. */ 133ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 134ef0ebfc3SJohn Baldwin u_int tc_depth; /* Length of tc_queues. */ 135ef0ebfc3SJohn Baldwin u_int tc_max_depth; /* Max length of tc_queues. */ 136ef0ebfc3SJohn Baldwin #endif 137961a7b24SJohn Baldwin }; 138961a7b24SJohn Baldwin 139ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 140ef0ebfc3SJohn Baldwin u_int turnstile_max_depth; 141ef0ebfc3SJohn Baldwin SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0, "turnstile profiling"); 142ef0ebfc3SJohn Baldwin SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0, 143ef0ebfc3SJohn Baldwin "turnstile chain stats"); 144ef0ebfc3SJohn Baldwin SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD, 145ef0ebfc3SJohn Baldwin &turnstile_max_depth, 0, "maxmimum depth achieved of a single chain"); 146ef0ebfc3SJohn Baldwin #endif 147961a7b24SJohn Baldwin static struct mtx td_contested_lock; 148961a7b24SJohn Baldwin static struct turnstile_chain turnstile_chains[TC_TABLESIZE]; 1492b7e2ee7SJeff Roberson static uma_zone_t turnstile_zone; 150c53c013bSJohn Baldwin 151c53c013bSJohn Baldwin /* 1529ed346baSBosko Milekic * Prototypes for non-exported routines. 1539ed346baSBosko Milekic */ 154961a7b24SJohn Baldwin static void init_turnstile0(void *dummy); 15501bd10e1SJohn Baldwin #ifdef TURNSTILE_PROFILING 15601bd10e1SJohn Baldwin static void init_turnstile_profiling(void *arg); 15701bd10e1SJohn Baldwin #endif 158f5c157d9SJohn Baldwin static void propagate_priority(struct thread *td); 159f5c157d9SJohn Baldwin static int turnstile_adjust_thread(struct turnstile *ts, 160f5c157d9SJohn Baldwin struct thread *td); 1617aa4f685SJohn Baldwin static struct thread *turnstile_first_waiter(struct turnstile *ts); 162961a7b24SJohn Baldwin static void turnstile_setowner(struct turnstile *ts, struct thread *owner); 1632b7e2ee7SJeff Roberson #ifdef INVARIANTS 1642b7e2ee7SJeff Roberson static void turnstile_dtor(void *mem, int size, void *arg); 1652b7e2ee7SJeff Roberson #endif 1662b7e2ee7SJeff Roberson static int turnstile_init(void *mem, int size, int flags); 1672502c107SJeff Roberson static void turnstile_fini(void *mem, int size); 16836412d79SJohn Baldwin 169961a7b24SJohn Baldwin /* 170961a7b24SJohn Baldwin * Walks the chain of turnstiles and their owners to propagate the priority 171961a7b24SJohn Baldwin * of the thread being blocked to all the threads holding locks that have to 172961a7b24SJohn Baldwin * release their locks before this thread can run again. 173961a7b24SJohn Baldwin */ 17436412d79SJohn Baldwin static void 175b40ce416SJulian Elischer propagate_priority(struct thread *td) 17636412d79SJohn Baldwin { 177961a7b24SJohn Baldwin struct turnstile *ts; 178961a7b24SJohn Baldwin int pri; 17936412d79SJohn Baldwin 1802502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 181961a7b24SJohn Baldwin pri = td->td_priority; 182961a7b24SJohn Baldwin ts = td->td_blocked; 183626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 1842502c107SJeff Roberson /* 1852502c107SJeff Roberson * Grab a recursive lock on this turnstile chain so it stays locked 1862502c107SJeff Roberson * for the whole operation. The caller expects us to return with 1872502c107SJeff Roberson * the original lock held. We only ever lock down the chain so 1882502c107SJeff Roberson * the lock order is constant. 1892502c107SJeff Roberson */ 1902502c107SJeff Roberson mtx_lock_spin(&ts->ts_lock); 19136412d79SJohn Baldwin for (;;) { 192961a7b24SJohn Baldwin td = ts->ts_owner; 19336412d79SJohn Baldwin 194b40ce416SJulian Elischer if (td == NULL) { 19536412d79SJohn Baldwin /* 1967aa4f685SJohn Baldwin * This might be a read lock with no owner. There's 1977aa4f685SJohn Baldwin * not much we can do, so just bail. 19836412d79SJohn Baldwin */ 1992502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 20036412d79SJohn Baldwin return; 20136412d79SJohn Baldwin } 2029ed346baSBosko Milekic 2032502c107SJeff Roberson thread_lock_flags(td, MTX_DUPOK); 2042502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 205e602ba25SJulian Elischer MPASS(td->td_proc != NULL); 206b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 2071bd0eefbSJohn Baldwin 208961a7b24SJohn Baldwin /* 2094b3b0413SJohn Baldwin * If the thread is asleep, then we are probably about 2104b3b0413SJohn Baldwin * to deadlock. To make debugging this easier, just 2114b3b0413SJohn Baldwin * panic and tell the user which thread misbehaved so 2124b3b0413SJohn Baldwin * they can hopefully get a stack trace from the truly 2134b3b0413SJohn Baldwin * misbehaving thread. 214961a7b24SJohn Baldwin */ 2154b3b0413SJohn Baldwin if (TD_IS_SLEEPING(td)) { 2164b3b0413SJohn Baldwin printf( 2174b3b0413SJohn Baldwin "Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n", 2184b3b0413SJohn Baldwin td->td_tid, td->td_proc->p_pid); 2194b3b0413SJohn Baldwin #ifdef DDB 2204b3b0413SJohn Baldwin db_trace_thread(td, -1); 2214b3b0413SJohn Baldwin #endif 2224b3b0413SJohn Baldwin panic("sleeping thread"); 2234b3b0413SJohn Baldwin } 224961a7b24SJohn Baldwin 225961a7b24SJohn Baldwin /* 226961a7b24SJohn Baldwin * If this thread already has higher priority than the 227961a7b24SJohn Baldwin * thread that is being blocked, we are finished. 228961a7b24SJohn Baldwin */ 2292502c107SJeff Roberson if (td->td_priority <= pri) { 2302502c107SJeff Roberson thread_unlock(td); 231961a7b24SJohn Baldwin return; 2322502c107SJeff Roberson } 2331bd0eefbSJohn Baldwin 23436412d79SJohn Baldwin /* 235f5c157d9SJohn Baldwin * Bump this thread's priority. 23636412d79SJohn Baldwin */ 237f5c157d9SJohn Baldwin sched_lend_prio(td, pri); 238f5c157d9SJohn Baldwin 239f5c157d9SJohn Baldwin /* 240f5c157d9SJohn Baldwin * If lock holder is actually running or on the run queue 241f5c157d9SJohn Baldwin * then we are done. 242f5c157d9SJohn Baldwin */ 243f5c157d9SJohn Baldwin if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) { 244f5c157d9SJohn Baldwin MPASS(td->td_blocked == NULL); 2452502c107SJeff Roberson thread_unlock(td); 24636412d79SJohn Baldwin return; 24736412d79SJohn Baldwin } 248d5a08a60SJake Burkholder 2491b43703bSJohn Baldwin #ifndef SMP 2501b43703bSJohn Baldwin /* 251b40ce416SJulian Elischer * For UP, we check to see if td is curthread (this shouldn't 2521b43703bSJohn Baldwin * ever happen however as it would mean we are in a deadlock.) 2531b43703bSJohn Baldwin */ 254b40ce416SJulian Elischer KASSERT(td != curthread, ("Deadlock detected")); 2551b43703bSJohn Baldwin #endif 2561b43703bSJohn Baldwin 25736412d79SJohn Baldwin /* 258961a7b24SJohn Baldwin * If we aren't blocked on a lock, we should be. 25936412d79SJohn Baldwin */ 260551cf4e1SJohn Baldwin KASSERT(TD_ON_LOCK(td), ( 261f5c157d9SJohn Baldwin "thread %d(%s):%d holds %s but isn't blocked on a lock\n", 262431f8906SJulian Elischer td->td_tid, td->td_name, td->td_state, 263961a7b24SJohn Baldwin ts->ts_lockobj->lo_name)); 26436412d79SJohn Baldwin 26536412d79SJohn Baldwin /* 266961a7b24SJohn Baldwin * Pick up the lock that td is blocked on. 26736412d79SJohn Baldwin */ 268961a7b24SJohn Baldwin ts = td->td_blocked; 269961a7b24SJohn Baldwin MPASS(ts != NULL); 270626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 271f5c157d9SJohn Baldwin /* Resort td on the list if needed. */ 272f5c157d9SJohn Baldwin if (!turnstile_adjust_thread(ts, td)) { 2732502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 274f5c157d9SJohn Baldwin return; 275f5c157d9SJohn Baldwin } 2762502c107SJeff Roberson /* The thread lock is released as ts lock above. */ 277f5c157d9SJohn Baldwin } 278f5c157d9SJohn Baldwin } 279f5c157d9SJohn Baldwin 280f5c157d9SJohn Baldwin /* 281f5c157d9SJohn Baldwin * Adjust the thread's position on a turnstile after its priority has been 282f5c157d9SJohn Baldwin * changed. 283f5c157d9SJohn Baldwin */ 284f5c157d9SJohn Baldwin static int 285f5c157d9SJohn Baldwin turnstile_adjust_thread(struct turnstile *ts, struct thread *td) 286f5c157d9SJohn Baldwin { 287f5c157d9SJohn Baldwin struct thread *td1, *td2; 2887aa4f685SJohn Baldwin int queue; 289f5c157d9SJohn Baldwin 2902502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 291f5c157d9SJohn Baldwin MPASS(TD_ON_LOCK(td)); 292f5c157d9SJohn Baldwin 29336412d79SJohn Baldwin /* 2946b6bd95eSJohn Baldwin * This thread may not be blocked on this turnstile anymore 2956b6bd95eSJohn Baldwin * but instead might already be woken up on another CPU 2962502c107SJeff Roberson * that is waiting on the thread lock in turnstile_unpend() to 2976b6bd95eSJohn Baldwin * finish waking this thread up. We can detect this case 2986b6bd95eSJohn Baldwin * by checking to see if this thread has been given a 2996b6bd95eSJohn Baldwin * turnstile by either turnstile_signal() or 300ef2c0ba7SJohn Baldwin * turnstile_broadcast(). In this case, treat the thread as 3016b6bd95eSJohn Baldwin * if it was already running. 30279a13d01SJohn Baldwin */ 303f5c157d9SJohn Baldwin if (td->td_turnstile != NULL) 304f5c157d9SJohn Baldwin return (0); 30579a13d01SJohn Baldwin 30679a13d01SJohn Baldwin /* 307f5c157d9SJohn Baldwin * Check if the thread needs to be moved on the blocked chain. 308f5c157d9SJohn Baldwin * It needs to be moved if either its priority is lower than 309f5c157d9SJohn Baldwin * the previous thread or higher than the next thread. 31036412d79SJohn Baldwin */ 311626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 312551cf4e1SJohn Baldwin td1 = TAILQ_PREV(td, threadqueue, td_lockq); 313f5c157d9SJohn Baldwin td2 = TAILQ_NEXT(td, td_lockq); 314f5c157d9SJohn Baldwin if ((td1 != NULL && td->td_priority < td1->td_priority) || 315f5c157d9SJohn Baldwin (td2 != NULL && td->td_priority > td2->td_priority)) { 31636412d79SJohn Baldwin 31736412d79SJohn Baldwin /* 318b40ce416SJulian Elischer * Remove thread from blocked chain and determine where 319f5c157d9SJohn Baldwin * it should be moved to. 32036412d79SJohn Baldwin */ 3217aa4f685SJohn Baldwin queue = td->td_tsqueue; 3227aa4f685SJohn Baldwin MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE); 323961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 3247aa4f685SJohn Baldwin TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq); 3257aa4f685SJohn Baldwin TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) { 326b40ce416SJulian Elischer MPASS(td1->td_proc->p_magic == P_MAGIC); 327f5c157d9SJohn Baldwin if (td1->td_priority > td->td_priority) 32836412d79SJohn Baldwin break; 32936412d79SJohn Baldwin } 3309ed346baSBosko Milekic 331f5c157d9SJohn Baldwin if (td1 == NULL) 3327aa4f685SJohn Baldwin TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq); 333f5c157d9SJohn Baldwin else 334551cf4e1SJohn Baldwin TAILQ_INSERT_BEFORE(td1, td, td_lockq); 335961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 336f5c157d9SJohn Baldwin if (td1 == NULL) 337f5c157d9SJohn Baldwin CTR3(KTR_LOCK, 338f5c157d9SJohn Baldwin "turnstile_adjust_thread: td %d put at tail on [%p] %s", 339f5c157d9SJohn Baldwin td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name); 340f5c157d9SJohn Baldwin else 34136412d79SJohn Baldwin CTR4(KTR_LOCK, 342f5c157d9SJohn Baldwin "turnstile_adjust_thread: td %d moved before %d on [%p] %s", 343f5c157d9SJohn Baldwin td->td_tid, td1->td_tid, ts->ts_lockobj, 344f5c157d9SJohn Baldwin ts->ts_lockobj->lo_name); 34536412d79SJohn Baldwin } 346f5c157d9SJohn Baldwin return (1); 34736412d79SJohn Baldwin } 34836412d79SJohn Baldwin 3496c35e809SDag-Erling Smørgrav /* 350961a7b24SJohn Baldwin * Early initialization of turnstiles. This is not done via a SYSINIT() 351961a7b24SJohn Baldwin * since this needs to be initialized very early when mutexes are first 352961a7b24SJohn Baldwin * initialized. 3536283b7d0SJohn Baldwin */ 3546283b7d0SJohn Baldwin void 355961a7b24SJohn Baldwin init_turnstiles(void) 3566283b7d0SJohn Baldwin { 357961a7b24SJohn Baldwin int i; 3586283b7d0SJohn Baldwin 359961a7b24SJohn Baldwin for (i = 0; i < TC_TABLESIZE; i++) { 360961a7b24SJohn Baldwin LIST_INIT(&turnstile_chains[i].tc_turnstiles); 361961a7b24SJohn Baldwin mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain", 362961a7b24SJohn Baldwin NULL, MTX_SPIN); 36301bd10e1SJohn Baldwin } 36401bd10e1SJohn Baldwin mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN); 365550d1c93SJohn Baldwin LIST_INIT(&thread0.td_contested); 36601bd10e1SJohn Baldwin thread0.td_turnstile = NULL; 36701bd10e1SJohn Baldwin } 36801bd10e1SJohn Baldwin 369ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 37001bd10e1SJohn Baldwin static void 37101bd10e1SJohn Baldwin init_turnstile_profiling(void *arg) 37201bd10e1SJohn Baldwin { 37301bd10e1SJohn Baldwin struct sysctl_oid *chain_oid; 37401bd10e1SJohn Baldwin char chain_name[10]; 37501bd10e1SJohn Baldwin int i; 37601bd10e1SJohn Baldwin 37701bd10e1SJohn Baldwin for (i = 0; i < TC_TABLESIZE; i++) { 378ef0ebfc3SJohn Baldwin snprintf(chain_name, sizeof(chain_name), "%d", i); 379ef0ebfc3SJohn Baldwin chain_oid = SYSCTL_ADD_NODE(NULL, 380ef0ebfc3SJohn Baldwin SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO, 381ef0ebfc3SJohn Baldwin chain_name, CTLFLAG_RD, NULL, "turnstile chain stats"); 382ef0ebfc3SJohn Baldwin SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO, 383ef0ebfc3SJohn Baldwin "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0, 384ef0ebfc3SJohn Baldwin NULL); 385ef0ebfc3SJohn Baldwin SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO, 386ef0ebfc3SJohn Baldwin "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth, 387ef0ebfc3SJohn Baldwin 0, NULL); 38801bd10e1SJohn Baldwin } 38901bd10e1SJohn Baldwin } 39001bd10e1SJohn Baldwin SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY, 39101bd10e1SJohn Baldwin init_turnstile_profiling, NULL); 392ef0ebfc3SJohn Baldwin #endif 3936283b7d0SJohn Baldwin 394961a7b24SJohn Baldwin static void 395961a7b24SJohn Baldwin init_turnstile0(void *dummy) 3966283b7d0SJohn Baldwin { 3976283b7d0SJohn Baldwin 3982b7e2ee7SJeff Roberson turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile), 3998c68f75aSJohn Baldwin NULL, 4002b7e2ee7SJeff Roberson #ifdef INVARIANTS 4018c68f75aSJohn Baldwin turnstile_dtor, 4022b7e2ee7SJeff Roberson #else 4038c68f75aSJohn Baldwin NULL, 4042b7e2ee7SJeff Roberson #endif 4058c68f75aSJohn Baldwin turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE); 406961a7b24SJohn Baldwin thread0.td_turnstile = turnstile_alloc(); 407961a7b24SJohn Baldwin } 408961a7b24SJohn Baldwin SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL); 4096c35e809SDag-Erling Smørgrav 410961a7b24SJohn Baldwin /* 411f5c157d9SJohn Baldwin * Update a thread on the turnstile list after it's priority has been changed. 412f5c157d9SJohn Baldwin * The old priority is passed in as an argument. 413f5c157d9SJohn Baldwin */ 414f5c157d9SJohn Baldwin void 415f5c157d9SJohn Baldwin turnstile_adjust(struct thread *td, u_char oldpri) 416f5c157d9SJohn Baldwin { 417f5c157d9SJohn Baldwin struct turnstile *ts; 418f5c157d9SJohn Baldwin 419f5c157d9SJohn Baldwin MPASS(TD_ON_LOCK(td)); 420f5c157d9SJohn Baldwin 421f5c157d9SJohn Baldwin /* 422f5c157d9SJohn Baldwin * Pick up the lock that td is blocked on. 423f5c157d9SJohn Baldwin */ 424f5c157d9SJohn Baldwin ts = td->td_blocked; 425f5c157d9SJohn Baldwin MPASS(ts != NULL); 426626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 4272502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 428f5c157d9SJohn Baldwin 429f5c157d9SJohn Baldwin /* Resort the turnstile on the list. */ 4302502c107SJeff Roberson if (!turnstile_adjust_thread(ts, td)) 431f5c157d9SJohn Baldwin return; 432f5c157d9SJohn Baldwin /* 433f5c157d9SJohn Baldwin * If our priority was lowered and we are at the head of the 434f5c157d9SJohn Baldwin * turnstile, then propagate our new priority up the chain. 435f5c157d9SJohn Baldwin * Note that we currently don't try to revoke lent priorities 436f5c157d9SJohn Baldwin * when our priority goes up. 437f5c157d9SJohn Baldwin */ 4387aa4f685SJohn Baldwin MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE || 4397aa4f685SJohn Baldwin td->td_tsqueue == TS_SHARED_QUEUE); 4407aa4f685SJohn Baldwin if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) && 4417aa4f685SJohn Baldwin td->td_priority < oldpri) { 442f5c157d9SJohn Baldwin propagate_priority(td); 4432502c107SJeff Roberson } 444f5c157d9SJohn Baldwin } 445f5c157d9SJohn Baldwin 446f5c157d9SJohn Baldwin /* 447961a7b24SJohn Baldwin * Set the owner of the lock this turnstile is attached to. 448961a7b24SJohn Baldwin */ 449961a7b24SJohn Baldwin static void 450961a7b24SJohn Baldwin turnstile_setowner(struct turnstile *ts, struct thread *owner) 451961a7b24SJohn Baldwin { 452961a7b24SJohn Baldwin 453961a7b24SJohn Baldwin mtx_assert(&td_contested_lock, MA_OWNED); 454961a7b24SJohn Baldwin MPASS(ts->ts_owner == NULL); 4557aa4f685SJohn Baldwin 4567aa4f685SJohn Baldwin /* A shared lock might not have an owner. */ 4577aa4f685SJohn Baldwin if (owner == NULL) 4587aa4f685SJohn Baldwin return; 4597aa4f685SJohn Baldwin 4607aa4f685SJohn Baldwin MPASS(owner->td_proc->p_magic == P_MAGIC); 461961a7b24SJohn Baldwin ts->ts_owner = owner; 462961a7b24SJohn Baldwin LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link); 463961a7b24SJohn Baldwin } 464961a7b24SJohn Baldwin 4652b7e2ee7SJeff Roberson #ifdef INVARIANTS 466961a7b24SJohn Baldwin /* 4672b7e2ee7SJeff Roberson * UMA zone item deallocator. 468961a7b24SJohn Baldwin */ 4692b7e2ee7SJeff Roberson static void 4702b7e2ee7SJeff Roberson turnstile_dtor(void *mem, int size, void *arg) 471961a7b24SJohn Baldwin { 472961a7b24SJohn Baldwin struct turnstile *ts; 473961a7b24SJohn Baldwin 4742b7e2ee7SJeff Roberson ts = mem; 4752b7e2ee7SJeff Roberson MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE])); 4762b7e2ee7SJeff Roberson MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE])); 4772b7e2ee7SJeff Roberson MPASS(TAILQ_EMPTY(&ts->ts_pending)); 4782b7e2ee7SJeff Roberson } 4792b7e2ee7SJeff Roberson #endif 4802b7e2ee7SJeff Roberson 4812b7e2ee7SJeff Roberson /* 4822b7e2ee7SJeff Roberson * UMA zone item initializer. 4832b7e2ee7SJeff Roberson */ 4842b7e2ee7SJeff Roberson static int 4852b7e2ee7SJeff Roberson turnstile_init(void *mem, int size, int flags) 4862b7e2ee7SJeff Roberson { 4872b7e2ee7SJeff Roberson struct turnstile *ts; 4882b7e2ee7SJeff Roberson 4892b7e2ee7SJeff Roberson bzero(mem, size); 4902b7e2ee7SJeff Roberson ts = mem; 4917aa4f685SJohn Baldwin TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]); 4927aa4f685SJohn Baldwin TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]); 493961a7b24SJohn Baldwin TAILQ_INIT(&ts->ts_pending); 494961a7b24SJohn Baldwin LIST_INIT(&ts->ts_free); 4952502c107SJeff Roberson mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN | MTX_RECURSE); 4962b7e2ee7SJeff Roberson return (0); 4972b7e2ee7SJeff Roberson } 4982b7e2ee7SJeff Roberson 4992502c107SJeff Roberson static void 5002502c107SJeff Roberson turnstile_fini(void *mem, int size) 5012502c107SJeff Roberson { 5022502c107SJeff Roberson struct turnstile *ts; 5032502c107SJeff Roberson 5042502c107SJeff Roberson ts = mem; 5052502c107SJeff Roberson mtx_destroy(&ts->ts_lock); 5062502c107SJeff Roberson } 5072502c107SJeff Roberson 5082b7e2ee7SJeff Roberson /* 5092b7e2ee7SJeff Roberson * Get a turnstile for a new thread. 5102b7e2ee7SJeff Roberson */ 5112b7e2ee7SJeff Roberson struct turnstile * 5122b7e2ee7SJeff Roberson turnstile_alloc(void) 5132b7e2ee7SJeff Roberson { 5142b7e2ee7SJeff Roberson 5152b7e2ee7SJeff Roberson return (uma_zalloc(turnstile_zone, M_WAITOK)); 516961a7b24SJohn Baldwin } 517961a7b24SJohn Baldwin 518961a7b24SJohn Baldwin /* 519961a7b24SJohn Baldwin * Free a turnstile when a thread is destroyed. 520961a7b24SJohn Baldwin */ 521961a7b24SJohn Baldwin void 522961a7b24SJohn Baldwin turnstile_free(struct turnstile *ts) 523961a7b24SJohn Baldwin { 524961a7b24SJohn Baldwin 5252b7e2ee7SJeff Roberson uma_zfree(turnstile_zone, ts); 526961a7b24SJohn Baldwin } 527961a7b24SJohn Baldwin 528961a7b24SJohn Baldwin /* 5292ff0e645SJohn Baldwin * Lock the turnstile chain associated with the specified lock. 5302ff0e645SJohn Baldwin */ 5312ff0e645SJohn Baldwin void 5322502c107SJeff Roberson turnstile_chain_lock(struct lock_object *lock) 5332ff0e645SJohn Baldwin { 5342ff0e645SJohn Baldwin struct turnstile_chain *tc; 5352ff0e645SJohn Baldwin 5362ff0e645SJohn Baldwin tc = TC_LOOKUP(lock); 5372ff0e645SJohn Baldwin mtx_lock_spin(&tc->tc_lock); 5382ff0e645SJohn Baldwin } 5392ff0e645SJohn Baldwin 5402502c107SJeff Roberson struct turnstile * 5412502c107SJeff Roberson turnstile_trywait(struct lock_object *lock) 5422502c107SJeff Roberson { 5432502c107SJeff Roberson struct turnstile_chain *tc; 5442502c107SJeff Roberson struct turnstile *ts; 5452502c107SJeff Roberson 5462502c107SJeff Roberson tc = TC_LOOKUP(lock); 5472502c107SJeff Roberson mtx_lock_spin(&tc->tc_lock); 5482502c107SJeff Roberson LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash) 5492502c107SJeff Roberson if (ts->ts_lockobj == lock) { 5502502c107SJeff Roberson mtx_lock_spin(&ts->ts_lock); 5512502c107SJeff Roberson return (ts); 5522502c107SJeff Roberson } 5532502c107SJeff Roberson 5542502c107SJeff Roberson ts = curthread->td_turnstile; 5552502c107SJeff Roberson MPASS(ts != NULL); 5562502c107SJeff Roberson mtx_lock_spin(&ts->ts_lock); 5572502c107SJeff Roberson KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer")); 5582502c107SJeff Roberson ts->ts_lockobj = lock; 5592502c107SJeff Roberson 5602502c107SJeff Roberson return (ts); 5612502c107SJeff Roberson } 5622502c107SJeff Roberson 5632502c107SJeff Roberson void 5642502c107SJeff Roberson turnstile_cancel(struct turnstile *ts) 5652502c107SJeff Roberson { 5662502c107SJeff Roberson struct turnstile_chain *tc; 5672502c107SJeff Roberson struct lock_object *lock; 5682502c107SJeff Roberson 5692502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 5702502c107SJeff Roberson 5712502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 5722502c107SJeff Roberson lock = ts->ts_lockobj; 5732502c107SJeff Roberson if (ts == curthread->td_turnstile) 5742502c107SJeff Roberson ts->ts_lockobj = NULL; 5752502c107SJeff Roberson tc = TC_LOOKUP(lock); 5762502c107SJeff Roberson mtx_unlock_spin(&tc->tc_lock); 5772502c107SJeff Roberson } 5782502c107SJeff Roberson 5792ff0e645SJohn Baldwin /* 580961a7b24SJohn Baldwin * Look up the turnstile for a lock in the hash table locking the associated 5812ff0e645SJohn Baldwin * turnstile chain along the way. If no turnstile is found in the hash 5822ff0e645SJohn Baldwin * table, NULL is returned. 583961a7b24SJohn Baldwin */ 584961a7b24SJohn Baldwin struct turnstile * 585961a7b24SJohn Baldwin turnstile_lookup(struct lock_object *lock) 586961a7b24SJohn Baldwin { 587961a7b24SJohn Baldwin struct turnstile_chain *tc; 588961a7b24SJohn Baldwin struct turnstile *ts; 589961a7b24SJohn Baldwin 590961a7b24SJohn Baldwin tc = TC_LOOKUP(lock); 5912ff0e645SJohn Baldwin mtx_assert(&tc->tc_lock, MA_OWNED); 592961a7b24SJohn Baldwin LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash) 5932502c107SJeff Roberson if (ts->ts_lockobj == lock) { 5942502c107SJeff Roberson mtx_lock_spin(&ts->ts_lock); 595961a7b24SJohn Baldwin return (ts); 5962502c107SJeff Roberson } 597961a7b24SJohn Baldwin return (NULL); 598961a7b24SJohn Baldwin } 599961a7b24SJohn Baldwin 600961a7b24SJohn Baldwin /* 601961a7b24SJohn Baldwin * Unlock the turnstile chain associated with a given lock. 602961a7b24SJohn Baldwin */ 603961a7b24SJohn Baldwin void 6042502c107SJeff Roberson turnstile_chain_unlock(struct lock_object *lock) 605961a7b24SJohn Baldwin { 606961a7b24SJohn Baldwin struct turnstile_chain *tc; 607961a7b24SJohn Baldwin 608961a7b24SJohn Baldwin tc = TC_LOOKUP(lock); 609961a7b24SJohn Baldwin mtx_unlock_spin(&tc->tc_lock); 610961a7b24SJohn Baldwin } 611961a7b24SJohn Baldwin 612961a7b24SJohn Baldwin /* 6137aa4f685SJohn Baldwin * Return a pointer to the thread waiting on this turnstile with the 6147aa4f685SJohn Baldwin * most important priority or NULL if the turnstile has no waiters. 6157aa4f685SJohn Baldwin */ 6167aa4f685SJohn Baldwin static struct thread * 6177aa4f685SJohn Baldwin turnstile_first_waiter(struct turnstile *ts) 6187aa4f685SJohn Baldwin { 6197aa4f685SJohn Baldwin struct thread *std, *xtd; 6207aa4f685SJohn Baldwin 6217aa4f685SJohn Baldwin std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]); 6227aa4f685SJohn Baldwin xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]); 6237aa4f685SJohn Baldwin if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority)) 6247aa4f685SJohn Baldwin return (std); 6257aa4f685SJohn Baldwin return (xtd); 6267aa4f685SJohn Baldwin } 6277aa4f685SJohn Baldwin 6287aa4f685SJohn Baldwin /* 629961a7b24SJohn Baldwin * Take ownership of a turnstile and adjust the priority of the new 630961a7b24SJohn Baldwin * owner appropriately. 631961a7b24SJohn Baldwin */ 632961a7b24SJohn Baldwin void 6332502c107SJeff Roberson turnstile_claim(struct turnstile *ts) 634961a7b24SJohn Baldwin { 635961a7b24SJohn Baldwin struct thread *td, *owner; 6362502c107SJeff Roberson struct turnstile_chain *tc; 637961a7b24SJohn Baldwin 6382502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 6392502c107SJeff Roberson MPASS(ts != curthread->td_turnstile); 640961a7b24SJohn Baldwin 641961a7b24SJohn Baldwin owner = curthread; 642961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 643961a7b24SJohn Baldwin turnstile_setowner(ts, owner); 644961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 645961a7b24SJohn Baldwin 6467aa4f685SJohn Baldwin td = turnstile_first_waiter(ts); 647961a7b24SJohn Baldwin MPASS(td != NULL); 648961a7b24SJohn Baldwin MPASS(td->td_proc->p_magic == P_MAGIC); 649626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 650961a7b24SJohn Baldwin 651961a7b24SJohn Baldwin /* 652961a7b24SJohn Baldwin * Update the priority of the new owner if needed. 653961a7b24SJohn Baldwin */ 6542502c107SJeff Roberson thread_lock(owner); 655961a7b24SJohn Baldwin if (td->td_priority < owner->td_priority) 656f5c157d9SJohn Baldwin sched_lend_prio(owner, td->td_priority); 6572502c107SJeff Roberson thread_unlock(owner); 6582502c107SJeff Roberson tc = TC_LOOKUP(ts->ts_lockobj); 6592502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 6602502c107SJeff Roberson mtx_unlock_spin(&tc->tc_lock); 661961a7b24SJohn Baldwin } 662961a7b24SJohn Baldwin 663961a7b24SJohn Baldwin /* 6642ff0e645SJohn Baldwin * Block the current thread on the turnstile assicated with 'lock'. This 6652ff0e645SJohn Baldwin * function will context switch and not return until this thread has been 6662ff0e645SJohn Baldwin * woken back up. This function must be called with the appropriate 6672ff0e645SJohn Baldwin * turnstile chain locked and will return with it unlocked. 668961a7b24SJohn Baldwin */ 669961a7b24SJohn Baldwin void 6702502c107SJeff Roberson turnstile_wait(struct turnstile *ts, struct thread *owner, int queue) 671961a7b24SJohn Baldwin { 672961a7b24SJohn Baldwin struct turnstile_chain *tc; 673961a7b24SJohn Baldwin struct thread *td, *td1; 6742502c107SJeff Roberson struct lock_object *lock; 675961a7b24SJohn Baldwin 676961a7b24SJohn Baldwin td = curthread; 6772502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 6787aa4f685SJohn Baldwin if (owner) 679961a7b24SJohn Baldwin MPASS(owner->td_proc->p_magic == P_MAGIC); 6807aa4f685SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 681961a7b24SJohn Baldwin 6822ff0e645SJohn Baldwin /* 6832ff0e645SJohn Baldwin * If the lock does not already have a turnstile, use this thread's 6842ff0e645SJohn Baldwin * turnstile. Otherwise insert the current thread into the 6852ff0e645SJohn Baldwin * turnstile already in use by this lock. 6862ff0e645SJohn Baldwin */ 6872502c107SJeff Roberson tc = TC_LOOKUP(ts->ts_lockobj); 6882502c107SJeff Roberson mtx_assert(&tc->tc_lock, MA_OWNED); 689*f7488600SJohn Baldwin if (ts == td->td_turnstile) { 690ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 691ef0ebfc3SJohn Baldwin tc->tc_depth++; 692ef0ebfc3SJohn Baldwin if (tc->tc_depth > tc->tc_max_depth) { 693ef0ebfc3SJohn Baldwin tc->tc_max_depth = tc->tc_depth; 694ef0ebfc3SJohn Baldwin if (tc->tc_max_depth > turnstile_max_depth) 695ef0ebfc3SJohn Baldwin turnstile_max_depth = tc->tc_max_depth; 696ef0ebfc3SJohn Baldwin } 697ef0ebfc3SJohn Baldwin #endif 698961a7b24SJohn Baldwin LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash); 699961a7b24SJohn Baldwin KASSERT(TAILQ_EMPTY(&ts->ts_pending), 700961a7b24SJohn Baldwin ("thread's turnstile has pending threads")); 7017aa4f685SJohn Baldwin KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]), 7027aa4f685SJohn Baldwin ("thread's turnstile has exclusive waiters")); 7037aa4f685SJohn Baldwin KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]), 7047aa4f685SJohn Baldwin ("thread's turnstile has shared waiters")); 705961a7b24SJohn Baldwin KASSERT(LIST_EMPTY(&ts->ts_free), 706961a7b24SJohn Baldwin ("thread's turnstile has a non-empty free list")); 7072502c107SJeff Roberson MPASS(ts->ts_lockobj != NULL); 708961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 7097aa4f685SJohn Baldwin TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq); 710961a7b24SJohn Baldwin turnstile_setowner(ts, owner); 711961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 712961a7b24SJohn Baldwin } else { 7137aa4f685SJohn Baldwin TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) 714961a7b24SJohn Baldwin if (td1->td_priority > td->td_priority) 7156c35e809SDag-Erling Smørgrav break; 716961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 717961a7b24SJohn Baldwin if (td1 != NULL) 718961a7b24SJohn Baldwin TAILQ_INSERT_BEFORE(td1, td, td_lockq); 719961a7b24SJohn Baldwin else 7207aa4f685SJohn Baldwin TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq); 7217aa4f685SJohn Baldwin MPASS(owner == ts->ts_owner); 722961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 723961a7b24SJohn Baldwin MPASS(td->td_turnstile != NULL); 724961a7b24SJohn Baldwin LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash); 7256c35e809SDag-Erling Smørgrav } 7262502c107SJeff Roberson thread_lock(td); 7272502c107SJeff Roberson thread_lock_set(td, &ts->ts_lock); 728961a7b24SJohn Baldwin td->td_turnstile = NULL; 72936412d79SJohn Baldwin 730961a7b24SJohn Baldwin /* Save who we are blocked on and switch. */ 7312502c107SJeff Roberson lock = ts->ts_lockobj; 7327aa4f685SJohn Baldwin td->td_tsqueue = queue; 733961a7b24SJohn Baldwin td->td_blocked = ts; 734961a7b24SJohn Baldwin td->td_lockname = lock->lo_name; 735f7829d0dSAttilio Rao td->td_blktick = ticks; 736551cf4e1SJohn Baldwin TD_SET_LOCK(td); 7372502c107SJeff Roberson mtx_unlock_spin(&tc->tc_lock); 738b40ce416SJulian Elischer propagate_priority(td); 7399ed346baSBosko Milekic 740961a7b24SJohn Baldwin if (LOCK_LOG_TEST(lock, 0)) 741f5c157d9SJohn Baldwin CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__, 742f5c157d9SJohn Baldwin td->td_tid, lock, lock->lo_name); 7439ed346baSBosko Milekic 744626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 7458df78c41SJeff Roberson mi_switch(SW_VOL | SWT_TURNSTILE, NULL); 7469ed346baSBosko Milekic 747961a7b24SJohn Baldwin if (LOCK_LOG_TEST(lock, 0)) 748f5c157d9SJohn Baldwin CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s", 749f5c157d9SJohn Baldwin __func__, td->td_tid, lock, lock->lo_name); 7502502c107SJeff Roberson thread_unlock(td); 75136412d79SJohn Baldwin } 7529ed346baSBosko Milekic 753961a7b24SJohn Baldwin /* 754961a7b24SJohn Baldwin * Pick the highest priority thread on this turnstile and put it on the 755961a7b24SJohn Baldwin * pending list. This must be called with the turnstile chain locked. 756961a7b24SJohn Baldwin */ 757961a7b24SJohn Baldwin int 7587aa4f685SJohn Baldwin turnstile_signal(struct turnstile *ts, int queue) 759961a7b24SJohn Baldwin { 760961a7b24SJohn Baldwin struct turnstile_chain *tc; 761961a7b24SJohn Baldwin struct thread *td; 762961a7b24SJohn Baldwin int empty; 763961a7b24SJohn Baldwin 764961a7b24SJohn Baldwin MPASS(ts != NULL); 7652502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 766961a7b24SJohn Baldwin MPASS(curthread->td_proc->p_magic == P_MAGIC); 7675dff04c3SJeff Roberson MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL); 7687aa4f685SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 7699ed346baSBosko Milekic 7709ed346baSBosko Milekic /* 771961a7b24SJohn Baldwin * Pick the highest priority thread blocked on this lock and 772961a7b24SJohn Baldwin * move it to the pending list. 7739ed346baSBosko Milekic */ 7747aa4f685SJohn Baldwin td = TAILQ_FIRST(&ts->ts_blocked[queue]); 775b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 776961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 7777aa4f685SJohn Baldwin TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq); 778961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 779961a7b24SJohn Baldwin TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq); 7809ed346baSBosko Milekic 781961a7b24SJohn Baldwin /* 782961a7b24SJohn Baldwin * If the turnstile is now empty, remove it from its chain and 783961a7b24SJohn Baldwin * give it to the about-to-be-woken thread. Otherwise take a 784961a7b24SJohn Baldwin * turnstile from the free list and give it to the thread. 785961a7b24SJohn Baldwin */ 7867aa4f685SJohn Baldwin empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) && 7877aa4f685SJohn Baldwin TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]); 788ef0ebfc3SJohn Baldwin if (empty) { 7892502c107SJeff Roberson tc = TC_LOOKUP(ts->ts_lockobj); 7902502c107SJeff Roberson mtx_assert(&tc->tc_lock, MA_OWNED); 791961a7b24SJohn Baldwin MPASS(LIST_EMPTY(&ts->ts_free)); 792ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 793ef0ebfc3SJohn Baldwin tc->tc_depth--; 794ef0ebfc3SJohn Baldwin #endif 795ef0ebfc3SJohn Baldwin } else 796961a7b24SJohn Baldwin ts = LIST_FIRST(&ts->ts_free); 797da1d503bSJohn Baldwin MPASS(ts != NULL); 798961a7b24SJohn Baldwin LIST_REMOVE(ts, ts_hash); 799961a7b24SJohn Baldwin td->td_turnstile = ts; 8009ed346baSBosko Milekic 801961a7b24SJohn Baldwin return (empty); 802961a7b24SJohn Baldwin } 803961a7b24SJohn Baldwin 804961a7b24SJohn Baldwin /* 805961a7b24SJohn Baldwin * Put all blocked threads on the pending list. This must be called with 806961a7b24SJohn Baldwin * the turnstile chain locked. 807961a7b24SJohn Baldwin */ 808961a7b24SJohn Baldwin void 8097aa4f685SJohn Baldwin turnstile_broadcast(struct turnstile *ts, int queue) 810961a7b24SJohn Baldwin { 811961a7b24SJohn Baldwin struct turnstile_chain *tc; 812961a7b24SJohn Baldwin struct turnstile *ts1; 813961a7b24SJohn Baldwin struct thread *td; 814961a7b24SJohn Baldwin 815961a7b24SJohn Baldwin MPASS(ts != NULL); 8162502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 817961a7b24SJohn Baldwin MPASS(curthread->td_proc->p_magic == P_MAGIC); 8185dff04c3SJeff Roberson MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL); 8192502c107SJeff Roberson /* 8202502c107SJeff Roberson * We must have the chain locked so that we can remove the empty 8212502c107SJeff Roberson * turnstile from the hash queue. 8222502c107SJeff Roberson */ 823961a7b24SJohn Baldwin tc = TC_LOOKUP(ts->ts_lockobj); 824961a7b24SJohn Baldwin mtx_assert(&tc->tc_lock, MA_OWNED); 8257aa4f685SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 826961a7b24SJohn Baldwin 827961a7b24SJohn Baldwin /* 828961a7b24SJohn Baldwin * Transfer the blocked list to the pending list. 829961a7b24SJohn Baldwin */ 830961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 8317aa4f685SJohn Baldwin TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq); 832961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 833961a7b24SJohn Baldwin 834961a7b24SJohn Baldwin /* 835961a7b24SJohn Baldwin * Give a turnstile to each thread. The last thread gets 8367aa4f685SJohn Baldwin * this turnstile if the turnstile is empty. 837961a7b24SJohn Baldwin */ 838961a7b24SJohn Baldwin TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) { 839961a7b24SJohn Baldwin if (LIST_EMPTY(&ts->ts_free)) { 840961a7b24SJohn Baldwin MPASS(TAILQ_NEXT(td, td_lockq) == NULL); 841961a7b24SJohn Baldwin ts1 = ts; 842ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 843ef0ebfc3SJohn Baldwin tc->tc_depth--; 844ef0ebfc3SJohn Baldwin #endif 84536412d79SJohn Baldwin } else 846961a7b24SJohn Baldwin ts1 = LIST_FIRST(&ts->ts_free); 847da1d503bSJohn Baldwin MPASS(ts1 != NULL); 848961a7b24SJohn Baldwin LIST_REMOVE(ts1, ts_hash); 849961a7b24SJohn Baldwin td->td_turnstile = ts1; 850961a7b24SJohn Baldwin } 851961a7b24SJohn Baldwin } 8529ed346baSBosko Milekic 853961a7b24SJohn Baldwin /* 854961a7b24SJohn Baldwin * Wakeup all threads on the pending list and adjust the priority of the 855961a7b24SJohn Baldwin * current thread appropriately. This must be called with the turnstile 856961a7b24SJohn Baldwin * chain locked. 857961a7b24SJohn Baldwin */ 858961a7b24SJohn Baldwin void 8597aa4f685SJohn Baldwin turnstile_unpend(struct turnstile *ts, int owner_type) 860961a7b24SJohn Baldwin { 861961a7b24SJohn Baldwin TAILQ_HEAD( ,thread) pending_threads; 8622502c107SJeff Roberson struct turnstile *nts; 863961a7b24SJohn Baldwin struct thread *td; 864f5c157d9SJohn Baldwin u_char cp, pri; 865961a7b24SJohn Baldwin 866961a7b24SJohn Baldwin MPASS(ts != NULL); 8672502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 8685dff04c3SJeff Roberson MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL); 869961a7b24SJohn Baldwin MPASS(!TAILQ_EMPTY(&ts->ts_pending)); 870961a7b24SJohn Baldwin 871961a7b24SJohn Baldwin /* 872961a7b24SJohn Baldwin * Move the list of pending threads out of the turnstile and 873961a7b24SJohn Baldwin * into a local variable. 874961a7b24SJohn Baldwin */ 875961a7b24SJohn Baldwin TAILQ_INIT(&pending_threads); 876961a7b24SJohn Baldwin TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq); 877961a7b24SJohn Baldwin #ifdef INVARIANTS 8787aa4f685SJohn Baldwin if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) && 8797aa4f685SJohn Baldwin TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE])) 880961a7b24SJohn Baldwin ts->ts_lockobj = NULL; 881961a7b24SJohn Baldwin #endif 8822502c107SJeff Roberson /* 8832502c107SJeff Roberson * Adjust the priority of curthread based on other contested 8842502c107SJeff Roberson * locks it owns. Don't lower the priority below the base 8852502c107SJeff Roberson * priority however. 8862502c107SJeff Roberson */ 8872502c107SJeff Roberson td = curthread; 8882502c107SJeff Roberson pri = PRI_MAX; 8892502c107SJeff Roberson thread_lock(td); 8902502c107SJeff Roberson mtx_lock_spin(&td_contested_lock); 891961a7b24SJohn Baldwin /* 892961a7b24SJohn Baldwin * Remove the turnstile from this thread's list of contested locks 893961a7b24SJohn Baldwin * since this thread doesn't own it anymore. New threads will 894961a7b24SJohn Baldwin * not be blocking on the turnstile until it is claimed by a new 8957aa4f685SJohn Baldwin * owner. There might not be a current owner if this is a shared 8967aa4f685SJohn Baldwin * lock. 897961a7b24SJohn Baldwin */ 8987aa4f685SJohn Baldwin if (ts->ts_owner != NULL) { 899961a7b24SJohn Baldwin ts->ts_owner = NULL; 900961a7b24SJohn Baldwin LIST_REMOVE(ts, ts_link); 9017aa4f685SJohn Baldwin } 9022502c107SJeff Roberson LIST_FOREACH(nts, &td->td_contested, ts_link) { 9032502c107SJeff Roberson cp = turnstile_first_waiter(nts)->td_priority; 90436412d79SJohn Baldwin if (cp < pri) 90536412d79SJohn Baldwin pri = cp; 90636412d79SJohn Baldwin } 907961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 908f5c157d9SJohn Baldwin sched_unlend_prio(td, pri); 9092502c107SJeff Roberson thread_unlock(td); 910961a7b24SJohn Baldwin /* 911961a7b24SJohn Baldwin * Wake up all the pending threads. If a thread is not blocked 912961a7b24SJohn Baldwin * on a lock, then it is currently executing on another CPU in 91367ba8678SJohn Baldwin * turnstile_wait() or sitting on a run queue waiting to resume 91467ba8678SJohn Baldwin * in turnstile_wait(). Set a flag to force it to try to acquire 915961a7b24SJohn Baldwin * the lock again instead of blocking. 916961a7b24SJohn Baldwin */ 917961a7b24SJohn Baldwin while (!TAILQ_EMPTY(&pending_threads)) { 918961a7b24SJohn Baldwin td = TAILQ_FIRST(&pending_threads); 919961a7b24SJohn Baldwin TAILQ_REMOVE(&pending_threads, td, td_lockq); 9202502c107SJeff Roberson thread_lock(td); 921626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 922961a7b24SJohn Baldwin MPASS(td->td_proc->p_magic == P_MAGIC); 9232502c107SJeff Roberson MPASS(TD_ON_LOCK(td)); 9242502c107SJeff Roberson TD_CLR_LOCK(td); 9252502c107SJeff Roberson MPASS(TD_CAN_RUN(td)); 926961a7b24SJohn Baldwin td->td_blocked = NULL; 927961a7b24SJohn Baldwin td->td_lockname = NULL; 928f7829d0dSAttilio Rao td->td_blktick = 0; 9297aa4f685SJohn Baldwin #ifdef INVARIANTS 9307aa4f685SJohn Baldwin td->td_tsqueue = 0xff; 9317aa4f685SJohn Baldwin #endif 932f0393f06SJeff Roberson sched_add(td, SRQ_BORING); 9332502c107SJeff Roberson thread_unlock(td); 934961a7b24SJohn Baldwin } 9352502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 9369ed346baSBosko Milekic } 9379ed346baSBosko Milekic 9389ed346baSBosko Milekic /* 939f1a4b852SJohn Baldwin * Give up ownership of a turnstile. This must be called with the 940f1a4b852SJohn Baldwin * turnstile chain locked. 941f1a4b852SJohn Baldwin */ 942f1a4b852SJohn Baldwin void 943f1a4b852SJohn Baldwin turnstile_disown(struct turnstile *ts) 944f1a4b852SJohn Baldwin { 945f1a4b852SJohn Baldwin struct thread *td; 946f1a4b852SJohn Baldwin u_char cp, pri; 947f1a4b852SJohn Baldwin 948f1a4b852SJohn Baldwin MPASS(ts != NULL); 9492502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 950f1a4b852SJohn Baldwin MPASS(ts->ts_owner == curthread); 951f1a4b852SJohn Baldwin MPASS(TAILQ_EMPTY(&ts->ts_pending)); 952f1a4b852SJohn Baldwin MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) || 953f1a4b852SJohn Baldwin !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE])); 954f1a4b852SJohn Baldwin 955f1a4b852SJohn Baldwin /* 956f1a4b852SJohn Baldwin * Remove the turnstile from this thread's list of contested locks 957f1a4b852SJohn Baldwin * since this thread doesn't own it anymore. New threads will 958f1a4b852SJohn Baldwin * not be blocking on the turnstile until it is claimed by a new 959f1a4b852SJohn Baldwin * owner. 960f1a4b852SJohn Baldwin */ 961f1a4b852SJohn Baldwin mtx_lock_spin(&td_contested_lock); 962f1a4b852SJohn Baldwin ts->ts_owner = NULL; 963f1a4b852SJohn Baldwin LIST_REMOVE(ts, ts_link); 964f1a4b852SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 965f1a4b852SJohn Baldwin 966f1a4b852SJohn Baldwin /* 967f1a4b852SJohn Baldwin * Adjust the priority of curthread based on other contested 968f1a4b852SJohn Baldwin * locks it owns. Don't lower the priority below the base 969f1a4b852SJohn Baldwin * priority however. 970f1a4b852SJohn Baldwin */ 971f1a4b852SJohn Baldwin td = curthread; 972f1a4b852SJohn Baldwin pri = PRI_MAX; 9732502c107SJeff Roberson thread_lock(td); 9742502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 975f1a4b852SJohn Baldwin mtx_lock_spin(&td_contested_lock); 976f1a4b852SJohn Baldwin LIST_FOREACH(ts, &td->td_contested, ts_link) { 977f1a4b852SJohn Baldwin cp = turnstile_first_waiter(ts)->td_priority; 978f1a4b852SJohn Baldwin if (cp < pri) 979f1a4b852SJohn Baldwin pri = cp; 980f1a4b852SJohn Baldwin } 981f1a4b852SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 982f1a4b852SJohn Baldwin sched_unlend_prio(td, pri); 9832502c107SJeff Roberson thread_unlock(td); 984f1a4b852SJohn Baldwin } 985f1a4b852SJohn Baldwin 986f1a4b852SJohn Baldwin /* 987961a7b24SJohn Baldwin * Return the first thread in a turnstile. 9889ed346baSBosko Milekic */ 989961a7b24SJohn Baldwin struct thread * 9907aa4f685SJohn Baldwin turnstile_head(struct turnstile *ts, int queue) 9910cde2e34SJason Evans { 992961a7b24SJohn Baldwin #ifdef INVARIANTS 9935cb0fbe4SJohn Baldwin 994961a7b24SJohn Baldwin MPASS(ts != NULL); 9957aa4f685SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 9962502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 9970cde2e34SJason Evans #endif 9987aa4f685SJohn Baldwin return (TAILQ_FIRST(&ts->ts_blocked[queue])); 999961a7b24SJohn Baldwin } 10007aa4f685SJohn Baldwin 1001f1a4b852SJohn Baldwin /* 1002f1a4b852SJohn Baldwin * Returns true if a sub-queue of a turnstile is empty. 1003f1a4b852SJohn Baldwin */ 1004f1a4b852SJohn Baldwin int 1005f1a4b852SJohn Baldwin turnstile_empty(struct turnstile *ts, int queue) 1006f1a4b852SJohn Baldwin { 1007f1a4b852SJohn Baldwin #ifdef INVARIANTS 1008f1a4b852SJohn Baldwin 1009f1a4b852SJohn Baldwin MPASS(ts != NULL); 1010f1a4b852SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 10112502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 1012f1a4b852SJohn Baldwin #endif 1013f1a4b852SJohn Baldwin return (TAILQ_EMPTY(&ts->ts_blocked[queue])); 1014f1a4b852SJohn Baldwin } 1015f1a4b852SJohn Baldwin 10167aa4f685SJohn Baldwin #ifdef DDB 10177aa4f685SJohn Baldwin static void 10187aa4f685SJohn Baldwin print_thread(struct thread *td, const char *prefix) 10197aa4f685SJohn Baldwin { 10207aa4f685SJohn Baldwin 10217aa4f685SJohn Baldwin db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid, 1022f9ab2f13SJohn Baldwin td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name : 1023431f8906SJulian Elischer td->td_name); 10247aa4f685SJohn Baldwin } 10257aa4f685SJohn Baldwin 10267aa4f685SJohn Baldwin static void 10277aa4f685SJohn Baldwin print_queue(struct threadqueue *queue, const char *header, const char *prefix) 10287aa4f685SJohn Baldwin { 10297aa4f685SJohn Baldwin struct thread *td; 10307aa4f685SJohn Baldwin 10317aa4f685SJohn Baldwin db_printf("%s:\n", header); 10327aa4f685SJohn Baldwin if (TAILQ_EMPTY(queue)) { 10337aa4f685SJohn Baldwin db_printf("%sempty\n", prefix); 10347aa4f685SJohn Baldwin return; 10357aa4f685SJohn Baldwin } 10367aa4f685SJohn Baldwin TAILQ_FOREACH(td, queue, td_lockq) { 10377aa4f685SJohn Baldwin print_thread(td, prefix); 10387aa4f685SJohn Baldwin } 10397aa4f685SJohn Baldwin } 10407aa4f685SJohn Baldwin 10417aa4f685SJohn Baldwin DB_SHOW_COMMAND(turnstile, db_show_turnstile) 10427aa4f685SJohn Baldwin { 10437aa4f685SJohn Baldwin struct turnstile_chain *tc; 10447aa4f685SJohn Baldwin struct turnstile *ts; 10457aa4f685SJohn Baldwin struct lock_object *lock; 10467aa4f685SJohn Baldwin int i; 10477aa4f685SJohn Baldwin 10487aa4f685SJohn Baldwin if (!have_addr) 10497aa4f685SJohn Baldwin return; 10507aa4f685SJohn Baldwin 10517aa4f685SJohn Baldwin /* 10527aa4f685SJohn Baldwin * First, see if there is an active turnstile for the lock indicated 10537aa4f685SJohn Baldwin * by the address. 10547aa4f685SJohn Baldwin */ 10557aa4f685SJohn Baldwin lock = (struct lock_object *)addr; 10567aa4f685SJohn Baldwin tc = TC_LOOKUP(lock); 10577aa4f685SJohn Baldwin LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash) 10587aa4f685SJohn Baldwin if (ts->ts_lockobj == lock) 10597aa4f685SJohn Baldwin goto found; 10607aa4f685SJohn Baldwin 10617aa4f685SJohn Baldwin /* 10627aa4f685SJohn Baldwin * Second, see if there is an active turnstile at the address 10637aa4f685SJohn Baldwin * indicated. 10647aa4f685SJohn Baldwin */ 10657aa4f685SJohn Baldwin for (i = 0; i < TC_TABLESIZE; i++) 10667aa4f685SJohn Baldwin LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) { 10677aa4f685SJohn Baldwin if (ts == (struct turnstile *)addr) 10687aa4f685SJohn Baldwin goto found; 10697aa4f685SJohn Baldwin } 10707aa4f685SJohn Baldwin 10717aa4f685SJohn Baldwin db_printf("Unable to locate a turnstile via %p\n", (void *)addr); 10727aa4f685SJohn Baldwin return; 10737aa4f685SJohn Baldwin found: 10747aa4f685SJohn Baldwin lock = ts->ts_lockobj; 10757aa4f685SJohn Baldwin db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name, 10767aa4f685SJohn Baldwin lock->lo_name); 10777aa4f685SJohn Baldwin if (ts->ts_owner) 10787aa4f685SJohn Baldwin print_thread(ts->ts_owner, "Lock Owner: "); 10797aa4f685SJohn Baldwin else 10807aa4f685SJohn Baldwin db_printf("Lock Owner: none\n"); 10817aa4f685SJohn Baldwin print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t"); 10827aa4f685SJohn Baldwin print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters", 10837aa4f685SJohn Baldwin "\t"); 10847aa4f685SJohn Baldwin print_queue(&ts->ts_pending, "Pending Threads", "\t"); 10857aa4f685SJohn Baldwin 10867aa4f685SJohn Baldwin } 1087ae110b53SJohn Baldwin 108877e66268SJohn Baldwin /* 108977e66268SJohn Baldwin * Show all the threads a particular thread is waiting on based on 109077e66268SJohn Baldwin * non-sleepable and non-spin locks. 109177e66268SJohn Baldwin */ 1092ae110b53SJohn Baldwin static void 109377e66268SJohn Baldwin print_lockchain(struct thread *td, const char *prefix) 1094ae110b53SJohn Baldwin { 1095ae110b53SJohn Baldwin struct lock_object *lock; 1096ae110b53SJohn Baldwin struct lock_class *class; 1097ae110b53SJohn Baldwin struct turnstile *ts; 1098ae110b53SJohn Baldwin 1099ae110b53SJohn Baldwin /* 1100ae110b53SJohn Baldwin * Follow the chain. We keep walking as long as the thread is 1101ae110b53SJohn Baldwin * blocked on a turnstile that has an owner. 1102ae110b53SJohn Baldwin */ 1103fed79884SJohn Baldwin while (!db_pager_quit) { 1104ae110b53SJohn Baldwin db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid, 1105ae110b53SJohn Baldwin td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name : 1106431f8906SJulian Elischer td->td_name); 1107ae110b53SJohn Baldwin switch (td->td_state) { 1108ae110b53SJohn Baldwin case TDS_INACTIVE: 1109ae110b53SJohn Baldwin db_printf("is inactive\n"); 1110ae110b53SJohn Baldwin return; 1111ae110b53SJohn Baldwin case TDS_CAN_RUN: 1112ae110b53SJohn Baldwin db_printf("can run\n"); 1113ae110b53SJohn Baldwin return; 1114ae110b53SJohn Baldwin case TDS_RUNQ: 1115ae110b53SJohn Baldwin db_printf("is on a run queue\n"); 1116ae110b53SJohn Baldwin return; 1117ae110b53SJohn Baldwin case TDS_RUNNING: 1118ae110b53SJohn Baldwin db_printf("running on CPU %d\n", td->td_oncpu); 1119ae110b53SJohn Baldwin return; 1120ae110b53SJohn Baldwin case TDS_INHIBITED: 1121ae110b53SJohn Baldwin if (TD_ON_LOCK(td)) { 1122ae110b53SJohn Baldwin ts = td->td_blocked; 1123ae110b53SJohn Baldwin lock = ts->ts_lockobj; 1124ae110b53SJohn Baldwin class = LOCK_CLASS(lock); 1125ae110b53SJohn Baldwin db_printf("blocked on lock %p (%s) \"%s\"\n", 1126ae110b53SJohn Baldwin lock, class->lc_name, lock->lo_name); 1127ae110b53SJohn Baldwin if (ts->ts_owner == NULL) 1128ae110b53SJohn Baldwin return; 1129ae110b53SJohn Baldwin td = ts->ts_owner; 1130ae110b53SJohn Baldwin break; 1131ae110b53SJohn Baldwin } 1132ae110b53SJohn Baldwin db_printf("inhibited\n"); 1133ae110b53SJohn Baldwin return; 1134ae110b53SJohn Baldwin default: 1135ae110b53SJohn Baldwin db_printf("??? (%#x)\n", td->td_state); 1136ae110b53SJohn Baldwin return; 1137ae110b53SJohn Baldwin } 1138ae110b53SJohn Baldwin } 1139ae110b53SJohn Baldwin } 1140ae110b53SJohn Baldwin 114177e66268SJohn Baldwin DB_SHOW_COMMAND(lockchain, db_show_lockchain) 1142ae110b53SJohn Baldwin { 1143ae110b53SJohn Baldwin struct thread *td; 1144ae110b53SJohn Baldwin 1145ae110b53SJohn Baldwin /* Figure out which thread to start with. */ 1146ae110b53SJohn Baldwin if (have_addr) 1147ae110b53SJohn Baldwin td = db_lookup_thread(addr, TRUE); 1148ae110b53SJohn Baldwin else 1149ae110b53SJohn Baldwin td = kdb_thread; 1150ae110b53SJohn Baldwin 115177e66268SJohn Baldwin print_lockchain(td, ""); 1152ae110b53SJohn Baldwin } 1153ae110b53SJohn Baldwin 115439297ba4SSam Leffler DB_SHOW_ALL_COMMAND(chains, db_show_allchains) 1155ae110b53SJohn Baldwin { 1156ae110b53SJohn Baldwin struct thread *td; 1157ae110b53SJohn Baldwin struct proc *p; 1158ae110b53SJohn Baldwin int i; 1159ae110b53SJohn Baldwin 1160ae110b53SJohn Baldwin i = 1; 11614f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1162ae110b53SJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) { 1163ae110b53SJohn Baldwin if (TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested)) { 1164ae110b53SJohn Baldwin db_printf("chain %d:\n", i++); 116577e66268SJohn Baldwin print_lockchain(td, " "); 1166ae110b53SJohn Baldwin } 1167fed79884SJohn Baldwin if (db_pager_quit) 1168fed79884SJohn Baldwin return; 1169ae110b53SJohn Baldwin } 1170ae110b53SJohn Baldwin } 1171ae110b53SJohn Baldwin } 117239297ba4SSam Leffler DB_SHOW_ALIAS(allchains, db_show_allchains) 1173ae110b53SJohn Baldwin 1174462a7addSJohn Baldwin /* 1175462a7addSJohn Baldwin * Show all the threads a particular thread is waiting on based on 1176462a7addSJohn Baldwin * sleepable locks. 1177462a7addSJohn Baldwin */ 1178462a7addSJohn Baldwin static void 1179462a7addSJohn Baldwin print_sleepchain(struct thread *td, const char *prefix) 1180462a7addSJohn Baldwin { 1181462a7addSJohn Baldwin struct thread *owner; 1182462a7addSJohn Baldwin 1183462a7addSJohn Baldwin /* 1184462a7addSJohn Baldwin * Follow the chain. We keep walking as long as the thread is 1185462a7addSJohn Baldwin * blocked on a sleep lock that has an owner. 1186462a7addSJohn Baldwin */ 1187462a7addSJohn Baldwin while (!db_pager_quit) { 1188462a7addSJohn Baldwin db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid, 1189462a7addSJohn Baldwin td->td_proc->p_pid, td->td_name[0] != '\0' ? td->td_name : 1190431f8906SJulian Elischer td->td_name); 1191462a7addSJohn Baldwin switch (td->td_state) { 1192462a7addSJohn Baldwin case TDS_INACTIVE: 1193462a7addSJohn Baldwin db_printf("is inactive\n"); 1194462a7addSJohn Baldwin return; 1195462a7addSJohn Baldwin case TDS_CAN_RUN: 1196462a7addSJohn Baldwin db_printf("can run\n"); 1197462a7addSJohn Baldwin return; 1198462a7addSJohn Baldwin case TDS_RUNQ: 1199462a7addSJohn Baldwin db_printf("is on a run queue\n"); 1200462a7addSJohn Baldwin return; 1201462a7addSJohn Baldwin case TDS_RUNNING: 1202462a7addSJohn Baldwin db_printf("running on CPU %d\n", td->td_oncpu); 1203462a7addSJohn Baldwin return; 1204462a7addSJohn Baldwin case TDS_INHIBITED: 1205462a7addSJohn Baldwin if (TD_ON_SLEEPQ(td)) { 1206462a7addSJohn Baldwin if (lockmgr_chain(td, &owner) || 1207462a7addSJohn Baldwin sx_chain(td, &owner)) { 1208462a7addSJohn Baldwin if (owner == NULL) 1209462a7addSJohn Baldwin return; 1210462a7addSJohn Baldwin td = owner; 1211462a7addSJohn Baldwin break; 1212462a7addSJohn Baldwin } 1213462a7addSJohn Baldwin db_printf("sleeping on %p \"%s\"\n", 1214462a7addSJohn Baldwin td->td_wchan, td->td_wmesg); 1215462a7addSJohn Baldwin return; 1216462a7addSJohn Baldwin } 1217462a7addSJohn Baldwin db_printf("inhibited\n"); 1218462a7addSJohn Baldwin return; 1219462a7addSJohn Baldwin default: 1220462a7addSJohn Baldwin db_printf("??? (%#x)\n", td->td_state); 1221462a7addSJohn Baldwin return; 1222462a7addSJohn Baldwin } 1223462a7addSJohn Baldwin } 1224462a7addSJohn Baldwin } 1225462a7addSJohn Baldwin 1226462a7addSJohn Baldwin DB_SHOW_COMMAND(sleepchain, db_show_sleepchain) 1227462a7addSJohn Baldwin { 1228462a7addSJohn Baldwin struct thread *td; 1229462a7addSJohn Baldwin 1230462a7addSJohn Baldwin /* Figure out which thread to start with. */ 1231462a7addSJohn Baldwin if (have_addr) 1232462a7addSJohn Baldwin td = db_lookup_thread(addr, TRUE); 1233462a7addSJohn Baldwin else 1234462a7addSJohn Baldwin td = kdb_thread; 1235462a7addSJohn Baldwin 1236462a7addSJohn Baldwin print_sleepchain(td, ""); 1237462a7addSJohn Baldwin } 1238462a7addSJohn Baldwin 1239ae110b53SJohn Baldwin static void print_waiters(struct turnstile *ts, int indent); 1240ae110b53SJohn Baldwin 1241ae110b53SJohn Baldwin static void 1242ae110b53SJohn Baldwin print_waiter(struct thread *td, int indent) 1243ae110b53SJohn Baldwin { 1244ae110b53SJohn Baldwin struct turnstile *ts; 1245ae110b53SJohn Baldwin int i; 1246ae110b53SJohn Baldwin 1247fed79884SJohn Baldwin if (db_pager_quit) 1248fed79884SJohn Baldwin return; 1249ae110b53SJohn Baldwin for (i = 0; i < indent; i++) 1250ae110b53SJohn Baldwin db_printf(" "); 1251ae110b53SJohn Baldwin print_thread(td, "thread "); 1252ae110b53SJohn Baldwin LIST_FOREACH(ts, &td->td_contested, ts_link) 1253ae110b53SJohn Baldwin print_waiters(ts, indent + 1); 1254ae110b53SJohn Baldwin } 1255ae110b53SJohn Baldwin 1256ae110b53SJohn Baldwin static void 1257ae110b53SJohn Baldwin print_waiters(struct turnstile *ts, int indent) 1258ae110b53SJohn Baldwin { 1259ae110b53SJohn Baldwin struct lock_object *lock; 1260ae110b53SJohn Baldwin struct lock_class *class; 1261ae110b53SJohn Baldwin struct thread *td; 1262ae110b53SJohn Baldwin int i; 1263ae110b53SJohn Baldwin 1264fed79884SJohn Baldwin if (db_pager_quit) 1265fed79884SJohn Baldwin return; 1266ae110b53SJohn Baldwin lock = ts->ts_lockobj; 1267ae110b53SJohn Baldwin class = LOCK_CLASS(lock); 1268ae110b53SJohn Baldwin for (i = 0; i < indent; i++) 1269ae110b53SJohn Baldwin db_printf(" "); 1270ae110b53SJohn Baldwin db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name); 1271ae110b53SJohn Baldwin TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq) 1272ae110b53SJohn Baldwin print_waiter(td, indent + 1); 1273ae110b53SJohn Baldwin TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq) 1274ae110b53SJohn Baldwin print_waiter(td, indent + 1); 1275ae110b53SJohn Baldwin TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) 1276ae110b53SJohn Baldwin print_waiter(td, indent + 1); 1277ae110b53SJohn Baldwin } 1278ae110b53SJohn Baldwin 127977e66268SJohn Baldwin DB_SHOW_COMMAND(locktree, db_show_locktree) 1280ae110b53SJohn Baldwin { 1281ae110b53SJohn Baldwin struct lock_object *lock; 1282ae110b53SJohn Baldwin struct lock_class *class; 1283ae110b53SJohn Baldwin struct turnstile_chain *tc; 1284ae110b53SJohn Baldwin struct turnstile *ts; 1285ae110b53SJohn Baldwin 1286ae110b53SJohn Baldwin if (!have_addr) 1287ae110b53SJohn Baldwin return; 1288ae110b53SJohn Baldwin lock = (struct lock_object *)addr; 1289ae110b53SJohn Baldwin tc = TC_LOOKUP(lock); 1290ae110b53SJohn Baldwin LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash) 1291ae110b53SJohn Baldwin if (ts->ts_lockobj == lock) 1292ae110b53SJohn Baldwin break; 1293ae110b53SJohn Baldwin if (ts == NULL) { 1294ae110b53SJohn Baldwin class = LOCK_CLASS(lock); 1295ae110b53SJohn Baldwin db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, 1296ae110b53SJohn Baldwin lock->lo_name); 1297ae110b53SJohn Baldwin } else 1298ae110b53SJohn Baldwin print_waiters(ts, 0); 1299ae110b53SJohn Baldwin } 13007aa4f685SJohn Baldwin #endif 1301