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 */ 310384fff8SJason Evans 320384fff8SJason Evans /* 33ba48b69aSJohn Baldwin * Machine independent bits of mutex implementation. 340384fff8SJason Evans */ 350384fff8SJason Evans 36677b542eSDavid E. O'Brien #include <sys/cdefs.h> 37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38677b542eSDavid E. O'Brien 392498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h" 409c36c934SJohn Baldwin #include "opt_ddb.h" 4100096801SJohn-Mark Gurney #include "opt_mprof.h" 42535eb309SJohn Baldwin #include "opt_mutex_wake_all.h" 439923b511SScott Long #include "opt_sched.h" 44a5a96a19SJohn Baldwin 450384fff8SJason Evans #include <sys/param.h> 466c35e809SDag-Erling Smørgrav #include <sys/systm.h> 4736412d79SJohn Baldwin #include <sys/bus.h> 481126349aSPaul Saab #include <sys/conf.h> 492d50560aSMarcel Moolenaar #include <sys/kdb.h> 5036412d79SJohn Baldwin #include <sys/kernel.h> 516c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 5219284646SJohn Baldwin #include <sys/lock.h> 53fb919e4dSMark Murray #include <sys/malloc.h> 5419284646SJohn Baldwin #include <sys/mutex.h> 550384fff8SJason Evans #include <sys/proc.h> 56c4f7a187SJohn Baldwin #include <sys/resourcevar.h> 57b43179fbSJeff Roberson #include <sys/sched.h> 586c35e809SDag-Erling Smørgrav #include <sys/sbuf.h> 59a5a96a19SJohn Baldwin #include <sys/sysctl.h> 60961a7b24SJohn Baldwin #include <sys/turnstile.h> 6136412d79SJohn Baldwin #include <sys/vmmeter.h> 620384fff8SJason Evans 6336412d79SJohn Baldwin #include <machine/atomic.h> 6436412d79SJohn Baldwin #include <machine/bus.h> 6536412d79SJohn Baldwin #include <machine/clock.h> 660384fff8SJason Evans #include <machine/cpu.h> 6736412d79SJohn Baldwin 689c36c934SJohn Baldwin #include <ddb/ddb.h> 699c36c934SJohn Baldwin 7036412d79SJohn Baldwin #include <vm/vm.h> 7136412d79SJohn Baldwin #include <vm/vm_extern.h> 7236412d79SJohn Baldwin 730cde2e34SJason Evans /* 74b9a80acaSStephan Uphoff * Force MUTEX_WAKE_ALL for now. 75b9a80acaSStephan Uphoff * single thread wakeup needs fixes to avoid race conditions with 76b9a80acaSStephan Uphoff * priority inheritance. 77b9a80acaSStephan Uphoff */ 78b9a80acaSStephan Uphoff #ifndef MUTEX_WAKE_ALL 79b9a80acaSStephan Uphoff #define MUTEX_WAKE_ALL 80b9a80acaSStephan Uphoff #endif 81b9a80acaSStephan Uphoff 82b9a80acaSStephan Uphoff /* 839ed346baSBosko Milekic * Internal utility macros. 840cde2e34SJason Evans */ 859ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 860cde2e34SJason Evans 879ed346baSBosko Milekic #define mtx_owner(m) (mtx_unowned((m)) ? NULL \ 88b40ce416SJulian Elischer : (struct thread *)((m)->mtx_lock & MTX_FLAGMASK)) 899ed346baSBosko Milekic 900cde2e34SJason Evans /* 9119284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 920cde2e34SJason Evans */ 9319284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 9419284646SJohn Baldwin "sleep mutex", 9519284646SJohn Baldwin LC_SLEEPLOCK | LC_RECURSABLE 9619284646SJohn Baldwin }; 9719284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 9819284646SJohn Baldwin "spin mutex", 9919284646SJohn Baldwin LC_SPINLOCK | LC_RECURSABLE 1008484de75SJohn Baldwin }; 1018484de75SJohn Baldwin 1029ed346baSBosko Milekic /* 103c53c013bSJohn Baldwin * System-wide mutexes 104c53c013bSJohn Baldwin */ 105c53c013bSJohn Baldwin struct mtx sched_lock; 106c53c013bSJohn Baldwin struct mtx Giant; 107c53c013bSJohn Baldwin 1086c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 1096c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging"); 1106c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling"); 1116c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0; 1126c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW, 1136c35e809SDag-Erling Smørgrav &mutex_prof_enable, 0, "Enable tracing of mutex holdtime"); 1146c35e809SDag-Erling Smørgrav 1156c35e809SDag-Erling Smørgrav struct mutex_prof { 1166c35e809SDag-Erling Smørgrav const char *name; 1176c35e809SDag-Erling Smørgrav const char *file; 1186c35e809SDag-Erling Smørgrav int line; 119ecf031c9SDag-Erling Smørgrav uintmax_t cnt_max; 120ecf031c9SDag-Erling Smørgrav uintmax_t cnt_tot; 121ecf031c9SDag-Erling Smørgrav uintmax_t cnt_cur; 1228dc10be8SRobert Watson uintmax_t cnt_contest_holding; 1238dc10be8SRobert Watson uintmax_t cnt_contest_locking; 124e6330704SDag-Erling Smørgrav struct mutex_prof *next; 1256c35e809SDag-Erling Smørgrav }; 1266c35e809SDag-Erling Smørgrav 1276c35e809SDag-Erling Smørgrav /* 1286c35e809SDag-Erling Smørgrav * mprof_buf is a static pool of profiling records to avoid possible 1296c35e809SDag-Erling Smørgrav * reentrance of the memory allocation functions. 1306c35e809SDag-Erling Smørgrav * 1316c35e809SDag-Erling Smørgrav * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE. 1326c35e809SDag-Erling Smørgrav */ 13300096801SJohn-Mark Gurney #ifdef MPROF_BUFFERS 13400096801SJohn-Mark Gurney #define NUM_MPROF_BUFFERS MPROF_BUFFERS 13500096801SJohn-Mark Gurney #else 136e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000 13700096801SJohn-Mark Gurney #endif 1386c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS]; 1396c35e809SDag-Erling Smørgrav static int first_free_mprof_buf; 14000096801SJohn-Mark Gurney #ifndef MPROF_HASH_SIZE 141e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009 14200096801SJohn-Mark Gurney #endif 14300096801SJohn-Mark Gurney #if NUM_MPROF_BUFFERS >= MPROF_HASH_SIZE 14400096801SJohn-Mark Gurney #error MPROF_BUFFERS must be larger than MPROF_HASH_SIZE 14500096801SJohn-Mark Gurney #endif 1466c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE]; 1470bd5f797SMike Makonnen /* SWAG: sbuf size = avg stat. line size * number of locks */ 1480bd5f797SMike Makonnen #define MPROF_SBUF_SIZE 256 * 400 1496c35e809SDag-Erling Smørgrav 1506c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions; 1516c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD, 1526c35e809SDag-Erling Smørgrav &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded"); 1536c35e809SDag-Erling Smørgrav static int mutex_prof_records; 1546c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD, 1556c35e809SDag-Erling Smørgrav &mutex_prof_records, 0, "Number of profiling records"); 1566c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS; 1576c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD, 1586c35e809SDag-Erling Smørgrav &mutex_prof_maxrecords, 0, "Maximum number of profiling records"); 1596c35e809SDag-Erling Smørgrav static int mutex_prof_rejected; 1606c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD, 1616c35e809SDag-Erling Smørgrav &mutex_prof_rejected, 0, "Number of rejected profiling records"); 1626c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE; 1636c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD, 1646c35e809SDag-Erling Smørgrav &mutex_prof_hashsize, 0, "Hash size"); 1656c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0; 1666c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD, 1676c35e809SDag-Erling Smørgrav &mutex_prof_collisions, 0, "Number of hash collisions"); 1686c35e809SDag-Erling Smørgrav 1696c35e809SDag-Erling Smørgrav /* 1706c35e809SDag-Erling Smørgrav * mprof_mtx protects the profiling buffers and the hash. 1716c35e809SDag-Erling Smørgrav */ 1726c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx; 173e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET); 1746c35e809SDag-Erling Smørgrav 175b784ffe9SDag-Erling Smørgrav static u_int64_t 176b784ffe9SDag-Erling Smørgrav nanoseconds(void) 177b784ffe9SDag-Erling Smørgrav { 178b784ffe9SDag-Erling Smørgrav struct timespec tv; 179b784ffe9SDag-Erling Smørgrav 180b784ffe9SDag-Erling Smørgrav nanotime(&tv); 181b784ffe9SDag-Erling Smørgrav return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec); 182b784ffe9SDag-Erling Smørgrav } 183b784ffe9SDag-Erling Smørgrav 1846c35e809SDag-Erling Smørgrav static int 1856c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 1866c35e809SDag-Erling Smørgrav { 1876c35e809SDag-Erling Smørgrav struct sbuf *sb; 1886c35e809SDag-Erling Smørgrav int error, i; 1890bd5f797SMike Makonnen static int multiplier = 1; 1906c35e809SDag-Erling Smørgrav 1916c35e809SDag-Erling Smørgrav if (first_free_mprof_buf == 0) 1926d036900SDag-Erling Smørgrav return (SYSCTL_OUT(req, "No locking recorded", 1936d036900SDag-Erling Smørgrav sizeof("No locking recorded"))); 1946c35e809SDag-Erling Smørgrav 1950bd5f797SMike Makonnen retry_sbufops: 1960bd5f797SMike Makonnen sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN); 1974f201858SGleb Smirnoff sbuf_printf(sb, "\n%6s %12s %11s %5s %12s %12s %s\n", 1988dc10be8SRobert Watson "max", "total", "count", "avg", "cnt_hold", "cnt_lock", "name"); 1996d036900SDag-Erling Smørgrav /* 2006d036900SDag-Erling Smørgrav * XXX this spinlock seems to be by far the largest perpetrator 2016d036900SDag-Erling Smørgrav * of spinlock latency (1.6 msec on an Athlon1600 was recorded 2026d036900SDag-Erling Smørgrav * even before I pessimized it further by moving the average 2036d036900SDag-Erling Smørgrav * computation here). 2046d036900SDag-Erling Smørgrav */ 2056c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 2060bd5f797SMike Makonnen for (i = 0; i < first_free_mprof_buf; ++i) { 2078dc10be8SRobert Watson sbuf_printf(sb, "%6ju %12ju %11ju %5ju %12ju %12ju %s:%d (%s)\n", 208ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_max / 1000, 209ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / 1000, 210ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur, 211ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 : 212ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000), 2138dc10be8SRobert Watson mprof_buf[i].cnt_contest_holding, 2148dc10be8SRobert Watson mprof_buf[i].cnt_contest_locking, 2156c35e809SDag-Erling Smørgrav mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 2160bd5f797SMike Makonnen if (sbuf_overflowed(sb)) { 2170bd5f797SMike Makonnen mtx_unlock_spin(&mprof_mtx); 2180bd5f797SMike Makonnen sbuf_delete(sb); 2190bd5f797SMike Makonnen multiplier++; 2200bd5f797SMike Makonnen goto retry_sbufops; 2210bd5f797SMike Makonnen } 2220bd5f797SMike Makonnen } 2236c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 2246c35e809SDag-Erling Smørgrav sbuf_finish(sb); 2256c35e809SDag-Erling Smørgrav error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2266c35e809SDag-Erling Smørgrav sbuf_delete(sb); 2276c35e809SDag-Erling Smørgrav return (error); 2286c35e809SDag-Erling Smørgrav } 2296c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD, 2306c35e809SDag-Erling Smørgrav NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 23194ffb20dSRobert Watson 23294ffb20dSRobert Watson static int 23394ffb20dSRobert Watson reset_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 23494ffb20dSRobert Watson { 23594ffb20dSRobert Watson int error, v; 23694ffb20dSRobert Watson 23794ffb20dSRobert Watson if (first_free_mprof_buf == 0) 23894ffb20dSRobert Watson return (0); 23994ffb20dSRobert Watson 24094ffb20dSRobert Watson v = 0; 24194ffb20dSRobert Watson error = sysctl_handle_int(oidp, &v, 0, req); 24294ffb20dSRobert Watson if (error) 24394ffb20dSRobert Watson return (error); 24494ffb20dSRobert Watson if (req->newptr == NULL) 24594ffb20dSRobert Watson return (error); 24694ffb20dSRobert Watson if (v == 0) 24794ffb20dSRobert Watson return (0); 24894ffb20dSRobert Watson 24994ffb20dSRobert Watson mtx_lock_spin(&mprof_mtx); 25094ffb20dSRobert Watson bzero(mprof_buf, sizeof(*mprof_buf) * first_free_mprof_buf); 25194ffb20dSRobert Watson bzero(mprof_hash, sizeof(struct mtx *) * MPROF_HASH_SIZE); 25294ffb20dSRobert Watson first_free_mprof_buf = 0; 25394ffb20dSRobert Watson mtx_unlock_spin(&mprof_mtx); 25494ffb20dSRobert Watson return (0); 25594ffb20dSRobert Watson } 25694ffb20dSRobert Watson SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW, 25794ffb20dSRobert Watson NULL, 0, reset_mutex_prof_stats, "I", "Reset mutex profiling statistics"); 2586c35e809SDag-Erling Smørgrav #endif 2596c35e809SDag-Erling Smørgrav 2600cde2e34SJason Evans /* 2616283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 2626283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 2636283b7d0SJohn Baldwin */ 2646283b7d0SJohn Baldwin void 2656283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 2666283b7d0SJohn Baldwin { 2676283b7d0SJohn Baldwin 268dde96c99SJohn Baldwin MPASS(curthread != NULL); 2690d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 2700d975d63SJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 2710d975d63SJohn Baldwin file, line)); 2728d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 2738d768e76SJohn Baldwin file, line); 274dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 275dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 276dde96c99SJohn Baldwin line); 277dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 2786c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 2796c35e809SDag-Erling Smørgrav /* don't reset the timer when/if recursing */ 280b61860adSDag-Erling Smørgrav if (m->mtx_acqtime == 0) { 281b61860adSDag-Erling Smørgrav m->mtx_filename = file; 282b61860adSDag-Erling Smørgrav m->mtx_lineno = line; 283b61860adSDag-Erling Smørgrav m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0; 2846c35e809SDag-Erling Smørgrav ++mutex_prof_acquisitions; 2856c35e809SDag-Erling Smørgrav } 2866c35e809SDag-Erling Smørgrav #endif 2876283b7d0SJohn Baldwin } 2886283b7d0SJohn Baldwin 2896283b7d0SJohn Baldwin void 2906283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 2916283b7d0SJohn Baldwin { 2926283b7d0SJohn Baldwin 293dde96c99SJohn Baldwin MPASS(curthread != NULL); 2940d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 2950d975d63SJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 2960d975d63SJohn Baldwin file, line)); 2970d975d63SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 2980d975d63SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 2990d975d63SJohn Baldwin line); 30021377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 3016c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 302b61860adSDag-Erling Smørgrav if (m->mtx_acqtime != 0) { 3036c35e809SDag-Erling Smørgrav static const char *unknown = "(unknown)"; 3046c35e809SDag-Erling Smørgrav struct mutex_prof *mpp; 305b784ffe9SDag-Erling Smørgrav u_int64_t acqtime, now; 3066c35e809SDag-Erling Smørgrav const char *p, *q; 307e6330704SDag-Erling Smørgrav volatile u_int hash; 3086c35e809SDag-Erling Smørgrav 309b784ffe9SDag-Erling Smørgrav now = nanoseconds(); 310b61860adSDag-Erling Smørgrav acqtime = m->mtx_acqtime; 311b61860adSDag-Erling Smørgrav m->mtx_acqtime = 0; 312b784ffe9SDag-Erling Smørgrav if (now <= acqtime) 3136c35e809SDag-Erling Smørgrav goto out; 3140bd5f797SMike Makonnen for (p = m->mtx_filename; 3150bd5f797SMike Makonnen p != NULL && strncmp(p, "../", 3) == 0; p += 3) 3166c35e809SDag-Erling Smørgrav /* nothing */ ; 3176c35e809SDag-Erling Smørgrav if (p == NULL || *p == '\0') 3186c35e809SDag-Erling Smørgrav p = unknown; 319b61860adSDag-Erling Smørgrav for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q) 3206c35e809SDag-Erling Smørgrav hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 3216c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 322e6330704SDag-Erling Smørgrav for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next) 323b61860adSDag-Erling Smørgrav if (mpp->line == m->mtx_lineno && 324b61860adSDag-Erling Smørgrav strcmp(mpp->file, p) == 0) 3256c35e809SDag-Erling Smørgrav break; 3266c35e809SDag-Erling Smørgrav if (mpp == NULL) { 3276c35e809SDag-Erling Smørgrav /* Just exit if we cannot get a trace buffer */ 3286c35e809SDag-Erling Smørgrav if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 3296c35e809SDag-Erling Smørgrav ++mutex_prof_rejected; 3306c35e809SDag-Erling Smørgrav goto unlock; 3316c35e809SDag-Erling Smørgrav } 3326c35e809SDag-Erling Smørgrav mpp = &mprof_buf[first_free_mprof_buf++]; 3336c35e809SDag-Erling Smørgrav mpp->name = mtx_name(m); 3346c35e809SDag-Erling Smørgrav mpp->file = p; 335b61860adSDag-Erling Smørgrav mpp->line = m->mtx_lineno; 336e6330704SDag-Erling Smørgrav mpp->next = mprof_hash[hash]; 337e6330704SDag-Erling Smørgrav if (mprof_hash[hash] != NULL) 338e6330704SDag-Erling Smørgrav ++mutex_prof_collisions; 3396c35e809SDag-Erling Smørgrav mprof_hash[hash] = mpp; 340e6330704SDag-Erling Smørgrav ++mutex_prof_records; 3416c35e809SDag-Erling Smørgrav } 3426c35e809SDag-Erling Smørgrav /* 3436c35e809SDag-Erling Smørgrav * Record if the mutex has been held longer now than ever 3446d036900SDag-Erling Smørgrav * before. 3456c35e809SDag-Erling Smørgrav */ 346ecf031c9SDag-Erling Smørgrav if (now - acqtime > mpp->cnt_max) 347ecf031c9SDag-Erling Smørgrav mpp->cnt_max = now - acqtime; 348ecf031c9SDag-Erling Smørgrav mpp->cnt_tot += now - acqtime; 349ecf031c9SDag-Erling Smørgrav mpp->cnt_cur++; 3508dc10be8SRobert Watson /* 3518dc10be8SRobert Watson * There's a small race, really we should cmpxchg 3528dc10be8SRobert Watson * 0 with the current value, but that would bill 3538dc10be8SRobert Watson * the contention to the wrong lock instance if 3548dc10be8SRobert Watson * it followed this also. 3558dc10be8SRobert Watson */ 3568dc10be8SRobert Watson mpp->cnt_contest_holding += m->mtx_contest_holding; 3578dc10be8SRobert Watson m->mtx_contest_holding = 0; 3588dc10be8SRobert Watson mpp->cnt_contest_locking += m->mtx_contest_locking; 3598dc10be8SRobert Watson m->mtx_contest_locking = 0; 3606c35e809SDag-Erling Smørgrav unlock: 3616c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 3626c35e809SDag-Erling Smørgrav } 3636c35e809SDag-Erling Smørgrav out: 3646c35e809SDag-Erling Smørgrav #endif 365dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 3666283b7d0SJohn Baldwin } 3676283b7d0SJohn Baldwin 3686283b7d0SJohn Baldwin void 3696283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 3706283b7d0SJohn Baldwin { 3716283b7d0SJohn Baldwin 372dde96c99SJohn Baldwin MPASS(curthread != NULL); 3730d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 3740d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 3750d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 3768d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 3778d768e76SJohn Baldwin file, line); 378dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 379dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 380dde96c99SJohn Baldwin line); 381dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3826283b7d0SJohn Baldwin } 3836283b7d0SJohn Baldwin 3846283b7d0SJohn Baldwin void 3856283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 3866283b7d0SJohn Baldwin { 3876283b7d0SJohn Baldwin 388dde96c99SJohn Baldwin MPASS(curthread != NULL); 3890d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 3900d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 3910d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 392dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 393dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 394dde96c99SJohn Baldwin line); 3950d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 396dde96c99SJohn Baldwin _rel_spin_lock(m); 3976283b7d0SJohn Baldwin } 3986283b7d0SJohn Baldwin 3996283b7d0SJohn Baldwin /* 4009ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 401eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 402eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 4030cde2e34SJason Evans */ 4040cde2e34SJason Evans int 4059ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 4060cde2e34SJason Evans { 4070cde2e34SJason Evans int rval; 4080cde2e34SJason Evans 409b40ce416SJulian Elischer MPASS(curthread != NULL); 4109ed346baSBosko Milekic 411eac09796SJohn Baldwin if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) { 412eac09796SJohn Baldwin m->mtx_recurse++; 413eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 414eac09796SJohn Baldwin rval = 1; 415eac09796SJohn Baldwin } else 416122eceefSJohn Baldwin rval = _obtain_lock(m, (uintptr_t)curthread); 4179ed346baSBosko Milekic 41819284646SJohn Baldwin LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 4196b869595SJohn Baldwin if (rval) 4202d96f0b1SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4212d96f0b1SJohn Baldwin file, line); 4229ed346baSBosko Milekic 42319284646SJohn Baldwin return (rval); 4240cde2e34SJason Evans } 4250cde2e34SJason Evans 4260cde2e34SJason Evans /* 4279ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4289ed346baSBosko Milekic * 4299ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4309ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4310cde2e34SJason Evans */ 4320cde2e34SJason Evans void 433122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file, 434bdcfcf5bSJohn Baldwin int line) 43536412d79SJohn Baldwin { 436701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 4372498cf8cSJohn Baldwin struct thread *owner; 4382498cf8cSJohn Baldwin #endif 4395fa8dd90SJohn Baldwin uintptr_t v; 44002bd1bcdSIan Dowse #ifdef KTR 44102bd1bcdSIan Dowse int cont_logged = 0; 44202bd1bcdSIan Dowse #endif 4438dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4448dc10be8SRobert Watson int contested; 4458dc10be8SRobert Watson #endif 44636412d79SJohn Baldwin 4475fa8dd90SJohn Baldwin if (mtx_owned(m)) { 448eac09796SJohn Baldwin KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0, 449eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 450eac09796SJohn Baldwin m->mtx_object.lo_name, file, line)); 45136412d79SJohn Baldwin m->mtx_recurse++; 45208812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 45319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4545746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 45536412d79SJohn Baldwin return; 45636412d79SJohn Baldwin } 4579ed346baSBosko Milekic 45819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 45915ec816aSJohn Baldwin CTR4(KTR_LOCK, 46015ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 46119284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 4621bd0eefbSJohn Baldwin 4638dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4648dc10be8SRobert Watson contested = 0; 4658dc10be8SRobert Watson #endif 466122eceefSJohn Baldwin while (!_obtain_lock(m, tid)) { 4678dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4688dc10be8SRobert Watson contested = 1; 4698dc10be8SRobert Watson atomic_add_int(&m->mtx_contest_holding, 1); 4708dc10be8SRobert Watson #endif 4712ff0e645SJohn Baldwin turnstile_lock(&m->mtx_object); 4725fa8dd90SJohn Baldwin v = m->mtx_lock; 4735fa8dd90SJohn Baldwin 47436412d79SJohn Baldwin /* 4759ed346baSBosko Milekic * Check if the lock has been released while spinning for 476961a7b24SJohn Baldwin * the turnstile chain lock. 47736412d79SJohn Baldwin */ 4785fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 479961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 4809f1b87f1SMaxime Henrion cpu_spinwait(); 48136412d79SJohn Baldwin continue; 48236412d79SJohn Baldwin } 4839ed346baSBosko Milekic 484535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 485535eb309SJohn Baldwin MPASS(v != MTX_CONTESTED); 486535eb309SJohn Baldwin #else 48736412d79SJohn Baldwin /* 4889ed346baSBosko Milekic * The mutex was marked contested on release. This means that 489f7ee1590SJohn Baldwin * there are other threads blocked on it. Grab ownership of 490f7ee1590SJohn Baldwin * it and propagate its priority to the current thread if 491f7ee1590SJohn Baldwin * necessary. 49236412d79SJohn Baldwin */ 49336412d79SJohn Baldwin if (v == MTX_CONTESTED) { 494122eceefSJohn Baldwin m->mtx_lock = tid | MTX_CONTESTED; 4952ff0e645SJohn Baldwin turnstile_claim(&m->mtx_object); 4968dc10be8SRobert Watson break; 49736412d79SJohn Baldwin } 498535eb309SJohn Baldwin #endif 4999ed346baSBosko Milekic 50036412d79SJohn Baldwin /* 5019ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 5029ed346baSBosko Milekic * setting the contested bit, the mutex was either released 5039ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 50436412d79SJohn Baldwin */ 50536412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 506122eceefSJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 507961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 5089f1b87f1SMaxime Henrion cpu_spinwait(); 50936412d79SJohn Baldwin continue; 51036412d79SJohn Baldwin } 51136412d79SJohn Baldwin 512701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 5132498cf8cSJohn Baldwin /* 5142498cf8cSJohn Baldwin * If the current owner of the lock is executing on another 5152498cf8cSJohn Baldwin * CPU, spin instead of blocking. 5162498cf8cSJohn Baldwin */ 5172498cf8cSJohn Baldwin owner = (struct thread *)(v & MTX_FLAGMASK); 518a9abdce4SRobert Watson #ifdef ADAPTIVE_GIANT 519a9abdce4SRobert Watson if (TD_IS_RUNNING(owner)) { 520a9abdce4SRobert Watson #else 52127dad03cSJohn Baldwin if (m != &Giant && TD_IS_RUNNING(owner)) { 522a9abdce4SRobert Watson #endif 523961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 52427dad03cSJohn Baldwin while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) { 5259f1b87f1SMaxime Henrion cpu_spinwait(); 5267fcca609SJohn Baldwin } 5272498cf8cSJohn Baldwin continue; 5282498cf8cSJohn Baldwin } 529701f1408SScott Long #endif /* SMP && !NO_ADAPTIVE_MUTEXES */ 5302498cf8cSJohn Baldwin 5319ed346baSBosko Milekic /* 5327feefcd6SJohn Baldwin * We definitely must sleep for this lock. 5339ed346baSBosko Milekic */ 53436412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 53536412d79SJohn Baldwin 53602bd1bcdSIan Dowse #ifdef KTR 53702bd1bcdSIan Dowse if (!cont_logged) { 53802bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 53902bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 540122eceefSJohn Baldwin (void *)tid, file, line, m->mtx_object.lo_name, 54102bd1bcdSIan Dowse WITNESS_FILE(&m->mtx_object), 54202bd1bcdSIan Dowse WITNESS_LINE(&m->mtx_object)); 54302bd1bcdSIan Dowse cont_logged = 1; 54402bd1bcdSIan Dowse } 54502bd1bcdSIan Dowse #endif 54636412d79SJohn Baldwin 5479ed346baSBosko Milekic /* 548961a7b24SJohn Baldwin * Block on the turnstile. 5499ed346baSBosko Milekic */ 5502ff0e645SJohn Baldwin turnstile_wait(&m->mtx_object, mtx_owner(m)); 55136412d79SJohn Baldwin } 5529ed346baSBosko Milekic 55302bd1bcdSIan Dowse #ifdef KTR 55402bd1bcdSIan Dowse if (cont_logged) { 55502bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 55602bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 557122eceefSJohn Baldwin m->mtx_object.lo_name, (void *)tid, file, line); 55802bd1bcdSIan Dowse } 55902bd1bcdSIan Dowse #endif 5608dc10be8SRobert Watson #ifdef MUTEX_PROFILING 5618dc10be8SRobert Watson if (contested) 5628dc10be8SRobert Watson m->mtx_contest_locking++; 5638dc10be8SRobert Watson m->mtx_contest_holding = 0; 5648dc10be8SRobert Watson #endif 56536412d79SJohn Baldwin return; 5669ed346baSBosko Milekic } 5679ed346baSBosko Milekic 56833fb8a38SJohn Baldwin #ifdef SMP 5699ed346baSBosko Milekic /* 5709ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 5719ed346baSBosko Milekic * 5729ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 5739ed346baSBosko Milekic * is handled inline. 5749ed346baSBosko Milekic */ 5759ed346baSBosko Milekic void 576122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file, 577bdcfcf5bSJohn Baldwin int line) 57836412d79SJohn Baldwin { 57936412d79SJohn Baldwin int i = 0; 58036412d79SJohn Baldwin 58119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 5825746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 5839ed346baSBosko Milekic 58436412d79SJohn Baldwin for (;;) { 585122eceefSJohn Baldwin if (_obtain_lock(m, tid)) 58636412d79SJohn Baldwin break; 5879ed346baSBosko Milekic 5887141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 589c6a37e84SJohn Baldwin spinlock_exit(); 59036412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 591703fc290SJohn Baldwin if (i++ < 10000000) { 5929f1b87f1SMaxime Henrion cpu_spinwait(); 59336412d79SJohn Baldwin continue; 594703fc290SJohn Baldwin } 5950e54ddadSJohn Baldwin if (i < 60000000) 59636412d79SJohn Baldwin DELAY(1); 5972d50560aSMarcel Moolenaar else if (!kdb_active) { 59841109518SJohn Baldwin printf("spin lock %s held by %p for > 5 seconds\n", 59919284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock); 60041109518SJohn Baldwin #ifdef WITNESS 60141109518SJohn Baldwin witness_display_spinlock(&m->mtx_object, 60241109518SJohn Baldwin mtx_owner(m)); 60341109518SJohn Baldwin #endif 60441109518SJohn Baldwin panic("spin lock held too long"); 60541109518SJohn Baldwin } 6069f1b87f1SMaxime Henrion cpu_spinwait(); 60736412d79SJohn Baldwin } 608c6a37e84SJohn Baldwin spinlock_enter(); 60936412d79SJohn Baldwin } 61036412d79SJohn Baldwin 61119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6129ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 6139ed346baSBosko Milekic 61436412d79SJohn Baldwin return; 61536412d79SJohn Baldwin } 61633fb8a38SJohn Baldwin #endif /* SMP */ 61736412d79SJohn Baldwin 6189ed346baSBosko Milekic /* 6199ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 6209ed346baSBosko Milekic * 6219ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 6229ed346baSBosko Milekic * need to wake up a blocked thread). 6239ed346baSBosko Milekic */ 62436412d79SJohn Baldwin void 6259ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 62636412d79SJohn Baldwin { 627961a7b24SJohn Baldwin struct turnstile *ts; 6280c0b25aeSJohn Baldwin #ifndef PREEMPTION 629b40ce416SJulian Elischer struct thread *td, *td1; 6300c0b25aeSJohn Baldwin #endif 6319ed346baSBosko Milekic 63208812b39SBosko Milekic if (mtx_recursed(m)) { 63336412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 63408812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 63519284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6369ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 63736412d79SJohn Baldwin return; 63836412d79SJohn Baldwin } 6399ed346baSBosko Milekic 6402ff0e645SJohn Baldwin turnstile_lock(&m->mtx_object); 641961a7b24SJohn Baldwin ts = turnstile_lookup(&m->mtx_object); 64219284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6439ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 6449ed346baSBosko Milekic 645ece2d989SPawel Jakub Dawidek #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 646961a7b24SJohn Baldwin if (ts == NULL) { 6472498cf8cSJohn Baldwin _release_lock_quick(m); 6482498cf8cSJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6492498cf8cSJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m); 650961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 6512498cf8cSJohn Baldwin return; 6522498cf8cSJohn Baldwin } 653961a7b24SJohn Baldwin #else 654961a7b24SJohn Baldwin MPASS(ts != NULL); 6552498cf8cSJohn Baldwin #endif 6560c0b25aeSJohn Baldwin #ifndef PREEMPTION 657961a7b24SJohn Baldwin /* XXX */ 658961a7b24SJohn Baldwin td1 = turnstile_head(ts); 6590c0b25aeSJohn Baldwin #endif 660535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 661535eb309SJohn Baldwin turnstile_broadcast(ts); 662535eb309SJohn Baldwin _release_lock_quick(m); 663535eb309SJohn Baldwin #else 664961a7b24SJohn Baldwin if (turnstile_signal(ts)) { 66536412d79SJohn Baldwin _release_lock_quick(m); 66619284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6679ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 668961a7b24SJohn Baldwin } else { 669f7ee1590SJohn Baldwin m->mtx_lock = MTX_CONTESTED; 67019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 671961a7b24SJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested", 672961a7b24SJohn Baldwin m); 673e0817317SJulian Elischer } 674535eb309SJohn Baldwin #endif 675961a7b24SJohn Baldwin turnstile_unpend(ts); 6769ed346baSBosko Milekic 6770c0b25aeSJohn Baldwin #ifndef PREEMPTION 678961a7b24SJohn Baldwin /* 679961a7b24SJohn Baldwin * XXX: This is just a hack until preemption is done. However, 680961a7b24SJohn Baldwin * once preemption is done we need to either wrap the 681961a7b24SJohn Baldwin * turnstile_signal() and release of the actual lock in an 682961a7b24SJohn Baldwin * extra critical section or change the preemption code to 683961a7b24SJohn Baldwin * always just set a flag and never do instant-preempts. 684961a7b24SJohn Baldwin */ 685961a7b24SJohn Baldwin td = curthread; 686961a7b24SJohn Baldwin if (td->td_critnest > 0 || td1->td_priority >= td->td_priority) 687961a7b24SJohn Baldwin return; 688961a7b24SJohn Baldwin mtx_lock_spin(&sched_lock); 689961a7b24SJohn Baldwin if (!TD_IS_RUNNING(td1)) { 69036412d79SJohn Baldwin #ifdef notyet 691b40ce416SJulian Elischer if (td->td_ithd != NULL) { 692b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 69336412d79SJohn Baldwin 69436412d79SJohn Baldwin if (it->it_interrupted) { 69519284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 69636412d79SJohn Baldwin CTR2(KTR_LOCK, 69715ec816aSJohn Baldwin "_mtx_unlock_sleep: %p interrupted %p", 69836412d79SJohn Baldwin it, it->it_interrupted); 69936412d79SJohn Baldwin intr_thd_fixup(it); 70036412d79SJohn Baldwin } 70136412d79SJohn Baldwin } 70236412d79SJohn Baldwin #endif 70319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 704562e4ffeSJohn Baldwin CTR2(KTR_LOCK, 7059ed346baSBosko Milekic "_mtx_unlock_sleep: %p switching out lock=%p", m, 7069ed346baSBosko Milekic (void *)m->mtx_lock); 7079ed346baSBosko Milekic 708bf0acc27SJohn Baldwin mi_switch(SW_INVOL, NULL); 70919284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7109ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 71131271627SJohn Baldwin m, (void *)m->mtx_lock); 71236412d79SJohn Baldwin } 7139ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 7140c0b25aeSJohn Baldwin #endif 7159ed346baSBosko Milekic 7169ed346baSBosko Milekic return; 7179ed346baSBosko Milekic } 7189ed346baSBosko Milekic 7199ed346baSBosko Milekic /* 7209ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 7219ed346baSBosko Milekic * See the _rel_spin_lock() macro for the details. 7229ed346baSBosko Milekic */ 7239ed346baSBosko Milekic 7249ed346baSBosko Milekic /* 72515ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 7269ed346baSBosko Milekic */ 7271103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 7280cde2e34SJason Evans void 72956771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line) 7300cde2e34SJason Evans { 7315cb0fbe4SJohn Baldwin 7321126349aSPaul Saab if (panicstr != NULL || dumping) 7335cb0fbe4SJohn Baldwin return; 734a10f4966SJake Burkholder switch (what) { 7350cde2e34SJason Evans case MA_OWNED: 7360cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 7370cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 738a10f4966SJake Burkholder if (!mtx_owned(m)) 7390cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 74019284646SJohn Baldwin m->mtx_object.lo_name, file, line); 741a10f4966SJake Burkholder if (mtx_recursed(m)) { 742a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 7430cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 74419284646SJohn Baldwin m->mtx_object.lo_name, file, line); 745a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 7460cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 74719284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7480cde2e34SJason Evans } 7490cde2e34SJason Evans break; 7500cde2e34SJason Evans case MA_NOTOWNED: 751a10f4966SJake Burkholder if (mtx_owned(m)) 7520cde2e34SJason Evans panic("mutex %s owned at %s:%d", 75319284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7540cde2e34SJason Evans break; 7550cde2e34SJason Evans default: 75656771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 7570cde2e34SJason Evans } 7580cde2e34SJason Evans } 7590cde2e34SJason Evans #endif 7600cde2e34SJason Evans 7619ed346baSBosko Milekic /* 7629ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 76319284646SJohn Baldwin * 76419284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 76519284646SJohn Baldwin * maintained by the witness code. 7669ed346baSBosko Milekic */ 76736412d79SJohn Baldwin #ifdef MUTEX_DEBUG 76836412d79SJohn Baldwin 7694d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 77036412d79SJohn Baldwin 77119284646SJohn Baldwin void 77219284646SJohn Baldwin mtx_validate(struct mtx *m) 77336412d79SJohn Baldwin { 77436412d79SJohn Baldwin 77536412d79SJohn Baldwin /* 776fa669ab7SPoul-Henning Kamp * XXX: When kernacc() does not require Giant we can reenable this check 777fa669ab7SPoul-Henning Kamp */ 778fa669ab7SPoul-Henning Kamp #ifdef notyet 779fa669ab7SPoul-Henning Kamp /* 78036412d79SJohn Baldwin * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly 78136412d79SJohn Baldwin * we can re-enable the kernacc() checks. 78236412d79SJohn Baldwin */ 78336412d79SJohn Baldwin #ifndef __alpha__ 78476dcbd6fSBosko Milekic /* 78576dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 78676dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 78776dcbd6fSBosko Milekic * requires Giant itself. 78876dcbd6fSBosko Milekic */ 789ab07087eSBosko Milekic if (!cold) 790ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 791ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 79219284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 79336412d79SJohn Baldwin #endif 794fa669ab7SPoul-Henning Kamp #endif 79536412d79SJohn Baldwin } 79636412d79SJohn Baldwin #endif 79736412d79SJohn Baldwin 7989ed346baSBosko Milekic /* 799c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 800c27b5699SAndrew R. Reiter */ 801c27b5699SAndrew R. Reiter void 802c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 803c27b5699SAndrew R. Reiter { 804c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 805c27b5699SAndrew R. Reiter 8060c88508aSJohn Baldwin mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 807c27b5699SAndrew R. Reiter } 808c27b5699SAndrew R. Reiter 809c27b5699SAndrew R. Reiter /* 8109ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 8110c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 8120c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 8130c88508aSJohn Baldwin * witness. 8149ed346baSBosko Milekic */ 81536412d79SJohn Baldwin void 8160c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts) 81736412d79SJohn Baldwin { 81819284646SJohn Baldwin struct lock_object *lock; 8199ed346baSBosko Milekic 82019284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 82175d468eeSJohn Baldwin MTX_NOWITNESS | MTX_DUPOK)) == 0); 8229ed346baSBosko Milekic 82336412d79SJohn Baldwin #ifdef MUTEX_DEBUG 8249ed346baSBosko Milekic /* Diagnostic and error correction */ 82519284646SJohn Baldwin mtx_validate(m); 8266936206eSJohn Baldwin #endif 82736412d79SJohn Baldwin 82819284646SJohn Baldwin lock = &m->mtx_object; 8297ada5876SJohn Baldwin KASSERT((lock->lo_flags & LO_INITIALIZED) == 0, 830b82af320SPoul-Henning Kamp ("mutex \"%s\" %p already initialized", name, m)); 8317ada5876SJohn Baldwin bzero(m, sizeof(*m)); 83219284646SJohn Baldwin if (opts & MTX_SPIN) 83319284646SJohn Baldwin lock->lo_class = &lock_class_mtx_spin; 83419284646SJohn Baldwin else 83519284646SJohn Baldwin lock->lo_class = &lock_class_mtx_sleep; 8360c88508aSJohn Baldwin lock->lo_name = name; 8370c88508aSJohn Baldwin lock->lo_type = type != NULL ? type : name; 83819284646SJohn Baldwin if (opts & MTX_QUIET) 83919284646SJohn Baldwin lock->lo_flags = LO_QUIET; 84019284646SJohn Baldwin if (opts & MTX_RECURSE) 84119284646SJohn Baldwin lock->lo_flags |= LO_RECURSABLE; 84219284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 84319284646SJohn Baldwin lock->lo_flags |= LO_WITNESS; 844f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 845f22a4b62SJeff Roberson lock->lo_flags |= LO_DUPOK; 84619284646SJohn Baldwin 84719284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 8489ed346baSBosko Milekic 84919284646SJohn Baldwin LOCK_LOG_INIT(lock, opts); 850d1c1b841SJason Evans 85119284646SJohn Baldwin WITNESS_INIT(lock); 85236412d79SJohn Baldwin } 85336412d79SJohn Baldwin 8549ed346baSBosko Milekic /* 85519284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 85619284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 85719284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 85819284646SJohn Baldwin * flags. 8599ed346baSBosko Milekic */ 86036412d79SJohn Baldwin void 86136412d79SJohn Baldwin mtx_destroy(struct mtx *m) 86236412d79SJohn Baldwin { 86336412d79SJohn Baldwin 86419284646SJohn Baldwin LOCK_LOG_DESTROY(&m->mtx_object, 0); 8659ed346baSBosko Milekic 86619284646SJohn Baldwin if (!mtx_owned(m)) 86719284646SJohn Baldwin MPASS(mtx_unowned(m)); 86819284646SJohn Baldwin else { 86908812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 8709ed346baSBosko Milekic 87119284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 872c86b6ff5SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 873c86b6ff5SJohn Baldwin __LINE__); 87436412d79SJohn Baldwin } 8750384fff8SJason Evans 87619284646SJohn Baldwin WITNESS_DESTROY(&m->mtx_object); 8770384fff8SJason Evans } 878d23f5958SMatthew Dillon 879d23f5958SMatthew Dillon /* 880c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 881c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 882c53c013bSJohn Baldwin * setup before this is called. 883c53c013bSJohn Baldwin */ 884c53c013bSJohn Baldwin void 885c53c013bSJohn Baldwin mutex_init(void) 886c53c013bSJohn Baldwin { 887c53c013bSJohn Baldwin 888c53c013bSJohn Baldwin /* Setup thread0 so that mutexes work. */ 889c53c013bSJohn Baldwin LIST_INIT(&thread0.td_contested); 890c53c013bSJohn Baldwin 891961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 892961a7b24SJohn Baldwin init_turnstiles(); 893961a7b24SJohn Baldwin 894c53c013bSJohn Baldwin /* 895c53c013bSJohn Baldwin * Initialize mutexes. 896c53c013bSJohn Baldwin */ 8970c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 8980c88508aSJohn Baldwin mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE); 8990c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 900c53c013bSJohn Baldwin mtx_lock(&Giant); 901c53c013bSJohn Baldwin } 902