xref: /freebsd/sys/kern/kern_sx.c (revision f5f9340b9807d44d200658ba1bbbbbb57ab72e07)
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"
40*f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h"
41a5aedd68SStacey Son #include "opt_kdtrace.h"
42e31d0833SAttilio Rao #include "opt_no_adaptive_sx.h"
434e7f640dSJohn Baldwin 
44677b542eSDavid E. O'Brien #include <sys/cdefs.h>
45677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
46677b542eSDavid E. O'Brien 
476281b30aSJason Evans #include <sys/param.h>
487a7ce668SAndriy Gapon #include <sys/systm.h>
496281b30aSJason Evans #include <sys/ktr.h>
5019284646SJohn Baldwin #include <sys/lock.h>
516281b30aSJason Evans #include <sys/mutex.h>
52d272fe53SJohn Baldwin #include <sys/proc.h>
534e7f640dSJohn Baldwin #include <sys/sleepqueue.h>
546281b30aSJason Evans #include <sys/sx.h>
55e31d0833SAttilio Rao #include <sys/sysctl.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 
71*f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
72*f5f9340bSFabien Thomas #include <sys/pmckern.h>
73*f5f9340bSFabien Thomas PMC_SOFT_DECLARE( , , lock, failed);
74*f5f9340bSFabien Thomas #endif
75*f5f9340bSFabien Thomas 
764e7f640dSJohn Baldwin /* Handy macros for sleep queues. */
774e7f640dSJohn Baldwin #define	SQ_EXCLUSIVE_QUEUE	0
784e7f640dSJohn Baldwin #define	SQ_SHARED_QUEUE		1
794e7f640dSJohn Baldwin 
804e7f640dSJohn Baldwin /*
814e7f640dSJohn Baldwin  * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file.  We
824e7f640dSJohn Baldwin  * drop Giant anytime we have to sleep or if we adaptively spin.
834e7f640dSJohn Baldwin  */
844e7f640dSJohn Baldwin #define	GIANT_DECLARE							\
854e7f640dSJohn Baldwin 	int _giantcnt = 0;						\
864e7f640dSJohn Baldwin 	WITNESS_SAVE_DECL(Giant)					\
874e7f640dSJohn Baldwin 
884e7f640dSJohn Baldwin #define	GIANT_SAVE() do {						\
894e7f640dSJohn Baldwin 	if (mtx_owned(&Giant)) {					\
904e7f640dSJohn Baldwin 		WITNESS_SAVE(&Giant.lock_object, Giant);		\
914e7f640dSJohn Baldwin 		while (mtx_owned(&Giant)) {				\
924e7f640dSJohn Baldwin 			_giantcnt++;					\
934e7f640dSJohn Baldwin 			mtx_unlock(&Giant);				\
944e7f640dSJohn Baldwin 		}							\
954e7f640dSJohn Baldwin 	}								\
964e7f640dSJohn Baldwin } while (0)
974e7f640dSJohn Baldwin 
984e7f640dSJohn Baldwin #define GIANT_RESTORE() do {						\
994e7f640dSJohn Baldwin 	if (_giantcnt > 0) {						\
1004e7f640dSJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);			\
1014e7f640dSJohn Baldwin 		while (_giantcnt--)					\
1024e7f640dSJohn Baldwin 			mtx_lock(&Giant);				\
1034e7f640dSJohn Baldwin 		WITNESS_RESTORE(&Giant.lock_object, Giant);		\
1044e7f640dSJohn Baldwin 	}								\
1054e7f640dSJohn Baldwin } while (0)
1064e7f640dSJohn Baldwin 
1074e7f640dSJohn Baldwin /*
108da7d0d1eSJohn Baldwin  * Returns true if an exclusive lock is recursed.  It assumes
109da7d0d1eSJohn Baldwin  * curthread currently has an exclusive lock.
1104e7f640dSJohn Baldwin  */
11190356491SAttilio Rao #define	sx_recurse		lock_object.lo_data
1124e7f640dSJohn Baldwin #define	sx_recursed(sx)		((sx)->sx_recurse != 0)
1134e7f640dSJohn Baldwin 
114d576deedSPawel Jakub Dawidek static void	assert_sx(const struct lock_object *lock, int what);
1154e7f640dSJohn Baldwin #ifdef DDB
116d576deedSPawel Jakub Dawidek static void	db_show_sx(const struct lock_object *lock);
117d272fe53SJohn Baldwin #endif
1186e21afd4SJohn Baldwin static void	lock_sx(struct lock_object *lock, int how);
119a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
120d576deedSPawel Jakub Dawidek static int	owner_sx(const struct lock_object *lock, struct thread **owner);
121a5aedd68SStacey Son #endif
1226e21afd4SJohn Baldwin static int	unlock_sx(struct lock_object *lock);
123d272fe53SJohn Baldwin 
12419284646SJohn Baldwin struct lock_class lock_class_sx = {
125ae8dde30SJohn Baldwin 	.lc_name = "sx",
126ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
127f9721b43SAttilio Rao 	.lc_assert = assert_sx,
128d272fe53SJohn Baldwin #ifdef DDB
129ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_sx,
130d272fe53SJohn Baldwin #endif
1316e21afd4SJohn Baldwin 	.lc_lock = lock_sx,
1326e21afd4SJohn Baldwin 	.lc_unlock = unlock_sx,
133a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
134a5aedd68SStacey Son 	.lc_owner = owner_sx,
135a5aedd68SStacey Son #endif
13619284646SJohn Baldwin };
13719284646SJohn Baldwin 
138781a35dfSJohn Baldwin #ifndef INVARIANTS
139781a35dfSJohn Baldwin #define	_sx_assert(sx, what, file, line)
140781a35dfSJohn Baldwin #endif
141781a35dfSJohn Baldwin 
1421ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX
1431ae1c2a3SAttilio Rao static u_int asx_retries = 10;
1441ae1c2a3SAttilio Rao static u_int asx_loops = 10000;
1456472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
146fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
147fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
1481ae1c2a3SAttilio Rao #endif
1491ae1c2a3SAttilio Rao 
1506281b30aSJason Evans void
151d576deedSPawel Jakub Dawidek assert_sx(const struct lock_object *lock, int what)
152f9721b43SAttilio Rao {
153f9721b43SAttilio Rao 
154d576deedSPawel Jakub Dawidek 	sx_assert((const struct sx *)lock, what);
155f9721b43SAttilio Rao }
156f9721b43SAttilio Rao 
157f9721b43SAttilio Rao void
1586e21afd4SJohn Baldwin lock_sx(struct lock_object *lock, int how)
1596e21afd4SJohn Baldwin {
1606e21afd4SJohn Baldwin 	struct sx *sx;
1616e21afd4SJohn Baldwin 
1626e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1636e21afd4SJohn Baldwin 	if (how)
1646e21afd4SJohn Baldwin 		sx_xlock(sx);
1656e21afd4SJohn Baldwin 	else
1666e21afd4SJohn Baldwin 		sx_slock(sx);
1676e21afd4SJohn Baldwin }
1686e21afd4SJohn Baldwin 
1696e21afd4SJohn Baldwin int
1706e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock)
1716e21afd4SJohn Baldwin {
1726e21afd4SJohn Baldwin 	struct sx *sx;
1736e21afd4SJohn Baldwin 
1746e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1757ec137e5SJohn Baldwin 	sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
1766e21afd4SJohn Baldwin 	if (sx_xlocked(sx)) {
1776e21afd4SJohn Baldwin 		sx_xunlock(sx);
1786e21afd4SJohn Baldwin 		return (1);
1796e21afd4SJohn Baldwin 	} else {
1806e21afd4SJohn Baldwin 		sx_sunlock(sx);
1816e21afd4SJohn Baldwin 		return (0);
1826e21afd4SJohn Baldwin 	}
1836e21afd4SJohn Baldwin }
1846e21afd4SJohn Baldwin 
185a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
186a5aedd68SStacey Son int
187d576deedSPawel Jakub Dawidek owner_sx(const struct lock_object *lock, struct thread **owner)
188a5aedd68SStacey Son {
189d576deedSPawel Jakub Dawidek         const struct sx *sx = (const struct sx *)lock;
190a5aedd68SStacey Son 	uintptr_t x = sx->sx_lock;
191a5aedd68SStacey Son 
192a5aedd68SStacey Son         *owner = (struct thread *)SX_OWNER(x);
193a5aedd68SStacey Son         return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
194a5aedd68SStacey Son 	    (*owner != NULL));
195a5aedd68SStacey Son }
196a5aedd68SStacey Son #endif
197a5aedd68SStacey Son 
1986e21afd4SJohn Baldwin void
199c27b5699SAndrew R. Reiter sx_sysinit(void *arg)
200c27b5699SAndrew R. Reiter {
201c27b5699SAndrew R. Reiter 	struct sx_args *sargs = arg;
202c27b5699SAndrew R. Reiter 
203e4cd31ddSJeff Roberson 	sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
204c27b5699SAndrew R. Reiter }
205c27b5699SAndrew R. Reiter 
206c27b5699SAndrew R. Reiter void
2074e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts)
2086281b30aSJason Evans {
2094e7f640dSJohn Baldwin 	int flags;
2106281b30aSJason Evans 
211b0d67325SJohn Baldwin 	MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
2121ae1c2a3SAttilio Rao 	    SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
213353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
214353998acSAttilio Rao 	    ("%s: sx_lock not aligned for %s: %p", __func__, description,
215353998acSAttilio Rao 	    &sx->sx_lock));
216b0d67325SJohn Baldwin 
217f0830182SAttilio Rao 	flags = LO_SLEEPABLE | LO_UPGRADABLE;
2184e7f640dSJohn Baldwin 	if (opts & SX_DUPOK)
2194e7f640dSJohn Baldwin 		flags |= LO_DUPOK;
2204e7f640dSJohn Baldwin 	if (opts & SX_NOPROFILE)
2214e7f640dSJohn Baldwin 		flags |= LO_NOPROFILE;
2224e7f640dSJohn Baldwin 	if (!(opts & SX_NOWITNESS))
2234e7f640dSJohn Baldwin 		flags |= LO_WITNESS;
224f0830182SAttilio Rao 	if (opts & SX_RECURSE)
225f0830182SAttilio Rao 		flags |= LO_RECURSABLE;
2264e7f640dSJohn Baldwin 	if (opts & SX_QUIET)
2274e7f640dSJohn Baldwin 		flags |= LO_QUIET;
2284e7f640dSJohn Baldwin 
229f0830182SAttilio Rao 	flags |= opts & SX_NOADAPTIVE;
2304e7f640dSJohn Baldwin 	sx->sx_lock = SX_LOCK_UNLOCKED;
2314e7f640dSJohn Baldwin 	sx->sx_recurse = 0;
2324e7f640dSJohn Baldwin 	lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
2336281b30aSJason Evans }
2346281b30aSJason Evans 
2356281b30aSJason Evans void
2366281b30aSJason Evans sx_destroy(struct sx *sx)
2376281b30aSJason Evans {
2386281b30aSJason Evans 
2394e7f640dSJohn Baldwin 	KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
2404e7f640dSJohn Baldwin 	KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
2410026c92cSJohn Baldwin 	sx->sx_lock = SX_LOCK_DESTROYED;
242aa89d8cdSJohn Baldwin 	lock_destroy(&sx->lock_object);
2436281b30aSJason Evans }
2446281b30aSJason Evans 
245f9819486SAttilio Rao int
246f9819486SAttilio Rao _sx_slock(struct sx *sx, int opts, const char *file, int line)
2476281b30aSJason Evans {
248f9819486SAttilio Rao 	int error = 0;
2496281b30aSJason Evans 
25035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
25135370593SAndriy Gapon 		return (0);
2524e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
2530026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
2540026c92cSJohn Baldwin 	    ("sx_slock() of destroyed sx @ %s:%d", file, line));
25541313430SJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
256f9819486SAttilio Rao 	error = __sx_slock(sx, opts, file, line);
257f9819486SAttilio Rao 	if (!error) {
258aa89d8cdSJohn Baldwin 		LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
259aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, 0, file, line);
260764e4d54SJohn Baldwin 		curthread->td_locks++;
2616281b30aSJason Evans 	}
2626281b30aSJason Evans 
263f9819486SAttilio Rao 	return (error);
264f9819486SAttilio Rao }
265f9819486SAttilio Rao 
2665f36700aSJohn Baldwin int
2679fde98bbSAttilio Rao sx_try_slock_(struct sx *sx, const char *file, int line)
2685f36700aSJohn Baldwin {
2694e7f640dSJohn Baldwin 	uintptr_t x;
2705f36700aSJohn Baldwin 
27135370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
27235370593SAndriy Gapon 		return (1);
27335370593SAndriy Gapon 
274764a938bSPawel Jakub Dawidek 	for (;;) {
2754e7f640dSJohn Baldwin 		x = sx->sx_lock;
2760026c92cSJohn Baldwin 		KASSERT(x != SX_LOCK_DESTROYED,
2770026c92cSJohn Baldwin 		    ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
278764a938bSPawel Jakub Dawidek 		if (!(x & SX_LOCK_SHARED))
279764a938bSPawel Jakub Dawidek 			break;
280764a938bSPawel Jakub Dawidek 		if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
281aa89d8cdSJohn Baldwin 			LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
282aa89d8cdSJohn Baldwin 			WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
283764e4d54SJohn Baldwin 			curthread->td_locks++;
2845f36700aSJohn Baldwin 			return (1);
2855f36700aSJohn Baldwin 		}
286764a938bSPawel Jakub Dawidek 	}
2874e7f640dSJohn Baldwin 
2884e7f640dSJohn Baldwin 	LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
2894e7f640dSJohn Baldwin 	return (0);
2905f36700aSJohn Baldwin }
2915f36700aSJohn Baldwin 
292f9819486SAttilio Rao int
293f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line)
2946281b30aSJason Evans {
295f9819486SAttilio Rao 	int error = 0;
2966281b30aSJason Evans 
29735370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
29835370593SAndriy Gapon 		return (0);
2994e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3000026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3010026c92cSJohn Baldwin 	    ("sx_xlock() of destroyed sx @ %s:%d", file, line));
302aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
30341313430SJohn Baldwin 	    line, NULL);
304f9819486SAttilio Rao 	error = __sx_xlock(sx, curthread, opts, file, line);
305f9819486SAttilio Rao 	if (!error) {
306f9819486SAttilio Rao 		LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
307f9819486SAttilio Rao 		    file, line);
308aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
309764e4d54SJohn Baldwin 		curthread->td_locks++;
3106281b30aSJason Evans 	}
3116281b30aSJason Evans 
312f9819486SAttilio Rao 	return (error);
313f9819486SAttilio Rao }
314f9819486SAttilio Rao 
3155f36700aSJohn Baldwin int
3169fde98bbSAttilio Rao sx_try_xlock_(struct sx *sx, const char *file, int line)
3175f36700aSJohn Baldwin {
3184e7f640dSJohn Baldwin 	int rval;
3195f36700aSJohn Baldwin 
32035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
32135370593SAndriy Gapon 		return (1);
32235370593SAndriy Gapon 
3234e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3240026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3250026c92cSJohn Baldwin 	    ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
3264e7f640dSJohn Baldwin 
327f0830182SAttilio Rao 	if (sx_xlocked(sx) &&
328f0830182SAttilio Rao 	    (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
3294e7f640dSJohn Baldwin 		sx->sx_recurse++;
3304e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
3314e7f640dSJohn Baldwin 		rval = 1;
3324e7f640dSJohn Baldwin 	} else
3334e7f640dSJohn Baldwin 		rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
3344e7f640dSJohn Baldwin 		    (uintptr_t)curthread);
3354e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
3364e7f640dSJohn Baldwin 	if (rval) {
3374e7f640dSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
3384e7f640dSJohn Baldwin 		    file, line);
339764e4d54SJohn Baldwin 		curthread->td_locks++;
3405f36700aSJohn Baldwin 	}
3414e7f640dSJohn Baldwin 
3424e7f640dSJohn Baldwin 	return (rval);
3435f36700aSJohn Baldwin }
3445f36700aSJohn Baldwin 
3456281b30aSJason Evans void
34619284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line)
3476281b30aSJason Evans {
3486281b30aSJason Evans 
34935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
35035370593SAndriy Gapon 		return;
3514e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3520026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3530026c92cSJohn Baldwin 	    ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
3547ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
355764e4d54SJohn Baldwin 	curthread->td_locks--;
356aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
357aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
3584e7f640dSJohn Baldwin 	__sx_sunlock(sx, file, line);
359a5aedd68SStacey Son 	LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
3606281b30aSJason Evans }
3616281b30aSJason Evans 
3626281b30aSJason Evans void
36319284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line)
3646281b30aSJason Evans {
3656281b30aSJason Evans 
36635370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
36735370593SAndriy Gapon 		return;
3684e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3690026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3700026c92cSJohn Baldwin 	    ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
3717ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED, file, line);
372764e4d54SJohn Baldwin 	curthread->td_locks--;
373aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
3744e7f640dSJohn Baldwin 	LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
3754e7f640dSJohn Baldwin 	    line);
376afc0bfbdSKip Macy 	if (!sx_recursed(sx))
377a5aedd68SStacey Son 		LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
3784e7f640dSJohn Baldwin 	__sx_xunlock(sx, curthread, file, line);
3796281b30aSJason Evans }
380d55229b7SJason Evans 
3814e7f640dSJohn Baldwin /*
3824e7f640dSJohn Baldwin  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
3834e7f640dSJohn Baldwin  * This will only succeed if this thread holds a single shared lock.
3844e7f640dSJohn Baldwin  * Return 1 if if the upgrade succeed, 0 otherwise.
3854e7f640dSJohn Baldwin  */
386d55229b7SJason Evans int
3879fde98bbSAttilio Rao sx_try_upgrade_(struct sx *sx, const char *file, int line)
388d55229b7SJason Evans {
3894e7f640dSJohn Baldwin 	uintptr_t x;
3904e7f640dSJohn Baldwin 	int success;
391d55229b7SJason Evans 
39235370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
39335370593SAndriy Gapon 		return (1);
39435370593SAndriy Gapon 
3950026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3960026c92cSJohn Baldwin 	    ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
3977ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
398d55229b7SJason Evans 
3994e7f640dSJohn Baldwin 	/*
4004e7f640dSJohn Baldwin 	 * Try to switch from one shared lock to an exclusive lock.  We need
4014e7f640dSJohn Baldwin 	 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
4024e7f640dSJohn Baldwin 	 * we will wake up the exclusive waiters when we drop the lock.
4034e7f640dSJohn Baldwin 	 */
4044e7f640dSJohn Baldwin 	x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
4054e7f640dSJohn Baldwin 	success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
4064e7f640dSJohn Baldwin 	    (uintptr_t)curthread | x);
4074e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
408a5aedd68SStacey Son 	if (success) {
409aa89d8cdSJohn Baldwin 		WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
410b0b7cb50SJohn Baldwin 		    file, line);
411a5aedd68SStacey Son 		LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
412a5aedd68SStacey Son 	}
4134e7f640dSJohn Baldwin 	return (success);
414d55229b7SJason Evans }
415d55229b7SJason Evans 
4164e7f640dSJohn Baldwin /*
4174e7f640dSJohn Baldwin  * Downgrade an unrecursed exclusive lock into a single shared lock.
4184e7f640dSJohn Baldwin  */
419d55229b7SJason Evans void
4209fde98bbSAttilio Rao sx_downgrade_(struct sx *sx, const char *file, int line)
421d55229b7SJason Evans {
4224e7f640dSJohn Baldwin 	uintptr_t x;
423da7bbd2cSJohn Baldwin 	int wakeup_swapper;
424d55229b7SJason Evans 
42535370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
42635370593SAndriy Gapon 		return;
42735370593SAndriy Gapon 
4280026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4290026c92cSJohn Baldwin 	    ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
4307ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
4314e7f640dSJohn Baldwin #ifndef INVARIANTS
4324e7f640dSJohn Baldwin 	if (sx_recursed(sx))
4334e7f640dSJohn Baldwin 		panic("downgrade of a recursed lock");
4344e7f640dSJohn Baldwin #endif
435d55229b7SJason Evans 
436aa89d8cdSJohn Baldwin 	WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
437d55229b7SJason Evans 
4384e7f640dSJohn Baldwin 	/*
4394e7f640dSJohn Baldwin 	 * Try to switch from an exclusive lock with no shared waiters
4404e7f640dSJohn Baldwin 	 * to one sharer with no shared waiters.  If there are
4414e7f640dSJohn Baldwin 	 * exclusive waiters, we don't need to lock the sleep queue so
4424e7f640dSJohn Baldwin 	 * long as we preserve the flag.  We do one quick try and if
4434e7f640dSJohn Baldwin 	 * that fails we grab the sleepq lock to keep the flags from
4444e7f640dSJohn Baldwin 	 * changing and do it the slow way.
4454e7f640dSJohn Baldwin 	 *
4464e7f640dSJohn Baldwin 	 * We have to lock the sleep queue if there are shared waiters
4474e7f640dSJohn Baldwin 	 * so we can wake them up.
4484e7f640dSJohn Baldwin 	 */
4494e7f640dSJohn Baldwin 	x = sx->sx_lock;
4504e7f640dSJohn Baldwin 	if (!(x & SX_LOCK_SHARED_WAITERS) &&
4514e7f640dSJohn Baldwin 	    atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
4524e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
4534e7f640dSJohn Baldwin 		LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
4544e7f640dSJohn Baldwin 		return;
4554e7f640dSJohn Baldwin 	}
4564e7f640dSJohn Baldwin 
4574e7f640dSJohn Baldwin 	/*
4584e7f640dSJohn Baldwin 	 * Lock the sleep queue so we can read the waiters bits
4594e7f640dSJohn Baldwin 	 * without any races and wakeup any shared waiters.
4604e7f640dSJohn Baldwin 	 */
4614e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
4624e7f640dSJohn Baldwin 
4634e7f640dSJohn Baldwin 	/*
4644e7f640dSJohn Baldwin 	 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
4654e7f640dSJohn Baldwin 	 * shared lock.  If there are any shared waiters, wake them up.
4664e7f640dSJohn Baldwin 	 */
467da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
4684e7f640dSJohn Baldwin 	x = sx->sx_lock;
4694e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
4704e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS));
4714e7f640dSJohn Baldwin 	if (x & SX_LOCK_SHARED_WAITERS)
472da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
473da7bbd2cSJohn Baldwin 		    0, SQ_SHARED_QUEUE);
4744e7f640dSJohn Baldwin 	sleepq_release(&sx->lock_object);
475d55229b7SJason Evans 
476aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
477a5aedd68SStacey Son 	LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
478da7bbd2cSJohn Baldwin 
479da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
480da7bbd2cSJohn Baldwin 		kick_proc0();
4814e7f640dSJohn Baldwin }
482d55229b7SJason Evans 
4834e7f640dSJohn Baldwin /*
4844e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xlock
4854e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
4864e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
4874e7f640dSJohn Baldwin  * accessible from at least sx.h.
4884e7f640dSJohn Baldwin  */
489f9819486SAttilio Rao int
490f9819486SAttilio Rao _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
491f9819486SAttilio Rao     int line)
4924e7f640dSJohn Baldwin {
4934e7f640dSJohn Baldwin 	GIANT_DECLARE;
4944e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
4954e7f640dSJohn Baldwin 	volatile struct thread *owner;
4961ae1c2a3SAttilio Rao 	u_int i, spintries = 0;
4974e7f640dSJohn Baldwin #endif
4984e7f640dSJohn Baldwin 	uintptr_t x;
4991723a064SJeff Roberson #ifdef LOCK_PROFILING
5001723a064SJeff Roberson 	uint64_t waittime = 0;
5011723a064SJeff Roberson 	int contested = 0;
5021723a064SJeff Roberson #endif
5031723a064SJeff Roberson 	int error = 0;
504a5aedd68SStacey Son #ifdef	KDTRACE_HOOKS
505a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
506a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
507a5aedd68SStacey Son 	int64_t sleep_time = 0;
508a5aedd68SStacey Son #endif
5094e7f640dSJohn Baldwin 
51035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
51135370593SAndriy Gapon 		return (0);
51235370593SAndriy Gapon 
5134e7f640dSJohn Baldwin 	/* If we already hold an exclusive lock, then recurse. */
5144e7f640dSJohn Baldwin 	if (sx_xlocked(sx)) {
515f0830182SAttilio Rao 		KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
516b0d67325SJohn Baldwin 	    ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
517b0d67325SJohn Baldwin 		    sx->lock_object.lo_name, file, line));
5184e7f640dSJohn Baldwin 		sx->sx_recurse++;
5194e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
5204e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
5214e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
522f9819486SAttilio Rao 		return (0);
5234e7f640dSJohn Baldwin 	}
5244e7f640dSJohn Baldwin 
5254e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
5264e7f640dSJohn Baldwin 		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
5274e7f640dSJohn Baldwin 		    sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
5284e7f640dSJohn Baldwin 
5294e7f640dSJohn Baldwin 	while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
530a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
531a5aedd68SStacey Son 		spin_cnt++;
532a5aedd68SStacey Son #endif
533*f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
534*f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
535*f5f9340bSFabien Thomas #endif
536eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
537eea4f254SJeff Roberson 		    &waittime);
5384e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5394e7f640dSJohn Baldwin 		/*
5404e7f640dSJohn Baldwin 		 * If the lock is write locked and the owner is
5414e7f640dSJohn Baldwin 		 * running on another CPU, spin until the owner stops
5424e7f640dSJohn Baldwin 		 * running or the state of the lock changes.
5434e7f640dSJohn Baldwin 		 */
5444e7f640dSJohn Baldwin 		x = sx->sx_lock;
5458545538bSJohn Baldwin 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
5461ae1c2a3SAttilio Rao 			if ((x & SX_LOCK_SHARED) == 0) {
5474e7f640dSJohn Baldwin 				x = SX_OWNER(x);
5484e7f640dSJohn Baldwin 				owner = (struct thread *)x;
5494e7f640dSJohn Baldwin 				if (TD_IS_RUNNING(owner)) {
5504e7f640dSJohn Baldwin 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5514e7f640dSJohn Baldwin 						CTR3(KTR_LOCK,
5524e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
5534e7f640dSJohn Baldwin 						    __func__, sx, owner);
5544e7f640dSJohn Baldwin 					GIANT_SAVE();
5554e7f640dSJohn Baldwin 					while (SX_OWNER(sx->sx_lock) == x &&
556a5aedd68SStacey Son 					    TD_IS_RUNNING(owner)) {
5574e7f640dSJohn Baldwin 						cpu_spinwait();
558a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
559a5aedd68SStacey Son 						spin_cnt++;
560a5aedd68SStacey Son #endif
561a5aedd68SStacey Son 					}
5624e7f640dSJohn Baldwin 					continue;
5634e7f640dSJohn Baldwin 				}
5641ae1c2a3SAttilio Rao 			} else if (SX_SHARERS(x) && spintries < asx_retries) {
5658d3635c4SAttilio Rao 				GIANT_SAVE();
5661ae1c2a3SAttilio Rao 				spintries++;
5671ae1c2a3SAttilio Rao 				for (i = 0; i < asx_loops; i++) {
5681ae1c2a3SAttilio Rao 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5691ae1c2a3SAttilio Rao 						CTR4(KTR_LOCK,
5701ae1c2a3SAttilio Rao 				    "%s: shared spinning on %p with %u and %u",
5711ae1c2a3SAttilio Rao 						    __func__, sx, spintries, i);
5721ae1c2a3SAttilio Rao 					x = sx->sx_lock;
5731ae1c2a3SAttilio Rao 					if ((x & SX_LOCK_SHARED) == 0 ||
5741ae1c2a3SAttilio Rao 					    SX_SHARERS(x) == 0)
5751ae1c2a3SAttilio Rao 						break;
5761ae1c2a3SAttilio Rao 					cpu_spinwait();
5771ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS
5781ae1c2a3SAttilio Rao 					spin_cnt++;
5791ae1c2a3SAttilio Rao #endif
5801ae1c2a3SAttilio Rao 				}
5811ae1c2a3SAttilio Rao 				if (i != asx_loops)
5821ae1c2a3SAttilio Rao 					continue;
5831ae1c2a3SAttilio Rao 			}
5844e7f640dSJohn Baldwin 		}
5854e7f640dSJohn Baldwin #endif
5864e7f640dSJohn Baldwin 
5874e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
5884e7f640dSJohn Baldwin 		x = sx->sx_lock;
5894e7f640dSJohn Baldwin 
5904e7f640dSJohn Baldwin 		/*
5914e7f640dSJohn Baldwin 		 * If the lock was released while spinning on the
5924e7f640dSJohn Baldwin 		 * sleep queue chain lock, try again.
5934e7f640dSJohn Baldwin 		 */
5944e7f640dSJohn Baldwin 		if (x == SX_LOCK_UNLOCKED) {
5954e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
5964e7f640dSJohn Baldwin 			continue;
5974e7f640dSJohn Baldwin 		}
5984e7f640dSJohn Baldwin 
5994e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
6004e7f640dSJohn Baldwin 		/*
6014e7f640dSJohn Baldwin 		 * The current lock owner might have started executing
6024e7f640dSJohn Baldwin 		 * on another CPU (or the lock could have changed
6034e7f640dSJohn Baldwin 		 * owners) while we were waiting on the sleep queue
6044e7f640dSJohn Baldwin 		 * chain lock.  If so, drop the sleep queue lock and try
6054e7f640dSJohn Baldwin 		 * again.
6064e7f640dSJohn Baldwin 		 */
6074e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
6081ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
6094e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
6104e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
6114e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6124e7f640dSJohn Baldwin 				continue;
6134e7f640dSJohn Baldwin 			}
6144e7f640dSJohn Baldwin 		}
6154e7f640dSJohn Baldwin #endif
6164e7f640dSJohn Baldwin 
6174e7f640dSJohn Baldwin 		/*
6184e7f640dSJohn Baldwin 		 * If an exclusive lock was released with both shared
6194e7f640dSJohn Baldwin 		 * and exclusive waiters and a shared waiter hasn't
6204e7f640dSJohn Baldwin 		 * woken up and acquired the lock yet, sx_lock will be
6214e7f640dSJohn Baldwin 		 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
6224e7f640dSJohn Baldwin 		 * If we see that value, try to acquire it once.  Note
6234e7f640dSJohn Baldwin 		 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
6244e7f640dSJohn Baldwin 		 * as there are other exclusive waiters still.  If we
6254e7f640dSJohn Baldwin 		 * fail, restart the loop.
6264e7f640dSJohn Baldwin 		 */
6274e7f640dSJohn Baldwin 		if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
6284e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock,
6294e7f640dSJohn Baldwin 			    SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
6304e7f640dSJohn Baldwin 			    tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
6314e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6324e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
6334e7f640dSJohn Baldwin 				    __func__, sx);
6344e7f640dSJohn Baldwin 				break;
6354e7f640dSJohn Baldwin 			}
6364e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6374e7f640dSJohn Baldwin 			continue;
6384e7f640dSJohn Baldwin 		}
6394e7f640dSJohn Baldwin 
6404e7f640dSJohn Baldwin 		/*
6414e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
6424e7f640dSJohn Baldwin 		 * than loop back and retry.
6434e7f640dSJohn Baldwin 		 */
6444e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
6454e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
6464e7f640dSJohn Baldwin 			    x | SX_LOCK_EXCLUSIVE_WAITERS)) {
6474e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6484e7f640dSJohn Baldwin 				continue;
6494e7f640dSJohn Baldwin 			}
6504e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
6514e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
6524e7f640dSJohn Baldwin 				    __func__, sx);
6534e7f640dSJohn Baldwin 		}
6544e7f640dSJohn Baldwin 
6554e7f640dSJohn Baldwin 		/*
6564e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the exclusive
6574e7f640dSJohn Baldwin 		 * lock and the exclusive waiters flag is set, we have
6584e7f640dSJohn Baldwin 		 * to sleep.
6594e7f640dSJohn Baldwin 		 */
6604e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6614e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
6624e7f640dSJohn Baldwin 			    __func__, sx);
6634e7f640dSJohn Baldwin 
664a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
665a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
666a5aedd68SStacey Son #endif
6674e7f640dSJohn Baldwin 		GIANT_SAVE();
6684e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
669f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
670f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
671f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
672c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
673f9819486SAttilio Rao 		else
674c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
675a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
676a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
677a5aedd68SStacey Son 		sleep_cnt++;
678a5aedd68SStacey Son #endif
679f9819486SAttilio Rao 		if (error) {
680f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
681f9819486SAttilio Rao 				CTR2(KTR_LOCK,
682f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
683f9819486SAttilio Rao 				    __func__, sx);
684f9819486SAttilio Rao 			break;
685f9819486SAttilio Rao 		}
6864e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6874e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
6884e7f640dSJohn Baldwin 			    __func__, sx);
6894e7f640dSJohn Baldwin 	}
6904e7f640dSJohn Baldwin 
6914e7f640dSJohn Baldwin 	GIANT_RESTORE();
692f9819486SAttilio Rao 	if (!error)
693a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
694a5aedd68SStacey Son 		    contested, waittime, file, line);
695a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
696a5aedd68SStacey Son 	if (sleep_time)
697a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
698a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
699a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
700a5aedd68SStacey Son #endif
701f9819486SAttilio Rao 	return (error);
7024e7f640dSJohn Baldwin }
7034e7f640dSJohn Baldwin 
7044e7f640dSJohn Baldwin /*
7054e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xunlock
7064e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7074e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7084e7f640dSJohn Baldwin  * accessible from at least sx.h.
7094e7f640dSJohn Baldwin  */
7104e7f640dSJohn Baldwin void
7114e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
7124e7f640dSJohn Baldwin {
7134e7f640dSJohn Baldwin 	uintptr_t x;
714da7bbd2cSJohn Baldwin 	int queue, wakeup_swapper;
7154e7f640dSJohn Baldwin 
71635370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
71735370593SAndriy Gapon 		return;
71835370593SAndriy Gapon 
7194e7f640dSJohn Baldwin 	MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
7204e7f640dSJohn Baldwin 
7214e7f640dSJohn Baldwin 	/* If the lock is recursed, then unrecurse one level. */
7224e7f640dSJohn Baldwin 	if (sx_xlocked(sx) && sx_recursed(sx)) {
7234e7f640dSJohn Baldwin 		if ((--sx->sx_recurse) == 0)
7244e7f640dSJohn Baldwin 			atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
7254e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7264e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
7274e7f640dSJohn Baldwin 		return;
7284e7f640dSJohn Baldwin 	}
7294e7f640dSJohn Baldwin 	MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
7304e7f640dSJohn Baldwin 	    SX_LOCK_EXCLUSIVE_WAITERS));
7314e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7324e7f640dSJohn Baldwin 		CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
7334e7f640dSJohn Baldwin 
7344e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
7354e7f640dSJohn Baldwin 	x = SX_LOCK_UNLOCKED;
7364e7f640dSJohn Baldwin 
7374e7f640dSJohn Baldwin 	/*
7384e7f640dSJohn Baldwin 	 * The wake up algorithm here is quite simple and probably not
7394e7f640dSJohn Baldwin 	 * ideal.  It gives precedence to shared waiters if they are
7404e7f640dSJohn Baldwin 	 * present.  For this condition, we have to preserve the
7414e7f640dSJohn Baldwin 	 * state of the exclusive waiters flag.
7422028867dSAttilio Rao 	 * If interruptible sleeps left the shared queue empty avoid a
7432028867dSAttilio Rao 	 * starvation for the threads sleeping on the exclusive queue by giving
7442028867dSAttilio Rao 	 * them precedence and cleaning up the shared waiters bit anyway.
7454e7f640dSJohn Baldwin 	 */
7462028867dSAttilio Rao 	if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
7472028867dSAttilio Rao 	    sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
7484e7f640dSJohn Baldwin 		queue = SQ_SHARED_QUEUE;
7494e7f640dSJohn Baldwin 		x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
7504e7f640dSJohn Baldwin 	} else
7514e7f640dSJohn Baldwin 		queue = SQ_EXCLUSIVE_QUEUE;
7524e7f640dSJohn Baldwin 
7534e7f640dSJohn Baldwin 	/* Wake up all the waiters for the specific queue. */
7544e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7554e7f640dSJohn Baldwin 		CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
7564e7f640dSJohn Baldwin 		    __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
7574e7f640dSJohn Baldwin 		    "exclusive");
7584e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, x);
759da7bbd2cSJohn Baldwin 	wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
760da7bbd2cSJohn Baldwin 	    queue);
761c5aa6b58SJeff Roberson 	sleepq_release(&sx->lock_object);
762da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
763da7bbd2cSJohn Baldwin 		kick_proc0();
7644e7f640dSJohn Baldwin }
7654e7f640dSJohn Baldwin 
7664e7f640dSJohn Baldwin /*
7674e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_slock
7684e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7694e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7704e7f640dSJohn Baldwin  * accessible from at least sx.h.
7714e7f640dSJohn Baldwin  */
772f9819486SAttilio Rao int
773f9819486SAttilio Rao _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
7744e7f640dSJohn Baldwin {
7754e7f640dSJohn Baldwin 	GIANT_DECLARE;
7764e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
7774e7f640dSJohn Baldwin 	volatile struct thread *owner;
7784e7f640dSJohn Baldwin #endif
7791723a064SJeff Roberson #ifdef LOCK_PROFILING
780c1a6d9faSAttilio Rao 	uint64_t waittime = 0;
781c1a6d9faSAttilio Rao 	int contested = 0;
7821723a064SJeff Roberson #endif
7834e7f640dSJohn Baldwin 	uintptr_t x;
784c1a6d9faSAttilio Rao 	int error = 0;
785a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
786a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
787a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
788a5aedd68SStacey Son 	int64_t sleep_time = 0;
789a5aedd68SStacey Son #endif
790c1a6d9faSAttilio Rao 
79135370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
79235370593SAndriy Gapon 		return (0);
79335370593SAndriy Gapon 
7944e7f640dSJohn Baldwin 	/*
7954e7f640dSJohn Baldwin 	 * As with rwlocks, we don't make any attempt to try to block
7964e7f640dSJohn Baldwin 	 * shared locks once there is an exclusive waiter.
7974e7f640dSJohn Baldwin 	 */
7984e7f640dSJohn Baldwin 	for (;;) {
799a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
800a5aedd68SStacey Son 		spin_cnt++;
801a5aedd68SStacey Son #endif
8024e7f640dSJohn Baldwin 		x = sx->sx_lock;
8034e7f640dSJohn Baldwin 
8044e7f640dSJohn Baldwin 		/*
8054e7f640dSJohn Baldwin 		 * If no other thread has an exclusive lock then try to bump up
8064e7f640dSJohn Baldwin 		 * the count of sharers.  Since we have to preserve the state
8074e7f640dSJohn Baldwin 		 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
8084e7f640dSJohn Baldwin 		 * shared lock loop back and retry.
8094e7f640dSJohn Baldwin 		 */
8104e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8114e7f640dSJohn Baldwin 			MPASS(!(x & SX_LOCK_SHARED_WAITERS));
8124e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
8134e7f640dSJohn Baldwin 			    x + SX_ONE_SHARER)) {
8144e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8154e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
8164e7f640dSJohn Baldwin 					    "%s: %p succeed %p -> %p", __func__,
8174e7f640dSJohn Baldwin 					    sx, (void *)x,
8184e7f640dSJohn Baldwin 					    (void *)(x + SX_ONE_SHARER));
8194e7f640dSJohn Baldwin 				break;
8204e7f640dSJohn Baldwin 			}
8214e7f640dSJohn Baldwin 			continue;
8224e7f640dSJohn Baldwin 		}
823*f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
824*f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
825*f5f9340bSFabien Thomas #endif
826eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
827eea4f254SJeff Roberson 		    &waittime);
8284e7f640dSJohn Baldwin 
8294e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8304e7f640dSJohn Baldwin 		/*
8314e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8324e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8334e7f640dSJohn Baldwin 		 * changes.
8344e7f640dSJohn Baldwin 		 */
8351ae1c2a3SAttilio Rao 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8364e7f640dSJohn Baldwin 			x = SX_OWNER(x);
8374e7f640dSJohn Baldwin 			owner = (struct thread *)x;
8384e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8394e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8404e7f640dSJohn Baldwin 					CTR3(KTR_LOCK,
8414e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
8424e7f640dSJohn Baldwin 					    __func__, sx, owner);
8434e7f640dSJohn Baldwin 				GIANT_SAVE();
8444e7f640dSJohn Baldwin 				while (SX_OWNER(sx->sx_lock) == x &&
845a5aedd68SStacey Son 				    TD_IS_RUNNING(owner)) {
846a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
847a5aedd68SStacey Son 					spin_cnt++;
848a5aedd68SStacey Son #endif
8494e7f640dSJohn Baldwin 					cpu_spinwait();
850a5aedd68SStacey Son 				}
8514e7f640dSJohn Baldwin 				continue;
8524e7f640dSJohn Baldwin 			}
8534e7f640dSJohn Baldwin 		}
8544e7f640dSJohn Baldwin #endif
8554e7f640dSJohn Baldwin 
8564e7f640dSJohn Baldwin 		/*
8574e7f640dSJohn Baldwin 		 * Some other thread already has an exclusive lock, so
8584e7f640dSJohn Baldwin 		 * start the process of blocking.
8594e7f640dSJohn Baldwin 		 */
8604e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
8614e7f640dSJohn Baldwin 		x = sx->sx_lock;
8624e7f640dSJohn Baldwin 
8634e7f640dSJohn Baldwin 		/*
8644e7f640dSJohn Baldwin 		 * The lock could have been released while we spun.
8654e7f640dSJohn Baldwin 		 * In this case loop back and retry.
8664e7f640dSJohn Baldwin 		 */
8674e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8684e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
8694e7f640dSJohn Baldwin 			continue;
8704e7f640dSJohn Baldwin 		}
8714e7f640dSJohn Baldwin 
8724e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8734e7f640dSJohn Baldwin 		/*
8744e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8754e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8764e7f640dSJohn Baldwin 		 * changes.
8774e7f640dSJohn Baldwin 		 */
8784e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
8791ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8804e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
8814e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8824e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
8834e7f640dSJohn Baldwin 				continue;
8844e7f640dSJohn Baldwin 			}
8854e7f640dSJohn Baldwin 		}
8864e7f640dSJohn Baldwin #endif
8874e7f640dSJohn Baldwin 
8884e7f640dSJohn Baldwin 		/*
8894e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
8904e7f640dSJohn Baldwin 		 * fail to set it drop the sleep queue lock and loop
8914e7f640dSJohn Baldwin 		 * back.
8924e7f640dSJohn Baldwin 		 */
8934e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED_WAITERS)) {
8944e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
8954e7f640dSJohn Baldwin 			    x | SX_LOCK_SHARED_WAITERS)) {
8964e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
8974e7f640dSJohn Baldwin 				continue;
8984e7f640dSJohn Baldwin 			}
8994e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
9004e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
9014e7f640dSJohn Baldwin 				    __func__, sx);
9024e7f640dSJohn Baldwin 		}
9034e7f640dSJohn Baldwin 
9044e7f640dSJohn Baldwin 		/*
9054e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the shared lock,
9064e7f640dSJohn Baldwin 		 * we have to sleep.
9074e7f640dSJohn Baldwin 		 */
9084e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9094e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
9104e7f640dSJohn Baldwin 			    __func__, sx);
9114e7f640dSJohn Baldwin 
912a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
913a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
914a5aedd68SStacey Son #endif
9154e7f640dSJohn Baldwin 		GIANT_SAVE();
9164e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
917f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
918f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
919f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
920c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
921f9819486SAttilio Rao 		else
922c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
923a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
924a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
925a5aedd68SStacey Son 		sleep_cnt++;
926a5aedd68SStacey Son #endif
927f9819486SAttilio Rao 		if (error) {
928f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
929f9819486SAttilio Rao 				CTR2(KTR_LOCK,
930f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
931f9819486SAttilio Rao 				    __func__, sx);
932f9819486SAttilio Rao 			break;
933f9819486SAttilio Rao 		}
9344e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9354e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
9364e7f640dSJohn Baldwin 			    __func__, sx);
9374e7f640dSJohn Baldwin 	}
938eea4f254SJeff Roberson 	if (error == 0)
939a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
940a5aedd68SStacey Son 		    contested, waittime, file, line);
941a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
942a5aedd68SStacey Son 	if (sleep_time)
943a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
944a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
945a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
946a5aedd68SStacey Son #endif
9474e7f640dSJohn Baldwin 	GIANT_RESTORE();
948f9819486SAttilio Rao 	return (error);
9494e7f640dSJohn Baldwin }
9504e7f640dSJohn Baldwin 
9514e7f640dSJohn Baldwin /*
9524e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_sunlock
9534e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
9544e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
9554e7f640dSJohn Baldwin  * accessible from at least sx.h.
9564e7f640dSJohn Baldwin  */
9574e7f640dSJohn Baldwin void
9584e7f640dSJohn Baldwin _sx_sunlock_hard(struct sx *sx, const char *file, int line)
9594e7f640dSJohn Baldwin {
9604e7f640dSJohn Baldwin 	uintptr_t x;
961da7bbd2cSJohn Baldwin 	int wakeup_swapper;
9624e7f640dSJohn Baldwin 
96335370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
96435370593SAndriy Gapon 		return;
96535370593SAndriy Gapon 
9664e7f640dSJohn Baldwin 	for (;;) {
9674e7f640dSJohn Baldwin 		x = sx->sx_lock;
9684e7f640dSJohn Baldwin 
9694e7f640dSJohn Baldwin 		/*
9704e7f640dSJohn Baldwin 		 * We should never have sharers while at least one thread
9714e7f640dSJohn Baldwin 		 * holds a shared lock.
9724e7f640dSJohn Baldwin 		 */
9734e7f640dSJohn Baldwin 		KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
9744e7f640dSJohn Baldwin 		    ("%s: waiting sharers", __func__));
9754e7f640dSJohn Baldwin 
9764e7f640dSJohn Baldwin 		/*
9774e7f640dSJohn Baldwin 		 * See if there is more than one shared lock held.  If
9784e7f640dSJohn Baldwin 		 * so, just drop one and return.
9794e7f640dSJohn Baldwin 		 */
9804e7f640dSJohn Baldwin 		if (SX_SHARERS(x) > 1) {
981ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
9824e7f640dSJohn Baldwin 			    x - SX_ONE_SHARER)) {
9834e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
9844e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
9854e7f640dSJohn Baldwin 					    "%s: %p succeeded %p -> %p",
9864e7f640dSJohn Baldwin 					    __func__, sx, (void *)x,
9874e7f640dSJohn Baldwin 					    (void *)(x - SX_ONE_SHARER));
9884e7f640dSJohn Baldwin 				break;
9894e7f640dSJohn Baldwin 			}
9904e7f640dSJohn Baldwin 			continue;
9914e7f640dSJohn Baldwin 		}
9924e7f640dSJohn Baldwin 
9934e7f640dSJohn Baldwin 		/*
9944e7f640dSJohn Baldwin 		 * If there aren't any waiters for an exclusive lock,
9954e7f640dSJohn Baldwin 		 * then try to drop it quickly.
9964e7f640dSJohn Baldwin 		 */
9974e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
9984e7f640dSJohn Baldwin 			MPASS(x == SX_SHARERS_LOCK(1));
999ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1000ddce63caSAttilio Rao 			    SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
10014e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
10024e7f640dSJohn Baldwin 					CTR2(KTR_LOCK, "%s: %p last succeeded",
10034e7f640dSJohn Baldwin 					    __func__, sx);
10044e7f640dSJohn Baldwin 				break;
10054e7f640dSJohn Baldwin 			}
10064e7f640dSJohn Baldwin 			continue;
10074e7f640dSJohn Baldwin 		}
10084e7f640dSJohn Baldwin 
10094e7f640dSJohn Baldwin 		/*
10104e7f640dSJohn Baldwin 		 * At this point, there should just be one sharer with
10114e7f640dSJohn Baldwin 		 * exclusive waiters.
10124e7f640dSJohn Baldwin 		 */
10134e7f640dSJohn Baldwin 		MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
10144e7f640dSJohn Baldwin 
10154e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
10164e7f640dSJohn Baldwin 
10174e7f640dSJohn Baldwin 		/*
10184e7f640dSJohn Baldwin 		 * Wake up semantic here is quite simple:
10194e7f640dSJohn Baldwin 		 * Just wake up all the exclusive waiters.
10204e7f640dSJohn Baldwin 		 * Note that the state of the lock could have changed,
10214e7f640dSJohn Baldwin 		 * so if it fails loop back and retry.
10224e7f640dSJohn Baldwin 		 */
1023ddce63caSAttilio Rao 		if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
10244e7f640dSJohn Baldwin 		    SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
10254e7f640dSJohn Baldwin 		    SX_LOCK_UNLOCKED)) {
10264e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
10274e7f640dSJohn Baldwin 			continue;
10284e7f640dSJohn Baldwin 		}
10294e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
10304e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p waking up all thread on"
10314e7f640dSJohn Baldwin 			    "exclusive queue", __func__, sx);
1032da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1033da7bbd2cSJohn Baldwin 		    0, SQ_EXCLUSIVE_QUEUE);
1034c5aa6b58SJeff Roberson 		sleepq_release(&sx->lock_object);
1035da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
1036da7bbd2cSJohn Baldwin 			kick_proc0();
10374e7f640dSJohn Baldwin 		break;
10384e7f640dSJohn Baldwin 	}
1039d55229b7SJason Evans }
10404e5e677bSJohn Baldwin 
10414e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT
1042781a35dfSJohn Baldwin #ifndef INVARIANTS
1043781a35dfSJohn Baldwin #undef	_sx_assert
1044781a35dfSJohn Baldwin #endif
1045781a35dfSJohn Baldwin 
10464e5e677bSJohn Baldwin /*
10474e5e677bSJohn Baldwin  * In the non-WITNESS case, sx_assert() can only detect that at least
10484e5e677bSJohn Baldwin  * *some* thread owns an slock, but it cannot guarantee that *this*
10494e5e677bSJohn Baldwin  * thread owns an slock.
10504e5e677bSJohn Baldwin  */
10514e5e677bSJohn Baldwin void
1052d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line)
10534e5e677bSJohn Baldwin {
10544e7f640dSJohn Baldwin #ifndef WITNESS
10554e7f640dSJohn Baldwin 	int slocked = 0;
10564e7f640dSJohn Baldwin #endif
10574e5e677bSJohn Baldwin 
105803129ba9SJohn Baldwin 	if (panicstr != NULL)
105903129ba9SJohn Baldwin 		return;
10604e5e677bSJohn Baldwin 	switch (what) {
10617ec137e5SJohn Baldwin 	case SA_SLOCKED:
10627ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_NOTRECURSED:
10637ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_RECURSED:
10644e7f640dSJohn Baldwin #ifndef WITNESS
10654e7f640dSJohn Baldwin 		slocked = 1;
10664e7f640dSJohn Baldwin 		/* FALLTHROUGH */
10674e7f640dSJohn Baldwin #endif
10687ec137e5SJohn Baldwin 	case SA_LOCKED:
10697ec137e5SJohn Baldwin 	case SA_LOCKED | SA_NOTRECURSED:
10707ec137e5SJohn Baldwin 	case SA_LOCKED | SA_RECURSED:
10714e5e677bSJohn Baldwin #ifdef WITNESS
1072aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
10734e5e677bSJohn Baldwin #else
10744e7f640dSJohn Baldwin 		/*
10754e7f640dSJohn Baldwin 		 * If some other thread has an exclusive lock or we
10764e7f640dSJohn Baldwin 		 * have one and are asserting a shared lock, fail.
10774e7f640dSJohn Baldwin 		 * Also, if no one has a lock at all, fail.
10784e7f640dSJohn Baldwin 		 */
10794e7f640dSJohn Baldwin 		if (sx->sx_lock == SX_LOCK_UNLOCKED ||
10804e7f640dSJohn Baldwin 		    (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
10814e7f640dSJohn Baldwin 		    sx_xholder(sx) != curthread)))
108203129ba9SJohn Baldwin 			panic("Lock %s not %slocked @ %s:%d\n",
10834e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, slocked ? "share " : "",
10844e7f640dSJohn Baldwin 			    file, line);
10854e7f640dSJohn Baldwin 
10864e7f640dSJohn Baldwin 		if (!(sx->sx_lock & SX_LOCK_SHARED)) {
10874e7f640dSJohn Baldwin 			if (sx_recursed(sx)) {
10887ec137e5SJohn Baldwin 				if (what & SA_NOTRECURSED)
10894e7f640dSJohn Baldwin 					panic("Lock %s recursed @ %s:%d\n",
10904e7f640dSJohn Baldwin 					    sx->lock_object.lo_name, file,
10914e7f640dSJohn Baldwin 					    line);
10927ec137e5SJohn Baldwin 			} else if (what & SA_RECURSED)
10934e7f640dSJohn Baldwin 				panic("Lock %s not recursed @ %s:%d\n",
10944e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
10954e7f640dSJohn Baldwin 		}
10964e5e677bSJohn Baldwin #endif
10974e5e677bSJohn Baldwin 		break;
10987ec137e5SJohn Baldwin 	case SA_XLOCKED:
10997ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_NOTRECURSED:
11007ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_RECURSED:
11014e7f640dSJohn Baldwin 		if (sx_xholder(sx) != curthread)
110203129ba9SJohn Baldwin 			panic("Lock %s not exclusively locked @ %s:%d\n",
1103aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11044e7f640dSJohn Baldwin 		if (sx_recursed(sx)) {
11057ec137e5SJohn Baldwin 			if (what & SA_NOTRECURSED)
11064e7f640dSJohn Baldwin 				panic("Lock %s recursed @ %s:%d\n",
11074e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
11087ec137e5SJohn Baldwin 		} else if (what & SA_RECURSED)
11094e7f640dSJohn Baldwin 			panic("Lock %s not recursed @ %s:%d\n",
11104e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11114e5e677bSJohn Baldwin 		break;
11127ec137e5SJohn Baldwin 	case SA_UNLOCKED:
111319b0efd3SPawel Jakub Dawidek #ifdef WITNESS
1114aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
111519b0efd3SPawel Jakub Dawidek #else
1116f6739b1dSPawel Jakub Dawidek 		/*
11174e7f640dSJohn Baldwin 		 * If we hold an exclusve lock fail.  We can't
11184e7f640dSJohn Baldwin 		 * reliably check to see if we hold a shared lock or
11194e7f640dSJohn Baldwin 		 * not.
1120f6739b1dSPawel Jakub Dawidek 		 */
11214e7f640dSJohn Baldwin 		if (sx_xholder(sx) == curthread)
112203129ba9SJohn Baldwin 			panic("Lock %s exclusively locked @ %s:%d\n",
1123aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
112419b0efd3SPawel Jakub Dawidek #endif
112519b0efd3SPawel Jakub Dawidek 		break;
11264e5e677bSJohn Baldwin 	default:
11274e5e677bSJohn Baldwin 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
11284e5e677bSJohn Baldwin 		    line);
11294e5e677bSJohn Baldwin 	}
11304e5e677bSJohn Baldwin }
11314e5e677bSJohn Baldwin #endif	/* INVARIANT_SUPPORT */
1132d272fe53SJohn Baldwin 
1133d272fe53SJohn Baldwin #ifdef DDB
11344e7f640dSJohn Baldwin static void
1135d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock)
1136d272fe53SJohn Baldwin {
1137d272fe53SJohn Baldwin 	struct thread *td;
1138d576deedSPawel Jakub Dawidek 	const struct sx *sx;
1139d272fe53SJohn Baldwin 
1140d576deedSPawel Jakub Dawidek 	sx = (const struct sx *)lock;
1141d272fe53SJohn Baldwin 
1142d272fe53SJohn Baldwin 	db_printf(" state: ");
11434e7f640dSJohn Baldwin 	if (sx->sx_lock == SX_LOCK_UNLOCKED)
11444e7f640dSJohn Baldwin 		db_printf("UNLOCKED\n");
11450026c92cSJohn Baldwin 	else if (sx->sx_lock == SX_LOCK_DESTROYED) {
11460026c92cSJohn Baldwin 		db_printf("DESTROYED\n");
11470026c92cSJohn Baldwin 		return;
11480026c92cSJohn Baldwin 	} else if (sx->sx_lock & SX_LOCK_SHARED)
11494e7f640dSJohn Baldwin 		db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
11504e7f640dSJohn Baldwin 	else {
11514e7f640dSJohn Baldwin 		td = sx_xholder(sx);
1152d272fe53SJohn Baldwin 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1153431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
11544e7f640dSJohn Baldwin 		if (sx_recursed(sx))
11554e7f640dSJohn Baldwin 			db_printf(" recursed: %d\n", sx->sx_recurse);
11564e7f640dSJohn Baldwin 	}
11574e7f640dSJohn Baldwin 
11584e7f640dSJohn Baldwin 	db_printf(" waiters: ");
11594e7f640dSJohn Baldwin 	switch(sx->sx_lock &
11604e7f640dSJohn Baldwin 	    (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
11614e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS:
11624e7f640dSJohn Baldwin 		db_printf("shared\n");
11634e7f640dSJohn Baldwin 		break;
11644e7f640dSJohn Baldwin 	case SX_LOCK_EXCLUSIVE_WAITERS:
11654e7f640dSJohn Baldwin 		db_printf("exclusive\n");
11664e7f640dSJohn Baldwin 		break;
11674e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
11684e7f640dSJohn Baldwin 		db_printf("exclusive and shared\n");
11694e7f640dSJohn Baldwin 		break;
11704e7f640dSJohn Baldwin 	default:
11714e7f640dSJohn Baldwin 		db_printf("none\n");
11724e7f640dSJohn Baldwin 	}
1173d272fe53SJohn Baldwin }
1174462a7addSJohn Baldwin 
1175462a7addSJohn Baldwin /*
1176462a7addSJohn Baldwin  * Check to see if a thread that is blocked on a sleep queue is actually
1177462a7addSJohn Baldwin  * blocked on an sx lock.  If so, output some details and return true.
1178462a7addSJohn Baldwin  * If the lock has an exclusive owner, return that in *ownerp.
1179462a7addSJohn Baldwin  */
1180462a7addSJohn Baldwin int
1181462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp)
1182462a7addSJohn Baldwin {
1183462a7addSJohn Baldwin 	struct sx *sx;
1184462a7addSJohn Baldwin 
1185462a7addSJohn Baldwin 	/*
11864e7f640dSJohn Baldwin 	 * Check to see if this thread is blocked on an sx lock.
11874e7f640dSJohn Baldwin 	 * First, we check the lock class.  If that is ok, then we
11884e7f640dSJohn Baldwin 	 * compare the lock name against the wait message.
1189462a7addSJohn Baldwin 	 */
11904e7f640dSJohn Baldwin 	sx = td->td_wchan;
11914e7f640dSJohn Baldwin 	if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
11924e7f640dSJohn Baldwin 	    sx->lock_object.lo_name != td->td_wmesg)
1193462a7addSJohn Baldwin 		return (0);
1194462a7addSJohn Baldwin 
1195462a7addSJohn Baldwin 	/* We think we have an sx lock, so output some details. */
1196462a7addSJohn Baldwin 	db_printf("blocked on sx \"%s\" ", td->td_wmesg);
11974e7f640dSJohn Baldwin 	*ownerp = sx_xholder(sx);
11984e7f640dSJohn Baldwin 	if (sx->sx_lock & SX_LOCK_SHARED)
11994e7f640dSJohn Baldwin 		db_printf("SLOCK (count %ju)\n",
12004e7f640dSJohn Baldwin 		    (uintmax_t)SX_SHARERS(sx->sx_lock));
12014e7f640dSJohn Baldwin 	else
1202462a7addSJohn Baldwin 		db_printf("XLOCK\n");
1203462a7addSJohn Baldwin 	return (1);
1204462a7addSJohn Baldwin }
1205d272fe53SJohn Baldwin #endif
1206