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> 58a5a96a19SJohn Baldwin #include <sys/sysctl.h> 59961a7b24SJohn Baldwin #include <sys/turnstile.h> 6036412d79SJohn Baldwin #include <sys/vmmeter.h> 617c0435b9SKip Macy #include <sys/lock_profile.h> 620384fff8SJason Evans 6336412d79SJohn Baldwin #include <machine/atomic.h> 6436412d79SJohn Baldwin #include <machine/bus.h> 650384fff8SJason Evans #include <machine/cpu.h> 6636412d79SJohn Baldwin 679c36c934SJohn Baldwin #include <ddb/ddb.h> 689c36c934SJohn Baldwin 698c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h> 708c4b6380SJohn Baldwin 7136412d79SJohn Baldwin #include <vm/vm.h> 7236412d79SJohn Baldwin #include <vm/vm_extern.h> 7336412d79SJohn Baldwin 74cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 75cd6e6e4eSJohn Baldwin #define ADAPTIVE_MUTEXES 76cd6e6e4eSJohn Baldwin #endif 77cd6e6e4eSJohn Baldwin 78f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 79f5f9340bSFabien Thomas #include <sys/pmckern.h> 80f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , lock, failed); 81f5f9340bSFabien Thomas #endif 82f5f9340bSFabien Thomas 83b9a80acaSStephan Uphoff /* 847f44c618SAttilio Rao * Return the mutex address when the lock cookie address is provided. 857f44c618SAttilio Rao * This functionality assumes that struct mtx* have a member named mtx_lock. 867f44c618SAttilio Rao */ 877f44c618SAttilio Rao #define mtxlock2mtx(c) (__containerof(c, struct mtx, mtx_lock)) 887f44c618SAttilio Rao 897f44c618SAttilio Rao /* 909ed346baSBosko Milekic * Internal utility macros. 910cde2e34SJason Evans */ 929ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 930cde2e34SJason Evans 94c0bfd703SJohn Baldwin #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED) 95c0bfd703SJohn Baldwin 9649b94bfcSJohn Baldwin #define mtx_owner(m) ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK)) 979ed346baSBosko Milekic 98d576deedSPawel Jakub Dawidek static void assert_mtx(const struct lock_object *lock, int what); 99d272fe53SJohn Baldwin #ifdef DDB 100d576deedSPawel Jakub Dawidek static void db_show_mtx(const struct lock_object *lock); 101d272fe53SJohn Baldwin #endif 1027faf4d90SDavide Italiano static void lock_mtx(struct lock_object *lock, uintptr_t how); 1037faf4d90SDavide Italiano static void lock_spin(struct lock_object *lock, uintptr_t how); 104a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 105d576deedSPawel Jakub Dawidek static int owner_mtx(const struct lock_object *lock, 106d576deedSPawel Jakub Dawidek struct thread **owner); 107a5aedd68SStacey Son #endif 1087faf4d90SDavide Italiano static uintptr_t unlock_mtx(struct lock_object *lock); 1097faf4d90SDavide Italiano static uintptr_t unlock_spin(struct lock_object *lock); 110d272fe53SJohn Baldwin 1110cde2e34SJason Evans /* 11219284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 1130cde2e34SJason Evans */ 11419284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 115ae8dde30SJohn Baldwin .lc_name = "sleep mutex", 116ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, 117f9721b43SAttilio Rao .lc_assert = assert_mtx, 118d272fe53SJohn Baldwin #ifdef DDB 119ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 120d272fe53SJohn Baldwin #endif 1216e21afd4SJohn Baldwin .lc_lock = lock_mtx, 1226e21afd4SJohn Baldwin .lc_unlock = unlock_mtx, 123a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 124a5aedd68SStacey Son .lc_owner = owner_mtx, 125a5aedd68SStacey Son #endif 12619284646SJohn Baldwin }; 12719284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 128ae8dde30SJohn Baldwin .lc_name = "spin mutex", 129ae8dde30SJohn Baldwin .lc_flags = LC_SPINLOCK | LC_RECURSABLE, 130f9721b43SAttilio Rao .lc_assert = assert_mtx, 131d272fe53SJohn Baldwin #ifdef DDB 132ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 133d272fe53SJohn Baldwin #endif 1346e21afd4SJohn Baldwin .lc_lock = lock_spin, 1356e21afd4SJohn Baldwin .lc_unlock = unlock_spin, 136a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 137a5aedd68SStacey Son .lc_owner = owner_mtx, 138a5aedd68SStacey Son #endif 1398484de75SJohn Baldwin }; 1408484de75SJohn Baldwin 1419ed346baSBosko Milekic /* 142c53c013bSJohn Baldwin * System-wide mutexes 143c53c013bSJohn Baldwin */ 1442502c107SJeff Roberson struct mtx blocked_lock; 145c53c013bSJohn Baldwin struct mtx Giant; 146c53c013bSJohn Baldwin 14767784314SPoul-Henning Kamp void 148d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what) 149f9721b43SAttilio Rao { 150f9721b43SAttilio Rao 151d576deedSPawel Jakub Dawidek mtx_assert((const struct mtx *)lock, what); 152f9721b43SAttilio Rao } 153f9721b43SAttilio Rao 15467784314SPoul-Henning Kamp void 1557faf4d90SDavide Italiano lock_mtx(struct lock_object *lock, uintptr_t how) 1566e21afd4SJohn Baldwin { 1576e21afd4SJohn Baldwin 1586e21afd4SJohn Baldwin mtx_lock((struct mtx *)lock); 1596e21afd4SJohn Baldwin } 1606e21afd4SJohn Baldwin 16167784314SPoul-Henning Kamp void 1627faf4d90SDavide Italiano lock_spin(struct lock_object *lock, uintptr_t how) 1636e21afd4SJohn Baldwin { 1646e21afd4SJohn Baldwin 1656e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 1666e21afd4SJohn Baldwin } 1676e21afd4SJohn Baldwin 1687faf4d90SDavide Italiano uintptr_t 1696e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock) 1706e21afd4SJohn Baldwin { 1716e21afd4SJohn Baldwin struct mtx *m; 1726e21afd4SJohn Baldwin 1736e21afd4SJohn Baldwin m = (struct mtx *)lock; 1746e21afd4SJohn Baldwin mtx_assert(m, MA_OWNED | MA_NOTRECURSED); 1756e21afd4SJohn Baldwin mtx_unlock(m); 1766e21afd4SJohn Baldwin return (0); 1776e21afd4SJohn Baldwin } 1786e21afd4SJohn Baldwin 1797faf4d90SDavide Italiano uintptr_t 1806e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock) 1816e21afd4SJohn Baldwin { 1826e21afd4SJohn Baldwin 1836e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 1846e21afd4SJohn Baldwin } 1856e21afd4SJohn Baldwin 186a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 187a5aedd68SStacey Son int 188d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner) 189a5aedd68SStacey Son { 190d576deedSPawel Jakub Dawidek const struct mtx *m = (const struct mtx *)lock; 191a5aedd68SStacey Son 192a5aedd68SStacey Son *owner = mtx_owner(m); 193a5aedd68SStacey Son return (mtx_unowned(m) == 0); 194a5aedd68SStacey Son } 195a5aedd68SStacey Son #endif 196a5aedd68SStacey Son 1970cde2e34SJason Evans /* 1986283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 1996283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 2006283b7d0SJohn Baldwin */ 2016283b7d0SJohn Baldwin void 2027f44c618SAttilio Rao __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 2036283b7d0SJohn Baldwin { 2047f44c618SAttilio Rao struct mtx *m; 2056283b7d0SJohn Baldwin 20635370593SAndriy Gapon if (SCHEDULER_STOPPED()) 20735370593SAndriy Gapon return; 2087f44c618SAttilio Rao 2097f44c618SAttilio Rao m = mtxlock2mtx(c); 2107f44c618SAttilio Rao 211cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 212e3ae0dfeSAttilio Rao ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d", 213e3ae0dfeSAttilio Rao curthread, m->lock_object.lo_name, file, line)); 214186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 215186abbd7SJohn Baldwin ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); 216aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 217aa89d8cdSJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2180d975d63SJohn Baldwin file, line)); 219ac6b769bSAttilio Rao WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) | 220ac6b769bSAttilio Rao LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 2217c0435b9SKip Macy 222961135eaSJohn Baldwin __mtx_lock(m, curthread, opts, file, line); 223aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 224dde96c99SJohn Baldwin line); 225ac6b769bSAttilio Rao WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE, 226ac6b769bSAttilio Rao file, line); 227ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 2286283b7d0SJohn Baldwin } 2296283b7d0SJohn Baldwin 2306283b7d0SJohn Baldwin void 2317f44c618SAttilio Rao __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 2326283b7d0SJohn Baldwin { 2337f44c618SAttilio Rao struct mtx *m; 23435370593SAndriy Gapon 23535370593SAndriy Gapon if (SCHEDULER_STOPPED()) 23635370593SAndriy Gapon return; 2377f44c618SAttilio Rao 2387f44c618SAttilio Rao m = mtxlock2mtx(c); 2397f44c618SAttilio Rao 240186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 241186abbd7SJohn Baldwin ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); 242aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 243aa89d8cdSJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2440d975d63SJohn Baldwin file, line)); 245aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 246aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 2470d975d63SJohn Baldwin line); 24821377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 249c66d7606SKip Macy 250961135eaSJohn Baldwin __mtx_unlock(m, curthread, opts, file, line); 251ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 2526283b7d0SJohn Baldwin } 2536283b7d0SJohn Baldwin 2546283b7d0SJohn Baldwin void 2557f44c618SAttilio Rao __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 2567f44c618SAttilio Rao int line) 2576283b7d0SJohn Baldwin { 2587f44c618SAttilio Rao struct mtx *m; 2596283b7d0SJohn Baldwin 26035370593SAndriy Gapon if (SCHEDULER_STOPPED()) 26135370593SAndriy Gapon return; 2627f44c618SAttilio Rao 2637f44c618SAttilio Rao m = mtxlock2mtx(c); 2647f44c618SAttilio Rao 265186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 266186abbd7SJohn Baldwin ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); 267aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 2680d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 269aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 270ad69e26bSJohn Baldwin if (mtx_owned(m)) 271ac6b769bSAttilio Rao KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 272ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0, 273ad69e26bSJohn Baldwin ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n", 274ad69e26bSJohn Baldwin m->lock_object.lo_name, file, line)); 275ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 276aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 27741313430SJohn Baldwin file, line, NULL); 278961135eaSJohn Baldwin __mtx_lock_spin(m, curthread, opts, file, line); 279aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 280dde96c99SJohn Baldwin line); 281aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 2826283b7d0SJohn Baldwin } 2836283b7d0SJohn Baldwin 28490b581f2SKonstantin Belousov int 28590b581f2SKonstantin Belousov __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 28690b581f2SKonstantin Belousov int line) 28790b581f2SKonstantin Belousov { 28890b581f2SKonstantin Belousov struct mtx *m; 28990b581f2SKonstantin Belousov 29090b581f2SKonstantin Belousov if (SCHEDULER_STOPPED()) 29190b581f2SKonstantin Belousov return (1); 29290b581f2SKonstantin Belousov 29390b581f2SKonstantin Belousov m = mtxlock2mtx(c); 29490b581f2SKonstantin Belousov 29590b581f2SKonstantin Belousov KASSERT(m->mtx_lock != MTX_DESTROYED, 29690b581f2SKonstantin Belousov ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line)); 29790b581f2SKonstantin Belousov KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 29890b581f2SKonstantin Belousov ("mtx_trylock_spin() of sleep mutex %s @ %s:%d", 29990b581f2SKonstantin Belousov m->lock_object.lo_name, file, line)); 30090b581f2SKonstantin Belousov KASSERT((opts & MTX_RECURSE) == 0, 30190b581f2SKonstantin Belousov ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n", 30290b581f2SKonstantin Belousov m->lock_object.lo_name, file, line)); 30390b581f2SKonstantin Belousov if (__mtx_trylock_spin(m, curthread, opts, file, line)) { 30490b581f2SKonstantin Belousov LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line); 30590b581f2SKonstantin Belousov WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 30690b581f2SKonstantin Belousov return (1); 30790b581f2SKonstantin Belousov } 30890b581f2SKonstantin Belousov LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line); 30990b581f2SKonstantin Belousov return (0); 31090b581f2SKonstantin Belousov } 31190b581f2SKonstantin Belousov 3126283b7d0SJohn Baldwin void 3137f44c618SAttilio Rao __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 3147f44c618SAttilio Rao int line) 3156283b7d0SJohn Baldwin { 3167f44c618SAttilio Rao struct mtx *m; 317c66d7606SKip Macy 31835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 31935370593SAndriy Gapon return; 3207f44c618SAttilio Rao 3217f44c618SAttilio Rao m = mtxlock2mtx(c); 3227f44c618SAttilio Rao 323186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 324186abbd7SJohn Baldwin ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); 325aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 3260d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 327aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 328aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 329aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 330dde96c99SJohn Baldwin line); 3310d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 332c66d7606SKip Macy 333961135eaSJohn Baldwin __mtx_unlock_spin(m); 3346283b7d0SJohn Baldwin } 3356283b7d0SJohn Baldwin 3366283b7d0SJohn Baldwin /* 3379ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 338eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 339eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 3400cde2e34SJason Evans */ 3410cde2e34SJason Evans int 3427f44c618SAttilio Rao _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line) 3430cde2e34SJason Evans { 3447f44c618SAttilio Rao struct mtx *m; 3451723a064SJeff Roberson #ifdef LOCK_PROFILING 3467c0435b9SKip Macy uint64_t waittime = 0; 3471723a064SJeff Roberson int contested = 0; 3481723a064SJeff Roberson #endif 3491723a064SJeff Roberson int rval; 3500cde2e34SJason Evans 35135370593SAndriy Gapon if (SCHEDULER_STOPPED()) 35235370593SAndriy Gapon return (1); 35335370593SAndriy Gapon 3547f44c618SAttilio Rao m = mtxlock2mtx(c); 3557f44c618SAttilio Rao 356cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 357e3ae0dfeSAttilio Rao ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d", 358e3ae0dfeSAttilio Rao curthread, m->lock_object.lo_name, file, line)); 359186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 360186abbd7SJohn Baldwin ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); 361aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 362aa89d8cdSJohn Baldwin ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 36383cece6fSJohn Baldwin file, line)); 3649ed346baSBosko Milekic 365ac6b769bSAttilio Rao if (mtx_owned(m) && ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 366ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0)) { 367eac09796SJohn Baldwin m->mtx_recurse++; 368eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 369eac09796SJohn Baldwin rval = 1; 370eac09796SJohn Baldwin } else 371961135eaSJohn Baldwin rval = _mtx_obtain_lock(m, (uintptr_t)curthread); 372ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 3739ed346baSBosko Milekic 374aa89d8cdSJohn Baldwin LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line); 375764e4d54SJohn Baldwin if (rval) { 376aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 3772d96f0b1SJohn Baldwin file, line); 378ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 379fe68a916SKip Macy if (m->mtx_recurse == 0) 38032cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, 381a5aedd68SStacey Son m, contested, waittime, file, line); 3827c0435b9SKip Macy 383764e4d54SJohn Baldwin } 3849ed346baSBosko Milekic 38519284646SJohn Baldwin return (rval); 3860cde2e34SJason Evans } 3870cde2e34SJason Evans 3880cde2e34SJason Evans /* 3897f44c618SAttilio Rao * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 3909ed346baSBosko Milekic * 3919ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 3929ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 3930cde2e34SJason Evans */ 3940cde2e34SJason Evans void 3957f44c618SAttilio Rao __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t tid, int opts, 3967f44c618SAttilio Rao const char *file, int line) 39736412d79SJohn Baldwin { 3987f44c618SAttilio Rao struct mtx *m; 3992502c107SJeff Roberson struct turnstile *ts; 4001723a064SJeff Roberson uintptr_t v; 401cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES 40276447e56SJohn Baldwin volatile struct thread *owner; 4032498cf8cSJohn Baldwin #endif 40402bd1bcdSIan Dowse #ifdef KTR 40502bd1bcdSIan Dowse int cont_logged = 0; 40602bd1bcdSIan Dowse #endif 4071723a064SJeff Roberson #ifdef LOCK_PROFILING 40870fe8436SKip Macy int contested = 0; 40970fe8436SKip Macy uint64_t waittime = 0; 4101723a064SJeff Roberson #endif 411a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 412*61852185SMateusz Guzik u_int spin_cnt = 0; 413*61852185SMateusz Guzik u_int sleep_cnt = 0; 414a5aedd68SStacey Son int64_t sleep_time = 0; 415076dd8ebSAndriy Gapon int64_t all_time = 0; 416a5aedd68SStacey Son #endif 41736412d79SJohn Baldwin 41835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 41935370593SAndriy Gapon return; 42035370593SAndriy Gapon 4217f44c618SAttilio Rao m = mtxlock2mtx(c); 4227f44c618SAttilio Rao 4235fa8dd90SJohn Baldwin if (mtx_owned(m)) { 424ac6b769bSAttilio Rao KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 425ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0, 426eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 427aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 428ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 42936412d79SJohn Baldwin m->mtx_recurse++; 43008812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 431aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 4325746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 43336412d79SJohn Baldwin return; 43436412d79SJohn Baldwin } 435ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 4369ed346baSBosko Milekic 437f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 438f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 439f5f9340bSFabien Thomas #endif 44070fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, 44170fe8436SKip Macy &contested, &waittime); 442aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 44315ec816aSJohn Baldwin CTR4(KTR_LOCK, 44415ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 445aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)m->mtx_lock, file, line); 446076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 447e2b25737SMark Johnston all_time -= lockstat_nsecs(&m->lock_object); 448076dd8ebSAndriy Gapon #endif 4491bd0eefbSJohn Baldwin 450fc4f686dSMateusz Guzik for (;;) { 451fc4f686dSMateusz Guzik if (m->mtx_lock == MTX_UNOWNED && _mtx_obtain_lock(m, tid)) 452fc4f686dSMateusz Guzik break; 453a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 454a5aedd68SStacey Son spin_cnt++; 455a5aedd68SStacey Son #endif 45649aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 45749aead8aSAttilio Rao /* 45849aead8aSAttilio Rao * If the owner is running on another CPU, spin until the 45949aead8aSAttilio Rao * owner stops running or the state of the lock changes. 46049aead8aSAttilio Rao */ 46149aead8aSAttilio Rao v = m->mtx_lock; 46249aead8aSAttilio Rao if (v != MTX_UNOWNED) { 46349aead8aSAttilio Rao owner = (struct thread *)(v & ~MTX_FLAGMASK); 46449aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 46549aead8aSAttilio Rao if (LOCK_LOG_TEST(&m->lock_object, 0)) 46649aead8aSAttilio Rao CTR3(KTR_LOCK, 46749aead8aSAttilio Rao "%s: spinning on %p held by %p", 46849aead8aSAttilio Rao __func__, m, owner); 4692cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 4702cba8dd3SJohn Baldwin sched_tdname((struct thread *)tid), 4712cba8dd3SJohn Baldwin "spinning", "lockname:\"%s\"", 4722cba8dd3SJohn Baldwin m->lock_object.lo_name); 47349aead8aSAttilio Rao while (mtx_owner(m) == owner && 474a5aedd68SStacey Son TD_IS_RUNNING(owner)) { 47549aead8aSAttilio Rao cpu_spinwait(); 476a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 477a5aedd68SStacey Son spin_cnt++; 478a5aedd68SStacey Son #endif 479a5aedd68SStacey Son } 4802cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 4812cba8dd3SJohn Baldwin sched_tdname((struct thread *)tid), 4822cba8dd3SJohn Baldwin "running"); 48349aead8aSAttilio Rao continue; 48449aead8aSAttilio Rao } 48549aead8aSAttilio Rao } 48649aead8aSAttilio Rao #endif 48749aead8aSAttilio Rao 4882502c107SJeff Roberson ts = turnstile_trywait(&m->lock_object); 4895fa8dd90SJohn Baldwin v = m->mtx_lock; 4905fa8dd90SJohn Baldwin 49136412d79SJohn Baldwin /* 4929ed346baSBosko Milekic * Check if the lock has been released while spinning for 493961a7b24SJohn Baldwin * the turnstile chain lock. 49436412d79SJohn Baldwin */ 4955fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 4962502c107SJeff Roberson turnstile_cancel(ts); 49736412d79SJohn Baldwin continue; 49836412d79SJohn Baldwin } 4999ed346baSBosko Milekic 50049aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 50149aead8aSAttilio Rao /* 502fa29f023SJohn Baldwin * The current lock owner might have started executing 503fa29f023SJohn Baldwin * on another CPU (or the lock could have changed 504fa29f023SJohn Baldwin * owners) while we were waiting on the turnstile 505fa29f023SJohn Baldwin * chain lock. If so, drop the turnstile lock and try 506fa29f023SJohn Baldwin * again. 50749aead8aSAttilio Rao */ 50849aead8aSAttilio Rao owner = (struct thread *)(v & ~MTX_FLAGMASK); 50949aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 51049aead8aSAttilio Rao turnstile_cancel(ts); 51149aead8aSAttilio Rao continue; 51249aead8aSAttilio Rao } 51349aead8aSAttilio Rao #endif 51449aead8aSAttilio Rao 51536412d79SJohn Baldwin /* 5169ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 5179ed346baSBosko Milekic * setting the contested bit, the mutex was either released 5189ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 51936412d79SJohn Baldwin */ 52036412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 521122eceefSJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 5222502c107SJeff Roberson turnstile_cancel(ts); 52336412d79SJohn Baldwin continue; 52436412d79SJohn Baldwin } 52536412d79SJohn Baldwin 5269ed346baSBosko Milekic /* 5277feefcd6SJohn Baldwin * We definitely must sleep for this lock. 5289ed346baSBosko Milekic */ 52936412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 53036412d79SJohn Baldwin 53102bd1bcdSIan Dowse #ifdef KTR 53202bd1bcdSIan Dowse if (!cont_logged) { 53302bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 53402bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 535aa89d8cdSJohn Baldwin (void *)tid, file, line, m->lock_object.lo_name, 536aa89d8cdSJohn Baldwin WITNESS_FILE(&m->lock_object), 537aa89d8cdSJohn Baldwin WITNESS_LINE(&m->lock_object)); 53802bd1bcdSIan Dowse cont_logged = 1; 53902bd1bcdSIan Dowse } 54002bd1bcdSIan Dowse #endif 54136412d79SJohn Baldwin 5429ed346baSBosko Milekic /* 543961a7b24SJohn Baldwin * Block on the turnstile. 5449ed346baSBosko Milekic */ 545a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 546e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&m->lock_object); 547a5aedd68SStacey Son #endif 5482502c107SJeff Roberson turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE); 549a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 550e2b25737SMark Johnston sleep_time += lockstat_nsecs(&m->lock_object); 551a5aedd68SStacey Son sleep_cnt++; 552a5aedd68SStacey Son #endif 55336412d79SJohn Baldwin } 554076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 555e2b25737SMark Johnston all_time += lockstat_nsecs(&m->lock_object); 556076dd8ebSAndriy Gapon #endif 55702bd1bcdSIan Dowse #ifdef KTR 55802bd1bcdSIan Dowse if (cont_logged) { 55902bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 56002bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 561aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)tid, file, line); 56202bd1bcdSIan Dowse } 56302bd1bcdSIan Dowse #endif 56432cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested, 565eea4f254SJeff Roberson waittime, file, line); 566a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 567a5aedd68SStacey Son if (sleep_time) 56832cd0147SMark Johnston LOCKSTAT_RECORD1(adaptive__block, m, sleep_time); 569a5aedd68SStacey Son 570a5aedd68SStacey Son /* 571a5aedd68SStacey Son * Only record the loops spinning and not sleeping. 572a5aedd68SStacey Son */ 573a5aedd68SStacey Son if (spin_cnt > sleep_cnt) 57432cd0147SMark Johnston LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time); 575a5aedd68SStacey Son #endif 5769ed346baSBosko Milekic } 5779ed346baSBosko Milekic 5782502c107SJeff Roberson static void 5792502c107SJeff Roberson _mtx_lock_spin_failed(struct mtx *m) 5802502c107SJeff Roberson { 5812502c107SJeff Roberson struct thread *td; 5822502c107SJeff Roberson 5832502c107SJeff Roberson td = mtx_owner(m); 5842502c107SJeff Roberson 5852502c107SJeff Roberson /* If the mutex is unlocked, try again. */ 5862502c107SJeff Roberson if (td == NULL) 5872502c107SJeff Roberson return; 588b95b98b0SKonstantin Belousov 5892502c107SJeff Roberson printf( "spin lock %p (%s) held by %p (tid %d) too long\n", 5902502c107SJeff Roberson m, m->lock_object.lo_name, td, td->td_tid); 5912502c107SJeff Roberson #ifdef WITNESS 59298332c8cSAttilio Rao witness_display_spinlock(&m->lock_object, td, printf); 5932502c107SJeff Roberson #endif 5942502c107SJeff Roberson panic("spin lock held too long"); 5952502c107SJeff Roberson } 5962502c107SJeff Roberson 597b95b98b0SKonstantin Belousov #ifdef SMP 5989ed346baSBosko Milekic /* 5997f44c618SAttilio Rao * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock. 6009ed346baSBosko Milekic * 6019ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 6029ed346baSBosko Milekic * is handled inline. 6039ed346baSBosko Milekic */ 6049ed346baSBosko Milekic void 6057f44c618SAttilio Rao _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t tid, int opts, 6067f44c618SAttilio Rao const char *file, int line) 60736412d79SJohn Baldwin { 6087f44c618SAttilio Rao struct mtx *m; 6091723a064SJeff Roberson int i = 0; 6101723a064SJeff Roberson #ifdef LOCK_PROFILING 6111723a064SJeff Roberson int contested = 0; 61270fe8436SKip Macy uint64_t waittime = 0; 6131723a064SJeff Roberson #endif 614076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 615076dd8ebSAndriy Gapon int64_t spin_time = 0; 616076dd8ebSAndriy Gapon #endif 61736412d79SJohn Baldwin 61835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 61935370593SAndriy Gapon return; 62035370593SAndriy Gapon 6217f44c618SAttilio Rao m = mtxlock2mtx(c); 6227f44c618SAttilio Rao 623aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 6245746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 6252cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 6262cba8dd3SJohn Baldwin "spinning", "lockname:\"%s\"", m->lock_object.lo_name); 6279ed346baSBosko Milekic 628f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 629f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 630f5f9340bSFabien Thomas #endif 63170fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 632076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 633e2b25737SMark Johnston spin_time -= lockstat_nsecs(&m->lock_object); 634076dd8ebSAndriy Gapon #endif 635fc4f686dSMateusz Guzik for (;;) { 636fc4f686dSMateusz Guzik if (m->mtx_lock == MTX_UNOWNED && _mtx_obtain_lock(m, tid)) 637fc4f686dSMateusz Guzik break; 6387141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 639c6a37e84SJohn Baldwin spinlock_exit(); 64036412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 641703fc290SJohn Baldwin if (i++ < 10000000) { 6429f1b87f1SMaxime Henrion cpu_spinwait(); 64336412d79SJohn Baldwin continue; 644703fc290SJohn Baldwin } 6450fa2168bSJohn Baldwin if (i < 60000000 || kdb_active || panicstr != NULL) 64636412d79SJohn Baldwin DELAY(1); 6472502c107SJeff Roberson else 6482502c107SJeff Roberson _mtx_lock_spin_failed(m); 6499f1b87f1SMaxime Henrion cpu_spinwait(); 65036412d79SJohn Baldwin } 651c6a37e84SJohn Baldwin spinlock_enter(); 65236412d79SJohn Baldwin } 653076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 654e2b25737SMark Johnston spin_time += lockstat_nsecs(&m->lock_object); 655076dd8ebSAndriy Gapon #endif 65636412d79SJohn Baldwin 657aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 6589ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 6592cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 6602cba8dd3SJohn Baldwin "running"); 6619ed346baSBosko Milekic 662c6d48c87SMark Johnston #ifdef KDTRACE_HOOKS 66332cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 66432cd0147SMark Johnston contested, waittime, file, line); 665e2b25737SMark Johnston if (spin_time != 0) 66632cd0147SMark Johnston LOCKSTAT_RECORD1(spin__spin, m, spin_time); 667c6d48c87SMark Johnston #endif 66836412d79SJohn Baldwin } 66933fb8a38SJohn Baldwin #endif /* SMP */ 67036412d79SJohn Baldwin 6712502c107SJeff Roberson void 672ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line) 6732502c107SJeff Roberson { 6742502c107SJeff Roberson struct mtx *m; 6752502c107SJeff Roberson uintptr_t tid; 6761723a064SJeff Roberson int i; 6771723a064SJeff Roberson #ifdef LOCK_PROFILING 6781723a064SJeff Roberson int contested = 0; 6791723a064SJeff Roberson uint64_t waittime = 0; 6801723a064SJeff Roberson #endif 681a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 682076dd8ebSAndriy Gapon int64_t spin_time = 0; 683a5aedd68SStacey Son #endif 6842502c107SJeff Roberson 6851723a064SJeff Roberson i = 0; 6862502c107SJeff Roberson tid = (uintptr_t)curthread; 68735370593SAndriy Gapon 688f61d6c5aSMark Johnston if (SCHEDULER_STOPPED()) { 689f61d6c5aSMark Johnston /* 690f61d6c5aSMark Johnston * Ensure that spinlock sections are balanced even when the 691f61d6c5aSMark Johnston * scheduler is stopped, since we may otherwise inadvertently 692f61d6c5aSMark Johnston * re-enable interrupts while dumping core. 693f61d6c5aSMark Johnston */ 694f61d6c5aSMark Johnston spinlock_enter(); 69535370593SAndriy Gapon return; 696f61d6c5aSMark Johnston } 69735370593SAndriy Gapon 698076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 699e2b25737SMark Johnston spin_time -= lockstat_nsecs(&td->td_lock->lock_object); 700076dd8ebSAndriy Gapon #endif 7012502c107SJeff Roberson for (;;) { 7022502c107SJeff Roberson retry: 7032502c107SJeff Roberson spinlock_enter(); 704710eacdcSJeff Roberson m = td->td_lock; 70513c85a48SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 70613c85a48SJohn Baldwin ("thread_lock() of destroyed mutex @ %s:%d", file, line)); 70713c85a48SJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 70813c85a48SJohn Baldwin ("thread_lock() of sleep mutex %s @ %s:%d", 70913c85a48SJohn Baldwin m->lock_object.lo_name, file, line)); 710ad69e26bSJohn Baldwin if (mtx_owned(m)) 711ad69e26bSJohn Baldwin KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 712ad69e26bSJohn Baldwin ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n", 713ad69e26bSJohn Baldwin m->lock_object.lo_name, file, line)); 7142502c107SJeff Roberson WITNESS_CHECKORDER(&m->lock_object, 71541313430SJohn Baldwin opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 716fc4f686dSMateusz Guzik for (;;) { 717fc4f686dSMateusz Guzik if (m->mtx_lock == MTX_UNOWNED && _mtx_obtain_lock(m, tid)) 718fc4f686dSMateusz Guzik break; 7192502c107SJeff Roberson if (m->mtx_lock == tid) { 7202502c107SJeff Roberson m->mtx_recurse++; 7212502c107SJeff Roberson break; 7222502c107SJeff Roberson } 723f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 724f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 725f5f9340bSFabien Thomas #endif 726eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&m->lock_object, 727eea4f254SJeff Roberson &contested, &waittime); 7282502c107SJeff Roberson /* Give interrupts a chance while we spin. */ 7292502c107SJeff Roberson spinlock_exit(); 7302502c107SJeff Roberson while (m->mtx_lock != MTX_UNOWNED) { 7312502c107SJeff Roberson if (i++ < 10000000) 7322502c107SJeff Roberson cpu_spinwait(); 7332502c107SJeff Roberson else if (i < 60000000 || 7342502c107SJeff Roberson kdb_active || panicstr != NULL) 7352502c107SJeff Roberson DELAY(1); 7362502c107SJeff Roberson else 7372502c107SJeff Roberson _mtx_lock_spin_failed(m); 7382502c107SJeff Roberson cpu_spinwait(); 7392502c107SJeff Roberson if (m != td->td_lock) 7402502c107SJeff Roberson goto retry; 7412502c107SJeff Roberson } 7422502c107SJeff Roberson spinlock_enter(); 7432502c107SJeff Roberson } 7442502c107SJeff Roberson if (m == td->td_lock) 7452502c107SJeff Roberson break; 746961135eaSJohn Baldwin __mtx_unlock_spin(m); /* does spinlock_exit() */ 7472502c107SJeff Roberson } 748076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 749e2b25737SMark Johnston spin_time += lockstat_nsecs(&m->lock_object); 750076dd8ebSAndriy Gapon #endif 751eea4f254SJeff Roberson if (m->mtx_recurse == 0) 75232cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 75332cd0147SMark Johnston contested, waittime, file, line); 75413c85a48SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 75513c85a48SJohn Baldwin line); 7562502c107SJeff Roberson WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 7575002e195SMark Johnston #ifdef KDTRACE_HOOKS 758156fbc14SMark Johnston if (spin_time != 0) 75932cd0147SMark Johnston LOCKSTAT_RECORD1(thread__spin, m, spin_time); 7605002e195SMark Johnston #endif 7612502c107SJeff Roberson } 7622502c107SJeff Roberson 7632502c107SJeff Roberson struct mtx * 7642502c107SJeff Roberson thread_lock_block(struct thread *td) 7652502c107SJeff Roberson { 7662502c107SJeff Roberson struct mtx *lock; 7672502c107SJeff Roberson 7682502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 769710eacdcSJeff Roberson lock = td->td_lock; 7702502c107SJeff Roberson td->td_lock = &blocked_lock; 7712502c107SJeff Roberson mtx_unlock_spin(lock); 7722502c107SJeff Roberson 7732502c107SJeff Roberson return (lock); 7742502c107SJeff Roberson } 7752502c107SJeff Roberson 7762502c107SJeff Roberson void 7772502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new) 7782502c107SJeff Roberson { 7792502c107SJeff Roberson mtx_assert(new, MA_OWNED); 7802502c107SJeff Roberson MPASS(td->td_lock == &blocked_lock); 78165d32cd8SMatt Jacob atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new); 7822502c107SJeff Roberson } 7832502c107SJeff Roberson 7842502c107SJeff Roberson void 7852502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new) 7862502c107SJeff Roberson { 7872502c107SJeff Roberson struct mtx *lock; 7882502c107SJeff Roberson 7892502c107SJeff Roberson mtx_assert(new, MA_OWNED); 7902502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 791710eacdcSJeff Roberson lock = td->td_lock; 7922502c107SJeff Roberson td->td_lock = new; 7932502c107SJeff Roberson mtx_unlock_spin(lock); 7942502c107SJeff Roberson } 7952502c107SJeff Roberson 7969ed346baSBosko Milekic /* 7977f44c618SAttilio Rao * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 7989ed346baSBosko Milekic * 7999ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 8009ed346baSBosko Milekic * need to wake up a blocked thread). 8019ed346baSBosko Milekic */ 80236412d79SJohn Baldwin void 8037f44c618SAttilio Rao __mtx_unlock_sleep(volatile uintptr_t *c, int opts, const char *file, int line) 80436412d79SJohn Baldwin { 8057f44c618SAttilio Rao struct mtx *m; 806961a7b24SJohn Baldwin struct turnstile *ts; 8079ed346baSBosko Milekic 80835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 80935370593SAndriy Gapon return; 81035370593SAndriy Gapon 8117f44c618SAttilio Rao m = mtxlock2mtx(c); 8127f44c618SAttilio Rao 81308812b39SBosko Milekic if (mtx_recursed(m)) { 81436412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 81508812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 816aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 8179ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 81836412d79SJohn Baldwin return; 81936412d79SJohn Baldwin } 8209ed346baSBosko Milekic 8212502c107SJeff Roberson /* 8222502c107SJeff Roberson * We have to lock the chain before the turnstile so this turnstile 8232502c107SJeff Roberson * can be removed from the hash list if it is empty. 8242502c107SJeff Roberson */ 8252502c107SJeff Roberson turnstile_chain_lock(&m->lock_object); 826aa89d8cdSJohn Baldwin ts = turnstile_lookup(&m->lock_object); 827aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 8289ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 829961a7b24SJohn Baldwin MPASS(ts != NULL); 8307aa4f685SJohn Baldwin turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 831961135eaSJohn Baldwin _mtx_release_lock_quick(m); 832bf9c6c31SJohn Baldwin 8332502c107SJeff Roberson /* 8342502c107SJeff Roberson * This turnstile is now no longer associated with the mutex. We can 8352502c107SJeff Roberson * unlock the chain lock so a new turnstile may take it's place. 8362502c107SJeff Roberson */ 8377aa4f685SJohn Baldwin turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 8382502c107SJeff Roberson turnstile_chain_unlock(&m->lock_object); 8399ed346baSBosko Milekic } 8409ed346baSBosko Milekic 8419ed346baSBosko Milekic /* 8429ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 843961135eaSJohn Baldwin * See the __mtx_unlock_spin() macro for the details. 8449ed346baSBosko Milekic */ 8459ed346baSBosko Milekic 8469ed346baSBosko Milekic /* 84715ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 8489ed346baSBosko Milekic */ 8491103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 8500cde2e34SJason Evans void 8517f44c618SAttilio Rao __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line) 8520cde2e34SJason Evans { 8537f44c618SAttilio Rao const struct mtx *m; 8545cb0fbe4SJohn Baldwin 8551126349aSPaul Saab if (panicstr != NULL || dumping) 8565cb0fbe4SJohn Baldwin return; 8577f44c618SAttilio Rao 8587f44c618SAttilio Rao m = mtxlock2mtx(c); 8597f44c618SAttilio Rao 860a10f4966SJake Burkholder switch (what) { 8610cde2e34SJason Evans case MA_OWNED: 8620cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 8630cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 864a10f4966SJake Burkholder if (!mtx_owned(m)) 8650cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 866aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 867a10f4966SJake Burkholder if (mtx_recursed(m)) { 868a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 8690cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 870aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 871a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 8720cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 873aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 8740cde2e34SJason Evans } 8750cde2e34SJason Evans break; 8760cde2e34SJason Evans case MA_NOTOWNED: 877a10f4966SJake Burkholder if (mtx_owned(m)) 8780cde2e34SJason Evans panic("mutex %s owned at %s:%d", 879aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 8800cde2e34SJason Evans break; 8810cde2e34SJason Evans default: 88256771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 8830cde2e34SJason Evans } 8840cde2e34SJason Evans } 8850cde2e34SJason Evans #endif 8860cde2e34SJason Evans 8879ed346baSBosko Milekic /* 888c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 889c27b5699SAndrew R. Reiter */ 890c27b5699SAndrew R. Reiter void 891c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 892c27b5699SAndrew R. Reiter { 893c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 894c27b5699SAndrew R. Reiter 8957f44c618SAttilio Rao mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL, 8967f44c618SAttilio Rao margs->ma_opts); 897c27b5699SAndrew R. Reiter } 898c27b5699SAndrew R. Reiter 899c27b5699SAndrew R. Reiter /* 9009ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 9010c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 9020c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 9030c88508aSJohn Baldwin * witness. 9049ed346baSBosko Milekic */ 90536412d79SJohn Baldwin void 9067f44c618SAttilio Rao _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts) 90736412d79SJohn Baldwin { 9087f44c618SAttilio Rao struct mtx *m; 90983a81bcbSJohn Baldwin struct lock_class *class; 91083a81bcbSJohn Baldwin int flags; 9119ed346baSBosko Milekic 9127f44c618SAttilio Rao m = mtxlock2mtx(c); 9137f44c618SAttilio Rao 91419284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 915fd07ddcfSDmitry Chagin MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0); 916353998acSAttilio Rao ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock, 917353998acSAttilio Rao ("%s: mtx_lock not aligned for %s: %p", __func__, name, 918353998acSAttilio Rao &m->mtx_lock)); 9199ed346baSBosko Milekic 92083a81bcbSJohn Baldwin /* Determine lock class and lock flags. */ 92119284646SJohn Baldwin if (opts & MTX_SPIN) 92283a81bcbSJohn Baldwin class = &lock_class_mtx_spin; 92319284646SJohn Baldwin else 92483a81bcbSJohn Baldwin class = &lock_class_mtx_sleep; 92583a81bcbSJohn Baldwin flags = 0; 92619284646SJohn Baldwin if (opts & MTX_QUIET) 92783a81bcbSJohn Baldwin flags |= LO_QUIET; 92819284646SJohn Baldwin if (opts & MTX_RECURSE) 92983a81bcbSJohn Baldwin flags |= LO_RECURSABLE; 93019284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 93183a81bcbSJohn Baldwin flags |= LO_WITNESS; 932f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 93383a81bcbSJohn Baldwin flags |= LO_DUPOK; 9347c0435b9SKip Macy if (opts & MTX_NOPROFILE) 9357c0435b9SKip Macy flags |= LO_NOPROFILE; 936fd07ddcfSDmitry Chagin if (opts & MTX_NEW) 937fd07ddcfSDmitry Chagin flags |= LO_NEW; 93819284646SJohn Baldwin 93983a81bcbSJohn Baldwin /* Initialize mutex. */ 940b5fb43e5SJohn Baldwin lock_init(&m->lock_object, class, name, type, flags); 941b5fb43e5SJohn Baldwin 94219284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 94383a81bcbSJohn Baldwin m->mtx_recurse = 0; 94436412d79SJohn Baldwin } 94536412d79SJohn Baldwin 9469ed346baSBosko Milekic /* 94719284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 94819284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 94919284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 95019284646SJohn Baldwin * flags. 9519ed346baSBosko Milekic */ 95236412d79SJohn Baldwin void 9537f44c618SAttilio Rao _mtx_destroy(volatile uintptr_t *c) 95436412d79SJohn Baldwin { 9557f44c618SAttilio Rao struct mtx *m; 9567f44c618SAttilio Rao 9577f44c618SAttilio Rao m = mtxlock2mtx(c); 95836412d79SJohn Baldwin 95919284646SJohn Baldwin if (!mtx_owned(m)) 96019284646SJohn Baldwin MPASS(mtx_unowned(m)); 96119284646SJohn Baldwin else { 96208812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 9639ed346baSBosko Milekic 964861a2308SScott Long /* Perform the non-mtx related part of mtx_unlock_spin(). */ 965aa89d8cdSJohn Baldwin if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) 966861a2308SScott Long spinlock_exit(); 967764e4d54SJohn Baldwin else 968ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 969861a2308SScott Long 970d3df4af3SJeff Roberson lock_profile_release_lock(&m->lock_object); 97119284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 972aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__, 973c86b6ff5SJohn Baldwin __LINE__); 97436412d79SJohn Baldwin } 9750384fff8SJason Evans 976186abbd7SJohn Baldwin m->mtx_lock = MTX_DESTROYED; 977aa89d8cdSJohn Baldwin lock_destroy(&m->lock_object); 9780384fff8SJason Evans } 979d23f5958SMatthew Dillon 980d23f5958SMatthew Dillon /* 981c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 982c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 983c53c013bSJohn Baldwin * setup before this is called. 984c53c013bSJohn Baldwin */ 985c53c013bSJohn Baldwin void 986c53c013bSJohn Baldwin mutex_init(void) 987c53c013bSJohn Baldwin { 988c53c013bSJohn Baldwin 989961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 990961a7b24SJohn Baldwin init_turnstiles(); 991961a7b24SJohn Baldwin 992c53c013bSJohn Baldwin /* 993c53c013bSJohn Baldwin * Initialize mutexes. 994c53c013bSJohn Baldwin */ 9950c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 9962502c107SJeff Roberson mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN); 9972502c107SJeff Roberson blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */ 9980c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 9996afb32fcSKonstantin Belousov mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN); 10005c7bebf9SKonstantin Belousov mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN); 10015c7bebf9SKonstantin Belousov mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN); 10025c7bebf9SKonstantin Belousov mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN); 10038c4b6380SJohn Baldwin mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 1004c53c013bSJohn Baldwin mtx_lock(&Giant); 1005c53c013bSJohn Baldwin } 1006d272fe53SJohn Baldwin 1007d272fe53SJohn Baldwin #ifdef DDB 1008d272fe53SJohn Baldwin void 1009d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock) 1010d272fe53SJohn Baldwin { 1011d272fe53SJohn Baldwin struct thread *td; 1012d576deedSPawel Jakub Dawidek const struct mtx *m; 1013d272fe53SJohn Baldwin 1014d576deedSPawel Jakub Dawidek m = (const struct mtx *)lock; 1015d272fe53SJohn Baldwin 1016d272fe53SJohn Baldwin db_printf(" flags: {"); 101783a81bcbSJohn Baldwin if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 1018d272fe53SJohn Baldwin db_printf("SPIN"); 1019d272fe53SJohn Baldwin else 1020d272fe53SJohn Baldwin db_printf("DEF"); 1021aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_RECURSABLE) 1022d272fe53SJohn Baldwin db_printf(", RECURSE"); 1023aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_DUPOK) 1024d272fe53SJohn Baldwin db_printf(", DUPOK"); 1025d272fe53SJohn Baldwin db_printf("}\n"); 1026d272fe53SJohn Baldwin db_printf(" state: {"); 1027d272fe53SJohn Baldwin if (mtx_unowned(m)) 1028d272fe53SJohn Baldwin db_printf("UNOWNED"); 1029c0bfd703SJohn Baldwin else if (mtx_destroyed(m)) 1030c0bfd703SJohn Baldwin db_printf("DESTROYED"); 1031d272fe53SJohn Baldwin else { 1032d272fe53SJohn Baldwin db_printf("OWNED"); 1033d272fe53SJohn Baldwin if (m->mtx_lock & MTX_CONTESTED) 1034d272fe53SJohn Baldwin db_printf(", CONTESTED"); 1035d272fe53SJohn Baldwin if (m->mtx_lock & MTX_RECURSED) 1036d272fe53SJohn Baldwin db_printf(", RECURSED"); 1037d272fe53SJohn Baldwin } 1038d272fe53SJohn Baldwin db_printf("}\n"); 1039c0bfd703SJohn Baldwin if (!mtx_unowned(m) && !mtx_destroyed(m)) { 1040d272fe53SJohn Baldwin td = mtx_owner(m); 1041d272fe53SJohn Baldwin db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 1042431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 1043d272fe53SJohn Baldwin if (mtx_recursed(m)) 1044d272fe53SJohn Baldwin db_printf(" recursed: %d\n", m->mtx_recurse); 1045d272fe53SJohn Baldwin } 1046d272fe53SJohn Baldwin } 1047d272fe53SJohn Baldwin #endif 1048