kern_lock.c revision 175166
1/*- 2 * Copyright (c) 1995 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Copyright (C) 1997 6 * John S. Dyson. All rights reserved. 7 * 8 * This code contains ideas from software contributed to Berkeley by 9 * Avadis Tevanian, Jr., Michael Wayne Young, and the Mach Operating 10 * System project at Carnegie-Mellon University. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 * 40 * @(#)kern_lock.c 8.18 (Berkeley) 5/21/95 41 */ 42 43#include <sys/cdefs.h> 44__FBSDID("$FreeBSD: head/sys/kern/kern_lock.c 175166 2008-01-08 23:48:31Z attilio $"); 45 46#include "opt_ddb.h" 47#include "opt_global.h" 48 49#include <sys/param.h> 50#include <sys/kdb.h> 51#include <sys/kernel.h> 52#include <sys/ktr.h> 53#include <sys/lock.h> 54#include <sys/lockmgr.h> 55#include <sys/mutex.h> 56#include <sys/proc.h> 57#include <sys/systm.h> 58#include <sys/lock_profile.h> 59#ifdef DEBUG_LOCKS 60#include <sys/stack.h> 61#endif 62 63static void assert_lockmgr(struct lock_object *lock, int what); 64#ifdef DDB 65#include <ddb/ddb.h> 66static void db_show_lockmgr(struct lock_object *lock); 67#endif 68static void lock_lockmgr(struct lock_object *lock, int how); 69static int unlock_lockmgr(struct lock_object *lock); 70 71struct lock_class lock_class_lockmgr = { 72 .lc_name = "lockmgr", 73 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, 74 .lc_assert = assert_lockmgr, 75#ifdef DDB 76 .lc_ddb_show = db_show_lockmgr, 77#endif 78 .lc_lock = lock_lockmgr, 79 .lc_unlock = unlock_lockmgr, 80}; 81 82/* 83 * Locking primitives implementation. 84 * Locks provide shared/exclusive sychronization. 85 */ 86 87void 88assert_lockmgr(struct lock_object *lock, int what) 89{ 90 91 panic("lockmgr locks do not support assertions"); 92} 93 94void 95lock_lockmgr(struct lock_object *lock, int how) 96{ 97 98 panic("lockmgr locks do not support sleep interlocking"); 99} 100 101int 102unlock_lockmgr(struct lock_object *lock) 103{ 104 105 panic("lockmgr locks do not support sleep interlocking"); 106} 107 108#define COUNT(td, x) ((td)->td_locks += (x)) 109#define LK_ALL (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | \ 110 LK_SHARE_NONZERO | LK_WAIT_NONZERO) 111 112static int acquire(struct lock **lkpp, int extflags, int wanted, int *contested, uint64_t *waittime); 113static int acquiredrain(struct lock *lkp, int extflags) ; 114 115static __inline void 116sharelock(struct thread *td, struct lock *lkp, int incr) { 117 lkp->lk_flags |= LK_SHARE_NONZERO; 118 lkp->lk_sharecount += incr; 119 COUNT(td, incr); 120} 121 122static __inline void 123shareunlock(struct thread *td, struct lock *lkp, int decr) { 124 125 KASSERT(lkp->lk_sharecount >= decr, ("shareunlock: count < decr")); 126 127 COUNT(td, -decr); 128 if (lkp->lk_sharecount == decr) { 129 lkp->lk_flags &= ~LK_SHARE_NONZERO; 130 if (lkp->lk_flags & (LK_WANT_UPGRADE | LK_WANT_EXCL)) { 131 wakeup(lkp); 132 } 133 lkp->lk_sharecount = 0; 134 } else { 135 lkp->lk_sharecount -= decr; 136 } 137} 138 139static int 140acquire(struct lock **lkpp, int extflags, int wanted, int *contested, uint64_t *waittime) 141{ 142 struct lock *lkp = *lkpp; 143 int error; 144 CTR3(KTR_LOCK, 145 "acquire(): lkp == %p, extflags == 0x%x, wanted == 0x%x", 146 lkp, extflags, wanted); 147 148 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & wanted)) 149 return EBUSY; 150 error = 0; 151 if ((lkp->lk_flags & wanted) != 0) 152 lock_profile_obtain_lock_failed(&lkp->lk_object, contested, waittime); 153 154 while ((lkp->lk_flags & wanted) != 0) { 155 CTR2(KTR_LOCK, 156 "acquire(): lkp == %p, lk_flags == 0x%x sleeping", 157 lkp, lkp->lk_flags); 158 lkp->lk_flags |= LK_WAIT_NONZERO; 159 lkp->lk_waitcount++; 160 error = msleep(lkp, lkp->lk_interlock, lkp->lk_prio, 161 lkp->lk_wmesg, 162 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0)); 163 lkp->lk_waitcount--; 164 if (lkp->lk_waitcount == 0) 165 lkp->lk_flags &= ~LK_WAIT_NONZERO; 166 if (error) 167 break; 168 if (extflags & LK_SLEEPFAIL) { 169 error = ENOLCK; 170 break; 171 } 172 if (lkp->lk_newlock != NULL) { 173 mtx_lock(lkp->lk_newlock->lk_interlock); 174 mtx_unlock(lkp->lk_interlock); 175 if (lkp->lk_waitcount == 0) 176 wakeup((void *)(&lkp->lk_newlock)); 177 *lkpp = lkp = lkp->lk_newlock; 178 } 179 } 180 mtx_assert(lkp->lk_interlock, MA_OWNED); 181 return (error); 182} 183 184/* 185 * Set, change, or release a lock. 186 * 187 * Shared requests increment the shared count. Exclusive requests set the 188 * LK_WANT_EXCL flag (preventing further shared locks), and wait for already 189 * accepted shared locks and shared-to-exclusive upgrades to go away. 190 */ 191int 192_lockmgr(struct lock *lkp, u_int flags, struct mtx *interlkp, 193 struct thread *td, char *file, int line) 194 195{ 196 int error; 197 int extflags, lockflags; 198 int contested = 0; 199 uint64_t waitstart = 0; 200 201 /* 202 * Lock owner can only be curthread in order to have a deadlock 203 * free implementation of the primitive. 204 */ 205 KASSERT(td == curthread, 206 ("lockmgr: owner thread (%p) cannot differ from curthread", td)); 207 208 error = 0; 209 210 if ((flags & LK_INTERNAL) == 0) 211 mtx_lock(lkp->lk_interlock); 212 CTR6(KTR_LOCK, 213 "lockmgr(): lkp == %p (lk_wmesg == \"%s\"), owner == %p, exclusivecount == %d, flags == 0x%x, " 214 "td == %p", lkp, lkp->lk_wmesg, lkp->lk_lockholder, 215 lkp->lk_exclusivecount, flags, td); 216#ifdef DEBUG_LOCKS 217 { 218 struct stack stack; /* XXX */ 219 stack_save(&stack); 220 CTRSTACK(KTR_LOCK, &stack, 0, 1); 221 } 222#endif 223 224 if (flags & LK_INTERLOCK) { 225 mtx_assert(interlkp, MA_OWNED | MA_NOTRECURSED); 226 mtx_unlock(interlkp); 227 } 228 229 if ((flags & (LK_NOWAIT|LK_RELEASE)) == 0) 230 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, 231 &lkp->lk_interlock->lock_object, 232 "Acquiring lockmgr lock \"%s\"", lkp->lk_wmesg); 233 234 if (panicstr != NULL) { 235 mtx_unlock(lkp->lk_interlock); 236 return (0); 237 } 238 if ((lkp->lk_flags & LK_NOSHARE) && 239 (flags & LK_TYPE_MASK) == LK_SHARED) { 240 flags &= ~LK_TYPE_MASK; 241 flags |= LK_EXCLUSIVE; 242 } 243 extflags = (flags | lkp->lk_flags) & LK_EXTFLG_MASK; 244 245 switch (flags & LK_TYPE_MASK) { 246 247 case LK_SHARED: 248 /* 249 * If we are not the exclusive lock holder, we have to block 250 * while there is an exclusive lock holder or while an 251 * exclusive lock request or upgrade request is in progress. 252 * 253 * However, if TDP_DEADLKTREAT is set, we override exclusive 254 * lock requests or upgrade requests ( but not the exclusive 255 * lock itself ). 256 */ 257 if (lkp->lk_lockholder != td) { 258 lockflags = LK_HAVE_EXCL; 259 if (td != NULL && !(td->td_pflags & TDP_DEADLKTREAT)) 260 lockflags |= LK_WANT_EXCL | LK_WANT_UPGRADE; 261 error = acquire(&lkp, extflags, lockflags, &contested, &waitstart); 262 if (error) 263 break; 264 sharelock(td, lkp, 1); 265 if (lkp->lk_sharecount == 1) 266 lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line); 267 268#if defined(DEBUG_LOCKS) 269 stack_save(&lkp->lk_stack); 270#endif 271 break; 272 } 273 /* 274 * We hold an exclusive lock, so downgrade it to shared. 275 * An alternative would be to fail with EDEADLK. 276 */ 277 sharelock(td, lkp, 1); 278 if (lkp->lk_sharecount == 1) 279 lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line); 280 /* FALLTHROUGH downgrade */ 281 282 case LK_DOWNGRADE: 283 KASSERT(lkp->lk_lockholder == td && lkp->lk_exclusivecount != 0, 284 ("lockmgr: not holding exclusive lock " 285 "(owner thread (%p) != thread (%p), exlcnt (%d) != 0", 286 lkp->lk_lockholder, td, lkp->lk_exclusivecount)); 287 sharelock(td, lkp, lkp->lk_exclusivecount); 288 COUNT(td, -lkp->lk_exclusivecount); 289 lkp->lk_exclusivecount = 0; 290 lkp->lk_flags &= ~LK_HAVE_EXCL; 291 lkp->lk_lockholder = LK_NOPROC; 292 if (lkp->lk_waitcount) 293 wakeup((void *)lkp); 294 break; 295 296 case LK_UPGRADE: 297 /* 298 * Upgrade a shared lock to an exclusive one. If another 299 * shared lock has already requested an upgrade to an 300 * exclusive lock, our shared lock is released and an 301 * exclusive lock is requested (which will be granted 302 * after the upgrade). If we return an error, the file 303 * will always be unlocked. 304 */ 305 if (lkp->lk_lockholder == td) 306 panic("lockmgr: upgrade exclusive lock"); 307 if (lkp->lk_sharecount <= 0) 308 panic("lockmgr: upgrade without shared"); 309 shareunlock(td, lkp, 1); 310 if (lkp->lk_sharecount == 0) 311 lock_profile_release_lock(&lkp->lk_object); 312 /* 313 * If we are just polling, check to see if we will block. 314 */ 315 if ((extflags & LK_NOWAIT) && 316 ((lkp->lk_flags & LK_WANT_UPGRADE) || 317 lkp->lk_sharecount > 1)) { 318 error = EBUSY; 319 break; 320 } 321 if ((lkp->lk_flags & LK_WANT_UPGRADE) == 0) { 322 /* 323 * We are first shared lock to request an upgrade, so 324 * request upgrade and wait for the shared count to 325 * drop to zero, then take exclusive lock. 326 */ 327 lkp->lk_flags |= LK_WANT_UPGRADE; 328 error = acquire(&lkp, extflags, LK_SHARE_NONZERO, &contested, &waitstart); 329 lkp->lk_flags &= ~LK_WANT_UPGRADE; 330 331 if (error) { 332 if ((lkp->lk_flags & ( LK_WANT_EXCL | LK_WAIT_NONZERO)) == (LK_WANT_EXCL | LK_WAIT_NONZERO)) 333 wakeup((void *)lkp); 334 break; 335 } 336 if (lkp->lk_exclusivecount != 0) 337 panic("lockmgr: non-zero exclusive count"); 338 lkp->lk_flags |= LK_HAVE_EXCL; 339 lkp->lk_lockholder = td; 340 lkp->lk_exclusivecount = 1; 341 COUNT(td, 1); 342 lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line); 343#if defined(DEBUG_LOCKS) 344 stack_save(&lkp->lk_stack); 345#endif 346 break; 347 } 348 /* 349 * Someone else has requested upgrade. Release our shared 350 * lock, awaken upgrade requestor if we are the last shared 351 * lock, then request an exclusive lock. 352 */ 353 if ( (lkp->lk_flags & (LK_SHARE_NONZERO|LK_WAIT_NONZERO)) == 354 LK_WAIT_NONZERO) 355 wakeup((void *)lkp); 356 /* FALLTHROUGH exclusive request */ 357 358 case LK_EXCLUSIVE: 359 if (lkp->lk_lockholder == td) { 360 /* 361 * Recursive lock. 362 */ 363 if ((extflags & (LK_NOWAIT | LK_CANRECURSE)) == 0) 364 panic("lockmgr: locking against myself"); 365 if ((extflags & LK_CANRECURSE) != 0) { 366 lkp->lk_exclusivecount++; 367 COUNT(td, 1); 368 break; 369 } 370 } 371 /* 372 * If we are just polling, check to see if we will sleep. 373 */ 374 if ((extflags & LK_NOWAIT) && 375 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO))) { 376 error = EBUSY; 377 break; 378 } 379 /* 380 * Try to acquire the want_exclusive flag. 381 */ 382 error = acquire(&lkp, extflags, (LK_HAVE_EXCL | LK_WANT_EXCL), &contested, &waitstart); 383 if (error) 384 break; 385 lkp->lk_flags |= LK_WANT_EXCL; 386 /* 387 * Wait for shared locks and upgrades to finish. 388 */ 389 error = acquire(&lkp, extflags, LK_HAVE_EXCL | LK_WANT_UPGRADE | LK_SHARE_NONZERO, &contested, &waitstart); 390 lkp->lk_flags &= ~LK_WANT_EXCL; 391 if (error) { 392 if (lkp->lk_flags & LK_WAIT_NONZERO) 393 wakeup((void *)lkp); 394 break; 395 } 396 lkp->lk_flags |= LK_HAVE_EXCL; 397 lkp->lk_lockholder = td; 398 if (lkp->lk_exclusivecount != 0) 399 panic("lockmgr: non-zero exclusive count"); 400 lkp->lk_exclusivecount = 1; 401 COUNT(td, 1); 402 lock_profile_obtain_lock_success(&lkp->lk_object, contested, waitstart, file, line); 403#if defined(DEBUG_LOCKS) 404 stack_save(&lkp->lk_stack); 405#endif 406 break; 407 408 case LK_RELEASE: 409 if (lkp->lk_exclusivecount != 0) { 410 if (lkp->lk_lockholder != td && 411 lkp->lk_lockholder != LK_KERNPROC) { 412 panic("lockmgr: thread %p, not %s %p unlocking", 413 td, "exclusive lock holder", 414 lkp->lk_lockholder); 415 } 416 if (lkp->lk_lockholder != LK_KERNPROC) 417 COUNT(td, -1); 418 if (lkp->lk_exclusivecount == 1) { 419 lkp->lk_flags &= ~LK_HAVE_EXCL; 420 lkp->lk_lockholder = LK_NOPROC; 421 lkp->lk_exclusivecount = 0; 422 lock_profile_release_lock(&lkp->lk_object); 423 } else { 424 lkp->lk_exclusivecount--; 425 } 426 } else if (lkp->lk_flags & LK_SHARE_NONZERO) 427 shareunlock(td, lkp, 1); 428 else { 429 printf("lockmgr: thread %p unlocking unheld lock\n", 430 td); 431 kdb_backtrace(); 432 } 433 434 if (lkp->lk_flags & LK_WAIT_NONZERO) 435 wakeup((void *)lkp); 436 break; 437 438 case LK_DRAIN: 439 /* 440 * Check that we do not already hold the lock, as it can 441 * never drain if we do. Unfortunately, we have no way to 442 * check for holding a shared lock, but at least we can 443 * check for an exclusive one. 444 */ 445 if (lkp->lk_lockholder == td) 446 panic("lockmgr: draining against myself"); 447 448 error = acquiredrain(lkp, extflags); 449 if (error) 450 break; 451 lkp->lk_flags |= LK_DRAINING | LK_HAVE_EXCL; 452 lkp->lk_lockholder = td; 453 lkp->lk_exclusivecount = 1; 454 COUNT(td, 1); 455#if defined(DEBUG_LOCKS) 456 stack_save(&lkp->lk_stack); 457#endif 458 break; 459 460 default: 461 mtx_unlock(lkp->lk_interlock); 462 panic("lockmgr: unknown locktype request %d", 463 flags & LK_TYPE_MASK); 464 /* NOTREACHED */ 465 } 466 if ((lkp->lk_flags & LK_WAITDRAIN) && 467 (lkp->lk_flags & (LK_HAVE_EXCL | LK_WANT_EXCL | LK_WANT_UPGRADE | 468 LK_SHARE_NONZERO | LK_WAIT_NONZERO)) == 0) { 469 lkp->lk_flags &= ~LK_WAITDRAIN; 470 wakeup((void *)&lkp->lk_flags); 471 } 472 mtx_unlock(lkp->lk_interlock); 473 return (error); 474} 475 476static int 477acquiredrain(struct lock *lkp, int extflags) { 478 int error; 479 480 if ((extflags & LK_NOWAIT) && (lkp->lk_flags & LK_ALL)) { 481 return EBUSY; 482 } 483 while (lkp->lk_flags & LK_ALL) { 484 lkp->lk_flags |= LK_WAITDRAIN; 485 error = msleep(&lkp->lk_flags, lkp->lk_interlock, lkp->lk_prio, 486 lkp->lk_wmesg, 487 ((extflags & LK_TIMELOCK) ? lkp->lk_timo : 0)); 488 if (error) 489 return error; 490 if (extflags & LK_SLEEPFAIL) { 491 return ENOLCK; 492 } 493 } 494 return 0; 495} 496 497/* 498 * Initialize a lock; required before use. 499 */ 500void 501lockinit(lkp, prio, wmesg, timo, flags) 502 struct lock *lkp; 503 int prio; 504 const char *wmesg; 505 int timo; 506 int flags; 507{ 508 CTR5(KTR_LOCK, "lockinit(): lkp == %p, prio == %d, wmesg == \"%s\", " 509 "timo == %d, flags = 0x%x\n", lkp, prio, wmesg, timo, flags); 510 511 lkp->lk_interlock = mtx_pool_alloc(mtxpool_lockbuilder); 512 lkp->lk_flags = (flags & LK_EXTFLG_MASK); 513 lkp->lk_sharecount = 0; 514 lkp->lk_waitcount = 0; 515 lkp->lk_exclusivecount = 0; 516 lkp->lk_prio = prio; 517 lkp->lk_timo = timo; 518 lkp->lk_lockholder = LK_NOPROC; 519 lkp->lk_newlock = NULL; 520#ifdef DEBUG_LOCKS 521 stack_zero(&lkp->lk_stack); 522#endif 523 lock_init(&lkp->lk_object, &lock_class_lockmgr, wmesg, NULL, 524 LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE); 525} 526 527/* 528 * Destroy a lock. 529 */ 530void 531lockdestroy(lkp) 532 struct lock *lkp; 533{ 534 535 CTR2(KTR_LOCK, "lockdestroy(): lkp == %p (lk_wmesg == \"%s\")", 536 lkp, lkp->lk_wmesg); 537 lock_destroy(&lkp->lk_object); 538} 539 540/* 541 * Disown the lockmgr. 542 */ 543void 544lockmgr_disown(struct lock *lkp) 545{ 546 struct thread *td; 547 548 td = curthread; 549 KASSERT(lkp->lk_exclusivecount || lkp->lk_lockholder == LK_KERNPROC, 550 ("%s: %p lockmgr must be exclusively locked", __func__, lkp)); 551 KASSERT(lkp->lk_lockholder == td, 552 ("%s: %p lockmgr must be locked by curthread (%p)", __func__, lkp, 553 td)); 554 555 /* 556 * Drop the lock reference and switch the owner. This will result 557 * in an atomic operation like td_lock is only accessed by curthread 558 * and lk_lockholder only needs one write. 559 */ 560 if (lkp->lk_lockholder == td) 561 td->td_locks--; 562 lkp->lk_lockholder = LK_KERNPROC; 563} 564 565/* 566 * Determine the status of a lock. 567 */ 568int 569lockstatus(lkp, td) 570 struct lock *lkp; 571 struct thread *td; 572{ 573 int lock_type = 0; 574 int interlocked; 575 576 if (!kdb_active) { 577 interlocked = 1; 578 mtx_lock(lkp->lk_interlock); 579 } else 580 interlocked = 0; 581 if (lkp->lk_exclusivecount != 0) { 582 if (td == NULL || lkp->lk_lockholder == td) 583 lock_type = LK_EXCLUSIVE; 584 else 585 lock_type = LK_EXCLOTHER; 586 } else if (lkp->lk_sharecount != 0) 587 lock_type = LK_SHARED; 588 if (interlocked) 589 mtx_unlock(lkp->lk_interlock); 590 return (lock_type); 591} 592 593/* 594 * Determine the number of holders of a lock. 595 */ 596int 597lockcount(lkp) 598 struct lock *lkp; 599{ 600 int count; 601 602 mtx_lock(lkp->lk_interlock); 603 count = lkp->lk_exclusivecount + lkp->lk_sharecount; 604 mtx_unlock(lkp->lk_interlock); 605 return (count); 606} 607 608/* 609 * Determine the number of waiters on a lock. 610 */ 611int 612lockwaiters(lkp) 613 struct lock *lkp; 614{ 615 int count; 616 617 mtx_lock(lkp->lk_interlock); 618 count = lkp->lk_waitcount; 619 mtx_unlock(lkp->lk_interlock); 620 return (count); 621} 622 623/* 624 * Print out information about state of a lock. Used by VOP_PRINT 625 * routines to display status about contained locks. 626 */ 627void 628lockmgr_printinfo(lkp) 629 struct lock *lkp; 630{ 631 632 if (lkp->lk_sharecount) 633 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg, 634 lkp->lk_sharecount); 635 else if (lkp->lk_flags & LK_HAVE_EXCL) 636 printf(" lock type %s: EXCL (count %d) by thread %p (pid %d)", 637 lkp->lk_wmesg, lkp->lk_exclusivecount, 638 lkp->lk_lockholder, lkp->lk_lockholder->td_proc->p_pid); 639 if (lkp->lk_waitcount > 0) 640 printf(" with %d pending", lkp->lk_waitcount); 641#ifdef DEBUG_LOCKS 642 stack_print_ddb(&lkp->lk_stack); 643#endif 644} 645 646#ifdef DDB 647/* 648 * Check to see if a thread that is blocked on a sleep queue is actually 649 * blocked on a 'struct lock'. If so, output some details and return true. 650 * If the lock has an exclusive owner, return that in *ownerp. 651 */ 652int 653lockmgr_chain(struct thread *td, struct thread **ownerp) 654{ 655 struct lock *lkp; 656 657 lkp = td->td_wchan; 658 659 /* Simple test to see if wchan points to a lockmgr lock. */ 660 if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr && 661 lkp->lk_wmesg == td->td_wmesg) 662 goto ok; 663 664 /* 665 * If this thread is doing a DRAIN, then it would be asleep on 666 * &lkp->lk_flags rather than lkp. 667 */ 668 lkp = (struct lock *)((char *)td->td_wchan - 669 offsetof(struct lock, lk_flags)); 670 if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr && 671 lkp->lk_wmesg == td->td_wmesg && (lkp->lk_flags & LK_WAITDRAIN)) 672 goto ok; 673 674 /* Doen't seem to be a lockmgr lock. */ 675 return (0); 676 677ok: 678 /* Ok, we think we have a lockmgr lock, so output some details. */ 679 db_printf("blocked on lk \"%s\" ", lkp->lk_wmesg); 680 if (lkp->lk_sharecount) { 681 db_printf("SHARED (count %d)\n", lkp->lk_sharecount); 682 *ownerp = NULL; 683 } else { 684 db_printf("EXCL (count %d)\n", lkp->lk_exclusivecount); 685 *ownerp = lkp->lk_lockholder; 686 } 687 return (1); 688} 689 690void 691db_show_lockmgr(struct lock_object *lock) 692{ 693 struct thread *td; 694 struct lock *lkp; 695 696 lkp = (struct lock *)lock; 697 698 db_printf(" lock type: %s\n", lkp->lk_wmesg); 699 db_printf(" state: "); 700 if (lkp->lk_sharecount) 701 db_printf("SHARED (count %d)\n", lkp->lk_sharecount); 702 else if (lkp->lk_flags & LK_HAVE_EXCL) { 703 td = lkp->lk_lockholder; 704 db_printf("EXCL (count %d) %p ", lkp->lk_exclusivecount, td); 705 db_printf("(tid %d, pid %d, \"%s\")\n", td->td_tid, 706 td->td_proc->p_pid, td->td_name); 707 } else 708 db_printf("UNLOCKED\n"); 709 if (lkp->lk_waitcount > 0) 710 db_printf(" waiters: %d\n", lkp->lk_waitcount); 711} 712#endif 713