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 215b784ffe9SDag-Erling Smørgrav u_int64_t counter[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 261b784ffe9SDag-Erling Smørgrav static u_int64_t 262b784ffe9SDag-Erling Smørgrav nanoseconds(void) 263b784ffe9SDag-Erling Smørgrav { 264b784ffe9SDag-Erling Smørgrav struct timespec tv; 265b784ffe9SDag-Erling Smørgrav 266b784ffe9SDag-Erling Smørgrav nanotime(&tv); 267b784ffe9SDag-Erling Smørgrav return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec); 268b784ffe9SDag-Erling Smørgrav } 269b784ffe9SDag-Erling Smørgrav 2706c35e809SDag-Erling Smørgrav static int 2716c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 2726c35e809SDag-Erling Smørgrav { 2736c35e809SDag-Erling Smørgrav struct sbuf *sb; 2746c35e809SDag-Erling Smørgrav int error, i; 2756c35e809SDag-Erling Smørgrav 2766c35e809SDag-Erling Smørgrav if (first_free_mprof_buf == 0) 2776c35e809SDag-Erling Smørgrav return SYSCTL_OUT(req, "No locking recorded", 2786c35e809SDag-Erling Smørgrav sizeof("No locking recorded")); 2796c35e809SDag-Erling Smørgrav 2806c35e809SDag-Erling Smørgrav sb = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND); 2816c35e809SDag-Erling Smørgrav sbuf_printf(sb, "%12s %12s %12s %12s %s\n", 2826c35e809SDag-Erling Smørgrav "max", "total", "count", "average", "name"); 2836c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 2846c35e809SDag-Erling Smørgrav for (i = 0; i < first_free_mprof_buf; ++i) 2856c35e809SDag-Erling Smørgrav sbuf_printf(sb, "%12llu %12llu %12llu %12llu %s:%d (%s)\n", 286b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_MAX] / 1000, 287b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_TOT] / 1000, 288b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_CNT], 289b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_AVG] / 1000, 2906c35e809SDag-Erling Smørgrav mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 2916c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 2926c35e809SDag-Erling Smørgrav sbuf_finish(sb); 2936c35e809SDag-Erling Smørgrav error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2946c35e809SDag-Erling Smørgrav sbuf_delete(sb); 2956c35e809SDag-Erling Smørgrav return (error); 2966c35e809SDag-Erling Smørgrav } 2976c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING|CTLFLAG_RD, 2986c35e809SDag-Erling Smørgrav NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 2996c35e809SDag-Erling Smørgrav #endif 3006c35e809SDag-Erling Smørgrav 3010cde2e34SJason Evans /* 3026283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 3036283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 3046283b7d0SJohn Baldwin */ 3056283b7d0SJohn Baldwin void 3066283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 3076283b7d0SJohn Baldwin { 3086283b7d0SJohn Baldwin 309dde96c99SJohn Baldwin MPASS(curthread != NULL); 310dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 311dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 312dde96c99SJohn Baldwin line); 313dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3146c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 3156c35e809SDag-Erling Smørgrav /* don't reset the timer when/if recursing */ 316b784ffe9SDag-Erling Smørgrav if (m->acqtime == 0) { 3176c35e809SDag-Erling Smørgrav m->file = file; 3186c35e809SDag-Erling Smørgrav m->line = line; 319b784ffe9SDag-Erling Smørgrav m->acqtime = mutex_prof_enable ? nanoseconds() : 0; 3206c35e809SDag-Erling Smørgrav ++mutex_prof_acquisitions; 3216c35e809SDag-Erling Smørgrav } 3226c35e809SDag-Erling Smørgrav #endif 3236283b7d0SJohn Baldwin } 3246283b7d0SJohn Baldwin 3256283b7d0SJohn Baldwin void 3266283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 3276283b7d0SJohn Baldwin { 3286283b7d0SJohn Baldwin 329dde96c99SJohn Baldwin MPASS(curthread != NULL); 33021377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 3316c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 332b784ffe9SDag-Erling Smørgrav if (m->acqtime != 0) { 3336c35e809SDag-Erling Smørgrav static const char *unknown = "(unknown)"; 3346c35e809SDag-Erling Smørgrav struct mutex_prof *mpp; 335b784ffe9SDag-Erling Smørgrav u_int64_t acqtime, now; 3366c35e809SDag-Erling Smørgrav const char *p, *q; 3376c35e809SDag-Erling Smørgrav volatile u_int hash, n; 3386c35e809SDag-Erling Smørgrav 339b784ffe9SDag-Erling Smørgrav now = nanoseconds(); 340b784ffe9SDag-Erling Smørgrav acqtime = m->acqtime; 341b784ffe9SDag-Erling Smørgrav m->acqtime = 0; 342b784ffe9SDag-Erling Smørgrav if (now <= acqtime) 3436c35e809SDag-Erling Smørgrav goto out; 3446c35e809SDag-Erling Smørgrav for (p = file; strncmp(p, "../", 3) == 0; p += 3) 3456c35e809SDag-Erling Smørgrav /* nothing */ ; 3466c35e809SDag-Erling Smørgrav if (p == NULL || *p == '\0') 3476c35e809SDag-Erling Smørgrav p = unknown; 3486c35e809SDag-Erling Smørgrav for (hash = line, q = p; *q != '\0'; ++q) 3496c35e809SDag-Erling Smørgrav hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 3506c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 3516c35e809SDag-Erling Smørgrav n = hash; 3526c35e809SDag-Erling Smørgrav while ((mpp = mprof_hash[n]) != NULL) { 3536c35e809SDag-Erling Smørgrav if (mpp->line == line && strcmp(mpp->file, p) == 0) 3546c35e809SDag-Erling Smørgrav break; 3556c35e809SDag-Erling Smørgrav n = (n + 1) % MPROF_HASH_SIZE; 3566c35e809SDag-Erling Smørgrav } 3576c35e809SDag-Erling Smørgrav if (mpp == NULL) { 3586c35e809SDag-Erling Smørgrav /* Just exit if we cannot get a trace buffer */ 3596c35e809SDag-Erling Smørgrav if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 3606c35e809SDag-Erling Smørgrav ++mutex_prof_rejected; 3616c35e809SDag-Erling Smørgrav goto unlock; 3626c35e809SDag-Erling Smørgrav } 3636c35e809SDag-Erling Smørgrav mpp = &mprof_buf[first_free_mprof_buf++]; 3646c35e809SDag-Erling Smørgrav mpp->name = mtx_name(m); 3656c35e809SDag-Erling Smørgrav mpp->file = p; 3666c35e809SDag-Erling Smørgrav mpp->line = line; 3676c35e809SDag-Erling Smørgrav mutex_prof_collisions += n - hash; 3686c35e809SDag-Erling Smørgrav ++mutex_prof_records; 3696c35e809SDag-Erling Smørgrav mprof_hash[hash] = mpp; 3706c35e809SDag-Erling Smørgrav } 3716c35e809SDag-Erling Smørgrav /* 3726c35e809SDag-Erling Smørgrav * Record if the mutex has been held longer now than ever 3736c35e809SDag-Erling Smørgrav * before 3746c35e809SDag-Erling Smørgrav */ 375b784ffe9SDag-Erling Smørgrav if ((now - acqtime) > mpp->counter[MPROF_MAX]) 376b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_MAX] = now - acqtime; 377b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_TOT] += now - acqtime; 378b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_CNT] += 1; 379b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_AVG] = 380b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_TOT] / mpp->counter[MPROF_CNT]; 3816c35e809SDag-Erling Smørgrav unlock: 3826c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 3836c35e809SDag-Erling Smørgrav } 3846c35e809SDag-Erling Smørgrav out: 3856c35e809SDag-Erling Smørgrav #endif 386dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 387dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 388dde96c99SJohn Baldwin line); 389dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 3906283b7d0SJohn Baldwin } 3916283b7d0SJohn Baldwin 3926283b7d0SJohn Baldwin void 3936283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 3946283b7d0SJohn Baldwin { 3956283b7d0SJohn Baldwin 396dde96c99SJohn Baldwin MPASS(curthread != NULL); 397dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 398dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 399dde96c99SJohn Baldwin line); 400dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 4016283b7d0SJohn Baldwin } 4026283b7d0SJohn Baldwin 4036283b7d0SJohn Baldwin void 4046283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 4056283b7d0SJohn Baldwin { 4066283b7d0SJohn Baldwin 407dde96c99SJohn Baldwin MPASS(curthread != NULL); 40821377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 409dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 410dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 411dde96c99SJohn Baldwin line); 412dde96c99SJohn Baldwin _rel_spin_lock(m); 4136283b7d0SJohn Baldwin } 4146283b7d0SJohn Baldwin 4156283b7d0SJohn Baldwin /* 4169ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 4179ed346baSBosko Milekic * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that 4189ed346baSBosko Milekic * if we're called, it's because we know we don't already own this lock. 4190cde2e34SJason Evans */ 4200cde2e34SJason Evans int 4219ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 4220cde2e34SJason Evans { 4230cde2e34SJason Evans int rval; 4240cde2e34SJason Evans 425b40ce416SJulian Elischer MPASS(curthread != NULL); 4269ed346baSBosko Milekic 427b40ce416SJulian Elischer rval = _obtain_lock(m, curthread); 4289ed346baSBosko Milekic 42919284646SJohn Baldwin LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 43019284646SJohn Baldwin if (rval) { 4319ed346baSBosko Milekic /* 4329ed346baSBosko Milekic * We do not handle recursion in _mtx_trylock; see the 4339ed346baSBosko Milekic * note at the top of the routine. 4349ed346baSBosko Milekic */ 4355746a1d8SBosko Milekic KASSERT(!mtx_recursed(m), 4365746a1d8SBosko Milekic ("mtx_trylock() called on a recursed mutex")); 4372d96f0b1SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4382d96f0b1SJohn Baldwin file, line); 4390cde2e34SJason Evans } 4409ed346baSBosko Milekic 44119284646SJohn Baldwin return (rval); 4420cde2e34SJason Evans } 4430cde2e34SJason Evans 4440cde2e34SJason Evans /* 4459ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4469ed346baSBosko Milekic * 4479ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4489ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4490cde2e34SJason Evans */ 4500cde2e34SJason Evans void 4519ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line) 45236412d79SJohn Baldwin { 453b40ce416SJulian Elischer struct thread *td = curthread; 45436412d79SJohn Baldwin 455b40ce416SJulian Elischer if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)td) { 45636412d79SJohn Baldwin m->mtx_recurse++; 45708812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 45819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4595746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 46036412d79SJohn Baldwin return; 46136412d79SJohn Baldwin } 4629ed346baSBosko Milekic 46319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 46415ec816aSJohn Baldwin CTR4(KTR_LOCK, 46515ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 46619284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 4671bd0eefbSJohn Baldwin 468b40ce416SJulian Elischer while (!_obtain_lock(m, td)) { 469f5271ebcSJohn Baldwin uintptr_t v; 470b40ce416SJulian Elischer struct thread *td1; 47136412d79SJohn Baldwin 4729ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 47336412d79SJohn Baldwin /* 4749ed346baSBosko Milekic * Check if the lock has been released while spinning for 4759ed346baSBosko Milekic * the sched_lock. 47636412d79SJohn Baldwin */ 47736412d79SJohn Baldwin if ((v = m->mtx_lock) == MTX_UNOWNED) { 4789ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 47936412d79SJohn Baldwin continue; 48036412d79SJohn Baldwin } 4819ed346baSBosko Milekic 48236412d79SJohn Baldwin /* 4839ed346baSBosko Milekic * The mutex was marked contested on release. This means that 484b40ce416SJulian Elischer * there are threads blocked on it. 48536412d79SJohn Baldwin */ 48636412d79SJohn Baldwin if (v == MTX_CONTESTED) { 487b40ce416SJulian Elischer td1 = TAILQ_FIRST(&m->mtx_blocked); 488b40ce416SJulian Elischer MPASS(td1 != NULL); 489b40ce416SJulian Elischer m->mtx_lock = (uintptr_t)td | MTX_CONTESTED; 4909ed346baSBosko Milekic 4912c100766SJulian Elischer if (td1->td_priority < td->td_priority) 4922c100766SJulian Elischer td->td_priority = td1->td_priority; 4939ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 49436412d79SJohn Baldwin return; 49536412d79SJohn Baldwin } 4969ed346baSBosko Milekic 49736412d79SJohn Baldwin /* 4989ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 4999ed346baSBosko Milekic * setting the contested bit, the mutex was either released 5009ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 50136412d79SJohn Baldwin */ 50236412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 50336412d79SJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, (void *)v, 50436412d79SJohn Baldwin (void *)(v | MTX_CONTESTED))) { 5059ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 50636412d79SJohn Baldwin continue; 50736412d79SJohn Baldwin } 50836412d79SJohn Baldwin 5099ed346baSBosko Milekic /* 5107feefcd6SJohn Baldwin * We definitely must sleep for this lock. 5119ed346baSBosko Milekic */ 51236412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 51336412d79SJohn Baldwin 51436412d79SJohn Baldwin #ifdef notyet 51536412d79SJohn Baldwin /* 5169ed346baSBosko Milekic * If we're borrowing an interrupted thread's VM context, we 5179ed346baSBosko Milekic * must clean up before going to sleep. 51836412d79SJohn Baldwin */ 519b40ce416SJulian Elischer if (td->td_ithd != NULL) { 520b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 52136412d79SJohn Baldwin 52236412d79SJohn Baldwin if (it->it_interrupted) { 52319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 52436412d79SJohn Baldwin CTR2(KTR_LOCK, 52515ec816aSJohn Baldwin "_mtx_lock_sleep: %p interrupted %p", 52636412d79SJohn Baldwin it, it->it_interrupted); 52736412d79SJohn Baldwin intr_thd_fixup(it); 52836412d79SJohn Baldwin } 52936412d79SJohn Baldwin } 53036412d79SJohn Baldwin #endif 53136412d79SJohn Baldwin 5329ed346baSBosko Milekic /* 5339ed346baSBosko Milekic * Put us on the list of threads blocked on this mutex. 5349ed346baSBosko Milekic */ 53536412d79SJohn Baldwin if (TAILQ_EMPTY(&m->mtx_blocked)) { 53618fc2ba9SJohn Baldwin td1 = mtx_owner(m); 537b40ce416SJulian Elischer LIST_INSERT_HEAD(&td1->td_contested, m, mtx_contested); 538b40ce416SJulian Elischer TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq); 53936412d79SJohn Baldwin } else { 540b40ce416SJulian Elischer TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq) 5412c100766SJulian Elischer if (td1->td_priority > td->td_priority) 54236412d79SJohn Baldwin break; 543b40ce416SJulian Elischer if (td1) 544b40ce416SJulian Elischer TAILQ_INSERT_BEFORE(td1, td, td_blkq); 54536412d79SJohn Baldwin else 546b40ce416SJulian Elischer TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq); 54736412d79SJohn Baldwin } 54836412d79SJohn Baldwin 5499ed346baSBosko Milekic /* 5509ed346baSBosko Milekic * Save who we're blocked on. 5519ed346baSBosko Milekic */ 552b40ce416SJulian Elischer td->td_blocked = m; 553b40ce416SJulian Elischer td->td_mtxname = m->mtx_object.lo_name; 554b40ce416SJulian Elischer td->td_proc->p_stat = SMTX; 555b40ce416SJulian Elischer propagate_priority(td); 5569ed346baSBosko Milekic 55719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 558562e4ffeSJohn Baldwin CTR3(KTR_LOCK, 559b40ce416SJulian Elischer "_mtx_lock_sleep: p %p blocked on [%p] %s", td, m, 56019284646SJohn Baldwin m->mtx_object.lo_name); 5619ed346baSBosko Milekic 562b40ce416SJulian Elischer td->td_proc->p_stats->p_ru.ru_nvcsw++; 56320cdcc5bSJohn Baldwin mi_switch(); 5649ed346baSBosko Milekic 56519284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 56636412d79SJohn Baldwin CTR3(KTR_LOCK, 5679ed346baSBosko Milekic "_mtx_lock_sleep: p %p free from blocked on [%p] %s", 568b40ce416SJulian Elischer td, m, m->mtx_object.lo_name); 5699ed346baSBosko Milekic 5709ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 57136412d79SJohn Baldwin } 5729ed346baSBosko Milekic 57336412d79SJohn Baldwin return; 5749ed346baSBosko Milekic } 5759ed346baSBosko Milekic 5769ed346baSBosko Milekic /* 5779ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 5789ed346baSBosko Milekic * 5799ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 5809ed346baSBosko Milekic * is handled inline. 5819ed346baSBosko Milekic */ 5829ed346baSBosko Milekic void 5837e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line) 58436412d79SJohn Baldwin { 58536412d79SJohn Baldwin int i = 0; 58636412d79SJohn Baldwin 58719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 5885746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 5899ed346baSBosko Milekic 59036412d79SJohn Baldwin for (;;) { 591b40ce416SJulian Elischer if (_obtain_lock(m, curthread)) 59236412d79SJohn Baldwin break; 5939ed346baSBosko Milekic 5947141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 5957e1f6dfeSJohn Baldwin critical_exit(); 59636412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 597bf07c922SJohn Baldwin if (i++ < 10000000) 59836412d79SJohn Baldwin continue; 599bf07c922SJohn Baldwin if (i++ < 60000000) 60036412d79SJohn Baldwin DELAY(1); 60136412d79SJohn Baldwin #ifdef DDB 60236412d79SJohn Baldwin else if (!db_active) 60336412d79SJohn Baldwin #else 60436412d79SJohn Baldwin else 60536412d79SJohn Baldwin #endif 6069ed346baSBosko Milekic panic("spin lock %s held by %p for > 5 seconds", 60719284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock); 60836412d79SJohn Baldwin } 6097e1f6dfeSJohn Baldwin critical_enter(); 61036412d79SJohn Baldwin } 61136412d79SJohn Baldwin 61219284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6139ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 6149ed346baSBosko Milekic 61536412d79SJohn Baldwin return; 61636412d79SJohn Baldwin } 61736412d79SJohn Baldwin 6189ed346baSBosko Milekic /* 6199ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 6209ed346baSBosko Milekic * 6219ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 6229ed346baSBosko Milekic * need to wake up a blocked thread). 6239ed346baSBosko Milekic */ 62436412d79SJohn Baldwin void 6259ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 62636412d79SJohn Baldwin { 627b40ce416SJulian Elischer struct thread *td, *td1; 62836412d79SJohn Baldwin struct mtx *m1; 62936412d79SJohn Baldwin int pri; 63036412d79SJohn Baldwin 631b40ce416SJulian Elischer td = curthread; 6329ed346baSBosko Milekic 63308812b39SBosko Milekic if (mtx_recursed(m)) { 63436412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 63508812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 63619284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6379ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 63836412d79SJohn Baldwin return; 63936412d79SJohn Baldwin } 6409ed346baSBosko Milekic 6419ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 64219284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6439ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 6449ed346baSBosko Milekic 645b40ce416SJulian Elischer td1 = TAILQ_FIRST(&m->mtx_blocked); 646b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 647b40ce416SJulian Elischer MPASS(td1->td_proc->p_magic == P_MAGIC); 6489ed346baSBosko Milekic 649b40ce416SJulian Elischer TAILQ_REMOVE(&m->mtx_blocked, td1, td_blkq); 6509ed346baSBosko Milekic 65136412d79SJohn Baldwin if (TAILQ_EMPTY(&m->mtx_blocked)) { 65236412d79SJohn Baldwin LIST_REMOVE(m, mtx_contested); 65336412d79SJohn Baldwin _release_lock_quick(m); 65419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6559ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 65636412d79SJohn Baldwin } else 6579ed346baSBosko Milekic atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED); 6589ed346baSBosko Milekic 659d5a08a60SJake Burkholder pri = PRI_MAX; 660b40ce416SJulian Elischer LIST_FOREACH(m1, &td->td_contested, mtx_contested) { 6612c100766SJulian Elischer int cp = TAILQ_FIRST(&m1->mtx_blocked)->td_priority; 66236412d79SJohn Baldwin if (cp < pri) 66336412d79SJohn Baldwin pri = cp; 66436412d79SJohn Baldwin } 6659ed346baSBosko Milekic 6662c100766SJulian Elischer if (pri > td->td_base_pri) 6672c100766SJulian Elischer pri = td->td_base_pri; 6682c100766SJulian Elischer td->td_priority = pri; 6699ed346baSBosko Milekic 67019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6719ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p", 672b40ce416SJulian Elischer m, td1); 6739ed346baSBosko Milekic 674b40ce416SJulian Elischer td1->td_blocked = NULL; 675b40ce416SJulian Elischer td1->td_proc->p_stat = SRUN; 676b40ce416SJulian Elischer setrunqueue(td1); 6779ed346baSBosko Milekic 6782c100766SJulian Elischer if (td->td_critnest == 1 && td1->td_priority < pri) { 67936412d79SJohn Baldwin #ifdef notyet 680b40ce416SJulian Elischer if (td->td_ithd != NULL) { 681b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 68236412d79SJohn Baldwin 68336412d79SJohn Baldwin if (it->it_interrupted) { 68419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 68536412d79SJohn Baldwin CTR2(KTR_LOCK, 68615ec816aSJohn Baldwin "_mtx_unlock_sleep: %p interrupted %p", 68736412d79SJohn Baldwin it, it->it_interrupted); 68836412d79SJohn Baldwin intr_thd_fixup(it); 68936412d79SJohn Baldwin } 69036412d79SJohn Baldwin } 69136412d79SJohn Baldwin #endif 692b40ce416SJulian Elischer setrunqueue(td); 69319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 694562e4ffeSJohn Baldwin CTR2(KTR_LOCK, 6959ed346baSBosko Milekic "_mtx_unlock_sleep: %p switching out lock=%p", m, 6969ed346baSBosko Milekic (void *)m->mtx_lock); 6979ed346baSBosko Milekic 698b40ce416SJulian Elischer td->td_proc->p_stats->p_ru.ru_nivcsw++; 69936412d79SJohn Baldwin mi_switch(); 70019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7019ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 70231271627SJohn Baldwin m, (void *)m->mtx_lock); 70336412d79SJohn Baldwin } 70436412d79SJohn Baldwin 7059ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 7069ed346baSBosko Milekic 7079ed346baSBosko Milekic return; 7089ed346baSBosko Milekic } 7099ed346baSBosko Milekic 7109ed346baSBosko Milekic /* 7119ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 7129ed346baSBosko Milekic * See the _rel_spin_lock() macro for the details. 7139ed346baSBosko Milekic */ 7149ed346baSBosko Milekic 7159ed346baSBosko Milekic /* 71615ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 7179ed346baSBosko Milekic */ 7181103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 7190cde2e34SJason Evans void 72056771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line) 7210cde2e34SJason Evans { 7225cb0fbe4SJohn Baldwin 7235cb0fbe4SJohn Baldwin if (panicstr != NULL) 7245cb0fbe4SJohn Baldwin return; 725a10f4966SJake Burkholder switch (what) { 7260cde2e34SJason Evans case MA_OWNED: 7270cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 7280cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 729a10f4966SJake Burkholder if (!mtx_owned(m)) 7300cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 73119284646SJohn Baldwin m->mtx_object.lo_name, file, line); 732a10f4966SJake Burkholder if (mtx_recursed(m)) { 733a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 7340cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 73519284646SJohn Baldwin m->mtx_object.lo_name, file, line); 736a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 7370cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 73819284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7390cde2e34SJason Evans } 7400cde2e34SJason Evans break; 7410cde2e34SJason Evans case MA_NOTOWNED: 742a10f4966SJake Burkholder if (mtx_owned(m)) 7430cde2e34SJason Evans panic("mutex %s owned at %s:%d", 74419284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7450cde2e34SJason Evans break; 7460cde2e34SJason Evans default: 74756771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 7480cde2e34SJason Evans } 7490cde2e34SJason Evans } 7500cde2e34SJason Evans #endif 7510cde2e34SJason Evans 7529ed346baSBosko Milekic /* 7539ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 75419284646SJohn Baldwin * 75519284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 75619284646SJohn Baldwin * maintained by the witness code. 7579ed346baSBosko Milekic */ 75836412d79SJohn Baldwin #ifdef MUTEX_DEBUG 75936412d79SJohn Baldwin 7604d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 76136412d79SJohn Baldwin 76219284646SJohn Baldwin void 76319284646SJohn Baldwin mtx_validate(struct mtx *m) 76436412d79SJohn Baldwin { 76536412d79SJohn Baldwin 76636412d79SJohn Baldwin /* 76736412d79SJohn Baldwin * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly 76836412d79SJohn Baldwin * we can re-enable the kernacc() checks. 76936412d79SJohn Baldwin */ 77036412d79SJohn Baldwin #ifndef __alpha__ 77176dcbd6fSBosko Milekic /* 77276dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 77376dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 77476dcbd6fSBosko Milekic * requires Giant itself. 77576dcbd6fSBosko Milekic */ 776ab07087eSBosko Milekic if (!cold) 777ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 778ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 77919284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 78036412d79SJohn Baldwin #endif 78136412d79SJohn Baldwin } 78236412d79SJohn Baldwin #endif 78336412d79SJohn Baldwin 7849ed346baSBosko Milekic /* 785c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 786c27b5699SAndrew R. Reiter */ 787c27b5699SAndrew R. Reiter void 788c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 789c27b5699SAndrew R. Reiter { 790c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 791c27b5699SAndrew R. Reiter 792c27b5699SAndrew R. Reiter mtx_init(margs->ma_mtx, margs->ma_desc, margs->ma_opts); 793c27b5699SAndrew R. Reiter } 794c27b5699SAndrew R. Reiter 795c27b5699SAndrew R. Reiter /* 7969ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 7979ed346baSBosko Milekic * `opts' with options contained in `opts' and description `description.' 7989ed346baSBosko Milekic */ 79936412d79SJohn Baldwin void 8009ed346baSBosko Milekic mtx_init(struct mtx *m, const char *description, int opts) 80136412d79SJohn Baldwin { 80219284646SJohn Baldwin struct lock_object *lock; 8039ed346baSBosko Milekic 80419284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 805f22a4b62SJeff Roberson MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0); 8069ed346baSBosko Milekic 80736412d79SJohn Baldwin #ifdef MUTEX_DEBUG 8089ed346baSBosko Milekic /* Diagnostic and error correction */ 80919284646SJohn Baldwin mtx_validate(m); 8106936206eSJohn Baldwin #endif 81136412d79SJohn Baldwin 81219284646SJohn Baldwin lock = &m->mtx_object; 8137ada5876SJohn Baldwin KASSERT((lock->lo_flags & LO_INITIALIZED) == 0, 8147ada5876SJohn Baldwin ("mutex %s %p already initialized", description, m)); 8157ada5876SJohn Baldwin bzero(m, sizeof(*m)); 81619284646SJohn Baldwin if (opts & MTX_SPIN) 81719284646SJohn Baldwin lock->lo_class = &lock_class_mtx_spin; 81819284646SJohn Baldwin else 81919284646SJohn Baldwin lock->lo_class = &lock_class_mtx_sleep; 82019284646SJohn Baldwin lock->lo_name = description; 82119284646SJohn Baldwin if (opts & MTX_QUIET) 82219284646SJohn Baldwin lock->lo_flags = LO_QUIET; 82319284646SJohn Baldwin if (opts & MTX_RECURSE) 82419284646SJohn Baldwin lock->lo_flags |= LO_RECURSABLE; 82519284646SJohn Baldwin if (opts & MTX_SLEEPABLE) 82619284646SJohn Baldwin lock->lo_flags |= LO_SLEEPABLE; 82719284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 82819284646SJohn Baldwin lock->lo_flags |= LO_WITNESS; 829f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 830f22a4b62SJeff Roberson lock->lo_flags |= LO_DUPOK; 83119284646SJohn Baldwin 83219284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 83336412d79SJohn Baldwin TAILQ_INIT(&m->mtx_blocked); 8349ed346baSBosko Milekic 83519284646SJohn Baldwin LOCK_LOG_INIT(lock, opts); 836d1c1b841SJason Evans 83719284646SJohn Baldwin WITNESS_INIT(lock); 83836412d79SJohn Baldwin } 83936412d79SJohn Baldwin 8409ed346baSBosko Milekic /* 84119284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 84219284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 84319284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 84419284646SJohn Baldwin * flags. 8459ed346baSBosko Milekic */ 84636412d79SJohn Baldwin void 84736412d79SJohn Baldwin mtx_destroy(struct mtx *m) 84836412d79SJohn Baldwin { 84936412d79SJohn Baldwin 85019284646SJohn Baldwin LOCK_LOG_DESTROY(&m->mtx_object, 0); 8519ed346baSBosko Milekic 85219284646SJohn Baldwin if (!mtx_owned(m)) 85319284646SJohn Baldwin MPASS(mtx_unowned(m)); 85419284646SJohn Baldwin else { 85508812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 8569ed346baSBosko Milekic 85719284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 858c86b6ff5SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 859c86b6ff5SJohn Baldwin __LINE__); 86036412d79SJohn Baldwin } 8610384fff8SJason Evans 86219284646SJohn Baldwin WITNESS_DESTROY(&m->mtx_object); 8630384fff8SJason Evans } 864d23f5958SMatthew Dillon 865d23f5958SMatthew Dillon /* 866d23f5958SMatthew Dillon * Encapsulated Giant mutex routines. These routines provide encapsulation 867d23f5958SMatthew Dillon * control for the Giant mutex, allowing sysctls to be used to turn on and 868d23f5958SMatthew Dillon * off Giant around certain subsystems. The default value for the sysctls 869d23f5958SMatthew Dillon * are set to what developers believe is stable and working in regards to 870d23f5958SMatthew Dillon * the Giant pushdown. Developers should not turn off Giant via these 871d23f5958SMatthew Dillon * sysctls unless they know what they are doing. 872d23f5958SMatthew Dillon * 873d23f5958SMatthew Dillon * Callers of mtx_lock_giant() are expected to pass the return value to an 874d23f5958SMatthew Dillon * accompanying mtx_unlock_giant() later on. If multiple subsystems are 875d23f5958SMatthew Dillon * effected by a Giant wrap, all related sysctl variables must be zero for 876d23f5958SMatthew Dillon * the subsystem call to operate without Giant (as determined by the caller). 877d23f5958SMatthew Dillon */ 878d23f5958SMatthew Dillon 879d23f5958SMatthew Dillon SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation"); 880d23f5958SMatthew Dillon 881d23f5958SMatthew Dillon static int kern_giant_all = 0; 882d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, ""); 883d23f5958SMatthew Dillon 884d23f5958SMatthew Dillon int kern_giant_proc = 1; /* Giant around PROC locks */ 885d23f5958SMatthew Dillon int kern_giant_file = 1; /* Giant around struct file & filedesc */ 886735da6deSMatthew Dillon int kern_giant_ucred = 1; /* Giant around ucred */ 887d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, ""); 888d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, ""); 889735da6deSMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, ""); 890d23f5958SMatthew Dillon 891d23f5958SMatthew Dillon int 892d23f5958SMatthew Dillon mtx_lock_giant(int sysctlvar) 893d23f5958SMatthew Dillon { 894d23f5958SMatthew Dillon if (sysctlvar || kern_giant_all) { 895d23f5958SMatthew Dillon mtx_lock(&Giant); 896d23f5958SMatthew Dillon return(1); 897d23f5958SMatthew Dillon } 898d23f5958SMatthew Dillon return(0); 899d23f5958SMatthew Dillon } 900d23f5958SMatthew Dillon 901d23f5958SMatthew Dillon void 902d23f5958SMatthew Dillon mtx_unlock_giant(int s) 903d23f5958SMatthew Dillon { 904d23f5958SMatthew Dillon if (s) 905d23f5958SMatthew Dillon mtx_unlock(&Giant); 906d23f5958SMatthew Dillon } 907d23f5958SMatthew Dillon 908