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" 42a5aedd68SStacey Son #include "opt_kdtrace.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 75cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 76cd6e6e4eSJohn Baldwin #define ADAPTIVE_MUTEXES 77cd6e6e4eSJohn Baldwin #endif 78cd6e6e4eSJohn Baldwin 79b9a80acaSStephan Uphoff /* 809ed346baSBosko Milekic * Internal utility macros. 810cde2e34SJason Evans */ 829ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 830cde2e34SJason Evans 84c0bfd703SJohn Baldwin #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED) 85c0bfd703SJohn Baldwin 8649b94bfcSJohn Baldwin #define mtx_owner(m) ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK)) 879ed346baSBosko Milekic 88*d576deedSPawel Jakub Dawidek static void assert_mtx(const struct lock_object *lock, int what); 89d272fe53SJohn Baldwin #ifdef DDB 90*d576deedSPawel Jakub Dawidek static void db_show_mtx(const struct lock_object *lock); 91d272fe53SJohn Baldwin #endif 926e21afd4SJohn Baldwin static void lock_mtx(struct lock_object *lock, int how); 936e21afd4SJohn Baldwin static void lock_spin(struct lock_object *lock, int how); 94a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 95*d576deedSPawel Jakub Dawidek static int owner_mtx(const struct lock_object *lock, 96*d576deedSPawel Jakub Dawidek struct thread **owner); 97a5aedd68SStacey Son #endif 986e21afd4SJohn Baldwin static int unlock_mtx(struct lock_object *lock); 996e21afd4SJohn Baldwin static int unlock_spin(struct lock_object *lock); 100d272fe53SJohn Baldwin 1010cde2e34SJason Evans /* 10219284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 1030cde2e34SJason Evans */ 10419284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 105ae8dde30SJohn Baldwin .lc_name = "sleep mutex", 106ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, 107f9721b43SAttilio Rao .lc_assert = assert_mtx, 108d272fe53SJohn Baldwin #ifdef DDB 109ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 110d272fe53SJohn Baldwin #endif 1116e21afd4SJohn Baldwin .lc_lock = lock_mtx, 1126e21afd4SJohn Baldwin .lc_unlock = unlock_mtx, 113a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 114a5aedd68SStacey Son .lc_owner = owner_mtx, 115a5aedd68SStacey Son #endif 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, 120f9721b43SAttilio Rao .lc_assert = assert_mtx, 121d272fe53SJohn Baldwin #ifdef DDB 122ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 123d272fe53SJohn Baldwin #endif 1246e21afd4SJohn Baldwin .lc_lock = lock_spin, 1256e21afd4SJohn Baldwin .lc_unlock = unlock_spin, 126a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 127a5aedd68SStacey Son .lc_owner = owner_mtx, 128a5aedd68SStacey Son #endif 1298484de75SJohn Baldwin }; 1308484de75SJohn Baldwin 1319ed346baSBosko Milekic /* 132c53c013bSJohn Baldwin * System-wide mutexes 133c53c013bSJohn Baldwin */ 1342502c107SJeff Roberson struct mtx blocked_lock; 135c53c013bSJohn Baldwin struct mtx Giant; 136c53c013bSJohn Baldwin 13767784314SPoul-Henning Kamp void 138*d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what) 139f9721b43SAttilio Rao { 140f9721b43SAttilio Rao 141*d576deedSPawel Jakub Dawidek mtx_assert((const struct mtx *)lock, what); 142f9721b43SAttilio Rao } 143f9721b43SAttilio Rao 14467784314SPoul-Henning Kamp void 1456e21afd4SJohn Baldwin lock_mtx(struct lock_object *lock, int how) 1466e21afd4SJohn Baldwin { 1476e21afd4SJohn Baldwin 1486e21afd4SJohn Baldwin mtx_lock((struct mtx *)lock); 1496e21afd4SJohn Baldwin } 1506e21afd4SJohn Baldwin 15167784314SPoul-Henning Kamp void 1526e21afd4SJohn Baldwin lock_spin(struct lock_object *lock, int how) 1536e21afd4SJohn Baldwin { 1546e21afd4SJohn Baldwin 1556e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 1566e21afd4SJohn Baldwin } 1576e21afd4SJohn Baldwin 15867784314SPoul-Henning Kamp int 1596e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock) 1606e21afd4SJohn Baldwin { 1616e21afd4SJohn Baldwin struct mtx *m; 1626e21afd4SJohn Baldwin 1636e21afd4SJohn Baldwin m = (struct mtx *)lock; 1646e21afd4SJohn Baldwin mtx_assert(m, MA_OWNED | MA_NOTRECURSED); 1656e21afd4SJohn Baldwin mtx_unlock(m); 1666e21afd4SJohn Baldwin return (0); 1676e21afd4SJohn Baldwin } 1686e21afd4SJohn Baldwin 16967784314SPoul-Henning Kamp int 1706e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock) 1716e21afd4SJohn Baldwin { 1726e21afd4SJohn Baldwin 1736e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 1746e21afd4SJohn Baldwin } 1756e21afd4SJohn Baldwin 176a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 177a5aedd68SStacey Son int 178*d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner) 179a5aedd68SStacey Son { 180*d576deedSPawel Jakub Dawidek const struct mtx *m = (const struct mtx *)lock; 181a5aedd68SStacey Son 182a5aedd68SStacey Son *owner = mtx_owner(m); 183a5aedd68SStacey Son return (mtx_unowned(m) == 0); 184a5aedd68SStacey Son } 185a5aedd68SStacey Son #endif 186a5aedd68SStacey Son 1870cde2e34SJason Evans /* 1886283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 1896283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 1906283b7d0SJohn Baldwin */ 1916283b7d0SJohn Baldwin void 1926283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 1936283b7d0SJohn Baldwin { 1946283b7d0SJohn Baldwin 195dde96c99SJohn Baldwin MPASS(curthread != NULL); 196186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 197186abbd7SJohn Baldwin ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); 198aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 199aa89d8cdSJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2000d975d63SJohn Baldwin file, line)); 201aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 20241313430SJohn Baldwin file, line, NULL); 2037c0435b9SKip Macy 204961135eaSJohn Baldwin __mtx_lock(m, curthread, opts, file, line); 205aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 206dde96c99SJohn Baldwin line); 207aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 208764e4d54SJohn Baldwin curthread->td_locks++; 2096283b7d0SJohn Baldwin } 2106283b7d0SJohn Baldwin 2116283b7d0SJohn Baldwin void 2126283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 2136283b7d0SJohn Baldwin { 214dde96c99SJohn Baldwin MPASS(curthread != NULL); 215186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 216186abbd7SJohn Baldwin ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); 217aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 218aa89d8cdSJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2190d975d63SJohn Baldwin file, line)); 220764e4d54SJohn Baldwin curthread->td_locks--; 221aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 222aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 2230d975d63SJohn Baldwin line); 22421377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 225c66d7606SKip Macy 22670fe8436SKip Macy if (m->mtx_recurse == 0) 227a5aedd68SStacey Son LOCKSTAT_PROFILE_RELEASE_LOCK(LS_MTX_UNLOCK_RELEASE, m); 228961135eaSJohn Baldwin __mtx_unlock(m, curthread, opts, file, line); 2296283b7d0SJohn Baldwin } 2306283b7d0SJohn Baldwin 2316283b7d0SJohn Baldwin void 2326283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 2336283b7d0SJohn Baldwin { 2346283b7d0SJohn Baldwin 235dde96c99SJohn Baldwin MPASS(curthread != NULL); 236186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 237186abbd7SJohn Baldwin ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); 238aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 2390d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 240aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 241ad69e26bSJohn Baldwin if (mtx_owned(m)) 242ad69e26bSJohn Baldwin KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 243ad69e26bSJohn Baldwin ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n", 244ad69e26bSJohn Baldwin m->lock_object.lo_name, file, line)); 245aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 24641313430SJohn Baldwin file, line, NULL); 247961135eaSJohn Baldwin __mtx_lock_spin(m, curthread, opts, file, line); 248aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 249dde96c99SJohn Baldwin line); 250aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 2516283b7d0SJohn Baldwin } 2526283b7d0SJohn Baldwin 2536283b7d0SJohn Baldwin void 2546283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 2556283b7d0SJohn Baldwin { 256c66d7606SKip Macy 257dde96c99SJohn Baldwin MPASS(curthread != NULL); 258186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 259186abbd7SJohn Baldwin ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); 260aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 2610d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 262aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 263aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 264aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 265dde96c99SJohn Baldwin line); 2660d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 267c66d7606SKip Macy 268961135eaSJohn Baldwin __mtx_unlock_spin(m); 2696283b7d0SJohn Baldwin } 2706283b7d0SJohn Baldwin 2716283b7d0SJohn Baldwin /* 2729ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 273eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 274eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 2750cde2e34SJason Evans */ 2760cde2e34SJason Evans int 2779ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 2780cde2e34SJason Evans { 2791723a064SJeff Roberson #ifdef LOCK_PROFILING 2807c0435b9SKip Macy uint64_t waittime = 0; 2811723a064SJeff Roberson int contested = 0; 2821723a064SJeff Roberson #endif 2831723a064SJeff Roberson int rval; 2840cde2e34SJason Evans 285b40ce416SJulian Elischer MPASS(curthread != NULL); 286186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 287186abbd7SJohn Baldwin ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); 288aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 289aa89d8cdSJohn Baldwin ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 29083cece6fSJohn Baldwin file, line)); 2919ed346baSBosko Milekic 292aa89d8cdSJohn Baldwin if (mtx_owned(m) && (m->lock_object.lo_flags & LO_RECURSABLE) != 0) { 293eac09796SJohn Baldwin m->mtx_recurse++; 294eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 295eac09796SJohn Baldwin rval = 1; 296eac09796SJohn Baldwin } else 297961135eaSJohn Baldwin rval = _mtx_obtain_lock(m, (uintptr_t)curthread); 2989ed346baSBosko Milekic 299aa89d8cdSJohn Baldwin LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line); 300764e4d54SJohn Baldwin if (rval) { 301aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 3022d96f0b1SJohn Baldwin file, line); 303764e4d54SJohn Baldwin curthread->td_locks++; 304fe68a916SKip Macy if (m->mtx_recurse == 0) 305a5aedd68SStacey Son LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_LOCK_ACQUIRE, 306a5aedd68SStacey Son m, contested, waittime, file, line); 3077c0435b9SKip Macy 308764e4d54SJohn Baldwin } 3099ed346baSBosko Milekic 31019284646SJohn Baldwin return (rval); 3110cde2e34SJason Evans } 3120cde2e34SJason Evans 3130cde2e34SJason Evans /* 3149ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 3159ed346baSBosko Milekic * 3169ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 3179ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 3180cde2e34SJason Evans */ 3190cde2e34SJason Evans void 320122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file, 321bdcfcf5bSJohn Baldwin int line) 32236412d79SJohn Baldwin { 3232502c107SJeff Roberson struct turnstile *ts; 3241723a064SJeff Roberson uintptr_t v; 325cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES 32676447e56SJohn Baldwin volatile struct thread *owner; 3272498cf8cSJohn Baldwin #endif 32802bd1bcdSIan Dowse #ifdef KTR 32902bd1bcdSIan Dowse int cont_logged = 0; 33002bd1bcdSIan Dowse #endif 3311723a064SJeff Roberson #ifdef LOCK_PROFILING 33270fe8436SKip Macy int contested = 0; 33370fe8436SKip Macy uint64_t waittime = 0; 3341723a064SJeff Roberson #endif 335a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 336a5aedd68SStacey Son uint64_t spin_cnt = 0; 337a5aedd68SStacey Son uint64_t sleep_cnt = 0; 338a5aedd68SStacey Son int64_t sleep_time = 0; 339a5aedd68SStacey Son #endif 34036412d79SJohn Baldwin 3415fa8dd90SJohn Baldwin if (mtx_owned(m)) { 342aa89d8cdSJohn Baldwin KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 343eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 344aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 34536412d79SJohn Baldwin m->mtx_recurse++; 34608812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 347aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 3485746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 34936412d79SJohn Baldwin return; 35036412d79SJohn Baldwin } 3519ed346baSBosko Milekic 35270fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, 35370fe8436SKip Macy &contested, &waittime); 354aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 35515ec816aSJohn Baldwin CTR4(KTR_LOCK, 35615ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 357aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)m->mtx_lock, file, line); 3581bd0eefbSJohn Baldwin 359961135eaSJohn Baldwin while (!_mtx_obtain_lock(m, tid)) { 360a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 361a5aedd68SStacey Son spin_cnt++; 362a5aedd68SStacey Son #endif 36349aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 36449aead8aSAttilio Rao /* 36549aead8aSAttilio Rao * If the owner is running on another CPU, spin until the 36649aead8aSAttilio Rao * owner stops running or the state of the lock changes. 36749aead8aSAttilio Rao */ 36849aead8aSAttilio Rao v = m->mtx_lock; 36949aead8aSAttilio Rao if (v != MTX_UNOWNED) { 37049aead8aSAttilio Rao owner = (struct thread *)(v & ~MTX_FLAGMASK); 37149aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 37249aead8aSAttilio Rao if (LOCK_LOG_TEST(&m->lock_object, 0)) 37349aead8aSAttilio Rao CTR3(KTR_LOCK, 37449aead8aSAttilio Rao "%s: spinning on %p held by %p", 37549aead8aSAttilio Rao __func__, m, owner); 37649aead8aSAttilio Rao while (mtx_owner(m) == owner && 377a5aedd68SStacey Son TD_IS_RUNNING(owner)) { 37849aead8aSAttilio Rao cpu_spinwait(); 379a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 380a5aedd68SStacey Son spin_cnt++; 381a5aedd68SStacey Son #endif 382a5aedd68SStacey Son } 38349aead8aSAttilio Rao continue; 38449aead8aSAttilio Rao } 38549aead8aSAttilio Rao } 38649aead8aSAttilio Rao #endif 38749aead8aSAttilio Rao 3882502c107SJeff Roberson ts = turnstile_trywait(&m->lock_object); 3895fa8dd90SJohn Baldwin v = m->mtx_lock; 3905fa8dd90SJohn Baldwin 39136412d79SJohn Baldwin /* 3929ed346baSBosko Milekic * Check if the lock has been released while spinning for 393961a7b24SJohn Baldwin * the turnstile chain lock. 39436412d79SJohn Baldwin */ 3955fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 3962502c107SJeff Roberson turnstile_cancel(ts); 39736412d79SJohn Baldwin continue; 39836412d79SJohn Baldwin } 3999ed346baSBosko Milekic 40049aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 40149aead8aSAttilio Rao /* 402fa29f023SJohn Baldwin * The current lock owner might have started executing 403fa29f023SJohn Baldwin * on another CPU (or the lock could have changed 404fa29f023SJohn Baldwin * owners) while we were waiting on the turnstile 405fa29f023SJohn Baldwin * chain lock. If so, drop the turnstile lock and try 406fa29f023SJohn Baldwin * again. 40749aead8aSAttilio Rao */ 40849aead8aSAttilio Rao owner = (struct thread *)(v & ~MTX_FLAGMASK); 40949aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 41049aead8aSAttilio Rao turnstile_cancel(ts); 41149aead8aSAttilio Rao continue; 41249aead8aSAttilio Rao } 41349aead8aSAttilio Rao #endif 41449aead8aSAttilio Rao 41536412d79SJohn Baldwin /* 4169ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 4179ed346baSBosko Milekic * setting the contested bit, the mutex was either released 4189ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 41936412d79SJohn Baldwin */ 42036412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 421122eceefSJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 4222502c107SJeff Roberson turnstile_cancel(ts); 42336412d79SJohn Baldwin continue; 42436412d79SJohn Baldwin } 42536412d79SJohn Baldwin 4269ed346baSBosko Milekic /* 4277feefcd6SJohn Baldwin * We definitely must sleep for this lock. 4289ed346baSBosko Milekic */ 42936412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 43036412d79SJohn Baldwin 43102bd1bcdSIan Dowse #ifdef KTR 43202bd1bcdSIan Dowse if (!cont_logged) { 43302bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 43402bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 435aa89d8cdSJohn Baldwin (void *)tid, file, line, m->lock_object.lo_name, 436aa89d8cdSJohn Baldwin WITNESS_FILE(&m->lock_object), 437aa89d8cdSJohn Baldwin WITNESS_LINE(&m->lock_object)); 43802bd1bcdSIan Dowse cont_logged = 1; 43902bd1bcdSIan Dowse } 44002bd1bcdSIan Dowse #endif 44136412d79SJohn Baldwin 4429ed346baSBosko Milekic /* 443961a7b24SJohn Baldwin * Block on the turnstile. 4449ed346baSBosko Milekic */ 445a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 446a5aedd68SStacey Son sleep_time -= lockstat_nsecs(); 447a5aedd68SStacey Son #endif 4482502c107SJeff Roberson turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE); 449a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 450a5aedd68SStacey Son sleep_time += lockstat_nsecs(); 451a5aedd68SStacey Son sleep_cnt++; 452a5aedd68SStacey Son #endif 45336412d79SJohn Baldwin } 45402bd1bcdSIan Dowse #ifdef KTR 45502bd1bcdSIan Dowse if (cont_logged) { 45602bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 45702bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 458aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)tid, file, line); 45902bd1bcdSIan Dowse } 46002bd1bcdSIan Dowse #endif 461a5aedd68SStacey Son LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_LOCK_ACQUIRE, m, contested, 462eea4f254SJeff Roberson waittime, file, line); 463a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 464a5aedd68SStacey Son if (sleep_time) 465a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_MTX_LOCK_BLOCK, m, sleep_time); 466a5aedd68SStacey Son 467a5aedd68SStacey Son /* 468a5aedd68SStacey Son * Only record the loops spinning and not sleeping. 469a5aedd68SStacey Son */ 470a5aedd68SStacey Son if (spin_cnt > sleep_cnt) 471a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_MTX_LOCK_SPIN, m, (spin_cnt - sleep_cnt)); 472a5aedd68SStacey Son #endif 4739ed346baSBosko Milekic } 4749ed346baSBosko Milekic 4752502c107SJeff Roberson static void 4762502c107SJeff Roberson _mtx_lock_spin_failed(struct mtx *m) 4772502c107SJeff Roberson { 4782502c107SJeff Roberson struct thread *td; 4792502c107SJeff Roberson 4802502c107SJeff Roberson td = mtx_owner(m); 4812502c107SJeff Roberson 4822502c107SJeff Roberson /* If the mutex is unlocked, try again. */ 4832502c107SJeff Roberson if (td == NULL) 4842502c107SJeff Roberson return; 485b95b98b0SKonstantin Belousov 4862502c107SJeff Roberson printf( "spin lock %p (%s) held by %p (tid %d) too long\n", 4872502c107SJeff Roberson m, m->lock_object.lo_name, td, td->td_tid); 4882502c107SJeff Roberson #ifdef WITNESS 48998332c8cSAttilio Rao witness_display_spinlock(&m->lock_object, td, printf); 4902502c107SJeff Roberson #endif 4912502c107SJeff Roberson panic("spin lock held too long"); 4922502c107SJeff Roberson } 4932502c107SJeff Roberson 494b95b98b0SKonstantin Belousov #ifdef SMP 4959ed346baSBosko Milekic /* 4969ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 4979ed346baSBosko Milekic * 4989ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 4999ed346baSBosko Milekic * is handled inline. 5009ed346baSBosko Milekic */ 5019ed346baSBosko Milekic void 502122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file, 503bdcfcf5bSJohn Baldwin int line) 50436412d79SJohn Baldwin { 5051723a064SJeff Roberson int i = 0; 5061723a064SJeff Roberson #ifdef LOCK_PROFILING 5071723a064SJeff Roberson int contested = 0; 50870fe8436SKip Macy uint64_t waittime = 0; 5091723a064SJeff Roberson #endif 51036412d79SJohn Baldwin 511aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 5125746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 5139ed346baSBosko Milekic 51470fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 515961135eaSJohn Baldwin while (!_mtx_obtain_lock(m, tid)) { 5169ed346baSBosko Milekic 5177141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 518c6a37e84SJohn Baldwin spinlock_exit(); 51936412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 520703fc290SJohn Baldwin if (i++ < 10000000) { 5219f1b87f1SMaxime Henrion cpu_spinwait(); 52236412d79SJohn Baldwin continue; 523703fc290SJohn Baldwin } 5240fa2168bSJohn Baldwin if (i < 60000000 || kdb_active || panicstr != NULL) 52536412d79SJohn Baldwin DELAY(1); 5262502c107SJeff Roberson else 5272502c107SJeff Roberson _mtx_lock_spin_failed(m); 5289f1b87f1SMaxime Henrion cpu_spinwait(); 52936412d79SJohn Baldwin } 530c6a37e84SJohn Baldwin spinlock_enter(); 53136412d79SJohn Baldwin } 53236412d79SJohn Baldwin 533aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 5349ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 5359ed346baSBosko Milekic 536a5aedd68SStacey Son LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_SPIN_LOCK_ACQUIRE, m, 537a5aedd68SStacey Son contested, waittime, (file), (line)); 538a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_MTX_SPIN_LOCK_SPIN, m, i); 53936412d79SJohn Baldwin } 54033fb8a38SJohn Baldwin #endif /* SMP */ 54136412d79SJohn Baldwin 5422502c107SJeff Roberson void 5432502c107SJeff Roberson _thread_lock_flags(struct thread *td, int opts, const char *file, int line) 5442502c107SJeff Roberson { 5452502c107SJeff Roberson struct mtx *m; 5462502c107SJeff Roberson uintptr_t tid; 5471723a064SJeff Roberson int i; 5481723a064SJeff Roberson #ifdef LOCK_PROFILING 5491723a064SJeff Roberson int contested = 0; 5501723a064SJeff Roberson uint64_t waittime = 0; 5511723a064SJeff Roberson #endif 552a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 553a5aedd68SStacey Son uint64_t spin_cnt = 0; 554a5aedd68SStacey Son #endif 5552502c107SJeff Roberson 5561723a064SJeff Roberson i = 0; 5572502c107SJeff Roberson tid = (uintptr_t)curthread; 5582502c107SJeff Roberson for (;;) { 5592502c107SJeff Roberson retry: 5602502c107SJeff Roberson spinlock_enter(); 561710eacdcSJeff Roberson m = td->td_lock; 56213c85a48SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 56313c85a48SJohn Baldwin ("thread_lock() of destroyed mutex @ %s:%d", file, line)); 56413c85a48SJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 56513c85a48SJohn Baldwin ("thread_lock() of sleep mutex %s @ %s:%d", 56613c85a48SJohn Baldwin m->lock_object.lo_name, file, line)); 567ad69e26bSJohn Baldwin if (mtx_owned(m)) 568ad69e26bSJohn Baldwin KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 569ad69e26bSJohn Baldwin ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n", 570ad69e26bSJohn Baldwin m->lock_object.lo_name, file, line)); 5712502c107SJeff Roberson WITNESS_CHECKORDER(&m->lock_object, 57241313430SJohn Baldwin opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 573961135eaSJohn Baldwin while (!_mtx_obtain_lock(m, tid)) { 574a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 575a5aedd68SStacey Son spin_cnt++; 576a5aedd68SStacey Son #endif 5772502c107SJeff Roberson if (m->mtx_lock == tid) { 5782502c107SJeff Roberson m->mtx_recurse++; 5792502c107SJeff Roberson break; 5802502c107SJeff Roberson } 581eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&m->lock_object, 582eea4f254SJeff Roberson &contested, &waittime); 5832502c107SJeff Roberson /* Give interrupts a chance while we spin. */ 5842502c107SJeff Roberson spinlock_exit(); 5852502c107SJeff Roberson while (m->mtx_lock != MTX_UNOWNED) { 5862502c107SJeff Roberson if (i++ < 10000000) 5872502c107SJeff Roberson cpu_spinwait(); 5882502c107SJeff Roberson else if (i < 60000000 || 5892502c107SJeff Roberson kdb_active || panicstr != NULL) 5902502c107SJeff Roberson DELAY(1); 5912502c107SJeff Roberson else 5922502c107SJeff Roberson _mtx_lock_spin_failed(m); 5932502c107SJeff Roberson cpu_spinwait(); 5942502c107SJeff Roberson if (m != td->td_lock) 5952502c107SJeff Roberson goto retry; 5962502c107SJeff Roberson } 5972502c107SJeff Roberson spinlock_enter(); 5982502c107SJeff Roberson } 5992502c107SJeff Roberson if (m == td->td_lock) 6002502c107SJeff Roberson break; 601961135eaSJohn Baldwin __mtx_unlock_spin(m); /* does spinlock_exit() */ 602a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 603a5aedd68SStacey Son spin_cnt++; 604a5aedd68SStacey Son #endif 6052502c107SJeff Roberson } 606eea4f254SJeff Roberson if (m->mtx_recurse == 0) 607a5aedd68SStacey Son LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_SPIN_LOCK_ACQUIRE, 608a5aedd68SStacey Son m, contested, waittime, (file), (line)); 60913c85a48SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 61013c85a48SJohn Baldwin line); 6112502c107SJeff Roberson WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 612a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_THREAD_LOCK_SPIN, m, spin_cnt); 6132502c107SJeff Roberson } 6142502c107SJeff Roberson 6152502c107SJeff Roberson struct mtx * 6162502c107SJeff Roberson thread_lock_block(struct thread *td) 6172502c107SJeff Roberson { 6182502c107SJeff Roberson struct mtx *lock; 6192502c107SJeff Roberson 6202502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 621710eacdcSJeff Roberson lock = td->td_lock; 6222502c107SJeff Roberson td->td_lock = &blocked_lock; 6232502c107SJeff Roberson mtx_unlock_spin(lock); 6242502c107SJeff Roberson 6252502c107SJeff Roberson return (lock); 6262502c107SJeff Roberson } 6272502c107SJeff Roberson 6282502c107SJeff Roberson void 6292502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new) 6302502c107SJeff Roberson { 6312502c107SJeff Roberson mtx_assert(new, MA_OWNED); 6322502c107SJeff Roberson MPASS(td->td_lock == &blocked_lock); 63365d32cd8SMatt Jacob atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new); 6342502c107SJeff Roberson } 6352502c107SJeff Roberson 6362502c107SJeff Roberson void 6372502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new) 6382502c107SJeff Roberson { 6392502c107SJeff Roberson struct mtx *lock; 6402502c107SJeff Roberson 6412502c107SJeff Roberson mtx_assert(new, MA_OWNED); 6422502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 643710eacdcSJeff Roberson lock = td->td_lock; 6442502c107SJeff Roberson td->td_lock = new; 6452502c107SJeff Roberson mtx_unlock_spin(lock); 6462502c107SJeff Roberson } 6472502c107SJeff Roberson 6489ed346baSBosko Milekic /* 6499ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 6509ed346baSBosko Milekic * 6519ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 6529ed346baSBosko Milekic * need to wake up a blocked thread). 6539ed346baSBosko Milekic */ 65436412d79SJohn Baldwin void 6559ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 65636412d79SJohn Baldwin { 657961a7b24SJohn Baldwin struct turnstile *ts; 6589ed346baSBosko Milekic 65908812b39SBosko Milekic if (mtx_recursed(m)) { 66036412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 66108812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 662aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 6639ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 66436412d79SJohn Baldwin return; 66536412d79SJohn Baldwin } 6669ed346baSBosko Milekic 6672502c107SJeff Roberson /* 6682502c107SJeff Roberson * We have to lock the chain before the turnstile so this turnstile 6692502c107SJeff Roberson * can be removed from the hash list if it is empty. 6702502c107SJeff Roberson */ 6712502c107SJeff Roberson turnstile_chain_lock(&m->lock_object); 672aa89d8cdSJohn Baldwin ts = turnstile_lookup(&m->lock_object); 673aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 6749ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 675961a7b24SJohn Baldwin MPASS(ts != NULL); 6767aa4f685SJohn Baldwin turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 677961135eaSJohn Baldwin _mtx_release_lock_quick(m); 678bf9c6c31SJohn Baldwin 6792502c107SJeff Roberson /* 6802502c107SJeff Roberson * This turnstile is now no longer associated with the mutex. We can 6812502c107SJeff Roberson * unlock the chain lock so a new turnstile may take it's place. 6822502c107SJeff Roberson */ 6837aa4f685SJohn Baldwin turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 6842502c107SJeff Roberson turnstile_chain_unlock(&m->lock_object); 6859ed346baSBosko Milekic } 6869ed346baSBosko Milekic 6879ed346baSBosko Milekic /* 6889ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 689961135eaSJohn Baldwin * See the __mtx_unlock_spin() macro for the details. 6909ed346baSBosko Milekic */ 6919ed346baSBosko Milekic 6929ed346baSBosko Milekic /* 69315ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 6949ed346baSBosko Milekic */ 6951103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 6960cde2e34SJason Evans void 697*d576deedSPawel Jakub Dawidek _mtx_assert(const struct mtx *m, int what, const char *file, int line) 6980cde2e34SJason Evans { 6995cb0fbe4SJohn Baldwin 7001126349aSPaul Saab if (panicstr != NULL || dumping) 7015cb0fbe4SJohn Baldwin return; 702a10f4966SJake Burkholder switch (what) { 7030cde2e34SJason Evans case MA_OWNED: 7040cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 7050cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 706a10f4966SJake Burkholder if (!mtx_owned(m)) 7070cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 708aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 709a10f4966SJake Burkholder if (mtx_recursed(m)) { 710a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 7110cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 712aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 713a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 7140cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 715aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 7160cde2e34SJason Evans } 7170cde2e34SJason Evans break; 7180cde2e34SJason Evans case MA_NOTOWNED: 719a10f4966SJake Burkholder if (mtx_owned(m)) 7200cde2e34SJason Evans panic("mutex %s owned at %s:%d", 721aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 7220cde2e34SJason Evans break; 7230cde2e34SJason Evans default: 72456771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 7250cde2e34SJason Evans } 7260cde2e34SJason Evans } 7270cde2e34SJason Evans #endif 7280cde2e34SJason Evans 7299ed346baSBosko Milekic /* 7309ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 73119284646SJohn Baldwin * 73219284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 73319284646SJohn Baldwin * maintained by the witness code. 7349ed346baSBosko Milekic */ 73536412d79SJohn Baldwin #ifdef MUTEX_DEBUG 73636412d79SJohn Baldwin 7374d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 73836412d79SJohn Baldwin 73919284646SJohn Baldwin void 74019284646SJohn Baldwin mtx_validate(struct mtx *m) 74136412d79SJohn Baldwin { 74236412d79SJohn Baldwin 74336412d79SJohn Baldwin /* 744fa669ab7SPoul-Henning Kamp * XXX: When kernacc() does not require Giant we can reenable this check 745fa669ab7SPoul-Henning Kamp */ 746fa669ab7SPoul-Henning Kamp #ifdef notyet 747fa669ab7SPoul-Henning Kamp /* 74876dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 74976dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 75076dcbd6fSBosko Milekic * requires Giant itself. 75176dcbd6fSBosko Milekic */ 752ab07087eSBosko Milekic if (!cold) 753ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 754ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 75519284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 75636412d79SJohn Baldwin #endif 75736412d79SJohn Baldwin } 75836412d79SJohn Baldwin #endif 75936412d79SJohn Baldwin 7609ed346baSBosko Milekic /* 761c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 762c27b5699SAndrew R. Reiter */ 763c27b5699SAndrew R. Reiter void 764c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 765c27b5699SAndrew R. Reiter { 766c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 767c27b5699SAndrew R. Reiter 7680c88508aSJohn Baldwin mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 769c27b5699SAndrew R. Reiter } 770c27b5699SAndrew R. Reiter 771c27b5699SAndrew R. Reiter /* 7729ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 7730c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 7740c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 7750c88508aSJohn Baldwin * witness. 7769ed346baSBosko Milekic */ 77736412d79SJohn Baldwin void 7780c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts) 77936412d79SJohn Baldwin { 78083a81bcbSJohn Baldwin struct lock_class *class; 78183a81bcbSJohn Baldwin int flags; 7829ed346baSBosko Milekic 78319284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 7847c0435b9SKip Macy MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0); 785353998acSAttilio Rao ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock, 786353998acSAttilio Rao ("%s: mtx_lock not aligned for %s: %p", __func__, name, 787353998acSAttilio Rao &m->mtx_lock)); 7889ed346baSBosko Milekic 78936412d79SJohn Baldwin #ifdef MUTEX_DEBUG 7909ed346baSBosko Milekic /* Diagnostic and error correction */ 79119284646SJohn Baldwin mtx_validate(m); 7926936206eSJohn Baldwin #endif 79336412d79SJohn Baldwin 79483a81bcbSJohn Baldwin /* Determine lock class and lock flags. */ 79519284646SJohn Baldwin if (opts & MTX_SPIN) 79683a81bcbSJohn Baldwin class = &lock_class_mtx_spin; 79719284646SJohn Baldwin else 79883a81bcbSJohn Baldwin class = &lock_class_mtx_sleep; 79983a81bcbSJohn Baldwin flags = 0; 80019284646SJohn Baldwin if (opts & MTX_QUIET) 80183a81bcbSJohn Baldwin flags |= LO_QUIET; 80219284646SJohn Baldwin if (opts & MTX_RECURSE) 80383a81bcbSJohn Baldwin flags |= LO_RECURSABLE; 80419284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 80583a81bcbSJohn Baldwin flags |= LO_WITNESS; 806f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 80783a81bcbSJohn Baldwin flags |= LO_DUPOK; 8087c0435b9SKip Macy if (opts & MTX_NOPROFILE) 8097c0435b9SKip Macy flags |= LO_NOPROFILE; 81019284646SJohn Baldwin 81183a81bcbSJohn Baldwin /* Initialize mutex. */ 81219284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 81383a81bcbSJohn Baldwin m->mtx_recurse = 0; 8149ed346baSBosko Milekic 815aa89d8cdSJohn Baldwin lock_init(&m->lock_object, class, name, type, flags); 81636412d79SJohn Baldwin } 81736412d79SJohn Baldwin 8189ed346baSBosko Milekic /* 81919284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 82019284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 82119284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 82219284646SJohn Baldwin * flags. 8239ed346baSBosko Milekic */ 82436412d79SJohn Baldwin void 82536412d79SJohn Baldwin mtx_destroy(struct mtx *m) 82636412d79SJohn Baldwin { 82736412d79SJohn Baldwin 82819284646SJohn Baldwin if (!mtx_owned(m)) 82919284646SJohn Baldwin MPASS(mtx_unowned(m)); 83019284646SJohn Baldwin else { 83108812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 8329ed346baSBosko Milekic 833861a2308SScott Long /* Perform the non-mtx related part of mtx_unlock_spin(). */ 834aa89d8cdSJohn Baldwin if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) 835861a2308SScott Long spinlock_exit(); 836764e4d54SJohn Baldwin else 837764e4d54SJohn Baldwin curthread->td_locks--; 838861a2308SScott Long 839d3df4af3SJeff Roberson lock_profile_release_lock(&m->lock_object); 84019284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 841aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__, 842c86b6ff5SJohn Baldwin __LINE__); 84336412d79SJohn Baldwin } 8440384fff8SJason Evans 845186abbd7SJohn Baldwin m->mtx_lock = MTX_DESTROYED; 846aa89d8cdSJohn Baldwin lock_destroy(&m->lock_object); 8470384fff8SJason Evans } 848d23f5958SMatthew Dillon 849d23f5958SMatthew Dillon /* 850c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 851c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 852c53c013bSJohn Baldwin * setup before this is called. 853c53c013bSJohn Baldwin */ 854c53c013bSJohn Baldwin void 855c53c013bSJohn Baldwin mutex_init(void) 856c53c013bSJohn Baldwin { 857c53c013bSJohn Baldwin 858961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 859961a7b24SJohn Baldwin init_turnstiles(); 860961a7b24SJohn Baldwin 861c53c013bSJohn Baldwin /* 862c53c013bSJohn Baldwin * Initialize mutexes. 863c53c013bSJohn Baldwin */ 8640c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 8652502c107SJeff Roberson mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN); 8662502c107SJeff Roberson blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */ 8670c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 8682502c107SJeff Roberson mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE); 8698c4b6380SJohn Baldwin mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 870c53c013bSJohn Baldwin mtx_lock(&Giant); 871c53c013bSJohn Baldwin } 872d272fe53SJohn Baldwin 873d272fe53SJohn Baldwin #ifdef DDB 874d272fe53SJohn Baldwin void 875*d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock) 876d272fe53SJohn Baldwin { 877d272fe53SJohn Baldwin struct thread *td; 878*d576deedSPawel Jakub Dawidek const struct mtx *m; 879d272fe53SJohn Baldwin 880*d576deedSPawel Jakub Dawidek m = (const struct mtx *)lock; 881d272fe53SJohn Baldwin 882d272fe53SJohn Baldwin db_printf(" flags: {"); 88383a81bcbSJohn Baldwin if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 884d272fe53SJohn Baldwin db_printf("SPIN"); 885d272fe53SJohn Baldwin else 886d272fe53SJohn Baldwin db_printf("DEF"); 887aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_RECURSABLE) 888d272fe53SJohn Baldwin db_printf(", RECURSE"); 889aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_DUPOK) 890d272fe53SJohn Baldwin db_printf(", DUPOK"); 891d272fe53SJohn Baldwin db_printf("}\n"); 892d272fe53SJohn Baldwin db_printf(" state: {"); 893d272fe53SJohn Baldwin if (mtx_unowned(m)) 894d272fe53SJohn Baldwin db_printf("UNOWNED"); 895c0bfd703SJohn Baldwin else if (mtx_destroyed(m)) 896c0bfd703SJohn Baldwin db_printf("DESTROYED"); 897d272fe53SJohn Baldwin else { 898d272fe53SJohn Baldwin db_printf("OWNED"); 899d272fe53SJohn Baldwin if (m->mtx_lock & MTX_CONTESTED) 900d272fe53SJohn Baldwin db_printf(", CONTESTED"); 901d272fe53SJohn Baldwin if (m->mtx_lock & MTX_RECURSED) 902d272fe53SJohn Baldwin db_printf(", RECURSED"); 903d272fe53SJohn Baldwin } 904d272fe53SJohn Baldwin db_printf("}\n"); 905c0bfd703SJohn Baldwin if (!mtx_unowned(m) && !mtx_destroyed(m)) { 906d272fe53SJohn Baldwin td = mtx_owner(m); 907d272fe53SJohn Baldwin db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 908431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 909d272fe53SJohn Baldwin if (mtx_recursed(m)) 910d272fe53SJohn Baldwin db_printf(" recursed: %d\n", m->mtx_recurse); 911d272fe53SJohn Baldwin } 912d272fe53SJohn Baldwin } 913d272fe53SJohn Baldwin #endif 914