xref: /freebsd/sys/kern/kern_sx.c (revision bc24577c252dc551430ee2f7df77f5bafbe5b493)
19454b2d8SWarner Losh /*-
24e7f640dSJohn Baldwin  * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org>
34e7f640dSJohn Baldwin  * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
44e7f640dSJohn Baldwin  * All rights reserved.
56281b30aSJason Evans  *
66281b30aSJason Evans  * Redistribution and use in source and binary forms, with or without
76281b30aSJason Evans  * modification, are permitted provided that the following conditions
86281b30aSJason Evans  * are met:
96281b30aSJason Evans  * 1. Redistributions of source code must retain the above copyright
106281b30aSJason Evans  *    notice(s), this list of conditions and the following disclaimer as
116281b30aSJason Evans  *    the first lines of this file unmodified other than the possible
126281b30aSJason Evans  *    addition of one or more copyright notices.
136281b30aSJason Evans  * 2. Redistributions in binary form must reproduce the above copyright
146281b30aSJason Evans  *    notice(s), this list of conditions and the following disclaimer in the
156281b30aSJason Evans  *    documentation and/or other materials provided with the distribution.
166281b30aSJason Evans  *
176281b30aSJason Evans  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
186281b30aSJason Evans  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
196281b30aSJason Evans  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
206281b30aSJason Evans  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
216281b30aSJason Evans  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
226281b30aSJason Evans  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
236281b30aSJason Evans  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
246281b30aSJason Evans  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256281b30aSJason Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266281b30aSJason Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
276281b30aSJason Evans  * DAMAGE.
286281b30aSJason Evans  */
296281b30aSJason Evans 
306281b30aSJason Evans /*
314e7f640dSJohn Baldwin  * Shared/exclusive locks.  This implementation attempts to ensure
324e7f640dSJohn Baldwin  * deterministic lock granting behavior, so that slocks and xlocks are
334e7f640dSJohn Baldwin  * interleaved.
346281b30aSJason Evans  *
356281b30aSJason Evans  * Priority propagation will not generally raise the priority of lock holders,
366281b30aSJason Evans  * so should not be relied upon in combination with sx locks.
376281b30aSJason Evans  */
386281b30aSJason Evans 
394e7f640dSJohn Baldwin #include "opt_ddb.h"
40f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h"
41e31d0833SAttilio Rao #include "opt_no_adaptive_sx.h"
424e7f640dSJohn Baldwin 
43677b542eSDavid E. O'Brien #include <sys/cdefs.h>
44677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
45677b542eSDavid E. O'Brien 
466281b30aSJason Evans #include <sys/param.h>
477a7ce668SAndriy Gapon #include <sys/systm.h>
48cd2fe4e6SAttilio Rao #include <sys/kdb.h>
490453ade5SMateusz Guzik #include <sys/kernel.h>
506281b30aSJason Evans #include <sys/ktr.h>
5119284646SJohn Baldwin #include <sys/lock.h>
526281b30aSJason Evans #include <sys/mutex.h>
53d272fe53SJohn Baldwin #include <sys/proc.h>
542cba8dd3SJohn Baldwin #include <sys/sched.h>
554e7f640dSJohn Baldwin #include <sys/sleepqueue.h>
566281b30aSJason Evans #include <sys/sx.h>
571ada9041SMateusz Guzik #include <sys/smp.h>
58e31d0833SAttilio Rao #include <sys/sysctl.h>
594e7f640dSJohn Baldwin 
60e31d0833SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
614e7f640dSJohn Baldwin #include <machine/cpu.h>
624e7f640dSJohn Baldwin #endif
636281b30aSJason Evans 
64462a7addSJohn Baldwin #ifdef DDB
65d272fe53SJohn Baldwin #include <ddb/ddb.h>
664e7f640dSJohn Baldwin #endif
67d272fe53SJohn Baldwin 
681ae1c2a3SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
691ae1c2a3SAttilio Rao #define	ADAPTIVE_SX
704e7f640dSJohn Baldwin #endif
714e7f640dSJohn Baldwin 
72f0830182SAttilio Rao CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE);
73c1a6d9faSAttilio Rao 
74f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
75f5f9340bSFabien Thomas #include <sys/pmckern.h>
76f5f9340bSFabien Thomas PMC_SOFT_DECLARE( , , lock, failed);
77f5f9340bSFabien Thomas #endif
78f5f9340bSFabien Thomas 
794e7f640dSJohn Baldwin /* Handy macros for sleep queues. */
804e7f640dSJohn Baldwin #define	SQ_EXCLUSIVE_QUEUE	0
814e7f640dSJohn Baldwin #define	SQ_SHARED_QUEUE		1
824e7f640dSJohn Baldwin 
834e7f640dSJohn Baldwin /*
844e7f640dSJohn Baldwin  * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file.  We
854e7f640dSJohn Baldwin  * drop Giant anytime we have to sleep or if we adaptively spin.
864e7f640dSJohn Baldwin  */
874e7f640dSJohn Baldwin #define	GIANT_DECLARE							\
884e7f640dSJohn Baldwin 	int _giantcnt = 0;						\
894e7f640dSJohn Baldwin 	WITNESS_SAVE_DECL(Giant)					\
904e7f640dSJohn Baldwin 
91e41d6166SMateusz Guzik #define	GIANT_SAVE(work) do {						\
924e7f640dSJohn Baldwin 	if (mtx_owned(&Giant)) {					\
93e41d6166SMateusz Guzik 		work++;							\
944e7f640dSJohn Baldwin 		WITNESS_SAVE(&Giant.lock_object, Giant);		\
954e7f640dSJohn Baldwin 		while (mtx_owned(&Giant)) {				\
964e7f640dSJohn Baldwin 			_giantcnt++;					\
974e7f640dSJohn Baldwin 			mtx_unlock(&Giant);				\
984e7f640dSJohn Baldwin 		}							\
994e7f640dSJohn Baldwin 	}								\
1004e7f640dSJohn Baldwin } while (0)
1014e7f640dSJohn Baldwin 
1024e7f640dSJohn Baldwin #define GIANT_RESTORE() do {						\
1034e7f640dSJohn Baldwin 	if (_giantcnt > 0) {						\
1044e7f640dSJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);			\
1054e7f640dSJohn Baldwin 		while (_giantcnt--)					\
1064e7f640dSJohn Baldwin 			mtx_lock(&Giant);				\
1074e7f640dSJohn Baldwin 		WITNESS_RESTORE(&Giant.lock_object, Giant);		\
1084e7f640dSJohn Baldwin 	}								\
1094e7f640dSJohn Baldwin } while (0)
1104e7f640dSJohn Baldwin 
1114e7f640dSJohn Baldwin /*
112da7d0d1eSJohn Baldwin  * Returns true if an exclusive lock is recursed.  It assumes
113da7d0d1eSJohn Baldwin  * curthread currently has an exclusive lock.
1144e7f640dSJohn Baldwin  */
1154e7f640dSJohn Baldwin #define	sx_recursed(sx)		((sx)->sx_recurse != 0)
1164e7f640dSJohn Baldwin 
117d576deedSPawel Jakub Dawidek static void	assert_sx(const struct lock_object *lock, int what);
1184e7f640dSJohn Baldwin #ifdef DDB
119d576deedSPawel Jakub Dawidek static void	db_show_sx(const struct lock_object *lock);
120d272fe53SJohn Baldwin #endif
1217faf4d90SDavide Italiano static void	lock_sx(struct lock_object *lock, uintptr_t how);
122a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
123d576deedSPawel Jakub Dawidek static int	owner_sx(const struct lock_object *lock, struct thread **owner);
124a5aedd68SStacey Son #endif
1257faf4d90SDavide Italiano static uintptr_t unlock_sx(struct lock_object *lock);
126d272fe53SJohn Baldwin 
12719284646SJohn Baldwin struct lock_class lock_class_sx = {
128ae8dde30SJohn Baldwin 	.lc_name = "sx",
129ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
130f9721b43SAttilio Rao 	.lc_assert = assert_sx,
131d272fe53SJohn Baldwin #ifdef DDB
132ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_sx,
133d272fe53SJohn Baldwin #endif
1346e21afd4SJohn Baldwin 	.lc_lock = lock_sx,
1356e21afd4SJohn Baldwin 	.lc_unlock = unlock_sx,
136a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
137a5aedd68SStacey Son 	.lc_owner = owner_sx,
138a5aedd68SStacey Son #endif
13919284646SJohn Baldwin };
14019284646SJohn Baldwin 
141781a35dfSJohn Baldwin #ifndef INVARIANTS
142781a35dfSJohn Baldwin #define	_sx_assert(sx, what, file, line)
143781a35dfSJohn Baldwin #endif
144781a35dfSJohn Baldwin 
1451ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX
146574adb65SMateusz Guzik static __read_frequently u_int asx_retries = 10;
147574adb65SMateusz Guzik static __read_frequently u_int asx_loops = 10000;
1486472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
149fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
150fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
1511ada9041SMateusz Guzik 
152574adb65SMateusz Guzik static struct lock_delay_config __read_frequently sx_delay;
1531ada9041SMateusz Guzik 
1548e5a3e9aSMateusz Guzik SYSCTL_INT(_debug_sx, OID_AUTO, delay_base, CTLFLAG_RW, &sx_delay.base,
1551ada9041SMateusz Guzik     0, "");
1561ada9041SMateusz Guzik SYSCTL_INT(_debug_sx, OID_AUTO, delay_max, CTLFLAG_RW, &sx_delay.max,
1571ada9041SMateusz Guzik     0, "");
1581ada9041SMateusz Guzik 
1598e5a3e9aSMateusz Guzik LOCK_DELAY_SYSINIT_DEFAULT(sx_delay);
1601ae1c2a3SAttilio Rao #endif
1611ae1c2a3SAttilio Rao 
1626281b30aSJason Evans void
163d576deedSPawel Jakub Dawidek assert_sx(const struct lock_object *lock, int what)
164f9721b43SAttilio Rao {
165f9721b43SAttilio Rao 
166d576deedSPawel Jakub Dawidek 	sx_assert((const struct sx *)lock, what);
167f9721b43SAttilio Rao }
168f9721b43SAttilio Rao 
169f9721b43SAttilio Rao void
1707faf4d90SDavide Italiano lock_sx(struct lock_object *lock, uintptr_t how)
1716e21afd4SJohn Baldwin {
1726e21afd4SJohn Baldwin 	struct sx *sx;
1736e21afd4SJohn Baldwin 
1746e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1756e21afd4SJohn Baldwin 	if (how)
1766e21afd4SJohn Baldwin 		sx_slock(sx);
177cf6b879fSDavide Italiano 	else
178cf6b879fSDavide Italiano 		sx_xlock(sx);
1796e21afd4SJohn Baldwin }
1806e21afd4SJohn Baldwin 
1817faf4d90SDavide Italiano uintptr_t
1826e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock)
1836e21afd4SJohn Baldwin {
1846e21afd4SJohn Baldwin 	struct sx *sx;
1856e21afd4SJohn Baldwin 
1866e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1877ec137e5SJohn Baldwin 	sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
1886e21afd4SJohn Baldwin 	if (sx_xlocked(sx)) {
1896e21afd4SJohn Baldwin 		sx_xunlock(sx);
190cf6b879fSDavide Italiano 		return (0);
1916e21afd4SJohn Baldwin 	} else {
1926e21afd4SJohn Baldwin 		sx_sunlock(sx);
193cf6b879fSDavide Italiano 		return (1);
1946e21afd4SJohn Baldwin 	}
1956e21afd4SJohn Baldwin }
1966e21afd4SJohn Baldwin 
197a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
198a5aedd68SStacey Son int
199d576deedSPawel Jakub Dawidek owner_sx(const struct lock_object *lock, struct thread **owner)
200a5aedd68SStacey Son {
201c365a293SMark Johnston 	const struct sx *sx;
202c365a293SMark Johnston 	uintptr_t x;
203a5aedd68SStacey Son 
204c365a293SMark Johnston 	sx = (const struct sx *)lock;
205c365a293SMark Johnston 	x = sx->sx_lock;
206c365a293SMark Johnston 	*owner = NULL;
207a5aedd68SStacey Son 	return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
208c365a293SMark Johnston 	    ((*owner = (struct thread *)SX_OWNER(x)) != NULL));
209a5aedd68SStacey Son }
210a5aedd68SStacey Son #endif
211a5aedd68SStacey Son 
2126e21afd4SJohn Baldwin void
213c27b5699SAndrew R. Reiter sx_sysinit(void *arg)
214c27b5699SAndrew R. Reiter {
215c27b5699SAndrew R. Reiter 	struct sx_args *sargs = arg;
216c27b5699SAndrew R. Reiter 
217e4cd31ddSJeff Roberson 	sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
218c27b5699SAndrew R. Reiter }
219c27b5699SAndrew R. Reiter 
220c27b5699SAndrew R. Reiter void
2214e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts)
2226281b30aSJason Evans {
2234e7f640dSJohn Baldwin 	int flags;
2246281b30aSJason Evans 
225b0d67325SJohn Baldwin 	MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
226fd07ddcfSDmitry Chagin 	    SX_NOPROFILE | SX_NOADAPTIVE | SX_NEW)) == 0);
227353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
228353998acSAttilio Rao 	    ("%s: sx_lock not aligned for %s: %p", __func__, description,
229353998acSAttilio Rao 	    &sx->sx_lock));
230b0d67325SJohn Baldwin 
231f0830182SAttilio Rao 	flags = LO_SLEEPABLE | LO_UPGRADABLE;
2324e7f640dSJohn Baldwin 	if (opts & SX_DUPOK)
2334e7f640dSJohn Baldwin 		flags |= LO_DUPOK;
2344e7f640dSJohn Baldwin 	if (opts & SX_NOPROFILE)
2354e7f640dSJohn Baldwin 		flags |= LO_NOPROFILE;
2364e7f640dSJohn Baldwin 	if (!(opts & SX_NOWITNESS))
2374e7f640dSJohn Baldwin 		flags |= LO_WITNESS;
238f0830182SAttilio Rao 	if (opts & SX_RECURSE)
239f0830182SAttilio Rao 		flags |= LO_RECURSABLE;
2404e7f640dSJohn Baldwin 	if (opts & SX_QUIET)
2414e7f640dSJohn Baldwin 		flags |= LO_QUIET;
242fd07ddcfSDmitry Chagin 	if (opts & SX_NEW)
243fd07ddcfSDmitry Chagin 		flags |= LO_NEW;
2444e7f640dSJohn Baldwin 
245f0830182SAttilio Rao 	flags |= opts & SX_NOADAPTIVE;
246b5fb43e5SJohn Baldwin 	lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
2474e7f640dSJohn Baldwin 	sx->sx_lock = SX_LOCK_UNLOCKED;
2484e7f640dSJohn Baldwin 	sx->sx_recurse = 0;
2496281b30aSJason Evans }
2506281b30aSJason Evans 
2516281b30aSJason Evans void
2526281b30aSJason Evans sx_destroy(struct sx *sx)
2536281b30aSJason Evans {
2546281b30aSJason Evans 
2554e7f640dSJohn Baldwin 	KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
2564e7f640dSJohn Baldwin 	KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
2570026c92cSJohn Baldwin 	sx->sx_lock = SX_LOCK_DESTROYED;
258aa89d8cdSJohn Baldwin 	lock_destroy(&sx->lock_object);
2596281b30aSJason Evans }
2606281b30aSJason Evans 
261f9819486SAttilio Rao int
2629fde98bbSAttilio Rao sx_try_slock_(struct sx *sx, const char *file, int line)
2635f36700aSJohn Baldwin {
2644e7f640dSJohn Baldwin 	uintptr_t x;
2655f36700aSJohn Baldwin 
26635370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
26735370593SAndriy Gapon 		return (1);
26835370593SAndriy Gapon 
269cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
270e3ae0dfeSAttilio Rao 	    ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
271e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
272e3ae0dfeSAttilio Rao 
2734e7f640dSJohn Baldwin 	x = sx->sx_lock;
2745c5df0d9SMateusz Guzik 	for (;;) {
2750026c92cSJohn Baldwin 		KASSERT(x != SX_LOCK_DESTROYED,
2760026c92cSJohn Baldwin 		    ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
277764a938bSPawel Jakub Dawidek 		if (!(x & SX_LOCK_SHARED))
278764a938bSPawel Jakub Dawidek 			break;
2795c5df0d9SMateusz Guzik 		if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, x + SX_ONE_SHARER)) {
280aa89d8cdSJohn Baldwin 			LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
281aa89d8cdSJohn Baldwin 			WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
282de2c95ccSMark Johnston 			LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire,
283de2c95ccSMark Johnston 			    sx, 0, 0, file, line, LOCKSTAT_READER);
284ce1c953eSMark Johnston 			TD_LOCKS_INC(curthread);
2855f36700aSJohn Baldwin 			return (1);
2865f36700aSJohn Baldwin 		}
287764a938bSPawel Jakub Dawidek 	}
2884e7f640dSJohn Baldwin 
2894e7f640dSJohn Baldwin 	LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
2904e7f640dSJohn Baldwin 	return (0);
2915f36700aSJohn Baldwin }
2925f36700aSJohn Baldwin 
293f9819486SAttilio Rao int
294f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line)
2956281b30aSJason Evans {
2966ebb77b6SMateusz Guzik 	uintptr_t tid, x;
297f9819486SAttilio Rao 	int error = 0;
2986281b30aSJason Evans 
299704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
300704cb42fSMark Johnston 	    !TD_IS_IDLETHREAD(curthread),
301e3ae0dfeSAttilio Rao 	    ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
302e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
3030026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3040026c92cSJohn Baldwin 	    ("sx_xlock() of destroyed sx @ %s:%d", file, line));
305aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
30641313430SJohn Baldwin 	    line, NULL);
3076ebb77b6SMateusz Guzik 	tid = (uintptr_t)curthread;
3086ebb77b6SMateusz Guzik 	x = SX_LOCK_UNLOCKED;
3096ebb77b6SMateusz Guzik 	if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid))
3106ebb77b6SMateusz Guzik 		error = _sx_xlock_hard(sx, x, tid, opts, file, line);
3116ebb77b6SMateusz Guzik 	else
3126ebb77b6SMateusz Guzik 		LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx,
3136ebb77b6SMateusz Guzik 		    0, 0, file, line, LOCKSTAT_WRITER);
314f9819486SAttilio Rao 	if (!error) {
315f9819486SAttilio Rao 		LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
316f9819486SAttilio Rao 		    file, line);
317aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
318ce1c953eSMark Johnston 		TD_LOCKS_INC(curthread);
3196281b30aSJason Evans 	}
3206281b30aSJason Evans 
321f9819486SAttilio Rao 	return (error);
322f9819486SAttilio Rao }
323f9819486SAttilio Rao 
3245f36700aSJohn Baldwin int
3259fde98bbSAttilio Rao sx_try_xlock_(struct sx *sx, const char *file, int line)
3265f36700aSJohn Baldwin {
3275c5df0d9SMateusz Guzik 	struct thread *td;
3285c5df0d9SMateusz Guzik 	uintptr_t tid, x;
3294e7f640dSJohn Baldwin 	int rval;
3305c5df0d9SMateusz Guzik 	bool recursed;
3315f36700aSJohn Baldwin 
3325c5df0d9SMateusz Guzik 	td = curthread;
3335c5df0d9SMateusz Guzik 	tid = (uintptr_t)td;
3345c5df0d9SMateusz Guzik 	if (SCHEDULER_STOPPED_TD(td))
33535370593SAndriy Gapon 		return (1);
33635370593SAndriy Gapon 
337704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
338e3ae0dfeSAttilio Rao 	    ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
339e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
3400026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3410026c92cSJohn Baldwin 	    ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
3424e7f640dSJohn Baldwin 
3435c5df0d9SMateusz Guzik 	rval = 1;
3445c5df0d9SMateusz Guzik 	recursed = false;
3455c5df0d9SMateusz Guzik 	x = SX_LOCK_UNLOCKED;
346b247fd39SMateusz Guzik 	for (;;) {
347b247fd39SMateusz Guzik 		if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid))
348b247fd39SMateusz Guzik 			break;
349b247fd39SMateusz Guzik 		if (x == SX_LOCK_UNLOCKED)
350b247fd39SMateusz Guzik 			continue;
3515c5df0d9SMateusz Guzik 		if (x == tid && (sx->lock_object.lo_flags & LO_RECURSABLE)) {
3524e7f640dSJohn Baldwin 			sx->sx_recurse++;
3534e7f640dSJohn Baldwin 			atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
354b247fd39SMateusz Guzik 			break;
3555c5df0d9SMateusz Guzik 		}
356b247fd39SMateusz Guzik 		rval = 0;
357b247fd39SMateusz Guzik 		break;
3585c5df0d9SMateusz Guzik 	}
3595c5df0d9SMateusz Guzik 
3604e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
3614e7f640dSJohn Baldwin 	if (rval) {
3624e7f640dSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
3634e7f640dSJohn Baldwin 		    file, line);
3645c5df0d9SMateusz Guzik 		if (!recursed)
365de2c95ccSMark Johnston 			LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire,
366de2c95ccSMark Johnston 			    sx, 0, 0, file, line, LOCKSTAT_WRITER);
367ce1c953eSMark Johnston 		TD_LOCKS_INC(curthread);
3685f36700aSJohn Baldwin 	}
3694e7f640dSJohn Baldwin 
3704e7f640dSJohn Baldwin 	return (rval);
3715f36700aSJohn Baldwin }
3725f36700aSJohn Baldwin 
3736281b30aSJason Evans void
37419284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line)
3756281b30aSJason Evans {
3766281b30aSJason Evans 
3770026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3780026c92cSJohn Baldwin 	    ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
3797ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED, file, line);
380aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
3814e7f640dSJohn Baldwin 	LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
3824e7f640dSJohn Baldwin 	    line);
3830108a980SMateusz Guzik #if LOCK_DEBUG > 0
3846ebb77b6SMateusz Guzik 	_sx_xunlock_hard(sx, (uintptr_t)curthread, file, line);
385ffd5c94cSMateusz Guzik #else
386ffd5c94cSMateusz Guzik 	__sx_xunlock(sx, curthread, file, line);
387ffd5c94cSMateusz Guzik #endif
388ce1c953eSMark Johnston 	TD_LOCKS_DEC(curthread);
3896281b30aSJason Evans }
390d55229b7SJason Evans 
3914e7f640dSJohn Baldwin /*
3924e7f640dSJohn Baldwin  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
3934e7f640dSJohn Baldwin  * This will only succeed if this thread holds a single shared lock.
3944e7f640dSJohn Baldwin  * Return 1 if if the upgrade succeed, 0 otherwise.
3954e7f640dSJohn Baldwin  */
396d55229b7SJason Evans int
3979fde98bbSAttilio Rao sx_try_upgrade_(struct sx *sx, const char *file, int line)
398d55229b7SJason Evans {
3994e7f640dSJohn Baldwin 	uintptr_t x;
4004e7f640dSJohn Baldwin 	int success;
401d55229b7SJason Evans 
40235370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
40335370593SAndriy Gapon 		return (1);
40435370593SAndriy Gapon 
4050026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4060026c92cSJohn Baldwin 	    ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
4077ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
408d55229b7SJason Evans 
4094e7f640dSJohn Baldwin 	/*
4104e7f640dSJohn Baldwin 	 * Try to switch from one shared lock to an exclusive lock.  We need
4114e7f640dSJohn Baldwin 	 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
4124e7f640dSJohn Baldwin 	 * we will wake up the exclusive waiters when we drop the lock.
4134e7f640dSJohn Baldwin 	 */
4144e7f640dSJohn Baldwin 	x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
415a2101806SMateusz Guzik 	success = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
4164e7f640dSJohn Baldwin 	    (uintptr_t)curthread | x);
4174e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
418a5aedd68SStacey Son 	if (success) {
419aa89d8cdSJohn Baldwin 		WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
420b0b7cb50SJohn Baldwin 		    file, line);
42132cd0147SMark Johnston 		LOCKSTAT_RECORD0(sx__upgrade, sx);
422a5aedd68SStacey Son 	}
4234e7f640dSJohn Baldwin 	return (success);
424d55229b7SJason Evans }
425d55229b7SJason Evans 
4264e7f640dSJohn Baldwin /*
4274e7f640dSJohn Baldwin  * Downgrade an unrecursed exclusive lock into a single shared lock.
4284e7f640dSJohn Baldwin  */
429d55229b7SJason Evans void
4309fde98bbSAttilio Rao sx_downgrade_(struct sx *sx, const char *file, int line)
431d55229b7SJason Evans {
4324e7f640dSJohn Baldwin 	uintptr_t x;
433da7bbd2cSJohn Baldwin 	int wakeup_swapper;
434d55229b7SJason Evans 
43535370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
43635370593SAndriy Gapon 		return;
43735370593SAndriy Gapon 
4380026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4390026c92cSJohn Baldwin 	    ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
4407ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
4414e7f640dSJohn Baldwin #ifndef INVARIANTS
4424e7f640dSJohn Baldwin 	if (sx_recursed(sx))
4434e7f640dSJohn Baldwin 		panic("downgrade of a recursed lock");
4444e7f640dSJohn Baldwin #endif
445d55229b7SJason Evans 
446aa89d8cdSJohn Baldwin 	WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
447d55229b7SJason Evans 
4484e7f640dSJohn Baldwin 	/*
4494e7f640dSJohn Baldwin 	 * Try to switch from an exclusive lock with no shared waiters
4504e7f640dSJohn Baldwin 	 * to one sharer with no shared waiters.  If there are
4514e7f640dSJohn Baldwin 	 * exclusive waiters, we don't need to lock the sleep queue so
4524e7f640dSJohn Baldwin 	 * long as we preserve the flag.  We do one quick try and if
4534e7f640dSJohn Baldwin 	 * that fails we grab the sleepq lock to keep the flags from
4544e7f640dSJohn Baldwin 	 * changing and do it the slow way.
4554e7f640dSJohn Baldwin 	 *
4564e7f640dSJohn Baldwin 	 * We have to lock the sleep queue if there are shared waiters
4574e7f640dSJohn Baldwin 	 * so we can wake them up.
4584e7f640dSJohn Baldwin 	 */
4594e7f640dSJohn Baldwin 	x = sx->sx_lock;
4604e7f640dSJohn Baldwin 	if (!(x & SX_LOCK_SHARED_WAITERS) &&
4614e7f640dSJohn Baldwin 	    atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
4624e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
4634e7f640dSJohn Baldwin 		LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
4644e7f640dSJohn Baldwin 		return;
4654e7f640dSJohn Baldwin 	}
4664e7f640dSJohn Baldwin 
4674e7f640dSJohn Baldwin 	/*
4684e7f640dSJohn Baldwin 	 * Lock the sleep queue so we can read the waiters bits
4694e7f640dSJohn Baldwin 	 * without any races and wakeup any shared waiters.
4704e7f640dSJohn Baldwin 	 */
4714e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
4724e7f640dSJohn Baldwin 
4734e7f640dSJohn Baldwin 	/*
4744e7f640dSJohn Baldwin 	 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
4754e7f640dSJohn Baldwin 	 * shared lock.  If there are any shared waiters, wake them up.
4764e7f640dSJohn Baldwin 	 */
477da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
4784e7f640dSJohn Baldwin 	x = sx->sx_lock;
4794e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
4804e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS));
4814e7f640dSJohn Baldwin 	if (x & SX_LOCK_SHARED_WAITERS)
482da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
483da7bbd2cSJohn Baldwin 		    0, SQ_SHARED_QUEUE);
4844e7f640dSJohn Baldwin 	sleepq_release(&sx->lock_object);
485d55229b7SJason Evans 
486aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
48732cd0147SMark Johnston 	LOCKSTAT_RECORD0(sx__downgrade, sx);
488da7bbd2cSJohn Baldwin 
489da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
490da7bbd2cSJohn Baldwin 		kick_proc0();
4914e7f640dSJohn Baldwin }
492d55229b7SJason Evans 
4934e7f640dSJohn Baldwin /*
4944e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xlock
4954e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
4964e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
4974e7f640dSJohn Baldwin  * accessible from at least sx.h.
4984e7f640dSJohn Baldwin  */
499f9819486SAttilio Rao int
500fa474043SMateusz Guzik _sx_xlock_hard(struct sx *sx, uintptr_t x, uintptr_t tid, int opts,
501fa474043SMateusz Guzik     const char *file, int line)
5024e7f640dSJohn Baldwin {
5034e7f640dSJohn Baldwin 	GIANT_DECLARE;
5044e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5054e7f640dSJohn Baldwin 	volatile struct thread *owner;
506d07e22cdSMateusz Guzik 	u_int i, n, spintries = 0;
5074e7f640dSJohn Baldwin #endif
5081723a064SJeff Roberson #ifdef LOCK_PROFILING
5091723a064SJeff Roberson 	uint64_t waittime = 0;
5101723a064SJeff Roberson 	int contested = 0;
5111723a064SJeff Roberson #endif
5121723a064SJeff Roberson 	int error = 0;
51304126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS)
5141ada9041SMateusz Guzik 	struct lock_delay_arg lda;
5151ada9041SMateusz Guzik #endif
516a5aedd68SStacey Son #ifdef	KDTRACE_HOOKS
51761852185SMateusz Guzik 	u_int sleep_cnt = 0;
518a5aedd68SStacey Son 	int64_t sleep_time = 0;
519076dd8ebSAndriy Gapon 	int64_t all_time = 0;
520a5aedd68SStacey Son #endif
521e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
522e41d6166SMateusz Guzik 	uintptr_t state;
523e41d6166SMateusz Guzik 	int extra_work;
524e41d6166SMateusz Guzik #endif
5254e7f640dSJohn Baldwin 
52635370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
52735370593SAndriy Gapon 		return (0);
52835370593SAndriy Gapon 
529fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX)
5301ada9041SMateusz Guzik 	lock_delay_arg_init(&lda, &sx_delay);
531fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS)
532fa5000a4SMateusz Guzik 	lock_delay_arg_init(&lda, NULL);
5331ada9041SMateusz Guzik #endif
5341ada9041SMateusz Guzik 
535c1aaf63cSMateusz Guzik 	if (__predict_false(x == SX_LOCK_UNLOCKED))
536c1aaf63cSMateusz Guzik 		x = SX_READ_VALUE(sx);
537c1aaf63cSMateusz Guzik 
5384e7f640dSJohn Baldwin 	/* If we already hold an exclusive lock, then recurse. */
539c5f61e6fSMateusz Guzik 	if (__predict_false(lv_sx_owner(x) == (struct thread *)tid)) {
540f0830182SAttilio Rao 		KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
541b0d67325SJohn Baldwin 	    ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
542b0d67325SJohn Baldwin 		    sx->lock_object.lo_name, file, line));
5434e7f640dSJohn Baldwin 		sx->sx_recurse++;
5444e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
5454e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
5464e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
547f9819486SAttilio Rao 		return (0);
5484e7f640dSJohn Baldwin 	}
5494e7f640dSJohn Baldwin 
5504e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
5514e7f640dSJohn Baldwin 		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
5524e7f640dSJohn Baldwin 		    sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
5534e7f640dSJohn Baldwin 
554ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS
555ae7d25a4SMateusz Guzik 	PMC_SOFT_CALL( , , lock, failed);
556ae7d25a4SMateusz Guzik #endif
557ae7d25a4SMateusz Guzik 	lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
558ae7d25a4SMateusz Guzik 	    &waittime);
559ae7d25a4SMateusz Guzik 
560e41d6166SMateusz Guzik #ifdef LOCK_PROFILING
561e41d6166SMateusz Guzik 	extra_work = 1;
562e41d6166SMateusz Guzik 	state = x;
563e41d6166SMateusz Guzik #elif defined(KDTRACE_HOOKS)
564e41d6166SMateusz Guzik 	extra_work = lockstat_enabled;
565e41d6166SMateusz Guzik 	if (__predict_false(extra_work)) {
566e2b25737SMark Johnston 		all_time -= lockstat_nsecs(&sx->lock_object);
567c5f61e6fSMateusz Guzik 		state = x;
568e41d6166SMateusz Guzik 	}
569076dd8ebSAndriy Gapon #endif
570e41d6166SMateusz Guzik 
571fc4f686dSMateusz Guzik 	for (;;) {
572c5f61e6fSMateusz Guzik 		if (x == SX_LOCK_UNLOCKED) {
573fa474043SMateusz Guzik 			if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid))
574fc4f686dSMateusz Guzik 				break;
575c5f61e6fSMateusz Guzik 			continue;
576c5f61e6fSMateusz Guzik 		}
577a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
5781ada9041SMateusz Guzik 		lda.spin_cnt++;
579a5aedd68SStacey Son #endif
5804e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5814e7f640dSJohn Baldwin 		/*
5824e7f640dSJohn Baldwin 		 * If the lock is write locked and the owner is
5834e7f640dSJohn Baldwin 		 * running on another CPU, spin until the owner stops
5844e7f640dSJohn Baldwin 		 * running or the state of the lock changes.
5854e7f640dSJohn Baldwin 		 */
5868545538bSJohn Baldwin 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
5871ae1c2a3SAttilio Rao 			if ((x & SX_LOCK_SHARED) == 0) {
588c5f61e6fSMateusz Guzik 				owner = lv_sx_owner(x);
5894e7f640dSJohn Baldwin 				if (TD_IS_RUNNING(owner)) {
5904e7f640dSJohn Baldwin 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5914e7f640dSJohn Baldwin 						CTR3(KTR_LOCK,
5924e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
5934e7f640dSJohn Baldwin 						    __func__, sx, owner);
5942cba8dd3SJohn Baldwin 					KTR_STATE1(KTR_SCHED, "thread",
5952cba8dd3SJohn Baldwin 					    sched_tdname(curthread), "spinning",
5962cba8dd3SJohn Baldwin 					    "lockname:\"%s\"",
5972cba8dd3SJohn Baldwin 					    sx->lock_object.lo_name);
598e41d6166SMateusz Guzik 					GIANT_SAVE(extra_work);
599c5f61e6fSMateusz Guzik 					do {
6001ada9041SMateusz Guzik 						lock_delay(&lda);
601c5f61e6fSMateusz Guzik 						x = SX_READ_VALUE(sx);
602c5f61e6fSMateusz Guzik 						owner = lv_sx_owner(x);
603c5f61e6fSMateusz Guzik 					} while (owner != NULL &&
604c5f61e6fSMateusz Guzik 						    TD_IS_RUNNING(owner));
6052cba8dd3SJohn Baldwin 					KTR_STATE0(KTR_SCHED, "thread",
6062cba8dd3SJohn Baldwin 					    sched_tdname(curthread), "running");
6074e7f640dSJohn Baldwin 					continue;
6084e7f640dSJohn Baldwin 				}
6091ae1c2a3SAttilio Rao 			} else if (SX_SHARERS(x) && spintries < asx_retries) {
6102cba8dd3SJohn Baldwin 				KTR_STATE1(KTR_SCHED, "thread",
6112cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "spinning",
6122cba8dd3SJohn Baldwin 				    "lockname:\"%s\"", sx->lock_object.lo_name);
613e41d6166SMateusz Guzik 				GIANT_SAVE(extra_work);
6141ae1c2a3SAttilio Rao 				spintries++;
615d07e22cdSMateusz Guzik 				for (i = 0; i < asx_loops; i += n) {
6161ae1c2a3SAttilio Rao 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
6171ae1c2a3SAttilio Rao 						CTR4(KTR_LOCK,
6181ae1c2a3SAttilio Rao 				    "%s: shared spinning on %p with %u and %u",
6191ae1c2a3SAttilio Rao 						    __func__, sx, spintries, i);
620d07e22cdSMateusz Guzik 					n = SX_SHARERS(x);
621d07e22cdSMateusz Guzik 					lock_delay_spin(n);
62220a15d17SMateusz Guzik 					x = SX_READ_VALUE(sx);
6231ae1c2a3SAttilio Rao 					if ((x & SX_LOCK_SHARED) == 0 ||
6241ae1c2a3SAttilio Rao 					    SX_SHARERS(x) == 0)
6251ae1c2a3SAttilio Rao 						break;
6261ae1c2a3SAttilio Rao 				}
62720a15d17SMateusz Guzik #ifdef KDTRACE_HOOKS
62820a15d17SMateusz Guzik 				lda.spin_cnt += i;
62920a15d17SMateusz Guzik #endif
6302cba8dd3SJohn Baldwin 				KTR_STATE0(KTR_SCHED, "thread",
6312cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "running");
6321ae1c2a3SAttilio Rao 				if (i != asx_loops)
6331ae1c2a3SAttilio Rao 					continue;
6341ae1c2a3SAttilio Rao 			}
6354e7f640dSJohn Baldwin 		}
6364e7f640dSJohn Baldwin #endif
6374e7f640dSJohn Baldwin 
6384e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
639c5f61e6fSMateusz Guzik 		x = SX_READ_VALUE(sx);
6404e7f640dSJohn Baldwin 
6414e7f640dSJohn Baldwin 		/*
6424e7f640dSJohn Baldwin 		 * If the lock was released while spinning on the
6434e7f640dSJohn Baldwin 		 * sleep queue chain lock, try again.
6444e7f640dSJohn Baldwin 		 */
6454e7f640dSJohn Baldwin 		if (x == SX_LOCK_UNLOCKED) {
6464e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6474e7f640dSJohn Baldwin 			continue;
6484e7f640dSJohn Baldwin 		}
6494e7f640dSJohn Baldwin 
6504e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
6514e7f640dSJohn Baldwin 		/*
6524e7f640dSJohn Baldwin 		 * The current lock owner might have started executing
6534e7f640dSJohn Baldwin 		 * on another CPU (or the lock could have changed
6544e7f640dSJohn Baldwin 		 * owners) while we were waiting on the sleep queue
6554e7f640dSJohn Baldwin 		 * chain lock.  If so, drop the sleep queue lock and try
6564e7f640dSJohn Baldwin 		 * again.
6574e7f640dSJohn Baldwin 		 */
6584e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
6591ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
6604e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
6614e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
6624e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6634e7f640dSJohn Baldwin 				continue;
6644e7f640dSJohn Baldwin 			}
6654e7f640dSJohn Baldwin 		}
6664e7f640dSJohn Baldwin #endif
6674e7f640dSJohn Baldwin 
6684e7f640dSJohn Baldwin 		/*
6694e7f640dSJohn Baldwin 		 * If an exclusive lock was released with both shared
6704e7f640dSJohn Baldwin 		 * and exclusive waiters and a shared waiter hasn't
6714e7f640dSJohn Baldwin 		 * woken up and acquired the lock yet, sx_lock will be
6724e7f640dSJohn Baldwin 		 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
6734e7f640dSJohn Baldwin 		 * If we see that value, try to acquire it once.  Note
6744e7f640dSJohn Baldwin 		 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
6754e7f640dSJohn Baldwin 		 * as there are other exclusive waiters still.  If we
6764e7f640dSJohn Baldwin 		 * fail, restart the loop.
6774e7f640dSJohn Baldwin 		 */
6784e7f640dSJohn Baldwin 		if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
6794e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock,
6804e7f640dSJohn Baldwin 			    SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
6814e7f640dSJohn Baldwin 			    tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
6824e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6834e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
6844e7f640dSJohn Baldwin 				    __func__, sx);
6854e7f640dSJohn Baldwin 				break;
6864e7f640dSJohn Baldwin 			}
6874e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
688c5f61e6fSMateusz Guzik 			x = SX_READ_VALUE(sx);
6894e7f640dSJohn Baldwin 			continue;
6904e7f640dSJohn Baldwin 		}
6914e7f640dSJohn Baldwin 
6924e7f640dSJohn Baldwin 		/*
6934e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
6944e7f640dSJohn Baldwin 		 * than loop back and retry.
6954e7f640dSJohn Baldwin 		 */
6964e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
6974e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
6984e7f640dSJohn Baldwin 			    x | SX_LOCK_EXCLUSIVE_WAITERS)) {
6994e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
700c5f61e6fSMateusz Guzik 				x = SX_READ_VALUE(sx);
7014e7f640dSJohn Baldwin 				continue;
7024e7f640dSJohn Baldwin 			}
7034e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
7044e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
7054e7f640dSJohn Baldwin 				    __func__, sx);
7064e7f640dSJohn Baldwin 		}
7074e7f640dSJohn Baldwin 
7084e7f640dSJohn Baldwin 		/*
7094e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the exclusive
7104e7f640dSJohn Baldwin 		 * lock and the exclusive waiters flag is set, we have
7114e7f640dSJohn Baldwin 		 * to sleep.
7124e7f640dSJohn Baldwin 		 */
7134e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7144e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
7154e7f640dSJohn Baldwin 			    __func__, sx);
7164e7f640dSJohn Baldwin 
717a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
718e2b25737SMark Johnston 		sleep_time -= lockstat_nsecs(&sx->lock_object);
719a5aedd68SStacey Son #endif
720e41d6166SMateusz Guzik 		GIANT_SAVE(extra_work);
7214e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
722f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
723f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
724f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
725c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
726f9819486SAttilio Rao 		else
727c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
728a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
729e2b25737SMark Johnston 		sleep_time += lockstat_nsecs(&sx->lock_object);
730a5aedd68SStacey Son 		sleep_cnt++;
731a5aedd68SStacey Son #endif
732f9819486SAttilio Rao 		if (error) {
733f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
734f9819486SAttilio Rao 				CTR2(KTR_LOCK,
735f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
736f9819486SAttilio Rao 				    __func__, sx);
737f9819486SAttilio Rao 			break;
738f9819486SAttilio Rao 		}
7394e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7404e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
7414e7f640dSJohn Baldwin 			    __func__, sx);
742c5f61e6fSMateusz Guzik 		x = SX_READ_VALUE(sx);
7434e7f640dSJohn Baldwin 	}
744e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
745e41d6166SMateusz Guzik 	if (__predict_true(!extra_work))
746e41d6166SMateusz Guzik 		return (error);
747e41d6166SMateusz Guzik #endif
748076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
749e2b25737SMark Johnston 	all_time += lockstat_nsecs(&sx->lock_object);
750076dd8ebSAndriy Gapon 	if (sleep_time)
75132cd0147SMark Johnston 		LOCKSTAT_RECORD4(sx__block, sx, sleep_time,
752076dd8ebSAndriy Gapon 		    LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0,
753076dd8ebSAndriy Gapon 		    (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
7541ada9041SMateusz Guzik 	if (lda.spin_cnt > sleep_cnt)
75532cd0147SMark Johnston 		LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time,
756076dd8ebSAndriy Gapon 		    LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0,
757076dd8ebSAndriy Gapon 		    (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
758076dd8ebSAndriy Gapon #endif
759f9819486SAttilio Rao 	if (!error)
760de2c95ccSMark Johnston 		LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx,
761de2c95ccSMark Johnston 		    contested, waittime, file, line, LOCKSTAT_WRITER);
762076dd8ebSAndriy Gapon 	GIANT_RESTORE();
763f9819486SAttilio Rao 	return (error);
7644e7f640dSJohn Baldwin }
7654e7f640dSJohn Baldwin 
7664e7f640dSJohn Baldwin /*
7674e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xunlock
7684e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7694e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7704e7f640dSJohn Baldwin  * accessible from at least sx.h.
7714e7f640dSJohn Baldwin  */
7724e7f640dSJohn Baldwin void
7734e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
7744e7f640dSJohn Baldwin {
775*bc24577cSMateusz Guzik 	uintptr_t x, setx;
776da7bbd2cSJohn Baldwin 	int queue, wakeup_swapper;
7774e7f640dSJohn Baldwin 
77835370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
77935370593SAndriy Gapon 		return;
78035370593SAndriy Gapon 
7814e7f640dSJohn Baldwin 	MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
7824e7f640dSJohn Baldwin 
7833b3cf014SMateusz Guzik 	x = SX_READ_VALUE(sx);
7843b3cf014SMateusz Guzik 	if (x & SX_LOCK_RECURSED) {
7856ebb77b6SMateusz Guzik 		/* The lock is recursed, unrecurse one level. */
7864e7f640dSJohn Baldwin 		if ((--sx->sx_recurse) == 0)
7874e7f640dSJohn Baldwin 			atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
7884e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7894e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
7904e7f640dSJohn Baldwin 		return;
7914e7f640dSJohn Baldwin 	}
7923b3cf014SMateusz Guzik 
7933b3cf014SMateusz Guzik 	LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_WRITER);
7943b3cf014SMateusz Guzik 	if (x == tid &&
7953b3cf014SMateusz Guzik 	    atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED))
7963b3cf014SMateusz Guzik 		return;
7973b3cf014SMateusz Guzik 
7984e7f640dSJohn Baldwin 	MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
7994e7f640dSJohn Baldwin 	    SX_LOCK_EXCLUSIVE_WAITERS));
8004e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
8014e7f640dSJohn Baldwin 		CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
8024e7f640dSJohn Baldwin 
8034e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
804*bc24577cSMateusz Guzik 	x = SX_READ_VALUE(sx);
8054e7f640dSJohn Baldwin 
8064e7f640dSJohn Baldwin 	/*
8074e7f640dSJohn Baldwin 	 * The wake up algorithm here is quite simple and probably not
8084e7f640dSJohn Baldwin 	 * ideal.  It gives precedence to shared waiters if they are
8094e7f640dSJohn Baldwin 	 * present.  For this condition, we have to preserve the
8104e7f640dSJohn Baldwin 	 * state of the exclusive waiters flag.
8112028867dSAttilio Rao 	 * If interruptible sleeps left the shared queue empty avoid a
8122028867dSAttilio Rao 	 * starvation for the threads sleeping on the exclusive queue by giving
8132028867dSAttilio Rao 	 * them precedence and cleaning up the shared waiters bit anyway.
8144e7f640dSJohn Baldwin 	 */
815*bc24577cSMateusz Guzik 	setx = SX_LOCK_UNLOCKED;
816*bc24577cSMateusz Guzik 	queue = SQ_EXCLUSIVE_QUEUE;
817*bc24577cSMateusz Guzik 	if ((x & SX_LOCK_SHARED_WAITERS) != 0 &&
8182028867dSAttilio Rao 	    sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
8194e7f640dSJohn Baldwin 		queue = SQ_SHARED_QUEUE;
820*bc24577cSMateusz Guzik 		setx |= (x & SX_LOCK_EXCLUSIVE_WAITERS);
821*bc24577cSMateusz Guzik 	}
822*bc24577cSMateusz Guzik 	atomic_store_rel_ptr(&sx->sx_lock, setx);
8234e7f640dSJohn Baldwin 
8244e7f640dSJohn Baldwin 	/* Wake up all the waiters for the specific queue. */
8254e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
8264e7f640dSJohn Baldwin 		CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
8274e7f640dSJohn Baldwin 		    __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
8284e7f640dSJohn Baldwin 		    "exclusive");
829*bc24577cSMateusz Guzik 
830da7bbd2cSJohn Baldwin 	wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
831da7bbd2cSJohn Baldwin 	    queue);
832c5aa6b58SJeff Roberson 	sleepq_release(&sx->lock_object);
833da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
834da7bbd2cSJohn Baldwin 		kick_proc0();
8354e7f640dSJohn Baldwin }
8364e7f640dSJohn Baldwin 
837834f70f3SMateusz Guzik static bool __always_inline
838834f70f3SMateusz Guzik __sx_slock_try(struct sx *sx, uintptr_t *xp, const char *file, int line)
839834f70f3SMateusz Guzik {
840834f70f3SMateusz Guzik 
841834f70f3SMateusz Guzik 	/*
842834f70f3SMateusz Guzik 	 * If no other thread has an exclusive lock then try to bump up
843834f70f3SMateusz Guzik 	 * the count of sharers.  Since we have to preserve the state
844834f70f3SMateusz Guzik 	 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
845834f70f3SMateusz Guzik 	 * shared lock loop back and retry.
846834f70f3SMateusz Guzik 	 */
847834f70f3SMateusz Guzik 	while (*xp & SX_LOCK_SHARED) {
848834f70f3SMateusz Guzik 		MPASS(!(*xp & SX_LOCK_SHARED_WAITERS));
849834f70f3SMateusz Guzik 		if (atomic_fcmpset_acq_ptr(&sx->sx_lock, xp,
850834f70f3SMateusz Guzik 		    *xp + SX_ONE_SHARER)) {
851834f70f3SMateusz Guzik 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
852834f70f3SMateusz Guzik 				CTR4(KTR_LOCK, "%s: %p succeed %p -> %p",
853834f70f3SMateusz Guzik 				    __func__, sx, (void *)*xp,
854834f70f3SMateusz Guzik 				    (void *)(*xp + SX_ONE_SHARER));
855834f70f3SMateusz Guzik 			return (true);
856834f70f3SMateusz Guzik 		}
857834f70f3SMateusz Guzik 	}
858834f70f3SMateusz Guzik 	return (false);
859834f70f3SMateusz Guzik }
860834f70f3SMateusz Guzik 
861834f70f3SMateusz Guzik static int __noinline
862834f70f3SMateusz Guzik _sx_slock_hard(struct sx *sx, int opts, const char *file, int line, uintptr_t x)
8634e7f640dSJohn Baldwin {
8644e7f640dSJohn Baldwin 	GIANT_DECLARE;
8654e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8664e7f640dSJohn Baldwin 	volatile struct thread *owner;
8674e7f640dSJohn Baldwin #endif
8681723a064SJeff Roberson #ifdef LOCK_PROFILING
869c1a6d9faSAttilio Rao 	uint64_t waittime = 0;
870c1a6d9faSAttilio Rao 	int contested = 0;
8711723a064SJeff Roberson #endif
872c1a6d9faSAttilio Rao 	int error = 0;
87304126895SMateusz Guzik #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS)
8741ada9041SMateusz Guzik 	struct lock_delay_arg lda;
8751ada9041SMateusz Guzik #endif
876a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
87761852185SMateusz Guzik 	u_int sleep_cnt = 0;
878a5aedd68SStacey Son 	int64_t sleep_time = 0;
879076dd8ebSAndriy Gapon 	int64_t all_time = 0;
880a5aedd68SStacey Son #endif
881e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
882e41d6166SMateusz Guzik 	uintptr_t state;
883e41d6166SMateusz Guzik 	int extra_work;
884e41d6166SMateusz Guzik #endif
885c1a6d9faSAttilio Rao 
88635370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
88735370593SAndriy Gapon 		return (0);
88835370593SAndriy Gapon 
889fa5000a4SMateusz Guzik #if defined(ADAPTIVE_SX)
8901ada9041SMateusz Guzik 	lock_delay_arg_init(&lda, &sx_delay);
891fa5000a4SMateusz Guzik #elif defined(KDTRACE_HOOKS)
892fa5000a4SMateusz Guzik 	lock_delay_arg_init(&lda, NULL);
8931ada9041SMateusz Guzik #endif
894e41d6166SMateusz Guzik 
895ae7d25a4SMateusz Guzik #ifdef HWPMC_HOOKS
896ae7d25a4SMateusz Guzik 	PMC_SOFT_CALL( , , lock, failed);
897ae7d25a4SMateusz Guzik #endif
898ae7d25a4SMateusz Guzik 	lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
899ae7d25a4SMateusz Guzik 	    &waittime);
900ae7d25a4SMateusz Guzik 
901e41d6166SMateusz Guzik #ifdef LOCK_PROFILING
902e41d6166SMateusz Guzik 	extra_work = 1;
903e41d6166SMateusz Guzik 	state = x;
904e41d6166SMateusz Guzik #elif defined(KDTRACE_HOOKS)
905e41d6166SMateusz Guzik 	extra_work = lockstat_enabled;
906e41d6166SMateusz Guzik 	if (__predict_false(extra_work)) {
907e2b25737SMark Johnston 		all_time -= lockstat_nsecs(&sx->lock_object);
908c5f61e6fSMateusz Guzik 		state = x;
909e41d6166SMateusz Guzik 	}
910c5f61e6fSMateusz Guzik #endif
911076dd8ebSAndriy Gapon 
9124e7f640dSJohn Baldwin 	/*
9134e7f640dSJohn Baldwin 	 * As with rwlocks, we don't make any attempt to try to block
9144e7f640dSJohn Baldwin 	 * shared locks once there is an exclusive waiter.
9154e7f640dSJohn Baldwin 	 */
9164e7f640dSJohn Baldwin 	for (;;) {
917834f70f3SMateusz Guzik 		if (__sx_slock_try(sx, &x, file, line))
9184e7f640dSJohn Baldwin 			break;
919c5f61e6fSMateusz Guzik #ifdef KDTRACE_HOOKS
920c5f61e6fSMateusz Guzik 		lda.spin_cnt++;
921c5f61e6fSMateusz Guzik #endif
922c5f61e6fSMateusz Guzik 
9234e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
9244e7f640dSJohn Baldwin 		/*
9254e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
9264e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
9274e7f640dSJohn Baldwin 		 * changes.
9284e7f640dSJohn Baldwin 		 */
9291ae1c2a3SAttilio Rao 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
930c5f61e6fSMateusz Guzik 			owner = lv_sx_owner(x);
9314e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
9324e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
9334e7f640dSJohn Baldwin 					CTR3(KTR_LOCK,
9344e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
9354e7f640dSJohn Baldwin 					    __func__, sx, owner);
9362cba8dd3SJohn Baldwin 				KTR_STATE1(KTR_SCHED, "thread",
9372cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "spinning",
9382cba8dd3SJohn Baldwin 				    "lockname:\"%s\"", sx->lock_object.lo_name);
939e41d6166SMateusz Guzik 				GIANT_SAVE(extra_work);
940c5f61e6fSMateusz Guzik 				do {
9411ada9041SMateusz Guzik 					lock_delay(&lda);
942c5f61e6fSMateusz Guzik 					x = SX_READ_VALUE(sx);
943c5f61e6fSMateusz Guzik 					owner = lv_sx_owner(x);
944c5f61e6fSMateusz Guzik 				} while (owner != NULL && TD_IS_RUNNING(owner));
9452cba8dd3SJohn Baldwin 				KTR_STATE0(KTR_SCHED, "thread",
9462cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "running");
9474e7f640dSJohn Baldwin 				continue;
9484e7f640dSJohn Baldwin 			}
9494e7f640dSJohn Baldwin 		}
9504e7f640dSJohn Baldwin #endif
9514e7f640dSJohn Baldwin 
9524e7f640dSJohn Baldwin 		/*
9534e7f640dSJohn Baldwin 		 * Some other thread already has an exclusive lock, so
9544e7f640dSJohn Baldwin 		 * start the process of blocking.
9554e7f640dSJohn Baldwin 		 */
9564e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
957c5f61e6fSMateusz Guzik 		x = SX_READ_VALUE(sx);
9584e7f640dSJohn Baldwin 
9594e7f640dSJohn Baldwin 		/*
9604e7f640dSJohn Baldwin 		 * The lock could have been released while we spun.
9614e7f640dSJohn Baldwin 		 * In this case loop back and retry.
9624e7f640dSJohn Baldwin 		 */
9634e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
9644e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
9654e7f640dSJohn Baldwin 			continue;
9664e7f640dSJohn Baldwin 		}
9674e7f640dSJohn Baldwin 
9684e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
9694e7f640dSJohn Baldwin 		/*
9704e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
9714e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
9724e7f640dSJohn Baldwin 		 * changes.
9734e7f640dSJohn Baldwin 		 */
9744e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
9751ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
9764e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
9774e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
9784e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
979c5f61e6fSMateusz Guzik 				x = SX_READ_VALUE(sx);
9804e7f640dSJohn Baldwin 				continue;
9814e7f640dSJohn Baldwin 			}
9824e7f640dSJohn Baldwin 		}
9834e7f640dSJohn Baldwin #endif
9844e7f640dSJohn Baldwin 
9854e7f640dSJohn Baldwin 		/*
9864e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
9874e7f640dSJohn Baldwin 		 * fail to set it drop the sleep queue lock and loop
9884e7f640dSJohn Baldwin 		 * back.
9894e7f640dSJohn Baldwin 		 */
9904e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED_WAITERS)) {
9914e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
9924e7f640dSJohn Baldwin 			    x | SX_LOCK_SHARED_WAITERS)) {
9934e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
994c5f61e6fSMateusz Guzik 				x = SX_READ_VALUE(sx);
9954e7f640dSJohn Baldwin 				continue;
9964e7f640dSJohn Baldwin 			}
9974e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
9984e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
9994e7f640dSJohn Baldwin 				    __func__, sx);
10004e7f640dSJohn Baldwin 		}
10014e7f640dSJohn Baldwin 
10024e7f640dSJohn Baldwin 		/*
10034e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the shared lock,
10044e7f640dSJohn Baldwin 		 * we have to sleep.
10054e7f640dSJohn Baldwin 		 */
10064e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
10074e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
10084e7f640dSJohn Baldwin 			    __func__, sx);
10094e7f640dSJohn Baldwin 
1010a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
1011e2b25737SMark Johnston 		sleep_time -= lockstat_nsecs(&sx->lock_object);
1012a5aedd68SStacey Son #endif
1013e41d6166SMateusz Guzik 		GIANT_SAVE(extra_work);
10144e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
1015f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
1016f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
1017f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
1018c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
1019f9819486SAttilio Rao 		else
1020c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
1021a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
1022e2b25737SMark Johnston 		sleep_time += lockstat_nsecs(&sx->lock_object);
1023a5aedd68SStacey Son 		sleep_cnt++;
1024a5aedd68SStacey Son #endif
1025f9819486SAttilio Rao 		if (error) {
1026f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
1027f9819486SAttilio Rao 				CTR2(KTR_LOCK,
1028f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
1029f9819486SAttilio Rao 				    __func__, sx);
1030f9819486SAttilio Rao 			break;
1031f9819486SAttilio Rao 		}
10324e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
10334e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
10344e7f640dSJohn Baldwin 			    __func__, sx);
1035c5f61e6fSMateusz Guzik 		x = SX_READ_VALUE(sx);
10364e7f640dSJohn Baldwin 	}
1037e41d6166SMateusz Guzik #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
1038e41d6166SMateusz Guzik 	if (__predict_true(!extra_work))
1039e41d6166SMateusz Guzik 		return (error);
1040e41d6166SMateusz Guzik #endif
1041076dd8ebSAndriy Gapon #ifdef KDTRACE_HOOKS
1042e2b25737SMark Johnston 	all_time += lockstat_nsecs(&sx->lock_object);
1043076dd8ebSAndriy Gapon 	if (sleep_time)
104432cd0147SMark Johnston 		LOCKSTAT_RECORD4(sx__block, sx, sleep_time,
1045076dd8ebSAndriy Gapon 		    LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0,
1046076dd8ebSAndriy Gapon 		    (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
10471ada9041SMateusz Guzik 	if (lda.spin_cnt > sleep_cnt)
104832cd0147SMark Johnston 		LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time,
1049076dd8ebSAndriy Gapon 		    LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0,
1050076dd8ebSAndriy Gapon 		    (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
1051076dd8ebSAndriy Gapon #endif
10523ae56ce9SMateusz Guzik 	if (error == 0) {
1053de2c95ccSMark Johnston 		LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx,
1054de2c95ccSMark Johnston 		    contested, waittime, file, line, LOCKSTAT_READER);
10553ae56ce9SMateusz Guzik 	}
10564e7f640dSJohn Baldwin 	GIANT_RESTORE();
1057f9819486SAttilio Rao 	return (error);
10584e7f640dSJohn Baldwin }
10594e7f640dSJohn Baldwin 
1060834f70f3SMateusz Guzik int
1061834f70f3SMateusz Guzik _sx_slock(struct sx *sx, int opts, const char *file, int line)
10624e7f640dSJohn Baldwin {
10634e7f640dSJohn Baldwin 	uintptr_t x;
1064834f70f3SMateusz Guzik 	int error;
10654e7f640dSJohn Baldwin 
1066704cb42fSMark Johnston 	KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
1067704cb42fSMark Johnston 	    !TD_IS_IDLETHREAD(curthread),
1068834f70f3SMateusz Guzik 	    ("sx_slock() by idle thread %p on sx %s @ %s:%d",
1069834f70f3SMateusz Guzik 	    curthread, sx->lock_object.lo_name, file, line));
10703ae56ce9SMateusz Guzik 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
1071834f70f3SMateusz Guzik 	    ("sx_slock() of destroyed sx @ %s:%d", file, line));
1072834f70f3SMateusz Guzik 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
1073834f70f3SMateusz Guzik 
1074834f70f3SMateusz Guzik 	error = 0;
1075c5f61e6fSMateusz Guzik 	x = SX_READ_VALUE(sx);
1076834f70f3SMateusz Guzik 	if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__acquire) ||
1077834f70f3SMateusz Guzik 	    !__sx_slock_try(sx, &x, file, line)))
1078834f70f3SMateusz Guzik 		error = _sx_slock_hard(sx, opts, file, line, x);
1079834f70f3SMateusz Guzik 	if (error == 0) {
1080834f70f3SMateusz Guzik 		LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
1081834f70f3SMateusz Guzik 		WITNESS_LOCK(&sx->lock_object, 0, file, line);
1082834f70f3SMateusz Guzik 		TD_LOCKS_INC(curthread);
1083834f70f3SMateusz Guzik 	}
1084834f70f3SMateusz Guzik 	return (error);
1085834f70f3SMateusz Guzik }
1086834f70f3SMateusz Guzik 
1087834f70f3SMateusz Guzik static bool __always_inline
1088834f70f3SMateusz Guzik _sx_sunlock_try(struct sx *sx, uintptr_t *xp)
1089834f70f3SMateusz Guzik {
1090834f70f3SMateusz Guzik 
10914e7f640dSJohn Baldwin 	for (;;) {
10924e7f640dSJohn Baldwin 		/*
10934e7f640dSJohn Baldwin 		 * We should never have sharers while at least one thread
10944e7f640dSJohn Baldwin 		 * holds a shared lock.
10954e7f640dSJohn Baldwin 		 */
1096834f70f3SMateusz Guzik 		KASSERT(!(*xp & SX_LOCK_SHARED_WAITERS),
10974e7f640dSJohn Baldwin 		    ("%s: waiting sharers", __func__));
10984e7f640dSJohn Baldwin 
10994e7f640dSJohn Baldwin 		/*
11004e7f640dSJohn Baldwin 		 * See if there is more than one shared lock held.  If
11014e7f640dSJohn Baldwin 		 * so, just drop one and return.
11024e7f640dSJohn Baldwin 		 */
1103834f70f3SMateusz Guzik 		if (SX_SHARERS(*xp) > 1) {
1104834f70f3SMateusz Guzik 			if (atomic_fcmpset_rel_ptr(&sx->sx_lock, xp,
1105834f70f3SMateusz Guzik 			    *xp - SX_ONE_SHARER)) {
11064e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
11074e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
11084e7f640dSJohn Baldwin 					    "%s: %p succeeded %p -> %p",
1109834f70f3SMateusz Guzik 					    __func__, sx, (void *)*xp,
1110834f70f3SMateusz Guzik 					    (void *)(*xp - SX_ONE_SHARER));
1111834f70f3SMateusz Guzik 				return (true);
11124e7f640dSJohn Baldwin 			}
11134e7f640dSJohn Baldwin 			continue;
11144e7f640dSJohn Baldwin 		}
11154e7f640dSJohn Baldwin 
11164e7f640dSJohn Baldwin 		/*
11174e7f640dSJohn Baldwin 		 * If there aren't any waiters for an exclusive lock,
11184e7f640dSJohn Baldwin 		 * then try to drop it quickly.
11194e7f640dSJohn Baldwin 		 */
1120834f70f3SMateusz Guzik 		if (!(*xp & SX_LOCK_EXCLUSIVE_WAITERS)) {
1121834f70f3SMateusz Guzik 			MPASS(*xp == SX_SHARERS_LOCK(1));
1122834f70f3SMateusz Guzik 			*xp = SX_SHARERS_LOCK(1);
1123fa474043SMateusz Guzik 			if (atomic_fcmpset_rel_ptr(&sx->sx_lock,
1124834f70f3SMateusz Guzik 			    xp, SX_LOCK_UNLOCKED)) {
11254e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
11264e7f640dSJohn Baldwin 					CTR2(KTR_LOCK, "%s: %p last succeeded",
11274e7f640dSJohn Baldwin 					    __func__, sx);
1128834f70f3SMateusz Guzik 				return (true);
11294e7f640dSJohn Baldwin 			}
11304e7f640dSJohn Baldwin 			continue;
11314e7f640dSJohn Baldwin 		}
1132834f70f3SMateusz Guzik 		break;
1133834f70f3SMateusz Guzik 	}
1134834f70f3SMateusz Guzik 	return (false);
1135834f70f3SMateusz Guzik }
1136834f70f3SMateusz Guzik 
1137834f70f3SMateusz Guzik static void __noinline
1138834f70f3SMateusz Guzik _sx_sunlock_hard(struct sx *sx, uintptr_t x, const char *file, int line)
1139834f70f3SMateusz Guzik {
1140834f70f3SMateusz Guzik 	int wakeup_swapper;
1141834f70f3SMateusz Guzik 
1142834f70f3SMateusz Guzik 	if (SCHEDULER_STOPPED())
1143834f70f3SMateusz Guzik 		return;
1144834f70f3SMateusz Guzik 
1145834f70f3SMateusz Guzik 	LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER);
1146834f70f3SMateusz Guzik 
1147834f70f3SMateusz Guzik 	for (;;) {
1148834f70f3SMateusz Guzik 		if (_sx_sunlock_try(sx, &x))
1149834f70f3SMateusz Guzik 			break;
11504e7f640dSJohn Baldwin 
11514e7f640dSJohn Baldwin 		/*
11524e7f640dSJohn Baldwin 		 * At this point, there should just be one sharer with
11534e7f640dSJohn Baldwin 		 * exclusive waiters.
11544e7f640dSJohn Baldwin 		 */
11554e7f640dSJohn Baldwin 		MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
11564e7f640dSJohn Baldwin 
11574e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
11584e7f640dSJohn Baldwin 
11594e7f640dSJohn Baldwin 		/*
11604e7f640dSJohn Baldwin 		 * Wake up semantic here is quite simple:
11614e7f640dSJohn Baldwin 		 * Just wake up all the exclusive waiters.
11624e7f640dSJohn Baldwin 		 * Note that the state of the lock could have changed,
11634e7f640dSJohn Baldwin 		 * so if it fails loop back and retry.
11644e7f640dSJohn Baldwin 		 */
1165ddce63caSAttilio Rao 		if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
11664e7f640dSJohn Baldwin 		    SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
11674e7f640dSJohn Baldwin 		    SX_LOCK_UNLOCKED)) {
11684e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
1169c5f61e6fSMateusz Guzik 			x = SX_READ_VALUE(sx);
11704e7f640dSJohn Baldwin 			continue;
11714e7f640dSJohn Baldwin 		}
11724e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
11734e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p waking up all thread on"
11744e7f640dSJohn Baldwin 			    "exclusive queue", __func__, sx);
1175da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1176da7bbd2cSJohn Baldwin 		    0, SQ_EXCLUSIVE_QUEUE);
1177c5aa6b58SJeff Roberson 		sleepq_release(&sx->lock_object);
1178da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
1179da7bbd2cSJohn Baldwin 			kick_proc0();
11804e7f640dSJohn Baldwin 		break;
11814e7f640dSJohn Baldwin 	}
1182834f70f3SMateusz Guzik }
1183834f70f3SMateusz Guzik 
1184834f70f3SMateusz Guzik void
1185834f70f3SMateusz Guzik _sx_sunlock(struct sx *sx, const char *file, int line)
1186834f70f3SMateusz Guzik {
1187834f70f3SMateusz Guzik 	uintptr_t x;
1188834f70f3SMateusz Guzik 
1189834f70f3SMateusz Guzik 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
1190834f70f3SMateusz Guzik 	    ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
1191834f70f3SMateusz Guzik 	_sx_assert(sx, SA_SLOCKED, file, line);
1192834f70f3SMateusz Guzik 	WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
1193834f70f3SMateusz Guzik 	LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
1194834f70f3SMateusz Guzik 
1195834f70f3SMateusz Guzik 	x = SX_READ_VALUE(sx);
1196834f70f3SMateusz Guzik 	if (__predict_false(LOCKSTAT_OOL_PROFILE_ENABLED(sx__release) ||
1197834f70f3SMateusz Guzik 	    !_sx_sunlock_try(sx, &x)))
1198834f70f3SMateusz Guzik 		_sx_sunlock_hard(sx, x, file, line);
1199834f70f3SMateusz Guzik 
12003ae56ce9SMateusz Guzik 	TD_LOCKS_DEC(curthread);
1201d55229b7SJason Evans }
12024e5e677bSJohn Baldwin 
12034e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT
1204781a35dfSJohn Baldwin #ifndef INVARIANTS
1205781a35dfSJohn Baldwin #undef	_sx_assert
1206781a35dfSJohn Baldwin #endif
1207781a35dfSJohn Baldwin 
12084e5e677bSJohn Baldwin /*
12094e5e677bSJohn Baldwin  * In the non-WITNESS case, sx_assert() can only detect that at least
12104e5e677bSJohn Baldwin  * *some* thread owns an slock, but it cannot guarantee that *this*
12114e5e677bSJohn Baldwin  * thread owns an slock.
12124e5e677bSJohn Baldwin  */
12134e5e677bSJohn Baldwin void
1214d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line)
12154e5e677bSJohn Baldwin {
12164e7f640dSJohn Baldwin #ifndef WITNESS
12174e7f640dSJohn Baldwin 	int slocked = 0;
12184e7f640dSJohn Baldwin #endif
12194e5e677bSJohn Baldwin 
122003129ba9SJohn Baldwin 	if (panicstr != NULL)
122103129ba9SJohn Baldwin 		return;
12224e5e677bSJohn Baldwin 	switch (what) {
12237ec137e5SJohn Baldwin 	case SA_SLOCKED:
12247ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_NOTRECURSED:
12257ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_RECURSED:
12264e7f640dSJohn Baldwin #ifndef WITNESS
12274e7f640dSJohn Baldwin 		slocked = 1;
12284e7f640dSJohn Baldwin 		/* FALLTHROUGH */
12294e7f640dSJohn Baldwin #endif
12307ec137e5SJohn Baldwin 	case SA_LOCKED:
12317ec137e5SJohn Baldwin 	case SA_LOCKED | SA_NOTRECURSED:
12327ec137e5SJohn Baldwin 	case SA_LOCKED | SA_RECURSED:
12334e5e677bSJohn Baldwin #ifdef WITNESS
1234aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
12354e5e677bSJohn Baldwin #else
12364e7f640dSJohn Baldwin 		/*
12374e7f640dSJohn Baldwin 		 * If some other thread has an exclusive lock or we
12384e7f640dSJohn Baldwin 		 * have one and are asserting a shared lock, fail.
12394e7f640dSJohn Baldwin 		 * Also, if no one has a lock at all, fail.
12404e7f640dSJohn Baldwin 		 */
12414e7f640dSJohn Baldwin 		if (sx->sx_lock == SX_LOCK_UNLOCKED ||
12424e7f640dSJohn Baldwin 		    (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
12434e7f640dSJohn Baldwin 		    sx_xholder(sx) != curthread)))
124403129ba9SJohn Baldwin 			panic("Lock %s not %slocked @ %s:%d\n",
12454e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, slocked ? "share " : "",
12464e7f640dSJohn Baldwin 			    file, line);
12474e7f640dSJohn Baldwin 
12484e7f640dSJohn Baldwin 		if (!(sx->sx_lock & SX_LOCK_SHARED)) {
12494e7f640dSJohn Baldwin 			if (sx_recursed(sx)) {
12507ec137e5SJohn Baldwin 				if (what & SA_NOTRECURSED)
12514e7f640dSJohn Baldwin 					panic("Lock %s recursed @ %s:%d\n",
12524e7f640dSJohn Baldwin 					    sx->lock_object.lo_name, file,
12534e7f640dSJohn Baldwin 					    line);
12547ec137e5SJohn Baldwin 			} else if (what & SA_RECURSED)
12554e7f640dSJohn Baldwin 				panic("Lock %s not recursed @ %s:%d\n",
12564e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
12574e7f640dSJohn Baldwin 		}
12584e5e677bSJohn Baldwin #endif
12594e5e677bSJohn Baldwin 		break;
12607ec137e5SJohn Baldwin 	case SA_XLOCKED:
12617ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_NOTRECURSED:
12627ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_RECURSED:
12634e7f640dSJohn Baldwin 		if (sx_xholder(sx) != curthread)
126403129ba9SJohn Baldwin 			panic("Lock %s not exclusively locked @ %s:%d\n",
1265aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
12664e7f640dSJohn Baldwin 		if (sx_recursed(sx)) {
12677ec137e5SJohn Baldwin 			if (what & SA_NOTRECURSED)
12684e7f640dSJohn Baldwin 				panic("Lock %s recursed @ %s:%d\n",
12694e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
12707ec137e5SJohn Baldwin 		} else if (what & SA_RECURSED)
12714e7f640dSJohn Baldwin 			panic("Lock %s not recursed @ %s:%d\n",
12724e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
12734e5e677bSJohn Baldwin 		break;
12747ec137e5SJohn Baldwin 	case SA_UNLOCKED:
127519b0efd3SPawel Jakub Dawidek #ifdef WITNESS
1276aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
127719b0efd3SPawel Jakub Dawidek #else
1278f6739b1dSPawel Jakub Dawidek 		/*
12794e7f640dSJohn Baldwin 		 * If we hold an exclusve lock fail.  We can't
12804e7f640dSJohn Baldwin 		 * reliably check to see if we hold a shared lock or
12814e7f640dSJohn Baldwin 		 * not.
1282f6739b1dSPawel Jakub Dawidek 		 */
12834e7f640dSJohn Baldwin 		if (sx_xholder(sx) == curthread)
128403129ba9SJohn Baldwin 			panic("Lock %s exclusively locked @ %s:%d\n",
1285aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
128619b0efd3SPawel Jakub Dawidek #endif
128719b0efd3SPawel Jakub Dawidek 		break;
12884e5e677bSJohn Baldwin 	default:
12894e5e677bSJohn Baldwin 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
12904e5e677bSJohn Baldwin 		    line);
12914e5e677bSJohn Baldwin 	}
12924e5e677bSJohn Baldwin }
12934e5e677bSJohn Baldwin #endif	/* INVARIANT_SUPPORT */
1294d272fe53SJohn Baldwin 
1295d272fe53SJohn Baldwin #ifdef DDB
12964e7f640dSJohn Baldwin static void
1297d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock)
1298d272fe53SJohn Baldwin {
1299d272fe53SJohn Baldwin 	struct thread *td;
1300d576deedSPawel Jakub Dawidek 	const struct sx *sx;
1301d272fe53SJohn Baldwin 
1302d576deedSPawel Jakub Dawidek 	sx = (const struct sx *)lock;
1303d272fe53SJohn Baldwin 
1304d272fe53SJohn Baldwin 	db_printf(" state: ");
13054e7f640dSJohn Baldwin 	if (sx->sx_lock == SX_LOCK_UNLOCKED)
13064e7f640dSJohn Baldwin 		db_printf("UNLOCKED\n");
13070026c92cSJohn Baldwin 	else if (sx->sx_lock == SX_LOCK_DESTROYED) {
13080026c92cSJohn Baldwin 		db_printf("DESTROYED\n");
13090026c92cSJohn Baldwin 		return;
13100026c92cSJohn Baldwin 	} else if (sx->sx_lock & SX_LOCK_SHARED)
13114e7f640dSJohn Baldwin 		db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
13124e7f640dSJohn Baldwin 	else {
13134e7f640dSJohn Baldwin 		td = sx_xholder(sx);
1314d272fe53SJohn Baldwin 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1315431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
13164e7f640dSJohn Baldwin 		if (sx_recursed(sx))
13174e7f640dSJohn Baldwin 			db_printf(" recursed: %d\n", sx->sx_recurse);
13184e7f640dSJohn Baldwin 	}
13194e7f640dSJohn Baldwin 
13204e7f640dSJohn Baldwin 	db_printf(" waiters: ");
13214e7f640dSJohn Baldwin 	switch(sx->sx_lock &
13224e7f640dSJohn Baldwin 	    (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
13234e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS:
13244e7f640dSJohn Baldwin 		db_printf("shared\n");
13254e7f640dSJohn Baldwin 		break;
13264e7f640dSJohn Baldwin 	case SX_LOCK_EXCLUSIVE_WAITERS:
13274e7f640dSJohn Baldwin 		db_printf("exclusive\n");
13284e7f640dSJohn Baldwin 		break;
13294e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
13304e7f640dSJohn Baldwin 		db_printf("exclusive and shared\n");
13314e7f640dSJohn Baldwin 		break;
13324e7f640dSJohn Baldwin 	default:
13334e7f640dSJohn Baldwin 		db_printf("none\n");
13344e7f640dSJohn Baldwin 	}
1335d272fe53SJohn Baldwin }
1336462a7addSJohn Baldwin 
1337462a7addSJohn Baldwin /*
1338462a7addSJohn Baldwin  * Check to see if a thread that is blocked on a sleep queue is actually
1339462a7addSJohn Baldwin  * blocked on an sx lock.  If so, output some details and return true.
1340462a7addSJohn Baldwin  * If the lock has an exclusive owner, return that in *ownerp.
1341462a7addSJohn Baldwin  */
1342462a7addSJohn Baldwin int
1343462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp)
1344462a7addSJohn Baldwin {
1345462a7addSJohn Baldwin 	struct sx *sx;
1346462a7addSJohn Baldwin 
1347462a7addSJohn Baldwin 	/*
13484e7f640dSJohn Baldwin 	 * Check to see if this thread is blocked on an sx lock.
13494e7f640dSJohn Baldwin 	 * First, we check the lock class.  If that is ok, then we
13504e7f640dSJohn Baldwin 	 * compare the lock name against the wait message.
1351462a7addSJohn Baldwin 	 */
13524e7f640dSJohn Baldwin 	sx = td->td_wchan;
13534e7f640dSJohn Baldwin 	if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
13544e7f640dSJohn Baldwin 	    sx->lock_object.lo_name != td->td_wmesg)
1355462a7addSJohn Baldwin 		return (0);
1356462a7addSJohn Baldwin 
1357462a7addSJohn Baldwin 	/* We think we have an sx lock, so output some details. */
1358462a7addSJohn Baldwin 	db_printf("blocked on sx \"%s\" ", td->td_wmesg);
13594e7f640dSJohn Baldwin 	*ownerp = sx_xholder(sx);
13604e7f640dSJohn Baldwin 	if (sx->sx_lock & SX_LOCK_SHARED)
13614e7f640dSJohn Baldwin 		db_printf("SLOCK (count %ju)\n",
13624e7f640dSJohn Baldwin 		    (uintmax_t)SX_SHARERS(sx->sx_lock));
13634e7f640dSJohn Baldwin 	else
1364462a7addSJohn Baldwin 		db_printf("XLOCK\n");
1365462a7addSJohn Baldwin 	return (1);
1366462a7addSJohn Baldwin }
1367d272fe53SJohn Baldwin #endif
1368