xref: /freebsd/sys/kern/kern_sx.c (revision d576deedb549253edd7dab4f594a77d52a98dc5f)
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"
40a5aedd68SStacey Son #include "opt_kdtrace.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>
476281b30aSJason Evans #include <sys/ktr.h>
4819284646SJohn Baldwin #include <sys/lock.h>
496281b30aSJason Evans #include <sys/mutex.h>
50d272fe53SJohn Baldwin #include <sys/proc.h>
514e7f640dSJohn Baldwin #include <sys/sleepqueue.h>
526281b30aSJason Evans #include <sys/sx.h>
53e31d0833SAttilio Rao #include <sys/sysctl.h>
544e7f640dSJohn Baldwin #include <sys/systm.h>
554e7f640dSJohn Baldwin 
56e31d0833SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
574e7f640dSJohn Baldwin #include <machine/cpu.h>
584e7f640dSJohn Baldwin #endif
596281b30aSJason Evans 
60462a7addSJohn Baldwin #ifdef DDB
61d272fe53SJohn Baldwin #include <ddb/ddb.h>
624e7f640dSJohn Baldwin #endif
63d272fe53SJohn Baldwin 
641ae1c2a3SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
651ae1c2a3SAttilio Rao #define	ADAPTIVE_SX
664e7f640dSJohn Baldwin #endif
674e7f640dSJohn Baldwin 
68f0830182SAttilio Rao CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE);
69c1a6d9faSAttilio Rao 
704e7f640dSJohn Baldwin /* Handy macros for sleep queues. */
714e7f640dSJohn Baldwin #define	SQ_EXCLUSIVE_QUEUE	0
724e7f640dSJohn Baldwin #define	SQ_SHARED_QUEUE		1
734e7f640dSJohn Baldwin 
744e7f640dSJohn Baldwin /*
754e7f640dSJohn Baldwin  * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file.  We
764e7f640dSJohn Baldwin  * drop Giant anytime we have to sleep or if we adaptively spin.
774e7f640dSJohn Baldwin  */
784e7f640dSJohn Baldwin #define	GIANT_DECLARE							\
794e7f640dSJohn Baldwin 	int _giantcnt = 0;						\
804e7f640dSJohn Baldwin 	WITNESS_SAVE_DECL(Giant)					\
814e7f640dSJohn Baldwin 
824e7f640dSJohn Baldwin #define	GIANT_SAVE() do {						\
834e7f640dSJohn Baldwin 	if (mtx_owned(&Giant)) {					\
844e7f640dSJohn Baldwin 		WITNESS_SAVE(&Giant.lock_object, Giant);		\
854e7f640dSJohn Baldwin 		while (mtx_owned(&Giant)) {				\
864e7f640dSJohn Baldwin 			_giantcnt++;					\
874e7f640dSJohn Baldwin 			mtx_unlock(&Giant);				\
884e7f640dSJohn Baldwin 		}							\
894e7f640dSJohn Baldwin 	}								\
904e7f640dSJohn Baldwin } while (0)
914e7f640dSJohn Baldwin 
924e7f640dSJohn Baldwin #define GIANT_RESTORE() do {						\
934e7f640dSJohn Baldwin 	if (_giantcnt > 0) {						\
944e7f640dSJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);			\
954e7f640dSJohn Baldwin 		while (_giantcnt--)					\
964e7f640dSJohn Baldwin 			mtx_lock(&Giant);				\
974e7f640dSJohn Baldwin 		WITNESS_RESTORE(&Giant.lock_object, Giant);		\
984e7f640dSJohn Baldwin 	}								\
994e7f640dSJohn Baldwin } while (0)
1004e7f640dSJohn Baldwin 
1014e7f640dSJohn Baldwin /*
102da7d0d1eSJohn Baldwin  * Returns true if an exclusive lock is recursed.  It assumes
103da7d0d1eSJohn Baldwin  * curthread currently has an exclusive lock.
1044e7f640dSJohn Baldwin  */
10590356491SAttilio Rao #define	sx_recurse		lock_object.lo_data
1064e7f640dSJohn Baldwin #define	sx_recursed(sx)		((sx)->sx_recurse != 0)
1074e7f640dSJohn Baldwin 
108*d576deedSPawel Jakub Dawidek static void	assert_sx(const struct lock_object *lock, int what);
1094e7f640dSJohn Baldwin #ifdef DDB
110*d576deedSPawel Jakub Dawidek static void	db_show_sx(const struct lock_object *lock);
111d272fe53SJohn Baldwin #endif
1126e21afd4SJohn Baldwin static void	lock_sx(struct lock_object *lock, int how);
113a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
114*d576deedSPawel Jakub Dawidek static int	owner_sx(const struct lock_object *lock, struct thread **owner);
115a5aedd68SStacey Son #endif
1166e21afd4SJohn Baldwin static int	unlock_sx(struct lock_object *lock);
117d272fe53SJohn Baldwin 
11819284646SJohn Baldwin struct lock_class lock_class_sx = {
119ae8dde30SJohn Baldwin 	.lc_name = "sx",
120ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
121f9721b43SAttilio Rao 	.lc_assert = assert_sx,
122d272fe53SJohn Baldwin #ifdef DDB
123ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_sx,
124d272fe53SJohn Baldwin #endif
1256e21afd4SJohn Baldwin 	.lc_lock = lock_sx,
1266e21afd4SJohn Baldwin 	.lc_unlock = unlock_sx,
127a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
128a5aedd68SStacey Son 	.lc_owner = owner_sx,
129a5aedd68SStacey Son #endif
13019284646SJohn Baldwin };
13119284646SJohn Baldwin 
132781a35dfSJohn Baldwin #ifndef INVARIANTS
133781a35dfSJohn Baldwin #define	_sx_assert(sx, what, file, line)
134781a35dfSJohn Baldwin #endif
135781a35dfSJohn Baldwin 
1361ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX
1371ae1c2a3SAttilio Rao static u_int asx_retries = 10;
1381ae1c2a3SAttilio Rao static u_int asx_loops = 10000;
1396472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
140fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
141fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
1421ae1c2a3SAttilio Rao #endif
1431ae1c2a3SAttilio Rao 
1446281b30aSJason Evans void
145*d576deedSPawel Jakub Dawidek assert_sx(const struct lock_object *lock, int what)
146f9721b43SAttilio Rao {
147f9721b43SAttilio Rao 
148*d576deedSPawel Jakub Dawidek 	sx_assert((const struct sx *)lock, what);
149f9721b43SAttilio Rao }
150f9721b43SAttilio Rao 
151f9721b43SAttilio Rao void
1526e21afd4SJohn Baldwin lock_sx(struct lock_object *lock, int how)
1536e21afd4SJohn Baldwin {
1546e21afd4SJohn Baldwin 	struct sx *sx;
1556e21afd4SJohn Baldwin 
1566e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1576e21afd4SJohn Baldwin 	if (how)
1586e21afd4SJohn Baldwin 		sx_xlock(sx);
1596e21afd4SJohn Baldwin 	else
1606e21afd4SJohn Baldwin 		sx_slock(sx);
1616e21afd4SJohn Baldwin }
1626e21afd4SJohn Baldwin 
1636e21afd4SJohn Baldwin int
1646e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock)
1656e21afd4SJohn Baldwin {
1666e21afd4SJohn Baldwin 	struct sx *sx;
1676e21afd4SJohn Baldwin 
1686e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1697ec137e5SJohn Baldwin 	sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
1706e21afd4SJohn Baldwin 	if (sx_xlocked(sx)) {
1716e21afd4SJohn Baldwin 		sx_xunlock(sx);
1726e21afd4SJohn Baldwin 		return (1);
1736e21afd4SJohn Baldwin 	} else {
1746e21afd4SJohn Baldwin 		sx_sunlock(sx);
1756e21afd4SJohn Baldwin 		return (0);
1766e21afd4SJohn Baldwin 	}
1776e21afd4SJohn Baldwin }
1786e21afd4SJohn Baldwin 
179a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
180a5aedd68SStacey Son int
181*d576deedSPawel Jakub Dawidek owner_sx(const struct lock_object *lock, struct thread **owner)
182a5aedd68SStacey Son {
183*d576deedSPawel Jakub Dawidek         const struct sx *sx = (const struct sx *)lock;
184a5aedd68SStacey Son 	uintptr_t x = sx->sx_lock;
185a5aedd68SStacey Son 
186a5aedd68SStacey Son         *owner = (struct thread *)SX_OWNER(x);
187a5aedd68SStacey Son         return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
188a5aedd68SStacey Son 	    (*owner != NULL));
189a5aedd68SStacey Son }
190a5aedd68SStacey Son #endif
191a5aedd68SStacey Son 
1926e21afd4SJohn Baldwin void
193c27b5699SAndrew R. Reiter sx_sysinit(void *arg)
194c27b5699SAndrew R. Reiter {
195c27b5699SAndrew R. Reiter 	struct sx_args *sargs = arg;
196c27b5699SAndrew R. Reiter 
197e4cd31ddSJeff Roberson 	sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
198c27b5699SAndrew R. Reiter }
199c27b5699SAndrew R. Reiter 
200c27b5699SAndrew R. Reiter void
2014e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts)
2026281b30aSJason Evans {
2034e7f640dSJohn Baldwin 	int flags;
2046281b30aSJason Evans 
205b0d67325SJohn Baldwin 	MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
2061ae1c2a3SAttilio Rao 	    SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
207353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
208353998acSAttilio Rao 	    ("%s: sx_lock not aligned for %s: %p", __func__, description,
209353998acSAttilio Rao 	    &sx->sx_lock));
210b0d67325SJohn Baldwin 
211f0830182SAttilio Rao 	flags = LO_SLEEPABLE | LO_UPGRADABLE;
2124e7f640dSJohn Baldwin 	if (opts & SX_DUPOK)
2134e7f640dSJohn Baldwin 		flags |= LO_DUPOK;
2144e7f640dSJohn Baldwin 	if (opts & SX_NOPROFILE)
2154e7f640dSJohn Baldwin 		flags |= LO_NOPROFILE;
2164e7f640dSJohn Baldwin 	if (!(opts & SX_NOWITNESS))
2174e7f640dSJohn Baldwin 		flags |= LO_WITNESS;
218f0830182SAttilio Rao 	if (opts & SX_RECURSE)
219f0830182SAttilio Rao 		flags |= LO_RECURSABLE;
2204e7f640dSJohn Baldwin 	if (opts & SX_QUIET)
2214e7f640dSJohn Baldwin 		flags |= LO_QUIET;
2224e7f640dSJohn Baldwin 
223f0830182SAttilio Rao 	flags |= opts & SX_NOADAPTIVE;
2244e7f640dSJohn Baldwin 	sx->sx_lock = SX_LOCK_UNLOCKED;
2254e7f640dSJohn Baldwin 	sx->sx_recurse = 0;
2264e7f640dSJohn Baldwin 	lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
2276281b30aSJason Evans }
2286281b30aSJason Evans 
2296281b30aSJason Evans void
2306281b30aSJason Evans sx_destroy(struct sx *sx)
2316281b30aSJason Evans {
2326281b30aSJason Evans 
2334e7f640dSJohn Baldwin 	KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
2344e7f640dSJohn Baldwin 	KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
2350026c92cSJohn Baldwin 	sx->sx_lock = SX_LOCK_DESTROYED;
236aa89d8cdSJohn Baldwin 	lock_destroy(&sx->lock_object);
2376281b30aSJason Evans }
2386281b30aSJason Evans 
239f9819486SAttilio Rao int
240f9819486SAttilio Rao _sx_slock(struct sx *sx, int opts, const char *file, int line)
2416281b30aSJason Evans {
242f9819486SAttilio Rao 	int error = 0;
2436281b30aSJason Evans 
2444e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
2450026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
2460026c92cSJohn Baldwin 	    ("sx_slock() of destroyed sx @ %s:%d", file, line));
24741313430SJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
248f9819486SAttilio Rao 	error = __sx_slock(sx, opts, file, line);
249f9819486SAttilio Rao 	if (!error) {
250aa89d8cdSJohn Baldwin 		LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
251aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, 0, file, line);
252764e4d54SJohn Baldwin 		curthread->td_locks++;
2536281b30aSJason Evans 	}
2546281b30aSJason Evans 
255f9819486SAttilio Rao 	return (error);
256f9819486SAttilio Rao }
257f9819486SAttilio Rao 
2585f36700aSJohn Baldwin int
2595f36700aSJohn Baldwin _sx_try_slock(struct sx *sx, const char *file, int line)
2605f36700aSJohn Baldwin {
2614e7f640dSJohn Baldwin 	uintptr_t x;
2625f36700aSJohn Baldwin 
263764a938bSPawel Jakub Dawidek 	for (;;) {
2644e7f640dSJohn Baldwin 		x = sx->sx_lock;
2650026c92cSJohn Baldwin 		KASSERT(x != SX_LOCK_DESTROYED,
2660026c92cSJohn Baldwin 		    ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
267764a938bSPawel Jakub Dawidek 		if (!(x & SX_LOCK_SHARED))
268764a938bSPawel Jakub Dawidek 			break;
269764a938bSPawel Jakub Dawidek 		if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
270aa89d8cdSJohn Baldwin 			LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
271aa89d8cdSJohn Baldwin 			WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
272764e4d54SJohn Baldwin 			curthread->td_locks++;
2735f36700aSJohn Baldwin 			return (1);
2745f36700aSJohn Baldwin 		}
275764a938bSPawel Jakub Dawidek 	}
2764e7f640dSJohn Baldwin 
2774e7f640dSJohn Baldwin 	LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
2784e7f640dSJohn Baldwin 	return (0);
2795f36700aSJohn Baldwin }
2805f36700aSJohn Baldwin 
281f9819486SAttilio Rao int
282f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line)
2836281b30aSJason Evans {
284f9819486SAttilio Rao 	int error = 0;
2856281b30aSJason Evans 
2864e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
2870026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
2880026c92cSJohn Baldwin 	    ("sx_xlock() of destroyed sx @ %s:%d", file, line));
289aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
29041313430SJohn Baldwin 	    line, NULL);
291f9819486SAttilio Rao 	error = __sx_xlock(sx, curthread, opts, file, line);
292f9819486SAttilio Rao 	if (!error) {
293f9819486SAttilio Rao 		LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
294f9819486SAttilio Rao 		    file, line);
295aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
296764e4d54SJohn Baldwin 		curthread->td_locks++;
2976281b30aSJason Evans 	}
2986281b30aSJason Evans 
299f9819486SAttilio Rao 	return (error);
300f9819486SAttilio Rao }
301f9819486SAttilio Rao 
3025f36700aSJohn Baldwin int
3035f36700aSJohn Baldwin _sx_try_xlock(struct sx *sx, const char *file, int line)
3045f36700aSJohn Baldwin {
3054e7f640dSJohn Baldwin 	int rval;
3065f36700aSJohn Baldwin 
3074e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3080026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3090026c92cSJohn Baldwin 	    ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
3104e7f640dSJohn Baldwin 
311f0830182SAttilio Rao 	if (sx_xlocked(sx) &&
312f0830182SAttilio Rao 	    (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
3134e7f640dSJohn Baldwin 		sx->sx_recurse++;
3144e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
3154e7f640dSJohn Baldwin 		rval = 1;
3164e7f640dSJohn Baldwin 	} else
3174e7f640dSJohn Baldwin 		rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
3184e7f640dSJohn Baldwin 		    (uintptr_t)curthread);
3194e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
3204e7f640dSJohn Baldwin 	if (rval) {
3214e7f640dSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
3224e7f640dSJohn Baldwin 		    file, line);
323764e4d54SJohn Baldwin 		curthread->td_locks++;
3245f36700aSJohn Baldwin 	}
3254e7f640dSJohn Baldwin 
3264e7f640dSJohn Baldwin 	return (rval);
3275f36700aSJohn Baldwin }
3285f36700aSJohn Baldwin 
3296281b30aSJason Evans void
33019284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line)
3316281b30aSJason Evans {
3326281b30aSJason Evans 
3334e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3340026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3350026c92cSJohn Baldwin 	    ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
3367ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
337764e4d54SJohn Baldwin 	curthread->td_locks--;
338aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
339aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
3404e7f640dSJohn Baldwin 	__sx_sunlock(sx, file, line);
341a5aedd68SStacey Son 	LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
3426281b30aSJason Evans }
3436281b30aSJason Evans 
3446281b30aSJason Evans void
34519284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line)
3466281b30aSJason Evans {
3476281b30aSJason Evans 
3484e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3490026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3500026c92cSJohn Baldwin 	    ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
3517ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED, file, line);
352764e4d54SJohn Baldwin 	curthread->td_locks--;
353aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
3544e7f640dSJohn Baldwin 	LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
3554e7f640dSJohn Baldwin 	    line);
356afc0bfbdSKip Macy 	if (!sx_recursed(sx))
357a5aedd68SStacey Son 		LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
3584e7f640dSJohn Baldwin 	__sx_xunlock(sx, curthread, file, line);
3596281b30aSJason Evans }
360d55229b7SJason Evans 
3614e7f640dSJohn Baldwin /*
3624e7f640dSJohn Baldwin  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
3634e7f640dSJohn Baldwin  * This will only succeed if this thread holds a single shared lock.
3644e7f640dSJohn Baldwin  * Return 1 if if the upgrade succeed, 0 otherwise.
3654e7f640dSJohn Baldwin  */
366d55229b7SJason Evans int
367d55229b7SJason Evans _sx_try_upgrade(struct sx *sx, const char *file, int line)
368d55229b7SJason Evans {
3694e7f640dSJohn Baldwin 	uintptr_t x;
3704e7f640dSJohn Baldwin 	int success;
371d55229b7SJason Evans 
3720026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3730026c92cSJohn Baldwin 	    ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
3747ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
375d55229b7SJason Evans 
3764e7f640dSJohn Baldwin 	/*
3774e7f640dSJohn Baldwin 	 * Try to switch from one shared lock to an exclusive lock.  We need
3784e7f640dSJohn Baldwin 	 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
3794e7f640dSJohn Baldwin 	 * we will wake up the exclusive waiters when we drop the lock.
3804e7f640dSJohn Baldwin 	 */
3814e7f640dSJohn Baldwin 	x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
3824e7f640dSJohn Baldwin 	success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
3834e7f640dSJohn Baldwin 	    (uintptr_t)curthread | x);
3844e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
385a5aedd68SStacey Son 	if (success) {
386aa89d8cdSJohn Baldwin 		WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
387b0b7cb50SJohn Baldwin 		    file, line);
388a5aedd68SStacey Son 		LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
389a5aedd68SStacey Son 	}
3904e7f640dSJohn Baldwin 	return (success);
391d55229b7SJason Evans }
392d55229b7SJason Evans 
3934e7f640dSJohn Baldwin /*
3944e7f640dSJohn Baldwin  * Downgrade an unrecursed exclusive lock into a single shared lock.
3954e7f640dSJohn Baldwin  */
396d55229b7SJason Evans void
397d55229b7SJason Evans _sx_downgrade(struct sx *sx, const char *file, int line)
398d55229b7SJason Evans {
3994e7f640dSJohn Baldwin 	uintptr_t x;
400da7bbd2cSJohn Baldwin 	int wakeup_swapper;
401d55229b7SJason Evans 
4020026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4030026c92cSJohn Baldwin 	    ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
4047ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
4054e7f640dSJohn Baldwin #ifndef INVARIANTS
4064e7f640dSJohn Baldwin 	if (sx_recursed(sx))
4074e7f640dSJohn Baldwin 		panic("downgrade of a recursed lock");
4084e7f640dSJohn Baldwin #endif
409d55229b7SJason Evans 
410aa89d8cdSJohn Baldwin 	WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
411d55229b7SJason Evans 
4124e7f640dSJohn Baldwin 	/*
4134e7f640dSJohn Baldwin 	 * Try to switch from an exclusive lock with no shared waiters
4144e7f640dSJohn Baldwin 	 * to one sharer with no shared waiters.  If there are
4154e7f640dSJohn Baldwin 	 * exclusive waiters, we don't need to lock the sleep queue so
4164e7f640dSJohn Baldwin 	 * long as we preserve the flag.  We do one quick try and if
4174e7f640dSJohn Baldwin 	 * that fails we grab the sleepq lock to keep the flags from
4184e7f640dSJohn Baldwin 	 * changing and do it the slow way.
4194e7f640dSJohn Baldwin 	 *
4204e7f640dSJohn Baldwin 	 * We have to lock the sleep queue if there are shared waiters
4214e7f640dSJohn Baldwin 	 * so we can wake them up.
4224e7f640dSJohn Baldwin 	 */
4234e7f640dSJohn Baldwin 	x = sx->sx_lock;
4244e7f640dSJohn Baldwin 	if (!(x & SX_LOCK_SHARED_WAITERS) &&
4254e7f640dSJohn Baldwin 	    atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
4264e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
4274e7f640dSJohn Baldwin 		LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
4284e7f640dSJohn Baldwin 		return;
4294e7f640dSJohn Baldwin 	}
4304e7f640dSJohn Baldwin 
4314e7f640dSJohn Baldwin 	/*
4324e7f640dSJohn Baldwin 	 * Lock the sleep queue so we can read the waiters bits
4334e7f640dSJohn Baldwin 	 * without any races and wakeup any shared waiters.
4344e7f640dSJohn Baldwin 	 */
4354e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
4364e7f640dSJohn Baldwin 
4374e7f640dSJohn Baldwin 	/*
4384e7f640dSJohn Baldwin 	 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
4394e7f640dSJohn Baldwin 	 * shared lock.  If there are any shared waiters, wake them up.
4404e7f640dSJohn Baldwin 	 */
441da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
4424e7f640dSJohn Baldwin 	x = sx->sx_lock;
4434e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
4444e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS));
4454e7f640dSJohn Baldwin 	if (x & SX_LOCK_SHARED_WAITERS)
446da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
447da7bbd2cSJohn Baldwin 		    0, SQ_SHARED_QUEUE);
4484e7f640dSJohn Baldwin 	sleepq_release(&sx->lock_object);
449d55229b7SJason Evans 
450aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
451a5aedd68SStacey Son 	LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
452da7bbd2cSJohn Baldwin 
453da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
454da7bbd2cSJohn Baldwin 		kick_proc0();
4554e7f640dSJohn Baldwin }
456d55229b7SJason Evans 
4574e7f640dSJohn Baldwin /*
4584e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xlock
4594e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
4604e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
4614e7f640dSJohn Baldwin  * accessible from at least sx.h.
4624e7f640dSJohn Baldwin  */
463f9819486SAttilio Rao int
464f9819486SAttilio Rao _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
465f9819486SAttilio Rao     int line)
4664e7f640dSJohn Baldwin {
4674e7f640dSJohn Baldwin 	GIANT_DECLARE;
4684e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
4694e7f640dSJohn Baldwin 	volatile struct thread *owner;
4701ae1c2a3SAttilio Rao 	u_int i, spintries = 0;
4714e7f640dSJohn Baldwin #endif
4724e7f640dSJohn Baldwin 	uintptr_t x;
4731723a064SJeff Roberson #ifdef LOCK_PROFILING
4741723a064SJeff Roberson 	uint64_t waittime = 0;
4751723a064SJeff Roberson 	int contested = 0;
4761723a064SJeff Roberson #endif
4771723a064SJeff Roberson 	int error = 0;
478a5aedd68SStacey Son #ifdef	KDTRACE_HOOKS
479a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
480a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
481a5aedd68SStacey Son 	int64_t sleep_time = 0;
482a5aedd68SStacey Son #endif
4834e7f640dSJohn Baldwin 
4844e7f640dSJohn Baldwin 	/* If we already hold an exclusive lock, then recurse. */
4854e7f640dSJohn Baldwin 	if (sx_xlocked(sx)) {
486f0830182SAttilio Rao 		KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
487b0d67325SJohn Baldwin 	    ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
488b0d67325SJohn Baldwin 		    sx->lock_object.lo_name, file, line));
4894e7f640dSJohn Baldwin 		sx->sx_recurse++;
4904e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
4914e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
4924e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
493f9819486SAttilio Rao 		return (0);
4944e7f640dSJohn Baldwin 	}
4954e7f640dSJohn Baldwin 
4964e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
4974e7f640dSJohn Baldwin 		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
4984e7f640dSJohn Baldwin 		    sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
4994e7f640dSJohn Baldwin 
5004e7f640dSJohn Baldwin 	while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
501a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
502a5aedd68SStacey Son 		spin_cnt++;
503a5aedd68SStacey Son #endif
504eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
505eea4f254SJeff Roberson 		    &waittime);
5064e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5074e7f640dSJohn Baldwin 		/*
5084e7f640dSJohn Baldwin 		 * If the lock is write locked and the owner is
5094e7f640dSJohn Baldwin 		 * running on another CPU, spin until the owner stops
5104e7f640dSJohn Baldwin 		 * running or the state of the lock changes.
5114e7f640dSJohn Baldwin 		 */
5124e7f640dSJohn Baldwin 		x = sx->sx_lock;
5138545538bSJohn Baldwin 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
5141ae1c2a3SAttilio Rao 			if ((x & SX_LOCK_SHARED) == 0) {
5154e7f640dSJohn Baldwin 				x = SX_OWNER(x);
5164e7f640dSJohn Baldwin 				owner = (struct thread *)x;
5174e7f640dSJohn Baldwin 				if (TD_IS_RUNNING(owner)) {
5184e7f640dSJohn Baldwin 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5194e7f640dSJohn Baldwin 						CTR3(KTR_LOCK,
5204e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
5214e7f640dSJohn Baldwin 						    __func__, sx, owner);
5224e7f640dSJohn Baldwin 					GIANT_SAVE();
5234e7f640dSJohn Baldwin 					while (SX_OWNER(sx->sx_lock) == x &&
524a5aedd68SStacey Son 					    TD_IS_RUNNING(owner)) {
5254e7f640dSJohn Baldwin 						cpu_spinwait();
526a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
527a5aedd68SStacey Son 						spin_cnt++;
528a5aedd68SStacey Son #endif
529a5aedd68SStacey Son 					}
5304e7f640dSJohn Baldwin 					continue;
5314e7f640dSJohn Baldwin 				}
5321ae1c2a3SAttilio Rao 			} else if (SX_SHARERS(x) && spintries < asx_retries) {
5338d3635c4SAttilio Rao 				GIANT_SAVE();
5341ae1c2a3SAttilio Rao 				spintries++;
5351ae1c2a3SAttilio Rao 				for (i = 0; i < asx_loops; i++) {
5361ae1c2a3SAttilio Rao 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5371ae1c2a3SAttilio Rao 						CTR4(KTR_LOCK,
5381ae1c2a3SAttilio Rao 				    "%s: shared spinning on %p with %u and %u",
5391ae1c2a3SAttilio Rao 						    __func__, sx, spintries, i);
5401ae1c2a3SAttilio Rao 					x = sx->sx_lock;
5411ae1c2a3SAttilio Rao 					if ((x & SX_LOCK_SHARED) == 0 ||
5421ae1c2a3SAttilio Rao 					    SX_SHARERS(x) == 0)
5431ae1c2a3SAttilio Rao 						break;
5441ae1c2a3SAttilio Rao 					cpu_spinwait();
5451ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS
5461ae1c2a3SAttilio Rao 					spin_cnt++;
5471ae1c2a3SAttilio Rao #endif
5481ae1c2a3SAttilio Rao 				}
5491ae1c2a3SAttilio Rao 				if (i != asx_loops)
5501ae1c2a3SAttilio Rao 					continue;
5511ae1c2a3SAttilio Rao 			}
5524e7f640dSJohn Baldwin 		}
5534e7f640dSJohn Baldwin #endif
5544e7f640dSJohn Baldwin 
5554e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
5564e7f640dSJohn Baldwin 		x = sx->sx_lock;
5574e7f640dSJohn Baldwin 
5584e7f640dSJohn Baldwin 		/*
5594e7f640dSJohn Baldwin 		 * If the lock was released while spinning on the
5604e7f640dSJohn Baldwin 		 * sleep queue chain lock, try again.
5614e7f640dSJohn Baldwin 		 */
5624e7f640dSJohn Baldwin 		if (x == SX_LOCK_UNLOCKED) {
5634e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
5644e7f640dSJohn Baldwin 			continue;
5654e7f640dSJohn Baldwin 		}
5664e7f640dSJohn Baldwin 
5674e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5684e7f640dSJohn Baldwin 		/*
5694e7f640dSJohn Baldwin 		 * The current lock owner might have started executing
5704e7f640dSJohn Baldwin 		 * on another CPU (or the lock could have changed
5714e7f640dSJohn Baldwin 		 * owners) while we were waiting on the sleep queue
5724e7f640dSJohn Baldwin 		 * chain lock.  If so, drop the sleep queue lock and try
5734e7f640dSJohn Baldwin 		 * again.
5744e7f640dSJohn Baldwin 		 */
5754e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
5761ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
5774e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
5784e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
5794e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
5804e7f640dSJohn Baldwin 				continue;
5814e7f640dSJohn Baldwin 			}
5824e7f640dSJohn Baldwin 		}
5834e7f640dSJohn Baldwin #endif
5844e7f640dSJohn Baldwin 
5854e7f640dSJohn Baldwin 		/*
5864e7f640dSJohn Baldwin 		 * If an exclusive lock was released with both shared
5874e7f640dSJohn Baldwin 		 * and exclusive waiters and a shared waiter hasn't
5884e7f640dSJohn Baldwin 		 * woken up and acquired the lock yet, sx_lock will be
5894e7f640dSJohn Baldwin 		 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
5904e7f640dSJohn Baldwin 		 * If we see that value, try to acquire it once.  Note
5914e7f640dSJohn Baldwin 		 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
5924e7f640dSJohn Baldwin 		 * as there are other exclusive waiters still.  If we
5934e7f640dSJohn Baldwin 		 * fail, restart the loop.
5944e7f640dSJohn Baldwin 		 */
5954e7f640dSJohn Baldwin 		if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
5964e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock,
5974e7f640dSJohn Baldwin 			    SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
5984e7f640dSJohn Baldwin 			    tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
5994e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6004e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
6014e7f640dSJohn Baldwin 				    __func__, sx);
6024e7f640dSJohn Baldwin 				break;
6034e7f640dSJohn Baldwin 			}
6044e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6054e7f640dSJohn Baldwin 			continue;
6064e7f640dSJohn Baldwin 		}
6074e7f640dSJohn Baldwin 
6084e7f640dSJohn Baldwin 		/*
6094e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
6104e7f640dSJohn Baldwin 		 * than loop back and retry.
6114e7f640dSJohn Baldwin 		 */
6124e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
6134e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
6144e7f640dSJohn Baldwin 			    x | SX_LOCK_EXCLUSIVE_WAITERS)) {
6154e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6164e7f640dSJohn Baldwin 				continue;
6174e7f640dSJohn Baldwin 			}
6184e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
6194e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
6204e7f640dSJohn Baldwin 				    __func__, sx);
6214e7f640dSJohn Baldwin 		}
6224e7f640dSJohn Baldwin 
6234e7f640dSJohn Baldwin 		/*
6244e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the exclusive
6254e7f640dSJohn Baldwin 		 * lock and the exclusive waiters flag is set, we have
6264e7f640dSJohn Baldwin 		 * to sleep.
6274e7f640dSJohn Baldwin 		 */
6284e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6294e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
6304e7f640dSJohn Baldwin 			    __func__, sx);
6314e7f640dSJohn Baldwin 
632a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
633a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
634a5aedd68SStacey Son #endif
6354e7f640dSJohn Baldwin 		GIANT_SAVE();
6364e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
637f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
638f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
639f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
640c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
641f9819486SAttilio Rao 		else
642c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
643a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
644a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
645a5aedd68SStacey Son 		sleep_cnt++;
646a5aedd68SStacey Son #endif
647f9819486SAttilio Rao 		if (error) {
648f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
649f9819486SAttilio Rao 				CTR2(KTR_LOCK,
650f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
651f9819486SAttilio Rao 				    __func__, sx);
652f9819486SAttilio Rao 			break;
653f9819486SAttilio Rao 		}
6544e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6554e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
6564e7f640dSJohn Baldwin 			    __func__, sx);
6574e7f640dSJohn Baldwin 	}
6584e7f640dSJohn Baldwin 
6594e7f640dSJohn Baldwin 	GIANT_RESTORE();
660f9819486SAttilio Rao 	if (!error)
661a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
662a5aedd68SStacey Son 		    contested, waittime, file, line);
663a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
664a5aedd68SStacey Son 	if (sleep_time)
665a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
666a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
667a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
668a5aedd68SStacey Son #endif
669f9819486SAttilio Rao 	return (error);
6704e7f640dSJohn Baldwin }
6714e7f640dSJohn Baldwin 
6724e7f640dSJohn Baldwin /*
6734e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xunlock
6744e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
6754e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
6764e7f640dSJohn Baldwin  * accessible from at least sx.h.
6774e7f640dSJohn Baldwin  */
6784e7f640dSJohn Baldwin void
6794e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
6804e7f640dSJohn Baldwin {
6814e7f640dSJohn Baldwin 	uintptr_t x;
682da7bbd2cSJohn Baldwin 	int queue, wakeup_swapper;
6834e7f640dSJohn Baldwin 
6844e7f640dSJohn Baldwin 	MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
6854e7f640dSJohn Baldwin 
6864e7f640dSJohn Baldwin 	/* If the lock is recursed, then unrecurse one level. */
6874e7f640dSJohn Baldwin 	if (sx_xlocked(sx) && sx_recursed(sx)) {
6884e7f640dSJohn Baldwin 		if ((--sx->sx_recurse) == 0)
6894e7f640dSJohn Baldwin 			atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
6904e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6914e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
6924e7f640dSJohn Baldwin 		return;
6934e7f640dSJohn Baldwin 	}
6944e7f640dSJohn Baldwin 	MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
6954e7f640dSJohn Baldwin 	    SX_LOCK_EXCLUSIVE_WAITERS));
6964e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
6974e7f640dSJohn Baldwin 		CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
6984e7f640dSJohn Baldwin 
6994e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
7004e7f640dSJohn Baldwin 	x = SX_LOCK_UNLOCKED;
7014e7f640dSJohn Baldwin 
7024e7f640dSJohn Baldwin 	/*
7034e7f640dSJohn Baldwin 	 * The wake up algorithm here is quite simple and probably not
7044e7f640dSJohn Baldwin 	 * ideal.  It gives precedence to shared waiters if they are
7054e7f640dSJohn Baldwin 	 * present.  For this condition, we have to preserve the
7064e7f640dSJohn Baldwin 	 * state of the exclusive waiters flag.
7072028867dSAttilio Rao 	 * If interruptible sleeps left the shared queue empty avoid a
7082028867dSAttilio Rao 	 * starvation for the threads sleeping on the exclusive queue by giving
7092028867dSAttilio Rao 	 * them precedence and cleaning up the shared waiters bit anyway.
7104e7f640dSJohn Baldwin 	 */
7112028867dSAttilio Rao 	if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
7122028867dSAttilio Rao 	    sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
7134e7f640dSJohn Baldwin 		queue = SQ_SHARED_QUEUE;
7144e7f640dSJohn Baldwin 		x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
7154e7f640dSJohn Baldwin 	} else
7164e7f640dSJohn Baldwin 		queue = SQ_EXCLUSIVE_QUEUE;
7174e7f640dSJohn Baldwin 
7184e7f640dSJohn Baldwin 	/* Wake up all the waiters for the specific queue. */
7194e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7204e7f640dSJohn Baldwin 		CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
7214e7f640dSJohn Baldwin 		    __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
7224e7f640dSJohn Baldwin 		    "exclusive");
7234e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, x);
724da7bbd2cSJohn Baldwin 	wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
725da7bbd2cSJohn Baldwin 	    queue);
726c5aa6b58SJeff Roberson 	sleepq_release(&sx->lock_object);
727da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
728da7bbd2cSJohn Baldwin 		kick_proc0();
7294e7f640dSJohn Baldwin }
7304e7f640dSJohn Baldwin 
7314e7f640dSJohn Baldwin /*
7324e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_slock
7334e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7344e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7354e7f640dSJohn Baldwin  * accessible from at least sx.h.
7364e7f640dSJohn Baldwin  */
737f9819486SAttilio Rao int
738f9819486SAttilio Rao _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
7394e7f640dSJohn Baldwin {
7404e7f640dSJohn Baldwin 	GIANT_DECLARE;
7414e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
7424e7f640dSJohn Baldwin 	volatile struct thread *owner;
7434e7f640dSJohn Baldwin #endif
7441723a064SJeff Roberson #ifdef LOCK_PROFILING
745c1a6d9faSAttilio Rao 	uint64_t waittime = 0;
746c1a6d9faSAttilio Rao 	int contested = 0;
7471723a064SJeff Roberson #endif
7484e7f640dSJohn Baldwin 	uintptr_t x;
749c1a6d9faSAttilio Rao 	int error = 0;
750a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
751a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
752a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
753a5aedd68SStacey Son 	int64_t sleep_time = 0;
754a5aedd68SStacey Son #endif
755c1a6d9faSAttilio Rao 
7564e7f640dSJohn Baldwin 	/*
7574e7f640dSJohn Baldwin 	 * As with rwlocks, we don't make any attempt to try to block
7584e7f640dSJohn Baldwin 	 * shared locks once there is an exclusive waiter.
7594e7f640dSJohn Baldwin 	 */
7604e7f640dSJohn Baldwin 	for (;;) {
761a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
762a5aedd68SStacey Son 		spin_cnt++;
763a5aedd68SStacey Son #endif
7644e7f640dSJohn Baldwin 		x = sx->sx_lock;
7654e7f640dSJohn Baldwin 
7664e7f640dSJohn Baldwin 		/*
7674e7f640dSJohn Baldwin 		 * If no other thread has an exclusive lock then try to bump up
7684e7f640dSJohn Baldwin 		 * the count of sharers.  Since we have to preserve the state
7694e7f640dSJohn Baldwin 		 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
7704e7f640dSJohn Baldwin 		 * shared lock loop back and retry.
7714e7f640dSJohn Baldwin 		 */
7724e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
7734e7f640dSJohn Baldwin 			MPASS(!(x & SX_LOCK_SHARED_WAITERS));
7744e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
7754e7f640dSJohn Baldwin 			    x + SX_ONE_SHARER)) {
7764e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
7774e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
7784e7f640dSJohn Baldwin 					    "%s: %p succeed %p -> %p", __func__,
7794e7f640dSJohn Baldwin 					    sx, (void *)x,
7804e7f640dSJohn Baldwin 					    (void *)(x + SX_ONE_SHARER));
7814e7f640dSJohn Baldwin 				break;
7824e7f640dSJohn Baldwin 			}
7834e7f640dSJohn Baldwin 			continue;
7844e7f640dSJohn Baldwin 		}
785eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
786eea4f254SJeff Roberson 		    &waittime);
7874e7f640dSJohn Baldwin 
7884e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
7894e7f640dSJohn Baldwin 		/*
7904e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
7914e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
7924e7f640dSJohn Baldwin 		 * changes.
7934e7f640dSJohn Baldwin 		 */
7941ae1c2a3SAttilio Rao 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
7954e7f640dSJohn Baldwin 			x = SX_OWNER(x);
7964e7f640dSJohn Baldwin 			owner = (struct thread *)x;
7974e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
7984e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
7994e7f640dSJohn Baldwin 					CTR3(KTR_LOCK,
8004e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
8014e7f640dSJohn Baldwin 					    __func__, sx, owner);
8024e7f640dSJohn Baldwin 				GIANT_SAVE();
8034e7f640dSJohn Baldwin 				while (SX_OWNER(sx->sx_lock) == x &&
804a5aedd68SStacey Son 				    TD_IS_RUNNING(owner)) {
805a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
806a5aedd68SStacey Son 					spin_cnt++;
807a5aedd68SStacey Son #endif
8084e7f640dSJohn Baldwin 					cpu_spinwait();
809a5aedd68SStacey Son 				}
8104e7f640dSJohn Baldwin 				continue;
8114e7f640dSJohn Baldwin 			}
8124e7f640dSJohn Baldwin 		}
8134e7f640dSJohn Baldwin #endif
8144e7f640dSJohn Baldwin 
8154e7f640dSJohn Baldwin 		/*
8164e7f640dSJohn Baldwin 		 * Some other thread already has an exclusive lock, so
8174e7f640dSJohn Baldwin 		 * start the process of blocking.
8184e7f640dSJohn Baldwin 		 */
8194e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
8204e7f640dSJohn Baldwin 		x = sx->sx_lock;
8214e7f640dSJohn Baldwin 
8224e7f640dSJohn Baldwin 		/*
8234e7f640dSJohn Baldwin 		 * The lock could have been released while we spun.
8244e7f640dSJohn Baldwin 		 * In this case loop back and retry.
8254e7f640dSJohn Baldwin 		 */
8264e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8274e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
8284e7f640dSJohn Baldwin 			continue;
8294e7f640dSJohn Baldwin 		}
8304e7f640dSJohn Baldwin 
8314e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8324e7f640dSJohn Baldwin 		/*
8334e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8344e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8354e7f640dSJohn Baldwin 		 * changes.
8364e7f640dSJohn Baldwin 		 */
8374e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
8381ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8394e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
8404e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8414e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
8424e7f640dSJohn Baldwin 				continue;
8434e7f640dSJohn Baldwin 			}
8444e7f640dSJohn Baldwin 		}
8454e7f640dSJohn Baldwin #endif
8464e7f640dSJohn Baldwin 
8474e7f640dSJohn Baldwin 		/*
8484e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
8494e7f640dSJohn Baldwin 		 * fail to set it drop the sleep queue lock and loop
8504e7f640dSJohn Baldwin 		 * back.
8514e7f640dSJohn Baldwin 		 */
8524e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED_WAITERS)) {
8534e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
8544e7f640dSJohn Baldwin 			    x | SX_LOCK_SHARED_WAITERS)) {
8554e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
8564e7f640dSJohn Baldwin 				continue;
8574e7f640dSJohn Baldwin 			}
8584e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
8594e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
8604e7f640dSJohn Baldwin 				    __func__, sx);
8614e7f640dSJohn Baldwin 		}
8624e7f640dSJohn Baldwin 
8634e7f640dSJohn Baldwin 		/*
8644e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the shared lock,
8654e7f640dSJohn Baldwin 		 * we have to sleep.
8664e7f640dSJohn Baldwin 		 */
8674e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
8684e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
8694e7f640dSJohn Baldwin 			    __func__, sx);
8704e7f640dSJohn Baldwin 
871a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
872a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
873a5aedd68SStacey Son #endif
8744e7f640dSJohn Baldwin 		GIANT_SAVE();
8754e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
876f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
877f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
878f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
879c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
880f9819486SAttilio Rao 		else
881c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
882a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
883a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
884a5aedd68SStacey Son 		sleep_cnt++;
885a5aedd68SStacey Son #endif
886f9819486SAttilio Rao 		if (error) {
887f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
888f9819486SAttilio Rao 				CTR2(KTR_LOCK,
889f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
890f9819486SAttilio Rao 				    __func__, sx);
891f9819486SAttilio Rao 			break;
892f9819486SAttilio Rao 		}
8934e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
8944e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
8954e7f640dSJohn Baldwin 			    __func__, sx);
8964e7f640dSJohn Baldwin 	}
897eea4f254SJeff Roberson 	if (error == 0)
898a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
899a5aedd68SStacey Son 		    contested, waittime, file, line);
900a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
901a5aedd68SStacey Son 	if (sleep_time)
902a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
903a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
904a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
905a5aedd68SStacey Son #endif
9064e7f640dSJohn Baldwin 	GIANT_RESTORE();
907f9819486SAttilio Rao 	return (error);
9084e7f640dSJohn Baldwin }
9094e7f640dSJohn Baldwin 
9104e7f640dSJohn Baldwin /*
9114e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_sunlock
9124e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
9134e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
9144e7f640dSJohn Baldwin  * accessible from at least sx.h.
9154e7f640dSJohn Baldwin  */
9164e7f640dSJohn Baldwin void
9174e7f640dSJohn Baldwin _sx_sunlock_hard(struct sx *sx, const char *file, int line)
9184e7f640dSJohn Baldwin {
9194e7f640dSJohn Baldwin 	uintptr_t x;
920da7bbd2cSJohn Baldwin 	int wakeup_swapper;
9214e7f640dSJohn Baldwin 
9224e7f640dSJohn Baldwin 	for (;;) {
9234e7f640dSJohn Baldwin 		x = sx->sx_lock;
9244e7f640dSJohn Baldwin 
9254e7f640dSJohn Baldwin 		/*
9264e7f640dSJohn Baldwin 		 * We should never have sharers while at least one thread
9274e7f640dSJohn Baldwin 		 * holds a shared lock.
9284e7f640dSJohn Baldwin 		 */
9294e7f640dSJohn Baldwin 		KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
9304e7f640dSJohn Baldwin 		    ("%s: waiting sharers", __func__));
9314e7f640dSJohn Baldwin 
9324e7f640dSJohn Baldwin 		/*
9334e7f640dSJohn Baldwin 		 * See if there is more than one shared lock held.  If
9344e7f640dSJohn Baldwin 		 * so, just drop one and return.
9354e7f640dSJohn Baldwin 		 */
9364e7f640dSJohn Baldwin 		if (SX_SHARERS(x) > 1) {
937ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
9384e7f640dSJohn Baldwin 			    x - SX_ONE_SHARER)) {
9394e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
9404e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
9414e7f640dSJohn Baldwin 					    "%s: %p succeeded %p -> %p",
9424e7f640dSJohn Baldwin 					    __func__, sx, (void *)x,
9434e7f640dSJohn Baldwin 					    (void *)(x - SX_ONE_SHARER));
9444e7f640dSJohn Baldwin 				break;
9454e7f640dSJohn Baldwin 			}
9464e7f640dSJohn Baldwin 			continue;
9474e7f640dSJohn Baldwin 		}
9484e7f640dSJohn Baldwin 
9494e7f640dSJohn Baldwin 		/*
9504e7f640dSJohn Baldwin 		 * If there aren't any waiters for an exclusive lock,
9514e7f640dSJohn Baldwin 		 * then try to drop it quickly.
9524e7f640dSJohn Baldwin 		 */
9534e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
9544e7f640dSJohn Baldwin 			MPASS(x == SX_SHARERS_LOCK(1));
955ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock,
956ddce63caSAttilio Rao 			    SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
9574e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
9584e7f640dSJohn Baldwin 					CTR2(KTR_LOCK, "%s: %p last succeeded",
9594e7f640dSJohn Baldwin 					    __func__, sx);
9604e7f640dSJohn Baldwin 				break;
9614e7f640dSJohn Baldwin 			}
9624e7f640dSJohn Baldwin 			continue;
9634e7f640dSJohn Baldwin 		}
9644e7f640dSJohn Baldwin 
9654e7f640dSJohn Baldwin 		/*
9664e7f640dSJohn Baldwin 		 * At this point, there should just be one sharer with
9674e7f640dSJohn Baldwin 		 * exclusive waiters.
9684e7f640dSJohn Baldwin 		 */
9694e7f640dSJohn Baldwin 		MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
9704e7f640dSJohn Baldwin 
9714e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
9724e7f640dSJohn Baldwin 
9734e7f640dSJohn Baldwin 		/*
9744e7f640dSJohn Baldwin 		 * Wake up semantic here is quite simple:
9754e7f640dSJohn Baldwin 		 * Just wake up all the exclusive waiters.
9764e7f640dSJohn Baldwin 		 * Note that the state of the lock could have changed,
9774e7f640dSJohn Baldwin 		 * so if it fails loop back and retry.
9784e7f640dSJohn Baldwin 		 */
979ddce63caSAttilio Rao 		if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
9804e7f640dSJohn Baldwin 		    SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
9814e7f640dSJohn Baldwin 		    SX_LOCK_UNLOCKED)) {
9824e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
9834e7f640dSJohn Baldwin 			continue;
9844e7f640dSJohn Baldwin 		}
9854e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9864e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p waking up all thread on"
9874e7f640dSJohn Baldwin 			    "exclusive queue", __func__, sx);
988da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
989da7bbd2cSJohn Baldwin 		    0, SQ_EXCLUSIVE_QUEUE);
990c5aa6b58SJeff Roberson 		sleepq_release(&sx->lock_object);
991da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
992da7bbd2cSJohn Baldwin 			kick_proc0();
9934e7f640dSJohn Baldwin 		break;
9944e7f640dSJohn Baldwin 	}
995d55229b7SJason Evans }
9964e5e677bSJohn Baldwin 
9974e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT
998781a35dfSJohn Baldwin #ifndef INVARIANTS
999781a35dfSJohn Baldwin #undef	_sx_assert
1000781a35dfSJohn Baldwin #endif
1001781a35dfSJohn Baldwin 
10024e5e677bSJohn Baldwin /*
10034e5e677bSJohn Baldwin  * In the non-WITNESS case, sx_assert() can only detect that at least
10044e5e677bSJohn Baldwin  * *some* thread owns an slock, but it cannot guarantee that *this*
10054e5e677bSJohn Baldwin  * thread owns an slock.
10064e5e677bSJohn Baldwin  */
10074e5e677bSJohn Baldwin void
1008*d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line)
10094e5e677bSJohn Baldwin {
10104e7f640dSJohn Baldwin #ifndef WITNESS
10114e7f640dSJohn Baldwin 	int slocked = 0;
10124e7f640dSJohn Baldwin #endif
10134e5e677bSJohn Baldwin 
101403129ba9SJohn Baldwin 	if (panicstr != NULL)
101503129ba9SJohn Baldwin 		return;
10164e5e677bSJohn Baldwin 	switch (what) {
10177ec137e5SJohn Baldwin 	case SA_SLOCKED:
10187ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_NOTRECURSED:
10197ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_RECURSED:
10204e7f640dSJohn Baldwin #ifndef WITNESS
10214e7f640dSJohn Baldwin 		slocked = 1;
10224e7f640dSJohn Baldwin 		/* FALLTHROUGH */
10234e7f640dSJohn Baldwin #endif
10247ec137e5SJohn Baldwin 	case SA_LOCKED:
10257ec137e5SJohn Baldwin 	case SA_LOCKED | SA_NOTRECURSED:
10267ec137e5SJohn Baldwin 	case SA_LOCKED | SA_RECURSED:
10274e5e677bSJohn Baldwin #ifdef WITNESS
1028aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
10294e5e677bSJohn Baldwin #else
10304e7f640dSJohn Baldwin 		/*
10314e7f640dSJohn Baldwin 		 * If some other thread has an exclusive lock or we
10324e7f640dSJohn Baldwin 		 * have one and are asserting a shared lock, fail.
10334e7f640dSJohn Baldwin 		 * Also, if no one has a lock at all, fail.
10344e7f640dSJohn Baldwin 		 */
10354e7f640dSJohn Baldwin 		if (sx->sx_lock == SX_LOCK_UNLOCKED ||
10364e7f640dSJohn Baldwin 		    (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
10374e7f640dSJohn Baldwin 		    sx_xholder(sx) != curthread)))
103803129ba9SJohn Baldwin 			panic("Lock %s not %slocked @ %s:%d\n",
10394e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, slocked ? "share " : "",
10404e7f640dSJohn Baldwin 			    file, line);
10414e7f640dSJohn Baldwin 
10424e7f640dSJohn Baldwin 		if (!(sx->sx_lock & SX_LOCK_SHARED)) {
10434e7f640dSJohn Baldwin 			if (sx_recursed(sx)) {
10447ec137e5SJohn Baldwin 				if (what & SA_NOTRECURSED)
10454e7f640dSJohn Baldwin 					panic("Lock %s recursed @ %s:%d\n",
10464e7f640dSJohn Baldwin 					    sx->lock_object.lo_name, file,
10474e7f640dSJohn Baldwin 					    line);
10487ec137e5SJohn Baldwin 			} else if (what & SA_RECURSED)
10494e7f640dSJohn Baldwin 				panic("Lock %s not recursed @ %s:%d\n",
10504e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
10514e7f640dSJohn Baldwin 		}
10524e5e677bSJohn Baldwin #endif
10534e5e677bSJohn Baldwin 		break;
10547ec137e5SJohn Baldwin 	case SA_XLOCKED:
10557ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_NOTRECURSED:
10567ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_RECURSED:
10574e7f640dSJohn Baldwin 		if (sx_xholder(sx) != curthread)
105803129ba9SJohn Baldwin 			panic("Lock %s not exclusively locked @ %s:%d\n",
1059aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
10604e7f640dSJohn Baldwin 		if (sx_recursed(sx)) {
10617ec137e5SJohn Baldwin 			if (what & SA_NOTRECURSED)
10624e7f640dSJohn Baldwin 				panic("Lock %s recursed @ %s:%d\n",
10634e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
10647ec137e5SJohn Baldwin 		} else if (what & SA_RECURSED)
10654e7f640dSJohn Baldwin 			panic("Lock %s not recursed @ %s:%d\n",
10664e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
10674e5e677bSJohn Baldwin 		break;
10687ec137e5SJohn Baldwin 	case SA_UNLOCKED:
106919b0efd3SPawel Jakub Dawidek #ifdef WITNESS
1070aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
107119b0efd3SPawel Jakub Dawidek #else
1072f6739b1dSPawel Jakub Dawidek 		/*
10734e7f640dSJohn Baldwin 		 * If we hold an exclusve lock fail.  We can't
10744e7f640dSJohn Baldwin 		 * reliably check to see if we hold a shared lock or
10754e7f640dSJohn Baldwin 		 * not.
1076f6739b1dSPawel Jakub Dawidek 		 */
10774e7f640dSJohn Baldwin 		if (sx_xholder(sx) == curthread)
107803129ba9SJohn Baldwin 			panic("Lock %s exclusively locked @ %s:%d\n",
1079aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
108019b0efd3SPawel Jakub Dawidek #endif
108119b0efd3SPawel Jakub Dawidek 		break;
10824e5e677bSJohn Baldwin 	default:
10834e5e677bSJohn Baldwin 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
10844e5e677bSJohn Baldwin 		    line);
10854e5e677bSJohn Baldwin 	}
10864e5e677bSJohn Baldwin }
10874e5e677bSJohn Baldwin #endif	/* INVARIANT_SUPPORT */
1088d272fe53SJohn Baldwin 
1089d272fe53SJohn Baldwin #ifdef DDB
10904e7f640dSJohn Baldwin static void
1091*d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock)
1092d272fe53SJohn Baldwin {
1093d272fe53SJohn Baldwin 	struct thread *td;
1094*d576deedSPawel Jakub Dawidek 	const struct sx *sx;
1095d272fe53SJohn Baldwin 
1096*d576deedSPawel Jakub Dawidek 	sx = (const struct sx *)lock;
1097d272fe53SJohn Baldwin 
1098d272fe53SJohn Baldwin 	db_printf(" state: ");
10994e7f640dSJohn Baldwin 	if (sx->sx_lock == SX_LOCK_UNLOCKED)
11004e7f640dSJohn Baldwin 		db_printf("UNLOCKED\n");
11010026c92cSJohn Baldwin 	else if (sx->sx_lock == SX_LOCK_DESTROYED) {
11020026c92cSJohn Baldwin 		db_printf("DESTROYED\n");
11030026c92cSJohn Baldwin 		return;
11040026c92cSJohn Baldwin 	} else if (sx->sx_lock & SX_LOCK_SHARED)
11054e7f640dSJohn Baldwin 		db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
11064e7f640dSJohn Baldwin 	else {
11074e7f640dSJohn Baldwin 		td = sx_xholder(sx);
1108d272fe53SJohn Baldwin 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1109431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
11104e7f640dSJohn Baldwin 		if (sx_recursed(sx))
11114e7f640dSJohn Baldwin 			db_printf(" recursed: %d\n", sx->sx_recurse);
11124e7f640dSJohn Baldwin 	}
11134e7f640dSJohn Baldwin 
11144e7f640dSJohn Baldwin 	db_printf(" waiters: ");
11154e7f640dSJohn Baldwin 	switch(sx->sx_lock &
11164e7f640dSJohn Baldwin 	    (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
11174e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS:
11184e7f640dSJohn Baldwin 		db_printf("shared\n");
11194e7f640dSJohn Baldwin 		break;
11204e7f640dSJohn Baldwin 	case SX_LOCK_EXCLUSIVE_WAITERS:
11214e7f640dSJohn Baldwin 		db_printf("exclusive\n");
11224e7f640dSJohn Baldwin 		break;
11234e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
11244e7f640dSJohn Baldwin 		db_printf("exclusive and shared\n");
11254e7f640dSJohn Baldwin 		break;
11264e7f640dSJohn Baldwin 	default:
11274e7f640dSJohn Baldwin 		db_printf("none\n");
11284e7f640dSJohn Baldwin 	}
1129d272fe53SJohn Baldwin }
1130462a7addSJohn Baldwin 
1131462a7addSJohn Baldwin /*
1132462a7addSJohn Baldwin  * Check to see if a thread that is blocked on a sleep queue is actually
1133462a7addSJohn Baldwin  * blocked on an sx lock.  If so, output some details and return true.
1134462a7addSJohn Baldwin  * If the lock has an exclusive owner, return that in *ownerp.
1135462a7addSJohn Baldwin  */
1136462a7addSJohn Baldwin int
1137462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp)
1138462a7addSJohn Baldwin {
1139462a7addSJohn Baldwin 	struct sx *sx;
1140462a7addSJohn Baldwin 
1141462a7addSJohn Baldwin 	/*
11424e7f640dSJohn Baldwin 	 * Check to see if this thread is blocked on an sx lock.
11434e7f640dSJohn Baldwin 	 * First, we check the lock class.  If that is ok, then we
11444e7f640dSJohn Baldwin 	 * compare the lock name against the wait message.
1145462a7addSJohn Baldwin 	 */
11464e7f640dSJohn Baldwin 	sx = td->td_wchan;
11474e7f640dSJohn Baldwin 	if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
11484e7f640dSJohn Baldwin 	    sx->lock_object.lo_name != td->td_wmesg)
1149462a7addSJohn Baldwin 		return (0);
1150462a7addSJohn Baldwin 
1151462a7addSJohn Baldwin 	/* We think we have an sx lock, so output some details. */
1152462a7addSJohn Baldwin 	db_printf("blocked on sx \"%s\" ", td->td_wmesg);
11534e7f640dSJohn Baldwin 	*ownerp = sx_xholder(sx);
11544e7f640dSJohn Baldwin 	if (sx->sx_lock & SX_LOCK_SHARED)
11554e7f640dSJohn Baldwin 		db_printf("SLOCK (count %ju)\n",
11564e7f640dSJohn Baldwin 		    (uintmax_t)SX_SHARERS(sx->sx_lock));
11574e7f640dSJohn Baldwin 	else
1158462a7addSJohn Baldwin 		db_printf("XLOCK\n");
1159462a7addSJohn Baldwin 	return (1);
1160462a7addSJohn Baldwin }
1161d272fe53SJohn Baldwin #endif
1162