xref: /freebsd/sys/kern/kern_mutex.c (revision 4730a8972b1f4b67bf9ffde8e63ca906ef4c9563)
10384fff8SJason Evans /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a36da99SPedro F. Giffuni  *
40384fff8SJason Evans  * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
50384fff8SJason Evans  *
60384fff8SJason Evans  * Redistribution and use in source and binary forms, with or without
70384fff8SJason Evans  * modification, are permitted provided that the following conditions
80384fff8SJason Evans  * are met:
90384fff8SJason Evans  * 1. Redistributions of source code must retain the above copyright
100384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer.
110384fff8SJason Evans  * 2. Redistributions in binary form must reproduce the above copyright
120384fff8SJason Evans  *    notice, this list of conditions and the following disclaimer in the
130384fff8SJason Evans  *    documentation and/or other materials provided with the distribution.
140384fff8SJason Evans  * 3. Berkeley Software Design Inc's name may not be used to endorse or
150384fff8SJason Evans  *    promote products derived from this software without specific prior
160384fff8SJason Evans  *    written permission.
170384fff8SJason Evans  *
180384fff8SJason Evans  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
190384fff8SJason Evans  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
200384fff8SJason Evans  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
210384fff8SJason Evans  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
220384fff8SJason Evans  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
230384fff8SJason Evans  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
240384fff8SJason Evans  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
250384fff8SJason Evans  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
260384fff8SJason Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
270384fff8SJason Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
280384fff8SJason Evans  * SUCH DAMAGE.
290384fff8SJason Evans  *
300384fff8SJason Evans  *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
3136412d79SJohn Baldwin  *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
320384fff8SJason Evans  */
330384fff8SJason Evans 
340384fff8SJason Evans /*
35ba48b69aSJohn Baldwin  * Machine independent bits of mutex implementation.
360384fff8SJason Evans  */
370384fff8SJason Evans 
38677b542eSDavid E. O'Brien #include <sys/cdefs.h>
39677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
40677b542eSDavid E. O'Brien 
412498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h"
429c36c934SJohn Baldwin #include "opt_ddb.h"
43f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h"
449923b511SScott Long #include "opt_sched.h"
45a5a96a19SJohn Baldwin 
460384fff8SJason Evans #include <sys/param.h>
476c35e809SDag-Erling Smørgrav #include <sys/systm.h>
4836412d79SJohn Baldwin #include <sys/bus.h>
491126349aSPaul Saab #include <sys/conf.h>
502d50560aSMarcel Moolenaar #include <sys/kdb.h>
5136412d79SJohn Baldwin #include <sys/kernel.h>
526c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
5319284646SJohn Baldwin #include <sys/lock.h>
54fb919e4dSMark Murray #include <sys/malloc.h>
5519284646SJohn Baldwin #include <sys/mutex.h>
560384fff8SJason Evans #include <sys/proc.h>
57c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
58b43179fbSJeff Roberson #include <sys/sched.h>
596c35e809SDag-Erling Smørgrav #include <sys/sbuf.h>
601ada9041SMateusz Guzik #include <sys/smp.h>
61a5a96a19SJohn Baldwin #include <sys/sysctl.h>
62961a7b24SJohn Baldwin #include <sys/turnstile.h>
6336412d79SJohn Baldwin #include <sys/vmmeter.h>
647c0435b9SKip Macy #include <sys/lock_profile.h>
650384fff8SJason Evans 
6636412d79SJohn Baldwin #include <machine/atomic.h>
6736412d79SJohn Baldwin #include <machine/bus.h>
680384fff8SJason Evans #include <machine/cpu.h>
6936412d79SJohn Baldwin 
709c36c934SJohn Baldwin #include <ddb/ddb.h>
719c36c934SJohn Baldwin 
728c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h>
738c4b6380SJohn Baldwin 
7436412d79SJohn Baldwin #include <vm/vm.h>
7536412d79SJohn Baldwin #include <vm/vm_extern.h>
7636412d79SJohn Baldwin 
77cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
78cd6e6e4eSJohn Baldwin #define	ADAPTIVE_MUTEXES
79cd6e6e4eSJohn Baldwin #endif
80cd6e6e4eSJohn Baldwin 
81f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
82f5f9340bSFabien Thomas #include <sys/pmckern.h>
83f5f9340bSFabien Thomas PMC_SOFT_DEFINE( , , lock, failed);
84f5f9340bSFabien Thomas #endif
85f5f9340bSFabien Thomas 
86b9a80acaSStephan Uphoff /*
877f44c618SAttilio Rao  * Return the mutex address when the lock cookie address is provided.
887f44c618SAttilio Rao  * This functionality assumes that struct mtx* have a member named mtx_lock.
897f44c618SAttilio Rao  */
907f44c618SAttilio Rao #define	mtxlock2mtx(c)	(__containerof(c, struct mtx, mtx_lock))
917f44c618SAttilio Rao 
927f44c618SAttilio Rao /*
939ed346baSBosko Milekic  * Internal utility macros.
940cde2e34SJason Evans  */
959ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
960cde2e34SJason Evans 
97c0bfd703SJohn Baldwin #define	mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
98c0bfd703SJohn Baldwin 
99d576deedSPawel Jakub Dawidek static void	assert_mtx(const struct lock_object *lock, int what);
100d272fe53SJohn Baldwin #ifdef DDB
101d576deedSPawel Jakub Dawidek static void	db_show_mtx(const struct lock_object *lock);
102d272fe53SJohn Baldwin #endif
1037faf4d90SDavide Italiano static void	lock_mtx(struct lock_object *lock, uintptr_t how);
1047faf4d90SDavide Italiano static void	lock_spin(struct lock_object *lock, uintptr_t how);
105a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
106d576deedSPawel Jakub Dawidek static int	owner_mtx(const struct lock_object *lock,
107d576deedSPawel Jakub Dawidek 		    struct thread **owner);
108a5aedd68SStacey Son #endif
1097faf4d90SDavide Italiano static uintptr_t unlock_mtx(struct lock_object *lock);
1107faf4d90SDavide Italiano static uintptr_t unlock_spin(struct lock_object *lock);
111d272fe53SJohn Baldwin 
1120cde2e34SJason Evans /*
11319284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
1140cde2e34SJason Evans  */
11519284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
116ae8dde30SJohn Baldwin 	.lc_name = "sleep mutex",
117ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
118f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
119d272fe53SJohn Baldwin #ifdef DDB
120ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
121d272fe53SJohn Baldwin #endif
1226e21afd4SJohn Baldwin 	.lc_lock = lock_mtx,
1236e21afd4SJohn Baldwin 	.lc_unlock = unlock_mtx,
124a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
125a5aedd68SStacey Son 	.lc_owner = owner_mtx,
126a5aedd68SStacey Son #endif
12719284646SJohn Baldwin };
12819284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
129ae8dde30SJohn Baldwin 	.lc_name = "spin mutex",
130ae8dde30SJohn Baldwin 	.lc_flags = LC_SPINLOCK | LC_RECURSABLE,
131f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
132d272fe53SJohn Baldwin #ifdef DDB
133ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
134d272fe53SJohn Baldwin #endif
1356e21afd4SJohn Baldwin 	.lc_lock = lock_spin,
1366e21afd4SJohn Baldwin 	.lc_unlock = unlock_spin,
137a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
138a5aedd68SStacey Son 	.lc_owner = owner_mtx,
139a5aedd68SStacey Son #endif
1408484de75SJohn Baldwin };
1418484de75SJohn Baldwin 
1421ada9041SMateusz Guzik #ifdef ADAPTIVE_MUTEXES
1432e77cad1SMateusz Guzik #ifdef MUTEX_CUSTOM_BACKOFF
1447029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1457029da5cSPawel Biernacki     "mtx debugging");
1461ada9041SMateusz Guzik 
147574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_delay;
1481ada9041SMateusz Guzik 
1496b8dd26eSMateusz Guzik SYSCTL_U16(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
1501ada9041SMateusz Guzik     0, "");
1516b8dd26eSMateusz Guzik SYSCTL_U16(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
1521ada9041SMateusz Guzik     0, "");
1531ada9041SMateusz Guzik 
1548e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
1552e77cad1SMateusz Guzik #else
1562e77cad1SMateusz Guzik #define mtx_delay	locks_delay
1572e77cad1SMateusz Guzik #endif
1581ada9041SMateusz Guzik #endif
1591ada9041SMateusz Guzik 
1602e77cad1SMateusz Guzik #ifdef MUTEX_SPIN_CUSTOM_BACKOFF
1617029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin,
1627029da5cSPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
163a0d45f0fSMateusz Guzik     "mtx spin debugging");
164a0d45f0fSMateusz Guzik 
165574adb65SMateusz Guzik static struct lock_delay_config __read_frequently mtx_spin_delay;
166a0d45f0fSMateusz Guzik 
1678e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW,
1688e5a3e9aSMateusz Guzik     &mtx_spin_delay.base, 0, "");
1698e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW,
1708e5a3e9aSMateusz Guzik     &mtx_spin_delay.max, 0, "");
171a0d45f0fSMateusz Guzik 
1728e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
1732e77cad1SMateusz Guzik #else
1742e77cad1SMateusz Guzik #define mtx_spin_delay	locks_delay
1752e77cad1SMateusz Guzik #endif
176a0d45f0fSMateusz Guzik 
1779ed346baSBosko Milekic /*
178c53c013bSJohn Baldwin  * System-wide mutexes
179c53c013bSJohn Baldwin  */
1802502c107SJeff Roberson struct mtx blocked_lock;
1816a569d35SMateusz Guzik struct mtx __exclusive_cache_line Giant;
182c53c013bSJohn Baldwin 
18315140a8aSMateusz Guzik static void _mtx_lock_indefinite_check(struct mtx *, struct lock_delay_arg *);
18415140a8aSMateusz Guzik 
18567784314SPoul-Henning Kamp void
186d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what)
187f9721b43SAttilio Rao {
188f9721b43SAttilio Rao 
189a11bf9a4SXin LI 	/*
190a11bf9a4SXin LI 	 * Treat LA_LOCKED as if LA_XLOCKED was asserted.
191a11bf9a4SXin LI 	 *
192a11bf9a4SXin LI 	 * Some callers of lc_assert uses LA_LOCKED to indicate that either
193a11bf9a4SXin LI 	 * a shared lock or write lock was held, while other callers uses
194a11bf9a4SXin LI 	 * the more strict LA_XLOCKED (used as MA_OWNED).
195a11bf9a4SXin LI 	 *
196a11bf9a4SXin LI 	 * Mutex is the only lock class that can not be shared, as a result,
197a11bf9a4SXin LI 	 * we can reasonably consider the caller really intends to assert
198a11bf9a4SXin LI 	 * LA_XLOCKED when they are asserting LA_LOCKED on a mutex object.
199a11bf9a4SXin LI 	 */
200a11bf9a4SXin LI 	if (what & LA_LOCKED) {
201a11bf9a4SXin LI 		what &= ~LA_LOCKED;
202a11bf9a4SXin LI 		what |= LA_XLOCKED;
203a11bf9a4SXin LI 	}
204d576deedSPawel Jakub Dawidek 	mtx_assert((const struct mtx *)lock, what);
205f9721b43SAttilio Rao }
206f9721b43SAttilio Rao 
20767784314SPoul-Henning Kamp void
2087faf4d90SDavide Italiano lock_mtx(struct lock_object *lock, uintptr_t how)
2096e21afd4SJohn Baldwin {
2106e21afd4SJohn Baldwin 
2116e21afd4SJohn Baldwin 	mtx_lock((struct mtx *)lock);
2126e21afd4SJohn Baldwin }
2136e21afd4SJohn Baldwin 
21467784314SPoul-Henning Kamp void
2157faf4d90SDavide Italiano lock_spin(struct lock_object *lock, uintptr_t how)
2166e21afd4SJohn Baldwin {
2176e21afd4SJohn Baldwin 
218*4730a897SAlexander Motin 	mtx_lock_spin((struct mtx *)lock);
2196e21afd4SJohn Baldwin }
2206e21afd4SJohn Baldwin 
2217faf4d90SDavide Italiano uintptr_t
2226e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock)
2236e21afd4SJohn Baldwin {
2246e21afd4SJohn Baldwin 	struct mtx *m;
2256e21afd4SJohn Baldwin 
2266e21afd4SJohn Baldwin 	m = (struct mtx *)lock;
2276e21afd4SJohn Baldwin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
2286e21afd4SJohn Baldwin 	mtx_unlock(m);
2296e21afd4SJohn Baldwin 	return (0);
2306e21afd4SJohn Baldwin }
2316e21afd4SJohn Baldwin 
2327faf4d90SDavide Italiano uintptr_t
2336e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock)
2346e21afd4SJohn Baldwin {
235*4730a897SAlexander Motin 	struct mtx *m;
2366e21afd4SJohn Baldwin 
237*4730a897SAlexander Motin 	m = (struct mtx *)lock;
238*4730a897SAlexander Motin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
239*4730a897SAlexander Motin 	mtx_unlock_spin(m);
240*4730a897SAlexander Motin 	return (0);
2416e21afd4SJohn Baldwin }
2426e21afd4SJohn Baldwin 
243a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
244a5aedd68SStacey Son int
245d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner)
246a5aedd68SStacey Son {
24702315a67SMark Johnston 	const struct mtx *m;
24802315a67SMark Johnston 	uintptr_t x;
249a5aedd68SStacey Son 
25002315a67SMark Johnston 	m = (const struct mtx *)lock;
25102315a67SMark Johnston 	x = m->mtx_lock;
25202315a67SMark Johnston 	*owner = (struct thread *)(x & ~MTX_FLAGMASK);
253e280ce46SMateusz Guzik 	return (*owner != NULL);
254a5aedd68SStacey Son }
255a5aedd68SStacey Son #endif
256a5aedd68SStacey Son 
2570cde2e34SJason Evans /*
2586283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2596283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2606283b7d0SJohn Baldwin  */
2616283b7d0SJohn Baldwin void
2627f44c618SAttilio Rao __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
2636283b7d0SJohn Baldwin {
2647f44c618SAttilio Rao 	struct mtx *m;
26508da2677SMateusz Guzik 	uintptr_t tid, v;
2666283b7d0SJohn Baldwin 
2677f44c618SAttilio Rao 	m = mtxlock2mtx(c);
2687f44c618SAttilio Rao 
269704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
270704cb42fSMark Johnston 	    !TD_IS_IDLETHREAD(curthread),
271e3ae0dfeSAttilio Rao 	    ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
272e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
273186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
274186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
275aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
276aa89d8cdSJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2770d975d63SJohn Baldwin 	    file, line));
278ac6b769bSAttilio Rao 	WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
279ac6b769bSAttilio Rao 	    LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
2807c0435b9SKip Macy 
28108da2677SMateusz Guzik 	tid = (uintptr_t)curthread;
28208da2677SMateusz Guzik 	v = MTX_UNOWNED;
28308da2677SMateusz Guzik 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
2842f1ddb89SMateusz Guzik 		_mtx_lock_sleep(m, v, opts, file, line);
28508da2677SMateusz Guzik 	else
28608da2677SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
28708da2677SMateusz Guzik 		    m, 0, 0, file, line);
288aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
289dde96c99SJohn Baldwin 	    line);
290ac6b769bSAttilio Rao 	WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
291ac6b769bSAttilio Rao 	    file, line);
292ce1c953eSMark Johnston 	TD_LOCKS_INC(curthread);
2936283b7d0SJohn Baldwin }
2946283b7d0SJohn Baldwin 
2956283b7d0SJohn Baldwin void
2967f44c618SAttilio Rao __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
2976283b7d0SJohn Baldwin {
2987f44c618SAttilio Rao 	struct mtx *m;
29935370593SAndriy Gapon 
3007f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3017f44c618SAttilio Rao 
302186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
303186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
304aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
305aa89d8cdSJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
3060d975d63SJohn Baldwin 	    file, line));
307aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
308aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
3090d975d63SJohn Baldwin 	    line);
31021377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
311c66d7606SKip Macy 
312ffd5c94cSMateusz Guzik #ifdef LOCK_PROFILING
313b584eb2eSMateusz Guzik 	__mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
314ffd5c94cSMateusz Guzik #else
315ffd5c94cSMateusz Guzik 	__mtx_unlock(m, curthread, opts, file, line);
316ffd5c94cSMateusz Guzik #endif
317ce1c953eSMark Johnston 	TD_LOCKS_DEC(curthread);
3186283b7d0SJohn Baldwin }
3196283b7d0SJohn Baldwin 
3206283b7d0SJohn Baldwin void
3217f44c618SAttilio Rao __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
3227f44c618SAttilio Rao     int line)
3236283b7d0SJohn Baldwin {
3247f44c618SAttilio Rao 	struct mtx *m;
32562bf13cbSMateusz Guzik #ifdef SMP
3260d74fe26SMateusz Guzik 	uintptr_t tid, v;
32762bf13cbSMateusz Guzik #endif
3286283b7d0SJohn Baldwin 
3297f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3307f44c618SAttilio Rao 
331186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
332186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
333aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
3340d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
335aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
336ad69e26bSJohn Baldwin 	if (mtx_owned(m))
337ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
338ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
339ad69e26bSJohn Baldwin 	    ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
340ad69e26bSJohn Baldwin 		    m->lock_object.lo_name, file, line));
341ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
342aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
34341313430SJohn Baldwin 	    file, line, NULL);
34462bf13cbSMateusz Guzik #ifdef SMP
3450d74fe26SMateusz Guzik 	spinlock_enter();
3460d74fe26SMateusz Guzik 	tid = (uintptr_t)curthread;
3470d74fe26SMateusz Guzik 	v = MTX_UNOWNED;
3480d74fe26SMateusz Guzik 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
3490d74fe26SMateusz Guzik 		_mtx_lock_spin(m, v, opts, file, line);
3500d74fe26SMateusz Guzik 	else
3516a467cc5SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_SPIN_LOCK_SUCCESS(spin__acquire,
3520d74fe26SMateusz Guzik 		    m, 0, 0, file, line);
35362bf13cbSMateusz Guzik #else
35462bf13cbSMateusz Guzik 	__mtx_lock_spin(m, curthread, opts, file, line);
35562bf13cbSMateusz Guzik #endif
356aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
357dde96c99SJohn Baldwin 	    line);
358aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
3596283b7d0SJohn Baldwin }
3606283b7d0SJohn Baldwin 
36190b581f2SKonstantin Belousov int
36290b581f2SKonstantin Belousov __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
36390b581f2SKonstantin Belousov     int line)
36490b581f2SKonstantin Belousov {
36590b581f2SKonstantin Belousov 	struct mtx *m;
36690b581f2SKonstantin Belousov 
36790b581f2SKonstantin Belousov 	if (SCHEDULER_STOPPED())
36890b581f2SKonstantin Belousov 		return (1);
36990b581f2SKonstantin Belousov 
37090b581f2SKonstantin Belousov 	m = mtxlock2mtx(c);
37190b581f2SKonstantin Belousov 
37290b581f2SKonstantin Belousov 	KASSERT(m->mtx_lock != MTX_DESTROYED,
37390b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
37490b581f2SKonstantin Belousov 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
37590b581f2SKonstantin Belousov 	    ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
37690b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
37790b581f2SKonstantin Belousov 	KASSERT((opts & MTX_RECURSE) == 0,
37890b581f2SKonstantin Belousov 	    ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
37990b581f2SKonstantin Belousov 	    m->lock_object.lo_name, file, line));
38090b581f2SKonstantin Belousov 	if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
38190b581f2SKonstantin Belousov 		LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
38290b581f2SKonstantin Belousov 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
38390b581f2SKonstantin Belousov 		return (1);
38490b581f2SKonstantin Belousov 	}
38590b581f2SKonstantin Belousov 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
38690b581f2SKonstantin Belousov 	return (0);
38790b581f2SKonstantin Belousov }
38890b581f2SKonstantin Belousov 
3896283b7d0SJohn Baldwin void
3907f44c618SAttilio Rao __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
3917f44c618SAttilio Rao     int line)
3926283b7d0SJohn Baldwin {
3937f44c618SAttilio Rao 	struct mtx *m;
394c66d7606SKip Macy 
3957f44c618SAttilio Rao 	m = mtxlock2mtx(c);
3967f44c618SAttilio Rao 
397186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
398186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
399aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
4000d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
401aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
402aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
403aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
404dde96c99SJohn Baldwin 	    line);
4050d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
406c66d7606SKip Macy 
407961135eaSJohn Baldwin 	__mtx_unlock_spin(m);
4086283b7d0SJohn Baldwin }
4096283b7d0SJohn Baldwin 
4106283b7d0SJohn Baldwin /*
4119ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
412eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
413eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
4140cde2e34SJason Evans  */
4150cde2e34SJason Evans int
416013c0b49SMateusz Guzik _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
4170cde2e34SJason Evans {
4185c5df0d9SMateusz Guzik 	struct thread *td;
4195c5df0d9SMateusz Guzik 	uintptr_t tid, v;
4201723a064SJeff Roberson #ifdef LOCK_PROFILING
4217c0435b9SKip Macy 	uint64_t waittime = 0;
4221723a064SJeff Roberson 	int contested = 0;
4231723a064SJeff Roberson #endif
4241723a064SJeff Roberson 	int rval;
4255c5df0d9SMateusz Guzik 	bool recursed;
4260cde2e34SJason Evans 
4275c5df0d9SMateusz Guzik 	td = curthread;
4285c5df0d9SMateusz Guzik 	tid = (uintptr_t)td;
4295c5df0d9SMateusz Guzik 	if (SCHEDULER_STOPPED_TD(td))
43035370593SAndriy Gapon 		return (1);
43135370593SAndriy Gapon 
432704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
433e3ae0dfeSAttilio Rao 	    ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
434e3ae0dfeSAttilio Rao 	    curthread, m->lock_object.lo_name, file, line));
435186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
436186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
437aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
438aa89d8cdSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
43983cece6fSJohn Baldwin 	    file, line));
4409ed346baSBosko Milekic 
4415c5df0d9SMateusz Guzik 	rval = 1;
4425c5df0d9SMateusz Guzik 	recursed = false;
4435c5df0d9SMateusz Guzik 	v = MTX_UNOWNED;
444b247fd39SMateusz Guzik 	for (;;) {
445b247fd39SMateusz Guzik 		if (_mtx_obtain_lock_fetch(m, &v, tid))
446b247fd39SMateusz Guzik 			break;
447b247fd39SMateusz Guzik 		if (v == MTX_UNOWNED)
448b247fd39SMateusz Guzik 			continue;
4495c5df0d9SMateusz Guzik 		if (v == tid &&
4505c5df0d9SMateusz Guzik 		    ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
451ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0)) {
452eac09796SJohn Baldwin 			m->mtx_recurse++;
453eac09796SJohn Baldwin 			atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
4545c5df0d9SMateusz Guzik 			recursed = true;
455b247fd39SMateusz Guzik 			break;
4565c5df0d9SMateusz Guzik 		}
457b247fd39SMateusz Guzik 		rval = 0;
458b247fd39SMateusz Guzik 		break;
4595c5df0d9SMateusz Guzik 	}
4605c5df0d9SMateusz Guzik 
461ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
4629ed346baSBosko Milekic 
463aa89d8cdSJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
464764e4d54SJohn Baldwin 	if (rval) {
465aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4662d96f0b1SJohn Baldwin 		    file, line);
467ce1c953eSMark Johnston 		TD_LOCKS_INC(curthread);
4685c5df0d9SMateusz Guzik 		if (!recursed)
46932cd0147SMark Johnston 			LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
470a5aedd68SStacey Son 			    m, contested, waittime, file, line);
471764e4d54SJohn Baldwin 	}
4729ed346baSBosko Milekic 
47319284646SJohn Baldwin 	return (rval);
4740cde2e34SJason Evans }
4750cde2e34SJason Evans 
476013c0b49SMateusz Guzik int
477013c0b49SMateusz Guzik _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
478013c0b49SMateusz Guzik {
479013c0b49SMateusz Guzik 	struct mtx *m;
480013c0b49SMateusz Guzik 
481013c0b49SMateusz Guzik 	m = mtxlock2mtx(c);
482013c0b49SMateusz Guzik 	return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
483013c0b49SMateusz Guzik }
484013c0b49SMateusz Guzik 
4850cde2e34SJason Evans /*
4867f44c618SAttilio Rao  * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4879ed346baSBosko Milekic  *
4889ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4899ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4900cde2e34SJason Evans  */
49109f1319aSMateusz Guzik #if LOCK_DEBUG > 0
4920cde2e34SJason Evans void
4932f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
4942f1ddb89SMateusz Guzik     int line)
49509f1319aSMateusz Guzik #else
49609f1319aSMateusz Guzik void
4972f1ddb89SMateusz Guzik __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
49809f1319aSMateusz Guzik #endif
49936412d79SJohn Baldwin {
5002f1ddb89SMateusz Guzik 	struct thread *td;
5017f44c618SAttilio Rao 	struct mtx *m;
5022502c107SJeff Roberson 	struct turnstile *ts;
5032f1ddb89SMateusz Guzik 	uintptr_t tid;
5042ccee9ccSMateusz Guzik 	struct thread *owner;
5051723a064SJeff Roberson #ifdef LOCK_PROFILING
50670fe8436SKip Macy 	int contested = 0;
50770fe8436SKip Macy 	uint64_t waittime = 0;
5081723a064SJeff Roberson #endif
5091ada9041SMateusz Guzik #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
5101ada9041SMateusz Guzik 	struct lock_delay_arg lda;
5111ada9041SMateusz Guzik #endif
512a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
51361852185SMateusz Guzik 	u_int sleep_cnt = 0;
514a5aedd68SStacey Son 	int64_t sleep_time = 0;
515076dd8ebSAndriy Gapon 	int64_t all_time = 0;
516dfaa7859SMateusz Guzik #endif
517dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
518f183fb16SMateusz Guzik 	int doing_lockprof = 0;
519a5aedd68SStacey Son #endif
52009bdec20SMateusz Guzik 
5212f1ddb89SMateusz Guzik 	td = curthread;
5222f1ddb89SMateusz Guzik 	tid = (uintptr_t)td;
52309bdec20SMateusz Guzik 	m = mtxlock2mtx(c);
52409bdec20SMateusz Guzik 
52509bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS
52609bdec20SMateusz Guzik 	if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
52709bdec20SMateusz Guzik 		while (v == MTX_UNOWNED) {
52809bdec20SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
52909bdec20SMateusz Guzik 				goto out_lockstat;
53009bdec20SMateusz Guzik 		}
53109bdec20SMateusz Guzik 		doing_lockprof = 1;
53209bdec20SMateusz Guzik 		all_time -= lockstat_nsecs(&m->lock_object);
53309bdec20SMateusz Guzik 	}
53409bdec20SMateusz Guzik #endif
53509bdec20SMateusz Guzik #ifdef LOCK_PROFILING
53609bdec20SMateusz Guzik 	doing_lockprof = 1;
53709bdec20SMateusz Guzik #endif
53809bdec20SMateusz Guzik 
5392f1ddb89SMateusz Guzik 	if (SCHEDULER_STOPPED_TD(td))
54035370593SAndriy Gapon 		return;
54135370593SAndriy Gapon 
542c1aaf63cSMateusz Guzik 	if (__predict_false(v == MTX_UNOWNED))
543c1aaf63cSMateusz Guzik 		v = MTX_READ_VALUE(m);
5447f44c618SAttilio Rao 
5452f1ddb89SMateusz Guzik 	if (__predict_false(lv_mtx_owner(v) == td)) {
546ac6b769bSAttilio Rao 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
547ac6b769bSAttilio Rao 		    (opts & MTX_RECURSE) != 0,
548eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
549aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, file, line));
550a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0
551ac6b769bSAttilio Rao 		opts &= ~MTX_RECURSE;
552a24c8eb8SMateusz Guzik #endif
55336412d79SJohn Baldwin 		m->mtx_recurse++;
55408812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
555aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
5565746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
55736412d79SJohn Baldwin 		return;
55836412d79SJohn Baldwin 	}
559a24c8eb8SMateusz Guzik #if LOCK_DEBUG > 0
560ac6b769bSAttilio Rao 	opts &= ~MTX_RECURSE;
561a24c8eb8SMateusz Guzik #endif
5629ed346baSBosko Milekic 
563f90d57b8SMateusz Guzik #if defined(ADAPTIVE_MUTEXES)
564f90d57b8SMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_delay);
565f90d57b8SMateusz Guzik #elif defined(KDTRACE_HOOKS)
566f90d57b8SMateusz Guzik 	lock_delay_arg_init_noadapt(&lda);
567f90d57b8SMateusz Guzik #endif
568f90d57b8SMateusz Guzik 
569f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
570f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
571f5f9340bSFabien Thomas #endif
5726a467cc5SMateusz Guzik 	lock_profile_obtain_lock_failed(&m->lock_object, false,
57370fe8436SKip Macy 		    &contested, &waittime);
574aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
57515ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
57615ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
577aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
5781bd0eefbSJohn Baldwin 
579fc4f686dSMateusz Guzik 	for (;;) {
5802604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
58190836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
582fc4f686dSMateusz Guzik 				break;
5832604eb9eSMateusz Guzik 			continue;
5842604eb9eSMateusz Guzik 		}
585a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
5861ada9041SMateusz Guzik 		lda.spin_cnt++;
587a5aedd68SStacey Son #endif
58849aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
58949aead8aSAttilio Rao 		/*
59049aead8aSAttilio Rao 		 * If the owner is running on another CPU, spin until the
59149aead8aSAttilio Rao 		 * owner stops running or the state of the lock changes.
59249aead8aSAttilio Rao 		 */
5932604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
59449aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
59549aead8aSAttilio Rao 			if (LOCK_LOG_TEST(&m->lock_object, 0))
59649aead8aSAttilio Rao 				CTR3(KTR_LOCK,
59749aead8aSAttilio Rao 				    "%s: spinning on %p held by %p",
59849aead8aSAttilio Rao 				    __func__, m, owner);
5992cba8dd3SJohn Baldwin 			KTR_STATE1(KTR_SCHED, "thread",
6002cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
6012cba8dd3SJohn Baldwin 			    "spinning", "lockname:\"%s\"",
6022cba8dd3SJohn Baldwin 			    m->lock_object.lo_name);
6032604eb9eSMateusz Guzik 			do {
6041ada9041SMateusz Guzik 				lock_delay(&lda);
605391df78aSMateusz Guzik 				v = MTX_READ_VALUE(m);
6062604eb9eSMateusz Guzik 				owner = lv_mtx_owner(v);
6072604eb9eSMateusz Guzik 			} while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
6082cba8dd3SJohn Baldwin 			KTR_STATE0(KTR_SCHED, "thread",
6092cba8dd3SJohn Baldwin 			    sched_tdname((struct thread *)tid),
6102cba8dd3SJohn Baldwin 			    "running");
61149aead8aSAttilio Rao 			continue;
61249aead8aSAttilio Rao 		}
61349aead8aSAttilio Rao #endif
61449aead8aSAttilio Rao 
6152502c107SJeff Roberson 		ts = turnstile_trywait(&m->lock_object);
6162604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
617310f24d7SMateusz Guzik retry_turnstile:
6185fa8dd90SJohn Baldwin 
61936412d79SJohn Baldwin 		/*
6209ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
621961a7b24SJohn Baldwin 		 * the turnstile chain lock.
62236412d79SJohn Baldwin 		 */
6235fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
6242502c107SJeff Roberson 			turnstile_cancel(ts);
62536412d79SJohn Baldwin 			continue;
62636412d79SJohn Baldwin 		}
6279ed346baSBosko Milekic 
62849aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
62949aead8aSAttilio Rao 		/*
630fa29f023SJohn Baldwin 		 * The current lock owner might have started executing
631fa29f023SJohn Baldwin 		 * on another CPU (or the lock could have changed
632fa29f023SJohn Baldwin 		 * owners) while we were waiting on the turnstile
633fa29f023SJohn Baldwin 		 * chain lock.  If so, drop the turnstile lock and try
634fa29f023SJohn Baldwin 		 * again.
63549aead8aSAttilio Rao 		 */
6362604eb9eSMateusz Guzik 		owner = lv_mtx_owner(v);
63749aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
63849aead8aSAttilio Rao 			turnstile_cancel(ts);
63949aead8aSAttilio Rao 			continue;
64049aead8aSAttilio Rao 		}
64149aead8aSAttilio Rao #endif
64249aead8aSAttilio Rao 
64336412d79SJohn Baldwin 		/*
6449ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
6459ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
6469ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
64736412d79SJohn Baldwin 		 */
64836412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
649310f24d7SMateusz Guzik 		    !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) {
650310f24d7SMateusz Guzik 			goto retry_turnstile;
65136412d79SJohn Baldwin 		}
65236412d79SJohn Baldwin 
6539ed346baSBosko Milekic 		/*
6547feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
6559ed346baSBosko Milekic 		 */
65636412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
65736412d79SJohn Baldwin 
6589ed346baSBosko Milekic 		/*
659961a7b24SJohn Baldwin 		 * Block on the turnstile.
6609ed346baSBosko Milekic 		 */
661a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
662e2b25737SMark Johnston 		sleep_time -= lockstat_nsecs(&m->lock_object);
663a5aedd68SStacey Son #endif
664284194f1SMateusz Guzik #ifndef ADAPTIVE_MUTEXES
665284194f1SMateusz Guzik 		owner = mtx_owner(m);
666284194f1SMateusz Guzik #endif
6678448e020SMateusz Guzik 		MPASS(owner == mtx_owner(m));
6688448e020SMateusz Guzik 		turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
669a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
670e2b25737SMark Johnston 		sleep_time += lockstat_nsecs(&m->lock_object);
671a5aedd68SStacey Son 		sleep_cnt++;
672a5aedd68SStacey Son #endif
6732604eb9eSMateusz Guzik 		v = MTX_READ_VALUE(m);
67436412d79SJohn Baldwin 	}
675dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
676dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
6777640beb9SMateusz Guzik 		return;
678dfaa7859SMateusz Guzik #endif
679dfaa7859SMateusz Guzik #ifdef KDTRACE_HOOKS
6807640beb9SMateusz Guzik 	all_time += lockstat_nsecs(&m->lock_object);
681a5aedd68SStacey Son 	if (sleep_time)
68232cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
683a5aedd68SStacey Son 
684a5aedd68SStacey Son 	/*
685a5aedd68SStacey Son 	 * Only record the loops spinning and not sleeping.
686a5aedd68SStacey Son 	 */
6871ada9041SMateusz Guzik 	if (lda.spin_cnt > sleep_cnt)
68832cd0147SMark Johnston 		LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
68909bdec20SMateusz Guzik out_lockstat:
690a5aedd68SStacey Son #endif
69109bdec20SMateusz Guzik 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
69209bdec20SMateusz Guzik 	    waittime, file, line);
6939ed346baSBosko Milekic }
6949ed346baSBosko Milekic 
695b95b98b0SKonstantin Belousov #ifdef SMP
6969ed346baSBosko Milekic /*
6977f44c618SAttilio Rao  * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
6989ed346baSBosko Milekic  *
6999ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
7009ed346baSBosko Milekic  * is handled inline.
7019ed346baSBosko Milekic  */
7020d74fe26SMateusz Guzik #if LOCK_DEBUG > 0
7039ed346baSBosko Milekic void
7040d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
7050d74fe26SMateusz Guzik     const char *file, int line)
7060d74fe26SMateusz Guzik #else
7070d74fe26SMateusz Guzik void
7080d74fe26SMateusz Guzik _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
7090d74fe26SMateusz Guzik #endif
71036412d79SJohn Baldwin {
7117f44c618SAttilio Rao 	struct mtx *m;
712a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
7130d74fe26SMateusz Guzik 	uintptr_t tid;
7141723a064SJeff Roberson #ifdef LOCK_PROFILING
7151723a064SJeff Roberson 	int contested = 0;
71670fe8436SKip Macy 	uint64_t waittime = 0;
7171723a064SJeff Roberson #endif
718076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
719076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
720076dd8ebSAndriy Gapon #endif
721dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
722f183fb16SMateusz Guzik 	int doing_lockprof = 0;
723dfaa7859SMateusz Guzik #endif
72436412d79SJohn Baldwin 
7250d74fe26SMateusz Guzik 	tid = (uintptr_t)curthread;
7267f44c618SAttilio Rao 	m = mtxlock2mtx(c);
7277f44c618SAttilio Rao 
72809bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS
72909bdec20SMateusz Guzik 	if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
73009bdec20SMateusz Guzik 		while (v == MTX_UNOWNED) {
73109bdec20SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
73209bdec20SMateusz Guzik 				goto out_lockstat;
73309bdec20SMateusz Guzik 		}
73409bdec20SMateusz Guzik 		doing_lockprof = 1;
73509bdec20SMateusz Guzik 		spin_time -= lockstat_nsecs(&m->lock_object);
73609bdec20SMateusz Guzik 	}
73709bdec20SMateusz Guzik #endif
73809bdec20SMateusz Guzik #ifdef LOCK_PROFILING
73909bdec20SMateusz Guzik 	doing_lockprof = 1;
74009bdec20SMateusz Guzik #endif
74109bdec20SMateusz Guzik 
74213d2ef0fSMateusz Guzik 	if (__predict_false(v == MTX_UNOWNED))
74313d2ef0fSMateusz Guzik 		v = MTX_READ_VALUE(m);
74413d2ef0fSMateusz Guzik 
74513d2ef0fSMateusz Guzik 	if (__predict_false(v == tid)) {
74613d2ef0fSMateusz Guzik 		m->mtx_recurse++;
74713d2ef0fSMateusz Guzik 		return;
74813d2ef0fSMateusz Guzik 	}
74913d2ef0fSMateusz Guzik 
7500d74fe26SMateusz Guzik 	if (SCHEDULER_STOPPED())
7510d74fe26SMateusz Guzik 		return;
7520d74fe26SMateusz Guzik 
753aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
7545746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
7552cba8dd3SJohn Baldwin 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
7562cba8dd3SJohn Baldwin 	    "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
7579ed346baSBosko Milekic 
758f90d57b8SMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
759f90d57b8SMateusz Guzik 
760f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
761f5f9340bSFabien Thomas 	PMC_SOFT_CALL( , , lock, failed);
762f5f9340bSFabien Thomas #endif
7636a467cc5SMateusz Guzik 	lock_profile_obtain_lock_failed(&m->lock_object, true, &contested, &waittime);
76409bdec20SMateusz Guzik 
765fc4f686dSMateusz Guzik 	for (;;) {
7662604eb9eSMateusz Guzik 		if (v == MTX_UNOWNED) {
76790836c32SMateusz Guzik 			if (_mtx_obtain_lock_fetch(m, &v, tid))
768fc4f686dSMateusz Guzik 				break;
76936412d79SJohn Baldwin 			continue;
770703fc290SJohn Baldwin 		}
7712604eb9eSMateusz Guzik 		/* Give interrupts a chance while we spin. */
7722604eb9eSMateusz Guzik 		spinlock_exit();
7732604eb9eSMateusz Guzik 		do {
77415140a8aSMateusz Guzik 			if (__predict_true(lda.spin_cnt < 10000000)) {
7752604eb9eSMateusz Guzik 				lock_delay(&lda);
7762604eb9eSMateusz Guzik 			} else {
77715140a8aSMateusz Guzik 				_mtx_lock_indefinite_check(m, &lda);
77836412d79SJohn Baldwin 			}
7792604eb9eSMateusz Guzik 			v = MTX_READ_VALUE(m);
7802604eb9eSMateusz Guzik 		} while (v != MTX_UNOWNED);
781c6a37e84SJohn Baldwin 		spinlock_enter();
78236412d79SJohn Baldwin 	}
78336412d79SJohn Baldwin 
784aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
7859ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
7862cba8dd3SJohn Baldwin 	KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
7872cba8dd3SJohn Baldwin 	    "running");
7889ed346baSBosko Milekic 
789dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
790dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
791dfaa7859SMateusz Guzik 		return;
792dfaa7859SMateusz Guzik #endif
793c6d48c87SMark Johnston #ifdef KDTRACE_HOOKS
794dfaa7859SMateusz Guzik 	spin_time += lockstat_nsecs(&m->lock_object);
79509bdec20SMateusz Guzik 	if (lda.spin_cnt != 0)
79609bdec20SMateusz Guzik 		LOCKSTAT_RECORD1(spin__spin, m, spin_time);
79709bdec20SMateusz Guzik out_lockstat:
798dfaa7859SMateusz Guzik #endif
7996a467cc5SMateusz Guzik 	LOCKSTAT_PROFILE_OBTAIN_SPIN_LOCK_SUCCESS(spin__acquire, m,
80032cd0147SMark Johnston 	    contested, waittime, file, line);
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));
8143fd19ce7SMateusz Guzik 	KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) == 0,
8153fd19ce7SMateusz Guzik 	    ("thread_lock: got a recursive mutex %s @ %s:%d\n",
816be49509eSMateusz Guzik 	    m->lock_object.lo_name, file, line));
817be49509eSMateusz Guzik 	WITNESS_CHECKORDER(&m->lock_object,
818be49509eSMateusz Guzik 	    opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
819be49509eSMateusz Guzik }
820be49509eSMateusz Guzik #else
821be49509eSMateusz Guzik #define thread_lock_validate(m, opts, file, line) do { } while (0)
822be49509eSMateusz Guzik #endif
823be49509eSMateusz Guzik 
824be49509eSMateusz Guzik #ifndef LOCK_PROFILING
825be49509eSMateusz Guzik #if LOCK_DEBUG > 0
826be49509eSMateusz Guzik void
827be49509eSMateusz Guzik _thread_lock(struct thread *td, int opts, const char *file, int line)
828be49509eSMateusz Guzik #else
829be49509eSMateusz Guzik void
830be49509eSMateusz Guzik _thread_lock(struct thread *td)
831be49509eSMateusz Guzik #endif
832be49509eSMateusz Guzik {
833be49509eSMateusz Guzik 	struct mtx *m;
8343fd19ce7SMateusz Guzik 	uintptr_t tid;
835be49509eSMateusz Guzik 
836be49509eSMateusz Guzik 	tid = (uintptr_t)curthread;
837be49509eSMateusz Guzik 
8382c50bafeSMateusz Guzik 	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
8392c50bafeSMateusz Guzik 		goto slowpath_noirq;
840be49509eSMateusz Guzik 	spinlock_enter();
841be49509eSMateusz Guzik 	m = td->td_lock;
842be49509eSMateusz Guzik 	thread_lock_validate(m, 0, file, line);
8433fd19ce7SMateusz Guzik 	if (__predict_false(m == &blocked_lock))
844be49509eSMateusz Guzik 		goto slowpath_unlocked;
8453fd19ce7SMateusz Guzik 	if (__predict_false(!_mtx_obtain_lock(m, tid)))
846be49509eSMateusz Guzik 		goto slowpath_unlocked;
847be49509eSMateusz Guzik 	if (__predict_true(m == td->td_lock)) {
848be49509eSMateusz Guzik 		WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
849be49509eSMateusz Guzik 		return;
850be49509eSMateusz Guzik 	}
851be49509eSMateusz Guzik 	_mtx_release_lock_quick(m);
852be49509eSMateusz Guzik slowpath_unlocked:
853be49509eSMateusz Guzik 	spinlock_exit();
8542c50bafeSMateusz Guzik slowpath_noirq:
8552c50bafeSMateusz Guzik #if LOCK_DEBUG > 0
8562c50bafeSMateusz Guzik 	thread_lock_flags_(td, opts, file, line);
8572c50bafeSMateusz Guzik #else
858be49509eSMateusz Guzik 	thread_lock_flags_(td, 0, 0, 0);
8592c50bafeSMateusz Guzik #endif
860be49509eSMateusz Guzik }
861be49509eSMateusz Guzik #endif
862be49509eSMateusz Guzik 
8632502c107SJeff Roberson void
864ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
8652502c107SJeff Roberson {
8662502c107SJeff Roberson 	struct mtx *m;
8675e5ad162SMateusz Guzik 	uintptr_t tid, v;
868a0d45f0fSMateusz Guzik 	struct lock_delay_arg lda;
8691723a064SJeff Roberson #ifdef LOCK_PROFILING
8701723a064SJeff Roberson 	int contested = 0;
8711723a064SJeff Roberson 	uint64_t waittime = 0;
8721723a064SJeff Roberson #endif
873a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
874076dd8ebSAndriy Gapon 	int64_t spin_time = 0;
875a5aedd68SStacey Son #endif
876dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
877dfaa7859SMateusz Guzik 	int doing_lockprof = 1;
878dfaa7859SMateusz Guzik #endif
8792502c107SJeff Roberson 
8802502c107SJeff Roberson 	tid = (uintptr_t)curthread;
88135370593SAndriy Gapon 
882f61d6c5aSMark Johnston 	if (SCHEDULER_STOPPED()) {
883f61d6c5aSMark Johnston 		/*
884f61d6c5aSMark Johnston 		 * Ensure that spinlock sections are balanced even when the
885f61d6c5aSMark Johnston 		 * scheduler is stopped, since we may otherwise inadvertently
886f61d6c5aSMark Johnston 		 * re-enable interrupts while dumping core.
887f61d6c5aSMark Johnston 		 */
888f61d6c5aSMark Johnston 		spinlock_enter();
88935370593SAndriy Gapon 		return;
890f61d6c5aSMark Johnston 	}
89135370593SAndriy Gapon 
892a0d45f0fSMateusz Guzik 	lock_delay_arg_init(&lda, &mtx_spin_delay);
893a0d45f0fSMateusz Guzik 
8941f4d28c7SMateusz Guzik #ifdef HWPMC_HOOKS
8951f4d28c7SMateusz Guzik 	PMC_SOFT_CALL( , , lock, failed);
8961f4d28c7SMateusz Guzik #endif
8971f4d28c7SMateusz Guzik 
898dfaa7859SMateusz Guzik #ifdef LOCK_PROFILING
899dfaa7859SMateusz Guzik 	doing_lockprof = 1;
900df1c30f6SWarner Losh #elif defined(KDTRACE_HOOKS)
901dfaa7859SMateusz Guzik 	doing_lockprof = lockstat_enabled;
90242862413SEric van Gyzen #endif
90342862413SEric van Gyzen #ifdef KDTRACE_HOOKS
904dfaa7859SMateusz Guzik 	if (__predict_false(doing_lockprof))
905e2b25737SMark Johnston 		spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
906076dd8ebSAndriy Gapon #endif
9079f4e008dSMateusz Guzik 	spinlock_enter();
9089f4e008dSMateusz Guzik 
9092502c107SJeff Roberson 	for (;;) {
9102502c107SJeff Roberson retry:
911710eacdcSJeff Roberson 		m = td->td_lock;
912be49509eSMateusz Guzik 		thread_lock_validate(m, opts, file, line);
9131f4d28c7SMateusz Guzik 		v = MTX_READ_VALUE(m);
914fc4f686dSMateusz Guzik 		for (;;) {
9151f4d28c7SMateusz Guzik 			if (v == MTX_UNOWNED) {
91690836c32SMateusz Guzik 				if (_mtx_obtain_lock_fetch(m, &v, tid))
917fc4f686dSMateusz Guzik 					break;
9185e5ad162SMateusz Guzik 				continue;
9191f4d28c7SMateusz Guzik 			}
9203fd19ce7SMateusz Guzik 			MPASS(v != tid);
9216a467cc5SMateusz Guzik 			lock_profile_obtain_lock_failed(&m->lock_object, true,
922eea4f254SJeff Roberson 			    &contested, &waittime);
9232502c107SJeff Roberson 			/* Give interrupts a chance while we spin. */
9242502c107SJeff Roberson 			spinlock_exit();
9255e5ad162SMateusz Guzik 			do {
92615140a8aSMateusz Guzik 				if (__predict_true(lda.spin_cnt < 10000000)) {
927a0d45f0fSMateusz Guzik 					lock_delay(&lda);
928a0d45f0fSMateusz Guzik 				} else {
92915140a8aSMateusz Guzik 					_mtx_lock_indefinite_check(m, &lda);
930a0d45f0fSMateusz Guzik 				}
9319f4e008dSMateusz Guzik 				if (m != td->td_lock) {
9329f4e008dSMateusz Guzik 					spinlock_enter();
9332502c107SJeff Roberson 					goto retry;
9349f4e008dSMateusz Guzik 				}
9355e5ad162SMateusz Guzik 				v = MTX_READ_VALUE(m);
9365e5ad162SMateusz Guzik 			} while (v != MTX_UNOWNED);
9372502c107SJeff Roberson 			spinlock_enter();
9382502c107SJeff Roberson 		}
9392502c107SJeff Roberson 		if (m == td->td_lock)
9402502c107SJeff Roberson 			break;
9419f4e008dSMateusz Guzik 		_mtx_release_lock_quick(m);
9422502c107SJeff Roberson 	}
943dfaa7859SMateusz Guzik 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
944dfaa7859SMateusz Guzik 	    line);
945dfaa7859SMateusz Guzik 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
946dfaa7859SMateusz Guzik 
947dfaa7859SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
948dfaa7859SMateusz Guzik 	if (__predict_true(!doing_lockprof))
949dfaa7859SMateusz Guzik 		return;
950dfaa7859SMateusz Guzik #endif
951076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
952e2b25737SMark Johnston 	spin_time += lockstat_nsecs(&m->lock_object);
953076dd8ebSAndriy Gapon #endif
9546a467cc5SMateusz Guzik 	LOCKSTAT_PROFILE_OBTAIN_SPIN_LOCK_SUCCESS(spin__acquire, m, contested,
9553fd19ce7SMateusz Guzik 	    waittime, file, line);
9565002e195SMark Johnston #ifdef KDTRACE_HOOKS
957d0f68f91SMark Johnston 	if (lda.spin_cnt != 0)
95832cd0147SMark Johnston 		LOCKSTAT_RECORD1(thread__spin, m, spin_time);
9595002e195SMark Johnston #endif
9602502c107SJeff Roberson }
9612502c107SJeff Roberson 
9622502c107SJeff Roberson struct mtx *
9632502c107SJeff Roberson thread_lock_block(struct thread *td)
9642502c107SJeff Roberson {
9652502c107SJeff Roberson 	struct mtx *lock;
9662502c107SJeff Roberson 
967710eacdcSJeff Roberson 	lock = td->td_lock;
96861a74c5cSJeff Roberson 	mtx_assert(lock, MA_OWNED);
9692502c107SJeff Roberson 	td->td_lock = &blocked_lock;
9702502c107SJeff Roberson 
9712502c107SJeff Roberson 	return (lock);
9722502c107SJeff Roberson }
9732502c107SJeff Roberson 
9742502c107SJeff Roberson void
9752502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new)
9762502c107SJeff Roberson {
97761a74c5cSJeff Roberson 
9782502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
97961a74c5cSJeff Roberson 	KASSERT(td->td_lock == &blocked_lock,
98061a74c5cSJeff Roberson 	    ("thread %p lock %p not blocked_lock %p",
98161a74c5cSJeff Roberson 	    td, td->td_lock, &blocked_lock));
98265d32cd8SMatt Jacob 	atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
9832502c107SJeff Roberson }
9842502c107SJeff Roberson 
9852502c107SJeff Roberson void
98661a74c5cSJeff Roberson thread_lock_block_wait(struct thread *td)
98761a74c5cSJeff Roberson {
98861a74c5cSJeff Roberson 
98961a74c5cSJeff Roberson 	while (td->td_lock == &blocked_lock)
99061a74c5cSJeff Roberson 		cpu_spinwait();
99161a74c5cSJeff Roberson 
99261a74c5cSJeff Roberson 	/* Acquire fence to be certain that all thread state is visible. */
99361a74c5cSJeff Roberson 	atomic_thread_fence_acq();
99461a74c5cSJeff Roberson }
99561a74c5cSJeff Roberson 
99661a74c5cSJeff Roberson void
9972502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new)
9982502c107SJeff Roberson {
9992502c107SJeff Roberson 	struct mtx *lock;
10002502c107SJeff Roberson 
10012502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
1002710eacdcSJeff Roberson 	lock = td->td_lock;
100361a74c5cSJeff Roberson 	mtx_assert(lock, MA_OWNED);
10042502c107SJeff Roberson 	td->td_lock = new;
10052502c107SJeff Roberson 	mtx_unlock_spin(lock);
10062502c107SJeff Roberson }
10072502c107SJeff Roberson 
10089ed346baSBosko Milekic /*
10097f44c618SAttilio Rao  * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
10109ed346baSBosko Milekic  *
10113b3cf014SMateusz Guzik  * We are only called here if the lock is recursed, contested (i.e. we
10123b3cf014SMateusz Guzik  * need to wake up a blocked thread) or lockstat probe is active.
10139ed346baSBosko Milekic  */
101409f1319aSMateusz Guzik #if LOCK_DEBUG > 0
101536412d79SJohn Baldwin void
1016b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
1017b584eb2eSMateusz Guzik     const char *file, int line)
101809f1319aSMateusz Guzik #else
101909f1319aSMateusz Guzik void
1020b584eb2eSMateusz Guzik __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
102109f1319aSMateusz Guzik #endif
102236412d79SJohn Baldwin {
10237f44c618SAttilio Rao 	struct mtx *m;
1024961a7b24SJohn Baldwin 	struct turnstile *ts;
1025b584eb2eSMateusz Guzik 	uintptr_t tid;
10269ed346baSBosko Milekic 
102735370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
102835370593SAndriy Gapon 		return;
102935370593SAndriy Gapon 
10303b3cf014SMateusz Guzik 	tid = (uintptr_t)curthread;
10317f44c618SAttilio Rao 	m = mtxlock2mtx(c);
1032b584eb2eSMateusz Guzik 
1033b584eb2eSMateusz Guzik 	if (__predict_false(v == tid))
10343b3cf014SMateusz Guzik 		v = MTX_READ_VALUE(m);
103590836c32SMateusz Guzik 
1036b584eb2eSMateusz Guzik 	if (__predict_false(v & MTX_RECURSED)) {
103736412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
103808812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1039aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
10409ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
104136412d79SJohn Baldwin 		return;
104236412d79SJohn Baldwin 	}
10439ed346baSBosko Milekic 
10443b3cf014SMateusz Guzik 	LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
10453b3cf014SMateusz Guzik 	if (v == tid && _mtx_release_lock(m, tid))
10463b3cf014SMateusz Guzik 		return;
10473b3cf014SMateusz Guzik 
10482502c107SJeff Roberson 	/*
10492502c107SJeff Roberson 	 * We have to lock the chain before the turnstile so this turnstile
10502502c107SJeff Roberson 	 * can be removed from the hash list if it is empty.
10512502c107SJeff Roberson 	 */
10522502c107SJeff Roberson 	turnstile_chain_lock(&m->lock_object);
10538448e020SMateusz Guzik 	_mtx_release_lock_quick(m);
1054aa89d8cdSJohn Baldwin 	ts = turnstile_lookup(&m->lock_object);
10558448e020SMateusz Guzik 	MPASS(ts != NULL);
1056aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
10579ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
10587aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1059bf9c6c31SJohn Baldwin 
10602502c107SJeff Roberson 	/*
10612502c107SJeff Roberson 	 * This turnstile is now no longer associated with the mutex.  We can
10622502c107SJeff Roberson 	 * unlock the chain lock so a new turnstile may take it's place.
10632502c107SJeff Roberson 	 */
1064d0a22279SMateusz Guzik 	turnstile_unpend(ts);
10652502c107SJeff Roberson 	turnstile_chain_unlock(&m->lock_object);
10669ed346baSBosko Milekic }
10679ed346baSBosko Milekic 
10689ed346baSBosko Milekic /*
10699ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
1070961135eaSJohn Baldwin  * See the __mtx_unlock_spin() macro for the details.
10719ed346baSBosko Milekic  */
10729ed346baSBosko Milekic 
10739ed346baSBosko Milekic /*
107415ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
10759ed346baSBosko Milekic  */
10761103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
10770cde2e34SJason Evans void
10787f44c618SAttilio Rao __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
10790cde2e34SJason Evans {
10807f44c618SAttilio Rao 	const struct mtx *m;
10815cb0fbe4SJohn Baldwin 
1082879e0604SMateusz Guzik 	if (KERNEL_PANICKED() || dumping || SCHEDULER_STOPPED())
10835cb0fbe4SJohn Baldwin 		return;
10847f44c618SAttilio Rao 
10857f44c618SAttilio Rao 	m = mtxlock2mtx(c);
10867f44c618SAttilio Rao 
1087a10f4966SJake Burkholder 	switch (what) {
10880cde2e34SJason Evans 	case MA_OWNED:
10890cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
10900cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
1091a10f4966SJake Burkholder 		if (!mtx_owned(m))
10920cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
1093aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
1094a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
1095a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
10960cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
1097aa89d8cdSJohn Baldwin 				    m->lock_object.lo_name, file, line);
1098a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
10990cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
1100aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
11010cde2e34SJason Evans 		}
11020cde2e34SJason Evans 		break;
11030cde2e34SJason Evans 	case MA_NOTOWNED:
1104a10f4966SJake Burkholder 		if (mtx_owned(m))
11050cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
1106aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
11070cde2e34SJason Evans 		break;
11080cde2e34SJason Evans 	default:
110956771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
11100cde2e34SJason Evans 	}
11110cde2e34SJason Evans }
11120cde2e34SJason Evans #endif
11130cde2e34SJason Evans 
11149ed346baSBosko Milekic /*
1115c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
1116c27b5699SAndrew R. Reiter  */
1117c27b5699SAndrew R. Reiter void
1118c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
1119c27b5699SAndrew R. Reiter {
1120c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
1121c27b5699SAndrew R. Reiter 
11227f44c618SAttilio Rao 	mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
11237f44c618SAttilio Rao 	    margs->ma_opts);
1124c27b5699SAndrew R. Reiter }
1125c27b5699SAndrew R. Reiter 
1126c27b5699SAndrew R. Reiter /*
11279ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
11280c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
11290c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
11300c88508aSJohn Baldwin  * witness.
11319ed346baSBosko Milekic  */
113236412d79SJohn Baldwin void
11337f44c618SAttilio Rao _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
113436412d79SJohn Baldwin {
11357f44c618SAttilio Rao 	struct mtx *m;
113683a81bcbSJohn Baldwin 	struct lock_class *class;
113783a81bcbSJohn Baldwin 	int flags;
11389ed346baSBosko Milekic 
11397f44c618SAttilio Rao 	m = mtxlock2mtx(c);
11407f44c618SAttilio Rao 
114119284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1142fd07ddcfSDmitry Chagin 	    MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1143353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1144353998acSAttilio Rao 	    ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1145353998acSAttilio Rao 	    &m->mtx_lock));
11469ed346baSBosko Milekic 
114783a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
114819284646SJohn Baldwin 	if (opts & MTX_SPIN)
114983a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
115019284646SJohn Baldwin 	else
115183a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
115283a81bcbSJohn Baldwin 	flags = 0;
115319284646SJohn Baldwin 	if (opts & MTX_QUIET)
115483a81bcbSJohn Baldwin 		flags |= LO_QUIET;
115519284646SJohn Baldwin 	if (opts & MTX_RECURSE)
115683a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
115719284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
115883a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
1159f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
116083a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
11617c0435b9SKip Macy 	if (opts & MTX_NOPROFILE)
11627c0435b9SKip Macy 		flags |= LO_NOPROFILE;
1163fd07ddcfSDmitry Chagin 	if (opts & MTX_NEW)
1164fd07ddcfSDmitry Chagin 		flags |= LO_NEW;
116519284646SJohn Baldwin 
116683a81bcbSJohn Baldwin 	/* Initialize mutex. */
1167b5fb43e5SJohn Baldwin 	lock_init(&m->lock_object, class, name, type, flags);
1168b5fb43e5SJohn Baldwin 
116919284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
117083a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
117136412d79SJohn Baldwin }
117236412d79SJohn Baldwin 
11739ed346baSBosko Milekic /*
117419284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
117519284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
117619284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
117719284646SJohn Baldwin  * flags.
11789ed346baSBosko Milekic  */
117936412d79SJohn Baldwin void
11807f44c618SAttilio Rao _mtx_destroy(volatile uintptr_t *c)
118136412d79SJohn Baldwin {
11827f44c618SAttilio Rao 	struct mtx *m;
11837f44c618SAttilio Rao 
11847f44c618SAttilio Rao 	m = mtxlock2mtx(c);
118536412d79SJohn Baldwin 
118619284646SJohn Baldwin 	if (!mtx_owned(m))
118719284646SJohn Baldwin 		MPASS(mtx_unowned(m));
118819284646SJohn Baldwin 	else {
118908812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
11909ed346baSBosko Milekic 
1191861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
11926a467cc5SMateusz Guzik 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) {
11936a467cc5SMateusz Guzik 			lock_profile_release_lock(&m->lock_object, true);
1194861a2308SScott Long 			spinlock_exit();
11956a467cc5SMateusz Guzik 		} else {
1196ce1c953eSMark Johnston 			TD_LOCKS_DEC(curthread);
11976a467cc5SMateusz Guzik 			lock_profile_release_lock(&m->lock_object, false);
11986a467cc5SMateusz Guzik 		}
1199861a2308SScott Long 
120019284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
1201aa89d8cdSJohn Baldwin 		WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1202c86b6ff5SJohn Baldwin 		    __LINE__);
120336412d79SJohn Baldwin 	}
12040384fff8SJason Evans 
1205186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
1206aa89d8cdSJohn Baldwin 	lock_destroy(&m->lock_object);
12070384fff8SJason Evans }
1208d23f5958SMatthew Dillon 
1209d23f5958SMatthew Dillon /*
1210c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
1211c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
1212c53c013bSJohn Baldwin  * setup before this is called.
1213c53c013bSJohn Baldwin  */
1214c53c013bSJohn Baldwin void
1215c53c013bSJohn Baldwin mutex_init(void)
1216c53c013bSJohn Baldwin {
1217c53c013bSJohn Baldwin 
1218961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
1219961a7b24SJohn Baldwin 	init_turnstiles();
1220961a7b24SJohn Baldwin 
1221c53c013bSJohn Baldwin 	/*
1222c53c013bSJohn Baldwin 	 * Initialize mutexes.
1223c53c013bSJohn Baldwin 	 */
12240c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
12252502c107SJeff Roberson 	mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
12262502c107SJeff Roberson 	blocked_lock.mtx_lock = 0xdeadc0de;	/* Always blocked. */
12270c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
12286afb32fcSKonstantin Belousov 	mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
12295c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
12305c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
12315c7bebf9SKonstantin Belousov 	mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
12328c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1233c53c013bSJohn Baldwin 	mtx_lock(&Giant);
1234c53c013bSJohn Baldwin }
1235d272fe53SJohn Baldwin 
123615140a8aSMateusz Guzik static void __noinline
123715140a8aSMateusz Guzik _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap)
123815140a8aSMateusz Guzik {
123915140a8aSMateusz Guzik 	struct thread *td;
124015140a8aSMateusz Guzik 
124115140a8aSMateusz Guzik 	ldap->spin_cnt++;
1242879e0604SMateusz Guzik 	if (ldap->spin_cnt < 60000000 || kdb_active || KERNEL_PANICKED())
12434cbbb748SJohn Baldwin 		cpu_lock_delay();
124415140a8aSMateusz Guzik 	else {
124515140a8aSMateusz Guzik 		td = mtx_owner(m);
124615140a8aSMateusz Guzik 
124715140a8aSMateusz Guzik 		/* If the mutex is unlocked, try again. */
124815140a8aSMateusz Guzik 		if (td == NULL)
124915140a8aSMateusz Guzik 			return;
125015140a8aSMateusz Guzik 
125115140a8aSMateusz Guzik 		printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
125215140a8aSMateusz Guzik 		    m, m->lock_object.lo_name, td, td->td_tid);
125315140a8aSMateusz Guzik #ifdef WITNESS
125415140a8aSMateusz Guzik 		witness_display_spinlock(&m->lock_object, td, printf);
125515140a8aSMateusz Guzik #endif
125615140a8aSMateusz Guzik 		panic("spin lock held too long");
125715140a8aSMateusz Guzik 	}
125815140a8aSMateusz Guzik 	cpu_spinwait();
125915140a8aSMateusz Guzik }
126015140a8aSMateusz Guzik 
1261d2576988SMateusz Guzik void
1262d2576988SMateusz Guzik mtx_spin_wait_unlocked(struct mtx *m)
1263d2576988SMateusz Guzik {
1264d2576988SMateusz Guzik 	struct lock_delay_arg lda;
1265d2576988SMateusz Guzik 
1266500ca73dSMateusz Guzik 	KASSERT(m->mtx_lock != MTX_DESTROYED,
1267500ca73dSMateusz Guzik 	    ("%s() of destroyed mutex %p", __func__, m));
1268500ca73dSMateusz Guzik 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
1269500ca73dSMateusz Guzik 	    ("%s() of sleep mutex %p (%s)", __func__, m,
1270500ca73dSMateusz Guzik 	    m->lock_object.lo_name));
1271500ca73dSMateusz Guzik 	KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1272500ca73dSMateusz Guzik 	    m->lock_object.lo_name));
1273500ca73dSMateusz Guzik 
1274d2576988SMateusz Guzik 	lda.spin_cnt = 0;
1275d2576988SMateusz Guzik 
1276d2576988SMateusz Guzik 	while (atomic_load_acq_ptr(&m->mtx_lock) != MTX_UNOWNED) {
1277d2576988SMateusz Guzik 		if (__predict_true(lda.spin_cnt < 10000000)) {
1278d2576988SMateusz Guzik 			cpu_spinwait();
1279d2576988SMateusz Guzik 			lda.spin_cnt++;
1280d2576988SMateusz Guzik 		} else {
1281d2576988SMateusz Guzik 			_mtx_lock_indefinite_check(m, &lda);
1282d2576988SMateusz Guzik 		}
1283d2576988SMateusz Guzik 	}
1284d2576988SMateusz Guzik }
1285d2576988SMateusz Guzik 
1286bd66a075SMateusz Guzik void
1287bd66a075SMateusz Guzik mtx_wait_unlocked(struct mtx *m)
1288bd66a075SMateusz Guzik {
1289bd66a075SMateusz Guzik 	struct thread *owner;
1290bd66a075SMateusz Guzik 	uintptr_t v;
1291bd66a075SMateusz Guzik 
1292bd66a075SMateusz Guzik 	KASSERT(m->mtx_lock != MTX_DESTROYED,
1293bd66a075SMateusz Guzik 	    ("%s() of destroyed mutex %p", __func__, m));
1294bd66a075SMateusz Guzik 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
1295bd66a075SMateusz Guzik 	    ("%s() not a sleep mutex %p (%s)", __func__, m,
1296bd66a075SMateusz Guzik 	    m->lock_object.lo_name));
1297bd66a075SMateusz Guzik 	KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1298bd66a075SMateusz Guzik 	    m->lock_object.lo_name));
1299bd66a075SMateusz Guzik 
1300bd66a075SMateusz Guzik 	for (;;) {
1301bd66a075SMateusz Guzik 		v = atomic_load_acq_ptr(&m->mtx_lock);
1302bd66a075SMateusz Guzik 		if (v == MTX_UNOWNED) {
1303bd66a075SMateusz Guzik 			break;
1304bd66a075SMateusz Guzik 		}
1305bd66a075SMateusz Guzik 		owner = lv_mtx_owner(v);
1306bd66a075SMateusz Guzik 		if (!TD_IS_RUNNING(owner)) {
1307bd66a075SMateusz Guzik 			mtx_lock(m);
1308bd66a075SMateusz Guzik 			mtx_unlock(m);
1309bd66a075SMateusz Guzik 			break;
1310bd66a075SMateusz Guzik 		}
1311bd66a075SMateusz Guzik 		cpu_spinwait();
1312bd66a075SMateusz Guzik 	}
1313bd66a075SMateusz Guzik }
1314bd66a075SMateusz Guzik 
1315d272fe53SJohn Baldwin #ifdef DDB
1316d272fe53SJohn Baldwin void
1317d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock)
1318d272fe53SJohn Baldwin {
1319d272fe53SJohn Baldwin 	struct thread *td;
1320d576deedSPawel Jakub Dawidek 	const struct mtx *m;
1321d272fe53SJohn Baldwin 
1322d576deedSPawel Jakub Dawidek 	m = (const struct mtx *)lock;
1323d272fe53SJohn Baldwin 
1324d272fe53SJohn Baldwin 	db_printf(" flags: {");
132583a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1326d272fe53SJohn Baldwin 		db_printf("SPIN");
1327d272fe53SJohn Baldwin 	else
1328d272fe53SJohn Baldwin 		db_printf("DEF");
1329aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_RECURSABLE)
1330d272fe53SJohn Baldwin 		db_printf(", RECURSE");
1331aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_DUPOK)
1332d272fe53SJohn Baldwin 		db_printf(", DUPOK");
1333d272fe53SJohn Baldwin 	db_printf("}\n");
1334d272fe53SJohn Baldwin 	db_printf(" state: {");
1335d272fe53SJohn Baldwin 	if (mtx_unowned(m))
1336d272fe53SJohn Baldwin 		db_printf("UNOWNED");
1337c0bfd703SJohn Baldwin 	else if (mtx_destroyed(m))
1338c0bfd703SJohn Baldwin 		db_printf("DESTROYED");
1339d272fe53SJohn Baldwin 	else {
1340d272fe53SJohn Baldwin 		db_printf("OWNED");
1341d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
1342d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
1343d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
1344d272fe53SJohn Baldwin 			db_printf(", RECURSED");
1345d272fe53SJohn Baldwin 	}
1346d272fe53SJohn Baldwin 	db_printf("}\n");
1347c0bfd703SJohn Baldwin 	if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1348d272fe53SJohn Baldwin 		td = mtx_owner(m);
1349d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1350431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
1351d272fe53SJohn Baldwin 		if (mtx_recursed(m))
1352d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
1353d272fe53SJohn Baldwin 	}
1354d272fe53SJohn Baldwin }
1355d272fe53SJohn Baldwin #endif
1356