xref: /freebsd/sys/kern/kern_mutex.c (revision 2ccee9cc522bb6260efba1b6efebfee2ba43c606)
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 
143574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_delay;
1441ada9041SMateusz Guzik 
1458e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
1461ada9041SMateusz Guzik     0, "");
1471ada9041SMateusz Guzik SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
1481ada9041SMateusz Guzik     0, "");
1491ada9041SMateusz Guzik 
1508e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
1511ada9041SMateusz Guzik #endif
1521ada9041SMateusz Guzik 
153a0d45f0fSMateusz Guzik static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL,
154a0d45f0fSMateusz Guzik     "mtx spin debugging");
155a0d45f0fSMateusz Guzik 
156574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_spin_delay;
157a0d45f0fSMateusz Guzik 
1588e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW,
1598e5a3e9aSMateusz Guzik     &mtx_spin_delay.base, 0, "");
1608e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW,
1618e5a3e9aSMateusz Guzik     &mtx_spin_delay.max, 0, "");
162a0d45f0fSMateusz Guzik 
1638e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
164a0d45f0fSMateusz Guzik 
1659ed346baSBosko Milekic /*
166c53c013bSJohn Baldwin  * System-wide mutexes
167c53c013bSJohn Baldwin  */
1682502c107SJeff Roberson struct mtx blocked_lock;
1696a569d35SMateusz Guzik struct mtx __exclusive_cache_line Giant;
170c53c013bSJohn Baldwin 
17167784314SPoul-Henning Kamp void
172d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what)
173f9721b43SAttilio Rao {
174f9721b43SAttilio Rao 
175d576deedSPawel Jakub Dawidek 	mtx_assert((const struct mtx *)lock, what);
176f9721b43SAttilio Rao }
177f9721b43SAttilio Rao 
17867784314SPoul-Henning Kamp void
1797faf4d90SDavide Italiano lock_mtx(struct lock_object *lock, uintptr_t how)
1806e21afd4SJohn Baldwin {
1816e21afd4SJohn Baldwin 
1826e21afd4SJohn Baldwin 	mtx_lock((struct mtx *)lock);
1836e21afd4SJohn Baldwin }
1846e21afd4SJohn Baldwin 
18567784314SPoul-Henning Kamp void
1867faf4d90SDavide Italiano lock_spin(struct lock_object *lock, uintptr_t how)
1876e21afd4SJohn Baldwin {
1886e21afd4SJohn Baldwin 
1896e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
1906e21afd4SJohn Baldwin }
1916e21afd4SJohn Baldwin 
1927faf4d90SDavide Italiano uintptr_t
1936e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock)
1946e21afd4SJohn Baldwin {
1956e21afd4SJohn Baldwin 	struct mtx *m;
1966e21afd4SJohn Baldwin 
1976e21afd4SJohn Baldwin 	m = (struct mtx *)lock;
1986e21afd4SJohn Baldwin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
1996e21afd4SJohn Baldwin 	mtx_unlock(m);
2006e21afd4SJohn Baldwin 	return (0);
2016e21afd4SJohn Baldwin }
2026e21afd4SJohn Baldwin 
2037faf4d90SDavide Italiano uintptr_t
2046e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock)
2056e21afd4SJohn Baldwin {
2066e21afd4SJohn Baldwin 
2076e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
2086e21afd4SJohn Baldwin }
2096e21afd4SJohn Baldwin 
210a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
211a5aedd68SStacey Son int
212d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner)
213a5aedd68SStacey Son {
21402315a67SMark Johnston 	const struct mtx *m;
21502315a67SMark Johnston 	uintptr_t x;
216a5aedd68SStacey Son 
21702315a67SMark Johnston 	m = (const struct mtx *)lock;
21802315a67SMark Johnston 	x = m->mtx_lock;
21902315a67SMark Johnston 	*owner = (struct thread *)(x & ~MTX_FLAGMASK);
220e280ce46SMateusz Guzik 	return (*owner != NULL);
221a5aedd68SStacey Son }
222a5aedd68SStacey Son #endif
223a5aedd68SStacey Son 
2240cde2e34SJason Evans /*
2256283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2266283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2276283b7d0SJohn Baldwin  */
2286283b7d0SJohn Baldwin void
2297f44c618SAttilio Rao __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
2306283b7d0SJohn Baldwin {
2317f44c618SAttilio Rao 	struct mtx *m;
23208da2677SMateusz Guzik 	uintptr_t tid, v;
2336283b7d0SJohn Baldwin 
2347f44c618SAttilio Rao 	m = mtxlock2mtx(c);
2357f44c618SAttilio Rao 
236704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
237704cb42fSMark Johnston 	    !TD_IS_IDLETHREAD(curthread),
238e3ae0dfeSAttilio Rao 	    ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
239e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
240186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
241186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
242aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
243aa89d8cdSJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2440d975d63SJohn Baldwin 	    file, line));
245ac6b769bSAttilio Rao 	WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
246ac6b769bSAttilio Rao 	    LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
2477c0435b9SKip Macy 
24808da2677SMateusz Guzik 	tid = (uintptr_t)curthread;
24908da2677SMateusz Guzik 	v = MTX_UNOWNED;
25008da2677SMateusz Guzik 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
2512f1ddb89SMateusz Guzik 		_mtx_lock_sleep(m, v, opts, file, line);
25208da2677SMateusz Guzik 	else
25308da2677SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
25408da2677SMateusz Guzik 		    m, 0, 0, file, line);
255aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
256dde96c99SJohn Baldwin 	    line);
257ac6b769bSAttilio Rao 	WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
258ac6b769bSAttilio Rao 	    file, line);
259ce1c953eSMark Johnston 	TD_LOCKS_INC(curthread);
2606283b7d0SJohn Baldwin }
2616283b7d0SJohn Baldwin 
2626283b7d0SJohn Baldwin void
2637f44c618SAttilio Rao __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
2646283b7d0SJohn Baldwin {
2657f44c618SAttilio Rao 	struct mtx *m;
26635370593SAndriy Gapon 
2677f44c618SAttilio Rao 	m = mtxlock2mtx(c);
2687f44c618SAttilio Rao 
269186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
270186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
271aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
272aa89d8cdSJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2730d975d63SJohn Baldwin 	    file, line));
274aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
275aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
2760d975d63SJohn Baldwin 	    line);
27721377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
278c66d7606SKip Macy 
279ffd5c94cSMateusz Guzik #ifdef LOCK_PROFILING
28008da2677SMateusz Guzik 	__mtx_unlock_sleep(c, opts, file, line);
281ffd5c94cSMateusz Guzik #else
282ffd5c94cSMateusz Guzik 	__mtx_unlock(m, curthread, opts, file, line);
283ffd5c94cSMateusz Guzik #endif
284ce1c953eSMark Johnston 	TD_LOCKS_DEC(curthread);
2856283b7d0SJohn Baldwin }
2866283b7d0SJohn Baldwin 
2876283b7d0SJohn Baldwin void
2887f44c618SAttilio Rao __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
2897f44c618SAttilio Rao     int line)
2906283b7d0SJohn Baldwin {
2917f44c618SAttilio Rao 	struct mtx *m;
29262bf13cbSMateusz Guzik #ifdef SMP
2930d74fe26SMateusz Guzik 	uintptr_t tid, v;
29462bf13cbSMateusz Guzik #endif
2956283b7d0SJohn Baldwin 
2967f44c618SAttilio Rao 	m = mtxlock2mtx(c);
2977f44c618SAttilio Rao 
298186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
299186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
300aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
3010d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
302aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
303ad69e26bSJohn Baldwin 	if (mtx_owned(m))
304ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
305ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
306ad69e26bSJohn Baldwin 	    ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
307ad69e26bSJohn Baldwin 		    m->lock_object.lo_name, file, line));
308ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
309aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
31041313430SJohn Baldwin 	    file, line, NULL);
31162bf13cbSMateusz Guzik #ifdef SMP
3120d74fe26SMateusz Guzik 	spinlock_enter();
3130d74fe26SMateusz Guzik 	tid = (uintptr_t)curthread;
3140d74fe26SMateusz Guzik 	v = MTX_UNOWNED;
3150d74fe26SMateusz Guzik 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
3160d74fe26SMateusz Guzik 		_mtx_lock_spin(m, v, opts, file, line);
3170d74fe26SMateusz Guzik 	else
3180d74fe26SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire,
3190d74fe26SMateusz Guzik 		    m, 0, 0, file, line);
32062bf13cbSMateusz Guzik #else
32162bf13cbSMateusz Guzik 	__mtx_lock_spin(m, curthread, opts, file, line);
32262bf13cbSMateusz Guzik #endif
323aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
324dde96c99SJohn Baldwin 	    line);
325aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
3266283b7d0SJohn Baldwin }
3276283b7d0SJohn Baldwin 
32890b581f2SKonstantin Belousov int
32990b581f2SKonstantin Belousov __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
33090b581f2SKonstantin Belousov     int line)
33190b581f2SKonstantin Belousov {
33290b581f2SKonstantin Belousov 	struct mtx *m;
33390b581f2SKonstantin Belousov 
33490b581f2SKonstantin Belousov 	if (SCHEDULER_STOPPED())
33590b581f2SKonstantin Belousov 		return (1);
33690b581f2SKonstantin Belousov 
33790b581f2SKonstantin Belousov 	m = mtxlock2mtx(c);
33890b581f2SKonstantin Belousov 
33990b581f2SKonstantin Belousov 	KASSERT(m->mtx_lock != MTX_DESTROYED,
34090b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
34190b581f2SKonstantin Belousov 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
34290b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
34390b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
34490b581f2SKonstantin Belousov 	KASSERT((opts & MTX_RECURSE) == 0,
34590b581f2SKonstantin Belousov 	    ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
34690b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
34790b581f2SKonstantin Belousov 	if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
34890b581f2SKonstantin Belousov 		LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
34990b581f2SKonstantin Belousov 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
35090b581f2SKonstantin Belousov 		return (1);
35190b581f2SKonstantin Belousov 	}
35290b581f2SKonstantin Belousov 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
35390b581f2SKonstantin Belousov 	return (0);
35490b581f2SKonstantin Belousov }
35590b581f2SKonstantin Belousov 
3566283b7d0SJohn Baldwin void
3577f44c618SAttilio Rao __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
3587f44c618SAttilio Rao     int line)
3596283b7d0SJohn Baldwin {
3607f44c618SAttilio Rao 	struct mtx *m;
361c66d7606SKip Macy 
3627f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3637f44c618SAttilio Rao 
364186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
365186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
366aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
3670d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
368aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
369aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
370aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
371dde96c99SJohn Baldwin 	    line);
3720d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
373c66d7606SKip Macy 
374961135eaSJohn Baldwin 	__mtx_unlock_spin(m);
3756283b7d0SJohn Baldwin }
3766283b7d0SJohn Baldwin 
3776283b7d0SJohn Baldwin /*
3789ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
379eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
380eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
3810cde2e34SJason Evans  */
3820cde2e34SJason Evans int
3837f44c618SAttilio Rao _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
3840cde2e34SJason Evans {
3857f44c618SAttilio Rao 	struct mtx *m;
3865c5df0d9SMateusz Guzik 	struct thread *td;
3875c5df0d9SMateusz Guzik 	uintptr_t tid, v;
3881723a064SJeff Roberson #ifdef LOCK_PROFILING
3897c0435b9SKip Macy 	uint64_t waittime = 0;
3901723a064SJeff Roberson 	int contested = 0;
3911723a064SJeff Roberson #endif
3921723a064SJeff Roberson 	int rval;
3935c5df0d9SMateusz Guzik 	bool recursed;
3940cde2e34SJason Evans 
3955c5df0d9SMateusz Guzik 	td = curthread;
3965c5df0d9SMateusz Guzik 	tid = (uintptr_t)td;
3975c5df0d9SMateusz Guzik 	if (SCHEDULER_STOPPED_TD(td))
39835370593SAndriy Gapon 		return (1);
39935370593SAndriy Gapon 
4007f44c618SAttilio Rao 	m = mtxlock2mtx(c);
4017f44c618SAttilio Rao 
402704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
403e3ae0dfeSAttilio Rao 	    ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
404e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
405186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
406186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
407aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
408aa89d8cdSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
40983cece6fSJohn Baldwin 	    file, line));
4109ed346baSBosko Milekic 
4115c5df0d9SMateusz Guzik 	rval = 1;
4125c5df0d9SMateusz Guzik 	recursed = false;
4135c5df0d9SMateusz Guzik 	v = MTX_UNOWNED;
414b247fd39SMateusz Guzik 	for (;;) {
415b247fd39SMateusz Guzik 		if (_mtx_obtain_lock_fetch(m, &v, tid))
416b247fd39SMateusz Guzik 			break;
417b247fd39SMateusz Guzik 		if (v == MTX_UNOWNED)
418b247fd39SMateusz Guzik 			continue;
4195c5df0d9SMateusz Guzik 		if (v == tid &&
4205c5df0d9SMateusz Guzik 		    ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
421ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0)) {
422eac09796SJohn Baldwin 			m->mtx_recurse++;
423eac09796SJohn Baldwin 			atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
4245c5df0d9SMateusz Guzik 			recursed = true;
425b247fd39SMateusz Guzik 			break;
4265c5df0d9SMateusz Guzik 		}
427b247fd39SMateusz Guzik 		rval = 0;
428b247fd39SMateusz Guzik 		break;
4295c5df0d9SMateusz Guzik 	}
4305c5df0d9SMateusz Guzik 
431ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
4329ed346baSBosko Milekic 
433aa89d8cdSJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
434764e4d54SJohn Baldwin 	if (rval) {
435aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4362d96f0b1SJohn Baldwin 		    file, line);
437ce1c953eSMark Johnston 		TD_LOCKS_INC(curthread);
4385c5df0d9SMateusz Guzik 		if (!recursed)
43932cd0147SMark Johnston 			LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
440a5aedd68SStacey Son 			    m, contested, waittime, file, line);
441764e4d54SJohn Baldwin 	}
4429ed346baSBosko Milekic 
44319284646SJohn Baldwin 	return (rval);
4440cde2e34SJason Evans }
4450cde2e34SJason Evans 
4460cde2e34SJason Evans /*
4477f44c618SAttilio Rao  * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4489ed346baSBosko Milekic  *
4499ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4509ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4510cde2e34SJason Evans  */
45209f1319aSMateusz Guzik #if LOCK_DEBUG > 0
4530cde2e34SJason Evans void
4542f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
4552f1ddb89SMateusz Guzik     int line)
45609f1319aSMateusz Guzik #else
45709f1319aSMateusz Guzik void
4582f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
45909f1319aSMateusz Guzik #endif
46036412d79SJohn Baldwin {
4612f1ddb89SMateusz Guzik 	struct thread *td;
4627f44c618SAttilio Rao 	struct mtx *m;
4632502c107SJeff Roberson 	struct turnstile *ts;
4642f1ddb89SMateusz Guzik 	uintptr_t tid;
465cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES
466*2ccee9ccSMateusz Guzik 	struct thread *owner;
4672498cf8cSJohn Baldwin #endif
46802bd1bcdSIan Dowse #ifdef KTR
46902bd1bcdSIan Dowse 	int cont_logged = 0;
47002bd1bcdSIan Dowse #endif
4711723a064SJeff Roberson #ifdef LOCK_PROFILING
47270fe8436SKip Macy 	int contested = 0;
47370fe8436SKip Macy 	uint64_t waittime = 0;
4741723a064SJeff Roberson #endif
4751ada9041SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
4761ada9041SMateusz Guzik 	struct lock_delay_arg lda;
4771ada9041SMateusz Guzik #endif
478a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
47961852185SMateusz Guzik 	u_int sleep_cnt = 0;
480a5aedd68SStacey Son 	int64_t sleep_time = 0;
481076dd8ebSAndriy Gapon 	int64_t all_time = 0;
482dfaa7859SMateusz Guzik #endif
483dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
484dfaa7859SMateusz Guzik 	int doing_lockprof;
485a5aedd68SStacey Son #endif
4862f1ddb89SMateusz Guzik 	td = curthread;
4872f1ddb89SMateusz Guzik 	tid = (uintptr_t)td;
4882f1ddb89SMateusz Guzik 	if (SCHEDULER_STOPPED_TD(td))
48935370593SAndriy Gapon 		return;
49035370593SAndriy Gapon 
491fa5000a4SMateusz Guzik #if defined(ADAPTIVE_MUTEXES)
4921ada9041SMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_delay);
493fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS)
494fa5000a4SMateusz Guzik 	lock_delay_arg_init(&lda, NULL);
4951ada9041SMateusz Guzik #endif
4967f44c618SAttilio Rao 	m = mtxlock2mtx(c);
497c1aaf63cSMateusz Guzik 	if (__predict_false(v == MTX_UNOWNED))
498c1aaf63cSMateusz Guzik 		v = MTX_READ_VALUE(m);
4997f44c618SAttilio Rao 
5002f1ddb89SMateusz Guzik 	if (__predict_false(lv_mtx_owner(v) == td)) {
501ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
502ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
503eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
504aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, file, line));
505a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0
506ac6b769bSAttilio Rao 		opts &= ~MTX_RECURSE;
507a24c8eb8SMateusz Guzik #endif
50836412d79SJohn Baldwin 		m->mtx_recurse++;
50908812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
510aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
5115746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
51236412d79SJohn Baldwin 		return;
51336412d79SJohn Baldwin 	}
514a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0
515ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
516a24c8eb8SMateusz Guzik #endif
5179ed346baSBosko Milekic 
518f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
519f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
520f5f9340bSFabien Thomas #endif
52170fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object,
52270fe8436SKip Macy 		    &contested, &waittime);
523aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
52415ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
52515ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
526aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
52709f1319aSMateusz Guzik #ifdef LOCK_PROFILING
528dfaa7859SMateusz Guzik 	doing_lockprof = 1;
529df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS)
530dfaa7859SMateusz Guzik 	doing_lockprof = lockstat_enabled;
531dfaa7859SMateusz Guzik 	if (__predict_false(doing_lockprof))
532e2b25737SMark Johnston 		all_time -= lockstat_nsecs(&m->lock_object);
533076dd8ebSAndriy Gapon #endif
5341bd0eefbSJohn Baldwin 
535fc4f686dSMateusz Guzik 	for (;;) {
5362604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
53790836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
538fc4f686dSMateusz Guzik 				break;
5392604eb9eSMateusz Guzik 			continue;
5402604eb9eSMateusz Guzik 		}
541a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
5421ada9041SMateusz Guzik 		lda.spin_cnt++;
543a5aedd68SStacey Son #endif
54449aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
54549aead8aSAttilio Rao 		/*
54649aead8aSAttilio Rao 		 * If the owner is running on another CPU, spin until the
54749aead8aSAttilio Rao 		 * owner stops running or the state of the lock changes.
54849aead8aSAttilio Rao 		 */
5492604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
55049aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
55149aead8aSAttilio Rao 			if (LOCK_LOG_TEST(&m->lock_object, 0))
55249aead8aSAttilio Rao 				CTR3(KTR_LOCK,
55349aead8aSAttilio Rao 				    "%s: spinning on %p held by %p",
55449aead8aSAttilio Rao 				    __func__, m, owner);
5552cba8dd3SJohn Baldwin 			KTR_STATE1(KTR_SCHED, "thread",
5562cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
5572cba8dd3SJohn Baldwin 			    "spinning", "lockname:\"%s\"",
5582cba8dd3SJohn Baldwin 			    m->lock_object.lo_name);
5592604eb9eSMateusz Guzik 			do {
5601ada9041SMateusz Guzik 				lock_delay(&lda);
561391df78aSMateusz Guzik 				v = MTX_READ_VALUE(m);
5622604eb9eSMateusz Guzik 				owner = lv_mtx_owner(v);
5632604eb9eSMateusz Guzik 			} while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
5642cba8dd3SJohn Baldwin 			KTR_STATE0(KTR_SCHED, "thread",
5652cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
5662cba8dd3SJohn Baldwin 			    "running");
56749aead8aSAttilio Rao 			continue;
56849aead8aSAttilio Rao 		}
56949aead8aSAttilio Rao #endif
57049aead8aSAttilio Rao 
5712502c107SJeff Roberson 		ts = turnstile_trywait(&m->lock_object);
5722604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
5735fa8dd90SJohn Baldwin 
57436412d79SJohn Baldwin 		/*
5759ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
576961a7b24SJohn Baldwin 		 * the turnstile chain lock.
57736412d79SJohn Baldwin 		 */
5785fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
5792502c107SJeff Roberson 			turnstile_cancel(ts);
58036412d79SJohn Baldwin 			continue;
58136412d79SJohn Baldwin 		}
5829ed346baSBosko Milekic 
58349aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
58449aead8aSAttilio Rao 		/*
585fa29f023SJohn Baldwin 		 * The current lock owner might have started executing
586fa29f023SJohn Baldwin 		 * on another CPU (or the lock could have changed
587fa29f023SJohn Baldwin 		 * owners) while we were waiting on the turnstile
588fa29f023SJohn Baldwin 		 * chain lock.  If so, drop the turnstile lock and try
589fa29f023SJohn Baldwin 		 * again.
59049aead8aSAttilio Rao 		 */
5912604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
59249aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
59349aead8aSAttilio Rao 			turnstile_cancel(ts);
59449aead8aSAttilio Rao 			continue;
59549aead8aSAttilio Rao 		}
59649aead8aSAttilio Rao #endif
59749aead8aSAttilio Rao 
59836412d79SJohn Baldwin 		/*
5999ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
6009ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
6019ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
60236412d79SJohn Baldwin 		 */
60336412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
604122eceefSJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
6052502c107SJeff Roberson 			turnstile_cancel(ts);
6062604eb9eSMateusz Guzik 			v = MTX_READ_VALUE(m);
60736412d79SJohn Baldwin 			continue;
60836412d79SJohn Baldwin 		}
60936412d79SJohn Baldwin 
6109ed346baSBosko Milekic 		/*
6117feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
6129ed346baSBosko Milekic 		 */
61336412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
61436412d79SJohn Baldwin 
61502bd1bcdSIan Dowse #ifdef KTR
61602bd1bcdSIan Dowse 		if (!cont_logged) {
61702bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
61802bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
619aa89d8cdSJohn Baldwin 			    (void *)tid, file, line, m->lock_object.lo_name,
620aa89d8cdSJohn Baldwin 			    WITNESS_FILE(&m->lock_object),
621aa89d8cdSJohn Baldwin 			    WITNESS_LINE(&m->lock_object));
62202bd1bcdSIan Dowse 			cont_logged = 1;
62302bd1bcdSIan Dowse 		}
62402bd1bcdSIan Dowse #endif
62536412d79SJohn Baldwin 
6269ed346baSBosko Milekic 		/*
627961a7b24SJohn Baldwin 		 * Block on the turnstile.
6289ed346baSBosko Milekic 		 */
629a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
630e2b25737SMark Johnston 		sleep_time -= lockstat_nsecs(&m->lock_object);
631a5aedd68SStacey Son #endif
6328448e020SMateusz Guzik 		MPASS(owner == mtx_owner(m));
6338448e020SMateusz Guzik 		turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
634a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
635e2b25737SMark Johnston 		sleep_time += lockstat_nsecs(&m->lock_object);
636a5aedd68SStacey Son 		sleep_cnt++;
637a5aedd68SStacey Son #endif
6382604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
63936412d79SJohn Baldwin 	}
64002bd1bcdSIan Dowse #ifdef KTR
64102bd1bcdSIan Dowse 	if (cont_logged) {
64202bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
64302bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
644aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)tid, file, line);
64502bd1bcdSIan Dowse 	}
64602bd1bcdSIan Dowse #endif
647dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
648dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
6497640beb9SMateusz Guzik 		return;
650dfaa7859SMateusz Guzik #endif
651dfaa7859SMateusz Guzik #ifdef KDTRACE_HOOKS
6527640beb9SMateusz Guzik 	all_time += lockstat_nsecs(&m->lock_object);
6537640beb9SMateusz Guzik #endif
65432cd0147SMark Johnston 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
655eea4f254SJeff Roberson 	    waittime, file, line);
656a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
657a5aedd68SStacey Son 	if (sleep_time)
65832cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
659a5aedd68SStacey Son 
660a5aedd68SStacey Son 	/*
661a5aedd68SStacey Son 	 * Only record the loops spinning and not sleeping.
662a5aedd68SStacey Son 	 */
6631ada9041SMateusz Guzik 	if (lda.spin_cnt > sleep_cnt)
66432cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
665a5aedd68SStacey Son #endif
6669ed346baSBosko Milekic }
6679ed346baSBosko Milekic 
6682502c107SJeff Roberson static void
6692502c107SJeff Roberson _mtx_lock_spin_failed(struct mtx *m)
6702502c107SJeff Roberson {
6712502c107SJeff Roberson 	struct thread *td;
6722502c107SJeff Roberson 
6732502c107SJeff Roberson 	td = mtx_owner(m);
6742502c107SJeff Roberson 
6752502c107SJeff Roberson 	/* If the mutex is unlocked, try again. */
6762502c107SJeff Roberson 	if (td == NULL)
6772502c107SJeff Roberson 		return;
678b95b98b0SKonstantin Belousov 
6792502c107SJeff Roberson 	printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
6802502c107SJeff Roberson 	    m, m->lock_object.lo_name, td, td->td_tid);
6812502c107SJeff Roberson #ifdef WITNESS
68298332c8cSAttilio Rao 	witness_display_spinlock(&m->lock_object, td, printf);
6832502c107SJeff Roberson #endif
6842502c107SJeff Roberson 	panic("spin lock held too long");
6852502c107SJeff Roberson }
6862502c107SJeff Roberson 
687b95b98b0SKonstantin Belousov #ifdef SMP
6889ed346baSBosko Milekic /*
6897f44c618SAttilio Rao  * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
6909ed346baSBosko Milekic  *
6919ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
6929ed346baSBosko Milekic  * is handled inline.
6939ed346baSBosko Milekic  */
6940d74fe26SMateusz Guzik #if LOCK_DEBUG > 0
6959ed346baSBosko Milekic void
6960d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
6970d74fe26SMateusz Guzik     const char *file, int line)
6980d74fe26SMateusz Guzik #else
6990d74fe26SMateusz Guzik void
7000d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
7010d74fe26SMateusz Guzik #endif
70236412d79SJohn Baldwin {
7037f44c618SAttilio Rao 	struct mtx *m;
704a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
7050d74fe26SMateusz Guzik 	uintptr_t tid;
7061723a064SJeff Roberson #ifdef LOCK_PROFILING
7071723a064SJeff Roberson 	int contested = 0;
70870fe8436SKip Macy 	uint64_t waittime = 0;
7091723a064SJeff Roberson #endif
710076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
711076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
712076dd8ebSAndriy Gapon #endif
713dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
714dfaa7859SMateusz Guzik 	int doing_lockprof;
715dfaa7859SMateusz Guzik #endif
71636412d79SJohn Baldwin 
7170d74fe26SMateusz Guzik 	tid = (uintptr_t)curthread;
7187f44c618SAttilio Rao 	m = mtxlock2mtx(c);
7197f44c618SAttilio Rao 
72013d2ef0fSMateusz Guzik 	if (__predict_false(v == MTX_UNOWNED))
72113d2ef0fSMateusz Guzik 		v = MTX_READ_VALUE(m);
72213d2ef0fSMateusz Guzik 
72313d2ef0fSMateusz Guzik 	if (__predict_false(v == tid)) {
72413d2ef0fSMateusz Guzik 		m->mtx_recurse++;
72513d2ef0fSMateusz Guzik 		return;
72613d2ef0fSMateusz Guzik 	}
72713d2ef0fSMateusz Guzik 
7280d74fe26SMateusz Guzik 	if (SCHEDULER_STOPPED())
7290d74fe26SMateusz Guzik 		return;
7300d74fe26SMateusz Guzik 
7310d74fe26SMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
7320d74fe26SMateusz Guzik 
733aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
7345746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
7352cba8dd3SJohn Baldwin 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
7362cba8dd3SJohn Baldwin 	    "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
7379ed346baSBosko Milekic 
738f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
739f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
740f5f9340bSFabien Thomas #endif
74170fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
742dfaa7859SMateusz Guzik #ifdef LOCK_PROFILING
743dfaa7859SMateusz Guzik 	doing_lockprof = 1;
744df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS)
745dfaa7859SMateusz Guzik 	doing_lockprof = lockstat_enabled;
746dfaa7859SMateusz Guzik 	if (__predict_false(doing_lockprof))
747e2b25737SMark Johnston 		spin_time -= lockstat_nsecs(&m->lock_object);
748076dd8ebSAndriy Gapon #endif
749fc4f686dSMateusz Guzik 	for (;;) {
7502604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
75190836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
752fc4f686dSMateusz Guzik 				break;
75336412d79SJohn Baldwin 			continue;
754703fc290SJohn Baldwin 		}
7552604eb9eSMateusz Guzik 		/* Give interrupts a chance while we spin. */
7562604eb9eSMateusz Guzik 		spinlock_exit();
7572604eb9eSMateusz Guzik 		do {
7582604eb9eSMateusz Guzik 			if (lda.spin_cnt < 10000000) {
7592604eb9eSMateusz Guzik 				lock_delay(&lda);
7602604eb9eSMateusz Guzik 			} else {
761a0d45f0fSMateusz Guzik 				lda.spin_cnt++;
762a0d45f0fSMateusz Guzik 				if (lda.spin_cnt < 60000000 || kdb_active ||
763a0d45f0fSMateusz Guzik 				    panicstr != NULL)
76436412d79SJohn Baldwin 					DELAY(1);
7652502c107SJeff Roberson 				else
7662502c107SJeff Roberson 					_mtx_lock_spin_failed(m);
7679f1b87f1SMaxime Henrion 				cpu_spinwait();
76836412d79SJohn Baldwin 			}
7692604eb9eSMateusz Guzik 			v = MTX_READ_VALUE(m);
7702604eb9eSMateusz Guzik 		} while (v != MTX_UNOWNED);
771c6a37e84SJohn Baldwin 		spinlock_enter();
77236412d79SJohn Baldwin 	}
77336412d79SJohn Baldwin 
774aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
7759ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
7762cba8dd3SJohn Baldwin 	KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
7772cba8dd3SJohn Baldwin 	    "running");
7789ed346baSBosko Milekic 
779dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
780dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
781dfaa7859SMateusz Guzik 		return;
782dfaa7859SMateusz Guzik #endif
783c6d48c87SMark Johnston #ifdef KDTRACE_HOOKS
784dfaa7859SMateusz Guzik 	spin_time += lockstat_nsecs(&m->lock_object);
785dfaa7859SMateusz Guzik #endif
78632cd0147SMark Johnston 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
78732cd0147SMark Johnston 	    contested, waittime, file, line);
788dfaa7859SMateusz Guzik #ifdef KDTRACE_HOOKS
789d0f68f91SMark Johnston 	if (lda.spin_cnt != 0)
79032cd0147SMark Johnston 		LOCKSTAT_RECORD1(spin__spin, m, spin_time);
791c6d48c87SMark Johnston #endif
79236412d79SJohn Baldwin }
79333fb8a38SJohn Baldwin #endif /* SMP */
79436412d79SJohn Baldwin 
795be49509eSMateusz Guzik #ifdef INVARIANTS
796be49509eSMateusz Guzik static void
797be49509eSMateusz Guzik thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
798be49509eSMateusz Guzik {
799be49509eSMateusz Guzik 
800be49509eSMateusz Guzik 	KASSERT(m->mtx_lock != MTX_DESTROYED,
801be49509eSMateusz Guzik 	    ("thread_lock() of destroyed mutex @ %s:%d", file, line));
802be49509eSMateusz Guzik 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
803be49509eSMateusz Guzik 	    ("thread_lock() of sleep mutex %s @ %s:%d",
804be49509eSMateusz Guzik 	    m->lock_object.lo_name, file, line));
805be49509eSMateusz Guzik 	if (mtx_owned(m))
806be49509eSMateusz Guzik 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
807be49509eSMateusz Guzik 		    ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
808be49509eSMateusz Guzik 		    m->lock_object.lo_name, file, line));
809be49509eSMateusz Guzik 	WITNESS_CHECKORDER(&m->lock_object,
810be49509eSMateusz Guzik 	    opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
811be49509eSMateusz Guzik }
812be49509eSMateusz Guzik #else
813be49509eSMateusz Guzik #define thread_lock_validate(m, opts, file, line) do { } while (0)
814be49509eSMateusz Guzik #endif
815be49509eSMateusz Guzik 
816be49509eSMateusz Guzik #ifndef LOCK_PROFILING
817be49509eSMateusz Guzik #if LOCK_DEBUG > 0
818be49509eSMateusz Guzik void
819be49509eSMateusz Guzik _thread_lock(struct thread *td, int opts, const char *file, int line)
820be49509eSMateusz Guzik #else
821be49509eSMateusz Guzik void
822be49509eSMateusz Guzik _thread_lock(struct thread *td)
823be49509eSMateusz Guzik #endif
824be49509eSMateusz Guzik {
825be49509eSMateusz Guzik 	struct mtx *m;
826be49509eSMateusz Guzik 	uintptr_t tid, v;
827be49509eSMateusz Guzik 
828be49509eSMateusz Guzik 	tid = (uintptr_t)curthread;
829be49509eSMateusz Guzik 
830be49509eSMateusz Guzik 	spinlock_enter();
831be49509eSMateusz Guzik 	m = td->td_lock;
832be49509eSMateusz Guzik 	thread_lock_validate(m, 0, file, line);
833be49509eSMateusz Guzik 	v = MTX_READ_VALUE(m);
834be49509eSMateusz Guzik 	if (__predict_true(v == MTX_UNOWNED)) {
835be49509eSMateusz Guzik 		if (__predict_false(!_mtx_obtain_lock(m, tid)))
836be49509eSMateusz Guzik 			goto slowpath_unlocked;
837be49509eSMateusz Guzik 	} else if (v == tid) {
838be49509eSMateusz Guzik 		m->mtx_recurse++;
839be49509eSMateusz Guzik 	} else
840be49509eSMateusz Guzik 		goto slowpath_unlocked;
841be49509eSMateusz Guzik 	if (__predict_true(m == td->td_lock)) {
842be49509eSMateusz Guzik 		WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
843be49509eSMateusz Guzik 		return;
844be49509eSMateusz Guzik 	}
845be49509eSMateusz Guzik 	if (m->mtx_recurse != 0)
846be49509eSMateusz Guzik 		m->mtx_recurse--;
847be49509eSMateusz Guzik 	else
848be49509eSMateusz Guzik 		_mtx_release_lock_quick(m);
849be49509eSMateusz Guzik slowpath_unlocked:
850be49509eSMateusz Guzik 	spinlock_exit();
851be49509eSMateusz Guzik 	thread_lock_flags_(td, 0, 0, 0);
852be49509eSMateusz Guzik }
853be49509eSMateusz Guzik #endif
854be49509eSMateusz Guzik 
8552502c107SJeff Roberson void
856ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
8572502c107SJeff Roberson {
8582502c107SJeff Roberson 	struct mtx *m;
8595e5ad162SMateusz Guzik 	uintptr_t tid, v;
860a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
8611723a064SJeff Roberson #ifdef LOCK_PROFILING
8621723a064SJeff Roberson 	int contested = 0;
8631723a064SJeff Roberson 	uint64_t waittime = 0;
8641723a064SJeff Roberson #endif
865a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
866076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
867a5aedd68SStacey Son #endif
868dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
869dfaa7859SMateusz Guzik 	int doing_lockprof = 1;
870dfaa7859SMateusz Guzik #endif
8712502c107SJeff Roberson 
8722502c107SJeff Roberson 	tid = (uintptr_t)curthread;
87335370593SAndriy Gapon 
874f61d6c5aSMark Johnston 	if (SCHEDULER_STOPPED()) {
875f61d6c5aSMark Johnston 		/*
876f61d6c5aSMark Johnston 		 * Ensure that spinlock sections are balanced even when the
877f61d6c5aSMark Johnston 		 * scheduler is stopped, since we may otherwise inadvertently
878f61d6c5aSMark Johnston 		 * re-enable interrupts while dumping core.
879f61d6c5aSMark Johnston 		 */
880f61d6c5aSMark Johnston 		spinlock_enter();
88135370593SAndriy Gapon 		return;
882f61d6c5aSMark Johnston 	}
88335370593SAndriy Gapon 
884a0d45f0fSMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
885a0d45f0fSMateusz Guzik 
886dfaa7859SMateusz Guzik #ifdef LOCK_PROFILING
887dfaa7859SMateusz Guzik 	doing_lockprof = 1;
888df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS)
889dfaa7859SMateusz Guzik 	doing_lockprof = lockstat_enabled;
890dfaa7859SMateusz Guzik 	if (__predict_false(doing_lockprof))
891e2b25737SMark Johnston 		spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
892076dd8ebSAndriy Gapon #endif
8932502c107SJeff Roberson 	for (;;) {
8942502c107SJeff Roberson retry:
895dc089651SMateusz Guzik 		v = MTX_UNOWNED;
8962502c107SJeff Roberson 		spinlock_enter();
897710eacdcSJeff Roberson 		m = td->td_lock;
898be49509eSMateusz Guzik 		thread_lock_validate(m, opts, file, line);
899fc4f686dSMateusz Guzik 		for (;;) {
90090836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
901fc4f686dSMateusz Guzik 				break;
90290836c32SMateusz Guzik 			if (v == MTX_UNOWNED)
9035e5ad162SMateusz Guzik 				continue;
9045e5ad162SMateusz Guzik 			if (v == tid) {
9052502c107SJeff Roberson 				m->mtx_recurse++;
9062502c107SJeff Roberson 				break;
9072502c107SJeff Roberson 			}
908f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
909f5f9340bSFabien Thomas 			PMC_SOFT_CALL( , , lock, failed);
910f5f9340bSFabien Thomas #endif
911eea4f254SJeff Roberson 			lock_profile_obtain_lock_failed(&m->lock_object,
912eea4f254SJeff Roberson 			    &contested, &waittime);
9132502c107SJeff Roberson 			/* Give interrupts a chance while we spin. */
9142502c107SJeff Roberson 			spinlock_exit();
9155e5ad162SMateusz Guzik 			do {
916a0d45f0fSMateusz Guzik 				if (lda.spin_cnt < 10000000) {
917a0d45f0fSMateusz Guzik 					lock_delay(&lda);
918a0d45f0fSMateusz Guzik 				} else {
919a0d45f0fSMateusz Guzik 					lda.spin_cnt++;
920a0d45f0fSMateusz Guzik 					if (lda.spin_cnt < 60000000 ||
9212502c107SJeff Roberson 					    kdb_active || panicstr != NULL)
9222502c107SJeff Roberson 						DELAY(1);
9232502c107SJeff Roberson 					else
9242502c107SJeff Roberson 						_mtx_lock_spin_failed(m);
9252502c107SJeff Roberson 					cpu_spinwait();
926a0d45f0fSMateusz Guzik 				}
9272502c107SJeff Roberson 				if (m != td->td_lock)
9282502c107SJeff Roberson 					goto retry;
9295e5ad162SMateusz Guzik 				v = MTX_READ_VALUE(m);
9305e5ad162SMateusz Guzik 			} while (v != MTX_UNOWNED);
9312502c107SJeff Roberson 			spinlock_enter();
9322502c107SJeff Roberson 		}
9332502c107SJeff Roberson 		if (m == td->td_lock)
9342502c107SJeff Roberson 			break;
935961135eaSJohn Baldwin 		__mtx_unlock_spin(m);	/* does spinlock_exit() */
9362502c107SJeff Roberson 	}
937dfaa7859SMateusz Guzik 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
938dfaa7859SMateusz Guzik 	    line);
939dfaa7859SMateusz Guzik 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
940dfaa7859SMateusz Guzik 
941dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
942dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
943dfaa7859SMateusz Guzik 		return;
944dfaa7859SMateusz Guzik #endif
945076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
946e2b25737SMark Johnston 	spin_time += lockstat_nsecs(&m->lock_object);
947076dd8ebSAndriy Gapon #endif
948eea4f254SJeff Roberson 	if (m->mtx_recurse == 0)
94932cd0147SMark Johnston 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
95032cd0147SMark Johnston 		    contested, waittime, file, line);
9515002e195SMark Johnston #ifdef KDTRACE_HOOKS
952d0f68f91SMark Johnston 	if (lda.spin_cnt != 0)
95332cd0147SMark Johnston 		LOCKSTAT_RECORD1(thread__spin, m, spin_time);
9545002e195SMark Johnston #endif
9552502c107SJeff Roberson }
9562502c107SJeff Roberson 
9572502c107SJeff Roberson struct mtx *
9582502c107SJeff Roberson thread_lock_block(struct thread *td)
9592502c107SJeff Roberson {
9602502c107SJeff Roberson 	struct mtx *lock;
9612502c107SJeff Roberson 
9622502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
963710eacdcSJeff Roberson 	lock = td->td_lock;
9642502c107SJeff Roberson 	td->td_lock = &blocked_lock;
9652502c107SJeff Roberson 	mtx_unlock_spin(lock);
9662502c107SJeff Roberson 
9672502c107SJeff Roberson 	return (lock);
9682502c107SJeff Roberson }
9692502c107SJeff Roberson 
9702502c107SJeff Roberson void
9712502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new)
9722502c107SJeff Roberson {
9732502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
9742502c107SJeff Roberson 	MPASS(td->td_lock == &blocked_lock);
97565d32cd8SMatt Jacob 	atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
9762502c107SJeff Roberson }
9772502c107SJeff Roberson 
9782502c107SJeff Roberson void
9792502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new)
9802502c107SJeff Roberson {
9812502c107SJeff Roberson 	struct mtx *lock;
9822502c107SJeff Roberson 
9832502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
9842502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
985710eacdcSJeff Roberson 	lock = td->td_lock;
9862502c107SJeff Roberson 	td->td_lock = new;
9872502c107SJeff Roberson 	mtx_unlock_spin(lock);
9882502c107SJeff Roberson }
9892502c107SJeff Roberson 
9909ed346baSBosko Milekic /*
9917f44c618SAttilio Rao  * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
9929ed346baSBosko Milekic  *
9933b3cf014SMateusz Guzik  * We are only called here if the lock is recursed, contested (i.e. we
9943b3cf014SMateusz Guzik  * need to wake up a blocked thread) or lockstat probe is active.
9959ed346baSBosko Milekic  */
99609f1319aSMateusz Guzik #if LOCK_DEBUG > 0
99736412d79SJohn Baldwin void
9987f44c618SAttilio Rao __mtx_unlock_sleep(volatile uintptr_t *c, int opts, const char *file, int line)
99909f1319aSMateusz Guzik #else
100009f1319aSMateusz Guzik void
1001a24c8eb8SMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c)
100209f1319aSMateusz Guzik #endif
100336412d79SJohn Baldwin {
10047f44c618SAttilio Rao 	struct mtx *m;
1005961a7b24SJohn Baldwin 	struct turnstile *ts;
1006*2ccee9ccSMateusz Guzik 	uintptr_t tid, v;
10079ed346baSBosko Milekic 
100835370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
100935370593SAndriy Gapon 		return;
101035370593SAndriy Gapon 
10113b3cf014SMateusz Guzik 	tid = (uintptr_t)curthread;
10127f44c618SAttilio Rao 	m = mtxlock2mtx(c);
10133b3cf014SMateusz Guzik 	v = MTX_READ_VALUE(m);
101490836c32SMateusz Guzik 
10153b3cf014SMateusz Guzik 	if (v & MTX_RECURSED) {
101636412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
101708812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1018aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
10199ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
102036412d79SJohn Baldwin 		return;
102136412d79SJohn Baldwin 	}
10229ed346baSBosko Milekic 
10233b3cf014SMateusz Guzik 	LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
10243b3cf014SMateusz Guzik 	if (v == tid && _mtx_release_lock(m, tid))
10253b3cf014SMateusz Guzik 		return;
10263b3cf014SMateusz Guzik 
10272502c107SJeff Roberson 	/*
10282502c107SJeff Roberson 	 * We have to lock the chain before the turnstile so this turnstile
10292502c107SJeff Roberson 	 * can be removed from the hash list if it is empty.
10302502c107SJeff Roberson 	 */
10312502c107SJeff Roberson 	turnstile_chain_lock(&m->lock_object);
10328448e020SMateusz Guzik 	_mtx_release_lock_quick(m);
1033aa89d8cdSJohn Baldwin 	ts = turnstile_lookup(&m->lock_object);
10348448e020SMateusz Guzik 	MPASS(ts != NULL);
1035aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
10369ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
10377aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1038bf9c6c31SJohn Baldwin 
10392502c107SJeff Roberson 	/*
10402502c107SJeff Roberson 	 * This turnstile is now no longer associated with the mutex.  We can
10412502c107SJeff Roberson 	 * unlock the chain lock so a new turnstile may take it's place.
10422502c107SJeff Roberson 	 */
10437aa4f685SJohn Baldwin 	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
10442502c107SJeff Roberson 	turnstile_chain_unlock(&m->lock_object);
10459ed346baSBosko Milekic }
10469ed346baSBosko Milekic 
10479ed346baSBosko Milekic /*
10489ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
1049961135eaSJohn Baldwin  * See the __mtx_unlock_spin() macro for the details.
10509ed346baSBosko Milekic  */
10519ed346baSBosko Milekic 
10529ed346baSBosko Milekic /*
105315ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
10549ed346baSBosko Milekic  */
10551103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
10560cde2e34SJason Evans void
10577f44c618SAttilio Rao __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
10580cde2e34SJason Evans {
10597f44c618SAttilio Rao 	const struct mtx *m;
10605cb0fbe4SJohn Baldwin 
1061310ab671SEric van Gyzen 	if (panicstr != NULL || dumping || SCHEDULER_STOPPED())
10625cb0fbe4SJohn Baldwin 		return;
10637f44c618SAttilio Rao 
10647f44c618SAttilio Rao 	m = mtxlock2mtx(c);
10657f44c618SAttilio Rao 
1066a10f4966SJake Burkholder 	switch (what) {
10670cde2e34SJason Evans 	case MA_OWNED:
10680cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
10690cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
1070a10f4966SJake Burkholder 		if (!mtx_owned(m))
10710cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
1072aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
1073a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
1074a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
10750cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
1076aa89d8cdSJohn Baldwin 				    m->lock_object.lo_name, file, line);
1077a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
10780cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
1079aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
10800cde2e34SJason Evans 		}
10810cde2e34SJason Evans 		break;
10820cde2e34SJason Evans 	case MA_NOTOWNED:
1083a10f4966SJake Burkholder 		if (mtx_owned(m))
10840cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
1085aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
10860cde2e34SJason Evans 		break;
10870cde2e34SJason Evans 	default:
108856771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
10890cde2e34SJason Evans 	}
10900cde2e34SJason Evans }
10910cde2e34SJason Evans #endif
10920cde2e34SJason Evans 
10939ed346baSBosko Milekic /*
1094c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
1095c27b5699SAndrew R. Reiter  */
1096c27b5699SAndrew R. Reiter void
1097c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
1098c27b5699SAndrew R. Reiter {
1099c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
1100c27b5699SAndrew R. Reiter 
11017f44c618SAttilio Rao 	mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
11027f44c618SAttilio Rao 	    margs->ma_opts);
1103c27b5699SAndrew R. Reiter }
1104c27b5699SAndrew R. Reiter 
1105c27b5699SAndrew R. Reiter /*
11069ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
11070c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
11080c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
11090c88508aSJohn Baldwin  * witness.
11109ed346baSBosko Milekic  */
111136412d79SJohn Baldwin void
11127f44c618SAttilio Rao _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
111336412d79SJohn Baldwin {
11147f44c618SAttilio Rao 	struct mtx *m;
111583a81bcbSJohn Baldwin 	struct lock_class *class;
111683a81bcbSJohn Baldwin 	int flags;
11179ed346baSBosko Milekic 
11187f44c618SAttilio Rao 	m = mtxlock2mtx(c);
11197f44c618SAttilio Rao 
112019284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1121fd07ddcfSDmitry Chagin 	    MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1122353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1123353998acSAttilio Rao 	    ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1124353998acSAttilio Rao 	    &m->mtx_lock));
11259ed346baSBosko Milekic 
112683a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
112719284646SJohn Baldwin 	if (opts & MTX_SPIN)
112883a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
112919284646SJohn Baldwin 	else
113083a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
113183a81bcbSJohn Baldwin 	flags = 0;
113219284646SJohn Baldwin 	if (opts & MTX_QUIET)
113383a81bcbSJohn Baldwin 		flags |= LO_QUIET;
113419284646SJohn Baldwin 	if (opts & MTX_RECURSE)
113583a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
113619284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
113783a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
1138f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
113983a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
11407c0435b9SKip Macy 	if (opts & MTX_NOPROFILE)
11417c0435b9SKip Macy 		flags |= LO_NOPROFILE;
1142fd07ddcfSDmitry Chagin 	if (opts & MTX_NEW)
1143fd07ddcfSDmitry Chagin 		flags |= LO_NEW;
114419284646SJohn Baldwin 
114583a81bcbSJohn Baldwin 	/* Initialize mutex. */
1146b5fb43e5SJohn Baldwin 	lock_init(&m->lock_object, class, name, type, flags);
1147b5fb43e5SJohn Baldwin 
114819284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
114983a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
115036412d79SJohn Baldwin }
115136412d79SJohn Baldwin 
11529ed346baSBosko Milekic /*
115319284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
115419284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
115519284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
115619284646SJohn Baldwin  * flags.
11579ed346baSBosko Milekic  */
115836412d79SJohn Baldwin void
11597f44c618SAttilio Rao _mtx_destroy(volatile uintptr_t *c)
116036412d79SJohn Baldwin {
11617f44c618SAttilio Rao 	struct mtx *m;
11627f44c618SAttilio Rao 
11637f44c618SAttilio Rao 	m = mtxlock2mtx(c);
116436412d79SJohn Baldwin 
116519284646SJohn Baldwin 	if (!mtx_owned(m))
116619284646SJohn Baldwin 		MPASS(mtx_unowned(m));
116719284646SJohn Baldwin 	else {
116808812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
11699ed346baSBosko Milekic 
1170861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
1171aa89d8cdSJohn Baldwin 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1172861a2308SScott Long 			spinlock_exit();
1173764e4d54SJohn Baldwin 		else
1174ce1c953eSMark Johnston 			TD_LOCKS_DEC(curthread);
1175861a2308SScott Long 
1176d3df4af3SJeff Roberson 		lock_profile_release_lock(&m->lock_object);
117719284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
1178aa89d8cdSJohn Baldwin 		WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1179c86b6ff5SJohn Baldwin 		    __LINE__);
118036412d79SJohn Baldwin 	}
11810384fff8SJason Evans 
1182186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
1183aa89d8cdSJohn Baldwin 	lock_destroy(&m->lock_object);
11840384fff8SJason Evans }
1185d23f5958SMatthew Dillon 
1186d23f5958SMatthew Dillon /*
1187c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
1188c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
1189c53c013bSJohn Baldwin  * setup before this is called.
1190c53c013bSJohn Baldwin  */
1191c53c013bSJohn Baldwin void
1192c53c013bSJohn Baldwin mutex_init(void)
1193c53c013bSJohn Baldwin {
1194c53c013bSJohn Baldwin 
1195961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
1196961a7b24SJohn Baldwin 	init_turnstiles();
1197961a7b24SJohn Baldwin 
1198c53c013bSJohn Baldwin 	/*
1199c53c013bSJohn Baldwin 	 * Initialize mutexes.
1200c53c013bSJohn Baldwin 	 */
12010c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
12022502c107SJeff Roberson 	mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
12032502c107SJeff Roberson 	blocked_lock.mtx_lock = 0xdeadc0de;	/* Always blocked. */
12040c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
12056afb32fcSKonstantin Belousov 	mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
12065c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
12075c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
12085c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
12098c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1210c53c013bSJohn Baldwin 	mtx_lock(&Giant);
1211c53c013bSJohn Baldwin }
1212d272fe53SJohn Baldwin 
1213d272fe53SJohn Baldwin #ifdef DDB
1214d272fe53SJohn Baldwin void
1215d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock)
1216d272fe53SJohn Baldwin {
1217d272fe53SJohn Baldwin 	struct thread *td;
1218d576deedSPawel Jakub Dawidek 	const struct mtx *m;
1219d272fe53SJohn Baldwin 
1220d576deedSPawel Jakub Dawidek 	m = (const struct mtx *)lock;
1221d272fe53SJohn Baldwin 
1222d272fe53SJohn Baldwin 	db_printf(" flags: {");
122383a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1224d272fe53SJohn Baldwin 		db_printf("SPIN");
1225d272fe53SJohn Baldwin 	else
1226d272fe53SJohn Baldwin 		db_printf("DEF");
1227aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_RECURSABLE)
1228d272fe53SJohn Baldwin 		db_printf(", RECURSE");
1229aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_DUPOK)
1230d272fe53SJohn Baldwin 		db_printf(", DUPOK");
1231d272fe53SJohn Baldwin 	db_printf("}\n");
1232d272fe53SJohn Baldwin 	db_printf(" state: {");
1233d272fe53SJohn Baldwin 	if (mtx_unowned(m))
1234d272fe53SJohn Baldwin 		db_printf("UNOWNED");
1235c0bfd703SJohn Baldwin 	else if (mtx_destroyed(m))
1236c0bfd703SJohn Baldwin 		db_printf("DESTROYED");
1237d272fe53SJohn Baldwin 	else {
1238d272fe53SJohn Baldwin 		db_printf("OWNED");
1239d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
1240d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
1241d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
1242d272fe53SJohn Baldwin 			db_printf(", RECURSED");
1243d272fe53SJohn Baldwin 	}
1244d272fe53SJohn Baldwin 	db_printf("}\n");
1245c0bfd703SJohn Baldwin 	if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1246d272fe53SJohn Baldwin 		td = mtx_owner(m);
1247d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1248431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
1249d272fe53SJohn Baldwin 		if (mtx_recursed(m))
1250d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
1251d272fe53SJohn Baldwin 	}
1252d272fe53SJohn Baldwin }
1253d272fe53SJohn Baldwin #endif
1254