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 84b9a80acaSStephan Uphoff /* 859ed346baSBosko Milekic * Internal utility macros. 860cde2e34SJason Evans */ 879ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 880cde2e34SJason Evans 8949b94bfcSJohn Baldwin #define mtx_owner(m) ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK)) 909ed346baSBosko Milekic 91d272fe53SJohn Baldwin #ifdef DDB 92d272fe53SJohn Baldwin static void db_show_mtx(struct lock_object *lock); 93d272fe53SJohn Baldwin #endif 94d272fe53SJohn Baldwin 950cde2e34SJason Evans /* 9619284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 970cde2e34SJason Evans */ 9819284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 9919284646SJohn Baldwin "sleep mutex", 100d272fe53SJohn Baldwin LC_SLEEPLOCK | LC_RECURSABLE, 101d272fe53SJohn Baldwin #ifdef DDB 102d272fe53SJohn Baldwin db_show_mtx 103d272fe53SJohn Baldwin #endif 10419284646SJohn Baldwin }; 10519284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 10619284646SJohn Baldwin "spin mutex", 107d272fe53SJohn Baldwin LC_SPINLOCK | LC_RECURSABLE, 108d272fe53SJohn Baldwin #ifdef DDB 109d272fe53SJohn Baldwin db_show_mtx 110d272fe53SJohn Baldwin #endif 1118484de75SJohn Baldwin }; 1128484de75SJohn Baldwin 1139ed346baSBosko Milekic /* 114c53c013bSJohn Baldwin * System-wide mutexes 115c53c013bSJohn Baldwin */ 116c53c013bSJohn Baldwin struct mtx sched_lock; 117c53c013bSJohn Baldwin struct mtx Giant; 118c53c013bSJohn Baldwin 1190cde2e34SJason Evans /* 1206283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 1216283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 1226283b7d0SJohn Baldwin */ 1236283b7d0SJohn Baldwin void 1246283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 1256283b7d0SJohn Baldwin { 1267c0435b9SKip Macy uint64_t waittime; 1276283b7d0SJohn Baldwin 128dde96c99SJohn Baldwin MPASS(curthread != NULL); 129186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 130186abbd7SJohn Baldwin ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); 13183a81bcbSJohn Baldwin KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep, 1320d975d63SJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 1330d975d63SJohn Baldwin file, line)); 1348d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 1358d768e76SJohn Baldwin file, line); 1367c0435b9SKip Macy 1377c0435b9SKip Macy lock_profile_waitstart(&waittime); 138dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 139dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 140dde96c99SJohn Baldwin line); 141dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 142764e4d54SJohn Baldwin curthread->td_locks++; 1437c0435b9SKip Macy lock_profile_obtain_lock_success(&m->mtx_object, waittime, file, line); 1446283b7d0SJohn Baldwin } 1456283b7d0SJohn Baldwin 1466283b7d0SJohn Baldwin void 1476283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 1486283b7d0SJohn Baldwin { 1496283b7d0SJohn Baldwin 150dde96c99SJohn Baldwin MPASS(curthread != NULL); 151186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 152186abbd7SJohn Baldwin ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); 15383a81bcbSJohn Baldwin KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep, 1540d975d63SJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 1550d975d63SJohn Baldwin file, line)); 156764e4d54SJohn Baldwin curthread->td_locks--; 1570d975d63SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 1580d975d63SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 1590d975d63SJohn Baldwin line); 16021377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 1616c35e809SDag-Erling Smørgrav 1627c0435b9SKip Macy lock_profile_release_lock(&m->mtx_object); 163dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 1646283b7d0SJohn Baldwin } 1656283b7d0SJohn Baldwin 1666283b7d0SJohn Baldwin void 1676283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 1686283b7d0SJohn Baldwin { 1696283b7d0SJohn Baldwin 1707c0435b9SKip Macy uint64_t waittime; 1717c0435b9SKip Macy 172dde96c99SJohn Baldwin MPASS(curthread != NULL); 173186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 174186abbd7SJohn Baldwin ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); 17583a81bcbSJohn Baldwin KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin, 1760d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 1770d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 1788d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 1798d768e76SJohn Baldwin file, line); 1807c0435b9SKip Macy lock_profile_waitstart(&waittime); 181dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 182dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 183dde96c99SJohn Baldwin line); 184dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 1857c0435b9SKip Macy lock_profile_obtain_lock_success(&m->mtx_object, waittime, file, line); 1866283b7d0SJohn Baldwin } 1876283b7d0SJohn Baldwin 1886283b7d0SJohn Baldwin void 1896283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 1906283b7d0SJohn Baldwin { 1916283b7d0SJohn Baldwin 192dde96c99SJohn Baldwin MPASS(curthread != NULL); 193186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 194186abbd7SJohn Baldwin ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); 19583a81bcbSJohn Baldwin KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin, 1960d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 1970d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 198dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 199dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 200dde96c99SJohn Baldwin line); 2010d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 2027c0435b9SKip Macy lock_profile_release_lock(&m->mtx_object); 203dde96c99SJohn Baldwin _rel_spin_lock(m); 2046283b7d0SJohn Baldwin } 2056283b7d0SJohn Baldwin 2066283b7d0SJohn Baldwin /* 2079ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 208eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 209eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 2100cde2e34SJason Evans */ 2110cde2e34SJason Evans int 2129ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 2130cde2e34SJason Evans { 2140cde2e34SJason Evans int rval; 2157c0435b9SKip Macy uint64_t waittime = 0; 2160cde2e34SJason Evans 217b40ce416SJulian Elischer MPASS(curthread != NULL); 218186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 219186abbd7SJohn Baldwin ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); 22083a81bcbSJohn Baldwin KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep, 22183cece6fSJohn Baldwin ("mtx_trylock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 22283cece6fSJohn Baldwin file, line)); 2239ed346baSBosko Milekic 224eac09796SJohn Baldwin if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) { 225eac09796SJohn Baldwin m->mtx_recurse++; 226eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 227eac09796SJohn Baldwin rval = 1; 228eac09796SJohn Baldwin } else 229122eceefSJohn Baldwin rval = _obtain_lock(m, (uintptr_t)curthread); 2309ed346baSBosko Milekic 23119284646SJohn Baldwin LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 232764e4d54SJohn Baldwin if (rval) { 2332d96f0b1SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 2342d96f0b1SJohn Baldwin file, line); 235764e4d54SJohn Baldwin curthread->td_locks++; 2367c0435b9SKip Macy lock_profile_obtain_lock_success(&m->mtx_object, waittime, file, line); 2377c0435b9SKip Macy 238764e4d54SJohn Baldwin } 2399ed346baSBosko Milekic 24019284646SJohn Baldwin return (rval); 2410cde2e34SJason Evans } 2420cde2e34SJason Evans 2430cde2e34SJason Evans /* 2449ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 2459ed346baSBosko Milekic * 2469ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 2479ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 2480cde2e34SJason Evans */ 2490cde2e34SJason Evans void 250122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file, 251bdcfcf5bSJohn Baldwin int line) 25236412d79SJohn Baldwin { 253701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 25476447e56SJohn Baldwin volatile struct thread *owner; 2552498cf8cSJohn Baldwin #endif 25602bd1bcdSIan Dowse #ifdef KTR 25702bd1bcdSIan Dowse int cont_logged = 0; 25802bd1bcdSIan Dowse #endif 2597c0435b9SKip Macy uintptr_t v; 2608dc10be8SRobert Watson int contested; 26136412d79SJohn Baldwin 2625fa8dd90SJohn Baldwin if (mtx_owned(m)) { 263eac09796SJohn Baldwin KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0, 264eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 265eac09796SJohn Baldwin m->mtx_object.lo_name, file, line)); 26636412d79SJohn Baldwin m->mtx_recurse++; 26708812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 26819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 2695746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 27036412d79SJohn Baldwin return; 27136412d79SJohn Baldwin } 2729ed346baSBosko Milekic 27319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 27415ec816aSJohn Baldwin CTR4(KTR_LOCK, 27515ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 27619284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 2771bd0eefbSJohn Baldwin 278122eceefSJohn Baldwin while (!_obtain_lock(m, tid)) { 2797c0435b9SKip Macy lock_profile_obtain_lock_failed(&m->mtx_object, &contested); 2802ff0e645SJohn Baldwin turnstile_lock(&m->mtx_object); 2815fa8dd90SJohn Baldwin v = m->mtx_lock; 2825fa8dd90SJohn Baldwin 28336412d79SJohn Baldwin /* 2849ed346baSBosko Milekic * Check if the lock has been released while spinning for 285961a7b24SJohn Baldwin * the turnstile chain lock. 28636412d79SJohn Baldwin */ 2875fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 288961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 2899f1b87f1SMaxime Henrion cpu_spinwait(); 29036412d79SJohn Baldwin continue; 29136412d79SJohn Baldwin } 2929ed346baSBosko Milekic 293535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 294535eb309SJohn Baldwin MPASS(v != MTX_CONTESTED); 295535eb309SJohn Baldwin #else 29636412d79SJohn Baldwin /* 2979ed346baSBosko Milekic * The mutex was marked contested on release. This means that 298f7ee1590SJohn Baldwin * there are other threads blocked on it. Grab ownership of 299f7ee1590SJohn Baldwin * it and propagate its priority to the current thread if 300f7ee1590SJohn Baldwin * necessary. 30136412d79SJohn Baldwin */ 30236412d79SJohn Baldwin if (v == MTX_CONTESTED) { 303122eceefSJohn Baldwin m->mtx_lock = tid | MTX_CONTESTED; 3042ff0e645SJohn Baldwin turnstile_claim(&m->mtx_object); 3058dc10be8SRobert Watson break; 30636412d79SJohn Baldwin } 307535eb309SJohn Baldwin #endif 3089ed346baSBosko Milekic 30936412d79SJohn Baldwin /* 3109ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 3119ed346baSBosko Milekic * setting the contested bit, the mutex was either released 3129ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 31336412d79SJohn Baldwin */ 31436412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 315122eceefSJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 316961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 3179f1b87f1SMaxime Henrion cpu_spinwait(); 31836412d79SJohn Baldwin continue; 31936412d79SJohn Baldwin } 32036412d79SJohn Baldwin 321701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 3222498cf8cSJohn Baldwin /* 3232498cf8cSJohn Baldwin * If the current owner of the lock is executing on another 3242498cf8cSJohn Baldwin * CPU, spin instead of blocking. 3252498cf8cSJohn Baldwin */ 32649b94bfcSJohn Baldwin owner = (struct thread *)(v & ~MTX_FLAGMASK); 327a9abdce4SRobert Watson #ifdef ADAPTIVE_GIANT 328a9abdce4SRobert Watson if (TD_IS_RUNNING(owner)) { 329a9abdce4SRobert Watson #else 33027dad03cSJohn Baldwin if (m != &Giant && TD_IS_RUNNING(owner)) { 331a9abdce4SRobert Watson #endif 332961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 33327dad03cSJohn Baldwin while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) { 3349f1b87f1SMaxime Henrion cpu_spinwait(); 3357fcca609SJohn Baldwin } 3362498cf8cSJohn Baldwin continue; 3372498cf8cSJohn Baldwin } 338701f1408SScott Long #endif /* SMP && !NO_ADAPTIVE_MUTEXES */ 3392498cf8cSJohn Baldwin 3409ed346baSBosko Milekic /* 3417feefcd6SJohn Baldwin * We definitely must sleep for this lock. 3429ed346baSBosko Milekic */ 34336412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 34436412d79SJohn Baldwin 34502bd1bcdSIan Dowse #ifdef KTR 34602bd1bcdSIan Dowse if (!cont_logged) { 34702bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 34802bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 349122eceefSJohn Baldwin (void *)tid, file, line, m->mtx_object.lo_name, 35002bd1bcdSIan Dowse WITNESS_FILE(&m->mtx_object), 35102bd1bcdSIan Dowse WITNESS_LINE(&m->mtx_object)); 35202bd1bcdSIan Dowse cont_logged = 1; 35302bd1bcdSIan Dowse } 35402bd1bcdSIan Dowse #endif 35536412d79SJohn Baldwin 3569ed346baSBosko Milekic /* 357961a7b24SJohn Baldwin * Block on the turnstile. 3589ed346baSBosko Milekic */ 3597aa4f685SJohn Baldwin turnstile_wait(&m->mtx_object, mtx_owner(m), 3607aa4f685SJohn Baldwin TS_EXCLUSIVE_QUEUE); 36136412d79SJohn Baldwin } 3629ed346baSBosko Milekic 36302bd1bcdSIan Dowse #ifdef KTR 36402bd1bcdSIan Dowse if (cont_logged) { 36502bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 36602bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 367122eceefSJohn Baldwin m->mtx_object.lo_name, (void *)tid, file, line); 36802bd1bcdSIan Dowse } 36902bd1bcdSIan Dowse #endif 3707c0435b9SKip Macy #ifdef LOCK_PROFILING 3718dc10be8SRobert Watson if (contested) 3727c0435b9SKip Macy m->mtx_object.lo_profile_obj.lpo_contest_locking++; 3737c0435b9SKip Macy m->mtx_object.lo_profile_obj.lpo_contest_holding = 0; 3748dc10be8SRobert Watson #endif 37536412d79SJohn Baldwin return; 3769ed346baSBosko Milekic } 3779ed346baSBosko Milekic 37833fb8a38SJohn Baldwin #ifdef SMP 3799ed346baSBosko Milekic /* 3809ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 3819ed346baSBosko Milekic * 3829ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 3839ed346baSBosko Milekic * is handled inline. 3849ed346baSBosko Milekic */ 3859ed346baSBosko Milekic void 386122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file, 387bdcfcf5bSJohn Baldwin int line) 38836412d79SJohn Baldwin { 3890fa2168bSJohn Baldwin struct thread *td; 3907c0435b9SKip Macy int contested, i = 0; 39136412d79SJohn Baldwin 39219284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 3935746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 3949ed346baSBosko Milekic 395f781b5a4SJohn Baldwin while (!_obtain_lock(m, tid)) { 3967c0435b9SKip Macy lock_profile_obtain_lock_failed(&m->mtx_object, &contested); 3979ed346baSBosko Milekic 3987141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 399c6a37e84SJohn Baldwin spinlock_exit(); 40036412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 401703fc290SJohn Baldwin if (i++ < 10000000) { 4029f1b87f1SMaxime Henrion cpu_spinwait(); 40336412d79SJohn Baldwin continue; 404703fc290SJohn Baldwin } 4050fa2168bSJohn Baldwin if (i < 60000000 || kdb_active || panicstr != NULL) 40636412d79SJohn Baldwin DELAY(1); 4070fa2168bSJohn Baldwin else { 4080fa2168bSJohn Baldwin td = mtx_owner(m); 4090fa2168bSJohn Baldwin 4100fa2168bSJohn Baldwin /* If the mutex is unlocked, try again. */ 4110fa2168bSJohn Baldwin if (td == NULL) 4120fa2168bSJohn Baldwin continue; 4130fa2168bSJohn Baldwin printf( 4140fa2168bSJohn Baldwin "spin lock %p (%s) held by %p (tid %d) too long\n", 4150fa2168bSJohn Baldwin m, m->mtx_object.lo_name, td, td->td_tid); 41641109518SJohn Baldwin #ifdef WITNESS 4170fa2168bSJohn Baldwin witness_display_spinlock(&m->mtx_object, td); 41841109518SJohn Baldwin #endif 41941109518SJohn Baldwin panic("spin lock held too long"); 42041109518SJohn Baldwin } 4219f1b87f1SMaxime Henrion cpu_spinwait(); 42236412d79SJohn Baldwin } 423c6a37e84SJohn Baldwin spinlock_enter(); 42436412d79SJohn Baldwin } 42536412d79SJohn Baldwin 42619284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4279ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 4289ed346baSBosko Milekic 42936412d79SJohn Baldwin return; 43036412d79SJohn Baldwin } 43133fb8a38SJohn Baldwin #endif /* SMP */ 43236412d79SJohn Baldwin 4339ed346baSBosko Milekic /* 4349ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 4359ed346baSBosko Milekic * 4369ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 4379ed346baSBosko Milekic * need to wake up a blocked thread). 4389ed346baSBosko Milekic */ 43936412d79SJohn Baldwin void 4409ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 44136412d79SJohn Baldwin { 442961a7b24SJohn Baldwin struct turnstile *ts; 4430c0b25aeSJohn Baldwin #ifndef PREEMPTION 444b40ce416SJulian Elischer struct thread *td, *td1; 4450c0b25aeSJohn Baldwin #endif 4469ed346baSBosko Milekic 44708812b39SBosko Milekic if (mtx_recursed(m)) { 44836412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 44908812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 45019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4519ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 45236412d79SJohn Baldwin return; 45336412d79SJohn Baldwin } 4549ed346baSBosko Milekic 4552ff0e645SJohn Baldwin turnstile_lock(&m->mtx_object); 456961a7b24SJohn Baldwin ts = turnstile_lookup(&m->mtx_object); 45719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4589ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 4599ed346baSBosko Milekic 460ece2d989SPawel Jakub Dawidek #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 461961a7b24SJohn Baldwin if (ts == NULL) { 4622498cf8cSJohn Baldwin _release_lock_quick(m); 4632498cf8cSJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4642498cf8cSJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m); 465961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 4662498cf8cSJohn Baldwin return; 4672498cf8cSJohn Baldwin } 468961a7b24SJohn Baldwin #else 469961a7b24SJohn Baldwin MPASS(ts != NULL); 4702498cf8cSJohn Baldwin #endif 4710c0b25aeSJohn Baldwin #ifndef PREEMPTION 472961a7b24SJohn Baldwin /* XXX */ 4737aa4f685SJohn Baldwin td1 = turnstile_head(ts, TS_EXCLUSIVE_QUEUE); 4740c0b25aeSJohn Baldwin #endif 475535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 4767aa4f685SJohn Baldwin turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 477535eb309SJohn Baldwin _release_lock_quick(m); 478535eb309SJohn Baldwin #else 4797aa4f685SJohn Baldwin if (turnstile_signal(ts, TS_EXCLUSIVE_QUEUE)) { 48036412d79SJohn Baldwin _release_lock_quick(m); 48119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4829ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 483961a7b24SJohn Baldwin } else { 484f7ee1590SJohn Baldwin m->mtx_lock = MTX_CONTESTED; 48519284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 486961a7b24SJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested", 487961a7b24SJohn Baldwin m); 488e0817317SJulian Elischer } 489535eb309SJohn Baldwin #endif 4907aa4f685SJohn Baldwin turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 4919ed346baSBosko Milekic 4920c0b25aeSJohn Baldwin #ifndef PREEMPTION 493961a7b24SJohn Baldwin /* 494961a7b24SJohn Baldwin * XXX: This is just a hack until preemption is done. However, 495961a7b24SJohn Baldwin * once preemption is done we need to either wrap the 496961a7b24SJohn Baldwin * turnstile_signal() and release of the actual lock in an 497961a7b24SJohn Baldwin * extra critical section or change the preemption code to 498961a7b24SJohn Baldwin * always just set a flag and never do instant-preempts. 499961a7b24SJohn Baldwin */ 500961a7b24SJohn Baldwin td = curthread; 501961a7b24SJohn Baldwin if (td->td_critnest > 0 || td1->td_priority >= td->td_priority) 502961a7b24SJohn Baldwin return; 503961a7b24SJohn Baldwin mtx_lock_spin(&sched_lock); 504961a7b24SJohn Baldwin if (!TD_IS_RUNNING(td1)) { 50536412d79SJohn Baldwin #ifdef notyet 506b40ce416SJulian Elischer if (td->td_ithd != NULL) { 507b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 50836412d79SJohn Baldwin 50936412d79SJohn Baldwin if (it->it_interrupted) { 51019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 51136412d79SJohn Baldwin CTR2(KTR_LOCK, 51215ec816aSJohn Baldwin "_mtx_unlock_sleep: %p interrupted %p", 51336412d79SJohn Baldwin it, it->it_interrupted); 51436412d79SJohn Baldwin intr_thd_fixup(it); 51536412d79SJohn Baldwin } 51636412d79SJohn Baldwin } 51736412d79SJohn Baldwin #endif 51819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 519562e4ffeSJohn Baldwin CTR2(KTR_LOCK, 5209ed346baSBosko Milekic "_mtx_unlock_sleep: %p switching out lock=%p", m, 5219ed346baSBosko Milekic (void *)m->mtx_lock); 5229ed346baSBosko Milekic 523bf0acc27SJohn Baldwin mi_switch(SW_INVOL, NULL); 52419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 5259ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 52631271627SJohn Baldwin m, (void *)m->mtx_lock); 52736412d79SJohn Baldwin } 5289ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 5290c0b25aeSJohn Baldwin #endif 5309ed346baSBosko Milekic 5319ed346baSBosko Milekic return; 5329ed346baSBosko Milekic } 5339ed346baSBosko Milekic 5349ed346baSBosko Milekic /* 5359ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 5369ed346baSBosko Milekic * See the _rel_spin_lock() macro for the details. 5379ed346baSBosko Milekic */ 5389ed346baSBosko Milekic 5399ed346baSBosko Milekic /* 54015ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 5419ed346baSBosko Milekic */ 5421103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 5430cde2e34SJason Evans void 54456771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line) 5450cde2e34SJason Evans { 5465cb0fbe4SJohn Baldwin 5471126349aSPaul Saab if (panicstr != NULL || dumping) 5485cb0fbe4SJohn Baldwin return; 549a10f4966SJake Burkholder switch (what) { 5500cde2e34SJason Evans case MA_OWNED: 5510cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 5520cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 553a10f4966SJake Burkholder if (!mtx_owned(m)) 5540cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 55519284646SJohn Baldwin m->mtx_object.lo_name, file, line); 556a10f4966SJake Burkholder if (mtx_recursed(m)) { 557a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 5580cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 55919284646SJohn Baldwin m->mtx_object.lo_name, file, line); 560a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 5610cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 56219284646SJohn Baldwin m->mtx_object.lo_name, file, line); 5630cde2e34SJason Evans } 5640cde2e34SJason Evans break; 5650cde2e34SJason Evans case MA_NOTOWNED: 566a10f4966SJake Burkholder if (mtx_owned(m)) 5670cde2e34SJason Evans panic("mutex %s owned at %s:%d", 56819284646SJohn Baldwin m->mtx_object.lo_name, file, line); 5690cde2e34SJason Evans break; 5700cde2e34SJason Evans default: 57156771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 5720cde2e34SJason Evans } 5730cde2e34SJason Evans } 5740cde2e34SJason Evans #endif 5750cde2e34SJason Evans 5769ed346baSBosko Milekic /* 5779ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 57819284646SJohn Baldwin * 57919284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 58019284646SJohn Baldwin * maintained by the witness code. 5819ed346baSBosko Milekic */ 58236412d79SJohn Baldwin #ifdef MUTEX_DEBUG 58336412d79SJohn Baldwin 5844d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 58536412d79SJohn Baldwin 58619284646SJohn Baldwin void 58719284646SJohn Baldwin mtx_validate(struct mtx *m) 58836412d79SJohn Baldwin { 58936412d79SJohn Baldwin 59036412d79SJohn Baldwin /* 591fa669ab7SPoul-Henning Kamp * XXX: When kernacc() does not require Giant we can reenable this check 592fa669ab7SPoul-Henning Kamp */ 593fa669ab7SPoul-Henning Kamp #ifdef notyet 594fa669ab7SPoul-Henning Kamp /* 59576dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 59676dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 59776dcbd6fSBosko Milekic * requires Giant itself. 59876dcbd6fSBosko Milekic */ 599ab07087eSBosko Milekic if (!cold) 600ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 601ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 60219284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 60336412d79SJohn Baldwin #endif 60436412d79SJohn Baldwin } 60536412d79SJohn Baldwin #endif 60636412d79SJohn Baldwin 6079ed346baSBosko Milekic /* 608c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 609c27b5699SAndrew R. Reiter */ 610c27b5699SAndrew R. Reiter void 611c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 612c27b5699SAndrew R. Reiter { 613c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 614c27b5699SAndrew R. Reiter 6150c88508aSJohn Baldwin mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 616c27b5699SAndrew R. Reiter } 617c27b5699SAndrew R. Reiter 618c27b5699SAndrew R. Reiter /* 6199ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 6200c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 6210c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 6220c88508aSJohn Baldwin * witness. 6239ed346baSBosko Milekic */ 62436412d79SJohn Baldwin void 6250c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts) 62636412d79SJohn Baldwin { 62783a81bcbSJohn Baldwin struct lock_class *class; 62883a81bcbSJohn Baldwin int flags; 6299ed346baSBosko Milekic 63019284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 6317c0435b9SKip Macy MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0); 6329ed346baSBosko Milekic 63336412d79SJohn Baldwin #ifdef MUTEX_DEBUG 6349ed346baSBosko Milekic /* Diagnostic and error correction */ 63519284646SJohn Baldwin mtx_validate(m); 6366936206eSJohn Baldwin #endif 63736412d79SJohn Baldwin 63883a81bcbSJohn Baldwin /* Determine lock class and lock flags. */ 63919284646SJohn Baldwin if (opts & MTX_SPIN) 64083a81bcbSJohn Baldwin class = &lock_class_mtx_spin; 64119284646SJohn Baldwin else 64283a81bcbSJohn Baldwin class = &lock_class_mtx_sleep; 64383a81bcbSJohn Baldwin flags = 0; 64419284646SJohn Baldwin if (opts & MTX_QUIET) 64583a81bcbSJohn Baldwin flags |= LO_QUIET; 64619284646SJohn Baldwin if (opts & MTX_RECURSE) 64783a81bcbSJohn Baldwin flags |= LO_RECURSABLE; 64819284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 64983a81bcbSJohn Baldwin flags |= LO_WITNESS; 650f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 65183a81bcbSJohn Baldwin flags |= LO_DUPOK; 6527c0435b9SKip Macy if (opts & MTX_NOPROFILE) 6537c0435b9SKip Macy flags |= LO_NOPROFILE; 65419284646SJohn Baldwin 65583a81bcbSJohn Baldwin /* Initialize mutex. */ 65619284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 65783a81bcbSJohn Baldwin m->mtx_recurse = 0; 6589ed346baSBosko Milekic 6597c0435b9SKip Macy lock_profile_object_init(&m->mtx_object, name); 66083a81bcbSJohn Baldwin lock_init(&m->mtx_object, class, name, type, flags); 66136412d79SJohn Baldwin } 66236412d79SJohn Baldwin 6639ed346baSBosko Milekic /* 66419284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 66519284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 66619284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 66719284646SJohn Baldwin * flags. 6689ed346baSBosko Milekic */ 66936412d79SJohn Baldwin void 67036412d79SJohn Baldwin mtx_destroy(struct mtx *m) 67136412d79SJohn Baldwin { 67236412d79SJohn Baldwin 67319284646SJohn Baldwin if (!mtx_owned(m)) 67419284646SJohn Baldwin MPASS(mtx_unowned(m)); 67519284646SJohn Baldwin else { 67608812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 6779ed346baSBosko Milekic 678861a2308SScott Long /* Perform the non-mtx related part of mtx_unlock_spin(). */ 67983a81bcbSJohn Baldwin if (LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin) 680861a2308SScott Long spinlock_exit(); 681764e4d54SJohn Baldwin else 682764e4d54SJohn Baldwin curthread->td_locks--; 683861a2308SScott Long 68419284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 685c86b6ff5SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 686c86b6ff5SJohn Baldwin __LINE__); 68736412d79SJohn Baldwin } 6880384fff8SJason Evans 689186abbd7SJohn Baldwin m->mtx_lock = MTX_DESTROYED; 6907c0435b9SKip Macy lock_profile_object_destroy(&m->mtx_object); 69183a81bcbSJohn Baldwin lock_destroy(&m->mtx_object); 6920384fff8SJason Evans } 693d23f5958SMatthew Dillon 694d23f5958SMatthew Dillon /* 695c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 696c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 697c53c013bSJohn Baldwin * setup before this is called. 698c53c013bSJohn Baldwin */ 699c53c013bSJohn Baldwin void 700c53c013bSJohn Baldwin mutex_init(void) 701c53c013bSJohn Baldwin { 702c53c013bSJohn Baldwin 703961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 704961a7b24SJohn Baldwin init_turnstiles(); 705961a7b24SJohn Baldwin 706c53c013bSJohn Baldwin /* 707c53c013bSJohn Baldwin * Initialize mutexes. 708c53c013bSJohn Baldwin */ 7090c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 7100c88508aSJohn Baldwin mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE); 7110c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 7128c4b6380SJohn Baldwin mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 713c53c013bSJohn Baldwin mtx_lock(&Giant); 7147c0435b9SKip Macy 7157c0435b9SKip Macy lock_profile_init(); 716c53c013bSJohn Baldwin } 717d272fe53SJohn Baldwin 718d272fe53SJohn Baldwin #ifdef DDB 719d272fe53SJohn Baldwin void 720d272fe53SJohn Baldwin db_show_mtx(struct lock_object *lock) 721d272fe53SJohn Baldwin { 722d272fe53SJohn Baldwin struct thread *td; 723d272fe53SJohn Baldwin struct mtx *m; 724d272fe53SJohn Baldwin 725d272fe53SJohn Baldwin m = (struct mtx *)lock; 726d272fe53SJohn Baldwin 727d272fe53SJohn Baldwin db_printf(" flags: {"); 72883a81bcbSJohn Baldwin if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 729d272fe53SJohn Baldwin db_printf("SPIN"); 730d272fe53SJohn Baldwin else 731d272fe53SJohn Baldwin db_printf("DEF"); 732d272fe53SJohn Baldwin if (m->mtx_object.lo_flags & LO_RECURSABLE) 733d272fe53SJohn Baldwin db_printf(", RECURSE"); 734d272fe53SJohn Baldwin if (m->mtx_object.lo_flags & LO_DUPOK) 735d272fe53SJohn Baldwin db_printf(", DUPOK"); 736d272fe53SJohn Baldwin db_printf("}\n"); 737d272fe53SJohn Baldwin db_printf(" state: {"); 738d272fe53SJohn Baldwin if (mtx_unowned(m)) 739d272fe53SJohn Baldwin db_printf("UNOWNED"); 740d272fe53SJohn Baldwin else { 741d272fe53SJohn Baldwin db_printf("OWNED"); 742d272fe53SJohn Baldwin if (m->mtx_lock & MTX_CONTESTED) 743d272fe53SJohn Baldwin db_printf(", CONTESTED"); 744d272fe53SJohn Baldwin if (m->mtx_lock & MTX_RECURSED) 745d272fe53SJohn Baldwin db_printf(", RECURSED"); 746d272fe53SJohn Baldwin } 747d272fe53SJohn Baldwin db_printf("}\n"); 748d272fe53SJohn Baldwin if (!mtx_unowned(m)) { 749d272fe53SJohn Baldwin td = mtx_owner(m); 750d272fe53SJohn Baldwin db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 751d272fe53SJohn Baldwin td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm); 752d272fe53SJohn Baldwin if (mtx_recursed(m)) 753d272fe53SJohn Baldwin db_printf(" recursed: %d\n", m->mtx_recurse); 754d272fe53SJohn Baldwin } 755d272fe53SJohn Baldwin } 756d272fe53SJohn Baldwin #endif 757