10384fff8SJason Evans /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a36da99SPedro F. Giffuni * 40384fff8SJason Evans * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 50384fff8SJason Evans * 60384fff8SJason Evans * Redistribution and use in source and binary forms, with or without 70384fff8SJason Evans * modification, are permitted provided that the following conditions 80384fff8SJason Evans * are met: 90384fff8SJason Evans * 1. Redistributions of source code must retain the above copyright 100384fff8SJason Evans * notice, this list of conditions and the following disclaimer. 110384fff8SJason Evans * 2. Redistributions in binary form must reproduce the above copyright 120384fff8SJason Evans * notice, this list of conditions and the following disclaimer in the 130384fff8SJason Evans * documentation and/or other materials provided with the distribution. 140384fff8SJason Evans * 3. Berkeley Software Design Inc's name may not be used to endorse or 150384fff8SJason Evans * promote products derived from this software without specific prior 160384fff8SJason Evans * written permission. 170384fff8SJason Evans * 180384fff8SJason Evans * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 190384fff8SJason Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 200384fff8SJason Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 210384fff8SJason Evans * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 220384fff8SJason Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 230384fff8SJason Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 240384fff8SJason Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 250384fff8SJason Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 260384fff8SJason Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 270384fff8SJason Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 280384fff8SJason Evans * SUCH DAMAGE. 290384fff8SJason Evans * 300384fff8SJason Evans * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 3136412d79SJohn Baldwin * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 320384fff8SJason Evans */ 330384fff8SJason Evans 340384fff8SJason Evans /* 35961a7b24SJohn Baldwin * Implementation of turnstiles used to hold queue of threads blocked on 36961a7b24SJohn Baldwin * non-sleepable locks. Sleepable locks use condition variables to 37961a7b24SJohn Baldwin * implement their queues. Turnstiles differ from a sleep queue in that 38961a7b24SJohn Baldwin * turnstile queue's are assigned to a lock held by an owning thread. Thus, 39961a7b24SJohn Baldwin * when one thread is enqueued onto a turnstile, it can lend its priority 40961a7b24SJohn Baldwin * to the owning thread. 41961a7b24SJohn Baldwin * 42961a7b24SJohn Baldwin * We wish to avoid bloating locks with an embedded turnstile and we do not 43961a7b24SJohn Baldwin * want to use back-pointers in the locks for the same reason. Thus, we 44961a7b24SJohn Baldwin * use a similar approach to that of Solaris 7 as described in Solaris 45961a7b24SJohn Baldwin * Internals by Jim Mauro and Richard McDougall. Turnstiles are looked up 46961a7b24SJohn Baldwin * in a hash table based on the address of the lock. Each entry in the 47961a7b24SJohn Baldwin * hash table is a linked-lists of turnstiles and is called a turnstile 48961a7b24SJohn Baldwin * chain. Each chain contains a spin mutex that protects all of the 49961a7b24SJohn Baldwin * turnstiles in the chain. 50961a7b24SJohn Baldwin * 512b7e2ee7SJeff Roberson * Each time a thread is created, a turnstile is allocated from a UMA zone 522b7e2ee7SJeff Roberson * and attached to that thread. When a thread blocks on a lock, if it is the 532b7e2ee7SJeff Roberson * first thread to block, it lends its turnstile to the lock. If the lock 542b7e2ee7SJeff Roberson * already has a turnstile, then it gives its turnstile to the lock's 552b7e2ee7SJeff Roberson * turnstile's free list. When a thread is woken up, it takes a turnstile from 562b7e2ee7SJeff Roberson * the free list if there are any other waiters. If it is the only thread 572b7e2ee7SJeff Roberson * blocked on the lock, then it reclaims the turnstile associated with the lock 582b7e2ee7SJeff Roberson * and removes it from the hash table. 590384fff8SJason Evans */ 600384fff8SJason Evans 61677b542eSDavid E. O'Brien #include <sys/cdefs.h> 62677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 63677b542eSDavid E. O'Brien 647aa4f685SJohn Baldwin #include "opt_ddb.h" 657aa4f685SJohn Baldwin #include "opt_turnstile_profiling.h" 663036ab79SJeff Roberson #include "opt_sched.h" 677aa4f685SJohn Baldwin 680384fff8SJason Evans #include <sys/param.h> 696c35e809SDag-Erling Smørgrav #include <sys/systm.h> 7091849f34SMarius Strobl #include <sys/kdb.h> 7136412d79SJohn Baldwin #include <sys/kernel.h> 726c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 7319284646SJohn Baldwin #include <sys/lock.h> 7419284646SJohn Baldwin #include <sys/mutex.h> 750384fff8SJason Evans #include <sys/proc.h> 76961a7b24SJohn Baldwin #include <sys/queue.h> 77b43179fbSJeff Roberson #include <sys/sched.h> 78b3e9e682SRyan Stone #include <sys/sdt.h> 79ef0ebfc3SJohn Baldwin #include <sys/sysctl.h> 80ef0ebfc3SJohn Baldwin #include <sys/turnstile.h> 8136412d79SJohn Baldwin 822b7e2ee7SJeff Roberson #include <vm/uma.h> 832b7e2ee7SJeff Roberson 847aa4f685SJohn Baldwin #ifdef DDB 857aa4f685SJohn Baldwin #include <ddb/ddb.h> 86462a7addSJohn Baldwin #include <sys/lockmgr.h> 87462a7addSJohn Baldwin #include <sys/sx.h> 887aa4f685SJohn Baldwin #endif 897aa4f685SJohn Baldwin 900cde2e34SJason Evans /* 91961a7b24SJohn Baldwin * Constants for the hash table of turnstile chains. TC_SHIFT is a magic 92961a7b24SJohn Baldwin * number chosen because the sleep queue's use the same value for the 93961a7b24SJohn Baldwin * shift. Basically, we ignore the lower 8 bits of the address. 94961a7b24SJohn Baldwin * TC_TABLESIZE must be a power of two for TC_MASK to work properly. 950cde2e34SJason Evans */ 96961a7b24SJohn Baldwin #define TC_TABLESIZE 128 /* Must be power of 2. */ 97961a7b24SJohn Baldwin #define TC_MASK (TC_TABLESIZE - 1) 98961a7b24SJohn Baldwin #define TC_SHIFT 8 99961a7b24SJohn Baldwin #define TC_HASH(lock) (((uintptr_t)(lock) >> TC_SHIFT) & TC_MASK) 100961a7b24SJohn Baldwin #define TC_LOOKUP(lock) &turnstile_chains[TC_HASH(lock)] 1019ed346baSBosko Milekic 1020cde2e34SJason Evans /* 103961a7b24SJohn Baldwin * There are three different lists of turnstiles as follows. The list 104961a7b24SJohn Baldwin * connected by ts_link entries is a per-thread list of all the turnstiles 105961a7b24SJohn Baldwin * attached to locks that we own. This is used to fixup our priority when 106961a7b24SJohn Baldwin * a lock is released. The other two lists use the ts_hash entries. The 1075b7de7e1SJohn Baldwin * first of these two is the turnstile chain list that a turnstile is on 1085b7de7e1SJohn Baldwin * when it is attached to a lock. The second list to use ts_hash is the 1095b7de7e1SJohn Baldwin * free list hung off of a turnstile that is attached to a lock. 110961a7b24SJohn Baldwin * 1117aa4f685SJohn Baldwin * Each turnstile contains three lists of threads. The two ts_blocked lists 1127aa4f685SJohn Baldwin * are linked list of threads blocked on the turnstile's lock. One list is 1137aa4f685SJohn Baldwin * for exclusive waiters, and the other is for shared waiters. The 114595bc82aSJohn Baldwin * ts_pending list is a linked list of threads previously awakened by 115961a7b24SJohn Baldwin * turnstile_signal() or turnstile_wait() that are waiting to be put on 116961a7b24SJohn Baldwin * the run queue. 117961a7b24SJohn Baldwin * 118961a7b24SJohn Baldwin * Locking key: 119961a7b24SJohn Baldwin * c - turnstile chain lock 120961a7b24SJohn Baldwin * q - td_contested lock 1210cde2e34SJason Evans */ 122961a7b24SJohn Baldwin struct turnstile { 1232502c107SJeff Roberson struct mtx ts_lock; /* Spin lock for self. */ 1247aa4f685SJohn Baldwin struct threadqueue ts_blocked[2]; /* (c + q) Blocked threads. */ 1257aa4f685SJohn Baldwin struct threadqueue ts_pending; /* (c) Pending threads. */ 126961a7b24SJohn Baldwin LIST_ENTRY(turnstile) ts_hash; /* (c) Chain and free list. */ 127961a7b24SJohn Baldwin LIST_ENTRY(turnstile) ts_link; /* (q) Contested locks. */ 128961a7b24SJohn Baldwin LIST_HEAD(, turnstile) ts_free; /* (c) Free turnstiles. */ 129961a7b24SJohn Baldwin struct lock_object *ts_lockobj; /* (c) Lock we reference. */ 13079a13d01SJohn Baldwin struct thread *ts_owner; /* (c + q) Who owns the lock. */ 1318484de75SJohn Baldwin }; 1328484de75SJohn Baldwin 133961a7b24SJohn Baldwin struct turnstile_chain { 134961a7b24SJohn Baldwin LIST_HEAD(, turnstile) tc_turnstiles; /* List of turnstiles. */ 135961a7b24SJohn Baldwin struct mtx tc_lock; /* Spin lock for this chain. */ 136ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 137ef0ebfc3SJohn Baldwin u_int tc_depth; /* Length of tc_queues. */ 138ef0ebfc3SJohn Baldwin u_int tc_max_depth; /* Max length of tc_queues. */ 139ef0ebfc3SJohn Baldwin #endif 140961a7b24SJohn Baldwin }; 141961a7b24SJohn Baldwin 142ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 143ef0ebfc3SJohn Baldwin u_int turnstile_max_depth; 1446472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, turnstile, CTLFLAG_RD, 0, 1456472ac3dSEd Schouten "turnstile profiling"); 1466472ac3dSEd Schouten static SYSCTL_NODE(_debug_turnstile, OID_AUTO, chains, CTLFLAG_RD, 0, 147ef0ebfc3SJohn Baldwin "turnstile chain stats"); 148ef0ebfc3SJohn Baldwin SYSCTL_UINT(_debug_turnstile, OID_AUTO, max_depth, CTLFLAG_RD, 14999006d44SDavide Italiano &turnstile_max_depth, 0, "maximum depth achieved of a single chain"); 150ef0ebfc3SJohn Baldwin #endif 151961a7b24SJohn Baldwin static struct mtx td_contested_lock; 152961a7b24SJohn Baldwin static struct turnstile_chain turnstile_chains[TC_TABLESIZE]; 1532b7e2ee7SJeff Roberson static uma_zone_t turnstile_zone; 154c53c013bSJohn Baldwin 155c53c013bSJohn Baldwin /* 1569ed346baSBosko Milekic * Prototypes for non-exported routines. 1579ed346baSBosko Milekic */ 158961a7b24SJohn Baldwin static void init_turnstile0(void *dummy); 15901bd10e1SJohn Baldwin #ifdef TURNSTILE_PROFILING 16001bd10e1SJohn Baldwin static void init_turnstile_profiling(void *arg); 16101bd10e1SJohn Baldwin #endif 162f5c157d9SJohn Baldwin static void propagate_priority(struct thread *td); 163f5c157d9SJohn Baldwin static int turnstile_adjust_thread(struct turnstile *ts, 164f5c157d9SJohn Baldwin struct thread *td); 1657aa4f685SJohn Baldwin static struct thread *turnstile_first_waiter(struct turnstile *ts); 166961a7b24SJohn Baldwin static void turnstile_setowner(struct turnstile *ts, struct thread *owner); 1672b7e2ee7SJeff Roberson #ifdef INVARIANTS 1682b7e2ee7SJeff Roberson static void turnstile_dtor(void *mem, int size, void *arg); 1692b7e2ee7SJeff Roberson #endif 1702b7e2ee7SJeff Roberson static int turnstile_init(void *mem, int size, int flags); 1712502c107SJeff Roberson static void turnstile_fini(void *mem, int size); 17236412d79SJohn Baldwin 173b3e9e682SRyan Stone SDT_PROVIDER_DECLARE(sched); 174d9fae5abSAndriy Gapon SDT_PROBE_DEFINE(sched, , , sleep); 175d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(sched, , , wakeup, "struct thread *", 176b3e9e682SRyan Stone "struct proc *"); 177b3e9e682SRyan Stone 178961a7b24SJohn Baldwin /* 179961a7b24SJohn Baldwin * Walks the chain of turnstiles and their owners to propagate the priority 180961a7b24SJohn Baldwin * of the thread being blocked to all the threads holding locks that have to 181961a7b24SJohn Baldwin * release their locks before this thread can run again. 182961a7b24SJohn Baldwin */ 18336412d79SJohn Baldwin static void 184b40ce416SJulian Elischer propagate_priority(struct thread *td) 18536412d79SJohn Baldwin { 186961a7b24SJohn Baldwin struct turnstile *ts; 187961a7b24SJohn Baldwin int pri; 18836412d79SJohn Baldwin 1892502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 190961a7b24SJohn Baldwin pri = td->td_priority; 191961a7b24SJohn Baldwin ts = td->td_blocked; 192626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 1932502c107SJeff Roberson /* 1942502c107SJeff Roberson * Grab a recursive lock on this turnstile chain so it stays locked 1952502c107SJeff Roberson * for the whole operation. The caller expects us to return with 1962502c107SJeff Roberson * the original lock held. We only ever lock down the chain so 1972502c107SJeff Roberson * the lock order is constant. 1982502c107SJeff Roberson */ 1992502c107SJeff Roberson mtx_lock_spin(&ts->ts_lock); 20036412d79SJohn Baldwin for (;;) { 201961a7b24SJohn Baldwin td = ts->ts_owner; 20236412d79SJohn Baldwin 203b40ce416SJulian Elischer if (td == NULL) { 20436412d79SJohn Baldwin /* 2057aa4f685SJohn Baldwin * This might be a read lock with no owner. There's 2067aa4f685SJohn Baldwin * not much we can do, so just bail. 20736412d79SJohn Baldwin */ 2082502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 20936412d79SJohn Baldwin return; 21036412d79SJohn Baldwin } 2119ed346baSBosko Milekic 2122502c107SJeff Roberson thread_lock_flags(td, MTX_DUPOK); 2132502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 214e602ba25SJulian Elischer MPASS(td->td_proc != NULL); 215b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 2161bd0eefbSJohn Baldwin 217961a7b24SJohn Baldwin /* 2184b3b0413SJohn Baldwin * If the thread is asleep, then we are probably about 219b2e054b0SPawel Jakub Dawidek * to deadlock. To make debugging this easier, show 220b2e054b0SPawel Jakub Dawidek * backtrace of misbehaving thread and panic to not 221b2e054b0SPawel Jakub Dawidek * leave the kernel deadlocked. 222961a7b24SJohn Baldwin */ 2234b3b0413SJohn Baldwin if (TD_IS_SLEEPING(td)) { 2244b3b0413SJohn Baldwin printf( 2254b3b0413SJohn Baldwin "Sleeping thread (tid %d, pid %d) owns a non-sleepable lock\n", 2264b3b0413SJohn Baldwin td->td_tid, td->td_proc->p_pid); 2270cc457b0SJohn Baldwin kdb_backtrace_thread(td); 2284b3b0413SJohn Baldwin panic("sleeping thread"); 2294b3b0413SJohn Baldwin } 230961a7b24SJohn Baldwin 231961a7b24SJohn Baldwin /* 232961a7b24SJohn Baldwin * If this thread already has higher priority than the 233961a7b24SJohn Baldwin * thread that is being blocked, we are finished. 234961a7b24SJohn Baldwin */ 2352502c107SJeff Roberson if (td->td_priority <= pri) { 2362502c107SJeff Roberson thread_unlock(td); 237961a7b24SJohn Baldwin return; 2382502c107SJeff Roberson } 2391bd0eefbSJohn Baldwin 24036412d79SJohn Baldwin /* 241f5c157d9SJohn Baldwin * Bump this thread's priority. 24236412d79SJohn Baldwin */ 243f5c157d9SJohn Baldwin sched_lend_prio(td, pri); 244f5c157d9SJohn Baldwin 245f5c157d9SJohn Baldwin /* 246f5c157d9SJohn Baldwin * If lock holder is actually running or on the run queue 247f5c157d9SJohn Baldwin * then we are done. 248f5c157d9SJohn Baldwin */ 249f5c157d9SJohn Baldwin if (TD_IS_RUNNING(td) || TD_ON_RUNQ(td)) { 250f5c157d9SJohn Baldwin MPASS(td->td_blocked == NULL); 2512502c107SJeff Roberson thread_unlock(td); 25236412d79SJohn Baldwin return; 25336412d79SJohn Baldwin } 254d5a08a60SJake Burkholder 2551b43703bSJohn Baldwin #ifndef SMP 2561b43703bSJohn Baldwin /* 257b40ce416SJulian Elischer * For UP, we check to see if td is curthread (this shouldn't 2581b43703bSJohn Baldwin * ever happen however as it would mean we are in a deadlock.) 2591b43703bSJohn Baldwin */ 260b40ce416SJulian Elischer KASSERT(td != curthread, ("Deadlock detected")); 2611b43703bSJohn Baldwin #endif 2621b43703bSJohn Baldwin 26336412d79SJohn Baldwin /* 264961a7b24SJohn Baldwin * If we aren't blocked on a lock, we should be. 26536412d79SJohn Baldwin */ 266551cf4e1SJohn Baldwin KASSERT(TD_ON_LOCK(td), ( 267f5c157d9SJohn Baldwin "thread %d(%s):%d holds %s but isn't blocked on a lock\n", 268431f8906SJulian Elischer td->td_tid, td->td_name, td->td_state, 269961a7b24SJohn Baldwin ts->ts_lockobj->lo_name)); 27036412d79SJohn Baldwin 27136412d79SJohn Baldwin /* 272961a7b24SJohn Baldwin * Pick up the lock that td is blocked on. 27336412d79SJohn Baldwin */ 274961a7b24SJohn Baldwin ts = td->td_blocked; 275961a7b24SJohn Baldwin MPASS(ts != NULL); 276626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 277f5c157d9SJohn Baldwin /* Resort td on the list if needed. */ 278f5c157d9SJohn Baldwin if (!turnstile_adjust_thread(ts, td)) { 2792502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 280f5c157d9SJohn Baldwin return; 281f5c157d9SJohn Baldwin } 2822502c107SJeff Roberson /* The thread lock is released as ts lock above. */ 283f5c157d9SJohn Baldwin } 284f5c157d9SJohn Baldwin } 285f5c157d9SJohn Baldwin 286f5c157d9SJohn Baldwin /* 287f5c157d9SJohn Baldwin * Adjust the thread's position on a turnstile after its priority has been 288f5c157d9SJohn Baldwin * changed. 289f5c157d9SJohn Baldwin */ 290f5c157d9SJohn Baldwin static int 291f5c157d9SJohn Baldwin turnstile_adjust_thread(struct turnstile *ts, struct thread *td) 292f5c157d9SJohn Baldwin { 293f5c157d9SJohn Baldwin struct thread *td1, *td2; 2947aa4f685SJohn Baldwin int queue; 295f5c157d9SJohn Baldwin 2962502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 297f5c157d9SJohn Baldwin MPASS(TD_ON_LOCK(td)); 298f5c157d9SJohn Baldwin 29936412d79SJohn Baldwin /* 3006b6bd95eSJohn Baldwin * This thread may not be blocked on this turnstile anymore 3016b6bd95eSJohn Baldwin * but instead might already be woken up on another CPU 3022502c107SJeff Roberson * that is waiting on the thread lock in turnstile_unpend() to 3036b6bd95eSJohn Baldwin * finish waking this thread up. We can detect this case 3046b6bd95eSJohn Baldwin * by checking to see if this thread has been given a 3056b6bd95eSJohn Baldwin * turnstile by either turnstile_signal() or 306ef2c0ba7SJohn Baldwin * turnstile_broadcast(). In this case, treat the thread as 3076b6bd95eSJohn Baldwin * if it was already running. 30879a13d01SJohn Baldwin */ 309f5c157d9SJohn Baldwin if (td->td_turnstile != NULL) 310f5c157d9SJohn Baldwin return (0); 31179a13d01SJohn Baldwin 31279a13d01SJohn Baldwin /* 313f5c157d9SJohn Baldwin * Check if the thread needs to be moved on the blocked chain. 314f5c157d9SJohn Baldwin * It needs to be moved if either its priority is lower than 315f5c157d9SJohn Baldwin * the previous thread or higher than the next thread. 31636412d79SJohn Baldwin */ 317626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 318551cf4e1SJohn Baldwin td1 = TAILQ_PREV(td, threadqueue, td_lockq); 319f5c157d9SJohn Baldwin td2 = TAILQ_NEXT(td, td_lockq); 320f5c157d9SJohn Baldwin if ((td1 != NULL && td->td_priority < td1->td_priority) || 321f5c157d9SJohn Baldwin (td2 != NULL && td->td_priority > td2->td_priority)) { 32236412d79SJohn Baldwin 32336412d79SJohn Baldwin /* 324b40ce416SJulian Elischer * Remove thread from blocked chain and determine where 325f5c157d9SJohn Baldwin * it should be moved to. 32636412d79SJohn Baldwin */ 3277aa4f685SJohn Baldwin queue = td->td_tsqueue; 3287aa4f685SJohn Baldwin MPASS(queue == TS_EXCLUSIVE_QUEUE || queue == TS_SHARED_QUEUE); 329961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 3307aa4f685SJohn Baldwin TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq); 3317aa4f685SJohn Baldwin TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) { 332b40ce416SJulian Elischer MPASS(td1->td_proc->p_magic == P_MAGIC); 333f5c157d9SJohn Baldwin if (td1->td_priority > td->td_priority) 33436412d79SJohn Baldwin break; 33536412d79SJohn Baldwin } 3369ed346baSBosko Milekic 337f5c157d9SJohn Baldwin if (td1 == NULL) 3387aa4f685SJohn Baldwin TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq); 339f5c157d9SJohn Baldwin else 340551cf4e1SJohn Baldwin TAILQ_INSERT_BEFORE(td1, td, td_lockq); 341961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 342f5c157d9SJohn Baldwin if (td1 == NULL) 343f5c157d9SJohn Baldwin CTR3(KTR_LOCK, 344f5c157d9SJohn Baldwin "turnstile_adjust_thread: td %d put at tail on [%p] %s", 345f5c157d9SJohn Baldwin td->td_tid, ts->ts_lockobj, ts->ts_lockobj->lo_name); 346f5c157d9SJohn Baldwin else 34736412d79SJohn Baldwin CTR4(KTR_LOCK, 348f5c157d9SJohn Baldwin "turnstile_adjust_thread: td %d moved before %d on [%p] %s", 349f5c157d9SJohn Baldwin td->td_tid, td1->td_tid, ts->ts_lockobj, 350f5c157d9SJohn Baldwin ts->ts_lockobj->lo_name); 35136412d79SJohn Baldwin } 352f5c157d9SJohn Baldwin return (1); 35336412d79SJohn Baldwin } 35436412d79SJohn Baldwin 3556c35e809SDag-Erling Smørgrav /* 356961a7b24SJohn Baldwin * Early initialization of turnstiles. This is not done via a SYSINIT() 357961a7b24SJohn Baldwin * since this needs to be initialized very early when mutexes are first 358961a7b24SJohn Baldwin * initialized. 3596283b7d0SJohn Baldwin */ 3606283b7d0SJohn Baldwin void 361961a7b24SJohn Baldwin init_turnstiles(void) 3626283b7d0SJohn Baldwin { 363961a7b24SJohn Baldwin int i; 3646283b7d0SJohn Baldwin 365961a7b24SJohn Baldwin for (i = 0; i < TC_TABLESIZE; i++) { 366961a7b24SJohn Baldwin LIST_INIT(&turnstile_chains[i].tc_turnstiles); 367961a7b24SJohn Baldwin mtx_init(&turnstile_chains[i].tc_lock, "turnstile chain", 368961a7b24SJohn Baldwin NULL, MTX_SPIN); 36901bd10e1SJohn Baldwin } 37001bd10e1SJohn Baldwin mtx_init(&td_contested_lock, "td_contested", NULL, MTX_SPIN); 371550d1c93SJohn Baldwin LIST_INIT(&thread0.td_contested); 37201bd10e1SJohn Baldwin thread0.td_turnstile = NULL; 37301bd10e1SJohn Baldwin } 37401bd10e1SJohn Baldwin 375ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 37601bd10e1SJohn Baldwin static void 37701bd10e1SJohn Baldwin init_turnstile_profiling(void *arg) 37801bd10e1SJohn Baldwin { 37901bd10e1SJohn Baldwin struct sysctl_oid *chain_oid; 38001bd10e1SJohn Baldwin char chain_name[10]; 38101bd10e1SJohn Baldwin int i; 38201bd10e1SJohn Baldwin 38301bd10e1SJohn Baldwin for (i = 0; i < TC_TABLESIZE; i++) { 384ef0ebfc3SJohn Baldwin snprintf(chain_name, sizeof(chain_name), "%d", i); 385ef0ebfc3SJohn Baldwin chain_oid = SYSCTL_ADD_NODE(NULL, 386ef0ebfc3SJohn Baldwin SYSCTL_STATIC_CHILDREN(_debug_turnstile_chains), OID_AUTO, 387ef0ebfc3SJohn Baldwin chain_name, CTLFLAG_RD, NULL, "turnstile chain stats"); 388ef0ebfc3SJohn Baldwin SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO, 389ef0ebfc3SJohn Baldwin "depth", CTLFLAG_RD, &turnstile_chains[i].tc_depth, 0, 390ef0ebfc3SJohn Baldwin NULL); 391ef0ebfc3SJohn Baldwin SYSCTL_ADD_UINT(NULL, SYSCTL_CHILDREN(chain_oid), OID_AUTO, 392ef0ebfc3SJohn Baldwin "max_depth", CTLFLAG_RD, &turnstile_chains[i].tc_max_depth, 393ef0ebfc3SJohn Baldwin 0, NULL); 39401bd10e1SJohn Baldwin } 39501bd10e1SJohn Baldwin } 39601bd10e1SJohn Baldwin SYSINIT(turnstile_profiling, SI_SUB_LOCK, SI_ORDER_ANY, 39701bd10e1SJohn Baldwin init_turnstile_profiling, NULL); 398ef0ebfc3SJohn Baldwin #endif 3996283b7d0SJohn Baldwin 400961a7b24SJohn Baldwin static void 401961a7b24SJohn Baldwin init_turnstile0(void *dummy) 4026283b7d0SJohn Baldwin { 4036283b7d0SJohn Baldwin 4042b7e2ee7SJeff Roberson turnstile_zone = uma_zcreate("TURNSTILE", sizeof(struct turnstile), 4058c68f75aSJohn Baldwin NULL, 4062b7e2ee7SJeff Roberson #ifdef INVARIANTS 4078c68f75aSJohn Baldwin turnstile_dtor, 4082b7e2ee7SJeff Roberson #else 4098c68f75aSJohn Baldwin NULL, 4102b7e2ee7SJeff Roberson #endif 4118c68f75aSJohn Baldwin turnstile_init, turnstile_fini, UMA_ALIGN_CACHE, UMA_ZONE_NOFREE); 412961a7b24SJohn Baldwin thread0.td_turnstile = turnstile_alloc(); 413961a7b24SJohn Baldwin } 414961a7b24SJohn Baldwin SYSINIT(turnstile0, SI_SUB_LOCK, SI_ORDER_ANY, init_turnstile0, NULL); 4156c35e809SDag-Erling Smørgrav 416961a7b24SJohn Baldwin /* 417f5c157d9SJohn Baldwin * Update a thread on the turnstile list after it's priority has been changed. 418f5c157d9SJohn Baldwin * The old priority is passed in as an argument. 419f5c157d9SJohn Baldwin */ 420f5c157d9SJohn Baldwin void 421f5c157d9SJohn Baldwin turnstile_adjust(struct thread *td, u_char oldpri) 422f5c157d9SJohn Baldwin { 423f5c157d9SJohn Baldwin struct turnstile *ts; 424f5c157d9SJohn Baldwin 425f5c157d9SJohn Baldwin MPASS(TD_ON_LOCK(td)); 426f5c157d9SJohn Baldwin 427f5c157d9SJohn Baldwin /* 428f5c157d9SJohn Baldwin * Pick up the lock that td is blocked on. 429f5c157d9SJohn Baldwin */ 430f5c157d9SJohn Baldwin ts = td->td_blocked; 431f5c157d9SJohn Baldwin MPASS(ts != NULL); 432626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 4332502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 434f5c157d9SJohn Baldwin 435f5c157d9SJohn Baldwin /* Resort the turnstile on the list. */ 4362502c107SJeff Roberson if (!turnstile_adjust_thread(ts, td)) 437f5c157d9SJohn Baldwin return; 438f5c157d9SJohn Baldwin /* 439f5c157d9SJohn Baldwin * If our priority was lowered and we are at the head of the 440f5c157d9SJohn Baldwin * turnstile, then propagate our new priority up the chain. 441f5c157d9SJohn Baldwin * Note that we currently don't try to revoke lent priorities 442f5c157d9SJohn Baldwin * when our priority goes up. 443f5c157d9SJohn Baldwin */ 4447aa4f685SJohn Baldwin MPASS(td->td_tsqueue == TS_EXCLUSIVE_QUEUE || 4457aa4f685SJohn Baldwin td->td_tsqueue == TS_SHARED_QUEUE); 4467aa4f685SJohn Baldwin if (td == TAILQ_FIRST(&ts->ts_blocked[td->td_tsqueue]) && 4477aa4f685SJohn Baldwin td->td_priority < oldpri) { 448f5c157d9SJohn Baldwin propagate_priority(td); 4492502c107SJeff Roberson } 450f5c157d9SJohn Baldwin } 451f5c157d9SJohn Baldwin 452f5c157d9SJohn Baldwin /* 453961a7b24SJohn Baldwin * Set the owner of the lock this turnstile is attached to. 454961a7b24SJohn Baldwin */ 455961a7b24SJohn Baldwin static void 456961a7b24SJohn Baldwin turnstile_setowner(struct turnstile *ts, struct thread *owner) 457961a7b24SJohn Baldwin { 458961a7b24SJohn Baldwin 459961a7b24SJohn Baldwin mtx_assert(&td_contested_lock, MA_OWNED); 460961a7b24SJohn Baldwin MPASS(ts->ts_owner == NULL); 4617aa4f685SJohn Baldwin 4627aa4f685SJohn Baldwin /* A shared lock might not have an owner. */ 4637aa4f685SJohn Baldwin if (owner == NULL) 4647aa4f685SJohn Baldwin return; 4657aa4f685SJohn Baldwin 4667aa4f685SJohn Baldwin MPASS(owner->td_proc->p_magic == P_MAGIC); 467961a7b24SJohn Baldwin ts->ts_owner = owner; 468961a7b24SJohn Baldwin LIST_INSERT_HEAD(&owner->td_contested, ts, ts_link); 469961a7b24SJohn Baldwin } 470961a7b24SJohn Baldwin 4712b7e2ee7SJeff Roberson #ifdef INVARIANTS 472961a7b24SJohn Baldwin /* 4732b7e2ee7SJeff Roberson * UMA zone item deallocator. 474961a7b24SJohn Baldwin */ 4752b7e2ee7SJeff Roberson static void 4762b7e2ee7SJeff Roberson turnstile_dtor(void *mem, int size, void *arg) 477961a7b24SJohn Baldwin { 478961a7b24SJohn Baldwin struct turnstile *ts; 479961a7b24SJohn Baldwin 4802b7e2ee7SJeff Roberson ts = mem; 4812b7e2ee7SJeff Roberson MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE])); 4822b7e2ee7SJeff Roberson MPASS(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE])); 4832b7e2ee7SJeff Roberson MPASS(TAILQ_EMPTY(&ts->ts_pending)); 4842b7e2ee7SJeff Roberson } 4852b7e2ee7SJeff Roberson #endif 4862b7e2ee7SJeff Roberson 4872b7e2ee7SJeff Roberson /* 4882b7e2ee7SJeff Roberson * UMA zone item initializer. 4892b7e2ee7SJeff Roberson */ 4902b7e2ee7SJeff Roberson static int 4912b7e2ee7SJeff Roberson turnstile_init(void *mem, int size, int flags) 4922b7e2ee7SJeff Roberson { 4932b7e2ee7SJeff Roberson struct turnstile *ts; 4942b7e2ee7SJeff Roberson 4952b7e2ee7SJeff Roberson bzero(mem, size); 4962b7e2ee7SJeff Roberson ts = mem; 4977aa4f685SJohn Baldwin TAILQ_INIT(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]); 4987aa4f685SJohn Baldwin TAILQ_INIT(&ts->ts_blocked[TS_SHARED_QUEUE]); 499961a7b24SJohn Baldwin TAILQ_INIT(&ts->ts_pending); 500961a7b24SJohn Baldwin LIST_INIT(&ts->ts_free); 5012502c107SJeff Roberson mtx_init(&ts->ts_lock, "turnstile lock", NULL, MTX_SPIN | MTX_RECURSE); 5022b7e2ee7SJeff Roberson return (0); 5032b7e2ee7SJeff Roberson } 5042b7e2ee7SJeff Roberson 5052502c107SJeff Roberson static void 5062502c107SJeff Roberson turnstile_fini(void *mem, int size) 5072502c107SJeff Roberson { 5082502c107SJeff Roberson struct turnstile *ts; 5092502c107SJeff Roberson 5102502c107SJeff Roberson ts = mem; 5112502c107SJeff Roberson mtx_destroy(&ts->ts_lock); 5122502c107SJeff Roberson } 5132502c107SJeff Roberson 5142b7e2ee7SJeff Roberson /* 5152b7e2ee7SJeff Roberson * Get a turnstile for a new thread. 5162b7e2ee7SJeff Roberson */ 5172b7e2ee7SJeff Roberson struct turnstile * 5182b7e2ee7SJeff Roberson turnstile_alloc(void) 5192b7e2ee7SJeff Roberson { 5202b7e2ee7SJeff Roberson 5212b7e2ee7SJeff Roberson return (uma_zalloc(turnstile_zone, M_WAITOK)); 522961a7b24SJohn Baldwin } 523961a7b24SJohn Baldwin 524961a7b24SJohn Baldwin /* 525961a7b24SJohn Baldwin * Free a turnstile when a thread is destroyed. 526961a7b24SJohn Baldwin */ 527961a7b24SJohn Baldwin void 528961a7b24SJohn Baldwin turnstile_free(struct turnstile *ts) 529961a7b24SJohn Baldwin { 530961a7b24SJohn Baldwin 5312b7e2ee7SJeff Roberson uma_zfree(turnstile_zone, ts); 532961a7b24SJohn Baldwin } 533961a7b24SJohn Baldwin 534961a7b24SJohn Baldwin /* 5352ff0e645SJohn Baldwin * Lock the turnstile chain associated with the specified lock. 5362ff0e645SJohn Baldwin */ 5372ff0e645SJohn Baldwin void 5382502c107SJeff Roberson turnstile_chain_lock(struct lock_object *lock) 5392ff0e645SJohn Baldwin { 5402ff0e645SJohn Baldwin struct turnstile_chain *tc; 5412ff0e645SJohn Baldwin 5422ff0e645SJohn Baldwin tc = TC_LOOKUP(lock); 5432ff0e645SJohn Baldwin mtx_lock_spin(&tc->tc_lock); 5442ff0e645SJohn Baldwin } 5452ff0e645SJohn Baldwin 5462502c107SJeff Roberson struct turnstile * 5472502c107SJeff Roberson turnstile_trywait(struct lock_object *lock) 5482502c107SJeff Roberson { 5492502c107SJeff Roberson struct turnstile_chain *tc; 5502502c107SJeff Roberson struct turnstile *ts; 5512502c107SJeff Roberson 5522502c107SJeff Roberson tc = TC_LOOKUP(lock); 5532502c107SJeff Roberson mtx_lock_spin(&tc->tc_lock); 5542502c107SJeff Roberson LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash) 5552502c107SJeff Roberson if (ts->ts_lockobj == lock) { 5562502c107SJeff Roberson mtx_lock_spin(&ts->ts_lock); 5572502c107SJeff Roberson return (ts); 5582502c107SJeff Roberson } 5592502c107SJeff Roberson 5602502c107SJeff Roberson ts = curthread->td_turnstile; 5612502c107SJeff Roberson MPASS(ts != NULL); 5622502c107SJeff Roberson mtx_lock_spin(&ts->ts_lock); 5632502c107SJeff Roberson KASSERT(ts->ts_lockobj == NULL, ("stale ts_lockobj pointer")); 5642502c107SJeff Roberson ts->ts_lockobj = lock; 5652502c107SJeff Roberson 5662502c107SJeff Roberson return (ts); 5672502c107SJeff Roberson } 5682502c107SJeff Roberson 56906bf2a6aSMatt Macy struct thread * 57006bf2a6aSMatt Macy turnstile_lock(struct turnstile *ts, struct lock_object **lockp) 57106bf2a6aSMatt Macy { 57206bf2a6aSMatt Macy struct turnstile_chain *tc; 57306bf2a6aSMatt Macy struct lock_object *lock; 57406bf2a6aSMatt Macy 57506bf2a6aSMatt Macy if ((lock = ts->ts_lockobj) == NULL) 57606bf2a6aSMatt Macy return (NULL); 57706bf2a6aSMatt Macy tc = TC_LOOKUP(lock); 57806bf2a6aSMatt Macy mtx_lock_spin(&tc->tc_lock); 57906bf2a6aSMatt Macy mtx_lock_spin(&ts->ts_lock); 58006bf2a6aSMatt Macy if (__predict_false(lock != ts->ts_lockobj)) { 58106bf2a6aSMatt Macy mtx_unlock_spin(&tc->tc_lock); 58206bf2a6aSMatt Macy mtx_unlock_spin(&ts->ts_lock); 58306bf2a6aSMatt Macy return (NULL); 58406bf2a6aSMatt Macy } 58506bf2a6aSMatt Macy *lockp = lock; 58606bf2a6aSMatt Macy return (ts->ts_owner); 58706bf2a6aSMatt Macy } 58806bf2a6aSMatt Macy 58906bf2a6aSMatt Macy void 59006bf2a6aSMatt Macy turnstile_unlock(struct turnstile *ts, struct lock_object *lock) 59106bf2a6aSMatt Macy { 59206bf2a6aSMatt Macy struct turnstile_chain *tc; 59306bf2a6aSMatt Macy 59406bf2a6aSMatt Macy mtx_assert(&ts->ts_lock, MA_OWNED); 59506bf2a6aSMatt Macy mtx_unlock_spin(&ts->ts_lock); 59606bf2a6aSMatt Macy if (ts == curthread->td_turnstile) 59706bf2a6aSMatt Macy ts->ts_lockobj = NULL; 59806bf2a6aSMatt Macy tc = TC_LOOKUP(lock); 59906bf2a6aSMatt Macy mtx_unlock_spin(&tc->tc_lock); 60006bf2a6aSMatt Macy } 60106bf2a6aSMatt Macy 60206bf2a6aSMatt Macy void 60306bf2a6aSMatt Macy turnstile_assert(struct turnstile *ts) 60406bf2a6aSMatt Macy { 60506bf2a6aSMatt Macy MPASS(ts->ts_lockobj == NULL); 60606bf2a6aSMatt Macy } 60706bf2a6aSMatt Macy 6082502c107SJeff Roberson void 6092502c107SJeff Roberson turnstile_cancel(struct turnstile *ts) 6102502c107SJeff Roberson { 6112502c107SJeff Roberson struct turnstile_chain *tc; 6122502c107SJeff Roberson struct lock_object *lock; 6132502c107SJeff Roberson 6142502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 6152502c107SJeff Roberson 6162502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 6172502c107SJeff Roberson lock = ts->ts_lockobj; 6182502c107SJeff Roberson if (ts == curthread->td_turnstile) 6192502c107SJeff Roberson ts->ts_lockobj = NULL; 6202502c107SJeff Roberson tc = TC_LOOKUP(lock); 6212502c107SJeff Roberson mtx_unlock_spin(&tc->tc_lock); 6222502c107SJeff Roberson } 6232502c107SJeff Roberson 6242ff0e645SJohn Baldwin /* 625961a7b24SJohn Baldwin * Look up the turnstile for a lock in the hash table locking the associated 6262ff0e645SJohn Baldwin * turnstile chain along the way. If no turnstile is found in the hash 6272ff0e645SJohn Baldwin * table, NULL is returned. 628961a7b24SJohn Baldwin */ 629961a7b24SJohn Baldwin struct turnstile * 630961a7b24SJohn Baldwin turnstile_lookup(struct lock_object *lock) 631961a7b24SJohn Baldwin { 632961a7b24SJohn Baldwin struct turnstile_chain *tc; 633961a7b24SJohn Baldwin struct turnstile *ts; 634961a7b24SJohn Baldwin 635961a7b24SJohn Baldwin tc = TC_LOOKUP(lock); 6362ff0e645SJohn Baldwin mtx_assert(&tc->tc_lock, MA_OWNED); 637961a7b24SJohn Baldwin LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash) 6382502c107SJeff Roberson if (ts->ts_lockobj == lock) { 6392502c107SJeff Roberson mtx_lock_spin(&ts->ts_lock); 640961a7b24SJohn Baldwin return (ts); 6412502c107SJeff Roberson } 642961a7b24SJohn Baldwin return (NULL); 643961a7b24SJohn Baldwin } 644961a7b24SJohn Baldwin 645961a7b24SJohn Baldwin /* 646961a7b24SJohn Baldwin * Unlock the turnstile chain associated with a given lock. 647961a7b24SJohn Baldwin */ 648961a7b24SJohn Baldwin void 6492502c107SJeff Roberson turnstile_chain_unlock(struct lock_object *lock) 650961a7b24SJohn Baldwin { 651961a7b24SJohn Baldwin struct turnstile_chain *tc; 652961a7b24SJohn Baldwin 653961a7b24SJohn Baldwin tc = TC_LOOKUP(lock); 654961a7b24SJohn Baldwin mtx_unlock_spin(&tc->tc_lock); 655961a7b24SJohn Baldwin } 656961a7b24SJohn Baldwin 657961a7b24SJohn Baldwin /* 6587aa4f685SJohn Baldwin * Return a pointer to the thread waiting on this turnstile with the 6597aa4f685SJohn Baldwin * most important priority or NULL if the turnstile has no waiters. 6607aa4f685SJohn Baldwin */ 6617aa4f685SJohn Baldwin static struct thread * 6627aa4f685SJohn Baldwin turnstile_first_waiter(struct turnstile *ts) 6637aa4f685SJohn Baldwin { 6647aa4f685SJohn Baldwin struct thread *std, *xtd; 6657aa4f685SJohn Baldwin 6667aa4f685SJohn Baldwin std = TAILQ_FIRST(&ts->ts_blocked[TS_SHARED_QUEUE]); 6677aa4f685SJohn Baldwin xtd = TAILQ_FIRST(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]); 6687aa4f685SJohn Baldwin if (xtd == NULL || (std != NULL && std->td_priority < xtd->td_priority)) 6697aa4f685SJohn Baldwin return (std); 6707aa4f685SJohn Baldwin return (xtd); 6717aa4f685SJohn Baldwin } 6727aa4f685SJohn Baldwin 6737aa4f685SJohn Baldwin /* 674961a7b24SJohn Baldwin * Take ownership of a turnstile and adjust the priority of the new 675961a7b24SJohn Baldwin * owner appropriately. 676961a7b24SJohn Baldwin */ 677961a7b24SJohn Baldwin void 6782502c107SJeff Roberson turnstile_claim(struct turnstile *ts) 679961a7b24SJohn Baldwin { 680961a7b24SJohn Baldwin struct thread *td, *owner; 6812502c107SJeff Roberson struct turnstile_chain *tc; 682961a7b24SJohn Baldwin 6832502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 6842502c107SJeff Roberson MPASS(ts != curthread->td_turnstile); 685961a7b24SJohn Baldwin 686961a7b24SJohn Baldwin owner = curthread; 687961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 688961a7b24SJohn Baldwin turnstile_setowner(ts, owner); 689961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 690961a7b24SJohn Baldwin 6917aa4f685SJohn Baldwin td = turnstile_first_waiter(ts); 692961a7b24SJohn Baldwin MPASS(td != NULL); 693961a7b24SJohn Baldwin MPASS(td->td_proc->p_magic == P_MAGIC); 694626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 695961a7b24SJohn Baldwin 696961a7b24SJohn Baldwin /* 697961a7b24SJohn Baldwin * Update the priority of the new owner if needed. 698961a7b24SJohn Baldwin */ 6992502c107SJeff Roberson thread_lock(owner); 700961a7b24SJohn Baldwin if (td->td_priority < owner->td_priority) 701f5c157d9SJohn Baldwin sched_lend_prio(owner, td->td_priority); 7022502c107SJeff Roberson thread_unlock(owner); 7032502c107SJeff Roberson tc = TC_LOOKUP(ts->ts_lockobj); 7042502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 7052502c107SJeff Roberson mtx_unlock_spin(&tc->tc_lock); 706961a7b24SJohn Baldwin } 707961a7b24SJohn Baldwin 708961a7b24SJohn Baldwin /* 7092ff0e645SJohn Baldwin * Block the current thread on the turnstile assicated with 'lock'. This 7102ff0e645SJohn Baldwin * function will context switch and not return until this thread has been 7112ff0e645SJohn Baldwin * woken back up. This function must be called with the appropriate 7122ff0e645SJohn Baldwin * turnstile chain locked and will return with it unlocked. 713961a7b24SJohn Baldwin */ 714961a7b24SJohn Baldwin void 7152502c107SJeff Roberson turnstile_wait(struct turnstile *ts, struct thread *owner, int queue) 716961a7b24SJohn Baldwin { 717961a7b24SJohn Baldwin struct turnstile_chain *tc; 718961a7b24SJohn Baldwin struct thread *td, *td1; 7192502c107SJeff Roberson struct lock_object *lock; 720961a7b24SJohn Baldwin 721961a7b24SJohn Baldwin td = curthread; 7222502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 7237aa4f685SJohn Baldwin if (owner) 724961a7b24SJohn Baldwin MPASS(owner->td_proc->p_magic == P_MAGIC); 7257aa4f685SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 726961a7b24SJohn Baldwin 7272ff0e645SJohn Baldwin /* 7282ff0e645SJohn Baldwin * If the lock does not already have a turnstile, use this thread's 7292ff0e645SJohn Baldwin * turnstile. Otherwise insert the current thread into the 7302ff0e645SJohn Baldwin * turnstile already in use by this lock. 7312ff0e645SJohn Baldwin */ 7322502c107SJeff Roberson tc = TC_LOOKUP(ts->ts_lockobj); 7332502c107SJeff Roberson mtx_assert(&tc->tc_lock, MA_OWNED); 734f7488600SJohn Baldwin if (ts == td->td_turnstile) { 735ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 736ef0ebfc3SJohn Baldwin tc->tc_depth++; 737ef0ebfc3SJohn Baldwin if (tc->tc_depth > tc->tc_max_depth) { 738ef0ebfc3SJohn Baldwin tc->tc_max_depth = tc->tc_depth; 739ef0ebfc3SJohn Baldwin if (tc->tc_max_depth > turnstile_max_depth) 740ef0ebfc3SJohn Baldwin turnstile_max_depth = tc->tc_max_depth; 741ef0ebfc3SJohn Baldwin } 742ef0ebfc3SJohn Baldwin #endif 743961a7b24SJohn Baldwin LIST_INSERT_HEAD(&tc->tc_turnstiles, ts, ts_hash); 744961a7b24SJohn Baldwin KASSERT(TAILQ_EMPTY(&ts->ts_pending), 745961a7b24SJohn Baldwin ("thread's turnstile has pending threads")); 7467aa4f685SJohn Baldwin KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]), 7477aa4f685SJohn Baldwin ("thread's turnstile has exclusive waiters")); 7487aa4f685SJohn Baldwin KASSERT(TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]), 7497aa4f685SJohn Baldwin ("thread's turnstile has shared waiters")); 750961a7b24SJohn Baldwin KASSERT(LIST_EMPTY(&ts->ts_free), 751961a7b24SJohn Baldwin ("thread's turnstile has a non-empty free list")); 7522502c107SJeff Roberson MPASS(ts->ts_lockobj != NULL); 753961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 7547aa4f685SJohn Baldwin TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq); 755961a7b24SJohn Baldwin turnstile_setowner(ts, owner); 756961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 757961a7b24SJohn Baldwin } else { 7587aa4f685SJohn Baldwin TAILQ_FOREACH(td1, &ts->ts_blocked[queue], td_lockq) 759961a7b24SJohn Baldwin if (td1->td_priority > td->td_priority) 7606c35e809SDag-Erling Smørgrav break; 761961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 762961a7b24SJohn Baldwin if (td1 != NULL) 763961a7b24SJohn Baldwin TAILQ_INSERT_BEFORE(td1, td, td_lockq); 764961a7b24SJohn Baldwin else 7657aa4f685SJohn Baldwin TAILQ_INSERT_TAIL(&ts->ts_blocked[queue], td, td_lockq); 7667aa4f685SJohn Baldwin MPASS(owner == ts->ts_owner); 767961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 768961a7b24SJohn Baldwin MPASS(td->td_turnstile != NULL); 769961a7b24SJohn Baldwin LIST_INSERT_HEAD(&ts->ts_free, td->td_turnstile, ts_hash); 7706c35e809SDag-Erling Smørgrav } 7712502c107SJeff Roberson thread_lock(td); 7722502c107SJeff Roberson thread_lock_set(td, &ts->ts_lock); 773961a7b24SJohn Baldwin td->td_turnstile = NULL; 77436412d79SJohn Baldwin 775961a7b24SJohn Baldwin /* Save who we are blocked on and switch. */ 7762502c107SJeff Roberson lock = ts->ts_lockobj; 7777aa4f685SJohn Baldwin td->td_tsqueue = queue; 778961a7b24SJohn Baldwin td->td_blocked = ts; 779961a7b24SJohn Baldwin td->td_lockname = lock->lo_name; 780f7829d0dSAttilio Rao td->td_blktick = ticks; 781551cf4e1SJohn Baldwin TD_SET_LOCK(td); 7822502c107SJeff Roberson mtx_unlock_spin(&tc->tc_lock); 783b40ce416SJulian Elischer propagate_priority(td); 7849ed346baSBosko Milekic 785961a7b24SJohn Baldwin if (LOCK_LOG_TEST(lock, 0)) 786f5c157d9SJohn Baldwin CTR4(KTR_LOCK, "%s: td %d blocked on [%p] %s", __func__, 787f5c157d9SJohn Baldwin td->td_tid, lock, lock->lo_name); 7889ed346baSBosko Milekic 789b3e9e682SRyan Stone SDT_PROBE0(sched, , , sleep); 790b3e9e682SRyan Stone 791626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 7928df78c41SJeff Roberson mi_switch(SW_VOL | SWT_TURNSTILE, NULL); 7939ed346baSBosko Milekic 794961a7b24SJohn Baldwin if (LOCK_LOG_TEST(lock, 0)) 795f5c157d9SJohn Baldwin CTR4(KTR_LOCK, "%s: td %d free from blocked on [%p] %s", 796f5c157d9SJohn Baldwin __func__, td->td_tid, lock, lock->lo_name); 7972502c107SJeff Roberson thread_unlock(td); 79836412d79SJohn Baldwin } 7999ed346baSBosko Milekic 800961a7b24SJohn Baldwin /* 801961a7b24SJohn Baldwin * Pick the highest priority thread on this turnstile and put it on the 802961a7b24SJohn Baldwin * pending list. This must be called with the turnstile chain locked. 803961a7b24SJohn Baldwin */ 804961a7b24SJohn Baldwin int 8057aa4f685SJohn Baldwin turnstile_signal(struct turnstile *ts, int queue) 806961a7b24SJohn Baldwin { 8073adccf38SMatt Macy struct turnstile_chain *tc __unused; 808961a7b24SJohn Baldwin struct thread *td; 809961a7b24SJohn Baldwin int empty; 810961a7b24SJohn Baldwin 811961a7b24SJohn Baldwin MPASS(ts != NULL); 8122502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 813961a7b24SJohn Baldwin MPASS(curthread->td_proc->p_magic == P_MAGIC); 8145dff04c3SJeff Roberson MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL); 8157aa4f685SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 8169ed346baSBosko Milekic 8179ed346baSBosko Milekic /* 818961a7b24SJohn Baldwin * Pick the highest priority thread blocked on this lock and 819961a7b24SJohn Baldwin * move it to the pending list. 8209ed346baSBosko Milekic */ 8217aa4f685SJohn Baldwin td = TAILQ_FIRST(&ts->ts_blocked[queue]); 822b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 823961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 8247aa4f685SJohn Baldwin TAILQ_REMOVE(&ts->ts_blocked[queue], td, td_lockq); 825961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 826961a7b24SJohn Baldwin TAILQ_INSERT_TAIL(&ts->ts_pending, td, td_lockq); 8279ed346baSBosko Milekic 828961a7b24SJohn Baldwin /* 829961a7b24SJohn Baldwin * If the turnstile is now empty, remove it from its chain and 830961a7b24SJohn Baldwin * give it to the about-to-be-woken thread. Otherwise take a 831961a7b24SJohn Baldwin * turnstile from the free list and give it to the thread. 832961a7b24SJohn Baldwin */ 8337aa4f685SJohn Baldwin empty = TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) && 8347aa4f685SJohn Baldwin TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE]); 835ef0ebfc3SJohn Baldwin if (empty) { 8362502c107SJeff Roberson tc = TC_LOOKUP(ts->ts_lockobj); 8372502c107SJeff Roberson mtx_assert(&tc->tc_lock, MA_OWNED); 838961a7b24SJohn Baldwin MPASS(LIST_EMPTY(&ts->ts_free)); 839ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 840ef0ebfc3SJohn Baldwin tc->tc_depth--; 841ef0ebfc3SJohn Baldwin #endif 842ef0ebfc3SJohn Baldwin } else 843961a7b24SJohn Baldwin ts = LIST_FIRST(&ts->ts_free); 844da1d503bSJohn Baldwin MPASS(ts != NULL); 845961a7b24SJohn Baldwin LIST_REMOVE(ts, ts_hash); 846961a7b24SJohn Baldwin td->td_turnstile = ts; 8479ed346baSBosko Milekic 848961a7b24SJohn Baldwin return (empty); 849961a7b24SJohn Baldwin } 850961a7b24SJohn Baldwin 851961a7b24SJohn Baldwin /* 852961a7b24SJohn Baldwin * Put all blocked threads on the pending list. This must be called with 853961a7b24SJohn Baldwin * the turnstile chain locked. 854961a7b24SJohn Baldwin */ 855961a7b24SJohn Baldwin void 8567aa4f685SJohn Baldwin turnstile_broadcast(struct turnstile *ts, int queue) 857961a7b24SJohn Baldwin { 8583adccf38SMatt Macy struct turnstile_chain *tc __unused; 859961a7b24SJohn Baldwin struct turnstile *ts1; 860961a7b24SJohn Baldwin struct thread *td; 861961a7b24SJohn Baldwin 862961a7b24SJohn Baldwin MPASS(ts != NULL); 8632502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 864961a7b24SJohn Baldwin MPASS(curthread->td_proc->p_magic == P_MAGIC); 8655dff04c3SJeff Roberson MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL); 8662502c107SJeff Roberson /* 8672502c107SJeff Roberson * We must have the chain locked so that we can remove the empty 8682502c107SJeff Roberson * turnstile from the hash queue. 8692502c107SJeff Roberson */ 870961a7b24SJohn Baldwin tc = TC_LOOKUP(ts->ts_lockobj); 871961a7b24SJohn Baldwin mtx_assert(&tc->tc_lock, MA_OWNED); 8727aa4f685SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 873961a7b24SJohn Baldwin 874961a7b24SJohn Baldwin /* 875961a7b24SJohn Baldwin * Transfer the blocked list to the pending list. 876961a7b24SJohn Baldwin */ 877961a7b24SJohn Baldwin mtx_lock_spin(&td_contested_lock); 8787aa4f685SJohn Baldwin TAILQ_CONCAT(&ts->ts_pending, &ts->ts_blocked[queue], td_lockq); 879961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 880961a7b24SJohn Baldwin 881961a7b24SJohn Baldwin /* 882961a7b24SJohn Baldwin * Give a turnstile to each thread. The last thread gets 8837aa4f685SJohn Baldwin * this turnstile if the turnstile is empty. 884961a7b24SJohn Baldwin */ 885961a7b24SJohn Baldwin TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) { 886961a7b24SJohn Baldwin if (LIST_EMPTY(&ts->ts_free)) { 887961a7b24SJohn Baldwin MPASS(TAILQ_NEXT(td, td_lockq) == NULL); 888961a7b24SJohn Baldwin ts1 = ts; 889ef0ebfc3SJohn Baldwin #ifdef TURNSTILE_PROFILING 890ef0ebfc3SJohn Baldwin tc->tc_depth--; 891ef0ebfc3SJohn Baldwin #endif 89236412d79SJohn Baldwin } else 893961a7b24SJohn Baldwin ts1 = LIST_FIRST(&ts->ts_free); 894da1d503bSJohn Baldwin MPASS(ts1 != NULL); 895961a7b24SJohn Baldwin LIST_REMOVE(ts1, ts_hash); 896961a7b24SJohn Baldwin td->td_turnstile = ts1; 897961a7b24SJohn Baldwin } 898961a7b24SJohn Baldwin } 8999ed346baSBosko Milekic 900961a7b24SJohn Baldwin /* 901961a7b24SJohn Baldwin * Wakeup all threads on the pending list and adjust the priority of the 902961a7b24SJohn Baldwin * current thread appropriately. This must be called with the turnstile 903961a7b24SJohn Baldwin * chain locked. 904961a7b24SJohn Baldwin */ 905961a7b24SJohn Baldwin void 906*d0a22279SMateusz Guzik turnstile_unpend(struct turnstile *ts) 907961a7b24SJohn Baldwin { 908961a7b24SJohn Baldwin TAILQ_HEAD( ,thread) pending_threads; 9092502c107SJeff Roberson struct turnstile *nts; 910961a7b24SJohn Baldwin struct thread *td; 911f5c157d9SJohn Baldwin u_char cp, pri; 912961a7b24SJohn Baldwin 913961a7b24SJohn Baldwin MPASS(ts != NULL); 9142502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 9155dff04c3SJeff Roberson MPASS(ts->ts_owner == curthread || ts->ts_owner == NULL); 916961a7b24SJohn Baldwin MPASS(!TAILQ_EMPTY(&ts->ts_pending)); 917961a7b24SJohn Baldwin 918961a7b24SJohn Baldwin /* 919961a7b24SJohn Baldwin * Move the list of pending threads out of the turnstile and 920961a7b24SJohn Baldwin * into a local variable. 921961a7b24SJohn Baldwin */ 922961a7b24SJohn Baldwin TAILQ_INIT(&pending_threads); 923961a7b24SJohn Baldwin TAILQ_CONCAT(&pending_threads, &ts->ts_pending, td_lockq); 924961a7b24SJohn Baldwin #ifdef INVARIANTS 9257aa4f685SJohn Baldwin if (TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) && 9267aa4f685SJohn Baldwin TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE])) 927961a7b24SJohn Baldwin ts->ts_lockobj = NULL; 928961a7b24SJohn Baldwin #endif 9292502c107SJeff Roberson /* 9302502c107SJeff Roberson * Adjust the priority of curthread based on other contested 9312502c107SJeff Roberson * locks it owns. Don't lower the priority below the base 9322502c107SJeff Roberson * priority however. 9332502c107SJeff Roberson */ 9342502c107SJeff Roberson td = curthread; 9352502c107SJeff Roberson pri = PRI_MAX; 9362502c107SJeff Roberson thread_lock(td); 9372502c107SJeff Roberson mtx_lock_spin(&td_contested_lock); 938961a7b24SJohn Baldwin /* 939961a7b24SJohn Baldwin * Remove the turnstile from this thread's list of contested locks 940961a7b24SJohn Baldwin * since this thread doesn't own it anymore. New threads will 941961a7b24SJohn Baldwin * not be blocking on the turnstile until it is claimed by a new 9427aa4f685SJohn Baldwin * owner. There might not be a current owner if this is a shared 9437aa4f685SJohn Baldwin * lock. 944961a7b24SJohn Baldwin */ 9457aa4f685SJohn Baldwin if (ts->ts_owner != NULL) { 946961a7b24SJohn Baldwin ts->ts_owner = NULL; 947961a7b24SJohn Baldwin LIST_REMOVE(ts, ts_link); 9487aa4f685SJohn Baldwin } 9492502c107SJeff Roberson LIST_FOREACH(nts, &td->td_contested, ts_link) { 9502502c107SJeff Roberson cp = turnstile_first_waiter(nts)->td_priority; 95136412d79SJohn Baldwin if (cp < pri) 95236412d79SJohn Baldwin pri = cp; 95336412d79SJohn Baldwin } 954961a7b24SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 955f5c157d9SJohn Baldwin sched_unlend_prio(td, pri); 9562502c107SJeff Roberson thread_unlock(td); 957961a7b24SJohn Baldwin /* 958961a7b24SJohn Baldwin * Wake up all the pending threads. If a thread is not blocked 959961a7b24SJohn Baldwin * on a lock, then it is currently executing on another CPU in 96067ba8678SJohn Baldwin * turnstile_wait() or sitting on a run queue waiting to resume 96167ba8678SJohn Baldwin * in turnstile_wait(). Set a flag to force it to try to acquire 962961a7b24SJohn Baldwin * the lock again instead of blocking. 963961a7b24SJohn Baldwin */ 964961a7b24SJohn Baldwin while (!TAILQ_EMPTY(&pending_threads)) { 965961a7b24SJohn Baldwin td = TAILQ_FIRST(&pending_threads); 966961a7b24SJohn Baldwin TAILQ_REMOVE(&pending_threads, td, td_lockq); 967b3e9e682SRyan Stone SDT_PROBE2(sched, , , wakeup, td, td->td_proc); 9682502c107SJeff Roberson thread_lock(td); 969626ac252SJeff Roberson THREAD_LOCKPTR_ASSERT(td, &ts->ts_lock); 970961a7b24SJohn Baldwin MPASS(td->td_proc->p_magic == P_MAGIC); 9712502c107SJeff Roberson MPASS(TD_ON_LOCK(td)); 9722502c107SJeff Roberson TD_CLR_LOCK(td); 9732502c107SJeff Roberson MPASS(TD_CAN_RUN(td)); 974961a7b24SJohn Baldwin td->td_blocked = NULL; 975961a7b24SJohn Baldwin td->td_lockname = NULL; 976f7829d0dSAttilio Rao td->td_blktick = 0; 9777aa4f685SJohn Baldwin #ifdef INVARIANTS 9787aa4f685SJohn Baldwin td->td_tsqueue = 0xff; 9797aa4f685SJohn Baldwin #endif 980f0393f06SJeff Roberson sched_add(td, SRQ_BORING); 9812502c107SJeff Roberson thread_unlock(td); 982961a7b24SJohn Baldwin } 9832502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 9849ed346baSBosko Milekic } 9859ed346baSBosko Milekic 9869ed346baSBosko Milekic /* 987f1a4b852SJohn Baldwin * Give up ownership of a turnstile. This must be called with the 988f1a4b852SJohn Baldwin * turnstile chain locked. 989f1a4b852SJohn Baldwin */ 990f1a4b852SJohn Baldwin void 991f1a4b852SJohn Baldwin turnstile_disown(struct turnstile *ts) 992f1a4b852SJohn Baldwin { 993f1a4b852SJohn Baldwin struct thread *td; 994f1a4b852SJohn Baldwin u_char cp, pri; 995f1a4b852SJohn Baldwin 996f1a4b852SJohn Baldwin MPASS(ts != NULL); 9972502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 998f1a4b852SJohn Baldwin MPASS(ts->ts_owner == curthread); 999f1a4b852SJohn Baldwin MPASS(TAILQ_EMPTY(&ts->ts_pending)); 1000f1a4b852SJohn Baldwin MPASS(!TAILQ_EMPTY(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE]) || 1001f1a4b852SJohn Baldwin !TAILQ_EMPTY(&ts->ts_blocked[TS_SHARED_QUEUE])); 1002f1a4b852SJohn Baldwin 1003f1a4b852SJohn Baldwin /* 1004f1a4b852SJohn Baldwin * Remove the turnstile from this thread's list of contested locks 1005f1a4b852SJohn Baldwin * since this thread doesn't own it anymore. New threads will 1006f1a4b852SJohn Baldwin * not be blocking on the turnstile until it is claimed by a new 1007f1a4b852SJohn Baldwin * owner. 1008f1a4b852SJohn Baldwin */ 1009f1a4b852SJohn Baldwin mtx_lock_spin(&td_contested_lock); 1010f1a4b852SJohn Baldwin ts->ts_owner = NULL; 1011f1a4b852SJohn Baldwin LIST_REMOVE(ts, ts_link); 1012f1a4b852SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 1013f1a4b852SJohn Baldwin 1014f1a4b852SJohn Baldwin /* 1015f1a4b852SJohn Baldwin * Adjust the priority of curthread based on other contested 1016f1a4b852SJohn Baldwin * locks it owns. Don't lower the priority below the base 1017f1a4b852SJohn Baldwin * priority however. 1018f1a4b852SJohn Baldwin */ 1019f1a4b852SJohn Baldwin td = curthread; 1020f1a4b852SJohn Baldwin pri = PRI_MAX; 10212502c107SJeff Roberson thread_lock(td); 10222502c107SJeff Roberson mtx_unlock_spin(&ts->ts_lock); 1023f1a4b852SJohn Baldwin mtx_lock_spin(&td_contested_lock); 1024f1a4b852SJohn Baldwin LIST_FOREACH(ts, &td->td_contested, ts_link) { 1025f1a4b852SJohn Baldwin cp = turnstile_first_waiter(ts)->td_priority; 1026f1a4b852SJohn Baldwin if (cp < pri) 1027f1a4b852SJohn Baldwin pri = cp; 1028f1a4b852SJohn Baldwin } 1029f1a4b852SJohn Baldwin mtx_unlock_spin(&td_contested_lock); 1030f1a4b852SJohn Baldwin sched_unlend_prio(td, pri); 10312502c107SJeff Roberson thread_unlock(td); 1032f1a4b852SJohn Baldwin } 1033f1a4b852SJohn Baldwin 1034f1a4b852SJohn Baldwin /* 1035961a7b24SJohn Baldwin * Return the first thread in a turnstile. 10369ed346baSBosko Milekic */ 1037961a7b24SJohn Baldwin struct thread * 10387aa4f685SJohn Baldwin turnstile_head(struct turnstile *ts, int queue) 10390cde2e34SJason Evans { 1040961a7b24SJohn Baldwin #ifdef INVARIANTS 10415cb0fbe4SJohn Baldwin 1042961a7b24SJohn Baldwin MPASS(ts != NULL); 10437aa4f685SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 10442502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 10450cde2e34SJason Evans #endif 10467aa4f685SJohn Baldwin return (TAILQ_FIRST(&ts->ts_blocked[queue])); 1047961a7b24SJohn Baldwin } 10487aa4f685SJohn Baldwin 1049f1a4b852SJohn Baldwin /* 1050f1a4b852SJohn Baldwin * Returns true if a sub-queue of a turnstile is empty. 1051f1a4b852SJohn Baldwin */ 1052f1a4b852SJohn Baldwin int 1053f1a4b852SJohn Baldwin turnstile_empty(struct turnstile *ts, int queue) 1054f1a4b852SJohn Baldwin { 1055f1a4b852SJohn Baldwin #ifdef INVARIANTS 1056f1a4b852SJohn Baldwin 1057f1a4b852SJohn Baldwin MPASS(ts != NULL); 1058f1a4b852SJohn Baldwin MPASS(queue == TS_SHARED_QUEUE || queue == TS_EXCLUSIVE_QUEUE); 10592502c107SJeff Roberson mtx_assert(&ts->ts_lock, MA_OWNED); 1060f1a4b852SJohn Baldwin #endif 1061f1a4b852SJohn Baldwin return (TAILQ_EMPTY(&ts->ts_blocked[queue])); 1062f1a4b852SJohn Baldwin } 1063f1a4b852SJohn Baldwin 10647aa4f685SJohn Baldwin #ifdef DDB 10657aa4f685SJohn Baldwin static void 10667aa4f685SJohn Baldwin print_thread(struct thread *td, const char *prefix) 10677aa4f685SJohn Baldwin { 10687aa4f685SJohn Baldwin 10697aa4f685SJohn Baldwin db_printf("%s%p (tid %d, pid %d, \"%s\")\n", prefix, td, td->td_tid, 107086a448c3SKonstantin Belousov td->td_proc->p_pid, td->td_name); 10717aa4f685SJohn Baldwin } 10727aa4f685SJohn Baldwin 10737aa4f685SJohn Baldwin static void 10747aa4f685SJohn Baldwin print_queue(struct threadqueue *queue, const char *header, const char *prefix) 10757aa4f685SJohn Baldwin { 10767aa4f685SJohn Baldwin struct thread *td; 10777aa4f685SJohn Baldwin 10787aa4f685SJohn Baldwin db_printf("%s:\n", header); 10797aa4f685SJohn Baldwin if (TAILQ_EMPTY(queue)) { 10807aa4f685SJohn Baldwin db_printf("%sempty\n", prefix); 10817aa4f685SJohn Baldwin return; 10827aa4f685SJohn Baldwin } 10837aa4f685SJohn Baldwin TAILQ_FOREACH(td, queue, td_lockq) { 10847aa4f685SJohn Baldwin print_thread(td, prefix); 10857aa4f685SJohn Baldwin } 10867aa4f685SJohn Baldwin } 10877aa4f685SJohn Baldwin 10887aa4f685SJohn Baldwin DB_SHOW_COMMAND(turnstile, db_show_turnstile) 10897aa4f685SJohn Baldwin { 10907aa4f685SJohn Baldwin struct turnstile_chain *tc; 10917aa4f685SJohn Baldwin struct turnstile *ts; 10927aa4f685SJohn Baldwin struct lock_object *lock; 10937aa4f685SJohn Baldwin int i; 10947aa4f685SJohn Baldwin 10957aa4f685SJohn Baldwin if (!have_addr) 10967aa4f685SJohn Baldwin return; 10977aa4f685SJohn Baldwin 10987aa4f685SJohn Baldwin /* 10997aa4f685SJohn Baldwin * First, see if there is an active turnstile for the lock indicated 11007aa4f685SJohn Baldwin * by the address. 11017aa4f685SJohn Baldwin */ 11027aa4f685SJohn Baldwin lock = (struct lock_object *)addr; 11037aa4f685SJohn Baldwin tc = TC_LOOKUP(lock); 11047aa4f685SJohn Baldwin LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash) 11057aa4f685SJohn Baldwin if (ts->ts_lockobj == lock) 11067aa4f685SJohn Baldwin goto found; 11077aa4f685SJohn Baldwin 11087aa4f685SJohn Baldwin /* 11097aa4f685SJohn Baldwin * Second, see if there is an active turnstile at the address 11107aa4f685SJohn Baldwin * indicated. 11117aa4f685SJohn Baldwin */ 11127aa4f685SJohn Baldwin for (i = 0; i < TC_TABLESIZE; i++) 11137aa4f685SJohn Baldwin LIST_FOREACH(ts, &turnstile_chains[i].tc_turnstiles, ts_hash) { 11147aa4f685SJohn Baldwin if (ts == (struct turnstile *)addr) 11157aa4f685SJohn Baldwin goto found; 11167aa4f685SJohn Baldwin } 11177aa4f685SJohn Baldwin 11187aa4f685SJohn Baldwin db_printf("Unable to locate a turnstile via %p\n", (void *)addr); 11197aa4f685SJohn Baldwin return; 11207aa4f685SJohn Baldwin found: 11217aa4f685SJohn Baldwin lock = ts->ts_lockobj; 11227aa4f685SJohn Baldwin db_printf("Lock: %p - (%s) %s\n", lock, LOCK_CLASS(lock)->lc_name, 11237aa4f685SJohn Baldwin lock->lo_name); 11247aa4f685SJohn Baldwin if (ts->ts_owner) 11257aa4f685SJohn Baldwin print_thread(ts->ts_owner, "Lock Owner: "); 11267aa4f685SJohn Baldwin else 11277aa4f685SJohn Baldwin db_printf("Lock Owner: none\n"); 11287aa4f685SJohn Baldwin print_queue(&ts->ts_blocked[TS_SHARED_QUEUE], "Shared Waiters", "\t"); 11297aa4f685SJohn Baldwin print_queue(&ts->ts_blocked[TS_EXCLUSIVE_QUEUE], "Exclusive Waiters", 11307aa4f685SJohn Baldwin "\t"); 11317aa4f685SJohn Baldwin print_queue(&ts->ts_pending, "Pending Threads", "\t"); 11327aa4f685SJohn Baldwin 11337aa4f685SJohn Baldwin } 1134ae110b53SJohn Baldwin 113577e66268SJohn Baldwin /* 113677e66268SJohn Baldwin * Show all the threads a particular thread is waiting on based on 11370c1d923eSConrad Meyer * non-spin locks. 113877e66268SJohn Baldwin */ 1139ae110b53SJohn Baldwin static void 114077e66268SJohn Baldwin print_lockchain(struct thread *td, const char *prefix) 1141ae110b53SJohn Baldwin { 1142ae110b53SJohn Baldwin struct lock_object *lock; 1143ae110b53SJohn Baldwin struct lock_class *class; 1144ae110b53SJohn Baldwin struct turnstile *ts; 11450c1d923eSConrad Meyer struct thread *owner; 1146ae110b53SJohn Baldwin 1147ae110b53SJohn Baldwin /* 1148ae110b53SJohn Baldwin * Follow the chain. We keep walking as long as the thread is 11490c1d923eSConrad Meyer * blocked on a lock that has an owner. 1150ae110b53SJohn Baldwin */ 1151fed79884SJohn Baldwin while (!db_pager_quit) { 1152ae110b53SJohn Baldwin db_printf("%sthread %d (pid %d, %s) ", prefix, td->td_tid, 115386a448c3SKonstantin Belousov td->td_proc->p_pid, td->td_name); 1154ae110b53SJohn Baldwin switch (td->td_state) { 1155ae110b53SJohn Baldwin case TDS_INACTIVE: 1156ae110b53SJohn Baldwin db_printf("is inactive\n"); 1157ae110b53SJohn Baldwin return; 1158ae110b53SJohn Baldwin case TDS_CAN_RUN: 1159ae110b53SJohn Baldwin db_printf("can run\n"); 1160ae110b53SJohn Baldwin return; 1161ae110b53SJohn Baldwin case TDS_RUNQ: 1162ae110b53SJohn Baldwin db_printf("is on a run queue\n"); 1163ae110b53SJohn Baldwin return; 1164ae110b53SJohn Baldwin case TDS_RUNNING: 1165ae110b53SJohn Baldwin db_printf("running on CPU %d\n", td->td_oncpu); 1166ae110b53SJohn Baldwin return; 1167ae110b53SJohn Baldwin case TDS_INHIBITED: 1168ae110b53SJohn Baldwin if (TD_ON_LOCK(td)) { 1169ae110b53SJohn Baldwin ts = td->td_blocked; 1170ae110b53SJohn Baldwin lock = ts->ts_lockobj; 1171ae110b53SJohn Baldwin class = LOCK_CLASS(lock); 1172ae110b53SJohn Baldwin db_printf("blocked on lock %p (%s) \"%s\"\n", 1173ae110b53SJohn Baldwin lock, class->lc_name, lock->lo_name); 1174ae110b53SJohn Baldwin if (ts->ts_owner == NULL) 1175ae110b53SJohn Baldwin return; 1176ae110b53SJohn Baldwin td = ts->ts_owner; 1177ae110b53SJohn Baldwin break; 11780c1d923eSConrad Meyer } else if (TD_ON_SLEEPQ(td)) { 11790c1d923eSConrad Meyer if (!lockmgr_chain(td, &owner) && 11800c1d923eSConrad Meyer !sx_chain(td, &owner)) { 11810c1d923eSConrad Meyer db_printf("sleeping on %p \"%s\"\n", 11820c1d923eSConrad Meyer td->td_wchan, td->td_wmesg); 11830c1d923eSConrad Meyer return; 11840c1d923eSConrad Meyer } 11850c1d923eSConrad Meyer if (owner == NULL) 11860c1d923eSConrad Meyer return; 11870c1d923eSConrad Meyer td = owner; 11880c1d923eSConrad Meyer break; 1189ae110b53SJohn Baldwin } 1190ae110b53SJohn Baldwin db_printf("inhibited\n"); 1191ae110b53SJohn Baldwin return; 1192ae110b53SJohn Baldwin default: 1193ae110b53SJohn Baldwin db_printf("??? (%#x)\n", td->td_state); 1194ae110b53SJohn Baldwin return; 1195ae110b53SJohn Baldwin } 1196ae110b53SJohn Baldwin } 1197ae110b53SJohn Baldwin } 1198ae110b53SJohn Baldwin 119977e66268SJohn Baldwin DB_SHOW_COMMAND(lockchain, db_show_lockchain) 1200ae110b53SJohn Baldwin { 1201ae110b53SJohn Baldwin struct thread *td; 1202ae110b53SJohn Baldwin 1203ae110b53SJohn Baldwin /* Figure out which thread to start with. */ 1204ae110b53SJohn Baldwin if (have_addr) 1205cd508278SPedro F. Giffuni td = db_lookup_thread(addr, true); 1206ae110b53SJohn Baldwin else 1207ae110b53SJohn Baldwin td = kdb_thread; 1208ae110b53SJohn Baldwin 120977e66268SJohn Baldwin print_lockchain(td, ""); 1210ae110b53SJohn Baldwin } 12110c1d923eSConrad Meyer DB_SHOW_ALIAS(sleepchain, db_show_lockchain); 1212ae110b53SJohn Baldwin 121339297ba4SSam Leffler DB_SHOW_ALL_COMMAND(chains, db_show_allchains) 1214ae110b53SJohn Baldwin { 1215ae110b53SJohn Baldwin struct thread *td; 1216ae110b53SJohn Baldwin struct proc *p; 1217ae110b53SJohn Baldwin int i; 1218ae110b53SJohn Baldwin 1219ae110b53SJohn Baldwin i = 1; 12204f506694SXin LI FOREACH_PROC_IN_SYSTEM(p) { 1221ae110b53SJohn Baldwin FOREACH_THREAD_IN_PROC(p, td) { 12220c1d923eSConrad Meyer if ((TD_ON_LOCK(td) && LIST_EMPTY(&td->td_contested)) 12230c1d923eSConrad Meyer || (TD_IS_INHIBITED(td) && TD_ON_SLEEPQ(td))) { 1224ae110b53SJohn Baldwin db_printf("chain %d:\n", i++); 122577e66268SJohn Baldwin print_lockchain(td, " "); 1226ae110b53SJohn Baldwin } 1227fed79884SJohn Baldwin if (db_pager_quit) 1228fed79884SJohn Baldwin return; 1229ae110b53SJohn Baldwin } 1230ae110b53SJohn Baldwin } 1231ae110b53SJohn Baldwin } 123239297ba4SSam Leffler DB_SHOW_ALIAS(allchains, db_show_allchains) 1233ae110b53SJohn Baldwin 1234ae110b53SJohn Baldwin static void print_waiters(struct turnstile *ts, int indent); 1235ae110b53SJohn Baldwin 1236ae110b53SJohn Baldwin static void 1237ae110b53SJohn Baldwin print_waiter(struct thread *td, int indent) 1238ae110b53SJohn Baldwin { 1239ae110b53SJohn Baldwin struct turnstile *ts; 1240ae110b53SJohn Baldwin int i; 1241ae110b53SJohn Baldwin 1242fed79884SJohn Baldwin if (db_pager_quit) 1243fed79884SJohn Baldwin return; 1244ae110b53SJohn Baldwin for (i = 0; i < indent; i++) 1245ae110b53SJohn Baldwin db_printf(" "); 1246ae110b53SJohn Baldwin print_thread(td, "thread "); 1247ae110b53SJohn Baldwin LIST_FOREACH(ts, &td->td_contested, ts_link) 1248ae110b53SJohn Baldwin print_waiters(ts, indent + 1); 1249ae110b53SJohn Baldwin } 1250ae110b53SJohn Baldwin 1251ae110b53SJohn Baldwin static void 1252ae110b53SJohn Baldwin print_waiters(struct turnstile *ts, int indent) 1253ae110b53SJohn Baldwin { 1254ae110b53SJohn Baldwin struct lock_object *lock; 1255ae110b53SJohn Baldwin struct lock_class *class; 1256ae110b53SJohn Baldwin struct thread *td; 1257ae110b53SJohn Baldwin int i; 1258ae110b53SJohn Baldwin 1259fed79884SJohn Baldwin if (db_pager_quit) 1260fed79884SJohn Baldwin return; 1261ae110b53SJohn Baldwin lock = ts->ts_lockobj; 1262ae110b53SJohn Baldwin class = LOCK_CLASS(lock); 1263ae110b53SJohn Baldwin for (i = 0; i < indent; i++) 1264ae110b53SJohn Baldwin db_printf(" "); 1265ae110b53SJohn Baldwin db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, lock->lo_name); 1266ae110b53SJohn Baldwin TAILQ_FOREACH(td, &ts->ts_blocked[TS_EXCLUSIVE_QUEUE], td_lockq) 1267ae110b53SJohn Baldwin print_waiter(td, indent + 1); 1268ae110b53SJohn Baldwin TAILQ_FOREACH(td, &ts->ts_blocked[TS_SHARED_QUEUE], td_lockq) 1269ae110b53SJohn Baldwin print_waiter(td, indent + 1); 1270ae110b53SJohn Baldwin TAILQ_FOREACH(td, &ts->ts_pending, td_lockq) 1271ae110b53SJohn Baldwin print_waiter(td, indent + 1); 1272ae110b53SJohn Baldwin } 1273ae110b53SJohn Baldwin 127477e66268SJohn Baldwin DB_SHOW_COMMAND(locktree, db_show_locktree) 1275ae110b53SJohn Baldwin { 1276ae110b53SJohn Baldwin struct lock_object *lock; 1277ae110b53SJohn Baldwin struct lock_class *class; 1278ae110b53SJohn Baldwin struct turnstile_chain *tc; 1279ae110b53SJohn Baldwin struct turnstile *ts; 1280ae110b53SJohn Baldwin 1281ae110b53SJohn Baldwin if (!have_addr) 1282ae110b53SJohn Baldwin return; 1283ae110b53SJohn Baldwin lock = (struct lock_object *)addr; 1284ae110b53SJohn Baldwin tc = TC_LOOKUP(lock); 1285ae110b53SJohn Baldwin LIST_FOREACH(ts, &tc->tc_turnstiles, ts_hash) 1286ae110b53SJohn Baldwin if (ts->ts_lockobj == lock) 1287ae110b53SJohn Baldwin break; 1288ae110b53SJohn Baldwin if (ts == NULL) { 1289ae110b53SJohn Baldwin class = LOCK_CLASS(lock); 1290ae110b53SJohn Baldwin db_printf("lock %p (%s) \"%s\"\n", lock, class->lc_name, 1291ae110b53SJohn Baldwin lock->lo_name); 1292ae110b53SJohn Baldwin } else 1293ae110b53SJohn Baldwin print_waiters(ts, 0); 1294ae110b53SJohn Baldwin } 12957aa4f685SJohn Baldwin #endif 1296