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