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_adaptive_sx.h" 40 #include "opt_ddb.h" 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <sys/ktr.h> 47 #include <sys/lock.h> 48 #include <sys/mutex.h> 49 #include <sys/proc.h> 50 #include <sys/sleepqueue.h> 51 #include <sys/sx.h> 52 #include <sys/systm.h> 53 54 #ifdef ADAPTIVE_SX 55 #include <machine/cpu.h> 56 #endif 57 58 #ifdef DDB 59 #include <ddb/ddb.h> 60 #endif 61 62 #if !defined(SMP) && defined(ADAPTIVE_SX) 63 #error "You must have SMP to enable the ADAPTIVE_SX option" 64 #endif 65 66 CTASSERT(((SX_ADAPTIVESPIN | SX_RECURSE) & LO_CLASSFLAGS) == 67 (SX_ADAPTIVESPIN | SX_RECURSE)); 68 69 /* Handy macros for sleep queues. */ 70 #define SQ_EXCLUSIVE_QUEUE 0 71 #define SQ_SHARED_QUEUE 1 72 73 /* 74 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We 75 * drop Giant anytime we have to sleep or if we adaptively spin. 76 */ 77 #define GIANT_DECLARE \ 78 int _giantcnt = 0; \ 79 WITNESS_SAVE_DECL(Giant) \ 80 81 #define GIANT_SAVE() do { \ 82 if (mtx_owned(&Giant)) { \ 83 WITNESS_SAVE(&Giant.lock_object, Giant); \ 84 while (mtx_owned(&Giant)) { \ 85 _giantcnt++; \ 86 mtx_unlock(&Giant); \ 87 } \ 88 } \ 89 } while (0) 90 91 #define GIANT_RESTORE() do { \ 92 if (_giantcnt > 0) { \ 93 mtx_assert(&Giant, MA_NOTOWNED); \ 94 while (_giantcnt--) \ 95 mtx_lock(&Giant); \ 96 WITNESS_RESTORE(&Giant.lock_object, Giant); \ 97 } \ 98 } while (0) 99 100 /* 101 * Returns true if an exclusive lock is recursed. It assumes 102 * curthread currently has an exclusive lock. 103 */ 104 #define sx_recursed(sx) ((sx)->sx_recurse != 0) 105 106 static void assert_sx(struct lock_object *lock, int what); 107 #ifdef DDB 108 static void db_show_sx(struct lock_object *lock); 109 #endif 110 static void lock_sx(struct lock_object *lock, int how); 111 static int unlock_sx(struct lock_object *lock); 112 113 struct lock_class lock_class_sx = { 114 .lc_name = "sx", 115 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, 116 .lc_assert = assert_sx, 117 #ifdef DDB 118 .lc_ddb_show = db_show_sx, 119 #endif 120 .lc_lock = lock_sx, 121 .lc_unlock = unlock_sx, 122 }; 123 124 #ifndef INVARIANTS 125 #define _sx_assert(sx, what, file, line) 126 #endif 127 128 void 129 assert_sx(struct lock_object *lock, int what) 130 { 131 132 sx_assert((struct sx *)lock, what); 133 } 134 135 void 136 lock_sx(struct lock_object *lock, int how) 137 { 138 struct sx *sx; 139 140 sx = (struct sx *)lock; 141 if (how) 142 sx_xlock(sx); 143 else 144 sx_slock(sx); 145 } 146 147 int 148 unlock_sx(struct lock_object *lock) 149 { 150 struct sx *sx; 151 152 sx = (struct sx *)lock; 153 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED); 154 if (sx_xlocked(sx)) { 155 sx_xunlock(sx); 156 return (1); 157 } else { 158 sx_sunlock(sx); 159 return (0); 160 } 161 } 162 163 void 164 sx_sysinit(void *arg) 165 { 166 struct sx_args *sargs = arg; 167 168 sx_init(sargs->sa_sx, sargs->sa_desc); 169 } 170 171 void 172 sx_init_flags(struct sx *sx, const char *description, int opts) 173 { 174 int flags; 175 176 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK | 177 SX_NOPROFILE | SX_ADAPTIVESPIN)) == 0); 178 179 flags = LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE; 180 if (opts & SX_DUPOK) 181 flags |= LO_DUPOK; 182 if (opts & SX_NOPROFILE) 183 flags |= LO_NOPROFILE; 184 if (!(opts & SX_NOWITNESS)) 185 flags |= LO_WITNESS; 186 if (opts & SX_QUIET) 187 flags |= LO_QUIET; 188 189 flags |= opts & (SX_ADAPTIVESPIN | SX_RECURSE); 190 sx->sx_lock = SX_LOCK_UNLOCKED; 191 sx->sx_recurse = 0; 192 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags); 193 } 194 195 void 196 sx_destroy(struct sx *sx) 197 { 198 199 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held")); 200 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed")); 201 sx->sx_lock = SX_LOCK_DESTROYED; 202 lock_destroy(&sx->lock_object); 203 } 204 205 int 206 _sx_slock(struct sx *sx, int opts, const char *file, int line) 207 { 208 int error = 0; 209 210 MPASS(curthread != NULL); 211 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 212 ("sx_slock() of destroyed sx @ %s:%d", file, line)); 213 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line); 214 error = __sx_slock(sx, opts, file, line); 215 if (!error) { 216 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 217 WITNESS_LOCK(&sx->lock_object, 0, file, line); 218 curthread->td_locks++; 219 } 220 221 return (error); 222 } 223 224 int 225 _sx_try_slock(struct sx *sx, const char *file, int line) 226 { 227 uintptr_t x; 228 229 for (;;) { 230 x = sx->sx_lock; 231 KASSERT(x != SX_LOCK_DESTROYED, 232 ("sx_try_slock() of destroyed sx @ %s:%d", file, line)); 233 if (!(x & SX_LOCK_SHARED)) 234 break; 235 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) { 236 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line); 237 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line); 238 curthread->td_locks++; 239 return (1); 240 } 241 } 242 243 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line); 244 return (0); 245 } 246 247 int 248 _sx_xlock(struct sx *sx, int opts, const char *file, int line) 249 { 250 int error = 0; 251 252 MPASS(curthread != NULL); 253 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 254 ("sx_xlock() of destroyed sx @ %s:%d", file, line)); 255 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 256 line); 257 error = __sx_xlock(sx, curthread, opts, file, line); 258 if (!error) { 259 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, 260 file, line); 261 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 262 curthread->td_locks++; 263 } 264 265 return (error); 266 } 267 268 int 269 _sx_try_xlock(struct sx *sx, const char *file, int line) 270 { 271 int rval; 272 273 MPASS(curthread != NULL); 274 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 275 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line)); 276 277 if (sx_xlocked(sx) && (sx->lock_object.lo_flags & SX_RECURSE) != 0) { 278 sx->sx_recurse++; 279 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 280 rval = 1; 281 } else 282 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, 283 (uintptr_t)curthread); 284 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line); 285 if (rval) { 286 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 287 file, line); 288 curthread->td_locks++; 289 } 290 291 return (rval); 292 } 293 294 void 295 _sx_sunlock(struct sx *sx, const char *file, int line) 296 { 297 298 MPASS(curthread != NULL); 299 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 300 ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 301 _sx_assert(sx, SA_SLOCKED, file, line); 302 curthread->td_locks--; 303 WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 304 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 305 #ifdef LOCK_PROFILING_SHARED 306 if (SX_SHARERS(sx->sx_lock) == 1) 307 lock_profile_release_lock(&sx->lock_object); 308 #endif 309 __sx_sunlock(sx, file, line); 310 } 311 312 void 313 _sx_xunlock(struct sx *sx, const char *file, int line) 314 { 315 316 MPASS(curthread != NULL); 317 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 318 ("sx_xunlock() of destroyed sx @ %s:%d", file, line)); 319 _sx_assert(sx, SA_XLOCKED, file, line); 320 curthread->td_locks--; 321 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 322 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file, 323 line); 324 if (!sx_recursed(sx)) 325 lock_profile_release_lock(&sx->lock_object); 326 __sx_xunlock(sx, curthread, file, line); 327 } 328 329 /* 330 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock. 331 * This will only succeed if this thread holds a single shared lock. 332 * Return 1 if if the upgrade succeed, 0 otherwise. 333 */ 334 int 335 _sx_try_upgrade(struct sx *sx, const char *file, int line) 336 { 337 uintptr_t x; 338 int success; 339 340 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 341 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 342 _sx_assert(sx, SA_SLOCKED, file, line); 343 344 /* 345 * Try to switch from one shared lock to an exclusive lock. We need 346 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 347 * we will wake up the exclusive waiters when we drop the lock. 348 */ 349 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS; 350 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, 351 (uintptr_t)curthread | x); 352 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 353 if (success) 354 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 355 file, line); 356 return (success); 357 } 358 359 /* 360 * Downgrade an unrecursed exclusive lock into a single shared lock. 361 */ 362 void 363 _sx_downgrade(struct sx *sx, const char *file, int line) 364 { 365 uintptr_t x; 366 367 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 368 ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 369 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 370 #ifndef INVARIANTS 371 if (sx_recursed(sx)) 372 panic("downgrade of a recursed lock"); 373 #endif 374 375 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 376 377 /* 378 * Try to switch from an exclusive lock with no shared waiters 379 * to one sharer with no shared waiters. If there are 380 * exclusive waiters, we don't need to lock the sleep queue so 381 * long as we preserve the flag. We do one quick try and if 382 * that fails we grab the sleepq lock to keep the flags from 383 * changing and do it the slow way. 384 * 385 * We have to lock the sleep queue if there are shared waiters 386 * so we can wake them up. 387 */ 388 x = sx->sx_lock; 389 if (!(x & SX_LOCK_SHARED_WAITERS) && 390 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 391 (x & SX_LOCK_EXCLUSIVE_WAITERS))) { 392 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 393 return; 394 } 395 396 /* 397 * Lock the sleep queue so we can read the waiters bits 398 * without any races and wakeup any shared waiters. 399 */ 400 sleepq_lock(&sx->lock_object); 401 402 /* 403 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 404 * shared lock. If there are any shared waiters, wake them up. 405 */ 406 x = sx->sx_lock; 407 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 408 (x & SX_LOCK_EXCLUSIVE_WAITERS)); 409 if (x & SX_LOCK_SHARED_WAITERS) 410 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1, 411 SQ_SHARED_QUEUE); 412 else 413 sleepq_release(&sx->lock_object); 414 415 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 416 } 417 418 /* 419 * This function represents the so-called 'hard case' for sx_xlock 420 * operation. All 'easy case' failures are redirected to this. Note 421 * that ideally this would be a static function, but it needs to be 422 * accessible from at least sx.h. 423 */ 424 int 425 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file, 426 int line) 427 { 428 GIANT_DECLARE; 429 #ifdef ADAPTIVE_SX 430 volatile struct thread *owner; 431 #endif 432 uint64_t waittime = 0; 433 uintptr_t x; 434 int contested = 0, error = 0; 435 436 /* If we already hold an exclusive lock, then recurse. */ 437 if (sx_xlocked(sx)) { 438 KASSERT((sx->lock_object.lo_flags & SX_RECURSE) != 0, 439 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 440 sx->lock_object.lo_name, file, line)); 441 sx->sx_recurse++; 442 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 443 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 444 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 445 return (0); 446 } 447 448 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 449 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 450 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 451 452 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) { 453 #ifdef ADAPTIVE_SX 454 /* 455 * If the lock is write locked and the owner is 456 * running on another CPU, spin until the owner stops 457 * running or the state of the lock changes. 458 */ 459 x = sx->sx_lock; 460 if (!(x & SX_LOCK_SHARED) && 461 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) { 462 x = SX_OWNER(x); 463 owner = (struct thread *)x; 464 if (TD_IS_RUNNING(owner)) { 465 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 466 CTR3(KTR_LOCK, 467 "%s: spinning on %p held by %p", 468 __func__, sx, owner); 469 GIANT_SAVE(); 470 lock_profile_obtain_lock_failed( 471 &sx->lock_object, &contested, &waittime); 472 while (SX_OWNER(sx->sx_lock) == x && 473 TD_IS_RUNNING(owner)) 474 cpu_spinwait(); 475 continue; 476 } 477 } 478 #endif 479 480 sleepq_lock(&sx->lock_object); 481 x = sx->sx_lock; 482 483 /* 484 * If the lock was released while spinning on the 485 * sleep queue chain lock, try again. 486 */ 487 if (x == SX_LOCK_UNLOCKED) { 488 sleepq_release(&sx->lock_object); 489 continue; 490 } 491 492 #ifdef ADAPTIVE_SX 493 /* 494 * The current lock owner might have started executing 495 * on another CPU (or the lock could have changed 496 * owners) while we were waiting on the sleep queue 497 * chain lock. If so, drop the sleep queue lock and try 498 * again. 499 */ 500 if (!(x & SX_LOCK_SHARED) && 501 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) { 502 owner = (struct thread *)SX_OWNER(x); 503 if (TD_IS_RUNNING(owner)) { 504 sleepq_release(&sx->lock_object); 505 continue; 506 } 507 } 508 #endif 509 510 /* 511 * If an exclusive lock was released with both shared 512 * and exclusive waiters and a shared waiter hasn't 513 * woken up and acquired the lock yet, sx_lock will be 514 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 515 * If we see that value, try to acquire it once. Note 516 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 517 * as there are other exclusive waiters still. If we 518 * fail, restart the loop. 519 */ 520 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 521 if (atomic_cmpset_acq_ptr(&sx->sx_lock, 522 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS, 523 tid | SX_LOCK_EXCLUSIVE_WAITERS)) { 524 sleepq_release(&sx->lock_object); 525 CTR2(KTR_LOCK, "%s: %p claimed by new writer", 526 __func__, sx); 527 break; 528 } 529 sleepq_release(&sx->lock_object); 530 continue; 531 } 532 533 /* 534 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 535 * than loop back and retry. 536 */ 537 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 538 if (!atomic_cmpset_ptr(&sx->sx_lock, x, 539 x | SX_LOCK_EXCLUSIVE_WAITERS)) { 540 sleepq_release(&sx->lock_object); 541 continue; 542 } 543 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 544 CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 545 __func__, sx); 546 } 547 548 /* 549 * Since we have been unable to acquire the exclusive 550 * lock and the exclusive waiters flag is set, we have 551 * to sleep. 552 */ 553 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 554 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 555 __func__, sx); 556 557 GIANT_SAVE(); 558 lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 559 &waittime); 560 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 561 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 562 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 563 if (!(opts & SX_INTERRUPTIBLE)) 564 sleepq_wait(&sx->lock_object); 565 else 566 error = sleepq_wait_sig(&sx->lock_object); 567 568 if (error) { 569 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 570 CTR2(KTR_LOCK, 571 "%s: interruptible sleep by %p suspended by signal", 572 __func__, sx); 573 break; 574 } 575 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 576 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 577 __func__, sx); 578 } 579 580 GIANT_RESTORE(); 581 if (!error) 582 lock_profile_obtain_lock_success(&sx->lock_object, contested, 583 waittime, file, line); 584 return (error); 585 } 586 587 /* 588 * This function represents the so-called 'hard case' for sx_xunlock 589 * operation. All 'easy case' failures are redirected to this. Note 590 * that ideally this would be a static function, but it needs to be 591 * accessible from at least sx.h. 592 */ 593 void 594 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line) 595 { 596 uintptr_t x; 597 int queue; 598 599 MPASS(!(sx->sx_lock & SX_LOCK_SHARED)); 600 601 /* If the lock is recursed, then unrecurse one level. */ 602 if (sx_xlocked(sx) && sx_recursed(sx)) { 603 if ((--sx->sx_recurse) == 0) 604 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 605 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 606 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 607 return; 608 } 609 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS | 610 SX_LOCK_EXCLUSIVE_WAITERS)); 611 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 612 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 613 614 sleepq_lock(&sx->lock_object); 615 x = SX_LOCK_UNLOCKED; 616 617 /* 618 * The wake up algorithm here is quite simple and probably not 619 * ideal. It gives precedence to shared waiters if they are 620 * present. For this condition, we have to preserve the 621 * state of the exclusive waiters flag. 622 */ 623 if (sx->sx_lock & SX_LOCK_SHARED_WAITERS) { 624 queue = SQ_SHARED_QUEUE; 625 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS); 626 } else 627 queue = SQ_EXCLUSIVE_QUEUE; 628 629 /* Wake up all the waiters for the specific queue. */ 630 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 631 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 632 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 633 "exclusive"); 634 atomic_store_rel_ptr(&sx->sx_lock, x); 635 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1, queue); 636 } 637 638 /* 639 * This function represents the so-called 'hard case' for sx_slock 640 * operation. All 'easy case' failures are redirected to this. Note 641 * that ideally this would be a static function, but it needs to be 642 * accessible from at least sx.h. 643 */ 644 int 645 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line) 646 { 647 GIANT_DECLARE; 648 #ifdef ADAPTIVE_SX 649 volatile struct thread *owner; 650 #endif 651 #ifdef LOCK_PROFILING_SHARED 652 uint64_t waittime = 0; 653 int contested = 0; 654 #endif 655 uintptr_t x; 656 int error = 0; 657 658 /* 659 * As with rwlocks, we don't make any attempt to try to block 660 * shared locks once there is an exclusive waiter. 661 */ 662 for (;;) { 663 x = sx->sx_lock; 664 665 /* 666 * If no other thread has an exclusive lock then try to bump up 667 * the count of sharers. Since we have to preserve the state 668 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 669 * shared lock loop back and retry. 670 */ 671 if (x & SX_LOCK_SHARED) { 672 MPASS(!(x & SX_LOCK_SHARED_WAITERS)); 673 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, 674 x + SX_ONE_SHARER)) { 675 #ifdef LOCK_PROFILING_SHARED 676 if (SX_SHARERS(x) == 0) 677 lock_profile_obtain_lock_success( 678 &sx->lock_object, contested, 679 waittime, file, line); 680 #endif 681 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 682 CTR4(KTR_LOCK, 683 "%s: %p succeed %p -> %p", __func__, 684 sx, (void *)x, 685 (void *)(x + SX_ONE_SHARER)); 686 break; 687 } 688 continue; 689 } 690 691 #ifdef ADAPTIVE_SX 692 /* 693 * If the owner is running on another CPU, spin until 694 * the owner stops running or the state of the lock 695 * changes. 696 */ 697 else if (sx->lock_object.lo_flags & SX_ADAPTIVESPIN) { 698 x = SX_OWNER(x); 699 owner = (struct thread *)x; 700 if (TD_IS_RUNNING(owner)) { 701 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 702 CTR3(KTR_LOCK, 703 "%s: spinning on %p held by %p", 704 __func__, sx, owner); 705 GIANT_SAVE(); 706 #ifdef LOCK_PROFILING_SHARED 707 lock_profile_obtain_lock_failed( 708 &sx->lock_object, &contested, &waittime); 709 #endif 710 while (SX_OWNER(sx->sx_lock) == x && 711 TD_IS_RUNNING(owner)) 712 cpu_spinwait(); 713 continue; 714 } 715 } 716 #endif 717 718 /* 719 * Some other thread already has an exclusive lock, so 720 * start the process of blocking. 721 */ 722 sleepq_lock(&sx->lock_object); 723 x = sx->sx_lock; 724 725 /* 726 * The lock could have been released while we spun. 727 * In this case loop back and retry. 728 */ 729 if (x & SX_LOCK_SHARED) { 730 sleepq_release(&sx->lock_object); 731 continue; 732 } 733 734 #ifdef ADAPTIVE_SX 735 /* 736 * If the owner is running on another CPU, spin until 737 * the owner stops running or the state of the lock 738 * changes. 739 */ 740 if (!(x & SX_LOCK_SHARED) && 741 (sx->lock_object.lo_flags & SX_ADAPTIVESPIN)) { 742 owner = (struct thread *)SX_OWNER(x); 743 if (TD_IS_RUNNING(owner)) { 744 sleepq_release(&sx->lock_object); 745 continue; 746 } 747 } 748 #endif 749 750 /* 751 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 752 * fail to set it drop the sleep queue lock and loop 753 * back. 754 */ 755 if (!(x & SX_LOCK_SHARED_WAITERS)) { 756 if (!atomic_cmpset_ptr(&sx->sx_lock, x, 757 x | SX_LOCK_SHARED_WAITERS)) { 758 sleepq_release(&sx->lock_object); 759 continue; 760 } 761 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 762 CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 763 __func__, sx); 764 } 765 766 /* 767 * Since we have been unable to acquire the shared lock, 768 * we have to sleep. 769 */ 770 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 771 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 772 __func__, sx); 773 774 GIANT_SAVE(); 775 #ifdef LOCK_PROFILING_SHARED 776 lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 777 &waittime); 778 #endif 779 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 780 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 781 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 782 if (!(opts & SX_INTERRUPTIBLE)) 783 sleepq_wait(&sx->lock_object); 784 else 785 error = sleepq_wait_sig(&sx->lock_object); 786 787 if (error) { 788 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 789 CTR2(KTR_LOCK, 790 "%s: interruptible sleep by %p suspended by signal", 791 __func__, sx); 792 break; 793 } 794 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 795 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 796 __func__, sx); 797 } 798 799 GIANT_RESTORE(); 800 return (error); 801 } 802 803 /* 804 * This function represents the so-called 'hard case' for sx_sunlock 805 * operation. All 'easy case' failures are redirected to this. Note 806 * that ideally this would be a static function, but it needs to be 807 * accessible from at least sx.h. 808 */ 809 void 810 _sx_sunlock_hard(struct sx *sx, const char *file, int line) 811 { 812 uintptr_t x; 813 814 for (;;) { 815 x = sx->sx_lock; 816 817 /* 818 * We should never have sharers while at least one thread 819 * holds a shared lock. 820 */ 821 KASSERT(!(x & SX_LOCK_SHARED_WAITERS), 822 ("%s: waiting sharers", __func__)); 823 824 /* 825 * See if there is more than one shared lock held. If 826 * so, just drop one and return. 827 */ 828 if (SX_SHARERS(x) > 1) { 829 if (atomic_cmpset_ptr(&sx->sx_lock, x, 830 x - SX_ONE_SHARER)) { 831 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 832 CTR4(KTR_LOCK, 833 "%s: %p succeeded %p -> %p", 834 __func__, sx, (void *)x, 835 (void *)(x - SX_ONE_SHARER)); 836 break; 837 } 838 continue; 839 } 840 841 /* 842 * If there aren't any waiters for an exclusive lock, 843 * then try to drop it quickly. 844 */ 845 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 846 MPASS(x == SX_SHARERS_LOCK(1)); 847 if (atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1), 848 SX_LOCK_UNLOCKED)) { 849 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 850 CTR2(KTR_LOCK, "%s: %p last succeeded", 851 __func__, sx); 852 break; 853 } 854 continue; 855 } 856 857 /* 858 * At this point, there should just be one sharer with 859 * exclusive waiters. 860 */ 861 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS)); 862 863 sleepq_lock(&sx->lock_object); 864 865 /* 866 * Wake up semantic here is quite simple: 867 * Just wake up all the exclusive waiters. 868 * Note that the state of the lock could have changed, 869 * so if it fails loop back and retry. 870 */ 871 if (!atomic_cmpset_ptr(&sx->sx_lock, 872 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS, 873 SX_LOCK_UNLOCKED)) { 874 sleepq_release(&sx->lock_object); 875 continue; 876 } 877 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 878 CTR2(KTR_LOCK, "%s: %p waking up all thread on" 879 "exclusive queue", __func__, sx); 880 sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, -1, 881 SQ_EXCLUSIVE_QUEUE); 882 break; 883 } 884 } 885 886 #ifdef INVARIANT_SUPPORT 887 #ifndef INVARIANTS 888 #undef _sx_assert 889 #endif 890 891 /* 892 * In the non-WITNESS case, sx_assert() can only detect that at least 893 * *some* thread owns an slock, but it cannot guarantee that *this* 894 * thread owns an slock. 895 */ 896 void 897 _sx_assert(struct sx *sx, int what, const char *file, int line) 898 { 899 #ifndef WITNESS 900 int slocked = 0; 901 #endif 902 903 if (panicstr != NULL) 904 return; 905 switch (what) { 906 case SA_SLOCKED: 907 case SA_SLOCKED | SA_NOTRECURSED: 908 case SA_SLOCKED | SA_RECURSED: 909 #ifndef WITNESS 910 slocked = 1; 911 /* FALLTHROUGH */ 912 #endif 913 case SA_LOCKED: 914 case SA_LOCKED | SA_NOTRECURSED: 915 case SA_LOCKED | SA_RECURSED: 916 #ifdef WITNESS 917 witness_assert(&sx->lock_object, what, file, line); 918 #else 919 /* 920 * If some other thread has an exclusive lock or we 921 * have one and are asserting a shared lock, fail. 922 * Also, if no one has a lock at all, fail. 923 */ 924 if (sx->sx_lock == SX_LOCK_UNLOCKED || 925 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 926 sx_xholder(sx) != curthread))) 927 panic("Lock %s not %slocked @ %s:%d\n", 928 sx->lock_object.lo_name, slocked ? "share " : "", 929 file, line); 930 931 if (!(sx->sx_lock & SX_LOCK_SHARED)) { 932 if (sx_recursed(sx)) { 933 if (what & SA_NOTRECURSED) 934 panic("Lock %s recursed @ %s:%d\n", 935 sx->lock_object.lo_name, file, 936 line); 937 } else if (what & SA_RECURSED) 938 panic("Lock %s not recursed @ %s:%d\n", 939 sx->lock_object.lo_name, file, line); 940 } 941 #endif 942 break; 943 case SA_XLOCKED: 944 case SA_XLOCKED | SA_NOTRECURSED: 945 case SA_XLOCKED | SA_RECURSED: 946 if (sx_xholder(sx) != curthread) 947 panic("Lock %s not exclusively locked @ %s:%d\n", 948 sx->lock_object.lo_name, file, line); 949 if (sx_recursed(sx)) { 950 if (what & SA_NOTRECURSED) 951 panic("Lock %s recursed @ %s:%d\n", 952 sx->lock_object.lo_name, file, line); 953 } else if (what & SA_RECURSED) 954 panic("Lock %s not recursed @ %s:%d\n", 955 sx->lock_object.lo_name, file, line); 956 break; 957 case SA_UNLOCKED: 958 #ifdef WITNESS 959 witness_assert(&sx->lock_object, what, file, line); 960 #else 961 /* 962 * If we hold an exclusve lock fail. We can't 963 * reliably check to see if we hold a shared lock or 964 * not. 965 */ 966 if (sx_xholder(sx) == curthread) 967 panic("Lock %s exclusively locked @ %s:%d\n", 968 sx->lock_object.lo_name, file, line); 969 #endif 970 break; 971 default: 972 panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 973 line); 974 } 975 } 976 #endif /* INVARIANT_SUPPORT */ 977 978 #ifdef DDB 979 static void 980 db_show_sx(struct lock_object *lock) 981 { 982 struct thread *td; 983 struct sx *sx; 984 985 sx = (struct sx *)lock; 986 987 db_printf(" state: "); 988 if (sx->sx_lock == SX_LOCK_UNLOCKED) 989 db_printf("UNLOCKED\n"); 990 else if (sx->sx_lock == SX_LOCK_DESTROYED) { 991 db_printf("DESTROYED\n"); 992 return; 993 } else if (sx->sx_lock & SX_LOCK_SHARED) 994 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 995 else { 996 td = sx_xholder(sx); 997 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 998 td->td_tid, td->td_proc->p_pid, td->td_name); 999 if (sx_recursed(sx)) 1000 db_printf(" recursed: %d\n", sx->sx_recurse); 1001 } 1002 1003 db_printf(" waiters: "); 1004 switch(sx->sx_lock & 1005 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 1006 case SX_LOCK_SHARED_WAITERS: 1007 db_printf("shared\n"); 1008 break; 1009 case SX_LOCK_EXCLUSIVE_WAITERS: 1010 db_printf("exclusive\n"); 1011 break; 1012 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 1013 db_printf("exclusive and shared\n"); 1014 break; 1015 default: 1016 db_printf("none\n"); 1017 } 1018 } 1019 1020 /* 1021 * Check to see if a thread that is blocked on a sleep queue is actually 1022 * blocked on an sx lock. If so, output some details and return true. 1023 * If the lock has an exclusive owner, return that in *ownerp. 1024 */ 1025 int 1026 sx_chain(struct thread *td, struct thread **ownerp) 1027 { 1028 struct sx *sx; 1029 1030 /* 1031 * Check to see if this thread is blocked on an sx lock. 1032 * First, we check the lock class. If that is ok, then we 1033 * compare the lock name against the wait message. 1034 */ 1035 sx = td->td_wchan; 1036 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 1037 sx->lock_object.lo_name != td->td_wmesg) 1038 return (0); 1039 1040 /* We think we have an sx lock, so output some details. */ 1041 db_printf("blocked on sx \"%s\" ", td->td_wmesg); 1042 *ownerp = sx_xholder(sx); 1043 if (sx->sx_lock & SX_LOCK_SHARED) 1044 db_printf("SLOCK (count %ju)\n", 1045 (uintmax_t)SX_SHARERS(sx->sx_lock)); 1046 else 1047 db_printf("XLOCK\n"); 1048 return (1); 1049 } 1050 #endif 1051