xref: /freebsd/sys/kern/kern_mutex.c (revision 353705930f6982077f267795ab3b8bd7ae201b06)
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"
417c0435b9SKip Macy #include "opt_global.h"
42a5aedd68SStacey Son #include "opt_kdtrace.h"
439923b511SScott Long #include "opt_sched.h"
44a5a96a19SJohn Baldwin 
450384fff8SJason Evans #include <sys/param.h>
466c35e809SDag-Erling Smørgrav #include <sys/systm.h>
4736412d79SJohn Baldwin #include <sys/bus.h>
481126349aSPaul Saab #include <sys/conf.h>
492d50560aSMarcel Moolenaar #include <sys/kdb.h>
5036412d79SJohn Baldwin #include <sys/kernel.h>
516c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
5219284646SJohn Baldwin #include <sys/lock.h>
53fb919e4dSMark Murray #include <sys/malloc.h>
5419284646SJohn Baldwin #include <sys/mutex.h>
550384fff8SJason Evans #include <sys/proc.h>
56c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
57b43179fbSJeff Roberson #include <sys/sched.h>
586c35e809SDag-Erling Smørgrav #include <sys/sbuf.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 
79b9a80acaSStephan Uphoff /*
809ed346baSBosko Milekic  * Internal utility macros.
810cde2e34SJason Evans  */
829ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
830cde2e34SJason Evans 
84c0bfd703SJohn Baldwin #define	mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
85c0bfd703SJohn Baldwin 
8649b94bfcSJohn Baldwin #define	mtx_owner(m)	((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
879ed346baSBosko Milekic 
88d576deedSPawel Jakub Dawidek static void	assert_mtx(const struct lock_object *lock, int what);
89d272fe53SJohn Baldwin #ifdef DDB
90d576deedSPawel Jakub Dawidek static void	db_show_mtx(const struct lock_object *lock);
91d272fe53SJohn Baldwin #endif
926e21afd4SJohn Baldwin static void	lock_mtx(struct lock_object *lock, int how);
936e21afd4SJohn Baldwin static void	lock_spin(struct lock_object *lock, int how);
94a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
95d576deedSPawel Jakub Dawidek static int	owner_mtx(const struct lock_object *lock,
96d576deedSPawel Jakub Dawidek 		    struct thread **owner);
97a5aedd68SStacey Son #endif
986e21afd4SJohn Baldwin static int	unlock_mtx(struct lock_object *lock);
996e21afd4SJohn Baldwin static int	unlock_spin(struct lock_object *lock);
100d272fe53SJohn Baldwin 
1010cde2e34SJason Evans /*
10219284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
1030cde2e34SJason Evans  */
10419284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
105ae8dde30SJohn Baldwin 	.lc_name = "sleep mutex",
106ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
107f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
108d272fe53SJohn Baldwin #ifdef DDB
109ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
110d272fe53SJohn Baldwin #endif
1116e21afd4SJohn Baldwin 	.lc_lock = lock_mtx,
1126e21afd4SJohn Baldwin 	.lc_unlock = unlock_mtx,
113a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
114a5aedd68SStacey Son 	.lc_owner = owner_mtx,
115a5aedd68SStacey Son #endif
11619284646SJohn Baldwin };
11719284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
118ae8dde30SJohn Baldwin 	.lc_name = "spin mutex",
119ae8dde30SJohn Baldwin 	.lc_flags = LC_SPINLOCK | LC_RECURSABLE,
120f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
121d272fe53SJohn Baldwin #ifdef DDB
122ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
123d272fe53SJohn Baldwin #endif
1246e21afd4SJohn Baldwin 	.lc_lock = lock_spin,
1256e21afd4SJohn Baldwin 	.lc_unlock = unlock_spin,
126a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
127a5aedd68SStacey Son 	.lc_owner = owner_mtx,
128a5aedd68SStacey Son #endif
1298484de75SJohn Baldwin };
1308484de75SJohn Baldwin 
1319ed346baSBosko Milekic /*
132c53c013bSJohn Baldwin  * System-wide mutexes
133c53c013bSJohn Baldwin  */
1342502c107SJeff Roberson struct mtx blocked_lock;
135c53c013bSJohn Baldwin struct mtx Giant;
136c53c013bSJohn Baldwin 
13767784314SPoul-Henning Kamp void
138d576deedSPawel Jakub Dawidek assert_mtx(const struct lock_object *lock, int what)
139f9721b43SAttilio Rao {
140f9721b43SAttilio Rao 
141d576deedSPawel Jakub Dawidek 	mtx_assert((const struct mtx *)lock, what);
142f9721b43SAttilio Rao }
143f9721b43SAttilio Rao 
14467784314SPoul-Henning Kamp void
1456e21afd4SJohn Baldwin lock_mtx(struct lock_object *lock, int how)
1466e21afd4SJohn Baldwin {
1476e21afd4SJohn Baldwin 
1486e21afd4SJohn Baldwin 	mtx_lock((struct mtx *)lock);
1496e21afd4SJohn Baldwin }
1506e21afd4SJohn Baldwin 
15167784314SPoul-Henning Kamp void
1526e21afd4SJohn Baldwin lock_spin(struct lock_object *lock, int how)
1536e21afd4SJohn Baldwin {
1546e21afd4SJohn Baldwin 
1556e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
1566e21afd4SJohn Baldwin }
1576e21afd4SJohn Baldwin 
15867784314SPoul-Henning Kamp int
1596e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock)
1606e21afd4SJohn Baldwin {
1616e21afd4SJohn Baldwin 	struct mtx *m;
1626e21afd4SJohn Baldwin 
1636e21afd4SJohn Baldwin 	m = (struct mtx *)lock;
1646e21afd4SJohn Baldwin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
1656e21afd4SJohn Baldwin 	mtx_unlock(m);
1666e21afd4SJohn Baldwin 	return (0);
1676e21afd4SJohn Baldwin }
1686e21afd4SJohn Baldwin 
16967784314SPoul-Henning Kamp int
1706e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock)
1716e21afd4SJohn Baldwin {
1726e21afd4SJohn Baldwin 
1736e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
1746e21afd4SJohn Baldwin }
1756e21afd4SJohn Baldwin 
176a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
177a5aedd68SStacey Son int
178d576deedSPawel Jakub Dawidek owner_mtx(const struct lock_object *lock, struct thread **owner)
179a5aedd68SStacey Son {
180d576deedSPawel Jakub Dawidek 	const struct mtx *m = (const struct mtx *)lock;
181a5aedd68SStacey Son 
182a5aedd68SStacey Son 	*owner = mtx_owner(m);
183a5aedd68SStacey Son 	return (mtx_unowned(m) == 0);
184a5aedd68SStacey Son }
185a5aedd68SStacey Son #endif
186a5aedd68SStacey Son 
1870cde2e34SJason Evans /*
1886283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
1896283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
1906283b7d0SJohn Baldwin  */
1916283b7d0SJohn Baldwin void
1926283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
1936283b7d0SJohn Baldwin {
1946283b7d0SJohn Baldwin 
195*35370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
196*35370593SAndriy Gapon 		return;
197dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
198186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
199186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
200aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
201aa89d8cdSJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2020d975d63SJohn Baldwin 	    file, line));
203aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
20441313430SJohn Baldwin 	    file, line, NULL);
2057c0435b9SKip Macy 
206961135eaSJohn Baldwin 	__mtx_lock(m, curthread, opts, file, line);
207aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
208dde96c99SJohn Baldwin 	    line);
209aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
210764e4d54SJohn Baldwin 	curthread->td_locks++;
2116283b7d0SJohn Baldwin }
2126283b7d0SJohn Baldwin 
2136283b7d0SJohn Baldwin void
2146283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
2156283b7d0SJohn Baldwin {
216*35370593SAndriy Gapon 
217*35370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
218*35370593SAndriy Gapon 		return;
219dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
220186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
221186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
222aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
223aa89d8cdSJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2240d975d63SJohn Baldwin 	    file, line));
225764e4d54SJohn Baldwin 	curthread->td_locks--;
226aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
227aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
2280d975d63SJohn Baldwin 	    line);
22921377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
230c66d7606SKip Macy 
23170fe8436SKip Macy 	if (m->mtx_recurse == 0)
232a5aedd68SStacey Son 		LOCKSTAT_PROFILE_RELEASE_LOCK(LS_MTX_UNLOCK_RELEASE, m);
233961135eaSJohn Baldwin 	__mtx_unlock(m, curthread, opts, file, line);
2346283b7d0SJohn Baldwin }
2356283b7d0SJohn Baldwin 
2366283b7d0SJohn Baldwin void
2376283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
2386283b7d0SJohn Baldwin {
2396283b7d0SJohn Baldwin 
240*35370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
241*35370593SAndriy Gapon 		return;
242dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
243186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
244186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
245aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
2460d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
247aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
248ad69e26bSJohn Baldwin 	if (mtx_owned(m))
249ad69e26bSJohn Baldwin 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
250ad69e26bSJohn Baldwin 	    ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
251ad69e26bSJohn Baldwin 		    m->lock_object.lo_name, file, line));
252aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
25341313430SJohn Baldwin 	    file, line, NULL);
254961135eaSJohn Baldwin 	__mtx_lock_spin(m, curthread, opts, file, line);
255aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
256dde96c99SJohn Baldwin 	    line);
257aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
2586283b7d0SJohn Baldwin }
2596283b7d0SJohn Baldwin 
2606283b7d0SJohn Baldwin void
2616283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
2626283b7d0SJohn Baldwin {
263c66d7606SKip Macy 
264*35370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
265*35370593SAndriy Gapon 		return;
266dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
267186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
268186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
269aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
2700d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
271aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
272aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
273aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
274dde96c99SJohn Baldwin 	    line);
2750d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
276c66d7606SKip Macy 
277961135eaSJohn Baldwin 	__mtx_unlock_spin(m);
2786283b7d0SJohn Baldwin }
2796283b7d0SJohn Baldwin 
2806283b7d0SJohn Baldwin /*
2819ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
282eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
283eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
2840cde2e34SJason Evans  */
2850cde2e34SJason Evans int
286ccdf2333SAttilio Rao mtx_trylock_flags_(struct mtx *m, int opts, const char *file, int line)
2870cde2e34SJason Evans {
2881723a064SJeff Roberson #ifdef LOCK_PROFILING
2897c0435b9SKip Macy 	uint64_t waittime = 0;
2901723a064SJeff Roberson 	int contested = 0;
2911723a064SJeff Roberson #endif
2921723a064SJeff Roberson 	int rval;
2930cde2e34SJason Evans 
294*35370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
295*35370593SAndriy Gapon 		return (1);
296*35370593SAndriy Gapon 
297b40ce416SJulian Elischer 	MPASS(curthread != NULL);
298186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
299186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
300aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
301aa89d8cdSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
30283cece6fSJohn Baldwin 	    file, line));
3039ed346baSBosko Milekic 
304aa89d8cdSJohn Baldwin 	if (mtx_owned(m) && (m->lock_object.lo_flags & LO_RECURSABLE) != 0) {
305eac09796SJohn Baldwin 		m->mtx_recurse++;
306eac09796SJohn Baldwin 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
307eac09796SJohn Baldwin 		rval = 1;
308eac09796SJohn Baldwin 	} else
309961135eaSJohn Baldwin 		rval = _mtx_obtain_lock(m, (uintptr_t)curthread);
3109ed346baSBosko Milekic 
311aa89d8cdSJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
312764e4d54SJohn Baldwin 	if (rval) {
313aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
3142d96f0b1SJohn Baldwin 		    file, line);
315764e4d54SJohn Baldwin 		curthread->td_locks++;
316fe68a916SKip Macy 		if (m->mtx_recurse == 0)
317a5aedd68SStacey Son 			LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_LOCK_ACQUIRE,
318a5aedd68SStacey Son 			    m, contested, waittime, file, line);
3197c0435b9SKip Macy 
320764e4d54SJohn Baldwin 	}
3219ed346baSBosko Milekic 
32219284646SJohn Baldwin 	return (rval);
3230cde2e34SJason Evans }
3240cde2e34SJason Evans 
3250cde2e34SJason Evans /*
3269ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
3279ed346baSBosko Milekic  *
3289ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
3299ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
3300cde2e34SJason Evans  */
3310cde2e34SJason Evans void
332122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
333bdcfcf5bSJohn Baldwin     int line)
33436412d79SJohn Baldwin {
3352502c107SJeff Roberson 	struct turnstile *ts;
3361723a064SJeff Roberson 	uintptr_t v;
337cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES
33876447e56SJohn Baldwin 	volatile struct thread *owner;
3392498cf8cSJohn Baldwin #endif
34002bd1bcdSIan Dowse #ifdef KTR
34102bd1bcdSIan Dowse 	int cont_logged = 0;
34202bd1bcdSIan Dowse #endif
3431723a064SJeff Roberson #ifdef LOCK_PROFILING
34470fe8436SKip Macy 	int contested = 0;
34570fe8436SKip Macy 	uint64_t waittime = 0;
3461723a064SJeff Roberson #endif
347a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
348a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
349a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
350a5aedd68SStacey Son 	int64_t sleep_time = 0;
351a5aedd68SStacey Son #endif
35236412d79SJohn Baldwin 
353*35370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
354*35370593SAndriy Gapon 		return;
355*35370593SAndriy Gapon 
3565fa8dd90SJohn Baldwin 	if (mtx_owned(m)) {
357aa89d8cdSJohn Baldwin 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
358eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
359aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, file, line));
36036412d79SJohn Baldwin 		m->mtx_recurse++;
36108812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
362aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
3635746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
36436412d79SJohn Baldwin 		return;
36536412d79SJohn Baldwin 	}
3669ed346baSBosko Milekic 
36770fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object,
36870fe8436SKip Macy 		    &contested, &waittime);
369aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
37015ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
37115ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
372aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
3731bd0eefbSJohn Baldwin 
374961135eaSJohn Baldwin 	while (!_mtx_obtain_lock(m, tid)) {
375a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
376a5aedd68SStacey Son 		spin_cnt++;
377a5aedd68SStacey Son #endif
37849aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
37949aead8aSAttilio Rao 		/*
38049aead8aSAttilio Rao 		 * If the owner is running on another CPU, spin until the
38149aead8aSAttilio Rao 		 * owner stops running or the state of the lock changes.
38249aead8aSAttilio Rao 		 */
38349aead8aSAttilio Rao 		v = m->mtx_lock;
38449aead8aSAttilio Rao 		if (v != MTX_UNOWNED) {
38549aead8aSAttilio Rao 			owner = (struct thread *)(v & ~MTX_FLAGMASK);
38649aead8aSAttilio Rao 			if (TD_IS_RUNNING(owner)) {
38749aead8aSAttilio Rao 				if (LOCK_LOG_TEST(&m->lock_object, 0))
38849aead8aSAttilio Rao 					CTR3(KTR_LOCK,
38949aead8aSAttilio Rao 					    "%s: spinning on %p held by %p",
39049aead8aSAttilio Rao 					    __func__, m, owner);
39149aead8aSAttilio Rao 				while (mtx_owner(m) == owner &&
392a5aedd68SStacey Son 				    TD_IS_RUNNING(owner)) {
39349aead8aSAttilio Rao 					cpu_spinwait();
394a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
395a5aedd68SStacey Son 					spin_cnt++;
396a5aedd68SStacey Son #endif
397a5aedd68SStacey Son 				}
39849aead8aSAttilio Rao 				continue;
39949aead8aSAttilio Rao 			}
40049aead8aSAttilio Rao 		}
40149aead8aSAttilio Rao #endif
40249aead8aSAttilio Rao 
4032502c107SJeff Roberson 		ts = turnstile_trywait(&m->lock_object);
4045fa8dd90SJohn Baldwin 		v = m->mtx_lock;
4055fa8dd90SJohn Baldwin 
40636412d79SJohn Baldwin 		/*
4079ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
408961a7b24SJohn Baldwin 		 * the turnstile chain lock.
40936412d79SJohn Baldwin 		 */
4105fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
4112502c107SJeff Roberson 			turnstile_cancel(ts);
41236412d79SJohn Baldwin 			continue;
41336412d79SJohn Baldwin 		}
4149ed346baSBosko Milekic 
41549aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
41649aead8aSAttilio Rao 		/*
417fa29f023SJohn Baldwin 		 * The current lock owner might have started executing
418fa29f023SJohn Baldwin 		 * on another CPU (or the lock could have changed
419fa29f023SJohn Baldwin 		 * owners) while we were waiting on the turnstile
420fa29f023SJohn Baldwin 		 * chain lock.  If so, drop the turnstile lock and try
421fa29f023SJohn Baldwin 		 * again.
42249aead8aSAttilio Rao 		 */
42349aead8aSAttilio Rao 		owner = (struct thread *)(v & ~MTX_FLAGMASK);
42449aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
42549aead8aSAttilio Rao 			turnstile_cancel(ts);
42649aead8aSAttilio Rao 			continue;
42749aead8aSAttilio Rao 		}
42849aead8aSAttilio Rao #endif
42949aead8aSAttilio Rao 
43036412d79SJohn Baldwin 		/*
4319ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
4329ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
4339ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
43436412d79SJohn Baldwin 		 */
43536412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
436122eceefSJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
4372502c107SJeff Roberson 			turnstile_cancel(ts);
43836412d79SJohn Baldwin 			continue;
43936412d79SJohn Baldwin 		}
44036412d79SJohn Baldwin 
4419ed346baSBosko Milekic 		/*
4427feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
4439ed346baSBosko Milekic 		 */
44436412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
44536412d79SJohn Baldwin 
44602bd1bcdSIan Dowse #ifdef KTR
44702bd1bcdSIan Dowse 		if (!cont_logged) {
44802bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
44902bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
450aa89d8cdSJohn Baldwin 			    (void *)tid, file, line, m->lock_object.lo_name,
451aa89d8cdSJohn Baldwin 			    WITNESS_FILE(&m->lock_object),
452aa89d8cdSJohn Baldwin 			    WITNESS_LINE(&m->lock_object));
45302bd1bcdSIan Dowse 			cont_logged = 1;
45402bd1bcdSIan Dowse 		}
45502bd1bcdSIan Dowse #endif
45636412d79SJohn Baldwin 
4579ed346baSBosko Milekic 		/*
458961a7b24SJohn Baldwin 		 * Block on the turnstile.
4599ed346baSBosko Milekic 		 */
460a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
461a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
462a5aedd68SStacey Son #endif
4632502c107SJeff Roberson 		turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE);
464a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
465a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
466a5aedd68SStacey Son 		sleep_cnt++;
467a5aedd68SStacey Son #endif
46836412d79SJohn Baldwin 	}
46902bd1bcdSIan Dowse #ifdef KTR
47002bd1bcdSIan Dowse 	if (cont_logged) {
47102bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
47202bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
473aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)tid, file, line);
47402bd1bcdSIan Dowse 	}
47502bd1bcdSIan Dowse #endif
476a5aedd68SStacey Son 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_LOCK_ACQUIRE, m, contested,
477eea4f254SJeff Roberson 	    waittime, file, line);
478a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
479a5aedd68SStacey Son 	if (sleep_time)
480a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_MTX_LOCK_BLOCK, m, sleep_time);
481a5aedd68SStacey Son 
482a5aedd68SStacey Son 	/*
483a5aedd68SStacey Son 	 * Only record the loops spinning and not sleeping.
484a5aedd68SStacey Son 	 */
485a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
486a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_MTX_LOCK_SPIN, m, (spin_cnt - sleep_cnt));
487a5aedd68SStacey Son #endif
4889ed346baSBosko Milekic }
4899ed346baSBosko Milekic 
4902502c107SJeff Roberson static void
4912502c107SJeff Roberson _mtx_lock_spin_failed(struct mtx *m)
4922502c107SJeff Roberson {
4932502c107SJeff Roberson 	struct thread *td;
4942502c107SJeff Roberson 
4952502c107SJeff Roberson 	td = mtx_owner(m);
4962502c107SJeff Roberson 
4972502c107SJeff Roberson 	/* If the mutex is unlocked, try again. */
4982502c107SJeff Roberson 	if (td == NULL)
4992502c107SJeff Roberson 		return;
500b95b98b0SKonstantin Belousov 
5012502c107SJeff Roberson 	printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
5022502c107SJeff Roberson 	    m, m->lock_object.lo_name, td, td->td_tid);
5032502c107SJeff Roberson #ifdef WITNESS
50498332c8cSAttilio Rao 	witness_display_spinlock(&m->lock_object, td, printf);
5052502c107SJeff Roberson #endif
5062502c107SJeff Roberson 	panic("spin lock held too long");
5072502c107SJeff Roberson }
5082502c107SJeff Roberson 
509b95b98b0SKonstantin Belousov #ifdef SMP
5109ed346baSBosko Milekic /*
5119ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
5129ed346baSBosko Milekic  *
5139ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
5149ed346baSBosko Milekic  * is handled inline.
5159ed346baSBosko Milekic  */
5169ed346baSBosko Milekic void
517122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file,
518bdcfcf5bSJohn Baldwin     int line)
51936412d79SJohn Baldwin {
5201723a064SJeff Roberson 	int i = 0;
5211723a064SJeff Roberson #ifdef LOCK_PROFILING
5221723a064SJeff Roberson 	int contested = 0;
52370fe8436SKip Macy 	uint64_t waittime = 0;
5241723a064SJeff Roberson #endif
52536412d79SJohn Baldwin 
526*35370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
527*35370593SAndriy Gapon 		return;
528*35370593SAndriy Gapon 
529aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
5305746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
5319ed346baSBosko Milekic 
53270fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
533961135eaSJohn Baldwin 	while (!_mtx_obtain_lock(m, tid)) {
5349ed346baSBosko Milekic 
5357141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
536c6a37e84SJohn Baldwin 		spinlock_exit();
53736412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
538703fc290SJohn Baldwin 			if (i++ < 10000000) {
5399f1b87f1SMaxime Henrion 				cpu_spinwait();
54036412d79SJohn Baldwin 				continue;
541703fc290SJohn Baldwin 			}
5420fa2168bSJohn Baldwin 			if (i < 60000000 || kdb_active || panicstr != NULL)
54336412d79SJohn Baldwin 				DELAY(1);
5442502c107SJeff Roberson 			else
5452502c107SJeff Roberson 				_mtx_lock_spin_failed(m);
5469f1b87f1SMaxime Henrion 			cpu_spinwait();
54736412d79SJohn Baldwin 		}
548c6a37e84SJohn Baldwin 		spinlock_enter();
54936412d79SJohn Baldwin 	}
55036412d79SJohn Baldwin 
551aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
5529ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
5539ed346baSBosko Milekic 
554a5aedd68SStacey Son 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_SPIN_LOCK_ACQUIRE, m,
555a5aedd68SStacey Son 	    contested, waittime, (file), (line));
556a5aedd68SStacey Son 	LOCKSTAT_RECORD1(LS_MTX_SPIN_LOCK_SPIN, m, i);
55736412d79SJohn Baldwin }
55833fb8a38SJohn Baldwin #endif /* SMP */
55936412d79SJohn Baldwin 
5602502c107SJeff Roberson void
561ccdf2333SAttilio Rao thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
5622502c107SJeff Roberson {
5632502c107SJeff Roberson 	struct mtx *m;
5642502c107SJeff Roberson 	uintptr_t tid;
5651723a064SJeff Roberson 	int i;
5661723a064SJeff Roberson #ifdef LOCK_PROFILING
5671723a064SJeff Roberson 	int contested = 0;
5681723a064SJeff Roberson 	uint64_t waittime = 0;
5691723a064SJeff Roberson #endif
570a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
571a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
572a5aedd68SStacey Son #endif
5732502c107SJeff Roberson 
5741723a064SJeff Roberson 	i = 0;
5752502c107SJeff Roberson 	tid = (uintptr_t)curthread;
576*35370593SAndriy Gapon 
577*35370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
578*35370593SAndriy Gapon 		return;
579*35370593SAndriy Gapon 
5802502c107SJeff Roberson 	for (;;) {
5812502c107SJeff Roberson retry:
5822502c107SJeff Roberson 		spinlock_enter();
583710eacdcSJeff Roberson 		m = td->td_lock;
58413c85a48SJohn Baldwin 		KASSERT(m->mtx_lock != MTX_DESTROYED,
58513c85a48SJohn Baldwin 		    ("thread_lock() of destroyed mutex @ %s:%d", file, line));
58613c85a48SJohn Baldwin 		KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
58713c85a48SJohn Baldwin 		    ("thread_lock() of sleep mutex %s @ %s:%d",
58813c85a48SJohn Baldwin 		    m->lock_object.lo_name, file, line));
589ad69e26bSJohn Baldwin 		if (mtx_owned(m))
590ad69e26bSJohn Baldwin 			KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
591ad69e26bSJohn Baldwin 	    ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
592ad69e26bSJohn Baldwin 			    m->lock_object.lo_name, file, line));
5932502c107SJeff Roberson 		WITNESS_CHECKORDER(&m->lock_object,
59441313430SJohn Baldwin 		    opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
595961135eaSJohn Baldwin 		while (!_mtx_obtain_lock(m, tid)) {
596a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
597a5aedd68SStacey Son 			spin_cnt++;
598a5aedd68SStacey Son #endif
5992502c107SJeff Roberson 			if (m->mtx_lock == tid) {
6002502c107SJeff Roberson 				m->mtx_recurse++;
6012502c107SJeff Roberson 				break;
6022502c107SJeff Roberson 			}
603eea4f254SJeff Roberson 			lock_profile_obtain_lock_failed(&m->lock_object,
604eea4f254SJeff Roberson 			    &contested, &waittime);
6052502c107SJeff Roberson 			/* Give interrupts a chance while we spin. */
6062502c107SJeff Roberson 			spinlock_exit();
6072502c107SJeff Roberson 			while (m->mtx_lock != MTX_UNOWNED) {
6082502c107SJeff Roberson 				if (i++ < 10000000)
6092502c107SJeff Roberson 					cpu_spinwait();
6102502c107SJeff Roberson 				else if (i < 60000000 ||
6112502c107SJeff Roberson 				    kdb_active || panicstr != NULL)
6122502c107SJeff Roberson 					DELAY(1);
6132502c107SJeff Roberson 				else
6142502c107SJeff Roberson 					_mtx_lock_spin_failed(m);
6152502c107SJeff Roberson 				cpu_spinwait();
6162502c107SJeff Roberson 				if (m != td->td_lock)
6172502c107SJeff Roberson 					goto retry;
6182502c107SJeff Roberson 			}
6192502c107SJeff Roberson 			spinlock_enter();
6202502c107SJeff Roberson 		}
6212502c107SJeff Roberson 		if (m == td->td_lock)
6222502c107SJeff Roberson 			break;
623961135eaSJohn Baldwin 		__mtx_unlock_spin(m);	/* does spinlock_exit() */
624a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
625a5aedd68SStacey Son 		spin_cnt++;
626a5aedd68SStacey Son #endif
6272502c107SJeff Roberson 	}
628eea4f254SJeff Roberson 	if (m->mtx_recurse == 0)
629a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_SPIN_LOCK_ACQUIRE,
630a5aedd68SStacey Son 		    m, contested, waittime, (file), (line));
63113c85a48SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
63213c85a48SJohn Baldwin 	    line);
6332502c107SJeff Roberson 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
634a5aedd68SStacey Son 	LOCKSTAT_RECORD1(LS_THREAD_LOCK_SPIN, m, spin_cnt);
6352502c107SJeff Roberson }
6362502c107SJeff Roberson 
6372502c107SJeff Roberson struct mtx *
6382502c107SJeff Roberson thread_lock_block(struct thread *td)
6392502c107SJeff Roberson {
6402502c107SJeff Roberson 	struct mtx *lock;
6412502c107SJeff Roberson 
6422502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
643710eacdcSJeff Roberson 	lock = td->td_lock;
6442502c107SJeff Roberson 	td->td_lock = &blocked_lock;
6452502c107SJeff Roberson 	mtx_unlock_spin(lock);
6462502c107SJeff Roberson 
6472502c107SJeff Roberson 	return (lock);
6482502c107SJeff Roberson }
6492502c107SJeff Roberson 
6502502c107SJeff Roberson void
6512502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new)
6522502c107SJeff Roberson {
6532502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
6542502c107SJeff Roberson 	MPASS(td->td_lock == &blocked_lock);
65565d32cd8SMatt Jacob 	atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
6562502c107SJeff Roberson }
6572502c107SJeff Roberson 
6582502c107SJeff Roberson void
6592502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new)
6602502c107SJeff Roberson {
6612502c107SJeff Roberson 	struct mtx *lock;
6622502c107SJeff Roberson 
6632502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
6642502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
665710eacdcSJeff Roberson 	lock = td->td_lock;
6662502c107SJeff Roberson 	td->td_lock = new;
6672502c107SJeff Roberson 	mtx_unlock_spin(lock);
6682502c107SJeff Roberson }
6692502c107SJeff Roberson 
6709ed346baSBosko Milekic /*
6719ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
6729ed346baSBosko Milekic  *
6739ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
6749ed346baSBosko Milekic  * need to wake up a blocked thread).
6759ed346baSBosko Milekic  */
67636412d79SJohn Baldwin void
6779ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
67836412d79SJohn Baldwin {
679961a7b24SJohn Baldwin 	struct turnstile *ts;
6809ed346baSBosko Milekic 
681*35370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
682*35370593SAndriy Gapon 		return;
683*35370593SAndriy Gapon 
68408812b39SBosko Milekic 	if (mtx_recursed(m)) {
68536412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
68608812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
687aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
6889ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
68936412d79SJohn Baldwin 		return;
69036412d79SJohn Baldwin 	}
6919ed346baSBosko Milekic 
6922502c107SJeff Roberson 	/*
6932502c107SJeff Roberson 	 * We have to lock the chain before the turnstile so this turnstile
6942502c107SJeff Roberson 	 * can be removed from the hash list if it is empty.
6952502c107SJeff Roberson 	 */
6962502c107SJeff Roberson 	turnstile_chain_lock(&m->lock_object);
697aa89d8cdSJohn Baldwin 	ts = turnstile_lookup(&m->lock_object);
698aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
6999ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
700961a7b24SJohn Baldwin 	MPASS(ts != NULL);
7017aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
702961135eaSJohn Baldwin 	_mtx_release_lock_quick(m);
703bf9c6c31SJohn Baldwin 
7042502c107SJeff Roberson 	/*
7052502c107SJeff Roberson 	 * This turnstile is now no longer associated with the mutex.  We can
7062502c107SJeff Roberson 	 * unlock the chain lock so a new turnstile may take it's place.
7072502c107SJeff Roberson 	 */
7087aa4f685SJohn Baldwin 	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
7092502c107SJeff Roberson 	turnstile_chain_unlock(&m->lock_object);
7109ed346baSBosko Milekic }
7119ed346baSBosko Milekic 
7129ed346baSBosko Milekic /*
7139ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
714961135eaSJohn Baldwin  * See the __mtx_unlock_spin() macro for the details.
7159ed346baSBosko Milekic  */
7169ed346baSBosko Milekic 
7179ed346baSBosko Milekic /*
71815ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
7199ed346baSBosko Milekic  */
7201103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
7210cde2e34SJason Evans void
722d576deedSPawel Jakub Dawidek _mtx_assert(const struct mtx *m, int what, const char *file, int line)
7230cde2e34SJason Evans {
7245cb0fbe4SJohn Baldwin 
7251126349aSPaul Saab 	if (panicstr != NULL || dumping)
7265cb0fbe4SJohn Baldwin 		return;
727a10f4966SJake Burkholder 	switch (what) {
7280cde2e34SJason Evans 	case MA_OWNED:
7290cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
7300cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
731a10f4966SJake Burkholder 		if (!mtx_owned(m))
7320cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
733aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
734a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
735a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
7360cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
737aa89d8cdSJohn Baldwin 				    m->lock_object.lo_name, file, line);
738a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
7390cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
740aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
7410cde2e34SJason Evans 		}
7420cde2e34SJason Evans 		break;
7430cde2e34SJason Evans 	case MA_NOTOWNED:
744a10f4966SJake Burkholder 		if (mtx_owned(m))
7450cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
746aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
7470cde2e34SJason Evans 		break;
7480cde2e34SJason Evans 	default:
74956771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
7500cde2e34SJason Evans 	}
7510cde2e34SJason Evans }
7520cde2e34SJason Evans #endif
7530cde2e34SJason Evans 
7549ed346baSBosko Milekic /*
7559ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
75619284646SJohn Baldwin  *
75719284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
75819284646SJohn Baldwin  * maintained by the witness code.
7599ed346baSBosko Milekic  */
76036412d79SJohn Baldwin #ifdef MUTEX_DEBUG
76136412d79SJohn Baldwin 
7624d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
76336412d79SJohn Baldwin 
76419284646SJohn Baldwin void
76519284646SJohn Baldwin mtx_validate(struct mtx *m)
76636412d79SJohn Baldwin {
76736412d79SJohn Baldwin 
76836412d79SJohn Baldwin /*
769fa669ab7SPoul-Henning Kamp  * XXX: When kernacc() does not require Giant we can reenable this check
770fa669ab7SPoul-Henning Kamp  */
771fa669ab7SPoul-Henning Kamp #ifdef notyet
772fa669ab7SPoul-Henning Kamp 	/*
77376dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
77476dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
77576dcbd6fSBosko Milekic 	 * requires Giant itself.
77676dcbd6fSBosko Milekic 	 */
777ab07087eSBosko Milekic 	if (!cold)
778ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
779ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
78019284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
78136412d79SJohn Baldwin #endif
78236412d79SJohn Baldwin }
78336412d79SJohn Baldwin #endif
78436412d79SJohn Baldwin 
7859ed346baSBosko Milekic /*
786c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
787c27b5699SAndrew R. Reiter  */
788c27b5699SAndrew R. Reiter void
789c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
790c27b5699SAndrew R. Reiter {
791c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
792c27b5699SAndrew R. Reiter 
7930c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
794c27b5699SAndrew R. Reiter }
795c27b5699SAndrew R. Reiter 
796c27b5699SAndrew R. Reiter /*
7979ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
7980c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
7990c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
8000c88508aSJohn Baldwin  * witness.
8019ed346baSBosko Milekic  */
80236412d79SJohn Baldwin void
8030c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
80436412d79SJohn Baldwin {
80583a81bcbSJohn Baldwin 	struct lock_class *class;
80683a81bcbSJohn Baldwin 	int flags;
8079ed346baSBosko Milekic 
80819284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
8097c0435b9SKip Macy 		MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0);
810353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
811353998acSAttilio Rao 	    ("%s: mtx_lock not aligned for %s: %p", __func__, name,
812353998acSAttilio Rao 	    &m->mtx_lock));
8139ed346baSBosko Milekic 
81436412d79SJohn Baldwin #ifdef MUTEX_DEBUG
8159ed346baSBosko Milekic 	/* Diagnostic and error correction */
81619284646SJohn Baldwin 	mtx_validate(m);
8176936206eSJohn Baldwin #endif
81836412d79SJohn Baldwin 
81983a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
82019284646SJohn Baldwin 	if (opts & MTX_SPIN)
82183a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
82219284646SJohn Baldwin 	else
82383a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
82483a81bcbSJohn Baldwin 	flags = 0;
82519284646SJohn Baldwin 	if (opts & MTX_QUIET)
82683a81bcbSJohn Baldwin 		flags |= LO_QUIET;
82719284646SJohn Baldwin 	if (opts & MTX_RECURSE)
82883a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
82919284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
83083a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
831f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
83283a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
8337c0435b9SKip Macy 	if (opts & MTX_NOPROFILE)
8347c0435b9SKip Macy 		flags |= LO_NOPROFILE;
83519284646SJohn Baldwin 
83683a81bcbSJohn Baldwin 	/* Initialize mutex. */
83719284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
83883a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
8399ed346baSBosko Milekic 
840aa89d8cdSJohn Baldwin 	lock_init(&m->lock_object, class, name, type, flags);
84136412d79SJohn Baldwin }
84236412d79SJohn Baldwin 
8439ed346baSBosko Milekic /*
84419284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
84519284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
84619284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
84719284646SJohn Baldwin  * flags.
8489ed346baSBosko Milekic  */
84936412d79SJohn Baldwin void
85036412d79SJohn Baldwin mtx_destroy(struct mtx *m)
85136412d79SJohn Baldwin {
85236412d79SJohn Baldwin 
85319284646SJohn Baldwin 	if (!mtx_owned(m))
85419284646SJohn Baldwin 		MPASS(mtx_unowned(m));
85519284646SJohn Baldwin 	else {
85608812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
8579ed346baSBosko Milekic 
858861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
859aa89d8cdSJohn Baldwin 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
860861a2308SScott Long 			spinlock_exit();
861764e4d54SJohn Baldwin 		else
862764e4d54SJohn Baldwin 			curthread->td_locks--;
863861a2308SScott Long 
864d3df4af3SJeff Roberson 		lock_profile_release_lock(&m->lock_object);
86519284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
866aa89d8cdSJohn Baldwin 		WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
867c86b6ff5SJohn Baldwin 		    __LINE__);
86836412d79SJohn Baldwin 	}
8690384fff8SJason Evans 
870186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
871aa89d8cdSJohn Baldwin 	lock_destroy(&m->lock_object);
8720384fff8SJason Evans }
873d23f5958SMatthew Dillon 
874d23f5958SMatthew Dillon /*
875c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
876c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
877c53c013bSJohn Baldwin  * setup before this is called.
878c53c013bSJohn Baldwin  */
879c53c013bSJohn Baldwin void
880c53c013bSJohn Baldwin mutex_init(void)
881c53c013bSJohn Baldwin {
882c53c013bSJohn Baldwin 
883961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
884961a7b24SJohn Baldwin 	init_turnstiles();
885961a7b24SJohn Baldwin 
886c53c013bSJohn Baldwin 	/*
887c53c013bSJohn Baldwin 	 * Initialize mutexes.
888c53c013bSJohn Baldwin 	 */
8890c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
8902502c107SJeff Roberson 	mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
8912502c107SJeff Roberson 	blocked_lock.mtx_lock = 0xdeadc0de;	/* Always blocked. */
8920c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
8932502c107SJeff Roberson 	mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
8948c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
895c53c013bSJohn Baldwin 	mtx_lock(&Giant);
896c53c013bSJohn Baldwin }
897d272fe53SJohn Baldwin 
898d272fe53SJohn Baldwin #ifdef DDB
899d272fe53SJohn Baldwin void
900d576deedSPawel Jakub Dawidek db_show_mtx(const struct lock_object *lock)
901d272fe53SJohn Baldwin {
902d272fe53SJohn Baldwin 	struct thread *td;
903d576deedSPawel Jakub Dawidek 	const struct mtx *m;
904d272fe53SJohn Baldwin 
905d576deedSPawel Jakub Dawidek 	m = (const struct mtx *)lock;
906d272fe53SJohn Baldwin 
907d272fe53SJohn Baldwin 	db_printf(" flags: {");
90883a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
909d272fe53SJohn Baldwin 		db_printf("SPIN");
910d272fe53SJohn Baldwin 	else
911d272fe53SJohn Baldwin 		db_printf("DEF");
912aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_RECURSABLE)
913d272fe53SJohn Baldwin 		db_printf(", RECURSE");
914aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_DUPOK)
915d272fe53SJohn Baldwin 		db_printf(", DUPOK");
916d272fe53SJohn Baldwin 	db_printf("}\n");
917d272fe53SJohn Baldwin 	db_printf(" state: {");
918d272fe53SJohn Baldwin 	if (mtx_unowned(m))
919d272fe53SJohn Baldwin 		db_printf("UNOWNED");
920c0bfd703SJohn Baldwin 	else if (mtx_destroyed(m))
921c0bfd703SJohn Baldwin 		db_printf("DESTROYED");
922d272fe53SJohn Baldwin 	else {
923d272fe53SJohn Baldwin 		db_printf("OWNED");
924d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
925d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
926d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
927d272fe53SJohn Baldwin 			db_printf(", RECURSED");
928d272fe53SJohn Baldwin 	}
929d272fe53SJohn Baldwin 	db_printf("}\n");
930c0bfd703SJohn Baldwin 	if (!mtx_unowned(m) && !mtx_destroyed(m)) {
931d272fe53SJohn Baldwin 		td = mtx_owner(m);
932d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
933431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
934d272fe53SJohn Baldwin 		if (mtx_recursed(m))
935d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
936d272fe53SJohn Baldwin 	}
937d272fe53SJohn Baldwin }
938d272fe53SJohn Baldwin #endif
939