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" 41f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h" 429923b511SScott Long #include "opt_sched.h" 43a5a96a19SJohn Baldwin 440384fff8SJason Evans #include <sys/param.h> 456c35e809SDag-Erling Smørgrav #include <sys/systm.h> 4636412d79SJohn Baldwin #include <sys/bus.h> 471126349aSPaul Saab #include <sys/conf.h> 482d50560aSMarcel Moolenaar #include <sys/kdb.h> 4936412d79SJohn Baldwin #include <sys/kernel.h> 506c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 5119284646SJohn Baldwin #include <sys/lock.h> 52fb919e4dSMark Murray #include <sys/malloc.h> 5319284646SJohn Baldwin #include <sys/mutex.h> 540384fff8SJason Evans #include <sys/proc.h> 55c4f7a187SJohn Baldwin #include <sys/resourcevar.h> 56b43179fbSJeff Roberson #include <sys/sched.h> 576c35e809SDag-Erling Smørgrav #include <sys/sbuf.h> 581ada9041SMateusz Guzik #include <sys/smp.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 79f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 80f5f9340bSFabien Thomas #include <sys/pmckern.h> 81f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , lock, failed); 82f5f9340bSFabien Thomas #endif 83f5f9340bSFabien Thomas 84b9a80acaSStephan Uphoff /* 857f44c618SAttilio Rao * Return the mutex address when the lock cookie address is provided. 867f44c618SAttilio Rao * This functionality assumes that struct mtx* have a member named mtx_lock. 877f44c618SAttilio Rao */ 887f44c618SAttilio Rao #define mtxlock2mtx(c) (__containerof(c, struct mtx, mtx_lock)) 897f44c618SAttilio Rao 907f44c618SAttilio Rao /* 919ed346baSBosko Milekic * Internal utility macros. 920cde2e34SJason Evans */ 939ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 940cde2e34SJason Evans 95c0bfd703SJohn Baldwin #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED) 96c0bfd703SJohn Baldwin 97d576deedSPawel Jakub Dawidek static void assert_mtx(const struct lock_object *lock, int what); 98d272fe53SJohn Baldwin #ifdef DDB 99d576deedSPawel Jakub Dawidek static void db_show_mtx(const struct lock_object *lock); 100d272fe53SJohn Baldwin #endif 1017faf4d90SDavide Italiano static void lock_mtx(struct lock_object *lock, uintptr_t how); 1027faf4d90SDavide Italiano static void lock_spin(struct lock_object *lock, uintptr_t how); 103a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 104d576deedSPawel Jakub Dawidek static int owner_mtx(const struct lock_object *lock, 105d576deedSPawel Jakub Dawidek struct thread **owner); 106a5aedd68SStacey Son #endif 1077faf4d90SDavide Italiano static uintptr_t unlock_mtx(struct lock_object *lock); 1087faf4d90SDavide Italiano static uintptr_t unlock_spin(struct lock_object *lock); 109d272fe53SJohn Baldwin 1100cde2e34SJason Evans /* 11119284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 1120cde2e34SJason Evans */ 11319284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 114ae8dde30SJohn Baldwin .lc_name = "sleep mutex", 115ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, 116f9721b43SAttilio Rao .lc_assert = assert_mtx, 117d272fe53SJohn Baldwin #ifdef DDB 118ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 119d272fe53SJohn Baldwin #endif 1206e21afd4SJohn Baldwin .lc_lock = lock_mtx, 1216e21afd4SJohn Baldwin .lc_unlock = unlock_mtx, 122a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 123a5aedd68SStacey Son .lc_owner = owner_mtx, 124a5aedd68SStacey Son #endif 12519284646SJohn Baldwin }; 12619284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 127ae8dde30SJohn Baldwin .lc_name = "spin mutex", 128ae8dde30SJohn Baldwin .lc_flags = LC_SPINLOCK | LC_RECURSABLE, 129f9721b43SAttilio Rao .lc_assert = assert_mtx, 130d272fe53SJohn Baldwin #ifdef DDB 131ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 132d272fe53SJohn Baldwin #endif 1336e21afd4SJohn Baldwin .lc_lock = lock_spin, 1346e21afd4SJohn Baldwin .lc_unlock = unlock_spin, 135a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 136a5aedd68SStacey Son .lc_owner = owner_mtx, 137a5aedd68SStacey Son #endif 1388484de75SJohn Baldwin }; 1398484de75SJohn Baldwin 1401ada9041SMateusz Guzik #ifdef ADAPTIVE_MUTEXES 1411ada9041SMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging"); 1421ada9041SMateusz Guzik 1438e5a3e9aSMateusz Guzik static struct lock_delay_config __read_mostly mtx_delay; 1441ada9041SMateusz Guzik 1458e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base, 1461ada9041SMateusz Guzik 0, ""); 1471ada9041SMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max, 1481ada9041SMateusz Guzik 0, ""); 1491ada9041SMateusz Guzik 1508e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay); 1511ada9041SMateusz Guzik #endif 1521ada9041SMateusz Guzik 153a0d45f0fSMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL, 154a0d45f0fSMateusz Guzik "mtx spin debugging"); 155a0d45f0fSMateusz Guzik 1568e5a3e9aSMateusz Guzik static struct lock_delay_config __read_mostly mtx_spin_delay; 157a0d45f0fSMateusz Guzik 1588e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW, 1598e5a3e9aSMateusz Guzik &mtx_spin_delay.base, 0, ""); 1608e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW, 1618e5a3e9aSMateusz Guzik &mtx_spin_delay.max, 0, ""); 162a0d45f0fSMateusz Guzik 1638e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay); 164a0d45f0fSMateusz Guzik 1659ed346baSBosko Milekic /* 166c53c013bSJohn Baldwin * System-wide mutexes 167c53c013bSJohn Baldwin */ 1682502c107SJeff Roberson struct mtx blocked_lock; 169c53c013bSJohn Baldwin struct mtx Giant; 170c53c013bSJohn Baldwin 17167784314SPoul-Henning Kamp void 172d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what) 173f9721b43SAttilio Rao { 174f9721b43SAttilio Rao 175d576deedSPawel Jakub Dawidek mtx_assert((const struct mtx *)lock, what); 176f9721b43SAttilio Rao } 177f9721b43SAttilio Rao 17867784314SPoul-Henning Kamp void 1797faf4d90SDavide Italiano lock_mtx(struct lock_object *lock, uintptr_t how) 1806e21afd4SJohn Baldwin { 1816e21afd4SJohn Baldwin 1826e21afd4SJohn Baldwin mtx_lock((struct mtx *)lock); 1836e21afd4SJohn Baldwin } 1846e21afd4SJohn Baldwin 18567784314SPoul-Henning Kamp void 1867faf4d90SDavide Italiano lock_spin(struct lock_object *lock, uintptr_t how) 1876e21afd4SJohn Baldwin { 1886e21afd4SJohn Baldwin 1896e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 1906e21afd4SJohn Baldwin } 1916e21afd4SJohn Baldwin 1927faf4d90SDavide Italiano uintptr_t 1936e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock) 1946e21afd4SJohn Baldwin { 1956e21afd4SJohn Baldwin struct mtx *m; 1966e21afd4SJohn Baldwin 1976e21afd4SJohn Baldwin m = (struct mtx *)lock; 1986e21afd4SJohn Baldwin mtx_assert(m, MA_OWNED | MA_NOTRECURSED); 1996e21afd4SJohn Baldwin mtx_unlock(m); 2006e21afd4SJohn Baldwin return (0); 2016e21afd4SJohn Baldwin } 2026e21afd4SJohn Baldwin 2037faf4d90SDavide Italiano uintptr_t 2046e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock) 2056e21afd4SJohn Baldwin { 2066e21afd4SJohn Baldwin 2076e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 2086e21afd4SJohn Baldwin } 2096e21afd4SJohn Baldwin 210a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 211a5aedd68SStacey Son int 212d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner) 213a5aedd68SStacey Son { 21402315a67SMark Johnston const struct mtx *m; 21502315a67SMark Johnston uintptr_t x; 216a5aedd68SStacey Son 21702315a67SMark Johnston m = (const struct mtx *)lock; 21802315a67SMark Johnston x = m->mtx_lock; 21902315a67SMark Johnston *owner = (struct thread *)(x & ~MTX_FLAGMASK); 22002315a67SMark Johnston return (x != MTX_UNOWNED); 221a5aedd68SStacey Son } 222a5aedd68SStacey Son #endif 223a5aedd68SStacey Son 2240cde2e34SJason Evans /* 2256283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 2266283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 2276283b7d0SJohn Baldwin */ 2286283b7d0SJohn Baldwin void 2297f44c618SAttilio Rao __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 2306283b7d0SJohn Baldwin { 2317f44c618SAttilio Rao struct mtx *m; 23208da2677SMateusz Guzik uintptr_t tid, v; 2336283b7d0SJohn Baldwin 2347f44c618SAttilio Rao m = mtxlock2mtx(c); 2357f44c618SAttilio Rao 236cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 237e3ae0dfeSAttilio Rao ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d", 238e3ae0dfeSAttilio Rao curthread, m->lock_object.lo_name, file, line)); 239186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 240186abbd7SJohn Baldwin ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); 241aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 242aa89d8cdSJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2430d975d63SJohn Baldwin file, line)); 244ac6b769bSAttilio Rao WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) | 245ac6b769bSAttilio Rao LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 2467c0435b9SKip Macy 24708da2677SMateusz Guzik tid = (uintptr_t)curthread; 24808da2677SMateusz Guzik v = MTX_UNOWNED; 24908da2677SMateusz Guzik if (!_mtx_obtain_lock_fetch(m, &v, tid)) 25008da2677SMateusz Guzik _mtx_lock_sleep(m, v, tid, opts, file, line); 25108da2677SMateusz Guzik else 25208da2677SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, 25308da2677SMateusz Guzik m, 0, 0, file, line); 254aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 255dde96c99SJohn Baldwin line); 256ac6b769bSAttilio Rao WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE, 257ac6b769bSAttilio Rao file, line); 258ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 2596283b7d0SJohn Baldwin } 2606283b7d0SJohn Baldwin 2616283b7d0SJohn Baldwin void 2627f44c618SAttilio Rao __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 2636283b7d0SJohn Baldwin { 2647f44c618SAttilio Rao struct mtx *m; 26535370593SAndriy Gapon 2667f44c618SAttilio Rao m = mtxlock2mtx(c); 2677f44c618SAttilio Rao 268186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 269186abbd7SJohn Baldwin ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); 270aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 271aa89d8cdSJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2720d975d63SJohn Baldwin file, line)); 273aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 274aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 2750d975d63SJohn Baldwin line); 27621377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 277c66d7606SKip Macy 278ffd5c94cSMateusz Guzik #ifdef LOCK_PROFILING 27908da2677SMateusz Guzik __mtx_unlock_sleep(c, opts, file, line); 280ffd5c94cSMateusz Guzik #else 281ffd5c94cSMateusz Guzik __mtx_unlock(m, curthread, opts, file, line); 282ffd5c94cSMateusz Guzik #endif 283ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 2846283b7d0SJohn Baldwin } 2856283b7d0SJohn Baldwin 2866283b7d0SJohn Baldwin void 2877f44c618SAttilio Rao __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 2887f44c618SAttilio Rao int line) 2896283b7d0SJohn Baldwin { 2907f44c618SAttilio Rao struct mtx *m; 2916283b7d0SJohn Baldwin 29235370593SAndriy Gapon if (SCHEDULER_STOPPED()) 29335370593SAndriy Gapon return; 2947f44c618SAttilio Rao 2957f44c618SAttilio Rao m = mtxlock2mtx(c); 2967f44c618SAttilio Rao 297186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 298186abbd7SJohn Baldwin ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); 299aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 3000d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 301aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 302ad69e26bSJohn Baldwin if (mtx_owned(m)) 303ac6b769bSAttilio Rao KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 304ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0, 305ad69e26bSJohn Baldwin ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n", 306ad69e26bSJohn Baldwin m->lock_object.lo_name, file, line)); 307ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 308aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 30941313430SJohn Baldwin file, line, NULL); 310961135eaSJohn Baldwin __mtx_lock_spin(m, curthread, opts, file, line); 311aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 312dde96c99SJohn Baldwin line); 313aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 3146283b7d0SJohn Baldwin } 3156283b7d0SJohn Baldwin 31690b581f2SKonstantin Belousov int 31790b581f2SKonstantin Belousov __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 31890b581f2SKonstantin Belousov int line) 31990b581f2SKonstantin Belousov { 32090b581f2SKonstantin Belousov struct mtx *m; 32190b581f2SKonstantin Belousov 32290b581f2SKonstantin Belousov if (SCHEDULER_STOPPED()) 32390b581f2SKonstantin Belousov return (1); 32490b581f2SKonstantin Belousov 32590b581f2SKonstantin Belousov m = mtxlock2mtx(c); 32690b581f2SKonstantin Belousov 32790b581f2SKonstantin Belousov KASSERT(m->mtx_lock != MTX_DESTROYED, 32890b581f2SKonstantin Belousov ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line)); 32990b581f2SKonstantin Belousov KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 33090b581f2SKonstantin Belousov ("mtx_trylock_spin() of sleep mutex %s @ %s:%d", 33190b581f2SKonstantin Belousov m->lock_object.lo_name, file, line)); 33290b581f2SKonstantin Belousov KASSERT((opts & MTX_RECURSE) == 0, 33390b581f2SKonstantin Belousov ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n", 33490b581f2SKonstantin Belousov m->lock_object.lo_name, file, line)); 33590b581f2SKonstantin Belousov if (__mtx_trylock_spin(m, curthread, opts, file, line)) { 33690b581f2SKonstantin Belousov LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line); 33790b581f2SKonstantin Belousov WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 33890b581f2SKonstantin Belousov return (1); 33990b581f2SKonstantin Belousov } 34090b581f2SKonstantin Belousov LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line); 34190b581f2SKonstantin Belousov return (0); 34290b581f2SKonstantin Belousov } 34390b581f2SKonstantin Belousov 3446283b7d0SJohn Baldwin void 3457f44c618SAttilio Rao __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 3467f44c618SAttilio Rao int line) 3476283b7d0SJohn Baldwin { 3487f44c618SAttilio Rao struct mtx *m; 349c66d7606SKip Macy 35035370593SAndriy Gapon if (SCHEDULER_STOPPED()) 35135370593SAndriy Gapon return; 3527f44c618SAttilio Rao 3537f44c618SAttilio Rao m = mtxlock2mtx(c); 3547f44c618SAttilio Rao 355186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 356186abbd7SJohn Baldwin ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); 357aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 3580d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 359aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 360aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 361aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 362dde96c99SJohn Baldwin line); 3630d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 364c66d7606SKip Macy 365961135eaSJohn Baldwin __mtx_unlock_spin(m); 3666283b7d0SJohn Baldwin } 3676283b7d0SJohn Baldwin 3686283b7d0SJohn Baldwin /* 3699ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 370eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 371eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 3720cde2e34SJason Evans */ 3730cde2e34SJason Evans int 3747f44c618SAttilio Rao _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line) 3750cde2e34SJason Evans { 3767f44c618SAttilio Rao struct mtx *m; 3775c5df0d9SMateusz Guzik struct thread *td; 3785c5df0d9SMateusz Guzik uintptr_t tid, v; 3791723a064SJeff Roberson #ifdef LOCK_PROFILING 3807c0435b9SKip Macy uint64_t waittime = 0; 3811723a064SJeff Roberson int contested = 0; 3821723a064SJeff Roberson #endif 3831723a064SJeff Roberson int rval; 3845c5df0d9SMateusz Guzik bool recursed; 3850cde2e34SJason Evans 3865c5df0d9SMateusz Guzik td = curthread; 3875c5df0d9SMateusz Guzik tid = (uintptr_t)td; 3885c5df0d9SMateusz Guzik if (SCHEDULER_STOPPED_TD(td)) 38935370593SAndriy Gapon return (1); 39035370593SAndriy Gapon 3917f44c618SAttilio Rao m = mtxlock2mtx(c); 3927f44c618SAttilio Rao 393cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 394e3ae0dfeSAttilio Rao ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d", 395e3ae0dfeSAttilio Rao curthread, m->lock_object.lo_name, file, line)); 396186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 397186abbd7SJohn Baldwin ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); 398aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 399aa89d8cdSJohn Baldwin ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 40083cece6fSJohn Baldwin file, line)); 4019ed346baSBosko Milekic 4025c5df0d9SMateusz Guzik rval = 1; 4035c5df0d9SMateusz Guzik recursed = false; 4045c5df0d9SMateusz Guzik v = MTX_UNOWNED; 405b247fd39SMateusz Guzik for (;;) { 406b247fd39SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 407b247fd39SMateusz Guzik break; 408b247fd39SMateusz Guzik if (v == MTX_UNOWNED) 409b247fd39SMateusz Guzik continue; 4105c5df0d9SMateusz Guzik if (v == tid && 4115c5df0d9SMateusz Guzik ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 412ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0)) { 413eac09796SJohn Baldwin m->mtx_recurse++; 414eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 4155c5df0d9SMateusz Guzik recursed = true; 416b247fd39SMateusz Guzik break; 4175c5df0d9SMateusz Guzik } 418b247fd39SMateusz Guzik rval = 0; 419b247fd39SMateusz Guzik break; 4205c5df0d9SMateusz Guzik } 4215c5df0d9SMateusz Guzik 422ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 4239ed346baSBosko Milekic 424aa89d8cdSJohn Baldwin LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line); 425764e4d54SJohn Baldwin if (rval) { 426aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4272d96f0b1SJohn Baldwin file, line); 428ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 4295c5df0d9SMateusz Guzik if (!recursed) 43032cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, 431a5aedd68SStacey Son m, contested, waittime, file, line); 432764e4d54SJohn Baldwin } 4339ed346baSBosko Milekic 43419284646SJohn Baldwin return (rval); 4350cde2e34SJason Evans } 4360cde2e34SJason Evans 4370cde2e34SJason Evans /* 4387f44c618SAttilio Rao * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4399ed346baSBosko Milekic * 4409ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4419ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4420cde2e34SJason Evans */ 44309f1319aSMateusz Guzik #if LOCK_DEBUG > 0 4440cde2e34SJason Evans void 44590836c32SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, uintptr_t tid, int opts, 4467f44c618SAttilio Rao const char *file, int line) 44709f1319aSMateusz Guzik #else 44809f1319aSMateusz Guzik void 449a24c8eb8SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, uintptr_t tid) 45009f1319aSMateusz Guzik #endif 45136412d79SJohn Baldwin { 4527f44c618SAttilio Rao struct mtx *m; 4532502c107SJeff Roberson struct turnstile *ts; 454cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES 45576447e56SJohn Baldwin volatile struct thread *owner; 4562498cf8cSJohn Baldwin #endif 45702bd1bcdSIan Dowse #ifdef KTR 45802bd1bcdSIan Dowse int cont_logged = 0; 45902bd1bcdSIan Dowse #endif 4601723a064SJeff Roberson #ifdef LOCK_PROFILING 46170fe8436SKip Macy int contested = 0; 46270fe8436SKip Macy uint64_t waittime = 0; 4631723a064SJeff Roberson #endif 4641ada9041SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS) 4651ada9041SMateusz Guzik struct lock_delay_arg lda; 4661ada9041SMateusz Guzik #endif 467a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 46861852185SMateusz Guzik u_int sleep_cnt = 0; 469a5aedd68SStacey Son int64_t sleep_time = 0; 470076dd8ebSAndriy Gapon int64_t all_time = 0; 4717640beb9SMateusz Guzik int doing_lockstat; 472a5aedd68SStacey Son #endif 47336412d79SJohn Baldwin 47435370593SAndriy Gapon if (SCHEDULER_STOPPED()) 47535370593SAndriy Gapon return; 47635370593SAndriy Gapon 477fa5000a4SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) 4781ada9041SMateusz Guzik lock_delay_arg_init(&lda, &mtx_delay); 479fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 480fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 4811ada9041SMateusz Guzik #endif 4827f44c618SAttilio Rao m = mtxlock2mtx(c); 483c1aaf63cSMateusz Guzik if (__predict_false(v == MTX_UNOWNED)) 484c1aaf63cSMateusz Guzik v = MTX_READ_VALUE(m); 4857f44c618SAttilio Rao 4862604eb9eSMateusz Guzik if (__predict_false(lv_mtx_owner(v) == (struct thread *)tid)) { 487ac6b769bSAttilio Rao KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 488ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0, 489eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 490aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 491a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0 492ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 493a24c8eb8SMateusz Guzik #endif 49436412d79SJohn Baldwin m->mtx_recurse++; 49508812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 496aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 4975746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 49836412d79SJohn Baldwin return; 49936412d79SJohn Baldwin } 500a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0 501ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 502a24c8eb8SMateusz Guzik #endif 5039ed346baSBosko Milekic 504f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 505f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 506f5f9340bSFabien Thomas #endif 50770fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, 50870fe8436SKip Macy &contested, &waittime); 509aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 51015ec816aSJohn Baldwin CTR4(KTR_LOCK, 51115ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 512aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)m->mtx_lock, file, line); 513076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 51409f1319aSMateusz Guzik #ifdef LOCK_PROFILING 51509f1319aSMateusz Guzik doing_lockstat = 1; 51609f1319aSMateusz Guzik #else 5177640beb9SMateusz Guzik doing_lockstat = lockstat_enabled; 51809f1319aSMateusz Guzik #endif 5197640beb9SMateusz Guzik if (__predict_false(doing_lockstat)) 520e2b25737SMark Johnston all_time -= lockstat_nsecs(&m->lock_object); 521076dd8ebSAndriy Gapon #endif 5221bd0eefbSJohn Baldwin 523fc4f686dSMateusz Guzik for (;;) { 5242604eb9eSMateusz Guzik if (v == MTX_UNOWNED) { 52590836c32SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 526fc4f686dSMateusz Guzik break; 5272604eb9eSMateusz Guzik continue; 5282604eb9eSMateusz Guzik } 529a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 5301ada9041SMateusz Guzik lda.spin_cnt++; 531a5aedd68SStacey Son #endif 53249aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 53349aead8aSAttilio Rao /* 53449aead8aSAttilio Rao * If the owner is running on another CPU, spin until the 53549aead8aSAttilio Rao * owner stops running or the state of the lock changes. 53649aead8aSAttilio Rao */ 5372604eb9eSMateusz Guzik owner = lv_mtx_owner(v); 53849aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 53949aead8aSAttilio Rao if (LOCK_LOG_TEST(&m->lock_object, 0)) 54049aead8aSAttilio Rao CTR3(KTR_LOCK, 54149aead8aSAttilio Rao "%s: spinning on %p held by %p", 54249aead8aSAttilio Rao __func__, m, owner); 5432cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 5442cba8dd3SJohn Baldwin sched_tdname((struct thread *)tid), 5452cba8dd3SJohn Baldwin "spinning", "lockname:\"%s\"", 5462cba8dd3SJohn Baldwin m->lock_object.lo_name); 5472604eb9eSMateusz Guzik do { 5481ada9041SMateusz Guzik lock_delay(&lda); 549391df78aSMateusz Guzik v = MTX_READ_VALUE(m); 5502604eb9eSMateusz Guzik owner = lv_mtx_owner(v); 5512604eb9eSMateusz Guzik } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner)); 5522cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 5532cba8dd3SJohn Baldwin sched_tdname((struct thread *)tid), 5542cba8dd3SJohn Baldwin "running"); 55549aead8aSAttilio Rao continue; 55649aead8aSAttilio Rao } 55749aead8aSAttilio Rao #endif 55849aead8aSAttilio Rao 5592502c107SJeff Roberson ts = turnstile_trywait(&m->lock_object); 5602604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 5615fa8dd90SJohn Baldwin 56236412d79SJohn Baldwin /* 5639ed346baSBosko Milekic * Check if the lock has been released while spinning for 564961a7b24SJohn Baldwin * the turnstile chain lock. 56536412d79SJohn Baldwin */ 5665fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 5672502c107SJeff Roberson turnstile_cancel(ts); 56836412d79SJohn Baldwin continue; 56936412d79SJohn Baldwin } 5709ed346baSBosko Milekic 57149aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 57249aead8aSAttilio Rao /* 573fa29f023SJohn Baldwin * The current lock owner might have started executing 574fa29f023SJohn Baldwin * on another CPU (or the lock could have changed 575fa29f023SJohn Baldwin * owners) while we were waiting on the turnstile 576fa29f023SJohn Baldwin * chain lock. If so, drop the turnstile lock and try 577fa29f023SJohn Baldwin * again. 57849aead8aSAttilio Rao */ 5792604eb9eSMateusz Guzik owner = lv_mtx_owner(v); 58049aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 58149aead8aSAttilio Rao turnstile_cancel(ts); 58249aead8aSAttilio Rao continue; 58349aead8aSAttilio Rao } 58449aead8aSAttilio Rao #endif 58549aead8aSAttilio Rao 58636412d79SJohn Baldwin /* 5879ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 5889ed346baSBosko Milekic * setting the contested bit, the mutex was either released 5899ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 59036412d79SJohn Baldwin */ 59136412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 592122eceefSJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 5932502c107SJeff Roberson turnstile_cancel(ts); 5942604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 59536412d79SJohn Baldwin continue; 59636412d79SJohn Baldwin } 59736412d79SJohn Baldwin 5989ed346baSBosko Milekic /* 5997feefcd6SJohn Baldwin * We definitely must sleep for this lock. 6009ed346baSBosko Milekic */ 60136412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 60236412d79SJohn Baldwin 60302bd1bcdSIan Dowse #ifdef KTR 60402bd1bcdSIan Dowse if (!cont_logged) { 60502bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 60602bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 607aa89d8cdSJohn Baldwin (void *)tid, file, line, m->lock_object.lo_name, 608aa89d8cdSJohn Baldwin WITNESS_FILE(&m->lock_object), 609aa89d8cdSJohn Baldwin WITNESS_LINE(&m->lock_object)); 61002bd1bcdSIan Dowse cont_logged = 1; 61102bd1bcdSIan Dowse } 61202bd1bcdSIan Dowse #endif 61336412d79SJohn Baldwin 6149ed346baSBosko Milekic /* 615961a7b24SJohn Baldwin * Block on the turnstile. 6169ed346baSBosko Milekic */ 617a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 618e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&m->lock_object); 619a5aedd68SStacey Son #endif 6202502c107SJeff Roberson turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE); 621a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 622e2b25737SMark Johnston sleep_time += lockstat_nsecs(&m->lock_object); 623a5aedd68SStacey Son sleep_cnt++; 624a5aedd68SStacey Son #endif 6252604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 62636412d79SJohn Baldwin } 62702bd1bcdSIan Dowse #ifdef KTR 62802bd1bcdSIan Dowse if (cont_logged) { 62902bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 63002bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 631aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)tid, file, line); 63202bd1bcdSIan Dowse } 63302bd1bcdSIan Dowse #endif 6347640beb9SMateusz Guzik #ifdef KDTRACE_HOOKS 6357640beb9SMateusz Guzik if (__predict_true(!doing_lockstat)) 6367640beb9SMateusz Guzik return; 6377640beb9SMateusz Guzik all_time += lockstat_nsecs(&m->lock_object); 6387640beb9SMateusz Guzik #endif 63932cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested, 640eea4f254SJeff Roberson waittime, file, line); 641a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 642a5aedd68SStacey Son if (sleep_time) 64332cd0147SMark Johnston LOCKSTAT_RECORD1(adaptive__block, m, sleep_time); 644a5aedd68SStacey Son 645a5aedd68SStacey Son /* 646a5aedd68SStacey Son * Only record the loops spinning and not sleeping. 647a5aedd68SStacey Son */ 6481ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 64932cd0147SMark Johnston LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time); 650a5aedd68SStacey Son #endif 6519ed346baSBosko Milekic } 6529ed346baSBosko Milekic 6532502c107SJeff Roberson static void 6542502c107SJeff Roberson _mtx_lock_spin_failed(struct mtx *m) 6552502c107SJeff Roberson { 6562502c107SJeff Roberson struct thread *td; 6572502c107SJeff Roberson 6582502c107SJeff Roberson td = mtx_owner(m); 6592502c107SJeff Roberson 6602502c107SJeff Roberson /* If the mutex is unlocked, try again. */ 6612502c107SJeff Roberson if (td == NULL) 6622502c107SJeff Roberson return; 663b95b98b0SKonstantin Belousov 6642502c107SJeff Roberson printf( "spin lock %p (%s) held by %p (tid %d) too long\n", 6652502c107SJeff Roberson m, m->lock_object.lo_name, td, td->td_tid); 6662502c107SJeff Roberson #ifdef WITNESS 66798332c8cSAttilio Rao witness_display_spinlock(&m->lock_object, td, printf); 6682502c107SJeff Roberson #endif 6692502c107SJeff Roberson panic("spin lock held too long"); 6702502c107SJeff Roberson } 6712502c107SJeff Roberson 672b95b98b0SKonstantin Belousov #ifdef SMP 6739ed346baSBosko Milekic /* 6747f44c618SAttilio Rao * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock. 6759ed346baSBosko Milekic * 6769ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 6779ed346baSBosko Milekic * is handled inline. 6789ed346baSBosko Milekic */ 6799ed346baSBosko Milekic void 68090836c32SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, uintptr_t tid, 68190836c32SMateusz Guzik int opts, const char *file, int line) 68236412d79SJohn Baldwin { 6837f44c618SAttilio Rao struct mtx *m; 684a0d45f0fSMateusz Guzik struct lock_delay_arg lda; 6851723a064SJeff Roberson #ifdef LOCK_PROFILING 6861723a064SJeff Roberson int contested = 0; 68770fe8436SKip Macy uint64_t waittime = 0; 6881723a064SJeff Roberson #endif 689076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 690076dd8ebSAndriy Gapon int64_t spin_time = 0; 691076dd8ebSAndriy Gapon #endif 69236412d79SJohn Baldwin 69335370593SAndriy Gapon if (SCHEDULER_STOPPED()) 69435370593SAndriy Gapon return; 69535370593SAndriy Gapon 696a0d45f0fSMateusz Guzik lock_delay_arg_init(&lda, &mtx_spin_delay); 6977f44c618SAttilio Rao m = mtxlock2mtx(c); 6987f44c618SAttilio Rao 699*13d2ef0fSMateusz Guzik if (__predict_false(v == MTX_UNOWNED)) 700*13d2ef0fSMateusz Guzik v = MTX_READ_VALUE(m); 701*13d2ef0fSMateusz Guzik 702*13d2ef0fSMateusz Guzik if (__predict_false(v == tid)) { 703*13d2ef0fSMateusz Guzik m->mtx_recurse++; 704*13d2ef0fSMateusz Guzik return; 705*13d2ef0fSMateusz Guzik } 706*13d2ef0fSMateusz Guzik 707aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 7085746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 7092cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 7102cba8dd3SJohn Baldwin "spinning", "lockname:\"%s\"", m->lock_object.lo_name); 7119ed346baSBosko Milekic 712f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 713f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 714f5f9340bSFabien Thomas #endif 71570fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 716076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 717e2b25737SMark Johnston spin_time -= lockstat_nsecs(&m->lock_object); 718076dd8ebSAndriy Gapon #endif 719fc4f686dSMateusz Guzik for (;;) { 7202604eb9eSMateusz Guzik if (v == MTX_UNOWNED) { 72190836c32SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 722fc4f686dSMateusz Guzik break; 72336412d79SJohn Baldwin continue; 724703fc290SJohn Baldwin } 7252604eb9eSMateusz Guzik /* Give interrupts a chance while we spin. */ 7262604eb9eSMateusz Guzik spinlock_exit(); 7272604eb9eSMateusz Guzik do { 7282604eb9eSMateusz Guzik if (lda.spin_cnt < 10000000) { 7292604eb9eSMateusz Guzik lock_delay(&lda); 7302604eb9eSMateusz Guzik } else { 731a0d45f0fSMateusz Guzik lda.spin_cnt++; 732a0d45f0fSMateusz Guzik if (lda.spin_cnt < 60000000 || kdb_active || 733a0d45f0fSMateusz Guzik panicstr != NULL) 73436412d79SJohn Baldwin DELAY(1); 7352502c107SJeff Roberson else 7362502c107SJeff Roberson _mtx_lock_spin_failed(m); 7379f1b87f1SMaxime Henrion cpu_spinwait(); 73836412d79SJohn Baldwin } 7392604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 7402604eb9eSMateusz Guzik } while (v != MTX_UNOWNED); 741c6a37e84SJohn Baldwin spinlock_enter(); 74236412d79SJohn Baldwin } 743076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 744e2b25737SMark Johnston spin_time += lockstat_nsecs(&m->lock_object); 745076dd8ebSAndriy Gapon #endif 74636412d79SJohn Baldwin 747aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 7489ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 7492cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 7502cba8dd3SJohn Baldwin "running"); 7519ed346baSBosko Milekic 752c6d48c87SMark Johnston #ifdef KDTRACE_HOOKS 75332cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 75432cd0147SMark Johnston contested, waittime, file, line); 755e2b25737SMark Johnston if (spin_time != 0) 75632cd0147SMark Johnston LOCKSTAT_RECORD1(spin__spin, m, spin_time); 757c6d48c87SMark Johnston #endif 75836412d79SJohn Baldwin } 75933fb8a38SJohn Baldwin #endif /* SMP */ 76036412d79SJohn Baldwin 7612502c107SJeff Roberson void 762ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line) 7632502c107SJeff Roberson { 7642502c107SJeff Roberson struct mtx *m; 7655e5ad162SMateusz Guzik uintptr_t tid, v; 766a0d45f0fSMateusz Guzik struct lock_delay_arg lda; 7671723a064SJeff Roberson #ifdef LOCK_PROFILING 7681723a064SJeff Roberson int contested = 0; 7691723a064SJeff Roberson uint64_t waittime = 0; 7701723a064SJeff Roberson #endif 771a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 772076dd8ebSAndriy Gapon int64_t spin_time = 0; 773a5aedd68SStacey Son #endif 7742502c107SJeff Roberson 7752502c107SJeff Roberson tid = (uintptr_t)curthread; 77635370593SAndriy Gapon 777f61d6c5aSMark Johnston if (SCHEDULER_STOPPED()) { 778f61d6c5aSMark Johnston /* 779f61d6c5aSMark Johnston * Ensure that spinlock sections are balanced even when the 780f61d6c5aSMark Johnston * scheduler is stopped, since we may otherwise inadvertently 781f61d6c5aSMark Johnston * re-enable interrupts while dumping core. 782f61d6c5aSMark Johnston */ 783f61d6c5aSMark Johnston spinlock_enter(); 78435370593SAndriy Gapon return; 785f61d6c5aSMark Johnston } 78635370593SAndriy Gapon 787a0d45f0fSMateusz Guzik lock_delay_arg_init(&lda, &mtx_spin_delay); 788a0d45f0fSMateusz Guzik 789076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 790e2b25737SMark Johnston spin_time -= lockstat_nsecs(&td->td_lock->lock_object); 791076dd8ebSAndriy Gapon #endif 7922502c107SJeff Roberson for (;;) { 7932502c107SJeff Roberson retry: 794dc089651SMateusz Guzik v = MTX_UNOWNED; 7952502c107SJeff Roberson spinlock_enter(); 796710eacdcSJeff Roberson m = td->td_lock; 79713c85a48SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 79813c85a48SJohn Baldwin ("thread_lock() of destroyed mutex @ %s:%d", file, line)); 79913c85a48SJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 80013c85a48SJohn Baldwin ("thread_lock() of sleep mutex %s @ %s:%d", 80113c85a48SJohn Baldwin m->lock_object.lo_name, file, line)); 802ad69e26bSJohn Baldwin if (mtx_owned(m)) 803ad69e26bSJohn Baldwin KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 804ad69e26bSJohn Baldwin ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n", 805ad69e26bSJohn Baldwin m->lock_object.lo_name, file, line)); 8062502c107SJeff Roberson WITNESS_CHECKORDER(&m->lock_object, 80741313430SJohn Baldwin opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 808fc4f686dSMateusz Guzik for (;;) { 80990836c32SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 810fc4f686dSMateusz Guzik break; 81190836c32SMateusz Guzik if (v == MTX_UNOWNED) 8125e5ad162SMateusz Guzik continue; 8135e5ad162SMateusz Guzik if (v == tid) { 8142502c107SJeff Roberson m->mtx_recurse++; 8152502c107SJeff Roberson break; 8162502c107SJeff Roberson } 817f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 818f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 819f5f9340bSFabien Thomas #endif 820eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&m->lock_object, 821eea4f254SJeff Roberson &contested, &waittime); 8222502c107SJeff Roberson /* Give interrupts a chance while we spin. */ 8232502c107SJeff Roberson spinlock_exit(); 8245e5ad162SMateusz Guzik do { 825a0d45f0fSMateusz Guzik if (lda.spin_cnt < 10000000) { 826a0d45f0fSMateusz Guzik lock_delay(&lda); 827a0d45f0fSMateusz Guzik } else { 828a0d45f0fSMateusz Guzik lda.spin_cnt++; 829a0d45f0fSMateusz Guzik if (lda.spin_cnt < 60000000 || 8302502c107SJeff Roberson kdb_active || panicstr != NULL) 8312502c107SJeff Roberson DELAY(1); 8322502c107SJeff Roberson else 8332502c107SJeff Roberson _mtx_lock_spin_failed(m); 8342502c107SJeff Roberson cpu_spinwait(); 835a0d45f0fSMateusz Guzik } 8362502c107SJeff Roberson if (m != td->td_lock) 8372502c107SJeff Roberson goto retry; 8385e5ad162SMateusz Guzik v = MTX_READ_VALUE(m); 8395e5ad162SMateusz Guzik } while (v != MTX_UNOWNED); 8402502c107SJeff Roberson spinlock_enter(); 8412502c107SJeff Roberson } 8422502c107SJeff Roberson if (m == td->td_lock) 8432502c107SJeff Roberson break; 844961135eaSJohn Baldwin __mtx_unlock_spin(m); /* does spinlock_exit() */ 8452502c107SJeff Roberson } 846076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 847e2b25737SMark Johnston spin_time += lockstat_nsecs(&m->lock_object); 848076dd8ebSAndriy Gapon #endif 849eea4f254SJeff Roberson if (m->mtx_recurse == 0) 85032cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 85132cd0147SMark Johnston contested, waittime, file, line); 85213c85a48SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 85313c85a48SJohn Baldwin line); 8542502c107SJeff Roberson WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 8555002e195SMark Johnston #ifdef KDTRACE_HOOKS 856156fbc14SMark Johnston if (spin_time != 0) 85732cd0147SMark Johnston LOCKSTAT_RECORD1(thread__spin, m, spin_time); 8585002e195SMark Johnston #endif 8592502c107SJeff Roberson } 8602502c107SJeff Roberson 8612502c107SJeff Roberson struct mtx * 8622502c107SJeff Roberson thread_lock_block(struct thread *td) 8632502c107SJeff Roberson { 8642502c107SJeff Roberson struct mtx *lock; 8652502c107SJeff Roberson 8662502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 867710eacdcSJeff Roberson lock = td->td_lock; 8682502c107SJeff Roberson td->td_lock = &blocked_lock; 8692502c107SJeff Roberson mtx_unlock_spin(lock); 8702502c107SJeff Roberson 8712502c107SJeff Roberson return (lock); 8722502c107SJeff Roberson } 8732502c107SJeff Roberson 8742502c107SJeff Roberson void 8752502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new) 8762502c107SJeff Roberson { 8772502c107SJeff Roberson mtx_assert(new, MA_OWNED); 8782502c107SJeff Roberson MPASS(td->td_lock == &blocked_lock); 87965d32cd8SMatt Jacob atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new); 8802502c107SJeff Roberson } 8812502c107SJeff Roberson 8822502c107SJeff Roberson void 8832502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new) 8842502c107SJeff Roberson { 8852502c107SJeff Roberson struct mtx *lock; 8862502c107SJeff Roberson 8872502c107SJeff Roberson mtx_assert(new, MA_OWNED); 8882502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 889710eacdcSJeff Roberson lock = td->td_lock; 8902502c107SJeff Roberson td->td_lock = new; 8912502c107SJeff Roberson mtx_unlock_spin(lock); 8922502c107SJeff Roberson } 8932502c107SJeff Roberson 8949ed346baSBosko Milekic /* 8957f44c618SAttilio Rao * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 8969ed346baSBosko Milekic * 8973b3cf014SMateusz Guzik * We are only called here if the lock is recursed, contested (i.e. we 8983b3cf014SMateusz Guzik * need to wake up a blocked thread) or lockstat probe is active. 8999ed346baSBosko Milekic */ 90009f1319aSMateusz Guzik #if LOCK_DEBUG > 0 90136412d79SJohn Baldwin void 9027f44c618SAttilio Rao __mtx_unlock_sleep(volatile uintptr_t *c, int opts, const char *file, int line) 90309f1319aSMateusz Guzik #else 90409f1319aSMateusz Guzik void 905a24c8eb8SMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c) 90609f1319aSMateusz Guzik #endif 90736412d79SJohn Baldwin { 9087f44c618SAttilio Rao struct mtx *m; 909961a7b24SJohn Baldwin struct turnstile *ts; 9103b3cf014SMateusz Guzik uintptr_t tid, v; 9119ed346baSBosko Milekic 91235370593SAndriy Gapon if (SCHEDULER_STOPPED()) 91335370593SAndriy Gapon return; 91435370593SAndriy Gapon 9153b3cf014SMateusz Guzik tid = (uintptr_t)curthread; 9167f44c618SAttilio Rao m = mtxlock2mtx(c); 9173b3cf014SMateusz Guzik v = MTX_READ_VALUE(m); 91890836c32SMateusz Guzik 9193b3cf014SMateusz Guzik if (v & MTX_RECURSED) { 92036412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 92108812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 922aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 9239ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 92436412d79SJohn Baldwin return; 92536412d79SJohn Baldwin } 9269ed346baSBosko Milekic 9273b3cf014SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m); 9283b3cf014SMateusz Guzik if (v == tid && _mtx_release_lock(m, tid)) 9293b3cf014SMateusz Guzik return; 9303b3cf014SMateusz Guzik 9312502c107SJeff Roberson /* 9322502c107SJeff Roberson * We have to lock the chain before the turnstile so this turnstile 9332502c107SJeff Roberson * can be removed from the hash list if it is empty. 9342502c107SJeff Roberson */ 9352502c107SJeff Roberson turnstile_chain_lock(&m->lock_object); 936aa89d8cdSJohn Baldwin ts = turnstile_lookup(&m->lock_object); 937aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 9389ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 939961a7b24SJohn Baldwin MPASS(ts != NULL); 9407aa4f685SJohn Baldwin turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 941961135eaSJohn Baldwin _mtx_release_lock_quick(m); 942bf9c6c31SJohn Baldwin 9432502c107SJeff Roberson /* 9442502c107SJeff Roberson * This turnstile is now no longer associated with the mutex. We can 9452502c107SJeff Roberson * unlock the chain lock so a new turnstile may take it's place. 9462502c107SJeff Roberson */ 9477aa4f685SJohn Baldwin turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 9482502c107SJeff Roberson turnstile_chain_unlock(&m->lock_object); 9499ed346baSBosko Milekic } 9509ed346baSBosko Milekic 9519ed346baSBosko Milekic /* 9529ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 953961135eaSJohn Baldwin * See the __mtx_unlock_spin() macro for the details. 9549ed346baSBosko Milekic */ 9559ed346baSBosko Milekic 9569ed346baSBosko Milekic /* 95715ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 9589ed346baSBosko Milekic */ 9591103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 9600cde2e34SJason Evans void 9617f44c618SAttilio Rao __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line) 9620cde2e34SJason Evans { 9637f44c618SAttilio Rao const struct mtx *m; 9645cb0fbe4SJohn Baldwin 965310ab671SEric van Gyzen if (panicstr != NULL || dumping || SCHEDULER_STOPPED()) 9665cb0fbe4SJohn Baldwin return; 9677f44c618SAttilio Rao 9687f44c618SAttilio Rao m = mtxlock2mtx(c); 9697f44c618SAttilio Rao 970a10f4966SJake Burkholder switch (what) { 9710cde2e34SJason Evans case MA_OWNED: 9720cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 9730cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 974a10f4966SJake Burkholder if (!mtx_owned(m)) 9750cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 976aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 977a10f4966SJake Burkholder if (mtx_recursed(m)) { 978a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 9790cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 980aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 981a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 9820cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 983aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 9840cde2e34SJason Evans } 9850cde2e34SJason Evans break; 9860cde2e34SJason Evans case MA_NOTOWNED: 987a10f4966SJake Burkholder if (mtx_owned(m)) 9880cde2e34SJason Evans panic("mutex %s owned at %s:%d", 989aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 9900cde2e34SJason Evans break; 9910cde2e34SJason Evans default: 99256771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 9930cde2e34SJason Evans } 9940cde2e34SJason Evans } 9950cde2e34SJason Evans #endif 9960cde2e34SJason Evans 9979ed346baSBosko Milekic /* 998c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 999c27b5699SAndrew R. Reiter */ 1000c27b5699SAndrew R. Reiter void 1001c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 1002c27b5699SAndrew R. Reiter { 1003c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 1004c27b5699SAndrew R. Reiter 10057f44c618SAttilio Rao mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL, 10067f44c618SAttilio Rao margs->ma_opts); 1007c27b5699SAndrew R. Reiter } 1008c27b5699SAndrew R. Reiter 1009c27b5699SAndrew R. Reiter /* 10109ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 10110c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 10120c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 10130c88508aSJohn Baldwin * witness. 10149ed346baSBosko Milekic */ 101536412d79SJohn Baldwin void 10167f44c618SAttilio Rao _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts) 101736412d79SJohn Baldwin { 10187f44c618SAttilio Rao struct mtx *m; 101983a81bcbSJohn Baldwin struct lock_class *class; 102083a81bcbSJohn Baldwin int flags; 10219ed346baSBosko Milekic 10227f44c618SAttilio Rao m = mtxlock2mtx(c); 10237f44c618SAttilio Rao 102419284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 1025fd07ddcfSDmitry Chagin MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0); 1026353998acSAttilio Rao ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock, 1027353998acSAttilio Rao ("%s: mtx_lock not aligned for %s: %p", __func__, name, 1028353998acSAttilio Rao &m->mtx_lock)); 10299ed346baSBosko Milekic 103083a81bcbSJohn Baldwin /* Determine lock class and lock flags. */ 103119284646SJohn Baldwin if (opts & MTX_SPIN) 103283a81bcbSJohn Baldwin class = &lock_class_mtx_spin; 103319284646SJohn Baldwin else 103483a81bcbSJohn Baldwin class = &lock_class_mtx_sleep; 103583a81bcbSJohn Baldwin flags = 0; 103619284646SJohn Baldwin if (opts & MTX_QUIET) 103783a81bcbSJohn Baldwin flags |= LO_QUIET; 103819284646SJohn Baldwin if (opts & MTX_RECURSE) 103983a81bcbSJohn Baldwin flags |= LO_RECURSABLE; 104019284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 104183a81bcbSJohn Baldwin flags |= LO_WITNESS; 1042f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 104383a81bcbSJohn Baldwin flags |= LO_DUPOK; 10447c0435b9SKip Macy if (opts & MTX_NOPROFILE) 10457c0435b9SKip Macy flags |= LO_NOPROFILE; 1046fd07ddcfSDmitry Chagin if (opts & MTX_NEW) 1047fd07ddcfSDmitry Chagin flags |= LO_NEW; 104819284646SJohn Baldwin 104983a81bcbSJohn Baldwin /* Initialize mutex. */ 1050b5fb43e5SJohn Baldwin lock_init(&m->lock_object, class, name, type, flags); 1051b5fb43e5SJohn Baldwin 105219284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 105383a81bcbSJohn Baldwin m->mtx_recurse = 0; 105436412d79SJohn Baldwin } 105536412d79SJohn Baldwin 10569ed346baSBosko Milekic /* 105719284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 105819284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 105919284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 106019284646SJohn Baldwin * flags. 10619ed346baSBosko Milekic */ 106236412d79SJohn Baldwin void 10637f44c618SAttilio Rao _mtx_destroy(volatile uintptr_t *c) 106436412d79SJohn Baldwin { 10657f44c618SAttilio Rao struct mtx *m; 10667f44c618SAttilio Rao 10677f44c618SAttilio Rao m = mtxlock2mtx(c); 106836412d79SJohn Baldwin 106919284646SJohn Baldwin if (!mtx_owned(m)) 107019284646SJohn Baldwin MPASS(mtx_unowned(m)); 107119284646SJohn Baldwin else { 107208812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 10739ed346baSBosko Milekic 1074861a2308SScott Long /* Perform the non-mtx related part of mtx_unlock_spin(). */ 1075aa89d8cdSJohn Baldwin if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) 1076861a2308SScott Long spinlock_exit(); 1077764e4d54SJohn Baldwin else 1078ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 1079861a2308SScott Long 1080d3df4af3SJeff Roberson lock_profile_release_lock(&m->lock_object); 108119284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 1082aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__, 1083c86b6ff5SJohn Baldwin __LINE__); 108436412d79SJohn Baldwin } 10850384fff8SJason Evans 1086186abbd7SJohn Baldwin m->mtx_lock = MTX_DESTROYED; 1087aa89d8cdSJohn Baldwin lock_destroy(&m->lock_object); 10880384fff8SJason Evans } 1089d23f5958SMatthew Dillon 1090d23f5958SMatthew Dillon /* 1091c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 1092c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 1093c53c013bSJohn Baldwin * setup before this is called. 1094c53c013bSJohn Baldwin */ 1095c53c013bSJohn Baldwin void 1096c53c013bSJohn Baldwin mutex_init(void) 1097c53c013bSJohn Baldwin { 1098c53c013bSJohn Baldwin 1099961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 1100961a7b24SJohn Baldwin init_turnstiles(); 1101961a7b24SJohn Baldwin 1102c53c013bSJohn Baldwin /* 1103c53c013bSJohn Baldwin * Initialize mutexes. 1104c53c013bSJohn Baldwin */ 11050c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 11062502c107SJeff Roberson mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN); 11072502c107SJeff Roberson blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */ 11080c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 11096afb32fcSKonstantin Belousov mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN); 11105c7bebf9SKonstantin Belousov mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN); 11115c7bebf9SKonstantin Belousov mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN); 11125c7bebf9SKonstantin Belousov mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN); 11138c4b6380SJohn Baldwin mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 1114c53c013bSJohn Baldwin mtx_lock(&Giant); 1115c53c013bSJohn Baldwin } 1116d272fe53SJohn Baldwin 1117d272fe53SJohn Baldwin #ifdef DDB 1118d272fe53SJohn Baldwin void 1119d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock) 1120d272fe53SJohn Baldwin { 1121d272fe53SJohn Baldwin struct thread *td; 1122d576deedSPawel Jakub Dawidek const struct mtx *m; 1123d272fe53SJohn Baldwin 1124d576deedSPawel Jakub Dawidek m = (const struct mtx *)lock; 1125d272fe53SJohn Baldwin 1126d272fe53SJohn Baldwin db_printf(" flags: {"); 112783a81bcbSJohn Baldwin if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 1128d272fe53SJohn Baldwin db_printf("SPIN"); 1129d272fe53SJohn Baldwin else 1130d272fe53SJohn Baldwin db_printf("DEF"); 1131aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_RECURSABLE) 1132d272fe53SJohn Baldwin db_printf(", RECURSE"); 1133aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_DUPOK) 1134d272fe53SJohn Baldwin db_printf(", DUPOK"); 1135d272fe53SJohn Baldwin db_printf("}\n"); 1136d272fe53SJohn Baldwin db_printf(" state: {"); 1137d272fe53SJohn Baldwin if (mtx_unowned(m)) 1138d272fe53SJohn Baldwin db_printf("UNOWNED"); 1139c0bfd703SJohn Baldwin else if (mtx_destroyed(m)) 1140c0bfd703SJohn Baldwin db_printf("DESTROYED"); 1141d272fe53SJohn Baldwin else { 1142d272fe53SJohn Baldwin db_printf("OWNED"); 1143d272fe53SJohn Baldwin if (m->mtx_lock & MTX_CONTESTED) 1144d272fe53SJohn Baldwin db_printf(", CONTESTED"); 1145d272fe53SJohn Baldwin if (m->mtx_lock & MTX_RECURSED) 1146d272fe53SJohn Baldwin db_printf(", RECURSED"); 1147d272fe53SJohn Baldwin } 1148d272fe53SJohn Baldwin db_printf("}\n"); 1149c0bfd703SJohn Baldwin if (!mtx_unowned(m) && !mtx_destroyed(m)) { 1150d272fe53SJohn Baldwin td = mtx_owner(m); 1151d272fe53SJohn Baldwin db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 1152431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 1153d272fe53SJohn Baldwin if (mtx_recursed(m)) 1154d272fe53SJohn Baldwin db_printf(" recursed: %d\n", m->mtx_recurse); 1155d272fe53SJohn Baldwin } 1156d272fe53SJohn Baldwin } 1157d272fe53SJohn Baldwin #endif 1158