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_global.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/sysctl.h> 59 #include <sys/turnstile.h> 60 #include <sys/vmmeter.h> 61 #include <sys/lock_profile.h> 62 63 #include <machine/atomic.h> 64 #include <machine/bus.h> 65 #include <machine/cpu.h> 66 67 #include <ddb/ddb.h> 68 69 #include <fs/devfs/devfs_int.h> 70 71 #include <vm/vm.h> 72 #include <vm/vm_extern.h> 73 74 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 75 #define ADAPTIVE_MUTEXES 76 #endif 77 78 /* 79 * Internal utility macros. 80 */ 81 #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 82 83 #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED) 84 85 #define mtx_owner(m) ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK)) 86 87 static void assert_mtx(struct lock_object *lock, int what); 88 #ifdef DDB 89 static void db_show_mtx(struct lock_object *lock); 90 #endif 91 static void lock_mtx(struct lock_object *lock, int how); 92 static void lock_spin(struct lock_object *lock, int how); 93 static int unlock_mtx(struct lock_object *lock); 94 static int unlock_spin(struct lock_object *lock); 95 96 /* 97 * Lock classes for sleep and spin mutexes. 98 */ 99 struct lock_class lock_class_mtx_sleep = { 100 .lc_name = "sleep mutex", 101 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, 102 .lc_assert = assert_mtx, 103 #ifdef DDB 104 .lc_ddb_show = db_show_mtx, 105 #endif 106 .lc_lock = lock_mtx, 107 .lc_unlock = unlock_mtx, 108 }; 109 struct lock_class lock_class_mtx_spin = { 110 .lc_name = "spin mutex", 111 .lc_flags = LC_SPINLOCK | LC_RECURSABLE, 112 .lc_assert = assert_mtx, 113 #ifdef DDB 114 .lc_ddb_show = db_show_mtx, 115 #endif 116 .lc_lock = lock_spin, 117 .lc_unlock = unlock_spin, 118 }; 119 120 /* 121 * System-wide mutexes 122 */ 123 struct mtx blocked_lock; 124 struct mtx Giant; 125 126 #ifdef LOCK_PROFILING 127 static inline void lock_profile_init(void) 128 { 129 int i; 130 /* Initialize the mutex profiling locks */ 131 for (i = 0; i < LPROF_LOCK_SIZE; i++) { 132 mtx_init(&lprof_locks[i], "mprof lock", 133 NULL, MTX_SPIN|MTX_QUIET|MTX_NOPROFILE); 134 } 135 } 136 #else 137 static inline void lock_profile_init(void) {;} 138 #endif 139 140 void 141 assert_mtx(struct lock_object *lock, int what) 142 { 143 144 mtx_assert((struct mtx *)lock, what); 145 } 146 147 void 148 lock_mtx(struct lock_object *lock, int how) 149 { 150 151 mtx_lock((struct mtx *)lock); 152 } 153 154 void 155 lock_spin(struct lock_object *lock, int how) 156 { 157 158 panic("spin locks can only use msleep_spin"); 159 } 160 161 int 162 unlock_mtx(struct lock_object *lock) 163 { 164 struct mtx *m; 165 166 m = (struct mtx *)lock; 167 mtx_assert(m, MA_OWNED | MA_NOTRECURSED); 168 mtx_unlock(m); 169 return (0); 170 } 171 172 int 173 unlock_spin(struct lock_object *lock) 174 { 175 176 panic("spin locks can only use msleep_spin"); 177 } 178 179 /* 180 * Function versions of the inlined __mtx_* macros. These are used by 181 * modules and can also be called from assembly language if needed. 182 */ 183 void 184 _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 185 { 186 187 MPASS(curthread != NULL); 188 KASSERT(m->mtx_lock != MTX_DESTROYED, 189 ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); 190 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 191 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 192 file, line)); 193 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 194 file, line); 195 196 _get_sleep_lock(m, curthread, opts, file, line); 197 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 198 line); 199 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 200 curthread->td_locks++; 201 } 202 203 void 204 _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 205 { 206 MPASS(curthread != NULL); 207 KASSERT(m->mtx_lock != MTX_DESTROYED, 208 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); 209 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 210 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 211 file, line)); 212 curthread->td_locks--; 213 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 214 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 215 line); 216 mtx_assert(m, MA_OWNED); 217 218 if (m->mtx_recurse == 0) 219 lock_profile_release_lock(&m->lock_object); 220 _rel_sleep_lock(m, curthread, opts, file, line); 221 } 222 223 void 224 _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 225 { 226 227 MPASS(curthread != NULL); 228 KASSERT(m->mtx_lock != MTX_DESTROYED, 229 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); 230 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 231 ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 232 m->lock_object.lo_name, file, line)); 233 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 234 file, line); 235 _get_spin_lock(m, curthread, opts, file, line); 236 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 237 line); 238 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 239 } 240 241 void 242 _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 243 { 244 245 MPASS(curthread != NULL); 246 KASSERT(m->mtx_lock != MTX_DESTROYED, 247 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); 248 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 249 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 250 m->lock_object.lo_name, file, line)); 251 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 252 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 253 line); 254 mtx_assert(m, MA_OWNED); 255 256 _rel_spin_lock(m); 257 } 258 259 /* 260 * The important part of mtx_trylock{,_flags}() 261 * Tries to acquire lock `m.' If this function is called on a mutex that 262 * is already owned, it will recursively acquire the lock. 263 */ 264 int 265 _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 266 { 267 int rval, contested = 0; 268 uint64_t waittime = 0; 269 270 MPASS(curthread != NULL); 271 KASSERT(m->mtx_lock != MTX_DESTROYED, 272 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); 273 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 274 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 275 file, line)); 276 277 if (mtx_owned(m) && (m->lock_object.lo_flags & LO_RECURSABLE) != 0) { 278 m->mtx_recurse++; 279 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 280 rval = 1; 281 } else 282 rval = _obtain_lock(m, (uintptr_t)curthread); 283 284 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line); 285 if (rval) { 286 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 287 file, line); 288 curthread->td_locks++; 289 if (m->mtx_recurse == 0) 290 lock_profile_obtain_lock_success(&m->lock_object, contested, 291 waittime, file, line); 292 293 } 294 295 return (rval); 296 } 297 298 /* 299 * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 300 * 301 * We call this if the lock is either contested (i.e. we need to go to 302 * sleep waiting for it), or if we need to recurse on it. 303 */ 304 void 305 _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file, 306 int line) 307 { 308 struct turnstile *ts; 309 #ifdef ADAPTIVE_MUTEXES 310 volatile struct thread *owner; 311 #endif 312 #ifdef KTR 313 int cont_logged = 0; 314 #endif 315 int contested = 0; 316 uint64_t waittime = 0; 317 uintptr_t v; 318 319 if (mtx_owned(m)) { 320 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 321 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 322 m->lock_object.lo_name, file, line)); 323 m->mtx_recurse++; 324 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 325 if (LOCK_LOG_TEST(&m->lock_object, opts)) 326 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 327 return; 328 } 329 330 lock_profile_obtain_lock_failed(&m->lock_object, 331 &contested, &waittime); 332 if (LOCK_LOG_TEST(&m->lock_object, opts)) 333 CTR4(KTR_LOCK, 334 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 335 m->lock_object.lo_name, (void *)m->mtx_lock, file, line); 336 337 while (!_obtain_lock(m, tid)) { 338 #ifdef ADAPTIVE_MUTEXES 339 /* 340 * If the owner is running on another CPU, spin until the 341 * owner stops running or the state of the lock changes. 342 */ 343 v = m->mtx_lock; 344 if (v != MTX_UNOWNED) { 345 owner = (struct thread *)(v & ~MTX_FLAGMASK); 346 #ifdef ADAPTIVE_GIANT 347 if (TD_IS_RUNNING(owner)) { 348 #else 349 if (m != &Giant && TD_IS_RUNNING(owner)) { 350 #endif 351 if (LOCK_LOG_TEST(&m->lock_object, 0)) 352 CTR3(KTR_LOCK, 353 "%s: spinning on %p held by %p", 354 __func__, m, owner); 355 while (mtx_owner(m) == owner && 356 TD_IS_RUNNING(owner)) 357 cpu_spinwait(); 358 continue; 359 } 360 } 361 #endif 362 363 ts = turnstile_trywait(&m->lock_object); 364 v = m->mtx_lock; 365 366 /* 367 * Check if the lock has been released while spinning for 368 * the turnstile chain lock. 369 */ 370 if (v == MTX_UNOWNED) { 371 turnstile_cancel(ts); 372 cpu_spinwait(); 373 continue; 374 } 375 376 MPASS(v != MTX_CONTESTED); 377 378 #ifdef ADAPTIVE_MUTEXES 379 /* 380 * If the current owner of the lock is executing on another 381 * CPU quit the hard path and try to spin. 382 */ 383 owner = (struct thread *)(v & ~MTX_FLAGMASK); 384 #ifdef ADAPTIVE_GIANT 385 if (TD_IS_RUNNING(owner)) { 386 #else 387 if (m != &Giant && TD_IS_RUNNING(owner)) { 388 #endif 389 turnstile_cancel(ts); 390 cpu_spinwait(); 391 continue; 392 } 393 #endif 394 395 /* 396 * If the mutex isn't already contested and a failure occurs 397 * setting the contested bit, the mutex was either released 398 * or the state of the MTX_RECURSED bit changed. 399 */ 400 if ((v & MTX_CONTESTED) == 0 && 401 !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 402 turnstile_cancel(ts); 403 cpu_spinwait(); 404 continue; 405 } 406 407 /* 408 * We definitely must sleep for this lock. 409 */ 410 mtx_assert(m, MA_NOTOWNED); 411 412 #ifdef KTR 413 if (!cont_logged) { 414 CTR6(KTR_CONTENTION, 415 "contention: %p at %s:%d wants %s, taken by %s:%d", 416 (void *)tid, file, line, m->lock_object.lo_name, 417 WITNESS_FILE(&m->lock_object), 418 WITNESS_LINE(&m->lock_object)); 419 cont_logged = 1; 420 } 421 #endif 422 423 /* 424 * Block on the turnstile. 425 */ 426 turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE); 427 } 428 #ifdef KTR 429 if (cont_logged) { 430 CTR4(KTR_CONTENTION, 431 "contention end: %s acquired by %p at %s:%d", 432 m->lock_object.lo_name, (void *)tid, file, line); 433 } 434 #endif 435 lock_profile_obtain_lock_success(&m->lock_object, contested, 436 waittime, (file), (line)); 437 } 438 439 static void 440 _mtx_lock_spin_failed(struct mtx *m) 441 { 442 struct thread *td; 443 444 td = mtx_owner(m); 445 446 /* If the mutex is unlocked, try again. */ 447 if (td == NULL) 448 return; 449 450 printf( "spin lock %p (%s) held by %p (tid %d) too long\n", 451 m, m->lock_object.lo_name, td, td->td_tid); 452 #ifdef WITNESS 453 witness_display_spinlock(&m->lock_object, td); 454 #endif 455 panic("spin lock held too long"); 456 } 457 458 #ifdef SMP 459 /* 460 * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 461 * 462 * This is only called if we need to actually spin for the lock. Recursion 463 * is handled inline. 464 */ 465 void 466 _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file, 467 int line) 468 { 469 int i = 0, contested = 0; 470 uint64_t waittime = 0; 471 472 if (LOCK_LOG_TEST(&m->lock_object, opts)) 473 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 474 475 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 476 while (!_obtain_lock(m, tid)) { 477 478 /* Give interrupts a chance while we spin. */ 479 spinlock_exit(); 480 while (m->mtx_lock != MTX_UNOWNED) { 481 if (i++ < 10000000) { 482 cpu_spinwait(); 483 continue; 484 } 485 if (i < 60000000 || kdb_active || panicstr != NULL) 486 DELAY(1); 487 else 488 _mtx_lock_spin_failed(m); 489 cpu_spinwait(); 490 } 491 spinlock_enter(); 492 } 493 494 if (LOCK_LOG_TEST(&m->lock_object, opts)) 495 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 496 497 lock_profile_obtain_lock_success(&m->lock_object, contested, 498 waittime, (file), (line)); 499 } 500 #endif /* SMP */ 501 502 void 503 _thread_lock_flags(struct thread *td, int opts, const char *file, int line) 504 { 505 struct mtx *m; 506 uintptr_t tid; 507 int i, contested; 508 uint64_t waittime; 509 510 511 contested = i = 0; 512 waittime = 0; 513 tid = (uintptr_t)curthread; 514 for (;;) { 515 retry: 516 spinlock_enter(); 517 m = td->td_lock; 518 WITNESS_CHECKORDER(&m->lock_object, 519 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line); 520 while (!_obtain_lock(m, tid)) { 521 if (m->mtx_lock == tid) { 522 m->mtx_recurse++; 523 break; 524 } 525 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 526 /* Give interrupts a chance while we spin. */ 527 spinlock_exit(); 528 while (m->mtx_lock != MTX_UNOWNED) { 529 if (i++ < 10000000) 530 cpu_spinwait(); 531 else if (i < 60000000 || 532 kdb_active || panicstr != NULL) 533 DELAY(1); 534 else 535 _mtx_lock_spin_failed(m); 536 cpu_spinwait(); 537 if (m != td->td_lock) 538 goto retry; 539 } 540 spinlock_enter(); 541 } 542 if (m == td->td_lock) 543 break; 544 _rel_spin_lock(m); /* does spinlock_exit() */ 545 } 546 lock_profile_obtain_lock_success(&m->lock_object, contested, 547 waittime, (file), (line)); 548 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 549 } 550 551 struct mtx * 552 thread_lock_block(struct thread *td) 553 { 554 struct mtx *lock; 555 556 spinlock_enter(); 557 THREAD_LOCK_ASSERT(td, MA_OWNED); 558 lock = td->td_lock; 559 td->td_lock = &blocked_lock; 560 mtx_unlock_spin(lock); 561 562 return (lock); 563 } 564 565 void 566 thread_lock_unblock(struct thread *td, struct mtx *new) 567 { 568 mtx_assert(new, MA_OWNED); 569 MPASS(td->td_lock == &blocked_lock); 570 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new); 571 spinlock_exit(); 572 } 573 574 void 575 thread_lock_set(struct thread *td, struct mtx *new) 576 { 577 struct mtx *lock; 578 579 mtx_assert(new, MA_OWNED); 580 THREAD_LOCK_ASSERT(td, MA_OWNED); 581 lock = td->td_lock; 582 td->td_lock = new; 583 mtx_unlock_spin(lock); 584 } 585 586 /* 587 * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 588 * 589 * We are only called here if the lock is recursed or contested (i.e. we 590 * need to wake up a blocked thread). 591 */ 592 void 593 _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 594 { 595 struct turnstile *ts; 596 597 if (mtx_recursed(m)) { 598 if (--(m->mtx_recurse) == 0) 599 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 600 if (LOCK_LOG_TEST(&m->lock_object, opts)) 601 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 602 return; 603 } 604 605 /* 606 * We have to lock the chain before the turnstile so this turnstile 607 * can be removed from the hash list if it is empty. 608 */ 609 turnstile_chain_lock(&m->lock_object); 610 ts = turnstile_lookup(&m->lock_object); 611 if (LOCK_LOG_TEST(&m->lock_object, opts)) 612 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 613 614 MPASS(ts != NULL); 615 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 616 _release_lock_quick(m); 617 /* 618 * This turnstile is now no longer associated with the mutex. We can 619 * unlock the chain lock so a new turnstile may take it's place. 620 */ 621 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 622 turnstile_chain_unlock(&m->lock_object); 623 } 624 625 /* 626 * All the unlocking of MTX_SPIN locks is done inline. 627 * See the _rel_spin_lock() macro for the details. 628 */ 629 630 /* 631 * The backing function for the INVARIANTS-enabled mtx_assert() 632 */ 633 #ifdef INVARIANT_SUPPORT 634 void 635 _mtx_assert(struct mtx *m, int what, const char *file, int line) 636 { 637 638 if (panicstr != NULL || dumping) 639 return; 640 switch (what) { 641 case MA_OWNED: 642 case MA_OWNED | MA_RECURSED: 643 case MA_OWNED | MA_NOTRECURSED: 644 if (!mtx_owned(m)) 645 panic("mutex %s not owned at %s:%d", 646 m->lock_object.lo_name, file, line); 647 if (mtx_recursed(m)) { 648 if ((what & MA_NOTRECURSED) != 0) 649 panic("mutex %s recursed at %s:%d", 650 m->lock_object.lo_name, file, line); 651 } else if ((what & MA_RECURSED) != 0) { 652 panic("mutex %s unrecursed at %s:%d", 653 m->lock_object.lo_name, file, line); 654 } 655 break; 656 case MA_NOTOWNED: 657 if (mtx_owned(m)) 658 panic("mutex %s owned at %s:%d", 659 m->lock_object.lo_name, file, line); 660 break; 661 default: 662 panic("unknown mtx_assert at %s:%d", file, line); 663 } 664 } 665 #endif 666 667 /* 668 * The MUTEX_DEBUG-enabled mtx_validate() 669 * 670 * Most of these checks have been moved off into the LO_INITIALIZED flag 671 * maintained by the witness code. 672 */ 673 #ifdef MUTEX_DEBUG 674 675 void mtx_validate(struct mtx *); 676 677 void 678 mtx_validate(struct mtx *m) 679 { 680 681 /* 682 * XXX: When kernacc() does not require Giant we can reenable this check 683 */ 684 #ifdef notyet 685 /* 686 * Can't call kernacc() from early init386(), especially when 687 * initializing Giant mutex, because some stuff in kernacc() 688 * requires Giant itself. 689 */ 690 if (!cold) 691 if (!kernacc((caddr_t)m, sizeof(m), 692 VM_PROT_READ | VM_PROT_WRITE)) 693 panic("Can't read and write to mutex %p", m); 694 #endif 695 } 696 #endif 697 698 /* 699 * General init routine used by the MTX_SYSINIT() macro. 700 */ 701 void 702 mtx_sysinit(void *arg) 703 { 704 struct mtx_args *margs = arg; 705 706 mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 707 } 708 709 /* 710 * Mutex initialization routine; initialize lock `m' of type contained in 711 * `opts' with options contained in `opts' and name `name.' The optional 712 * lock type `type' is used as a general lock category name for use with 713 * witness. 714 */ 715 void 716 mtx_init(struct mtx *m, const char *name, const char *type, int opts) 717 { 718 struct lock_class *class; 719 int flags; 720 721 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 722 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0); 723 724 #ifdef MUTEX_DEBUG 725 /* Diagnostic and error correction */ 726 mtx_validate(m); 727 #endif 728 729 /* Determine lock class and lock flags. */ 730 if (opts & MTX_SPIN) 731 class = &lock_class_mtx_spin; 732 else 733 class = &lock_class_mtx_sleep; 734 flags = 0; 735 if (opts & MTX_QUIET) 736 flags |= LO_QUIET; 737 if (opts & MTX_RECURSE) 738 flags |= LO_RECURSABLE; 739 if ((opts & MTX_NOWITNESS) == 0) 740 flags |= LO_WITNESS; 741 if (opts & MTX_DUPOK) 742 flags |= LO_DUPOK; 743 if (opts & MTX_NOPROFILE) 744 flags |= LO_NOPROFILE; 745 746 /* Initialize mutex. */ 747 m->mtx_lock = MTX_UNOWNED; 748 m->mtx_recurse = 0; 749 750 lock_init(&m->lock_object, class, name, type, flags); 751 } 752 753 /* 754 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 755 * passed in as a flag here because if the corresponding mtx_init() was 756 * called with MTX_QUIET set, then it will already be set in the mutex's 757 * flags. 758 */ 759 void 760 mtx_destroy(struct mtx *m) 761 { 762 763 if (!mtx_owned(m)) 764 MPASS(mtx_unowned(m)); 765 else { 766 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 767 768 /* Perform the non-mtx related part of mtx_unlock_spin(). */ 769 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) 770 spinlock_exit(); 771 else 772 curthread->td_locks--; 773 774 /* Tell witness this isn't locked to make it happy. */ 775 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__, 776 __LINE__); 777 } 778 779 m->mtx_lock = MTX_DESTROYED; 780 lock_destroy(&m->lock_object); 781 } 782 783 /* 784 * Intialize the mutex code and system mutexes. This is called from the MD 785 * startup code prior to mi_startup(). The per-CPU data space needs to be 786 * setup before this is called. 787 */ 788 void 789 mutex_init(void) 790 { 791 792 /* Setup turnstiles so that sleep mutexes work. */ 793 init_turnstiles(); 794 795 /* 796 * Initialize mutexes. 797 */ 798 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 799 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN); 800 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */ 801 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 802 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 803 mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 804 mtx_lock(&Giant); 805 806 lock_profile_init(); 807 } 808 809 #ifdef DDB 810 void 811 db_show_mtx(struct lock_object *lock) 812 { 813 struct thread *td; 814 struct mtx *m; 815 816 m = (struct mtx *)lock; 817 818 db_printf(" flags: {"); 819 if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 820 db_printf("SPIN"); 821 else 822 db_printf("DEF"); 823 if (m->lock_object.lo_flags & LO_RECURSABLE) 824 db_printf(", RECURSE"); 825 if (m->lock_object.lo_flags & LO_DUPOK) 826 db_printf(", DUPOK"); 827 db_printf("}\n"); 828 db_printf(" state: {"); 829 if (mtx_unowned(m)) 830 db_printf("UNOWNED"); 831 else if (mtx_destroyed(m)) 832 db_printf("DESTROYED"); 833 else { 834 db_printf("OWNED"); 835 if (m->mtx_lock & MTX_CONTESTED) 836 db_printf(", CONTESTED"); 837 if (m->mtx_lock & MTX_RECURSED) 838 db_printf(", RECURSED"); 839 } 840 db_printf("}\n"); 841 if (!mtx_unowned(m) && !mtx_destroyed(m)) { 842 td = mtx_owner(m); 843 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 844 td->td_tid, td->td_proc->p_pid, td->td_name); 845 if (mtx_recursed(m)) 846 db_printf(" recursed: %d\n", m->mtx_recurse); 847 } 848 } 849 #endif 850