xref: /freebsd/sys/kern/kern_mutex.c (revision eea4f254fe8f66e4d58b9befe435010a2d485423)
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"
429923b511SScott Long #include "opt_sched.h"
43a5a96a19SJohn Baldwin 
440384fff8SJason Evans #include <sys/param.h>
456c35e809SDag-Erling Smørgrav #include <sys/systm.h>
4636412d79SJohn Baldwin #include <sys/bus.h>
471126349aSPaul Saab #include <sys/conf.h>
482d50560aSMarcel Moolenaar #include <sys/kdb.h>
4936412d79SJohn Baldwin #include <sys/kernel.h>
506c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
5119284646SJohn Baldwin #include <sys/lock.h>
52fb919e4dSMark Murray #include <sys/malloc.h>
5319284646SJohn Baldwin #include <sys/mutex.h>
540384fff8SJason Evans #include <sys/proc.h>
55c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
56b43179fbSJeff Roberson #include <sys/sched.h>
576c35e809SDag-Erling Smørgrav #include <sys/sbuf.h>
58a5a96a19SJohn Baldwin #include <sys/sysctl.h>
59961a7b24SJohn Baldwin #include <sys/turnstile.h>
6036412d79SJohn Baldwin #include <sys/vmmeter.h>
617c0435b9SKip Macy #include <sys/lock_profile.h>
620384fff8SJason Evans 
6336412d79SJohn Baldwin #include <machine/atomic.h>
6436412d79SJohn Baldwin #include <machine/bus.h>
650384fff8SJason Evans #include <machine/cpu.h>
6636412d79SJohn Baldwin 
679c36c934SJohn Baldwin #include <ddb/ddb.h>
689c36c934SJohn Baldwin 
698c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h>
708c4b6380SJohn Baldwin 
7136412d79SJohn Baldwin #include <vm/vm.h>
7236412d79SJohn Baldwin #include <vm/vm_extern.h>
7336412d79SJohn Baldwin 
74cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
75cd6e6e4eSJohn Baldwin #define	ADAPTIVE_MUTEXES
76cd6e6e4eSJohn Baldwin #endif
77cd6e6e4eSJohn Baldwin 
78b9a80acaSStephan Uphoff /*
799ed346baSBosko Milekic  * Internal utility macros.
800cde2e34SJason Evans  */
819ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
820cde2e34SJason Evans 
83c0bfd703SJohn Baldwin #define	mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
84c0bfd703SJohn Baldwin 
8549b94bfcSJohn Baldwin #define	mtx_owner(m)	((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
869ed346baSBosko Milekic 
87f9721b43SAttilio Rao static void	assert_mtx(struct lock_object *lock, int what);
88d272fe53SJohn Baldwin #ifdef DDB
89d272fe53SJohn Baldwin static void	db_show_mtx(struct lock_object *lock);
90d272fe53SJohn Baldwin #endif
916e21afd4SJohn Baldwin static void	lock_mtx(struct lock_object *lock, int how);
926e21afd4SJohn Baldwin static void	lock_spin(struct lock_object *lock, int how);
936e21afd4SJohn Baldwin static int	unlock_mtx(struct lock_object *lock);
946e21afd4SJohn Baldwin static int	unlock_spin(struct lock_object *lock);
95d272fe53SJohn Baldwin 
960cde2e34SJason Evans /*
9719284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
980cde2e34SJason Evans  */
9919284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
100ae8dde30SJohn Baldwin 	.lc_name = "sleep mutex",
101ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
102f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
103d272fe53SJohn Baldwin #ifdef DDB
104ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
105d272fe53SJohn Baldwin #endif
1066e21afd4SJohn Baldwin 	.lc_lock = lock_mtx,
1076e21afd4SJohn Baldwin 	.lc_unlock = unlock_mtx,
10819284646SJohn Baldwin };
10919284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
110ae8dde30SJohn Baldwin 	.lc_name = "spin mutex",
111ae8dde30SJohn Baldwin 	.lc_flags = LC_SPINLOCK | LC_RECURSABLE,
112f9721b43SAttilio Rao 	.lc_assert = assert_mtx,
113d272fe53SJohn Baldwin #ifdef DDB
114ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
115d272fe53SJohn Baldwin #endif
1166e21afd4SJohn Baldwin 	.lc_lock = lock_spin,
1176e21afd4SJohn Baldwin 	.lc_unlock = unlock_spin,
1188484de75SJohn Baldwin };
1198484de75SJohn Baldwin 
1209ed346baSBosko Milekic /*
121c53c013bSJohn Baldwin  * System-wide mutexes
122c53c013bSJohn Baldwin  */
1232502c107SJeff Roberson struct mtx blocked_lock;
124c53c013bSJohn Baldwin struct mtx Giant;
125c53c013bSJohn Baldwin 
1266e21afd4SJohn Baldwin void
127f9721b43SAttilio Rao assert_mtx(struct lock_object *lock, int what)
128f9721b43SAttilio Rao {
129f9721b43SAttilio Rao 
130f9721b43SAttilio Rao 	mtx_assert((struct mtx *)lock, what);
131f9721b43SAttilio Rao }
132f9721b43SAttilio Rao 
133f9721b43SAttilio Rao void
1346e21afd4SJohn Baldwin lock_mtx(struct lock_object *lock, int how)
1356e21afd4SJohn Baldwin {
1366e21afd4SJohn Baldwin 
1376e21afd4SJohn Baldwin 	mtx_lock((struct mtx *)lock);
1386e21afd4SJohn Baldwin }
1396e21afd4SJohn Baldwin 
1406e21afd4SJohn Baldwin void
1416e21afd4SJohn Baldwin lock_spin(struct lock_object *lock, int how)
1426e21afd4SJohn Baldwin {
1436e21afd4SJohn Baldwin 
1446e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
1456e21afd4SJohn Baldwin }
1466e21afd4SJohn Baldwin 
1476e21afd4SJohn Baldwin int
1486e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock)
1496e21afd4SJohn Baldwin {
1506e21afd4SJohn Baldwin 	struct mtx *m;
1516e21afd4SJohn Baldwin 
1526e21afd4SJohn Baldwin 	m = (struct mtx *)lock;
1536e21afd4SJohn Baldwin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
1546e21afd4SJohn Baldwin 	mtx_unlock(m);
1556e21afd4SJohn Baldwin 	return (0);
1566e21afd4SJohn Baldwin }
1576e21afd4SJohn Baldwin 
1586e21afd4SJohn Baldwin int
1596e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock)
1606e21afd4SJohn Baldwin {
1616e21afd4SJohn Baldwin 
1626e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
1636e21afd4SJohn Baldwin }
1646e21afd4SJohn Baldwin 
1650cde2e34SJason Evans /*
1666283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
1676283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
1686283b7d0SJohn Baldwin  */
1696283b7d0SJohn Baldwin void
1706283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
1716283b7d0SJohn Baldwin {
1726283b7d0SJohn Baldwin 
173dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
174186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
175186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
176aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
177aa89d8cdSJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
1780d975d63SJohn Baldwin 	    file, line));
179aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
1808d768e76SJohn Baldwin 	    file, line);
1817c0435b9SKip Macy 
182dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
183aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
184dde96c99SJohn Baldwin 	    line);
185aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
186764e4d54SJohn Baldwin 	curthread->td_locks++;
1876283b7d0SJohn Baldwin }
1886283b7d0SJohn Baldwin 
1896283b7d0SJohn Baldwin void
1906283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
1916283b7d0SJohn Baldwin {
192dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
193186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
194186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
195aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
196aa89d8cdSJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
1970d975d63SJohn Baldwin 	    file, line));
198764e4d54SJohn Baldwin 	curthread->td_locks--;
199aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
200aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
2010d975d63SJohn Baldwin 	    line);
20221377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
203c66d7606SKip Macy 
20470fe8436SKip Macy 	if (m->mtx_recurse == 0)
205aa89d8cdSJohn Baldwin 		lock_profile_release_lock(&m->lock_object);
206dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
2076283b7d0SJohn Baldwin }
2086283b7d0SJohn Baldwin 
2096283b7d0SJohn Baldwin void
2106283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
2116283b7d0SJohn Baldwin {
2126283b7d0SJohn Baldwin 
213dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
214186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
215186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
216aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
2170d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
218aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
219aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
2208d768e76SJohn Baldwin 	    file, line);
221dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
222aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
223dde96c99SJohn Baldwin 	    line);
224aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
2256283b7d0SJohn Baldwin }
2266283b7d0SJohn Baldwin 
2276283b7d0SJohn Baldwin void
2286283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
2296283b7d0SJohn Baldwin {
230c66d7606SKip Macy 
231dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
232186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
233186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
234aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
2350d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
236aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
237aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
238aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
239dde96c99SJohn Baldwin 	    line);
2400d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
241c66d7606SKip Macy 
242dde96c99SJohn Baldwin 	_rel_spin_lock(m);
2436283b7d0SJohn Baldwin }
2446283b7d0SJohn Baldwin 
2456283b7d0SJohn Baldwin /*
2469ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
247eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
248eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
2490cde2e34SJason Evans  */
2500cde2e34SJason Evans int
2519ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
2520cde2e34SJason Evans {
253fe68a916SKip Macy 	int rval, contested = 0;
2547c0435b9SKip Macy 	uint64_t waittime = 0;
2550cde2e34SJason Evans 
256b40ce416SJulian Elischer 	MPASS(curthread != NULL);
257186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
258186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
259aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
260aa89d8cdSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
26183cece6fSJohn Baldwin 	    file, line));
2629ed346baSBosko Milekic 
263aa89d8cdSJohn Baldwin 	if (mtx_owned(m) && (m->lock_object.lo_flags & LO_RECURSABLE) != 0) {
264eac09796SJohn Baldwin 		m->mtx_recurse++;
265eac09796SJohn Baldwin 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
266eac09796SJohn Baldwin 		rval = 1;
267eac09796SJohn Baldwin 	} else
268122eceefSJohn Baldwin 		rval = _obtain_lock(m, (uintptr_t)curthread);
2699ed346baSBosko Milekic 
270aa89d8cdSJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
271764e4d54SJohn Baldwin 	if (rval) {
272aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
2732d96f0b1SJohn Baldwin 		    file, line);
274764e4d54SJohn Baldwin 		curthread->td_locks++;
275fe68a916SKip Macy 		if (m->mtx_recurse == 0)
276aa89d8cdSJohn Baldwin 			lock_profile_obtain_lock_success(&m->lock_object, contested,
277fe68a916SKip Macy 			    waittime, file, line);
2787c0435b9SKip Macy 
279764e4d54SJohn Baldwin 	}
2809ed346baSBosko Milekic 
28119284646SJohn Baldwin 	return (rval);
2820cde2e34SJason Evans }
2830cde2e34SJason Evans 
2840cde2e34SJason Evans /*
2859ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
2869ed346baSBosko Milekic  *
2879ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
2889ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
2890cde2e34SJason Evans  */
2900cde2e34SJason Evans void
291122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
292bdcfcf5bSJohn Baldwin     int line)
29336412d79SJohn Baldwin {
2942502c107SJeff Roberson 	struct turnstile *ts;
295cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES
29676447e56SJohn Baldwin 	volatile struct thread *owner;
2972498cf8cSJohn Baldwin #endif
29802bd1bcdSIan Dowse #ifdef KTR
29902bd1bcdSIan Dowse 	int cont_logged = 0;
30002bd1bcdSIan Dowse #endif
30170fe8436SKip Macy 	int contested = 0;
30270fe8436SKip Macy 	uint64_t waittime = 0;
3037c0435b9SKip Macy 	uintptr_t v;
30436412d79SJohn Baldwin 
3055fa8dd90SJohn Baldwin 	if (mtx_owned(m)) {
306aa89d8cdSJohn Baldwin 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
307eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
308aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, file, line));
30936412d79SJohn Baldwin 		m->mtx_recurse++;
31008812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
311aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
3125746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
31336412d79SJohn Baldwin 		return;
31436412d79SJohn Baldwin 	}
3159ed346baSBosko Milekic 
31670fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object,
31770fe8436SKip Macy 		    &contested, &waittime);
318aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
31915ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
32015ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
321aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
3221bd0eefbSJohn Baldwin 
323122eceefSJohn Baldwin 	while (!_obtain_lock(m, tid)) {
32449aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
32549aead8aSAttilio Rao 		/*
32649aead8aSAttilio Rao 		 * If the owner is running on another CPU, spin until the
32749aead8aSAttilio Rao 		 * owner stops running or the state of the lock changes.
32849aead8aSAttilio Rao 		 */
32949aead8aSAttilio Rao 		v = m->mtx_lock;
33049aead8aSAttilio Rao 		if (v != MTX_UNOWNED) {
33149aead8aSAttilio Rao 			owner = (struct thread *)(v & ~MTX_FLAGMASK);
33249aead8aSAttilio Rao 			if (TD_IS_RUNNING(owner)) {
33349aead8aSAttilio Rao 				if (LOCK_LOG_TEST(&m->lock_object, 0))
33449aead8aSAttilio Rao 					CTR3(KTR_LOCK,
33549aead8aSAttilio Rao 					    "%s: spinning on %p held by %p",
33649aead8aSAttilio Rao 					    __func__, m, owner);
33749aead8aSAttilio Rao 				while (mtx_owner(m) == owner &&
33849aead8aSAttilio Rao 				    TD_IS_RUNNING(owner))
33949aead8aSAttilio Rao 					cpu_spinwait();
34049aead8aSAttilio Rao 				continue;
34149aead8aSAttilio Rao 			}
34249aead8aSAttilio Rao 		}
34349aead8aSAttilio Rao #endif
34449aead8aSAttilio Rao 
3452502c107SJeff Roberson 		ts = turnstile_trywait(&m->lock_object);
3465fa8dd90SJohn Baldwin 		v = m->mtx_lock;
3475fa8dd90SJohn Baldwin 
34836412d79SJohn Baldwin 		/*
3499ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
350961a7b24SJohn Baldwin 		 * the turnstile chain lock.
35136412d79SJohn Baldwin 		 */
3525fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
3532502c107SJeff Roberson 			turnstile_cancel(ts);
3549f1b87f1SMaxime Henrion 			cpu_spinwait();
35536412d79SJohn Baldwin 			continue;
35636412d79SJohn Baldwin 		}
3579ed346baSBosko Milekic 
358535eb309SJohn Baldwin 		MPASS(v != MTX_CONTESTED);
3599ed346baSBosko Milekic 
36049aead8aSAttilio Rao #ifdef ADAPTIVE_MUTEXES
36149aead8aSAttilio Rao 		/*
36249aead8aSAttilio Rao 		 * If the current owner of the lock is executing on another
36349aead8aSAttilio Rao 		 * CPU quit the hard path and try to spin.
36449aead8aSAttilio Rao 		 */
36549aead8aSAttilio Rao 		owner = (struct thread *)(v & ~MTX_FLAGMASK);
36649aead8aSAttilio Rao 		if (TD_IS_RUNNING(owner)) {
36749aead8aSAttilio Rao 			turnstile_cancel(ts);
36849aead8aSAttilio Rao 			cpu_spinwait();
36949aead8aSAttilio Rao 			continue;
37049aead8aSAttilio Rao 		}
37149aead8aSAttilio Rao #endif
37249aead8aSAttilio Rao 
37336412d79SJohn Baldwin 		/*
3749ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
3759ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
3769ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
37736412d79SJohn Baldwin 		 */
37836412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
379122eceefSJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
3802502c107SJeff Roberson 			turnstile_cancel(ts);
3819f1b87f1SMaxime Henrion 			cpu_spinwait();
38236412d79SJohn Baldwin 			continue;
38336412d79SJohn Baldwin 		}
38436412d79SJohn Baldwin 
3859ed346baSBosko Milekic 		/*
3867feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
3879ed346baSBosko Milekic 		 */
38836412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
38936412d79SJohn Baldwin 
39002bd1bcdSIan Dowse #ifdef KTR
39102bd1bcdSIan Dowse 		if (!cont_logged) {
39202bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
39302bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
394aa89d8cdSJohn Baldwin 			    (void *)tid, file, line, m->lock_object.lo_name,
395aa89d8cdSJohn Baldwin 			    WITNESS_FILE(&m->lock_object),
396aa89d8cdSJohn Baldwin 			    WITNESS_LINE(&m->lock_object));
39702bd1bcdSIan Dowse 			cont_logged = 1;
39802bd1bcdSIan Dowse 		}
39902bd1bcdSIan Dowse #endif
40036412d79SJohn Baldwin 
4019ed346baSBosko Milekic 		/*
402961a7b24SJohn Baldwin 		 * Block on the turnstile.
4039ed346baSBosko Milekic 		 */
4042502c107SJeff Roberson 		turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE);
40536412d79SJohn Baldwin 	}
40602bd1bcdSIan Dowse #ifdef KTR
40702bd1bcdSIan Dowse 	if (cont_logged) {
40802bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
40902bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
410aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)tid, file, line);
41102bd1bcdSIan Dowse 	}
41202bd1bcdSIan Dowse #endif
41370fe8436SKip Macy 	lock_profile_obtain_lock_success(&m->lock_object, contested,
414eea4f254SJeff Roberson 	    waittime, file, line);
4159ed346baSBosko Milekic }
4169ed346baSBosko Milekic 
4172502c107SJeff Roberson static void
4182502c107SJeff Roberson _mtx_lock_spin_failed(struct mtx *m)
4192502c107SJeff Roberson {
4202502c107SJeff Roberson 	struct thread *td;
4212502c107SJeff Roberson 
4222502c107SJeff Roberson 	td = mtx_owner(m);
4232502c107SJeff Roberson 
4242502c107SJeff Roberson 	/* If the mutex is unlocked, try again. */
4252502c107SJeff Roberson 	if (td == NULL)
4262502c107SJeff Roberson 		return;
427b95b98b0SKonstantin Belousov 
4282502c107SJeff Roberson 	printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
4292502c107SJeff Roberson 	    m, m->lock_object.lo_name, td, td->td_tid);
4302502c107SJeff Roberson #ifdef WITNESS
4312502c107SJeff Roberson 	witness_display_spinlock(&m->lock_object, td);
4322502c107SJeff Roberson #endif
4332502c107SJeff Roberson 	panic("spin lock held too long");
4342502c107SJeff Roberson }
4352502c107SJeff Roberson 
436b95b98b0SKonstantin Belousov #ifdef SMP
4379ed346baSBosko Milekic /*
4389ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
4399ed346baSBosko Milekic  *
4409ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
4419ed346baSBosko Milekic  * is handled inline.
4429ed346baSBosko Milekic  */
4439ed346baSBosko Milekic void
444122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file,
445bdcfcf5bSJohn Baldwin     int line)
44636412d79SJohn Baldwin {
44770fe8436SKip Macy 	int i = 0, contested = 0;
44870fe8436SKip Macy 	uint64_t waittime = 0;
44936412d79SJohn Baldwin 
450aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
4515746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
4529ed346baSBosko Milekic 
45370fe8436SKip Macy 	lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
454f781b5a4SJohn Baldwin 	while (!_obtain_lock(m, tid)) {
4559ed346baSBosko Milekic 
4567141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
457c6a37e84SJohn Baldwin 		spinlock_exit();
45836412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
459703fc290SJohn Baldwin 			if (i++ < 10000000) {
4609f1b87f1SMaxime Henrion 				cpu_spinwait();
46136412d79SJohn Baldwin 				continue;
462703fc290SJohn Baldwin 			}
4630fa2168bSJohn Baldwin 			if (i < 60000000 || kdb_active || panicstr != NULL)
46436412d79SJohn Baldwin 				DELAY(1);
4652502c107SJeff Roberson 			else
4662502c107SJeff Roberson 				_mtx_lock_spin_failed(m);
4679f1b87f1SMaxime Henrion 			cpu_spinwait();
46836412d79SJohn Baldwin 		}
469c6a37e84SJohn Baldwin 		spinlock_enter();
47036412d79SJohn Baldwin 	}
47136412d79SJohn Baldwin 
472aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
4739ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
4749ed346baSBosko Milekic 
47570fe8436SKip Macy 	lock_profile_obtain_lock_success(&m->lock_object, contested,
47670fe8436SKip Macy 	    waittime, (file), (line));
47736412d79SJohn Baldwin }
47833fb8a38SJohn Baldwin #endif /* SMP */
47936412d79SJohn Baldwin 
4802502c107SJeff Roberson void
4812502c107SJeff Roberson _thread_lock_flags(struct thread *td, int opts, const char *file, int line)
4822502c107SJeff Roberson {
4832502c107SJeff Roberson 	struct mtx *m;
4842502c107SJeff Roberson 	uintptr_t tid;
485773890b9SJeff Roberson 	int i, contested;
486773890b9SJeff Roberson 	uint64_t waittime;
4872502c107SJeff Roberson 
488773890b9SJeff Roberson 
489773890b9SJeff Roberson 	contested = i = 0;
490773890b9SJeff Roberson 	waittime = 0;
4912502c107SJeff Roberson 	tid = (uintptr_t)curthread;
4922502c107SJeff Roberson 	for (;;) {
4932502c107SJeff Roberson retry:
4942502c107SJeff Roberson 		spinlock_enter();
495710eacdcSJeff Roberson 		m = td->td_lock;
4962502c107SJeff Roberson 		WITNESS_CHECKORDER(&m->lock_object,
4972502c107SJeff Roberson 		    opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line);
4982502c107SJeff Roberson 		while (!_obtain_lock(m, tid)) {
4992502c107SJeff Roberson 			if (m->mtx_lock == tid) {
5002502c107SJeff Roberson 				m->mtx_recurse++;
5012502c107SJeff Roberson 				break;
5022502c107SJeff Roberson 			}
503eea4f254SJeff Roberson 			lock_profile_obtain_lock_failed(&m->lock_object,
504eea4f254SJeff Roberson 			    &contested, &waittime);
5052502c107SJeff Roberson 			/* Give interrupts a chance while we spin. */
5062502c107SJeff Roberson 			spinlock_exit();
5072502c107SJeff Roberson 			while (m->mtx_lock != MTX_UNOWNED) {
5082502c107SJeff Roberson 				if (i++ < 10000000)
5092502c107SJeff Roberson 					cpu_spinwait();
5102502c107SJeff Roberson 				else if (i < 60000000 ||
5112502c107SJeff Roberson 				    kdb_active || panicstr != NULL)
5122502c107SJeff Roberson 					DELAY(1);
5132502c107SJeff Roberson 				else
5142502c107SJeff Roberson 					_mtx_lock_spin_failed(m);
5152502c107SJeff Roberson 				cpu_spinwait();
5162502c107SJeff Roberson 				if (m != td->td_lock)
5172502c107SJeff Roberson 					goto retry;
5182502c107SJeff Roberson 			}
5192502c107SJeff Roberson 			spinlock_enter();
5202502c107SJeff Roberson 		}
5212502c107SJeff Roberson 		if (m == td->td_lock)
5222502c107SJeff Roberson 			break;
5232502c107SJeff Roberson 		_rel_spin_lock(m);	/* does spinlock_exit() */
5242502c107SJeff Roberson 	}
525eea4f254SJeff Roberson 	if (m->mtx_recurse == 0)
526773890b9SJeff Roberson 		lock_profile_obtain_lock_success(&m->lock_object, contested,
527773890b9SJeff Roberson 		    waittime, (file), (line));
5282502c107SJeff Roberson 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
5292502c107SJeff Roberson }
5302502c107SJeff Roberson 
5312502c107SJeff Roberson struct mtx *
5322502c107SJeff Roberson thread_lock_block(struct thread *td)
5332502c107SJeff Roberson {
5342502c107SJeff Roberson 	struct mtx *lock;
5352502c107SJeff Roberson 
5362502c107SJeff Roberson 	spinlock_enter();
5372502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
538710eacdcSJeff Roberson 	lock = td->td_lock;
5392502c107SJeff Roberson 	td->td_lock = &blocked_lock;
5402502c107SJeff Roberson 	mtx_unlock_spin(lock);
5412502c107SJeff Roberson 
5422502c107SJeff Roberson 	return (lock);
5432502c107SJeff Roberson }
5442502c107SJeff Roberson 
5452502c107SJeff Roberson void
5462502c107SJeff Roberson thread_lock_unblock(struct thread *td, struct mtx *new)
5472502c107SJeff Roberson {
5482502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
5492502c107SJeff Roberson 	MPASS(td->td_lock == &blocked_lock);
55065d32cd8SMatt Jacob 	atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
5512502c107SJeff Roberson 	spinlock_exit();
5522502c107SJeff Roberson }
5532502c107SJeff Roberson 
5542502c107SJeff Roberson void
5552502c107SJeff Roberson thread_lock_set(struct thread *td, struct mtx *new)
5562502c107SJeff Roberson {
5572502c107SJeff Roberson 	struct mtx *lock;
5582502c107SJeff Roberson 
5592502c107SJeff Roberson 	mtx_assert(new, MA_OWNED);
5602502c107SJeff Roberson 	THREAD_LOCK_ASSERT(td, MA_OWNED);
561710eacdcSJeff Roberson 	lock = td->td_lock;
5622502c107SJeff Roberson 	td->td_lock = new;
5632502c107SJeff Roberson 	mtx_unlock_spin(lock);
5642502c107SJeff Roberson }
5652502c107SJeff Roberson 
5669ed346baSBosko Milekic /*
5679ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
5689ed346baSBosko Milekic  *
5699ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
5709ed346baSBosko Milekic  * need to wake up a blocked thread).
5719ed346baSBosko Milekic  */
57236412d79SJohn Baldwin void
5739ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
57436412d79SJohn Baldwin {
575961a7b24SJohn Baldwin 	struct turnstile *ts;
5769ed346baSBosko Milekic 
57708812b39SBosko Milekic 	if (mtx_recursed(m)) {
57836412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
57908812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
580aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
5819ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
58236412d79SJohn Baldwin 		return;
58336412d79SJohn Baldwin 	}
5849ed346baSBosko Milekic 
5852502c107SJeff Roberson 	/*
5862502c107SJeff Roberson 	 * We have to lock the chain before the turnstile so this turnstile
5872502c107SJeff Roberson 	 * can be removed from the hash list if it is empty.
5882502c107SJeff Roberson 	 */
5892502c107SJeff Roberson 	turnstile_chain_lock(&m->lock_object);
590aa89d8cdSJohn Baldwin 	ts = turnstile_lookup(&m->lock_object);
591aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
5929ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
5939ed346baSBosko Milekic 
594961a7b24SJohn Baldwin 	MPASS(ts != NULL);
5957aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
596535eb309SJohn Baldwin 	_release_lock_quick(m);
5972502c107SJeff Roberson 	/*
5982502c107SJeff Roberson 	 * This turnstile is now no longer associated with the mutex.  We can
5992502c107SJeff Roberson 	 * unlock the chain lock so a new turnstile may take it's place.
6002502c107SJeff Roberson 	 */
6017aa4f685SJohn Baldwin 	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
6022502c107SJeff Roberson 	turnstile_chain_unlock(&m->lock_object);
6039ed346baSBosko Milekic }
6049ed346baSBosko Milekic 
6059ed346baSBosko Milekic /*
6069ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
6079ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
6089ed346baSBosko Milekic  */
6099ed346baSBosko Milekic 
6109ed346baSBosko Milekic /*
61115ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
6129ed346baSBosko Milekic  */
6131103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
6140cde2e34SJason Evans void
61556771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
6160cde2e34SJason Evans {
6175cb0fbe4SJohn Baldwin 
6181126349aSPaul Saab 	if (panicstr != NULL || dumping)
6195cb0fbe4SJohn Baldwin 		return;
620a10f4966SJake Burkholder 	switch (what) {
6210cde2e34SJason Evans 	case MA_OWNED:
6220cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
6230cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
624a10f4966SJake Burkholder 		if (!mtx_owned(m))
6250cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
626aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
627a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
628a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
6290cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
630aa89d8cdSJohn Baldwin 				    m->lock_object.lo_name, file, line);
631a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
6320cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
633aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
6340cde2e34SJason Evans 		}
6350cde2e34SJason Evans 		break;
6360cde2e34SJason Evans 	case MA_NOTOWNED:
637a10f4966SJake Burkholder 		if (mtx_owned(m))
6380cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
639aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
6400cde2e34SJason Evans 		break;
6410cde2e34SJason Evans 	default:
64256771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
6430cde2e34SJason Evans 	}
6440cde2e34SJason Evans }
6450cde2e34SJason Evans #endif
6460cde2e34SJason Evans 
6479ed346baSBosko Milekic /*
6489ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
64919284646SJohn Baldwin  *
65019284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
65119284646SJohn Baldwin  * maintained by the witness code.
6529ed346baSBosko Milekic  */
65336412d79SJohn Baldwin #ifdef MUTEX_DEBUG
65436412d79SJohn Baldwin 
6554d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
65636412d79SJohn Baldwin 
65719284646SJohn Baldwin void
65819284646SJohn Baldwin mtx_validate(struct mtx *m)
65936412d79SJohn Baldwin {
66036412d79SJohn Baldwin 
66136412d79SJohn Baldwin /*
662fa669ab7SPoul-Henning Kamp  * XXX: When kernacc() does not require Giant we can reenable this check
663fa669ab7SPoul-Henning Kamp  */
664fa669ab7SPoul-Henning Kamp #ifdef notyet
665fa669ab7SPoul-Henning Kamp 	/*
66676dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
66776dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
66876dcbd6fSBosko Milekic 	 * requires Giant itself.
66976dcbd6fSBosko Milekic 	 */
670ab07087eSBosko Milekic 	if (!cold)
671ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
672ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
67319284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
67436412d79SJohn Baldwin #endif
67536412d79SJohn Baldwin }
67636412d79SJohn Baldwin #endif
67736412d79SJohn Baldwin 
6789ed346baSBosko Milekic /*
679c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
680c27b5699SAndrew R. Reiter  */
681c27b5699SAndrew R. Reiter void
682c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
683c27b5699SAndrew R. Reiter {
684c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
685c27b5699SAndrew R. Reiter 
6860c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
687c27b5699SAndrew R. Reiter }
688c27b5699SAndrew R. Reiter 
689c27b5699SAndrew R. Reiter /*
6909ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
6910c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
6920c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
6930c88508aSJohn Baldwin  * witness.
6949ed346baSBosko Milekic  */
69536412d79SJohn Baldwin void
6960c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
69736412d79SJohn Baldwin {
69883a81bcbSJohn Baldwin 	struct lock_class *class;
69983a81bcbSJohn Baldwin 	int flags;
7009ed346baSBosko Milekic 
70119284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
7027c0435b9SKip Macy 		MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0);
7039ed346baSBosko Milekic 
70436412d79SJohn Baldwin #ifdef MUTEX_DEBUG
7059ed346baSBosko Milekic 	/* Diagnostic and error correction */
70619284646SJohn Baldwin 	mtx_validate(m);
7076936206eSJohn Baldwin #endif
70836412d79SJohn Baldwin 
70983a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
71019284646SJohn Baldwin 	if (opts & MTX_SPIN)
71183a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
71219284646SJohn Baldwin 	else
71383a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
71483a81bcbSJohn Baldwin 	flags = 0;
71519284646SJohn Baldwin 	if (opts & MTX_QUIET)
71683a81bcbSJohn Baldwin 		flags |= LO_QUIET;
71719284646SJohn Baldwin 	if (opts & MTX_RECURSE)
71883a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
71919284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
72083a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
721f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
72283a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
7237c0435b9SKip Macy 	if (opts & MTX_NOPROFILE)
7247c0435b9SKip Macy 		flags |= LO_NOPROFILE;
72519284646SJohn Baldwin 
72683a81bcbSJohn Baldwin 	/* Initialize mutex. */
72719284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
72883a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
7299ed346baSBosko Milekic 
730aa89d8cdSJohn Baldwin 	lock_init(&m->lock_object, class, name, type, flags);
73136412d79SJohn Baldwin }
73236412d79SJohn Baldwin 
7339ed346baSBosko Milekic /*
73419284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
73519284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
73619284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
73719284646SJohn Baldwin  * flags.
7389ed346baSBosko Milekic  */
73936412d79SJohn Baldwin void
74036412d79SJohn Baldwin mtx_destroy(struct mtx *m)
74136412d79SJohn Baldwin {
74236412d79SJohn Baldwin 
74319284646SJohn Baldwin 	if (!mtx_owned(m))
74419284646SJohn Baldwin 		MPASS(mtx_unowned(m));
74519284646SJohn Baldwin 	else {
74608812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
7479ed346baSBosko Milekic 
748861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
749aa89d8cdSJohn Baldwin 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
750861a2308SScott Long 			spinlock_exit();
751764e4d54SJohn Baldwin 		else
752764e4d54SJohn Baldwin 			curthread->td_locks--;
753861a2308SScott Long 
75419284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
755aa89d8cdSJohn Baldwin 		WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
756c86b6ff5SJohn Baldwin 		    __LINE__);
75736412d79SJohn Baldwin 	}
7580384fff8SJason Evans 
759186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
760aa89d8cdSJohn Baldwin 	lock_destroy(&m->lock_object);
7610384fff8SJason Evans }
762d23f5958SMatthew Dillon 
763d23f5958SMatthew Dillon /*
764c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
765c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
766c53c013bSJohn Baldwin  * setup before this is called.
767c53c013bSJohn Baldwin  */
768c53c013bSJohn Baldwin void
769c53c013bSJohn Baldwin mutex_init(void)
770c53c013bSJohn Baldwin {
771c53c013bSJohn Baldwin 
772961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
773961a7b24SJohn Baldwin 	init_turnstiles();
774961a7b24SJohn Baldwin 
775c53c013bSJohn Baldwin 	/*
776c53c013bSJohn Baldwin 	 * Initialize mutexes.
777c53c013bSJohn Baldwin 	 */
7780c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
7792502c107SJeff Roberson 	mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
7802502c107SJeff Roberson 	blocked_lock.mtx_lock = 0xdeadc0de;	/* Always blocked. */
7810c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
7822502c107SJeff Roberson 	mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
7838c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
784c53c013bSJohn Baldwin 	mtx_lock(&Giant);
785c53c013bSJohn Baldwin }
786d272fe53SJohn Baldwin 
787d272fe53SJohn Baldwin #ifdef DDB
788d272fe53SJohn Baldwin void
789d272fe53SJohn Baldwin db_show_mtx(struct lock_object *lock)
790d272fe53SJohn Baldwin {
791d272fe53SJohn Baldwin 	struct thread *td;
792d272fe53SJohn Baldwin 	struct mtx *m;
793d272fe53SJohn Baldwin 
794d272fe53SJohn Baldwin 	m = (struct mtx *)lock;
795d272fe53SJohn Baldwin 
796d272fe53SJohn Baldwin 	db_printf(" flags: {");
79783a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
798d272fe53SJohn Baldwin 		db_printf("SPIN");
799d272fe53SJohn Baldwin 	else
800d272fe53SJohn Baldwin 		db_printf("DEF");
801aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_RECURSABLE)
802d272fe53SJohn Baldwin 		db_printf(", RECURSE");
803aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_DUPOK)
804d272fe53SJohn Baldwin 		db_printf(", DUPOK");
805d272fe53SJohn Baldwin 	db_printf("}\n");
806d272fe53SJohn Baldwin 	db_printf(" state: {");
807d272fe53SJohn Baldwin 	if (mtx_unowned(m))
808d272fe53SJohn Baldwin 		db_printf("UNOWNED");
809c0bfd703SJohn Baldwin 	else if (mtx_destroyed(m))
810c0bfd703SJohn Baldwin 		db_printf("DESTROYED");
811d272fe53SJohn Baldwin 	else {
812d272fe53SJohn Baldwin 		db_printf("OWNED");
813d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
814d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
815d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
816d272fe53SJohn Baldwin 			db_printf(", RECURSED");
817d272fe53SJohn Baldwin 	}
818d272fe53SJohn Baldwin 	db_printf("}\n");
819c0bfd703SJohn Baldwin 	if (!mtx_unowned(m) && !mtx_destroyed(m)) {
820d272fe53SJohn Baldwin 		td = mtx_owner(m);
821d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
822431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
823d272fe53SJohn Baldwin 		if (mtx_recursed(m))
824d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
825d272fe53SJohn Baldwin 	}
826d272fe53SJohn Baldwin }
827d272fe53SJohn Baldwin #endif
828