1 /*- 2 * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * Machine independent bits of reader/writer lock implementation. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include "opt_ddb.h" 35 #include "opt_hwpmc_hooks.h" 36 #include "opt_no_adaptive_rwlocks.h" 37 38 #include <sys/param.h> 39 #include <sys/kdb.h> 40 #include <sys/ktr.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/mutex.h> 44 #include <sys/proc.h> 45 #include <sys/rwlock.h> 46 #include <sys/sched.h> 47 #include <sys/sysctl.h> 48 #include <sys/systm.h> 49 #include <sys/turnstile.h> 50 51 #include <machine/cpu.h> 52 53 #if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS) 54 #define ADAPTIVE_RWLOCKS 55 #endif 56 57 #ifdef HWPMC_HOOKS 58 #include <sys/pmckern.h> 59 PMC_SOFT_DECLARE( , , lock, failed); 60 #endif 61 62 /* 63 * Return the rwlock address when the lock cookie address is provided. 64 * This functionality assumes that struct rwlock* have a member named rw_lock. 65 */ 66 #define rwlock2rw(c) (__containerof(c, struct rwlock, rw_lock)) 67 68 #ifdef ADAPTIVE_RWLOCKS 69 static int rowner_retries = 10; 70 static int rowner_loops = 10000; 71 static SYSCTL_NODE(_debug, OID_AUTO, rwlock, CTLFLAG_RD, NULL, 72 "rwlock debugging"); 73 SYSCTL_INT(_debug_rwlock, OID_AUTO, retry, CTLFLAG_RW, &rowner_retries, 0, ""); 74 SYSCTL_INT(_debug_rwlock, OID_AUTO, loops, CTLFLAG_RW, &rowner_loops, 0, ""); 75 #endif 76 77 #ifdef DDB 78 #include <ddb/ddb.h> 79 80 static void db_show_rwlock(const struct lock_object *lock); 81 #endif 82 static void assert_rw(const struct lock_object *lock, int what); 83 static void lock_rw(struct lock_object *lock, uintptr_t how); 84 #ifdef KDTRACE_HOOKS 85 static int owner_rw(const struct lock_object *lock, struct thread **owner); 86 #endif 87 static uintptr_t unlock_rw(struct lock_object *lock); 88 89 struct lock_class lock_class_rw = { 90 .lc_name = "rw", 91 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE, 92 .lc_assert = assert_rw, 93 #ifdef DDB 94 .lc_ddb_show = db_show_rwlock, 95 #endif 96 .lc_lock = lock_rw, 97 .lc_unlock = unlock_rw, 98 #ifdef KDTRACE_HOOKS 99 .lc_owner = owner_rw, 100 #endif 101 }; 102 103 /* 104 * Return a pointer to the owning thread if the lock is write-locked or 105 * NULL if the lock is unlocked or read-locked. 106 */ 107 #define rw_wowner(rw) \ 108 ((rw)->rw_lock & RW_LOCK_READ ? NULL : \ 109 (struct thread *)RW_OWNER((rw)->rw_lock)) 110 111 /* 112 * Returns if a write owner is recursed. Write ownership is not assured 113 * here and should be previously checked. 114 */ 115 #define rw_recursed(rw) ((rw)->rw_recurse != 0) 116 117 /* 118 * Return true if curthread helds the lock. 119 */ 120 #define rw_wlocked(rw) (rw_wowner((rw)) == curthread) 121 122 /* 123 * Return a pointer to the owning thread for this lock who should receive 124 * any priority lent by threads that block on this lock. Currently this 125 * is identical to rw_wowner(). 126 */ 127 #define rw_owner(rw) rw_wowner(rw) 128 129 #ifndef INVARIANTS 130 #define __rw_assert(c, what, file, line) 131 #endif 132 133 void 134 assert_rw(const struct lock_object *lock, int what) 135 { 136 137 rw_assert((const struct rwlock *)lock, what); 138 } 139 140 void 141 lock_rw(struct lock_object *lock, uintptr_t how) 142 { 143 struct rwlock *rw; 144 145 rw = (struct rwlock *)lock; 146 if (how) 147 rw_rlock(rw); 148 else 149 rw_wlock(rw); 150 } 151 152 uintptr_t 153 unlock_rw(struct lock_object *lock) 154 { 155 struct rwlock *rw; 156 157 rw = (struct rwlock *)lock; 158 rw_assert(rw, RA_LOCKED | LA_NOTRECURSED); 159 if (rw->rw_lock & RW_LOCK_READ) { 160 rw_runlock(rw); 161 return (1); 162 } else { 163 rw_wunlock(rw); 164 return (0); 165 } 166 } 167 168 #ifdef KDTRACE_HOOKS 169 int 170 owner_rw(const struct lock_object *lock, struct thread **owner) 171 { 172 const struct rwlock *rw = (const struct rwlock *)lock; 173 uintptr_t x = rw->rw_lock; 174 175 *owner = rw_wowner(rw); 176 return ((x & RW_LOCK_READ) != 0 ? (RW_READERS(x) != 0) : 177 (*owner != NULL)); 178 } 179 #endif 180 181 void 182 _rw_init_flags(volatile uintptr_t *c, const char *name, int opts) 183 { 184 struct rwlock *rw; 185 int flags; 186 187 rw = rwlock2rw(c); 188 189 MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET | 190 RW_RECURSE | RW_NEW)) == 0); 191 ASSERT_ATOMIC_LOAD_PTR(rw->rw_lock, 192 ("%s: rw_lock not aligned for %s: %p", __func__, name, 193 &rw->rw_lock)); 194 195 flags = LO_UPGRADABLE; 196 if (opts & RW_DUPOK) 197 flags |= LO_DUPOK; 198 if (opts & RW_NOPROFILE) 199 flags |= LO_NOPROFILE; 200 if (!(opts & RW_NOWITNESS)) 201 flags |= LO_WITNESS; 202 if (opts & RW_RECURSE) 203 flags |= LO_RECURSABLE; 204 if (opts & RW_QUIET) 205 flags |= LO_QUIET; 206 if (opts & RW_NEW) 207 flags |= LO_NEW; 208 209 lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags); 210 rw->rw_lock = RW_UNLOCKED; 211 rw->rw_recurse = 0; 212 } 213 214 void 215 _rw_destroy(volatile uintptr_t *c) 216 { 217 struct rwlock *rw; 218 219 rw = rwlock2rw(c); 220 221 KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock %p not unlocked", rw)); 222 KASSERT(rw->rw_recurse == 0, ("rw lock %p still recursed", rw)); 223 rw->rw_lock = RW_DESTROYED; 224 lock_destroy(&rw->lock_object); 225 } 226 227 void 228 rw_sysinit(void *arg) 229 { 230 struct rw_args *args = arg; 231 232 rw_init((struct rwlock *)args->ra_rw, args->ra_desc); 233 } 234 235 void 236 rw_sysinit_flags(void *arg) 237 { 238 struct rw_args_flags *args = arg; 239 240 rw_init_flags((struct rwlock *)args->ra_rw, args->ra_desc, 241 args->ra_flags); 242 } 243 244 int 245 _rw_wowned(const volatile uintptr_t *c) 246 { 247 248 return (rw_wowner(rwlock2rw(c)) == curthread); 249 } 250 251 void 252 _rw_wlock_cookie(volatile uintptr_t *c, const char *file, int line) 253 { 254 struct rwlock *rw; 255 256 if (SCHEDULER_STOPPED()) 257 return; 258 259 rw = rwlock2rw(c); 260 261 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 262 ("rw_wlock() by idle thread %p on rwlock %s @ %s:%d", 263 curthread, rw->lock_object.lo_name, file, line)); 264 KASSERT(rw->rw_lock != RW_DESTROYED, 265 ("rw_wlock() of destroyed rwlock @ %s:%d", file, line)); 266 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 267 line, NULL); 268 __rw_wlock(rw, curthread, file, line); 269 LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line); 270 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line); 271 curthread->td_locks++; 272 } 273 274 int 275 __rw_try_wlock(volatile uintptr_t *c, const char *file, int line) 276 { 277 struct rwlock *rw; 278 int rval; 279 280 if (SCHEDULER_STOPPED()) 281 return (1); 282 283 rw = rwlock2rw(c); 284 285 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 286 ("rw_try_wlock() by idle thread %p on rwlock %s @ %s:%d", 287 curthread, rw->lock_object.lo_name, file, line)); 288 KASSERT(rw->rw_lock != RW_DESTROYED, 289 ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line)); 290 291 if (rw_wlocked(rw) && 292 (rw->lock_object.lo_flags & LO_RECURSABLE) != 0) { 293 rw->rw_recurse++; 294 rval = 1; 295 } else 296 rval = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_UNLOCKED, 297 (uintptr_t)curthread); 298 299 LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line); 300 if (rval) { 301 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 302 file, line); 303 curthread->td_locks++; 304 } 305 return (rval); 306 } 307 308 void 309 _rw_wunlock_cookie(volatile uintptr_t *c, const char *file, int line) 310 { 311 struct rwlock *rw; 312 313 if (SCHEDULER_STOPPED()) 314 return; 315 316 rw = rwlock2rw(c); 317 318 KASSERT(rw->rw_lock != RW_DESTROYED, 319 ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line)); 320 __rw_assert(c, RA_WLOCKED, file, line); 321 WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line); 322 LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file, 323 line); 324 __rw_wunlock(rw, curthread, file, line); 325 curthread->td_locks--; 326 } 327 /* 328 * Determines whether a new reader can acquire a lock. Succeeds if the 329 * reader already owns a read lock and the lock is locked for read to 330 * prevent deadlock from reader recursion. Also succeeds if the lock 331 * is unlocked and has no writer waiters or spinners. Failing otherwise 332 * prioritizes writers before readers. 333 */ 334 #define RW_CAN_READ(_rw) \ 335 ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) & \ 336 (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) == \ 337 RW_LOCK_READ) 338 339 void 340 __rw_rlock(volatile uintptr_t *c, const char *file, int line) 341 { 342 struct rwlock *rw; 343 struct turnstile *ts; 344 #ifdef ADAPTIVE_RWLOCKS 345 volatile struct thread *owner; 346 int spintries = 0; 347 int i; 348 #endif 349 #ifdef LOCK_PROFILING 350 uint64_t waittime = 0; 351 int contested = 0; 352 #endif 353 uintptr_t v; 354 #ifdef KDTRACE_HOOKS 355 uint64_t spin_cnt = 0; 356 uint64_t sleep_cnt = 0; 357 int64_t sleep_time = 0; 358 #endif 359 360 if (SCHEDULER_STOPPED()) 361 return; 362 363 rw = rwlock2rw(c); 364 365 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 366 ("rw_rlock() by idle thread %p on rwlock %s @ %s:%d", 367 curthread, rw->lock_object.lo_name, file, line)); 368 KASSERT(rw->rw_lock != RW_DESTROYED, 369 ("rw_rlock() of destroyed rwlock @ %s:%d", file, line)); 370 KASSERT(rw_wowner(rw) != curthread, 371 ("rw_rlock: wlock already held for %s @ %s:%d", 372 rw->lock_object.lo_name, file, line)); 373 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line, NULL); 374 375 for (;;) { 376 #ifdef KDTRACE_HOOKS 377 spin_cnt++; 378 #endif 379 /* 380 * Handle the easy case. If no other thread has a write 381 * lock, then try to bump up the count of read locks. Note 382 * that we have to preserve the current state of the 383 * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a 384 * read lock, then rw_lock must have changed, so restart 385 * the loop. Note that this handles the case of a 386 * completely unlocked rwlock since such a lock is encoded 387 * as a read lock with no waiters. 388 */ 389 v = rw->rw_lock; 390 if (RW_CAN_READ(v)) { 391 /* 392 * The RW_LOCK_READ_WAITERS flag should only be set 393 * if the lock has been unlocked and write waiters 394 * were present. 395 */ 396 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, 397 v + RW_ONE_READER)) { 398 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 399 CTR4(KTR_LOCK, 400 "%s: %p succeed %p -> %p", __func__, 401 rw, (void *)v, 402 (void *)(v + RW_ONE_READER)); 403 break; 404 } 405 continue; 406 } 407 #ifdef HWPMC_HOOKS 408 PMC_SOFT_CALL( , , lock, failed); 409 #endif 410 lock_profile_obtain_lock_failed(&rw->lock_object, 411 &contested, &waittime); 412 413 #ifdef ADAPTIVE_RWLOCKS 414 /* 415 * If the owner is running on another CPU, spin until 416 * the owner stops running or the state of the lock 417 * changes. 418 */ 419 if ((v & RW_LOCK_READ) == 0) { 420 owner = (struct thread *)RW_OWNER(v); 421 if (TD_IS_RUNNING(owner)) { 422 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 423 CTR3(KTR_LOCK, 424 "%s: spinning on %p held by %p", 425 __func__, rw, owner); 426 KTR_STATE1(KTR_SCHED, "thread", 427 sched_tdname(curthread), "spinning", 428 "lockname:\"%s\"", rw->lock_object.lo_name); 429 while ((struct thread*)RW_OWNER(rw->rw_lock) == 430 owner && TD_IS_RUNNING(owner)) { 431 cpu_spinwait(); 432 #ifdef KDTRACE_HOOKS 433 spin_cnt++; 434 #endif 435 } 436 KTR_STATE0(KTR_SCHED, "thread", 437 sched_tdname(curthread), "running"); 438 continue; 439 } 440 } else if (spintries < rowner_retries) { 441 spintries++; 442 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread), 443 "spinning", "lockname:\"%s\"", 444 rw->lock_object.lo_name); 445 for (i = 0; i < rowner_loops; i++) { 446 v = rw->rw_lock; 447 if ((v & RW_LOCK_READ) == 0 || RW_CAN_READ(v)) 448 break; 449 cpu_spinwait(); 450 } 451 #ifdef KDTRACE_HOOKS 452 spin_cnt += rowner_loops - i; 453 #endif 454 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread), 455 "running"); 456 if (i != rowner_loops) 457 continue; 458 } 459 #endif 460 461 /* 462 * Okay, now it's the hard case. Some other thread already 463 * has a write lock or there are write waiters present, 464 * acquire the turnstile lock so we can begin the process 465 * of blocking. 466 */ 467 ts = turnstile_trywait(&rw->lock_object); 468 469 /* 470 * The lock might have been released while we spun, so 471 * recheck its state and restart the loop if needed. 472 */ 473 v = rw->rw_lock; 474 if (RW_CAN_READ(v)) { 475 turnstile_cancel(ts); 476 continue; 477 } 478 479 #ifdef ADAPTIVE_RWLOCKS 480 /* 481 * The current lock owner might have started executing 482 * on another CPU (or the lock could have changed 483 * owners) while we were waiting on the turnstile 484 * chain lock. If so, drop the turnstile lock and try 485 * again. 486 */ 487 if ((v & RW_LOCK_READ) == 0) { 488 owner = (struct thread *)RW_OWNER(v); 489 if (TD_IS_RUNNING(owner)) { 490 turnstile_cancel(ts); 491 continue; 492 } 493 } 494 #endif 495 496 /* 497 * The lock is held in write mode or it already has waiters. 498 */ 499 MPASS(!RW_CAN_READ(v)); 500 501 /* 502 * If the RW_LOCK_READ_WAITERS flag is already set, then 503 * we can go ahead and block. If it is not set then try 504 * to set it. If we fail to set it drop the turnstile 505 * lock and restart the loop. 506 */ 507 if (!(v & RW_LOCK_READ_WAITERS)) { 508 if (!atomic_cmpset_ptr(&rw->rw_lock, v, 509 v | RW_LOCK_READ_WAITERS)) { 510 turnstile_cancel(ts); 511 continue; 512 } 513 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 514 CTR2(KTR_LOCK, "%s: %p set read waiters flag", 515 __func__, rw); 516 } 517 518 /* 519 * We were unable to acquire the lock and the read waiters 520 * flag is set, so we must block on the turnstile. 521 */ 522 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 523 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 524 rw); 525 #ifdef KDTRACE_HOOKS 526 sleep_time -= lockstat_nsecs(); 527 #endif 528 turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE); 529 #ifdef KDTRACE_HOOKS 530 sleep_time += lockstat_nsecs(); 531 sleep_cnt++; 532 #endif 533 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 534 CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 535 __func__, rw); 536 } 537 538 /* 539 * TODO: acquire "owner of record" here. Here be turnstile dragons 540 * however. turnstiles don't like owners changing between calls to 541 * turnstile_wait() currently. 542 */ 543 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_RLOCK_ACQUIRE, rw, contested, 544 waittime, file, line); 545 LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line); 546 WITNESS_LOCK(&rw->lock_object, 0, file, line); 547 curthread->td_locks++; 548 curthread->td_rw_rlocks++; 549 #ifdef KDTRACE_HOOKS 550 if (sleep_time) 551 LOCKSTAT_RECORD1(LS_RW_RLOCK_BLOCK, rw, sleep_time); 552 553 /* 554 * Record only the loops spinning and not sleeping. 555 */ 556 if (spin_cnt > sleep_cnt) 557 LOCKSTAT_RECORD1(LS_RW_RLOCK_SPIN, rw, (spin_cnt - sleep_cnt)); 558 #endif 559 } 560 561 int 562 __rw_try_rlock(volatile uintptr_t *c, const char *file, int line) 563 { 564 struct rwlock *rw; 565 uintptr_t x; 566 567 if (SCHEDULER_STOPPED()) 568 return (1); 569 570 rw = rwlock2rw(c); 571 572 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 573 ("rw_try_rlock() by idle thread %p on rwlock %s @ %s:%d", 574 curthread, rw->lock_object.lo_name, file, line)); 575 576 for (;;) { 577 x = rw->rw_lock; 578 KASSERT(rw->rw_lock != RW_DESTROYED, 579 ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line)); 580 if (!(x & RW_LOCK_READ)) 581 break; 582 if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, x + RW_ONE_READER)) { 583 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file, 584 line); 585 WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line); 586 curthread->td_locks++; 587 curthread->td_rw_rlocks++; 588 return (1); 589 } 590 } 591 592 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line); 593 return (0); 594 } 595 596 void 597 _rw_runlock_cookie(volatile uintptr_t *c, const char *file, int line) 598 { 599 struct rwlock *rw; 600 struct turnstile *ts; 601 uintptr_t x, v, queue; 602 603 if (SCHEDULER_STOPPED()) 604 return; 605 606 rw = rwlock2rw(c); 607 608 KASSERT(rw->rw_lock != RW_DESTROYED, 609 ("rw_runlock() of destroyed rwlock @ %s:%d", file, line)); 610 __rw_assert(c, RA_RLOCKED, file, line); 611 WITNESS_UNLOCK(&rw->lock_object, 0, file, line); 612 LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line); 613 614 /* TODO: drop "owner of record" here. */ 615 616 for (;;) { 617 /* 618 * See if there is more than one read lock held. If so, 619 * just drop one and return. 620 */ 621 x = rw->rw_lock; 622 if (RW_READERS(x) > 1) { 623 if (atomic_cmpset_rel_ptr(&rw->rw_lock, x, 624 x - RW_ONE_READER)) { 625 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 626 CTR4(KTR_LOCK, 627 "%s: %p succeeded %p -> %p", 628 __func__, rw, (void *)x, 629 (void *)(x - RW_ONE_READER)); 630 break; 631 } 632 continue; 633 } 634 /* 635 * If there aren't any waiters for a write lock, then try 636 * to drop it quickly. 637 */ 638 if (!(x & RW_LOCK_WAITERS)) { 639 MPASS((x & ~RW_LOCK_WRITE_SPINNER) == 640 RW_READERS_LOCK(1)); 641 if (atomic_cmpset_rel_ptr(&rw->rw_lock, x, 642 RW_UNLOCKED)) { 643 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 644 CTR2(KTR_LOCK, "%s: %p last succeeded", 645 __func__, rw); 646 break; 647 } 648 continue; 649 } 650 /* 651 * Ok, we know we have waiters and we think we are the 652 * last reader, so grab the turnstile lock. 653 */ 654 turnstile_chain_lock(&rw->lock_object); 655 v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER); 656 MPASS(v & RW_LOCK_WAITERS); 657 658 /* 659 * Try to drop our lock leaving the lock in a unlocked 660 * state. 661 * 662 * If you wanted to do explicit lock handoff you'd have to 663 * do it here. You'd also want to use turnstile_signal() 664 * and you'd have to handle the race where a higher 665 * priority thread blocks on the write lock before the 666 * thread you wakeup actually runs and have the new thread 667 * "steal" the lock. For now it's a lot simpler to just 668 * wakeup all of the waiters. 669 * 670 * As above, if we fail, then another thread might have 671 * acquired a read lock, so drop the turnstile lock and 672 * restart. 673 */ 674 x = RW_UNLOCKED; 675 if (v & RW_LOCK_WRITE_WAITERS) { 676 queue = TS_EXCLUSIVE_QUEUE; 677 x |= (v & RW_LOCK_READ_WAITERS); 678 } else 679 queue = TS_SHARED_QUEUE; 680 if (!atomic_cmpset_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v, 681 x)) { 682 turnstile_chain_unlock(&rw->lock_object); 683 continue; 684 } 685 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 686 CTR2(KTR_LOCK, "%s: %p last succeeded with waiters", 687 __func__, rw); 688 689 /* 690 * Ok. The lock is released and all that's left is to 691 * wake up the waiters. Note that the lock might not be 692 * free anymore, but in that case the writers will just 693 * block again if they run before the new lock holder(s) 694 * release the lock. 695 */ 696 ts = turnstile_lookup(&rw->lock_object); 697 MPASS(ts != NULL); 698 turnstile_broadcast(ts, queue); 699 turnstile_unpend(ts, TS_SHARED_LOCK); 700 turnstile_chain_unlock(&rw->lock_object); 701 break; 702 } 703 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_RW_RUNLOCK_RELEASE, rw); 704 curthread->td_locks--; 705 curthread->td_rw_rlocks--; 706 } 707 708 /* 709 * This function is called when we are unable to obtain a write lock on the 710 * first try. This means that at least one other thread holds either a 711 * read or write lock. 712 */ 713 void 714 __rw_wlock_hard(volatile uintptr_t *c, uintptr_t tid, const char *file, 715 int line) 716 { 717 struct rwlock *rw; 718 struct turnstile *ts; 719 #ifdef ADAPTIVE_RWLOCKS 720 volatile struct thread *owner; 721 int spintries = 0; 722 int i; 723 #endif 724 uintptr_t v, x; 725 #ifdef LOCK_PROFILING 726 uint64_t waittime = 0; 727 int contested = 0; 728 #endif 729 #ifdef KDTRACE_HOOKS 730 uint64_t spin_cnt = 0; 731 uint64_t sleep_cnt = 0; 732 int64_t sleep_time = 0; 733 #endif 734 735 if (SCHEDULER_STOPPED()) 736 return; 737 738 rw = rwlock2rw(c); 739 740 if (rw_wlocked(rw)) { 741 KASSERT(rw->lock_object.lo_flags & LO_RECURSABLE, 742 ("%s: recursing but non-recursive rw %s @ %s:%d\n", 743 __func__, rw->lock_object.lo_name, file, line)); 744 rw->rw_recurse++; 745 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 746 CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw); 747 return; 748 } 749 750 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 751 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 752 rw->lock_object.lo_name, (void *)rw->rw_lock, file, line); 753 754 while (!_rw_write_lock(rw, tid)) { 755 #ifdef KDTRACE_HOOKS 756 spin_cnt++; 757 #endif 758 #ifdef HWPMC_HOOKS 759 PMC_SOFT_CALL( , , lock, failed); 760 #endif 761 lock_profile_obtain_lock_failed(&rw->lock_object, 762 &contested, &waittime); 763 #ifdef ADAPTIVE_RWLOCKS 764 /* 765 * If the lock is write locked and the owner is 766 * running on another CPU, spin until the owner stops 767 * running or the state of the lock changes. 768 */ 769 v = rw->rw_lock; 770 owner = (struct thread *)RW_OWNER(v); 771 if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) { 772 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 773 CTR3(KTR_LOCK, "%s: spinning on %p held by %p", 774 __func__, rw, owner); 775 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread), 776 "spinning", "lockname:\"%s\"", 777 rw->lock_object.lo_name); 778 while ((struct thread*)RW_OWNER(rw->rw_lock) == owner && 779 TD_IS_RUNNING(owner)) { 780 cpu_spinwait(); 781 #ifdef KDTRACE_HOOKS 782 spin_cnt++; 783 #endif 784 } 785 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread), 786 "running"); 787 continue; 788 } 789 if ((v & RW_LOCK_READ) && RW_READERS(v) && 790 spintries < rowner_retries) { 791 if (!(v & RW_LOCK_WRITE_SPINNER)) { 792 if (!atomic_cmpset_ptr(&rw->rw_lock, v, 793 v | RW_LOCK_WRITE_SPINNER)) { 794 continue; 795 } 796 } 797 spintries++; 798 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread), 799 "spinning", "lockname:\"%s\"", 800 rw->lock_object.lo_name); 801 for (i = 0; i < rowner_loops; i++) { 802 if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0) 803 break; 804 cpu_spinwait(); 805 } 806 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread), 807 "running"); 808 #ifdef KDTRACE_HOOKS 809 spin_cnt += rowner_loops - i; 810 #endif 811 if (i != rowner_loops) 812 continue; 813 } 814 #endif 815 ts = turnstile_trywait(&rw->lock_object); 816 v = rw->rw_lock; 817 818 #ifdef ADAPTIVE_RWLOCKS 819 /* 820 * The current lock owner might have started executing 821 * on another CPU (or the lock could have changed 822 * owners) while we were waiting on the turnstile 823 * chain lock. If so, drop the turnstile lock and try 824 * again. 825 */ 826 if (!(v & RW_LOCK_READ)) { 827 owner = (struct thread *)RW_OWNER(v); 828 if (TD_IS_RUNNING(owner)) { 829 turnstile_cancel(ts); 830 continue; 831 } 832 } 833 #endif 834 /* 835 * Check for the waiters flags about this rwlock. 836 * If the lock was released, without maintain any pending 837 * waiters queue, simply try to acquire it. 838 * If a pending waiters queue is present, claim the lock 839 * ownership and maintain the pending queue. 840 */ 841 x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER); 842 if ((v & ~x) == RW_UNLOCKED) { 843 x &= ~RW_LOCK_WRITE_SPINNER; 844 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) { 845 if (x) 846 turnstile_claim(ts); 847 else 848 turnstile_cancel(ts); 849 break; 850 } 851 turnstile_cancel(ts); 852 continue; 853 } 854 /* 855 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to 856 * set it. If we fail to set it, then loop back and try 857 * again. 858 */ 859 if (!(v & RW_LOCK_WRITE_WAITERS)) { 860 if (!atomic_cmpset_ptr(&rw->rw_lock, v, 861 v | RW_LOCK_WRITE_WAITERS)) { 862 turnstile_cancel(ts); 863 continue; 864 } 865 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 866 CTR2(KTR_LOCK, "%s: %p set write waiters flag", 867 __func__, rw); 868 } 869 /* 870 * We were unable to acquire the lock and the write waiters 871 * flag is set, so we must block on the turnstile. 872 */ 873 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 874 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 875 rw); 876 #ifdef KDTRACE_HOOKS 877 sleep_time -= lockstat_nsecs(); 878 #endif 879 turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE); 880 #ifdef KDTRACE_HOOKS 881 sleep_time += lockstat_nsecs(); 882 sleep_cnt++; 883 #endif 884 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 885 CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 886 __func__, rw); 887 #ifdef ADAPTIVE_RWLOCKS 888 spintries = 0; 889 #endif 890 } 891 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_WLOCK_ACQUIRE, rw, contested, 892 waittime, file, line); 893 #ifdef KDTRACE_HOOKS 894 if (sleep_time) 895 LOCKSTAT_RECORD1(LS_RW_WLOCK_BLOCK, rw, sleep_time); 896 897 /* 898 * Record only the loops spinning and not sleeping. 899 */ 900 if (spin_cnt > sleep_cnt) 901 LOCKSTAT_RECORD1(LS_RW_WLOCK_SPIN, rw, (spin_cnt - sleep_cnt)); 902 #endif 903 } 904 905 /* 906 * This function is called if the first try at releasing a write lock failed. 907 * This means that one of the 2 waiter bits must be set indicating that at 908 * least one thread is waiting on this lock. 909 */ 910 void 911 __rw_wunlock_hard(volatile uintptr_t *c, uintptr_t tid, const char *file, 912 int line) 913 { 914 struct rwlock *rw; 915 struct turnstile *ts; 916 uintptr_t v; 917 int queue; 918 919 if (SCHEDULER_STOPPED()) 920 return; 921 922 rw = rwlock2rw(c); 923 924 if (rw_wlocked(rw) && rw_recursed(rw)) { 925 rw->rw_recurse--; 926 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 927 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw); 928 return; 929 } 930 931 KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS), 932 ("%s: neither of the waiter flags are set", __func__)); 933 934 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 935 CTR2(KTR_LOCK, "%s: %p contested", __func__, rw); 936 937 turnstile_chain_lock(&rw->lock_object); 938 ts = turnstile_lookup(&rw->lock_object); 939 MPASS(ts != NULL); 940 941 /* 942 * Use the same algo as sx locks for now. Prefer waking up shared 943 * waiters if we have any over writers. This is probably not ideal. 944 * 945 * 'v' is the value we are going to write back to rw_lock. If we 946 * have waiters on both queues, we need to preserve the state of 947 * the waiter flag for the queue we don't wake up. For now this is 948 * hardcoded for the algorithm mentioned above. 949 * 950 * In the case of both readers and writers waiting we wakeup the 951 * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a 952 * new writer comes in before a reader it will claim the lock up 953 * above. There is probably a potential priority inversion in 954 * there that could be worked around either by waking both queues 955 * of waiters or doing some complicated lock handoff gymnastics. 956 */ 957 v = RW_UNLOCKED; 958 if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) { 959 queue = TS_EXCLUSIVE_QUEUE; 960 v |= (rw->rw_lock & RW_LOCK_READ_WAITERS); 961 } else 962 queue = TS_SHARED_QUEUE; 963 964 /* Wake up all waiters for the specific queue. */ 965 if (LOCK_LOG_TEST(&rw->lock_object, 0)) 966 CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw, 967 queue == TS_SHARED_QUEUE ? "read" : "write"); 968 turnstile_broadcast(ts, queue); 969 atomic_store_rel_ptr(&rw->rw_lock, v); 970 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 971 turnstile_chain_unlock(&rw->lock_object); 972 } 973 974 /* 975 * Attempt to do a non-blocking upgrade from a read lock to a write 976 * lock. This will only succeed if this thread holds a single read 977 * lock. Returns true if the upgrade succeeded and false otherwise. 978 */ 979 int 980 __rw_try_upgrade(volatile uintptr_t *c, const char *file, int line) 981 { 982 struct rwlock *rw; 983 uintptr_t v, x, tid; 984 struct turnstile *ts; 985 int success; 986 987 if (SCHEDULER_STOPPED()) 988 return (1); 989 990 rw = rwlock2rw(c); 991 992 KASSERT(rw->rw_lock != RW_DESTROYED, 993 ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line)); 994 __rw_assert(c, RA_RLOCKED, file, line); 995 996 /* 997 * Attempt to switch from one reader to a writer. If there 998 * are any write waiters, then we will have to lock the 999 * turnstile first to prevent races with another writer 1000 * calling turnstile_wait() before we have claimed this 1001 * turnstile. So, do the simple case of no waiters first. 1002 */ 1003 tid = (uintptr_t)curthread; 1004 success = 0; 1005 for (;;) { 1006 v = rw->rw_lock; 1007 if (RW_READERS(v) > 1) 1008 break; 1009 if (!(v & RW_LOCK_WAITERS)) { 1010 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid); 1011 if (!success) 1012 continue; 1013 break; 1014 } 1015 1016 /* 1017 * Ok, we think we have waiters, so lock the turnstile. 1018 */ 1019 ts = turnstile_trywait(&rw->lock_object); 1020 v = rw->rw_lock; 1021 if (RW_READERS(v) > 1) { 1022 turnstile_cancel(ts); 1023 break; 1024 } 1025 /* 1026 * Try to switch from one reader to a writer again. This time 1027 * we honor the current state of the waiters flags. 1028 * If we obtain the lock with the flags set, then claim 1029 * ownership of the turnstile. 1030 */ 1031 x = rw->rw_lock & RW_LOCK_WAITERS; 1032 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x); 1033 if (success) { 1034 if (x) 1035 turnstile_claim(ts); 1036 else 1037 turnstile_cancel(ts); 1038 break; 1039 } 1040 turnstile_cancel(ts); 1041 } 1042 LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line); 1043 if (success) { 1044 curthread->td_rw_rlocks--; 1045 WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 1046 file, line); 1047 LOCKSTAT_RECORD0(LS_RW_TRYUPGRADE_UPGRADE, rw); 1048 } 1049 return (success); 1050 } 1051 1052 /* 1053 * Downgrade a write lock into a single read lock. 1054 */ 1055 void 1056 __rw_downgrade(volatile uintptr_t *c, const char *file, int line) 1057 { 1058 struct rwlock *rw; 1059 struct turnstile *ts; 1060 uintptr_t tid, v; 1061 int rwait, wwait; 1062 1063 if (SCHEDULER_STOPPED()) 1064 return; 1065 1066 rw = rwlock2rw(c); 1067 1068 KASSERT(rw->rw_lock != RW_DESTROYED, 1069 ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line)); 1070 __rw_assert(c, RA_WLOCKED | RA_NOTRECURSED, file, line); 1071 #ifndef INVARIANTS 1072 if (rw_recursed(rw)) 1073 panic("downgrade of a recursed lock"); 1074 #endif 1075 1076 WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line); 1077 1078 /* 1079 * Convert from a writer to a single reader. First we handle 1080 * the easy case with no waiters. If there are any waiters, we 1081 * lock the turnstile and "disown" the lock. 1082 */ 1083 tid = (uintptr_t)curthread; 1084 if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1))) 1085 goto out; 1086 1087 /* 1088 * Ok, we think we have waiters, so lock the turnstile so we can 1089 * read the waiter flags without any races. 1090 */ 1091 turnstile_chain_lock(&rw->lock_object); 1092 v = rw->rw_lock & RW_LOCK_WAITERS; 1093 rwait = v & RW_LOCK_READ_WAITERS; 1094 wwait = v & RW_LOCK_WRITE_WAITERS; 1095 MPASS(rwait | wwait); 1096 1097 /* 1098 * Downgrade from a write lock while preserving waiters flag 1099 * and give up ownership of the turnstile. 1100 */ 1101 ts = turnstile_lookup(&rw->lock_object); 1102 MPASS(ts != NULL); 1103 if (!wwait) 1104 v &= ~RW_LOCK_READ_WAITERS; 1105 atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v); 1106 /* 1107 * Wake other readers if there are no writers pending. Otherwise they 1108 * won't be able to acquire the lock anyway. 1109 */ 1110 if (rwait && !wwait) { 1111 turnstile_broadcast(ts, TS_SHARED_QUEUE); 1112 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 1113 } else 1114 turnstile_disown(ts); 1115 turnstile_chain_unlock(&rw->lock_object); 1116 out: 1117 curthread->td_rw_rlocks++; 1118 LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line); 1119 LOCKSTAT_RECORD0(LS_RW_DOWNGRADE_DOWNGRADE, rw); 1120 } 1121 1122 #ifdef INVARIANT_SUPPORT 1123 #ifndef INVARIANTS 1124 #undef __rw_assert 1125 #endif 1126 1127 /* 1128 * In the non-WITNESS case, rw_assert() can only detect that at least 1129 * *some* thread owns an rlock, but it cannot guarantee that *this* 1130 * thread owns an rlock. 1131 */ 1132 void 1133 __rw_assert(const volatile uintptr_t *c, int what, const char *file, int line) 1134 { 1135 const struct rwlock *rw; 1136 1137 if (panicstr != NULL) 1138 return; 1139 1140 rw = rwlock2rw(c); 1141 1142 switch (what) { 1143 case RA_LOCKED: 1144 case RA_LOCKED | RA_RECURSED: 1145 case RA_LOCKED | RA_NOTRECURSED: 1146 case RA_RLOCKED: 1147 case RA_RLOCKED | RA_RECURSED: 1148 case RA_RLOCKED | RA_NOTRECURSED: 1149 #ifdef WITNESS 1150 witness_assert(&rw->lock_object, what, file, line); 1151 #else 1152 /* 1153 * If some other thread has a write lock or we have one 1154 * and are asserting a read lock, fail. Also, if no one 1155 * has a lock at all, fail. 1156 */ 1157 if (rw->rw_lock == RW_UNLOCKED || 1158 (!(rw->rw_lock & RW_LOCK_READ) && (what & RA_RLOCKED || 1159 rw_wowner(rw) != curthread))) 1160 panic("Lock %s not %slocked @ %s:%d\n", 1161 rw->lock_object.lo_name, (what & RA_RLOCKED) ? 1162 "read " : "", file, line); 1163 1164 if (!(rw->rw_lock & RW_LOCK_READ) && !(what & RA_RLOCKED)) { 1165 if (rw_recursed(rw)) { 1166 if (what & RA_NOTRECURSED) 1167 panic("Lock %s recursed @ %s:%d\n", 1168 rw->lock_object.lo_name, file, 1169 line); 1170 } else if (what & RA_RECURSED) 1171 panic("Lock %s not recursed @ %s:%d\n", 1172 rw->lock_object.lo_name, file, line); 1173 } 1174 #endif 1175 break; 1176 case RA_WLOCKED: 1177 case RA_WLOCKED | RA_RECURSED: 1178 case RA_WLOCKED | RA_NOTRECURSED: 1179 if (rw_wowner(rw) != curthread) 1180 panic("Lock %s not exclusively locked @ %s:%d\n", 1181 rw->lock_object.lo_name, file, line); 1182 if (rw_recursed(rw)) { 1183 if (what & RA_NOTRECURSED) 1184 panic("Lock %s recursed @ %s:%d\n", 1185 rw->lock_object.lo_name, file, line); 1186 } else if (what & RA_RECURSED) 1187 panic("Lock %s not recursed @ %s:%d\n", 1188 rw->lock_object.lo_name, file, line); 1189 break; 1190 case RA_UNLOCKED: 1191 #ifdef WITNESS 1192 witness_assert(&rw->lock_object, what, file, line); 1193 #else 1194 /* 1195 * If we hold a write lock fail. We can't reliably check 1196 * to see if we hold a read lock or not. 1197 */ 1198 if (rw_wowner(rw) == curthread) 1199 panic("Lock %s exclusively locked @ %s:%d\n", 1200 rw->lock_object.lo_name, file, line); 1201 #endif 1202 break; 1203 default: 1204 panic("Unknown rw lock assertion: %d @ %s:%d", what, file, 1205 line); 1206 } 1207 } 1208 #endif /* INVARIANT_SUPPORT */ 1209 1210 #ifdef DDB 1211 void 1212 db_show_rwlock(const struct lock_object *lock) 1213 { 1214 const struct rwlock *rw; 1215 struct thread *td; 1216 1217 rw = (const struct rwlock *)lock; 1218 1219 db_printf(" state: "); 1220 if (rw->rw_lock == RW_UNLOCKED) 1221 db_printf("UNLOCKED\n"); 1222 else if (rw->rw_lock == RW_DESTROYED) { 1223 db_printf("DESTROYED\n"); 1224 return; 1225 } else if (rw->rw_lock & RW_LOCK_READ) 1226 db_printf("RLOCK: %ju locks\n", 1227 (uintmax_t)(RW_READERS(rw->rw_lock))); 1228 else { 1229 td = rw_wowner(rw); 1230 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1231 td->td_tid, td->td_proc->p_pid, td->td_name); 1232 if (rw_recursed(rw)) 1233 db_printf(" recursed: %u\n", rw->rw_recurse); 1234 } 1235 db_printf(" waiters: "); 1236 switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) { 1237 case RW_LOCK_READ_WAITERS: 1238 db_printf("readers\n"); 1239 break; 1240 case RW_LOCK_WRITE_WAITERS: 1241 db_printf("writers\n"); 1242 break; 1243 case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS: 1244 db_printf("readers and writers\n"); 1245 break; 1246 default: 1247 db_printf("none\n"); 1248 break; 1249 } 1250 } 1251 1252 #endif 1253