xref: /freebsd/sys/kern/kern_mutex.c (revision cd6e6e4e1120a7e33f2776148d16c1785de27d6b)
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"
42535eb309SJohn Baldwin #include "opt_mutex_wake_all.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 
750cde2e34SJason Evans /*
76b9a80acaSStephan Uphoff  * Force MUTEX_WAKE_ALL for now.
77b9a80acaSStephan Uphoff  * single thread wakeup needs fixes to avoid race conditions with
78b9a80acaSStephan Uphoff  * priority inheritance.
79b9a80acaSStephan Uphoff  */
80b9a80acaSStephan Uphoff #ifndef MUTEX_WAKE_ALL
81b9a80acaSStephan Uphoff #define MUTEX_WAKE_ALL
82b9a80acaSStephan Uphoff #endif
83b9a80acaSStephan Uphoff 
84cd6e6e4eSJohn Baldwin #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
85cd6e6e4eSJohn Baldwin #define	ADAPTIVE_MUTEXES
86cd6e6e4eSJohn Baldwin #endif
87cd6e6e4eSJohn Baldwin 
88b9a80acaSStephan Uphoff /*
899ed346baSBosko Milekic  * Internal utility macros.
900cde2e34SJason Evans  */
919ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
920cde2e34SJason Evans 
9349b94bfcSJohn Baldwin #define	mtx_owner(m)	((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
949ed346baSBosko Milekic 
95d272fe53SJohn Baldwin #ifdef DDB
96d272fe53SJohn Baldwin static void	db_show_mtx(struct lock_object *lock);
97d272fe53SJohn Baldwin #endif
986e21afd4SJohn Baldwin static void	lock_mtx(struct lock_object *lock, int how);
996e21afd4SJohn Baldwin static void	lock_spin(struct lock_object *lock, int how);
1006e21afd4SJohn Baldwin static int	unlock_mtx(struct lock_object *lock);
1016e21afd4SJohn Baldwin static int	unlock_spin(struct lock_object *lock);
102d272fe53SJohn Baldwin 
1030cde2e34SJason Evans /*
10419284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
1050cde2e34SJason Evans  */
10619284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
107ae8dde30SJohn Baldwin 	.lc_name = "sleep mutex",
108ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
109d272fe53SJohn Baldwin #ifdef DDB
110ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
111d272fe53SJohn Baldwin #endif
1126e21afd4SJohn Baldwin 	.lc_lock = lock_mtx,
1136e21afd4SJohn Baldwin 	.lc_unlock = unlock_mtx,
11419284646SJohn Baldwin };
11519284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
116ae8dde30SJohn Baldwin 	.lc_name = "spin mutex",
117ae8dde30SJohn Baldwin 	.lc_flags = LC_SPINLOCK | LC_RECURSABLE,
118d272fe53SJohn Baldwin #ifdef DDB
119ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
120d272fe53SJohn Baldwin #endif
1216e21afd4SJohn Baldwin 	.lc_lock = lock_spin,
1226e21afd4SJohn Baldwin 	.lc_unlock = unlock_spin,
1238484de75SJohn Baldwin };
1248484de75SJohn Baldwin 
1259ed346baSBosko Milekic /*
126c53c013bSJohn Baldwin  * System-wide mutexes
127c53c013bSJohn Baldwin  */
128c53c013bSJohn Baldwin struct mtx sched_lock;
129c53c013bSJohn Baldwin struct mtx Giant;
130c53c013bSJohn Baldwin 
1311364a812SKip Macy #ifdef LOCK_PROFILING
1321364a812SKip Macy static inline void lock_profile_init(void)
1331364a812SKip Macy {
1341364a812SKip Macy         int i;
1351364a812SKip Macy         /* Initialize the mutex profiling locks */
1361364a812SKip Macy         for (i = 0; i < LPROF_LOCK_SIZE; i++) {
1371364a812SKip Macy                 mtx_init(&lprof_locks[i], "mprof lock",
1381364a812SKip Macy                     NULL, MTX_SPIN|MTX_QUIET|MTX_NOPROFILE);
1391364a812SKip Macy         }
1401364a812SKip Macy }
1411364a812SKip Macy #else
1421364a812SKip Macy static inline void lock_profile_init(void) {;}
1431364a812SKip Macy #endif
1441364a812SKip Macy 
1456e21afd4SJohn Baldwin void
1466e21afd4SJohn Baldwin lock_mtx(struct lock_object *lock, int how)
1476e21afd4SJohn Baldwin {
1486e21afd4SJohn Baldwin 
1496e21afd4SJohn Baldwin 	mtx_lock((struct mtx *)lock);
1506e21afd4SJohn Baldwin }
1516e21afd4SJohn Baldwin 
1526e21afd4SJohn Baldwin void
1536e21afd4SJohn Baldwin lock_spin(struct lock_object *lock, int how)
1546e21afd4SJohn Baldwin {
1556e21afd4SJohn Baldwin 
1566e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
1576e21afd4SJohn Baldwin }
1586e21afd4SJohn Baldwin 
1596e21afd4SJohn Baldwin int
1606e21afd4SJohn Baldwin unlock_mtx(struct lock_object *lock)
1616e21afd4SJohn Baldwin {
1626e21afd4SJohn Baldwin 	struct mtx *m;
1636e21afd4SJohn Baldwin 
1646e21afd4SJohn Baldwin 	m = (struct mtx *)lock;
1656e21afd4SJohn Baldwin 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
1666e21afd4SJohn Baldwin 	mtx_unlock(m);
1676e21afd4SJohn Baldwin 	return (0);
1686e21afd4SJohn Baldwin }
1696e21afd4SJohn Baldwin 
1706e21afd4SJohn Baldwin int
1716e21afd4SJohn Baldwin unlock_spin(struct lock_object *lock)
1726e21afd4SJohn Baldwin {
1736e21afd4SJohn Baldwin 
1746e21afd4SJohn Baldwin 	panic("spin locks can only use msleep_spin");
1756e21afd4SJohn Baldwin }
1766e21afd4SJohn Baldwin 
1770cde2e34SJason Evans /*
1786283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
1796283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
1806283b7d0SJohn Baldwin  */
1816283b7d0SJohn Baldwin void
1826283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
1836283b7d0SJohn Baldwin {
1846283b7d0SJohn Baldwin 
185dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
186186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
187186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
188aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
189aa89d8cdSJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
1900d975d63SJohn Baldwin 	    file, line));
191aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
1928d768e76SJohn Baldwin 	    file, line);
1937c0435b9SKip Macy 
194dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
195aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
196dde96c99SJohn Baldwin 	    line);
197aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
198764e4d54SJohn Baldwin 	curthread->td_locks++;
1996283b7d0SJohn Baldwin }
2006283b7d0SJohn Baldwin 
2016283b7d0SJohn Baldwin void
2026283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
2036283b7d0SJohn Baldwin {
204dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
205186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
206186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
207aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
208aa89d8cdSJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
2090d975d63SJohn Baldwin 	    file, line));
210764e4d54SJohn Baldwin 	curthread->td_locks--;
211aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
212aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
2130d975d63SJohn Baldwin 	    line);
21421377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
215c66d7606SKip Macy 
216aa89d8cdSJohn Baldwin 	lock_profile_release_lock(&m->lock_object);
217dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
2186283b7d0SJohn Baldwin }
2196283b7d0SJohn Baldwin 
2206283b7d0SJohn Baldwin void
2216283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
2226283b7d0SJohn Baldwin {
2236283b7d0SJohn Baldwin 
224dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
225186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
226186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
227aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
2280d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
229aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
230aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
2318d768e76SJohn Baldwin 	    file, line);
232dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
233aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
234dde96c99SJohn Baldwin 	    line);
235aa89d8cdSJohn Baldwin 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
2366283b7d0SJohn Baldwin }
2376283b7d0SJohn Baldwin 
2386283b7d0SJohn Baldwin void
2396283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
2406283b7d0SJohn Baldwin {
241c66d7606SKip Macy 
242dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
243186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
244186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
245aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
2460d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
247aa89d8cdSJohn Baldwin 	    m->lock_object.lo_name, file, line));
248aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
249aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
250dde96c99SJohn Baldwin 	    line);
2510d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
252c66d7606SKip Macy 
253aa89d8cdSJohn Baldwin 	lock_profile_release_lock(&m->lock_object);
254dde96c99SJohn Baldwin 	_rel_spin_lock(m);
2556283b7d0SJohn Baldwin }
2566283b7d0SJohn Baldwin 
2576283b7d0SJohn Baldwin /*
2589ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
259eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
260eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
2610cde2e34SJason Evans  */
2620cde2e34SJason Evans int
2639ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
2640cde2e34SJason Evans {
265fe68a916SKip Macy 	int rval, contested = 0;
2667c0435b9SKip Macy 	uint64_t waittime = 0;
2670cde2e34SJason Evans 
268b40ce416SJulian Elischer 	MPASS(curthread != NULL);
269186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
270186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
271aa89d8cdSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
272aa89d8cdSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
27383cece6fSJohn Baldwin 	    file, line));
2749ed346baSBosko Milekic 
275aa89d8cdSJohn Baldwin 	if (mtx_owned(m) && (m->lock_object.lo_flags & LO_RECURSABLE) != 0) {
276eac09796SJohn Baldwin 		m->mtx_recurse++;
277eac09796SJohn Baldwin 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
278eac09796SJohn Baldwin 		rval = 1;
279eac09796SJohn Baldwin 	} else
280122eceefSJohn Baldwin 		rval = _obtain_lock(m, (uintptr_t)curthread);
2819ed346baSBosko Milekic 
282aa89d8cdSJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
283764e4d54SJohn Baldwin 	if (rval) {
284aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
2852d96f0b1SJohn Baldwin 		    file, line);
286764e4d54SJohn Baldwin 		curthread->td_locks++;
287fe68a916SKip Macy 		if (m->mtx_recurse == 0)
288aa89d8cdSJohn Baldwin 			lock_profile_obtain_lock_success(&m->lock_object, contested,
289fe68a916SKip Macy 			    waittime, file, line);
2907c0435b9SKip Macy 
291764e4d54SJohn Baldwin 	}
2929ed346baSBosko Milekic 
29319284646SJohn Baldwin 	return (rval);
2940cde2e34SJason Evans }
2950cde2e34SJason Evans 
2960cde2e34SJason Evans /*
2979ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
2989ed346baSBosko Milekic  *
2999ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
3009ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
3010cde2e34SJason Evans  */
3020cde2e34SJason Evans void
303122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
304bdcfcf5bSJohn Baldwin     int line)
30536412d79SJohn Baldwin {
306cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES
30776447e56SJohn Baldwin 	volatile struct thread *owner;
3082498cf8cSJohn Baldwin #endif
30902bd1bcdSIan Dowse #ifdef KTR
31002bd1bcdSIan Dowse 	int cont_logged = 0;
31102bd1bcdSIan Dowse #endif
3127c0435b9SKip Macy 	uintptr_t v;
31336412d79SJohn Baldwin 
3145fa8dd90SJohn Baldwin 	if (mtx_owned(m)) {
315aa89d8cdSJohn Baldwin 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
316eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
317aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, file, line));
31836412d79SJohn Baldwin 		m->mtx_recurse++;
31908812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
320aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
3215746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
32236412d79SJohn Baldwin 		return;
32336412d79SJohn Baldwin 	}
3249ed346baSBosko Milekic 
325aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
32615ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
32715ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
328aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
3291bd0eefbSJohn Baldwin 
330122eceefSJohn Baldwin 	while (!_obtain_lock(m, tid)) {
331aa89d8cdSJohn Baldwin 		turnstile_lock(&m->lock_object);
3325fa8dd90SJohn Baldwin 		v = m->mtx_lock;
3335fa8dd90SJohn Baldwin 
33436412d79SJohn Baldwin 		/*
3359ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
336961a7b24SJohn Baldwin 		 * the turnstile chain lock.
33736412d79SJohn Baldwin 		 */
3385fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
339aa89d8cdSJohn Baldwin 			turnstile_release(&m->lock_object);
3409f1b87f1SMaxime Henrion 			cpu_spinwait();
34136412d79SJohn Baldwin 			continue;
34236412d79SJohn Baldwin 		}
3439ed346baSBosko Milekic 
344535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
345535eb309SJohn Baldwin 		MPASS(v != MTX_CONTESTED);
346535eb309SJohn Baldwin #else
34736412d79SJohn Baldwin 		/*
3489ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
349f7ee1590SJohn Baldwin 		 * there are other threads blocked on it.  Grab ownership of
350f7ee1590SJohn Baldwin 		 * it and propagate its priority to the current thread if
351f7ee1590SJohn Baldwin 		 * necessary.
35236412d79SJohn Baldwin 		 */
35336412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
354122eceefSJohn Baldwin 			m->mtx_lock = tid | MTX_CONTESTED;
355aa89d8cdSJohn Baldwin 			turnstile_claim(&m->lock_object);
3568dc10be8SRobert Watson 			break;
35736412d79SJohn Baldwin 		}
358535eb309SJohn Baldwin #endif
3599ed346baSBosko Milekic 
36036412d79SJohn Baldwin 		/*
3619ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
3629ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
3639ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
36436412d79SJohn Baldwin 		 */
36536412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
366122eceefSJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
367aa89d8cdSJohn Baldwin 			turnstile_release(&m->lock_object);
3689f1b87f1SMaxime Henrion 			cpu_spinwait();
36936412d79SJohn Baldwin 			continue;
37036412d79SJohn Baldwin 		}
37136412d79SJohn Baldwin 
372cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES
3732498cf8cSJohn Baldwin 		/*
3742498cf8cSJohn Baldwin 		 * If the current owner of the lock is executing on another
3752498cf8cSJohn Baldwin 		 * CPU, spin instead of blocking.
3762498cf8cSJohn Baldwin 		 */
37749b94bfcSJohn Baldwin 		owner = (struct thread *)(v & ~MTX_FLAGMASK);
378a9abdce4SRobert Watson #ifdef ADAPTIVE_GIANT
3791364a812SKip Macy 		if (TD_IS_RUNNING(owner))
380a9abdce4SRobert Watson #else
3811364a812SKip Macy 		if (m != &Giant && TD_IS_RUNNING(owner))
382a9abdce4SRobert Watson #endif
3831364a812SKip Macy 		{
384aa89d8cdSJohn Baldwin 			turnstile_release(&m->lock_object);
38527dad03cSJohn Baldwin 			while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
3869f1b87f1SMaxime Henrion 				cpu_spinwait();
3877fcca609SJohn Baldwin 			}
3882498cf8cSJohn Baldwin 			continue;
3892498cf8cSJohn Baldwin 		}
390cd6e6e4eSJohn Baldwin #endif	/* ADAPTIVE_MUTEXES */
3912498cf8cSJohn Baldwin 
3929ed346baSBosko Milekic 		/*
3937feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
3949ed346baSBosko Milekic 		 */
39536412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
39636412d79SJohn Baldwin 
39702bd1bcdSIan Dowse #ifdef KTR
39802bd1bcdSIan Dowse 		if (!cont_logged) {
39902bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
40002bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
401aa89d8cdSJohn Baldwin 			    (void *)tid, file, line, m->lock_object.lo_name,
402aa89d8cdSJohn Baldwin 			    WITNESS_FILE(&m->lock_object),
403aa89d8cdSJohn Baldwin 			    WITNESS_LINE(&m->lock_object));
40402bd1bcdSIan Dowse 			cont_logged = 1;
40502bd1bcdSIan Dowse 		}
40602bd1bcdSIan Dowse #endif
40736412d79SJohn Baldwin 
4089ed346baSBosko Milekic 		/*
409961a7b24SJohn Baldwin 		 * Block on the turnstile.
4109ed346baSBosko Milekic 		 */
411aa89d8cdSJohn Baldwin 		turnstile_wait(&m->lock_object, mtx_owner(m),
4127aa4f685SJohn Baldwin 		    TS_EXCLUSIVE_QUEUE);
41336412d79SJohn Baldwin 	}
41402bd1bcdSIan Dowse #ifdef KTR
41502bd1bcdSIan Dowse 	if (cont_logged) {
41602bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
41702bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
418aa89d8cdSJohn Baldwin 		    m->lock_object.lo_name, (void *)tid, file, line);
41902bd1bcdSIan Dowse 	}
42002bd1bcdSIan Dowse #endif
42136412d79SJohn Baldwin 	return;
4229ed346baSBosko Milekic }
4239ed346baSBosko Milekic 
42433fb8a38SJohn Baldwin #ifdef SMP
4259ed346baSBosko Milekic /*
4269ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
4279ed346baSBosko Milekic  *
4289ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
4299ed346baSBosko Milekic  * is handled inline.
4309ed346baSBosko Milekic  */
4319ed346baSBosko Milekic void
432122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file,
433bdcfcf5bSJohn Baldwin     int line)
43436412d79SJohn Baldwin {
435fe68a916SKip Macy 	int i = 0;
4360fa2168bSJohn Baldwin 	struct thread *td;
43736412d79SJohn Baldwin 
438aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
4395746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
4409ed346baSBosko Milekic 
441f781b5a4SJohn Baldwin 	while (!_obtain_lock(m, tid)) {
4429ed346baSBosko Milekic 
4437141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
444c6a37e84SJohn Baldwin 		spinlock_exit();
44536412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
446703fc290SJohn Baldwin 			if (i++ < 10000000) {
4479f1b87f1SMaxime Henrion 				cpu_spinwait();
44836412d79SJohn Baldwin 				continue;
449703fc290SJohn Baldwin 			}
4500fa2168bSJohn Baldwin 			if (i < 60000000 || kdb_active || panicstr != NULL)
45136412d79SJohn Baldwin 				DELAY(1);
4520fa2168bSJohn Baldwin 			else {
4530fa2168bSJohn Baldwin 				td = mtx_owner(m);
4540fa2168bSJohn Baldwin 
4550fa2168bSJohn Baldwin 				/* If the mutex is unlocked, try again. */
4560fa2168bSJohn Baldwin 				if (td == NULL)
4570fa2168bSJohn Baldwin 					continue;
4580fa2168bSJohn Baldwin 				printf(
4590fa2168bSJohn Baldwin 			"spin lock %p (%s) held by %p (tid %d) too long\n",
460aa89d8cdSJohn Baldwin 				    m, m->lock_object.lo_name, td, td->td_tid);
46141109518SJohn Baldwin #ifdef WITNESS
462aa89d8cdSJohn Baldwin 				witness_display_spinlock(&m->lock_object, td);
46341109518SJohn Baldwin #endif
46441109518SJohn Baldwin 				panic("spin lock held too long");
46541109518SJohn Baldwin 			}
4669f1b87f1SMaxime Henrion 			cpu_spinwait();
46736412d79SJohn Baldwin 		}
468c6a37e84SJohn Baldwin 		spinlock_enter();
46936412d79SJohn Baldwin 	}
47036412d79SJohn Baldwin 
471aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
4729ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
4739ed346baSBosko Milekic 
47436412d79SJohn Baldwin 	return;
47536412d79SJohn Baldwin }
47633fb8a38SJohn Baldwin #endif /* SMP */
47736412d79SJohn Baldwin 
4789ed346baSBosko Milekic /*
4799ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
4809ed346baSBosko Milekic  *
4819ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
4829ed346baSBosko Milekic  * need to wake up a blocked thread).
4839ed346baSBosko Milekic  */
48436412d79SJohn Baldwin void
4859ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
48636412d79SJohn Baldwin {
487961a7b24SJohn Baldwin 	struct turnstile *ts;
4880c0b25aeSJohn Baldwin #ifndef PREEMPTION
489b40ce416SJulian Elischer 	struct thread *td, *td1;
4900c0b25aeSJohn Baldwin #endif
4919ed346baSBosko Milekic 
49208812b39SBosko Milekic 	if (mtx_recursed(m)) {
49336412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
49408812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
495aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
4969ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
49736412d79SJohn Baldwin 		return;
49836412d79SJohn Baldwin 	}
4999ed346baSBosko Milekic 
500aa89d8cdSJohn Baldwin 	turnstile_lock(&m->lock_object);
501aa89d8cdSJohn Baldwin 	ts = turnstile_lookup(&m->lock_object);
502aa89d8cdSJohn Baldwin 	if (LOCK_LOG_TEST(&m->lock_object, opts))
5039ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
5049ed346baSBosko Milekic 
505cd6e6e4eSJohn Baldwin #ifdef ADAPTIVE_MUTEXES
506961a7b24SJohn Baldwin 	if (ts == NULL) {
5072498cf8cSJohn Baldwin 		_release_lock_quick(m);
508aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
5092498cf8cSJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
510aa89d8cdSJohn Baldwin 		turnstile_release(&m->lock_object);
5112498cf8cSJohn Baldwin 		return;
5122498cf8cSJohn Baldwin 	}
513961a7b24SJohn Baldwin #else
514961a7b24SJohn Baldwin 	MPASS(ts != NULL);
5152498cf8cSJohn Baldwin #endif
5160c0b25aeSJohn Baldwin #ifndef PREEMPTION
517961a7b24SJohn Baldwin 	/* XXX */
5187aa4f685SJohn Baldwin 	td1 = turnstile_head(ts, TS_EXCLUSIVE_QUEUE);
5190c0b25aeSJohn Baldwin #endif
520535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
5217aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
522535eb309SJohn Baldwin 	_release_lock_quick(m);
523535eb309SJohn Baldwin #else
5247aa4f685SJohn Baldwin 	if (turnstile_signal(ts, TS_EXCLUSIVE_QUEUE)) {
52536412d79SJohn Baldwin 		_release_lock_quick(m);
526aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
5279ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
528961a7b24SJohn Baldwin 	} else {
529f7ee1590SJohn Baldwin 		m->mtx_lock = MTX_CONTESTED;
530aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
531961a7b24SJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
532961a7b24SJohn Baldwin 			    m);
533e0817317SJulian Elischer 	}
534535eb309SJohn Baldwin #endif
5357aa4f685SJohn Baldwin 	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
5369ed346baSBosko Milekic 
5370c0b25aeSJohn Baldwin #ifndef PREEMPTION
538961a7b24SJohn Baldwin 	/*
539961a7b24SJohn Baldwin 	 * XXX: This is just a hack until preemption is done.  However,
540961a7b24SJohn Baldwin 	 * once preemption is done we need to either wrap the
541961a7b24SJohn Baldwin 	 * turnstile_signal() and release of the actual lock in an
542961a7b24SJohn Baldwin 	 * extra critical section or change the preemption code to
543961a7b24SJohn Baldwin 	 * always just set a flag and never do instant-preempts.
544961a7b24SJohn Baldwin 	 */
545961a7b24SJohn Baldwin 	td = curthread;
546961a7b24SJohn Baldwin 	if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
547961a7b24SJohn Baldwin 		return;
548961a7b24SJohn Baldwin 	mtx_lock_spin(&sched_lock);
549961a7b24SJohn Baldwin 	if (!TD_IS_RUNNING(td1)) {
55036412d79SJohn Baldwin #ifdef notyet
551b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
552b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
55336412d79SJohn Baldwin 
55436412d79SJohn Baldwin 			if (it->it_interrupted) {
555aa89d8cdSJohn Baldwin 				if (LOCK_LOG_TEST(&m->lock_object, opts))
55636412d79SJohn Baldwin 					CTR2(KTR_LOCK,
55715ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
55836412d79SJohn Baldwin 					    it, it->it_interrupted);
55936412d79SJohn Baldwin 				intr_thd_fixup(it);
56036412d79SJohn Baldwin 			}
56136412d79SJohn Baldwin 		}
56236412d79SJohn Baldwin #endif
563aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
564562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
5659ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
5669ed346baSBosko Milekic 			    (void *)m->mtx_lock);
5679ed346baSBosko Milekic 
568bf0acc27SJohn Baldwin 		mi_switch(SW_INVOL, NULL);
569aa89d8cdSJohn Baldwin 		if (LOCK_LOG_TEST(&m->lock_object, opts))
5709ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
57131271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
57236412d79SJohn Baldwin 	}
5739ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
5740c0b25aeSJohn Baldwin #endif
5759ed346baSBosko Milekic 
5769ed346baSBosko Milekic 	return;
5779ed346baSBosko Milekic }
5789ed346baSBosko Milekic 
5799ed346baSBosko Milekic /*
5809ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
5819ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
5829ed346baSBosko Milekic  */
5839ed346baSBosko Milekic 
5849ed346baSBosko Milekic /*
58515ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
5869ed346baSBosko Milekic  */
5871103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
5880cde2e34SJason Evans void
58956771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
5900cde2e34SJason Evans {
5915cb0fbe4SJohn Baldwin 
5921126349aSPaul Saab 	if (panicstr != NULL || dumping)
5935cb0fbe4SJohn Baldwin 		return;
594a10f4966SJake Burkholder 	switch (what) {
5950cde2e34SJason Evans 	case MA_OWNED:
5960cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
5970cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
598a10f4966SJake Burkholder 		if (!mtx_owned(m))
5990cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
600aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
601a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
602a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
6030cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
604aa89d8cdSJohn Baldwin 				    m->lock_object.lo_name, file, line);
605a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
6060cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
607aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
6080cde2e34SJason Evans 		}
6090cde2e34SJason Evans 		break;
6100cde2e34SJason Evans 	case MA_NOTOWNED:
611a10f4966SJake Burkholder 		if (mtx_owned(m))
6120cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
613aa89d8cdSJohn Baldwin 			    m->lock_object.lo_name, file, line);
6140cde2e34SJason Evans 		break;
6150cde2e34SJason Evans 	default:
61656771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
6170cde2e34SJason Evans 	}
6180cde2e34SJason Evans }
6190cde2e34SJason Evans #endif
6200cde2e34SJason Evans 
6219ed346baSBosko Milekic /*
6229ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
62319284646SJohn Baldwin  *
62419284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
62519284646SJohn Baldwin  * maintained by the witness code.
6269ed346baSBosko Milekic  */
62736412d79SJohn Baldwin #ifdef MUTEX_DEBUG
62836412d79SJohn Baldwin 
6294d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
63036412d79SJohn Baldwin 
63119284646SJohn Baldwin void
63219284646SJohn Baldwin mtx_validate(struct mtx *m)
63336412d79SJohn Baldwin {
63436412d79SJohn Baldwin 
63536412d79SJohn Baldwin /*
636fa669ab7SPoul-Henning Kamp  * XXX: When kernacc() does not require Giant we can reenable this check
637fa669ab7SPoul-Henning Kamp  */
638fa669ab7SPoul-Henning Kamp #ifdef notyet
639fa669ab7SPoul-Henning Kamp 	/*
64076dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
64176dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
64276dcbd6fSBosko Milekic 	 * requires Giant itself.
64376dcbd6fSBosko Milekic 	 */
644ab07087eSBosko Milekic 	if (!cold)
645ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
646ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
64719284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
64836412d79SJohn Baldwin #endif
64936412d79SJohn Baldwin }
65036412d79SJohn Baldwin #endif
65136412d79SJohn Baldwin 
6529ed346baSBosko Milekic /*
653c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
654c27b5699SAndrew R. Reiter  */
655c27b5699SAndrew R. Reiter void
656c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
657c27b5699SAndrew R. Reiter {
658c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
659c27b5699SAndrew R. Reiter 
6600c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
661c27b5699SAndrew R. Reiter }
662c27b5699SAndrew R. Reiter 
663c27b5699SAndrew R. Reiter /*
6649ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
6650c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
6660c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
6670c88508aSJohn Baldwin  * witness.
6689ed346baSBosko Milekic  */
66936412d79SJohn Baldwin void
6700c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
67136412d79SJohn Baldwin {
67283a81bcbSJohn Baldwin 	struct lock_class *class;
67383a81bcbSJohn Baldwin 	int flags;
6749ed346baSBosko Milekic 
67519284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
6767c0435b9SKip Macy 		MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0);
6779ed346baSBosko Milekic 
67836412d79SJohn Baldwin #ifdef MUTEX_DEBUG
6799ed346baSBosko Milekic 	/* Diagnostic and error correction */
68019284646SJohn Baldwin 	mtx_validate(m);
6816936206eSJohn Baldwin #endif
68236412d79SJohn Baldwin 
68383a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
68419284646SJohn Baldwin 	if (opts & MTX_SPIN)
68583a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
68619284646SJohn Baldwin 	else
68783a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
68883a81bcbSJohn Baldwin 	flags = 0;
68919284646SJohn Baldwin 	if (opts & MTX_QUIET)
69083a81bcbSJohn Baldwin 		flags |= LO_QUIET;
69119284646SJohn Baldwin 	if (opts & MTX_RECURSE)
69283a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
69319284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
69483a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
695f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
69683a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
6977c0435b9SKip Macy 	if (opts & MTX_NOPROFILE)
6987c0435b9SKip Macy 		flags |= LO_NOPROFILE;
69919284646SJohn Baldwin 
70083a81bcbSJohn Baldwin 	/* Initialize mutex. */
70119284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
70283a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
7039ed346baSBosko Milekic 
704aa89d8cdSJohn Baldwin 	lock_profile_object_init(&m->lock_object, class, name);
705aa89d8cdSJohn Baldwin 	lock_init(&m->lock_object, class, name, type, flags);
70636412d79SJohn Baldwin }
70736412d79SJohn Baldwin 
7089ed346baSBosko Milekic /*
70919284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
71019284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
71119284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
71219284646SJohn Baldwin  * flags.
7139ed346baSBosko Milekic  */
71436412d79SJohn Baldwin void
71536412d79SJohn Baldwin mtx_destroy(struct mtx *m)
71636412d79SJohn Baldwin {
71736412d79SJohn Baldwin 
71819284646SJohn Baldwin 	if (!mtx_owned(m))
71919284646SJohn Baldwin 		MPASS(mtx_unowned(m));
72019284646SJohn Baldwin 	else {
72108812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
7229ed346baSBosko Milekic 
723861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
724aa89d8cdSJohn Baldwin 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
725861a2308SScott Long 			spinlock_exit();
726764e4d54SJohn Baldwin 		else
727764e4d54SJohn Baldwin 			curthread->td_locks--;
728861a2308SScott Long 
72919284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
730aa89d8cdSJohn Baldwin 		WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
731c86b6ff5SJohn Baldwin 		    __LINE__);
73236412d79SJohn Baldwin 	}
7330384fff8SJason Evans 
734186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
735aa89d8cdSJohn Baldwin 	lock_profile_object_destroy(&m->lock_object);
736aa89d8cdSJohn Baldwin 	lock_destroy(&m->lock_object);
7370384fff8SJason Evans }
738d23f5958SMatthew Dillon 
739d23f5958SMatthew Dillon /*
740c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
741c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
742c53c013bSJohn Baldwin  * setup before this is called.
743c53c013bSJohn Baldwin  */
744c53c013bSJohn Baldwin void
745c53c013bSJohn Baldwin mutex_init(void)
746c53c013bSJohn Baldwin {
747c53c013bSJohn Baldwin 
748961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
749961a7b24SJohn Baldwin 	init_turnstiles();
750961a7b24SJohn Baldwin 
751c53c013bSJohn Baldwin 	/*
752c53c013bSJohn Baldwin 	 * Initialize mutexes.
753c53c013bSJohn Baldwin 	 */
7540c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
7550c88508aSJohn Baldwin 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
7560c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
7578c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
758c53c013bSJohn Baldwin 	mtx_lock(&Giant);
7597c0435b9SKip Macy 
7607c0435b9SKip Macy 	lock_profile_init();
761c53c013bSJohn Baldwin }
762d272fe53SJohn Baldwin 
763d272fe53SJohn Baldwin #ifdef DDB
764d272fe53SJohn Baldwin void
765d272fe53SJohn Baldwin db_show_mtx(struct lock_object *lock)
766d272fe53SJohn Baldwin {
767d272fe53SJohn Baldwin 	struct thread *td;
768d272fe53SJohn Baldwin 	struct mtx *m;
769d272fe53SJohn Baldwin 
770d272fe53SJohn Baldwin 	m = (struct mtx *)lock;
771d272fe53SJohn Baldwin 
772d272fe53SJohn Baldwin 	db_printf(" flags: {");
77383a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
774d272fe53SJohn Baldwin 		db_printf("SPIN");
775d272fe53SJohn Baldwin 	else
776d272fe53SJohn Baldwin 		db_printf("DEF");
777aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_RECURSABLE)
778d272fe53SJohn Baldwin 		db_printf(", RECURSE");
779aa89d8cdSJohn Baldwin 	if (m->lock_object.lo_flags & LO_DUPOK)
780d272fe53SJohn Baldwin 		db_printf(", DUPOK");
781d272fe53SJohn Baldwin 	db_printf("}\n");
782d272fe53SJohn Baldwin 	db_printf(" state: {");
783d272fe53SJohn Baldwin 	if (mtx_unowned(m))
784d272fe53SJohn Baldwin 		db_printf("UNOWNED");
785d272fe53SJohn Baldwin 	else {
786d272fe53SJohn Baldwin 		db_printf("OWNED");
787d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
788d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
789d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
790d272fe53SJohn Baldwin 			db_printf(", RECURSED");
791d272fe53SJohn Baldwin 	}
792d272fe53SJohn Baldwin 	db_printf("}\n");
793d272fe53SJohn Baldwin 	if (!mtx_unowned(m)) {
794d272fe53SJohn Baldwin 		td = mtx_owner(m);
795d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
796d272fe53SJohn Baldwin 		    td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
797d272fe53SJohn Baldwin 		if (mtx_recursed(m))
798d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
799d272fe53SJohn Baldwin 	}
800d272fe53SJohn Baldwin }
801d272fe53SJohn Baldwin #endif
802