xref: /freebsd/sys/kern/kern_sx.c (revision fd07ddcf6fb1bf6fde30a92216e3cb4093c29175)
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>
532cba8dd3SJohn 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 |
212*fd07ddcfSDmitry Chagin 	    SX_NOPROFILE | SX_NOADAPTIVE | SX_NEW)) == 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;
228*fd07ddcfSDmitry Chagin 	if (opts & SX_NEW)
229*fd07ddcfSDmitry Chagin 		flags |= LO_NEW;
2304e7f640dSJohn Baldwin 
231f0830182SAttilio Rao 	flags |= opts & SX_NOADAPTIVE;
232b5fb43e5SJohn Baldwin 	lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
2334e7f640dSJohn Baldwin 	sx->sx_lock = SX_LOCK_UNLOCKED;
2344e7f640dSJohn Baldwin 	sx->sx_recurse = 0;
2356281b30aSJason Evans }
2366281b30aSJason Evans 
2376281b30aSJason Evans void
2386281b30aSJason Evans sx_destroy(struct sx *sx)
2396281b30aSJason Evans {
2406281b30aSJason Evans 
2414e7f640dSJohn Baldwin 	KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
2424e7f640dSJohn Baldwin 	KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
2430026c92cSJohn Baldwin 	sx->sx_lock = SX_LOCK_DESTROYED;
244aa89d8cdSJohn Baldwin 	lock_destroy(&sx->lock_object);
2456281b30aSJason Evans }
2466281b30aSJason Evans 
247f9819486SAttilio Rao int
248f9819486SAttilio Rao _sx_slock(struct sx *sx, int opts, const char *file, int line)
2496281b30aSJason Evans {
250f9819486SAttilio Rao 	int error = 0;
2516281b30aSJason Evans 
25235370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
25335370593SAndriy Gapon 		return (0);
254cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
255e3ae0dfeSAttilio Rao 	    ("sx_slock() by idle thread %p on sx %s @ %s:%d",
256e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
2570026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
2580026c92cSJohn Baldwin 	    ("sx_slock() of destroyed sx @ %s:%d", file, line));
25941313430SJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
260f9819486SAttilio Rao 	error = __sx_slock(sx, opts, file, line);
261f9819486SAttilio Rao 	if (!error) {
262aa89d8cdSJohn Baldwin 		LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
263aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, 0, file, line);
264764e4d54SJohn Baldwin 		curthread->td_locks++;
2656281b30aSJason Evans 	}
2666281b30aSJason Evans 
267f9819486SAttilio Rao 	return (error);
268f9819486SAttilio Rao }
269f9819486SAttilio Rao 
2705f36700aSJohn Baldwin int
2719fde98bbSAttilio Rao sx_try_slock_(struct sx *sx, const char *file, int line)
2725f36700aSJohn Baldwin {
2734e7f640dSJohn Baldwin 	uintptr_t x;
2745f36700aSJohn Baldwin 
27535370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
27635370593SAndriy Gapon 		return (1);
27735370593SAndriy Gapon 
278cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
279e3ae0dfeSAttilio Rao 	    ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
280e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
281e3ae0dfeSAttilio Rao 
282764a938bSPawel Jakub Dawidek 	for (;;) {
2834e7f640dSJohn Baldwin 		x = sx->sx_lock;
2840026c92cSJohn Baldwin 		KASSERT(x != SX_LOCK_DESTROYED,
2850026c92cSJohn Baldwin 		    ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
286764a938bSPawel Jakub Dawidek 		if (!(x & SX_LOCK_SHARED))
287764a938bSPawel Jakub Dawidek 			break;
288764a938bSPawel Jakub Dawidek 		if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
289aa89d8cdSJohn Baldwin 			LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
290aa89d8cdSJohn Baldwin 			WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
291764e4d54SJohn Baldwin 			curthread->td_locks++;
2925f36700aSJohn Baldwin 			return (1);
2935f36700aSJohn Baldwin 		}
294764a938bSPawel Jakub Dawidek 	}
2954e7f640dSJohn Baldwin 
2964e7f640dSJohn Baldwin 	LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
2974e7f640dSJohn Baldwin 	return (0);
2985f36700aSJohn Baldwin }
2995f36700aSJohn Baldwin 
300f9819486SAttilio Rao int
301f9819486SAttilio Rao _sx_xlock(struct sx *sx, int opts, const char *file, int line)
3026281b30aSJason Evans {
303f9819486SAttilio Rao 	int error = 0;
3046281b30aSJason Evans 
30535370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
30635370593SAndriy Gapon 		return (0);
307cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
308e3ae0dfeSAttilio Rao 	    ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
309e3ae0dfeSAttilio 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 
333cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
334e3ae0dfeSAttilio Rao 	    ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
335e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
3360026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3370026c92cSJohn Baldwin 	    ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
3384e7f640dSJohn Baldwin 
339f0830182SAttilio Rao 	if (sx_xlocked(sx) &&
340f0830182SAttilio Rao 	    (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
3414e7f640dSJohn Baldwin 		sx->sx_recurse++;
3424e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
3434e7f640dSJohn Baldwin 		rval = 1;
3444e7f640dSJohn Baldwin 	} else
3454e7f640dSJohn Baldwin 		rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
3464e7f640dSJohn Baldwin 		    (uintptr_t)curthread);
3474e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
3484e7f640dSJohn Baldwin 	if (rval) {
3494e7f640dSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
3504e7f640dSJohn Baldwin 		    file, line);
351764e4d54SJohn Baldwin 		curthread->td_locks++;
3525f36700aSJohn Baldwin 	}
3534e7f640dSJohn Baldwin 
3544e7f640dSJohn Baldwin 	return (rval);
3555f36700aSJohn Baldwin }
3565f36700aSJohn Baldwin 
3576281b30aSJason Evans void
35819284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line)
3596281b30aSJason Evans {
3606281b30aSJason Evans 
36135370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
36235370593SAndriy Gapon 		return;
3630026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3640026c92cSJohn Baldwin 	    ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
3657ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
366aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
367aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
3684e7f640dSJohn Baldwin 	__sx_sunlock(sx, file, line);
369b5fb43e5SJohn Baldwin 	curthread->td_locks--;
3706281b30aSJason Evans }
3716281b30aSJason Evans 
3726281b30aSJason Evans void
37319284646SJohn Baldwin _sx_xunlock(struct sx *sx, const char *file, int line)
3746281b30aSJason Evans {
3756281b30aSJason Evans 
37635370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
37735370593SAndriy Gapon 		return;
3780026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3790026c92cSJohn Baldwin 	    ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
3807ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED, file, line);
381aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
3824e7f640dSJohn Baldwin 	LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
3834e7f640dSJohn Baldwin 	    line);
3844e7f640dSJohn Baldwin 	__sx_xunlock(sx, curthread, file, line);
385b5fb43e5SJohn Baldwin 	curthread->td_locks--;
3866281b30aSJason Evans }
387d55229b7SJason Evans 
3884e7f640dSJohn Baldwin /*
3894e7f640dSJohn Baldwin  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
3904e7f640dSJohn Baldwin  * This will only succeed if this thread holds a single shared lock.
3914e7f640dSJohn Baldwin  * Return 1 if if the upgrade succeed, 0 otherwise.
3924e7f640dSJohn Baldwin  */
393d55229b7SJason Evans int
3949fde98bbSAttilio Rao sx_try_upgrade_(struct sx *sx, const char *file, int line)
395d55229b7SJason Evans {
3964e7f640dSJohn Baldwin 	uintptr_t x;
3974e7f640dSJohn Baldwin 	int success;
398d55229b7SJason Evans 
39935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
40035370593SAndriy Gapon 		return (1);
40135370593SAndriy Gapon 
4020026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4030026c92cSJohn Baldwin 	    ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
4047ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
405d55229b7SJason Evans 
4064e7f640dSJohn Baldwin 	/*
4074e7f640dSJohn Baldwin 	 * Try to switch from one shared lock to an exclusive lock.  We need
4084e7f640dSJohn Baldwin 	 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
4094e7f640dSJohn Baldwin 	 * we will wake up the exclusive waiters when we drop the lock.
4104e7f640dSJohn Baldwin 	 */
4114e7f640dSJohn Baldwin 	x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
4124e7f640dSJohn Baldwin 	success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
4134e7f640dSJohn Baldwin 	    (uintptr_t)curthread | x);
4144e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
415a5aedd68SStacey Son 	if (success) {
416aa89d8cdSJohn Baldwin 		WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
417b0b7cb50SJohn Baldwin 		    file, line);
418a5aedd68SStacey Son 		LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
419a5aedd68SStacey Son 	}
4204e7f640dSJohn Baldwin 	return (success);
421d55229b7SJason Evans }
422d55229b7SJason Evans 
4234e7f640dSJohn Baldwin /*
4244e7f640dSJohn Baldwin  * Downgrade an unrecursed exclusive lock into a single shared lock.
4254e7f640dSJohn Baldwin  */
426d55229b7SJason Evans void
4279fde98bbSAttilio Rao sx_downgrade_(struct sx *sx, const char *file, int line)
428d55229b7SJason Evans {
4294e7f640dSJohn Baldwin 	uintptr_t x;
430da7bbd2cSJohn Baldwin 	int wakeup_swapper;
431d55229b7SJason Evans 
43235370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
43335370593SAndriy Gapon 		return;
43435370593SAndriy Gapon 
4350026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4360026c92cSJohn Baldwin 	    ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
4377ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
4384e7f640dSJohn Baldwin #ifndef INVARIANTS
4394e7f640dSJohn Baldwin 	if (sx_recursed(sx))
4404e7f640dSJohn Baldwin 		panic("downgrade of a recursed lock");
4414e7f640dSJohn Baldwin #endif
442d55229b7SJason Evans 
443aa89d8cdSJohn Baldwin 	WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
444d55229b7SJason Evans 
4454e7f640dSJohn Baldwin 	/*
4464e7f640dSJohn Baldwin 	 * Try to switch from an exclusive lock with no shared waiters
4474e7f640dSJohn Baldwin 	 * to one sharer with no shared waiters.  If there are
4484e7f640dSJohn Baldwin 	 * exclusive waiters, we don't need to lock the sleep queue so
4494e7f640dSJohn Baldwin 	 * long as we preserve the flag.  We do one quick try and if
4504e7f640dSJohn Baldwin 	 * that fails we grab the sleepq lock to keep the flags from
4514e7f640dSJohn Baldwin 	 * changing and do it the slow way.
4524e7f640dSJohn Baldwin 	 *
4534e7f640dSJohn Baldwin 	 * We have to lock the sleep queue if there are shared waiters
4544e7f640dSJohn Baldwin 	 * so we can wake them up.
4554e7f640dSJohn Baldwin 	 */
4564e7f640dSJohn Baldwin 	x = sx->sx_lock;
4574e7f640dSJohn Baldwin 	if (!(x & SX_LOCK_SHARED_WAITERS) &&
4584e7f640dSJohn Baldwin 	    atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
4594e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
4604e7f640dSJohn Baldwin 		LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
4614e7f640dSJohn Baldwin 		return;
4624e7f640dSJohn Baldwin 	}
4634e7f640dSJohn Baldwin 
4644e7f640dSJohn Baldwin 	/*
4654e7f640dSJohn Baldwin 	 * Lock the sleep queue so we can read the waiters bits
4664e7f640dSJohn Baldwin 	 * without any races and wakeup any shared waiters.
4674e7f640dSJohn Baldwin 	 */
4684e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
4694e7f640dSJohn Baldwin 
4704e7f640dSJohn Baldwin 	/*
4714e7f640dSJohn Baldwin 	 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
4724e7f640dSJohn Baldwin 	 * shared lock.  If there are any shared waiters, wake them up.
4734e7f640dSJohn Baldwin 	 */
474da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
4754e7f640dSJohn Baldwin 	x = sx->sx_lock;
4764e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
4774e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS));
4784e7f640dSJohn Baldwin 	if (x & SX_LOCK_SHARED_WAITERS)
479da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
480da7bbd2cSJohn Baldwin 		    0, SQ_SHARED_QUEUE);
4814e7f640dSJohn Baldwin 	sleepq_release(&sx->lock_object);
482d55229b7SJason Evans 
483aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
484a5aedd68SStacey Son 	LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
485da7bbd2cSJohn Baldwin 
486da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
487da7bbd2cSJohn Baldwin 		kick_proc0();
4884e7f640dSJohn Baldwin }
489d55229b7SJason Evans 
4904e7f640dSJohn Baldwin /*
4914e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xlock
4924e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
4934e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
4944e7f640dSJohn Baldwin  * accessible from at least sx.h.
4954e7f640dSJohn Baldwin  */
496f9819486SAttilio Rao int
497f9819486SAttilio Rao _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
498f9819486SAttilio Rao     int line)
4994e7f640dSJohn Baldwin {
5004e7f640dSJohn Baldwin 	GIANT_DECLARE;
5014e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5024e7f640dSJohn Baldwin 	volatile struct thread *owner;
5031ae1c2a3SAttilio Rao 	u_int i, spintries = 0;
5044e7f640dSJohn Baldwin #endif
5054e7f640dSJohn Baldwin 	uintptr_t x;
5061723a064SJeff Roberson #ifdef LOCK_PROFILING
5071723a064SJeff Roberson 	uint64_t waittime = 0;
5081723a064SJeff Roberson 	int contested = 0;
5091723a064SJeff Roberson #endif
5101723a064SJeff Roberson 	int error = 0;
511a5aedd68SStacey Son #ifdef	KDTRACE_HOOKS
512a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
513a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
514a5aedd68SStacey Son 	int64_t sleep_time = 0;
515a5aedd68SStacey Son #endif
5164e7f640dSJohn Baldwin 
51735370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
51835370593SAndriy Gapon 		return (0);
51935370593SAndriy Gapon 
5204e7f640dSJohn Baldwin 	/* If we already hold an exclusive lock, then recurse. */
5214e7f640dSJohn Baldwin 	if (sx_xlocked(sx)) {
522f0830182SAttilio Rao 		KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
523b0d67325SJohn Baldwin 	    ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
524b0d67325SJohn Baldwin 		    sx->lock_object.lo_name, file, line));
5254e7f640dSJohn Baldwin 		sx->sx_recurse++;
5264e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
5274e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
5284e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
529f9819486SAttilio Rao 		return (0);
5304e7f640dSJohn Baldwin 	}
5314e7f640dSJohn Baldwin 
5324e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
5334e7f640dSJohn Baldwin 		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
5344e7f640dSJohn Baldwin 		    sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
5354e7f640dSJohn Baldwin 
5364e7f640dSJohn Baldwin 	while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
537a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
538a5aedd68SStacey Son 		spin_cnt++;
539a5aedd68SStacey Son #endif
540f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
541f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
542f5f9340bSFabien Thomas #endif
543eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
544eea4f254SJeff Roberson 		    &waittime);
5454e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5464e7f640dSJohn Baldwin 		/*
5474e7f640dSJohn Baldwin 		 * If the lock is write locked and the owner is
5484e7f640dSJohn Baldwin 		 * running on another CPU, spin until the owner stops
5494e7f640dSJohn Baldwin 		 * running or the state of the lock changes.
5504e7f640dSJohn Baldwin 		 */
5514e7f640dSJohn Baldwin 		x = sx->sx_lock;
5528545538bSJohn Baldwin 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
5531ae1c2a3SAttilio Rao 			if ((x & SX_LOCK_SHARED) == 0) {
5544e7f640dSJohn Baldwin 				x = SX_OWNER(x);
5554e7f640dSJohn Baldwin 				owner = (struct thread *)x;
5564e7f640dSJohn Baldwin 				if (TD_IS_RUNNING(owner)) {
5574e7f640dSJohn Baldwin 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5584e7f640dSJohn Baldwin 						CTR3(KTR_LOCK,
5594e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
5604e7f640dSJohn Baldwin 						    __func__, sx, owner);
5612cba8dd3SJohn Baldwin 					KTR_STATE1(KTR_SCHED, "thread",
5622cba8dd3SJohn Baldwin 					    sched_tdname(curthread), "spinning",
5632cba8dd3SJohn Baldwin 					    "lockname:\"%s\"",
5642cba8dd3SJohn Baldwin 					    sx->lock_object.lo_name);
5654e7f640dSJohn Baldwin 					GIANT_SAVE();
5664e7f640dSJohn Baldwin 					while (SX_OWNER(sx->sx_lock) == x &&
567a5aedd68SStacey Son 					    TD_IS_RUNNING(owner)) {
5684e7f640dSJohn Baldwin 						cpu_spinwait();
569a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
570a5aedd68SStacey Son 						spin_cnt++;
571a5aedd68SStacey Son #endif
572a5aedd68SStacey Son 					}
5732cba8dd3SJohn Baldwin 					KTR_STATE0(KTR_SCHED, "thread",
5742cba8dd3SJohn Baldwin 					    sched_tdname(curthread), "running");
5754e7f640dSJohn Baldwin 					continue;
5764e7f640dSJohn Baldwin 				}
5771ae1c2a3SAttilio Rao 			} else if (SX_SHARERS(x) && spintries < asx_retries) {
5782cba8dd3SJohn Baldwin 				KTR_STATE1(KTR_SCHED, "thread",
5792cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "spinning",
5802cba8dd3SJohn Baldwin 				    "lockname:\"%s\"", sx->lock_object.lo_name);
5818d3635c4SAttilio Rao 				GIANT_SAVE();
5821ae1c2a3SAttilio Rao 				spintries++;
5831ae1c2a3SAttilio Rao 				for (i = 0; i < asx_loops; i++) {
5841ae1c2a3SAttilio Rao 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5851ae1c2a3SAttilio Rao 						CTR4(KTR_LOCK,
5861ae1c2a3SAttilio Rao 				    "%s: shared spinning on %p with %u and %u",
5871ae1c2a3SAttilio Rao 						    __func__, sx, spintries, i);
5881ae1c2a3SAttilio Rao 					x = sx->sx_lock;
5891ae1c2a3SAttilio Rao 					if ((x & SX_LOCK_SHARED) == 0 ||
5901ae1c2a3SAttilio Rao 					    SX_SHARERS(x) == 0)
5911ae1c2a3SAttilio Rao 						break;
5921ae1c2a3SAttilio Rao 					cpu_spinwait();
5931ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS
5941ae1c2a3SAttilio Rao 					spin_cnt++;
5951ae1c2a3SAttilio Rao #endif
5961ae1c2a3SAttilio Rao 				}
5972cba8dd3SJohn Baldwin 				KTR_STATE0(KTR_SCHED, "thread",
5982cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "running");
5991ae1c2a3SAttilio Rao 				if (i != asx_loops)
6001ae1c2a3SAttilio Rao 					continue;
6011ae1c2a3SAttilio Rao 			}
6024e7f640dSJohn Baldwin 		}
6034e7f640dSJohn Baldwin #endif
6044e7f640dSJohn Baldwin 
6054e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
6064e7f640dSJohn Baldwin 		x = sx->sx_lock;
6074e7f640dSJohn Baldwin 
6084e7f640dSJohn Baldwin 		/*
6094e7f640dSJohn Baldwin 		 * If the lock was released while spinning on the
6104e7f640dSJohn Baldwin 		 * sleep queue chain lock, try again.
6114e7f640dSJohn Baldwin 		 */
6124e7f640dSJohn Baldwin 		if (x == SX_LOCK_UNLOCKED) {
6134e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6144e7f640dSJohn Baldwin 			continue;
6154e7f640dSJohn Baldwin 		}
6164e7f640dSJohn Baldwin 
6174e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
6184e7f640dSJohn Baldwin 		/*
6194e7f640dSJohn Baldwin 		 * The current lock owner might have started executing
6204e7f640dSJohn Baldwin 		 * on another CPU (or the lock could have changed
6214e7f640dSJohn Baldwin 		 * owners) while we were waiting on the sleep queue
6224e7f640dSJohn Baldwin 		 * chain lock.  If so, drop the sleep queue lock and try
6234e7f640dSJohn Baldwin 		 * again.
6244e7f640dSJohn Baldwin 		 */
6254e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
6261ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
6274e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
6284e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
6294e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6304e7f640dSJohn Baldwin 				continue;
6314e7f640dSJohn Baldwin 			}
6324e7f640dSJohn Baldwin 		}
6334e7f640dSJohn Baldwin #endif
6344e7f640dSJohn Baldwin 
6354e7f640dSJohn Baldwin 		/*
6364e7f640dSJohn Baldwin 		 * If an exclusive lock was released with both shared
6374e7f640dSJohn Baldwin 		 * and exclusive waiters and a shared waiter hasn't
6384e7f640dSJohn Baldwin 		 * woken up and acquired the lock yet, sx_lock will be
6394e7f640dSJohn Baldwin 		 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
6404e7f640dSJohn Baldwin 		 * If we see that value, try to acquire it once.  Note
6414e7f640dSJohn Baldwin 		 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
6424e7f640dSJohn Baldwin 		 * as there are other exclusive waiters still.  If we
6434e7f640dSJohn Baldwin 		 * fail, restart the loop.
6444e7f640dSJohn Baldwin 		 */
6454e7f640dSJohn Baldwin 		if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
6464e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock,
6474e7f640dSJohn Baldwin 			    SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
6484e7f640dSJohn Baldwin 			    tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
6494e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6504e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
6514e7f640dSJohn Baldwin 				    __func__, sx);
6524e7f640dSJohn Baldwin 				break;
6534e7f640dSJohn Baldwin 			}
6544e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6554e7f640dSJohn Baldwin 			continue;
6564e7f640dSJohn Baldwin 		}
6574e7f640dSJohn Baldwin 
6584e7f640dSJohn Baldwin 		/*
6594e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
6604e7f640dSJohn Baldwin 		 * than loop back and retry.
6614e7f640dSJohn Baldwin 		 */
6624e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
6634e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
6644e7f640dSJohn Baldwin 			    x | SX_LOCK_EXCLUSIVE_WAITERS)) {
6654e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6664e7f640dSJohn Baldwin 				continue;
6674e7f640dSJohn Baldwin 			}
6684e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
6694e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
6704e7f640dSJohn Baldwin 				    __func__, sx);
6714e7f640dSJohn Baldwin 		}
6724e7f640dSJohn Baldwin 
6734e7f640dSJohn Baldwin 		/*
6744e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the exclusive
6754e7f640dSJohn Baldwin 		 * lock and the exclusive waiters flag is set, we have
6764e7f640dSJohn Baldwin 		 * to sleep.
6774e7f640dSJohn Baldwin 		 */
6784e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6794e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
6804e7f640dSJohn Baldwin 			    __func__, sx);
6814e7f640dSJohn Baldwin 
682a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
683a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
684a5aedd68SStacey Son #endif
6854e7f640dSJohn Baldwin 		GIANT_SAVE();
6864e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
687f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
688f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
689f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
690c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
691f9819486SAttilio Rao 		else
692c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
693a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
694a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
695a5aedd68SStacey Son 		sleep_cnt++;
696a5aedd68SStacey Son #endif
697f9819486SAttilio Rao 		if (error) {
698f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
699f9819486SAttilio Rao 				CTR2(KTR_LOCK,
700f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
701f9819486SAttilio Rao 				    __func__, sx);
702f9819486SAttilio Rao 			break;
703f9819486SAttilio Rao 		}
7044e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7054e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
7064e7f640dSJohn Baldwin 			    __func__, sx);
7074e7f640dSJohn Baldwin 	}
7084e7f640dSJohn Baldwin 
7094e7f640dSJohn Baldwin 	GIANT_RESTORE();
710f9819486SAttilio Rao 	if (!error)
711a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
712a5aedd68SStacey Son 		    contested, waittime, file, line);
713a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
714a5aedd68SStacey Son 	if (sleep_time)
715a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
716a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
717a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
718a5aedd68SStacey Son #endif
719f9819486SAttilio Rao 	return (error);
7204e7f640dSJohn Baldwin }
7214e7f640dSJohn Baldwin 
7224e7f640dSJohn Baldwin /*
7234e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xunlock
7244e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7254e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7264e7f640dSJohn Baldwin  * accessible from at least sx.h.
7274e7f640dSJohn Baldwin  */
7284e7f640dSJohn Baldwin void
7294e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
7304e7f640dSJohn Baldwin {
7314e7f640dSJohn Baldwin 	uintptr_t x;
732da7bbd2cSJohn Baldwin 	int queue, wakeup_swapper;
7334e7f640dSJohn Baldwin 
73435370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
73535370593SAndriy Gapon 		return;
73635370593SAndriy Gapon 
7374e7f640dSJohn Baldwin 	MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
7384e7f640dSJohn Baldwin 
7394e7f640dSJohn Baldwin 	/* If the lock is recursed, then unrecurse one level. */
7404e7f640dSJohn Baldwin 	if (sx_xlocked(sx) && sx_recursed(sx)) {
7414e7f640dSJohn Baldwin 		if ((--sx->sx_recurse) == 0)
7424e7f640dSJohn Baldwin 			atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
7434e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7444e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
7454e7f640dSJohn Baldwin 		return;
7464e7f640dSJohn Baldwin 	}
7474e7f640dSJohn Baldwin 	MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
7484e7f640dSJohn Baldwin 	    SX_LOCK_EXCLUSIVE_WAITERS));
7494e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7504e7f640dSJohn Baldwin 		CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
7514e7f640dSJohn Baldwin 
7524e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
7534e7f640dSJohn Baldwin 	x = SX_LOCK_UNLOCKED;
7544e7f640dSJohn Baldwin 
7554e7f640dSJohn Baldwin 	/*
7564e7f640dSJohn Baldwin 	 * The wake up algorithm here is quite simple and probably not
7574e7f640dSJohn Baldwin 	 * ideal.  It gives precedence to shared waiters if they are
7584e7f640dSJohn Baldwin 	 * present.  For this condition, we have to preserve the
7594e7f640dSJohn Baldwin 	 * state of the exclusive waiters flag.
7602028867dSAttilio Rao 	 * If interruptible sleeps left the shared queue empty avoid a
7612028867dSAttilio Rao 	 * starvation for the threads sleeping on the exclusive queue by giving
7622028867dSAttilio Rao 	 * them precedence and cleaning up the shared waiters bit anyway.
7634e7f640dSJohn Baldwin 	 */
7642028867dSAttilio Rao 	if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
7652028867dSAttilio Rao 	    sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
7664e7f640dSJohn Baldwin 		queue = SQ_SHARED_QUEUE;
7674e7f640dSJohn Baldwin 		x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
7684e7f640dSJohn Baldwin 	} else
7694e7f640dSJohn Baldwin 		queue = SQ_EXCLUSIVE_QUEUE;
7704e7f640dSJohn Baldwin 
7714e7f640dSJohn Baldwin 	/* Wake up all the waiters for the specific queue. */
7724e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7734e7f640dSJohn Baldwin 		CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
7744e7f640dSJohn Baldwin 		    __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
7754e7f640dSJohn Baldwin 		    "exclusive");
7764e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, x);
777da7bbd2cSJohn Baldwin 	wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
778da7bbd2cSJohn Baldwin 	    queue);
779c5aa6b58SJeff Roberson 	sleepq_release(&sx->lock_object);
780da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
781da7bbd2cSJohn Baldwin 		kick_proc0();
7824e7f640dSJohn Baldwin }
7834e7f640dSJohn Baldwin 
7844e7f640dSJohn Baldwin /*
7854e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_slock
7864e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7874e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7884e7f640dSJohn Baldwin  * accessible from at least sx.h.
7894e7f640dSJohn Baldwin  */
790f9819486SAttilio Rao int
791f9819486SAttilio Rao _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
7924e7f640dSJohn Baldwin {
7934e7f640dSJohn Baldwin 	GIANT_DECLARE;
7944e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
7954e7f640dSJohn Baldwin 	volatile struct thread *owner;
7964e7f640dSJohn Baldwin #endif
7971723a064SJeff Roberson #ifdef LOCK_PROFILING
798c1a6d9faSAttilio Rao 	uint64_t waittime = 0;
799c1a6d9faSAttilio Rao 	int contested = 0;
8001723a064SJeff Roberson #endif
8014e7f640dSJohn Baldwin 	uintptr_t x;
802c1a6d9faSAttilio Rao 	int error = 0;
803a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
804a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
805a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
806a5aedd68SStacey Son 	int64_t sleep_time = 0;
807a5aedd68SStacey Son #endif
808c1a6d9faSAttilio Rao 
80935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
81035370593SAndriy Gapon 		return (0);
81135370593SAndriy Gapon 
8124e7f640dSJohn Baldwin 	/*
8134e7f640dSJohn Baldwin 	 * As with rwlocks, we don't make any attempt to try to block
8144e7f640dSJohn Baldwin 	 * shared locks once there is an exclusive waiter.
8154e7f640dSJohn Baldwin 	 */
8164e7f640dSJohn Baldwin 	for (;;) {
817a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
818a5aedd68SStacey Son 		spin_cnt++;
819a5aedd68SStacey Son #endif
8204e7f640dSJohn Baldwin 		x = sx->sx_lock;
8214e7f640dSJohn Baldwin 
8224e7f640dSJohn Baldwin 		/*
8234e7f640dSJohn Baldwin 		 * If no other thread has an exclusive lock then try to bump up
8244e7f640dSJohn Baldwin 		 * the count of sharers.  Since we have to preserve the state
8254e7f640dSJohn Baldwin 		 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
8264e7f640dSJohn Baldwin 		 * shared lock loop back and retry.
8274e7f640dSJohn Baldwin 		 */
8284e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8294e7f640dSJohn Baldwin 			MPASS(!(x & SX_LOCK_SHARED_WAITERS));
8304e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
8314e7f640dSJohn Baldwin 			    x + SX_ONE_SHARER)) {
8324e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8334e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
8344e7f640dSJohn Baldwin 					    "%s: %p succeed %p -> %p", __func__,
8354e7f640dSJohn Baldwin 					    sx, (void *)x,
8364e7f640dSJohn Baldwin 					    (void *)(x + SX_ONE_SHARER));
8374e7f640dSJohn Baldwin 				break;
8384e7f640dSJohn Baldwin 			}
8394e7f640dSJohn Baldwin 			continue;
8404e7f640dSJohn Baldwin 		}
841f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
842f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
843f5f9340bSFabien Thomas #endif
844eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
845eea4f254SJeff Roberson 		    &waittime);
8464e7f640dSJohn Baldwin 
8474e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8484e7f640dSJohn Baldwin 		/*
8494e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8504e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8514e7f640dSJohn Baldwin 		 * changes.
8524e7f640dSJohn Baldwin 		 */
8531ae1c2a3SAttilio Rao 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8544e7f640dSJohn Baldwin 			x = SX_OWNER(x);
8554e7f640dSJohn Baldwin 			owner = (struct thread *)x;
8564e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8574e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8584e7f640dSJohn Baldwin 					CTR3(KTR_LOCK,
8594e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
8604e7f640dSJohn Baldwin 					    __func__, sx, owner);
8612cba8dd3SJohn Baldwin 				KTR_STATE1(KTR_SCHED, "thread",
8622cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "spinning",
8632cba8dd3SJohn Baldwin 				    "lockname:\"%s\"", sx->lock_object.lo_name);
8644e7f640dSJohn Baldwin 				GIANT_SAVE();
8654e7f640dSJohn Baldwin 				while (SX_OWNER(sx->sx_lock) == x &&
866a5aedd68SStacey Son 				    TD_IS_RUNNING(owner)) {
867a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
868a5aedd68SStacey Son 					spin_cnt++;
869a5aedd68SStacey Son #endif
8704e7f640dSJohn Baldwin 					cpu_spinwait();
871a5aedd68SStacey Son 				}
8722cba8dd3SJohn Baldwin 				KTR_STATE0(KTR_SCHED, "thread",
8732cba8dd3SJohn Baldwin 				    sched_tdname(curthread), "running");
8744e7f640dSJohn Baldwin 				continue;
8754e7f640dSJohn Baldwin 			}
8764e7f640dSJohn Baldwin 		}
8774e7f640dSJohn Baldwin #endif
8784e7f640dSJohn Baldwin 
8794e7f640dSJohn Baldwin 		/*
8804e7f640dSJohn Baldwin 		 * Some other thread already has an exclusive lock, so
8814e7f640dSJohn Baldwin 		 * start the process of blocking.
8824e7f640dSJohn Baldwin 		 */
8834e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
8844e7f640dSJohn Baldwin 		x = sx->sx_lock;
8854e7f640dSJohn Baldwin 
8864e7f640dSJohn Baldwin 		/*
8874e7f640dSJohn Baldwin 		 * The lock could have been released while we spun.
8884e7f640dSJohn Baldwin 		 * In this case loop back and retry.
8894e7f640dSJohn Baldwin 		 */
8904e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8914e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
8924e7f640dSJohn Baldwin 			continue;
8934e7f640dSJohn Baldwin 		}
8944e7f640dSJohn Baldwin 
8954e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8964e7f640dSJohn Baldwin 		/*
8974e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8984e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8994e7f640dSJohn Baldwin 		 * changes.
9004e7f640dSJohn Baldwin 		 */
9014e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
9021ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
9034e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
9044e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
9054e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
9064e7f640dSJohn Baldwin 				continue;
9074e7f640dSJohn Baldwin 			}
9084e7f640dSJohn Baldwin 		}
9094e7f640dSJohn Baldwin #endif
9104e7f640dSJohn Baldwin 
9114e7f640dSJohn Baldwin 		/*
9124e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
9134e7f640dSJohn Baldwin 		 * fail to set it drop the sleep queue lock and loop
9144e7f640dSJohn Baldwin 		 * back.
9154e7f640dSJohn Baldwin 		 */
9164e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED_WAITERS)) {
9174e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
9184e7f640dSJohn Baldwin 			    x | SX_LOCK_SHARED_WAITERS)) {
9194e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
9204e7f640dSJohn Baldwin 				continue;
9214e7f640dSJohn Baldwin 			}
9224e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
9234e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
9244e7f640dSJohn Baldwin 				    __func__, sx);
9254e7f640dSJohn Baldwin 		}
9264e7f640dSJohn Baldwin 
9274e7f640dSJohn Baldwin 		/*
9284e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the shared lock,
9294e7f640dSJohn Baldwin 		 * we have to sleep.
9304e7f640dSJohn Baldwin 		 */
9314e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9324e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
9334e7f640dSJohn Baldwin 			    __func__, sx);
9344e7f640dSJohn Baldwin 
935a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
936a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
937a5aedd68SStacey Son #endif
9384e7f640dSJohn Baldwin 		GIANT_SAVE();
9394e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
940f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
941f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
942f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
943c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
944f9819486SAttilio Rao 		else
945c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
946a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
947a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
948a5aedd68SStacey Son 		sleep_cnt++;
949a5aedd68SStacey Son #endif
950f9819486SAttilio Rao 		if (error) {
951f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
952f9819486SAttilio Rao 				CTR2(KTR_LOCK,
953f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
954f9819486SAttilio Rao 				    __func__, sx);
955f9819486SAttilio Rao 			break;
956f9819486SAttilio Rao 		}
9574e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9584e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
9594e7f640dSJohn Baldwin 			    __func__, sx);
9604e7f640dSJohn Baldwin 	}
961eea4f254SJeff Roberson 	if (error == 0)
962a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
963a5aedd68SStacey Son 		    contested, waittime, file, line);
964a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
965a5aedd68SStacey Son 	if (sleep_time)
966a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
967a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
968a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
969a5aedd68SStacey Son #endif
9704e7f640dSJohn Baldwin 	GIANT_RESTORE();
971f9819486SAttilio Rao 	return (error);
9724e7f640dSJohn Baldwin }
9734e7f640dSJohn Baldwin 
9744e7f640dSJohn Baldwin /*
9754e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_sunlock
9764e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
9774e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
9784e7f640dSJohn Baldwin  * accessible from at least sx.h.
9794e7f640dSJohn Baldwin  */
9804e7f640dSJohn Baldwin void
9814e7f640dSJohn Baldwin _sx_sunlock_hard(struct sx *sx, const char *file, int line)
9824e7f640dSJohn Baldwin {
9834e7f640dSJohn Baldwin 	uintptr_t x;
984da7bbd2cSJohn Baldwin 	int wakeup_swapper;
9854e7f640dSJohn Baldwin 
98635370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
98735370593SAndriy Gapon 		return;
98835370593SAndriy Gapon 
9894e7f640dSJohn Baldwin 	for (;;) {
9904e7f640dSJohn Baldwin 		x = sx->sx_lock;
9914e7f640dSJohn Baldwin 
9924e7f640dSJohn Baldwin 		/*
9934e7f640dSJohn Baldwin 		 * We should never have sharers while at least one thread
9944e7f640dSJohn Baldwin 		 * holds a shared lock.
9954e7f640dSJohn Baldwin 		 */
9964e7f640dSJohn Baldwin 		KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
9974e7f640dSJohn Baldwin 		    ("%s: waiting sharers", __func__));
9984e7f640dSJohn Baldwin 
9994e7f640dSJohn Baldwin 		/*
10004e7f640dSJohn Baldwin 		 * See if there is more than one shared lock held.  If
10014e7f640dSJohn Baldwin 		 * so, just drop one and return.
10024e7f640dSJohn Baldwin 		 */
10034e7f640dSJohn Baldwin 		if (SX_SHARERS(x) > 1) {
1004ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
10054e7f640dSJohn Baldwin 			    x - SX_ONE_SHARER)) {
10064e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
10074e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
10084e7f640dSJohn Baldwin 					    "%s: %p succeeded %p -> %p",
10094e7f640dSJohn Baldwin 					    __func__, sx, (void *)x,
10104e7f640dSJohn Baldwin 					    (void *)(x - SX_ONE_SHARER));
10114e7f640dSJohn Baldwin 				break;
10124e7f640dSJohn Baldwin 			}
10134e7f640dSJohn Baldwin 			continue;
10144e7f640dSJohn Baldwin 		}
10154e7f640dSJohn Baldwin 
10164e7f640dSJohn Baldwin 		/*
10174e7f640dSJohn Baldwin 		 * If there aren't any waiters for an exclusive lock,
10184e7f640dSJohn Baldwin 		 * then try to drop it quickly.
10194e7f640dSJohn Baldwin 		 */
10204e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
10214e7f640dSJohn Baldwin 			MPASS(x == SX_SHARERS_LOCK(1));
1022ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1023ddce63caSAttilio Rao 			    SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
10244e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
10254e7f640dSJohn Baldwin 					CTR2(KTR_LOCK, "%s: %p last succeeded",
10264e7f640dSJohn Baldwin 					    __func__, sx);
10274e7f640dSJohn Baldwin 				break;
10284e7f640dSJohn Baldwin 			}
10294e7f640dSJohn Baldwin 			continue;
10304e7f640dSJohn Baldwin 		}
10314e7f640dSJohn Baldwin 
10324e7f640dSJohn Baldwin 		/*
10334e7f640dSJohn Baldwin 		 * At this point, there should just be one sharer with
10344e7f640dSJohn Baldwin 		 * exclusive waiters.
10354e7f640dSJohn Baldwin 		 */
10364e7f640dSJohn Baldwin 		MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
10374e7f640dSJohn Baldwin 
10384e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
10394e7f640dSJohn Baldwin 
10404e7f640dSJohn Baldwin 		/*
10414e7f640dSJohn Baldwin 		 * Wake up semantic here is quite simple:
10424e7f640dSJohn Baldwin 		 * Just wake up all the exclusive waiters.
10434e7f640dSJohn Baldwin 		 * Note that the state of the lock could have changed,
10444e7f640dSJohn Baldwin 		 * so if it fails loop back and retry.
10454e7f640dSJohn Baldwin 		 */
1046ddce63caSAttilio Rao 		if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
10474e7f640dSJohn Baldwin 		    SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
10484e7f640dSJohn Baldwin 		    SX_LOCK_UNLOCKED)) {
10494e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
10504e7f640dSJohn Baldwin 			continue;
10514e7f640dSJohn Baldwin 		}
10524e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
10534e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p waking up all thread on"
10544e7f640dSJohn Baldwin 			    "exclusive queue", __func__, sx);
1055da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1056da7bbd2cSJohn Baldwin 		    0, SQ_EXCLUSIVE_QUEUE);
1057c5aa6b58SJeff Roberson 		sleepq_release(&sx->lock_object);
1058da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
1059da7bbd2cSJohn Baldwin 			kick_proc0();
10604e7f640dSJohn Baldwin 		break;
10614e7f640dSJohn Baldwin 	}
1062d55229b7SJason Evans }
10634e5e677bSJohn Baldwin 
10644e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT
1065781a35dfSJohn Baldwin #ifndef INVARIANTS
1066781a35dfSJohn Baldwin #undef	_sx_assert
1067781a35dfSJohn Baldwin #endif
1068781a35dfSJohn Baldwin 
10694e5e677bSJohn Baldwin /*
10704e5e677bSJohn Baldwin  * In the non-WITNESS case, sx_assert() can only detect that at least
10714e5e677bSJohn Baldwin  * *some* thread owns an slock, but it cannot guarantee that *this*
10724e5e677bSJohn Baldwin  * thread owns an slock.
10734e5e677bSJohn Baldwin  */
10744e5e677bSJohn Baldwin void
1075d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line)
10764e5e677bSJohn Baldwin {
10774e7f640dSJohn Baldwin #ifndef WITNESS
10784e7f640dSJohn Baldwin 	int slocked = 0;
10794e7f640dSJohn Baldwin #endif
10804e5e677bSJohn Baldwin 
108103129ba9SJohn Baldwin 	if (panicstr != NULL)
108203129ba9SJohn Baldwin 		return;
10834e5e677bSJohn Baldwin 	switch (what) {
10847ec137e5SJohn Baldwin 	case SA_SLOCKED:
10857ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_NOTRECURSED:
10867ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_RECURSED:
10874e7f640dSJohn Baldwin #ifndef WITNESS
10884e7f640dSJohn Baldwin 		slocked = 1;
10894e7f640dSJohn Baldwin 		/* FALLTHROUGH */
10904e7f640dSJohn Baldwin #endif
10917ec137e5SJohn Baldwin 	case SA_LOCKED:
10927ec137e5SJohn Baldwin 	case SA_LOCKED | SA_NOTRECURSED:
10937ec137e5SJohn Baldwin 	case SA_LOCKED | SA_RECURSED:
10944e5e677bSJohn Baldwin #ifdef WITNESS
1095aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
10964e5e677bSJohn Baldwin #else
10974e7f640dSJohn Baldwin 		/*
10984e7f640dSJohn Baldwin 		 * If some other thread has an exclusive lock or we
10994e7f640dSJohn Baldwin 		 * have one and are asserting a shared lock, fail.
11004e7f640dSJohn Baldwin 		 * Also, if no one has a lock at all, fail.
11014e7f640dSJohn Baldwin 		 */
11024e7f640dSJohn Baldwin 		if (sx->sx_lock == SX_LOCK_UNLOCKED ||
11034e7f640dSJohn Baldwin 		    (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
11044e7f640dSJohn Baldwin 		    sx_xholder(sx) != curthread)))
110503129ba9SJohn Baldwin 			panic("Lock %s not %slocked @ %s:%d\n",
11064e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, slocked ? "share " : "",
11074e7f640dSJohn Baldwin 			    file, line);
11084e7f640dSJohn Baldwin 
11094e7f640dSJohn Baldwin 		if (!(sx->sx_lock & SX_LOCK_SHARED)) {
11104e7f640dSJohn Baldwin 			if (sx_recursed(sx)) {
11117ec137e5SJohn Baldwin 				if (what & SA_NOTRECURSED)
11124e7f640dSJohn Baldwin 					panic("Lock %s recursed @ %s:%d\n",
11134e7f640dSJohn Baldwin 					    sx->lock_object.lo_name, file,
11144e7f640dSJohn Baldwin 					    line);
11157ec137e5SJohn Baldwin 			} else if (what & SA_RECURSED)
11164e7f640dSJohn Baldwin 				panic("Lock %s not recursed @ %s:%d\n",
11174e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
11184e7f640dSJohn Baldwin 		}
11194e5e677bSJohn Baldwin #endif
11204e5e677bSJohn Baldwin 		break;
11217ec137e5SJohn Baldwin 	case SA_XLOCKED:
11227ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_NOTRECURSED:
11237ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_RECURSED:
11244e7f640dSJohn Baldwin 		if (sx_xholder(sx) != curthread)
112503129ba9SJohn Baldwin 			panic("Lock %s not exclusively locked @ %s:%d\n",
1126aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11274e7f640dSJohn Baldwin 		if (sx_recursed(sx)) {
11287ec137e5SJohn Baldwin 			if (what & SA_NOTRECURSED)
11294e7f640dSJohn Baldwin 				panic("Lock %s recursed @ %s:%d\n",
11304e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
11317ec137e5SJohn Baldwin 		} else if (what & SA_RECURSED)
11324e7f640dSJohn Baldwin 			panic("Lock %s not recursed @ %s:%d\n",
11334e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11344e5e677bSJohn Baldwin 		break;
11357ec137e5SJohn Baldwin 	case SA_UNLOCKED:
113619b0efd3SPawel Jakub Dawidek #ifdef WITNESS
1137aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
113819b0efd3SPawel Jakub Dawidek #else
1139f6739b1dSPawel Jakub Dawidek 		/*
11404e7f640dSJohn Baldwin 		 * If we hold an exclusve lock fail.  We can't
11414e7f640dSJohn Baldwin 		 * reliably check to see if we hold a shared lock or
11424e7f640dSJohn Baldwin 		 * not.
1143f6739b1dSPawel Jakub Dawidek 		 */
11444e7f640dSJohn Baldwin 		if (sx_xholder(sx) == curthread)
114503129ba9SJohn Baldwin 			panic("Lock %s exclusively locked @ %s:%d\n",
1146aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
114719b0efd3SPawel Jakub Dawidek #endif
114819b0efd3SPawel Jakub Dawidek 		break;
11494e5e677bSJohn Baldwin 	default:
11504e5e677bSJohn Baldwin 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
11514e5e677bSJohn Baldwin 		    line);
11524e5e677bSJohn Baldwin 	}
11534e5e677bSJohn Baldwin }
11544e5e677bSJohn Baldwin #endif	/* INVARIANT_SUPPORT */
1155d272fe53SJohn Baldwin 
1156d272fe53SJohn Baldwin #ifdef DDB
11574e7f640dSJohn Baldwin static void
1158d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock)
1159d272fe53SJohn Baldwin {
1160d272fe53SJohn Baldwin 	struct thread *td;
1161d576deedSPawel Jakub Dawidek 	const struct sx *sx;
1162d272fe53SJohn Baldwin 
1163d576deedSPawel Jakub Dawidek 	sx = (const struct sx *)lock;
1164d272fe53SJohn Baldwin 
1165d272fe53SJohn Baldwin 	db_printf(" state: ");
11664e7f640dSJohn Baldwin 	if (sx->sx_lock == SX_LOCK_UNLOCKED)
11674e7f640dSJohn Baldwin 		db_printf("UNLOCKED\n");
11680026c92cSJohn Baldwin 	else if (sx->sx_lock == SX_LOCK_DESTROYED) {
11690026c92cSJohn Baldwin 		db_printf("DESTROYED\n");
11700026c92cSJohn Baldwin 		return;
11710026c92cSJohn Baldwin 	} else if (sx->sx_lock & SX_LOCK_SHARED)
11724e7f640dSJohn Baldwin 		db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
11734e7f640dSJohn Baldwin 	else {
11744e7f640dSJohn Baldwin 		td = sx_xholder(sx);
1175d272fe53SJohn Baldwin 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1176431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
11774e7f640dSJohn Baldwin 		if (sx_recursed(sx))
11784e7f640dSJohn Baldwin 			db_printf(" recursed: %d\n", sx->sx_recurse);
11794e7f640dSJohn Baldwin 	}
11804e7f640dSJohn Baldwin 
11814e7f640dSJohn Baldwin 	db_printf(" waiters: ");
11824e7f640dSJohn Baldwin 	switch(sx->sx_lock &
11834e7f640dSJohn Baldwin 	    (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
11844e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS:
11854e7f640dSJohn Baldwin 		db_printf("shared\n");
11864e7f640dSJohn Baldwin 		break;
11874e7f640dSJohn Baldwin 	case SX_LOCK_EXCLUSIVE_WAITERS:
11884e7f640dSJohn Baldwin 		db_printf("exclusive\n");
11894e7f640dSJohn Baldwin 		break;
11904e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
11914e7f640dSJohn Baldwin 		db_printf("exclusive and shared\n");
11924e7f640dSJohn Baldwin 		break;
11934e7f640dSJohn Baldwin 	default:
11944e7f640dSJohn Baldwin 		db_printf("none\n");
11954e7f640dSJohn Baldwin 	}
1196d272fe53SJohn Baldwin }
1197462a7addSJohn Baldwin 
1198462a7addSJohn Baldwin /*
1199462a7addSJohn Baldwin  * Check to see if a thread that is blocked on a sleep queue is actually
1200462a7addSJohn Baldwin  * blocked on an sx lock.  If so, output some details and return true.
1201462a7addSJohn Baldwin  * If the lock has an exclusive owner, return that in *ownerp.
1202462a7addSJohn Baldwin  */
1203462a7addSJohn Baldwin int
1204462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp)
1205462a7addSJohn Baldwin {
1206462a7addSJohn Baldwin 	struct sx *sx;
1207462a7addSJohn Baldwin 
1208462a7addSJohn Baldwin 	/*
12094e7f640dSJohn Baldwin 	 * Check to see if this thread is blocked on an sx lock.
12104e7f640dSJohn Baldwin 	 * First, we check the lock class.  If that is ok, then we
12114e7f640dSJohn Baldwin 	 * compare the lock name against the wait message.
1212462a7addSJohn Baldwin 	 */
12134e7f640dSJohn Baldwin 	sx = td->td_wchan;
12144e7f640dSJohn Baldwin 	if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
12154e7f640dSJohn Baldwin 	    sx->lock_object.lo_name != td->td_wmesg)
1216462a7addSJohn Baldwin 		return (0);
1217462a7addSJohn Baldwin 
1218462a7addSJohn Baldwin 	/* We think we have an sx lock, so output some details. */
1219462a7addSJohn Baldwin 	db_printf("blocked on sx \"%s\" ", td->td_wmesg);
12204e7f640dSJohn Baldwin 	*ownerp = sx_xholder(sx);
12214e7f640dSJohn Baldwin 	if (sx->sx_lock & SX_LOCK_SHARED)
12224e7f640dSJohn Baldwin 		db_printf("SLOCK (count %ju)\n",
12234e7f640dSJohn Baldwin 		    (uintmax_t)SX_SHARERS(sx->sx_lock));
12244e7f640dSJohn Baldwin 	else
1225462a7addSJohn Baldwin 		db_printf("XLOCK\n");
1226462a7addSJohn Baldwin 	return (1);
1227462a7addSJohn Baldwin }
1228d272fe53SJohn Baldwin #endif
1229