xref: /freebsd/sys/kern/kern_sx.c (revision 2cba8dd301342c1c31f6dbf496e8351fa8359584)
19454b2d8SWarner Losh /*-
24e7f640dSJohn Baldwin  * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org>
34e7f640dSJohn Baldwin  * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
44e7f640dSJohn Baldwin  * All rights reserved.
56281b30aSJason Evans  *
66281b30aSJason Evans  * Redistribution and use in source and binary forms, with or without
76281b30aSJason Evans  * modification, are permitted provided that the following conditions
86281b30aSJason Evans  * are met:
96281b30aSJason Evans  * 1. Redistributions of source code must retain the above copyright
106281b30aSJason Evans  *    notice(s), this list of conditions and the following disclaimer as
116281b30aSJason Evans  *    the first lines of this file unmodified other than the possible
126281b30aSJason Evans  *    addition of one or more copyright notices.
136281b30aSJason Evans  * 2. Redistributions in binary form must reproduce the above copyright
146281b30aSJason Evans  *    notice(s), this list of conditions and the following disclaimer in the
156281b30aSJason Evans  *    documentation and/or other materials provided with the distribution.
166281b30aSJason Evans  *
176281b30aSJason Evans  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
186281b30aSJason Evans  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
196281b30aSJason Evans  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
206281b30aSJason Evans  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
216281b30aSJason Evans  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
226281b30aSJason Evans  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
236281b30aSJason Evans  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
246281b30aSJason Evans  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
256281b30aSJason Evans  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
266281b30aSJason Evans  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
276281b30aSJason Evans  * DAMAGE.
286281b30aSJason Evans  */
296281b30aSJason Evans 
306281b30aSJason Evans /*
314e7f640dSJohn Baldwin  * Shared/exclusive locks.  This implementation attempts to ensure
324e7f640dSJohn Baldwin  * deterministic lock granting behavior, so that slocks and xlocks are
334e7f640dSJohn Baldwin  * interleaved.
346281b30aSJason Evans  *
356281b30aSJason Evans  * Priority propagation will not generally raise the priority of lock holders,
366281b30aSJason Evans  * so should not be relied upon in combination with sx locks.
376281b30aSJason Evans  */
386281b30aSJason Evans 
394e7f640dSJohn Baldwin #include "opt_ddb.h"
40f5f9340bSFabien Thomas #include "opt_hwpmc_hooks.h"
41e31d0833SAttilio Rao #include "opt_no_adaptive_sx.h"
424e7f640dSJohn Baldwin 
43677b542eSDavid E. O'Brien #include <sys/cdefs.h>
44677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
45677b542eSDavid E. O'Brien 
466281b30aSJason Evans #include <sys/param.h>
477a7ce668SAndriy Gapon #include <sys/systm.h>
48cd2fe4e6SAttilio Rao #include <sys/kdb.h>
496281b30aSJason Evans #include <sys/ktr.h>
5019284646SJohn Baldwin #include <sys/lock.h>
516281b30aSJason Evans #include <sys/mutex.h>
52d272fe53SJohn Baldwin #include <sys/proc.h>
53*2cba8dd3SJohn Baldwin #include <sys/sched.h>
544e7f640dSJohn Baldwin #include <sys/sleepqueue.h>
556281b30aSJason Evans #include <sys/sx.h>
56e31d0833SAttilio Rao #include <sys/sysctl.h>
574e7f640dSJohn Baldwin 
58e31d0833SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
594e7f640dSJohn Baldwin #include <machine/cpu.h>
604e7f640dSJohn Baldwin #endif
616281b30aSJason Evans 
62462a7addSJohn Baldwin #ifdef DDB
63d272fe53SJohn Baldwin #include <ddb/ddb.h>
644e7f640dSJohn Baldwin #endif
65d272fe53SJohn Baldwin 
661ae1c2a3SAttilio Rao #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
671ae1c2a3SAttilio Rao #define	ADAPTIVE_SX
684e7f640dSJohn Baldwin #endif
694e7f640dSJohn Baldwin 
70f0830182SAttilio Rao CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE);
71c1a6d9faSAttilio Rao 
72f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
73f5f9340bSFabien Thomas #include <sys/pmckern.h>
74f5f9340bSFabien Thomas PMC_SOFT_DECLARE( , , lock, failed);
75f5f9340bSFabien Thomas #endif
76f5f9340bSFabien Thomas 
774e7f640dSJohn Baldwin /* Handy macros for sleep queues. */
784e7f640dSJohn Baldwin #define	SQ_EXCLUSIVE_QUEUE	0
794e7f640dSJohn Baldwin #define	SQ_SHARED_QUEUE		1
804e7f640dSJohn Baldwin 
814e7f640dSJohn Baldwin /*
824e7f640dSJohn Baldwin  * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file.  We
834e7f640dSJohn Baldwin  * drop Giant anytime we have to sleep or if we adaptively spin.
844e7f640dSJohn Baldwin  */
854e7f640dSJohn Baldwin #define	GIANT_DECLARE							\
864e7f640dSJohn Baldwin 	int _giantcnt = 0;						\
874e7f640dSJohn Baldwin 	WITNESS_SAVE_DECL(Giant)					\
884e7f640dSJohn Baldwin 
894e7f640dSJohn Baldwin #define	GIANT_SAVE() do {						\
904e7f640dSJohn Baldwin 	if (mtx_owned(&Giant)) {					\
914e7f640dSJohn Baldwin 		WITNESS_SAVE(&Giant.lock_object, Giant);		\
924e7f640dSJohn Baldwin 		while (mtx_owned(&Giant)) {				\
934e7f640dSJohn Baldwin 			_giantcnt++;					\
944e7f640dSJohn Baldwin 			mtx_unlock(&Giant);				\
954e7f640dSJohn Baldwin 		}							\
964e7f640dSJohn Baldwin 	}								\
974e7f640dSJohn Baldwin } while (0)
984e7f640dSJohn Baldwin 
994e7f640dSJohn Baldwin #define GIANT_RESTORE() do {						\
1004e7f640dSJohn Baldwin 	if (_giantcnt > 0) {						\
1014e7f640dSJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);			\
1024e7f640dSJohn Baldwin 		while (_giantcnt--)					\
1034e7f640dSJohn Baldwin 			mtx_lock(&Giant);				\
1044e7f640dSJohn Baldwin 		WITNESS_RESTORE(&Giant.lock_object, Giant);		\
1054e7f640dSJohn Baldwin 	}								\
1064e7f640dSJohn Baldwin } while (0)
1074e7f640dSJohn Baldwin 
1084e7f640dSJohn Baldwin /*
109da7d0d1eSJohn Baldwin  * Returns true if an exclusive lock is recursed.  It assumes
110da7d0d1eSJohn Baldwin  * curthread currently has an exclusive lock.
1114e7f640dSJohn Baldwin  */
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
1187faf4d90SDavide Italiano static void	lock_sx(struct lock_object *lock, uintptr_t how);
119a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
120d576deedSPawel Jakub Dawidek static int	owner_sx(const struct lock_object *lock, struct thread **owner);
121a5aedd68SStacey Son #endif
1227faf4d90SDavide Italiano static uintptr_t 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
1587faf4d90SDavide Italiano lock_sx(struct lock_object *lock, uintptr_t how)
1596e21afd4SJohn Baldwin {
1606e21afd4SJohn Baldwin 	struct sx *sx;
1616e21afd4SJohn Baldwin 
1626e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1636e21afd4SJohn Baldwin 	if (how)
1646e21afd4SJohn Baldwin 		sx_slock(sx);
165cf6b879fSDavide Italiano 	else
166cf6b879fSDavide Italiano 		sx_xlock(sx);
1676e21afd4SJohn Baldwin }
1686e21afd4SJohn Baldwin 
1697faf4d90SDavide Italiano uintptr_t
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);
178cf6b879fSDavide Italiano 		return (0);
1796e21afd4SJohn Baldwin 	} else {
1806e21afd4SJohn Baldwin 		sx_sunlock(sx);
181cf6b879fSDavide Italiano 		return (1);
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;
230b5fb43e5SJohn Baldwin 	lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
2314e7f640dSJohn Baldwin 	sx->sx_lock = SX_LOCK_UNLOCKED;
2324e7f640dSJohn Baldwin 	sx->sx_recurse = 0;
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);
252cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
253e3ae0dfeSAttilio Rao 	    ("sx_slock() by idle thread %p on sx %s @ %s:%d",
254e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
2550026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
2560026c92cSJohn Baldwin 	    ("sx_slock() of destroyed sx @ %s:%d", file, line));
25741313430SJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
258f9819486SAttilio Rao 	error = __sx_slock(sx, opts, file, line);
259f9819486SAttilio Rao 	if (!error) {
260aa89d8cdSJohn Baldwin 		LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
261aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, 0, file, line);
262764e4d54SJohn Baldwin 		curthread->td_locks++;
2636281b30aSJason Evans 	}
2646281b30aSJason Evans 
265f9819486SAttilio Rao 	return (error);
266f9819486SAttilio Rao }
267f9819486SAttilio Rao 
2685f36700aSJohn Baldwin int
2699fde98bbSAttilio Rao sx_try_slock_(struct sx *sx, const char *file, int line)
2705f36700aSJohn Baldwin {
2714e7f640dSJohn Baldwin 	uintptr_t x;
2725f36700aSJohn Baldwin 
27335370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
27435370593SAndriy Gapon 		return (1);
27535370593SAndriy Gapon 
276cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
277e3ae0dfeSAttilio Rao 	    ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
278e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
279e3ae0dfeSAttilio Rao 
280764a938bSPawel Jakub Dawidek 	for (;;) {
2814e7f640dSJohn Baldwin 		x = sx->sx_lock;
2820026c92cSJohn Baldwin 		KASSERT(x != SX_LOCK_DESTROYED,
2830026c92cSJohn Baldwin 		    ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
284764a938bSPawel Jakub Dawidek 		if (!(x & SX_LOCK_SHARED))
285764a938bSPawel Jakub Dawidek 			break;
286764a938bSPawel Jakub Dawidek 		if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
287aa89d8cdSJohn Baldwin 			LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
288aa89d8cdSJohn Baldwin 			WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
289764e4d54SJohn Baldwin 			curthread->td_locks++;
2905f36700aSJohn Baldwin 			return (1);
2915f36700aSJohn Baldwin 		}
292764a938bSPawel Jakub Dawidek 	}
2934e7f640dSJohn Baldwin 
2944e7f640dSJohn Baldwin 	LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
2954e7f640dSJohn Baldwin 	return (0);
2965f36700aSJohn Baldwin }
2975f36700aSJohn Baldwin 
298f9819486SAttilio Rao int
299f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line)
3006281b30aSJason Evans {
301f9819486SAttilio Rao 	int error = 0;
3026281b30aSJason Evans 
30335370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
30435370593SAndriy Gapon 		return (0);
305cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
306e3ae0dfeSAttilio Rao 	    ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
307e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
3080026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3090026c92cSJohn Baldwin 	    ("sx_xlock() of destroyed sx @ %s:%d", file, line));
310aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
31141313430SJohn Baldwin 	    line, NULL);
312f9819486SAttilio Rao 	error = __sx_xlock(sx, curthread, opts, file, line);
313f9819486SAttilio Rao 	if (!error) {
314f9819486SAttilio Rao 		LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
315f9819486SAttilio Rao 		    file, line);
316aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
317764e4d54SJohn Baldwin 		curthread->td_locks++;
3186281b30aSJason Evans 	}
3196281b30aSJason Evans 
320f9819486SAttilio Rao 	return (error);
321f9819486SAttilio Rao }
322f9819486SAttilio Rao 
3235f36700aSJohn Baldwin int
3249fde98bbSAttilio Rao sx_try_xlock_(struct sx *sx, const char *file, int line)
3255f36700aSJohn Baldwin {
3264e7f640dSJohn Baldwin 	int rval;
3275f36700aSJohn Baldwin 
32835370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
32935370593SAndriy Gapon 		return (1);
33035370593SAndriy Gapon 
331cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
332e3ae0dfeSAttilio Rao 	    ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
333e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
3340026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3350026c92cSJohn Baldwin 	    ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
3364e7f640dSJohn Baldwin 
337f0830182SAttilio Rao 	if (sx_xlocked(sx) &&
338f0830182SAttilio Rao 	    (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
3394e7f640dSJohn Baldwin 		sx->sx_recurse++;
3404e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
3414e7f640dSJohn Baldwin 		rval = 1;
3424e7f640dSJohn Baldwin 	} else
3434e7f640dSJohn Baldwin 		rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
3444e7f640dSJohn Baldwin 		    (uintptr_t)curthread);
3454e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
3464e7f640dSJohn Baldwin 	if (rval) {
3474e7f640dSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
3484e7f640dSJohn Baldwin 		    file, line);
349764e4d54SJohn Baldwin 		curthread->td_locks++;
3505f36700aSJohn Baldwin 	}
3514e7f640dSJohn Baldwin 
3524e7f640dSJohn Baldwin 	return (rval);
3535f36700aSJohn Baldwin }
3545f36700aSJohn Baldwin 
3556281b30aSJason Evans void
35619284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line)
3576281b30aSJason Evans {
3586281b30aSJason Evans 
35935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
36035370593SAndriy Gapon 		return;
3610026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3620026c92cSJohn Baldwin 	    ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
3637ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
364aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
365aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
3664e7f640dSJohn Baldwin 	__sx_sunlock(sx, file, line);
367b5fb43e5SJohn Baldwin 	curthread->td_locks--;
3686281b30aSJason Evans }
3696281b30aSJason Evans 
3706281b30aSJason Evans void
37119284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line)
3726281b30aSJason Evans {
3736281b30aSJason Evans 
37435370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
37535370593SAndriy Gapon 		return;
3760026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3770026c92cSJohn Baldwin 	    ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
3787ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED, file, line);
379aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
3804e7f640dSJohn Baldwin 	LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
3814e7f640dSJohn Baldwin 	    line);
3824e7f640dSJohn Baldwin 	__sx_xunlock(sx, curthread, file, line);
383b5fb43e5SJohn Baldwin 	curthread->td_locks--;
3846281b30aSJason Evans }
385d55229b7SJason Evans 
3864e7f640dSJohn Baldwin /*
3874e7f640dSJohn Baldwin  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
3884e7f640dSJohn Baldwin  * This will only succeed if this thread holds a single shared lock.
3894e7f640dSJohn Baldwin  * Return 1 if if the upgrade succeed, 0 otherwise.
3904e7f640dSJohn Baldwin  */
391d55229b7SJason Evans int
3929fde98bbSAttilio Rao sx_try_upgrade_(struct sx *sx, const char *file, int line)
393d55229b7SJason Evans {
3944e7f640dSJohn Baldwin 	uintptr_t x;
3954e7f640dSJohn Baldwin 	int success;
396d55229b7SJason Evans 
39735370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
39835370593SAndriy Gapon 		return (1);
39935370593SAndriy Gapon 
4000026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4010026c92cSJohn Baldwin 	    ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
4027ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
403d55229b7SJason Evans 
4044e7f640dSJohn Baldwin 	/*
4054e7f640dSJohn Baldwin 	 * Try to switch from one shared lock to an exclusive lock.  We need
4064e7f640dSJohn Baldwin 	 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
4074e7f640dSJohn Baldwin 	 * we will wake up the exclusive waiters when we drop the lock.
4084e7f640dSJohn Baldwin 	 */
4094e7f640dSJohn Baldwin 	x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
4104e7f640dSJohn Baldwin 	success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
4114e7f640dSJohn Baldwin 	    (uintptr_t)curthread | x);
4124e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
413a5aedd68SStacey Son 	if (success) {
414aa89d8cdSJohn Baldwin 		WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
415b0b7cb50SJohn Baldwin 		    file, line);
416a5aedd68SStacey Son 		LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
417a5aedd68SStacey Son 	}
4184e7f640dSJohn Baldwin 	return (success);
419d55229b7SJason Evans }
420d55229b7SJason Evans 
4214e7f640dSJohn Baldwin /*
4224e7f640dSJohn Baldwin  * Downgrade an unrecursed exclusive lock into a single shared lock.
4234e7f640dSJohn Baldwin  */
424d55229b7SJason Evans void
4259fde98bbSAttilio Rao sx_downgrade_(struct sx *sx, const char *file, int line)
426d55229b7SJason Evans {
4274e7f640dSJohn Baldwin 	uintptr_t x;
428da7bbd2cSJohn Baldwin 	int wakeup_swapper;
429d55229b7SJason Evans 
43035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
43135370593SAndriy Gapon 		return;
43235370593SAndriy Gapon 
4330026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4340026c92cSJohn Baldwin 	    ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
4357ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
4364e7f640dSJohn Baldwin #ifndef INVARIANTS
4374e7f640dSJohn Baldwin 	if (sx_recursed(sx))
4384e7f640dSJohn Baldwin 		panic("downgrade of a recursed lock");
4394e7f640dSJohn Baldwin #endif
440d55229b7SJason Evans 
441aa89d8cdSJohn Baldwin 	WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
442d55229b7SJason Evans 
4434e7f640dSJohn Baldwin 	/*
4444e7f640dSJohn Baldwin 	 * Try to switch from an exclusive lock with no shared waiters
4454e7f640dSJohn Baldwin 	 * to one sharer with no shared waiters.  If there are
4464e7f640dSJohn Baldwin 	 * exclusive waiters, we don't need to lock the sleep queue so
4474e7f640dSJohn Baldwin 	 * long as we preserve the flag.  We do one quick try and if
4484e7f640dSJohn Baldwin 	 * that fails we grab the sleepq lock to keep the flags from
4494e7f640dSJohn Baldwin 	 * changing and do it the slow way.
4504e7f640dSJohn Baldwin 	 *
4514e7f640dSJohn Baldwin 	 * We have to lock the sleep queue if there are shared waiters
4524e7f640dSJohn Baldwin 	 * so we can wake them up.
4534e7f640dSJohn Baldwin 	 */
4544e7f640dSJohn Baldwin 	x = sx->sx_lock;
4554e7f640dSJohn Baldwin 	if (!(x & SX_LOCK_SHARED_WAITERS) &&
4564e7f640dSJohn Baldwin 	    atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
4574e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
4584e7f640dSJohn Baldwin 		LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
4594e7f640dSJohn Baldwin 		return;
4604e7f640dSJohn Baldwin 	}
4614e7f640dSJohn Baldwin 
4624e7f640dSJohn Baldwin 	/*
4634e7f640dSJohn Baldwin 	 * Lock the sleep queue so we can read the waiters bits
4644e7f640dSJohn Baldwin 	 * without any races and wakeup any shared waiters.
4654e7f640dSJohn Baldwin 	 */
4664e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
4674e7f640dSJohn Baldwin 
4684e7f640dSJohn Baldwin 	/*
4694e7f640dSJohn Baldwin 	 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
4704e7f640dSJohn Baldwin 	 * shared lock.  If there are any shared waiters, wake them up.
4714e7f640dSJohn Baldwin 	 */
472da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
4734e7f640dSJohn Baldwin 	x = sx->sx_lock;
4744e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
4754e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS));
4764e7f640dSJohn Baldwin 	if (x & SX_LOCK_SHARED_WAITERS)
477da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
478da7bbd2cSJohn Baldwin 		    0, SQ_SHARED_QUEUE);
4794e7f640dSJohn Baldwin 	sleepq_release(&sx->lock_object);
480d55229b7SJason Evans 
481aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
482a5aedd68SStacey Son 	LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
483da7bbd2cSJohn Baldwin 
484da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
485da7bbd2cSJohn Baldwin 		kick_proc0();
4864e7f640dSJohn Baldwin }
487d55229b7SJason Evans 
4884e7f640dSJohn Baldwin /*
4894e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xlock
4904e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
4914e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
4924e7f640dSJohn Baldwin  * accessible from at least sx.h.
4934e7f640dSJohn Baldwin  */
494f9819486SAttilio Rao int
495f9819486SAttilio Rao _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
496f9819486SAttilio Rao     int line)
4974e7f640dSJohn Baldwin {
4984e7f640dSJohn Baldwin 	GIANT_DECLARE;
4994e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5004e7f640dSJohn Baldwin 	volatile struct thread *owner;
5011ae1c2a3SAttilio Rao 	u_int i, spintries = 0;
5024e7f640dSJohn Baldwin #endif
5034e7f640dSJohn Baldwin 	uintptr_t x;
5041723a064SJeff Roberson #ifdef LOCK_PROFILING
5051723a064SJeff Roberson 	uint64_t waittime = 0;
5061723a064SJeff Roberson 	int contested = 0;
5071723a064SJeff Roberson #endif
5081723a064SJeff Roberson 	int error = 0;
509a5aedd68SStacey Son #ifdef	KDTRACE_HOOKS
510a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
511a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
512a5aedd68SStacey Son 	int64_t sleep_time = 0;
513a5aedd68SStacey Son #endif
5144e7f640dSJohn Baldwin 
51535370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
51635370593SAndriy Gapon 		return (0);
51735370593SAndriy Gapon 
5184e7f640dSJohn Baldwin 	/* If we already hold an exclusive lock, then recurse. */
5194e7f640dSJohn Baldwin 	if (sx_xlocked(sx)) {
520f0830182SAttilio Rao 		KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
521b0d67325SJohn Baldwin 	    ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
522b0d67325SJohn Baldwin 		    sx->lock_object.lo_name, file, line));
5234e7f640dSJohn Baldwin 		sx->sx_recurse++;
5244e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
5254e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
5264e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
527f9819486SAttilio Rao 		return (0);
5284e7f640dSJohn Baldwin 	}
5294e7f640dSJohn Baldwin 
5304e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
5314e7f640dSJohn Baldwin 		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
5324e7f640dSJohn Baldwin 		    sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
5334e7f640dSJohn Baldwin 
5344e7f640dSJohn Baldwin 	while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
535a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
536a5aedd68SStacey Son 		spin_cnt++;
537a5aedd68SStacey Son #endif
538f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
539f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
540f5f9340bSFabien Thomas #endif
541eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
542eea4f254SJeff Roberson 		    &waittime);
5434e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5444e7f640dSJohn Baldwin 		/*
5454e7f640dSJohn Baldwin 		 * If the lock is write locked and the owner is
5464e7f640dSJohn Baldwin 		 * running on another CPU, spin until the owner stops
5474e7f640dSJohn Baldwin 		 * running or the state of the lock changes.
5484e7f640dSJohn Baldwin 		 */
5494e7f640dSJohn Baldwin 		x = sx->sx_lock;
5508545538bSJohn Baldwin 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
5511ae1c2a3SAttilio Rao 			if ((x & SX_LOCK_SHARED) == 0) {
5524e7f640dSJohn Baldwin 				x = SX_OWNER(x);
5534e7f640dSJohn Baldwin 				owner = (struct thread *)x;
5544e7f640dSJohn Baldwin 				if (TD_IS_RUNNING(owner)) {
5554e7f640dSJohn Baldwin 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5564e7f640dSJohn Baldwin 						CTR3(KTR_LOCK,
5574e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
5584e7f640dSJohn Baldwin 						    __func__, sx, owner);
559*2cba8dd3SJohn Baldwin 					KTR_STATE1(KTR_SCHED, "thread",
560*2cba8dd3SJohn Baldwin 					    sched_tdname(curthread), "spinning",
561*2cba8dd3SJohn Baldwin 					    "lockname:\"%s\"",
562*2cba8dd3SJohn Baldwin 					    sx->lock_object.lo_name);
5634e7f640dSJohn Baldwin 					GIANT_SAVE();
5644e7f640dSJohn Baldwin 					while (SX_OWNER(sx->sx_lock) == x &&
565a5aedd68SStacey Son 					    TD_IS_RUNNING(owner)) {
5664e7f640dSJohn Baldwin 						cpu_spinwait();
567a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
568a5aedd68SStacey Son 						spin_cnt++;
569a5aedd68SStacey Son #endif
570a5aedd68SStacey Son 					}
571*2cba8dd3SJohn Baldwin 					KTR_STATE0(KTR_SCHED, "thread",
572*2cba8dd3SJohn Baldwin 					    sched_tdname(curthread), "running");
5734e7f640dSJohn Baldwin 					continue;
5744e7f640dSJohn Baldwin 				}
5751ae1c2a3SAttilio Rao 			} else if (SX_SHARERS(x) && spintries < asx_retries) {
576*2cba8dd3SJohn Baldwin 				KTR_STATE1(KTR_SCHED, "thread",
577*2cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "spinning",
578*2cba8dd3SJohn Baldwin 				    "lockname:\"%s\"", sx->lock_object.lo_name);
5798d3635c4SAttilio Rao 				GIANT_SAVE();
5801ae1c2a3SAttilio Rao 				spintries++;
5811ae1c2a3SAttilio Rao 				for (i = 0; i < asx_loops; i++) {
5821ae1c2a3SAttilio Rao 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5831ae1c2a3SAttilio Rao 						CTR4(KTR_LOCK,
5841ae1c2a3SAttilio Rao 				    "%s: shared spinning on %p with %u and %u",
5851ae1c2a3SAttilio Rao 						    __func__, sx, spintries, i);
5861ae1c2a3SAttilio Rao 					x = sx->sx_lock;
5871ae1c2a3SAttilio Rao 					if ((x & SX_LOCK_SHARED) == 0 ||
5881ae1c2a3SAttilio Rao 					    SX_SHARERS(x) == 0)
5891ae1c2a3SAttilio Rao 						break;
5901ae1c2a3SAttilio Rao 					cpu_spinwait();
5911ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS
5921ae1c2a3SAttilio Rao 					spin_cnt++;
5931ae1c2a3SAttilio Rao #endif
5941ae1c2a3SAttilio Rao 				}
595*2cba8dd3SJohn Baldwin 				KTR_STATE0(KTR_SCHED, "thread",
596*2cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "running");
5971ae1c2a3SAttilio Rao 				if (i != asx_loops)
5981ae1c2a3SAttilio Rao 					continue;
5991ae1c2a3SAttilio Rao 			}
6004e7f640dSJohn Baldwin 		}
6014e7f640dSJohn Baldwin #endif
6024e7f640dSJohn Baldwin 
6034e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
6044e7f640dSJohn Baldwin 		x = sx->sx_lock;
6054e7f640dSJohn Baldwin 
6064e7f640dSJohn Baldwin 		/*
6074e7f640dSJohn Baldwin 		 * If the lock was released while spinning on the
6084e7f640dSJohn Baldwin 		 * sleep queue chain lock, try again.
6094e7f640dSJohn Baldwin 		 */
6104e7f640dSJohn Baldwin 		if (x == SX_LOCK_UNLOCKED) {
6114e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6124e7f640dSJohn Baldwin 			continue;
6134e7f640dSJohn Baldwin 		}
6144e7f640dSJohn Baldwin 
6154e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
6164e7f640dSJohn Baldwin 		/*
6174e7f640dSJohn Baldwin 		 * The current lock owner might have started executing
6184e7f640dSJohn Baldwin 		 * on another CPU (or the lock could have changed
6194e7f640dSJohn Baldwin 		 * owners) while we were waiting on the sleep queue
6204e7f640dSJohn Baldwin 		 * chain lock.  If so, drop the sleep queue lock and try
6214e7f640dSJohn Baldwin 		 * again.
6224e7f640dSJohn Baldwin 		 */
6234e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
6241ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
6254e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
6264e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
6274e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6284e7f640dSJohn Baldwin 				continue;
6294e7f640dSJohn Baldwin 			}
6304e7f640dSJohn Baldwin 		}
6314e7f640dSJohn Baldwin #endif
6324e7f640dSJohn Baldwin 
6334e7f640dSJohn Baldwin 		/*
6344e7f640dSJohn Baldwin 		 * If an exclusive lock was released with both shared
6354e7f640dSJohn Baldwin 		 * and exclusive waiters and a shared waiter hasn't
6364e7f640dSJohn Baldwin 		 * woken up and acquired the lock yet, sx_lock will be
6374e7f640dSJohn Baldwin 		 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
6384e7f640dSJohn Baldwin 		 * If we see that value, try to acquire it once.  Note
6394e7f640dSJohn Baldwin 		 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
6404e7f640dSJohn Baldwin 		 * as there are other exclusive waiters still.  If we
6414e7f640dSJohn Baldwin 		 * fail, restart the loop.
6424e7f640dSJohn Baldwin 		 */
6434e7f640dSJohn Baldwin 		if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
6444e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock,
6454e7f640dSJohn Baldwin 			    SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
6464e7f640dSJohn Baldwin 			    tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
6474e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6484e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
6494e7f640dSJohn Baldwin 				    __func__, sx);
6504e7f640dSJohn Baldwin 				break;
6514e7f640dSJohn Baldwin 			}
6524e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6534e7f640dSJohn Baldwin 			continue;
6544e7f640dSJohn Baldwin 		}
6554e7f640dSJohn Baldwin 
6564e7f640dSJohn Baldwin 		/*
6574e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
6584e7f640dSJohn Baldwin 		 * than loop back and retry.
6594e7f640dSJohn Baldwin 		 */
6604e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
6614e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
6624e7f640dSJohn Baldwin 			    x | SX_LOCK_EXCLUSIVE_WAITERS)) {
6634e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6644e7f640dSJohn Baldwin 				continue;
6654e7f640dSJohn Baldwin 			}
6664e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
6674e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
6684e7f640dSJohn Baldwin 				    __func__, sx);
6694e7f640dSJohn Baldwin 		}
6704e7f640dSJohn Baldwin 
6714e7f640dSJohn Baldwin 		/*
6724e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the exclusive
6734e7f640dSJohn Baldwin 		 * lock and the exclusive waiters flag is set, we have
6744e7f640dSJohn Baldwin 		 * to sleep.
6754e7f640dSJohn Baldwin 		 */
6764e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6774e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
6784e7f640dSJohn Baldwin 			    __func__, sx);
6794e7f640dSJohn Baldwin 
680a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
681a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
682a5aedd68SStacey Son #endif
6834e7f640dSJohn Baldwin 		GIANT_SAVE();
6844e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
685f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
686f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
687f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
688c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
689f9819486SAttilio Rao 		else
690c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
691a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
692a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
693a5aedd68SStacey Son 		sleep_cnt++;
694a5aedd68SStacey Son #endif
695f9819486SAttilio Rao 		if (error) {
696f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
697f9819486SAttilio Rao 				CTR2(KTR_LOCK,
698f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
699f9819486SAttilio Rao 				    __func__, sx);
700f9819486SAttilio Rao 			break;
701f9819486SAttilio Rao 		}
7024e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7034e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
7044e7f640dSJohn Baldwin 			    __func__, sx);
7054e7f640dSJohn Baldwin 	}
7064e7f640dSJohn Baldwin 
7074e7f640dSJohn Baldwin 	GIANT_RESTORE();
708f9819486SAttilio Rao 	if (!error)
709a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
710a5aedd68SStacey Son 		    contested, waittime, file, line);
711a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
712a5aedd68SStacey Son 	if (sleep_time)
713a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
714a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
715a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
716a5aedd68SStacey Son #endif
717f9819486SAttilio Rao 	return (error);
7184e7f640dSJohn Baldwin }
7194e7f640dSJohn Baldwin 
7204e7f640dSJohn Baldwin /*
7214e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xunlock
7224e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7234e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7244e7f640dSJohn Baldwin  * accessible from at least sx.h.
7254e7f640dSJohn Baldwin  */
7264e7f640dSJohn Baldwin void
7274e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
7284e7f640dSJohn Baldwin {
7294e7f640dSJohn Baldwin 	uintptr_t x;
730da7bbd2cSJohn Baldwin 	int queue, wakeup_swapper;
7314e7f640dSJohn Baldwin 
73235370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
73335370593SAndriy Gapon 		return;
73435370593SAndriy Gapon 
7354e7f640dSJohn Baldwin 	MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
7364e7f640dSJohn Baldwin 
7374e7f640dSJohn Baldwin 	/* If the lock is recursed, then unrecurse one level. */
7384e7f640dSJohn Baldwin 	if (sx_xlocked(sx) && sx_recursed(sx)) {
7394e7f640dSJohn Baldwin 		if ((--sx->sx_recurse) == 0)
7404e7f640dSJohn Baldwin 			atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
7414e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7424e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
7434e7f640dSJohn Baldwin 		return;
7444e7f640dSJohn Baldwin 	}
7454e7f640dSJohn Baldwin 	MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
7464e7f640dSJohn Baldwin 	    SX_LOCK_EXCLUSIVE_WAITERS));
7474e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7484e7f640dSJohn Baldwin 		CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
7494e7f640dSJohn Baldwin 
7504e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
7514e7f640dSJohn Baldwin 	x = SX_LOCK_UNLOCKED;
7524e7f640dSJohn Baldwin 
7534e7f640dSJohn Baldwin 	/*
7544e7f640dSJohn Baldwin 	 * The wake up algorithm here is quite simple and probably not
7554e7f640dSJohn Baldwin 	 * ideal.  It gives precedence to shared waiters if they are
7564e7f640dSJohn Baldwin 	 * present.  For this condition, we have to preserve the
7574e7f640dSJohn Baldwin 	 * state of the exclusive waiters flag.
7582028867dSAttilio Rao 	 * If interruptible sleeps left the shared queue empty avoid a
7592028867dSAttilio Rao 	 * starvation for the threads sleeping on the exclusive queue by giving
7602028867dSAttilio Rao 	 * them precedence and cleaning up the shared waiters bit anyway.
7614e7f640dSJohn Baldwin 	 */
7622028867dSAttilio Rao 	if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
7632028867dSAttilio Rao 	    sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
7644e7f640dSJohn Baldwin 		queue = SQ_SHARED_QUEUE;
7654e7f640dSJohn Baldwin 		x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
7664e7f640dSJohn Baldwin 	} else
7674e7f640dSJohn Baldwin 		queue = SQ_EXCLUSIVE_QUEUE;
7684e7f640dSJohn Baldwin 
7694e7f640dSJohn Baldwin 	/* Wake up all the waiters for the specific queue. */
7704e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7714e7f640dSJohn Baldwin 		CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
7724e7f640dSJohn Baldwin 		    __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
7734e7f640dSJohn Baldwin 		    "exclusive");
7744e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, x);
775da7bbd2cSJohn Baldwin 	wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
776da7bbd2cSJohn Baldwin 	    queue);
777c5aa6b58SJeff Roberson 	sleepq_release(&sx->lock_object);
778da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
779da7bbd2cSJohn Baldwin 		kick_proc0();
7804e7f640dSJohn Baldwin }
7814e7f640dSJohn Baldwin 
7824e7f640dSJohn Baldwin /*
7834e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_slock
7844e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7854e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7864e7f640dSJohn Baldwin  * accessible from at least sx.h.
7874e7f640dSJohn Baldwin  */
788f9819486SAttilio Rao int
789f9819486SAttilio Rao _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
7904e7f640dSJohn Baldwin {
7914e7f640dSJohn Baldwin 	GIANT_DECLARE;
7924e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
7934e7f640dSJohn Baldwin 	volatile struct thread *owner;
7944e7f640dSJohn Baldwin #endif
7951723a064SJeff Roberson #ifdef LOCK_PROFILING
796c1a6d9faSAttilio Rao 	uint64_t waittime = 0;
797c1a6d9faSAttilio Rao 	int contested = 0;
7981723a064SJeff Roberson #endif
7994e7f640dSJohn Baldwin 	uintptr_t x;
800c1a6d9faSAttilio Rao 	int error = 0;
801a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
802a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
803a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
804a5aedd68SStacey Son 	int64_t sleep_time = 0;
805a5aedd68SStacey Son #endif
806c1a6d9faSAttilio Rao 
80735370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
80835370593SAndriy Gapon 		return (0);
80935370593SAndriy Gapon 
8104e7f640dSJohn Baldwin 	/*
8114e7f640dSJohn Baldwin 	 * As with rwlocks, we don't make any attempt to try to block
8124e7f640dSJohn Baldwin 	 * shared locks once there is an exclusive waiter.
8134e7f640dSJohn Baldwin 	 */
8144e7f640dSJohn Baldwin 	for (;;) {
815a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
816a5aedd68SStacey Son 		spin_cnt++;
817a5aedd68SStacey Son #endif
8184e7f640dSJohn Baldwin 		x = sx->sx_lock;
8194e7f640dSJohn Baldwin 
8204e7f640dSJohn Baldwin 		/*
8214e7f640dSJohn Baldwin 		 * If no other thread has an exclusive lock then try to bump up
8224e7f640dSJohn Baldwin 		 * the count of sharers.  Since we have to preserve the state
8234e7f640dSJohn Baldwin 		 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
8244e7f640dSJohn Baldwin 		 * shared lock loop back and retry.
8254e7f640dSJohn Baldwin 		 */
8264e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8274e7f640dSJohn Baldwin 			MPASS(!(x & SX_LOCK_SHARED_WAITERS));
8284e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
8294e7f640dSJohn Baldwin 			    x + SX_ONE_SHARER)) {
8304e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8314e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
8324e7f640dSJohn Baldwin 					    "%s: %p succeed %p -> %p", __func__,
8334e7f640dSJohn Baldwin 					    sx, (void *)x,
8344e7f640dSJohn Baldwin 					    (void *)(x + SX_ONE_SHARER));
8354e7f640dSJohn Baldwin 				break;
8364e7f640dSJohn Baldwin 			}
8374e7f640dSJohn Baldwin 			continue;
8384e7f640dSJohn Baldwin 		}
839f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
840f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
841f5f9340bSFabien Thomas #endif
842eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
843eea4f254SJeff Roberson 		    &waittime);
8444e7f640dSJohn Baldwin 
8454e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8464e7f640dSJohn Baldwin 		/*
8474e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8484e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8494e7f640dSJohn Baldwin 		 * changes.
8504e7f640dSJohn Baldwin 		 */
8511ae1c2a3SAttilio Rao 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8524e7f640dSJohn Baldwin 			x = SX_OWNER(x);
8534e7f640dSJohn Baldwin 			owner = (struct thread *)x;
8544e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8554e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8564e7f640dSJohn Baldwin 					CTR3(KTR_LOCK,
8574e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
8584e7f640dSJohn Baldwin 					    __func__, sx, owner);
859*2cba8dd3SJohn Baldwin 				KTR_STATE1(KTR_SCHED, "thread",
860*2cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "spinning",
861*2cba8dd3SJohn Baldwin 				    "lockname:\"%s\"", sx->lock_object.lo_name);
8624e7f640dSJohn Baldwin 				GIANT_SAVE();
8634e7f640dSJohn Baldwin 				while (SX_OWNER(sx->sx_lock) == x &&
864a5aedd68SStacey Son 				    TD_IS_RUNNING(owner)) {
865a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
866a5aedd68SStacey Son 					spin_cnt++;
867a5aedd68SStacey Son #endif
8684e7f640dSJohn Baldwin 					cpu_spinwait();
869a5aedd68SStacey Son 				}
870*2cba8dd3SJohn Baldwin 				KTR_STATE0(KTR_SCHED, "thread",
871*2cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "running");
8724e7f640dSJohn Baldwin 				continue;
8734e7f640dSJohn Baldwin 			}
8744e7f640dSJohn Baldwin 		}
8754e7f640dSJohn Baldwin #endif
8764e7f640dSJohn Baldwin 
8774e7f640dSJohn Baldwin 		/*
8784e7f640dSJohn Baldwin 		 * Some other thread already has an exclusive lock, so
8794e7f640dSJohn Baldwin 		 * start the process of blocking.
8804e7f640dSJohn Baldwin 		 */
8814e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
8824e7f640dSJohn Baldwin 		x = sx->sx_lock;
8834e7f640dSJohn Baldwin 
8844e7f640dSJohn Baldwin 		/*
8854e7f640dSJohn Baldwin 		 * The lock could have been released while we spun.
8864e7f640dSJohn Baldwin 		 * In this case loop back and retry.
8874e7f640dSJohn Baldwin 		 */
8884e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8894e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
8904e7f640dSJohn Baldwin 			continue;
8914e7f640dSJohn Baldwin 		}
8924e7f640dSJohn Baldwin 
8934e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8944e7f640dSJohn Baldwin 		/*
8954e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8964e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8974e7f640dSJohn Baldwin 		 * changes.
8984e7f640dSJohn Baldwin 		 */
8994e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
9001ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
9014e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
9024e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
9034e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
9044e7f640dSJohn Baldwin 				continue;
9054e7f640dSJohn Baldwin 			}
9064e7f640dSJohn Baldwin 		}
9074e7f640dSJohn Baldwin #endif
9084e7f640dSJohn Baldwin 
9094e7f640dSJohn Baldwin 		/*
9104e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
9114e7f640dSJohn Baldwin 		 * fail to set it drop the sleep queue lock and loop
9124e7f640dSJohn Baldwin 		 * back.
9134e7f640dSJohn Baldwin 		 */
9144e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED_WAITERS)) {
9154e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
9164e7f640dSJohn Baldwin 			    x | SX_LOCK_SHARED_WAITERS)) {
9174e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
9184e7f640dSJohn Baldwin 				continue;
9194e7f640dSJohn Baldwin 			}
9204e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
9214e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
9224e7f640dSJohn Baldwin 				    __func__, sx);
9234e7f640dSJohn Baldwin 		}
9244e7f640dSJohn Baldwin 
9254e7f640dSJohn Baldwin 		/*
9264e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the shared lock,
9274e7f640dSJohn Baldwin 		 * we have to sleep.
9284e7f640dSJohn Baldwin 		 */
9294e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9304e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
9314e7f640dSJohn Baldwin 			    __func__, sx);
9324e7f640dSJohn Baldwin 
933a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
934a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
935a5aedd68SStacey Son #endif
9364e7f640dSJohn Baldwin 		GIANT_SAVE();
9374e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
938f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
939f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
940f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
941c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
942f9819486SAttilio Rao 		else
943c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
944a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
945a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
946a5aedd68SStacey Son 		sleep_cnt++;
947a5aedd68SStacey Son #endif
948f9819486SAttilio Rao 		if (error) {
949f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
950f9819486SAttilio Rao 				CTR2(KTR_LOCK,
951f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
952f9819486SAttilio Rao 				    __func__, sx);
953f9819486SAttilio Rao 			break;
954f9819486SAttilio Rao 		}
9554e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9564e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
9574e7f640dSJohn Baldwin 			    __func__, sx);
9584e7f640dSJohn Baldwin 	}
959eea4f254SJeff Roberson 	if (error == 0)
960a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
961a5aedd68SStacey Son 		    contested, waittime, file, line);
962a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
963a5aedd68SStacey Son 	if (sleep_time)
964a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
965a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
966a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
967a5aedd68SStacey Son #endif
9684e7f640dSJohn Baldwin 	GIANT_RESTORE();
969f9819486SAttilio Rao 	return (error);
9704e7f640dSJohn Baldwin }
9714e7f640dSJohn Baldwin 
9724e7f640dSJohn Baldwin /*
9734e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_sunlock
9744e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
9754e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
9764e7f640dSJohn Baldwin  * accessible from at least sx.h.
9774e7f640dSJohn Baldwin  */
9784e7f640dSJohn Baldwin void
9794e7f640dSJohn Baldwin _sx_sunlock_hard(struct sx *sx, const char *file, int line)
9804e7f640dSJohn Baldwin {
9814e7f640dSJohn Baldwin 	uintptr_t x;
982da7bbd2cSJohn Baldwin 	int wakeup_swapper;
9834e7f640dSJohn Baldwin 
98435370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
98535370593SAndriy Gapon 		return;
98635370593SAndriy Gapon 
9874e7f640dSJohn Baldwin 	for (;;) {
9884e7f640dSJohn Baldwin 		x = sx->sx_lock;
9894e7f640dSJohn Baldwin 
9904e7f640dSJohn Baldwin 		/*
9914e7f640dSJohn Baldwin 		 * We should never have sharers while at least one thread
9924e7f640dSJohn Baldwin 		 * holds a shared lock.
9934e7f640dSJohn Baldwin 		 */
9944e7f640dSJohn Baldwin 		KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
9954e7f640dSJohn Baldwin 		    ("%s: waiting sharers", __func__));
9964e7f640dSJohn Baldwin 
9974e7f640dSJohn Baldwin 		/*
9984e7f640dSJohn Baldwin 		 * See if there is more than one shared lock held.  If
9994e7f640dSJohn Baldwin 		 * so, just drop one and return.
10004e7f640dSJohn Baldwin 		 */
10014e7f640dSJohn Baldwin 		if (SX_SHARERS(x) > 1) {
1002ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
10034e7f640dSJohn Baldwin 			    x - SX_ONE_SHARER)) {
10044e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
10054e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
10064e7f640dSJohn Baldwin 					    "%s: %p succeeded %p -> %p",
10074e7f640dSJohn Baldwin 					    __func__, sx, (void *)x,
10084e7f640dSJohn Baldwin 					    (void *)(x - SX_ONE_SHARER));
10094e7f640dSJohn Baldwin 				break;
10104e7f640dSJohn Baldwin 			}
10114e7f640dSJohn Baldwin 			continue;
10124e7f640dSJohn Baldwin 		}
10134e7f640dSJohn Baldwin 
10144e7f640dSJohn Baldwin 		/*
10154e7f640dSJohn Baldwin 		 * If there aren't any waiters for an exclusive lock,
10164e7f640dSJohn Baldwin 		 * then try to drop it quickly.
10174e7f640dSJohn Baldwin 		 */
10184e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
10194e7f640dSJohn Baldwin 			MPASS(x == SX_SHARERS_LOCK(1));
1020ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1021ddce63caSAttilio Rao 			    SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
10224e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
10234e7f640dSJohn Baldwin 					CTR2(KTR_LOCK, "%s: %p last succeeded",
10244e7f640dSJohn Baldwin 					    __func__, sx);
10254e7f640dSJohn Baldwin 				break;
10264e7f640dSJohn Baldwin 			}
10274e7f640dSJohn Baldwin 			continue;
10284e7f640dSJohn Baldwin 		}
10294e7f640dSJohn Baldwin 
10304e7f640dSJohn Baldwin 		/*
10314e7f640dSJohn Baldwin 		 * At this point, there should just be one sharer with
10324e7f640dSJohn Baldwin 		 * exclusive waiters.
10334e7f640dSJohn Baldwin 		 */
10344e7f640dSJohn Baldwin 		MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
10354e7f640dSJohn Baldwin 
10364e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
10374e7f640dSJohn Baldwin 
10384e7f640dSJohn Baldwin 		/*
10394e7f640dSJohn Baldwin 		 * Wake up semantic here is quite simple:
10404e7f640dSJohn Baldwin 		 * Just wake up all the exclusive waiters.
10414e7f640dSJohn Baldwin 		 * Note that the state of the lock could have changed,
10424e7f640dSJohn Baldwin 		 * so if it fails loop back and retry.
10434e7f640dSJohn Baldwin 		 */
1044ddce63caSAttilio Rao 		if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
10454e7f640dSJohn Baldwin 		    SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
10464e7f640dSJohn Baldwin 		    SX_LOCK_UNLOCKED)) {
10474e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
10484e7f640dSJohn Baldwin 			continue;
10494e7f640dSJohn Baldwin 		}
10504e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
10514e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p waking up all thread on"
10524e7f640dSJohn Baldwin 			    "exclusive queue", __func__, sx);
1053da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1054da7bbd2cSJohn Baldwin 		    0, SQ_EXCLUSIVE_QUEUE);
1055c5aa6b58SJeff Roberson 		sleepq_release(&sx->lock_object);
1056da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
1057da7bbd2cSJohn Baldwin 			kick_proc0();
10584e7f640dSJohn Baldwin 		break;
10594e7f640dSJohn Baldwin 	}
1060d55229b7SJason Evans }
10614e5e677bSJohn Baldwin 
10624e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT
1063781a35dfSJohn Baldwin #ifndef INVARIANTS
1064781a35dfSJohn Baldwin #undef	_sx_assert
1065781a35dfSJohn Baldwin #endif
1066781a35dfSJohn Baldwin 
10674e5e677bSJohn Baldwin /*
10684e5e677bSJohn Baldwin  * In the non-WITNESS case, sx_assert() can only detect that at least
10694e5e677bSJohn Baldwin  * *some* thread owns an slock, but it cannot guarantee that *this*
10704e5e677bSJohn Baldwin  * thread owns an slock.
10714e5e677bSJohn Baldwin  */
10724e5e677bSJohn Baldwin void
1073d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line)
10744e5e677bSJohn Baldwin {
10754e7f640dSJohn Baldwin #ifndef WITNESS
10764e7f640dSJohn Baldwin 	int slocked = 0;
10774e7f640dSJohn Baldwin #endif
10784e5e677bSJohn Baldwin 
107903129ba9SJohn Baldwin 	if (panicstr != NULL)
108003129ba9SJohn Baldwin 		return;
10814e5e677bSJohn Baldwin 	switch (what) {
10827ec137e5SJohn Baldwin 	case SA_SLOCKED:
10837ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_NOTRECURSED:
10847ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_RECURSED:
10854e7f640dSJohn Baldwin #ifndef WITNESS
10864e7f640dSJohn Baldwin 		slocked = 1;
10874e7f640dSJohn Baldwin 		/* FALLTHROUGH */
10884e7f640dSJohn Baldwin #endif
10897ec137e5SJohn Baldwin 	case SA_LOCKED:
10907ec137e5SJohn Baldwin 	case SA_LOCKED | SA_NOTRECURSED:
10917ec137e5SJohn Baldwin 	case SA_LOCKED | SA_RECURSED:
10924e5e677bSJohn Baldwin #ifdef WITNESS
1093aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
10944e5e677bSJohn Baldwin #else
10954e7f640dSJohn Baldwin 		/*
10964e7f640dSJohn Baldwin 		 * If some other thread has an exclusive lock or we
10974e7f640dSJohn Baldwin 		 * have one and are asserting a shared lock, fail.
10984e7f640dSJohn Baldwin 		 * Also, if no one has a lock at all, fail.
10994e7f640dSJohn Baldwin 		 */
11004e7f640dSJohn Baldwin 		if (sx->sx_lock == SX_LOCK_UNLOCKED ||
11014e7f640dSJohn Baldwin 		    (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
11024e7f640dSJohn Baldwin 		    sx_xholder(sx) != curthread)))
110303129ba9SJohn Baldwin 			panic("Lock %s not %slocked @ %s:%d\n",
11044e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, slocked ? "share " : "",
11054e7f640dSJohn Baldwin 			    file, line);
11064e7f640dSJohn Baldwin 
11074e7f640dSJohn Baldwin 		if (!(sx->sx_lock & SX_LOCK_SHARED)) {
11084e7f640dSJohn Baldwin 			if (sx_recursed(sx)) {
11097ec137e5SJohn Baldwin 				if (what & SA_NOTRECURSED)
11104e7f640dSJohn Baldwin 					panic("Lock %s recursed @ %s:%d\n",
11114e7f640dSJohn Baldwin 					    sx->lock_object.lo_name, file,
11124e7f640dSJohn Baldwin 					    line);
11137ec137e5SJohn Baldwin 			} else if (what & SA_RECURSED)
11144e7f640dSJohn Baldwin 				panic("Lock %s not recursed @ %s:%d\n",
11154e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
11164e7f640dSJohn Baldwin 		}
11174e5e677bSJohn Baldwin #endif
11184e5e677bSJohn Baldwin 		break;
11197ec137e5SJohn Baldwin 	case SA_XLOCKED:
11207ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_NOTRECURSED:
11217ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_RECURSED:
11224e7f640dSJohn Baldwin 		if (sx_xholder(sx) != curthread)
112303129ba9SJohn Baldwin 			panic("Lock %s not exclusively locked @ %s:%d\n",
1124aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11254e7f640dSJohn Baldwin 		if (sx_recursed(sx)) {
11267ec137e5SJohn Baldwin 			if (what & SA_NOTRECURSED)
11274e7f640dSJohn Baldwin 				panic("Lock %s recursed @ %s:%d\n",
11284e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
11297ec137e5SJohn Baldwin 		} else if (what & SA_RECURSED)
11304e7f640dSJohn Baldwin 			panic("Lock %s not recursed @ %s:%d\n",
11314e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11324e5e677bSJohn Baldwin 		break;
11337ec137e5SJohn Baldwin 	case SA_UNLOCKED:
113419b0efd3SPawel Jakub Dawidek #ifdef WITNESS
1135aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
113619b0efd3SPawel Jakub Dawidek #else
1137f6739b1dSPawel Jakub Dawidek 		/*
11384e7f640dSJohn Baldwin 		 * If we hold an exclusve lock fail.  We can't
11394e7f640dSJohn Baldwin 		 * reliably check to see if we hold a shared lock or
11404e7f640dSJohn Baldwin 		 * not.
1141f6739b1dSPawel Jakub Dawidek 		 */
11424e7f640dSJohn Baldwin 		if (sx_xholder(sx) == curthread)
114303129ba9SJohn Baldwin 			panic("Lock %s exclusively locked @ %s:%d\n",
1144aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
114519b0efd3SPawel Jakub Dawidek #endif
114619b0efd3SPawel Jakub Dawidek 		break;
11474e5e677bSJohn Baldwin 	default:
11484e5e677bSJohn Baldwin 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
11494e5e677bSJohn Baldwin 		    line);
11504e5e677bSJohn Baldwin 	}
11514e5e677bSJohn Baldwin }
11524e5e677bSJohn Baldwin #endif	/* INVARIANT_SUPPORT */
1153d272fe53SJohn Baldwin 
1154d272fe53SJohn Baldwin #ifdef DDB
11554e7f640dSJohn Baldwin static void
1156d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock)
1157d272fe53SJohn Baldwin {
1158d272fe53SJohn Baldwin 	struct thread *td;
1159d576deedSPawel Jakub Dawidek 	const struct sx *sx;
1160d272fe53SJohn Baldwin 
1161d576deedSPawel Jakub Dawidek 	sx = (const struct sx *)lock;
1162d272fe53SJohn Baldwin 
1163d272fe53SJohn Baldwin 	db_printf(" state: ");
11644e7f640dSJohn Baldwin 	if (sx->sx_lock == SX_LOCK_UNLOCKED)
11654e7f640dSJohn Baldwin 		db_printf("UNLOCKED\n");
11660026c92cSJohn Baldwin 	else if (sx->sx_lock == SX_LOCK_DESTROYED) {
11670026c92cSJohn Baldwin 		db_printf("DESTROYED\n");
11680026c92cSJohn Baldwin 		return;
11690026c92cSJohn Baldwin 	} else if (sx->sx_lock & SX_LOCK_SHARED)
11704e7f640dSJohn Baldwin 		db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
11714e7f640dSJohn Baldwin 	else {
11724e7f640dSJohn Baldwin 		td = sx_xholder(sx);
1173d272fe53SJohn Baldwin 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1174431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
11754e7f640dSJohn Baldwin 		if (sx_recursed(sx))
11764e7f640dSJohn Baldwin 			db_printf(" recursed: %d\n", sx->sx_recurse);
11774e7f640dSJohn Baldwin 	}
11784e7f640dSJohn Baldwin 
11794e7f640dSJohn Baldwin 	db_printf(" waiters: ");
11804e7f640dSJohn Baldwin 	switch(sx->sx_lock &
11814e7f640dSJohn Baldwin 	    (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
11824e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS:
11834e7f640dSJohn Baldwin 		db_printf("shared\n");
11844e7f640dSJohn Baldwin 		break;
11854e7f640dSJohn Baldwin 	case SX_LOCK_EXCLUSIVE_WAITERS:
11864e7f640dSJohn Baldwin 		db_printf("exclusive\n");
11874e7f640dSJohn Baldwin 		break;
11884e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
11894e7f640dSJohn Baldwin 		db_printf("exclusive and shared\n");
11904e7f640dSJohn Baldwin 		break;
11914e7f640dSJohn Baldwin 	default:
11924e7f640dSJohn Baldwin 		db_printf("none\n");
11934e7f640dSJohn Baldwin 	}
1194d272fe53SJohn Baldwin }
1195462a7addSJohn Baldwin 
1196462a7addSJohn Baldwin /*
1197462a7addSJohn Baldwin  * Check to see if a thread that is blocked on a sleep queue is actually
1198462a7addSJohn Baldwin  * blocked on an sx lock.  If so, output some details and return true.
1199462a7addSJohn Baldwin  * If the lock has an exclusive owner, return that in *ownerp.
1200462a7addSJohn Baldwin  */
1201462a7addSJohn Baldwin int
1202462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp)
1203462a7addSJohn Baldwin {
1204462a7addSJohn Baldwin 	struct sx *sx;
1205462a7addSJohn Baldwin 
1206462a7addSJohn Baldwin 	/*
12074e7f640dSJohn Baldwin 	 * Check to see if this thread is blocked on an sx lock.
12084e7f640dSJohn Baldwin 	 * First, we check the lock class.  If that is ok, then we
12094e7f640dSJohn Baldwin 	 * compare the lock name against the wait message.
1210462a7addSJohn Baldwin 	 */
12114e7f640dSJohn Baldwin 	sx = td->td_wchan;
12124e7f640dSJohn Baldwin 	if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
12134e7f640dSJohn Baldwin 	    sx->lock_object.lo_name != td->td_wmesg)
1214462a7addSJohn Baldwin 		return (0);
1215462a7addSJohn Baldwin 
1216462a7addSJohn Baldwin 	/* We think we have an sx lock, so output some details. */
1217462a7addSJohn Baldwin 	db_printf("blocked on sx \"%s\" ", td->td_wmesg);
12184e7f640dSJohn Baldwin 	*ownerp = sx_xholder(sx);
12194e7f640dSJohn Baldwin 	if (sx->sx_lock & SX_LOCK_SHARED)
12204e7f640dSJohn Baldwin 		db_printf("SLOCK (count %ju)\n",
12214e7f640dSJohn Baldwin 		    (uintmax_t)SX_SHARERS(sx->sx_lock));
12224e7f640dSJohn Baldwin 	else
1223462a7addSJohn Baldwin 		db_printf("XLOCK\n");
1224462a7addSJohn Baldwin 	return (1);
1225462a7addSJohn Baldwin }
1226d272fe53SJohn Baldwin #endif
1227