10384fff8SJason Evans /*- 20384fff8SJason Evans * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 30384fff8SJason Evans * 40384fff8SJason Evans * Redistribution and use in source and binary forms, with or without 50384fff8SJason Evans * modification, are permitted provided that the following conditions 60384fff8SJason Evans * are met: 70384fff8SJason Evans * 1. Redistributions of source code must retain the above copyright 80384fff8SJason Evans * notice, this list of conditions and the following disclaimer. 90384fff8SJason Evans * 2. Redistributions in binary form must reproduce the above copyright 100384fff8SJason Evans * notice, this list of conditions and the following disclaimer in the 110384fff8SJason Evans * documentation and/or other materials provided with the distribution. 120384fff8SJason Evans * 3. Berkeley Software Design Inc's name may not be used to endorse or 130384fff8SJason Evans * promote products derived from this software without specific prior 140384fff8SJason Evans * written permission. 150384fff8SJason Evans * 160384fff8SJason Evans * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 170384fff8SJason Evans * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 180384fff8SJason Evans * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 190384fff8SJason Evans * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 200384fff8SJason Evans * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 210384fff8SJason Evans * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 220384fff8SJason Evans * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 230384fff8SJason Evans * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 240384fff8SJason Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 250384fff8SJason Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 260384fff8SJason Evans * SUCH DAMAGE. 270384fff8SJason Evans * 280384fff8SJason Evans * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 2936412d79SJohn Baldwin * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 300384fff8SJason Evans */ 310384fff8SJason Evans 320384fff8SJason Evans /* 33ba48b69aSJohn Baldwin * Machine independent bits of mutex implementation. 340384fff8SJason Evans */ 350384fff8SJason Evans 36677b542eSDavid E. O'Brien #include <sys/cdefs.h> 37677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 38677b542eSDavid E. O'Brien 392498cf8cSJohn Baldwin #include "opt_adaptive_mutexes.h" 409c36c934SJohn Baldwin #include "opt_ddb.h" 4100096801SJohn-Mark Gurney #include "opt_mprof.h" 42535eb309SJohn Baldwin #include "opt_mutex_wake_all.h" 439923b511SScott Long #include "opt_sched.h" 44a5a96a19SJohn Baldwin 450384fff8SJason Evans #include <sys/param.h> 466c35e809SDag-Erling Smørgrav #include <sys/systm.h> 4736412d79SJohn Baldwin #include <sys/bus.h> 481126349aSPaul Saab #include <sys/conf.h> 492d50560aSMarcel Moolenaar #include <sys/kdb.h> 5036412d79SJohn Baldwin #include <sys/kernel.h> 516c35e809SDag-Erling Smørgrav #include <sys/ktr.h> 5219284646SJohn Baldwin #include <sys/lock.h> 53fb919e4dSMark Murray #include <sys/malloc.h> 5419284646SJohn Baldwin #include <sys/mutex.h> 550384fff8SJason Evans #include <sys/proc.h> 56c4f7a187SJohn Baldwin #include <sys/resourcevar.h> 57b43179fbSJeff Roberson #include <sys/sched.h> 586c35e809SDag-Erling Smørgrav #include <sys/sbuf.h> 59a5a96a19SJohn Baldwin #include <sys/sysctl.h> 60961a7b24SJohn Baldwin #include <sys/turnstile.h> 6136412d79SJohn Baldwin #include <sys/vmmeter.h> 620384fff8SJason Evans 6336412d79SJohn Baldwin #include <machine/atomic.h> 6436412d79SJohn Baldwin #include <machine/bus.h> 6536412d79SJohn Baldwin #include <machine/clock.h> 660384fff8SJason Evans #include <machine/cpu.h> 6736412d79SJohn Baldwin 689c36c934SJohn Baldwin #include <ddb/ddb.h> 699c36c934SJohn Baldwin 708c4b6380SJohn Baldwin #include <fs/devfs/devfs_int.h> 718c4b6380SJohn Baldwin 7236412d79SJohn Baldwin #include <vm/vm.h> 7336412d79SJohn Baldwin #include <vm/vm_extern.h> 7436412d79SJohn Baldwin 750cde2e34SJason Evans /* 76b9a80acaSStephan Uphoff * Force MUTEX_WAKE_ALL for now. 77b9a80acaSStephan Uphoff * single thread wakeup needs fixes to avoid race conditions with 78b9a80acaSStephan Uphoff * priority inheritance. 79b9a80acaSStephan Uphoff */ 80b9a80acaSStephan Uphoff #ifndef MUTEX_WAKE_ALL 81b9a80acaSStephan Uphoff #define MUTEX_WAKE_ALL 82b9a80acaSStephan Uphoff #endif 83b9a80acaSStephan Uphoff 84b9a80acaSStephan Uphoff /* 859ed346baSBosko Milekic * Internal utility macros. 860cde2e34SJason Evans */ 879ed346baSBosko Milekic #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED) 880cde2e34SJason Evans 899ed346baSBosko Milekic #define mtx_owner(m) (mtx_unowned((m)) ? NULL \ 90b40ce416SJulian Elischer : (struct thread *)((m)->mtx_lock & MTX_FLAGMASK)) 919ed346baSBosko Milekic 920cde2e34SJason Evans /* 9319284646SJohn Baldwin * Lock classes for sleep and spin mutexes. 940cde2e34SJason Evans */ 9519284646SJohn Baldwin struct lock_class lock_class_mtx_sleep = { 9619284646SJohn Baldwin "sleep mutex", 9719284646SJohn Baldwin LC_SLEEPLOCK | LC_RECURSABLE 9819284646SJohn Baldwin }; 9919284646SJohn Baldwin struct lock_class lock_class_mtx_spin = { 10019284646SJohn Baldwin "spin mutex", 10119284646SJohn Baldwin LC_SPINLOCK | LC_RECURSABLE 1028484de75SJohn Baldwin }; 1038484de75SJohn Baldwin 1049ed346baSBosko Milekic /* 105c53c013bSJohn Baldwin * System-wide mutexes 106c53c013bSJohn Baldwin */ 107c53c013bSJohn Baldwin struct mtx sched_lock; 108c53c013bSJohn Baldwin struct mtx Giant; 109c53c013bSJohn Baldwin 1106c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 1116c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug, OID_AUTO, mutex, CTLFLAG_RD, NULL, "mutex debugging"); 1126c35e809SDag-Erling Smørgrav SYSCTL_NODE(_debug_mutex, OID_AUTO, prof, CTLFLAG_RD, NULL, "mutex profiling"); 1136c35e809SDag-Erling Smørgrav static int mutex_prof_enable = 0; 1146c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, enable, CTLFLAG_RW, 1156c35e809SDag-Erling Smørgrav &mutex_prof_enable, 0, "Enable tracing of mutex holdtime"); 1166c35e809SDag-Erling Smørgrav 1176c35e809SDag-Erling Smørgrav struct mutex_prof { 1186c35e809SDag-Erling Smørgrav const char *name; 1196c35e809SDag-Erling Smørgrav const char *file; 1206c35e809SDag-Erling Smørgrav int line; 121ecf031c9SDag-Erling Smørgrav uintmax_t cnt_max; 122ecf031c9SDag-Erling Smørgrav uintmax_t cnt_tot; 123ecf031c9SDag-Erling Smørgrav uintmax_t cnt_cur; 1248dc10be8SRobert Watson uintmax_t cnt_contest_holding; 1258dc10be8SRobert Watson uintmax_t cnt_contest_locking; 126e6330704SDag-Erling Smørgrav struct mutex_prof *next; 1276c35e809SDag-Erling Smørgrav }; 1286c35e809SDag-Erling Smørgrav 1296c35e809SDag-Erling Smørgrav /* 1306c35e809SDag-Erling Smørgrav * mprof_buf is a static pool of profiling records to avoid possible 1316c35e809SDag-Erling Smørgrav * reentrance of the memory allocation functions. 1326c35e809SDag-Erling Smørgrav * 1336c35e809SDag-Erling Smørgrav * Note: NUM_MPROF_BUFFERS must be smaller than MPROF_HASH_SIZE. 1346c35e809SDag-Erling Smørgrav */ 13500096801SJohn-Mark Gurney #ifdef MPROF_BUFFERS 13600096801SJohn-Mark Gurney #define NUM_MPROF_BUFFERS MPROF_BUFFERS 13700096801SJohn-Mark Gurney #else 138e6330704SDag-Erling Smørgrav #define NUM_MPROF_BUFFERS 1000 13900096801SJohn-Mark Gurney #endif 1406c35e809SDag-Erling Smørgrav static struct mutex_prof mprof_buf[NUM_MPROF_BUFFERS]; 1416c35e809SDag-Erling Smørgrav static int first_free_mprof_buf; 14200096801SJohn-Mark Gurney #ifndef MPROF_HASH_SIZE 143e6330704SDag-Erling Smørgrav #define MPROF_HASH_SIZE 1009 14400096801SJohn-Mark Gurney #endif 14500096801SJohn-Mark Gurney #if NUM_MPROF_BUFFERS >= MPROF_HASH_SIZE 14600096801SJohn-Mark Gurney #error MPROF_BUFFERS must be larger than MPROF_HASH_SIZE 14700096801SJohn-Mark Gurney #endif 1486c35e809SDag-Erling Smørgrav static struct mutex_prof *mprof_hash[MPROF_HASH_SIZE]; 1490bd5f797SMike Makonnen /* SWAG: sbuf size = avg stat. line size * number of locks */ 1500bd5f797SMike Makonnen #define MPROF_SBUF_SIZE 256 * 400 1516c35e809SDag-Erling Smørgrav 1526c35e809SDag-Erling Smørgrav static int mutex_prof_acquisitions; 1536c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, acquisitions, CTLFLAG_RD, 1546c35e809SDag-Erling Smørgrav &mutex_prof_acquisitions, 0, "Number of mutex acquistions recorded"); 1556c35e809SDag-Erling Smørgrav static int mutex_prof_records; 1566c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, records, CTLFLAG_RD, 1576c35e809SDag-Erling Smørgrav &mutex_prof_records, 0, "Number of profiling records"); 1586c35e809SDag-Erling Smørgrav static int mutex_prof_maxrecords = NUM_MPROF_BUFFERS; 1596c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, maxrecords, CTLFLAG_RD, 1606c35e809SDag-Erling Smørgrav &mutex_prof_maxrecords, 0, "Maximum number of profiling records"); 1616c35e809SDag-Erling Smørgrav static int mutex_prof_rejected; 1626c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, rejected, CTLFLAG_RD, 1636c35e809SDag-Erling Smørgrav &mutex_prof_rejected, 0, "Number of rejected profiling records"); 1646c35e809SDag-Erling Smørgrav static int mutex_prof_hashsize = MPROF_HASH_SIZE; 1656c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, hashsize, CTLFLAG_RD, 1666c35e809SDag-Erling Smørgrav &mutex_prof_hashsize, 0, "Hash size"); 1676c35e809SDag-Erling Smørgrav static int mutex_prof_collisions = 0; 1686c35e809SDag-Erling Smørgrav SYSCTL_INT(_debug_mutex_prof, OID_AUTO, collisions, CTLFLAG_RD, 1696c35e809SDag-Erling Smørgrav &mutex_prof_collisions, 0, "Number of hash collisions"); 1706c35e809SDag-Erling Smørgrav 1716c35e809SDag-Erling Smørgrav /* 1726c35e809SDag-Erling Smørgrav * mprof_mtx protects the profiling buffers and the hash. 1736c35e809SDag-Erling Smørgrav */ 1746c35e809SDag-Erling Smørgrav static struct mtx mprof_mtx; 175e6330704SDag-Erling Smørgrav MTX_SYSINIT(mprof, &mprof_mtx, "mutex profiling lock", MTX_SPIN | MTX_QUIET); 1766c35e809SDag-Erling Smørgrav 177b784ffe9SDag-Erling Smørgrav static u_int64_t 178b784ffe9SDag-Erling Smørgrav nanoseconds(void) 179b784ffe9SDag-Erling Smørgrav { 180b784ffe9SDag-Erling Smørgrav struct timespec tv; 181b784ffe9SDag-Erling Smørgrav 182b784ffe9SDag-Erling Smørgrav nanotime(&tv); 183b784ffe9SDag-Erling Smørgrav return (tv.tv_sec * (u_int64_t)1000000000 + tv.tv_nsec); 184b784ffe9SDag-Erling Smørgrav } 185b784ffe9SDag-Erling Smørgrav 1866c35e809SDag-Erling Smørgrav static int 1876c35e809SDag-Erling Smørgrav dump_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 1886c35e809SDag-Erling Smørgrav { 1896c35e809SDag-Erling Smørgrav struct sbuf *sb; 1906c35e809SDag-Erling Smørgrav int error, i; 1910bd5f797SMike Makonnen static int multiplier = 1; 1926c35e809SDag-Erling Smørgrav 1936c35e809SDag-Erling Smørgrav if (first_free_mprof_buf == 0) 1946d036900SDag-Erling Smørgrav return (SYSCTL_OUT(req, "No locking recorded", 1956d036900SDag-Erling Smørgrav sizeof("No locking recorded"))); 1966c35e809SDag-Erling Smørgrav 1970bd5f797SMike Makonnen retry_sbufops: 1980bd5f797SMike Makonnen sb = sbuf_new(NULL, NULL, MPROF_SBUF_SIZE * multiplier, SBUF_FIXEDLEN); 1994f201858SGleb Smirnoff sbuf_printf(sb, "\n%6s %12s %11s %5s %12s %12s %s\n", 2008dc10be8SRobert Watson "max", "total", "count", "avg", "cnt_hold", "cnt_lock", "name"); 2016d036900SDag-Erling Smørgrav /* 2026d036900SDag-Erling Smørgrav * XXX this spinlock seems to be by far the largest perpetrator 2036d036900SDag-Erling Smørgrav * of spinlock latency (1.6 msec on an Athlon1600 was recorded 2046d036900SDag-Erling Smørgrav * even before I pessimized it further by moving the average 2056d036900SDag-Erling Smørgrav * computation here). 2066d036900SDag-Erling Smørgrav */ 2076c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 2080bd5f797SMike Makonnen for (i = 0; i < first_free_mprof_buf; ++i) { 2098dc10be8SRobert Watson sbuf_printf(sb, "%6ju %12ju %11ju %5ju %12ju %12ju %s:%d (%s)\n", 210ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_max / 1000, 211ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / 1000, 212ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur, 213ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_cur == 0 ? (uintmax_t)0 : 214ecf031c9SDag-Erling Smørgrav mprof_buf[i].cnt_tot / (mprof_buf[i].cnt_cur * 1000), 2158dc10be8SRobert Watson mprof_buf[i].cnt_contest_holding, 2168dc10be8SRobert Watson mprof_buf[i].cnt_contest_locking, 2176c35e809SDag-Erling Smørgrav mprof_buf[i].file, mprof_buf[i].line, mprof_buf[i].name); 2180bd5f797SMike Makonnen if (sbuf_overflowed(sb)) { 2190bd5f797SMike Makonnen mtx_unlock_spin(&mprof_mtx); 2200bd5f797SMike Makonnen sbuf_delete(sb); 2210bd5f797SMike Makonnen multiplier++; 2220bd5f797SMike Makonnen goto retry_sbufops; 2230bd5f797SMike Makonnen } 2240bd5f797SMike Makonnen } 2256c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 2266c35e809SDag-Erling Smørgrav sbuf_finish(sb); 2276c35e809SDag-Erling Smørgrav error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2286c35e809SDag-Erling Smørgrav sbuf_delete(sb); 2296c35e809SDag-Erling Smørgrav return (error); 2306c35e809SDag-Erling Smørgrav } 2316c35e809SDag-Erling Smørgrav SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, stats, CTLTYPE_STRING | CTLFLAG_RD, 2326c35e809SDag-Erling Smørgrav NULL, 0, dump_mutex_prof_stats, "A", "Mutex profiling statistics"); 23394ffb20dSRobert Watson 23494ffb20dSRobert Watson static int 23594ffb20dSRobert Watson reset_mutex_prof_stats(SYSCTL_HANDLER_ARGS) 23694ffb20dSRobert Watson { 23794ffb20dSRobert Watson int error, v; 23894ffb20dSRobert Watson 23994ffb20dSRobert Watson if (first_free_mprof_buf == 0) 24094ffb20dSRobert Watson return (0); 24194ffb20dSRobert Watson 24294ffb20dSRobert Watson v = 0; 24394ffb20dSRobert Watson error = sysctl_handle_int(oidp, &v, 0, req); 24494ffb20dSRobert Watson if (error) 24594ffb20dSRobert Watson return (error); 24694ffb20dSRobert Watson if (req->newptr == NULL) 24794ffb20dSRobert Watson return (error); 24894ffb20dSRobert Watson if (v == 0) 24994ffb20dSRobert Watson return (0); 25094ffb20dSRobert Watson 25194ffb20dSRobert Watson mtx_lock_spin(&mprof_mtx); 25294ffb20dSRobert Watson bzero(mprof_buf, sizeof(*mprof_buf) * first_free_mprof_buf); 25394ffb20dSRobert Watson bzero(mprof_hash, sizeof(struct mtx *) * MPROF_HASH_SIZE); 25494ffb20dSRobert Watson first_free_mprof_buf = 0; 25594ffb20dSRobert Watson mtx_unlock_spin(&mprof_mtx); 25694ffb20dSRobert Watson return (0); 25794ffb20dSRobert Watson } 25894ffb20dSRobert Watson SYSCTL_PROC(_debug_mutex_prof, OID_AUTO, reset, CTLTYPE_INT | CTLFLAG_RW, 25994ffb20dSRobert Watson NULL, 0, reset_mutex_prof_stats, "I", "Reset mutex profiling statistics"); 2606c35e809SDag-Erling Smørgrav #endif 2616c35e809SDag-Erling Smørgrav 2620cde2e34SJason Evans /* 2636283b7d0SJohn Baldwin * Function versions of the inlined __mtx_* macros. These are used by 2646283b7d0SJohn Baldwin * modules and can also be called from assembly language if needed. 2656283b7d0SJohn Baldwin */ 2666283b7d0SJohn Baldwin void 2676283b7d0SJohn Baldwin _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line) 2686283b7d0SJohn Baldwin { 2696283b7d0SJohn Baldwin 270dde96c99SJohn Baldwin MPASS(curthread != NULL); 2710d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 2720d975d63SJohn Baldwin ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 2730d975d63SJohn Baldwin file, line)); 2748d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 2758d768e76SJohn Baldwin file, line); 276dde96c99SJohn Baldwin _get_sleep_lock(m, curthread, opts, file, line); 277dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 278dde96c99SJohn Baldwin line); 279dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 2806c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 2816c35e809SDag-Erling Smørgrav /* don't reset the timer when/if recursing */ 282b61860adSDag-Erling Smørgrav if (m->mtx_acqtime == 0) { 283b61860adSDag-Erling Smørgrav m->mtx_filename = file; 284b61860adSDag-Erling Smørgrav m->mtx_lineno = line; 285b61860adSDag-Erling Smørgrav m->mtx_acqtime = mutex_prof_enable ? nanoseconds() : 0; 2866c35e809SDag-Erling Smørgrav ++mutex_prof_acquisitions; 2876c35e809SDag-Erling Smørgrav } 2886c35e809SDag-Erling Smørgrav #endif 2896283b7d0SJohn Baldwin } 2906283b7d0SJohn Baldwin 2916283b7d0SJohn Baldwin void 2926283b7d0SJohn Baldwin _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line) 2936283b7d0SJohn Baldwin { 2946283b7d0SJohn Baldwin 295dde96c99SJohn Baldwin MPASS(curthread != NULL); 2960d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 2970d975d63SJohn Baldwin ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 2980d975d63SJohn Baldwin file, line)); 2990d975d63SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3000d975d63SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 3010d975d63SJohn Baldwin line); 30221377ce0SJohn Baldwin mtx_assert(m, MA_OWNED); 3036c35e809SDag-Erling Smørgrav #ifdef MUTEX_PROFILING 304b61860adSDag-Erling Smørgrav if (m->mtx_acqtime != 0) { 3056c35e809SDag-Erling Smørgrav static const char *unknown = "(unknown)"; 3066c35e809SDag-Erling Smørgrav struct mutex_prof *mpp; 307b784ffe9SDag-Erling Smørgrav u_int64_t acqtime, now; 3086c35e809SDag-Erling Smørgrav const char *p, *q; 309e6330704SDag-Erling Smørgrav volatile u_int hash; 3106c35e809SDag-Erling Smørgrav 311b784ffe9SDag-Erling Smørgrav now = nanoseconds(); 312b61860adSDag-Erling Smørgrav acqtime = m->mtx_acqtime; 313b61860adSDag-Erling Smørgrav m->mtx_acqtime = 0; 314b784ffe9SDag-Erling Smørgrav if (now <= acqtime) 3156c35e809SDag-Erling Smørgrav goto out; 3160bd5f797SMike Makonnen for (p = m->mtx_filename; 3170bd5f797SMike Makonnen p != NULL && strncmp(p, "../", 3) == 0; p += 3) 3186c35e809SDag-Erling Smørgrav /* nothing */ ; 3196c35e809SDag-Erling Smørgrav if (p == NULL || *p == '\0') 3206c35e809SDag-Erling Smørgrav p = unknown; 321b61860adSDag-Erling Smørgrav for (hash = m->mtx_lineno, q = p; *q != '\0'; ++q) 3226c35e809SDag-Erling Smørgrav hash = (hash * 2 + *q) % MPROF_HASH_SIZE; 3236c35e809SDag-Erling Smørgrav mtx_lock_spin(&mprof_mtx); 324e6330704SDag-Erling Smørgrav for (mpp = mprof_hash[hash]; mpp != NULL; mpp = mpp->next) 325b61860adSDag-Erling Smørgrav if (mpp->line == m->mtx_lineno && 326b61860adSDag-Erling Smørgrav strcmp(mpp->file, p) == 0) 3276c35e809SDag-Erling Smørgrav break; 3286c35e809SDag-Erling Smørgrav if (mpp == NULL) { 3296c35e809SDag-Erling Smørgrav /* Just exit if we cannot get a trace buffer */ 3306c35e809SDag-Erling Smørgrav if (first_free_mprof_buf >= NUM_MPROF_BUFFERS) { 3316c35e809SDag-Erling Smørgrav ++mutex_prof_rejected; 3326c35e809SDag-Erling Smørgrav goto unlock; 3336c35e809SDag-Erling Smørgrav } 3346c35e809SDag-Erling Smørgrav mpp = &mprof_buf[first_free_mprof_buf++]; 3356c35e809SDag-Erling Smørgrav mpp->name = mtx_name(m); 3366c35e809SDag-Erling Smørgrav mpp->file = p; 337b61860adSDag-Erling Smørgrav mpp->line = m->mtx_lineno; 338e6330704SDag-Erling Smørgrav mpp->next = mprof_hash[hash]; 339e6330704SDag-Erling Smørgrav if (mprof_hash[hash] != NULL) 340e6330704SDag-Erling Smørgrav ++mutex_prof_collisions; 3416c35e809SDag-Erling Smørgrav mprof_hash[hash] = mpp; 342e6330704SDag-Erling Smørgrav ++mutex_prof_records; 3436c35e809SDag-Erling Smørgrav } 3446c35e809SDag-Erling Smørgrav /* 3456c35e809SDag-Erling Smørgrav * Record if the mutex has been held longer now than ever 3466d036900SDag-Erling Smørgrav * before. 3476c35e809SDag-Erling Smørgrav */ 348ecf031c9SDag-Erling Smørgrav if (now - acqtime > mpp->cnt_max) 349ecf031c9SDag-Erling Smørgrav mpp->cnt_max = now - acqtime; 350ecf031c9SDag-Erling Smørgrav mpp->cnt_tot += now - acqtime; 351ecf031c9SDag-Erling Smørgrav mpp->cnt_cur++; 3528dc10be8SRobert Watson /* 3538dc10be8SRobert Watson * There's a small race, really we should cmpxchg 3548dc10be8SRobert Watson * 0 with the current value, but that would bill 3558dc10be8SRobert Watson * the contention to the wrong lock instance if 3568dc10be8SRobert Watson * it followed this also. 3578dc10be8SRobert Watson */ 3588dc10be8SRobert Watson mpp->cnt_contest_holding += m->mtx_contest_holding; 3598dc10be8SRobert Watson m->mtx_contest_holding = 0; 3608dc10be8SRobert Watson mpp->cnt_contest_locking += m->mtx_contest_locking; 3618dc10be8SRobert Watson m->mtx_contest_locking = 0; 3626c35e809SDag-Erling Smørgrav unlock: 3636c35e809SDag-Erling Smørgrav mtx_unlock_spin(&mprof_mtx); 3646c35e809SDag-Erling Smørgrav } 3656c35e809SDag-Erling Smørgrav out: 3666c35e809SDag-Erling Smørgrav #endif 367dde96c99SJohn Baldwin _rel_sleep_lock(m, curthread, opts, file, line); 3686283b7d0SJohn Baldwin } 3696283b7d0SJohn Baldwin 3706283b7d0SJohn Baldwin void 3716283b7d0SJohn Baldwin _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line) 3726283b7d0SJohn Baldwin { 3736283b7d0SJohn Baldwin 374dde96c99SJohn Baldwin MPASS(curthread != NULL); 3750d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 3760d975d63SJohn Baldwin ("mtx_lock_spin() of sleep mutex %s @ %s:%d", 3770d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 3788d768e76SJohn Baldwin WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE, 3798d768e76SJohn Baldwin file, line); 380dde96c99SJohn Baldwin _get_spin_lock(m, curthread, opts, file, line); 381dde96c99SJohn Baldwin LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file, 382dde96c99SJohn Baldwin line); 383dde96c99SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 3846283b7d0SJohn Baldwin } 3856283b7d0SJohn Baldwin 3866283b7d0SJohn Baldwin void 3876283b7d0SJohn Baldwin _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line) 3886283b7d0SJohn Baldwin { 3896283b7d0SJohn Baldwin 390dde96c99SJohn Baldwin MPASS(curthread != NULL); 3910d975d63SJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_spin, 3920d975d63SJohn Baldwin ("mtx_unlock_spin() of sleep mutex %s @ %s:%d", 3930d975d63SJohn Baldwin m->mtx_object.lo_name, file, line)); 394dde96c99SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line); 395dde96c99SJohn Baldwin LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file, 396dde96c99SJohn Baldwin line); 3970d975d63SJohn Baldwin mtx_assert(m, MA_OWNED); 398dde96c99SJohn Baldwin _rel_spin_lock(m); 3996283b7d0SJohn Baldwin } 4006283b7d0SJohn Baldwin 4016283b7d0SJohn Baldwin /* 4029ed346baSBosko Milekic * The important part of mtx_trylock{,_flags}() 403eac09796SJohn Baldwin * Tries to acquire lock `m.' If this function is called on a mutex that 404eac09796SJohn Baldwin * is already owned, it will recursively acquire the lock. 4050cde2e34SJason Evans */ 4060cde2e34SJason Evans int 4079ed346baSBosko Milekic _mtx_trylock(struct mtx *m, int opts, const char *file, int line) 4080cde2e34SJason Evans { 4090cde2e34SJason Evans int rval; 4100cde2e34SJason Evans 411b40ce416SJulian Elischer MPASS(curthread != NULL); 41283cece6fSJohn Baldwin KASSERT(m->mtx_object.lo_class == &lock_class_mtx_sleep, 41383cece6fSJohn Baldwin ("mtx_trylock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name, 41483cece6fSJohn Baldwin file, line)); 4159ed346baSBosko Milekic 416eac09796SJohn Baldwin if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) { 417eac09796SJohn Baldwin m->mtx_recurse++; 418eac09796SJohn Baldwin atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 419eac09796SJohn Baldwin rval = 1; 420eac09796SJohn Baldwin } else 421122eceefSJohn Baldwin rval = _obtain_lock(m, (uintptr_t)curthread); 4229ed346baSBosko Milekic 42319284646SJohn Baldwin LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line); 4246b869595SJohn Baldwin if (rval) 4252d96f0b1SJohn Baldwin WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK, 4262d96f0b1SJohn Baldwin file, line); 4279ed346baSBosko Milekic 42819284646SJohn Baldwin return (rval); 4290cde2e34SJason Evans } 4300cde2e34SJason Evans 4310cde2e34SJason Evans /* 4329ed346baSBosko Milekic * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock. 4339ed346baSBosko Milekic * 4349ed346baSBosko Milekic * We call this if the lock is either contested (i.e. we need to go to 4359ed346baSBosko Milekic * sleep waiting for it), or if we need to recurse on it. 4360cde2e34SJason Evans */ 4370cde2e34SJason Evans void 438122eceefSJohn Baldwin _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file, 439bdcfcf5bSJohn Baldwin int line) 44036412d79SJohn Baldwin { 441701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 4422498cf8cSJohn Baldwin struct thread *owner; 4432498cf8cSJohn Baldwin #endif 4445fa8dd90SJohn Baldwin uintptr_t v; 44502bd1bcdSIan Dowse #ifdef KTR 44602bd1bcdSIan Dowse int cont_logged = 0; 44702bd1bcdSIan Dowse #endif 4488dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4498dc10be8SRobert Watson int contested; 4508dc10be8SRobert Watson #endif 45136412d79SJohn Baldwin 4525fa8dd90SJohn Baldwin if (mtx_owned(m)) { 453eac09796SJohn Baldwin KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0, 454eac09796SJohn Baldwin ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n", 455eac09796SJohn Baldwin m->mtx_object.lo_name, file, line)); 45636412d79SJohn Baldwin m->mtx_recurse++; 45708812b39SBosko Milekic atomic_set_ptr(&m->mtx_lock, MTX_RECURSED); 45819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 4595746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m); 46036412d79SJohn Baldwin return; 46136412d79SJohn Baldwin } 4629ed346baSBosko Milekic 46319284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 46415ec816aSJohn Baldwin CTR4(KTR_LOCK, 46515ec816aSJohn Baldwin "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d", 46619284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock, file, line); 4671bd0eefbSJohn Baldwin 4688dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4698dc10be8SRobert Watson contested = 0; 4708dc10be8SRobert Watson #endif 471122eceefSJohn Baldwin while (!_obtain_lock(m, tid)) { 4728dc10be8SRobert Watson #ifdef MUTEX_PROFILING 4738dc10be8SRobert Watson contested = 1; 4748dc10be8SRobert Watson atomic_add_int(&m->mtx_contest_holding, 1); 4758dc10be8SRobert Watson #endif 4762ff0e645SJohn Baldwin turnstile_lock(&m->mtx_object); 4775fa8dd90SJohn Baldwin v = m->mtx_lock; 4785fa8dd90SJohn Baldwin 47936412d79SJohn Baldwin /* 4809ed346baSBosko Milekic * Check if the lock has been released while spinning for 481961a7b24SJohn Baldwin * the turnstile chain lock. 48236412d79SJohn Baldwin */ 4835fa8dd90SJohn Baldwin if (v == MTX_UNOWNED) { 484961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 4859f1b87f1SMaxime Henrion cpu_spinwait(); 48636412d79SJohn Baldwin continue; 48736412d79SJohn Baldwin } 4889ed346baSBosko Milekic 489535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 490535eb309SJohn Baldwin MPASS(v != MTX_CONTESTED); 491535eb309SJohn Baldwin #else 49236412d79SJohn Baldwin /* 4939ed346baSBosko Milekic * The mutex was marked contested on release. This means that 494f7ee1590SJohn Baldwin * there are other threads blocked on it. Grab ownership of 495f7ee1590SJohn Baldwin * it and propagate its priority to the current thread if 496f7ee1590SJohn Baldwin * necessary. 49736412d79SJohn Baldwin */ 49836412d79SJohn Baldwin if (v == MTX_CONTESTED) { 499122eceefSJohn Baldwin m->mtx_lock = tid | MTX_CONTESTED; 5002ff0e645SJohn Baldwin turnstile_claim(&m->mtx_object); 5018dc10be8SRobert Watson break; 50236412d79SJohn Baldwin } 503535eb309SJohn Baldwin #endif 5049ed346baSBosko Milekic 50536412d79SJohn Baldwin /* 5069ed346baSBosko Milekic * If the mutex isn't already contested and a failure occurs 5079ed346baSBosko Milekic * setting the contested bit, the mutex was either released 5089ed346baSBosko Milekic * or the state of the MTX_RECURSED bit changed. 50936412d79SJohn Baldwin */ 51036412d79SJohn Baldwin if ((v & MTX_CONTESTED) == 0 && 511122eceefSJohn Baldwin !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) { 512961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 5139f1b87f1SMaxime Henrion cpu_spinwait(); 51436412d79SJohn Baldwin continue; 51536412d79SJohn Baldwin } 51636412d79SJohn Baldwin 517701f1408SScott Long #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 5182498cf8cSJohn Baldwin /* 5192498cf8cSJohn Baldwin * If the current owner of the lock is executing on another 5202498cf8cSJohn Baldwin * CPU, spin instead of blocking. 5212498cf8cSJohn Baldwin */ 5222498cf8cSJohn Baldwin owner = (struct thread *)(v & MTX_FLAGMASK); 523a9abdce4SRobert Watson #ifdef ADAPTIVE_GIANT 524a9abdce4SRobert Watson if (TD_IS_RUNNING(owner)) { 525a9abdce4SRobert Watson #else 52627dad03cSJohn Baldwin if (m != &Giant && TD_IS_RUNNING(owner)) { 527a9abdce4SRobert Watson #endif 528961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 52927dad03cSJohn Baldwin while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) { 5309f1b87f1SMaxime Henrion cpu_spinwait(); 5317fcca609SJohn Baldwin } 5322498cf8cSJohn Baldwin continue; 5332498cf8cSJohn Baldwin } 534701f1408SScott Long #endif /* SMP && !NO_ADAPTIVE_MUTEXES */ 5352498cf8cSJohn Baldwin 5369ed346baSBosko Milekic /* 5377feefcd6SJohn Baldwin * We definitely must sleep for this lock. 5389ed346baSBosko Milekic */ 53936412d79SJohn Baldwin mtx_assert(m, MA_NOTOWNED); 54036412d79SJohn Baldwin 54102bd1bcdSIan Dowse #ifdef KTR 54202bd1bcdSIan Dowse if (!cont_logged) { 54302bd1bcdSIan Dowse CTR6(KTR_CONTENTION, 54402bd1bcdSIan Dowse "contention: %p at %s:%d wants %s, taken by %s:%d", 545122eceefSJohn Baldwin (void *)tid, file, line, m->mtx_object.lo_name, 54602bd1bcdSIan Dowse WITNESS_FILE(&m->mtx_object), 54702bd1bcdSIan Dowse WITNESS_LINE(&m->mtx_object)); 54802bd1bcdSIan Dowse cont_logged = 1; 54902bd1bcdSIan Dowse } 55002bd1bcdSIan Dowse #endif 55136412d79SJohn Baldwin 5529ed346baSBosko Milekic /* 553961a7b24SJohn Baldwin * Block on the turnstile. 5549ed346baSBosko Milekic */ 5552ff0e645SJohn Baldwin turnstile_wait(&m->mtx_object, mtx_owner(m)); 55636412d79SJohn Baldwin } 5579ed346baSBosko Milekic 55802bd1bcdSIan Dowse #ifdef KTR 55902bd1bcdSIan Dowse if (cont_logged) { 56002bd1bcdSIan Dowse CTR4(KTR_CONTENTION, 56102bd1bcdSIan Dowse "contention end: %s acquired by %p at %s:%d", 562122eceefSJohn Baldwin m->mtx_object.lo_name, (void *)tid, file, line); 56302bd1bcdSIan Dowse } 56402bd1bcdSIan Dowse #endif 5658dc10be8SRobert Watson #ifdef MUTEX_PROFILING 5668dc10be8SRobert Watson if (contested) 5678dc10be8SRobert Watson m->mtx_contest_locking++; 5688dc10be8SRobert Watson m->mtx_contest_holding = 0; 5698dc10be8SRobert Watson #endif 57036412d79SJohn Baldwin return; 5719ed346baSBosko Milekic } 5729ed346baSBosko Milekic 57333fb8a38SJohn Baldwin #ifdef SMP 5749ed346baSBosko Milekic /* 5759ed346baSBosko Milekic * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock. 5769ed346baSBosko Milekic * 5779ed346baSBosko Milekic * This is only called if we need to actually spin for the lock. Recursion 5789ed346baSBosko Milekic * is handled inline. 5799ed346baSBosko Milekic */ 5809ed346baSBosko Milekic void 581122eceefSJohn Baldwin _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file, 582bdcfcf5bSJohn Baldwin int line) 58336412d79SJohn Baldwin { 58436412d79SJohn Baldwin int i = 0; 58536412d79SJohn Baldwin 58619284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 5875746a1d8SBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m); 5889ed346baSBosko Milekic 58936412d79SJohn Baldwin for (;;) { 590122eceefSJohn Baldwin if (_obtain_lock(m, tid)) 59136412d79SJohn Baldwin break; 5929ed346baSBosko Milekic 5937141f2adSJohn Baldwin /* Give interrupts a chance while we spin. */ 594c6a37e84SJohn Baldwin spinlock_exit(); 59536412d79SJohn Baldwin while (m->mtx_lock != MTX_UNOWNED) { 596703fc290SJohn Baldwin if (i++ < 10000000) { 5979f1b87f1SMaxime Henrion cpu_spinwait(); 59836412d79SJohn Baldwin continue; 599703fc290SJohn Baldwin } 6000e54ddadSJohn Baldwin if (i < 60000000) 60136412d79SJohn Baldwin DELAY(1); 60283cece6fSJohn Baldwin else if (!kdb_active && !panicstr) { 60341109518SJohn Baldwin printf("spin lock %s held by %p for > 5 seconds\n", 60419284646SJohn Baldwin m->mtx_object.lo_name, (void *)m->mtx_lock); 60541109518SJohn Baldwin #ifdef WITNESS 60641109518SJohn Baldwin witness_display_spinlock(&m->mtx_object, 60741109518SJohn Baldwin mtx_owner(m)); 60841109518SJohn Baldwin #endif 60941109518SJohn Baldwin panic("spin lock held too long"); 61041109518SJohn Baldwin } 6119f1b87f1SMaxime Henrion cpu_spinwait(); 61236412d79SJohn Baldwin } 613c6a37e84SJohn Baldwin spinlock_enter(); 61436412d79SJohn Baldwin } 61536412d79SJohn Baldwin 61619284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6179ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m); 6189ed346baSBosko Milekic 61936412d79SJohn Baldwin return; 62036412d79SJohn Baldwin } 62133fb8a38SJohn Baldwin #endif /* SMP */ 62236412d79SJohn Baldwin 6239ed346baSBosko Milekic /* 6249ed346baSBosko Milekic * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock. 6259ed346baSBosko Milekic * 6269ed346baSBosko Milekic * We are only called here if the lock is recursed or contested (i.e. we 6279ed346baSBosko Milekic * need to wake up a blocked thread). 6289ed346baSBosko Milekic */ 62936412d79SJohn Baldwin void 6309ed346baSBosko Milekic _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line) 63136412d79SJohn Baldwin { 632961a7b24SJohn Baldwin struct turnstile *ts; 6330c0b25aeSJohn Baldwin #ifndef PREEMPTION 634b40ce416SJulian Elischer struct thread *td, *td1; 6350c0b25aeSJohn Baldwin #endif 6369ed346baSBosko Milekic 63708812b39SBosko Milekic if (mtx_recursed(m)) { 63836412d79SJohn Baldwin if (--(m->mtx_recurse) == 0) 63908812b39SBosko Milekic atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED); 64019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6419ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m); 64236412d79SJohn Baldwin return; 64336412d79SJohn Baldwin } 6449ed346baSBosko Milekic 6452ff0e645SJohn Baldwin turnstile_lock(&m->mtx_object); 646961a7b24SJohn Baldwin ts = turnstile_lookup(&m->mtx_object); 64719284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6489ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m); 6499ed346baSBosko Milekic 650ece2d989SPawel Jakub Dawidek #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES) 651961a7b24SJohn Baldwin if (ts == NULL) { 6522498cf8cSJohn Baldwin _release_lock_quick(m); 6532498cf8cSJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6542498cf8cSJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m); 655961a7b24SJohn Baldwin turnstile_release(&m->mtx_object); 6562498cf8cSJohn Baldwin return; 6572498cf8cSJohn Baldwin } 658961a7b24SJohn Baldwin #else 659961a7b24SJohn Baldwin MPASS(ts != NULL); 6602498cf8cSJohn Baldwin #endif 6610c0b25aeSJohn Baldwin #ifndef PREEMPTION 662961a7b24SJohn Baldwin /* XXX */ 663961a7b24SJohn Baldwin td1 = turnstile_head(ts); 6640c0b25aeSJohn Baldwin #endif 665535eb309SJohn Baldwin #ifdef MUTEX_WAKE_ALL 666535eb309SJohn Baldwin turnstile_broadcast(ts); 667535eb309SJohn Baldwin _release_lock_quick(m); 668535eb309SJohn Baldwin #else 669961a7b24SJohn Baldwin if (turnstile_signal(ts)) { 67036412d79SJohn Baldwin _release_lock_quick(m); 67119284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 6729ed346baSBosko Milekic CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m); 673961a7b24SJohn Baldwin } else { 674f7ee1590SJohn Baldwin m->mtx_lock = MTX_CONTESTED; 67519284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 676961a7b24SJohn Baldwin CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested", 677961a7b24SJohn Baldwin m); 678e0817317SJulian Elischer } 679535eb309SJohn Baldwin #endif 680961a7b24SJohn Baldwin turnstile_unpend(ts); 6819ed346baSBosko Milekic 6820c0b25aeSJohn Baldwin #ifndef PREEMPTION 683961a7b24SJohn Baldwin /* 684961a7b24SJohn Baldwin * XXX: This is just a hack until preemption is done. However, 685961a7b24SJohn Baldwin * once preemption is done we need to either wrap the 686961a7b24SJohn Baldwin * turnstile_signal() and release of the actual lock in an 687961a7b24SJohn Baldwin * extra critical section or change the preemption code to 688961a7b24SJohn Baldwin * always just set a flag and never do instant-preempts. 689961a7b24SJohn Baldwin */ 690961a7b24SJohn Baldwin td = curthread; 691961a7b24SJohn Baldwin if (td->td_critnest > 0 || td1->td_priority >= td->td_priority) 692961a7b24SJohn Baldwin return; 693961a7b24SJohn Baldwin mtx_lock_spin(&sched_lock); 694961a7b24SJohn Baldwin if (!TD_IS_RUNNING(td1)) { 69536412d79SJohn Baldwin #ifdef notyet 696b40ce416SJulian Elischer if (td->td_ithd != NULL) { 697b40ce416SJulian Elischer struct ithd *it = td->td_ithd; 69836412d79SJohn Baldwin 69936412d79SJohn Baldwin if (it->it_interrupted) { 70019284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 70136412d79SJohn Baldwin CTR2(KTR_LOCK, 70215ec816aSJohn Baldwin "_mtx_unlock_sleep: %p interrupted %p", 70336412d79SJohn Baldwin it, it->it_interrupted); 70436412d79SJohn Baldwin intr_thd_fixup(it); 70536412d79SJohn Baldwin } 70636412d79SJohn Baldwin } 70736412d79SJohn Baldwin #endif 70819284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 709562e4ffeSJohn Baldwin CTR2(KTR_LOCK, 7109ed346baSBosko Milekic "_mtx_unlock_sleep: %p switching out lock=%p", m, 7119ed346baSBosko Milekic (void *)m->mtx_lock); 7129ed346baSBosko Milekic 713bf0acc27SJohn Baldwin mi_switch(SW_INVOL, NULL); 71419284646SJohn Baldwin if (LOCK_LOG_TEST(&m->mtx_object, opts)) 7159ed346baSBosko Milekic CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p", 71631271627SJohn Baldwin m, (void *)m->mtx_lock); 71736412d79SJohn Baldwin } 7189ed346baSBosko Milekic mtx_unlock_spin(&sched_lock); 7190c0b25aeSJohn Baldwin #endif 7209ed346baSBosko Milekic 7219ed346baSBosko Milekic return; 7229ed346baSBosko Milekic } 7239ed346baSBosko Milekic 7249ed346baSBosko Milekic /* 7259ed346baSBosko Milekic * All the unlocking of MTX_SPIN locks is done inline. 7269ed346baSBosko Milekic * See the _rel_spin_lock() macro for the details. 7279ed346baSBosko Milekic */ 7289ed346baSBosko Milekic 7299ed346baSBosko Milekic /* 73015ec816aSJohn Baldwin * The backing function for the INVARIANTS-enabled mtx_assert() 7319ed346baSBosko Milekic */ 7321103f3b0SJohn Baldwin #ifdef INVARIANT_SUPPORT 7330cde2e34SJason Evans void 73456771ca7SJason Evans _mtx_assert(struct mtx *m, int what, const char *file, int line) 7350cde2e34SJason Evans { 7365cb0fbe4SJohn Baldwin 7371126349aSPaul Saab if (panicstr != NULL || dumping) 7385cb0fbe4SJohn Baldwin return; 739a10f4966SJake Burkholder switch (what) { 7400cde2e34SJason Evans case MA_OWNED: 7410cde2e34SJason Evans case MA_OWNED | MA_RECURSED: 7420cde2e34SJason Evans case MA_OWNED | MA_NOTRECURSED: 743a10f4966SJake Burkholder if (!mtx_owned(m)) 7440cde2e34SJason Evans panic("mutex %s not owned at %s:%d", 74519284646SJohn Baldwin m->mtx_object.lo_name, file, line); 746a10f4966SJake Burkholder if (mtx_recursed(m)) { 747a10f4966SJake Burkholder if ((what & MA_NOTRECURSED) != 0) 7480cde2e34SJason Evans panic("mutex %s recursed at %s:%d", 74919284646SJohn Baldwin m->mtx_object.lo_name, file, line); 750a10f4966SJake Burkholder } else if ((what & MA_RECURSED) != 0) { 7510cde2e34SJason Evans panic("mutex %s unrecursed at %s:%d", 75219284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7530cde2e34SJason Evans } 7540cde2e34SJason Evans break; 7550cde2e34SJason Evans case MA_NOTOWNED: 756a10f4966SJake Burkholder if (mtx_owned(m)) 7570cde2e34SJason Evans panic("mutex %s owned at %s:%d", 75819284646SJohn Baldwin m->mtx_object.lo_name, file, line); 7590cde2e34SJason Evans break; 7600cde2e34SJason Evans default: 76156771ca7SJason Evans panic("unknown mtx_assert at %s:%d", file, line); 7620cde2e34SJason Evans } 7630cde2e34SJason Evans } 7640cde2e34SJason Evans #endif 7650cde2e34SJason Evans 7669ed346baSBosko Milekic /* 7679ed346baSBosko Milekic * The MUTEX_DEBUG-enabled mtx_validate() 76819284646SJohn Baldwin * 76919284646SJohn Baldwin * Most of these checks have been moved off into the LO_INITIALIZED flag 77019284646SJohn Baldwin * maintained by the witness code. 7719ed346baSBosko Milekic */ 77236412d79SJohn Baldwin #ifdef MUTEX_DEBUG 77336412d79SJohn Baldwin 7744d77a549SAlfred Perlstein void mtx_validate(struct mtx *); 77536412d79SJohn Baldwin 77619284646SJohn Baldwin void 77719284646SJohn Baldwin mtx_validate(struct mtx *m) 77836412d79SJohn Baldwin { 77936412d79SJohn Baldwin 78036412d79SJohn Baldwin /* 781fa669ab7SPoul-Henning Kamp * XXX: When kernacc() does not require Giant we can reenable this check 782fa669ab7SPoul-Henning Kamp */ 783fa669ab7SPoul-Henning Kamp #ifdef notyet 784fa669ab7SPoul-Henning Kamp /* 78536412d79SJohn Baldwin * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly 78636412d79SJohn Baldwin * we can re-enable the kernacc() checks. 78736412d79SJohn Baldwin */ 78836412d79SJohn Baldwin #ifndef __alpha__ 78976dcbd6fSBosko Milekic /* 79076dcbd6fSBosko Milekic * Can't call kernacc() from early init386(), especially when 79176dcbd6fSBosko Milekic * initializing Giant mutex, because some stuff in kernacc() 79276dcbd6fSBosko Milekic * requires Giant itself. 79376dcbd6fSBosko Milekic */ 794ab07087eSBosko Milekic if (!cold) 795ab07087eSBosko Milekic if (!kernacc((caddr_t)m, sizeof(m), 796ab07087eSBosko Milekic VM_PROT_READ | VM_PROT_WRITE)) 79719284646SJohn Baldwin panic("Can't read and write to mutex %p", m); 79836412d79SJohn Baldwin #endif 799fa669ab7SPoul-Henning Kamp #endif 80036412d79SJohn Baldwin } 80136412d79SJohn Baldwin #endif 80236412d79SJohn Baldwin 8039ed346baSBosko Milekic /* 804c27b5699SAndrew R. Reiter * General init routine used by the MTX_SYSINIT() macro. 805c27b5699SAndrew R. Reiter */ 806c27b5699SAndrew R. Reiter void 807c27b5699SAndrew R. Reiter mtx_sysinit(void *arg) 808c27b5699SAndrew R. Reiter { 809c27b5699SAndrew R. Reiter struct mtx_args *margs = arg; 810c27b5699SAndrew R. Reiter 8110c88508aSJohn Baldwin mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts); 812c27b5699SAndrew R. Reiter } 813c27b5699SAndrew R. Reiter 814c27b5699SAndrew R. Reiter /* 8159ed346baSBosko Milekic * Mutex initialization routine; initialize lock `m' of type contained in 8160c88508aSJohn Baldwin * `opts' with options contained in `opts' and name `name.' The optional 8170c88508aSJohn Baldwin * lock type `type' is used as a general lock category name for use with 8180c88508aSJohn Baldwin * witness. 8199ed346baSBosko Milekic */ 82036412d79SJohn Baldwin void 8210c88508aSJohn Baldwin mtx_init(struct mtx *m, const char *name, const char *type, int opts) 82236412d79SJohn Baldwin { 82319284646SJohn Baldwin struct lock_object *lock; 8249ed346baSBosko Milekic 82519284646SJohn Baldwin MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE | 82675d468eeSJohn Baldwin MTX_NOWITNESS | MTX_DUPOK)) == 0); 8279ed346baSBosko Milekic 82836412d79SJohn Baldwin #ifdef MUTEX_DEBUG 8299ed346baSBosko Milekic /* Diagnostic and error correction */ 83019284646SJohn Baldwin mtx_validate(m); 8316936206eSJohn Baldwin #endif 83236412d79SJohn Baldwin 83319284646SJohn Baldwin lock = &m->mtx_object; 8347ada5876SJohn Baldwin KASSERT((lock->lo_flags & LO_INITIALIZED) == 0, 835b82af320SPoul-Henning Kamp ("mutex \"%s\" %p already initialized", name, m)); 8367ada5876SJohn Baldwin bzero(m, sizeof(*m)); 83719284646SJohn Baldwin if (opts & MTX_SPIN) 83819284646SJohn Baldwin lock->lo_class = &lock_class_mtx_spin; 83919284646SJohn Baldwin else 84019284646SJohn Baldwin lock->lo_class = &lock_class_mtx_sleep; 8410c88508aSJohn Baldwin lock->lo_name = name; 8420c88508aSJohn Baldwin lock->lo_type = type != NULL ? type : name; 84319284646SJohn Baldwin if (opts & MTX_QUIET) 84419284646SJohn Baldwin lock->lo_flags = LO_QUIET; 84519284646SJohn Baldwin if (opts & MTX_RECURSE) 84619284646SJohn Baldwin lock->lo_flags |= LO_RECURSABLE; 84719284646SJohn Baldwin if ((opts & MTX_NOWITNESS) == 0) 84819284646SJohn Baldwin lock->lo_flags |= LO_WITNESS; 849f22a4b62SJeff Roberson if (opts & MTX_DUPOK) 850f22a4b62SJeff Roberson lock->lo_flags |= LO_DUPOK; 85119284646SJohn Baldwin 85219284646SJohn Baldwin m->mtx_lock = MTX_UNOWNED; 8539ed346baSBosko Milekic 85419284646SJohn Baldwin LOCK_LOG_INIT(lock, opts); 855d1c1b841SJason Evans 85619284646SJohn Baldwin WITNESS_INIT(lock); 85736412d79SJohn Baldwin } 85836412d79SJohn Baldwin 8599ed346baSBosko Milekic /* 86019284646SJohn Baldwin * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be 86119284646SJohn Baldwin * passed in as a flag here because if the corresponding mtx_init() was 86219284646SJohn Baldwin * called with MTX_QUIET set, then it will already be set in the mutex's 86319284646SJohn Baldwin * flags. 8649ed346baSBosko Milekic */ 86536412d79SJohn Baldwin void 86636412d79SJohn Baldwin mtx_destroy(struct mtx *m) 86736412d79SJohn Baldwin { 86836412d79SJohn Baldwin 86919284646SJohn Baldwin LOCK_LOG_DESTROY(&m->mtx_object, 0); 8709ed346baSBosko Milekic 87119284646SJohn Baldwin if (!mtx_owned(m)) 87219284646SJohn Baldwin MPASS(mtx_unowned(m)); 87319284646SJohn Baldwin else { 87408812b39SBosko Milekic MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0); 8759ed346baSBosko Milekic 87619284646SJohn Baldwin /* Tell witness this isn't locked to make it happy. */ 877c86b6ff5SJohn Baldwin WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__, 878c86b6ff5SJohn Baldwin __LINE__); 87936412d79SJohn Baldwin } 8800384fff8SJason Evans 88119284646SJohn Baldwin WITNESS_DESTROY(&m->mtx_object); 8820384fff8SJason Evans } 883d23f5958SMatthew Dillon 884d23f5958SMatthew Dillon /* 885c53c013bSJohn Baldwin * Intialize the mutex code and system mutexes. This is called from the MD 886c53c013bSJohn Baldwin * startup code prior to mi_startup(). The per-CPU data space needs to be 887c53c013bSJohn Baldwin * setup before this is called. 888c53c013bSJohn Baldwin */ 889c53c013bSJohn Baldwin void 890c53c013bSJohn Baldwin mutex_init(void) 891c53c013bSJohn Baldwin { 892c53c013bSJohn Baldwin 893c53c013bSJohn Baldwin /* Setup thread0 so that mutexes work. */ 894c53c013bSJohn Baldwin LIST_INIT(&thread0.td_contested); 895c53c013bSJohn Baldwin 896961a7b24SJohn Baldwin /* Setup turnstiles so that sleep mutexes work. */ 897961a7b24SJohn Baldwin init_turnstiles(); 898961a7b24SJohn Baldwin 899c53c013bSJohn Baldwin /* 900c53c013bSJohn Baldwin * Initialize mutexes. 901c53c013bSJohn Baldwin */ 9020c88508aSJohn Baldwin mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE); 9030c88508aSJohn Baldwin mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE); 9040c88508aSJohn Baldwin mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK); 9058c4b6380SJohn Baldwin mtx_init(&devmtx, "cdev", NULL, MTX_DEF); 906c53c013bSJohn Baldwin mtx_lock(&Giant); 907c53c013bSJohn Baldwin } 908