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; 4164e7f640dSJohn Baldwin int success; 417d55229b7SJason Evans 41835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 41935370593SAndriy Gapon return (1); 42035370593SAndriy Gapon 4210026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4220026c92cSJohn Baldwin ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line)); 4237ec137e5SJohn Baldwin _sx_assert(sx, SA_SLOCKED, file, line); 424d55229b7SJason Evans 4254e7f640dSJohn Baldwin /* 4264e7f640dSJohn Baldwin * Try to switch from one shared lock to an exclusive lock. We need 4274e7f640dSJohn Baldwin * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that 4284e7f640dSJohn Baldwin * we will wake up the exclusive waiters when we drop the lock. 4294e7f640dSJohn Baldwin */ 4304e7f640dSJohn Baldwin x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS; 431a2101806SMateusz Guzik success = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x, 4324e7f640dSJohn Baldwin (uintptr_t)curthread | x); 4334e7f640dSJohn Baldwin LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line); 434a5aedd68SStacey Son if (success) { 435aa89d8cdSJohn Baldwin WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 436b0b7cb50SJohn Baldwin file, line); 43732cd0147SMark Johnston LOCKSTAT_RECORD0(sx__upgrade, sx); 438a5aedd68SStacey Son } 4394e7f640dSJohn Baldwin return (success); 440d55229b7SJason Evans } 441d55229b7SJason Evans 442013c0b49SMateusz Guzik int 443013c0b49SMateusz Guzik sx_try_upgrade_(struct sx *sx, const char *file, int line) 444013c0b49SMateusz Guzik { 445013c0b49SMateusz Guzik 446013c0b49SMateusz Guzik return (sx_try_upgrade_int(sx LOCK_FILE_LINE_ARG)); 447013c0b49SMateusz Guzik } 448013c0b49SMateusz Guzik 4494e7f640dSJohn Baldwin /* 4504e7f640dSJohn Baldwin * Downgrade an unrecursed exclusive lock into a single shared lock. 4514e7f640dSJohn Baldwin */ 452d55229b7SJason Evans void 453013c0b49SMateusz Guzik sx_downgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 454d55229b7SJason Evans { 4554e7f640dSJohn Baldwin uintptr_t x; 456da7bbd2cSJohn Baldwin int wakeup_swapper; 457d55229b7SJason Evans 45835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 45935370593SAndriy Gapon return; 46035370593SAndriy Gapon 4610026c92cSJohn Baldwin KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 4620026c92cSJohn Baldwin ("sx_downgrade() of destroyed sx @ %s:%d", file, line)); 4637ec137e5SJohn Baldwin _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line); 4644e7f640dSJohn Baldwin #ifndef INVARIANTS 4654e7f640dSJohn Baldwin if (sx_recursed(sx)) 4664e7f640dSJohn Baldwin panic("downgrade of a recursed lock"); 4674e7f640dSJohn Baldwin #endif 468d55229b7SJason Evans 469aa89d8cdSJohn Baldwin WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line); 470d55229b7SJason Evans 4714e7f640dSJohn Baldwin /* 4724e7f640dSJohn Baldwin * Try to switch from an exclusive lock with no shared waiters 4734e7f640dSJohn Baldwin * to one sharer with no shared waiters. If there are 4744e7f640dSJohn Baldwin * exclusive waiters, we don't need to lock the sleep queue so 4754e7f640dSJohn Baldwin * long as we preserve the flag. We do one quick try and if 4764e7f640dSJohn Baldwin * that fails we grab the sleepq lock to keep the flags from 4774e7f640dSJohn Baldwin * changing and do it the slow way. 4784e7f640dSJohn Baldwin * 4794e7f640dSJohn Baldwin * We have to lock the sleep queue if there are shared waiters 4804e7f640dSJohn Baldwin * so we can wake them up. 4814e7f640dSJohn Baldwin */ 4824e7f640dSJohn Baldwin x = sx->sx_lock; 4834e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS) && 4844e7f640dSJohn Baldwin atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) | 48526d94f99SMark Johnston (x & SX_LOCK_EXCLUSIVE_WAITERS))) 48626d94f99SMark Johnston goto out; 4874e7f640dSJohn Baldwin 4884e7f640dSJohn Baldwin /* 4894e7f640dSJohn Baldwin * Lock the sleep queue so we can read the waiters bits 4904e7f640dSJohn Baldwin * without any races and wakeup any shared waiters. 4914e7f640dSJohn Baldwin */ 4924e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 4934e7f640dSJohn Baldwin 4944e7f640dSJohn Baldwin /* 4954e7f640dSJohn Baldwin * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single 4964e7f640dSJohn Baldwin * shared lock. If there are any shared waiters, wake them up. 4974e7f640dSJohn Baldwin */ 498da7bbd2cSJohn Baldwin wakeup_swapper = 0; 4994e7f640dSJohn Baldwin x = sx->sx_lock; 5004e7f640dSJohn Baldwin atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | 5014e7f640dSJohn Baldwin (x & SX_LOCK_EXCLUSIVE_WAITERS)); 5024e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED_WAITERS) 503da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 504da7bbd2cSJohn Baldwin 0, SQ_SHARED_QUEUE); 5054e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 506d55229b7SJason Evans 507da7bbd2cSJohn Baldwin if (wakeup_swapper) 508da7bbd2cSJohn Baldwin kick_proc0(); 50926d94f99SMark Johnston 51026d94f99SMark Johnston out: 51126d94f99SMark Johnston LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line); 51226d94f99SMark Johnston LOCKSTAT_RECORD0(sx__downgrade, sx); 5134e7f640dSJohn Baldwin } 514d55229b7SJason Evans 515013c0b49SMateusz Guzik void 516013c0b49SMateusz Guzik sx_downgrade_(struct sx *sx, const char *file, int line) 517013c0b49SMateusz Guzik { 518013c0b49SMateusz Guzik 519013c0b49SMateusz Guzik sx_downgrade_int(sx LOCK_FILE_LINE_ARG); 520013c0b49SMateusz Guzik } 521013c0b49SMateusz Guzik 5224e7f640dSJohn Baldwin /* 5234e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xlock 5244e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 5254e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 5264e7f640dSJohn Baldwin * accessible from at least sx.h. 5274e7f640dSJohn Baldwin */ 528f9819486SAttilio Rao int 529013c0b49SMateusz Guzik _sx_xlock_hard(struct sx *sx, uintptr_t x, int opts LOCK_FILE_LINE_ARG_DEF) 5304e7f640dSJohn Baldwin { 5314e7f640dSJohn Baldwin GIANT_DECLARE; 532013c0b49SMateusz Guzik uintptr_t tid; 5334e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 5344e7f640dSJohn Baldwin volatile struct thread *owner; 535d07e22cdSMateusz Guzik u_int i, n, spintries = 0; 536fb106123SMateusz Guzik bool adaptive; 53728f1a9e3SMateusz Guzik int sleep_reason = 0; 5384e7f640dSJohn Baldwin #endif 5391723a064SJeff Roberson #ifdef LOCK_PROFILING 5401723a064SJeff Roberson uint64_t waittime = 0; 5411723a064SJeff Roberson int contested = 0; 5421723a064SJeff Roberson #endif 5431723a064SJeff Roberson int error = 0; 54404126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 5451ada9041SMateusz Guzik struct lock_delay_arg lda; 5461ada9041SMateusz Guzik #endif 547a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 54861852185SMateusz Guzik u_int sleep_cnt = 0; 549a5aedd68SStacey Son int64_t sleep_time = 0; 550076dd8ebSAndriy Gapon int64_t all_time = 0; 551a5aedd68SStacey Son #endif 552e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 553e41d6166SMateusz Guzik uintptr_t state; 554e41d6166SMateusz Guzik #endif 555284194f1SMateusz Guzik int extra_work = 0; 5564e7f640dSJohn Baldwin 557013c0b49SMateusz Guzik tid = (uintptr_t)curthread; 55835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 55935370593SAndriy Gapon return (0); 56035370593SAndriy Gapon 561fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 5621ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 563fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 564fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 5651ada9041SMateusz Guzik #endif 5661ada9041SMateusz Guzik 567c1aaf63cSMateusz Guzik if (__predict_false(x == SX_LOCK_UNLOCKED)) 568c1aaf63cSMateusz Guzik x = SX_READ_VALUE(sx); 569c1aaf63cSMateusz Guzik 5704e7f640dSJohn Baldwin /* If we already hold an exclusive lock, then recurse. */ 571c5f61e6fSMateusz Guzik if (__predict_false(lv_sx_owner(x) == (struct thread *)tid)) { 572f0830182SAttilio Rao KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0, 573b0d67325SJohn Baldwin ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n", 574b0d67325SJohn Baldwin sx->lock_object.lo_name, file, line)); 5754e7f640dSJohn Baldwin sx->sx_recurse++; 5764e7f640dSJohn Baldwin atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 5774e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5784e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx); 579f9819486SAttilio Rao return (0); 5804e7f640dSJohn Baldwin } 5814e7f640dSJohn Baldwin 5824e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 5834e7f640dSJohn Baldwin CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 5844e7f640dSJohn Baldwin sx->lock_object.lo_name, (void *)sx->sx_lock, file, line); 5854e7f640dSJohn Baldwin 586fb106123SMateusz Guzik #ifdef ADAPTIVE_SX 587fb106123SMateusz Guzik adaptive = ((sx->lock_object.lo_flags & SX_NOADAPTIVE) != 0); 588fb106123SMateusz Guzik #endif 589fb106123SMateusz Guzik 590ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS 591ae7d25a4SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 592ae7d25a4SMateusz Guzik #endif 593ae7d25a4SMateusz Guzik lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 594ae7d25a4SMateusz Guzik &waittime); 595ae7d25a4SMateusz Guzik 596e41d6166SMateusz Guzik #ifdef LOCK_PROFILING 597e41d6166SMateusz Guzik extra_work = 1; 598e41d6166SMateusz Guzik state = x; 599e41d6166SMateusz Guzik #elif defined(KDTRACE_HOOKS) 600e41d6166SMateusz Guzik extra_work = lockstat_enabled; 601e41d6166SMateusz Guzik if (__predict_false(extra_work)) { 602e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 603c5f61e6fSMateusz Guzik state = x; 604e41d6166SMateusz Guzik } 605076dd8ebSAndriy Gapon #endif 606fb106123SMateusz Guzik #ifndef INVARIANTS 607fb106123SMateusz Guzik GIANT_SAVE(extra_work); 608fb106123SMateusz Guzik #endif 609e41d6166SMateusz Guzik 610fc4f686dSMateusz Guzik for (;;) { 611c5f61e6fSMateusz Guzik if (x == SX_LOCK_UNLOCKED) { 612fa474043SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid)) 613fc4f686dSMateusz Guzik break; 614c5f61e6fSMateusz Guzik continue; 615c5f61e6fSMateusz Guzik } 616fb106123SMateusz Guzik #ifdef INVARIANTS 617fb106123SMateusz Guzik GIANT_SAVE(extra_work); 618fb106123SMateusz Guzik #endif 619a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 6201ada9041SMateusz Guzik lda.spin_cnt++; 621a5aedd68SStacey Son #endif 6224e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 623fb106123SMateusz Guzik if (__predict_false(!adaptive)) 624fb106123SMateusz Guzik goto sleepq; 6254e7f640dSJohn Baldwin /* 6264e7f640dSJohn Baldwin * If the lock is write locked and the owner is 6274e7f640dSJohn Baldwin * running on another CPU, spin until the owner stops 6284e7f640dSJohn Baldwin * running or the state of the lock changes. 6294e7f640dSJohn Baldwin */ 6301ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0) { 631c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 6324e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 6334e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6344e7f640dSJohn Baldwin CTR3(KTR_LOCK, 6354e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 6364e7f640dSJohn Baldwin __func__, sx, owner); 6372cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 6382cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 6392cba8dd3SJohn Baldwin "lockname:\"%s\"", 6402cba8dd3SJohn Baldwin sx->lock_object.lo_name); 641c5f61e6fSMateusz Guzik do { 6421ada9041SMateusz Guzik lock_delay(&lda); 643c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 644c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 645c5f61e6fSMateusz Guzik } while (owner != NULL && 646c5f61e6fSMateusz Guzik TD_IS_RUNNING(owner)); 6472cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 6482cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 6494e7f640dSJohn Baldwin continue; 6504e7f640dSJohn Baldwin } 65128f1a9e3SMateusz Guzik sleep_reason = 1; 6521ae1c2a3SAttilio Rao } else if (SX_SHARERS(x) && spintries < asx_retries) { 6532cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 6542cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 6552cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 6561ae1c2a3SAttilio Rao spintries++; 657d07e22cdSMateusz Guzik for (i = 0; i < asx_loops; i += n) { 6581ae1c2a3SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 6591ae1c2a3SAttilio Rao CTR4(KTR_LOCK, 6601ae1c2a3SAttilio Rao "%s: shared spinning on %p with %u and %u", 6611ae1c2a3SAttilio Rao __func__, sx, spintries, i); 662d07e22cdSMateusz Guzik n = SX_SHARERS(x); 663d07e22cdSMateusz Guzik lock_delay_spin(n); 66420a15d17SMateusz Guzik x = SX_READ_VALUE(sx); 6651ae1c2a3SAttilio Rao if ((x & SX_LOCK_SHARED) == 0 || 6661ae1c2a3SAttilio Rao SX_SHARERS(x) == 0) 6671ae1c2a3SAttilio Rao break; 6681ae1c2a3SAttilio Rao } 66920a15d17SMateusz Guzik #ifdef KDTRACE_HOOKS 67020a15d17SMateusz Guzik lda.spin_cnt += i; 67120a15d17SMateusz Guzik #endif 6722cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 6732cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 674*efa9f177SMateusz Guzik if (i < asx_loops) 6751ae1c2a3SAttilio Rao continue; 67628f1a9e3SMateusz Guzik sleep_reason = 2; 6771ae1c2a3SAttilio Rao } 678fb106123SMateusz Guzik sleepq: 679cde25ed4SMateusz Guzik #endif 6804e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 681c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 68293118b62SMateusz Guzik retry_sleepq: 6834e7f640dSJohn Baldwin 6844e7f640dSJohn Baldwin /* 6854e7f640dSJohn Baldwin * If the lock was released while spinning on the 6864e7f640dSJohn Baldwin * sleep queue chain lock, try again. 6874e7f640dSJohn Baldwin */ 6884e7f640dSJohn Baldwin if (x == SX_LOCK_UNLOCKED) { 6894e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 6904e7f640dSJohn Baldwin continue; 6914e7f640dSJohn Baldwin } 6924e7f640dSJohn Baldwin 6934e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 6944e7f640dSJohn Baldwin /* 6954e7f640dSJohn Baldwin * The current lock owner might have started executing 6964e7f640dSJohn Baldwin * on another CPU (or the lock could have changed 6974e7f640dSJohn Baldwin * owners) while we were waiting on the sleep queue 6984e7f640dSJohn Baldwin * chain lock. If so, drop the sleep queue lock and try 6994e7f640dSJohn Baldwin * again. 7004e7f640dSJohn Baldwin */ 70128f1a9e3SMateusz Guzik if (adaptive) { 70228f1a9e3SMateusz Guzik if (!(x & SX_LOCK_SHARED)) { 7034e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 7044e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 7054e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 7064e7f640dSJohn Baldwin continue; 7074e7f640dSJohn Baldwin } 70828f1a9e3SMateusz Guzik } else if (SX_SHARERS(x) > 0 && sleep_reason == 1) { 70928f1a9e3SMateusz Guzik sleepq_release(&sx->lock_object); 71028f1a9e3SMateusz Guzik continue; 71128f1a9e3SMateusz Guzik } 7124e7f640dSJohn Baldwin } 7134e7f640dSJohn Baldwin #endif 7144e7f640dSJohn Baldwin 7154e7f640dSJohn Baldwin /* 7164e7f640dSJohn Baldwin * If an exclusive lock was released with both shared 7174e7f640dSJohn Baldwin * and exclusive waiters and a shared waiter hasn't 7184e7f640dSJohn Baldwin * woken up and acquired the lock yet, sx_lock will be 7194e7f640dSJohn Baldwin * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS. 7204e7f640dSJohn Baldwin * If we see that value, try to acquire it once. Note 7214e7f640dSJohn Baldwin * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS 7224e7f640dSJohn Baldwin * as there are other exclusive waiters still. If we 7234e7f640dSJohn Baldwin * fail, restart the loop. 7244e7f640dSJohn Baldwin */ 7254e7f640dSJohn Baldwin if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) { 72693118b62SMateusz Guzik if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, 72793118b62SMateusz Guzik tid | SX_LOCK_EXCLUSIVE_WAITERS)) 72893118b62SMateusz Guzik goto retry_sleepq; 7294e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 7304e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p claimed by new writer", 7314e7f640dSJohn Baldwin __func__, sx); 7324e7f640dSJohn Baldwin break; 7334e7f640dSJohn Baldwin } 7344e7f640dSJohn Baldwin 7354e7f640dSJohn Baldwin /* 7364e7f640dSJohn Baldwin * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail, 7374e7f640dSJohn Baldwin * than loop back and retry. 7384e7f640dSJohn Baldwin */ 7394e7f640dSJohn Baldwin if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) { 74093118b62SMateusz Guzik if (!atomic_fcmpset_ptr(&sx->sx_lock, &x, 7414e7f640dSJohn Baldwin x | SX_LOCK_EXCLUSIVE_WAITERS)) { 74293118b62SMateusz Guzik goto retry_sleepq; 7434e7f640dSJohn Baldwin } 7444e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7454e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set excl waiters flag", 7464e7f640dSJohn Baldwin __func__, sx); 7474e7f640dSJohn Baldwin } 7484e7f640dSJohn Baldwin 7494e7f640dSJohn Baldwin /* 7504e7f640dSJohn Baldwin * Since we have been unable to acquire the exclusive 7514e7f640dSJohn Baldwin * lock and the exclusive waiters flag is set, we have 7524e7f640dSJohn Baldwin * to sleep. 7534e7f640dSJohn Baldwin */ 7544e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7554e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 7564e7f640dSJohn Baldwin __func__, sx); 7574e7f640dSJohn Baldwin 758a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 759e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 760a5aedd68SStacey Son #endif 7614e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 762f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 763f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE); 764f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 765c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 766f9819486SAttilio Rao else 767c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 768a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 769e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 770a5aedd68SStacey Son sleep_cnt++; 771a5aedd68SStacey Son #endif 772f9819486SAttilio Rao if (error) { 773f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 774f9819486SAttilio Rao CTR2(KTR_LOCK, 775f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 776f9819486SAttilio Rao __func__, sx); 777f9819486SAttilio Rao break; 778f9819486SAttilio Rao } 7794e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 7804e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 7814e7f640dSJohn Baldwin __func__, sx); 782c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 7834e7f640dSJohn Baldwin } 784e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 785e41d6166SMateusz Guzik if (__predict_true(!extra_work)) 786e41d6166SMateusz Guzik return (error); 787e41d6166SMateusz Guzik #endif 788076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 789e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 790076dd8ebSAndriy Gapon if (sleep_time) 79132cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 792076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 793076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 7941ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 79532cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 796076dd8ebSAndriy Gapon LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0, 797076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 798076dd8ebSAndriy Gapon #endif 799f9819486SAttilio Rao if (!error) 800de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 801de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_WRITER); 802076dd8ebSAndriy Gapon GIANT_RESTORE(); 803f9819486SAttilio Rao return (error); 8044e7f640dSJohn Baldwin } 8054e7f640dSJohn Baldwin 8064e7f640dSJohn Baldwin /* 8074e7f640dSJohn Baldwin * This function represents the so-called 'hard case' for sx_xunlock 8084e7f640dSJohn Baldwin * operation. All 'easy case' failures are redirected to this. Note 8094e7f640dSJohn Baldwin * that ideally this would be a static function, but it needs to be 8104e7f640dSJohn Baldwin * accessible from at least sx.h. 8114e7f640dSJohn Baldwin */ 8124e7f640dSJohn Baldwin void 813b584eb2eSMateusz Guzik _sx_xunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 8144e7f640dSJohn Baldwin { 815b584eb2eSMateusz Guzik uintptr_t tid, setx; 816da7bbd2cSJohn Baldwin int queue, wakeup_swapper; 8174e7f640dSJohn Baldwin 81835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 81935370593SAndriy Gapon return; 82035370593SAndriy Gapon 821b584eb2eSMateusz Guzik tid = (uintptr_t)curthread; 8224e7f640dSJohn Baldwin 823b584eb2eSMateusz Guzik if (__predict_false(x == tid)) 8243b3cf014SMateusz Guzik x = SX_READ_VALUE(sx); 825b584eb2eSMateusz Guzik 826b584eb2eSMateusz Guzik MPASS(!(x & SX_LOCK_SHARED)); 827b584eb2eSMateusz Guzik 828b584eb2eSMateusz Guzik if (__predict_false(x & SX_LOCK_RECURSED)) { 8296ebb77b6SMateusz Guzik /* The lock is recursed, unrecurse one level. */ 8304e7f640dSJohn Baldwin if ((--sx->sx_recurse) == 0) 8314e7f640dSJohn Baldwin atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED); 8324e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8334e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx); 8344e7f640dSJohn Baldwin return; 8354e7f640dSJohn Baldwin } 8363b3cf014SMateusz Guzik 8373b3cf014SMateusz Guzik LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_WRITER); 8383b3cf014SMateusz Guzik if (x == tid && 8393b3cf014SMateusz Guzik atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED)) 8403b3cf014SMateusz Guzik return; 8413b3cf014SMateusz Guzik 8424e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8434e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p contested", __func__, sx); 8444e7f640dSJohn Baldwin 8454e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 846bc24577cSMateusz Guzik x = SX_READ_VALUE(sx); 8472d96bd88SMateusz Guzik MPASS(x & (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)); 8484e7f640dSJohn Baldwin 8494e7f640dSJohn Baldwin /* 8504e7f640dSJohn Baldwin * The wake up algorithm here is quite simple and probably not 8514e7f640dSJohn Baldwin * ideal. It gives precedence to shared waiters if they are 8524e7f640dSJohn Baldwin * present. For this condition, we have to preserve the 8534e7f640dSJohn Baldwin * state of the exclusive waiters flag. 8542028867dSAttilio Rao * If interruptible sleeps left the shared queue empty avoid a 8552028867dSAttilio Rao * starvation for the threads sleeping on the exclusive queue by giving 8562028867dSAttilio Rao * them precedence and cleaning up the shared waiters bit anyway. 8574e7f640dSJohn Baldwin */ 858bc24577cSMateusz Guzik setx = SX_LOCK_UNLOCKED; 859bc24577cSMateusz Guzik queue = SQ_EXCLUSIVE_QUEUE; 860bc24577cSMateusz Guzik if ((x & SX_LOCK_SHARED_WAITERS) != 0 && 8612028867dSAttilio Rao sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) { 8624e7f640dSJohn Baldwin queue = SQ_SHARED_QUEUE; 863bc24577cSMateusz Guzik setx |= (x & SX_LOCK_EXCLUSIVE_WAITERS); 864bc24577cSMateusz Guzik } 865bc24577cSMateusz Guzik atomic_store_rel_ptr(&sx->sx_lock, setx); 8664e7f640dSJohn Baldwin 8674e7f640dSJohn Baldwin /* Wake up all the waiters for the specific queue. */ 8684e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 8694e7f640dSJohn Baldwin CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue", 8704e7f640dSJohn Baldwin __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" : 8714e7f640dSJohn Baldwin "exclusive"); 872bc24577cSMateusz Guzik 873da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0, 874da7bbd2cSJohn Baldwin queue); 875c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 876da7bbd2cSJohn Baldwin if (wakeup_swapper) 877da7bbd2cSJohn Baldwin kick_proc0(); 8784e7f640dSJohn Baldwin } 8794e7f640dSJohn Baldwin 880834f70f3SMateusz Guzik static bool __always_inline 881013c0b49SMateusz Guzik __sx_slock_try(struct sx *sx, uintptr_t *xp LOCK_FILE_LINE_ARG_DEF) 882834f70f3SMateusz Guzik { 883834f70f3SMateusz Guzik 884834f70f3SMateusz Guzik /* 885834f70f3SMateusz Guzik * If no other thread has an exclusive lock then try to bump up 886834f70f3SMateusz Guzik * the count of sharers. Since we have to preserve the state 887834f70f3SMateusz Guzik * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the 888834f70f3SMateusz Guzik * shared lock loop back and retry. 889834f70f3SMateusz Guzik */ 890834f70f3SMateusz Guzik while (*xp & SX_LOCK_SHARED) { 891834f70f3SMateusz Guzik MPASS(!(*xp & SX_LOCK_SHARED_WAITERS)); 892834f70f3SMateusz Guzik if (atomic_fcmpset_acq_ptr(&sx->sx_lock, xp, 893834f70f3SMateusz Guzik *xp + SX_ONE_SHARER)) { 894834f70f3SMateusz Guzik if (LOCK_LOG_TEST(&sx->lock_object, 0)) 895834f70f3SMateusz Guzik CTR4(KTR_LOCK, "%s: %p succeed %p -> %p", 896834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 897834f70f3SMateusz Guzik (void *)(*xp + SX_ONE_SHARER)); 898834f70f3SMateusz Guzik return (true); 899834f70f3SMateusz Guzik } 900834f70f3SMateusz Guzik } 901834f70f3SMateusz Guzik return (false); 902834f70f3SMateusz Guzik } 903834f70f3SMateusz Guzik 904834f70f3SMateusz Guzik static int __noinline 905013c0b49SMateusz Guzik _sx_slock_hard(struct sx *sx, int opts, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 9064e7f640dSJohn Baldwin { 9074e7f640dSJohn Baldwin GIANT_DECLARE; 9084e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 9094e7f640dSJohn Baldwin volatile struct thread *owner; 910fb106123SMateusz Guzik bool adaptive; 9114e7f640dSJohn Baldwin #endif 9121723a064SJeff Roberson #ifdef LOCK_PROFILING 913c1a6d9faSAttilio Rao uint64_t waittime = 0; 914c1a6d9faSAttilio Rao int contested = 0; 9151723a064SJeff Roberson #endif 916c1a6d9faSAttilio Rao int error = 0; 91704126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS) 9181ada9041SMateusz Guzik struct lock_delay_arg lda; 9191ada9041SMateusz Guzik #endif 920a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 92161852185SMateusz Guzik u_int sleep_cnt = 0; 922a5aedd68SStacey Son int64_t sleep_time = 0; 923076dd8ebSAndriy Gapon int64_t all_time = 0; 924a5aedd68SStacey Son #endif 925e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 926e41d6166SMateusz Guzik uintptr_t state; 927e41d6166SMateusz Guzik #endif 928284194f1SMateusz Guzik int extra_work = 0; 929c1a6d9faSAttilio Rao 93035370593SAndriy Gapon if (SCHEDULER_STOPPED()) 93135370593SAndriy Gapon return (0); 93235370593SAndriy Gapon 933fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX) 9341ada9041SMateusz Guzik lock_delay_arg_init(&lda, &sx_delay); 935fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS) 936fa5000a4SMateusz Guzik lock_delay_arg_init(&lda, NULL); 9371ada9041SMateusz Guzik #endif 938e41d6166SMateusz Guzik 939fb106123SMateusz Guzik #ifdef ADAPTIVE_SX 940fb106123SMateusz Guzik adaptive = ((sx->lock_object.lo_flags & SX_NOADAPTIVE) != 0); 941fb106123SMateusz Guzik #endif 942fb106123SMateusz Guzik 943ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS 944ae7d25a4SMateusz Guzik PMC_SOFT_CALL( , , lock, failed); 945ae7d25a4SMateusz Guzik #endif 946ae7d25a4SMateusz Guzik lock_profile_obtain_lock_failed(&sx->lock_object, &contested, 947ae7d25a4SMateusz Guzik &waittime); 948ae7d25a4SMateusz Guzik 949e41d6166SMateusz Guzik #ifdef LOCK_PROFILING 950e41d6166SMateusz Guzik extra_work = 1; 951e41d6166SMateusz Guzik state = x; 952e41d6166SMateusz Guzik #elif defined(KDTRACE_HOOKS) 953e41d6166SMateusz Guzik extra_work = lockstat_enabled; 954e41d6166SMateusz Guzik if (__predict_false(extra_work)) { 955e2b25737SMark Johnston all_time -= lockstat_nsecs(&sx->lock_object); 956c5f61e6fSMateusz Guzik state = x; 957e41d6166SMateusz Guzik } 958c5f61e6fSMateusz Guzik #endif 959fb106123SMateusz Guzik #ifndef INVARIANTS 960fb106123SMateusz Guzik GIANT_SAVE(extra_work); 961fb106123SMateusz Guzik #endif 962076dd8ebSAndriy Gapon 9634e7f640dSJohn Baldwin /* 9644e7f640dSJohn Baldwin * As with rwlocks, we don't make any attempt to try to block 9654e7f640dSJohn Baldwin * shared locks once there is an exclusive waiter. 9664e7f640dSJohn Baldwin */ 9674e7f640dSJohn Baldwin for (;;) { 968013c0b49SMateusz Guzik if (__sx_slock_try(sx, &x LOCK_FILE_LINE_ARG)) 9694e7f640dSJohn Baldwin break; 970fb106123SMateusz Guzik #ifdef INVARIANTS 971fb106123SMateusz Guzik GIANT_SAVE(extra_work); 972fb106123SMateusz Guzik #endif 973c5f61e6fSMateusz Guzik #ifdef KDTRACE_HOOKS 974c5f61e6fSMateusz Guzik lda.spin_cnt++; 975c5f61e6fSMateusz Guzik #endif 976c5f61e6fSMateusz Guzik 9774e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 978fb106123SMateusz Guzik if (__predict_false(!adaptive)) 979fb106123SMateusz Guzik goto sleepq; 9804e7f640dSJohn Baldwin /* 9814e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 9824e7f640dSJohn Baldwin * the owner stops running or the state of the lock 9834e7f640dSJohn Baldwin * changes. 9844e7f640dSJohn Baldwin */ 985c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 9864e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 9874e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 9884e7f640dSJohn Baldwin CTR3(KTR_LOCK, 9894e7f640dSJohn Baldwin "%s: spinning on %p held by %p", 9904e7f640dSJohn Baldwin __func__, sx, owner); 9912cba8dd3SJohn Baldwin KTR_STATE1(KTR_SCHED, "thread", 9922cba8dd3SJohn Baldwin sched_tdname(curthread), "spinning", 9932cba8dd3SJohn Baldwin "lockname:\"%s\"", sx->lock_object.lo_name); 994c5f61e6fSMateusz Guzik do { 9951ada9041SMateusz Guzik lock_delay(&lda); 996c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 997c5f61e6fSMateusz Guzik owner = lv_sx_owner(x); 998c5f61e6fSMateusz Guzik } while (owner != NULL && TD_IS_RUNNING(owner)); 9992cba8dd3SJohn Baldwin KTR_STATE0(KTR_SCHED, "thread", 10002cba8dd3SJohn Baldwin sched_tdname(curthread), "running"); 10014e7f640dSJohn Baldwin continue; 10024e7f640dSJohn Baldwin } 1003cde25ed4SMateusz Guzik sleepq: 10044e7f640dSJohn Baldwin #endif 10054e7f640dSJohn Baldwin 10064e7f640dSJohn Baldwin /* 10074e7f640dSJohn Baldwin * Some other thread already has an exclusive lock, so 10084e7f640dSJohn Baldwin * start the process of blocking. 10094e7f640dSJohn Baldwin */ 10104e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 1011c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 101293118b62SMateusz Guzik retry_sleepq: 10134e7f640dSJohn Baldwin /* 10144e7f640dSJohn Baldwin * The lock could have been released while we spun. 10154e7f640dSJohn Baldwin * In this case loop back and retry. 10164e7f640dSJohn Baldwin */ 10174e7f640dSJohn Baldwin if (x & SX_LOCK_SHARED) { 10184e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 10194e7f640dSJohn Baldwin continue; 10204e7f640dSJohn Baldwin } 10214e7f640dSJohn Baldwin 10224e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX 10234e7f640dSJohn Baldwin /* 10244e7f640dSJohn Baldwin * If the owner is running on another CPU, spin until 10254e7f640dSJohn Baldwin * the owner stops running or the state of the lock 10264e7f640dSJohn Baldwin * changes. 10274e7f640dSJohn Baldwin */ 1028fb106123SMateusz Guzik if (!(x & SX_LOCK_SHARED) && adaptive) { 10294e7f640dSJohn Baldwin owner = (struct thread *)SX_OWNER(x); 10304e7f640dSJohn Baldwin if (TD_IS_RUNNING(owner)) { 10314e7f640dSJohn Baldwin sleepq_release(&sx->lock_object); 1032c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10334e7f640dSJohn Baldwin continue; 10344e7f640dSJohn Baldwin } 10354e7f640dSJohn Baldwin } 10364e7f640dSJohn Baldwin #endif 10374e7f640dSJohn Baldwin 10384e7f640dSJohn Baldwin /* 10394e7f640dSJohn Baldwin * Try to set the SX_LOCK_SHARED_WAITERS flag. If we 10404e7f640dSJohn Baldwin * fail to set it drop the sleep queue lock and loop 10414e7f640dSJohn Baldwin * back. 10424e7f640dSJohn Baldwin */ 10434e7f640dSJohn Baldwin if (!(x & SX_LOCK_SHARED_WAITERS)) { 104493118b62SMateusz Guzik if (!atomic_fcmpset_ptr(&sx->sx_lock, &x, 104593118b62SMateusz Guzik x | SX_LOCK_SHARED_WAITERS)) 104693118b62SMateusz Guzik goto retry_sleepq; 10474e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10484e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p set shared waiters flag", 10494e7f640dSJohn Baldwin __func__, sx); 10504e7f640dSJohn Baldwin } 10514e7f640dSJohn Baldwin 10524e7f640dSJohn Baldwin /* 10534e7f640dSJohn Baldwin * Since we have been unable to acquire the shared lock, 10544e7f640dSJohn Baldwin * we have to sleep. 10554e7f640dSJohn Baldwin */ 10564e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10574e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p blocking on sleep queue", 10584e7f640dSJohn Baldwin __func__, sx); 10594e7f640dSJohn Baldwin 1060a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 1061e2b25737SMark Johnston sleep_time -= lockstat_nsecs(&sx->lock_object); 1062a5aedd68SStacey Son #endif 10634e7f640dSJohn Baldwin sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name, 1064f9819486SAttilio Rao SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ? 1065f9819486SAttilio Rao SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE); 1066f9819486SAttilio Rao if (!(opts & SX_INTERRUPTIBLE)) 1067c5aa6b58SJeff Roberson sleepq_wait(&sx->lock_object, 0); 1068f9819486SAttilio Rao else 1069c5aa6b58SJeff Roberson error = sleepq_wait_sig(&sx->lock_object, 0); 1070a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 1071e2b25737SMark Johnston sleep_time += lockstat_nsecs(&sx->lock_object); 1072a5aedd68SStacey Son sleep_cnt++; 1073a5aedd68SStacey Son #endif 1074f9819486SAttilio Rao if (error) { 1075f9819486SAttilio Rao if (LOCK_LOG_TEST(&sx->lock_object, 0)) 1076f9819486SAttilio Rao CTR2(KTR_LOCK, 1077f9819486SAttilio Rao "%s: interruptible sleep by %p suspended by signal", 1078f9819486SAttilio Rao __func__, sx); 1079f9819486SAttilio Rao break; 1080f9819486SAttilio Rao } 10814e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 10824e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p resuming from sleep queue", 10834e7f640dSJohn Baldwin __func__, sx); 1084c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 10854e7f640dSJohn Baldwin } 1086e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING) 1087e41d6166SMateusz Guzik if (__predict_true(!extra_work)) 1088e41d6166SMateusz Guzik return (error); 1089e41d6166SMateusz Guzik #endif 1090076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS 1091e2b25737SMark Johnston all_time += lockstat_nsecs(&sx->lock_object); 1092076dd8ebSAndriy Gapon if (sleep_time) 109332cd0147SMark Johnston LOCKSTAT_RECORD4(sx__block, sx, sleep_time, 1094076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1095076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 10961ada9041SMateusz Guzik if (lda.spin_cnt > sleep_cnt) 109732cd0147SMark Johnston LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time, 1098076dd8ebSAndriy Gapon LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0, 1099076dd8ebSAndriy Gapon (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state)); 1100076dd8ebSAndriy Gapon #endif 11013ae56ce9SMateusz Guzik if (error == 0) { 1102de2c95ccSMark Johnston LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx, 1103de2c95ccSMark Johnston contested, waittime, file, line, LOCKSTAT_READER); 11043ae56ce9SMateusz Guzik } 11054e7f640dSJohn Baldwin GIANT_RESTORE(); 1106f9819486SAttilio Rao return (error); 11074e7f640dSJohn Baldwin } 11084e7f640dSJohn Baldwin 1109834f70f3SMateusz Guzik int 1110013c0b49SMateusz Guzik _sx_slock_int(struct sx *sx, int opts LOCK_FILE_LINE_ARG_DEF) 11114e7f640dSJohn Baldwin { 11124e7f640dSJohn Baldwin uintptr_t x; 1113834f70f3SMateusz Guzik int error; 11144e7f640dSJohn Baldwin 1115704cb42fSMark Johnston KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() || 1116704cb42fSMark Johnston !TD_IS_IDLETHREAD(curthread), 1117834f70f3SMateusz Guzik ("sx_slock() by idle thread %p on sx %s @ %s:%d", 1118834f70f3SMateusz Guzik curthread, sx->lock_object.lo_name, file, line)); 11193ae56ce9SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1120834f70f3SMateusz Guzik ("sx_slock() of destroyed sx @ %s:%d", file, line)); 1121834f70f3SMateusz Guzik WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL); 1122834f70f3SMateusz Guzik 1123834f70f3SMateusz Guzik error = 0; 1124c5f61e6fSMateusz Guzik x = SX_READ_VALUE(sx); 1125834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__acquire) || 1126013c0b49SMateusz Guzik !__sx_slock_try(sx, &x LOCK_FILE_LINE_ARG))) 1127013c0b49SMateusz Guzik error = _sx_slock_hard(sx, opts, x LOCK_FILE_LINE_ARG); 1128834f70f3SMateusz Guzik if (error == 0) { 1129834f70f3SMateusz Guzik LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line); 1130834f70f3SMateusz Guzik WITNESS_LOCK(&sx->lock_object, 0, file, line); 1131834f70f3SMateusz Guzik TD_LOCKS_INC(curthread); 1132834f70f3SMateusz Guzik } 1133834f70f3SMateusz Guzik return (error); 1134834f70f3SMateusz Guzik } 1135834f70f3SMateusz Guzik 1136013c0b49SMateusz Guzik int 1137013c0b49SMateusz Guzik _sx_slock(struct sx *sx, int opts, const char *file, int line) 1138013c0b49SMateusz Guzik { 1139013c0b49SMateusz Guzik 1140013c0b49SMateusz Guzik return (_sx_slock_int(sx, opts LOCK_FILE_LINE_ARG)); 1141013c0b49SMateusz Guzik } 1142013c0b49SMateusz Guzik 1143834f70f3SMateusz Guzik static bool __always_inline 1144834f70f3SMateusz Guzik _sx_sunlock_try(struct sx *sx, uintptr_t *xp) 1145834f70f3SMateusz Guzik { 1146834f70f3SMateusz Guzik 11474e7f640dSJohn Baldwin for (;;) { 11484e7f640dSJohn Baldwin /* 11494e7f640dSJohn Baldwin * We should never have sharers while at least one thread 11504e7f640dSJohn Baldwin * holds a shared lock. 11514e7f640dSJohn Baldwin */ 1152834f70f3SMateusz Guzik KASSERT(!(*xp & SX_LOCK_SHARED_WAITERS), 11534e7f640dSJohn Baldwin ("%s: waiting sharers", __func__)); 11544e7f640dSJohn Baldwin 11554e7f640dSJohn Baldwin /* 11564e7f640dSJohn Baldwin * See if there is more than one shared lock held. If 11574e7f640dSJohn Baldwin * so, just drop one and return. 11584e7f640dSJohn Baldwin */ 1159834f70f3SMateusz Guzik if (SX_SHARERS(*xp) > 1) { 1160834f70f3SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, xp, 1161834f70f3SMateusz Guzik *xp - SX_ONE_SHARER)) { 11624e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 11634e7f640dSJohn Baldwin CTR4(KTR_LOCK, 11644e7f640dSJohn Baldwin "%s: %p succeeded %p -> %p", 1165834f70f3SMateusz Guzik __func__, sx, (void *)*xp, 1166834f70f3SMateusz Guzik (void *)(*xp - SX_ONE_SHARER)); 1167834f70f3SMateusz Guzik return (true); 11684e7f640dSJohn Baldwin } 11694e7f640dSJohn Baldwin continue; 11704e7f640dSJohn Baldwin } 11714e7f640dSJohn Baldwin 11724e7f640dSJohn Baldwin /* 11734e7f640dSJohn Baldwin * If there aren't any waiters for an exclusive lock, 11744e7f640dSJohn Baldwin * then try to drop it quickly. 11754e7f640dSJohn Baldwin */ 1176834f70f3SMateusz Guzik if (!(*xp & SX_LOCK_EXCLUSIVE_WAITERS)) { 1177834f70f3SMateusz Guzik MPASS(*xp == SX_SHARERS_LOCK(1)); 1178834f70f3SMateusz Guzik *xp = SX_SHARERS_LOCK(1); 1179fa474043SMateusz Guzik if (atomic_fcmpset_rel_ptr(&sx->sx_lock, 1180834f70f3SMateusz Guzik xp, SX_LOCK_UNLOCKED)) { 11814e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 11824e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p last succeeded", 11834e7f640dSJohn Baldwin __func__, sx); 1184834f70f3SMateusz Guzik return (true); 11854e7f640dSJohn Baldwin } 11864e7f640dSJohn Baldwin continue; 11874e7f640dSJohn Baldwin } 1188834f70f3SMateusz Guzik break; 1189834f70f3SMateusz Guzik } 1190834f70f3SMateusz Guzik return (false); 1191834f70f3SMateusz Guzik } 1192834f70f3SMateusz Guzik 1193834f70f3SMateusz Guzik static void __noinline 1194013c0b49SMateusz Guzik _sx_sunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF) 1195834f70f3SMateusz Guzik { 1196834f70f3SMateusz Guzik int wakeup_swapper; 1197cec17473SMateusz Guzik uintptr_t setx; 1198834f70f3SMateusz Guzik 1199834f70f3SMateusz Guzik if (SCHEDULER_STOPPED()) 1200834f70f3SMateusz Guzik return; 1201834f70f3SMateusz Guzik 1202834f70f3SMateusz Guzik if (_sx_sunlock_try(sx, &x)) 1203cec17473SMateusz Guzik goto out_lockstat; 12044e7f640dSJohn Baldwin 12054e7f640dSJohn Baldwin /* 12064e7f640dSJohn Baldwin * At this point, there should just be one sharer with 12074e7f640dSJohn Baldwin * exclusive waiters. 12084e7f640dSJohn Baldwin */ 12094e7f640dSJohn Baldwin MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS)); 12104e7f640dSJohn Baldwin 12114e7f640dSJohn Baldwin sleepq_lock(&sx->lock_object); 1212cec17473SMateusz Guzik x = SX_READ_VALUE(sx); 1213cec17473SMateusz Guzik for (;;) { 1214cec17473SMateusz Guzik MPASS(x & SX_LOCK_EXCLUSIVE_WAITERS); 1215cec17473SMateusz Guzik MPASS(!(x & SX_LOCK_SHARED_WAITERS)); 12164e7f640dSJohn Baldwin /* 12174e7f640dSJohn Baldwin * Wake up semantic here is quite simple: 12184e7f640dSJohn Baldwin * Just wake up all the exclusive waiters. 12194e7f640dSJohn Baldwin * Note that the state of the lock could have changed, 12204e7f640dSJohn Baldwin * so if it fails loop back and retry. 12214e7f640dSJohn Baldwin */ 1222cec17473SMateusz Guzik setx = x - SX_ONE_SHARER; 1223cec17473SMateusz Guzik setx &= ~SX_LOCK_EXCLUSIVE_WAITERS; 1224cec17473SMateusz Guzik if (!atomic_fcmpset_rel_ptr(&sx->sx_lock, &x, setx)) 12254e7f640dSJohn Baldwin continue; 12264e7f640dSJohn Baldwin if (LOCK_LOG_TEST(&sx->lock_object, 0)) 12274e7f640dSJohn Baldwin CTR2(KTR_LOCK, "%s: %p waking up all thread on" 12284e7f640dSJohn Baldwin "exclusive queue", __func__, sx); 1229da7bbd2cSJohn Baldwin wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 1230da7bbd2cSJohn Baldwin 0, SQ_EXCLUSIVE_QUEUE); 1231cec17473SMateusz Guzik break; 1232cec17473SMateusz Guzik } 1233c5aa6b58SJeff Roberson sleepq_release(&sx->lock_object); 1234da7bbd2cSJohn Baldwin if (wakeup_swapper) 1235da7bbd2cSJohn Baldwin kick_proc0(); 1236cec17473SMateusz Guzik out_lockstat: 1237dbe4541dSMark Johnston LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER); 1238834f70f3SMateusz Guzik } 1239834f70f3SMateusz Guzik 1240834f70f3SMateusz Guzik void 1241013c0b49SMateusz Guzik _sx_sunlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF) 1242834f70f3SMateusz Guzik { 1243834f70f3SMateusz Guzik uintptr_t x; 1244834f70f3SMateusz Guzik 1245834f70f3SMateusz Guzik KASSERT(sx->sx_lock != SX_LOCK_DESTROYED, 1246834f70f3SMateusz Guzik ("sx_sunlock() of destroyed sx @ %s:%d", file, line)); 1247834f70f3SMateusz Guzik _sx_assert(sx, SA_SLOCKED, file, line); 1248834f70f3SMateusz Guzik WITNESS_UNLOCK(&sx->lock_object, 0, file, line); 1249834f70f3SMateusz Guzik LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line); 1250834f70f3SMateusz Guzik 1251834f70f3SMateusz Guzik x = SX_READ_VALUE(sx); 1252834f70f3SMateusz Guzik if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__release) || 1253834f70f3SMateusz Guzik !_sx_sunlock_try(sx, &x))) 1254013c0b49SMateusz Guzik _sx_sunlock_hard(sx, x LOCK_FILE_LINE_ARG); 1255834f70f3SMateusz Guzik 12563ae56ce9SMateusz Guzik TD_LOCKS_DEC(curthread); 1257d55229b7SJason Evans } 12584e5e677bSJohn Baldwin 1259013c0b49SMateusz Guzik void 1260013c0b49SMateusz Guzik _sx_sunlock(struct sx *sx, const char *file, int line) 1261013c0b49SMateusz Guzik { 1262013c0b49SMateusz Guzik 1263013c0b49SMateusz Guzik _sx_sunlock_int(sx LOCK_FILE_LINE_ARG); 1264013c0b49SMateusz Guzik } 1265013c0b49SMateusz Guzik 12664e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT 1267781a35dfSJohn Baldwin #ifndef INVARIANTS 1268781a35dfSJohn Baldwin #undef _sx_assert 1269781a35dfSJohn Baldwin #endif 1270781a35dfSJohn Baldwin 12714e5e677bSJohn Baldwin /* 12724e5e677bSJohn Baldwin * In the non-WITNESS case, sx_assert() can only detect that at least 12734e5e677bSJohn Baldwin * *some* thread owns an slock, but it cannot guarantee that *this* 12744e5e677bSJohn Baldwin * thread owns an slock. 12754e5e677bSJohn Baldwin */ 12764e5e677bSJohn Baldwin void 1277d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line) 12784e5e677bSJohn Baldwin { 12794e7f640dSJohn Baldwin #ifndef WITNESS 12804e7f640dSJohn Baldwin int slocked = 0; 12814e7f640dSJohn Baldwin #endif 12824e5e677bSJohn Baldwin 128303129ba9SJohn Baldwin if (panicstr != NULL) 128403129ba9SJohn Baldwin return; 12854e5e677bSJohn Baldwin switch (what) { 12867ec137e5SJohn Baldwin case SA_SLOCKED: 12877ec137e5SJohn Baldwin case SA_SLOCKED | SA_NOTRECURSED: 12887ec137e5SJohn Baldwin case SA_SLOCKED | SA_RECURSED: 12894e7f640dSJohn Baldwin #ifndef WITNESS 12904e7f640dSJohn Baldwin slocked = 1; 12914e7f640dSJohn Baldwin /* FALLTHROUGH */ 12924e7f640dSJohn Baldwin #endif 12937ec137e5SJohn Baldwin case SA_LOCKED: 12947ec137e5SJohn Baldwin case SA_LOCKED | SA_NOTRECURSED: 12957ec137e5SJohn Baldwin case SA_LOCKED | SA_RECURSED: 12964e5e677bSJohn Baldwin #ifdef WITNESS 1297aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 12984e5e677bSJohn Baldwin #else 12994e7f640dSJohn Baldwin /* 13004e7f640dSJohn Baldwin * If some other thread has an exclusive lock or we 13014e7f640dSJohn Baldwin * have one and are asserting a shared lock, fail. 13024e7f640dSJohn Baldwin * Also, if no one has a lock at all, fail. 13034e7f640dSJohn Baldwin */ 13044e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED || 13054e7f640dSJohn Baldwin (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked || 13064e7f640dSJohn Baldwin sx_xholder(sx) != curthread))) 130703129ba9SJohn Baldwin panic("Lock %s not %slocked @ %s:%d\n", 13084e7f640dSJohn Baldwin sx->lock_object.lo_name, slocked ? "share " : "", 13094e7f640dSJohn Baldwin file, line); 13104e7f640dSJohn Baldwin 13114e7f640dSJohn Baldwin if (!(sx->sx_lock & SX_LOCK_SHARED)) { 13124e7f640dSJohn Baldwin if (sx_recursed(sx)) { 13137ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 13144e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 13154e7f640dSJohn Baldwin sx->lock_object.lo_name, file, 13164e7f640dSJohn Baldwin line); 13177ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 13184e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 13194e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13204e7f640dSJohn Baldwin } 13214e5e677bSJohn Baldwin #endif 13224e5e677bSJohn Baldwin break; 13237ec137e5SJohn Baldwin case SA_XLOCKED: 13247ec137e5SJohn Baldwin case SA_XLOCKED | SA_NOTRECURSED: 13257ec137e5SJohn Baldwin case SA_XLOCKED | SA_RECURSED: 13264e7f640dSJohn Baldwin if (sx_xholder(sx) != curthread) 132703129ba9SJohn Baldwin panic("Lock %s not exclusively locked @ %s:%d\n", 1328aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 13294e7f640dSJohn Baldwin if (sx_recursed(sx)) { 13307ec137e5SJohn Baldwin if (what & SA_NOTRECURSED) 13314e7f640dSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 13324e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13337ec137e5SJohn Baldwin } else if (what & SA_RECURSED) 13344e7f640dSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 13354e7f640dSJohn Baldwin sx->lock_object.lo_name, file, line); 13364e5e677bSJohn Baldwin break; 13377ec137e5SJohn Baldwin case SA_UNLOCKED: 133819b0efd3SPawel Jakub Dawidek #ifdef WITNESS 1339aa89d8cdSJohn Baldwin witness_assert(&sx->lock_object, what, file, line); 134019b0efd3SPawel Jakub Dawidek #else 1341f6739b1dSPawel Jakub Dawidek /* 13424e7f640dSJohn Baldwin * If we hold an exclusve lock fail. We can't 13434e7f640dSJohn Baldwin * reliably check to see if we hold a shared lock or 13444e7f640dSJohn Baldwin * not. 1345f6739b1dSPawel Jakub Dawidek */ 13464e7f640dSJohn Baldwin if (sx_xholder(sx) == curthread) 134703129ba9SJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 1348aa89d8cdSJohn Baldwin sx->lock_object.lo_name, file, line); 134919b0efd3SPawel Jakub Dawidek #endif 135019b0efd3SPawel Jakub Dawidek break; 13514e5e677bSJohn Baldwin default: 13524e5e677bSJohn Baldwin panic("Unknown sx lock assertion: %d @ %s:%d", what, file, 13534e5e677bSJohn Baldwin line); 13544e5e677bSJohn Baldwin } 13554e5e677bSJohn Baldwin } 13564e5e677bSJohn Baldwin #endif /* INVARIANT_SUPPORT */ 1357d272fe53SJohn Baldwin 1358d272fe53SJohn Baldwin #ifdef DDB 13594e7f640dSJohn Baldwin static void 1360d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock) 1361d272fe53SJohn Baldwin { 1362d272fe53SJohn Baldwin struct thread *td; 1363d576deedSPawel Jakub Dawidek const struct sx *sx; 1364d272fe53SJohn Baldwin 1365d576deedSPawel Jakub Dawidek sx = (const struct sx *)lock; 1366d272fe53SJohn Baldwin 1367d272fe53SJohn Baldwin db_printf(" state: "); 13684e7f640dSJohn Baldwin if (sx->sx_lock == SX_LOCK_UNLOCKED) 13694e7f640dSJohn Baldwin db_printf("UNLOCKED\n"); 13700026c92cSJohn Baldwin else if (sx->sx_lock == SX_LOCK_DESTROYED) { 13710026c92cSJohn Baldwin db_printf("DESTROYED\n"); 13720026c92cSJohn Baldwin return; 13730026c92cSJohn Baldwin } else if (sx->sx_lock & SX_LOCK_SHARED) 13744e7f640dSJohn Baldwin db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock)); 13754e7f640dSJohn Baldwin else { 13764e7f640dSJohn Baldwin td = sx_xholder(sx); 1377d272fe53SJohn Baldwin db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 1378431f8906SJulian Elischer td->td_tid, td->td_proc->p_pid, td->td_name); 13794e7f640dSJohn Baldwin if (sx_recursed(sx)) 13804e7f640dSJohn Baldwin db_printf(" recursed: %d\n", sx->sx_recurse); 13814e7f640dSJohn Baldwin } 13824e7f640dSJohn Baldwin 13834e7f640dSJohn Baldwin db_printf(" waiters: "); 13844e7f640dSJohn Baldwin switch(sx->sx_lock & 13854e7f640dSJohn Baldwin (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) { 13864e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS: 13874e7f640dSJohn Baldwin db_printf("shared\n"); 13884e7f640dSJohn Baldwin break; 13894e7f640dSJohn Baldwin case SX_LOCK_EXCLUSIVE_WAITERS: 13904e7f640dSJohn Baldwin db_printf("exclusive\n"); 13914e7f640dSJohn Baldwin break; 13924e7f640dSJohn Baldwin case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS: 13934e7f640dSJohn Baldwin db_printf("exclusive and shared\n"); 13944e7f640dSJohn Baldwin break; 13954e7f640dSJohn Baldwin default: 13964e7f640dSJohn Baldwin db_printf("none\n"); 13974e7f640dSJohn Baldwin } 1398d272fe53SJohn Baldwin } 1399462a7addSJohn Baldwin 1400462a7addSJohn Baldwin /* 1401462a7addSJohn Baldwin * Check to see if a thread that is blocked on a sleep queue is actually 1402462a7addSJohn Baldwin * blocked on an sx lock. If so, output some details and return true. 1403462a7addSJohn Baldwin * If the lock has an exclusive owner, return that in *ownerp. 1404462a7addSJohn Baldwin */ 1405462a7addSJohn Baldwin int 1406462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp) 1407462a7addSJohn Baldwin { 1408462a7addSJohn Baldwin struct sx *sx; 1409462a7addSJohn Baldwin 1410462a7addSJohn Baldwin /* 14114e7f640dSJohn Baldwin * Check to see if this thread is blocked on an sx lock. 14124e7f640dSJohn Baldwin * First, we check the lock class. If that is ok, then we 14134e7f640dSJohn Baldwin * compare the lock name against the wait message. 1414462a7addSJohn Baldwin */ 14154e7f640dSJohn Baldwin sx = td->td_wchan; 14164e7f640dSJohn Baldwin if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx || 14174e7f640dSJohn Baldwin sx->lock_object.lo_name != td->td_wmesg) 1418462a7addSJohn Baldwin return (0); 1419462a7addSJohn Baldwin 1420462a7addSJohn Baldwin /* We think we have an sx lock, so output some details. */ 1421462a7addSJohn Baldwin db_printf("blocked on sx \"%s\" ", td->td_wmesg); 14224e7f640dSJohn Baldwin *ownerp = sx_xholder(sx); 14234e7f640dSJohn Baldwin if (sx->sx_lock & SX_LOCK_SHARED) 14244e7f640dSJohn Baldwin db_printf("SLOCK (count %ju)\n", 14254e7f640dSJohn Baldwin (uintmax_t)SX_SHARERS(sx->sx_lock)); 14264e7f640dSJohn Baldwin else 1427462a7addSJohn Baldwin db_printf("XLOCK\n"); 1428462a7addSJohn Baldwin return (1); 1429462a7addSJohn Baldwin } 1430d272fe53SJohn Baldwin #endif 1431