1 /*- 2 * Copyright (c) 2008 Attilio Rao <attilio@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice(s), this list of conditions and the following disclaimer as 10 * the first lines of this file unmodified other than the possible 11 * addition of one or more copyright notices. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice(s), this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 26 * DAMAGE. 27 */ 28 29 #include "opt_adaptive_lockmgrs.h" 30 #include "opt_ddb.h" 31 #include "opt_hwpmc_hooks.h" 32 #include "opt_kdtrace.h" 33 34 #include <sys/cdefs.h> 35 __FBSDID("$FreeBSD$"); 36 37 #include <sys/param.h> 38 #include <sys/ktr.h> 39 #include <sys/lock.h> 40 #include <sys/lock_profile.h> 41 #include <sys/lockmgr.h> 42 #include <sys/mutex.h> 43 #include <sys/proc.h> 44 #include <sys/sleepqueue.h> 45 #ifdef DEBUG_LOCKS 46 #include <sys/stack.h> 47 #endif 48 #include <sys/sysctl.h> 49 #include <sys/systm.h> 50 51 #include <machine/cpu.h> 52 53 #ifdef DDB 54 #include <ddb/ddb.h> 55 #endif 56 57 #ifdef HWPMC_HOOKS 58 #include <sys/pmckern.h> 59 PMC_SOFT_DECLARE( , , lock, failed); 60 #endif 61 62 CTASSERT(((LK_ADAPTIVE | LK_NOSHARE) & LO_CLASSFLAGS) == 63 (LK_ADAPTIVE | LK_NOSHARE)); 64 CTASSERT(LK_UNLOCKED == (LK_UNLOCKED & 65 ~(LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS))); 66 67 #define SQ_EXCLUSIVE_QUEUE 0 68 #define SQ_SHARED_QUEUE 1 69 70 #ifndef INVARIANTS 71 #define _lockmgr_assert(lk, what, file, line) 72 #define TD_LOCKS_INC(td) 73 #define TD_LOCKS_DEC(td) 74 #else 75 #define TD_LOCKS_INC(td) ((td)->td_locks++) 76 #define TD_LOCKS_DEC(td) ((td)->td_locks--) 77 #endif 78 #define TD_SLOCKS_INC(td) ((td)->td_lk_slocks++) 79 #define TD_SLOCKS_DEC(td) ((td)->td_lk_slocks--) 80 81 #ifndef DEBUG_LOCKS 82 #define STACK_PRINT(lk) 83 #define STACK_SAVE(lk) 84 #define STACK_ZERO(lk) 85 #else 86 #define STACK_PRINT(lk) stack_print_ddb(&(lk)->lk_stack) 87 #define STACK_SAVE(lk) stack_save(&(lk)->lk_stack) 88 #define STACK_ZERO(lk) stack_zero(&(lk)->lk_stack) 89 #endif 90 91 #define LOCK_LOG2(lk, string, arg1, arg2) \ 92 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \ 93 CTR2(KTR_LOCK, (string), (arg1), (arg2)) 94 #define LOCK_LOG3(lk, string, arg1, arg2, arg3) \ 95 if (LOCK_LOG_TEST(&(lk)->lock_object, 0)) \ 96 CTR3(KTR_LOCK, (string), (arg1), (arg2), (arg3)) 97 98 #define GIANT_DECLARE \ 99 int _i = 0; \ 100 WITNESS_SAVE_DECL(Giant) 101 #define GIANT_RESTORE() do { \ 102 if (_i > 0) { \ 103 while (_i--) \ 104 mtx_lock(&Giant); \ 105 WITNESS_RESTORE(&Giant.lock_object, Giant); \ 106 } \ 107 } while (0) 108 #define GIANT_SAVE() do { \ 109 if (mtx_owned(&Giant)) { \ 110 WITNESS_SAVE(&Giant.lock_object, Giant); \ 111 while (mtx_owned(&Giant)) { \ 112 _i++; \ 113 mtx_unlock(&Giant); \ 114 } \ 115 } \ 116 } while (0) 117 118 #define LK_CAN_SHARE(x) \ 119 (((x) & LK_SHARE) && (((x) & LK_EXCLUSIVE_WAITERS) == 0 || \ 120 ((x) & LK_EXCLUSIVE_SPINNERS) == 0 || \ 121 curthread->td_lk_slocks || (curthread->td_pflags & TDP_DEADLKTREAT))) 122 #define LK_TRYOP(x) \ 123 ((x) & LK_NOWAIT) 124 125 #define LK_CAN_WITNESS(x) \ 126 (((x) & LK_NOWITNESS) == 0 && !LK_TRYOP(x)) 127 #define LK_TRYWIT(x) \ 128 (LK_TRYOP(x) ? LOP_TRYLOCK : 0) 129 130 #define LK_CAN_ADAPT(lk, f) \ 131 (((lk)->lock_object.lo_flags & LK_ADAPTIVE) != 0 && \ 132 ((f) & LK_SLEEPFAIL) == 0) 133 134 #define lockmgr_disowned(lk) \ 135 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == LK_KERNPROC) 136 137 #define lockmgr_xlocked(lk) \ 138 (((lk)->lk_lock & ~(LK_FLAGMASK & ~LK_SHARE)) == (uintptr_t)curthread) 139 140 static void assert_lockmgr(const struct lock_object *lock, int how); 141 #ifdef DDB 142 static void db_show_lockmgr(const struct lock_object *lock); 143 #endif 144 static void lock_lockmgr(struct lock_object *lock, int how); 145 #ifdef KDTRACE_HOOKS 146 static int owner_lockmgr(const struct lock_object *lock, 147 struct thread **owner); 148 #endif 149 static int unlock_lockmgr(struct lock_object *lock); 150 151 struct lock_class lock_class_lockmgr = { 152 .lc_name = "lockmgr", 153 .lc_flags = LC_RECURSABLE | LC_SLEEPABLE | LC_SLEEPLOCK | LC_UPGRADABLE, 154 .lc_assert = assert_lockmgr, 155 #ifdef DDB 156 .lc_ddb_show = db_show_lockmgr, 157 #endif 158 .lc_lock = lock_lockmgr, 159 .lc_unlock = unlock_lockmgr, 160 #ifdef KDTRACE_HOOKS 161 .lc_owner = owner_lockmgr, 162 #endif 163 }; 164 165 #ifdef ADAPTIVE_LOCKMGRS 166 static u_int alk_retries = 10; 167 static u_int alk_loops = 10000; 168 static SYSCTL_NODE(_debug, OID_AUTO, lockmgr, CTLFLAG_RD, NULL, 169 "lockmgr debugging"); 170 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, retries, CTLFLAG_RW, &alk_retries, 0, ""); 171 SYSCTL_UINT(_debug_lockmgr, OID_AUTO, loops, CTLFLAG_RW, &alk_loops, 0, ""); 172 #endif 173 174 static __inline struct thread * 175 lockmgr_xholder(const struct lock *lk) 176 { 177 uintptr_t x; 178 179 x = lk->lk_lock; 180 return ((x & LK_SHARE) ? NULL : (struct thread *)LK_HOLDER(x)); 181 } 182 183 /* 184 * It assumes sleepq_lock held and returns with this one unheld. 185 * It also assumes the generic interlock is sane and previously checked. 186 * If LK_INTERLOCK is specified the interlock is not reacquired after the 187 * sleep. 188 */ 189 static __inline int 190 sleeplk(struct lock *lk, u_int flags, struct lock_object *ilk, 191 const char *wmesg, int pri, int timo, int queue) 192 { 193 GIANT_DECLARE; 194 struct lock_class *class; 195 int catch, error; 196 197 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL; 198 catch = pri & PCATCH; 199 pri &= PRIMASK; 200 error = 0; 201 202 LOCK_LOG3(lk, "%s: %p blocking on the %s sleepqueue", __func__, lk, 203 (queue == SQ_EXCLUSIVE_QUEUE) ? "exclusive" : "shared"); 204 205 if (flags & LK_INTERLOCK) 206 class->lc_unlock(ilk); 207 if (queue == SQ_EXCLUSIVE_QUEUE && (flags & LK_SLEEPFAIL) != 0) 208 lk->lk_exslpfail++; 209 GIANT_SAVE(); 210 sleepq_add(&lk->lock_object, NULL, wmesg, SLEEPQ_LK | (catch ? 211 SLEEPQ_INTERRUPTIBLE : 0), queue); 212 if ((flags & LK_TIMELOCK) && timo) 213 sleepq_set_timeout(&lk->lock_object, timo); 214 215 /* 216 * Decisional switch for real sleeping. 217 */ 218 if ((flags & LK_TIMELOCK) && timo && catch) 219 error = sleepq_timedwait_sig(&lk->lock_object, pri); 220 else if ((flags & LK_TIMELOCK) && timo) 221 error = sleepq_timedwait(&lk->lock_object, pri); 222 else if (catch) 223 error = sleepq_wait_sig(&lk->lock_object, pri); 224 else 225 sleepq_wait(&lk->lock_object, pri); 226 GIANT_RESTORE(); 227 if ((flags & LK_SLEEPFAIL) && error == 0) 228 error = ENOLCK; 229 230 return (error); 231 } 232 233 static __inline int 234 wakeupshlk(struct lock *lk, const char *file, int line) 235 { 236 uintptr_t v, x; 237 u_int realexslp; 238 int queue, wakeup_swapper; 239 240 TD_LOCKS_DEC(curthread); 241 TD_SLOCKS_DEC(curthread); 242 WITNESS_UNLOCK(&lk->lock_object, 0, file, line); 243 LOCK_LOG_LOCK("SUNLOCK", &lk->lock_object, 0, 0, file, line); 244 245 wakeup_swapper = 0; 246 for (;;) { 247 x = lk->lk_lock; 248 249 /* 250 * If there is more than one shared lock held, just drop one 251 * and return. 252 */ 253 if (LK_SHARERS(x) > 1) { 254 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, 255 x - LK_ONE_SHARER)) 256 break; 257 continue; 258 } 259 260 /* 261 * If there are not waiters on the exclusive queue, drop the 262 * lock quickly. 263 */ 264 if ((x & LK_ALL_WAITERS) == 0) { 265 MPASS((x & ~LK_EXCLUSIVE_SPINNERS) == 266 LK_SHARERS_LOCK(1)); 267 if (atomic_cmpset_rel_ptr(&lk->lk_lock, x, LK_UNLOCKED)) 268 break; 269 continue; 270 } 271 272 /* 273 * We should have a sharer with waiters, so enter the hard 274 * path in order to handle wakeups correctly. 275 */ 276 sleepq_lock(&lk->lock_object); 277 x = lk->lk_lock & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS); 278 v = LK_UNLOCKED; 279 280 /* 281 * If the lock has exclusive waiters, give them preference in 282 * order to avoid deadlock with shared runners up. 283 * If interruptible sleeps left the exclusive queue empty 284 * avoid a starvation for the threads sleeping on the shared 285 * queue by giving them precedence and cleaning up the 286 * exclusive waiters bit anyway. 287 * Please note that lk_exslpfail count may be lying about 288 * the real number of waiters with the LK_SLEEPFAIL flag on 289 * because they may be used in conjuction with interruptible 290 * sleeps so lk_exslpfail might be considered an 'upper limit' 291 * bound, including the edge cases. 292 */ 293 realexslp = sleepq_sleepcnt(&lk->lock_object, 294 SQ_EXCLUSIVE_QUEUE); 295 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) { 296 if (lk->lk_exslpfail < realexslp) { 297 lk->lk_exslpfail = 0; 298 queue = SQ_EXCLUSIVE_QUEUE; 299 v |= (x & LK_SHARED_WAITERS); 300 } else { 301 lk->lk_exslpfail = 0; 302 LOCK_LOG2(lk, 303 "%s: %p has only LK_SLEEPFAIL sleepers", 304 __func__, lk); 305 LOCK_LOG2(lk, 306 "%s: %p waking up threads on the exclusive queue", 307 __func__, lk); 308 wakeup_swapper = 309 sleepq_broadcast(&lk->lock_object, 310 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE); 311 queue = SQ_SHARED_QUEUE; 312 } 313 314 } else { 315 316 /* 317 * Exclusive waiters sleeping with LK_SLEEPFAIL on 318 * and using interruptible sleeps/timeout may have 319 * left spourious lk_exslpfail counts on, so clean 320 * it up anyway. 321 */ 322 lk->lk_exslpfail = 0; 323 queue = SQ_SHARED_QUEUE; 324 } 325 326 if (!atomic_cmpset_rel_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x, 327 v)) { 328 sleepq_release(&lk->lock_object); 329 continue; 330 } 331 LOCK_LOG3(lk, "%s: %p waking up threads on the %s queue", 332 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" : 333 "exclusive"); 334 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, SLEEPQ_LK, 335 0, queue); 336 sleepq_release(&lk->lock_object); 337 break; 338 } 339 340 lock_profile_release_lock(&lk->lock_object); 341 return (wakeup_swapper); 342 } 343 344 static void 345 assert_lockmgr(const struct lock_object *lock, int what) 346 { 347 348 panic("lockmgr locks do not support assertions"); 349 } 350 351 static void 352 lock_lockmgr(struct lock_object *lock, int how) 353 { 354 355 panic("lockmgr locks do not support sleep interlocking"); 356 } 357 358 static int 359 unlock_lockmgr(struct lock_object *lock) 360 { 361 362 panic("lockmgr locks do not support sleep interlocking"); 363 } 364 365 #ifdef KDTRACE_HOOKS 366 static int 367 owner_lockmgr(const struct lock_object *lock, struct thread **owner) 368 { 369 370 panic("lockmgr locks do not support owner inquiring"); 371 } 372 #endif 373 374 void 375 lockinit(struct lock *lk, int pri, const char *wmesg, int timo, int flags) 376 { 377 int iflags; 378 379 MPASS((flags & ~LK_INIT_MASK) == 0); 380 ASSERT_ATOMIC_LOAD_PTR(lk->lk_lock, 381 ("%s: lockmgr not aligned for %s: %p", __func__, wmesg, 382 &lk->lk_lock)); 383 384 iflags = LO_SLEEPABLE | LO_UPGRADABLE; 385 if (flags & LK_CANRECURSE) 386 iflags |= LO_RECURSABLE; 387 if ((flags & LK_NODUP) == 0) 388 iflags |= LO_DUPOK; 389 if (flags & LK_NOPROFILE) 390 iflags |= LO_NOPROFILE; 391 if ((flags & LK_NOWITNESS) == 0) 392 iflags |= LO_WITNESS; 393 if (flags & LK_QUIET) 394 iflags |= LO_QUIET; 395 iflags |= flags & (LK_ADAPTIVE | LK_NOSHARE); 396 397 lk->lk_lock = LK_UNLOCKED; 398 lk->lk_recurse = 0; 399 lk->lk_exslpfail = 0; 400 lk->lk_timo = timo; 401 lk->lk_pri = pri; 402 lock_init(&lk->lock_object, &lock_class_lockmgr, wmesg, NULL, iflags); 403 STACK_ZERO(lk); 404 } 405 406 /* 407 * XXX: Gross hacks to manipulate external lock flags after 408 * initialization. Used for certain vnode and buf locks. 409 */ 410 void 411 lockallowshare(struct lock *lk) 412 { 413 414 lockmgr_assert(lk, KA_XLOCKED); 415 lk->lock_object.lo_flags &= ~LK_NOSHARE; 416 } 417 418 void 419 lockallowrecurse(struct lock *lk) 420 { 421 422 lockmgr_assert(lk, KA_XLOCKED); 423 lk->lock_object.lo_flags |= LO_RECURSABLE; 424 } 425 426 void 427 lockdisablerecurse(struct lock *lk) 428 { 429 430 lockmgr_assert(lk, KA_XLOCKED); 431 lk->lock_object.lo_flags &= ~LO_RECURSABLE; 432 } 433 434 void 435 lockdestroy(struct lock *lk) 436 { 437 438 KASSERT(lk->lk_lock == LK_UNLOCKED, ("lockmgr still held")); 439 KASSERT(lk->lk_recurse == 0, ("lockmgr still recursed")); 440 KASSERT(lk->lk_exslpfail == 0, ("lockmgr still exclusive waiters")); 441 lock_destroy(&lk->lock_object); 442 } 443 444 int 445 __lockmgr_args(struct lock *lk, u_int flags, struct lock_object *ilk, 446 const char *wmesg, int pri, int timo, const char *file, int line) 447 { 448 GIANT_DECLARE; 449 struct lock_class *class; 450 const char *iwmesg; 451 uintptr_t tid, v, x; 452 u_int op, realexslp; 453 int error, ipri, itimo, queue, wakeup_swapper; 454 #ifdef LOCK_PROFILING 455 uint64_t waittime = 0; 456 int contested = 0; 457 #endif 458 #ifdef ADAPTIVE_LOCKMGRS 459 volatile struct thread *owner; 460 u_int i, spintries = 0; 461 #endif 462 463 error = 0; 464 tid = (uintptr_t)curthread; 465 op = (flags & LK_TYPE_MASK); 466 iwmesg = (wmesg == LK_WMESG_DEFAULT) ? lk->lock_object.lo_name : wmesg; 467 ipri = (pri == LK_PRIO_DEFAULT) ? lk->lk_pri : pri; 468 itimo = (timo == LK_TIMO_DEFAULT) ? lk->lk_timo : timo; 469 470 MPASS((flags & ~LK_TOTAL_MASK) == 0); 471 KASSERT((op & (op - 1)) == 0, 472 ("%s: Invalid requested operation @ %s:%d", __func__, file, line)); 473 KASSERT((flags & (LK_NOWAIT | LK_SLEEPFAIL)) == 0 || 474 (op != LK_DOWNGRADE && op != LK_RELEASE), 475 ("%s: Invalid flags in regard of the operation desired @ %s:%d", 476 __func__, file, line)); 477 KASSERT((flags & LK_INTERLOCK) == 0 || ilk != NULL, 478 ("%s: LK_INTERLOCK passed without valid interlock @ %s:%d", 479 __func__, file, line)); 480 KASSERT(!TD_IS_IDLETHREAD(curthread), 481 ("%s: idle thread %p on lockmgr %s @ %s:%d", __func__, curthread, 482 lk->lock_object.lo_name, file, line)); 483 484 class = (flags & LK_INTERLOCK) ? LOCK_CLASS(ilk) : NULL; 485 if (panicstr != NULL) { 486 if (flags & LK_INTERLOCK) 487 class->lc_unlock(ilk); 488 return (0); 489 } 490 491 if (lk->lock_object.lo_flags & LK_NOSHARE) { 492 switch (op) { 493 case LK_SHARED: 494 op = LK_EXCLUSIVE; 495 break; 496 case LK_UPGRADE: 497 case LK_DOWNGRADE: 498 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, 499 file, line); 500 return (0); 501 } 502 } 503 504 wakeup_swapper = 0; 505 switch (op) { 506 case LK_SHARED: 507 if (LK_CAN_WITNESS(flags)) 508 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER, 509 file, line, ilk); 510 for (;;) { 511 x = lk->lk_lock; 512 513 /* 514 * If no other thread has an exclusive lock, or 515 * no exclusive waiter is present, bump the count of 516 * sharers. Since we have to preserve the state of 517 * waiters, if we fail to acquire the shared lock 518 * loop back and retry. 519 */ 520 if (LK_CAN_SHARE(x)) { 521 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x, 522 x + LK_ONE_SHARER)) 523 break; 524 continue; 525 } 526 #ifdef HWPMC_HOOKS 527 PMC_SOFT_CALL( , , lock, failed); 528 #endif 529 lock_profile_obtain_lock_failed(&lk->lock_object, 530 &contested, &waittime); 531 532 /* 533 * If the lock is already held by curthread in 534 * exclusive way avoid a deadlock. 535 */ 536 if (LK_HOLDER(x) == tid) { 537 LOCK_LOG2(lk, 538 "%s: %p already held in exclusive mode", 539 __func__, lk); 540 error = EDEADLK; 541 break; 542 } 543 544 /* 545 * If the lock is expected to not sleep just give up 546 * and return. 547 */ 548 if (LK_TRYOP(flags)) { 549 LOCK_LOG2(lk, "%s: %p fails the try operation", 550 __func__, lk); 551 error = EBUSY; 552 break; 553 } 554 555 #ifdef ADAPTIVE_LOCKMGRS 556 /* 557 * If the owner is running on another CPU, spin until 558 * the owner stops running or the state of the lock 559 * changes. We need a double-state handle here 560 * because for a failed acquisition the lock can be 561 * either held in exclusive mode or shared mode 562 * (for the writer starvation avoidance technique). 563 */ 564 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 && 565 LK_HOLDER(x) != LK_KERNPROC) { 566 owner = (struct thread *)LK_HOLDER(x); 567 if (LOCK_LOG_TEST(&lk->lock_object, 0)) 568 CTR3(KTR_LOCK, 569 "%s: spinning on %p held by %p", 570 __func__, lk, owner); 571 572 /* 573 * If we are holding also an interlock drop it 574 * in order to avoid a deadlock if the lockmgr 575 * owner is adaptively spinning on the 576 * interlock itself. 577 */ 578 if (flags & LK_INTERLOCK) { 579 class->lc_unlock(ilk); 580 flags &= ~LK_INTERLOCK; 581 } 582 GIANT_SAVE(); 583 while (LK_HOLDER(lk->lk_lock) == 584 (uintptr_t)owner && TD_IS_RUNNING(owner)) 585 cpu_spinwait(); 586 GIANT_RESTORE(); 587 continue; 588 } else if (LK_CAN_ADAPT(lk, flags) && 589 (x & LK_SHARE) != 0 && LK_SHARERS(x) && 590 spintries < alk_retries) { 591 if (flags & LK_INTERLOCK) { 592 class->lc_unlock(ilk); 593 flags &= ~LK_INTERLOCK; 594 } 595 GIANT_SAVE(); 596 spintries++; 597 for (i = 0; i < alk_loops; i++) { 598 if (LOCK_LOG_TEST(&lk->lock_object, 0)) 599 CTR4(KTR_LOCK, 600 "%s: shared spinning on %p with %u and %u", 601 __func__, lk, spintries, i); 602 x = lk->lk_lock; 603 if ((x & LK_SHARE) == 0 || 604 LK_CAN_SHARE(x) != 0) 605 break; 606 cpu_spinwait(); 607 } 608 GIANT_RESTORE(); 609 if (i != alk_loops) 610 continue; 611 } 612 #endif 613 614 /* 615 * Acquire the sleepqueue chain lock because we 616 * probabilly will need to manipulate waiters flags. 617 */ 618 sleepq_lock(&lk->lock_object); 619 x = lk->lk_lock; 620 621 /* 622 * if the lock can be acquired in shared mode, try 623 * again. 624 */ 625 if (LK_CAN_SHARE(x)) { 626 sleepq_release(&lk->lock_object); 627 continue; 628 } 629 630 #ifdef ADAPTIVE_LOCKMGRS 631 /* 632 * The current lock owner might have started executing 633 * on another CPU (or the lock could have changed 634 * owner) while we were waiting on the turnstile 635 * chain lock. If so, drop the turnstile lock and try 636 * again. 637 */ 638 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 && 639 LK_HOLDER(x) != LK_KERNPROC) { 640 owner = (struct thread *)LK_HOLDER(x); 641 if (TD_IS_RUNNING(owner)) { 642 sleepq_release(&lk->lock_object); 643 continue; 644 } 645 } 646 #endif 647 648 /* 649 * Try to set the LK_SHARED_WAITERS flag. If we fail, 650 * loop back and retry. 651 */ 652 if ((x & LK_SHARED_WAITERS) == 0) { 653 if (!atomic_cmpset_acq_ptr(&lk->lk_lock, x, 654 x | LK_SHARED_WAITERS)) { 655 sleepq_release(&lk->lock_object); 656 continue; 657 } 658 LOCK_LOG2(lk, "%s: %p set shared waiters flag", 659 __func__, lk); 660 } 661 662 /* 663 * As far as we have been unable to acquire the 664 * shared lock and the shared waiters flag is set, 665 * we will sleep. 666 */ 667 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo, 668 SQ_SHARED_QUEUE); 669 flags &= ~LK_INTERLOCK; 670 if (error) { 671 LOCK_LOG3(lk, 672 "%s: interrupted sleep for %p with %d", 673 __func__, lk, error); 674 break; 675 } 676 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue", 677 __func__, lk); 678 } 679 if (error == 0) { 680 lock_profile_obtain_lock_success(&lk->lock_object, 681 contested, waittime, file, line); 682 LOCK_LOG_LOCK("SLOCK", &lk->lock_object, 0, 0, file, 683 line); 684 WITNESS_LOCK(&lk->lock_object, LK_TRYWIT(flags), file, 685 line); 686 TD_LOCKS_INC(curthread); 687 TD_SLOCKS_INC(curthread); 688 STACK_SAVE(lk); 689 } 690 break; 691 case LK_UPGRADE: 692 _lockmgr_assert(lk, KA_SLOCKED, file, line); 693 v = lk->lk_lock; 694 x = v & LK_ALL_WAITERS; 695 v &= LK_EXCLUSIVE_SPINNERS; 696 697 /* 698 * Try to switch from one shared lock to an exclusive one. 699 * We need to preserve waiters flags during the operation. 700 */ 701 if (atomic_cmpset_ptr(&lk->lk_lock, LK_SHARERS_LOCK(1) | x | v, 702 tid | x)) { 703 LOCK_LOG_LOCK("XUPGRADE", &lk->lock_object, 0, 0, file, 704 line); 705 WITNESS_UPGRADE(&lk->lock_object, LOP_EXCLUSIVE | 706 LK_TRYWIT(flags), file, line); 707 TD_SLOCKS_DEC(curthread); 708 break; 709 } 710 711 /* 712 * We have been unable to succeed in upgrading, so just 713 * give up the shared lock. 714 */ 715 wakeup_swapper |= wakeupshlk(lk, file, line); 716 717 /* FALLTHROUGH */ 718 case LK_EXCLUSIVE: 719 if (LK_CAN_WITNESS(flags)) 720 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER | 721 LOP_EXCLUSIVE, file, line, ilk); 722 723 /* 724 * If curthread already holds the lock and this one is 725 * allowed to recurse, simply recurse on it. 726 */ 727 if (lockmgr_xlocked(lk)) { 728 if ((flags & LK_CANRECURSE) == 0 && 729 (lk->lock_object.lo_flags & LO_RECURSABLE) == 0) { 730 731 /* 732 * If the lock is expected to not panic just 733 * give up and return. 734 */ 735 if (LK_TRYOP(flags)) { 736 LOCK_LOG2(lk, 737 "%s: %p fails the try operation", 738 __func__, lk); 739 error = EBUSY; 740 break; 741 } 742 if (flags & LK_INTERLOCK) 743 class->lc_unlock(ilk); 744 panic("%s: recursing on non recursive lockmgr %s @ %s:%d\n", 745 __func__, iwmesg, file, line); 746 } 747 lk->lk_recurse++; 748 LOCK_LOG2(lk, "%s: %p recursing", __func__, lk); 749 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0, 750 lk->lk_recurse, file, line); 751 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE | 752 LK_TRYWIT(flags), file, line); 753 TD_LOCKS_INC(curthread); 754 break; 755 } 756 757 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, 758 tid)) { 759 #ifdef HWPMC_HOOKS 760 PMC_SOFT_CALL( , , lock, failed); 761 #endif 762 lock_profile_obtain_lock_failed(&lk->lock_object, 763 &contested, &waittime); 764 765 /* 766 * If the lock is expected to not sleep just give up 767 * and return. 768 */ 769 if (LK_TRYOP(flags)) { 770 LOCK_LOG2(lk, "%s: %p fails the try operation", 771 __func__, lk); 772 error = EBUSY; 773 break; 774 } 775 776 #ifdef ADAPTIVE_LOCKMGRS 777 /* 778 * If the owner is running on another CPU, spin until 779 * the owner stops running or the state of the lock 780 * changes. 781 */ 782 x = lk->lk_lock; 783 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 && 784 LK_HOLDER(x) != LK_KERNPROC) { 785 owner = (struct thread *)LK_HOLDER(x); 786 if (LOCK_LOG_TEST(&lk->lock_object, 0)) 787 CTR3(KTR_LOCK, 788 "%s: spinning on %p held by %p", 789 __func__, lk, owner); 790 791 /* 792 * If we are holding also an interlock drop it 793 * in order to avoid a deadlock if the lockmgr 794 * owner is adaptively spinning on the 795 * interlock itself. 796 */ 797 if (flags & LK_INTERLOCK) { 798 class->lc_unlock(ilk); 799 flags &= ~LK_INTERLOCK; 800 } 801 GIANT_SAVE(); 802 while (LK_HOLDER(lk->lk_lock) == 803 (uintptr_t)owner && TD_IS_RUNNING(owner)) 804 cpu_spinwait(); 805 GIANT_RESTORE(); 806 continue; 807 } else if (LK_CAN_ADAPT(lk, flags) && 808 (x & LK_SHARE) != 0 && LK_SHARERS(x) && 809 spintries < alk_retries) { 810 if ((x & LK_EXCLUSIVE_SPINNERS) == 0 && 811 !atomic_cmpset_ptr(&lk->lk_lock, x, 812 x | LK_EXCLUSIVE_SPINNERS)) 813 continue; 814 if (flags & LK_INTERLOCK) { 815 class->lc_unlock(ilk); 816 flags &= ~LK_INTERLOCK; 817 } 818 GIANT_SAVE(); 819 spintries++; 820 for (i = 0; i < alk_loops; i++) { 821 if (LOCK_LOG_TEST(&lk->lock_object, 0)) 822 CTR4(KTR_LOCK, 823 "%s: shared spinning on %p with %u and %u", 824 __func__, lk, spintries, i); 825 if ((lk->lk_lock & 826 LK_EXCLUSIVE_SPINNERS) == 0) 827 break; 828 cpu_spinwait(); 829 } 830 GIANT_RESTORE(); 831 if (i != alk_loops) 832 continue; 833 } 834 #endif 835 836 /* 837 * Acquire the sleepqueue chain lock because we 838 * probabilly will need to manipulate waiters flags. 839 */ 840 sleepq_lock(&lk->lock_object); 841 x = lk->lk_lock; 842 843 /* 844 * if the lock has been released while we spun on 845 * the sleepqueue chain lock just try again. 846 */ 847 if (x == LK_UNLOCKED) { 848 sleepq_release(&lk->lock_object); 849 continue; 850 } 851 852 #ifdef ADAPTIVE_LOCKMGRS 853 /* 854 * The current lock owner might have started executing 855 * on another CPU (or the lock could have changed 856 * owner) while we were waiting on the turnstile 857 * chain lock. If so, drop the turnstile lock and try 858 * again. 859 */ 860 if (LK_CAN_ADAPT(lk, flags) && (x & LK_SHARE) == 0 && 861 LK_HOLDER(x) != LK_KERNPROC) { 862 owner = (struct thread *)LK_HOLDER(x); 863 if (TD_IS_RUNNING(owner)) { 864 sleepq_release(&lk->lock_object); 865 continue; 866 } 867 } 868 #endif 869 870 /* 871 * The lock can be in the state where there is a 872 * pending queue of waiters, but still no owner. 873 * This happens when the lock is contested and an 874 * owner is going to claim the lock. 875 * If curthread is the one successfully acquiring it 876 * claim lock ownership and return, preserving waiters 877 * flags. 878 */ 879 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS); 880 if ((x & ~v) == LK_UNLOCKED) { 881 v &= ~LK_EXCLUSIVE_SPINNERS; 882 if (atomic_cmpset_acq_ptr(&lk->lk_lock, x, 883 tid | v)) { 884 sleepq_release(&lk->lock_object); 885 LOCK_LOG2(lk, 886 "%s: %p claimed by a new writer", 887 __func__, lk); 888 break; 889 } 890 sleepq_release(&lk->lock_object); 891 continue; 892 } 893 894 /* 895 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we 896 * fail, loop back and retry. 897 */ 898 if ((x & LK_EXCLUSIVE_WAITERS) == 0) { 899 if (!atomic_cmpset_ptr(&lk->lk_lock, x, 900 x | LK_EXCLUSIVE_WAITERS)) { 901 sleepq_release(&lk->lock_object); 902 continue; 903 } 904 LOCK_LOG2(lk, "%s: %p set excl waiters flag", 905 __func__, lk); 906 } 907 908 /* 909 * As far as we have been unable to acquire the 910 * exclusive lock and the exclusive waiters flag 911 * is set, we will sleep. 912 */ 913 error = sleeplk(lk, flags, ilk, iwmesg, ipri, itimo, 914 SQ_EXCLUSIVE_QUEUE); 915 flags &= ~LK_INTERLOCK; 916 if (error) { 917 LOCK_LOG3(lk, 918 "%s: interrupted sleep for %p with %d", 919 __func__, lk, error); 920 break; 921 } 922 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue", 923 __func__, lk); 924 } 925 if (error == 0) { 926 lock_profile_obtain_lock_success(&lk->lock_object, 927 contested, waittime, file, line); 928 LOCK_LOG_LOCK("XLOCK", &lk->lock_object, 0, 929 lk->lk_recurse, file, line); 930 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE | 931 LK_TRYWIT(flags), file, line); 932 TD_LOCKS_INC(curthread); 933 STACK_SAVE(lk); 934 } 935 break; 936 case LK_DOWNGRADE: 937 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line); 938 LOCK_LOG_LOCK("XDOWNGRADE", &lk->lock_object, 0, 0, file, line); 939 WITNESS_DOWNGRADE(&lk->lock_object, 0, file, line); 940 TD_SLOCKS_INC(curthread); 941 942 /* 943 * In order to preserve waiters flags, just spin. 944 */ 945 for (;;) { 946 x = lk->lk_lock; 947 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0); 948 x &= LK_ALL_WAITERS; 949 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x, 950 LK_SHARERS_LOCK(1) | x)) 951 break; 952 cpu_spinwait(); 953 } 954 break; 955 case LK_RELEASE: 956 _lockmgr_assert(lk, KA_LOCKED, file, line); 957 x = lk->lk_lock; 958 959 if ((x & LK_SHARE) == 0) { 960 961 /* 962 * As first option, treact the lock as if it has not 963 * any waiter. 964 * Fix-up the tid var if the lock has been disowned. 965 */ 966 if (LK_HOLDER(x) == LK_KERNPROC) 967 tid = LK_KERNPROC; 968 else { 969 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, 970 file, line); 971 TD_LOCKS_DEC(curthread); 972 } 973 LOCK_LOG_LOCK("XUNLOCK", &lk->lock_object, 0, 974 lk->lk_recurse, file, line); 975 976 /* 977 * The lock is held in exclusive mode. 978 * If the lock is recursed also, then unrecurse it. 979 */ 980 if (lockmgr_xlocked(lk) && lockmgr_recursed(lk)) { 981 LOCK_LOG2(lk, "%s: %p unrecursing", __func__, 982 lk); 983 lk->lk_recurse--; 984 break; 985 } 986 if (tid != LK_KERNPROC) 987 lock_profile_release_lock(&lk->lock_object); 988 989 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid, 990 LK_UNLOCKED)) 991 break; 992 993 sleepq_lock(&lk->lock_object); 994 x = lk->lk_lock; 995 v = LK_UNLOCKED; 996 997 /* 998 * If the lock has exclusive waiters, give them 999 * preference in order to avoid deadlock with 1000 * shared runners up. 1001 * If interruptible sleeps left the exclusive queue 1002 * empty avoid a starvation for the threads sleeping 1003 * on the shared queue by giving them precedence 1004 * and cleaning up the exclusive waiters bit anyway. 1005 * Please note that lk_exslpfail count may be lying 1006 * about the real number of waiters with the 1007 * LK_SLEEPFAIL flag on because they may be used in 1008 * conjuction with interruptible sleeps so 1009 * lk_exslpfail might be considered an 'upper limit' 1010 * bound, including the edge cases. 1011 */ 1012 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0); 1013 realexslp = sleepq_sleepcnt(&lk->lock_object, 1014 SQ_EXCLUSIVE_QUEUE); 1015 if ((x & LK_EXCLUSIVE_WAITERS) != 0 && realexslp != 0) { 1016 if (lk->lk_exslpfail < realexslp) { 1017 lk->lk_exslpfail = 0; 1018 queue = SQ_EXCLUSIVE_QUEUE; 1019 v |= (x & LK_SHARED_WAITERS); 1020 } else { 1021 lk->lk_exslpfail = 0; 1022 LOCK_LOG2(lk, 1023 "%s: %p has only LK_SLEEPFAIL sleepers", 1024 __func__, lk); 1025 LOCK_LOG2(lk, 1026 "%s: %p waking up threads on the exclusive queue", 1027 __func__, lk); 1028 wakeup_swapper = 1029 sleepq_broadcast(&lk->lock_object, 1030 SLEEPQ_LK, 0, SQ_EXCLUSIVE_QUEUE); 1031 queue = SQ_SHARED_QUEUE; 1032 } 1033 } else { 1034 1035 /* 1036 * Exclusive waiters sleeping with LK_SLEEPFAIL 1037 * on and using interruptible sleeps/timeout 1038 * may have left spourious lk_exslpfail counts 1039 * on, so clean it up anyway. 1040 */ 1041 lk->lk_exslpfail = 0; 1042 queue = SQ_SHARED_QUEUE; 1043 } 1044 1045 LOCK_LOG3(lk, 1046 "%s: %p waking up threads on the %s queue", 1047 __func__, lk, queue == SQ_SHARED_QUEUE ? "shared" : 1048 "exclusive"); 1049 atomic_store_rel_ptr(&lk->lk_lock, v); 1050 wakeup_swapper |= sleepq_broadcast(&lk->lock_object, 1051 SLEEPQ_LK, 0, queue); 1052 sleepq_release(&lk->lock_object); 1053 break; 1054 } else 1055 wakeup_swapper = wakeupshlk(lk, file, line); 1056 break; 1057 case LK_DRAIN: 1058 if (LK_CAN_WITNESS(flags)) 1059 WITNESS_CHECKORDER(&lk->lock_object, LOP_NEWORDER | 1060 LOP_EXCLUSIVE, file, line, ilk); 1061 1062 /* 1063 * Trying to drain a lock we already own will result in a 1064 * deadlock. 1065 */ 1066 if (lockmgr_xlocked(lk)) { 1067 if (flags & LK_INTERLOCK) 1068 class->lc_unlock(ilk); 1069 panic("%s: draining %s with the lock held @ %s:%d\n", 1070 __func__, iwmesg, file, line); 1071 } 1072 1073 while (!atomic_cmpset_acq_ptr(&lk->lk_lock, LK_UNLOCKED, tid)) { 1074 #ifdef HWPMC_HOOKS 1075 PMC_SOFT_CALL( , , lock, failed); 1076 #endif 1077 lock_profile_obtain_lock_failed(&lk->lock_object, 1078 &contested, &waittime); 1079 1080 /* 1081 * If the lock is expected to not sleep just give up 1082 * and return. 1083 */ 1084 if (LK_TRYOP(flags)) { 1085 LOCK_LOG2(lk, "%s: %p fails the try operation", 1086 __func__, lk); 1087 error = EBUSY; 1088 break; 1089 } 1090 1091 /* 1092 * Acquire the sleepqueue chain lock because we 1093 * probabilly will need to manipulate waiters flags. 1094 */ 1095 sleepq_lock(&lk->lock_object); 1096 x = lk->lk_lock; 1097 1098 /* 1099 * if the lock has been released while we spun on 1100 * the sleepqueue chain lock just try again. 1101 */ 1102 if (x == LK_UNLOCKED) { 1103 sleepq_release(&lk->lock_object); 1104 continue; 1105 } 1106 1107 v = x & (LK_ALL_WAITERS | LK_EXCLUSIVE_SPINNERS); 1108 if ((x & ~v) == LK_UNLOCKED) { 1109 v = (x & ~LK_EXCLUSIVE_SPINNERS); 1110 1111 /* 1112 * If interruptible sleeps left the exclusive 1113 * queue empty avoid a starvation for the 1114 * threads sleeping on the shared queue by 1115 * giving them precedence and cleaning up the 1116 * exclusive waiters bit anyway. 1117 * Please note that lk_exslpfail count may be 1118 * lying about the real number of waiters with 1119 * the LK_SLEEPFAIL flag on because they may 1120 * be used in conjuction with interruptible 1121 * sleeps so lk_exslpfail might be considered 1122 * an 'upper limit' bound, including the edge 1123 * cases. 1124 */ 1125 if (v & LK_EXCLUSIVE_WAITERS) { 1126 queue = SQ_EXCLUSIVE_QUEUE; 1127 v &= ~LK_EXCLUSIVE_WAITERS; 1128 } else { 1129 1130 /* 1131 * Exclusive waiters sleeping with 1132 * LK_SLEEPFAIL on and using 1133 * interruptible sleeps/timeout may 1134 * have left spourious lk_exslpfail 1135 * counts on, so clean it up anyway. 1136 */ 1137 MPASS(v & LK_SHARED_WAITERS); 1138 lk->lk_exslpfail = 0; 1139 queue = SQ_SHARED_QUEUE; 1140 v &= ~LK_SHARED_WAITERS; 1141 } 1142 if (queue == SQ_EXCLUSIVE_QUEUE) { 1143 realexslp = 1144 sleepq_sleepcnt(&lk->lock_object, 1145 SQ_EXCLUSIVE_QUEUE); 1146 if (lk->lk_exslpfail >= realexslp) { 1147 lk->lk_exslpfail = 0; 1148 queue = SQ_SHARED_QUEUE; 1149 v &= ~LK_SHARED_WAITERS; 1150 if (realexslp != 0) { 1151 LOCK_LOG2(lk, 1152 "%s: %p has only LK_SLEEPFAIL sleepers", 1153 __func__, lk); 1154 LOCK_LOG2(lk, 1155 "%s: %p waking up threads on the exclusive queue", 1156 __func__, lk); 1157 wakeup_swapper = 1158 sleepq_broadcast( 1159 &lk->lock_object, 1160 SLEEPQ_LK, 0, 1161 SQ_EXCLUSIVE_QUEUE); 1162 } 1163 } else 1164 lk->lk_exslpfail = 0; 1165 } 1166 if (!atomic_cmpset_ptr(&lk->lk_lock, x, v)) { 1167 sleepq_release(&lk->lock_object); 1168 continue; 1169 } 1170 LOCK_LOG3(lk, 1171 "%s: %p waking up all threads on the %s queue", 1172 __func__, lk, queue == SQ_SHARED_QUEUE ? 1173 "shared" : "exclusive"); 1174 wakeup_swapper |= sleepq_broadcast( 1175 &lk->lock_object, SLEEPQ_LK, 0, queue); 1176 1177 /* 1178 * If shared waiters have been woken up we need 1179 * to wait for one of them to acquire the lock 1180 * before to set the exclusive waiters in 1181 * order to avoid a deadlock. 1182 */ 1183 if (queue == SQ_SHARED_QUEUE) { 1184 for (v = lk->lk_lock; 1185 (v & LK_SHARE) && !LK_SHARERS(v); 1186 v = lk->lk_lock) 1187 cpu_spinwait(); 1188 } 1189 } 1190 1191 /* 1192 * Try to set the LK_EXCLUSIVE_WAITERS flag. If we 1193 * fail, loop back and retry. 1194 */ 1195 if ((x & LK_EXCLUSIVE_WAITERS) == 0) { 1196 if (!atomic_cmpset_ptr(&lk->lk_lock, x, 1197 x | LK_EXCLUSIVE_WAITERS)) { 1198 sleepq_release(&lk->lock_object); 1199 continue; 1200 } 1201 LOCK_LOG2(lk, "%s: %p set drain waiters flag", 1202 __func__, lk); 1203 } 1204 1205 /* 1206 * As far as we have been unable to acquire the 1207 * exclusive lock and the exclusive waiters flag 1208 * is set, we will sleep. 1209 */ 1210 if (flags & LK_INTERLOCK) { 1211 class->lc_unlock(ilk); 1212 flags &= ~LK_INTERLOCK; 1213 } 1214 GIANT_SAVE(); 1215 sleepq_add(&lk->lock_object, NULL, iwmesg, SLEEPQ_LK, 1216 SQ_EXCLUSIVE_QUEUE); 1217 sleepq_wait(&lk->lock_object, ipri & PRIMASK); 1218 GIANT_RESTORE(); 1219 LOCK_LOG2(lk, "%s: %p resuming from the sleep queue", 1220 __func__, lk); 1221 } 1222 1223 if (error == 0) { 1224 lock_profile_obtain_lock_success(&lk->lock_object, 1225 contested, waittime, file, line); 1226 LOCK_LOG_LOCK("DRAIN", &lk->lock_object, 0, 1227 lk->lk_recurse, file, line); 1228 WITNESS_LOCK(&lk->lock_object, LOP_EXCLUSIVE | 1229 LK_TRYWIT(flags), file, line); 1230 TD_LOCKS_INC(curthread); 1231 STACK_SAVE(lk); 1232 } 1233 break; 1234 default: 1235 if (flags & LK_INTERLOCK) 1236 class->lc_unlock(ilk); 1237 panic("%s: unknown lockmgr request 0x%x\n", __func__, op); 1238 } 1239 1240 if (flags & LK_INTERLOCK) 1241 class->lc_unlock(ilk); 1242 if (wakeup_swapper) 1243 kick_proc0(); 1244 1245 return (error); 1246 } 1247 1248 void 1249 _lockmgr_disown(struct lock *lk, const char *file, int line) 1250 { 1251 uintptr_t tid, x; 1252 1253 if (SCHEDULER_STOPPED()) 1254 return; 1255 1256 tid = (uintptr_t)curthread; 1257 _lockmgr_assert(lk, KA_XLOCKED | KA_NOTRECURSED, file, line); 1258 1259 /* 1260 * If the owner is already LK_KERNPROC just skip the whole operation. 1261 */ 1262 if (LK_HOLDER(lk->lk_lock) != tid) 1263 return; 1264 lock_profile_release_lock(&lk->lock_object); 1265 LOCK_LOG_LOCK("XDISOWN", &lk->lock_object, 0, 0, file, line); 1266 WITNESS_UNLOCK(&lk->lock_object, LOP_EXCLUSIVE, file, line); 1267 TD_LOCKS_DEC(curthread); 1268 STACK_SAVE(lk); 1269 1270 /* 1271 * In order to preserve waiters flags, just spin. 1272 */ 1273 for (;;) { 1274 x = lk->lk_lock; 1275 MPASS((x & LK_EXCLUSIVE_SPINNERS) == 0); 1276 x &= LK_ALL_WAITERS; 1277 if (atomic_cmpset_rel_ptr(&lk->lk_lock, tid | x, 1278 LK_KERNPROC | x)) 1279 return; 1280 cpu_spinwait(); 1281 } 1282 } 1283 1284 void 1285 lockmgr_printinfo(const struct lock *lk) 1286 { 1287 struct thread *td; 1288 uintptr_t x; 1289 1290 if (lk->lk_lock == LK_UNLOCKED) 1291 printf("lock type %s: UNLOCKED\n", lk->lock_object.lo_name); 1292 else if (lk->lk_lock & LK_SHARE) 1293 printf("lock type %s: SHARED (count %ju)\n", 1294 lk->lock_object.lo_name, 1295 (uintmax_t)LK_SHARERS(lk->lk_lock)); 1296 else { 1297 td = lockmgr_xholder(lk); 1298 printf("lock type %s: EXCL by thread %p " 1299 "(pid %d, %s, tid %d)\n", lk->lock_object.lo_name, td, 1300 td->td_proc->p_pid, td->td_proc->p_comm, td->td_tid); 1301 } 1302 1303 x = lk->lk_lock; 1304 if (x & LK_EXCLUSIVE_WAITERS) 1305 printf(" with exclusive waiters pending\n"); 1306 if (x & LK_SHARED_WAITERS) 1307 printf(" with shared waiters pending\n"); 1308 if (x & LK_EXCLUSIVE_SPINNERS) 1309 printf(" with exclusive spinners pending\n"); 1310 1311 STACK_PRINT(lk); 1312 } 1313 1314 int 1315 lockstatus(const struct lock *lk) 1316 { 1317 uintptr_t v, x; 1318 int ret; 1319 1320 ret = LK_SHARED; 1321 x = lk->lk_lock; 1322 v = LK_HOLDER(x); 1323 1324 if ((x & LK_SHARE) == 0) { 1325 if (v == (uintptr_t)curthread || v == LK_KERNPROC) 1326 ret = LK_EXCLUSIVE; 1327 else 1328 ret = LK_EXCLOTHER; 1329 } else if (x == LK_UNLOCKED) 1330 ret = 0; 1331 1332 return (ret); 1333 } 1334 1335 #ifdef INVARIANT_SUPPORT 1336 1337 FEATURE(invariant_support, 1338 "Support for modules compiled with INVARIANTS option"); 1339 1340 #ifndef INVARIANTS 1341 #undef _lockmgr_assert 1342 #endif 1343 1344 void 1345 _lockmgr_assert(const struct lock *lk, int what, const char *file, int line) 1346 { 1347 int slocked = 0; 1348 1349 if (panicstr != NULL) 1350 return; 1351 switch (what) { 1352 case KA_SLOCKED: 1353 case KA_SLOCKED | KA_NOTRECURSED: 1354 case KA_SLOCKED | KA_RECURSED: 1355 slocked = 1; 1356 case KA_LOCKED: 1357 case KA_LOCKED | KA_NOTRECURSED: 1358 case KA_LOCKED | KA_RECURSED: 1359 #ifdef WITNESS 1360 1361 /* 1362 * We cannot trust WITNESS if the lock is held in exclusive 1363 * mode and a call to lockmgr_disown() happened. 1364 * Workaround this skipping the check if the lock is held in 1365 * exclusive mode even for the KA_LOCKED case. 1366 */ 1367 if (slocked || (lk->lk_lock & LK_SHARE)) { 1368 witness_assert(&lk->lock_object, what, file, line); 1369 break; 1370 } 1371 #endif 1372 if (lk->lk_lock == LK_UNLOCKED || 1373 ((lk->lk_lock & LK_SHARE) == 0 && (slocked || 1374 (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk))))) 1375 panic("Lock %s not %slocked @ %s:%d\n", 1376 lk->lock_object.lo_name, slocked ? "share" : "", 1377 file, line); 1378 1379 if ((lk->lk_lock & LK_SHARE) == 0) { 1380 if (lockmgr_recursed(lk)) { 1381 if (what & KA_NOTRECURSED) 1382 panic("Lock %s recursed @ %s:%d\n", 1383 lk->lock_object.lo_name, file, 1384 line); 1385 } else if (what & KA_RECURSED) 1386 panic("Lock %s not recursed @ %s:%d\n", 1387 lk->lock_object.lo_name, file, line); 1388 } 1389 break; 1390 case KA_XLOCKED: 1391 case KA_XLOCKED | KA_NOTRECURSED: 1392 case KA_XLOCKED | KA_RECURSED: 1393 if (!lockmgr_xlocked(lk) && !lockmgr_disowned(lk)) 1394 panic("Lock %s not exclusively locked @ %s:%d\n", 1395 lk->lock_object.lo_name, file, line); 1396 if (lockmgr_recursed(lk)) { 1397 if (what & KA_NOTRECURSED) 1398 panic("Lock %s recursed @ %s:%d\n", 1399 lk->lock_object.lo_name, file, line); 1400 } else if (what & KA_RECURSED) 1401 panic("Lock %s not recursed @ %s:%d\n", 1402 lk->lock_object.lo_name, file, line); 1403 break; 1404 case KA_UNLOCKED: 1405 if (lockmgr_xlocked(lk) || lockmgr_disowned(lk)) 1406 panic("Lock %s exclusively locked @ %s:%d\n", 1407 lk->lock_object.lo_name, file, line); 1408 break; 1409 default: 1410 panic("Unknown lockmgr assertion: %d @ %s:%d\n", what, file, 1411 line); 1412 } 1413 } 1414 #endif 1415 1416 #ifdef DDB 1417 int 1418 lockmgr_chain(struct thread *td, struct thread **ownerp) 1419 { 1420 struct lock *lk; 1421 1422 lk = td->td_wchan; 1423 1424 if (LOCK_CLASS(&lk->lock_object) != &lock_class_lockmgr) 1425 return (0); 1426 db_printf("blocked on lockmgr %s", lk->lock_object.lo_name); 1427 if (lk->lk_lock & LK_SHARE) 1428 db_printf("SHARED (count %ju)\n", 1429 (uintmax_t)LK_SHARERS(lk->lk_lock)); 1430 else 1431 db_printf("EXCL\n"); 1432 *ownerp = lockmgr_xholder(lk); 1433 1434 return (1); 1435 } 1436 1437 static void 1438 db_show_lockmgr(const struct lock_object *lock) 1439 { 1440 struct thread *td; 1441 const struct lock *lk; 1442 1443 lk = (const struct lock *)lock; 1444 1445 db_printf(" state: "); 1446 if (lk->lk_lock == LK_UNLOCKED) 1447 db_printf("UNLOCKED\n"); 1448 else if (lk->lk_lock & LK_SHARE) 1449 db_printf("SLOCK: %ju\n", (uintmax_t)LK_SHARERS(lk->lk_lock)); 1450 else { 1451 td = lockmgr_xholder(lk); 1452 if (td == (struct thread *)LK_KERNPROC) 1453 db_printf("XLOCK: LK_KERNPROC\n"); 1454 else 1455 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1456 td->td_tid, td->td_proc->p_pid, 1457 td->td_proc->p_comm); 1458 if (lockmgr_recursed(lk)) 1459 db_printf(" recursed: %d\n", lk->lk_recurse); 1460 } 1461 db_printf(" waiters: "); 1462 switch (lk->lk_lock & LK_ALL_WAITERS) { 1463 case LK_SHARED_WAITERS: 1464 db_printf("shared\n"); 1465 break; 1466 case LK_EXCLUSIVE_WAITERS: 1467 db_printf("exclusive\n"); 1468 break; 1469 case LK_ALL_WAITERS: 1470 db_printf("shared and exclusive\n"); 1471 break; 1472 default: 1473 db_printf("none\n"); 1474 } 1475 db_printf(" spinners: "); 1476 if (lk->lk_lock & LK_EXCLUSIVE_SPINNERS) 1477 db_printf("exclusive\n"); 1478 else 1479 db_printf("none\n"); 1480 } 1481 #endif 1482