xref: /freebsd/sys/kern/kern_sx.c (revision e3ae0dfe694c2c457ffa1fb92036bdae74db8c76)
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"
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 
71f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
72f5f9340bSFabien Thomas #include <sys/pmckern.h>
73f5f9340bSFabien Thomas PMC_SOFT_DECLARE( , , lock, failed);
74f5f9340bSFabien Thomas #endif
75f5f9340bSFabien 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);
253*e3ae0dfeSAttilio Rao 	KASSERT(!TD_IS_IDLETHREAD(curthread),
254*e3ae0dfeSAttilio Rao 	    ("sx_slock() by idle thread %p on sx %s @ %s:%d",
255*e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
2560026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
2570026c92cSJohn Baldwin 	    ("sx_slock() of destroyed sx @ %s:%d", file, line));
25841313430SJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
259f9819486SAttilio Rao 	error = __sx_slock(sx, opts, file, line);
260f9819486SAttilio Rao 	if (!error) {
261aa89d8cdSJohn Baldwin 		LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
262aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, 0, file, line);
263764e4d54SJohn Baldwin 		curthread->td_locks++;
2646281b30aSJason Evans 	}
2656281b30aSJason Evans 
266f9819486SAttilio Rao 	return (error);
267f9819486SAttilio Rao }
268f9819486SAttilio Rao 
2695f36700aSJohn Baldwin int
2709fde98bbSAttilio Rao sx_try_slock_(struct sx *sx, const char *file, int line)
2715f36700aSJohn Baldwin {
2724e7f640dSJohn Baldwin 	uintptr_t x;
2735f36700aSJohn Baldwin 
27435370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
27535370593SAndriy Gapon 		return (1);
27635370593SAndriy Gapon 
277*e3ae0dfeSAttilio Rao 	KASSERT(!TD_IS_IDLETHREAD(curthread),
278*e3ae0dfeSAttilio Rao 	    ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
279*e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
280*e3ae0dfeSAttilio Rao 
281764a938bSPawel Jakub Dawidek 	for (;;) {
2824e7f640dSJohn Baldwin 		x = sx->sx_lock;
2830026c92cSJohn Baldwin 		KASSERT(x != SX_LOCK_DESTROYED,
2840026c92cSJohn Baldwin 		    ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
285764a938bSPawel Jakub Dawidek 		if (!(x & SX_LOCK_SHARED))
286764a938bSPawel Jakub Dawidek 			break;
287764a938bSPawel Jakub Dawidek 		if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
288aa89d8cdSJohn Baldwin 			LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
289aa89d8cdSJohn Baldwin 			WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
290764e4d54SJohn Baldwin 			curthread->td_locks++;
2915f36700aSJohn Baldwin 			return (1);
2925f36700aSJohn Baldwin 		}
293764a938bSPawel Jakub Dawidek 	}
2944e7f640dSJohn Baldwin 
2954e7f640dSJohn Baldwin 	LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
2964e7f640dSJohn Baldwin 	return (0);
2975f36700aSJohn Baldwin }
2985f36700aSJohn Baldwin 
299f9819486SAttilio Rao int
300f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line)
3016281b30aSJason Evans {
302f9819486SAttilio Rao 	int error = 0;
3036281b30aSJason Evans 
30435370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
30535370593SAndriy Gapon 		return (0);
3064e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
307*e3ae0dfeSAttilio Rao 	KASSERT(!TD_IS_IDLETHREAD(curthread),
308*e3ae0dfeSAttilio Rao 	    ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
309*e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
3100026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3110026c92cSJohn Baldwin 	    ("sx_xlock() of destroyed sx @ %s:%d", file, line));
312aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
31341313430SJohn Baldwin 	    line, NULL);
314f9819486SAttilio Rao 	error = __sx_xlock(sx, curthread, opts, file, line);
315f9819486SAttilio Rao 	if (!error) {
316f9819486SAttilio Rao 		LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
317f9819486SAttilio Rao 		    file, line);
318aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
319764e4d54SJohn Baldwin 		curthread->td_locks++;
3206281b30aSJason Evans 	}
3216281b30aSJason Evans 
322f9819486SAttilio Rao 	return (error);
323f9819486SAttilio Rao }
324f9819486SAttilio Rao 
3255f36700aSJohn Baldwin int
3269fde98bbSAttilio Rao sx_try_xlock_(struct sx *sx, const char *file, int line)
3275f36700aSJohn Baldwin {
3284e7f640dSJohn Baldwin 	int rval;
3295f36700aSJohn Baldwin 
33035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
33135370593SAndriy Gapon 		return (1);
33235370593SAndriy Gapon 
3334e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
334*e3ae0dfeSAttilio Rao 	KASSERT(!TD_IS_IDLETHREAD(curthread),
335*e3ae0dfeSAttilio Rao 	    ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
336*e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
3370026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3380026c92cSJohn Baldwin 	    ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
3394e7f640dSJohn Baldwin 
340f0830182SAttilio Rao 	if (sx_xlocked(sx) &&
341f0830182SAttilio Rao 	    (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
3424e7f640dSJohn Baldwin 		sx->sx_recurse++;
3434e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
3444e7f640dSJohn Baldwin 		rval = 1;
3454e7f640dSJohn Baldwin 	} else
3464e7f640dSJohn Baldwin 		rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
3474e7f640dSJohn Baldwin 		    (uintptr_t)curthread);
3484e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
3494e7f640dSJohn Baldwin 	if (rval) {
3504e7f640dSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
3514e7f640dSJohn Baldwin 		    file, line);
352764e4d54SJohn Baldwin 		curthread->td_locks++;
3535f36700aSJohn Baldwin 	}
3544e7f640dSJohn Baldwin 
3554e7f640dSJohn Baldwin 	return (rval);
3565f36700aSJohn Baldwin }
3575f36700aSJohn Baldwin 
3586281b30aSJason Evans void
35919284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line)
3606281b30aSJason Evans {
3616281b30aSJason Evans 
36235370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
36335370593SAndriy Gapon 		return;
3644e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3650026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3660026c92cSJohn Baldwin 	    ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
3677ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
368764e4d54SJohn Baldwin 	curthread->td_locks--;
369aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
370aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
3714e7f640dSJohn Baldwin 	__sx_sunlock(sx, file, line);
372a5aedd68SStacey Son 	LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
3736281b30aSJason Evans }
3746281b30aSJason Evans 
3756281b30aSJason Evans void
37619284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line)
3776281b30aSJason Evans {
3786281b30aSJason Evans 
37935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
38035370593SAndriy Gapon 		return;
3814e7f640dSJohn Baldwin 	MPASS(curthread != NULL);
3820026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3830026c92cSJohn Baldwin 	    ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
3847ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED, file, line);
385764e4d54SJohn Baldwin 	curthread->td_locks--;
386aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
3874e7f640dSJohn Baldwin 	LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
3884e7f640dSJohn Baldwin 	    line);
389afc0bfbdSKip Macy 	if (!sx_recursed(sx))
390a5aedd68SStacey Son 		LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
3914e7f640dSJohn Baldwin 	__sx_xunlock(sx, curthread, file, line);
3926281b30aSJason Evans }
393d55229b7SJason Evans 
3944e7f640dSJohn Baldwin /*
3954e7f640dSJohn Baldwin  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
3964e7f640dSJohn Baldwin  * This will only succeed if this thread holds a single shared lock.
3974e7f640dSJohn Baldwin  * Return 1 if if the upgrade succeed, 0 otherwise.
3984e7f640dSJohn Baldwin  */
399d55229b7SJason Evans int
4009fde98bbSAttilio Rao sx_try_upgrade_(struct sx *sx, const char *file, int line)
401d55229b7SJason Evans {
4024e7f640dSJohn Baldwin 	uintptr_t x;
4034e7f640dSJohn Baldwin 	int success;
404d55229b7SJason Evans 
40535370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
40635370593SAndriy Gapon 		return (1);
40735370593SAndriy Gapon 
4080026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4090026c92cSJohn Baldwin 	    ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
4107ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
411d55229b7SJason Evans 
4124e7f640dSJohn Baldwin 	/*
4134e7f640dSJohn Baldwin 	 * Try to switch from one shared lock to an exclusive lock.  We need
4144e7f640dSJohn Baldwin 	 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
4154e7f640dSJohn Baldwin 	 * we will wake up the exclusive waiters when we drop the lock.
4164e7f640dSJohn Baldwin 	 */
4174e7f640dSJohn Baldwin 	x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
4184e7f640dSJohn Baldwin 	success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
4194e7f640dSJohn Baldwin 	    (uintptr_t)curthread | x);
4204e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
421a5aedd68SStacey Son 	if (success) {
422aa89d8cdSJohn Baldwin 		WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
423b0b7cb50SJohn Baldwin 		    file, line);
424a5aedd68SStacey Son 		LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
425a5aedd68SStacey Son 	}
4264e7f640dSJohn Baldwin 	return (success);
427d55229b7SJason Evans }
428d55229b7SJason Evans 
4294e7f640dSJohn Baldwin /*
4304e7f640dSJohn Baldwin  * Downgrade an unrecursed exclusive lock into a single shared lock.
4314e7f640dSJohn Baldwin  */
432d55229b7SJason Evans void
4339fde98bbSAttilio Rao sx_downgrade_(struct sx *sx, const char *file, int line)
434d55229b7SJason Evans {
4354e7f640dSJohn Baldwin 	uintptr_t x;
436da7bbd2cSJohn Baldwin 	int wakeup_swapper;
437d55229b7SJason Evans 
43835370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
43935370593SAndriy Gapon 		return;
44035370593SAndriy Gapon 
4410026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4420026c92cSJohn Baldwin 	    ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
4437ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
4444e7f640dSJohn Baldwin #ifndef INVARIANTS
4454e7f640dSJohn Baldwin 	if (sx_recursed(sx))
4464e7f640dSJohn Baldwin 		panic("downgrade of a recursed lock");
4474e7f640dSJohn Baldwin #endif
448d55229b7SJason Evans 
449aa89d8cdSJohn Baldwin 	WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
450d55229b7SJason Evans 
4514e7f640dSJohn Baldwin 	/*
4524e7f640dSJohn Baldwin 	 * Try to switch from an exclusive lock with no shared waiters
4534e7f640dSJohn Baldwin 	 * to one sharer with no shared waiters.  If there are
4544e7f640dSJohn Baldwin 	 * exclusive waiters, we don't need to lock the sleep queue so
4554e7f640dSJohn Baldwin 	 * long as we preserve the flag.  We do one quick try and if
4564e7f640dSJohn Baldwin 	 * that fails we grab the sleepq lock to keep the flags from
4574e7f640dSJohn Baldwin 	 * changing and do it the slow way.
4584e7f640dSJohn Baldwin 	 *
4594e7f640dSJohn Baldwin 	 * We have to lock the sleep queue if there are shared waiters
4604e7f640dSJohn Baldwin 	 * so we can wake them up.
4614e7f640dSJohn Baldwin 	 */
4624e7f640dSJohn Baldwin 	x = sx->sx_lock;
4634e7f640dSJohn Baldwin 	if (!(x & SX_LOCK_SHARED_WAITERS) &&
4644e7f640dSJohn Baldwin 	    atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
4654e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
4664e7f640dSJohn Baldwin 		LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
4674e7f640dSJohn Baldwin 		return;
4684e7f640dSJohn Baldwin 	}
4694e7f640dSJohn Baldwin 
4704e7f640dSJohn Baldwin 	/*
4714e7f640dSJohn Baldwin 	 * Lock the sleep queue so we can read the waiters bits
4724e7f640dSJohn Baldwin 	 * without any races and wakeup any shared waiters.
4734e7f640dSJohn Baldwin 	 */
4744e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
4754e7f640dSJohn Baldwin 
4764e7f640dSJohn Baldwin 	/*
4774e7f640dSJohn Baldwin 	 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
4784e7f640dSJohn Baldwin 	 * shared lock.  If there are any shared waiters, wake them up.
4794e7f640dSJohn Baldwin 	 */
480da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
4814e7f640dSJohn Baldwin 	x = sx->sx_lock;
4824e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
4834e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS));
4844e7f640dSJohn Baldwin 	if (x & SX_LOCK_SHARED_WAITERS)
485da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
486da7bbd2cSJohn Baldwin 		    0, SQ_SHARED_QUEUE);
4874e7f640dSJohn Baldwin 	sleepq_release(&sx->lock_object);
488d55229b7SJason Evans 
489aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
490a5aedd68SStacey Son 	LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
491da7bbd2cSJohn Baldwin 
492da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
493da7bbd2cSJohn Baldwin 		kick_proc0();
4944e7f640dSJohn Baldwin }
495d55229b7SJason Evans 
4964e7f640dSJohn Baldwin /*
4974e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xlock
4984e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
4994e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
5004e7f640dSJohn Baldwin  * accessible from at least sx.h.
5014e7f640dSJohn Baldwin  */
502f9819486SAttilio Rao int
503f9819486SAttilio Rao _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
504f9819486SAttilio Rao     int line)
5054e7f640dSJohn Baldwin {
5064e7f640dSJohn Baldwin 	GIANT_DECLARE;
5074e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5084e7f640dSJohn Baldwin 	volatile struct thread *owner;
5091ae1c2a3SAttilio Rao 	u_int i, spintries = 0;
5104e7f640dSJohn Baldwin #endif
5114e7f640dSJohn Baldwin 	uintptr_t x;
5121723a064SJeff Roberson #ifdef LOCK_PROFILING
5131723a064SJeff Roberson 	uint64_t waittime = 0;
5141723a064SJeff Roberson 	int contested = 0;
5151723a064SJeff Roberson #endif
5161723a064SJeff Roberson 	int error = 0;
517a5aedd68SStacey Son #ifdef	KDTRACE_HOOKS
518a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
519a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
520a5aedd68SStacey Son 	int64_t sleep_time = 0;
521a5aedd68SStacey Son #endif
5224e7f640dSJohn Baldwin 
52335370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
52435370593SAndriy Gapon 		return (0);
52535370593SAndriy Gapon 
5264e7f640dSJohn Baldwin 	/* If we already hold an exclusive lock, then recurse. */
5274e7f640dSJohn Baldwin 	if (sx_xlocked(sx)) {
528f0830182SAttilio Rao 		KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
529b0d67325SJohn Baldwin 	    ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
530b0d67325SJohn Baldwin 		    sx->lock_object.lo_name, file, line));
5314e7f640dSJohn Baldwin 		sx->sx_recurse++;
5324e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
5334e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
5344e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
535f9819486SAttilio Rao 		return (0);
5364e7f640dSJohn Baldwin 	}
5374e7f640dSJohn Baldwin 
5384e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
5394e7f640dSJohn Baldwin 		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
5404e7f640dSJohn Baldwin 		    sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
5414e7f640dSJohn Baldwin 
5424e7f640dSJohn Baldwin 	while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
543a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
544a5aedd68SStacey Son 		spin_cnt++;
545a5aedd68SStacey Son #endif
546f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
547f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
548f5f9340bSFabien Thomas #endif
549eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
550eea4f254SJeff Roberson 		    &waittime);
5514e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5524e7f640dSJohn Baldwin 		/*
5534e7f640dSJohn Baldwin 		 * If the lock is write locked and the owner is
5544e7f640dSJohn Baldwin 		 * running on another CPU, spin until the owner stops
5554e7f640dSJohn Baldwin 		 * running or the state of the lock changes.
5564e7f640dSJohn Baldwin 		 */
5574e7f640dSJohn Baldwin 		x = sx->sx_lock;
5588545538bSJohn Baldwin 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
5591ae1c2a3SAttilio Rao 			if ((x & SX_LOCK_SHARED) == 0) {
5604e7f640dSJohn Baldwin 				x = SX_OWNER(x);
5614e7f640dSJohn Baldwin 				owner = (struct thread *)x;
5624e7f640dSJohn Baldwin 				if (TD_IS_RUNNING(owner)) {
5634e7f640dSJohn Baldwin 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5644e7f640dSJohn Baldwin 						CTR3(KTR_LOCK,
5654e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
5664e7f640dSJohn Baldwin 						    __func__, sx, owner);
5674e7f640dSJohn Baldwin 					GIANT_SAVE();
5684e7f640dSJohn Baldwin 					while (SX_OWNER(sx->sx_lock) == x &&
569a5aedd68SStacey Son 					    TD_IS_RUNNING(owner)) {
5704e7f640dSJohn Baldwin 						cpu_spinwait();
571a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
572a5aedd68SStacey Son 						spin_cnt++;
573a5aedd68SStacey Son #endif
574a5aedd68SStacey Son 					}
5754e7f640dSJohn Baldwin 					continue;
5764e7f640dSJohn Baldwin 				}
5771ae1c2a3SAttilio Rao 			} else if (SX_SHARERS(x) && spintries < asx_retries) {
5788d3635c4SAttilio Rao 				GIANT_SAVE();
5791ae1c2a3SAttilio Rao 				spintries++;
5801ae1c2a3SAttilio Rao 				for (i = 0; i < asx_loops; i++) {
5811ae1c2a3SAttilio Rao 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5821ae1c2a3SAttilio Rao 						CTR4(KTR_LOCK,
5831ae1c2a3SAttilio Rao 				    "%s: shared spinning on %p with %u and %u",
5841ae1c2a3SAttilio Rao 						    __func__, sx, spintries, i);
5851ae1c2a3SAttilio Rao 					x = sx->sx_lock;
5861ae1c2a3SAttilio Rao 					if ((x & SX_LOCK_SHARED) == 0 ||
5871ae1c2a3SAttilio Rao 					    SX_SHARERS(x) == 0)
5881ae1c2a3SAttilio Rao 						break;
5891ae1c2a3SAttilio Rao 					cpu_spinwait();
5901ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS
5911ae1c2a3SAttilio Rao 					spin_cnt++;
5921ae1c2a3SAttilio Rao #endif
5931ae1c2a3SAttilio Rao 				}
5941ae1c2a3SAttilio Rao 				if (i != asx_loops)
5951ae1c2a3SAttilio Rao 					continue;
5961ae1c2a3SAttilio Rao 			}
5974e7f640dSJohn Baldwin 		}
5984e7f640dSJohn Baldwin #endif
5994e7f640dSJohn Baldwin 
6004e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
6014e7f640dSJohn Baldwin 		x = sx->sx_lock;
6024e7f640dSJohn Baldwin 
6034e7f640dSJohn Baldwin 		/*
6044e7f640dSJohn Baldwin 		 * If the lock was released while spinning on the
6054e7f640dSJohn Baldwin 		 * sleep queue chain lock, try again.
6064e7f640dSJohn Baldwin 		 */
6074e7f640dSJohn Baldwin 		if (x == SX_LOCK_UNLOCKED) {
6084e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6094e7f640dSJohn Baldwin 			continue;
6104e7f640dSJohn Baldwin 		}
6114e7f640dSJohn Baldwin 
6124e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
6134e7f640dSJohn Baldwin 		/*
6144e7f640dSJohn Baldwin 		 * The current lock owner might have started executing
6154e7f640dSJohn Baldwin 		 * on another CPU (or the lock could have changed
6164e7f640dSJohn Baldwin 		 * owners) while we were waiting on the sleep queue
6174e7f640dSJohn Baldwin 		 * chain lock.  If so, drop the sleep queue lock and try
6184e7f640dSJohn Baldwin 		 * again.
6194e7f640dSJohn Baldwin 		 */
6204e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
6211ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
6224e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
6234e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
6244e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6254e7f640dSJohn Baldwin 				continue;
6264e7f640dSJohn Baldwin 			}
6274e7f640dSJohn Baldwin 		}
6284e7f640dSJohn Baldwin #endif
6294e7f640dSJohn Baldwin 
6304e7f640dSJohn Baldwin 		/*
6314e7f640dSJohn Baldwin 		 * If an exclusive lock was released with both shared
6324e7f640dSJohn Baldwin 		 * and exclusive waiters and a shared waiter hasn't
6334e7f640dSJohn Baldwin 		 * woken up and acquired the lock yet, sx_lock will be
6344e7f640dSJohn Baldwin 		 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
6354e7f640dSJohn Baldwin 		 * If we see that value, try to acquire it once.  Note
6364e7f640dSJohn Baldwin 		 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
6374e7f640dSJohn Baldwin 		 * as there are other exclusive waiters still.  If we
6384e7f640dSJohn Baldwin 		 * fail, restart the loop.
6394e7f640dSJohn Baldwin 		 */
6404e7f640dSJohn Baldwin 		if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
6414e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock,
6424e7f640dSJohn Baldwin 			    SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
6434e7f640dSJohn Baldwin 			    tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
6444e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6454e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
6464e7f640dSJohn Baldwin 				    __func__, sx);
6474e7f640dSJohn Baldwin 				break;
6484e7f640dSJohn Baldwin 			}
6494e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6504e7f640dSJohn Baldwin 			continue;
6514e7f640dSJohn Baldwin 		}
6524e7f640dSJohn Baldwin 
6534e7f640dSJohn Baldwin 		/*
6544e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
6554e7f640dSJohn Baldwin 		 * than loop back and retry.
6564e7f640dSJohn Baldwin 		 */
6574e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
6584e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
6594e7f640dSJohn Baldwin 			    x | SX_LOCK_EXCLUSIVE_WAITERS)) {
6604e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6614e7f640dSJohn Baldwin 				continue;
6624e7f640dSJohn Baldwin 			}
6634e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
6644e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
6654e7f640dSJohn Baldwin 				    __func__, sx);
6664e7f640dSJohn Baldwin 		}
6674e7f640dSJohn Baldwin 
6684e7f640dSJohn Baldwin 		/*
6694e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the exclusive
6704e7f640dSJohn Baldwin 		 * lock and the exclusive waiters flag is set, we have
6714e7f640dSJohn Baldwin 		 * to sleep.
6724e7f640dSJohn Baldwin 		 */
6734e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6744e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
6754e7f640dSJohn Baldwin 			    __func__, sx);
6764e7f640dSJohn Baldwin 
677a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
678a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
679a5aedd68SStacey Son #endif
6804e7f640dSJohn Baldwin 		GIANT_SAVE();
6814e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
682f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
683f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
684f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
685c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
686f9819486SAttilio Rao 		else
687c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
688a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
689a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
690a5aedd68SStacey Son 		sleep_cnt++;
691a5aedd68SStacey Son #endif
692f9819486SAttilio Rao 		if (error) {
693f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
694f9819486SAttilio Rao 				CTR2(KTR_LOCK,
695f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
696f9819486SAttilio Rao 				    __func__, sx);
697f9819486SAttilio Rao 			break;
698f9819486SAttilio Rao 		}
6994e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7004e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
7014e7f640dSJohn Baldwin 			    __func__, sx);
7024e7f640dSJohn Baldwin 	}
7034e7f640dSJohn Baldwin 
7044e7f640dSJohn Baldwin 	GIANT_RESTORE();
705f9819486SAttilio Rao 	if (!error)
706a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
707a5aedd68SStacey Son 		    contested, waittime, file, line);
708a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
709a5aedd68SStacey Son 	if (sleep_time)
710a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
711a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
712a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
713a5aedd68SStacey Son #endif
714f9819486SAttilio Rao 	return (error);
7154e7f640dSJohn Baldwin }
7164e7f640dSJohn Baldwin 
7174e7f640dSJohn Baldwin /*
7184e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xunlock
7194e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7204e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7214e7f640dSJohn Baldwin  * accessible from at least sx.h.
7224e7f640dSJohn Baldwin  */
7234e7f640dSJohn Baldwin void
7244e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
7254e7f640dSJohn Baldwin {
7264e7f640dSJohn Baldwin 	uintptr_t x;
727da7bbd2cSJohn Baldwin 	int queue, wakeup_swapper;
7284e7f640dSJohn Baldwin 
72935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
73035370593SAndriy Gapon 		return;
73135370593SAndriy Gapon 
7324e7f640dSJohn Baldwin 	MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
7334e7f640dSJohn Baldwin 
7344e7f640dSJohn Baldwin 	/* If the lock is recursed, then unrecurse one level. */
7354e7f640dSJohn Baldwin 	if (sx_xlocked(sx) && sx_recursed(sx)) {
7364e7f640dSJohn Baldwin 		if ((--sx->sx_recurse) == 0)
7374e7f640dSJohn Baldwin 			atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
7384e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7394e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
7404e7f640dSJohn Baldwin 		return;
7414e7f640dSJohn Baldwin 	}
7424e7f640dSJohn Baldwin 	MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
7434e7f640dSJohn Baldwin 	    SX_LOCK_EXCLUSIVE_WAITERS));
7444e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7454e7f640dSJohn Baldwin 		CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
7464e7f640dSJohn Baldwin 
7474e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
7484e7f640dSJohn Baldwin 	x = SX_LOCK_UNLOCKED;
7494e7f640dSJohn Baldwin 
7504e7f640dSJohn Baldwin 	/*
7514e7f640dSJohn Baldwin 	 * The wake up algorithm here is quite simple and probably not
7524e7f640dSJohn Baldwin 	 * ideal.  It gives precedence to shared waiters if they are
7534e7f640dSJohn Baldwin 	 * present.  For this condition, we have to preserve the
7544e7f640dSJohn Baldwin 	 * state of the exclusive waiters flag.
7552028867dSAttilio Rao 	 * If interruptible sleeps left the shared queue empty avoid a
7562028867dSAttilio Rao 	 * starvation for the threads sleeping on the exclusive queue by giving
7572028867dSAttilio Rao 	 * them precedence and cleaning up the shared waiters bit anyway.
7584e7f640dSJohn Baldwin 	 */
7592028867dSAttilio Rao 	if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
7602028867dSAttilio Rao 	    sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
7614e7f640dSJohn Baldwin 		queue = SQ_SHARED_QUEUE;
7624e7f640dSJohn Baldwin 		x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
7634e7f640dSJohn Baldwin 	} else
7644e7f640dSJohn Baldwin 		queue = SQ_EXCLUSIVE_QUEUE;
7654e7f640dSJohn Baldwin 
7664e7f640dSJohn Baldwin 	/* Wake up all the waiters for the specific queue. */
7674e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7684e7f640dSJohn Baldwin 		CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
7694e7f640dSJohn Baldwin 		    __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
7704e7f640dSJohn Baldwin 		    "exclusive");
7714e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, x);
772da7bbd2cSJohn Baldwin 	wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
773da7bbd2cSJohn Baldwin 	    queue);
774c5aa6b58SJeff Roberson 	sleepq_release(&sx->lock_object);
775da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
776da7bbd2cSJohn Baldwin 		kick_proc0();
7774e7f640dSJohn Baldwin }
7784e7f640dSJohn Baldwin 
7794e7f640dSJohn Baldwin /*
7804e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_slock
7814e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7824e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7834e7f640dSJohn Baldwin  * accessible from at least sx.h.
7844e7f640dSJohn Baldwin  */
785f9819486SAttilio Rao int
786f9819486SAttilio Rao _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
7874e7f640dSJohn Baldwin {
7884e7f640dSJohn Baldwin 	GIANT_DECLARE;
7894e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
7904e7f640dSJohn Baldwin 	volatile struct thread *owner;
7914e7f640dSJohn Baldwin #endif
7921723a064SJeff Roberson #ifdef LOCK_PROFILING
793c1a6d9faSAttilio Rao 	uint64_t waittime = 0;
794c1a6d9faSAttilio Rao 	int contested = 0;
7951723a064SJeff Roberson #endif
7964e7f640dSJohn Baldwin 	uintptr_t x;
797c1a6d9faSAttilio Rao 	int error = 0;
798a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
799a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
800a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
801a5aedd68SStacey Son 	int64_t sleep_time = 0;
802a5aedd68SStacey Son #endif
803c1a6d9faSAttilio Rao 
80435370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
80535370593SAndriy Gapon 		return (0);
80635370593SAndriy Gapon 
8074e7f640dSJohn Baldwin 	/*
8084e7f640dSJohn Baldwin 	 * As with rwlocks, we don't make any attempt to try to block
8094e7f640dSJohn Baldwin 	 * shared locks once there is an exclusive waiter.
8104e7f640dSJohn Baldwin 	 */
8114e7f640dSJohn Baldwin 	for (;;) {
812a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
813a5aedd68SStacey Son 		spin_cnt++;
814a5aedd68SStacey Son #endif
8154e7f640dSJohn Baldwin 		x = sx->sx_lock;
8164e7f640dSJohn Baldwin 
8174e7f640dSJohn Baldwin 		/*
8184e7f640dSJohn Baldwin 		 * If no other thread has an exclusive lock then try to bump up
8194e7f640dSJohn Baldwin 		 * the count of sharers.  Since we have to preserve the state
8204e7f640dSJohn Baldwin 		 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
8214e7f640dSJohn Baldwin 		 * shared lock loop back and retry.
8224e7f640dSJohn Baldwin 		 */
8234e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8244e7f640dSJohn Baldwin 			MPASS(!(x & SX_LOCK_SHARED_WAITERS));
8254e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
8264e7f640dSJohn Baldwin 			    x + SX_ONE_SHARER)) {
8274e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8284e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
8294e7f640dSJohn Baldwin 					    "%s: %p succeed %p -> %p", __func__,
8304e7f640dSJohn Baldwin 					    sx, (void *)x,
8314e7f640dSJohn Baldwin 					    (void *)(x + SX_ONE_SHARER));
8324e7f640dSJohn Baldwin 				break;
8334e7f640dSJohn Baldwin 			}
8344e7f640dSJohn Baldwin 			continue;
8354e7f640dSJohn Baldwin 		}
836f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
837f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
838f5f9340bSFabien Thomas #endif
839eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
840eea4f254SJeff Roberson 		    &waittime);
8414e7f640dSJohn Baldwin 
8424e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8434e7f640dSJohn Baldwin 		/*
8444e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8454e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8464e7f640dSJohn Baldwin 		 * changes.
8474e7f640dSJohn Baldwin 		 */
8481ae1c2a3SAttilio Rao 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8494e7f640dSJohn Baldwin 			x = SX_OWNER(x);
8504e7f640dSJohn Baldwin 			owner = (struct thread *)x;
8514e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8524e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8534e7f640dSJohn Baldwin 					CTR3(KTR_LOCK,
8544e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
8554e7f640dSJohn Baldwin 					    __func__, sx, owner);
8564e7f640dSJohn Baldwin 				GIANT_SAVE();
8574e7f640dSJohn Baldwin 				while (SX_OWNER(sx->sx_lock) == x &&
858a5aedd68SStacey Son 				    TD_IS_RUNNING(owner)) {
859a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
860a5aedd68SStacey Son 					spin_cnt++;
861a5aedd68SStacey Son #endif
8624e7f640dSJohn Baldwin 					cpu_spinwait();
863a5aedd68SStacey Son 				}
8644e7f640dSJohn Baldwin 				continue;
8654e7f640dSJohn Baldwin 			}
8664e7f640dSJohn Baldwin 		}
8674e7f640dSJohn Baldwin #endif
8684e7f640dSJohn Baldwin 
8694e7f640dSJohn Baldwin 		/*
8704e7f640dSJohn Baldwin 		 * Some other thread already has an exclusive lock, so
8714e7f640dSJohn Baldwin 		 * start the process of blocking.
8724e7f640dSJohn Baldwin 		 */
8734e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
8744e7f640dSJohn Baldwin 		x = sx->sx_lock;
8754e7f640dSJohn Baldwin 
8764e7f640dSJohn Baldwin 		/*
8774e7f640dSJohn Baldwin 		 * The lock could have been released while we spun.
8784e7f640dSJohn Baldwin 		 * In this case loop back and retry.
8794e7f640dSJohn Baldwin 		 */
8804e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8814e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
8824e7f640dSJohn Baldwin 			continue;
8834e7f640dSJohn Baldwin 		}
8844e7f640dSJohn Baldwin 
8854e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8864e7f640dSJohn Baldwin 		/*
8874e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8884e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8894e7f640dSJohn Baldwin 		 * changes.
8904e7f640dSJohn Baldwin 		 */
8914e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
8921ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8934e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
8944e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8954e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
8964e7f640dSJohn Baldwin 				continue;
8974e7f640dSJohn Baldwin 			}
8984e7f640dSJohn Baldwin 		}
8994e7f640dSJohn Baldwin #endif
9004e7f640dSJohn Baldwin 
9014e7f640dSJohn Baldwin 		/*
9024e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
9034e7f640dSJohn Baldwin 		 * fail to set it drop the sleep queue lock and loop
9044e7f640dSJohn Baldwin 		 * back.
9054e7f640dSJohn Baldwin 		 */
9064e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED_WAITERS)) {
9074e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
9084e7f640dSJohn Baldwin 			    x | SX_LOCK_SHARED_WAITERS)) {
9094e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
9104e7f640dSJohn Baldwin 				continue;
9114e7f640dSJohn Baldwin 			}
9124e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
9134e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
9144e7f640dSJohn Baldwin 				    __func__, sx);
9154e7f640dSJohn Baldwin 		}
9164e7f640dSJohn Baldwin 
9174e7f640dSJohn Baldwin 		/*
9184e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the shared lock,
9194e7f640dSJohn Baldwin 		 * we have to sleep.
9204e7f640dSJohn Baldwin 		 */
9214e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9224e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
9234e7f640dSJohn Baldwin 			    __func__, sx);
9244e7f640dSJohn Baldwin 
925a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
926a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
927a5aedd68SStacey Son #endif
9284e7f640dSJohn Baldwin 		GIANT_SAVE();
9294e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
930f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
931f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
932f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
933c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
934f9819486SAttilio Rao 		else
935c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
936a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
937a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
938a5aedd68SStacey Son 		sleep_cnt++;
939a5aedd68SStacey Son #endif
940f9819486SAttilio Rao 		if (error) {
941f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
942f9819486SAttilio Rao 				CTR2(KTR_LOCK,
943f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
944f9819486SAttilio Rao 				    __func__, sx);
945f9819486SAttilio Rao 			break;
946f9819486SAttilio Rao 		}
9474e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9484e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
9494e7f640dSJohn Baldwin 			    __func__, sx);
9504e7f640dSJohn Baldwin 	}
951eea4f254SJeff Roberson 	if (error == 0)
952a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
953a5aedd68SStacey Son 		    contested, waittime, file, line);
954a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
955a5aedd68SStacey Son 	if (sleep_time)
956a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
957a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
958a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
959a5aedd68SStacey Son #endif
9604e7f640dSJohn Baldwin 	GIANT_RESTORE();
961f9819486SAttilio Rao 	return (error);
9624e7f640dSJohn Baldwin }
9634e7f640dSJohn Baldwin 
9644e7f640dSJohn Baldwin /*
9654e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_sunlock
9664e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
9674e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
9684e7f640dSJohn Baldwin  * accessible from at least sx.h.
9694e7f640dSJohn Baldwin  */
9704e7f640dSJohn Baldwin void
9714e7f640dSJohn Baldwin _sx_sunlock_hard(struct sx *sx, const char *file, int line)
9724e7f640dSJohn Baldwin {
9734e7f640dSJohn Baldwin 	uintptr_t x;
974da7bbd2cSJohn Baldwin 	int wakeup_swapper;
9754e7f640dSJohn Baldwin 
97635370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
97735370593SAndriy Gapon 		return;
97835370593SAndriy Gapon 
9794e7f640dSJohn Baldwin 	for (;;) {
9804e7f640dSJohn Baldwin 		x = sx->sx_lock;
9814e7f640dSJohn Baldwin 
9824e7f640dSJohn Baldwin 		/*
9834e7f640dSJohn Baldwin 		 * We should never have sharers while at least one thread
9844e7f640dSJohn Baldwin 		 * holds a shared lock.
9854e7f640dSJohn Baldwin 		 */
9864e7f640dSJohn Baldwin 		KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
9874e7f640dSJohn Baldwin 		    ("%s: waiting sharers", __func__));
9884e7f640dSJohn Baldwin 
9894e7f640dSJohn Baldwin 		/*
9904e7f640dSJohn Baldwin 		 * See if there is more than one shared lock held.  If
9914e7f640dSJohn Baldwin 		 * so, just drop one and return.
9924e7f640dSJohn Baldwin 		 */
9934e7f640dSJohn Baldwin 		if (SX_SHARERS(x) > 1) {
994ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
9954e7f640dSJohn Baldwin 			    x - SX_ONE_SHARER)) {
9964e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
9974e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
9984e7f640dSJohn Baldwin 					    "%s: %p succeeded %p -> %p",
9994e7f640dSJohn Baldwin 					    __func__, sx, (void *)x,
10004e7f640dSJohn Baldwin 					    (void *)(x - SX_ONE_SHARER));
10014e7f640dSJohn Baldwin 				break;
10024e7f640dSJohn Baldwin 			}
10034e7f640dSJohn Baldwin 			continue;
10044e7f640dSJohn Baldwin 		}
10054e7f640dSJohn Baldwin 
10064e7f640dSJohn Baldwin 		/*
10074e7f640dSJohn Baldwin 		 * If there aren't any waiters for an exclusive lock,
10084e7f640dSJohn Baldwin 		 * then try to drop it quickly.
10094e7f640dSJohn Baldwin 		 */
10104e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
10114e7f640dSJohn Baldwin 			MPASS(x == SX_SHARERS_LOCK(1));
1012ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1013ddce63caSAttilio Rao 			    SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
10144e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
10154e7f640dSJohn Baldwin 					CTR2(KTR_LOCK, "%s: %p last succeeded",
10164e7f640dSJohn Baldwin 					    __func__, sx);
10174e7f640dSJohn Baldwin 				break;
10184e7f640dSJohn Baldwin 			}
10194e7f640dSJohn Baldwin 			continue;
10204e7f640dSJohn Baldwin 		}
10214e7f640dSJohn Baldwin 
10224e7f640dSJohn Baldwin 		/*
10234e7f640dSJohn Baldwin 		 * At this point, there should just be one sharer with
10244e7f640dSJohn Baldwin 		 * exclusive waiters.
10254e7f640dSJohn Baldwin 		 */
10264e7f640dSJohn Baldwin 		MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
10274e7f640dSJohn Baldwin 
10284e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
10294e7f640dSJohn Baldwin 
10304e7f640dSJohn Baldwin 		/*
10314e7f640dSJohn Baldwin 		 * Wake up semantic here is quite simple:
10324e7f640dSJohn Baldwin 		 * Just wake up all the exclusive waiters.
10334e7f640dSJohn Baldwin 		 * Note that the state of the lock could have changed,
10344e7f640dSJohn Baldwin 		 * so if it fails loop back and retry.
10354e7f640dSJohn Baldwin 		 */
1036ddce63caSAttilio Rao 		if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
10374e7f640dSJohn Baldwin 		    SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
10384e7f640dSJohn Baldwin 		    SX_LOCK_UNLOCKED)) {
10394e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
10404e7f640dSJohn Baldwin 			continue;
10414e7f640dSJohn Baldwin 		}
10424e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
10434e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p waking up all thread on"
10444e7f640dSJohn Baldwin 			    "exclusive queue", __func__, sx);
1045da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1046da7bbd2cSJohn Baldwin 		    0, SQ_EXCLUSIVE_QUEUE);
1047c5aa6b58SJeff Roberson 		sleepq_release(&sx->lock_object);
1048da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
1049da7bbd2cSJohn Baldwin 			kick_proc0();
10504e7f640dSJohn Baldwin 		break;
10514e7f640dSJohn Baldwin 	}
1052d55229b7SJason Evans }
10534e5e677bSJohn Baldwin 
10544e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT
1055781a35dfSJohn Baldwin #ifndef INVARIANTS
1056781a35dfSJohn Baldwin #undef	_sx_assert
1057781a35dfSJohn Baldwin #endif
1058781a35dfSJohn Baldwin 
10594e5e677bSJohn Baldwin /*
10604e5e677bSJohn Baldwin  * In the non-WITNESS case, sx_assert() can only detect that at least
10614e5e677bSJohn Baldwin  * *some* thread owns an slock, but it cannot guarantee that *this*
10624e5e677bSJohn Baldwin  * thread owns an slock.
10634e5e677bSJohn Baldwin  */
10644e5e677bSJohn Baldwin void
1065d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line)
10664e5e677bSJohn Baldwin {
10674e7f640dSJohn Baldwin #ifndef WITNESS
10684e7f640dSJohn Baldwin 	int slocked = 0;
10694e7f640dSJohn Baldwin #endif
10704e5e677bSJohn Baldwin 
107103129ba9SJohn Baldwin 	if (panicstr != NULL)
107203129ba9SJohn Baldwin 		return;
10734e5e677bSJohn Baldwin 	switch (what) {
10747ec137e5SJohn Baldwin 	case SA_SLOCKED:
10757ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_NOTRECURSED:
10767ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_RECURSED:
10774e7f640dSJohn Baldwin #ifndef WITNESS
10784e7f640dSJohn Baldwin 		slocked = 1;
10794e7f640dSJohn Baldwin 		/* FALLTHROUGH */
10804e7f640dSJohn Baldwin #endif
10817ec137e5SJohn Baldwin 	case SA_LOCKED:
10827ec137e5SJohn Baldwin 	case SA_LOCKED | SA_NOTRECURSED:
10837ec137e5SJohn Baldwin 	case SA_LOCKED | SA_RECURSED:
10844e5e677bSJohn Baldwin #ifdef WITNESS
1085aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
10864e5e677bSJohn Baldwin #else
10874e7f640dSJohn Baldwin 		/*
10884e7f640dSJohn Baldwin 		 * If some other thread has an exclusive lock or we
10894e7f640dSJohn Baldwin 		 * have one and are asserting a shared lock, fail.
10904e7f640dSJohn Baldwin 		 * Also, if no one has a lock at all, fail.
10914e7f640dSJohn Baldwin 		 */
10924e7f640dSJohn Baldwin 		if (sx->sx_lock == SX_LOCK_UNLOCKED ||
10934e7f640dSJohn Baldwin 		    (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
10944e7f640dSJohn Baldwin 		    sx_xholder(sx) != curthread)))
109503129ba9SJohn Baldwin 			panic("Lock %s not %slocked @ %s:%d\n",
10964e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, slocked ? "share " : "",
10974e7f640dSJohn Baldwin 			    file, line);
10984e7f640dSJohn Baldwin 
10994e7f640dSJohn Baldwin 		if (!(sx->sx_lock & SX_LOCK_SHARED)) {
11004e7f640dSJohn Baldwin 			if (sx_recursed(sx)) {
11017ec137e5SJohn Baldwin 				if (what & SA_NOTRECURSED)
11024e7f640dSJohn Baldwin 					panic("Lock %s recursed @ %s:%d\n",
11034e7f640dSJohn Baldwin 					    sx->lock_object.lo_name, file,
11044e7f640dSJohn Baldwin 					    line);
11057ec137e5SJohn Baldwin 			} else if (what & SA_RECURSED)
11064e7f640dSJohn Baldwin 				panic("Lock %s not recursed @ %s:%d\n",
11074e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
11084e7f640dSJohn Baldwin 		}
11094e5e677bSJohn Baldwin #endif
11104e5e677bSJohn Baldwin 		break;
11117ec137e5SJohn Baldwin 	case SA_XLOCKED:
11127ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_NOTRECURSED:
11137ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_RECURSED:
11144e7f640dSJohn Baldwin 		if (sx_xholder(sx) != curthread)
111503129ba9SJohn Baldwin 			panic("Lock %s not exclusively locked @ %s:%d\n",
1116aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11174e7f640dSJohn Baldwin 		if (sx_recursed(sx)) {
11187ec137e5SJohn Baldwin 			if (what & SA_NOTRECURSED)
11194e7f640dSJohn Baldwin 				panic("Lock %s recursed @ %s:%d\n",
11204e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
11217ec137e5SJohn Baldwin 		} else if (what & SA_RECURSED)
11224e7f640dSJohn Baldwin 			panic("Lock %s not recursed @ %s:%d\n",
11234e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11244e5e677bSJohn Baldwin 		break;
11257ec137e5SJohn Baldwin 	case SA_UNLOCKED:
112619b0efd3SPawel Jakub Dawidek #ifdef WITNESS
1127aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
112819b0efd3SPawel Jakub Dawidek #else
1129f6739b1dSPawel Jakub Dawidek 		/*
11304e7f640dSJohn Baldwin 		 * If we hold an exclusve lock fail.  We can't
11314e7f640dSJohn Baldwin 		 * reliably check to see if we hold a shared lock or
11324e7f640dSJohn Baldwin 		 * not.
1133f6739b1dSPawel Jakub Dawidek 		 */
11344e7f640dSJohn Baldwin 		if (sx_xholder(sx) == curthread)
113503129ba9SJohn Baldwin 			panic("Lock %s exclusively locked @ %s:%d\n",
1136aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
113719b0efd3SPawel Jakub Dawidek #endif
113819b0efd3SPawel Jakub Dawidek 		break;
11394e5e677bSJohn Baldwin 	default:
11404e5e677bSJohn Baldwin 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
11414e5e677bSJohn Baldwin 		    line);
11424e5e677bSJohn Baldwin 	}
11434e5e677bSJohn Baldwin }
11444e5e677bSJohn Baldwin #endif	/* INVARIANT_SUPPORT */
1145d272fe53SJohn Baldwin 
1146d272fe53SJohn Baldwin #ifdef DDB
11474e7f640dSJohn Baldwin static void
1148d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock)
1149d272fe53SJohn Baldwin {
1150d272fe53SJohn Baldwin 	struct thread *td;
1151d576deedSPawel Jakub Dawidek 	const struct sx *sx;
1152d272fe53SJohn Baldwin 
1153d576deedSPawel Jakub Dawidek 	sx = (const struct sx *)lock;
1154d272fe53SJohn Baldwin 
1155d272fe53SJohn Baldwin 	db_printf(" state: ");
11564e7f640dSJohn Baldwin 	if (sx->sx_lock == SX_LOCK_UNLOCKED)
11574e7f640dSJohn Baldwin 		db_printf("UNLOCKED\n");
11580026c92cSJohn Baldwin 	else if (sx->sx_lock == SX_LOCK_DESTROYED) {
11590026c92cSJohn Baldwin 		db_printf("DESTROYED\n");
11600026c92cSJohn Baldwin 		return;
11610026c92cSJohn Baldwin 	} else if (sx->sx_lock & SX_LOCK_SHARED)
11624e7f640dSJohn Baldwin 		db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
11634e7f640dSJohn Baldwin 	else {
11644e7f640dSJohn Baldwin 		td = sx_xholder(sx);
1165d272fe53SJohn Baldwin 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1166431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
11674e7f640dSJohn Baldwin 		if (sx_recursed(sx))
11684e7f640dSJohn Baldwin 			db_printf(" recursed: %d\n", sx->sx_recurse);
11694e7f640dSJohn Baldwin 	}
11704e7f640dSJohn Baldwin 
11714e7f640dSJohn Baldwin 	db_printf(" waiters: ");
11724e7f640dSJohn Baldwin 	switch(sx->sx_lock &
11734e7f640dSJohn Baldwin 	    (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
11744e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS:
11754e7f640dSJohn Baldwin 		db_printf("shared\n");
11764e7f640dSJohn Baldwin 		break;
11774e7f640dSJohn Baldwin 	case SX_LOCK_EXCLUSIVE_WAITERS:
11784e7f640dSJohn Baldwin 		db_printf("exclusive\n");
11794e7f640dSJohn Baldwin 		break;
11804e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
11814e7f640dSJohn Baldwin 		db_printf("exclusive and shared\n");
11824e7f640dSJohn Baldwin 		break;
11834e7f640dSJohn Baldwin 	default:
11844e7f640dSJohn Baldwin 		db_printf("none\n");
11854e7f640dSJohn Baldwin 	}
1186d272fe53SJohn Baldwin }
1187462a7addSJohn Baldwin 
1188462a7addSJohn Baldwin /*
1189462a7addSJohn Baldwin  * Check to see if a thread that is blocked on a sleep queue is actually
1190462a7addSJohn Baldwin  * blocked on an sx lock.  If so, output some details and return true.
1191462a7addSJohn Baldwin  * If the lock has an exclusive owner, return that in *ownerp.
1192462a7addSJohn Baldwin  */
1193462a7addSJohn Baldwin int
1194462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp)
1195462a7addSJohn Baldwin {
1196462a7addSJohn Baldwin 	struct sx *sx;
1197462a7addSJohn Baldwin 
1198462a7addSJohn Baldwin 	/*
11994e7f640dSJohn Baldwin 	 * Check to see if this thread is blocked on an sx lock.
12004e7f640dSJohn Baldwin 	 * First, we check the lock class.  If that is ok, then we
12014e7f640dSJohn Baldwin 	 * compare the lock name against the wait message.
1202462a7addSJohn Baldwin 	 */
12034e7f640dSJohn Baldwin 	sx = td->td_wchan;
12044e7f640dSJohn Baldwin 	if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
12054e7f640dSJohn Baldwin 	    sx->lock_object.lo_name != td->td_wmesg)
1206462a7addSJohn Baldwin 		return (0);
1207462a7addSJohn Baldwin 
1208462a7addSJohn Baldwin 	/* We think we have an sx lock, so output some details. */
1209462a7addSJohn Baldwin 	db_printf("blocked on sx \"%s\" ", td->td_wmesg);
12104e7f640dSJohn Baldwin 	*ownerp = sx_xholder(sx);
12114e7f640dSJohn Baldwin 	if (sx->sx_lock & SX_LOCK_SHARED)
12124e7f640dSJohn Baldwin 		db_printf("SLOCK (count %ju)\n",
12134e7f640dSJohn Baldwin 		    (uintmax_t)SX_SHARERS(sx->sx_lock));
12144e7f640dSJohn Baldwin 	else
1215462a7addSJohn Baldwin 		db_printf("XLOCK\n");
1216462a7addSJohn Baldwin 	return (1);
1217462a7addSJohn Baldwin }
1218d272fe53SJohn Baldwin #endif
1219