xref: /freebsd/sys/kern/kern_mutex.c (revision d272fe53a49fff21d1b3bf5e6f27f16676a1e07f)
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"
4100096801SJohn-Mark Gurney #include "opt_mprof.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>
620384fff8SJason Evans 
6336412d79SJohn Baldwin #include <machine/atomic.h>
6436412d79SJohn Baldwin #include <machine/bus.h>
6536412d79SJohn Baldwin #include <machine/clock.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 
899ed346baSBosko Milekic #define mtx_owner(m)	(mtx_unowned((m)) ? NULL \
90b40ce416SJulian Elischer 	: (struct thread *)((m)->mtx_lock & MTX_FLAGMASK))
919ed346baSBosko Milekic 
92d272fe53SJohn Baldwin #ifdef DDB
93d272fe53SJohn Baldwin static void	db_show_mtx(struct lock_object *lock);
94d272fe53SJohn Baldwin #endif
95d272fe53SJohn Baldwin 
960cde2e34SJason Evans /*
9719284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
980cde2e34SJason Evans  */
9919284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
10019284646SJohn Baldwin 	"sleep mutex",
101d272fe53SJohn Baldwin 	LC_SLEEPLOCK | LC_RECURSABLE,
102d272fe53SJohn Baldwin #ifdef DDB
103d272fe53SJohn Baldwin 	db_show_mtx
104d272fe53SJohn Baldwin #endif
10519284646SJohn Baldwin };
10619284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
10719284646SJohn Baldwin 	"spin mutex",
108d272fe53SJohn Baldwin 	LC_SPINLOCK | LC_RECURSABLE,
109d272fe53SJohn Baldwin #ifdef DDB
110d272fe53SJohn Baldwin 	db_show_mtx
111d272fe53SJohn Baldwin #endif
1128484de75SJohn Baldwin };
1138484de75SJohn Baldwin 
1149ed346baSBosko Milekic /*
115c53c013bSJohn Baldwin  * System-wide mutexes
116c53c013bSJohn Baldwin  */
117c53c013bSJohn Baldwin struct mtx sched_lock;
118c53c013bSJohn Baldwin struct mtx Giant;
119c53c013bSJohn Baldwin 
1206c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
1216c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging");
1226c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling");
1236c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0;
1246c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW,
1256c35e809SDag-Erling Smørgrav     &mutex_prof_enable, 0, "Enable tracing of mutex holdtime");
1266c35e809SDag-Erling Smørgrav 
1276c35e809SDag-Erling Smørgrav struct mutex_prof {
1286c35e809SDag-Erling Smørgrav 	const char	*name;
1296c35e809SDag-Erling Smørgrav 	const char	*file;
1306c35e809SDag-Erling Smørgrav 	int		line;
131ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_max;
132ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_tot;
133ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_cur;
1348dc10be8SRobert Watson 	uintmax_t	cnt_contest_holding;
1358dc10be8SRobert Watson 	uintmax_t	cnt_contest_locking;
136e6330704SDag-Erling Smørgrav 	struct mutex_prof *next;
1376c35e809SDag-Erling Smørgrav };
1386c35e809SDag-Erling Smørgrav 
1396c35e809SDag-Erling Smørgrav /*
1406c35e809SDag-Erling Smørgrav  * mprof_buf is a static pool of profiling records to avoid possible
1416c35e809SDag-Erling Smørgrav  * reentrance of the memory allocation functions.
1426c35e809SDag-Erling Smørgrav  *
1436c35e809SDag-Erling Smørgrav  * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE.
1446c35e809SDag-Erling Smørgrav  */
14500096801SJohn-Mark Gurney #ifdef MPROF_BUFFERS
14600096801SJohn-Mark Gurney #define NUM_MPROF_BUFFERS	MPROF_BUFFERS
14700096801SJohn-Mark Gurney #else
148e6330704SDag-Erling Smørgrav #define	NUM_MPROF_BUFFERS	1000
14900096801SJohn-Mark Gurney #endif
1506c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS];
1516c35e809SDag-Erling Smørgrav static int first_free_mprof_buf;
15200096801SJohn-Mark Gurney #ifndef MPROF_HASH_SIZE
153e6330704SDag-Erling Smørgrav #define	MPROF_HASH_SIZE		1009
15400096801SJohn-Mark Gurney #endif
15500096801SJohn-Mark Gurney #if NUM_MPROF_BUFFERS >= MPROF_HASH_SIZE
15600096801SJohn-Mark Gurney #error MPROF_BUFFERS must be larger than MPROF_HASH_SIZE
15700096801SJohn-Mark Gurney #endif
1586c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE];
1590bd5f797SMike Makonnen /* SWAG: sbuf size = avg stat. line size * number of locks */
1600bd5f797SMike Makonnen #define MPROF_SBUF_SIZE		256 * 400
1616c35e809SDag-Erling Smørgrav 
1626c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions;
1636c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD,
1646c35e809SDag-Erling Smørgrav     &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded");
1656c35e809SDag-Erling Smørgrav static int mutex_prof_records;
1666c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD,
1676c35e809SDag-Erling Smørgrav     &mutex_prof_records, 0, "Number of profiling records");
1686c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS;
1696c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD,
1706c35e809SDag-Erling Smørgrav     &mutex_prof_maxrecords, 0, "Maximum number of profiling records");
1716c35e809SDag-Erling Smørgrav static int mutex_prof_rejected;
1726c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD,
1736c35e809SDag-Erling Smørgrav     &mutex_prof_rejected, 0, "Number of rejected profiling records");
1746c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE;
1756c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD,
1766c35e809SDag-Erling Smørgrav     &mutex_prof_hashsize, 0, "Hash size");
1776c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0;
1786c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD,
1796c35e809SDag-Erling Smørgrav     &mutex_prof_collisions, 0, "Number of hash collisions");
1806c35e809SDag-Erling Smørgrav 
1816c35e809SDag-Erling Smørgrav /*
1826c35e809SDag-Erling Smørgrav  * mprof_mtx protects the profiling buffers and the hash.
1836c35e809SDag-Erling Smørgrav  */
1846c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx;
185e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET);
1866c35e809SDag-Erling Smørgrav 
187b784ffe9SDag-Erling Smørgrav static u_int64_t
188b784ffe9SDag-Erling Smørgrav nanoseconds(void)
189b784ffe9SDag-Erling Smørgrav {
190b784ffe9SDag-Erling Smørgrav 	struct timespec tv;
191b784ffe9SDag-Erling Smørgrav 
192b784ffe9SDag-Erling Smørgrav 	nanotime(&tv);
193b784ffe9SDag-Erling Smørgrav 	return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec);
194b784ffe9SDag-Erling Smørgrav }
195b784ffe9SDag-Erling Smørgrav 
1966c35e809SDag-Erling Smørgrav static int
1976c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
1986c35e809SDag-Erling Smørgrav {
1996c35e809SDag-Erling Smørgrav 	struct sbuf *sb;
2006c35e809SDag-Erling Smørgrav 	int error, i;
2010bd5f797SMike Makonnen 	static int multiplier = 1;
2026c35e809SDag-Erling Smørgrav 
2036c35e809SDag-Erling Smørgrav 	if (first_free_mprof_buf == 0)
2046d036900SDag-Erling Smørgrav 		return (SYSCTL_OUT(req, "No locking recorded",
2056d036900SDag-Erling Smørgrav 		    sizeof("No locking recorded")));
2066c35e809SDag-Erling Smørgrav 
2070bd5f797SMike Makonnen retry_sbufops:
2080bd5f797SMike Makonnen 	sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN);
2094f201858SGleb Smirnoff 	sbuf_printf(sb, "\n%6s %12s %11s %5s %12s %12s %s\n",
2108dc10be8SRobert Watson 	    "max", "total", "count", "avg", "cnt_hold", "cnt_lock", "name");
2116d036900SDag-Erling Smørgrav 	/*
2126d036900SDag-Erling Smørgrav 	 * XXX this spinlock seems to be by far the largest perpetrator
2136d036900SDag-Erling Smørgrav 	 * of spinlock latency (1.6 msec on an Athlon1600 was recorded
2146d036900SDag-Erling Smørgrav 	 * even before I pessimized it further by moving the average
2156d036900SDag-Erling Smørgrav 	 * computation here).
2166d036900SDag-Erling Smørgrav 	 */
2176c35e809SDag-Erling Smørgrav 	mtx_lock_spin(&mprof_mtx);
2180bd5f797SMike Makonnen 	for (i = 0; i < first_free_mprof_buf; ++i) {
2198dc10be8SRobert Watson 		sbuf_printf(sb, "%6ju %12ju %11ju %5ju %12ju %12ju %s:%d (%s)\n",
220ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_max / 1000,
221ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_tot / 1000,
222ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_cur,
223ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 :
224ecf031c9SDag-Erling Smørgrav 			mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000),
2258dc10be8SRobert Watson 		    mprof_buf[i].cnt_contest_holding,
2268dc10be8SRobert Watson 		    mprof_buf[i].cnt_contest_locking,
2276c35e809SDag-Erling Smørgrav 		    mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name);
2280bd5f797SMike Makonnen 		if (sbuf_overflowed(sb)) {
2290bd5f797SMike Makonnen 			mtx_unlock_spin(&mprof_mtx);
2300bd5f797SMike Makonnen 			sbuf_delete(sb);
2310bd5f797SMike Makonnen 			multiplier++;
2320bd5f797SMike Makonnen 			goto retry_sbufops;
2330bd5f797SMike Makonnen 		}
2340bd5f797SMike Makonnen 	}
2356c35e809SDag-Erling Smørgrav 	mtx_unlock_spin(&mprof_mtx);
2366c35e809SDag-Erling Smørgrav 	sbuf_finish(sb);
2376c35e809SDag-Erling Smørgrav 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2386c35e809SDag-Erling Smørgrav 	sbuf_delete(sb);
2396c35e809SDag-Erling Smørgrav 	return (error);
2406c35e809SDag-Erling Smørgrav }
2416c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD,
2426c35e809SDag-Erling Smørgrav     NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics");
24394ffb20dSRobert Watson 
24494ffb20dSRobert Watson static int
24594ffb20dSRobert Watson reset_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
24694ffb20dSRobert Watson {
24794ffb20dSRobert Watson 	int error, v;
24894ffb20dSRobert Watson 
24994ffb20dSRobert Watson 	if (first_free_mprof_buf == 0)
25094ffb20dSRobert Watson 		return (0);
25194ffb20dSRobert Watson 
25294ffb20dSRobert Watson 	v = 0;
25394ffb20dSRobert Watson 	error = sysctl_handle_int(oidp, &v, 0, req);
25494ffb20dSRobert Watson 	if (error)
25594ffb20dSRobert Watson 		return (error);
25694ffb20dSRobert Watson 	if (req->newptr == NULL)
25794ffb20dSRobert Watson 		return (error);
25894ffb20dSRobert Watson 	if (v == 0)
25994ffb20dSRobert Watson 		return (0);
26094ffb20dSRobert Watson 
26194ffb20dSRobert Watson 	mtx_lock_spin(&mprof_mtx);
26294ffb20dSRobert Watson 	bzero(mprof_buf, sizeof(*mprof_buf) * first_free_mprof_buf);
26394ffb20dSRobert Watson 	bzero(mprof_hash, sizeof(struct mtx *) * MPROF_HASH_SIZE);
26494ffb20dSRobert Watson 	first_free_mprof_buf = 0;
26594ffb20dSRobert Watson 	mtx_unlock_spin(&mprof_mtx);
26694ffb20dSRobert Watson 	return (0);
26794ffb20dSRobert Watson }
26894ffb20dSRobert Watson SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW,
26994ffb20dSRobert Watson     NULL, 0, reset_mutex_prof_stats, "I", "Reset mutex profiling statistics");
2706c35e809SDag-Erling Smørgrav #endif
2716c35e809SDag-Erling Smørgrav 
2720cde2e34SJason Evans /*
2736283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2746283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2756283b7d0SJohn Baldwin  */
2766283b7d0SJohn Baldwin void
2776283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
2786283b7d0SJohn Baldwin {
2796283b7d0SJohn Baldwin 
280dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
2810d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
2820d975d63SJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
2830d975d63SJohn Baldwin 	    file, line));
2848d768e76SJohn Baldwin 	WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
2858d768e76SJohn Baldwin 	    file, line);
286dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
287dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
288dde96c99SJohn Baldwin 	    line);
289dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
2906c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
2916c35e809SDag-Erling Smørgrav 	/* don't reset the timer when/if recursing */
292b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime == 0) {
293b61860adSDag-Erling Smørgrav 		m->mtx_filename = file;
294b61860adSDag-Erling Smørgrav 		m->mtx_lineno = line;
295b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0;
2966c35e809SDag-Erling Smørgrav 		++mutex_prof_acquisitions;
2976c35e809SDag-Erling Smørgrav 	}
2986c35e809SDag-Erling Smørgrav #endif
2996283b7d0SJohn Baldwin }
3006283b7d0SJohn Baldwin 
3016283b7d0SJohn Baldwin void
3026283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
3036283b7d0SJohn Baldwin {
3046283b7d0SJohn Baldwin 
305dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3060d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
3070d975d63SJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
3080d975d63SJohn Baldwin 	    file, line));
3090d975d63SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3100d975d63SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
3110d975d63SJohn Baldwin 	    line);
31221377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
3136c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
314b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime != 0) {
3156c35e809SDag-Erling Smørgrav 		static const char *unknown = "(unknown)";
3166c35e809SDag-Erling Smørgrav 		struct mutex_prof *mpp;
317b784ffe9SDag-Erling Smørgrav 		u_int64_t acqtime, now;
3186c35e809SDag-Erling Smørgrav 		const char *p, *q;
319e6330704SDag-Erling Smørgrav 		volatile u_int hash;
3206c35e809SDag-Erling Smørgrav 
321b784ffe9SDag-Erling Smørgrav 		now = nanoseconds();
322b61860adSDag-Erling Smørgrav 		acqtime = m->mtx_acqtime;
323b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = 0;
324b784ffe9SDag-Erling Smørgrav 		if (now <= acqtime)
3256c35e809SDag-Erling Smørgrav 			goto out;
3260bd5f797SMike Makonnen 		for (p = m->mtx_filename;
3270bd5f797SMike Makonnen 		    p != NULL && strncmp(p, "../", 3) == 0; p += 3)
3286c35e809SDag-Erling Smørgrav 			/* nothing */ ;
3296c35e809SDag-Erling Smørgrav 		if (p == NULL || *p == '\0')
3306c35e809SDag-Erling Smørgrav 			p = unknown;
331b61860adSDag-Erling Smørgrav 		for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q)
3326c35e809SDag-Erling Smørgrav 			hash = (hash * 2 + *q) % MPROF_HASH_SIZE;
3336c35e809SDag-Erling Smørgrav 		mtx_lock_spin(&mprof_mtx);
334e6330704SDag-Erling Smørgrav 		for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next)
335b61860adSDag-Erling Smørgrav 			if (mpp->line == m->mtx_lineno &&
336b61860adSDag-Erling Smørgrav 			    strcmp(mpp->file, p) == 0)
3376c35e809SDag-Erling Smørgrav 				break;
3386c35e809SDag-Erling Smørgrav 		if (mpp == NULL) {
3396c35e809SDag-Erling Smørgrav 			/* Just exit if we cannot get a trace buffer */
3406c35e809SDag-Erling Smørgrav 			if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) {
3416c35e809SDag-Erling Smørgrav 				++mutex_prof_rejected;
3426c35e809SDag-Erling Smørgrav 				goto unlock;
3436c35e809SDag-Erling Smørgrav 			}
3446c35e809SDag-Erling Smørgrav 			mpp = &mprof_buf[first_free_mprof_buf++];
3456c35e809SDag-Erling Smørgrav 			mpp->name = mtx_name(m);
3466c35e809SDag-Erling Smørgrav 			mpp->file = p;
347b61860adSDag-Erling Smørgrav 			mpp->line = m->mtx_lineno;
348e6330704SDag-Erling Smørgrav 			mpp->next = mprof_hash[hash];
349e6330704SDag-Erling Smørgrav 			if (mprof_hash[hash] != NULL)
350e6330704SDag-Erling Smørgrav 				++mutex_prof_collisions;
3516c35e809SDag-Erling Smørgrav 			mprof_hash[hash] = mpp;
352e6330704SDag-Erling Smørgrav 			++mutex_prof_records;
3536c35e809SDag-Erling Smørgrav 		}
3546c35e809SDag-Erling Smørgrav 		/*
3556c35e809SDag-Erling Smørgrav 		 * Record if the mutex has been held longer now than ever
3566d036900SDag-Erling Smørgrav 		 * before.
3576c35e809SDag-Erling Smørgrav 		 */
358ecf031c9SDag-Erling Smørgrav 		if (now - acqtime > mpp->cnt_max)
359ecf031c9SDag-Erling Smørgrav 			mpp->cnt_max = now - acqtime;
360ecf031c9SDag-Erling Smørgrav 		mpp->cnt_tot += now - acqtime;
361ecf031c9SDag-Erling Smørgrav 		mpp->cnt_cur++;
3628dc10be8SRobert Watson 		/*
3638dc10be8SRobert Watson 		 * There's a small race, really we should cmpxchg
3648dc10be8SRobert Watson 		 * 0 with the current value, but that would bill
3658dc10be8SRobert Watson 		 * the contention to the wrong lock instance if
3668dc10be8SRobert Watson 		 * it followed this also.
3678dc10be8SRobert Watson 		 */
3688dc10be8SRobert Watson 		mpp->cnt_contest_holding += m->mtx_contest_holding;
3698dc10be8SRobert Watson 		m->mtx_contest_holding = 0;
3708dc10be8SRobert Watson 		mpp->cnt_contest_locking += m->mtx_contest_locking;
3718dc10be8SRobert Watson 		m->mtx_contest_locking = 0;
3726c35e809SDag-Erling Smørgrav unlock:
3736c35e809SDag-Erling Smørgrav 		mtx_unlock_spin(&mprof_mtx);
3746c35e809SDag-Erling Smørgrav 	}
3756c35e809SDag-Erling Smørgrav out:
3766c35e809SDag-Erling Smørgrav #endif
377dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
3786283b7d0SJohn Baldwin }
3796283b7d0SJohn Baldwin 
3806283b7d0SJohn Baldwin void
3816283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3826283b7d0SJohn Baldwin {
3836283b7d0SJohn Baldwin 
384dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3850d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
3860d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
3870d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
3888d768e76SJohn Baldwin 	WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
3898d768e76SJohn Baldwin 	    file, line);
390dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
391dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
392dde96c99SJohn Baldwin 	    line);
393dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3946283b7d0SJohn Baldwin }
3956283b7d0SJohn Baldwin 
3966283b7d0SJohn Baldwin void
3976283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3986283b7d0SJohn Baldwin {
3996283b7d0SJohn Baldwin 
400dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
4010d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
4020d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
4030d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
404dde96c99SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
405dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
406dde96c99SJohn Baldwin 	    line);
4070d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
408dde96c99SJohn Baldwin 	_rel_spin_lock(m);
4096283b7d0SJohn Baldwin }
4106283b7d0SJohn Baldwin 
4116283b7d0SJohn Baldwin /*
4129ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
413eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
414eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
4150cde2e34SJason Evans  */
4160cde2e34SJason Evans int
4179ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
4180cde2e34SJason Evans {
4190cde2e34SJason Evans 	int rval;
4200cde2e34SJason Evans 
421b40ce416SJulian Elischer 	MPASS(curthread != NULL);
42283cece6fSJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
42383cece6fSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
42483cece6fSJohn Baldwin 	    file, line));
4259ed346baSBosko Milekic 
426eac09796SJohn Baldwin 	if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) {
427eac09796SJohn Baldwin 		m->mtx_recurse++;
428eac09796SJohn Baldwin 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
429eac09796SJohn Baldwin 		rval = 1;
430eac09796SJohn Baldwin 	} else
431122eceefSJohn Baldwin 		rval = _obtain_lock(m, (uintptr_t)curthread);
4329ed346baSBosko Milekic 
43319284646SJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
4346b869595SJohn Baldwin 	if (rval)
4352d96f0b1SJohn Baldwin 		WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4362d96f0b1SJohn Baldwin 		    file, line);
4379ed346baSBosko Milekic 
43819284646SJohn Baldwin 	return (rval);
4390cde2e34SJason Evans }
4400cde2e34SJason Evans 
4410cde2e34SJason Evans /*
4429ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4439ed346baSBosko Milekic  *
4449ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4459ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4460cde2e34SJason Evans  */
4470cde2e34SJason Evans void
448122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
449bdcfcf5bSJohn Baldwin     int line)
45036412d79SJohn Baldwin {
451701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
4522498cf8cSJohn Baldwin 	struct thread *owner;
4532498cf8cSJohn Baldwin #endif
4545fa8dd90SJohn Baldwin 	uintptr_t v;
45502bd1bcdSIan Dowse #ifdef KTR
45602bd1bcdSIan Dowse 	int cont_logged = 0;
45702bd1bcdSIan Dowse #endif
4588dc10be8SRobert Watson #ifdef MUTEX_PROFILING
4598dc10be8SRobert Watson 	int contested;
4608dc10be8SRobert Watson #endif
46136412d79SJohn Baldwin 
4625fa8dd90SJohn Baldwin 	if (mtx_owned(m)) {
463eac09796SJohn Baldwin 		KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0,
464eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
465eac09796SJohn Baldwin 		    m->mtx_object.lo_name, file, line));
46636412d79SJohn Baldwin 		m->mtx_recurse++;
46708812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
46819284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4695746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
47036412d79SJohn Baldwin 		return;
47136412d79SJohn Baldwin 	}
4729ed346baSBosko Milekic 
47319284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
47415ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
47515ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
47619284646SJohn Baldwin 		    m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
4771bd0eefbSJohn Baldwin 
4788dc10be8SRobert Watson #ifdef MUTEX_PROFILING
4798dc10be8SRobert Watson 	contested = 0;
4808dc10be8SRobert Watson #endif
481122eceefSJohn Baldwin 	while (!_obtain_lock(m, tid)) {
4828dc10be8SRobert Watson #ifdef MUTEX_PROFILING
4838dc10be8SRobert Watson 		contested = 1;
4848dc10be8SRobert Watson 		atomic_add_int(&m->mtx_contest_holding, 1);
4858dc10be8SRobert Watson #endif
4862ff0e645SJohn Baldwin 		turnstile_lock(&m->mtx_object);
4875fa8dd90SJohn Baldwin 		v = m->mtx_lock;
4885fa8dd90SJohn Baldwin 
48936412d79SJohn Baldwin 		/*
4909ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
491961a7b24SJohn Baldwin 		 * the turnstile chain lock.
49236412d79SJohn Baldwin 		 */
4935fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
494961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
4959f1b87f1SMaxime Henrion 			cpu_spinwait();
49636412d79SJohn Baldwin 			continue;
49736412d79SJohn Baldwin 		}
4989ed346baSBosko Milekic 
499535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
500535eb309SJohn Baldwin 		MPASS(v != MTX_CONTESTED);
501535eb309SJohn Baldwin #else
50236412d79SJohn Baldwin 		/*
5039ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
504f7ee1590SJohn Baldwin 		 * there are other threads blocked on it.  Grab ownership of
505f7ee1590SJohn Baldwin 		 * it and propagate its priority to the current thread if
506f7ee1590SJohn Baldwin 		 * necessary.
50736412d79SJohn Baldwin 		 */
50836412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
509122eceefSJohn Baldwin 			m->mtx_lock = tid | MTX_CONTESTED;
5102ff0e645SJohn Baldwin 			turnstile_claim(&m->mtx_object);
5118dc10be8SRobert Watson 			break;
51236412d79SJohn Baldwin 		}
513535eb309SJohn Baldwin #endif
5149ed346baSBosko Milekic 
51536412d79SJohn Baldwin 		/*
5169ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
5179ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
5189ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
51936412d79SJohn Baldwin 		 */
52036412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
521122eceefSJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
522961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
5239f1b87f1SMaxime Henrion 			cpu_spinwait();
52436412d79SJohn Baldwin 			continue;
52536412d79SJohn Baldwin 		}
52636412d79SJohn Baldwin 
527701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
5282498cf8cSJohn Baldwin 		/*
5292498cf8cSJohn Baldwin 		 * If the current owner of the lock is executing on another
5302498cf8cSJohn Baldwin 		 * CPU, spin instead of blocking.
5312498cf8cSJohn Baldwin 		 */
5322498cf8cSJohn Baldwin 		owner = (struct thread *)(v & MTX_FLAGMASK);
533a9abdce4SRobert Watson #ifdef ADAPTIVE_GIANT
534a9abdce4SRobert Watson 		if (TD_IS_RUNNING(owner)) {
535a9abdce4SRobert Watson #else
53627dad03cSJohn Baldwin 		if (m != &Giant && TD_IS_RUNNING(owner)) {
537a9abdce4SRobert Watson #endif
538961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
53927dad03cSJohn Baldwin 			while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
5409f1b87f1SMaxime Henrion 				cpu_spinwait();
5417fcca609SJohn Baldwin 			}
5422498cf8cSJohn Baldwin 			continue;
5432498cf8cSJohn Baldwin 		}
544701f1408SScott Long #endif	/* SMP && !NO_ADAPTIVE_MUTEXES */
5452498cf8cSJohn Baldwin 
5469ed346baSBosko Milekic 		/*
5477feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
5489ed346baSBosko Milekic 		 */
54936412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
55036412d79SJohn Baldwin 
55102bd1bcdSIan Dowse #ifdef KTR
55202bd1bcdSIan Dowse 		if (!cont_logged) {
55302bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
55402bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
555122eceefSJohn Baldwin 			    (void *)tid, file, line, m->mtx_object.lo_name,
55602bd1bcdSIan Dowse 			    WITNESS_FILE(&m->mtx_object),
55702bd1bcdSIan Dowse 			    WITNESS_LINE(&m->mtx_object));
55802bd1bcdSIan Dowse 			cont_logged = 1;
55902bd1bcdSIan Dowse 		}
56002bd1bcdSIan Dowse #endif
56136412d79SJohn Baldwin 
5629ed346baSBosko Milekic 		/*
563961a7b24SJohn Baldwin 		 * Block on the turnstile.
5649ed346baSBosko Milekic 		 */
5652ff0e645SJohn Baldwin 		turnstile_wait(&m->mtx_object, mtx_owner(m));
56636412d79SJohn Baldwin 	}
5679ed346baSBosko Milekic 
56802bd1bcdSIan Dowse #ifdef KTR
56902bd1bcdSIan Dowse 	if (cont_logged) {
57002bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
57102bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
572122eceefSJohn Baldwin 		    m->mtx_object.lo_name, (void *)tid, file, line);
57302bd1bcdSIan Dowse 	}
57402bd1bcdSIan Dowse #endif
5758dc10be8SRobert Watson #ifdef MUTEX_PROFILING
5768dc10be8SRobert Watson 	if (contested)
5778dc10be8SRobert Watson 		m->mtx_contest_locking++;
5788dc10be8SRobert Watson 	m->mtx_contest_holding = 0;
5798dc10be8SRobert Watson #endif
58036412d79SJohn Baldwin 	return;
5819ed346baSBosko Milekic }
5829ed346baSBosko Milekic 
58333fb8a38SJohn Baldwin #ifdef SMP
5849ed346baSBosko Milekic /*
5859ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
5869ed346baSBosko Milekic  *
5879ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
5889ed346baSBosko Milekic  * is handled inline.
5899ed346baSBosko Milekic  */
5909ed346baSBosko Milekic void
591122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file,
592bdcfcf5bSJohn Baldwin     int line)
59336412d79SJohn Baldwin {
59436412d79SJohn Baldwin 	int i = 0;
59536412d79SJohn Baldwin 
59619284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
5975746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
5989ed346baSBosko Milekic 
59936412d79SJohn Baldwin 	for (;;) {
600122eceefSJohn Baldwin 		if (_obtain_lock(m, tid))
60136412d79SJohn Baldwin 			break;
6029ed346baSBosko Milekic 
6037141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
604c6a37e84SJohn Baldwin 		spinlock_exit();
60536412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
606703fc290SJohn Baldwin 			if (i++ < 10000000) {
6079f1b87f1SMaxime Henrion 				cpu_spinwait();
60836412d79SJohn Baldwin 				continue;
609703fc290SJohn Baldwin 			}
6100e54ddadSJohn Baldwin 			if (i < 60000000)
61136412d79SJohn Baldwin 				DELAY(1);
61283cece6fSJohn Baldwin 			else if (!kdb_active && !panicstr) {
61341109518SJohn Baldwin 				printf("spin lock %s held by %p for > 5 seconds\n",
61419284646SJohn Baldwin 				    m->mtx_object.lo_name, (void *)m->mtx_lock);
61541109518SJohn Baldwin #ifdef WITNESS
61641109518SJohn Baldwin 				witness_display_spinlock(&m->mtx_object,
61741109518SJohn Baldwin 				    mtx_owner(m));
61841109518SJohn Baldwin #endif
61941109518SJohn Baldwin 				panic("spin lock held too long");
62041109518SJohn Baldwin 			}
6219f1b87f1SMaxime Henrion 			cpu_spinwait();
62236412d79SJohn Baldwin 		}
623c6a37e84SJohn Baldwin 		spinlock_enter();
62436412d79SJohn Baldwin 	}
62536412d79SJohn Baldwin 
62619284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6279ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
6289ed346baSBosko Milekic 
62936412d79SJohn Baldwin 	return;
63036412d79SJohn Baldwin }
63133fb8a38SJohn Baldwin #endif /* SMP */
63236412d79SJohn Baldwin 
6339ed346baSBosko Milekic /*
6349ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
6359ed346baSBosko Milekic  *
6369ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
6379ed346baSBosko Milekic  * need to wake up a blocked thread).
6389ed346baSBosko Milekic  */
63936412d79SJohn Baldwin void
6409ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
64136412d79SJohn Baldwin {
642961a7b24SJohn Baldwin 	struct turnstile *ts;
6430c0b25aeSJohn Baldwin #ifndef PREEMPTION
644b40ce416SJulian Elischer 	struct thread *td, *td1;
6450c0b25aeSJohn Baldwin #endif
6469ed346baSBosko Milekic 
64708812b39SBosko Milekic 	if (mtx_recursed(m)) {
64836412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
64908812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
65019284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6519ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
65236412d79SJohn Baldwin 		return;
65336412d79SJohn Baldwin 	}
6549ed346baSBosko Milekic 
6552ff0e645SJohn Baldwin 	turnstile_lock(&m->mtx_object);
656961a7b24SJohn Baldwin 	ts = turnstile_lookup(&m->mtx_object);
65719284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6589ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
6599ed346baSBosko Milekic 
660ece2d989SPawel Jakub Dawidek #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
661961a7b24SJohn Baldwin 	if (ts == NULL) {
6622498cf8cSJohn Baldwin 		_release_lock_quick(m);
6632498cf8cSJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6642498cf8cSJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
665961a7b24SJohn Baldwin 		turnstile_release(&m->mtx_object);
6662498cf8cSJohn Baldwin 		return;
6672498cf8cSJohn Baldwin 	}
668961a7b24SJohn Baldwin #else
669961a7b24SJohn Baldwin 	MPASS(ts != NULL);
6702498cf8cSJohn Baldwin #endif
6710c0b25aeSJohn Baldwin #ifndef PREEMPTION
672961a7b24SJohn Baldwin 	/* XXX */
673961a7b24SJohn Baldwin 	td1 = turnstile_head(ts);
6740c0b25aeSJohn Baldwin #endif
675535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
676535eb309SJohn Baldwin 	turnstile_broadcast(ts);
677535eb309SJohn Baldwin 	_release_lock_quick(m);
678535eb309SJohn Baldwin #else
679961a7b24SJohn Baldwin 	if (turnstile_signal(ts)) {
68036412d79SJohn Baldwin 		_release_lock_quick(m);
68119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6829ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
683961a7b24SJohn Baldwin 	} else {
684f7ee1590SJohn Baldwin 		m->mtx_lock = MTX_CONTESTED;
68519284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
686961a7b24SJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
687961a7b24SJohn Baldwin 			    m);
688e0817317SJulian Elischer 	}
689535eb309SJohn Baldwin #endif
690961a7b24SJohn Baldwin 	turnstile_unpend(ts);
6919ed346baSBosko Milekic 
6920c0b25aeSJohn Baldwin #ifndef PREEMPTION
693961a7b24SJohn Baldwin 	/*
694961a7b24SJohn Baldwin 	 * XXX: This is just a hack until preemption is done.  However,
695961a7b24SJohn Baldwin 	 * once preemption is done we need to either wrap the
696961a7b24SJohn Baldwin 	 * turnstile_signal() and release of the actual lock in an
697961a7b24SJohn Baldwin 	 * extra critical section or change the preemption code to
698961a7b24SJohn Baldwin 	 * always just set a flag and never do instant-preempts.
699961a7b24SJohn Baldwin 	 */
700961a7b24SJohn Baldwin 	td = curthread;
701961a7b24SJohn Baldwin 	if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
702961a7b24SJohn Baldwin 		return;
703961a7b24SJohn Baldwin 	mtx_lock_spin(&sched_lock);
704961a7b24SJohn Baldwin 	if (!TD_IS_RUNNING(td1)) {
70536412d79SJohn Baldwin #ifdef notyet
706b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
707b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
70836412d79SJohn Baldwin 
70936412d79SJohn Baldwin 			if (it->it_interrupted) {
71019284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
71136412d79SJohn Baldwin 					CTR2(KTR_LOCK,
71215ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
71336412d79SJohn Baldwin 					    it, it->it_interrupted);
71436412d79SJohn Baldwin 				intr_thd_fixup(it);
71536412d79SJohn Baldwin 			}
71636412d79SJohn Baldwin 		}
71736412d79SJohn Baldwin #endif
71819284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
719562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
7209ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
7219ed346baSBosko Milekic 			    (void *)m->mtx_lock);
7229ed346baSBosko Milekic 
723bf0acc27SJohn Baldwin 		mi_switch(SW_INVOL, NULL);
72419284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7259ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
72631271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
72736412d79SJohn Baldwin 	}
7289ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
7290c0b25aeSJohn Baldwin #endif
7309ed346baSBosko Milekic 
7319ed346baSBosko Milekic 	return;
7329ed346baSBosko Milekic }
7339ed346baSBosko Milekic 
7349ed346baSBosko Milekic /*
7359ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
7369ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
7379ed346baSBosko Milekic  */
7389ed346baSBosko Milekic 
7399ed346baSBosko Milekic /*
74015ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
7419ed346baSBosko Milekic  */
7421103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
7430cde2e34SJason Evans void
74456771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
7450cde2e34SJason Evans {
7465cb0fbe4SJohn Baldwin 
7471126349aSPaul Saab 	if (panicstr != NULL || dumping)
7485cb0fbe4SJohn Baldwin 		return;
749a10f4966SJake Burkholder 	switch (what) {
7500cde2e34SJason Evans 	case MA_OWNED:
7510cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
7520cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
753a10f4966SJake Burkholder 		if (!mtx_owned(m))
7540cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
75519284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
756a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
757a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
7580cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
75919284646SJohn Baldwin 				    m->mtx_object.lo_name, file, line);
760a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
7610cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
76219284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7630cde2e34SJason Evans 		}
7640cde2e34SJason Evans 		break;
7650cde2e34SJason Evans 	case MA_NOTOWNED:
766a10f4966SJake Burkholder 		if (mtx_owned(m))
7670cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
76819284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7690cde2e34SJason Evans 		break;
7700cde2e34SJason Evans 	default:
77156771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
7720cde2e34SJason Evans 	}
7730cde2e34SJason Evans }
7740cde2e34SJason Evans #endif
7750cde2e34SJason Evans 
7769ed346baSBosko Milekic /*
7779ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
77819284646SJohn Baldwin  *
77919284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
78019284646SJohn Baldwin  * maintained by the witness code.
7819ed346baSBosko Milekic  */
78236412d79SJohn Baldwin #ifdef MUTEX_DEBUG
78336412d79SJohn Baldwin 
7844d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
78536412d79SJohn Baldwin 
78619284646SJohn Baldwin void
78719284646SJohn Baldwin mtx_validate(struct mtx *m)
78836412d79SJohn Baldwin {
78936412d79SJohn Baldwin 
79036412d79SJohn Baldwin /*
791fa669ab7SPoul-Henning Kamp  * XXX: When kernacc() does not require Giant we can reenable this check
792fa669ab7SPoul-Henning Kamp  */
793fa669ab7SPoul-Henning Kamp #ifdef notyet
794fa669ab7SPoul-Henning Kamp /*
79536412d79SJohn Baldwin  * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
79636412d79SJohn Baldwin  * we can re-enable the kernacc() checks.
79736412d79SJohn Baldwin  */
79836412d79SJohn Baldwin #ifndef __alpha__
79976dcbd6fSBosko Milekic 	/*
80076dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
80176dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
80276dcbd6fSBosko Milekic 	 * requires Giant itself.
80376dcbd6fSBosko Milekic 	 */
804ab07087eSBosko Milekic 	if (!cold)
805ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
806ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
80719284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
80836412d79SJohn Baldwin #endif
809fa669ab7SPoul-Henning Kamp #endif
81036412d79SJohn Baldwin }
81136412d79SJohn Baldwin #endif
81236412d79SJohn Baldwin 
8139ed346baSBosko Milekic /*
814c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
815c27b5699SAndrew R. Reiter  */
816c27b5699SAndrew R. Reiter void
817c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
818c27b5699SAndrew R. Reiter {
819c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
820c27b5699SAndrew R. Reiter 
8210c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
822c27b5699SAndrew R. Reiter }
823c27b5699SAndrew R. Reiter 
824c27b5699SAndrew R. Reiter /*
8259ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
8260c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
8270c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
8280c88508aSJohn Baldwin  * witness.
8299ed346baSBosko Milekic  */
83036412d79SJohn Baldwin void
8310c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
83236412d79SJohn Baldwin {
83319284646SJohn Baldwin 	struct lock_object *lock;
8349ed346baSBosko Milekic 
83519284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
83675d468eeSJohn Baldwin 	    MTX_NOWITNESS | MTX_DUPOK)) == 0);
8379ed346baSBosko Milekic 
83836412d79SJohn Baldwin #ifdef MUTEX_DEBUG
8399ed346baSBosko Milekic 	/* Diagnostic and error correction */
84019284646SJohn Baldwin 	mtx_validate(m);
8416936206eSJohn Baldwin #endif
84236412d79SJohn Baldwin 
84319284646SJohn Baldwin 	lock = &m->mtx_object;
8447ada5876SJohn Baldwin 	KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
845b82af320SPoul-Henning Kamp 	    ("mutex \"%s\" %p already initialized", name, m));
8467ada5876SJohn Baldwin 	bzero(m, sizeof(*m));
84719284646SJohn Baldwin 	if (opts & MTX_SPIN)
84819284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_spin;
84919284646SJohn Baldwin 	else
85019284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_sleep;
8510c88508aSJohn Baldwin 	lock->lo_name = name;
8520c88508aSJohn Baldwin 	lock->lo_type = type != NULL ? type : name;
85319284646SJohn Baldwin 	if (opts & MTX_QUIET)
85419284646SJohn Baldwin 		lock->lo_flags = LO_QUIET;
85519284646SJohn Baldwin 	if (opts & MTX_RECURSE)
85619284646SJohn Baldwin 		lock->lo_flags |= LO_RECURSABLE;
85719284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
85819284646SJohn Baldwin 		lock->lo_flags |= LO_WITNESS;
859f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
860f22a4b62SJeff Roberson 		lock->lo_flags |= LO_DUPOK;
86119284646SJohn Baldwin 
86219284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
8639ed346baSBosko Milekic 
86419284646SJohn Baldwin 	LOCK_LOG_INIT(lock, opts);
865d1c1b841SJason Evans 
86619284646SJohn Baldwin 	WITNESS_INIT(lock);
86736412d79SJohn Baldwin }
86836412d79SJohn Baldwin 
8699ed346baSBosko Milekic /*
87019284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
87119284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
87219284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
87319284646SJohn Baldwin  * flags.
8749ed346baSBosko Milekic  */
87536412d79SJohn Baldwin void
87636412d79SJohn Baldwin mtx_destroy(struct mtx *m)
87736412d79SJohn Baldwin {
87836412d79SJohn Baldwin 
87919284646SJohn Baldwin 	LOCK_LOG_DESTROY(&m->mtx_object, 0);
8809ed346baSBosko Milekic 
88119284646SJohn Baldwin 	if (!mtx_owned(m))
88219284646SJohn Baldwin 		MPASS(mtx_unowned(m));
88319284646SJohn Baldwin 	else {
88408812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
8859ed346baSBosko Milekic 
88619284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
887c86b6ff5SJohn Baldwin 		WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
888c86b6ff5SJohn Baldwin 		    __LINE__);
88936412d79SJohn Baldwin 	}
8900384fff8SJason Evans 
89119284646SJohn Baldwin 	WITNESS_DESTROY(&m->mtx_object);
8920384fff8SJason Evans }
893d23f5958SMatthew Dillon 
894d23f5958SMatthew Dillon /*
895c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
896c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
897c53c013bSJohn Baldwin  * setup before this is called.
898c53c013bSJohn Baldwin  */
899c53c013bSJohn Baldwin void
900c53c013bSJohn Baldwin mutex_init(void)
901c53c013bSJohn Baldwin {
902c53c013bSJohn Baldwin 
903c53c013bSJohn Baldwin 	/* Setup thread0 so that mutexes work. */
904c53c013bSJohn Baldwin 	LIST_INIT(&thread0.td_contested);
905c53c013bSJohn Baldwin 
906961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
907961a7b24SJohn Baldwin 	init_turnstiles();
908961a7b24SJohn Baldwin 
909c53c013bSJohn Baldwin 	/*
910c53c013bSJohn Baldwin 	 * Initialize mutexes.
911c53c013bSJohn Baldwin 	 */
9120c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
9130c88508aSJohn Baldwin 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
9140c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
9158c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
916c53c013bSJohn Baldwin 	mtx_lock(&Giant);
917c53c013bSJohn Baldwin }
918d272fe53SJohn Baldwin 
919d272fe53SJohn Baldwin #ifdef DDB
920d272fe53SJohn Baldwin /* XXX: This function is not mutex-specific. */
921d272fe53SJohn Baldwin DB_SHOW_COMMAND(lock, db_show_lock)
922d272fe53SJohn Baldwin {
923d272fe53SJohn Baldwin 	struct lock_object *lock;
924d272fe53SJohn Baldwin 
925d272fe53SJohn Baldwin 	if (!have_addr)
926d272fe53SJohn Baldwin 		return;
927d272fe53SJohn Baldwin 	lock = (struct lock_object *)addr;
928d272fe53SJohn Baldwin 	if (lock->lo_class != &lock_class_mtx_sleep &&
929d272fe53SJohn Baldwin 	    lock->lo_class != &lock_class_mtx_spin &&
930d272fe53SJohn Baldwin 	    lock->lo_class != &lock_class_sx) {
931d272fe53SJohn Baldwin 		db_printf("Unknown lock class\n");
932d272fe53SJohn Baldwin 		return;
933d272fe53SJohn Baldwin 	}
934d272fe53SJohn Baldwin 	db_printf(" class: %s\n", lock->lo_class->lc_name);
935d272fe53SJohn Baldwin 	db_printf(" name: %s\n", lock->lo_name);
936d272fe53SJohn Baldwin 	if (lock->lo_type && lock->lo_type != lock->lo_name)
937d272fe53SJohn Baldwin 		db_printf(" type: %s\n", lock->lo_type);
938d272fe53SJohn Baldwin 	lock->lo_class->lc_ddb_show(lock);
939d272fe53SJohn Baldwin }
940d272fe53SJohn Baldwin 
941d272fe53SJohn Baldwin void
942d272fe53SJohn Baldwin db_show_mtx(struct lock_object *lock)
943d272fe53SJohn Baldwin {
944d272fe53SJohn Baldwin 	struct thread *td;
945d272fe53SJohn Baldwin 	struct mtx *m;
946d272fe53SJohn Baldwin 
947d272fe53SJohn Baldwin 	m = (struct mtx *)lock;
948d272fe53SJohn Baldwin 
949d272fe53SJohn Baldwin 	db_printf(" flags: {");
950d272fe53SJohn Baldwin 	if (m->mtx_object.lo_class == &lock_class_mtx_spin)
951d272fe53SJohn Baldwin 		db_printf("SPIN");
952d272fe53SJohn Baldwin 	else
953d272fe53SJohn Baldwin 		db_printf("DEF");
954d272fe53SJohn Baldwin 	if (m->mtx_object.lo_flags & LO_RECURSABLE)
955d272fe53SJohn Baldwin 		db_printf(", RECURSE");
956d272fe53SJohn Baldwin 	if (m->mtx_object.lo_flags & LO_DUPOK)
957d272fe53SJohn Baldwin 		db_printf(", DUPOK");
958d272fe53SJohn Baldwin 	db_printf("}\n");
959d272fe53SJohn Baldwin 	db_printf(" state: {");
960d272fe53SJohn Baldwin 	if (mtx_unowned(m))
961d272fe53SJohn Baldwin 		db_printf("UNOWNED");
962d272fe53SJohn Baldwin 	else {
963d272fe53SJohn Baldwin 		db_printf("OWNED");
964d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
965d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
966d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
967d272fe53SJohn Baldwin 			db_printf(", RECURSED");
968d272fe53SJohn Baldwin 	}
969d272fe53SJohn Baldwin 	db_printf("}\n");
970d272fe53SJohn Baldwin 	if (!mtx_unowned(m)) {
971d272fe53SJohn Baldwin 		td = mtx_owner(m);
972d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
973d272fe53SJohn Baldwin 		    td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
974d272fe53SJohn Baldwin 		if (mtx_recursed(m))
975d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
976d272fe53SJohn Baldwin 	}
977d272fe53SJohn Baldwin }
978d272fe53SJohn Baldwin #endif
979