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 145*574adb65SMateusz Guzik static __read_frequently u_int asx_retries = 10; 146*574adb65SMateusz Guzik static __read_frequently 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 151*574adb65SMateusz Guzik static struct lock_delay_config __read_frequently 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 298704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 299704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 300e3ae0dfeSAttilio Rao ("sx_xlock() by idle thread %p on sx %s @ %s:%d", 301e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 3020026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3030026c92cSJohn Baldwin ("sx_xlock() of destroyed sx @ %s:%d", file, line)); 304aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 30541313430SJohn Baldwin line, NULL); 3066ebb77b6SMateusz Guzik tid = (uintptr_t)curthread; 3076ebb77b6SMateusz Guzik x = SX_LOCK_UNLOCKED; 3086ebb77b6SMateusz Guzik if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 3096ebb77b6SMateusz Guzik error = _sx_xlock_hard(sx, x, tid, opts, file, line); 3106ebb77b6SMateusz Guzik else 3116ebb77b6SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 3126ebb77b6SMateusz Guzik 0, 0, file, line, LOCKSTAT_WRITER); 313f9819486SAttilio Rao if (!error) { 314f9819486SAttilio Rao LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, 315f9819486SAttilio Rao file, line); 316aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 317ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 3186281b30aSJason Evans } 3196281b30aSJason Evans 320f9819486SAttilio Rao return (error); 321f9819486SAttilio Rao } 322f9819486SAttilio Rao 3235f36700aSJohn Baldwin int 3249fde98bbSAttilio Rao sx_try_xlock_(struct sx *sx, const char *file, int line) 3255f36700aSJohn Baldwin { 3265c5df0d9SMateusz Guzik struct thread *td; 3275c5df0d9SMateusz Guzik uintptr_t tid, x; 3284e7f640dSJohn Baldwin int rval; 3295c5df0d9SMateusz Guzik bool recursed; 3305f36700aSJohn Baldwin 3315c5df0d9SMateusz Guzik td = curthread; 3325c5df0d9SMateusz Guzik tid = (uintptr_t)td; 3335c5df0d9SMateusz Guzik if (SCHEDULER_STOPPED_TD(td)) 33435370593SAndriy Gapon return (1); 33535370593SAndriy Gapon 336704cb42fSMark Johnston KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td), 337e3ae0dfeSAttilio Rao ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d", 338e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 3390026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3400026c92cSJohn Baldwin ("sx_try_xlock() of destroyed sx @ %s:%d", file, line)); 3414e7f640dSJohn Baldwin 3425c5df0d9SMateusz Guzik rval = 1; 3435c5df0d9SMateusz Guzik recursed = false; 3445c5df0d9SMateusz Guzik x = SX_LOCK_UNLOCKED; 345b247fd39SMateusz Guzik for (;;) { 346b247fd39SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 347b247fd39SMateusz Guzik break; 348b247fd39SMateusz Guzik if (x == SX_LOCK_UNLOCKED) 349b247fd39SMateusz Guzik continue; 3505c5df0d9SMateusz Guzik if (x == tid && (sx->lock_object.lo_flags & LO_RECURSABLE)) { 3514e7f640dSJohn Baldwin sx->sx_recurse++; 3524e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 353b247fd39SMateusz Guzik break; 3545c5df0d9SMateusz Guzik } 355b247fd39SMateusz Guzik rval = 0; 356b247fd39SMateusz Guzik break; 3575c5df0d9SMateusz Guzik } 3585c5df0d9SMateusz Guzik 3594e7f640dSJohn Baldwin LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line); 3604e7f640dSJohn Baldwin if (rval) { 3614e7f640dSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 3624e7f640dSJohn Baldwin file, line); 3635c5df0d9SMateusz Guzik if (!recursed) 364de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, 365de2c95ccSMark Johnston sx, 0, 0, file, line, LOCKSTAT_WRITER); 366ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 3675f36700aSJohn Baldwin } 3684e7f640dSJohn Baldwin 3694e7f640dSJohn Baldwin return (rval); 3705f36700aSJohn Baldwin } 3715f36700aSJohn Baldwin 3726281b30aSJason Evans void 37319284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line) 3746281b30aSJason Evans { 3756281b30aSJason Evans 3760026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3770026c92cSJohn Baldwin ("sx_xunlock() of destroyed sx @ %s:%d", file, line)); 3787ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED, file, line); 379aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 3804e7f640dSJohn Baldwin LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file, 3814e7f640dSJohn Baldwin line); 3820108a980SMateusz Guzik #if LOCK_DEBUG > 0 3836ebb77b6SMateusz Guzik _sx_xunlock_hard(sx, (uintptr_t)curthread, file, line); 384ffd5c94cSMateusz Guzik #else 385ffd5c94cSMateusz Guzik __sx_xunlock(sx, curthread, file, line); 386ffd5c94cSMateusz Guzik #endif 387ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 3886281b30aSJason Evans } 389d55229b7SJason Evans 3904e7f640dSJohn Baldwin /* 3914e7f640dSJohn Baldwin * Try to do a non-blocking upgrade from a shared lock to an exclusive lock. 3924e7f640dSJohn Baldwin * This will only succeed if this thread holds a single shared lock. 3934e7f640dSJohn Baldwin * Return 1 if if the upgrade succeed, 0 otherwise. 3944e7f640dSJohn Baldwin */ 395d55229b7SJason Evans int 3969fde98bbSAttilio Rao sx_try_upgrade_(struct sx *sx, const char *file, int line) 397d55229b7SJason Evans { 3984e7f640dSJohn Baldwin uintptr_t x; 3994e7f640dSJohn Baldwin int success; 400d55229b7SJason Evans 40135370593SAndriy Gapon if (SCHEDULER_STOPPED()) 40235370593SAndriy Gapon return (1); 40335370593SAndriy Gapon 4040026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4050026c92cSJohn Baldwin ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 4067ec137e5SJohn Baldwin _sx_assert(sx, SA_SLOCKED, file, line); 407d55229b7SJason Evans 4084e7f640dSJohn Baldwin /* 4094e7f640dSJohn Baldwin * Try to switch from one shared lock to an exclusive lock. We need 4104e7f640dSJohn Baldwin * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 4114e7f640dSJohn Baldwin * we will wake up the exclusive waiters when we drop the lock. 4124e7f640dSJohn Baldwin */ 4134e7f640dSJohn Baldwin x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS; 414a2101806SMateusz Guzik success = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, 4154e7f640dSJohn Baldwin (uintptr_t)curthread | x); 4164e7f640dSJohn Baldwin LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 417a5aedd68SStacey Son if (success) { 418aa89d8cdSJohn Baldwin WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 419b0b7cb50SJohn Baldwin file, line); 42032cd0147SMark Johnston LOCKSTAT_RECORD0(sx__upgrade, sx); 421a5aedd68SStacey Son } 4224e7f640dSJohn Baldwin return (success); 423d55229b7SJason Evans } 424d55229b7SJason Evans 4254e7f640dSJohn Baldwin /* 4264e7f640dSJohn Baldwin * Downgrade an unrecursed exclusive lock into a single shared lock. 4274e7f640dSJohn Baldwin */ 428d55229b7SJason Evans void 4299fde98bbSAttilio Rao sx_downgrade_(struct sx *sx, const char *file, int line) 430d55229b7SJason Evans { 4314e7f640dSJohn Baldwin uintptr_t x; 432da7bbd2cSJohn Baldwin int wakeup_swapper; 433d55229b7SJason Evans 43435370593SAndriy Gapon if (SCHEDULER_STOPPED()) 43535370593SAndriy Gapon return; 43635370593SAndriy Gapon 4370026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4380026c92cSJohn Baldwin ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 4397ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 4404e7f640dSJohn Baldwin #ifndef INVARIANTS 4414e7f640dSJohn Baldwin if (sx_recursed(sx)) 4424e7f640dSJohn Baldwin panic("downgrade of a recursed lock"); 4434e7f640dSJohn Baldwin #endif 444d55229b7SJason Evans 445aa89d8cdSJohn Baldwin WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 446d55229b7SJason Evans 4474e7f640dSJohn Baldwin /* 4484e7f640dSJohn Baldwin * Try to switch from an exclusive lock with no shared waiters 4494e7f640dSJohn Baldwin * to one sharer with no shared waiters. If there are 4504e7f640dSJohn Baldwin * exclusive waiters, we don't need to lock the sleep queue so 4514e7f640dSJohn Baldwin * long as we preserve the flag. We do one quick try and if 4524e7f640dSJohn Baldwin * that fails we grab the sleepq lock to keep the flags from 4534e7f640dSJohn Baldwin * changing and do it the slow way. 4544e7f640dSJohn Baldwin * 4554e7f640dSJohn Baldwin * We have to lock the sleep queue if there are shared waiters 4564e7f640dSJohn Baldwin * so we can wake them up. 4574e7f640dSJohn Baldwin */ 4584e7f640dSJohn Baldwin x = sx->sx_lock; 4594e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS) && 4604e7f640dSJohn Baldwin atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 4614e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS))) { 4624e7f640dSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 4634e7f640dSJohn Baldwin return; 4644e7f640dSJohn Baldwin } 4654e7f640dSJohn Baldwin 4664e7f640dSJohn Baldwin /* 4674e7f640dSJohn Baldwin * Lock the sleep queue so we can read the waiters bits 4684e7f640dSJohn Baldwin * without any races and wakeup any shared waiters. 4694e7f640dSJohn Baldwin */ 4704e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 4714e7f640dSJohn Baldwin 4724e7f640dSJohn Baldwin /* 4734e7f640dSJohn Baldwin * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 4744e7f640dSJohn Baldwin * shared lock. If there are any shared waiters, wake them up. 4754e7f640dSJohn Baldwin */ 476da7bbd2cSJohn Baldwin wakeup_swapper = 0; 4774e7f640dSJohn Baldwin x = sx->sx_lock; 4784e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 4794e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS)); 4804e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED_WAITERS) 481da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 482da7bbd2cSJohn Baldwin 0, SQ_SHARED_QUEUE); 4834e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 484d55229b7SJason Evans 485aa89d8cdSJohn Baldwin LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 48632cd0147SMark Johnston LOCKSTAT_RECORD0(sx__downgrade, sx); 487da7bbd2cSJohn Baldwin 488da7bbd2cSJohn Baldwin if (wakeup_swapper) 489da7bbd2cSJohn Baldwin kick_proc0(); 4904e7f640dSJohn Baldwin } 491d55229b7SJason Evans 4924e7f640dSJohn Baldwin /* 4934e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xlock 4944e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 4954e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 4964e7f640dSJohn Baldwin * accessible from at least sx.h. 4974e7f640dSJohn Baldwin */ 498f9819486SAttilio Rao int 499fa474043SMateusz Guzik _sx_xlock_hard(struct sx *sx, uintptr_t x, uintptr_t tid, int opts, 500fa474043SMateusz Guzik const char *file, int line) 5014e7f640dSJohn Baldwin { 5024e7f640dSJohn Baldwin GIANT_DECLARE; 5034e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5044e7f640dSJohn Baldwin volatile struct thread *owner; 5051ae1c2a3SAttilio Rao u_int i, spintries = 0; 5064e7f640dSJohn Baldwin #endif 5071723a064SJeff Roberson #ifdef LOCK_PROFILING 5081723a064SJeff Roberson uint64_t waittime = 0; 5091723a064SJeff Roberson int contested = 0; 5101723a064SJeff Roberson #endif 5111723a064SJeff Roberson int error = 0; 51204126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 5131ada9041SMateusz Guzik struct lock_delay_arg lda; 5141ada9041SMateusz Guzik #endif 515a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 516076dd8ebSAndriy Gapon uintptr_t state; 51761852185SMateusz Guzik u_int sleep_cnt = 0; 518a5aedd68SStacey Son int64_t sleep_time = 0; 519076dd8ebSAndriy Gapon int64_t all_time = 0; 520a5aedd68SStacey Son #endif 5214e7f640dSJohn Baldwin 52235370593SAndriy Gapon if (SCHEDULER_STOPPED()) 52335370593SAndriy Gapon return (0); 52435370593SAndriy Gapon 525fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 5261ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 527fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 528fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 5291ada9041SMateusz Guzik #endif 5301ada9041SMateusz Guzik 531c1aaf63cSMateusz Guzik if (__predict_false(x == SX_LOCK_UNLOCKED)) 532c1aaf63cSMateusz Guzik x = SX_READ_VALUE(sx); 533c1aaf63cSMateusz Guzik 5344e7f640dSJohn Baldwin /* If we already hold an exclusive lock, then recurse. */ 535c5f61e6fSMateusz Guzik if (__predict_false(lv_sx_owner(x) == (struct thread *)tid)) { 536f0830182SAttilio Rao KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0, 537b0d67325SJohn Baldwin ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 538b0d67325SJohn Baldwin sx->lock_object.lo_name, file, line)); 5394e7f640dSJohn Baldwin sx->sx_recurse++; 5404e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 5414e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5424e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 543f9819486SAttilio Rao return (0); 5444e7f640dSJohn Baldwin } 5454e7f640dSJohn Baldwin 5464e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5474e7f640dSJohn Baldwin CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 5484e7f640dSJohn Baldwin sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 5494e7f640dSJohn Baldwin 550076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 551e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 552c5f61e6fSMateusz Guzik state = x; 553076dd8ebSAndriy Gapon #endif 554fc4f686dSMateusz Guzik for (;;) { 555c5f61e6fSMateusz Guzik if (x == SX_LOCK_UNLOCKED) { 556fa474043SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 557fc4f686dSMateusz Guzik break; 558c5f61e6fSMateusz Guzik continue; 559c5f61e6fSMateusz Guzik } 560a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 5611ada9041SMateusz Guzik lda.spin_cnt++; 562a5aedd68SStacey Son #endif 563f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 564f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 565f5f9340bSFabien Thomas #endif 566eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 567eea4f254SJeff Roberson &waittime); 5684e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5694e7f640dSJohn Baldwin /* 5704e7f640dSJohn Baldwin * If the lock is write locked and the owner is 5714e7f640dSJohn Baldwin * running on another CPU, spin until the owner stops 5724e7f640dSJohn Baldwin * running or the state of the lock changes. 5734e7f640dSJohn Baldwin */ 5748545538bSJohn Baldwin if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 5751ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0) { 576c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 5774e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 5784e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5794e7f640dSJohn Baldwin CTR3(KTR_LOCK, 5804e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 5814e7f640dSJohn Baldwin __func__, sx, owner); 5822cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 5832cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 5842cba8dd3SJohn Baldwin "lockname:\"%s\"", 5852cba8dd3SJohn Baldwin sx->lock_object.lo_name); 5864e7f640dSJohn Baldwin GIANT_SAVE(); 587c5f61e6fSMateusz Guzik do { 5881ada9041SMateusz Guzik lock_delay(&lda); 589c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 590c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 591c5f61e6fSMateusz Guzik } while (owner != NULL && 592c5f61e6fSMateusz Guzik TD_IS_RUNNING(owner)); 5932cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 5942cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 5954e7f640dSJohn Baldwin continue; 5964e7f640dSJohn Baldwin } 5971ae1c2a3SAttilio Rao } else if (SX_SHARERS(x) && spintries < asx_retries) { 5982cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 5992cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 6002cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 6018d3635c4SAttilio Rao GIANT_SAVE(); 6021ae1c2a3SAttilio Rao spintries++; 6031ae1c2a3SAttilio Rao for (i = 0; i < asx_loops; i++) { 6041ae1c2a3SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6051ae1c2a3SAttilio Rao CTR4(KTR_LOCK, 6061ae1c2a3SAttilio Rao "%s: shared spinning on %p with %u and %u", 6071ae1c2a3SAttilio Rao __func__, sx, spintries, i); 6081ae1c2a3SAttilio Rao x = sx->sx_lock; 6091ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0 || 6101ae1c2a3SAttilio Rao SX_SHARERS(x) == 0) 6111ae1c2a3SAttilio Rao break; 6121ae1c2a3SAttilio Rao cpu_spinwait(); 6131ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS 6141ada9041SMateusz Guzik lda.spin_cnt++; 6151ae1c2a3SAttilio Rao #endif 6161ae1c2a3SAttilio Rao } 6172cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 6182cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 619c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6201ae1c2a3SAttilio Rao if (i != asx_loops) 6211ae1c2a3SAttilio Rao continue; 6221ae1c2a3SAttilio Rao } 6234e7f640dSJohn Baldwin } 6244e7f640dSJohn Baldwin #endif 6254e7f640dSJohn Baldwin 6264e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 627c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6284e7f640dSJohn Baldwin 6294e7f640dSJohn Baldwin /* 6304e7f640dSJohn Baldwin * If the lock was released while spinning on the 6314e7f640dSJohn Baldwin * sleep queue chain lock, try again. 6324e7f640dSJohn Baldwin */ 6334e7f640dSJohn Baldwin if (x == SX_LOCK_UNLOCKED) { 6344e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6354e7f640dSJohn Baldwin continue; 6364e7f640dSJohn Baldwin } 6374e7f640dSJohn Baldwin 6384e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 6394e7f640dSJohn Baldwin /* 6404e7f640dSJohn Baldwin * The current lock owner might have started executing 6414e7f640dSJohn Baldwin * on another CPU (or the lock could have changed 6424e7f640dSJohn Baldwin * owners) while we were waiting on the sleep queue 6434e7f640dSJohn Baldwin * chain lock. If so, drop the sleep queue lock and try 6444e7f640dSJohn Baldwin * again. 6454e7f640dSJohn Baldwin */ 6464e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 6471ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 6484e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 6494e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 6504e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6514e7f640dSJohn Baldwin continue; 6524e7f640dSJohn Baldwin } 6534e7f640dSJohn Baldwin } 6544e7f640dSJohn Baldwin #endif 6554e7f640dSJohn Baldwin 6564e7f640dSJohn Baldwin /* 6574e7f640dSJohn Baldwin * If an exclusive lock was released with both shared 6584e7f640dSJohn Baldwin * and exclusive waiters and a shared waiter hasn't 6594e7f640dSJohn Baldwin * woken up and acquired the lock yet, sx_lock will be 6604e7f640dSJohn Baldwin * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 6614e7f640dSJohn Baldwin * If we see that value, try to acquire it once. Note 6624e7f640dSJohn Baldwin * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 6634e7f640dSJohn Baldwin * as there are other exclusive waiters still. If we 6644e7f640dSJohn Baldwin * fail, restart the loop. 6654e7f640dSJohn Baldwin */ 6664e7f640dSJohn Baldwin if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 6674e7f640dSJohn Baldwin if (atomic_cmpset_acq_ptr(&sx->sx_lock, 6684e7f640dSJohn Baldwin SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS, 6694e7f640dSJohn Baldwin tid | SX_LOCK_EXCLUSIVE_WAITERS)) { 6704e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6714e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p claimed by new writer", 6724e7f640dSJohn Baldwin __func__, sx); 6734e7f640dSJohn Baldwin break; 6744e7f640dSJohn Baldwin } 6754e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 676c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6774e7f640dSJohn Baldwin continue; 6784e7f640dSJohn Baldwin } 6794e7f640dSJohn Baldwin 6804e7f640dSJohn Baldwin /* 6814e7f640dSJohn Baldwin * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 6824e7f640dSJohn Baldwin * than loop back and retry. 6834e7f640dSJohn Baldwin */ 6844e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 6854e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 6864e7f640dSJohn Baldwin x | SX_LOCK_EXCLUSIVE_WAITERS)) { 6874e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 688c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 6894e7f640dSJohn Baldwin continue; 6904e7f640dSJohn Baldwin } 6914e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6924e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 6934e7f640dSJohn Baldwin __func__, sx); 6944e7f640dSJohn Baldwin } 6954e7f640dSJohn Baldwin 6964e7f640dSJohn Baldwin /* 6974e7f640dSJohn Baldwin * Since we have been unable to acquire the exclusive 6984e7f640dSJohn Baldwin * lock and the exclusive waiters flag is set, we have 6994e7f640dSJohn Baldwin * to sleep. 7004e7f640dSJohn Baldwin */ 7014e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7024e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 7034e7f640dSJohn Baldwin __func__, sx); 7044e7f640dSJohn Baldwin 705a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 706e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 707a5aedd68SStacey Son #endif 7084e7f640dSJohn Baldwin GIANT_SAVE(); 7094e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 710f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 711f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 712f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 713c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 714f9819486SAttilio Rao else 715c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 716a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 717e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 718a5aedd68SStacey Son sleep_cnt++; 719a5aedd68SStacey Son #endif 720f9819486SAttilio Rao if (error) { 721f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 722f9819486SAttilio Rao CTR2(KTR_LOCK, 723f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 724f9819486SAttilio Rao __func__, sx); 725f9819486SAttilio Rao break; 726f9819486SAttilio Rao } 7274e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7284e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 7294e7f640dSJohn Baldwin __func__, sx); 730c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7314e7f640dSJohn Baldwin } 732076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 733e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 734076dd8ebSAndriy Gapon if (sleep_time) 73532cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 736076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 737076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 7381ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 73932cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 740076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 741076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 742076dd8ebSAndriy Gapon #endif 743f9819486SAttilio Rao if (!error) 744de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 745de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_WRITER); 746076dd8ebSAndriy Gapon GIANT_RESTORE(); 747f9819486SAttilio Rao return (error); 7484e7f640dSJohn Baldwin } 7494e7f640dSJohn Baldwin 7504e7f640dSJohn Baldwin /* 7514e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xunlock 7524e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 7534e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 7544e7f640dSJohn Baldwin * accessible from at least sx.h. 7554e7f640dSJohn Baldwin */ 7564e7f640dSJohn Baldwin void 7574e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line) 7584e7f640dSJohn Baldwin { 7594e7f640dSJohn Baldwin uintptr_t x; 760da7bbd2cSJohn Baldwin int queue, wakeup_swapper; 7614e7f640dSJohn Baldwin 76235370593SAndriy Gapon if (SCHEDULER_STOPPED()) 76335370593SAndriy Gapon return; 76435370593SAndriy Gapon 7654e7f640dSJohn Baldwin MPASS(!(sx->sx_lock & SX_LOCK_SHARED)); 7664e7f640dSJohn Baldwin 7673b3cf014SMateusz Guzik x = SX_READ_VALUE(sx); 7683b3cf014SMateusz Guzik if (x & SX_LOCK_RECURSED) { 7696ebb77b6SMateusz Guzik /* The lock is recursed, unrecurse one level. */ 7704e7f640dSJohn Baldwin if ((--sx->sx_recurse) == 0) 7714e7f640dSJohn Baldwin atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 7724e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7734e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 7744e7f640dSJohn Baldwin return; 7754e7f640dSJohn Baldwin } 7763b3cf014SMateusz Guzik 7773b3cf014SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_WRITER); 7783b3cf014SMateusz Guzik if (x == tid && 7793b3cf014SMateusz Guzik atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED)) 7803b3cf014SMateusz Guzik return; 7813b3cf014SMateusz Guzik 7824e7f640dSJohn Baldwin MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS | 7834e7f640dSJohn Baldwin SX_LOCK_EXCLUSIVE_WAITERS)); 7844e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7854e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 7864e7f640dSJohn Baldwin 7874e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 7884e7f640dSJohn Baldwin x = SX_LOCK_UNLOCKED; 7894e7f640dSJohn Baldwin 7904e7f640dSJohn Baldwin /* 7914e7f640dSJohn Baldwin * The wake up algorithm here is quite simple and probably not 7924e7f640dSJohn Baldwin * ideal. It gives precedence to shared waiters if they are 7934e7f640dSJohn Baldwin * present. For this condition, we have to preserve the 7944e7f640dSJohn Baldwin * state of the exclusive waiters flag. 7952028867dSAttilio Rao * If interruptible sleeps left the shared queue empty avoid a 7962028867dSAttilio Rao * starvation for the threads sleeping on the exclusive queue by giving 7972028867dSAttilio Rao * them precedence and cleaning up the shared waiters bit anyway. 7984e7f640dSJohn Baldwin */ 7992028867dSAttilio Rao if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 && 8002028867dSAttilio Rao sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) { 8014e7f640dSJohn Baldwin queue = SQ_SHARED_QUEUE; 8024e7f640dSJohn Baldwin x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS); 8034e7f640dSJohn Baldwin } else 8044e7f640dSJohn Baldwin queue = SQ_EXCLUSIVE_QUEUE; 8054e7f640dSJohn Baldwin 8064e7f640dSJohn Baldwin /* Wake up all the waiters for the specific queue. */ 8074e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8084e7f640dSJohn Baldwin CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 8094e7f640dSJohn Baldwin __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 8104e7f640dSJohn Baldwin "exclusive"); 8114e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, x); 812da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 813da7bbd2cSJohn Baldwin queue); 814c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 815da7bbd2cSJohn Baldwin if (wakeup_swapper) 816da7bbd2cSJohn Baldwin kick_proc0(); 8174e7f640dSJohn Baldwin } 8184e7f640dSJohn Baldwin 819834f70f3SMateusz Guzik static bool __always_inline 820834f70f3SMateusz Guzik __sx_slock_try(struct sx *sx, uintptr_t *xp, const char *file, int line) 821834f70f3SMateusz Guzik { 822834f70f3SMateusz Guzik 823834f70f3SMateusz Guzik /* 824834f70f3SMateusz Guzik * If no other thread has an exclusive lock then try to bump up 825834f70f3SMateusz Guzik * the count of sharers. Since we have to preserve the state 826834f70f3SMateusz Guzik * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 827834f70f3SMateusz Guzik * shared lock loop back and retry. 828834f70f3SMateusz Guzik */ 829834f70f3SMateusz Guzik while (*xp & SX_LOCK_SHARED) { 830834f70f3SMateusz Guzik MPASS(!(*xp & SX_LOCK_SHARED_WAITERS)); 831834f70f3SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, xp, 832834f70f3SMateusz Guzik *xp + SX_ONE_SHARER)) { 833834f70f3SMateusz Guzik if (LOCK_LOG_TEST(&sx->lock_object, 0)) 834834f70f3SMateusz Guzik CTR4(KTR_LOCK, "%s: %p succeed %p -> %p", 835834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 836834f70f3SMateusz Guzik (void *)(*xp + SX_ONE_SHARER)); 837834f70f3SMateusz Guzik return (true); 838834f70f3SMateusz Guzik } 839834f70f3SMateusz Guzik } 840834f70f3SMateusz Guzik return (false); 841834f70f3SMateusz Guzik } 842834f70f3SMateusz Guzik 843834f70f3SMateusz Guzik static int __noinline 844834f70f3SMateusz Guzik _sx_slock_hard(struct sx *sx, int opts, const char *file, int line, uintptr_t x) 8454e7f640dSJohn Baldwin { 8464e7f640dSJohn Baldwin GIANT_DECLARE; 8474e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 8484e7f640dSJohn Baldwin volatile struct thread *owner; 8494e7f640dSJohn Baldwin #endif 8501723a064SJeff Roberson #ifdef LOCK_PROFILING 851c1a6d9faSAttilio Rao uint64_t waittime = 0; 852c1a6d9faSAttilio Rao int contested = 0; 8531723a064SJeff Roberson #endif 854c1a6d9faSAttilio Rao int error = 0; 85504126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 8561ada9041SMateusz Guzik struct lock_delay_arg lda; 8571ada9041SMateusz Guzik #endif 858a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 859076dd8ebSAndriy Gapon uintptr_t state; 86061852185SMateusz Guzik u_int sleep_cnt = 0; 861a5aedd68SStacey Son int64_t sleep_time = 0; 862076dd8ebSAndriy Gapon int64_t all_time = 0; 863a5aedd68SStacey Son #endif 864c1a6d9faSAttilio Rao 86535370593SAndriy Gapon if (SCHEDULER_STOPPED()) 86635370593SAndriy Gapon return (0); 86735370593SAndriy Gapon 868fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 8691ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 870fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 871fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 8721ada9041SMateusz Guzik #endif 873076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 874e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 875c5f61e6fSMateusz Guzik state = x; 876c5f61e6fSMateusz Guzik #endif 877076dd8ebSAndriy Gapon 8784e7f640dSJohn Baldwin /* 8794e7f640dSJohn Baldwin * As with rwlocks, we don't make any attempt to try to block 8804e7f640dSJohn Baldwin * shared locks once there is an exclusive waiter. 8814e7f640dSJohn Baldwin */ 8824e7f640dSJohn Baldwin for (;;) { 883834f70f3SMateusz Guzik if (__sx_slock_try(sx, &x, file, line)) 8844e7f640dSJohn Baldwin break; 885c5f61e6fSMateusz Guzik #ifdef KDTRACE_HOOKS 886c5f61e6fSMateusz Guzik lda.spin_cnt++; 887c5f61e6fSMateusz Guzik #endif 888c5f61e6fSMateusz Guzik 889f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 890f5f9340bSFabien Thomas PMC_SOFT_CALL( , , lock, failed); 891f5f9340bSFabien Thomas #endif 892eea4f254SJeff Roberson lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 893eea4f254SJeff Roberson &waittime); 8944e7f640dSJohn Baldwin 8954e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 8964e7f640dSJohn Baldwin /* 8974e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 8984e7f640dSJohn Baldwin * the owner stops running or the state of the lock 8994e7f640dSJohn Baldwin * changes. 9004e7f640dSJohn Baldwin */ 9011ae1c2a3SAttilio Rao if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 902c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 9034e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 9044e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9054e7f640dSJohn Baldwin CTR3(KTR_LOCK, 9064e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 9074e7f640dSJohn Baldwin __func__, sx, owner); 9082cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 9092cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 9102cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 9114e7f640dSJohn Baldwin GIANT_SAVE(); 912c5f61e6fSMateusz Guzik do { 9131ada9041SMateusz Guzik lock_delay(&lda); 914c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 915c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 916c5f61e6fSMateusz Guzik } while (owner != NULL && TD_IS_RUNNING(owner)); 9172cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 9182cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 9194e7f640dSJohn Baldwin continue; 9204e7f640dSJohn Baldwin } 9214e7f640dSJohn Baldwin } 9224e7f640dSJohn Baldwin #endif 9234e7f640dSJohn Baldwin 9244e7f640dSJohn Baldwin /* 9254e7f640dSJohn Baldwin * Some other thread already has an exclusive lock, so 9264e7f640dSJohn Baldwin * start the process of blocking. 9274e7f640dSJohn Baldwin */ 9284e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 929c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 9304e7f640dSJohn Baldwin 9314e7f640dSJohn Baldwin /* 9324e7f640dSJohn Baldwin * The lock could have been released while we spun. 9334e7f640dSJohn Baldwin * In this case loop back and retry. 9344e7f640dSJohn Baldwin */ 9354e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 9364e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 9374e7f640dSJohn Baldwin continue; 9384e7f640dSJohn Baldwin } 9394e7f640dSJohn Baldwin 9404e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 9414e7f640dSJohn Baldwin /* 9424e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 9434e7f640dSJohn Baldwin * the owner stops running or the state of the lock 9444e7f640dSJohn Baldwin * changes. 9454e7f640dSJohn Baldwin */ 9464e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED) && 9471ae1c2a3SAttilio Rao (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) { 9484e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 9494e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 9504e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 951c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 9524e7f640dSJohn Baldwin continue; 9534e7f640dSJohn Baldwin } 9544e7f640dSJohn Baldwin } 9554e7f640dSJohn Baldwin #endif 9564e7f640dSJohn Baldwin 9574e7f640dSJohn Baldwin /* 9584e7f640dSJohn Baldwin * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 9594e7f640dSJohn Baldwin * fail to set it drop the sleep queue lock and loop 9604e7f640dSJohn Baldwin * back. 9614e7f640dSJohn Baldwin */ 9624e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS)) { 9634e7f640dSJohn Baldwin if (!atomic_cmpset_ptr(&sx->sx_lock, x, 9644e7f640dSJohn Baldwin x | SX_LOCK_SHARED_WAITERS)) { 9654e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 966c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 9674e7f640dSJohn Baldwin continue; 9684e7f640dSJohn Baldwin } 9694e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9704e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 9714e7f640dSJohn Baldwin __func__, sx); 9724e7f640dSJohn Baldwin } 9734e7f640dSJohn Baldwin 9744e7f640dSJohn Baldwin /* 9754e7f640dSJohn Baldwin * Since we have been unable to acquire the shared lock, 9764e7f640dSJohn Baldwin * we have to sleep. 9774e7f640dSJohn Baldwin */ 9784e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9794e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 9804e7f640dSJohn Baldwin __func__, sx); 9814e7f640dSJohn Baldwin 982a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 983e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 984a5aedd68SStacey Son #endif 9854e7f640dSJohn Baldwin GIANT_SAVE(); 9864e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 987f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 988f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 989f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 990c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 991f9819486SAttilio Rao else 992c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 993a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 994e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 995a5aedd68SStacey Son sleep_cnt++; 996a5aedd68SStacey Son #endif 997f9819486SAttilio Rao if (error) { 998f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 999f9819486SAttilio Rao CTR2(KTR_LOCK, 1000f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 1001f9819486SAttilio Rao __func__, sx); 1002f9819486SAttilio Rao break; 1003f9819486SAttilio Rao } 10044e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10054e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 10064e7f640dSJohn Baldwin __func__, sx); 1007c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10084e7f640dSJohn Baldwin } 1009076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 1010e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 1011076dd8ebSAndriy Gapon if (sleep_time) 101232cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 1013076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1014076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 10151ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 101632cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 1017076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1018076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 1019076dd8ebSAndriy Gapon #endif 10203ae56ce9SMateusz Guzik if (error == 0) { 1021de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 1022de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_READER); 10233ae56ce9SMateusz Guzik } 10244e7f640dSJohn Baldwin GIANT_RESTORE(); 1025f9819486SAttilio Rao return (error); 10264e7f640dSJohn Baldwin } 10274e7f640dSJohn Baldwin 1028834f70f3SMateusz Guzik int 1029834f70f3SMateusz Guzik _sx_slock(struct sx *sx, int opts, const char *file, int line) 10304e7f640dSJohn Baldwin { 10314e7f640dSJohn Baldwin uintptr_t x; 1032834f70f3SMateusz Guzik int error; 10334e7f640dSJohn Baldwin 1034704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 1035704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 1036834f70f3SMateusz Guzik ("sx_slock() by idle thread %p on sx %s @ %s:%d", 1037834f70f3SMateusz Guzik curthread, sx->lock_object.lo_name, file, line)); 10383ae56ce9SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1039834f70f3SMateusz Guzik ("sx_slock() of destroyed sx @ %s:%d", file, line)); 1040834f70f3SMateusz Guzik WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 1041834f70f3SMateusz Guzik 1042834f70f3SMateusz Guzik error = 0; 1043c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 1044834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__acquire) || 1045834f70f3SMateusz Guzik !__sx_slock_try(sx, &x, file, line))) 1046834f70f3SMateusz Guzik error = _sx_slock_hard(sx, opts, file, line, x); 1047834f70f3SMateusz Guzik if (error == 0) { 1048834f70f3SMateusz Guzik LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 1049834f70f3SMateusz Guzik WITNESS_LOCK(&sx->lock_object, 0, file, line); 1050834f70f3SMateusz Guzik TD_LOCKS_INC(curthread); 1051834f70f3SMateusz Guzik } 1052834f70f3SMateusz Guzik return (error); 1053834f70f3SMateusz Guzik } 1054834f70f3SMateusz Guzik 1055834f70f3SMateusz Guzik static bool __always_inline 1056834f70f3SMateusz Guzik _sx_sunlock_try(struct sx *sx, uintptr_t *xp) 1057834f70f3SMateusz Guzik { 1058834f70f3SMateusz Guzik 10594e7f640dSJohn Baldwin for (;;) { 10604e7f640dSJohn Baldwin /* 10614e7f640dSJohn Baldwin * We should never have sharers while at least one thread 10624e7f640dSJohn Baldwin * holds a shared lock. 10634e7f640dSJohn Baldwin */ 1064834f70f3SMateusz Guzik KASSERT(!(*xp & SX_LOCK_SHARED_WAITERS), 10654e7f640dSJohn Baldwin ("%s: waiting sharers", __func__)); 10664e7f640dSJohn Baldwin 10674e7f640dSJohn Baldwin /* 10684e7f640dSJohn Baldwin * See if there is more than one shared lock held. If 10694e7f640dSJohn Baldwin * so, just drop one and return. 10704e7f640dSJohn Baldwin */ 1071834f70f3SMateusz Guzik if (SX_SHARERS(*xp) > 1) { 1072834f70f3SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, xp, 1073834f70f3SMateusz Guzik *xp - SX_ONE_SHARER)) { 10744e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10754e7f640dSJohn Baldwin CTR4(KTR_LOCK, 10764e7f640dSJohn Baldwin "%s: %p succeeded %p -> %p", 1077834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 1078834f70f3SMateusz Guzik (void *)(*xp - SX_ONE_SHARER)); 1079834f70f3SMateusz Guzik return (true); 10804e7f640dSJohn Baldwin } 10814e7f640dSJohn Baldwin continue; 10824e7f640dSJohn Baldwin } 10834e7f640dSJohn Baldwin 10844e7f640dSJohn Baldwin /* 10854e7f640dSJohn Baldwin * If there aren't any waiters for an exclusive lock, 10864e7f640dSJohn Baldwin * then try to drop it quickly. 10874e7f640dSJohn Baldwin */ 1088834f70f3SMateusz Guzik if (!(*xp & SX_LOCK_EXCLUSIVE_WAITERS)) { 1089834f70f3SMateusz Guzik MPASS(*xp == SX_SHARERS_LOCK(1)); 1090834f70f3SMateusz Guzik *xp = SX_SHARERS_LOCK(1); 1091fa474043SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, 1092834f70f3SMateusz Guzik xp, SX_LOCK_UNLOCKED)) { 10934e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10944e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p last succeeded", 10954e7f640dSJohn Baldwin __func__, sx); 1096834f70f3SMateusz Guzik return (true); 10974e7f640dSJohn Baldwin } 10984e7f640dSJohn Baldwin continue; 10994e7f640dSJohn Baldwin } 1100834f70f3SMateusz Guzik break; 1101834f70f3SMateusz Guzik } 1102834f70f3SMateusz Guzik return (false); 1103834f70f3SMateusz Guzik } 1104834f70f3SMateusz Guzik 1105834f70f3SMateusz Guzik static void __noinline 1106834f70f3SMateusz Guzik _sx_sunlock_hard(struct sx *sx, uintptr_t x, const char *file, int line) 1107834f70f3SMateusz Guzik { 1108834f70f3SMateusz Guzik int wakeup_swapper; 1109834f70f3SMateusz Guzik 1110834f70f3SMateusz Guzik if (SCHEDULER_STOPPED()) 1111834f70f3SMateusz Guzik return; 1112834f70f3SMateusz Guzik 1113834f70f3SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER); 1114834f70f3SMateusz Guzik 1115834f70f3SMateusz Guzik for (;;) { 1116834f70f3SMateusz Guzik if (_sx_sunlock_try(sx, &x)) 1117834f70f3SMateusz Guzik break; 11184e7f640dSJohn Baldwin 11194e7f640dSJohn Baldwin /* 11204e7f640dSJohn Baldwin * At this point, there should just be one sharer with 11214e7f640dSJohn Baldwin * exclusive waiters. 11224e7f640dSJohn Baldwin */ 11234e7f640dSJohn Baldwin MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS)); 11244e7f640dSJohn Baldwin 11254e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 11264e7f640dSJohn Baldwin 11274e7f640dSJohn Baldwin /* 11284e7f640dSJohn Baldwin * Wake up semantic here is quite simple: 11294e7f640dSJohn Baldwin * Just wake up all the exclusive waiters. 11304e7f640dSJohn Baldwin * Note that the state of the lock could have changed, 11314e7f640dSJohn Baldwin * so if it fails loop back and retry. 11324e7f640dSJohn Baldwin */ 1133ddce63caSAttilio Rao if (!atomic_cmpset_rel_ptr(&sx->sx_lock, 11344e7f640dSJohn Baldwin SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS, 11354e7f640dSJohn Baldwin SX_LOCK_UNLOCKED)) { 11364e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 1137c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 11384e7f640dSJohn Baldwin continue; 11394e7f640dSJohn Baldwin } 11404e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 11414e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p waking up all thread on" 11424e7f640dSJohn Baldwin "exclusive queue", __func__, sx); 1143da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 1144da7bbd2cSJohn Baldwin 0, SQ_EXCLUSIVE_QUEUE); 1145c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 1146da7bbd2cSJohn Baldwin if (wakeup_swapper) 1147da7bbd2cSJohn Baldwin kick_proc0(); 11484e7f640dSJohn Baldwin break; 11494e7f640dSJohn Baldwin } 1150834f70f3SMateusz Guzik } 1151834f70f3SMateusz Guzik 1152834f70f3SMateusz Guzik void 1153834f70f3SMateusz Guzik _sx_sunlock(struct sx *sx, const char *file, int line) 1154834f70f3SMateusz Guzik { 1155834f70f3SMateusz Guzik uintptr_t x; 1156834f70f3SMateusz Guzik 1157834f70f3SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1158834f70f3SMateusz Guzik ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 1159834f70f3SMateusz Guzik _sx_assert(sx, SA_SLOCKED, file, line); 1160834f70f3SMateusz Guzik WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 1161834f70f3SMateusz Guzik LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 1162834f70f3SMateusz Guzik 1163834f70f3SMateusz Guzik x = SX_READ_VALUE(sx); 1164834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__release) || 1165834f70f3SMateusz Guzik !_sx_sunlock_try(sx, &x))) 1166834f70f3SMateusz Guzik _sx_sunlock_hard(sx, x, file, line); 1167834f70f3SMateusz Guzik 11683ae56ce9SMateusz Guzik TD_LOCKS_DEC(curthread); 1169d55229b7SJason Evans } 11704e5e677bSJohn Baldwin 11714e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT 1172781a35dfSJohn Baldwin #ifndef INVARIANTS 1173781a35dfSJohn Baldwin #undef _sx_assert 1174781a35dfSJohn Baldwin #endif 1175781a35dfSJohn Baldwin 11764e5e677bSJohn Baldwin /* 11774e5e677bSJohn Baldwin * In the non-WITNESS case, sx_assert() can only detect that at least 11784e5e677bSJohn Baldwin * *some* thread owns an slock, but it cannot guarantee that *this* 11794e5e677bSJohn Baldwin * thread owns an slock. 11804e5e677bSJohn Baldwin */ 11814e5e677bSJohn Baldwin void 1182d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line) 11834e5e677bSJohn Baldwin { 11844e7f640dSJohn Baldwin #ifndef WITNESS 11854e7f640dSJohn Baldwin int slocked = 0; 11864e7f640dSJohn Baldwin #endif 11874e5e677bSJohn Baldwin 118803129ba9SJohn Baldwin if (panicstr != NULL) 118903129ba9SJohn Baldwin return; 11904e5e677bSJohn Baldwin switch (what) { 11917ec137e5SJohn Baldwin case SA_SLOCKED: 11927ec137e5SJohn Baldwin case SA_SLOCKED | SA_NOTRECURSED: 11937ec137e5SJohn Baldwin case SA_SLOCKED | SA_RECURSED: 11944e7f640dSJohn Baldwin #ifndef WITNESS 11954e7f640dSJohn Baldwin slocked = 1; 11964e7f640dSJohn Baldwin /* FALLTHROUGH */ 11974e7f640dSJohn Baldwin #endif 11987ec137e5SJohn Baldwin case SA_LOCKED: 11997ec137e5SJohn Baldwin case SA_LOCKED | SA_NOTRECURSED: 12007ec137e5SJohn Baldwin case SA_LOCKED | SA_RECURSED: 12014e5e677bSJohn Baldwin #ifdef WITNESS 1202aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 12034e5e677bSJohn Baldwin #else 12044e7f640dSJohn Baldwin /* 12054e7f640dSJohn Baldwin * If some other thread has an exclusive lock or we 12064e7f640dSJohn Baldwin * have one and are asserting a shared lock, fail. 12074e7f640dSJohn Baldwin * Also, if no one has a lock at all, fail. 12084e7f640dSJohn Baldwin */ 12094e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED || 12104e7f640dSJohn Baldwin (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 12114e7f640dSJohn Baldwin sx_xholder(sx) != curthread))) 121203129ba9SJohn Baldwin panic("Lock %s not %slocked @ %s:%d\n", 12134e7f640dSJohn Baldwin sx->lock_object.lo_name, slocked ? "share " : "", 12144e7f640dSJohn Baldwin file, line); 12154e7f640dSJohn Baldwin 12164e7f640dSJohn Baldwin if (!(sx->sx_lock & SX_LOCK_SHARED)) { 12174e7f640dSJohn Baldwin if (sx_recursed(sx)) { 12187ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 12194e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 12204e7f640dSJohn Baldwin sx->lock_object.lo_name, file, 12214e7f640dSJohn Baldwin line); 12227ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 12234e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 12244e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 12254e7f640dSJohn Baldwin } 12264e5e677bSJohn Baldwin #endif 12274e5e677bSJohn Baldwin break; 12287ec137e5SJohn Baldwin case SA_XLOCKED: 12297ec137e5SJohn Baldwin case SA_XLOCKED | SA_NOTRECURSED: 12307ec137e5SJohn Baldwin case SA_XLOCKED | SA_RECURSED: 12314e7f640dSJohn Baldwin if (sx_xholder(sx) != curthread) 123203129ba9SJohn Baldwin panic("Lock %s not exclusively locked @ %s:%d\n", 1233aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 12344e7f640dSJohn Baldwin if (sx_recursed(sx)) { 12357ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 12364e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 12374e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 12387ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 12394e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 12404e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 12414e5e677bSJohn Baldwin break; 12427ec137e5SJohn Baldwin case SA_UNLOCKED: 124319b0efd3SPawel Jakub Dawidek #ifdef WITNESS 1244aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 124519b0efd3SPawel Jakub Dawidek #else 1246f6739b1dSPawel Jakub Dawidek /* 12474e7f640dSJohn Baldwin * If we hold an exclusve lock fail. We can't 12484e7f640dSJohn Baldwin * reliably check to see if we hold a shared lock or 12494e7f640dSJohn Baldwin * not. 1250f6739b1dSPawel Jakub Dawidek */ 12514e7f640dSJohn Baldwin if (sx_xholder(sx) == curthread) 125203129ba9SJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 1253aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 125419b0efd3SPawel Jakub Dawidek #endif 125519b0efd3SPawel Jakub Dawidek break; 12564e5e677bSJohn Baldwin default: 12574e5e677bSJohn Baldwin panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 12584e5e677bSJohn Baldwin line); 12594e5e677bSJohn Baldwin } 12604e5e677bSJohn Baldwin } 12614e5e677bSJohn Baldwin #endif /* INVARIANT_SUPPORT */ 1262d272fe53SJohn Baldwin 1263d272fe53SJohn Baldwin #ifdef DDB 12644e7f640dSJohn Baldwin static void 1265d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock) 1266d272fe53SJohn Baldwin { 1267d272fe53SJohn Baldwin struct thread *td; 1268d576deedSPawel Jakub Dawidek const struct sx *sx; 1269d272fe53SJohn Baldwin 1270d576deedSPawel Jakub Dawidek sx = (const struct sx *)lock; 1271d272fe53SJohn Baldwin 1272d272fe53SJohn Baldwin db_printf(" state: "); 12734e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED) 12744e7f640dSJohn Baldwin db_printf("UNLOCKED\n"); 12750026c92cSJohn Baldwin else if (sx->sx_lock == SX_LOCK_DESTROYED) { 12760026c92cSJohn Baldwin db_printf("DESTROYED\n"); 12770026c92cSJohn Baldwin return; 12780026c92cSJohn Baldwin } else if (sx->sx_lock & SX_LOCK_SHARED) 12794e7f640dSJohn Baldwin db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 12804e7f640dSJohn Baldwin else { 12814e7f640dSJohn Baldwin td = sx_xholder(sx); 1282d272fe53SJohn Baldwin db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1283431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 12844e7f640dSJohn Baldwin if (sx_recursed(sx)) 12854e7f640dSJohn Baldwin db_printf(" recursed: %d\n", sx->sx_recurse); 12864e7f640dSJohn Baldwin } 12874e7f640dSJohn Baldwin 12884e7f640dSJohn Baldwin db_printf(" waiters: "); 12894e7f640dSJohn Baldwin switch(sx->sx_lock & 12904e7f640dSJohn Baldwin (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 12914e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS: 12924e7f640dSJohn Baldwin db_printf("shared\n"); 12934e7f640dSJohn Baldwin break; 12944e7f640dSJohn Baldwin case SX_LOCK_EXCLUSIVE_WAITERS: 12954e7f640dSJohn Baldwin db_printf("exclusive\n"); 12964e7f640dSJohn Baldwin break; 12974e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 12984e7f640dSJohn Baldwin db_printf("exclusive and shared\n"); 12994e7f640dSJohn Baldwin break; 13004e7f640dSJohn Baldwin default: 13014e7f640dSJohn Baldwin db_printf("none\n"); 13024e7f640dSJohn Baldwin } 1303d272fe53SJohn Baldwin } 1304462a7addSJohn Baldwin 1305462a7addSJohn Baldwin /* 1306462a7addSJohn Baldwin * Check to see if a thread that is blocked on a sleep queue is actually 1307462a7addSJohn Baldwin * blocked on an sx lock. If so, output some details and return true. 1308462a7addSJohn Baldwin * If the lock has an exclusive owner, return that in *ownerp. 1309462a7addSJohn Baldwin */ 1310462a7addSJohn Baldwin int 1311462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp) 1312462a7addSJohn Baldwin { 1313462a7addSJohn Baldwin struct sx *sx; 1314462a7addSJohn Baldwin 1315462a7addSJohn Baldwin /* 13164e7f640dSJohn Baldwin * Check to see if this thread is blocked on an sx lock. 13174e7f640dSJohn Baldwin * First, we check the lock class. If that is ok, then we 13184e7f640dSJohn Baldwin * compare the lock name against the wait message. 1319462a7addSJohn Baldwin */ 13204e7f640dSJohn Baldwin sx = td->td_wchan; 13214e7f640dSJohn Baldwin if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 13224e7f640dSJohn Baldwin sx->lock_object.lo_name != td->td_wmesg) 1323462a7addSJohn Baldwin return (0); 1324462a7addSJohn Baldwin 1325462a7addSJohn Baldwin /* We think we have an sx lock, so output some details. */ 1326462a7addSJohn Baldwin db_printf("blocked on sx \"%s\" ", td->td_wmesg); 13274e7f640dSJohn Baldwin *ownerp = sx_xholder(sx); 13284e7f640dSJohn Baldwin if (sx->sx_lock & SX_LOCK_SHARED) 13294e7f640dSJohn Baldwin db_printf("SLOCK (count %ju)\n", 13304e7f640dSJohn Baldwin (uintmax_t)SX_SHARERS(sx->sx_lock)); 13314e7f640dSJohn Baldwin else 1332462a7addSJohn Baldwin db_printf("XLOCK\n"); 1333462a7addSJohn Baldwin return (1); 1334462a7addSJohn Baldwin } 1335d272fe53SJohn Baldwin #endif 1336