xref: /freebsd/sys/kern/kern_mutex.c (revision 0d975d6341424d106982194360aef1e8a3ac9711)
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 		KASSERT(td->td_state != TDS_SURPLUS, ("Mutex owner SURPLUS"));
123e602ba25SJulian Elischer 		MPASS(td->td_proc != NULL);
124b40ce416SJulian Elischer 		MPASS(td->td_proc->p_magic == P_MAGIC);
125e602ba25SJulian Elischer 		KASSERT(td->td_state != TDS_SLP,
126e602ba25SJulian Elischer 		    ("sleeping thread owns a mutex"));
1272c100766SJulian Elischer 		if (td->td_priority <= pri) /* lower is higher priority */
12836412d79SJohn Baldwin 			return;
1291bd0eefbSJohn Baldwin 
1301bd0eefbSJohn Baldwin 
13136412d79SJohn Baldwin 		/*
13236412d79SJohn Baldwin 		 * If lock holder is actually running, just bump priority.
13336412d79SJohn Baldwin 		 */
134e602ba25SJulian Elischer 		if (td->td_state == TDS_RUNNING) {
135e602ba25SJulian Elischer 			td->td_priority = pri;
13636412d79SJohn Baldwin 			return;
13736412d79SJohn Baldwin 		}
138d5a08a60SJake Burkholder 
1391b43703bSJohn Baldwin #ifndef SMP
1401b43703bSJohn Baldwin 		/*
141b40ce416SJulian Elischer 		 * For UP, we check to see if td is curthread (this shouldn't
1421b43703bSJohn Baldwin 		 * ever happen however as it would mean we are in a deadlock.)
1431b43703bSJohn Baldwin 		 */
144b40ce416SJulian Elischer 		KASSERT(td != curthread, ("Deadlock detected"));
1451b43703bSJohn Baldwin #endif
1461b43703bSJohn Baldwin 
14736412d79SJohn Baldwin 		/*
148b40ce416SJulian Elischer 		 * If on run queue move to new run queue, and quit.
149b40ce416SJulian Elischer 		 * XXXKSE this gets a lot more complicated under threads
150b40ce416SJulian Elischer 		 * but try anyhow.
151e602ba25SJulian Elischer 		 * We should have a special call to do this more efficiently.
15236412d79SJohn Baldwin 		 */
153e602ba25SJulian Elischer 		if (td->td_state == TDS_RUNQ) {
154b40ce416SJulian Elischer 			MPASS(td->td_blocked == NULL);
155b40ce416SJulian Elischer 			remrunqueue(td);
156e602ba25SJulian Elischer 			td->td_priority = pri;
157b40ce416SJulian Elischer 			setrunqueue(td);
15836412d79SJohn Baldwin 			return;
15936412d79SJohn Baldwin 		}
160e602ba25SJulian Elischer 		/*
161e602ba25SJulian Elischer 		 * Adjust for any other cases.
162e602ba25SJulian Elischer 		 */
163e602ba25SJulian Elischer 		td->td_priority = pri;
16436412d79SJohn Baldwin 
16536412d79SJohn Baldwin 		/*
1661bd0eefbSJohn Baldwin 		 * If we aren't blocked on a mutex, we should be.
16736412d79SJohn Baldwin 		 */
168e602ba25SJulian Elischer 		KASSERT(td->td_state == TDS_MTX, (
1691bd0eefbSJohn Baldwin 		    "process %d(%s):%d holds %s but isn't blocked on a mutex\n",
170e602ba25SJulian Elischer 		    td->td_proc->p_pid, td->td_proc->p_comm, td->td_state,
17119284646SJohn Baldwin 		    m->mtx_object.lo_name));
17236412d79SJohn Baldwin 
17336412d79SJohn Baldwin 		/*
174b40ce416SJulian Elischer 		 * Pick up the mutex that td is blocked on.
17536412d79SJohn Baldwin 		 */
176b40ce416SJulian Elischer 		m = td->td_blocked;
17736412d79SJohn Baldwin 		MPASS(m != NULL);
17836412d79SJohn Baldwin 
17936412d79SJohn Baldwin 		/*
180b40ce416SJulian Elischer 		 * Check if the thread needs to be moved up on
18136412d79SJohn Baldwin 		 * the blocked chain
18236412d79SJohn Baldwin 		 */
183b40ce416SJulian Elischer 		if (td == TAILQ_FIRST(&m->mtx_blocked)) {
1841bd0eefbSJohn Baldwin 			continue;
1851bd0eefbSJohn Baldwin 		}
1869ed346baSBosko Milekic 
187b40ce416SJulian Elischer 		td1 = TAILQ_PREV(td, threadqueue, td_blkq);
1882c100766SJulian Elischer 		if (td1->td_priority <= pri) {
18936412d79SJohn Baldwin 			continue;
19036412d79SJohn Baldwin 		}
19136412d79SJohn Baldwin 
19236412d79SJohn Baldwin 		/*
193b40ce416SJulian Elischer 		 * Remove thread from blocked chain and determine where
194b40ce416SJulian Elischer 		 * it should be moved up to.  Since we know that td1 has
195b40ce416SJulian Elischer 		 * a lower priority than td, we know that at least one
196b40ce416SJulian Elischer 		 * thread in the chain has a lower priority and that
197b40ce416SJulian Elischer 		 * td1 will thus not be NULL after the loop.
19836412d79SJohn Baldwin 		 */
199b40ce416SJulian Elischer 		TAILQ_REMOVE(&m->mtx_blocked, td, td_blkq);
200b40ce416SJulian Elischer 		TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq) {
201b40ce416SJulian Elischer 			MPASS(td1->td_proc->p_magic == P_MAGIC);
2022c100766SJulian Elischer 			if (td1->td_priority > pri)
20336412d79SJohn Baldwin 				break;
20436412d79SJohn Baldwin 		}
2059ed346baSBosko Milekic 
206b40ce416SJulian Elischer 		MPASS(td1 != NULL);
207b40ce416SJulian Elischer 		TAILQ_INSERT_BEFORE(td1, td, td_blkq);
20836412d79SJohn Baldwin 		CTR4(KTR_LOCK,
2098484de75SJohn Baldwin 		    "propagate_priority: p %p moved before %p on [%p] %s",
210b40ce416SJulian Elischer 		    td, td1, m, m->mtx_object.lo_name);
21136412d79SJohn Baldwin 	}
21236412d79SJohn Baldwin }
21336412d79SJohn Baldwin 
2146c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
2156c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging");
2166c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling");
2176c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0;
2186c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW,
2196c35e809SDag-Erling Smørgrav     &mutex_prof_enable, 0, "Enable tracing of mutex holdtime");
2206c35e809SDag-Erling Smørgrav 
2216c35e809SDag-Erling Smørgrav struct mutex_prof {
2226c35e809SDag-Erling Smørgrav 	const char *name;
2236c35e809SDag-Erling Smørgrav 	const char *file;
2246c35e809SDag-Erling Smørgrav 	int line;
2256c35e809SDag-Erling Smørgrav #define MPROF_MAX 0
2266c35e809SDag-Erling Smørgrav #define MPROF_TOT 1
2276c35e809SDag-Erling Smørgrav #define MPROF_CNT 2
2286c35e809SDag-Erling Smørgrav #define MPROF_AVG 3
229db586c8bSDag-Erling Smørgrav 	uintmax_t counter[4];
230e6330704SDag-Erling Smørgrav 	struct mutex_prof *next;
2316c35e809SDag-Erling Smørgrav };
2326c35e809SDag-Erling Smørgrav 
2336c35e809SDag-Erling Smørgrav /*
2346c35e809SDag-Erling Smørgrav  * mprof_buf is a static pool of profiling records to avoid possible
2356c35e809SDag-Erling Smørgrav  * reentrance of the memory allocation functions.
2366c35e809SDag-Erling Smørgrav  *
2376c35e809SDag-Erling Smørgrav  * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE.
2386c35e809SDag-Erling Smørgrav  */
239e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000
2406c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS];
2416c35e809SDag-Erling Smørgrav static int first_free_mprof_buf;
242e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009
2436c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE];
2446c35e809SDag-Erling Smørgrav 
2456c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions;
2466c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD,
2476c35e809SDag-Erling Smørgrav     &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded");
2486c35e809SDag-Erling Smørgrav static int mutex_prof_records;
2496c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD,
2506c35e809SDag-Erling Smørgrav     &mutex_prof_records, 0, "Number of profiling records");
2516c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS;
2526c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD,
2536c35e809SDag-Erling Smørgrav     &mutex_prof_maxrecords, 0, "Maximum number of profiling records");
2546c35e809SDag-Erling Smørgrav static int mutex_prof_rejected;
2556c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD,
2566c35e809SDag-Erling Smørgrav     &mutex_prof_rejected, 0, "Number of rejected profiling records");
2576c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE;
2586c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD,
2596c35e809SDag-Erling Smørgrav     &mutex_prof_hashsize, 0, "Hash size");
2606c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0;
2616c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD,
2626c35e809SDag-Erling Smørgrav     &mutex_prof_collisions, 0, "Number of hash collisions");
2636c35e809SDag-Erling Smørgrav 
2646c35e809SDag-Erling Smørgrav /*
2656c35e809SDag-Erling Smørgrav  * mprof_mtx protects the profiling buffers and the hash.
2666c35e809SDag-Erling Smørgrav  */
2676c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx;
268e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET);
2696c35e809SDag-Erling Smørgrav 
270b784ffe9SDag-Erling Smørgrav static u_int64_t
271b784ffe9SDag-Erling Smørgrav nanoseconds(void)
272b784ffe9SDag-Erling Smørgrav {
273b784ffe9SDag-Erling Smørgrav 	struct timespec tv;
274b784ffe9SDag-Erling Smørgrav 
275b784ffe9SDag-Erling Smørgrav 	nanotime(&tv);
276b784ffe9SDag-Erling Smørgrav 	return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec);
277b784ffe9SDag-Erling Smørgrav }
278b784ffe9SDag-Erling Smørgrav 
2796c35e809SDag-Erling Smørgrav static int
2806c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
2816c35e809SDag-Erling Smørgrav {
2826c35e809SDag-Erling Smørgrav 	struct sbuf *sb;
2836c35e809SDag-Erling Smørgrav 	int error, i;
2846c35e809SDag-Erling Smørgrav 
2856c35e809SDag-Erling Smørgrav 	if (first_free_mprof_buf == 0)
2866c35e809SDag-Erling Smørgrav 		return SYSCTL_OUT(req, "No locking recorded",
2876c35e809SDag-Erling Smørgrav 		    sizeof("No locking recorded"));
2886c35e809SDag-Erling Smørgrav 
2896c35e809SDag-Erling Smørgrav 	sb = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND);
2906c35e809SDag-Erling Smørgrav 	sbuf_printf(sb, "%12s %12s %12s %12s %s\n",
2916c35e809SDag-Erling Smørgrav 	    "max", "total", "count", "average", "name");
2926c35e809SDag-Erling Smørgrav 	mtx_lock_spin(&mprof_mtx);
2936c35e809SDag-Erling Smørgrav 	for (i = 0; i < first_free_mprof_buf; ++i)
294db586c8bSDag-Erling Smørgrav 		sbuf_printf(sb, "%12ju %12ju %12ju %12ju %s:%d (%s)\n",
295b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_MAX] / 1000,
296b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_TOT] / 1000,
297b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_CNT],
298b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_AVG] / 1000,
2996c35e809SDag-Erling Smørgrav 		    mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name);
3006c35e809SDag-Erling Smørgrav 	mtx_unlock_spin(&mprof_mtx);
3016c35e809SDag-Erling Smørgrav 	sbuf_finish(sb);
3026c35e809SDag-Erling Smørgrav 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
3036c35e809SDag-Erling Smørgrav 	sbuf_delete(sb);
3046c35e809SDag-Erling Smørgrav 	return (error);
3056c35e809SDag-Erling Smørgrav }
3066c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING|CTLFLAG_RD,
3076c35e809SDag-Erling Smørgrav     NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics");
3086c35e809SDag-Erling Smørgrav #endif
3096c35e809SDag-Erling Smørgrav 
3100cde2e34SJason Evans /*
3116283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
3126283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
3136283b7d0SJohn Baldwin  */
3146283b7d0SJohn Baldwin void
3156283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
3166283b7d0SJohn Baldwin {
3176283b7d0SJohn Baldwin 
318dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3190d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
3200d975d63SJohn Baldwin 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
3210d975d63SJohn Baldwin 	    file, line));
322dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
323dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
324dde96c99SJohn Baldwin 	    line);
325dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3266c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
3276c35e809SDag-Erling Smørgrav 	/* don't reset the timer when/if recursing */
328b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime == 0) {
329b61860adSDag-Erling Smørgrav 		m->mtx_filename = file;
330b61860adSDag-Erling Smørgrav 		m->mtx_lineno = line;
331b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0;
3326c35e809SDag-Erling Smørgrav 		++mutex_prof_acquisitions;
3336c35e809SDag-Erling Smørgrav 	}
3346c35e809SDag-Erling Smørgrav #endif
3356283b7d0SJohn Baldwin }
3366283b7d0SJohn Baldwin 
3376283b7d0SJohn Baldwin void
3386283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
3396283b7d0SJohn Baldwin {
3406283b7d0SJohn Baldwin 
341dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
3420d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep,
3430d975d63SJohn Baldwin 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
3440d975d63SJohn Baldwin 	    file, line));
3450d975d63SJohn Baldwin  	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3460d975d63SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
3470d975d63SJohn Baldwin 	    line);
34821377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
3496c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
350b61860adSDag-Erling Smørgrav 	if (m->mtx_acqtime != 0) {
3516c35e809SDag-Erling Smørgrav 		static const char *unknown = "(unknown)";
3526c35e809SDag-Erling Smørgrav 		struct mutex_prof *mpp;
353b784ffe9SDag-Erling Smørgrav 		u_int64_t acqtime, now;
3546c35e809SDag-Erling Smørgrav 		const char *p, *q;
355e6330704SDag-Erling Smørgrav 		volatile u_int hash;
3566c35e809SDag-Erling Smørgrav 
357b784ffe9SDag-Erling Smørgrav 		now = nanoseconds();
358b61860adSDag-Erling Smørgrav 		acqtime = m->mtx_acqtime;
359b61860adSDag-Erling Smørgrav 		m->mtx_acqtime = 0;
360b784ffe9SDag-Erling Smørgrav 		if (now <= acqtime)
3616c35e809SDag-Erling Smørgrav 			goto out;
362b61860adSDag-Erling Smørgrav 		for (p = m->mtx_filename; strncmp(p, "../", 3) == 0; p += 3)
3636c35e809SDag-Erling Smørgrav 			/* nothing */ ;
3646c35e809SDag-Erling Smørgrav 		if (p == NULL || *p == '\0')
3656c35e809SDag-Erling Smørgrav 			p = unknown;
366b61860adSDag-Erling Smørgrav 		for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q)
3676c35e809SDag-Erling Smørgrav 			hash = (hash * 2 + *q) % MPROF_HASH_SIZE;
3686c35e809SDag-Erling Smørgrav 		mtx_lock_spin(&mprof_mtx);
369e6330704SDag-Erling Smørgrav 		for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next)
370b61860adSDag-Erling Smørgrav 			if (mpp->line == m->mtx_lineno &&
371b61860adSDag-Erling Smørgrav 			    strcmp(mpp->file, p) == 0)
3726c35e809SDag-Erling Smørgrav 				break;
3736c35e809SDag-Erling Smørgrav 		if (mpp == NULL) {
3746c35e809SDag-Erling Smørgrav 			/* Just exit if we cannot get a trace buffer */
3756c35e809SDag-Erling Smørgrav 			if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) {
3766c35e809SDag-Erling Smørgrav 				++mutex_prof_rejected;
3776c35e809SDag-Erling Smørgrav 				goto unlock;
3786c35e809SDag-Erling Smørgrav 			}
3796c35e809SDag-Erling Smørgrav 			mpp = &mprof_buf[first_free_mprof_buf++];
3806c35e809SDag-Erling Smørgrav 			mpp->name = mtx_name(m);
3816c35e809SDag-Erling Smørgrav 			mpp->file = p;
382b61860adSDag-Erling Smørgrav 			mpp->line = m->mtx_lineno;
383e6330704SDag-Erling Smørgrav 			mpp->next = mprof_hash[hash];
384e6330704SDag-Erling Smørgrav 			if (mprof_hash[hash] != NULL)
385e6330704SDag-Erling Smørgrav 				++mutex_prof_collisions;
3866c35e809SDag-Erling Smørgrav 			mprof_hash[hash] = mpp;
387e6330704SDag-Erling Smørgrav 			++mutex_prof_records;
3886c35e809SDag-Erling Smørgrav 		}
3896c35e809SDag-Erling Smørgrav 		/*
3906c35e809SDag-Erling Smørgrav 		 * Record if the mutex has been held longer now than ever
3916c35e809SDag-Erling Smørgrav 		 * before
3926c35e809SDag-Erling Smørgrav 		 */
393b784ffe9SDag-Erling Smørgrav 		if ((now - acqtime) > mpp->counter[MPROF_MAX])
394b784ffe9SDag-Erling Smørgrav 			mpp->counter[MPROF_MAX] = now - acqtime;
395b784ffe9SDag-Erling Smørgrav 		mpp->counter[MPROF_TOT] += now - acqtime;
396b784ffe9SDag-Erling Smørgrav 		mpp->counter[MPROF_CNT] += 1;
397b784ffe9SDag-Erling Smørgrav 		mpp->counter[MPROF_AVG] =
398b784ffe9SDag-Erling Smørgrav 		    mpp->counter[MPROF_TOT] / mpp->counter[MPROF_CNT];
3996c35e809SDag-Erling Smørgrav unlock:
4006c35e809SDag-Erling Smørgrav 		mtx_unlock_spin(&mprof_mtx);
4016c35e809SDag-Erling Smørgrav 	}
4026c35e809SDag-Erling Smørgrav out:
4036c35e809SDag-Erling Smørgrav #endif
404dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
4056283b7d0SJohn Baldwin }
4066283b7d0SJohn Baldwin 
4076283b7d0SJohn Baldwin void
4086283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
4096283b7d0SJohn Baldwin {
4106283b7d0SJohn Baldwin 
411dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
4120d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
4130d975d63SJohn Baldwin 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
4140d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
415ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1
416dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
417e8fdcfb5SJohn Baldwin #else
418e8fdcfb5SJohn Baldwin 	critical_enter();
419e8fdcfb5SJohn Baldwin #endif
420dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
421dde96c99SJohn Baldwin 	    line);
422dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
4236283b7d0SJohn Baldwin }
4246283b7d0SJohn Baldwin 
4256283b7d0SJohn Baldwin void
4266283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
4276283b7d0SJohn Baldwin {
4286283b7d0SJohn Baldwin 
429dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
4300d975d63SJohn Baldwin 	KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin,
4310d975d63SJohn Baldwin 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
4320d975d63SJohn Baldwin 	    m->mtx_object.lo_name, file, line));
433dde96c99SJohn Baldwin  	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
434dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
435dde96c99SJohn Baldwin 	    line);
4360d975d63SJohn Baldwin 	mtx_assert(m, MA_OWNED);
437ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1
438dde96c99SJohn Baldwin 	_rel_spin_lock(m);
439e8fdcfb5SJohn Baldwin #else
440e8fdcfb5SJohn Baldwin 	critical_exit();
441e8fdcfb5SJohn Baldwin #endif
4426283b7d0SJohn Baldwin }
4436283b7d0SJohn Baldwin 
4446283b7d0SJohn Baldwin /*
4459ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
4469ed346baSBosko Milekic  * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that
4479ed346baSBosko Milekic  * if we're called, it's because we know we don't already own this lock.
4480cde2e34SJason Evans  */
4490cde2e34SJason Evans int
4509ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
4510cde2e34SJason Evans {
4520cde2e34SJason Evans 	int rval;
4530cde2e34SJason Evans 
454b40ce416SJulian Elischer 	MPASS(curthread != NULL);
4559ed346baSBosko Milekic 
456b40ce416SJulian Elischer 	rval = _obtain_lock(m, curthread);
4579ed346baSBosko Milekic 
45819284646SJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
45919284646SJohn Baldwin 	if (rval) {
4609ed346baSBosko Milekic 		/*
4619ed346baSBosko Milekic 		 * We do not handle recursion in _mtx_trylock; see the
4629ed346baSBosko Milekic 		 * note at the top of the routine.
4639ed346baSBosko Milekic 		 */
4645746a1d8SBosko Milekic 		KASSERT(!mtx_recursed(m),
4655746a1d8SBosko Milekic 		    ("mtx_trylock() called on a recursed mutex"));
4662d96f0b1SJohn Baldwin 		WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4672d96f0b1SJohn Baldwin 		    file, line);
4680cde2e34SJason Evans 	}
4699ed346baSBosko Milekic 
47019284646SJohn Baldwin 	return (rval);
4710cde2e34SJason Evans }
4720cde2e34SJason Evans 
4730cde2e34SJason Evans /*
4749ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4759ed346baSBosko Milekic  *
4769ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4779ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4780cde2e34SJason Evans  */
4790cde2e34SJason Evans void
4809ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line)
48136412d79SJohn Baldwin {
482b40ce416SJulian Elischer 	struct thread *td = curthread;
4832498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
4842498cf8cSJohn Baldwin 	struct thread *owner;
4852498cf8cSJohn Baldwin #endif
48602bd1bcdSIan Dowse #ifdef KTR
48702bd1bcdSIan Dowse 	int cont_logged = 0;
48802bd1bcdSIan Dowse #endif
48936412d79SJohn Baldwin 
490b40ce416SJulian Elischer 	if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)td) {
49136412d79SJohn Baldwin 		m->mtx_recurse++;
49208812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
49319284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4945746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
49536412d79SJohn Baldwin 		return;
49636412d79SJohn Baldwin 	}
4979ed346baSBosko Milekic 
49819284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
49915ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
50015ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
50119284646SJohn Baldwin 		    m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
5021bd0eefbSJohn Baldwin 
503b40ce416SJulian Elischer 	while (!_obtain_lock(m, td)) {
504f5271ebcSJohn Baldwin 		uintptr_t v;
505b40ce416SJulian Elischer 		struct thread *td1;
50636412d79SJohn Baldwin 
5079ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
50836412d79SJohn Baldwin 		/*
5099ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
5109ed346baSBosko Milekic 		 * the sched_lock.
51136412d79SJohn Baldwin 		 */
51236412d79SJohn Baldwin 		if ((v = m->mtx_lock) == MTX_UNOWNED) {
5139ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
514703fc290SJohn Baldwin #ifdef __i386__
5156b8c6989SJohn Baldwin 			ia32_pause();
516703fc290SJohn Baldwin #endif
51736412d79SJohn Baldwin 			continue;
51836412d79SJohn Baldwin 		}
5199ed346baSBosko Milekic 
52036412d79SJohn Baldwin 		/*
5219ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
522b40ce416SJulian Elischer 		 * there are threads blocked on it.
52336412d79SJohn Baldwin 		 */
52436412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
525b40ce416SJulian Elischer 			td1 = TAILQ_FIRST(&m->mtx_blocked);
526b40ce416SJulian Elischer 			MPASS(td1 != NULL);
527b40ce416SJulian Elischer 			m->mtx_lock = (uintptr_t)td | MTX_CONTESTED;
5289ed346baSBosko Milekic 
5292c100766SJulian Elischer 			if (td1->td_priority < td->td_priority)
5302c100766SJulian Elischer 				td->td_priority = td1->td_priority;
5319ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
53236412d79SJohn Baldwin 			return;
53336412d79SJohn Baldwin 		}
5349ed346baSBosko Milekic 
53536412d79SJohn Baldwin 		/*
5369ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
5379ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
5389ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
53936412d79SJohn Baldwin 		 */
54036412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
54136412d79SJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, (void *)v,
54236412d79SJohn Baldwin 			(void *)(v | MTX_CONTESTED))) {
5439ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
544703fc290SJohn Baldwin #ifdef __i386__
5456b8c6989SJohn Baldwin 			ia32_pause();
546703fc290SJohn Baldwin #endif
54736412d79SJohn Baldwin 			continue;
54836412d79SJohn Baldwin 		}
54936412d79SJohn Baldwin 
5502498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
5512498cf8cSJohn Baldwin 		/*
5522498cf8cSJohn Baldwin 		 * If the current owner of the lock is executing on another
5532498cf8cSJohn Baldwin 		 * CPU, spin instead of blocking.
5542498cf8cSJohn Baldwin 		 */
5552498cf8cSJohn Baldwin 		owner = (struct thread *)(v & MTX_FLAGMASK);
5566a95e08fSJohn Baldwin 		if (m != &Giant && thread_running(owner)) {
5572498cf8cSJohn Baldwin 			mtx_unlock_spin(&sched_lock);
5586a95e08fSJohn Baldwin 			while (mtx_owner(m) == owner && thread_running(owner)) {
559703fc290SJohn Baldwin #ifdef __i386__
5606b8c6989SJohn Baldwin 				ia32_pause();
561703fc290SJohn Baldwin #endif
5627fcca609SJohn Baldwin 			}
5632498cf8cSJohn Baldwin 			continue;
5642498cf8cSJohn Baldwin 		}
5652498cf8cSJohn Baldwin #endif	/* SMP && ADAPTIVE_MUTEXES */
5662498cf8cSJohn Baldwin 
5679ed346baSBosko Milekic 		/*
5687feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
5699ed346baSBosko Milekic 		 */
57036412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
57136412d79SJohn Baldwin 
57236412d79SJohn Baldwin #ifdef notyet
57336412d79SJohn Baldwin 		/*
5749ed346baSBosko Milekic 		 * If we're borrowing an interrupted thread's VM context, we
5759ed346baSBosko Milekic 		 * must clean up before going to sleep.
57636412d79SJohn Baldwin 		 */
577b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
578b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
57936412d79SJohn Baldwin 
58036412d79SJohn Baldwin 			if (it->it_interrupted) {
58119284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
58236412d79SJohn Baldwin 					CTR2(KTR_LOCK,
58315ec816aSJohn Baldwin 				    "_mtx_lock_sleep: %p interrupted %p",
58436412d79SJohn Baldwin 					    it, it->it_interrupted);
58536412d79SJohn Baldwin 				intr_thd_fixup(it);
58636412d79SJohn Baldwin 			}
58736412d79SJohn Baldwin 		}
58836412d79SJohn Baldwin #endif
58936412d79SJohn Baldwin 
5909ed346baSBosko Milekic 		/*
5919ed346baSBosko Milekic 		 * Put us on the list of threads blocked on this mutex.
5929ed346baSBosko Milekic 		 */
59336412d79SJohn Baldwin 		if (TAILQ_EMPTY(&m->mtx_blocked)) {
59418fc2ba9SJohn Baldwin 			td1 = mtx_owner(m);
595b40ce416SJulian Elischer 			LIST_INSERT_HEAD(&td1->td_contested, m, mtx_contested);
596b40ce416SJulian Elischer 			TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq);
59736412d79SJohn Baldwin 		} else {
598b40ce416SJulian Elischer 			TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq)
5992c100766SJulian Elischer 				if (td1->td_priority > td->td_priority)
60036412d79SJohn Baldwin 					break;
601b40ce416SJulian Elischer 			if (td1)
602b40ce416SJulian Elischer 				TAILQ_INSERT_BEFORE(td1, td, td_blkq);
60336412d79SJohn Baldwin 			else
604b40ce416SJulian Elischer 				TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq);
60536412d79SJohn Baldwin 		}
60602bd1bcdSIan Dowse #ifdef KTR
60702bd1bcdSIan Dowse 		if (!cont_logged) {
60802bd1bcdSIan Dowse 			CTR6(KTR_CONTENTION,
60902bd1bcdSIan Dowse 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
61002bd1bcdSIan Dowse 			    td, file, line, m->mtx_object.lo_name,
61102bd1bcdSIan Dowse 			    WITNESS_FILE(&m->mtx_object),
61202bd1bcdSIan Dowse 			    WITNESS_LINE(&m->mtx_object));
61302bd1bcdSIan Dowse 			cont_logged = 1;
61402bd1bcdSIan Dowse 		}
61502bd1bcdSIan Dowse #endif
61636412d79SJohn Baldwin 
6179ed346baSBosko Milekic 		/*
6189ed346baSBosko Milekic 		 * Save who we're blocked on.
6199ed346baSBosko Milekic 		 */
620b40ce416SJulian Elischer 		td->td_blocked = m;
621b40ce416SJulian Elischer 		td->td_mtxname = m->mtx_object.lo_name;
622e602ba25SJulian Elischer 		td->td_state = TDS_MTX;
623b40ce416SJulian Elischer 		propagate_priority(td);
6249ed346baSBosko Milekic 
62519284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
626562e4ffeSJohn Baldwin 			CTR3(KTR_LOCK,
627b40ce416SJulian Elischer 			    "_mtx_lock_sleep: p %p blocked on [%p] %s", td, m,
62819284646SJohn Baldwin 			    m->mtx_object.lo_name);
6299ed346baSBosko Milekic 
630b40ce416SJulian Elischer 		td->td_proc->p_stats->p_ru.ru_nvcsw++;
63120cdcc5bSJohn Baldwin 		mi_switch();
6329ed346baSBosko Milekic 
63319284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
63436412d79SJohn Baldwin 			CTR3(KTR_LOCK,
6359ed346baSBosko Milekic 			  "_mtx_lock_sleep: p %p free from blocked on [%p] %s",
636b40ce416SJulian Elischer 			  td, m, m->mtx_object.lo_name);
6379ed346baSBosko Milekic 
6389ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
63936412d79SJohn Baldwin 	}
6409ed346baSBosko Milekic 
64102bd1bcdSIan Dowse #ifdef KTR
64202bd1bcdSIan Dowse 	if (cont_logged) {
64302bd1bcdSIan Dowse 		CTR4(KTR_CONTENTION,
64402bd1bcdSIan Dowse 		    "contention end: %s acquired by %p at %s:%d",
64502bd1bcdSIan Dowse 		    m->mtx_object.lo_name, td, file, line);
64602bd1bcdSIan Dowse 	}
64702bd1bcdSIan Dowse #endif
64836412d79SJohn Baldwin 	return;
6499ed346baSBosko Milekic }
6509ed346baSBosko Milekic 
6519ed346baSBosko Milekic /*
6529ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
6539ed346baSBosko Milekic  *
6549ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
6559ed346baSBosko Milekic  * is handled inline.
6569ed346baSBosko Milekic  */
6579ed346baSBosko Milekic void
6587e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line)
65936412d79SJohn Baldwin {
66036412d79SJohn Baldwin 	int i = 0;
66136412d79SJohn Baldwin 
66219284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6635746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
6649ed346baSBosko Milekic 
66536412d79SJohn Baldwin 	for (;;) {
666b40ce416SJulian Elischer 		if (_obtain_lock(m, curthread))
66736412d79SJohn Baldwin 			break;
6689ed346baSBosko Milekic 
6697141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
6707e1f6dfeSJohn Baldwin 		critical_exit();
67136412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
672703fc290SJohn Baldwin 			if (i++ < 10000000) {
673703fc290SJohn Baldwin #ifdef __i386__
6746b8c6989SJohn Baldwin 				ia32_pause();
675703fc290SJohn Baldwin #endif
67636412d79SJohn Baldwin 				continue;
677703fc290SJohn Baldwin 			}
6780e54ddadSJohn Baldwin 			if (i < 60000000)
67936412d79SJohn Baldwin 				DELAY(1);
68036412d79SJohn Baldwin #ifdef DDB
68136412d79SJohn Baldwin 			else if (!db_active)
68236412d79SJohn Baldwin #else
68336412d79SJohn Baldwin 			else
68436412d79SJohn Baldwin #endif
6859ed346baSBosko Milekic 				panic("spin lock %s held by %p for > 5 seconds",
68619284646SJohn Baldwin 				    m->mtx_object.lo_name, (void *)m->mtx_lock);
687703fc290SJohn Baldwin #ifdef __i386__
6886b8c6989SJohn Baldwin 			ia32_pause();
689703fc290SJohn Baldwin #endif
69036412d79SJohn Baldwin 		}
6917e1f6dfeSJohn Baldwin 		critical_enter();
69236412d79SJohn Baldwin 	}
69336412d79SJohn Baldwin 
69419284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6959ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
6969ed346baSBosko Milekic 
69736412d79SJohn Baldwin 	return;
69836412d79SJohn Baldwin }
69936412d79SJohn Baldwin 
7009ed346baSBosko Milekic /*
7019ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
7029ed346baSBosko Milekic  *
7039ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
7049ed346baSBosko Milekic  * need to wake up a blocked thread).
7059ed346baSBosko Milekic  */
70636412d79SJohn Baldwin void
7079ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
70836412d79SJohn Baldwin {
709b40ce416SJulian Elischer 	struct thread *td, *td1;
71036412d79SJohn Baldwin 	struct mtx *m1;
71136412d79SJohn Baldwin 	int pri;
71236412d79SJohn Baldwin 
713b40ce416SJulian Elischer 	td = curthread;
7149ed346baSBosko Milekic 
71508812b39SBosko Milekic 	if (mtx_recursed(m)) {
71636412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
71708812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
71819284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7199ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
72036412d79SJohn Baldwin 		return;
72136412d79SJohn Baldwin 	}
7229ed346baSBosko Milekic 
7239ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
72419284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
7259ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
7269ed346baSBosko Milekic 
727b40ce416SJulian Elischer 	td1 = TAILQ_FIRST(&m->mtx_blocked);
7282498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
7292498cf8cSJohn Baldwin 	if (td1 == NULL) {
7302498cf8cSJohn Baldwin 		_release_lock_quick(m);
7312498cf8cSJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7322498cf8cSJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
7332498cf8cSJohn Baldwin 		mtx_unlock_spin(&sched_lock);
7342498cf8cSJohn Baldwin 		return;
7352498cf8cSJohn Baldwin 	}
7362498cf8cSJohn Baldwin #endif
737b40ce416SJulian Elischer 	MPASS(td->td_proc->p_magic == P_MAGIC);
738b40ce416SJulian Elischer 	MPASS(td1->td_proc->p_magic == P_MAGIC);
7399ed346baSBosko Milekic 
740b40ce416SJulian Elischer 	TAILQ_REMOVE(&m->mtx_blocked, td1, td_blkq);
7419ed346baSBosko Milekic 
74236412d79SJohn Baldwin 	if (TAILQ_EMPTY(&m->mtx_blocked)) {
74336412d79SJohn Baldwin 		LIST_REMOVE(m, mtx_contested);
74436412d79SJohn Baldwin 		_release_lock_quick(m);
74519284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7469ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
74736412d79SJohn Baldwin 	} else
7489ed346baSBosko Milekic 		atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED);
7499ed346baSBosko Milekic 
750d5a08a60SJake Burkholder 	pri = PRI_MAX;
751b40ce416SJulian Elischer 	LIST_FOREACH(m1, &td->td_contested, mtx_contested) {
7522c100766SJulian Elischer 		int cp = TAILQ_FIRST(&m1->mtx_blocked)->td_priority;
75336412d79SJohn Baldwin 		if (cp < pri)
75436412d79SJohn Baldwin 			pri = cp;
75536412d79SJohn Baldwin 	}
7569ed346baSBosko Milekic 
7572c100766SJulian Elischer 	if (pri > td->td_base_pri)
7582c100766SJulian Elischer 		pri = td->td_base_pri;
7592c100766SJulian Elischer 	td->td_priority = pri;
7609ed346baSBosko Milekic 
76119284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
7629ed346baSBosko Milekic 		CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p",
763b40ce416SJulian Elischer 		    m, td1);
7649ed346baSBosko Milekic 
765b40ce416SJulian Elischer 	td1->td_blocked = NULL;
766b40ce416SJulian Elischer 	setrunqueue(td1);
7679ed346baSBosko Milekic 
7682c100766SJulian Elischer 	if (td->td_critnest == 1 && td1->td_priority < pri) {
76936412d79SJohn Baldwin #ifdef notyet
770b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
771b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
77236412d79SJohn Baldwin 
77336412d79SJohn Baldwin 			if (it->it_interrupted) {
77419284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
77536412d79SJohn Baldwin 					CTR2(KTR_LOCK,
77615ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
77736412d79SJohn Baldwin 					    it, it->it_interrupted);
77836412d79SJohn Baldwin 				intr_thd_fixup(it);
77936412d79SJohn Baldwin 			}
78036412d79SJohn Baldwin 		}
78136412d79SJohn Baldwin #endif
78219284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
783562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
7849ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
7859ed346baSBosko Milekic 			    (void *)m->mtx_lock);
7869ed346baSBosko Milekic 
787b40ce416SJulian Elischer 		td->td_proc->p_stats->p_ru.ru_nivcsw++;
78836412d79SJohn Baldwin 		mi_switch();
78919284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7909ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
79131271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
79236412d79SJohn Baldwin 	}
79336412d79SJohn Baldwin 
7949ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
7959ed346baSBosko Milekic 
7969ed346baSBosko Milekic 	return;
7979ed346baSBosko Milekic }
7989ed346baSBosko Milekic 
7999ed346baSBosko Milekic /*
8009ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
8019ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
8029ed346baSBosko Milekic  */
8039ed346baSBosko Milekic 
8049ed346baSBosko Milekic /*
80515ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
8069ed346baSBosko Milekic  */
8071103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
8080cde2e34SJason Evans void
80956771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
8100cde2e34SJason Evans {
8115cb0fbe4SJohn Baldwin 
8125cb0fbe4SJohn Baldwin 	if (panicstr != NULL)
8135cb0fbe4SJohn Baldwin 		return;
814a10f4966SJake Burkholder 	switch (what) {
8150cde2e34SJason Evans 	case MA_OWNED:
8160cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
8170cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
818a10f4966SJake Burkholder 		if (!mtx_owned(m))
8190cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
82019284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
821a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
822a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
8230cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
82419284646SJohn Baldwin 				    m->mtx_object.lo_name, file, line);
825a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
8260cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
82719284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
8280cde2e34SJason Evans 		}
8290cde2e34SJason Evans 		break;
8300cde2e34SJason Evans 	case MA_NOTOWNED:
831a10f4966SJake Burkholder 		if (mtx_owned(m))
8320cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
83319284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
8340cde2e34SJason Evans 		break;
8350cde2e34SJason Evans 	default:
83656771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
8370cde2e34SJason Evans 	}
8380cde2e34SJason Evans }
8390cde2e34SJason Evans #endif
8400cde2e34SJason Evans 
8419ed346baSBosko Milekic /*
8429ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
84319284646SJohn Baldwin  *
84419284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
84519284646SJohn Baldwin  * maintained by the witness code.
8469ed346baSBosko Milekic  */
84736412d79SJohn Baldwin #ifdef MUTEX_DEBUG
84836412d79SJohn Baldwin 
8494d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
85036412d79SJohn Baldwin 
85119284646SJohn Baldwin void
85219284646SJohn Baldwin mtx_validate(struct mtx *m)
85336412d79SJohn Baldwin {
85436412d79SJohn Baldwin 
85536412d79SJohn Baldwin /*
85636412d79SJohn Baldwin  * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
85736412d79SJohn Baldwin  * we can re-enable the kernacc() checks.
85836412d79SJohn Baldwin  */
85936412d79SJohn Baldwin #ifndef __alpha__
86076dcbd6fSBosko Milekic 	/*
86176dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
86276dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
86376dcbd6fSBosko Milekic 	 * requires Giant itself.
86476dcbd6fSBosko Milekic 	 */
865ab07087eSBosko Milekic 	if (!cold)
866ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
867ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
86819284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
86936412d79SJohn Baldwin #endif
87036412d79SJohn Baldwin }
87136412d79SJohn Baldwin #endif
87236412d79SJohn Baldwin 
8739ed346baSBosko Milekic /*
874c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
875c27b5699SAndrew R. Reiter  */
876c27b5699SAndrew R. Reiter void
877c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
878c27b5699SAndrew R. Reiter {
879c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
880c27b5699SAndrew R. Reiter 
8810c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
882c27b5699SAndrew R. Reiter }
883c27b5699SAndrew R. Reiter 
884c27b5699SAndrew R. Reiter /*
8859ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
8860c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
8870c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
8880c88508aSJohn Baldwin  * witness.
8899ed346baSBosko Milekic  */
89036412d79SJohn Baldwin void
8910c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
89236412d79SJohn Baldwin {
89319284646SJohn Baldwin 	struct lock_object *lock;
8949ed346baSBosko Milekic 
89519284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
896f22a4b62SJeff Roberson 	    MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0);
8979ed346baSBosko Milekic 
89836412d79SJohn Baldwin #ifdef MUTEX_DEBUG
8999ed346baSBosko Milekic 	/* Diagnostic and error correction */
90019284646SJohn Baldwin 	mtx_validate(m);
9016936206eSJohn Baldwin #endif
90236412d79SJohn Baldwin 
90319284646SJohn Baldwin 	lock = &m->mtx_object;
9047ada5876SJohn Baldwin 	KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
9050c88508aSJohn Baldwin 	    ("mutex %s %p already initialized", name, m));
9067ada5876SJohn Baldwin 	bzero(m, sizeof(*m));
90719284646SJohn Baldwin 	if (opts & MTX_SPIN)
90819284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_spin;
90919284646SJohn Baldwin 	else
91019284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_sleep;
9110c88508aSJohn Baldwin 	lock->lo_name = name;
9120c88508aSJohn Baldwin 	lock->lo_type = type != NULL ? type : name;
91319284646SJohn Baldwin 	if (opts & MTX_QUIET)
91419284646SJohn Baldwin 		lock->lo_flags = LO_QUIET;
91519284646SJohn Baldwin 	if (opts & MTX_RECURSE)
91619284646SJohn Baldwin 		lock->lo_flags |= LO_RECURSABLE;
91719284646SJohn Baldwin 	if (opts & MTX_SLEEPABLE)
91819284646SJohn Baldwin 		lock->lo_flags |= LO_SLEEPABLE;
91919284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
92019284646SJohn Baldwin 		lock->lo_flags |= LO_WITNESS;
921f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
922f22a4b62SJeff Roberson 		lock->lo_flags |= LO_DUPOK;
92319284646SJohn Baldwin 
92419284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
92536412d79SJohn Baldwin 	TAILQ_INIT(&m->mtx_blocked);
9269ed346baSBosko Milekic 
92719284646SJohn Baldwin 	LOCK_LOG_INIT(lock, opts);
928d1c1b841SJason Evans 
92919284646SJohn Baldwin 	WITNESS_INIT(lock);
93036412d79SJohn Baldwin }
93136412d79SJohn Baldwin 
9329ed346baSBosko Milekic /*
93319284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
93419284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
93519284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
93619284646SJohn Baldwin  * flags.
9379ed346baSBosko Milekic  */
93836412d79SJohn Baldwin void
93936412d79SJohn Baldwin mtx_destroy(struct mtx *m)
94036412d79SJohn Baldwin {
94136412d79SJohn Baldwin 
94219284646SJohn Baldwin 	LOCK_LOG_DESTROY(&m->mtx_object, 0);
9439ed346baSBosko Milekic 
94419284646SJohn Baldwin 	if (!mtx_owned(m))
94519284646SJohn Baldwin 		MPASS(mtx_unowned(m));
94619284646SJohn Baldwin 	else {
94708812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
9489ed346baSBosko Milekic 
94919284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
950c86b6ff5SJohn Baldwin 		WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
951c86b6ff5SJohn Baldwin 		    __LINE__);
95236412d79SJohn Baldwin 	}
9530384fff8SJason Evans 
95419284646SJohn Baldwin 	WITNESS_DESTROY(&m->mtx_object);
9550384fff8SJason Evans }
956d23f5958SMatthew Dillon 
957d23f5958SMatthew Dillon /*
958c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
959c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
960c53c013bSJohn Baldwin  * setup before this is called.
961c53c013bSJohn Baldwin  */
962c53c013bSJohn Baldwin void
963c53c013bSJohn Baldwin mutex_init(void)
964c53c013bSJohn Baldwin {
965c53c013bSJohn Baldwin 
966c53c013bSJohn Baldwin 	/* Setup thread0 so that mutexes work. */
967c53c013bSJohn Baldwin 	LIST_INIT(&thread0.td_contested);
968c53c013bSJohn Baldwin 
969c53c013bSJohn Baldwin 	/*
970c53c013bSJohn Baldwin 	 * Initialize mutexes.
971c53c013bSJohn Baldwin 	 */
9720c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
9730c88508aSJohn Baldwin 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
9740c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
975c53c013bSJohn Baldwin 	mtx_lock(&Giant);
976c53c013bSJohn Baldwin }
977c53c013bSJohn Baldwin 
978c53c013bSJohn Baldwin /*
979d23f5958SMatthew Dillon  * Encapsulated Giant mutex routines.  These routines provide encapsulation
980d23f5958SMatthew Dillon  * control for the Giant mutex, allowing sysctls to be used to turn on and
981d23f5958SMatthew Dillon  * off Giant around certain subsystems.  The default value for the sysctls
982d23f5958SMatthew Dillon  * are set to what developers believe is stable and working in regards to
983d23f5958SMatthew Dillon  * the Giant pushdown.  Developers should not turn off Giant via these
984d23f5958SMatthew Dillon  * sysctls unless they know what they are doing.
985d23f5958SMatthew Dillon  *
986d23f5958SMatthew Dillon  * Callers of mtx_lock_giant() are expected to pass the return value to an
987d23f5958SMatthew Dillon  * accompanying mtx_unlock_giant() later on.  If multiple subsystems are
988d23f5958SMatthew Dillon  * effected by a Giant wrap, all related sysctl variables must be zero for
989d23f5958SMatthew Dillon  * the subsystem call to operate without Giant (as determined by the caller).
990d23f5958SMatthew Dillon  */
991d23f5958SMatthew Dillon 
992d23f5958SMatthew Dillon SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation");
993d23f5958SMatthew Dillon 
994d23f5958SMatthew Dillon static int kern_giant_all = 0;
995d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, "");
996d23f5958SMatthew Dillon 
997d23f5958SMatthew Dillon int kern_giant_proc = 1;	/* Giant around PROC locks */
998d23f5958SMatthew Dillon int kern_giant_file = 1;	/* Giant around struct file & filedesc */
999735da6deSMatthew Dillon int kern_giant_ucred = 1;	/* Giant around ucred */
1000d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, "");
1001d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, "");
1002735da6deSMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, "");
1003d23f5958SMatthew Dillon 
1004d23f5958SMatthew Dillon int
1005d23f5958SMatthew Dillon mtx_lock_giant(int sysctlvar)
1006d23f5958SMatthew Dillon {
1007d23f5958SMatthew Dillon 	if (sysctlvar || kern_giant_all) {
1008d23f5958SMatthew Dillon 		mtx_lock(&Giant);
1009d23f5958SMatthew Dillon 		return(1);
1010d23f5958SMatthew Dillon 	}
1011d23f5958SMatthew Dillon 	return(0);
1012d23f5958SMatthew Dillon }
1013d23f5958SMatthew Dillon 
1014d23f5958SMatthew Dillon void
1015d23f5958SMatthew Dillon mtx_unlock_giant(int s)
1016d23f5958SMatthew Dillon {
1017d23f5958SMatthew Dillon 	if (s)
1018d23f5958SMatthew Dillon 		mtx_unlock(&Giant);
1019d23f5958SMatthew Dillon }
1020d23f5958SMatthew Dillon 
1021