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$"); 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 63 static void assert_lockmgr(struct lock_object *lock, int what); 64 #ifdef DDB 65 #include <ddb/ddb.h> 66 static void db_show_lockmgr(struct lock_object *lock); 67 #endif 68 static void lock_lockmgr(struct lock_object *lock, int how); 69 static int unlock_lockmgr(struct lock_object *lock); 70 71 struct 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 87 void 88 assert_lockmgr(struct lock_object *lock, int what) 89 { 90 91 panic("lockmgr locks do not support assertions"); 92 } 93 94 void 95 lock_lockmgr(struct lock_object *lock, int how) 96 { 97 98 panic("lockmgr locks do not support sleep interlocking"); 99 } 100 101 int 102 unlock_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 112 static int acquire(struct lock **lkpp, int extflags, int wanted, int *contested, uint64_t *waittime); 113 static int acquiredrain(struct lock *lkp, int extflags) ; 114 115 static __inline void 116 sharelock(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 122 static __inline void 123 shareunlock(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 139 static int 140 acquire(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 */ 191 int 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 476 static int 477 acquiredrain(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 */ 500 void 501 lockinit(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 */ 530 void 531 lockdestroy(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 */ 543 void 544 lockmgr_disown(struct lock *lkp) 545 { 546 struct thread *td; 547 548 td = curthread; 549 KASSERT(panicstr != NULL || lkp->lk_exclusivecount, 550 ("%s: %p lockmgr must be exclusively locked", __func__, lkp)); 551 KASSERT(panicstr != NULL || lkp->lk_lockholder == td || 552 lkp->lk_lockholder == LK_KERNPROC, 553 ("%s: %p lockmgr must be locked by curthread (%p)", __func__, lkp, 554 td)); 555 556 /* 557 * Drop the lock reference and switch the owner. This will result 558 * in an atomic operation like td_lock is only accessed by curthread 559 * and lk_lockholder only needs one write. Note also that the lock 560 * owner can be alredy KERNPROC, so in that case just skip the 561 * decrement. 562 */ 563 if (lkp->lk_lockholder == td) 564 td->td_locks--; 565 lkp->lk_lockholder = LK_KERNPROC; 566 } 567 568 /* 569 * Determine the status of a lock. 570 */ 571 int 572 lockstatus(lkp, td) 573 struct lock *lkp; 574 struct thread *td; 575 { 576 int lock_type = 0; 577 int interlocked; 578 579 if (!kdb_active) { 580 interlocked = 1; 581 mtx_lock(lkp->lk_interlock); 582 } else 583 interlocked = 0; 584 if (lkp->lk_exclusivecount != 0) { 585 if (td == NULL || lkp->lk_lockholder == td) 586 lock_type = LK_EXCLUSIVE; 587 else 588 lock_type = LK_EXCLOTHER; 589 } else if (lkp->lk_sharecount != 0) 590 lock_type = LK_SHARED; 591 if (interlocked) 592 mtx_unlock(lkp->lk_interlock); 593 return (lock_type); 594 } 595 596 /* 597 * Determine the number of holders of a lock. 598 */ 599 int 600 lockcount(lkp) 601 struct lock *lkp; 602 { 603 int count; 604 605 mtx_lock(lkp->lk_interlock); 606 count = lkp->lk_exclusivecount + lkp->lk_sharecount; 607 mtx_unlock(lkp->lk_interlock); 608 return (count); 609 } 610 611 /* 612 * Determine the number of waiters on a lock. 613 */ 614 int 615 lockwaiters(lkp) 616 struct lock *lkp; 617 { 618 int count; 619 620 mtx_lock(lkp->lk_interlock); 621 count = lkp->lk_waitcount; 622 mtx_unlock(lkp->lk_interlock); 623 return (count); 624 } 625 626 /* 627 * Print out information about state of a lock. Used by VOP_PRINT 628 * routines to display status about contained locks. 629 */ 630 void 631 lockmgr_printinfo(lkp) 632 struct lock *lkp; 633 { 634 635 if (lkp->lk_sharecount) 636 printf(" lock type %s: SHARED (count %d)", lkp->lk_wmesg, 637 lkp->lk_sharecount); 638 else if (lkp->lk_flags & LK_HAVE_EXCL) 639 printf(" lock type %s: EXCL (count %d) by thread %p (pid %d)", 640 lkp->lk_wmesg, lkp->lk_exclusivecount, 641 lkp->lk_lockholder, lkp->lk_lockholder->td_proc->p_pid); 642 if (lkp->lk_waitcount > 0) 643 printf(" with %d pending", lkp->lk_waitcount); 644 #ifdef DEBUG_LOCKS 645 stack_print_ddb(&lkp->lk_stack); 646 #endif 647 } 648 649 #ifdef DDB 650 /* 651 * Check to see if a thread that is blocked on a sleep queue is actually 652 * blocked on a 'struct lock'. If so, output some details and return true. 653 * If the lock has an exclusive owner, return that in *ownerp. 654 */ 655 int 656 lockmgr_chain(struct thread *td, struct thread **ownerp) 657 { 658 struct lock *lkp; 659 660 lkp = td->td_wchan; 661 662 /* Simple test to see if wchan points to a lockmgr lock. */ 663 if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr && 664 lkp->lk_wmesg == td->td_wmesg) 665 goto ok; 666 667 /* 668 * If this thread is doing a DRAIN, then it would be asleep on 669 * &lkp->lk_flags rather than lkp. 670 */ 671 lkp = (struct lock *)((char *)td->td_wchan - 672 offsetof(struct lock, lk_flags)); 673 if (LOCK_CLASS(&lkp->lk_object) == &lock_class_lockmgr && 674 lkp->lk_wmesg == td->td_wmesg && (lkp->lk_flags & LK_WAITDRAIN)) 675 goto ok; 676 677 /* Doen't seem to be a lockmgr lock. */ 678 return (0); 679 680 ok: 681 /* Ok, we think we have a lockmgr lock, so output some details. */ 682 db_printf("blocked on lk \"%s\" ", lkp->lk_wmesg); 683 if (lkp->lk_sharecount) { 684 db_printf("SHARED (count %d)\n", lkp->lk_sharecount); 685 *ownerp = NULL; 686 } else { 687 db_printf("EXCL (count %d)\n", lkp->lk_exclusivecount); 688 *ownerp = lkp->lk_lockholder; 689 } 690 return (1); 691 } 692 693 void 694 db_show_lockmgr(struct lock_object *lock) 695 { 696 struct thread *td; 697 struct lock *lkp; 698 699 lkp = (struct lock *)lock; 700 701 db_printf(" lock type: %s\n", lkp->lk_wmesg); 702 db_printf(" state: "); 703 if (lkp->lk_sharecount) 704 db_printf("SHARED (count %d)\n", lkp->lk_sharecount); 705 else if (lkp->lk_flags & LK_HAVE_EXCL) { 706 td = lkp->lk_lockholder; 707 db_printf("EXCL (count %d) %p ", lkp->lk_exclusivecount, td); 708 db_printf("(tid %d, pid %d, \"%s\")\n", td->td_tid, 709 td->td_proc->p_pid, td->td_name); 710 } else 711 db_printf("UNLOCKED\n"); 712 if (lkp->lk_waitcount > 0) 713 db_printf(" waiters: %d\n", lkp->lk_waitcount); 714 } 715 #endif 716