10384fff8SJason Evans /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a36da99SPedro F. Giffuni * 40384fff8SJason Evans * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 50384fff8SJason Evans * 60384fff8SJason Evans * Redistribution and use in source and binary forms, with or without 70384fff8SJason Evans * modification, are permitted provided that the following conditions 80384fff8SJason Evans * are met: 90384fff8SJason Evans * 1. Redistributions of source code must retain the above copyright 100384fff8SJason Evans * notice, this list of conditions and the following disclaimer. 110384fff8SJason Evans * 2. Redistributions in binary form must reproduce the above copyright 120384fff8SJason Evans * notice, this list of conditions and the following disclaimer in the 130384fff8SJason Evans * documentation and/or other materials provided with the distribution. 140384fff8SJason Evans * 3. Berkeley Software Design Inc's name may not be used to endorse or 150384fff8SJason Evans * promote products derived from this software without specific prior 160384fff8SJason Evans * written permission. 170384fff8SJason Evans * 180384fff8SJason Evans * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 190384fff8SJason Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 200384fff8SJason Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 210384fff8SJason Evans * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 220384fff8SJason Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 230384fff8SJason Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 240384fff8SJason Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 250384fff8SJason Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 260384fff8SJason Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 270384fff8SJason Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 280384fff8SJason Evans * SUCH DAMAGE. 290384fff8SJason Evans * 300384fff8SJason Evans * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 3136412d79SJohn Baldwin * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 320384fff8SJason Evans */ 330384fff8SJason Evans 340384fff8SJason Evans /* 35ba48b69aSJohn Baldwin * Machine independent bits of mutex implementation. 360384fff8SJason Evans */ 370384fff8SJason Evans 38677b542eSDavid E. O'Brien #include <sys/cdefs.h> 39677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 40677b542eSDavid E. O'Brien 412498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h" 429c36c934SJohn Baldwin #include "opt_ddb.h" 43f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h" 449923b511SScott Long #include "opt_sched.h" 45a5a96a19SJohn Baldwin 460384fff8SJason Evans #include <sys/param.h> 476c35e809SDag-Erling Smørgrav #include <sys/systm.h> 4836412d79SJohn Baldwin #include <sys/bus.h> 491126349aSPaul Saab #include <sys/conf.h> 502d50560aSMarcel Moolenaar #include <sys/kdb.h> 5136412d79SJohn Baldwin #include <sys/kernel.h> 526c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 5319284646SJohn Baldwin #include <sys/lock.h> 54fb919e4dSMark Murray #include <sys/malloc.h> 5519284646SJohn Baldwin #include <sys/mutex.h> 560384fff8SJason Evans #include <sys/proc.h> 57c4f7a187SJohn Baldwin #include <sys/resourcevar.h> 58b43179fbSJeff Roberson #include <sys/sched.h> 596c35e809SDag-Erling Smørgrav #include <sys/sbuf.h> 601ada9041SMateusz Guzik #include <sys/smp.h> 61a5a96a19SJohn Baldwin #include <sys/sysctl.h> 62961a7b24SJohn Baldwin #include <sys/turnstile.h> 6336412d79SJohn Baldwin #include <sys/vmmeter.h> 647c0435b9SKip Macy #include <sys/lock_profile.h> 650384fff8SJason Evans 6636412d79SJohn Baldwin #include <machine/atomic.h> 6736412d79SJohn Baldwin #include <machine/bus.h> 680384fff8SJason Evans #include <machine/cpu.h> 6936412d79SJohn Baldwin 709c36c934SJohn Baldwin #include <ddb/ddb.h> 719c36c934SJohn Baldwin 728c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h> 738c4b6380SJohn Baldwin 7436412d79SJohn Baldwin #include <vm/vm.h> 7536412d79SJohn Baldwin #include <vm/vm_extern.h> 7636412d79SJohn Baldwin 77cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 78cd6e6e4eSJohn Baldwin #define ADAPTIVE_MUTEXES 79cd6e6e4eSJohn Baldwin #endif 80cd6e6e4eSJohn Baldwin 81f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 82f5f9340bSFabien Thomas #include <sys/pmckern.h> 83f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , lock, failed); 84f5f9340bSFabien Thomas #endif 85f5f9340bSFabien Thomas 86b9a80acaSStephan Uphoff /* 877f44c618SAttilio Rao * Return the mutex address when the lock cookie address is provided. 887f44c618SAttilio Rao * This functionality assumes that struct mtx* have a member named mtx_lock. 897f44c618SAttilio Rao */ 907f44c618SAttilio Rao #define mtxlock2mtx(c) (__containerof(c, struct mtx, mtx_lock)) 917f44c618SAttilio Rao 927f44c618SAttilio Rao /* 939ed346baSBosko Milekic * Internal utility macros. 940cde2e34SJason Evans */ 959ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 960cde2e34SJason Evans 97c0bfd703SJohn Baldwin #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED) 98c0bfd703SJohn Baldwin 99d576deedSPawel Jakub Dawidek static void assert_mtx(const struct lock_object *lock, int what); 100d272fe53SJohn Baldwin #ifdef DDB 101d576deedSPawel Jakub Dawidek static void db_show_mtx(const struct lock_object *lock); 102d272fe53SJohn Baldwin #endif 1037faf4d90SDavide Italiano static void lock_mtx(struct lock_object *lock, uintptr_t how); 1047faf4d90SDavide Italiano static void lock_spin(struct lock_object *lock, uintptr_t how); 105a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 106d576deedSPawel Jakub Dawidek static int owner_mtx(const struct lock_object *lock, 107d576deedSPawel Jakub Dawidek struct thread **owner); 108a5aedd68SStacey Son #endif 1097faf4d90SDavide Italiano static uintptr_t unlock_mtx(struct lock_object *lock); 1107faf4d90SDavide Italiano static uintptr_t unlock_spin(struct lock_object *lock); 111d272fe53SJohn Baldwin 1120cde2e34SJason Evans /* 11319284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 1140cde2e34SJason Evans */ 11519284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 116ae8dde30SJohn Baldwin .lc_name = "sleep mutex", 117ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, 118f9721b43SAttilio Rao .lc_assert = assert_mtx, 119d272fe53SJohn Baldwin #ifdef DDB 120ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 121d272fe53SJohn Baldwin #endif 1226e21afd4SJohn Baldwin .lc_lock = lock_mtx, 1236e21afd4SJohn Baldwin .lc_unlock = unlock_mtx, 124a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 125a5aedd68SStacey Son .lc_owner = owner_mtx, 126a5aedd68SStacey Son #endif 12719284646SJohn Baldwin }; 12819284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 129ae8dde30SJohn Baldwin .lc_name = "spin mutex", 130ae8dde30SJohn Baldwin .lc_flags = LC_SPINLOCK | LC_RECURSABLE, 131f9721b43SAttilio Rao .lc_assert = assert_mtx, 132d272fe53SJohn Baldwin #ifdef DDB 133ae8dde30SJohn Baldwin .lc_ddb_show = db_show_mtx, 134d272fe53SJohn Baldwin #endif 1356e21afd4SJohn Baldwin .lc_lock = lock_spin, 1366e21afd4SJohn Baldwin .lc_unlock = unlock_spin, 137a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 138a5aedd68SStacey Son .lc_owner = owner_mtx, 139a5aedd68SStacey Son #endif 1408484de75SJohn Baldwin }; 1418484de75SJohn Baldwin 1421ada9041SMateusz Guzik #ifdef ADAPTIVE_MUTEXES 1431ada9041SMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging"); 1441ada9041SMateusz Guzik 145574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_delay; 1461ada9041SMateusz Guzik 1478e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base, 1481ada9041SMateusz Guzik 0, ""); 1491ada9041SMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max, 1501ada9041SMateusz Guzik 0, ""); 1511ada9041SMateusz Guzik 1528e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay); 1531ada9041SMateusz Guzik #endif 1541ada9041SMateusz Guzik 155a0d45f0fSMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL, 156a0d45f0fSMateusz Guzik "mtx spin debugging"); 157a0d45f0fSMateusz Guzik 158574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_spin_delay; 159a0d45f0fSMateusz Guzik 1608e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW, 1618e5a3e9aSMateusz Guzik &mtx_spin_delay.base, 0, ""); 1628e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW, 1638e5a3e9aSMateusz Guzik &mtx_spin_delay.max, 0, ""); 164a0d45f0fSMateusz Guzik 1658e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay); 166a0d45f0fSMateusz Guzik 1679ed346baSBosko Milekic /* 168c53c013bSJohn Baldwin * System-wide mutexes 169c53c013bSJohn Baldwin */ 1702502c107SJeff Roberson struct mtx blocked_lock; 1716a569d35SMateusz Guzik struct mtx __exclusive_cache_line Giant; 172c53c013bSJohn Baldwin 17315140a8aSMateusz Guzik static void _mtx_lock_indefinite_check(struct mtx *, struct lock_delay_arg *); 17415140a8aSMateusz Guzik 17567784314SPoul-Henning Kamp void 176d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what) 177f9721b43SAttilio Rao { 178f9721b43SAttilio Rao 179*a11bf9a4SXin LI /* 180*a11bf9a4SXin LI * Treat LA_LOCKED as if LA_XLOCKED was asserted. 181*a11bf9a4SXin LI * 182*a11bf9a4SXin LI * Some callers of lc_assert uses LA_LOCKED to indicate that either 183*a11bf9a4SXin LI * a shared lock or write lock was held, while other callers uses 184*a11bf9a4SXin LI * the more strict LA_XLOCKED (used as MA_OWNED). 185*a11bf9a4SXin LI * 186*a11bf9a4SXin LI * Mutex is the only lock class that can not be shared, as a result, 187*a11bf9a4SXin LI * we can reasonably consider the caller really intends to assert 188*a11bf9a4SXin LI * LA_XLOCKED when they are asserting LA_LOCKED on a mutex object. 189*a11bf9a4SXin LI */ 190*a11bf9a4SXin LI if (what & LA_LOCKED) { 191*a11bf9a4SXin LI what &= ~LA_LOCKED; 192*a11bf9a4SXin LI what |= LA_XLOCKED; 193*a11bf9a4SXin LI } 194d576deedSPawel Jakub Dawidek mtx_assert((const struct mtx *)lock, what); 195f9721b43SAttilio Rao } 196f9721b43SAttilio Rao 19767784314SPoul-Henning Kamp void 1987faf4d90SDavide Italiano lock_mtx(struct lock_object *lock, uintptr_t how) 1996e21afd4SJohn Baldwin { 2006e21afd4SJohn Baldwin 2016e21afd4SJohn Baldwin mtx_lock((struct mtx *)lock); 2026e21afd4SJohn Baldwin } 2036e21afd4SJohn Baldwin 20467784314SPoul-Henning Kamp void 2057faf4d90SDavide Italiano lock_spin(struct lock_object *lock, uintptr_t how) 2066e21afd4SJohn Baldwin { 2076e21afd4SJohn Baldwin 2086e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 2096e21afd4SJohn Baldwin } 2106e21afd4SJohn Baldwin 2117faf4d90SDavide Italiano uintptr_t 2126e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock) 2136e21afd4SJohn Baldwin { 2146e21afd4SJohn Baldwin struct mtx *m; 2156e21afd4SJohn Baldwin 2166e21afd4SJohn Baldwin m = (struct mtx *)lock; 2176e21afd4SJohn Baldwin mtx_assert(m, MA_OWNED | MA_NOTRECURSED); 2186e21afd4SJohn Baldwin mtx_unlock(m); 2196e21afd4SJohn Baldwin return (0); 2206e21afd4SJohn Baldwin } 2216e21afd4SJohn Baldwin 2227faf4d90SDavide Italiano uintptr_t 2236e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock) 2246e21afd4SJohn Baldwin { 2256e21afd4SJohn Baldwin 2266e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 2276e21afd4SJohn Baldwin } 2286e21afd4SJohn Baldwin 229a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 230a5aedd68SStacey Son int 231d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner) 232a5aedd68SStacey Son { 23302315a67SMark Johnston const struct mtx *m; 23402315a67SMark Johnston uintptr_t x; 235a5aedd68SStacey Son 23602315a67SMark Johnston m = (const struct mtx *)lock; 23702315a67SMark Johnston x = m->mtx_lock; 23802315a67SMark Johnston *owner = (struct thread *)(x & ~MTX_FLAGMASK); 239e280ce46SMateusz Guzik return (*owner != NULL); 240a5aedd68SStacey Son } 241a5aedd68SStacey Son #endif 242a5aedd68SStacey Son 2430cde2e34SJason Evans /* 2446283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 2456283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 2466283b7d0SJohn Baldwin */ 2476283b7d0SJohn Baldwin void 2487f44c618SAttilio Rao __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 2496283b7d0SJohn Baldwin { 2507f44c618SAttilio Rao struct mtx *m; 25108da2677SMateusz Guzik uintptr_t tid, v; 2526283b7d0SJohn Baldwin 2537f44c618SAttilio Rao m = mtxlock2mtx(c); 2547f44c618SAttilio Rao 255704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 256704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 257e3ae0dfeSAttilio Rao ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d", 258e3ae0dfeSAttilio Rao curthread, m->lock_object.lo_name, file, line)); 259186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 260186abbd7SJohn Baldwin ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); 261aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 262aa89d8cdSJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2630d975d63SJohn Baldwin file, line)); 264ac6b769bSAttilio Rao WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) | 265ac6b769bSAttilio Rao LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 2667c0435b9SKip Macy 26708da2677SMateusz Guzik tid = (uintptr_t)curthread; 26808da2677SMateusz Guzik v = MTX_UNOWNED; 26908da2677SMateusz Guzik if (!_mtx_obtain_lock_fetch(m, &v, tid)) 2702f1ddb89SMateusz Guzik _mtx_lock_sleep(m, v, opts, file, line); 27108da2677SMateusz Guzik else 27208da2677SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, 27308da2677SMateusz Guzik m, 0, 0, file, line); 274aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 275dde96c99SJohn Baldwin line); 276ac6b769bSAttilio Rao WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE, 277ac6b769bSAttilio Rao file, line); 278ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 2796283b7d0SJohn Baldwin } 2806283b7d0SJohn Baldwin 2816283b7d0SJohn Baldwin void 2827f44c618SAttilio Rao __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 2836283b7d0SJohn Baldwin { 2847f44c618SAttilio Rao struct mtx *m; 28535370593SAndriy Gapon 2867f44c618SAttilio Rao m = mtxlock2mtx(c); 2877f44c618SAttilio Rao 288186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 289186abbd7SJohn Baldwin ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); 290aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 291aa89d8cdSJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2920d975d63SJohn Baldwin file, line)); 293aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 294aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 2950d975d63SJohn Baldwin line); 29621377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 297c66d7606SKip Macy 298ffd5c94cSMateusz Guzik #ifdef LOCK_PROFILING 299b584eb2eSMateusz Guzik __mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line); 300ffd5c94cSMateusz Guzik #else 301ffd5c94cSMateusz Guzik __mtx_unlock(m, curthread, opts, file, line); 302ffd5c94cSMateusz Guzik #endif 303ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 3046283b7d0SJohn Baldwin } 3056283b7d0SJohn Baldwin 3066283b7d0SJohn Baldwin void 3077f44c618SAttilio Rao __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 3087f44c618SAttilio Rao int line) 3096283b7d0SJohn Baldwin { 3107f44c618SAttilio Rao struct mtx *m; 31162bf13cbSMateusz Guzik #ifdef SMP 3120d74fe26SMateusz Guzik uintptr_t tid, v; 31362bf13cbSMateusz Guzik #endif 3146283b7d0SJohn Baldwin 3157f44c618SAttilio Rao m = mtxlock2mtx(c); 3167f44c618SAttilio Rao 317186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 318186abbd7SJohn Baldwin ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); 319aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 3200d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 321aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 322ad69e26bSJohn Baldwin if (mtx_owned(m)) 323ac6b769bSAttilio Rao KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 324ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0, 325ad69e26bSJohn Baldwin ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n", 326ad69e26bSJohn Baldwin m->lock_object.lo_name, file, line)); 327ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 328aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 32941313430SJohn Baldwin file, line, NULL); 33062bf13cbSMateusz Guzik #ifdef SMP 3310d74fe26SMateusz Guzik spinlock_enter(); 3320d74fe26SMateusz Guzik tid = (uintptr_t)curthread; 3330d74fe26SMateusz Guzik v = MTX_UNOWNED; 3340d74fe26SMateusz Guzik if (!_mtx_obtain_lock_fetch(m, &v, tid)) 3350d74fe26SMateusz Guzik _mtx_lock_spin(m, v, opts, file, line); 3360d74fe26SMateusz Guzik else 3370d74fe26SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, 3380d74fe26SMateusz Guzik m, 0, 0, file, line); 33962bf13cbSMateusz Guzik #else 34062bf13cbSMateusz Guzik __mtx_lock_spin(m, curthread, opts, file, line); 34162bf13cbSMateusz Guzik #endif 342aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 343dde96c99SJohn Baldwin line); 344aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 3456283b7d0SJohn Baldwin } 3466283b7d0SJohn Baldwin 34790b581f2SKonstantin Belousov int 34890b581f2SKonstantin Belousov __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 34990b581f2SKonstantin Belousov int line) 35090b581f2SKonstantin Belousov { 35190b581f2SKonstantin Belousov struct mtx *m; 35290b581f2SKonstantin Belousov 35390b581f2SKonstantin Belousov if (SCHEDULER_STOPPED()) 35490b581f2SKonstantin Belousov return (1); 35590b581f2SKonstantin Belousov 35690b581f2SKonstantin Belousov m = mtxlock2mtx(c); 35790b581f2SKonstantin Belousov 35890b581f2SKonstantin Belousov KASSERT(m->mtx_lock != MTX_DESTROYED, 35990b581f2SKonstantin Belousov ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line)); 36090b581f2SKonstantin Belousov KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 36190b581f2SKonstantin Belousov ("mtx_trylock_spin() of sleep mutex %s @ %s:%d", 36290b581f2SKonstantin Belousov m->lock_object.lo_name, file, line)); 36390b581f2SKonstantin Belousov KASSERT((opts & MTX_RECURSE) == 0, 36490b581f2SKonstantin Belousov ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n", 36590b581f2SKonstantin Belousov m->lock_object.lo_name, file, line)); 36690b581f2SKonstantin Belousov if (__mtx_trylock_spin(m, curthread, opts, file, line)) { 36790b581f2SKonstantin Belousov LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line); 36890b581f2SKonstantin Belousov WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 36990b581f2SKonstantin Belousov return (1); 37090b581f2SKonstantin Belousov } 37190b581f2SKonstantin Belousov LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line); 37290b581f2SKonstantin Belousov return (0); 37390b581f2SKonstantin Belousov } 37490b581f2SKonstantin Belousov 3756283b7d0SJohn Baldwin void 3767f44c618SAttilio Rao __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 3777f44c618SAttilio Rao int line) 3786283b7d0SJohn Baldwin { 3797f44c618SAttilio Rao struct mtx *m; 380c66d7606SKip Macy 3817f44c618SAttilio Rao m = mtxlock2mtx(c); 3827f44c618SAttilio Rao 383186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 384186abbd7SJohn Baldwin ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); 385aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 3860d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 387aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 388aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 389aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 390dde96c99SJohn Baldwin line); 3910d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 392c66d7606SKip Macy 393961135eaSJohn Baldwin __mtx_unlock_spin(m); 3946283b7d0SJohn Baldwin } 3956283b7d0SJohn Baldwin 3966283b7d0SJohn Baldwin /* 3979ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 398eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 399eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 4000cde2e34SJason Evans */ 4010cde2e34SJason Evans int 402013c0b49SMateusz Guzik _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF) 4030cde2e34SJason Evans { 4045c5df0d9SMateusz Guzik struct thread *td; 4055c5df0d9SMateusz Guzik uintptr_t tid, v; 4061723a064SJeff Roberson #ifdef LOCK_PROFILING 4077c0435b9SKip Macy uint64_t waittime = 0; 4081723a064SJeff Roberson int contested = 0; 4091723a064SJeff Roberson #endif 4101723a064SJeff Roberson int rval; 4115c5df0d9SMateusz Guzik bool recursed; 4120cde2e34SJason Evans 4135c5df0d9SMateusz Guzik td = curthread; 4145c5df0d9SMateusz Guzik tid = (uintptr_t)td; 4155c5df0d9SMateusz Guzik if (SCHEDULER_STOPPED_TD(td)) 41635370593SAndriy Gapon return (1); 41735370593SAndriy Gapon 418704cb42fSMark Johnston KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td), 419e3ae0dfeSAttilio Rao ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d", 420e3ae0dfeSAttilio Rao curthread, m->lock_object.lo_name, file, line)); 421186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 422186abbd7SJohn Baldwin ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); 423aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 424aa89d8cdSJohn Baldwin ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 42583cece6fSJohn Baldwin file, line)); 4269ed346baSBosko Milekic 4275c5df0d9SMateusz Guzik rval = 1; 4285c5df0d9SMateusz Guzik recursed = false; 4295c5df0d9SMateusz Guzik v = MTX_UNOWNED; 430b247fd39SMateusz Guzik for (;;) { 431b247fd39SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 432b247fd39SMateusz Guzik break; 433b247fd39SMateusz Guzik if (v == MTX_UNOWNED) 434b247fd39SMateusz Guzik continue; 4355c5df0d9SMateusz Guzik if (v == tid && 4365c5df0d9SMateusz Guzik ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 437ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0)) { 438eac09796SJohn Baldwin m->mtx_recurse++; 439eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 4405c5df0d9SMateusz Guzik recursed = true; 441b247fd39SMateusz Guzik break; 4425c5df0d9SMateusz Guzik } 443b247fd39SMateusz Guzik rval = 0; 444b247fd39SMateusz Guzik break; 4455c5df0d9SMateusz Guzik } 4465c5df0d9SMateusz Guzik 447ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 4489ed346baSBosko Milekic 449aa89d8cdSJohn Baldwin LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line); 450764e4d54SJohn Baldwin if (rval) { 451aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4522d96f0b1SJohn Baldwin file, line); 453ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 4545c5df0d9SMateusz Guzik if (!recursed) 45532cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, 456a5aedd68SStacey Son m, contested, waittime, file, line); 457764e4d54SJohn Baldwin } 4589ed346baSBosko Milekic 45919284646SJohn Baldwin return (rval); 4600cde2e34SJason Evans } 4610cde2e34SJason Evans 462013c0b49SMateusz Guzik int 463013c0b49SMateusz Guzik _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line) 464013c0b49SMateusz Guzik { 465013c0b49SMateusz Guzik struct mtx *m; 466013c0b49SMateusz Guzik 467013c0b49SMateusz Guzik m = mtxlock2mtx(c); 468013c0b49SMateusz Guzik return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG)); 469013c0b49SMateusz Guzik } 470013c0b49SMateusz Guzik 4710cde2e34SJason Evans /* 4727f44c618SAttilio Rao * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4739ed346baSBosko Milekic * 4749ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4759ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4760cde2e34SJason Evans */ 47709f1319aSMateusz Guzik #if LOCK_DEBUG > 0 4780cde2e34SJason Evans void 4792f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file, 4802f1ddb89SMateusz Guzik int line) 48109f1319aSMateusz Guzik #else 48209f1319aSMateusz Guzik void 4832f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v) 48409f1319aSMateusz Guzik #endif 48536412d79SJohn Baldwin { 4862f1ddb89SMateusz Guzik struct thread *td; 4877f44c618SAttilio Rao struct mtx *m; 4882502c107SJeff Roberson struct turnstile *ts; 4892f1ddb89SMateusz Guzik uintptr_t tid; 4902ccee9ccSMateusz Guzik struct thread *owner; 4911723a064SJeff Roberson #ifdef LOCK_PROFILING 49270fe8436SKip Macy int contested = 0; 49370fe8436SKip Macy uint64_t waittime = 0; 4941723a064SJeff Roberson #endif 4951ada9041SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS) 4961ada9041SMateusz Guzik struct lock_delay_arg lda; 4971ada9041SMateusz Guzik #endif 498a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 49961852185SMateusz Guzik u_int sleep_cnt = 0; 500a5aedd68SStacey Son int64_t sleep_time = 0; 501076dd8ebSAndriy Gapon int64_t all_time = 0; 502dfaa7859SMateusz Guzik #endif 503dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 504f183fb16SMateusz Guzik int doing_lockprof = 0; 505a5aedd68SStacey Son #endif 50609bdec20SMateusz Guzik 5072f1ddb89SMateusz Guzik td = curthread; 5082f1ddb89SMateusz Guzik tid = (uintptr_t)td; 50909bdec20SMateusz Guzik m = mtxlock2mtx(c); 51009bdec20SMateusz Guzik 51109bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS 51209bdec20SMateusz Guzik if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) { 51309bdec20SMateusz Guzik while (v == MTX_UNOWNED) { 51409bdec20SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 51509bdec20SMateusz Guzik goto out_lockstat; 51609bdec20SMateusz Guzik } 51709bdec20SMateusz Guzik doing_lockprof = 1; 51809bdec20SMateusz Guzik all_time -= lockstat_nsecs(&m->lock_object); 51909bdec20SMateusz Guzik } 52009bdec20SMateusz Guzik #endif 52109bdec20SMateusz Guzik #ifdef LOCK_PROFILING 52209bdec20SMateusz Guzik doing_lockprof = 1; 52309bdec20SMateusz Guzik #endif 52409bdec20SMateusz Guzik 5252f1ddb89SMateusz Guzik if (SCHEDULER_STOPPED_TD(td)) 52635370593SAndriy Gapon return; 52735370593SAndriy Gapon 528fa5000a4SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) 5291ada9041SMateusz Guzik lock_delay_arg_init(&lda, &mtx_delay); 530fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 531fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 5321ada9041SMateusz Guzik #endif 53309bdec20SMateusz Guzik 534c1aaf63cSMateusz Guzik if (__predict_false(v == MTX_UNOWNED)) 535c1aaf63cSMateusz Guzik v = MTX_READ_VALUE(m); 5367f44c618SAttilio Rao 5372f1ddb89SMateusz Guzik if (__predict_false(lv_mtx_owner(v) == td)) { 538ac6b769bSAttilio Rao KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 539ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0, 540eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 541aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 542a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0 543ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 544a24c8eb8SMateusz Guzik #endif 54536412d79SJohn Baldwin m->mtx_recurse++; 54608812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 547aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 5485746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 54936412d79SJohn Baldwin return; 55036412d79SJohn Baldwin } 551a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0 552ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 553a24c8eb8SMateusz Guzik #endif 5549ed346baSBosko Milekic 555f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 556f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 557f5f9340bSFabien Thomas #endif 55870fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, 55970fe8436SKip Macy &contested, &waittime); 560aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 56115ec816aSJohn Baldwin CTR4(KTR_LOCK, 56215ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 563aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)m->mtx_lock, file, line); 5641bd0eefbSJohn Baldwin 565fc4f686dSMateusz Guzik for (;;) { 5662604eb9eSMateusz Guzik if (v == MTX_UNOWNED) { 56790836c32SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 568fc4f686dSMateusz Guzik break; 5692604eb9eSMateusz Guzik continue; 5702604eb9eSMateusz Guzik } 571a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 5721ada9041SMateusz Guzik lda.spin_cnt++; 573a5aedd68SStacey Son #endif 57449aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 57549aead8aSAttilio Rao /* 57649aead8aSAttilio Rao * If the owner is running on another CPU, spin until the 57749aead8aSAttilio Rao * owner stops running or the state of the lock changes. 57849aead8aSAttilio Rao */ 5792604eb9eSMateusz Guzik owner = lv_mtx_owner(v); 58049aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 58149aead8aSAttilio Rao if (LOCK_LOG_TEST(&m->lock_object, 0)) 58249aead8aSAttilio Rao CTR3(KTR_LOCK, 58349aead8aSAttilio Rao "%s: spinning on %p held by %p", 58449aead8aSAttilio Rao __func__, m, owner); 5852cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 5862cba8dd3SJohn Baldwin sched_tdname((struct thread *)tid), 5872cba8dd3SJohn Baldwin "spinning", "lockname:\"%s\"", 5882cba8dd3SJohn Baldwin m->lock_object.lo_name); 5892604eb9eSMateusz Guzik do { 5901ada9041SMateusz Guzik lock_delay(&lda); 591391df78aSMateusz Guzik v = MTX_READ_VALUE(m); 5922604eb9eSMateusz Guzik owner = lv_mtx_owner(v); 5932604eb9eSMateusz Guzik } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner)); 5942cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 5952cba8dd3SJohn Baldwin sched_tdname((struct thread *)tid), 5962cba8dd3SJohn Baldwin "running"); 59749aead8aSAttilio Rao continue; 59849aead8aSAttilio Rao } 59949aead8aSAttilio Rao #endif 60049aead8aSAttilio Rao 6012502c107SJeff Roberson ts = turnstile_trywait(&m->lock_object); 6022604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 603310f24d7SMateusz Guzik retry_turnstile: 6045fa8dd90SJohn Baldwin 60536412d79SJohn Baldwin /* 6069ed346baSBosko Milekic * Check if the lock has been released while spinning for 607961a7b24SJohn Baldwin * the turnstile chain lock. 60836412d79SJohn Baldwin */ 6095fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 6102502c107SJeff Roberson turnstile_cancel(ts); 61136412d79SJohn Baldwin continue; 61236412d79SJohn Baldwin } 6139ed346baSBosko Milekic 61449aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 61549aead8aSAttilio Rao /* 616fa29f023SJohn Baldwin * The current lock owner might have started executing 617fa29f023SJohn Baldwin * on another CPU (or the lock could have changed 618fa29f023SJohn Baldwin * owners) while we were waiting on the turnstile 619fa29f023SJohn Baldwin * chain lock. If so, drop the turnstile lock and try 620fa29f023SJohn Baldwin * again. 62149aead8aSAttilio Rao */ 6222604eb9eSMateusz Guzik owner = lv_mtx_owner(v); 62349aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 62449aead8aSAttilio Rao turnstile_cancel(ts); 62549aead8aSAttilio Rao continue; 62649aead8aSAttilio Rao } 62749aead8aSAttilio Rao #endif 62849aead8aSAttilio Rao 62936412d79SJohn Baldwin /* 6309ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 6319ed346baSBosko Milekic * setting the contested bit, the mutex was either released 6329ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 63336412d79SJohn Baldwin */ 63436412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 635310f24d7SMateusz Guzik !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) { 636310f24d7SMateusz Guzik goto retry_turnstile; 63736412d79SJohn Baldwin } 63836412d79SJohn Baldwin 6399ed346baSBosko Milekic /* 6407feefcd6SJohn Baldwin * We definitely must sleep for this lock. 6419ed346baSBosko Milekic */ 64236412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 64336412d79SJohn Baldwin 6449ed346baSBosko Milekic /* 645961a7b24SJohn Baldwin * Block on the turnstile. 6469ed346baSBosko Milekic */ 647a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 648e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&m->lock_object); 649a5aedd68SStacey Son #endif 650284194f1SMateusz Guzik #ifndef ADAPTIVE_MUTEXES 651284194f1SMateusz Guzik owner = mtx_owner(m); 652284194f1SMateusz Guzik #endif 6538448e020SMateusz Guzik MPASS(owner == mtx_owner(m)); 6548448e020SMateusz Guzik turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE); 655a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 656e2b25737SMark Johnston sleep_time += lockstat_nsecs(&m->lock_object); 657a5aedd68SStacey Son sleep_cnt++; 658a5aedd68SStacey Son #endif 6592604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 66036412d79SJohn Baldwin } 661dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 662dfaa7859SMateusz Guzik if (__predict_true(!doing_lockprof)) 6637640beb9SMateusz Guzik return; 664dfaa7859SMateusz Guzik #endif 665dfaa7859SMateusz Guzik #ifdef KDTRACE_HOOKS 6667640beb9SMateusz Guzik all_time += lockstat_nsecs(&m->lock_object); 667a5aedd68SStacey Son if (sleep_time) 66832cd0147SMark Johnston LOCKSTAT_RECORD1(adaptive__block, m, sleep_time); 669a5aedd68SStacey Son 670a5aedd68SStacey Son /* 671a5aedd68SStacey Son * Only record the loops spinning and not sleeping. 672a5aedd68SStacey Son */ 6731ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 67432cd0147SMark Johnston LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time); 67509bdec20SMateusz Guzik out_lockstat: 676a5aedd68SStacey Son #endif 67709bdec20SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested, 67809bdec20SMateusz Guzik waittime, file, line); 6799ed346baSBosko Milekic } 6809ed346baSBosko Milekic 681b95b98b0SKonstantin Belousov #ifdef SMP 6829ed346baSBosko Milekic /* 6837f44c618SAttilio Rao * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock. 6849ed346baSBosko Milekic * 6859ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 6869ed346baSBosko Milekic * is handled inline. 6879ed346baSBosko Milekic */ 6880d74fe26SMateusz Guzik #if LOCK_DEBUG > 0 6899ed346baSBosko Milekic void 6900d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts, 6910d74fe26SMateusz Guzik const char *file, int line) 6920d74fe26SMateusz Guzik #else 6930d74fe26SMateusz Guzik void 6940d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v) 6950d74fe26SMateusz Guzik #endif 69636412d79SJohn Baldwin { 6977f44c618SAttilio Rao struct mtx *m; 698a0d45f0fSMateusz Guzik struct lock_delay_arg lda; 6990d74fe26SMateusz Guzik uintptr_t tid; 7001723a064SJeff Roberson #ifdef LOCK_PROFILING 7011723a064SJeff Roberson int contested = 0; 70270fe8436SKip Macy uint64_t waittime = 0; 7031723a064SJeff Roberson #endif 704076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 705076dd8ebSAndriy Gapon int64_t spin_time = 0; 706076dd8ebSAndriy Gapon #endif 707dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 708f183fb16SMateusz Guzik int doing_lockprof = 0; 709dfaa7859SMateusz Guzik #endif 71036412d79SJohn Baldwin 7110d74fe26SMateusz Guzik tid = (uintptr_t)curthread; 7127f44c618SAttilio Rao m = mtxlock2mtx(c); 7137f44c618SAttilio Rao 71409bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS 71509bdec20SMateusz Guzik if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) { 71609bdec20SMateusz Guzik while (v == MTX_UNOWNED) { 71709bdec20SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 71809bdec20SMateusz Guzik goto out_lockstat; 71909bdec20SMateusz Guzik } 72009bdec20SMateusz Guzik doing_lockprof = 1; 72109bdec20SMateusz Guzik spin_time -= lockstat_nsecs(&m->lock_object); 72209bdec20SMateusz Guzik } 72309bdec20SMateusz Guzik #endif 72409bdec20SMateusz Guzik #ifdef LOCK_PROFILING 72509bdec20SMateusz Guzik doing_lockprof = 1; 72609bdec20SMateusz Guzik #endif 72709bdec20SMateusz Guzik 72813d2ef0fSMateusz Guzik if (__predict_false(v == MTX_UNOWNED)) 72913d2ef0fSMateusz Guzik v = MTX_READ_VALUE(m); 73013d2ef0fSMateusz Guzik 73113d2ef0fSMateusz Guzik if (__predict_false(v == tid)) { 73213d2ef0fSMateusz Guzik m->mtx_recurse++; 73313d2ef0fSMateusz Guzik return; 73413d2ef0fSMateusz Guzik } 73513d2ef0fSMateusz Guzik 7360d74fe26SMateusz Guzik if (SCHEDULER_STOPPED()) 7370d74fe26SMateusz Guzik return; 7380d74fe26SMateusz Guzik 7390d74fe26SMateusz Guzik lock_delay_arg_init(&lda, &mtx_spin_delay); 7400d74fe26SMateusz Guzik 741aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 7425746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 7432cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 7442cba8dd3SJohn Baldwin "spinning", "lockname:\"%s\"", m->lock_object.lo_name); 7459ed346baSBosko Milekic 746f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 747f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 748f5f9340bSFabien Thomas #endif 74970fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 75009bdec20SMateusz Guzik 751fc4f686dSMateusz Guzik for (;;) { 7522604eb9eSMateusz Guzik if (v == MTX_UNOWNED) { 75390836c32SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 754fc4f686dSMateusz Guzik break; 75536412d79SJohn Baldwin continue; 756703fc290SJohn Baldwin } 7572604eb9eSMateusz Guzik /* Give interrupts a chance while we spin. */ 7582604eb9eSMateusz Guzik spinlock_exit(); 7592604eb9eSMateusz Guzik do { 76015140a8aSMateusz Guzik if (__predict_true(lda.spin_cnt < 10000000)) { 7612604eb9eSMateusz Guzik lock_delay(&lda); 7622604eb9eSMateusz Guzik } else { 76315140a8aSMateusz Guzik _mtx_lock_indefinite_check(m, &lda); 76436412d79SJohn Baldwin } 7652604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 7662604eb9eSMateusz Guzik } while (v != MTX_UNOWNED); 767c6a37e84SJohn Baldwin spinlock_enter(); 76836412d79SJohn Baldwin } 76936412d79SJohn Baldwin 770aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 7719ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 7722cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 7732cba8dd3SJohn Baldwin "running"); 7749ed346baSBosko Milekic 775dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 776dfaa7859SMateusz Guzik if (__predict_true(!doing_lockprof)) 777dfaa7859SMateusz Guzik return; 778dfaa7859SMateusz Guzik #endif 779c6d48c87SMark Johnston #ifdef KDTRACE_HOOKS 780dfaa7859SMateusz Guzik spin_time += lockstat_nsecs(&m->lock_object); 78109bdec20SMateusz Guzik if (lda.spin_cnt != 0) 78209bdec20SMateusz Guzik LOCKSTAT_RECORD1(spin__spin, m, spin_time); 78309bdec20SMateusz Guzik out_lockstat: 784dfaa7859SMateusz Guzik #endif 78532cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 78632cd0147SMark Johnston contested, waittime, file, line); 78736412d79SJohn Baldwin } 78833fb8a38SJohn Baldwin #endif /* SMP */ 78936412d79SJohn Baldwin 790be49509eSMateusz Guzik #ifdef INVARIANTS 791be49509eSMateusz Guzik static void 792be49509eSMateusz Guzik thread_lock_validate(struct mtx *m, int opts, const char *file, int line) 793be49509eSMateusz Guzik { 794be49509eSMateusz Guzik 795be49509eSMateusz Guzik KASSERT(m->mtx_lock != MTX_DESTROYED, 796be49509eSMateusz Guzik ("thread_lock() of destroyed mutex @ %s:%d", file, line)); 797be49509eSMateusz Guzik KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 798be49509eSMateusz Guzik ("thread_lock() of sleep mutex %s @ %s:%d", 799be49509eSMateusz Guzik m->lock_object.lo_name, file, line)); 800be49509eSMateusz Guzik if (mtx_owned(m)) 801be49509eSMateusz Guzik KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 802be49509eSMateusz Guzik ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n", 803be49509eSMateusz Guzik m->lock_object.lo_name, file, line)); 804be49509eSMateusz Guzik WITNESS_CHECKORDER(&m->lock_object, 805be49509eSMateusz Guzik opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 806be49509eSMateusz Guzik } 807be49509eSMateusz Guzik #else 808be49509eSMateusz Guzik #define thread_lock_validate(m, opts, file, line) do { } while (0) 809be49509eSMateusz Guzik #endif 810be49509eSMateusz Guzik 811be49509eSMateusz Guzik #ifndef LOCK_PROFILING 812be49509eSMateusz Guzik #if LOCK_DEBUG > 0 813be49509eSMateusz Guzik void 814be49509eSMateusz Guzik _thread_lock(struct thread *td, int opts, const char *file, int line) 815be49509eSMateusz Guzik #else 816be49509eSMateusz Guzik void 817be49509eSMateusz Guzik _thread_lock(struct thread *td) 818be49509eSMateusz Guzik #endif 819be49509eSMateusz Guzik { 820be49509eSMateusz Guzik struct mtx *m; 821be49509eSMateusz Guzik uintptr_t tid, v; 822be49509eSMateusz Guzik 823be49509eSMateusz Guzik tid = (uintptr_t)curthread; 824be49509eSMateusz Guzik 8252c50bafeSMateusz Guzik if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire))) 8262c50bafeSMateusz Guzik goto slowpath_noirq; 827be49509eSMateusz Guzik spinlock_enter(); 828be49509eSMateusz Guzik m = td->td_lock; 829be49509eSMateusz Guzik thread_lock_validate(m, 0, file, line); 830be49509eSMateusz Guzik v = MTX_READ_VALUE(m); 831be49509eSMateusz Guzik if (__predict_true(v == MTX_UNOWNED)) { 832be49509eSMateusz Guzik if (__predict_false(!_mtx_obtain_lock(m, tid))) 833be49509eSMateusz Guzik goto slowpath_unlocked; 834be49509eSMateusz Guzik } else if (v == tid) { 835be49509eSMateusz Guzik m->mtx_recurse++; 836be49509eSMateusz Guzik } else 837be49509eSMateusz Guzik goto slowpath_unlocked; 838be49509eSMateusz Guzik if (__predict_true(m == td->td_lock)) { 839be49509eSMateusz Guzik WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line); 840be49509eSMateusz Guzik return; 841be49509eSMateusz Guzik } 8429f4e008dSMateusz Guzik MPASS(m->mtx_recurse == 0); 843be49509eSMateusz Guzik _mtx_release_lock_quick(m); 844be49509eSMateusz Guzik slowpath_unlocked: 845be49509eSMateusz Guzik spinlock_exit(); 8462c50bafeSMateusz Guzik slowpath_noirq: 8472c50bafeSMateusz Guzik #if LOCK_DEBUG > 0 8482c50bafeSMateusz Guzik thread_lock_flags_(td, opts, file, line); 8492c50bafeSMateusz Guzik #else 850be49509eSMateusz Guzik thread_lock_flags_(td, 0, 0, 0); 8512c50bafeSMateusz Guzik #endif 852be49509eSMateusz Guzik } 853be49509eSMateusz Guzik #endif 854be49509eSMateusz Guzik 8552502c107SJeff Roberson void 856ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line) 8572502c107SJeff Roberson { 8582502c107SJeff Roberson struct mtx *m; 8595e5ad162SMateusz Guzik uintptr_t tid, v; 860a0d45f0fSMateusz Guzik struct lock_delay_arg lda; 8611723a064SJeff Roberson #ifdef LOCK_PROFILING 8621723a064SJeff Roberson int contested = 0; 8631723a064SJeff Roberson uint64_t waittime = 0; 8641723a064SJeff Roberson #endif 865a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 866076dd8ebSAndriy Gapon int64_t spin_time = 0; 867a5aedd68SStacey Son #endif 868dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 869dfaa7859SMateusz Guzik int doing_lockprof = 1; 870dfaa7859SMateusz Guzik #endif 8712502c107SJeff Roberson 8722502c107SJeff Roberson tid = (uintptr_t)curthread; 87335370593SAndriy Gapon 874f61d6c5aSMark Johnston if (SCHEDULER_STOPPED()) { 875f61d6c5aSMark Johnston /* 876f61d6c5aSMark Johnston * Ensure that spinlock sections are balanced even when the 877f61d6c5aSMark Johnston * scheduler is stopped, since we may otherwise inadvertently 878f61d6c5aSMark Johnston * re-enable interrupts while dumping core. 879f61d6c5aSMark Johnston */ 880f61d6c5aSMark Johnston spinlock_enter(); 88135370593SAndriy Gapon return; 882f61d6c5aSMark Johnston } 88335370593SAndriy Gapon 884a0d45f0fSMateusz Guzik lock_delay_arg_init(&lda, &mtx_spin_delay); 885a0d45f0fSMateusz Guzik 8861f4d28c7SMateusz Guzik #ifdef HWPMC_HOOKS 8871f4d28c7SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 8881f4d28c7SMateusz Guzik #endif 8891f4d28c7SMateusz Guzik 890dfaa7859SMateusz Guzik #ifdef LOCK_PROFILING 891dfaa7859SMateusz Guzik doing_lockprof = 1; 892df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS) 893dfaa7859SMateusz Guzik doing_lockprof = lockstat_enabled; 894dfaa7859SMateusz Guzik if (__predict_false(doing_lockprof)) 895e2b25737SMark Johnston spin_time -= lockstat_nsecs(&td->td_lock->lock_object); 896076dd8ebSAndriy Gapon #endif 8979f4e008dSMateusz Guzik spinlock_enter(); 8989f4e008dSMateusz Guzik 8992502c107SJeff Roberson for (;;) { 9002502c107SJeff Roberson retry: 901710eacdcSJeff Roberson m = td->td_lock; 902be49509eSMateusz Guzik thread_lock_validate(m, opts, file, line); 9031f4d28c7SMateusz Guzik v = MTX_READ_VALUE(m); 904fc4f686dSMateusz Guzik for (;;) { 9051f4d28c7SMateusz Guzik if (v == MTX_UNOWNED) { 90690836c32SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 907fc4f686dSMateusz Guzik break; 9085e5ad162SMateusz Guzik continue; 9091f4d28c7SMateusz Guzik } 9105e5ad162SMateusz Guzik if (v == tid) { 9112502c107SJeff Roberson m->mtx_recurse++; 9129f4e008dSMateusz Guzik MPASS(m == td->td_lock); 9132502c107SJeff Roberson break; 9142502c107SJeff Roberson } 915eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&m->lock_object, 916eea4f254SJeff Roberson &contested, &waittime); 9172502c107SJeff Roberson /* Give interrupts a chance while we spin. */ 9182502c107SJeff Roberson spinlock_exit(); 9195e5ad162SMateusz Guzik do { 92015140a8aSMateusz Guzik if (__predict_true(lda.spin_cnt < 10000000)) { 921a0d45f0fSMateusz Guzik lock_delay(&lda); 922a0d45f0fSMateusz Guzik } else { 92315140a8aSMateusz Guzik _mtx_lock_indefinite_check(m, &lda); 924a0d45f0fSMateusz Guzik } 9259f4e008dSMateusz Guzik if (m != td->td_lock) { 9269f4e008dSMateusz Guzik spinlock_enter(); 9272502c107SJeff Roberson goto retry; 9289f4e008dSMateusz Guzik } 9295e5ad162SMateusz Guzik v = MTX_READ_VALUE(m); 9305e5ad162SMateusz Guzik } while (v != MTX_UNOWNED); 9312502c107SJeff Roberson spinlock_enter(); 9322502c107SJeff Roberson } 9332502c107SJeff Roberson if (m == td->td_lock) 9342502c107SJeff Roberson break; 9359f4e008dSMateusz Guzik MPASS(m->mtx_recurse == 0); 9369f4e008dSMateusz Guzik _mtx_release_lock_quick(m); 9372502c107SJeff Roberson } 938dfaa7859SMateusz Guzik LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 939dfaa7859SMateusz Guzik line); 940dfaa7859SMateusz Guzik WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 941dfaa7859SMateusz Guzik 942dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 943dfaa7859SMateusz Guzik if (__predict_true(!doing_lockprof)) 944dfaa7859SMateusz Guzik return; 945dfaa7859SMateusz Guzik #endif 946076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 947e2b25737SMark Johnston spin_time += lockstat_nsecs(&m->lock_object); 948076dd8ebSAndriy Gapon #endif 949eea4f254SJeff Roberson if (m->mtx_recurse == 0) 95032cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 95132cd0147SMark Johnston contested, waittime, file, line); 9525002e195SMark Johnston #ifdef KDTRACE_HOOKS 953d0f68f91SMark Johnston if (lda.spin_cnt != 0) 95432cd0147SMark Johnston LOCKSTAT_RECORD1(thread__spin, m, spin_time); 9555002e195SMark Johnston #endif 9562502c107SJeff Roberson } 9572502c107SJeff Roberson 9582502c107SJeff Roberson struct mtx * 9592502c107SJeff Roberson thread_lock_block(struct thread *td) 9602502c107SJeff Roberson { 9612502c107SJeff Roberson struct mtx *lock; 9622502c107SJeff Roberson 9632502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 964710eacdcSJeff Roberson lock = td->td_lock; 9652502c107SJeff Roberson td->td_lock = &blocked_lock; 9662502c107SJeff Roberson mtx_unlock_spin(lock); 9672502c107SJeff Roberson 9682502c107SJeff Roberson return (lock); 9692502c107SJeff Roberson } 9702502c107SJeff Roberson 9712502c107SJeff Roberson void 9722502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new) 9732502c107SJeff Roberson { 9742502c107SJeff Roberson mtx_assert(new, MA_OWNED); 9752502c107SJeff Roberson MPASS(td->td_lock == &blocked_lock); 97665d32cd8SMatt Jacob atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new); 9772502c107SJeff Roberson } 9782502c107SJeff Roberson 9792502c107SJeff Roberson void 9802502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new) 9812502c107SJeff Roberson { 9822502c107SJeff Roberson struct mtx *lock; 9832502c107SJeff Roberson 9842502c107SJeff Roberson mtx_assert(new, MA_OWNED); 9852502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 986710eacdcSJeff Roberson lock = td->td_lock; 9872502c107SJeff Roberson td->td_lock = new; 9882502c107SJeff Roberson mtx_unlock_spin(lock); 9892502c107SJeff Roberson } 9902502c107SJeff Roberson 9919ed346baSBosko Milekic /* 9927f44c618SAttilio Rao * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 9939ed346baSBosko Milekic * 9943b3cf014SMateusz Guzik * We are only called here if the lock is recursed, contested (i.e. we 9953b3cf014SMateusz Guzik * need to wake up a blocked thread) or lockstat probe is active. 9969ed346baSBosko Milekic */ 99709f1319aSMateusz Guzik #if LOCK_DEBUG > 0 99836412d79SJohn Baldwin void 999b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, 1000b584eb2eSMateusz Guzik const char *file, int line) 100109f1319aSMateusz Guzik #else 100209f1319aSMateusz Guzik void 1003b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v) 100409f1319aSMateusz Guzik #endif 100536412d79SJohn Baldwin { 10067f44c618SAttilio Rao struct mtx *m; 1007961a7b24SJohn Baldwin struct turnstile *ts; 1008b584eb2eSMateusz Guzik uintptr_t tid; 10099ed346baSBosko Milekic 101035370593SAndriy Gapon if (SCHEDULER_STOPPED()) 101135370593SAndriy Gapon return; 101235370593SAndriy Gapon 10133b3cf014SMateusz Guzik tid = (uintptr_t)curthread; 10147f44c618SAttilio Rao m = mtxlock2mtx(c); 1015b584eb2eSMateusz Guzik 1016b584eb2eSMateusz Guzik if (__predict_false(v == tid)) 10173b3cf014SMateusz Guzik v = MTX_READ_VALUE(m); 101890836c32SMateusz Guzik 1019b584eb2eSMateusz Guzik if (__predict_false(v & MTX_RECURSED)) { 102036412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 102108812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 1022aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 10239ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 102436412d79SJohn Baldwin return; 102536412d79SJohn Baldwin } 10269ed346baSBosko Milekic 10273b3cf014SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m); 10283b3cf014SMateusz Guzik if (v == tid && _mtx_release_lock(m, tid)) 10293b3cf014SMateusz Guzik return; 10303b3cf014SMateusz Guzik 10312502c107SJeff Roberson /* 10322502c107SJeff Roberson * We have to lock the chain before the turnstile so this turnstile 10332502c107SJeff Roberson * can be removed from the hash list if it is empty. 10342502c107SJeff Roberson */ 10352502c107SJeff Roberson turnstile_chain_lock(&m->lock_object); 10368448e020SMateusz Guzik _mtx_release_lock_quick(m); 1037aa89d8cdSJohn Baldwin ts = turnstile_lookup(&m->lock_object); 10388448e020SMateusz Guzik MPASS(ts != NULL); 1039aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 10409ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 10417aa4f685SJohn Baldwin turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 1042bf9c6c31SJohn Baldwin 10432502c107SJeff Roberson /* 10442502c107SJeff Roberson * This turnstile is now no longer associated with the mutex. We can 10452502c107SJeff Roberson * unlock the chain lock so a new turnstile may take it's place. 10462502c107SJeff Roberson */ 1047d0a22279SMateusz Guzik turnstile_unpend(ts); 10482502c107SJeff Roberson turnstile_chain_unlock(&m->lock_object); 10499ed346baSBosko Milekic } 10509ed346baSBosko Milekic 10519ed346baSBosko Milekic /* 10529ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 1053961135eaSJohn Baldwin * See the __mtx_unlock_spin() macro for the details. 10549ed346baSBosko Milekic */ 10559ed346baSBosko Milekic 10569ed346baSBosko Milekic /* 105715ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 10589ed346baSBosko Milekic */ 10591103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 10600cde2e34SJason Evans void 10617f44c618SAttilio Rao __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line) 10620cde2e34SJason Evans { 10637f44c618SAttilio Rao const struct mtx *m; 10645cb0fbe4SJohn Baldwin 1065310ab671SEric van Gyzen if (panicstr != NULL || dumping || SCHEDULER_STOPPED()) 10665cb0fbe4SJohn Baldwin return; 10677f44c618SAttilio Rao 10687f44c618SAttilio Rao m = mtxlock2mtx(c); 10697f44c618SAttilio Rao 1070a10f4966SJake Burkholder switch (what) { 10710cde2e34SJason Evans case MA_OWNED: 10720cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 10730cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 1074a10f4966SJake Burkholder if (!mtx_owned(m)) 10750cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 1076aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 1077a10f4966SJake Burkholder if (mtx_recursed(m)) { 1078a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 10790cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 1080aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 1081a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 10820cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 1083aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 10840cde2e34SJason Evans } 10850cde2e34SJason Evans break; 10860cde2e34SJason Evans case MA_NOTOWNED: 1087a10f4966SJake Burkholder if (mtx_owned(m)) 10880cde2e34SJason Evans panic("mutex %s owned at %s:%d", 1089aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 10900cde2e34SJason Evans break; 10910cde2e34SJason Evans default: 109256771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 10930cde2e34SJason Evans } 10940cde2e34SJason Evans } 10950cde2e34SJason Evans #endif 10960cde2e34SJason Evans 10979ed346baSBosko Milekic /* 1098c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 1099c27b5699SAndrew R. Reiter */ 1100c27b5699SAndrew R. Reiter void 1101c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 1102c27b5699SAndrew R. Reiter { 1103c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 1104c27b5699SAndrew R. Reiter 11057f44c618SAttilio Rao mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL, 11067f44c618SAttilio Rao margs->ma_opts); 1107c27b5699SAndrew R. Reiter } 1108c27b5699SAndrew R. Reiter 1109c27b5699SAndrew R. Reiter /* 11109ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 11110c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 11120c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 11130c88508aSJohn Baldwin * witness. 11149ed346baSBosko Milekic */ 111536412d79SJohn Baldwin void 11167f44c618SAttilio Rao _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts) 111736412d79SJohn Baldwin { 11187f44c618SAttilio Rao struct mtx *m; 111983a81bcbSJohn Baldwin struct lock_class *class; 112083a81bcbSJohn Baldwin int flags; 11219ed346baSBosko Milekic 11227f44c618SAttilio Rao m = mtxlock2mtx(c); 11237f44c618SAttilio Rao 112419284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 1125fd07ddcfSDmitry Chagin MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0); 1126353998acSAttilio Rao ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock, 1127353998acSAttilio Rao ("%s: mtx_lock not aligned for %s: %p", __func__, name, 1128353998acSAttilio Rao &m->mtx_lock)); 11299ed346baSBosko Milekic 113083a81bcbSJohn Baldwin /* Determine lock class and lock flags. */ 113119284646SJohn Baldwin if (opts & MTX_SPIN) 113283a81bcbSJohn Baldwin class = &lock_class_mtx_spin; 113319284646SJohn Baldwin else 113483a81bcbSJohn Baldwin class = &lock_class_mtx_sleep; 113583a81bcbSJohn Baldwin flags = 0; 113619284646SJohn Baldwin if (opts & MTX_QUIET) 113783a81bcbSJohn Baldwin flags |= LO_QUIET; 113819284646SJohn Baldwin if (opts & MTX_RECURSE) 113983a81bcbSJohn Baldwin flags |= LO_RECURSABLE; 114019284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 114183a81bcbSJohn Baldwin flags |= LO_WITNESS; 1142f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 114383a81bcbSJohn Baldwin flags |= LO_DUPOK; 11447c0435b9SKip Macy if (opts & MTX_NOPROFILE) 11457c0435b9SKip Macy flags |= LO_NOPROFILE; 1146fd07ddcfSDmitry Chagin if (opts & MTX_NEW) 1147fd07ddcfSDmitry Chagin flags |= LO_NEW; 114819284646SJohn Baldwin 114983a81bcbSJohn Baldwin /* Initialize mutex. */ 1150b5fb43e5SJohn Baldwin lock_init(&m->lock_object, class, name, type, flags); 1151b5fb43e5SJohn Baldwin 115219284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 115383a81bcbSJohn Baldwin m->mtx_recurse = 0; 115436412d79SJohn Baldwin } 115536412d79SJohn Baldwin 11569ed346baSBosko Milekic /* 115719284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 115819284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 115919284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 116019284646SJohn Baldwin * flags. 11619ed346baSBosko Milekic */ 116236412d79SJohn Baldwin void 11637f44c618SAttilio Rao _mtx_destroy(volatile uintptr_t *c) 116436412d79SJohn Baldwin { 11657f44c618SAttilio Rao struct mtx *m; 11667f44c618SAttilio Rao 11677f44c618SAttilio Rao m = mtxlock2mtx(c); 116836412d79SJohn Baldwin 116919284646SJohn Baldwin if (!mtx_owned(m)) 117019284646SJohn Baldwin MPASS(mtx_unowned(m)); 117119284646SJohn Baldwin else { 117208812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 11739ed346baSBosko Milekic 1174861a2308SScott Long /* Perform the non-mtx related part of mtx_unlock_spin(). */ 1175aa89d8cdSJohn Baldwin if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) 1176861a2308SScott Long spinlock_exit(); 1177764e4d54SJohn Baldwin else 1178ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 1179861a2308SScott Long 1180d3df4af3SJeff Roberson lock_profile_release_lock(&m->lock_object); 118119284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 1182aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__, 1183c86b6ff5SJohn Baldwin __LINE__); 118436412d79SJohn Baldwin } 11850384fff8SJason Evans 1186186abbd7SJohn Baldwin m->mtx_lock = MTX_DESTROYED; 1187aa89d8cdSJohn Baldwin lock_destroy(&m->lock_object); 11880384fff8SJason Evans } 1189d23f5958SMatthew Dillon 1190d23f5958SMatthew Dillon /* 1191c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 1192c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 1193c53c013bSJohn Baldwin * setup before this is called. 1194c53c013bSJohn Baldwin */ 1195c53c013bSJohn Baldwin void 1196c53c013bSJohn Baldwin mutex_init(void) 1197c53c013bSJohn Baldwin { 1198c53c013bSJohn Baldwin 1199961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 1200961a7b24SJohn Baldwin init_turnstiles(); 1201961a7b24SJohn Baldwin 1202c53c013bSJohn Baldwin /* 1203c53c013bSJohn Baldwin * Initialize mutexes. 1204c53c013bSJohn Baldwin */ 12050c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 12062502c107SJeff Roberson mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN); 12072502c107SJeff Roberson blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */ 12080c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 12096afb32fcSKonstantin Belousov mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN); 12105c7bebf9SKonstantin Belousov mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN); 12115c7bebf9SKonstantin Belousov mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN); 12125c7bebf9SKonstantin Belousov mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN); 12138c4b6380SJohn Baldwin mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 1214c53c013bSJohn Baldwin mtx_lock(&Giant); 1215c53c013bSJohn Baldwin } 1216d272fe53SJohn Baldwin 121715140a8aSMateusz Guzik static void __noinline 121815140a8aSMateusz Guzik _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap) 121915140a8aSMateusz Guzik { 122015140a8aSMateusz Guzik struct thread *td; 122115140a8aSMateusz Guzik 122215140a8aSMateusz Guzik ldap->spin_cnt++; 122315140a8aSMateusz Guzik if (ldap->spin_cnt < 60000000 || kdb_active || panicstr != NULL) 12244cbbb748SJohn Baldwin cpu_lock_delay(); 122515140a8aSMateusz Guzik else { 122615140a8aSMateusz Guzik td = mtx_owner(m); 122715140a8aSMateusz Guzik 122815140a8aSMateusz Guzik /* If the mutex is unlocked, try again. */ 122915140a8aSMateusz Guzik if (td == NULL) 123015140a8aSMateusz Guzik return; 123115140a8aSMateusz Guzik 123215140a8aSMateusz Guzik printf( "spin lock %p (%s) held by %p (tid %d) too long\n", 123315140a8aSMateusz Guzik m, m->lock_object.lo_name, td, td->td_tid); 123415140a8aSMateusz Guzik #ifdef WITNESS 123515140a8aSMateusz Guzik witness_display_spinlock(&m->lock_object, td, printf); 123615140a8aSMateusz Guzik #endif 123715140a8aSMateusz Guzik panic("spin lock held too long"); 123815140a8aSMateusz Guzik } 123915140a8aSMateusz Guzik cpu_spinwait(); 124015140a8aSMateusz Guzik } 124115140a8aSMateusz Guzik 1242d2576988SMateusz Guzik void 1243d2576988SMateusz Guzik mtx_spin_wait_unlocked(struct mtx *m) 1244d2576988SMateusz Guzik { 1245d2576988SMateusz Guzik struct lock_delay_arg lda; 1246d2576988SMateusz Guzik 1247500ca73dSMateusz Guzik KASSERT(m->mtx_lock != MTX_DESTROYED, 1248500ca73dSMateusz Guzik ("%s() of destroyed mutex %p", __func__, m)); 1249500ca73dSMateusz Guzik KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 1250500ca73dSMateusz Guzik ("%s() of sleep mutex %p (%s)", __func__, m, 1251500ca73dSMateusz Guzik m->lock_object.lo_name)); 1252500ca73dSMateusz Guzik KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m, 1253500ca73dSMateusz Guzik m->lock_object.lo_name)); 1254500ca73dSMateusz Guzik 1255d2576988SMateusz Guzik lda.spin_cnt = 0; 1256d2576988SMateusz Guzik 1257d2576988SMateusz Guzik while (atomic_load_acq_ptr(&m->mtx_lock) != MTX_UNOWNED) { 1258d2576988SMateusz Guzik if (__predict_true(lda.spin_cnt < 10000000)) { 1259d2576988SMateusz Guzik cpu_spinwait(); 1260d2576988SMateusz Guzik lda.spin_cnt++; 1261d2576988SMateusz Guzik } else { 1262d2576988SMateusz Guzik _mtx_lock_indefinite_check(m, &lda); 1263d2576988SMateusz Guzik } 1264d2576988SMateusz Guzik } 1265d2576988SMateusz Guzik } 1266d2576988SMateusz Guzik 1267d272fe53SJohn Baldwin #ifdef DDB 1268d272fe53SJohn Baldwin void 1269d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock) 1270d272fe53SJohn Baldwin { 1271d272fe53SJohn Baldwin struct thread *td; 1272d576deedSPawel Jakub Dawidek const struct mtx *m; 1273d272fe53SJohn Baldwin 1274d576deedSPawel Jakub Dawidek m = (const struct mtx *)lock; 1275d272fe53SJohn Baldwin 1276d272fe53SJohn Baldwin db_printf(" flags: {"); 127783a81bcbSJohn Baldwin if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 1278d272fe53SJohn Baldwin db_printf("SPIN"); 1279d272fe53SJohn Baldwin else 1280d272fe53SJohn Baldwin db_printf("DEF"); 1281aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_RECURSABLE) 1282d272fe53SJohn Baldwin db_printf(", RECURSE"); 1283aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_DUPOK) 1284d272fe53SJohn Baldwin db_printf(", DUPOK"); 1285d272fe53SJohn Baldwin db_printf("}\n"); 1286d272fe53SJohn Baldwin db_printf(" state: {"); 1287d272fe53SJohn Baldwin if (mtx_unowned(m)) 1288d272fe53SJohn Baldwin db_printf("UNOWNED"); 1289c0bfd703SJohn Baldwin else if (mtx_destroyed(m)) 1290c0bfd703SJohn Baldwin db_printf("DESTROYED"); 1291d272fe53SJohn Baldwin else { 1292d272fe53SJohn Baldwin db_printf("OWNED"); 1293d272fe53SJohn Baldwin if (m->mtx_lock & MTX_CONTESTED) 1294d272fe53SJohn Baldwin db_printf(", CONTESTED"); 1295d272fe53SJohn Baldwin if (m->mtx_lock & MTX_RECURSED) 1296d272fe53SJohn Baldwin db_printf(", RECURSED"); 1297d272fe53SJohn Baldwin } 1298d272fe53SJohn Baldwin db_printf("}\n"); 1299c0bfd703SJohn Baldwin if (!mtx_unowned(m) && !mtx_destroyed(m)) { 1300d272fe53SJohn Baldwin td = mtx_owner(m); 1301d272fe53SJohn Baldwin db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 1302431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 1303d272fe53SJohn Baldwin if (mtx_recursed(m)) 1304d272fe53SJohn Baldwin db_printf(" recursed: %d\n", m->mtx_recurse); 1305d272fe53SJohn Baldwin } 1306d272fe53SJohn Baldwin } 1307d272fe53SJohn Baldwin #endif 1308