xref: /freebsd/sys/kern/subr_turnstile.c (revision 551cf4e1503e868324f01a6c98262a8e4c7b8cdb)
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  * $FreeBSD$
310384fff8SJason Evans  */
320384fff8SJason Evans 
330384fff8SJason Evans /*
34ba48b69aSJohn Baldwin  * Machine independent bits of mutex implementation.
350384fff8SJason Evans  */
360384fff8SJason Evans 
372498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h"
389c36c934SJohn Baldwin #include "opt_ddb.h"
39a5a96a19SJohn Baldwin 
400384fff8SJason Evans #include <sys/param.h>
416c35e809SDag-Erling Smørgrav #include <sys/systm.h>
4236412d79SJohn Baldwin #include <sys/bus.h>
4336412d79SJohn Baldwin #include <sys/kernel.h>
446c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
4519284646SJohn Baldwin #include <sys/lock.h>
46fb919e4dSMark Murray #include <sys/malloc.h>
4719284646SJohn Baldwin #include <sys/mutex.h>
480384fff8SJason Evans #include <sys/proc.h>
49c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
506c35e809SDag-Erling Smørgrav #include <sys/sbuf.h>
51db586c8bSDag-Erling Smørgrav #include <sys/stdint.h>
52a5a96a19SJohn Baldwin #include <sys/sysctl.h>
5336412d79SJohn Baldwin #include <sys/vmmeter.h>
540384fff8SJason Evans 
5536412d79SJohn Baldwin #include <machine/atomic.h>
5636412d79SJohn Baldwin #include <machine/bus.h>
5736412d79SJohn Baldwin #include <machine/clock.h>
580384fff8SJason Evans #include <machine/cpu.h>
5936412d79SJohn Baldwin 
609c36c934SJohn Baldwin #include <ddb/ddb.h>
619c36c934SJohn Baldwin 
6236412d79SJohn Baldwin #include <vm/vm.h>
6336412d79SJohn Baldwin #include <vm/vm_extern.h>
6436412d79SJohn Baldwin 
650cde2e34SJason Evans /*
669ed346baSBosko Milekic  * Internal utility macros.
670cde2e34SJason Evans  */
689ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
690cde2e34SJason Evans 
709ed346baSBosko Milekic #define mtx_owner(m)	(mtx_unowned((m)) ? NULL \
71b40ce416SJulian Elischer 	: (struct thread *)((m)->mtx_lock & MTX_FLAGMASK))
729ed346baSBosko Milekic 
736a95e08fSJohn Baldwin /* XXXKSE This test will change. */
746a95e08fSJohn Baldwin #define	thread_running(td)						\
755853d37dSJohn Baldwin 	((td)->td_kse != NULL && (td)->td_kse->ke_oncpu != NOCPU)
765853d37dSJohn Baldwin 
770cde2e34SJason Evans /*
7819284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
790cde2e34SJason Evans  */
8019284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
8119284646SJohn Baldwin 	"sleep mutex",
8219284646SJohn Baldwin 	LC_SLEEPLOCK | LC_RECURSABLE
8319284646SJohn Baldwin };
8419284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
8519284646SJohn Baldwin 	"spin mutex",
8619284646SJohn Baldwin 	LC_SPINLOCK | LC_RECURSABLE
878484de75SJohn Baldwin };
888484de75SJohn Baldwin 
899ed346baSBosko Milekic /*
90c53c013bSJohn Baldwin  * System-wide mutexes
91c53c013bSJohn Baldwin  */
92c53c013bSJohn Baldwin struct mtx sched_lock;
93c53c013bSJohn Baldwin struct mtx Giant;
94c53c013bSJohn Baldwin 
95c53c013bSJohn Baldwin /*
969ed346baSBosko Milekic  * Prototypes for non-exported routines.
979ed346baSBosko Milekic  */
98b40ce416SJulian Elischer static void	propagate_priority(struct thread *);
9936412d79SJohn Baldwin 
10036412d79SJohn Baldwin static void
101b40ce416SJulian Elischer propagate_priority(struct thread *td)
10236412d79SJohn Baldwin {
1032c100766SJulian Elischer 	int pri = td->td_priority;
104b40ce416SJulian Elischer 	struct mtx *m = td->td_blocked;
10536412d79SJohn Baldwin 
1061bd0eefbSJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
10736412d79SJohn Baldwin 	for (;;) {
108b40ce416SJulian Elischer 		struct thread *td1;
10936412d79SJohn Baldwin 
110b40ce416SJulian Elischer 		td = mtx_owner(m);
11136412d79SJohn Baldwin 
112b40ce416SJulian Elischer 		if (td == NULL) {
11336412d79SJohn Baldwin 			/*
11436412d79SJohn Baldwin 			 * This really isn't quite right. Really
115b40ce416SJulian Elischer 			 * ought to bump priority of thread that
11636412d79SJohn Baldwin 			 * next acquires the mutex.
11736412d79SJohn Baldwin 			 */
11836412d79SJohn Baldwin 			MPASS(m->mtx_lock == MTX_CONTESTED);
11936412d79SJohn Baldwin 			return;
12036412d79SJohn Baldwin 		}
1219ed346baSBosko Milekic 
122e602ba25SJulian Elischer 		MPASS(td->td_proc != NULL);
123b40ce416SJulian Elischer 		MPASS(td->td_proc->p_magic == P_MAGIC);
12471fad9fdSJulian Elischer 		KASSERT(!TD_IS_SLEEPING(td), ("sleeping thread owns a mutex"));
1252c100766SJulian Elischer 		if (td->td_priority <= pri) /* lower is higher priority */
12636412d79SJohn Baldwin 			return;
1271bd0eefbSJohn Baldwin 
1281bd0eefbSJohn Baldwin 
12936412d79SJohn Baldwin 		/*
13036412d79SJohn Baldwin 		 * If lock holder is actually running, just bump priority.
13136412d79SJohn Baldwin 		 */
13271fad9fdSJulian Elischer 		if (TD_IS_RUNNING(td)) {
133e602ba25SJulian Elischer 			td->td_priority = pri;
13436412d79SJohn Baldwin 			return;
13536412d79SJohn Baldwin 		}
136d5a08a60SJake Burkholder 
1371b43703bSJohn Baldwin #ifndef SMP
1381b43703bSJohn Baldwin 		/*
139b40ce416SJulian Elischer 		 * For UP, we check to see if td is curthread (this shouldn't
1401b43703bSJohn Baldwin 		 * ever happen however as it would mean we are in a deadlock.)
1411b43703bSJohn Baldwin 		 */
142b40ce416SJulian Elischer 		KASSERT(td != curthread, ("Deadlock detected"));
1431b43703bSJohn Baldwin #endif
1441b43703bSJohn Baldwin 
14536412d79SJohn Baldwin 		/*
146b40ce416SJulian Elischer 		 * If on run queue move to new run queue, and quit.
147b40ce416SJulian Elischer 		 * XXXKSE this gets a lot more complicated under threads
148b40ce416SJulian Elischer 		 * but try anyhow.
149e602ba25SJulian Elischer 		 * We should have a special call to do this more efficiently.
15036412d79SJohn Baldwin 		 */
15171fad9fdSJulian Elischer 		if (TD_ON_RUNQ(td)) {
152b40ce416SJulian Elischer 			MPASS(td->td_blocked == NULL);
153b40ce416SJulian Elischer 			remrunqueue(td);
154e602ba25SJulian Elischer 			td->td_priority = pri;
155b40ce416SJulian Elischer 			setrunqueue(td);
15636412d79SJohn Baldwin 			return;
15736412d79SJohn Baldwin 		}
158e602ba25SJulian Elischer 		/*
159e602ba25SJulian Elischer 		 * Adjust for any other cases.
160e602ba25SJulian Elischer 		 */
161e602ba25SJulian Elischer 		td->td_priority = pri;
16236412d79SJohn Baldwin 
16336412d79SJohn Baldwin 		/*
1641bd0eefbSJohn Baldwin 		 * If we aren't blocked on a mutex, we should be.
16536412d79SJohn Baldwin 		 */
166551cf4e1SJohn Baldwin 		KASSERT(TD_ON_LOCK(td), (
1671bd0eefbSJohn Baldwin 		    "process %d(%s):%d holds %s but isn't blocked on a mutex\n",
168e602ba25SJulian Elischer 		    td->td_proc->p_pid, td->td_proc->p_comm, td->td_state,
16919284646SJohn Baldwin 		    m->mtx_object.lo_name));
17036412d79SJohn Baldwin 
17136412d79SJohn Baldwin 		/*
172b40ce416SJulian Elischer 		 * Pick up the mutex that td is blocked on.
17336412d79SJohn Baldwin 		 */
174b40ce416SJulian Elischer 		m = td->td_blocked;
17536412d79SJohn Baldwin 		MPASS(m != NULL);
17636412d79SJohn Baldwin 
17736412d79SJohn Baldwin 		/*
178b40ce416SJulian Elischer 		 * Check if the thread needs to be moved up on
17936412d79SJohn Baldwin 		 * the blocked chain
18036412d79SJohn Baldwin 		 */
181b40ce416SJulian Elischer 		if (td == TAILQ_FIRST(&m->mtx_blocked)) {
1821bd0eefbSJohn Baldwin 			continue;
1831bd0eefbSJohn Baldwin 		}
1849ed346baSBosko Milekic 
185551cf4e1SJohn Baldwin 		td1 = TAILQ_PREV(td, threadqueue, td_lockq);
1862c100766SJulian Elischer 		if (td1->td_priority <= pri) {
18736412d79SJohn Baldwin 			continue;
18836412d79SJohn Baldwin 		}
18936412d79SJohn Baldwin 
19036412d79SJohn Baldwin 		/*
191b40ce416SJulian Elischer 		 * Remove thread from blocked chain and determine where
192b40ce416SJulian Elischer 		 * it should be moved up to.  Since we know that td1 has
193b40ce416SJulian Elischer 		 * a lower priority than td, we know that at least one
194b40ce416SJulian Elischer 		 * thread in the chain has a lower priority and that
195b40ce416SJulian Elischer 		 * td1 will thus not be NULL after the loop.
19636412d79SJohn Baldwin 		 */
197551cf4e1SJohn Baldwin 		TAILQ_REMOVE(&m->mtx_blocked, td, td_lockq);
198551cf4e1SJohn Baldwin 		TAILQ_FOREACH(td1, &m->mtx_blocked, td_lockq) {
199b40ce416SJulian Elischer 			MPASS(td1->td_proc->p_magic == P_MAGIC);
2002c100766SJulian Elischer 			if (td1->td_priority > pri)
20136412d79SJohn Baldwin 				break;
20236412d79SJohn Baldwin 		}
2039ed346baSBosko Milekic 
204b40ce416SJulian Elischer 		MPASS(td1 != NULL);
205551cf4e1SJohn Baldwin 		TAILQ_INSERT_BEFORE(td1, td, td_lockq);
20636412d79SJohn Baldwin 		CTR4(KTR_LOCK,
2078484de75SJohn Baldwin 		    "propagate_priority: p %p moved before %p on [%p] %s",
208b40ce416SJulian Elischer 		    td, td1, m, m->mtx_object.lo_name);
20936412d79SJohn Baldwin 	}
21036412d79SJohn Baldwin }
21136412d79SJohn Baldwin 
2126c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
2136c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging");
2146c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling");
2156c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0;
2166c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW,
2176c35e809SDag-Erling Smørgrav     &mutex_prof_enable, 0, "Enable tracing of mutex holdtime");
2186c35e809SDag-Erling Smørgrav 
2196c35e809SDag-Erling Smørgrav struct mutex_prof {
2206c35e809SDag-Erling Smørgrav 	const char *name;
2216c35e809SDag-Erling Smørgrav 	const char *file;
2226c35e809SDag-Erling Smørgrav 	int line;
2236c35e809SDag-Erling Smørgrav #define MPROF_MAX 0
2246c35e809SDag-Erling Smørgrav #define MPROF_TOT 1
2256c35e809SDag-Erling Smørgrav #define MPROF_CNT 2
2266c35e809SDag-Erling Smørgrav #define MPROF_AVG 3
227db586c8bSDag-Erling Smørgrav 	uintmax_t counter[4];
228e6330704SDag-Erling Smørgrav 	struct mutex_prof *next;
2296c35e809SDag-Erling Smørgrav };
2306c35e809SDag-Erling Smørgrav 
2316c35e809SDag-Erling Smørgrav /*
2326c35e809SDag-Erling Smørgrav  * mprof_buf is a static pool of profiling records to avoid possible
2336c35e809SDag-Erling Smørgrav  * reentrance of the memory allocation functions.
2346c35e809SDag-Erling Smørgrav  *
2356c35e809SDag-Erling Smørgrav  * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE.
2366c35e809SDag-Erling Smørgrav  */
237e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000
2386c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS];
2396c35e809SDag-Erling Smørgrav static int first_free_mprof_buf;
240e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009
2416c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE];
2426c35e809SDag-Erling Smørgrav 
2436c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions;
2446c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD,
2456c35e809SDag-Erling Smørgrav     &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded");
2466c35e809SDag-Erling Smørgrav static int mutex_prof_records;
2476c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD,
2486c35e809SDag-Erling Smørgrav     &mutex_prof_records, 0, "Number of profiling records");
2496c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS;
2506c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD,
2516c35e809SDag-Erling Smørgrav     &mutex_prof_maxrecords, 0, "Maximum number of profiling records");
2526c35e809SDag-Erling Smørgrav static int mutex_prof_rejected;
2536c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD,
2546c35e809SDag-Erling Smørgrav     &mutex_prof_rejected, 0, "Number of rejected profiling records");
2556c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE;
2566c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD,
2576c35e809SDag-Erling Smørgrav     &mutex_prof_hashsize, 0, "Hash size");
2586c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0;
2596c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD,
2606c35e809SDag-Erling Smørgrav     &mutex_prof_collisions, 0, "Number of hash collisions");
2616c35e809SDag-Erling Smørgrav 
2626c35e809SDag-Erling Smørgrav /*
2636c35e809SDag-Erling Smørgrav  * mprof_mtx protects the profiling buffers and the hash.
2646c35e809SDag-Erling Smørgrav  */
2656c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx;
266e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET);
2676c35e809SDag-Erling Smørgrav 
268b784ffe9SDag-Erling Smørgrav static u_int64_t
269b784ffe9SDag-Erling Smørgrav nanoseconds(void)
270b784ffe9SDag-Erling Smørgrav {
271b784ffe9SDag-Erling Smørgrav 	struct timespec tv;
272b784ffe9SDag-Erling Smørgrav 
273b784ffe9SDag-Erling Smørgrav 	nanotime(&tv);
274b784ffe9SDag-Erling Smørgrav 	return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec);
275b784ffe9SDag-Erling Smørgrav }
276b784ffe9SDag-Erling Smørgrav 
2776c35e809SDag-Erling Smørgrav static int
2786c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
2796c35e809SDag-Erling Smørgrav {
2806c35e809SDag-Erling Smørgrav 	struct sbuf *sb;
2816c35e809SDag-Erling Smørgrav 	int error, i;
2826c35e809SDag-Erling Smørgrav 
2836c35e809SDag-Erling Smørgrav 	if (first_free_mprof_buf == 0)
2846c35e809SDag-Erling Smørgrav 		return SYSCTL_OUT(req, "No locking recorded",
2856c35e809SDag-Erling Smørgrav 		    sizeof("No locking recorded"));
2866c35e809SDag-Erling Smørgrav 
2876c35e809SDag-Erling Smørgrav 	sb = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND);
2886c35e809SDag-Erling Smørgrav 	sbuf_printf(sb, "%12s %12s %12s %12s %s\n",
2896c35e809SDag-Erling Smørgrav 	    "max", "total", "count", "average", "name");
2906c35e809SDag-Erling Smørgrav 	mtx_lock_spin(&mprof_mtx);
2916c35e809SDag-Erling Smørgrav 	for (i = 0; i < first_free_mprof_buf; ++i)
292db586c8bSDag-Erling Smørgrav 		sbuf_printf(sb, "%12ju %12ju %12ju %12ju %s:%d (%s)\n",
293b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_MAX] / 1000,
294b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_TOT] / 1000,
295b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_CNT],
296b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_AVG] / 1000,
2976c35e809SDag-Erling Smørgrav 		    mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name);
2986c35e809SDag-Erling Smørgrav 	mtx_unlock_spin(&mprof_mtx);
2996c35e809SDag-Erling Smørgrav 	sbuf_finish(sb);
3006c35e809SDag-Erling Smørgrav 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
3016c35e809SDag-Erling Smørgrav 	sbuf_delete(sb);
3026c35e809SDag-Erling Smørgrav 	return (error);
3036c35e809SDag-Erling Smørgrav }
3046c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING|CTLFLAG_RD,
3056c35e809SDag-Erling Smørgrav     NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics");
3066c35e809SDag-Erling Smørgrav #endif
3076c35e809SDag-Erling Smørgrav 
3080cde2e34SJason Evans /*
3096283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
3106283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
3116283b7d0SJohn Baldwin  */
3126283b7d0SJohn Baldwin void
3136283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
3146283b7d0SJohn Baldwin {
3156283b7d0SJohn Baldwin 
316dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3170d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
3180d975d63SJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
3190d975d63SJohn Baldwin 	    file, line));
320dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
321dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
322dde96c99SJohn Baldwin 	    line);
323dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3246c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
3256c35e809SDag-Erling Smørgrav 	/* don't reset the timer when/if recursing */
326b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime == 0) {
327b61860adSDag-Erling Smørgrav 		m->mtx_filename = file;
328b61860adSDag-Erling Smørgrav 		m->mtx_lineno = line;
329b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0;
3306c35e809SDag-Erling Smørgrav 		++mutex_prof_acquisitions;
3316c35e809SDag-Erling Smørgrav 	}
3326c35e809SDag-Erling Smørgrav #endif
3336283b7d0SJohn Baldwin }
3346283b7d0SJohn Baldwin 
3356283b7d0SJohn Baldwin void
3366283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
3376283b7d0SJohn Baldwin {
3386283b7d0SJohn Baldwin 
339dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3400d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
3410d975d63SJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
3420d975d63SJohn Baldwin 	    file, line));
3430d975d63SJohn Baldwin  	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3440d975d63SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
3450d975d63SJohn Baldwin 	    line);
34621377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
3476c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
348b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime != 0) {
3496c35e809SDag-Erling Smørgrav 		static const char *unknown = "(unknown)";
3506c35e809SDag-Erling Smørgrav 		struct mutex_prof *mpp;
351b784ffe9SDag-Erling Smørgrav 		u_int64_t acqtime, now;
3526c35e809SDag-Erling Smørgrav 		const char *p, *q;
353e6330704SDag-Erling Smørgrav 		volatile u_int hash;
3546c35e809SDag-Erling Smørgrav 
355b784ffe9SDag-Erling Smørgrav 		now = nanoseconds();
356b61860adSDag-Erling Smørgrav 		acqtime = m->mtx_acqtime;
357b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = 0;
358b784ffe9SDag-Erling Smørgrav 		if (now <= acqtime)
3596c35e809SDag-Erling Smørgrav 			goto out;
360b61860adSDag-Erling Smørgrav 		for (p = m->mtx_filename; strncmp(p, "../", 3) == 0; p += 3)
3616c35e809SDag-Erling Smørgrav 			/* nothing */ ;
3626c35e809SDag-Erling Smørgrav 		if (p == NULL || *p == '\0')
3636c35e809SDag-Erling Smørgrav 			p = unknown;
364b61860adSDag-Erling Smørgrav 		for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q)
3656c35e809SDag-Erling Smørgrav 			hash = (hash * 2 + *q) % MPROF_HASH_SIZE;
3666c35e809SDag-Erling Smørgrav 		mtx_lock_spin(&mprof_mtx);
367e6330704SDag-Erling Smørgrav 		for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next)
368b61860adSDag-Erling Smørgrav 			if (mpp->line == m->mtx_lineno &&
369b61860adSDag-Erling Smørgrav 			    strcmp(mpp->file, p) == 0)
3706c35e809SDag-Erling Smørgrav 				break;
3716c35e809SDag-Erling Smørgrav 		if (mpp == NULL) {
3726c35e809SDag-Erling Smørgrav 			/* Just exit if we cannot get a trace buffer */
3736c35e809SDag-Erling Smørgrav 			if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) {
3746c35e809SDag-Erling Smørgrav 				++mutex_prof_rejected;
3756c35e809SDag-Erling Smørgrav 				goto unlock;
3766c35e809SDag-Erling Smørgrav 			}
3776c35e809SDag-Erling Smørgrav 			mpp = &mprof_buf[first_free_mprof_buf++];
3786c35e809SDag-Erling Smørgrav 			mpp->name = mtx_name(m);
3796c35e809SDag-Erling Smørgrav 			mpp->file = p;
380b61860adSDag-Erling Smørgrav 			mpp->line = m->mtx_lineno;
381e6330704SDag-Erling Smørgrav 			mpp->next = mprof_hash[hash];
382e6330704SDag-Erling Smørgrav 			if (mprof_hash[hash] != NULL)
383e6330704SDag-Erling Smørgrav 				++mutex_prof_collisions;
3846c35e809SDag-Erling Smørgrav 			mprof_hash[hash] = mpp;
385e6330704SDag-Erling Smørgrav 			++mutex_prof_records;
3866c35e809SDag-Erling Smørgrav 		}
3876c35e809SDag-Erling Smørgrav 		/*
3886c35e809SDag-Erling Smørgrav 		 * Record if the mutex has been held longer now than ever
3896c35e809SDag-Erling Smørgrav 		 * before
3906c35e809SDag-Erling Smørgrav 		 */
391b784ffe9SDag-Erling Smørgrav 		if ((now - acqtime) > mpp->counter[MPROF_MAX])
392b784ffe9SDag-Erling Smørgrav 			mpp->counter[MPROF_MAX] = now - acqtime;
393b784ffe9SDag-Erling Smørgrav 		mpp->counter[MPROF_TOT] += now - acqtime;
394b784ffe9SDag-Erling Smørgrav 		mpp->counter[MPROF_CNT] += 1;
395b784ffe9SDag-Erling Smørgrav 		mpp->counter[MPROF_AVG] =
396b784ffe9SDag-Erling Smørgrav 		    mpp->counter[MPROF_TOT] / mpp->counter[MPROF_CNT];
3976c35e809SDag-Erling Smørgrav unlock:
3986c35e809SDag-Erling Smørgrav 		mtx_unlock_spin(&mprof_mtx);
3996c35e809SDag-Erling Smørgrav 	}
4006c35e809SDag-Erling Smørgrav out:
4016c35e809SDag-Erling Smørgrav #endif
402dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
4036283b7d0SJohn Baldwin }
4046283b7d0SJohn Baldwin 
4056283b7d0SJohn Baldwin void
4066283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
4076283b7d0SJohn Baldwin {
4086283b7d0SJohn Baldwin 
409dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
4100d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
4110d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
4120d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
413ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1
414dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
415e8fdcfb5SJohn Baldwin #else
416e8fdcfb5SJohn Baldwin 	critical_enter();
417e8fdcfb5SJohn Baldwin #endif
418dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
419dde96c99SJohn Baldwin 	    line);
420dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
4216283b7d0SJohn Baldwin }
4226283b7d0SJohn Baldwin 
4236283b7d0SJohn Baldwin void
4246283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
4256283b7d0SJohn Baldwin {
4266283b7d0SJohn Baldwin 
427dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
4280d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
4290d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
4300d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
431dde96c99SJohn Baldwin  	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
432dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
433dde96c99SJohn Baldwin 	    line);
4340d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
435ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1
436dde96c99SJohn Baldwin 	_rel_spin_lock(m);
437e8fdcfb5SJohn Baldwin #else
438e8fdcfb5SJohn Baldwin 	critical_exit();
439e8fdcfb5SJohn Baldwin #endif
4406283b7d0SJohn Baldwin }
4416283b7d0SJohn Baldwin 
4426283b7d0SJohn Baldwin /*
4439ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
4449ed346baSBosko Milekic  * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that
4459ed346baSBosko Milekic  * if we're called, it's because we know we don't already own this lock.
4460cde2e34SJason Evans  */
4470cde2e34SJason Evans int
4489ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
4490cde2e34SJason Evans {
4500cde2e34SJason Evans 	int rval;
4510cde2e34SJason Evans 
452b40ce416SJulian Elischer 	MPASS(curthread != NULL);
4539ed346baSBosko Milekic 
454b40ce416SJulian Elischer 	rval = _obtain_lock(m, curthread);
4559ed346baSBosko Milekic 
45619284646SJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
45719284646SJohn Baldwin 	if (rval) {
4589ed346baSBosko Milekic 		/*
4599ed346baSBosko Milekic 		 * We do not handle recursion in _mtx_trylock; see the
4609ed346baSBosko Milekic 		 * note at the top of the routine.
4619ed346baSBosko Milekic 		 */
4625746a1d8SBosko Milekic 		KASSERT(!mtx_recursed(m),
4635746a1d8SBosko Milekic 		    ("mtx_trylock() called on a recursed mutex"));
4642d96f0b1SJohn Baldwin 		WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4652d96f0b1SJohn Baldwin 		    file, line);
4660cde2e34SJason Evans 	}
4679ed346baSBosko Milekic 
46819284646SJohn Baldwin 	return (rval);
4690cde2e34SJason Evans }
4700cde2e34SJason Evans 
4710cde2e34SJason Evans /*
4729ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4739ed346baSBosko Milekic  *
4749ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4759ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4760cde2e34SJason Evans  */
4770cde2e34SJason Evans void
4789ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line)
47936412d79SJohn Baldwin {
480b40ce416SJulian Elischer 	struct thread *td = curthread;
4812498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
4822498cf8cSJohn Baldwin 	struct thread *owner;
4832498cf8cSJohn Baldwin #endif
48402bd1bcdSIan Dowse #ifdef KTR
48502bd1bcdSIan Dowse 	int cont_logged = 0;
48602bd1bcdSIan Dowse #endif
48736412d79SJohn Baldwin 
488b40ce416SJulian Elischer 	if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)td) {
48936412d79SJohn Baldwin 		m->mtx_recurse++;
49008812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
49119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4925746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
49336412d79SJohn Baldwin 		return;
49436412d79SJohn Baldwin 	}
4959ed346baSBosko Milekic 
49619284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
49715ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
49815ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
49919284646SJohn Baldwin 		    m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
5001bd0eefbSJohn Baldwin 
501b40ce416SJulian Elischer 	while (!_obtain_lock(m, td)) {
502f5271ebcSJohn Baldwin 		uintptr_t v;
503b40ce416SJulian Elischer 		struct thread *td1;
50436412d79SJohn Baldwin 
5059ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
50636412d79SJohn Baldwin 		/*
5079ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
5089ed346baSBosko Milekic 		 * the sched_lock.
50936412d79SJohn Baldwin 		 */
51036412d79SJohn Baldwin 		if ((v = m->mtx_lock) == MTX_UNOWNED) {
5119ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
512703fc290SJohn Baldwin #ifdef __i386__
5136b8c6989SJohn Baldwin 			ia32_pause();
514703fc290SJohn Baldwin #endif
51536412d79SJohn Baldwin 			continue;
51636412d79SJohn Baldwin 		}
5179ed346baSBosko Milekic 
51836412d79SJohn Baldwin 		/*
5199ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
520b40ce416SJulian Elischer 		 * there are threads blocked on it.
52136412d79SJohn Baldwin 		 */
52236412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
523b40ce416SJulian Elischer 			td1 = TAILQ_FIRST(&m->mtx_blocked);
524b40ce416SJulian Elischer 			MPASS(td1 != NULL);
525b40ce416SJulian Elischer 			m->mtx_lock = (uintptr_t)td | MTX_CONTESTED;
5269ed346baSBosko Milekic 
5272c100766SJulian Elischer 			if (td1->td_priority < td->td_priority)
5282c100766SJulian Elischer 				td->td_priority = td1->td_priority;
5299ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
53036412d79SJohn Baldwin 			return;
53136412d79SJohn Baldwin 		}
5329ed346baSBosko Milekic 
53336412d79SJohn Baldwin 		/*
5349ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
5359ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
5369ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
53736412d79SJohn Baldwin 		 */
53836412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
53936412d79SJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, (void *)v,
54036412d79SJohn Baldwin 			(void *)(v | MTX_CONTESTED))) {
5419ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
542703fc290SJohn Baldwin #ifdef __i386__
5436b8c6989SJohn Baldwin 			ia32_pause();
544703fc290SJohn Baldwin #endif
54536412d79SJohn Baldwin 			continue;
54636412d79SJohn Baldwin 		}
54736412d79SJohn Baldwin 
5482498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
5492498cf8cSJohn Baldwin 		/*
5502498cf8cSJohn Baldwin 		 * If the current owner of the lock is executing on another
5512498cf8cSJohn Baldwin 		 * CPU, spin instead of blocking.
5522498cf8cSJohn Baldwin 		 */
5532498cf8cSJohn Baldwin 		owner = (struct thread *)(v & MTX_FLAGMASK);
5546a95e08fSJohn Baldwin 		if (m != &Giant && thread_running(owner)) {
5552498cf8cSJohn Baldwin 			mtx_unlock_spin(&sched_lock);
5566a95e08fSJohn Baldwin 			while (mtx_owner(m) == owner && thread_running(owner)) {
557703fc290SJohn Baldwin #ifdef __i386__
5586b8c6989SJohn Baldwin 				ia32_pause();
559703fc290SJohn Baldwin #endif
5607fcca609SJohn Baldwin 			}
5612498cf8cSJohn Baldwin 			continue;
5622498cf8cSJohn Baldwin 		}
5632498cf8cSJohn Baldwin #endif	/* SMP && ADAPTIVE_MUTEXES */
5642498cf8cSJohn Baldwin 
5659ed346baSBosko Milekic 		/*
5667feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
5679ed346baSBosko Milekic 		 */
56836412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
56936412d79SJohn Baldwin 
57036412d79SJohn Baldwin #ifdef notyet
57136412d79SJohn Baldwin 		/*
5729ed346baSBosko Milekic 		 * If we're borrowing an interrupted thread's VM context, we
5739ed346baSBosko Milekic 		 * must clean up before going to sleep.
57436412d79SJohn Baldwin 		 */
575b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
576b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
57736412d79SJohn Baldwin 
57836412d79SJohn Baldwin 			if (it->it_interrupted) {
57919284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
58036412d79SJohn Baldwin 					CTR2(KTR_LOCK,
58115ec816aSJohn Baldwin 				    "_mtx_lock_sleep: %p interrupted %p",
58236412d79SJohn Baldwin 					    it, it->it_interrupted);
58336412d79SJohn Baldwin 				intr_thd_fixup(it);
58436412d79SJohn Baldwin 			}
58536412d79SJohn Baldwin 		}
58636412d79SJohn Baldwin #endif
58736412d79SJohn Baldwin 
5889ed346baSBosko Milekic 		/*
5899ed346baSBosko Milekic 		 * Put us on the list of threads blocked on this mutex.
5909ed346baSBosko Milekic 		 */
59136412d79SJohn Baldwin 		if (TAILQ_EMPTY(&m->mtx_blocked)) {
59218fc2ba9SJohn Baldwin 			td1 = mtx_owner(m);
593b40ce416SJulian Elischer 			LIST_INSERT_HEAD(&td1->td_contested, m, mtx_contested);
594551cf4e1SJohn Baldwin 			TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_lockq);
59536412d79SJohn Baldwin 		} else {
596551cf4e1SJohn Baldwin 			TAILQ_FOREACH(td1, &m->mtx_blocked, td_lockq)
5972c100766SJulian Elischer 				if (td1->td_priority > td->td_priority)
59836412d79SJohn Baldwin 					break;
599b40ce416SJulian Elischer 			if (td1)
600551cf4e1SJohn Baldwin 				TAILQ_INSERT_BEFORE(td1, td, td_lockq);
60136412d79SJohn Baldwin 			else
602551cf4e1SJohn Baldwin 				TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_lockq);
60336412d79SJohn Baldwin 		}
60402bd1bcdSIan Dowse #ifdef KTR
60502bd1bcdSIan Dowse 		if (!cont_logged) {
60602bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
60702bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
60802bd1bcdSIan Dowse 			    td, file, line, m->mtx_object.lo_name,
60902bd1bcdSIan Dowse 			    WITNESS_FILE(&m->mtx_object),
61002bd1bcdSIan Dowse 			    WITNESS_LINE(&m->mtx_object));
61102bd1bcdSIan Dowse 			cont_logged = 1;
61202bd1bcdSIan Dowse 		}
61302bd1bcdSIan Dowse #endif
61436412d79SJohn Baldwin 
6159ed346baSBosko Milekic 		/*
6169ed346baSBosko Milekic 		 * Save who we're blocked on.
6179ed346baSBosko Milekic 		 */
618b40ce416SJulian Elischer 		td->td_blocked = m;
619551cf4e1SJohn Baldwin 		td->td_lockname = m->mtx_object.lo_name;
620551cf4e1SJohn Baldwin 		TD_SET_LOCK(td);
621b40ce416SJulian Elischer 		propagate_priority(td);
6229ed346baSBosko Milekic 
62319284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
624562e4ffeSJohn Baldwin 			CTR3(KTR_LOCK,
625b40ce416SJulian Elischer 			    "_mtx_lock_sleep: p %p blocked on [%p] %s", td, m,
62619284646SJohn Baldwin 			    m->mtx_object.lo_name);
6279ed346baSBosko Milekic 
628b40ce416SJulian Elischer 		td->td_proc->p_stats->p_ru.ru_nvcsw++;
62920cdcc5bSJohn Baldwin 		mi_switch();
6309ed346baSBosko Milekic 
63119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
63236412d79SJohn Baldwin 			CTR3(KTR_LOCK,
6339ed346baSBosko Milekic 			  "_mtx_lock_sleep: p %p free from blocked on [%p] %s",
634b40ce416SJulian Elischer 			  td, m, m->mtx_object.lo_name);
6359ed346baSBosko Milekic 
6369ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
63736412d79SJohn Baldwin 	}
6389ed346baSBosko Milekic 
63902bd1bcdSIan Dowse #ifdef KTR
64002bd1bcdSIan Dowse 	if (cont_logged) {
64102bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
64202bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
64302bd1bcdSIan Dowse 		    m->mtx_object.lo_name, td, file, line);
64402bd1bcdSIan Dowse 	}
64502bd1bcdSIan Dowse #endif
64636412d79SJohn Baldwin 	return;
6479ed346baSBosko Milekic }
6489ed346baSBosko Milekic 
6499ed346baSBosko Milekic /*
6509ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
6519ed346baSBosko Milekic  *
6529ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
6539ed346baSBosko Milekic  * is handled inline.
6549ed346baSBosko Milekic  */
6559ed346baSBosko Milekic void
6567e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line)
65736412d79SJohn Baldwin {
65836412d79SJohn Baldwin 	int i = 0;
65936412d79SJohn Baldwin 
66019284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6615746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
6629ed346baSBosko Milekic 
66336412d79SJohn Baldwin 	for (;;) {
664b40ce416SJulian Elischer 		if (_obtain_lock(m, curthread))
66536412d79SJohn Baldwin 			break;
6669ed346baSBosko Milekic 
6677141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
6687e1f6dfeSJohn Baldwin 		critical_exit();
66936412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
670703fc290SJohn Baldwin 			if (i++ < 10000000) {
671703fc290SJohn Baldwin #ifdef __i386__
6726b8c6989SJohn Baldwin 				ia32_pause();
673703fc290SJohn Baldwin #endif
67436412d79SJohn Baldwin 				continue;
675703fc290SJohn Baldwin 			}
6760e54ddadSJohn Baldwin 			if (i < 60000000)
67736412d79SJohn Baldwin 				DELAY(1);
67836412d79SJohn Baldwin #ifdef DDB
67936412d79SJohn Baldwin 			else if (!db_active)
68036412d79SJohn Baldwin #else
68136412d79SJohn Baldwin 			else
68236412d79SJohn Baldwin #endif
6839ed346baSBosko Milekic 				panic("spin lock %s held by %p for > 5 seconds",
68419284646SJohn Baldwin 				    m->mtx_object.lo_name, (void *)m->mtx_lock);
685703fc290SJohn Baldwin #ifdef __i386__
6866b8c6989SJohn Baldwin 			ia32_pause();
687703fc290SJohn Baldwin #endif
68836412d79SJohn Baldwin 		}
6897e1f6dfeSJohn Baldwin 		critical_enter();
69036412d79SJohn Baldwin 	}
69136412d79SJohn Baldwin 
69219284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6939ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
6949ed346baSBosko Milekic 
69536412d79SJohn Baldwin 	return;
69636412d79SJohn Baldwin }
69736412d79SJohn Baldwin 
6989ed346baSBosko Milekic /*
6999ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
7009ed346baSBosko Milekic  *
7019ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
7029ed346baSBosko Milekic  * need to wake up a blocked thread).
7039ed346baSBosko Milekic  */
70436412d79SJohn Baldwin void
7059ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
70636412d79SJohn Baldwin {
707b40ce416SJulian Elischer 	struct thread *td, *td1;
70836412d79SJohn Baldwin 	struct mtx *m1;
70936412d79SJohn Baldwin 	int pri;
71036412d79SJohn Baldwin 
711b40ce416SJulian Elischer 	td = curthread;
7129ed346baSBosko Milekic 
71308812b39SBosko Milekic 	if (mtx_recursed(m)) {
71436412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
71508812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
71619284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7179ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
71836412d79SJohn Baldwin 		return;
71936412d79SJohn Baldwin 	}
7209ed346baSBosko Milekic 
7219ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
72219284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
7239ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
7249ed346baSBosko Milekic 
725b40ce416SJulian Elischer 	td1 = TAILQ_FIRST(&m->mtx_blocked);
7262498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
7272498cf8cSJohn Baldwin 	if (td1 == NULL) {
7282498cf8cSJohn Baldwin 		_release_lock_quick(m);
7292498cf8cSJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7302498cf8cSJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
7312498cf8cSJohn Baldwin 		mtx_unlock_spin(&sched_lock);
7322498cf8cSJohn Baldwin 		return;
7332498cf8cSJohn Baldwin 	}
7342498cf8cSJohn Baldwin #endif
735b40ce416SJulian Elischer 	MPASS(td->td_proc->p_magic == P_MAGIC);
736b40ce416SJulian Elischer 	MPASS(td1->td_proc->p_magic == P_MAGIC);
7379ed346baSBosko Milekic 
738551cf4e1SJohn Baldwin 	TAILQ_REMOVE(&m->mtx_blocked, td1, td_lockq);
7399ed346baSBosko Milekic 
74036412d79SJohn Baldwin 	if (TAILQ_EMPTY(&m->mtx_blocked)) {
74136412d79SJohn Baldwin 		LIST_REMOVE(m, mtx_contested);
74236412d79SJohn Baldwin 		_release_lock_quick(m);
74319284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7449ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
74536412d79SJohn Baldwin 	} else
7469ed346baSBosko Milekic 		atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED);
7479ed346baSBosko Milekic 
748d5a08a60SJake Burkholder 	pri = PRI_MAX;
749b40ce416SJulian Elischer 	LIST_FOREACH(m1, &td->td_contested, mtx_contested) {
7502c100766SJulian Elischer 		int cp = TAILQ_FIRST(&m1->mtx_blocked)->td_priority;
75136412d79SJohn Baldwin 		if (cp < pri)
75236412d79SJohn Baldwin 			pri = cp;
75336412d79SJohn Baldwin 	}
7549ed346baSBosko Milekic 
7552c100766SJulian Elischer 	if (pri > td->td_base_pri)
7562c100766SJulian Elischer 		pri = td->td_base_pri;
7572c100766SJulian Elischer 	td->td_priority = pri;
7589ed346baSBosko Milekic 
75919284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
7609ed346baSBosko Milekic 		CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p",
761b40ce416SJulian Elischer 		    m, td1);
7629ed346baSBosko Milekic 
763b40ce416SJulian Elischer 	td1->td_blocked = NULL;
764551cf4e1SJohn Baldwin 	TD_CLR_LOCK(td1);
765e0817317SJulian Elischer 	if (!TD_CAN_RUN(td1)) {
766e0817317SJulian Elischer 		mtx_unlock_spin(&sched_lock);
767e0817317SJulian Elischer 		return;
768e0817317SJulian Elischer 	}
76927354830SJulian Elischer 	setrunqueue(td1);
7709ed346baSBosko Milekic 
7712c100766SJulian Elischer 	if (td->td_critnest == 1 && td1->td_priority < pri) {
77236412d79SJohn Baldwin #ifdef notyet
773b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
774b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
77536412d79SJohn Baldwin 
77636412d79SJohn Baldwin 			if (it->it_interrupted) {
77719284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
77836412d79SJohn Baldwin 					CTR2(KTR_LOCK,
77915ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
78036412d79SJohn Baldwin 					    it, it->it_interrupted);
78136412d79SJohn Baldwin 				intr_thd_fixup(it);
78236412d79SJohn Baldwin 			}
78336412d79SJohn Baldwin 		}
78436412d79SJohn Baldwin #endif
78519284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
786562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
7879ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
7889ed346baSBosko Milekic 			    (void *)m->mtx_lock);
7899ed346baSBosko Milekic 
790b40ce416SJulian Elischer 		td->td_proc->p_stats->p_ru.ru_nivcsw++;
79136412d79SJohn Baldwin 		mi_switch();
79219284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7939ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
79431271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
79536412d79SJohn Baldwin 	}
79636412d79SJohn Baldwin 
7979ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
7989ed346baSBosko Milekic 
7999ed346baSBosko Milekic 	return;
8009ed346baSBosko Milekic }
8019ed346baSBosko Milekic 
8029ed346baSBosko Milekic /*
8039ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
8049ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
8059ed346baSBosko Milekic  */
8069ed346baSBosko Milekic 
8079ed346baSBosko Milekic /*
80815ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
8099ed346baSBosko Milekic  */
8101103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
8110cde2e34SJason Evans void
81256771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
8130cde2e34SJason Evans {
8145cb0fbe4SJohn Baldwin 
8155cb0fbe4SJohn Baldwin 	if (panicstr != NULL)
8165cb0fbe4SJohn Baldwin 		return;
817a10f4966SJake Burkholder 	switch (what) {
8180cde2e34SJason Evans 	case MA_OWNED:
8190cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
8200cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
821a10f4966SJake Burkholder 		if (!mtx_owned(m))
8220cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
82319284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
824a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
825a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
8260cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
82719284646SJohn Baldwin 				    m->mtx_object.lo_name, file, line);
828a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
8290cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
83019284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
8310cde2e34SJason Evans 		}
8320cde2e34SJason Evans 		break;
8330cde2e34SJason Evans 	case MA_NOTOWNED:
834a10f4966SJake Burkholder 		if (mtx_owned(m))
8350cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
83619284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
8370cde2e34SJason Evans 		break;
8380cde2e34SJason Evans 	default:
83956771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
8400cde2e34SJason Evans 	}
8410cde2e34SJason Evans }
8420cde2e34SJason Evans #endif
8430cde2e34SJason Evans 
8449ed346baSBosko Milekic /*
8459ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
84619284646SJohn Baldwin  *
84719284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
84819284646SJohn Baldwin  * maintained by the witness code.
8499ed346baSBosko Milekic  */
85036412d79SJohn Baldwin #ifdef MUTEX_DEBUG
85136412d79SJohn Baldwin 
8524d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
85336412d79SJohn Baldwin 
85419284646SJohn Baldwin void
85519284646SJohn Baldwin mtx_validate(struct mtx *m)
85636412d79SJohn Baldwin {
85736412d79SJohn Baldwin 
85836412d79SJohn Baldwin /*
85936412d79SJohn Baldwin  * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
86036412d79SJohn Baldwin  * we can re-enable the kernacc() checks.
86136412d79SJohn Baldwin  */
86236412d79SJohn Baldwin #ifndef __alpha__
86376dcbd6fSBosko Milekic 	/*
86476dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
86576dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
86676dcbd6fSBosko Milekic 	 * requires Giant itself.
86776dcbd6fSBosko Milekic 	 */
868ab07087eSBosko Milekic 	if (!cold)
869ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
870ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
87119284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
87236412d79SJohn Baldwin #endif
87336412d79SJohn Baldwin }
87436412d79SJohn Baldwin #endif
87536412d79SJohn Baldwin 
8769ed346baSBosko Milekic /*
877c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
878c27b5699SAndrew R. Reiter  */
879c27b5699SAndrew R. Reiter void
880c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
881c27b5699SAndrew R. Reiter {
882c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
883c27b5699SAndrew R. Reiter 
8840c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
885c27b5699SAndrew R. Reiter }
886c27b5699SAndrew R. Reiter 
887c27b5699SAndrew R. Reiter /*
8889ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
8890c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
8900c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
8910c88508aSJohn Baldwin  * witness.
8929ed346baSBosko Milekic  */
89336412d79SJohn Baldwin void
8940c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
89536412d79SJohn Baldwin {
89619284646SJohn Baldwin 	struct lock_object *lock;
8979ed346baSBosko Milekic 
89819284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
899f22a4b62SJeff Roberson 	    MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0);
9009ed346baSBosko Milekic 
90136412d79SJohn Baldwin #ifdef MUTEX_DEBUG
9029ed346baSBosko Milekic 	/* Diagnostic and error correction */
90319284646SJohn Baldwin 	mtx_validate(m);
9046936206eSJohn Baldwin #endif
90536412d79SJohn Baldwin 
90619284646SJohn Baldwin 	lock = &m->mtx_object;
9077ada5876SJohn Baldwin 	KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
9080c88508aSJohn Baldwin 	    ("mutex %s %p already initialized", name, m));
9097ada5876SJohn Baldwin 	bzero(m, sizeof(*m));
91019284646SJohn Baldwin 	if (opts & MTX_SPIN)
91119284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_spin;
91219284646SJohn Baldwin 	else
91319284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_sleep;
9140c88508aSJohn Baldwin 	lock->lo_name = name;
9150c88508aSJohn Baldwin 	lock->lo_type = type != NULL ? type : name;
91619284646SJohn Baldwin 	if (opts & MTX_QUIET)
91719284646SJohn Baldwin 		lock->lo_flags = LO_QUIET;
91819284646SJohn Baldwin 	if (opts & MTX_RECURSE)
91919284646SJohn Baldwin 		lock->lo_flags |= LO_RECURSABLE;
92019284646SJohn Baldwin 	if (opts & MTX_SLEEPABLE)
92119284646SJohn Baldwin 		lock->lo_flags |= LO_SLEEPABLE;
92219284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
92319284646SJohn Baldwin 		lock->lo_flags |= LO_WITNESS;
924f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
925f22a4b62SJeff Roberson 		lock->lo_flags |= LO_DUPOK;
92619284646SJohn Baldwin 
92719284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
92836412d79SJohn Baldwin 	TAILQ_INIT(&m->mtx_blocked);
9299ed346baSBosko Milekic 
93019284646SJohn Baldwin 	LOCK_LOG_INIT(lock, opts);
931d1c1b841SJason Evans 
93219284646SJohn Baldwin 	WITNESS_INIT(lock);
93336412d79SJohn Baldwin }
93436412d79SJohn Baldwin 
9359ed346baSBosko Milekic /*
93619284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
93719284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
93819284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
93919284646SJohn Baldwin  * flags.
9409ed346baSBosko Milekic  */
94136412d79SJohn Baldwin void
94236412d79SJohn Baldwin mtx_destroy(struct mtx *m)
94336412d79SJohn Baldwin {
94436412d79SJohn Baldwin 
94519284646SJohn Baldwin 	LOCK_LOG_DESTROY(&m->mtx_object, 0);
9469ed346baSBosko Milekic 
94719284646SJohn Baldwin 	if (!mtx_owned(m))
94819284646SJohn Baldwin 		MPASS(mtx_unowned(m));
94919284646SJohn Baldwin 	else {
95008812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
9519ed346baSBosko Milekic 
95219284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
953c86b6ff5SJohn Baldwin 		WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
954c86b6ff5SJohn Baldwin 		    __LINE__);
95536412d79SJohn Baldwin 	}
9560384fff8SJason Evans 
95719284646SJohn Baldwin 	WITNESS_DESTROY(&m->mtx_object);
9580384fff8SJason Evans }
959d23f5958SMatthew Dillon 
960d23f5958SMatthew Dillon /*
961c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
962c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
963c53c013bSJohn Baldwin  * setup before this is called.
964c53c013bSJohn Baldwin  */
965c53c013bSJohn Baldwin void
966c53c013bSJohn Baldwin mutex_init(void)
967c53c013bSJohn Baldwin {
968c53c013bSJohn Baldwin 
969c53c013bSJohn Baldwin 	/* Setup thread0 so that mutexes work. */
970c53c013bSJohn Baldwin 	LIST_INIT(&thread0.td_contested);
971c53c013bSJohn Baldwin 
972c53c013bSJohn Baldwin 	/*
973c53c013bSJohn Baldwin 	 * Initialize mutexes.
974c53c013bSJohn Baldwin 	 */
9750c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
9760c88508aSJohn Baldwin 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
9770c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
978c53c013bSJohn Baldwin 	mtx_lock(&Giant);
979c53c013bSJohn Baldwin }
980c53c013bSJohn Baldwin 
981c53c013bSJohn Baldwin /*
982d23f5958SMatthew Dillon  * Encapsulated Giant mutex routines.  These routines provide encapsulation
983d23f5958SMatthew Dillon  * control for the Giant mutex, allowing sysctls to be used to turn on and
984d23f5958SMatthew Dillon  * off Giant around certain subsystems.  The default value for the sysctls
985d23f5958SMatthew Dillon  * are set to what developers believe is stable and working in regards to
986d23f5958SMatthew Dillon  * the Giant pushdown.  Developers should not turn off Giant via these
987d23f5958SMatthew Dillon  * sysctls unless they know what they are doing.
988d23f5958SMatthew Dillon  *
989d23f5958SMatthew Dillon  * Callers of mtx_lock_giant() are expected to pass the return value to an
990d23f5958SMatthew Dillon  * accompanying mtx_unlock_giant() later on.  If multiple subsystems are
991d23f5958SMatthew Dillon  * effected by a Giant wrap, all related sysctl variables must be zero for
992d23f5958SMatthew Dillon  * the subsystem call to operate without Giant (as determined by the caller).
993d23f5958SMatthew Dillon  */
994d23f5958SMatthew Dillon 
995d23f5958SMatthew Dillon SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation");
996d23f5958SMatthew Dillon 
997d23f5958SMatthew Dillon static int kern_giant_all = 0;
998d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, "");
999d23f5958SMatthew Dillon 
1000d23f5958SMatthew Dillon int kern_giant_proc = 1;	/* Giant around PROC locks */
1001d23f5958SMatthew Dillon int kern_giant_file = 1;	/* Giant around struct file & filedesc */
1002735da6deSMatthew Dillon int kern_giant_ucred = 1;	/* Giant around ucred */
1003d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, "");
1004d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, "");
1005735da6deSMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, "");
1006d23f5958SMatthew Dillon 
1007d23f5958SMatthew Dillon int
1008d23f5958SMatthew Dillon mtx_lock_giant(int sysctlvar)
1009d23f5958SMatthew Dillon {
1010d23f5958SMatthew Dillon 	if (sysctlvar || kern_giant_all) {
1011d23f5958SMatthew Dillon 		mtx_lock(&Giant);
1012d23f5958SMatthew Dillon 		return(1);
1013d23f5958SMatthew Dillon 	}
1014d23f5958SMatthew Dillon 	return(0);
1015d23f5958SMatthew Dillon }
1016d23f5958SMatthew Dillon 
1017d23f5958SMatthew Dillon void
1018d23f5958SMatthew Dillon mtx_unlock_giant(int s)
1019d23f5958SMatthew Dillon {
1020d23f5958SMatthew Dillon 	if (s)
1021d23f5958SMatthew Dillon 		mtx_unlock(&Giant);
1022d23f5958SMatthew Dillon }
1023d23f5958SMatthew Dillon 
1024