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]; 2226c35e809SDag-Erling Smørgrav }; 2236c35e809SDag-Erling Smørgrav 2246c35e809SDag-Erling Smørgrav /* 2256c35e809SDag-Erling Smørgrav * mprof_buf is a static pool of profiling records to avoid possible 2266c35e809SDag-Erling Smørgrav * reentrance of the memory allocation functions. 2276c35e809SDag-Erling Smørgrav * 2286c35e809SDag-Erling Smørgrav * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE. 2296c35e809SDag-Erling Smørgrav */ 2306c35e809SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 4096 2316c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS]; 2326c35e809SDag-Erling Smørgrav static int first_free_mprof_buf; 2336c35e809SDag-Erling Smørgrav #define MPROF_HASH_SIZE 32771 2346c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE]; 2356c35e809SDag-Erling Smørgrav 2366c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions; 2376c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD, 2386c35e809SDag-Erling Smørgrav &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded"); 2396c35e809SDag-Erling Smørgrav static int mutex_prof_records; 2406c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD, 2416c35e809SDag-Erling Smørgrav &mutex_prof_records, 0, "Number of profiling records"); 2426c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS; 2436c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD, 2446c35e809SDag-Erling Smørgrav &mutex_prof_maxrecords, 0, "Maximum number of profiling records"); 2456c35e809SDag-Erling Smørgrav static int mutex_prof_rejected; 2466c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD, 2476c35e809SDag-Erling Smørgrav &mutex_prof_rejected, 0, "Number of rejected profiling records"); 2486c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE; 2496c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD, 2506c35e809SDag-Erling Smørgrav &mutex_prof_hashsize, 0, "Hash size"); 2516c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0; 2526c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD, 2536c35e809SDag-Erling Smørgrav &mutex_prof_collisions, 0, "Number of hash collisions"); 2546c35e809SDag-Erling Smørgrav 2556c35e809SDag-Erling Smørgrav /* 2566c35e809SDag-Erling Smørgrav * mprof_mtx protects the profiling buffers and the hash. 2576c35e809SDag-Erling Smørgrav */ 2586c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx; 2596c35e809SDag-Erling Smørgrav 2606c35e809SDag-Erling Smørgrav static void 2616c35e809SDag-Erling Smørgrav mprof_init(void *arg __unused) 2626c35e809SDag-Erling Smørgrav { 2636c35e809SDag-Erling Smørgrav mtx_init(&mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET); 2646c35e809SDag-Erling Smørgrav } 2656c35e809SDag-Erling Smørgrav SYSINIT(mprofinit, SI_SUB_LOCK, SI_ORDER_ANY, mprof_init, NULL); 2666c35e809SDag-Erling Smørgrav 267b784ffe9SDag-Erling Smørgrav static u_int64_t 268b784ffe9SDag-Erling Smørgrav nanoseconds(void) 269b784ffe9SDag-Erling Smørgrav { 270b784ffe9SDag-Erling Smørgrav struct timespec tv; 271b784ffe9SDag-Erling Smørgrav 272b784ffe9SDag-Erling Smørgrav nanotime(&tv); 273b784ffe9SDag-Erling Smørgrav return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec); 274b784ffe9SDag-Erling Smørgrav } 275b784ffe9SDag-Erling Smørgrav 2766c35e809SDag-Erling Smørgrav static int 2776c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 2786c35e809SDag-Erling Smørgrav { 2796c35e809SDag-Erling Smørgrav struct sbuf *sb; 2806c35e809SDag-Erling Smørgrav int error, i; 2816c35e809SDag-Erling Smørgrav 2826c35e809SDag-Erling Smørgrav if (first_free_mprof_buf == 0) 2836c35e809SDag-Erling Smørgrav return SYSCTL_OUT(req, "No locking recorded", 2846c35e809SDag-Erling Smørgrav sizeof("No locking recorded")); 2856c35e809SDag-Erling Smørgrav 2866c35e809SDag-Erling Smørgrav sb = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND); 2876c35e809SDag-Erling Smørgrav sbuf_printf(sb, "%12s %12s %12s %12s %s\n", 2886c35e809SDag-Erling Smørgrav "max", "total", "count", "average", "name"); 2896c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 2906c35e809SDag-Erling Smørgrav for (i = 0; i < first_free_mprof_buf; ++i) 2916c35e809SDag-Erling Smørgrav sbuf_printf(sb, "%12llu %12llu %12llu %12llu %s:%d (%s)\n", 292b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_MAX] / 1000, 293b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_TOT] / 1000, 294b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_CNT], 295b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_AVG] / 1000, 2966c35e809SDag-Erling Smørgrav mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 2976c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 2986c35e809SDag-Erling Smørgrav sbuf_finish(sb); 2996c35e809SDag-Erling Smørgrav error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 3006c35e809SDag-Erling Smørgrav sbuf_delete(sb); 3016c35e809SDag-Erling Smørgrav return (error); 3026c35e809SDag-Erling Smørgrav } 3036c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING|CTLFLAG_RD, 3046c35e809SDag-Erling Smørgrav NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 3056c35e809SDag-Erling Smørgrav #endif 3066c35e809SDag-Erling Smørgrav 3070cde2e34SJason Evans /* 3086283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 3096283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 3106283b7d0SJohn Baldwin */ 3116283b7d0SJohn Baldwin void 3126283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 3136283b7d0SJohn Baldwin { 3146283b7d0SJohn Baldwin 315dde96c99SJohn Baldwin MPASS(curthread != NULL); 316dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 317dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 318dde96c99SJohn Baldwin line); 319dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3206c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 3216c35e809SDag-Erling Smørgrav /* don't reset the timer when/if recursing */ 322b784ffe9SDag-Erling Smørgrav if (m->acqtime == 0) { 3236c35e809SDag-Erling Smørgrav m->file = file; 3246c35e809SDag-Erling Smørgrav m->line = line; 325b784ffe9SDag-Erling Smørgrav m->acqtime = mutex_prof_enable ? nanoseconds() : 0; 3266c35e809SDag-Erling Smørgrav ++mutex_prof_acquisitions; 3276c35e809SDag-Erling Smørgrav } 3286c35e809SDag-Erling Smørgrav #endif 3296283b7d0SJohn Baldwin } 3306283b7d0SJohn Baldwin 3316283b7d0SJohn Baldwin void 3326283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 3336283b7d0SJohn Baldwin { 3346283b7d0SJohn Baldwin 335dde96c99SJohn Baldwin MPASS(curthread != NULL); 33621377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 3376c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 338b784ffe9SDag-Erling Smørgrav if (m->acqtime != 0) { 3396c35e809SDag-Erling Smørgrav static const char *unknown = "(unknown)"; 3406c35e809SDag-Erling Smørgrav struct mutex_prof *mpp; 341b784ffe9SDag-Erling Smørgrav u_int64_t acqtime, now; 3426c35e809SDag-Erling Smørgrav const char *p, *q; 3436c35e809SDag-Erling Smørgrav volatile u_int hash, n; 3446c35e809SDag-Erling Smørgrav 345b784ffe9SDag-Erling Smørgrav now = nanoseconds(); 346b784ffe9SDag-Erling Smørgrav acqtime = m->acqtime; 347b784ffe9SDag-Erling Smørgrav m->acqtime = 0; 348b784ffe9SDag-Erling Smørgrav if (now <= acqtime) 3496c35e809SDag-Erling Smørgrav goto out; 3506c35e809SDag-Erling Smørgrav for (p = file; strncmp(p, "../", 3) == 0; p += 3) 3516c35e809SDag-Erling Smørgrav /* nothing */ ; 3526c35e809SDag-Erling Smørgrav if (p == NULL || *p == '\0') 3536c35e809SDag-Erling Smørgrav p = unknown; 3546c35e809SDag-Erling Smørgrav for (hash = line, q = p; *q != '\0'; ++q) 3556c35e809SDag-Erling Smørgrav hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 3566c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 3576c35e809SDag-Erling Smørgrav n = hash; 3586c35e809SDag-Erling Smørgrav while ((mpp = mprof_hash[n]) != NULL) { 3596c35e809SDag-Erling Smørgrav if (mpp->line == line && strcmp(mpp->file, p) == 0) 3606c35e809SDag-Erling Smørgrav break; 3616c35e809SDag-Erling Smørgrav n = (n + 1) % MPROF_HASH_SIZE; 3626c35e809SDag-Erling Smørgrav } 3636c35e809SDag-Erling Smørgrav if (mpp == NULL) { 3646c35e809SDag-Erling Smørgrav /* Just exit if we cannot get a trace buffer */ 3656c35e809SDag-Erling Smørgrav if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 3666c35e809SDag-Erling Smørgrav ++mutex_prof_rejected; 3676c35e809SDag-Erling Smørgrav goto unlock; 3686c35e809SDag-Erling Smørgrav } 3696c35e809SDag-Erling Smørgrav mpp = &mprof_buf[first_free_mprof_buf++]; 3706c35e809SDag-Erling Smørgrav mpp->name = mtx_name(m); 3716c35e809SDag-Erling Smørgrav mpp->file = p; 3726c35e809SDag-Erling Smørgrav mpp->line = line; 3736c35e809SDag-Erling Smørgrav mutex_prof_collisions += n - hash; 3746c35e809SDag-Erling Smørgrav ++mutex_prof_records; 3756c35e809SDag-Erling Smørgrav mprof_hash[hash] = mpp; 3766c35e809SDag-Erling Smørgrav } 3776c35e809SDag-Erling Smørgrav /* 3786c35e809SDag-Erling Smørgrav * Record if the mutex has been held longer now than ever 3796c35e809SDag-Erling Smørgrav * before 3806c35e809SDag-Erling Smørgrav */ 381b784ffe9SDag-Erling Smørgrav if ((now - acqtime) > mpp->counter[MPROF_MAX]) 382b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_MAX] = now - acqtime; 383b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_TOT] += now - acqtime; 384b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_CNT] += 1; 385b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_AVG] = 386b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_TOT] / mpp->counter[MPROF_CNT]; 3876c35e809SDag-Erling Smørgrav unlock: 3886c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 3896c35e809SDag-Erling Smørgrav } 3906c35e809SDag-Erling Smørgrav out: 3916c35e809SDag-Erling Smørgrav #endif 392dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 393dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 394dde96c99SJohn Baldwin line); 395dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 3966283b7d0SJohn Baldwin } 3976283b7d0SJohn Baldwin 3986283b7d0SJohn Baldwin void 3996283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 4006283b7d0SJohn Baldwin { 4016283b7d0SJohn Baldwin 402dde96c99SJohn Baldwin MPASS(curthread != NULL); 403dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 404dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 405dde96c99SJohn Baldwin line); 406dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 4076283b7d0SJohn Baldwin } 4086283b7d0SJohn Baldwin 4096283b7d0SJohn Baldwin void 4106283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 4116283b7d0SJohn Baldwin { 4126283b7d0SJohn Baldwin 413dde96c99SJohn Baldwin MPASS(curthread != NULL); 41421377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 415dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 416dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 417dde96c99SJohn Baldwin line); 418dde96c99SJohn Baldwin _rel_spin_lock(m); 4196283b7d0SJohn Baldwin } 4206283b7d0SJohn Baldwin 4216283b7d0SJohn Baldwin /* 4229ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 4239ed346baSBosko Milekic * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that 4249ed346baSBosko Milekic * if we're called, it's because we know we don't already own this lock. 4250cde2e34SJason Evans */ 4260cde2e34SJason Evans int 4279ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 4280cde2e34SJason Evans { 4290cde2e34SJason Evans int rval; 4300cde2e34SJason Evans 431b40ce416SJulian Elischer MPASS(curthread != NULL); 4329ed346baSBosko Milekic 433b40ce416SJulian Elischer rval = _obtain_lock(m, curthread); 4349ed346baSBosko Milekic 43519284646SJohn Baldwin LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 43619284646SJohn Baldwin if (rval) { 4379ed346baSBosko Milekic /* 4389ed346baSBosko Milekic * We do not handle recursion in _mtx_trylock; see the 4399ed346baSBosko Milekic * note at the top of the routine. 4409ed346baSBosko Milekic */ 4415746a1d8SBosko Milekic KASSERT(!mtx_recursed(m), 4425746a1d8SBosko Milekic ("mtx_trylock() called on a recursed mutex")); 4432d96f0b1SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4442d96f0b1SJohn Baldwin file, line); 4450cde2e34SJason Evans } 4469ed346baSBosko Milekic 44719284646SJohn Baldwin return (rval); 4480cde2e34SJason Evans } 4490cde2e34SJason Evans 4500cde2e34SJason Evans /* 4519ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4529ed346baSBosko Milekic * 4539ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4549ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4550cde2e34SJason Evans */ 4560cde2e34SJason Evans void 4579ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line) 45836412d79SJohn Baldwin { 459b40ce416SJulian Elischer struct thread *td = curthread; 46036412d79SJohn Baldwin 461b40ce416SJulian Elischer if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)td) { 46236412d79SJohn Baldwin m->mtx_recurse++; 46308812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 46419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4655746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 46636412d79SJohn Baldwin return; 46736412d79SJohn Baldwin } 4689ed346baSBosko Milekic 46919284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 47015ec816aSJohn Baldwin CTR4(KTR_LOCK, 47115ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 47219284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 4731bd0eefbSJohn Baldwin 474b40ce416SJulian Elischer while (!_obtain_lock(m, td)) { 475f5271ebcSJohn Baldwin uintptr_t v; 476b40ce416SJulian Elischer struct thread *td1; 47736412d79SJohn Baldwin 4789ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 47936412d79SJohn Baldwin /* 4809ed346baSBosko Milekic * Check if the lock has been released while spinning for 4819ed346baSBosko Milekic * the sched_lock. 48236412d79SJohn Baldwin */ 48336412d79SJohn Baldwin if ((v = m->mtx_lock) == MTX_UNOWNED) { 4849ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 48536412d79SJohn Baldwin continue; 48636412d79SJohn Baldwin } 4879ed346baSBosko Milekic 48836412d79SJohn Baldwin /* 4899ed346baSBosko Milekic * The mutex was marked contested on release. This means that 490b40ce416SJulian Elischer * there are threads blocked on it. 49136412d79SJohn Baldwin */ 49236412d79SJohn Baldwin if (v == MTX_CONTESTED) { 493b40ce416SJulian Elischer td1 = TAILQ_FIRST(&m->mtx_blocked); 494b40ce416SJulian Elischer MPASS(td1 != NULL); 495b40ce416SJulian Elischer m->mtx_lock = (uintptr_t)td | MTX_CONTESTED; 4969ed346baSBosko Milekic 4972c100766SJulian Elischer if (td1->td_priority < td->td_priority) 4982c100766SJulian Elischer td->td_priority = td1->td_priority; 4999ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 50036412d79SJohn Baldwin return; 50136412d79SJohn Baldwin } 5029ed346baSBosko Milekic 50336412d79SJohn Baldwin /* 5049ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 5059ed346baSBosko Milekic * setting the contested bit, the mutex was either released 5069ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 50736412d79SJohn Baldwin */ 50836412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 50936412d79SJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, (void *)v, 51036412d79SJohn Baldwin (void *)(v | MTX_CONTESTED))) { 5119ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 51236412d79SJohn Baldwin continue; 51336412d79SJohn Baldwin } 51436412d79SJohn Baldwin 5159ed346baSBosko Milekic /* 5167feefcd6SJohn Baldwin * We definitely must sleep for this lock. 5179ed346baSBosko Milekic */ 51836412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 51936412d79SJohn Baldwin 52036412d79SJohn Baldwin #ifdef notyet 52136412d79SJohn Baldwin /* 5229ed346baSBosko Milekic * If we're borrowing an interrupted thread's VM context, we 5239ed346baSBosko Milekic * must clean up before going to sleep. 52436412d79SJohn Baldwin */ 525b40ce416SJulian Elischer if (td->td_ithd != NULL) { 526b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 52736412d79SJohn Baldwin 52836412d79SJohn Baldwin if (it->it_interrupted) { 52919284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 53036412d79SJohn Baldwin CTR2(KTR_LOCK, 53115ec816aSJohn Baldwin "_mtx_lock_sleep: %p interrupted %p", 53236412d79SJohn Baldwin it, it->it_interrupted); 53336412d79SJohn Baldwin intr_thd_fixup(it); 53436412d79SJohn Baldwin } 53536412d79SJohn Baldwin } 53636412d79SJohn Baldwin #endif 53736412d79SJohn Baldwin 5389ed346baSBosko Milekic /* 5399ed346baSBosko Milekic * Put us on the list of threads blocked on this mutex. 5409ed346baSBosko Milekic */ 54136412d79SJohn Baldwin if (TAILQ_EMPTY(&m->mtx_blocked)) { 54218fc2ba9SJohn Baldwin td1 = mtx_owner(m); 543b40ce416SJulian Elischer LIST_INSERT_HEAD(&td1->td_contested, m, mtx_contested); 544b40ce416SJulian Elischer TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq); 54536412d79SJohn Baldwin } else { 546b40ce416SJulian Elischer TAILQ_FOREACH(td1, &m->mtx_blocked, td_blkq) 5472c100766SJulian Elischer if (td1->td_priority > td->td_priority) 54836412d79SJohn Baldwin break; 549b40ce416SJulian Elischer if (td1) 550b40ce416SJulian Elischer TAILQ_INSERT_BEFORE(td1, td, td_blkq); 55136412d79SJohn Baldwin else 552b40ce416SJulian Elischer TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_blkq); 55336412d79SJohn Baldwin } 55436412d79SJohn Baldwin 5559ed346baSBosko Milekic /* 5569ed346baSBosko Milekic * Save who we're blocked on. 5579ed346baSBosko Milekic */ 558b40ce416SJulian Elischer td->td_blocked = m; 559b40ce416SJulian Elischer td->td_mtxname = m->mtx_object.lo_name; 560b40ce416SJulian Elischer td->td_proc->p_stat = SMTX; 561b40ce416SJulian Elischer propagate_priority(td); 5629ed346baSBosko Milekic 56319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 564562e4ffeSJohn Baldwin CTR3(KTR_LOCK, 565b40ce416SJulian Elischer "_mtx_lock_sleep: p %p blocked on [%p] %s", td, m, 56619284646SJohn Baldwin m->mtx_object.lo_name); 5679ed346baSBosko Milekic 568b40ce416SJulian Elischer td->td_proc->p_stats->p_ru.ru_nvcsw++; 56920cdcc5bSJohn Baldwin mi_switch(); 5709ed346baSBosko Milekic 57119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 57236412d79SJohn Baldwin CTR3(KTR_LOCK, 5739ed346baSBosko Milekic "_mtx_lock_sleep: p %p free from blocked on [%p] %s", 574b40ce416SJulian Elischer td, m, m->mtx_object.lo_name); 5759ed346baSBosko Milekic 5769ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 57736412d79SJohn Baldwin } 5789ed346baSBosko Milekic 57936412d79SJohn Baldwin return; 5809ed346baSBosko Milekic } 5819ed346baSBosko Milekic 5829ed346baSBosko Milekic /* 5839ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 5849ed346baSBosko Milekic * 5859ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 5869ed346baSBosko Milekic * is handled inline. 5879ed346baSBosko Milekic */ 5889ed346baSBosko Milekic void 5897e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line) 59036412d79SJohn Baldwin { 59136412d79SJohn Baldwin int i = 0; 59236412d79SJohn Baldwin 59319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 5945746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 5959ed346baSBosko Milekic 59636412d79SJohn Baldwin for (;;) { 597b40ce416SJulian Elischer if (_obtain_lock(m, curthread)) 59836412d79SJohn Baldwin break; 5999ed346baSBosko Milekic 6007141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 6017e1f6dfeSJohn Baldwin critical_exit(); 60236412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 603bf07c922SJohn Baldwin if (i++ < 10000000) 60436412d79SJohn Baldwin continue; 605bf07c922SJohn Baldwin if (i++ < 60000000) 60636412d79SJohn Baldwin DELAY(1); 60736412d79SJohn Baldwin #ifdef DDB 60836412d79SJohn Baldwin else if (!db_active) 60936412d79SJohn Baldwin #else 61036412d79SJohn Baldwin else 61136412d79SJohn Baldwin #endif 6129ed346baSBosko Milekic panic("spin lock %s held by %p for > 5 seconds", 61319284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock); 61436412d79SJohn Baldwin } 6157e1f6dfeSJohn Baldwin critical_enter(); 61636412d79SJohn Baldwin } 61736412d79SJohn Baldwin 61819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6199ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 6209ed346baSBosko Milekic 62136412d79SJohn Baldwin return; 62236412d79SJohn Baldwin } 62336412d79SJohn Baldwin 6249ed346baSBosko Milekic /* 6259ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 6269ed346baSBosko Milekic * 6279ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 6289ed346baSBosko Milekic * need to wake up a blocked thread). 6299ed346baSBosko Milekic */ 63036412d79SJohn Baldwin void 6319ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 63236412d79SJohn Baldwin { 633b40ce416SJulian Elischer struct thread *td, *td1; 63436412d79SJohn Baldwin struct mtx *m1; 63536412d79SJohn Baldwin int pri; 63636412d79SJohn Baldwin 637b40ce416SJulian Elischer td = curthread; 6389ed346baSBosko Milekic 63908812b39SBosko Milekic if (mtx_recursed(m)) { 64036412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 64108812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 64219284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6439ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 64436412d79SJohn Baldwin return; 64536412d79SJohn Baldwin } 6469ed346baSBosko Milekic 6479ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 64819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6499ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 6509ed346baSBosko Milekic 651b40ce416SJulian Elischer td1 = TAILQ_FIRST(&m->mtx_blocked); 652b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 653b40ce416SJulian Elischer MPASS(td1->td_proc->p_magic == P_MAGIC); 6549ed346baSBosko Milekic 655b40ce416SJulian Elischer TAILQ_REMOVE(&m->mtx_blocked, td1, td_blkq); 6569ed346baSBosko Milekic 65736412d79SJohn Baldwin if (TAILQ_EMPTY(&m->mtx_blocked)) { 65836412d79SJohn Baldwin LIST_REMOVE(m, mtx_contested); 65936412d79SJohn Baldwin _release_lock_quick(m); 66019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6619ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 66236412d79SJohn Baldwin } else 6639ed346baSBosko Milekic atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED); 6649ed346baSBosko Milekic 665d5a08a60SJake Burkholder pri = PRI_MAX; 666b40ce416SJulian Elischer LIST_FOREACH(m1, &td->td_contested, mtx_contested) { 6672c100766SJulian Elischer int cp = TAILQ_FIRST(&m1->mtx_blocked)->td_priority; 66836412d79SJohn Baldwin if (cp < pri) 66936412d79SJohn Baldwin pri = cp; 67036412d79SJohn Baldwin } 6719ed346baSBosko Milekic 6722c100766SJulian Elischer if (pri > td->td_base_pri) 6732c100766SJulian Elischer pri = td->td_base_pri; 6742c100766SJulian Elischer td->td_priority = pri; 6759ed346baSBosko Milekic 67619284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6779ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p", 678b40ce416SJulian Elischer m, td1); 6799ed346baSBosko Milekic 680b40ce416SJulian Elischer td1->td_blocked = NULL; 681b40ce416SJulian Elischer td1->td_proc->p_stat = SRUN; 682b40ce416SJulian Elischer setrunqueue(td1); 6839ed346baSBosko Milekic 6842c100766SJulian Elischer if (td->td_critnest == 1 && td1->td_priority < pri) { 68536412d79SJohn Baldwin #ifdef notyet 686b40ce416SJulian Elischer if (td->td_ithd != NULL) { 687b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 68836412d79SJohn Baldwin 68936412d79SJohn Baldwin if (it->it_interrupted) { 69019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 69136412d79SJohn Baldwin CTR2(KTR_LOCK, 69215ec816aSJohn Baldwin "_mtx_unlock_sleep: %p interrupted %p", 69336412d79SJohn Baldwin it, it->it_interrupted); 69436412d79SJohn Baldwin intr_thd_fixup(it); 69536412d79SJohn Baldwin } 69636412d79SJohn Baldwin } 69736412d79SJohn Baldwin #endif 698b40ce416SJulian Elischer setrunqueue(td); 69919284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 700562e4ffeSJohn Baldwin CTR2(KTR_LOCK, 7019ed346baSBosko Milekic "_mtx_unlock_sleep: %p switching out lock=%p", m, 7029ed346baSBosko Milekic (void *)m->mtx_lock); 7039ed346baSBosko Milekic 704b40ce416SJulian Elischer td->td_proc->p_stats->p_ru.ru_nivcsw++; 70536412d79SJohn Baldwin mi_switch(); 70619284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7079ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 70831271627SJohn Baldwin m, (void *)m->mtx_lock); 70936412d79SJohn Baldwin } 71036412d79SJohn Baldwin 7119ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 7129ed346baSBosko Milekic 7139ed346baSBosko Milekic return; 7149ed346baSBosko Milekic } 7159ed346baSBosko Milekic 7169ed346baSBosko Milekic /* 7179ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 7189ed346baSBosko Milekic * See the _rel_spin_lock() macro for the details. 7199ed346baSBosko Milekic */ 7209ed346baSBosko Milekic 7219ed346baSBosko Milekic /* 72215ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 7239ed346baSBosko Milekic */ 7241103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 7250cde2e34SJason Evans void 72656771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line) 7270cde2e34SJason Evans { 7285cb0fbe4SJohn Baldwin 7295cb0fbe4SJohn Baldwin if (panicstr != NULL) 7305cb0fbe4SJohn Baldwin return; 731a10f4966SJake Burkholder switch (what) { 7320cde2e34SJason Evans case MA_OWNED: 7330cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 7340cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 735a10f4966SJake Burkholder if (!mtx_owned(m)) 7360cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 73719284646SJohn Baldwin m->mtx_object.lo_name, file, line); 738a10f4966SJake Burkholder if (mtx_recursed(m)) { 739a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 7400cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 74119284646SJohn Baldwin m->mtx_object.lo_name, file, line); 742a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 7430cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 74419284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7450cde2e34SJason Evans } 7460cde2e34SJason Evans break; 7470cde2e34SJason Evans case MA_NOTOWNED: 748a10f4966SJake Burkholder if (mtx_owned(m)) 7490cde2e34SJason Evans panic("mutex %s owned at %s:%d", 75019284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7510cde2e34SJason Evans break; 7520cde2e34SJason Evans default: 75356771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 7540cde2e34SJason Evans } 7550cde2e34SJason Evans } 7560cde2e34SJason Evans #endif 7570cde2e34SJason Evans 7589ed346baSBosko Milekic /* 7599ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 76019284646SJohn Baldwin * 76119284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 76219284646SJohn Baldwin * maintained by the witness code. 7639ed346baSBosko Milekic */ 76436412d79SJohn Baldwin #ifdef MUTEX_DEBUG 76536412d79SJohn Baldwin 7664d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 76736412d79SJohn Baldwin 76819284646SJohn Baldwin void 76919284646SJohn Baldwin mtx_validate(struct mtx *m) 77036412d79SJohn Baldwin { 77136412d79SJohn Baldwin 77236412d79SJohn Baldwin /* 77336412d79SJohn Baldwin * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly 77436412d79SJohn Baldwin * we can re-enable the kernacc() checks. 77536412d79SJohn Baldwin */ 77636412d79SJohn Baldwin #ifndef __alpha__ 77776dcbd6fSBosko Milekic /* 77876dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 77976dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 78076dcbd6fSBosko Milekic * requires Giant itself. 78176dcbd6fSBosko Milekic */ 782ab07087eSBosko Milekic if (!cold) 783ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 784ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 78519284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 78636412d79SJohn Baldwin #endif 78736412d79SJohn Baldwin } 78836412d79SJohn Baldwin #endif 78936412d79SJohn Baldwin 7909ed346baSBosko Milekic /* 791c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 792c27b5699SAndrew R. Reiter */ 793c27b5699SAndrew R. Reiter void 794c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 795c27b5699SAndrew R. Reiter { 796c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 797c27b5699SAndrew R. Reiter 798c27b5699SAndrew R. Reiter mtx_init(margs->ma_mtx, margs->ma_desc, margs->ma_opts); 799c27b5699SAndrew R. Reiter } 800c27b5699SAndrew R. Reiter 801c27b5699SAndrew R. Reiter /* 8029ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 8039ed346baSBosko Milekic * `opts' with options contained in `opts' and description `description.' 8049ed346baSBosko Milekic */ 80536412d79SJohn Baldwin void 8069ed346baSBosko Milekic mtx_init(struct mtx *m, const char *description, int opts) 80736412d79SJohn Baldwin { 80819284646SJohn Baldwin struct lock_object *lock; 8099ed346baSBosko Milekic 81019284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 811f22a4b62SJeff Roberson MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0); 8129ed346baSBosko Milekic 81336412d79SJohn Baldwin #ifdef MUTEX_DEBUG 8149ed346baSBosko Milekic /* Diagnostic and error correction */ 81519284646SJohn Baldwin mtx_validate(m); 8166936206eSJohn Baldwin #endif 81736412d79SJohn Baldwin 81819284646SJohn Baldwin lock = &m->mtx_object; 8197ada5876SJohn Baldwin KASSERT((lock->lo_flags & LO_INITIALIZED) == 0, 8207ada5876SJohn Baldwin ("mutex %s %p already initialized", description, m)); 8217ada5876SJohn Baldwin bzero(m, sizeof(*m)); 82219284646SJohn Baldwin if (opts & MTX_SPIN) 82319284646SJohn Baldwin lock->lo_class = &lock_class_mtx_spin; 82419284646SJohn Baldwin else 82519284646SJohn Baldwin lock->lo_class = &lock_class_mtx_sleep; 82619284646SJohn Baldwin lock->lo_name = description; 82719284646SJohn Baldwin if (opts & MTX_QUIET) 82819284646SJohn Baldwin lock->lo_flags = LO_QUIET; 82919284646SJohn Baldwin if (opts & MTX_RECURSE) 83019284646SJohn Baldwin lock->lo_flags |= LO_RECURSABLE; 83119284646SJohn Baldwin if (opts & MTX_SLEEPABLE) 83219284646SJohn Baldwin lock->lo_flags |= LO_SLEEPABLE; 83319284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 83419284646SJohn Baldwin lock->lo_flags |= LO_WITNESS; 835f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 836f22a4b62SJeff Roberson lock->lo_flags |= LO_DUPOK; 83719284646SJohn Baldwin 83819284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 83936412d79SJohn Baldwin TAILQ_INIT(&m->mtx_blocked); 8409ed346baSBosko Milekic 84119284646SJohn Baldwin LOCK_LOG_INIT(lock, opts); 842d1c1b841SJason Evans 84319284646SJohn Baldwin WITNESS_INIT(lock); 84436412d79SJohn Baldwin } 84536412d79SJohn Baldwin 8469ed346baSBosko Milekic /* 84719284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 84819284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 84919284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 85019284646SJohn Baldwin * flags. 8519ed346baSBosko Milekic */ 85236412d79SJohn Baldwin void 85336412d79SJohn Baldwin mtx_destroy(struct mtx *m) 85436412d79SJohn Baldwin { 85536412d79SJohn Baldwin 85619284646SJohn Baldwin LOCK_LOG_DESTROY(&m->mtx_object, 0); 8579ed346baSBosko Milekic 85819284646SJohn Baldwin if (!mtx_owned(m)) 85919284646SJohn Baldwin MPASS(mtx_unowned(m)); 86019284646SJohn Baldwin else { 86108812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 8629ed346baSBosko Milekic 86319284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 864c86b6ff5SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 865c86b6ff5SJohn Baldwin __LINE__); 86636412d79SJohn Baldwin } 8670384fff8SJason Evans 86819284646SJohn Baldwin WITNESS_DESTROY(&m->mtx_object); 8690384fff8SJason Evans } 870d23f5958SMatthew Dillon 871d23f5958SMatthew Dillon /* 872c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 873c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 874c53c013bSJohn Baldwin * setup before this is called. 875c53c013bSJohn Baldwin */ 876c53c013bSJohn Baldwin void 877c53c013bSJohn Baldwin mutex_init(void) 878c53c013bSJohn Baldwin { 879c53c013bSJohn Baldwin 880c53c013bSJohn Baldwin /* Setup thread0 so that mutexes work. */ 881c53c013bSJohn Baldwin LIST_INIT(&thread0.td_contested); 882c53c013bSJohn Baldwin 883c53c013bSJohn Baldwin /* 884c53c013bSJohn Baldwin * Initialize mutexes. 885c53c013bSJohn Baldwin */ 886c53c013bSJohn Baldwin mtx_init(&Giant, "Giant", MTX_DEF | MTX_RECURSE); 887c53c013bSJohn Baldwin mtx_init(&sched_lock, "sched lock", MTX_SPIN | MTX_RECURSE); 888c53c013bSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", MTX_DEF | MTX_DUPOK); 889c53c013bSJohn Baldwin mtx_lock(&Giant); 890c53c013bSJohn Baldwin } 891c53c013bSJohn Baldwin 892c53c013bSJohn Baldwin /* 893d23f5958SMatthew Dillon * Encapsulated Giant mutex routines. These routines provide encapsulation 894d23f5958SMatthew Dillon * control for the Giant mutex, allowing sysctls to be used to turn on and 895d23f5958SMatthew Dillon * off Giant around certain subsystems. The default value for the sysctls 896d23f5958SMatthew Dillon * are set to what developers believe is stable and working in regards to 897d23f5958SMatthew Dillon * the Giant pushdown. Developers should not turn off Giant via these 898d23f5958SMatthew Dillon * sysctls unless they know what they are doing. 899d23f5958SMatthew Dillon * 900d23f5958SMatthew Dillon * Callers of mtx_lock_giant() are expected to pass the return value to an 901d23f5958SMatthew Dillon * accompanying mtx_unlock_giant() later on. If multiple subsystems are 902d23f5958SMatthew Dillon * effected by a Giant wrap, all related sysctl variables must be zero for 903d23f5958SMatthew Dillon * the subsystem call to operate without Giant (as determined by the caller). 904d23f5958SMatthew Dillon */ 905d23f5958SMatthew Dillon 906d23f5958SMatthew Dillon SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation"); 907d23f5958SMatthew Dillon 908d23f5958SMatthew Dillon static int kern_giant_all = 0; 909d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, ""); 910d23f5958SMatthew Dillon 911d23f5958SMatthew Dillon int kern_giant_proc = 1; /* Giant around PROC locks */ 912d23f5958SMatthew Dillon int kern_giant_file = 1; /* Giant around struct file & filedesc */ 913735da6deSMatthew Dillon int kern_giant_ucred = 1; /* Giant around ucred */ 914d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, ""); 915d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, ""); 916735da6deSMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, ""); 917d23f5958SMatthew Dillon 918d23f5958SMatthew Dillon int 919d23f5958SMatthew Dillon mtx_lock_giant(int sysctlvar) 920d23f5958SMatthew Dillon { 921d23f5958SMatthew Dillon if (sysctlvar || kern_giant_all) { 922d23f5958SMatthew Dillon mtx_lock(&Giant); 923d23f5958SMatthew Dillon return(1); 924d23f5958SMatthew Dillon } 925d23f5958SMatthew Dillon return(0); 926d23f5958SMatthew Dillon } 927d23f5958SMatthew Dillon 928d23f5958SMatthew Dillon void 929d23f5958SMatthew Dillon mtx_unlock_giant(int s) 930d23f5958SMatthew Dillon { 931d23f5958SMatthew Dillon if (s) 932d23f5958SMatthew Dillon mtx_unlock(&Giant); 933d23f5958SMatthew Dillon } 934d23f5958SMatthew Dillon 935