xref: /freebsd/sys/kern/kern_mutex.c (revision dba45599c498deed01e1c98acef74e183c1bbf8d)
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>
392498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h"
409c36c934SJohn Baldwin #include "opt_ddb.h"
41f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h"
429923b511SScott Long #include "opt_sched.h"
43a5a96a19SJohn Baldwin 
440384fff8SJason Evans #include <sys/param.h>
456c35e809SDag-Erling Smørgrav #include <sys/systm.h>
4636412d79SJohn Baldwin #include <sys/bus.h>
471126349aSPaul Saab #include <sys/conf.h>
482d50560aSMarcel Moolenaar #include <sys/kdb.h>
4936412d79SJohn Baldwin #include <sys/kernel.h>
506c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
5119284646SJohn Baldwin #include <sys/lock.h>
52fb919e4dSMark Murray #include <sys/malloc.h>
5319284646SJohn Baldwin #include <sys/mutex.h>
540384fff8SJason Evans #include <sys/proc.h>
55c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
56b43179fbSJeff Roberson #include <sys/sched.h>
576c35e809SDag-Erling Smørgrav #include <sys/sbuf.h>
581ada9041SMateusz Guzik #include <sys/smp.h>
59a5a96a19SJohn Baldwin #include <sys/sysctl.h>
60961a7b24SJohn Baldwin #include <sys/turnstile.h>
6136412d79SJohn Baldwin #include <sys/vmmeter.h>
627c0435b9SKip Macy #include <sys/lock_profile.h>
630384fff8SJason Evans 
6436412d79SJohn Baldwin #include <machine/atomic.h>
6536412d79SJohn Baldwin #include <machine/bus.h>
660384fff8SJason Evans #include <machine/cpu.h>
6736412d79SJohn Baldwin 
689c36c934SJohn Baldwin #include <ddb/ddb.h>
699c36c934SJohn Baldwin 
708c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h>
718c4b6380SJohn Baldwin 
7236412d79SJohn Baldwin #include <vm/vm.h>
7336412d79SJohn Baldwin #include <vm/vm_extern.h>
7436412d79SJohn Baldwin 
75cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
76cd6e6e4eSJohn Baldwin #define	ADAPTIVE_MUTEXES
77cd6e6e4eSJohn Baldwin #endif
78cd6e6e4eSJohn Baldwin 
79f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
80f5f9340bSFabien Thomas #include <sys/pmckern.h>
81f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , lock, failed);
82f5f9340bSFabien Thomas #endif
83f5f9340bSFabien Thomas 
84b9a80acaSStephan Uphoff /*
857f44c618SAttilio Rao  * Return the mutex address when the lock cookie address is provided.
867f44c618SAttilio Rao  * This functionality assumes that struct mtx* have a member named mtx_lock.
877f44c618SAttilio Rao  */
887f44c618SAttilio Rao #define	mtxlock2mtx(c)	(__containerof(c, struct mtx, mtx_lock))
897f44c618SAttilio Rao 
907f44c618SAttilio Rao /*
919ed346baSBosko Milekic  * Internal utility macros.
920cde2e34SJason Evans  */
939ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
940cde2e34SJason Evans 
95c0bfd703SJohn Baldwin #define	mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
96c0bfd703SJohn Baldwin 
97d576deedSPawel Jakub Dawidek static void	assert_mtx(const struct lock_object *lock, int what);
98d272fe53SJohn Baldwin #ifdef DDB
99d576deedSPawel Jakub Dawidek static void	db_show_mtx(const struct lock_object *lock);
100d272fe53SJohn Baldwin #endif
1017faf4d90SDavide Italiano static void	lock_mtx(struct lock_object *lock, uintptr_t how);
1027faf4d90SDavide Italiano static void	lock_spin(struct lock_object *lock, uintptr_t how);
103656991b0SGleb Smirnoff static int	trylock_mtx(struct lock_object *lock, uintptr_t how);
104656991b0SGleb Smirnoff static int	trylock_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,
123656991b0SGleb Smirnoff 	.lc_trylock = trylock_mtx,
1246e21afd4SJohn Baldwin 	.lc_unlock = unlock_mtx,
125a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
126a5aedd68SStacey Son 	.lc_owner = owner_mtx,
127a5aedd68SStacey Son #endif
12819284646SJohn Baldwin };
12919284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
130ae8dde30SJohn Baldwin 	.lc_name = "spin mutex",
131ae8dde30SJohn Baldwin 	.lc_flags = LC_SPINLOCK | LC_RECURSABLE,
132f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
133d272fe53SJohn Baldwin #ifdef DDB
134ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
135d272fe53SJohn Baldwin #endif
1366e21afd4SJohn Baldwin 	.lc_lock = lock_spin,
137656991b0SGleb Smirnoff 	.lc_trylock = trylock_spin,
1386e21afd4SJohn Baldwin 	.lc_unlock = unlock_spin,
139a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
140a5aedd68SStacey Son 	.lc_owner = owner_mtx,
141a5aedd68SStacey Son #endif
1428484de75SJohn Baldwin };
1438484de75SJohn Baldwin 
1441ada9041SMateusz Guzik #ifdef ADAPTIVE_MUTEXES
1452e77cad1SMateusz Guzik #ifdef MUTEX_CUSTOM_BACKOFF
1467029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1477029da5cSPawel Biernacki     "mtx debugging");
1481ada9041SMateusz Guzik 
149574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_delay;
1501ada9041SMateusz Guzik 
1516b8dd26eSMateusz Guzik SYSCTL_U16(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
1521ada9041SMateusz Guzik     0, "");
1536b8dd26eSMateusz Guzik SYSCTL_U16(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
1541ada9041SMateusz Guzik     0, "");
1551ada9041SMateusz Guzik 
1568e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
1572e77cad1SMateusz Guzik #else
1582e77cad1SMateusz Guzik #define mtx_delay	locks_delay
1592e77cad1SMateusz Guzik #endif
1601ada9041SMateusz Guzik #endif
1611ada9041SMateusz Guzik 
1622e77cad1SMateusz Guzik #ifdef MUTEX_SPIN_CUSTOM_BACKOFF
1637029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin,
1647029da5cSPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
165a0d45f0fSMateusz Guzik     "mtx spin debugging");
166a0d45f0fSMateusz Guzik 
167574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_spin_delay;
168a0d45f0fSMateusz Guzik 
1698e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW,
1708e5a3e9aSMateusz Guzik     &mtx_spin_delay.base, 0, "");
1718e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW,
1728e5a3e9aSMateusz Guzik     &mtx_spin_delay.max, 0, "");
173a0d45f0fSMateusz Guzik 
1748e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
1752e77cad1SMateusz Guzik #else
1762e77cad1SMateusz Guzik #define mtx_spin_delay	locks_delay
1772e77cad1SMateusz Guzik #endif
178a0d45f0fSMateusz Guzik 
1799ed346baSBosko Milekic /*
180c53c013bSJohn Baldwin  * System-wide mutexes
181c53c013bSJohn Baldwin  */
1822502c107SJeff Roberson struct mtx blocked_lock;
1836a569d35SMateusz Guzik struct mtx __exclusive_cache_line Giant;
184c53c013bSJohn Baldwin 
18515140a8aSMateusz Guzik static void _mtx_lock_indefinite_check(struct mtx *, struct lock_delay_arg *);
18615140a8aSMateusz Guzik 
18795a9594aSGleb Smirnoff static void
188d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what)
189f9721b43SAttilio Rao {
190f9721b43SAttilio Rao 
191a11bf9a4SXin LI 	/*
192a11bf9a4SXin LI 	 * Treat LA_LOCKED as if LA_XLOCKED was asserted.
193a11bf9a4SXin LI 	 *
194a11bf9a4SXin LI 	 * Some callers of lc_assert uses LA_LOCKED to indicate that either
195a11bf9a4SXin LI 	 * a shared lock or write lock was held, while other callers uses
196a11bf9a4SXin LI 	 * the more strict LA_XLOCKED (used as MA_OWNED).
197a11bf9a4SXin LI 	 *
198a11bf9a4SXin LI 	 * Mutex is the only lock class that can not be shared, as a result,
199a11bf9a4SXin LI 	 * we can reasonably consider the caller really intends to assert
200a11bf9a4SXin LI 	 * LA_XLOCKED when they are asserting LA_LOCKED on a mutex object.
201a11bf9a4SXin LI 	 */
202a11bf9a4SXin LI 	if (what & LA_LOCKED) {
203a11bf9a4SXin LI 		what &= ~LA_LOCKED;
204a11bf9a4SXin LI 		what |= LA_XLOCKED;
205a11bf9a4SXin LI 	}
206d576deedSPawel Jakub Dawidek 	mtx_assert((const struct mtx *)lock, what);
207f9721b43SAttilio Rao }
208f9721b43SAttilio Rao 
20995a9594aSGleb Smirnoff static void
2107faf4d90SDavide Italiano lock_mtx(struct lock_object *lock, uintptr_t how)
2116e21afd4SJohn Baldwin {
2126e21afd4SJohn Baldwin 
2136e21afd4SJohn Baldwin 	mtx_lock((struct mtx *)lock);
2146e21afd4SJohn Baldwin }
2156e21afd4SJohn Baldwin 
21695a9594aSGleb Smirnoff static void
2177faf4d90SDavide Italiano lock_spin(struct lock_object *lock, uintptr_t how)
2186e21afd4SJohn Baldwin {
2196e21afd4SJohn Baldwin 
2204730a897SAlexander Motin 	mtx_lock_spin((struct mtx *)lock);
2216e21afd4SJohn Baldwin }
2226e21afd4SJohn Baldwin 
223656991b0SGleb Smirnoff static int
224656991b0SGleb Smirnoff trylock_mtx(struct lock_object *lock, uintptr_t how)
225656991b0SGleb Smirnoff {
226656991b0SGleb Smirnoff 
227656991b0SGleb Smirnoff 	return (mtx_trylock((struct mtx *)lock));
228656991b0SGleb Smirnoff }
229656991b0SGleb Smirnoff 
230656991b0SGleb Smirnoff static int
231656991b0SGleb Smirnoff trylock_spin(struct lock_object *lock, uintptr_t how)
232656991b0SGleb Smirnoff {
233656991b0SGleb Smirnoff 
234656991b0SGleb Smirnoff 	return (mtx_trylock_spin((struct mtx *)lock));
235656991b0SGleb Smirnoff }
236656991b0SGleb Smirnoff 
23795a9594aSGleb Smirnoff static uintptr_t
2386e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock)
2396e21afd4SJohn Baldwin {
2406e21afd4SJohn Baldwin 	struct mtx *m;
2416e21afd4SJohn Baldwin 
2426e21afd4SJohn Baldwin 	m = (struct mtx *)lock;
2436e21afd4SJohn Baldwin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
2446e21afd4SJohn Baldwin 	mtx_unlock(m);
2456e21afd4SJohn Baldwin 	return (0);
2466e21afd4SJohn Baldwin }
2476e21afd4SJohn Baldwin 
24895a9594aSGleb Smirnoff static uintptr_t
2496e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock)
2506e21afd4SJohn Baldwin {
2514730a897SAlexander Motin 	struct mtx *m;
2526e21afd4SJohn Baldwin 
2534730a897SAlexander Motin 	m = (struct mtx *)lock;
2544730a897SAlexander Motin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
2554730a897SAlexander Motin 	mtx_unlock_spin(m);
2564730a897SAlexander Motin 	return (0);
2576e21afd4SJohn Baldwin }
2586e21afd4SJohn Baldwin 
259a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
26095a9594aSGleb Smirnoff static int
261d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner)
262a5aedd68SStacey Son {
26302315a67SMark Johnston 	const struct mtx *m;
26402315a67SMark Johnston 	uintptr_t x;
265a5aedd68SStacey Son 
26602315a67SMark Johnston 	m = (const struct mtx *)lock;
26702315a67SMark Johnston 	x = m->mtx_lock;
26802315a67SMark Johnston 	*owner = (struct thread *)(x & ~MTX_FLAGMASK);
269e280ce46SMateusz Guzik 	return (*owner != NULL);
270a5aedd68SStacey Son }
271a5aedd68SStacey Son #endif
272a5aedd68SStacey Son 
2730cde2e34SJason Evans /*
2746283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2756283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2766283b7d0SJohn Baldwin  */
2776283b7d0SJohn Baldwin void
2787f44c618SAttilio Rao __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
2796283b7d0SJohn Baldwin {
2807f44c618SAttilio Rao 	struct mtx *m;
28108da2677SMateusz Guzik 	uintptr_t tid, v;
2826283b7d0SJohn Baldwin 
2837f44c618SAttilio Rao 	m = mtxlock2mtx(c);
2847f44c618SAttilio Rao 
285704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
286704cb42fSMark Johnston 	    !TD_IS_IDLETHREAD(curthread),
287e3ae0dfeSAttilio Rao 	    ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
288e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
289186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
290186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
291*dba45599SJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) != &lock_class_mtx_spin,
292aa89d8cdSJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2930d975d63SJohn Baldwin 	    file, line));
294ac6b769bSAttilio Rao 	WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
295ac6b769bSAttilio Rao 	    LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
2967c0435b9SKip Macy 
29708da2677SMateusz Guzik 	tid = (uintptr_t)curthread;
29808da2677SMateusz Guzik 	v = MTX_UNOWNED;
29908da2677SMateusz Guzik 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
3002f1ddb89SMateusz Guzik 		_mtx_lock_sleep(m, v, opts, file, line);
30108da2677SMateusz Guzik 	else
30208da2677SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
30308da2677SMateusz Guzik 		    m, 0, 0, file, line);
304aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
305dde96c99SJohn Baldwin 	    line);
306ac6b769bSAttilio Rao 	WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
307ac6b769bSAttilio Rao 	    file, line);
308ce1c953eSMark Johnston 	TD_LOCKS_INC(curthread);
3096283b7d0SJohn Baldwin }
3106283b7d0SJohn Baldwin 
3116283b7d0SJohn Baldwin void
3127f44c618SAttilio Rao __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
3136283b7d0SJohn Baldwin {
3147f44c618SAttilio Rao 	struct mtx *m;
31535370593SAndriy Gapon 
3167f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3177f44c618SAttilio Rao 
318186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
319186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
320*dba45599SJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) != &lock_class_mtx_spin,
321aa89d8cdSJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
3220d975d63SJohn Baldwin 	    file, line));
323aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
324aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
3250d975d63SJohn Baldwin 	    line);
32621377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
327c66d7606SKip Macy 
328ffd5c94cSMateusz Guzik #ifdef LOCK_PROFILING
329b584eb2eSMateusz Guzik 	__mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
330ffd5c94cSMateusz Guzik #else
331ffd5c94cSMateusz Guzik 	__mtx_unlock(m, curthread, opts, file, line);
332ffd5c94cSMateusz Guzik #endif
333ce1c953eSMark Johnston 	TD_LOCKS_DEC(curthread);
3346283b7d0SJohn Baldwin }
3356283b7d0SJohn Baldwin 
3366283b7d0SJohn Baldwin void
3377f44c618SAttilio Rao __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
3387f44c618SAttilio Rao     int line)
3396283b7d0SJohn Baldwin {
3407f44c618SAttilio Rao 	struct mtx *m;
34162bf13cbSMateusz Guzik #ifdef SMP
3420d74fe26SMateusz Guzik 	uintptr_t tid, v;
34362bf13cbSMateusz Guzik #endif
3446283b7d0SJohn Baldwin 
3457f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3467f44c618SAttilio Rao 
347186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
348186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
349*dba45599SJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) != &lock_class_mtx_sleep,
3500d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
351aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
352ad69e26bSJohn Baldwin 	if (mtx_owned(m))
353ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
354ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
355ad69e26bSJohn Baldwin 	    ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
356ad69e26bSJohn Baldwin 		    m->lock_object.lo_name, file, line));
357ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
358aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
35941313430SJohn Baldwin 	    file, line, NULL);
36062bf13cbSMateusz Guzik #ifdef SMP
3610d74fe26SMateusz Guzik 	spinlock_enter();
3620d74fe26SMateusz Guzik 	tid = (uintptr_t)curthread;
3630d74fe26SMateusz Guzik 	v = MTX_UNOWNED;
3640d74fe26SMateusz Guzik 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
3650d74fe26SMateusz Guzik 		_mtx_lock_spin(m, v, opts, file, line);
3660d74fe26SMateusz Guzik 	else
3676a467cc5SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_SPIN_LOCK_SUCCESS(spin__acquire,
3680d74fe26SMateusz Guzik 		    m, 0, 0, file, line);
36962bf13cbSMateusz Guzik #else
37062bf13cbSMateusz Guzik 	__mtx_lock_spin(m, curthread, opts, file, line);
37162bf13cbSMateusz Guzik #endif
372aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
373dde96c99SJohn Baldwin 	    line);
374aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
3756283b7d0SJohn Baldwin }
3766283b7d0SJohn Baldwin 
37790b581f2SKonstantin Belousov int
37890b581f2SKonstantin Belousov __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
37990b581f2SKonstantin Belousov     int line)
38090b581f2SKonstantin Belousov {
38190b581f2SKonstantin Belousov 	struct mtx *m;
38290b581f2SKonstantin Belousov 
38390b581f2SKonstantin Belousov 	if (SCHEDULER_STOPPED())
38490b581f2SKonstantin Belousov 		return (1);
38590b581f2SKonstantin Belousov 
38690b581f2SKonstantin Belousov 	m = mtxlock2mtx(c);
38790b581f2SKonstantin Belousov 
38890b581f2SKonstantin Belousov 	KASSERT(m->mtx_lock != MTX_DESTROYED,
38990b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
390*dba45599SJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) != &lock_class_mtx_sleep,
39190b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
39290b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
39390b581f2SKonstantin Belousov 	KASSERT((opts & MTX_RECURSE) == 0,
39490b581f2SKonstantin Belousov 	    ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
39590b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
39690b581f2SKonstantin Belousov 	if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
39790b581f2SKonstantin Belousov 		LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
39890b581f2SKonstantin Belousov 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
39990b581f2SKonstantin Belousov 		return (1);
40090b581f2SKonstantin Belousov 	}
40190b581f2SKonstantin Belousov 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
40290b581f2SKonstantin Belousov 	return (0);
40390b581f2SKonstantin Belousov }
40490b581f2SKonstantin Belousov 
4056283b7d0SJohn Baldwin void
4067f44c618SAttilio Rao __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
4077f44c618SAttilio Rao     int line)
4086283b7d0SJohn Baldwin {
4097f44c618SAttilio Rao 	struct mtx *m;
410c66d7606SKip Macy 
4117f44c618SAttilio Rao 	m = mtxlock2mtx(c);
4127f44c618SAttilio Rao 
413186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
414186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
415*dba45599SJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) != &lock_class_mtx_sleep,
4160d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
417aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
418aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
419aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
420dde96c99SJohn Baldwin 	    line);
4210d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
422c66d7606SKip Macy 
423961135eaSJohn Baldwin 	__mtx_unlock_spin(m);
4246283b7d0SJohn Baldwin }
4256283b7d0SJohn Baldwin 
4266283b7d0SJohn Baldwin /*
4279ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
428eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
429eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
4300cde2e34SJason Evans  */
4310cde2e34SJason Evans int
432013c0b49SMateusz Guzik _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
4330cde2e34SJason Evans {
4345c5df0d9SMateusz Guzik 	struct thread *td;
4355c5df0d9SMateusz Guzik 	uintptr_t tid, v;
4361723a064SJeff Roberson #ifdef LOCK_PROFILING
4377c0435b9SKip Macy 	uint64_t waittime = 0;
4381723a064SJeff Roberson 	int contested = 0;
4391723a064SJeff Roberson #endif
4401723a064SJeff Roberson 	int rval;
4415c5df0d9SMateusz Guzik 	bool recursed;
4420cde2e34SJason Evans 
4435c5df0d9SMateusz Guzik 	td = curthread;
4445c5df0d9SMateusz Guzik 	tid = (uintptr_t)td;
4456b353101SOlivier Certner 	if (SCHEDULER_STOPPED())
44635370593SAndriy Gapon 		return (1);
44735370593SAndriy Gapon 
448704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
449e3ae0dfeSAttilio Rao 	    ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
450e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
451186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
452186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
453*dba45599SJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) != &lock_class_mtx_spin,
454aa89d8cdSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
45583cece6fSJohn Baldwin 	    file, line));
4569ed346baSBosko Milekic 
4575c5df0d9SMateusz Guzik 	rval = 1;
4585c5df0d9SMateusz Guzik 	recursed = false;
4595c5df0d9SMateusz Guzik 	v = MTX_UNOWNED;
460b247fd39SMateusz Guzik 	for (;;) {
461b247fd39SMateusz Guzik 		if (_mtx_obtain_lock_fetch(m, &v, tid))
462b247fd39SMateusz Guzik 			break;
463b247fd39SMateusz Guzik 		if (v == MTX_UNOWNED)
464b247fd39SMateusz Guzik 			continue;
4655c5df0d9SMateusz Guzik 		if (v == tid &&
4665c5df0d9SMateusz Guzik 		    ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
467ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0)) {
468eac09796SJohn Baldwin 			m->mtx_recurse++;
469eac09796SJohn Baldwin 			atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
4705c5df0d9SMateusz Guzik 			recursed = true;
471b247fd39SMateusz Guzik 			break;
4725c5df0d9SMateusz Guzik 		}
473b247fd39SMateusz Guzik 		rval = 0;
474b247fd39SMateusz Guzik 		break;
4755c5df0d9SMateusz Guzik 	}
4765c5df0d9SMateusz Guzik 
477ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
4789ed346baSBosko Milekic 
479aa89d8cdSJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
480764e4d54SJohn Baldwin 	if (rval) {
481aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4822d96f0b1SJohn Baldwin 		    file, line);
483ce1c953eSMark Johnston 		TD_LOCKS_INC(curthread);
4845c5df0d9SMateusz Guzik 		if (!recursed)
48532cd0147SMark Johnston 			LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
486a5aedd68SStacey Son 			    m, contested, waittime, file, line);
487764e4d54SJohn Baldwin 	}
4889ed346baSBosko Milekic 
48919284646SJohn Baldwin 	return (rval);
4900cde2e34SJason Evans }
4910cde2e34SJason Evans 
492013c0b49SMateusz Guzik int
493013c0b49SMateusz Guzik _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
494013c0b49SMateusz Guzik {
495013c0b49SMateusz Guzik 	struct mtx *m;
496013c0b49SMateusz Guzik 
497013c0b49SMateusz Guzik 	m = mtxlock2mtx(c);
498013c0b49SMateusz Guzik 	return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
499013c0b49SMateusz Guzik }
500013c0b49SMateusz Guzik 
5010cde2e34SJason Evans /*
5027f44c618SAttilio Rao  * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
5039ed346baSBosko Milekic  *
5049ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
5059ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
5060cde2e34SJason Evans  */
50709f1319aSMateusz Guzik #if LOCK_DEBUG > 0
5080cde2e34SJason Evans void
5092f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
5102f1ddb89SMateusz Guzik     int line)
51109f1319aSMateusz Guzik #else
51209f1319aSMateusz Guzik void
5132f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
51409f1319aSMateusz Guzik #endif
51536412d79SJohn Baldwin {
5162f1ddb89SMateusz Guzik 	struct thread *td;
5177f44c618SAttilio Rao 	struct mtx *m;
5182502c107SJeff Roberson 	struct turnstile *ts;
5192f1ddb89SMateusz Guzik 	uintptr_t tid;
5202ccee9ccSMateusz Guzik 	struct thread *owner;
5211723a064SJeff Roberson #ifdef LOCK_PROFILING
52270fe8436SKip Macy 	int contested = 0;
52370fe8436SKip Macy 	uint64_t waittime = 0;
5241723a064SJeff Roberson #endif
5251ada9041SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
5261ada9041SMateusz Guzik 	struct lock_delay_arg lda;
5271ada9041SMateusz Guzik #endif
528a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
52961852185SMateusz Guzik 	u_int sleep_cnt = 0;
530a5aedd68SStacey Son 	int64_t sleep_time = 0;
531076dd8ebSAndriy Gapon 	int64_t all_time = 0;
532dfaa7859SMateusz Guzik #endif
533dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
534f183fb16SMateusz Guzik 	int doing_lockprof = 0;
535a5aedd68SStacey Son #endif
53609bdec20SMateusz Guzik 
5372f1ddb89SMateusz Guzik 	td = curthread;
5382f1ddb89SMateusz Guzik 	tid = (uintptr_t)td;
53909bdec20SMateusz Guzik 	m = mtxlock2mtx(c);
54009bdec20SMateusz Guzik 
54109bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS
54209bdec20SMateusz Guzik 	if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
54309bdec20SMateusz Guzik 		while (v == MTX_UNOWNED) {
54409bdec20SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
54509bdec20SMateusz Guzik 				goto out_lockstat;
54609bdec20SMateusz Guzik 		}
54709bdec20SMateusz Guzik 		doing_lockprof = 1;
54809bdec20SMateusz Guzik 		all_time -= lockstat_nsecs(&m->lock_object);
54909bdec20SMateusz Guzik 	}
55009bdec20SMateusz Guzik #endif
55109bdec20SMateusz Guzik #ifdef LOCK_PROFILING
55209bdec20SMateusz Guzik 	doing_lockprof = 1;
55309bdec20SMateusz Guzik #endif
55409bdec20SMateusz Guzik 
5556b353101SOlivier Certner 	if (SCHEDULER_STOPPED())
55635370593SAndriy Gapon 		return;
55735370593SAndriy Gapon 
558c1aaf63cSMateusz Guzik 	if (__predict_false(v == MTX_UNOWNED))
559c1aaf63cSMateusz Guzik 		v = MTX_READ_VALUE(m);
5607f44c618SAttilio Rao 
5612f1ddb89SMateusz Guzik 	if (__predict_false(lv_mtx_owner(v) == td)) {
562ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
563ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
564eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
565aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, file, line));
566a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0
567ac6b769bSAttilio Rao 		opts &= ~MTX_RECURSE;
568a24c8eb8SMateusz Guzik #endif
56936412d79SJohn Baldwin 		m->mtx_recurse++;
57008812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
571aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
5725746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
57336412d79SJohn Baldwin 		return;
57436412d79SJohn Baldwin 	}
575a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0
576ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
577a24c8eb8SMateusz Guzik #endif
5789ed346baSBosko Milekic 
579f90d57b8SMateusz Guzik #if defined(ADAPTIVE_MUTEXES)
580f90d57b8SMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_delay);
581f90d57b8SMateusz Guzik #elif defined(KDTRACE_HOOKS)
582f90d57b8SMateusz Guzik 	lock_delay_arg_init_noadapt(&lda);
583f90d57b8SMateusz Guzik #endif
584f90d57b8SMateusz Guzik 
585f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
586f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
587f5f9340bSFabien Thomas #endif
5886a467cc5SMateusz Guzik 	lock_profile_obtain_lock_failed(&m->lock_object, false,
58970fe8436SKip Macy 		    &contested, &waittime);
590aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
59115ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
59215ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
593aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
5941bd0eefbSJohn Baldwin 
5957530de77SMateusz Guzik 	THREAD_CONTENDS_ON_LOCK(&m->lock_object);
5967530de77SMateusz Guzik 
597fc4f686dSMateusz Guzik 	for (;;) {
5982604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
59990836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
600fc4f686dSMateusz Guzik 				break;
6012604eb9eSMateusz Guzik 			continue;
6022604eb9eSMateusz Guzik 		}
603a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
6041ada9041SMateusz Guzik 		lda.spin_cnt++;
605a5aedd68SStacey Son #endif
60649aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
60749aead8aSAttilio Rao 		/*
60849aead8aSAttilio Rao 		 * If the owner is running on another CPU, spin until the
60949aead8aSAttilio Rao 		 * owner stops running or the state of the lock changes.
61049aead8aSAttilio Rao 		 */
6112604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
61249aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
61349aead8aSAttilio Rao 			if (LOCK_LOG_TEST(&m->lock_object, 0))
61449aead8aSAttilio Rao 				CTR3(KTR_LOCK,
61549aead8aSAttilio Rao 				    "%s: spinning on %p held by %p",
61649aead8aSAttilio Rao 				    __func__, m, owner);
6172cba8dd3SJohn Baldwin 			KTR_STATE1(KTR_SCHED, "thread",
6182cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
6192cba8dd3SJohn Baldwin 			    "spinning", "lockname:\"%s\"",
6202cba8dd3SJohn Baldwin 			    m->lock_object.lo_name);
6212604eb9eSMateusz Guzik 			do {
6221ada9041SMateusz Guzik 				lock_delay(&lda);
623391df78aSMateusz Guzik 				v = MTX_READ_VALUE(m);
6242604eb9eSMateusz Guzik 				owner = lv_mtx_owner(v);
6252604eb9eSMateusz Guzik 			} while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
6262cba8dd3SJohn Baldwin 			KTR_STATE0(KTR_SCHED, "thread",
6272cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
6282cba8dd3SJohn Baldwin 			    "running");
62949aead8aSAttilio Rao 			continue;
63049aead8aSAttilio Rao 		}
63149aead8aSAttilio Rao #endif
63249aead8aSAttilio Rao 
6332502c107SJeff Roberson 		ts = turnstile_trywait(&m->lock_object);
6342604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
635310f24d7SMateusz Guzik retry_turnstile:
6365fa8dd90SJohn Baldwin 
63736412d79SJohn Baldwin 		/*
6389ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
639961a7b24SJohn Baldwin 		 * the turnstile chain lock.
64036412d79SJohn Baldwin 		 */
6415fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
6422502c107SJeff Roberson 			turnstile_cancel(ts);
64336412d79SJohn Baldwin 			continue;
64436412d79SJohn Baldwin 		}
6459ed346baSBosko Milekic 
64649aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
64749aead8aSAttilio Rao 		/*
648fa29f023SJohn Baldwin 		 * The current lock owner might have started executing
649fa29f023SJohn Baldwin 		 * on another CPU (or the lock could have changed
650fa29f023SJohn Baldwin 		 * owners) while we were waiting on the turnstile
651fa29f023SJohn Baldwin 		 * chain lock.  If so, drop the turnstile lock and try
652fa29f023SJohn Baldwin 		 * again.
65349aead8aSAttilio Rao 		 */
6542604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
65549aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
65649aead8aSAttilio Rao 			turnstile_cancel(ts);
65749aead8aSAttilio Rao 			continue;
65849aead8aSAttilio Rao 		}
65949aead8aSAttilio Rao #endif
66049aead8aSAttilio Rao 
66136412d79SJohn Baldwin 		/*
6629ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
6639ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
6649ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
66536412d79SJohn Baldwin 		 */
66636412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
667310f24d7SMateusz Guzik 		    !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) {
668310f24d7SMateusz Guzik 			goto retry_turnstile;
66936412d79SJohn Baldwin 		}
67036412d79SJohn Baldwin 
6719ed346baSBosko Milekic 		/*
6727feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
6739ed346baSBosko Milekic 		 */
67436412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
67536412d79SJohn Baldwin 
6769ed346baSBosko Milekic 		/*
677961a7b24SJohn Baldwin 		 * Block on the turnstile.
6789ed346baSBosko Milekic 		 */
679a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
680e2b25737SMark Johnston 		sleep_time -= lockstat_nsecs(&m->lock_object);
681a5aedd68SStacey Son #endif
682284194f1SMateusz Guzik #ifndef ADAPTIVE_MUTEXES
683284194f1SMateusz Guzik 		owner = mtx_owner(m);
684284194f1SMateusz Guzik #endif
6858448e020SMateusz Guzik 		MPASS(owner == mtx_owner(m));
6868448e020SMateusz Guzik 		turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
687a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
688e2b25737SMark Johnston 		sleep_time += lockstat_nsecs(&m->lock_object);
689a5aedd68SStacey Son 		sleep_cnt++;
690a5aedd68SStacey Son #endif
6912604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
69236412d79SJohn Baldwin 	}
6937530de77SMateusz Guzik 	THREAD_CONTENTION_DONE(&m->lock_object);
694dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
695dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
6967640beb9SMateusz Guzik 		return;
697dfaa7859SMateusz Guzik #endif
698dfaa7859SMateusz Guzik #ifdef KDTRACE_HOOKS
6997640beb9SMateusz Guzik 	all_time += lockstat_nsecs(&m->lock_object);
700a5aedd68SStacey Son 	if (sleep_time)
70132cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
702a5aedd68SStacey Son 
703a5aedd68SStacey Son 	/*
704a5aedd68SStacey Son 	 * Only record the loops spinning and not sleeping.
705a5aedd68SStacey Son 	 */
7061ada9041SMateusz Guzik 	if (lda.spin_cnt > sleep_cnt)
70732cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
70809bdec20SMateusz Guzik out_lockstat:
709a5aedd68SStacey Son #endif
71009bdec20SMateusz Guzik 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
71109bdec20SMateusz Guzik 	    waittime, file, line);
7129ed346baSBosko Milekic }
7139ed346baSBosko Milekic 
714b95b98b0SKonstantin Belousov #ifdef SMP
7159ed346baSBosko Milekic /*
7167f44c618SAttilio Rao  * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
7179ed346baSBosko Milekic  *
7189ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
7199ed346baSBosko Milekic  * is handled inline.
7209ed346baSBosko Milekic  */
7210d74fe26SMateusz Guzik #if LOCK_DEBUG > 0
7229ed346baSBosko Milekic void
7230d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
7240d74fe26SMateusz Guzik     const char *file, int line)
7250d74fe26SMateusz Guzik #else
7260d74fe26SMateusz Guzik void
7270d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
7280d74fe26SMateusz Guzik #endif
72936412d79SJohn Baldwin {
7307f44c618SAttilio Rao 	struct mtx *m;
731a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
7320d74fe26SMateusz Guzik 	uintptr_t tid;
7331723a064SJeff Roberson #ifdef LOCK_PROFILING
7341723a064SJeff Roberson 	int contested = 0;
73570fe8436SKip Macy 	uint64_t waittime = 0;
7361723a064SJeff Roberson #endif
737076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
738076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
739076dd8ebSAndriy Gapon #endif
740dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
741f183fb16SMateusz Guzik 	int doing_lockprof = 0;
742dfaa7859SMateusz Guzik #endif
74336412d79SJohn Baldwin 
7440d74fe26SMateusz Guzik 	tid = (uintptr_t)curthread;
7457f44c618SAttilio Rao 	m = mtxlock2mtx(c);
7467f44c618SAttilio Rao 
74709bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS
74809bdec20SMateusz Guzik 	if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
74909bdec20SMateusz Guzik 		while (v == MTX_UNOWNED) {
75009bdec20SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
75109bdec20SMateusz Guzik 				goto out_lockstat;
75209bdec20SMateusz Guzik 		}
75309bdec20SMateusz Guzik 		doing_lockprof = 1;
75409bdec20SMateusz Guzik 		spin_time -= lockstat_nsecs(&m->lock_object);
75509bdec20SMateusz Guzik 	}
75609bdec20SMateusz Guzik #endif
75709bdec20SMateusz Guzik #ifdef LOCK_PROFILING
75809bdec20SMateusz Guzik 	doing_lockprof = 1;
75909bdec20SMateusz Guzik #endif
76009bdec20SMateusz Guzik 
76113d2ef0fSMateusz Guzik 	if (__predict_false(v == MTX_UNOWNED))
76213d2ef0fSMateusz Guzik 		v = MTX_READ_VALUE(m);
76313d2ef0fSMateusz Guzik 
76413d2ef0fSMateusz Guzik 	if (__predict_false(v == tid)) {
76513d2ef0fSMateusz Guzik 		m->mtx_recurse++;
76613d2ef0fSMateusz Guzik 		return;
76713d2ef0fSMateusz Guzik 	}
76813d2ef0fSMateusz Guzik 
7690d74fe26SMateusz Guzik 	if (SCHEDULER_STOPPED())
7700d74fe26SMateusz Guzik 		return;
7710d74fe26SMateusz Guzik 
772aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
7735746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
7742cba8dd3SJohn Baldwin 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
7752cba8dd3SJohn Baldwin 	    "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
7769ed346baSBosko Milekic 
777f90d57b8SMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
778f90d57b8SMateusz Guzik 
779f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
780f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
781f5f9340bSFabien Thomas #endif
7826a467cc5SMateusz Guzik 	lock_profile_obtain_lock_failed(&m->lock_object, true, &contested, &waittime);
78309bdec20SMateusz Guzik 
784fc4f686dSMateusz Guzik 	for (;;) {
7852604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
78690836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
787fc4f686dSMateusz Guzik 				break;
78836412d79SJohn Baldwin 			continue;
789703fc290SJohn Baldwin 		}
7902604eb9eSMateusz Guzik 		/* Give interrupts a chance while we spin. */
7912604eb9eSMateusz Guzik 		spinlock_exit();
7922604eb9eSMateusz Guzik 		do {
79315140a8aSMateusz Guzik 			if (__predict_true(lda.spin_cnt < 10000000)) {
7942604eb9eSMateusz Guzik 				lock_delay(&lda);
7952604eb9eSMateusz Guzik 			} else {
79615140a8aSMateusz Guzik 				_mtx_lock_indefinite_check(m, &lda);
79736412d79SJohn Baldwin 			}
7982604eb9eSMateusz Guzik 			v = MTX_READ_VALUE(m);
7992604eb9eSMateusz Guzik 		} while (v != MTX_UNOWNED);
800c6a37e84SJohn Baldwin 		spinlock_enter();
80136412d79SJohn Baldwin 	}
80236412d79SJohn Baldwin 
803aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
8049ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
8052cba8dd3SJohn Baldwin 	KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
8062cba8dd3SJohn Baldwin 	    "running");
8079ed346baSBosko Milekic 
808dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
809dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
810dfaa7859SMateusz Guzik 		return;
811dfaa7859SMateusz Guzik #endif
812c6d48c87SMark Johnston #ifdef KDTRACE_HOOKS
813dfaa7859SMateusz Guzik 	spin_time += lockstat_nsecs(&m->lock_object);
81409bdec20SMateusz Guzik 	if (lda.spin_cnt != 0)
81509bdec20SMateusz Guzik 		LOCKSTAT_RECORD1(spin__spin, m, spin_time);
81609bdec20SMateusz Guzik out_lockstat:
817dfaa7859SMateusz Guzik #endif
8186a467cc5SMateusz Guzik 	LOCKSTAT_PROFILE_OBTAIN_SPIN_LOCK_SUCCESS(spin__acquire, m,
81932cd0147SMark Johnston 	    contested, waittime, file, line);
82036412d79SJohn Baldwin }
82133fb8a38SJohn Baldwin #endif /* SMP */
82236412d79SJohn Baldwin 
823be49509eSMateusz Guzik #ifdef INVARIANTS
824be49509eSMateusz Guzik static void
825be49509eSMateusz Guzik thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
826be49509eSMateusz Guzik {
827be49509eSMateusz Guzik 
828be49509eSMateusz Guzik 	KASSERT(m->mtx_lock != MTX_DESTROYED,
829be49509eSMateusz Guzik 	    ("thread_lock() of destroyed mutex @ %s:%d", file, line));
830*dba45599SJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) != &lock_class_mtx_sleep,
831be49509eSMateusz Guzik 	    ("thread_lock() of sleep mutex %s @ %s:%d",
832be49509eSMateusz Guzik 	    m->lock_object.lo_name, file, line));
8333fd19ce7SMateusz Guzik 	KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) == 0,
8343fd19ce7SMateusz Guzik 	    ("thread_lock: got a recursive mutex %s @ %s:%d\n",
835be49509eSMateusz Guzik 	    m->lock_object.lo_name, file, line));
836be49509eSMateusz Guzik 	WITNESS_CHECKORDER(&m->lock_object,
837be49509eSMateusz Guzik 	    opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
838be49509eSMateusz Guzik }
839be49509eSMateusz Guzik #else
840be49509eSMateusz Guzik #define thread_lock_validate(m, opts, file, line) do { } while (0)
841be49509eSMateusz Guzik #endif
842be49509eSMateusz Guzik 
843be49509eSMateusz Guzik #ifndef LOCK_PROFILING
844be49509eSMateusz Guzik #if LOCK_DEBUG > 0
845be49509eSMateusz Guzik void
846be49509eSMateusz Guzik _thread_lock(struct thread *td, int opts, const char *file, int line)
847be49509eSMateusz Guzik #else
848be49509eSMateusz Guzik void
849be49509eSMateusz Guzik _thread_lock(struct thread *td)
850be49509eSMateusz Guzik #endif
851be49509eSMateusz Guzik {
852be49509eSMateusz Guzik 	struct mtx *m;
8533fd19ce7SMateusz Guzik 	uintptr_t tid;
854be49509eSMateusz Guzik 
855be49509eSMateusz Guzik 	tid = (uintptr_t)curthread;
856be49509eSMateusz Guzik 
8572c50bafeSMateusz Guzik 	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
8582c50bafeSMateusz Guzik 		goto slowpath_noirq;
859be49509eSMateusz Guzik 	spinlock_enter();
860be49509eSMateusz Guzik 	m = td->td_lock;
861be49509eSMateusz Guzik 	thread_lock_validate(m, 0, file, line);
8623fd19ce7SMateusz Guzik 	if (__predict_false(m == &blocked_lock))
863be49509eSMateusz Guzik 		goto slowpath_unlocked;
8643fd19ce7SMateusz Guzik 	if (__predict_false(!_mtx_obtain_lock(m, tid)))
865be49509eSMateusz Guzik 		goto slowpath_unlocked;
866be49509eSMateusz Guzik 	if (__predict_true(m == td->td_lock)) {
867be49509eSMateusz Guzik 		WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
868be49509eSMateusz Guzik 		return;
869be49509eSMateusz Guzik 	}
870be49509eSMateusz Guzik 	_mtx_release_lock_quick(m);
871be49509eSMateusz Guzik slowpath_unlocked:
872be49509eSMateusz Guzik 	spinlock_exit();
8732c50bafeSMateusz Guzik slowpath_noirq:
8742c50bafeSMateusz Guzik #if LOCK_DEBUG > 0
8752c50bafeSMateusz Guzik 	thread_lock_flags_(td, opts, file, line);
8762c50bafeSMateusz Guzik #else
877be49509eSMateusz Guzik 	thread_lock_flags_(td, 0, 0, 0);
8782c50bafeSMateusz Guzik #endif
879be49509eSMateusz Guzik }
880be49509eSMateusz Guzik #endif
881be49509eSMateusz Guzik 
8822502c107SJeff Roberson void
883ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
8842502c107SJeff Roberson {
8852502c107SJeff Roberson 	struct mtx *m;
8865e5ad162SMateusz Guzik 	uintptr_t tid, v;
887a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
8881723a064SJeff Roberson #ifdef LOCK_PROFILING
8891723a064SJeff Roberson 	int contested = 0;
8901723a064SJeff Roberson 	uint64_t waittime = 0;
8911723a064SJeff Roberson #endif
892a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
893076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
894a5aedd68SStacey Son #endif
895dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
896dfaa7859SMateusz Guzik 	int doing_lockprof = 1;
897dfaa7859SMateusz Guzik #endif
8982502c107SJeff Roberson 
8992502c107SJeff Roberson 	tid = (uintptr_t)curthread;
90035370593SAndriy Gapon 
901f61d6c5aSMark Johnston 	if (SCHEDULER_STOPPED()) {
902f61d6c5aSMark Johnston 		/*
903f61d6c5aSMark Johnston 		 * Ensure that spinlock sections are balanced even when the
904f61d6c5aSMark Johnston 		 * scheduler is stopped, since we may otherwise inadvertently
905f61d6c5aSMark Johnston 		 * re-enable interrupts while dumping core.
906f61d6c5aSMark Johnston 		 */
907f61d6c5aSMark Johnston 		spinlock_enter();
90835370593SAndriy Gapon 		return;
909f61d6c5aSMark Johnston 	}
91035370593SAndriy Gapon 
911a0d45f0fSMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
912a0d45f0fSMateusz Guzik 
9131f4d28c7SMateusz Guzik #ifdef HWPMC_HOOKS
9141f4d28c7SMateusz Guzik 	PMC_SOFT_CALL( , , lock, failed);
9151f4d28c7SMateusz Guzik #endif
9161f4d28c7SMateusz Guzik 
917dfaa7859SMateusz Guzik #ifdef LOCK_PROFILING
918dfaa7859SMateusz Guzik 	doing_lockprof = 1;
919df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS)
920dfaa7859SMateusz Guzik 	doing_lockprof = lockstat_enabled;
92142862413SEric van Gyzen #endif
92242862413SEric van Gyzen #ifdef KDTRACE_HOOKS
923dfaa7859SMateusz Guzik 	if (__predict_false(doing_lockprof))
924e2b25737SMark Johnston 		spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
925076dd8ebSAndriy Gapon #endif
9269f4e008dSMateusz Guzik 	spinlock_enter();
9279f4e008dSMateusz Guzik 
9282502c107SJeff Roberson 	for (;;) {
9292502c107SJeff Roberson retry:
930710eacdcSJeff Roberson 		m = td->td_lock;
931be49509eSMateusz Guzik 		thread_lock_validate(m, opts, file, line);
9321f4d28c7SMateusz Guzik 		v = MTX_READ_VALUE(m);
933fc4f686dSMateusz Guzik 		for (;;) {
9341f4d28c7SMateusz Guzik 			if (v == MTX_UNOWNED) {
93590836c32SMateusz Guzik 				if (_mtx_obtain_lock_fetch(m, &v, tid))
936fc4f686dSMateusz Guzik 					break;
9375e5ad162SMateusz Guzik 				continue;
9381f4d28c7SMateusz Guzik 			}
9393fd19ce7SMateusz Guzik 			MPASS(v != tid);
9406a467cc5SMateusz Guzik 			lock_profile_obtain_lock_failed(&m->lock_object, true,
941eea4f254SJeff Roberson 			    &contested, &waittime);
9422502c107SJeff Roberson 			/* Give interrupts a chance while we spin. */
9432502c107SJeff Roberson 			spinlock_exit();
9445e5ad162SMateusz Guzik 			do {
94515140a8aSMateusz Guzik 				if (__predict_true(lda.spin_cnt < 10000000)) {
946a0d45f0fSMateusz Guzik 					lock_delay(&lda);
947a0d45f0fSMateusz Guzik 				} else {
94815140a8aSMateusz Guzik 					_mtx_lock_indefinite_check(m, &lda);
949a0d45f0fSMateusz Guzik 				}
9509f4e008dSMateusz Guzik 				if (m != td->td_lock) {
9519f4e008dSMateusz Guzik 					spinlock_enter();
9522502c107SJeff Roberson 					goto retry;
9539f4e008dSMateusz Guzik 				}
9545e5ad162SMateusz Guzik 				v = MTX_READ_VALUE(m);
9555e5ad162SMateusz Guzik 			} while (v != MTX_UNOWNED);
9562502c107SJeff Roberson 			spinlock_enter();
9572502c107SJeff Roberson 		}
9582502c107SJeff Roberson 		if (m == td->td_lock)
9592502c107SJeff Roberson 			break;
9609f4e008dSMateusz Guzik 		_mtx_release_lock_quick(m);
9612502c107SJeff Roberson 	}
962dfaa7859SMateusz Guzik 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
963dfaa7859SMateusz Guzik 	    line);
964dfaa7859SMateusz Guzik 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
965dfaa7859SMateusz Guzik 
966dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
967dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
968dfaa7859SMateusz Guzik 		return;
969dfaa7859SMateusz Guzik #endif
970076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
971e2b25737SMark Johnston 	spin_time += lockstat_nsecs(&m->lock_object);
972076dd8ebSAndriy Gapon #endif
9736a467cc5SMateusz Guzik 	LOCKSTAT_PROFILE_OBTAIN_SPIN_LOCK_SUCCESS(spin__acquire, m, contested,
9743fd19ce7SMateusz Guzik 	    waittime, file, line);
9755002e195SMark Johnston #ifdef KDTRACE_HOOKS
976d0f68f91SMark Johnston 	if (lda.spin_cnt != 0)
97732cd0147SMark Johnston 		LOCKSTAT_RECORD1(thread__spin, m, spin_time);
9785002e195SMark Johnston #endif
9792502c107SJeff Roberson }
9802502c107SJeff Roberson 
9812502c107SJeff Roberson struct mtx *
9822502c107SJeff Roberson thread_lock_block(struct thread *td)
9832502c107SJeff Roberson {
9842502c107SJeff Roberson 	struct mtx *lock;
9852502c107SJeff Roberson 
986710eacdcSJeff Roberson 	lock = td->td_lock;
98761a74c5cSJeff Roberson 	mtx_assert(lock, MA_OWNED);
9882502c107SJeff Roberson 	td->td_lock = &blocked_lock;
9892502c107SJeff Roberson 
9902502c107SJeff Roberson 	return (lock);
9912502c107SJeff Roberson }
9922502c107SJeff Roberson 
9932502c107SJeff Roberson void
9942502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new)
9952502c107SJeff Roberson {
99661a74c5cSJeff Roberson 
9972502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
99861a74c5cSJeff Roberson 	KASSERT(td->td_lock == &blocked_lock,
99961a74c5cSJeff Roberson 	    ("thread %p lock %p not blocked_lock %p",
100061a74c5cSJeff Roberson 	    td, td->td_lock, &blocked_lock));
100165d32cd8SMatt Jacob 	atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
10022502c107SJeff Roberson }
10032502c107SJeff Roberson 
10042502c107SJeff Roberson void
100561a74c5cSJeff Roberson thread_lock_block_wait(struct thread *td)
100661a74c5cSJeff Roberson {
100761a74c5cSJeff Roberson 
100861a74c5cSJeff Roberson 	while (td->td_lock == &blocked_lock)
100961a74c5cSJeff Roberson 		cpu_spinwait();
101061a74c5cSJeff Roberson 
101161a74c5cSJeff Roberson 	/* Acquire fence to be certain that all thread state is visible. */
101261a74c5cSJeff Roberson 	atomic_thread_fence_acq();
101361a74c5cSJeff Roberson }
101461a74c5cSJeff Roberson 
101561a74c5cSJeff Roberson void
10162502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new)
10172502c107SJeff Roberson {
10182502c107SJeff Roberson 	struct mtx *lock;
10192502c107SJeff Roberson 
10202502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
1021710eacdcSJeff Roberson 	lock = td->td_lock;
102261a74c5cSJeff Roberson 	mtx_assert(lock, MA_OWNED);
10232502c107SJeff Roberson 	td->td_lock = new;
10242502c107SJeff Roberson 	mtx_unlock_spin(lock);
10252502c107SJeff Roberson }
10262502c107SJeff Roberson 
10279ed346baSBosko Milekic /*
10287f44c618SAttilio Rao  * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
10299ed346baSBosko Milekic  *
10303b3cf014SMateusz Guzik  * We are only called here if the lock is recursed, contested (i.e. we
10313b3cf014SMateusz Guzik  * need to wake up a blocked thread) or lockstat probe is active.
10329ed346baSBosko Milekic  */
103309f1319aSMateusz Guzik #if LOCK_DEBUG > 0
103436412d79SJohn Baldwin void
1035b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
1036b584eb2eSMateusz Guzik     const char *file, int line)
103709f1319aSMateusz Guzik #else
103809f1319aSMateusz Guzik void
1039b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
104009f1319aSMateusz Guzik #endif
104136412d79SJohn Baldwin {
10427f44c618SAttilio Rao 	struct mtx *m;
1043961a7b24SJohn Baldwin 	struct turnstile *ts;
1044b584eb2eSMateusz Guzik 	uintptr_t tid;
10459ed346baSBosko Milekic 
104635370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
104735370593SAndriy Gapon 		return;
104835370593SAndriy Gapon 
10493b3cf014SMateusz Guzik 	tid = (uintptr_t)curthread;
10507f44c618SAttilio Rao 	m = mtxlock2mtx(c);
1051b584eb2eSMateusz Guzik 
1052b584eb2eSMateusz Guzik 	if (__predict_false(v == tid))
10533b3cf014SMateusz Guzik 		v = MTX_READ_VALUE(m);
105490836c32SMateusz Guzik 
1055b584eb2eSMateusz Guzik 	if (__predict_false(v & MTX_RECURSED)) {
105636412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
105708812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1058aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
10599ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
106036412d79SJohn Baldwin 		return;
106136412d79SJohn Baldwin 	}
10629ed346baSBosko Milekic 
10633b3cf014SMateusz Guzik 	LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
10643b3cf014SMateusz Guzik 	if (v == tid && _mtx_release_lock(m, tid))
10653b3cf014SMateusz Guzik 		return;
10663b3cf014SMateusz Guzik 
10672502c107SJeff Roberson 	/*
10682502c107SJeff Roberson 	 * We have to lock the chain before the turnstile so this turnstile
10692502c107SJeff Roberson 	 * can be removed from the hash list if it is empty.
10702502c107SJeff Roberson 	 */
10712502c107SJeff Roberson 	turnstile_chain_lock(&m->lock_object);
10728448e020SMateusz Guzik 	_mtx_release_lock_quick(m);
1073aa89d8cdSJohn Baldwin 	ts = turnstile_lookup(&m->lock_object);
107487ee63baSMateusz Guzik 	if (__predict_false(ts == NULL)) {
107573da0265SJohn Baldwin 		panic("got NULL turnstile on mutex %p v %p", m, (void *)v);
107687ee63baSMateusz Guzik 	}
1077aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
10789ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
10797aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1080bf9c6c31SJohn Baldwin 
10812502c107SJeff Roberson 	/*
10822502c107SJeff Roberson 	 * This turnstile is now no longer associated with the mutex.  We can
10832502c107SJeff Roberson 	 * unlock the chain lock so a new turnstile may take it's place.
10842502c107SJeff Roberson 	 */
1085d0a22279SMateusz Guzik 	turnstile_unpend(ts);
10862502c107SJeff Roberson 	turnstile_chain_unlock(&m->lock_object);
10879ed346baSBosko Milekic }
10889ed346baSBosko Milekic 
10899ed346baSBosko Milekic /*
10909ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
1091961135eaSJohn Baldwin  * See the __mtx_unlock_spin() macro for the details.
10929ed346baSBosko Milekic  */
10939ed346baSBosko Milekic 
10949ed346baSBosko Milekic /*
109515ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
10969ed346baSBosko Milekic  */
10971103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
10980cde2e34SJason Evans void
10997f44c618SAttilio Rao __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
11000cde2e34SJason Evans {
11017f44c618SAttilio Rao 	const struct mtx *m;
11025cb0fbe4SJohn Baldwin 
1103879e0604SMateusz Guzik 	if (KERNEL_PANICKED() || dumping || SCHEDULER_STOPPED())
11045cb0fbe4SJohn Baldwin 		return;
11057f44c618SAttilio Rao 
11067f44c618SAttilio Rao 	m = mtxlock2mtx(c);
11077f44c618SAttilio Rao 
1108a10f4966SJake Burkholder 	switch (what) {
11090cde2e34SJason Evans 	case MA_OWNED:
11100cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
11110cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
1112a10f4966SJake Burkholder 		if (!mtx_owned(m))
11130cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
1114aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
1115a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
1116a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
11170cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
1118aa89d8cdSJohn Baldwin 				    m->lock_object.lo_name, file, line);
1119a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
11200cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
1121aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
11220cde2e34SJason Evans 		}
11230cde2e34SJason Evans 		break;
11240cde2e34SJason Evans 	case MA_NOTOWNED:
1125a10f4966SJake Burkholder 		if (mtx_owned(m))
11260cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
1127aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
11280cde2e34SJason Evans 		break;
11290cde2e34SJason Evans 	default:
113056771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
11310cde2e34SJason Evans 	}
11320cde2e34SJason Evans }
11330cde2e34SJason Evans #endif
11340cde2e34SJason Evans 
11359ed346baSBosko Milekic /*
1136c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
1137c27b5699SAndrew R. Reiter  */
1138c27b5699SAndrew R. Reiter void
1139c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
1140c27b5699SAndrew R. Reiter {
1141c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
1142c27b5699SAndrew R. Reiter 
11437f44c618SAttilio Rao 	mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
11447f44c618SAttilio Rao 	    margs->ma_opts);
1145c27b5699SAndrew R. Reiter }
1146c27b5699SAndrew R. Reiter 
1147c27b5699SAndrew R. Reiter /*
11489ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
11490c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
11500c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
11510c88508aSJohn Baldwin  * witness.
11529ed346baSBosko Milekic  */
115336412d79SJohn Baldwin void
11547f44c618SAttilio Rao _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
115536412d79SJohn Baldwin {
11567f44c618SAttilio Rao 	struct mtx *m;
115783a81bcbSJohn Baldwin 	struct lock_class *class;
115883a81bcbSJohn Baldwin 	int flags;
11599ed346baSBosko Milekic 
11607f44c618SAttilio Rao 	m = mtxlock2mtx(c);
11617f44c618SAttilio Rao 
116219284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1163fd07ddcfSDmitry Chagin 	    MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1164353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1165353998acSAttilio Rao 	    ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1166353998acSAttilio Rao 	    &m->mtx_lock));
11679ed346baSBosko Milekic 
116883a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
116919284646SJohn Baldwin 	if (opts & MTX_SPIN)
117083a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
117119284646SJohn Baldwin 	else
117283a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
117383a81bcbSJohn Baldwin 	flags = 0;
117419284646SJohn Baldwin 	if (opts & MTX_QUIET)
117583a81bcbSJohn Baldwin 		flags |= LO_QUIET;
117619284646SJohn Baldwin 	if (opts & MTX_RECURSE)
117783a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
117819284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
117983a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
1180f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
118183a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
11827c0435b9SKip Macy 	if (opts & MTX_NOPROFILE)
11837c0435b9SKip Macy 		flags |= LO_NOPROFILE;
1184fd07ddcfSDmitry Chagin 	if (opts & MTX_NEW)
1185fd07ddcfSDmitry Chagin 		flags |= LO_NEW;
118619284646SJohn Baldwin 
118783a81bcbSJohn Baldwin 	/* Initialize mutex. */
1188b5fb43e5SJohn Baldwin 	lock_init(&m->lock_object, class, name, type, flags);
1189b5fb43e5SJohn Baldwin 
119019284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
119183a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
119236412d79SJohn Baldwin }
119336412d79SJohn Baldwin 
11949ed346baSBosko Milekic /*
119519284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
119619284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
119719284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
119819284646SJohn Baldwin  * flags.
11999ed346baSBosko Milekic  */
120036412d79SJohn Baldwin void
12017f44c618SAttilio Rao _mtx_destroy(volatile uintptr_t *c)
120236412d79SJohn Baldwin {
12037f44c618SAttilio Rao 	struct mtx *m;
12047f44c618SAttilio Rao 
12057f44c618SAttilio Rao 	m = mtxlock2mtx(c);
120636412d79SJohn Baldwin 
120719284646SJohn Baldwin 	if (!mtx_owned(m))
120819284646SJohn Baldwin 		MPASS(mtx_unowned(m));
120919284646SJohn Baldwin 	else {
121008812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
12119ed346baSBosko Milekic 
1212861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
12136a467cc5SMateusz Guzik 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) {
12146a467cc5SMateusz Guzik 			lock_profile_release_lock(&m->lock_object, true);
1215861a2308SScott Long 			spinlock_exit();
12166a467cc5SMateusz Guzik 		} else {
1217ce1c953eSMark Johnston 			TD_LOCKS_DEC(curthread);
12186a467cc5SMateusz Guzik 			lock_profile_release_lock(&m->lock_object, false);
12196a467cc5SMateusz Guzik 		}
1220861a2308SScott Long 
122119284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
1222aa89d8cdSJohn Baldwin 		WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1223c86b6ff5SJohn Baldwin 		    __LINE__);
122436412d79SJohn Baldwin 	}
12250384fff8SJason Evans 
1226186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
1227aa89d8cdSJohn Baldwin 	lock_destroy(&m->lock_object);
12280384fff8SJason Evans }
1229d23f5958SMatthew Dillon 
1230d23f5958SMatthew Dillon /*
1231c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
1232c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
1233c53c013bSJohn Baldwin  * setup before this is called.
1234c53c013bSJohn Baldwin  */
1235c53c013bSJohn Baldwin void
1236c53c013bSJohn Baldwin mutex_init(void)
1237c53c013bSJohn Baldwin {
1238c53c013bSJohn Baldwin 
1239961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
1240961a7b24SJohn Baldwin 	init_turnstiles();
1241961a7b24SJohn Baldwin 
1242c53c013bSJohn Baldwin 	/*
1243c53c013bSJohn Baldwin 	 * Initialize mutexes.
1244c53c013bSJohn Baldwin 	 */
12450c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
12462502c107SJeff Roberson 	mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
12472502c107SJeff Roberson 	blocked_lock.mtx_lock = 0xdeadc0de;	/* Always blocked. */
12480c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
12496afb32fcSKonstantin Belousov 	mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
12505c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
12515c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
12525c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
12538c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1254c53c013bSJohn Baldwin 	mtx_lock(&Giant);
1255c53c013bSJohn Baldwin }
1256d272fe53SJohn Baldwin 
125715140a8aSMateusz Guzik static void __noinline
125815140a8aSMateusz Guzik _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap)
125915140a8aSMateusz Guzik {
126015140a8aSMateusz Guzik 	struct thread *td;
126115140a8aSMateusz Guzik 
126215140a8aSMateusz Guzik 	ldap->spin_cnt++;
1263879e0604SMateusz Guzik 	if (ldap->spin_cnt < 60000000 || kdb_active || KERNEL_PANICKED())
12644cbbb748SJohn Baldwin 		cpu_lock_delay();
126515140a8aSMateusz Guzik 	else {
126615140a8aSMateusz Guzik 		td = mtx_owner(m);
126715140a8aSMateusz Guzik 
126815140a8aSMateusz Guzik 		/* If the mutex is unlocked, try again. */
126915140a8aSMateusz Guzik 		if (td == NULL)
127015140a8aSMateusz Guzik 			return;
127115140a8aSMateusz Guzik 
127215140a8aSMateusz Guzik 		printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
127315140a8aSMateusz Guzik 		    m, m->lock_object.lo_name, td, td->td_tid);
127415140a8aSMateusz Guzik #ifdef WITNESS
127515140a8aSMateusz Guzik 		witness_display_spinlock(&m->lock_object, td, printf);
127615140a8aSMateusz Guzik #endif
127715140a8aSMateusz Guzik 		panic("spin lock held too long");
127815140a8aSMateusz Guzik 	}
127915140a8aSMateusz Guzik 	cpu_spinwait();
128015140a8aSMateusz Guzik }
128115140a8aSMateusz Guzik 
1282d2576988SMateusz Guzik void
1283d2576988SMateusz Guzik mtx_spin_wait_unlocked(struct mtx *m)
1284d2576988SMateusz Guzik {
1285d2576988SMateusz Guzik 	struct lock_delay_arg lda;
1286d2576988SMateusz Guzik 
1287500ca73dSMateusz Guzik 	KASSERT(m->mtx_lock != MTX_DESTROYED,
1288500ca73dSMateusz Guzik 	    ("%s() of destroyed mutex %p", __func__, m));
1289*dba45599SJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) != &lock_class_mtx_sleep,
1290500ca73dSMateusz Guzik 	    ("%s() of sleep mutex %p (%s)", __func__, m,
1291500ca73dSMateusz Guzik 	    m->lock_object.lo_name));
1292500ca73dSMateusz Guzik 	KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1293500ca73dSMateusz Guzik 	    m->lock_object.lo_name));
1294500ca73dSMateusz Guzik 
1295d2576988SMateusz Guzik 	lda.spin_cnt = 0;
1296d2576988SMateusz Guzik 
1297d2576988SMateusz Guzik 	while (atomic_load_acq_ptr(&m->mtx_lock) != MTX_UNOWNED) {
1298d2576988SMateusz Guzik 		if (__predict_true(lda.spin_cnt < 10000000)) {
1299d2576988SMateusz Guzik 			cpu_spinwait();
1300d2576988SMateusz Guzik 			lda.spin_cnt++;
1301d2576988SMateusz Guzik 		} else {
1302d2576988SMateusz Guzik 			_mtx_lock_indefinite_check(m, &lda);
1303d2576988SMateusz Guzik 		}
1304d2576988SMateusz Guzik 	}
1305d2576988SMateusz Guzik }
1306d2576988SMateusz Guzik 
1307bd66a075SMateusz Guzik void
1308bd66a075SMateusz Guzik mtx_wait_unlocked(struct mtx *m)
1309bd66a075SMateusz Guzik {
1310bd66a075SMateusz Guzik 	struct thread *owner;
1311bd66a075SMateusz Guzik 	uintptr_t v;
1312bd66a075SMateusz Guzik 
1313bd66a075SMateusz Guzik 	KASSERT(m->mtx_lock != MTX_DESTROYED,
1314bd66a075SMateusz Guzik 	    ("%s() of destroyed mutex %p", __func__, m));
1315*dba45599SJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) != &lock_class_mtx_spin,
1316*dba45599SJohn Baldwin 	    ("%s() of spin mutex %p (%s)", __func__, m,
1317bd66a075SMateusz Guzik 	    m->lock_object.lo_name));
1318bd66a075SMateusz Guzik 	KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1319bd66a075SMateusz Guzik 	    m->lock_object.lo_name));
1320bd66a075SMateusz Guzik 
1321bd66a075SMateusz Guzik 	for (;;) {
1322bd66a075SMateusz Guzik 		v = atomic_load_acq_ptr(&m->mtx_lock);
1323bd66a075SMateusz Guzik 		if (v == MTX_UNOWNED) {
1324bd66a075SMateusz Guzik 			break;
1325bd66a075SMateusz Guzik 		}
1326bd66a075SMateusz Guzik 		owner = lv_mtx_owner(v);
1327bd66a075SMateusz Guzik 		if (!TD_IS_RUNNING(owner)) {
1328bd66a075SMateusz Guzik 			mtx_lock(m);
1329bd66a075SMateusz Guzik 			mtx_unlock(m);
1330bd66a075SMateusz Guzik 			break;
1331bd66a075SMateusz Guzik 		}
1332bd66a075SMateusz Guzik 		cpu_spinwait();
1333bd66a075SMateusz Guzik 	}
1334bd66a075SMateusz Guzik }
1335bd66a075SMateusz Guzik 
1336d272fe53SJohn Baldwin #ifdef DDB
133795a9594aSGleb Smirnoff static void
1338d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock)
1339d272fe53SJohn Baldwin {
1340d272fe53SJohn Baldwin 	struct thread *td;
1341d576deedSPawel Jakub Dawidek 	const struct mtx *m;
1342d272fe53SJohn Baldwin 
1343d576deedSPawel Jakub Dawidek 	m = (const struct mtx *)lock;
1344d272fe53SJohn Baldwin 
1345d272fe53SJohn Baldwin 	db_printf(" flags: {");
134683a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1347d272fe53SJohn Baldwin 		db_printf("SPIN");
1348d272fe53SJohn Baldwin 	else
1349d272fe53SJohn Baldwin 		db_printf("DEF");
1350aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_RECURSABLE)
1351d272fe53SJohn Baldwin 		db_printf(", RECURSE");
1352aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_DUPOK)
1353d272fe53SJohn Baldwin 		db_printf(", DUPOK");
1354d272fe53SJohn Baldwin 	db_printf("}\n");
1355d272fe53SJohn Baldwin 	db_printf(" state: {");
1356d272fe53SJohn Baldwin 	if (mtx_unowned(m))
1357d272fe53SJohn Baldwin 		db_printf("UNOWNED");
1358c0bfd703SJohn Baldwin 	else if (mtx_destroyed(m))
1359c0bfd703SJohn Baldwin 		db_printf("DESTROYED");
1360d272fe53SJohn Baldwin 	else {
1361d272fe53SJohn Baldwin 		db_printf("OWNED");
1362d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
1363d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
1364d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
1365d272fe53SJohn Baldwin 			db_printf(", RECURSED");
1366d272fe53SJohn Baldwin 	}
1367d272fe53SJohn Baldwin 	db_printf("}\n");
1368c0bfd703SJohn Baldwin 	if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1369d272fe53SJohn Baldwin 		td = mtx_owner(m);
1370d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1371431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
1372d272fe53SJohn Baldwin 		if (mtx_recursed(m))
1373d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
1374d272fe53SJohn Baldwin 	}
1375d272fe53SJohn Baldwin }
1376d272fe53SJohn Baldwin #endif
1377