xref: /freebsd/sys/kern/kern_mutex.c (revision 703fc290fbd69962f3ce0744f1f5a66a84c8fe23)
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>
51a5a96a19SJohn Baldwin #include <sys/sysctl.h>
5236412d79SJohn Baldwin #include <sys/vmmeter.h>
530384fff8SJason Evans 
5436412d79SJohn Baldwin #include <machine/atomic.h>
5536412d79SJohn Baldwin #include <machine/bus.h>
5636412d79SJohn Baldwin #include <machine/clock.h>
570384fff8SJason Evans #include <machine/cpu.h>
5836412d79SJohn Baldwin 
599c36c934SJohn Baldwin #include <ddb/ddb.h>
609c36c934SJohn Baldwin 
6136412d79SJohn Baldwin #include <vm/vm.h>
6236412d79SJohn Baldwin #include <vm/vm_extern.h>
6336412d79SJohn Baldwin 
640cde2e34SJason Evans /*
659ed346baSBosko Milekic  * Internal utility macros.
660cde2e34SJason Evans  */
679ed346baSBosko Milekic #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
680cde2e34SJason Evans 
699ed346baSBosko Milekic #define mtx_owner(m)	(mtx_unowned((m)) ? NULL \
70b40ce416SJulian Elischer 	: (struct thread *)((m)->mtx_lock & MTX_FLAGMASK))
719ed346baSBosko Milekic 
720cde2e34SJason Evans /*
7319284646SJohn Baldwin  * Lock classes for sleep and spin mutexes.
740cde2e34SJason Evans  */
7519284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = {
7619284646SJohn Baldwin 	"sleep mutex",
7719284646SJohn Baldwin 	LC_SLEEPLOCK | LC_RECURSABLE
7819284646SJohn Baldwin };
7919284646SJohn Baldwin struct lock_class lock_class_mtx_spin = {
8019284646SJohn Baldwin 	"spin mutex",
8119284646SJohn Baldwin 	LC_SPINLOCK | LC_RECURSABLE
828484de75SJohn Baldwin };
838484de75SJohn Baldwin 
849ed346baSBosko Milekic /*
85c53c013bSJohn Baldwin  * System-wide mutexes
86c53c013bSJohn Baldwin  */
87c53c013bSJohn Baldwin struct mtx sched_lock;
88c53c013bSJohn Baldwin struct mtx Giant;
89c53c013bSJohn Baldwin 
90c53c013bSJohn Baldwin /*
919ed346baSBosko Milekic  * Prototypes for non-exported routines.
929ed346baSBosko Milekic  */
93b40ce416SJulian Elischer static void	propagate_priority(struct thread *);
9436412d79SJohn Baldwin 
9536412d79SJohn Baldwin static void
96b40ce416SJulian Elischer propagate_priority(struct thread *td)
9736412d79SJohn Baldwin {
982c100766SJulian Elischer 	int pri = td->td_priority;
99b40ce416SJulian Elischer 	struct mtx *m = td->td_blocked;
10036412d79SJohn Baldwin 
1011bd0eefbSJohn Baldwin 	mtx_assert(&sched_lock, MA_OWNED);
10236412d79SJohn Baldwin 	for (;;) {
103b40ce416SJulian Elischer 		struct thread *td1;
10436412d79SJohn Baldwin 
105b40ce416SJulian Elischer 		td = mtx_owner(m);
10636412d79SJohn Baldwin 
107b40ce416SJulian Elischer 		if (td == NULL) {
10836412d79SJohn Baldwin 			/*
10936412d79SJohn Baldwin 			 * This really isn't quite right. Really
110b40ce416SJulian Elischer 			 * ought to bump priority of thread that
11136412d79SJohn Baldwin 			 * next acquires the mutex.
11236412d79SJohn Baldwin 			 */
11336412d79SJohn Baldwin 			MPASS(m->mtx_lock == MTX_CONTESTED);
11436412d79SJohn Baldwin 			return;
11536412d79SJohn Baldwin 		}
1169ed346baSBosko Milekic 
117b40ce416SJulian Elischer 		MPASS(td->td_proc->p_magic == P_MAGIC);
118b40ce416SJulian Elischer 		KASSERT(td->td_proc->p_stat != SSLEEP, ("sleeping thread owns a mutex"));
1192c100766SJulian Elischer 		if (td->td_priority <= pri) /* lower is higher priority */
12036412d79SJohn Baldwin 			return;
1211bd0eefbSJohn Baldwin 
1221bd0eefbSJohn Baldwin 		/*
123b40ce416SJulian Elischer 		 * Bump this thread's priority.
1241bd0eefbSJohn Baldwin 		 */
1252c100766SJulian Elischer 		td->td_priority = pri;
1261bd0eefbSJohn Baldwin 
12736412d79SJohn Baldwin 		/*
12836412d79SJohn Baldwin 		 * If lock holder is actually running, just bump priority.
12936412d79SJohn Baldwin 		 */
130b40ce416SJulian Elischer 		 /* XXXKSE this test is not sufficient */
131b40ce416SJulian Elischer 		if (td->td_kse && (td->td_kse->ke_oncpu != NOCPU)) {
132b40ce416SJulian Elischer 			MPASS(td->td_proc->p_stat == SRUN
133b40ce416SJulian Elischer 			|| td->td_proc->p_stat == SZOMB
134b40ce416SJulian Elischer 			|| td->td_proc->p_stat == SSTOP);
13536412d79SJohn Baldwin 			return;
13636412d79SJohn Baldwin 		}
137d5a08a60SJake Burkholder 
1381b43703bSJohn Baldwin #ifndef SMP
1391b43703bSJohn Baldwin 		/*
140b40ce416SJulian Elischer 		 * For UP, we check to see if td is curthread (this shouldn't
1411b43703bSJohn Baldwin 		 * ever happen however as it would mean we are in a deadlock.)
1421b43703bSJohn Baldwin 		 */
143b40ce416SJulian Elischer 		KASSERT(td != curthread, ("Deadlock detected"));
1441b43703bSJohn Baldwin #endif
1451b43703bSJohn Baldwin 
14636412d79SJohn Baldwin 		/*
147b40ce416SJulian Elischer 		 * If on run queue move to new run queue, and quit.
148b40ce416SJulian Elischer 		 * XXXKSE this gets a lot more complicated under threads
149b40ce416SJulian Elischer 		 * but try anyhow.
15036412d79SJohn Baldwin 		 */
151b40ce416SJulian Elischer 		if (td->td_proc->p_stat == SRUN) {
152b40ce416SJulian Elischer 			MPASS(td->td_blocked == NULL);
153b40ce416SJulian Elischer 			remrunqueue(td);
154b40ce416SJulian Elischer 			setrunqueue(td);
15536412d79SJohn Baldwin 			return;
15636412d79SJohn Baldwin 		}
15736412d79SJohn Baldwin 
15836412d79SJohn Baldwin 		/*
1591bd0eefbSJohn Baldwin 		 * If we aren't blocked on a mutex, we should be.
16036412d79SJohn Baldwin 		 */
161b40ce416SJulian Elischer 		KASSERT(td->td_proc->p_stat == SMTX, (
1621bd0eefbSJohn Baldwin 		    "process %d(%s):%d holds %s but isn't blocked on a mutex\n",
163b40ce416SJulian Elischer 		    td->td_proc->p_pid, td->td_proc->p_comm, td->td_proc->p_stat,
16419284646SJohn Baldwin 		    m->mtx_object.lo_name));
16536412d79SJohn Baldwin 
16636412d79SJohn Baldwin 		/*
167b40ce416SJulian Elischer 		 * Pick up the mutex that td is blocked on.
16836412d79SJohn Baldwin 		 */
169b40ce416SJulian Elischer 		m = td->td_blocked;
17036412d79SJohn Baldwin 		MPASS(m != NULL);
17136412d79SJohn Baldwin 
17236412d79SJohn Baldwin 		/*
173b40ce416SJulian Elischer 		 * Check if the thread needs to be moved up on
17436412d79SJohn Baldwin 		 * the blocked chain
17536412d79SJohn Baldwin 		 */
176b40ce416SJulian Elischer 		if (td == TAILQ_FIRST(&m->mtx_blocked)) {
1771bd0eefbSJohn Baldwin 			continue;
1781bd0eefbSJohn Baldwin 		}
1799ed346baSBosko Milekic 
180b40ce416SJulian Elischer 		td1 = TAILQ_PREV(td, threadqueue, td_blkq);
1812c100766SJulian Elischer 		if (td1->td_priority <= pri) {
18236412d79SJohn Baldwin 			continue;
18336412d79SJohn Baldwin 		}
18436412d79SJohn Baldwin 
18536412d79SJohn Baldwin 		/*
186b40ce416SJulian Elischer 		 * Remove thread from blocked chain and determine where
187b40ce416SJulian Elischer 		 * it should be moved up to.  Since we know that td1 has
188b40ce416SJulian Elischer 		 * a lower priority than td, we know that at least one
189b40ce416SJulian Elischer 		 * thread in the chain has a lower priority and that
190b40ce416SJulian Elischer 		 * td1 will thus not be NULL after the loop.
19136412d79SJohn Baldwin 		 */
192b40ce416SJulian Elischer 		TAILQ_REMOVE(&m->mtx_blocked, td, td_blkq);
193b40ce416SJulian Elischer 		TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq) {
194b40ce416SJulian Elischer 			MPASS(td1->td_proc->p_magic == P_MAGIC);
1952c100766SJulian Elischer 			if (td1->td_priority > pri)
19636412d79SJohn Baldwin 				break;
19736412d79SJohn Baldwin 		}
1989ed346baSBosko Milekic 
199b40ce416SJulian Elischer 		MPASS(td1 != NULL);
200b40ce416SJulian Elischer 		TAILQ_INSERT_BEFORE(td1, td, td_blkq);
20136412d79SJohn Baldwin 		CTR4(KTR_LOCK,
2028484de75SJohn Baldwin 		    "propagate_priority: p %p moved before %p on [%p] %s",
203b40ce416SJulian Elischer 		    td, td1, m, m->mtx_object.lo_name);
20436412d79SJohn Baldwin 	}
20536412d79SJohn Baldwin }
20636412d79SJohn Baldwin 
2076c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
2086c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging");
2096c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling");
2106c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0;
2116c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW,
2126c35e809SDag-Erling Smørgrav     &mutex_prof_enable, 0, "Enable tracing of mutex holdtime");
2136c35e809SDag-Erling Smørgrav 
2146c35e809SDag-Erling Smørgrav struct mutex_prof {
2156c35e809SDag-Erling Smørgrav 	const char *name;
2166c35e809SDag-Erling Smørgrav 	const char *file;
2176c35e809SDag-Erling Smørgrav 	int line;
2186c35e809SDag-Erling Smørgrav #define MPROF_MAX 0
2196c35e809SDag-Erling Smørgrav #define MPROF_TOT 1
2206c35e809SDag-Erling Smørgrav #define MPROF_CNT 2
2216c35e809SDag-Erling Smørgrav #define MPROF_AVG 3
222b784ffe9SDag-Erling Smørgrav 	u_int64_t counter[4];
223e6330704SDag-Erling Smørgrav 	struct mutex_prof *next;
2246c35e809SDag-Erling Smørgrav };
2256c35e809SDag-Erling Smørgrav 
2266c35e809SDag-Erling Smørgrav /*
2276c35e809SDag-Erling Smørgrav  * mprof_buf is a static pool of profiling records to avoid possible
2286c35e809SDag-Erling Smørgrav  * reentrance of the memory allocation functions.
2296c35e809SDag-Erling Smørgrav  *
2306c35e809SDag-Erling Smørgrav  * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE.
2316c35e809SDag-Erling Smørgrav  */
232e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000
2336c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS];
2346c35e809SDag-Erling Smørgrav static int first_free_mprof_buf;
235e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009
2366c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE];
2376c35e809SDag-Erling Smørgrav 
2386c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions;
2396c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD,
2406c35e809SDag-Erling Smørgrav     &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded");
2416c35e809SDag-Erling Smørgrav static int mutex_prof_records;
2426c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD,
2436c35e809SDag-Erling Smørgrav     &mutex_prof_records, 0, "Number of profiling records");
2446c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS;
2456c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD,
2466c35e809SDag-Erling Smørgrav     &mutex_prof_maxrecords, 0, "Maximum number of profiling records");
2476c35e809SDag-Erling Smørgrav static int mutex_prof_rejected;
2486c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD,
2496c35e809SDag-Erling Smørgrav     &mutex_prof_rejected, 0, "Number of rejected profiling records");
2506c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE;
2516c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD,
2526c35e809SDag-Erling Smørgrav     &mutex_prof_hashsize, 0, "Hash size");
2536c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0;
2546c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD,
2556c35e809SDag-Erling Smørgrav     &mutex_prof_collisions, 0, "Number of hash collisions");
2566c35e809SDag-Erling Smørgrav 
2576c35e809SDag-Erling Smørgrav /*
2586c35e809SDag-Erling Smørgrav  * mprof_mtx protects the profiling buffers and the hash.
2596c35e809SDag-Erling Smørgrav  */
2606c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx;
261e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET);
2626c35e809SDag-Erling Smørgrav 
263b784ffe9SDag-Erling Smørgrav static u_int64_t
264b784ffe9SDag-Erling Smørgrav nanoseconds(void)
265b784ffe9SDag-Erling Smørgrav {
266b784ffe9SDag-Erling Smørgrav 	struct timespec tv;
267b784ffe9SDag-Erling Smørgrav 
268b784ffe9SDag-Erling Smørgrav 	nanotime(&tv);
269b784ffe9SDag-Erling Smørgrav 	return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec);
270b784ffe9SDag-Erling Smørgrav }
271b784ffe9SDag-Erling Smørgrav 
2726c35e809SDag-Erling Smørgrav static int
2736c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS)
2746c35e809SDag-Erling Smørgrav {
2756c35e809SDag-Erling Smørgrav 	struct sbuf *sb;
2766c35e809SDag-Erling Smørgrav 	int error, i;
2776c35e809SDag-Erling Smørgrav 
2786c35e809SDag-Erling Smørgrav 	if (first_free_mprof_buf == 0)
2796c35e809SDag-Erling Smørgrav 		return SYSCTL_OUT(req, "No locking recorded",
2806c35e809SDag-Erling Smørgrav 		    sizeof("No locking recorded"));
2816c35e809SDag-Erling Smørgrav 
2826c35e809SDag-Erling Smørgrav 	sb = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND);
2836c35e809SDag-Erling Smørgrav 	sbuf_printf(sb, "%12s %12s %12s %12s %s\n",
2846c35e809SDag-Erling Smørgrav 	    "max", "total", "count", "average", "name");
2856c35e809SDag-Erling Smørgrav 	mtx_lock_spin(&mprof_mtx);
2866c35e809SDag-Erling Smørgrav 	for (i = 0; i < first_free_mprof_buf; ++i)
2876c35e809SDag-Erling Smørgrav 		sbuf_printf(sb, "%12llu %12llu %12llu %12llu %s:%d (%s)\n",
288b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_MAX] / 1000,
289b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_TOT] / 1000,
290b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_CNT],
291b784ffe9SDag-Erling Smørgrav 		    mprof_buf[i].counter[MPROF_AVG] / 1000,
2926c35e809SDag-Erling Smørgrav 		    mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name);
2936c35e809SDag-Erling Smørgrav 	mtx_unlock_spin(&mprof_mtx);
2946c35e809SDag-Erling Smørgrav 	sbuf_finish(sb);
2956c35e809SDag-Erling Smørgrav 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2966c35e809SDag-Erling Smørgrav 	sbuf_delete(sb);
2976c35e809SDag-Erling Smørgrav 	return (error);
2986c35e809SDag-Erling Smørgrav }
2996c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING|CTLFLAG_RD,
3006c35e809SDag-Erling Smørgrav     NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics");
3016c35e809SDag-Erling Smørgrav #endif
3026c35e809SDag-Erling Smørgrav 
3030cde2e34SJason Evans /*
3046283b7d0SJohn Baldwin  * Function versions of the inlined __mtx_* macros.  These are used by
3056283b7d0SJohn Baldwin  * modules and can also be called from assembly language if needed.
3066283b7d0SJohn Baldwin  */
3076283b7d0SJohn Baldwin void
3086283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
3096283b7d0SJohn Baldwin {
3106283b7d0SJohn Baldwin 
311dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
312dde96c99SJohn Baldwin 	_get_sleep_lock(m, curthread, opts, file, line);
313dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
314dde96c99SJohn Baldwin 	    line);
315dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
3166c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
3176c35e809SDag-Erling Smørgrav 	/* don't reset the timer when/if recursing */
318b784ffe9SDag-Erling Smørgrav 	if (m->acqtime == 0) {
3196c35e809SDag-Erling Smørgrav 		m->file = file;
3206c35e809SDag-Erling Smørgrav 		m->line = line;
321b784ffe9SDag-Erling Smørgrav 		m->acqtime = mutex_prof_enable ? nanoseconds() : 0;
3226c35e809SDag-Erling Smørgrav 		++mutex_prof_acquisitions;
3236c35e809SDag-Erling Smørgrav 	}
3246c35e809SDag-Erling Smørgrav #endif
3256283b7d0SJohn Baldwin }
3266283b7d0SJohn Baldwin 
3276283b7d0SJohn Baldwin void
3286283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
3296283b7d0SJohn Baldwin {
3306283b7d0SJohn Baldwin 
331dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
33221377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
3336c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING
334b784ffe9SDag-Erling Smørgrav 	if (m->acqtime != 0) {
3356c35e809SDag-Erling Smørgrav 		static const char *unknown = "(unknown)";
3366c35e809SDag-Erling Smørgrav 		struct mutex_prof *mpp;
337b784ffe9SDag-Erling Smørgrav 		u_int64_t acqtime, now;
3386c35e809SDag-Erling Smørgrav 		const char *p, *q;
339e6330704SDag-Erling Smørgrav 		volatile u_int hash;
3406c35e809SDag-Erling Smørgrav 
341b784ffe9SDag-Erling Smørgrav 		now = nanoseconds();
342b784ffe9SDag-Erling Smørgrav 		acqtime = m->acqtime;
343b784ffe9SDag-Erling Smørgrav 		m->acqtime = 0;
344b784ffe9SDag-Erling Smørgrav 		if (now <= acqtime)
3456c35e809SDag-Erling Smørgrav 			goto out;
3466c35e809SDag-Erling Smørgrav 		for (p = file; strncmp(p, "../", 3) == 0; p += 3)
3476c35e809SDag-Erling Smørgrav 			/* nothing */ ;
3486c35e809SDag-Erling Smørgrav 		if (p == NULL || *p == '\0')
3496c35e809SDag-Erling Smørgrav 			p = unknown;
3506c35e809SDag-Erling Smørgrav 		for (hash = line, q = p; *q != '\0'; ++q)
3516c35e809SDag-Erling Smørgrav 			hash = (hash * 2 + *q) % MPROF_HASH_SIZE;
3526c35e809SDag-Erling Smørgrav 		mtx_lock_spin(&mprof_mtx);
353e6330704SDag-Erling Smørgrav 		for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next)
3546c35e809SDag-Erling Smørgrav 			if (mpp->line == line && strcmp(mpp->file, p) == 0)
3556c35e809SDag-Erling Smørgrav 				break;
3566c35e809SDag-Erling Smørgrav 		if (mpp == NULL) {
3576c35e809SDag-Erling Smørgrav 			/* Just exit if we cannot get a trace buffer */
3586c35e809SDag-Erling Smørgrav 			if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) {
3596c35e809SDag-Erling Smørgrav 				++mutex_prof_rejected;
3606c35e809SDag-Erling Smørgrav 				goto unlock;
3616c35e809SDag-Erling Smørgrav 			}
3626c35e809SDag-Erling Smørgrav 			mpp = &mprof_buf[first_free_mprof_buf++];
3636c35e809SDag-Erling Smørgrav 			mpp->name = mtx_name(m);
3646c35e809SDag-Erling Smørgrav 			mpp->file = p;
3656c35e809SDag-Erling Smørgrav 			mpp->line = line;
366e6330704SDag-Erling Smørgrav 			mpp->next = mprof_hash[hash];
367e6330704SDag-Erling Smørgrav 			if (mprof_hash[hash] != NULL)
368e6330704SDag-Erling Smørgrav 				++mutex_prof_collisions;
3696c35e809SDag-Erling Smørgrav 			mprof_hash[hash] = mpp;
370e6330704SDag-Erling Smørgrav 			++mutex_prof_records;
3716c35e809SDag-Erling Smørgrav 		}
3726c35e809SDag-Erling Smørgrav 		/*
3736c35e809SDag-Erling Smørgrav 		 * Record if the mutex has been held longer now than ever
3746c35e809SDag-Erling Smørgrav 		 * before
3756c35e809SDag-Erling Smørgrav 		 */
376b784ffe9SDag-Erling Smørgrav 		if ((now - acqtime) > mpp->counter[MPROF_MAX])
377b784ffe9SDag-Erling Smørgrav 			mpp->counter[MPROF_MAX] = now - acqtime;
378b784ffe9SDag-Erling Smørgrav 		mpp->counter[MPROF_TOT] += now - acqtime;
379b784ffe9SDag-Erling Smørgrav 		mpp->counter[MPROF_CNT] += 1;
380b784ffe9SDag-Erling Smørgrav 		mpp->counter[MPROF_AVG] =
381b784ffe9SDag-Erling Smørgrav 		    mpp->counter[MPROF_TOT] / mpp->counter[MPROF_CNT];
3826c35e809SDag-Erling Smørgrav unlock:
3836c35e809SDag-Erling Smørgrav 		mtx_unlock_spin(&mprof_mtx);
3846c35e809SDag-Erling Smørgrav 	}
3856c35e809SDag-Erling Smørgrav out:
3866c35e809SDag-Erling Smørgrav #endif
387dde96c99SJohn Baldwin  	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
388dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
389dde96c99SJohn Baldwin 	    line);
390dde96c99SJohn Baldwin 	_rel_sleep_lock(m, curthread, opts, file, line);
3916283b7d0SJohn Baldwin }
3926283b7d0SJohn Baldwin 
3936283b7d0SJohn Baldwin void
3946283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
3956283b7d0SJohn Baldwin {
3966283b7d0SJohn Baldwin 
397dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
398e8fdcfb5SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0
399dde96c99SJohn Baldwin 	_get_spin_lock(m, curthread, opts, file, line);
400e8fdcfb5SJohn Baldwin #else
401e8fdcfb5SJohn Baldwin 	critical_enter();
402e8fdcfb5SJohn Baldwin #endif
403dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
404dde96c99SJohn Baldwin 	    line);
405dde96c99SJohn Baldwin 	WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
4066283b7d0SJohn Baldwin }
4076283b7d0SJohn Baldwin 
4086283b7d0SJohn Baldwin void
4096283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
4106283b7d0SJohn Baldwin {
4116283b7d0SJohn Baldwin 
412dde96c99SJohn Baldwin 	MPASS(curthread != NULL);
41321377ce0SJohn Baldwin 	mtx_assert(m, MA_OWNED);
414dde96c99SJohn Baldwin  	WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
415dde96c99SJohn Baldwin 	LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
416dde96c99SJohn Baldwin 	    line);
417e8fdcfb5SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0
418dde96c99SJohn Baldwin 	_rel_spin_lock(m);
419e8fdcfb5SJohn Baldwin #else
420e8fdcfb5SJohn Baldwin 	critical_exit();
421e8fdcfb5SJohn Baldwin #endif
4226283b7d0SJohn Baldwin }
4236283b7d0SJohn Baldwin 
4246283b7d0SJohn Baldwin /*
4259ed346baSBosko Milekic  * The important part of mtx_trylock{,_flags}()
4269ed346baSBosko Milekic  * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that
4279ed346baSBosko Milekic  * if we're called, it's because we know we don't already own this lock.
4280cde2e34SJason Evans  */
4290cde2e34SJason Evans int
4309ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
4310cde2e34SJason Evans {
4320cde2e34SJason Evans 	int rval;
4330cde2e34SJason Evans 
434b40ce416SJulian Elischer 	MPASS(curthread != NULL);
4359ed346baSBosko Milekic 
436b40ce416SJulian Elischer 	rval = _obtain_lock(m, curthread);
4379ed346baSBosko Milekic 
43819284646SJohn Baldwin 	LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
43919284646SJohn Baldwin 	if (rval) {
4409ed346baSBosko Milekic 		/*
4419ed346baSBosko Milekic 		 * We do not handle recursion in _mtx_trylock; see the
4429ed346baSBosko Milekic 		 * note at the top of the routine.
4439ed346baSBosko Milekic 		 */
4445746a1d8SBosko Milekic 		KASSERT(!mtx_recursed(m),
4455746a1d8SBosko Milekic 		    ("mtx_trylock() called on a recursed mutex"));
4462d96f0b1SJohn Baldwin 		WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
4472d96f0b1SJohn Baldwin 		    file, line);
4480cde2e34SJason Evans 	}
4499ed346baSBosko Milekic 
45019284646SJohn Baldwin 	return (rval);
4510cde2e34SJason Evans }
4520cde2e34SJason Evans 
4530cde2e34SJason Evans /*
4549ed346baSBosko Milekic  * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
4559ed346baSBosko Milekic  *
4569ed346baSBosko Milekic  * We call this if the lock is either contested (i.e. we need to go to
4579ed346baSBosko Milekic  * sleep waiting for it), or if we need to recurse on it.
4580cde2e34SJason Evans  */
4590cde2e34SJason Evans void
4609ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line)
46136412d79SJohn Baldwin {
462b40ce416SJulian Elischer 	struct thread *td = curthread;
4632498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
4642498cf8cSJohn Baldwin 	struct thread *owner;
4652498cf8cSJohn Baldwin #endif
46636412d79SJohn Baldwin 
467b40ce416SJulian Elischer 	if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)td) {
46836412d79SJohn Baldwin 		m->mtx_recurse++;
46908812b39SBosko Milekic 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
47019284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
4715746a1d8SBosko Milekic 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
47236412d79SJohn Baldwin 		return;
47336412d79SJohn Baldwin 	}
4749ed346baSBosko Milekic 
47519284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
47615ec816aSJohn Baldwin 		CTR4(KTR_LOCK,
47715ec816aSJohn Baldwin 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
47819284646SJohn Baldwin 		    m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
4791bd0eefbSJohn Baldwin 
480b40ce416SJulian Elischer 	while (!_obtain_lock(m, td)) {
481f5271ebcSJohn Baldwin 		uintptr_t v;
482b40ce416SJulian Elischer 		struct thread *td1;
48336412d79SJohn Baldwin 
4849ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
48536412d79SJohn Baldwin 		/*
4869ed346baSBosko Milekic 		 * Check if the lock has been released while spinning for
4879ed346baSBosko Milekic 		 * the sched_lock.
48836412d79SJohn Baldwin 		 */
48936412d79SJohn Baldwin 		if ((v = m->mtx_lock) == MTX_UNOWNED) {
4909ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
491703fc290SJohn Baldwin #ifdef __i386__
492703fc290SJohn Baldwin 			cpu_pause();
493703fc290SJohn Baldwin #endif
49436412d79SJohn Baldwin 			continue;
49536412d79SJohn Baldwin 		}
4969ed346baSBosko Milekic 
49736412d79SJohn Baldwin 		/*
4989ed346baSBosko Milekic 		 * The mutex was marked contested on release. This means that
499b40ce416SJulian Elischer 		 * there are threads blocked on it.
50036412d79SJohn Baldwin 		 */
50136412d79SJohn Baldwin 		if (v == MTX_CONTESTED) {
502b40ce416SJulian Elischer 			td1 = TAILQ_FIRST(&m->mtx_blocked);
503b40ce416SJulian Elischer 			MPASS(td1 != NULL);
504b40ce416SJulian Elischer 			m->mtx_lock = (uintptr_t)td | MTX_CONTESTED;
5059ed346baSBosko Milekic 
5062c100766SJulian Elischer 			if (td1->td_priority < td->td_priority)
5072c100766SJulian Elischer 				td->td_priority = td1->td_priority;
5089ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
50936412d79SJohn Baldwin 			return;
51036412d79SJohn Baldwin 		}
5119ed346baSBosko Milekic 
51236412d79SJohn Baldwin 		/*
5139ed346baSBosko Milekic 		 * If the mutex isn't already contested and a failure occurs
5149ed346baSBosko Milekic 		 * setting the contested bit, the mutex was either released
5159ed346baSBosko Milekic 		 * or the state of the MTX_RECURSED bit changed.
51636412d79SJohn Baldwin 		 */
51736412d79SJohn Baldwin 		if ((v & MTX_CONTESTED) == 0 &&
51836412d79SJohn Baldwin 		    !atomic_cmpset_ptr(&m->mtx_lock, (void *)v,
51936412d79SJohn Baldwin 			(void *)(v | MTX_CONTESTED))) {
5209ed346baSBosko Milekic 			mtx_unlock_spin(&sched_lock);
521703fc290SJohn Baldwin #ifdef __i386__
522703fc290SJohn Baldwin 			cpu_pause();
523703fc290SJohn Baldwin #endif
52436412d79SJohn Baldwin 			continue;
52536412d79SJohn Baldwin 		}
52636412d79SJohn Baldwin 
5272498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
5282498cf8cSJohn Baldwin 		/*
5292498cf8cSJohn Baldwin 		 * If the current owner of the lock is executing on another
5302498cf8cSJohn Baldwin 		 * CPU, spin instead of blocking.
5312498cf8cSJohn Baldwin 		 */
5322498cf8cSJohn Baldwin 		owner = (struct thread *)(v & MTX_FLAGMASK);
5332498cf8cSJohn Baldwin 		if (m != &Giant && owner->td_kse != NULL &&
5342498cf8cSJohn Baldwin 		    owner->td_kse->ke_oncpu != NOCPU) {
5352498cf8cSJohn Baldwin 			mtx_unlock_spin(&sched_lock);
536703fc290SJohn Baldwin #ifdef __i386__
537703fc290SJohn Baldwin 			cpu_pause();
538703fc290SJohn Baldwin #endif
5392498cf8cSJohn Baldwin 			continue;
5402498cf8cSJohn Baldwin 		}
5412498cf8cSJohn Baldwin #endif	/* SMP && ADAPTIVE_MUTEXES */
5422498cf8cSJohn Baldwin 
5439ed346baSBosko Milekic 		/*
5447feefcd6SJohn Baldwin 		 * We definitely must sleep for this lock.
5459ed346baSBosko Milekic 		 */
54636412d79SJohn Baldwin 		mtx_assert(m, MA_NOTOWNED);
54736412d79SJohn Baldwin 
54836412d79SJohn Baldwin #ifdef notyet
54936412d79SJohn Baldwin 		/*
5509ed346baSBosko Milekic 		 * If we're borrowing an interrupted thread's VM context, we
5519ed346baSBosko Milekic 		 * must clean up before going to sleep.
55236412d79SJohn Baldwin 		 */
553b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
554b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
55536412d79SJohn Baldwin 
55636412d79SJohn Baldwin 			if (it->it_interrupted) {
55719284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
55836412d79SJohn Baldwin 					CTR2(KTR_LOCK,
55915ec816aSJohn Baldwin 				    "_mtx_lock_sleep: %p interrupted %p",
56036412d79SJohn Baldwin 					    it, it->it_interrupted);
56136412d79SJohn Baldwin 				intr_thd_fixup(it);
56236412d79SJohn Baldwin 			}
56336412d79SJohn Baldwin 		}
56436412d79SJohn Baldwin #endif
56536412d79SJohn Baldwin 
5669ed346baSBosko Milekic 		/*
5679ed346baSBosko Milekic 		 * Put us on the list of threads blocked on this mutex.
5689ed346baSBosko Milekic 		 */
56936412d79SJohn Baldwin 		if (TAILQ_EMPTY(&m->mtx_blocked)) {
57018fc2ba9SJohn Baldwin 			td1 = mtx_owner(m);
571b40ce416SJulian Elischer 			LIST_INSERT_HEAD(&td1->td_contested, m, mtx_contested);
572b40ce416SJulian Elischer 			TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq);
57336412d79SJohn Baldwin 		} else {
574b40ce416SJulian Elischer 			TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq)
5752c100766SJulian Elischer 				if (td1->td_priority > td->td_priority)
57636412d79SJohn Baldwin 					break;
577b40ce416SJulian Elischer 			if (td1)
578b40ce416SJulian Elischer 				TAILQ_INSERT_BEFORE(td1, td, td_blkq);
57936412d79SJohn Baldwin 			else
580b40ce416SJulian Elischer 				TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq);
58136412d79SJohn Baldwin 		}
58236412d79SJohn Baldwin 
5839ed346baSBosko Milekic 		/*
5849ed346baSBosko Milekic 		 * Save who we're blocked on.
5859ed346baSBosko Milekic 		 */
586b40ce416SJulian Elischer 		td->td_blocked = m;
587b40ce416SJulian Elischer 		td->td_mtxname = m->mtx_object.lo_name;
588b40ce416SJulian Elischer 		td->td_proc->p_stat = SMTX;
589b40ce416SJulian Elischer 		propagate_priority(td);
5909ed346baSBosko Milekic 
59119284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
592562e4ffeSJohn Baldwin 			CTR3(KTR_LOCK,
593b40ce416SJulian Elischer 			    "_mtx_lock_sleep: p %p blocked on [%p] %s", td, m,
59419284646SJohn Baldwin 			    m->mtx_object.lo_name);
5959ed346baSBosko Milekic 
596b40ce416SJulian Elischer 		td->td_proc->p_stats->p_ru.ru_nvcsw++;
59720cdcc5bSJohn Baldwin 		mi_switch();
5989ed346baSBosko Milekic 
59919284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
60036412d79SJohn Baldwin 			CTR3(KTR_LOCK,
6019ed346baSBosko Milekic 			  "_mtx_lock_sleep: p %p free from blocked on [%p] %s",
602b40ce416SJulian Elischer 			  td, m, m->mtx_object.lo_name);
6039ed346baSBosko Milekic 
6049ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
60536412d79SJohn Baldwin 	}
6069ed346baSBosko Milekic 
60736412d79SJohn Baldwin 	return;
6089ed346baSBosko Milekic }
6099ed346baSBosko Milekic 
6109ed346baSBosko Milekic /*
6119ed346baSBosko Milekic  * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
6129ed346baSBosko Milekic  *
6139ed346baSBosko Milekic  * This is only called if we need to actually spin for the lock. Recursion
6149ed346baSBosko Milekic  * is handled inline.
6159ed346baSBosko Milekic  */
6169ed346baSBosko Milekic void
6177e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line)
61836412d79SJohn Baldwin {
61936412d79SJohn Baldwin 	int i = 0;
62036412d79SJohn Baldwin 
62119284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6225746a1d8SBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
6239ed346baSBosko Milekic 
62436412d79SJohn Baldwin 	for (;;) {
625b40ce416SJulian Elischer 		if (_obtain_lock(m, curthread))
62636412d79SJohn Baldwin 			break;
6279ed346baSBosko Milekic 
6287141f2adSJohn Baldwin 		/* Give interrupts a chance while we spin. */
6297e1f6dfeSJohn Baldwin 		critical_exit();
63036412d79SJohn Baldwin 		while (m->mtx_lock != MTX_UNOWNED) {
631703fc290SJohn Baldwin 			if (i++ < 10000000) {
632703fc290SJohn Baldwin #ifdef __i386__
633703fc290SJohn Baldwin 				cpu_pause();
634703fc290SJohn Baldwin #endif
63536412d79SJohn Baldwin 				continue;
636703fc290SJohn Baldwin 			}
6370e54ddadSJohn Baldwin 			if (i < 60000000)
63836412d79SJohn Baldwin 				DELAY(1);
63936412d79SJohn Baldwin #ifdef DDB
64036412d79SJohn Baldwin 			else if (!db_active)
64136412d79SJohn Baldwin #else
64236412d79SJohn Baldwin 			else
64336412d79SJohn Baldwin #endif
6449ed346baSBosko Milekic 				panic("spin lock %s held by %p for > 5 seconds",
64519284646SJohn Baldwin 				    m->mtx_object.lo_name, (void *)m->mtx_lock);
646703fc290SJohn Baldwin #ifdef __i386__
647703fc290SJohn Baldwin 			cpu_pause();
648703fc290SJohn Baldwin #endif
64936412d79SJohn Baldwin 		}
6507e1f6dfeSJohn Baldwin 		critical_enter();
65136412d79SJohn Baldwin 	}
65236412d79SJohn Baldwin 
65319284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6549ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
6559ed346baSBosko Milekic 
65636412d79SJohn Baldwin 	return;
65736412d79SJohn Baldwin }
65836412d79SJohn Baldwin 
6599ed346baSBosko Milekic /*
6609ed346baSBosko Milekic  * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
6619ed346baSBosko Milekic  *
6629ed346baSBosko Milekic  * We are only called here if the lock is recursed or contested (i.e. we
6639ed346baSBosko Milekic  * need to wake up a blocked thread).
6649ed346baSBosko Milekic  */
66536412d79SJohn Baldwin void
6669ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
66736412d79SJohn Baldwin {
668b40ce416SJulian Elischer 	struct thread *td, *td1;
66936412d79SJohn Baldwin 	struct mtx *m1;
67036412d79SJohn Baldwin 	int pri;
67136412d79SJohn Baldwin 
672b40ce416SJulian Elischer 	td = curthread;
6739ed346baSBosko Milekic 
67408812b39SBosko Milekic 	if (mtx_recursed(m)) {
67536412d79SJohn Baldwin 		if (--(m->mtx_recurse) == 0)
67608812b39SBosko Milekic 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
67719284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6789ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
67936412d79SJohn Baldwin 		return;
68036412d79SJohn Baldwin 	}
6819ed346baSBosko Milekic 
6829ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
68319284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
6849ed346baSBosko Milekic 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
6859ed346baSBosko Milekic 
686b40ce416SJulian Elischer 	td1 = TAILQ_FIRST(&m->mtx_blocked);
6872498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES)
6882498cf8cSJohn Baldwin 	if (td1 == NULL) {
6892498cf8cSJohn Baldwin 		_release_lock_quick(m);
6902498cf8cSJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
6912498cf8cSJohn Baldwin 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
6922498cf8cSJohn Baldwin 		mtx_unlock_spin(&sched_lock);
6932498cf8cSJohn Baldwin 		return;
6942498cf8cSJohn Baldwin 	}
6952498cf8cSJohn Baldwin #endif
696b40ce416SJulian Elischer 	MPASS(td->td_proc->p_magic == P_MAGIC);
697b40ce416SJulian Elischer 	MPASS(td1->td_proc->p_magic == P_MAGIC);
6989ed346baSBosko Milekic 
699b40ce416SJulian Elischer 	TAILQ_REMOVE(&m->mtx_blocked, td1, td_blkq);
7009ed346baSBosko Milekic 
70136412d79SJohn Baldwin 	if (TAILQ_EMPTY(&m->mtx_blocked)) {
70236412d79SJohn Baldwin 		LIST_REMOVE(m, mtx_contested);
70336412d79SJohn Baldwin 		_release_lock_quick(m);
70419284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7059ed346baSBosko Milekic 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
70636412d79SJohn Baldwin 	} else
7079ed346baSBosko Milekic 		atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED);
7089ed346baSBosko Milekic 
709d5a08a60SJake Burkholder 	pri = PRI_MAX;
710b40ce416SJulian Elischer 	LIST_FOREACH(m1, &td->td_contested, mtx_contested) {
7112c100766SJulian Elischer 		int cp = TAILQ_FIRST(&m1->mtx_blocked)->td_priority;
71236412d79SJohn Baldwin 		if (cp < pri)
71336412d79SJohn Baldwin 			pri = cp;
71436412d79SJohn Baldwin 	}
7159ed346baSBosko Milekic 
7162c100766SJulian Elischer 	if (pri > td->td_base_pri)
7172c100766SJulian Elischer 		pri = td->td_base_pri;
7182c100766SJulian Elischer 	td->td_priority = pri;
7199ed346baSBosko Milekic 
72019284646SJohn Baldwin 	if (LOCK_LOG_TEST(&m->mtx_object, opts))
7219ed346baSBosko Milekic 		CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p",
722b40ce416SJulian Elischer 		    m, td1);
7239ed346baSBosko Milekic 
724b40ce416SJulian Elischer 	td1->td_blocked = NULL;
725b40ce416SJulian Elischer 	td1->td_proc->p_stat = SRUN;
726b40ce416SJulian Elischer 	setrunqueue(td1);
7279ed346baSBosko Milekic 
7282c100766SJulian Elischer 	if (td->td_critnest == 1 && td1->td_priority < pri) {
72936412d79SJohn Baldwin #ifdef notyet
730b40ce416SJulian Elischer 		if (td->td_ithd != NULL) {
731b40ce416SJulian Elischer 			struct ithd *it = td->td_ithd;
73236412d79SJohn Baldwin 
73336412d79SJohn Baldwin 			if (it->it_interrupted) {
73419284646SJohn Baldwin 				if (LOCK_LOG_TEST(&m->mtx_object, opts))
73536412d79SJohn Baldwin 					CTR2(KTR_LOCK,
73615ec816aSJohn Baldwin 				    "_mtx_unlock_sleep: %p interrupted %p",
73736412d79SJohn Baldwin 					    it, it->it_interrupted);
73836412d79SJohn Baldwin 				intr_thd_fixup(it);
73936412d79SJohn Baldwin 			}
74036412d79SJohn Baldwin 		}
74136412d79SJohn Baldwin #endif
742b40ce416SJulian Elischer 		setrunqueue(td);
74319284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
744562e4ffeSJohn Baldwin 			CTR2(KTR_LOCK,
7459ed346baSBosko Milekic 			    "_mtx_unlock_sleep: %p switching out lock=%p", m,
7469ed346baSBosko Milekic 			    (void *)m->mtx_lock);
7479ed346baSBosko Milekic 
748b40ce416SJulian Elischer 		td->td_proc->p_stats->p_ru.ru_nivcsw++;
74936412d79SJohn Baldwin 		mi_switch();
75019284646SJohn Baldwin 		if (LOCK_LOG_TEST(&m->mtx_object, opts))
7519ed346baSBosko Milekic 			CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
75231271627SJohn Baldwin 			    m, (void *)m->mtx_lock);
75336412d79SJohn Baldwin 	}
75436412d79SJohn Baldwin 
7559ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
7569ed346baSBosko Milekic 
7579ed346baSBosko Milekic 	return;
7589ed346baSBosko Milekic }
7599ed346baSBosko Milekic 
7609ed346baSBosko Milekic /*
7619ed346baSBosko Milekic  * All the unlocking of MTX_SPIN locks is done inline.
7629ed346baSBosko Milekic  * See the _rel_spin_lock() macro for the details.
7639ed346baSBosko Milekic  */
7649ed346baSBosko Milekic 
7659ed346baSBosko Milekic /*
76615ec816aSJohn Baldwin  * The backing function for the INVARIANTS-enabled mtx_assert()
7679ed346baSBosko Milekic  */
7681103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT
7690cde2e34SJason Evans void
77056771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line)
7710cde2e34SJason Evans {
7725cb0fbe4SJohn Baldwin 
7735cb0fbe4SJohn Baldwin 	if (panicstr != NULL)
7745cb0fbe4SJohn Baldwin 		return;
775a10f4966SJake Burkholder 	switch (what) {
7760cde2e34SJason Evans 	case MA_OWNED:
7770cde2e34SJason Evans 	case MA_OWNED | MA_RECURSED:
7780cde2e34SJason Evans 	case MA_OWNED | MA_NOTRECURSED:
779a10f4966SJake Burkholder 		if (!mtx_owned(m))
7800cde2e34SJason Evans 			panic("mutex %s not owned at %s:%d",
78119284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
782a10f4966SJake Burkholder 		if (mtx_recursed(m)) {
783a10f4966SJake Burkholder 			if ((what & MA_NOTRECURSED) != 0)
7840cde2e34SJason Evans 				panic("mutex %s recursed at %s:%d",
78519284646SJohn Baldwin 				    m->mtx_object.lo_name, file, line);
786a10f4966SJake Burkholder 		} else if ((what & MA_RECURSED) != 0) {
7870cde2e34SJason Evans 			panic("mutex %s unrecursed at %s:%d",
78819284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7890cde2e34SJason Evans 		}
7900cde2e34SJason Evans 		break;
7910cde2e34SJason Evans 	case MA_NOTOWNED:
792a10f4966SJake Burkholder 		if (mtx_owned(m))
7930cde2e34SJason Evans 			panic("mutex %s owned at %s:%d",
79419284646SJohn Baldwin 			    m->mtx_object.lo_name, file, line);
7950cde2e34SJason Evans 		break;
7960cde2e34SJason Evans 	default:
79756771ca7SJason Evans 		panic("unknown mtx_assert at %s:%d", file, line);
7980cde2e34SJason Evans 	}
7990cde2e34SJason Evans }
8000cde2e34SJason Evans #endif
8010cde2e34SJason Evans 
8029ed346baSBosko Milekic /*
8039ed346baSBosko Milekic  * The MUTEX_DEBUG-enabled mtx_validate()
80419284646SJohn Baldwin  *
80519284646SJohn Baldwin  * Most of these checks have been moved off into the LO_INITIALIZED flag
80619284646SJohn Baldwin  * maintained by the witness code.
8079ed346baSBosko Milekic  */
80836412d79SJohn Baldwin #ifdef MUTEX_DEBUG
80936412d79SJohn Baldwin 
8104d77a549SAlfred Perlstein void	mtx_validate(struct mtx *);
81136412d79SJohn Baldwin 
81219284646SJohn Baldwin void
81319284646SJohn Baldwin mtx_validate(struct mtx *m)
81436412d79SJohn Baldwin {
81536412d79SJohn Baldwin 
81636412d79SJohn Baldwin /*
81736412d79SJohn Baldwin  * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
81836412d79SJohn Baldwin  * we can re-enable the kernacc() checks.
81936412d79SJohn Baldwin  */
82036412d79SJohn Baldwin #ifndef __alpha__
82176dcbd6fSBosko Milekic 	/*
82276dcbd6fSBosko Milekic 	 * Can't call kernacc() from early init386(), especially when
82376dcbd6fSBosko Milekic 	 * initializing Giant mutex, because some stuff in kernacc()
82476dcbd6fSBosko Milekic 	 * requires Giant itself.
82576dcbd6fSBosko Milekic 	 */
826ab07087eSBosko Milekic 	if (!cold)
827ab07087eSBosko Milekic 		if (!kernacc((caddr_t)m, sizeof(m),
828ab07087eSBosko Milekic 		    VM_PROT_READ | VM_PROT_WRITE))
82919284646SJohn Baldwin 			panic("Can't read and write to mutex %p", m);
83036412d79SJohn Baldwin #endif
83136412d79SJohn Baldwin }
83236412d79SJohn Baldwin #endif
83336412d79SJohn Baldwin 
8349ed346baSBosko Milekic /*
835c27b5699SAndrew R. Reiter  * General init routine used by the MTX_SYSINIT() macro.
836c27b5699SAndrew R. Reiter  */
837c27b5699SAndrew R. Reiter void
838c27b5699SAndrew R. Reiter mtx_sysinit(void *arg)
839c27b5699SAndrew R. Reiter {
840c27b5699SAndrew R. Reiter 	struct mtx_args *margs = arg;
841c27b5699SAndrew R. Reiter 
8420c88508aSJohn Baldwin 	mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
843c27b5699SAndrew R. Reiter }
844c27b5699SAndrew R. Reiter 
845c27b5699SAndrew R. Reiter /*
8469ed346baSBosko Milekic  * Mutex initialization routine; initialize lock `m' of type contained in
8470c88508aSJohn Baldwin  * `opts' with options contained in `opts' and name `name.'  The optional
8480c88508aSJohn Baldwin  * lock type `type' is used as a general lock category name for use with
8490c88508aSJohn Baldwin  * witness.
8509ed346baSBosko Milekic  */
85136412d79SJohn Baldwin void
8520c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts)
85336412d79SJohn Baldwin {
85419284646SJohn Baldwin 	struct lock_object *lock;
8559ed346baSBosko Milekic 
85619284646SJohn Baldwin 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
857f22a4b62SJeff Roberson 	    MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0);
8589ed346baSBosko Milekic 
85936412d79SJohn Baldwin #ifdef MUTEX_DEBUG
8609ed346baSBosko Milekic 	/* Diagnostic and error correction */
86119284646SJohn Baldwin 	mtx_validate(m);
8626936206eSJohn Baldwin #endif
86336412d79SJohn Baldwin 
86419284646SJohn Baldwin 	lock = &m->mtx_object;
8657ada5876SJohn Baldwin 	KASSERT((lock->lo_flags & LO_INITIALIZED) == 0,
8660c88508aSJohn Baldwin 	    ("mutex %s %p already initialized", name, m));
8677ada5876SJohn Baldwin 	bzero(m, sizeof(*m));
86819284646SJohn Baldwin 	if (opts & MTX_SPIN)
86919284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_spin;
87019284646SJohn Baldwin 	else
87119284646SJohn Baldwin 		lock->lo_class = &lock_class_mtx_sleep;
8720c88508aSJohn Baldwin 	lock->lo_name = name;
8730c88508aSJohn Baldwin 	lock->lo_type = type != NULL ? type : name;
87419284646SJohn Baldwin 	if (opts & MTX_QUIET)
87519284646SJohn Baldwin 		lock->lo_flags = LO_QUIET;
87619284646SJohn Baldwin 	if (opts & MTX_RECURSE)
87719284646SJohn Baldwin 		lock->lo_flags |= LO_RECURSABLE;
87819284646SJohn Baldwin 	if (opts & MTX_SLEEPABLE)
87919284646SJohn Baldwin 		lock->lo_flags |= LO_SLEEPABLE;
88019284646SJohn Baldwin 	if ((opts & MTX_NOWITNESS) == 0)
88119284646SJohn Baldwin 		lock->lo_flags |= LO_WITNESS;
882f22a4b62SJeff Roberson 	if (opts & MTX_DUPOK)
883f22a4b62SJeff Roberson 		lock->lo_flags |= LO_DUPOK;
88419284646SJohn Baldwin 
88519284646SJohn Baldwin 	m->mtx_lock = MTX_UNOWNED;
88636412d79SJohn Baldwin 	TAILQ_INIT(&m->mtx_blocked);
8879ed346baSBosko Milekic 
88819284646SJohn Baldwin 	LOCK_LOG_INIT(lock, opts);
889d1c1b841SJason Evans 
89019284646SJohn Baldwin 	WITNESS_INIT(lock);
89136412d79SJohn Baldwin }
89236412d79SJohn Baldwin 
8939ed346baSBosko Milekic /*
89419284646SJohn Baldwin  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
89519284646SJohn Baldwin  * passed in as a flag here because if the corresponding mtx_init() was
89619284646SJohn Baldwin  * called with MTX_QUIET set, then it will already be set in the mutex's
89719284646SJohn Baldwin  * flags.
8989ed346baSBosko Milekic  */
89936412d79SJohn Baldwin void
90036412d79SJohn Baldwin mtx_destroy(struct mtx *m)
90136412d79SJohn Baldwin {
90236412d79SJohn Baldwin 
90319284646SJohn Baldwin 	LOCK_LOG_DESTROY(&m->mtx_object, 0);
9049ed346baSBosko Milekic 
90519284646SJohn Baldwin 	if (!mtx_owned(m))
90619284646SJohn Baldwin 		MPASS(mtx_unowned(m));
90719284646SJohn Baldwin 	else {
90808812b39SBosko Milekic 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
9099ed346baSBosko Milekic 
91019284646SJohn Baldwin 		/* Tell witness this isn't locked to make it happy. */
911c86b6ff5SJohn Baldwin 		WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
912c86b6ff5SJohn Baldwin 		    __LINE__);
91336412d79SJohn Baldwin 	}
9140384fff8SJason Evans 
91519284646SJohn Baldwin 	WITNESS_DESTROY(&m->mtx_object);
9160384fff8SJason Evans }
917d23f5958SMatthew Dillon 
918d23f5958SMatthew Dillon /*
919c53c013bSJohn Baldwin  * Intialize the mutex code and system mutexes.  This is called from the MD
920c53c013bSJohn Baldwin  * startup code prior to mi_startup().  The per-CPU data space needs to be
921c53c013bSJohn Baldwin  * setup before this is called.
922c53c013bSJohn Baldwin  */
923c53c013bSJohn Baldwin void
924c53c013bSJohn Baldwin mutex_init(void)
925c53c013bSJohn Baldwin {
926c53c013bSJohn Baldwin 
927c53c013bSJohn Baldwin 	/* Setup thread0 so that mutexes work. */
928c53c013bSJohn Baldwin 	LIST_INIT(&thread0.td_contested);
929c53c013bSJohn Baldwin 
930c53c013bSJohn Baldwin 	/*
931c53c013bSJohn Baldwin 	 * Initialize mutexes.
932c53c013bSJohn Baldwin 	 */
9330c88508aSJohn Baldwin 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
9340c88508aSJohn Baldwin 	mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
9350c88508aSJohn Baldwin 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
936c53c013bSJohn Baldwin 	mtx_lock(&Giant);
937c53c013bSJohn Baldwin }
938c53c013bSJohn Baldwin 
939c53c013bSJohn Baldwin /*
940d23f5958SMatthew Dillon  * Encapsulated Giant mutex routines.  These routines provide encapsulation
941d23f5958SMatthew Dillon  * control for the Giant mutex, allowing sysctls to be used to turn on and
942d23f5958SMatthew Dillon  * off Giant around certain subsystems.  The default value for the sysctls
943d23f5958SMatthew Dillon  * are set to what developers believe is stable and working in regards to
944d23f5958SMatthew Dillon  * the Giant pushdown.  Developers should not turn off Giant via these
945d23f5958SMatthew Dillon  * sysctls unless they know what they are doing.
946d23f5958SMatthew Dillon  *
947d23f5958SMatthew Dillon  * Callers of mtx_lock_giant() are expected to pass the return value to an
948d23f5958SMatthew Dillon  * accompanying mtx_unlock_giant() later on.  If multiple subsystems are
949d23f5958SMatthew Dillon  * effected by a Giant wrap, all related sysctl variables must be zero for
950d23f5958SMatthew Dillon  * the subsystem call to operate without Giant (as determined by the caller).
951d23f5958SMatthew Dillon  */
952d23f5958SMatthew Dillon 
953d23f5958SMatthew Dillon SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation");
954d23f5958SMatthew Dillon 
955d23f5958SMatthew Dillon static int kern_giant_all = 0;
956d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, "");
957d23f5958SMatthew Dillon 
958d23f5958SMatthew Dillon int kern_giant_proc = 1;	/* Giant around PROC locks */
959d23f5958SMatthew Dillon int kern_giant_file = 1;	/* Giant around struct file & filedesc */
960735da6deSMatthew Dillon int kern_giant_ucred = 1;	/* Giant around ucred */
961d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, "");
962d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, "");
963735da6deSMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, "");
964d23f5958SMatthew Dillon 
965d23f5958SMatthew Dillon int
966d23f5958SMatthew Dillon mtx_lock_giant(int sysctlvar)
967d23f5958SMatthew Dillon {
968d23f5958SMatthew Dillon 	if (sysctlvar || kern_giant_all) {
969d23f5958SMatthew Dillon 		mtx_lock(&Giant);
970d23f5958SMatthew Dillon 		return(1);
971d23f5958SMatthew Dillon 	}
972d23f5958SMatthew Dillon 	return(0);
973d23f5958SMatthew Dillon }
974d23f5958SMatthew Dillon 
975d23f5958SMatthew Dillon void
976d23f5958SMatthew Dillon mtx_unlock_giant(int s)
977d23f5958SMatthew Dillon {
978d23f5958SMatthew Dillon 	if (s)
979d23f5958SMatthew Dillon 		mtx_unlock(&Giant);
980d23f5958SMatthew Dillon }
981d23f5958SMatthew Dillon 
982