xref: /freebsd/sys/kern/kern_mutex.c (revision 9923b511ed09f7e9aff331c1de463c09bf9af55e)
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>
482d50560aSMarcel Moolenaar #include <sys/kdb.h>
4936412d79SJohn Baldwin #include <sys/kernel.h>
506c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
5119284646SJohn Baldwin #include <sys/lock.h>
52fb919e4dSMark Murray #include <sys/malloc.h>
5319284646SJohn Baldwin #include <sys/mutex.h>
540384fff8SJason Evans #include <sys/proc.h>
55c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
56b43179fbSJeff Roberson #include <sys/sched.h>
576c35e809SDag-Erling Smørgrav #include <sys/sbuf.h>
58a5a96a19SJohn Baldwin #include <sys/sysctl.h>
59961a7b24SJohn Baldwin #include <sys/turnstile.h>
6036412d79SJohn Baldwin #include <sys/vmmeter.h>
610384fff8SJason Evans 
6236412d79SJohn Baldwin #include <machine/atomic.h>
6336412d79SJohn Baldwin #include <machine/bus.h>
6436412d79SJohn Baldwin #include <machine/clock.h>
650384fff8SJason Evans #include <machine/cpu.h>
6636412d79SJohn Baldwin 
679c36c934SJohn Baldwin #include <ddb/ddb.h>
689c36c934SJohn Baldwin 
6936412d79SJohn Baldwin #include <vm/vm.h>
7036412d79SJohn Baldwin #include <vm/vm_extern.h>
7136412d79SJohn Baldwin 
720cde2e34SJason Evans /*
739ed346baSBosko Milekic  * Internal utility macros.
740cde2e34SJason Evans  */
759ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
760cde2e34SJason Evans 
779ed346baSBosko Milekic #define mtx_owner(m)	(mtx_unowned((m)) ? NULL \
78b40ce416SJulian Elischer 	: (struct thread *)((m)->mtx_lock & MTX_FLAGMASK))
799ed346baSBosko Milekic 
800cde2e34SJason Evans /*
8119284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
820cde2e34SJason Evans  */
8319284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
8419284646SJohn Baldwin 	"sleep mutex",
8519284646SJohn Baldwin 	LC_SLEEPLOCK | LC_RECURSABLE
8619284646SJohn Baldwin };
8719284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
8819284646SJohn Baldwin 	"spin mutex",
8919284646SJohn Baldwin 	LC_SPINLOCK | LC_RECURSABLE
908484de75SJohn Baldwin };
918484de75SJohn Baldwin 
929ed346baSBosko Milekic /*
93c53c013bSJohn Baldwin  * System-wide mutexes
94c53c013bSJohn Baldwin  */
95c53c013bSJohn Baldwin struct mtx sched_lock;
96c53c013bSJohn Baldwin struct mtx Giant;
97c53c013bSJohn Baldwin 
986c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
996c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging");
1006c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling");
1016c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0;
1026c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW,
1036c35e809SDag-Erling Smørgrav     &mutex_prof_enable, 0, "Enable tracing of mutex holdtime");
1046c35e809SDag-Erling Smørgrav 
1056c35e809SDag-Erling Smørgrav struct mutex_prof {
1066c35e809SDag-Erling Smørgrav 	const char	*name;
1076c35e809SDag-Erling Smørgrav 	const char	*file;
1086c35e809SDag-Erling Smørgrav 	int		line;
109ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_max;
110ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_tot;
111ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_cur;
1128dc10be8SRobert Watson 	uintmax_t	cnt_contest_holding;
1138dc10be8SRobert Watson 	uintmax_t	cnt_contest_locking;
114e6330704SDag-Erling Smørgrav 	struct mutex_prof *next;
1156c35e809SDag-Erling Smørgrav };
1166c35e809SDag-Erling Smørgrav 
1176c35e809SDag-Erling Smørgrav /*
1186c35e809SDag-Erling Smørgrav  * mprof_buf is a static pool of profiling records to avoid possible
1196c35e809SDag-Erling Smørgrav  * reentrance of the memory allocation functions.
1206c35e809SDag-Erling Smørgrav  *
1216c35e809SDag-Erling Smørgrav  * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE.
1226c35e809SDag-Erling Smørgrav  */
12300096801SJohn-Mark Gurney #ifdef MPROF_BUFFERS
12400096801SJohn-Mark Gurney #define NUM_MPROF_BUFFERS	MPROF_BUFFERS
12500096801SJohn-Mark Gurney #else
126e6330704SDag-Erling Smørgrav #define	NUM_MPROF_BUFFERS	1000
12700096801SJohn-Mark Gurney #endif
1286c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS];
1296c35e809SDag-Erling Smørgrav static int first_free_mprof_buf;
13000096801SJohn-Mark Gurney #ifndef MPROF_HASH_SIZE
131e6330704SDag-Erling Smørgrav #define	MPROF_HASH_SIZE		1009
13200096801SJohn-Mark Gurney #endif
13300096801SJohn-Mark Gurney #if NUM_MPROF_BUFFERS >= MPROF_HASH_SIZE
13400096801SJohn-Mark Gurney #error MPROF_BUFFERS must be larger than MPROF_HASH_SIZE
13500096801SJohn-Mark Gurney #endif
1366c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE];
1370bd5f797SMike Makonnen /* SWAG: sbuf size = avg stat. line size * number of locks */
1380bd5f797SMike Makonnen #define MPROF_SBUF_SIZE		256 * 400
1396c35e809SDag-Erling Smørgrav 
1406c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions;
1416c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD,
1426c35e809SDag-Erling Smørgrav     &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded");
1436c35e809SDag-Erling Smørgrav static int mutex_prof_records;
1446c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD,
1456c35e809SDag-Erling Smørgrav     &mutex_prof_records, 0, "Number of profiling records");
1466c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS;
1476c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD,
1486c35e809SDag-Erling Smørgrav     &mutex_prof_maxrecords, 0, "Maximum number of profiling records");
1496c35e809SDag-Erling Smørgrav static int mutex_prof_rejected;
1506c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD,
1516c35e809SDag-Erling Smørgrav     &mutex_prof_rejected, 0, "Number of rejected profiling records");
1526c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE;
1536c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD,
1546c35e809SDag-Erling Smørgrav     &mutex_prof_hashsize, 0, "Hash size");
1556c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0;
1566c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD,
1576c35e809SDag-Erling Smørgrav     &mutex_prof_collisions, 0, "Number of hash collisions");
1586c35e809SDag-Erling Smørgrav 
1596c35e809SDag-Erling Smørgrav /*
1606c35e809SDag-Erling Smørgrav  * mprof_mtx protects the profiling buffers and the hash.
1616c35e809SDag-Erling Smørgrav  */
1626c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx;
163e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET);
1646c35e809SDag-Erling Smørgrav 
165b784ffe9SDag-Erling Smørgrav static u_int64_t
166b784ffe9SDag-Erling Smørgrav nanoseconds(void)
167b784ffe9SDag-Erling Smørgrav {
168b784ffe9SDag-Erling Smørgrav 	struct timespec tv;
169b784ffe9SDag-Erling Smørgrav 
170b784ffe9SDag-Erling Smørgrav 	nanotime(&tv);
171b784ffe9SDag-Erling Smørgrav 	return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec);
172b784ffe9SDag-Erling Smørgrav }
173b784ffe9SDag-Erling Smørgrav 
1746c35e809SDag-Erling Smørgrav static int
1756c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
1766c35e809SDag-Erling Smørgrav {
1776c35e809SDag-Erling Smørgrav 	struct sbuf *sb;
1786c35e809SDag-Erling Smørgrav 	int error, i;
1790bd5f797SMike Makonnen 	static int multiplier = 1;
1806c35e809SDag-Erling Smørgrav 
1816c35e809SDag-Erling Smørgrav 	if (first_free_mprof_buf == 0)
1826d036900SDag-Erling Smørgrav 		return (SYSCTL_OUT(req, "No locking recorded",
1836d036900SDag-Erling Smørgrav 		    sizeof("No locking recorded")));
1846c35e809SDag-Erling Smørgrav 
1850bd5f797SMike Makonnen retry_sbufops:
1860bd5f797SMike Makonnen 	sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN);
1878dc10be8SRobert Watson 	sbuf_printf(sb, "%6s %12s %11s %5s %12s %12s %s\n",
1888dc10be8SRobert Watson 	    "max", "total", "count", "avg", "cnt_hold", "cnt_lock", "name");
1896d036900SDag-Erling Smørgrav 	/*
1906d036900SDag-Erling Smørgrav 	 * XXX this spinlock seems to be by far the largest perpetrator
1916d036900SDag-Erling Smørgrav 	 * of spinlock latency (1.6 msec on an Athlon1600 was recorded
1926d036900SDag-Erling Smørgrav 	 * even before I pessimized it further by moving the average
1936d036900SDag-Erling Smørgrav 	 * computation here).
1946d036900SDag-Erling Smørgrav 	 */
1956c35e809SDag-Erling Smørgrav 	mtx_lock_spin(&mprof_mtx);
1960bd5f797SMike Makonnen 	for (i = 0; i < first_free_mprof_buf; ++i) {
1978dc10be8SRobert Watson 		sbuf_printf(sb, "%6ju %12ju %11ju %5ju %12ju %12ju %s:%d (%s)\n",
198ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_max / 1000,
199ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_tot / 1000,
200ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_cur,
201ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 :
202ecf031c9SDag-Erling Smørgrav 			mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000),
2038dc10be8SRobert Watson 		    mprof_buf[i].cnt_contest_holding,
2048dc10be8SRobert Watson 		    mprof_buf[i].cnt_contest_locking,
2056c35e809SDag-Erling Smørgrav 		    mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name);
2060bd5f797SMike Makonnen 		if (sbuf_overflowed(sb)) {
2070bd5f797SMike Makonnen 			mtx_unlock_spin(&mprof_mtx);
2080bd5f797SMike Makonnen 			sbuf_delete(sb);
2090bd5f797SMike Makonnen 			multiplier++;
2100bd5f797SMike Makonnen 			goto retry_sbufops;
2110bd5f797SMike Makonnen 		}
2120bd5f797SMike Makonnen 	}
2136c35e809SDag-Erling Smørgrav 	mtx_unlock_spin(&mprof_mtx);
2146c35e809SDag-Erling Smørgrav 	sbuf_finish(sb);
2156c35e809SDag-Erling Smørgrav 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2166c35e809SDag-Erling Smørgrav 	sbuf_delete(sb);
2176c35e809SDag-Erling Smørgrav 	return (error);
2186c35e809SDag-Erling Smørgrav }
2196c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD,
2206c35e809SDag-Erling Smørgrav     NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics");
22194ffb20dSRobert Watson 
22294ffb20dSRobert Watson static int
22394ffb20dSRobert Watson reset_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
22494ffb20dSRobert Watson {
22594ffb20dSRobert Watson 	int error, v;
22694ffb20dSRobert Watson 
22794ffb20dSRobert Watson 	if (first_free_mprof_buf == 0)
22894ffb20dSRobert Watson 		return (0);
22994ffb20dSRobert Watson 
23094ffb20dSRobert Watson 	v = 0;
23194ffb20dSRobert Watson 	error = sysctl_handle_int(oidp, &v, 0, req);
23294ffb20dSRobert Watson 	if (error)
23394ffb20dSRobert Watson 		return (error);
23494ffb20dSRobert Watson 	if (req->newptr == NULL)
23594ffb20dSRobert Watson 		return (error);
23694ffb20dSRobert Watson 	if (v == 0)
23794ffb20dSRobert Watson 		return (0);
23894ffb20dSRobert Watson 
23994ffb20dSRobert Watson 	mtx_lock_spin(&mprof_mtx);
24094ffb20dSRobert Watson 	bzero(mprof_buf, sizeof(*mprof_buf) * first_free_mprof_buf);
24194ffb20dSRobert Watson 	bzero(mprof_hash, sizeof(struct mtx *) * MPROF_HASH_SIZE);
24294ffb20dSRobert Watson 	first_free_mprof_buf = 0;
24394ffb20dSRobert Watson 	mtx_unlock_spin(&mprof_mtx);
24494ffb20dSRobert Watson 	return (0);
24594ffb20dSRobert Watson }
24694ffb20dSRobert Watson SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW,
24794ffb20dSRobert Watson     NULL, 0, reset_mutex_prof_stats, "I", "Reset mutex profiling statistics");
2486c35e809SDag-Erling Smørgrav #endif
2496c35e809SDag-Erling Smørgrav 
2500cde2e34SJason Evans /*
2516283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2526283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2536283b7d0SJohn Baldwin  */
2546283b7d0SJohn Baldwin void
2556283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
2566283b7d0SJohn Baldwin {
2576283b7d0SJohn Baldwin 
258dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
2590d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
2600d975d63SJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
2610d975d63SJohn Baldwin 	    file, line));
2628d768e76SJohn Baldwin 	WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
2638d768e76SJohn Baldwin 	    file, line);
264dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
265dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
266dde96c99SJohn Baldwin 	    line);
267dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
2686c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
2696c35e809SDag-Erling Smørgrav 	/* don't reset the timer when/if recursing */
270b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime == 0) {
271b61860adSDag-Erling Smørgrav 		m->mtx_filename = file;
272b61860adSDag-Erling Smørgrav 		m->mtx_lineno = line;
273b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0;
2746c35e809SDag-Erling Smørgrav 		++mutex_prof_acquisitions;
2756c35e809SDag-Erling Smørgrav 	}
2766c35e809SDag-Erling Smørgrav #endif
2776283b7d0SJohn Baldwin }
2786283b7d0SJohn Baldwin 
2796283b7d0SJohn Baldwin void
2806283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
2816283b7d0SJohn Baldwin {
2826283b7d0SJohn Baldwin 
283dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
2840d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
2850d975d63SJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
2860d975d63SJohn Baldwin 	    file, line));
2870d975d63SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
2880d975d63SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
2890d975d63SJohn Baldwin 	    line);
29021377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
2916c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
292b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime != 0) {
2936c35e809SDag-Erling Smørgrav 		static const char *unknown = "(unknown)";
2946c35e809SDag-Erling Smørgrav 		struct mutex_prof *mpp;
295b784ffe9SDag-Erling Smørgrav 		u_int64_t acqtime, now;
2966c35e809SDag-Erling Smørgrav 		const char *p, *q;
297e6330704SDag-Erling Smørgrav 		volatile u_int hash;
2986c35e809SDag-Erling Smørgrav 
299b784ffe9SDag-Erling Smørgrav 		now = nanoseconds();
300b61860adSDag-Erling Smørgrav 		acqtime = m->mtx_acqtime;
301b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = 0;
302b784ffe9SDag-Erling Smørgrav 		if (now <= acqtime)
3036c35e809SDag-Erling Smørgrav 			goto out;
3040bd5f797SMike Makonnen 		for (p = m->mtx_filename;
3050bd5f797SMike Makonnen 		    p != NULL && strncmp(p, "../", 3) == 0; p += 3)
3066c35e809SDag-Erling Smørgrav 			/* nothing */ ;
3076c35e809SDag-Erling Smørgrav 		if (p == NULL || *p == '\0')
3086c35e809SDag-Erling Smørgrav 			p = unknown;
309b61860adSDag-Erling Smørgrav 		for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q)
3106c35e809SDag-Erling Smørgrav 			hash = (hash * 2 + *q) % MPROF_HASH_SIZE;
3116c35e809SDag-Erling Smørgrav 		mtx_lock_spin(&mprof_mtx);
312e6330704SDag-Erling Smørgrav 		for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next)
313b61860adSDag-Erling Smørgrav 			if (mpp->line == m->mtx_lineno &&
314b61860adSDag-Erling Smørgrav 			    strcmp(mpp->file, p) == 0)
3156c35e809SDag-Erling Smørgrav 				break;
3166c35e809SDag-Erling Smørgrav 		if (mpp == NULL) {
3176c35e809SDag-Erling Smørgrav 			/* Just exit if we cannot get a trace buffer */
3186c35e809SDag-Erling Smørgrav 			if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) {
3196c35e809SDag-Erling Smørgrav 				++mutex_prof_rejected;
3206c35e809SDag-Erling Smørgrav 				goto unlock;
3216c35e809SDag-Erling Smørgrav 			}
3226c35e809SDag-Erling Smørgrav 			mpp = &mprof_buf[first_free_mprof_buf++];
3236c35e809SDag-Erling Smørgrav 			mpp->name = mtx_name(m);
3246c35e809SDag-Erling Smørgrav 			mpp->file = p;
325b61860adSDag-Erling Smørgrav 			mpp->line = m->mtx_lineno;
326e6330704SDag-Erling Smørgrav 			mpp->next = mprof_hash[hash];
327e6330704SDag-Erling Smørgrav 			if (mprof_hash[hash] != NULL)
328e6330704SDag-Erling Smørgrav 				++mutex_prof_collisions;
3296c35e809SDag-Erling Smørgrav 			mprof_hash[hash] = mpp;
330e6330704SDag-Erling Smørgrav 			++mutex_prof_records;
3316c35e809SDag-Erling Smørgrav 		}
3326c35e809SDag-Erling Smørgrav 		/*
3336c35e809SDag-Erling Smørgrav 		 * Record if the mutex has been held longer now than ever
3346d036900SDag-Erling Smørgrav 		 * before.
3356c35e809SDag-Erling Smørgrav 		 */
336ecf031c9SDag-Erling Smørgrav 		if (now - acqtime > mpp->cnt_max)
337ecf031c9SDag-Erling Smørgrav 			mpp->cnt_max = now - acqtime;
338ecf031c9SDag-Erling Smørgrav 		mpp->cnt_tot += now - acqtime;
339ecf031c9SDag-Erling Smørgrav 		mpp->cnt_cur++;
3408dc10be8SRobert Watson 		/*
3418dc10be8SRobert Watson 		 * There's a small race, really we should cmpxchg
3428dc10be8SRobert Watson 		 * 0 with the current value, but that would bill
3438dc10be8SRobert Watson 		 * the contention to the wrong lock instance if
3448dc10be8SRobert Watson 		 * it followed this also.
3458dc10be8SRobert Watson 		 */
3468dc10be8SRobert Watson 		mpp->cnt_contest_holding += m->mtx_contest_holding;
3478dc10be8SRobert Watson 		m->mtx_contest_holding = 0;
3488dc10be8SRobert Watson 		mpp->cnt_contest_locking += m->mtx_contest_locking;
3498dc10be8SRobert Watson 		m->mtx_contest_locking = 0;
3506c35e809SDag-Erling Smørgrav unlock:
3516c35e809SDag-Erling Smørgrav 		mtx_unlock_spin(&mprof_mtx);
3526c35e809SDag-Erling Smørgrav 	}
3536c35e809SDag-Erling Smørgrav out:
3546c35e809SDag-Erling Smørgrav #endif
355dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
3566283b7d0SJohn Baldwin }
3576283b7d0SJohn Baldwin 
3586283b7d0SJohn Baldwin void
3596283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3606283b7d0SJohn Baldwin {
3616283b7d0SJohn Baldwin 
362dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3630d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
3640d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
3650d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
3668d768e76SJohn Baldwin 	WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
3678d768e76SJohn Baldwin 	    file, line);
368ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1
369dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
370e8fdcfb5SJohn Baldwin #else
371e8fdcfb5SJohn Baldwin 	critical_enter();
372e8fdcfb5SJohn Baldwin #endif
373dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
374dde96c99SJohn Baldwin 	    line);
375dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3766283b7d0SJohn Baldwin }
3776283b7d0SJohn Baldwin 
3786283b7d0SJohn Baldwin void
3796283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3806283b7d0SJohn Baldwin {
3816283b7d0SJohn Baldwin 
382dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3830d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
3840d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
3850d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
386dde96c99SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
387dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
388dde96c99SJohn Baldwin 	    line);
3890d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
390ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1
391dde96c99SJohn Baldwin 	_rel_spin_lock(m);
392e8fdcfb5SJohn Baldwin #else
393e8fdcfb5SJohn Baldwin 	critical_exit();
394e8fdcfb5SJohn Baldwin #endif
3956283b7d0SJohn Baldwin }
3966283b7d0SJohn Baldwin 
3976283b7d0SJohn Baldwin /*
3989ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
399eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
400eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
4010cde2e34SJason Evans  */
4020cde2e34SJason Evans int
4039ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
4040cde2e34SJason Evans {
4050cde2e34SJason Evans 	int rval;
4060cde2e34SJason Evans 
407b40ce416SJulian Elischer 	MPASS(curthread != NULL);
4089ed346baSBosko Milekic 
409eac09796SJohn Baldwin 	if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) {
410eac09796SJohn Baldwin 		m->mtx_recurse++;
411eac09796SJohn Baldwin 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
412eac09796SJohn Baldwin 		rval = 1;
413eac09796SJohn Baldwin 	} else
414b40ce416SJulian Elischer 		rval = _obtain_lock(m, curthread);
4159ed346baSBosko Milekic 
41619284646SJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
4176b869595SJohn Baldwin 	if (rval)
4182d96f0b1SJohn Baldwin 		WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4192d96f0b1SJohn Baldwin 		    file, line);
4209ed346baSBosko Milekic 
42119284646SJohn Baldwin 	return (rval);
4220cde2e34SJason Evans }
4230cde2e34SJason Evans 
4240cde2e34SJason Evans /*
4259ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4269ed346baSBosko Milekic  *
4279ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4289ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4290cde2e34SJason Evans  */
4300cde2e34SJason Evans void
431bdcfcf5bSJohn Baldwin _mtx_lock_sleep(struct mtx *m, struct thread *td, int opts, const char *file,
432bdcfcf5bSJohn Baldwin     int line)
43336412d79SJohn Baldwin {
434961a7b24SJohn Baldwin 	struct turnstile *ts;
435701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
4362498cf8cSJohn Baldwin 	struct thread *owner;
4372498cf8cSJohn Baldwin #endif
4385fa8dd90SJohn Baldwin 	uintptr_t v;
43902bd1bcdSIan Dowse #ifdef KTR
44002bd1bcdSIan Dowse 	int cont_logged = 0;
44102bd1bcdSIan Dowse #endif
4428dc10be8SRobert Watson #ifdef MUTEX_PROFILING
4438dc10be8SRobert Watson 	int contested;
4448dc10be8SRobert Watson #endif
44536412d79SJohn Baldwin 
4465fa8dd90SJohn Baldwin 	if (mtx_owned(m)) {
447eac09796SJohn Baldwin 		KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0,
448eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
449eac09796SJohn Baldwin 		    m->mtx_object.lo_name, file, line));
45036412d79SJohn Baldwin 		m->mtx_recurse++;
45108812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
45219284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4535746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
45436412d79SJohn Baldwin 		return;
45536412d79SJohn Baldwin 	}
4569ed346baSBosko Milekic 
45719284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
45815ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
45915ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
46019284646SJohn Baldwin 		    m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
4611bd0eefbSJohn Baldwin 
4628dc10be8SRobert Watson #ifdef MUTEX_PROFILING
4638dc10be8SRobert Watson 	contested = 0;
4648dc10be8SRobert Watson #endif
465b40ce416SJulian Elischer 	while (!_obtain_lock(m, td)) {
4668dc10be8SRobert Watson #ifdef MUTEX_PROFILING
4678dc10be8SRobert Watson 		contested = 1;
4688dc10be8SRobert Watson 		atomic_add_int(&m->mtx_contest_holding, 1);
4698dc10be8SRobert Watson #endif
470961a7b24SJohn Baldwin 		ts = turnstile_lookup(&m->mtx_object);
4715fa8dd90SJohn Baldwin 		v = m->mtx_lock;
4725fa8dd90SJohn Baldwin 
47336412d79SJohn Baldwin 		/*
4749ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
475961a7b24SJohn Baldwin 		 * the turnstile chain lock.
47636412d79SJohn Baldwin 		 */
4775fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
478961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
4799f1b87f1SMaxime Henrion 			cpu_spinwait();
48036412d79SJohn Baldwin 			continue;
48136412d79SJohn Baldwin 		}
4829ed346baSBosko Milekic 
483535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
484535eb309SJohn Baldwin 		MPASS(v != MTX_CONTESTED);
485535eb309SJohn Baldwin #else
48636412d79SJohn Baldwin 		/*
4879ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
488f7ee1590SJohn Baldwin 		 * there are other threads blocked on it.  Grab ownership of
489f7ee1590SJohn Baldwin 		 * it and propagate its priority to the current thread if
490f7ee1590SJohn Baldwin 		 * necessary.
49136412d79SJohn Baldwin 		 */
49236412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
493961a7b24SJohn Baldwin 			MPASS(ts != NULL);
494b40ce416SJulian Elischer 			m->mtx_lock = (uintptr_t)td | MTX_CONTESTED;
495961a7b24SJohn Baldwin 			turnstile_claim(ts);
4968dc10be8SRobert Watson 			break;
49736412d79SJohn Baldwin 		}
498535eb309SJohn Baldwin #endif
4999ed346baSBosko Milekic 
50036412d79SJohn Baldwin 		/*
5019ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
5029ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
5039ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
50436412d79SJohn Baldwin 		 */
50536412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
50636412d79SJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, (void *)v,
50736412d79SJohn Baldwin 			(void *)(v | MTX_CONTESTED))) {
508961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
5099f1b87f1SMaxime Henrion 			cpu_spinwait();
51036412d79SJohn Baldwin 			continue;
51136412d79SJohn Baldwin 		}
51236412d79SJohn Baldwin 
513701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
5142498cf8cSJohn Baldwin 		/*
5152498cf8cSJohn Baldwin 		 * If the current owner of the lock is executing on another
5162498cf8cSJohn Baldwin 		 * CPU, spin instead of blocking.
5172498cf8cSJohn Baldwin 		 */
5182498cf8cSJohn Baldwin 		owner = (struct thread *)(v & MTX_FLAGMASK);
519a9abdce4SRobert Watson #ifdef ADAPTIVE_GIANT
520a9abdce4SRobert Watson 		if (TD_IS_RUNNING(owner)) {
521a9abdce4SRobert Watson #else
52227dad03cSJohn Baldwin 		if (m != &Giant && TD_IS_RUNNING(owner)) {
523a9abdce4SRobert Watson #endif
524961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
52527dad03cSJohn Baldwin 			while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
5269f1b87f1SMaxime Henrion 				cpu_spinwait();
5277fcca609SJohn Baldwin 			}
5282498cf8cSJohn Baldwin 			continue;
5292498cf8cSJohn Baldwin 		}
530701f1408SScott Long #endif	/* SMP && !NO_ADAPTIVE_MUTEXES */
5312498cf8cSJohn Baldwin 
5329ed346baSBosko Milekic 		/*
5337feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
5349ed346baSBosko Milekic 		 */
53536412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
53636412d79SJohn Baldwin 
53702bd1bcdSIan Dowse #ifdef KTR
53802bd1bcdSIan Dowse 		if (!cont_logged) {
53902bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
54002bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
54102bd1bcdSIan Dowse 			    td, file, line, m->mtx_object.lo_name,
54202bd1bcdSIan Dowse 			    WITNESS_FILE(&m->mtx_object),
54302bd1bcdSIan Dowse 			    WITNESS_LINE(&m->mtx_object));
54402bd1bcdSIan Dowse 			cont_logged = 1;
54502bd1bcdSIan Dowse 		}
54602bd1bcdSIan Dowse #endif
54736412d79SJohn Baldwin 
5489ed346baSBosko Milekic 		/*
549961a7b24SJohn Baldwin 		 * Block on the turnstile.
5509ed346baSBosko Milekic 		 */
551961a7b24SJohn Baldwin 		turnstile_wait(ts, &m->mtx_object, mtx_owner(m));
55236412d79SJohn Baldwin 	}
5539ed346baSBosko Milekic 
55402bd1bcdSIan Dowse #ifdef KTR
55502bd1bcdSIan Dowse 	if (cont_logged) {
55602bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
55702bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
55802bd1bcdSIan Dowse 		    m->mtx_object.lo_name, td, file, line);
55902bd1bcdSIan Dowse 	}
56002bd1bcdSIan Dowse #endif
5618dc10be8SRobert Watson #ifdef MUTEX_PROFILING
5628dc10be8SRobert Watson 	if (contested)
5638dc10be8SRobert Watson 		m->mtx_contest_locking++;
5648dc10be8SRobert Watson 	m->mtx_contest_holding = 0;
5658dc10be8SRobert Watson #endif
56636412d79SJohn Baldwin 	return;
5679ed346baSBosko Milekic }
5689ed346baSBosko Milekic 
5699ed346baSBosko Milekic /*
5709ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
5719ed346baSBosko Milekic  *
5729ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
5739ed346baSBosko Milekic  * is handled inline.
5749ed346baSBosko Milekic  */
5759ed346baSBosko Milekic void
576bdcfcf5bSJohn Baldwin _mtx_lock_spin(struct mtx *m, struct thread *td, int opts, const char *file,
577bdcfcf5bSJohn Baldwin     int line)
57836412d79SJohn Baldwin {
57936412d79SJohn Baldwin 	int i = 0;
58036412d79SJohn Baldwin 
58119284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
5825746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
5839ed346baSBosko Milekic 
58436412d79SJohn Baldwin 	for (;;) {
585bdcfcf5bSJohn Baldwin 		if (_obtain_lock(m, td))
58636412d79SJohn Baldwin 			break;
5879ed346baSBosko Milekic 
5887141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
5897e1f6dfeSJohn Baldwin 		critical_exit();
59036412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
591703fc290SJohn Baldwin 			if (i++ < 10000000) {
5929f1b87f1SMaxime Henrion 				cpu_spinwait();
59336412d79SJohn Baldwin 				continue;
594703fc290SJohn Baldwin 			}
5950e54ddadSJohn Baldwin 			if (i < 60000000)
59636412d79SJohn Baldwin 				DELAY(1);
5972d50560aSMarcel Moolenaar 			else if (!kdb_active) {
59841109518SJohn Baldwin 				printf("spin lock %s held by %p for > 5 seconds\n",
59919284646SJohn Baldwin 				    m->mtx_object.lo_name, (void *)m->mtx_lock);
60041109518SJohn Baldwin #ifdef WITNESS
60141109518SJohn Baldwin 				witness_display_spinlock(&m->mtx_object,
60241109518SJohn Baldwin 				    mtx_owner(m));
60341109518SJohn Baldwin #endif
60441109518SJohn Baldwin 				panic("spin lock held too long");
60541109518SJohn Baldwin 			}
6069f1b87f1SMaxime Henrion 			cpu_spinwait();
60736412d79SJohn Baldwin 		}
6087e1f6dfeSJohn Baldwin 		critical_enter();
60936412d79SJohn Baldwin 	}
61036412d79SJohn Baldwin 
61119284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6129ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
6139ed346baSBosko Milekic 
61436412d79SJohn Baldwin 	return;
61536412d79SJohn Baldwin }
61636412d79SJohn Baldwin 
6179ed346baSBosko Milekic /*
6189ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
6199ed346baSBosko Milekic  *
6209ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
6219ed346baSBosko Milekic  * need to wake up a blocked thread).
6229ed346baSBosko Milekic  */
62336412d79SJohn Baldwin void
6249ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
62536412d79SJohn Baldwin {
626961a7b24SJohn Baldwin 	struct turnstile *ts;
6270c0b25aeSJohn Baldwin #ifndef PREEMPTION
628b40ce416SJulian Elischer 	struct thread *td, *td1;
6290c0b25aeSJohn Baldwin #endif
6309ed346baSBosko Milekic 
63108812b39SBosko Milekic 	if (mtx_recursed(m)) {
63236412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
63308812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
63419284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6359ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
63636412d79SJohn Baldwin 		return;
63736412d79SJohn Baldwin 	}
6389ed346baSBosko Milekic 
639961a7b24SJohn Baldwin 	ts = turnstile_lookup(&m->mtx_object);
64019284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6419ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
6429ed346baSBosko Milekic 
643ece2d989SPawel Jakub Dawidek #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
644961a7b24SJohn Baldwin 	if (ts == NULL) {
6452498cf8cSJohn Baldwin 		_release_lock_quick(m);
6462498cf8cSJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6472498cf8cSJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
648961a7b24SJohn Baldwin 		turnstile_release(&m->mtx_object);
6492498cf8cSJohn Baldwin 		return;
6502498cf8cSJohn Baldwin 	}
651961a7b24SJohn Baldwin #else
652961a7b24SJohn Baldwin 	MPASS(ts != NULL);
6532498cf8cSJohn Baldwin #endif
6540c0b25aeSJohn Baldwin #ifndef PREEMPTION
655961a7b24SJohn Baldwin 	/* XXX */
656961a7b24SJohn Baldwin 	td1 = turnstile_head(ts);
6570c0b25aeSJohn Baldwin #endif
658535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL
659535eb309SJohn Baldwin 	turnstile_broadcast(ts);
660535eb309SJohn Baldwin 	_release_lock_quick(m);
661535eb309SJohn Baldwin #else
662961a7b24SJohn Baldwin 	if (turnstile_signal(ts)) {
66336412d79SJohn Baldwin 		_release_lock_quick(m);
66419284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6659ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
666961a7b24SJohn Baldwin 	} else {
667f7ee1590SJohn Baldwin 		m->mtx_lock = MTX_CONTESTED;
66819284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
669961a7b24SJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
670961a7b24SJohn Baldwin 			    m);
671e0817317SJulian Elischer 	}
672535eb309SJohn Baldwin #endif
673961a7b24SJohn Baldwin 	turnstile_unpend(ts);
6749ed346baSBosko Milekic 
6750c0b25aeSJohn Baldwin #ifndef PREEMPTION
676961a7b24SJohn Baldwin 	/*
677961a7b24SJohn Baldwin 	 * XXX: This is just a hack until preemption is done.  However,
678961a7b24SJohn Baldwin 	 * once preemption is done we need to either wrap the
679961a7b24SJohn Baldwin 	 * turnstile_signal() and release of the actual lock in an
680961a7b24SJohn Baldwin 	 * extra critical section or change the preemption code to
681961a7b24SJohn Baldwin 	 * always just set a flag and never do instant-preempts.
682961a7b24SJohn Baldwin 	 */
683961a7b24SJohn Baldwin 	td = curthread;
684961a7b24SJohn Baldwin 	if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
685961a7b24SJohn Baldwin 		return;
686961a7b24SJohn Baldwin 	mtx_lock_spin(&sched_lock);
687961a7b24SJohn Baldwin 	if (!TD_IS_RUNNING(td1)) {
68836412d79SJohn Baldwin #ifdef notyet
689b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
690b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
69136412d79SJohn Baldwin 
69236412d79SJohn Baldwin 			if (it->it_interrupted) {
69319284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
69436412d79SJohn Baldwin 					CTR2(KTR_LOCK,
69515ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
69636412d79SJohn Baldwin 					    it, it->it_interrupted);
69736412d79SJohn Baldwin 				intr_thd_fixup(it);
69836412d79SJohn Baldwin 			}
69936412d79SJohn Baldwin 		}
70036412d79SJohn Baldwin #endif
70119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
702562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
7039ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
7049ed346baSBosko Milekic 			    (void *)m->mtx_lock);
7059ed346baSBosko Milekic 
706bf0acc27SJohn Baldwin 		mi_switch(SW_INVOL, NULL);
70719284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7089ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
70931271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
71036412d79SJohn Baldwin 	}
7119ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
7120c0b25aeSJohn Baldwin #endif
7139ed346baSBosko Milekic 
7149ed346baSBosko Milekic 	return;
7159ed346baSBosko Milekic }
7169ed346baSBosko Milekic 
7179ed346baSBosko Milekic /*
7189ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
7199ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
7209ed346baSBosko Milekic  */
7219ed346baSBosko Milekic 
7229ed346baSBosko Milekic /*
72315ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
7249ed346baSBosko Milekic  */
7251103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
7260cde2e34SJason Evans void
72756771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
7280cde2e34SJason Evans {
7295cb0fbe4SJohn Baldwin 
7305cb0fbe4SJohn Baldwin 	if (panicstr != NULL)
7315cb0fbe4SJohn Baldwin 		return;
732a10f4966SJake Burkholder 	switch (what) {
7330cde2e34SJason Evans 	case MA_OWNED:
7340cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
7350cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
736a10f4966SJake Burkholder 		if (!mtx_owned(m))
7370cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
73819284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
739a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
740a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
7410cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
74219284646SJohn Baldwin 				    m->mtx_object.lo_name, file, line);
743a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
7440cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
74519284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7460cde2e34SJason Evans 		}
7470cde2e34SJason Evans 		break;
7480cde2e34SJason Evans 	case MA_NOTOWNED:
749a10f4966SJake Burkholder 		if (mtx_owned(m))
7500cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
75119284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7520cde2e34SJason Evans 		break;
7530cde2e34SJason Evans 	default:
75456771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
7550cde2e34SJason Evans 	}
7560cde2e34SJason Evans }
7570cde2e34SJason Evans #endif
7580cde2e34SJason Evans 
7599ed346baSBosko Milekic /*
7609ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
76119284646SJohn Baldwin  *
76219284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
76319284646SJohn Baldwin  * maintained by the witness code.
7649ed346baSBosko Milekic  */
76536412d79SJohn Baldwin #ifdef MUTEX_DEBUG
76636412d79SJohn Baldwin 
7674d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
76836412d79SJohn Baldwin 
76919284646SJohn Baldwin void
77019284646SJohn Baldwin mtx_validate(struct mtx *m)
77136412d79SJohn Baldwin {
77236412d79SJohn Baldwin 
77336412d79SJohn Baldwin /*
774fa669ab7SPoul-Henning Kamp  * XXX: When kernacc() does not require Giant we can reenable this check
775fa669ab7SPoul-Henning Kamp  */
776fa669ab7SPoul-Henning Kamp #ifdef notyet
777fa669ab7SPoul-Henning Kamp /*
77836412d79SJohn Baldwin  * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
77936412d79SJohn Baldwin  * we can re-enable the kernacc() checks.
78036412d79SJohn Baldwin  */
78136412d79SJohn Baldwin #ifndef __alpha__
78276dcbd6fSBosko Milekic 	/*
78376dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
78476dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
78576dcbd6fSBosko Milekic 	 * requires Giant itself.
78676dcbd6fSBosko Milekic 	 */
787ab07087eSBosko Milekic 	if (!cold)
788ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
789ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
79019284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
79136412d79SJohn Baldwin #endif
792fa669ab7SPoul-Henning Kamp #endif
79336412d79SJohn Baldwin }
79436412d79SJohn Baldwin #endif
79536412d79SJohn Baldwin 
7969ed346baSBosko Milekic /*
797c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
798c27b5699SAndrew R. Reiter  */
799c27b5699SAndrew R. Reiter void
800c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
801c27b5699SAndrew R. Reiter {
802c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
803c27b5699SAndrew R. Reiter 
8040c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
805c27b5699SAndrew R. Reiter }
806c27b5699SAndrew R. Reiter 
807c27b5699SAndrew R. Reiter /*
8089ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
8090c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
8100c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
8110c88508aSJohn Baldwin  * witness.
8129ed346baSBosko Milekic  */
81336412d79SJohn Baldwin void
8140c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
81536412d79SJohn Baldwin {
81619284646SJohn Baldwin 	struct lock_object *lock;
8179ed346baSBosko Milekic 
81819284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
81975d468eeSJohn Baldwin 	    MTX_NOWITNESS | MTX_DUPOK)) == 0);
8209ed346baSBosko Milekic 
82136412d79SJohn Baldwin #ifdef MUTEX_DEBUG
8229ed346baSBosko Milekic 	/* Diagnostic and error correction */
82319284646SJohn Baldwin 	mtx_validate(m);
8246936206eSJohn Baldwin #endif
82536412d79SJohn Baldwin 
82619284646SJohn Baldwin 	lock = &m->mtx_object;
8277ada5876SJohn Baldwin 	KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
828b82af320SPoul-Henning Kamp 	    ("mutex \"%s\" %p already initialized", name, m));
8297ada5876SJohn Baldwin 	bzero(m, sizeof(*m));
83019284646SJohn Baldwin 	if (opts & MTX_SPIN)
83119284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_spin;
83219284646SJohn Baldwin 	else
83319284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_sleep;
8340c88508aSJohn Baldwin 	lock->lo_name = name;
8350c88508aSJohn Baldwin 	lock->lo_type = type != NULL ? type : name;
83619284646SJohn Baldwin 	if (opts & MTX_QUIET)
83719284646SJohn Baldwin 		lock->lo_flags = LO_QUIET;
83819284646SJohn Baldwin 	if (opts & MTX_RECURSE)
83919284646SJohn Baldwin 		lock->lo_flags |= LO_RECURSABLE;
84019284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
84119284646SJohn Baldwin 		lock->lo_flags |= LO_WITNESS;
842f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
843f22a4b62SJeff Roberson 		lock->lo_flags |= LO_DUPOK;
84419284646SJohn Baldwin 
84519284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
8469ed346baSBosko Milekic 
84719284646SJohn Baldwin 	LOCK_LOG_INIT(lock, opts);
848d1c1b841SJason Evans 
84919284646SJohn Baldwin 	WITNESS_INIT(lock);
85036412d79SJohn Baldwin }
85136412d79SJohn Baldwin 
8529ed346baSBosko Milekic /*
85319284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
85419284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
85519284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
85619284646SJohn Baldwin  * flags.
8579ed346baSBosko Milekic  */
85836412d79SJohn Baldwin void
85936412d79SJohn Baldwin mtx_destroy(struct mtx *m)
86036412d79SJohn Baldwin {
86136412d79SJohn Baldwin 
86219284646SJohn Baldwin 	LOCK_LOG_DESTROY(&m->mtx_object, 0);
8639ed346baSBosko Milekic 
86419284646SJohn Baldwin 	if (!mtx_owned(m))
86519284646SJohn Baldwin 		MPASS(mtx_unowned(m));
86619284646SJohn Baldwin 	else {
86708812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
8689ed346baSBosko Milekic 
86919284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
870c86b6ff5SJohn Baldwin 		WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
871c86b6ff5SJohn Baldwin 		    __LINE__);
87236412d79SJohn Baldwin 	}
8730384fff8SJason Evans 
87419284646SJohn Baldwin 	WITNESS_DESTROY(&m->mtx_object);
8750384fff8SJason Evans }
876d23f5958SMatthew Dillon 
877d23f5958SMatthew Dillon /*
878c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
879c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
880c53c013bSJohn Baldwin  * setup before this is called.
881c53c013bSJohn Baldwin  */
882c53c013bSJohn Baldwin void
883c53c013bSJohn Baldwin mutex_init(void)
884c53c013bSJohn Baldwin {
885c53c013bSJohn Baldwin 
886c53c013bSJohn Baldwin 	/* Setup thread0 so that mutexes work. */
887c53c013bSJohn Baldwin 	LIST_INIT(&thread0.td_contested);
888c53c013bSJohn Baldwin 
889961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
890961a7b24SJohn Baldwin 	init_turnstiles();
891961a7b24SJohn Baldwin 
892c53c013bSJohn Baldwin 	/*
893c53c013bSJohn Baldwin 	 * Initialize mutexes.
894c53c013bSJohn Baldwin 	 */
8950c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
8960c88508aSJohn Baldwin 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
8970c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
898c53c013bSJohn Baldwin 	mtx_lock(&Giant);
899c53c013bSJohn Baldwin }
900