xref: /freebsd/sys/kern/kern_mutex.c (revision 8a36da99deb0e19363ec04e4d3facd869c1028f5)
10384fff8SJason Evans /*-
2*8a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a36da99SPedro F. Giffuni  *
40384fff8SJason Evans  * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
50384fff8SJason Evans  *
60384fff8SJason Evans  * Redistribution and use in source and binary forms, with or without
70384fff8SJason Evans  * modification, are permitted provided that the following conditions
80384fff8SJason Evans  * are met:
90384fff8SJason Evans  * 1. Redistributions of source code must retain the above copyright
100384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer.
110384fff8SJason Evans  * 2. Redistributions in binary form must reproduce the above copyright
120384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer in the
130384fff8SJason Evans  *    documentation and/or other materials provided with the distribution.
140384fff8SJason Evans  * 3. Berkeley Software Design Inc's name may not be used to endorse or
150384fff8SJason Evans  *    promote products derived from this software without specific prior
160384fff8SJason Evans  *    written permission.
170384fff8SJason Evans  *
180384fff8SJason Evans  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
190384fff8SJason Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
200384fff8SJason Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
210384fff8SJason Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
220384fff8SJason Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
230384fff8SJason Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
240384fff8SJason Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
250384fff8SJason Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
260384fff8SJason Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
270384fff8SJason Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
280384fff8SJason Evans  * SUCH DAMAGE.
290384fff8SJason Evans  *
300384fff8SJason Evans  *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
3136412d79SJohn Baldwin  *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
320384fff8SJason Evans  */
330384fff8SJason Evans 
340384fff8SJason Evans /*
35ba48b69aSJohn Baldwin  * Machine independent bits of mutex implementation.
360384fff8SJason Evans  */
370384fff8SJason Evans 
38677b542eSDavid E. O'Brien #include <sys/cdefs.h>
39677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
40677b542eSDavid E. O'Brien 
412498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h"
429c36c934SJohn Baldwin #include "opt_ddb.h"
43f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h"
449923b511SScott Long #include "opt_sched.h"
45a5a96a19SJohn Baldwin 
460384fff8SJason Evans #include <sys/param.h>
476c35e809SDag-Erling Smørgrav #include <sys/systm.h>
4836412d79SJohn Baldwin #include <sys/bus.h>
491126349aSPaul Saab #include <sys/conf.h>
502d50560aSMarcel Moolenaar #include <sys/kdb.h>
5136412d79SJohn Baldwin #include <sys/kernel.h>
526c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
5319284646SJohn Baldwin #include <sys/lock.h>
54fb919e4dSMark Murray #include <sys/malloc.h>
5519284646SJohn Baldwin #include <sys/mutex.h>
560384fff8SJason Evans #include <sys/proc.h>
57c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
58b43179fbSJeff Roberson #include <sys/sched.h>
596c35e809SDag-Erling Smørgrav #include <sys/sbuf.h>
601ada9041SMateusz Guzik #include <sys/smp.h>
61a5a96a19SJohn Baldwin #include <sys/sysctl.h>
62961a7b24SJohn Baldwin #include <sys/turnstile.h>
6336412d79SJohn Baldwin #include <sys/vmmeter.h>
647c0435b9SKip Macy #include <sys/lock_profile.h>
650384fff8SJason Evans 
6636412d79SJohn Baldwin #include <machine/atomic.h>
6736412d79SJohn Baldwin #include <machine/bus.h>
680384fff8SJason Evans #include <machine/cpu.h>
6936412d79SJohn Baldwin 
709c36c934SJohn Baldwin #include <ddb/ddb.h>
719c36c934SJohn Baldwin 
728c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h>
738c4b6380SJohn Baldwin 
7436412d79SJohn Baldwin #include <vm/vm.h>
7536412d79SJohn Baldwin #include <vm/vm_extern.h>
7636412d79SJohn Baldwin 
77cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
78cd6e6e4eSJohn Baldwin #define	ADAPTIVE_MUTEXES
79cd6e6e4eSJohn Baldwin #endif
80cd6e6e4eSJohn Baldwin 
81f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
82f5f9340bSFabien Thomas #include <sys/pmckern.h>
83f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , lock, failed);
84f5f9340bSFabien Thomas #endif
85f5f9340bSFabien Thomas 
86b9a80acaSStephan Uphoff /*
877f44c618SAttilio Rao  * Return the mutex address when the lock cookie address is provided.
887f44c618SAttilio Rao  * This functionality assumes that struct mtx* have a member named mtx_lock.
897f44c618SAttilio Rao  */
907f44c618SAttilio Rao #define	mtxlock2mtx(c)	(__containerof(c, struct mtx, mtx_lock))
917f44c618SAttilio Rao 
927f44c618SAttilio Rao /*
939ed346baSBosko Milekic  * Internal utility macros.
940cde2e34SJason Evans  */
959ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
960cde2e34SJason Evans 
97c0bfd703SJohn Baldwin #define	mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
98c0bfd703SJohn Baldwin 
99d576deedSPawel Jakub Dawidek static void	assert_mtx(const struct lock_object *lock, int what);
100d272fe53SJohn Baldwin #ifdef DDB
101d576deedSPawel Jakub Dawidek static void	db_show_mtx(const struct lock_object *lock);
102d272fe53SJohn Baldwin #endif
1037faf4d90SDavide Italiano static void	lock_mtx(struct lock_object *lock, uintptr_t how);
1047faf4d90SDavide Italiano static void	lock_spin(struct lock_object *lock, uintptr_t how);
105a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
106d576deedSPawel Jakub Dawidek static int	owner_mtx(const struct lock_object *lock,
107d576deedSPawel Jakub Dawidek 		    struct thread **owner);
108a5aedd68SStacey Son #endif
1097faf4d90SDavide Italiano static uintptr_t unlock_mtx(struct lock_object *lock);
1107faf4d90SDavide Italiano static uintptr_t unlock_spin(struct lock_object *lock);
111d272fe53SJohn Baldwin 
1120cde2e34SJason Evans /*
11319284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
1140cde2e34SJason Evans  */
11519284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
116ae8dde30SJohn Baldwin 	.lc_name = "sleep mutex",
117ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
118f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
119d272fe53SJohn Baldwin #ifdef DDB
120ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
121d272fe53SJohn Baldwin #endif
1226e21afd4SJohn Baldwin 	.lc_lock = lock_mtx,
1236e21afd4SJohn Baldwin 	.lc_unlock = unlock_mtx,
124a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
125a5aedd68SStacey Son 	.lc_owner = owner_mtx,
126a5aedd68SStacey Son #endif
12719284646SJohn Baldwin };
12819284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
129ae8dde30SJohn Baldwin 	.lc_name = "spin mutex",
130ae8dde30SJohn Baldwin 	.lc_flags = LC_SPINLOCK | LC_RECURSABLE,
131f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
132d272fe53SJohn Baldwin #ifdef DDB
133ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
134d272fe53SJohn Baldwin #endif
1356e21afd4SJohn Baldwin 	.lc_lock = lock_spin,
1366e21afd4SJohn Baldwin 	.lc_unlock = unlock_spin,
137a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
138a5aedd68SStacey Son 	.lc_owner = owner_mtx,
139a5aedd68SStacey Son #endif
1408484de75SJohn Baldwin };
1418484de75SJohn Baldwin 
1421ada9041SMateusz Guzik #ifdef ADAPTIVE_MUTEXES
1431ada9041SMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging");
1441ada9041SMateusz Guzik 
145574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_delay;
1461ada9041SMateusz Guzik 
1478e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
1481ada9041SMateusz Guzik     0, "");
1491ada9041SMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
1501ada9041SMateusz Guzik     0, "");
1511ada9041SMateusz Guzik 
1528e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
1531ada9041SMateusz Guzik #endif
1541ada9041SMateusz Guzik 
155a0d45f0fSMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL,
156a0d45f0fSMateusz Guzik     "mtx spin debugging");
157a0d45f0fSMateusz Guzik 
158574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_spin_delay;
159a0d45f0fSMateusz Guzik 
1608e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW,
1618e5a3e9aSMateusz Guzik     &mtx_spin_delay.base, 0, "");
1628e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW,
1638e5a3e9aSMateusz Guzik     &mtx_spin_delay.max, 0, "");
164a0d45f0fSMateusz Guzik 
1658e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
166a0d45f0fSMateusz Guzik 
1679ed346baSBosko Milekic /*
168c53c013bSJohn Baldwin  * System-wide mutexes
169c53c013bSJohn Baldwin  */
1702502c107SJeff Roberson struct mtx blocked_lock;
1716a569d35SMateusz Guzik struct mtx __exclusive_cache_line Giant;
172c53c013bSJohn Baldwin 
17367784314SPoul-Henning Kamp void
174d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what)
175f9721b43SAttilio Rao {
176f9721b43SAttilio Rao 
177d576deedSPawel Jakub Dawidek 	mtx_assert((const struct mtx *)lock, what);
178f9721b43SAttilio Rao }
179f9721b43SAttilio Rao 
18067784314SPoul-Henning Kamp void
1817faf4d90SDavide Italiano lock_mtx(struct lock_object *lock, uintptr_t how)
1826e21afd4SJohn Baldwin {
1836e21afd4SJohn Baldwin 
1846e21afd4SJohn Baldwin 	mtx_lock((struct mtx *)lock);
1856e21afd4SJohn Baldwin }
1866e21afd4SJohn Baldwin 
18767784314SPoul-Henning Kamp void
1887faf4d90SDavide Italiano lock_spin(struct lock_object *lock, uintptr_t how)
1896e21afd4SJohn Baldwin {
1906e21afd4SJohn Baldwin 
1916e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
1926e21afd4SJohn Baldwin }
1936e21afd4SJohn Baldwin 
1947faf4d90SDavide Italiano uintptr_t
1956e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock)
1966e21afd4SJohn Baldwin {
1976e21afd4SJohn Baldwin 	struct mtx *m;
1986e21afd4SJohn Baldwin 
1996e21afd4SJohn Baldwin 	m = (struct mtx *)lock;
2006e21afd4SJohn Baldwin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
2016e21afd4SJohn Baldwin 	mtx_unlock(m);
2026e21afd4SJohn Baldwin 	return (0);
2036e21afd4SJohn Baldwin }
2046e21afd4SJohn Baldwin 
2057faf4d90SDavide Italiano uintptr_t
2066e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock)
2076e21afd4SJohn Baldwin {
2086e21afd4SJohn Baldwin 
2096e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
2106e21afd4SJohn Baldwin }
2116e21afd4SJohn Baldwin 
212a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
213a5aedd68SStacey Son int
214d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner)
215a5aedd68SStacey Son {
21602315a67SMark Johnston 	const struct mtx *m;
21702315a67SMark Johnston 	uintptr_t x;
218a5aedd68SStacey Son 
21902315a67SMark Johnston 	m = (const struct mtx *)lock;
22002315a67SMark Johnston 	x = m->mtx_lock;
22102315a67SMark Johnston 	*owner = (struct thread *)(x & ~MTX_FLAGMASK);
222e280ce46SMateusz Guzik 	return (*owner != NULL);
223a5aedd68SStacey Son }
224a5aedd68SStacey Son #endif
225a5aedd68SStacey Son 
2260cde2e34SJason Evans /*
2276283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2286283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2296283b7d0SJohn Baldwin  */
2306283b7d0SJohn Baldwin void
2317f44c618SAttilio Rao __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
2326283b7d0SJohn Baldwin {
2337f44c618SAttilio Rao 	struct mtx *m;
23408da2677SMateusz Guzik 	uintptr_t tid, v;
2356283b7d0SJohn Baldwin 
2367f44c618SAttilio Rao 	m = mtxlock2mtx(c);
2377f44c618SAttilio Rao 
238704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
239704cb42fSMark Johnston 	    !TD_IS_IDLETHREAD(curthread),
240e3ae0dfeSAttilio Rao 	    ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
241e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
242186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
243186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
244aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
245aa89d8cdSJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2460d975d63SJohn Baldwin 	    file, line));
247ac6b769bSAttilio Rao 	WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
248ac6b769bSAttilio Rao 	    LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
2497c0435b9SKip Macy 
25008da2677SMateusz Guzik 	tid = (uintptr_t)curthread;
25108da2677SMateusz Guzik 	v = MTX_UNOWNED;
25208da2677SMateusz Guzik 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
2532f1ddb89SMateusz Guzik 		_mtx_lock_sleep(m, v, opts, file, line);
25408da2677SMateusz Guzik 	else
25508da2677SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
25608da2677SMateusz Guzik 		    m, 0, 0, file, line);
257aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
258dde96c99SJohn Baldwin 	    line);
259ac6b769bSAttilio Rao 	WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
260ac6b769bSAttilio Rao 	    file, line);
261ce1c953eSMark Johnston 	TD_LOCKS_INC(curthread);
2626283b7d0SJohn Baldwin }
2636283b7d0SJohn Baldwin 
2646283b7d0SJohn Baldwin void
2657f44c618SAttilio Rao __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
2666283b7d0SJohn Baldwin {
2677f44c618SAttilio Rao 	struct mtx *m;
26835370593SAndriy Gapon 
2697f44c618SAttilio Rao 	m = mtxlock2mtx(c);
2707f44c618SAttilio Rao 
271186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
272186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
273aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
274aa89d8cdSJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2750d975d63SJohn Baldwin 	    file, line));
276aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
277aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
2780d975d63SJohn Baldwin 	    line);
27921377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
280c66d7606SKip Macy 
281ffd5c94cSMateusz Guzik #ifdef LOCK_PROFILING
282b584eb2eSMateusz Guzik 	__mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
283ffd5c94cSMateusz Guzik #else
284ffd5c94cSMateusz Guzik 	__mtx_unlock(m, curthread, opts, file, line);
285ffd5c94cSMateusz Guzik #endif
286ce1c953eSMark Johnston 	TD_LOCKS_DEC(curthread);
2876283b7d0SJohn Baldwin }
2886283b7d0SJohn Baldwin 
2896283b7d0SJohn Baldwin void
2907f44c618SAttilio Rao __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
2917f44c618SAttilio Rao     int line)
2926283b7d0SJohn Baldwin {
2937f44c618SAttilio Rao 	struct mtx *m;
29462bf13cbSMateusz Guzik #ifdef SMP
2950d74fe26SMateusz Guzik 	uintptr_t tid, v;
29662bf13cbSMateusz Guzik #endif
2976283b7d0SJohn Baldwin 
2987f44c618SAttilio Rao 	m = mtxlock2mtx(c);
2997f44c618SAttilio Rao 
300186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
301186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
302aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
3030d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
304aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
305ad69e26bSJohn Baldwin 	if (mtx_owned(m))
306ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
307ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
308ad69e26bSJohn Baldwin 	    ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
309ad69e26bSJohn Baldwin 		    m->lock_object.lo_name, file, line));
310ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
311aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
31241313430SJohn Baldwin 	    file, line, NULL);
31362bf13cbSMateusz Guzik #ifdef SMP
3140d74fe26SMateusz Guzik 	spinlock_enter();
3150d74fe26SMateusz Guzik 	tid = (uintptr_t)curthread;
3160d74fe26SMateusz Guzik 	v = MTX_UNOWNED;
3170d74fe26SMateusz Guzik 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
3180d74fe26SMateusz Guzik 		_mtx_lock_spin(m, v, opts, file, line);
3190d74fe26SMateusz Guzik 	else
3200d74fe26SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire,
3210d74fe26SMateusz Guzik 		    m, 0, 0, file, line);
32262bf13cbSMateusz Guzik #else
32362bf13cbSMateusz Guzik 	__mtx_lock_spin(m, curthread, opts, file, line);
32462bf13cbSMateusz Guzik #endif
325aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
326dde96c99SJohn Baldwin 	    line);
327aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
3286283b7d0SJohn Baldwin }
3296283b7d0SJohn Baldwin 
33090b581f2SKonstantin Belousov int
33190b581f2SKonstantin Belousov __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
33290b581f2SKonstantin Belousov     int line)
33390b581f2SKonstantin Belousov {
33490b581f2SKonstantin Belousov 	struct mtx *m;
33590b581f2SKonstantin Belousov 
33690b581f2SKonstantin Belousov 	if (SCHEDULER_STOPPED())
33790b581f2SKonstantin Belousov 		return (1);
33890b581f2SKonstantin Belousov 
33990b581f2SKonstantin Belousov 	m = mtxlock2mtx(c);
34090b581f2SKonstantin Belousov 
34190b581f2SKonstantin Belousov 	KASSERT(m->mtx_lock != MTX_DESTROYED,
34290b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
34390b581f2SKonstantin Belousov 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
34490b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
34590b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
34690b581f2SKonstantin Belousov 	KASSERT((opts & MTX_RECURSE) == 0,
34790b581f2SKonstantin Belousov 	    ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
34890b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
34990b581f2SKonstantin Belousov 	if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
35090b581f2SKonstantin Belousov 		LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
35190b581f2SKonstantin Belousov 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
35290b581f2SKonstantin Belousov 		return (1);
35390b581f2SKonstantin Belousov 	}
35490b581f2SKonstantin Belousov 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
35590b581f2SKonstantin Belousov 	return (0);
35690b581f2SKonstantin Belousov }
35790b581f2SKonstantin Belousov 
3586283b7d0SJohn Baldwin void
3597f44c618SAttilio Rao __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
3607f44c618SAttilio Rao     int line)
3616283b7d0SJohn Baldwin {
3627f44c618SAttilio Rao 	struct mtx *m;
363c66d7606SKip Macy 
3647f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3657f44c618SAttilio Rao 
366186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
367186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
368aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
3690d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
370aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
371aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
372aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
373dde96c99SJohn Baldwin 	    line);
3740d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
375c66d7606SKip Macy 
376961135eaSJohn Baldwin 	__mtx_unlock_spin(m);
3776283b7d0SJohn Baldwin }
3786283b7d0SJohn Baldwin 
3796283b7d0SJohn Baldwin /*
3809ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
381eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
382eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
3830cde2e34SJason Evans  */
3840cde2e34SJason Evans int
385013c0b49SMateusz Guzik _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
3860cde2e34SJason Evans {
3875c5df0d9SMateusz Guzik 	struct thread *td;
3885c5df0d9SMateusz Guzik 	uintptr_t tid, v;
3891723a064SJeff Roberson #ifdef LOCK_PROFILING
3907c0435b9SKip Macy 	uint64_t waittime = 0;
3911723a064SJeff Roberson 	int contested = 0;
3921723a064SJeff Roberson #endif
3931723a064SJeff Roberson 	int rval;
3945c5df0d9SMateusz Guzik 	bool recursed;
3950cde2e34SJason Evans 
3965c5df0d9SMateusz Guzik 	td = curthread;
3975c5df0d9SMateusz Guzik 	tid = (uintptr_t)td;
3985c5df0d9SMateusz Guzik 	if (SCHEDULER_STOPPED_TD(td))
39935370593SAndriy Gapon 		return (1);
40035370593SAndriy Gapon 
401704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
402e3ae0dfeSAttilio Rao 	    ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
403e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
404186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
405186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
406aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
407aa89d8cdSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
40883cece6fSJohn Baldwin 	    file, line));
4099ed346baSBosko Milekic 
4105c5df0d9SMateusz Guzik 	rval = 1;
4115c5df0d9SMateusz Guzik 	recursed = false;
4125c5df0d9SMateusz Guzik 	v = MTX_UNOWNED;
413b247fd39SMateusz Guzik 	for (;;) {
414b247fd39SMateusz Guzik 		if (_mtx_obtain_lock_fetch(m, &v, tid))
415b247fd39SMateusz Guzik 			break;
416b247fd39SMateusz Guzik 		if (v == MTX_UNOWNED)
417b247fd39SMateusz Guzik 			continue;
4185c5df0d9SMateusz Guzik 		if (v == tid &&
4195c5df0d9SMateusz Guzik 		    ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
420ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0)) {
421eac09796SJohn Baldwin 			m->mtx_recurse++;
422eac09796SJohn Baldwin 			atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
4235c5df0d9SMateusz Guzik 			recursed = true;
424b247fd39SMateusz Guzik 			break;
4255c5df0d9SMateusz Guzik 		}
426b247fd39SMateusz Guzik 		rval = 0;
427b247fd39SMateusz Guzik 		break;
4285c5df0d9SMateusz Guzik 	}
4295c5df0d9SMateusz Guzik 
430ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
4319ed346baSBosko Milekic 
432aa89d8cdSJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
433764e4d54SJohn Baldwin 	if (rval) {
434aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4352d96f0b1SJohn Baldwin 		    file, line);
436ce1c953eSMark Johnston 		TD_LOCKS_INC(curthread);
4375c5df0d9SMateusz Guzik 		if (!recursed)
43832cd0147SMark Johnston 			LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
439a5aedd68SStacey Son 			    m, contested, waittime, file, line);
440764e4d54SJohn Baldwin 	}
4419ed346baSBosko Milekic 
44219284646SJohn Baldwin 	return (rval);
4430cde2e34SJason Evans }
4440cde2e34SJason Evans 
445013c0b49SMateusz Guzik int
446013c0b49SMateusz Guzik _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
447013c0b49SMateusz Guzik {
448013c0b49SMateusz Guzik 	struct mtx *m;
449013c0b49SMateusz Guzik 
450013c0b49SMateusz Guzik 	m = mtxlock2mtx(c);
451013c0b49SMateusz Guzik 	return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
452013c0b49SMateusz Guzik }
453013c0b49SMateusz Guzik 
4540cde2e34SJason Evans /*
4557f44c618SAttilio Rao  * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4569ed346baSBosko Milekic  *
4579ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4589ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4590cde2e34SJason Evans  */
46009f1319aSMateusz Guzik #if LOCK_DEBUG > 0
4610cde2e34SJason Evans void
4622f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
4632f1ddb89SMateusz Guzik     int line)
46409f1319aSMateusz Guzik #else
46509f1319aSMateusz Guzik void
4662f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
46709f1319aSMateusz Guzik #endif
46836412d79SJohn Baldwin {
4692f1ddb89SMateusz Guzik 	struct thread *td;
4707f44c618SAttilio Rao 	struct mtx *m;
4712502c107SJeff Roberson 	struct turnstile *ts;
4722f1ddb89SMateusz Guzik 	uintptr_t tid;
4732ccee9ccSMateusz Guzik 	struct thread *owner;
47402bd1bcdSIan Dowse #ifdef KTR
47502bd1bcdSIan Dowse 	int cont_logged = 0;
47602bd1bcdSIan Dowse #endif
4771723a064SJeff Roberson #ifdef LOCK_PROFILING
47870fe8436SKip Macy 	int contested = 0;
47970fe8436SKip Macy 	uint64_t waittime = 0;
4801723a064SJeff Roberson #endif
4811ada9041SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
4821ada9041SMateusz Guzik 	struct lock_delay_arg lda;
4831ada9041SMateusz Guzik #endif
484a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
48561852185SMateusz Guzik 	u_int sleep_cnt = 0;
486a5aedd68SStacey Son 	int64_t sleep_time = 0;
487076dd8ebSAndriy Gapon 	int64_t all_time = 0;
488dfaa7859SMateusz Guzik #endif
489dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
490dfaa7859SMateusz Guzik 	int doing_lockprof;
491a5aedd68SStacey Son #endif
4922f1ddb89SMateusz Guzik 	td = curthread;
4932f1ddb89SMateusz Guzik 	tid = (uintptr_t)td;
4942f1ddb89SMateusz Guzik 	if (SCHEDULER_STOPPED_TD(td))
49535370593SAndriy Gapon 		return;
49635370593SAndriy Gapon 
497fa5000a4SMateusz Guzik #if defined(ADAPTIVE_MUTEXES)
4981ada9041SMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_delay);
499fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS)
500fa5000a4SMateusz Guzik 	lock_delay_arg_init(&lda, NULL);
5011ada9041SMateusz Guzik #endif
5027f44c618SAttilio Rao 	m = mtxlock2mtx(c);
503c1aaf63cSMateusz Guzik 	if (__predict_false(v == MTX_UNOWNED))
504c1aaf63cSMateusz Guzik 		v = MTX_READ_VALUE(m);
5057f44c618SAttilio Rao 
5062f1ddb89SMateusz Guzik 	if (__predict_false(lv_mtx_owner(v) == td)) {
507ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
508ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
509eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
510aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, file, line));
511a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0
512ac6b769bSAttilio Rao 		opts &= ~MTX_RECURSE;
513a24c8eb8SMateusz Guzik #endif
51436412d79SJohn Baldwin 		m->mtx_recurse++;
51508812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
516aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
5175746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
51836412d79SJohn Baldwin 		return;
51936412d79SJohn Baldwin 	}
520a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0
521ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
522a24c8eb8SMateusz Guzik #endif
5239ed346baSBosko Milekic 
524f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
525f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
526f5f9340bSFabien Thomas #endif
52770fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object,
52870fe8436SKip Macy 		    &contested, &waittime);
529aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
53015ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
53115ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
532aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
53309f1319aSMateusz Guzik #ifdef LOCK_PROFILING
534dfaa7859SMateusz Guzik 	doing_lockprof = 1;
535df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS)
536dfaa7859SMateusz Guzik 	doing_lockprof = lockstat_enabled;
537dfaa7859SMateusz Guzik 	if (__predict_false(doing_lockprof))
538e2b25737SMark Johnston 		all_time -= lockstat_nsecs(&m->lock_object);
539076dd8ebSAndriy Gapon #endif
5401bd0eefbSJohn Baldwin 
541fc4f686dSMateusz Guzik 	for (;;) {
5422604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
54390836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
544fc4f686dSMateusz Guzik 				break;
5452604eb9eSMateusz Guzik 			continue;
5462604eb9eSMateusz Guzik 		}
547a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
5481ada9041SMateusz Guzik 		lda.spin_cnt++;
549a5aedd68SStacey Son #endif
55049aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
55149aead8aSAttilio Rao 		/*
55249aead8aSAttilio Rao 		 * If the owner is running on another CPU, spin until the
55349aead8aSAttilio Rao 		 * owner stops running or the state of the lock changes.
55449aead8aSAttilio Rao 		 */
5552604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
55649aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
55749aead8aSAttilio Rao 			if (LOCK_LOG_TEST(&m->lock_object, 0))
55849aead8aSAttilio Rao 				CTR3(KTR_LOCK,
55949aead8aSAttilio Rao 				    "%s: spinning on %p held by %p",
56049aead8aSAttilio Rao 				    __func__, m, owner);
5612cba8dd3SJohn Baldwin 			KTR_STATE1(KTR_SCHED, "thread",
5622cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
5632cba8dd3SJohn Baldwin 			    "spinning", "lockname:\"%s\"",
5642cba8dd3SJohn Baldwin 			    m->lock_object.lo_name);
5652604eb9eSMateusz Guzik 			do {
5661ada9041SMateusz Guzik 				lock_delay(&lda);
567391df78aSMateusz Guzik 				v = MTX_READ_VALUE(m);
5682604eb9eSMateusz Guzik 				owner = lv_mtx_owner(v);
5692604eb9eSMateusz Guzik 			} while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
5702cba8dd3SJohn Baldwin 			KTR_STATE0(KTR_SCHED, "thread",
5712cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
5722cba8dd3SJohn Baldwin 			    "running");
57349aead8aSAttilio Rao 			continue;
57449aead8aSAttilio Rao 		}
57549aead8aSAttilio Rao #endif
57649aead8aSAttilio Rao 
5772502c107SJeff Roberson 		ts = turnstile_trywait(&m->lock_object);
5782604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
5795fa8dd90SJohn Baldwin 
58036412d79SJohn Baldwin 		/*
5819ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
582961a7b24SJohn Baldwin 		 * the turnstile chain lock.
58336412d79SJohn Baldwin 		 */
5845fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
5852502c107SJeff Roberson 			turnstile_cancel(ts);
58636412d79SJohn Baldwin 			continue;
58736412d79SJohn Baldwin 		}
5889ed346baSBosko Milekic 
58949aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
59049aead8aSAttilio Rao 		/*
591fa29f023SJohn Baldwin 		 * The current lock owner might have started executing
592fa29f023SJohn Baldwin 		 * on another CPU (or the lock could have changed
593fa29f023SJohn Baldwin 		 * owners) while we were waiting on the turnstile
594fa29f023SJohn Baldwin 		 * chain lock.  If so, drop the turnstile lock and try
595fa29f023SJohn Baldwin 		 * again.
59649aead8aSAttilio Rao 		 */
5972604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
59849aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
59949aead8aSAttilio Rao 			turnstile_cancel(ts);
60049aead8aSAttilio Rao 			continue;
60149aead8aSAttilio Rao 		}
60249aead8aSAttilio Rao #endif
60349aead8aSAttilio Rao 
60436412d79SJohn Baldwin 		/*
6059ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
6069ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
6079ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
60836412d79SJohn Baldwin 		 */
60936412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
610122eceefSJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
6112502c107SJeff Roberson 			turnstile_cancel(ts);
6122604eb9eSMateusz Guzik 			v = MTX_READ_VALUE(m);
61336412d79SJohn Baldwin 			continue;
61436412d79SJohn Baldwin 		}
61536412d79SJohn Baldwin 
6169ed346baSBosko Milekic 		/*
6177feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
6189ed346baSBosko Milekic 		 */
61936412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
62036412d79SJohn Baldwin 
62102bd1bcdSIan Dowse #ifdef KTR
62202bd1bcdSIan Dowse 		if (!cont_logged) {
62302bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
62402bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
625aa89d8cdSJohn Baldwin 			    (void *)tid, file, line, m->lock_object.lo_name,
626aa89d8cdSJohn Baldwin 			    WITNESS_FILE(&m->lock_object),
627aa89d8cdSJohn Baldwin 			    WITNESS_LINE(&m->lock_object));
62802bd1bcdSIan Dowse 			cont_logged = 1;
62902bd1bcdSIan Dowse 		}
63002bd1bcdSIan Dowse #endif
63136412d79SJohn Baldwin 
6329ed346baSBosko Milekic 		/*
633961a7b24SJohn Baldwin 		 * Block on the turnstile.
6349ed346baSBosko Milekic 		 */
635a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
636e2b25737SMark Johnston 		sleep_time -= lockstat_nsecs(&m->lock_object);
637a5aedd68SStacey Son #endif
638284194f1SMateusz Guzik #ifndef ADAPTIVE_MUTEXES
639284194f1SMateusz Guzik 		owner = mtx_owner(m);
640284194f1SMateusz Guzik #endif
6418448e020SMateusz Guzik 		MPASS(owner == mtx_owner(m));
6428448e020SMateusz Guzik 		turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
643a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
644e2b25737SMark Johnston 		sleep_time += lockstat_nsecs(&m->lock_object);
645a5aedd68SStacey Son 		sleep_cnt++;
646a5aedd68SStacey Son #endif
6472604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
64836412d79SJohn Baldwin 	}
64902bd1bcdSIan Dowse #ifdef KTR
65002bd1bcdSIan Dowse 	if (cont_logged) {
65102bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
65202bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
653aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)tid, file, line);
65402bd1bcdSIan Dowse 	}
65502bd1bcdSIan Dowse #endif
656dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
657dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
6587640beb9SMateusz Guzik 		return;
659dfaa7859SMateusz Guzik #endif
660dfaa7859SMateusz Guzik #ifdef KDTRACE_HOOKS
6617640beb9SMateusz Guzik 	all_time += lockstat_nsecs(&m->lock_object);
6627640beb9SMateusz Guzik #endif
66332cd0147SMark Johnston 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
664eea4f254SJeff Roberson 	    waittime, file, line);
665a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
666a5aedd68SStacey Son 	if (sleep_time)
66732cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
668a5aedd68SStacey Son 
669a5aedd68SStacey Son 	/*
670a5aedd68SStacey Son 	 * Only record the loops spinning and not sleeping.
671a5aedd68SStacey Son 	 */
6721ada9041SMateusz Guzik 	if (lda.spin_cnt > sleep_cnt)
67332cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
674a5aedd68SStacey Son #endif
6759ed346baSBosko Milekic }
6769ed346baSBosko Milekic 
6772502c107SJeff Roberson static void
6782502c107SJeff Roberson _mtx_lock_spin_failed(struct mtx *m)
6792502c107SJeff Roberson {
6802502c107SJeff Roberson 	struct thread *td;
6812502c107SJeff Roberson 
6822502c107SJeff Roberson 	td = mtx_owner(m);
6832502c107SJeff Roberson 
6842502c107SJeff Roberson 	/* If the mutex is unlocked, try again. */
6852502c107SJeff Roberson 	if (td == NULL)
6862502c107SJeff Roberson 		return;
687b95b98b0SKonstantin Belousov 
6882502c107SJeff Roberson 	printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
6892502c107SJeff Roberson 	    m, m->lock_object.lo_name, td, td->td_tid);
6902502c107SJeff Roberson #ifdef WITNESS
69198332c8cSAttilio Rao 	witness_display_spinlock(&m->lock_object, td, printf);
6922502c107SJeff Roberson #endif
6932502c107SJeff Roberson 	panic("spin lock held too long");
6942502c107SJeff Roberson }
6952502c107SJeff Roberson 
696b95b98b0SKonstantin Belousov #ifdef SMP
6979ed346baSBosko Milekic /*
6987f44c618SAttilio Rao  * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
6999ed346baSBosko Milekic  *
7009ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
7019ed346baSBosko Milekic  * is handled inline.
7029ed346baSBosko Milekic  */
7030d74fe26SMateusz Guzik #if LOCK_DEBUG > 0
7049ed346baSBosko Milekic void
7050d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
7060d74fe26SMateusz Guzik     const char *file, int line)
7070d74fe26SMateusz Guzik #else
7080d74fe26SMateusz Guzik void
7090d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
7100d74fe26SMateusz Guzik #endif
71136412d79SJohn Baldwin {
7127f44c618SAttilio Rao 	struct mtx *m;
713a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
7140d74fe26SMateusz Guzik 	uintptr_t tid;
7151723a064SJeff Roberson #ifdef LOCK_PROFILING
7161723a064SJeff Roberson 	int contested = 0;
71770fe8436SKip Macy 	uint64_t waittime = 0;
7181723a064SJeff Roberson #endif
719076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
720076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
721076dd8ebSAndriy Gapon #endif
722dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
723dfaa7859SMateusz Guzik 	int doing_lockprof;
724dfaa7859SMateusz Guzik #endif
72536412d79SJohn Baldwin 
7260d74fe26SMateusz Guzik 	tid = (uintptr_t)curthread;
7277f44c618SAttilio Rao 	m = mtxlock2mtx(c);
7287f44c618SAttilio Rao 
72913d2ef0fSMateusz Guzik 	if (__predict_false(v == MTX_UNOWNED))
73013d2ef0fSMateusz Guzik 		v = MTX_READ_VALUE(m);
73113d2ef0fSMateusz Guzik 
73213d2ef0fSMateusz Guzik 	if (__predict_false(v == tid)) {
73313d2ef0fSMateusz Guzik 		m->mtx_recurse++;
73413d2ef0fSMateusz Guzik 		return;
73513d2ef0fSMateusz Guzik 	}
73613d2ef0fSMateusz Guzik 
7370d74fe26SMateusz Guzik 	if (SCHEDULER_STOPPED())
7380d74fe26SMateusz Guzik 		return;
7390d74fe26SMateusz Guzik 
7400d74fe26SMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
7410d74fe26SMateusz Guzik 
742aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
7435746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
7442cba8dd3SJohn Baldwin 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
7452cba8dd3SJohn Baldwin 	    "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
7469ed346baSBosko Milekic 
747f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
748f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
749f5f9340bSFabien Thomas #endif
75070fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
751dfaa7859SMateusz Guzik #ifdef LOCK_PROFILING
752dfaa7859SMateusz Guzik 	doing_lockprof = 1;
753df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS)
754dfaa7859SMateusz Guzik 	doing_lockprof = lockstat_enabled;
755dfaa7859SMateusz Guzik 	if (__predict_false(doing_lockprof))
756e2b25737SMark Johnston 		spin_time -= lockstat_nsecs(&m->lock_object);
757076dd8ebSAndriy Gapon #endif
758fc4f686dSMateusz Guzik 	for (;;) {
7592604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
76090836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
761fc4f686dSMateusz Guzik 				break;
76236412d79SJohn Baldwin 			continue;
763703fc290SJohn Baldwin 		}
7642604eb9eSMateusz Guzik 		/* Give interrupts a chance while we spin. */
7652604eb9eSMateusz Guzik 		spinlock_exit();
7662604eb9eSMateusz Guzik 		do {
7672604eb9eSMateusz Guzik 			if (lda.spin_cnt < 10000000) {
7682604eb9eSMateusz Guzik 				lock_delay(&lda);
7692604eb9eSMateusz Guzik 			} else {
770a0d45f0fSMateusz Guzik 				lda.spin_cnt++;
771a0d45f0fSMateusz Guzik 				if (lda.spin_cnt < 60000000 || kdb_active ||
772a0d45f0fSMateusz Guzik 				    panicstr != NULL)
77336412d79SJohn Baldwin 					DELAY(1);
7742502c107SJeff Roberson 				else
7752502c107SJeff Roberson 					_mtx_lock_spin_failed(m);
7769f1b87f1SMaxime Henrion 				cpu_spinwait();
77736412d79SJohn Baldwin 			}
7782604eb9eSMateusz Guzik 			v = MTX_READ_VALUE(m);
7792604eb9eSMateusz Guzik 		} while (v != MTX_UNOWNED);
780c6a37e84SJohn Baldwin 		spinlock_enter();
78136412d79SJohn Baldwin 	}
78236412d79SJohn Baldwin 
783aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
7849ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
7852cba8dd3SJohn Baldwin 	KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
7862cba8dd3SJohn Baldwin 	    "running");
7879ed346baSBosko Milekic 
788dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
789dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
790dfaa7859SMateusz Guzik 		return;
791dfaa7859SMateusz Guzik #endif
792c6d48c87SMark Johnston #ifdef KDTRACE_HOOKS
793dfaa7859SMateusz Guzik 	spin_time += lockstat_nsecs(&m->lock_object);
794dfaa7859SMateusz Guzik #endif
79532cd0147SMark Johnston 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
79632cd0147SMark Johnston 	    contested, waittime, file, line);
797dfaa7859SMateusz Guzik #ifdef KDTRACE_HOOKS
798d0f68f91SMark Johnston 	if (lda.spin_cnt != 0)
79932cd0147SMark Johnston 		LOCKSTAT_RECORD1(spin__spin, m, spin_time);
800c6d48c87SMark Johnston #endif
80136412d79SJohn Baldwin }
80233fb8a38SJohn Baldwin #endif /* SMP */
80336412d79SJohn Baldwin 
804be49509eSMateusz Guzik #ifdef INVARIANTS
805be49509eSMateusz Guzik static void
806be49509eSMateusz Guzik thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
807be49509eSMateusz Guzik {
808be49509eSMateusz Guzik 
809be49509eSMateusz Guzik 	KASSERT(m->mtx_lock != MTX_DESTROYED,
810be49509eSMateusz Guzik 	    ("thread_lock() of destroyed mutex @ %s:%d", file, line));
811be49509eSMateusz Guzik 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
812be49509eSMateusz Guzik 	    ("thread_lock() of sleep mutex %s @ %s:%d",
813be49509eSMateusz Guzik 	    m->lock_object.lo_name, file, line));
814be49509eSMateusz Guzik 	if (mtx_owned(m))
815be49509eSMateusz Guzik 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
816be49509eSMateusz Guzik 		    ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
817be49509eSMateusz Guzik 		    m->lock_object.lo_name, file, line));
818be49509eSMateusz Guzik 	WITNESS_CHECKORDER(&m->lock_object,
819be49509eSMateusz Guzik 	    opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
820be49509eSMateusz Guzik }
821be49509eSMateusz Guzik #else
822be49509eSMateusz Guzik #define thread_lock_validate(m, opts, file, line) do { } while (0)
823be49509eSMateusz Guzik #endif
824be49509eSMateusz Guzik 
825be49509eSMateusz Guzik #ifndef LOCK_PROFILING
826be49509eSMateusz Guzik #if LOCK_DEBUG > 0
827be49509eSMateusz Guzik void
828be49509eSMateusz Guzik _thread_lock(struct thread *td, int opts, const char *file, int line)
829be49509eSMateusz Guzik #else
830be49509eSMateusz Guzik void
831be49509eSMateusz Guzik _thread_lock(struct thread *td)
832be49509eSMateusz Guzik #endif
833be49509eSMateusz Guzik {
834be49509eSMateusz Guzik 	struct mtx *m;
835be49509eSMateusz Guzik 	uintptr_t tid, v;
836be49509eSMateusz Guzik 
837be49509eSMateusz Guzik 	tid = (uintptr_t)curthread;
838be49509eSMateusz Guzik 
8392c50bafeSMateusz Guzik 	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
8402c50bafeSMateusz Guzik 		goto slowpath_noirq;
841be49509eSMateusz Guzik 	spinlock_enter();
842be49509eSMateusz Guzik 	m = td->td_lock;
843be49509eSMateusz Guzik 	thread_lock_validate(m, 0, file, line);
844be49509eSMateusz Guzik 	v = MTX_READ_VALUE(m);
845be49509eSMateusz Guzik 	if (__predict_true(v == MTX_UNOWNED)) {
846be49509eSMateusz Guzik 		if (__predict_false(!_mtx_obtain_lock(m, tid)))
847be49509eSMateusz Guzik 			goto slowpath_unlocked;
848be49509eSMateusz Guzik 	} else if (v == tid) {
849be49509eSMateusz Guzik 		m->mtx_recurse++;
850be49509eSMateusz Guzik 	} else
851be49509eSMateusz Guzik 		goto slowpath_unlocked;
852be49509eSMateusz Guzik 	if (__predict_true(m == td->td_lock)) {
853be49509eSMateusz Guzik 		WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
854be49509eSMateusz Guzik 		return;
855be49509eSMateusz Guzik 	}
856be49509eSMateusz Guzik 	if (m->mtx_recurse != 0)
857be49509eSMateusz Guzik 		m->mtx_recurse--;
858be49509eSMateusz Guzik 	else
859be49509eSMateusz Guzik 		_mtx_release_lock_quick(m);
860be49509eSMateusz Guzik slowpath_unlocked:
861be49509eSMateusz Guzik 	spinlock_exit();
8622c50bafeSMateusz Guzik slowpath_noirq:
8632c50bafeSMateusz Guzik #if LOCK_DEBUG > 0
8642c50bafeSMateusz Guzik 	thread_lock_flags_(td, opts, file, line);
8652c50bafeSMateusz Guzik #else
866be49509eSMateusz Guzik 	thread_lock_flags_(td, 0, 0, 0);
8672c50bafeSMateusz Guzik #endif
868be49509eSMateusz Guzik }
869be49509eSMateusz Guzik #endif
870be49509eSMateusz Guzik 
8712502c107SJeff Roberson void
872ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
8732502c107SJeff Roberson {
8742502c107SJeff Roberson 	struct mtx *m;
8755e5ad162SMateusz Guzik 	uintptr_t tid, v;
876a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
8771723a064SJeff Roberson #ifdef LOCK_PROFILING
8781723a064SJeff Roberson 	int contested = 0;
8791723a064SJeff Roberson 	uint64_t waittime = 0;
8801723a064SJeff Roberson #endif
881a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
882076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
883a5aedd68SStacey Son #endif
884dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
885dfaa7859SMateusz Guzik 	int doing_lockprof = 1;
886dfaa7859SMateusz Guzik #endif
8872502c107SJeff Roberson 
8882502c107SJeff Roberson 	tid = (uintptr_t)curthread;
88935370593SAndriy Gapon 
890f61d6c5aSMark Johnston 	if (SCHEDULER_STOPPED()) {
891f61d6c5aSMark Johnston 		/*
892f61d6c5aSMark Johnston 		 * Ensure that spinlock sections are balanced even when the
893f61d6c5aSMark Johnston 		 * scheduler is stopped, since we may otherwise inadvertently
894f61d6c5aSMark Johnston 		 * re-enable interrupts while dumping core.
895f61d6c5aSMark Johnston 		 */
896f61d6c5aSMark Johnston 		spinlock_enter();
89735370593SAndriy Gapon 		return;
898f61d6c5aSMark Johnston 	}
89935370593SAndriy Gapon 
900a0d45f0fSMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
901a0d45f0fSMateusz Guzik 
902dfaa7859SMateusz Guzik #ifdef LOCK_PROFILING
903dfaa7859SMateusz Guzik 	doing_lockprof = 1;
904df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS)
905dfaa7859SMateusz Guzik 	doing_lockprof = lockstat_enabled;
906dfaa7859SMateusz Guzik 	if (__predict_false(doing_lockprof))
907e2b25737SMark Johnston 		spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
908076dd8ebSAndriy Gapon #endif
9092502c107SJeff Roberson 	for (;;) {
9102502c107SJeff Roberson retry:
911dc089651SMateusz Guzik 		v = MTX_UNOWNED;
9122502c107SJeff Roberson 		spinlock_enter();
913710eacdcSJeff Roberson 		m = td->td_lock;
914be49509eSMateusz Guzik 		thread_lock_validate(m, opts, file, line);
915fc4f686dSMateusz Guzik 		for (;;) {
91690836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
917fc4f686dSMateusz Guzik 				break;
91890836c32SMateusz Guzik 			if (v == MTX_UNOWNED)
9195e5ad162SMateusz Guzik 				continue;
9205e5ad162SMateusz Guzik 			if (v == tid) {
9212502c107SJeff Roberson 				m->mtx_recurse++;
9222502c107SJeff Roberson 				break;
9232502c107SJeff Roberson 			}
924f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
925f5f9340bSFabien Thomas 			PMC_SOFT_CALL( , , lock, failed);
926f5f9340bSFabien Thomas #endif
927eea4f254SJeff Roberson 			lock_profile_obtain_lock_failed(&m->lock_object,
928eea4f254SJeff Roberson 			    &contested, &waittime);
9292502c107SJeff Roberson 			/* Give interrupts a chance while we spin. */
9302502c107SJeff Roberson 			spinlock_exit();
9315e5ad162SMateusz Guzik 			do {
932a0d45f0fSMateusz Guzik 				if (lda.spin_cnt < 10000000) {
933a0d45f0fSMateusz Guzik 					lock_delay(&lda);
934a0d45f0fSMateusz Guzik 				} else {
935a0d45f0fSMateusz Guzik 					lda.spin_cnt++;
936a0d45f0fSMateusz Guzik 					if (lda.spin_cnt < 60000000 ||
9372502c107SJeff Roberson 					    kdb_active || panicstr != NULL)
9382502c107SJeff Roberson 						DELAY(1);
9392502c107SJeff Roberson 					else
9402502c107SJeff Roberson 						_mtx_lock_spin_failed(m);
9412502c107SJeff Roberson 					cpu_spinwait();
942a0d45f0fSMateusz Guzik 				}
9432502c107SJeff Roberson 				if (m != td->td_lock)
9442502c107SJeff Roberson 					goto retry;
9455e5ad162SMateusz Guzik 				v = MTX_READ_VALUE(m);
9465e5ad162SMateusz Guzik 			} while (v != MTX_UNOWNED);
9472502c107SJeff Roberson 			spinlock_enter();
9482502c107SJeff Roberson 		}
9492502c107SJeff Roberson 		if (m == td->td_lock)
9502502c107SJeff Roberson 			break;
951961135eaSJohn Baldwin 		__mtx_unlock_spin(m);	/* does spinlock_exit() */
9522502c107SJeff Roberson 	}
953dfaa7859SMateusz Guzik 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
954dfaa7859SMateusz Guzik 	    line);
955dfaa7859SMateusz Guzik 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
956dfaa7859SMateusz Guzik 
957dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
958dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
959dfaa7859SMateusz Guzik 		return;
960dfaa7859SMateusz Guzik #endif
961076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
962e2b25737SMark Johnston 	spin_time += lockstat_nsecs(&m->lock_object);
963076dd8ebSAndriy Gapon #endif
964eea4f254SJeff Roberson 	if (m->mtx_recurse == 0)
96532cd0147SMark Johnston 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
96632cd0147SMark Johnston 		    contested, waittime, file, line);
9675002e195SMark Johnston #ifdef KDTRACE_HOOKS
968d0f68f91SMark Johnston 	if (lda.spin_cnt != 0)
96932cd0147SMark Johnston 		LOCKSTAT_RECORD1(thread__spin, m, spin_time);
9705002e195SMark Johnston #endif
9712502c107SJeff Roberson }
9722502c107SJeff Roberson 
9732502c107SJeff Roberson struct mtx *
9742502c107SJeff Roberson thread_lock_block(struct thread *td)
9752502c107SJeff Roberson {
9762502c107SJeff Roberson 	struct mtx *lock;
9772502c107SJeff Roberson 
9782502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
979710eacdcSJeff Roberson 	lock = td->td_lock;
9802502c107SJeff Roberson 	td->td_lock = &blocked_lock;
9812502c107SJeff Roberson 	mtx_unlock_spin(lock);
9822502c107SJeff Roberson 
9832502c107SJeff Roberson 	return (lock);
9842502c107SJeff Roberson }
9852502c107SJeff Roberson 
9862502c107SJeff Roberson void
9872502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new)
9882502c107SJeff Roberson {
9892502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
9902502c107SJeff Roberson 	MPASS(td->td_lock == &blocked_lock);
99165d32cd8SMatt Jacob 	atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
9922502c107SJeff Roberson }
9932502c107SJeff Roberson 
9942502c107SJeff Roberson void
9952502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new)
9962502c107SJeff Roberson {
9972502c107SJeff Roberson 	struct mtx *lock;
9982502c107SJeff Roberson 
9992502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
10002502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
1001710eacdcSJeff Roberson 	lock = td->td_lock;
10022502c107SJeff Roberson 	td->td_lock = new;
10032502c107SJeff Roberson 	mtx_unlock_spin(lock);
10042502c107SJeff Roberson }
10052502c107SJeff Roberson 
10069ed346baSBosko Milekic /*
10077f44c618SAttilio Rao  * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
10089ed346baSBosko Milekic  *
10093b3cf014SMateusz Guzik  * We are only called here if the lock is recursed, contested (i.e. we
10103b3cf014SMateusz Guzik  * need to wake up a blocked thread) or lockstat probe is active.
10119ed346baSBosko Milekic  */
101209f1319aSMateusz Guzik #if LOCK_DEBUG > 0
101336412d79SJohn Baldwin void
1014b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
1015b584eb2eSMateusz Guzik     const char *file, int line)
101609f1319aSMateusz Guzik #else
101709f1319aSMateusz Guzik void
1018b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
101909f1319aSMateusz Guzik #endif
102036412d79SJohn Baldwin {
10217f44c618SAttilio Rao 	struct mtx *m;
1022961a7b24SJohn Baldwin 	struct turnstile *ts;
1023b584eb2eSMateusz Guzik 	uintptr_t tid;
10249ed346baSBosko Milekic 
102535370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
102635370593SAndriy Gapon 		return;
102735370593SAndriy Gapon 
10283b3cf014SMateusz Guzik 	tid = (uintptr_t)curthread;
10297f44c618SAttilio Rao 	m = mtxlock2mtx(c);
1030b584eb2eSMateusz Guzik 
1031b584eb2eSMateusz Guzik 	if (__predict_false(v == tid))
10323b3cf014SMateusz Guzik 		v = MTX_READ_VALUE(m);
103390836c32SMateusz Guzik 
1034b584eb2eSMateusz Guzik 	if (__predict_false(v & MTX_RECURSED)) {
103536412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
103608812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1037aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
10389ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
103936412d79SJohn Baldwin 		return;
104036412d79SJohn Baldwin 	}
10419ed346baSBosko Milekic 
10423b3cf014SMateusz Guzik 	LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
10433b3cf014SMateusz Guzik 	if (v == tid && _mtx_release_lock(m, tid))
10443b3cf014SMateusz Guzik 		return;
10453b3cf014SMateusz Guzik 
10462502c107SJeff Roberson 	/*
10472502c107SJeff Roberson 	 * We have to lock the chain before the turnstile so this turnstile
10482502c107SJeff Roberson 	 * can be removed from the hash list if it is empty.
10492502c107SJeff Roberson 	 */
10502502c107SJeff Roberson 	turnstile_chain_lock(&m->lock_object);
10518448e020SMateusz Guzik 	_mtx_release_lock_quick(m);
1052aa89d8cdSJohn Baldwin 	ts = turnstile_lookup(&m->lock_object);
10538448e020SMateusz Guzik 	MPASS(ts != NULL);
1054aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
10559ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
10567aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1057bf9c6c31SJohn Baldwin 
10582502c107SJeff Roberson 	/*
10592502c107SJeff Roberson 	 * This turnstile is now no longer associated with the mutex.  We can
10602502c107SJeff Roberson 	 * unlock the chain lock so a new turnstile may take it's place.
10612502c107SJeff Roberson 	 */
10627aa4f685SJohn Baldwin 	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
10632502c107SJeff Roberson 	turnstile_chain_unlock(&m->lock_object);
10649ed346baSBosko Milekic }
10659ed346baSBosko Milekic 
10669ed346baSBosko Milekic /*
10679ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
1068961135eaSJohn Baldwin  * See the __mtx_unlock_spin() macro for the details.
10699ed346baSBosko Milekic  */
10709ed346baSBosko Milekic 
10719ed346baSBosko Milekic /*
107215ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
10739ed346baSBosko Milekic  */
10741103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
10750cde2e34SJason Evans void
10767f44c618SAttilio Rao __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
10770cde2e34SJason Evans {
10787f44c618SAttilio Rao 	const struct mtx *m;
10795cb0fbe4SJohn Baldwin 
1080310ab671SEric van Gyzen 	if (panicstr != NULL || dumping || SCHEDULER_STOPPED())
10815cb0fbe4SJohn Baldwin 		return;
10827f44c618SAttilio Rao 
10837f44c618SAttilio Rao 	m = mtxlock2mtx(c);
10847f44c618SAttilio Rao 
1085a10f4966SJake Burkholder 	switch (what) {
10860cde2e34SJason Evans 	case MA_OWNED:
10870cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
10880cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
1089a10f4966SJake Burkholder 		if (!mtx_owned(m))
10900cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
1091aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
1092a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
1093a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
10940cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
1095aa89d8cdSJohn Baldwin 				    m->lock_object.lo_name, file, line);
1096a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
10970cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
1098aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
10990cde2e34SJason Evans 		}
11000cde2e34SJason Evans 		break;
11010cde2e34SJason Evans 	case MA_NOTOWNED:
1102a10f4966SJake Burkholder 		if (mtx_owned(m))
11030cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
1104aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
11050cde2e34SJason Evans 		break;
11060cde2e34SJason Evans 	default:
110756771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
11080cde2e34SJason Evans 	}
11090cde2e34SJason Evans }
11100cde2e34SJason Evans #endif
11110cde2e34SJason Evans 
11129ed346baSBosko Milekic /*
1113c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
1114c27b5699SAndrew R. Reiter  */
1115c27b5699SAndrew R. Reiter void
1116c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
1117c27b5699SAndrew R. Reiter {
1118c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
1119c27b5699SAndrew R. Reiter 
11207f44c618SAttilio Rao 	mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
11217f44c618SAttilio Rao 	    margs->ma_opts);
1122c27b5699SAndrew R. Reiter }
1123c27b5699SAndrew R. Reiter 
1124c27b5699SAndrew R. Reiter /*
11259ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
11260c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
11270c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
11280c88508aSJohn Baldwin  * witness.
11299ed346baSBosko Milekic  */
113036412d79SJohn Baldwin void
11317f44c618SAttilio Rao _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
113236412d79SJohn Baldwin {
11337f44c618SAttilio Rao 	struct mtx *m;
113483a81bcbSJohn Baldwin 	struct lock_class *class;
113583a81bcbSJohn Baldwin 	int flags;
11369ed346baSBosko Milekic 
11377f44c618SAttilio Rao 	m = mtxlock2mtx(c);
11387f44c618SAttilio Rao 
113919284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1140fd07ddcfSDmitry Chagin 	    MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1141353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1142353998acSAttilio Rao 	    ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1143353998acSAttilio Rao 	    &m->mtx_lock));
11449ed346baSBosko Milekic 
114583a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
114619284646SJohn Baldwin 	if (opts & MTX_SPIN)
114783a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
114819284646SJohn Baldwin 	else
114983a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
115083a81bcbSJohn Baldwin 	flags = 0;
115119284646SJohn Baldwin 	if (opts & MTX_QUIET)
115283a81bcbSJohn Baldwin 		flags |= LO_QUIET;
115319284646SJohn Baldwin 	if (opts & MTX_RECURSE)
115483a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
115519284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
115683a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
1157f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
115883a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
11597c0435b9SKip Macy 	if (opts & MTX_NOPROFILE)
11607c0435b9SKip Macy 		flags |= LO_NOPROFILE;
1161fd07ddcfSDmitry Chagin 	if (opts & MTX_NEW)
1162fd07ddcfSDmitry Chagin 		flags |= LO_NEW;
116319284646SJohn Baldwin 
116483a81bcbSJohn Baldwin 	/* Initialize mutex. */
1165b5fb43e5SJohn Baldwin 	lock_init(&m->lock_object, class, name, type, flags);
1166b5fb43e5SJohn Baldwin 
116719284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
116883a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
116936412d79SJohn Baldwin }
117036412d79SJohn Baldwin 
11719ed346baSBosko Milekic /*
117219284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
117319284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
117419284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
117519284646SJohn Baldwin  * flags.
11769ed346baSBosko Milekic  */
117736412d79SJohn Baldwin void
11787f44c618SAttilio Rao _mtx_destroy(volatile uintptr_t *c)
117936412d79SJohn Baldwin {
11807f44c618SAttilio Rao 	struct mtx *m;
11817f44c618SAttilio Rao 
11827f44c618SAttilio Rao 	m = mtxlock2mtx(c);
118336412d79SJohn Baldwin 
118419284646SJohn Baldwin 	if (!mtx_owned(m))
118519284646SJohn Baldwin 		MPASS(mtx_unowned(m));
118619284646SJohn Baldwin 	else {
118708812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
11889ed346baSBosko Milekic 
1189861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
1190aa89d8cdSJohn Baldwin 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1191861a2308SScott Long 			spinlock_exit();
1192764e4d54SJohn Baldwin 		else
1193ce1c953eSMark Johnston 			TD_LOCKS_DEC(curthread);
1194861a2308SScott Long 
1195d3df4af3SJeff Roberson 		lock_profile_release_lock(&m->lock_object);
119619284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
1197aa89d8cdSJohn Baldwin 		WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1198c86b6ff5SJohn Baldwin 		    __LINE__);
119936412d79SJohn Baldwin 	}
12000384fff8SJason Evans 
1201186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
1202aa89d8cdSJohn Baldwin 	lock_destroy(&m->lock_object);
12030384fff8SJason Evans }
1204d23f5958SMatthew Dillon 
1205d23f5958SMatthew Dillon /*
1206c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
1207c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
1208c53c013bSJohn Baldwin  * setup before this is called.
1209c53c013bSJohn Baldwin  */
1210c53c013bSJohn Baldwin void
1211c53c013bSJohn Baldwin mutex_init(void)
1212c53c013bSJohn Baldwin {
1213c53c013bSJohn Baldwin 
1214961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
1215961a7b24SJohn Baldwin 	init_turnstiles();
1216961a7b24SJohn Baldwin 
1217c53c013bSJohn Baldwin 	/*
1218c53c013bSJohn Baldwin 	 * Initialize mutexes.
1219c53c013bSJohn Baldwin 	 */
12200c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
12212502c107SJeff Roberson 	mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
12222502c107SJeff Roberson 	blocked_lock.mtx_lock = 0xdeadc0de;	/* Always blocked. */
12230c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
12246afb32fcSKonstantin Belousov 	mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
12255c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
12265c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
12275c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
12288c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1229c53c013bSJohn Baldwin 	mtx_lock(&Giant);
1230c53c013bSJohn Baldwin }
1231d272fe53SJohn Baldwin 
1232d272fe53SJohn Baldwin #ifdef DDB
1233d272fe53SJohn Baldwin void
1234d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock)
1235d272fe53SJohn Baldwin {
1236d272fe53SJohn Baldwin 	struct thread *td;
1237d576deedSPawel Jakub Dawidek 	const struct mtx *m;
1238d272fe53SJohn Baldwin 
1239d576deedSPawel Jakub Dawidek 	m = (const struct mtx *)lock;
1240d272fe53SJohn Baldwin 
1241d272fe53SJohn Baldwin 	db_printf(" flags: {");
124283a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1243d272fe53SJohn Baldwin 		db_printf("SPIN");
1244d272fe53SJohn Baldwin 	else
1245d272fe53SJohn Baldwin 		db_printf("DEF");
1246aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_RECURSABLE)
1247d272fe53SJohn Baldwin 		db_printf(", RECURSE");
1248aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_DUPOK)
1249d272fe53SJohn Baldwin 		db_printf(", DUPOK");
1250d272fe53SJohn Baldwin 	db_printf("}\n");
1251d272fe53SJohn Baldwin 	db_printf(" state: {");
1252d272fe53SJohn Baldwin 	if (mtx_unowned(m))
1253d272fe53SJohn Baldwin 		db_printf("UNOWNED");
1254c0bfd703SJohn Baldwin 	else if (mtx_destroyed(m))
1255c0bfd703SJohn Baldwin 		db_printf("DESTROYED");
1256d272fe53SJohn Baldwin 	else {
1257d272fe53SJohn Baldwin 		db_printf("OWNED");
1258d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
1259d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
1260d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
1261d272fe53SJohn Baldwin 			db_printf(", RECURSED");
1262d272fe53SJohn Baldwin 	}
1263d272fe53SJohn Baldwin 	db_printf("}\n");
1264c0bfd703SJohn Baldwin 	if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1265d272fe53SJohn Baldwin 		td = mtx_owner(m);
1266d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1267431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
1268d272fe53SJohn Baldwin 		if (mtx_recursed(m))
1269d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
1270d272fe53SJohn Baldwin 	}
1271d272fe53SJohn Baldwin }
1272d272fe53SJohn Baldwin #endif
1273