xref: /freebsd/sys/kern/kern_mutex.c (revision 764e4d54e9d5ea383b7ef04841155e4fc68f9082)
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>
650384fff8SJason Evans #include <machine/cpu.h>
6636412d79SJohn Baldwin 
679c36c934SJohn Baldwin #include <ddb/ddb.h>
689c36c934SJohn Baldwin 
698c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h>
708c4b6380SJohn Baldwin 
7136412d79SJohn Baldwin #include <vm/vm.h>
7236412d79SJohn Baldwin #include <vm/vm_extern.h>
7336412d79SJohn Baldwin 
740cde2e34SJason Evans /*
75b9a80acaSStephan Uphoff  * Force MUTEX_WAKE_ALL for now.
76b9a80acaSStephan Uphoff  * single thread wakeup needs fixes to avoid race conditions with
77b9a80acaSStephan Uphoff  * priority inheritance.
78b9a80acaSStephan Uphoff  */
79b9a80acaSStephan Uphoff #ifndef MUTEX_WAKE_ALL
80b9a80acaSStephan Uphoff #define MUTEX_WAKE_ALL
81b9a80acaSStephan Uphoff #endif
82b9a80acaSStephan Uphoff 
83b9a80acaSStephan Uphoff /*
849ed346baSBosko Milekic  * Internal utility macros.
850cde2e34SJason Evans  */
869ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
870cde2e34SJason Evans 
8849b94bfcSJohn Baldwin #define	mtx_owner(m)	((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
899ed346baSBosko Milekic 
90d272fe53SJohn Baldwin #ifdef DDB
91d272fe53SJohn Baldwin static void	db_show_mtx(struct lock_object *lock);
92d272fe53SJohn Baldwin #endif
93d272fe53SJohn Baldwin 
940cde2e34SJason Evans /*
9519284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
960cde2e34SJason Evans  */
9719284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
9819284646SJohn Baldwin 	"sleep mutex",
99d272fe53SJohn Baldwin 	LC_SLEEPLOCK | LC_RECURSABLE,
100d272fe53SJohn Baldwin #ifdef DDB
101d272fe53SJohn Baldwin 	db_show_mtx
102d272fe53SJohn Baldwin #endif
10319284646SJohn Baldwin };
10419284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
10519284646SJohn Baldwin 	"spin mutex",
106d272fe53SJohn Baldwin 	LC_SPINLOCK | LC_RECURSABLE,
107d272fe53SJohn Baldwin #ifdef DDB
108d272fe53SJohn Baldwin 	db_show_mtx
109d272fe53SJohn Baldwin #endif
1108484de75SJohn Baldwin };
1118484de75SJohn Baldwin 
1129ed346baSBosko Milekic /*
113c53c013bSJohn Baldwin  * System-wide mutexes
114c53c013bSJohn Baldwin  */
115c53c013bSJohn Baldwin struct mtx sched_lock;
116c53c013bSJohn Baldwin struct mtx Giant;
117c53c013bSJohn Baldwin 
1186c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
1196c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging");
1206c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling");
1216c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0;
1226c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW,
1236c35e809SDag-Erling Smørgrav     &mutex_prof_enable, 0, "Enable tracing of mutex holdtime");
1246c35e809SDag-Erling Smørgrav 
1256c35e809SDag-Erling Smørgrav struct mutex_prof {
1266c35e809SDag-Erling Smørgrav 	const char	*name;
1276c35e809SDag-Erling Smørgrav 	const char	*file;
1286c35e809SDag-Erling Smørgrav 	int		line;
129ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_max;
130ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_tot;
131ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_cur;
1328dc10be8SRobert Watson 	uintmax_t	cnt_contest_holding;
1338dc10be8SRobert Watson 	uintmax_t	cnt_contest_locking;
134e6330704SDag-Erling Smørgrav 	struct mutex_prof *next;
1356c35e809SDag-Erling Smørgrav };
1366c35e809SDag-Erling Smørgrav 
1376c35e809SDag-Erling Smørgrav /*
1386c35e809SDag-Erling Smørgrav  * mprof_buf is a static pool of profiling records to avoid possible
1396c35e809SDag-Erling Smørgrav  * reentrance of the memory allocation functions.
1406c35e809SDag-Erling Smørgrav  *
1416c35e809SDag-Erling Smørgrav  * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE.
1426c35e809SDag-Erling Smørgrav  */
14300096801SJohn-Mark Gurney #ifdef MPROF_BUFFERS
14400096801SJohn-Mark Gurney #define NUM_MPROF_BUFFERS	MPROF_BUFFERS
14500096801SJohn-Mark Gurney #else
146e6330704SDag-Erling Smørgrav #define	NUM_MPROF_BUFFERS	1000
14700096801SJohn-Mark Gurney #endif
1486c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS];
1496c35e809SDag-Erling Smørgrav static int first_free_mprof_buf;
15000096801SJohn-Mark Gurney #ifndef MPROF_HASH_SIZE
151e6330704SDag-Erling Smørgrav #define	MPROF_HASH_SIZE		1009
15200096801SJohn-Mark Gurney #endif
15300096801SJohn-Mark Gurney #if NUM_MPROF_BUFFERS >= MPROF_HASH_SIZE
15400096801SJohn-Mark Gurney #error MPROF_BUFFERS must be larger than MPROF_HASH_SIZE
15500096801SJohn-Mark Gurney #endif
1566c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE];
1570bd5f797SMike Makonnen /* SWAG: sbuf size = avg stat. line size * number of locks */
1580bd5f797SMike Makonnen #define MPROF_SBUF_SIZE		256 * 400
1596c35e809SDag-Erling Smørgrav 
1606c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions;
1616c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD,
1626c35e809SDag-Erling Smørgrav     &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded");
1636c35e809SDag-Erling Smørgrav static int mutex_prof_records;
1646c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD,
1656c35e809SDag-Erling Smørgrav     &mutex_prof_records, 0, "Number of profiling records");
1666c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS;
1676c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD,
1686c35e809SDag-Erling Smørgrav     &mutex_prof_maxrecords, 0, "Maximum number of profiling records");
1696c35e809SDag-Erling Smørgrav static int mutex_prof_rejected;
1706c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD,
1716c35e809SDag-Erling Smørgrav     &mutex_prof_rejected, 0, "Number of rejected profiling records");
1726c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE;
1736c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD,
1746c35e809SDag-Erling Smørgrav     &mutex_prof_hashsize, 0, "Hash size");
1756c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0;
1766c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD,
1776c35e809SDag-Erling Smørgrav     &mutex_prof_collisions, 0, "Number of hash collisions");
1786c35e809SDag-Erling Smørgrav 
1796c35e809SDag-Erling Smørgrav /*
1806c35e809SDag-Erling Smørgrav  * mprof_mtx protects the profiling buffers and the hash.
1816c35e809SDag-Erling Smørgrav  */
1826c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx;
183e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET);
1846c35e809SDag-Erling Smørgrav 
185b784ffe9SDag-Erling Smørgrav static u_int64_t
186b784ffe9SDag-Erling Smørgrav nanoseconds(void)
187b784ffe9SDag-Erling Smørgrav {
188b784ffe9SDag-Erling Smørgrav 	struct timespec tv;
189b784ffe9SDag-Erling Smørgrav 
190b784ffe9SDag-Erling Smørgrav 	nanotime(&tv);
191b784ffe9SDag-Erling Smørgrav 	return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec);
192b784ffe9SDag-Erling Smørgrav }
193b784ffe9SDag-Erling Smørgrav 
1946c35e809SDag-Erling Smørgrav static int
1956c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
1966c35e809SDag-Erling Smørgrav {
1976c35e809SDag-Erling Smørgrav 	struct sbuf *sb;
1986c35e809SDag-Erling Smørgrav 	int error, i;
1990bd5f797SMike Makonnen 	static int multiplier = 1;
2006c35e809SDag-Erling Smørgrav 
2016c35e809SDag-Erling Smørgrav 	if (first_free_mprof_buf == 0)
2026d036900SDag-Erling Smørgrav 		return (SYSCTL_OUT(req, "No locking recorded",
2036d036900SDag-Erling Smørgrav 		    sizeof("No locking recorded")));
2046c35e809SDag-Erling Smørgrav 
2050bd5f797SMike Makonnen retry_sbufops:
2060bd5f797SMike Makonnen 	sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN);
2074f201858SGleb Smirnoff 	sbuf_printf(sb, "\n%6s %12s %11s %5s %12s %12s %s\n",
2088dc10be8SRobert Watson 	    "max", "total", "count", "avg", "cnt_hold", "cnt_lock", "name");
2096d036900SDag-Erling Smørgrav 	/*
2106d036900SDag-Erling Smørgrav 	 * XXX this spinlock seems to be by far the largest perpetrator
2116d036900SDag-Erling Smørgrav 	 * of spinlock latency (1.6 msec on an Athlon1600 was recorded
2126d036900SDag-Erling Smørgrav 	 * even before I pessimized it further by moving the average
2136d036900SDag-Erling Smørgrav 	 * computation here).
2146d036900SDag-Erling Smørgrav 	 */
2156c35e809SDag-Erling Smørgrav 	mtx_lock_spin(&mprof_mtx);
2160bd5f797SMike Makonnen 	for (i = 0; i < first_free_mprof_buf; ++i) {
2178dc10be8SRobert Watson 		sbuf_printf(sb, "%6ju %12ju %11ju %5ju %12ju %12ju %s:%d (%s)\n",
218ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_max / 1000,
219ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_tot / 1000,
220ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_cur,
221ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 :
222ecf031c9SDag-Erling Smørgrav 			mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000),
2238dc10be8SRobert Watson 		    mprof_buf[i].cnt_contest_holding,
2248dc10be8SRobert Watson 		    mprof_buf[i].cnt_contest_locking,
2256c35e809SDag-Erling Smørgrav 		    mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name);
2260bd5f797SMike Makonnen 		if (sbuf_overflowed(sb)) {
2270bd5f797SMike Makonnen 			mtx_unlock_spin(&mprof_mtx);
2280bd5f797SMike Makonnen 			sbuf_delete(sb);
2290bd5f797SMike Makonnen 			multiplier++;
2300bd5f797SMike Makonnen 			goto retry_sbufops;
2310bd5f797SMike Makonnen 		}
2320bd5f797SMike Makonnen 	}
2336c35e809SDag-Erling Smørgrav 	mtx_unlock_spin(&mprof_mtx);
2346c35e809SDag-Erling Smørgrav 	sbuf_finish(sb);
2356c35e809SDag-Erling Smørgrav 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2366c35e809SDag-Erling Smørgrav 	sbuf_delete(sb);
2376c35e809SDag-Erling Smørgrav 	return (error);
2386c35e809SDag-Erling Smørgrav }
2396c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD,
2406c35e809SDag-Erling Smørgrav     NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics");
24194ffb20dSRobert Watson 
24294ffb20dSRobert Watson static int
24394ffb20dSRobert Watson reset_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
24494ffb20dSRobert Watson {
24594ffb20dSRobert Watson 	int error, v;
24694ffb20dSRobert Watson 
24794ffb20dSRobert Watson 	if (first_free_mprof_buf == 0)
24894ffb20dSRobert Watson 		return (0);
24994ffb20dSRobert Watson 
25094ffb20dSRobert Watson 	v = 0;
25194ffb20dSRobert Watson 	error = sysctl_handle_int(oidp, &v, 0, req);
25294ffb20dSRobert Watson 	if (error)
25394ffb20dSRobert Watson 		return (error);
25494ffb20dSRobert Watson 	if (req->newptr == NULL)
25594ffb20dSRobert Watson 		return (error);
25694ffb20dSRobert Watson 	if (v == 0)
25794ffb20dSRobert Watson 		return (0);
25894ffb20dSRobert Watson 
25994ffb20dSRobert Watson 	mtx_lock_spin(&mprof_mtx);
26094ffb20dSRobert Watson 	bzero(mprof_buf, sizeof(*mprof_buf) * first_free_mprof_buf);
26194ffb20dSRobert Watson 	bzero(mprof_hash, sizeof(struct mtx *) * MPROF_HASH_SIZE);
26294ffb20dSRobert Watson 	first_free_mprof_buf = 0;
26394ffb20dSRobert Watson 	mtx_unlock_spin(&mprof_mtx);
26494ffb20dSRobert Watson 	return (0);
26594ffb20dSRobert Watson }
26694ffb20dSRobert Watson SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW,
26794ffb20dSRobert Watson     NULL, 0, reset_mutex_prof_stats, "I", "Reset mutex profiling statistics");
2686c35e809SDag-Erling Smørgrav #endif
2696c35e809SDag-Erling Smørgrav 
2700cde2e34SJason Evans /*
2716283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2726283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2736283b7d0SJohn Baldwin  */
2746283b7d0SJohn Baldwin void
2756283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
2766283b7d0SJohn Baldwin {
2776283b7d0SJohn Baldwin 
278dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
279186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
280186abbd7SJohn Baldwin 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
28183a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &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);
290764e4d54SJohn Baldwin 	curthread->td_locks++;
2916c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
2926c35e809SDag-Erling Smørgrav 	/* don't reset the timer when/if recursing */
293b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime == 0) {
294b61860adSDag-Erling Smørgrav 		m->mtx_filename = file;
295b61860adSDag-Erling Smørgrav 		m->mtx_lineno = line;
296b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0;
2976c35e809SDag-Erling Smørgrav 		++mutex_prof_acquisitions;
2986c35e809SDag-Erling Smørgrav 	}
2996c35e809SDag-Erling Smørgrav #endif
3006283b7d0SJohn Baldwin }
3016283b7d0SJohn Baldwin 
3026283b7d0SJohn Baldwin void
3036283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
3046283b7d0SJohn Baldwin {
3056283b7d0SJohn Baldwin 
306dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
307186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
308186abbd7SJohn Baldwin 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
30983a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
3100d975d63SJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
3110d975d63SJohn Baldwin 	    file, line));
312764e4d54SJohn Baldwin 	curthread->td_locks--;
3130d975d63SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3140d975d63SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
3150d975d63SJohn Baldwin 	    line);
31621377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
3176c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
318b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime != 0) {
3196c35e809SDag-Erling Smørgrav 		static const char *unknown = "(unknown)";
3206c35e809SDag-Erling Smørgrav 		struct mutex_prof *mpp;
321b784ffe9SDag-Erling Smørgrav 		u_int64_t acqtime, now;
3226c35e809SDag-Erling Smørgrav 		const char *p, *q;
323e6330704SDag-Erling Smørgrav 		volatile u_int hash;
3246c35e809SDag-Erling Smørgrav 
325b784ffe9SDag-Erling Smørgrav 		now = nanoseconds();
326b61860adSDag-Erling Smørgrav 		acqtime = m->mtx_acqtime;
327b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = 0;
328b784ffe9SDag-Erling Smørgrav 		if (now <= acqtime)
3296c35e809SDag-Erling Smørgrav 			goto out;
3300bd5f797SMike Makonnen 		for (p = m->mtx_filename;
3310bd5f797SMike Makonnen 		    p != NULL && strncmp(p, "../", 3) == 0; p += 3)
3326c35e809SDag-Erling Smørgrav 			/* nothing */ ;
3336c35e809SDag-Erling Smørgrav 		if (p == NULL || *p == '\0')
3346c35e809SDag-Erling Smørgrav 			p = unknown;
335b61860adSDag-Erling Smørgrav 		for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q)
3366c35e809SDag-Erling Smørgrav 			hash = (hash * 2 + *q) % MPROF_HASH_SIZE;
3376c35e809SDag-Erling Smørgrav 		mtx_lock_spin(&mprof_mtx);
338e6330704SDag-Erling Smørgrav 		for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next)
339b61860adSDag-Erling Smørgrav 			if (mpp->line == m->mtx_lineno &&
340b61860adSDag-Erling Smørgrav 			    strcmp(mpp->file, p) == 0)
3416c35e809SDag-Erling Smørgrav 				break;
3426c35e809SDag-Erling Smørgrav 		if (mpp == NULL) {
3436c35e809SDag-Erling Smørgrav 			/* Just exit if we cannot get a trace buffer */
3446c35e809SDag-Erling Smørgrav 			if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) {
3456c35e809SDag-Erling Smørgrav 				++mutex_prof_rejected;
3466c35e809SDag-Erling Smørgrav 				goto unlock;
3476c35e809SDag-Erling Smørgrav 			}
3486c35e809SDag-Erling Smørgrav 			mpp = &mprof_buf[first_free_mprof_buf++];
3496c35e809SDag-Erling Smørgrav 			mpp->name = mtx_name(m);
3506c35e809SDag-Erling Smørgrav 			mpp->file = p;
351b61860adSDag-Erling Smørgrav 			mpp->line = m->mtx_lineno;
352e6330704SDag-Erling Smørgrav 			mpp->next = mprof_hash[hash];
353e6330704SDag-Erling Smørgrav 			if (mprof_hash[hash] != NULL)
354e6330704SDag-Erling Smørgrav 				++mutex_prof_collisions;
3556c35e809SDag-Erling Smørgrav 			mprof_hash[hash] = mpp;
356e6330704SDag-Erling Smørgrav 			++mutex_prof_records;
3576c35e809SDag-Erling Smørgrav 		}
3586c35e809SDag-Erling Smørgrav 		/*
3596c35e809SDag-Erling Smørgrav 		 * Record if the mutex has been held longer now than ever
3606d036900SDag-Erling Smørgrav 		 * before.
3616c35e809SDag-Erling Smørgrav 		 */
362ecf031c9SDag-Erling Smørgrav 		if (now - acqtime > mpp->cnt_max)
363ecf031c9SDag-Erling Smørgrav 			mpp->cnt_max = now - acqtime;
364ecf031c9SDag-Erling Smørgrav 		mpp->cnt_tot += now - acqtime;
365ecf031c9SDag-Erling Smørgrav 		mpp->cnt_cur++;
3668dc10be8SRobert Watson 		/*
3678dc10be8SRobert Watson 		 * There's a small race, really we should cmpxchg
3688dc10be8SRobert Watson 		 * 0 with the current value, but that would bill
3698dc10be8SRobert Watson 		 * the contention to the wrong lock instance if
3708dc10be8SRobert Watson 		 * it followed this also.
3718dc10be8SRobert Watson 		 */
3728dc10be8SRobert Watson 		mpp->cnt_contest_holding += m->mtx_contest_holding;
3738dc10be8SRobert Watson 		m->mtx_contest_holding = 0;
3748dc10be8SRobert Watson 		mpp->cnt_contest_locking += m->mtx_contest_locking;
3758dc10be8SRobert Watson 		m->mtx_contest_locking = 0;
3766c35e809SDag-Erling Smørgrav unlock:
3776c35e809SDag-Erling Smørgrav 		mtx_unlock_spin(&mprof_mtx);
3786c35e809SDag-Erling Smørgrav 	}
3796c35e809SDag-Erling Smørgrav out:
3806c35e809SDag-Erling Smørgrav #endif
381dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
3826283b7d0SJohn Baldwin }
3836283b7d0SJohn Baldwin 
3846283b7d0SJohn Baldwin void
3856283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3866283b7d0SJohn Baldwin {
3876283b7d0SJohn Baldwin 
388dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
389186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
390186abbd7SJohn Baldwin 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
39183a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin,
3920d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
3930d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
3948d768e76SJohn Baldwin 	WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
3958d768e76SJohn Baldwin 	    file, line);
396dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
397dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
398dde96c99SJohn Baldwin 	    line);
399dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
4006283b7d0SJohn Baldwin }
4016283b7d0SJohn Baldwin 
4026283b7d0SJohn Baldwin void
4036283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
4046283b7d0SJohn Baldwin {
4056283b7d0SJohn Baldwin 
406dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
407186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
408186abbd7SJohn Baldwin 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
40983a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin,
4100d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
4110d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
412dde96c99SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
413dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
414dde96c99SJohn Baldwin 	    line);
4150d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
416dde96c99SJohn Baldwin 	_rel_spin_lock(m);
4176283b7d0SJohn Baldwin }
4186283b7d0SJohn Baldwin 
4196283b7d0SJohn Baldwin /*
4209ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
421eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
422eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
4230cde2e34SJason Evans  */
4240cde2e34SJason Evans int
4259ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
4260cde2e34SJason Evans {
4270cde2e34SJason Evans 	int rval;
4280cde2e34SJason Evans 
429b40ce416SJulian Elischer 	MPASS(curthread != NULL);
430186abbd7SJohn Baldwin 	KASSERT(m->mtx_lock != MTX_DESTROYED,
431186abbd7SJohn Baldwin 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
43283a81bcbSJohn Baldwin 	KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
43383cece6fSJohn Baldwin 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
43483cece6fSJohn Baldwin 	    file, line));
4359ed346baSBosko Milekic 
436eac09796SJohn Baldwin 	if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) {
437eac09796SJohn Baldwin 		m->mtx_recurse++;
438eac09796SJohn Baldwin 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
439eac09796SJohn Baldwin 		rval = 1;
440eac09796SJohn Baldwin 	} else
441122eceefSJohn Baldwin 		rval = _obtain_lock(m, (uintptr_t)curthread);
4429ed346baSBosko Milekic 
44319284646SJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
444764e4d54SJohn Baldwin 	if (rval) {
4452d96f0b1SJohn Baldwin 		WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4462d96f0b1SJohn Baldwin 		    file, line);
447764e4d54SJohn Baldwin 		curthread->td_locks++;
448764e4d54SJohn Baldwin 	}
4499ed346baSBosko Milekic 
45019284646SJohn Baldwin 	return (rval);
4510cde2e34SJason Evans }
4520cde2e34SJason Evans 
4530cde2e34SJason Evans /*
4549ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4559ed346baSBosko Milekic  *
4569ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4579ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4580cde2e34SJason Evans  */
4590cde2e34SJason Evans void
460122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
461bdcfcf5bSJohn Baldwin     int line)
46236412d79SJohn Baldwin {
463701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
46476447e56SJohn Baldwin 	volatile struct thread *owner;
4652498cf8cSJohn Baldwin #endif
4665fa8dd90SJohn Baldwin 	uintptr_t v;
46702bd1bcdSIan Dowse #ifdef KTR
46802bd1bcdSIan Dowse 	int cont_logged = 0;
46902bd1bcdSIan Dowse #endif
4708dc10be8SRobert Watson #ifdef MUTEX_PROFILING
4718dc10be8SRobert Watson 	int contested;
4728dc10be8SRobert Watson #endif
47336412d79SJohn Baldwin 
4745fa8dd90SJohn Baldwin 	if (mtx_owned(m)) {
475eac09796SJohn Baldwin 		KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0,
476eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
477eac09796SJohn Baldwin 		    m->mtx_object.lo_name, file, line));
47836412d79SJohn Baldwin 		m->mtx_recurse++;
47908812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
48019284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4815746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
48236412d79SJohn Baldwin 		return;
48336412d79SJohn Baldwin 	}
4849ed346baSBosko Milekic 
48519284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
48615ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
48715ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
48819284646SJohn Baldwin 		    m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
4891bd0eefbSJohn Baldwin 
4908dc10be8SRobert Watson #ifdef MUTEX_PROFILING
4918dc10be8SRobert Watson 	contested = 0;
4928dc10be8SRobert Watson #endif
493122eceefSJohn Baldwin 	while (!_obtain_lock(m, tid)) {
4948dc10be8SRobert Watson #ifdef MUTEX_PROFILING
4958dc10be8SRobert Watson 		contested = 1;
4968dc10be8SRobert Watson 		atomic_add_int(&m->mtx_contest_holding, 1);
4978dc10be8SRobert Watson #endif
4982ff0e645SJohn Baldwin 		turnstile_lock(&m->mtx_object);
4995fa8dd90SJohn Baldwin 		v = m->mtx_lock;
5005fa8dd90SJohn Baldwin 
50136412d79SJohn Baldwin 		/*
5029ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
503961a7b24SJohn Baldwin 		 * the turnstile chain lock.
50436412d79SJohn Baldwin 		 */
5055fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
506961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
5079f1b87f1SMaxime Henrion 			cpu_spinwait();
50836412d79SJohn Baldwin 			continue;
50936412d79SJohn Baldwin 		}
5109ed346baSBosko Milekic 
511535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
512535eb309SJohn Baldwin 		MPASS(v != MTX_CONTESTED);
513535eb309SJohn Baldwin #else
51436412d79SJohn Baldwin 		/*
5159ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
516f7ee1590SJohn Baldwin 		 * there are other threads blocked on it.  Grab ownership of
517f7ee1590SJohn Baldwin 		 * it and propagate its priority to the current thread if
518f7ee1590SJohn Baldwin 		 * necessary.
51936412d79SJohn Baldwin 		 */
52036412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
521122eceefSJohn Baldwin 			m->mtx_lock = tid | MTX_CONTESTED;
5222ff0e645SJohn Baldwin 			turnstile_claim(&m->mtx_object);
5238dc10be8SRobert Watson 			break;
52436412d79SJohn Baldwin 		}
525535eb309SJohn Baldwin #endif
5269ed346baSBosko Milekic 
52736412d79SJohn Baldwin 		/*
5289ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
5299ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
5309ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
53136412d79SJohn Baldwin 		 */
53236412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
533122eceefSJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
534961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
5359f1b87f1SMaxime Henrion 			cpu_spinwait();
53636412d79SJohn Baldwin 			continue;
53736412d79SJohn Baldwin 		}
53836412d79SJohn Baldwin 
539701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
5402498cf8cSJohn Baldwin 		/*
5412498cf8cSJohn Baldwin 		 * If the current owner of the lock is executing on another
5422498cf8cSJohn Baldwin 		 * CPU, spin instead of blocking.
5432498cf8cSJohn Baldwin 		 */
54449b94bfcSJohn Baldwin 		owner = (struct thread *)(v & ~MTX_FLAGMASK);
545a9abdce4SRobert Watson #ifdef ADAPTIVE_GIANT
546a9abdce4SRobert Watson 		if (TD_IS_RUNNING(owner)) {
547a9abdce4SRobert Watson #else
54827dad03cSJohn Baldwin 		if (m != &Giant && TD_IS_RUNNING(owner)) {
549a9abdce4SRobert Watson #endif
550961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
55127dad03cSJohn Baldwin 			while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
5529f1b87f1SMaxime Henrion 				cpu_spinwait();
5537fcca609SJohn Baldwin 			}
5542498cf8cSJohn Baldwin 			continue;
5552498cf8cSJohn Baldwin 		}
556701f1408SScott Long #endif	/* SMP && !NO_ADAPTIVE_MUTEXES */
5572498cf8cSJohn Baldwin 
5589ed346baSBosko Milekic 		/*
5597feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
5609ed346baSBosko Milekic 		 */
56136412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
56236412d79SJohn Baldwin 
56302bd1bcdSIan Dowse #ifdef KTR
56402bd1bcdSIan Dowse 		if (!cont_logged) {
56502bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
56602bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
567122eceefSJohn Baldwin 			    (void *)tid, file, line, m->mtx_object.lo_name,
56802bd1bcdSIan Dowse 			    WITNESS_FILE(&m->mtx_object),
56902bd1bcdSIan Dowse 			    WITNESS_LINE(&m->mtx_object));
57002bd1bcdSIan Dowse 			cont_logged = 1;
57102bd1bcdSIan Dowse 		}
57202bd1bcdSIan Dowse #endif
57336412d79SJohn Baldwin 
5749ed346baSBosko Milekic 		/*
575961a7b24SJohn Baldwin 		 * Block on the turnstile.
5769ed346baSBosko Milekic 		 */
5777aa4f685SJohn Baldwin 		turnstile_wait(&m->mtx_object, mtx_owner(m),
5787aa4f685SJohn Baldwin 		    TS_EXCLUSIVE_QUEUE);
57936412d79SJohn Baldwin 	}
5809ed346baSBosko Milekic 
58102bd1bcdSIan Dowse #ifdef KTR
58202bd1bcdSIan Dowse 	if (cont_logged) {
58302bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
58402bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
585122eceefSJohn Baldwin 		    m->mtx_object.lo_name, (void *)tid, file, line);
58602bd1bcdSIan Dowse 	}
58702bd1bcdSIan Dowse #endif
5888dc10be8SRobert Watson #ifdef MUTEX_PROFILING
5898dc10be8SRobert Watson 	if (contested)
5908dc10be8SRobert Watson 		m->mtx_contest_locking++;
5918dc10be8SRobert Watson 	m->mtx_contest_holding = 0;
5928dc10be8SRobert Watson #endif
59336412d79SJohn Baldwin 	return;
5949ed346baSBosko Milekic }
5959ed346baSBosko Milekic 
59633fb8a38SJohn Baldwin #ifdef SMP
5979ed346baSBosko Milekic /*
5989ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
5999ed346baSBosko Milekic  *
6009ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
6019ed346baSBosko Milekic  * is handled inline.
6029ed346baSBosko Milekic  */
6039ed346baSBosko Milekic void
604122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file,
605bdcfcf5bSJohn Baldwin     int line)
60636412d79SJohn Baldwin {
60736412d79SJohn Baldwin 	int i = 0;
60836412d79SJohn Baldwin 
60919284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6105746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
6119ed346baSBosko Milekic 
612f781b5a4SJohn Baldwin 	while (!_obtain_lock(m, tid)) {
6139ed346baSBosko Milekic 
6147141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
615c6a37e84SJohn Baldwin 		spinlock_exit();
61636412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
617703fc290SJohn Baldwin 			if (i++ < 10000000) {
6189f1b87f1SMaxime Henrion 				cpu_spinwait();
61936412d79SJohn Baldwin 				continue;
620703fc290SJohn Baldwin 			}
6210e54ddadSJohn Baldwin 			if (i < 60000000)
62236412d79SJohn Baldwin 				DELAY(1);
62383cece6fSJohn Baldwin 			else if (!kdb_active && !panicstr) {
62441109518SJohn Baldwin 				printf("spin lock %s held by %p for > 5 seconds\n",
62519284646SJohn Baldwin 				    m->mtx_object.lo_name, (void *)m->mtx_lock);
62641109518SJohn Baldwin #ifdef WITNESS
62741109518SJohn Baldwin 				witness_display_spinlock(&m->mtx_object,
62841109518SJohn Baldwin 				    mtx_owner(m));
62941109518SJohn Baldwin #endif
63041109518SJohn Baldwin 				panic("spin lock held too long");
63141109518SJohn Baldwin 			}
6329f1b87f1SMaxime Henrion 			cpu_spinwait();
63336412d79SJohn Baldwin 		}
634c6a37e84SJohn Baldwin 		spinlock_enter();
63536412d79SJohn Baldwin 	}
63636412d79SJohn Baldwin 
63719284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6389ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
6399ed346baSBosko Milekic 
64036412d79SJohn Baldwin 	return;
64136412d79SJohn Baldwin }
64233fb8a38SJohn Baldwin #endif /* SMP */
64336412d79SJohn Baldwin 
6449ed346baSBosko Milekic /*
6459ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
6469ed346baSBosko Milekic  *
6479ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
6489ed346baSBosko Milekic  * need to wake up a blocked thread).
6499ed346baSBosko Milekic  */
65036412d79SJohn Baldwin void
6519ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
65236412d79SJohn Baldwin {
653961a7b24SJohn Baldwin 	struct turnstile *ts;
6540c0b25aeSJohn Baldwin #ifndef PREEMPTION
655b40ce416SJulian Elischer 	struct thread *td, *td1;
6560c0b25aeSJohn Baldwin #endif
6579ed346baSBosko Milekic 
65808812b39SBosko Milekic 	if (mtx_recursed(m)) {
65936412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
66008812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
66119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6629ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
66336412d79SJohn Baldwin 		return;
66436412d79SJohn Baldwin 	}
6659ed346baSBosko Milekic 
6662ff0e645SJohn Baldwin 	turnstile_lock(&m->mtx_object);
667961a7b24SJohn Baldwin 	ts = turnstile_lookup(&m->mtx_object);
66819284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6699ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
6709ed346baSBosko Milekic 
671ece2d989SPawel Jakub Dawidek #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
672961a7b24SJohn Baldwin 	if (ts == NULL) {
6732498cf8cSJohn Baldwin 		_release_lock_quick(m);
6742498cf8cSJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6752498cf8cSJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
676961a7b24SJohn Baldwin 		turnstile_release(&m->mtx_object);
6772498cf8cSJohn Baldwin 		return;
6782498cf8cSJohn Baldwin 	}
679961a7b24SJohn Baldwin #else
680961a7b24SJohn Baldwin 	MPASS(ts != NULL);
6812498cf8cSJohn Baldwin #endif
6820c0b25aeSJohn Baldwin #ifndef PREEMPTION
683961a7b24SJohn Baldwin 	/* XXX */
6847aa4f685SJohn Baldwin 	td1 = turnstile_head(ts, TS_EXCLUSIVE_QUEUE);
6850c0b25aeSJohn Baldwin #endif
686535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
6877aa4f685SJohn Baldwin 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
688535eb309SJohn Baldwin 	_release_lock_quick(m);
689535eb309SJohn Baldwin #else
6907aa4f685SJohn Baldwin 	if (turnstile_signal(ts, TS_EXCLUSIVE_QUEUE)) {
69136412d79SJohn Baldwin 		_release_lock_quick(m);
69219284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6939ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
694961a7b24SJohn Baldwin 	} else {
695f7ee1590SJohn Baldwin 		m->mtx_lock = MTX_CONTESTED;
69619284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
697961a7b24SJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
698961a7b24SJohn Baldwin 			    m);
699e0817317SJulian Elischer 	}
700535eb309SJohn Baldwin #endif
7017aa4f685SJohn Baldwin 	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
7029ed346baSBosko Milekic 
7030c0b25aeSJohn Baldwin #ifndef PREEMPTION
704961a7b24SJohn Baldwin 	/*
705961a7b24SJohn Baldwin 	 * XXX: This is just a hack until preemption is done.  However,
706961a7b24SJohn Baldwin 	 * once preemption is done we need to either wrap the
707961a7b24SJohn Baldwin 	 * turnstile_signal() and release of the actual lock in an
708961a7b24SJohn Baldwin 	 * extra critical section or change the preemption code to
709961a7b24SJohn Baldwin 	 * always just set a flag and never do instant-preempts.
710961a7b24SJohn Baldwin 	 */
711961a7b24SJohn Baldwin 	td = curthread;
712961a7b24SJohn Baldwin 	if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
713961a7b24SJohn Baldwin 		return;
714961a7b24SJohn Baldwin 	mtx_lock_spin(&sched_lock);
715961a7b24SJohn Baldwin 	if (!TD_IS_RUNNING(td1)) {
71636412d79SJohn Baldwin #ifdef notyet
717b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
718b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
71936412d79SJohn Baldwin 
72036412d79SJohn Baldwin 			if (it->it_interrupted) {
72119284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
72236412d79SJohn Baldwin 					CTR2(KTR_LOCK,
72315ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
72436412d79SJohn Baldwin 					    it, it->it_interrupted);
72536412d79SJohn Baldwin 				intr_thd_fixup(it);
72636412d79SJohn Baldwin 			}
72736412d79SJohn Baldwin 		}
72836412d79SJohn Baldwin #endif
72919284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
730562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
7319ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
7329ed346baSBosko Milekic 			    (void *)m->mtx_lock);
7339ed346baSBosko Milekic 
734bf0acc27SJohn Baldwin 		mi_switch(SW_INVOL, NULL);
73519284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7369ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
73731271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
73836412d79SJohn Baldwin 	}
7399ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
7400c0b25aeSJohn Baldwin #endif
7419ed346baSBosko Milekic 
7429ed346baSBosko Milekic 	return;
7439ed346baSBosko Milekic }
7449ed346baSBosko Milekic 
7459ed346baSBosko Milekic /*
7469ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
7479ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
7489ed346baSBosko Milekic  */
7499ed346baSBosko Milekic 
7509ed346baSBosko Milekic /*
75115ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
7529ed346baSBosko Milekic  */
7531103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
7540cde2e34SJason Evans void
75556771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
7560cde2e34SJason Evans {
7575cb0fbe4SJohn Baldwin 
7581126349aSPaul Saab 	if (panicstr != NULL || dumping)
7595cb0fbe4SJohn Baldwin 		return;
760a10f4966SJake Burkholder 	switch (what) {
7610cde2e34SJason Evans 	case MA_OWNED:
7620cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
7630cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
764a10f4966SJake Burkholder 		if (!mtx_owned(m))
7650cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
76619284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
767a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
768a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
7690cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
77019284646SJohn Baldwin 				    m->mtx_object.lo_name, file, line);
771a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
7720cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
77319284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7740cde2e34SJason Evans 		}
7750cde2e34SJason Evans 		break;
7760cde2e34SJason Evans 	case MA_NOTOWNED:
777a10f4966SJake Burkholder 		if (mtx_owned(m))
7780cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
77919284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7800cde2e34SJason Evans 		break;
7810cde2e34SJason Evans 	default:
78256771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
7830cde2e34SJason Evans 	}
7840cde2e34SJason Evans }
7850cde2e34SJason Evans #endif
7860cde2e34SJason Evans 
7879ed346baSBosko Milekic /*
7889ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
78919284646SJohn Baldwin  *
79019284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
79119284646SJohn Baldwin  * maintained by the witness code.
7929ed346baSBosko Milekic  */
79336412d79SJohn Baldwin #ifdef MUTEX_DEBUG
79436412d79SJohn Baldwin 
7954d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
79636412d79SJohn Baldwin 
79719284646SJohn Baldwin void
79819284646SJohn Baldwin mtx_validate(struct mtx *m)
79936412d79SJohn Baldwin {
80036412d79SJohn Baldwin 
80136412d79SJohn Baldwin /*
802fa669ab7SPoul-Henning Kamp  * XXX: When kernacc() does not require Giant we can reenable this check
803fa669ab7SPoul-Henning Kamp  */
804fa669ab7SPoul-Henning Kamp #ifdef notyet
805fa669ab7SPoul-Henning Kamp 	/*
80676dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
80776dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
80876dcbd6fSBosko Milekic 	 * requires Giant itself.
80976dcbd6fSBosko Milekic 	 */
810ab07087eSBosko Milekic 	if (!cold)
811ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
812ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
81319284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
81436412d79SJohn Baldwin #endif
81536412d79SJohn Baldwin }
81636412d79SJohn Baldwin #endif
81736412d79SJohn Baldwin 
8189ed346baSBosko Milekic /*
819c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
820c27b5699SAndrew R. Reiter  */
821c27b5699SAndrew R. Reiter void
822c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
823c27b5699SAndrew R. Reiter {
824c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
825c27b5699SAndrew R. Reiter 
8260c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
827c27b5699SAndrew R. Reiter }
828c27b5699SAndrew R. Reiter 
829c27b5699SAndrew R. Reiter /*
8309ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
8310c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
8320c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
8330c88508aSJohn Baldwin  * witness.
8349ed346baSBosko Milekic  */
83536412d79SJohn Baldwin void
8360c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
83736412d79SJohn Baldwin {
83883a81bcbSJohn Baldwin 	struct lock_class *class;
83983a81bcbSJohn Baldwin 	int flags;
8409ed346baSBosko Milekic 
84119284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
84275d468eeSJohn Baldwin 	    MTX_NOWITNESS | MTX_DUPOK)) == 0);
8439ed346baSBosko Milekic 
84436412d79SJohn Baldwin #ifdef MUTEX_DEBUG
8459ed346baSBosko Milekic 	/* Diagnostic and error correction */
84619284646SJohn Baldwin 	mtx_validate(m);
8476936206eSJohn Baldwin #endif
84836412d79SJohn Baldwin 
84983a81bcbSJohn Baldwin 	/* Determine lock class and lock flags. */
85019284646SJohn Baldwin 	if (opts & MTX_SPIN)
85183a81bcbSJohn Baldwin 		class = &lock_class_mtx_spin;
85219284646SJohn Baldwin 	else
85383a81bcbSJohn Baldwin 		class = &lock_class_mtx_sleep;
85483a81bcbSJohn Baldwin 	flags = 0;
85519284646SJohn Baldwin 	if (opts & MTX_QUIET)
85683a81bcbSJohn Baldwin 		flags |= LO_QUIET;
85719284646SJohn Baldwin 	if (opts & MTX_RECURSE)
85883a81bcbSJohn Baldwin 		flags |= LO_RECURSABLE;
85919284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
86083a81bcbSJohn Baldwin 		flags |= LO_WITNESS;
861f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
86283a81bcbSJohn Baldwin 		flags |= LO_DUPOK;
86319284646SJohn Baldwin 
86483a81bcbSJohn Baldwin 	/* Initialize mutex. */
86519284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
86683a81bcbSJohn Baldwin 	m->mtx_recurse = 0;
86783a81bcbSJohn Baldwin #ifdef MUTEX_PROFILING
86883a81bcbSJohn Baldwin 	m->mtx_acqtime = 0;
86983a81bcbSJohn Baldwin 	m->mtx_filename = NULL;
87083a81bcbSJohn Baldwin 	m->mtx_lineno = 0;
87183a81bcbSJohn Baldwin 	m->mtx_contest_holding = 0;
87283a81bcbSJohn Baldwin 	m->mtx_contest_locking = 0;
87383a81bcbSJohn Baldwin #endif
8749ed346baSBosko Milekic 
87583a81bcbSJohn Baldwin 	lock_init(&m->mtx_object, class, name, type, flags);
87636412d79SJohn Baldwin }
87736412d79SJohn Baldwin 
8789ed346baSBosko Milekic /*
87919284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
88019284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
88119284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
88219284646SJohn Baldwin  * flags.
8839ed346baSBosko Milekic  */
88436412d79SJohn Baldwin void
88536412d79SJohn Baldwin mtx_destroy(struct mtx *m)
88636412d79SJohn Baldwin {
88736412d79SJohn Baldwin 
88819284646SJohn Baldwin 	if (!mtx_owned(m))
88919284646SJohn Baldwin 		MPASS(mtx_unowned(m));
89019284646SJohn Baldwin 	else {
89108812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
8929ed346baSBosko Milekic 
893861a2308SScott Long 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
89483a81bcbSJohn Baldwin 		if (LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin)
895861a2308SScott Long 			spinlock_exit();
896764e4d54SJohn Baldwin 		else
897764e4d54SJohn Baldwin 			curthread->td_locks--;
898861a2308SScott Long 
89919284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
900c86b6ff5SJohn Baldwin 		WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
901c86b6ff5SJohn Baldwin 		    __LINE__);
90236412d79SJohn Baldwin 	}
9030384fff8SJason Evans 
904186abbd7SJohn Baldwin 	m->mtx_lock = MTX_DESTROYED;
90583a81bcbSJohn Baldwin 	lock_destroy(&m->mtx_object);
9060384fff8SJason Evans }
907d23f5958SMatthew Dillon 
908d23f5958SMatthew Dillon /*
909c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
910c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
911c53c013bSJohn Baldwin  * setup before this is called.
912c53c013bSJohn Baldwin  */
913c53c013bSJohn Baldwin void
914c53c013bSJohn Baldwin mutex_init(void)
915c53c013bSJohn Baldwin {
916c53c013bSJohn Baldwin 
917961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
918961a7b24SJohn Baldwin 	init_turnstiles();
919961a7b24SJohn Baldwin 
920c53c013bSJohn Baldwin 	/*
921c53c013bSJohn Baldwin 	 * Initialize mutexes.
922c53c013bSJohn Baldwin 	 */
9230c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
9240c88508aSJohn Baldwin 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
9250c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
9268c4b6380SJohn Baldwin 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
927c53c013bSJohn Baldwin 	mtx_lock(&Giant);
928c53c013bSJohn Baldwin }
929d272fe53SJohn Baldwin 
930d272fe53SJohn Baldwin #ifdef DDB
931d272fe53SJohn Baldwin void
932d272fe53SJohn Baldwin db_show_mtx(struct lock_object *lock)
933d272fe53SJohn Baldwin {
934d272fe53SJohn Baldwin 	struct thread *td;
935d272fe53SJohn Baldwin 	struct mtx *m;
936d272fe53SJohn Baldwin 
937d272fe53SJohn Baldwin 	m = (struct mtx *)lock;
938d272fe53SJohn Baldwin 
939d272fe53SJohn Baldwin 	db_printf(" flags: {");
94083a81bcbSJohn Baldwin 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
941d272fe53SJohn Baldwin 		db_printf("SPIN");
942d272fe53SJohn Baldwin 	else
943d272fe53SJohn Baldwin 		db_printf("DEF");
944d272fe53SJohn Baldwin 	if (m->mtx_object.lo_flags & LO_RECURSABLE)
945d272fe53SJohn Baldwin 		db_printf(", RECURSE");
946d272fe53SJohn Baldwin 	if (m->mtx_object.lo_flags & LO_DUPOK)
947d272fe53SJohn Baldwin 		db_printf(", DUPOK");
948d272fe53SJohn Baldwin 	db_printf("}\n");
949d272fe53SJohn Baldwin 	db_printf(" state: {");
950d272fe53SJohn Baldwin 	if (mtx_unowned(m))
951d272fe53SJohn Baldwin 		db_printf("UNOWNED");
952d272fe53SJohn Baldwin 	else {
953d272fe53SJohn Baldwin 		db_printf("OWNED");
954d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_CONTESTED)
955d272fe53SJohn Baldwin 			db_printf(", CONTESTED");
956d272fe53SJohn Baldwin 		if (m->mtx_lock & MTX_RECURSED)
957d272fe53SJohn Baldwin 			db_printf(", RECURSED");
958d272fe53SJohn Baldwin 	}
959d272fe53SJohn Baldwin 	db_printf("}\n");
960d272fe53SJohn Baldwin 	if (!mtx_unowned(m)) {
961d272fe53SJohn Baldwin 		td = mtx_owner(m);
962d272fe53SJohn Baldwin 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
963d272fe53SJohn Baldwin 		    td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
964d272fe53SJohn Baldwin 		if (mtx_recursed(m))
965d272fe53SJohn Baldwin 			db_printf(" recursed: %d\n", m->mtx_recurse);
966d272fe53SJohn Baldwin 	}
967d272fe53SJohn Baldwin }
968d272fe53SJohn Baldwin #endif
969