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; 221ecf031c9SDag-Erling Smørgrav uintmax_t cnt_max; 222ecf031c9SDag-Erling Smørgrav uintmax_t cnt_tot; 223ecf031c9SDag-Erling Smørgrav uintmax_t cnt_cur; 224e6330704SDag-Erling Smørgrav struct mutex_prof *next; 2256c35e809SDag-Erling Smørgrav }; 2266c35e809SDag-Erling Smørgrav 2276c35e809SDag-Erling Smørgrav /* 2286c35e809SDag-Erling Smørgrav * mprof_buf is a static pool of profiling records to avoid possible 2296c35e809SDag-Erling Smørgrav * reentrance of the memory allocation functions. 2306c35e809SDag-Erling Smørgrav * 2316c35e809SDag-Erling Smørgrav * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE. 2326c35e809SDag-Erling Smørgrav */ 233e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000 2346c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS]; 2356c35e809SDag-Erling Smørgrav static int first_free_mprof_buf; 236e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009 2376c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE]; 2380bd5f797SMike Makonnen /* SWAG: sbuf size = avg stat. line size * number of locks */ 2390bd5f797SMike Makonnen #define MPROF_SBUF_SIZE 256 * 400 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; 2800bd5f797SMike Makonnen static int multiplier = 1; 2816c35e809SDag-Erling Smørgrav 2826c35e809SDag-Erling Smørgrav if (first_free_mprof_buf == 0) 2836d036900SDag-Erling Smørgrav return (SYSCTL_OUT(req, "No locking recorded", 2846d036900SDag-Erling Smørgrav sizeof("No locking recorded"))); 2856c35e809SDag-Erling Smørgrav 2860bd5f797SMike Makonnen retry_sbufops: 2870bd5f797SMike Makonnen sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN); 2886d036900SDag-Erling Smørgrav sbuf_printf(sb, "%6s %12s %11s %5s %s\n", 2896d036900SDag-Erling Smørgrav "max", "total", "count", "avg", "name"); 2906d036900SDag-Erling Smørgrav /* 2916d036900SDag-Erling Smørgrav * XXX this spinlock seems to be by far the largest perpetrator 2926d036900SDag-Erling Smørgrav * of spinlock latency (1.6 msec on an Athlon1600 was recorded 2936d036900SDag-Erling Smørgrav * even before I pessimized it further by moving the average 2946d036900SDag-Erling Smørgrav * computation here). 2956d036900SDag-Erling Smørgrav */ 2966c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 2970bd5f797SMike Makonnen for (i = 0; i < first_free_mprof_buf; ++i) { 2986d036900SDag-Erling Smørgrav sbuf_printf(sb, "%6ju %12ju %11ju %5ju %s:%d (%s)\n", 299ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_max / 1000, 300ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / 1000, 301ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur, 302ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 : 303ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000), 3046c35e809SDag-Erling Smørgrav mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 3050bd5f797SMike Makonnen if (sbuf_overflowed(sb)) { 3060bd5f797SMike Makonnen mtx_unlock_spin(&mprof_mtx); 3070bd5f797SMike Makonnen sbuf_delete(sb); 3080bd5f797SMike Makonnen multiplier++; 3090bd5f797SMike Makonnen goto retry_sbufops; 3100bd5f797SMike Makonnen } 3110bd5f797SMike Makonnen } 3126c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 3136c35e809SDag-Erling Smørgrav sbuf_finish(sb); 3146c35e809SDag-Erling Smørgrav error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 3156c35e809SDag-Erling Smørgrav sbuf_delete(sb); 3166c35e809SDag-Erling Smørgrav return (error); 3176c35e809SDag-Erling Smørgrav } 3186c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD, 3196c35e809SDag-Erling Smørgrav NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 3206c35e809SDag-Erling Smørgrav #endif 3216c35e809SDag-Erling Smørgrav 3220cde2e34SJason Evans /* 3236283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 3246283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 3256283b7d0SJohn Baldwin */ 3266283b7d0SJohn Baldwin void 3276283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 3286283b7d0SJohn Baldwin { 3296283b7d0SJohn Baldwin 330dde96c99SJohn Baldwin MPASS(curthread != NULL); 3310d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 3320d975d63SJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 3330d975d63SJohn Baldwin file, line)); 334dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 335dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 336dde96c99SJohn Baldwin line); 337dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3386c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 3396c35e809SDag-Erling Smørgrav /* don't reset the timer when/if recursing */ 340b61860adSDag-Erling Smørgrav if (m->mtx_acqtime == 0) { 341b61860adSDag-Erling Smørgrav m->mtx_filename = file; 342b61860adSDag-Erling Smørgrav m->mtx_lineno = line; 343b61860adSDag-Erling Smørgrav m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0; 3446c35e809SDag-Erling Smørgrav ++mutex_prof_acquisitions; 3456c35e809SDag-Erling Smørgrav } 3466c35e809SDag-Erling Smørgrav #endif 3476283b7d0SJohn Baldwin } 3486283b7d0SJohn Baldwin 3496283b7d0SJohn Baldwin void 3506283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 3516283b7d0SJohn Baldwin { 3526283b7d0SJohn Baldwin 353dde96c99SJohn Baldwin MPASS(curthread != NULL); 3540d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 3550d975d63SJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 3560d975d63SJohn Baldwin file, line)); 3570d975d63SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3580d975d63SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 3590d975d63SJohn Baldwin line); 36021377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 3616c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 362b61860adSDag-Erling Smørgrav if (m->mtx_acqtime != 0) { 3636c35e809SDag-Erling Smørgrav static const char *unknown = "(unknown)"; 3646c35e809SDag-Erling Smørgrav struct mutex_prof *mpp; 365b784ffe9SDag-Erling Smørgrav u_int64_t acqtime, now; 3666c35e809SDag-Erling Smørgrav const char *p, *q; 367e6330704SDag-Erling Smørgrav volatile u_int hash; 3686c35e809SDag-Erling Smørgrav 369b784ffe9SDag-Erling Smørgrav now = nanoseconds(); 370b61860adSDag-Erling Smørgrav acqtime = m->mtx_acqtime; 371b61860adSDag-Erling Smørgrav m->mtx_acqtime = 0; 372b784ffe9SDag-Erling Smørgrav if (now <= acqtime) 3736c35e809SDag-Erling Smørgrav goto out; 3740bd5f797SMike Makonnen for (p = m->mtx_filename; 3750bd5f797SMike Makonnen p != NULL && strncmp(p, "../", 3) == 0; p += 3) 3766c35e809SDag-Erling Smørgrav /* nothing */ ; 3776c35e809SDag-Erling Smørgrav if (p == NULL || *p == '\0') 3786c35e809SDag-Erling Smørgrav p = unknown; 379b61860adSDag-Erling Smørgrav for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q) 3806c35e809SDag-Erling Smørgrav hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 3816c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 382e6330704SDag-Erling Smørgrav for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next) 383b61860adSDag-Erling Smørgrav if (mpp->line == m->mtx_lineno && 384b61860adSDag-Erling Smørgrav strcmp(mpp->file, p) == 0) 3856c35e809SDag-Erling Smørgrav break; 3866c35e809SDag-Erling Smørgrav if (mpp == NULL) { 3876c35e809SDag-Erling Smørgrav /* Just exit if we cannot get a trace buffer */ 3886c35e809SDag-Erling Smørgrav if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 3896c35e809SDag-Erling Smørgrav ++mutex_prof_rejected; 3906c35e809SDag-Erling Smørgrav goto unlock; 3916c35e809SDag-Erling Smørgrav } 3926c35e809SDag-Erling Smørgrav mpp = &mprof_buf[first_free_mprof_buf++]; 3936c35e809SDag-Erling Smørgrav mpp->name = mtx_name(m); 3946c35e809SDag-Erling Smørgrav mpp->file = p; 395b61860adSDag-Erling Smørgrav mpp->line = m->mtx_lineno; 396e6330704SDag-Erling Smørgrav mpp->next = mprof_hash[hash]; 397e6330704SDag-Erling Smørgrav if (mprof_hash[hash] != NULL) 398e6330704SDag-Erling Smørgrav ++mutex_prof_collisions; 3996c35e809SDag-Erling Smørgrav mprof_hash[hash] = mpp; 400e6330704SDag-Erling Smørgrav ++mutex_prof_records; 4016c35e809SDag-Erling Smørgrav } 4026c35e809SDag-Erling Smørgrav /* 4036c35e809SDag-Erling Smørgrav * Record if the mutex has been held longer now than ever 4046d036900SDag-Erling Smørgrav * before. 4056c35e809SDag-Erling Smørgrav */ 406ecf031c9SDag-Erling Smørgrav if (now - acqtime > mpp->cnt_max) 407ecf031c9SDag-Erling Smørgrav mpp->cnt_max = now - acqtime; 408ecf031c9SDag-Erling Smørgrav mpp->cnt_tot += now - acqtime; 409ecf031c9SDag-Erling Smørgrav mpp->cnt_cur++; 4106c35e809SDag-Erling Smørgrav unlock: 4116c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 4126c35e809SDag-Erling Smørgrav } 4136c35e809SDag-Erling Smørgrav out: 4146c35e809SDag-Erling Smørgrav #endif 415dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 4166283b7d0SJohn Baldwin } 4176283b7d0SJohn Baldwin 4186283b7d0SJohn Baldwin void 4196283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 4206283b7d0SJohn Baldwin { 4216283b7d0SJohn Baldwin 422dde96c99SJohn Baldwin MPASS(curthread != NULL); 4230d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 4240d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 4250d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 426ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1 427dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 428e8fdcfb5SJohn Baldwin #else 429e8fdcfb5SJohn Baldwin critical_enter(); 430e8fdcfb5SJohn Baldwin #endif 431dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 432dde96c99SJohn Baldwin line); 433dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 4346283b7d0SJohn Baldwin } 4356283b7d0SJohn Baldwin 4366283b7d0SJohn Baldwin void 4376283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 4386283b7d0SJohn Baldwin { 4396283b7d0SJohn Baldwin 440dde96c99SJohn Baldwin MPASS(curthread != NULL); 4410d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 4420d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 4430d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 444dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 445dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 446dde96c99SJohn Baldwin line); 4470d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 448ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1 449dde96c99SJohn Baldwin _rel_spin_lock(m); 450e8fdcfb5SJohn Baldwin #else 451e8fdcfb5SJohn Baldwin critical_exit(); 452e8fdcfb5SJohn Baldwin #endif 4536283b7d0SJohn Baldwin } 4546283b7d0SJohn Baldwin 4556283b7d0SJohn Baldwin /* 4569ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 4579ed346baSBosko Milekic * Tries to acquire lock `m.' We do NOT handle recursion here; we assume that 4589ed346baSBosko Milekic * if we're called, it's because we know we don't already own this lock. 4590cde2e34SJason Evans */ 4600cde2e34SJason Evans int 4619ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 4620cde2e34SJason Evans { 4630cde2e34SJason Evans int rval; 4640cde2e34SJason Evans 465b40ce416SJulian Elischer MPASS(curthread != NULL); 4669ed346baSBosko Milekic 4676b869595SJohn Baldwin KASSERT(!mtx_owned(m), 4686b869595SJohn Baldwin ("mtx_trylock() called on a mutex already owned")); 4696b869595SJohn Baldwin 470b40ce416SJulian Elischer rval = _obtain_lock(m, curthread); 4719ed346baSBosko Milekic 47219284646SJohn Baldwin LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 4736b869595SJohn Baldwin if (rval) 4742d96f0b1SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4752d96f0b1SJohn Baldwin file, line); 4769ed346baSBosko Milekic 47719284646SJohn Baldwin return (rval); 4780cde2e34SJason Evans } 4790cde2e34SJason Evans 4800cde2e34SJason Evans /* 4819ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4829ed346baSBosko Milekic * 4839ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4849ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4850cde2e34SJason Evans */ 4860cde2e34SJason Evans void 4879ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line) 48836412d79SJohn Baldwin { 489b40ce416SJulian Elischer struct thread *td = curthread; 4905fa8dd90SJohn Baldwin struct thread *td1; 4912498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES) 4922498cf8cSJohn Baldwin struct thread *owner; 4932498cf8cSJohn Baldwin #endif 4945fa8dd90SJohn Baldwin uintptr_t v; 49502bd1bcdSIan Dowse #ifdef KTR 49602bd1bcdSIan Dowse int cont_logged = 0; 49702bd1bcdSIan Dowse #endif 49836412d79SJohn Baldwin 4995fa8dd90SJohn Baldwin if (mtx_owned(m)) { 50036412d79SJohn Baldwin m->mtx_recurse++; 50108812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 50219284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 5035746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 50436412d79SJohn Baldwin return; 50536412d79SJohn Baldwin } 5069ed346baSBosko Milekic 50719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 50815ec816aSJohn Baldwin CTR4(KTR_LOCK, 50915ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 51019284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 5111bd0eefbSJohn Baldwin 512b40ce416SJulian Elischer while (!_obtain_lock(m, td)) { 51336412d79SJohn Baldwin 5149ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 5155fa8dd90SJohn Baldwin v = m->mtx_lock; 5165fa8dd90SJohn Baldwin 51736412d79SJohn Baldwin /* 5189ed346baSBosko Milekic * Check if the lock has been released while spinning for 5199ed346baSBosko Milekic * the sched_lock. 52036412d79SJohn Baldwin */ 5215fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 5229ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 523703fc290SJohn Baldwin #ifdef __i386__ 5246b8c6989SJohn Baldwin ia32_pause(); 525703fc290SJohn Baldwin #endif 52636412d79SJohn Baldwin continue; 52736412d79SJohn Baldwin } 5289ed346baSBosko Milekic 52936412d79SJohn Baldwin /* 5309ed346baSBosko Milekic * The mutex was marked contested on release. This means that 531b40ce416SJulian Elischer * there are threads blocked on it. 53236412d79SJohn Baldwin */ 53336412d79SJohn Baldwin if (v == MTX_CONTESTED) { 534b40ce416SJulian Elischer td1 = TAILQ_FIRST(&m->mtx_blocked); 535b40ce416SJulian Elischer MPASS(td1 != NULL); 536b40ce416SJulian Elischer m->mtx_lock = (uintptr_t)td | MTX_CONTESTED; 5379ed346baSBosko Milekic 5382c100766SJulian Elischer if (td1->td_priority < td->td_priority) 5392c100766SJulian Elischer td->td_priority = td1->td_priority; 5409ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 54136412d79SJohn Baldwin return; 54236412d79SJohn Baldwin } 5439ed346baSBosko Milekic 54436412d79SJohn Baldwin /* 5459ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 5469ed346baSBosko Milekic * setting the contested bit, the mutex was either released 5479ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 54836412d79SJohn Baldwin */ 54936412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 55036412d79SJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, (void *)v, 55136412d79SJohn Baldwin (void *)(v | MTX_CONTESTED))) { 5529ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 553703fc290SJohn Baldwin #ifdef __i386__ 5546b8c6989SJohn Baldwin ia32_pause(); 555703fc290SJohn Baldwin #endif 55636412d79SJohn Baldwin continue; 55736412d79SJohn Baldwin } 55836412d79SJohn Baldwin 5592498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES) 5602498cf8cSJohn Baldwin /* 5612498cf8cSJohn Baldwin * If the current owner of the lock is executing on another 5622498cf8cSJohn Baldwin * CPU, spin instead of blocking. 5632498cf8cSJohn Baldwin */ 5642498cf8cSJohn Baldwin owner = (struct thread *)(v & MTX_FLAGMASK); 5656a95e08fSJohn Baldwin if (m != &Giant && thread_running(owner)) { 5662498cf8cSJohn Baldwin mtx_unlock_spin(&sched_lock); 5676a95e08fSJohn Baldwin while (mtx_owner(m) == owner && thread_running(owner)) { 568703fc290SJohn Baldwin #ifdef __i386__ 5696b8c6989SJohn Baldwin ia32_pause(); 570703fc290SJohn Baldwin #endif 5717fcca609SJohn Baldwin } 5722498cf8cSJohn Baldwin continue; 5732498cf8cSJohn Baldwin } 5742498cf8cSJohn Baldwin #endif /* SMP && ADAPTIVE_MUTEXES */ 5752498cf8cSJohn Baldwin 5769ed346baSBosko Milekic /* 5777feefcd6SJohn Baldwin * We definitely must sleep for this lock. 5789ed346baSBosko Milekic */ 57936412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 58036412d79SJohn Baldwin 58136412d79SJohn Baldwin #ifdef notyet 58236412d79SJohn Baldwin /* 5839ed346baSBosko Milekic * If we're borrowing an interrupted thread's VM context, we 5849ed346baSBosko Milekic * must clean up before going to sleep. 58536412d79SJohn Baldwin */ 586b40ce416SJulian Elischer if (td->td_ithd != NULL) { 587b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 58836412d79SJohn Baldwin 58936412d79SJohn Baldwin if (it->it_interrupted) { 59019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 59136412d79SJohn Baldwin CTR2(KTR_LOCK, 59215ec816aSJohn Baldwin "_mtx_lock_sleep: %p interrupted %p", 59336412d79SJohn Baldwin it, it->it_interrupted); 59436412d79SJohn Baldwin intr_thd_fixup(it); 59536412d79SJohn Baldwin } 59636412d79SJohn Baldwin } 59736412d79SJohn Baldwin #endif 59836412d79SJohn Baldwin 5999ed346baSBosko Milekic /* 6009ed346baSBosko Milekic * Put us on the list of threads blocked on this mutex. 6019ed346baSBosko Milekic */ 60236412d79SJohn Baldwin if (TAILQ_EMPTY(&m->mtx_blocked)) { 60318fc2ba9SJohn Baldwin td1 = mtx_owner(m); 604b40ce416SJulian Elischer LIST_INSERT_HEAD(&td1->td_contested, m, mtx_contested); 605551cf4e1SJohn Baldwin TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_lockq); 60636412d79SJohn Baldwin } else { 607551cf4e1SJohn Baldwin TAILQ_FOREACH(td1, &m->mtx_blocked, td_lockq) 6082c100766SJulian Elischer if (td1->td_priority > td->td_priority) 60936412d79SJohn Baldwin break; 610b40ce416SJulian Elischer if (td1) 611551cf4e1SJohn Baldwin TAILQ_INSERT_BEFORE(td1, td, td_lockq); 61236412d79SJohn Baldwin else 613551cf4e1SJohn Baldwin TAILQ_INSERT_TAIL(&m->mtx_blocked, td, td_lockq); 61436412d79SJohn Baldwin } 61502bd1bcdSIan Dowse #ifdef KTR 61602bd1bcdSIan Dowse if (!cont_logged) { 61702bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 61802bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 61902bd1bcdSIan Dowse td, file, line, m->mtx_object.lo_name, 62002bd1bcdSIan Dowse WITNESS_FILE(&m->mtx_object), 62102bd1bcdSIan Dowse WITNESS_LINE(&m->mtx_object)); 62202bd1bcdSIan Dowse cont_logged = 1; 62302bd1bcdSIan Dowse } 62402bd1bcdSIan Dowse #endif 62536412d79SJohn Baldwin 6269ed346baSBosko Milekic /* 6279ed346baSBosko Milekic * Save who we're blocked on. 6289ed346baSBosko Milekic */ 629b40ce416SJulian Elischer td->td_blocked = m; 630551cf4e1SJohn Baldwin td->td_lockname = m->mtx_object.lo_name; 631551cf4e1SJohn Baldwin TD_SET_LOCK(td); 632b40ce416SJulian Elischer propagate_priority(td); 6339ed346baSBosko Milekic 63419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 635562e4ffeSJohn Baldwin CTR3(KTR_LOCK, 636b40ce416SJulian Elischer "_mtx_lock_sleep: p %p blocked on [%p] %s", td, m, 63719284646SJohn Baldwin m->mtx_object.lo_name); 6389ed346baSBosko Milekic 639b40ce416SJulian Elischer td->td_proc->p_stats->p_ru.ru_nvcsw++; 64020cdcc5bSJohn Baldwin mi_switch(); 6419ed346baSBosko Milekic 64219284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 64336412d79SJohn Baldwin CTR3(KTR_LOCK, 6449ed346baSBosko Milekic "_mtx_lock_sleep: p %p free from blocked on [%p] %s", 645b40ce416SJulian Elischer td, m, m->mtx_object.lo_name); 6469ed346baSBosko Milekic 6479ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 64836412d79SJohn Baldwin } 6499ed346baSBosko Milekic 65002bd1bcdSIan Dowse #ifdef KTR 65102bd1bcdSIan Dowse if (cont_logged) { 65202bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 65302bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 65402bd1bcdSIan Dowse m->mtx_object.lo_name, td, file, line); 65502bd1bcdSIan Dowse } 65602bd1bcdSIan Dowse #endif 65736412d79SJohn Baldwin return; 6589ed346baSBosko Milekic } 6599ed346baSBosko Milekic 6609ed346baSBosko Milekic /* 6619ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 6629ed346baSBosko Milekic * 6639ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 6649ed346baSBosko Milekic * is handled inline. 6659ed346baSBosko Milekic */ 6669ed346baSBosko Milekic void 6677e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line) 66836412d79SJohn Baldwin { 66936412d79SJohn Baldwin int i = 0; 67036412d79SJohn Baldwin 67119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6725746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 6739ed346baSBosko Milekic 67436412d79SJohn Baldwin for (;;) { 675b40ce416SJulian Elischer if (_obtain_lock(m, curthread)) 67636412d79SJohn Baldwin break; 6779ed346baSBosko Milekic 6787141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 6797e1f6dfeSJohn Baldwin critical_exit(); 68036412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 681703fc290SJohn Baldwin if (i++ < 10000000) { 682703fc290SJohn Baldwin #ifdef __i386__ 6836b8c6989SJohn Baldwin ia32_pause(); 684703fc290SJohn Baldwin #endif 68536412d79SJohn Baldwin continue; 686703fc290SJohn Baldwin } 6870e54ddadSJohn Baldwin if (i < 60000000) 68836412d79SJohn Baldwin DELAY(1); 68936412d79SJohn Baldwin #ifdef DDB 69036412d79SJohn Baldwin else if (!db_active) 69136412d79SJohn Baldwin #else 69236412d79SJohn Baldwin else 69336412d79SJohn Baldwin #endif 6949ed346baSBosko Milekic panic("spin lock %s held by %p for > 5 seconds", 69519284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock); 696703fc290SJohn Baldwin #ifdef __i386__ 6976b8c6989SJohn Baldwin ia32_pause(); 698703fc290SJohn Baldwin #endif 69936412d79SJohn Baldwin } 7007e1f6dfeSJohn Baldwin critical_enter(); 70136412d79SJohn Baldwin } 70236412d79SJohn Baldwin 70319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7049ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 7059ed346baSBosko Milekic 70636412d79SJohn Baldwin return; 70736412d79SJohn Baldwin } 70836412d79SJohn Baldwin 7099ed346baSBosko Milekic /* 7109ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 7119ed346baSBosko Milekic * 7129ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 7139ed346baSBosko Milekic * need to wake up a blocked thread). 7149ed346baSBosko Milekic */ 71536412d79SJohn Baldwin void 7169ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 71736412d79SJohn Baldwin { 718b40ce416SJulian Elischer struct thread *td, *td1; 71936412d79SJohn Baldwin struct mtx *m1; 72036412d79SJohn Baldwin int pri; 72136412d79SJohn Baldwin 722b40ce416SJulian Elischer td = curthread; 7239ed346baSBosko Milekic 72408812b39SBosko Milekic if (mtx_recursed(m)) { 72536412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 72608812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 72719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7289ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 72936412d79SJohn Baldwin return; 73036412d79SJohn Baldwin } 7319ed346baSBosko Milekic 7329ed346baSBosko Milekic mtx_lock_spin(&sched_lock); 73319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7349ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 7359ed346baSBosko Milekic 736b40ce416SJulian Elischer td1 = TAILQ_FIRST(&m->mtx_blocked); 7372498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES) 7382498cf8cSJohn Baldwin if (td1 == NULL) { 7392498cf8cSJohn Baldwin _release_lock_quick(m); 7402498cf8cSJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7412498cf8cSJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m); 7422498cf8cSJohn Baldwin mtx_unlock_spin(&sched_lock); 7432498cf8cSJohn Baldwin return; 7442498cf8cSJohn Baldwin } 7452498cf8cSJohn Baldwin #endif 746b40ce416SJulian Elischer MPASS(td->td_proc->p_magic == P_MAGIC); 747b40ce416SJulian Elischer MPASS(td1->td_proc->p_magic == P_MAGIC); 7489ed346baSBosko Milekic 749551cf4e1SJohn Baldwin TAILQ_REMOVE(&m->mtx_blocked, td1, td_lockq); 7509ed346baSBosko Milekic 75136412d79SJohn Baldwin if (TAILQ_EMPTY(&m->mtx_blocked)) { 75236412d79SJohn Baldwin LIST_REMOVE(m, mtx_contested); 75336412d79SJohn Baldwin _release_lock_quick(m); 75419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7559ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 75636412d79SJohn Baldwin } else 7579ed346baSBosko Milekic atomic_store_rel_ptr(&m->mtx_lock, (void *)MTX_CONTESTED); 7589ed346baSBosko Milekic 759d5a08a60SJake Burkholder pri = PRI_MAX; 760b40ce416SJulian Elischer LIST_FOREACH(m1, &td->td_contested, mtx_contested) { 7612c100766SJulian Elischer int cp = TAILQ_FIRST(&m1->mtx_blocked)->td_priority; 76236412d79SJohn Baldwin if (cp < pri) 76336412d79SJohn Baldwin pri = cp; 76436412d79SJohn Baldwin } 7659ed346baSBosko Milekic 7662c100766SJulian Elischer if (pri > td->td_base_pri) 7672c100766SJulian Elischer pri = td->td_base_pri; 7682c100766SJulian Elischer td->td_priority = pri; 7699ed346baSBosko Milekic 77019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7719ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p contested setrunqueue %p", 772b40ce416SJulian Elischer m, td1); 7739ed346baSBosko Milekic 774b40ce416SJulian Elischer td1->td_blocked = NULL; 775551cf4e1SJohn Baldwin TD_CLR_LOCK(td1); 776e0817317SJulian Elischer if (!TD_CAN_RUN(td1)) { 777e0817317SJulian Elischer mtx_unlock_spin(&sched_lock); 778e0817317SJulian Elischer return; 779e0817317SJulian Elischer } 78027354830SJulian Elischer setrunqueue(td1); 7819ed346baSBosko Milekic 7822c100766SJulian Elischer if (td->td_critnest == 1 && td1->td_priority < pri) { 78336412d79SJohn Baldwin #ifdef notyet 784b40ce416SJulian Elischer if (td->td_ithd != NULL) { 785b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 78636412d79SJohn Baldwin 78736412d79SJohn Baldwin if (it->it_interrupted) { 78819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 78936412d79SJohn Baldwin CTR2(KTR_LOCK, 79015ec816aSJohn Baldwin "_mtx_unlock_sleep: %p interrupted %p", 79136412d79SJohn Baldwin it, it->it_interrupted); 79236412d79SJohn Baldwin intr_thd_fixup(it); 79336412d79SJohn Baldwin } 79436412d79SJohn Baldwin } 79536412d79SJohn Baldwin #endif 79619284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 797562e4ffeSJohn Baldwin CTR2(KTR_LOCK, 7989ed346baSBosko Milekic "_mtx_unlock_sleep: %p switching out lock=%p", m, 7999ed346baSBosko Milekic (void *)m->mtx_lock); 8009ed346baSBosko Milekic 801b40ce416SJulian Elischer td->td_proc->p_stats->p_ru.ru_nivcsw++; 80236412d79SJohn Baldwin mi_switch(); 80319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 8049ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 80531271627SJohn Baldwin m, (void *)m->mtx_lock); 80636412d79SJohn Baldwin } 80736412d79SJohn Baldwin 8089ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 8099ed346baSBosko Milekic 8109ed346baSBosko Milekic return; 8119ed346baSBosko Milekic } 8129ed346baSBosko Milekic 8139ed346baSBosko Milekic /* 8149ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 8159ed346baSBosko Milekic * See the _rel_spin_lock() macro for the details. 8169ed346baSBosko Milekic */ 8179ed346baSBosko Milekic 8189ed346baSBosko Milekic /* 81915ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 8209ed346baSBosko Milekic */ 8211103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 8220cde2e34SJason Evans void 82356771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line) 8240cde2e34SJason Evans { 8255cb0fbe4SJohn Baldwin 8265cb0fbe4SJohn Baldwin if (panicstr != NULL) 8275cb0fbe4SJohn Baldwin return; 828a10f4966SJake Burkholder switch (what) { 8290cde2e34SJason Evans case MA_OWNED: 8300cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 8310cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 832a10f4966SJake Burkholder if (!mtx_owned(m)) 8330cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 83419284646SJohn Baldwin m->mtx_object.lo_name, file, line); 835a10f4966SJake Burkholder if (mtx_recursed(m)) { 836a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 8370cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 83819284646SJohn Baldwin m->mtx_object.lo_name, file, line); 839a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 8400cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 84119284646SJohn Baldwin m->mtx_object.lo_name, file, line); 8420cde2e34SJason Evans } 8430cde2e34SJason Evans break; 8440cde2e34SJason Evans case MA_NOTOWNED: 845a10f4966SJake Burkholder if (mtx_owned(m)) 8460cde2e34SJason Evans panic("mutex %s owned at %s:%d", 84719284646SJohn Baldwin m->mtx_object.lo_name, file, line); 8480cde2e34SJason Evans break; 8490cde2e34SJason Evans default: 85056771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 8510cde2e34SJason Evans } 8520cde2e34SJason Evans } 8530cde2e34SJason Evans #endif 8540cde2e34SJason Evans 8559ed346baSBosko Milekic /* 8569ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 85719284646SJohn Baldwin * 85819284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 85919284646SJohn Baldwin * maintained by the witness code. 8609ed346baSBosko Milekic */ 86136412d79SJohn Baldwin #ifdef MUTEX_DEBUG 86236412d79SJohn Baldwin 8634d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 86436412d79SJohn Baldwin 86519284646SJohn Baldwin void 86619284646SJohn Baldwin mtx_validate(struct mtx *m) 86736412d79SJohn Baldwin { 86836412d79SJohn Baldwin 86936412d79SJohn Baldwin /* 870fa669ab7SPoul-Henning Kamp * XXX: When kernacc() does not require Giant we can reenable this check 871fa669ab7SPoul-Henning Kamp */ 872fa669ab7SPoul-Henning Kamp #ifdef notyet 873fa669ab7SPoul-Henning Kamp /* 87436412d79SJohn Baldwin * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly 87536412d79SJohn Baldwin * we can re-enable the kernacc() checks. 87636412d79SJohn Baldwin */ 87736412d79SJohn Baldwin #ifndef __alpha__ 87876dcbd6fSBosko Milekic /* 87976dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 88076dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 88176dcbd6fSBosko Milekic * requires Giant itself. 88276dcbd6fSBosko Milekic */ 883ab07087eSBosko Milekic if (!cold) 884ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 885ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 88619284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 88736412d79SJohn Baldwin #endif 888fa669ab7SPoul-Henning Kamp #endif 88936412d79SJohn Baldwin } 89036412d79SJohn Baldwin #endif 89136412d79SJohn Baldwin 8929ed346baSBosko Milekic /* 893c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 894c27b5699SAndrew R. Reiter */ 895c27b5699SAndrew R. Reiter void 896c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 897c27b5699SAndrew R. Reiter { 898c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 899c27b5699SAndrew R. Reiter 9000c88508aSJohn Baldwin mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 901c27b5699SAndrew R. Reiter } 902c27b5699SAndrew R. Reiter 903c27b5699SAndrew R. Reiter /* 9049ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 9050c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 9060c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 9070c88508aSJohn Baldwin * witness. 9089ed346baSBosko Milekic */ 90936412d79SJohn Baldwin void 9100c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts) 91136412d79SJohn Baldwin { 91219284646SJohn Baldwin struct lock_object *lock; 9139ed346baSBosko Milekic 91419284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 915f22a4b62SJeff Roberson MTX_SLEEPABLE | MTX_NOWITNESS | MTX_DUPOK)) == 0); 9169ed346baSBosko Milekic 91736412d79SJohn Baldwin #ifdef MUTEX_DEBUG 9189ed346baSBosko Milekic /* Diagnostic and error correction */ 91919284646SJohn Baldwin mtx_validate(m); 9206936206eSJohn Baldwin #endif 92136412d79SJohn Baldwin 92219284646SJohn Baldwin lock = &m->mtx_object; 9237ada5876SJohn Baldwin KASSERT((lock->lo_flags & LO_INITIALIZED) == 0, 9240c88508aSJohn Baldwin ("mutex %s %p already initialized", name, m)); 9257ada5876SJohn Baldwin bzero(m, sizeof(*m)); 92619284646SJohn Baldwin if (opts & MTX_SPIN) 92719284646SJohn Baldwin lock->lo_class = &lock_class_mtx_spin; 92819284646SJohn Baldwin else 92919284646SJohn Baldwin lock->lo_class = &lock_class_mtx_sleep; 9300c88508aSJohn Baldwin lock->lo_name = name; 9310c88508aSJohn Baldwin lock->lo_type = type != NULL ? type : name; 93219284646SJohn Baldwin if (opts & MTX_QUIET) 93319284646SJohn Baldwin lock->lo_flags = LO_QUIET; 93419284646SJohn Baldwin if (opts & MTX_RECURSE) 93519284646SJohn Baldwin lock->lo_flags |= LO_RECURSABLE; 93619284646SJohn Baldwin if (opts & MTX_SLEEPABLE) 93719284646SJohn Baldwin lock->lo_flags |= LO_SLEEPABLE; 93819284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 93919284646SJohn Baldwin lock->lo_flags |= LO_WITNESS; 940f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 941f22a4b62SJeff Roberson lock->lo_flags |= LO_DUPOK; 94219284646SJohn Baldwin 94319284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 94436412d79SJohn Baldwin TAILQ_INIT(&m->mtx_blocked); 9459ed346baSBosko Milekic 94619284646SJohn Baldwin LOCK_LOG_INIT(lock, opts); 947d1c1b841SJason Evans 94819284646SJohn Baldwin WITNESS_INIT(lock); 94936412d79SJohn Baldwin } 95036412d79SJohn Baldwin 9519ed346baSBosko Milekic /* 95219284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 95319284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 95419284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 95519284646SJohn Baldwin * flags. 9569ed346baSBosko Milekic */ 95736412d79SJohn Baldwin void 95836412d79SJohn Baldwin mtx_destroy(struct mtx *m) 95936412d79SJohn Baldwin { 96036412d79SJohn Baldwin 96119284646SJohn Baldwin LOCK_LOG_DESTROY(&m->mtx_object, 0); 9629ed346baSBosko Milekic 96319284646SJohn Baldwin if (!mtx_owned(m)) 96419284646SJohn Baldwin MPASS(mtx_unowned(m)); 96519284646SJohn Baldwin else { 96608812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 9679ed346baSBosko Milekic 96819284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 969c86b6ff5SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 970c86b6ff5SJohn Baldwin __LINE__); 97136412d79SJohn Baldwin } 9720384fff8SJason Evans 97319284646SJohn Baldwin WITNESS_DESTROY(&m->mtx_object); 9740384fff8SJason Evans } 975d23f5958SMatthew Dillon 976d23f5958SMatthew Dillon /* 977c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 978c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 979c53c013bSJohn Baldwin * setup before this is called. 980c53c013bSJohn Baldwin */ 981c53c013bSJohn Baldwin void 982c53c013bSJohn Baldwin mutex_init(void) 983c53c013bSJohn Baldwin { 984c53c013bSJohn Baldwin 985c53c013bSJohn Baldwin /* Setup thread0 so that mutexes work. */ 986c53c013bSJohn Baldwin LIST_INIT(&thread0.td_contested); 987c53c013bSJohn Baldwin 988c53c013bSJohn Baldwin /* 989c53c013bSJohn Baldwin * Initialize mutexes. 990c53c013bSJohn Baldwin */ 9910c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 9920c88508aSJohn Baldwin mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE); 9930c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 994c53c013bSJohn Baldwin mtx_lock(&Giant); 995c53c013bSJohn Baldwin } 996c53c013bSJohn Baldwin 997c53c013bSJohn Baldwin /* 998d23f5958SMatthew Dillon * Encapsulated Giant mutex routines. These routines provide encapsulation 999d23f5958SMatthew Dillon * control for the Giant mutex, allowing sysctls to be used to turn on and 1000d23f5958SMatthew Dillon * off Giant around certain subsystems. The default value for the sysctls 1001d23f5958SMatthew Dillon * are set to what developers believe is stable and working in regards to 1002d23f5958SMatthew Dillon * the Giant pushdown. Developers should not turn off Giant via these 1003d23f5958SMatthew Dillon * sysctls unless they know what they are doing. 1004d23f5958SMatthew Dillon * 1005d23f5958SMatthew Dillon * Callers of mtx_lock_giant() are expected to pass the return value to an 1006d23f5958SMatthew Dillon * accompanying mtx_unlock_giant() later on. If multiple subsystems are 1007d23f5958SMatthew Dillon * effected by a Giant wrap, all related sysctl variables must be zero for 1008d23f5958SMatthew Dillon * the subsystem call to operate without Giant (as determined by the caller). 1009d23f5958SMatthew Dillon */ 1010d23f5958SMatthew Dillon 1011d23f5958SMatthew Dillon SYSCTL_NODE(_kern, OID_AUTO, giant, CTLFLAG_RD, NULL, "Giant mutex manipulation"); 1012d23f5958SMatthew Dillon 1013d23f5958SMatthew Dillon static int kern_giant_all = 0; 1014d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, all, CTLFLAG_RW, &kern_giant_all, 0, ""); 1015d23f5958SMatthew Dillon 1016d23f5958SMatthew Dillon int kern_giant_proc = 1; /* Giant around PROC locks */ 1017d23f5958SMatthew Dillon int kern_giant_file = 1; /* Giant around struct file & filedesc */ 1018735da6deSMatthew Dillon int kern_giant_ucred = 1; /* Giant around ucred */ 1019d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, proc, CTLFLAG_RW, &kern_giant_proc, 0, ""); 1020d23f5958SMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, file, CTLFLAG_RW, &kern_giant_file, 0, ""); 1021735da6deSMatthew Dillon SYSCTL_INT(_kern_giant, OID_AUTO, ucred, CTLFLAG_RW, &kern_giant_ucred, 0, ""); 1022d23f5958SMatthew Dillon 1023d23f5958SMatthew Dillon int 1024d23f5958SMatthew Dillon mtx_lock_giant(int sysctlvar) 1025d23f5958SMatthew Dillon { 1026d23f5958SMatthew Dillon if (sysctlvar || kern_giant_all) { 1027d23f5958SMatthew Dillon mtx_lock(&Giant); 1028d23f5958SMatthew Dillon return(1); 1029d23f5958SMatthew Dillon } 1030d23f5958SMatthew Dillon return(0); 1031d23f5958SMatthew Dillon } 1032d23f5958SMatthew Dillon 1033d23f5958SMatthew Dillon void 1034d23f5958SMatthew Dillon mtx_unlock_giant(int s) 1035d23f5958SMatthew Dillon { 1036d23f5958SMatthew Dillon if (s) 1037d23f5958SMatthew Dillon mtx_unlock(&Giant); 1038d23f5958SMatthew Dillon } 1039