xref: /freebsd/sys/kern/kern_mutex.c (revision eac097962f758ce4f18eccb35318b1c0cfe72e2a)
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"
41a5a96a19SJohn Baldwin 
420384fff8SJason Evans #include <sys/param.h>
436c35e809SDag-Erling Smørgrav #include <sys/systm.h>
4436412d79SJohn Baldwin #include <sys/bus.h>
4536412d79SJohn Baldwin #include <sys/kernel.h>
466c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
4719284646SJohn Baldwin #include <sys/lock.h>
48fb919e4dSMark Murray #include <sys/malloc.h>
4919284646SJohn Baldwin #include <sys/mutex.h>
500384fff8SJason Evans #include <sys/proc.h>
51c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
52b43179fbSJeff Roberson #include <sys/sched.h>
536c35e809SDag-Erling Smørgrav #include <sys/sbuf.h>
54a5a96a19SJohn Baldwin #include <sys/sysctl.h>
55961a7b24SJohn Baldwin #include <sys/turnstile.h>
5636412d79SJohn Baldwin #include <sys/vmmeter.h>
570384fff8SJason Evans 
5836412d79SJohn Baldwin #include <machine/atomic.h>
5936412d79SJohn Baldwin #include <machine/bus.h>
6036412d79SJohn Baldwin #include <machine/clock.h>
610384fff8SJason Evans #include <machine/cpu.h>
6236412d79SJohn Baldwin 
639c36c934SJohn Baldwin #include <ddb/ddb.h>
649c36c934SJohn Baldwin 
6536412d79SJohn Baldwin #include <vm/vm.h>
6636412d79SJohn Baldwin #include <vm/vm_extern.h>
6736412d79SJohn Baldwin 
680cde2e34SJason Evans /*
699ed346baSBosko Milekic  * Internal utility macros.
700cde2e34SJason Evans  */
719ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
720cde2e34SJason Evans 
739ed346baSBosko Milekic #define mtx_owner(m)	(mtx_unowned((m)) ? NULL \
74b40ce416SJulian Elischer 	: (struct thread *)((m)->mtx_lock & MTX_FLAGMASK))
759ed346baSBosko Milekic 
760cde2e34SJason Evans /*
7719284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
780cde2e34SJason Evans  */
7919284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
8019284646SJohn Baldwin 	"sleep mutex",
8119284646SJohn Baldwin 	LC_SLEEPLOCK | LC_RECURSABLE
8219284646SJohn Baldwin };
8319284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
8419284646SJohn Baldwin 	"spin mutex",
8519284646SJohn Baldwin 	LC_SPINLOCK | LC_RECURSABLE
868484de75SJohn Baldwin };
878484de75SJohn Baldwin 
889ed346baSBosko Milekic /*
89c53c013bSJohn Baldwin  * System-wide mutexes
90c53c013bSJohn Baldwin  */
91c53c013bSJohn Baldwin struct mtx sched_lock;
92c53c013bSJohn Baldwin struct mtx Giant;
93c53c013bSJohn Baldwin 
946c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
956c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging");
966c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling");
976c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0;
986c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW,
996c35e809SDag-Erling Smørgrav     &mutex_prof_enable, 0, "Enable tracing of mutex holdtime");
1006c35e809SDag-Erling Smørgrav 
1016c35e809SDag-Erling Smørgrav struct mutex_prof {
1026c35e809SDag-Erling Smørgrav 	const char	*name;
1036c35e809SDag-Erling Smørgrav 	const char	*file;
1046c35e809SDag-Erling Smørgrav 	int		line;
105ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_max;
106ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_tot;
107ecf031c9SDag-Erling Smørgrav 	uintmax_t	cnt_cur;
108e6330704SDag-Erling Smørgrav 	struct mutex_prof *next;
1096c35e809SDag-Erling Smørgrav };
1106c35e809SDag-Erling Smørgrav 
1116c35e809SDag-Erling Smørgrav /*
1126c35e809SDag-Erling Smørgrav  * mprof_buf is a static pool of profiling records to avoid possible
1136c35e809SDag-Erling Smørgrav  * reentrance of the memory allocation functions.
1146c35e809SDag-Erling Smørgrav  *
1156c35e809SDag-Erling Smørgrav  * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE.
1166c35e809SDag-Erling Smørgrav  */
117e6330704SDag-Erling Smørgrav #define	NUM_MPROF_BUFFERS	1000
1186c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS];
1196c35e809SDag-Erling Smørgrav static int first_free_mprof_buf;
120e6330704SDag-Erling Smørgrav #define	MPROF_HASH_SIZE		1009
1216c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE];
1220bd5f797SMike Makonnen /* SWAG: sbuf size = avg stat. line size * number of locks */
1230bd5f797SMike Makonnen #define MPROF_SBUF_SIZE		256 * 400
1246c35e809SDag-Erling Smørgrav 
1256c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions;
1266c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD,
1276c35e809SDag-Erling Smørgrav     &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded");
1286c35e809SDag-Erling Smørgrav static int mutex_prof_records;
1296c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD,
1306c35e809SDag-Erling Smørgrav     &mutex_prof_records, 0, "Number of profiling records");
1316c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS;
1326c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD,
1336c35e809SDag-Erling Smørgrav     &mutex_prof_maxrecords, 0, "Maximum number of profiling records");
1346c35e809SDag-Erling Smørgrav static int mutex_prof_rejected;
1356c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD,
1366c35e809SDag-Erling Smørgrav     &mutex_prof_rejected, 0, "Number of rejected profiling records");
1376c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE;
1386c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD,
1396c35e809SDag-Erling Smørgrav     &mutex_prof_hashsize, 0, "Hash size");
1406c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0;
1416c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD,
1426c35e809SDag-Erling Smørgrav     &mutex_prof_collisions, 0, "Number of hash collisions");
1436c35e809SDag-Erling Smørgrav 
1446c35e809SDag-Erling Smørgrav /*
1456c35e809SDag-Erling Smørgrav  * mprof_mtx protects the profiling buffers and the hash.
1466c35e809SDag-Erling Smørgrav  */
1476c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx;
148e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET);
1496c35e809SDag-Erling Smørgrav 
150b784ffe9SDag-Erling Smørgrav static u_int64_t
151b784ffe9SDag-Erling Smørgrav nanoseconds(void)
152b784ffe9SDag-Erling Smørgrav {
153b784ffe9SDag-Erling Smørgrav 	struct timespec tv;
154b784ffe9SDag-Erling Smørgrav 
155b784ffe9SDag-Erling Smørgrav 	nanotime(&tv);
156b784ffe9SDag-Erling Smørgrav 	return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec);
157b784ffe9SDag-Erling Smørgrav }
158b784ffe9SDag-Erling Smørgrav 
1596c35e809SDag-Erling Smørgrav static int
1606c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
1616c35e809SDag-Erling Smørgrav {
1626c35e809SDag-Erling Smørgrav 	struct sbuf *sb;
1636c35e809SDag-Erling Smørgrav 	int error, i;
1640bd5f797SMike Makonnen 	static int multiplier = 1;
1656c35e809SDag-Erling Smørgrav 
1666c35e809SDag-Erling Smørgrav 	if (first_free_mprof_buf == 0)
1676d036900SDag-Erling Smørgrav 		return (SYSCTL_OUT(req, "No locking recorded",
1686d036900SDag-Erling Smørgrav 		    sizeof("No locking recorded")));
1696c35e809SDag-Erling Smørgrav 
1700bd5f797SMike Makonnen retry_sbufops:
1710bd5f797SMike Makonnen 	sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN);
1726d036900SDag-Erling Smørgrav 	sbuf_printf(sb, "%6s %12s %11s %5s %s\n",
1736d036900SDag-Erling Smørgrav 	    "max", "total", "count", "avg", "name");
1746d036900SDag-Erling Smørgrav 	/*
1756d036900SDag-Erling Smørgrav 	 * XXX this spinlock seems to be by far the largest perpetrator
1766d036900SDag-Erling Smørgrav 	 * of spinlock latency (1.6 msec on an Athlon1600 was recorded
1776d036900SDag-Erling Smørgrav 	 * even before I pessimized it further by moving the average
1786d036900SDag-Erling Smørgrav 	 * computation here).
1796d036900SDag-Erling Smørgrav 	 */
1806c35e809SDag-Erling Smørgrav 	mtx_lock_spin(&mprof_mtx);
1810bd5f797SMike Makonnen 	for (i = 0; i < first_free_mprof_buf; ++i) {
1826d036900SDag-Erling Smørgrav 		sbuf_printf(sb, "%6ju %12ju %11ju %5ju %s:%d (%s)\n",
183ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_max / 1000,
184ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_tot / 1000,
185ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_cur,
186ecf031c9SDag-Erling Smørgrav 		    mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 :
187ecf031c9SDag-Erling Smørgrav 			mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000),
1886c35e809SDag-Erling Smørgrav 		    mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name);
1890bd5f797SMike Makonnen 		if (sbuf_overflowed(sb)) {
1900bd5f797SMike Makonnen 			mtx_unlock_spin(&mprof_mtx);
1910bd5f797SMike Makonnen 			sbuf_delete(sb);
1920bd5f797SMike Makonnen 			multiplier++;
1930bd5f797SMike Makonnen 			goto retry_sbufops;
1940bd5f797SMike Makonnen 		}
1950bd5f797SMike Makonnen 	}
1966c35e809SDag-Erling Smørgrav 	mtx_unlock_spin(&mprof_mtx);
1976c35e809SDag-Erling Smørgrav 	sbuf_finish(sb);
1986c35e809SDag-Erling Smørgrav 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
1996c35e809SDag-Erling Smørgrav 	sbuf_delete(sb);
2006c35e809SDag-Erling Smørgrav 	return (error);
2016c35e809SDag-Erling Smørgrav }
2026c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD,
2036c35e809SDag-Erling Smørgrav     NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics");
2046c35e809SDag-Erling Smørgrav #endif
2056c35e809SDag-Erling Smørgrav 
2060cde2e34SJason Evans /*
2076283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2086283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2096283b7d0SJohn Baldwin  */
2106283b7d0SJohn Baldwin void
2116283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
2126283b7d0SJohn Baldwin {
2136283b7d0SJohn Baldwin 
214dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
2150d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
2160d975d63SJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
2170d975d63SJohn Baldwin 	    file, line));
218dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
219dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
220dde96c99SJohn Baldwin 	    line);
221dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
2226c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
2236c35e809SDag-Erling Smørgrav 	/* don't reset the timer when/if recursing */
224b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime == 0) {
225b61860adSDag-Erling Smørgrav 		m->mtx_filename = file;
226b61860adSDag-Erling Smørgrav 		m->mtx_lineno = line;
227b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0;
2286c35e809SDag-Erling Smørgrav 		++mutex_prof_acquisitions;
2296c35e809SDag-Erling Smørgrav 	}
2306c35e809SDag-Erling Smørgrav #endif
2316283b7d0SJohn Baldwin }
2326283b7d0SJohn Baldwin 
2336283b7d0SJohn Baldwin void
2346283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
2356283b7d0SJohn Baldwin {
2366283b7d0SJohn Baldwin 
237dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
2380d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
2390d975d63SJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
2400d975d63SJohn Baldwin 	    file, line));
2410d975d63SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
2420d975d63SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
2430d975d63SJohn Baldwin 	    line);
24421377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
2456c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
246b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime != 0) {
2476c35e809SDag-Erling Smørgrav 		static const char *unknown = "(unknown)";
2486c35e809SDag-Erling Smørgrav 		struct mutex_prof *mpp;
249b784ffe9SDag-Erling Smørgrav 		u_int64_t acqtime, now;
2506c35e809SDag-Erling Smørgrav 		const char *p, *q;
251e6330704SDag-Erling Smørgrav 		volatile u_int hash;
2526c35e809SDag-Erling Smørgrav 
253b784ffe9SDag-Erling Smørgrav 		now = nanoseconds();
254b61860adSDag-Erling Smørgrav 		acqtime = m->mtx_acqtime;
255b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = 0;
256b784ffe9SDag-Erling Smørgrav 		if (now <= acqtime)
2576c35e809SDag-Erling Smørgrav 			goto out;
2580bd5f797SMike Makonnen 		for (p = m->mtx_filename;
2590bd5f797SMike Makonnen 		    p != NULL && strncmp(p, "../", 3) == 0; p += 3)
2606c35e809SDag-Erling Smørgrav 			/* nothing */ ;
2616c35e809SDag-Erling Smørgrav 		if (p == NULL || *p == '\0')
2626c35e809SDag-Erling Smørgrav 			p = unknown;
263b61860adSDag-Erling Smørgrav 		for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q)
2646c35e809SDag-Erling Smørgrav 			hash = (hash * 2 + *q) % MPROF_HASH_SIZE;
2656c35e809SDag-Erling Smørgrav 		mtx_lock_spin(&mprof_mtx);
266e6330704SDag-Erling Smørgrav 		for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next)
267b61860adSDag-Erling Smørgrav 			if (mpp->line == m->mtx_lineno &&
268b61860adSDag-Erling Smørgrav 			    strcmp(mpp->file, p) == 0)
2696c35e809SDag-Erling Smørgrav 				break;
2706c35e809SDag-Erling Smørgrav 		if (mpp == NULL) {
2716c35e809SDag-Erling Smørgrav 			/* Just exit if we cannot get a trace buffer */
2726c35e809SDag-Erling Smørgrav 			if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) {
2736c35e809SDag-Erling Smørgrav 				++mutex_prof_rejected;
2746c35e809SDag-Erling Smørgrav 				goto unlock;
2756c35e809SDag-Erling Smørgrav 			}
2766c35e809SDag-Erling Smørgrav 			mpp = &mprof_buf[first_free_mprof_buf++];
2776c35e809SDag-Erling Smørgrav 			mpp->name = mtx_name(m);
2786c35e809SDag-Erling Smørgrav 			mpp->file = p;
279b61860adSDag-Erling Smørgrav 			mpp->line = m->mtx_lineno;
280e6330704SDag-Erling Smørgrav 			mpp->next = mprof_hash[hash];
281e6330704SDag-Erling Smørgrav 			if (mprof_hash[hash] != NULL)
282e6330704SDag-Erling Smørgrav 				++mutex_prof_collisions;
2836c35e809SDag-Erling Smørgrav 			mprof_hash[hash] = mpp;
284e6330704SDag-Erling Smørgrav 			++mutex_prof_records;
2856c35e809SDag-Erling Smørgrav 		}
2866c35e809SDag-Erling Smørgrav 		/*
2876c35e809SDag-Erling Smørgrav 		 * Record if the mutex has been held longer now than ever
2886d036900SDag-Erling Smørgrav 		 * before.
2896c35e809SDag-Erling Smørgrav 		 */
290ecf031c9SDag-Erling Smørgrav 		if (now - acqtime > mpp->cnt_max)
291ecf031c9SDag-Erling Smørgrav 			mpp->cnt_max = now - acqtime;
292ecf031c9SDag-Erling Smørgrav 		mpp->cnt_tot += now - acqtime;
293ecf031c9SDag-Erling Smørgrav 		mpp->cnt_cur++;
2946c35e809SDag-Erling Smørgrav unlock:
2956c35e809SDag-Erling Smørgrav 		mtx_unlock_spin(&mprof_mtx);
2966c35e809SDag-Erling Smørgrav 	}
2976c35e809SDag-Erling Smørgrav out:
2986c35e809SDag-Erling Smørgrav #endif
299dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
3006283b7d0SJohn Baldwin }
3016283b7d0SJohn Baldwin 
3026283b7d0SJohn Baldwin void
3036283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3046283b7d0SJohn Baldwin {
3056283b7d0SJohn Baldwin 
306dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3070d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
3080d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
3090d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
310ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1
311dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
312e8fdcfb5SJohn Baldwin #else
313e8fdcfb5SJohn Baldwin 	critical_enter();
314e8fdcfb5SJohn Baldwin #endif
315dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
316dde96c99SJohn Baldwin 	    line);
317dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3186283b7d0SJohn Baldwin }
3196283b7d0SJohn Baldwin 
3206283b7d0SJohn Baldwin void
3216283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3226283b7d0SJohn Baldwin {
3236283b7d0SJohn Baldwin 
324dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3250d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
3260d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
3270d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
328dde96c99SJohn Baldwin 	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
329dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
330dde96c99SJohn Baldwin 	    line);
3310d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
332ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1
333dde96c99SJohn Baldwin 	_rel_spin_lock(m);
334e8fdcfb5SJohn Baldwin #else
335e8fdcfb5SJohn Baldwin 	critical_exit();
336e8fdcfb5SJohn Baldwin #endif
3376283b7d0SJohn Baldwin }
3386283b7d0SJohn Baldwin 
3396283b7d0SJohn Baldwin /*
3409ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
341eac09796SJohn Baldwin  * Tries to acquire lock `m.'  If this function is called on a mutex that
342eac09796SJohn Baldwin  * is already owned, it will recursively acquire the lock.
3430cde2e34SJason Evans  */
3440cde2e34SJason Evans int
3459ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
3460cde2e34SJason Evans {
3470cde2e34SJason Evans 	int rval;
3480cde2e34SJason Evans 
349b40ce416SJulian Elischer 	MPASS(curthread != NULL);
3509ed346baSBosko Milekic 
351eac09796SJohn Baldwin 	if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) {
352eac09796SJohn Baldwin 		m->mtx_recurse++;
353eac09796SJohn Baldwin 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
354eac09796SJohn Baldwin 		rval = 1;
355eac09796SJohn Baldwin 	} else
356b40ce416SJulian Elischer 		rval = _obtain_lock(m, curthread);
3579ed346baSBosko Milekic 
35819284646SJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
3596b869595SJohn Baldwin 	if (rval)
3602d96f0b1SJohn Baldwin 		WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
3612d96f0b1SJohn Baldwin 		    file, line);
3629ed346baSBosko Milekic 
36319284646SJohn Baldwin 	return (rval);
3640cde2e34SJason Evans }
3650cde2e34SJason Evans 
3660cde2e34SJason Evans /*
3679ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
3689ed346baSBosko Milekic  *
3699ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
3709ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
3710cde2e34SJason Evans  */
3720cde2e34SJason Evans void
3739ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line)
37436412d79SJohn Baldwin {
375961a7b24SJohn Baldwin 	struct turnstile *ts;
376b40ce416SJulian Elischer 	struct thread *td = curthread;
3772498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
3782498cf8cSJohn Baldwin 	struct thread *owner;
3792498cf8cSJohn Baldwin #endif
3805fa8dd90SJohn Baldwin 	uintptr_t v;
38102bd1bcdSIan Dowse #ifdef KTR
38202bd1bcdSIan Dowse 	int cont_logged = 0;
38302bd1bcdSIan Dowse #endif
38436412d79SJohn Baldwin 
3855fa8dd90SJohn Baldwin 	if (mtx_owned(m)) {
386eac09796SJohn Baldwin 		KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0,
387eac09796SJohn Baldwin 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
388eac09796SJohn Baldwin 		    m->mtx_object.lo_name, file, line));
38936412d79SJohn Baldwin 		m->mtx_recurse++;
39008812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
39119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
3925746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
39336412d79SJohn Baldwin 		return;
39436412d79SJohn Baldwin 	}
3959ed346baSBosko Milekic 
39619284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
39715ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
39815ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
39919284646SJohn Baldwin 		    m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
4001bd0eefbSJohn Baldwin 
401b40ce416SJulian Elischer 	while (!_obtain_lock(m, td)) {
40236412d79SJohn Baldwin 
403961a7b24SJohn Baldwin 		ts = turnstile_lookup(&m->mtx_object);
4045fa8dd90SJohn Baldwin 		v = m->mtx_lock;
4055fa8dd90SJohn Baldwin 
40636412d79SJohn Baldwin 		/*
4079ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
408961a7b24SJohn Baldwin 		 * the turnstile chain lock.
40936412d79SJohn Baldwin 		 */
4105fa8dd90SJohn Baldwin 		if (v == MTX_UNOWNED) {
411961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
412703fc290SJohn Baldwin #ifdef __i386__
4136b8c6989SJohn Baldwin 			ia32_pause();
414703fc290SJohn Baldwin #endif
41536412d79SJohn Baldwin 			continue;
41636412d79SJohn Baldwin 		}
4179ed346baSBosko Milekic 
41836412d79SJohn Baldwin 		/*
4199ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
420f7ee1590SJohn Baldwin 		 * there are other threads blocked on it.  Grab ownership of
421f7ee1590SJohn Baldwin 		 * it and propagate its priority to the current thread if
422f7ee1590SJohn Baldwin 		 * necessary.
42336412d79SJohn Baldwin 		 */
42436412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
425961a7b24SJohn Baldwin 			MPASS(ts != NULL);
426b40ce416SJulian Elischer 			m->mtx_lock = (uintptr_t)td | MTX_CONTESTED;
427961a7b24SJohn Baldwin 			turnstile_claim(ts);
42836412d79SJohn Baldwin 			return;
42936412d79SJohn Baldwin 		}
4309ed346baSBosko Milekic 
43136412d79SJohn Baldwin 		/*
4329ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
4339ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
4349ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
43536412d79SJohn Baldwin 		 */
43636412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
43736412d79SJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, (void *)v,
43836412d79SJohn Baldwin 			(void *)(v | MTX_CONTESTED))) {
439961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
440703fc290SJohn Baldwin #ifdef __i386__
4416b8c6989SJohn Baldwin 			ia32_pause();
442703fc290SJohn Baldwin #endif
44336412d79SJohn Baldwin 			continue;
44436412d79SJohn Baldwin 		}
44536412d79SJohn Baldwin 
4462498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
4472498cf8cSJohn Baldwin 		/*
4482498cf8cSJohn Baldwin 		 * If the current owner of the lock is executing on another
4492498cf8cSJohn Baldwin 		 * CPU, spin instead of blocking.
4502498cf8cSJohn Baldwin 		 */
4512498cf8cSJohn Baldwin 		owner = (struct thread *)(v & MTX_FLAGMASK);
45227dad03cSJohn Baldwin 		if (m != &Giant && TD_IS_RUNNING(owner)) {
453961a7b24SJohn Baldwin 			turnstile_release(&m->mtx_object);
45427dad03cSJohn Baldwin 			while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
455703fc290SJohn Baldwin #ifdef __i386__
4566b8c6989SJohn Baldwin 				ia32_pause();
457703fc290SJohn Baldwin #endif
4587fcca609SJohn Baldwin 			}
4592498cf8cSJohn Baldwin 			continue;
4602498cf8cSJohn Baldwin 		}
4612498cf8cSJohn Baldwin #endif	/* SMP && ADAPTIVE_MUTEXES */
4622498cf8cSJohn Baldwin 
4639ed346baSBosko Milekic 		/*
4647feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
4659ed346baSBosko Milekic 		 */
46636412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
46736412d79SJohn Baldwin 
46802bd1bcdSIan Dowse #ifdef KTR
46902bd1bcdSIan Dowse 		if (!cont_logged) {
47002bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
47102bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
47202bd1bcdSIan Dowse 			    td, file, line, m->mtx_object.lo_name,
47302bd1bcdSIan Dowse 			    WITNESS_FILE(&m->mtx_object),
47402bd1bcdSIan Dowse 			    WITNESS_LINE(&m->mtx_object));
47502bd1bcdSIan Dowse 			cont_logged = 1;
47602bd1bcdSIan Dowse 		}
47702bd1bcdSIan Dowse #endif
47836412d79SJohn Baldwin 
4799ed346baSBosko Milekic 		/*
480961a7b24SJohn Baldwin 		 * Block on the turnstile.
4819ed346baSBosko Milekic 		 */
482961a7b24SJohn Baldwin 		turnstile_wait(ts, &m->mtx_object, mtx_owner(m));
48336412d79SJohn Baldwin 	}
4849ed346baSBosko Milekic 
48502bd1bcdSIan Dowse #ifdef KTR
48602bd1bcdSIan Dowse 	if (cont_logged) {
48702bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
48802bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
48902bd1bcdSIan Dowse 		    m->mtx_object.lo_name, td, file, line);
49002bd1bcdSIan Dowse 	}
49102bd1bcdSIan Dowse #endif
49236412d79SJohn Baldwin 	return;
4939ed346baSBosko Milekic }
4949ed346baSBosko Milekic 
4959ed346baSBosko Milekic /*
4969ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
4979ed346baSBosko Milekic  *
4989ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
4999ed346baSBosko Milekic  * is handled inline.
5009ed346baSBosko Milekic  */
5019ed346baSBosko Milekic void
5027e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line)
50336412d79SJohn Baldwin {
50436412d79SJohn Baldwin 	int i = 0;
50536412d79SJohn Baldwin 
50619284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
5075746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
5089ed346baSBosko Milekic 
50936412d79SJohn Baldwin 	for (;;) {
510b40ce416SJulian Elischer 		if (_obtain_lock(m, curthread))
51136412d79SJohn Baldwin 			break;
5129ed346baSBosko Milekic 
5137141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
5147e1f6dfeSJohn Baldwin 		critical_exit();
51536412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
516703fc290SJohn Baldwin 			if (i++ < 10000000) {
517703fc290SJohn Baldwin #ifdef __i386__
5186b8c6989SJohn Baldwin 				ia32_pause();
519703fc290SJohn Baldwin #endif
52036412d79SJohn Baldwin 				continue;
521703fc290SJohn Baldwin 			}
5220e54ddadSJohn Baldwin 			if (i < 60000000)
52336412d79SJohn Baldwin 				DELAY(1);
52436412d79SJohn Baldwin #ifdef DDB
52541109518SJohn Baldwin 			else if (!db_active) {
52636412d79SJohn Baldwin #else
52741109518SJohn Baldwin 			else {
52836412d79SJohn Baldwin #endif
52941109518SJohn Baldwin 				printf("spin lock %s held by %p for > 5 seconds\n",
53019284646SJohn Baldwin 				    m->mtx_object.lo_name, (void *)m->mtx_lock);
53141109518SJohn Baldwin #ifdef WITNESS
53241109518SJohn Baldwin 				witness_display_spinlock(&m->mtx_object,
53341109518SJohn Baldwin 				    mtx_owner(m));
53441109518SJohn Baldwin #endif
53541109518SJohn Baldwin 				panic("spin lock held too long");
53641109518SJohn Baldwin 			}
537703fc290SJohn Baldwin #ifdef __i386__
5386b8c6989SJohn Baldwin 			ia32_pause();
539703fc290SJohn Baldwin #endif
54036412d79SJohn Baldwin 		}
5417e1f6dfeSJohn Baldwin 		critical_enter();
54236412d79SJohn Baldwin 	}
54336412d79SJohn Baldwin 
54419284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
5459ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
5469ed346baSBosko Milekic 
54736412d79SJohn Baldwin 	return;
54836412d79SJohn Baldwin }
54936412d79SJohn Baldwin 
5509ed346baSBosko Milekic /*
5519ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
5529ed346baSBosko Milekic  *
5539ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
5549ed346baSBosko Milekic  * need to wake up a blocked thread).
5559ed346baSBosko Milekic  */
55636412d79SJohn Baldwin void
5579ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
55836412d79SJohn Baldwin {
559961a7b24SJohn Baldwin 	struct turnstile *ts;
560b40ce416SJulian Elischer 	struct thread *td, *td1;
5619ed346baSBosko Milekic 
56208812b39SBosko Milekic 	if (mtx_recursed(m)) {
56336412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
56408812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
56519284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
5669ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
56736412d79SJohn Baldwin 		return;
56836412d79SJohn Baldwin 	}
5699ed346baSBosko Milekic 
570961a7b24SJohn Baldwin 	ts = turnstile_lookup(&m->mtx_object);
57119284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
5729ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
5739ed346baSBosko Milekic 
5742498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
575961a7b24SJohn Baldwin 	if (ts == NULL) {
5762498cf8cSJohn Baldwin 		_release_lock_quick(m);
5772498cf8cSJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
5782498cf8cSJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
579961a7b24SJohn Baldwin 		turnstile_release(&m->mtx_object);
5802498cf8cSJohn Baldwin 		return;
5812498cf8cSJohn Baldwin 	}
582961a7b24SJohn Baldwin #else
583961a7b24SJohn Baldwin 	MPASS(ts != NULL);
5842498cf8cSJohn Baldwin #endif
585961a7b24SJohn Baldwin 	/* XXX */
586961a7b24SJohn Baldwin 	td1 = turnstile_head(ts);
587961a7b24SJohn Baldwin 	if (turnstile_signal(ts)) {
58836412d79SJohn Baldwin 		_release_lock_quick(m);
58919284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
5909ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
591961a7b24SJohn Baldwin 	} else {
592f7ee1590SJohn Baldwin 		m->mtx_lock = MTX_CONTESTED;
59319284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
594961a7b24SJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
595961a7b24SJohn Baldwin 			    m);
596e0817317SJulian Elischer 	}
597961a7b24SJohn Baldwin 	turnstile_unpend(ts);
5989ed346baSBosko Milekic 
599961a7b24SJohn Baldwin 	/*
600961a7b24SJohn Baldwin 	 * XXX: This is just a hack until preemption is done.  However,
601961a7b24SJohn Baldwin 	 * once preemption is done we need to either wrap the
602961a7b24SJohn Baldwin 	 * turnstile_signal() and release of the actual lock in an
603961a7b24SJohn Baldwin 	 * extra critical section or change the preemption code to
604961a7b24SJohn Baldwin 	 * always just set a flag and never do instant-preempts.
605961a7b24SJohn Baldwin 	 */
606961a7b24SJohn Baldwin 	td = curthread;
607961a7b24SJohn Baldwin 	if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
608961a7b24SJohn Baldwin 		return;
609961a7b24SJohn Baldwin 	mtx_lock_spin(&sched_lock);
610961a7b24SJohn Baldwin 	if (!TD_IS_RUNNING(td1)) {
61136412d79SJohn Baldwin #ifdef notyet
612b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
613b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
61436412d79SJohn Baldwin 
61536412d79SJohn Baldwin 			if (it->it_interrupted) {
61619284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
61736412d79SJohn Baldwin 					CTR2(KTR_LOCK,
61815ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
61936412d79SJohn Baldwin 					    it, it->it_interrupted);
62036412d79SJohn Baldwin 				intr_thd_fixup(it);
62136412d79SJohn Baldwin 			}
62236412d79SJohn Baldwin 		}
62336412d79SJohn Baldwin #endif
62419284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
625562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
6269ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
6279ed346baSBosko Milekic 			    (void *)m->mtx_lock);
6289ed346baSBosko Milekic 
629b40ce416SJulian Elischer 		td->td_proc->p_stats->p_ru.ru_nivcsw++;
63036412d79SJohn Baldwin 		mi_switch();
63119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6329ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
63331271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
63436412d79SJohn Baldwin 	}
6359ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
6369ed346baSBosko Milekic 
6379ed346baSBosko Milekic 	return;
6389ed346baSBosko Milekic }
6399ed346baSBosko Milekic 
6409ed346baSBosko Milekic /*
6419ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
6429ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
6439ed346baSBosko Milekic  */
6449ed346baSBosko Milekic 
6459ed346baSBosko Milekic /*
64615ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
6479ed346baSBosko Milekic  */
6481103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
6490cde2e34SJason Evans void
65056771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
6510cde2e34SJason Evans {
6525cb0fbe4SJohn Baldwin 
6535cb0fbe4SJohn Baldwin 	if (panicstr != NULL)
6545cb0fbe4SJohn Baldwin 		return;
655a10f4966SJake Burkholder 	switch (what) {
6560cde2e34SJason Evans 	case MA_OWNED:
6570cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
6580cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
659a10f4966SJake Burkholder 		if (!mtx_owned(m))
6600cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
66119284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
662a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
663a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
6640cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
66519284646SJohn Baldwin 				    m->mtx_object.lo_name, file, line);
666a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
6670cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
66819284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
6690cde2e34SJason Evans 		}
6700cde2e34SJason Evans 		break;
6710cde2e34SJason Evans 	case MA_NOTOWNED:
672a10f4966SJake Burkholder 		if (mtx_owned(m))
6730cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
67419284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
6750cde2e34SJason Evans 		break;
6760cde2e34SJason Evans 	default:
67756771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
6780cde2e34SJason Evans 	}
6790cde2e34SJason Evans }
6800cde2e34SJason Evans #endif
6810cde2e34SJason Evans 
6829ed346baSBosko Milekic /*
6839ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
68419284646SJohn Baldwin  *
68519284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
68619284646SJohn Baldwin  * maintained by the witness code.
6879ed346baSBosko Milekic  */
68836412d79SJohn Baldwin #ifdef MUTEX_DEBUG
68936412d79SJohn Baldwin 
6904d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
69136412d79SJohn Baldwin 
69219284646SJohn Baldwin void
69319284646SJohn Baldwin mtx_validate(struct mtx *m)
69436412d79SJohn Baldwin {
69536412d79SJohn Baldwin 
69636412d79SJohn Baldwin /*
697fa669ab7SPoul-Henning Kamp  * XXX: When kernacc() does not require Giant we can reenable this check
698fa669ab7SPoul-Henning Kamp  */
699fa669ab7SPoul-Henning Kamp #ifdef notyet
700fa669ab7SPoul-Henning Kamp /*
70136412d79SJohn Baldwin  * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
70236412d79SJohn Baldwin  * we can re-enable the kernacc() checks.
70336412d79SJohn Baldwin  */
70436412d79SJohn Baldwin #ifndef __alpha__
70576dcbd6fSBosko Milekic 	/*
70676dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
70776dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
70876dcbd6fSBosko Milekic 	 * requires Giant itself.
70976dcbd6fSBosko Milekic 	 */
710ab07087eSBosko Milekic 	if (!cold)
711ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
712ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
71319284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
71436412d79SJohn Baldwin #endif
715fa669ab7SPoul-Henning Kamp #endif
71636412d79SJohn Baldwin }
71736412d79SJohn Baldwin #endif
71836412d79SJohn Baldwin 
7199ed346baSBosko Milekic /*
720c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
721c27b5699SAndrew R. Reiter  */
722c27b5699SAndrew R. Reiter void
723c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
724c27b5699SAndrew R. Reiter {
725c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
726c27b5699SAndrew R. Reiter 
7270c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
728c27b5699SAndrew R. Reiter }
729c27b5699SAndrew R. Reiter 
730c27b5699SAndrew R. Reiter /*
7319ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
7320c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
7330c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
7340c88508aSJohn Baldwin  * witness.
7359ed346baSBosko Milekic  */
73636412d79SJohn Baldwin void
7370c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
73836412d79SJohn Baldwin {
73919284646SJohn Baldwin 	struct lock_object *lock;
7409ed346baSBosko Milekic 
74119284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
74275d468eeSJohn Baldwin 	    MTX_NOWITNESS | MTX_DUPOK)) == 0);
7439ed346baSBosko Milekic 
74436412d79SJohn Baldwin #ifdef MUTEX_DEBUG
7459ed346baSBosko Milekic 	/* Diagnostic and error correction */
74619284646SJohn Baldwin 	mtx_validate(m);
7476936206eSJohn Baldwin #endif
74836412d79SJohn Baldwin 
74919284646SJohn Baldwin 	lock = &m->mtx_object;
7507ada5876SJohn Baldwin 	KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
751b82af320SPoul-Henning Kamp 	    ("mutex \"%s\" %p already initialized", name, m));
7527ada5876SJohn Baldwin 	bzero(m, sizeof(*m));
75319284646SJohn Baldwin 	if (opts & MTX_SPIN)
75419284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_spin;
75519284646SJohn Baldwin 	else
75619284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_sleep;
7570c88508aSJohn Baldwin 	lock->lo_name = name;
7580c88508aSJohn Baldwin 	lock->lo_type = type != NULL ? type : name;
75919284646SJohn Baldwin 	if (opts & MTX_QUIET)
76019284646SJohn Baldwin 		lock->lo_flags = LO_QUIET;
76119284646SJohn Baldwin 	if (opts & MTX_RECURSE)
76219284646SJohn Baldwin 		lock->lo_flags |= LO_RECURSABLE;
76319284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
76419284646SJohn Baldwin 		lock->lo_flags |= LO_WITNESS;
765f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
766f22a4b62SJeff Roberson 		lock->lo_flags |= LO_DUPOK;
76719284646SJohn Baldwin 
76819284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
7699ed346baSBosko Milekic 
77019284646SJohn Baldwin 	LOCK_LOG_INIT(lock, opts);
771d1c1b841SJason Evans 
77219284646SJohn Baldwin 	WITNESS_INIT(lock);
77336412d79SJohn Baldwin }
77436412d79SJohn Baldwin 
7759ed346baSBosko Milekic /*
77619284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
77719284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
77819284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
77919284646SJohn Baldwin  * flags.
7809ed346baSBosko Milekic  */
78136412d79SJohn Baldwin void
78236412d79SJohn Baldwin mtx_destroy(struct mtx *m)
78336412d79SJohn Baldwin {
78436412d79SJohn Baldwin 
78519284646SJohn Baldwin 	LOCK_LOG_DESTROY(&m->mtx_object, 0);
7869ed346baSBosko Milekic 
78719284646SJohn Baldwin 	if (!mtx_owned(m))
78819284646SJohn Baldwin 		MPASS(mtx_unowned(m));
78919284646SJohn Baldwin 	else {
79008812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
7919ed346baSBosko Milekic 
79219284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
793c86b6ff5SJohn Baldwin 		WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
794c86b6ff5SJohn Baldwin 		    __LINE__);
79536412d79SJohn Baldwin 	}
7960384fff8SJason Evans 
79719284646SJohn Baldwin 	WITNESS_DESTROY(&m->mtx_object);
7980384fff8SJason Evans }
799d23f5958SMatthew Dillon 
800d23f5958SMatthew Dillon /*
801c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
802c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
803c53c013bSJohn Baldwin  * setup before this is called.
804c53c013bSJohn Baldwin  */
805c53c013bSJohn Baldwin void
806c53c013bSJohn Baldwin mutex_init(void)
807c53c013bSJohn Baldwin {
808c53c013bSJohn Baldwin 
809c53c013bSJohn Baldwin 	/* Setup thread0 so that mutexes work. */
810c53c013bSJohn Baldwin 	LIST_INIT(&thread0.td_contested);
811c53c013bSJohn Baldwin 
812961a7b24SJohn Baldwin 	/* Setup turnstiles so that sleep mutexes work. */
813961a7b24SJohn Baldwin 	init_turnstiles();
814961a7b24SJohn Baldwin 
815c53c013bSJohn Baldwin 	/*
816c53c013bSJohn Baldwin 	 * Initialize mutexes.
817c53c013bSJohn Baldwin 	 */
8180c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
8190c88508aSJohn Baldwin 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
8200c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
821c53c013bSJohn Baldwin 	mtx_lock(&Giant);
822c53c013bSJohn Baldwin }
823