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 914e7f640dSJohn Baldwin #define GIANT_SAVE() do { \ 924e7f640dSJohn Baldwin if (mtx_owned(&Giant)) { \ 934e7f640dSJohn Baldwin WITNESS_SAVE(&Giant.lock_object, Giant); \ 944e7f640dSJohn Baldwin while (mtx_owned(&Giant)) { \ 954e7f640dSJohn Baldwin _giantcnt++; \ 964e7f640dSJohn Baldwin mtx_unlock(&Giant); \ 974e7f640dSJohn Baldwin } \ 984e7f640dSJohn Baldwin } \ 994e7f640dSJohn Baldwin } while (0) 1004e7f640dSJohn Baldwin 1014e7f640dSJohn Baldwin #define GIANT_RESTORE() do { \ 1024e7f640dSJohn Baldwin if (_giantcnt > 0) { \ 1034e7f640dSJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); \ 1044e7f640dSJohn Baldwin while (_giantcnt--) \ 1054e7f640dSJohn Baldwin mtx_lock(&Giant); \ 1064e7f640dSJohn Baldwin WITNESS_RESTORE(&Giant.lock_object, Giant); \ 1074e7f640dSJohn Baldwin } \ 1084e7f640dSJohn Baldwin } while (0) 1094e7f640dSJohn Baldwin 1104e7f640dSJohn Baldwin /* 111da7d0d1eSJohn Baldwin * Returns true if an exclusive lock is recursed. It assumes 112da7d0d1eSJohn Baldwin * curthread currently has an exclusive lock. 1134e7f640dSJohn Baldwin */ 1144e7f640dSJohn Baldwin #define sx_recursed(sx) ((sx)->sx_recurse != 0) 1154e7f640dSJohn Baldwin 116d576deedSPawel Jakub Dawidek static void assert_sx(const struct lock_object *lock, int what); 1174e7f640dSJohn Baldwin #ifdef DDB 118d576deedSPawel Jakub Dawidek static void db_show_sx(const struct lock_object *lock); 119d272fe53SJohn Baldwin #endif 1207faf4d90SDavide Italiano static void lock_sx(struct lock_object *lock, uintptr_t how); 121a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 122d576deedSPawel Jakub Dawidek static int owner_sx(const struct lock_object *lock, struct thread **owner); 123a5aedd68SStacey Son #endif 1247faf4d90SDavide Italiano static uintptr_t unlock_sx(struct lock_object *lock); 125d272fe53SJohn Baldwin 12619284646SJohn Baldwin struct lock_class lock_class_sx = { 127ae8dde30SJohn Baldwin .lc_name = "sx", 128ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, 129f9721b43SAttilio Rao .lc_assert = assert_sx, 130d272fe53SJohn Baldwin #ifdef DDB 131ae8dde30SJohn Baldwin .lc_ddb_show = db_show_sx, 132d272fe53SJohn Baldwin #endif 1336e21afd4SJohn Baldwin .lc_lock = lock_sx, 1346e21afd4SJohn Baldwin .lc_unlock = unlock_sx, 135a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 136a5aedd68SStacey Son .lc_owner = owner_sx, 137a5aedd68SStacey Son #endif 13819284646SJohn Baldwin }; 13919284646SJohn Baldwin 140781a35dfSJohn Baldwin #ifndef INVARIANTS 141781a35dfSJohn Baldwin #define _sx_assert(sx, what, file, line) 142781a35dfSJohn Baldwin #endif 143781a35dfSJohn Baldwin 1441ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX 1451ae1c2a3SAttilio Rao static u_int asx_retries = 10; 1461ae1c2a3SAttilio Rao static u_int asx_loops = 10000; 1476472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging"); 148fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, ""); 149fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, ""); 1501ada9041SMateusz Guzik 1518e5a3e9aSMateusz Guzik static struct lock_delay_config __read_mostly sx_delay; 1521ada9041SMateusz Guzik 1538e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_sx, OID_AUTO, delay_base, CTLFLAG_RW, &sx_delay.base, 1541ada9041SMateusz Guzik 0, ""); 1551ada9041SMateusz Guzik SYSCTL_INT(_debug_sx, OID_AUTO, delay_max, CTLFLAG_RW, &sx_delay.max, 1561ada9041SMateusz Guzik 0, ""); 1571ada9041SMateusz Guzik 1588e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(sx_delay); 1591ae1c2a3SAttilio Rao #endif 1601ae1c2a3SAttilio Rao 1616281b30aSJason Evans void 162d576deedSPawel Jakub Dawidek assert_sx(const struct lock_object *lock, int what) 163f9721b43SAttilio Rao { 164f9721b43SAttilio Rao 165d576deedSPawel Jakub Dawidek sx_assert((const struct sx *)lock, what); 166f9721b43SAttilio Rao } 167f9721b43SAttilio Rao 168f9721b43SAttilio Rao void 1697faf4d90SDavide Italiano lock_sx(struct lock_object *lock, uintptr_t how) 1706e21afd4SJohn Baldwin { 1716e21afd4SJohn Baldwin struct sx *sx; 1726e21afd4SJohn Baldwin 1736e21afd4SJohn Baldwin sx = (struct sx *)lock; 1746e21afd4SJohn Baldwin if (how) 1756e21afd4SJohn Baldwin sx_slock(sx); 176cf6b879fSDavide Italiano else 177cf6b879fSDavide Italiano sx_xlock(sx); 1786e21afd4SJohn Baldwin } 1796e21afd4SJohn Baldwin 1807faf4d90SDavide Italiano uintptr_t 1816e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock) 1826e21afd4SJohn Baldwin { 1836e21afd4SJohn Baldwin struct sx *sx; 1846e21afd4SJohn Baldwin 1856e21afd4SJohn Baldwin sx = (struct sx *)lock; 1867ec137e5SJohn Baldwin sx_assert(sx, SA_LOCKED | SA_NOTRECURSED); 1876e21afd4SJohn Baldwin if (sx_xlocked(sx)) { 1886e21afd4SJohn Baldwin sx_xunlock(sx); 189cf6b879fSDavide Italiano return (0); 1906e21afd4SJohn Baldwin } else { 1916e21afd4SJohn Baldwin sx_sunlock(sx); 192cf6b879fSDavide Italiano return (1); 1936e21afd4SJohn Baldwin } 1946e21afd4SJohn Baldwin } 1956e21afd4SJohn Baldwin 196a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 197a5aedd68SStacey Son int 198d576deedSPawel Jakub Dawidek owner_sx(const struct lock_object *lock, struct thread **owner) 199a5aedd68SStacey Son { 200c365a293SMark Johnston const struct sx *sx; 201c365a293SMark Johnston uintptr_t x; 202a5aedd68SStacey Son 203c365a293SMark Johnston sx = (const struct sx *)lock; 204c365a293SMark Johnston x = sx->sx_lock; 205c365a293SMark Johnston *owner = NULL; 206a5aedd68SStacey Son return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) : 207c365a293SMark Johnston ((*owner = (struct thread *)SX_OWNER(x)) != NULL)); 208a5aedd68SStacey Son } 209a5aedd68SStacey Son #endif 210a5aedd68SStacey Son 2116e21afd4SJohn Baldwin void 212c27b5699SAndrew R. Reiter sx_sysinit(void *arg) 213c27b5699SAndrew R. Reiter { 214c27b5699SAndrew R. Reiter struct sx_args *sargs = arg; 215c27b5699SAndrew R. Reiter 216e4cd31ddSJeff Roberson sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags); 217c27b5699SAndrew R. Reiter } 218c27b5699SAndrew R. Reiter 219c27b5699SAndrew R. Reiter void 2204e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts) 2216281b30aSJason Evans { 2224e7f640dSJohn Baldwin int flags; 2236281b30aSJason Evans 224b0d67325SJohn Baldwin MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK | 225fd07ddcfSDmitry Chagin SX_NOPROFILE | SX_NOADAPTIVE | SX_NEW)) == 0); 226353998acSAttilio Rao ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock, 227353998acSAttilio Rao ("%s: sx_lock not aligned for %s: %p", __func__, description, 228353998acSAttilio Rao &sx->sx_lock)); 229b0d67325SJohn Baldwin 230f0830182SAttilio Rao flags = LO_SLEEPABLE | LO_UPGRADABLE; 2314e7f640dSJohn Baldwin if (opts & SX_DUPOK) 2324e7f640dSJohn Baldwin flags |= LO_DUPOK; 2334e7f640dSJohn Baldwin if (opts & SX_NOPROFILE) 2344e7f640dSJohn Baldwin flags |= LO_NOPROFILE; 2354e7f640dSJohn Baldwin if (!(opts & SX_NOWITNESS)) 2364e7f640dSJohn Baldwin flags |= LO_WITNESS; 237f0830182SAttilio Rao if (opts & SX_RECURSE) 238f0830182SAttilio Rao flags |= LO_RECURSABLE; 2394e7f640dSJohn Baldwin if (opts & SX_QUIET) 2404e7f640dSJohn Baldwin flags |= LO_QUIET; 241fd07ddcfSDmitry Chagin if (opts & SX_NEW) 242fd07ddcfSDmitry Chagin flags |= LO_NEW; 2434e7f640dSJohn Baldwin 244f0830182SAttilio Rao flags |= opts & SX_NOADAPTIVE; 245b5fb43e5SJohn Baldwin lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags); 2464e7f640dSJohn Baldwin sx->sx_lock = SX_LOCK_UNLOCKED; 2474e7f640dSJohn Baldwin sx->sx_recurse = 0; 2486281b30aSJason Evans } 2496281b30aSJason Evans 2506281b30aSJason Evans void 2516281b30aSJason Evans sx_destroy(struct sx *sx) 2526281b30aSJason Evans { 2536281b30aSJason Evans 2544e7f640dSJohn Baldwin KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held")); 2554e7f640dSJohn Baldwin KASSERT(sx->sx_recurse == 0, ("sx lock still recursed")); 2560026c92cSJohn Baldwin sx->sx_lock = SX_LOCK_DESTROYED; 257aa89d8cdSJohn Baldwin lock_destroy(&sx->lock_object); 2586281b30aSJason Evans } 2596281b30aSJason Evans 260f9819486SAttilio Rao int 2619fde98bbSAttilio Rao sx_try_slock_(struct sx *sx, const char *file, int line) 2625f36700aSJohn Baldwin { 2634e7f640dSJohn Baldwin uintptr_t x; 2645f36700aSJohn Baldwin 26535370593SAndriy Gapon if (SCHEDULER_STOPPED()) 26635370593SAndriy Gapon return (1); 26735370593SAndriy Gapon 268cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 269e3ae0dfeSAttilio Rao ("sx_try_slock() by idle thread %p on sx %s @ %s:%d", 270e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 271e3ae0dfeSAttilio Rao 2724e7f640dSJohn Baldwin x = sx->sx_lock; 2735c5df0d9SMateusz Guzik for (;;) { 2740026c92cSJohn Baldwin KASSERT(x != SX_LOCK_DESTROYED, 2750026c92cSJohn Baldwin ("sx_try_slock() of destroyed sx @ %s:%d", file, line)); 276764a938bSPawel Jakub Dawidek if (!(x & SX_LOCK_SHARED)) 277764a938bSPawel Jakub Dawidek break; 2785c5df0d9SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, x + SX_ONE_SHARER)) { 279aa89d8cdSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line); 280aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line); 281de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, 282de2c95ccSMark Johnston sx, 0, 0, file, line, LOCKSTAT_READER); 283ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 2845f36700aSJohn Baldwin return (1); 2855f36700aSJohn Baldwin } 286764a938bSPawel Jakub Dawidek } 2874e7f640dSJohn Baldwin 2884e7f640dSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line); 2894e7f640dSJohn Baldwin return (0); 2905f36700aSJohn Baldwin } 2915f36700aSJohn Baldwin 292f9819486SAttilio Rao int 293f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line) 2946281b30aSJason Evans { 2956ebb77b6SMateusz Guzik uintptr_t tid, x; 296f9819486SAttilio Rao int error = 0; 2976281b30aSJason Evans 298cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 299e3ae0dfeSAttilio Rao ("sx_xlock() by idle thread %p on sx %s @ %s:%d", 300e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 3010026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3020026c92cSJohn Baldwin ("sx_xlock() of destroyed sx @ %s:%d", file, line)); 303aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 30441313430SJohn Baldwin line, NULL); 3056ebb77b6SMateusz Guzik tid = (uintptr_t)curthread; 3066ebb77b6SMateusz Guzik x = SX_LOCK_UNLOCKED; 3076ebb77b6SMateusz Guzik if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 3086ebb77b6SMateusz Guzik error = _sx_xlock_hard(sx, x, tid, opts, file, line); 3096ebb77b6SMateusz Guzik else 3106ebb77b6SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 3116ebb77b6SMateusz Guzik 0, 0, file, line, LOCKSTAT_WRITER); 312f9819486SAttilio Rao if (!error) { 313f9819486SAttilio Rao LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, 314f9819486SAttilio Rao file, line); 315aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 316ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 3176281b30aSJason Evans } 3186281b30aSJason Evans 319f9819486SAttilio Rao return (error); 320f9819486SAttilio Rao } 321f9819486SAttilio Rao 3225f36700aSJohn Baldwin int 3239fde98bbSAttilio Rao sx_try_xlock_(struct sx *sx, const char *file, int line) 3245f36700aSJohn Baldwin { 3255c5df0d9SMateusz Guzik struct thread *td; 3265c5df0d9SMateusz Guzik uintptr_t tid, x; 3274e7f640dSJohn Baldwin int rval; 3285c5df0d9SMateusz Guzik bool recursed; 3295f36700aSJohn Baldwin 3305c5df0d9SMateusz Guzik td = curthread; 3315c5df0d9SMateusz Guzik tid = (uintptr_t)td; 3325c5df0d9SMateusz Guzik if (SCHEDULER_STOPPED_TD(td)) 33335370593SAndriy Gapon return (1); 33435370593SAndriy Gapon 335cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 336e3ae0dfeSAttilio Rao ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d", 337e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 3380026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3390026c92cSJohn Baldwin ("sx_try_xlock() of destroyed sx @ %s:%d", file, line)); 3404e7f640dSJohn Baldwin 3415c5df0d9SMateusz Guzik rval = 1; 3425c5df0d9SMateusz Guzik recursed = false; 3435c5df0d9SMateusz Guzik x = SX_LOCK_UNLOCKED; 344*b247fd39SMateusz Guzik for (;;) { 345*b247fd39SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 346*b247fd39SMateusz Guzik break; 347*b247fd39SMateusz Guzik if (x == SX_LOCK_UNLOCKED) 348*b247fd39SMateusz Guzik continue; 3495c5df0d9SMateusz Guzik if (x == tid && (sx->lock_object.lo_flags & LO_RECURSABLE)) { 3504e7f640dSJohn Baldwin sx->sx_recurse++; 3514e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 352*b247fd39SMateusz Guzik break; 3535c5df0d9SMateusz Guzik } 354*b247fd39SMateusz Guzik rval = 0; 355*b247fd39SMateusz Guzik break; 3565c5df0d9SMateusz Guzik } 3575c5df0d9SMateusz Guzik 3584e7f640dSJohn Baldwin LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line); 3594e7f640dSJohn Baldwin if (rval) { 3604e7f640dSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 3614e7f640dSJohn Baldwin file, line); 3625c5df0d9SMateusz Guzik if (!recursed) 363de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, 364de2c95ccSMark Johnston sx, 0, 0, file, line, LOCKSTAT_WRITER); 365ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 3665f36700aSJohn Baldwin } 3674e7f640dSJohn Baldwin 3684e7f640dSJohn Baldwin return (rval); 3695f36700aSJohn Baldwin } 3705f36700aSJohn Baldwin 3716281b30aSJason Evans void 37219284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line) 3736281b30aSJason Evans { 3746281b30aSJason Evans 3750026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3760026c92cSJohn Baldwin ("sx_xunlock() of destroyed sx @ %s:%d", file, line)); 3777ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED, file, line); 378aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 3794e7f640dSJohn Baldwin LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file, 3804e7f640dSJohn Baldwin line); 3810108a980SMateusz Guzik #if LOCK_DEBUG > 0 3826ebb77b6SMateusz Guzik _sx_xunlock_hard(sx, (uintptr_t)curthread, file, line); 383ffd5c94cSMateusz Guzik #else 384ffd5c94cSMateusz Guzik __sx_xunlock(sx, curthread, file, line); 385ffd5c94cSMateusz Guzik #endif 386ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 3876281b30aSJason Evans } 388d55229b7SJason Evans 3894e7f640dSJohn Baldwin /* 3904e7f640dSJohn Baldwin * Try to do a non-blocking upgrade from a shared lock to an exclusive lock. 3914e7f640dSJohn Baldwin * This will only succeed if this thread holds a single shared lock. 3924e7f640dSJohn Baldwin * Return 1 if if the upgrade succeed, 0 otherwise. 3934e7f640dSJohn Baldwin */ 394d55229b7SJason Evans int 3959fde98bbSAttilio Rao sx_try_upgrade_(struct sx *sx, const char *file, int line) 396d55229b7SJason Evans { 3974e7f640dSJohn Baldwin uintptr_t x; 3984e7f640dSJohn Baldwin int success; 399d55229b7SJason Evans 40035370593SAndriy Gapon if (SCHEDULER_STOPPED()) 40135370593SAndriy Gapon return (1); 40235370593SAndriy Gapon 4030026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4040026c92cSJohn Baldwin ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 4057ec137e5SJohn Baldwin _sx_assert(sx, SA_SLOCKED, file, line); 406d55229b7SJason Evans 4074e7f640dSJohn Baldwin /* 4084e7f640dSJohn Baldwin * Try to switch from one shared lock to an exclusive lock. We need 4094e7f640dSJohn Baldwin * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 4104e7f640dSJohn Baldwin * we will wake up the exclusive waiters when we drop the lock. 4114e7f640dSJohn Baldwin */ 4124e7f640dSJohn Baldwin x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS; 4134e7f640dSJohn Baldwin success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, 4144e7f640dSJohn Baldwin (uintptr_t)curthread | x); 4154e7f640dSJohn Baldwin LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 416a5aedd68SStacey Son if (success) { 417aa89d8cdSJohn Baldwin WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 418b0b7cb50SJohn Baldwin file, line); 41932cd0147SMark Johnston LOCKSTAT_RECORD0(sx__upgrade, sx); 420a5aedd68SStacey Son } 4214e7f640dSJohn Baldwin return (success); 422d55229b7SJason Evans } 423d55229b7SJason Evans 4244e7f640dSJohn Baldwin /* 4254e7f640dSJohn Baldwin * Downgrade an unrecursed exclusive lock into a single shared lock. 4264e7f640dSJohn Baldwin */ 427d55229b7SJason Evans void 4289fde98bbSAttilio Rao sx_downgrade_(struct sx *sx, const char *file, int line) 429d55229b7SJason Evans { 4304e7f640dSJohn Baldwin uintptr_t x; 431da7bbd2cSJohn Baldwin int wakeup_swapper; 432d55229b7SJason Evans 43335370593SAndriy Gapon if (SCHEDULER_STOPPED()) 43435370593SAndriy Gapon return; 43535370593SAndriy Gapon 4360026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4370026c92cSJohn Baldwin ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 4387ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 4394e7f640dSJohn Baldwin #ifndef INVARIANTS 4404e7f640dSJohn Baldwin if (sx_recursed(sx)) 4414e7f640dSJohn Baldwin panic("downgrade of a recursed lock"); 4424e7f640dSJohn Baldwin #endif 443d55229b7SJason Evans 444aa89d8cdSJohn Baldwin WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 445d55229b7SJason Evans 4464e7f640dSJohn Baldwin /* 4474e7f640dSJohn Baldwin * Try to switch from an exclusive lock with no shared waiters 4484e7f640dSJohn Baldwin * to one sharer with no shared waiters. If there are 4494e7f640dSJohn Baldwin * exclusive waiters, we don't need to lock the sleep queue so 4504e7f640dSJohn Baldwin * long as we preserve the flag. We do one quick try and if 4514e7f640dSJohn Baldwin * that fails we grab the sleepq lock to keep the flags from 4524e7f640dSJohn Baldwin * changing and do it the slow way. 4534e7f640dSJohn Baldwin * 4544e7f640dSJohn Baldwin * We have to lock the sleep queue if there are shared waiters 4554e7f640dSJohn Baldwin * so we can wake them up. 4564e7f640dSJohn Baldwin */ 4574e7f640dSJohn Baldwin x = sx->sx_lock; 4584e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS) && 4594e7f640dSJohn Baldwin atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 4604e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS))) { 4614e7f640dSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 4624e7f640dSJohn Baldwin return; 4634e7f640dSJohn Baldwin } 4644e7f640dSJohn Baldwin 4654e7f640dSJohn Baldwin /* 4664e7f640dSJohn Baldwin * Lock the sleep queue so we can read the waiters bits 4674e7f640dSJohn Baldwin * without any races and wakeup any shared waiters. 4684e7f640dSJohn Baldwin */ 4694e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 4704e7f640dSJohn Baldwin 4714e7f640dSJohn Baldwin /* 4724e7f640dSJohn Baldwin * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 4734e7f640dSJohn Baldwin * shared lock. If there are any shared waiters, wake them up. 4744e7f640dSJohn Baldwin */ 475da7bbd2cSJohn Baldwin wakeup_swapper = 0; 4764e7f640dSJohn Baldwin x = sx->sx_lock; 4774e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 4784e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS)); 4794e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED_WAITERS) 480da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 481da7bbd2cSJohn Baldwin 0, SQ_SHARED_QUEUE); 4824e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 483d55229b7SJason Evans 484aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 48532cd0147SMark Johnston LOCKSTAT_RECORD0(sx__downgrade, sx); 486da7bbd2cSJohn Baldwin 487da7bbd2cSJohn Baldwin if (wakeup_swapper) 488da7bbd2cSJohn Baldwin kick_proc0(); 4894e7f640dSJohn Baldwin } 490d55229b7SJason Evans 4914e7f640dSJohn Baldwin /* 4924e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xlock 4934e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 4944e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 4954e7f640dSJohn Baldwin * accessible from at least sx.h. 4964e7f640dSJohn Baldwin */ 497f9819486SAttilio Rao int 498fa474043SMateusz Guzik _sx_xlock_hard(struct sx *sx, uintptr_t x, uintptr_t tid, int opts, 499fa474043SMateusz Guzik const char *file, int line) 5004e7f640dSJohn Baldwin { 5014e7f640dSJohn Baldwin GIANT_DECLARE; 5024e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5034e7f640dSJohn Baldwin volatile struct thread *owner; 5041ae1c2a3SAttilio Rao u_int i, spintries = 0; 5054e7f640dSJohn Baldwin #endif 5061723a064SJeff Roberson #ifdef LOCK_PROFILING 5071723a064SJeff Roberson uint64_t waittime = 0; 5081723a064SJeff Roberson int contested = 0; 5091723a064SJeff Roberson #endif 5101723a064SJeff Roberson int error = 0; 51104126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 5121ada9041SMateusz Guzik struct lock_delay_arg lda; 5131ada9041SMateusz Guzik #endif 514a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 515076dd8ebSAndriy Gapon uintptr_t state; 51661852185SMateusz Guzik u_int sleep_cnt = 0; 517a5aedd68SStacey Son int64_t sleep_time = 0; 518076dd8ebSAndriy Gapon int64_t all_time = 0; 519a5aedd68SStacey Son #endif 5204e7f640dSJohn Baldwin 52135370593SAndriy Gapon if (SCHEDULER_STOPPED()) 52235370593SAndriy Gapon return (0); 52335370593SAndriy Gapon 524fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 5251ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 526fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 527fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 5281ada9041SMateusz Guzik #endif 5291ada9041SMateusz Guzik 530c1aaf63cSMateusz Guzik if (__predict_false(x == SX_LOCK_UNLOCKED)) 531c1aaf63cSMateusz Guzik x = SX_READ_VALUE(sx); 532c1aaf63cSMateusz Guzik 5334e7f640dSJohn Baldwin /* If we already hold an exclusive lock, then recurse. */ 534c5f61e6fSMateusz Guzik if (__predict_false(lv_sx_owner(x) == (struct thread *)tid)) { 535f0830182SAttilio Rao KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0, 536b0d67325SJohn Baldwin ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 537b0d67325SJohn Baldwin sx->lock_object.lo_name, file, line)); 5384e7f640dSJohn Baldwin sx->sx_recurse++; 5394e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 5404e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5414e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 542f9819486SAttilio Rao return (0); 5434e7f640dSJohn Baldwin } 5444e7f640dSJohn Baldwin 5454e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5464e7f640dSJohn Baldwin CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 5474e7f640dSJohn Baldwin sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 5484e7f640dSJohn Baldwin 549076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 550e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 551c5f61e6fSMateusz Guzik state = x; 552076dd8ebSAndriy Gapon #endif 553fc4f686dSMateusz Guzik for (;;) { 554c5f61e6fSMateusz Guzik if (x == SX_LOCK_UNLOCKED) { 555fa474043SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 556fc4f686dSMateusz Guzik break; 557c5f61e6fSMateusz Guzik continue; 558c5f61e6fSMateusz Guzik } 559a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 5601ada9041SMateusz Guzik lda.spin_cnt++; 561a5aedd68SStacey Son #endif 562f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 563f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 564f5f9340bSFabien Thomas #endif 565eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 566eea4f254SJeff Roberson &waittime); 5674e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5684e7f640dSJohn Baldwin /* 5694e7f640dSJohn Baldwin * If the lock is write locked and the owner is 5704e7f640dSJohn Baldwin * running on another CPU, spin until the owner stops 5714e7f640dSJohn Baldwin * running or the state of the lock changes. 5724e7f640dSJohn Baldwin */ 5738545538bSJohn Baldwin if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 5741ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0) { 575c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 5764e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 5774e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5784e7f640dSJohn Baldwin CTR3(KTR_LOCK, 5794e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 5804e7f640dSJohn Baldwin __func__, sx, owner); 5812cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 5822cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 5832cba8dd3SJohn Baldwin "lockname:\"%s\"", 5842cba8dd3SJohn Baldwin sx->lock_object.lo_name); 5854e7f640dSJohn Baldwin GIANT_SAVE(); 586c5f61e6fSMateusz Guzik do { 5871ada9041SMateusz Guzik lock_delay(&lda); 588c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 589c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 590c5f61e6fSMateusz Guzik } while (owner != NULL && 591c5f61e6fSMateusz Guzik TD_IS_RUNNING(owner)); 5922cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 5932cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 5944e7f640dSJohn Baldwin continue; 5954e7f640dSJohn Baldwin } 5961ae1c2a3SAttilio Rao } else if (SX_SHARERS(x) && spintries < asx_retries) { 5972cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 5982cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 5992cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 6008d3635c4SAttilio Rao GIANT_SAVE(); 6011ae1c2a3SAttilio Rao spintries++; 6021ae1c2a3SAttilio Rao for (i = 0; i < asx_loops; i++) { 6031ae1c2a3SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6041ae1c2a3SAttilio Rao CTR4(KTR_LOCK, 6051ae1c2a3SAttilio Rao "%s: shared spinning on %p with %u and %u", 6061ae1c2a3SAttilio Rao __func__, sx, spintries, i); 6071ae1c2a3SAttilio Rao x = sx->sx_lock; 6081ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0 || 6091ae1c2a3SAttilio Rao SX_SHARERS(x) == 0) 6101ae1c2a3SAttilio Rao break; 6111ae1c2a3SAttilio Rao cpu_spinwait(); 6121ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS 6131ada9041SMateusz Guzik lda.spin_cnt++; 6141ae1c2a3SAttilio Rao #endif 6151ae1c2a3SAttilio Rao } 6162cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 6172cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 618c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6191ae1c2a3SAttilio Rao if (i != asx_loops) 6201ae1c2a3SAttilio Rao continue; 6211ae1c2a3SAttilio Rao } 6224e7f640dSJohn Baldwin } 6234e7f640dSJohn Baldwin #endif 6244e7f640dSJohn Baldwin 6254e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 626c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6274e7f640dSJohn Baldwin 6284e7f640dSJohn Baldwin /* 6294e7f640dSJohn Baldwin * If the lock was released while spinning on the 6304e7f640dSJohn Baldwin * sleep queue chain lock, try again. 6314e7f640dSJohn Baldwin */ 6324e7f640dSJohn Baldwin if (x == SX_LOCK_UNLOCKED) { 6334e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6344e7f640dSJohn Baldwin continue; 6354e7f640dSJohn Baldwin } 6364e7f640dSJohn Baldwin 6374e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 6384e7f640dSJohn Baldwin /* 6394e7f640dSJohn Baldwin * The current lock owner might have started executing 6404e7f640dSJohn Baldwin * on another CPU (or the lock could have changed 6414e7f640dSJohn Baldwin * owners) while we were waiting on the sleep queue 6424e7f640dSJohn Baldwin * chain lock. If so, drop the sleep queue lock and try 6434e7f640dSJohn Baldwin * again. 6444e7f640dSJohn Baldwin */ 6454e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 6461ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 6474e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 6484e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 6494e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6504e7f640dSJohn Baldwin continue; 6514e7f640dSJohn Baldwin } 6524e7f640dSJohn Baldwin } 6534e7f640dSJohn Baldwin #endif 6544e7f640dSJohn Baldwin 6554e7f640dSJohn Baldwin /* 6564e7f640dSJohn Baldwin * If an exclusive lock was released with both shared 6574e7f640dSJohn Baldwin * and exclusive waiters and a shared waiter hasn't 6584e7f640dSJohn Baldwin * woken up and acquired the lock yet, sx_lock will be 6594e7f640dSJohn Baldwin * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 6604e7f640dSJohn Baldwin * If we see that value, try to acquire it once. Note 6614e7f640dSJohn Baldwin * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 6624e7f640dSJohn Baldwin * as there are other exclusive waiters still. If we 6634e7f640dSJohn Baldwin * fail, restart the loop. 6644e7f640dSJohn Baldwin */ 6654e7f640dSJohn Baldwin if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 6664e7f640dSJohn Baldwin if (atomic_cmpset_acq_ptr(&sx->sx_lock, 6674e7f640dSJohn Baldwin SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS, 6684e7f640dSJohn Baldwin tid | SX_LOCK_EXCLUSIVE_WAITERS)) { 6694e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6704e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p claimed by new writer", 6714e7f640dSJohn Baldwin __func__, sx); 6724e7f640dSJohn Baldwin break; 6734e7f640dSJohn Baldwin } 6744e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 675c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6764e7f640dSJohn Baldwin continue; 6774e7f640dSJohn Baldwin } 6784e7f640dSJohn Baldwin 6794e7f640dSJohn Baldwin /* 6804e7f640dSJohn Baldwin * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 6814e7f640dSJohn Baldwin * than loop back and retry. 6824e7f640dSJohn Baldwin */ 6834e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 6844e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 6854e7f640dSJohn Baldwin x | SX_LOCK_EXCLUSIVE_WAITERS)) { 6864e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 687c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6884e7f640dSJohn Baldwin continue; 6894e7f640dSJohn Baldwin } 6904e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6914e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 6924e7f640dSJohn Baldwin __func__, sx); 6934e7f640dSJohn Baldwin } 6944e7f640dSJohn Baldwin 6954e7f640dSJohn Baldwin /* 6964e7f640dSJohn Baldwin * Since we have been unable to acquire the exclusive 6974e7f640dSJohn Baldwin * lock and the exclusive waiters flag is set, we have 6984e7f640dSJohn Baldwin * to sleep. 6994e7f640dSJohn Baldwin */ 7004e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7014e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 7024e7f640dSJohn Baldwin __func__, sx); 7034e7f640dSJohn Baldwin 704a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 705e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 706a5aedd68SStacey Son #endif 7074e7f640dSJohn Baldwin GIANT_SAVE(); 7084e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 709f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 710f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 711f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 712c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 713f9819486SAttilio Rao else 714c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 715a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 716e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 717a5aedd68SStacey Son sleep_cnt++; 718a5aedd68SStacey Son #endif 719f9819486SAttilio Rao if (error) { 720f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 721f9819486SAttilio Rao CTR2(KTR_LOCK, 722f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 723f9819486SAttilio Rao __func__, sx); 724f9819486SAttilio Rao break; 725f9819486SAttilio Rao } 7264e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7274e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 7284e7f640dSJohn Baldwin __func__, sx); 729c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7304e7f640dSJohn Baldwin } 731076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 732e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 733076dd8ebSAndriy Gapon if (sleep_time) 73432cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 735076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 736076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 7371ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 73832cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 739076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 740076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 741076dd8ebSAndriy Gapon #endif 742f9819486SAttilio Rao if (!error) 743de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 744de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_WRITER); 745076dd8ebSAndriy Gapon GIANT_RESTORE(); 746f9819486SAttilio Rao return (error); 7474e7f640dSJohn Baldwin } 7484e7f640dSJohn Baldwin 7494e7f640dSJohn Baldwin /* 7504e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xunlock 7514e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 7524e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 7534e7f640dSJohn Baldwin * accessible from at least sx.h. 7544e7f640dSJohn Baldwin */ 7554e7f640dSJohn Baldwin void 7564e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line) 7574e7f640dSJohn Baldwin { 7584e7f640dSJohn Baldwin uintptr_t x; 759da7bbd2cSJohn Baldwin int queue, wakeup_swapper; 7604e7f640dSJohn Baldwin 76135370593SAndriy Gapon if (SCHEDULER_STOPPED()) 76235370593SAndriy Gapon return; 76335370593SAndriy Gapon 7644e7f640dSJohn Baldwin MPASS(!(sx->sx_lock & SX_LOCK_SHARED)); 7654e7f640dSJohn Baldwin 7663b3cf014SMateusz Guzik x = SX_READ_VALUE(sx); 7673b3cf014SMateusz Guzik if (x & SX_LOCK_RECURSED) { 7686ebb77b6SMateusz Guzik /* The lock is recursed, unrecurse one level. */ 7694e7f640dSJohn Baldwin if ((--sx->sx_recurse) == 0) 7704e7f640dSJohn Baldwin atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 7714e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7724e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 7734e7f640dSJohn Baldwin return; 7744e7f640dSJohn Baldwin } 7753b3cf014SMateusz Guzik 7763b3cf014SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_WRITER); 7773b3cf014SMateusz Guzik if (x == tid && 7783b3cf014SMateusz Guzik atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED)) 7793b3cf014SMateusz Guzik return; 7803b3cf014SMateusz Guzik 7814e7f640dSJohn Baldwin MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS | 7824e7f640dSJohn Baldwin SX_LOCK_EXCLUSIVE_WAITERS)); 7834e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7844e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 7854e7f640dSJohn Baldwin 7864e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 7874e7f640dSJohn Baldwin x = SX_LOCK_UNLOCKED; 7884e7f640dSJohn Baldwin 7894e7f640dSJohn Baldwin /* 7904e7f640dSJohn Baldwin * The wake up algorithm here is quite simple and probably not 7914e7f640dSJohn Baldwin * ideal. It gives precedence to shared waiters if they are 7924e7f640dSJohn Baldwin * present. For this condition, we have to preserve the 7934e7f640dSJohn Baldwin * state of the exclusive waiters flag. 7942028867dSAttilio Rao * If interruptible sleeps left the shared queue empty avoid a 7952028867dSAttilio Rao * starvation for the threads sleeping on the exclusive queue by giving 7962028867dSAttilio Rao * them precedence and cleaning up the shared waiters bit anyway. 7974e7f640dSJohn Baldwin */ 7982028867dSAttilio Rao if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 && 7992028867dSAttilio Rao sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) { 8004e7f640dSJohn Baldwin queue = SQ_SHARED_QUEUE; 8014e7f640dSJohn Baldwin x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS); 8024e7f640dSJohn Baldwin } else 8034e7f640dSJohn Baldwin queue = SQ_EXCLUSIVE_QUEUE; 8044e7f640dSJohn Baldwin 8054e7f640dSJohn Baldwin /* Wake up all the waiters for the specific queue. */ 8064e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8074e7f640dSJohn Baldwin CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 8084e7f640dSJohn Baldwin __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 8094e7f640dSJohn Baldwin "exclusive"); 8104e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, x); 811da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 812da7bbd2cSJohn Baldwin queue); 813c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 814da7bbd2cSJohn Baldwin if (wakeup_swapper) 815da7bbd2cSJohn Baldwin kick_proc0(); 8164e7f640dSJohn Baldwin } 8174e7f640dSJohn Baldwin 818834f70f3SMateusz Guzik static bool __always_inline 819834f70f3SMateusz Guzik __sx_slock_try(struct sx *sx, uintptr_t *xp, const char *file, int line) 820834f70f3SMateusz Guzik { 821834f70f3SMateusz Guzik 822834f70f3SMateusz Guzik /* 823834f70f3SMateusz Guzik * If no other thread has an exclusive lock then try to bump up 824834f70f3SMateusz Guzik * the count of sharers. Since we have to preserve the state 825834f70f3SMateusz Guzik * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 826834f70f3SMateusz Guzik * shared lock loop back and retry. 827834f70f3SMateusz Guzik */ 828834f70f3SMateusz Guzik while (*xp & SX_LOCK_SHARED) { 829834f70f3SMateusz Guzik MPASS(!(*xp & SX_LOCK_SHARED_WAITERS)); 830834f70f3SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, xp, 831834f70f3SMateusz Guzik *xp + SX_ONE_SHARER)) { 832834f70f3SMateusz Guzik if (LOCK_LOG_TEST(&sx->lock_object, 0)) 833834f70f3SMateusz Guzik CTR4(KTR_LOCK, "%s: %p succeed %p -> %p", 834834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 835834f70f3SMateusz Guzik (void *)(*xp + SX_ONE_SHARER)); 836834f70f3SMateusz Guzik return (true); 837834f70f3SMateusz Guzik } 838834f70f3SMateusz Guzik } 839834f70f3SMateusz Guzik return (false); 840834f70f3SMateusz Guzik } 841834f70f3SMateusz Guzik 842834f70f3SMateusz Guzik static int __noinline 843834f70f3SMateusz Guzik _sx_slock_hard(struct sx *sx, int opts, const char *file, int line, uintptr_t x) 8444e7f640dSJohn Baldwin { 8454e7f640dSJohn Baldwin GIANT_DECLARE; 8464e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 8474e7f640dSJohn Baldwin volatile struct thread *owner; 8484e7f640dSJohn Baldwin #endif 8491723a064SJeff Roberson #ifdef LOCK_PROFILING 850c1a6d9faSAttilio Rao uint64_t waittime = 0; 851c1a6d9faSAttilio Rao int contested = 0; 8521723a064SJeff Roberson #endif 853c1a6d9faSAttilio Rao int error = 0; 85404126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 8551ada9041SMateusz Guzik struct lock_delay_arg lda; 8561ada9041SMateusz Guzik #endif 857a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 858076dd8ebSAndriy Gapon uintptr_t state; 85961852185SMateusz Guzik u_int sleep_cnt = 0; 860a5aedd68SStacey Son int64_t sleep_time = 0; 861076dd8ebSAndriy Gapon int64_t all_time = 0; 862a5aedd68SStacey Son #endif 863c1a6d9faSAttilio Rao 86435370593SAndriy Gapon if (SCHEDULER_STOPPED()) 86535370593SAndriy Gapon return (0); 86635370593SAndriy Gapon 867fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 8681ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 869fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 870fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 8711ada9041SMateusz Guzik #endif 872076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 873e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 874c5f61e6fSMateusz Guzik state = x; 875c5f61e6fSMateusz Guzik #endif 876076dd8ebSAndriy Gapon 8774e7f640dSJohn Baldwin /* 8784e7f640dSJohn Baldwin * As with rwlocks, we don't make any attempt to try to block 8794e7f640dSJohn Baldwin * shared locks once there is an exclusive waiter. 8804e7f640dSJohn Baldwin */ 8814e7f640dSJohn Baldwin for (;;) { 882834f70f3SMateusz Guzik if (__sx_slock_try(sx, &x, file, line)) 8834e7f640dSJohn Baldwin break; 884c5f61e6fSMateusz Guzik #ifdef KDTRACE_HOOKS 885c5f61e6fSMateusz Guzik lda.spin_cnt++; 886c5f61e6fSMateusz Guzik #endif 887c5f61e6fSMateusz Guzik 888f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 889f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 890f5f9340bSFabien Thomas #endif 891eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 892eea4f254SJeff Roberson &waittime); 8934e7f640dSJohn Baldwin 8944e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 8954e7f640dSJohn Baldwin /* 8964e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 8974e7f640dSJohn Baldwin * the owner stops running or the state of the lock 8984e7f640dSJohn Baldwin * changes. 8994e7f640dSJohn Baldwin */ 9001ae1c2a3SAttilio Rao if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 901c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 9024e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 9034e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9044e7f640dSJohn Baldwin CTR3(KTR_LOCK, 9054e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 9064e7f640dSJohn Baldwin __func__, sx, owner); 9072cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 9082cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 9092cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 9104e7f640dSJohn Baldwin GIANT_SAVE(); 911c5f61e6fSMateusz Guzik do { 9121ada9041SMateusz Guzik lock_delay(&lda); 913c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 914c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 915c5f61e6fSMateusz Guzik } while (owner != NULL && TD_IS_RUNNING(owner)); 9162cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 9172cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 9184e7f640dSJohn Baldwin continue; 9194e7f640dSJohn Baldwin } 9204e7f640dSJohn Baldwin } 9214e7f640dSJohn Baldwin #endif 9224e7f640dSJohn Baldwin 9234e7f640dSJohn Baldwin /* 9244e7f640dSJohn Baldwin * Some other thread already has an exclusive lock, so 9254e7f640dSJohn Baldwin * start the process of blocking. 9264e7f640dSJohn Baldwin */ 9274e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 928c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 9294e7f640dSJohn Baldwin 9304e7f640dSJohn Baldwin /* 9314e7f640dSJohn Baldwin * The lock could have been released while we spun. 9324e7f640dSJohn Baldwin * In this case loop back and retry. 9334e7f640dSJohn Baldwin */ 9344e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 9354e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 9364e7f640dSJohn Baldwin continue; 9374e7f640dSJohn Baldwin } 9384e7f640dSJohn Baldwin 9394e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 9404e7f640dSJohn Baldwin /* 9414e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 9424e7f640dSJohn Baldwin * the owner stops running or the state of the lock 9434e7f640dSJohn Baldwin * changes. 9444e7f640dSJohn Baldwin */ 9454e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 9461ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 9474e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 9484e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 9494e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 950c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 9514e7f640dSJohn Baldwin continue; 9524e7f640dSJohn Baldwin } 9534e7f640dSJohn Baldwin } 9544e7f640dSJohn Baldwin #endif 9554e7f640dSJohn Baldwin 9564e7f640dSJohn Baldwin /* 9574e7f640dSJohn Baldwin * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 9584e7f640dSJohn Baldwin * fail to set it drop the sleep queue lock and loop 9594e7f640dSJohn Baldwin * back. 9604e7f640dSJohn Baldwin */ 9614e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS)) { 9624e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 9634e7f640dSJohn Baldwin x | SX_LOCK_SHARED_WAITERS)) { 9644e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 965c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 9664e7f640dSJohn Baldwin continue; 9674e7f640dSJohn Baldwin } 9684e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9694e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 9704e7f640dSJohn Baldwin __func__, sx); 9714e7f640dSJohn Baldwin } 9724e7f640dSJohn Baldwin 9734e7f640dSJohn Baldwin /* 9744e7f640dSJohn Baldwin * Since we have been unable to acquire the shared lock, 9754e7f640dSJohn Baldwin * we have to sleep. 9764e7f640dSJohn Baldwin */ 9774e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9784e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 9794e7f640dSJohn Baldwin __func__, sx); 9804e7f640dSJohn Baldwin 981a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 982e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 983a5aedd68SStacey Son #endif 9844e7f640dSJohn Baldwin GIANT_SAVE(); 9854e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 986f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 987f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 988f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 989c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 990f9819486SAttilio Rao else 991c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 992a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 993e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 994a5aedd68SStacey Son sleep_cnt++; 995a5aedd68SStacey Son #endif 996f9819486SAttilio Rao if (error) { 997f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 998f9819486SAttilio Rao CTR2(KTR_LOCK, 999f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 1000f9819486SAttilio Rao __func__, sx); 1001f9819486SAttilio Rao break; 1002f9819486SAttilio Rao } 10034e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10044e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 10054e7f640dSJohn Baldwin __func__, sx); 1006c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10074e7f640dSJohn Baldwin } 1008076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 1009e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 1010076dd8ebSAndriy Gapon if (sleep_time) 101132cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 1012076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1013076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 10141ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 101532cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 1016076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1017076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 1018076dd8ebSAndriy Gapon #endif 10193ae56ce9SMateusz Guzik if (error == 0) { 1020de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 1021de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_READER); 10223ae56ce9SMateusz Guzik } 10234e7f640dSJohn Baldwin GIANT_RESTORE(); 1024f9819486SAttilio Rao return (error); 10254e7f640dSJohn Baldwin } 10264e7f640dSJohn Baldwin 1027834f70f3SMateusz Guzik int 1028834f70f3SMateusz Guzik _sx_slock(struct sx *sx, int opts, const char *file, int line) 10294e7f640dSJohn Baldwin { 10304e7f640dSJohn Baldwin uintptr_t x; 1031834f70f3SMateusz Guzik int error; 10324e7f640dSJohn Baldwin 1033834f70f3SMateusz Guzik KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 1034834f70f3SMateusz Guzik ("sx_slock() by idle thread %p on sx %s @ %s:%d", 1035834f70f3SMateusz Guzik curthread, sx->lock_object.lo_name, file, line)); 10363ae56ce9SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1037834f70f3SMateusz Guzik ("sx_slock() of destroyed sx @ %s:%d", file, line)); 1038834f70f3SMateusz Guzik WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 1039834f70f3SMateusz Guzik 1040834f70f3SMateusz Guzik error = 0; 1041c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 1042834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__acquire) || 1043834f70f3SMateusz Guzik !__sx_slock_try(sx, &x, file, line))) 1044834f70f3SMateusz Guzik error = _sx_slock_hard(sx, opts, file, line, x); 1045834f70f3SMateusz Guzik if (error == 0) { 1046834f70f3SMateusz Guzik LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 1047834f70f3SMateusz Guzik WITNESS_LOCK(&sx->lock_object, 0, file, line); 1048834f70f3SMateusz Guzik TD_LOCKS_INC(curthread); 1049834f70f3SMateusz Guzik } 1050834f70f3SMateusz Guzik return (error); 1051834f70f3SMateusz Guzik } 1052834f70f3SMateusz Guzik 1053834f70f3SMateusz Guzik static bool __always_inline 1054834f70f3SMateusz Guzik _sx_sunlock_try(struct sx *sx, uintptr_t *xp) 1055834f70f3SMateusz Guzik { 1056834f70f3SMateusz Guzik 10574e7f640dSJohn Baldwin for (;;) { 10584e7f640dSJohn Baldwin /* 10594e7f640dSJohn Baldwin * We should never have sharers while at least one thread 10604e7f640dSJohn Baldwin * holds a shared lock. 10614e7f640dSJohn Baldwin */ 1062834f70f3SMateusz Guzik KASSERT(!(*xp & SX_LOCK_SHARED_WAITERS), 10634e7f640dSJohn Baldwin ("%s: waiting sharers", __func__)); 10644e7f640dSJohn Baldwin 10654e7f640dSJohn Baldwin /* 10664e7f640dSJohn Baldwin * See if there is more than one shared lock held. If 10674e7f640dSJohn Baldwin * so, just drop one and return. 10684e7f640dSJohn Baldwin */ 1069834f70f3SMateusz Guzik if (SX_SHARERS(*xp) > 1) { 1070834f70f3SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, xp, 1071834f70f3SMateusz Guzik *xp - SX_ONE_SHARER)) { 10724e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10734e7f640dSJohn Baldwin CTR4(KTR_LOCK, 10744e7f640dSJohn Baldwin "%s: %p succeeded %p -> %p", 1075834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 1076834f70f3SMateusz Guzik (void *)(*xp - SX_ONE_SHARER)); 1077834f70f3SMateusz Guzik return (true); 10784e7f640dSJohn Baldwin } 10794e7f640dSJohn Baldwin continue; 10804e7f640dSJohn Baldwin } 10814e7f640dSJohn Baldwin 10824e7f640dSJohn Baldwin /* 10834e7f640dSJohn Baldwin * If there aren't any waiters for an exclusive lock, 10844e7f640dSJohn Baldwin * then try to drop it quickly. 10854e7f640dSJohn Baldwin */ 1086834f70f3SMateusz Guzik if (!(*xp & SX_LOCK_EXCLUSIVE_WAITERS)) { 1087834f70f3SMateusz Guzik MPASS(*xp == SX_SHARERS_LOCK(1)); 1088834f70f3SMateusz Guzik *xp = SX_SHARERS_LOCK(1); 1089fa474043SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, 1090834f70f3SMateusz Guzik xp, SX_LOCK_UNLOCKED)) { 10914e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10924e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p last succeeded", 10934e7f640dSJohn Baldwin __func__, sx); 1094834f70f3SMateusz Guzik return (true); 10954e7f640dSJohn Baldwin } 10964e7f640dSJohn Baldwin continue; 10974e7f640dSJohn Baldwin } 1098834f70f3SMateusz Guzik break; 1099834f70f3SMateusz Guzik } 1100834f70f3SMateusz Guzik return (false); 1101834f70f3SMateusz Guzik } 1102834f70f3SMateusz Guzik 1103834f70f3SMateusz Guzik static void __noinline 1104834f70f3SMateusz Guzik _sx_sunlock_hard(struct sx *sx, uintptr_t x, const char *file, int line) 1105834f70f3SMateusz Guzik { 1106834f70f3SMateusz Guzik int wakeup_swapper; 1107834f70f3SMateusz Guzik 1108834f70f3SMateusz Guzik if (SCHEDULER_STOPPED()) 1109834f70f3SMateusz Guzik return; 1110834f70f3SMateusz Guzik 1111834f70f3SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER); 1112834f70f3SMateusz Guzik 1113834f70f3SMateusz Guzik for (;;) { 1114834f70f3SMateusz Guzik if (_sx_sunlock_try(sx, &x)) 1115834f70f3SMateusz Guzik break; 11164e7f640dSJohn Baldwin 11174e7f640dSJohn Baldwin /* 11184e7f640dSJohn Baldwin * At this point, there should just be one sharer with 11194e7f640dSJohn Baldwin * exclusive waiters. 11204e7f640dSJohn Baldwin */ 11214e7f640dSJohn Baldwin MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS)); 11224e7f640dSJohn Baldwin 11234e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 11244e7f640dSJohn Baldwin 11254e7f640dSJohn Baldwin /* 11264e7f640dSJohn Baldwin * Wake up semantic here is quite simple: 11274e7f640dSJohn Baldwin * Just wake up all the exclusive waiters. 11284e7f640dSJohn Baldwin * Note that the state of the lock could have changed, 11294e7f640dSJohn Baldwin * so if it fails loop back and retry. 11304e7f640dSJohn Baldwin */ 1131ddce63caSAttilio Rao if (!atomic_cmpset_rel_ptr(&sx->sx_lock, 11324e7f640dSJohn Baldwin SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS, 11334e7f640dSJohn Baldwin SX_LOCK_UNLOCKED)) { 11344e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 1135c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 11364e7f640dSJohn Baldwin continue; 11374e7f640dSJohn Baldwin } 11384e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 11394e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p waking up all thread on" 11404e7f640dSJohn Baldwin "exclusive queue", __func__, sx); 1141da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 1142da7bbd2cSJohn Baldwin 0, SQ_EXCLUSIVE_QUEUE); 1143c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 1144da7bbd2cSJohn Baldwin if (wakeup_swapper) 1145da7bbd2cSJohn Baldwin kick_proc0(); 11464e7f640dSJohn Baldwin break; 11474e7f640dSJohn Baldwin } 1148834f70f3SMateusz Guzik } 1149834f70f3SMateusz Guzik 1150834f70f3SMateusz Guzik void 1151834f70f3SMateusz Guzik _sx_sunlock(struct sx *sx, const char *file, int line) 1152834f70f3SMateusz Guzik { 1153834f70f3SMateusz Guzik uintptr_t x; 1154834f70f3SMateusz Guzik 1155834f70f3SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1156834f70f3SMateusz Guzik ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 1157834f70f3SMateusz Guzik _sx_assert(sx, SA_SLOCKED, file, line); 1158834f70f3SMateusz Guzik WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 1159834f70f3SMateusz Guzik LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 1160834f70f3SMateusz Guzik 1161834f70f3SMateusz Guzik x = SX_READ_VALUE(sx); 1162834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__release) || 1163834f70f3SMateusz Guzik !_sx_sunlock_try(sx, &x))) 1164834f70f3SMateusz Guzik _sx_sunlock_hard(sx, x, file, line); 1165834f70f3SMateusz Guzik 11663ae56ce9SMateusz Guzik TD_LOCKS_DEC(curthread); 1167d55229b7SJason Evans } 11684e5e677bSJohn Baldwin 11694e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT 1170781a35dfSJohn Baldwin #ifndef INVARIANTS 1171781a35dfSJohn Baldwin #undef _sx_assert 1172781a35dfSJohn Baldwin #endif 1173781a35dfSJohn Baldwin 11744e5e677bSJohn Baldwin /* 11754e5e677bSJohn Baldwin * In the non-WITNESS case, sx_assert() can only detect that at least 11764e5e677bSJohn Baldwin * *some* thread owns an slock, but it cannot guarantee that *this* 11774e5e677bSJohn Baldwin * thread owns an slock. 11784e5e677bSJohn Baldwin */ 11794e5e677bSJohn Baldwin void 1180d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line) 11814e5e677bSJohn Baldwin { 11824e7f640dSJohn Baldwin #ifndef WITNESS 11834e7f640dSJohn Baldwin int slocked = 0; 11844e7f640dSJohn Baldwin #endif 11854e5e677bSJohn Baldwin 118603129ba9SJohn Baldwin if (panicstr != NULL) 118703129ba9SJohn Baldwin return; 11884e5e677bSJohn Baldwin switch (what) { 11897ec137e5SJohn Baldwin case SA_SLOCKED: 11907ec137e5SJohn Baldwin case SA_SLOCKED | SA_NOTRECURSED: 11917ec137e5SJohn Baldwin case SA_SLOCKED | SA_RECURSED: 11924e7f640dSJohn Baldwin #ifndef WITNESS 11934e7f640dSJohn Baldwin slocked = 1; 11944e7f640dSJohn Baldwin /* FALLTHROUGH */ 11954e7f640dSJohn Baldwin #endif 11967ec137e5SJohn Baldwin case SA_LOCKED: 11977ec137e5SJohn Baldwin case SA_LOCKED | SA_NOTRECURSED: 11987ec137e5SJohn Baldwin case SA_LOCKED | SA_RECURSED: 11994e5e677bSJohn Baldwin #ifdef WITNESS 1200aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 12014e5e677bSJohn Baldwin #else 12024e7f640dSJohn Baldwin /* 12034e7f640dSJohn Baldwin * If some other thread has an exclusive lock or we 12044e7f640dSJohn Baldwin * have one and are asserting a shared lock, fail. 12054e7f640dSJohn Baldwin * Also, if no one has a lock at all, fail. 12064e7f640dSJohn Baldwin */ 12074e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED || 12084e7f640dSJohn Baldwin (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 12094e7f640dSJohn Baldwin sx_xholder(sx) != curthread))) 121003129ba9SJohn Baldwin panic("Lock %s not %slocked @ %s:%d\n", 12114e7f640dSJohn Baldwin sx->lock_object.lo_name, slocked ? "share " : "", 12124e7f640dSJohn Baldwin file, line); 12134e7f640dSJohn Baldwin 12144e7f640dSJohn Baldwin if (!(sx->sx_lock & SX_LOCK_SHARED)) { 12154e7f640dSJohn Baldwin if (sx_recursed(sx)) { 12167ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 12174e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 12184e7f640dSJohn Baldwin sx->lock_object.lo_name, file, 12194e7f640dSJohn Baldwin line); 12207ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 12214e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 12224e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 12234e7f640dSJohn Baldwin } 12244e5e677bSJohn Baldwin #endif 12254e5e677bSJohn Baldwin break; 12267ec137e5SJohn Baldwin case SA_XLOCKED: 12277ec137e5SJohn Baldwin case SA_XLOCKED | SA_NOTRECURSED: 12287ec137e5SJohn Baldwin case SA_XLOCKED | SA_RECURSED: 12294e7f640dSJohn Baldwin if (sx_xholder(sx) != curthread) 123003129ba9SJohn Baldwin panic("Lock %s not exclusively locked @ %s:%d\n", 1231aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 12324e7f640dSJohn Baldwin if (sx_recursed(sx)) { 12337ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 12344e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 12354e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 12367ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 12374e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 12384e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 12394e5e677bSJohn Baldwin break; 12407ec137e5SJohn Baldwin case SA_UNLOCKED: 124119b0efd3SPawel Jakub Dawidek #ifdef WITNESS 1242aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 124319b0efd3SPawel Jakub Dawidek #else 1244f6739b1dSPawel Jakub Dawidek /* 12454e7f640dSJohn Baldwin * If we hold an exclusve lock fail. We can't 12464e7f640dSJohn Baldwin * reliably check to see if we hold a shared lock or 12474e7f640dSJohn Baldwin * not. 1248f6739b1dSPawel Jakub Dawidek */ 12494e7f640dSJohn Baldwin if (sx_xholder(sx) == curthread) 125003129ba9SJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 1251aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 125219b0efd3SPawel Jakub Dawidek #endif 125319b0efd3SPawel Jakub Dawidek break; 12544e5e677bSJohn Baldwin default: 12554e5e677bSJohn Baldwin panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 12564e5e677bSJohn Baldwin line); 12574e5e677bSJohn Baldwin } 12584e5e677bSJohn Baldwin } 12594e5e677bSJohn Baldwin #endif /* INVARIANT_SUPPORT */ 1260d272fe53SJohn Baldwin 1261d272fe53SJohn Baldwin #ifdef DDB 12624e7f640dSJohn Baldwin static void 1263d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock) 1264d272fe53SJohn Baldwin { 1265d272fe53SJohn Baldwin struct thread *td; 1266d576deedSPawel Jakub Dawidek const struct sx *sx; 1267d272fe53SJohn Baldwin 1268d576deedSPawel Jakub Dawidek sx = (const struct sx *)lock; 1269d272fe53SJohn Baldwin 1270d272fe53SJohn Baldwin db_printf(" state: "); 12714e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED) 12724e7f640dSJohn Baldwin db_printf("UNLOCKED\n"); 12730026c92cSJohn Baldwin else if (sx->sx_lock == SX_LOCK_DESTROYED) { 12740026c92cSJohn Baldwin db_printf("DESTROYED\n"); 12750026c92cSJohn Baldwin return; 12760026c92cSJohn Baldwin } else if (sx->sx_lock & SX_LOCK_SHARED) 12774e7f640dSJohn Baldwin db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 12784e7f640dSJohn Baldwin else { 12794e7f640dSJohn Baldwin td = sx_xholder(sx); 1280d272fe53SJohn Baldwin db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1281431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 12824e7f640dSJohn Baldwin if (sx_recursed(sx)) 12834e7f640dSJohn Baldwin db_printf(" recursed: %d\n", sx->sx_recurse); 12844e7f640dSJohn Baldwin } 12854e7f640dSJohn Baldwin 12864e7f640dSJohn Baldwin db_printf(" waiters: "); 12874e7f640dSJohn Baldwin switch(sx->sx_lock & 12884e7f640dSJohn Baldwin (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 12894e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS: 12904e7f640dSJohn Baldwin db_printf("shared\n"); 12914e7f640dSJohn Baldwin break; 12924e7f640dSJohn Baldwin case SX_LOCK_EXCLUSIVE_WAITERS: 12934e7f640dSJohn Baldwin db_printf("exclusive\n"); 12944e7f640dSJohn Baldwin break; 12954e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 12964e7f640dSJohn Baldwin db_printf("exclusive and shared\n"); 12974e7f640dSJohn Baldwin break; 12984e7f640dSJohn Baldwin default: 12994e7f640dSJohn Baldwin db_printf("none\n"); 13004e7f640dSJohn Baldwin } 1301d272fe53SJohn Baldwin } 1302462a7addSJohn Baldwin 1303462a7addSJohn Baldwin /* 1304462a7addSJohn Baldwin * Check to see if a thread that is blocked on a sleep queue is actually 1305462a7addSJohn Baldwin * blocked on an sx lock. If so, output some details and return true. 1306462a7addSJohn Baldwin * If the lock has an exclusive owner, return that in *ownerp. 1307462a7addSJohn Baldwin */ 1308462a7addSJohn Baldwin int 1309462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp) 1310462a7addSJohn Baldwin { 1311462a7addSJohn Baldwin struct sx *sx; 1312462a7addSJohn Baldwin 1313462a7addSJohn Baldwin /* 13144e7f640dSJohn Baldwin * Check to see if this thread is blocked on an sx lock. 13154e7f640dSJohn Baldwin * First, we check the lock class. If that is ok, then we 13164e7f640dSJohn Baldwin * compare the lock name against the wait message. 1317462a7addSJohn Baldwin */ 13184e7f640dSJohn Baldwin sx = td->td_wchan; 13194e7f640dSJohn Baldwin if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 13204e7f640dSJohn Baldwin sx->lock_object.lo_name != td->td_wmesg) 1321462a7addSJohn Baldwin return (0); 1322462a7addSJohn Baldwin 1323462a7addSJohn Baldwin /* We think we have an sx lock, so output some details. */ 1324462a7addSJohn Baldwin db_printf("blocked on sx \"%s\" ", td->td_wmesg); 13254e7f640dSJohn Baldwin *ownerp = sx_xholder(sx); 13264e7f640dSJohn Baldwin if (sx->sx_lock & SX_LOCK_SHARED) 13274e7f640dSJohn Baldwin db_printf("SLOCK (count %ju)\n", 13284e7f640dSJohn Baldwin (uintmax_t)SX_SHARERS(sx->sx_lock)); 13294e7f640dSJohn Baldwin else 1330462a7addSJohn Baldwin db_printf("XLOCK\n"); 1331462a7addSJohn Baldwin return (1); 1332462a7addSJohn Baldwin } 1333d272fe53SJohn Baldwin #endif 1334