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 691ae1c2a3SAttilio Rao CTASSERT(((SX_NOADAPTIVE | SX_RECURSE) & LO_CLASSFLAGS) == 701ae1c2a3SAttilio Rao (SX_NOADAPTIVE | SX_RECURSE)); 71c1a6d9faSAttilio Rao 724e7f640dSJohn Baldwin /* Handy macros for sleep queues. */ 734e7f640dSJohn Baldwin #define SQ_EXCLUSIVE_QUEUE 0 744e7f640dSJohn Baldwin #define SQ_SHARED_QUEUE 1 754e7f640dSJohn Baldwin 764e7f640dSJohn Baldwin /* 774e7f640dSJohn Baldwin * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We 784e7f640dSJohn Baldwin * drop Giant anytime we have to sleep or if we adaptively spin. 794e7f640dSJohn Baldwin */ 804e7f640dSJohn Baldwin #define GIANT_DECLARE \ 814e7f640dSJohn Baldwin int _giantcnt = 0; \ 824e7f640dSJohn Baldwin WITNESS_SAVE_DECL(Giant) \ 834e7f640dSJohn Baldwin 844e7f640dSJohn Baldwin #define GIANT_SAVE() do { \ 854e7f640dSJohn Baldwin if (mtx_owned(&Giant)) { \ 864e7f640dSJohn Baldwin WITNESS_SAVE(&Giant.lock_object, Giant); \ 874e7f640dSJohn Baldwin while (mtx_owned(&Giant)) { \ 884e7f640dSJohn Baldwin _giantcnt++; \ 894e7f640dSJohn Baldwin mtx_unlock(&Giant); \ 904e7f640dSJohn Baldwin } \ 914e7f640dSJohn Baldwin } \ 924e7f640dSJohn Baldwin } while (0) 934e7f640dSJohn Baldwin 944e7f640dSJohn Baldwin #define GIANT_RESTORE() do { \ 954e7f640dSJohn Baldwin if (_giantcnt > 0) { \ 964e7f640dSJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); \ 974e7f640dSJohn Baldwin while (_giantcnt--) \ 984e7f640dSJohn Baldwin mtx_lock(&Giant); \ 994e7f640dSJohn Baldwin WITNESS_RESTORE(&Giant.lock_object, Giant); \ 1004e7f640dSJohn Baldwin } \ 1014e7f640dSJohn Baldwin } while (0) 1024e7f640dSJohn Baldwin 1034e7f640dSJohn Baldwin /* 104da7d0d1eSJohn Baldwin * Returns true if an exclusive lock is recursed. It assumes 105da7d0d1eSJohn Baldwin * curthread currently has an exclusive lock. 1064e7f640dSJohn Baldwin */ 10790356491SAttilio Rao #define sx_recurse lock_object.lo_data 1084e7f640dSJohn Baldwin #define sx_recursed(sx) ((sx)->sx_recurse != 0) 1094e7f640dSJohn Baldwin 110f9721b43SAttilio Rao static void assert_sx(struct lock_object *lock, int what); 1114e7f640dSJohn Baldwin #ifdef DDB 112d272fe53SJohn Baldwin static void db_show_sx(struct lock_object *lock); 113d272fe53SJohn Baldwin #endif 1146e21afd4SJohn Baldwin static void lock_sx(struct lock_object *lock, int how); 115a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 116a5aedd68SStacey Son static int owner_sx(struct lock_object *lock, struct thread **owner); 117a5aedd68SStacey Son #endif 1186e21afd4SJohn Baldwin static int unlock_sx(struct lock_object *lock); 119d272fe53SJohn Baldwin 12019284646SJohn Baldwin struct lock_class lock_class_sx = { 121ae8dde30SJohn Baldwin .lc_name = "sx", 122ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, 123f9721b43SAttilio Rao .lc_assert = assert_sx, 124d272fe53SJohn Baldwin #ifdef DDB 125ae8dde30SJohn Baldwin .lc_ddb_show = db_show_sx, 126d272fe53SJohn Baldwin #endif 1276e21afd4SJohn Baldwin .lc_lock = lock_sx, 1286e21afd4SJohn Baldwin .lc_unlock = unlock_sx, 129a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 130a5aedd68SStacey Son .lc_owner = owner_sx, 131a5aedd68SStacey Son #endif 13219284646SJohn Baldwin }; 13319284646SJohn Baldwin 134781a35dfSJohn Baldwin #ifndef INVARIANTS 135781a35dfSJohn Baldwin #define _sx_assert(sx, what, file, line) 136781a35dfSJohn Baldwin #endif 137781a35dfSJohn Baldwin 1381ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX 1391ae1c2a3SAttilio Rao static u_int asx_retries = 10; 1401ae1c2a3SAttilio Rao static u_int asx_loops = 10000; 1411ae1c2a3SAttilio Rao SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging"); 1421ae1c2a3SAttilio Rao SYSCTL_INT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, ""); 1431ae1c2a3SAttilio Rao SYSCTL_INT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, ""); 1441ae1c2a3SAttilio Rao #endif 1451ae1c2a3SAttilio Rao 1466281b30aSJason Evans void 147f9721b43SAttilio Rao assert_sx(struct lock_object *lock, int what) 148f9721b43SAttilio Rao { 149f9721b43SAttilio Rao 150f9721b43SAttilio Rao sx_assert((struct sx *)lock, what); 151f9721b43SAttilio Rao } 152f9721b43SAttilio Rao 153f9721b43SAttilio Rao void 1546e21afd4SJohn Baldwin lock_sx(struct lock_object *lock, int how) 1556e21afd4SJohn Baldwin { 1566e21afd4SJohn Baldwin struct sx *sx; 1576e21afd4SJohn Baldwin 1586e21afd4SJohn Baldwin sx = (struct sx *)lock; 1596e21afd4SJohn Baldwin if (how) 1606e21afd4SJohn Baldwin sx_xlock(sx); 1616e21afd4SJohn Baldwin else 1626e21afd4SJohn Baldwin sx_slock(sx); 1636e21afd4SJohn Baldwin } 1646e21afd4SJohn Baldwin 1656e21afd4SJohn Baldwin int 1666e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock) 1676e21afd4SJohn Baldwin { 1686e21afd4SJohn Baldwin struct sx *sx; 1696e21afd4SJohn Baldwin 1706e21afd4SJohn Baldwin sx = (struct sx *)lock; 1717ec137e5SJohn Baldwin sx_assert(sx, SA_LOCKED | SA_NOTRECURSED); 1726e21afd4SJohn Baldwin if (sx_xlocked(sx)) { 1736e21afd4SJohn Baldwin sx_xunlock(sx); 1746e21afd4SJohn Baldwin return (1); 1756e21afd4SJohn Baldwin } else { 1766e21afd4SJohn Baldwin sx_sunlock(sx); 1776e21afd4SJohn Baldwin return (0); 1786e21afd4SJohn Baldwin } 1796e21afd4SJohn Baldwin } 1806e21afd4SJohn Baldwin 181a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 182a5aedd68SStacey Son int 183a5aedd68SStacey Son owner_sx(struct lock_object *lock, struct thread **owner) 184a5aedd68SStacey Son { 185a5aedd68SStacey Son struct sx *sx = (struct sx *)lock; 186a5aedd68SStacey Son uintptr_t x = sx->sx_lock; 187a5aedd68SStacey Son 188a5aedd68SStacey Son *owner = (struct thread *)SX_OWNER(x); 189a5aedd68SStacey Son return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) : 190a5aedd68SStacey Son (*owner != NULL)); 191a5aedd68SStacey Son } 192a5aedd68SStacey Son #endif 193a5aedd68SStacey Son 1946e21afd4SJohn Baldwin void 195c27b5699SAndrew R. Reiter sx_sysinit(void *arg) 196c27b5699SAndrew R. Reiter { 197c27b5699SAndrew R. Reiter struct sx_args *sargs = arg; 198c27b5699SAndrew R. Reiter 199c27b5699SAndrew R. Reiter sx_init(sargs->sa_sx, sargs->sa_desc); 200c27b5699SAndrew R. Reiter } 201c27b5699SAndrew R. Reiter 202c27b5699SAndrew R. Reiter void 2034e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts) 2046281b30aSJason Evans { 2054e7f640dSJohn Baldwin int flags; 2066281b30aSJason Evans 207b0d67325SJohn Baldwin MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK | 2081ae1c2a3SAttilio Rao SX_NOPROFILE | SX_NOADAPTIVE)) == 0); 209b0d67325SJohn Baldwin 2102c7289cbSAttilio Rao flags = LO_RECURSABLE | LO_SLEEPABLE | LO_UPGRADABLE; 2114e7f640dSJohn Baldwin if (opts & SX_DUPOK) 2124e7f640dSJohn Baldwin flags |= LO_DUPOK; 2134e7f640dSJohn Baldwin if (opts & SX_NOPROFILE) 2144e7f640dSJohn Baldwin flags |= LO_NOPROFILE; 2154e7f640dSJohn Baldwin if (!(opts & SX_NOWITNESS)) 2164e7f640dSJohn Baldwin flags |= LO_WITNESS; 2174e7f640dSJohn Baldwin if (opts & SX_QUIET) 2184e7f640dSJohn Baldwin flags |= LO_QUIET; 2194e7f640dSJohn Baldwin 2201ae1c2a3SAttilio Rao flags |= opts & (SX_NOADAPTIVE | SX_RECURSE); 2214e7f640dSJohn Baldwin sx->sx_lock = SX_LOCK_UNLOCKED; 2224e7f640dSJohn Baldwin sx->sx_recurse = 0; 2234e7f640dSJohn Baldwin lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags); 2246281b30aSJason Evans } 2256281b30aSJason Evans 2266281b30aSJason Evans void 2276281b30aSJason Evans sx_destroy(struct sx *sx) 2286281b30aSJason Evans { 2296281b30aSJason Evans 2304e7f640dSJohn Baldwin KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held")); 2314e7f640dSJohn Baldwin KASSERT(sx->sx_recurse == 0, ("sx lock still recursed")); 2320026c92cSJohn Baldwin sx->sx_lock = SX_LOCK_DESTROYED; 233aa89d8cdSJohn Baldwin lock_destroy(&sx->lock_object); 2346281b30aSJason Evans } 2356281b30aSJason Evans 236f9819486SAttilio Rao int 237f9819486SAttilio Rao _sx_slock(struct sx *sx, int opts, const char *file, int line) 2386281b30aSJason Evans { 239f9819486SAttilio Rao int error = 0; 2406281b30aSJason Evans 2414e7f640dSJohn Baldwin MPASS(curthread != NULL); 2420026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 2430026c92cSJohn Baldwin ("sx_slock() of destroyed sx @ %s:%d", file, line)); 24441313430SJohn Baldwin WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 245f9819486SAttilio Rao error = __sx_slock(sx, opts, file, line); 246f9819486SAttilio Rao if (!error) { 247aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 248aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, 0, file, line); 249764e4d54SJohn Baldwin curthread->td_locks++; 2506281b30aSJason Evans } 2516281b30aSJason Evans 252f9819486SAttilio Rao return (error); 253f9819486SAttilio Rao } 254f9819486SAttilio Rao 2555f36700aSJohn Baldwin int 2565f36700aSJohn Baldwin _sx_try_slock(struct sx *sx, const char *file, int line) 2575f36700aSJohn Baldwin { 2584e7f640dSJohn Baldwin uintptr_t x; 2595f36700aSJohn Baldwin 260764a938bSPawel Jakub Dawidek for (;;) { 2614e7f640dSJohn Baldwin x = sx->sx_lock; 2620026c92cSJohn Baldwin KASSERT(x != SX_LOCK_DESTROYED, 2630026c92cSJohn Baldwin ("sx_try_slock() of destroyed sx @ %s:%d", file, line)); 264764a938bSPawel Jakub Dawidek if (!(x & SX_LOCK_SHARED)) 265764a938bSPawel Jakub Dawidek break; 266764a938bSPawel Jakub Dawidek if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) { 267aa89d8cdSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line); 268aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line); 269764e4d54SJohn Baldwin curthread->td_locks++; 2705f36700aSJohn Baldwin return (1); 2715f36700aSJohn Baldwin } 272764a938bSPawel Jakub Dawidek } 2734e7f640dSJohn Baldwin 2744e7f640dSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line); 2754e7f640dSJohn Baldwin return (0); 2765f36700aSJohn Baldwin } 2775f36700aSJohn Baldwin 278f9819486SAttilio Rao int 279f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line) 2806281b30aSJason Evans { 281f9819486SAttilio Rao int error = 0; 2826281b30aSJason Evans 2834e7f640dSJohn Baldwin MPASS(curthread != NULL); 2840026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 2850026c92cSJohn Baldwin ("sx_xlock() of destroyed sx @ %s:%d", file, line)); 286aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 28741313430SJohn Baldwin line, NULL); 288f9819486SAttilio Rao error = __sx_xlock(sx, curthread, opts, file, line); 289f9819486SAttilio Rao if (!error) { 290f9819486SAttilio Rao LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, 291f9819486SAttilio Rao file, line); 292aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 293764e4d54SJohn Baldwin curthread->td_locks++; 2946281b30aSJason Evans } 2956281b30aSJason Evans 296f9819486SAttilio Rao return (error); 297f9819486SAttilio Rao } 298f9819486SAttilio Rao 2995f36700aSJohn Baldwin int 3005f36700aSJohn Baldwin _sx_try_xlock(struct sx *sx, const char *file, int line) 3015f36700aSJohn Baldwin { 3024e7f640dSJohn Baldwin int rval; 3035f36700aSJohn Baldwin 3044e7f640dSJohn Baldwin MPASS(curthread != NULL); 3050026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3060026c92cSJohn Baldwin ("sx_try_xlock() of destroyed sx @ %s:%d", file, line)); 3074e7f640dSJohn Baldwin 308b0d67325SJohn Baldwin if (sx_xlocked(sx) && (sx->lock_object.lo_flags & SX_RECURSE) != 0) { 3094e7f640dSJohn Baldwin sx->sx_recurse++; 3104e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 3114e7f640dSJohn Baldwin rval = 1; 3124e7f640dSJohn Baldwin } else 3134e7f640dSJohn Baldwin rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, 3144e7f640dSJohn Baldwin (uintptr_t)curthread); 3154e7f640dSJohn Baldwin LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line); 3164e7f640dSJohn Baldwin if (rval) { 3174e7f640dSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 3184e7f640dSJohn Baldwin file, line); 319764e4d54SJohn Baldwin curthread->td_locks++; 3205f36700aSJohn Baldwin } 3214e7f640dSJohn Baldwin 3224e7f640dSJohn Baldwin return (rval); 3235f36700aSJohn Baldwin } 3245f36700aSJohn Baldwin 3256281b30aSJason Evans void 32619284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line) 3276281b30aSJason Evans { 3286281b30aSJason Evans 3294e7f640dSJohn Baldwin MPASS(curthread != NULL); 3300026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3310026c92cSJohn Baldwin ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 3327ec137e5SJohn Baldwin _sx_assert(sx, SA_SLOCKED, file, line); 333764e4d54SJohn Baldwin curthread->td_locks--; 334aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 335aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 3364e7f640dSJohn Baldwin __sx_sunlock(sx, file, line); 337a5aedd68SStacey Son LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx); 3386281b30aSJason Evans } 3396281b30aSJason Evans 3406281b30aSJason Evans void 34119284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line) 3426281b30aSJason Evans { 3436281b30aSJason Evans 3444e7f640dSJohn Baldwin MPASS(curthread != NULL); 3450026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3460026c92cSJohn Baldwin ("sx_xunlock() of destroyed sx @ %s:%d", file, line)); 3477ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED, file, line); 348764e4d54SJohn Baldwin curthread->td_locks--; 349aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 3504e7f640dSJohn Baldwin LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file, 3514e7f640dSJohn Baldwin line); 352afc0bfbdSKip Macy if (!sx_recursed(sx)) 353a5aedd68SStacey Son LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx); 3544e7f640dSJohn Baldwin __sx_xunlock(sx, curthread, file, line); 3556281b30aSJason Evans } 356d55229b7SJason Evans 3574e7f640dSJohn Baldwin /* 3584e7f640dSJohn Baldwin * Try to do a non-blocking upgrade from a shared lock to an exclusive lock. 3594e7f640dSJohn Baldwin * This will only succeed if this thread holds a single shared lock. 3604e7f640dSJohn Baldwin * Return 1 if if the upgrade succeed, 0 otherwise. 3614e7f640dSJohn Baldwin */ 362d55229b7SJason Evans int 363d55229b7SJason Evans _sx_try_upgrade(struct sx *sx, const char *file, int line) 364d55229b7SJason Evans { 3654e7f640dSJohn Baldwin uintptr_t x; 3664e7f640dSJohn Baldwin int success; 367d55229b7SJason Evans 3680026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3690026c92cSJohn Baldwin ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 3707ec137e5SJohn Baldwin _sx_assert(sx, SA_SLOCKED, file, line); 371d55229b7SJason Evans 3724e7f640dSJohn Baldwin /* 3734e7f640dSJohn Baldwin * Try to switch from one shared lock to an exclusive lock. We need 3744e7f640dSJohn Baldwin * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 3754e7f640dSJohn Baldwin * we will wake up the exclusive waiters when we drop the lock. 3764e7f640dSJohn Baldwin */ 3774e7f640dSJohn Baldwin x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS; 3784e7f640dSJohn Baldwin success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, 3794e7f640dSJohn Baldwin (uintptr_t)curthread | x); 3804e7f640dSJohn Baldwin LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 381a5aedd68SStacey Son if (success) { 382aa89d8cdSJohn Baldwin WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 383b0b7cb50SJohn Baldwin file, line); 384a5aedd68SStacey Son LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx); 385a5aedd68SStacey Son } 3864e7f640dSJohn Baldwin return (success); 387d55229b7SJason Evans } 388d55229b7SJason Evans 3894e7f640dSJohn Baldwin /* 3904e7f640dSJohn Baldwin * Downgrade an unrecursed exclusive lock into a single shared lock. 3914e7f640dSJohn Baldwin */ 392d55229b7SJason Evans void 393d55229b7SJason Evans _sx_downgrade(struct sx *sx, const char *file, int line) 394d55229b7SJason Evans { 3954e7f640dSJohn Baldwin uintptr_t x; 396da7bbd2cSJohn Baldwin int wakeup_swapper; 397d55229b7SJason Evans 3980026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3990026c92cSJohn Baldwin ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 4007ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 4014e7f640dSJohn Baldwin #ifndef INVARIANTS 4024e7f640dSJohn Baldwin if (sx_recursed(sx)) 4034e7f640dSJohn Baldwin panic("downgrade of a recursed lock"); 4044e7f640dSJohn Baldwin #endif 405d55229b7SJason Evans 406aa89d8cdSJohn Baldwin WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 407d55229b7SJason Evans 4084e7f640dSJohn Baldwin /* 4094e7f640dSJohn Baldwin * Try to switch from an exclusive lock with no shared waiters 4104e7f640dSJohn Baldwin * to one sharer with no shared waiters. If there are 4114e7f640dSJohn Baldwin * exclusive waiters, we don't need to lock the sleep queue so 4124e7f640dSJohn Baldwin * long as we preserve the flag. We do one quick try and if 4134e7f640dSJohn Baldwin * that fails we grab the sleepq lock to keep the flags from 4144e7f640dSJohn Baldwin * changing and do it the slow way. 4154e7f640dSJohn Baldwin * 4164e7f640dSJohn Baldwin * We have to lock the sleep queue if there are shared waiters 4174e7f640dSJohn Baldwin * so we can wake them up. 4184e7f640dSJohn Baldwin */ 4194e7f640dSJohn Baldwin x = sx->sx_lock; 4204e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS) && 4214e7f640dSJohn Baldwin atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 4224e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS))) { 4234e7f640dSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 4244e7f640dSJohn Baldwin return; 4254e7f640dSJohn Baldwin } 4264e7f640dSJohn Baldwin 4274e7f640dSJohn Baldwin /* 4284e7f640dSJohn Baldwin * Lock the sleep queue so we can read the waiters bits 4294e7f640dSJohn Baldwin * without any races and wakeup any shared waiters. 4304e7f640dSJohn Baldwin */ 4314e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 4324e7f640dSJohn Baldwin 4334e7f640dSJohn Baldwin /* 4344e7f640dSJohn Baldwin * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 4354e7f640dSJohn Baldwin * shared lock. If there are any shared waiters, wake them up. 4364e7f640dSJohn Baldwin */ 437da7bbd2cSJohn Baldwin wakeup_swapper = 0; 4384e7f640dSJohn Baldwin x = sx->sx_lock; 4394e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 4404e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS)); 4414e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED_WAITERS) 442da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 443da7bbd2cSJohn Baldwin 0, SQ_SHARED_QUEUE); 4444e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 445d55229b7SJason Evans 446aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 447a5aedd68SStacey Son LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx); 448da7bbd2cSJohn Baldwin 449da7bbd2cSJohn Baldwin if (wakeup_swapper) 450da7bbd2cSJohn Baldwin kick_proc0(); 4514e7f640dSJohn Baldwin } 452d55229b7SJason Evans 4534e7f640dSJohn Baldwin /* 4544e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xlock 4554e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 4564e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 4574e7f640dSJohn Baldwin * accessible from at least sx.h. 4584e7f640dSJohn Baldwin */ 459f9819486SAttilio Rao int 460f9819486SAttilio Rao _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file, 461f9819486SAttilio Rao int line) 4624e7f640dSJohn Baldwin { 4634e7f640dSJohn Baldwin GIANT_DECLARE; 4644e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 4654e7f640dSJohn Baldwin volatile struct thread *owner; 4661ae1c2a3SAttilio Rao u_int i, spintries = 0; 4674e7f640dSJohn Baldwin #endif 4684e7f640dSJohn Baldwin uintptr_t x; 4691723a064SJeff Roberson #ifdef LOCK_PROFILING 4701723a064SJeff Roberson uint64_t waittime = 0; 4711723a064SJeff Roberson int contested = 0; 4721723a064SJeff Roberson #endif 4731723a064SJeff Roberson int error = 0; 474a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 475a5aedd68SStacey Son uint64_t spin_cnt = 0; 476a5aedd68SStacey Son uint64_t sleep_cnt = 0; 477a5aedd68SStacey Son int64_t sleep_time = 0; 478a5aedd68SStacey Son #endif 4794e7f640dSJohn Baldwin 4804e7f640dSJohn Baldwin /* If we already hold an exclusive lock, then recurse. */ 4814e7f640dSJohn Baldwin if (sx_xlocked(sx)) { 482b0d67325SJohn Baldwin KASSERT((sx->lock_object.lo_flags & SX_RECURSE) != 0, 483b0d67325SJohn Baldwin ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 484b0d67325SJohn Baldwin sx->lock_object.lo_name, file, line)); 4854e7f640dSJohn Baldwin sx->sx_recurse++; 4864e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 4874e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 4884e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 489f9819486SAttilio Rao return (0); 4904e7f640dSJohn Baldwin } 4914e7f640dSJohn Baldwin 4924e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 4934e7f640dSJohn Baldwin CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 4944e7f640dSJohn Baldwin sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 4954e7f640dSJohn Baldwin 4964e7f640dSJohn Baldwin while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) { 497a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 498a5aedd68SStacey Son spin_cnt++; 499a5aedd68SStacey Son #endif 500eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 501eea4f254SJeff Roberson &waittime); 5024e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5034e7f640dSJohn Baldwin /* 5044e7f640dSJohn Baldwin * If the lock is write locked and the owner is 5054e7f640dSJohn Baldwin * running on another CPU, spin until the owner stops 5064e7f640dSJohn Baldwin * running or the state of the lock changes. 5074e7f640dSJohn Baldwin */ 5084e7f640dSJohn Baldwin x = sx->sx_lock; 5091ae1c2a3SAttilio Rao if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) != 0) { 5101ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0) { 5114e7f640dSJohn Baldwin x = SX_OWNER(x); 5124e7f640dSJohn Baldwin owner = (struct thread *)x; 5134e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 5144e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5154e7f640dSJohn Baldwin CTR3(KTR_LOCK, 5164e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 5174e7f640dSJohn Baldwin __func__, sx, owner); 5184e7f640dSJohn Baldwin GIANT_SAVE(); 5194e7f640dSJohn Baldwin while (SX_OWNER(sx->sx_lock) == x && 520a5aedd68SStacey Son TD_IS_RUNNING(owner)) { 5214e7f640dSJohn Baldwin cpu_spinwait(); 522a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 523a5aedd68SStacey Son spin_cnt++; 524a5aedd68SStacey Son #endif 525a5aedd68SStacey Son } 5264e7f640dSJohn Baldwin continue; 5274e7f640dSJohn Baldwin } 5281ae1c2a3SAttilio Rao } else if (SX_SHARERS(x) && spintries < asx_retries) { 5291ae1c2a3SAttilio Rao spintries++; 5301ae1c2a3SAttilio Rao for (i = 0; i < asx_loops; i++) { 5311ae1c2a3SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5321ae1c2a3SAttilio Rao CTR4(KTR_LOCK, 5331ae1c2a3SAttilio Rao "%s: shared spinning on %p with %u and %u", 5341ae1c2a3SAttilio Rao __func__, sx, spintries, i); 5351ae1c2a3SAttilio Rao GIANT_SAVE(); 5361ae1c2a3SAttilio Rao x = sx->sx_lock; 5371ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0 || 5381ae1c2a3SAttilio Rao SX_SHARERS(x) == 0) 5391ae1c2a3SAttilio Rao break; 5401ae1c2a3SAttilio Rao cpu_spinwait(); 5411ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS 5421ae1c2a3SAttilio Rao spin_cnt++; 5431ae1c2a3SAttilio Rao #endif 5441ae1c2a3SAttilio Rao } 5451ae1c2a3SAttilio Rao if (i != asx_loops) 5461ae1c2a3SAttilio Rao continue; 5471ae1c2a3SAttilio Rao } 5484e7f640dSJohn Baldwin } 5494e7f640dSJohn Baldwin #endif 5504e7f640dSJohn Baldwin 5514e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 5524e7f640dSJohn Baldwin x = sx->sx_lock; 5534e7f640dSJohn Baldwin 5544e7f640dSJohn Baldwin /* 5554e7f640dSJohn Baldwin * If the lock was released while spinning on the 5564e7f640dSJohn Baldwin * sleep queue chain lock, try again. 5574e7f640dSJohn Baldwin */ 5584e7f640dSJohn Baldwin if (x == SX_LOCK_UNLOCKED) { 5594e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 5604e7f640dSJohn Baldwin continue; 5614e7f640dSJohn Baldwin } 5624e7f640dSJohn Baldwin 5634e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5644e7f640dSJohn Baldwin /* 5654e7f640dSJohn Baldwin * The current lock owner might have started executing 5664e7f640dSJohn Baldwin * on another CPU (or the lock could have changed 5674e7f640dSJohn Baldwin * owners) while we were waiting on the sleep queue 5684e7f640dSJohn Baldwin * chain lock. If so, drop the sleep queue lock and try 5694e7f640dSJohn Baldwin * again. 5704e7f640dSJohn Baldwin */ 5714e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 5721ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 5734e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 5744e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 5754e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 5764e7f640dSJohn Baldwin continue; 5774e7f640dSJohn Baldwin } 5784e7f640dSJohn Baldwin } 5794e7f640dSJohn Baldwin #endif 5804e7f640dSJohn Baldwin 5814e7f640dSJohn Baldwin /* 5824e7f640dSJohn Baldwin * If an exclusive lock was released with both shared 5834e7f640dSJohn Baldwin * and exclusive waiters and a shared waiter hasn't 5844e7f640dSJohn Baldwin * woken up and acquired the lock yet, sx_lock will be 5854e7f640dSJohn Baldwin * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 5864e7f640dSJohn Baldwin * If we see that value, try to acquire it once. Note 5874e7f640dSJohn Baldwin * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 5884e7f640dSJohn Baldwin * as there are other exclusive waiters still. If we 5894e7f640dSJohn Baldwin * fail, restart the loop. 5904e7f640dSJohn Baldwin */ 5914e7f640dSJohn Baldwin if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 5924e7f640dSJohn Baldwin if (atomic_cmpset_acq_ptr(&sx->sx_lock, 5934e7f640dSJohn Baldwin SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS, 5944e7f640dSJohn Baldwin tid | SX_LOCK_EXCLUSIVE_WAITERS)) { 5954e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 5964e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p claimed by new writer", 5974e7f640dSJohn Baldwin __func__, sx); 5984e7f640dSJohn Baldwin break; 5994e7f640dSJohn Baldwin } 6004e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6014e7f640dSJohn Baldwin continue; 6024e7f640dSJohn Baldwin } 6034e7f640dSJohn Baldwin 6044e7f640dSJohn Baldwin /* 6054e7f640dSJohn Baldwin * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 6064e7f640dSJohn Baldwin * than loop back and retry. 6074e7f640dSJohn Baldwin */ 6084e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 6094e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 6104e7f640dSJohn Baldwin x | SX_LOCK_EXCLUSIVE_WAITERS)) { 6114e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6124e7f640dSJohn Baldwin continue; 6134e7f640dSJohn Baldwin } 6144e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6154e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 6164e7f640dSJohn Baldwin __func__, sx); 6174e7f640dSJohn Baldwin } 6184e7f640dSJohn Baldwin 6194e7f640dSJohn Baldwin /* 6204e7f640dSJohn Baldwin * Since we have been unable to acquire the exclusive 6214e7f640dSJohn Baldwin * lock and the exclusive waiters flag is set, we have 6224e7f640dSJohn Baldwin * to sleep. 6234e7f640dSJohn Baldwin */ 6244e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6254e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 6264e7f640dSJohn Baldwin __func__, sx); 6274e7f640dSJohn Baldwin 628a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 629a5aedd68SStacey Son sleep_time -= lockstat_nsecs(); 630a5aedd68SStacey Son #endif 6314e7f640dSJohn Baldwin GIANT_SAVE(); 6324e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 633f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 634f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 635f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 636c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 637f9819486SAttilio Rao else 638c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 639a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 640a5aedd68SStacey Son sleep_time += lockstat_nsecs(); 641a5aedd68SStacey Son sleep_cnt++; 642a5aedd68SStacey Son #endif 643f9819486SAttilio Rao if (error) { 644f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 645f9819486SAttilio Rao CTR2(KTR_LOCK, 646f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 647f9819486SAttilio Rao __func__, sx); 648f9819486SAttilio Rao break; 649f9819486SAttilio Rao } 6504e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6514e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 6524e7f640dSJohn Baldwin __func__, sx); 6534e7f640dSJohn Baldwin } 6544e7f640dSJohn Baldwin 6554e7f640dSJohn Baldwin GIANT_RESTORE(); 656f9819486SAttilio Rao if (!error) 657a5aedd68SStacey Son LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx, 658a5aedd68SStacey Son contested, waittime, file, line); 659a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 660a5aedd68SStacey Son if (sleep_time) 661a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time); 662a5aedd68SStacey Son if (spin_cnt > sleep_cnt) 663a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt)); 664a5aedd68SStacey Son #endif 665f9819486SAttilio Rao return (error); 6664e7f640dSJohn Baldwin } 6674e7f640dSJohn Baldwin 6684e7f640dSJohn Baldwin /* 6694e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xunlock 6704e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 6714e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 6724e7f640dSJohn Baldwin * accessible from at least sx.h. 6734e7f640dSJohn Baldwin */ 6744e7f640dSJohn Baldwin void 6754e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line) 6764e7f640dSJohn Baldwin { 6774e7f640dSJohn Baldwin uintptr_t x; 678da7bbd2cSJohn Baldwin int queue, wakeup_swapper; 6794e7f640dSJohn Baldwin 6804e7f640dSJohn Baldwin MPASS(!(sx->sx_lock & SX_LOCK_SHARED)); 6814e7f640dSJohn Baldwin 6824e7f640dSJohn Baldwin /* If the lock is recursed, then unrecurse one level. */ 6834e7f640dSJohn Baldwin if (sx_xlocked(sx) && sx_recursed(sx)) { 6844e7f640dSJohn Baldwin if ((--sx->sx_recurse) == 0) 6854e7f640dSJohn Baldwin atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 6864e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6874e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 6884e7f640dSJohn Baldwin return; 6894e7f640dSJohn Baldwin } 6904e7f640dSJohn Baldwin MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS | 6914e7f640dSJohn Baldwin SX_LOCK_EXCLUSIVE_WAITERS)); 6924e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6934e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 6944e7f640dSJohn Baldwin 6954e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 6964e7f640dSJohn Baldwin x = SX_LOCK_UNLOCKED; 6974e7f640dSJohn Baldwin 6984e7f640dSJohn Baldwin /* 6994e7f640dSJohn Baldwin * The wake up algorithm here is quite simple and probably not 7004e7f640dSJohn Baldwin * ideal. It gives precedence to shared waiters if they are 7014e7f640dSJohn Baldwin * present. For this condition, we have to preserve the 7024e7f640dSJohn Baldwin * state of the exclusive waiters flag. 7034e7f640dSJohn Baldwin */ 7044e7f640dSJohn Baldwin if (sx->sx_lock & SX_LOCK_SHARED_WAITERS) { 7054e7f640dSJohn Baldwin queue = SQ_SHARED_QUEUE; 7064e7f640dSJohn Baldwin x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS); 7074e7f640dSJohn Baldwin } else 7084e7f640dSJohn Baldwin queue = SQ_EXCLUSIVE_QUEUE; 7094e7f640dSJohn Baldwin 7104e7f640dSJohn Baldwin /* Wake up all the waiters for the specific queue. */ 7114e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7124e7f640dSJohn Baldwin CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 7134e7f640dSJohn Baldwin __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 7144e7f640dSJohn Baldwin "exclusive"); 7154e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, x); 716da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 717da7bbd2cSJohn Baldwin queue); 718c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 719da7bbd2cSJohn Baldwin if (wakeup_swapper) 720da7bbd2cSJohn Baldwin kick_proc0(); 7214e7f640dSJohn Baldwin } 7224e7f640dSJohn Baldwin 7234e7f640dSJohn Baldwin /* 7244e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_slock 7254e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 7264e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 7274e7f640dSJohn Baldwin * accessible from at least sx.h. 7284e7f640dSJohn Baldwin */ 729f9819486SAttilio Rao int 730f9819486SAttilio Rao _sx_slock_hard(struct sx *sx, int opts, const char *file, int line) 7314e7f640dSJohn Baldwin { 7324e7f640dSJohn Baldwin GIANT_DECLARE; 7334e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 7344e7f640dSJohn Baldwin volatile struct thread *owner; 7354e7f640dSJohn Baldwin #endif 7361723a064SJeff Roberson #ifdef LOCK_PROFILING 737c1a6d9faSAttilio Rao uint64_t waittime = 0; 738c1a6d9faSAttilio Rao int contested = 0; 7391723a064SJeff Roberson #endif 7404e7f640dSJohn Baldwin uintptr_t x; 741c1a6d9faSAttilio Rao int error = 0; 742a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 743a5aedd68SStacey Son uint64_t spin_cnt = 0; 744a5aedd68SStacey Son uint64_t sleep_cnt = 0; 745a5aedd68SStacey Son int64_t sleep_time = 0; 746a5aedd68SStacey Son #endif 747c1a6d9faSAttilio Rao 7484e7f640dSJohn Baldwin /* 7494e7f640dSJohn Baldwin * As with rwlocks, we don't make any attempt to try to block 7504e7f640dSJohn Baldwin * shared locks once there is an exclusive waiter. 7514e7f640dSJohn Baldwin */ 7524e7f640dSJohn Baldwin for (;;) { 753a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 754a5aedd68SStacey Son spin_cnt++; 755a5aedd68SStacey Son #endif 7564e7f640dSJohn Baldwin x = sx->sx_lock; 7574e7f640dSJohn Baldwin 7584e7f640dSJohn Baldwin /* 7594e7f640dSJohn Baldwin * If no other thread has an exclusive lock then try to bump up 7604e7f640dSJohn Baldwin * the count of sharers. Since we have to preserve the state 7614e7f640dSJohn Baldwin * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 7624e7f640dSJohn Baldwin * shared lock loop back and retry. 7634e7f640dSJohn Baldwin */ 7644e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 7654e7f640dSJohn Baldwin MPASS(!(x & SX_LOCK_SHARED_WAITERS)); 7664e7f640dSJohn Baldwin if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, 7674e7f640dSJohn Baldwin x + SX_ONE_SHARER)) { 7684e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7694e7f640dSJohn Baldwin CTR4(KTR_LOCK, 7704e7f640dSJohn Baldwin "%s: %p succeed %p -> %p", __func__, 7714e7f640dSJohn Baldwin sx, (void *)x, 7724e7f640dSJohn Baldwin (void *)(x + SX_ONE_SHARER)); 7734e7f640dSJohn Baldwin break; 7744e7f640dSJohn Baldwin } 7754e7f640dSJohn Baldwin continue; 7764e7f640dSJohn Baldwin } 777eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 778eea4f254SJeff Roberson &waittime); 7794e7f640dSJohn Baldwin 7804e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 7814e7f640dSJohn Baldwin /* 7824e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 7834e7f640dSJohn Baldwin * the owner stops running or the state of the lock 7844e7f640dSJohn Baldwin * changes. 7854e7f640dSJohn Baldwin */ 7861ae1c2a3SAttilio Rao if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 7874e7f640dSJohn Baldwin x = SX_OWNER(x); 7884e7f640dSJohn Baldwin owner = (struct thread *)x; 7894e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 7904e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7914e7f640dSJohn Baldwin CTR3(KTR_LOCK, 7924e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 7934e7f640dSJohn Baldwin __func__, sx, owner); 7944e7f640dSJohn Baldwin GIANT_SAVE(); 7954e7f640dSJohn Baldwin while (SX_OWNER(sx->sx_lock) == x && 796a5aedd68SStacey Son TD_IS_RUNNING(owner)) { 797a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 798a5aedd68SStacey Son spin_cnt++; 799a5aedd68SStacey Son #endif 8004e7f640dSJohn Baldwin cpu_spinwait(); 801a5aedd68SStacey Son } 8024e7f640dSJohn Baldwin continue; 8034e7f640dSJohn Baldwin } 8044e7f640dSJohn Baldwin } 8054e7f640dSJohn Baldwin #endif 8064e7f640dSJohn Baldwin 8074e7f640dSJohn Baldwin /* 8084e7f640dSJohn Baldwin * Some other thread already has an exclusive lock, so 8094e7f640dSJohn Baldwin * start the process of blocking. 8104e7f640dSJohn Baldwin */ 8114e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 8124e7f640dSJohn Baldwin x = sx->sx_lock; 8134e7f640dSJohn Baldwin 8144e7f640dSJohn Baldwin /* 8154e7f640dSJohn Baldwin * The lock could have been released while we spun. 8164e7f640dSJohn Baldwin * In this case loop back and retry. 8174e7f640dSJohn Baldwin */ 8184e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 8194e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 8204e7f640dSJohn Baldwin continue; 8214e7f640dSJohn Baldwin } 8224e7f640dSJohn Baldwin 8234e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 8244e7f640dSJohn Baldwin /* 8254e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 8264e7f640dSJohn Baldwin * the owner stops running or the state of the lock 8274e7f640dSJohn Baldwin * changes. 8284e7f640dSJohn Baldwin */ 8294e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 8301ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 8314e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 8324e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 8334e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 8344e7f640dSJohn Baldwin continue; 8354e7f640dSJohn Baldwin } 8364e7f640dSJohn Baldwin } 8374e7f640dSJohn Baldwin #endif 8384e7f640dSJohn Baldwin 8394e7f640dSJohn Baldwin /* 8404e7f640dSJohn Baldwin * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 8414e7f640dSJohn Baldwin * fail to set it drop the sleep queue lock and loop 8424e7f640dSJohn Baldwin * back. 8434e7f640dSJohn Baldwin */ 8444e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS)) { 8454e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 8464e7f640dSJohn Baldwin x | SX_LOCK_SHARED_WAITERS)) { 8474e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 8484e7f640dSJohn Baldwin continue; 8494e7f640dSJohn Baldwin } 8504e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8514e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 8524e7f640dSJohn Baldwin __func__, sx); 8534e7f640dSJohn Baldwin } 8544e7f640dSJohn Baldwin 8554e7f640dSJohn Baldwin /* 8564e7f640dSJohn Baldwin * Since we have been unable to acquire the shared lock, 8574e7f640dSJohn Baldwin * we have to sleep. 8584e7f640dSJohn Baldwin */ 8594e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8604e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 8614e7f640dSJohn Baldwin __func__, sx); 8624e7f640dSJohn Baldwin 863a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 864a5aedd68SStacey Son sleep_time -= lockstat_nsecs(); 865a5aedd68SStacey Son #endif 8664e7f640dSJohn Baldwin GIANT_SAVE(); 8674e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 868f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 869f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 870f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 871c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 872f9819486SAttilio Rao else 873c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 874a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 875a5aedd68SStacey Son sleep_time += lockstat_nsecs(); 876a5aedd68SStacey Son sleep_cnt++; 877a5aedd68SStacey Son #endif 878f9819486SAttilio Rao if (error) { 879f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 880f9819486SAttilio Rao CTR2(KTR_LOCK, 881f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 882f9819486SAttilio Rao __func__, sx); 883f9819486SAttilio Rao break; 884f9819486SAttilio Rao } 8854e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8864e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 8874e7f640dSJohn Baldwin __func__, sx); 8884e7f640dSJohn Baldwin } 889eea4f254SJeff Roberson if (error == 0) 890a5aedd68SStacey Son LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx, 891a5aedd68SStacey Son contested, waittime, file, line); 892a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 893a5aedd68SStacey Son if (sleep_time) 894a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time); 895a5aedd68SStacey Son if (spin_cnt > sleep_cnt) 896a5aedd68SStacey Son LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt)); 897a5aedd68SStacey Son #endif 8984e7f640dSJohn Baldwin GIANT_RESTORE(); 899f9819486SAttilio Rao return (error); 9004e7f640dSJohn Baldwin } 9014e7f640dSJohn Baldwin 9024e7f640dSJohn Baldwin /* 9034e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_sunlock 9044e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 9054e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 9064e7f640dSJohn Baldwin * accessible from at least sx.h. 9074e7f640dSJohn Baldwin */ 9084e7f640dSJohn Baldwin void 9094e7f640dSJohn Baldwin _sx_sunlock_hard(struct sx *sx, const char *file, int line) 9104e7f640dSJohn Baldwin { 9114e7f640dSJohn Baldwin uintptr_t x; 912da7bbd2cSJohn Baldwin int wakeup_swapper; 9134e7f640dSJohn Baldwin 9144e7f640dSJohn Baldwin for (;;) { 9154e7f640dSJohn Baldwin x = sx->sx_lock; 9164e7f640dSJohn Baldwin 9174e7f640dSJohn Baldwin /* 9184e7f640dSJohn Baldwin * We should never have sharers while at least one thread 9194e7f640dSJohn Baldwin * holds a shared lock. 9204e7f640dSJohn Baldwin */ 9214e7f640dSJohn Baldwin KASSERT(!(x & SX_LOCK_SHARED_WAITERS), 9224e7f640dSJohn Baldwin ("%s: waiting sharers", __func__)); 9234e7f640dSJohn Baldwin 9244e7f640dSJohn Baldwin /* 9254e7f640dSJohn Baldwin * See if there is more than one shared lock held. If 9264e7f640dSJohn Baldwin * so, just drop one and return. 9274e7f640dSJohn Baldwin */ 9284e7f640dSJohn Baldwin if (SX_SHARERS(x) > 1) { 9294e7f640dSJohn Baldwin if (atomic_cmpset_ptr(&sx->sx_lock, x, 9304e7f640dSJohn Baldwin x - SX_ONE_SHARER)) { 9314e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9324e7f640dSJohn Baldwin CTR4(KTR_LOCK, 9334e7f640dSJohn Baldwin "%s: %p succeeded %p -> %p", 9344e7f640dSJohn Baldwin __func__, sx, (void *)x, 9354e7f640dSJohn Baldwin (void *)(x - SX_ONE_SHARER)); 9364e7f640dSJohn Baldwin break; 9374e7f640dSJohn Baldwin } 9384e7f640dSJohn Baldwin continue; 9394e7f640dSJohn Baldwin } 9404e7f640dSJohn Baldwin 9414e7f640dSJohn Baldwin /* 9424e7f640dSJohn Baldwin * If there aren't any waiters for an exclusive lock, 9434e7f640dSJohn Baldwin * then try to drop it quickly. 9444e7f640dSJohn Baldwin */ 9454e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 9464e7f640dSJohn Baldwin MPASS(x == SX_SHARERS_LOCK(1)); 9474e7f640dSJohn Baldwin if (atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1), 9484e7f640dSJohn Baldwin SX_LOCK_UNLOCKED)) { 9494e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9504e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p last succeeded", 9514e7f640dSJohn Baldwin __func__, sx); 9524e7f640dSJohn Baldwin break; 9534e7f640dSJohn Baldwin } 9544e7f640dSJohn Baldwin continue; 9554e7f640dSJohn Baldwin } 9564e7f640dSJohn Baldwin 9574e7f640dSJohn Baldwin /* 9584e7f640dSJohn Baldwin * At this point, there should just be one sharer with 9594e7f640dSJohn Baldwin * exclusive waiters. 9604e7f640dSJohn Baldwin */ 9614e7f640dSJohn Baldwin MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS)); 9624e7f640dSJohn Baldwin 9634e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 9644e7f640dSJohn Baldwin 9654e7f640dSJohn Baldwin /* 9664e7f640dSJohn Baldwin * Wake up semantic here is quite simple: 9674e7f640dSJohn Baldwin * Just wake up all the exclusive waiters. 9684e7f640dSJohn Baldwin * Note that the state of the lock could have changed, 9694e7f640dSJohn Baldwin * so if it fails loop back and retry. 9704e7f640dSJohn Baldwin */ 9714e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, 9724e7f640dSJohn Baldwin SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS, 9734e7f640dSJohn Baldwin SX_LOCK_UNLOCKED)) { 9744e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 9754e7f640dSJohn Baldwin continue; 9764e7f640dSJohn Baldwin } 9774e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9784e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p waking up all thread on" 9794e7f640dSJohn Baldwin "exclusive queue", __func__, sx); 980da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 981da7bbd2cSJohn Baldwin 0, SQ_EXCLUSIVE_QUEUE); 982c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 983da7bbd2cSJohn Baldwin if (wakeup_swapper) 984da7bbd2cSJohn Baldwin kick_proc0(); 9854e7f640dSJohn Baldwin break; 9864e7f640dSJohn Baldwin } 987d55229b7SJason Evans } 9884e5e677bSJohn Baldwin 9894e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT 990781a35dfSJohn Baldwin #ifndef INVARIANTS 991781a35dfSJohn Baldwin #undef _sx_assert 992781a35dfSJohn Baldwin #endif 993781a35dfSJohn Baldwin 9944e5e677bSJohn Baldwin /* 9954e5e677bSJohn Baldwin * In the non-WITNESS case, sx_assert() can only detect that at least 9964e5e677bSJohn Baldwin * *some* thread owns an slock, but it cannot guarantee that *this* 9974e5e677bSJohn Baldwin * thread owns an slock. 9984e5e677bSJohn Baldwin */ 9994e5e677bSJohn Baldwin void 10004e5e677bSJohn Baldwin _sx_assert(struct sx *sx, int what, const char *file, int line) 10014e5e677bSJohn Baldwin { 10024e7f640dSJohn Baldwin #ifndef WITNESS 10034e7f640dSJohn Baldwin int slocked = 0; 10044e7f640dSJohn Baldwin #endif 10054e5e677bSJohn Baldwin 100603129ba9SJohn Baldwin if (panicstr != NULL) 100703129ba9SJohn Baldwin return; 10084e5e677bSJohn Baldwin switch (what) { 10097ec137e5SJohn Baldwin case SA_SLOCKED: 10107ec137e5SJohn Baldwin case SA_SLOCKED | SA_NOTRECURSED: 10117ec137e5SJohn Baldwin case SA_SLOCKED | SA_RECURSED: 10124e7f640dSJohn Baldwin #ifndef WITNESS 10134e7f640dSJohn Baldwin slocked = 1; 10144e7f640dSJohn Baldwin /* FALLTHROUGH */ 10154e7f640dSJohn Baldwin #endif 10167ec137e5SJohn Baldwin case SA_LOCKED: 10177ec137e5SJohn Baldwin case SA_LOCKED | SA_NOTRECURSED: 10187ec137e5SJohn Baldwin case SA_LOCKED | SA_RECURSED: 10194e5e677bSJohn Baldwin #ifdef WITNESS 1020aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 10214e5e677bSJohn Baldwin #else 10224e7f640dSJohn Baldwin /* 10234e7f640dSJohn Baldwin * If some other thread has an exclusive lock or we 10244e7f640dSJohn Baldwin * have one and are asserting a shared lock, fail. 10254e7f640dSJohn Baldwin * Also, if no one has a lock at all, fail. 10264e7f640dSJohn Baldwin */ 10274e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED || 10284e7f640dSJohn Baldwin (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 10294e7f640dSJohn Baldwin sx_xholder(sx) != curthread))) 103003129ba9SJohn Baldwin panic("Lock %s not %slocked @ %s:%d\n", 10314e7f640dSJohn Baldwin sx->lock_object.lo_name, slocked ? "share " : "", 10324e7f640dSJohn Baldwin file, line); 10334e7f640dSJohn Baldwin 10344e7f640dSJohn Baldwin if (!(sx->sx_lock & SX_LOCK_SHARED)) { 10354e7f640dSJohn Baldwin if (sx_recursed(sx)) { 10367ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 10374e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 10384e7f640dSJohn Baldwin sx->lock_object.lo_name, file, 10394e7f640dSJohn Baldwin line); 10407ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 10414e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 10424e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 10434e7f640dSJohn Baldwin } 10444e5e677bSJohn Baldwin #endif 10454e5e677bSJohn Baldwin break; 10467ec137e5SJohn Baldwin case SA_XLOCKED: 10477ec137e5SJohn Baldwin case SA_XLOCKED | SA_NOTRECURSED: 10487ec137e5SJohn Baldwin case SA_XLOCKED | SA_RECURSED: 10494e7f640dSJohn Baldwin if (sx_xholder(sx) != curthread) 105003129ba9SJohn Baldwin panic("Lock %s not exclusively locked @ %s:%d\n", 1051aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 10524e7f640dSJohn Baldwin if (sx_recursed(sx)) { 10537ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 10544e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 10554e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 10567ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 10574e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 10584e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 10594e5e677bSJohn Baldwin break; 10607ec137e5SJohn Baldwin case SA_UNLOCKED: 106119b0efd3SPawel Jakub Dawidek #ifdef WITNESS 1062aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 106319b0efd3SPawel Jakub Dawidek #else 1064f6739b1dSPawel Jakub Dawidek /* 10654e7f640dSJohn Baldwin * If we hold an exclusve lock fail. We can't 10664e7f640dSJohn Baldwin * reliably check to see if we hold a shared lock or 10674e7f640dSJohn Baldwin * not. 1068f6739b1dSPawel Jakub Dawidek */ 10694e7f640dSJohn Baldwin if (sx_xholder(sx) == curthread) 107003129ba9SJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 1071aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 107219b0efd3SPawel Jakub Dawidek #endif 107319b0efd3SPawel Jakub Dawidek break; 10744e5e677bSJohn Baldwin default: 10754e5e677bSJohn Baldwin panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 10764e5e677bSJohn Baldwin line); 10774e5e677bSJohn Baldwin } 10784e5e677bSJohn Baldwin } 10794e5e677bSJohn Baldwin #endif /* INVARIANT_SUPPORT */ 1080d272fe53SJohn Baldwin 1081d272fe53SJohn Baldwin #ifdef DDB 10824e7f640dSJohn Baldwin static void 1083d272fe53SJohn Baldwin db_show_sx(struct lock_object *lock) 1084d272fe53SJohn Baldwin { 1085d272fe53SJohn Baldwin struct thread *td; 1086d272fe53SJohn Baldwin struct sx *sx; 1087d272fe53SJohn Baldwin 1088d272fe53SJohn Baldwin sx = (struct sx *)lock; 1089d272fe53SJohn Baldwin 1090d272fe53SJohn Baldwin db_printf(" state: "); 10914e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED) 10924e7f640dSJohn Baldwin db_printf("UNLOCKED\n"); 10930026c92cSJohn Baldwin else if (sx->sx_lock == SX_LOCK_DESTROYED) { 10940026c92cSJohn Baldwin db_printf("DESTROYED\n"); 10950026c92cSJohn Baldwin return; 10960026c92cSJohn Baldwin } else if (sx->sx_lock & SX_LOCK_SHARED) 10974e7f640dSJohn Baldwin db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 10984e7f640dSJohn Baldwin else { 10994e7f640dSJohn Baldwin td = sx_xholder(sx); 1100d272fe53SJohn Baldwin db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1101431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 11024e7f640dSJohn Baldwin if (sx_recursed(sx)) 11034e7f640dSJohn Baldwin db_printf(" recursed: %d\n", sx->sx_recurse); 11044e7f640dSJohn Baldwin } 11054e7f640dSJohn Baldwin 11064e7f640dSJohn Baldwin db_printf(" waiters: "); 11074e7f640dSJohn Baldwin switch(sx->sx_lock & 11084e7f640dSJohn Baldwin (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 11094e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS: 11104e7f640dSJohn Baldwin db_printf("shared\n"); 11114e7f640dSJohn Baldwin break; 11124e7f640dSJohn Baldwin case SX_LOCK_EXCLUSIVE_WAITERS: 11134e7f640dSJohn Baldwin db_printf("exclusive\n"); 11144e7f640dSJohn Baldwin break; 11154e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 11164e7f640dSJohn Baldwin db_printf("exclusive and shared\n"); 11174e7f640dSJohn Baldwin break; 11184e7f640dSJohn Baldwin default: 11194e7f640dSJohn Baldwin db_printf("none\n"); 11204e7f640dSJohn Baldwin } 1121d272fe53SJohn Baldwin } 1122462a7addSJohn Baldwin 1123462a7addSJohn Baldwin /* 1124462a7addSJohn Baldwin * Check to see if a thread that is blocked on a sleep queue is actually 1125462a7addSJohn Baldwin * blocked on an sx lock. If so, output some details and return true. 1126462a7addSJohn Baldwin * If the lock has an exclusive owner, return that in *ownerp. 1127462a7addSJohn Baldwin */ 1128462a7addSJohn Baldwin int 1129462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp) 1130462a7addSJohn Baldwin { 1131462a7addSJohn Baldwin struct sx *sx; 1132462a7addSJohn Baldwin 1133462a7addSJohn Baldwin /* 11344e7f640dSJohn Baldwin * Check to see if this thread is blocked on an sx lock. 11354e7f640dSJohn Baldwin * First, we check the lock class. If that is ok, then we 11364e7f640dSJohn Baldwin * compare the lock name against the wait message. 1137462a7addSJohn Baldwin */ 11384e7f640dSJohn Baldwin sx = td->td_wchan; 11394e7f640dSJohn Baldwin if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 11404e7f640dSJohn Baldwin sx->lock_object.lo_name != td->td_wmesg) 1141462a7addSJohn Baldwin return (0); 1142462a7addSJohn Baldwin 1143462a7addSJohn Baldwin /* We think we have an sx lock, so output some details. */ 1144462a7addSJohn Baldwin db_printf("blocked on sx \"%s\" ", td->td_wmesg); 11454e7f640dSJohn Baldwin *ownerp = sx_xholder(sx); 11464e7f640dSJohn Baldwin if (sx->sx_lock & SX_LOCK_SHARED) 11474e7f640dSJohn Baldwin db_printf("SLOCK (count %ju)\n", 11484e7f640dSJohn Baldwin (uintmax_t)SX_SHARERS(sx->sx_lock)); 11494e7f640dSJohn Baldwin else 1150462a7addSJohn Baldwin db_printf("XLOCK\n"); 1151462a7addSJohn Baldwin return (1); 1152462a7addSJohn Baldwin } 1153d272fe53SJohn Baldwin #endif 1154