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" 40f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.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> 477a7ce668SAndriy Gapon #include <sys/systm.h> 48cd2fe4e6SAttilio Rao #include <sys/kdb.h> 490453ade5SMateusz Guzik #include <sys/kernel.h> 506281b30aSJason Evans #include <sys/ktr.h> 5119284646SJohn Baldwin #include <sys/lock.h> 526281b30aSJason Evans #include <sys/mutex.h> 53d272fe53SJohn Baldwin #include <sys/proc.h> 542cba8dd3SJohn Baldwin #include <sys/sched.h> 554e7f640dSJohn Baldwin #include <sys/sleepqueue.h> 566281b30aSJason Evans #include <sys/sx.h> 571ada9041SMateusz Guzik #include <sys/smp.h> 58e31d0833SAttilio Rao #include <sys/sysctl.h> 594e7f640dSJohn Baldwin 60e31d0833SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 614e7f640dSJohn Baldwin #include <machine/cpu.h> 624e7f640dSJohn Baldwin #endif 636281b30aSJason Evans 64462a7addSJohn Baldwin #ifdef DDB 65d272fe53SJohn Baldwin #include <ddb/ddb.h> 664e7f640dSJohn Baldwin #endif 67d272fe53SJohn Baldwin 681ae1c2a3SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 691ae1c2a3SAttilio Rao #define ADAPTIVE_SX 704e7f640dSJohn Baldwin #endif 714e7f640dSJohn Baldwin 72f0830182SAttilio Rao CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE); 73c1a6d9faSAttilio Rao 74f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 75f5f9340bSFabien Thomas #include <sys/pmckern.h> 76f5f9340bSFabien Thomas PMC_SOFT_DECLARE( , , lock, failed); 77f5f9340bSFabien Thomas #endif 78f5f9340bSFabien Thomas 794e7f640dSJohn Baldwin /* Handy macros for sleep queues. */ 804e7f640dSJohn Baldwin #define SQ_EXCLUSIVE_QUEUE 0 814e7f640dSJohn Baldwin #define SQ_SHARED_QUEUE 1 824e7f640dSJohn Baldwin 834e7f640dSJohn Baldwin /* 844e7f640dSJohn Baldwin * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We 854e7f640dSJohn Baldwin * drop Giant anytime we have to sleep or if we adaptively spin. 864e7f640dSJohn Baldwin */ 874e7f640dSJohn Baldwin #define GIANT_DECLARE \ 884e7f640dSJohn Baldwin int _giantcnt = 0; \ 894e7f640dSJohn Baldwin WITNESS_SAVE_DECL(Giant) \ 904e7f640dSJohn Baldwin 91e41d6166SMateusz Guzik #define GIANT_SAVE(work) do { \ 924e7f640dSJohn Baldwin if (mtx_owned(&Giant)) { \ 93e41d6166SMateusz Guzik work++; \ 944e7f640dSJohn Baldwin WITNESS_SAVE(&Giant.lock_object, Giant); \ 954e7f640dSJohn Baldwin while (mtx_owned(&Giant)) { \ 964e7f640dSJohn Baldwin _giantcnt++; \ 974e7f640dSJohn Baldwin mtx_unlock(&Giant); \ 984e7f640dSJohn Baldwin } \ 994e7f640dSJohn Baldwin } \ 1004e7f640dSJohn Baldwin } while (0) 1014e7f640dSJohn Baldwin 1024e7f640dSJohn Baldwin #define GIANT_RESTORE() do { \ 1034e7f640dSJohn Baldwin if (_giantcnt > 0) { \ 1044e7f640dSJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); \ 1054e7f640dSJohn Baldwin while (_giantcnt--) \ 1064e7f640dSJohn Baldwin mtx_lock(&Giant); \ 1074e7f640dSJohn Baldwin WITNESS_RESTORE(&Giant.lock_object, Giant); \ 1084e7f640dSJohn Baldwin } \ 1094e7f640dSJohn Baldwin } while (0) 1104e7f640dSJohn Baldwin 1114e7f640dSJohn Baldwin /* 112da7d0d1eSJohn Baldwin * Returns true if an exclusive lock is recursed. It assumes 113da7d0d1eSJohn Baldwin * curthread currently has an exclusive lock. 1144e7f640dSJohn Baldwin */ 1154e7f640dSJohn Baldwin #define sx_recursed(sx) ((sx)->sx_recurse != 0) 1164e7f640dSJohn Baldwin 117d576deedSPawel Jakub Dawidek static void assert_sx(const struct lock_object *lock, int what); 1184e7f640dSJohn Baldwin #ifdef DDB 119d576deedSPawel Jakub Dawidek static void db_show_sx(const struct lock_object *lock); 120d272fe53SJohn Baldwin #endif 1217faf4d90SDavide Italiano static void lock_sx(struct lock_object *lock, uintptr_t how); 122a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 123d576deedSPawel Jakub Dawidek static int owner_sx(const struct lock_object *lock, struct thread **owner); 124a5aedd68SStacey Son #endif 1257faf4d90SDavide Italiano static uintptr_t unlock_sx(struct lock_object *lock); 126d272fe53SJohn Baldwin 12719284646SJohn Baldwin struct lock_class lock_class_sx = { 128ae8dde30SJohn Baldwin .lc_name = "sx", 129ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, 130f9721b43SAttilio Rao .lc_assert = assert_sx, 131d272fe53SJohn Baldwin #ifdef DDB 132ae8dde30SJohn Baldwin .lc_ddb_show = db_show_sx, 133d272fe53SJohn Baldwin #endif 1346e21afd4SJohn Baldwin .lc_lock = lock_sx, 1356e21afd4SJohn Baldwin .lc_unlock = unlock_sx, 136a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 137a5aedd68SStacey Son .lc_owner = owner_sx, 138a5aedd68SStacey Son #endif 13919284646SJohn Baldwin }; 14019284646SJohn Baldwin 141781a35dfSJohn Baldwin #ifndef INVARIANTS 142781a35dfSJohn Baldwin #define _sx_assert(sx, what, file, line) 143781a35dfSJohn Baldwin #endif 144781a35dfSJohn Baldwin 1451ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX 146574adb65SMateusz Guzik static __read_frequently u_int asx_retries = 10; 147574adb65SMateusz Guzik static __read_frequently u_int asx_loops = 10000; 1486472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging"); 149fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, ""); 150fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, ""); 1511ada9041SMateusz Guzik 152574adb65SMateusz Guzik static struct lock_delay_config __read_frequently sx_delay; 1531ada9041SMateusz Guzik 1548e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_sx, OID_AUTO, delay_base, CTLFLAG_RW, &sx_delay.base, 1551ada9041SMateusz Guzik 0, ""); 1561ada9041SMateusz Guzik SYSCTL_INT(_debug_sx, OID_AUTO, delay_max, CTLFLAG_RW, &sx_delay.max, 1571ada9041SMateusz Guzik 0, ""); 1581ada9041SMateusz Guzik 1598e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(sx_delay); 1601ae1c2a3SAttilio Rao #endif 1611ae1c2a3SAttilio Rao 1626281b30aSJason Evans void 163d576deedSPawel Jakub Dawidek assert_sx(const struct lock_object *lock, int what) 164f9721b43SAttilio Rao { 165f9721b43SAttilio Rao 166d576deedSPawel Jakub Dawidek sx_assert((const struct sx *)lock, what); 167f9721b43SAttilio Rao } 168f9721b43SAttilio Rao 169f9721b43SAttilio Rao void 1707faf4d90SDavide Italiano lock_sx(struct lock_object *lock, uintptr_t how) 1716e21afd4SJohn Baldwin { 1726e21afd4SJohn Baldwin struct sx *sx; 1736e21afd4SJohn Baldwin 1746e21afd4SJohn Baldwin sx = (struct sx *)lock; 1756e21afd4SJohn Baldwin if (how) 1766e21afd4SJohn Baldwin sx_slock(sx); 177cf6b879fSDavide Italiano else 178cf6b879fSDavide Italiano sx_xlock(sx); 1796e21afd4SJohn Baldwin } 1806e21afd4SJohn Baldwin 1817faf4d90SDavide Italiano uintptr_t 1826e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock) 1836e21afd4SJohn Baldwin { 1846e21afd4SJohn Baldwin struct sx *sx; 1856e21afd4SJohn Baldwin 1866e21afd4SJohn Baldwin sx = (struct sx *)lock; 1877ec137e5SJohn Baldwin sx_assert(sx, SA_LOCKED | SA_NOTRECURSED); 1886e21afd4SJohn Baldwin if (sx_xlocked(sx)) { 1896e21afd4SJohn Baldwin sx_xunlock(sx); 190cf6b879fSDavide Italiano return (0); 1916e21afd4SJohn Baldwin } else { 1926e21afd4SJohn Baldwin sx_sunlock(sx); 193cf6b879fSDavide Italiano return (1); 1946e21afd4SJohn Baldwin } 1956e21afd4SJohn Baldwin } 1966e21afd4SJohn Baldwin 197a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 198a5aedd68SStacey Son int 199d576deedSPawel Jakub Dawidek owner_sx(const struct lock_object *lock, struct thread **owner) 200a5aedd68SStacey Son { 201c365a293SMark Johnston const struct sx *sx; 202c365a293SMark Johnston uintptr_t x; 203a5aedd68SStacey Son 204c365a293SMark Johnston sx = (const struct sx *)lock; 205c365a293SMark Johnston x = sx->sx_lock; 206c365a293SMark Johnston *owner = NULL; 207a5aedd68SStacey Son return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) : 208c365a293SMark Johnston ((*owner = (struct thread *)SX_OWNER(x)) != NULL)); 209a5aedd68SStacey Son } 210a5aedd68SStacey Son #endif 211a5aedd68SStacey Son 2126e21afd4SJohn Baldwin void 213c27b5699SAndrew R. Reiter sx_sysinit(void *arg) 214c27b5699SAndrew R. Reiter { 215c27b5699SAndrew R. Reiter struct sx_args *sargs = arg; 216c27b5699SAndrew R. Reiter 217e4cd31ddSJeff Roberson sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags); 218c27b5699SAndrew R. Reiter } 219c27b5699SAndrew R. Reiter 220c27b5699SAndrew R. Reiter void 2214e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts) 2226281b30aSJason Evans { 2234e7f640dSJohn Baldwin int flags; 2246281b30aSJason Evans 225b0d67325SJohn Baldwin MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK | 226fd07ddcfSDmitry Chagin SX_NOPROFILE | SX_NOADAPTIVE | SX_NEW)) == 0); 227353998acSAttilio Rao ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock, 228353998acSAttilio Rao ("%s: sx_lock not aligned for %s: %p", __func__, description, 229353998acSAttilio Rao &sx->sx_lock)); 230b0d67325SJohn Baldwin 231f0830182SAttilio Rao flags = LO_SLEEPABLE | LO_UPGRADABLE; 2324e7f640dSJohn Baldwin if (opts & SX_DUPOK) 2334e7f640dSJohn Baldwin flags |= LO_DUPOK; 2344e7f640dSJohn Baldwin if (opts & SX_NOPROFILE) 2354e7f640dSJohn Baldwin flags |= LO_NOPROFILE; 2364e7f640dSJohn Baldwin if (!(opts & SX_NOWITNESS)) 2374e7f640dSJohn Baldwin flags |= LO_WITNESS; 238f0830182SAttilio Rao if (opts & SX_RECURSE) 239f0830182SAttilio Rao flags |= LO_RECURSABLE; 2404e7f640dSJohn Baldwin if (opts & SX_QUIET) 2414e7f640dSJohn Baldwin flags |= LO_QUIET; 242fd07ddcfSDmitry Chagin if (opts & SX_NEW) 243fd07ddcfSDmitry Chagin flags |= LO_NEW; 2444e7f640dSJohn Baldwin 245f0830182SAttilio Rao flags |= opts & SX_NOADAPTIVE; 246b5fb43e5SJohn Baldwin lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags); 2474e7f640dSJohn Baldwin sx->sx_lock = SX_LOCK_UNLOCKED; 2484e7f640dSJohn Baldwin sx->sx_recurse = 0; 2496281b30aSJason Evans } 2506281b30aSJason Evans 2516281b30aSJason Evans void 2526281b30aSJason Evans sx_destroy(struct sx *sx) 2536281b30aSJason Evans { 2546281b30aSJason Evans 2554e7f640dSJohn Baldwin KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held")); 2564e7f640dSJohn Baldwin KASSERT(sx->sx_recurse == 0, ("sx lock still recursed")); 2570026c92cSJohn Baldwin sx->sx_lock = SX_LOCK_DESTROYED; 258aa89d8cdSJohn Baldwin lock_destroy(&sx->lock_object); 2596281b30aSJason Evans } 2606281b30aSJason Evans 261f9819486SAttilio Rao int 262*013c0b49SMateusz Guzik sx_try_slock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 2635f36700aSJohn Baldwin { 2644e7f640dSJohn Baldwin uintptr_t x; 2655f36700aSJohn Baldwin 26635370593SAndriy Gapon if (SCHEDULER_STOPPED()) 26735370593SAndriy Gapon return (1); 26835370593SAndriy Gapon 269cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 270e3ae0dfeSAttilio Rao ("sx_try_slock() by idle thread %p on sx %s @ %s:%d", 271e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 272e3ae0dfeSAttilio Rao 2734e7f640dSJohn Baldwin x = sx->sx_lock; 2745c5df0d9SMateusz Guzik for (;;) { 2750026c92cSJohn Baldwin KASSERT(x != SX_LOCK_DESTROYED, 2760026c92cSJohn Baldwin ("sx_try_slock() of destroyed sx @ %s:%d", file, line)); 277764a938bSPawel Jakub Dawidek if (!(x & SX_LOCK_SHARED)) 278764a938bSPawel Jakub Dawidek break; 2795c5df0d9SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, x + SX_ONE_SHARER)) { 280aa89d8cdSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line); 281aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line); 282de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, 283de2c95ccSMark Johnston sx, 0, 0, file, line, LOCKSTAT_READER); 284ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 2855f36700aSJohn Baldwin return (1); 2865f36700aSJohn Baldwin } 287764a938bSPawel Jakub Dawidek } 2884e7f640dSJohn Baldwin 2894e7f640dSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line); 2904e7f640dSJohn Baldwin return (0); 2915f36700aSJohn Baldwin } 2925f36700aSJohn Baldwin 293f9819486SAttilio Rao int 294*013c0b49SMateusz Guzik sx_try_slock_(struct sx *sx, const char *file, int line) 295*013c0b49SMateusz Guzik { 296*013c0b49SMateusz Guzik 297*013c0b49SMateusz Guzik return (sx_try_slock_int(sx LOCK_FILE_LINE_ARG)); 298*013c0b49SMateusz Guzik } 299*013c0b49SMateusz Guzik 300*013c0b49SMateusz Guzik int 301f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line) 3026281b30aSJason Evans { 3036ebb77b6SMateusz Guzik uintptr_t tid, x; 304f9819486SAttilio Rao int error = 0; 3056281b30aSJason Evans 306704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 307704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 308e3ae0dfeSAttilio Rao ("sx_xlock() by idle thread %p on sx %s @ %s:%d", 309e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 3100026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3110026c92cSJohn Baldwin ("sx_xlock() of destroyed sx @ %s:%d", file, line)); 312aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 31341313430SJohn Baldwin line, NULL); 3146ebb77b6SMateusz Guzik tid = (uintptr_t)curthread; 3156ebb77b6SMateusz Guzik x = SX_LOCK_UNLOCKED; 3166ebb77b6SMateusz Guzik if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 317*013c0b49SMateusz Guzik error = _sx_xlock_hard(sx, x, opts LOCK_FILE_LINE_ARG); 3186ebb77b6SMateusz Guzik else 3196ebb77b6SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 3206ebb77b6SMateusz Guzik 0, 0, file, line, LOCKSTAT_WRITER); 321f9819486SAttilio Rao if (!error) { 322f9819486SAttilio Rao LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, 323f9819486SAttilio Rao file, line); 324aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 325ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 3266281b30aSJason Evans } 3276281b30aSJason Evans 328f9819486SAttilio Rao return (error); 329f9819486SAttilio Rao } 330f9819486SAttilio Rao 3315f36700aSJohn Baldwin int 332*013c0b49SMateusz Guzik sx_try_xlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 3335f36700aSJohn Baldwin { 3345c5df0d9SMateusz Guzik struct thread *td; 3355c5df0d9SMateusz Guzik uintptr_t tid, x; 3364e7f640dSJohn Baldwin int rval; 3375c5df0d9SMateusz Guzik bool recursed; 3385f36700aSJohn Baldwin 3395c5df0d9SMateusz Guzik td = curthread; 3405c5df0d9SMateusz Guzik tid = (uintptr_t)td; 3415c5df0d9SMateusz Guzik if (SCHEDULER_STOPPED_TD(td)) 34235370593SAndriy Gapon return (1); 34335370593SAndriy Gapon 344704cb42fSMark Johnston KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td), 345e3ae0dfeSAttilio Rao ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d", 346e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 3470026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3480026c92cSJohn Baldwin ("sx_try_xlock() of destroyed sx @ %s:%d", file, line)); 3494e7f640dSJohn Baldwin 3505c5df0d9SMateusz Guzik rval = 1; 3515c5df0d9SMateusz Guzik recursed = false; 3525c5df0d9SMateusz Guzik x = SX_LOCK_UNLOCKED; 353b247fd39SMateusz Guzik for (;;) { 354b247fd39SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 355b247fd39SMateusz Guzik break; 356b247fd39SMateusz Guzik if (x == SX_LOCK_UNLOCKED) 357b247fd39SMateusz Guzik continue; 3585c5df0d9SMateusz Guzik if (x == tid && (sx->lock_object.lo_flags & LO_RECURSABLE)) { 3594e7f640dSJohn Baldwin sx->sx_recurse++; 3604e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 361b247fd39SMateusz Guzik break; 3625c5df0d9SMateusz Guzik } 363b247fd39SMateusz Guzik rval = 0; 364b247fd39SMateusz Guzik break; 3655c5df0d9SMateusz Guzik } 3665c5df0d9SMateusz Guzik 3674e7f640dSJohn Baldwin LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line); 3684e7f640dSJohn Baldwin if (rval) { 3694e7f640dSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 3704e7f640dSJohn Baldwin file, line); 3715c5df0d9SMateusz Guzik if (!recursed) 372de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, 373de2c95ccSMark Johnston sx, 0, 0, file, line, LOCKSTAT_WRITER); 374ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 3755f36700aSJohn Baldwin } 3764e7f640dSJohn Baldwin 3774e7f640dSJohn Baldwin return (rval); 3785f36700aSJohn Baldwin } 3795f36700aSJohn Baldwin 380*013c0b49SMateusz Guzik int 381*013c0b49SMateusz Guzik sx_try_xlock_(struct sx *sx, const char *file, int line) 382*013c0b49SMateusz Guzik { 383*013c0b49SMateusz Guzik 384*013c0b49SMateusz Guzik return (sx_try_xlock_int(sx LOCK_FILE_LINE_ARG)); 385*013c0b49SMateusz Guzik } 386*013c0b49SMateusz Guzik 3876281b30aSJason Evans void 38819284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line) 3896281b30aSJason Evans { 3906281b30aSJason Evans 3910026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3920026c92cSJohn Baldwin ("sx_xunlock() of destroyed sx @ %s:%d", file, line)); 3937ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED, file, line); 394aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 3954e7f640dSJohn Baldwin LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file, 3964e7f640dSJohn Baldwin line); 3970108a980SMateusz Guzik #if LOCK_DEBUG > 0 3986ebb77b6SMateusz Guzik _sx_xunlock_hard(sx, (uintptr_t)curthread, file, line); 399ffd5c94cSMateusz Guzik #else 400ffd5c94cSMateusz Guzik __sx_xunlock(sx, curthread, file, line); 401ffd5c94cSMateusz Guzik #endif 402ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 4036281b30aSJason Evans } 404d55229b7SJason Evans 4054e7f640dSJohn Baldwin /* 4064e7f640dSJohn Baldwin * Try to do a non-blocking upgrade from a shared lock to an exclusive lock. 4074e7f640dSJohn Baldwin * This will only succeed if this thread holds a single shared lock. 4084e7f640dSJohn Baldwin * Return 1 if if the upgrade succeed, 0 otherwise. 4094e7f640dSJohn Baldwin */ 410d55229b7SJason Evans int 411*013c0b49SMateusz Guzik sx_try_upgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 412d55229b7SJason Evans { 4134e7f640dSJohn Baldwin uintptr_t x; 4144e7f640dSJohn Baldwin int success; 415d55229b7SJason Evans 41635370593SAndriy Gapon if (SCHEDULER_STOPPED()) 41735370593SAndriy Gapon return (1); 41835370593SAndriy Gapon 4190026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4200026c92cSJohn Baldwin ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 4217ec137e5SJohn Baldwin _sx_assert(sx, SA_SLOCKED, file, line); 422d55229b7SJason Evans 4234e7f640dSJohn Baldwin /* 4244e7f640dSJohn Baldwin * Try to switch from one shared lock to an exclusive lock. We need 4254e7f640dSJohn Baldwin * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 4264e7f640dSJohn Baldwin * we will wake up the exclusive waiters when we drop the lock. 4274e7f640dSJohn Baldwin */ 4284e7f640dSJohn Baldwin x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS; 429a2101806SMateusz Guzik success = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, 4304e7f640dSJohn Baldwin (uintptr_t)curthread | x); 4314e7f640dSJohn Baldwin LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 432a5aedd68SStacey Son if (success) { 433aa89d8cdSJohn Baldwin WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 434b0b7cb50SJohn Baldwin file, line); 43532cd0147SMark Johnston LOCKSTAT_RECORD0(sx__upgrade, sx); 436a5aedd68SStacey Son } 4374e7f640dSJohn Baldwin return (success); 438d55229b7SJason Evans } 439d55229b7SJason Evans 440*013c0b49SMateusz Guzik int 441*013c0b49SMateusz Guzik sx_try_upgrade_(struct sx *sx, const char *file, int line) 442*013c0b49SMateusz Guzik { 443*013c0b49SMateusz Guzik 444*013c0b49SMateusz Guzik return (sx_try_upgrade_int(sx LOCK_FILE_LINE_ARG)); 445*013c0b49SMateusz Guzik } 446*013c0b49SMateusz Guzik 4474e7f640dSJohn Baldwin /* 4484e7f640dSJohn Baldwin * Downgrade an unrecursed exclusive lock into a single shared lock. 4494e7f640dSJohn Baldwin */ 450d55229b7SJason Evans void 451*013c0b49SMateusz Guzik sx_downgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 452d55229b7SJason Evans { 4534e7f640dSJohn Baldwin uintptr_t x; 454da7bbd2cSJohn Baldwin int wakeup_swapper; 455d55229b7SJason Evans 45635370593SAndriy Gapon if (SCHEDULER_STOPPED()) 45735370593SAndriy Gapon return; 45835370593SAndriy Gapon 4590026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4600026c92cSJohn Baldwin ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 4617ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 4624e7f640dSJohn Baldwin #ifndef INVARIANTS 4634e7f640dSJohn Baldwin if (sx_recursed(sx)) 4644e7f640dSJohn Baldwin panic("downgrade of a recursed lock"); 4654e7f640dSJohn Baldwin #endif 466d55229b7SJason Evans 467aa89d8cdSJohn Baldwin WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 468d55229b7SJason Evans 4694e7f640dSJohn Baldwin /* 4704e7f640dSJohn Baldwin * Try to switch from an exclusive lock with no shared waiters 4714e7f640dSJohn Baldwin * to one sharer with no shared waiters. If there are 4724e7f640dSJohn Baldwin * exclusive waiters, we don't need to lock the sleep queue so 4734e7f640dSJohn Baldwin * long as we preserve the flag. We do one quick try and if 4744e7f640dSJohn Baldwin * that fails we grab the sleepq lock to keep the flags from 4754e7f640dSJohn Baldwin * changing and do it the slow way. 4764e7f640dSJohn Baldwin * 4774e7f640dSJohn Baldwin * We have to lock the sleep queue if there are shared waiters 4784e7f640dSJohn Baldwin * so we can wake them up. 4794e7f640dSJohn Baldwin */ 4804e7f640dSJohn Baldwin x = sx->sx_lock; 4814e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS) && 4824e7f640dSJohn Baldwin atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 4834e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS))) { 4844e7f640dSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 4854e7f640dSJohn Baldwin return; 4864e7f640dSJohn Baldwin } 4874e7f640dSJohn Baldwin 4884e7f640dSJohn Baldwin /* 4894e7f640dSJohn Baldwin * Lock the sleep queue so we can read the waiters bits 4904e7f640dSJohn Baldwin * without any races and wakeup any shared waiters. 4914e7f640dSJohn Baldwin */ 4924e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 4934e7f640dSJohn Baldwin 4944e7f640dSJohn Baldwin /* 4954e7f640dSJohn Baldwin * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 4964e7f640dSJohn Baldwin * shared lock. If there are any shared waiters, wake them up. 4974e7f640dSJohn Baldwin */ 498da7bbd2cSJohn Baldwin wakeup_swapper = 0; 4994e7f640dSJohn Baldwin x = sx->sx_lock; 5004e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 5014e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS)); 5024e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED_WAITERS) 503da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 504da7bbd2cSJohn Baldwin 0, SQ_SHARED_QUEUE); 5054e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 506d55229b7SJason Evans 507aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 50832cd0147SMark Johnston LOCKSTAT_RECORD0(sx__downgrade, sx); 509da7bbd2cSJohn Baldwin 510da7bbd2cSJohn Baldwin if (wakeup_swapper) 511da7bbd2cSJohn Baldwin kick_proc0(); 5124e7f640dSJohn Baldwin } 513d55229b7SJason Evans 514*013c0b49SMateusz Guzik void 515*013c0b49SMateusz Guzik sx_downgrade_(struct sx *sx, const char *file, int line) 516*013c0b49SMateusz Guzik { 517*013c0b49SMateusz Guzik 518*013c0b49SMateusz Guzik sx_downgrade_int(sx LOCK_FILE_LINE_ARG); 519*013c0b49SMateusz Guzik } 520*013c0b49SMateusz Guzik 5214e7f640dSJohn Baldwin /* 5224e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xlock 5234e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 5244e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 5254e7f640dSJohn Baldwin * accessible from at least sx.h. 5264e7f640dSJohn Baldwin */ 527f9819486SAttilio Rao int 528*013c0b49SMateusz Guzik _sx_xlock_hard(struct sx *sx, uintptr_t x, int opts LOCK_FILE_LINE_ARG_DEF) 5294e7f640dSJohn Baldwin { 5304e7f640dSJohn Baldwin GIANT_DECLARE; 531*013c0b49SMateusz Guzik uintptr_t tid; 5324e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5334e7f640dSJohn Baldwin volatile struct thread *owner; 534d07e22cdSMateusz Guzik u_int i, n, spintries = 0; 5354e7f640dSJohn Baldwin #endif 5361723a064SJeff Roberson #ifdef LOCK_PROFILING 5371723a064SJeff Roberson uint64_t waittime = 0; 5381723a064SJeff Roberson int contested = 0; 5391723a064SJeff Roberson #endif 5401723a064SJeff Roberson int error = 0; 54104126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 5421ada9041SMateusz Guzik struct lock_delay_arg lda; 5431ada9041SMateusz Guzik #endif 544a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 54561852185SMateusz Guzik u_int sleep_cnt = 0; 546a5aedd68SStacey Son int64_t sleep_time = 0; 547076dd8ebSAndriy Gapon int64_t all_time = 0; 548a5aedd68SStacey Son #endif 549e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 550e41d6166SMateusz Guzik uintptr_t state; 551e41d6166SMateusz Guzik #endif 552284194f1SMateusz Guzik int extra_work = 0; 5534e7f640dSJohn Baldwin 554*013c0b49SMateusz Guzik tid = (uintptr_t)curthread; 55535370593SAndriy Gapon if (SCHEDULER_STOPPED()) 55635370593SAndriy Gapon return (0); 55735370593SAndriy Gapon 558fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 5591ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 560fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 561fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 5621ada9041SMateusz Guzik #endif 5631ada9041SMateusz Guzik 564c1aaf63cSMateusz Guzik if (__predict_false(x == SX_LOCK_UNLOCKED)) 565c1aaf63cSMateusz Guzik x = SX_READ_VALUE(sx); 566c1aaf63cSMateusz Guzik 5674e7f640dSJohn Baldwin /* If we already hold an exclusive lock, then recurse. */ 568c5f61e6fSMateusz Guzik if (__predict_false(lv_sx_owner(x) == (struct thread *)tid)) { 569f0830182SAttilio Rao KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0, 570b0d67325SJohn Baldwin ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 571b0d67325SJohn Baldwin sx->lock_object.lo_name, file, line)); 5724e7f640dSJohn Baldwin sx->sx_recurse++; 5734e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 5744e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5754e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 576f9819486SAttilio Rao return (0); 5774e7f640dSJohn Baldwin } 5784e7f640dSJohn Baldwin 5794e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5804e7f640dSJohn Baldwin CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 5814e7f640dSJohn Baldwin sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 5824e7f640dSJohn Baldwin 583ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS 584ae7d25a4SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 585ae7d25a4SMateusz Guzik #endif 586ae7d25a4SMateusz Guzik lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 587ae7d25a4SMateusz Guzik &waittime); 588ae7d25a4SMateusz Guzik 589e41d6166SMateusz Guzik #ifdef LOCK_PROFILING 590e41d6166SMateusz Guzik extra_work = 1; 591e41d6166SMateusz Guzik state = x; 592e41d6166SMateusz Guzik #elif defined(KDTRACE_HOOKS) 593e41d6166SMateusz Guzik extra_work = lockstat_enabled; 594e41d6166SMateusz Guzik if (__predict_false(extra_work)) { 595e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 596c5f61e6fSMateusz Guzik state = x; 597e41d6166SMateusz Guzik } 598076dd8ebSAndriy Gapon #endif 599e41d6166SMateusz Guzik 600fc4f686dSMateusz Guzik for (;;) { 601c5f61e6fSMateusz Guzik if (x == SX_LOCK_UNLOCKED) { 602fa474043SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 603fc4f686dSMateusz Guzik break; 604c5f61e6fSMateusz Guzik continue; 605c5f61e6fSMateusz Guzik } 606a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 6071ada9041SMateusz Guzik lda.spin_cnt++; 608a5aedd68SStacey Son #endif 6094e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 6104e7f640dSJohn Baldwin /* 6114e7f640dSJohn Baldwin * If the lock is write locked and the owner is 6124e7f640dSJohn Baldwin * running on another CPU, spin until the owner stops 6134e7f640dSJohn Baldwin * running or the state of the lock changes. 6144e7f640dSJohn Baldwin */ 6158545538bSJohn Baldwin if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 6161ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0) { 617c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 6184e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 6194e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6204e7f640dSJohn Baldwin CTR3(KTR_LOCK, 6214e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 6224e7f640dSJohn Baldwin __func__, sx, owner); 6232cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 6242cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 6252cba8dd3SJohn Baldwin "lockname:\"%s\"", 6262cba8dd3SJohn Baldwin sx->lock_object.lo_name); 627e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 628c5f61e6fSMateusz Guzik do { 6291ada9041SMateusz Guzik lock_delay(&lda); 630c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 631c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 632c5f61e6fSMateusz Guzik } while (owner != NULL && 633c5f61e6fSMateusz Guzik TD_IS_RUNNING(owner)); 6342cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 6352cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 6364e7f640dSJohn Baldwin continue; 6374e7f640dSJohn Baldwin } 6381ae1c2a3SAttilio Rao } else if (SX_SHARERS(x) && spintries < asx_retries) { 6392cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 6402cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 6412cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 642e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 6431ae1c2a3SAttilio Rao spintries++; 644d07e22cdSMateusz Guzik for (i = 0; i < asx_loops; i += n) { 6451ae1c2a3SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6461ae1c2a3SAttilio Rao CTR4(KTR_LOCK, 6471ae1c2a3SAttilio Rao "%s: shared spinning on %p with %u and %u", 6481ae1c2a3SAttilio Rao __func__, sx, spintries, i); 649d07e22cdSMateusz Guzik n = SX_SHARERS(x); 650d07e22cdSMateusz Guzik lock_delay_spin(n); 65120a15d17SMateusz Guzik x = SX_READ_VALUE(sx); 6521ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0 || 6531ae1c2a3SAttilio Rao SX_SHARERS(x) == 0) 6541ae1c2a3SAttilio Rao break; 6551ae1c2a3SAttilio Rao } 65620a15d17SMateusz Guzik #ifdef KDTRACE_HOOKS 65720a15d17SMateusz Guzik lda.spin_cnt += i; 65820a15d17SMateusz Guzik #endif 6592cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 6602cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 6611ae1c2a3SAttilio Rao if (i != asx_loops) 6621ae1c2a3SAttilio Rao continue; 6631ae1c2a3SAttilio Rao } 6644e7f640dSJohn Baldwin } 6654e7f640dSJohn Baldwin #endif 6664e7f640dSJohn Baldwin 6674e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 668c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6694e7f640dSJohn Baldwin 6704e7f640dSJohn Baldwin /* 6714e7f640dSJohn Baldwin * If the lock was released while spinning on the 6724e7f640dSJohn Baldwin * sleep queue chain lock, try again. 6734e7f640dSJohn Baldwin */ 6744e7f640dSJohn Baldwin if (x == SX_LOCK_UNLOCKED) { 6754e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6764e7f640dSJohn Baldwin continue; 6774e7f640dSJohn Baldwin } 6784e7f640dSJohn Baldwin 6794e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 6804e7f640dSJohn Baldwin /* 6814e7f640dSJohn Baldwin * The current lock owner might have started executing 6824e7f640dSJohn Baldwin * on another CPU (or the lock could have changed 6834e7f640dSJohn Baldwin * owners) while we were waiting on the sleep queue 6844e7f640dSJohn Baldwin * chain lock. If so, drop the sleep queue lock and try 6854e7f640dSJohn Baldwin * again. 6864e7f640dSJohn Baldwin */ 6874e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 6881ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 6894e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 6904e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 6914e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6924e7f640dSJohn Baldwin continue; 6934e7f640dSJohn Baldwin } 6944e7f640dSJohn Baldwin } 6954e7f640dSJohn Baldwin #endif 6964e7f640dSJohn Baldwin 6974e7f640dSJohn Baldwin /* 6984e7f640dSJohn Baldwin * If an exclusive lock was released with both shared 6994e7f640dSJohn Baldwin * and exclusive waiters and a shared waiter hasn't 7004e7f640dSJohn Baldwin * woken up and acquired the lock yet, sx_lock will be 7014e7f640dSJohn Baldwin * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 7024e7f640dSJohn Baldwin * If we see that value, try to acquire it once. Note 7034e7f640dSJohn Baldwin * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 7044e7f640dSJohn Baldwin * as there are other exclusive waiters still. If we 7054e7f640dSJohn Baldwin * fail, restart the loop. 7064e7f640dSJohn Baldwin */ 7074e7f640dSJohn Baldwin if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 7084e7f640dSJohn Baldwin if (atomic_cmpset_acq_ptr(&sx->sx_lock, 7094e7f640dSJohn Baldwin SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS, 7104e7f640dSJohn Baldwin tid | SX_LOCK_EXCLUSIVE_WAITERS)) { 7114e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 7124e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p claimed by new writer", 7134e7f640dSJohn Baldwin __func__, sx); 7144e7f640dSJohn Baldwin break; 7154e7f640dSJohn Baldwin } 7164e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 717c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7184e7f640dSJohn Baldwin continue; 7194e7f640dSJohn Baldwin } 7204e7f640dSJohn Baldwin 7214e7f640dSJohn Baldwin /* 7224e7f640dSJohn Baldwin * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 7234e7f640dSJohn Baldwin * than loop back and retry. 7244e7f640dSJohn Baldwin */ 7254e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 7264e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 7274e7f640dSJohn Baldwin x | SX_LOCK_EXCLUSIVE_WAITERS)) { 7284e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 729c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7304e7f640dSJohn Baldwin continue; 7314e7f640dSJohn Baldwin } 7324e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7334e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 7344e7f640dSJohn Baldwin __func__, sx); 7354e7f640dSJohn Baldwin } 7364e7f640dSJohn Baldwin 7374e7f640dSJohn Baldwin /* 7384e7f640dSJohn Baldwin * Since we have been unable to acquire the exclusive 7394e7f640dSJohn Baldwin * lock and the exclusive waiters flag is set, we have 7404e7f640dSJohn Baldwin * to sleep. 7414e7f640dSJohn Baldwin */ 7424e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7434e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 7444e7f640dSJohn Baldwin __func__, sx); 7454e7f640dSJohn Baldwin 746a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 747e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 748a5aedd68SStacey Son #endif 749e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 7504e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 751f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 752f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 753f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 754c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 755f9819486SAttilio Rao else 756c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 757a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 758e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 759a5aedd68SStacey Son sleep_cnt++; 760a5aedd68SStacey Son #endif 761f9819486SAttilio Rao if (error) { 762f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 763f9819486SAttilio Rao CTR2(KTR_LOCK, 764f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 765f9819486SAttilio Rao __func__, sx); 766f9819486SAttilio Rao break; 767f9819486SAttilio Rao } 7684e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7694e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 7704e7f640dSJohn Baldwin __func__, sx); 771c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7724e7f640dSJohn Baldwin } 773e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 774e41d6166SMateusz Guzik if (__predict_true(!extra_work)) 775e41d6166SMateusz Guzik return (error); 776e41d6166SMateusz Guzik #endif 777076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 778e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 779076dd8ebSAndriy Gapon if (sleep_time) 78032cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 781076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 782076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 7831ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 78432cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 785076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 786076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 787076dd8ebSAndriy Gapon #endif 788f9819486SAttilio Rao if (!error) 789de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 790de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_WRITER); 791076dd8ebSAndriy Gapon GIANT_RESTORE(); 792f9819486SAttilio Rao return (error); 7934e7f640dSJohn Baldwin } 7944e7f640dSJohn Baldwin 7954e7f640dSJohn Baldwin /* 7964e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xunlock 7974e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 7984e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 7994e7f640dSJohn Baldwin * accessible from at least sx.h. 8004e7f640dSJohn Baldwin */ 8014e7f640dSJohn Baldwin void 802*013c0b49SMateusz Guzik _sx_xunlock_hard(struct sx *sx, uintptr_t tid LOCK_FILE_LINE_ARG_DEF) 8034e7f640dSJohn Baldwin { 804bc24577cSMateusz Guzik uintptr_t x, setx; 805da7bbd2cSJohn Baldwin int queue, wakeup_swapper; 8064e7f640dSJohn Baldwin 80735370593SAndriy Gapon if (SCHEDULER_STOPPED()) 80835370593SAndriy Gapon return; 80935370593SAndriy Gapon 8104e7f640dSJohn Baldwin MPASS(!(sx->sx_lock & SX_LOCK_SHARED)); 8114e7f640dSJohn Baldwin 8123b3cf014SMateusz Guzik x = SX_READ_VALUE(sx); 8133b3cf014SMateusz Guzik if (x & SX_LOCK_RECURSED) { 8146ebb77b6SMateusz Guzik /* The lock is recursed, unrecurse one level. */ 8154e7f640dSJohn Baldwin if ((--sx->sx_recurse) == 0) 8164e7f640dSJohn Baldwin atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 8174e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8184e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 8194e7f640dSJohn Baldwin return; 8204e7f640dSJohn Baldwin } 8213b3cf014SMateusz Guzik 8223b3cf014SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_WRITER); 8233b3cf014SMateusz Guzik if (x == tid && 8243b3cf014SMateusz Guzik atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED)) 8253b3cf014SMateusz Guzik return; 8263b3cf014SMateusz Guzik 8274e7f640dSJohn Baldwin MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS | 8284e7f640dSJohn Baldwin SX_LOCK_EXCLUSIVE_WAITERS)); 8294e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8304e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 8314e7f640dSJohn Baldwin 8324e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 833bc24577cSMateusz Guzik x = SX_READ_VALUE(sx); 8344e7f640dSJohn Baldwin 8354e7f640dSJohn Baldwin /* 8364e7f640dSJohn Baldwin * The wake up algorithm here is quite simple and probably not 8374e7f640dSJohn Baldwin * ideal. It gives precedence to shared waiters if they are 8384e7f640dSJohn Baldwin * present. For this condition, we have to preserve the 8394e7f640dSJohn Baldwin * state of the exclusive waiters flag. 8402028867dSAttilio Rao * If interruptible sleeps left the shared queue empty avoid a 8412028867dSAttilio Rao * starvation for the threads sleeping on the exclusive queue by giving 8422028867dSAttilio Rao * them precedence and cleaning up the shared waiters bit anyway. 8434e7f640dSJohn Baldwin */ 844bc24577cSMateusz Guzik setx = SX_LOCK_UNLOCKED; 845bc24577cSMateusz Guzik queue = SQ_EXCLUSIVE_QUEUE; 846bc24577cSMateusz Guzik if ((x & SX_LOCK_SHARED_WAITERS) != 0 && 8472028867dSAttilio Rao sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) { 8484e7f640dSJohn Baldwin queue = SQ_SHARED_QUEUE; 849bc24577cSMateusz Guzik setx |= (x & SX_LOCK_EXCLUSIVE_WAITERS); 850bc24577cSMateusz Guzik } 851bc24577cSMateusz Guzik atomic_store_rel_ptr(&sx->sx_lock, setx); 8524e7f640dSJohn Baldwin 8534e7f640dSJohn Baldwin /* Wake up all the waiters for the specific queue. */ 8544e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8554e7f640dSJohn Baldwin CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 8564e7f640dSJohn Baldwin __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 8574e7f640dSJohn Baldwin "exclusive"); 858bc24577cSMateusz Guzik 859da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 860da7bbd2cSJohn Baldwin queue); 861c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 862da7bbd2cSJohn Baldwin if (wakeup_swapper) 863da7bbd2cSJohn Baldwin kick_proc0(); 8644e7f640dSJohn Baldwin } 8654e7f640dSJohn Baldwin 866834f70f3SMateusz Guzik static bool __always_inline 867*013c0b49SMateusz Guzik __sx_slock_try(struct sx *sx, uintptr_t *xp LOCK_FILE_LINE_ARG_DEF) 868834f70f3SMateusz Guzik { 869834f70f3SMateusz Guzik 870834f70f3SMateusz Guzik /* 871834f70f3SMateusz Guzik * If no other thread has an exclusive lock then try to bump up 872834f70f3SMateusz Guzik * the count of sharers. Since we have to preserve the state 873834f70f3SMateusz Guzik * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 874834f70f3SMateusz Guzik * shared lock loop back and retry. 875834f70f3SMateusz Guzik */ 876834f70f3SMateusz Guzik while (*xp & SX_LOCK_SHARED) { 877834f70f3SMateusz Guzik MPASS(!(*xp & SX_LOCK_SHARED_WAITERS)); 878834f70f3SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, xp, 879834f70f3SMateusz Guzik *xp + SX_ONE_SHARER)) { 880834f70f3SMateusz Guzik if (LOCK_LOG_TEST(&sx->lock_object, 0)) 881834f70f3SMateusz Guzik CTR4(KTR_LOCK, "%s: %p succeed %p -> %p", 882834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 883834f70f3SMateusz Guzik (void *)(*xp + SX_ONE_SHARER)); 884834f70f3SMateusz Guzik return (true); 885834f70f3SMateusz Guzik } 886834f70f3SMateusz Guzik } 887834f70f3SMateusz Guzik return (false); 888834f70f3SMateusz Guzik } 889834f70f3SMateusz Guzik 890834f70f3SMateusz Guzik static int __noinline 891*013c0b49SMateusz Guzik _sx_slock_hard(struct sx *sx, int opts, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 8924e7f640dSJohn Baldwin { 8934e7f640dSJohn Baldwin GIANT_DECLARE; 8944e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 8954e7f640dSJohn Baldwin volatile struct thread *owner; 8964e7f640dSJohn Baldwin #endif 8971723a064SJeff Roberson #ifdef LOCK_PROFILING 898c1a6d9faSAttilio Rao uint64_t waittime = 0; 899c1a6d9faSAttilio Rao int contested = 0; 9001723a064SJeff Roberson #endif 901c1a6d9faSAttilio Rao int error = 0; 90204126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 9031ada9041SMateusz Guzik struct lock_delay_arg lda; 9041ada9041SMateusz Guzik #endif 905a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 90661852185SMateusz Guzik u_int sleep_cnt = 0; 907a5aedd68SStacey Son int64_t sleep_time = 0; 908076dd8ebSAndriy Gapon int64_t all_time = 0; 909a5aedd68SStacey Son #endif 910e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 911e41d6166SMateusz Guzik uintptr_t state; 912e41d6166SMateusz Guzik #endif 913284194f1SMateusz Guzik int extra_work = 0; 914c1a6d9faSAttilio Rao 91535370593SAndriy Gapon if (SCHEDULER_STOPPED()) 91635370593SAndriy Gapon return (0); 91735370593SAndriy Gapon 918fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 9191ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 920fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 921fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 9221ada9041SMateusz Guzik #endif 923e41d6166SMateusz Guzik 924ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS 925ae7d25a4SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 926ae7d25a4SMateusz Guzik #endif 927ae7d25a4SMateusz Guzik lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 928ae7d25a4SMateusz Guzik &waittime); 929ae7d25a4SMateusz Guzik 930e41d6166SMateusz Guzik #ifdef LOCK_PROFILING 931e41d6166SMateusz Guzik extra_work = 1; 932e41d6166SMateusz Guzik state = x; 933e41d6166SMateusz Guzik #elif defined(KDTRACE_HOOKS) 934e41d6166SMateusz Guzik extra_work = lockstat_enabled; 935e41d6166SMateusz Guzik if (__predict_false(extra_work)) { 936e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 937c5f61e6fSMateusz Guzik state = x; 938e41d6166SMateusz Guzik } 939c5f61e6fSMateusz Guzik #endif 940076dd8ebSAndriy Gapon 9414e7f640dSJohn Baldwin /* 9424e7f640dSJohn Baldwin * As with rwlocks, we don't make any attempt to try to block 9434e7f640dSJohn Baldwin * shared locks once there is an exclusive waiter. 9444e7f640dSJohn Baldwin */ 9454e7f640dSJohn Baldwin for (;;) { 946*013c0b49SMateusz Guzik if (__sx_slock_try(sx, &x LOCK_FILE_LINE_ARG)) 9474e7f640dSJohn Baldwin break; 948c5f61e6fSMateusz Guzik #ifdef KDTRACE_HOOKS 949c5f61e6fSMateusz Guzik lda.spin_cnt++; 950c5f61e6fSMateusz Guzik #endif 951c5f61e6fSMateusz Guzik 9524e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 9534e7f640dSJohn Baldwin /* 9544e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 9554e7f640dSJohn Baldwin * the owner stops running or the state of the lock 9564e7f640dSJohn Baldwin * changes. 9574e7f640dSJohn Baldwin */ 9581ae1c2a3SAttilio Rao if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 959c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 9604e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 9614e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9624e7f640dSJohn Baldwin CTR3(KTR_LOCK, 9634e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 9644e7f640dSJohn Baldwin __func__, sx, owner); 9652cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 9662cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 9672cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 968e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 969c5f61e6fSMateusz Guzik do { 9701ada9041SMateusz Guzik lock_delay(&lda); 971c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 972c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 973c5f61e6fSMateusz Guzik } while (owner != NULL && TD_IS_RUNNING(owner)); 9742cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 9752cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 9764e7f640dSJohn Baldwin continue; 9774e7f640dSJohn Baldwin } 9784e7f640dSJohn Baldwin } 9794e7f640dSJohn Baldwin #endif 9804e7f640dSJohn Baldwin 9814e7f640dSJohn Baldwin /* 9824e7f640dSJohn Baldwin * Some other thread already has an exclusive lock, so 9834e7f640dSJohn Baldwin * start the process of blocking. 9844e7f640dSJohn Baldwin */ 9854e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 986c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 9874e7f640dSJohn Baldwin 9884e7f640dSJohn Baldwin /* 9894e7f640dSJohn Baldwin * The lock could have been released while we spun. 9904e7f640dSJohn Baldwin * In this case loop back and retry. 9914e7f640dSJohn Baldwin */ 9924e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 9934e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 9944e7f640dSJohn Baldwin continue; 9954e7f640dSJohn Baldwin } 9964e7f640dSJohn Baldwin 9974e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 9984e7f640dSJohn Baldwin /* 9994e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 10004e7f640dSJohn Baldwin * the owner stops running or the state of the lock 10014e7f640dSJohn Baldwin * changes. 10024e7f640dSJohn Baldwin */ 10034e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 10041ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 10054e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 10064e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 10074e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 1008c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10094e7f640dSJohn Baldwin continue; 10104e7f640dSJohn Baldwin } 10114e7f640dSJohn Baldwin } 10124e7f640dSJohn Baldwin #endif 10134e7f640dSJohn Baldwin 10144e7f640dSJohn Baldwin /* 10154e7f640dSJohn Baldwin * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 10164e7f640dSJohn Baldwin * fail to set it drop the sleep queue lock and loop 10174e7f640dSJohn Baldwin * back. 10184e7f640dSJohn Baldwin */ 10194e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS)) { 10204e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 10214e7f640dSJohn Baldwin x | SX_LOCK_SHARED_WAITERS)) { 10224e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 1023c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10244e7f640dSJohn Baldwin continue; 10254e7f640dSJohn Baldwin } 10264e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10274e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 10284e7f640dSJohn Baldwin __func__, sx); 10294e7f640dSJohn Baldwin } 10304e7f640dSJohn Baldwin 10314e7f640dSJohn Baldwin /* 10324e7f640dSJohn Baldwin * Since we have been unable to acquire the shared lock, 10334e7f640dSJohn Baldwin * we have to sleep. 10344e7f640dSJohn Baldwin */ 10354e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10364e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 10374e7f640dSJohn Baldwin __func__, sx); 10384e7f640dSJohn Baldwin 1039a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 1040e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 1041a5aedd68SStacey Son #endif 1042e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 10434e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 1044f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 1045f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 1046f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 1047c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 1048f9819486SAttilio Rao else 1049c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 1050a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 1051e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 1052a5aedd68SStacey Son sleep_cnt++; 1053a5aedd68SStacey Son #endif 1054f9819486SAttilio Rao if (error) { 1055f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1056f9819486SAttilio Rao CTR2(KTR_LOCK, 1057f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 1058f9819486SAttilio Rao __func__, sx); 1059f9819486SAttilio Rao break; 1060f9819486SAttilio Rao } 10614e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10624e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 10634e7f640dSJohn Baldwin __func__, sx); 1064c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10654e7f640dSJohn Baldwin } 1066e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 1067e41d6166SMateusz Guzik if (__predict_true(!extra_work)) 1068e41d6166SMateusz Guzik return (error); 1069e41d6166SMateusz Guzik #endif 1070076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 1071e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 1072076dd8ebSAndriy Gapon if (sleep_time) 107332cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 1074076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1075076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 10761ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 107732cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 1078076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1079076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 1080076dd8ebSAndriy Gapon #endif 10813ae56ce9SMateusz Guzik if (error == 0) { 1082de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 1083de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_READER); 10843ae56ce9SMateusz Guzik } 10854e7f640dSJohn Baldwin GIANT_RESTORE(); 1086f9819486SAttilio Rao return (error); 10874e7f640dSJohn Baldwin } 10884e7f640dSJohn Baldwin 1089834f70f3SMateusz Guzik int 1090*013c0b49SMateusz Guzik _sx_slock_int(struct sx *sx, int opts LOCK_FILE_LINE_ARG_DEF) 10914e7f640dSJohn Baldwin { 10924e7f640dSJohn Baldwin uintptr_t x; 1093834f70f3SMateusz Guzik int error; 10944e7f640dSJohn Baldwin 1095704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 1096704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 1097834f70f3SMateusz Guzik ("sx_slock() by idle thread %p on sx %s @ %s:%d", 1098834f70f3SMateusz Guzik curthread, sx->lock_object.lo_name, file, line)); 10993ae56ce9SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1100834f70f3SMateusz Guzik ("sx_slock() of destroyed sx @ %s:%d", file, line)); 1101834f70f3SMateusz Guzik WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 1102834f70f3SMateusz Guzik 1103834f70f3SMateusz Guzik error = 0; 1104c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 1105834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__acquire) || 1106*013c0b49SMateusz Guzik !__sx_slock_try(sx, &x LOCK_FILE_LINE_ARG))) 1107*013c0b49SMateusz Guzik error = _sx_slock_hard(sx, opts, x LOCK_FILE_LINE_ARG); 1108834f70f3SMateusz Guzik if (error == 0) { 1109834f70f3SMateusz Guzik LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 1110834f70f3SMateusz Guzik WITNESS_LOCK(&sx->lock_object, 0, file, line); 1111834f70f3SMateusz Guzik TD_LOCKS_INC(curthread); 1112834f70f3SMateusz Guzik } 1113834f70f3SMateusz Guzik return (error); 1114834f70f3SMateusz Guzik } 1115834f70f3SMateusz Guzik 1116*013c0b49SMateusz Guzik int 1117*013c0b49SMateusz Guzik _sx_slock(struct sx *sx, int opts, const char *file, int line) 1118*013c0b49SMateusz Guzik { 1119*013c0b49SMateusz Guzik 1120*013c0b49SMateusz Guzik return (_sx_slock_int(sx, opts LOCK_FILE_LINE_ARG)); 1121*013c0b49SMateusz Guzik } 1122*013c0b49SMateusz Guzik 1123834f70f3SMateusz Guzik static bool __always_inline 1124834f70f3SMateusz Guzik _sx_sunlock_try(struct sx *sx, uintptr_t *xp) 1125834f70f3SMateusz Guzik { 1126834f70f3SMateusz Guzik 11274e7f640dSJohn Baldwin for (;;) { 11284e7f640dSJohn Baldwin /* 11294e7f640dSJohn Baldwin * We should never have sharers while at least one thread 11304e7f640dSJohn Baldwin * holds a shared lock. 11314e7f640dSJohn Baldwin */ 1132834f70f3SMateusz Guzik KASSERT(!(*xp & SX_LOCK_SHARED_WAITERS), 11334e7f640dSJohn Baldwin ("%s: waiting sharers", __func__)); 11344e7f640dSJohn Baldwin 11354e7f640dSJohn Baldwin /* 11364e7f640dSJohn Baldwin * See if there is more than one shared lock held. If 11374e7f640dSJohn Baldwin * so, just drop one and return. 11384e7f640dSJohn Baldwin */ 1139834f70f3SMateusz Guzik if (SX_SHARERS(*xp) > 1) { 1140834f70f3SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, xp, 1141834f70f3SMateusz Guzik *xp - SX_ONE_SHARER)) { 11424e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 11434e7f640dSJohn Baldwin CTR4(KTR_LOCK, 11444e7f640dSJohn Baldwin "%s: %p succeeded %p -> %p", 1145834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 1146834f70f3SMateusz Guzik (void *)(*xp - SX_ONE_SHARER)); 1147834f70f3SMateusz Guzik return (true); 11484e7f640dSJohn Baldwin } 11494e7f640dSJohn Baldwin continue; 11504e7f640dSJohn Baldwin } 11514e7f640dSJohn Baldwin 11524e7f640dSJohn Baldwin /* 11534e7f640dSJohn Baldwin * If there aren't any waiters for an exclusive lock, 11544e7f640dSJohn Baldwin * then try to drop it quickly. 11554e7f640dSJohn Baldwin */ 1156834f70f3SMateusz Guzik if (!(*xp & SX_LOCK_EXCLUSIVE_WAITERS)) { 1157834f70f3SMateusz Guzik MPASS(*xp == SX_SHARERS_LOCK(1)); 1158834f70f3SMateusz Guzik *xp = SX_SHARERS_LOCK(1); 1159fa474043SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, 1160834f70f3SMateusz Guzik xp, SX_LOCK_UNLOCKED)) { 11614e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 11624e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p last succeeded", 11634e7f640dSJohn Baldwin __func__, sx); 1164834f70f3SMateusz Guzik return (true); 11654e7f640dSJohn Baldwin } 11664e7f640dSJohn Baldwin continue; 11674e7f640dSJohn Baldwin } 1168834f70f3SMateusz Guzik break; 1169834f70f3SMateusz Guzik } 1170834f70f3SMateusz Guzik return (false); 1171834f70f3SMateusz Guzik } 1172834f70f3SMateusz Guzik 1173834f70f3SMateusz Guzik static void __noinline 1174*013c0b49SMateusz Guzik _sx_sunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 1175834f70f3SMateusz Guzik { 1176834f70f3SMateusz Guzik int wakeup_swapper; 1177834f70f3SMateusz Guzik 1178834f70f3SMateusz Guzik if (SCHEDULER_STOPPED()) 1179834f70f3SMateusz Guzik return; 1180834f70f3SMateusz Guzik 1181834f70f3SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER); 1182834f70f3SMateusz Guzik 1183834f70f3SMateusz Guzik for (;;) { 1184834f70f3SMateusz Guzik if (_sx_sunlock_try(sx, &x)) 1185834f70f3SMateusz Guzik break; 11864e7f640dSJohn Baldwin 11874e7f640dSJohn Baldwin /* 11884e7f640dSJohn Baldwin * At this point, there should just be one sharer with 11894e7f640dSJohn Baldwin * exclusive waiters. 11904e7f640dSJohn Baldwin */ 11914e7f640dSJohn Baldwin MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS)); 11924e7f640dSJohn Baldwin 11934e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 11944e7f640dSJohn Baldwin 11954e7f640dSJohn Baldwin /* 11964e7f640dSJohn Baldwin * Wake up semantic here is quite simple: 11974e7f640dSJohn Baldwin * Just wake up all the exclusive waiters. 11984e7f640dSJohn Baldwin * Note that the state of the lock could have changed, 11994e7f640dSJohn Baldwin * so if it fails loop back and retry. 12004e7f640dSJohn Baldwin */ 1201ddce63caSAttilio Rao if (!atomic_cmpset_rel_ptr(&sx->sx_lock, 12024e7f640dSJohn Baldwin SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS, 12034e7f640dSJohn Baldwin SX_LOCK_UNLOCKED)) { 12044e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 1205c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 12064e7f640dSJohn Baldwin continue; 12074e7f640dSJohn Baldwin } 12084e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 12094e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p waking up all thread on" 12104e7f640dSJohn Baldwin "exclusive queue", __func__, sx); 1211da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 1212da7bbd2cSJohn Baldwin 0, SQ_EXCLUSIVE_QUEUE); 1213c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 1214da7bbd2cSJohn Baldwin if (wakeup_swapper) 1215da7bbd2cSJohn Baldwin kick_proc0(); 12164e7f640dSJohn Baldwin break; 12174e7f640dSJohn Baldwin } 1218834f70f3SMateusz Guzik } 1219834f70f3SMateusz Guzik 1220834f70f3SMateusz Guzik void 1221*013c0b49SMateusz Guzik _sx_sunlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 1222834f70f3SMateusz Guzik { 1223834f70f3SMateusz Guzik uintptr_t x; 1224834f70f3SMateusz Guzik 1225834f70f3SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1226834f70f3SMateusz Guzik ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 1227834f70f3SMateusz Guzik _sx_assert(sx, SA_SLOCKED, file, line); 1228834f70f3SMateusz Guzik WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 1229834f70f3SMateusz Guzik LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 1230834f70f3SMateusz Guzik 1231834f70f3SMateusz Guzik x = SX_READ_VALUE(sx); 1232834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__release) || 1233834f70f3SMateusz Guzik !_sx_sunlock_try(sx, &x))) 1234*013c0b49SMateusz Guzik _sx_sunlock_hard(sx, x LOCK_FILE_LINE_ARG); 1235834f70f3SMateusz Guzik 12363ae56ce9SMateusz Guzik TD_LOCKS_DEC(curthread); 1237d55229b7SJason Evans } 12384e5e677bSJohn Baldwin 1239*013c0b49SMateusz Guzik void 1240*013c0b49SMateusz Guzik _sx_sunlock(struct sx *sx, const char *file, int line) 1241*013c0b49SMateusz Guzik { 1242*013c0b49SMateusz Guzik 1243*013c0b49SMateusz Guzik _sx_sunlock_int(sx LOCK_FILE_LINE_ARG); 1244*013c0b49SMateusz Guzik } 1245*013c0b49SMateusz Guzik 12464e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT 1247781a35dfSJohn Baldwin #ifndef INVARIANTS 1248781a35dfSJohn Baldwin #undef _sx_assert 1249781a35dfSJohn Baldwin #endif 1250781a35dfSJohn Baldwin 12514e5e677bSJohn Baldwin /* 12524e5e677bSJohn Baldwin * In the non-WITNESS case, sx_assert() can only detect that at least 12534e5e677bSJohn Baldwin * *some* thread owns an slock, but it cannot guarantee that *this* 12544e5e677bSJohn Baldwin * thread owns an slock. 12554e5e677bSJohn Baldwin */ 12564e5e677bSJohn Baldwin void 1257d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line) 12584e5e677bSJohn Baldwin { 12594e7f640dSJohn Baldwin #ifndef WITNESS 12604e7f640dSJohn Baldwin int slocked = 0; 12614e7f640dSJohn Baldwin #endif 12624e5e677bSJohn Baldwin 126303129ba9SJohn Baldwin if (panicstr != NULL) 126403129ba9SJohn Baldwin return; 12654e5e677bSJohn Baldwin switch (what) { 12667ec137e5SJohn Baldwin case SA_SLOCKED: 12677ec137e5SJohn Baldwin case SA_SLOCKED | SA_NOTRECURSED: 12687ec137e5SJohn Baldwin case SA_SLOCKED | SA_RECURSED: 12694e7f640dSJohn Baldwin #ifndef WITNESS 12704e7f640dSJohn Baldwin slocked = 1; 12714e7f640dSJohn Baldwin /* FALLTHROUGH */ 12724e7f640dSJohn Baldwin #endif 12737ec137e5SJohn Baldwin case SA_LOCKED: 12747ec137e5SJohn Baldwin case SA_LOCKED | SA_NOTRECURSED: 12757ec137e5SJohn Baldwin case SA_LOCKED | SA_RECURSED: 12764e5e677bSJohn Baldwin #ifdef WITNESS 1277aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 12784e5e677bSJohn Baldwin #else 12794e7f640dSJohn Baldwin /* 12804e7f640dSJohn Baldwin * If some other thread has an exclusive lock or we 12814e7f640dSJohn Baldwin * have one and are asserting a shared lock, fail. 12824e7f640dSJohn Baldwin * Also, if no one has a lock at all, fail. 12834e7f640dSJohn Baldwin */ 12844e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED || 12854e7f640dSJohn Baldwin (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 12864e7f640dSJohn Baldwin sx_xholder(sx) != curthread))) 128703129ba9SJohn Baldwin panic("Lock %s not %slocked @ %s:%d\n", 12884e7f640dSJohn Baldwin sx->lock_object.lo_name, slocked ? "share " : "", 12894e7f640dSJohn Baldwin file, line); 12904e7f640dSJohn Baldwin 12914e7f640dSJohn Baldwin if (!(sx->sx_lock & SX_LOCK_SHARED)) { 12924e7f640dSJohn Baldwin if (sx_recursed(sx)) { 12937ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 12944e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 12954e7f640dSJohn Baldwin sx->lock_object.lo_name, file, 12964e7f640dSJohn Baldwin line); 12977ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 12984e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 12994e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13004e7f640dSJohn Baldwin } 13014e5e677bSJohn Baldwin #endif 13024e5e677bSJohn Baldwin break; 13037ec137e5SJohn Baldwin case SA_XLOCKED: 13047ec137e5SJohn Baldwin case SA_XLOCKED | SA_NOTRECURSED: 13057ec137e5SJohn Baldwin case SA_XLOCKED | SA_RECURSED: 13064e7f640dSJohn Baldwin if (sx_xholder(sx) != curthread) 130703129ba9SJohn Baldwin panic("Lock %s not exclusively locked @ %s:%d\n", 1308aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 13094e7f640dSJohn Baldwin if (sx_recursed(sx)) { 13107ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 13114e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 13124e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13137ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 13144e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 13154e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13164e5e677bSJohn Baldwin break; 13177ec137e5SJohn Baldwin case SA_UNLOCKED: 131819b0efd3SPawel Jakub Dawidek #ifdef WITNESS 1319aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 132019b0efd3SPawel Jakub Dawidek #else 1321f6739b1dSPawel Jakub Dawidek /* 13224e7f640dSJohn Baldwin * If we hold an exclusve lock fail. We can't 13234e7f640dSJohn Baldwin * reliably check to see if we hold a shared lock or 13244e7f640dSJohn Baldwin * not. 1325f6739b1dSPawel Jakub Dawidek */ 13264e7f640dSJohn Baldwin if (sx_xholder(sx) == curthread) 132703129ba9SJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 1328aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 132919b0efd3SPawel Jakub Dawidek #endif 133019b0efd3SPawel Jakub Dawidek break; 13314e5e677bSJohn Baldwin default: 13324e5e677bSJohn Baldwin panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 13334e5e677bSJohn Baldwin line); 13344e5e677bSJohn Baldwin } 13354e5e677bSJohn Baldwin } 13364e5e677bSJohn Baldwin #endif /* INVARIANT_SUPPORT */ 1337d272fe53SJohn Baldwin 1338d272fe53SJohn Baldwin #ifdef DDB 13394e7f640dSJohn Baldwin static void 1340d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock) 1341d272fe53SJohn Baldwin { 1342d272fe53SJohn Baldwin struct thread *td; 1343d576deedSPawel Jakub Dawidek const struct sx *sx; 1344d272fe53SJohn Baldwin 1345d576deedSPawel Jakub Dawidek sx = (const struct sx *)lock; 1346d272fe53SJohn Baldwin 1347d272fe53SJohn Baldwin db_printf(" state: "); 13484e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED) 13494e7f640dSJohn Baldwin db_printf("UNLOCKED\n"); 13500026c92cSJohn Baldwin else if (sx->sx_lock == SX_LOCK_DESTROYED) { 13510026c92cSJohn Baldwin db_printf("DESTROYED\n"); 13520026c92cSJohn Baldwin return; 13530026c92cSJohn Baldwin } else if (sx->sx_lock & SX_LOCK_SHARED) 13544e7f640dSJohn Baldwin db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 13554e7f640dSJohn Baldwin else { 13564e7f640dSJohn Baldwin td = sx_xholder(sx); 1357d272fe53SJohn Baldwin db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1358431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 13594e7f640dSJohn Baldwin if (sx_recursed(sx)) 13604e7f640dSJohn Baldwin db_printf(" recursed: %d\n", sx->sx_recurse); 13614e7f640dSJohn Baldwin } 13624e7f640dSJohn Baldwin 13634e7f640dSJohn Baldwin db_printf(" waiters: "); 13644e7f640dSJohn Baldwin switch(sx->sx_lock & 13654e7f640dSJohn Baldwin (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 13664e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS: 13674e7f640dSJohn Baldwin db_printf("shared\n"); 13684e7f640dSJohn Baldwin break; 13694e7f640dSJohn Baldwin case SX_LOCK_EXCLUSIVE_WAITERS: 13704e7f640dSJohn Baldwin db_printf("exclusive\n"); 13714e7f640dSJohn Baldwin break; 13724e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 13734e7f640dSJohn Baldwin db_printf("exclusive and shared\n"); 13744e7f640dSJohn Baldwin break; 13754e7f640dSJohn Baldwin default: 13764e7f640dSJohn Baldwin db_printf("none\n"); 13774e7f640dSJohn Baldwin } 1378d272fe53SJohn Baldwin } 1379462a7addSJohn Baldwin 1380462a7addSJohn Baldwin /* 1381462a7addSJohn Baldwin * Check to see if a thread that is blocked on a sleep queue is actually 1382462a7addSJohn Baldwin * blocked on an sx lock. If so, output some details and return true. 1383462a7addSJohn Baldwin * If the lock has an exclusive owner, return that in *ownerp. 1384462a7addSJohn Baldwin */ 1385462a7addSJohn Baldwin int 1386462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp) 1387462a7addSJohn Baldwin { 1388462a7addSJohn Baldwin struct sx *sx; 1389462a7addSJohn Baldwin 1390462a7addSJohn Baldwin /* 13914e7f640dSJohn Baldwin * Check to see if this thread is blocked on an sx lock. 13924e7f640dSJohn Baldwin * First, we check the lock class. If that is ok, then we 13934e7f640dSJohn Baldwin * compare the lock name against the wait message. 1394462a7addSJohn Baldwin */ 13954e7f640dSJohn Baldwin sx = td->td_wchan; 13964e7f640dSJohn Baldwin if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 13974e7f640dSJohn Baldwin sx->lock_object.lo_name != td->td_wmesg) 1398462a7addSJohn Baldwin return (0); 1399462a7addSJohn Baldwin 1400462a7addSJohn Baldwin /* We think we have an sx lock, so output some details. */ 1401462a7addSJohn Baldwin db_printf("blocked on sx \"%s\" ", td->td_wmesg); 14024e7f640dSJohn Baldwin *ownerp = sx_xholder(sx); 14034e7f640dSJohn Baldwin if (sx->sx_lock & SX_LOCK_SHARED) 14044e7f640dSJohn Baldwin db_printf("SLOCK (count %ju)\n", 14054e7f640dSJohn Baldwin (uintmax_t)SX_SHARERS(sx->sx_lock)); 14064e7f640dSJohn Baldwin else 1407462a7addSJohn Baldwin db_printf("XLOCK\n"); 1408462a7addSJohn Baldwin return (1); 1409462a7addSJohn Baldwin } 1410d272fe53SJohn Baldwin #endif 1411