1 /* $NetBSD: kern_mutex.c,v 1.98 2021/08/25 04:13:42 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2002, 2006, 2007, 2008, 2019 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe and Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Kernel mutex implementation, modeled after those found in Solaris, 34 * a description of which can be found in: 35 * 36 * Solaris Internals: Core Kernel Architecture, Jim Mauro and 37 * Richard McDougall. 38 */ 39 40 #define __MUTEX_PRIVATE 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: kern_mutex.c,v 1.98 2021/08/25 04:13:42 thorpej Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/atomic.h> 47 #include <sys/proc.h> 48 #include <sys/mutex.h> 49 #include <sys/sched.h> 50 #include <sys/sleepq.h> 51 #include <sys/systm.h> 52 #include <sys/lockdebug.h> 53 #include <sys/kernel.h> 54 #include <sys/intr.h> 55 #include <sys/lock.h> 56 #include <sys/types.h> 57 #include <sys/cpu.h> 58 #include <sys/pserialize.h> 59 60 #include <dev/lockstat.h> 61 62 #include <machine/lock.h> 63 64 /* 65 * When not running a debug kernel, spin mutexes are not much 66 * more than an splraiseipl() and splx() pair. 67 */ 68 69 #if defined(DIAGNOSTIC) || defined(MULTIPROCESSOR) || defined(LOCKDEBUG) 70 #define FULL 71 #endif 72 73 /* 74 * Debugging support. 75 */ 76 77 #define MUTEX_WANTLOCK(mtx) \ 78 LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx), \ 79 (uintptr_t)__builtin_return_address(0), 0) 80 #define MUTEX_TESTLOCK(mtx) \ 81 LOCKDEBUG_WANTLOCK(MUTEX_DEBUG_P(mtx), (mtx), \ 82 (uintptr_t)__builtin_return_address(0), -1) 83 #define MUTEX_LOCKED(mtx) \ 84 LOCKDEBUG_LOCKED(MUTEX_DEBUG_P(mtx), (mtx), NULL, \ 85 (uintptr_t)__builtin_return_address(0), 0) 86 #define MUTEX_UNLOCKED(mtx) \ 87 LOCKDEBUG_UNLOCKED(MUTEX_DEBUG_P(mtx), (mtx), \ 88 (uintptr_t)__builtin_return_address(0), 0) 89 #define MUTEX_ABORT(mtx, msg) \ 90 mutex_abort(__func__, __LINE__, mtx, msg) 91 92 #if defined(LOCKDEBUG) 93 94 #define MUTEX_DASSERT(mtx, cond) \ 95 do { \ 96 if (__predict_false(!(cond))) \ 97 MUTEX_ABORT(mtx, "assertion failed: " #cond); \ 98 } while (/* CONSTCOND */ 0) 99 100 #else /* LOCKDEBUG */ 101 102 #define MUTEX_DASSERT(mtx, cond) /* nothing */ 103 104 #endif /* LOCKDEBUG */ 105 106 #if defined(DIAGNOSTIC) 107 108 #define MUTEX_ASSERT(mtx, cond) \ 109 do { \ 110 if (__predict_false(!(cond))) \ 111 MUTEX_ABORT(mtx, "assertion failed: " #cond); \ 112 } while (/* CONSTCOND */ 0) 113 114 #else /* DIAGNOSTIC */ 115 116 #define MUTEX_ASSERT(mtx, cond) /* nothing */ 117 118 #endif /* DIAGNOSTIC */ 119 120 /* 121 * Some architectures can't use __cpu_simple_lock as is so allow a way 122 * for them to use an alternate definition. 123 */ 124 #ifndef MUTEX_SPINBIT_LOCK_INIT 125 #define MUTEX_SPINBIT_LOCK_INIT(mtx) __cpu_simple_lock_init(&(mtx)->mtx_lock) 126 #endif 127 #ifndef MUTEX_SPINBIT_LOCKED_P 128 #define MUTEX_SPINBIT_LOCKED_P(mtx) __SIMPLELOCK_LOCKED_P(&(mtx)->mtx_lock) 129 #endif 130 #ifndef MUTEX_SPINBIT_LOCK_TRY 131 #define MUTEX_SPINBIT_LOCK_TRY(mtx) __cpu_simple_lock_try(&(mtx)->mtx_lock) 132 #endif 133 #ifndef MUTEX_SPINBIT_LOCK_UNLOCK 134 #define MUTEX_SPINBIT_LOCK_UNLOCK(mtx) __cpu_simple_unlock(&(mtx)->mtx_lock) 135 #endif 136 137 #ifndef MUTEX_INITIALIZE_SPIN_IPL 138 #define MUTEX_INITIALIZE_SPIN_IPL(mtx, ipl) \ 139 ((mtx)->mtx_ipl = makeiplcookie((ipl))) 140 #endif 141 142 /* 143 * Spin mutex SPL save / restore. 144 */ 145 146 #define MUTEX_SPIN_SPLRAISE(mtx) \ 147 do { \ 148 struct cpu_info *x__ci; \ 149 int x__cnt, s; \ 150 s = splraiseipl(MUTEX_SPIN_IPL(mtx)); \ 151 x__ci = curcpu(); \ 152 x__cnt = x__ci->ci_mtx_count--; \ 153 __insn_barrier(); \ 154 if (x__cnt == 0) \ 155 x__ci->ci_mtx_oldspl = (s); \ 156 } while (/* CONSTCOND */ 0) 157 158 #define MUTEX_SPIN_SPLRESTORE(mtx) \ 159 do { \ 160 struct cpu_info *x__ci = curcpu(); \ 161 int s = x__ci->ci_mtx_oldspl; \ 162 __insn_barrier(); \ 163 if (++(x__ci->ci_mtx_count) == 0) \ 164 splx(s); \ 165 } while (/* CONSTCOND */ 0) 166 167 /* 168 * Memory barriers. 169 */ 170 #ifdef __HAVE_ATOMIC_AS_MEMBAR 171 #define MUTEX_MEMBAR_ENTER() 172 #define MUTEX_MEMBAR_EXIT() 173 #else 174 #define MUTEX_MEMBAR_ENTER() membar_enter() 175 #define MUTEX_MEMBAR_EXIT() membar_exit() 176 #endif 177 178 /* 179 * For architectures that provide 'simple' mutexes: they provide a 180 * CAS function that is either MP-safe, or does not need to be MP 181 * safe. Adaptive mutexes on these architectures do not require an 182 * additional interlock. 183 */ 184 185 #ifdef __HAVE_SIMPLE_MUTEXES 186 187 #define MUTEX_OWNER(owner) \ 188 (owner & MUTEX_THREAD) 189 #define MUTEX_HAS_WAITERS(mtx) \ 190 (((int)(mtx)->mtx_owner & MUTEX_BIT_WAITERS) != 0) 191 192 #define MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug) \ 193 do { \ 194 if (!dodebug) \ 195 (mtx)->mtx_owner |= MUTEX_BIT_NODEBUG; \ 196 } while (/* CONSTCOND */ 0) 197 198 #define MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl) \ 199 do { \ 200 (mtx)->mtx_owner = MUTEX_BIT_SPIN; \ 201 if (!dodebug) \ 202 (mtx)->mtx_owner |= MUTEX_BIT_NODEBUG; \ 203 MUTEX_INITIALIZE_SPIN_IPL((mtx), (ipl)); \ 204 MUTEX_SPINBIT_LOCK_INIT((mtx)); \ 205 } while (/* CONSTCOND */ 0) 206 207 #define MUTEX_DESTROY(mtx) \ 208 do { \ 209 (mtx)->mtx_owner = MUTEX_THREAD; \ 210 } while (/* CONSTCOND */ 0) 211 212 #define MUTEX_SPIN_P(owner) \ 213 (((owner) & MUTEX_BIT_SPIN) != 0) 214 #define MUTEX_ADAPTIVE_P(owner) \ 215 (((owner) & MUTEX_BIT_SPIN) == 0) 216 217 #ifndef MUTEX_CAS 218 #define MUTEX_CAS(p, o, n) \ 219 (atomic_cas_ulong((volatile unsigned long *)(p), (o), (n)) == (o)) 220 #endif /* MUTEX_CAS */ 221 222 #define MUTEX_DEBUG_P(mtx) (((mtx)->mtx_owner & MUTEX_BIT_NODEBUG) == 0) 223 #if defined(LOCKDEBUG) 224 #define MUTEX_OWNED(owner) (((owner) & ~MUTEX_BIT_NODEBUG) != 0) 225 #define MUTEX_INHERITDEBUG(n, o) (n) |= (o) & MUTEX_BIT_NODEBUG 226 #else /* defined(LOCKDEBUG) */ 227 #define MUTEX_OWNED(owner) ((owner) != 0) 228 #define MUTEX_INHERITDEBUG(n, o) /* nothing */ 229 #endif /* defined(LOCKDEBUG) */ 230 231 static inline int 232 MUTEX_ACQUIRE(kmutex_t *mtx, uintptr_t curthread) 233 { 234 int rv; 235 uintptr_t oldown = 0; 236 uintptr_t newown = curthread; 237 238 MUTEX_INHERITDEBUG(oldown, mtx->mtx_owner); 239 MUTEX_INHERITDEBUG(newown, oldown); 240 rv = MUTEX_CAS(&mtx->mtx_owner, oldown, newown); 241 MUTEX_MEMBAR_ENTER(); 242 return rv; 243 } 244 245 static inline int 246 MUTEX_SET_WAITERS(kmutex_t *mtx, uintptr_t owner) 247 { 248 int rv; 249 rv = MUTEX_CAS(&mtx->mtx_owner, owner, owner | MUTEX_BIT_WAITERS); 250 MUTEX_MEMBAR_ENTER(); 251 return rv; 252 } 253 254 static inline void 255 MUTEX_RELEASE(kmutex_t *mtx) 256 { 257 uintptr_t newown; 258 259 MUTEX_MEMBAR_EXIT(); 260 newown = 0; 261 MUTEX_INHERITDEBUG(newown, mtx->mtx_owner); 262 mtx->mtx_owner = newown; 263 } 264 #endif /* __HAVE_SIMPLE_MUTEXES */ 265 266 /* 267 * Patch in stubs via strong alias where they are not available. 268 */ 269 270 #if defined(LOCKDEBUG) 271 #undef __HAVE_MUTEX_STUBS 272 #undef __HAVE_SPIN_MUTEX_STUBS 273 #endif 274 275 #ifndef __HAVE_MUTEX_STUBS 276 __strong_alias(mutex_enter,mutex_vector_enter); 277 __strong_alias(mutex_exit,mutex_vector_exit); 278 #endif 279 280 #ifndef __HAVE_SPIN_MUTEX_STUBS 281 __strong_alias(mutex_spin_enter,mutex_vector_enter); 282 __strong_alias(mutex_spin_exit,mutex_vector_exit); 283 #endif 284 285 static void mutex_abort(const char *, size_t, const kmutex_t *, 286 const char *); 287 static void mutex_dump(const volatile void *, lockop_printer_t); 288 289 lockops_t mutex_spin_lockops = { 290 .lo_name = "Mutex", 291 .lo_type = LOCKOPS_SPIN, 292 .lo_dump = mutex_dump, 293 }; 294 295 lockops_t mutex_adaptive_lockops = { 296 .lo_name = "Mutex", 297 .lo_type = LOCKOPS_SLEEP, 298 .lo_dump = mutex_dump, 299 }; 300 301 syncobj_t mutex_syncobj = { 302 .sobj_flag = SOBJ_SLEEPQ_SORTED, 303 .sobj_unsleep = turnstile_unsleep, 304 .sobj_changepri = turnstile_changepri, 305 .sobj_lendpri = sleepq_lendpri, 306 .sobj_owner = (void *)mutex_owner, 307 }; 308 309 /* 310 * mutex_dump: 311 * 312 * Dump the contents of a mutex structure. 313 */ 314 static void 315 mutex_dump(const volatile void *cookie, lockop_printer_t pr) 316 { 317 const volatile kmutex_t *mtx = cookie; 318 uintptr_t owner = mtx->mtx_owner; 319 320 pr("owner field : %#018lx wait/spin: %16d/%d\n", 321 (long)MUTEX_OWNER(owner), MUTEX_HAS_WAITERS(mtx), 322 MUTEX_SPIN_P(owner)); 323 } 324 325 /* 326 * mutex_abort: 327 * 328 * Dump information about an error and panic the system. This 329 * generates a lot of machine code in the DIAGNOSTIC case, so 330 * we ask the compiler to not inline it. 331 */ 332 static void __noinline 333 mutex_abort(const char *func, size_t line, const kmutex_t *mtx, const char *msg) 334 { 335 336 LOCKDEBUG_ABORT(func, line, mtx, (MUTEX_SPIN_P(mtx->mtx_owner) ? 337 &mutex_spin_lockops : &mutex_adaptive_lockops), msg); 338 } 339 340 /* 341 * mutex_init: 342 * 343 * Initialize a mutex for use. Note that adaptive mutexes are in 344 * essence spin mutexes that can sleep to avoid deadlock and wasting 345 * CPU time. We can't easily provide a type of mutex that always 346 * sleeps - see comments in mutex_vector_enter() about releasing 347 * mutexes unlocked. 348 */ 349 void _mutex_init(kmutex_t *, kmutex_type_t, int, uintptr_t); 350 void 351 _mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl, 352 uintptr_t return_address) 353 { 354 lockops_t *lockops __unused; 355 bool dodebug; 356 357 memset(mtx, 0, sizeof(*mtx)); 358 359 if (ipl == IPL_NONE || ipl == IPL_SOFTCLOCK || 360 ipl == IPL_SOFTBIO || ipl == IPL_SOFTNET || 361 ipl == IPL_SOFTSERIAL) { 362 lockops = (type == MUTEX_NODEBUG ? 363 NULL : &mutex_adaptive_lockops); 364 dodebug = LOCKDEBUG_ALLOC(mtx, lockops, return_address); 365 MUTEX_INITIALIZE_ADAPTIVE(mtx, dodebug); 366 } else { 367 lockops = (type == MUTEX_NODEBUG ? 368 NULL : &mutex_spin_lockops); 369 dodebug = LOCKDEBUG_ALLOC(mtx, lockops, return_address); 370 MUTEX_INITIALIZE_SPIN(mtx, dodebug, ipl); 371 } 372 } 373 374 void 375 mutex_init(kmutex_t *mtx, kmutex_type_t type, int ipl) 376 { 377 378 _mutex_init(mtx, type, ipl, (uintptr_t)__builtin_return_address(0)); 379 } 380 381 /* 382 * mutex_destroy: 383 * 384 * Tear down a mutex. 385 */ 386 void 387 mutex_destroy(kmutex_t *mtx) 388 { 389 uintptr_t owner = mtx->mtx_owner; 390 391 if (MUTEX_ADAPTIVE_P(owner)) { 392 MUTEX_ASSERT(mtx, !MUTEX_OWNED(owner)); 393 MUTEX_ASSERT(mtx, !MUTEX_HAS_WAITERS(mtx)); 394 } else { 395 MUTEX_ASSERT(mtx, !MUTEX_SPINBIT_LOCKED_P(mtx)); 396 } 397 398 LOCKDEBUG_FREE(MUTEX_DEBUG_P(mtx), mtx); 399 MUTEX_DESTROY(mtx); 400 } 401 402 #ifdef MULTIPROCESSOR 403 /* 404 * mutex_oncpu: 405 * 406 * Return true if an adaptive mutex owner is running on a CPU in the 407 * system. If the target is waiting on the kernel big lock, then we 408 * must release it. This is necessary to avoid deadlock. 409 */ 410 static bool 411 mutex_oncpu(uintptr_t owner) 412 { 413 struct cpu_info *ci; 414 lwp_t *l; 415 416 KASSERT(kpreempt_disabled()); 417 418 if (!MUTEX_OWNED(owner)) { 419 return false; 420 } 421 422 /* 423 * See lwp_dtor() why dereference of the LWP pointer is safe. 424 * We must have kernel preemption disabled for that. 425 */ 426 l = (lwp_t *)MUTEX_OWNER(owner); 427 ci = l->l_cpu; 428 429 if (ci && ci->ci_curlwp == l) { 430 /* Target is running; do we need to block? */ 431 return (ci->ci_biglock_wanted != l); 432 } 433 434 /* Not running. It may be safe to block now. */ 435 return false; 436 } 437 #endif /* MULTIPROCESSOR */ 438 439 /* 440 * mutex_vector_enter: 441 * 442 * Support routine for mutex_enter() that must handle all cases. In 443 * the LOCKDEBUG case, mutex_enter() is always aliased here, even if 444 * fast-path stubs are available. If a mutex_spin_enter() stub is 445 * not available, then it is also aliased directly here. 446 */ 447 void 448 mutex_vector_enter(kmutex_t *mtx) 449 { 450 uintptr_t owner, curthread; 451 turnstile_t *ts; 452 #ifdef MULTIPROCESSOR 453 u_int count; 454 #endif 455 LOCKSTAT_COUNTER(spincnt); 456 LOCKSTAT_COUNTER(slpcnt); 457 LOCKSTAT_TIMER(spintime); 458 LOCKSTAT_TIMER(slptime); 459 LOCKSTAT_FLAG(lsflag); 460 461 /* 462 * Handle spin mutexes. 463 */ 464 KPREEMPT_DISABLE(curlwp); 465 owner = mtx->mtx_owner; 466 if (MUTEX_SPIN_P(owner)) { 467 #if defined(LOCKDEBUG) && defined(MULTIPROCESSOR) 468 u_int spins = 0; 469 #endif 470 KPREEMPT_ENABLE(curlwp); 471 MUTEX_SPIN_SPLRAISE(mtx); 472 MUTEX_WANTLOCK(mtx); 473 #ifdef FULL 474 if (MUTEX_SPINBIT_LOCK_TRY(mtx)) { 475 MUTEX_LOCKED(mtx); 476 return; 477 } 478 #if !defined(MULTIPROCESSOR) 479 MUTEX_ABORT(mtx, "locking against myself"); 480 #else /* !MULTIPROCESSOR */ 481 482 LOCKSTAT_ENTER(lsflag); 483 LOCKSTAT_START_TIMER(lsflag, spintime); 484 count = SPINLOCK_BACKOFF_MIN; 485 486 /* 487 * Spin testing the lock word and do exponential backoff 488 * to reduce cache line ping-ponging between CPUs. 489 */ 490 do { 491 while (MUTEX_SPINBIT_LOCKED_P(mtx)) { 492 SPINLOCK_SPIN_HOOK; 493 SPINLOCK_BACKOFF(count); 494 #ifdef LOCKDEBUG 495 if (SPINLOCK_SPINOUT(spins)) 496 MUTEX_ABORT(mtx, "spinout"); 497 #endif /* LOCKDEBUG */ 498 } 499 } while (!MUTEX_SPINBIT_LOCK_TRY(mtx)); 500 501 if (count != SPINLOCK_BACKOFF_MIN) { 502 LOCKSTAT_STOP_TIMER(lsflag, spintime); 503 LOCKSTAT_EVENT(lsflag, mtx, 504 LB_SPIN_MUTEX | LB_SPIN, 1, spintime); 505 } 506 LOCKSTAT_EXIT(lsflag); 507 #endif /* !MULTIPROCESSOR */ 508 #endif /* FULL */ 509 MUTEX_LOCKED(mtx); 510 return; 511 } 512 513 curthread = (uintptr_t)curlwp; 514 515 MUTEX_DASSERT(mtx, MUTEX_ADAPTIVE_P(owner)); 516 MUTEX_ASSERT(mtx, curthread != 0); 517 MUTEX_ASSERT(mtx, !cpu_intr_p()); 518 MUTEX_WANTLOCK(mtx); 519 520 if (panicstr == NULL) { 521 KDASSERT(pserialize_not_in_read_section()); 522 LOCKDEBUG_BARRIER(&kernel_lock, 1); 523 } 524 525 LOCKSTAT_ENTER(lsflag); 526 527 /* 528 * Adaptive mutex; spin trying to acquire the mutex. If we 529 * determine that the owner is not running on a processor, 530 * then we stop spinning, and sleep instead. 531 */ 532 for (;;) { 533 if (!MUTEX_OWNED(owner)) { 534 /* 535 * Mutex owner clear could mean two things: 536 * 537 * * The mutex has been released. 538 * * The owner field hasn't been set yet. 539 * 540 * Try to acquire it again. If that fails, 541 * we'll just loop again. 542 */ 543 if (MUTEX_ACQUIRE(mtx, curthread)) 544 break; 545 owner = mtx->mtx_owner; 546 continue; 547 } 548 if (__predict_false(MUTEX_OWNER(owner) == curthread)) { 549 MUTEX_ABORT(mtx, "locking against myself"); 550 } 551 #ifdef MULTIPROCESSOR 552 /* 553 * Check to see if the owner is running on a processor. 554 * If so, then we should just spin, as the owner will 555 * likely release the lock very soon. 556 */ 557 if (mutex_oncpu(owner)) { 558 LOCKSTAT_START_TIMER(lsflag, spintime); 559 count = SPINLOCK_BACKOFF_MIN; 560 do { 561 KPREEMPT_ENABLE(curlwp); 562 SPINLOCK_BACKOFF(count); 563 KPREEMPT_DISABLE(curlwp); 564 owner = mtx->mtx_owner; 565 } while (mutex_oncpu(owner)); 566 LOCKSTAT_STOP_TIMER(lsflag, spintime); 567 LOCKSTAT_COUNT(spincnt, 1); 568 if (!MUTEX_OWNED(owner)) 569 continue; 570 } 571 #endif 572 573 ts = turnstile_lookup(mtx); 574 575 /* 576 * Once we have the turnstile chain interlock, mark the 577 * mutex as having waiters. If that fails, spin again: 578 * chances are that the mutex has been released. 579 */ 580 if (!MUTEX_SET_WAITERS(mtx, owner)) { 581 turnstile_exit(mtx); 582 owner = mtx->mtx_owner; 583 continue; 584 } 585 586 #ifdef MULTIPROCESSOR 587 /* 588 * mutex_exit() is permitted to release the mutex without 589 * any interlocking instructions, and the following can 590 * occur as a result: 591 * 592 * CPU 1: MUTEX_SET_WAITERS() CPU2: mutex_exit() 593 * ---------------------------- ---------------------------- 594 * .. acquire cache line 595 * .. test for waiters 596 * acquire cache line <- lose cache line 597 * lock cache line .. 598 * verify mutex is held .. 599 * set waiters .. 600 * unlock cache line .. 601 * lose cache line -> acquire cache line 602 * .. clear lock word, waiters 603 * return success 604 * 605 * There is another race that can occur: a third CPU could 606 * acquire the mutex as soon as it is released. Since 607 * adaptive mutexes are primarily spin mutexes, this is not 608 * something that we need to worry about too much. What we 609 * do need to ensure is that the waiters bit gets set. 610 * 611 * To allow the unlocked release, we need to make some 612 * assumptions here: 613 * 614 * o Release is the only non-atomic/unlocked operation 615 * that can be performed on the mutex. (It must still 616 * be atomic on the local CPU, e.g. in case interrupted 617 * or preempted). 618 * 619 * o At any given time, MUTEX_SET_WAITERS() can only ever 620 * be in progress on one CPU in the system - guaranteed 621 * by the turnstile chain lock. 622 * 623 * o No other operations other than MUTEX_SET_WAITERS() 624 * and release can modify a mutex with a non-zero 625 * owner field. 626 * 627 * o The result of a successful MUTEX_SET_WAITERS() call 628 * is an unbuffered write that is immediately visible 629 * to all other processors in the system. 630 * 631 * o If the holding LWP switches away, it posts a store 632 * fence before changing curlwp, ensuring that any 633 * overwrite of the mutex waiters flag by mutex_exit() 634 * completes before the modification of curlwp becomes 635 * visible to this CPU. 636 * 637 * o cpu_switchto() posts a store fence after setting curlwp 638 * and before resuming execution of an LWP. 639 * 640 * o _kernel_lock() posts a store fence before setting 641 * curcpu()->ci_biglock_wanted, and after clearing it. 642 * This ensures that any overwrite of the mutex waiters 643 * flag by mutex_exit() completes before the modification 644 * of ci_biglock_wanted becomes visible. 645 * 646 * We now post a read memory barrier (after setting the 647 * waiters field) and check the lock holder's status again. 648 * Some of the possible outcomes (not an exhaustive list): 649 * 650 * 1. The on-CPU check returns true: the holding LWP is 651 * running again. The lock may be released soon and 652 * we should spin. Importantly, we can't trust the 653 * value of the waiters flag. 654 * 655 * 2. The on-CPU check returns false: the holding LWP is 656 * not running. We now have the opportunity to check 657 * if mutex_exit() has blatted the modifications made 658 * by MUTEX_SET_WAITERS(). 659 * 660 * 3. The on-CPU check returns false: the holding LWP may 661 * or may not be running. It has context switched at 662 * some point during our check. Again, we have the 663 * chance to see if the waiters bit is still set or 664 * has been overwritten. 665 * 666 * 4. The on-CPU check returns false: the holding LWP is 667 * running on a CPU, but wants the big lock. It's OK 668 * to check the waiters field in this case. 669 * 670 * 5. The has-waiters check fails: the mutex has been 671 * released, the waiters flag cleared and another LWP 672 * now owns the mutex. 673 * 674 * 6. The has-waiters check fails: the mutex has been 675 * released. 676 * 677 * If the waiters bit is not set it's unsafe to go asleep, 678 * as we might never be awoken. 679 */ 680 membar_consumer(); 681 if (mutex_oncpu(owner)) { 682 turnstile_exit(mtx); 683 owner = mtx->mtx_owner; 684 continue; 685 } 686 membar_consumer(); 687 if (!MUTEX_HAS_WAITERS(mtx)) { 688 turnstile_exit(mtx); 689 owner = mtx->mtx_owner; 690 continue; 691 } 692 #endif /* MULTIPROCESSOR */ 693 694 LOCKSTAT_START_TIMER(lsflag, slptime); 695 696 turnstile_block(ts, TS_WRITER_Q, mtx, &mutex_syncobj); 697 698 LOCKSTAT_STOP_TIMER(lsflag, slptime); 699 LOCKSTAT_COUNT(slpcnt, 1); 700 701 owner = mtx->mtx_owner; 702 } 703 KPREEMPT_ENABLE(curlwp); 704 705 LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SLEEP1, 706 slpcnt, slptime); 707 LOCKSTAT_EVENT(lsflag, mtx, LB_ADAPTIVE_MUTEX | LB_SPIN, 708 spincnt, spintime); 709 LOCKSTAT_EXIT(lsflag); 710 711 MUTEX_DASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread); 712 MUTEX_LOCKED(mtx); 713 } 714 715 /* 716 * mutex_vector_exit: 717 * 718 * Support routine for mutex_exit() that handles all cases. 719 */ 720 void 721 mutex_vector_exit(kmutex_t *mtx) 722 { 723 turnstile_t *ts; 724 uintptr_t curthread; 725 726 if (MUTEX_SPIN_P(mtx->mtx_owner)) { 727 #ifdef FULL 728 if (__predict_false(!MUTEX_SPINBIT_LOCKED_P(mtx))) { 729 MUTEX_ABORT(mtx, "exiting unheld spin mutex"); 730 } 731 MUTEX_UNLOCKED(mtx); 732 MUTEX_SPINBIT_LOCK_UNLOCK(mtx); 733 #endif 734 MUTEX_SPIN_SPLRESTORE(mtx); 735 return; 736 } 737 738 #ifndef __HAVE_MUTEX_STUBS 739 /* 740 * On some architectures without mutex stubs, we can enter here to 741 * release mutexes before interrupts and whatnot are up and running. 742 * We need this hack to keep them sweet. 743 */ 744 if (__predict_false(cold)) { 745 MUTEX_UNLOCKED(mtx); 746 MUTEX_RELEASE(mtx); 747 return; 748 } 749 #endif 750 751 curthread = (uintptr_t)curlwp; 752 MUTEX_DASSERT(mtx, curthread != 0); 753 MUTEX_ASSERT(mtx, MUTEX_OWNER(mtx->mtx_owner) == curthread); 754 MUTEX_UNLOCKED(mtx); 755 #if !defined(LOCKDEBUG) 756 __USE(curthread); 757 #endif 758 759 #ifdef LOCKDEBUG 760 /* 761 * Avoid having to take the turnstile chain lock every time 762 * around. Raise the priority level to splhigh() in order 763 * to disable preemption and so make the following atomic. 764 */ 765 { 766 int s = splhigh(); 767 if (!MUTEX_HAS_WAITERS(mtx)) { 768 MUTEX_RELEASE(mtx); 769 splx(s); 770 return; 771 } 772 splx(s); 773 } 774 #endif 775 776 /* 777 * Get this lock's turnstile. This gets the interlock on 778 * the sleep queue. Once we have that, we can clear the 779 * lock. If there was no turnstile for the lock, there 780 * were no waiters remaining. 781 */ 782 ts = turnstile_lookup(mtx); 783 784 if (ts == NULL) { 785 MUTEX_RELEASE(mtx); 786 turnstile_exit(mtx); 787 } else { 788 MUTEX_RELEASE(mtx); 789 turnstile_wakeup(ts, TS_WRITER_Q, 790 TS_WAITERS(ts, TS_WRITER_Q), NULL); 791 } 792 } 793 794 #ifndef __HAVE_SIMPLE_MUTEXES 795 /* 796 * mutex_wakeup: 797 * 798 * Support routine for mutex_exit() that wakes up all waiters. 799 * We assume that the mutex has been released, but it need not 800 * be. 801 */ 802 void 803 mutex_wakeup(kmutex_t *mtx) 804 { 805 turnstile_t *ts; 806 807 ts = turnstile_lookup(mtx); 808 if (ts == NULL) { 809 turnstile_exit(mtx); 810 return; 811 } 812 MUTEX_CLEAR_WAITERS(mtx); 813 turnstile_wakeup(ts, TS_WRITER_Q, TS_WAITERS(ts, TS_WRITER_Q), NULL); 814 } 815 #endif /* !__HAVE_SIMPLE_MUTEXES */ 816 817 /* 818 * mutex_owned: 819 * 820 * Return true if the current LWP (adaptive) or CPU (spin) 821 * holds the mutex. 822 */ 823 int 824 mutex_owned(const kmutex_t *mtx) 825 { 826 827 if (mtx == NULL) 828 return 0; 829 if (MUTEX_ADAPTIVE_P(mtx->mtx_owner)) 830 return MUTEX_OWNER(mtx->mtx_owner) == (uintptr_t)curlwp; 831 #ifdef FULL 832 return MUTEX_SPINBIT_LOCKED_P(mtx); 833 #else 834 return 1; 835 #endif 836 } 837 838 /* 839 * mutex_owner: 840 * 841 * Return the current owner of an adaptive mutex. Used for 842 * priority inheritance. 843 */ 844 lwp_t * 845 mutex_owner(const kmutex_t *mtx) 846 { 847 848 MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx->mtx_owner)); 849 return (struct lwp *)MUTEX_OWNER(mtx->mtx_owner); 850 } 851 852 /* 853 * mutex_owner_running: 854 * 855 * Return true if an adaptive mutex is unheld, or held and the owner is 856 * running on a CPU. For the pagedaemon only - do not document or use 857 * in other code. 858 */ 859 bool 860 mutex_owner_running(const kmutex_t *mtx) 861 { 862 #ifdef MULTIPROCESSOR 863 uintptr_t owner; 864 bool rv; 865 866 MUTEX_ASSERT(mtx, MUTEX_ADAPTIVE_P(mtx->mtx_owner)); 867 kpreempt_disable(); 868 owner = mtx->mtx_owner; 869 rv = !MUTEX_OWNED(owner) || mutex_oncpu(MUTEX_OWNER(owner)); 870 kpreempt_enable(); 871 return rv; 872 #else 873 return mutex_owner(mtx) == curlwp; 874 #endif 875 } 876 877 /* 878 * mutex_ownable: 879 * 880 * When compiled with DEBUG and LOCKDEBUG defined, ensure that 881 * the mutex is available. We cannot use !mutex_owned() since 882 * that won't work correctly for spin mutexes. 883 */ 884 int 885 mutex_ownable(const kmutex_t *mtx) 886 { 887 888 #ifdef LOCKDEBUG 889 MUTEX_TESTLOCK(mtx); 890 #endif 891 return 1; 892 } 893 894 /* 895 * mutex_tryenter: 896 * 897 * Try to acquire the mutex; return non-zero if we did. 898 */ 899 int 900 mutex_tryenter(kmutex_t *mtx) 901 { 902 uintptr_t curthread; 903 904 /* 905 * Handle spin mutexes. 906 */ 907 if (MUTEX_SPIN_P(mtx->mtx_owner)) { 908 MUTEX_SPIN_SPLRAISE(mtx); 909 #ifdef FULL 910 if (MUTEX_SPINBIT_LOCK_TRY(mtx)) { 911 MUTEX_WANTLOCK(mtx); 912 MUTEX_LOCKED(mtx); 913 return 1; 914 } 915 MUTEX_SPIN_SPLRESTORE(mtx); 916 #else 917 MUTEX_WANTLOCK(mtx); 918 MUTEX_LOCKED(mtx); 919 return 1; 920 #endif 921 } else { 922 curthread = (uintptr_t)curlwp; 923 MUTEX_ASSERT(mtx, curthread != 0); 924 if (MUTEX_ACQUIRE(mtx, curthread)) { 925 MUTEX_WANTLOCK(mtx); 926 MUTEX_LOCKED(mtx); 927 MUTEX_DASSERT(mtx, 928 MUTEX_OWNER(mtx->mtx_owner) == curthread); 929 return 1; 930 } 931 } 932 933 return 0; 934 } 935 936 #if defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) 937 /* 938 * mutex_spin_retry: 939 * 940 * Support routine for mutex_spin_enter(). Assumes that the caller 941 * has already raised the SPL, and adjusted counters. 942 */ 943 void 944 mutex_spin_retry(kmutex_t *mtx) 945 { 946 #ifdef MULTIPROCESSOR 947 u_int count; 948 LOCKSTAT_TIMER(spintime); 949 LOCKSTAT_FLAG(lsflag); 950 #ifdef LOCKDEBUG 951 u_int spins = 0; 952 #endif /* LOCKDEBUG */ 953 954 MUTEX_WANTLOCK(mtx); 955 956 LOCKSTAT_ENTER(lsflag); 957 LOCKSTAT_START_TIMER(lsflag, spintime); 958 count = SPINLOCK_BACKOFF_MIN; 959 960 /* 961 * Spin testing the lock word and do exponential backoff 962 * to reduce cache line ping-ponging between CPUs. 963 */ 964 do { 965 while (MUTEX_SPINBIT_LOCKED_P(mtx)) { 966 SPINLOCK_BACKOFF(count); 967 #ifdef LOCKDEBUG 968 if (SPINLOCK_SPINOUT(spins)) 969 MUTEX_ABORT(mtx, "spinout"); 970 #endif /* LOCKDEBUG */ 971 } 972 } while (!MUTEX_SPINBIT_LOCK_TRY(mtx)); 973 974 LOCKSTAT_STOP_TIMER(lsflag, spintime); 975 LOCKSTAT_EVENT(lsflag, mtx, LB_SPIN_MUTEX | LB_SPIN, 1, spintime); 976 LOCKSTAT_EXIT(lsflag); 977 978 MUTEX_LOCKED(mtx); 979 #else /* MULTIPROCESSOR */ 980 MUTEX_ABORT(mtx, "locking against myself"); 981 #endif /* MULTIPROCESSOR */ 982 } 983 #endif /* defined(__HAVE_SPIN_MUTEX_STUBS) || defined(FULL) */ 984