kern_mutex.c revision 173733
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: head/sys/kern/kern_mutex.c 173733 2007-11-18 14:43:53Z attilio $"); 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 87static void assert_mtx(struct lock_object *lock, int what); 88#ifdef DDB 89static void db_show_mtx(struct lock_object *lock); 90#endif 91static void lock_mtx(struct lock_object *lock, int how); 92static void lock_spin(struct lock_object *lock, int how); 93static int unlock_mtx(struct lock_object *lock); 94static int unlock_spin(struct lock_object *lock); 95 96/* 97 * Lock classes for sleep and spin mutexes. 98 */ 99struct 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}; 109struct 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 */ 123struct mtx blocked_lock; 124struct mtx Giant; 125 126#ifdef LOCK_PROFILING 127static 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 137static inline void lock_profile_init(void) {;} 138#endif 139 140void 141assert_mtx(struct lock_object *lock, int what) 142{ 143 144 mtx_assert((struct mtx *)lock, what); 145} 146 147void 148lock_mtx(struct lock_object *lock, int how) 149{ 150 151 mtx_lock((struct mtx *)lock); 152} 153 154void 155lock_spin(struct lock_object *lock, int how) 156{ 157 158 panic("spin locks can only use msleep_spin"); 159} 160 161int 162unlock_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 172int 173unlock_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 */ 183void 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 203void 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 223void 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 241void 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 */ 264int 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 */ 304void 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 ts = turnstile_trywait(&m->lock_object); 339 v = m->mtx_lock; 340 341 /* 342 * Check if the lock has been released while spinning for 343 * the turnstile chain lock. 344 */ 345 if (v == MTX_UNOWNED) { 346 turnstile_cancel(ts); 347 cpu_spinwait(); 348 continue; 349 } 350 351 MPASS(v != MTX_CONTESTED); 352 353 /* 354 * If the mutex isn't already contested and a failure occurs 355 * setting the contested bit, the mutex was either released 356 * or the state of the MTX_RECURSED bit changed. 357 */ 358 if ((v & MTX_CONTESTED) == 0 && 359 !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 360 turnstile_cancel(ts); 361 cpu_spinwait(); 362 continue; 363 } 364 365#ifdef ADAPTIVE_MUTEXES 366 /* 367 * If the current owner of the lock is executing on another 368 * CPU, spin instead of blocking. 369 */ 370 owner = (struct thread *)(v & ~MTX_FLAGMASK); 371#ifdef ADAPTIVE_GIANT 372 if (TD_IS_RUNNING(owner)) 373#else 374 if (m != &Giant && TD_IS_RUNNING(owner)) 375#endif 376 { 377 turnstile_cancel(ts); 378 while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) { 379 cpu_spinwait(); 380 } 381 continue; 382 } 383#endif /* ADAPTIVE_MUTEXES */ 384 385 /* 386 * We definitely must sleep for this lock. 387 */ 388 mtx_assert(m, MA_NOTOWNED); 389 390#ifdef KTR 391 if (!cont_logged) { 392 CTR6(KTR_CONTENTION, 393 "contention: %p at %s:%d wants %s, taken by %s:%d", 394 (void *)tid, file, line, m->lock_object.lo_name, 395 WITNESS_FILE(&m->lock_object), 396 WITNESS_LINE(&m->lock_object)); 397 cont_logged = 1; 398 } 399#endif 400 401 /* 402 * Block on the turnstile. 403 */ 404 turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE); 405 } 406#ifdef KTR 407 if (cont_logged) { 408 CTR4(KTR_CONTENTION, 409 "contention end: %s acquired by %p at %s:%d", 410 m->lock_object.lo_name, (void *)tid, file, line); 411 } 412#endif 413 lock_profile_obtain_lock_success(&m->lock_object, contested, 414 waittime, (file), (line)); 415} 416 417static void 418_mtx_lock_spin_failed(struct mtx *m) 419{ 420 struct thread *td; 421 422 td = mtx_owner(m); 423 424 /* If the mutex is unlocked, try again. */ 425 if (td == NULL) 426 return; 427 428 printf( "spin lock %p (%s) held by %p (tid %d) too long\n", 429 m, m->lock_object.lo_name, td, td->td_tid); 430#ifdef WITNESS 431 witness_display_spinlock(&m->lock_object, td); 432#endif 433 panic("spin lock held too long"); 434} 435 436#ifdef SMP 437/* 438 * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 439 * 440 * This is only called if we need to actually spin for the lock. Recursion 441 * is handled inline. 442 */ 443void 444_mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file, 445 int line) 446{ 447 int i = 0, contested = 0; 448 uint64_t waittime = 0; 449 450 if (LOCK_LOG_TEST(&m->lock_object, opts)) 451 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 452 453 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 454 while (!_obtain_lock(m, tid)) { 455 456 /* Give interrupts a chance while we spin. */ 457 spinlock_exit(); 458 while (m->mtx_lock != MTX_UNOWNED) { 459 if (i++ < 10000000) { 460 cpu_spinwait(); 461 continue; 462 } 463 if (i < 60000000 || kdb_active || panicstr != NULL) 464 DELAY(1); 465 else 466 _mtx_lock_spin_failed(m); 467 cpu_spinwait(); 468 } 469 spinlock_enter(); 470 } 471 472 if (LOCK_LOG_TEST(&m->lock_object, opts)) 473 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 474 475 lock_profile_obtain_lock_success(&m->lock_object, contested, 476 waittime, (file), (line)); 477} 478#endif /* SMP */ 479 480void 481_thread_lock_flags(struct thread *td, int opts, const char *file, int line) 482{ 483 struct mtx *m; 484 uintptr_t tid; 485 int i, contested; 486 uint64_t waittime; 487 488 489 contested = i = 0; 490 waittime = 0; 491 tid = (uintptr_t)curthread; 492 for (;;) { 493retry: 494 spinlock_enter(); 495 m = td->td_lock; 496 WITNESS_CHECKORDER(&m->lock_object, 497 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line); 498 while (!_obtain_lock(m, tid)) { 499 if (m->mtx_lock == tid) { 500 m->mtx_recurse++; 501 break; 502 } 503 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 504 /* Give interrupts a chance while we spin. */ 505 spinlock_exit(); 506 while (m->mtx_lock != MTX_UNOWNED) { 507 if (i++ < 10000000) 508 cpu_spinwait(); 509 else if (i < 60000000 || 510 kdb_active || panicstr != NULL) 511 DELAY(1); 512 else 513 _mtx_lock_spin_failed(m); 514 cpu_spinwait(); 515 if (m != td->td_lock) 516 goto retry; 517 } 518 spinlock_enter(); 519 } 520 if (m == td->td_lock) 521 break; 522 _rel_spin_lock(m); /* does spinlock_exit() */ 523 } 524 lock_profile_obtain_lock_success(&m->lock_object, contested, 525 waittime, (file), (line)); 526 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 527} 528 529struct mtx * 530thread_lock_block(struct thread *td) 531{ 532 struct mtx *lock; 533 534 spinlock_enter(); 535 THREAD_LOCK_ASSERT(td, MA_OWNED); 536 lock = td->td_lock; 537 td->td_lock = &blocked_lock; 538 mtx_unlock_spin(lock); 539 540 return (lock); 541} 542 543void 544thread_lock_unblock(struct thread *td, struct mtx *new) 545{ 546 mtx_assert(new, MA_OWNED); 547 MPASS(td->td_lock == &blocked_lock); 548 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new); 549 spinlock_exit(); 550} 551 552void 553thread_lock_set(struct thread *td, struct mtx *new) 554{ 555 struct mtx *lock; 556 557 mtx_assert(new, MA_OWNED); 558 THREAD_LOCK_ASSERT(td, MA_OWNED); 559 lock = td->td_lock; 560 td->td_lock = new; 561 mtx_unlock_spin(lock); 562} 563 564/* 565 * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 566 * 567 * We are only called here if the lock is recursed or contested (i.e. we 568 * need to wake up a blocked thread). 569 */ 570void 571_mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 572{ 573 struct turnstile *ts; 574 575 if (mtx_recursed(m)) { 576 if (--(m->mtx_recurse) == 0) 577 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 578 if (LOCK_LOG_TEST(&m->lock_object, opts)) 579 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 580 return; 581 } 582 583 /* 584 * We have to lock the chain before the turnstile so this turnstile 585 * can be removed from the hash list if it is empty. 586 */ 587 turnstile_chain_lock(&m->lock_object); 588 ts = turnstile_lookup(&m->lock_object); 589 if (LOCK_LOG_TEST(&m->lock_object, opts)) 590 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 591 592#ifdef ADAPTIVE_MUTEXES 593 if (ts == NULL) { 594 _release_lock_quick(m); 595 if (LOCK_LOG_TEST(&m->lock_object, opts)) 596 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m); 597 turnstile_chain_unlock(&m->lock_object); 598 return; 599 } 600#else 601 MPASS(ts != NULL); 602#endif 603 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 604 _release_lock_quick(m); 605 /* 606 * This turnstile is now no longer associated with the mutex. We can 607 * unlock the chain lock so a new turnstile may take it's place. 608 */ 609 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 610 turnstile_chain_unlock(&m->lock_object); 611} 612 613/* 614 * All the unlocking of MTX_SPIN locks is done inline. 615 * See the _rel_spin_lock() macro for the details. 616 */ 617 618/* 619 * The backing function for the INVARIANTS-enabled mtx_assert() 620 */ 621#ifdef INVARIANT_SUPPORT 622void 623_mtx_assert(struct mtx *m, int what, const char *file, int line) 624{ 625 626 if (panicstr != NULL || dumping) 627 return; 628 switch (what) { 629 case MA_OWNED: 630 case MA_OWNED | MA_RECURSED: 631 case MA_OWNED | MA_NOTRECURSED: 632 if (!mtx_owned(m)) 633 panic("mutex %s not owned at %s:%d", 634 m->lock_object.lo_name, file, line); 635 if (mtx_recursed(m)) { 636 if ((what & MA_NOTRECURSED) != 0) 637 panic("mutex %s recursed at %s:%d", 638 m->lock_object.lo_name, file, line); 639 } else if ((what & MA_RECURSED) != 0) { 640 panic("mutex %s unrecursed at %s:%d", 641 m->lock_object.lo_name, file, line); 642 } 643 break; 644 case MA_NOTOWNED: 645 if (mtx_owned(m)) 646 panic("mutex %s owned at %s:%d", 647 m->lock_object.lo_name, file, line); 648 break; 649 default: 650 panic("unknown mtx_assert at %s:%d", file, line); 651 } 652} 653#endif 654 655/* 656 * The MUTEX_DEBUG-enabled mtx_validate() 657 * 658 * Most of these checks have been moved off into the LO_INITIALIZED flag 659 * maintained by the witness code. 660 */ 661#ifdef MUTEX_DEBUG 662 663void mtx_validate(struct mtx *); 664 665void 666mtx_validate(struct mtx *m) 667{ 668 669/* 670 * XXX: When kernacc() does not require Giant we can reenable this check 671 */ 672#ifdef notyet 673 /* 674 * Can't call kernacc() from early init386(), especially when 675 * initializing Giant mutex, because some stuff in kernacc() 676 * requires Giant itself. 677 */ 678 if (!cold) 679 if (!kernacc((caddr_t)m, sizeof(m), 680 VM_PROT_READ | VM_PROT_WRITE)) 681 panic("Can't read and write to mutex %p", m); 682#endif 683} 684#endif 685 686/* 687 * General init routine used by the MTX_SYSINIT() macro. 688 */ 689void 690mtx_sysinit(void *arg) 691{ 692 struct mtx_args *margs = arg; 693 694 mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 695} 696 697/* 698 * Mutex initialization routine; initialize lock `m' of type contained in 699 * `opts' with options contained in `opts' and name `name.' The optional 700 * lock type `type' is used as a general lock category name for use with 701 * witness. 702 */ 703void 704mtx_init(struct mtx *m, const char *name, const char *type, int opts) 705{ 706 struct lock_class *class; 707 int flags; 708 709 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 710 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0); 711 712#ifdef MUTEX_DEBUG 713 /* Diagnostic and error correction */ 714 mtx_validate(m); 715#endif 716 717 /* Determine lock class and lock flags. */ 718 if (opts & MTX_SPIN) 719 class = &lock_class_mtx_spin; 720 else 721 class = &lock_class_mtx_sleep; 722 flags = 0; 723 if (opts & MTX_QUIET) 724 flags |= LO_QUIET; 725 if (opts & MTX_RECURSE) 726 flags |= LO_RECURSABLE; 727 if ((opts & MTX_NOWITNESS) == 0) 728 flags |= LO_WITNESS; 729 if (opts & MTX_DUPOK) 730 flags |= LO_DUPOK; 731 if (opts & MTX_NOPROFILE) 732 flags |= LO_NOPROFILE; 733 734 /* Initialize mutex. */ 735 m->mtx_lock = MTX_UNOWNED; 736 m->mtx_recurse = 0; 737 738 lock_init(&m->lock_object, class, name, type, flags); 739} 740 741/* 742 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 743 * passed in as a flag here because if the corresponding mtx_init() was 744 * called with MTX_QUIET set, then it will already be set in the mutex's 745 * flags. 746 */ 747void 748mtx_destroy(struct mtx *m) 749{ 750 751 if (!mtx_owned(m)) 752 MPASS(mtx_unowned(m)); 753 else { 754 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 755 756 /* Perform the non-mtx related part of mtx_unlock_spin(). */ 757 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) 758 spinlock_exit(); 759 else 760 curthread->td_locks--; 761 762 /* Tell witness this isn't locked to make it happy. */ 763 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__, 764 __LINE__); 765 } 766 767 m->mtx_lock = MTX_DESTROYED; 768 lock_destroy(&m->lock_object); 769} 770 771/* 772 * Intialize the mutex code and system mutexes. This is called from the MD 773 * startup code prior to mi_startup(). The per-CPU data space needs to be 774 * setup before this is called. 775 */ 776void 777mutex_init(void) 778{ 779 780 /* Setup turnstiles so that sleep mutexes work. */ 781 init_turnstiles(); 782 783 /* 784 * Initialize mutexes. 785 */ 786 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 787 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN); 788 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */ 789 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 790 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 791 mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 792 mtx_lock(&Giant); 793 794 lock_profile_init(); 795} 796 797#ifdef DDB 798void 799db_show_mtx(struct lock_object *lock) 800{ 801 struct thread *td; 802 struct mtx *m; 803 804 m = (struct mtx *)lock; 805 806 db_printf(" flags: {"); 807 if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 808 db_printf("SPIN"); 809 else 810 db_printf("DEF"); 811 if (m->lock_object.lo_flags & LO_RECURSABLE) 812 db_printf(", RECURSE"); 813 if (m->lock_object.lo_flags & LO_DUPOK) 814 db_printf(", DUPOK"); 815 db_printf("}\n"); 816 db_printf(" state: {"); 817 if (mtx_unowned(m)) 818 db_printf("UNOWNED"); 819 else if (mtx_destroyed(m)) 820 db_printf("DESTROYED"); 821 else { 822 db_printf("OWNED"); 823 if (m->mtx_lock & MTX_CONTESTED) 824 db_printf(", CONTESTED"); 825 if (m->mtx_lock & MTX_RECURSED) 826 db_printf(", RECURSED"); 827 } 828 db_printf("}\n"); 829 if (!mtx_unowned(m) && !mtx_destroyed(m)) { 830 td = mtx_owner(m); 831 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 832 td->td_tid, td->td_proc->p_pid, td->td_name); 833 if (mtx_recursed(m)) 834 db_printf(" recursed: %d\n", m->mtx_recurse); 835 } 836} 837#endif 838