1 /*- 2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Berkeley Software Design Inc's name may not be used to endorse or 13 * promote products derived from this software without specific prior 14 * written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 29 * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 30 */ 31 32 /* 33 * Machine independent bits of mutex implementation. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include "opt_adaptive_mutexes.h" 40 #include "opt_ddb.h" 41 #include "opt_hwpmc_hooks.h" 42 #include "opt_sched.h" 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/bus.h> 47 #include <sys/conf.h> 48 #include <sys/kdb.h> 49 #include <sys/kernel.h> 50 #include <sys/ktr.h> 51 #include <sys/lock.h> 52 #include <sys/malloc.h> 53 #include <sys/mutex.h> 54 #include <sys/proc.h> 55 #include <sys/resourcevar.h> 56 #include <sys/sched.h> 57 #include <sys/sbuf.h> 58 #include <sys/smp.h> 59 #include <sys/sysctl.h> 60 #include <sys/turnstile.h> 61 #include <sys/vmmeter.h> 62 #include <sys/lock_profile.h> 63 64 #include <machine/atomic.h> 65 #include <machine/bus.h> 66 #include <machine/cpu.h> 67 68 #include <ddb/ddb.h> 69 70 #include <fs/devfs/devfs_int.h> 71 72 #include <vm/vm.h> 73 #include <vm/vm_extern.h> 74 75 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 76 #define ADAPTIVE_MUTEXES 77 #endif 78 79 #ifdef HWPMC_HOOKS 80 #include <sys/pmckern.h> 81 PMC_SOFT_DEFINE( , , lock, failed); 82 #endif 83 84 /* 85 * Return the mutex address when the lock cookie address is provided. 86 * This functionality assumes that struct mtx* have a member named mtx_lock. 87 */ 88 #define mtxlock2mtx(c) (__containerof(c, struct mtx, mtx_lock)) 89 90 /* 91 * Internal utility macros. 92 */ 93 #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 94 95 #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED) 96 97 static void assert_mtx(const struct lock_object *lock, int what); 98 #ifdef DDB 99 static void db_show_mtx(const struct lock_object *lock); 100 #endif 101 static void lock_mtx(struct lock_object *lock, uintptr_t how); 102 static void lock_spin(struct lock_object *lock, uintptr_t how); 103 #ifdef KDTRACE_HOOKS 104 static int owner_mtx(const struct lock_object *lock, 105 struct thread **owner); 106 #endif 107 static uintptr_t unlock_mtx(struct lock_object *lock); 108 static uintptr_t unlock_spin(struct lock_object *lock); 109 110 /* 111 * Lock classes for sleep and spin mutexes. 112 */ 113 struct lock_class lock_class_mtx_sleep = { 114 .lc_name = "sleep mutex", 115 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, 116 .lc_assert = assert_mtx, 117 #ifdef DDB 118 .lc_ddb_show = db_show_mtx, 119 #endif 120 .lc_lock = lock_mtx, 121 .lc_unlock = unlock_mtx, 122 #ifdef KDTRACE_HOOKS 123 .lc_owner = owner_mtx, 124 #endif 125 }; 126 struct lock_class lock_class_mtx_spin = { 127 .lc_name = "spin mutex", 128 .lc_flags = LC_SPINLOCK | LC_RECURSABLE, 129 .lc_assert = assert_mtx, 130 #ifdef DDB 131 .lc_ddb_show = db_show_mtx, 132 #endif 133 .lc_lock = lock_spin, 134 .lc_unlock = unlock_spin, 135 #ifdef KDTRACE_HOOKS 136 .lc_owner = owner_mtx, 137 #endif 138 }; 139 140 #ifdef ADAPTIVE_MUTEXES 141 static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging"); 142 143 static struct lock_delay_config __read_frequently mtx_delay; 144 145 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base, 146 0, ""); 147 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max, 148 0, ""); 149 150 LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay); 151 #endif 152 153 static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL, 154 "mtx spin debugging"); 155 156 static struct lock_delay_config __read_frequently mtx_spin_delay; 157 158 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW, 159 &mtx_spin_delay.base, 0, ""); 160 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW, 161 &mtx_spin_delay.max, 0, ""); 162 163 LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay); 164 165 /* 166 * System-wide mutexes 167 */ 168 struct mtx blocked_lock; 169 struct mtx __exclusive_cache_line Giant; 170 171 void 172 assert_mtx(const struct lock_object *lock, int what) 173 { 174 175 mtx_assert((const struct mtx *)lock, what); 176 } 177 178 void 179 lock_mtx(struct lock_object *lock, uintptr_t how) 180 { 181 182 mtx_lock((struct mtx *)lock); 183 } 184 185 void 186 lock_spin(struct lock_object *lock, uintptr_t how) 187 { 188 189 panic("spin locks can only use msleep_spin"); 190 } 191 192 uintptr_t 193 unlock_mtx(struct lock_object *lock) 194 { 195 struct mtx *m; 196 197 m = (struct mtx *)lock; 198 mtx_assert(m, MA_OWNED | MA_NOTRECURSED); 199 mtx_unlock(m); 200 return (0); 201 } 202 203 uintptr_t 204 unlock_spin(struct lock_object *lock) 205 { 206 207 panic("spin locks can only use msleep_spin"); 208 } 209 210 #ifdef KDTRACE_HOOKS 211 int 212 owner_mtx(const struct lock_object *lock, struct thread **owner) 213 { 214 const struct mtx *m; 215 uintptr_t x; 216 217 m = (const struct mtx *)lock; 218 x = m->mtx_lock; 219 *owner = (struct thread *)(x & ~MTX_FLAGMASK); 220 return (*owner != NULL); 221 } 222 #endif 223 224 /* 225 * Function versions of the inlined __mtx_* macros. These are used by 226 * modules and can also be called from assembly language if needed. 227 */ 228 void 229 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 230 { 231 struct mtx *m; 232 uintptr_t tid, v; 233 234 m = mtxlock2mtx(c); 235 236 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 237 !TD_IS_IDLETHREAD(curthread), 238 ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d", 239 curthread, m->lock_object.lo_name, file, line)); 240 KASSERT(m->mtx_lock != MTX_DESTROYED, 241 ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); 242 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 243 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 244 file, line)); 245 WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) | 246 LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 247 248 tid = (uintptr_t)curthread; 249 v = MTX_UNOWNED; 250 if (!_mtx_obtain_lock_fetch(m, &v, tid)) 251 _mtx_lock_sleep(m, v, opts, file, line); 252 else 253 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, 254 m, 0, 0, file, line); 255 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 256 line); 257 WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE, 258 file, line); 259 TD_LOCKS_INC(curthread); 260 } 261 262 void 263 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 264 { 265 struct mtx *m; 266 267 m = mtxlock2mtx(c); 268 269 KASSERT(m->mtx_lock != MTX_DESTROYED, 270 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); 271 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 272 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 273 file, line)); 274 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 275 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 276 line); 277 mtx_assert(m, MA_OWNED); 278 279 #ifdef LOCK_PROFILING 280 __mtx_unlock_sleep(c, opts, file, line); 281 #else 282 __mtx_unlock(m, curthread, opts, file, line); 283 #endif 284 TD_LOCKS_DEC(curthread); 285 } 286 287 void 288 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 289 int line) 290 { 291 struct mtx *m; 292 #ifdef SMP 293 uintptr_t tid, v; 294 #endif 295 296 m = mtxlock2mtx(c); 297 298 KASSERT(m->mtx_lock != MTX_DESTROYED, 299 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); 300 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 301 ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 302 m->lock_object.lo_name, file, line)); 303 if (mtx_owned(m)) 304 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 305 (opts & MTX_RECURSE) != 0, 306 ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n", 307 m->lock_object.lo_name, file, line)); 308 opts &= ~MTX_RECURSE; 309 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 310 file, line, NULL); 311 #ifdef SMP 312 spinlock_enter(); 313 tid = (uintptr_t)curthread; 314 v = MTX_UNOWNED; 315 if (!_mtx_obtain_lock_fetch(m, &v, tid)) 316 _mtx_lock_spin(m, v, opts, file, line); 317 else 318 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, 319 m, 0, 0, file, line); 320 #else 321 __mtx_lock_spin(m, curthread, opts, file, line); 322 #endif 323 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 324 line); 325 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 326 } 327 328 int 329 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 330 int line) 331 { 332 struct mtx *m; 333 334 if (SCHEDULER_STOPPED()) 335 return (1); 336 337 m = mtxlock2mtx(c); 338 339 KASSERT(m->mtx_lock != MTX_DESTROYED, 340 ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line)); 341 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 342 ("mtx_trylock_spin() of sleep mutex %s @ %s:%d", 343 m->lock_object.lo_name, file, line)); 344 KASSERT((opts & MTX_RECURSE) == 0, 345 ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n", 346 m->lock_object.lo_name, file, line)); 347 if (__mtx_trylock_spin(m, curthread, opts, file, line)) { 348 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line); 349 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 350 return (1); 351 } 352 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line); 353 return (0); 354 } 355 356 void 357 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 358 int line) 359 { 360 struct mtx *m; 361 362 m = mtxlock2mtx(c); 363 364 KASSERT(m->mtx_lock != MTX_DESTROYED, 365 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); 366 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 367 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 368 m->lock_object.lo_name, file, line)); 369 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 370 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 371 line); 372 mtx_assert(m, MA_OWNED); 373 374 __mtx_unlock_spin(m); 375 } 376 377 /* 378 * The important part of mtx_trylock{,_flags}() 379 * Tries to acquire lock `m.' If this function is called on a mutex that 380 * is already owned, it will recursively acquire the lock. 381 */ 382 int 383 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line) 384 { 385 struct mtx *m; 386 struct thread *td; 387 uintptr_t tid, v; 388 #ifdef LOCK_PROFILING 389 uint64_t waittime = 0; 390 int contested = 0; 391 #endif 392 int rval; 393 bool recursed; 394 395 td = curthread; 396 tid = (uintptr_t)td; 397 if (SCHEDULER_STOPPED_TD(td)) 398 return (1); 399 400 m = mtxlock2mtx(c); 401 402 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td), 403 ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d", 404 curthread, m->lock_object.lo_name, file, line)); 405 KASSERT(m->mtx_lock != MTX_DESTROYED, 406 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); 407 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 408 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 409 file, line)); 410 411 rval = 1; 412 recursed = false; 413 v = MTX_UNOWNED; 414 for (;;) { 415 if (_mtx_obtain_lock_fetch(m, &v, tid)) 416 break; 417 if (v == MTX_UNOWNED) 418 continue; 419 if (v == tid && 420 ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 421 (opts & MTX_RECURSE) != 0)) { 422 m->mtx_recurse++; 423 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 424 recursed = true; 425 break; 426 } 427 rval = 0; 428 break; 429 } 430 431 opts &= ~MTX_RECURSE; 432 433 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line); 434 if (rval) { 435 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 436 file, line); 437 TD_LOCKS_INC(curthread); 438 if (!recursed) 439 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, 440 m, contested, waittime, file, line); 441 } 442 443 return (rval); 444 } 445 446 /* 447 * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 448 * 449 * We call this if the lock is either contested (i.e. we need to go to 450 * sleep waiting for it), or if we need to recurse on it. 451 */ 452 #if LOCK_DEBUG > 0 453 void 454 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file, 455 int line) 456 #else 457 void 458 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v) 459 #endif 460 { 461 struct thread *td; 462 struct mtx *m; 463 struct turnstile *ts; 464 uintptr_t tid; 465 struct thread *owner; 466 #ifdef KTR 467 int cont_logged = 0; 468 #endif 469 #ifdef LOCK_PROFILING 470 int contested = 0; 471 uint64_t waittime = 0; 472 #endif 473 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS) 474 struct lock_delay_arg lda; 475 #endif 476 #ifdef KDTRACE_HOOKS 477 u_int sleep_cnt = 0; 478 int64_t sleep_time = 0; 479 int64_t all_time = 0; 480 #endif 481 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 482 int doing_lockprof; 483 #endif 484 td = curthread; 485 tid = (uintptr_t)td; 486 if (SCHEDULER_STOPPED_TD(td)) 487 return; 488 489 #if defined(ADAPTIVE_MUTEXES) 490 lock_delay_arg_init(&lda, &mtx_delay); 491 #elif defined(KDTRACE_HOOKS) 492 lock_delay_arg_init(&lda, NULL); 493 #endif 494 m = mtxlock2mtx(c); 495 if (__predict_false(v == MTX_UNOWNED)) 496 v = MTX_READ_VALUE(m); 497 498 if (__predict_false(lv_mtx_owner(v) == td)) { 499 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 500 (opts & MTX_RECURSE) != 0, 501 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 502 m->lock_object.lo_name, file, line)); 503 #if LOCK_DEBUG > 0 504 opts &= ~MTX_RECURSE; 505 #endif 506 m->mtx_recurse++; 507 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 508 if (LOCK_LOG_TEST(&m->lock_object, opts)) 509 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 510 return; 511 } 512 #if LOCK_DEBUG > 0 513 opts &= ~MTX_RECURSE; 514 #endif 515 516 #ifdef HWPMC_HOOKS 517 PMC_SOFT_CALL( , , lock, failed); 518 #endif 519 lock_profile_obtain_lock_failed(&m->lock_object, 520 &contested, &waittime); 521 if (LOCK_LOG_TEST(&m->lock_object, opts)) 522 CTR4(KTR_LOCK, 523 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 524 m->lock_object.lo_name, (void *)m->mtx_lock, file, line); 525 #ifdef LOCK_PROFILING 526 doing_lockprof = 1; 527 #elif defined(KDTRACE_HOOKS) 528 doing_lockprof = lockstat_enabled; 529 if (__predict_false(doing_lockprof)) 530 all_time -= lockstat_nsecs(&m->lock_object); 531 #endif 532 533 for (;;) { 534 if (v == MTX_UNOWNED) { 535 if (_mtx_obtain_lock_fetch(m, &v, tid)) 536 break; 537 continue; 538 } 539 #ifdef KDTRACE_HOOKS 540 lda.spin_cnt++; 541 #endif 542 #ifdef ADAPTIVE_MUTEXES 543 /* 544 * If the owner is running on another CPU, spin until the 545 * owner stops running or the state of the lock changes. 546 */ 547 owner = lv_mtx_owner(v); 548 if (TD_IS_RUNNING(owner)) { 549 if (LOCK_LOG_TEST(&m->lock_object, 0)) 550 CTR3(KTR_LOCK, 551 "%s: spinning on %p held by %p", 552 __func__, m, owner); 553 KTR_STATE1(KTR_SCHED, "thread", 554 sched_tdname((struct thread *)tid), 555 "spinning", "lockname:\"%s\"", 556 m->lock_object.lo_name); 557 do { 558 lock_delay(&lda); 559 v = MTX_READ_VALUE(m); 560 owner = lv_mtx_owner(v); 561 } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner)); 562 KTR_STATE0(KTR_SCHED, "thread", 563 sched_tdname((struct thread *)tid), 564 "running"); 565 continue; 566 } 567 #endif 568 569 ts = turnstile_trywait(&m->lock_object); 570 v = MTX_READ_VALUE(m); 571 572 /* 573 * Check if the lock has been released while spinning for 574 * the turnstile chain lock. 575 */ 576 if (v == MTX_UNOWNED) { 577 turnstile_cancel(ts); 578 continue; 579 } 580 581 #ifdef ADAPTIVE_MUTEXES 582 /* 583 * The current lock owner might have started executing 584 * on another CPU (or the lock could have changed 585 * owners) while we were waiting on the turnstile 586 * chain lock. If so, drop the turnstile lock and try 587 * again. 588 */ 589 owner = lv_mtx_owner(v); 590 if (TD_IS_RUNNING(owner)) { 591 turnstile_cancel(ts); 592 continue; 593 } 594 #endif 595 596 /* 597 * If the mutex isn't already contested and a failure occurs 598 * setting the contested bit, the mutex was either released 599 * or the state of the MTX_RECURSED bit changed. 600 */ 601 if ((v & MTX_CONTESTED) == 0 && 602 !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 603 turnstile_cancel(ts); 604 v = MTX_READ_VALUE(m); 605 continue; 606 } 607 608 /* 609 * We definitely must sleep for this lock. 610 */ 611 mtx_assert(m, MA_NOTOWNED); 612 613 #ifdef KTR 614 if (!cont_logged) { 615 CTR6(KTR_CONTENTION, 616 "contention: %p at %s:%d wants %s, taken by %s:%d", 617 (void *)tid, file, line, m->lock_object.lo_name, 618 WITNESS_FILE(&m->lock_object), 619 WITNESS_LINE(&m->lock_object)); 620 cont_logged = 1; 621 } 622 #endif 623 624 /* 625 * Block on the turnstile. 626 */ 627 #ifdef KDTRACE_HOOKS 628 sleep_time -= lockstat_nsecs(&m->lock_object); 629 #endif 630 #ifndef ADAPTIVE_MUTEXES 631 owner = mtx_owner(m); 632 #endif 633 MPASS(owner == mtx_owner(m)); 634 turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE); 635 #ifdef KDTRACE_HOOKS 636 sleep_time += lockstat_nsecs(&m->lock_object); 637 sleep_cnt++; 638 #endif 639 v = MTX_READ_VALUE(m); 640 } 641 #ifdef KTR 642 if (cont_logged) { 643 CTR4(KTR_CONTENTION, 644 "contention end: %s acquired by %p at %s:%d", 645 m->lock_object.lo_name, (void *)tid, file, line); 646 } 647 #endif 648 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 649 if (__predict_true(!doing_lockprof)) 650 return; 651 #endif 652 #ifdef KDTRACE_HOOKS 653 all_time += lockstat_nsecs(&m->lock_object); 654 #endif 655 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested, 656 waittime, file, line); 657 #ifdef KDTRACE_HOOKS 658 if (sleep_time) 659 LOCKSTAT_RECORD1(adaptive__block, m, sleep_time); 660 661 /* 662 * Only record the loops spinning and not sleeping. 663 */ 664 if (lda.spin_cnt > sleep_cnt) 665 LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time); 666 #endif 667 } 668 669 static void 670 _mtx_lock_spin_failed(struct mtx *m) 671 { 672 struct thread *td; 673 674 td = mtx_owner(m); 675 676 /* If the mutex is unlocked, try again. */ 677 if (td == NULL) 678 return; 679 680 printf( "spin lock %p (%s) held by %p (tid %d) too long\n", 681 m, m->lock_object.lo_name, td, td->td_tid); 682 #ifdef WITNESS 683 witness_display_spinlock(&m->lock_object, td, printf); 684 #endif 685 panic("spin lock held too long"); 686 } 687 688 #ifdef SMP 689 /* 690 * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock. 691 * 692 * This is only called if we need to actually spin for the lock. Recursion 693 * is handled inline. 694 */ 695 #if LOCK_DEBUG > 0 696 void 697 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts, 698 const char *file, int line) 699 #else 700 void 701 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v) 702 #endif 703 { 704 struct mtx *m; 705 struct lock_delay_arg lda; 706 uintptr_t tid; 707 #ifdef LOCK_PROFILING 708 int contested = 0; 709 uint64_t waittime = 0; 710 #endif 711 #ifdef KDTRACE_HOOKS 712 int64_t spin_time = 0; 713 #endif 714 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 715 int doing_lockprof; 716 #endif 717 718 tid = (uintptr_t)curthread; 719 m = mtxlock2mtx(c); 720 721 if (__predict_false(v == MTX_UNOWNED)) 722 v = MTX_READ_VALUE(m); 723 724 if (__predict_false(v == tid)) { 725 m->mtx_recurse++; 726 return; 727 } 728 729 if (SCHEDULER_STOPPED()) 730 return; 731 732 lock_delay_arg_init(&lda, &mtx_spin_delay); 733 734 if (LOCK_LOG_TEST(&m->lock_object, opts)) 735 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 736 KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 737 "spinning", "lockname:\"%s\"", m->lock_object.lo_name); 738 739 #ifdef HWPMC_HOOKS 740 PMC_SOFT_CALL( , , lock, failed); 741 #endif 742 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 743 #ifdef LOCK_PROFILING 744 doing_lockprof = 1; 745 #elif defined(KDTRACE_HOOKS) 746 doing_lockprof = lockstat_enabled; 747 if (__predict_false(doing_lockprof)) 748 spin_time -= lockstat_nsecs(&m->lock_object); 749 #endif 750 for (;;) { 751 if (v == MTX_UNOWNED) { 752 if (_mtx_obtain_lock_fetch(m, &v, tid)) 753 break; 754 continue; 755 } 756 /* Give interrupts a chance while we spin. */ 757 spinlock_exit(); 758 do { 759 if (lda.spin_cnt < 10000000) { 760 lock_delay(&lda); 761 } else { 762 lda.spin_cnt++; 763 if (lda.spin_cnt < 60000000 || kdb_active || 764 panicstr != NULL) 765 DELAY(1); 766 else 767 _mtx_lock_spin_failed(m); 768 cpu_spinwait(); 769 } 770 v = MTX_READ_VALUE(m); 771 } while (v != MTX_UNOWNED); 772 spinlock_enter(); 773 } 774 775 if (LOCK_LOG_TEST(&m->lock_object, opts)) 776 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 777 KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 778 "running"); 779 780 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 781 if (__predict_true(!doing_lockprof)) 782 return; 783 #endif 784 #ifdef KDTRACE_HOOKS 785 spin_time += lockstat_nsecs(&m->lock_object); 786 #endif 787 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 788 contested, waittime, file, line); 789 #ifdef KDTRACE_HOOKS 790 if (lda.spin_cnt != 0) 791 LOCKSTAT_RECORD1(spin__spin, m, spin_time); 792 #endif 793 } 794 #endif /* SMP */ 795 796 #ifdef INVARIANTS 797 static void 798 thread_lock_validate(struct mtx *m, int opts, const char *file, int line) 799 { 800 801 KASSERT(m->mtx_lock != MTX_DESTROYED, 802 ("thread_lock() of destroyed mutex @ %s:%d", file, line)); 803 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 804 ("thread_lock() of sleep mutex %s @ %s:%d", 805 m->lock_object.lo_name, file, line)); 806 if (mtx_owned(m)) 807 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 808 ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n", 809 m->lock_object.lo_name, file, line)); 810 WITNESS_CHECKORDER(&m->lock_object, 811 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 812 } 813 #else 814 #define thread_lock_validate(m, opts, file, line) do { } while (0) 815 #endif 816 817 #ifndef LOCK_PROFILING 818 #if LOCK_DEBUG > 0 819 void 820 _thread_lock(struct thread *td, int opts, const char *file, int line) 821 #else 822 void 823 _thread_lock(struct thread *td) 824 #endif 825 { 826 struct mtx *m; 827 uintptr_t tid, v; 828 829 tid = (uintptr_t)curthread; 830 831 spinlock_enter(); 832 m = td->td_lock; 833 thread_lock_validate(m, 0, file, line); 834 v = MTX_READ_VALUE(m); 835 if (__predict_true(v == MTX_UNOWNED)) { 836 if (__predict_false(!_mtx_obtain_lock(m, tid))) 837 goto slowpath_unlocked; 838 } else if (v == tid) { 839 m->mtx_recurse++; 840 } else 841 goto slowpath_unlocked; 842 if (__predict_true(m == td->td_lock)) { 843 WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line); 844 return; 845 } 846 if (m->mtx_recurse != 0) 847 m->mtx_recurse--; 848 else 849 _mtx_release_lock_quick(m); 850 slowpath_unlocked: 851 spinlock_exit(); 852 thread_lock_flags_(td, 0, 0, 0); 853 } 854 #endif 855 856 void 857 thread_lock_flags_(struct thread *td, int opts, const char *file, int line) 858 { 859 struct mtx *m; 860 uintptr_t tid, v; 861 struct lock_delay_arg lda; 862 #ifdef LOCK_PROFILING 863 int contested = 0; 864 uint64_t waittime = 0; 865 #endif 866 #ifdef KDTRACE_HOOKS 867 int64_t spin_time = 0; 868 #endif 869 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 870 int doing_lockprof = 1; 871 #endif 872 873 tid = (uintptr_t)curthread; 874 875 if (SCHEDULER_STOPPED()) { 876 /* 877 * Ensure that spinlock sections are balanced even when the 878 * scheduler is stopped, since we may otherwise inadvertently 879 * re-enable interrupts while dumping core. 880 */ 881 spinlock_enter(); 882 return; 883 } 884 885 lock_delay_arg_init(&lda, &mtx_spin_delay); 886 887 #ifdef LOCK_PROFILING 888 doing_lockprof = 1; 889 #elif defined(KDTRACE_HOOKS) 890 doing_lockprof = lockstat_enabled; 891 if (__predict_false(doing_lockprof)) 892 spin_time -= lockstat_nsecs(&td->td_lock->lock_object); 893 #endif 894 for (;;) { 895 retry: 896 v = MTX_UNOWNED; 897 spinlock_enter(); 898 m = td->td_lock; 899 thread_lock_validate(m, opts, file, line); 900 for (;;) { 901 if (_mtx_obtain_lock_fetch(m, &v, tid)) 902 break; 903 if (v == MTX_UNOWNED) 904 continue; 905 if (v == tid) { 906 m->mtx_recurse++; 907 break; 908 } 909 #ifdef HWPMC_HOOKS 910 PMC_SOFT_CALL( , , lock, failed); 911 #endif 912 lock_profile_obtain_lock_failed(&m->lock_object, 913 &contested, &waittime); 914 /* Give interrupts a chance while we spin. */ 915 spinlock_exit(); 916 do { 917 if (lda.spin_cnt < 10000000) { 918 lock_delay(&lda); 919 } else { 920 lda.spin_cnt++; 921 if (lda.spin_cnt < 60000000 || 922 kdb_active || panicstr != NULL) 923 DELAY(1); 924 else 925 _mtx_lock_spin_failed(m); 926 cpu_spinwait(); 927 } 928 if (m != td->td_lock) 929 goto retry; 930 v = MTX_READ_VALUE(m); 931 } while (v != MTX_UNOWNED); 932 spinlock_enter(); 933 } 934 if (m == td->td_lock) 935 break; 936 __mtx_unlock_spin(m); /* does spinlock_exit() */ 937 } 938 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 939 line); 940 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 941 942 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 943 if (__predict_true(!doing_lockprof)) 944 return; 945 #endif 946 #ifdef KDTRACE_HOOKS 947 spin_time += lockstat_nsecs(&m->lock_object); 948 #endif 949 if (m->mtx_recurse == 0) 950 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 951 contested, waittime, file, line); 952 #ifdef KDTRACE_HOOKS 953 if (lda.spin_cnt != 0) 954 LOCKSTAT_RECORD1(thread__spin, m, spin_time); 955 #endif 956 } 957 958 struct mtx * 959 thread_lock_block(struct thread *td) 960 { 961 struct mtx *lock; 962 963 THREAD_LOCK_ASSERT(td, MA_OWNED); 964 lock = td->td_lock; 965 td->td_lock = &blocked_lock; 966 mtx_unlock_spin(lock); 967 968 return (lock); 969 } 970 971 void 972 thread_lock_unblock(struct thread *td, struct mtx *new) 973 { 974 mtx_assert(new, MA_OWNED); 975 MPASS(td->td_lock == &blocked_lock); 976 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new); 977 } 978 979 void 980 thread_lock_set(struct thread *td, struct mtx *new) 981 { 982 struct mtx *lock; 983 984 mtx_assert(new, MA_OWNED); 985 THREAD_LOCK_ASSERT(td, MA_OWNED); 986 lock = td->td_lock; 987 td->td_lock = new; 988 mtx_unlock_spin(lock); 989 } 990 991 /* 992 * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 993 * 994 * We are only called here if the lock is recursed, contested (i.e. we 995 * need to wake up a blocked thread) or lockstat probe is active. 996 */ 997 #if LOCK_DEBUG > 0 998 void 999 __mtx_unlock_sleep(volatile uintptr_t *c, int opts, const char *file, int line) 1000 #else 1001 void 1002 __mtx_unlock_sleep(volatile uintptr_t *c) 1003 #endif 1004 { 1005 struct mtx *m; 1006 struct turnstile *ts; 1007 uintptr_t tid, v; 1008 1009 if (SCHEDULER_STOPPED()) 1010 return; 1011 1012 tid = (uintptr_t)curthread; 1013 m = mtxlock2mtx(c); 1014 v = MTX_READ_VALUE(m); 1015 1016 if (v & MTX_RECURSED) { 1017 if (--(m->mtx_recurse) == 0) 1018 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 1019 if (LOCK_LOG_TEST(&m->lock_object, opts)) 1020 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 1021 return; 1022 } 1023 1024 LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m); 1025 if (v == tid && _mtx_release_lock(m, tid)) 1026 return; 1027 1028 /* 1029 * We have to lock the chain before the turnstile so this turnstile 1030 * can be removed from the hash list if it is empty. 1031 */ 1032 turnstile_chain_lock(&m->lock_object); 1033 _mtx_release_lock_quick(m); 1034 ts = turnstile_lookup(&m->lock_object); 1035 MPASS(ts != NULL); 1036 if (LOCK_LOG_TEST(&m->lock_object, opts)) 1037 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 1038 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 1039 1040 /* 1041 * This turnstile is now no longer associated with the mutex. We can 1042 * unlock the chain lock so a new turnstile may take it's place. 1043 */ 1044 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 1045 turnstile_chain_unlock(&m->lock_object); 1046 } 1047 1048 /* 1049 * All the unlocking of MTX_SPIN locks is done inline. 1050 * See the __mtx_unlock_spin() macro for the details. 1051 */ 1052 1053 /* 1054 * The backing function for the INVARIANTS-enabled mtx_assert() 1055 */ 1056 #ifdef INVARIANT_SUPPORT 1057 void 1058 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line) 1059 { 1060 const struct mtx *m; 1061 1062 if (panicstr != NULL || dumping || SCHEDULER_STOPPED()) 1063 return; 1064 1065 m = mtxlock2mtx(c); 1066 1067 switch (what) { 1068 case MA_OWNED: 1069 case MA_OWNED | MA_RECURSED: 1070 case MA_OWNED | MA_NOTRECURSED: 1071 if (!mtx_owned(m)) 1072 panic("mutex %s not owned at %s:%d", 1073 m->lock_object.lo_name, file, line); 1074 if (mtx_recursed(m)) { 1075 if ((what & MA_NOTRECURSED) != 0) 1076 panic("mutex %s recursed at %s:%d", 1077 m->lock_object.lo_name, file, line); 1078 } else if ((what & MA_RECURSED) != 0) { 1079 panic("mutex %s unrecursed at %s:%d", 1080 m->lock_object.lo_name, file, line); 1081 } 1082 break; 1083 case MA_NOTOWNED: 1084 if (mtx_owned(m)) 1085 panic("mutex %s owned at %s:%d", 1086 m->lock_object.lo_name, file, line); 1087 break; 1088 default: 1089 panic("unknown mtx_assert at %s:%d", file, line); 1090 } 1091 } 1092 #endif 1093 1094 /* 1095 * General init routine used by the MTX_SYSINIT() macro. 1096 */ 1097 void 1098 mtx_sysinit(void *arg) 1099 { 1100 struct mtx_args *margs = arg; 1101 1102 mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL, 1103 margs->ma_opts); 1104 } 1105 1106 /* 1107 * Mutex initialization routine; initialize lock `m' of type contained in 1108 * `opts' with options contained in `opts' and name `name.' The optional 1109 * lock type `type' is used as a general lock category name for use with 1110 * witness. 1111 */ 1112 void 1113 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts) 1114 { 1115 struct mtx *m; 1116 struct lock_class *class; 1117 int flags; 1118 1119 m = mtxlock2mtx(c); 1120 1121 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 1122 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0); 1123 ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock, 1124 ("%s: mtx_lock not aligned for %s: %p", __func__, name, 1125 &m->mtx_lock)); 1126 1127 /* Determine lock class and lock flags. */ 1128 if (opts & MTX_SPIN) 1129 class = &lock_class_mtx_spin; 1130 else 1131 class = &lock_class_mtx_sleep; 1132 flags = 0; 1133 if (opts & MTX_QUIET) 1134 flags |= LO_QUIET; 1135 if (opts & MTX_RECURSE) 1136 flags |= LO_RECURSABLE; 1137 if ((opts & MTX_NOWITNESS) == 0) 1138 flags |= LO_WITNESS; 1139 if (opts & MTX_DUPOK) 1140 flags |= LO_DUPOK; 1141 if (opts & MTX_NOPROFILE) 1142 flags |= LO_NOPROFILE; 1143 if (opts & MTX_NEW) 1144 flags |= LO_NEW; 1145 1146 /* Initialize mutex. */ 1147 lock_init(&m->lock_object, class, name, type, flags); 1148 1149 m->mtx_lock = MTX_UNOWNED; 1150 m->mtx_recurse = 0; 1151 } 1152 1153 /* 1154 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 1155 * passed in as a flag here because if the corresponding mtx_init() was 1156 * called with MTX_QUIET set, then it will already be set in the mutex's 1157 * flags. 1158 */ 1159 void 1160 _mtx_destroy(volatile uintptr_t *c) 1161 { 1162 struct mtx *m; 1163 1164 m = mtxlock2mtx(c); 1165 1166 if (!mtx_owned(m)) 1167 MPASS(mtx_unowned(m)); 1168 else { 1169 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 1170 1171 /* Perform the non-mtx related part of mtx_unlock_spin(). */ 1172 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) 1173 spinlock_exit(); 1174 else 1175 TD_LOCKS_DEC(curthread); 1176 1177 lock_profile_release_lock(&m->lock_object); 1178 /* Tell witness this isn't locked to make it happy. */ 1179 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__, 1180 __LINE__); 1181 } 1182 1183 m->mtx_lock = MTX_DESTROYED; 1184 lock_destroy(&m->lock_object); 1185 } 1186 1187 /* 1188 * Intialize the mutex code and system mutexes. This is called from the MD 1189 * startup code prior to mi_startup(). The per-CPU data space needs to be 1190 * setup before this is called. 1191 */ 1192 void 1193 mutex_init(void) 1194 { 1195 1196 /* Setup turnstiles so that sleep mutexes work. */ 1197 init_turnstiles(); 1198 1199 /* 1200 * Initialize mutexes. 1201 */ 1202 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 1203 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN); 1204 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */ 1205 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 1206 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN); 1207 mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN); 1208 mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN); 1209 mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN); 1210 mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 1211 mtx_lock(&Giant); 1212 } 1213 1214 #ifdef DDB 1215 void 1216 db_show_mtx(const struct lock_object *lock) 1217 { 1218 struct thread *td; 1219 const struct mtx *m; 1220 1221 m = (const struct mtx *)lock; 1222 1223 db_printf(" flags: {"); 1224 if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 1225 db_printf("SPIN"); 1226 else 1227 db_printf("DEF"); 1228 if (m->lock_object.lo_flags & LO_RECURSABLE) 1229 db_printf(", RECURSE"); 1230 if (m->lock_object.lo_flags & LO_DUPOK) 1231 db_printf(", DUPOK"); 1232 db_printf("}\n"); 1233 db_printf(" state: {"); 1234 if (mtx_unowned(m)) 1235 db_printf("UNOWNED"); 1236 else if (mtx_destroyed(m)) 1237 db_printf("DESTROYED"); 1238 else { 1239 db_printf("OWNED"); 1240 if (m->mtx_lock & MTX_CONTESTED) 1241 db_printf(", CONTESTED"); 1242 if (m->mtx_lock & MTX_RECURSED) 1243 db_printf(", RECURSED"); 1244 } 1245 db_printf("}\n"); 1246 if (!mtx_unowned(m) && !mtx_destroyed(m)) { 1247 td = mtx_owner(m); 1248 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 1249 td->td_tid, td->td_proc->p_pid, td->td_name); 1250 if (mtx_recursed(m)) 1251 db_printf(" recursed: %d\n", m->mtx_recurse); 1252 } 1253 } 1254 #endif 1255