10384fff8SJason Evans /*- 20384fff8SJason Evans * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 30384fff8SJason Evans * 40384fff8SJason Evans * Redistribution and use in source and binary forms, with or without 50384fff8SJason Evans * modification, are permitted provided that the following conditions 60384fff8SJason Evans * are met: 70384fff8SJason Evans * 1. Redistributions of source code must retain the above copyright 80384fff8SJason Evans * notice, this list of conditions and the following disclaimer. 90384fff8SJason Evans * 2. Redistributions in binary form must reproduce the above copyright 100384fff8SJason Evans * notice, this list of conditions and the following disclaimer in the 110384fff8SJason Evans * documentation and/or other materials provided with the distribution. 120384fff8SJason Evans * 3. Berkeley Software Design Inc's name may not be used to endorse or 130384fff8SJason Evans * promote products derived from this software without specific prior 140384fff8SJason Evans * written permission. 150384fff8SJason Evans * 160384fff8SJason Evans * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 170384fff8SJason Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 180384fff8SJason Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 190384fff8SJason Evans * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 200384fff8SJason Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 210384fff8SJason Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 220384fff8SJason Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 230384fff8SJason Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 240384fff8SJason Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 250384fff8SJason Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 260384fff8SJason Evans * SUCH DAMAGE. 270384fff8SJason Evans * 280384fff8SJason Evans * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 2936412d79SJohn Baldwin * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 300384fff8SJason Evans * $FreeBSD$ 310384fff8SJason Evans */ 320384fff8SJason Evans 330384fff8SJason Evans /* 34ba48b69aSJohn Baldwin * Machine independent bits of mutex implementation. 350384fff8SJason Evans */ 360384fff8SJason Evans 372498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h" 389c36c934SJohn Baldwin #include "opt_ddb.h" 39a5a96a19SJohn Baldwin 400384fff8SJason Evans #include <sys/param.h> 416c35e809SDag-Erling Smørgrav #include <sys/systm.h> 4236412d79SJohn Baldwin #include <sys/bus.h> 4336412d79SJohn Baldwin #include <sys/kernel.h> 446c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 4519284646SJohn Baldwin #include <sys/lock.h> 46fb919e4dSMark Murray #include <sys/malloc.h> 4719284646SJohn Baldwin #include <sys/mutex.h> 480384fff8SJason Evans #include <sys/proc.h> 49c4f7a187SJohn Baldwin #include <sys/resourcevar.h> 50b43179fbSJeff Roberson #include <sys/sched.h> 516c35e809SDag-Erling Smørgrav #include <sys/sbuf.h> 52db586c8bSDag-Erling Smørgrav #include <sys/stdint.h> 53a5a96a19SJohn Baldwin #include <sys/sysctl.h> 5436412d79SJohn Baldwin #include <sys/vmmeter.h> 550384fff8SJason Evans 5636412d79SJohn Baldwin #include <machine/atomic.h> 5736412d79SJohn Baldwin #include <machine/bus.h> 5836412d79SJohn Baldwin #include <machine/clock.h> 590384fff8SJason Evans #include <machine/cpu.h> 6036412d79SJohn Baldwin 619c36c934SJohn Baldwin #include <ddb/ddb.h> 629c36c934SJohn Baldwin 6336412d79SJohn Baldwin #include <vm/vm.h> 6436412d79SJohn Baldwin #include <vm/vm_extern.h> 6536412d79SJohn Baldwin 660cde2e34SJason Evans /* 679ed346baSBosko Milekic * Internal utility macros. 680cde2e34SJason Evans */ 699ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 700cde2e34SJason Evans 719ed346baSBosko Milekic #define mtx_owner(m) (mtx_unowned((m)) ? NULL \ 72b40ce416SJulian Elischer : (struct thread *)((m)->mtx_lock & MTX_FLAGMASK)) 739ed346baSBosko Milekic 746a95e08fSJohn Baldwin /* XXXKSE This test will change. */ 756a95e08fSJohn Baldwin #define thread_running(td) \ 765853d37dSJohn Baldwin ((td)->td_kse != NULL && (td)->td_kse->ke_oncpu != NOCPU) 775853d37dSJohn Baldwin 780cde2e34SJason Evans /* 7919284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 800cde2e34SJason Evans */ 8119284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 8219284646SJohn Baldwin "sleep mutex", 8319284646SJohn Baldwin LC_SLEEPLOCK | LC_RECURSABLE 8419284646SJohn Baldwin }; 8519284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 8619284646SJohn Baldwin "spin mutex", 8719284646SJohn Baldwin LC_SPINLOCK | LC_RECURSABLE 888484de75SJohn Baldwin }; 898484de75SJohn Baldwin 909ed346baSBosko Milekic /* 91c53c013bSJohn Baldwin * System-wide mutexes 92c53c013bSJohn Baldwin */ 93c53c013bSJohn Baldwin struct mtx sched_lock; 94c53c013bSJohn Baldwin struct mtx Giant; 95c53c013bSJohn Baldwin 96c53c013bSJohn Baldwin /* 979ed346baSBosko Milekic * Prototypes for non-exported routines. 989ed346baSBosko Milekic */ 99b40ce416SJulian Elischer static void propagate_priority(struct thread *); 10036412d79SJohn Baldwin 10136412d79SJohn Baldwin static void 102b40ce416SJulian Elischer propagate_priority(struct thread *td) 10336412d79SJohn Baldwin { 1042c100766SJulian Elischer int pri = td->td_priority; 105b40ce416SJulian Elischer struct mtx *m = td->td_blocked; 10636412d79SJohn Baldwin 1071bd0eefbSJohn Baldwin mtx_assert(&sched_lock, MA_OWNED); 10836412d79SJohn Baldwin for (;;) { 109b40ce416SJulian Elischer struct thread *td1; 11036412d79SJohn Baldwin 111b40ce416SJulian Elischer td = mtx_owner(m); 11236412d79SJohn Baldwin 113b40ce416SJulian Elischer if (td == NULL) { 11436412d79SJohn Baldwin /* 11536412d79SJohn Baldwin * This really isn't quite right. Really 116b40ce416SJulian Elischer * ought to bump priority of thread that 11736412d79SJohn Baldwin * next acquires the mutex. 11836412d79SJohn Baldwin */ 11936412d79SJohn Baldwin MPASS(m->mtx_lock == MTX_CONTESTED); 12036412d79SJohn Baldwin return; 12136412d79SJohn Baldwin } 1229ed346baSBosko Milekic 123e602ba25SJulian Elischer MPASS(td->td_proc != NULL); 124b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 12571fad9fdSJulian Elischer KASSERT(!TD_IS_SLEEPING(td), ("sleeping thread owns a mutex")); 1262c100766SJulian Elischer if (td->td_priority <= pri) /* lower is higher priority */ 12736412d79SJohn Baldwin return; 1281bd0eefbSJohn Baldwin 1291bd0eefbSJohn Baldwin 13036412d79SJohn Baldwin /* 13136412d79SJohn Baldwin * If lock holder is actually running, just bump priority. 13236412d79SJohn Baldwin */ 13371fad9fdSJulian Elischer if (TD_IS_RUNNING(td)) { 134e602ba25SJulian Elischer td->td_priority = pri; 13536412d79SJohn Baldwin return; 13636412d79SJohn Baldwin } 137d5a08a60SJake Burkholder 1381b43703bSJohn Baldwin #ifndef SMP 1391b43703bSJohn Baldwin /* 140b40ce416SJulian Elischer * For UP, we check to see if td is curthread (this shouldn't 1411b43703bSJohn Baldwin * ever happen however as it would mean we are in a deadlock.) 1421b43703bSJohn Baldwin */ 143b40ce416SJulian Elischer KASSERT(td != curthread, ("Deadlock detected")); 1441b43703bSJohn Baldwin #endif 1451b43703bSJohn Baldwin 14636412d79SJohn Baldwin /* 147b40ce416SJulian Elischer * If on run queue move to new run queue, and quit. 148b40ce416SJulian Elischer * XXXKSE this gets a lot more complicated under threads 149b40ce416SJulian Elischer * but try anyhow. 15036412d79SJohn Baldwin */ 15171fad9fdSJulian Elischer if (TD_ON_RUNQ(td)) { 152b40ce416SJulian Elischer MPASS(td->td_blocked == NULL); 153b43179fbSJeff Roberson sched_prio(td, pri); 15436412d79SJohn Baldwin return; 15536412d79SJohn Baldwin } 156e602ba25SJulian Elischer /* 157e602ba25SJulian Elischer * Adjust for any other cases. 158e602ba25SJulian Elischer */ 159e602ba25SJulian Elischer td->td_priority = pri; 16036412d79SJohn Baldwin 16136412d79SJohn Baldwin /* 1621bd0eefbSJohn Baldwin * If we aren't blocked on a mutex, we should be. 16336412d79SJohn Baldwin */ 164551cf4e1SJohn Baldwin KASSERT(TD_ON_LOCK(td), ( 1651bd0eefbSJohn Baldwin "process %d(%s):%d holds %s but isn't blocked on a mutex\n", 166e602ba25SJulian Elischer td->td_proc->p_pid, td->td_proc->p_comm, td->td_state, 16719284646SJohn Baldwin m->mtx_object.lo_name)); 16836412d79SJohn Baldwin 16936412d79SJohn Baldwin /* 170b40ce416SJulian Elischer * Pick up the mutex that td is blocked on. 17136412d79SJohn Baldwin */ 172b40ce416SJulian Elischer m = td->td_blocked; 17336412d79SJohn Baldwin MPASS(m != NULL); 17436412d79SJohn Baldwin 17536412d79SJohn Baldwin /* 176b40ce416SJulian Elischer * Check if the thread needs to be moved up on 17736412d79SJohn Baldwin * the blocked chain 17836412d79SJohn Baldwin */ 179b40ce416SJulian Elischer if (td == TAILQ_FIRST(&m->mtx_blocked)) { 1801bd0eefbSJohn Baldwin continue; 1811bd0eefbSJohn Baldwin } 1829ed346baSBosko Milekic 183551cf4e1SJohn Baldwin td1 = TAILQ_PREV(td, threadqueue, td_lockq); 1842c100766SJulian Elischer if (td1->td_priority <= pri) { 18536412d79SJohn Baldwin continue; 18636412d79SJohn Baldwin } 18736412d79SJohn Baldwin 18836412d79SJohn Baldwin /* 189b40ce416SJulian Elischer * Remove thread from blocked chain and determine where 190b40ce416SJulian Elischer * it should be moved up to. Since we know that td1 has 191b40ce416SJulian Elischer * a lower priority than td, we know that at least one 192b40ce416SJulian Elischer * thread in the chain has a lower priority and that 193b40ce416SJulian Elischer * td1 will thus not be NULL after the loop. 19436412d79SJohn Baldwin */ 195551cf4e1SJohn Baldwin TAILQ_REMOVE(&m->mtx_blocked, td, td_lockq); 196551cf4e1SJohn Baldwin TAILQ_FOREACH(td1, &m->mtx_blocked, td_lockq) { 197b40ce416SJulian Elischer MPASS(td1->td_proc->p_magic == P_MAGIC); 1982c100766SJulian Elischer if (td1->td_priority > pri) 19936412d79SJohn Baldwin break; 20036412d79SJohn Baldwin } 2019ed346baSBosko Milekic 202b40ce416SJulian Elischer MPASS(td1 != NULL); 203551cf4e1SJohn Baldwin TAILQ_INSERT_BEFORE(td1, td, td_lockq); 20436412d79SJohn Baldwin CTR4(KTR_LOCK, 2058484de75SJohn Baldwin "propagate_priority: p %p moved before %p on [%p] %s", 206b40ce416SJulian Elischer td, td1, m, m->mtx_object.lo_name); 20736412d79SJohn Baldwin } 20836412d79SJohn Baldwin } 20936412d79SJohn Baldwin 2106c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 2116c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging"); 2126c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling"); 2136c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0; 2146c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW, 2156c35e809SDag-Erling Smørgrav &mutex_prof_enable, 0, "Enable tracing of mutex holdtime"); 2166c35e809SDag-Erling Smørgrav 2176c35e809SDag-Erling Smørgrav struct mutex_prof { 2186c35e809SDag-Erling Smørgrav const char *name; 2196c35e809SDag-Erling Smørgrav const char *file; 2206c35e809SDag-Erling Smørgrav int line; 2216c35e809SDag-Erling Smørgrav #define MPROF_MAX 0 2226c35e809SDag-Erling Smørgrav #define MPROF_TOT 1 2236c35e809SDag-Erling Smørgrav #define MPROF_CNT 2 2246c35e809SDag-Erling Smørgrav #define MPROF_AVG 3 225db586c8bSDag-Erling Smørgrav uintmax_t counter[4]; 226e6330704SDag-Erling Smørgrav struct mutex_prof *next; 2276c35e809SDag-Erling Smørgrav }; 2286c35e809SDag-Erling Smørgrav 2296c35e809SDag-Erling Smørgrav /* 2306c35e809SDag-Erling Smørgrav * mprof_buf is a static pool of profiling records to avoid possible 2316c35e809SDag-Erling Smørgrav * reentrance of the memory allocation functions. 2326c35e809SDag-Erling Smørgrav * 2336c35e809SDag-Erling Smørgrav * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE. 2346c35e809SDag-Erling Smørgrav */ 235e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000 2366c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS]; 2376c35e809SDag-Erling Smørgrav static int first_free_mprof_buf; 238e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009 2396c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE]; 2406c35e809SDag-Erling Smørgrav 2416c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions; 2426c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD, 2436c35e809SDag-Erling Smørgrav &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded"); 2446c35e809SDag-Erling Smørgrav static int mutex_prof_records; 2456c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD, 2466c35e809SDag-Erling Smørgrav &mutex_prof_records, 0, "Number of profiling records"); 2476c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS; 2486c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD, 2496c35e809SDag-Erling Smørgrav &mutex_prof_maxrecords, 0, "Maximum number of profiling records"); 2506c35e809SDag-Erling Smørgrav static int mutex_prof_rejected; 2516c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD, 2526c35e809SDag-Erling Smørgrav &mutex_prof_rejected, 0, "Number of rejected profiling records"); 2536c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE; 2546c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD, 2556c35e809SDag-Erling Smørgrav &mutex_prof_hashsize, 0, "Hash size"); 2566c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0; 2576c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD, 2586c35e809SDag-Erling Smørgrav &mutex_prof_collisions, 0, "Number of hash collisions"); 2596c35e809SDag-Erling Smørgrav 2606c35e809SDag-Erling Smørgrav /* 2616c35e809SDag-Erling Smørgrav * mprof_mtx protects the profiling buffers and the hash. 2626c35e809SDag-Erling Smørgrav */ 2636c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx; 264e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET); 2656c35e809SDag-Erling Smørgrav 266b784ffe9SDag-Erling Smørgrav static u_int64_t 267b784ffe9SDag-Erling Smørgrav nanoseconds(void) 268b784ffe9SDag-Erling Smørgrav { 269b784ffe9SDag-Erling Smørgrav struct timespec tv; 270b784ffe9SDag-Erling Smørgrav 271b784ffe9SDag-Erling Smørgrav nanotime(&tv); 272b784ffe9SDag-Erling Smørgrav return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec); 273b784ffe9SDag-Erling Smørgrav } 274b784ffe9SDag-Erling Smørgrav 2756c35e809SDag-Erling Smørgrav static int 2766c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 2776c35e809SDag-Erling Smørgrav { 2786c35e809SDag-Erling Smørgrav struct sbuf *sb; 2796c35e809SDag-Erling Smørgrav int error, i; 2806c35e809SDag-Erling Smørgrav 2816c35e809SDag-Erling Smørgrav if (first_free_mprof_buf == 0) 2826c35e809SDag-Erling Smørgrav return SYSCTL_OUT(req, "No locking recorded", 2836c35e809SDag-Erling Smørgrav sizeof("No locking recorded")); 2846c35e809SDag-Erling Smørgrav 2856c35e809SDag-Erling Smørgrav sb = sbuf_new(NULL, NULL, 1024, SBUF_AUTOEXTEND); 2866c35e809SDag-Erling Smørgrav sbuf_printf(sb, "%12s %12s %12s %12s %s\n", 2876c35e809SDag-Erling Smørgrav "max", "total", "count", "average", "name"); 2886c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 2896c35e809SDag-Erling Smørgrav for (i = 0; i < first_free_mprof_buf; ++i) 290db586c8bSDag-Erling Smørgrav sbuf_printf(sb, "%12ju %12ju %12ju %12ju %s:%d (%s)\n", 291b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_MAX] / 1000, 292b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_TOT] / 1000, 293b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_CNT], 294b784ffe9SDag-Erling Smørgrav mprof_buf[i].counter[MPROF_AVG] / 1000, 2956c35e809SDag-Erling Smørgrav mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 2966c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 2976c35e809SDag-Erling Smørgrav sbuf_finish(sb); 2986c35e809SDag-Erling Smørgrav error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2996c35e809SDag-Erling Smørgrav sbuf_delete(sb); 3006c35e809SDag-Erling Smørgrav return (error); 3016c35e809SDag-Erling Smørgrav } 3026c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING|CTLFLAG_RD, 3036c35e809SDag-Erling Smørgrav NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 3046c35e809SDag-Erling Smørgrav #endif 3056c35e809SDag-Erling Smørgrav 3060cde2e34SJason Evans /* 3076283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 3086283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 3096283b7d0SJohn Baldwin */ 3106283b7d0SJohn Baldwin void 3116283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 3126283b7d0SJohn Baldwin { 3136283b7d0SJohn Baldwin 314dde96c99SJohn Baldwin MPASS(curthread != NULL); 3150d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 3160d975d63SJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 3170d975d63SJohn Baldwin file, line)); 318dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 319dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 320dde96c99SJohn Baldwin line); 321dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3226c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 3236c35e809SDag-Erling Smørgrav /* don't reset the timer when/if recursing */ 324b61860adSDag-Erling Smørgrav if (m->mtx_acqtime == 0) { 325b61860adSDag-Erling Smørgrav m->mtx_filename = file; 326b61860adSDag-Erling Smørgrav m->mtx_lineno = line; 327b61860adSDag-Erling Smørgrav m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0; 3286c35e809SDag-Erling Smørgrav ++mutex_prof_acquisitions; 3296c35e809SDag-Erling Smørgrav } 3306c35e809SDag-Erling Smørgrav #endif 3316283b7d0SJohn Baldwin } 3326283b7d0SJohn Baldwin 3336283b7d0SJohn Baldwin void 3346283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 3356283b7d0SJohn Baldwin { 3366283b7d0SJohn Baldwin 337dde96c99SJohn Baldwin MPASS(curthread != NULL); 3380d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 3390d975d63SJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 3400d975d63SJohn Baldwin file, line)); 3410d975d63SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3420d975d63SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 3430d975d63SJohn Baldwin line); 34421377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 3456c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 346b61860adSDag-Erling Smørgrav if (m->mtx_acqtime != 0) { 3476c35e809SDag-Erling Smørgrav static const char *unknown = "(unknown)"; 3486c35e809SDag-Erling Smørgrav struct mutex_prof *mpp; 349b784ffe9SDag-Erling Smørgrav u_int64_t acqtime, now; 3506c35e809SDag-Erling Smørgrav const char *p, *q; 351e6330704SDag-Erling Smørgrav volatile u_int hash; 3526c35e809SDag-Erling Smørgrav 353b784ffe9SDag-Erling Smørgrav now = nanoseconds(); 354b61860adSDag-Erling Smørgrav acqtime = m->mtx_acqtime; 355b61860adSDag-Erling Smørgrav m->mtx_acqtime = 0; 356b784ffe9SDag-Erling Smørgrav if (now <= acqtime) 3576c35e809SDag-Erling Smørgrav goto out; 358b61860adSDag-Erling Smørgrav for (p = m->mtx_filename; strncmp(p, "../", 3) == 0; p += 3) 3596c35e809SDag-Erling Smørgrav /* nothing */ ; 3606c35e809SDag-Erling Smørgrav if (p == NULL || *p == '\0') 3616c35e809SDag-Erling Smørgrav p = unknown; 362b61860adSDag-Erling Smørgrav for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q) 3636c35e809SDag-Erling Smørgrav hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 3646c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 365e6330704SDag-Erling Smørgrav for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next) 366b61860adSDag-Erling Smørgrav if (mpp->line == m->mtx_lineno && 367b61860adSDag-Erling Smørgrav strcmp(mpp->file, p) == 0) 3686c35e809SDag-Erling Smørgrav break; 3696c35e809SDag-Erling Smørgrav if (mpp == NULL) { 3706c35e809SDag-Erling Smørgrav /* Just exit if we cannot get a trace buffer */ 3716c35e809SDag-Erling Smørgrav if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 3726c35e809SDag-Erling Smørgrav ++mutex_prof_rejected; 3736c35e809SDag-Erling Smørgrav goto unlock; 3746c35e809SDag-Erling Smørgrav } 3756c35e809SDag-Erling Smørgrav mpp = &mprof_buf[first_free_mprof_buf++]; 3766c35e809SDag-Erling Smørgrav mpp->name = mtx_name(m); 3776c35e809SDag-Erling Smørgrav mpp->file = p; 378b61860adSDag-Erling Smørgrav mpp->line = m->mtx_lineno; 379e6330704SDag-Erling Smørgrav mpp->next = mprof_hash[hash]; 380e6330704SDag-Erling Smørgrav if (mprof_hash[hash] != NULL) 381e6330704SDag-Erling Smørgrav ++mutex_prof_collisions; 3826c35e809SDag-Erling Smørgrav mprof_hash[hash] = mpp; 383e6330704SDag-Erling Smørgrav ++mutex_prof_records; 3846c35e809SDag-Erling Smørgrav } 3856c35e809SDag-Erling Smørgrav /* 3866c35e809SDag-Erling Smørgrav * Record if the mutex has been held longer now than ever 3876c35e809SDag-Erling Smørgrav * before 3886c35e809SDag-Erling Smørgrav */ 389b784ffe9SDag-Erling Smørgrav if ((now - acqtime) > mpp->counter[MPROF_MAX]) 390b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_MAX] = now - acqtime; 391b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_TOT] += now - acqtime; 392b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_CNT] += 1; 393b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_AVG] = 394b784ffe9SDag-Erling Smørgrav mpp->counter[MPROF_TOT] / mpp->counter[MPROF_CNT]; 3956c35e809SDag-Erling Smørgrav unlock: 3966c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 3976c35e809SDag-Erling Smørgrav } 3986c35e809SDag-Erling Smørgrav out: 3996c35e809SDag-Erling Smørgrav #endif 400dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 4016283b7d0SJohn Baldwin } 4026283b7d0SJohn Baldwin 4036283b7d0SJohn Baldwin void 4046283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 4056283b7d0SJohn Baldwin { 4066283b7d0SJohn Baldwin 407dde96c99SJohn Baldwin MPASS(curthread != NULL); 4080d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 4090d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 4100d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 411ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1 412dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 413e8fdcfb5SJohn Baldwin #else 414e8fdcfb5SJohn Baldwin critical_enter(); 415e8fdcfb5SJohn Baldwin #endif 416dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 417dde96c99SJohn Baldwin line); 418dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 4196283b7d0SJohn Baldwin } 4206283b7d0SJohn Baldwin 4216283b7d0SJohn Baldwin void 4226283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 4236283b7d0SJohn Baldwin { 4246283b7d0SJohn Baldwin 425dde96c99SJohn Baldwin MPASS(curthread != NULL); 4260d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 4270d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 4280d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 429dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 430dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 431dde96c99SJohn Baldwin line); 4320d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 433ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1 434dde96c99SJohn Baldwin _rel_spin_lock(m); 435e8fdcfb5SJohn Baldwin #else 436e8fdcfb5SJohn Baldwin critical_exit(); 437e8fdcfb5SJohn Baldwin #endif 4386283b7d0SJohn Baldwin } 4396283b7d0SJohn Baldwin 4406283b7d0SJohn Baldwin /* 4419ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 4429ed346baSBosko Milekic * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that 4439ed346baSBosko Milekic * if we're called, it's because we know we don't already own this lock. 4440cde2e34SJason Evans */ 4450cde2e34SJason Evans int 4469ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 4470cde2e34SJason Evans { 4480cde2e34SJason Evans int rval; 4490cde2e34SJason Evans 450b40ce416SJulian Elischer MPASS(curthread != NULL); 4519ed346baSBosko Milekic 452b40ce416SJulian Elischer rval = _obtain_lock(m, curthread); 4539ed346baSBosko Milekic 45419284646SJohn Baldwin LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 45519284646SJohn Baldwin if (rval) { 4569ed346baSBosko Milekic /* 4579ed346baSBosko Milekic * We do not handle recursion in _mtx_trylock; see the 4589ed346baSBosko Milekic * note at the top of the routine. 4599ed346baSBosko Milekic */ 4605746a1d8SBosko Milekic KASSERT(!mtx_recursed(m), 4615746a1d8SBosko Milekic ("mtx_trylock() called on a recursed mutex")); 4622d96f0b1SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4632d96f0b1SJohn Baldwin file, line); 4640cde2e34SJason Evans } 4659ed346baSBosko Milekic 46619284646SJohn Baldwin return (rval); 4670cde2e34SJason Evans } 4680cde2e34SJason Evans 4690cde2e34SJason Evans /* 4709ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4719ed346baSBosko Milekic * 4729ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4739ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4740cde2e34SJason Evans */ 4750cde2e34SJason Evans void 4769ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line) 47736412d79SJohn Baldwin { 478b40ce416SJulian Elischer struct thread *td = curthread; 4792498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES) 4802498cf8cSJohn Baldwin struct thread *owner; 4812498cf8cSJohn Baldwin #endif 48202bd1bcdSIan Dowse #ifdef KTR 48302bd1bcdSIan Dowse int cont_logged = 0; 48402bd1bcdSIan Dowse #endif 48536412d79SJohn Baldwin 486b40ce416SJulian Elischer if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)td) { 48736412d79SJohn Baldwin m->mtx_recurse++; 48808812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 48919284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4905746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 49136412d79SJohn Baldwin return; 49236412d79SJohn Baldwin } 4939ed346baSBosko Milekic 49419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 49515ec816aSJohn Baldwin CTR4(KTR_LOCK, 49615ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 49719284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 4981bd0eefbSJohn Baldwin 499b40ce416SJulian Elischer while (!_obtain_lock(m, td)) { 500f5271ebcSJohn Baldwin uintptr_t v; 501b40ce416SJulian Elischer struct thread *td1; 50236412d79SJohn Baldwin 5039ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 50436412d79SJohn Baldwin /* 5059ed346baSBosko Milekic * Check if the lock has been released while spinning for 5069ed346baSBosko Milekic * the sched_lock. 50736412d79SJohn Baldwin */ 50836412d79SJohn Baldwin if ((v = m->mtx_lock) == MTX_UNOWNED) { 5099ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 510703fc290SJohn Baldwin #ifdef __i386__ 5116b8c6989SJohn Baldwin ia32_pause(); 512703fc290SJohn Baldwin #endif 51336412d79SJohn Baldwin continue; 51436412d79SJohn Baldwin } 5159ed346baSBosko Milekic 51636412d79SJohn Baldwin /* 5179ed346baSBosko Milekic * The mutex was marked contested on release. This means that 518b40ce416SJulian Elischer * there are threads blocked on it. 51936412d79SJohn Baldwin */ 52036412d79SJohn Baldwin if (v == MTX_CONTESTED) { 521b40ce416SJulian Elischer td1 = TAILQ_FIRST(&m->mtx_blocked); 522b40ce416SJulian Elischer MPASS(td1 != NULL); 523b40ce416SJulian Elischer m->mtx_lock = (uintptr_t)td | MTX_CONTESTED; 5249ed346baSBosko Milekic 5252c100766SJulian Elischer if (td1->td_priority < td->td_priority) 5262c100766SJulian Elischer td->td_priority = td1->td_priority; 5279ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 52836412d79SJohn Baldwin return; 52936412d79SJohn Baldwin } 5309ed346baSBosko Milekic 53136412d79SJohn Baldwin /* 5329ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 5339ed346baSBosko Milekic * setting the contested bit, the mutex was either released 5349ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 53536412d79SJohn Baldwin */ 53636412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 53736412d79SJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, (void *)v, 53836412d79SJohn Baldwin (void *)(v | MTX_CONTESTED))) { 5399ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 540703fc290SJohn Baldwin #ifdef __i386__ 5416b8c6989SJohn Baldwin ia32_pause(); 542703fc290SJohn Baldwin #endif 54336412d79SJohn Baldwin continue; 54436412d79SJohn Baldwin } 54536412d79SJohn Baldwin 5462498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES) 5472498cf8cSJohn Baldwin /* 5482498cf8cSJohn Baldwin * If the current owner of the lock is executing on another 5492498cf8cSJohn Baldwin * CPU, spin instead of blocking. 5502498cf8cSJohn Baldwin */ 5512498cf8cSJohn Baldwin owner = (struct thread *)(v & MTX_FLAGMASK); 5526a95e08fSJohn Baldwin if (m != &Giant && thread_running(owner)) { 5532498cf8cSJohn Baldwin mtx_unlock_spin(&sched_lock); 5546a95e08fSJohn Baldwin while (mtx_owner(m) == owner && thread_running(owner)) { 555703fc290SJohn Baldwin #ifdef __i386__ 5566b8c6989SJohn Baldwin ia32_pause(); 557703fc290SJohn Baldwin #endif 5587fcca609SJohn Baldwin } 5592498cf8cSJohn Baldwin continue; 5602498cf8cSJohn Baldwin } 5612498cf8cSJohn Baldwin #endif /* SMP && ADAPTIVE_MUTEXES */ 5622498cf8cSJohn Baldwin 5639ed346baSBosko Milekic /* 5647feefcd6SJohn Baldwin * We definitely must sleep for this lock. 5659ed346baSBosko Milekic */ 56636412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 56736412d79SJohn Baldwin 56836412d79SJohn Baldwin #ifdef notyet 56936412d79SJohn Baldwin /* 5709ed346baSBosko Milekic * If we're borrowing an interrupted thread's VM context, we 5719ed346baSBosko Milekic * must clean up before going to sleep. 57236412d79SJohn Baldwin */ 573b40ce416SJulian Elischer if (td->td_ithd != NULL) { 574b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 57536412d79SJohn Baldwin 57636412d79SJohn Baldwin if (it->it_interrupted) { 57719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 57836412d79SJohn Baldwin CTR2(KTR_LOCK, 57915ec816aSJohn Baldwin "_mtx_lock_sleep: %p interrupted %p", 58036412d79SJohn Baldwin it, it->it_interrupted); 58136412d79SJohn Baldwin intr_thd_fixup(it); 58236412d79SJohn Baldwin } 58336412d79SJohn Baldwin } 58436412d79SJohn Baldwin #endif 58536412d79SJohn Baldwin 5869ed346baSBosko Milekic /* 5879ed346baSBosko Milekic * Put us on the list of threads blocked on this mutex. 5889ed346baSBosko Milekic */ 58936412d79SJohn Baldwin if (TAILQ_EMPTY(&m->mtx_blocked)) { 59018fc2ba9SJohn Baldwin td1 = mtx_owner(m); 591b40ce416SJulian Elischer LIST_INSERT_HEAD(&td1->td_contested, m, mtx_contested); 592551cf4e1SJohn Baldwin TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_lockq); 59336412d79SJohn Baldwin } else { 594551cf4e1SJohn Baldwin TAILQ_FOREACH(td1, &m->mtx_blocked, td_lockq) 5952c100766SJulian Elischer if (td1->td_priority > td->td_priority) 59636412d79SJohn Baldwin break; 597b40ce416SJulian Elischer if (td1) 598551cf4e1SJohn Baldwin TAILQ_INSERT_BEFORE(td1, td, td_lockq); 59936412d79SJohn Baldwin else 600551cf4e1SJohn Baldwin TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_lockq); 60136412d79SJohn Baldwin } 60202bd1bcdSIan Dowse #ifdef KTR 60302bd1bcdSIan Dowse if (!cont_logged) { 60402bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 60502bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 60602bd1bcdSIan Dowse td, file, line, m->mtx_object.lo_name, 60702bd1bcdSIan Dowse WITNESS_FILE(&m->mtx_object), 60802bd1bcdSIan Dowse WITNESS_LINE(&m->mtx_object)); 60902bd1bcdSIan Dowse cont_logged = 1; 61002bd1bcdSIan Dowse } 61102bd1bcdSIan Dowse #endif 61236412d79SJohn Baldwin 6139ed346baSBosko Milekic /* 6149ed346baSBosko Milekic * Save who we're blocked on. 6159ed346baSBosko Milekic */ 616b40ce416SJulian Elischer td->td_blocked = m; 617551cf4e1SJohn Baldwin td->td_lockname = m->mtx_object.lo_name; 618551cf4e1SJohn Baldwin TD_SET_LOCK(td); 619b40ce416SJulian Elischer propagate_priority(td); 6209ed346baSBosko Milekic 62119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 622562e4ffeSJohn Baldwin CTR3(KTR_LOCK, 623b40ce416SJulian Elischer "_mtx_lock_sleep: p %p blocked on [%p] %s", td, m, 62419284646SJohn Baldwin m->mtx_object.lo_name); 6259ed346baSBosko Milekic 626b40ce416SJulian Elischer td->td_proc->p_stats->p_ru.ru_nvcsw++; 62720cdcc5bSJohn Baldwin mi_switch(); 6289ed346baSBosko Milekic 62919284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 63036412d79SJohn Baldwin CTR3(KTR_LOCK, 6319ed346baSBosko Milekic "_mtx_lock_sleep: p %p free from blocked on [%p] %s", 632b40ce416SJulian Elischer td, m, m->mtx_object.lo_name); 6339ed346baSBosko Milekic 6349ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 63536412d79SJohn Baldwin } 6369ed346baSBosko Milekic 63702bd1bcdSIan Dowse #ifdef KTR 63802bd1bcdSIan Dowse if (cont_logged) { 63902bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 64002bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 64102bd1bcdSIan Dowse m->mtx_object.lo_name, td, file, line); 64202bd1bcdSIan Dowse } 64302bd1bcdSIan Dowse #endif 64436412d79SJohn Baldwin return; 6459ed346baSBosko Milekic } 6469ed346baSBosko Milekic 6479ed346baSBosko Milekic /* 6489ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 6499ed346baSBosko Milekic * 6509ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 6519ed346baSBosko Milekic * is handled inline. 6529ed346baSBosko Milekic */ 6539ed346baSBosko Milekic void 6547e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line) 65536412d79SJohn Baldwin { 65636412d79SJohn Baldwin int i = 0; 65736412d79SJohn Baldwin 65819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6595746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 6609ed346baSBosko Milekic 66136412d79SJohn Baldwin for (;;) { 662b40ce416SJulian Elischer if (_obtain_lock(m, curthread)) 66336412d79SJohn Baldwin break; 6649ed346baSBosko Milekic 6657141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 6667e1f6dfeSJohn Baldwin critical_exit(); 66736412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 668703fc290SJohn Baldwin if (i++ < 10000000) { 669703fc290SJohn Baldwin #ifdef __i386__ 6706b8c6989SJohn Baldwin ia32_pause(); 671703fc290SJohn Baldwin #endif 67236412d79SJohn Baldwin continue; 673703fc290SJohn Baldwin } 6740e54ddadSJohn Baldwin if (i < 60000000) 67536412d79SJohn Baldwin DELAY(1); 67636412d79SJohn Baldwin #ifdef DDB 67736412d79SJohn Baldwin else if (!db_active) 67836412d79SJohn Baldwin #else 67936412d79SJohn Baldwin else 68036412d79SJohn Baldwin #endif 6819ed346baSBosko Milekic panic("spin lock %s held by %p for > 5 seconds", 68219284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock); 683703fc290SJohn Baldwin #ifdef __i386__ 6846b8c6989SJohn Baldwin ia32_pause(); 685703fc290SJohn Baldwin #endif 68636412d79SJohn Baldwin } 6877e1f6dfeSJohn Baldwin critical_enter(); 68836412d79SJohn Baldwin } 68936412d79SJohn Baldwin 69019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6919ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 6929ed346baSBosko Milekic 69336412d79SJohn Baldwin return; 69436412d79SJohn Baldwin } 69536412d79SJohn Baldwin 6969ed346baSBosko Milekic /* 6979ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 6989ed346baSBosko Milekic * 6999ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 7009ed346baSBosko Milekic * need to wake up a blocked thread). 7019ed346baSBosko Milekic */ 70236412d79SJohn Baldwin void 7039ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 70436412d79SJohn Baldwin { 705b40ce416SJulian Elischer struct thread *td, *td1; 70636412d79SJohn Baldwin struct mtx *m1; 70736412d79SJohn Baldwin int pri; 70836412d79SJohn Baldwin 709b40ce416SJulian Elischer td = curthread; 7109ed346baSBosko Milekic 71108812b39SBosko Milekic if (mtx_recursed(m)) { 71236412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 71308812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 71419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7159ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 71636412d79SJohn Baldwin return; 71736412d79SJohn Baldwin } 7189ed346baSBosko Milekic 7199ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 72019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7219ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 7229ed346baSBosko Milekic 723b40ce416SJulian Elischer td1 = TAILQ_FIRST(&m->mtx_blocked); 7242498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES) 7252498cf8cSJohn Baldwin if (td1 == NULL) { 7262498cf8cSJohn Baldwin _release_lock_quick(m); 7272498cf8cSJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7282498cf8cSJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m); 7292498cf8cSJohn Baldwin mtx_unlock_spin(&sched_lock); 7302498cf8cSJohn Baldwin return; 7312498cf8cSJohn Baldwin } 7322498cf8cSJohn Baldwin #endif 733b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 734b40ce416SJulian Elischer MPASS(td1->td_proc->p_magic == P_MAGIC); 7359ed346baSBosko Milekic 736551cf4e1SJohn Baldwin TAILQ_REMOVE(&m->mtx_blocked, td1, td_lockq); 7379ed346baSBosko Milekic 73836412d79SJohn Baldwin if (TAILQ_EMPTY(&m->mtx_blocked)) { 73936412d79SJohn Baldwin LIST_REMOVE(m, mtx_contested); 74036412d79SJohn Baldwin _release_lock_quick(m); 74119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7429ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 74336412d79SJohn Baldwin } else 7449ed346baSBosko Milekic atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED); 7459ed346baSBosko Milekic 746d5a08a60SJake Burkholder pri = PRI_MAX; 747b40ce416SJulian Elischer LIST_FOREACH(m1, &td->td_contested, mtx_contested) { 7482c100766SJulian Elischer int cp = TAILQ_FIRST(&m1->mtx_blocked)->td_priority; 74936412d79SJohn Baldwin if (cp < pri) 75036412d79SJohn Baldwin pri = cp; 75136412d79SJohn Baldwin } 7529ed346baSBosko Milekic 7532c100766SJulian Elischer if (pri > td->td_base_pri) 7542c100766SJulian Elischer pri = td->td_base_pri; 7552c100766SJulian Elischer td->td_priority = pri; 7569ed346baSBosko Milekic 75719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7589ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p", 759b40ce416SJulian Elischer m, td1); 7609ed346baSBosko Milekic 761b40ce416SJulian Elischer td1->td_blocked = NULL; 762551cf4e1SJohn Baldwin TD_CLR_LOCK(td1); 763e0817317SJulian Elischer if (!TD_CAN_RUN(td1)) { 764e0817317SJulian Elischer mtx_unlock_spin(&sched_lock); 765e0817317SJulian Elischer return; 766e0817317SJulian Elischer } 76727354830SJulian Elischer setrunqueue(td1); 7689ed346baSBosko Milekic 7692c100766SJulian Elischer if (td->td_critnest == 1 && td1->td_priority < pri) { 77036412d79SJohn Baldwin #ifdef notyet 771b40ce416SJulian Elischer if (td->td_ithd != NULL) { 772b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 77336412d79SJohn Baldwin 77436412d79SJohn Baldwin if (it->it_interrupted) { 77519284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 77636412d79SJohn Baldwin CTR2(KTR_LOCK, 77715ec816aSJohn Baldwin "_mtx_unlock_sleep: %p interrupted %p", 77836412d79SJohn Baldwin it, it->it_interrupted); 77936412d79SJohn Baldwin intr_thd_fixup(it); 78036412d79SJohn Baldwin } 78136412d79SJohn Baldwin } 78236412d79SJohn Baldwin #endif 78319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 784562e4ffeSJohn Baldwin CTR2(KTR_LOCK, 7859ed346baSBosko Milekic "_mtx_unlock_sleep: %p switching out lock=%p", m, 7869ed346baSBosko Milekic (void *)m->mtx_lock); 7879ed346baSBosko Milekic 788b40ce416SJulian Elischer td->td_proc->p_stats->p_ru.ru_nivcsw++; 78936412d79SJohn Baldwin mi_switch(); 79019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7919ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 79231271627SJohn Baldwin m, (void *)m->mtx_lock); 79336412d79SJohn Baldwin } 79436412d79SJohn Baldwin 7959ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 7969ed346baSBosko Milekic 7979ed346baSBosko Milekic return; 7989ed346baSBosko Milekic } 7999ed346baSBosko Milekic 8009ed346baSBosko Milekic /* 8019ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 8029ed346baSBosko Milekic * See the _rel_spin_lock() macro for the details. 8039ed346baSBosko Milekic */ 8049ed346baSBosko Milekic 8059ed346baSBosko Milekic /* 80615ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 8079ed346baSBosko Milekic */ 8081103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 8090cde2e34SJason Evans void 81056771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line) 8110cde2e34SJason Evans { 8125cb0fbe4SJohn Baldwin 8135cb0fbe4SJohn Baldwin if (panicstr != NULL) 8145cb0fbe4SJohn Baldwin return; 815a10f4966SJake Burkholder switch (what) { 8160cde2e34SJason Evans case MA_OWNED: 8170cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 8180cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 819a10f4966SJake Burkholder if (!mtx_owned(m)) 8200cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 82119284646SJohn Baldwin m->mtx_object.lo_name, file, line); 822a10f4966SJake Burkholder if (mtx_recursed(m)) { 823a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 8240cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 82519284646SJohn Baldwin m->mtx_object.lo_name, file, line); 826a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 8270cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 82819284646SJohn Baldwin m->mtx_object.lo_name, file, line); 8290cde2e34SJason Evans } 8300cde2e34SJason Evans break; 8310cde2e34SJason Evans case MA_NOTOWNED: 832a10f4966SJake Burkholder if (mtx_owned(m)) 8330cde2e34SJason Evans panic("mutex %s owned at %s:%d", 83419284646SJohn Baldwin m->mtx_object.lo_name, file, line); 8350cde2e34SJason Evans break; 8360cde2e34SJason Evans default: 83756771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 8380cde2e34SJason Evans } 8390cde2e34SJason Evans } 8400cde2e34SJason Evans #endif 8410cde2e34SJason Evans 8429ed346baSBosko Milekic /* 8439ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 84419284646SJohn Baldwin * 84519284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 84619284646SJohn Baldwin * maintained by the witness code. 8479ed346baSBosko Milekic */ 84836412d79SJohn Baldwin #ifdef MUTEX_DEBUG 84936412d79SJohn Baldwin 8504d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 85136412d79SJohn Baldwin 85219284646SJohn Baldwin void 85319284646SJohn Baldwin mtx_validate(struct mtx *m) 85436412d79SJohn Baldwin { 85536412d79SJohn Baldwin 85636412d79SJohn Baldwin /* 85736412d79SJohn Baldwin * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly 85836412d79SJohn Baldwin * we can re-enable the kernacc() checks. 85936412d79SJohn Baldwin */ 86036412d79SJohn Baldwin #ifndef __alpha__ 86176dcbd6fSBosko Milekic /* 86276dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 86376dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 86476dcbd6fSBosko Milekic * requires Giant itself. 86576dcbd6fSBosko Milekic */ 866ab07087eSBosko Milekic if (!cold) 867ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 868ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 86919284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 87036412d79SJohn Baldwin #endif 87136412d79SJohn Baldwin } 87236412d79SJohn Baldwin #endif 87336412d79SJohn Baldwin 8749ed346baSBosko Milekic /* 875c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 876c27b5699SAndrew R. Reiter */ 877c27b5699SAndrew R. Reiter void 878c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 879c27b5699SAndrew R. Reiter { 880c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 881c27b5699SAndrew R. Reiter 8820c88508aSJohn Baldwin mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 883c27b5699SAndrew R. Reiter } 884c27b5699SAndrew R. Reiter 885c27b5699SAndrew R. Reiter /* 8869ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 8870c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 8880c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 8890c88508aSJohn Baldwin * witness. 8909ed346baSBosko Milekic */ 89136412d79SJohn Baldwin void 8920c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts) 89336412d79SJohn Baldwin { 89419284646SJohn Baldwin struct lock_object *lock; 8959ed346baSBosko Milekic 89619284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 897f22a4b62SJeff Roberson MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0); 8989ed346baSBosko Milekic 89936412d79SJohn Baldwin #ifdef MUTEX_DEBUG 9009ed346baSBosko Milekic /* Diagnostic and error correction */ 90119284646SJohn Baldwin mtx_validate(m); 9026936206eSJohn Baldwin #endif 90336412d79SJohn Baldwin 90419284646SJohn Baldwin lock = &m->mtx_object; 9057ada5876SJohn Baldwin KASSERT((lock->lo_flags & LO_INITIALIZED) == 0, 9060c88508aSJohn Baldwin ("mutex %s %p already initialized", name, m)); 9077ada5876SJohn Baldwin bzero(m, sizeof(*m)); 90819284646SJohn Baldwin if (opts & MTX_SPIN) 90919284646SJohn Baldwin lock->lo_class = &lock_class_mtx_spin; 91019284646SJohn Baldwin else 91119284646SJohn Baldwin lock->lo_class = &lock_class_mtx_sleep; 9120c88508aSJohn Baldwin lock->lo_name = name; 9130c88508aSJohn Baldwin lock->lo_type = type != NULL ? type : name; 91419284646SJohn Baldwin if (opts & MTX_QUIET) 91519284646SJohn Baldwin lock->lo_flags = LO_QUIET; 91619284646SJohn Baldwin if (opts & MTX_RECURSE) 91719284646SJohn Baldwin lock->lo_flags |= LO_RECURSABLE; 91819284646SJohn Baldwin if (opts & MTX_SLEEPABLE) 91919284646SJohn Baldwin lock->lo_flags |= LO_SLEEPABLE; 92019284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 92119284646SJohn Baldwin lock->lo_flags |= LO_WITNESS; 922f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 923f22a4b62SJeff Roberson lock->lo_flags |= LO_DUPOK; 92419284646SJohn Baldwin 92519284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 92636412d79SJohn Baldwin TAILQ_INIT(&m->mtx_blocked); 9279ed346baSBosko Milekic 92819284646SJohn Baldwin LOCK_LOG_INIT(lock, opts); 929d1c1b841SJason Evans 93019284646SJohn Baldwin WITNESS_INIT(lock); 93136412d79SJohn Baldwin } 93236412d79SJohn Baldwin 9339ed346baSBosko Milekic /* 93419284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 93519284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 93619284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 93719284646SJohn Baldwin * flags. 9389ed346baSBosko Milekic */ 93936412d79SJohn Baldwin void 94036412d79SJohn Baldwin mtx_destroy(struct mtx *m) 94136412d79SJohn Baldwin { 94236412d79SJohn Baldwin 94319284646SJohn Baldwin LOCK_LOG_DESTROY(&m->mtx_object, 0); 9449ed346baSBosko Milekic 94519284646SJohn Baldwin if (!mtx_owned(m)) 94619284646SJohn Baldwin MPASS(mtx_unowned(m)); 94719284646SJohn Baldwin else { 94808812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 9499ed346baSBosko Milekic 95019284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 951c86b6ff5SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 952c86b6ff5SJohn Baldwin __LINE__); 95336412d79SJohn Baldwin } 9540384fff8SJason Evans 95519284646SJohn Baldwin WITNESS_DESTROY(&m->mtx_object); 9560384fff8SJason Evans } 957d23f5958SMatthew Dillon 958d23f5958SMatthew Dillon /* 959c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 960c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 961c53c013bSJohn Baldwin * setup before this is called. 962c53c013bSJohn Baldwin */ 963c53c013bSJohn Baldwin void 964c53c013bSJohn Baldwin mutex_init(void) 965c53c013bSJohn Baldwin { 966c53c013bSJohn Baldwin 967c53c013bSJohn Baldwin /* Setup thread0 so that mutexes work. */ 968c53c013bSJohn Baldwin LIST_INIT(&thread0.td_contested); 969c53c013bSJohn Baldwin 970c53c013bSJohn Baldwin /* 971c53c013bSJohn Baldwin * Initialize mutexes. 972c53c013bSJohn Baldwin */ 9730c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 9740c88508aSJohn Baldwin mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE); 9750c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 976c53c013bSJohn Baldwin mtx_lock(&Giant); 977c53c013bSJohn Baldwin } 978c53c013bSJohn Baldwin 979c53c013bSJohn Baldwin /* 980d23f5958SMatthew Dillon * Encapsulated Giant mutex routines. These routines provide encapsulation 981d23f5958SMatthew Dillon * control for the Giant mutex, allowing sysctls to be used to turn on and 982d23f5958SMatthew Dillon * off Giant around certain subsystems. The default value for the sysctls 983d23f5958SMatthew Dillon * are set to what developers believe is stable and working in regards to 984d23f5958SMatthew Dillon * the Giant pushdown. Developers should not turn off Giant via these 985d23f5958SMatthew Dillon * sysctls unless they know what they are doing. 986d23f5958SMatthew Dillon * 987d23f5958SMatthew Dillon * Callers of mtx_lock_giant() are expected to pass the return value to an 988d23f5958SMatthew Dillon * accompanying mtx_unlock_giant() later on. If multiple subsystems are 989d23f5958SMatthew Dillon * effected by a Giant wrap, all related sysctl variables must be zero for 990d23f5958SMatthew Dillon * the subsystem call to operate without Giant (as determined by the caller). 991d23f5958SMatthew Dillon */ 992d23f5958SMatthew Dillon 993d23f5958SMatthew Dillon SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation"); 994d23f5958SMatthew Dillon 995d23f5958SMatthew Dillon static int kern_giant_all = 0; 996d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, ""); 997d23f5958SMatthew Dillon 998d23f5958SMatthew Dillon int kern_giant_proc = 1; /* Giant around PROC locks */ 999d23f5958SMatthew Dillon int kern_giant_file = 1; /* Giant around struct file & filedesc */ 1000735da6deSMatthew Dillon int kern_giant_ucred = 1; /* Giant around ucred */ 1001d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, ""); 1002d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, ""); 1003735da6deSMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, ""); 1004d23f5958SMatthew Dillon 1005d23f5958SMatthew Dillon int 1006d23f5958SMatthew Dillon mtx_lock_giant(int sysctlvar) 1007d23f5958SMatthew Dillon { 1008d23f5958SMatthew Dillon if (sysctlvar || kern_giant_all) { 1009d23f5958SMatthew Dillon mtx_lock(&Giant); 1010d23f5958SMatthew Dillon return(1); 1011d23f5958SMatthew Dillon } 1012d23f5958SMatthew Dillon return(0); 1013d23f5958SMatthew Dillon } 1014d23f5958SMatthew Dillon 1015d23f5958SMatthew Dillon void 1016d23f5958SMatthew Dillon mtx_unlock_giant(int s) 1017d23f5958SMatthew Dillon { 1018d23f5958SMatthew Dillon if (s) 1019d23f5958SMatthew Dillon mtx_unlock(&Giant); 1020d23f5958SMatthew Dillon } 1021d23f5958SMatthew Dillon 1022