1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org> 5 * Copyright (c) 2001 Jason Evans <jasone@freebsd.org> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice(s), this list of conditions and the following disclaimer as 13 * the first lines of this file unmodified other than the possible 14 * addition of one or more copyright notices. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice(s), this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 29 * DAMAGE. 30 */ 31 32 /* 33 * Shared/exclusive locks. This implementation attempts to ensure 34 * deterministic lock granting behavior, so that slocks and xlocks are 35 * interleaved. 36 * 37 * Priority propagation will not generally raise the priority of lock holders, 38 * so should not be relied upon in combination with sx locks. 39 */ 40 41 #include "opt_ddb.h" 42 #include "opt_hwpmc_hooks.h" 43 #include "opt_no_adaptive_sx.h" 44 45 #include <sys/cdefs.h> 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/kdb.h> 49 #include <sys/kernel.h> 50 #include <sys/ktr.h> 51 #include <sys/lock.h> 52 #include <sys/mutex.h> 53 #include <sys/proc.h> 54 #include <sys/sched.h> 55 #include <sys/sleepqueue.h> 56 #include <sys/sx.h> 57 #include <sys/smp.h> 58 #include <sys/sysctl.h> 59 60 #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 61 #include <machine/cpu.h> 62 #endif 63 64 #ifdef DDB 65 #include <ddb/ddb.h> 66 #endif 67 68 #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 69 #define ADAPTIVE_SX 70 #endif 71 72 #ifdef HWPMC_HOOKS 73 #include <sys/pmckern.h> 74 PMC_SOFT_DECLARE( , , lock, failed); 75 #endif 76 77 /* Handy macros for sleep queues. */ 78 #define SQ_EXCLUSIVE_QUEUE 0 79 #define SQ_SHARED_QUEUE 1 80 81 /* 82 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We 83 * drop Giant anytime we have to sleep or if we adaptively spin. 84 */ 85 #define GIANT_DECLARE \ 86 int _giantcnt = 0; \ 87 WITNESS_SAVE_DECL(Giant) \ 88 89 #define GIANT_SAVE(work) do { \ 90 if (__predict_false(mtx_owned(&Giant))) { \ 91 work++; \ 92 WITNESS_SAVE(&Giant.lock_object, Giant); \ 93 while (mtx_owned(&Giant)) { \ 94 _giantcnt++; \ 95 mtx_unlock(&Giant); \ 96 } \ 97 } \ 98 } while (0) 99 100 #define GIANT_RESTORE() do { \ 101 if (_giantcnt > 0) { \ 102 mtx_assert(&Giant, MA_NOTOWNED); \ 103 while (_giantcnt--) \ 104 mtx_lock(&Giant); \ 105 WITNESS_RESTORE(&Giant.lock_object, Giant); \ 106 } \ 107 } while (0) 108 109 /* 110 * Returns true if an exclusive lock is recursed. It assumes 111 * curthread currently has an exclusive lock. 112 */ 113 #define sx_recursed(sx) ((sx)->sx_recurse != 0) 114 115 static void assert_sx(const struct lock_object *lock, int what); 116 #ifdef DDB 117 static void db_show_sx(const struct lock_object *lock); 118 #endif 119 static void lock_sx(struct lock_object *lock, uintptr_t how); 120 #ifdef KDTRACE_HOOKS 121 static int owner_sx(const struct lock_object *lock, struct thread **owner); 122 #endif 123 static uintptr_t unlock_sx(struct lock_object *lock); 124 125 struct lock_class lock_class_sx = { 126 .lc_name = "sx", 127 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, 128 .lc_assert = assert_sx, 129 #ifdef DDB 130 .lc_ddb_show = db_show_sx, 131 #endif 132 .lc_lock = lock_sx, 133 .lc_unlock = unlock_sx, 134 #ifdef KDTRACE_HOOKS 135 .lc_owner = owner_sx, 136 #endif 137 }; 138 139 #ifndef INVARIANTS 140 #define _sx_assert(sx, what, file, line) 141 #endif 142 143 #ifdef ADAPTIVE_SX 144 #ifdef SX_CUSTOM_BACKOFF 145 static u_short __read_frequently asx_retries; 146 static u_short __read_frequently asx_loops; 147 static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 148 "sxlock debugging"); 149 SYSCTL_U16(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, ""); 150 SYSCTL_U16(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, ""); 151 152 static struct lock_delay_config __read_frequently sx_delay; 153 154 SYSCTL_U16(_debug_sx, OID_AUTO, delay_base, CTLFLAG_RW, &sx_delay.base, 155 0, ""); 156 SYSCTL_U16(_debug_sx, OID_AUTO, delay_max, CTLFLAG_RW, &sx_delay.max, 157 0, ""); 158 159 static void 160 sx_lock_delay_init(void *arg __unused) 161 { 162 163 lock_delay_default_init(&sx_delay); 164 asx_retries = 10; 165 asx_loops = max(10000, sx_delay.max); 166 } 167 LOCK_DELAY_SYSINIT(sx_lock_delay_init); 168 #else 169 #define sx_delay locks_delay 170 #define asx_retries locks_delay_retries 171 #define asx_loops locks_delay_loops 172 #endif 173 #endif 174 175 void 176 assert_sx(const struct lock_object *lock, int what) 177 { 178 179 sx_assert((const struct sx *)lock, what); 180 } 181 182 void 183 lock_sx(struct lock_object *lock, uintptr_t how) 184 { 185 struct sx *sx; 186 187 sx = (struct sx *)lock; 188 if (how) 189 sx_slock(sx); 190 else 191 sx_xlock(sx); 192 } 193 194 uintptr_t 195 unlock_sx(struct lock_object *lock) 196 { 197 struct sx *sx; 198 199 sx = (struct sx *)lock; 200 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED); 201 if (sx_xlocked(sx)) { 202 sx_xunlock(sx); 203 return (0); 204 } else { 205 sx_sunlock(sx); 206 return (1); 207 } 208 } 209 210 #ifdef KDTRACE_HOOKS 211 int 212 owner_sx(const struct lock_object *lock, struct thread **owner) 213 { 214 const struct sx *sx; 215 uintptr_t x; 216 217 sx = (const struct sx *)lock; 218 x = sx->sx_lock; 219 *owner = NULL; 220 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) : 221 ((*owner = (struct thread *)SX_OWNER(x)) != NULL)); 222 } 223 #endif 224 225 void 226 sx_sysinit(void *arg) 227 { 228 struct sx_args *sargs = arg; 229 230 sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags); 231 } 232 233 void 234 sx_init_flags(struct sx *sx, const char *description, int opts) 235 { 236 int flags; 237 238 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK | 239 SX_NOPROFILE | SX_NEW)) == 0); 240 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock, 241 ("%s: sx_lock not aligned for %s: %p", __func__, description, 242 &sx->sx_lock)); 243 244 flags = LO_SLEEPABLE | LO_UPGRADABLE; 245 if (opts & SX_DUPOK) 246 flags |= LO_DUPOK; 247 if (opts & SX_NOPROFILE) 248 flags |= LO_NOPROFILE; 249 if (!(opts & SX_NOWITNESS)) 250 flags |= LO_WITNESS; 251 if (opts & SX_RECURSE) 252 flags |= LO_RECURSABLE; 253 if (opts & SX_QUIET) 254 flags |= LO_QUIET; 255 if (opts & SX_NEW) 256 flags |= LO_NEW; 257 258 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags); 259 sx->sx_lock = SX_LOCK_UNLOCKED; 260 sx->sx_recurse = 0; 261 } 262 263 void 264 sx_destroy(struct sx *sx) 265 { 266 267 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held")); 268 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed")); 269 sx->sx_lock = SX_LOCK_DESTROYED; 270 lock_destroy(&sx->lock_object); 271 } 272 273 int 274 sx_try_slock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 275 { 276 uintptr_t x; 277 278 if (SCHEDULER_STOPPED()) 279 return (1); 280 281 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 282 ("sx_try_slock() by idle thread %p on sx %s @ %s:%d", 283 curthread, sx->lock_object.lo_name, file, line)); 284 285 x = sx->sx_lock; 286 for (;;) { 287 KASSERT(x != SX_LOCK_DESTROYED, 288 ("sx_try_slock() of destroyed sx @ %s:%d", file, line)); 289 if (!(x & SX_LOCK_SHARED)) 290 break; 291 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, x + SX_ONE_SHARER)) { 292 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line); 293 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line); 294 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, 295 sx, 0, 0, file, line, LOCKSTAT_READER); 296 TD_LOCKS_INC(curthread); 297 curthread->td_sx_slocks++; 298 return (1); 299 } 300 } 301 302 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line); 303 return (0); 304 } 305 306 int 307 sx_try_slock_(struct sx *sx, const char *file, int line) 308 { 309 310 return (sx_try_slock_int(sx LOCK_FILE_LINE_ARG)); 311 } 312 313 int 314 _sx_xlock(struct sx *sx, int opts, const char *file, int line) 315 { 316 uintptr_t tid, x; 317 int error = 0; 318 319 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 320 !TD_IS_IDLETHREAD(curthread), 321 ("sx_xlock() by idle thread %p on sx %s @ %s:%d", 322 curthread, sx->lock_object.lo_name, file, line)); 323 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 324 ("sx_xlock() of destroyed sx @ %s:%d", file, line)); 325 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 326 line, NULL); 327 tid = (uintptr_t)curthread; 328 x = SX_LOCK_UNLOCKED; 329 if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 330 error = _sx_xlock_hard(sx, x, opts LOCK_FILE_LINE_ARG); 331 else 332 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 333 0, 0, file, line, LOCKSTAT_WRITER); 334 if (!error) { 335 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, 336 file, line); 337 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 338 TD_LOCKS_INC(curthread); 339 } 340 341 return (error); 342 } 343 344 int 345 sx_try_xlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 346 { 347 struct thread *td; 348 uintptr_t tid, x; 349 int rval; 350 bool recursed; 351 352 td = curthread; 353 tid = (uintptr_t)td; 354 if (SCHEDULER_STOPPED_TD(td)) 355 return (1); 356 357 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td), 358 ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d", 359 curthread, sx->lock_object.lo_name, file, line)); 360 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 361 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line)); 362 363 rval = 1; 364 recursed = false; 365 x = SX_LOCK_UNLOCKED; 366 for (;;) { 367 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 368 break; 369 if (x == SX_LOCK_UNLOCKED) 370 continue; 371 if (x == tid && (sx->lock_object.lo_flags & LO_RECURSABLE)) { 372 sx->sx_recurse++; 373 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 374 break; 375 } 376 rval = 0; 377 break; 378 } 379 380 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line); 381 if (rval) { 382 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 383 file, line); 384 if (!recursed) 385 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, 386 sx, 0, 0, file, line, LOCKSTAT_WRITER); 387 TD_LOCKS_INC(curthread); 388 } 389 390 return (rval); 391 } 392 393 int 394 sx_try_xlock_(struct sx *sx, const char *file, int line) 395 { 396 397 return (sx_try_xlock_int(sx LOCK_FILE_LINE_ARG)); 398 } 399 400 void 401 _sx_xunlock(struct sx *sx, const char *file, int line) 402 { 403 404 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 405 ("sx_xunlock() of destroyed sx @ %s:%d", file, line)); 406 _sx_assert(sx, SA_XLOCKED, file, line); 407 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 408 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file, 409 line); 410 #if LOCK_DEBUG > 0 411 _sx_xunlock_hard(sx, (uintptr_t)curthread, file, line); 412 #else 413 __sx_xunlock(sx, curthread, file, line); 414 #endif 415 TD_LOCKS_DEC(curthread); 416 } 417 418 /* 419 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock. 420 * This will only succeed if this thread holds a single shared lock. 421 * Return 1 if if the upgrade succeed, 0 otherwise. 422 */ 423 int 424 sx_try_upgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 425 { 426 uintptr_t x; 427 uintptr_t waiters; 428 int success; 429 430 if (SCHEDULER_STOPPED()) 431 return (1); 432 433 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 434 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 435 _sx_assert(sx, SA_SLOCKED, file, line); 436 437 /* 438 * Try to switch from one shared lock to an exclusive lock. We need 439 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 440 * we will wake up the exclusive waiters when we drop the lock. 441 */ 442 success = 0; 443 x = SX_READ_VALUE(sx); 444 for (;;) { 445 if (SX_SHARERS(x) > 1) 446 break; 447 waiters = (x & SX_LOCK_WAITERS); 448 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, 449 (uintptr_t)curthread | waiters)) { 450 success = 1; 451 break; 452 } 453 } 454 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 455 if (success) { 456 curthread->td_sx_slocks--; 457 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 458 file, line); 459 LOCKSTAT_RECORD0(sx__upgrade, sx); 460 } 461 return (success); 462 } 463 464 int 465 sx_try_upgrade_(struct sx *sx, const char *file, int line) 466 { 467 468 return (sx_try_upgrade_int(sx LOCK_FILE_LINE_ARG)); 469 } 470 471 /* 472 * Downgrade an unrecursed exclusive lock into a single shared lock. 473 */ 474 void 475 sx_downgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 476 { 477 uintptr_t x; 478 int wakeup_swapper; 479 480 if (SCHEDULER_STOPPED()) 481 return; 482 483 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 484 ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 485 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 486 #ifndef INVARIANTS 487 if (sx_recursed(sx)) 488 panic("downgrade of a recursed lock"); 489 #endif 490 491 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 492 493 /* 494 * Try to switch from an exclusive lock with no shared waiters 495 * to one sharer with no shared waiters. If there are 496 * exclusive waiters, we don't need to lock the sleep queue so 497 * long as we preserve the flag. We do one quick try and if 498 * that fails we grab the sleepq lock to keep the flags from 499 * changing and do it the slow way. 500 * 501 * We have to lock the sleep queue if there are shared waiters 502 * so we can wake them up. 503 */ 504 x = sx->sx_lock; 505 if (!(x & SX_LOCK_SHARED_WAITERS) && 506 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 507 (x & SX_LOCK_EXCLUSIVE_WAITERS))) 508 goto out; 509 510 /* 511 * Lock the sleep queue so we can read the waiters bits 512 * without any races and wakeup any shared waiters. 513 */ 514 sleepq_lock(&sx->lock_object); 515 516 /* 517 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 518 * shared lock. If there are any shared waiters, wake them up. 519 */ 520 wakeup_swapper = 0; 521 x = sx->sx_lock; 522 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 523 (x & SX_LOCK_EXCLUSIVE_WAITERS)); 524 if (x & SX_LOCK_SHARED_WAITERS) 525 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 526 0, SQ_SHARED_QUEUE); 527 sleepq_release(&sx->lock_object); 528 529 if (wakeup_swapper) 530 kick_proc0(); 531 532 out: 533 curthread->td_sx_slocks++; 534 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 535 LOCKSTAT_RECORD0(sx__downgrade, sx); 536 } 537 538 void 539 sx_downgrade_(struct sx *sx, const char *file, int line) 540 { 541 542 sx_downgrade_int(sx LOCK_FILE_LINE_ARG); 543 } 544 545 #ifdef ADAPTIVE_SX 546 static inline void 547 sx_drop_critical(uintptr_t x, bool *in_critical, int *extra_work) 548 { 549 550 if (x & SX_LOCK_WRITE_SPINNER) 551 return; 552 if (*in_critical) { 553 critical_exit(); 554 *in_critical = false; 555 (*extra_work)--; 556 } 557 } 558 #else 559 #define sx_drop_critical(x, in_critical, extra_work) do { } while (0) 560 #endif 561 562 /* 563 * This function represents the so-called 'hard case' for sx_xlock 564 * operation. All 'easy case' failures are redirected to this. Note 565 * that ideally this would be a static function, but it needs to be 566 * accessible from at least sx.h. 567 */ 568 int 569 _sx_xlock_hard(struct sx *sx, uintptr_t x, int opts LOCK_FILE_LINE_ARG_DEF) 570 { 571 GIANT_DECLARE; 572 uintptr_t tid, setx; 573 #ifdef ADAPTIVE_SX 574 struct thread *owner; 575 u_int i, n, spintries = 0; 576 enum { READERS, WRITER } sleep_reason = READERS; 577 bool in_critical = false; 578 #endif 579 #ifdef LOCK_PROFILING 580 uint64_t waittime = 0; 581 int contested = 0; 582 #endif 583 int error = 0; 584 #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 585 struct lock_delay_arg lda; 586 #endif 587 #ifdef KDTRACE_HOOKS 588 u_int sleep_cnt = 0; 589 int64_t sleep_time = 0; 590 int64_t all_time = 0; 591 #endif 592 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 593 uintptr_t state = 0; 594 int doing_lockprof = 0; 595 #endif 596 int extra_work = 0; 597 598 tid = (uintptr_t)curthread; 599 600 #ifdef KDTRACE_HOOKS 601 if (LOCKSTAT_PROFILE_ENABLED(sx__acquire)) { 602 while (x == SX_LOCK_UNLOCKED) { 603 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 604 goto out_lockstat; 605 } 606 extra_work = 1; 607 doing_lockprof = 1; 608 all_time -= lockstat_nsecs(&sx->lock_object); 609 state = x; 610 } 611 #endif 612 #ifdef LOCK_PROFILING 613 extra_work = 1; 614 doing_lockprof = 1; 615 state = x; 616 #endif 617 618 if (SCHEDULER_STOPPED()) 619 return (0); 620 621 if (__predict_false(x == SX_LOCK_UNLOCKED)) 622 x = SX_READ_VALUE(sx); 623 624 /* If we already hold an exclusive lock, then recurse. */ 625 if (__predict_false(lv_sx_owner(x) == (struct thread *)tid)) { 626 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0, 627 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 628 sx->lock_object.lo_name, file, line)); 629 sx->sx_recurse++; 630 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 631 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 632 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 633 return (0); 634 } 635 636 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 637 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 638 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 639 640 #if defined(ADAPTIVE_SX) 641 lock_delay_arg_init(&lda, &sx_delay); 642 #elif defined(KDTRACE_HOOKS) 643 lock_delay_arg_init_noadapt(&lda); 644 #endif 645 646 #ifdef HWPMC_HOOKS 647 PMC_SOFT_CALL( , , lock, failed); 648 #endif 649 lock_profile_obtain_lock_failed(&sx->lock_object, false, &contested, 650 &waittime); 651 652 #ifndef INVARIANTS 653 GIANT_SAVE(extra_work); 654 #endif 655 656 THREAD_CONTENDS_ON_LOCK(&sx->lock_object); 657 658 for (;;) { 659 if (x == SX_LOCK_UNLOCKED) { 660 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 661 break; 662 continue; 663 } 664 #ifdef INVARIANTS 665 GIANT_SAVE(extra_work); 666 #endif 667 #ifdef KDTRACE_HOOKS 668 lda.spin_cnt++; 669 #endif 670 #ifdef ADAPTIVE_SX 671 if (x == (SX_LOCK_SHARED | SX_LOCK_WRITE_SPINNER)) { 672 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 673 break; 674 continue; 675 } 676 677 /* 678 * If the lock is write locked and the owner is 679 * running on another CPU, spin until the owner stops 680 * running or the state of the lock changes. 681 */ 682 if ((x & SX_LOCK_SHARED) == 0) { 683 sx_drop_critical(x, &in_critical, &extra_work); 684 sleep_reason = WRITER; 685 owner = lv_sx_owner(x); 686 if (!TD_IS_RUNNING(owner)) 687 goto sleepq; 688 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 689 CTR3(KTR_LOCK, "%s: spinning on %p held by %p", 690 __func__, sx, owner); 691 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread), 692 "spinning", "lockname:\"%s\"", 693 sx->lock_object.lo_name); 694 do { 695 lock_delay(&lda); 696 x = SX_READ_VALUE(sx); 697 owner = lv_sx_owner(x); 698 } while (owner != NULL && TD_IS_RUNNING(owner)); 699 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread), 700 "running"); 701 continue; 702 } else if (SX_SHARERS(x) > 0) { 703 sleep_reason = READERS; 704 if (spintries == asx_retries) 705 goto sleepq; 706 if (!(x & SX_LOCK_WRITE_SPINNER)) { 707 if (!in_critical) { 708 critical_enter(); 709 in_critical = true; 710 extra_work++; 711 } 712 if (!atomic_fcmpset_ptr(&sx->sx_lock, &x, 713 x | SX_LOCK_WRITE_SPINNER)) { 714 critical_exit(); 715 in_critical = false; 716 extra_work--; 717 continue; 718 } 719 } 720 spintries++; 721 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread), 722 "spinning", "lockname:\"%s\"", 723 sx->lock_object.lo_name); 724 n = SX_SHARERS(x); 725 for (i = 0; i < asx_loops; i += n) { 726 lock_delay_spin(n); 727 x = SX_READ_VALUE(sx); 728 if (!(x & SX_LOCK_WRITE_SPINNER)) 729 break; 730 if (!(x & SX_LOCK_SHARED)) 731 break; 732 n = SX_SHARERS(x); 733 if (n == 0) 734 break; 735 } 736 #ifdef KDTRACE_HOOKS 737 lda.spin_cnt += i; 738 #endif 739 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread), 740 "running"); 741 if (i < asx_loops) 742 continue; 743 } 744 sleepq: 745 #endif 746 sleepq_lock(&sx->lock_object); 747 x = SX_READ_VALUE(sx); 748 retry_sleepq: 749 750 /* 751 * If the lock was released while spinning on the 752 * sleep queue chain lock, try again. 753 */ 754 if (x == SX_LOCK_UNLOCKED) { 755 sleepq_release(&sx->lock_object); 756 sx_drop_critical(x, &in_critical, &extra_work); 757 continue; 758 } 759 760 #ifdef ADAPTIVE_SX 761 /* 762 * The current lock owner might have started executing 763 * on another CPU (or the lock could have changed 764 * owners) while we were waiting on the sleep queue 765 * chain lock. If so, drop the sleep queue lock and try 766 * again. 767 */ 768 if (!(x & SX_LOCK_SHARED)) { 769 owner = (struct thread *)SX_OWNER(x); 770 if (TD_IS_RUNNING(owner)) { 771 sleepq_release(&sx->lock_object); 772 sx_drop_critical(x, &in_critical, 773 &extra_work); 774 continue; 775 } 776 } else if (SX_SHARERS(x) > 0 && sleep_reason == WRITER) { 777 sleepq_release(&sx->lock_object); 778 sx_drop_critical(x, &in_critical, &extra_work); 779 continue; 780 } 781 #endif 782 783 /* 784 * If an exclusive lock was released with both shared 785 * and exclusive waiters and a shared waiter hasn't 786 * woken up and acquired the lock yet, sx_lock will be 787 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 788 * If we see that value, try to acquire it once. Note 789 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 790 * as there are other exclusive waiters still. If we 791 * fail, restart the loop. 792 */ 793 setx = x & (SX_LOCK_WAITERS | SX_LOCK_WRITE_SPINNER); 794 if ((x & ~setx) == SX_LOCK_SHARED) { 795 setx &= ~SX_LOCK_WRITE_SPINNER; 796 if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid | setx)) 797 goto retry_sleepq; 798 sleepq_release(&sx->lock_object); 799 CTR2(KTR_LOCK, "%s: %p claimed by new writer", 800 __func__, sx); 801 break; 802 } 803 804 #ifdef ADAPTIVE_SX 805 /* 806 * It is possible we set the SX_LOCK_WRITE_SPINNER bit. 807 * It is an invariant that when the bit is set, there is 808 * a writer ready to grab the lock. Thus clear the bit since 809 * we are going to sleep. 810 */ 811 if (in_critical) { 812 if ((x & SX_LOCK_WRITE_SPINNER) || 813 !((x & SX_LOCK_EXCLUSIVE_WAITERS))) { 814 setx = x & ~SX_LOCK_WRITE_SPINNER; 815 setx |= SX_LOCK_EXCLUSIVE_WAITERS; 816 if (!atomic_fcmpset_ptr(&sx->sx_lock, &x, 817 setx)) { 818 goto retry_sleepq; 819 } 820 } 821 critical_exit(); 822 in_critical = false; 823 } else { 824 #endif 825 /* 826 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 827 * than loop back and retry. 828 */ 829 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 830 if (!atomic_fcmpset_ptr(&sx->sx_lock, &x, 831 x | SX_LOCK_EXCLUSIVE_WAITERS)) { 832 goto retry_sleepq; 833 } 834 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 835 CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 836 __func__, sx); 837 } 838 #ifdef ADAPTIVE_SX 839 } 840 #endif 841 842 /* 843 * Since we have been unable to acquire the exclusive 844 * lock and the exclusive waiters flag is set, we have 845 * to sleep. 846 */ 847 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 848 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 849 __func__, sx); 850 851 #ifdef KDTRACE_HOOKS 852 sleep_time -= lockstat_nsecs(&sx->lock_object); 853 #endif 854 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 855 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 856 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 857 if (!(opts & SX_INTERRUPTIBLE)) 858 sleepq_wait(&sx->lock_object, 0); 859 else 860 error = sleepq_wait_sig(&sx->lock_object, 0); 861 #ifdef KDTRACE_HOOKS 862 sleep_time += lockstat_nsecs(&sx->lock_object); 863 sleep_cnt++; 864 #endif 865 if (error) { 866 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 867 CTR2(KTR_LOCK, 868 "%s: interruptible sleep by %p suspended by signal", 869 __func__, sx); 870 break; 871 } 872 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 873 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 874 __func__, sx); 875 x = SX_READ_VALUE(sx); 876 } 877 THREAD_CONTENTION_DONE(&sx->lock_object); 878 if (__predict_true(!extra_work)) 879 return (error); 880 #ifdef ADAPTIVE_SX 881 if (in_critical) 882 critical_exit(); 883 #endif 884 GIANT_RESTORE(); 885 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 886 if (__predict_true(!doing_lockprof)) 887 return (error); 888 #endif 889 #ifdef KDTRACE_HOOKS 890 all_time += lockstat_nsecs(&sx->lock_object); 891 if (sleep_time) 892 LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 893 LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 894 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 895 if (lda.spin_cnt > sleep_cnt) 896 LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 897 LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 898 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 899 out_lockstat: 900 #endif 901 if (!error) 902 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 903 contested, waittime, file, line, LOCKSTAT_WRITER); 904 return (error); 905 } 906 907 /* 908 * This function represents the so-called 'hard case' for sx_xunlock 909 * operation. All 'easy case' failures are redirected to this. Note 910 * that ideally this would be a static function, but it needs to be 911 * accessible from at least sx.h. 912 */ 913 void 914 _sx_xunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 915 { 916 uintptr_t tid, setx; 917 int queue, wakeup_swapper; 918 919 if (SCHEDULER_STOPPED()) 920 return; 921 922 tid = (uintptr_t)curthread; 923 924 if (__predict_false(x == tid)) 925 x = SX_READ_VALUE(sx); 926 927 MPASS(!(x & SX_LOCK_SHARED)); 928 929 if (__predict_false(x & SX_LOCK_RECURSED)) { 930 /* The lock is recursed, unrecurse one level. */ 931 if ((--sx->sx_recurse) == 0) 932 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 933 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 934 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 935 return; 936 } 937 938 LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_WRITER); 939 if (x == tid && 940 atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED)) 941 return; 942 943 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 944 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 945 946 sleepq_lock(&sx->lock_object); 947 x = SX_READ_VALUE(sx); 948 MPASS(x & (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)); 949 950 /* 951 * The wake up algorithm here is quite simple and probably not 952 * ideal. It gives precedence to shared waiters if they are 953 * present. For this condition, we have to preserve the 954 * state of the exclusive waiters flag. 955 * If interruptible sleeps left the shared queue empty avoid a 956 * starvation for the threads sleeping on the exclusive queue by giving 957 * them precedence and cleaning up the shared waiters bit anyway. 958 */ 959 setx = SX_LOCK_UNLOCKED; 960 queue = SQ_SHARED_QUEUE; 961 if ((x & SX_LOCK_EXCLUSIVE_WAITERS) != 0 && 962 sleepq_sleepcnt(&sx->lock_object, SQ_EXCLUSIVE_QUEUE) != 0) { 963 queue = SQ_EXCLUSIVE_QUEUE; 964 setx |= (x & SX_LOCK_SHARED_WAITERS); 965 } 966 atomic_store_rel_ptr(&sx->sx_lock, setx); 967 968 /* Wake up all the waiters for the specific queue. */ 969 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 970 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 971 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 972 "exclusive"); 973 974 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 975 queue); 976 sleepq_release(&sx->lock_object); 977 if (wakeup_swapper) 978 kick_proc0(); 979 } 980 981 static bool __always_inline 982 __sx_can_read(struct thread *td, uintptr_t x, bool fp) 983 { 984 985 if ((x & (SX_LOCK_SHARED | SX_LOCK_EXCLUSIVE_WAITERS | SX_LOCK_WRITE_SPINNER)) 986 == SX_LOCK_SHARED) 987 return (true); 988 if (!fp && td->td_sx_slocks && (x & SX_LOCK_SHARED)) 989 return (true); 990 return (false); 991 } 992 993 static bool __always_inline 994 __sx_slock_try(struct sx *sx, struct thread *td, uintptr_t *xp, bool fp 995 LOCK_FILE_LINE_ARG_DEF) 996 { 997 998 /* 999 * If no other thread has an exclusive lock then try to bump up 1000 * the count of sharers. Since we have to preserve the state 1001 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 1002 * shared lock loop back and retry. 1003 */ 1004 while (__sx_can_read(td, *xp, fp)) { 1005 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, xp, 1006 *xp + SX_ONE_SHARER)) { 1007 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1008 CTR4(KTR_LOCK, "%s: %p succeed %p -> %p", 1009 __func__, sx, (void *)*xp, 1010 (void *)(*xp + SX_ONE_SHARER)); 1011 td->td_sx_slocks++; 1012 return (true); 1013 } 1014 } 1015 return (false); 1016 } 1017 1018 static int __noinline 1019 _sx_slock_hard(struct sx *sx, int opts, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 1020 { 1021 GIANT_DECLARE; 1022 struct thread *td; 1023 #ifdef ADAPTIVE_SX 1024 struct thread *owner; 1025 u_int i, n, spintries = 0; 1026 #endif 1027 #ifdef LOCK_PROFILING 1028 uint64_t waittime = 0; 1029 int contested = 0; 1030 #endif 1031 int error = 0; 1032 #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 1033 struct lock_delay_arg lda; 1034 #endif 1035 #ifdef KDTRACE_HOOKS 1036 u_int sleep_cnt = 0; 1037 int64_t sleep_time = 0; 1038 int64_t all_time = 0; 1039 #endif 1040 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 1041 uintptr_t state = 0; 1042 #endif 1043 int extra_work __sdt_used = 0; 1044 1045 td = curthread; 1046 1047 #ifdef KDTRACE_HOOKS 1048 if (LOCKSTAT_PROFILE_ENABLED(sx__acquire)) { 1049 if (__sx_slock_try(sx, td, &x, false LOCK_FILE_LINE_ARG)) 1050 goto out_lockstat; 1051 extra_work = 1; 1052 all_time -= lockstat_nsecs(&sx->lock_object); 1053 state = x; 1054 } 1055 #endif 1056 #ifdef LOCK_PROFILING 1057 extra_work = 1; 1058 state = x; 1059 #endif 1060 1061 if (SCHEDULER_STOPPED()) 1062 return (0); 1063 1064 #if defined(ADAPTIVE_SX) 1065 lock_delay_arg_init(&lda, &sx_delay); 1066 #elif defined(KDTRACE_HOOKS) 1067 lock_delay_arg_init_noadapt(&lda); 1068 #endif 1069 1070 #ifdef HWPMC_HOOKS 1071 PMC_SOFT_CALL( , , lock, failed); 1072 #endif 1073 lock_profile_obtain_lock_failed(&sx->lock_object, false, &contested, 1074 &waittime); 1075 1076 #ifndef INVARIANTS 1077 GIANT_SAVE(extra_work); 1078 #endif 1079 1080 THREAD_CONTENDS_ON_LOCK(&sx->lock_object); 1081 1082 /* 1083 * As with rwlocks, we don't make any attempt to try to block 1084 * shared locks once there is an exclusive waiter. 1085 */ 1086 for (;;) { 1087 if (__sx_slock_try(sx, td, &x, false LOCK_FILE_LINE_ARG)) 1088 break; 1089 #ifdef INVARIANTS 1090 GIANT_SAVE(extra_work); 1091 #endif 1092 #ifdef KDTRACE_HOOKS 1093 lda.spin_cnt++; 1094 #endif 1095 1096 #ifdef ADAPTIVE_SX 1097 /* 1098 * If the owner is running on another CPU, spin until 1099 * the owner stops running or the state of the lock 1100 * changes. 1101 */ 1102 if ((x & SX_LOCK_SHARED) == 0) { 1103 owner = lv_sx_owner(x); 1104 if (TD_IS_RUNNING(owner)) { 1105 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1106 CTR3(KTR_LOCK, 1107 "%s: spinning on %p held by %p", 1108 __func__, sx, owner); 1109 KTR_STATE1(KTR_SCHED, "thread", 1110 sched_tdname(curthread), "spinning", 1111 "lockname:\"%s\"", sx->lock_object.lo_name); 1112 do { 1113 lock_delay(&lda); 1114 x = SX_READ_VALUE(sx); 1115 owner = lv_sx_owner(x); 1116 } while (owner != NULL && TD_IS_RUNNING(owner)); 1117 KTR_STATE0(KTR_SCHED, "thread", 1118 sched_tdname(curthread), "running"); 1119 continue; 1120 } 1121 } else { 1122 if ((x & SX_LOCK_WRITE_SPINNER) && SX_SHARERS(x) == 0) { 1123 MPASS(!__sx_can_read(td, x, false)); 1124 lock_delay_spin(2); 1125 x = SX_READ_VALUE(sx); 1126 continue; 1127 } 1128 if (spintries < asx_retries) { 1129 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread), 1130 "spinning", "lockname:\"%s\"", 1131 sx->lock_object.lo_name); 1132 n = SX_SHARERS(x); 1133 for (i = 0; i < asx_loops; i += n) { 1134 lock_delay_spin(n); 1135 x = SX_READ_VALUE(sx); 1136 if (!(x & SX_LOCK_SHARED)) 1137 break; 1138 n = SX_SHARERS(x); 1139 if (n == 0) 1140 break; 1141 if (__sx_can_read(td, x, false)) 1142 break; 1143 } 1144 #ifdef KDTRACE_HOOKS 1145 lda.spin_cnt += i; 1146 #endif 1147 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread), 1148 "running"); 1149 if (i < asx_loops) 1150 continue; 1151 } 1152 } 1153 #endif 1154 1155 /* 1156 * Some other thread already has an exclusive lock, so 1157 * start the process of blocking. 1158 */ 1159 sleepq_lock(&sx->lock_object); 1160 x = SX_READ_VALUE(sx); 1161 retry_sleepq: 1162 if (((x & SX_LOCK_WRITE_SPINNER) && SX_SHARERS(x) == 0) || 1163 __sx_can_read(td, x, false)) { 1164 sleepq_release(&sx->lock_object); 1165 continue; 1166 } 1167 1168 #ifdef ADAPTIVE_SX 1169 /* 1170 * If the owner is running on another CPU, spin until 1171 * the owner stops running or the state of the lock 1172 * changes. 1173 */ 1174 if (!(x & SX_LOCK_SHARED)) { 1175 owner = (struct thread *)SX_OWNER(x); 1176 if (TD_IS_RUNNING(owner)) { 1177 sleepq_release(&sx->lock_object); 1178 x = SX_READ_VALUE(sx); 1179 continue; 1180 } 1181 } 1182 #endif 1183 1184 /* 1185 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 1186 * fail to set it drop the sleep queue lock and loop 1187 * back. 1188 */ 1189 if (!(x & SX_LOCK_SHARED_WAITERS)) { 1190 if (!atomic_fcmpset_ptr(&sx->sx_lock, &x, 1191 x | SX_LOCK_SHARED_WAITERS)) 1192 goto retry_sleepq; 1193 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1194 CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 1195 __func__, sx); 1196 } 1197 1198 /* 1199 * Since we have been unable to acquire the shared lock, 1200 * we have to sleep. 1201 */ 1202 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1203 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 1204 __func__, sx); 1205 1206 #ifdef KDTRACE_HOOKS 1207 sleep_time -= lockstat_nsecs(&sx->lock_object); 1208 #endif 1209 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 1210 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 1211 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 1212 if (!(opts & SX_INTERRUPTIBLE)) 1213 sleepq_wait(&sx->lock_object, 0); 1214 else 1215 error = sleepq_wait_sig(&sx->lock_object, 0); 1216 #ifdef KDTRACE_HOOKS 1217 sleep_time += lockstat_nsecs(&sx->lock_object); 1218 sleep_cnt++; 1219 #endif 1220 if (error) { 1221 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1222 CTR2(KTR_LOCK, 1223 "%s: interruptible sleep by %p suspended by signal", 1224 __func__, sx); 1225 break; 1226 } 1227 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1228 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 1229 __func__, sx); 1230 x = SX_READ_VALUE(sx); 1231 } 1232 THREAD_CONTENTION_DONE(&sx->lock_object); 1233 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 1234 if (__predict_true(!extra_work)) 1235 return (error); 1236 #endif 1237 #ifdef KDTRACE_HOOKS 1238 all_time += lockstat_nsecs(&sx->lock_object); 1239 if (sleep_time) 1240 LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 1241 LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1242 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 1243 if (lda.spin_cnt > sleep_cnt) 1244 LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 1245 LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1246 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 1247 out_lockstat: 1248 #endif 1249 if (error == 0) { 1250 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 1251 contested, waittime, file, line, LOCKSTAT_READER); 1252 } 1253 GIANT_RESTORE(); 1254 return (error); 1255 } 1256 1257 int 1258 _sx_slock_int(struct sx *sx, int opts LOCK_FILE_LINE_ARG_DEF) 1259 { 1260 struct thread *td; 1261 uintptr_t x; 1262 int error; 1263 1264 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 1265 !TD_IS_IDLETHREAD(curthread), 1266 ("sx_slock() by idle thread %p on sx %s @ %s:%d", 1267 curthread, sx->lock_object.lo_name, file, line)); 1268 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1269 ("sx_slock() of destroyed sx @ %s:%d", file, line)); 1270 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 1271 1272 error = 0; 1273 td = curthread; 1274 x = SX_READ_VALUE(sx); 1275 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(sx__acquire) || 1276 !__sx_slock_try(sx, td, &x, true LOCK_FILE_LINE_ARG))) 1277 error = _sx_slock_hard(sx, opts, x LOCK_FILE_LINE_ARG); 1278 else 1279 lock_profile_obtain_lock_success(&sx->lock_object, false, 0, 0, 1280 file, line); 1281 if (error == 0) { 1282 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 1283 WITNESS_LOCK(&sx->lock_object, 0, file, line); 1284 TD_LOCKS_INC(curthread); 1285 } 1286 return (error); 1287 } 1288 1289 int 1290 _sx_slock(struct sx *sx, int opts, const char *file, int line) 1291 { 1292 1293 return (_sx_slock_int(sx, opts LOCK_FILE_LINE_ARG)); 1294 } 1295 1296 static bool __always_inline 1297 _sx_sunlock_try(struct sx *sx, struct thread *td, uintptr_t *xp) 1298 { 1299 1300 for (;;) { 1301 if (SX_SHARERS(*xp) > 1 || !(*xp & SX_LOCK_WAITERS)) { 1302 if (atomic_fcmpset_rel_ptr(&sx->sx_lock, xp, 1303 *xp - SX_ONE_SHARER)) { 1304 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1305 CTR4(KTR_LOCK, 1306 "%s: %p succeeded %p -> %p", 1307 __func__, sx, (void *)*xp, 1308 (void *)(*xp - SX_ONE_SHARER)); 1309 td->td_sx_slocks--; 1310 return (true); 1311 } 1312 continue; 1313 } 1314 break; 1315 } 1316 return (false); 1317 } 1318 1319 static void __noinline 1320 _sx_sunlock_hard(struct sx *sx, struct thread *td, uintptr_t x 1321 LOCK_FILE_LINE_ARG_DEF) 1322 { 1323 int wakeup_swapper = 0; 1324 uintptr_t setx, queue; 1325 1326 if (SCHEDULER_STOPPED()) 1327 return; 1328 1329 if (_sx_sunlock_try(sx, td, &x)) 1330 goto out_lockstat; 1331 1332 sleepq_lock(&sx->lock_object); 1333 x = SX_READ_VALUE(sx); 1334 for (;;) { 1335 if (_sx_sunlock_try(sx, td, &x)) 1336 break; 1337 1338 /* 1339 * Wake up semantic here is quite simple: 1340 * Just wake up all the exclusive waiters. 1341 * Note that the state of the lock could have changed, 1342 * so if it fails loop back and retry. 1343 */ 1344 setx = SX_LOCK_UNLOCKED; 1345 queue = SQ_SHARED_QUEUE; 1346 if (x & SX_LOCK_EXCLUSIVE_WAITERS) { 1347 setx |= (x & SX_LOCK_SHARED_WAITERS); 1348 queue = SQ_EXCLUSIVE_QUEUE; 1349 } 1350 setx |= (x & SX_LOCK_WRITE_SPINNER); 1351 if (!atomic_fcmpset_rel_ptr(&sx->sx_lock, &x, setx)) 1352 continue; 1353 if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1354 CTR2(KTR_LOCK, "%s: %p waking up all thread on" 1355 "exclusive queue", __func__, sx); 1356 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 1357 0, queue); 1358 td->td_sx_slocks--; 1359 break; 1360 } 1361 sleepq_release(&sx->lock_object); 1362 if (wakeup_swapper) 1363 kick_proc0(); 1364 out_lockstat: 1365 LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER); 1366 } 1367 1368 void 1369 _sx_sunlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 1370 { 1371 struct thread *td; 1372 uintptr_t x; 1373 1374 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1375 ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 1376 _sx_assert(sx, SA_SLOCKED, file, line); 1377 WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 1378 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 1379 1380 td = curthread; 1381 x = SX_READ_VALUE(sx); 1382 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(sx__release) || 1383 !_sx_sunlock_try(sx, td, &x))) 1384 _sx_sunlock_hard(sx, td, x LOCK_FILE_LINE_ARG); 1385 else 1386 lock_profile_release_lock(&sx->lock_object, false); 1387 1388 TD_LOCKS_DEC(curthread); 1389 } 1390 1391 void 1392 _sx_sunlock(struct sx *sx, const char *file, int line) 1393 { 1394 1395 _sx_sunlock_int(sx LOCK_FILE_LINE_ARG); 1396 } 1397 1398 #ifdef INVARIANT_SUPPORT 1399 #ifndef INVARIANTS 1400 #undef _sx_assert 1401 #endif 1402 1403 /* 1404 * In the non-WITNESS case, sx_assert() can only detect that at least 1405 * *some* thread owns an slock, but it cannot guarantee that *this* 1406 * thread owns an slock. 1407 */ 1408 void 1409 _sx_assert(const struct sx *sx, int what, const char *file, int line) 1410 { 1411 #ifndef WITNESS 1412 int slocked = 0; 1413 #endif 1414 1415 if (SCHEDULER_STOPPED()) 1416 return; 1417 switch (what) { 1418 case SA_SLOCKED: 1419 case SA_SLOCKED | SA_NOTRECURSED: 1420 case SA_SLOCKED | SA_RECURSED: 1421 #ifndef WITNESS 1422 slocked = 1; 1423 /* FALLTHROUGH */ 1424 #endif 1425 case SA_LOCKED: 1426 case SA_LOCKED | SA_NOTRECURSED: 1427 case SA_LOCKED | SA_RECURSED: 1428 #ifdef WITNESS 1429 witness_assert(&sx->lock_object, what, file, line); 1430 #else 1431 /* 1432 * If some other thread has an exclusive lock or we 1433 * have one and are asserting a shared lock, fail. 1434 * Also, if no one has a lock at all, fail. 1435 */ 1436 if (sx->sx_lock == SX_LOCK_UNLOCKED || 1437 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 1438 sx_xholder(sx) != curthread))) 1439 panic("Lock %s not %slocked @ %s:%d\n", 1440 sx->lock_object.lo_name, slocked ? "share " : "", 1441 file, line); 1442 1443 if (!(sx->sx_lock & SX_LOCK_SHARED)) { 1444 if (sx_recursed(sx)) { 1445 if (what & SA_NOTRECURSED) 1446 panic("Lock %s recursed @ %s:%d\n", 1447 sx->lock_object.lo_name, file, 1448 line); 1449 } else if (what & SA_RECURSED) 1450 panic("Lock %s not recursed @ %s:%d\n", 1451 sx->lock_object.lo_name, file, line); 1452 } 1453 #endif 1454 break; 1455 case SA_XLOCKED: 1456 case SA_XLOCKED | SA_NOTRECURSED: 1457 case SA_XLOCKED | SA_RECURSED: 1458 if (sx_xholder(sx) != curthread) 1459 panic("Lock %s not exclusively locked @ %s:%d\n", 1460 sx->lock_object.lo_name, file, line); 1461 if (sx_recursed(sx)) { 1462 if (what & SA_NOTRECURSED) 1463 panic("Lock %s recursed @ %s:%d\n", 1464 sx->lock_object.lo_name, file, line); 1465 } else if (what & SA_RECURSED) 1466 panic("Lock %s not recursed @ %s:%d\n", 1467 sx->lock_object.lo_name, file, line); 1468 break; 1469 case SA_UNLOCKED: 1470 #ifdef WITNESS 1471 witness_assert(&sx->lock_object, what, file, line); 1472 #else 1473 /* 1474 * If we hold an exclusve lock fail. We can't 1475 * reliably check to see if we hold a shared lock or 1476 * not. 1477 */ 1478 if (sx_xholder(sx) == curthread) 1479 panic("Lock %s exclusively locked @ %s:%d\n", 1480 sx->lock_object.lo_name, file, line); 1481 #endif 1482 break; 1483 default: 1484 panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 1485 line); 1486 } 1487 } 1488 #endif /* INVARIANT_SUPPORT */ 1489 1490 #ifdef DDB 1491 static void 1492 db_show_sx(const struct lock_object *lock) 1493 { 1494 struct thread *td; 1495 const struct sx *sx; 1496 1497 sx = (const struct sx *)lock; 1498 1499 db_printf(" state: "); 1500 if (sx->sx_lock == SX_LOCK_UNLOCKED) 1501 db_printf("UNLOCKED\n"); 1502 else if (sx->sx_lock == SX_LOCK_DESTROYED) { 1503 db_printf("DESTROYED\n"); 1504 return; 1505 } else if (sx->sx_lock & SX_LOCK_SHARED) 1506 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 1507 else { 1508 td = sx_xholder(sx); 1509 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1510 td->td_tid, td->td_proc->p_pid, td->td_name); 1511 if (sx_recursed(sx)) 1512 db_printf(" recursed: %d\n", sx->sx_recurse); 1513 } 1514 1515 db_printf(" waiters: "); 1516 switch(sx->sx_lock & 1517 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 1518 case SX_LOCK_SHARED_WAITERS: 1519 db_printf("shared\n"); 1520 break; 1521 case SX_LOCK_EXCLUSIVE_WAITERS: 1522 db_printf("exclusive\n"); 1523 break; 1524 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 1525 db_printf("exclusive and shared\n"); 1526 break; 1527 default: 1528 db_printf("none\n"); 1529 } 1530 } 1531 1532 /* 1533 * Check to see if a thread that is blocked on a sleep queue is actually 1534 * blocked on an sx lock. If so, output some details and return true. 1535 * If the lock has an exclusive owner, return that in *ownerp. 1536 */ 1537 int 1538 sx_chain(struct thread *td, struct thread **ownerp) 1539 { 1540 const struct sx *sx; 1541 1542 /* 1543 * Check to see if this thread is blocked on an sx lock. 1544 * First, we check the lock class. If that is ok, then we 1545 * compare the lock name against the wait message. 1546 */ 1547 sx = td->td_wchan; 1548 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 1549 sx->lock_object.lo_name != td->td_wmesg) 1550 return (0); 1551 1552 /* We think we have an sx lock, so output some details. */ 1553 db_printf("blocked on sx \"%s\" ", td->td_wmesg); 1554 *ownerp = sx_xholder(sx); 1555 if (sx->sx_lock & SX_LOCK_SHARED) 1556 db_printf("SLOCK (count %ju)\n", 1557 (uintmax_t)SX_SHARERS(sx->sx_lock)); 1558 else 1559 db_printf("XLOCK\n"); 1560 return (1); 1561 } 1562 #endif 1563