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 179d576deedSPawel Jakub Dawidek mtx_assert((const struct mtx *)lock, what); 180f9721b43SAttilio Rao } 181f9721b43SAttilio Rao 18267784314SPoul-Henning Kamp void 1837faf4d90SDavide Italiano lock_mtx(struct lock_object *lock, uintptr_t how) 1846e21afd4SJohn Baldwin { 1856e21afd4SJohn Baldwin 1866e21afd4SJohn Baldwin mtx_lock((struct mtx *)lock); 1876e21afd4SJohn Baldwin } 1886e21afd4SJohn Baldwin 18967784314SPoul-Henning Kamp void 1907faf4d90SDavide Italiano lock_spin(struct lock_object *lock, uintptr_t how) 1916e21afd4SJohn Baldwin { 1926e21afd4SJohn Baldwin 1936e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 1946e21afd4SJohn Baldwin } 1956e21afd4SJohn Baldwin 1967faf4d90SDavide Italiano uintptr_t 1976e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock) 1986e21afd4SJohn Baldwin { 1996e21afd4SJohn Baldwin struct mtx *m; 2006e21afd4SJohn Baldwin 2016e21afd4SJohn Baldwin m = (struct mtx *)lock; 2026e21afd4SJohn Baldwin mtx_assert(m, MA_OWNED | MA_NOTRECURSED); 2036e21afd4SJohn Baldwin mtx_unlock(m); 2046e21afd4SJohn Baldwin return (0); 2056e21afd4SJohn Baldwin } 2066e21afd4SJohn Baldwin 2077faf4d90SDavide Italiano uintptr_t 2086e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock) 2096e21afd4SJohn Baldwin { 2106e21afd4SJohn Baldwin 2116e21afd4SJohn Baldwin panic("spin locks can only use msleep_spin"); 2126e21afd4SJohn Baldwin } 2136e21afd4SJohn Baldwin 214a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 215a5aedd68SStacey Son int 216d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner) 217a5aedd68SStacey Son { 21802315a67SMark Johnston const struct mtx *m; 21902315a67SMark Johnston uintptr_t x; 220a5aedd68SStacey Son 22102315a67SMark Johnston m = (const struct mtx *)lock; 22202315a67SMark Johnston x = m->mtx_lock; 22302315a67SMark Johnston *owner = (struct thread *)(x & ~MTX_FLAGMASK); 224e280ce46SMateusz Guzik return (*owner != NULL); 225a5aedd68SStacey Son } 226a5aedd68SStacey Son #endif 227a5aedd68SStacey Son 2280cde2e34SJason Evans /* 2296283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 2306283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 2316283b7d0SJohn Baldwin */ 2326283b7d0SJohn Baldwin void 2337f44c618SAttilio Rao __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 2346283b7d0SJohn Baldwin { 2357f44c618SAttilio Rao struct mtx *m; 23608da2677SMateusz Guzik uintptr_t tid, v; 2376283b7d0SJohn Baldwin 2387f44c618SAttilio Rao m = mtxlock2mtx(c); 2397f44c618SAttilio Rao 240704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 241704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 242e3ae0dfeSAttilio Rao ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d", 243e3ae0dfeSAttilio Rao curthread, m->lock_object.lo_name, file, line)); 244186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 245186abbd7SJohn Baldwin ("mtx_lock() of destroyed mutex @ %s:%d", file, line)); 246aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 247aa89d8cdSJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2480d975d63SJohn Baldwin file, line)); 249ac6b769bSAttilio Rao WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) | 250ac6b769bSAttilio Rao LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 2517c0435b9SKip Macy 25208da2677SMateusz Guzik tid = (uintptr_t)curthread; 25308da2677SMateusz Guzik v = MTX_UNOWNED; 25408da2677SMateusz Guzik if (!_mtx_obtain_lock_fetch(m, &v, tid)) 2552f1ddb89SMateusz Guzik _mtx_lock_sleep(m, v, opts, file, line); 25608da2677SMateusz Guzik else 25708da2677SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, 25808da2677SMateusz Guzik m, 0, 0, file, line); 259aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 260dde96c99SJohn Baldwin line); 261ac6b769bSAttilio Rao WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE, 262ac6b769bSAttilio Rao file, line); 263ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 2646283b7d0SJohn Baldwin } 2656283b7d0SJohn Baldwin 2666283b7d0SJohn Baldwin void 2677f44c618SAttilio Rao __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line) 2686283b7d0SJohn Baldwin { 2697f44c618SAttilio Rao struct mtx *m; 27035370593SAndriy Gapon 2717f44c618SAttilio Rao m = mtxlock2mtx(c); 2727f44c618SAttilio Rao 273186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 274186abbd7SJohn Baldwin ("mtx_unlock() of destroyed mutex @ %s:%d", file, line)); 275aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 276aa89d8cdSJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 2770d975d63SJohn Baldwin file, line)); 278aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 279aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 2800d975d63SJohn Baldwin line); 28121377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 282c66d7606SKip Macy 283ffd5c94cSMateusz Guzik #ifdef LOCK_PROFILING 284b584eb2eSMateusz Guzik __mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line); 285ffd5c94cSMateusz Guzik #else 286ffd5c94cSMateusz Guzik __mtx_unlock(m, curthread, opts, file, line); 287ffd5c94cSMateusz Guzik #endif 288ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 2896283b7d0SJohn Baldwin } 2906283b7d0SJohn Baldwin 2916283b7d0SJohn Baldwin void 2927f44c618SAttilio Rao __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 2937f44c618SAttilio Rao int line) 2946283b7d0SJohn Baldwin { 2957f44c618SAttilio Rao struct mtx *m; 29662bf13cbSMateusz Guzik #ifdef SMP 2970d74fe26SMateusz Guzik uintptr_t tid, v; 29862bf13cbSMateusz Guzik #endif 2996283b7d0SJohn Baldwin 3007f44c618SAttilio Rao m = mtxlock2mtx(c); 3017f44c618SAttilio Rao 302186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 303186abbd7SJohn Baldwin ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line)); 304aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 3050d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 306aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 307ad69e26bSJohn Baldwin if (mtx_owned(m)) 308ac6b769bSAttilio Rao KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 309ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0, 310ad69e26bSJohn Baldwin ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n", 311ad69e26bSJohn Baldwin m->lock_object.lo_name, file, line)); 312ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 313aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 31441313430SJohn Baldwin file, line, NULL); 31562bf13cbSMateusz Guzik #ifdef SMP 3160d74fe26SMateusz Guzik spinlock_enter(); 3170d74fe26SMateusz Guzik tid = (uintptr_t)curthread; 3180d74fe26SMateusz Guzik v = MTX_UNOWNED; 3190d74fe26SMateusz Guzik if (!_mtx_obtain_lock_fetch(m, &v, tid)) 3200d74fe26SMateusz Guzik _mtx_lock_spin(m, v, opts, file, line); 3210d74fe26SMateusz Guzik else 3220d74fe26SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, 3230d74fe26SMateusz Guzik m, 0, 0, file, line); 32462bf13cbSMateusz Guzik #else 32562bf13cbSMateusz Guzik __mtx_lock_spin(m, curthread, opts, file, line); 32662bf13cbSMateusz Guzik #endif 327aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 328dde96c99SJohn Baldwin line); 329aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 3306283b7d0SJohn Baldwin } 3316283b7d0SJohn Baldwin 33290b581f2SKonstantin Belousov int 33390b581f2SKonstantin Belousov __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 33490b581f2SKonstantin Belousov int line) 33590b581f2SKonstantin Belousov { 33690b581f2SKonstantin Belousov struct mtx *m; 33790b581f2SKonstantin Belousov 33890b581f2SKonstantin Belousov if (SCHEDULER_STOPPED()) 33990b581f2SKonstantin Belousov return (1); 34090b581f2SKonstantin Belousov 34190b581f2SKonstantin Belousov m = mtxlock2mtx(c); 34290b581f2SKonstantin Belousov 34390b581f2SKonstantin Belousov KASSERT(m->mtx_lock != MTX_DESTROYED, 34490b581f2SKonstantin Belousov ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line)); 34590b581f2SKonstantin Belousov KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 34690b581f2SKonstantin Belousov ("mtx_trylock_spin() of sleep mutex %s @ %s:%d", 34790b581f2SKonstantin Belousov m->lock_object.lo_name, file, line)); 34890b581f2SKonstantin Belousov KASSERT((opts & MTX_RECURSE) == 0, 34990b581f2SKonstantin Belousov ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n", 35090b581f2SKonstantin Belousov m->lock_object.lo_name, file, line)); 35190b581f2SKonstantin Belousov if (__mtx_trylock_spin(m, curthread, opts, file, line)) { 35290b581f2SKonstantin Belousov LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line); 35390b581f2SKonstantin Belousov WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 35490b581f2SKonstantin Belousov return (1); 35590b581f2SKonstantin Belousov } 35690b581f2SKonstantin Belousov LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line); 35790b581f2SKonstantin Belousov return (0); 35890b581f2SKonstantin Belousov } 35990b581f2SKonstantin Belousov 3606283b7d0SJohn Baldwin void 3617f44c618SAttilio Rao __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file, 3627f44c618SAttilio Rao int line) 3636283b7d0SJohn Baldwin { 3647f44c618SAttilio Rao struct mtx *m; 365c66d7606SKip Macy 3667f44c618SAttilio Rao m = mtxlock2mtx(c); 3677f44c618SAttilio Rao 368186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 369186abbd7SJohn Baldwin ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line)); 370aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 3710d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 372aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 373aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 374aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file, 375dde96c99SJohn Baldwin line); 3760d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 377c66d7606SKip Macy 378961135eaSJohn Baldwin __mtx_unlock_spin(m); 3796283b7d0SJohn Baldwin } 3806283b7d0SJohn Baldwin 3816283b7d0SJohn Baldwin /* 3829ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 383eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 384eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 3850cde2e34SJason Evans */ 3860cde2e34SJason Evans int 387013c0b49SMateusz Guzik _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF) 3880cde2e34SJason Evans { 3895c5df0d9SMateusz Guzik struct thread *td; 3905c5df0d9SMateusz Guzik uintptr_t tid, v; 3911723a064SJeff Roberson #ifdef LOCK_PROFILING 3927c0435b9SKip Macy uint64_t waittime = 0; 3931723a064SJeff Roberson int contested = 0; 3941723a064SJeff Roberson #endif 3951723a064SJeff Roberson int rval; 3965c5df0d9SMateusz Guzik bool recursed; 3970cde2e34SJason Evans 3985c5df0d9SMateusz Guzik td = curthread; 3995c5df0d9SMateusz Guzik tid = (uintptr_t)td; 4005c5df0d9SMateusz Guzik if (SCHEDULER_STOPPED_TD(td)) 40135370593SAndriy Gapon return (1); 40235370593SAndriy Gapon 403704cb42fSMark Johnston KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td), 404e3ae0dfeSAttilio Rao ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d", 405e3ae0dfeSAttilio Rao curthread, m->lock_object.lo_name, file, line)); 406186abbd7SJohn Baldwin KASSERT(m->mtx_lock != MTX_DESTROYED, 407186abbd7SJohn Baldwin ("mtx_trylock() of destroyed mutex @ %s:%d", file, line)); 408aa89d8cdSJohn Baldwin KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep, 409aa89d8cdSJohn Baldwin ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name, 41083cece6fSJohn Baldwin file, line)); 4119ed346baSBosko Milekic 4125c5df0d9SMateusz Guzik rval = 1; 4135c5df0d9SMateusz Guzik recursed = false; 4145c5df0d9SMateusz Guzik v = MTX_UNOWNED; 415b247fd39SMateusz Guzik for (;;) { 416b247fd39SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 417b247fd39SMateusz Guzik break; 418b247fd39SMateusz Guzik if (v == MTX_UNOWNED) 419b247fd39SMateusz Guzik continue; 4205c5df0d9SMateusz Guzik if (v == tid && 4215c5df0d9SMateusz Guzik ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 422ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0)) { 423eac09796SJohn Baldwin m->mtx_recurse++; 424eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 4255c5df0d9SMateusz Guzik recursed = true; 426b247fd39SMateusz Guzik break; 4275c5df0d9SMateusz Guzik } 428b247fd39SMateusz Guzik rval = 0; 429b247fd39SMateusz Guzik break; 4305c5df0d9SMateusz Guzik } 4315c5df0d9SMateusz Guzik 432ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 4339ed346baSBosko Milekic 434aa89d8cdSJohn Baldwin LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line); 435764e4d54SJohn Baldwin if (rval) { 436aa89d8cdSJohn Baldwin WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4372d96f0b1SJohn Baldwin file, line); 438ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 4395c5df0d9SMateusz Guzik if (!recursed) 44032cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, 441a5aedd68SStacey Son m, contested, waittime, file, line); 442764e4d54SJohn Baldwin } 4439ed346baSBosko Milekic 44419284646SJohn Baldwin return (rval); 4450cde2e34SJason Evans } 4460cde2e34SJason Evans 447013c0b49SMateusz Guzik int 448013c0b49SMateusz Guzik _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line) 449013c0b49SMateusz Guzik { 450013c0b49SMateusz Guzik struct mtx *m; 451013c0b49SMateusz Guzik 452013c0b49SMateusz Guzik m = mtxlock2mtx(c); 453013c0b49SMateusz Guzik return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG)); 454013c0b49SMateusz Guzik } 455013c0b49SMateusz Guzik 4560cde2e34SJason Evans /* 4577f44c618SAttilio Rao * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4589ed346baSBosko Milekic * 4599ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4609ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4610cde2e34SJason Evans */ 46209f1319aSMateusz Guzik #if LOCK_DEBUG > 0 4630cde2e34SJason Evans void 4642f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file, 4652f1ddb89SMateusz Guzik int line) 46609f1319aSMateusz Guzik #else 46709f1319aSMateusz Guzik void 4682f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v) 46909f1319aSMateusz Guzik #endif 47036412d79SJohn Baldwin { 4712f1ddb89SMateusz Guzik struct thread *td; 4727f44c618SAttilio Rao struct mtx *m; 4732502c107SJeff Roberson struct turnstile *ts; 4742f1ddb89SMateusz Guzik uintptr_t tid; 4752ccee9ccSMateusz Guzik struct thread *owner; 4761723a064SJeff Roberson #ifdef LOCK_PROFILING 47770fe8436SKip Macy int contested = 0; 47870fe8436SKip Macy uint64_t waittime = 0; 4791723a064SJeff Roberson #endif 4801ada9041SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS) 4811ada9041SMateusz Guzik struct lock_delay_arg lda; 4821ada9041SMateusz Guzik #endif 483a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 48461852185SMateusz Guzik u_int sleep_cnt = 0; 485a5aedd68SStacey Son int64_t sleep_time = 0; 486076dd8ebSAndriy Gapon int64_t all_time = 0; 487dfaa7859SMateusz Guzik #endif 488dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 489dfaa7859SMateusz Guzik int doing_lockprof; 490a5aedd68SStacey Son #endif 49109bdec20SMateusz Guzik 4922f1ddb89SMateusz Guzik td = curthread; 4932f1ddb89SMateusz Guzik tid = (uintptr_t)td; 49409bdec20SMateusz Guzik m = mtxlock2mtx(c); 49509bdec20SMateusz Guzik 49609bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS 49709bdec20SMateusz Guzik if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) { 49809bdec20SMateusz Guzik while (v == MTX_UNOWNED) { 49909bdec20SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 50009bdec20SMateusz Guzik goto out_lockstat; 50109bdec20SMateusz Guzik } 50209bdec20SMateusz Guzik doing_lockprof = 1; 50309bdec20SMateusz Guzik all_time -= lockstat_nsecs(&m->lock_object); 50409bdec20SMateusz Guzik } 50509bdec20SMateusz Guzik #endif 50609bdec20SMateusz Guzik #ifdef LOCK_PROFILING 50709bdec20SMateusz Guzik doing_lockprof = 1; 50809bdec20SMateusz Guzik #endif 50909bdec20SMateusz Guzik 5102f1ddb89SMateusz Guzik if (SCHEDULER_STOPPED_TD(td)) 51135370593SAndriy Gapon return; 51235370593SAndriy Gapon 513fa5000a4SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) 5141ada9041SMateusz Guzik lock_delay_arg_init(&lda, &mtx_delay); 515fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 516fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 5171ada9041SMateusz Guzik #endif 51809bdec20SMateusz Guzik 519c1aaf63cSMateusz Guzik if (__predict_false(v == MTX_UNOWNED)) 520c1aaf63cSMateusz Guzik v = MTX_READ_VALUE(m); 5217f44c618SAttilio Rao 5222f1ddb89SMateusz Guzik if (__predict_false(lv_mtx_owner(v) == td)) { 523ac6b769bSAttilio Rao KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 || 524ac6b769bSAttilio Rao (opts & MTX_RECURSE) != 0, 525eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 526aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line)); 527a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0 528ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 529a24c8eb8SMateusz Guzik #endif 53036412d79SJohn Baldwin m->mtx_recurse++; 53108812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 532aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 5335746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 53436412d79SJohn Baldwin return; 53536412d79SJohn Baldwin } 536a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0 537ac6b769bSAttilio Rao opts &= ~MTX_RECURSE; 538a24c8eb8SMateusz Guzik #endif 5399ed346baSBosko Milekic 540f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 541f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 542f5f9340bSFabien Thomas #endif 54370fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, 54470fe8436SKip Macy &contested, &waittime); 545aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 54615ec816aSJohn Baldwin CTR4(KTR_LOCK, 54715ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 548aa89d8cdSJohn Baldwin m->lock_object.lo_name, (void *)m->mtx_lock, file, line); 5491bd0eefbSJohn Baldwin 550fc4f686dSMateusz Guzik for (;;) { 5512604eb9eSMateusz Guzik if (v == MTX_UNOWNED) { 55290836c32SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 553fc4f686dSMateusz Guzik break; 5542604eb9eSMateusz Guzik continue; 5552604eb9eSMateusz Guzik } 556a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 5571ada9041SMateusz Guzik lda.spin_cnt++; 558a5aedd68SStacey Son #endif 55949aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 56049aead8aSAttilio Rao /* 56149aead8aSAttilio Rao * If the owner is running on another CPU, spin until the 56249aead8aSAttilio Rao * owner stops running or the state of the lock changes. 56349aead8aSAttilio Rao */ 5642604eb9eSMateusz Guzik owner = lv_mtx_owner(v); 56549aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 56649aead8aSAttilio Rao if (LOCK_LOG_TEST(&m->lock_object, 0)) 56749aead8aSAttilio Rao CTR3(KTR_LOCK, 56849aead8aSAttilio Rao "%s: spinning on %p held by %p", 56949aead8aSAttilio Rao __func__, m, owner); 5702cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 5712cba8dd3SJohn Baldwin sched_tdname((struct thread *)tid), 5722cba8dd3SJohn Baldwin "spinning", "lockname:\"%s\"", 5732cba8dd3SJohn Baldwin m->lock_object.lo_name); 5742604eb9eSMateusz Guzik do { 5751ada9041SMateusz Guzik lock_delay(&lda); 576391df78aSMateusz Guzik v = MTX_READ_VALUE(m); 5772604eb9eSMateusz Guzik owner = lv_mtx_owner(v); 5782604eb9eSMateusz Guzik } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner)); 5792cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 5802cba8dd3SJohn Baldwin sched_tdname((struct thread *)tid), 5812cba8dd3SJohn Baldwin "running"); 58249aead8aSAttilio Rao continue; 58349aead8aSAttilio Rao } 58449aead8aSAttilio Rao #endif 58549aead8aSAttilio Rao 5862502c107SJeff Roberson ts = turnstile_trywait(&m->lock_object); 5872604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 588310f24d7SMateusz Guzik retry_turnstile: 5895fa8dd90SJohn Baldwin 59036412d79SJohn Baldwin /* 5919ed346baSBosko Milekic * Check if the lock has been released while spinning for 592961a7b24SJohn Baldwin * the turnstile chain lock. 59336412d79SJohn Baldwin */ 5945fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 5952502c107SJeff Roberson turnstile_cancel(ts); 59636412d79SJohn Baldwin continue; 59736412d79SJohn Baldwin } 5989ed346baSBosko Milekic 59949aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES 60049aead8aSAttilio Rao /* 601fa29f023SJohn Baldwin * The current lock owner might have started executing 602fa29f023SJohn Baldwin * on another CPU (or the lock could have changed 603fa29f023SJohn Baldwin * owners) while we were waiting on the turnstile 604fa29f023SJohn Baldwin * chain lock. If so, drop the turnstile lock and try 605fa29f023SJohn Baldwin * again. 60649aead8aSAttilio Rao */ 6072604eb9eSMateusz Guzik owner = lv_mtx_owner(v); 60849aead8aSAttilio Rao if (TD_IS_RUNNING(owner)) { 60949aead8aSAttilio Rao turnstile_cancel(ts); 61049aead8aSAttilio Rao continue; 61149aead8aSAttilio Rao } 61249aead8aSAttilio Rao #endif 61349aead8aSAttilio Rao 61436412d79SJohn Baldwin /* 6159ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 6169ed346baSBosko Milekic * setting the contested bit, the mutex was either released 6179ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 61836412d79SJohn Baldwin */ 61936412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 620310f24d7SMateusz Guzik !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) { 621310f24d7SMateusz Guzik goto retry_turnstile; 62236412d79SJohn Baldwin } 62336412d79SJohn Baldwin 6249ed346baSBosko Milekic /* 6257feefcd6SJohn Baldwin * We definitely must sleep for this lock. 6269ed346baSBosko Milekic */ 62736412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 62836412d79SJohn Baldwin 6299ed346baSBosko Milekic /* 630961a7b24SJohn Baldwin * Block on the turnstile. 6319ed346baSBosko Milekic */ 632a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 633e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&m->lock_object); 634a5aedd68SStacey Son #endif 635284194f1SMateusz Guzik #ifndef ADAPTIVE_MUTEXES 636284194f1SMateusz Guzik owner = mtx_owner(m); 637284194f1SMateusz Guzik #endif 6388448e020SMateusz Guzik MPASS(owner == mtx_owner(m)); 6398448e020SMateusz Guzik turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE); 640a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 641e2b25737SMark Johnston sleep_time += lockstat_nsecs(&m->lock_object); 642a5aedd68SStacey Son sleep_cnt++; 643a5aedd68SStacey Son #endif 6442604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 64536412d79SJohn Baldwin } 646dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 647dfaa7859SMateusz Guzik if (__predict_true(!doing_lockprof)) 6487640beb9SMateusz Guzik return; 649dfaa7859SMateusz Guzik #endif 650dfaa7859SMateusz Guzik #ifdef KDTRACE_HOOKS 6517640beb9SMateusz Guzik all_time += lockstat_nsecs(&m->lock_object); 652a5aedd68SStacey Son if (sleep_time) 65332cd0147SMark Johnston LOCKSTAT_RECORD1(adaptive__block, m, sleep_time); 654a5aedd68SStacey Son 655a5aedd68SStacey Son /* 656a5aedd68SStacey Son * Only record the loops spinning and not sleeping. 657a5aedd68SStacey Son */ 6581ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 65932cd0147SMark Johnston LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time); 66009bdec20SMateusz Guzik out_lockstat: 661a5aedd68SStacey Son #endif 66209bdec20SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested, 66309bdec20SMateusz Guzik waittime, file, line); 6649ed346baSBosko Milekic } 6659ed346baSBosko Milekic 666b95b98b0SKonstantin Belousov #ifdef SMP 6679ed346baSBosko Milekic /* 6687f44c618SAttilio Rao * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock. 6699ed346baSBosko Milekic * 6709ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 6719ed346baSBosko Milekic * is handled inline. 6729ed346baSBosko Milekic */ 6730d74fe26SMateusz Guzik #if LOCK_DEBUG > 0 6749ed346baSBosko Milekic void 6750d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts, 6760d74fe26SMateusz Guzik const char *file, int line) 6770d74fe26SMateusz Guzik #else 6780d74fe26SMateusz Guzik void 6790d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v) 6800d74fe26SMateusz Guzik #endif 68136412d79SJohn Baldwin { 6827f44c618SAttilio Rao struct mtx *m; 683a0d45f0fSMateusz Guzik struct lock_delay_arg lda; 6840d74fe26SMateusz Guzik uintptr_t tid; 6851723a064SJeff Roberson #ifdef LOCK_PROFILING 6861723a064SJeff Roberson int contested = 0; 68770fe8436SKip Macy uint64_t waittime = 0; 6881723a064SJeff Roberson #endif 689076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 690076dd8ebSAndriy Gapon int64_t spin_time = 0; 691076dd8ebSAndriy Gapon #endif 692dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 693dfaa7859SMateusz Guzik int doing_lockprof; 694dfaa7859SMateusz Guzik #endif 69536412d79SJohn Baldwin 6960d74fe26SMateusz Guzik tid = (uintptr_t)curthread; 6977f44c618SAttilio Rao m = mtxlock2mtx(c); 6987f44c618SAttilio Rao 69909bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS 70009bdec20SMateusz Guzik if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) { 70109bdec20SMateusz Guzik while (v == MTX_UNOWNED) { 70209bdec20SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 70309bdec20SMateusz Guzik goto out_lockstat; 70409bdec20SMateusz Guzik } 70509bdec20SMateusz Guzik doing_lockprof = 1; 70609bdec20SMateusz Guzik spin_time -= lockstat_nsecs(&m->lock_object); 70709bdec20SMateusz Guzik } 70809bdec20SMateusz Guzik #endif 70909bdec20SMateusz Guzik #ifdef LOCK_PROFILING 71009bdec20SMateusz Guzik doing_lockprof = 1; 71109bdec20SMateusz Guzik #endif 71209bdec20SMateusz Guzik 71313d2ef0fSMateusz Guzik if (__predict_false(v == MTX_UNOWNED)) 71413d2ef0fSMateusz Guzik v = MTX_READ_VALUE(m); 71513d2ef0fSMateusz Guzik 71613d2ef0fSMateusz Guzik if (__predict_false(v == tid)) { 71713d2ef0fSMateusz Guzik m->mtx_recurse++; 71813d2ef0fSMateusz Guzik return; 71913d2ef0fSMateusz Guzik } 72013d2ef0fSMateusz Guzik 7210d74fe26SMateusz Guzik if (SCHEDULER_STOPPED()) 7220d74fe26SMateusz Guzik return; 7230d74fe26SMateusz Guzik 7240d74fe26SMateusz Guzik lock_delay_arg_init(&lda, &mtx_spin_delay); 7250d74fe26SMateusz Guzik 726aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 7275746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 7282cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 7292cba8dd3SJohn Baldwin "spinning", "lockname:\"%s\"", m->lock_object.lo_name); 7309ed346baSBosko Milekic 731f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 732f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 733f5f9340bSFabien Thomas #endif 73470fe8436SKip Macy lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime); 73509bdec20SMateusz Guzik 736fc4f686dSMateusz Guzik for (;;) { 7372604eb9eSMateusz Guzik if (v == MTX_UNOWNED) { 73890836c32SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 739fc4f686dSMateusz Guzik break; 74036412d79SJohn Baldwin continue; 741703fc290SJohn Baldwin } 7422604eb9eSMateusz Guzik /* Give interrupts a chance while we spin. */ 7432604eb9eSMateusz Guzik spinlock_exit(); 7442604eb9eSMateusz Guzik do { 74515140a8aSMateusz Guzik if (__predict_true(lda.spin_cnt < 10000000)) { 7462604eb9eSMateusz Guzik lock_delay(&lda); 7472604eb9eSMateusz Guzik } else { 74815140a8aSMateusz Guzik _mtx_lock_indefinite_check(m, &lda); 74936412d79SJohn Baldwin } 7502604eb9eSMateusz Guzik v = MTX_READ_VALUE(m); 7512604eb9eSMateusz Guzik } while (v != MTX_UNOWNED); 752c6a37e84SJohn Baldwin spinlock_enter(); 75336412d79SJohn Baldwin } 75436412d79SJohn Baldwin 755aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 7569ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 7572cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid), 7582cba8dd3SJohn Baldwin "running"); 7599ed346baSBosko Milekic 760dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 761dfaa7859SMateusz Guzik if (__predict_true(!doing_lockprof)) 762dfaa7859SMateusz Guzik return; 763dfaa7859SMateusz Guzik #endif 764c6d48c87SMark Johnston #ifdef KDTRACE_HOOKS 765dfaa7859SMateusz Guzik spin_time += lockstat_nsecs(&m->lock_object); 76609bdec20SMateusz Guzik if (lda.spin_cnt != 0) 76709bdec20SMateusz Guzik LOCKSTAT_RECORD1(spin__spin, m, spin_time); 76809bdec20SMateusz Guzik out_lockstat: 769dfaa7859SMateusz Guzik #endif 77032cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 77132cd0147SMark Johnston contested, waittime, file, line); 77236412d79SJohn Baldwin } 77333fb8a38SJohn Baldwin #endif /* SMP */ 77436412d79SJohn Baldwin 775be49509eSMateusz Guzik #ifdef INVARIANTS 776be49509eSMateusz Guzik static void 777be49509eSMateusz Guzik thread_lock_validate(struct mtx *m, int opts, const char *file, int line) 778be49509eSMateusz Guzik { 779be49509eSMateusz Guzik 780be49509eSMateusz Guzik KASSERT(m->mtx_lock != MTX_DESTROYED, 781be49509eSMateusz Guzik ("thread_lock() of destroyed mutex @ %s:%d", file, line)); 782be49509eSMateusz Guzik KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 783be49509eSMateusz Guzik ("thread_lock() of sleep mutex %s @ %s:%d", 784be49509eSMateusz Guzik m->lock_object.lo_name, file, line)); 785be49509eSMateusz Guzik if (mtx_owned(m)) 786be49509eSMateusz Guzik KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0, 787be49509eSMateusz Guzik ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n", 788be49509eSMateusz Guzik m->lock_object.lo_name, file, line)); 789be49509eSMateusz Guzik WITNESS_CHECKORDER(&m->lock_object, 790be49509eSMateusz Guzik opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL); 791be49509eSMateusz Guzik } 792be49509eSMateusz Guzik #else 793be49509eSMateusz Guzik #define thread_lock_validate(m, opts, file, line) do { } while (0) 794be49509eSMateusz Guzik #endif 795be49509eSMateusz Guzik 796be49509eSMateusz Guzik #ifndef LOCK_PROFILING 797be49509eSMateusz Guzik #if LOCK_DEBUG > 0 798be49509eSMateusz Guzik void 799be49509eSMateusz Guzik _thread_lock(struct thread *td, int opts, const char *file, int line) 800be49509eSMateusz Guzik #else 801be49509eSMateusz Guzik void 802be49509eSMateusz Guzik _thread_lock(struct thread *td) 803be49509eSMateusz Guzik #endif 804be49509eSMateusz Guzik { 805be49509eSMateusz Guzik struct mtx *m; 806be49509eSMateusz Guzik uintptr_t tid, v; 807be49509eSMateusz Guzik 808be49509eSMateusz Guzik tid = (uintptr_t)curthread; 809be49509eSMateusz Guzik 8102c50bafeSMateusz Guzik if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire))) 8112c50bafeSMateusz Guzik goto slowpath_noirq; 812be49509eSMateusz Guzik spinlock_enter(); 813be49509eSMateusz Guzik m = td->td_lock; 814be49509eSMateusz Guzik thread_lock_validate(m, 0, file, line); 815be49509eSMateusz Guzik v = MTX_READ_VALUE(m); 816be49509eSMateusz Guzik if (__predict_true(v == MTX_UNOWNED)) { 817be49509eSMateusz Guzik if (__predict_false(!_mtx_obtain_lock(m, tid))) 818be49509eSMateusz Guzik goto slowpath_unlocked; 819be49509eSMateusz Guzik } else if (v == tid) { 820be49509eSMateusz Guzik m->mtx_recurse++; 821be49509eSMateusz Guzik } else 822be49509eSMateusz Guzik goto slowpath_unlocked; 823be49509eSMateusz Guzik if (__predict_true(m == td->td_lock)) { 824be49509eSMateusz Guzik WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line); 825be49509eSMateusz Guzik return; 826be49509eSMateusz Guzik } 8279f4e008dSMateusz Guzik MPASS(m->mtx_recurse == 0); 828be49509eSMateusz Guzik _mtx_release_lock_quick(m); 829be49509eSMateusz Guzik slowpath_unlocked: 830be49509eSMateusz Guzik spinlock_exit(); 8312c50bafeSMateusz Guzik slowpath_noirq: 8322c50bafeSMateusz Guzik #if LOCK_DEBUG > 0 8332c50bafeSMateusz Guzik thread_lock_flags_(td, opts, file, line); 8342c50bafeSMateusz Guzik #else 835be49509eSMateusz Guzik thread_lock_flags_(td, 0, 0, 0); 8362c50bafeSMateusz Guzik #endif 837be49509eSMateusz Guzik } 838be49509eSMateusz Guzik #endif 839be49509eSMateusz Guzik 8402502c107SJeff Roberson void 841ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line) 8422502c107SJeff Roberson { 8432502c107SJeff Roberson struct mtx *m; 8445e5ad162SMateusz Guzik uintptr_t tid, v; 845a0d45f0fSMateusz Guzik struct lock_delay_arg lda; 8461723a064SJeff Roberson #ifdef LOCK_PROFILING 8471723a064SJeff Roberson int contested = 0; 8481723a064SJeff Roberson uint64_t waittime = 0; 8491723a064SJeff Roberson #endif 850a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 851076dd8ebSAndriy Gapon int64_t spin_time = 0; 852a5aedd68SStacey Son #endif 853dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 854dfaa7859SMateusz Guzik int doing_lockprof = 1; 855dfaa7859SMateusz Guzik #endif 8562502c107SJeff Roberson 8572502c107SJeff Roberson tid = (uintptr_t)curthread; 85835370593SAndriy Gapon 859f61d6c5aSMark Johnston if (SCHEDULER_STOPPED()) { 860f61d6c5aSMark Johnston /* 861f61d6c5aSMark Johnston * Ensure that spinlock sections are balanced even when the 862f61d6c5aSMark Johnston * scheduler is stopped, since we may otherwise inadvertently 863f61d6c5aSMark Johnston * re-enable interrupts while dumping core. 864f61d6c5aSMark Johnston */ 865f61d6c5aSMark Johnston spinlock_enter(); 86635370593SAndriy Gapon return; 867f61d6c5aSMark Johnston } 86835370593SAndriy Gapon 869a0d45f0fSMateusz Guzik lock_delay_arg_init(&lda, &mtx_spin_delay); 870a0d45f0fSMateusz Guzik 8711f4d28c7SMateusz Guzik #ifdef HWPMC_HOOKS 8721f4d28c7SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 8731f4d28c7SMateusz Guzik #endif 8741f4d28c7SMateusz Guzik 875dfaa7859SMateusz Guzik #ifdef LOCK_PROFILING 876dfaa7859SMateusz Guzik doing_lockprof = 1; 877df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS) 878dfaa7859SMateusz Guzik doing_lockprof = lockstat_enabled; 879dfaa7859SMateusz Guzik if (__predict_false(doing_lockprof)) 880e2b25737SMark Johnston spin_time -= lockstat_nsecs(&td->td_lock->lock_object); 881076dd8ebSAndriy Gapon #endif 8829f4e008dSMateusz Guzik spinlock_enter(); 8839f4e008dSMateusz Guzik 8842502c107SJeff Roberson for (;;) { 8852502c107SJeff Roberson retry: 886710eacdcSJeff Roberson m = td->td_lock; 887be49509eSMateusz Guzik thread_lock_validate(m, opts, file, line); 8881f4d28c7SMateusz Guzik v = MTX_READ_VALUE(m); 889fc4f686dSMateusz Guzik for (;;) { 8901f4d28c7SMateusz Guzik if (v == MTX_UNOWNED) { 89190836c32SMateusz Guzik if (_mtx_obtain_lock_fetch(m, &v, tid)) 892fc4f686dSMateusz Guzik break; 8935e5ad162SMateusz Guzik continue; 8941f4d28c7SMateusz Guzik } 8955e5ad162SMateusz Guzik if (v == tid) { 8962502c107SJeff Roberson m->mtx_recurse++; 8979f4e008dSMateusz Guzik MPASS(m == td->td_lock); 8982502c107SJeff Roberson break; 8992502c107SJeff Roberson } 900eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&m->lock_object, 901eea4f254SJeff Roberson &contested, &waittime); 9022502c107SJeff Roberson /* Give interrupts a chance while we spin. */ 9032502c107SJeff Roberson spinlock_exit(); 9045e5ad162SMateusz Guzik do { 90515140a8aSMateusz Guzik if (__predict_true(lda.spin_cnt < 10000000)) { 906a0d45f0fSMateusz Guzik lock_delay(&lda); 907a0d45f0fSMateusz Guzik } else { 90815140a8aSMateusz Guzik _mtx_lock_indefinite_check(m, &lda); 909a0d45f0fSMateusz Guzik } 9109f4e008dSMateusz Guzik if (m != td->td_lock) { 9119f4e008dSMateusz Guzik spinlock_enter(); 9122502c107SJeff Roberson goto retry; 9139f4e008dSMateusz Guzik } 9145e5ad162SMateusz Guzik v = MTX_READ_VALUE(m); 9155e5ad162SMateusz Guzik } while (v != MTX_UNOWNED); 9162502c107SJeff Roberson spinlock_enter(); 9172502c107SJeff Roberson } 9182502c107SJeff Roberson if (m == td->td_lock) 9192502c107SJeff Roberson break; 9209f4e008dSMateusz Guzik MPASS(m->mtx_recurse == 0); 9219f4e008dSMateusz Guzik _mtx_release_lock_quick(m); 9222502c107SJeff Roberson } 923dfaa7859SMateusz Guzik LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file, 924dfaa7859SMateusz Guzik line); 925dfaa7859SMateusz Guzik WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line); 926dfaa7859SMateusz Guzik 927dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 928dfaa7859SMateusz Guzik if (__predict_true(!doing_lockprof)) 929dfaa7859SMateusz Guzik return; 930dfaa7859SMateusz Guzik #endif 931076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 932e2b25737SMark Johnston spin_time += lockstat_nsecs(&m->lock_object); 933076dd8ebSAndriy Gapon #endif 934eea4f254SJeff Roberson if (m->mtx_recurse == 0) 93532cd0147SMark Johnston LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, 93632cd0147SMark Johnston contested, waittime, file, line); 9375002e195SMark Johnston #ifdef KDTRACE_HOOKS 938d0f68f91SMark Johnston if (lda.spin_cnt != 0) 93932cd0147SMark Johnston LOCKSTAT_RECORD1(thread__spin, m, spin_time); 9405002e195SMark Johnston #endif 9412502c107SJeff Roberson } 9422502c107SJeff Roberson 9432502c107SJeff Roberson struct mtx * 9442502c107SJeff Roberson thread_lock_block(struct thread *td) 9452502c107SJeff Roberson { 9462502c107SJeff Roberson struct mtx *lock; 9472502c107SJeff Roberson 9482502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 949710eacdcSJeff Roberson lock = td->td_lock; 9502502c107SJeff Roberson td->td_lock = &blocked_lock; 9512502c107SJeff Roberson mtx_unlock_spin(lock); 9522502c107SJeff Roberson 9532502c107SJeff Roberson return (lock); 9542502c107SJeff Roberson } 9552502c107SJeff Roberson 9562502c107SJeff Roberson void 9572502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new) 9582502c107SJeff Roberson { 9592502c107SJeff Roberson mtx_assert(new, MA_OWNED); 9602502c107SJeff Roberson MPASS(td->td_lock == &blocked_lock); 96165d32cd8SMatt Jacob atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new); 9622502c107SJeff Roberson } 9632502c107SJeff Roberson 9642502c107SJeff Roberson void 9652502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new) 9662502c107SJeff Roberson { 9672502c107SJeff Roberson struct mtx *lock; 9682502c107SJeff Roberson 9692502c107SJeff Roberson mtx_assert(new, MA_OWNED); 9702502c107SJeff Roberson THREAD_LOCK_ASSERT(td, MA_OWNED); 971710eacdcSJeff Roberson lock = td->td_lock; 9722502c107SJeff Roberson td->td_lock = new; 9732502c107SJeff Roberson mtx_unlock_spin(lock); 9742502c107SJeff Roberson } 9752502c107SJeff Roberson 9769ed346baSBosko Milekic /* 9777f44c618SAttilio Rao * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 9789ed346baSBosko Milekic * 9793b3cf014SMateusz Guzik * We are only called here if the lock is recursed, contested (i.e. we 9803b3cf014SMateusz Guzik * need to wake up a blocked thread) or lockstat probe is active. 9819ed346baSBosko Milekic */ 98209f1319aSMateusz Guzik #if LOCK_DEBUG > 0 98336412d79SJohn Baldwin void 984b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, 985b584eb2eSMateusz Guzik const char *file, int line) 98609f1319aSMateusz Guzik #else 98709f1319aSMateusz Guzik void 988b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v) 98909f1319aSMateusz Guzik #endif 99036412d79SJohn Baldwin { 9917f44c618SAttilio Rao struct mtx *m; 992961a7b24SJohn Baldwin struct turnstile *ts; 993b584eb2eSMateusz Guzik uintptr_t tid; 9949ed346baSBosko Milekic 99535370593SAndriy Gapon if (SCHEDULER_STOPPED()) 99635370593SAndriy Gapon return; 99735370593SAndriy Gapon 9983b3cf014SMateusz Guzik tid = (uintptr_t)curthread; 9997f44c618SAttilio Rao m = mtxlock2mtx(c); 1000b584eb2eSMateusz Guzik 1001b584eb2eSMateusz Guzik if (__predict_false(v == tid)) 10023b3cf014SMateusz Guzik v = MTX_READ_VALUE(m); 100390836c32SMateusz Guzik 1004b584eb2eSMateusz Guzik if (__predict_false(v & MTX_RECURSED)) { 100536412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 100608812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 1007aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 10089ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 100936412d79SJohn Baldwin return; 101036412d79SJohn Baldwin } 10119ed346baSBosko Milekic 10123b3cf014SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m); 10133b3cf014SMateusz Guzik if (v == tid && _mtx_release_lock(m, tid)) 10143b3cf014SMateusz Guzik return; 10153b3cf014SMateusz Guzik 10162502c107SJeff Roberson /* 10172502c107SJeff Roberson * We have to lock the chain before the turnstile so this turnstile 10182502c107SJeff Roberson * can be removed from the hash list if it is empty. 10192502c107SJeff Roberson */ 10202502c107SJeff Roberson turnstile_chain_lock(&m->lock_object); 10218448e020SMateusz Guzik _mtx_release_lock_quick(m); 1022aa89d8cdSJohn Baldwin ts = turnstile_lookup(&m->lock_object); 10238448e020SMateusz Guzik MPASS(ts != NULL); 1024aa89d8cdSJohn Baldwin if (LOCK_LOG_TEST(&m->lock_object, opts)) 10259ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 10267aa4f685SJohn Baldwin turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 1027bf9c6c31SJohn Baldwin 10282502c107SJeff Roberson /* 10292502c107SJeff Roberson * This turnstile is now no longer associated with the mutex. We can 10302502c107SJeff Roberson * unlock the chain lock so a new turnstile may take it's place. 10312502c107SJeff Roberson */ 1032*d0a22279SMateusz Guzik turnstile_unpend(ts); 10332502c107SJeff Roberson turnstile_chain_unlock(&m->lock_object); 10349ed346baSBosko Milekic } 10359ed346baSBosko Milekic 10369ed346baSBosko Milekic /* 10379ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 1038961135eaSJohn Baldwin * See the __mtx_unlock_spin() macro for the details. 10399ed346baSBosko Milekic */ 10409ed346baSBosko Milekic 10419ed346baSBosko Milekic /* 104215ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 10439ed346baSBosko Milekic */ 10441103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 10450cde2e34SJason Evans void 10467f44c618SAttilio Rao __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line) 10470cde2e34SJason Evans { 10487f44c618SAttilio Rao const struct mtx *m; 10495cb0fbe4SJohn Baldwin 1050310ab671SEric van Gyzen if (panicstr != NULL || dumping || SCHEDULER_STOPPED()) 10515cb0fbe4SJohn Baldwin return; 10527f44c618SAttilio Rao 10537f44c618SAttilio Rao m = mtxlock2mtx(c); 10547f44c618SAttilio Rao 1055a10f4966SJake Burkholder switch (what) { 10560cde2e34SJason Evans case MA_OWNED: 10570cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 10580cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 1059a10f4966SJake Burkholder if (!mtx_owned(m)) 10600cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 1061aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 1062a10f4966SJake Burkholder if (mtx_recursed(m)) { 1063a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 10640cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 1065aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 1066a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 10670cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 1068aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 10690cde2e34SJason Evans } 10700cde2e34SJason Evans break; 10710cde2e34SJason Evans case MA_NOTOWNED: 1072a10f4966SJake Burkholder if (mtx_owned(m)) 10730cde2e34SJason Evans panic("mutex %s owned at %s:%d", 1074aa89d8cdSJohn Baldwin m->lock_object.lo_name, file, line); 10750cde2e34SJason Evans break; 10760cde2e34SJason Evans default: 107756771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 10780cde2e34SJason Evans } 10790cde2e34SJason Evans } 10800cde2e34SJason Evans #endif 10810cde2e34SJason Evans 10829ed346baSBosko Milekic /* 1083c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 1084c27b5699SAndrew R. Reiter */ 1085c27b5699SAndrew R. Reiter void 1086c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 1087c27b5699SAndrew R. Reiter { 1088c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 1089c27b5699SAndrew R. Reiter 10907f44c618SAttilio Rao mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL, 10917f44c618SAttilio Rao margs->ma_opts); 1092c27b5699SAndrew R. Reiter } 1093c27b5699SAndrew R. Reiter 1094c27b5699SAndrew R. Reiter /* 10959ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 10960c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 10970c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 10980c88508aSJohn Baldwin * witness. 10999ed346baSBosko Milekic */ 110036412d79SJohn Baldwin void 11017f44c618SAttilio Rao _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts) 110236412d79SJohn Baldwin { 11037f44c618SAttilio Rao struct mtx *m; 110483a81bcbSJohn Baldwin struct lock_class *class; 110583a81bcbSJohn Baldwin int flags; 11069ed346baSBosko Milekic 11077f44c618SAttilio Rao m = mtxlock2mtx(c); 11087f44c618SAttilio Rao 110919284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 1110fd07ddcfSDmitry Chagin MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0); 1111353998acSAttilio Rao ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock, 1112353998acSAttilio Rao ("%s: mtx_lock not aligned for %s: %p", __func__, name, 1113353998acSAttilio Rao &m->mtx_lock)); 11149ed346baSBosko Milekic 111583a81bcbSJohn Baldwin /* Determine lock class and lock flags. */ 111619284646SJohn Baldwin if (opts & MTX_SPIN) 111783a81bcbSJohn Baldwin class = &lock_class_mtx_spin; 111819284646SJohn Baldwin else 111983a81bcbSJohn Baldwin class = &lock_class_mtx_sleep; 112083a81bcbSJohn Baldwin flags = 0; 112119284646SJohn Baldwin if (opts & MTX_QUIET) 112283a81bcbSJohn Baldwin flags |= LO_QUIET; 112319284646SJohn Baldwin if (opts & MTX_RECURSE) 112483a81bcbSJohn Baldwin flags |= LO_RECURSABLE; 112519284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 112683a81bcbSJohn Baldwin flags |= LO_WITNESS; 1127f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 112883a81bcbSJohn Baldwin flags |= LO_DUPOK; 11297c0435b9SKip Macy if (opts & MTX_NOPROFILE) 11307c0435b9SKip Macy flags |= LO_NOPROFILE; 1131fd07ddcfSDmitry Chagin if (opts & MTX_NEW) 1132fd07ddcfSDmitry Chagin flags |= LO_NEW; 113319284646SJohn Baldwin 113483a81bcbSJohn Baldwin /* Initialize mutex. */ 1135b5fb43e5SJohn Baldwin lock_init(&m->lock_object, class, name, type, flags); 1136b5fb43e5SJohn Baldwin 113719284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 113883a81bcbSJohn Baldwin m->mtx_recurse = 0; 113936412d79SJohn Baldwin } 114036412d79SJohn Baldwin 11419ed346baSBosko Milekic /* 114219284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 114319284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 114419284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 114519284646SJohn Baldwin * flags. 11469ed346baSBosko Milekic */ 114736412d79SJohn Baldwin void 11487f44c618SAttilio Rao _mtx_destroy(volatile uintptr_t *c) 114936412d79SJohn Baldwin { 11507f44c618SAttilio Rao struct mtx *m; 11517f44c618SAttilio Rao 11527f44c618SAttilio Rao m = mtxlock2mtx(c); 115336412d79SJohn Baldwin 115419284646SJohn Baldwin if (!mtx_owned(m)) 115519284646SJohn Baldwin MPASS(mtx_unowned(m)); 115619284646SJohn Baldwin else { 115708812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 11589ed346baSBosko Milekic 1159861a2308SScott Long /* Perform the non-mtx related part of mtx_unlock_spin(). */ 1160aa89d8cdSJohn Baldwin if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) 1161861a2308SScott Long spinlock_exit(); 1162764e4d54SJohn Baldwin else 1163ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 1164861a2308SScott Long 1165d3df4af3SJeff Roberson lock_profile_release_lock(&m->lock_object); 116619284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 1167aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__, 1168c86b6ff5SJohn Baldwin __LINE__); 116936412d79SJohn Baldwin } 11700384fff8SJason Evans 1171186abbd7SJohn Baldwin m->mtx_lock = MTX_DESTROYED; 1172aa89d8cdSJohn Baldwin lock_destroy(&m->lock_object); 11730384fff8SJason Evans } 1174d23f5958SMatthew Dillon 1175d23f5958SMatthew Dillon /* 1176c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 1177c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 1178c53c013bSJohn Baldwin * setup before this is called. 1179c53c013bSJohn Baldwin */ 1180c53c013bSJohn Baldwin void 1181c53c013bSJohn Baldwin mutex_init(void) 1182c53c013bSJohn Baldwin { 1183c53c013bSJohn Baldwin 1184961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 1185961a7b24SJohn Baldwin init_turnstiles(); 1186961a7b24SJohn Baldwin 1187c53c013bSJohn Baldwin /* 1188c53c013bSJohn Baldwin * Initialize mutexes. 1189c53c013bSJohn Baldwin */ 11900c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 11912502c107SJeff Roberson mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN); 11922502c107SJeff Roberson blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */ 11930c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 11946afb32fcSKonstantin Belousov mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN); 11955c7bebf9SKonstantin Belousov mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN); 11965c7bebf9SKonstantin Belousov mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN); 11975c7bebf9SKonstantin Belousov mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN); 11988c4b6380SJohn Baldwin mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 1199c53c013bSJohn Baldwin mtx_lock(&Giant); 1200c53c013bSJohn Baldwin } 1201d272fe53SJohn Baldwin 120215140a8aSMateusz Guzik static void __noinline 120315140a8aSMateusz Guzik _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap) 120415140a8aSMateusz Guzik { 120515140a8aSMateusz Guzik struct thread *td; 120615140a8aSMateusz Guzik 120715140a8aSMateusz Guzik ldap->spin_cnt++; 120815140a8aSMateusz Guzik if (ldap->spin_cnt < 60000000 || kdb_active || panicstr != NULL) 120915140a8aSMateusz Guzik DELAY(1); 121015140a8aSMateusz Guzik else { 121115140a8aSMateusz Guzik td = mtx_owner(m); 121215140a8aSMateusz Guzik 121315140a8aSMateusz Guzik /* If the mutex is unlocked, try again. */ 121415140a8aSMateusz Guzik if (td == NULL) 121515140a8aSMateusz Guzik return; 121615140a8aSMateusz Guzik 121715140a8aSMateusz Guzik printf( "spin lock %p (%s) held by %p (tid %d) too long\n", 121815140a8aSMateusz Guzik m, m->lock_object.lo_name, td, td->td_tid); 121915140a8aSMateusz Guzik #ifdef WITNESS 122015140a8aSMateusz Guzik witness_display_spinlock(&m->lock_object, td, printf); 122115140a8aSMateusz Guzik #endif 122215140a8aSMateusz Guzik panic("spin lock held too long"); 122315140a8aSMateusz Guzik } 122415140a8aSMateusz Guzik cpu_spinwait(); 122515140a8aSMateusz Guzik } 122615140a8aSMateusz Guzik 1227d2576988SMateusz Guzik void 1228d2576988SMateusz Guzik mtx_spin_wait_unlocked(struct mtx *m) 1229d2576988SMateusz Guzik { 1230d2576988SMateusz Guzik struct lock_delay_arg lda; 1231d2576988SMateusz Guzik 1232500ca73dSMateusz Guzik KASSERT(m->mtx_lock != MTX_DESTROYED, 1233500ca73dSMateusz Guzik ("%s() of destroyed mutex %p", __func__, m)); 1234500ca73dSMateusz Guzik KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin, 1235500ca73dSMateusz Guzik ("%s() of sleep mutex %p (%s)", __func__, m, 1236500ca73dSMateusz Guzik m->lock_object.lo_name)); 1237500ca73dSMateusz Guzik KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m, 1238500ca73dSMateusz Guzik m->lock_object.lo_name)); 1239500ca73dSMateusz Guzik 1240d2576988SMateusz Guzik lda.spin_cnt = 0; 1241d2576988SMateusz Guzik 1242d2576988SMateusz Guzik while (atomic_load_acq_ptr(&m->mtx_lock) != MTX_UNOWNED) { 1243d2576988SMateusz Guzik if (__predict_true(lda.spin_cnt < 10000000)) { 1244d2576988SMateusz Guzik cpu_spinwait(); 1245d2576988SMateusz Guzik lda.spin_cnt++; 1246d2576988SMateusz Guzik } else { 1247d2576988SMateusz Guzik _mtx_lock_indefinite_check(m, &lda); 1248d2576988SMateusz Guzik } 1249d2576988SMateusz Guzik } 1250d2576988SMateusz Guzik } 1251d2576988SMateusz Guzik 1252d272fe53SJohn Baldwin #ifdef DDB 1253d272fe53SJohn Baldwin void 1254d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock) 1255d272fe53SJohn Baldwin { 1256d272fe53SJohn Baldwin struct thread *td; 1257d576deedSPawel Jakub Dawidek const struct mtx *m; 1258d272fe53SJohn Baldwin 1259d576deedSPawel Jakub Dawidek m = (const struct mtx *)lock; 1260d272fe53SJohn Baldwin 1261d272fe53SJohn Baldwin db_printf(" flags: {"); 126283a81bcbSJohn Baldwin if (LOCK_CLASS(lock) == &lock_class_mtx_spin) 1263d272fe53SJohn Baldwin db_printf("SPIN"); 1264d272fe53SJohn Baldwin else 1265d272fe53SJohn Baldwin db_printf("DEF"); 1266aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_RECURSABLE) 1267d272fe53SJohn Baldwin db_printf(", RECURSE"); 1268aa89d8cdSJohn Baldwin if (m->lock_object.lo_flags & LO_DUPOK) 1269d272fe53SJohn Baldwin db_printf(", DUPOK"); 1270d272fe53SJohn Baldwin db_printf("}\n"); 1271d272fe53SJohn Baldwin db_printf(" state: {"); 1272d272fe53SJohn Baldwin if (mtx_unowned(m)) 1273d272fe53SJohn Baldwin db_printf("UNOWNED"); 1274c0bfd703SJohn Baldwin else if (mtx_destroyed(m)) 1275c0bfd703SJohn Baldwin db_printf("DESTROYED"); 1276d272fe53SJohn Baldwin else { 1277d272fe53SJohn Baldwin db_printf("OWNED"); 1278d272fe53SJohn Baldwin if (m->mtx_lock & MTX_CONTESTED) 1279d272fe53SJohn Baldwin db_printf(", CONTESTED"); 1280d272fe53SJohn Baldwin if (m->mtx_lock & MTX_RECURSED) 1281d272fe53SJohn Baldwin db_printf(", RECURSED"); 1282d272fe53SJohn Baldwin } 1283d272fe53SJohn Baldwin db_printf("}\n"); 1284c0bfd703SJohn Baldwin if (!mtx_unowned(m) && !mtx_destroyed(m)) { 1285d272fe53SJohn Baldwin td = mtx_owner(m); 1286d272fe53SJohn Baldwin db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td, 1287431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 1288d272fe53SJohn Baldwin if (mtx_recursed(m)) 1289d272fe53SJohn Baldwin db_printf(" recursed: %d\n", m->mtx_recurse); 1290d272fe53SJohn Baldwin } 1291d272fe53SJohn Baldwin } 1292d272fe53SJohn Baldwin #endif 1293