19454b2d8SWarner Losh /*- 24e7f640dSJohn Baldwin * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org> 34e7f640dSJohn Baldwin * Copyright (c) 2001 Jason Evans <jasone@freebsd.org> 44e7f640dSJohn Baldwin * All rights reserved. 56281b30aSJason Evans * 66281b30aSJason Evans * Redistribution and use in source and binary forms, with or without 76281b30aSJason Evans * modification, are permitted provided that the following conditions 86281b30aSJason Evans * are met: 96281b30aSJason Evans * 1. Redistributions of source code must retain the above copyright 106281b30aSJason Evans * notice(s), this list of conditions and the following disclaimer as 116281b30aSJason Evans * the first lines of this file unmodified other than the possible 126281b30aSJason Evans * addition of one or more copyright notices. 136281b30aSJason Evans * 2. Redistributions in binary form must reproduce the above copyright 146281b30aSJason Evans * notice(s), this list of conditions and the following disclaimer in the 156281b30aSJason Evans * documentation and/or other materials provided with the distribution. 166281b30aSJason Evans * 176281b30aSJason Evans * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 186281b30aSJason Evans * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 196281b30aSJason Evans * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 206281b30aSJason Evans * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 216281b30aSJason Evans * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 226281b30aSJason Evans * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 236281b30aSJason Evans * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 246281b30aSJason Evans * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 256281b30aSJason Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 266281b30aSJason Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 276281b30aSJason Evans * DAMAGE. 286281b30aSJason Evans */ 296281b30aSJason Evans 306281b30aSJason Evans /* 314e7f640dSJohn Baldwin * Shared/exclusive locks. This implementation attempts to ensure 324e7f640dSJohn Baldwin * deterministic lock granting behavior, so that slocks and xlocks are 334e7f640dSJohn Baldwin * interleaved. 346281b30aSJason Evans * 356281b30aSJason Evans * Priority propagation will not generally raise the priority of lock holders, 366281b30aSJason Evans * so should not be relied upon in combination with sx locks. 376281b30aSJason Evans */ 386281b30aSJason Evans 394e7f640dSJohn Baldwin #include "opt_ddb.h" 40a5aedd68SStacey Son #include "opt_kdtrace.h" 41e31d0833SAttilio Rao #include "opt_no_adaptive_sx.h" 424e7f640dSJohn Baldwin 43677b542eSDavid E. O'Brien #include <sys/cdefs.h> 44677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 45677b542eSDavid E. O'Brien 466281b30aSJason Evans #include <sys/param.h> 476281b30aSJason Evans #include <sys/ktr.h> 48e31d0833SAttilio Rao #include <sys/linker_set.h> 4919284646SJohn Baldwin #include <sys/lock.h> 506281b30aSJason Evans #include <sys/mutex.h> 51d272fe53SJohn Baldwin #include <sys/proc.h> 524e7f640dSJohn Baldwin #include <sys/sleepqueue.h> 536281b30aSJason Evans #include <sys/sx.h> 54e31d0833SAttilio Rao #include <sys/sysctl.h> 554e7f640dSJohn Baldwin #include <sys/systm.h> 564e7f640dSJohn Baldwin 57e31d0833SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 584e7f640dSJohn Baldwin #include <machine/cpu.h> 594e7f640dSJohn Baldwin #endif 606281b30aSJason Evans 61462a7addSJohn Baldwin #ifdef DDB 62d272fe53SJohn Baldwin #include <ddb/ddb.h> 634e7f640dSJohn Baldwin #endif 64d272fe53SJohn Baldwin 651ae1c2a3SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 661ae1c2a3SAttilio Rao #define ADAPTIVE_SX 674e7f640dSJohn Baldwin #endif 684e7f640dSJohn Baldwin 69f0830182SAttilio Rao CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE); 70c1a6d9faSAttilio Rao 714e7f640dSJohn Baldwin /* Handy macros for sleep queues. */ 724e7f640dSJohn Baldwin #define SQ_EXCLUSIVE_QUEUE 0 734e7f640dSJohn Baldwin #define SQ_SHARED_QUEUE 1 744e7f640dSJohn Baldwin 754e7f640dSJohn Baldwin /* 764e7f640dSJohn Baldwin * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We 774e7f640dSJohn Baldwin * drop Giant anytime we have to sleep or if we adaptively spin. 784e7f640dSJohn Baldwin */ 794e7f640dSJohn Baldwin #define GIANT_DECLARE \ 804e7f640dSJohn Baldwin int _giantcnt = 0; \ 814e7f640dSJohn Baldwin WITNESS_SAVE_DECL(Giant) \ 824e7f640dSJohn Baldwin 834e7f640dSJohn Baldwin #define GIANT_SAVE() do { \ 844e7f640dSJohn Baldwin if (mtx_owned(&Giant)) { \ 854e7f640dSJohn Baldwin WITNESS_SAVE(&Giant.lock_object, Giant); \ 864e7f640dSJohn Baldwin while (mtx_owned(&Giant)) { \ 874e7f640dSJohn Baldwin _giantcnt++; \ 884e7f640dSJohn Baldwin mtx_unlock(&Giant); \ 894e7f640dSJohn Baldwin } \ 904e7f640dSJohn Baldwin } \ 914e7f640dSJohn Baldwin } while (0) 924e7f640dSJohn Baldwin 934e7f640dSJohn Baldwin #define GIANT_RESTORE() do { \ 944e7f640dSJohn Baldwin if (_giantcnt > 0) { \ 954e7f640dSJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); \ 964e7f640dSJohn Baldwin while (_giantcnt--) \ 974e7f640dSJohn Baldwin mtx_lock(&Giant); \ 984e7f640dSJohn Baldwin WITNESS_RESTORE(&Giant.lock_object, Giant); \ 994e7f640dSJohn Baldwin } \ 1004e7f640dSJohn Baldwin } while (0) 1014e7f640dSJohn Baldwin 1024e7f640dSJohn Baldwin /* 103da7d0d1eSJohn Baldwin * Returns true if an exclusive lock is recursed. It assumes 104da7d0d1eSJohn Baldwin * curthread currently has an exclusive lock. 1054e7f640dSJohn Baldwin */ 10690356491SAttilio Rao #define sx_recurse lock_object.lo_data 1074e7f640dSJohn Baldwin #define sx_recursed(sx) ((sx)->sx_recurse != 0) 1084e7f640dSJohn Baldwin 109f9721b43SAttilio Rao static void assert_sx(struct lock_object *lock, int what); 1104e7f640dSJohn Baldwin #ifdef DDB 111d272fe53SJohn Baldwin static void db_show_sx(struct lock_object *lock); 112d272fe53SJohn Baldwin #endif 1136e21afd4SJohn Baldwin static void lock_sx(struct lock_object *lock, int how); 114a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 115a5aedd68SStacey Son static int owner_sx(struct lock_object *lock, struct thread **owner); 116a5aedd68SStacey Son #endif 1176e21afd4SJohn Baldwin static int unlock_sx(struct lock_object *lock); 118d272fe53SJohn Baldwin 11919284646SJohn Baldwin struct lock_class lock_class_sx = { 120ae8dde30SJohn Baldwin .lc_name = "sx", 121ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, 122f9721b43SAttilio Rao .lc_assert = assert_sx, 123d272fe53SJohn Baldwin #ifdef DDB 124ae8dde30SJohn Baldwin .lc_ddb_show = db_show_sx, 125d272fe53SJohn Baldwin #endif 1266e21afd4SJohn Baldwin .lc_lock = lock_sx, 1276e21afd4SJohn Baldwin .lc_unlock = unlock_sx, 128a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 129a5aedd68SStacey Son .lc_owner = owner_sx, 130a5aedd68SStacey Son #endif 13119284646SJohn Baldwin }; 13219284646SJohn Baldwin 133781a35dfSJohn Baldwin #ifndef INVARIANTS 134781a35dfSJohn Baldwin #define _sx_assert(sx, what, file, line) 135781a35dfSJohn Baldwin #endif 136781a35dfSJohn Baldwin 1371ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX 1381ae1c2a3SAttilio Rao static u_int asx_retries = 10; 1391ae1c2a3SAttilio Rao static u_int asx_loops = 10000; 1401ae1c2a3SAttilio Rao SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging"); 1411ae1c2a3SAttilio Rao SYSCTL_INT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, ""); 1421ae1c2a3SAttilio Rao SYSCTL_INT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, ""); 1431ae1c2a3SAttilio Rao #endif 1441ae1c2a3SAttilio Rao 1456281b30aSJason Evans void 146f9721b43SAttilio Rao assert_sx(struct lock_object *lock, int what) 147f9721b43SAttilio Rao { 148f9721b43SAttilio Rao 149f9721b43SAttilio Rao sx_assert((struct sx *)lock, what); 150f9721b43SAttilio Rao } 151f9721b43SAttilio Rao 152f9721b43SAttilio Rao void 1536e21afd4SJohn Baldwin lock_sx(struct lock_object *lock, int how) 1546e21afd4SJohn Baldwin { 1556e21afd4SJohn Baldwin struct sx *sx; 1566e21afd4SJohn Baldwin 1576e21afd4SJohn Baldwin sx = (struct sx *)lock; 1586e21afd4SJohn Baldwin if (how) 1596e21afd4SJohn Baldwin sx_xlock(sx); 1606e21afd4SJohn Baldwin else 1616e21afd4SJohn Baldwin sx_slock(sx); 1626e21afd4SJohn Baldwin } 1636e21afd4SJohn Baldwin 1646e21afd4SJohn Baldwin int 1656e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock) 1666e21afd4SJohn Baldwin { 1676e21afd4SJohn Baldwin struct sx *sx; 1686e21afd4SJohn Baldwin 1696e21afd4SJohn Baldwin sx = (struct sx *)lock; 1707ec137e5SJohn Baldwin sx_assert(sx, SA_LOCKED | SA_NOTRECURSED); 1716e21afd4SJohn Baldwin if (sx_xlocked(sx)) { 1726e21afd4SJohn Baldwin sx_xunlock(sx); 1736e21afd4SJohn Baldwin return (1); 1746e21afd4SJohn Baldwin } else { 1756e21afd4SJohn Baldwin sx_sunlock(sx); 1766e21afd4SJohn Baldwin return (0); 1776e21afd4SJohn Baldwin } 1786e21afd4SJohn Baldwin } 1796e21afd4SJohn Baldwin 180a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 181a5aedd68SStacey Son int 182a5aedd68SStacey Son owner_sx(struct lock_object *lock, struct thread **owner) 183a5aedd68SStacey Son { 184a5aedd68SStacey Son struct sx *sx = (struct sx *)lock; 185a5aedd68SStacey Son uintptr_t x = sx->sx_lock; 186a5aedd68SStacey Son 187a5aedd68SStacey Son *owner = (struct thread *)SX_OWNER(x); 188a5aedd68SStacey Son return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) : 189a5aedd68SStacey Son (*owner != NULL)); 190a5aedd68SStacey Son } 191a5aedd68SStacey Son #endif 192a5aedd68SStacey Son 1936e21afd4SJohn Baldwin void 194c27b5699SAndrew R. Reiter sx_sysinit(void *arg) 195c27b5699SAndrew R. Reiter { 196c27b5699SAndrew R. Reiter struct sx_args *sargs = arg; 197c27b5699SAndrew R. Reiter 198c27b5699SAndrew R. Reiter sx_init(sargs->sa_sx, sargs->sa_desc); 199c27b5699SAndrew R. Reiter } 200c27b5699SAndrew R. Reiter 201c27b5699SAndrew R. Reiter void 2024e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts) 2036281b30aSJason Evans { 2044e7f640dSJohn Baldwin int flags; 2056281b30aSJason Evans 206b0d67325SJohn Baldwin MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK | 2071ae1c2a3SAttilio Rao SX_NOPROFILE | SX_NOADAPTIVE)) == 0); 208353998acSAttilio Rao ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock, 209353998acSAttilio Rao ("%s: sx_lock not aligned for %s: %p", __func__, description, 210353998acSAttilio Rao &sx->sx_lock)); 211b0d67325SJohn Baldwin 212f0830182SAttilio Rao flags = LO_SLEEPABLE | LO_UPGRADABLE; 2134e7f640dSJohn Baldwin if (opts & SX_DUPOK) 2144e7f640dSJohn Baldwin flags |= LO_DUPOK; 2154e7f640dSJohn Baldwin if (opts & SX_NOPROFILE) 2164e7f640dSJohn Baldwin flags |= LO_NOPROFILE; 2174e7f640dSJohn Baldwin if (!(opts & SX_NOWITNESS)) 2184e7f640dSJohn Baldwin flags |= LO_WITNESS; 219f0830182SAttilio Rao if (opts & SX_RECURSE) 220f0830182SAttilio Rao flags |= LO_RECURSABLE; 2214e7f640dSJohn Baldwin if (opts & SX_QUIET) 2224e7f640dSJohn Baldwin flags |= LO_QUIET; 2234e7f640dSJohn Baldwin 224f0830182SAttilio Rao flags |= opts & SX_NOADAPTIVE; 2254e7f640dSJohn Baldwin sx->sx_lock = SX_LOCK_UNLOCKED; 2264e7f640dSJohn Baldwin sx->sx_recurse = 0; 2274e7f640dSJohn Baldwin lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags); 2286281b30aSJason Evans } 2296281b30aSJason Evans 2306281b30aSJason Evans void 2316281b30aSJason Evans sx_destroy(struct sx *sx) 2326281b30aSJason Evans { 2336281b30aSJason Evans 2344e7f640dSJohn Baldwin KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held")); 2354e7f640dSJohn Baldwin KASSERT(sx->sx_recurse == 0, ("sx lock still recursed")); 2360026c92cSJohn Baldwin sx->sx_lock = SX_LOCK_DESTROYED; 237aa89d8cdSJohn Baldwin lock_destroy(&sx->lock_object); 2386281b30aSJason Evans } 2396281b30aSJason Evans 240f9819486SAttilio Rao int 241f9819486SAttilio Rao _sx_slock(struct sx *sx, int opts, const char *file, int line) 2426281b30aSJason Evans { 243f9819486SAttilio Rao int error = 0; 2446281b30aSJason Evans 2454e7f640dSJohn Baldwin MPASS(curthread != NULL); 2460026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 2470026c92cSJohn Baldwin ("sx_slock() of destroyed sx @ %s:%d", file, line)); 24841313430SJohn Baldwin WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 249f9819486SAttilio Rao error = __sx_slock(sx, opts, file, line); 250f9819486SAttilio Rao if (!error) { 251aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 252aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, 0, file, line); 253764e4d54SJohn Baldwin curthread->td_locks++; 2546281b30aSJason Evans } 2556281b30aSJason Evans 256f9819486SAttilio Rao return (error); 257f9819486SAttilio Rao } 258f9819486SAttilio Rao 2595f36700aSJohn Baldwin int 2605f36700aSJohn Baldwin _sx_try_slock(struct sx *sx, const char *file, int line) 2615f36700aSJohn Baldwin { 2624e7f640dSJohn Baldwin uintptr_t x; 2635f36700aSJohn Baldwin 264764a938bSPawel Jakub Dawidek for (;;) { 2654e7f640dSJohn Baldwin x = sx->sx_lock; 2660026c92cSJohn Baldwin KASSERT(x != SX_LOCK_DESTROYED, 2670026c92cSJohn Baldwin ("sx_try_slock() of destroyed sx @ %s:%d", file, line)); 268764a938bSPawel Jakub Dawidek if (!(x & SX_LOCK_SHARED)) 269764a938bSPawel Jakub Dawidek break; 270764a938bSPawel Jakub Dawidek if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) { 271aa89d8cdSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line); 272aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line); 273764e4d54SJohn Baldwin curthread->td_locks++; 2745f36700aSJohn Baldwin return (1); 2755f36700aSJohn Baldwin } 276764a938bSPawel Jakub Dawidek } 2774e7f640dSJohn Baldwin 2784e7f640dSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line); 2794e7f640dSJohn Baldwin return (0); 2805f36700aSJohn Baldwin } 2815f36700aSJohn Baldwin 282f9819486SAttilio Rao int 283f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line) 2846281b30aSJason Evans { 285f9819486SAttilio Rao int error = 0; 2866281b30aSJason Evans 2874e7f640dSJohn Baldwin MPASS(curthread != NULL); 2880026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 2890026c92cSJohn Baldwin ("sx_xlock() of destroyed sx @ %s:%d", file, line)); 290aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 29141313430SJohn Baldwin line, NULL); 292f9819486SAttilio Rao error = __sx_xlock(sx, curthread, opts, file, line); 293f9819486SAttilio Rao if (!error) { 294f9819486SAttilio Rao LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, 295f9819486SAttilio Rao file, line); 296aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 297764e4d54SJohn Baldwin curthread->td_locks++; 2986281b30aSJason Evans } 2996281b30aSJason Evans 300f9819486SAttilio Rao return (error); 301f9819486SAttilio Rao } 302f9819486SAttilio Rao 3035f36700aSJohn Baldwin int 3045f36700aSJohn Baldwin _sx_try_xlock(struct sx *sx, const char *file, int line) 3055f36700aSJohn Baldwin { 3064e7f640dSJohn Baldwin int rval; 3075f36700aSJohn Baldwin 3084e7f640dSJohn Baldwin MPASS(curthread != NULL); 3090026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3100026c92cSJohn Baldwin ("sx_try_xlock() of destroyed sx @ %s:%d", file, line)); 3114e7f640dSJohn Baldwin 312f0830182SAttilio Rao if (sx_xlocked(sx) && 313f0830182SAttilio Rao (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) { 3144e7f640dSJohn Baldwin sx->sx_recurse++; 3154e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 3164e7f640dSJohn Baldwin rval = 1; 3174e7f640dSJohn Baldwin } else 3184e7f640dSJohn Baldwin rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, 3194e7f640dSJohn Baldwin (uintptr_t)curthread); 3204e7f640dSJohn Baldwin LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line); 3214e7f640dSJohn Baldwin if (rval) { 3224e7f640dSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 3234e7f640dSJohn Baldwin file, line); 324764e4d54SJohn Baldwin curthread->td_locks++; 3255f36700aSJohn Baldwin } 3264e7f640dSJohn Baldwin 3274e7f640dSJohn Baldwin return (rval); 3285f36700aSJohn Baldwin } 3295f36700aSJohn Baldwin 3306281b30aSJason Evans void 33119284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line) 3326281b30aSJason Evans { 3336281b30aSJason Evans 3344e7f640dSJohn Baldwin MPASS(curthread != NULL); 3350026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3360026c92cSJohn Baldwin ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 3377ec137e5SJohn Baldwin _sx_assert(sx, SA_SLOCKED, file, line); 338764e4d54SJohn Baldwin curthread->td_locks--; 339aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 340aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 3414e7f640dSJohn Baldwin __sx_sunlock(sx, file, line); 342a5aedd68SStacey Son LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx); 3436281b30aSJason Evans } 3446281b30aSJason Evans 3456281b30aSJason Evans void 34619284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line) 3476281b30aSJason Evans { 3486281b30aSJason Evans 3494e7f640dSJohn Baldwin MPASS(curthread != NULL); 3500026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3510026c92cSJohn Baldwin ("sx_xunlock() of destroyed sx @ %s:%d", file, line)); 3527ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED, file, line); 353764e4d54SJohn Baldwin curthread->td_locks--; 354aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 3554e7f640dSJohn Baldwin LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file, 3564e7f640dSJohn Baldwin line); 357afc0bfbdSKip Macy if (!sx_recursed(sx)) 358a5aedd68SStacey Son LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx); 3594e7f640dSJohn Baldwin __sx_xunlock(sx, curthread, file, line); 3606281b30aSJason Evans } 361d55229b7SJason Evans 3624e7f640dSJohn Baldwin /* 3634e7f640dSJohn Baldwin * Try to do a non-blocking upgrade from a shared lock to an exclusive lock. 3644e7f640dSJohn Baldwin * This will only succeed if this thread holds a single shared lock. 3654e7f640dSJohn Baldwin * Return 1 if if the upgrade succeed, 0 otherwise. 3664e7f640dSJohn Baldwin */ 367d55229b7SJason Evans int 368d55229b7SJason Evans _sx_try_upgrade(struct sx *sx, const char *file, int line) 369d55229b7SJason Evans { 3704e7f640dSJohn Baldwin uintptr_t x; 3714e7f640dSJohn Baldwin int success; 372d55229b7SJason Evans 3730026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3740026c92cSJohn Baldwin ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 3757ec137e5SJohn Baldwin _sx_assert(sx, SA_SLOCKED, file, line); 376d55229b7SJason Evans 3774e7f640dSJohn Baldwin /* 3784e7f640dSJohn Baldwin * Try to switch from one shared lock to an exclusive lock. We need 3794e7f640dSJohn Baldwin * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 3804e7f640dSJohn Baldwin * we will wake up the exclusive waiters when we drop the lock. 3814e7f640dSJohn Baldwin */ 3824e7f640dSJohn Baldwin x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS; 3834e7f640dSJohn Baldwin success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, 3844e7f640dSJohn Baldwin (uintptr_t)curthread | x); 3854e7f640dSJohn Baldwin LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 386a5aedd68SStacey Son if (success) { 387aa89d8cdSJohn Baldwin WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 388b0b7cb50SJohn Baldwin file, line); 389a5aedd68SStacey Son LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx); 390a5aedd68SStacey Son } 3914e7f640dSJohn Baldwin return (success); 392d55229b7SJason Evans } 393d55229b7SJason Evans 3944e7f640dSJohn Baldwin /* 3954e7f640dSJohn Baldwin * Downgrade an unrecursed exclusive lock into a single shared lock. 3964e7f640dSJohn Baldwin */ 397d55229b7SJason Evans void 398d55229b7SJason Evans _sx_downgrade(struct sx *sx, const char *file, int line) 399d55229b7SJason Evans { 4004e7f640dSJohn Baldwin uintptr_t x; 401da7bbd2cSJohn Baldwin int wakeup_swapper; 402d55229b7SJason Evans 4030026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4040026c92cSJohn Baldwin ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 4057ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 4064e7f640dSJohn Baldwin #ifndef INVARIANTS 4074e7f640dSJohn Baldwin if (sx_recursed(sx)) 4084e7f640dSJohn Baldwin panic("downgrade of a recursed lock"); 4094e7f640dSJohn Baldwin #endif 410d55229b7SJason Evans 411aa89d8cdSJohn Baldwin WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 412d55229b7SJason Evans 4134e7f640dSJohn Baldwin /* 4144e7f640dSJohn Baldwin * Try to switch from an exclusive lock with no shared waiters 4154e7f640dSJohn Baldwin * to one sharer with no shared waiters. If there are 4164e7f640dSJohn Baldwin * exclusive waiters, we don't need to lock the sleep queue so 4174e7f640dSJohn Baldwin * long as we preserve the flag. We do one quick try and if 4184e7f640dSJohn Baldwin * that fails we grab the sleepq lock to keep the flags from 4194e7f640dSJohn Baldwin * changing and do it the slow way. 4204e7f640dSJohn Baldwin * 4214e7f640dSJohn Baldwin * We have to lock the sleep queue if there are shared waiters 4224e7f640dSJohn Baldwin * so we can wake them up. 4234e7f640dSJohn Baldwin */ 4244e7f640dSJohn Baldwin x = sx->sx_lock; 4254e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS) && 4264e7f640dSJohn Baldwin atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 4274e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS))) { 4284e7f640dSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 4294e7f640dSJohn Baldwin return; 4304e7f640dSJohn Baldwin } 4314e7f640dSJohn Baldwin 4324e7f640dSJohn Baldwin /* 4334e7f640dSJohn Baldwin * Lock the sleep queue so we can read the waiters bits 4344e7f640dSJohn Baldwin * without any races and wakeup any shared waiters. 4354e7f640dSJohn Baldwin */ 4364e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 4374e7f640dSJohn Baldwin 4384e7f640dSJohn Baldwin /* 4394e7f640dSJohn Baldwin * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 4404e7f640dSJohn Baldwin * shared lock. If there are any shared waiters, wake them up. 4414e7f640dSJohn Baldwin */ 442da7bbd2cSJohn Baldwin wakeup_swapper = 0; 4434e7f640dSJohn Baldwin x = sx->sx_lock; 4444e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 4454e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS)); 4464e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED_WAITERS) 447da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 448da7bbd2cSJohn Baldwin 0, SQ_SHARED_QUEUE); 4494e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 450d55229b7SJason Evans 451aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 452a5aedd68SStacey Son LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx); 453da7bbd2cSJohn Baldwin 454da7bbd2cSJohn Baldwin if (wakeup_swapper) 455da7bbd2cSJohn Baldwin kick_proc0(); 4564e7f640dSJohn Baldwin } 457d55229b7SJason Evans 4584e7f640dSJohn Baldwin /* 4594e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xlock 4604e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 4614e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 4624e7f640dSJohn Baldwin * accessible from at least sx.h. 4634e7f640dSJohn Baldwin */ 464f9819486SAttilio Rao int 465f9819486SAttilio Rao _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file, 466f9819486SAttilio Rao int line) 4674e7f640dSJohn Baldwin { 4684e7f640dSJohn Baldwin GIANT_DECLARE; 4694e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 4704e7f640dSJohn Baldwin volatile struct thread *owner; 4711ae1c2a3SAttilio Rao u_int i, spintries = 0; 4724e7f640dSJohn Baldwin #endif 4734e7f640dSJohn Baldwin uintptr_t x; 4741723a064SJeff Roberson #ifdef LOCK_PROFILING 4751723a064SJeff Roberson uint64_t waittime = 0; 4761723a064SJeff Roberson int contested = 0; 4771723a064SJeff Roberson #endif 4781723a064SJeff Roberson int error = 0; 479a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 480a5aedd68SStacey Son uint64_t spin_cnt = 0; 481a5aedd68SStacey Son uint64_t sleep_cnt = 0; 482a5aedd68SStacey Son int64_t sleep_time = 0; 483a5aedd68SStacey Son #endif 4844e7f640dSJohn Baldwin 4854e7f640dSJohn Baldwin /* If we already hold an exclusive lock, then recurse. */ 4864e7f640dSJohn Baldwin if (sx_xlocked(sx)) { 487f0830182SAttilio Rao KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0, 488b0d67325SJohn Baldwin ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 489b0d67325SJohn Baldwin sx->lock_object.lo_name, file, line)); 4904e7f640dSJohn Baldwin sx->sx_recurse++; 4914e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 4924e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 4934e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 494f9819486SAttilio Rao return (0); 4954e7f640dSJohn Baldwin } 4964e7f640dSJohn Baldwin 4974e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 4984e7f640dSJohn Baldwin CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 4994e7f640dSJohn Baldwin sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 5004e7f640dSJohn Baldwin 5014e7f640dSJohn Baldwin while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) { 502a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 503a5aedd68SStacey Son spin_cnt++; 504a5aedd68SStacey Son #endif 505eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 506eea4f254SJeff Roberson &waittime); 5074e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5084e7f640dSJohn Baldwin /* 5094e7f640dSJohn Baldwin * If the lock is write locked and the owner is 5104e7f640dSJohn Baldwin * running on another CPU, spin until the owner stops 5114e7f640dSJohn Baldwin * running or the state of the lock changes. 5124e7f640dSJohn Baldwin */ 5134e7f640dSJohn Baldwin x = sx->sx_lock; 5141ae1c2a3SAttilio Rao if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) != 0) { 5151ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0) { 5164e7f640dSJohn Baldwin x = SX_OWNER(x); 5174e7f640dSJohn Baldwin owner = (struct thread *)x; 5184e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 5194e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5204e7f640dSJohn Baldwin CTR3(KTR_LOCK, 5214e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 5224e7f640dSJohn Baldwin __func__, sx, owner); 5234e7f640dSJohn Baldwin GIANT_SAVE(); 5244e7f640dSJohn Baldwin while (SX_OWNER(sx->sx_lock) == x && 525a5aedd68SStacey Son TD_IS_RUNNING(owner)) { 5264e7f640dSJohn Baldwin cpu_spinwait(); 527a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 528a5aedd68SStacey Son spin_cnt++; 529a5aedd68SStacey Son #endif 530a5aedd68SStacey Son } 5314e7f640dSJohn Baldwin continue; 5324e7f640dSJohn Baldwin } 5331ae1c2a3SAttilio Rao } else if (SX_SHARERS(x) && spintries < asx_retries) { 5348d3635c4SAttilio Rao GIANT_SAVE(); 5351ae1c2a3SAttilio Rao spintries++; 5361ae1c2a3SAttilio Rao for (i = 0; i < asx_loops; i++) { 5371ae1c2a3SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5381ae1c2a3SAttilio Rao CTR4(KTR_LOCK, 5391ae1c2a3SAttilio Rao "%s: shared spinning on %p with %u and %u", 5401ae1c2a3SAttilio Rao __func__, sx, spintries, i); 5411ae1c2a3SAttilio Rao x = sx->sx_lock; 5421ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0 || 5431ae1c2a3SAttilio Rao SX_SHARERS(x) == 0) 5441ae1c2a3SAttilio Rao break; 5451ae1c2a3SAttilio Rao cpu_spinwait(); 5461ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS 5471ae1c2a3SAttilio Rao spin_cnt++; 5481ae1c2a3SAttilio Rao #endif 5491ae1c2a3SAttilio Rao } 5501ae1c2a3SAttilio Rao if (i != asx_loops) 5511ae1c2a3SAttilio Rao continue; 5521ae1c2a3SAttilio Rao } 5534e7f640dSJohn Baldwin } 5544e7f640dSJohn Baldwin #endif 5554e7f640dSJohn Baldwin 5564e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 5574e7f640dSJohn Baldwin x = sx->sx_lock; 5584e7f640dSJohn Baldwin 5594e7f640dSJohn Baldwin /* 5604e7f640dSJohn Baldwin * If the lock was released while spinning on the 5614e7f640dSJohn Baldwin * sleep queue chain lock, try again. 5624e7f640dSJohn Baldwin */ 5634e7f640dSJohn Baldwin if (x == SX_LOCK_UNLOCKED) { 5644e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 5654e7f640dSJohn Baldwin continue; 5664e7f640dSJohn Baldwin } 5674e7f640dSJohn Baldwin 5684e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5694e7f640dSJohn Baldwin /* 5704e7f640dSJohn Baldwin * The current lock owner might have started executing 5714e7f640dSJohn Baldwin * on another CPU (or the lock could have changed 5724e7f640dSJohn Baldwin * owners) while we were waiting on the sleep queue 5734e7f640dSJohn Baldwin * chain lock. If so, drop the sleep queue lock and try 5744e7f640dSJohn Baldwin * again. 5754e7f640dSJohn Baldwin */ 5764e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 5771ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 5784e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 5794e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 5804e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 5814e7f640dSJohn Baldwin continue; 5824e7f640dSJohn Baldwin } 5834e7f640dSJohn Baldwin } 5844e7f640dSJohn Baldwin #endif 5854e7f640dSJohn Baldwin 5864e7f640dSJohn Baldwin /* 5874e7f640dSJohn Baldwin * If an exclusive lock was released with both shared 5884e7f640dSJohn Baldwin * and exclusive waiters and a shared waiter hasn't 5894e7f640dSJohn Baldwin * woken up and acquired the lock yet, sx_lock will be 5904e7f640dSJohn Baldwin * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 5914e7f640dSJohn Baldwin * If we see that value, try to acquire it once. Note 5924e7f640dSJohn Baldwin * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 5934e7f640dSJohn Baldwin * as there are other exclusive waiters still. If we 5944e7f640dSJohn Baldwin * fail, restart the loop. 5954e7f640dSJohn Baldwin */ 5964e7f640dSJohn Baldwin if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 5974e7f640dSJohn Baldwin if (atomic_cmpset_acq_ptr(&sx->sx_lock, 5984e7f640dSJohn Baldwin SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS, 5994e7f640dSJohn Baldwin tid | SX_LOCK_EXCLUSIVE_WAITERS)) { 6004e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6014e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p claimed by new writer", 6024e7f640dSJohn Baldwin __func__, sx); 6034e7f640dSJohn Baldwin break; 6044e7f640dSJohn Baldwin } 6054e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6064e7f640dSJohn Baldwin continue; 6074e7f640dSJohn Baldwin } 6084e7f640dSJohn Baldwin 6094e7f640dSJohn Baldwin /* 6104e7f640dSJohn Baldwin * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 6114e7f640dSJohn Baldwin * than loop back and retry. 6124e7f640dSJohn Baldwin */ 6134e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 6144e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 6154e7f640dSJohn Baldwin x | SX_LOCK_EXCLUSIVE_WAITERS)) { 6164e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6174e7f640dSJohn Baldwin continue; 6184e7f640dSJohn Baldwin } 6194e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6204e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 6214e7f640dSJohn Baldwin __func__, sx); 6224e7f640dSJohn Baldwin } 6234e7f640dSJohn Baldwin 6244e7f640dSJohn Baldwin /* 6254e7f640dSJohn Baldwin * Since we have been unable to acquire the exclusive 6264e7f640dSJohn Baldwin * lock and the exclusive waiters flag is set, we have 6274e7f640dSJohn Baldwin * to sleep. 6284e7f640dSJohn Baldwin */ 6294e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6304e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 6314e7f640dSJohn Baldwin __func__, sx); 6324e7f640dSJohn Baldwin 633a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 634a5aedd68SStacey Son sleep_time -= lockstat_nsecs(); 635a5aedd68SStacey Son #endif 6364e7f640dSJohn Baldwin GIANT_SAVE(); 6374e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 638f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 639f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 640f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 641c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 642f9819486SAttilio Rao else 643c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 644a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 645a5aedd68SStacey Son sleep_time += lockstat_nsecs(); 646a5aedd68SStacey Son sleep_cnt++; 647a5aedd68SStacey Son #endif 648f9819486SAttilio Rao if (error) { 649f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 650f9819486SAttilio Rao CTR2(KTR_LOCK, 651f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 652f9819486SAttilio Rao __func__, sx); 653f9819486SAttilio Rao break; 654f9819486SAttilio Rao } 6554e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6564e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 6574e7f640dSJohn Baldwin __func__, sx); 6584e7f640dSJohn Baldwin } 6594e7f640dSJohn Baldwin 6604e7f640dSJohn Baldwin GIANT_RESTORE(); 661f9819486SAttilio Rao if (!error) 662a5aedd68SStacey Son LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx, 663a5aedd68SStacey Son contested, waittime, file, line); 664a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 665a5aedd68SStacey Son if (sleep_time) 666a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time); 667a5aedd68SStacey Son if (spin_cnt > sleep_cnt) 668a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt)); 669a5aedd68SStacey Son #endif 670f9819486SAttilio Rao return (error); 6714e7f640dSJohn Baldwin } 6724e7f640dSJohn Baldwin 6734e7f640dSJohn Baldwin /* 6744e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xunlock 6754e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 6764e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 6774e7f640dSJohn Baldwin * accessible from at least sx.h. 6784e7f640dSJohn Baldwin */ 6794e7f640dSJohn Baldwin void 6804e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line) 6814e7f640dSJohn Baldwin { 6824e7f640dSJohn Baldwin uintptr_t x; 683da7bbd2cSJohn Baldwin int queue, wakeup_swapper; 6844e7f640dSJohn Baldwin 6854e7f640dSJohn Baldwin MPASS(!(sx->sx_lock & SX_LOCK_SHARED)); 6864e7f640dSJohn Baldwin 6874e7f640dSJohn Baldwin /* If the lock is recursed, then unrecurse one level. */ 6884e7f640dSJohn Baldwin if (sx_xlocked(sx) && sx_recursed(sx)) { 6894e7f640dSJohn Baldwin if ((--sx->sx_recurse) == 0) 6904e7f640dSJohn Baldwin atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 6914e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6924e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 6934e7f640dSJohn Baldwin return; 6944e7f640dSJohn Baldwin } 6954e7f640dSJohn Baldwin MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS | 6964e7f640dSJohn Baldwin SX_LOCK_EXCLUSIVE_WAITERS)); 6974e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6984e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 6994e7f640dSJohn Baldwin 7004e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 7014e7f640dSJohn Baldwin x = SX_LOCK_UNLOCKED; 7024e7f640dSJohn Baldwin 7034e7f640dSJohn Baldwin /* 7044e7f640dSJohn Baldwin * The wake up algorithm here is quite simple and probably not 7054e7f640dSJohn Baldwin * ideal. It gives precedence to shared waiters if they are 7064e7f640dSJohn Baldwin * present. For this condition, we have to preserve the 7074e7f640dSJohn Baldwin * state of the exclusive waiters flag. 7082028867dSAttilio Rao * If interruptible sleeps left the shared queue empty avoid a 7092028867dSAttilio Rao * starvation for the threads sleeping on the exclusive queue by giving 7102028867dSAttilio Rao * them precedence and cleaning up the shared waiters bit anyway. 7114e7f640dSJohn Baldwin */ 7122028867dSAttilio Rao if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 && 7132028867dSAttilio Rao sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) { 7144e7f640dSJohn Baldwin queue = SQ_SHARED_QUEUE; 7154e7f640dSJohn Baldwin x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS); 7164e7f640dSJohn Baldwin } else 7174e7f640dSJohn Baldwin queue = SQ_EXCLUSIVE_QUEUE; 7184e7f640dSJohn Baldwin 7194e7f640dSJohn Baldwin /* Wake up all the waiters for the specific queue. */ 7204e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7214e7f640dSJohn Baldwin CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 7224e7f640dSJohn Baldwin __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 7234e7f640dSJohn Baldwin "exclusive"); 7244e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, x); 725da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 726da7bbd2cSJohn Baldwin queue); 727c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 728da7bbd2cSJohn Baldwin if (wakeup_swapper) 729da7bbd2cSJohn Baldwin kick_proc0(); 7304e7f640dSJohn Baldwin } 7314e7f640dSJohn Baldwin 7324e7f640dSJohn Baldwin /* 7334e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_slock 7344e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 7354e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 7364e7f640dSJohn Baldwin * accessible from at least sx.h. 7374e7f640dSJohn Baldwin */ 738f9819486SAttilio Rao int 739f9819486SAttilio Rao _sx_slock_hard(struct sx *sx, int opts, const char *file, int line) 7404e7f640dSJohn Baldwin { 7414e7f640dSJohn Baldwin GIANT_DECLARE; 7424e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 7434e7f640dSJohn Baldwin volatile struct thread *owner; 7444e7f640dSJohn Baldwin #endif 7451723a064SJeff Roberson #ifdef LOCK_PROFILING 746c1a6d9faSAttilio Rao uint64_t waittime = 0; 747c1a6d9faSAttilio Rao int contested = 0; 7481723a064SJeff Roberson #endif 7494e7f640dSJohn Baldwin uintptr_t x; 750c1a6d9faSAttilio Rao int error = 0; 751a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 752a5aedd68SStacey Son uint64_t spin_cnt = 0; 753a5aedd68SStacey Son uint64_t sleep_cnt = 0; 754a5aedd68SStacey Son int64_t sleep_time = 0; 755a5aedd68SStacey Son #endif 756c1a6d9faSAttilio Rao 7574e7f640dSJohn Baldwin /* 7584e7f640dSJohn Baldwin * As with rwlocks, we don't make any attempt to try to block 7594e7f640dSJohn Baldwin * shared locks once there is an exclusive waiter. 7604e7f640dSJohn Baldwin */ 7614e7f640dSJohn Baldwin for (;;) { 762a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 763a5aedd68SStacey Son spin_cnt++; 764a5aedd68SStacey Son #endif 7654e7f640dSJohn Baldwin x = sx->sx_lock; 7664e7f640dSJohn Baldwin 7674e7f640dSJohn Baldwin /* 7684e7f640dSJohn Baldwin * If no other thread has an exclusive lock then try to bump up 7694e7f640dSJohn Baldwin * the count of sharers. Since we have to preserve the state 7704e7f640dSJohn Baldwin * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 7714e7f640dSJohn Baldwin * shared lock loop back and retry. 7724e7f640dSJohn Baldwin */ 7734e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 7744e7f640dSJohn Baldwin MPASS(!(x & SX_LOCK_SHARED_WAITERS)); 7754e7f640dSJohn Baldwin if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, 7764e7f640dSJohn Baldwin x + SX_ONE_SHARER)) { 7774e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7784e7f640dSJohn Baldwin CTR4(KTR_LOCK, 7794e7f640dSJohn Baldwin "%s: %p succeed %p -> %p", __func__, 7804e7f640dSJohn Baldwin sx, (void *)x, 7814e7f640dSJohn Baldwin (void *)(x + SX_ONE_SHARER)); 7824e7f640dSJohn Baldwin break; 7834e7f640dSJohn Baldwin } 7844e7f640dSJohn Baldwin continue; 7854e7f640dSJohn Baldwin } 786eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 787eea4f254SJeff Roberson &waittime); 7884e7f640dSJohn Baldwin 7894e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 7904e7f640dSJohn Baldwin /* 7914e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 7924e7f640dSJohn Baldwin * the owner stops running or the state of the lock 7934e7f640dSJohn Baldwin * changes. 7944e7f640dSJohn Baldwin */ 7951ae1c2a3SAttilio Rao if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 7964e7f640dSJohn Baldwin x = SX_OWNER(x); 7974e7f640dSJohn Baldwin owner = (struct thread *)x; 7984e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 7994e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8004e7f640dSJohn Baldwin CTR3(KTR_LOCK, 8014e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 8024e7f640dSJohn Baldwin __func__, sx, owner); 8034e7f640dSJohn Baldwin GIANT_SAVE(); 8044e7f640dSJohn Baldwin while (SX_OWNER(sx->sx_lock) == x && 805a5aedd68SStacey Son TD_IS_RUNNING(owner)) { 806a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 807a5aedd68SStacey Son spin_cnt++; 808a5aedd68SStacey Son #endif 8094e7f640dSJohn Baldwin cpu_spinwait(); 810a5aedd68SStacey Son } 8114e7f640dSJohn Baldwin continue; 8124e7f640dSJohn Baldwin } 8134e7f640dSJohn Baldwin } 8144e7f640dSJohn Baldwin #endif 8154e7f640dSJohn Baldwin 8164e7f640dSJohn Baldwin /* 8174e7f640dSJohn Baldwin * Some other thread already has an exclusive lock, so 8184e7f640dSJohn Baldwin * start the process of blocking. 8194e7f640dSJohn Baldwin */ 8204e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 8214e7f640dSJohn Baldwin x = sx->sx_lock; 8224e7f640dSJohn Baldwin 8234e7f640dSJohn Baldwin /* 8244e7f640dSJohn Baldwin * The lock could have been released while we spun. 8254e7f640dSJohn Baldwin * In this case loop back and retry. 8264e7f640dSJohn Baldwin */ 8274e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 8284e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 8294e7f640dSJohn Baldwin continue; 8304e7f640dSJohn Baldwin } 8314e7f640dSJohn Baldwin 8324e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 8334e7f640dSJohn Baldwin /* 8344e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 8354e7f640dSJohn Baldwin * the owner stops running or the state of the lock 8364e7f640dSJohn Baldwin * changes. 8374e7f640dSJohn Baldwin */ 8384e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 8391ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 8404e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 8414e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 8424e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 8434e7f640dSJohn Baldwin continue; 8444e7f640dSJohn Baldwin } 8454e7f640dSJohn Baldwin } 8464e7f640dSJohn Baldwin #endif 8474e7f640dSJohn Baldwin 8484e7f640dSJohn Baldwin /* 8494e7f640dSJohn Baldwin * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 8504e7f640dSJohn Baldwin * fail to set it drop the sleep queue lock and loop 8514e7f640dSJohn Baldwin * back. 8524e7f640dSJohn Baldwin */ 8534e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS)) { 8544e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 8554e7f640dSJohn Baldwin x | SX_LOCK_SHARED_WAITERS)) { 8564e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 8574e7f640dSJohn Baldwin continue; 8584e7f640dSJohn Baldwin } 8594e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8604e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 8614e7f640dSJohn Baldwin __func__, sx); 8624e7f640dSJohn Baldwin } 8634e7f640dSJohn Baldwin 8644e7f640dSJohn Baldwin /* 8654e7f640dSJohn Baldwin * Since we have been unable to acquire the shared lock, 8664e7f640dSJohn Baldwin * we have to sleep. 8674e7f640dSJohn Baldwin */ 8684e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8694e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 8704e7f640dSJohn Baldwin __func__, sx); 8714e7f640dSJohn Baldwin 872a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 873a5aedd68SStacey Son sleep_time -= lockstat_nsecs(); 874a5aedd68SStacey Son #endif 8754e7f640dSJohn Baldwin GIANT_SAVE(); 8764e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 877f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 878f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 879f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 880c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 881f9819486SAttilio Rao else 882c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 883a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 884a5aedd68SStacey Son sleep_time += lockstat_nsecs(); 885a5aedd68SStacey Son sleep_cnt++; 886a5aedd68SStacey Son #endif 887f9819486SAttilio Rao if (error) { 888f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 889f9819486SAttilio Rao CTR2(KTR_LOCK, 890f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 891f9819486SAttilio Rao __func__, sx); 892f9819486SAttilio Rao break; 893f9819486SAttilio Rao } 8944e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8954e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 8964e7f640dSJohn Baldwin __func__, sx); 8974e7f640dSJohn Baldwin } 898eea4f254SJeff Roberson if (error == 0) 899a5aedd68SStacey Son LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx, 900a5aedd68SStacey Son contested, waittime, file, line); 901a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 902a5aedd68SStacey Son if (sleep_time) 903a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time); 904a5aedd68SStacey Son if (spin_cnt > sleep_cnt) 905a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt)); 906a5aedd68SStacey Son #endif 9074e7f640dSJohn Baldwin GIANT_RESTORE(); 908f9819486SAttilio Rao return (error); 9094e7f640dSJohn Baldwin } 9104e7f640dSJohn Baldwin 9114e7f640dSJohn Baldwin /* 9124e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_sunlock 9134e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 9144e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 9154e7f640dSJohn Baldwin * accessible from at least sx.h. 9164e7f640dSJohn Baldwin */ 9174e7f640dSJohn Baldwin void 9184e7f640dSJohn Baldwin _sx_sunlock_hard(struct sx *sx, const char *file, int line) 9194e7f640dSJohn Baldwin { 9204e7f640dSJohn Baldwin uintptr_t x; 921da7bbd2cSJohn Baldwin int wakeup_swapper; 9224e7f640dSJohn Baldwin 9234e7f640dSJohn Baldwin for (;;) { 9244e7f640dSJohn Baldwin x = sx->sx_lock; 9254e7f640dSJohn Baldwin 9264e7f640dSJohn Baldwin /* 9274e7f640dSJohn Baldwin * We should never have sharers while at least one thread 9284e7f640dSJohn Baldwin * holds a shared lock. 9294e7f640dSJohn Baldwin */ 9304e7f640dSJohn Baldwin KASSERT(!(x & SX_LOCK_SHARED_WAITERS), 9314e7f640dSJohn Baldwin ("%s: waiting sharers", __func__)); 9324e7f640dSJohn Baldwin 9334e7f640dSJohn Baldwin /* 9344e7f640dSJohn Baldwin * See if there is more than one shared lock held. If 9354e7f640dSJohn Baldwin * so, just drop one and return. 9364e7f640dSJohn Baldwin */ 9374e7f640dSJohn Baldwin if (SX_SHARERS(x) > 1) { 938ddce63caSAttilio Rao if (atomic_cmpset_rel_ptr(&sx->sx_lock, x, 9394e7f640dSJohn Baldwin x - SX_ONE_SHARER)) { 9404e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9414e7f640dSJohn Baldwin CTR4(KTR_LOCK, 9424e7f640dSJohn Baldwin "%s: %p succeeded %p -> %p", 9434e7f640dSJohn Baldwin __func__, sx, (void *)x, 9444e7f640dSJohn Baldwin (void *)(x - SX_ONE_SHARER)); 9454e7f640dSJohn Baldwin break; 9464e7f640dSJohn Baldwin } 9474e7f640dSJohn Baldwin continue; 9484e7f640dSJohn Baldwin } 9494e7f640dSJohn Baldwin 9504e7f640dSJohn Baldwin /* 9514e7f640dSJohn Baldwin * If there aren't any waiters for an exclusive lock, 9524e7f640dSJohn Baldwin * then try to drop it quickly. 9534e7f640dSJohn Baldwin */ 9544e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 9554e7f640dSJohn Baldwin MPASS(x == SX_SHARERS_LOCK(1)); 956ddce63caSAttilio Rao if (atomic_cmpset_rel_ptr(&sx->sx_lock, 957ddce63caSAttilio Rao SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) { 9584e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9594e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p last succeeded", 9604e7f640dSJohn Baldwin __func__, sx); 9614e7f640dSJohn Baldwin break; 9624e7f640dSJohn Baldwin } 9634e7f640dSJohn Baldwin continue; 9644e7f640dSJohn Baldwin } 9654e7f640dSJohn Baldwin 9664e7f640dSJohn Baldwin /* 9674e7f640dSJohn Baldwin * At this point, there should just be one sharer with 9684e7f640dSJohn Baldwin * exclusive waiters. 9694e7f640dSJohn Baldwin */ 9704e7f640dSJohn Baldwin MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS)); 9714e7f640dSJohn Baldwin 9724e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 9734e7f640dSJohn Baldwin 9744e7f640dSJohn Baldwin /* 9754e7f640dSJohn Baldwin * Wake up semantic here is quite simple: 9764e7f640dSJohn Baldwin * Just wake up all the exclusive waiters. 9774e7f640dSJohn Baldwin * Note that the state of the lock could have changed, 9784e7f640dSJohn Baldwin * so if it fails loop back and retry. 9794e7f640dSJohn Baldwin */ 980ddce63caSAttilio Rao if (!atomic_cmpset_rel_ptr(&sx->sx_lock, 9814e7f640dSJohn Baldwin SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS, 9824e7f640dSJohn Baldwin SX_LOCK_UNLOCKED)) { 9834e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 9844e7f640dSJohn Baldwin continue; 9854e7f640dSJohn Baldwin } 9864e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9874e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p waking up all thread on" 9884e7f640dSJohn Baldwin "exclusive queue", __func__, sx); 989da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 990da7bbd2cSJohn Baldwin 0, SQ_EXCLUSIVE_QUEUE); 991c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 992da7bbd2cSJohn Baldwin if (wakeup_swapper) 993da7bbd2cSJohn Baldwin kick_proc0(); 9944e7f640dSJohn Baldwin break; 9954e7f640dSJohn Baldwin } 996d55229b7SJason Evans } 9974e5e677bSJohn Baldwin 9984e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT 999781a35dfSJohn Baldwin #ifndef INVARIANTS 1000781a35dfSJohn Baldwin #undef _sx_assert 1001781a35dfSJohn Baldwin #endif 1002781a35dfSJohn Baldwin 10034e5e677bSJohn Baldwin /* 10044e5e677bSJohn Baldwin * In the non-WITNESS case, sx_assert() can only detect that at least 10054e5e677bSJohn Baldwin * *some* thread owns an slock, but it cannot guarantee that *this* 10064e5e677bSJohn Baldwin * thread owns an slock. 10074e5e677bSJohn Baldwin */ 10084e5e677bSJohn Baldwin void 10094e5e677bSJohn Baldwin _sx_assert(struct sx *sx, int what, const char *file, int line) 10104e5e677bSJohn Baldwin { 10114e7f640dSJohn Baldwin #ifndef WITNESS 10124e7f640dSJohn Baldwin int slocked = 0; 10134e7f640dSJohn Baldwin #endif 10144e5e677bSJohn Baldwin 101503129ba9SJohn Baldwin if (panicstr != NULL) 101603129ba9SJohn Baldwin return; 10174e5e677bSJohn Baldwin switch (what) { 10187ec137e5SJohn Baldwin case SA_SLOCKED: 10197ec137e5SJohn Baldwin case SA_SLOCKED | SA_NOTRECURSED: 10207ec137e5SJohn Baldwin case SA_SLOCKED | SA_RECURSED: 10214e7f640dSJohn Baldwin #ifndef WITNESS 10224e7f640dSJohn Baldwin slocked = 1; 10234e7f640dSJohn Baldwin /* FALLTHROUGH */ 10244e7f640dSJohn Baldwin #endif 10257ec137e5SJohn Baldwin case SA_LOCKED: 10267ec137e5SJohn Baldwin case SA_LOCKED | SA_NOTRECURSED: 10277ec137e5SJohn Baldwin case SA_LOCKED | SA_RECURSED: 10284e5e677bSJohn Baldwin #ifdef WITNESS 1029aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 10304e5e677bSJohn Baldwin #else 10314e7f640dSJohn Baldwin /* 10324e7f640dSJohn Baldwin * If some other thread has an exclusive lock or we 10334e7f640dSJohn Baldwin * have one and are asserting a shared lock, fail. 10344e7f640dSJohn Baldwin * Also, if no one has a lock at all, fail. 10354e7f640dSJohn Baldwin */ 10364e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED || 10374e7f640dSJohn Baldwin (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 10384e7f640dSJohn Baldwin sx_xholder(sx) != curthread))) 103903129ba9SJohn Baldwin panic("Lock %s not %slocked @ %s:%d\n", 10404e7f640dSJohn Baldwin sx->lock_object.lo_name, slocked ? "share " : "", 10414e7f640dSJohn Baldwin file, line); 10424e7f640dSJohn Baldwin 10434e7f640dSJohn Baldwin if (!(sx->sx_lock & SX_LOCK_SHARED)) { 10444e7f640dSJohn Baldwin if (sx_recursed(sx)) { 10457ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 10464e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 10474e7f640dSJohn Baldwin sx->lock_object.lo_name, file, 10484e7f640dSJohn Baldwin line); 10497ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 10504e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 10514e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 10524e7f640dSJohn Baldwin } 10534e5e677bSJohn Baldwin #endif 10544e5e677bSJohn Baldwin break; 10557ec137e5SJohn Baldwin case SA_XLOCKED: 10567ec137e5SJohn Baldwin case SA_XLOCKED | SA_NOTRECURSED: 10577ec137e5SJohn Baldwin case SA_XLOCKED | SA_RECURSED: 10584e7f640dSJohn Baldwin if (sx_xholder(sx) != curthread) 105903129ba9SJohn Baldwin panic("Lock %s not exclusively locked @ %s:%d\n", 1060aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 10614e7f640dSJohn Baldwin if (sx_recursed(sx)) { 10627ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 10634e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 10644e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 10657ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 10664e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 10674e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 10684e5e677bSJohn Baldwin break; 10697ec137e5SJohn Baldwin case SA_UNLOCKED: 107019b0efd3SPawel Jakub Dawidek #ifdef WITNESS 1071aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 107219b0efd3SPawel Jakub Dawidek #else 1073f6739b1dSPawel Jakub Dawidek /* 10744e7f640dSJohn Baldwin * If we hold an exclusve lock fail. We can't 10754e7f640dSJohn Baldwin * reliably check to see if we hold a shared lock or 10764e7f640dSJohn Baldwin * not. 1077f6739b1dSPawel Jakub Dawidek */ 10784e7f640dSJohn Baldwin if (sx_xholder(sx) == curthread) 107903129ba9SJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 1080aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 108119b0efd3SPawel Jakub Dawidek #endif 108219b0efd3SPawel Jakub Dawidek break; 10834e5e677bSJohn Baldwin default: 10844e5e677bSJohn Baldwin panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 10854e5e677bSJohn Baldwin line); 10864e5e677bSJohn Baldwin } 10874e5e677bSJohn Baldwin } 10884e5e677bSJohn Baldwin #endif /* INVARIANT_SUPPORT */ 1089d272fe53SJohn Baldwin 1090d272fe53SJohn Baldwin #ifdef DDB 10914e7f640dSJohn Baldwin static void 1092d272fe53SJohn Baldwin db_show_sx(struct lock_object *lock) 1093d272fe53SJohn Baldwin { 1094d272fe53SJohn Baldwin struct thread *td; 1095d272fe53SJohn Baldwin struct sx *sx; 1096d272fe53SJohn Baldwin 1097d272fe53SJohn Baldwin sx = (struct sx *)lock; 1098d272fe53SJohn Baldwin 1099d272fe53SJohn Baldwin db_printf(" state: "); 11004e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED) 11014e7f640dSJohn Baldwin db_printf("UNLOCKED\n"); 11020026c92cSJohn Baldwin else if (sx->sx_lock == SX_LOCK_DESTROYED) { 11030026c92cSJohn Baldwin db_printf("DESTROYED\n"); 11040026c92cSJohn Baldwin return; 11050026c92cSJohn Baldwin } else if (sx->sx_lock & SX_LOCK_SHARED) 11064e7f640dSJohn Baldwin db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 11074e7f640dSJohn Baldwin else { 11084e7f640dSJohn Baldwin td = sx_xholder(sx); 1109d272fe53SJohn Baldwin db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1110431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 11114e7f640dSJohn Baldwin if (sx_recursed(sx)) 11124e7f640dSJohn Baldwin db_printf(" recursed: %d\n", sx->sx_recurse); 11134e7f640dSJohn Baldwin } 11144e7f640dSJohn Baldwin 11154e7f640dSJohn Baldwin db_printf(" waiters: "); 11164e7f640dSJohn Baldwin switch(sx->sx_lock & 11174e7f640dSJohn Baldwin (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 11184e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS: 11194e7f640dSJohn Baldwin db_printf("shared\n"); 11204e7f640dSJohn Baldwin break; 11214e7f640dSJohn Baldwin case SX_LOCK_EXCLUSIVE_WAITERS: 11224e7f640dSJohn Baldwin db_printf("exclusive\n"); 11234e7f640dSJohn Baldwin break; 11244e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 11254e7f640dSJohn Baldwin db_printf("exclusive and shared\n"); 11264e7f640dSJohn Baldwin break; 11274e7f640dSJohn Baldwin default: 11284e7f640dSJohn Baldwin db_printf("none\n"); 11294e7f640dSJohn Baldwin } 1130d272fe53SJohn Baldwin } 1131462a7addSJohn Baldwin 1132462a7addSJohn Baldwin /* 1133462a7addSJohn Baldwin * Check to see if a thread that is blocked on a sleep queue is actually 1134462a7addSJohn Baldwin * blocked on an sx lock. If so, output some details and return true. 1135462a7addSJohn Baldwin * If the lock has an exclusive owner, return that in *ownerp. 1136462a7addSJohn Baldwin */ 1137462a7addSJohn Baldwin int 1138462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp) 1139462a7addSJohn Baldwin { 1140462a7addSJohn Baldwin struct sx *sx; 1141462a7addSJohn Baldwin 1142462a7addSJohn Baldwin /* 11434e7f640dSJohn Baldwin * Check to see if this thread is blocked on an sx lock. 11444e7f640dSJohn Baldwin * First, we check the lock class. If that is ok, then we 11454e7f640dSJohn Baldwin * compare the lock name against the wait message. 1146462a7addSJohn Baldwin */ 11474e7f640dSJohn Baldwin sx = td->td_wchan; 11484e7f640dSJohn Baldwin if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 11494e7f640dSJohn Baldwin sx->lock_object.lo_name != td->td_wmesg) 1150462a7addSJohn Baldwin return (0); 1151462a7addSJohn Baldwin 1152462a7addSJohn Baldwin /* We think we have an sx lock, so output some details. */ 1153462a7addSJohn Baldwin db_printf("blocked on sx \"%s\" ", td->td_wmesg); 11544e7f640dSJohn Baldwin *ownerp = sx_xholder(sx); 11554e7f640dSJohn Baldwin if (sx->sx_lock & SX_LOCK_SHARED) 11564e7f640dSJohn Baldwin db_printf("SLOCK (count %ju)\n", 11574e7f640dSJohn Baldwin (uintmax_t)SX_SHARERS(sx->sx_lock)); 11584e7f640dSJohn Baldwin else 1159462a7addSJohn Baldwin db_printf("XLOCK\n"); 1160462a7addSJohn Baldwin return (1); 1161462a7addSJohn Baldwin } 1162d272fe53SJohn Baldwin #endif 1163