10384fff8SJason Evans /*- 20384fff8SJason Evans * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 30384fff8SJason Evans * 40384fff8SJason Evans * Redistribution and use in source and binary forms, with or without 50384fff8SJason Evans * modification, are permitted provided that the following conditions 60384fff8SJason Evans * are met: 70384fff8SJason Evans * 1. Redistributions of source code must retain the above copyright 80384fff8SJason Evans * notice, this list of conditions and the following disclaimer. 90384fff8SJason Evans * 2. Redistributions in binary form must reproduce the above copyright 100384fff8SJason Evans * notice, this list of conditions and the following disclaimer in the 110384fff8SJason Evans * documentation and/or other materials provided with the distribution. 120384fff8SJason Evans * 3. Berkeley Software Design Inc's name may not be used to endorse or 130384fff8SJason Evans * promote products derived from this software without specific prior 140384fff8SJason Evans * written permission. 150384fff8SJason Evans * 160384fff8SJason Evans * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 170384fff8SJason Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 180384fff8SJason Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 190384fff8SJason Evans * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 200384fff8SJason Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 210384fff8SJason Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 220384fff8SJason Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 230384fff8SJason Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 240384fff8SJason Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 250384fff8SJason Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 260384fff8SJason Evans * SUCH DAMAGE. 270384fff8SJason Evans * 280384fff8SJason Evans * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 2936412d79SJohn Baldwin * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 300384fff8SJason Evans */ 310384fff8SJason Evans 320384fff8SJason Evans /* 33ba48b69aSJohn Baldwin * Machine independent bits of mutex implementation. 340384fff8SJason Evans */ 350384fff8SJason Evans 36677b542eSDavid E. O'Brien #include <sys/cdefs.h> 37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38677b542eSDavid E. O'Brien 392498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h" 409c36c934SJohn Baldwin #include "opt_ddb.h" 417c0435b9SKip Macy #include "opt_global.h" 42535eb309SJohn Baldwin #include "opt_mutex_wake_all.h" 439923b511SScott Long #include "opt_sched.h" 44a5a96a19SJohn Baldwin 450384fff8SJason Evans #include <sys/param.h> 466c35e809SDag-Erling Smørgrav #include <sys/systm.h> 4736412d79SJohn Baldwin #include <sys/bus.h> 481126349aSPaul Saab #include <sys/conf.h> 492d50560aSMarcel Moolenaar #include <sys/kdb.h> 5036412d79SJohn Baldwin #include <sys/kernel.h> 516c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 5219284646SJohn Baldwin #include <sys/lock.h> 53fb919e4dSMark Murray #include <sys/malloc.h> 5419284646SJohn Baldwin #include <sys/mutex.h> 550384fff8SJason Evans #include <sys/proc.h> 56c4f7a187SJohn Baldwin #include <sys/resourcevar.h> 57b43179fbSJeff Roberson #include <sys/sched.h> 586c35e809SDag-Erling Smørgrav #include <sys/sbuf.h> 59a5a96a19SJohn Baldwin #include <sys/sysctl.h> 60961a7b24SJohn Baldwin #include <sys/turnstile.h> 6136412d79SJohn Baldwin #include <sys/vmmeter.h> 627c0435b9SKip Macy #include <sys/lock_profile.h> 630384fff8SJason Evans 6436412d79SJohn Baldwin #include <machine/atomic.h> 6536412d79SJohn Baldwin #include <machine/bus.h> 660384fff8SJason Evans #include <machine/cpu.h> 6736412d79SJohn Baldwin 689c36c934SJohn Baldwin #include <ddb/ddb.h> 699c36c934SJohn Baldwin 708c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h> 718c4b6380SJohn Baldwin 7236412d79SJohn Baldwin #include <vm/vm.h> 7336412d79SJohn Baldwin #include <vm/vm_extern.h> 7436412d79SJohn Baldwin 750cde2e34SJason Evans /* 76b9a80acaSStephan Uphoff * Force MUTEX_WAKE_ALL for now. 77b9a80acaSStephan Uphoff * single thread wakeup needs fixes to avoid race conditions with 78b9a80acaSStephan Uphoff * priority inheritance. 79b9a80acaSStephan Uphoff */ 80b9a80acaSStephan Uphoff #ifndef MUTEX_WAKE_ALL 81b9a80acaSStephan Uphoff #define MUTEX_WAKE_ALL 82b9a80acaSStephan Uphoff #endif 83b9a80acaSStephan Uphoff 84cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 85cd6e6e4eSJohn Baldwin #define ADAPTIVE_MUTEXES 86cd6e6e4eSJohn Baldwin #endif 87cd6e6e4eSJohn Baldwin 88b9a80acaSStephan Uphoff /* 899ed346baSBosko Milekic * Internal utility macros. 900cde2e34SJason Evans */ 919ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 920cde2e34SJason Evans 93c0bfd703SJohn Baldwin #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED) 94c0bfd703SJohn Baldwin 9549b94bfcSJohn Baldwin #define mtx_owner(m) ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK)) 969ed346baSBosko Milekic 97d272fe53SJohn Baldwin #ifdef DDB 98d272fe53SJohn Baldwin static void db_show_mtx(struct lock_object *lock); 99d272fe53SJohn Baldwin #endif 1006e21afd4SJohn Baldwin static void lock_mtx(struct lock_object *lock, int how); 1016e21afd4SJohn Baldwin static void lock_spin(struct lock_object *lock, int how); 1026e21afd4SJohn Baldwin static int unlock_mtx(struct lock_object *lock); 1036e21afd4SJohn Baldwin static int unlock_spin(struct lock_object *lock); 104d272fe53SJohn Baldwin 1050cde2e34SJason Evans /* 10619284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 1070cde2e34SJason Evans */ 10819284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 109ae8dde30SJohn Baldwin .lc_name = "sleep mutex", 110ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, 111d272fe53SJohn Baldwin #ifdef DDB 112ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 113d272fe53SJohn Baldwin #endif 1146e21afd4SJohn Baldwin .lc_lock = lock_mtx, 1156e21afd4SJohn Baldwin .lc_unlock = unlock_mtx, 11619284646SJohn Baldwin }; 11719284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 118ae8dde30SJohn Baldwin .lc_name = "spin mutex", 119ae8dde30SJohn Baldwin .lc_flags = LC_SPINLOCK | LC_RECURSABLE, 120d272fe53SJohn Baldwin #ifdef DDB 121ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 122d272fe53SJohn Baldwin #endif 1236e21afd4SJohn Baldwin .lc_lock = lock_spin, 1246e21afd4SJohn Baldwin .lc_unlock = unlock_spin, 1258484de75SJohn Baldwin }; 1268484de75SJohn Baldwin 1279ed346baSBosko Milekic /* 128c53c013bSJohn Baldwin * System-wide mutexes 129c53c013bSJohn Baldwin */ 130c53c013bSJohn Baldwin struct mtx sched_lock; 131c53c013bSJohn Baldwin struct mtx Giant; 132c53c013bSJohn Baldwin 1331364a812SKip Macy #ifdef LOCK_PROFILING 1341364a812SKip Macy static inline void lock_profile_init(void) 1351364a812SKip Macy { 1361364a812SKip Macy int i; 1371364a812SKip Macy /* Initialize the mutex profiling locks */ 1381364a812SKip Macy for (i = 0; i < LPROF_LOCK_SIZE; i++) { 1391364a812SKip Macy mtx_init(&lprof_locks[i], "mprof lock", 1401364a812SKip Macy NULL, MTX_SPIN|MTX_QUIET|MTX_NOPROFILE); 1411364a812SKip Macy } 1421364a812SKip Macy } 1431364a812SKip Macy #else 1441364a812SKip Macy static inline void lock_profile_init(void) {;} 1451364a812SKip Macy #endif 1461364a812SKip Macy 1476e21afd4SJohn Baldwin void 1486e21afd4SJohn Baldwin lock_mtx(struct lock_object *lock, int how) 1496e21afd4SJohn Baldwin { 1506e21afd4SJohn Baldwin 1516e21afd4SJohn Baldwin mtx_lock((struct mtx *)lock); 1526e21afd4SJohn Baldwin } 1536e21afd4SJohn Baldwin 1546e21afd4SJohn Baldwin void 1556e21afd4SJohn Baldwin lock_spin(struct lock_object *lock, int how) 1566e21afd4SJohn Baldwin { 1576e21afd4SJohn Baldwin 1586e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 1596e21afd4SJohn Baldwin } 1606e21afd4SJohn Baldwin 1616e21afd4SJohn Baldwin int 1626e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock) 1636e21afd4SJohn Baldwin { 1646e21afd4SJohn Baldwin struct mtx *m; 1656e21afd4SJohn Baldwin 1666e21afd4SJohn Baldwin m = (struct mtx *)lock; 1676e21afd4SJohn Baldwin mtx_assert(m, MA_OWNED | MA_NOTRECURSED); 1686e21afd4SJohn Baldwin mtx_unlock(m); 1696e21afd4SJohn Baldwin return (0); 1706e21afd4SJohn Baldwin } 1716e21afd4SJohn Baldwin 1726e21afd4SJohn Baldwin int 1736e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock) 1746e21afd4SJohn Baldwin { 1756e21afd4SJohn Baldwin 1766e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 1776e21afd4SJohn Baldwin } 1786e21afd4SJohn Baldwin 1790cde2e34SJason Evans /* 1806283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 1816283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 1826283b7d0SJohn Baldwin */ 1836283b7d0SJohn Baldwin void 1846283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 1856283b7d0SJohn Baldwin { 1866283b7d0SJohn Baldwin 187dde96c99SJohn Baldwin MPASS(curthread != NULL); 188186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 189186abbd7SJohn Baldwin ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); 190aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 191aa89d8cdSJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 1920d975d63SJohn Baldwin file, line)); 193aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 1948d768e76SJohn Baldwin file, line); 1957c0435b9SKip Macy 196dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 197aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 198dde96c99SJohn Baldwin line); 199aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 200764e4d54SJohn Baldwin curthread->td_locks++; 2016283b7d0SJohn Baldwin } 2026283b7d0SJohn Baldwin 2036283b7d0SJohn Baldwin void 2046283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 2056283b7d0SJohn Baldwin { 206dde96c99SJohn Baldwin MPASS(curthread != NULL); 207186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 208186abbd7SJohn Baldwin ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); 209aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 210aa89d8cdSJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2110d975d63SJohn Baldwin file, line)); 212764e4d54SJohn Baldwin curthread->td_locks--; 213aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 214aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 2150d975d63SJohn Baldwin line); 21621377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 217c66d7606SKip Macy 21870fe8436SKip Macy if (m->mtx_recurse == 0) 219aa89d8cdSJohn Baldwin lock_profile_release_lock(&m->lock_object); 220dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 2216283b7d0SJohn Baldwin } 2226283b7d0SJohn Baldwin 2236283b7d0SJohn Baldwin void 2246283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 2256283b7d0SJohn Baldwin { 2266283b7d0SJohn Baldwin 227dde96c99SJohn Baldwin MPASS(curthread != NULL); 228186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 229186abbd7SJohn Baldwin ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); 230aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 2310d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 232aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 233aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 2348d768e76SJohn Baldwin file, line); 235dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 236aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 237dde96c99SJohn Baldwin line); 238aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 2396283b7d0SJohn Baldwin } 2406283b7d0SJohn Baldwin 2416283b7d0SJohn Baldwin void 2426283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 2436283b7d0SJohn Baldwin { 244c66d7606SKip Macy 245dde96c99SJohn Baldwin MPASS(curthread != NULL); 246186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 247186abbd7SJohn Baldwin ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); 248aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 2490d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 250aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 251aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 252aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 253dde96c99SJohn Baldwin line); 2540d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 255c66d7606SKip Macy 256dde96c99SJohn Baldwin _rel_spin_lock(m); 2576283b7d0SJohn Baldwin } 2586283b7d0SJohn Baldwin 2596283b7d0SJohn Baldwin /* 2609ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 261eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 262eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 2630cde2e34SJason Evans */ 2640cde2e34SJason Evans int 2659ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 2660cde2e34SJason Evans { 267fe68a916SKip Macy int rval, contested = 0; 2687c0435b9SKip Macy uint64_t waittime = 0; 2690cde2e34SJason Evans 270b40ce416SJulian Elischer MPASS(curthread != NULL); 271186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 272186abbd7SJohn Baldwin ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); 273aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 274aa89d8cdSJohn Baldwin ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 27583cece6fSJohn Baldwin file, line)); 2769ed346baSBosko Milekic 277aa89d8cdSJohn Baldwin if (mtx_owned(m) && (m->lock_object.lo_flags & LO_RECURSABLE) != 0) { 278eac09796SJohn Baldwin m->mtx_recurse++; 279eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 280eac09796SJohn Baldwin rval = 1; 281eac09796SJohn Baldwin } else 282122eceefSJohn Baldwin rval = _obtain_lock(m, (uintptr_t)curthread); 2839ed346baSBosko Milekic 284aa89d8cdSJohn Baldwin LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line); 285764e4d54SJohn Baldwin if (rval) { 286aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 2872d96f0b1SJohn Baldwin file, line); 288764e4d54SJohn Baldwin curthread->td_locks++; 289fe68a916SKip Macy if (m->mtx_recurse == 0) 290aa89d8cdSJohn Baldwin lock_profile_obtain_lock_success(&m->lock_object, contested, 291fe68a916SKip Macy waittime, file, line); 2927c0435b9SKip Macy 293764e4d54SJohn Baldwin } 2949ed346baSBosko Milekic 29519284646SJohn Baldwin return (rval); 2960cde2e34SJason Evans } 2970cde2e34SJason Evans 2980cde2e34SJason Evans /* 2999ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 3009ed346baSBosko Milekic * 3019ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 3029ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 3030cde2e34SJason Evans */ 3040cde2e34SJason Evans void 305122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file, 306bdcfcf5bSJohn Baldwin int line) 30736412d79SJohn Baldwin { 308cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES 30976447e56SJohn Baldwin volatile struct thread *owner; 3102498cf8cSJohn Baldwin #endif 31102bd1bcdSIan Dowse #ifdef KTR 31202bd1bcdSIan Dowse int cont_logged = 0; 31302bd1bcdSIan Dowse #endif 31470fe8436SKip Macy int contested = 0; 31570fe8436SKip Macy uint64_t waittime = 0; 3167c0435b9SKip Macy uintptr_t v; 31736412d79SJohn Baldwin 3185fa8dd90SJohn Baldwin if (mtx_owned(m)) { 319aa89d8cdSJohn Baldwin KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 320eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 321aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 32236412d79SJohn Baldwin m->mtx_recurse++; 32308812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 324aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 3255746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 32636412d79SJohn Baldwin return; 32736412d79SJohn Baldwin } 3289ed346baSBosko Milekic 32970fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, 33070fe8436SKip Macy &contested, &waittime); 331aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 33215ec816aSJohn Baldwin CTR4(KTR_LOCK, 33315ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 334aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)m->mtx_lock, file, line); 3351bd0eefbSJohn Baldwin 336122eceefSJohn Baldwin while (!_obtain_lock(m, tid)) { 337aa89d8cdSJohn Baldwin turnstile_lock(&m->lock_object); 3385fa8dd90SJohn Baldwin v = m->mtx_lock; 3395fa8dd90SJohn Baldwin 34036412d79SJohn Baldwin /* 3419ed346baSBosko Milekic * Check if the lock has been released while spinning for 342961a7b24SJohn Baldwin * the turnstile chain lock. 34336412d79SJohn Baldwin */ 3445fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 345aa89d8cdSJohn Baldwin turnstile_release(&m->lock_object); 3469f1b87f1SMaxime Henrion cpu_spinwait(); 34736412d79SJohn Baldwin continue; 34836412d79SJohn Baldwin } 3499ed346baSBosko Milekic 350535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 351535eb309SJohn Baldwin MPASS(v != MTX_CONTESTED); 352535eb309SJohn Baldwin #else 35336412d79SJohn Baldwin /* 3549ed346baSBosko Milekic * The mutex was marked contested on release. This means that 355f7ee1590SJohn Baldwin * there are other threads blocked on it. Grab ownership of 356f7ee1590SJohn Baldwin * it and propagate its priority to the current thread if 357f7ee1590SJohn Baldwin * necessary. 35836412d79SJohn Baldwin */ 35936412d79SJohn Baldwin if (v == MTX_CONTESTED) { 360122eceefSJohn Baldwin m->mtx_lock = tid | MTX_CONTESTED; 361aa89d8cdSJohn Baldwin turnstile_claim(&m->lock_object); 3628dc10be8SRobert Watson break; 36336412d79SJohn Baldwin } 364535eb309SJohn Baldwin #endif 3659ed346baSBosko Milekic 36636412d79SJohn Baldwin /* 3679ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 3689ed346baSBosko Milekic * setting the contested bit, the mutex was either released 3699ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 37036412d79SJohn Baldwin */ 37136412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 372122eceefSJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 373aa89d8cdSJohn Baldwin turnstile_release(&m->lock_object); 3749f1b87f1SMaxime Henrion cpu_spinwait(); 37536412d79SJohn Baldwin continue; 37636412d79SJohn Baldwin } 37736412d79SJohn Baldwin 378cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES 3792498cf8cSJohn Baldwin /* 3802498cf8cSJohn Baldwin * If the current owner of the lock is executing on another 3812498cf8cSJohn Baldwin * CPU, spin instead of blocking. 3822498cf8cSJohn Baldwin */ 38349b94bfcSJohn Baldwin owner = (struct thread *)(v & ~MTX_FLAGMASK); 384a9abdce4SRobert Watson #ifdef ADAPTIVE_GIANT 3851364a812SKip Macy if (TD_IS_RUNNING(owner)) 386a9abdce4SRobert Watson #else 3871364a812SKip Macy if (m != &Giant && TD_IS_RUNNING(owner)) 388a9abdce4SRobert Watson #endif 3891364a812SKip Macy { 390aa89d8cdSJohn Baldwin turnstile_release(&m->lock_object); 39127dad03cSJohn Baldwin while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) { 3929f1b87f1SMaxime Henrion cpu_spinwait(); 3937fcca609SJohn Baldwin } 3942498cf8cSJohn Baldwin continue; 3952498cf8cSJohn Baldwin } 396cd6e6e4eSJohn Baldwin #endif /* ADAPTIVE_MUTEXES */ 3972498cf8cSJohn Baldwin 3989ed346baSBosko Milekic /* 3997feefcd6SJohn Baldwin * We definitely must sleep for this lock. 4009ed346baSBosko Milekic */ 40136412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 40236412d79SJohn Baldwin 40302bd1bcdSIan Dowse #ifdef KTR 40402bd1bcdSIan Dowse if (!cont_logged) { 40502bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 40602bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 407aa89d8cdSJohn Baldwin (void *)tid, file, line, m->lock_object.lo_name, 408aa89d8cdSJohn Baldwin WITNESS_FILE(&m->lock_object), 409aa89d8cdSJohn Baldwin WITNESS_LINE(&m->lock_object)); 41002bd1bcdSIan Dowse cont_logged = 1; 41102bd1bcdSIan Dowse } 41202bd1bcdSIan Dowse #endif 41336412d79SJohn Baldwin 4149ed346baSBosko Milekic /* 415961a7b24SJohn Baldwin * Block on the turnstile. 4169ed346baSBosko Milekic */ 417aa89d8cdSJohn Baldwin turnstile_wait(&m->lock_object, mtx_owner(m), 4187aa4f685SJohn Baldwin TS_EXCLUSIVE_QUEUE); 41936412d79SJohn Baldwin } 42002bd1bcdSIan Dowse #ifdef KTR 42102bd1bcdSIan Dowse if (cont_logged) { 42202bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 42302bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 424aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)tid, file, line); 42502bd1bcdSIan Dowse } 42602bd1bcdSIan Dowse #endif 42770fe8436SKip Macy lock_profile_obtain_lock_success(&m->lock_object, contested, 42870fe8436SKip Macy waittime, (file), (line)); 4299ed346baSBosko Milekic } 4309ed346baSBosko Milekic 43133fb8a38SJohn Baldwin #ifdef SMP 4329ed346baSBosko Milekic /* 4339ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 4349ed346baSBosko Milekic * 4359ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 4369ed346baSBosko Milekic * is handled inline. 4379ed346baSBosko Milekic */ 4389ed346baSBosko Milekic void 439122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file, 440bdcfcf5bSJohn Baldwin int line) 44136412d79SJohn Baldwin { 44270fe8436SKip Macy int i = 0, contested = 0; 4430fa2168bSJohn Baldwin struct thread *td; 44470fe8436SKip Macy uint64_t waittime = 0; 44536412d79SJohn Baldwin 446aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 4475746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 4489ed346baSBosko Milekic 44970fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 450f781b5a4SJohn Baldwin while (!_obtain_lock(m, tid)) { 4519ed346baSBosko Milekic 4527141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 453c6a37e84SJohn Baldwin spinlock_exit(); 45436412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 455703fc290SJohn Baldwin if (i++ < 10000000) { 4569f1b87f1SMaxime Henrion cpu_spinwait(); 45736412d79SJohn Baldwin continue; 458703fc290SJohn Baldwin } 4590fa2168bSJohn Baldwin if (i < 60000000 || kdb_active || panicstr != NULL) 46036412d79SJohn Baldwin DELAY(1); 4610fa2168bSJohn Baldwin else { 4620fa2168bSJohn Baldwin td = mtx_owner(m); 4630fa2168bSJohn Baldwin 4640fa2168bSJohn Baldwin /* If the mutex is unlocked, try again. */ 4650fa2168bSJohn Baldwin if (td == NULL) 4660fa2168bSJohn Baldwin continue; 4670fa2168bSJohn Baldwin printf( 4680fa2168bSJohn Baldwin "spin lock %p (%s) held by %p (tid %d) too long\n", 469aa89d8cdSJohn Baldwin m, m->lock_object.lo_name, td, td->td_tid); 47041109518SJohn Baldwin #ifdef WITNESS 471aa89d8cdSJohn Baldwin witness_display_spinlock(&m->lock_object, td); 47241109518SJohn Baldwin #endif 47341109518SJohn Baldwin panic("spin lock held too long"); 47441109518SJohn Baldwin } 4759f1b87f1SMaxime Henrion cpu_spinwait(); 47636412d79SJohn Baldwin } 477c6a37e84SJohn Baldwin spinlock_enter(); 47836412d79SJohn Baldwin } 47936412d79SJohn Baldwin 480aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 4819ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 4829ed346baSBosko Milekic 48370fe8436SKip Macy lock_profile_obtain_lock_success(&m->lock_object, contested, 48470fe8436SKip Macy waittime, (file), (line)); 48570fe8436SKip Macy 48636412d79SJohn Baldwin } 48733fb8a38SJohn Baldwin #endif /* SMP */ 48836412d79SJohn Baldwin 4899ed346baSBosko Milekic /* 4909ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 4919ed346baSBosko Milekic * 4929ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 4939ed346baSBosko Milekic * need to wake up a blocked thread). 4949ed346baSBosko Milekic */ 49536412d79SJohn Baldwin void 4969ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 49736412d79SJohn Baldwin { 498961a7b24SJohn Baldwin struct turnstile *ts; 4990c0b25aeSJohn Baldwin #ifndef PREEMPTION 500b40ce416SJulian Elischer struct thread *td, *td1; 5010c0b25aeSJohn Baldwin #endif 5029ed346baSBosko Milekic 50308812b39SBosko Milekic if (mtx_recursed(m)) { 50436412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 50508812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 506aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 5079ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 50836412d79SJohn Baldwin return; 50936412d79SJohn Baldwin } 5109ed346baSBosko Milekic 511aa89d8cdSJohn Baldwin turnstile_lock(&m->lock_object); 512aa89d8cdSJohn Baldwin ts = turnstile_lookup(&m->lock_object); 513aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 5149ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 5159ed346baSBosko Milekic 516cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES 517961a7b24SJohn Baldwin if (ts == NULL) { 5182498cf8cSJohn Baldwin _release_lock_quick(m); 519aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 5202498cf8cSJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m); 521aa89d8cdSJohn Baldwin turnstile_release(&m->lock_object); 5222498cf8cSJohn Baldwin return; 5232498cf8cSJohn Baldwin } 524961a7b24SJohn Baldwin #else 525961a7b24SJohn Baldwin MPASS(ts != NULL); 5262498cf8cSJohn Baldwin #endif 5270c0b25aeSJohn Baldwin #ifndef PREEMPTION 528961a7b24SJohn Baldwin /* XXX */ 5297aa4f685SJohn Baldwin td1 = turnstile_head(ts, TS_EXCLUSIVE_QUEUE); 5300c0b25aeSJohn Baldwin #endif 531535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 5327aa4f685SJohn Baldwin turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 533535eb309SJohn Baldwin _release_lock_quick(m); 534535eb309SJohn Baldwin #else 5357aa4f685SJohn Baldwin if (turnstile_signal(ts, TS_EXCLUSIVE_QUEUE)) { 53636412d79SJohn Baldwin _release_lock_quick(m); 537aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 5389ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 539961a7b24SJohn Baldwin } else { 540f7ee1590SJohn Baldwin m->mtx_lock = MTX_CONTESTED; 541aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 542961a7b24SJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested", 543961a7b24SJohn Baldwin m); 544e0817317SJulian Elischer } 545535eb309SJohn Baldwin #endif 5467aa4f685SJohn Baldwin turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 5479ed346baSBosko Milekic 5480c0b25aeSJohn Baldwin #ifndef PREEMPTION 549961a7b24SJohn Baldwin /* 550961a7b24SJohn Baldwin * XXX: This is just a hack until preemption is done. However, 551961a7b24SJohn Baldwin * once preemption is done we need to either wrap the 552961a7b24SJohn Baldwin * turnstile_signal() and release of the actual lock in an 553961a7b24SJohn Baldwin * extra critical section or change the preemption code to 554961a7b24SJohn Baldwin * always just set a flag and never do instant-preempts. 555961a7b24SJohn Baldwin */ 556961a7b24SJohn Baldwin td = curthread; 557961a7b24SJohn Baldwin if (td->td_critnest > 0 || td1->td_priority >= td->td_priority) 558961a7b24SJohn Baldwin return; 55970fe8436SKip Macy 560961a7b24SJohn Baldwin mtx_lock_spin(&sched_lock); 561961a7b24SJohn Baldwin if (!TD_IS_RUNNING(td1)) { 56236412d79SJohn Baldwin #ifdef notyet 563b40ce416SJulian Elischer if (td->td_ithd != NULL) { 564b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 56536412d79SJohn Baldwin 56636412d79SJohn Baldwin if (it->it_interrupted) { 567aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 56836412d79SJohn Baldwin CTR2(KTR_LOCK, 56915ec816aSJohn Baldwin "_mtx_unlock_sleep: %p interrupted %p", 57036412d79SJohn Baldwin it, it->it_interrupted); 57136412d79SJohn Baldwin intr_thd_fixup(it); 57236412d79SJohn Baldwin } 57336412d79SJohn Baldwin } 57436412d79SJohn Baldwin #endif 575aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 576562e4ffeSJohn Baldwin CTR2(KTR_LOCK, 5779ed346baSBosko Milekic "_mtx_unlock_sleep: %p switching out lock=%p", m, 5789ed346baSBosko Milekic (void *)m->mtx_lock); 5799ed346baSBosko Milekic 580bf0acc27SJohn Baldwin mi_switch(SW_INVOL, NULL); 581aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 5829ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 58331271627SJohn Baldwin m, (void *)m->mtx_lock); 58436412d79SJohn Baldwin } 5859ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 5860c0b25aeSJohn Baldwin #endif 5879ed346baSBosko Milekic } 5889ed346baSBosko Milekic 5899ed346baSBosko Milekic /* 5909ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 5919ed346baSBosko Milekic * See the _rel_spin_lock() macro for the details. 5929ed346baSBosko Milekic */ 5939ed346baSBosko Milekic 5949ed346baSBosko Milekic /* 59515ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 5969ed346baSBosko Milekic */ 5971103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 5980cde2e34SJason Evans void 59956771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line) 6000cde2e34SJason Evans { 6015cb0fbe4SJohn Baldwin 6021126349aSPaul Saab if (panicstr != NULL || dumping) 6035cb0fbe4SJohn Baldwin return; 604a10f4966SJake Burkholder switch (what) { 6050cde2e34SJason Evans case MA_OWNED: 6060cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 6070cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 608a10f4966SJake Burkholder if (!mtx_owned(m)) 6090cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 610aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 611a10f4966SJake Burkholder if (mtx_recursed(m)) { 612a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 6130cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 614aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 615a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 6160cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 617aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 6180cde2e34SJason Evans } 6190cde2e34SJason Evans break; 6200cde2e34SJason Evans case MA_NOTOWNED: 621a10f4966SJake Burkholder if (mtx_owned(m)) 6220cde2e34SJason Evans panic("mutex %s owned at %s:%d", 623aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 6240cde2e34SJason Evans break; 6250cde2e34SJason Evans default: 62656771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 6270cde2e34SJason Evans } 6280cde2e34SJason Evans } 6290cde2e34SJason Evans #endif 6300cde2e34SJason Evans 6319ed346baSBosko Milekic /* 6329ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 63319284646SJohn Baldwin * 63419284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 63519284646SJohn Baldwin * maintained by the witness code. 6369ed346baSBosko Milekic */ 63736412d79SJohn Baldwin #ifdef MUTEX_DEBUG 63836412d79SJohn Baldwin 6394d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 64036412d79SJohn Baldwin 64119284646SJohn Baldwin void 64219284646SJohn Baldwin mtx_validate(struct mtx *m) 64336412d79SJohn Baldwin { 64436412d79SJohn Baldwin 64536412d79SJohn Baldwin /* 646fa669ab7SPoul-Henning Kamp * XXX: When kernacc() does not require Giant we can reenable this check 647fa669ab7SPoul-Henning Kamp */ 648fa669ab7SPoul-Henning Kamp #ifdef notyet 649fa669ab7SPoul-Henning Kamp /* 65076dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 65176dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 65276dcbd6fSBosko Milekic * requires Giant itself. 65376dcbd6fSBosko Milekic */ 654ab07087eSBosko Milekic if (!cold) 655ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 656ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 65719284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 65836412d79SJohn Baldwin #endif 65936412d79SJohn Baldwin } 66036412d79SJohn Baldwin #endif 66136412d79SJohn Baldwin 6629ed346baSBosko Milekic /* 663c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 664c27b5699SAndrew R. Reiter */ 665c27b5699SAndrew R. Reiter void 666c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 667c27b5699SAndrew R. Reiter { 668c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 669c27b5699SAndrew R. Reiter 6700c88508aSJohn Baldwin mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 671c27b5699SAndrew R. Reiter } 672c27b5699SAndrew R. Reiter 673c27b5699SAndrew R. Reiter /* 6749ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 6750c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 6760c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 6770c88508aSJohn Baldwin * witness. 6789ed346baSBosko Milekic */ 67936412d79SJohn Baldwin void 6800c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts) 68136412d79SJohn Baldwin { 68283a81bcbSJohn Baldwin struct lock_class *class; 68383a81bcbSJohn Baldwin int flags; 6849ed346baSBosko Milekic 68519284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 6867c0435b9SKip Macy MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0); 6879ed346baSBosko Milekic 68836412d79SJohn Baldwin #ifdef MUTEX_DEBUG 6899ed346baSBosko Milekic /* Diagnostic and error correction */ 69019284646SJohn Baldwin mtx_validate(m); 6916936206eSJohn Baldwin #endif 69236412d79SJohn Baldwin 69383a81bcbSJohn Baldwin /* Determine lock class and lock flags. */ 69419284646SJohn Baldwin if (opts & MTX_SPIN) 69583a81bcbSJohn Baldwin class = &lock_class_mtx_spin; 69619284646SJohn Baldwin else 69783a81bcbSJohn Baldwin class = &lock_class_mtx_sleep; 69883a81bcbSJohn Baldwin flags = 0; 69919284646SJohn Baldwin if (opts & MTX_QUIET) 70083a81bcbSJohn Baldwin flags |= LO_QUIET; 70119284646SJohn Baldwin if (opts & MTX_RECURSE) 70283a81bcbSJohn Baldwin flags |= LO_RECURSABLE; 70319284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 70483a81bcbSJohn Baldwin flags |= LO_WITNESS; 705f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 70683a81bcbSJohn Baldwin flags |= LO_DUPOK; 7077c0435b9SKip Macy if (opts & MTX_NOPROFILE) 7087c0435b9SKip Macy flags |= LO_NOPROFILE; 70919284646SJohn Baldwin 71083a81bcbSJohn Baldwin /* Initialize mutex. */ 71119284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 71283a81bcbSJohn Baldwin m->mtx_recurse = 0; 7139ed346baSBosko Milekic 714aa89d8cdSJohn Baldwin lock_profile_object_init(&m->lock_object, class, name); 715aa89d8cdSJohn Baldwin lock_init(&m->lock_object, class, name, type, flags); 71636412d79SJohn Baldwin } 71736412d79SJohn Baldwin 7189ed346baSBosko Milekic /* 71919284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 72019284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 72119284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 72219284646SJohn Baldwin * flags. 7239ed346baSBosko Milekic */ 72436412d79SJohn Baldwin void 72536412d79SJohn Baldwin mtx_destroy(struct mtx *m) 72636412d79SJohn Baldwin { 72736412d79SJohn Baldwin 72819284646SJohn Baldwin if (!mtx_owned(m)) 72919284646SJohn Baldwin MPASS(mtx_unowned(m)); 73019284646SJohn Baldwin else { 73108812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 7329ed346baSBosko Milekic 733861a2308SScott Long /* Perform the non-mtx related part of mtx_unlock_spin(). */ 734aa89d8cdSJohn Baldwin if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) 735861a2308SScott Long spinlock_exit(); 736764e4d54SJohn Baldwin else 737764e4d54SJohn Baldwin curthread->td_locks--; 738861a2308SScott Long 73919284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 740aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__, 741c86b6ff5SJohn Baldwin __LINE__); 74236412d79SJohn Baldwin } 7430384fff8SJason Evans 744186abbd7SJohn Baldwin m->mtx_lock = MTX_DESTROYED; 745aa89d8cdSJohn Baldwin lock_profile_object_destroy(&m->lock_object); 746aa89d8cdSJohn Baldwin lock_destroy(&m->lock_object); 7470384fff8SJason Evans } 748d23f5958SMatthew Dillon 749d23f5958SMatthew Dillon /* 750c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 751c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 752c53c013bSJohn Baldwin * setup before this is called. 753c53c013bSJohn Baldwin */ 754c53c013bSJohn Baldwin void 755c53c013bSJohn Baldwin mutex_init(void) 756c53c013bSJohn Baldwin { 757c53c013bSJohn Baldwin 758961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 759961a7b24SJohn Baldwin init_turnstiles(); 760961a7b24SJohn Baldwin 761c53c013bSJohn Baldwin /* 762c53c013bSJohn Baldwin * Initialize mutexes. 763c53c013bSJohn Baldwin */ 7640c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 7650c88508aSJohn Baldwin mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE); 7660c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 7678c4b6380SJohn Baldwin mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 768c53c013bSJohn Baldwin mtx_lock(&Giant); 7697c0435b9SKip Macy 7707c0435b9SKip Macy lock_profile_init(); 771c53c013bSJohn Baldwin } 772d272fe53SJohn Baldwin 773d272fe53SJohn Baldwin #ifdef DDB 774d272fe53SJohn Baldwin void 775d272fe53SJohn Baldwin db_show_mtx(struct lock_object *lock) 776d272fe53SJohn Baldwin { 777d272fe53SJohn Baldwin struct thread *td; 778d272fe53SJohn Baldwin struct mtx *m; 779d272fe53SJohn Baldwin 780d272fe53SJohn Baldwin m = (struct mtx *)lock; 781d272fe53SJohn Baldwin 782d272fe53SJohn Baldwin db_printf(" flags: {"); 78383a81bcbSJohn Baldwin if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 784d272fe53SJohn Baldwin db_printf("SPIN"); 785d272fe53SJohn Baldwin else 786d272fe53SJohn Baldwin db_printf("DEF"); 787aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_RECURSABLE) 788d272fe53SJohn Baldwin db_printf(", RECURSE"); 789aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_DUPOK) 790d272fe53SJohn Baldwin db_printf(", DUPOK"); 791d272fe53SJohn Baldwin db_printf("}\n"); 792d272fe53SJohn Baldwin db_printf(" state: {"); 793d272fe53SJohn Baldwin if (mtx_unowned(m)) 794d272fe53SJohn Baldwin db_printf("UNOWNED"); 795c0bfd703SJohn Baldwin else if (mtx_destroyed(m)) 796c0bfd703SJohn Baldwin db_printf("DESTROYED"); 797d272fe53SJohn Baldwin else { 798d272fe53SJohn Baldwin db_printf("OWNED"); 799d272fe53SJohn Baldwin if (m->mtx_lock & MTX_CONTESTED) 800d272fe53SJohn Baldwin db_printf(", CONTESTED"); 801d272fe53SJohn Baldwin if (m->mtx_lock & MTX_RECURSED) 802d272fe53SJohn Baldwin db_printf(", RECURSED"); 803d272fe53SJohn Baldwin } 804d272fe53SJohn Baldwin db_printf("}\n"); 805c0bfd703SJohn Baldwin if (!mtx_unowned(m) && !mtx_destroyed(m)) { 806d272fe53SJohn Baldwin td = mtx_owner(m); 807d272fe53SJohn Baldwin db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 808d272fe53SJohn Baldwin td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm); 809d272fe53SJohn Baldwin if (mtx_recursed(m)) 810d272fe53SJohn Baldwin db_printf(" recursed: %d\n", m->mtx_recurse); 811d272fe53SJohn Baldwin } 812d272fe53SJohn Baldwin } 813d272fe53SJohn Baldwin #endif 814