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 262013c0b49SMateusz 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 294013c0b49SMateusz Guzik sx_try_slock_(struct sx *sx, const char *file, int line) 295013c0b49SMateusz Guzik { 296013c0b49SMateusz Guzik 297013c0b49SMateusz Guzik return (sx_try_slock_int(sx LOCK_FILE_LINE_ARG)); 298013c0b49SMateusz Guzik } 299013c0b49SMateusz Guzik 300013c0b49SMateusz 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)) 317013c0b49SMateusz 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 332013c0b49SMateusz 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 380013c0b49SMateusz Guzik int 381013c0b49SMateusz Guzik sx_try_xlock_(struct sx *sx, const char *file, int line) 382013c0b49SMateusz Guzik { 383013c0b49SMateusz Guzik 384013c0b49SMateusz Guzik return (sx_try_xlock_int(sx LOCK_FILE_LINE_ARG)); 385013c0b49SMateusz Guzik } 386013c0b49SMateusz 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 411013c0b49SMateusz 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 440013c0b49SMateusz Guzik int 441013c0b49SMateusz Guzik sx_try_upgrade_(struct sx *sx, const char *file, int line) 442013c0b49SMateusz Guzik { 443013c0b49SMateusz Guzik 444013c0b49SMateusz Guzik return (sx_try_upgrade_int(sx LOCK_FILE_LINE_ARG)); 445013c0b49SMateusz Guzik } 446013c0b49SMateusz Guzik 4474e7f640dSJohn Baldwin /* 4484e7f640dSJohn Baldwin * Downgrade an unrecursed exclusive lock into a single shared lock. 4494e7f640dSJohn Baldwin */ 450d55229b7SJason Evans void 451013c0b49SMateusz 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) | 48326d94f99SMark Johnston (x & SX_LOCK_EXCLUSIVE_WAITERS))) 48426d94f99SMark Johnston goto out; 4854e7f640dSJohn Baldwin 4864e7f640dSJohn Baldwin /* 4874e7f640dSJohn Baldwin * Lock the sleep queue so we can read the waiters bits 4884e7f640dSJohn Baldwin * without any races and wakeup any shared waiters. 4894e7f640dSJohn Baldwin */ 4904e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 4914e7f640dSJohn Baldwin 4924e7f640dSJohn Baldwin /* 4934e7f640dSJohn Baldwin * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 4944e7f640dSJohn Baldwin * shared lock. If there are any shared waiters, wake them up. 4954e7f640dSJohn Baldwin */ 496da7bbd2cSJohn Baldwin wakeup_swapper = 0; 4974e7f640dSJohn Baldwin x = sx->sx_lock; 4984e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 4994e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS)); 5004e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED_WAITERS) 501da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 502da7bbd2cSJohn Baldwin 0, SQ_SHARED_QUEUE); 5034e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 504d55229b7SJason Evans 505da7bbd2cSJohn Baldwin if (wakeup_swapper) 506da7bbd2cSJohn Baldwin kick_proc0(); 50726d94f99SMark Johnston 50826d94f99SMark Johnston out: 50926d94f99SMark Johnston LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 51026d94f99SMark Johnston LOCKSTAT_RECORD0(sx__downgrade, sx); 5114e7f640dSJohn Baldwin } 512d55229b7SJason Evans 513013c0b49SMateusz Guzik void 514013c0b49SMateusz Guzik sx_downgrade_(struct sx *sx, const char *file, int line) 515013c0b49SMateusz Guzik { 516013c0b49SMateusz Guzik 517013c0b49SMateusz Guzik sx_downgrade_int(sx LOCK_FILE_LINE_ARG); 518013c0b49SMateusz Guzik } 519013c0b49SMateusz Guzik 5204e7f640dSJohn Baldwin /* 5214e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xlock 5224e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 5234e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 5244e7f640dSJohn Baldwin * accessible from at least sx.h. 5254e7f640dSJohn Baldwin */ 526f9819486SAttilio Rao int 527013c0b49SMateusz Guzik _sx_xlock_hard(struct sx *sx, uintptr_t x, int opts LOCK_FILE_LINE_ARG_DEF) 5284e7f640dSJohn Baldwin { 5294e7f640dSJohn Baldwin GIANT_DECLARE; 530013c0b49SMateusz Guzik uintptr_t tid; 5314e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5324e7f640dSJohn Baldwin volatile struct thread *owner; 533d07e22cdSMateusz Guzik u_int i, n, spintries = 0; 5344e7f640dSJohn Baldwin #endif 5351723a064SJeff Roberson #ifdef LOCK_PROFILING 5361723a064SJeff Roberson uint64_t waittime = 0; 5371723a064SJeff Roberson int contested = 0; 5381723a064SJeff Roberson #endif 5391723a064SJeff Roberson int error = 0; 54004126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 5411ada9041SMateusz Guzik struct lock_delay_arg lda; 5421ada9041SMateusz Guzik #endif 543a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 54461852185SMateusz Guzik u_int sleep_cnt = 0; 545a5aedd68SStacey Son int64_t sleep_time = 0; 546076dd8ebSAndriy Gapon int64_t all_time = 0; 547a5aedd68SStacey Son #endif 548e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 549e41d6166SMateusz Guzik uintptr_t state; 550e41d6166SMateusz Guzik #endif 551284194f1SMateusz Guzik int extra_work = 0; 5524e7f640dSJohn Baldwin 553013c0b49SMateusz Guzik tid = (uintptr_t)curthread; 55435370593SAndriy Gapon if (SCHEDULER_STOPPED()) 55535370593SAndriy Gapon return (0); 55635370593SAndriy Gapon 557fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 5581ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 559fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 560fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 5611ada9041SMateusz Guzik #endif 5621ada9041SMateusz Guzik 563c1aaf63cSMateusz Guzik if (__predict_false(x == SX_LOCK_UNLOCKED)) 564c1aaf63cSMateusz Guzik x = SX_READ_VALUE(sx); 565c1aaf63cSMateusz Guzik 5664e7f640dSJohn Baldwin /* If we already hold an exclusive lock, then recurse. */ 567c5f61e6fSMateusz Guzik if (__predict_false(lv_sx_owner(x) == (struct thread *)tid)) { 568f0830182SAttilio Rao KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0, 569b0d67325SJohn Baldwin ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 570b0d67325SJohn Baldwin sx->lock_object.lo_name, file, line)); 5714e7f640dSJohn Baldwin sx->sx_recurse++; 5724e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 5734e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5744e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 575f9819486SAttilio Rao return (0); 5764e7f640dSJohn Baldwin } 5774e7f640dSJohn Baldwin 5784e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5794e7f640dSJohn Baldwin CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 5804e7f640dSJohn Baldwin sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 5814e7f640dSJohn Baldwin 582ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS 583ae7d25a4SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 584ae7d25a4SMateusz Guzik #endif 585ae7d25a4SMateusz Guzik lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 586ae7d25a4SMateusz Guzik &waittime); 587ae7d25a4SMateusz Guzik 588e41d6166SMateusz Guzik #ifdef LOCK_PROFILING 589e41d6166SMateusz Guzik extra_work = 1; 590e41d6166SMateusz Guzik state = x; 591e41d6166SMateusz Guzik #elif defined(KDTRACE_HOOKS) 592e41d6166SMateusz Guzik extra_work = lockstat_enabled; 593e41d6166SMateusz Guzik if (__predict_false(extra_work)) { 594e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 595c5f61e6fSMateusz Guzik state = x; 596e41d6166SMateusz Guzik } 597076dd8ebSAndriy Gapon #endif 598e41d6166SMateusz Guzik 599fc4f686dSMateusz Guzik for (;;) { 600c5f61e6fSMateusz Guzik if (x == SX_LOCK_UNLOCKED) { 601fa474043SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 602fc4f686dSMateusz Guzik break; 603c5f61e6fSMateusz Guzik continue; 604c5f61e6fSMateusz Guzik } 605a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 6061ada9041SMateusz Guzik lda.spin_cnt++; 607a5aedd68SStacey Son #endif 6084e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 6094e7f640dSJohn Baldwin /* 6104e7f640dSJohn Baldwin * If the lock is write locked and the owner is 6114e7f640dSJohn Baldwin * running on another CPU, spin until the owner stops 6124e7f640dSJohn Baldwin * running or the state of the lock changes. 6134e7f640dSJohn Baldwin */ 6148545538bSJohn Baldwin if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 6151ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0) { 616c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 6174e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 6184e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6194e7f640dSJohn Baldwin CTR3(KTR_LOCK, 6204e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 6214e7f640dSJohn Baldwin __func__, sx, owner); 6222cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 6232cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 6242cba8dd3SJohn Baldwin "lockname:\"%s\"", 6252cba8dd3SJohn Baldwin sx->lock_object.lo_name); 626e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 627c5f61e6fSMateusz Guzik do { 6281ada9041SMateusz Guzik lock_delay(&lda); 629c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 630c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 631c5f61e6fSMateusz Guzik } while (owner != NULL && 632c5f61e6fSMateusz Guzik TD_IS_RUNNING(owner)); 6332cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 6342cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 6354e7f640dSJohn Baldwin continue; 6364e7f640dSJohn Baldwin } 6371ae1c2a3SAttilio Rao } else if (SX_SHARERS(x) && spintries < asx_retries) { 6382cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 6392cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 6402cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 641e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 6421ae1c2a3SAttilio Rao spintries++; 643d07e22cdSMateusz Guzik for (i = 0; i < asx_loops; i += n) { 6441ae1c2a3SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6451ae1c2a3SAttilio Rao CTR4(KTR_LOCK, 6461ae1c2a3SAttilio Rao "%s: shared spinning on %p with %u and %u", 6471ae1c2a3SAttilio Rao __func__, sx, spintries, i); 648d07e22cdSMateusz Guzik n = SX_SHARERS(x); 649d07e22cdSMateusz Guzik lock_delay_spin(n); 65020a15d17SMateusz Guzik x = SX_READ_VALUE(sx); 6511ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0 || 6521ae1c2a3SAttilio Rao SX_SHARERS(x) == 0) 6531ae1c2a3SAttilio Rao break; 6541ae1c2a3SAttilio Rao } 65520a15d17SMateusz Guzik #ifdef KDTRACE_HOOKS 65620a15d17SMateusz Guzik lda.spin_cnt += i; 65720a15d17SMateusz Guzik #endif 6582cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 6592cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 6601ae1c2a3SAttilio Rao if (i != asx_loops) 6611ae1c2a3SAttilio Rao continue; 6621ae1c2a3SAttilio Rao } 6634e7f640dSJohn Baldwin } 6644e7f640dSJohn Baldwin #endif 6654e7f640dSJohn Baldwin 6664e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 667c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6684e7f640dSJohn Baldwin 6694e7f640dSJohn Baldwin /* 6704e7f640dSJohn Baldwin * If the lock was released while spinning on the 6714e7f640dSJohn Baldwin * sleep queue chain lock, try again. 6724e7f640dSJohn Baldwin */ 6734e7f640dSJohn Baldwin if (x == SX_LOCK_UNLOCKED) { 6744e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6754e7f640dSJohn Baldwin continue; 6764e7f640dSJohn Baldwin } 6774e7f640dSJohn Baldwin 6784e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 6794e7f640dSJohn Baldwin /* 6804e7f640dSJohn Baldwin * The current lock owner might have started executing 6814e7f640dSJohn Baldwin * on another CPU (or the lock could have changed 6824e7f640dSJohn Baldwin * owners) while we were waiting on the sleep queue 6834e7f640dSJohn Baldwin * chain lock. If so, drop the sleep queue lock and try 6844e7f640dSJohn Baldwin * again. 6854e7f640dSJohn Baldwin */ 6864e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 6871ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 6884e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 6894e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 6904e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6914e7f640dSJohn Baldwin continue; 6924e7f640dSJohn Baldwin } 6934e7f640dSJohn Baldwin } 6944e7f640dSJohn Baldwin #endif 6954e7f640dSJohn Baldwin 6964e7f640dSJohn Baldwin /* 6974e7f640dSJohn Baldwin * If an exclusive lock was released with both shared 6984e7f640dSJohn Baldwin * and exclusive waiters and a shared waiter hasn't 6994e7f640dSJohn Baldwin * woken up and acquired the lock yet, sx_lock will be 7004e7f640dSJohn Baldwin * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 7014e7f640dSJohn Baldwin * If we see that value, try to acquire it once. Note 7024e7f640dSJohn Baldwin * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 7034e7f640dSJohn Baldwin * as there are other exclusive waiters still. If we 7044e7f640dSJohn Baldwin * fail, restart the loop. 7054e7f640dSJohn Baldwin */ 7064e7f640dSJohn Baldwin if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 7074e7f640dSJohn Baldwin if (atomic_cmpset_acq_ptr(&sx->sx_lock, 7084e7f640dSJohn Baldwin SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS, 7094e7f640dSJohn Baldwin tid | SX_LOCK_EXCLUSIVE_WAITERS)) { 7104e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 7114e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p claimed by new writer", 7124e7f640dSJohn Baldwin __func__, sx); 7134e7f640dSJohn Baldwin break; 7144e7f640dSJohn Baldwin } 7154e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 716c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7174e7f640dSJohn Baldwin continue; 7184e7f640dSJohn Baldwin } 7194e7f640dSJohn Baldwin 7204e7f640dSJohn Baldwin /* 7214e7f640dSJohn Baldwin * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 7224e7f640dSJohn Baldwin * than loop back and retry. 7234e7f640dSJohn Baldwin */ 7244e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 7254e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 7264e7f640dSJohn Baldwin x | SX_LOCK_EXCLUSIVE_WAITERS)) { 7274e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 728c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7294e7f640dSJohn Baldwin continue; 7304e7f640dSJohn Baldwin } 7314e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7324e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 7334e7f640dSJohn Baldwin __func__, sx); 7344e7f640dSJohn Baldwin } 7354e7f640dSJohn Baldwin 7364e7f640dSJohn Baldwin /* 7374e7f640dSJohn Baldwin * Since we have been unable to acquire the exclusive 7384e7f640dSJohn Baldwin * lock and the exclusive waiters flag is set, we have 7394e7f640dSJohn Baldwin * to sleep. 7404e7f640dSJohn Baldwin */ 7414e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7424e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 7434e7f640dSJohn Baldwin __func__, sx); 7444e7f640dSJohn Baldwin 745a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 746e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 747a5aedd68SStacey Son #endif 748e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 7494e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 750f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 751f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 752f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 753c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 754f9819486SAttilio Rao else 755c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 756a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 757e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 758a5aedd68SStacey Son sleep_cnt++; 759a5aedd68SStacey Son #endif 760f9819486SAttilio Rao if (error) { 761f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 762f9819486SAttilio Rao CTR2(KTR_LOCK, 763f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 764f9819486SAttilio Rao __func__, sx); 765f9819486SAttilio Rao break; 766f9819486SAttilio Rao } 7674e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7684e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 7694e7f640dSJohn Baldwin __func__, sx); 770c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7714e7f640dSJohn Baldwin } 772e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 773e41d6166SMateusz Guzik if (__predict_true(!extra_work)) 774e41d6166SMateusz Guzik return (error); 775e41d6166SMateusz Guzik #endif 776076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 777e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 778076dd8ebSAndriy Gapon if (sleep_time) 77932cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 780076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 781076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 7821ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 78332cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 784076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 785076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 786076dd8ebSAndriy Gapon #endif 787f9819486SAttilio Rao if (!error) 788de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 789de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_WRITER); 790076dd8ebSAndriy Gapon GIANT_RESTORE(); 791f9819486SAttilio Rao return (error); 7924e7f640dSJohn Baldwin } 7934e7f640dSJohn Baldwin 7944e7f640dSJohn Baldwin /* 7954e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xunlock 7964e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 7974e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 7984e7f640dSJohn Baldwin * accessible from at least sx.h. 7994e7f640dSJohn Baldwin */ 8004e7f640dSJohn Baldwin void 801b584eb2eSMateusz Guzik _sx_xunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 8024e7f640dSJohn Baldwin { 803b584eb2eSMateusz Guzik uintptr_t tid, setx; 804da7bbd2cSJohn Baldwin int queue, wakeup_swapper; 8054e7f640dSJohn Baldwin 80635370593SAndriy Gapon if (SCHEDULER_STOPPED()) 80735370593SAndriy Gapon return; 80835370593SAndriy Gapon 809b584eb2eSMateusz Guzik tid = (uintptr_t)curthread; 8104e7f640dSJohn Baldwin 811b584eb2eSMateusz Guzik if (__predict_false(x == tid)) 8123b3cf014SMateusz Guzik x = SX_READ_VALUE(sx); 813b584eb2eSMateusz Guzik 814b584eb2eSMateusz Guzik MPASS(!(x & SX_LOCK_SHARED)); 815b584eb2eSMateusz Guzik 816b584eb2eSMateusz Guzik if (__predict_false(x & SX_LOCK_RECURSED)) { 8176ebb77b6SMateusz Guzik /* The lock is recursed, unrecurse one level. */ 8184e7f640dSJohn Baldwin if ((--sx->sx_recurse) == 0) 8194e7f640dSJohn Baldwin atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 8204e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8214e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 8224e7f640dSJohn Baldwin return; 8234e7f640dSJohn Baldwin } 8243b3cf014SMateusz Guzik 8253b3cf014SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_WRITER); 8263b3cf014SMateusz Guzik if (x == tid && 8273b3cf014SMateusz Guzik atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED)) 8283b3cf014SMateusz Guzik return; 8293b3cf014SMateusz Guzik 8304e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8314e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 8324e7f640dSJohn Baldwin 8334e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 834bc24577cSMateusz Guzik x = SX_READ_VALUE(sx); 8352d96bd88SMateusz Guzik MPASS(x & (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)); 8364e7f640dSJohn Baldwin 8374e7f640dSJohn Baldwin /* 8384e7f640dSJohn Baldwin * The wake up algorithm here is quite simple and probably not 8394e7f640dSJohn Baldwin * ideal. It gives precedence to shared waiters if they are 8404e7f640dSJohn Baldwin * present. For this condition, we have to preserve the 8414e7f640dSJohn Baldwin * state of the exclusive waiters flag. 8422028867dSAttilio Rao * If interruptible sleeps left the shared queue empty avoid a 8432028867dSAttilio Rao * starvation for the threads sleeping on the exclusive queue by giving 8442028867dSAttilio Rao * them precedence and cleaning up the shared waiters bit anyway. 8454e7f640dSJohn Baldwin */ 846bc24577cSMateusz Guzik setx = SX_LOCK_UNLOCKED; 847bc24577cSMateusz Guzik queue = SQ_EXCLUSIVE_QUEUE; 848bc24577cSMateusz Guzik if ((x & SX_LOCK_SHARED_WAITERS) != 0 && 8492028867dSAttilio Rao sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) { 8504e7f640dSJohn Baldwin queue = SQ_SHARED_QUEUE; 851bc24577cSMateusz Guzik setx |= (x & SX_LOCK_EXCLUSIVE_WAITERS); 852bc24577cSMateusz Guzik } 853bc24577cSMateusz Guzik atomic_store_rel_ptr(&sx->sx_lock, setx); 8544e7f640dSJohn Baldwin 8554e7f640dSJohn Baldwin /* Wake up all the waiters for the specific queue. */ 8564e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8574e7f640dSJohn Baldwin CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 8584e7f640dSJohn Baldwin __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 8594e7f640dSJohn Baldwin "exclusive"); 860bc24577cSMateusz Guzik 861da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 862da7bbd2cSJohn Baldwin queue); 863c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 864da7bbd2cSJohn Baldwin if (wakeup_swapper) 865da7bbd2cSJohn Baldwin kick_proc0(); 8664e7f640dSJohn Baldwin } 8674e7f640dSJohn Baldwin 868834f70f3SMateusz Guzik static bool __always_inline 869013c0b49SMateusz Guzik __sx_slock_try(struct sx *sx, uintptr_t *xp LOCK_FILE_LINE_ARG_DEF) 870834f70f3SMateusz Guzik { 871834f70f3SMateusz Guzik 872834f70f3SMateusz Guzik /* 873834f70f3SMateusz Guzik * If no other thread has an exclusive lock then try to bump up 874834f70f3SMateusz Guzik * the count of sharers. Since we have to preserve the state 875834f70f3SMateusz Guzik * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 876834f70f3SMateusz Guzik * shared lock loop back and retry. 877834f70f3SMateusz Guzik */ 878834f70f3SMateusz Guzik while (*xp & SX_LOCK_SHARED) { 879834f70f3SMateusz Guzik MPASS(!(*xp & SX_LOCK_SHARED_WAITERS)); 880834f70f3SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, xp, 881834f70f3SMateusz Guzik *xp + SX_ONE_SHARER)) { 882834f70f3SMateusz Guzik if (LOCK_LOG_TEST(&sx->lock_object, 0)) 883834f70f3SMateusz Guzik CTR4(KTR_LOCK, "%s: %p succeed %p -> %p", 884834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 885834f70f3SMateusz Guzik (void *)(*xp + SX_ONE_SHARER)); 886834f70f3SMateusz Guzik return (true); 887834f70f3SMateusz Guzik } 888834f70f3SMateusz Guzik } 889834f70f3SMateusz Guzik return (false); 890834f70f3SMateusz Guzik } 891834f70f3SMateusz Guzik 892834f70f3SMateusz Guzik static int __noinline 893013c0b49SMateusz Guzik _sx_slock_hard(struct sx *sx, int opts, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 8944e7f640dSJohn Baldwin { 8954e7f640dSJohn Baldwin GIANT_DECLARE; 8964e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 8974e7f640dSJohn Baldwin volatile struct thread *owner; 8984e7f640dSJohn Baldwin #endif 8991723a064SJeff Roberson #ifdef LOCK_PROFILING 900c1a6d9faSAttilio Rao uint64_t waittime = 0; 901c1a6d9faSAttilio Rao int contested = 0; 9021723a064SJeff Roberson #endif 903c1a6d9faSAttilio Rao int error = 0; 90404126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 9051ada9041SMateusz Guzik struct lock_delay_arg lda; 9061ada9041SMateusz Guzik #endif 907a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 90861852185SMateusz Guzik u_int sleep_cnt = 0; 909a5aedd68SStacey Son int64_t sleep_time = 0; 910076dd8ebSAndriy Gapon int64_t all_time = 0; 911a5aedd68SStacey Son #endif 912e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 913e41d6166SMateusz Guzik uintptr_t state; 914e41d6166SMateusz Guzik #endif 915284194f1SMateusz Guzik int extra_work = 0; 916c1a6d9faSAttilio Rao 91735370593SAndriy Gapon if (SCHEDULER_STOPPED()) 91835370593SAndriy Gapon return (0); 91935370593SAndriy Gapon 920fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 9211ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 922fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 923fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 9241ada9041SMateusz Guzik #endif 925e41d6166SMateusz Guzik 926ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS 927ae7d25a4SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 928ae7d25a4SMateusz Guzik #endif 929ae7d25a4SMateusz Guzik lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 930ae7d25a4SMateusz Guzik &waittime); 931ae7d25a4SMateusz Guzik 932e41d6166SMateusz Guzik #ifdef LOCK_PROFILING 933e41d6166SMateusz Guzik extra_work = 1; 934e41d6166SMateusz Guzik state = x; 935e41d6166SMateusz Guzik #elif defined(KDTRACE_HOOKS) 936e41d6166SMateusz Guzik extra_work = lockstat_enabled; 937e41d6166SMateusz Guzik if (__predict_false(extra_work)) { 938e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 939c5f61e6fSMateusz Guzik state = x; 940e41d6166SMateusz Guzik } 941c5f61e6fSMateusz Guzik #endif 942076dd8ebSAndriy Gapon 9434e7f640dSJohn Baldwin /* 9444e7f640dSJohn Baldwin * As with rwlocks, we don't make any attempt to try to block 9454e7f640dSJohn Baldwin * shared locks once there is an exclusive waiter. 9464e7f640dSJohn Baldwin */ 9474e7f640dSJohn Baldwin for (;;) { 948013c0b49SMateusz Guzik if (__sx_slock_try(sx, &x LOCK_FILE_LINE_ARG)) 9494e7f640dSJohn Baldwin break; 950c5f61e6fSMateusz Guzik #ifdef KDTRACE_HOOKS 951c5f61e6fSMateusz Guzik lda.spin_cnt++; 952c5f61e6fSMateusz Guzik #endif 953c5f61e6fSMateusz Guzik 9544e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 9554e7f640dSJohn Baldwin /* 9564e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 9574e7f640dSJohn Baldwin * the owner stops running or the state of the lock 9584e7f640dSJohn Baldwin * changes. 9594e7f640dSJohn Baldwin */ 9601ae1c2a3SAttilio Rao if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 961c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 9624e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 9634e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9644e7f640dSJohn Baldwin CTR3(KTR_LOCK, 9654e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 9664e7f640dSJohn Baldwin __func__, sx, owner); 9672cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 9682cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 9692cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 970e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 971c5f61e6fSMateusz Guzik do { 9721ada9041SMateusz Guzik lock_delay(&lda); 973c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 974c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 975c5f61e6fSMateusz Guzik } while (owner != NULL && TD_IS_RUNNING(owner)); 9762cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 9772cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 9784e7f640dSJohn Baldwin continue; 9794e7f640dSJohn Baldwin } 9804e7f640dSJohn Baldwin } 9814e7f640dSJohn Baldwin #endif 9824e7f640dSJohn Baldwin 9834e7f640dSJohn Baldwin /* 9844e7f640dSJohn Baldwin * Some other thread already has an exclusive lock, so 9854e7f640dSJohn Baldwin * start the process of blocking. 9864e7f640dSJohn Baldwin */ 9874e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 988c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 9894e7f640dSJohn Baldwin 9904e7f640dSJohn Baldwin /* 9914e7f640dSJohn Baldwin * The lock could have been released while we spun. 9924e7f640dSJohn Baldwin * In this case loop back and retry. 9934e7f640dSJohn Baldwin */ 9944e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 9954e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 9964e7f640dSJohn Baldwin continue; 9974e7f640dSJohn Baldwin } 9984e7f640dSJohn Baldwin 9994e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 10004e7f640dSJohn Baldwin /* 10014e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 10024e7f640dSJohn Baldwin * the owner stops running or the state of the lock 10034e7f640dSJohn Baldwin * changes. 10044e7f640dSJohn Baldwin */ 10054e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 10061ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 10074e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 10084e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 10094e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 1010c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10114e7f640dSJohn Baldwin continue; 10124e7f640dSJohn Baldwin } 10134e7f640dSJohn Baldwin } 10144e7f640dSJohn Baldwin #endif 10154e7f640dSJohn Baldwin 10164e7f640dSJohn Baldwin /* 10174e7f640dSJohn Baldwin * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 10184e7f640dSJohn Baldwin * fail to set it drop the sleep queue lock and loop 10194e7f640dSJohn Baldwin * back. 10204e7f640dSJohn Baldwin */ 10214e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS)) { 10224e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 10234e7f640dSJohn Baldwin x | SX_LOCK_SHARED_WAITERS)) { 10244e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 1025c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10264e7f640dSJohn Baldwin continue; 10274e7f640dSJohn Baldwin } 10284e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10294e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 10304e7f640dSJohn Baldwin __func__, sx); 10314e7f640dSJohn Baldwin } 10324e7f640dSJohn Baldwin 10334e7f640dSJohn Baldwin /* 10344e7f640dSJohn Baldwin * Since we have been unable to acquire the shared lock, 10354e7f640dSJohn Baldwin * we have to sleep. 10364e7f640dSJohn Baldwin */ 10374e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10384e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 10394e7f640dSJohn Baldwin __func__, sx); 10404e7f640dSJohn Baldwin 1041a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 1042e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 1043a5aedd68SStacey Son #endif 1044e41d6166SMateusz Guzik GIANT_SAVE(extra_work); 10454e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 1046f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 1047f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 1048f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 1049c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 1050f9819486SAttilio Rao else 1051c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 1052a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 1053e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 1054a5aedd68SStacey Son sleep_cnt++; 1055a5aedd68SStacey Son #endif 1056f9819486SAttilio Rao if (error) { 1057f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1058f9819486SAttilio Rao CTR2(KTR_LOCK, 1059f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 1060f9819486SAttilio Rao __func__, sx); 1061f9819486SAttilio Rao break; 1062f9819486SAttilio Rao } 10634e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10644e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 10654e7f640dSJohn Baldwin __func__, sx); 1066c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10674e7f640dSJohn Baldwin } 1068e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 1069e41d6166SMateusz Guzik if (__predict_true(!extra_work)) 1070e41d6166SMateusz Guzik return (error); 1071e41d6166SMateusz Guzik #endif 1072076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 1073e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 1074076dd8ebSAndriy Gapon if (sleep_time) 107532cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 1076076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1077076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 10781ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 107932cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 1080076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1081076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 1082076dd8ebSAndriy Gapon #endif 10833ae56ce9SMateusz Guzik if (error == 0) { 1084de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 1085de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_READER); 10863ae56ce9SMateusz Guzik } 10874e7f640dSJohn Baldwin GIANT_RESTORE(); 1088f9819486SAttilio Rao return (error); 10894e7f640dSJohn Baldwin } 10904e7f640dSJohn Baldwin 1091834f70f3SMateusz Guzik int 1092013c0b49SMateusz Guzik _sx_slock_int(struct sx *sx, int opts LOCK_FILE_LINE_ARG_DEF) 10934e7f640dSJohn Baldwin { 10944e7f640dSJohn Baldwin uintptr_t x; 1095834f70f3SMateusz Guzik int error; 10964e7f640dSJohn Baldwin 1097704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 1098704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 1099834f70f3SMateusz Guzik ("sx_slock() by idle thread %p on sx %s @ %s:%d", 1100834f70f3SMateusz Guzik curthread, sx->lock_object.lo_name, file, line)); 11013ae56ce9SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1102834f70f3SMateusz Guzik ("sx_slock() of destroyed sx @ %s:%d", file, line)); 1103834f70f3SMateusz Guzik WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 1104834f70f3SMateusz Guzik 1105834f70f3SMateusz Guzik error = 0; 1106c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 1107834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__acquire) || 1108013c0b49SMateusz Guzik !__sx_slock_try(sx, &x LOCK_FILE_LINE_ARG))) 1109013c0b49SMateusz Guzik error = _sx_slock_hard(sx, opts, x LOCK_FILE_LINE_ARG); 1110834f70f3SMateusz Guzik if (error == 0) { 1111834f70f3SMateusz Guzik LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 1112834f70f3SMateusz Guzik WITNESS_LOCK(&sx->lock_object, 0, file, line); 1113834f70f3SMateusz Guzik TD_LOCKS_INC(curthread); 1114834f70f3SMateusz Guzik } 1115834f70f3SMateusz Guzik return (error); 1116834f70f3SMateusz Guzik } 1117834f70f3SMateusz Guzik 1118013c0b49SMateusz Guzik int 1119013c0b49SMateusz Guzik _sx_slock(struct sx *sx, int opts, const char *file, int line) 1120013c0b49SMateusz Guzik { 1121013c0b49SMateusz Guzik 1122013c0b49SMateusz Guzik return (_sx_slock_int(sx, opts LOCK_FILE_LINE_ARG)); 1123013c0b49SMateusz Guzik } 1124013c0b49SMateusz Guzik 1125834f70f3SMateusz Guzik static bool __always_inline 1126834f70f3SMateusz Guzik _sx_sunlock_try(struct sx *sx, uintptr_t *xp) 1127834f70f3SMateusz Guzik { 1128834f70f3SMateusz Guzik 11294e7f640dSJohn Baldwin for (;;) { 11304e7f640dSJohn Baldwin /* 11314e7f640dSJohn Baldwin * We should never have sharers while at least one thread 11324e7f640dSJohn Baldwin * holds a shared lock. 11334e7f640dSJohn Baldwin */ 1134834f70f3SMateusz Guzik KASSERT(!(*xp & SX_LOCK_SHARED_WAITERS), 11354e7f640dSJohn Baldwin ("%s: waiting sharers", __func__)); 11364e7f640dSJohn Baldwin 11374e7f640dSJohn Baldwin /* 11384e7f640dSJohn Baldwin * See if there is more than one shared lock held. If 11394e7f640dSJohn Baldwin * so, just drop one and return. 11404e7f640dSJohn Baldwin */ 1141834f70f3SMateusz Guzik if (SX_SHARERS(*xp) > 1) { 1142834f70f3SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, xp, 1143834f70f3SMateusz Guzik *xp - SX_ONE_SHARER)) { 11444e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 11454e7f640dSJohn Baldwin CTR4(KTR_LOCK, 11464e7f640dSJohn Baldwin "%s: %p succeeded %p -> %p", 1147834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 1148834f70f3SMateusz Guzik (void *)(*xp - SX_ONE_SHARER)); 1149834f70f3SMateusz Guzik return (true); 11504e7f640dSJohn Baldwin } 11514e7f640dSJohn Baldwin continue; 11524e7f640dSJohn Baldwin } 11534e7f640dSJohn Baldwin 11544e7f640dSJohn Baldwin /* 11554e7f640dSJohn Baldwin * If there aren't any waiters for an exclusive lock, 11564e7f640dSJohn Baldwin * then try to drop it quickly. 11574e7f640dSJohn Baldwin */ 1158834f70f3SMateusz Guzik if (!(*xp & SX_LOCK_EXCLUSIVE_WAITERS)) { 1159834f70f3SMateusz Guzik MPASS(*xp == SX_SHARERS_LOCK(1)); 1160834f70f3SMateusz Guzik *xp = SX_SHARERS_LOCK(1); 1161fa474043SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, 1162834f70f3SMateusz Guzik xp, SX_LOCK_UNLOCKED)) { 11634e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 11644e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p last succeeded", 11654e7f640dSJohn Baldwin __func__, sx); 1166834f70f3SMateusz Guzik return (true); 11674e7f640dSJohn Baldwin } 11684e7f640dSJohn Baldwin continue; 11694e7f640dSJohn Baldwin } 1170834f70f3SMateusz Guzik break; 1171834f70f3SMateusz Guzik } 1172834f70f3SMateusz Guzik return (false); 1173834f70f3SMateusz Guzik } 1174834f70f3SMateusz Guzik 1175834f70f3SMateusz Guzik static void __noinline 1176013c0b49SMateusz Guzik _sx_sunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 1177834f70f3SMateusz Guzik { 1178834f70f3SMateusz Guzik int wakeup_swapper; 1179834f70f3SMateusz Guzik 1180834f70f3SMateusz Guzik if (SCHEDULER_STOPPED()) 1181834f70f3SMateusz Guzik return; 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 } 1218*dbe4541dSMark Johnston LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER); 1219834f70f3SMateusz Guzik } 1220834f70f3SMateusz Guzik 1221834f70f3SMateusz Guzik void 1222013c0b49SMateusz Guzik _sx_sunlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 1223834f70f3SMateusz Guzik { 1224834f70f3SMateusz Guzik uintptr_t x; 1225834f70f3SMateusz Guzik 1226834f70f3SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1227834f70f3SMateusz Guzik ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 1228834f70f3SMateusz Guzik _sx_assert(sx, SA_SLOCKED, file, line); 1229834f70f3SMateusz Guzik WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 1230834f70f3SMateusz Guzik LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 1231834f70f3SMateusz Guzik 1232834f70f3SMateusz Guzik x = SX_READ_VALUE(sx); 1233834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__release) || 1234834f70f3SMateusz Guzik !_sx_sunlock_try(sx, &x))) 1235013c0b49SMateusz Guzik _sx_sunlock_hard(sx, x LOCK_FILE_LINE_ARG); 1236834f70f3SMateusz Guzik 12373ae56ce9SMateusz Guzik TD_LOCKS_DEC(curthread); 1238d55229b7SJason Evans } 12394e5e677bSJohn Baldwin 1240013c0b49SMateusz Guzik void 1241013c0b49SMateusz Guzik _sx_sunlock(struct sx *sx, const char *file, int line) 1242013c0b49SMateusz Guzik { 1243013c0b49SMateusz Guzik 1244013c0b49SMateusz Guzik _sx_sunlock_int(sx LOCK_FILE_LINE_ARG); 1245013c0b49SMateusz Guzik } 1246013c0b49SMateusz Guzik 12474e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT 1248781a35dfSJohn Baldwin #ifndef INVARIANTS 1249781a35dfSJohn Baldwin #undef _sx_assert 1250781a35dfSJohn Baldwin #endif 1251781a35dfSJohn Baldwin 12524e5e677bSJohn Baldwin /* 12534e5e677bSJohn Baldwin * In the non-WITNESS case, sx_assert() can only detect that at least 12544e5e677bSJohn Baldwin * *some* thread owns an slock, but it cannot guarantee that *this* 12554e5e677bSJohn Baldwin * thread owns an slock. 12564e5e677bSJohn Baldwin */ 12574e5e677bSJohn Baldwin void 1258d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line) 12594e5e677bSJohn Baldwin { 12604e7f640dSJohn Baldwin #ifndef WITNESS 12614e7f640dSJohn Baldwin int slocked = 0; 12624e7f640dSJohn Baldwin #endif 12634e5e677bSJohn Baldwin 126403129ba9SJohn Baldwin if (panicstr != NULL) 126503129ba9SJohn Baldwin return; 12664e5e677bSJohn Baldwin switch (what) { 12677ec137e5SJohn Baldwin case SA_SLOCKED: 12687ec137e5SJohn Baldwin case SA_SLOCKED | SA_NOTRECURSED: 12697ec137e5SJohn Baldwin case SA_SLOCKED | SA_RECURSED: 12704e7f640dSJohn Baldwin #ifndef WITNESS 12714e7f640dSJohn Baldwin slocked = 1; 12724e7f640dSJohn Baldwin /* FALLTHROUGH */ 12734e7f640dSJohn Baldwin #endif 12747ec137e5SJohn Baldwin case SA_LOCKED: 12757ec137e5SJohn Baldwin case SA_LOCKED | SA_NOTRECURSED: 12767ec137e5SJohn Baldwin case SA_LOCKED | SA_RECURSED: 12774e5e677bSJohn Baldwin #ifdef WITNESS 1278aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 12794e5e677bSJohn Baldwin #else 12804e7f640dSJohn Baldwin /* 12814e7f640dSJohn Baldwin * If some other thread has an exclusive lock or we 12824e7f640dSJohn Baldwin * have one and are asserting a shared lock, fail. 12834e7f640dSJohn Baldwin * Also, if no one has a lock at all, fail. 12844e7f640dSJohn Baldwin */ 12854e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED || 12864e7f640dSJohn Baldwin (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 12874e7f640dSJohn Baldwin sx_xholder(sx) != curthread))) 128803129ba9SJohn Baldwin panic("Lock %s not %slocked @ %s:%d\n", 12894e7f640dSJohn Baldwin sx->lock_object.lo_name, slocked ? "share " : "", 12904e7f640dSJohn Baldwin file, line); 12914e7f640dSJohn Baldwin 12924e7f640dSJohn Baldwin if (!(sx->sx_lock & SX_LOCK_SHARED)) { 12934e7f640dSJohn Baldwin if (sx_recursed(sx)) { 12947ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 12954e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 12964e7f640dSJohn Baldwin sx->lock_object.lo_name, file, 12974e7f640dSJohn Baldwin line); 12987ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 12994e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 13004e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13014e7f640dSJohn Baldwin } 13024e5e677bSJohn Baldwin #endif 13034e5e677bSJohn Baldwin break; 13047ec137e5SJohn Baldwin case SA_XLOCKED: 13057ec137e5SJohn Baldwin case SA_XLOCKED | SA_NOTRECURSED: 13067ec137e5SJohn Baldwin case SA_XLOCKED | SA_RECURSED: 13074e7f640dSJohn Baldwin if (sx_xholder(sx) != curthread) 130803129ba9SJohn Baldwin panic("Lock %s not exclusively locked @ %s:%d\n", 1309aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 13104e7f640dSJohn Baldwin if (sx_recursed(sx)) { 13117ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 13124e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 13134e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13147ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 13154e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 13164e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13174e5e677bSJohn Baldwin break; 13187ec137e5SJohn Baldwin case SA_UNLOCKED: 131919b0efd3SPawel Jakub Dawidek #ifdef WITNESS 1320aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 132119b0efd3SPawel Jakub Dawidek #else 1322f6739b1dSPawel Jakub Dawidek /* 13234e7f640dSJohn Baldwin * If we hold an exclusve lock fail. We can't 13244e7f640dSJohn Baldwin * reliably check to see if we hold a shared lock or 13254e7f640dSJohn Baldwin * not. 1326f6739b1dSPawel Jakub Dawidek */ 13274e7f640dSJohn Baldwin if (sx_xholder(sx) == curthread) 132803129ba9SJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 1329aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 133019b0efd3SPawel Jakub Dawidek #endif 133119b0efd3SPawel Jakub Dawidek break; 13324e5e677bSJohn Baldwin default: 13334e5e677bSJohn Baldwin panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 13344e5e677bSJohn Baldwin line); 13354e5e677bSJohn Baldwin } 13364e5e677bSJohn Baldwin } 13374e5e677bSJohn Baldwin #endif /* INVARIANT_SUPPORT */ 1338d272fe53SJohn Baldwin 1339d272fe53SJohn Baldwin #ifdef DDB 13404e7f640dSJohn Baldwin static void 1341d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock) 1342d272fe53SJohn Baldwin { 1343d272fe53SJohn Baldwin struct thread *td; 1344d576deedSPawel Jakub Dawidek const struct sx *sx; 1345d272fe53SJohn Baldwin 1346d576deedSPawel Jakub Dawidek sx = (const struct sx *)lock; 1347d272fe53SJohn Baldwin 1348d272fe53SJohn Baldwin db_printf(" state: "); 13494e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED) 13504e7f640dSJohn Baldwin db_printf("UNLOCKED\n"); 13510026c92cSJohn Baldwin else if (sx->sx_lock == SX_LOCK_DESTROYED) { 13520026c92cSJohn Baldwin db_printf("DESTROYED\n"); 13530026c92cSJohn Baldwin return; 13540026c92cSJohn Baldwin } else if (sx->sx_lock & SX_LOCK_SHARED) 13554e7f640dSJohn Baldwin db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 13564e7f640dSJohn Baldwin else { 13574e7f640dSJohn Baldwin td = sx_xholder(sx); 1358d272fe53SJohn Baldwin db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1359431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 13604e7f640dSJohn Baldwin if (sx_recursed(sx)) 13614e7f640dSJohn Baldwin db_printf(" recursed: %d\n", sx->sx_recurse); 13624e7f640dSJohn Baldwin } 13634e7f640dSJohn Baldwin 13644e7f640dSJohn Baldwin db_printf(" waiters: "); 13654e7f640dSJohn Baldwin switch(sx->sx_lock & 13664e7f640dSJohn Baldwin (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 13674e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS: 13684e7f640dSJohn Baldwin db_printf("shared\n"); 13694e7f640dSJohn Baldwin break; 13704e7f640dSJohn Baldwin case SX_LOCK_EXCLUSIVE_WAITERS: 13714e7f640dSJohn Baldwin db_printf("exclusive\n"); 13724e7f640dSJohn Baldwin break; 13734e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 13744e7f640dSJohn Baldwin db_printf("exclusive and shared\n"); 13754e7f640dSJohn Baldwin break; 13764e7f640dSJohn Baldwin default: 13774e7f640dSJohn Baldwin db_printf("none\n"); 13784e7f640dSJohn Baldwin } 1379d272fe53SJohn Baldwin } 1380462a7addSJohn Baldwin 1381462a7addSJohn Baldwin /* 1382462a7addSJohn Baldwin * Check to see if a thread that is blocked on a sleep queue is actually 1383462a7addSJohn Baldwin * blocked on an sx lock. If so, output some details and return true. 1384462a7addSJohn Baldwin * If the lock has an exclusive owner, return that in *ownerp. 1385462a7addSJohn Baldwin */ 1386462a7addSJohn Baldwin int 1387462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp) 1388462a7addSJohn Baldwin { 1389462a7addSJohn Baldwin struct sx *sx; 1390462a7addSJohn Baldwin 1391462a7addSJohn Baldwin /* 13924e7f640dSJohn Baldwin * Check to see if this thread is blocked on an sx lock. 13934e7f640dSJohn Baldwin * First, we check the lock class. If that is ok, then we 13944e7f640dSJohn Baldwin * compare the lock name against the wait message. 1395462a7addSJohn Baldwin */ 13964e7f640dSJohn Baldwin sx = td->td_wchan; 13974e7f640dSJohn Baldwin if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 13984e7f640dSJohn Baldwin sx->lock_object.lo_name != td->td_wmesg) 1399462a7addSJohn Baldwin return (0); 1400462a7addSJohn Baldwin 1401462a7addSJohn Baldwin /* We think we have an sx lock, so output some details. */ 1402462a7addSJohn Baldwin db_printf("blocked on sx \"%s\" ", td->td_wmesg); 14034e7f640dSJohn Baldwin *ownerp = sx_xholder(sx); 14044e7f640dSJohn Baldwin if (sx->sx_lock & SX_LOCK_SHARED) 14054e7f640dSJohn Baldwin db_printf("SLOCK (count %ju)\n", 14064e7f640dSJohn Baldwin (uintmax_t)SX_SHARERS(sx->sx_lock)); 14074e7f640dSJohn Baldwin else 1408462a7addSJohn Baldwin db_printf("XLOCK\n"); 1409462a7addSJohn Baldwin return (1); 1410462a7addSJohn Baldwin } 1411d272fe53SJohn Baldwin #endif 1412