xref: /freebsd/sys/kern/kern_mutex.c (revision 08da2677755f0874ea99c5a42181aacc3eb17690)
10384fff8SJason Evans /*-
20384fff8SJason Evans  * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
30384fff8SJason Evans  *
40384fff8SJason Evans  * Redistribution and use in source and binary forms, with or without
50384fff8SJason Evans  * modification, are permitted provided that the following conditions
60384fff8SJason Evans  * are met:
70384fff8SJason Evans  * 1. Redistributions of source code must retain the above copyright
80384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer.
90384fff8SJason Evans  * 2. Redistributions in binary form must reproduce the above copyright
100384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer in the
110384fff8SJason Evans  *    documentation and/or other materials provided with the distribution.
120384fff8SJason Evans  * 3. Berkeley Software Design Inc's name may not be used to endorse or
130384fff8SJason Evans  *    promote products derived from this software without specific prior
140384fff8SJason Evans  *    written permission.
150384fff8SJason Evans  *
160384fff8SJason Evans  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
170384fff8SJason Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180384fff8SJason Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
190384fff8SJason Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
200384fff8SJason Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
210384fff8SJason Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
220384fff8SJason Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
230384fff8SJason Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
240384fff8SJason Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
250384fff8SJason Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
260384fff8SJason Evans  * SUCH DAMAGE.
270384fff8SJason Evans  *
280384fff8SJason Evans  *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
2936412d79SJohn Baldwin  *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
300384fff8SJason Evans  */
310384fff8SJason Evans 
320384fff8SJason Evans /*
33ba48b69aSJohn Baldwin  * Machine independent bits of mutex implementation.
340384fff8SJason Evans  */
350384fff8SJason Evans 
36677b542eSDavid E. O'Brien #include <sys/cdefs.h>
37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
38677b542eSDavid E. O'Brien 
392498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h"
409c36c934SJohn Baldwin #include "opt_ddb.h"
41f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h"
429923b511SScott Long #include "opt_sched.h"
43a5a96a19SJohn Baldwin 
440384fff8SJason Evans #include <sys/param.h>
456c35e809SDag-Erling Smørgrav #include <sys/systm.h>
4636412d79SJohn Baldwin #include <sys/bus.h>
471126349aSPaul Saab #include <sys/conf.h>
482d50560aSMarcel Moolenaar #include <sys/kdb.h>
4936412d79SJohn Baldwin #include <sys/kernel.h>
506c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
5119284646SJohn Baldwin #include <sys/lock.h>
52fb919e4dSMark Murray #include <sys/malloc.h>
5319284646SJohn Baldwin #include <sys/mutex.h>
540384fff8SJason Evans #include <sys/proc.h>
55c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
56b43179fbSJeff Roberson #include <sys/sched.h>
576c35e809SDag-Erling Smørgrav #include <sys/sbuf.h>
581ada9041SMateusz Guzik #include <sys/smp.h>
59a5a96a19SJohn Baldwin #include <sys/sysctl.h>
60961a7b24SJohn Baldwin #include <sys/turnstile.h>
6136412d79SJohn Baldwin #include <sys/vmmeter.h>
627c0435b9SKip Macy #include <sys/lock_profile.h>
630384fff8SJason Evans 
6436412d79SJohn Baldwin #include <machine/atomic.h>
6536412d79SJohn Baldwin #include <machine/bus.h>
660384fff8SJason Evans #include <machine/cpu.h>
6736412d79SJohn Baldwin 
689c36c934SJohn Baldwin #include <ddb/ddb.h>
699c36c934SJohn Baldwin 
708c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h>
718c4b6380SJohn Baldwin 
7236412d79SJohn Baldwin #include <vm/vm.h>
7336412d79SJohn Baldwin #include <vm/vm_extern.h>
7436412d79SJohn Baldwin 
75cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
76cd6e6e4eSJohn Baldwin #define	ADAPTIVE_MUTEXES
77cd6e6e4eSJohn Baldwin #endif
78cd6e6e4eSJohn Baldwin 
79f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
80f5f9340bSFabien Thomas #include <sys/pmckern.h>
81f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , lock, failed);
82f5f9340bSFabien Thomas #endif
83f5f9340bSFabien Thomas 
84b9a80acaSStephan Uphoff /*
857f44c618SAttilio Rao  * Return the mutex address when the lock cookie address is provided.
867f44c618SAttilio Rao  * This functionality assumes that struct mtx* have a member named mtx_lock.
877f44c618SAttilio Rao  */
887f44c618SAttilio Rao #define	mtxlock2mtx(c)	(__containerof(c, struct mtx, mtx_lock))
897f44c618SAttilio Rao 
907f44c618SAttilio Rao /*
919ed346baSBosko Milekic  * Internal utility macros.
920cde2e34SJason Evans  */
939ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
940cde2e34SJason Evans 
95c0bfd703SJohn Baldwin #define	mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
96c0bfd703SJohn Baldwin 
97d576deedSPawel Jakub Dawidek static void	assert_mtx(const struct lock_object *lock, int what);
98d272fe53SJohn Baldwin #ifdef DDB
99d576deedSPawel Jakub Dawidek static void	db_show_mtx(const struct lock_object *lock);
100d272fe53SJohn Baldwin #endif
1017faf4d90SDavide Italiano static void	lock_mtx(struct lock_object *lock, uintptr_t how);
1027faf4d90SDavide Italiano static void	lock_spin(struct lock_object *lock, uintptr_t how);
103a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
104d576deedSPawel Jakub Dawidek static int	owner_mtx(const struct lock_object *lock,
105d576deedSPawel Jakub Dawidek 		    struct thread **owner);
106a5aedd68SStacey Son #endif
1077faf4d90SDavide Italiano static uintptr_t unlock_mtx(struct lock_object *lock);
1087faf4d90SDavide Italiano static uintptr_t unlock_spin(struct lock_object *lock);
109d272fe53SJohn Baldwin 
1100cde2e34SJason Evans /*
11119284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
1120cde2e34SJason Evans  */
11319284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
114ae8dde30SJohn Baldwin 	.lc_name = "sleep mutex",
115ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
116f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
117d272fe53SJohn Baldwin #ifdef DDB
118ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
119d272fe53SJohn Baldwin #endif
1206e21afd4SJohn Baldwin 	.lc_lock = lock_mtx,
1216e21afd4SJohn Baldwin 	.lc_unlock = unlock_mtx,
122a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
123a5aedd68SStacey Son 	.lc_owner = owner_mtx,
124a5aedd68SStacey Son #endif
12519284646SJohn Baldwin };
12619284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
127ae8dde30SJohn Baldwin 	.lc_name = "spin mutex",
128ae8dde30SJohn Baldwin 	.lc_flags = LC_SPINLOCK | LC_RECURSABLE,
129f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
130d272fe53SJohn Baldwin #ifdef DDB
131ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
132d272fe53SJohn Baldwin #endif
1336e21afd4SJohn Baldwin 	.lc_lock = lock_spin,
1346e21afd4SJohn Baldwin 	.lc_unlock = unlock_spin,
135a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
136a5aedd68SStacey Son 	.lc_owner = owner_mtx,
137a5aedd68SStacey Son #endif
1388484de75SJohn Baldwin };
1398484de75SJohn Baldwin 
1401ada9041SMateusz Guzik #ifdef ADAPTIVE_MUTEXES
1411ada9041SMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging");
1421ada9041SMateusz Guzik 
14329051116SMateusz Guzik static struct lock_delay_config __read_mostly mtx_delay = {
1441ada9041SMateusz Guzik 	.initial	= 1000,
1451ada9041SMateusz Guzik 	.step		= 500,
1461ada9041SMateusz Guzik 	.min		= 100,
1471ada9041SMateusz Guzik 	.max		= 5000,
1481ada9041SMateusz Guzik };
1491ada9041SMateusz Guzik 
1501ada9041SMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_initial, CTLFLAG_RW, &mtx_delay.initial,
1511ada9041SMateusz Guzik     0, "");
1521ada9041SMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_step, CTLFLAG_RW, &mtx_delay.step,
1531ada9041SMateusz Guzik     0, "");
1541ada9041SMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_min, CTLFLAG_RW, &mtx_delay.min,
1551ada9041SMateusz Guzik     0, "");
1561ada9041SMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
1571ada9041SMateusz Guzik     0, "");
1581ada9041SMateusz Guzik 
1591ada9041SMateusz Guzik static void
1601ada9041SMateusz Guzik mtx_delay_sysinit(void *dummy)
1611ada9041SMateusz Guzik {
1621ada9041SMateusz Guzik 
1631ada9041SMateusz Guzik 	mtx_delay.initial = mp_ncpus * 25;
1641ada9041SMateusz Guzik 	mtx_delay.step = (mp_ncpus * 25) / 2;
1651ada9041SMateusz Guzik 	mtx_delay.min = mp_ncpus * 5;
1661ada9041SMateusz Guzik 	mtx_delay.max = mp_ncpus * 25 * 10;
1671ada9041SMateusz Guzik }
1681ada9041SMateusz Guzik LOCK_DELAY_SYSINIT(mtx_delay_sysinit);
1691ada9041SMateusz Guzik #endif
1701ada9041SMateusz Guzik 
171a0d45f0fSMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL,
172a0d45f0fSMateusz Guzik     "mtx spin debugging");
173a0d45f0fSMateusz Guzik 
17429051116SMateusz Guzik static struct lock_delay_config __read_mostly mtx_spin_delay = {
175a0d45f0fSMateusz Guzik 	.initial        = 1000,
176a0d45f0fSMateusz Guzik 	.step           = 500,
177a0d45f0fSMateusz Guzik 	.min            = 100,
178a0d45f0fSMateusz Guzik 	.max            = 5000,
179a0d45f0fSMateusz Guzik };
180a0d45f0fSMateusz Guzik 
181a0d45f0fSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_initial, CTLFLAG_RW,
182a0d45f0fSMateusz Guzik     &mtx_spin_delay.initial, 0, "");
183a0d45f0fSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_step, CTLFLAG_RW, &mtx_spin_delay.step,
184a0d45f0fSMateusz Guzik     0, "");
185a0d45f0fSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_min, CTLFLAG_RW, &mtx_spin_delay.min,
186a0d45f0fSMateusz Guzik     0, "");
187a0d45f0fSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_spin_delay.max,
188a0d45f0fSMateusz Guzik     0, "");
189a0d45f0fSMateusz Guzik 
190a0d45f0fSMateusz Guzik static void
191a0d45f0fSMateusz Guzik mtx_spin_delay_sysinit(void *dummy)
192a0d45f0fSMateusz Guzik {
193a0d45f0fSMateusz Guzik 
194a0d45f0fSMateusz Guzik 	mtx_spin_delay.initial = mp_ncpus * 25;
195a0d45f0fSMateusz Guzik 	mtx_spin_delay.step = (mp_ncpus * 25) / 2;
196a0d45f0fSMateusz Guzik 	mtx_spin_delay.min = mp_ncpus * 5;
197a0d45f0fSMateusz Guzik 	mtx_spin_delay.max = mp_ncpus * 25 * 10;
198a0d45f0fSMateusz Guzik }
199a0d45f0fSMateusz Guzik LOCK_DELAY_SYSINIT(mtx_spin_delay_sysinit);
200a0d45f0fSMateusz Guzik 
2019ed346baSBosko Milekic /*
202c53c013bSJohn Baldwin  * System-wide mutexes
203c53c013bSJohn Baldwin  */
2042502c107SJeff Roberson struct mtx blocked_lock;
205c53c013bSJohn Baldwin struct mtx Giant;
206c53c013bSJohn Baldwin 
20767784314SPoul-Henning Kamp void
208d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what)
209f9721b43SAttilio Rao {
210f9721b43SAttilio Rao 
211d576deedSPawel Jakub Dawidek 	mtx_assert((const struct mtx *)lock, what);
212f9721b43SAttilio Rao }
213f9721b43SAttilio Rao 
21467784314SPoul-Henning Kamp void
2157faf4d90SDavide Italiano lock_mtx(struct lock_object *lock, uintptr_t how)
2166e21afd4SJohn Baldwin {
2176e21afd4SJohn Baldwin 
2186e21afd4SJohn Baldwin 	mtx_lock((struct mtx *)lock);
2196e21afd4SJohn Baldwin }
2206e21afd4SJohn Baldwin 
22167784314SPoul-Henning Kamp void
2227faf4d90SDavide Italiano lock_spin(struct lock_object *lock, uintptr_t how)
2236e21afd4SJohn Baldwin {
2246e21afd4SJohn Baldwin 
2256e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
2266e21afd4SJohn Baldwin }
2276e21afd4SJohn Baldwin 
2287faf4d90SDavide Italiano uintptr_t
2296e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock)
2306e21afd4SJohn Baldwin {
2316e21afd4SJohn Baldwin 	struct mtx *m;
2326e21afd4SJohn Baldwin 
2336e21afd4SJohn Baldwin 	m = (struct mtx *)lock;
2346e21afd4SJohn Baldwin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
2356e21afd4SJohn Baldwin 	mtx_unlock(m);
2366e21afd4SJohn Baldwin 	return (0);
2376e21afd4SJohn Baldwin }
2386e21afd4SJohn Baldwin 
2397faf4d90SDavide Italiano uintptr_t
2406e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock)
2416e21afd4SJohn Baldwin {
2426e21afd4SJohn Baldwin 
2436e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
2446e21afd4SJohn Baldwin }
2456e21afd4SJohn Baldwin 
246a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
247a5aedd68SStacey Son int
248d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner)
249a5aedd68SStacey Son {
25002315a67SMark Johnston 	const struct mtx *m;
25102315a67SMark Johnston 	uintptr_t x;
252a5aedd68SStacey Son 
25302315a67SMark Johnston 	m = (const struct mtx *)lock;
25402315a67SMark Johnston 	x = m->mtx_lock;
25502315a67SMark Johnston 	*owner = (struct thread *)(x & ~MTX_FLAGMASK);
25602315a67SMark Johnston 	return (x != MTX_UNOWNED);
257a5aedd68SStacey Son }
258a5aedd68SStacey Son #endif
259a5aedd68SStacey Son 
2600cde2e34SJason Evans /*
2616283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2626283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2636283b7d0SJohn Baldwin  */
2646283b7d0SJohn Baldwin void
2657f44c618SAttilio Rao __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
2666283b7d0SJohn Baldwin {
2677f44c618SAttilio Rao 	struct mtx *m;
268*08da2677SMateusz Guzik 	uintptr_t tid, v;
2696283b7d0SJohn Baldwin 
27035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
27135370593SAndriy Gapon 		return;
2727f44c618SAttilio Rao 
2737f44c618SAttilio Rao 	m = mtxlock2mtx(c);
2747f44c618SAttilio Rao 
275cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
276e3ae0dfeSAttilio Rao 	    ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
277e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
278186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
279186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
280aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
281aa89d8cdSJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2820d975d63SJohn Baldwin 	    file, line));
283ac6b769bSAttilio Rao 	WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
284ac6b769bSAttilio Rao 	    LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
2857c0435b9SKip Macy 
286*08da2677SMateusz Guzik 	tid = (uintptr_t)curthread;
287*08da2677SMateusz Guzik 	v = MTX_UNOWNED;
288*08da2677SMateusz Guzik 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
289*08da2677SMateusz Guzik 		_mtx_lock_sleep(m, v, tid, opts, file, line);
290*08da2677SMateusz Guzik 	else
291*08da2677SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
292*08da2677SMateusz Guzik 		    m, 0, 0, file, line);
293aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
294dde96c99SJohn Baldwin 	    line);
295ac6b769bSAttilio Rao 	WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
296ac6b769bSAttilio Rao 	    file, line);
297ce1c953eSMark Johnston 	TD_LOCKS_INC(curthread);
2986283b7d0SJohn Baldwin }
2996283b7d0SJohn Baldwin 
3006283b7d0SJohn Baldwin void
3017f44c618SAttilio Rao __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
3026283b7d0SJohn Baldwin {
3037f44c618SAttilio Rao 	struct mtx *m;
30435370593SAndriy Gapon 
30535370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
30635370593SAndriy Gapon 		return;
3077f44c618SAttilio Rao 
3087f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3097f44c618SAttilio Rao 
310186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
311186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
312aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
313aa89d8cdSJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
3140d975d63SJohn Baldwin 	    file, line));
315aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
316aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
3170d975d63SJohn Baldwin 	    line);
31821377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
319c66d7606SKip Macy 
320*08da2677SMateusz Guzik 	__mtx_unlock_sleep(c, opts, file, line);
321ce1c953eSMark Johnston 	TD_LOCKS_DEC(curthread);
3226283b7d0SJohn Baldwin }
3236283b7d0SJohn Baldwin 
3246283b7d0SJohn Baldwin void
3257f44c618SAttilio Rao __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
3267f44c618SAttilio Rao     int line)
3276283b7d0SJohn Baldwin {
3287f44c618SAttilio Rao 	struct mtx *m;
3296283b7d0SJohn Baldwin 
33035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
33135370593SAndriy Gapon 		return;
3327f44c618SAttilio Rao 
3337f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3347f44c618SAttilio Rao 
335186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
336186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
337aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
3380d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
339aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
340ad69e26bSJohn Baldwin 	if (mtx_owned(m))
341ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
342ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
343ad69e26bSJohn Baldwin 	    ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
344ad69e26bSJohn Baldwin 		    m->lock_object.lo_name, file, line));
345ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
346aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
34741313430SJohn Baldwin 	    file, line, NULL);
348961135eaSJohn Baldwin 	__mtx_lock_spin(m, curthread, opts, file, line);
349aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
350dde96c99SJohn Baldwin 	    line);
351aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
3526283b7d0SJohn Baldwin }
3536283b7d0SJohn Baldwin 
35490b581f2SKonstantin Belousov int
35590b581f2SKonstantin Belousov __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
35690b581f2SKonstantin Belousov     int line)
35790b581f2SKonstantin Belousov {
35890b581f2SKonstantin Belousov 	struct mtx *m;
35990b581f2SKonstantin Belousov 
36090b581f2SKonstantin Belousov 	if (SCHEDULER_STOPPED())
36190b581f2SKonstantin Belousov 		return (1);
36290b581f2SKonstantin Belousov 
36390b581f2SKonstantin Belousov 	m = mtxlock2mtx(c);
36490b581f2SKonstantin Belousov 
36590b581f2SKonstantin Belousov 	KASSERT(m->mtx_lock != MTX_DESTROYED,
36690b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
36790b581f2SKonstantin Belousov 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
36890b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
36990b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
37090b581f2SKonstantin Belousov 	KASSERT((opts & MTX_RECURSE) == 0,
37190b581f2SKonstantin Belousov 	    ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
37290b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
37390b581f2SKonstantin Belousov 	if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
37490b581f2SKonstantin Belousov 		LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
37590b581f2SKonstantin Belousov 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
37690b581f2SKonstantin Belousov 		return (1);
37790b581f2SKonstantin Belousov 	}
37890b581f2SKonstantin Belousov 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
37990b581f2SKonstantin Belousov 	return (0);
38090b581f2SKonstantin Belousov }
38190b581f2SKonstantin Belousov 
3826283b7d0SJohn Baldwin void
3837f44c618SAttilio Rao __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
3847f44c618SAttilio Rao     int line)
3856283b7d0SJohn Baldwin {
3867f44c618SAttilio Rao 	struct mtx *m;
387c66d7606SKip Macy 
38835370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
38935370593SAndriy Gapon 		return;
3907f44c618SAttilio Rao 
3917f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3927f44c618SAttilio Rao 
393186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
394186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
395aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
3960d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
397aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
398aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
399aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
400dde96c99SJohn Baldwin 	    line);
4010d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
402c66d7606SKip Macy 
403961135eaSJohn Baldwin 	__mtx_unlock_spin(m);
4046283b7d0SJohn Baldwin }
4056283b7d0SJohn Baldwin 
4066283b7d0SJohn Baldwin /*
4079ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
408eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
409eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
4100cde2e34SJason Evans  */
4110cde2e34SJason Evans int
4127f44c618SAttilio Rao _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
4130cde2e34SJason Evans {
4147f44c618SAttilio Rao 	struct mtx *m;
4151723a064SJeff Roberson #ifdef LOCK_PROFILING
4167c0435b9SKip Macy 	uint64_t waittime = 0;
4171723a064SJeff Roberson 	int contested = 0;
4181723a064SJeff Roberson #endif
4191723a064SJeff Roberson 	int rval;
4200cde2e34SJason Evans 
42135370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
42235370593SAndriy Gapon 		return (1);
42335370593SAndriy Gapon 
4247f44c618SAttilio Rao 	m = mtxlock2mtx(c);
4257f44c618SAttilio Rao 
426cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
427e3ae0dfeSAttilio Rao 	    ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
428e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
429186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
430186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
431aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
432aa89d8cdSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
43383cece6fSJohn Baldwin 	    file, line));
4349ed346baSBosko Milekic 
435ac6b769bSAttilio Rao 	if (mtx_owned(m) && ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
436ac6b769bSAttilio Rao 	    (opts & MTX_RECURSE) != 0)) {
437eac09796SJohn Baldwin 		m->mtx_recurse++;
438eac09796SJohn Baldwin 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
439eac09796SJohn Baldwin 		rval = 1;
440eac09796SJohn Baldwin 	} else
441961135eaSJohn Baldwin 		rval = _mtx_obtain_lock(m, (uintptr_t)curthread);
442ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
4439ed346baSBosko Milekic 
444aa89d8cdSJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
445764e4d54SJohn Baldwin 	if (rval) {
446aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4472d96f0b1SJohn Baldwin 		    file, line);
448ce1c953eSMark Johnston 		TD_LOCKS_INC(curthread);
449fe68a916SKip Macy 		if (m->mtx_recurse == 0)
45032cd0147SMark Johnston 			LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
451a5aedd68SStacey Son 			    m, contested, waittime, file, line);
4527c0435b9SKip Macy 
453764e4d54SJohn Baldwin 	}
4549ed346baSBosko Milekic 
45519284646SJohn Baldwin 	return (rval);
4560cde2e34SJason Evans }
4570cde2e34SJason Evans 
4580cde2e34SJason Evans /*
4597f44c618SAttilio Rao  * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4609ed346baSBosko Milekic  *
4619ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4629ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4630cde2e34SJason Evans  */
4640cde2e34SJason Evans void
46590836c32SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, uintptr_t tid, int opts,
4667f44c618SAttilio Rao     const char *file, int line)
46736412d79SJohn Baldwin {
4687f44c618SAttilio Rao 	struct mtx *m;
4692502c107SJeff Roberson 	struct turnstile *ts;
470cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES
47176447e56SJohn Baldwin 	volatile struct thread *owner;
4722498cf8cSJohn Baldwin #endif
47302bd1bcdSIan Dowse #ifdef KTR
47402bd1bcdSIan Dowse 	int cont_logged = 0;
47502bd1bcdSIan Dowse #endif
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;
487a5aedd68SStacey Son #endif
48836412d79SJohn Baldwin 
48935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
49035370593SAndriy Gapon 		return;
49135370593SAndriy Gapon 
492fa5000a4SMateusz Guzik #if defined(ADAPTIVE_MUTEXES)
4931ada9041SMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_delay);
494fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS)
495fa5000a4SMateusz Guzik 	lock_delay_arg_init(&lda, NULL);
4961ada9041SMateusz Guzik #endif
4977f44c618SAttilio Rao 	m = mtxlock2mtx(c);
4987f44c618SAttilio Rao 
4992604eb9eSMateusz Guzik 	if (__predict_false(lv_mtx_owner(v) == (struct thread *)tid)) {
500ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
501ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
502eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
503aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, file, line));
504ac6b769bSAttilio Rao 		opts &= ~MTX_RECURSE;
50536412d79SJohn Baldwin 		m->mtx_recurse++;
50608812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
507aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
5085746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
50936412d79SJohn Baldwin 		return;
51036412d79SJohn Baldwin 	}
511ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
5129ed346baSBosko Milekic 
513f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
514f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
515f5f9340bSFabien Thomas #endif
51670fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object,
51770fe8436SKip Macy 		    &contested, &waittime);
518aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
51915ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
52015ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
521aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
522076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
523e2b25737SMark Johnston 	all_time -= lockstat_nsecs(&m->lock_object);
524076dd8ebSAndriy Gapon #endif
5251bd0eefbSJohn Baldwin 
526fc4f686dSMateusz Guzik 	for (;;) {
5272604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
52890836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
529fc4f686dSMateusz Guzik 				break;
5302604eb9eSMateusz Guzik 			continue;
5312604eb9eSMateusz Guzik 		}
532a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
5331ada9041SMateusz Guzik 		lda.spin_cnt++;
534a5aedd68SStacey Son #endif
53549aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
53649aead8aSAttilio Rao 		/*
53749aead8aSAttilio Rao 		 * If the owner is running on another CPU, spin until the
53849aead8aSAttilio Rao 		 * owner stops running or the state of the lock changes.
53949aead8aSAttilio Rao 		 */
5402604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
54149aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
54249aead8aSAttilio Rao 			if (LOCK_LOG_TEST(&m->lock_object, 0))
54349aead8aSAttilio Rao 				CTR3(KTR_LOCK,
54449aead8aSAttilio Rao 				    "%s: spinning on %p held by %p",
54549aead8aSAttilio Rao 				    __func__, m, owner);
5462cba8dd3SJohn Baldwin 			KTR_STATE1(KTR_SCHED, "thread",
5472cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
5482cba8dd3SJohn Baldwin 			    "spinning", "lockname:\"%s\"",
5492cba8dd3SJohn Baldwin 			    m->lock_object.lo_name);
5502604eb9eSMateusz Guzik 			do {
5511ada9041SMateusz Guzik 				lock_delay(&lda);
552391df78aSMateusz Guzik 				v = MTX_READ_VALUE(m);
5532604eb9eSMateusz Guzik 				owner = lv_mtx_owner(v);
5542604eb9eSMateusz Guzik 			} while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
5552cba8dd3SJohn Baldwin 			KTR_STATE0(KTR_SCHED, "thread",
5562cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
5572cba8dd3SJohn Baldwin 			    "running");
55849aead8aSAttilio Rao 			continue;
55949aead8aSAttilio Rao 		}
56049aead8aSAttilio Rao #endif
56149aead8aSAttilio Rao 
5622502c107SJeff Roberson 		ts = turnstile_trywait(&m->lock_object);
5632604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
5645fa8dd90SJohn Baldwin 
56536412d79SJohn Baldwin 		/*
5669ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
567961a7b24SJohn Baldwin 		 * the turnstile chain lock.
56836412d79SJohn Baldwin 		 */
5695fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
5702502c107SJeff Roberson 			turnstile_cancel(ts);
57136412d79SJohn Baldwin 			continue;
57236412d79SJohn Baldwin 		}
5739ed346baSBosko Milekic 
57449aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
57549aead8aSAttilio Rao 		/*
576fa29f023SJohn Baldwin 		 * The current lock owner might have started executing
577fa29f023SJohn Baldwin 		 * on another CPU (or the lock could have changed
578fa29f023SJohn Baldwin 		 * owners) while we were waiting on the turnstile
579fa29f023SJohn Baldwin 		 * chain lock.  If so, drop the turnstile lock and try
580fa29f023SJohn Baldwin 		 * again.
58149aead8aSAttilio Rao 		 */
5822604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
58349aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
58449aead8aSAttilio Rao 			turnstile_cancel(ts);
58549aead8aSAttilio Rao 			continue;
58649aead8aSAttilio Rao 		}
58749aead8aSAttilio Rao #endif
58849aead8aSAttilio Rao 
58936412d79SJohn Baldwin 		/*
5909ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
5919ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
5929ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
59336412d79SJohn Baldwin 		 */
59436412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
595122eceefSJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
5962502c107SJeff Roberson 			turnstile_cancel(ts);
5972604eb9eSMateusz Guzik 			v = MTX_READ_VALUE(m);
59836412d79SJohn Baldwin 			continue;
59936412d79SJohn Baldwin 		}
60036412d79SJohn Baldwin 
6019ed346baSBosko Milekic 		/*
6027feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
6039ed346baSBosko Milekic 		 */
60436412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
60536412d79SJohn Baldwin 
60602bd1bcdSIan Dowse #ifdef KTR
60702bd1bcdSIan Dowse 		if (!cont_logged) {
60802bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
60902bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
610aa89d8cdSJohn Baldwin 			    (void *)tid, file, line, m->lock_object.lo_name,
611aa89d8cdSJohn Baldwin 			    WITNESS_FILE(&m->lock_object),
612aa89d8cdSJohn Baldwin 			    WITNESS_LINE(&m->lock_object));
61302bd1bcdSIan Dowse 			cont_logged = 1;
61402bd1bcdSIan Dowse 		}
61502bd1bcdSIan Dowse #endif
61636412d79SJohn Baldwin 
6179ed346baSBosko Milekic 		/*
618961a7b24SJohn Baldwin 		 * Block on the turnstile.
6199ed346baSBosko Milekic 		 */
620a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
621e2b25737SMark Johnston 		sleep_time -= lockstat_nsecs(&m->lock_object);
622a5aedd68SStacey Son #endif
6232502c107SJeff Roberson 		turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE);
624a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
625e2b25737SMark Johnston 		sleep_time += lockstat_nsecs(&m->lock_object);
626a5aedd68SStacey Son 		sleep_cnt++;
627a5aedd68SStacey Son #endif
6282604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
62936412d79SJohn Baldwin 	}
630076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
631e2b25737SMark Johnston 	all_time += lockstat_nsecs(&m->lock_object);
632076dd8ebSAndriy Gapon #endif
63302bd1bcdSIan Dowse #ifdef KTR
63402bd1bcdSIan Dowse 	if (cont_logged) {
63502bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
63602bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
637aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)tid, file, line);
63802bd1bcdSIan Dowse 	}
63902bd1bcdSIan Dowse #endif
64032cd0147SMark Johnston 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
641eea4f254SJeff Roberson 	    waittime, file, line);
642a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
643a5aedd68SStacey Son 	if (sleep_time)
64432cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
645a5aedd68SStacey Son 
646a5aedd68SStacey Son 	/*
647a5aedd68SStacey Son 	 * Only record the loops spinning and not sleeping.
648a5aedd68SStacey Son 	 */
6491ada9041SMateusz Guzik 	if (lda.spin_cnt > sleep_cnt)
65032cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
651a5aedd68SStacey Son #endif
6529ed346baSBosko Milekic }
6539ed346baSBosko Milekic 
6542502c107SJeff Roberson static void
6552502c107SJeff Roberson _mtx_lock_spin_failed(struct mtx *m)
6562502c107SJeff Roberson {
6572502c107SJeff Roberson 	struct thread *td;
6582502c107SJeff Roberson 
6592502c107SJeff Roberson 	td = mtx_owner(m);
6602502c107SJeff Roberson 
6612502c107SJeff Roberson 	/* If the mutex is unlocked, try again. */
6622502c107SJeff Roberson 	if (td == NULL)
6632502c107SJeff Roberson 		return;
664b95b98b0SKonstantin Belousov 
6652502c107SJeff Roberson 	printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
6662502c107SJeff Roberson 	    m, m->lock_object.lo_name, td, td->td_tid);
6672502c107SJeff Roberson #ifdef WITNESS
66898332c8cSAttilio Rao 	witness_display_spinlock(&m->lock_object, td, printf);
6692502c107SJeff Roberson #endif
6702502c107SJeff Roberson 	panic("spin lock held too long");
6712502c107SJeff Roberson }
6722502c107SJeff Roberson 
673b95b98b0SKonstantin Belousov #ifdef SMP
6749ed346baSBosko Milekic /*
6757f44c618SAttilio Rao  * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
6769ed346baSBosko Milekic  *
6779ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
6789ed346baSBosko Milekic  * is handled inline.
6799ed346baSBosko Milekic  */
6809ed346baSBosko Milekic void
68190836c32SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, uintptr_t tid,
68290836c32SMateusz Guzik     int opts, const char *file, int line)
68336412d79SJohn Baldwin {
6847f44c618SAttilio Rao 	struct mtx *m;
685a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
6861723a064SJeff Roberson #ifdef LOCK_PROFILING
6871723a064SJeff Roberson 	int contested = 0;
68870fe8436SKip Macy 	uint64_t waittime = 0;
6891723a064SJeff Roberson #endif
690076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
691076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
692076dd8ebSAndriy Gapon #endif
69336412d79SJohn Baldwin 
69435370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
69535370593SAndriy Gapon 		return;
69635370593SAndriy Gapon 
697a0d45f0fSMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
6987f44c618SAttilio Rao 	m = mtxlock2mtx(c);
6997f44c618SAttilio Rao 
700aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
7015746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
7022cba8dd3SJohn Baldwin 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
7032cba8dd3SJohn Baldwin 	    "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
7049ed346baSBosko Milekic 
705f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
706f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
707f5f9340bSFabien Thomas #endif
70870fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
709076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
710e2b25737SMark Johnston 	spin_time -= lockstat_nsecs(&m->lock_object);
711076dd8ebSAndriy Gapon #endif
712fc4f686dSMateusz Guzik 	for (;;) {
7132604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
71490836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
715fc4f686dSMateusz Guzik 				break;
71636412d79SJohn Baldwin 			continue;
717703fc290SJohn Baldwin 		}
7182604eb9eSMateusz Guzik 		/* Give interrupts a chance while we spin. */
7192604eb9eSMateusz Guzik 		spinlock_exit();
7202604eb9eSMateusz Guzik 		do {
7212604eb9eSMateusz Guzik 			if (lda.spin_cnt < 10000000) {
7222604eb9eSMateusz Guzik 				lock_delay(&lda);
7232604eb9eSMateusz Guzik 			} else {
724a0d45f0fSMateusz Guzik 				lda.spin_cnt++;
725a0d45f0fSMateusz Guzik 				if (lda.spin_cnt < 60000000 || kdb_active ||
726a0d45f0fSMateusz Guzik 				    panicstr != NULL)
72736412d79SJohn Baldwin 					DELAY(1);
7282502c107SJeff Roberson 				else
7292502c107SJeff Roberson 					_mtx_lock_spin_failed(m);
7309f1b87f1SMaxime Henrion 				cpu_spinwait();
73136412d79SJohn Baldwin 			}
7322604eb9eSMateusz Guzik 			v = MTX_READ_VALUE(m);
7332604eb9eSMateusz Guzik 		} while (v != MTX_UNOWNED);
734c6a37e84SJohn Baldwin 		spinlock_enter();
73536412d79SJohn Baldwin 	}
736076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
737e2b25737SMark Johnston 	spin_time += lockstat_nsecs(&m->lock_object);
738076dd8ebSAndriy Gapon #endif
73936412d79SJohn Baldwin 
740aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
7419ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
7422cba8dd3SJohn Baldwin 	KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
7432cba8dd3SJohn Baldwin 	    "running");
7449ed346baSBosko Milekic 
745c6d48c87SMark Johnston #ifdef KDTRACE_HOOKS
74632cd0147SMark Johnston 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
74732cd0147SMark Johnston 	    contested, waittime, file, line);
748e2b25737SMark Johnston 	if (spin_time != 0)
74932cd0147SMark Johnston 		LOCKSTAT_RECORD1(spin__spin, m, spin_time);
750c6d48c87SMark Johnston #endif
75136412d79SJohn Baldwin }
75233fb8a38SJohn Baldwin #endif /* SMP */
75336412d79SJohn Baldwin 
7542502c107SJeff Roberson void
755ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
7562502c107SJeff Roberson {
7572502c107SJeff Roberson 	struct mtx *m;
7585e5ad162SMateusz Guzik 	uintptr_t tid, v;
759a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
7601723a064SJeff Roberson #ifdef LOCK_PROFILING
7611723a064SJeff Roberson 	int contested = 0;
7621723a064SJeff Roberson 	uint64_t waittime = 0;
7631723a064SJeff Roberson #endif
764a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
765076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
766a5aedd68SStacey Son #endif
7672502c107SJeff Roberson 
7682502c107SJeff Roberson 	tid = (uintptr_t)curthread;
76935370593SAndriy Gapon 
770f61d6c5aSMark Johnston 	if (SCHEDULER_STOPPED()) {
771f61d6c5aSMark Johnston 		/*
772f61d6c5aSMark Johnston 		 * Ensure that spinlock sections are balanced even when the
773f61d6c5aSMark Johnston 		 * scheduler is stopped, since we may otherwise inadvertently
774f61d6c5aSMark Johnston 		 * re-enable interrupts while dumping core.
775f61d6c5aSMark Johnston 		 */
776f61d6c5aSMark Johnston 		spinlock_enter();
77735370593SAndriy Gapon 		return;
778f61d6c5aSMark Johnston 	}
77935370593SAndriy Gapon 
780a0d45f0fSMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
781a0d45f0fSMateusz Guzik 
782076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
783e2b25737SMark Johnston 	spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
784076dd8ebSAndriy Gapon #endif
7852502c107SJeff Roberson 	for (;;) {
7862502c107SJeff Roberson retry:
7872502c107SJeff Roberson 		spinlock_enter();
788710eacdcSJeff Roberson 		m = td->td_lock;
78913c85a48SJohn Baldwin 		KASSERT(m->mtx_lock != MTX_DESTROYED,
79013c85a48SJohn Baldwin 		    ("thread_lock() of destroyed mutex @ %s:%d", file, line));
79113c85a48SJohn Baldwin 		KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
79213c85a48SJohn Baldwin 		    ("thread_lock() of sleep mutex %s @ %s:%d",
79313c85a48SJohn Baldwin 		    m->lock_object.lo_name, file, line));
794ad69e26bSJohn Baldwin 		if (mtx_owned(m))
795ad69e26bSJohn Baldwin 			KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
796ad69e26bSJohn Baldwin 	    ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
797ad69e26bSJohn Baldwin 			    m->lock_object.lo_name, file, line));
7982502c107SJeff Roberson 		WITNESS_CHECKORDER(&m->lock_object,
79941313430SJohn Baldwin 		    opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
800fc4f686dSMateusz Guzik 		for (;;) {
80190836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
802fc4f686dSMateusz Guzik 				break;
80390836c32SMateusz Guzik 			if (v == MTX_UNOWNED)
8045e5ad162SMateusz Guzik 				continue;
8055e5ad162SMateusz Guzik 			if (v == tid) {
8062502c107SJeff Roberson 				m->mtx_recurse++;
8072502c107SJeff Roberson 				break;
8082502c107SJeff Roberson 			}
809f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
810f5f9340bSFabien Thomas 			PMC_SOFT_CALL( , , lock, failed);
811f5f9340bSFabien Thomas #endif
812eea4f254SJeff Roberson 			lock_profile_obtain_lock_failed(&m->lock_object,
813eea4f254SJeff Roberson 			    &contested, &waittime);
8142502c107SJeff Roberson 			/* Give interrupts a chance while we spin. */
8152502c107SJeff Roberson 			spinlock_exit();
8165e5ad162SMateusz Guzik 			do {
817a0d45f0fSMateusz Guzik 				if (lda.spin_cnt < 10000000) {
818a0d45f0fSMateusz Guzik 					lock_delay(&lda);
819a0d45f0fSMateusz Guzik 				} else {
820a0d45f0fSMateusz Guzik 					lda.spin_cnt++;
821a0d45f0fSMateusz Guzik 					if (lda.spin_cnt < 60000000 ||
8222502c107SJeff Roberson 					    kdb_active || panicstr != NULL)
8232502c107SJeff Roberson 						DELAY(1);
8242502c107SJeff Roberson 					else
8252502c107SJeff Roberson 						_mtx_lock_spin_failed(m);
8262502c107SJeff Roberson 					cpu_spinwait();
827a0d45f0fSMateusz Guzik 				}
8282502c107SJeff Roberson 				if (m != td->td_lock)
8292502c107SJeff Roberson 					goto retry;
8305e5ad162SMateusz Guzik 				v = MTX_READ_VALUE(m);
8315e5ad162SMateusz Guzik 			} while (v != MTX_UNOWNED);
8322502c107SJeff Roberson 			spinlock_enter();
8332502c107SJeff Roberson 		}
8342502c107SJeff Roberson 		if (m == td->td_lock)
8352502c107SJeff Roberson 			break;
836961135eaSJohn Baldwin 		__mtx_unlock_spin(m);	/* does spinlock_exit() */
8372502c107SJeff Roberson 	}
838076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
839e2b25737SMark Johnston 	spin_time += lockstat_nsecs(&m->lock_object);
840076dd8ebSAndriy Gapon #endif
841eea4f254SJeff Roberson 	if (m->mtx_recurse == 0)
84232cd0147SMark Johnston 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
84332cd0147SMark Johnston 		    contested, waittime, file, line);
84413c85a48SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
84513c85a48SJohn Baldwin 	    line);
8462502c107SJeff Roberson 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
8475002e195SMark Johnston #ifdef KDTRACE_HOOKS
848156fbc14SMark Johnston 	if (spin_time != 0)
84932cd0147SMark Johnston 		LOCKSTAT_RECORD1(thread__spin, m, spin_time);
8505002e195SMark Johnston #endif
8512502c107SJeff Roberson }
8522502c107SJeff Roberson 
8532502c107SJeff Roberson struct mtx *
8542502c107SJeff Roberson thread_lock_block(struct thread *td)
8552502c107SJeff Roberson {
8562502c107SJeff Roberson 	struct mtx *lock;
8572502c107SJeff Roberson 
8582502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
859710eacdcSJeff Roberson 	lock = td->td_lock;
8602502c107SJeff Roberson 	td->td_lock = &blocked_lock;
8612502c107SJeff Roberson 	mtx_unlock_spin(lock);
8622502c107SJeff Roberson 
8632502c107SJeff Roberson 	return (lock);
8642502c107SJeff Roberson }
8652502c107SJeff Roberson 
8662502c107SJeff Roberson void
8672502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new)
8682502c107SJeff Roberson {
8692502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
8702502c107SJeff Roberson 	MPASS(td->td_lock == &blocked_lock);
87165d32cd8SMatt Jacob 	atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
8722502c107SJeff Roberson }
8732502c107SJeff Roberson 
8742502c107SJeff Roberson void
8752502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new)
8762502c107SJeff Roberson {
8772502c107SJeff Roberson 	struct mtx *lock;
8782502c107SJeff Roberson 
8792502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
8802502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
881710eacdcSJeff Roberson 	lock = td->td_lock;
8822502c107SJeff Roberson 	td->td_lock = new;
8832502c107SJeff Roberson 	mtx_unlock_spin(lock);
8842502c107SJeff Roberson }
8852502c107SJeff Roberson 
8869ed346baSBosko Milekic /*
8877f44c618SAttilio Rao  * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
8889ed346baSBosko Milekic  *
8899ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
8909ed346baSBosko Milekic  * need to wake up a blocked thread).
8919ed346baSBosko Milekic  */
89236412d79SJohn Baldwin void
8937f44c618SAttilio Rao __mtx_unlock_sleep(volatile uintptr_t *c, int opts, const char *file, int line)
89436412d79SJohn Baldwin {
8957f44c618SAttilio Rao 	struct mtx *m;
896961a7b24SJohn Baldwin 	struct turnstile *ts;
8979ed346baSBosko Milekic 
89835370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
89935370593SAndriy Gapon 		return;
90035370593SAndriy Gapon 
9017f44c618SAttilio Rao 	m = mtxlock2mtx(c);
90290836c32SMateusz Guzik 
903*08da2677SMateusz Guzik 	if (!mtx_recursed(m)) {
904*08da2677SMateusz Guzik 		LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
90590836c32SMateusz Guzik 		if (_mtx_release_lock(m, (uintptr_t)curthread))
90690836c32SMateusz Guzik 			return;
907*08da2677SMateusz Guzik 	} else {
90836412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
90908812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
910aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
9119ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
91236412d79SJohn Baldwin 		return;
91336412d79SJohn Baldwin 	}
9149ed346baSBosko Milekic 
9152502c107SJeff Roberson 	/*
9162502c107SJeff Roberson 	 * We have to lock the chain before the turnstile so this turnstile
9172502c107SJeff Roberson 	 * can be removed from the hash list if it is empty.
9182502c107SJeff Roberson 	 */
9192502c107SJeff Roberson 	turnstile_chain_lock(&m->lock_object);
920aa89d8cdSJohn Baldwin 	ts = turnstile_lookup(&m->lock_object);
921aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
9229ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
923961a7b24SJohn Baldwin 	MPASS(ts != NULL);
9247aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
925961135eaSJohn Baldwin 	_mtx_release_lock_quick(m);
926bf9c6c31SJohn Baldwin 
9272502c107SJeff Roberson 	/*
9282502c107SJeff Roberson 	 * This turnstile is now no longer associated with the mutex.  We can
9292502c107SJeff Roberson 	 * unlock the chain lock so a new turnstile may take it's place.
9302502c107SJeff Roberson 	 */
9317aa4f685SJohn Baldwin 	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
9322502c107SJeff Roberson 	turnstile_chain_unlock(&m->lock_object);
9339ed346baSBosko Milekic }
9349ed346baSBosko Milekic 
9359ed346baSBosko Milekic /*
9369ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
937961135eaSJohn Baldwin  * See the __mtx_unlock_spin() macro for the details.
9389ed346baSBosko Milekic  */
9399ed346baSBosko Milekic 
9409ed346baSBosko Milekic /*
94115ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
9429ed346baSBosko Milekic  */
9431103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
9440cde2e34SJason Evans void
9457f44c618SAttilio Rao __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
9460cde2e34SJason Evans {
9477f44c618SAttilio Rao 	const struct mtx *m;
9485cb0fbe4SJohn Baldwin 
949310ab671SEric van Gyzen 	if (panicstr != NULL || dumping || SCHEDULER_STOPPED())
9505cb0fbe4SJohn Baldwin 		return;
9517f44c618SAttilio Rao 
9527f44c618SAttilio Rao 	m = mtxlock2mtx(c);
9537f44c618SAttilio Rao 
954a10f4966SJake Burkholder 	switch (what) {
9550cde2e34SJason Evans 	case MA_OWNED:
9560cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
9570cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
958a10f4966SJake Burkholder 		if (!mtx_owned(m))
9590cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
960aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
961a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
962a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
9630cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
964aa89d8cdSJohn Baldwin 				    m->lock_object.lo_name, file, line);
965a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
9660cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
967aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
9680cde2e34SJason Evans 		}
9690cde2e34SJason Evans 		break;
9700cde2e34SJason Evans 	case MA_NOTOWNED:
971a10f4966SJake Burkholder 		if (mtx_owned(m))
9720cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
973aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
9740cde2e34SJason Evans 		break;
9750cde2e34SJason Evans 	default:
97656771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
9770cde2e34SJason Evans 	}
9780cde2e34SJason Evans }
9790cde2e34SJason Evans #endif
9800cde2e34SJason Evans 
9819ed346baSBosko Milekic /*
982c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
983c27b5699SAndrew R. Reiter  */
984c27b5699SAndrew R. Reiter void
985c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
986c27b5699SAndrew R. Reiter {
987c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
988c27b5699SAndrew R. Reiter 
9897f44c618SAttilio Rao 	mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
9907f44c618SAttilio Rao 	    margs->ma_opts);
991c27b5699SAndrew R. Reiter }
992c27b5699SAndrew R. Reiter 
993c27b5699SAndrew R. Reiter /*
9949ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
9950c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
9960c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
9970c88508aSJohn Baldwin  * witness.
9989ed346baSBosko Milekic  */
99936412d79SJohn Baldwin void
10007f44c618SAttilio Rao _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
100136412d79SJohn Baldwin {
10027f44c618SAttilio Rao 	struct mtx *m;
100383a81bcbSJohn Baldwin 	struct lock_class *class;
100483a81bcbSJohn Baldwin 	int flags;
10059ed346baSBosko Milekic 
10067f44c618SAttilio Rao 	m = mtxlock2mtx(c);
10077f44c618SAttilio Rao 
100819284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1009fd07ddcfSDmitry Chagin 	    MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1010353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1011353998acSAttilio Rao 	    ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1012353998acSAttilio Rao 	    &m->mtx_lock));
10139ed346baSBosko Milekic 
101483a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
101519284646SJohn Baldwin 	if (opts & MTX_SPIN)
101683a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
101719284646SJohn Baldwin 	else
101883a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
101983a81bcbSJohn Baldwin 	flags = 0;
102019284646SJohn Baldwin 	if (opts & MTX_QUIET)
102183a81bcbSJohn Baldwin 		flags |= LO_QUIET;
102219284646SJohn Baldwin 	if (opts & MTX_RECURSE)
102383a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
102419284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
102583a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
1026f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
102783a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
10287c0435b9SKip Macy 	if (opts & MTX_NOPROFILE)
10297c0435b9SKip Macy 		flags |= LO_NOPROFILE;
1030fd07ddcfSDmitry Chagin 	if (opts & MTX_NEW)
1031fd07ddcfSDmitry Chagin 		flags |= LO_NEW;
103219284646SJohn Baldwin 
103383a81bcbSJohn Baldwin 	/* Initialize mutex. */
1034b5fb43e5SJohn Baldwin 	lock_init(&m->lock_object, class, name, type, flags);
1035b5fb43e5SJohn Baldwin 
103619284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
103783a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
103836412d79SJohn Baldwin }
103936412d79SJohn Baldwin 
10409ed346baSBosko Milekic /*
104119284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
104219284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
104319284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
104419284646SJohn Baldwin  * flags.
10459ed346baSBosko Milekic  */
104636412d79SJohn Baldwin void
10477f44c618SAttilio Rao _mtx_destroy(volatile uintptr_t *c)
104836412d79SJohn Baldwin {
10497f44c618SAttilio Rao 	struct mtx *m;
10507f44c618SAttilio Rao 
10517f44c618SAttilio Rao 	m = mtxlock2mtx(c);
105236412d79SJohn Baldwin 
105319284646SJohn Baldwin 	if (!mtx_owned(m))
105419284646SJohn Baldwin 		MPASS(mtx_unowned(m));
105519284646SJohn Baldwin 	else {
105608812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
10579ed346baSBosko Milekic 
1058861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
1059aa89d8cdSJohn Baldwin 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1060861a2308SScott Long 			spinlock_exit();
1061764e4d54SJohn Baldwin 		else
1062ce1c953eSMark Johnston 			TD_LOCKS_DEC(curthread);
1063861a2308SScott Long 
1064d3df4af3SJeff Roberson 		lock_profile_release_lock(&m->lock_object);
106519284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
1066aa89d8cdSJohn Baldwin 		WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1067c86b6ff5SJohn Baldwin 		    __LINE__);
106836412d79SJohn Baldwin 	}
10690384fff8SJason Evans 
1070186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
1071aa89d8cdSJohn Baldwin 	lock_destroy(&m->lock_object);
10720384fff8SJason Evans }
1073d23f5958SMatthew Dillon 
1074d23f5958SMatthew Dillon /*
1075c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
1076c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
1077c53c013bSJohn Baldwin  * setup before this is called.
1078c53c013bSJohn Baldwin  */
1079c53c013bSJohn Baldwin void
1080c53c013bSJohn Baldwin mutex_init(void)
1081c53c013bSJohn Baldwin {
1082c53c013bSJohn Baldwin 
1083961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
1084961a7b24SJohn Baldwin 	init_turnstiles();
1085961a7b24SJohn Baldwin 
1086c53c013bSJohn Baldwin 	/*
1087c53c013bSJohn Baldwin 	 * Initialize mutexes.
1088c53c013bSJohn Baldwin 	 */
10890c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
10902502c107SJeff Roberson 	mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
10912502c107SJeff Roberson 	blocked_lock.mtx_lock = 0xdeadc0de;	/* Always blocked. */
10920c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
10936afb32fcSKonstantin Belousov 	mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
10945c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
10955c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
10965c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
10978c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1098c53c013bSJohn Baldwin 	mtx_lock(&Giant);
1099c53c013bSJohn Baldwin }
1100d272fe53SJohn Baldwin 
1101d272fe53SJohn Baldwin #ifdef DDB
1102d272fe53SJohn Baldwin void
1103d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock)
1104d272fe53SJohn Baldwin {
1105d272fe53SJohn Baldwin 	struct thread *td;
1106d576deedSPawel Jakub Dawidek 	const struct mtx *m;
1107d272fe53SJohn Baldwin 
1108d576deedSPawel Jakub Dawidek 	m = (const struct mtx *)lock;
1109d272fe53SJohn Baldwin 
1110d272fe53SJohn Baldwin 	db_printf(" flags: {");
111183a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1112d272fe53SJohn Baldwin 		db_printf("SPIN");
1113d272fe53SJohn Baldwin 	else
1114d272fe53SJohn Baldwin 		db_printf("DEF");
1115aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_RECURSABLE)
1116d272fe53SJohn Baldwin 		db_printf(", RECURSE");
1117aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_DUPOK)
1118d272fe53SJohn Baldwin 		db_printf(", DUPOK");
1119d272fe53SJohn Baldwin 	db_printf("}\n");
1120d272fe53SJohn Baldwin 	db_printf(" state: {");
1121d272fe53SJohn Baldwin 	if (mtx_unowned(m))
1122d272fe53SJohn Baldwin 		db_printf("UNOWNED");
1123c0bfd703SJohn Baldwin 	else if (mtx_destroyed(m))
1124c0bfd703SJohn Baldwin 		db_printf("DESTROYED");
1125d272fe53SJohn Baldwin 	else {
1126d272fe53SJohn Baldwin 		db_printf("OWNED");
1127d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
1128d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
1129d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
1130d272fe53SJohn Baldwin 			db_printf(", RECURSED");
1131d272fe53SJohn Baldwin 	}
1132d272fe53SJohn Baldwin 	db_printf("}\n");
1133c0bfd703SJohn Baldwin 	if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1134d272fe53SJohn Baldwin 		td = mtx_owner(m);
1135d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1136431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
1137d272fe53SJohn Baldwin 		if (mtx_recursed(m))
1138d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
1139d272fe53SJohn Baldwin 	}
1140d272fe53SJohn Baldwin }
1141d272fe53SJohn Baldwin #endif
1142