19454b2d8SWarner Losh /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38a36da99SPedro F. Giffuni * 44e7f640dSJohn Baldwin * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org> 54e7f640dSJohn Baldwin * Copyright (c) 2001 Jason Evans <jasone@freebsd.org> 64e7f640dSJohn Baldwin * All rights reserved. 76281b30aSJason Evans * 86281b30aSJason Evans * Redistribution and use in source and binary forms, with or without 96281b30aSJason Evans * modification, are permitted provided that the following conditions 106281b30aSJason Evans * are met: 116281b30aSJason Evans * 1. Redistributions of source code must retain the above copyright 126281b30aSJason Evans * notice(s), this list of conditions and the following disclaimer as 136281b30aSJason Evans * the first lines of this file unmodified other than the possible 146281b30aSJason Evans * addition of one or more copyright notices. 156281b30aSJason Evans * 2. Redistributions in binary form must reproduce the above copyright 166281b30aSJason Evans * notice(s), this list of conditions and the following disclaimer in the 176281b30aSJason Evans * documentation and/or other materials provided with the distribution. 186281b30aSJason Evans * 196281b30aSJason Evans * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY 206281b30aSJason Evans * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 216281b30aSJason Evans * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 226281b30aSJason Evans * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY 236281b30aSJason Evans * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 246281b30aSJason Evans * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 256281b30aSJason Evans * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 266281b30aSJason Evans * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 276281b30aSJason Evans * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 286281b30aSJason Evans * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 296281b30aSJason Evans * DAMAGE. 306281b30aSJason Evans */ 316281b30aSJason Evans 326281b30aSJason Evans /* 334e7f640dSJohn Baldwin * Shared/exclusive locks. This implementation attempts to ensure 344e7f640dSJohn Baldwin * deterministic lock granting behavior, so that slocks and xlocks are 354e7f640dSJohn Baldwin * interleaved. 366281b30aSJason Evans * 376281b30aSJason Evans * Priority propagation will not generally raise the priority of lock holders, 386281b30aSJason Evans * so should not be relied upon in combination with sx locks. 396281b30aSJason Evans */ 406281b30aSJason Evans 414e7f640dSJohn Baldwin #include "opt_ddb.h" 42f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h" 43e31d0833SAttilio Rao #include "opt_no_adaptive_sx.h" 444e7f640dSJohn Baldwin 45677b542eSDavid E. O'Brien #include <sys/cdefs.h> 46677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 47677b542eSDavid E. O'Brien 486281b30aSJason Evans #include <sys/param.h> 497a7ce668SAndriy Gapon #include <sys/systm.h> 50cd2fe4e6SAttilio Rao #include <sys/kdb.h> 510453ade5SMateusz Guzik #include <sys/kernel.h> 526281b30aSJason Evans #include <sys/ktr.h> 5319284646SJohn Baldwin #include <sys/lock.h> 546281b30aSJason Evans #include <sys/mutex.h> 55d272fe53SJohn Baldwin #include <sys/proc.h> 562cba8dd3SJohn Baldwin #include <sys/sched.h> 574e7f640dSJohn Baldwin #include <sys/sleepqueue.h> 586281b30aSJason Evans #include <sys/sx.h> 591ada9041SMateusz Guzik #include <sys/smp.h> 60e31d0833SAttilio Rao #include <sys/sysctl.h> 614e7f640dSJohn Baldwin 62e31d0833SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 634e7f640dSJohn Baldwin #include <machine/cpu.h> 644e7f640dSJohn Baldwin #endif 656281b30aSJason Evans 66462a7addSJohn Baldwin #ifdef DDB 67d272fe53SJohn Baldwin #include <ddb/ddb.h> 684e7f640dSJohn Baldwin #endif 69d272fe53SJohn Baldwin 701ae1c2a3SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX) 711ae1c2a3SAttilio Rao #define ADAPTIVE_SX 724e7f640dSJohn Baldwin #endif 734e7f640dSJohn Baldwin 74f0830182SAttilio Rao CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE); 75c1a6d9faSAttilio Rao 76f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS 77f5f9340bSFabien Thomas #include <sys/pmckern.h> 78f5f9340bSFabien Thomas PMC_SOFT_DECLARE( , , lock, failed); 79f5f9340bSFabien Thomas #endif 80f5f9340bSFabien Thomas 814e7f640dSJohn Baldwin /* Handy macros for sleep queues. */ 824e7f640dSJohn Baldwin #define SQ_EXCLUSIVE_QUEUE 0 834e7f640dSJohn Baldwin #define SQ_SHARED_QUEUE 1 844e7f640dSJohn Baldwin 854e7f640dSJohn Baldwin /* 864e7f640dSJohn Baldwin * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We 874e7f640dSJohn Baldwin * drop Giant anytime we have to sleep or if we adaptively spin. 884e7f640dSJohn Baldwin */ 894e7f640dSJohn Baldwin #define GIANT_DECLARE \ 904e7f640dSJohn Baldwin int _giantcnt = 0; \ 914e7f640dSJohn Baldwin WITNESS_SAVE_DECL(Giant) \ 924e7f640dSJohn Baldwin 93e41d6166SMateusz Guzik #define GIANT_SAVE(work) do { \ 94fb106123SMateusz Guzik if (__predict_false(mtx_owned(&Giant))) { \ 95e41d6166SMateusz Guzik work++; \ 964e7f640dSJohn Baldwin WITNESS_SAVE(&Giant.lock_object, Giant); \ 974e7f640dSJohn Baldwin while (mtx_owned(&Giant)) { \ 984e7f640dSJohn Baldwin _giantcnt++; \ 994e7f640dSJohn Baldwin mtx_unlock(&Giant); \ 1004e7f640dSJohn Baldwin } \ 1014e7f640dSJohn Baldwin } \ 1024e7f640dSJohn Baldwin } while (0) 1034e7f640dSJohn Baldwin 1044e7f640dSJohn Baldwin #define GIANT_RESTORE() do { \ 1054e7f640dSJohn Baldwin if (_giantcnt > 0) { \ 1064e7f640dSJohn Baldwin mtx_assert(&Giant, MA_NOTOWNED); \ 1074e7f640dSJohn Baldwin while (_giantcnt--) \ 1084e7f640dSJohn Baldwin mtx_lock(&Giant); \ 1094e7f640dSJohn Baldwin WITNESS_RESTORE(&Giant.lock_object, Giant); \ 1104e7f640dSJohn Baldwin } \ 1114e7f640dSJohn Baldwin } while (0) 1124e7f640dSJohn Baldwin 1134e7f640dSJohn Baldwin /* 114da7d0d1eSJohn Baldwin * Returns true if an exclusive lock is recursed. It assumes 115da7d0d1eSJohn Baldwin * curthread currently has an exclusive lock. 1164e7f640dSJohn Baldwin */ 1174e7f640dSJohn Baldwin #define sx_recursed(sx) ((sx)->sx_recurse != 0) 1184e7f640dSJohn Baldwin 119d576deedSPawel Jakub Dawidek static void assert_sx(const struct lock_object *lock, int what); 1204e7f640dSJohn Baldwin #ifdef DDB 121d576deedSPawel Jakub Dawidek static void db_show_sx(const struct lock_object *lock); 122d272fe53SJohn Baldwin #endif 1237faf4d90SDavide Italiano static void lock_sx(struct lock_object *lock, uintptr_t how); 124a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 125d576deedSPawel Jakub Dawidek static int owner_sx(const struct lock_object *lock, struct thread **owner); 126a5aedd68SStacey Son #endif 1277faf4d90SDavide Italiano static uintptr_t unlock_sx(struct lock_object *lock); 128d272fe53SJohn Baldwin 12919284646SJohn Baldwin struct lock_class lock_class_sx = { 130ae8dde30SJohn Baldwin .lc_name = "sx", 131ae8dde30SJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE, 132f9721b43SAttilio Rao .lc_assert = assert_sx, 133d272fe53SJohn Baldwin #ifdef DDB 134ae8dde30SJohn Baldwin .lc_ddb_show = db_show_sx, 135d272fe53SJohn Baldwin #endif 1366e21afd4SJohn Baldwin .lc_lock = lock_sx, 1376e21afd4SJohn Baldwin .lc_unlock = unlock_sx, 138a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 139a5aedd68SStacey Son .lc_owner = owner_sx, 140a5aedd68SStacey Son #endif 14119284646SJohn Baldwin }; 14219284646SJohn Baldwin 143781a35dfSJohn Baldwin #ifndef INVARIANTS 144781a35dfSJohn Baldwin #define _sx_assert(sx, what, file, line) 145781a35dfSJohn Baldwin #endif 146781a35dfSJohn Baldwin 1471ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX 148574adb65SMateusz Guzik static __read_frequently u_int asx_retries = 10; 149574adb65SMateusz Guzik static __read_frequently u_int asx_loops = 10000; 1506472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging"); 151fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, ""); 152fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, ""); 1531ada9041SMateusz Guzik 154574adb65SMateusz Guzik static struct lock_delay_config __read_frequently sx_delay; 1551ada9041SMateusz Guzik 1568e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_sx, OID_AUTO, delay_base, CTLFLAG_RW, &sx_delay.base, 1571ada9041SMateusz Guzik 0, ""); 1581ada9041SMateusz Guzik SYSCTL_INT(_debug_sx, OID_AUTO, delay_max, CTLFLAG_RW, &sx_delay.max, 1591ada9041SMateusz Guzik 0, ""); 1601ada9041SMateusz Guzik 1618e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(sx_delay); 1621ae1c2a3SAttilio Rao #endif 1631ae1c2a3SAttilio Rao 1646281b30aSJason Evans void 165d576deedSPawel Jakub Dawidek assert_sx(const struct lock_object *lock, int what) 166f9721b43SAttilio Rao { 167f9721b43SAttilio Rao 168d576deedSPawel Jakub Dawidek sx_assert((const struct sx *)lock, what); 169f9721b43SAttilio Rao } 170f9721b43SAttilio Rao 171f9721b43SAttilio Rao void 1727faf4d90SDavide Italiano lock_sx(struct lock_object *lock, uintptr_t how) 1736e21afd4SJohn Baldwin { 1746e21afd4SJohn Baldwin struct sx *sx; 1756e21afd4SJohn Baldwin 1766e21afd4SJohn Baldwin sx = (struct sx *)lock; 1776e21afd4SJohn Baldwin if (how) 1786e21afd4SJohn Baldwin sx_slock(sx); 179cf6b879fSDavide Italiano else 180cf6b879fSDavide Italiano sx_xlock(sx); 1816e21afd4SJohn Baldwin } 1826e21afd4SJohn Baldwin 1837faf4d90SDavide Italiano uintptr_t 1846e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock) 1856e21afd4SJohn Baldwin { 1866e21afd4SJohn Baldwin struct sx *sx; 1876e21afd4SJohn Baldwin 1886e21afd4SJohn Baldwin sx = (struct sx *)lock; 1897ec137e5SJohn Baldwin sx_assert(sx, SA_LOCKED | SA_NOTRECURSED); 1906e21afd4SJohn Baldwin if (sx_xlocked(sx)) { 1916e21afd4SJohn Baldwin sx_xunlock(sx); 192cf6b879fSDavide Italiano return (0); 1936e21afd4SJohn Baldwin } else { 1946e21afd4SJohn Baldwin sx_sunlock(sx); 195cf6b879fSDavide Italiano return (1); 1966e21afd4SJohn Baldwin } 1976e21afd4SJohn Baldwin } 1986e21afd4SJohn Baldwin 199a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 200a5aedd68SStacey Son int 201d576deedSPawel Jakub Dawidek owner_sx(const struct lock_object *lock, struct thread **owner) 202a5aedd68SStacey Son { 203c365a293SMark Johnston const struct sx *sx; 204c365a293SMark Johnston uintptr_t x; 205a5aedd68SStacey Son 206c365a293SMark Johnston sx = (const struct sx *)lock; 207c365a293SMark Johnston x = sx->sx_lock; 208c365a293SMark Johnston *owner = NULL; 209a5aedd68SStacey Son return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) : 210c365a293SMark Johnston ((*owner = (struct thread *)SX_OWNER(x)) != NULL)); 211a5aedd68SStacey Son } 212a5aedd68SStacey Son #endif 213a5aedd68SStacey Son 2146e21afd4SJohn Baldwin void 215c27b5699SAndrew R. Reiter sx_sysinit(void *arg) 216c27b5699SAndrew R. Reiter { 217c27b5699SAndrew R. Reiter struct sx_args *sargs = arg; 218c27b5699SAndrew R. Reiter 219e4cd31ddSJeff Roberson sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags); 220c27b5699SAndrew R. Reiter } 221c27b5699SAndrew R. Reiter 222c27b5699SAndrew R. Reiter void 2234e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts) 2246281b30aSJason Evans { 2254e7f640dSJohn Baldwin int flags; 2266281b30aSJason Evans 227b0d67325SJohn Baldwin MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK | 228fd07ddcfSDmitry Chagin SX_NOPROFILE | SX_NOADAPTIVE | SX_NEW)) == 0); 229353998acSAttilio Rao ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock, 230353998acSAttilio Rao ("%s: sx_lock not aligned for %s: %p", __func__, description, 231353998acSAttilio Rao &sx->sx_lock)); 232b0d67325SJohn Baldwin 233f0830182SAttilio Rao flags = LO_SLEEPABLE | LO_UPGRADABLE; 2344e7f640dSJohn Baldwin if (opts & SX_DUPOK) 2354e7f640dSJohn Baldwin flags |= LO_DUPOK; 2364e7f640dSJohn Baldwin if (opts & SX_NOPROFILE) 2374e7f640dSJohn Baldwin flags |= LO_NOPROFILE; 2384e7f640dSJohn Baldwin if (!(opts & SX_NOWITNESS)) 2394e7f640dSJohn Baldwin flags |= LO_WITNESS; 240f0830182SAttilio Rao if (opts & SX_RECURSE) 241f0830182SAttilio Rao flags |= LO_RECURSABLE; 2424e7f640dSJohn Baldwin if (opts & SX_QUIET) 2434e7f640dSJohn Baldwin flags |= LO_QUIET; 244fd07ddcfSDmitry Chagin if (opts & SX_NEW) 245fd07ddcfSDmitry Chagin flags |= LO_NEW; 2464e7f640dSJohn Baldwin 247f0830182SAttilio Rao flags |= opts & SX_NOADAPTIVE; 248b5fb43e5SJohn Baldwin lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags); 2494e7f640dSJohn Baldwin sx->sx_lock = SX_LOCK_UNLOCKED; 2504e7f640dSJohn Baldwin sx->sx_recurse = 0; 2516281b30aSJason Evans } 2526281b30aSJason Evans 2536281b30aSJason Evans void 2546281b30aSJason Evans sx_destroy(struct sx *sx) 2556281b30aSJason Evans { 2566281b30aSJason Evans 2574e7f640dSJohn Baldwin KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held")); 2584e7f640dSJohn Baldwin KASSERT(sx->sx_recurse == 0, ("sx lock still recursed")); 2590026c92cSJohn Baldwin sx->sx_lock = SX_LOCK_DESTROYED; 260aa89d8cdSJohn Baldwin lock_destroy(&sx->lock_object); 2616281b30aSJason Evans } 2626281b30aSJason Evans 263f9819486SAttilio Rao int 264013c0b49SMateusz Guzik sx_try_slock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 2655f36700aSJohn Baldwin { 2664e7f640dSJohn Baldwin uintptr_t x; 2675f36700aSJohn Baldwin 26835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 26935370593SAndriy Gapon return (1); 27035370593SAndriy Gapon 271cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 272e3ae0dfeSAttilio Rao ("sx_try_slock() by idle thread %p on sx %s @ %s:%d", 273e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 274e3ae0dfeSAttilio Rao 2754e7f640dSJohn Baldwin x = sx->sx_lock; 2765c5df0d9SMateusz Guzik for (;;) { 2770026c92cSJohn Baldwin KASSERT(x != SX_LOCK_DESTROYED, 2780026c92cSJohn Baldwin ("sx_try_slock() of destroyed sx @ %s:%d", file, line)); 279764a938bSPawel Jakub Dawidek if (!(x & SX_LOCK_SHARED)) 280764a938bSPawel Jakub Dawidek break; 2815c5df0d9SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, x + SX_ONE_SHARER)) { 282aa89d8cdSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line); 283aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line); 284de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, 285de2c95ccSMark Johnston sx, 0, 0, file, line, LOCKSTAT_READER); 286ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 2875f36700aSJohn Baldwin return (1); 2885f36700aSJohn Baldwin } 289764a938bSPawel Jakub Dawidek } 2904e7f640dSJohn Baldwin 2914e7f640dSJohn Baldwin LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line); 2924e7f640dSJohn Baldwin return (0); 2935f36700aSJohn Baldwin } 2945f36700aSJohn Baldwin 295f9819486SAttilio Rao int 296013c0b49SMateusz Guzik sx_try_slock_(struct sx *sx, const char *file, int line) 297013c0b49SMateusz Guzik { 298013c0b49SMateusz Guzik 299013c0b49SMateusz Guzik return (sx_try_slock_int(sx LOCK_FILE_LINE_ARG)); 300013c0b49SMateusz Guzik } 301013c0b49SMateusz Guzik 302013c0b49SMateusz Guzik int 303f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line) 3046281b30aSJason Evans { 3056ebb77b6SMateusz Guzik uintptr_t tid, x; 306f9819486SAttilio Rao int error = 0; 3076281b30aSJason Evans 308704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 309704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 310e3ae0dfeSAttilio Rao ("sx_xlock() by idle thread %p on sx %s @ %s:%d", 311e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 3120026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3130026c92cSJohn Baldwin ("sx_xlock() of destroyed sx @ %s:%d", file, line)); 314aa89d8cdSJohn Baldwin WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 31541313430SJohn Baldwin line, NULL); 3166ebb77b6SMateusz Guzik tid = (uintptr_t)curthread; 3176ebb77b6SMateusz Guzik x = SX_LOCK_UNLOCKED; 3186ebb77b6SMateusz Guzik if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 319013c0b49SMateusz Guzik error = _sx_xlock_hard(sx, x, opts LOCK_FILE_LINE_ARG); 3206ebb77b6SMateusz Guzik else 3216ebb77b6SMateusz Guzik LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 3226ebb77b6SMateusz Guzik 0, 0, file, line, LOCKSTAT_WRITER); 323f9819486SAttilio Rao if (!error) { 324f9819486SAttilio Rao LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse, 325f9819486SAttilio Rao file, line); 326aa89d8cdSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 327ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 3286281b30aSJason Evans } 3296281b30aSJason Evans 330f9819486SAttilio Rao return (error); 331f9819486SAttilio Rao } 332f9819486SAttilio Rao 3335f36700aSJohn Baldwin int 334013c0b49SMateusz Guzik sx_try_xlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 3355f36700aSJohn Baldwin { 3365c5df0d9SMateusz Guzik struct thread *td; 3375c5df0d9SMateusz Guzik uintptr_t tid, x; 3384e7f640dSJohn Baldwin int rval; 3395c5df0d9SMateusz Guzik bool recursed; 3405f36700aSJohn Baldwin 3415c5df0d9SMateusz Guzik td = curthread; 3425c5df0d9SMateusz Guzik tid = (uintptr_t)td; 3435c5df0d9SMateusz Guzik if (SCHEDULER_STOPPED_TD(td)) 34435370593SAndriy Gapon return (1); 34535370593SAndriy Gapon 346704cb42fSMark Johnston KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td), 347e3ae0dfeSAttilio Rao ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d", 348e3ae0dfeSAttilio Rao curthread, sx->lock_object.lo_name, file, line)); 3490026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3500026c92cSJohn Baldwin ("sx_try_xlock() of destroyed sx @ %s:%d", file, line)); 3514e7f640dSJohn Baldwin 3525c5df0d9SMateusz Guzik rval = 1; 3535c5df0d9SMateusz Guzik recursed = false; 3545c5df0d9SMateusz Guzik x = SX_LOCK_UNLOCKED; 355b247fd39SMateusz Guzik for (;;) { 356b247fd39SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 357b247fd39SMateusz Guzik break; 358b247fd39SMateusz Guzik if (x == SX_LOCK_UNLOCKED) 359b247fd39SMateusz Guzik continue; 3605c5df0d9SMateusz Guzik if (x == tid && (sx->lock_object.lo_flags & LO_RECURSABLE)) { 3614e7f640dSJohn Baldwin sx->sx_recurse++; 3624e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 363b247fd39SMateusz Guzik break; 3645c5df0d9SMateusz Guzik } 365b247fd39SMateusz Guzik rval = 0; 366b247fd39SMateusz Guzik break; 3675c5df0d9SMateusz Guzik } 3685c5df0d9SMateusz Guzik 3694e7f640dSJohn Baldwin LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line); 3704e7f640dSJohn Baldwin if (rval) { 3714e7f640dSJohn Baldwin WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 3724e7f640dSJohn Baldwin file, line); 3735c5df0d9SMateusz Guzik if (!recursed) 374de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, 375de2c95ccSMark Johnston sx, 0, 0, file, line, LOCKSTAT_WRITER); 376ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 3775f36700aSJohn Baldwin } 3784e7f640dSJohn Baldwin 3794e7f640dSJohn Baldwin return (rval); 3805f36700aSJohn Baldwin } 3815f36700aSJohn Baldwin 382013c0b49SMateusz Guzik int 383013c0b49SMateusz Guzik sx_try_xlock_(struct sx *sx, const char *file, int line) 384013c0b49SMateusz Guzik { 385013c0b49SMateusz Guzik 386013c0b49SMateusz Guzik return (sx_try_xlock_int(sx LOCK_FILE_LINE_ARG)); 387013c0b49SMateusz Guzik } 388013c0b49SMateusz Guzik 3896281b30aSJason Evans void 39019284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line) 3916281b30aSJason Evans { 3926281b30aSJason Evans 3930026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 3940026c92cSJohn Baldwin ("sx_xunlock() of destroyed sx @ %s:%d", file, line)); 3957ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED, file, line); 396aa89d8cdSJohn Baldwin WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line); 3974e7f640dSJohn Baldwin LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file, 3984e7f640dSJohn Baldwin line); 3990108a980SMateusz Guzik #if LOCK_DEBUG > 0 4006ebb77b6SMateusz Guzik _sx_xunlock_hard(sx, (uintptr_t)curthread, file, line); 401ffd5c94cSMateusz Guzik #else 402ffd5c94cSMateusz Guzik __sx_xunlock(sx, curthread, file, line); 403ffd5c94cSMateusz Guzik #endif 404ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 4056281b30aSJason Evans } 406d55229b7SJason Evans 4074e7f640dSJohn Baldwin /* 4084e7f640dSJohn Baldwin * Try to do a non-blocking upgrade from a shared lock to an exclusive lock. 4094e7f640dSJohn Baldwin * This will only succeed if this thread holds a single shared lock. 4104e7f640dSJohn Baldwin * Return 1 if if the upgrade succeed, 0 otherwise. 4114e7f640dSJohn Baldwin */ 412d55229b7SJason Evans int 413013c0b49SMateusz Guzik sx_try_upgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 414d55229b7SJason Evans { 4154e7f640dSJohn Baldwin uintptr_t x; 416a8e747c5SMateusz Guzik uintptr_t waiters; 4174e7f640dSJohn Baldwin int success; 418d55229b7SJason Evans 41935370593SAndriy Gapon if (SCHEDULER_STOPPED()) 42035370593SAndriy Gapon return (1); 42135370593SAndriy Gapon 4220026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4230026c92cSJohn Baldwin ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 4247ec137e5SJohn Baldwin _sx_assert(sx, SA_SLOCKED, file, line); 425d55229b7SJason Evans 4264e7f640dSJohn Baldwin /* 4274e7f640dSJohn Baldwin * Try to switch from one shared lock to an exclusive lock. We need 4284e7f640dSJohn Baldwin * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 4294e7f640dSJohn Baldwin * we will wake up the exclusive waiters when we drop the lock. 4304e7f640dSJohn Baldwin */ 431a8e747c5SMateusz Guzik success = 0; 432a8e747c5SMateusz Guzik x = SX_READ_VALUE(sx); 433a8e747c5SMateusz Guzik for (;;) { 434a8e747c5SMateusz Guzik if (SX_SHARERS(x) > 1) 435a8e747c5SMateusz Guzik break; 436a8e747c5SMateusz Guzik waiters = (x & SX_LOCK_EXCLUSIVE_WAITERS); 437a8e747c5SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, 438a8e747c5SMateusz Guzik (uintptr_t)curthread | waiters)) { 439a8e747c5SMateusz Guzik success = 1; 440a8e747c5SMateusz Guzik break; 441a8e747c5SMateusz Guzik } 442a8e747c5SMateusz Guzik } 4434e7f640dSJohn Baldwin LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 444a5aedd68SStacey Son if (success) { 445aa89d8cdSJohn Baldwin WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 446b0b7cb50SJohn Baldwin file, line); 44732cd0147SMark Johnston LOCKSTAT_RECORD0(sx__upgrade, sx); 448a5aedd68SStacey Son } 4494e7f640dSJohn Baldwin return (success); 450d55229b7SJason Evans } 451d55229b7SJason Evans 452013c0b49SMateusz Guzik int 453013c0b49SMateusz Guzik sx_try_upgrade_(struct sx *sx, const char *file, int line) 454013c0b49SMateusz Guzik { 455013c0b49SMateusz Guzik 456013c0b49SMateusz Guzik return (sx_try_upgrade_int(sx LOCK_FILE_LINE_ARG)); 457013c0b49SMateusz Guzik } 458013c0b49SMateusz Guzik 4594e7f640dSJohn Baldwin /* 4604e7f640dSJohn Baldwin * Downgrade an unrecursed exclusive lock into a single shared lock. 4614e7f640dSJohn Baldwin */ 462d55229b7SJason Evans void 463013c0b49SMateusz Guzik sx_downgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 464d55229b7SJason Evans { 4654e7f640dSJohn Baldwin uintptr_t x; 466da7bbd2cSJohn Baldwin int wakeup_swapper; 467d55229b7SJason Evans 46835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 46935370593SAndriy Gapon return; 47035370593SAndriy Gapon 4710026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4720026c92cSJohn Baldwin ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 4737ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 4744e7f640dSJohn Baldwin #ifndef INVARIANTS 4754e7f640dSJohn Baldwin if (sx_recursed(sx)) 4764e7f640dSJohn Baldwin panic("downgrade of a recursed lock"); 4774e7f640dSJohn Baldwin #endif 478d55229b7SJason Evans 479aa89d8cdSJohn Baldwin WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 480d55229b7SJason Evans 4814e7f640dSJohn Baldwin /* 4824e7f640dSJohn Baldwin * Try to switch from an exclusive lock with no shared waiters 4834e7f640dSJohn Baldwin * to one sharer with no shared waiters. If there are 4844e7f640dSJohn Baldwin * exclusive waiters, we don't need to lock the sleep queue so 4854e7f640dSJohn Baldwin * long as we preserve the flag. We do one quick try and if 4864e7f640dSJohn Baldwin * that fails we grab the sleepq lock to keep the flags from 4874e7f640dSJohn Baldwin * changing and do it the slow way. 4884e7f640dSJohn Baldwin * 4894e7f640dSJohn Baldwin * We have to lock the sleep queue if there are shared waiters 4904e7f640dSJohn Baldwin * so we can wake them up. 4914e7f640dSJohn Baldwin */ 4924e7f640dSJohn Baldwin x = sx->sx_lock; 4934e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS) && 4944e7f640dSJohn Baldwin atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 49526d94f99SMark Johnston (x & SX_LOCK_EXCLUSIVE_WAITERS))) 49626d94f99SMark Johnston goto out; 4974e7f640dSJohn Baldwin 4984e7f640dSJohn Baldwin /* 4994e7f640dSJohn Baldwin * Lock the sleep queue so we can read the waiters bits 5004e7f640dSJohn Baldwin * without any races and wakeup any shared waiters. 5014e7f640dSJohn Baldwin */ 5024e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 5034e7f640dSJohn Baldwin 5044e7f640dSJohn Baldwin /* 5054e7f640dSJohn Baldwin * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 5064e7f640dSJohn Baldwin * shared lock. If there are any shared waiters, wake them up. 5074e7f640dSJohn Baldwin */ 508da7bbd2cSJohn Baldwin wakeup_swapper = 0; 5094e7f640dSJohn Baldwin x = sx->sx_lock; 5104e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 5114e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS)); 5124e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED_WAITERS) 513da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 514da7bbd2cSJohn Baldwin 0, SQ_SHARED_QUEUE); 5154e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 516d55229b7SJason Evans 517da7bbd2cSJohn Baldwin if (wakeup_swapper) 518da7bbd2cSJohn Baldwin kick_proc0(); 51926d94f99SMark Johnston 52026d94f99SMark Johnston out: 52126d94f99SMark Johnston LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 52226d94f99SMark Johnston LOCKSTAT_RECORD0(sx__downgrade, sx); 5234e7f640dSJohn Baldwin } 524d55229b7SJason Evans 525013c0b49SMateusz Guzik void 526013c0b49SMateusz Guzik sx_downgrade_(struct sx *sx, const char *file, int line) 527013c0b49SMateusz Guzik { 528013c0b49SMateusz Guzik 529013c0b49SMateusz Guzik sx_downgrade_int(sx LOCK_FILE_LINE_ARG); 530013c0b49SMateusz Guzik } 531013c0b49SMateusz Guzik 5324e7f640dSJohn Baldwin /* 5334e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xlock 5344e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 5354e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 5364e7f640dSJohn Baldwin * accessible from at least sx.h. 5374e7f640dSJohn Baldwin */ 538f9819486SAttilio Rao int 539013c0b49SMateusz Guzik _sx_xlock_hard(struct sx *sx, uintptr_t x, int opts LOCK_FILE_LINE_ARG_DEF) 5404e7f640dSJohn Baldwin { 5414e7f640dSJohn Baldwin GIANT_DECLARE; 542013c0b49SMateusz Guzik uintptr_t tid; 5434e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5444e7f640dSJohn Baldwin volatile struct thread *owner; 545d07e22cdSMateusz Guzik u_int i, n, spintries = 0; 546d94df98cSMateusz Guzik enum { READERS, WRITER } sleep_reason; 547fb106123SMateusz Guzik bool adaptive; 5484e7f640dSJohn Baldwin #endif 5491723a064SJeff Roberson #ifdef LOCK_PROFILING 5501723a064SJeff Roberson uint64_t waittime = 0; 5511723a064SJeff Roberson int contested = 0; 5521723a064SJeff Roberson #endif 5531723a064SJeff Roberson int error = 0; 55404126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 5551ada9041SMateusz Guzik struct lock_delay_arg lda; 5561ada9041SMateusz Guzik #endif 557a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 55861852185SMateusz Guzik u_int sleep_cnt = 0; 559a5aedd68SStacey Son int64_t sleep_time = 0; 560076dd8ebSAndriy Gapon int64_t all_time = 0; 561a5aedd68SStacey Son #endif 562e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 563e41d6166SMateusz Guzik uintptr_t state; 564e41d6166SMateusz Guzik #endif 565284194f1SMateusz Guzik int extra_work = 0; 5664e7f640dSJohn Baldwin 567013c0b49SMateusz Guzik tid = (uintptr_t)curthread; 568*09bdec20SMateusz Guzik 569*09bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS 570*09bdec20SMateusz Guzik if (LOCKSTAT_PROFILE_ENABLED(sx__acquire)) { 571*09bdec20SMateusz Guzik while (x == SX_LOCK_UNLOCKED) { 572*09bdec20SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 573*09bdec20SMateusz Guzik goto out_lockstat; 574*09bdec20SMateusz Guzik } 575*09bdec20SMateusz Guzik extra_work = 1; 576*09bdec20SMateusz Guzik all_time -= lockstat_nsecs(&sx->lock_object); 577*09bdec20SMateusz Guzik state = x; 578*09bdec20SMateusz Guzik } 579*09bdec20SMateusz Guzik #endif 580*09bdec20SMateusz Guzik #ifdef LOCK_PROFILING 581*09bdec20SMateusz Guzik extra_work = 1; 582*09bdec20SMateusz Guzik state = x; 583*09bdec20SMateusz Guzik #endif 584*09bdec20SMateusz Guzik 58535370593SAndriy Gapon if (SCHEDULER_STOPPED()) 58635370593SAndriy Gapon return (0); 58735370593SAndriy Gapon 588fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 5891ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 590fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 591fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 5921ada9041SMateusz Guzik #endif 5931ada9041SMateusz Guzik 594c1aaf63cSMateusz Guzik if (__predict_false(x == SX_LOCK_UNLOCKED)) 595c1aaf63cSMateusz Guzik x = SX_READ_VALUE(sx); 596c1aaf63cSMateusz Guzik 5974e7f640dSJohn Baldwin /* If we already hold an exclusive lock, then recurse. */ 598c5f61e6fSMateusz Guzik if (__predict_false(lv_sx_owner(x) == (struct thread *)tid)) { 599f0830182SAttilio Rao KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0, 600b0d67325SJohn Baldwin ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 601b0d67325SJohn Baldwin sx->lock_object.lo_name, file, line)); 6024e7f640dSJohn Baldwin sx->sx_recurse++; 6034e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 6044e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6054e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 606f9819486SAttilio Rao return (0); 6074e7f640dSJohn Baldwin } 6084e7f640dSJohn Baldwin 6094e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6104e7f640dSJohn Baldwin CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 6114e7f640dSJohn Baldwin sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 6124e7f640dSJohn Baldwin 613fb106123SMateusz Guzik #ifdef ADAPTIVE_SX 614c505b599SMateusz Guzik adaptive = ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0); 615fb106123SMateusz Guzik #endif 616fb106123SMateusz Guzik 617ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS 618ae7d25a4SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 619ae7d25a4SMateusz Guzik #endif 620ae7d25a4SMateusz Guzik lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 621ae7d25a4SMateusz Guzik &waittime); 622ae7d25a4SMateusz Guzik 623fb106123SMateusz Guzik #ifndef INVARIANTS 624fb106123SMateusz Guzik GIANT_SAVE(extra_work); 625fb106123SMateusz Guzik #endif 626e41d6166SMateusz Guzik 627fc4f686dSMateusz Guzik for (;;) { 628c5f61e6fSMateusz Guzik if (x == SX_LOCK_UNLOCKED) { 629fa474043SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 630fc4f686dSMateusz Guzik break; 631c5f61e6fSMateusz Guzik continue; 632c5f61e6fSMateusz Guzik } 633fb106123SMateusz Guzik #ifdef INVARIANTS 634fb106123SMateusz Guzik GIANT_SAVE(extra_work); 635fb106123SMateusz Guzik #endif 636a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 6371ada9041SMateusz Guzik lda.spin_cnt++; 638a5aedd68SStacey Son #endif 6394e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 640fb106123SMateusz Guzik if (__predict_false(!adaptive)) 641fb106123SMateusz Guzik goto sleepq; 6424e7f640dSJohn Baldwin /* 6434e7f640dSJohn Baldwin * If the lock is write locked and the owner is 6444e7f640dSJohn Baldwin * running on another CPU, spin until the owner stops 6454e7f640dSJohn Baldwin * running or the state of the lock changes. 6464e7f640dSJohn Baldwin */ 6471ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0) { 648d94df98cSMateusz Guzik sleep_reason = WRITER; 649c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 650d94df98cSMateusz Guzik if (!TD_IS_RUNNING(owner)) 651d94df98cSMateusz Guzik goto sleepq; 6524e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 653d94df98cSMateusz Guzik CTR3(KTR_LOCK, "%s: spinning on %p held by %p", 6544e7f640dSJohn Baldwin __func__, sx, owner); 655d94df98cSMateusz Guzik KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread), 656d94df98cSMateusz Guzik "spinning", "lockname:\"%s\"", 6572cba8dd3SJohn Baldwin sx->lock_object.lo_name); 658c5f61e6fSMateusz Guzik do { 6591ada9041SMateusz Guzik lock_delay(&lda); 660c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 661c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 662d94df98cSMateusz Guzik } while (owner != NULL && TD_IS_RUNNING(owner)); 663d94df98cSMateusz Guzik KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread), 664d94df98cSMateusz Guzik "running"); 6654e7f640dSJohn Baldwin continue; 666d94df98cSMateusz Guzik } else if (SX_SHARERS(x) > 0) { 667d94df98cSMateusz Guzik sleep_reason = READERS; 668d94df98cSMateusz Guzik if (spintries == asx_retries) 669d94df98cSMateusz Guzik goto sleepq; 6701ae1c2a3SAttilio Rao spintries++; 671d94df98cSMateusz Guzik KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread), 672d94df98cSMateusz Guzik "spinning", "lockname:\"%s\"", 673d94df98cSMateusz Guzik sx->lock_object.lo_name); 674d07e22cdSMateusz Guzik for (i = 0; i < asx_loops; i += n) { 675d07e22cdSMateusz Guzik n = SX_SHARERS(x); 676d07e22cdSMateusz Guzik lock_delay_spin(n); 67720a15d17SMateusz Guzik x = SX_READ_VALUE(sx); 6781ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0 || 6791ae1c2a3SAttilio Rao SX_SHARERS(x) == 0) 6801ae1c2a3SAttilio Rao break; 6811ae1c2a3SAttilio Rao } 68220a15d17SMateusz Guzik #ifdef KDTRACE_HOOKS 68320a15d17SMateusz Guzik lda.spin_cnt += i; 68420a15d17SMateusz Guzik #endif 685d94df98cSMateusz Guzik KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread), 686d94df98cSMateusz Guzik "running"); 687efa9f177SMateusz Guzik if (i < asx_loops) 6881ae1c2a3SAttilio Rao continue; 6891ae1c2a3SAttilio Rao } 690fb106123SMateusz Guzik sleepq: 691cde25ed4SMateusz Guzik #endif 6924e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 693c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 69493118b62SMateusz Guzik retry_sleepq: 6954e7f640dSJohn Baldwin 6964e7f640dSJohn Baldwin /* 6974e7f640dSJohn Baldwin * If the lock was released while spinning on the 6984e7f640dSJohn Baldwin * sleep queue chain lock, try again. 6994e7f640dSJohn Baldwin */ 7004e7f640dSJohn Baldwin if (x == SX_LOCK_UNLOCKED) { 7014e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 7024e7f640dSJohn Baldwin continue; 7034e7f640dSJohn Baldwin } 7044e7f640dSJohn Baldwin 7054e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 7064e7f640dSJohn Baldwin /* 7074e7f640dSJohn Baldwin * The current lock owner might have started executing 7084e7f640dSJohn Baldwin * on another CPU (or the lock could have changed 7094e7f640dSJohn Baldwin * owners) while we were waiting on the sleep queue 7104e7f640dSJohn Baldwin * chain lock. If so, drop the sleep queue lock and try 7114e7f640dSJohn Baldwin * again. 7124e7f640dSJohn Baldwin */ 71328f1a9e3SMateusz Guzik if (adaptive) { 71428f1a9e3SMateusz Guzik if (!(x & SX_LOCK_SHARED)) { 7154e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 7164e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 7174e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 7184e7f640dSJohn Baldwin continue; 7194e7f640dSJohn Baldwin } 720d94df98cSMateusz Guzik } else if (SX_SHARERS(x) > 0 && sleep_reason == WRITER) { 72128f1a9e3SMateusz Guzik sleepq_release(&sx->lock_object); 72228f1a9e3SMateusz Guzik continue; 72328f1a9e3SMateusz Guzik } 7244e7f640dSJohn Baldwin } 7254e7f640dSJohn Baldwin #endif 7264e7f640dSJohn Baldwin 7274e7f640dSJohn Baldwin /* 7284e7f640dSJohn Baldwin * If an exclusive lock was released with both shared 7294e7f640dSJohn Baldwin * and exclusive waiters and a shared waiter hasn't 7304e7f640dSJohn Baldwin * woken up and acquired the lock yet, sx_lock will be 7314e7f640dSJohn Baldwin * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 7324e7f640dSJohn Baldwin * If we see that value, try to acquire it once. Note 7334e7f640dSJohn Baldwin * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 7344e7f640dSJohn Baldwin * as there are other exclusive waiters still. If we 7354e7f640dSJohn Baldwin * fail, restart the loop. 7364e7f640dSJohn Baldwin */ 7374e7f640dSJohn Baldwin if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 73893118b62SMateusz Guzik if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, 73993118b62SMateusz Guzik tid | SX_LOCK_EXCLUSIVE_WAITERS)) 74093118b62SMateusz Guzik goto retry_sleepq; 7414e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 7424e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p claimed by new writer", 7434e7f640dSJohn Baldwin __func__, sx); 7444e7f640dSJohn Baldwin break; 7454e7f640dSJohn Baldwin } 7464e7f640dSJohn Baldwin 7474e7f640dSJohn Baldwin /* 7484e7f640dSJohn Baldwin * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 7494e7f640dSJohn Baldwin * than loop back and retry. 7504e7f640dSJohn Baldwin */ 7514e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 75293118b62SMateusz Guzik if (!atomic_fcmpset_ptr(&sx->sx_lock, &x, 7534e7f640dSJohn Baldwin x | SX_LOCK_EXCLUSIVE_WAITERS)) { 75493118b62SMateusz Guzik goto retry_sleepq; 7554e7f640dSJohn Baldwin } 7564e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7574e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 7584e7f640dSJohn Baldwin __func__, sx); 7594e7f640dSJohn Baldwin } 7604e7f640dSJohn Baldwin 7614e7f640dSJohn Baldwin /* 7624e7f640dSJohn Baldwin * Since we have been unable to acquire the exclusive 7634e7f640dSJohn Baldwin * lock and the exclusive waiters flag is set, we have 7644e7f640dSJohn Baldwin * to sleep. 7654e7f640dSJohn Baldwin */ 7664e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7674e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 7684e7f640dSJohn Baldwin __func__, sx); 7694e7f640dSJohn Baldwin 770a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 771e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 772a5aedd68SStacey Son #endif 7734e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 774f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 775f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 776f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 777c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 778f9819486SAttilio Rao else 779c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 780a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 781e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 782a5aedd68SStacey Son sleep_cnt++; 783a5aedd68SStacey Son #endif 784f9819486SAttilio Rao if (error) { 785f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 786f9819486SAttilio Rao CTR2(KTR_LOCK, 787f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 788f9819486SAttilio Rao __func__, sx); 789f9819486SAttilio Rao break; 790f9819486SAttilio Rao } 7914e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7924e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 7934e7f640dSJohn Baldwin __func__, sx); 794c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7954e7f640dSJohn Baldwin } 796e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 797e41d6166SMateusz Guzik if (__predict_true(!extra_work)) 798e41d6166SMateusz Guzik return (error); 799e41d6166SMateusz Guzik #endif 800076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 801e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 802076dd8ebSAndriy Gapon if (sleep_time) 80332cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 804076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 805076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 8061ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 80732cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 808076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 809076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 810*09bdec20SMateusz Guzik out_lockstat: 811076dd8ebSAndriy Gapon #endif 812f9819486SAttilio Rao if (!error) 813de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 814de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_WRITER); 815076dd8ebSAndriy Gapon GIANT_RESTORE(); 816f9819486SAttilio Rao return (error); 8174e7f640dSJohn Baldwin } 8184e7f640dSJohn Baldwin 8194e7f640dSJohn Baldwin /* 8204e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xunlock 8214e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 8224e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 8234e7f640dSJohn Baldwin * accessible from at least sx.h. 8244e7f640dSJohn Baldwin */ 8254e7f640dSJohn Baldwin void 826b584eb2eSMateusz Guzik _sx_xunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 8274e7f640dSJohn Baldwin { 828b584eb2eSMateusz Guzik uintptr_t tid, setx; 829da7bbd2cSJohn Baldwin int queue, wakeup_swapper; 8304e7f640dSJohn Baldwin 83135370593SAndriy Gapon if (SCHEDULER_STOPPED()) 83235370593SAndriy Gapon return; 83335370593SAndriy Gapon 834b584eb2eSMateusz Guzik tid = (uintptr_t)curthread; 8354e7f640dSJohn Baldwin 836b584eb2eSMateusz Guzik if (__predict_false(x == tid)) 8373b3cf014SMateusz Guzik x = SX_READ_VALUE(sx); 838b584eb2eSMateusz Guzik 839b584eb2eSMateusz Guzik MPASS(!(x & SX_LOCK_SHARED)); 840b584eb2eSMateusz Guzik 841b584eb2eSMateusz Guzik if (__predict_false(x & SX_LOCK_RECURSED)) { 8426ebb77b6SMateusz Guzik /* The lock is recursed, unrecurse one level. */ 8434e7f640dSJohn Baldwin if ((--sx->sx_recurse) == 0) 8444e7f640dSJohn Baldwin atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 8454e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8464e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 8474e7f640dSJohn Baldwin return; 8484e7f640dSJohn Baldwin } 8493b3cf014SMateusz Guzik 8503b3cf014SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_WRITER); 8513b3cf014SMateusz Guzik if (x == tid && 8523b3cf014SMateusz Guzik atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED)) 8533b3cf014SMateusz Guzik return; 8543b3cf014SMateusz Guzik 8554e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8564e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 8574e7f640dSJohn Baldwin 8584e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 859bc24577cSMateusz Guzik x = SX_READ_VALUE(sx); 8602d96bd88SMateusz Guzik MPASS(x & (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)); 8614e7f640dSJohn Baldwin 8624e7f640dSJohn Baldwin /* 8634e7f640dSJohn Baldwin * The wake up algorithm here is quite simple and probably not 8644e7f640dSJohn Baldwin * ideal. It gives precedence to shared waiters if they are 8654e7f640dSJohn Baldwin * present. For this condition, we have to preserve the 8664e7f640dSJohn Baldwin * state of the exclusive waiters flag. 8672028867dSAttilio Rao * If interruptible sleeps left the shared queue empty avoid a 8682028867dSAttilio Rao * starvation for the threads sleeping on the exclusive queue by giving 8692028867dSAttilio Rao * them precedence and cleaning up the shared waiters bit anyway. 8704e7f640dSJohn Baldwin */ 871bc24577cSMateusz Guzik setx = SX_LOCK_UNLOCKED; 872bc24577cSMateusz Guzik queue = SQ_EXCLUSIVE_QUEUE; 873bc24577cSMateusz Guzik if ((x & SX_LOCK_SHARED_WAITERS) != 0 && 8742028867dSAttilio Rao sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) { 8754e7f640dSJohn Baldwin queue = SQ_SHARED_QUEUE; 876bc24577cSMateusz Guzik setx |= (x & SX_LOCK_EXCLUSIVE_WAITERS); 877bc24577cSMateusz Guzik } 878bc24577cSMateusz Guzik atomic_store_rel_ptr(&sx->sx_lock, setx); 8794e7f640dSJohn Baldwin 8804e7f640dSJohn Baldwin /* Wake up all the waiters for the specific queue. */ 8814e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8824e7f640dSJohn Baldwin CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 8834e7f640dSJohn Baldwin __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 8844e7f640dSJohn Baldwin "exclusive"); 885bc24577cSMateusz Guzik 886da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 887da7bbd2cSJohn Baldwin queue); 888c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 889da7bbd2cSJohn Baldwin if (wakeup_swapper) 890da7bbd2cSJohn Baldwin kick_proc0(); 8914e7f640dSJohn Baldwin } 8924e7f640dSJohn Baldwin 893834f70f3SMateusz Guzik static bool __always_inline 894013c0b49SMateusz Guzik __sx_slock_try(struct sx *sx, uintptr_t *xp LOCK_FILE_LINE_ARG_DEF) 895834f70f3SMateusz Guzik { 896834f70f3SMateusz Guzik 897834f70f3SMateusz Guzik /* 898834f70f3SMateusz Guzik * If no other thread has an exclusive lock then try to bump up 899834f70f3SMateusz Guzik * the count of sharers. Since we have to preserve the state 900834f70f3SMateusz Guzik * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 901834f70f3SMateusz Guzik * shared lock loop back and retry. 902834f70f3SMateusz Guzik */ 903834f70f3SMateusz Guzik while (*xp & SX_LOCK_SHARED) { 904834f70f3SMateusz Guzik MPASS(!(*xp & SX_LOCK_SHARED_WAITERS)); 905834f70f3SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, xp, 906834f70f3SMateusz Guzik *xp + SX_ONE_SHARER)) { 907834f70f3SMateusz Guzik if (LOCK_LOG_TEST(&sx->lock_object, 0)) 908834f70f3SMateusz Guzik CTR4(KTR_LOCK, "%s: %p succeed %p -> %p", 909834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 910834f70f3SMateusz Guzik (void *)(*xp + SX_ONE_SHARER)); 911834f70f3SMateusz Guzik return (true); 912834f70f3SMateusz Guzik } 913834f70f3SMateusz Guzik } 914834f70f3SMateusz Guzik return (false); 915834f70f3SMateusz Guzik } 916834f70f3SMateusz Guzik 917834f70f3SMateusz Guzik static int __noinline 918013c0b49SMateusz Guzik _sx_slock_hard(struct sx *sx, int opts, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 9194e7f640dSJohn Baldwin { 9204e7f640dSJohn Baldwin GIANT_DECLARE; 9214e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 9224e7f640dSJohn Baldwin volatile struct thread *owner; 923fb106123SMateusz Guzik bool adaptive; 9244e7f640dSJohn Baldwin #endif 9251723a064SJeff Roberson #ifdef LOCK_PROFILING 926c1a6d9faSAttilio Rao uint64_t waittime = 0; 927c1a6d9faSAttilio Rao int contested = 0; 9281723a064SJeff Roberson #endif 929c1a6d9faSAttilio Rao int error = 0; 93004126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 9311ada9041SMateusz Guzik struct lock_delay_arg lda; 9321ada9041SMateusz Guzik #endif 933a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 93461852185SMateusz Guzik u_int sleep_cnt = 0; 935a5aedd68SStacey Son int64_t sleep_time = 0; 936076dd8ebSAndriy Gapon int64_t all_time = 0; 937a5aedd68SStacey Son #endif 938e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 939e41d6166SMateusz Guzik uintptr_t state; 940e41d6166SMateusz Guzik #endif 941284194f1SMateusz Guzik int extra_work = 0; 942c1a6d9faSAttilio Rao 943*09bdec20SMateusz Guzik #ifdef KDTRACE_HOOKS 944*09bdec20SMateusz Guzik if (LOCKSTAT_PROFILE_ENABLED(sx__acquire)) { 945*09bdec20SMateusz Guzik if (__sx_slock_try(sx, &x LOCK_FILE_LINE_ARG)) 946*09bdec20SMateusz Guzik goto out_lockstat; 947*09bdec20SMateusz Guzik extra_work = 1; 948*09bdec20SMateusz Guzik all_time -= lockstat_nsecs(&sx->lock_object); 949*09bdec20SMateusz Guzik state = x; 950*09bdec20SMateusz Guzik } 951*09bdec20SMateusz Guzik #endif 952*09bdec20SMateusz Guzik #ifdef LOCK_PROFILING 953*09bdec20SMateusz Guzik extra_work = 1; 954*09bdec20SMateusz Guzik state = x; 955*09bdec20SMateusz Guzik #endif 956*09bdec20SMateusz Guzik 95735370593SAndriy Gapon if (SCHEDULER_STOPPED()) 95835370593SAndriy Gapon return (0); 95935370593SAndriy Gapon 960fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 9611ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 962fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 963fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 9641ada9041SMateusz Guzik #endif 965e41d6166SMateusz Guzik 966fb106123SMateusz Guzik #ifdef ADAPTIVE_SX 967c505b599SMateusz Guzik adaptive = ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0); 968fb106123SMateusz Guzik #endif 969fb106123SMateusz Guzik 970ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS 971ae7d25a4SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 972ae7d25a4SMateusz Guzik #endif 973ae7d25a4SMateusz Guzik lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 974ae7d25a4SMateusz Guzik &waittime); 975ae7d25a4SMateusz Guzik 976fb106123SMateusz Guzik #ifndef INVARIANTS 977fb106123SMateusz Guzik GIANT_SAVE(extra_work); 978fb106123SMateusz Guzik #endif 979076dd8ebSAndriy Gapon 9804e7f640dSJohn Baldwin /* 9814e7f640dSJohn Baldwin * As with rwlocks, we don't make any attempt to try to block 9824e7f640dSJohn Baldwin * shared locks once there is an exclusive waiter. 9834e7f640dSJohn Baldwin */ 9844e7f640dSJohn Baldwin for (;;) { 985013c0b49SMateusz Guzik if (__sx_slock_try(sx, &x LOCK_FILE_LINE_ARG)) 9864e7f640dSJohn Baldwin break; 987fb106123SMateusz Guzik #ifdef INVARIANTS 988fb106123SMateusz Guzik GIANT_SAVE(extra_work); 989fb106123SMateusz Guzik #endif 990c5f61e6fSMateusz Guzik #ifdef KDTRACE_HOOKS 991c5f61e6fSMateusz Guzik lda.spin_cnt++; 992c5f61e6fSMateusz Guzik #endif 993c5f61e6fSMateusz Guzik 9944e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 995fb106123SMateusz Guzik if (__predict_false(!adaptive)) 996fb106123SMateusz Guzik goto sleepq; 9974e7f640dSJohn Baldwin /* 9984e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 9994e7f640dSJohn Baldwin * the owner stops running or the state of the lock 10004e7f640dSJohn Baldwin * changes. 10014e7f640dSJohn Baldwin */ 1002c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 10034e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 10044e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10054e7f640dSJohn Baldwin CTR3(KTR_LOCK, 10064e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 10074e7f640dSJohn Baldwin __func__, sx, owner); 10082cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 10092cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 10102cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 1011c5f61e6fSMateusz Guzik do { 10121ada9041SMateusz Guzik lock_delay(&lda); 1013c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 1014c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 1015c5f61e6fSMateusz Guzik } while (owner != NULL && TD_IS_RUNNING(owner)); 10162cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 10172cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 10184e7f640dSJohn Baldwin continue; 10194e7f640dSJohn Baldwin } 1020cde25ed4SMateusz Guzik sleepq: 10214e7f640dSJohn Baldwin #endif 10224e7f640dSJohn Baldwin 10234e7f640dSJohn Baldwin /* 10244e7f640dSJohn Baldwin * Some other thread already has an exclusive lock, so 10254e7f640dSJohn Baldwin * start the process of blocking. 10264e7f640dSJohn Baldwin */ 10274e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 1028c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 102993118b62SMateusz Guzik retry_sleepq: 10304e7f640dSJohn Baldwin /* 10314e7f640dSJohn Baldwin * The lock could have been released while we spun. 10324e7f640dSJohn Baldwin * In this case loop back and retry. 10334e7f640dSJohn Baldwin */ 10344e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 10354e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 10364e7f640dSJohn Baldwin continue; 10374e7f640dSJohn Baldwin } 10384e7f640dSJohn Baldwin 10394e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 10404e7f640dSJohn Baldwin /* 10414e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 10424e7f640dSJohn Baldwin * the owner stops running or the state of the lock 10434e7f640dSJohn Baldwin * changes. 10444e7f640dSJohn Baldwin */ 1045fb106123SMateusz Guzik if (!(x & SX_LOCK_SHARED) && adaptive) { 10464e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 10474e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 10484e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 1049c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10504e7f640dSJohn Baldwin continue; 10514e7f640dSJohn Baldwin } 10524e7f640dSJohn Baldwin } 10534e7f640dSJohn Baldwin #endif 10544e7f640dSJohn Baldwin 10554e7f640dSJohn Baldwin /* 10564e7f640dSJohn Baldwin * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 10574e7f640dSJohn Baldwin * fail to set it drop the sleep queue lock and loop 10584e7f640dSJohn Baldwin * back. 10594e7f640dSJohn Baldwin */ 10604e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS)) { 106193118b62SMateusz Guzik if (!atomic_fcmpset_ptr(&sx->sx_lock, &x, 106293118b62SMateusz Guzik x | SX_LOCK_SHARED_WAITERS)) 106393118b62SMateusz Guzik goto retry_sleepq; 10644e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10654e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 10664e7f640dSJohn Baldwin __func__, sx); 10674e7f640dSJohn Baldwin } 10684e7f640dSJohn Baldwin 10694e7f640dSJohn Baldwin /* 10704e7f640dSJohn Baldwin * Since we have been unable to acquire the shared lock, 10714e7f640dSJohn Baldwin * we have to sleep. 10724e7f640dSJohn Baldwin */ 10734e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10744e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 10754e7f640dSJohn Baldwin __func__, sx); 10764e7f640dSJohn Baldwin 1077a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 1078e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 1079a5aedd68SStacey Son #endif 10804e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 1081f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 1082f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 1083f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 1084c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 1085f9819486SAttilio Rao else 1086c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 1087a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 1088e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 1089a5aedd68SStacey Son sleep_cnt++; 1090a5aedd68SStacey Son #endif 1091f9819486SAttilio Rao if (error) { 1092f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1093f9819486SAttilio Rao CTR2(KTR_LOCK, 1094f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 1095f9819486SAttilio Rao __func__, sx); 1096f9819486SAttilio Rao break; 1097f9819486SAttilio Rao } 10984e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10994e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 11004e7f640dSJohn Baldwin __func__, sx); 1101c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 11024e7f640dSJohn Baldwin } 1103e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 1104e41d6166SMateusz Guzik if (__predict_true(!extra_work)) 1105e41d6166SMateusz Guzik return (error); 1106e41d6166SMateusz Guzik #endif 1107076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 1108e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 1109076dd8ebSAndriy Gapon if (sleep_time) 111032cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 1111076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1112076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 11131ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 111432cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 1115076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1116076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 1117*09bdec20SMateusz Guzik out_lockstat: 1118076dd8ebSAndriy Gapon #endif 11193ae56ce9SMateusz Guzik if (error == 0) { 1120de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 1121de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_READER); 11223ae56ce9SMateusz Guzik } 11234e7f640dSJohn Baldwin GIANT_RESTORE(); 1124f9819486SAttilio Rao return (error); 11254e7f640dSJohn Baldwin } 11264e7f640dSJohn Baldwin 1127834f70f3SMateusz Guzik int 1128013c0b49SMateusz Guzik _sx_slock_int(struct sx *sx, int opts LOCK_FILE_LINE_ARG_DEF) 11294e7f640dSJohn Baldwin { 11304e7f640dSJohn Baldwin uintptr_t x; 1131834f70f3SMateusz Guzik int error; 11324e7f640dSJohn Baldwin 1133704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 1134704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 1135834f70f3SMateusz Guzik ("sx_slock() by idle thread %p on sx %s @ %s:%d", 1136834f70f3SMateusz Guzik curthread, sx->lock_object.lo_name, file, line)); 11373ae56ce9SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1138834f70f3SMateusz Guzik ("sx_slock() of destroyed sx @ %s:%d", file, line)); 1139834f70f3SMateusz Guzik WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 1140834f70f3SMateusz Guzik 1141834f70f3SMateusz Guzik error = 0; 1142c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 1143e4ccf57fSMateusz Guzik if (__predict_false(LOCKSTAT_PROFILE_ENABLED(sx__acquire) || 1144013c0b49SMateusz Guzik !__sx_slock_try(sx, &x LOCK_FILE_LINE_ARG))) 1145013c0b49SMateusz Guzik error = _sx_slock_hard(sx, opts, x LOCK_FILE_LINE_ARG); 1146e4ccf57fSMateusz Guzik else 1147e4ccf57fSMateusz Guzik lock_profile_obtain_lock_success(&sx->lock_object, 0, 0, 1148e4ccf57fSMateusz Guzik file, line); 1149834f70f3SMateusz Guzik if (error == 0) { 1150834f70f3SMateusz Guzik LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 1151834f70f3SMateusz Guzik WITNESS_LOCK(&sx->lock_object, 0, file, line); 1152834f70f3SMateusz Guzik TD_LOCKS_INC(curthread); 1153834f70f3SMateusz Guzik } 1154834f70f3SMateusz Guzik return (error); 1155834f70f3SMateusz Guzik } 1156834f70f3SMateusz Guzik 1157013c0b49SMateusz Guzik int 1158013c0b49SMateusz Guzik _sx_slock(struct sx *sx, int opts, const char *file, int line) 1159013c0b49SMateusz Guzik { 1160013c0b49SMateusz Guzik 1161013c0b49SMateusz Guzik return (_sx_slock_int(sx, opts LOCK_FILE_LINE_ARG)); 1162013c0b49SMateusz Guzik } 1163013c0b49SMateusz Guzik 1164834f70f3SMateusz Guzik static bool __always_inline 1165834f70f3SMateusz Guzik _sx_sunlock_try(struct sx *sx, uintptr_t *xp) 1166834f70f3SMateusz Guzik { 1167834f70f3SMateusz Guzik 11684e7f640dSJohn Baldwin for (;;) { 11694e7f640dSJohn Baldwin /* 11704e7f640dSJohn Baldwin * We should never have sharers while at least one thread 11714e7f640dSJohn Baldwin * holds a shared lock. 11724e7f640dSJohn Baldwin */ 1173834f70f3SMateusz Guzik KASSERT(!(*xp & SX_LOCK_SHARED_WAITERS), 11744e7f640dSJohn Baldwin ("%s: waiting sharers", __func__)); 11754e7f640dSJohn Baldwin 11764e7f640dSJohn Baldwin /* 11774e7f640dSJohn Baldwin * See if there is more than one shared lock held. If 11784e7f640dSJohn Baldwin * so, just drop one and return. 11794e7f640dSJohn Baldwin */ 1180834f70f3SMateusz Guzik if (SX_SHARERS(*xp) > 1) { 1181834f70f3SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, xp, 1182834f70f3SMateusz Guzik *xp - SX_ONE_SHARER)) { 11834e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 11844e7f640dSJohn Baldwin CTR4(KTR_LOCK, 11854e7f640dSJohn Baldwin "%s: %p succeeded %p -> %p", 1186834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 1187834f70f3SMateusz Guzik (void *)(*xp - SX_ONE_SHARER)); 1188834f70f3SMateusz Guzik return (true); 11894e7f640dSJohn Baldwin } 11904e7f640dSJohn Baldwin continue; 11914e7f640dSJohn Baldwin } 11924e7f640dSJohn Baldwin 11934e7f640dSJohn Baldwin /* 11944e7f640dSJohn Baldwin * If there aren't any waiters for an exclusive lock, 11954e7f640dSJohn Baldwin * then try to drop it quickly. 11964e7f640dSJohn Baldwin */ 1197834f70f3SMateusz Guzik if (!(*xp & SX_LOCK_EXCLUSIVE_WAITERS)) { 1198834f70f3SMateusz Guzik MPASS(*xp == SX_SHARERS_LOCK(1)); 1199834f70f3SMateusz Guzik *xp = SX_SHARERS_LOCK(1); 1200fa474043SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, 1201834f70f3SMateusz Guzik xp, SX_LOCK_UNLOCKED)) { 12024e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 12034e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p last succeeded", 12044e7f640dSJohn Baldwin __func__, sx); 1205834f70f3SMateusz Guzik return (true); 12064e7f640dSJohn Baldwin } 12074e7f640dSJohn Baldwin continue; 12084e7f640dSJohn Baldwin } 1209834f70f3SMateusz Guzik break; 1210834f70f3SMateusz Guzik } 1211834f70f3SMateusz Guzik return (false); 1212834f70f3SMateusz Guzik } 1213834f70f3SMateusz Guzik 1214834f70f3SMateusz Guzik static void __noinline 1215013c0b49SMateusz Guzik _sx_sunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 1216834f70f3SMateusz Guzik { 12171b54ffc8SMateusz Guzik int wakeup_swapper = 0; 1218cec17473SMateusz Guzik uintptr_t setx; 1219834f70f3SMateusz Guzik 1220834f70f3SMateusz Guzik if (SCHEDULER_STOPPED()) 1221834f70f3SMateusz Guzik return; 1222834f70f3SMateusz Guzik 1223834f70f3SMateusz Guzik if (_sx_sunlock_try(sx, &x)) 1224cec17473SMateusz Guzik goto out_lockstat; 12254e7f640dSJohn Baldwin 12264e7f640dSJohn Baldwin /* 12274e7f640dSJohn Baldwin * At this point, there should just be one sharer with 12284e7f640dSJohn Baldwin * exclusive waiters. 12294e7f640dSJohn Baldwin */ 12304e7f640dSJohn Baldwin MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS)); 12314e7f640dSJohn Baldwin 12324e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 1233cec17473SMateusz Guzik x = SX_READ_VALUE(sx); 1234cec17473SMateusz Guzik for (;;) { 1235cec17473SMateusz Guzik MPASS(x & SX_LOCK_EXCLUSIVE_WAITERS); 1236cec17473SMateusz Guzik MPASS(!(x & SX_LOCK_SHARED_WAITERS)); 12371b54ffc8SMateusz Guzik if (_sx_sunlock_try(sx, &x)) 12381b54ffc8SMateusz Guzik break; 12391b54ffc8SMateusz Guzik 12404e7f640dSJohn Baldwin /* 12414e7f640dSJohn Baldwin * Wake up semantic here is quite simple: 12424e7f640dSJohn Baldwin * Just wake up all the exclusive waiters. 12434e7f640dSJohn Baldwin * Note that the state of the lock could have changed, 12444e7f640dSJohn Baldwin * so if it fails loop back and retry. 12454e7f640dSJohn Baldwin */ 1246cec17473SMateusz Guzik setx = x - SX_ONE_SHARER; 1247cec17473SMateusz Guzik setx &= ~SX_LOCK_EXCLUSIVE_WAITERS; 1248cec17473SMateusz Guzik if (!atomic_fcmpset_rel_ptr(&sx->sx_lock, &x, setx)) 12494e7f640dSJohn Baldwin continue; 12504e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 12514e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p waking up all thread on" 12524e7f640dSJohn Baldwin "exclusive queue", __func__, sx); 1253da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 1254da7bbd2cSJohn Baldwin 0, SQ_EXCLUSIVE_QUEUE); 1255cec17473SMateusz Guzik break; 1256cec17473SMateusz Guzik } 1257c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 1258da7bbd2cSJohn Baldwin if (wakeup_swapper) 1259da7bbd2cSJohn Baldwin kick_proc0(); 1260cec17473SMateusz Guzik out_lockstat: 1261dbe4541dSMark Johnston LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER); 1262834f70f3SMateusz Guzik } 1263834f70f3SMateusz Guzik 1264834f70f3SMateusz Guzik void 1265013c0b49SMateusz Guzik _sx_sunlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 1266834f70f3SMateusz Guzik { 1267834f70f3SMateusz Guzik uintptr_t x; 1268834f70f3SMateusz Guzik 1269834f70f3SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1270834f70f3SMateusz Guzik ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 1271834f70f3SMateusz Guzik _sx_assert(sx, SA_SLOCKED, file, line); 1272834f70f3SMateusz Guzik WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 1273834f70f3SMateusz Guzik LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 1274834f70f3SMateusz Guzik 1275834f70f3SMateusz Guzik x = SX_READ_VALUE(sx); 1276e4ccf57fSMateusz Guzik if (__predict_false(LOCKSTAT_PROFILE_ENABLED(sx__release) || 1277834f70f3SMateusz Guzik !_sx_sunlock_try(sx, &x))) 1278013c0b49SMateusz Guzik _sx_sunlock_hard(sx, x LOCK_FILE_LINE_ARG); 1279e4ccf57fSMateusz Guzik else 1280e4ccf57fSMateusz Guzik lock_profile_release_lock(&sx->lock_object); 1281834f70f3SMateusz Guzik 12823ae56ce9SMateusz Guzik TD_LOCKS_DEC(curthread); 1283d55229b7SJason Evans } 12844e5e677bSJohn Baldwin 1285013c0b49SMateusz Guzik void 1286013c0b49SMateusz Guzik _sx_sunlock(struct sx *sx, const char *file, int line) 1287013c0b49SMateusz Guzik { 1288013c0b49SMateusz Guzik 1289013c0b49SMateusz Guzik _sx_sunlock_int(sx LOCK_FILE_LINE_ARG); 1290013c0b49SMateusz Guzik } 1291013c0b49SMateusz Guzik 12924e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT 1293781a35dfSJohn Baldwin #ifndef INVARIANTS 1294781a35dfSJohn Baldwin #undef _sx_assert 1295781a35dfSJohn Baldwin #endif 1296781a35dfSJohn Baldwin 12974e5e677bSJohn Baldwin /* 12984e5e677bSJohn Baldwin * In the non-WITNESS case, sx_assert() can only detect that at least 12994e5e677bSJohn Baldwin * *some* thread owns an slock, but it cannot guarantee that *this* 13004e5e677bSJohn Baldwin * thread owns an slock. 13014e5e677bSJohn Baldwin */ 13024e5e677bSJohn Baldwin void 1303d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line) 13044e5e677bSJohn Baldwin { 13054e7f640dSJohn Baldwin #ifndef WITNESS 13064e7f640dSJohn Baldwin int slocked = 0; 13074e7f640dSJohn Baldwin #endif 13084e5e677bSJohn Baldwin 130903129ba9SJohn Baldwin if (panicstr != NULL) 131003129ba9SJohn Baldwin return; 13114e5e677bSJohn Baldwin switch (what) { 13127ec137e5SJohn Baldwin case SA_SLOCKED: 13137ec137e5SJohn Baldwin case SA_SLOCKED | SA_NOTRECURSED: 13147ec137e5SJohn Baldwin case SA_SLOCKED | SA_RECURSED: 13154e7f640dSJohn Baldwin #ifndef WITNESS 13164e7f640dSJohn Baldwin slocked = 1; 13174e7f640dSJohn Baldwin /* FALLTHROUGH */ 13184e7f640dSJohn Baldwin #endif 13197ec137e5SJohn Baldwin case SA_LOCKED: 13207ec137e5SJohn Baldwin case SA_LOCKED | SA_NOTRECURSED: 13217ec137e5SJohn Baldwin case SA_LOCKED | SA_RECURSED: 13224e5e677bSJohn Baldwin #ifdef WITNESS 1323aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 13244e5e677bSJohn Baldwin #else 13254e7f640dSJohn Baldwin /* 13264e7f640dSJohn Baldwin * If some other thread has an exclusive lock or we 13274e7f640dSJohn Baldwin * have one and are asserting a shared lock, fail. 13284e7f640dSJohn Baldwin * Also, if no one has a lock at all, fail. 13294e7f640dSJohn Baldwin */ 13304e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED || 13314e7f640dSJohn Baldwin (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 13324e7f640dSJohn Baldwin sx_xholder(sx) != curthread))) 133303129ba9SJohn Baldwin panic("Lock %s not %slocked @ %s:%d\n", 13344e7f640dSJohn Baldwin sx->lock_object.lo_name, slocked ? "share " : "", 13354e7f640dSJohn Baldwin file, line); 13364e7f640dSJohn Baldwin 13374e7f640dSJohn Baldwin if (!(sx->sx_lock & SX_LOCK_SHARED)) { 13384e7f640dSJohn Baldwin if (sx_recursed(sx)) { 13397ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 13404e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 13414e7f640dSJohn Baldwin sx->lock_object.lo_name, file, 13424e7f640dSJohn Baldwin line); 13437ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 13444e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 13454e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13464e7f640dSJohn Baldwin } 13474e5e677bSJohn Baldwin #endif 13484e5e677bSJohn Baldwin break; 13497ec137e5SJohn Baldwin case SA_XLOCKED: 13507ec137e5SJohn Baldwin case SA_XLOCKED | SA_NOTRECURSED: 13517ec137e5SJohn Baldwin case SA_XLOCKED | SA_RECURSED: 13524e7f640dSJohn Baldwin if (sx_xholder(sx) != curthread) 135303129ba9SJohn Baldwin panic("Lock %s not exclusively locked @ %s:%d\n", 1354aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 13554e7f640dSJohn Baldwin if (sx_recursed(sx)) { 13567ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 13574e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 13584e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13597ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 13604e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 13614e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13624e5e677bSJohn Baldwin break; 13637ec137e5SJohn Baldwin case SA_UNLOCKED: 136419b0efd3SPawel Jakub Dawidek #ifdef WITNESS 1365aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 136619b0efd3SPawel Jakub Dawidek #else 1367f6739b1dSPawel Jakub Dawidek /* 13684e7f640dSJohn Baldwin * If we hold an exclusve lock fail. We can't 13694e7f640dSJohn Baldwin * reliably check to see if we hold a shared lock or 13704e7f640dSJohn Baldwin * not. 1371f6739b1dSPawel Jakub Dawidek */ 13724e7f640dSJohn Baldwin if (sx_xholder(sx) == curthread) 137303129ba9SJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 1374aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 137519b0efd3SPawel Jakub Dawidek #endif 137619b0efd3SPawel Jakub Dawidek break; 13774e5e677bSJohn Baldwin default: 13784e5e677bSJohn Baldwin panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 13794e5e677bSJohn Baldwin line); 13804e5e677bSJohn Baldwin } 13814e5e677bSJohn Baldwin } 13824e5e677bSJohn Baldwin #endif /* INVARIANT_SUPPORT */ 1383d272fe53SJohn Baldwin 1384d272fe53SJohn Baldwin #ifdef DDB 13854e7f640dSJohn Baldwin static void 1386d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock) 1387d272fe53SJohn Baldwin { 1388d272fe53SJohn Baldwin struct thread *td; 1389d576deedSPawel Jakub Dawidek const struct sx *sx; 1390d272fe53SJohn Baldwin 1391d576deedSPawel Jakub Dawidek sx = (const struct sx *)lock; 1392d272fe53SJohn Baldwin 1393d272fe53SJohn Baldwin db_printf(" state: "); 13944e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED) 13954e7f640dSJohn Baldwin db_printf("UNLOCKED\n"); 13960026c92cSJohn Baldwin else if (sx->sx_lock == SX_LOCK_DESTROYED) { 13970026c92cSJohn Baldwin db_printf("DESTROYED\n"); 13980026c92cSJohn Baldwin return; 13990026c92cSJohn Baldwin } else if (sx->sx_lock & SX_LOCK_SHARED) 14004e7f640dSJohn Baldwin db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 14014e7f640dSJohn Baldwin else { 14024e7f640dSJohn Baldwin td = sx_xholder(sx); 1403d272fe53SJohn Baldwin db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1404431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 14054e7f640dSJohn Baldwin if (sx_recursed(sx)) 14064e7f640dSJohn Baldwin db_printf(" recursed: %d\n", sx->sx_recurse); 14074e7f640dSJohn Baldwin } 14084e7f640dSJohn Baldwin 14094e7f640dSJohn Baldwin db_printf(" waiters: "); 14104e7f640dSJohn Baldwin switch(sx->sx_lock & 14114e7f640dSJohn Baldwin (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 14124e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS: 14134e7f640dSJohn Baldwin db_printf("shared\n"); 14144e7f640dSJohn Baldwin break; 14154e7f640dSJohn Baldwin case SX_LOCK_EXCLUSIVE_WAITERS: 14164e7f640dSJohn Baldwin db_printf("exclusive\n"); 14174e7f640dSJohn Baldwin break; 14184e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 14194e7f640dSJohn Baldwin db_printf("exclusive and shared\n"); 14204e7f640dSJohn Baldwin break; 14214e7f640dSJohn Baldwin default: 14224e7f640dSJohn Baldwin db_printf("none\n"); 14234e7f640dSJohn Baldwin } 1424d272fe53SJohn Baldwin } 1425462a7addSJohn Baldwin 1426462a7addSJohn Baldwin /* 1427462a7addSJohn Baldwin * Check to see if a thread that is blocked on a sleep queue is actually 1428462a7addSJohn Baldwin * blocked on an sx lock. If so, output some details and return true. 1429462a7addSJohn Baldwin * If the lock has an exclusive owner, return that in *ownerp. 1430462a7addSJohn Baldwin */ 1431462a7addSJohn Baldwin int 1432462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp) 1433462a7addSJohn Baldwin { 1434462a7addSJohn Baldwin struct sx *sx; 1435462a7addSJohn Baldwin 1436462a7addSJohn Baldwin /* 14374e7f640dSJohn Baldwin * Check to see if this thread is blocked on an sx lock. 14384e7f640dSJohn Baldwin * First, we check the lock class. If that is ok, then we 14394e7f640dSJohn Baldwin * compare the lock name against the wait message. 1440462a7addSJohn Baldwin */ 14414e7f640dSJohn Baldwin sx = td->td_wchan; 14424e7f640dSJohn Baldwin if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 14434e7f640dSJohn Baldwin sx->lock_object.lo_name != td->td_wmesg) 1444462a7addSJohn Baldwin return (0); 1445462a7addSJohn Baldwin 1446462a7addSJohn Baldwin /* We think we have an sx lock, so output some details. */ 1447462a7addSJohn Baldwin db_printf("blocked on sx \"%s\" ", td->td_wmesg); 14484e7f640dSJohn Baldwin *ownerp = sx_xholder(sx); 14494e7f640dSJohn Baldwin if (sx->sx_lock & SX_LOCK_SHARED) 14504e7f640dSJohn Baldwin db_printf("SLOCK (count %ju)\n", 14514e7f640dSJohn Baldwin (uintmax_t)SX_SHARERS(sx->sx_lock)); 14524e7f640dSJohn Baldwin else 1453462a7addSJohn Baldwin db_printf("XLOCK\n"); 1454462a7addSJohn Baldwin return (1); 1455462a7addSJohn Baldwin } 1456d272fe53SJohn Baldwin #endif 1457