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> 482d50560aSMarcel Moolenaar #include <sys/kdb.h> 4936412d79SJohn Baldwin #include <sys/kernel.h> 506c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 5119284646SJohn Baldwin #include <sys/lock.h> 52fb919e4dSMark Murray #include <sys/malloc.h> 5319284646SJohn Baldwin #include <sys/mutex.h> 540384fff8SJason Evans #include <sys/proc.h> 55c4f7a187SJohn Baldwin #include <sys/resourcevar.h> 56b43179fbSJeff Roberson #include <sys/sched.h> 576c35e809SDag-Erling Smørgrav #include <sys/sbuf.h> 58a5a96a19SJohn Baldwin #include <sys/sysctl.h> 59961a7b24SJohn Baldwin #include <sys/turnstile.h> 6036412d79SJohn Baldwin #include <sys/vmmeter.h> 610384fff8SJason Evans 6236412d79SJohn Baldwin #include <machine/atomic.h> 6336412d79SJohn Baldwin #include <machine/bus.h> 6436412d79SJohn Baldwin #include <machine/clock.h> 650384fff8SJason Evans #include <machine/cpu.h> 6636412d79SJohn Baldwin 679c36c934SJohn Baldwin #include <ddb/ddb.h> 689c36c934SJohn Baldwin 6936412d79SJohn Baldwin #include <vm/vm.h> 7036412d79SJohn Baldwin #include <vm/vm_extern.h> 7136412d79SJohn Baldwin 720cde2e34SJason Evans /* 73b9a80acaSStephan Uphoff * Force MUTEX_WAKE_ALL for now. 74b9a80acaSStephan Uphoff * single thread wakeup needs fixes to avoid race conditions with 75b9a80acaSStephan Uphoff * priority inheritance. 76b9a80acaSStephan Uphoff */ 77b9a80acaSStephan Uphoff #ifndef MUTEX_WAKE_ALL 78b9a80acaSStephan Uphoff #define MUTEX_WAKE_ALL 79b9a80acaSStephan Uphoff #endif 80b9a80acaSStephan Uphoff 81b9a80acaSStephan Uphoff /* 829ed346baSBosko Milekic * Internal utility macros. 830cde2e34SJason Evans */ 849ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 850cde2e34SJason Evans 869ed346baSBosko Milekic #define mtx_owner(m) (mtx_unowned((m)) ? NULL \ 87b40ce416SJulian Elischer : (struct thread *)((m)->mtx_lock & MTX_FLAGMASK)) 889ed346baSBosko Milekic 890cde2e34SJason Evans /* 9019284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 910cde2e34SJason Evans */ 9219284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 9319284646SJohn Baldwin "sleep mutex", 9419284646SJohn Baldwin LC_SLEEPLOCK | LC_RECURSABLE 9519284646SJohn Baldwin }; 9619284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 9719284646SJohn Baldwin "spin mutex", 9819284646SJohn Baldwin LC_SPINLOCK | LC_RECURSABLE 998484de75SJohn Baldwin }; 1008484de75SJohn Baldwin 1019ed346baSBosko Milekic /* 102c53c013bSJohn Baldwin * System-wide mutexes 103c53c013bSJohn Baldwin */ 104c53c013bSJohn Baldwin struct mtx sched_lock; 105c53c013bSJohn Baldwin struct mtx Giant; 106c53c013bSJohn Baldwin 1076c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 1086c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging"); 1096c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling"); 1106c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0; 1116c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW, 1126c35e809SDag-Erling Smørgrav &mutex_prof_enable, 0, "Enable tracing of mutex holdtime"); 1136c35e809SDag-Erling Smørgrav 1146c35e809SDag-Erling Smørgrav struct mutex_prof { 1156c35e809SDag-Erling Smørgrav const char *name; 1166c35e809SDag-Erling Smørgrav const char *file; 1176c35e809SDag-Erling Smørgrav int line; 118ecf031c9SDag-Erling Smørgrav uintmax_t cnt_max; 119ecf031c9SDag-Erling Smørgrav uintmax_t cnt_tot; 120ecf031c9SDag-Erling Smørgrav uintmax_t cnt_cur; 1218dc10be8SRobert Watson uintmax_t cnt_contest_holding; 1228dc10be8SRobert Watson uintmax_t cnt_contest_locking; 123e6330704SDag-Erling Smørgrav struct mutex_prof *next; 1246c35e809SDag-Erling Smørgrav }; 1256c35e809SDag-Erling Smørgrav 1266c35e809SDag-Erling Smørgrav /* 1276c35e809SDag-Erling Smørgrav * mprof_buf is a static pool of profiling records to avoid possible 1286c35e809SDag-Erling Smørgrav * reentrance of the memory allocation functions. 1296c35e809SDag-Erling Smørgrav * 1306c35e809SDag-Erling Smørgrav * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE. 1316c35e809SDag-Erling Smørgrav */ 13200096801SJohn-Mark Gurney #ifdef MPROF_BUFFERS 13300096801SJohn-Mark Gurney #define NUM_MPROF_BUFFERS MPROF_BUFFERS 13400096801SJohn-Mark Gurney #else 135e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000 13600096801SJohn-Mark Gurney #endif 1376c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS]; 1386c35e809SDag-Erling Smørgrav static int first_free_mprof_buf; 13900096801SJohn-Mark Gurney #ifndef MPROF_HASH_SIZE 140e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009 14100096801SJohn-Mark Gurney #endif 14200096801SJohn-Mark Gurney #if NUM_MPROF_BUFFERS >= MPROF_HASH_SIZE 14300096801SJohn-Mark Gurney #error MPROF_BUFFERS must be larger than MPROF_HASH_SIZE 14400096801SJohn-Mark Gurney #endif 1456c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE]; 1460bd5f797SMike Makonnen /* SWAG: sbuf size = avg stat. line size * number of locks */ 1470bd5f797SMike Makonnen #define MPROF_SBUF_SIZE 256 * 400 1486c35e809SDag-Erling Smørgrav 1496c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions; 1506c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD, 1516c35e809SDag-Erling Smørgrav &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded"); 1526c35e809SDag-Erling Smørgrav static int mutex_prof_records; 1536c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD, 1546c35e809SDag-Erling Smørgrav &mutex_prof_records, 0, "Number of profiling records"); 1556c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS; 1566c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD, 1576c35e809SDag-Erling Smørgrav &mutex_prof_maxrecords, 0, "Maximum number of profiling records"); 1586c35e809SDag-Erling Smørgrav static int mutex_prof_rejected; 1596c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD, 1606c35e809SDag-Erling Smørgrav &mutex_prof_rejected, 0, "Number of rejected profiling records"); 1616c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE; 1626c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD, 1636c35e809SDag-Erling Smørgrav &mutex_prof_hashsize, 0, "Hash size"); 1646c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0; 1656c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD, 1666c35e809SDag-Erling Smørgrav &mutex_prof_collisions, 0, "Number of hash collisions"); 1676c35e809SDag-Erling Smørgrav 1686c35e809SDag-Erling Smørgrav /* 1696c35e809SDag-Erling Smørgrav * mprof_mtx protects the profiling buffers and the hash. 1706c35e809SDag-Erling Smørgrav */ 1716c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx; 172e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET); 1736c35e809SDag-Erling Smørgrav 174b784ffe9SDag-Erling Smørgrav static u_int64_t 175b784ffe9SDag-Erling Smørgrav nanoseconds(void) 176b784ffe9SDag-Erling Smørgrav { 177b784ffe9SDag-Erling Smørgrav struct timespec tv; 178b784ffe9SDag-Erling Smørgrav 179b784ffe9SDag-Erling Smørgrav nanotime(&tv); 180b784ffe9SDag-Erling Smørgrav return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec); 181b784ffe9SDag-Erling Smørgrav } 182b784ffe9SDag-Erling Smørgrav 1836c35e809SDag-Erling Smørgrav static int 1846c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 1856c35e809SDag-Erling Smørgrav { 1866c35e809SDag-Erling Smørgrav struct sbuf *sb; 1876c35e809SDag-Erling Smørgrav int error, i; 1880bd5f797SMike Makonnen static int multiplier = 1; 1896c35e809SDag-Erling Smørgrav 1906c35e809SDag-Erling Smørgrav if (first_free_mprof_buf == 0) 1916d036900SDag-Erling Smørgrav return (SYSCTL_OUT(req, "No locking recorded", 1926d036900SDag-Erling Smørgrav sizeof("No locking recorded"))); 1936c35e809SDag-Erling Smørgrav 1940bd5f797SMike Makonnen retry_sbufops: 1950bd5f797SMike Makonnen sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN); 1968dc10be8SRobert Watson sbuf_printf(sb, "%6s %12s %11s %5s %12s %12s %s\n", 1978dc10be8SRobert Watson "max", "total", "count", "avg", "cnt_hold", "cnt_lock", "name"); 1986d036900SDag-Erling Smørgrav /* 1996d036900SDag-Erling Smørgrav * XXX this spinlock seems to be by far the largest perpetrator 2006d036900SDag-Erling Smørgrav * of spinlock latency (1.6 msec on an Athlon1600 was recorded 2016d036900SDag-Erling Smørgrav * even before I pessimized it further by moving the average 2026d036900SDag-Erling Smørgrav * computation here). 2036d036900SDag-Erling Smørgrav */ 2046c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 2050bd5f797SMike Makonnen for (i = 0; i < first_free_mprof_buf; ++i) { 2068dc10be8SRobert Watson sbuf_printf(sb, "%6ju %12ju %11ju %5ju %12ju %12ju %s:%d (%s)\n", 207ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_max / 1000, 208ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / 1000, 209ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur, 210ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 : 211ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000), 2128dc10be8SRobert Watson mprof_buf[i].cnt_contest_holding, 2138dc10be8SRobert Watson mprof_buf[i].cnt_contest_locking, 2146c35e809SDag-Erling Smørgrav mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 2150bd5f797SMike Makonnen if (sbuf_overflowed(sb)) { 2160bd5f797SMike Makonnen mtx_unlock_spin(&mprof_mtx); 2170bd5f797SMike Makonnen sbuf_delete(sb); 2180bd5f797SMike Makonnen multiplier++; 2190bd5f797SMike Makonnen goto retry_sbufops; 2200bd5f797SMike Makonnen } 2210bd5f797SMike Makonnen } 2226c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 2236c35e809SDag-Erling Smørgrav sbuf_finish(sb); 2246c35e809SDag-Erling Smørgrav error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2256c35e809SDag-Erling Smørgrav sbuf_delete(sb); 2266c35e809SDag-Erling Smørgrav return (error); 2276c35e809SDag-Erling Smørgrav } 2286c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD, 2296c35e809SDag-Erling Smørgrav NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 23094ffb20dSRobert Watson 23194ffb20dSRobert Watson static int 23294ffb20dSRobert Watson reset_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 23394ffb20dSRobert Watson { 23494ffb20dSRobert Watson int error, v; 23594ffb20dSRobert Watson 23694ffb20dSRobert Watson if (first_free_mprof_buf == 0) 23794ffb20dSRobert Watson return (0); 23894ffb20dSRobert Watson 23994ffb20dSRobert Watson v = 0; 24094ffb20dSRobert Watson error = sysctl_handle_int(oidp, &v, 0, req); 24194ffb20dSRobert Watson if (error) 24294ffb20dSRobert Watson return (error); 24394ffb20dSRobert Watson if (req->newptr == NULL) 24494ffb20dSRobert Watson return (error); 24594ffb20dSRobert Watson if (v == 0) 24694ffb20dSRobert Watson return (0); 24794ffb20dSRobert Watson 24894ffb20dSRobert Watson mtx_lock_spin(&mprof_mtx); 24994ffb20dSRobert Watson bzero(mprof_buf, sizeof(*mprof_buf) * first_free_mprof_buf); 25094ffb20dSRobert Watson bzero(mprof_hash, sizeof(struct mtx *) * MPROF_HASH_SIZE); 25194ffb20dSRobert Watson first_free_mprof_buf = 0; 25294ffb20dSRobert Watson mtx_unlock_spin(&mprof_mtx); 25394ffb20dSRobert Watson return (0); 25494ffb20dSRobert Watson } 25594ffb20dSRobert Watson SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW, 25694ffb20dSRobert Watson NULL, 0, reset_mutex_prof_stats, "I", "Reset mutex profiling statistics"); 2576c35e809SDag-Erling Smørgrav #endif 2586c35e809SDag-Erling Smørgrav 2590cde2e34SJason Evans /* 2606283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 2616283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 2626283b7d0SJohn Baldwin */ 2636283b7d0SJohn Baldwin void 2646283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 2656283b7d0SJohn Baldwin { 2666283b7d0SJohn Baldwin 267dde96c99SJohn Baldwin MPASS(curthread != NULL); 2680d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 2690d975d63SJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 2700d975d63SJohn Baldwin file, line)); 2718d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 2728d768e76SJohn Baldwin file, line); 273dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 274dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 275dde96c99SJohn Baldwin line); 276dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 2776c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 2786c35e809SDag-Erling Smørgrav /* don't reset the timer when/if recursing */ 279b61860adSDag-Erling Smørgrav if (m->mtx_acqtime == 0) { 280b61860adSDag-Erling Smørgrav m->mtx_filename = file; 281b61860adSDag-Erling Smørgrav m->mtx_lineno = line; 282b61860adSDag-Erling Smørgrav m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0; 2836c35e809SDag-Erling Smørgrav ++mutex_prof_acquisitions; 2846c35e809SDag-Erling Smørgrav } 2856c35e809SDag-Erling Smørgrav #endif 2866283b7d0SJohn Baldwin } 2876283b7d0SJohn Baldwin 2886283b7d0SJohn Baldwin void 2896283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 2906283b7d0SJohn Baldwin { 2916283b7d0SJohn Baldwin 292dde96c99SJohn Baldwin MPASS(curthread != NULL); 2930d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 2940d975d63SJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 2950d975d63SJohn Baldwin file, line)); 2960d975d63SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 2970d975d63SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 2980d975d63SJohn Baldwin line); 29921377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 3006c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 301b61860adSDag-Erling Smørgrav if (m->mtx_acqtime != 0) { 3026c35e809SDag-Erling Smørgrav static const char *unknown = "(unknown)"; 3036c35e809SDag-Erling Smørgrav struct mutex_prof *mpp; 304b784ffe9SDag-Erling Smørgrav u_int64_t acqtime, now; 3056c35e809SDag-Erling Smørgrav const char *p, *q; 306e6330704SDag-Erling Smørgrav volatile u_int hash; 3076c35e809SDag-Erling Smørgrav 308b784ffe9SDag-Erling Smørgrav now = nanoseconds(); 309b61860adSDag-Erling Smørgrav acqtime = m->mtx_acqtime; 310b61860adSDag-Erling Smørgrav m->mtx_acqtime = 0; 311b784ffe9SDag-Erling Smørgrav if (now <= acqtime) 3126c35e809SDag-Erling Smørgrav goto out; 3130bd5f797SMike Makonnen for (p = m->mtx_filename; 3140bd5f797SMike Makonnen p != NULL && strncmp(p, "../", 3) == 0; p += 3) 3156c35e809SDag-Erling Smørgrav /* nothing */ ; 3166c35e809SDag-Erling Smørgrav if (p == NULL || *p == '\0') 3176c35e809SDag-Erling Smørgrav p = unknown; 318b61860adSDag-Erling Smørgrav for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q) 3196c35e809SDag-Erling Smørgrav hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 3206c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 321e6330704SDag-Erling Smørgrav for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next) 322b61860adSDag-Erling Smørgrav if (mpp->line == m->mtx_lineno && 323b61860adSDag-Erling Smørgrav strcmp(mpp->file, p) == 0) 3246c35e809SDag-Erling Smørgrav break; 3256c35e809SDag-Erling Smørgrav if (mpp == NULL) { 3266c35e809SDag-Erling Smørgrav /* Just exit if we cannot get a trace buffer */ 3276c35e809SDag-Erling Smørgrav if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 3286c35e809SDag-Erling Smørgrav ++mutex_prof_rejected; 3296c35e809SDag-Erling Smørgrav goto unlock; 3306c35e809SDag-Erling Smørgrav } 3316c35e809SDag-Erling Smørgrav mpp = &mprof_buf[first_free_mprof_buf++]; 3326c35e809SDag-Erling Smørgrav mpp->name = mtx_name(m); 3336c35e809SDag-Erling Smørgrav mpp->file = p; 334b61860adSDag-Erling Smørgrav mpp->line = m->mtx_lineno; 335e6330704SDag-Erling Smørgrav mpp->next = mprof_hash[hash]; 336e6330704SDag-Erling Smørgrav if (mprof_hash[hash] != NULL) 337e6330704SDag-Erling Smørgrav ++mutex_prof_collisions; 3386c35e809SDag-Erling Smørgrav mprof_hash[hash] = mpp; 339e6330704SDag-Erling Smørgrav ++mutex_prof_records; 3406c35e809SDag-Erling Smørgrav } 3416c35e809SDag-Erling Smørgrav /* 3426c35e809SDag-Erling Smørgrav * Record if the mutex has been held longer now than ever 3436d036900SDag-Erling Smørgrav * before. 3446c35e809SDag-Erling Smørgrav */ 345ecf031c9SDag-Erling Smørgrav if (now - acqtime > mpp->cnt_max) 346ecf031c9SDag-Erling Smørgrav mpp->cnt_max = now - acqtime; 347ecf031c9SDag-Erling Smørgrav mpp->cnt_tot += now - acqtime; 348ecf031c9SDag-Erling Smørgrav mpp->cnt_cur++; 3498dc10be8SRobert Watson /* 3508dc10be8SRobert Watson * There's a small race, really we should cmpxchg 3518dc10be8SRobert Watson * 0 with the current value, but that would bill 3528dc10be8SRobert Watson * the contention to the wrong lock instance if 3538dc10be8SRobert Watson * it followed this also. 3548dc10be8SRobert Watson */ 3558dc10be8SRobert Watson mpp->cnt_contest_holding += m->mtx_contest_holding; 3568dc10be8SRobert Watson m->mtx_contest_holding = 0; 3578dc10be8SRobert Watson mpp->cnt_contest_locking += m->mtx_contest_locking; 3588dc10be8SRobert Watson m->mtx_contest_locking = 0; 3596c35e809SDag-Erling Smørgrav unlock: 3606c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 3616c35e809SDag-Erling Smørgrav } 3626c35e809SDag-Erling Smørgrav out: 3636c35e809SDag-Erling Smørgrav #endif 364dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 3656283b7d0SJohn Baldwin } 3666283b7d0SJohn Baldwin 3676283b7d0SJohn Baldwin void 3686283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 3696283b7d0SJohn Baldwin { 3706283b7d0SJohn Baldwin 371dde96c99SJohn Baldwin MPASS(curthread != NULL); 3720d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 3730d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 3740d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 3758d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 3768d768e76SJohn Baldwin file, line); 377dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 378dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 379dde96c99SJohn Baldwin line); 380dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3816283b7d0SJohn Baldwin } 3826283b7d0SJohn Baldwin 3836283b7d0SJohn Baldwin void 3846283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 3856283b7d0SJohn Baldwin { 3866283b7d0SJohn Baldwin 387dde96c99SJohn Baldwin MPASS(curthread != NULL); 3880d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 3890d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 3900d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 391dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 392dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 393dde96c99SJohn Baldwin line); 3940d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 395dde96c99SJohn Baldwin _rel_spin_lock(m); 3966283b7d0SJohn Baldwin } 3976283b7d0SJohn Baldwin 3986283b7d0SJohn Baldwin /* 3999ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 400eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 401eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 4020cde2e34SJason Evans */ 4030cde2e34SJason Evans int 4049ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 4050cde2e34SJason Evans { 4060cde2e34SJason Evans int rval; 4070cde2e34SJason Evans 408b40ce416SJulian Elischer MPASS(curthread != NULL); 4099ed346baSBosko Milekic 410eac09796SJohn Baldwin if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) { 411eac09796SJohn Baldwin m->mtx_recurse++; 412eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 413eac09796SJohn Baldwin rval = 1; 414eac09796SJohn Baldwin } else 415b40ce416SJulian Elischer rval = _obtain_lock(m, curthread); 4169ed346baSBosko Milekic 41719284646SJohn Baldwin LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 4186b869595SJohn Baldwin if (rval) 4192d96f0b1SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4202d96f0b1SJohn Baldwin file, line); 4219ed346baSBosko Milekic 42219284646SJohn Baldwin return (rval); 4230cde2e34SJason Evans } 4240cde2e34SJason Evans 4250cde2e34SJason Evans /* 4269ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4279ed346baSBosko Milekic * 4289ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4299ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4300cde2e34SJason Evans */ 4310cde2e34SJason Evans void 432bdcfcf5bSJohn Baldwin _mtx_lock_sleep(struct mtx *m, struct thread *td, int opts, const char *file, 433bdcfcf5bSJohn Baldwin int line) 43436412d79SJohn Baldwin { 435701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 4362498cf8cSJohn Baldwin struct thread *owner; 4372498cf8cSJohn Baldwin #endif 4385fa8dd90SJohn Baldwin uintptr_t v; 43902bd1bcdSIan Dowse #ifdef KTR 44002bd1bcdSIan Dowse int cont_logged = 0; 44102bd1bcdSIan Dowse #endif 4428dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4438dc10be8SRobert Watson int contested; 4448dc10be8SRobert Watson #endif 44536412d79SJohn Baldwin 4465fa8dd90SJohn Baldwin if (mtx_owned(m)) { 447eac09796SJohn Baldwin KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0, 448eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 449eac09796SJohn Baldwin m->mtx_object.lo_name, file, line)); 45036412d79SJohn Baldwin m->mtx_recurse++; 45108812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 45219284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4535746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 45436412d79SJohn Baldwin return; 45536412d79SJohn Baldwin } 4569ed346baSBosko Milekic 45719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 45815ec816aSJohn Baldwin CTR4(KTR_LOCK, 45915ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 46019284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 4611bd0eefbSJohn Baldwin 4628dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4638dc10be8SRobert Watson contested = 0; 4648dc10be8SRobert Watson #endif 465b40ce416SJulian Elischer while (!_obtain_lock(m, td)) { 4668dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4678dc10be8SRobert Watson contested = 1; 4688dc10be8SRobert Watson atomic_add_int(&m->mtx_contest_holding, 1); 4698dc10be8SRobert Watson #endif 4702ff0e645SJohn Baldwin turnstile_lock(&m->mtx_object); 4715fa8dd90SJohn Baldwin v = m->mtx_lock; 4725fa8dd90SJohn Baldwin 47336412d79SJohn Baldwin /* 4749ed346baSBosko Milekic * Check if the lock has been released while spinning for 475961a7b24SJohn Baldwin * the turnstile chain lock. 47636412d79SJohn Baldwin */ 4775fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 478961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 4799f1b87f1SMaxime Henrion cpu_spinwait(); 48036412d79SJohn Baldwin continue; 48136412d79SJohn Baldwin } 4829ed346baSBosko Milekic 483535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 484535eb309SJohn Baldwin MPASS(v != MTX_CONTESTED); 485535eb309SJohn Baldwin #else 48636412d79SJohn Baldwin /* 4879ed346baSBosko Milekic * The mutex was marked contested on release. This means that 488f7ee1590SJohn Baldwin * there are other threads blocked on it. Grab ownership of 489f7ee1590SJohn Baldwin * it and propagate its priority to the current thread if 490f7ee1590SJohn Baldwin * necessary. 49136412d79SJohn Baldwin */ 49236412d79SJohn Baldwin if (v == MTX_CONTESTED) { 493b40ce416SJulian Elischer m->mtx_lock = (uintptr_t)td | MTX_CONTESTED; 4942ff0e645SJohn Baldwin turnstile_claim(&m->mtx_object); 4958dc10be8SRobert Watson break; 49636412d79SJohn Baldwin } 497535eb309SJohn Baldwin #endif 4989ed346baSBosko Milekic 49936412d79SJohn Baldwin /* 5009ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 5019ed346baSBosko Milekic * setting the contested bit, the mutex was either released 5029ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 50336412d79SJohn Baldwin */ 50436412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 50536412d79SJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, (void *)v, 50636412d79SJohn Baldwin (void *)(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", 54002bd1bcdSIan Dowse td, 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", 55702bd1bcdSIan Dowse m->mtx_object.lo_name, td, 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 576bdcfcf5bSJohn Baldwin _mtx_lock_spin(struct mtx *m, struct thread *td, 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 (;;) { 585bdcfcf5bSJohn Baldwin if (_obtain_lock(m, td)) 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 7325cb0fbe4SJohn Baldwin if (panicstr != NULL) 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