xref: /freebsd/sys/kern/kern_mutex.c (revision 6c35e80948d3b56bd8da8142094c0951726450a9)
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 
379c36c934SJohn Baldwin #include "opt_ddb.h"
38a5a96a19SJohn Baldwin 
390384fff8SJason Evans #include <sys/param.h>
406c35e809SDag-Erling Smørgrav #include <sys/systm.h>
4136412d79SJohn Baldwin #include <sys/bus.h>
4236412d79SJohn Baldwin #include <sys/kernel.h>
436c35e809SDag-Erling Smørgrav #include <sys/ktr.h>
4419284646SJohn Baldwin #include <sys/lock.h>
45fb919e4dSMark Murray #include <sys/malloc.h>
4619284646SJohn Baldwin #include <sys/mutex.h>
470384fff8SJason Evans #include <sys/proc.h>
48c4f7a187SJohn Baldwin #include <sys/resourcevar.h>
496c35e809SDag-Erling Smørgrav #include <sys/sbuf.h>
50a5a96a19SJohn Baldwin #include <sys/sysctl.h>
5136412d79SJohn Baldwin #include <sys/vmmeter.h>
520384fff8SJason Evans 
5336412d79SJohn Baldwin #include <machine/atomic.h>
5436412d79SJohn Baldwin #include <machine/bus.h>
5536412d79SJohn Baldwin #include <machine/clock.h>
560384fff8SJason Evans #include <machine/cpu.h>
5736412d79SJohn Baldwin 
589c36c934SJohn Baldwin #include <ddb/ddb.h>
599c36c934SJohn Baldwin 
6036412d79SJohn Baldwin #include <vm/vm.h>
6136412d79SJohn Baldwin #include <vm/vm_extern.h>
6236412d79SJohn Baldwin 
630cde2e34SJason Evans /*
649ed346baSBosko Milekic  * Internal utility macros.
650cde2e34SJason Evans  */
669ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
670cde2e34SJason Evans 
689ed346baSBosko Milekic #define mtx_owner(m)	(mtx_unowned((m)) ? NULL \
69b40ce416SJulian Elischer 	: (struct thread *)((m)->mtx_lock & MTX_FLAGMASK))
709ed346baSBosko Milekic 
710cde2e34SJason Evans /*
7219284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
730cde2e34SJason Evans  */
7419284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
7519284646SJohn Baldwin 	"sleep mutex",
7619284646SJohn Baldwin 	LC_SLEEPLOCK | LC_RECURSABLE
7719284646SJohn Baldwin };
7819284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
7919284646SJohn Baldwin 	"spin mutex",
8019284646SJohn Baldwin 	LC_SPINLOCK | LC_RECURSABLE
818484de75SJohn Baldwin };
828484de75SJohn Baldwin 
839ed346baSBosko Milekic /*
849ed346baSBosko Milekic  * Prototypes for non-exported routines.
859ed346baSBosko Milekic  */
86b40ce416SJulian Elischer static void	propagate_priority(struct thread *);
8736412d79SJohn Baldwin 
8836412d79SJohn Baldwin static void
89b40ce416SJulian Elischer propagate_priority(struct thread *td)
9036412d79SJohn Baldwin {
912c100766SJulian Elischer 	int pri = td->td_priority;
92b40ce416SJulian Elischer 	struct mtx *m = td->td_blocked;
9336412d79SJohn Baldwin 
941bd0eefbSJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
9536412d79SJohn Baldwin 	for (;;) {
96b40ce416SJulian Elischer 		struct thread *td1;
9736412d79SJohn Baldwin 
98b40ce416SJulian Elischer 		td = mtx_owner(m);
9936412d79SJohn Baldwin 
100b40ce416SJulian Elischer 		if (td == NULL) {
10136412d79SJohn Baldwin 			/*
10236412d79SJohn Baldwin 			 * This really isn't quite right. Really
103b40ce416SJulian Elischer 			 * ought to bump priority of thread that
10436412d79SJohn Baldwin 			 * next acquires the mutex.
10536412d79SJohn Baldwin 			 */
10636412d79SJohn Baldwin 			MPASS(m->mtx_lock == MTX_CONTESTED);
10736412d79SJohn Baldwin 			return;
10836412d79SJohn Baldwin 		}
1099ed346baSBosko Milekic 
110b40ce416SJulian Elischer 		MPASS(td->td_proc->p_magic == P_MAGIC);
111b40ce416SJulian Elischer 		KASSERT(td->td_proc->p_stat != SSLEEP, ("sleeping thread owns a mutex"));
1122c100766SJulian Elischer 		if (td->td_priority <= pri) /* lower is higher priority */
11336412d79SJohn Baldwin 			return;
1141bd0eefbSJohn Baldwin 
1151bd0eefbSJohn Baldwin 		/*
116b40ce416SJulian Elischer 		 * Bump this thread's priority.
1171bd0eefbSJohn Baldwin 		 */
1182c100766SJulian Elischer 		td->td_priority = pri;
1191bd0eefbSJohn Baldwin 
12036412d79SJohn Baldwin 		/*
12136412d79SJohn Baldwin 		 * If lock holder is actually running, just bump priority.
12236412d79SJohn Baldwin 		 */
123b40ce416SJulian Elischer 		 /* XXXKSE this test is not sufficient */
124b40ce416SJulian Elischer 		if (td->td_kse && (td->td_kse->ke_oncpu != NOCPU)) {
125b40ce416SJulian Elischer 			MPASS(td->td_proc->p_stat == SRUN
126b40ce416SJulian Elischer 			|| td->td_proc->p_stat == SZOMB
127b40ce416SJulian Elischer 			|| td->td_proc->p_stat == SSTOP);
12836412d79SJohn Baldwin 			return;
12936412d79SJohn Baldwin 		}
130d5a08a60SJake Burkholder 
1311b43703bSJohn Baldwin #ifndef SMP
1321b43703bSJohn Baldwin 		/*
133b40ce416SJulian Elischer 		 * For UP, we check to see if td is curthread (this shouldn't
1341b43703bSJohn Baldwin 		 * ever happen however as it would mean we are in a deadlock.)
1351b43703bSJohn Baldwin 		 */
136b40ce416SJulian Elischer 		KASSERT(td != curthread, ("Deadlock detected"));
1371b43703bSJohn Baldwin #endif
1381b43703bSJohn Baldwin 
13936412d79SJohn Baldwin 		/*
140b40ce416SJulian Elischer 		 * If on run queue move to new run queue, and quit.
141b40ce416SJulian Elischer 		 * XXXKSE this gets a lot more complicated under threads
142b40ce416SJulian Elischer 		 * but try anyhow.
14336412d79SJohn Baldwin 		 */
144b40ce416SJulian Elischer 		if (td->td_proc->p_stat == SRUN) {
145b40ce416SJulian Elischer 			MPASS(td->td_blocked == NULL);
146b40ce416SJulian Elischer 			remrunqueue(td);
147b40ce416SJulian Elischer 			setrunqueue(td);
14836412d79SJohn Baldwin 			return;
14936412d79SJohn Baldwin 		}
15036412d79SJohn Baldwin 
15136412d79SJohn Baldwin 		/*
1521bd0eefbSJohn Baldwin 		 * If we aren't blocked on a mutex, we should be.
15336412d79SJohn Baldwin 		 */
154b40ce416SJulian Elischer 		KASSERT(td->td_proc->p_stat == SMTX, (
1551bd0eefbSJohn Baldwin 		    "process %d(%s):%d holds %s but isn't blocked on a mutex\n",
156b40ce416SJulian Elischer 		    td->td_proc->p_pid, td->td_proc->p_comm, td->td_proc->p_stat,
15719284646SJohn Baldwin 		    m->mtx_object.lo_name));
15836412d79SJohn Baldwin 
15936412d79SJohn Baldwin 		/*
160b40ce416SJulian Elischer 		 * Pick up the mutex that td is blocked on.
16136412d79SJohn Baldwin 		 */
162b40ce416SJulian Elischer 		m = td->td_blocked;
16336412d79SJohn Baldwin 		MPASS(m != NULL);
16436412d79SJohn Baldwin 
16536412d79SJohn Baldwin 		/*
166b40ce416SJulian Elischer 		 * Check if the thread needs to be moved up on
16736412d79SJohn Baldwin 		 * the blocked chain
16836412d79SJohn Baldwin 		 */
169b40ce416SJulian Elischer 		if (td == TAILQ_FIRST(&m->mtx_blocked)) {
1701bd0eefbSJohn Baldwin 			continue;
1711bd0eefbSJohn Baldwin 		}
1729ed346baSBosko Milekic 
173b40ce416SJulian Elischer 		td1 = TAILQ_PREV(td, threadqueue, td_blkq);
1742c100766SJulian Elischer 		if (td1->td_priority <= pri) {
17536412d79SJohn Baldwin 			continue;
17636412d79SJohn Baldwin 		}
17736412d79SJohn Baldwin 
17836412d79SJohn Baldwin 		/*
179b40ce416SJulian Elischer 		 * Remove thread from blocked chain and determine where
180b40ce416SJulian Elischer 		 * it should be moved up to.  Since we know that td1 has
181b40ce416SJulian Elischer 		 * a lower priority than td, we know that at least one
182b40ce416SJulian Elischer 		 * thread in the chain has a lower priority and that
183b40ce416SJulian Elischer 		 * td1 will thus not be NULL after the loop.
18436412d79SJohn Baldwin 		 */
185b40ce416SJulian Elischer 		TAILQ_REMOVE(&m->mtx_blocked, td, td_blkq);
186b40ce416SJulian Elischer 		TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq) {
187b40ce416SJulian Elischer 			MPASS(td1->td_proc->p_magic == P_MAGIC);
1882c100766SJulian Elischer 			if (td1->td_priority > pri)
18936412d79SJohn Baldwin 				break;
19036412d79SJohn Baldwin 		}
1919ed346baSBosko Milekic 
192b40ce416SJulian Elischer 		MPASS(td1 != NULL);
193b40ce416SJulian Elischer 		TAILQ_INSERT_BEFORE(td1, td, td_blkq);
19436412d79SJohn Baldwin 		CTR4(KTR_LOCK,
1958484de75SJohn Baldwin 		    "propagate_priority: p %p moved before %p on [%p] %s",
196b40ce416SJulian Elischer 		    td, td1, m, m->mtx_object.lo_name);
19736412d79SJohn Baldwin 	}
19836412d79SJohn Baldwin }
19936412d79SJohn Baldwin 
2006c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
2016c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging");
2026c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling");
2036c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0;
2046c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW,
2056c35e809SDag-Erling Smørgrav     &mutex_prof_enable, 0, "Enable tracing of mutex holdtime");
2066c35e809SDag-Erling Smørgrav 
2076c35e809SDag-Erling Smørgrav struct mutex_prof {
2086c35e809SDag-Erling Smørgrav 	const char *name;
2096c35e809SDag-Erling Smørgrav 	const char *file;
2106c35e809SDag-Erling Smørgrav 	int line;
2116c35e809SDag-Erling Smørgrav #define MPROF_MAX 0
2126c35e809SDag-Erling Smørgrav #define MPROF_TOT 1
2136c35e809SDag-Erling Smørgrav #define MPROF_CNT 2
2146c35e809SDag-Erling Smørgrav #define MPROF_AVG 3
2156c35e809SDag-Erling Smørgrav 	u_int64_t cycles[4];
2166c35e809SDag-Erling Smørgrav };
2176c35e809SDag-Erling Smørgrav 
2186c35e809SDag-Erling Smørgrav /*
2196c35e809SDag-Erling Smørgrav  * mprof_buf is a static pool of profiling records to avoid possible
2206c35e809SDag-Erling Smørgrav  * reentrance of the memory allocation functions.
2216c35e809SDag-Erling Smørgrav  *
2226c35e809SDag-Erling Smørgrav  * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE.
2236c35e809SDag-Erling Smørgrav  */
2246c35e809SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 4096
2256c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS];
2266c35e809SDag-Erling Smørgrav static int first_free_mprof_buf;
2276c35e809SDag-Erling Smørgrav #define MPROF_HASH_SIZE 32771
2286c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE];
2296c35e809SDag-Erling Smørgrav 
2306c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions;
2316c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD,
2326c35e809SDag-Erling Smørgrav     &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded");
2336c35e809SDag-Erling Smørgrav static int mutex_prof_records;
2346c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD,
2356c35e809SDag-Erling Smørgrav     &mutex_prof_records, 0, "Number of profiling records");
2366c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS;
2376c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD,
2386c35e809SDag-Erling Smørgrav     &mutex_prof_maxrecords, 0, "Maximum number of profiling records");
2396c35e809SDag-Erling Smørgrav static int mutex_prof_rejected;
2406c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD,
2416c35e809SDag-Erling Smørgrav     &mutex_prof_rejected, 0, "Number of rejected profiling records");
2426c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE;
2436c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD,
2446c35e809SDag-Erling Smørgrav     &mutex_prof_hashsize, 0, "Hash size");
2456c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0;
2466c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD,
2476c35e809SDag-Erling Smørgrav     &mutex_prof_collisions, 0, "Number of hash collisions");
2486c35e809SDag-Erling Smørgrav 
2496c35e809SDag-Erling Smørgrav /*
2506c35e809SDag-Erling Smørgrav  * mprof_mtx protects the profiling buffers and the hash.
2516c35e809SDag-Erling Smørgrav  */
2526c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx;
2536c35e809SDag-Erling Smørgrav 
2546c35e809SDag-Erling Smørgrav static void
2556c35e809SDag-Erling Smørgrav mprof_init(void *arg __unused)
2566c35e809SDag-Erling Smørgrav {
2576c35e809SDag-Erling Smørgrav 	mtx_init(&mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET);
2586c35e809SDag-Erling Smørgrav }
2596c35e809SDag-Erling Smørgrav SYSINIT(mprofinit, SI_SUB_LOCK, SI_ORDER_ANY, mprof_init, NULL);
2606c35e809SDag-Erling Smørgrav 
2616c35e809SDag-Erling Smørgrav static int
2626c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
2636c35e809SDag-Erling Smørgrav {
2646c35e809SDag-Erling Smørgrav 	struct sbuf *sb;
2656c35e809SDag-Erling Smørgrav 	int error, i;
2666c35e809SDag-Erling Smørgrav 
2676c35e809SDag-Erling Smørgrav 	if (first_free_mprof_buf == 0)
2686c35e809SDag-Erling Smørgrav 		return SYSCTL_OUT(req, "No locking recorded",
2696c35e809SDag-Erling Smørgrav 		    sizeof("No locking recorded"));
2706c35e809SDag-Erling Smørgrav 
2716c35e809SDag-Erling Smørgrav 	sb = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND);
2726c35e809SDag-Erling Smørgrav 	sbuf_printf(sb, "%12s %12s %12s %12s %s\n",
2736c35e809SDag-Erling Smørgrav 	    "max", "total", "count", "average", "name");
2746c35e809SDag-Erling Smørgrav 	mtx_lock_spin(&mprof_mtx);
2756c35e809SDag-Erling Smørgrav 	for (i = 0; i < first_free_mprof_buf; ++i)
2766c35e809SDag-Erling Smørgrav 		sbuf_printf(sb, "%12llu %12llu %12llu %12llu %s:%d (%s)\n",
2776c35e809SDag-Erling Smørgrav 		    mprof_buf[i].cycles[MPROF_MAX],
2786c35e809SDag-Erling Smørgrav 		    mprof_buf[i].cycles[MPROF_TOT],
2796c35e809SDag-Erling Smørgrav 		    mprof_buf[i].cycles[MPROF_CNT],
2806c35e809SDag-Erling Smørgrav 		    mprof_buf[i].cycles[MPROF_AVG],
2816c35e809SDag-Erling Smørgrav 		    mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name);
2826c35e809SDag-Erling Smørgrav 	mtx_unlock_spin(&mprof_mtx);
2836c35e809SDag-Erling Smørgrav 	sbuf_finish(sb);
2846c35e809SDag-Erling Smørgrav 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2856c35e809SDag-Erling Smørgrav 	sbuf_delete(sb);
2866c35e809SDag-Erling Smørgrav 	return (error);
2876c35e809SDag-Erling Smørgrav }
2886c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING|CTLFLAG_RD,
2896c35e809SDag-Erling Smørgrav     NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics");
2906c35e809SDag-Erling Smørgrav #endif
2916c35e809SDag-Erling Smørgrav 
2920cde2e34SJason Evans /*
2936283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
2946283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
2956283b7d0SJohn Baldwin  */
2966283b7d0SJohn Baldwin void
2976283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
2986283b7d0SJohn Baldwin {
2996283b7d0SJohn Baldwin 
300dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
301dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
302dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
303dde96c99SJohn Baldwin 	    line);
304dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3056c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
3066c35e809SDag-Erling Smørgrav 	/* don't reset the timer when/if recursing */
3076c35e809SDag-Erling Smørgrav 	if (m->cycles == 0) {
3086c35e809SDag-Erling Smørgrav 		m->file = file;
3096c35e809SDag-Erling Smørgrav 		m->line = line;
3106c35e809SDag-Erling Smørgrav 		m->cycles = mutex_prof_enable ? get_cyclecount() : 0;
3116c35e809SDag-Erling Smørgrav 		++mutex_prof_acquisitions;
3126c35e809SDag-Erling Smørgrav 	}
3136c35e809SDag-Erling Smørgrav #endif
3146283b7d0SJohn Baldwin }
3156283b7d0SJohn Baldwin 
3166283b7d0SJohn Baldwin void
3176283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
3186283b7d0SJohn Baldwin {
3196283b7d0SJohn Baldwin 
320dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
32121377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
3226c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
3236c35e809SDag-Erling Smørgrav 	if (m->cycles != 0) {
3246c35e809SDag-Erling Smørgrav 		static const char *unknown = "(unknown)";
3256c35e809SDag-Erling Smørgrav 		struct mutex_prof *mpp;
3266c35e809SDag-Erling Smørgrav 		u_int64_t cycles, mcycles;
3276c35e809SDag-Erling Smørgrav 		const char *p, *q;
3286c35e809SDag-Erling Smørgrav 		volatile u_int hash, n;
3296c35e809SDag-Erling Smørgrav 
3306c35e809SDag-Erling Smørgrav 		cycles = get_cyclecount();
3316c35e809SDag-Erling Smørgrav 		mcycles = m->cycles;
3326c35e809SDag-Erling Smørgrav 		m->cycles = 0;
3336c35e809SDag-Erling Smørgrav 		if (cycles <= mcycles)
3346c35e809SDag-Erling Smørgrav 			goto out;
3356c35e809SDag-Erling Smørgrav 		for (p = file; strncmp(p, "../", 3) == 0; p += 3)
3366c35e809SDag-Erling Smørgrav 			/* nothing */ ;
3376c35e809SDag-Erling Smørgrav 		if (p == NULL || *p == '\0')
3386c35e809SDag-Erling Smørgrav 			p = unknown;
3396c35e809SDag-Erling Smørgrav 		for (hash = line, q = p; *q != '\0'; ++q)
3406c35e809SDag-Erling Smørgrav 			hash = (hash * 2 + *q) % MPROF_HASH_SIZE;
3416c35e809SDag-Erling Smørgrav 		mtx_lock_spin(&mprof_mtx);
3426c35e809SDag-Erling Smørgrav 		n = hash;
3436c35e809SDag-Erling Smørgrav 		while ((mpp = mprof_hash[n]) != NULL) {
3446c35e809SDag-Erling Smørgrav 			if (mpp->line == line && strcmp(mpp->file, p) == 0)
3456c35e809SDag-Erling Smørgrav 				break;
3466c35e809SDag-Erling Smørgrav 			n = (n + 1) % MPROF_HASH_SIZE;
3476c35e809SDag-Erling Smørgrav 		}
3486c35e809SDag-Erling Smørgrav 		if (mpp == NULL) {
3496c35e809SDag-Erling Smørgrav 			/* Just exit if we cannot get a trace buffer */
3506c35e809SDag-Erling Smørgrav 			if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) {
3516c35e809SDag-Erling Smørgrav 				++mutex_prof_rejected;
3526c35e809SDag-Erling Smørgrav 				goto unlock;
3536c35e809SDag-Erling Smørgrav 			}
3546c35e809SDag-Erling Smørgrav 			mpp = &mprof_buf[first_free_mprof_buf++];
3556c35e809SDag-Erling Smørgrav 			mpp->name = mtx_name(m);
3566c35e809SDag-Erling Smørgrav 			mpp->file = p;
3576c35e809SDag-Erling Smørgrav 			mpp->line = line;
3586c35e809SDag-Erling Smørgrav 			mutex_prof_collisions += n - hash;
3596c35e809SDag-Erling Smørgrav 			++mutex_prof_records;
3606c35e809SDag-Erling Smørgrav 			mprof_hash[hash] = mpp;
3616c35e809SDag-Erling Smørgrav 		}
3626c35e809SDag-Erling Smørgrav 		/*
3636c35e809SDag-Erling Smørgrav 		 * Record if the mutex has been held longer now than ever
3646c35e809SDag-Erling Smørgrav 		 * before
3656c35e809SDag-Erling Smørgrav 		 */
3666c35e809SDag-Erling Smørgrav 		if ((cycles - mcycles) > mpp->cycles[MPROF_MAX])
3676c35e809SDag-Erling Smørgrav 			mpp->cycles[MPROF_MAX] = cycles - mcycles;
3686c35e809SDag-Erling Smørgrav 		mpp->cycles[MPROF_TOT] += cycles - mcycles;
3696c35e809SDag-Erling Smørgrav 		mpp->cycles[MPROF_CNT] += 1;
3706c35e809SDag-Erling Smørgrav 		mpp->cycles[MPROF_AVG] =
3716c35e809SDag-Erling Smørgrav 		    mpp->cycles[MPROF_TOT] / mpp->cycles[MPROF_CNT];
3726c35e809SDag-Erling Smørgrav unlock:
3736c35e809SDag-Erling Smørgrav 		mtx_unlock_spin(&mprof_mtx);
3746c35e809SDag-Erling Smørgrav 	}
3756c35e809SDag-Erling Smørgrav out:
3766c35e809SDag-Erling Smørgrav #endif
377dde96c99SJohn Baldwin  	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
378dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
379dde96c99SJohn Baldwin 	    line);
380dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
3816283b7d0SJohn Baldwin }
3826283b7d0SJohn Baldwin 
3836283b7d0SJohn Baldwin void
3846283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3856283b7d0SJohn Baldwin {
3866283b7d0SJohn Baldwin 
387dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
388dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
389dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
390dde96c99SJohn Baldwin 	    line);
391dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3926283b7d0SJohn Baldwin }
3936283b7d0SJohn Baldwin 
3946283b7d0SJohn Baldwin void
3956283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3966283b7d0SJohn Baldwin {
3976283b7d0SJohn Baldwin 
398dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
39921377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
400dde96c99SJohn Baldwin  	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
401dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
402dde96c99SJohn Baldwin 	    line);
403dde96c99SJohn Baldwin 	_rel_spin_lock(m);
4046283b7d0SJohn Baldwin }
4056283b7d0SJohn Baldwin 
4066283b7d0SJohn Baldwin /*
4079ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
4089ed346baSBosko Milekic  * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that
4099ed346baSBosko Milekic  * if we're called, it's because we know we don't already own this lock.
4100cde2e34SJason Evans  */
4110cde2e34SJason Evans int
4129ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
4130cde2e34SJason Evans {
4140cde2e34SJason Evans 	int rval;
4150cde2e34SJason Evans 
416b40ce416SJulian Elischer 	MPASS(curthread != NULL);
4179ed346baSBosko Milekic 
418b40ce416SJulian Elischer 	rval = _obtain_lock(m, curthread);
4199ed346baSBosko Milekic 
42019284646SJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
42119284646SJohn Baldwin 	if (rval) {
4229ed346baSBosko Milekic 		/*
4239ed346baSBosko Milekic 		 * We do not handle recursion in _mtx_trylock; see the
4249ed346baSBosko Milekic 		 * note at the top of the routine.
4259ed346baSBosko Milekic 		 */
4265746a1d8SBosko Milekic 		KASSERT(!mtx_recursed(m),
4275746a1d8SBosko Milekic 		    ("mtx_trylock() called on a recursed mutex"));
4282d96f0b1SJohn Baldwin 		WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4292d96f0b1SJohn Baldwin 		    file, line);
4300cde2e34SJason Evans 	}
4319ed346baSBosko Milekic 
43219284646SJohn Baldwin 	return (rval);
4330cde2e34SJason Evans }
4340cde2e34SJason Evans 
4350cde2e34SJason Evans /*
4369ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4379ed346baSBosko Milekic  *
4389ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4399ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4400cde2e34SJason Evans  */
4410cde2e34SJason Evans void
4429ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line)
44336412d79SJohn Baldwin {
444b40ce416SJulian Elischer 	struct thread *td = curthread;
44536412d79SJohn Baldwin 
446b40ce416SJulian Elischer 	if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)td) {
44736412d79SJohn Baldwin 		m->mtx_recurse++;
44808812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
44919284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4505746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
45136412d79SJohn Baldwin 		return;
45236412d79SJohn Baldwin 	}
4539ed346baSBosko Milekic 
45419284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
45515ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
45615ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
45719284646SJohn Baldwin 		    m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
4581bd0eefbSJohn Baldwin 
459b40ce416SJulian Elischer 	while (!_obtain_lock(m, td)) {
460f5271ebcSJohn Baldwin 		uintptr_t v;
461b40ce416SJulian Elischer 		struct thread *td1;
46236412d79SJohn Baldwin 
4639ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
46436412d79SJohn Baldwin 		/*
4659ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
4669ed346baSBosko Milekic 		 * the sched_lock.
46736412d79SJohn Baldwin 		 */
46836412d79SJohn Baldwin 		if ((v = m->mtx_lock) == MTX_UNOWNED) {
4699ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
47036412d79SJohn Baldwin 			continue;
47136412d79SJohn Baldwin 		}
4729ed346baSBosko Milekic 
47336412d79SJohn Baldwin 		/*
4749ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
475b40ce416SJulian Elischer 		 * there are threads blocked on it.
47636412d79SJohn Baldwin 		 */
47736412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
478b40ce416SJulian Elischer 			td1 = TAILQ_FIRST(&m->mtx_blocked);
479b40ce416SJulian Elischer 			MPASS(td1 != NULL);
480b40ce416SJulian Elischer 			m->mtx_lock = (uintptr_t)td | MTX_CONTESTED;
4819ed346baSBosko Milekic 
4822c100766SJulian Elischer 			if (td1->td_priority < td->td_priority)
4832c100766SJulian Elischer 				td->td_priority = td1->td_priority;
4849ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
48536412d79SJohn Baldwin 			return;
48636412d79SJohn Baldwin 		}
4879ed346baSBosko Milekic 
48836412d79SJohn Baldwin 		/*
4899ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
4909ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
4919ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
49236412d79SJohn Baldwin 		 */
49336412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
49436412d79SJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, (void *)v,
49536412d79SJohn Baldwin 			(void *)(v | MTX_CONTESTED))) {
4969ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
49736412d79SJohn Baldwin 			continue;
49836412d79SJohn Baldwin 		}
49936412d79SJohn Baldwin 
5009ed346baSBosko Milekic 		/*
5019ed346baSBosko Milekic 		 * We deffinately must sleep for this lock.
5029ed346baSBosko Milekic 		 */
50336412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
50436412d79SJohn Baldwin 
50536412d79SJohn Baldwin #ifdef notyet
50636412d79SJohn Baldwin 		/*
5079ed346baSBosko Milekic 		 * If we're borrowing an interrupted thread's VM context, we
5089ed346baSBosko Milekic 		 * must clean up before going to sleep.
50936412d79SJohn Baldwin 		 */
510b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
511b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
51236412d79SJohn Baldwin 
51336412d79SJohn Baldwin 			if (it->it_interrupted) {
51419284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
51536412d79SJohn Baldwin 					CTR2(KTR_LOCK,
51615ec816aSJohn Baldwin 				    "_mtx_lock_sleep: %p interrupted %p",
51736412d79SJohn Baldwin 					    it, it->it_interrupted);
51836412d79SJohn Baldwin 				intr_thd_fixup(it);
51936412d79SJohn Baldwin 			}
52036412d79SJohn Baldwin 		}
52136412d79SJohn Baldwin #endif
52236412d79SJohn Baldwin 
5239ed346baSBosko Milekic 		/*
5249ed346baSBosko Milekic 		 * Put us on the list of threads blocked on this mutex.
5259ed346baSBosko Milekic 		 */
52636412d79SJohn Baldwin 		if (TAILQ_EMPTY(&m->mtx_blocked)) {
52718fc2ba9SJohn Baldwin 			td1 = mtx_owner(m);
528b40ce416SJulian Elischer 			LIST_INSERT_HEAD(&td1->td_contested, m, mtx_contested);
529b40ce416SJulian Elischer 			TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq);
53036412d79SJohn Baldwin 		} else {
531b40ce416SJulian Elischer 			TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq)
5322c100766SJulian Elischer 				if (td1->td_priority > td->td_priority)
53336412d79SJohn Baldwin 					break;
534b40ce416SJulian Elischer 			if (td1)
535b40ce416SJulian Elischer 				TAILQ_INSERT_BEFORE(td1, td, td_blkq);
53636412d79SJohn Baldwin 			else
537b40ce416SJulian Elischer 				TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq);
53836412d79SJohn Baldwin 		}
53936412d79SJohn Baldwin 
5409ed346baSBosko Milekic 		/*
5419ed346baSBosko Milekic 		 * Save who we're blocked on.
5429ed346baSBosko Milekic 		 */
543b40ce416SJulian Elischer 		td->td_blocked = m;
544b40ce416SJulian Elischer 		td->td_mtxname = m->mtx_object.lo_name;
545b40ce416SJulian Elischer 		td->td_proc->p_stat = SMTX;
546b40ce416SJulian Elischer 		propagate_priority(td);
5479ed346baSBosko Milekic 
54819284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
549562e4ffeSJohn Baldwin 			CTR3(KTR_LOCK,
550b40ce416SJulian Elischer 			    "_mtx_lock_sleep: p %p blocked on [%p] %s", td, m,
55119284646SJohn Baldwin 			    m->mtx_object.lo_name);
5529ed346baSBosko Milekic 
553b40ce416SJulian Elischer 		td->td_proc->p_stats->p_ru.ru_nvcsw++;
55420cdcc5bSJohn Baldwin 		mi_switch();
5559ed346baSBosko Milekic 
55619284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
55736412d79SJohn Baldwin 			CTR3(KTR_LOCK,
5589ed346baSBosko Milekic 			  "_mtx_lock_sleep: p %p free from blocked on [%p] %s",
559b40ce416SJulian Elischer 			  td, m, m->mtx_object.lo_name);
5609ed346baSBosko Milekic 
5619ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
56236412d79SJohn Baldwin 	}
5639ed346baSBosko Milekic 
56436412d79SJohn Baldwin 	return;
5659ed346baSBosko Milekic }
5669ed346baSBosko Milekic 
5679ed346baSBosko Milekic /*
5689ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
5699ed346baSBosko Milekic  *
5709ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
5719ed346baSBosko Milekic  * is handled inline.
5729ed346baSBosko Milekic  */
5739ed346baSBosko Milekic void
5747e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line)
57536412d79SJohn Baldwin {
57636412d79SJohn Baldwin 	int i = 0;
57736412d79SJohn Baldwin 
57819284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
5795746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
5809ed346baSBosko Milekic 
58136412d79SJohn Baldwin 	for (;;) {
582b40ce416SJulian Elischer 		if (_obtain_lock(m, curthread))
58336412d79SJohn Baldwin 			break;
5849ed346baSBosko Milekic 
5857141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
5867e1f6dfeSJohn Baldwin 		critical_exit();
58736412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
588bf07c922SJohn Baldwin 			if (i++ < 10000000)
58936412d79SJohn Baldwin 				continue;
590bf07c922SJohn Baldwin 			if (i++ < 60000000)
59136412d79SJohn Baldwin 				DELAY(1);
59236412d79SJohn Baldwin #ifdef DDB
59336412d79SJohn Baldwin 			else if (!db_active)
59436412d79SJohn Baldwin #else
59536412d79SJohn Baldwin 			else
59636412d79SJohn Baldwin #endif
5979ed346baSBosko Milekic 			panic("spin lock %s held by %p for > 5 seconds",
59819284646SJohn Baldwin 			    m->mtx_object.lo_name, (void *)m->mtx_lock);
59936412d79SJohn Baldwin 		}
6007e1f6dfeSJohn Baldwin 		critical_enter();
60136412d79SJohn Baldwin 	}
60236412d79SJohn Baldwin 
60319284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6049ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
6059ed346baSBosko Milekic 
60636412d79SJohn Baldwin 	return;
60736412d79SJohn Baldwin }
60836412d79SJohn Baldwin 
6099ed346baSBosko Milekic /*
6109ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
6119ed346baSBosko Milekic  *
6129ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
6139ed346baSBosko Milekic  * need to wake up a blocked thread).
6149ed346baSBosko Milekic  */
61536412d79SJohn Baldwin void
6169ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
61736412d79SJohn Baldwin {
618b40ce416SJulian Elischer 	struct thread *td, *td1;
61936412d79SJohn Baldwin 	struct mtx *m1;
62036412d79SJohn Baldwin 	int pri;
62136412d79SJohn Baldwin 
622b40ce416SJulian Elischer 	td = curthread;
6239ed346baSBosko Milekic 
62408812b39SBosko Milekic 	if (mtx_recursed(m)) {
62536412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
62608812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
62719284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6289ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
62936412d79SJohn Baldwin 		return;
63036412d79SJohn Baldwin 	}
6319ed346baSBosko Milekic 
6329ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
63319284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6349ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
6359ed346baSBosko Milekic 
636b40ce416SJulian Elischer 	td1 = TAILQ_FIRST(&m->mtx_blocked);
637b40ce416SJulian Elischer 	MPASS(td->td_proc->p_magic == P_MAGIC);
638b40ce416SJulian Elischer 	MPASS(td1->td_proc->p_magic == P_MAGIC);
6399ed346baSBosko Milekic 
640b40ce416SJulian Elischer 	TAILQ_REMOVE(&m->mtx_blocked, td1, td_blkq);
6419ed346baSBosko Milekic 
64236412d79SJohn Baldwin 	if (TAILQ_EMPTY(&m->mtx_blocked)) {
64336412d79SJohn Baldwin 		LIST_REMOVE(m, mtx_contested);
64436412d79SJohn Baldwin 		_release_lock_quick(m);
64519284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6469ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
64736412d79SJohn Baldwin 	} else
6489ed346baSBosko Milekic 		atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED);
6499ed346baSBosko Milekic 
650d5a08a60SJake Burkholder 	pri = PRI_MAX;
651b40ce416SJulian Elischer 	LIST_FOREACH(m1, &td->td_contested, mtx_contested) {
6522c100766SJulian Elischer 		int cp = TAILQ_FIRST(&m1->mtx_blocked)->td_priority;
65336412d79SJohn Baldwin 		if (cp < pri)
65436412d79SJohn Baldwin 			pri = cp;
65536412d79SJohn Baldwin 	}
6569ed346baSBosko Milekic 
6572c100766SJulian Elischer 	if (pri > td->td_base_pri)
6582c100766SJulian Elischer 		pri = td->td_base_pri;
6592c100766SJulian Elischer 	td->td_priority = pri;
6609ed346baSBosko Milekic 
66119284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6629ed346baSBosko Milekic 		CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p",
663b40ce416SJulian Elischer 		    m, td1);
6649ed346baSBosko Milekic 
665b40ce416SJulian Elischer 	td1->td_blocked = NULL;
666b40ce416SJulian Elischer 	td1->td_proc->p_stat = SRUN;
667b40ce416SJulian Elischer 	setrunqueue(td1);
6689ed346baSBosko Milekic 
6692c100766SJulian Elischer 	if (td->td_critnest == 1 && td1->td_priority < pri) {
67036412d79SJohn Baldwin #ifdef notyet
671b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
672b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
67336412d79SJohn Baldwin 
67436412d79SJohn Baldwin 			if (it->it_interrupted) {
67519284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
67636412d79SJohn Baldwin 					CTR2(KTR_LOCK,
67715ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
67836412d79SJohn Baldwin 					    it, it->it_interrupted);
67936412d79SJohn Baldwin 				intr_thd_fixup(it);
68036412d79SJohn Baldwin 			}
68136412d79SJohn Baldwin 		}
68236412d79SJohn Baldwin #endif
683b40ce416SJulian Elischer 		setrunqueue(td);
68419284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
685562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
6869ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
6879ed346baSBosko Milekic 			    (void *)m->mtx_lock);
6889ed346baSBosko Milekic 
689b40ce416SJulian Elischer 		td->td_proc->p_stats->p_ru.ru_nivcsw++;
69036412d79SJohn Baldwin 		mi_switch();
69119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6929ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
69331271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
69436412d79SJohn Baldwin 	}
69536412d79SJohn Baldwin 
6969ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
6979ed346baSBosko Milekic 
6989ed346baSBosko Milekic 	return;
6999ed346baSBosko Milekic }
7009ed346baSBosko Milekic 
7019ed346baSBosko Milekic /*
7029ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
7039ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
7049ed346baSBosko Milekic  */
7059ed346baSBosko Milekic 
7069ed346baSBosko Milekic /*
70715ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
7089ed346baSBosko Milekic  */
7091103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
7100cde2e34SJason Evans void
71156771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
7120cde2e34SJason Evans {
7135cb0fbe4SJohn Baldwin 
7145cb0fbe4SJohn Baldwin 	if (panicstr != NULL)
7155cb0fbe4SJohn Baldwin 		return;
716a10f4966SJake Burkholder 	switch (what) {
7170cde2e34SJason Evans 	case MA_OWNED:
7180cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
7190cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
720a10f4966SJake Burkholder 		if (!mtx_owned(m))
7210cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
72219284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
723a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
724a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
7250cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
72619284646SJohn Baldwin 				    m->mtx_object.lo_name, file, line);
727a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
7280cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
72919284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7300cde2e34SJason Evans 		}
7310cde2e34SJason Evans 		break;
7320cde2e34SJason Evans 	case MA_NOTOWNED:
733a10f4966SJake Burkholder 		if (mtx_owned(m))
7340cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
73519284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7360cde2e34SJason Evans 		break;
7370cde2e34SJason Evans 	default:
73856771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
7390cde2e34SJason Evans 	}
7400cde2e34SJason Evans }
7410cde2e34SJason Evans #endif
7420cde2e34SJason Evans 
7439ed346baSBosko Milekic /*
7449ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
74519284646SJohn Baldwin  *
74619284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
74719284646SJohn Baldwin  * maintained by the witness code.
7489ed346baSBosko Milekic  */
74936412d79SJohn Baldwin #ifdef MUTEX_DEBUG
75036412d79SJohn Baldwin 
7514d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
75236412d79SJohn Baldwin 
75319284646SJohn Baldwin void
75419284646SJohn Baldwin mtx_validate(struct mtx *m)
75536412d79SJohn Baldwin {
75636412d79SJohn Baldwin 
75736412d79SJohn Baldwin /*
75836412d79SJohn Baldwin  * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
75936412d79SJohn Baldwin  * we can re-enable the kernacc() checks.
76036412d79SJohn Baldwin  */
76136412d79SJohn Baldwin #ifndef __alpha__
76276dcbd6fSBosko Milekic 	/*
76376dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
76476dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
76576dcbd6fSBosko Milekic 	 * requires Giant itself.
76676dcbd6fSBosko Milekic 	 */
767ab07087eSBosko Milekic 	if (!cold)
768ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
769ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
77019284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
77136412d79SJohn Baldwin #endif
77236412d79SJohn Baldwin }
77336412d79SJohn Baldwin #endif
77436412d79SJohn Baldwin 
7759ed346baSBosko Milekic /*
7769ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
7779ed346baSBosko Milekic  * `opts' with options contained in `opts' and description `description.'
7789ed346baSBosko Milekic  */
77936412d79SJohn Baldwin void
7809ed346baSBosko Milekic mtx_init(struct mtx *m, const char *description, int opts)
78136412d79SJohn Baldwin {
78219284646SJohn Baldwin 	struct lock_object *lock;
7839ed346baSBosko Milekic 
78419284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
785f22a4b62SJeff Roberson 	    MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0);
7869ed346baSBosko Milekic 
78736412d79SJohn Baldwin #ifdef MUTEX_DEBUG
7889ed346baSBosko Milekic 	/* Diagnostic and error correction */
78919284646SJohn Baldwin 	mtx_validate(m);
7906936206eSJohn Baldwin #endif
79136412d79SJohn Baldwin 
79219284646SJohn Baldwin 	lock = &m->mtx_object;
7937ada5876SJohn Baldwin 	KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
7947ada5876SJohn Baldwin 	    ("mutex %s %p already initialized", description, m));
7957ada5876SJohn Baldwin 	bzero(m, sizeof(*m));
79619284646SJohn Baldwin 	if (opts & MTX_SPIN)
79719284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_spin;
79819284646SJohn Baldwin 	else
79919284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_sleep;
80019284646SJohn Baldwin 	lock->lo_name = description;
80119284646SJohn Baldwin 	if (opts & MTX_QUIET)
80219284646SJohn Baldwin 		lock->lo_flags = LO_QUIET;
80319284646SJohn Baldwin 	if (opts & MTX_RECURSE)
80419284646SJohn Baldwin 		lock->lo_flags |= LO_RECURSABLE;
80519284646SJohn Baldwin 	if (opts & MTX_SLEEPABLE)
80619284646SJohn Baldwin 		lock->lo_flags |= LO_SLEEPABLE;
80719284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
80819284646SJohn Baldwin 		lock->lo_flags |= LO_WITNESS;
809f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
810f22a4b62SJeff Roberson 		lock->lo_flags |= LO_DUPOK;
81119284646SJohn Baldwin 
81219284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
81336412d79SJohn Baldwin 	TAILQ_INIT(&m->mtx_blocked);
8149ed346baSBosko Milekic 
81519284646SJohn Baldwin 	LOCK_LOG_INIT(lock, opts);
816d1c1b841SJason Evans 
81719284646SJohn Baldwin 	WITNESS_INIT(lock);
81836412d79SJohn Baldwin }
81936412d79SJohn Baldwin 
8209ed346baSBosko Milekic /*
82119284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
82219284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
82319284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
82419284646SJohn Baldwin  * flags.
8259ed346baSBosko Milekic  */
82636412d79SJohn Baldwin void
82736412d79SJohn Baldwin mtx_destroy(struct mtx *m)
82836412d79SJohn Baldwin {
82936412d79SJohn Baldwin 
83019284646SJohn Baldwin 	LOCK_LOG_DESTROY(&m->mtx_object, 0);
8319ed346baSBosko Milekic 
83219284646SJohn Baldwin 	if (!mtx_owned(m))
83319284646SJohn Baldwin 		MPASS(mtx_unowned(m));
83419284646SJohn Baldwin 	else {
83508812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
8369ed346baSBosko Milekic 
83719284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
838c86b6ff5SJohn Baldwin 		WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
839c86b6ff5SJohn Baldwin 		    __LINE__);
84036412d79SJohn Baldwin 	}
8410384fff8SJason Evans 
84219284646SJohn Baldwin 	WITNESS_DESTROY(&m->mtx_object);
8430384fff8SJason Evans }
844d23f5958SMatthew Dillon 
845d23f5958SMatthew Dillon /*
846d23f5958SMatthew Dillon  * Encapsulated Giant mutex routines.  These routines provide encapsulation
847d23f5958SMatthew Dillon  * control for the Giant mutex, allowing sysctls to be used to turn on and
848d23f5958SMatthew Dillon  * off Giant around certain subsystems.  The default value for the sysctls
849d23f5958SMatthew Dillon  * are set to what developers believe is stable and working in regards to
850d23f5958SMatthew Dillon  * the Giant pushdown.  Developers should not turn off Giant via these
851d23f5958SMatthew Dillon  * sysctls unless they know what they are doing.
852d23f5958SMatthew Dillon  *
853d23f5958SMatthew Dillon  * Callers of mtx_lock_giant() are expected to pass the return value to an
854d23f5958SMatthew Dillon  * accompanying mtx_unlock_giant() later on.  If multiple subsystems are
855d23f5958SMatthew Dillon  * effected by a Giant wrap, all related sysctl variables must be zero for
856d23f5958SMatthew Dillon  * the subsystem call to operate without Giant (as determined by the caller).
857d23f5958SMatthew Dillon  */
858d23f5958SMatthew Dillon 
859d23f5958SMatthew Dillon SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation");
860d23f5958SMatthew Dillon 
861d23f5958SMatthew Dillon static int kern_giant_all = 0;
862d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, "");
863d23f5958SMatthew Dillon 
864d23f5958SMatthew Dillon int kern_giant_proc = 1;	/* Giant around PROC locks */
865d23f5958SMatthew Dillon int kern_giant_file = 1;	/* Giant around struct file & filedesc */
866735da6deSMatthew Dillon int kern_giant_ucred = 1;	/* Giant around ucred */
867d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, "");
868d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, "");
869735da6deSMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, "");
870d23f5958SMatthew Dillon 
871d23f5958SMatthew Dillon int
872d23f5958SMatthew Dillon mtx_lock_giant(int sysctlvar)
873d23f5958SMatthew Dillon {
874d23f5958SMatthew Dillon 	if (sysctlvar || kern_giant_all) {
875d23f5958SMatthew Dillon 		mtx_lock(&Giant);
876d23f5958SMatthew Dillon 		return(1);
877d23f5958SMatthew Dillon 	}
878d23f5958SMatthew Dillon 	return(0);
879d23f5958SMatthew Dillon }
880d23f5958SMatthew Dillon 
881d23f5958SMatthew Dillon void
882d23f5958SMatthew Dillon mtx_unlock_giant(int s)
883d23f5958SMatthew Dillon {
884d23f5958SMatthew Dillon 	if (s)
885d23f5958SMatthew Dillon 		mtx_unlock(&Giant);
886d23f5958SMatthew Dillon }
887d23f5958SMatthew Dillon 
888