1 /*- 2 * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org> 3 * Copyright (c) 2001 Jason Evans <jasone@freebsd.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice(s), this list of conditions and the following disclaimer as 11 * the first lines of this file unmodified other than the possible 12 * addition of one or more copyright notices. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice(s), this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 27 * DAMAGE. 28 */ 29 30 /* 31 * Shared/exclusive locks. This implementation attempts to ensure 32 * deterministic lock granting behavior, so that slocks and xlocks are 33 * interleaved. 34 * 35 * Priority propagation will not generally raise the priority of lock holders, 36 * so should not be relied upon in combination with sx locks. 37 */ 38 39 #include "opt_ddb.h" 40 #include "opt_hwpmc_hooks.h" 41 #include "opt_no_adaptive_sx.h" 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kdb.h> 49 #include <sys/ktr.h> 50 #include <sys/lock.h> 51 #include <sys/mutex.h> 52 #include <sys/proc.h> 53 #include <sys/sleepqueue.h> 54 #include <sys/sx.h> 55 #include <sys/sysctl.h> 56 57 #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 58 #include <machine/cpu.h> 59 #endif 60 61 #ifdef DDB 62 #include <ddb/ddb.h> 63 #endif 64 65 #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 66 #define ADAPTIVE_SX 67 #endif 68 69 CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE); 70 71 #ifdef HWPMC_HOOKS 72 #include <sys/pmckern.h> 73 PMC_SOFT_DECLARE( , , lock, failed); 74 #endif 75 76 /* Handy macros for sleep queues. */ 77 #define SQ_EXCLUSIVE_QUEUE 0 78 #define SQ_SHARED_QUEUE 1 79 80 /* 81 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We 82 * drop Giant anytime we have to sleep or if we adaptively spin. 83 */ 84 #define GIANT_DECLARE \ 85 int _giantcnt = 0; \ 86 WITNESS_SAVE_DECL(Giant) \ 87 88 #define GIANT_SAVE() do { \ 89 if (mtx_owned(&Giant)) { \ 90 WITNESS_SAVE(&Giant.lock_object, Giant); \ 91 while (mtx_owned(&Giant)) { \ 92 _giantcnt++; \ 93 mtx_unlock(&Giant); \ 94 } \ 95 } \ 96 } while (0) 97 98 #define GIANT_RESTORE() do { \ 99 if (_giantcnt > 0) { \ 100 mtx_assert(&Giant, MA_NOTOWNED); \ 101 while (_giantcnt--) \ 102 mtx_lock(&Giant); \ 103 WITNESS_RESTORE(&Giant.lock_object, Giant); \ 104 } \ 105 } while (0) 106 107 /* 108 * Returns true if an exclusive lock is recursed. It assumes 109 * curthread currently has an exclusive lock. 110 */ 111 #define sx_recursed(sx) ((sx)->sx_recurse != 0) 112 113 static void assert_sx(const struct lock_object *lock, int what); 114 #ifdef DDB 115 static void db_show_sx(const struct lock_object *lock); 116 #endif 117 static void lock_sx(struct lock_object *lock, uintptr_t how); 118 #ifdef KDTRACE_HOOKS 119 static int owner_sx(const struct lock_object *lock, struct thread **owner); 120 #endif 121 static uintptr_t unlock_sx(struct lock_object *lock); 122 123 struct lock_class lock_class_sx = { 124 .lc_name = "sx", 125 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, 126 .lc_assert = assert_sx, 127 #ifdef DDB 128 .lc_ddb_show = db_show_sx, 129 #endif 130 .lc_lock = lock_sx, 131 .lc_unlock = unlock_sx, 132 #ifdef KDTRACE_HOOKS 133 .lc_owner = owner_sx, 134 #endif 135 }; 136 137 #ifndef INVARIANTS 138 #define _sx_assert(sx, what, file, line) 139 #endif 140 141 #ifdef ADAPTIVE_SX 142 static u_int asx_retries = 10; 143 static u_int asx_loops = 10000; 144 static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging"); 145 SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, ""); 146 SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, ""); 147 #endif 148 149 void 150 assert_sx(const struct lock_object *lock, int what) 151 { 152 153 sx_assert((const struct sx *)lock, what); 154 } 155 156 void 157 lock_sx(struct lock_object *lock, uintptr_t how) 158 { 159 struct sx *sx; 160 161 sx = (struct sx *)lock; 162 if (how) 163 sx_slock(sx); 164 else 165 sx_xlock(sx); 166 } 167 168 uintptr_t 169 unlock_sx(struct lock_object *lock) 170 { 171 struct sx *sx; 172 173 sx = (struct sx *)lock; 174 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED); 175 if (sx_xlocked(sx)) { 176 sx_xunlock(sx); 177 return (0); 178 } else { 179 sx_sunlock(sx); 180 return (1); 181 } 182 } 183 184 #ifdef KDTRACE_HOOKS 185 int 186 owner_sx(const struct lock_object *lock, struct thread **owner) 187 { 188 const struct sx *sx = (const struct sx *)lock; 189 uintptr_t x = sx->sx_lock; 190 191 *owner = (struct thread *)SX_OWNER(x); 192 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) : 193 (*owner != NULL)); 194 } 195 #endif 196 197 void 198 sx_sysinit(void *arg) 199 { 200 struct sx_args *sargs = arg; 201 202 sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags); 203 } 204 205 void 206 sx_init_flags(struct sx *sx, const char *description, int opts) 207 { 208 int flags; 209 210 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK | 211 SX_NOPROFILE | SX_NOADAPTIVE)) == 0); 212 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock, 213 ("%s: sx_lock not aligned for %s: %p", __func__, description, 214 &sx->sx_lock)); 215 216 flags = LO_SLEEPABLE | LO_UPGRADABLE; 217 if (opts & SX_DUPOK) 218 flags |= LO_DUPOK; 219 if (opts & SX_NOPROFILE) 220 flags |= LO_NOPROFILE; 221 if (!(opts & SX_NOWITNESS)) 222 flags |= LO_WITNESS; 223 if (opts & SX_RECURSE) 224 flags |= LO_RECURSABLE; 225 if (opts & SX_QUIET) 226 flags |= LO_QUIET; 227 228 flags |= opts & SX_NOADAPTIVE; 229 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags); 230 sx->sx_lock = SX_LOCK_UNLOCKED; 231 sx->sx_recurse = 0; 232 } 233 234 void 235 sx_destroy(struct sx *sx) 236 { 237 238 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held")); 239 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed")); 240 sx->sx_lock = SX_LOCK_DESTROYED; 241 lock_destroy(&sx->lock_object); 242 } 243 244 int 245 _sx_slock(struct sx *sx, int opts, const char *file, int line) 246 { 247 int error = 0; 248 249 if (SCHEDULER_STOPPED()) 250 return (0); 251 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 252 ("sx_slock() by idle thread %p on sx %s @ %s:%d", 253 curthread, sx->lock_object.lo_name, file, line)); 254 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 255 ("sx_slock() of destroyed sx @ %s:%d", file, line)); 256 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 257 error = __sx_slock(sx, opts, file, line); 258 if (!error) { 259 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 260 WITNESS_LOCK(&sx->lock_object, 0, file, line); 261 curthread->td_locks++; 262 } 263 264 return (error); 265 } 266 267 int 268 sx_try_slock_(struct sx *sx, const char *file, int line) 269 { 270 uintptr_t x; 271 272 if (SCHEDULER_STOPPED()) 273 return (1); 274 275 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 276 ("sx_try_slock() by idle thread %p on sx %s @ %s:%d", 277 curthread, sx->lock_object.lo_name, file, line)); 278 279 for (;;) { 280 x = sx->sx_lock; 281 KASSERT(x != SX_LOCK_DESTROYED, 282 ("sx_try_slock() of destroyed sx @ %s:%d", file, line)); 283 if (!(x & SX_LOCK_SHARED)) 284 break; 285 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) { 286 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line); 287 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line); 288 curthread->td_locks++; 289 return (1); 290 } 291 } 292 293 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line); 294 return (0); 295 } 296 297 int 298 _sx_xlock(struct sx *sx, int opts, const char *file, int line) 299 { 300 int error = 0; 301 302 if (SCHEDULER_STOPPED()) 303 return (0); 304 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 305 ("sx_xlock() by idle thread %p on sx %s @ %s:%d", 306 curthread, sx->lock_object.lo_name, file, line)); 307 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 308 ("sx_xlock() of destroyed sx @ %s:%d", file, line)); 309 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 310 line, NULL); 311 error = __sx_xlock(sx, curthread, opts, file, line); 312 if (!error) { 313 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, 314 file, line); 315 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 316 curthread->td_locks++; 317 } 318 319 return (error); 320 } 321 322 int 323 sx_try_xlock_(struct sx *sx, const char *file, int line) 324 { 325 int rval; 326 327 if (SCHEDULER_STOPPED()) 328 return (1); 329 330 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 331 ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d", 332 curthread, sx->lock_object.lo_name, file, line)); 333 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 334 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line)); 335 336 if (sx_xlocked(sx) && 337 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) { 338 sx->sx_recurse++; 339 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 340 rval = 1; 341 } else 342 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, 343 (uintptr_t)curthread); 344 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line); 345 if (rval) { 346 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 347 file, line); 348 curthread->td_locks++; 349 } 350 351 return (rval); 352 } 353 354 void 355 _sx_sunlock(struct sx *sx, const char *file, int line) 356 { 357 358 if (SCHEDULER_STOPPED()) 359 return; 360 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 361 ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 362 _sx_assert(sx, SA_SLOCKED, file, line); 363 WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 364 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 365 __sx_sunlock(sx, file, line); 366 curthread->td_locks--; 367 } 368 369 void 370 _sx_xunlock(struct sx *sx, const char *file, int line) 371 { 372 373 if (SCHEDULER_STOPPED()) 374 return; 375 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 376 ("sx_xunlock() of destroyed sx @ %s:%d", file, line)); 377 _sx_assert(sx, SA_XLOCKED, file, line); 378 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 379 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file, 380 line); 381 __sx_xunlock(sx, curthread, file, line); 382 curthread->td_locks--; 383 } 384 385 /* 386 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock. 387 * This will only succeed if this thread holds a single shared lock. 388 * Return 1 if if the upgrade succeed, 0 otherwise. 389 */ 390 int 391 sx_try_upgrade_(struct sx *sx, const char *file, int line) 392 { 393 uintptr_t x; 394 int success; 395 396 if (SCHEDULER_STOPPED()) 397 return (1); 398 399 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 400 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 401 _sx_assert(sx, SA_SLOCKED, file, line); 402 403 /* 404 * Try to switch from one shared lock to an exclusive lock. We need 405 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 406 * we will wake up the exclusive waiters when we drop the lock. 407 */ 408 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS; 409 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, 410 (uintptr_t)curthread | x); 411 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 412 if (success) { 413 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 414 file, line); 415 LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx); 416 } 417 return (success); 418 } 419 420 /* 421 * Downgrade an unrecursed exclusive lock into a single shared lock. 422 */ 423 void 424 sx_downgrade_(struct sx *sx, const char *file, int line) 425 { 426 uintptr_t x; 427 int wakeup_swapper; 428 429 if (SCHEDULER_STOPPED()) 430 return; 431 432 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 433 ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 434 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 435 #ifndef INVARIANTS 436 if (sx_recursed(sx)) 437 panic("downgrade of a recursed lock"); 438 #endif 439 440 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 441 442 /* 443 * Try to switch from an exclusive lock with no shared waiters 444 * to one sharer with no shared waiters. If there are 445 * exclusive waiters, we don't need to lock the sleep queue so 446 * long as we preserve the flag. We do one quick try and if 447 * that fails we grab the sleepq lock to keep the flags from 448 * changing and do it the slow way. 449 * 450 * We have to lock the sleep queue if there are shared waiters 451 * so we can wake them up. 452 */ 453 x = sx->sx_lock; 454 if (!(x & SX_LOCK_SHARED_WAITERS) && 455 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 456 (x & SX_LOCK_EXCLUSIVE_WAITERS))) { 457 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 458 return; 459 } 460 461 /* 462 * Lock the sleep queue so we can read the waiters bits 463 * without any races and wakeup any shared waiters. 464 */ 465 sleepq_lock(&sx->lock_object); 466 467 /* 468 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 469 * shared lock. If there are any shared waiters, wake them up. 470 */ 471 wakeup_swapper = 0; 472 x = sx->sx_lock; 473 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 474 (x & SX_LOCK_EXCLUSIVE_WAITERS)); 475 if (x & SX_LOCK_SHARED_WAITERS) 476 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 477 0, SQ_SHARED_QUEUE); 478 sleepq_release(&sx->lock_object); 479 480 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 481 LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx); 482 483 if (wakeup_swapper) 484 kick_proc0(); 485 } 486 487 /* 488 * This function represents the so-called 'hard case' for sx_xlock 489 * operation. All 'easy case' failures are redirected to this. Note 490 * that ideally this would be a static function, but it needs to be 491 * accessible from at least sx.h. 492 */ 493 int 494 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file, 495 int line) 496 { 497 GIANT_DECLARE; 498 #ifdef ADAPTIVE_SX 499 volatile struct thread *owner; 500 u_int i, spintries = 0; 501 #endif 502 uintptr_t x; 503 #ifdef LOCK_PROFILING 504 uint64_t waittime = 0; 505 int contested = 0; 506 #endif 507 int error = 0; 508 #ifdef KDTRACE_HOOKS 509 uint64_t spin_cnt = 0; 510 uint64_t sleep_cnt = 0; 511 int64_t sleep_time = 0; 512 #endif 513 514 if (SCHEDULER_STOPPED()) 515 return (0); 516 517 /* If we already hold an exclusive lock, then recurse. */ 518 if (sx_xlocked(sx)) { 519 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0, 520 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 521 sx->lock_object.lo_name, file, line)); 522 sx->sx_recurse++; 523 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 524 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 525 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 526 return (0); 527 } 528 529 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 530 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 531 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 532 533 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) { 534 #ifdef KDTRACE_HOOKS 535 spin_cnt++; 536 #endif 537 #ifdef HWPMC_HOOKS 538 PMC_SOFT_CALL( , , lock, failed); 539 #endif 540 lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 541 &waittime); 542 #ifdef ADAPTIVE_SX 543 /* 544 * If the lock is write locked and the owner is 545 * running on another CPU, spin until the owner stops 546 * running or the state of the lock changes. 547 */ 548 x = sx->sx_lock; 549 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 550 if ((x & SX_LOCK_SHARED) == 0) { 551 x = SX_OWNER(x); 552 owner = (struct thread *)x; 553 if (TD_IS_RUNNING(owner)) { 554 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 555 CTR3(KTR_LOCK, 556 "%s: spinning on %p held by %p", 557 __func__, sx, owner); 558 GIANT_SAVE(); 559 while (SX_OWNER(sx->sx_lock) == x && 560 TD_IS_RUNNING(owner)) { 561 cpu_spinwait(); 562 #ifdef KDTRACE_HOOKS 563 spin_cnt++; 564 #endif 565 } 566 continue; 567 } 568 } else if (SX_SHARERS(x) && spintries < asx_retries) { 569 GIANT_SAVE(); 570 spintries++; 571 for (i = 0; i < asx_loops; i++) { 572 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 573 CTR4(KTR_LOCK, 574 "%s: shared spinning on %p with %u and %u", 575 __func__, sx, spintries, i); 576 x = sx->sx_lock; 577 if ((x & SX_LOCK_SHARED) == 0 || 578 SX_SHARERS(x) == 0) 579 break; 580 cpu_spinwait(); 581 #ifdef KDTRACE_HOOKS 582 spin_cnt++; 583 #endif 584 } 585 if (i != asx_loops) 586 continue; 587 } 588 } 589 #endif 590 591 sleepq_lock(&sx->lock_object); 592 x = sx->sx_lock; 593 594 /* 595 * If the lock was released while spinning on the 596 * sleep queue chain lock, try again. 597 */ 598 if (x == SX_LOCK_UNLOCKED) { 599 sleepq_release(&sx->lock_object); 600 continue; 601 } 602 603 #ifdef ADAPTIVE_SX 604 /* 605 * The current lock owner might have started executing 606 * on another CPU (or the lock could have changed 607 * owners) while we were waiting on the sleep queue 608 * chain lock. If so, drop the sleep queue lock and try 609 * again. 610 */ 611 if (!(x & SX_LOCK_SHARED) && 612 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 613 owner = (struct thread *)SX_OWNER(x); 614 if (TD_IS_RUNNING(owner)) { 615 sleepq_release(&sx->lock_object); 616 continue; 617 } 618 } 619 #endif 620 621 /* 622 * If an exclusive lock was released with both shared 623 * and exclusive waiters and a shared waiter hasn't 624 * woken up and acquired the lock yet, sx_lock will be 625 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 626 * If we see that value, try to acquire it once. Note 627 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 628 * as there are other exclusive waiters still. If we 629 * fail, restart the loop. 630 */ 631 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 632 if (atomic_cmpset_acq_ptr(&sx->sx_lock, 633 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS, 634 tid | SX_LOCK_EXCLUSIVE_WAITERS)) { 635 sleepq_release(&sx->lock_object); 636 CTR2(KTR_LOCK, "%s: %p claimed by new writer", 637 __func__, sx); 638 break; 639 } 640 sleepq_release(&sx->lock_object); 641 continue; 642 } 643 644 /* 645 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 646 * than loop back and retry. 647 */ 648 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 649 if (!atomic_cmpset_ptr(&sx->sx_lock, x, 650 x | SX_LOCK_EXCLUSIVE_WAITERS)) { 651 sleepq_release(&sx->lock_object); 652 continue; 653 } 654 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 655 CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 656 __func__, sx); 657 } 658 659 /* 660 * Since we have been unable to acquire the exclusive 661 * lock and the exclusive waiters flag is set, we have 662 * to sleep. 663 */ 664 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 665 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 666 __func__, sx); 667 668 #ifdef KDTRACE_HOOKS 669 sleep_time -= lockstat_nsecs(); 670 #endif 671 GIANT_SAVE(); 672 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 673 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 674 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 675 if (!(opts & SX_INTERRUPTIBLE)) 676 sleepq_wait(&sx->lock_object, 0); 677 else 678 error = sleepq_wait_sig(&sx->lock_object, 0); 679 #ifdef KDTRACE_HOOKS 680 sleep_time += lockstat_nsecs(); 681 sleep_cnt++; 682 #endif 683 if (error) { 684 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 685 CTR2(KTR_LOCK, 686 "%s: interruptible sleep by %p suspended by signal", 687 __func__, sx); 688 break; 689 } 690 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 691 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 692 __func__, sx); 693 } 694 695 GIANT_RESTORE(); 696 if (!error) 697 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx, 698 contested, waittime, file, line); 699 #ifdef KDTRACE_HOOKS 700 if (sleep_time) 701 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time); 702 if (spin_cnt > sleep_cnt) 703 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt)); 704 #endif 705 return (error); 706 } 707 708 /* 709 * This function represents the so-called 'hard case' for sx_xunlock 710 * operation. All 'easy case' failures are redirected to this. Note 711 * that ideally this would be a static function, but it needs to be 712 * accessible from at least sx.h. 713 */ 714 void 715 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line) 716 { 717 uintptr_t x; 718 int queue, wakeup_swapper; 719 720 if (SCHEDULER_STOPPED()) 721 return; 722 723 MPASS(!(sx->sx_lock & SX_LOCK_SHARED)); 724 725 /* If the lock is recursed, then unrecurse one level. */ 726 if (sx_xlocked(sx) && sx_recursed(sx)) { 727 if ((--sx->sx_recurse) == 0) 728 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 729 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 730 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 731 return; 732 } 733 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS | 734 SX_LOCK_EXCLUSIVE_WAITERS)); 735 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 736 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 737 738 sleepq_lock(&sx->lock_object); 739 x = SX_LOCK_UNLOCKED; 740 741 /* 742 * The wake up algorithm here is quite simple and probably not 743 * ideal. It gives precedence to shared waiters if they are 744 * present. For this condition, we have to preserve the 745 * state of the exclusive waiters flag. 746 * If interruptible sleeps left the shared queue empty avoid a 747 * starvation for the threads sleeping on the exclusive queue by giving 748 * them precedence and cleaning up the shared waiters bit anyway. 749 */ 750 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 && 751 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) { 752 queue = SQ_SHARED_QUEUE; 753 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS); 754 } else 755 queue = SQ_EXCLUSIVE_QUEUE; 756 757 /* Wake up all the waiters for the specific queue. */ 758 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 759 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 760 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 761 "exclusive"); 762 atomic_store_rel_ptr(&sx->sx_lock, x); 763 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 764 queue); 765 sleepq_release(&sx->lock_object); 766 if (wakeup_swapper) 767 kick_proc0(); 768 } 769 770 /* 771 * This function represents the so-called 'hard case' for sx_slock 772 * operation. All 'easy case' failures are redirected to this. Note 773 * that ideally this would be a static function, but it needs to be 774 * accessible from at least sx.h. 775 */ 776 int 777 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line) 778 { 779 GIANT_DECLARE; 780 #ifdef ADAPTIVE_SX 781 volatile struct thread *owner; 782 #endif 783 #ifdef LOCK_PROFILING 784 uint64_t waittime = 0; 785 int contested = 0; 786 #endif 787 uintptr_t x; 788 int error = 0; 789 #ifdef KDTRACE_HOOKS 790 uint64_t spin_cnt = 0; 791 uint64_t sleep_cnt = 0; 792 int64_t sleep_time = 0; 793 #endif 794 795 if (SCHEDULER_STOPPED()) 796 return (0); 797 798 /* 799 * As with rwlocks, we don't make any attempt to try to block 800 * shared locks once there is an exclusive waiter. 801 */ 802 for (;;) { 803 #ifdef KDTRACE_HOOKS 804 spin_cnt++; 805 #endif 806 x = sx->sx_lock; 807 808 /* 809 * If no other thread has an exclusive lock then try to bump up 810 * the count of sharers. Since we have to preserve the state 811 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 812 * shared lock loop back and retry. 813 */ 814 if (x & SX_LOCK_SHARED) { 815 MPASS(!(x & SX_LOCK_SHARED_WAITERS)); 816 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, 817 x + SX_ONE_SHARER)) { 818 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 819 CTR4(KTR_LOCK, 820 "%s: %p succeed %p -> %p", __func__, 821 sx, (void *)x, 822 (void *)(x + SX_ONE_SHARER)); 823 break; 824 } 825 continue; 826 } 827 #ifdef HWPMC_HOOKS 828 PMC_SOFT_CALL( , , lock, failed); 829 #endif 830 lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 831 &waittime); 832 833 #ifdef ADAPTIVE_SX 834 /* 835 * If the owner is running on another CPU, spin until 836 * the owner stops running or the state of the lock 837 * changes. 838 */ 839 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 840 x = SX_OWNER(x); 841 owner = (struct thread *)x; 842 if (TD_IS_RUNNING(owner)) { 843 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 844 CTR3(KTR_LOCK, 845 "%s: spinning on %p held by %p", 846 __func__, sx, owner); 847 GIANT_SAVE(); 848 while (SX_OWNER(sx->sx_lock) == x && 849 TD_IS_RUNNING(owner)) { 850 #ifdef KDTRACE_HOOKS 851 spin_cnt++; 852 #endif 853 cpu_spinwait(); 854 } 855 continue; 856 } 857 } 858 #endif 859 860 /* 861 * Some other thread already has an exclusive lock, so 862 * start the process of blocking. 863 */ 864 sleepq_lock(&sx->lock_object); 865 x = sx->sx_lock; 866 867 /* 868 * The lock could have been released while we spun. 869 * In this case loop back and retry. 870 */ 871 if (x & SX_LOCK_SHARED) { 872 sleepq_release(&sx->lock_object); 873 continue; 874 } 875 876 #ifdef ADAPTIVE_SX 877 /* 878 * If the owner is running on another CPU, spin until 879 * the owner stops running or the state of the lock 880 * changes. 881 */ 882 if (!(x & SX_LOCK_SHARED) && 883 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 884 owner = (struct thread *)SX_OWNER(x); 885 if (TD_IS_RUNNING(owner)) { 886 sleepq_release(&sx->lock_object); 887 continue; 888 } 889 } 890 #endif 891 892 /* 893 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 894 * fail to set it drop the sleep queue lock and loop 895 * back. 896 */ 897 if (!(x & SX_LOCK_SHARED_WAITERS)) { 898 if (!atomic_cmpset_ptr(&sx->sx_lock, x, 899 x | SX_LOCK_SHARED_WAITERS)) { 900 sleepq_release(&sx->lock_object); 901 continue; 902 } 903 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 904 CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 905 __func__, sx); 906 } 907 908 /* 909 * Since we have been unable to acquire the shared lock, 910 * we have to sleep. 911 */ 912 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 913 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 914 __func__, sx); 915 916 #ifdef KDTRACE_HOOKS 917 sleep_time -= lockstat_nsecs(); 918 #endif 919 GIANT_SAVE(); 920 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 921 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 922 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 923 if (!(opts & SX_INTERRUPTIBLE)) 924 sleepq_wait(&sx->lock_object, 0); 925 else 926 error = sleepq_wait_sig(&sx->lock_object, 0); 927 #ifdef KDTRACE_HOOKS 928 sleep_time += lockstat_nsecs(); 929 sleep_cnt++; 930 #endif 931 if (error) { 932 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 933 CTR2(KTR_LOCK, 934 "%s: interruptible sleep by %p suspended by signal", 935 __func__, sx); 936 break; 937 } 938 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 939 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 940 __func__, sx); 941 } 942 if (error == 0) 943 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx, 944 contested, waittime, file, line); 945 #ifdef KDTRACE_HOOKS 946 if (sleep_time) 947 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time); 948 if (spin_cnt > sleep_cnt) 949 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt)); 950 #endif 951 GIANT_RESTORE(); 952 return (error); 953 } 954 955 /* 956 * This function represents the so-called 'hard case' for sx_sunlock 957 * operation. All 'easy case' failures are redirected to this. Note 958 * that ideally this would be a static function, but it needs to be 959 * accessible from at least sx.h. 960 */ 961 void 962 _sx_sunlock_hard(struct sx *sx, const char *file, int line) 963 { 964 uintptr_t x; 965 int wakeup_swapper; 966 967 if (SCHEDULER_STOPPED()) 968 return; 969 970 for (;;) { 971 x = sx->sx_lock; 972 973 /* 974 * We should never have sharers while at least one thread 975 * holds a shared lock. 976 */ 977 KASSERT(!(x & SX_LOCK_SHARED_WAITERS), 978 ("%s: waiting sharers", __func__)); 979 980 /* 981 * See if there is more than one shared lock held. If 982 * so, just drop one and return. 983 */ 984 if (SX_SHARERS(x) > 1) { 985 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x, 986 x - SX_ONE_SHARER)) { 987 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 988 CTR4(KTR_LOCK, 989 "%s: %p succeeded %p -> %p", 990 __func__, sx, (void *)x, 991 (void *)(x - SX_ONE_SHARER)); 992 break; 993 } 994 continue; 995 } 996 997 /* 998 * If there aren't any waiters for an exclusive lock, 999 * then try to drop it quickly. 1000 */ 1001 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 1002 MPASS(x == SX_SHARERS_LOCK(1)); 1003 if (atomic_cmpset_rel_ptr(&sx->sx_lock, 1004 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) { 1005 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1006 CTR2(KTR_LOCK, "%s: %p last succeeded", 1007 __func__, sx); 1008 break; 1009 } 1010 continue; 1011 } 1012 1013 /* 1014 * At this point, there should just be one sharer with 1015 * exclusive waiters. 1016 */ 1017 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS)); 1018 1019 sleepq_lock(&sx->lock_object); 1020 1021 /* 1022 * Wake up semantic here is quite simple: 1023 * Just wake up all the exclusive waiters. 1024 * Note that the state of the lock could have changed, 1025 * so if it fails loop back and retry. 1026 */ 1027 if (!atomic_cmpset_rel_ptr(&sx->sx_lock, 1028 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS, 1029 SX_LOCK_UNLOCKED)) { 1030 sleepq_release(&sx->lock_object); 1031 continue; 1032 } 1033 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1034 CTR2(KTR_LOCK, "%s: %p waking up all thread on" 1035 "exclusive queue", __func__, sx); 1036 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 1037 0, SQ_EXCLUSIVE_QUEUE); 1038 sleepq_release(&sx->lock_object); 1039 if (wakeup_swapper) 1040 kick_proc0(); 1041 break; 1042 } 1043 } 1044 1045 #ifdef INVARIANT_SUPPORT 1046 #ifndef INVARIANTS 1047 #undef _sx_assert 1048 #endif 1049 1050 /* 1051 * In the non-WITNESS case, sx_assert() can only detect that at least 1052 * *some* thread owns an slock, but it cannot guarantee that *this* 1053 * thread owns an slock. 1054 */ 1055 void 1056 _sx_assert(const struct sx *sx, int what, const char *file, int line) 1057 { 1058 #ifndef WITNESS 1059 int slocked = 0; 1060 #endif 1061 1062 if (panicstr != NULL) 1063 return; 1064 switch (what) { 1065 case SA_SLOCKED: 1066 case SA_SLOCKED | SA_NOTRECURSED: 1067 case SA_SLOCKED | SA_RECURSED: 1068 #ifndef WITNESS 1069 slocked = 1; 1070 /* FALLTHROUGH */ 1071 #endif 1072 case SA_LOCKED: 1073 case SA_LOCKED | SA_NOTRECURSED: 1074 case SA_LOCKED | SA_RECURSED: 1075 #ifdef WITNESS 1076 witness_assert(&sx->lock_object, what, file, line); 1077 #else 1078 /* 1079 * If some other thread has an exclusive lock or we 1080 * have one and are asserting a shared lock, fail. 1081 * Also, if no one has a lock at all, fail. 1082 */ 1083 if (sx->sx_lock == SX_LOCK_UNLOCKED || 1084 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 1085 sx_xholder(sx) != curthread))) 1086 panic("Lock %s not %slocked @ %s:%d\n", 1087 sx->lock_object.lo_name, slocked ? "share " : "", 1088 file, line); 1089 1090 if (!(sx->sx_lock & SX_LOCK_SHARED)) { 1091 if (sx_recursed(sx)) { 1092 if (what & SA_NOTRECURSED) 1093 panic("Lock %s recursed @ %s:%d\n", 1094 sx->lock_object.lo_name, file, 1095 line); 1096 } else if (what & SA_RECURSED) 1097 panic("Lock %s not recursed @ %s:%d\n", 1098 sx->lock_object.lo_name, file, line); 1099 } 1100 #endif 1101 break; 1102 case SA_XLOCKED: 1103 case SA_XLOCKED | SA_NOTRECURSED: 1104 case SA_XLOCKED | SA_RECURSED: 1105 if (sx_xholder(sx) != curthread) 1106 panic("Lock %s not exclusively locked @ %s:%d\n", 1107 sx->lock_object.lo_name, file, line); 1108 if (sx_recursed(sx)) { 1109 if (what & SA_NOTRECURSED) 1110 panic("Lock %s recursed @ %s:%d\n", 1111 sx->lock_object.lo_name, file, line); 1112 } else if (what & SA_RECURSED) 1113 panic("Lock %s not recursed @ %s:%d\n", 1114 sx->lock_object.lo_name, file, line); 1115 break; 1116 case SA_UNLOCKED: 1117 #ifdef WITNESS 1118 witness_assert(&sx->lock_object, what, file, line); 1119 #else 1120 /* 1121 * If we hold an exclusve lock fail. We can't 1122 * reliably check to see if we hold a shared lock or 1123 * not. 1124 */ 1125 if (sx_xholder(sx) == curthread) 1126 panic("Lock %s exclusively locked @ %s:%d\n", 1127 sx->lock_object.lo_name, file, line); 1128 #endif 1129 break; 1130 default: 1131 panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 1132 line); 1133 } 1134 } 1135 #endif /* INVARIANT_SUPPORT */ 1136 1137 #ifdef DDB 1138 static void 1139 db_show_sx(const struct lock_object *lock) 1140 { 1141 struct thread *td; 1142 const struct sx *sx; 1143 1144 sx = (const struct sx *)lock; 1145 1146 db_printf(" state: "); 1147 if (sx->sx_lock == SX_LOCK_UNLOCKED) 1148 db_printf("UNLOCKED\n"); 1149 else if (sx->sx_lock == SX_LOCK_DESTROYED) { 1150 db_printf("DESTROYED\n"); 1151 return; 1152 } else if (sx->sx_lock & SX_LOCK_SHARED) 1153 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 1154 else { 1155 td = sx_xholder(sx); 1156 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1157 td->td_tid, td->td_proc->p_pid, td->td_name); 1158 if (sx_recursed(sx)) 1159 db_printf(" recursed: %d\n", sx->sx_recurse); 1160 } 1161 1162 db_printf(" waiters: "); 1163 switch(sx->sx_lock & 1164 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 1165 case SX_LOCK_SHARED_WAITERS: 1166 db_printf("shared\n"); 1167 break; 1168 case SX_LOCK_EXCLUSIVE_WAITERS: 1169 db_printf("exclusive\n"); 1170 break; 1171 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 1172 db_printf("exclusive and shared\n"); 1173 break; 1174 default: 1175 db_printf("none\n"); 1176 } 1177 } 1178 1179 /* 1180 * Check to see if a thread that is blocked on a sleep queue is actually 1181 * blocked on an sx lock. If so, output some details and return true. 1182 * If the lock has an exclusive owner, return that in *ownerp. 1183 */ 1184 int 1185 sx_chain(struct thread *td, struct thread **ownerp) 1186 { 1187 struct sx *sx; 1188 1189 /* 1190 * Check to see if this thread is blocked on an sx lock. 1191 * First, we check the lock class. If that is ok, then we 1192 * compare the lock name against the wait message. 1193 */ 1194 sx = td->td_wchan; 1195 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 1196 sx->lock_object.lo_name != td->td_wmesg) 1197 return (0); 1198 1199 /* We think we have an sx lock, so output some details. */ 1200 db_printf("blocked on sx \"%s\" ", td->td_wmesg); 1201 *ownerp = sx_xholder(sx); 1202 if (sx->sx_lock & SX_LOCK_SHARED) 1203 db_printf("SLOCK (count %ju)\n", 1204 (uintmax_t)SX_SHARERS(sx->sx_lock)); 1205 else 1206 db_printf("XLOCK\n"); 1207 return (1); 1208 } 1209 #endif 1210