xref: /freebsd/sys/kern/kern_mutex.c (revision ae8dde30c26dc1596e19515cb9b222726ffca625)
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 
84b9a80acaSStephan Uphoff /*
859ed346baSBosko Milekic  * Internal utility macros.
860cde2e34SJason Evans  */
879ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
880cde2e34SJason Evans 
8949b94bfcSJohn Baldwin #define	mtx_owner(m)	((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
909ed346baSBosko Milekic 
91d272fe53SJohn Baldwin #ifdef DDB
92d272fe53SJohn Baldwin static void	db_show_mtx(struct lock_object *lock);
93d272fe53SJohn Baldwin #endif
94d272fe53SJohn Baldwin 
950cde2e34SJason Evans /*
9619284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
970cde2e34SJason Evans  */
9819284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
99ae8dde30SJohn Baldwin 	.lc_name = "sleep mutex",
100ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
101d272fe53SJohn Baldwin #ifdef DDB
102ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
103d272fe53SJohn Baldwin #endif
10419284646SJohn Baldwin };
10519284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
106ae8dde30SJohn Baldwin 	.lc_name = "spin mutex",
107ae8dde30SJohn Baldwin 	.lc_flags = LC_SPINLOCK | LC_RECURSABLE,
108d272fe53SJohn Baldwin #ifdef DDB
109ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_mtx,
110d272fe53SJohn Baldwin #endif
1118484de75SJohn Baldwin };
1128484de75SJohn Baldwin 
1139ed346baSBosko Milekic /*
114c53c013bSJohn Baldwin  * System-wide mutexes
115c53c013bSJohn Baldwin  */
116c53c013bSJohn Baldwin struct mtx sched_lock;
117c53c013bSJohn Baldwin struct mtx Giant;
118c53c013bSJohn Baldwin 
1191364a812SKip Macy #ifdef LOCK_PROFILING
1201364a812SKip Macy static inline void lock_profile_init(void)
1211364a812SKip Macy {
1221364a812SKip Macy         int i;
1231364a812SKip Macy         /* Initialize the mutex profiling locks */
1241364a812SKip Macy         for (i = 0; i < LPROF_LOCK_SIZE; i++) {
1251364a812SKip Macy                 mtx_init(&lprof_locks[i], "mprof lock",
1261364a812SKip Macy                     NULL, MTX_SPIN|MTX_QUIET|MTX_NOPROFILE);
1271364a812SKip Macy         }
1281364a812SKip Macy }
1291364a812SKip Macy #else
1301364a812SKip Macy static inline void lock_profile_init(void) {;}
1311364a812SKip Macy #endif
1321364a812SKip Macy 
1330cde2e34SJason Evans /*
1346283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
1356283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
1366283b7d0SJohn Baldwin  */
1376283b7d0SJohn Baldwin void
1386283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
1396283b7d0SJohn Baldwin {
1406283b7d0SJohn Baldwin 
141dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
142186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
143186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
14483a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
1450d975d63SJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
1460d975d63SJohn Baldwin 	    file, line));
1478d768e76SJohn Baldwin 	WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
1488d768e76SJohn Baldwin 	    file, line);
1497c0435b9SKip Macy 
150dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
151dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
152dde96c99SJohn Baldwin 	    line);
153dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
154764e4d54SJohn Baldwin 	curthread->td_locks++;
1556283b7d0SJohn Baldwin }
1566283b7d0SJohn Baldwin 
1576283b7d0SJohn Baldwin void
1586283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
1596283b7d0SJohn Baldwin {
160dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
161186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
162186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
16383a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
1640d975d63SJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
1650d975d63SJohn Baldwin 	    file, line));
166764e4d54SJohn Baldwin 	curthread->td_locks--;
1670d975d63SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
1680d975d63SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
1690d975d63SJohn Baldwin 	    line);
17021377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
171c66d7606SKip Macy 
172c66d7606SKip Macy 	lock_profile_release_lock(&m->mtx_object);
173dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
1746283b7d0SJohn Baldwin }
1756283b7d0SJohn Baldwin 
1766283b7d0SJohn Baldwin void
1776283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
1786283b7d0SJohn Baldwin {
1796283b7d0SJohn Baldwin 
180dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
181186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
182186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
18383a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin,
1840d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
1850d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
1868d768e76SJohn Baldwin 	WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
1878d768e76SJohn Baldwin 	    file, line);
188dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
189dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
190dde96c99SJohn Baldwin 	    line);
191dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
1926283b7d0SJohn Baldwin }
1936283b7d0SJohn Baldwin 
1946283b7d0SJohn Baldwin void
1956283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
1966283b7d0SJohn Baldwin {
197c66d7606SKip Macy 
198dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
199186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
200186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
20183a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin,
2020d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
2030d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
204dde96c99SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
205dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
206dde96c99SJohn Baldwin 	    line);
2070d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
208c66d7606SKip Macy 
209c66d7606SKip Macy 	lock_profile_release_lock(&m->mtx_object);
210dde96c99SJohn Baldwin 	_rel_spin_lock(m);
2116283b7d0SJohn Baldwin }
2126283b7d0SJohn Baldwin 
2136283b7d0SJohn Baldwin /*
2149ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
215eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
216eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
2170cde2e34SJason Evans  */
2180cde2e34SJason Evans int
2199ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
2200cde2e34SJason Evans {
221fe68a916SKip Macy 	int rval, contested = 0;
2227c0435b9SKip Macy 	uint64_t waittime = 0;
2230cde2e34SJason Evans 
224b40ce416SJulian Elischer 	MPASS(curthread != NULL);
225186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
226186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
22783a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
22883cece6fSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
22983cece6fSJohn Baldwin 	    file, line));
2309ed346baSBosko Milekic 
231eac09796SJohn Baldwin 	if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) {
232eac09796SJohn Baldwin 		m->mtx_recurse++;
233eac09796SJohn Baldwin 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
234eac09796SJohn Baldwin 		rval = 1;
235eac09796SJohn Baldwin 	} else
236122eceefSJohn Baldwin 		rval = _obtain_lock(m, (uintptr_t)curthread);
2379ed346baSBosko Milekic 
23819284646SJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
239764e4d54SJohn Baldwin 	if (rval) {
2402d96f0b1SJohn Baldwin 		WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
2412d96f0b1SJohn Baldwin 		    file, line);
242764e4d54SJohn Baldwin 		curthread->td_locks++;
243fe68a916SKip Macy 		if (m->mtx_recurse == 0)
244fe68a916SKip Macy 			lock_profile_obtain_lock_success(&m->mtx_object, contested,
245fe68a916SKip Macy 			    waittime, file, line);
2467c0435b9SKip Macy 
247764e4d54SJohn Baldwin 	}
2489ed346baSBosko Milekic 
24919284646SJohn Baldwin 	return (rval);
2500cde2e34SJason Evans }
2510cde2e34SJason Evans 
2520cde2e34SJason Evans /*
2539ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
2549ed346baSBosko Milekic  *
2559ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
2569ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
2570cde2e34SJason Evans  */
2580cde2e34SJason Evans void
259122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
260bdcfcf5bSJohn Baldwin     int line)
26136412d79SJohn Baldwin {
262701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
26376447e56SJohn Baldwin 	volatile struct thread *owner;
2642498cf8cSJohn Baldwin #endif
26502bd1bcdSIan Dowse #ifdef KTR
26602bd1bcdSIan Dowse 	int cont_logged = 0;
26702bd1bcdSIan Dowse #endif
2687c0435b9SKip Macy 	uintptr_t v;
26936412d79SJohn Baldwin 
2705fa8dd90SJohn Baldwin 	if (mtx_owned(m)) {
271eac09796SJohn Baldwin 		KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0,
272eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
273eac09796SJohn Baldwin 		    m->mtx_object.lo_name, file, line));
27436412d79SJohn Baldwin 		m->mtx_recurse++;
27508812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
27619284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
2775746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
27836412d79SJohn Baldwin 		return;
27936412d79SJohn Baldwin 	}
2809ed346baSBosko Milekic 
28119284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
28215ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
28315ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
28419284646SJohn Baldwin 		    m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
2851bd0eefbSJohn Baldwin 
286122eceefSJohn Baldwin 	while (!_obtain_lock(m, tid)) {
2872ff0e645SJohn Baldwin 		turnstile_lock(&m->mtx_object);
2885fa8dd90SJohn Baldwin 		v = m->mtx_lock;
2895fa8dd90SJohn Baldwin 
29036412d79SJohn Baldwin 		/*
2919ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
292961a7b24SJohn Baldwin 		 * the turnstile chain lock.
29336412d79SJohn Baldwin 		 */
2945fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
295961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
2969f1b87f1SMaxime Henrion 			cpu_spinwait();
29736412d79SJohn Baldwin 			continue;
29836412d79SJohn Baldwin 		}
2999ed346baSBosko Milekic 
300535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
301535eb309SJohn Baldwin 		MPASS(v != MTX_CONTESTED);
302535eb309SJohn Baldwin #else
30336412d79SJohn Baldwin 		/*
3049ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
305f7ee1590SJohn Baldwin 		 * there are other threads blocked on it.  Grab ownership of
306f7ee1590SJohn Baldwin 		 * it and propagate its priority to the current thread if
307f7ee1590SJohn Baldwin 		 * necessary.
30836412d79SJohn Baldwin 		 */
30936412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
310122eceefSJohn Baldwin 			m->mtx_lock = tid | MTX_CONTESTED;
3112ff0e645SJohn Baldwin 			turnstile_claim(&m->mtx_object);
3128dc10be8SRobert Watson 			break;
31336412d79SJohn Baldwin 		}
314535eb309SJohn Baldwin #endif
3159ed346baSBosko Milekic 
31636412d79SJohn Baldwin 		/*
3179ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
3189ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
3199ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
32036412d79SJohn Baldwin 		 */
32136412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
322122eceefSJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
323961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
3249f1b87f1SMaxime Henrion 			cpu_spinwait();
32536412d79SJohn Baldwin 			continue;
32636412d79SJohn Baldwin 		}
32736412d79SJohn Baldwin 
328701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
3292498cf8cSJohn Baldwin 		/*
3302498cf8cSJohn Baldwin 		 * If the current owner of the lock is executing on another
3312498cf8cSJohn Baldwin 		 * CPU, spin instead of blocking.
3322498cf8cSJohn Baldwin 		 */
33349b94bfcSJohn Baldwin 		owner = (struct thread *)(v & ~MTX_FLAGMASK);
334a9abdce4SRobert Watson #ifdef ADAPTIVE_GIANT
3351364a812SKip Macy 		if (TD_IS_RUNNING(owner))
336a9abdce4SRobert Watson #else
3371364a812SKip Macy 		if (m != &Giant && TD_IS_RUNNING(owner))
338a9abdce4SRobert Watson #endif
3391364a812SKip Macy 		{
340961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
34127dad03cSJohn Baldwin 			while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
3429f1b87f1SMaxime Henrion 				cpu_spinwait();
3437fcca609SJohn Baldwin 			}
3442498cf8cSJohn Baldwin 			continue;
3452498cf8cSJohn Baldwin 		}
346701f1408SScott Long #endif	/* SMP && !NO_ADAPTIVE_MUTEXES */
3472498cf8cSJohn Baldwin 
3489ed346baSBosko Milekic 		/*
3497feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
3509ed346baSBosko Milekic 		 */
35136412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
35236412d79SJohn Baldwin 
35302bd1bcdSIan Dowse #ifdef KTR
35402bd1bcdSIan Dowse 		if (!cont_logged) {
35502bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
35602bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
357122eceefSJohn Baldwin 			    (void *)tid, file, line, m->mtx_object.lo_name,
35802bd1bcdSIan Dowse 			    WITNESS_FILE(&m->mtx_object),
35902bd1bcdSIan Dowse 			    WITNESS_LINE(&m->mtx_object));
36002bd1bcdSIan Dowse 			cont_logged = 1;
36102bd1bcdSIan Dowse 		}
36202bd1bcdSIan Dowse #endif
36336412d79SJohn Baldwin 
3649ed346baSBosko Milekic 		/*
365961a7b24SJohn Baldwin 		 * Block on the turnstile.
3669ed346baSBosko Milekic 		 */
3677aa4f685SJohn Baldwin 		turnstile_wait(&m->mtx_object, mtx_owner(m),
3687aa4f685SJohn Baldwin 		    TS_EXCLUSIVE_QUEUE);
36936412d79SJohn Baldwin 	}
37002bd1bcdSIan Dowse #ifdef KTR
37102bd1bcdSIan Dowse 	if (cont_logged) {
37202bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
37302bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
374122eceefSJohn Baldwin 		    m->mtx_object.lo_name, (void *)tid, file, line);
37502bd1bcdSIan Dowse 	}
37602bd1bcdSIan Dowse #endif
37736412d79SJohn Baldwin 	return;
3789ed346baSBosko Milekic }
3799ed346baSBosko Milekic 
38033fb8a38SJohn Baldwin #ifdef SMP
3819ed346baSBosko Milekic /*
3829ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
3839ed346baSBosko Milekic  *
3849ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
3859ed346baSBosko Milekic  * is handled inline.
3869ed346baSBosko Milekic  */
3879ed346baSBosko Milekic void
388122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file,
389bdcfcf5bSJohn Baldwin     int line)
39036412d79SJohn Baldwin {
391fe68a916SKip Macy 	int i = 0;
3920fa2168bSJohn Baldwin 	struct thread *td;
39336412d79SJohn Baldwin 
39419284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
3955746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
3969ed346baSBosko Milekic 
397f781b5a4SJohn Baldwin 	while (!_obtain_lock(m, tid)) {
3989ed346baSBosko Milekic 
3997141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
400c6a37e84SJohn Baldwin 		spinlock_exit();
40136412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
402703fc290SJohn Baldwin 			if (i++ < 10000000) {
4039f1b87f1SMaxime Henrion 				cpu_spinwait();
40436412d79SJohn Baldwin 				continue;
405703fc290SJohn Baldwin 			}
4060fa2168bSJohn Baldwin 			if (i < 60000000 || kdb_active || panicstr != NULL)
40736412d79SJohn Baldwin 				DELAY(1);
4080fa2168bSJohn Baldwin 			else {
4090fa2168bSJohn Baldwin 				td = mtx_owner(m);
4100fa2168bSJohn Baldwin 
4110fa2168bSJohn Baldwin 				/* If the mutex is unlocked, try again. */
4120fa2168bSJohn Baldwin 				if (td == NULL)
4130fa2168bSJohn Baldwin 					continue;
4140fa2168bSJohn Baldwin 				printf(
4150fa2168bSJohn Baldwin 			"spin lock %p (%s) held by %p (tid %d) too long\n",
4160fa2168bSJohn Baldwin 				    m, m->mtx_object.lo_name, td, td->td_tid);
41741109518SJohn Baldwin #ifdef WITNESS
4180fa2168bSJohn Baldwin 				witness_display_spinlock(&m->mtx_object, td);
41941109518SJohn Baldwin #endif
42041109518SJohn Baldwin 				panic("spin lock held too long");
42141109518SJohn Baldwin 			}
4229f1b87f1SMaxime Henrion 			cpu_spinwait();
42336412d79SJohn Baldwin 		}
424c6a37e84SJohn Baldwin 		spinlock_enter();
42536412d79SJohn Baldwin 	}
42636412d79SJohn Baldwin 
42719284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
4289ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
4299ed346baSBosko Milekic 
43036412d79SJohn Baldwin 	return;
43136412d79SJohn Baldwin }
43233fb8a38SJohn Baldwin #endif /* SMP */
43336412d79SJohn Baldwin 
4349ed346baSBosko Milekic /*
4359ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
4369ed346baSBosko Milekic  *
4379ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
4389ed346baSBosko Milekic  * need to wake up a blocked thread).
4399ed346baSBosko Milekic  */
44036412d79SJohn Baldwin void
4419ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
44236412d79SJohn Baldwin {
443961a7b24SJohn Baldwin 	struct turnstile *ts;
4440c0b25aeSJohn Baldwin #ifndef PREEMPTION
445b40ce416SJulian Elischer 	struct thread *td, *td1;
4460c0b25aeSJohn Baldwin #endif
4479ed346baSBosko Milekic 
44808812b39SBosko Milekic 	if (mtx_recursed(m)) {
44936412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
45008812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
45119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4529ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
45336412d79SJohn Baldwin 		return;
45436412d79SJohn Baldwin 	}
4559ed346baSBosko Milekic 
4562ff0e645SJohn Baldwin 	turnstile_lock(&m->mtx_object);
457961a7b24SJohn Baldwin 	ts = turnstile_lookup(&m->mtx_object);
45819284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
4599ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
4609ed346baSBosko Milekic 
461ece2d989SPawel Jakub Dawidek #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
462961a7b24SJohn Baldwin 	if (ts == NULL) {
4632498cf8cSJohn Baldwin 		_release_lock_quick(m);
4642498cf8cSJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4652498cf8cSJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
466961a7b24SJohn Baldwin 		turnstile_release(&m->mtx_object);
4672498cf8cSJohn Baldwin 		return;
4682498cf8cSJohn Baldwin 	}
469961a7b24SJohn Baldwin #else
470961a7b24SJohn Baldwin 	MPASS(ts != NULL);
4712498cf8cSJohn Baldwin #endif
4720c0b25aeSJohn Baldwin #ifndef PREEMPTION
473961a7b24SJohn Baldwin 	/* XXX */
4747aa4f685SJohn Baldwin 	td1 = turnstile_head(ts, TS_EXCLUSIVE_QUEUE);
4750c0b25aeSJohn Baldwin #endif
476535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
4777aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
478535eb309SJohn Baldwin 	_release_lock_quick(m);
479535eb309SJohn Baldwin #else
4807aa4f685SJohn Baldwin 	if (turnstile_signal(ts, TS_EXCLUSIVE_QUEUE)) {
48136412d79SJohn Baldwin 		_release_lock_quick(m);
48219284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4839ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
484961a7b24SJohn Baldwin 	} else {
485f7ee1590SJohn Baldwin 		m->mtx_lock = MTX_CONTESTED;
48619284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
487961a7b24SJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
488961a7b24SJohn Baldwin 			    m);
489e0817317SJulian Elischer 	}
490535eb309SJohn Baldwin #endif
4917aa4f685SJohn Baldwin 	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
4929ed346baSBosko Milekic 
4930c0b25aeSJohn Baldwin #ifndef PREEMPTION
494961a7b24SJohn Baldwin 	/*
495961a7b24SJohn Baldwin 	 * XXX: This is just a hack until preemption is done.  However,
496961a7b24SJohn Baldwin 	 * once preemption is done we need to either wrap the
497961a7b24SJohn Baldwin 	 * turnstile_signal() and release of the actual lock in an
498961a7b24SJohn Baldwin 	 * extra critical section or change the preemption code to
499961a7b24SJohn Baldwin 	 * always just set a flag and never do instant-preempts.
500961a7b24SJohn Baldwin 	 */
501961a7b24SJohn Baldwin 	td = curthread;
502961a7b24SJohn Baldwin 	if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
503961a7b24SJohn Baldwin 		return;
504961a7b24SJohn Baldwin 	mtx_lock_spin(&sched_lock);
505961a7b24SJohn Baldwin 	if (!TD_IS_RUNNING(td1)) {
50636412d79SJohn Baldwin #ifdef notyet
507b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
508b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
50936412d79SJohn Baldwin 
51036412d79SJohn Baldwin 			if (it->it_interrupted) {
51119284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
51236412d79SJohn Baldwin 					CTR2(KTR_LOCK,
51315ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
51436412d79SJohn Baldwin 					    it, it->it_interrupted);
51536412d79SJohn Baldwin 				intr_thd_fixup(it);
51636412d79SJohn Baldwin 			}
51736412d79SJohn Baldwin 		}
51836412d79SJohn Baldwin #endif
51919284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
520562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
5219ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
5229ed346baSBosko Milekic 			    (void *)m->mtx_lock);
5239ed346baSBosko Milekic 
524bf0acc27SJohn Baldwin 		mi_switch(SW_INVOL, NULL);
52519284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
5269ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
52731271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
52836412d79SJohn Baldwin 	}
5299ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
5300c0b25aeSJohn Baldwin #endif
5319ed346baSBosko Milekic 
5329ed346baSBosko Milekic 	return;
5339ed346baSBosko Milekic }
5349ed346baSBosko Milekic 
5359ed346baSBosko Milekic /*
5369ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
5379ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
5389ed346baSBosko Milekic  */
5399ed346baSBosko Milekic 
5409ed346baSBosko Milekic /*
54115ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
5429ed346baSBosko Milekic  */
5431103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
5440cde2e34SJason Evans void
54556771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
5460cde2e34SJason Evans {
5475cb0fbe4SJohn Baldwin 
5481126349aSPaul Saab 	if (panicstr != NULL || dumping)
5495cb0fbe4SJohn Baldwin 		return;
550a10f4966SJake Burkholder 	switch (what) {
5510cde2e34SJason Evans 	case MA_OWNED:
5520cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
5530cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
554a10f4966SJake Burkholder 		if (!mtx_owned(m))
5550cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
55619284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
557a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
558a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
5590cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
56019284646SJohn Baldwin 				    m->mtx_object.lo_name, file, line);
561a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
5620cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
56319284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
5640cde2e34SJason Evans 		}
5650cde2e34SJason Evans 		break;
5660cde2e34SJason Evans 	case MA_NOTOWNED:
567a10f4966SJake Burkholder 		if (mtx_owned(m))
5680cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
56919284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
5700cde2e34SJason Evans 		break;
5710cde2e34SJason Evans 	default:
57256771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
5730cde2e34SJason Evans 	}
5740cde2e34SJason Evans }
5750cde2e34SJason Evans #endif
5760cde2e34SJason Evans 
5779ed346baSBosko Milekic /*
5789ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
57919284646SJohn Baldwin  *
58019284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
58119284646SJohn Baldwin  * maintained by the witness code.
5829ed346baSBosko Milekic  */
58336412d79SJohn Baldwin #ifdef MUTEX_DEBUG
58436412d79SJohn Baldwin 
5854d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
58636412d79SJohn Baldwin 
58719284646SJohn Baldwin void
58819284646SJohn Baldwin mtx_validate(struct mtx *m)
58936412d79SJohn Baldwin {
59036412d79SJohn Baldwin 
59136412d79SJohn Baldwin /*
592fa669ab7SPoul-Henning Kamp  * XXX: When kernacc() does not require Giant we can reenable this check
593fa669ab7SPoul-Henning Kamp  */
594fa669ab7SPoul-Henning Kamp #ifdef notyet
595fa669ab7SPoul-Henning Kamp 	/*
59676dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
59776dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
59876dcbd6fSBosko Milekic 	 * requires Giant itself.
59976dcbd6fSBosko Milekic 	 */
600ab07087eSBosko Milekic 	if (!cold)
601ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
602ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
60319284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
60436412d79SJohn Baldwin #endif
60536412d79SJohn Baldwin }
60636412d79SJohn Baldwin #endif
60736412d79SJohn Baldwin 
6089ed346baSBosko Milekic /*
609c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
610c27b5699SAndrew R. Reiter  */
611c27b5699SAndrew R. Reiter void
612c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
613c27b5699SAndrew R. Reiter {
614c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
615c27b5699SAndrew R. Reiter 
6160c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
617c27b5699SAndrew R. Reiter }
618c27b5699SAndrew R. Reiter 
619c27b5699SAndrew R. Reiter /*
6209ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
6210c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
6220c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
6230c88508aSJohn Baldwin  * witness.
6249ed346baSBosko Milekic  */
62536412d79SJohn Baldwin void
6260c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
62736412d79SJohn Baldwin {
62883a81bcbSJohn Baldwin 	struct lock_class *class;
62983a81bcbSJohn Baldwin 	int flags;
6309ed346baSBosko Milekic 
63119284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
6327c0435b9SKip Macy 		MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0);
6339ed346baSBosko Milekic 
63436412d79SJohn Baldwin #ifdef MUTEX_DEBUG
6359ed346baSBosko Milekic 	/* Diagnostic and error correction */
63619284646SJohn Baldwin 	mtx_validate(m);
6376936206eSJohn Baldwin #endif
63836412d79SJohn Baldwin 
63983a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
64019284646SJohn Baldwin 	if (opts & MTX_SPIN)
64183a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
64219284646SJohn Baldwin 	else
64383a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
64483a81bcbSJohn Baldwin 	flags = 0;
64519284646SJohn Baldwin 	if (opts & MTX_QUIET)
64683a81bcbSJohn Baldwin 		flags |= LO_QUIET;
64719284646SJohn Baldwin 	if (opts & MTX_RECURSE)
64883a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
64919284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
65083a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
651f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
65283a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
6537c0435b9SKip Macy 	if (opts & MTX_NOPROFILE)
6547c0435b9SKip Macy 		flags |= LO_NOPROFILE;
65519284646SJohn Baldwin 
65683a81bcbSJohn Baldwin 	/* Initialize mutex. */
65719284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
65883a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
6599ed346baSBosko Milekic 
66061bd5e21SKip Macy 	lock_profile_object_init(&m->mtx_object, class, name);
66183a81bcbSJohn Baldwin 	lock_init(&m->mtx_object, class, name, type, flags);
66236412d79SJohn Baldwin }
66336412d79SJohn Baldwin 
6649ed346baSBosko Milekic /*
66519284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
66619284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
66719284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
66819284646SJohn Baldwin  * flags.
6699ed346baSBosko Milekic  */
67036412d79SJohn Baldwin void
67136412d79SJohn Baldwin mtx_destroy(struct mtx *m)
67236412d79SJohn Baldwin {
67336412d79SJohn Baldwin 
67419284646SJohn Baldwin 	if (!mtx_owned(m))
67519284646SJohn Baldwin 		MPASS(mtx_unowned(m));
67619284646SJohn Baldwin 	else {
67708812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
6789ed346baSBosko Milekic 
679861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
68083a81bcbSJohn Baldwin 		if (LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin)
681861a2308SScott Long 			spinlock_exit();
682764e4d54SJohn Baldwin 		else
683764e4d54SJohn Baldwin 			curthread->td_locks--;
684861a2308SScott Long 
68519284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
686c86b6ff5SJohn Baldwin 		WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
687c86b6ff5SJohn Baldwin 		    __LINE__);
68836412d79SJohn Baldwin 	}
6890384fff8SJason Evans 
690186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
6917c0435b9SKip Macy 	lock_profile_object_destroy(&m->mtx_object);
69283a81bcbSJohn Baldwin 	lock_destroy(&m->mtx_object);
6930384fff8SJason Evans }
694d23f5958SMatthew Dillon 
695d23f5958SMatthew Dillon /*
696c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
697c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
698c53c013bSJohn Baldwin  * setup before this is called.
699c53c013bSJohn Baldwin  */
700c53c013bSJohn Baldwin void
701c53c013bSJohn Baldwin mutex_init(void)
702c53c013bSJohn Baldwin {
703c53c013bSJohn Baldwin 
704961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
705961a7b24SJohn Baldwin 	init_turnstiles();
706961a7b24SJohn Baldwin 
707c53c013bSJohn Baldwin 	/*
708c53c013bSJohn Baldwin 	 * Initialize mutexes.
709c53c013bSJohn Baldwin 	 */
7100c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
7110c88508aSJohn Baldwin 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
7120c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
7138c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
714c53c013bSJohn Baldwin 	mtx_lock(&Giant);
7157c0435b9SKip Macy 
7167c0435b9SKip Macy 	lock_profile_init();
717c53c013bSJohn Baldwin }
718d272fe53SJohn Baldwin 
719d272fe53SJohn Baldwin #ifdef DDB
720d272fe53SJohn Baldwin void
721d272fe53SJohn Baldwin db_show_mtx(struct lock_object *lock)
722d272fe53SJohn Baldwin {
723d272fe53SJohn Baldwin 	struct thread *td;
724d272fe53SJohn Baldwin 	struct mtx *m;
725d272fe53SJohn Baldwin 
726d272fe53SJohn Baldwin 	m = (struct mtx *)lock;
727d272fe53SJohn Baldwin 
728d272fe53SJohn Baldwin 	db_printf(" flags: {");
72983a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
730d272fe53SJohn Baldwin 		db_printf("SPIN");
731d272fe53SJohn Baldwin 	else
732d272fe53SJohn Baldwin 		db_printf("DEF");
733d272fe53SJohn Baldwin 	if (m->mtx_object.lo_flags & LO_RECURSABLE)
734d272fe53SJohn Baldwin 		db_printf(", RECURSE");
735d272fe53SJohn Baldwin 	if (m->mtx_object.lo_flags & LO_DUPOK)
736d272fe53SJohn Baldwin 		db_printf(", DUPOK");
737d272fe53SJohn Baldwin 	db_printf("}\n");
738d272fe53SJohn Baldwin 	db_printf(" state: {");
739d272fe53SJohn Baldwin 	if (mtx_unowned(m))
740d272fe53SJohn Baldwin 		db_printf("UNOWNED");
741d272fe53SJohn Baldwin 	else {
742d272fe53SJohn Baldwin 		db_printf("OWNED");
743d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
744d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
745d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
746d272fe53SJohn Baldwin 			db_printf(", RECURSED");
747d272fe53SJohn Baldwin 	}
748d272fe53SJohn Baldwin 	db_printf("}\n");
749d272fe53SJohn Baldwin 	if (!mtx_unowned(m)) {
750d272fe53SJohn Baldwin 		td = mtx_owner(m);
751d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
752d272fe53SJohn Baldwin 		    td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
753d272fe53SJohn Baldwin 		if (mtx_recursed(m))
754d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
755d272fe53SJohn Baldwin 	}
756d272fe53SJohn Baldwin }
757d272fe53SJohn Baldwin #endif
758