xref: /freebsd/sys/kern/kern_sx.c (revision f0830182234c4e185eab23ca0c76f5248581362f)
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>
48e31d0833SAttilio Rao #include <sys/linker_set.h>
4919284646SJohn Baldwin #include <sys/lock.h>
506281b30aSJason Evans #include <sys/mutex.h>
51d272fe53SJohn Baldwin #include <sys/proc.h>
524e7f640dSJohn Baldwin #include <sys/sleepqueue.h>
536281b30aSJason Evans #include <sys/sx.h>
54e31d0833SAttilio Rao #include <sys/sysctl.h>
554e7f640dSJohn Baldwin #include <sys/systm.h>
564e7f640dSJohn Baldwin 
57e31d0833SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
584e7f640dSJohn Baldwin #include <machine/cpu.h>
594e7f640dSJohn Baldwin #endif
606281b30aSJason Evans 
61462a7addSJohn Baldwin #ifdef DDB
62d272fe53SJohn Baldwin #include <ddb/ddb.h>
634e7f640dSJohn Baldwin #endif
64d272fe53SJohn Baldwin 
651ae1c2a3SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
661ae1c2a3SAttilio Rao #define	ADAPTIVE_SX
674e7f640dSJohn Baldwin #endif
684e7f640dSJohn Baldwin 
69f0830182SAttilio Rao CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE);
70c1a6d9faSAttilio Rao 
714e7f640dSJohn Baldwin /* Handy macros for sleep queues. */
724e7f640dSJohn Baldwin #define	SQ_EXCLUSIVE_QUEUE	0
734e7f640dSJohn Baldwin #define	SQ_SHARED_QUEUE		1
744e7f640dSJohn Baldwin 
754e7f640dSJohn Baldwin /*
764e7f640dSJohn Baldwin  * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file.  We
774e7f640dSJohn Baldwin  * drop Giant anytime we have to sleep or if we adaptively spin.
784e7f640dSJohn Baldwin  */
794e7f640dSJohn Baldwin #define	GIANT_DECLARE							\
804e7f640dSJohn Baldwin 	int _giantcnt = 0;						\
814e7f640dSJohn Baldwin 	WITNESS_SAVE_DECL(Giant)					\
824e7f640dSJohn Baldwin 
834e7f640dSJohn Baldwin #define	GIANT_SAVE() do {						\
844e7f640dSJohn Baldwin 	if (mtx_owned(&Giant)) {					\
854e7f640dSJohn Baldwin 		WITNESS_SAVE(&Giant.lock_object, Giant);		\
864e7f640dSJohn Baldwin 		while (mtx_owned(&Giant)) {				\
874e7f640dSJohn Baldwin 			_giantcnt++;					\
884e7f640dSJohn Baldwin 			mtx_unlock(&Giant);				\
894e7f640dSJohn Baldwin 		}							\
904e7f640dSJohn Baldwin 	}								\
914e7f640dSJohn Baldwin } while (0)
924e7f640dSJohn Baldwin 
934e7f640dSJohn Baldwin #define GIANT_RESTORE() do {						\
944e7f640dSJohn Baldwin 	if (_giantcnt > 0) {						\
954e7f640dSJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);			\
964e7f640dSJohn Baldwin 		while (_giantcnt--)					\
974e7f640dSJohn Baldwin 			mtx_lock(&Giant);				\
984e7f640dSJohn Baldwin 		WITNESS_RESTORE(&Giant.lock_object, Giant);		\
994e7f640dSJohn Baldwin 	}								\
1004e7f640dSJohn Baldwin } while (0)
1014e7f640dSJohn Baldwin 
1024e7f640dSJohn Baldwin /*
103da7d0d1eSJohn Baldwin  * Returns true if an exclusive lock is recursed.  It assumes
104da7d0d1eSJohn Baldwin  * curthread currently has an exclusive lock.
1054e7f640dSJohn Baldwin  */
10690356491SAttilio Rao #define	sx_recurse		lock_object.lo_data
1074e7f640dSJohn Baldwin #define	sx_recursed(sx)		((sx)->sx_recurse != 0)
1084e7f640dSJohn Baldwin 
109f9721b43SAttilio Rao static void	assert_sx(struct lock_object *lock, int what);
1104e7f640dSJohn Baldwin #ifdef DDB
111d272fe53SJohn Baldwin static void	db_show_sx(struct lock_object *lock);
112d272fe53SJohn Baldwin #endif
1136e21afd4SJohn Baldwin static void	lock_sx(struct lock_object *lock, int how);
114a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
115a5aedd68SStacey Son static int	owner_sx(struct lock_object *lock, struct thread **owner);
116a5aedd68SStacey Son #endif
1176e21afd4SJohn Baldwin static int	unlock_sx(struct lock_object *lock);
118d272fe53SJohn Baldwin 
11919284646SJohn Baldwin struct lock_class lock_class_sx = {
120ae8dde30SJohn Baldwin 	.lc_name = "sx",
121ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
122f9721b43SAttilio Rao 	.lc_assert = assert_sx,
123d272fe53SJohn Baldwin #ifdef DDB
124ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_sx,
125d272fe53SJohn Baldwin #endif
1266e21afd4SJohn Baldwin 	.lc_lock = lock_sx,
1276e21afd4SJohn Baldwin 	.lc_unlock = unlock_sx,
128a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
129a5aedd68SStacey Son 	.lc_owner = owner_sx,
130a5aedd68SStacey Son #endif
13119284646SJohn Baldwin };
13219284646SJohn Baldwin 
133781a35dfSJohn Baldwin #ifndef INVARIANTS
134781a35dfSJohn Baldwin #define	_sx_assert(sx, what, file, line)
135781a35dfSJohn Baldwin #endif
136781a35dfSJohn Baldwin 
1371ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX
1381ae1c2a3SAttilio Rao static u_int asx_retries = 10;
1391ae1c2a3SAttilio Rao static u_int asx_loops = 10000;
1401ae1c2a3SAttilio Rao SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
1411ae1c2a3SAttilio Rao SYSCTL_INT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
1421ae1c2a3SAttilio Rao SYSCTL_INT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
1431ae1c2a3SAttilio Rao #endif
1441ae1c2a3SAttilio Rao 
1456281b30aSJason Evans void
146f9721b43SAttilio Rao assert_sx(struct lock_object *lock, int what)
147f9721b43SAttilio Rao {
148f9721b43SAttilio Rao 
149f9721b43SAttilio Rao 	sx_assert((struct sx *)lock, what);
150f9721b43SAttilio Rao }
151f9721b43SAttilio Rao 
152f9721b43SAttilio Rao void
1536e21afd4SJohn Baldwin lock_sx(struct lock_object *lock, int how)
1546e21afd4SJohn Baldwin {
1556e21afd4SJohn Baldwin 	struct sx *sx;
1566e21afd4SJohn Baldwin 
1576e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1586e21afd4SJohn Baldwin 	if (how)
1596e21afd4SJohn Baldwin 		sx_xlock(sx);
1606e21afd4SJohn Baldwin 	else
1616e21afd4SJohn Baldwin 		sx_slock(sx);
1626e21afd4SJohn Baldwin }
1636e21afd4SJohn Baldwin 
1646e21afd4SJohn Baldwin int
1656e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock)
1666e21afd4SJohn Baldwin {
1676e21afd4SJohn Baldwin 	struct sx *sx;
1686e21afd4SJohn Baldwin 
1696e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1707ec137e5SJohn Baldwin 	sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
1716e21afd4SJohn Baldwin 	if (sx_xlocked(sx)) {
1726e21afd4SJohn Baldwin 		sx_xunlock(sx);
1736e21afd4SJohn Baldwin 		return (1);
1746e21afd4SJohn Baldwin 	} else {
1756e21afd4SJohn Baldwin 		sx_sunlock(sx);
1766e21afd4SJohn Baldwin 		return (0);
1776e21afd4SJohn Baldwin 	}
1786e21afd4SJohn Baldwin }
1796e21afd4SJohn Baldwin 
180a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
181a5aedd68SStacey Son int
182a5aedd68SStacey Son owner_sx(struct lock_object *lock, struct thread **owner)
183a5aedd68SStacey Son {
184a5aedd68SStacey Son         struct sx *sx = (struct sx *)lock;
185a5aedd68SStacey Son 	uintptr_t x = sx->sx_lock;
186a5aedd68SStacey Son 
187a5aedd68SStacey Son         *owner = (struct thread *)SX_OWNER(x);
188a5aedd68SStacey Son         return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
189a5aedd68SStacey Son 	    (*owner != NULL));
190a5aedd68SStacey Son }
191a5aedd68SStacey Son #endif
192a5aedd68SStacey Son 
1936e21afd4SJohn Baldwin void
194c27b5699SAndrew R. Reiter sx_sysinit(void *arg)
195c27b5699SAndrew R. Reiter {
196c27b5699SAndrew R. Reiter 	struct sx_args *sargs = arg;
197c27b5699SAndrew R. Reiter 
198c27b5699SAndrew R. Reiter 	sx_init(sargs->sa_sx, sargs->sa_desc);
199c27b5699SAndrew R. Reiter }
200c27b5699SAndrew R. Reiter 
201c27b5699SAndrew R. Reiter void
2024e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts)
2036281b30aSJason Evans {
2044e7f640dSJohn Baldwin 	int flags;
2056281b30aSJason Evans 
206b0d67325SJohn Baldwin 	MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
2071ae1c2a3SAttilio Rao 	    SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
208b0d67325SJohn Baldwin 
209f0830182SAttilio Rao 	flags = LO_SLEEPABLE | LO_UPGRADABLE;
2104e7f640dSJohn Baldwin 	if (opts & SX_DUPOK)
2114e7f640dSJohn Baldwin 		flags |= LO_DUPOK;
2124e7f640dSJohn Baldwin 	if (opts & SX_NOPROFILE)
2134e7f640dSJohn Baldwin 		flags |= LO_NOPROFILE;
2144e7f640dSJohn Baldwin 	if (!(opts & SX_NOWITNESS))
2154e7f640dSJohn Baldwin 		flags |= LO_WITNESS;
216f0830182SAttilio Rao 	if (opts & SX_RECURSE)
217f0830182SAttilio Rao 		flags |= LO_RECURSABLE;
2184e7f640dSJohn Baldwin 	if (opts & SX_QUIET)
2194e7f640dSJohn Baldwin 		flags |= LO_QUIET;
2204e7f640dSJohn Baldwin 
221f0830182SAttilio Rao 	flags |= opts & SX_NOADAPTIVE;
2224e7f640dSJohn Baldwin 	sx->sx_lock = SX_LOCK_UNLOCKED;
2234e7f640dSJohn Baldwin 	sx->sx_recurse = 0;
2244e7f640dSJohn Baldwin 	lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
2256281b30aSJason Evans }
2266281b30aSJason Evans 
2276281b30aSJason Evans void
2286281b30aSJason Evans sx_destroy(struct sx *sx)
2296281b30aSJason Evans {
2306281b30aSJason Evans 
2314e7f640dSJohn Baldwin 	KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
2324e7f640dSJohn Baldwin 	KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
2330026c92cSJohn Baldwin 	sx->sx_lock = SX_LOCK_DESTROYED;
234aa89d8cdSJohn Baldwin 	lock_destroy(&sx->lock_object);
2356281b30aSJason Evans }
2366281b30aSJason Evans 
237f9819486SAttilio Rao int
238f9819486SAttilio Rao _sx_slock(struct sx *sx, int opts, const char *file, int line)
2396281b30aSJason Evans {
240f9819486SAttilio Rao 	int error = 0;
2416281b30aSJason Evans 
2424e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
2430026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
2440026c92cSJohn Baldwin 	    ("sx_slock() of destroyed sx @ %s:%d", file, line));
24541313430SJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
246f9819486SAttilio Rao 	error = __sx_slock(sx, opts, file, line);
247f9819486SAttilio Rao 	if (!error) {
248aa89d8cdSJohn Baldwin 		LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
249aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, 0, file, line);
250764e4d54SJohn Baldwin 		curthread->td_locks++;
2516281b30aSJason Evans 	}
2526281b30aSJason Evans 
253f9819486SAttilio Rao 	return (error);
254f9819486SAttilio Rao }
255f9819486SAttilio Rao 
2565f36700aSJohn Baldwin int
2575f36700aSJohn Baldwin _sx_try_slock(struct sx *sx, const char *file, int line)
2585f36700aSJohn Baldwin {
2594e7f640dSJohn Baldwin 	uintptr_t x;
2605f36700aSJohn Baldwin 
261764a938bSPawel Jakub Dawidek 	for (;;) {
2624e7f640dSJohn Baldwin 		x = sx->sx_lock;
2630026c92cSJohn Baldwin 		KASSERT(x != SX_LOCK_DESTROYED,
2640026c92cSJohn Baldwin 		    ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
265764a938bSPawel Jakub Dawidek 		if (!(x & SX_LOCK_SHARED))
266764a938bSPawel Jakub Dawidek 			break;
267764a938bSPawel Jakub Dawidek 		if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
268aa89d8cdSJohn Baldwin 			LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
269aa89d8cdSJohn Baldwin 			WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
270764e4d54SJohn Baldwin 			curthread->td_locks++;
2715f36700aSJohn Baldwin 			return (1);
2725f36700aSJohn Baldwin 		}
273764a938bSPawel Jakub Dawidek 	}
2744e7f640dSJohn Baldwin 
2754e7f640dSJohn Baldwin 	LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
2764e7f640dSJohn Baldwin 	return (0);
2775f36700aSJohn Baldwin }
2785f36700aSJohn Baldwin 
279f9819486SAttilio Rao int
280f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line)
2816281b30aSJason Evans {
282f9819486SAttilio Rao 	int error = 0;
2836281b30aSJason Evans 
2844e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
2850026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
2860026c92cSJohn Baldwin 	    ("sx_xlock() of destroyed sx @ %s:%d", file, line));
287aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
28841313430SJohn Baldwin 	    line, NULL);
289f9819486SAttilio Rao 	error = __sx_xlock(sx, curthread, opts, file, line);
290f9819486SAttilio Rao 	if (!error) {
291f9819486SAttilio Rao 		LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
292f9819486SAttilio Rao 		    file, line);
293aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
294764e4d54SJohn Baldwin 		curthread->td_locks++;
2956281b30aSJason Evans 	}
2966281b30aSJason Evans 
297f9819486SAttilio Rao 	return (error);
298f9819486SAttilio Rao }
299f9819486SAttilio Rao 
3005f36700aSJohn Baldwin int
3015f36700aSJohn Baldwin _sx_try_xlock(struct sx *sx, const char *file, int line)
3025f36700aSJohn Baldwin {
3034e7f640dSJohn Baldwin 	int rval;
3045f36700aSJohn Baldwin 
3054e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3060026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3070026c92cSJohn Baldwin 	    ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
3084e7f640dSJohn Baldwin 
309f0830182SAttilio Rao 	if (sx_xlocked(sx) &&
310f0830182SAttilio Rao 	    (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
3114e7f640dSJohn Baldwin 		sx->sx_recurse++;
3124e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
3134e7f640dSJohn Baldwin 		rval = 1;
3144e7f640dSJohn Baldwin 	} else
3154e7f640dSJohn Baldwin 		rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
3164e7f640dSJohn Baldwin 		    (uintptr_t)curthread);
3174e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
3184e7f640dSJohn Baldwin 	if (rval) {
3194e7f640dSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
3204e7f640dSJohn Baldwin 		    file, line);
321764e4d54SJohn Baldwin 		curthread->td_locks++;
3225f36700aSJohn Baldwin 	}
3234e7f640dSJohn Baldwin 
3244e7f640dSJohn Baldwin 	return (rval);
3255f36700aSJohn Baldwin }
3265f36700aSJohn Baldwin 
3276281b30aSJason Evans void
32819284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line)
3296281b30aSJason Evans {
3306281b30aSJason Evans 
3314e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3320026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3330026c92cSJohn Baldwin 	    ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
3347ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
335764e4d54SJohn Baldwin 	curthread->td_locks--;
336aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
337aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
3384e7f640dSJohn Baldwin 	__sx_sunlock(sx, file, line);
339a5aedd68SStacey Son 	LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
3406281b30aSJason Evans }
3416281b30aSJason Evans 
3426281b30aSJason Evans void
34319284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line)
3446281b30aSJason Evans {
3456281b30aSJason Evans 
3464e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3470026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3480026c92cSJohn Baldwin 	    ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
3497ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED, file, line);
350764e4d54SJohn Baldwin 	curthread->td_locks--;
351aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
3524e7f640dSJohn Baldwin 	LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
3534e7f640dSJohn Baldwin 	    line);
354afc0bfbdSKip Macy 	if (!sx_recursed(sx))
355a5aedd68SStacey Son 		LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
3564e7f640dSJohn Baldwin 	__sx_xunlock(sx, curthread, file, line);
3576281b30aSJason Evans }
358d55229b7SJason Evans 
3594e7f640dSJohn Baldwin /*
3604e7f640dSJohn Baldwin  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
3614e7f640dSJohn Baldwin  * This will only succeed if this thread holds a single shared lock.
3624e7f640dSJohn Baldwin  * Return 1 if if the upgrade succeed, 0 otherwise.
3634e7f640dSJohn Baldwin  */
364d55229b7SJason Evans int
365d55229b7SJason Evans _sx_try_upgrade(struct sx *sx, const char *file, int line)
366d55229b7SJason Evans {
3674e7f640dSJohn Baldwin 	uintptr_t x;
3684e7f640dSJohn Baldwin 	int success;
369d55229b7SJason Evans 
3700026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3710026c92cSJohn Baldwin 	    ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
3727ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
373d55229b7SJason Evans 
3744e7f640dSJohn Baldwin 	/*
3754e7f640dSJohn Baldwin 	 * Try to switch from one shared lock to an exclusive lock.  We need
3764e7f640dSJohn Baldwin 	 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
3774e7f640dSJohn Baldwin 	 * we will wake up the exclusive waiters when we drop the lock.
3784e7f640dSJohn Baldwin 	 */
3794e7f640dSJohn Baldwin 	x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
3804e7f640dSJohn Baldwin 	success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
3814e7f640dSJohn Baldwin 	    (uintptr_t)curthread | x);
3824e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
383a5aedd68SStacey Son 	if (success) {
384aa89d8cdSJohn Baldwin 		WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
385b0b7cb50SJohn Baldwin 		    file, line);
386a5aedd68SStacey Son 		LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
387a5aedd68SStacey Son 	}
3884e7f640dSJohn Baldwin 	return (success);
389d55229b7SJason Evans }
390d55229b7SJason Evans 
3914e7f640dSJohn Baldwin /*
3924e7f640dSJohn Baldwin  * Downgrade an unrecursed exclusive lock into a single shared lock.
3934e7f640dSJohn Baldwin  */
394d55229b7SJason Evans void
395d55229b7SJason Evans _sx_downgrade(struct sx *sx, const char *file, int line)
396d55229b7SJason Evans {
3974e7f640dSJohn Baldwin 	uintptr_t x;
398da7bbd2cSJohn Baldwin 	int wakeup_swapper;
399d55229b7SJason Evans 
4000026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4010026c92cSJohn Baldwin 	    ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
4027ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
4034e7f640dSJohn Baldwin #ifndef INVARIANTS
4044e7f640dSJohn Baldwin 	if (sx_recursed(sx))
4054e7f640dSJohn Baldwin 		panic("downgrade of a recursed lock");
4064e7f640dSJohn Baldwin #endif
407d55229b7SJason Evans 
408aa89d8cdSJohn Baldwin 	WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
409d55229b7SJason Evans 
4104e7f640dSJohn Baldwin 	/*
4114e7f640dSJohn Baldwin 	 * Try to switch from an exclusive lock with no shared waiters
4124e7f640dSJohn Baldwin 	 * to one sharer with no shared waiters.  If there are
4134e7f640dSJohn Baldwin 	 * exclusive waiters, we don't need to lock the sleep queue so
4144e7f640dSJohn Baldwin 	 * long as we preserve the flag.  We do one quick try and if
4154e7f640dSJohn Baldwin 	 * that fails we grab the sleepq lock to keep the flags from
4164e7f640dSJohn Baldwin 	 * changing and do it the slow way.
4174e7f640dSJohn Baldwin 	 *
4184e7f640dSJohn Baldwin 	 * We have to lock the sleep queue if there are shared waiters
4194e7f640dSJohn Baldwin 	 * so we can wake them up.
4204e7f640dSJohn Baldwin 	 */
4214e7f640dSJohn Baldwin 	x = sx->sx_lock;
4224e7f640dSJohn Baldwin 	if (!(x & SX_LOCK_SHARED_WAITERS) &&
4234e7f640dSJohn Baldwin 	    atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
4244e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
4254e7f640dSJohn Baldwin 		LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
4264e7f640dSJohn Baldwin 		return;
4274e7f640dSJohn Baldwin 	}
4284e7f640dSJohn Baldwin 
4294e7f640dSJohn Baldwin 	/*
4304e7f640dSJohn Baldwin 	 * Lock the sleep queue so we can read the waiters bits
4314e7f640dSJohn Baldwin 	 * without any races and wakeup any shared waiters.
4324e7f640dSJohn Baldwin 	 */
4334e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
4344e7f640dSJohn Baldwin 
4354e7f640dSJohn Baldwin 	/*
4364e7f640dSJohn Baldwin 	 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
4374e7f640dSJohn Baldwin 	 * shared lock.  If there are any shared waiters, wake them up.
4384e7f640dSJohn Baldwin 	 */
439da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
4404e7f640dSJohn Baldwin 	x = sx->sx_lock;
4414e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
4424e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS));
4434e7f640dSJohn Baldwin 	if (x & SX_LOCK_SHARED_WAITERS)
444da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
445da7bbd2cSJohn Baldwin 		    0, SQ_SHARED_QUEUE);
4464e7f640dSJohn Baldwin 	sleepq_release(&sx->lock_object);
447d55229b7SJason Evans 
448aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
449a5aedd68SStacey Son 	LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
450da7bbd2cSJohn Baldwin 
451da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
452da7bbd2cSJohn Baldwin 		kick_proc0();
4534e7f640dSJohn Baldwin }
454d55229b7SJason Evans 
4554e7f640dSJohn Baldwin /*
4564e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xlock
4574e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
4584e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
4594e7f640dSJohn Baldwin  * accessible from at least sx.h.
4604e7f640dSJohn Baldwin  */
461f9819486SAttilio Rao int
462f9819486SAttilio Rao _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
463f9819486SAttilio Rao     int line)
4644e7f640dSJohn Baldwin {
4654e7f640dSJohn Baldwin 	GIANT_DECLARE;
4664e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
4674e7f640dSJohn Baldwin 	volatile struct thread *owner;
4681ae1c2a3SAttilio Rao 	u_int i, spintries = 0;
4694e7f640dSJohn Baldwin #endif
4704e7f640dSJohn Baldwin 	uintptr_t x;
4711723a064SJeff Roberson #ifdef LOCK_PROFILING
4721723a064SJeff Roberson 	uint64_t waittime = 0;
4731723a064SJeff Roberson 	int contested = 0;
4741723a064SJeff Roberson #endif
4751723a064SJeff Roberson 	int error = 0;
476a5aedd68SStacey Son #ifdef	KDTRACE_HOOKS
477a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
478a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
479a5aedd68SStacey Son 	int64_t sleep_time = 0;
480a5aedd68SStacey Son #endif
4814e7f640dSJohn Baldwin 
4824e7f640dSJohn Baldwin 	/* If we already hold an exclusive lock, then recurse. */
4834e7f640dSJohn Baldwin 	if (sx_xlocked(sx)) {
484f0830182SAttilio Rao 		KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
485b0d67325SJohn Baldwin 	    ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
486b0d67325SJohn Baldwin 		    sx->lock_object.lo_name, file, line));
4874e7f640dSJohn Baldwin 		sx->sx_recurse++;
4884e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
4894e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
4904e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
491f9819486SAttilio Rao 		return (0);
4924e7f640dSJohn Baldwin 	}
4934e7f640dSJohn Baldwin 
4944e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
4954e7f640dSJohn Baldwin 		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
4964e7f640dSJohn Baldwin 		    sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
4974e7f640dSJohn Baldwin 
4984e7f640dSJohn Baldwin 	while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
499a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
500a5aedd68SStacey Son 		spin_cnt++;
501a5aedd68SStacey Son #endif
502eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
503eea4f254SJeff Roberson 		    &waittime);
5044e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5054e7f640dSJohn Baldwin 		/*
5064e7f640dSJohn Baldwin 		 * If the lock is write locked and the owner is
5074e7f640dSJohn Baldwin 		 * running on another CPU, spin until the owner stops
5084e7f640dSJohn Baldwin 		 * running or the state of the lock changes.
5094e7f640dSJohn Baldwin 		 */
5104e7f640dSJohn Baldwin 		x = sx->sx_lock;
5111ae1c2a3SAttilio Rao 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) != 0) {
5121ae1c2a3SAttilio Rao 			if ((x & SX_LOCK_SHARED) == 0) {
5134e7f640dSJohn Baldwin 				x = SX_OWNER(x);
5144e7f640dSJohn Baldwin 				owner = (struct thread *)x;
5154e7f640dSJohn Baldwin 				if (TD_IS_RUNNING(owner)) {
5164e7f640dSJohn Baldwin 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5174e7f640dSJohn Baldwin 						CTR3(KTR_LOCK,
5184e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
5194e7f640dSJohn Baldwin 						    __func__, sx, owner);
5204e7f640dSJohn Baldwin 					GIANT_SAVE();
5214e7f640dSJohn Baldwin 					while (SX_OWNER(sx->sx_lock) == x &&
522a5aedd68SStacey Son 					    TD_IS_RUNNING(owner)) {
5234e7f640dSJohn Baldwin 						cpu_spinwait();
524a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
525a5aedd68SStacey Son 						spin_cnt++;
526a5aedd68SStacey Son #endif
527a5aedd68SStacey Son 					}
5284e7f640dSJohn Baldwin 					continue;
5294e7f640dSJohn Baldwin 				}
5301ae1c2a3SAttilio Rao 			} else if (SX_SHARERS(x) && spintries < asx_retries) {
5311ae1c2a3SAttilio Rao 				spintries++;
5321ae1c2a3SAttilio Rao 				for (i = 0; i < asx_loops; i++) {
5331ae1c2a3SAttilio Rao 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5341ae1c2a3SAttilio Rao 						CTR4(KTR_LOCK,
5351ae1c2a3SAttilio Rao 				    "%s: shared spinning on %p with %u and %u",
5361ae1c2a3SAttilio Rao 						    __func__, sx, spintries, i);
5371ae1c2a3SAttilio Rao 					GIANT_SAVE();
5381ae1c2a3SAttilio Rao 					x = sx->sx_lock;
5391ae1c2a3SAttilio Rao 					if ((x & SX_LOCK_SHARED) == 0 ||
5401ae1c2a3SAttilio Rao 					    SX_SHARERS(x) == 0)
5411ae1c2a3SAttilio Rao 						break;
5421ae1c2a3SAttilio Rao 					cpu_spinwait();
5431ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS
5441ae1c2a3SAttilio Rao 					spin_cnt++;
5451ae1c2a3SAttilio Rao #endif
5461ae1c2a3SAttilio Rao 				}
5471ae1c2a3SAttilio Rao 				if (i != asx_loops)
5481ae1c2a3SAttilio Rao 					continue;
5491ae1c2a3SAttilio Rao 			}
5504e7f640dSJohn Baldwin 		}
5514e7f640dSJohn Baldwin #endif
5524e7f640dSJohn Baldwin 
5534e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
5544e7f640dSJohn Baldwin 		x = sx->sx_lock;
5554e7f640dSJohn Baldwin 
5564e7f640dSJohn Baldwin 		/*
5574e7f640dSJohn Baldwin 		 * If the lock was released while spinning on the
5584e7f640dSJohn Baldwin 		 * sleep queue chain lock, try again.
5594e7f640dSJohn Baldwin 		 */
5604e7f640dSJohn Baldwin 		if (x == SX_LOCK_UNLOCKED) {
5614e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
5624e7f640dSJohn Baldwin 			continue;
5634e7f640dSJohn Baldwin 		}
5644e7f640dSJohn Baldwin 
5654e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5664e7f640dSJohn Baldwin 		/*
5674e7f640dSJohn Baldwin 		 * The current lock owner might have started executing
5684e7f640dSJohn Baldwin 		 * on another CPU (or the lock could have changed
5694e7f640dSJohn Baldwin 		 * owners) while we were waiting on the sleep queue
5704e7f640dSJohn Baldwin 		 * chain lock.  If so, drop the sleep queue lock and try
5714e7f640dSJohn Baldwin 		 * again.
5724e7f640dSJohn Baldwin 		 */
5734e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
5741ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
5754e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
5764e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
5774e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
5784e7f640dSJohn Baldwin 				continue;
5794e7f640dSJohn Baldwin 			}
5804e7f640dSJohn Baldwin 		}
5814e7f640dSJohn Baldwin #endif
5824e7f640dSJohn Baldwin 
5834e7f640dSJohn Baldwin 		/*
5844e7f640dSJohn Baldwin 		 * If an exclusive lock was released with both shared
5854e7f640dSJohn Baldwin 		 * and exclusive waiters and a shared waiter hasn't
5864e7f640dSJohn Baldwin 		 * woken up and acquired the lock yet, sx_lock will be
5874e7f640dSJohn Baldwin 		 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
5884e7f640dSJohn Baldwin 		 * If we see that value, try to acquire it once.  Note
5894e7f640dSJohn Baldwin 		 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
5904e7f640dSJohn Baldwin 		 * as there are other exclusive waiters still.  If we
5914e7f640dSJohn Baldwin 		 * fail, restart the loop.
5924e7f640dSJohn Baldwin 		 */
5934e7f640dSJohn Baldwin 		if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
5944e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock,
5954e7f640dSJohn Baldwin 			    SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
5964e7f640dSJohn Baldwin 			    tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
5974e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
5984e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
5994e7f640dSJohn Baldwin 				    __func__, sx);
6004e7f640dSJohn Baldwin 				break;
6014e7f640dSJohn Baldwin 			}
6024e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6034e7f640dSJohn Baldwin 			continue;
6044e7f640dSJohn Baldwin 		}
6054e7f640dSJohn Baldwin 
6064e7f640dSJohn Baldwin 		/*
6074e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
6084e7f640dSJohn Baldwin 		 * than loop back and retry.
6094e7f640dSJohn Baldwin 		 */
6104e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
6114e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
6124e7f640dSJohn Baldwin 			    x | SX_LOCK_EXCLUSIVE_WAITERS)) {
6134e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6144e7f640dSJohn Baldwin 				continue;
6154e7f640dSJohn Baldwin 			}
6164e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
6174e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
6184e7f640dSJohn Baldwin 				    __func__, sx);
6194e7f640dSJohn Baldwin 		}
6204e7f640dSJohn Baldwin 
6214e7f640dSJohn Baldwin 		/*
6224e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the exclusive
6234e7f640dSJohn Baldwin 		 * lock and the exclusive waiters flag is set, we have
6244e7f640dSJohn Baldwin 		 * to sleep.
6254e7f640dSJohn Baldwin 		 */
6264e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6274e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
6284e7f640dSJohn Baldwin 			    __func__, sx);
6294e7f640dSJohn Baldwin 
630a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
631a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
632a5aedd68SStacey Son #endif
6334e7f640dSJohn Baldwin 		GIANT_SAVE();
6344e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
635f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
636f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
637f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
638c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
639f9819486SAttilio Rao 		else
640c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
641a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
642a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
643a5aedd68SStacey Son 		sleep_cnt++;
644a5aedd68SStacey Son #endif
645f9819486SAttilio Rao 		if (error) {
646f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
647f9819486SAttilio Rao 				CTR2(KTR_LOCK,
648f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
649f9819486SAttilio Rao 				    __func__, sx);
650f9819486SAttilio Rao 			break;
651f9819486SAttilio Rao 		}
6524e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6534e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
6544e7f640dSJohn Baldwin 			    __func__, sx);
6554e7f640dSJohn Baldwin 	}
6564e7f640dSJohn Baldwin 
6574e7f640dSJohn Baldwin 	GIANT_RESTORE();
658f9819486SAttilio Rao 	if (!error)
659a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
660a5aedd68SStacey Son 		    contested, waittime, file, line);
661a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
662a5aedd68SStacey Son 	if (sleep_time)
663a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
664a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
665a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
666a5aedd68SStacey Son #endif
667f9819486SAttilio Rao 	return (error);
6684e7f640dSJohn Baldwin }
6694e7f640dSJohn Baldwin 
6704e7f640dSJohn Baldwin /*
6714e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xunlock
6724e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
6734e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
6744e7f640dSJohn Baldwin  * accessible from at least sx.h.
6754e7f640dSJohn Baldwin  */
6764e7f640dSJohn Baldwin void
6774e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
6784e7f640dSJohn Baldwin {
6794e7f640dSJohn Baldwin 	uintptr_t x;
680da7bbd2cSJohn Baldwin 	int queue, wakeup_swapper;
6814e7f640dSJohn Baldwin 
6824e7f640dSJohn Baldwin 	MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
6834e7f640dSJohn Baldwin 
6844e7f640dSJohn Baldwin 	/* If the lock is recursed, then unrecurse one level. */
6854e7f640dSJohn Baldwin 	if (sx_xlocked(sx) && sx_recursed(sx)) {
6864e7f640dSJohn Baldwin 		if ((--sx->sx_recurse) == 0)
6874e7f640dSJohn Baldwin 			atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
6884e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6894e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
6904e7f640dSJohn Baldwin 		return;
6914e7f640dSJohn Baldwin 	}
6924e7f640dSJohn Baldwin 	MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
6934e7f640dSJohn Baldwin 	    SX_LOCK_EXCLUSIVE_WAITERS));
6944e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
6954e7f640dSJohn Baldwin 		CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
6964e7f640dSJohn Baldwin 
6974e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
6984e7f640dSJohn Baldwin 	x = SX_LOCK_UNLOCKED;
6994e7f640dSJohn Baldwin 
7004e7f640dSJohn Baldwin 	/*
7014e7f640dSJohn Baldwin 	 * The wake up algorithm here is quite simple and probably not
7024e7f640dSJohn Baldwin 	 * ideal.  It gives precedence to shared waiters if they are
7034e7f640dSJohn Baldwin 	 * present.  For this condition, we have to preserve the
7044e7f640dSJohn Baldwin 	 * state of the exclusive waiters flag.
7054e7f640dSJohn Baldwin 	 */
7064e7f640dSJohn Baldwin 	if (sx->sx_lock & SX_LOCK_SHARED_WAITERS) {
7074e7f640dSJohn Baldwin 		queue = SQ_SHARED_QUEUE;
7084e7f640dSJohn Baldwin 		x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
7094e7f640dSJohn Baldwin 	} else
7104e7f640dSJohn Baldwin 		queue = SQ_EXCLUSIVE_QUEUE;
7114e7f640dSJohn Baldwin 
7124e7f640dSJohn Baldwin 	/* Wake up all the waiters for the specific queue. */
7134e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7144e7f640dSJohn Baldwin 		CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
7154e7f640dSJohn Baldwin 		    __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
7164e7f640dSJohn Baldwin 		    "exclusive");
7174e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, x);
718da7bbd2cSJohn Baldwin 	wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
719da7bbd2cSJohn Baldwin 	    queue);
720c5aa6b58SJeff Roberson 	sleepq_release(&sx->lock_object);
721da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
722da7bbd2cSJohn Baldwin 		kick_proc0();
7234e7f640dSJohn Baldwin }
7244e7f640dSJohn Baldwin 
7254e7f640dSJohn Baldwin /*
7264e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_slock
7274e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7284e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7294e7f640dSJohn Baldwin  * accessible from at least sx.h.
7304e7f640dSJohn Baldwin  */
731f9819486SAttilio Rao int
732f9819486SAttilio Rao _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
7334e7f640dSJohn Baldwin {
7344e7f640dSJohn Baldwin 	GIANT_DECLARE;
7354e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
7364e7f640dSJohn Baldwin 	volatile struct thread *owner;
7374e7f640dSJohn Baldwin #endif
7381723a064SJeff Roberson #ifdef LOCK_PROFILING
739c1a6d9faSAttilio Rao 	uint64_t waittime = 0;
740c1a6d9faSAttilio Rao 	int contested = 0;
7411723a064SJeff Roberson #endif
7424e7f640dSJohn Baldwin 	uintptr_t x;
743c1a6d9faSAttilio Rao 	int error = 0;
744a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
745a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
746a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
747a5aedd68SStacey Son 	int64_t sleep_time = 0;
748a5aedd68SStacey Son #endif
749c1a6d9faSAttilio Rao 
7504e7f640dSJohn Baldwin 	/*
7514e7f640dSJohn Baldwin 	 * As with rwlocks, we don't make any attempt to try to block
7524e7f640dSJohn Baldwin 	 * shared locks once there is an exclusive waiter.
7534e7f640dSJohn Baldwin 	 */
7544e7f640dSJohn Baldwin 	for (;;) {
755a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
756a5aedd68SStacey Son 		spin_cnt++;
757a5aedd68SStacey Son #endif
7584e7f640dSJohn Baldwin 		x = sx->sx_lock;
7594e7f640dSJohn Baldwin 
7604e7f640dSJohn Baldwin 		/*
7614e7f640dSJohn Baldwin 		 * If no other thread has an exclusive lock then try to bump up
7624e7f640dSJohn Baldwin 		 * the count of sharers.  Since we have to preserve the state
7634e7f640dSJohn Baldwin 		 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
7644e7f640dSJohn Baldwin 		 * shared lock loop back and retry.
7654e7f640dSJohn Baldwin 		 */
7664e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
7674e7f640dSJohn Baldwin 			MPASS(!(x & SX_LOCK_SHARED_WAITERS));
7684e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
7694e7f640dSJohn Baldwin 			    x + SX_ONE_SHARER)) {
7704e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
7714e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
7724e7f640dSJohn Baldwin 					    "%s: %p succeed %p -> %p", __func__,
7734e7f640dSJohn Baldwin 					    sx, (void *)x,
7744e7f640dSJohn Baldwin 					    (void *)(x + SX_ONE_SHARER));
7754e7f640dSJohn Baldwin 				break;
7764e7f640dSJohn Baldwin 			}
7774e7f640dSJohn Baldwin 			continue;
7784e7f640dSJohn Baldwin 		}
779eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
780eea4f254SJeff Roberson 		    &waittime);
7814e7f640dSJohn Baldwin 
7824e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
7834e7f640dSJohn Baldwin 		/*
7844e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
7854e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
7864e7f640dSJohn Baldwin 		 * changes.
7874e7f640dSJohn Baldwin 		 */
7881ae1c2a3SAttilio Rao 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
7894e7f640dSJohn Baldwin 			x = SX_OWNER(x);
7904e7f640dSJohn Baldwin 			owner = (struct thread *)x;
7914e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
7924e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
7934e7f640dSJohn Baldwin 					CTR3(KTR_LOCK,
7944e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
7954e7f640dSJohn Baldwin 					    __func__, sx, owner);
7964e7f640dSJohn Baldwin 				GIANT_SAVE();
7974e7f640dSJohn Baldwin 				while (SX_OWNER(sx->sx_lock) == x &&
798a5aedd68SStacey Son 				    TD_IS_RUNNING(owner)) {
799a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
800a5aedd68SStacey Son 					spin_cnt++;
801a5aedd68SStacey Son #endif
8024e7f640dSJohn Baldwin 					cpu_spinwait();
803a5aedd68SStacey Son 				}
8044e7f640dSJohn Baldwin 				continue;
8054e7f640dSJohn Baldwin 			}
8064e7f640dSJohn Baldwin 		}
8074e7f640dSJohn Baldwin #endif
8084e7f640dSJohn Baldwin 
8094e7f640dSJohn Baldwin 		/*
8104e7f640dSJohn Baldwin 		 * Some other thread already has an exclusive lock, so
8114e7f640dSJohn Baldwin 		 * start the process of blocking.
8124e7f640dSJohn Baldwin 		 */
8134e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
8144e7f640dSJohn Baldwin 		x = sx->sx_lock;
8154e7f640dSJohn Baldwin 
8164e7f640dSJohn Baldwin 		/*
8174e7f640dSJohn Baldwin 		 * The lock could have been released while we spun.
8184e7f640dSJohn Baldwin 		 * In this case loop back and retry.
8194e7f640dSJohn Baldwin 		 */
8204e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8214e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
8224e7f640dSJohn Baldwin 			continue;
8234e7f640dSJohn Baldwin 		}
8244e7f640dSJohn Baldwin 
8254e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8264e7f640dSJohn Baldwin 		/*
8274e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8284e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8294e7f640dSJohn Baldwin 		 * changes.
8304e7f640dSJohn Baldwin 		 */
8314e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
8321ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8334e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
8344e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8354e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
8364e7f640dSJohn Baldwin 				continue;
8374e7f640dSJohn Baldwin 			}
8384e7f640dSJohn Baldwin 		}
8394e7f640dSJohn Baldwin #endif
8404e7f640dSJohn Baldwin 
8414e7f640dSJohn Baldwin 		/*
8424e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
8434e7f640dSJohn Baldwin 		 * fail to set it drop the sleep queue lock and loop
8444e7f640dSJohn Baldwin 		 * back.
8454e7f640dSJohn Baldwin 		 */
8464e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED_WAITERS)) {
8474e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
8484e7f640dSJohn Baldwin 			    x | SX_LOCK_SHARED_WAITERS)) {
8494e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
8504e7f640dSJohn Baldwin 				continue;
8514e7f640dSJohn Baldwin 			}
8524e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
8534e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
8544e7f640dSJohn Baldwin 				    __func__, sx);
8554e7f640dSJohn Baldwin 		}
8564e7f640dSJohn Baldwin 
8574e7f640dSJohn Baldwin 		/*
8584e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the shared lock,
8594e7f640dSJohn Baldwin 		 * we have to sleep.
8604e7f640dSJohn Baldwin 		 */
8614e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
8624e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
8634e7f640dSJohn Baldwin 			    __func__, sx);
8644e7f640dSJohn Baldwin 
865a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
866a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
867a5aedd68SStacey Son #endif
8684e7f640dSJohn Baldwin 		GIANT_SAVE();
8694e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
870f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
871f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
872f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
873c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
874f9819486SAttilio Rao 		else
875c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
876a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
877a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
878a5aedd68SStacey Son 		sleep_cnt++;
879a5aedd68SStacey Son #endif
880f9819486SAttilio Rao 		if (error) {
881f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
882f9819486SAttilio Rao 				CTR2(KTR_LOCK,
883f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
884f9819486SAttilio Rao 				    __func__, sx);
885f9819486SAttilio Rao 			break;
886f9819486SAttilio Rao 		}
8874e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
8884e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
8894e7f640dSJohn Baldwin 			    __func__, sx);
8904e7f640dSJohn Baldwin 	}
891eea4f254SJeff Roberson 	if (error == 0)
892a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
893a5aedd68SStacey Son 		    contested, waittime, file, line);
894a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
895a5aedd68SStacey Son 	if (sleep_time)
896a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
897a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
898a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
899a5aedd68SStacey Son #endif
9004e7f640dSJohn Baldwin 	GIANT_RESTORE();
901f9819486SAttilio Rao 	return (error);
9024e7f640dSJohn Baldwin }
9034e7f640dSJohn Baldwin 
9044e7f640dSJohn Baldwin /*
9054e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_sunlock
9064e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
9074e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
9084e7f640dSJohn Baldwin  * accessible from at least sx.h.
9094e7f640dSJohn Baldwin  */
9104e7f640dSJohn Baldwin void
9114e7f640dSJohn Baldwin _sx_sunlock_hard(struct sx *sx, const char *file, int line)
9124e7f640dSJohn Baldwin {
9134e7f640dSJohn Baldwin 	uintptr_t x;
914da7bbd2cSJohn Baldwin 	int wakeup_swapper;
9154e7f640dSJohn Baldwin 
9164e7f640dSJohn Baldwin 	for (;;) {
9174e7f640dSJohn Baldwin 		x = sx->sx_lock;
9184e7f640dSJohn Baldwin 
9194e7f640dSJohn Baldwin 		/*
9204e7f640dSJohn Baldwin 		 * We should never have sharers while at least one thread
9214e7f640dSJohn Baldwin 		 * holds a shared lock.
9224e7f640dSJohn Baldwin 		 */
9234e7f640dSJohn Baldwin 		KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
9244e7f640dSJohn Baldwin 		    ("%s: waiting sharers", __func__));
9254e7f640dSJohn Baldwin 
9264e7f640dSJohn Baldwin 		/*
9274e7f640dSJohn Baldwin 		 * See if there is more than one shared lock held.  If
9284e7f640dSJohn Baldwin 		 * so, just drop one and return.
9294e7f640dSJohn Baldwin 		 */
9304e7f640dSJohn Baldwin 		if (SX_SHARERS(x) > 1) {
9314e7f640dSJohn Baldwin 			if (atomic_cmpset_ptr(&sx->sx_lock, x,
9324e7f640dSJohn Baldwin 			    x - SX_ONE_SHARER)) {
9334e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
9344e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
9354e7f640dSJohn Baldwin 					    "%s: %p succeeded %p -> %p",
9364e7f640dSJohn Baldwin 					    __func__, sx, (void *)x,
9374e7f640dSJohn Baldwin 					    (void *)(x - SX_ONE_SHARER));
9384e7f640dSJohn Baldwin 				break;
9394e7f640dSJohn Baldwin 			}
9404e7f640dSJohn Baldwin 			continue;
9414e7f640dSJohn Baldwin 		}
9424e7f640dSJohn Baldwin 
9434e7f640dSJohn Baldwin 		/*
9444e7f640dSJohn Baldwin 		 * If there aren't any waiters for an exclusive lock,
9454e7f640dSJohn Baldwin 		 * then try to drop it quickly.
9464e7f640dSJohn Baldwin 		 */
9474e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
9484e7f640dSJohn Baldwin 			MPASS(x == SX_SHARERS_LOCK(1));
9494e7f640dSJohn Baldwin 			if (atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1),
9504e7f640dSJohn Baldwin 			    SX_LOCK_UNLOCKED)) {
9514e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
9524e7f640dSJohn Baldwin 					CTR2(KTR_LOCK, "%s: %p last succeeded",
9534e7f640dSJohn Baldwin 					    __func__, sx);
9544e7f640dSJohn Baldwin 				break;
9554e7f640dSJohn Baldwin 			}
9564e7f640dSJohn Baldwin 			continue;
9574e7f640dSJohn Baldwin 		}
9584e7f640dSJohn Baldwin 
9594e7f640dSJohn Baldwin 		/*
9604e7f640dSJohn Baldwin 		 * At this point, there should just be one sharer with
9614e7f640dSJohn Baldwin 		 * exclusive waiters.
9624e7f640dSJohn Baldwin 		 */
9634e7f640dSJohn Baldwin 		MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
9644e7f640dSJohn Baldwin 
9654e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
9664e7f640dSJohn Baldwin 
9674e7f640dSJohn Baldwin 		/*
9684e7f640dSJohn Baldwin 		 * Wake up semantic here is quite simple:
9694e7f640dSJohn Baldwin 		 * Just wake up all the exclusive waiters.
9704e7f640dSJohn Baldwin 		 * Note that the state of the lock could have changed,
9714e7f640dSJohn Baldwin 		 * so if it fails loop back and retry.
9724e7f640dSJohn Baldwin 		 */
9734e7f640dSJohn Baldwin 		if (!atomic_cmpset_ptr(&sx->sx_lock,
9744e7f640dSJohn Baldwin 		    SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
9754e7f640dSJohn Baldwin 		    SX_LOCK_UNLOCKED)) {
9764e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
9774e7f640dSJohn Baldwin 			continue;
9784e7f640dSJohn Baldwin 		}
9794e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9804e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p waking up all thread on"
9814e7f640dSJohn Baldwin 			    "exclusive queue", __func__, sx);
982da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
983da7bbd2cSJohn Baldwin 		    0, SQ_EXCLUSIVE_QUEUE);
984c5aa6b58SJeff Roberson 		sleepq_release(&sx->lock_object);
985da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
986da7bbd2cSJohn Baldwin 			kick_proc0();
9874e7f640dSJohn Baldwin 		break;
9884e7f640dSJohn Baldwin 	}
989d55229b7SJason Evans }
9904e5e677bSJohn Baldwin 
9914e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT
992781a35dfSJohn Baldwin #ifndef INVARIANTS
993781a35dfSJohn Baldwin #undef	_sx_assert
994781a35dfSJohn Baldwin #endif
995781a35dfSJohn Baldwin 
9964e5e677bSJohn Baldwin /*
9974e5e677bSJohn Baldwin  * In the non-WITNESS case, sx_assert() can only detect that at least
9984e5e677bSJohn Baldwin  * *some* thread owns an slock, but it cannot guarantee that *this*
9994e5e677bSJohn Baldwin  * thread owns an slock.
10004e5e677bSJohn Baldwin  */
10014e5e677bSJohn Baldwin void
10024e5e677bSJohn Baldwin _sx_assert(struct sx *sx, int what, const char *file, int line)
10034e5e677bSJohn Baldwin {
10044e7f640dSJohn Baldwin #ifndef WITNESS
10054e7f640dSJohn Baldwin 	int slocked = 0;
10064e7f640dSJohn Baldwin #endif
10074e5e677bSJohn Baldwin 
100803129ba9SJohn Baldwin 	if (panicstr != NULL)
100903129ba9SJohn Baldwin 		return;
10104e5e677bSJohn Baldwin 	switch (what) {
10117ec137e5SJohn Baldwin 	case SA_SLOCKED:
10127ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_NOTRECURSED:
10137ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_RECURSED:
10144e7f640dSJohn Baldwin #ifndef WITNESS
10154e7f640dSJohn Baldwin 		slocked = 1;
10164e7f640dSJohn Baldwin 		/* FALLTHROUGH */
10174e7f640dSJohn Baldwin #endif
10187ec137e5SJohn Baldwin 	case SA_LOCKED:
10197ec137e5SJohn Baldwin 	case SA_LOCKED | SA_NOTRECURSED:
10207ec137e5SJohn Baldwin 	case SA_LOCKED | SA_RECURSED:
10214e5e677bSJohn Baldwin #ifdef WITNESS
1022aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
10234e5e677bSJohn Baldwin #else
10244e7f640dSJohn Baldwin 		/*
10254e7f640dSJohn Baldwin 		 * If some other thread has an exclusive lock or we
10264e7f640dSJohn Baldwin 		 * have one and are asserting a shared lock, fail.
10274e7f640dSJohn Baldwin 		 * Also, if no one has a lock at all, fail.
10284e7f640dSJohn Baldwin 		 */
10294e7f640dSJohn Baldwin 		if (sx->sx_lock == SX_LOCK_UNLOCKED ||
10304e7f640dSJohn Baldwin 		    (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
10314e7f640dSJohn Baldwin 		    sx_xholder(sx) != curthread)))
103203129ba9SJohn Baldwin 			panic("Lock %s not %slocked @ %s:%d\n",
10334e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, slocked ? "share " : "",
10344e7f640dSJohn Baldwin 			    file, line);
10354e7f640dSJohn Baldwin 
10364e7f640dSJohn Baldwin 		if (!(sx->sx_lock & SX_LOCK_SHARED)) {
10374e7f640dSJohn Baldwin 			if (sx_recursed(sx)) {
10387ec137e5SJohn Baldwin 				if (what & SA_NOTRECURSED)
10394e7f640dSJohn Baldwin 					panic("Lock %s recursed @ %s:%d\n",
10404e7f640dSJohn Baldwin 					    sx->lock_object.lo_name, file,
10414e7f640dSJohn Baldwin 					    line);
10427ec137e5SJohn Baldwin 			} else if (what & SA_RECURSED)
10434e7f640dSJohn Baldwin 				panic("Lock %s not recursed @ %s:%d\n",
10444e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
10454e7f640dSJohn Baldwin 		}
10464e5e677bSJohn Baldwin #endif
10474e5e677bSJohn Baldwin 		break;
10487ec137e5SJohn Baldwin 	case SA_XLOCKED:
10497ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_NOTRECURSED:
10507ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_RECURSED:
10514e7f640dSJohn Baldwin 		if (sx_xholder(sx) != curthread)
105203129ba9SJohn Baldwin 			panic("Lock %s not exclusively locked @ %s:%d\n",
1053aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
10544e7f640dSJohn Baldwin 		if (sx_recursed(sx)) {
10557ec137e5SJohn Baldwin 			if (what & SA_NOTRECURSED)
10564e7f640dSJohn Baldwin 				panic("Lock %s recursed @ %s:%d\n",
10574e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
10587ec137e5SJohn Baldwin 		} else if (what & SA_RECURSED)
10594e7f640dSJohn Baldwin 			panic("Lock %s not recursed @ %s:%d\n",
10604e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
10614e5e677bSJohn Baldwin 		break;
10627ec137e5SJohn Baldwin 	case SA_UNLOCKED:
106319b0efd3SPawel Jakub Dawidek #ifdef WITNESS
1064aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
106519b0efd3SPawel Jakub Dawidek #else
1066f6739b1dSPawel Jakub Dawidek 		/*
10674e7f640dSJohn Baldwin 		 * If we hold an exclusve lock fail.  We can't
10684e7f640dSJohn Baldwin 		 * reliably check to see if we hold a shared lock or
10694e7f640dSJohn Baldwin 		 * not.
1070f6739b1dSPawel Jakub Dawidek 		 */
10714e7f640dSJohn Baldwin 		if (sx_xholder(sx) == curthread)
107203129ba9SJohn Baldwin 			panic("Lock %s exclusively locked @ %s:%d\n",
1073aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
107419b0efd3SPawel Jakub Dawidek #endif
107519b0efd3SPawel Jakub Dawidek 		break;
10764e5e677bSJohn Baldwin 	default:
10774e5e677bSJohn Baldwin 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
10784e5e677bSJohn Baldwin 		    line);
10794e5e677bSJohn Baldwin 	}
10804e5e677bSJohn Baldwin }
10814e5e677bSJohn Baldwin #endif	/* INVARIANT_SUPPORT */
1082d272fe53SJohn Baldwin 
1083d272fe53SJohn Baldwin #ifdef DDB
10844e7f640dSJohn Baldwin static void
1085d272fe53SJohn Baldwin db_show_sx(struct lock_object *lock)
1086d272fe53SJohn Baldwin {
1087d272fe53SJohn Baldwin 	struct thread *td;
1088d272fe53SJohn Baldwin 	struct sx *sx;
1089d272fe53SJohn Baldwin 
1090d272fe53SJohn Baldwin 	sx = (struct sx *)lock;
1091d272fe53SJohn Baldwin 
1092d272fe53SJohn Baldwin 	db_printf(" state: ");
10934e7f640dSJohn Baldwin 	if (sx->sx_lock == SX_LOCK_UNLOCKED)
10944e7f640dSJohn Baldwin 		db_printf("UNLOCKED\n");
10950026c92cSJohn Baldwin 	else if (sx->sx_lock == SX_LOCK_DESTROYED) {
10960026c92cSJohn Baldwin 		db_printf("DESTROYED\n");
10970026c92cSJohn Baldwin 		return;
10980026c92cSJohn Baldwin 	} else if (sx->sx_lock & SX_LOCK_SHARED)
10994e7f640dSJohn Baldwin 		db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
11004e7f640dSJohn Baldwin 	else {
11014e7f640dSJohn Baldwin 		td = sx_xholder(sx);
1102d272fe53SJohn Baldwin 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1103431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
11044e7f640dSJohn Baldwin 		if (sx_recursed(sx))
11054e7f640dSJohn Baldwin 			db_printf(" recursed: %d\n", sx->sx_recurse);
11064e7f640dSJohn Baldwin 	}
11074e7f640dSJohn Baldwin 
11084e7f640dSJohn Baldwin 	db_printf(" waiters: ");
11094e7f640dSJohn Baldwin 	switch(sx->sx_lock &
11104e7f640dSJohn Baldwin 	    (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
11114e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS:
11124e7f640dSJohn Baldwin 		db_printf("shared\n");
11134e7f640dSJohn Baldwin 		break;
11144e7f640dSJohn Baldwin 	case SX_LOCK_EXCLUSIVE_WAITERS:
11154e7f640dSJohn Baldwin 		db_printf("exclusive\n");
11164e7f640dSJohn Baldwin 		break;
11174e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
11184e7f640dSJohn Baldwin 		db_printf("exclusive and shared\n");
11194e7f640dSJohn Baldwin 		break;
11204e7f640dSJohn Baldwin 	default:
11214e7f640dSJohn Baldwin 		db_printf("none\n");
11224e7f640dSJohn Baldwin 	}
1123d272fe53SJohn Baldwin }
1124462a7addSJohn Baldwin 
1125462a7addSJohn Baldwin /*
1126462a7addSJohn Baldwin  * Check to see if a thread that is blocked on a sleep queue is actually
1127462a7addSJohn Baldwin  * blocked on an sx lock.  If so, output some details and return true.
1128462a7addSJohn Baldwin  * If the lock has an exclusive owner, return that in *ownerp.
1129462a7addSJohn Baldwin  */
1130462a7addSJohn Baldwin int
1131462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp)
1132462a7addSJohn Baldwin {
1133462a7addSJohn Baldwin 	struct sx *sx;
1134462a7addSJohn Baldwin 
1135462a7addSJohn Baldwin 	/*
11364e7f640dSJohn Baldwin 	 * Check to see if this thread is blocked on an sx lock.
11374e7f640dSJohn Baldwin 	 * First, we check the lock class.  If that is ok, then we
11384e7f640dSJohn Baldwin 	 * compare the lock name against the wait message.
1139462a7addSJohn Baldwin 	 */
11404e7f640dSJohn Baldwin 	sx = td->td_wchan;
11414e7f640dSJohn Baldwin 	if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
11424e7f640dSJohn Baldwin 	    sx->lock_object.lo_name != td->td_wmesg)
1143462a7addSJohn Baldwin 		return (0);
1144462a7addSJohn Baldwin 
1145462a7addSJohn Baldwin 	/* We think we have an sx lock, so output some details. */
1146462a7addSJohn Baldwin 	db_printf("blocked on sx \"%s\" ", td->td_wmesg);
11474e7f640dSJohn Baldwin 	*ownerp = sx_xholder(sx);
11484e7f640dSJohn Baldwin 	if (sx->sx_lock & SX_LOCK_SHARED)
11494e7f640dSJohn Baldwin 		db_printf("SLOCK (count %ju)\n",
11504e7f640dSJohn Baldwin 		    (uintmax_t)SX_SHARERS(sx->sx_lock));
11514e7f640dSJohn Baldwin 	else
1152462a7addSJohn Baldwin 		db_printf("XLOCK\n");
1153462a7addSJohn Baldwin 	return (1);
1154462a7addSJohn Baldwin }
1155d272fe53SJohn Baldwin #endif
1156