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 /* 84c53c013bSJohn Baldwin * System-wide mutexes 85c53c013bSJohn Baldwin */ 86c53c013bSJohn Baldwin struct mtx sched_lock; 87c53c013bSJohn Baldwin struct mtx Giant; 88c53c013bSJohn Baldwin 89c53c013bSJohn Baldwin /* 909ed346baSBosko Milekic * Prototypes for non-exported routines. 919ed346baSBosko Milekic */ 92b40ce416SJulian Elischer static void propagate_priority(struct thread *); 9336412d79SJohn Baldwin 9436412d79SJohn Baldwin static void 95b40ce416SJulian Elischer propagate_priority(struct thread *td) 9636412d79SJohn Baldwin { 972c100766SJulian Elischer int pri = td->td_priority; 98b40ce416SJulian Elischer struct mtx *m = td->td_blocked; 9936412d79SJohn Baldwin 1001bd0eefbSJohn Baldwin mtx_assert(&sched_lock, MA_OWNED); 10136412d79SJohn Baldwin for (;;) { 102b40ce416SJulian Elischer struct thread *td1; 10336412d79SJohn Baldwin 104b40ce416SJulian Elischer td = mtx_owner(m); 10536412d79SJohn Baldwin 106b40ce416SJulian Elischer if (td == NULL) { 10736412d79SJohn Baldwin /* 10836412d79SJohn Baldwin * This really isn't quite right. Really 109b40ce416SJulian Elischer * ought to bump priority of thread that 11036412d79SJohn Baldwin * next acquires the mutex. 11136412d79SJohn Baldwin */ 11236412d79SJohn Baldwin MPASS(m->mtx_lock == MTX_CONTESTED); 11336412d79SJohn Baldwin return; 11436412d79SJohn Baldwin } 1159ed346baSBosko Milekic 116b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 117b40ce416SJulian Elischer KASSERT(td->td_proc->p_stat != SSLEEP, ("sleeping thread owns a mutex")); 1182c100766SJulian Elischer if (td->td_priority <= pri) /* lower is higher priority */ 11936412d79SJohn Baldwin return; 1201bd0eefbSJohn Baldwin 1211bd0eefbSJohn Baldwin /* 122b40ce416SJulian Elischer * Bump this thread's priority. 1231bd0eefbSJohn Baldwin */ 1242c100766SJulian Elischer td->td_priority = pri; 1251bd0eefbSJohn Baldwin 12636412d79SJohn Baldwin /* 12736412d79SJohn Baldwin * If lock holder is actually running, just bump priority. 12836412d79SJohn Baldwin */ 129b40ce416SJulian Elischer /* XXXKSE this test is not sufficient */ 130b40ce416SJulian Elischer if (td->td_kse && (td->td_kse->ke_oncpu != NOCPU)) { 131b40ce416SJulian Elischer MPASS(td->td_proc->p_stat == SRUN 132b40ce416SJulian Elischer || td->td_proc->p_stat == SZOMB 133b40ce416SJulian Elischer || td->td_proc->p_stat == SSTOP); 13436412d79SJohn Baldwin return; 13536412d79SJohn Baldwin } 136d5a08a60SJake Burkholder 1371b43703bSJohn Baldwin #ifndef SMP 1381b43703bSJohn Baldwin /* 139b40ce416SJulian Elischer * For UP, we check to see if td is curthread (this shouldn't 1401b43703bSJohn Baldwin * ever happen however as it would mean we are in a deadlock.) 1411b43703bSJohn Baldwin */ 142b40ce416SJulian Elischer KASSERT(td != curthread, ("Deadlock detected")); 1431b43703bSJohn Baldwin #endif 1441b43703bSJohn Baldwin 14536412d79SJohn Baldwin /* 146b40ce416SJulian Elischer * If on run queue move to new run queue, and quit. 147b40ce416SJulian Elischer * XXXKSE this gets a lot more complicated under threads 148b40ce416SJulian Elischer * but try anyhow. 14936412d79SJohn Baldwin */ 150b40ce416SJulian Elischer if (td->td_proc->p_stat == SRUN) { 151b40ce416SJulian Elischer MPASS(td->td_blocked == NULL); 152b40ce416SJulian Elischer remrunqueue(td); 153b40ce416SJulian Elischer setrunqueue(td); 15436412d79SJohn Baldwin return; 15536412d79SJohn Baldwin } 15636412d79SJohn Baldwin 15736412d79SJohn Baldwin /* 1581bd0eefbSJohn Baldwin * If we aren't blocked on a mutex, we should be. 15936412d79SJohn Baldwin */ 160b40ce416SJulian Elischer KASSERT(td->td_proc->p_stat == SMTX, ( 1611bd0eefbSJohn Baldwin "process %d(%s):%d holds %s but isn't blocked on a mutex\n", 162b40ce416SJulian Elischer td->td_proc->p_pid, td->td_proc->p_comm, td->td_proc->p_stat, 16319284646SJohn Baldwin m->mtx_object.lo_name)); 16436412d79SJohn Baldwin 16536412d79SJohn Baldwin /* 166b40ce416SJulian Elischer * Pick up the mutex that td is blocked on. 16736412d79SJohn Baldwin */ 168b40ce416SJulian Elischer m = td->td_blocked; 16936412d79SJohn Baldwin MPASS(m != NULL); 17036412d79SJohn Baldwin 17136412d79SJohn Baldwin /* 172b40ce416SJulian Elischer * Check if the thread needs to be moved up on 17336412d79SJohn Baldwin * the blocked chain 17436412d79SJohn Baldwin */ 175b40ce416SJulian Elischer if (td == TAILQ_FIRST(&m->mtx_blocked)) { 1761bd0eefbSJohn Baldwin continue; 1771bd0eefbSJohn Baldwin } 1789ed346baSBosko Milekic 179b40ce416SJulian Elischer td1 = TAILQ_PREV(td, threadqueue, td_blkq); 1802c100766SJulian Elischer if (td1->td_priority <= pri) { 18136412d79SJohn Baldwin continue; 18236412d79SJohn Baldwin } 18336412d79SJohn Baldwin 18436412d79SJohn Baldwin /* 185b40ce416SJulian Elischer * Remove thread from blocked chain and determine where 186b40ce416SJulian Elischer * it should be moved up to. Since we know that td1 has 187b40ce416SJulian Elischer * a lower priority than td, we know that at least one 188b40ce416SJulian Elischer * thread in the chain has a lower priority and that 189b40ce416SJulian Elischer * td1 will thus not be NULL after the loop. 19036412d79SJohn Baldwin */ 191b40ce416SJulian Elischer TAILQ_REMOVE(&m->mtx_blocked, td, td_blkq); 192b40ce416SJulian Elischer TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq) { 193b40ce416SJulian Elischer MPASS(td1->td_proc->p_magic == P_MAGIC); 1942c100766SJulian Elischer if (td1->td_priority > pri) 19536412d79SJohn Baldwin break; 19636412d79SJohn Baldwin } 1979ed346baSBosko Milekic 198b40ce416SJulian Elischer MPASS(td1 != NULL); 199b40ce416SJulian Elischer TAILQ_INSERT_BEFORE(td1, td, td_blkq); 20036412d79SJohn Baldwin CTR4(KTR_LOCK, 2018484de75SJohn Baldwin "propagate_priority: p %p moved before %p on [%p] %s", 202b40ce416SJulian Elischer td, td1, m, m->mtx_object.lo_name); 20336412d79SJohn Baldwin } 20436412d79SJohn Baldwin } 20536412d79SJohn Baldwin 2066c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 2076c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging"); 2086c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling"); 2096c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0; 2106c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW, 2116c35e809SDag-Erling Smørgrav &mutex_prof_enable, 0, "Enable tracing of mutex holdtime"); 2126c35e809SDag-Erling Smørgrav 2136c35e809SDag-Erling Smørgrav struct mutex_prof { 2146c35e809SDag-Erling Smørgrav const char *name; 2156c35e809SDag-Erling Smørgrav const char *file; 2166c35e809SDag-Erling Smørgrav int line; 2176c35e809SDag-Erling Smørgrav #define MPROF_MAX 0 2186c35e809SDag-Erling Smørgrav #define MPROF_TOT 1 2196c35e809SDag-Erling Smørgrav #define MPROF_CNT 2 2206c35e809SDag-Erling Smørgrav #define MPROF_AVG 3 221b784ffe9SDag-Erling Smørgrav u_int64_t counter[4]; 222e6330704SDag-Erling Smørgrav struct mutex_prof *next; 2236c35e809SDag-Erling Smørgrav }; 2246c35e809SDag-Erling Smørgrav 2256c35e809SDag-Erling Smørgrav /* 2266c35e809SDag-Erling Smørgrav * mprof_buf is a static pool of profiling records to avoid possible 2276c35e809SDag-Erling Smørgrav * reentrance of the memory allocation functions. 2286c35e809SDag-Erling Smørgrav * 2296c35e809SDag-Erling Smørgrav * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE. 2306c35e809SDag-Erling Smørgrav */ 231e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000 2326c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS]; 2336c35e809SDag-Erling Smørgrav static int first_free_mprof_buf; 234e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009 2356c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE]; 2366c35e809SDag-Erling Smørgrav 2376c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions; 2386c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD, 2396c35e809SDag-Erling Smørgrav &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded"); 2406c35e809SDag-Erling Smørgrav static int mutex_prof_records; 2416c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD, 2426c35e809SDag-Erling Smørgrav &mutex_prof_records, 0, "Number of profiling records"); 2436c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS; 2446c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD, 2456c35e809SDag-Erling Smørgrav &mutex_prof_maxrecords, 0, "Maximum number of profiling records"); 2466c35e809SDag-Erling Smørgrav static int mutex_prof_rejected; 2476c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD, 2486c35e809SDag-Erling Smørgrav &mutex_prof_rejected, 0, "Number of rejected profiling records"); 2496c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE; 2506c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD, 2516c35e809SDag-Erling Smørgrav &mutex_prof_hashsize, 0, "Hash size"); 2526c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0; 2536c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD, 2546c35e809SDag-Erling Smørgrav &mutex_prof_collisions, 0, "Number of hash collisions"); 2556c35e809SDag-Erling Smørgrav 2566c35e809SDag-Erling Smørgrav /* 2576c35e809SDag-Erling Smørgrav * mprof_mtx protects the profiling buffers and the hash. 2586c35e809SDag-Erling Smørgrav */ 2596c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx; 260e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET); 2616c35e809SDag-Erling Smørgrav 262b784ffe9SDag-Erling Smørgrav static u_int64_t 263b784ffe9SDag-Erling Smørgrav nanoseconds(void) 264b784ffe9SDag-Erling Smørgrav { 265b784ffe9SDag-Erling Smørgrav struct timespec tv; 266b784ffe9SDag-Erling Smørgrav 267b784ffe9SDag-Erling Smørgrav nanotime(&tv); 268b784ffe9SDag-Erling Smørgrav return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec); 269b784ffe9SDag-Erling Smørgrav } 270b784ffe9SDag-Erling Smørgrav 2716c35e809SDag-Erling Smørgrav static int 2726c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 2736c35e809SDag-Erling Smørgrav { 2746c35e809SDag-Erling Smørgrav struct sbuf *sb; 2756c35e809SDag-Erling Smørgrav int error, i; 2766c35e809SDag-Erling Smørgrav 2776c35e809SDag-Erling Smørgrav if (first_free_mprof_buf == 0) 2786c35e809SDag-Erling Smørgrav return SYSCTL_OUT(req, "No locking recorded", 2796c35e809SDag-Erling Smørgrav sizeof("No locking recorded")); 2806c35e809SDag-Erling Smørgrav 2816c35e809SDag-Erling Smørgrav sb = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND); 2826c35e809SDag-Erling Smørgrav sbuf_printf(sb, "%12s %12s %12s %12s %s\n", 2836c35e809SDag-Erling Smørgrav "max", "total", "count", "average", "name"); 2846c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 2856c35e809SDag-Erling Smørgrav for (i = 0; i < first_free_mprof_buf; ++i) 2866c35e809SDag-Erling Smørgrav sbuf_printf(sb, "%12llu %12llu %12llu %12llu %s:%d (%s)\n", 287b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_MAX] / 1000, 288b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_TOT] / 1000, 289b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_CNT], 290b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_AVG] / 1000, 2916c35e809SDag-Erling Smørgrav mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 2926c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 2936c35e809SDag-Erling Smørgrav sbuf_finish(sb); 2946c35e809SDag-Erling Smørgrav error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2956c35e809SDag-Erling Smørgrav sbuf_delete(sb); 2966c35e809SDag-Erling Smørgrav return (error); 2976c35e809SDag-Erling Smørgrav } 2986c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING|CTLFLAG_RD, 2996c35e809SDag-Erling Smørgrav NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 3006c35e809SDag-Erling Smørgrav #endif 3016c35e809SDag-Erling Smørgrav 3020cde2e34SJason Evans /* 3036283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 3046283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 3056283b7d0SJohn Baldwin */ 3066283b7d0SJohn Baldwin void 3076283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 3086283b7d0SJohn Baldwin { 3096283b7d0SJohn Baldwin 310dde96c99SJohn Baldwin MPASS(curthread != NULL); 311dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 312dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 313dde96c99SJohn Baldwin line); 314dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3156c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 3166c35e809SDag-Erling Smørgrav /* don't reset the timer when/if recursing */ 317b784ffe9SDag-Erling Smørgrav if (m->acqtime == 0) { 3186c35e809SDag-Erling Smørgrav m->file = file; 3196c35e809SDag-Erling Smørgrav m->line = line; 320b784ffe9SDag-Erling Smørgrav m->acqtime = mutex_prof_enable ? nanoseconds() : 0; 3216c35e809SDag-Erling Smørgrav ++mutex_prof_acquisitions; 3226c35e809SDag-Erling Smørgrav } 3236c35e809SDag-Erling Smørgrav #endif 3246283b7d0SJohn Baldwin } 3256283b7d0SJohn Baldwin 3266283b7d0SJohn Baldwin void 3276283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 3286283b7d0SJohn Baldwin { 3296283b7d0SJohn Baldwin 330dde96c99SJohn Baldwin MPASS(curthread != NULL); 33121377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 3326c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 333b784ffe9SDag-Erling Smørgrav if (m->acqtime != 0) { 3346c35e809SDag-Erling Smørgrav static const char *unknown = "(unknown)"; 3356c35e809SDag-Erling Smørgrav struct mutex_prof *mpp; 336b784ffe9SDag-Erling Smørgrav u_int64_t acqtime, now; 3376c35e809SDag-Erling Smørgrav const char *p, *q; 338e6330704SDag-Erling Smørgrav volatile u_int hash; 3396c35e809SDag-Erling Smørgrav 340b784ffe9SDag-Erling Smørgrav now = nanoseconds(); 341b784ffe9SDag-Erling Smørgrav acqtime = m->acqtime; 342b784ffe9SDag-Erling Smørgrav m->acqtime = 0; 343b784ffe9SDag-Erling Smørgrav if (now <= acqtime) 3446c35e809SDag-Erling Smørgrav goto out; 3456c35e809SDag-Erling Smørgrav for (p = file; strncmp(p, "../", 3) == 0; p += 3) 3466c35e809SDag-Erling Smørgrav /* nothing */ ; 3476c35e809SDag-Erling Smørgrav if (p == NULL || *p == '\0') 3486c35e809SDag-Erling Smørgrav p = unknown; 3496c35e809SDag-Erling Smørgrav for (hash = line, q = p; *q != '\0'; ++q) 3506c35e809SDag-Erling Smørgrav hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 3516c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 352e6330704SDag-Erling Smørgrav for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next) 3536c35e809SDag-Erling Smørgrav if (mpp->line == line && strcmp(mpp->file, p) == 0) 3546c35e809SDag-Erling Smørgrav break; 3556c35e809SDag-Erling Smørgrav if (mpp == NULL) { 3566c35e809SDag-Erling Smørgrav /* Just exit if we cannot get a trace buffer */ 3576c35e809SDag-Erling Smørgrav if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 3586c35e809SDag-Erling Smørgrav ++mutex_prof_rejected; 3596c35e809SDag-Erling Smørgrav goto unlock; 3606c35e809SDag-Erling Smørgrav } 3616c35e809SDag-Erling Smørgrav mpp = &mprof_buf[first_free_mprof_buf++]; 3626c35e809SDag-Erling Smørgrav mpp->name = mtx_name(m); 3636c35e809SDag-Erling Smørgrav mpp->file = p; 3646c35e809SDag-Erling Smørgrav mpp->line = line; 365e6330704SDag-Erling Smørgrav mpp->next = mprof_hash[hash]; 366e6330704SDag-Erling Smørgrav if (mprof_hash[hash] != NULL) 367e6330704SDag-Erling Smørgrav ++mutex_prof_collisions; 3686c35e809SDag-Erling Smørgrav mprof_hash[hash] = mpp; 369e6330704SDag-Erling Smørgrav ++mutex_prof_records; 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 7920c88508aSJohn Baldwin mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 793c27b5699SAndrew R. Reiter } 794c27b5699SAndrew R. Reiter 795c27b5699SAndrew R. Reiter /* 7969ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 7970c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 7980c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 7990c88508aSJohn Baldwin * witness. 8009ed346baSBosko Milekic */ 80136412d79SJohn Baldwin void 8020c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts) 80336412d79SJohn Baldwin { 80419284646SJohn Baldwin struct lock_object *lock; 8059ed346baSBosko Milekic 80619284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 807f22a4b62SJeff Roberson MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0); 8089ed346baSBosko Milekic 80936412d79SJohn Baldwin #ifdef MUTEX_DEBUG 8109ed346baSBosko Milekic /* Diagnostic and error correction */ 81119284646SJohn Baldwin mtx_validate(m); 8126936206eSJohn Baldwin #endif 81336412d79SJohn Baldwin 81419284646SJohn Baldwin lock = &m->mtx_object; 8157ada5876SJohn Baldwin KASSERT((lock->lo_flags & LO_INITIALIZED) == 0, 8160c88508aSJohn Baldwin ("mutex %s %p already initialized", name, m)); 8177ada5876SJohn Baldwin bzero(m, sizeof(*m)); 81819284646SJohn Baldwin if (opts & MTX_SPIN) 81919284646SJohn Baldwin lock->lo_class = &lock_class_mtx_spin; 82019284646SJohn Baldwin else 82119284646SJohn Baldwin lock->lo_class = &lock_class_mtx_sleep; 8220c88508aSJohn Baldwin lock->lo_name = name; 8230c88508aSJohn Baldwin lock->lo_type = type != NULL ? type : name; 82419284646SJohn Baldwin if (opts & MTX_QUIET) 82519284646SJohn Baldwin lock->lo_flags = LO_QUIET; 82619284646SJohn Baldwin if (opts & MTX_RECURSE) 82719284646SJohn Baldwin lock->lo_flags |= LO_RECURSABLE; 82819284646SJohn Baldwin if (opts & MTX_SLEEPABLE) 82919284646SJohn Baldwin lock->lo_flags |= LO_SLEEPABLE; 83019284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 83119284646SJohn Baldwin lock->lo_flags |= LO_WITNESS; 832f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 833f22a4b62SJeff Roberson lock->lo_flags |= LO_DUPOK; 83419284646SJohn Baldwin 83519284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 83636412d79SJohn Baldwin TAILQ_INIT(&m->mtx_blocked); 8379ed346baSBosko Milekic 83819284646SJohn Baldwin LOCK_LOG_INIT(lock, opts); 839d1c1b841SJason Evans 84019284646SJohn Baldwin WITNESS_INIT(lock); 84136412d79SJohn Baldwin } 84236412d79SJohn Baldwin 8439ed346baSBosko Milekic /* 84419284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 84519284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 84619284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 84719284646SJohn Baldwin * flags. 8489ed346baSBosko Milekic */ 84936412d79SJohn Baldwin void 85036412d79SJohn Baldwin mtx_destroy(struct mtx *m) 85136412d79SJohn Baldwin { 85236412d79SJohn Baldwin 85319284646SJohn Baldwin LOCK_LOG_DESTROY(&m->mtx_object, 0); 8549ed346baSBosko Milekic 85519284646SJohn Baldwin if (!mtx_owned(m)) 85619284646SJohn Baldwin MPASS(mtx_unowned(m)); 85719284646SJohn Baldwin else { 85808812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 8599ed346baSBosko Milekic 86019284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 861c86b6ff5SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 862c86b6ff5SJohn Baldwin __LINE__); 86336412d79SJohn Baldwin } 8640384fff8SJason Evans 86519284646SJohn Baldwin WITNESS_DESTROY(&m->mtx_object); 8660384fff8SJason Evans } 867d23f5958SMatthew Dillon 868d23f5958SMatthew Dillon /* 869c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 870c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 871c53c013bSJohn Baldwin * setup before this is called. 872c53c013bSJohn Baldwin */ 873c53c013bSJohn Baldwin void 874c53c013bSJohn Baldwin mutex_init(void) 875c53c013bSJohn Baldwin { 876c53c013bSJohn Baldwin 877c53c013bSJohn Baldwin /* Setup thread0 so that mutexes work. */ 878c53c013bSJohn Baldwin LIST_INIT(&thread0.td_contested); 879c53c013bSJohn Baldwin 880c53c013bSJohn Baldwin /* 881c53c013bSJohn Baldwin * Initialize mutexes. 882c53c013bSJohn Baldwin */ 8830c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 8840c88508aSJohn Baldwin mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE); 8850c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 886c53c013bSJohn Baldwin mtx_lock(&Giant); 887c53c013bSJohn Baldwin } 888c53c013bSJohn Baldwin 889c53c013bSJohn Baldwin /* 890d23f5958SMatthew Dillon * Encapsulated Giant mutex routines. These routines provide encapsulation 891d23f5958SMatthew Dillon * control for the Giant mutex, allowing sysctls to be used to turn on and 892d23f5958SMatthew Dillon * off Giant around certain subsystems. The default value for the sysctls 893d23f5958SMatthew Dillon * are set to what developers believe is stable and working in regards to 894d23f5958SMatthew Dillon * the Giant pushdown. Developers should not turn off Giant via these 895d23f5958SMatthew Dillon * sysctls unless they know what they are doing. 896d23f5958SMatthew Dillon * 897d23f5958SMatthew Dillon * Callers of mtx_lock_giant() are expected to pass the return value to an 898d23f5958SMatthew Dillon * accompanying mtx_unlock_giant() later on. If multiple subsystems are 899d23f5958SMatthew Dillon * effected by a Giant wrap, all related sysctl variables must be zero for 900d23f5958SMatthew Dillon * the subsystem call to operate without Giant (as determined by the caller). 901d23f5958SMatthew Dillon */ 902d23f5958SMatthew Dillon 903d23f5958SMatthew Dillon SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation"); 904d23f5958SMatthew Dillon 905d23f5958SMatthew Dillon static int kern_giant_all = 0; 906d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, ""); 907d23f5958SMatthew Dillon 908d23f5958SMatthew Dillon int kern_giant_proc = 1; /* Giant around PROC locks */ 909d23f5958SMatthew Dillon int kern_giant_file = 1; /* Giant around struct file & filedesc */ 910735da6deSMatthew Dillon int kern_giant_ucred = 1; /* Giant around ucred */ 911d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, ""); 912d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, ""); 913735da6deSMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, ""); 914d23f5958SMatthew Dillon 915d23f5958SMatthew Dillon int 916d23f5958SMatthew Dillon mtx_lock_giant(int sysctlvar) 917d23f5958SMatthew Dillon { 918d23f5958SMatthew Dillon if (sysctlvar || kern_giant_all) { 919d23f5958SMatthew Dillon mtx_lock(&Giant); 920d23f5958SMatthew Dillon return(1); 921d23f5958SMatthew Dillon } 922d23f5958SMatthew Dillon return(0); 923d23f5958SMatthew Dillon } 924d23f5958SMatthew Dillon 925d23f5958SMatthew Dillon void 926d23f5958SMatthew Dillon mtx_unlock_giant(int s) 927d23f5958SMatthew Dillon { 928d23f5958SMatthew Dillon if (s) 929d23f5958SMatthew Dillon mtx_unlock(&Giant); 930d23f5958SMatthew Dillon } 931d23f5958SMatthew Dillon 932