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" 41535eb309SJohn Baldwin #include "opt_mutex_wake_all.h" 42a5a96a19SJohn Baldwin 430384fff8SJason Evans #include <sys/param.h> 446c35e809SDag-Erling Smørgrav #include <sys/systm.h> 4536412d79SJohn Baldwin #include <sys/bus.h> 462d50560aSMarcel Moolenaar #include <sys/kdb.h> 4736412d79SJohn Baldwin #include <sys/kernel.h> 486c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 4919284646SJohn Baldwin #include <sys/lock.h> 50fb919e4dSMark Murray #include <sys/malloc.h> 5119284646SJohn Baldwin #include <sys/mutex.h> 520384fff8SJason Evans #include <sys/proc.h> 53c4f7a187SJohn Baldwin #include <sys/resourcevar.h> 54b43179fbSJeff Roberson #include <sys/sched.h> 556c35e809SDag-Erling Smørgrav #include <sys/sbuf.h> 56a5a96a19SJohn Baldwin #include <sys/sysctl.h> 57961a7b24SJohn Baldwin #include <sys/turnstile.h> 5836412d79SJohn Baldwin #include <sys/vmmeter.h> 590384fff8SJason Evans 6036412d79SJohn Baldwin #include <machine/atomic.h> 6136412d79SJohn Baldwin #include <machine/bus.h> 6236412d79SJohn Baldwin #include <machine/clock.h> 630384fff8SJason Evans #include <machine/cpu.h> 6436412d79SJohn Baldwin 659c36c934SJohn Baldwin #include <ddb/ddb.h> 669c36c934SJohn Baldwin 6736412d79SJohn Baldwin #include <vm/vm.h> 6836412d79SJohn Baldwin #include <vm/vm_extern.h> 6936412d79SJohn Baldwin 700cde2e34SJason Evans /* 719ed346baSBosko Milekic * Internal utility macros. 720cde2e34SJason Evans */ 739ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 740cde2e34SJason Evans 759ed346baSBosko Milekic #define mtx_owner(m) (mtx_unowned((m)) ? NULL \ 76b40ce416SJulian Elischer : (struct thread *)((m)->mtx_lock & MTX_FLAGMASK)) 779ed346baSBosko Milekic 780cde2e34SJason Evans /* 7919284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 800cde2e34SJason Evans */ 8119284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 8219284646SJohn Baldwin "sleep mutex", 8319284646SJohn Baldwin LC_SLEEPLOCK | LC_RECURSABLE 8419284646SJohn Baldwin }; 8519284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 8619284646SJohn Baldwin "spin mutex", 8719284646SJohn Baldwin LC_SPINLOCK | LC_RECURSABLE 888484de75SJohn Baldwin }; 898484de75SJohn Baldwin 909ed346baSBosko Milekic /* 91c53c013bSJohn Baldwin * System-wide mutexes 92c53c013bSJohn Baldwin */ 93c53c013bSJohn Baldwin struct mtx sched_lock; 94c53c013bSJohn Baldwin struct mtx Giant; 95c53c013bSJohn Baldwin 966c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 976c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging"); 986c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling"); 996c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0; 1006c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW, 1016c35e809SDag-Erling Smørgrav &mutex_prof_enable, 0, "Enable tracing of mutex holdtime"); 1026c35e809SDag-Erling Smørgrav 1036c35e809SDag-Erling Smørgrav struct mutex_prof { 1046c35e809SDag-Erling Smørgrav const char *name; 1056c35e809SDag-Erling Smørgrav const char *file; 1066c35e809SDag-Erling Smørgrav int line; 107ecf031c9SDag-Erling Smørgrav uintmax_t cnt_max; 108ecf031c9SDag-Erling Smørgrav uintmax_t cnt_tot; 109ecf031c9SDag-Erling Smørgrav uintmax_t cnt_cur; 1108dc10be8SRobert Watson uintmax_t cnt_contest_holding; 1118dc10be8SRobert Watson uintmax_t cnt_contest_locking; 112e6330704SDag-Erling Smørgrav struct mutex_prof *next; 1136c35e809SDag-Erling Smørgrav }; 1146c35e809SDag-Erling Smørgrav 1156c35e809SDag-Erling Smørgrav /* 1166c35e809SDag-Erling Smørgrav * mprof_buf is a static pool of profiling records to avoid possible 1176c35e809SDag-Erling Smørgrav * reentrance of the memory allocation functions. 1186c35e809SDag-Erling Smørgrav * 1196c35e809SDag-Erling Smørgrav * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE. 1206c35e809SDag-Erling Smørgrav */ 121e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000 1226c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS]; 1236c35e809SDag-Erling Smørgrav static int first_free_mprof_buf; 124e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009 1256c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE]; 1260bd5f797SMike Makonnen /* SWAG: sbuf size = avg stat. line size * number of locks */ 1270bd5f797SMike Makonnen #define MPROF_SBUF_SIZE 256 * 400 1286c35e809SDag-Erling Smørgrav 1296c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions; 1306c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD, 1316c35e809SDag-Erling Smørgrav &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded"); 1326c35e809SDag-Erling Smørgrav static int mutex_prof_records; 1336c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD, 1346c35e809SDag-Erling Smørgrav &mutex_prof_records, 0, "Number of profiling records"); 1356c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS; 1366c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD, 1376c35e809SDag-Erling Smørgrav &mutex_prof_maxrecords, 0, "Maximum number of profiling records"); 1386c35e809SDag-Erling Smørgrav static int mutex_prof_rejected; 1396c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD, 1406c35e809SDag-Erling Smørgrav &mutex_prof_rejected, 0, "Number of rejected profiling records"); 1416c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE; 1426c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD, 1436c35e809SDag-Erling Smørgrav &mutex_prof_hashsize, 0, "Hash size"); 1446c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0; 1456c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD, 1466c35e809SDag-Erling Smørgrav &mutex_prof_collisions, 0, "Number of hash collisions"); 1476c35e809SDag-Erling Smørgrav 1486c35e809SDag-Erling Smørgrav /* 1496c35e809SDag-Erling Smørgrav * mprof_mtx protects the profiling buffers and the hash. 1506c35e809SDag-Erling Smørgrav */ 1516c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx; 152e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET); 1536c35e809SDag-Erling Smørgrav 154b784ffe9SDag-Erling Smørgrav static u_int64_t 155b784ffe9SDag-Erling Smørgrav nanoseconds(void) 156b784ffe9SDag-Erling Smørgrav { 157b784ffe9SDag-Erling Smørgrav struct timespec tv; 158b784ffe9SDag-Erling Smørgrav 159b784ffe9SDag-Erling Smørgrav nanotime(&tv); 160b784ffe9SDag-Erling Smørgrav return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec); 161b784ffe9SDag-Erling Smørgrav } 162b784ffe9SDag-Erling Smørgrav 1636c35e809SDag-Erling Smørgrav static int 1646c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 1656c35e809SDag-Erling Smørgrav { 1666c35e809SDag-Erling Smørgrav struct sbuf *sb; 1676c35e809SDag-Erling Smørgrav int error, i; 1680bd5f797SMike Makonnen static int multiplier = 1; 1696c35e809SDag-Erling Smørgrav 1706c35e809SDag-Erling Smørgrav if (first_free_mprof_buf == 0) 1716d036900SDag-Erling Smørgrav return (SYSCTL_OUT(req, "No locking recorded", 1726d036900SDag-Erling Smørgrav sizeof("No locking recorded"))); 1736c35e809SDag-Erling Smørgrav 1740bd5f797SMike Makonnen retry_sbufops: 1750bd5f797SMike Makonnen sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN); 1768dc10be8SRobert Watson sbuf_printf(sb, "%6s %12s %11s %5s %12s %12s %s\n", 1778dc10be8SRobert Watson "max", "total", "count", "avg", "cnt_hold", "cnt_lock", "name"); 1786d036900SDag-Erling Smørgrav /* 1796d036900SDag-Erling Smørgrav * XXX this spinlock seems to be by far the largest perpetrator 1806d036900SDag-Erling Smørgrav * of spinlock latency (1.6 msec on an Athlon1600 was recorded 1816d036900SDag-Erling Smørgrav * even before I pessimized it further by moving the average 1826d036900SDag-Erling Smørgrav * computation here). 1836d036900SDag-Erling Smørgrav */ 1846c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 1850bd5f797SMike Makonnen for (i = 0; i < first_free_mprof_buf; ++i) { 1868dc10be8SRobert Watson sbuf_printf(sb, "%6ju %12ju %11ju %5ju %12ju %12ju %s:%d (%s)\n", 187ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_max / 1000, 188ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / 1000, 189ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur, 190ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 : 191ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000), 1928dc10be8SRobert Watson mprof_buf[i].cnt_contest_holding, 1938dc10be8SRobert Watson mprof_buf[i].cnt_contest_locking, 1946c35e809SDag-Erling Smørgrav mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 1950bd5f797SMike Makonnen if (sbuf_overflowed(sb)) { 1960bd5f797SMike Makonnen mtx_unlock_spin(&mprof_mtx); 1970bd5f797SMike Makonnen sbuf_delete(sb); 1980bd5f797SMike Makonnen multiplier++; 1990bd5f797SMike Makonnen goto retry_sbufops; 2000bd5f797SMike Makonnen } 2010bd5f797SMike Makonnen } 2026c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 2036c35e809SDag-Erling Smørgrav sbuf_finish(sb); 2046c35e809SDag-Erling Smørgrav error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2056c35e809SDag-Erling Smørgrav sbuf_delete(sb); 2066c35e809SDag-Erling Smørgrav return (error); 2076c35e809SDag-Erling Smørgrav } 2086c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD, 2096c35e809SDag-Erling Smørgrav NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 21094ffb20dSRobert Watson 21194ffb20dSRobert Watson static int 21294ffb20dSRobert Watson reset_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 21394ffb20dSRobert Watson { 21494ffb20dSRobert Watson int error, v; 21594ffb20dSRobert Watson 21694ffb20dSRobert Watson if (first_free_mprof_buf == 0) 21794ffb20dSRobert Watson return (0); 21894ffb20dSRobert Watson 21994ffb20dSRobert Watson v = 0; 22094ffb20dSRobert Watson error = sysctl_handle_int(oidp, &v, 0, req); 22194ffb20dSRobert Watson if (error) 22294ffb20dSRobert Watson return (error); 22394ffb20dSRobert Watson if (req->newptr == NULL) 22494ffb20dSRobert Watson return (error); 22594ffb20dSRobert Watson if (v == 0) 22694ffb20dSRobert Watson return (0); 22794ffb20dSRobert Watson 22894ffb20dSRobert Watson mtx_lock_spin(&mprof_mtx); 22994ffb20dSRobert Watson bzero(mprof_buf, sizeof(*mprof_buf) * first_free_mprof_buf); 23094ffb20dSRobert Watson bzero(mprof_hash, sizeof(struct mtx *) * MPROF_HASH_SIZE); 23194ffb20dSRobert Watson first_free_mprof_buf = 0; 23294ffb20dSRobert Watson mtx_unlock_spin(&mprof_mtx); 23394ffb20dSRobert Watson return (0); 23494ffb20dSRobert Watson } 23594ffb20dSRobert Watson SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW, 23694ffb20dSRobert Watson NULL, 0, reset_mutex_prof_stats, "I", "Reset mutex profiling statistics"); 2376c35e809SDag-Erling Smørgrav #endif 2386c35e809SDag-Erling Smørgrav 2390cde2e34SJason Evans /* 2406283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 2416283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 2426283b7d0SJohn Baldwin */ 2436283b7d0SJohn Baldwin void 2446283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 2456283b7d0SJohn Baldwin { 2466283b7d0SJohn Baldwin 247dde96c99SJohn Baldwin MPASS(curthread != NULL); 2480d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 2490d975d63SJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 2500d975d63SJohn Baldwin file, line)); 2518d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 2528d768e76SJohn Baldwin file, line); 253dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 254dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 255dde96c99SJohn Baldwin line); 256dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 2576c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 2586c35e809SDag-Erling Smørgrav /* don't reset the timer when/if recursing */ 259b61860adSDag-Erling Smørgrav if (m->mtx_acqtime == 0) { 260b61860adSDag-Erling Smørgrav m->mtx_filename = file; 261b61860adSDag-Erling Smørgrav m->mtx_lineno = line; 262b61860adSDag-Erling Smørgrav m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0; 2636c35e809SDag-Erling Smørgrav ++mutex_prof_acquisitions; 2646c35e809SDag-Erling Smørgrav } 2656c35e809SDag-Erling Smørgrav #endif 2666283b7d0SJohn Baldwin } 2676283b7d0SJohn Baldwin 2686283b7d0SJohn Baldwin void 2696283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 2706283b7d0SJohn Baldwin { 2716283b7d0SJohn Baldwin 272dde96c99SJohn Baldwin MPASS(curthread != NULL); 2730d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 2740d975d63SJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 2750d975d63SJohn Baldwin file, line)); 2760d975d63SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 2770d975d63SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 2780d975d63SJohn Baldwin line); 27921377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 2806c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 281b61860adSDag-Erling Smørgrav if (m->mtx_acqtime != 0) { 2826c35e809SDag-Erling Smørgrav static const char *unknown = "(unknown)"; 2836c35e809SDag-Erling Smørgrav struct mutex_prof *mpp; 284b784ffe9SDag-Erling Smørgrav u_int64_t acqtime, now; 2856c35e809SDag-Erling Smørgrav const char *p, *q; 286e6330704SDag-Erling Smørgrav volatile u_int hash; 2876c35e809SDag-Erling Smørgrav 288b784ffe9SDag-Erling Smørgrav now = nanoseconds(); 289b61860adSDag-Erling Smørgrav acqtime = m->mtx_acqtime; 290b61860adSDag-Erling Smørgrav m->mtx_acqtime = 0; 291b784ffe9SDag-Erling Smørgrav if (now <= acqtime) 2926c35e809SDag-Erling Smørgrav goto out; 2930bd5f797SMike Makonnen for (p = m->mtx_filename; 2940bd5f797SMike Makonnen p != NULL && strncmp(p, "../", 3) == 0; p += 3) 2956c35e809SDag-Erling Smørgrav /* nothing */ ; 2966c35e809SDag-Erling Smørgrav if (p == NULL || *p == '\0') 2976c35e809SDag-Erling Smørgrav p = unknown; 298b61860adSDag-Erling Smørgrav for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q) 2996c35e809SDag-Erling Smørgrav hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 3006c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 301e6330704SDag-Erling Smørgrav for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next) 302b61860adSDag-Erling Smørgrav if (mpp->line == m->mtx_lineno && 303b61860adSDag-Erling Smørgrav strcmp(mpp->file, p) == 0) 3046c35e809SDag-Erling Smørgrav break; 3056c35e809SDag-Erling Smørgrav if (mpp == NULL) { 3066c35e809SDag-Erling Smørgrav /* Just exit if we cannot get a trace buffer */ 3076c35e809SDag-Erling Smørgrav if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 3086c35e809SDag-Erling Smørgrav ++mutex_prof_rejected; 3096c35e809SDag-Erling Smørgrav goto unlock; 3106c35e809SDag-Erling Smørgrav } 3116c35e809SDag-Erling Smørgrav mpp = &mprof_buf[first_free_mprof_buf++]; 3126c35e809SDag-Erling Smørgrav mpp->name = mtx_name(m); 3136c35e809SDag-Erling Smørgrav mpp->file = p; 314b61860adSDag-Erling Smørgrav mpp->line = m->mtx_lineno; 315e6330704SDag-Erling Smørgrav mpp->next = mprof_hash[hash]; 316e6330704SDag-Erling Smørgrav if (mprof_hash[hash] != NULL) 317e6330704SDag-Erling Smørgrav ++mutex_prof_collisions; 3186c35e809SDag-Erling Smørgrav mprof_hash[hash] = mpp; 319e6330704SDag-Erling Smørgrav ++mutex_prof_records; 3206c35e809SDag-Erling Smørgrav } 3216c35e809SDag-Erling Smørgrav /* 3226c35e809SDag-Erling Smørgrav * Record if the mutex has been held longer now than ever 3236d036900SDag-Erling Smørgrav * before. 3246c35e809SDag-Erling Smørgrav */ 325ecf031c9SDag-Erling Smørgrav if (now - acqtime > mpp->cnt_max) 326ecf031c9SDag-Erling Smørgrav mpp->cnt_max = now - acqtime; 327ecf031c9SDag-Erling Smørgrav mpp->cnt_tot += now - acqtime; 328ecf031c9SDag-Erling Smørgrav mpp->cnt_cur++; 3298dc10be8SRobert Watson /* 3308dc10be8SRobert Watson * There's a small race, really we should cmpxchg 3318dc10be8SRobert Watson * 0 with the current value, but that would bill 3328dc10be8SRobert Watson * the contention to the wrong lock instance if 3338dc10be8SRobert Watson * it followed this also. 3348dc10be8SRobert Watson */ 3358dc10be8SRobert Watson mpp->cnt_contest_holding += m->mtx_contest_holding; 3368dc10be8SRobert Watson m->mtx_contest_holding = 0; 3378dc10be8SRobert Watson mpp->cnt_contest_locking += m->mtx_contest_locking; 3388dc10be8SRobert Watson m->mtx_contest_locking = 0; 3396c35e809SDag-Erling Smørgrav unlock: 3406c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 3416c35e809SDag-Erling Smørgrav } 3426c35e809SDag-Erling Smørgrav out: 3436c35e809SDag-Erling Smørgrav #endif 344dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 3456283b7d0SJohn Baldwin } 3466283b7d0SJohn Baldwin 3476283b7d0SJohn Baldwin void 3486283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 3496283b7d0SJohn Baldwin { 3506283b7d0SJohn Baldwin 351dde96c99SJohn Baldwin MPASS(curthread != NULL); 3520d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 3530d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 3540d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 3558d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 3568d768e76SJohn Baldwin file, line); 357ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1 358dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 359e8fdcfb5SJohn Baldwin #else 360e8fdcfb5SJohn Baldwin critical_enter(); 361e8fdcfb5SJohn Baldwin #endif 362dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 363dde96c99SJohn Baldwin line); 364dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3656283b7d0SJohn Baldwin } 3666283b7d0SJohn Baldwin 3676283b7d0SJohn Baldwin void 3686283b7d0SJohn Baldwin _mtx_unlock_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_unlock_spin() of sleep mutex %s @ %s:%d", 3740d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 375dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 376dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 377dde96c99SJohn Baldwin line); 3780d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 379ce39e722SJohn Baldwin #if defined(SMP) || LOCK_DEBUG > 0 || 1 380dde96c99SJohn Baldwin _rel_spin_lock(m); 381e8fdcfb5SJohn Baldwin #else 382e8fdcfb5SJohn Baldwin critical_exit(); 383e8fdcfb5SJohn Baldwin #endif 3846283b7d0SJohn Baldwin } 3856283b7d0SJohn Baldwin 3866283b7d0SJohn Baldwin /* 3879ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 388eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 389eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 3900cde2e34SJason Evans */ 3910cde2e34SJason Evans int 3929ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 3930cde2e34SJason Evans { 3940cde2e34SJason Evans int rval; 3950cde2e34SJason Evans 396b40ce416SJulian Elischer MPASS(curthread != NULL); 3979ed346baSBosko Milekic 398eac09796SJohn Baldwin if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) { 399eac09796SJohn Baldwin m->mtx_recurse++; 400eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 401eac09796SJohn Baldwin rval = 1; 402eac09796SJohn Baldwin } else 403b40ce416SJulian Elischer rval = _obtain_lock(m, curthread); 4049ed346baSBosko Milekic 40519284646SJohn Baldwin LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 4066b869595SJohn Baldwin if (rval) 4072d96f0b1SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4082d96f0b1SJohn Baldwin file, line); 4099ed346baSBosko Milekic 41019284646SJohn Baldwin return (rval); 4110cde2e34SJason Evans } 4120cde2e34SJason Evans 4130cde2e34SJason Evans /* 4149ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4159ed346baSBosko Milekic * 4169ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4179ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4180cde2e34SJason Evans */ 4190cde2e34SJason Evans void 4209ed346baSBosko Milekic _mtx_lock_sleep(struct mtx *m, int opts, const char *file, int line) 42136412d79SJohn Baldwin { 422961a7b24SJohn Baldwin struct turnstile *ts; 423b40ce416SJulian Elischer struct thread *td = curthread; 4242498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES) 4252498cf8cSJohn Baldwin struct thread *owner; 4262498cf8cSJohn Baldwin #endif 4275fa8dd90SJohn Baldwin uintptr_t v; 42802bd1bcdSIan Dowse #ifdef KTR 42902bd1bcdSIan Dowse int cont_logged = 0; 43002bd1bcdSIan Dowse #endif 4318dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4328dc10be8SRobert Watson int contested; 4338dc10be8SRobert Watson #endif 43436412d79SJohn Baldwin 4355fa8dd90SJohn Baldwin if (mtx_owned(m)) { 436eac09796SJohn Baldwin KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0, 437eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 438eac09796SJohn Baldwin m->mtx_object.lo_name, file, line)); 43936412d79SJohn Baldwin m->mtx_recurse++; 44008812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 44119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4425746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 44336412d79SJohn Baldwin return; 44436412d79SJohn Baldwin } 4459ed346baSBosko Milekic 44619284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 44715ec816aSJohn Baldwin CTR4(KTR_LOCK, 44815ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 44919284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 4501bd0eefbSJohn Baldwin 4518dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4528dc10be8SRobert Watson contested = 0; 4538dc10be8SRobert Watson #endif 454b40ce416SJulian Elischer while (!_obtain_lock(m, td)) { 4558dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4568dc10be8SRobert Watson contested = 1; 4578dc10be8SRobert Watson atomic_add_int(&m->mtx_contest_holding, 1); 4588dc10be8SRobert Watson #endif 459961a7b24SJohn Baldwin ts = turnstile_lookup(&m->mtx_object); 4605fa8dd90SJohn Baldwin v = m->mtx_lock; 4615fa8dd90SJohn Baldwin 46236412d79SJohn Baldwin /* 4639ed346baSBosko Milekic * Check if the lock has been released while spinning for 464961a7b24SJohn Baldwin * the turnstile chain lock. 46536412d79SJohn Baldwin */ 4665fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 467961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 468703fc290SJohn Baldwin #ifdef __i386__ 4696b8c6989SJohn Baldwin ia32_pause(); 470703fc290SJohn Baldwin #endif 47136412d79SJohn Baldwin continue; 47236412d79SJohn Baldwin } 4739ed346baSBosko Milekic 474535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 475535eb309SJohn Baldwin MPASS(v != MTX_CONTESTED); 476535eb309SJohn Baldwin #else 47736412d79SJohn Baldwin /* 4789ed346baSBosko Milekic * The mutex was marked contested on release. This means that 479f7ee1590SJohn Baldwin * there are other threads blocked on it. Grab ownership of 480f7ee1590SJohn Baldwin * it and propagate its priority to the current thread if 481f7ee1590SJohn Baldwin * necessary. 48236412d79SJohn Baldwin */ 48336412d79SJohn Baldwin if (v == MTX_CONTESTED) { 484961a7b24SJohn Baldwin MPASS(ts != NULL); 485b40ce416SJulian Elischer m->mtx_lock = (uintptr_t)td | MTX_CONTESTED; 486961a7b24SJohn Baldwin turnstile_claim(ts); 4878dc10be8SRobert Watson break; 48836412d79SJohn Baldwin } 489535eb309SJohn Baldwin #endif 4909ed346baSBosko Milekic 49136412d79SJohn Baldwin /* 4929ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 4939ed346baSBosko Milekic * setting the contested bit, the mutex was either released 4949ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 49536412d79SJohn Baldwin */ 49636412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 49736412d79SJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, (void *)v, 49836412d79SJohn Baldwin (void *)(v | MTX_CONTESTED))) { 499961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 500703fc290SJohn Baldwin #ifdef __i386__ 5016b8c6989SJohn Baldwin ia32_pause(); 502703fc290SJohn Baldwin #endif 50336412d79SJohn Baldwin continue; 50436412d79SJohn Baldwin } 50536412d79SJohn Baldwin 5062498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES) 5072498cf8cSJohn Baldwin /* 5082498cf8cSJohn Baldwin * If the current owner of the lock is executing on another 5092498cf8cSJohn Baldwin * CPU, spin instead of blocking. 5102498cf8cSJohn Baldwin */ 5112498cf8cSJohn Baldwin owner = (struct thread *)(v & MTX_FLAGMASK); 51227dad03cSJohn Baldwin if (m != &Giant && TD_IS_RUNNING(owner)) { 513961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 51427dad03cSJohn Baldwin while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) { 515703fc290SJohn Baldwin #ifdef __i386__ 5166b8c6989SJohn Baldwin ia32_pause(); 517703fc290SJohn Baldwin #endif 5187fcca609SJohn Baldwin } 5192498cf8cSJohn Baldwin continue; 5202498cf8cSJohn Baldwin } 5212498cf8cSJohn Baldwin #endif /* SMP && ADAPTIVE_MUTEXES */ 5222498cf8cSJohn Baldwin 5239ed346baSBosko Milekic /* 5247feefcd6SJohn Baldwin * We definitely must sleep for this lock. 5259ed346baSBosko Milekic */ 52636412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 52736412d79SJohn Baldwin 52802bd1bcdSIan Dowse #ifdef KTR 52902bd1bcdSIan Dowse if (!cont_logged) { 53002bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 53102bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 53202bd1bcdSIan Dowse td, file, line, m->mtx_object.lo_name, 53302bd1bcdSIan Dowse WITNESS_FILE(&m->mtx_object), 53402bd1bcdSIan Dowse WITNESS_LINE(&m->mtx_object)); 53502bd1bcdSIan Dowse cont_logged = 1; 53602bd1bcdSIan Dowse } 53702bd1bcdSIan Dowse #endif 53836412d79SJohn Baldwin 5399ed346baSBosko Milekic /* 540961a7b24SJohn Baldwin * Block on the turnstile. 5419ed346baSBosko Milekic */ 542961a7b24SJohn Baldwin turnstile_wait(ts, &m->mtx_object, mtx_owner(m)); 54336412d79SJohn Baldwin } 5449ed346baSBosko Milekic 54502bd1bcdSIan Dowse #ifdef KTR 54602bd1bcdSIan Dowse if (cont_logged) { 54702bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 54802bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 54902bd1bcdSIan Dowse m->mtx_object.lo_name, td, file, line); 55002bd1bcdSIan Dowse } 55102bd1bcdSIan Dowse #endif 5528dc10be8SRobert Watson #ifdef MUTEX_PROFILING 5538dc10be8SRobert Watson if (contested) 5548dc10be8SRobert Watson m->mtx_contest_locking++; 5558dc10be8SRobert Watson m->mtx_contest_holding = 0; 5568dc10be8SRobert Watson #endif 55736412d79SJohn Baldwin return; 5589ed346baSBosko Milekic } 5599ed346baSBosko Milekic 5609ed346baSBosko Milekic /* 5619ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 5629ed346baSBosko Milekic * 5639ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 5649ed346baSBosko Milekic * is handled inline. 5659ed346baSBosko Milekic */ 5669ed346baSBosko Milekic void 5677e1f6dfeSJohn Baldwin _mtx_lock_spin(struct mtx *m, int opts, const char *file, int line) 56836412d79SJohn Baldwin { 56936412d79SJohn Baldwin int i = 0; 57036412d79SJohn Baldwin 57119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 5725746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 5739ed346baSBosko Milekic 57436412d79SJohn Baldwin for (;;) { 575b40ce416SJulian Elischer if (_obtain_lock(m, curthread)) 57636412d79SJohn Baldwin break; 5779ed346baSBosko Milekic 5787141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 5797e1f6dfeSJohn Baldwin critical_exit(); 58036412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 581703fc290SJohn Baldwin if (i++ < 10000000) { 582703fc290SJohn Baldwin #ifdef __i386__ 5836b8c6989SJohn Baldwin ia32_pause(); 584703fc290SJohn Baldwin #endif 58536412d79SJohn Baldwin continue; 586703fc290SJohn Baldwin } 5870e54ddadSJohn Baldwin if (i < 60000000) 58836412d79SJohn Baldwin DELAY(1); 5892d50560aSMarcel Moolenaar else if (!kdb_active) { 59041109518SJohn Baldwin printf("spin lock %s held by %p for > 5 seconds\n", 59119284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock); 59241109518SJohn Baldwin #ifdef WITNESS 59341109518SJohn Baldwin witness_display_spinlock(&m->mtx_object, 59441109518SJohn Baldwin mtx_owner(m)); 59541109518SJohn Baldwin #endif 59641109518SJohn Baldwin panic("spin lock held too long"); 59741109518SJohn Baldwin } 598703fc290SJohn Baldwin #ifdef __i386__ 5996b8c6989SJohn Baldwin ia32_pause(); 600703fc290SJohn Baldwin #endif 60136412d79SJohn Baldwin } 6027e1f6dfeSJohn Baldwin critical_enter(); 60336412d79SJohn Baldwin } 60436412d79SJohn Baldwin 60519284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6069ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 6079ed346baSBosko Milekic 60836412d79SJohn Baldwin return; 60936412d79SJohn Baldwin } 61036412d79SJohn Baldwin 6119ed346baSBosko Milekic /* 6129ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 6139ed346baSBosko Milekic * 6149ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 6159ed346baSBosko Milekic * need to wake up a blocked thread). 6169ed346baSBosko Milekic */ 61736412d79SJohn Baldwin void 6189ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 61936412d79SJohn Baldwin { 620961a7b24SJohn Baldwin struct turnstile *ts; 6210c0b25aeSJohn Baldwin #ifndef PREEMPTION 622b40ce416SJulian Elischer struct thread *td, *td1; 6230c0b25aeSJohn Baldwin #endif 6249ed346baSBosko Milekic 62508812b39SBosko Milekic if (mtx_recursed(m)) { 62636412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 62708812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 62819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6299ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 63036412d79SJohn Baldwin return; 63136412d79SJohn Baldwin } 6329ed346baSBosko Milekic 633961a7b24SJohn Baldwin ts = turnstile_lookup(&m->mtx_object); 63419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6359ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 6369ed346baSBosko Milekic 6372498cf8cSJohn Baldwin #if defined(SMP) && defined(ADAPTIVE_MUTEXES) 638961a7b24SJohn Baldwin if (ts == NULL) { 6392498cf8cSJohn Baldwin _release_lock_quick(m); 6402498cf8cSJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6412498cf8cSJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m); 642961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 6432498cf8cSJohn Baldwin return; 6442498cf8cSJohn Baldwin } 645961a7b24SJohn Baldwin #else 646961a7b24SJohn Baldwin MPASS(ts != NULL); 6472498cf8cSJohn Baldwin #endif 6480c0b25aeSJohn Baldwin #ifndef PREEMPTION 649961a7b24SJohn Baldwin /* XXX */ 650961a7b24SJohn Baldwin td1 = turnstile_head(ts); 6510c0b25aeSJohn Baldwin #endif 652535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 653535eb309SJohn Baldwin turnstile_broadcast(ts); 654535eb309SJohn Baldwin _release_lock_quick(m); 655535eb309SJohn Baldwin #else 656961a7b24SJohn Baldwin if (turnstile_signal(ts)) { 65736412d79SJohn Baldwin _release_lock_quick(m); 65819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6599ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 660961a7b24SJohn Baldwin } else { 661f7ee1590SJohn Baldwin m->mtx_lock = MTX_CONTESTED; 66219284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 663961a7b24SJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested", 664961a7b24SJohn Baldwin m); 665e0817317SJulian Elischer } 666535eb309SJohn Baldwin #endif 667961a7b24SJohn Baldwin turnstile_unpend(ts); 6689ed346baSBosko Milekic 6690c0b25aeSJohn Baldwin #ifndef PREEMPTION 670961a7b24SJohn Baldwin /* 671961a7b24SJohn Baldwin * XXX: This is just a hack until preemption is done. However, 672961a7b24SJohn Baldwin * once preemption is done we need to either wrap the 673961a7b24SJohn Baldwin * turnstile_signal() and release of the actual lock in an 674961a7b24SJohn Baldwin * extra critical section or change the preemption code to 675961a7b24SJohn Baldwin * always just set a flag and never do instant-preempts. 676961a7b24SJohn Baldwin */ 677961a7b24SJohn Baldwin td = curthread; 678961a7b24SJohn Baldwin if (td->td_critnest > 0 || td1->td_priority >= td->td_priority) 679961a7b24SJohn Baldwin return; 680961a7b24SJohn Baldwin mtx_lock_spin(&sched_lock); 681961a7b24SJohn Baldwin if (!TD_IS_RUNNING(td1)) { 68236412d79SJohn Baldwin #ifdef notyet 683b40ce416SJulian Elischer if (td->td_ithd != NULL) { 684b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 68536412d79SJohn Baldwin 68636412d79SJohn Baldwin if (it->it_interrupted) { 68719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 68836412d79SJohn Baldwin CTR2(KTR_LOCK, 68915ec816aSJohn Baldwin "_mtx_unlock_sleep: %p interrupted %p", 69036412d79SJohn Baldwin it, it->it_interrupted); 69136412d79SJohn Baldwin intr_thd_fixup(it); 69236412d79SJohn Baldwin } 69336412d79SJohn Baldwin } 69436412d79SJohn Baldwin #endif 69519284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 696562e4ffeSJohn Baldwin CTR2(KTR_LOCK, 6979ed346baSBosko Milekic "_mtx_unlock_sleep: %p switching out lock=%p", m, 6989ed346baSBosko Milekic (void *)m->mtx_lock); 6999ed346baSBosko Milekic 700bf0acc27SJohn Baldwin mi_switch(SW_INVOL, NULL); 70119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7029ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 70331271627SJohn Baldwin m, (void *)m->mtx_lock); 70436412d79SJohn Baldwin } 7059ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 7060c0b25aeSJohn Baldwin #endif 7079ed346baSBosko Milekic 7089ed346baSBosko Milekic return; 7099ed346baSBosko Milekic } 7109ed346baSBosko Milekic 7119ed346baSBosko Milekic /* 7129ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 7139ed346baSBosko Milekic * See the _rel_spin_lock() macro for the details. 7149ed346baSBosko Milekic */ 7159ed346baSBosko Milekic 7169ed346baSBosko Milekic /* 71715ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 7189ed346baSBosko Milekic */ 7191103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 7200cde2e34SJason Evans void 72156771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line) 7220cde2e34SJason Evans { 7235cb0fbe4SJohn Baldwin 7245cb0fbe4SJohn Baldwin if (panicstr != NULL) 7255cb0fbe4SJohn Baldwin return; 726a10f4966SJake Burkholder switch (what) { 7270cde2e34SJason Evans case MA_OWNED: 7280cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 7290cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 730a10f4966SJake Burkholder if (!mtx_owned(m)) 7310cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 73219284646SJohn Baldwin m->mtx_object.lo_name, file, line); 733a10f4966SJake Burkholder if (mtx_recursed(m)) { 734a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 7350cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 73619284646SJohn Baldwin m->mtx_object.lo_name, file, line); 737a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 7380cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 73919284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7400cde2e34SJason Evans } 7410cde2e34SJason Evans break; 7420cde2e34SJason Evans case MA_NOTOWNED: 743a10f4966SJake Burkholder if (mtx_owned(m)) 7440cde2e34SJason Evans panic("mutex %s owned at %s:%d", 74519284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7460cde2e34SJason Evans break; 7470cde2e34SJason Evans default: 74856771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 7490cde2e34SJason Evans } 7500cde2e34SJason Evans } 7510cde2e34SJason Evans #endif 7520cde2e34SJason Evans 7539ed346baSBosko Milekic /* 7549ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 75519284646SJohn Baldwin * 75619284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 75719284646SJohn Baldwin * maintained by the witness code. 7589ed346baSBosko Milekic */ 75936412d79SJohn Baldwin #ifdef MUTEX_DEBUG 76036412d79SJohn Baldwin 7614d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 76236412d79SJohn Baldwin 76319284646SJohn Baldwin void 76419284646SJohn Baldwin mtx_validate(struct mtx *m) 76536412d79SJohn Baldwin { 76636412d79SJohn Baldwin 76736412d79SJohn Baldwin /* 768fa669ab7SPoul-Henning Kamp * XXX: When kernacc() does not require Giant we can reenable this check 769fa669ab7SPoul-Henning Kamp */ 770fa669ab7SPoul-Henning Kamp #ifdef notyet 771fa669ab7SPoul-Henning Kamp /* 77236412d79SJohn Baldwin * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly 77336412d79SJohn Baldwin * we can re-enable the kernacc() checks. 77436412d79SJohn Baldwin */ 77536412d79SJohn Baldwin #ifndef __alpha__ 77676dcbd6fSBosko Milekic /* 77776dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 77876dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 77976dcbd6fSBosko Milekic * requires Giant itself. 78076dcbd6fSBosko Milekic */ 781ab07087eSBosko Milekic if (!cold) 782ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 783ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 78419284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 78536412d79SJohn Baldwin #endif 786fa669ab7SPoul-Henning Kamp #endif 78736412d79SJohn Baldwin } 78836412d79SJohn Baldwin #endif 78936412d79SJohn Baldwin 7909ed346baSBosko Milekic /* 791c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 792c27b5699SAndrew R. Reiter */ 793c27b5699SAndrew R. Reiter void 794c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 795c27b5699SAndrew R. Reiter { 796c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 797c27b5699SAndrew R. Reiter 7980c88508aSJohn Baldwin mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 799c27b5699SAndrew R. Reiter } 800c27b5699SAndrew R. Reiter 801c27b5699SAndrew R. Reiter /* 8029ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 8030c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 8040c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 8050c88508aSJohn Baldwin * witness. 8069ed346baSBosko Milekic */ 80736412d79SJohn Baldwin void 8080c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts) 80936412d79SJohn Baldwin { 81019284646SJohn Baldwin struct lock_object *lock; 8119ed346baSBosko Milekic 81219284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 81375d468eeSJohn Baldwin MTX_NOWITNESS | MTX_DUPOK)) == 0); 8149ed346baSBosko Milekic 81536412d79SJohn Baldwin #ifdef MUTEX_DEBUG 8169ed346baSBosko Milekic /* Diagnostic and error correction */ 81719284646SJohn Baldwin mtx_validate(m); 8186936206eSJohn Baldwin #endif 81936412d79SJohn Baldwin 82019284646SJohn Baldwin lock = &m->mtx_object; 8217ada5876SJohn Baldwin KASSERT((lock->lo_flags & LO_INITIALIZED) == 0, 822b82af320SPoul-Henning Kamp ("mutex \"%s\" %p already initialized", name, m)); 8237ada5876SJohn Baldwin bzero(m, sizeof(*m)); 82419284646SJohn Baldwin if (opts & MTX_SPIN) 82519284646SJohn Baldwin lock->lo_class = &lock_class_mtx_spin; 82619284646SJohn Baldwin else 82719284646SJohn Baldwin lock->lo_class = &lock_class_mtx_sleep; 8280c88508aSJohn Baldwin lock->lo_name = name; 8290c88508aSJohn Baldwin lock->lo_type = type != NULL ? type : name; 83019284646SJohn Baldwin if (opts & MTX_QUIET) 83119284646SJohn Baldwin lock->lo_flags = LO_QUIET; 83219284646SJohn Baldwin if (opts & MTX_RECURSE) 83319284646SJohn Baldwin lock->lo_flags |= LO_RECURSABLE; 83419284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 83519284646SJohn Baldwin lock->lo_flags |= LO_WITNESS; 836f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 837f22a4b62SJeff Roberson lock->lo_flags |= LO_DUPOK; 83819284646SJohn Baldwin 83919284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 8409ed346baSBosko Milekic 84119284646SJohn Baldwin LOCK_LOG_INIT(lock, opts); 842d1c1b841SJason Evans 84319284646SJohn Baldwin WITNESS_INIT(lock); 84436412d79SJohn Baldwin } 84536412d79SJohn Baldwin 8469ed346baSBosko Milekic /* 84719284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 84819284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 84919284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 85019284646SJohn Baldwin * flags. 8519ed346baSBosko Milekic */ 85236412d79SJohn Baldwin void 85336412d79SJohn Baldwin mtx_destroy(struct mtx *m) 85436412d79SJohn Baldwin { 85536412d79SJohn Baldwin 85619284646SJohn Baldwin LOCK_LOG_DESTROY(&m->mtx_object, 0); 8579ed346baSBosko Milekic 85819284646SJohn Baldwin if (!mtx_owned(m)) 85919284646SJohn Baldwin MPASS(mtx_unowned(m)); 86019284646SJohn Baldwin else { 86108812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 8629ed346baSBosko Milekic 86319284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 864c86b6ff5SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 865c86b6ff5SJohn Baldwin __LINE__); 86636412d79SJohn Baldwin } 8670384fff8SJason Evans 86819284646SJohn Baldwin WITNESS_DESTROY(&m->mtx_object); 8690384fff8SJason Evans } 870d23f5958SMatthew Dillon 871d23f5958SMatthew Dillon /* 872c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 873c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 874c53c013bSJohn Baldwin * setup before this is called. 875c53c013bSJohn Baldwin */ 876c53c013bSJohn Baldwin void 877c53c013bSJohn Baldwin mutex_init(void) 878c53c013bSJohn Baldwin { 879c53c013bSJohn Baldwin 880c53c013bSJohn Baldwin /* Setup thread0 so that mutexes work. */ 881c53c013bSJohn Baldwin LIST_INIT(&thread0.td_contested); 882c53c013bSJohn Baldwin 883961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 884961a7b24SJohn Baldwin init_turnstiles(); 885961a7b24SJohn Baldwin 886c53c013bSJohn Baldwin /* 887c53c013bSJohn Baldwin * Initialize mutexes. 888c53c013bSJohn Baldwin */ 8890c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 8900c88508aSJohn Baldwin mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE); 8910c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 892c53c013bSJohn Baldwin mtx_lock(&Giant); 893c53c013bSJohn Baldwin } 894