xref: /freebsd/sys/kern/kern_sx.c (revision b5fb43e5727b6307531d8df908ca32882ce60f6d)
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>
49cd2fe4e6SAttilio Rao #include <sys/kdb.h>
506281b30aSJason Evans #include <sys/ktr.h>
5119284646SJohn Baldwin #include <sys/lock.h>
526281b30aSJason Evans #include <sys/mutex.h>
53d272fe53SJohn Baldwin #include <sys/proc.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  */
11290356491SAttilio Rao #define	sx_recurse		lock_object.lo_data
1134e7f640dSJohn Baldwin #define	sx_recursed(sx)		((sx)->sx_recurse != 0)
1144e7f640dSJohn Baldwin 
115d576deedSPawel Jakub Dawidek static void	assert_sx(const struct lock_object *lock, int what);
1164e7f640dSJohn Baldwin #ifdef DDB
117d576deedSPawel Jakub Dawidek static void	db_show_sx(const struct lock_object *lock);
118d272fe53SJohn Baldwin #endif
1196e21afd4SJohn Baldwin static void	lock_sx(struct lock_object *lock, int how);
120a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
121d576deedSPawel Jakub Dawidek static int	owner_sx(const struct lock_object *lock, struct thread **owner);
122a5aedd68SStacey Son #endif
1236e21afd4SJohn Baldwin static int	unlock_sx(struct lock_object *lock);
124d272fe53SJohn Baldwin 
12519284646SJohn Baldwin struct lock_class lock_class_sx = {
126ae8dde30SJohn Baldwin 	.lc_name = "sx",
127ae8dde30SJohn Baldwin 	.lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
128f9721b43SAttilio Rao 	.lc_assert = assert_sx,
129d272fe53SJohn Baldwin #ifdef DDB
130ae8dde30SJohn Baldwin 	.lc_ddb_show = db_show_sx,
131d272fe53SJohn Baldwin #endif
1326e21afd4SJohn Baldwin 	.lc_lock = lock_sx,
1336e21afd4SJohn Baldwin 	.lc_unlock = unlock_sx,
134a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
135a5aedd68SStacey Son 	.lc_owner = owner_sx,
136a5aedd68SStacey Son #endif
13719284646SJohn Baldwin };
13819284646SJohn Baldwin 
139781a35dfSJohn Baldwin #ifndef INVARIANTS
140781a35dfSJohn Baldwin #define	_sx_assert(sx, what, file, line)
141781a35dfSJohn Baldwin #endif
142781a35dfSJohn Baldwin 
1431ae1c2a3SAttilio Rao #ifdef ADAPTIVE_SX
1441ae1c2a3SAttilio Rao static u_int asx_retries = 10;
1451ae1c2a3SAttilio Rao static u_int asx_loops = 10000;
1466472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
147fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
148fbbb13f9SMatthew D Fleming SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
1491ae1c2a3SAttilio Rao #endif
1501ae1c2a3SAttilio Rao 
1516281b30aSJason Evans void
152d576deedSPawel Jakub Dawidek assert_sx(const struct lock_object *lock, int what)
153f9721b43SAttilio Rao {
154f9721b43SAttilio Rao 
155d576deedSPawel Jakub Dawidek 	sx_assert((const struct sx *)lock, what);
156f9721b43SAttilio Rao }
157f9721b43SAttilio Rao 
158f9721b43SAttilio Rao void
1596e21afd4SJohn Baldwin lock_sx(struct lock_object *lock, int how)
1606e21afd4SJohn Baldwin {
1616e21afd4SJohn Baldwin 	struct sx *sx;
1626e21afd4SJohn Baldwin 
1636e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1646e21afd4SJohn Baldwin 	if (how)
1656e21afd4SJohn Baldwin 		sx_xlock(sx);
1666e21afd4SJohn Baldwin 	else
1676e21afd4SJohn Baldwin 		sx_slock(sx);
1686e21afd4SJohn Baldwin }
1696e21afd4SJohn Baldwin 
1706e21afd4SJohn Baldwin int
1716e21afd4SJohn Baldwin unlock_sx(struct lock_object *lock)
1726e21afd4SJohn Baldwin {
1736e21afd4SJohn Baldwin 	struct sx *sx;
1746e21afd4SJohn Baldwin 
1756e21afd4SJohn Baldwin 	sx = (struct sx *)lock;
1767ec137e5SJohn Baldwin 	sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
1776e21afd4SJohn Baldwin 	if (sx_xlocked(sx)) {
1786e21afd4SJohn Baldwin 		sx_xunlock(sx);
1796e21afd4SJohn Baldwin 		return (1);
1806e21afd4SJohn Baldwin 	} else {
1816e21afd4SJohn Baldwin 		sx_sunlock(sx);
1826e21afd4SJohn Baldwin 		return (0);
1836e21afd4SJohn Baldwin 	}
1846e21afd4SJohn Baldwin }
1856e21afd4SJohn Baldwin 
186a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
187a5aedd68SStacey Son int
188d576deedSPawel Jakub Dawidek owner_sx(const struct lock_object *lock, struct thread **owner)
189a5aedd68SStacey Son {
190d576deedSPawel Jakub Dawidek         const struct sx *sx = (const struct sx *)lock;
191a5aedd68SStacey Son 	uintptr_t x = sx->sx_lock;
192a5aedd68SStacey Son 
193a5aedd68SStacey Son         *owner = (struct thread *)SX_OWNER(x);
194a5aedd68SStacey Son         return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
195a5aedd68SStacey Son 	    (*owner != NULL));
196a5aedd68SStacey Son }
197a5aedd68SStacey Son #endif
198a5aedd68SStacey Son 
1996e21afd4SJohn Baldwin void
200c27b5699SAndrew R. Reiter sx_sysinit(void *arg)
201c27b5699SAndrew R. Reiter {
202c27b5699SAndrew R. Reiter 	struct sx_args *sargs = arg;
203c27b5699SAndrew R. Reiter 
204e4cd31ddSJeff Roberson 	sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
205c27b5699SAndrew R. Reiter }
206c27b5699SAndrew R. Reiter 
207c27b5699SAndrew R. Reiter void
2084e7f640dSJohn Baldwin sx_init_flags(struct sx *sx, const char *description, int opts)
2096281b30aSJason Evans {
2104e7f640dSJohn Baldwin 	int flags;
2116281b30aSJason Evans 
212b0d67325SJohn Baldwin 	MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
2131ae1c2a3SAttilio Rao 	    SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
214353998acSAttilio Rao 	ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
215353998acSAttilio Rao 	    ("%s: sx_lock not aligned for %s: %p", __func__, description,
216353998acSAttilio Rao 	    &sx->sx_lock));
217b0d67325SJohn Baldwin 
218f0830182SAttilio Rao 	flags = LO_SLEEPABLE | LO_UPGRADABLE;
2194e7f640dSJohn Baldwin 	if (opts & SX_DUPOK)
2204e7f640dSJohn Baldwin 		flags |= LO_DUPOK;
2214e7f640dSJohn Baldwin 	if (opts & SX_NOPROFILE)
2224e7f640dSJohn Baldwin 		flags |= LO_NOPROFILE;
2234e7f640dSJohn Baldwin 	if (!(opts & SX_NOWITNESS))
2244e7f640dSJohn Baldwin 		flags |= LO_WITNESS;
225f0830182SAttilio Rao 	if (opts & SX_RECURSE)
226f0830182SAttilio Rao 		flags |= LO_RECURSABLE;
2274e7f640dSJohn Baldwin 	if (opts & SX_QUIET)
2284e7f640dSJohn Baldwin 		flags |= LO_QUIET;
2294e7f640dSJohn Baldwin 
230f0830182SAttilio Rao 	flags |= opts & SX_NOADAPTIVE;
231*b5fb43e5SJohn Baldwin 	lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
2324e7f640dSJohn Baldwin 	sx->sx_lock = SX_LOCK_UNLOCKED;
2334e7f640dSJohn Baldwin 	sx->sx_recurse = 0;
2346281b30aSJason Evans }
2356281b30aSJason Evans 
2366281b30aSJason Evans void
2376281b30aSJason Evans sx_destroy(struct sx *sx)
2386281b30aSJason Evans {
2396281b30aSJason Evans 
2404e7f640dSJohn Baldwin 	KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
2414e7f640dSJohn Baldwin 	KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
2420026c92cSJohn Baldwin 	sx->sx_lock = SX_LOCK_DESTROYED;
243aa89d8cdSJohn Baldwin 	lock_destroy(&sx->lock_object);
2446281b30aSJason Evans }
2456281b30aSJason Evans 
246f9819486SAttilio Rao int
247f9819486SAttilio Rao _sx_slock(struct sx *sx, int opts, const char *file, int line)
2486281b30aSJason Evans {
249f9819486SAttilio Rao 	int error = 0;
2506281b30aSJason Evans 
25135370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
25235370593SAndriy Gapon 		return (0);
253cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
254e3ae0dfeSAttilio Rao 	    ("sx_slock() by idle thread %p on sx %s @ %s:%d",
255e3ae0dfeSAttilio 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 
277cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
278e3ae0dfeSAttilio Rao 	    ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
279e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
280e3ae0dfeSAttilio 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);
306cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
307e3ae0dfeSAttilio Rao 	    ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
308e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
3090026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3100026c92cSJohn Baldwin 	    ("sx_xlock() of destroyed sx @ %s:%d", file, line));
311aa89d8cdSJohn Baldwin 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
31241313430SJohn Baldwin 	    line, NULL);
313f9819486SAttilio Rao 	error = __sx_xlock(sx, curthread, opts, file, line);
314f9819486SAttilio Rao 	if (!error) {
315f9819486SAttilio Rao 		LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
316f9819486SAttilio Rao 		    file, line);
317aa89d8cdSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
318764e4d54SJohn Baldwin 		curthread->td_locks++;
3196281b30aSJason Evans 	}
3206281b30aSJason Evans 
321f9819486SAttilio Rao 	return (error);
322f9819486SAttilio Rao }
323f9819486SAttilio Rao 
3245f36700aSJohn Baldwin int
3259fde98bbSAttilio Rao sx_try_xlock_(struct sx *sx, const char *file, int line)
3265f36700aSJohn Baldwin {
3274e7f640dSJohn Baldwin 	int rval;
3285f36700aSJohn Baldwin 
32935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
33035370593SAndriy Gapon 		return (1);
33135370593SAndriy Gapon 
332cd2fe4e6SAttilio Rao 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
333e3ae0dfeSAttilio Rao 	    ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
334e3ae0dfeSAttilio Rao 	    curthread, sx->lock_object.lo_name, file, line));
3350026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3360026c92cSJohn Baldwin 	    ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
3374e7f640dSJohn Baldwin 
338f0830182SAttilio Rao 	if (sx_xlocked(sx) &&
339f0830182SAttilio Rao 	    (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
3404e7f640dSJohn Baldwin 		sx->sx_recurse++;
3414e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
3424e7f640dSJohn Baldwin 		rval = 1;
3434e7f640dSJohn Baldwin 	} else
3444e7f640dSJohn Baldwin 		rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
3454e7f640dSJohn Baldwin 		    (uintptr_t)curthread);
3464e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
3474e7f640dSJohn Baldwin 	if (rval) {
3484e7f640dSJohn Baldwin 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
3494e7f640dSJohn Baldwin 		    file, line);
350764e4d54SJohn Baldwin 		curthread->td_locks++;
3515f36700aSJohn Baldwin 	}
3524e7f640dSJohn Baldwin 
3534e7f640dSJohn Baldwin 	return (rval);
3545f36700aSJohn Baldwin }
3555f36700aSJohn Baldwin 
3566281b30aSJason Evans void
35719284646SJohn Baldwin _sx_sunlock(struct sx *sx, const char *file, int line)
3586281b30aSJason Evans {
3596281b30aSJason Evans 
36035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
36135370593SAndriy Gapon 		return;
3620026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
3630026c92cSJohn Baldwin 	    ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
3647ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
365aa89d8cdSJohn Baldwin 	WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
366aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
3674e7f640dSJohn Baldwin 	__sx_sunlock(sx, file, line);
368a5aedd68SStacey Son 	LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
369*b5fb43e5SJohn 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);
384afc0bfbdSKip Macy 	if (!sx_recursed(sx))
385a5aedd68SStacey Son 		LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
3864e7f640dSJohn Baldwin 	__sx_xunlock(sx, curthread, file, line);
387*b5fb43e5SJohn Baldwin 	curthread->td_locks--;
3886281b30aSJason Evans }
389d55229b7SJason Evans 
3904e7f640dSJohn Baldwin /*
3914e7f640dSJohn Baldwin  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
3924e7f640dSJohn Baldwin  * This will only succeed if this thread holds a single shared lock.
3934e7f640dSJohn Baldwin  * Return 1 if if the upgrade succeed, 0 otherwise.
3944e7f640dSJohn Baldwin  */
395d55229b7SJason Evans int
3969fde98bbSAttilio Rao sx_try_upgrade_(struct sx *sx, const char *file, int line)
397d55229b7SJason Evans {
3984e7f640dSJohn Baldwin 	uintptr_t x;
3994e7f640dSJohn Baldwin 	int success;
400d55229b7SJason Evans 
40135370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
40235370593SAndriy Gapon 		return (1);
40335370593SAndriy Gapon 
4040026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4050026c92cSJohn Baldwin 	    ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
4067ec137e5SJohn Baldwin 	_sx_assert(sx, SA_SLOCKED, file, line);
407d55229b7SJason Evans 
4084e7f640dSJohn Baldwin 	/*
4094e7f640dSJohn Baldwin 	 * Try to switch from one shared lock to an exclusive lock.  We need
4104e7f640dSJohn Baldwin 	 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
4114e7f640dSJohn Baldwin 	 * we will wake up the exclusive waiters when we drop the lock.
4124e7f640dSJohn Baldwin 	 */
4134e7f640dSJohn Baldwin 	x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
4144e7f640dSJohn Baldwin 	success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
4154e7f640dSJohn Baldwin 	    (uintptr_t)curthread | x);
4164e7f640dSJohn Baldwin 	LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
417a5aedd68SStacey Son 	if (success) {
418aa89d8cdSJohn Baldwin 		WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
419b0b7cb50SJohn Baldwin 		    file, line);
420a5aedd68SStacey Son 		LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
421a5aedd68SStacey Son 	}
4224e7f640dSJohn Baldwin 	return (success);
423d55229b7SJason Evans }
424d55229b7SJason Evans 
4254e7f640dSJohn Baldwin /*
4264e7f640dSJohn Baldwin  * Downgrade an unrecursed exclusive lock into a single shared lock.
4274e7f640dSJohn Baldwin  */
428d55229b7SJason Evans void
4299fde98bbSAttilio Rao sx_downgrade_(struct sx *sx, const char *file, int line)
430d55229b7SJason Evans {
4314e7f640dSJohn Baldwin 	uintptr_t x;
432da7bbd2cSJohn Baldwin 	int wakeup_swapper;
433d55229b7SJason Evans 
43435370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
43535370593SAndriy Gapon 		return;
43635370593SAndriy Gapon 
4370026c92cSJohn Baldwin 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
4380026c92cSJohn Baldwin 	    ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
4397ec137e5SJohn Baldwin 	_sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
4404e7f640dSJohn Baldwin #ifndef INVARIANTS
4414e7f640dSJohn Baldwin 	if (sx_recursed(sx))
4424e7f640dSJohn Baldwin 		panic("downgrade of a recursed lock");
4434e7f640dSJohn Baldwin #endif
444d55229b7SJason Evans 
445aa89d8cdSJohn Baldwin 	WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
446d55229b7SJason Evans 
4474e7f640dSJohn Baldwin 	/*
4484e7f640dSJohn Baldwin 	 * Try to switch from an exclusive lock with no shared waiters
4494e7f640dSJohn Baldwin 	 * to one sharer with no shared waiters.  If there are
4504e7f640dSJohn Baldwin 	 * exclusive waiters, we don't need to lock the sleep queue so
4514e7f640dSJohn Baldwin 	 * long as we preserve the flag.  We do one quick try and if
4524e7f640dSJohn Baldwin 	 * that fails we grab the sleepq lock to keep the flags from
4534e7f640dSJohn Baldwin 	 * changing and do it the slow way.
4544e7f640dSJohn Baldwin 	 *
4554e7f640dSJohn Baldwin 	 * We have to lock the sleep queue if there are shared waiters
4564e7f640dSJohn Baldwin 	 * so we can wake them up.
4574e7f640dSJohn Baldwin 	 */
4584e7f640dSJohn Baldwin 	x = sx->sx_lock;
4594e7f640dSJohn Baldwin 	if (!(x & SX_LOCK_SHARED_WAITERS) &&
4604e7f640dSJohn Baldwin 	    atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
4614e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
4624e7f640dSJohn Baldwin 		LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
4634e7f640dSJohn Baldwin 		return;
4644e7f640dSJohn Baldwin 	}
4654e7f640dSJohn Baldwin 
4664e7f640dSJohn Baldwin 	/*
4674e7f640dSJohn Baldwin 	 * Lock the sleep queue so we can read the waiters bits
4684e7f640dSJohn Baldwin 	 * without any races and wakeup any shared waiters.
4694e7f640dSJohn Baldwin 	 */
4704e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
4714e7f640dSJohn Baldwin 
4724e7f640dSJohn Baldwin 	/*
4734e7f640dSJohn Baldwin 	 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
4744e7f640dSJohn Baldwin 	 * shared lock.  If there are any shared waiters, wake them up.
4754e7f640dSJohn Baldwin 	 */
476da7bbd2cSJohn Baldwin 	wakeup_swapper = 0;
4774e7f640dSJohn Baldwin 	x = sx->sx_lock;
4784e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
4794e7f640dSJohn Baldwin 	    (x & SX_LOCK_EXCLUSIVE_WAITERS));
4804e7f640dSJohn Baldwin 	if (x & SX_LOCK_SHARED_WAITERS)
481da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
482da7bbd2cSJohn Baldwin 		    0, SQ_SHARED_QUEUE);
4834e7f640dSJohn Baldwin 	sleepq_release(&sx->lock_object);
484d55229b7SJason Evans 
485aa89d8cdSJohn Baldwin 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
486a5aedd68SStacey Son 	LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
487da7bbd2cSJohn Baldwin 
488da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
489da7bbd2cSJohn Baldwin 		kick_proc0();
4904e7f640dSJohn Baldwin }
491d55229b7SJason Evans 
4924e7f640dSJohn Baldwin /*
4934e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xlock
4944e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
4954e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
4964e7f640dSJohn Baldwin  * accessible from at least sx.h.
4974e7f640dSJohn Baldwin  */
498f9819486SAttilio Rao int
499f9819486SAttilio Rao _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
500f9819486SAttilio Rao     int line)
5014e7f640dSJohn Baldwin {
5024e7f640dSJohn Baldwin 	GIANT_DECLARE;
5034e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5044e7f640dSJohn Baldwin 	volatile struct thread *owner;
5051ae1c2a3SAttilio Rao 	u_int i, spintries = 0;
5064e7f640dSJohn Baldwin #endif
5074e7f640dSJohn Baldwin 	uintptr_t x;
5081723a064SJeff Roberson #ifdef LOCK_PROFILING
5091723a064SJeff Roberson 	uint64_t waittime = 0;
5101723a064SJeff Roberson 	int contested = 0;
5111723a064SJeff Roberson #endif
5121723a064SJeff Roberson 	int error = 0;
513a5aedd68SStacey Son #ifdef	KDTRACE_HOOKS
514a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
515a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
516a5aedd68SStacey Son 	int64_t sleep_time = 0;
517a5aedd68SStacey Son #endif
5184e7f640dSJohn Baldwin 
51935370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
52035370593SAndriy Gapon 		return (0);
52135370593SAndriy Gapon 
5224e7f640dSJohn Baldwin 	/* If we already hold an exclusive lock, then recurse. */
5234e7f640dSJohn Baldwin 	if (sx_xlocked(sx)) {
524f0830182SAttilio Rao 		KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
525b0d67325SJohn Baldwin 	    ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
526b0d67325SJohn Baldwin 		    sx->lock_object.lo_name, file, line));
5274e7f640dSJohn Baldwin 		sx->sx_recurse++;
5284e7f640dSJohn Baldwin 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
5294e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
5304e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
531f9819486SAttilio Rao 		return (0);
5324e7f640dSJohn Baldwin 	}
5334e7f640dSJohn Baldwin 
5344e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
5354e7f640dSJohn Baldwin 		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
5364e7f640dSJohn Baldwin 		    sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
5374e7f640dSJohn Baldwin 
5384e7f640dSJohn Baldwin 	while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
539a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
540a5aedd68SStacey Son 		spin_cnt++;
541a5aedd68SStacey Son #endif
542f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
543f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
544f5f9340bSFabien Thomas #endif
545eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
546eea4f254SJeff Roberson 		    &waittime);
5474e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
5484e7f640dSJohn Baldwin 		/*
5494e7f640dSJohn Baldwin 		 * If the lock is write locked and the owner is
5504e7f640dSJohn Baldwin 		 * running on another CPU, spin until the owner stops
5514e7f640dSJohn Baldwin 		 * running or the state of the lock changes.
5524e7f640dSJohn Baldwin 		 */
5534e7f640dSJohn Baldwin 		x = sx->sx_lock;
5548545538bSJohn Baldwin 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
5551ae1c2a3SAttilio Rao 			if ((x & SX_LOCK_SHARED) == 0) {
5564e7f640dSJohn Baldwin 				x = SX_OWNER(x);
5574e7f640dSJohn Baldwin 				owner = (struct thread *)x;
5584e7f640dSJohn Baldwin 				if (TD_IS_RUNNING(owner)) {
5594e7f640dSJohn Baldwin 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5604e7f640dSJohn Baldwin 						CTR3(KTR_LOCK,
5614e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
5624e7f640dSJohn Baldwin 						    __func__, sx, owner);
5634e7f640dSJohn Baldwin 					GIANT_SAVE();
5644e7f640dSJohn Baldwin 					while (SX_OWNER(sx->sx_lock) == x &&
565a5aedd68SStacey Son 					    TD_IS_RUNNING(owner)) {
5664e7f640dSJohn Baldwin 						cpu_spinwait();
567a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
568a5aedd68SStacey Son 						spin_cnt++;
569a5aedd68SStacey Son #endif
570a5aedd68SStacey Son 					}
5714e7f640dSJohn Baldwin 					continue;
5724e7f640dSJohn Baldwin 				}
5731ae1c2a3SAttilio Rao 			} else if (SX_SHARERS(x) && spintries < asx_retries) {
5748d3635c4SAttilio Rao 				GIANT_SAVE();
5751ae1c2a3SAttilio Rao 				spintries++;
5761ae1c2a3SAttilio Rao 				for (i = 0; i < asx_loops; i++) {
5771ae1c2a3SAttilio Rao 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
5781ae1c2a3SAttilio Rao 						CTR4(KTR_LOCK,
5791ae1c2a3SAttilio Rao 				    "%s: shared spinning on %p with %u and %u",
5801ae1c2a3SAttilio Rao 						    __func__, sx, spintries, i);
5811ae1c2a3SAttilio Rao 					x = sx->sx_lock;
5821ae1c2a3SAttilio Rao 					if ((x & SX_LOCK_SHARED) == 0 ||
5831ae1c2a3SAttilio Rao 					    SX_SHARERS(x) == 0)
5841ae1c2a3SAttilio Rao 						break;
5851ae1c2a3SAttilio Rao 					cpu_spinwait();
5861ae1c2a3SAttilio Rao #ifdef KDTRACE_HOOKS
5871ae1c2a3SAttilio Rao 					spin_cnt++;
5881ae1c2a3SAttilio Rao #endif
5891ae1c2a3SAttilio Rao 				}
5901ae1c2a3SAttilio Rao 				if (i != asx_loops)
5911ae1c2a3SAttilio Rao 					continue;
5921ae1c2a3SAttilio Rao 			}
5934e7f640dSJohn Baldwin 		}
5944e7f640dSJohn Baldwin #endif
5954e7f640dSJohn Baldwin 
5964e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
5974e7f640dSJohn Baldwin 		x = sx->sx_lock;
5984e7f640dSJohn Baldwin 
5994e7f640dSJohn Baldwin 		/*
6004e7f640dSJohn Baldwin 		 * If the lock was released while spinning on the
6014e7f640dSJohn Baldwin 		 * sleep queue chain lock, try again.
6024e7f640dSJohn Baldwin 		 */
6034e7f640dSJohn Baldwin 		if (x == SX_LOCK_UNLOCKED) {
6044e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6054e7f640dSJohn Baldwin 			continue;
6064e7f640dSJohn Baldwin 		}
6074e7f640dSJohn Baldwin 
6084e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
6094e7f640dSJohn Baldwin 		/*
6104e7f640dSJohn Baldwin 		 * The current lock owner might have started executing
6114e7f640dSJohn Baldwin 		 * on another CPU (or the lock could have changed
6124e7f640dSJohn Baldwin 		 * owners) while we were waiting on the sleep queue
6134e7f640dSJohn Baldwin 		 * chain lock.  If so, drop the sleep queue lock and try
6144e7f640dSJohn Baldwin 		 * again.
6154e7f640dSJohn Baldwin 		 */
6164e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
6171ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
6184e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
6194e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
6204e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6214e7f640dSJohn Baldwin 				continue;
6224e7f640dSJohn Baldwin 			}
6234e7f640dSJohn Baldwin 		}
6244e7f640dSJohn Baldwin #endif
6254e7f640dSJohn Baldwin 
6264e7f640dSJohn Baldwin 		/*
6274e7f640dSJohn Baldwin 		 * If an exclusive lock was released with both shared
6284e7f640dSJohn Baldwin 		 * and exclusive waiters and a shared waiter hasn't
6294e7f640dSJohn Baldwin 		 * woken up and acquired the lock yet, sx_lock will be
6304e7f640dSJohn Baldwin 		 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
6314e7f640dSJohn Baldwin 		 * If we see that value, try to acquire it once.  Note
6324e7f640dSJohn Baldwin 		 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
6334e7f640dSJohn Baldwin 		 * as there are other exclusive waiters still.  If we
6344e7f640dSJohn Baldwin 		 * fail, restart the loop.
6354e7f640dSJohn Baldwin 		 */
6364e7f640dSJohn Baldwin 		if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
6374e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock,
6384e7f640dSJohn Baldwin 			    SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
6394e7f640dSJohn Baldwin 			    tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
6404e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6414e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
6424e7f640dSJohn Baldwin 				    __func__, sx);
6434e7f640dSJohn Baldwin 				break;
6444e7f640dSJohn Baldwin 			}
6454e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
6464e7f640dSJohn Baldwin 			continue;
6474e7f640dSJohn Baldwin 		}
6484e7f640dSJohn Baldwin 
6494e7f640dSJohn Baldwin 		/*
6504e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
6514e7f640dSJohn Baldwin 		 * than loop back and retry.
6524e7f640dSJohn Baldwin 		 */
6534e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
6544e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
6554e7f640dSJohn Baldwin 			    x | SX_LOCK_EXCLUSIVE_WAITERS)) {
6564e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
6574e7f640dSJohn Baldwin 				continue;
6584e7f640dSJohn Baldwin 			}
6594e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
6604e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
6614e7f640dSJohn Baldwin 				    __func__, sx);
6624e7f640dSJohn Baldwin 		}
6634e7f640dSJohn Baldwin 
6644e7f640dSJohn Baldwin 		/*
6654e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the exclusive
6664e7f640dSJohn Baldwin 		 * lock and the exclusive waiters flag is set, we have
6674e7f640dSJohn Baldwin 		 * to sleep.
6684e7f640dSJohn Baldwin 		 */
6694e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6704e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
6714e7f640dSJohn Baldwin 			    __func__, sx);
6724e7f640dSJohn Baldwin 
673a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
674a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
675a5aedd68SStacey Son #endif
6764e7f640dSJohn Baldwin 		GIANT_SAVE();
6774e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
678f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
679f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
680f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
681c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
682f9819486SAttilio Rao 		else
683c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
684a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
685a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
686a5aedd68SStacey Son 		sleep_cnt++;
687a5aedd68SStacey Son #endif
688f9819486SAttilio Rao 		if (error) {
689f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
690f9819486SAttilio Rao 				CTR2(KTR_LOCK,
691f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
692f9819486SAttilio Rao 				    __func__, sx);
693f9819486SAttilio Rao 			break;
694f9819486SAttilio Rao 		}
6954e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
6964e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
6974e7f640dSJohn Baldwin 			    __func__, sx);
6984e7f640dSJohn Baldwin 	}
6994e7f640dSJohn Baldwin 
7004e7f640dSJohn Baldwin 	GIANT_RESTORE();
701f9819486SAttilio Rao 	if (!error)
702a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
703a5aedd68SStacey Son 		    contested, waittime, file, line);
704a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
705a5aedd68SStacey Son 	if (sleep_time)
706a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
707a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
708a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
709a5aedd68SStacey Son #endif
710f9819486SAttilio Rao 	return (error);
7114e7f640dSJohn Baldwin }
7124e7f640dSJohn Baldwin 
7134e7f640dSJohn Baldwin /*
7144e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_xunlock
7154e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7164e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7174e7f640dSJohn Baldwin  * accessible from at least sx.h.
7184e7f640dSJohn Baldwin  */
7194e7f640dSJohn Baldwin void
7204e7f640dSJohn Baldwin _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
7214e7f640dSJohn Baldwin {
7224e7f640dSJohn Baldwin 	uintptr_t x;
723da7bbd2cSJohn Baldwin 	int queue, wakeup_swapper;
7244e7f640dSJohn Baldwin 
72535370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
72635370593SAndriy Gapon 		return;
72735370593SAndriy Gapon 
7284e7f640dSJohn Baldwin 	MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
7294e7f640dSJohn Baldwin 
7304e7f640dSJohn Baldwin 	/* If the lock is recursed, then unrecurse one level. */
7314e7f640dSJohn Baldwin 	if (sx_xlocked(sx) && sx_recursed(sx)) {
7324e7f640dSJohn Baldwin 		if ((--sx->sx_recurse) == 0)
7334e7f640dSJohn Baldwin 			atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
7344e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
7354e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
7364e7f640dSJohn Baldwin 		return;
7374e7f640dSJohn Baldwin 	}
7384e7f640dSJohn Baldwin 	MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
7394e7f640dSJohn Baldwin 	    SX_LOCK_EXCLUSIVE_WAITERS));
7404e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7414e7f640dSJohn Baldwin 		CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
7424e7f640dSJohn Baldwin 
7434e7f640dSJohn Baldwin 	sleepq_lock(&sx->lock_object);
7444e7f640dSJohn Baldwin 	x = SX_LOCK_UNLOCKED;
7454e7f640dSJohn Baldwin 
7464e7f640dSJohn Baldwin 	/*
7474e7f640dSJohn Baldwin 	 * The wake up algorithm here is quite simple and probably not
7484e7f640dSJohn Baldwin 	 * ideal.  It gives precedence to shared waiters if they are
7494e7f640dSJohn Baldwin 	 * present.  For this condition, we have to preserve the
7504e7f640dSJohn Baldwin 	 * state of the exclusive waiters flag.
7512028867dSAttilio Rao 	 * If interruptible sleeps left the shared queue empty avoid a
7522028867dSAttilio Rao 	 * starvation for the threads sleeping on the exclusive queue by giving
7532028867dSAttilio Rao 	 * them precedence and cleaning up the shared waiters bit anyway.
7544e7f640dSJohn Baldwin 	 */
7552028867dSAttilio Rao 	if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
7562028867dSAttilio Rao 	    sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
7574e7f640dSJohn Baldwin 		queue = SQ_SHARED_QUEUE;
7584e7f640dSJohn Baldwin 		x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
7594e7f640dSJohn Baldwin 	} else
7604e7f640dSJohn Baldwin 		queue = SQ_EXCLUSIVE_QUEUE;
7614e7f640dSJohn Baldwin 
7624e7f640dSJohn Baldwin 	/* Wake up all the waiters for the specific queue. */
7634e7f640dSJohn Baldwin 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
7644e7f640dSJohn Baldwin 		CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
7654e7f640dSJohn Baldwin 		    __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
7664e7f640dSJohn Baldwin 		    "exclusive");
7674e7f640dSJohn Baldwin 	atomic_store_rel_ptr(&sx->sx_lock, x);
768da7bbd2cSJohn Baldwin 	wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
769da7bbd2cSJohn Baldwin 	    queue);
770c5aa6b58SJeff Roberson 	sleepq_release(&sx->lock_object);
771da7bbd2cSJohn Baldwin 	if (wakeup_swapper)
772da7bbd2cSJohn Baldwin 		kick_proc0();
7734e7f640dSJohn Baldwin }
7744e7f640dSJohn Baldwin 
7754e7f640dSJohn Baldwin /*
7764e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_slock
7774e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
7784e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
7794e7f640dSJohn Baldwin  * accessible from at least sx.h.
7804e7f640dSJohn Baldwin  */
781f9819486SAttilio Rao int
782f9819486SAttilio Rao _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
7834e7f640dSJohn Baldwin {
7844e7f640dSJohn Baldwin 	GIANT_DECLARE;
7854e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
7864e7f640dSJohn Baldwin 	volatile struct thread *owner;
7874e7f640dSJohn Baldwin #endif
7881723a064SJeff Roberson #ifdef LOCK_PROFILING
789c1a6d9faSAttilio Rao 	uint64_t waittime = 0;
790c1a6d9faSAttilio Rao 	int contested = 0;
7911723a064SJeff Roberson #endif
7924e7f640dSJohn Baldwin 	uintptr_t x;
793c1a6d9faSAttilio Rao 	int error = 0;
794a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
795a5aedd68SStacey Son 	uint64_t spin_cnt = 0;
796a5aedd68SStacey Son 	uint64_t sleep_cnt = 0;
797a5aedd68SStacey Son 	int64_t sleep_time = 0;
798a5aedd68SStacey Son #endif
799c1a6d9faSAttilio Rao 
80035370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
80135370593SAndriy Gapon 		return (0);
80235370593SAndriy Gapon 
8034e7f640dSJohn Baldwin 	/*
8044e7f640dSJohn Baldwin 	 * As with rwlocks, we don't make any attempt to try to block
8054e7f640dSJohn Baldwin 	 * shared locks once there is an exclusive waiter.
8064e7f640dSJohn Baldwin 	 */
8074e7f640dSJohn Baldwin 	for (;;) {
808a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
809a5aedd68SStacey Son 		spin_cnt++;
810a5aedd68SStacey Son #endif
8114e7f640dSJohn Baldwin 		x = sx->sx_lock;
8124e7f640dSJohn Baldwin 
8134e7f640dSJohn Baldwin 		/*
8144e7f640dSJohn Baldwin 		 * If no other thread has an exclusive lock then try to bump up
8154e7f640dSJohn Baldwin 		 * the count of sharers.  Since we have to preserve the state
8164e7f640dSJohn Baldwin 		 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
8174e7f640dSJohn Baldwin 		 * shared lock loop back and retry.
8184e7f640dSJohn Baldwin 		 */
8194e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8204e7f640dSJohn Baldwin 			MPASS(!(x & SX_LOCK_SHARED_WAITERS));
8214e7f640dSJohn Baldwin 			if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
8224e7f640dSJohn Baldwin 			    x + SX_ONE_SHARER)) {
8234e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8244e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
8254e7f640dSJohn Baldwin 					    "%s: %p succeed %p -> %p", __func__,
8264e7f640dSJohn Baldwin 					    sx, (void *)x,
8274e7f640dSJohn Baldwin 					    (void *)(x + SX_ONE_SHARER));
8284e7f640dSJohn Baldwin 				break;
8294e7f640dSJohn Baldwin 			}
8304e7f640dSJohn Baldwin 			continue;
8314e7f640dSJohn Baldwin 		}
832f5f9340bSFabien Thomas #ifdef HWPMC_HOOKS
833f5f9340bSFabien Thomas 		PMC_SOFT_CALL( , , lock, failed);
834f5f9340bSFabien Thomas #endif
835eea4f254SJeff Roberson 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
836eea4f254SJeff Roberson 		    &waittime);
8374e7f640dSJohn Baldwin 
8384e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8394e7f640dSJohn Baldwin 		/*
8404e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8414e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8424e7f640dSJohn Baldwin 		 * changes.
8434e7f640dSJohn Baldwin 		 */
8441ae1c2a3SAttilio Rao 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8454e7f640dSJohn Baldwin 			x = SX_OWNER(x);
8464e7f640dSJohn Baldwin 			owner = (struct thread *)x;
8474e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8484e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
8494e7f640dSJohn Baldwin 					CTR3(KTR_LOCK,
8504e7f640dSJohn Baldwin 					    "%s: spinning on %p held by %p",
8514e7f640dSJohn Baldwin 					    __func__, sx, owner);
8524e7f640dSJohn Baldwin 				GIANT_SAVE();
8534e7f640dSJohn Baldwin 				while (SX_OWNER(sx->sx_lock) == x &&
854a5aedd68SStacey Son 				    TD_IS_RUNNING(owner)) {
855a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
856a5aedd68SStacey Son 					spin_cnt++;
857a5aedd68SStacey Son #endif
8584e7f640dSJohn Baldwin 					cpu_spinwait();
859a5aedd68SStacey Son 				}
8604e7f640dSJohn Baldwin 				continue;
8614e7f640dSJohn Baldwin 			}
8624e7f640dSJohn Baldwin 		}
8634e7f640dSJohn Baldwin #endif
8644e7f640dSJohn Baldwin 
8654e7f640dSJohn Baldwin 		/*
8664e7f640dSJohn Baldwin 		 * Some other thread already has an exclusive lock, so
8674e7f640dSJohn Baldwin 		 * start the process of blocking.
8684e7f640dSJohn Baldwin 		 */
8694e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
8704e7f640dSJohn Baldwin 		x = sx->sx_lock;
8714e7f640dSJohn Baldwin 
8724e7f640dSJohn Baldwin 		/*
8734e7f640dSJohn Baldwin 		 * The lock could have been released while we spun.
8744e7f640dSJohn Baldwin 		 * In this case loop back and retry.
8754e7f640dSJohn Baldwin 		 */
8764e7f640dSJohn Baldwin 		if (x & SX_LOCK_SHARED) {
8774e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
8784e7f640dSJohn Baldwin 			continue;
8794e7f640dSJohn Baldwin 		}
8804e7f640dSJohn Baldwin 
8814e7f640dSJohn Baldwin #ifdef ADAPTIVE_SX
8824e7f640dSJohn Baldwin 		/*
8834e7f640dSJohn Baldwin 		 * If the owner is running on another CPU, spin until
8844e7f640dSJohn Baldwin 		 * the owner stops running or the state of the lock
8854e7f640dSJohn Baldwin 		 * changes.
8864e7f640dSJohn Baldwin 		 */
8874e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED) &&
8881ae1c2a3SAttilio Rao 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
8894e7f640dSJohn Baldwin 			owner = (struct thread *)SX_OWNER(x);
8904e7f640dSJohn Baldwin 			if (TD_IS_RUNNING(owner)) {
8914e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
8924e7f640dSJohn Baldwin 				continue;
8934e7f640dSJohn Baldwin 			}
8944e7f640dSJohn Baldwin 		}
8954e7f640dSJohn Baldwin #endif
8964e7f640dSJohn Baldwin 
8974e7f640dSJohn Baldwin 		/*
8984e7f640dSJohn Baldwin 		 * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
8994e7f640dSJohn Baldwin 		 * fail to set it drop the sleep queue lock and loop
9004e7f640dSJohn Baldwin 		 * back.
9014e7f640dSJohn Baldwin 		 */
9024e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_SHARED_WAITERS)) {
9034e7f640dSJohn Baldwin 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
9044e7f640dSJohn Baldwin 			    x | SX_LOCK_SHARED_WAITERS)) {
9054e7f640dSJohn Baldwin 				sleepq_release(&sx->lock_object);
9064e7f640dSJohn Baldwin 				continue;
9074e7f640dSJohn Baldwin 			}
9084e7f640dSJohn Baldwin 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
9094e7f640dSJohn Baldwin 				CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
9104e7f640dSJohn Baldwin 				    __func__, sx);
9114e7f640dSJohn Baldwin 		}
9124e7f640dSJohn Baldwin 
9134e7f640dSJohn Baldwin 		/*
9144e7f640dSJohn Baldwin 		 * Since we have been unable to acquire the shared lock,
9154e7f640dSJohn Baldwin 		 * we have to sleep.
9164e7f640dSJohn Baldwin 		 */
9174e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9184e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
9194e7f640dSJohn Baldwin 			    __func__, sx);
9204e7f640dSJohn Baldwin 
921a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
922a5aedd68SStacey Son 		sleep_time -= lockstat_nsecs();
923a5aedd68SStacey Son #endif
9244e7f640dSJohn Baldwin 		GIANT_SAVE();
9254e7f640dSJohn Baldwin 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
926f9819486SAttilio Rao 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
927f9819486SAttilio Rao 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
928f9819486SAttilio Rao 		if (!(opts & SX_INTERRUPTIBLE))
929c5aa6b58SJeff Roberson 			sleepq_wait(&sx->lock_object, 0);
930f9819486SAttilio Rao 		else
931c5aa6b58SJeff Roberson 			error = sleepq_wait_sig(&sx->lock_object, 0);
932a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
933a5aedd68SStacey Son 		sleep_time += lockstat_nsecs();
934a5aedd68SStacey Son 		sleep_cnt++;
935a5aedd68SStacey Son #endif
936f9819486SAttilio Rao 		if (error) {
937f9819486SAttilio Rao 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
938f9819486SAttilio Rao 				CTR2(KTR_LOCK,
939f9819486SAttilio Rao 			"%s: interruptible sleep by %p suspended by signal",
940f9819486SAttilio Rao 				    __func__, sx);
941f9819486SAttilio Rao 			break;
942f9819486SAttilio Rao 		}
9434e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
9444e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
9454e7f640dSJohn Baldwin 			    __func__, sx);
9464e7f640dSJohn Baldwin 	}
947eea4f254SJeff Roberson 	if (error == 0)
948a5aedd68SStacey Son 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
949a5aedd68SStacey Son 		    contested, waittime, file, line);
950a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
951a5aedd68SStacey Son 	if (sleep_time)
952a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
953a5aedd68SStacey Son 	if (spin_cnt > sleep_cnt)
954a5aedd68SStacey Son 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
955a5aedd68SStacey Son #endif
9564e7f640dSJohn Baldwin 	GIANT_RESTORE();
957f9819486SAttilio Rao 	return (error);
9584e7f640dSJohn Baldwin }
9594e7f640dSJohn Baldwin 
9604e7f640dSJohn Baldwin /*
9614e7f640dSJohn Baldwin  * This function represents the so-called 'hard case' for sx_sunlock
9624e7f640dSJohn Baldwin  * operation.  All 'easy case' failures are redirected to this.  Note
9634e7f640dSJohn Baldwin  * that ideally this would be a static function, but it needs to be
9644e7f640dSJohn Baldwin  * accessible from at least sx.h.
9654e7f640dSJohn Baldwin  */
9664e7f640dSJohn Baldwin void
9674e7f640dSJohn Baldwin _sx_sunlock_hard(struct sx *sx, const char *file, int line)
9684e7f640dSJohn Baldwin {
9694e7f640dSJohn Baldwin 	uintptr_t x;
970da7bbd2cSJohn Baldwin 	int wakeup_swapper;
9714e7f640dSJohn Baldwin 
97235370593SAndriy Gapon 	if (SCHEDULER_STOPPED())
97335370593SAndriy Gapon 		return;
97435370593SAndriy Gapon 
9754e7f640dSJohn Baldwin 	for (;;) {
9764e7f640dSJohn Baldwin 		x = sx->sx_lock;
9774e7f640dSJohn Baldwin 
9784e7f640dSJohn Baldwin 		/*
9794e7f640dSJohn Baldwin 		 * We should never have sharers while at least one thread
9804e7f640dSJohn Baldwin 		 * holds a shared lock.
9814e7f640dSJohn Baldwin 		 */
9824e7f640dSJohn Baldwin 		KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
9834e7f640dSJohn Baldwin 		    ("%s: waiting sharers", __func__));
9844e7f640dSJohn Baldwin 
9854e7f640dSJohn Baldwin 		/*
9864e7f640dSJohn Baldwin 		 * See if there is more than one shared lock held.  If
9874e7f640dSJohn Baldwin 		 * so, just drop one and return.
9884e7f640dSJohn Baldwin 		 */
9894e7f640dSJohn Baldwin 		if (SX_SHARERS(x) > 1) {
990ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
9914e7f640dSJohn Baldwin 			    x - SX_ONE_SHARER)) {
9924e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
9934e7f640dSJohn Baldwin 					CTR4(KTR_LOCK,
9944e7f640dSJohn Baldwin 					    "%s: %p succeeded %p -> %p",
9954e7f640dSJohn Baldwin 					    __func__, sx, (void *)x,
9964e7f640dSJohn Baldwin 					    (void *)(x - SX_ONE_SHARER));
9974e7f640dSJohn Baldwin 				break;
9984e7f640dSJohn Baldwin 			}
9994e7f640dSJohn Baldwin 			continue;
10004e7f640dSJohn Baldwin 		}
10014e7f640dSJohn Baldwin 
10024e7f640dSJohn Baldwin 		/*
10034e7f640dSJohn Baldwin 		 * If there aren't any waiters for an exclusive lock,
10044e7f640dSJohn Baldwin 		 * then try to drop it quickly.
10054e7f640dSJohn Baldwin 		 */
10064e7f640dSJohn Baldwin 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
10074e7f640dSJohn Baldwin 			MPASS(x == SX_SHARERS_LOCK(1));
1008ddce63caSAttilio Rao 			if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1009ddce63caSAttilio Rao 			    SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
10104e7f640dSJohn Baldwin 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
10114e7f640dSJohn Baldwin 					CTR2(KTR_LOCK, "%s: %p last succeeded",
10124e7f640dSJohn Baldwin 					    __func__, sx);
10134e7f640dSJohn Baldwin 				break;
10144e7f640dSJohn Baldwin 			}
10154e7f640dSJohn Baldwin 			continue;
10164e7f640dSJohn Baldwin 		}
10174e7f640dSJohn Baldwin 
10184e7f640dSJohn Baldwin 		/*
10194e7f640dSJohn Baldwin 		 * At this point, there should just be one sharer with
10204e7f640dSJohn Baldwin 		 * exclusive waiters.
10214e7f640dSJohn Baldwin 		 */
10224e7f640dSJohn Baldwin 		MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
10234e7f640dSJohn Baldwin 
10244e7f640dSJohn Baldwin 		sleepq_lock(&sx->lock_object);
10254e7f640dSJohn Baldwin 
10264e7f640dSJohn Baldwin 		/*
10274e7f640dSJohn Baldwin 		 * Wake up semantic here is quite simple:
10284e7f640dSJohn Baldwin 		 * Just wake up all the exclusive waiters.
10294e7f640dSJohn Baldwin 		 * Note that the state of the lock could have changed,
10304e7f640dSJohn Baldwin 		 * so if it fails loop back and retry.
10314e7f640dSJohn Baldwin 		 */
1032ddce63caSAttilio Rao 		if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
10334e7f640dSJohn Baldwin 		    SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
10344e7f640dSJohn Baldwin 		    SX_LOCK_UNLOCKED)) {
10354e7f640dSJohn Baldwin 			sleepq_release(&sx->lock_object);
10364e7f640dSJohn Baldwin 			continue;
10374e7f640dSJohn Baldwin 		}
10384e7f640dSJohn Baldwin 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
10394e7f640dSJohn Baldwin 			CTR2(KTR_LOCK, "%s: %p waking up all thread on"
10404e7f640dSJohn Baldwin 			    "exclusive queue", __func__, sx);
1041da7bbd2cSJohn Baldwin 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1042da7bbd2cSJohn Baldwin 		    0, SQ_EXCLUSIVE_QUEUE);
1043c5aa6b58SJeff Roberson 		sleepq_release(&sx->lock_object);
1044da7bbd2cSJohn Baldwin 		if (wakeup_swapper)
1045da7bbd2cSJohn Baldwin 			kick_proc0();
10464e7f640dSJohn Baldwin 		break;
10474e7f640dSJohn Baldwin 	}
1048d55229b7SJason Evans }
10494e5e677bSJohn Baldwin 
10504e5e677bSJohn Baldwin #ifdef INVARIANT_SUPPORT
1051781a35dfSJohn Baldwin #ifndef INVARIANTS
1052781a35dfSJohn Baldwin #undef	_sx_assert
1053781a35dfSJohn Baldwin #endif
1054781a35dfSJohn Baldwin 
10554e5e677bSJohn Baldwin /*
10564e5e677bSJohn Baldwin  * In the non-WITNESS case, sx_assert() can only detect that at least
10574e5e677bSJohn Baldwin  * *some* thread owns an slock, but it cannot guarantee that *this*
10584e5e677bSJohn Baldwin  * thread owns an slock.
10594e5e677bSJohn Baldwin  */
10604e5e677bSJohn Baldwin void
1061d576deedSPawel Jakub Dawidek _sx_assert(const struct sx *sx, int what, const char *file, int line)
10624e5e677bSJohn Baldwin {
10634e7f640dSJohn Baldwin #ifndef WITNESS
10644e7f640dSJohn Baldwin 	int slocked = 0;
10654e7f640dSJohn Baldwin #endif
10664e5e677bSJohn Baldwin 
106703129ba9SJohn Baldwin 	if (panicstr != NULL)
106803129ba9SJohn Baldwin 		return;
10694e5e677bSJohn Baldwin 	switch (what) {
10707ec137e5SJohn Baldwin 	case SA_SLOCKED:
10717ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_NOTRECURSED:
10727ec137e5SJohn Baldwin 	case SA_SLOCKED | SA_RECURSED:
10734e7f640dSJohn Baldwin #ifndef WITNESS
10744e7f640dSJohn Baldwin 		slocked = 1;
10754e7f640dSJohn Baldwin 		/* FALLTHROUGH */
10764e7f640dSJohn Baldwin #endif
10777ec137e5SJohn Baldwin 	case SA_LOCKED:
10787ec137e5SJohn Baldwin 	case SA_LOCKED | SA_NOTRECURSED:
10797ec137e5SJohn Baldwin 	case SA_LOCKED | SA_RECURSED:
10804e5e677bSJohn Baldwin #ifdef WITNESS
1081aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
10824e5e677bSJohn Baldwin #else
10834e7f640dSJohn Baldwin 		/*
10844e7f640dSJohn Baldwin 		 * If some other thread has an exclusive lock or we
10854e7f640dSJohn Baldwin 		 * have one and are asserting a shared lock, fail.
10864e7f640dSJohn Baldwin 		 * Also, if no one has a lock at all, fail.
10874e7f640dSJohn Baldwin 		 */
10884e7f640dSJohn Baldwin 		if (sx->sx_lock == SX_LOCK_UNLOCKED ||
10894e7f640dSJohn Baldwin 		    (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
10904e7f640dSJohn Baldwin 		    sx_xholder(sx) != curthread)))
109103129ba9SJohn Baldwin 			panic("Lock %s not %slocked @ %s:%d\n",
10924e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, slocked ? "share " : "",
10934e7f640dSJohn Baldwin 			    file, line);
10944e7f640dSJohn Baldwin 
10954e7f640dSJohn Baldwin 		if (!(sx->sx_lock & SX_LOCK_SHARED)) {
10964e7f640dSJohn Baldwin 			if (sx_recursed(sx)) {
10977ec137e5SJohn Baldwin 				if (what & SA_NOTRECURSED)
10984e7f640dSJohn Baldwin 					panic("Lock %s recursed @ %s:%d\n",
10994e7f640dSJohn Baldwin 					    sx->lock_object.lo_name, file,
11004e7f640dSJohn Baldwin 					    line);
11017ec137e5SJohn Baldwin 			} else if (what & SA_RECURSED)
11024e7f640dSJohn Baldwin 				panic("Lock %s not recursed @ %s:%d\n",
11034e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
11044e7f640dSJohn Baldwin 		}
11054e5e677bSJohn Baldwin #endif
11064e5e677bSJohn Baldwin 		break;
11077ec137e5SJohn Baldwin 	case SA_XLOCKED:
11087ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_NOTRECURSED:
11097ec137e5SJohn Baldwin 	case SA_XLOCKED | SA_RECURSED:
11104e7f640dSJohn Baldwin 		if (sx_xholder(sx) != curthread)
111103129ba9SJohn Baldwin 			panic("Lock %s not exclusively locked @ %s:%d\n",
1112aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11134e7f640dSJohn Baldwin 		if (sx_recursed(sx)) {
11147ec137e5SJohn Baldwin 			if (what & SA_NOTRECURSED)
11154e7f640dSJohn Baldwin 				panic("Lock %s recursed @ %s:%d\n",
11164e7f640dSJohn Baldwin 				    sx->lock_object.lo_name, file, line);
11177ec137e5SJohn Baldwin 		} else if (what & SA_RECURSED)
11184e7f640dSJohn Baldwin 			panic("Lock %s not recursed @ %s:%d\n",
11194e7f640dSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
11204e5e677bSJohn Baldwin 		break;
11217ec137e5SJohn Baldwin 	case SA_UNLOCKED:
112219b0efd3SPawel Jakub Dawidek #ifdef WITNESS
1123aa89d8cdSJohn Baldwin 		witness_assert(&sx->lock_object, what, file, line);
112419b0efd3SPawel Jakub Dawidek #else
1125f6739b1dSPawel Jakub Dawidek 		/*
11264e7f640dSJohn Baldwin 		 * If we hold an exclusve lock fail.  We can't
11274e7f640dSJohn Baldwin 		 * reliably check to see if we hold a shared lock or
11284e7f640dSJohn Baldwin 		 * not.
1129f6739b1dSPawel Jakub Dawidek 		 */
11304e7f640dSJohn Baldwin 		if (sx_xholder(sx) == curthread)
113103129ba9SJohn Baldwin 			panic("Lock %s exclusively locked @ %s:%d\n",
1132aa89d8cdSJohn Baldwin 			    sx->lock_object.lo_name, file, line);
113319b0efd3SPawel Jakub Dawidek #endif
113419b0efd3SPawel Jakub Dawidek 		break;
11354e5e677bSJohn Baldwin 	default:
11364e5e677bSJohn Baldwin 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
11374e5e677bSJohn Baldwin 		    line);
11384e5e677bSJohn Baldwin 	}
11394e5e677bSJohn Baldwin }
11404e5e677bSJohn Baldwin #endif	/* INVARIANT_SUPPORT */
1141d272fe53SJohn Baldwin 
1142d272fe53SJohn Baldwin #ifdef DDB
11434e7f640dSJohn Baldwin static void
1144d576deedSPawel Jakub Dawidek db_show_sx(const struct lock_object *lock)
1145d272fe53SJohn Baldwin {
1146d272fe53SJohn Baldwin 	struct thread *td;
1147d576deedSPawel Jakub Dawidek 	const struct sx *sx;
1148d272fe53SJohn Baldwin 
1149d576deedSPawel Jakub Dawidek 	sx = (const struct sx *)lock;
1150d272fe53SJohn Baldwin 
1151d272fe53SJohn Baldwin 	db_printf(" state: ");
11524e7f640dSJohn Baldwin 	if (sx->sx_lock == SX_LOCK_UNLOCKED)
11534e7f640dSJohn Baldwin 		db_printf("UNLOCKED\n");
11540026c92cSJohn Baldwin 	else if (sx->sx_lock == SX_LOCK_DESTROYED) {
11550026c92cSJohn Baldwin 		db_printf("DESTROYED\n");
11560026c92cSJohn Baldwin 		return;
11570026c92cSJohn Baldwin 	} else if (sx->sx_lock & SX_LOCK_SHARED)
11584e7f640dSJohn Baldwin 		db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
11594e7f640dSJohn Baldwin 	else {
11604e7f640dSJohn Baldwin 		td = sx_xholder(sx);
1161d272fe53SJohn Baldwin 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1162431f8906SJulian Elischer 		    td->td_tid, td->td_proc->p_pid, td->td_name);
11634e7f640dSJohn Baldwin 		if (sx_recursed(sx))
11644e7f640dSJohn Baldwin 			db_printf(" recursed: %d\n", sx->sx_recurse);
11654e7f640dSJohn Baldwin 	}
11664e7f640dSJohn Baldwin 
11674e7f640dSJohn Baldwin 	db_printf(" waiters: ");
11684e7f640dSJohn Baldwin 	switch(sx->sx_lock &
11694e7f640dSJohn Baldwin 	    (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
11704e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS:
11714e7f640dSJohn Baldwin 		db_printf("shared\n");
11724e7f640dSJohn Baldwin 		break;
11734e7f640dSJohn Baldwin 	case SX_LOCK_EXCLUSIVE_WAITERS:
11744e7f640dSJohn Baldwin 		db_printf("exclusive\n");
11754e7f640dSJohn Baldwin 		break;
11764e7f640dSJohn Baldwin 	case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
11774e7f640dSJohn Baldwin 		db_printf("exclusive and shared\n");
11784e7f640dSJohn Baldwin 		break;
11794e7f640dSJohn Baldwin 	default:
11804e7f640dSJohn Baldwin 		db_printf("none\n");
11814e7f640dSJohn Baldwin 	}
1182d272fe53SJohn Baldwin }
1183462a7addSJohn Baldwin 
1184462a7addSJohn Baldwin /*
1185462a7addSJohn Baldwin  * Check to see if a thread that is blocked on a sleep queue is actually
1186462a7addSJohn Baldwin  * blocked on an sx lock.  If so, output some details and return true.
1187462a7addSJohn Baldwin  * If the lock has an exclusive owner, return that in *ownerp.
1188462a7addSJohn Baldwin  */
1189462a7addSJohn Baldwin int
1190462a7addSJohn Baldwin sx_chain(struct thread *td, struct thread **ownerp)
1191462a7addSJohn Baldwin {
1192462a7addSJohn Baldwin 	struct sx *sx;
1193462a7addSJohn Baldwin 
1194462a7addSJohn Baldwin 	/*
11954e7f640dSJohn Baldwin 	 * Check to see if this thread is blocked on an sx lock.
11964e7f640dSJohn Baldwin 	 * First, we check the lock class.  If that is ok, then we
11974e7f640dSJohn Baldwin 	 * compare the lock name against the wait message.
1198462a7addSJohn Baldwin 	 */
11994e7f640dSJohn Baldwin 	sx = td->td_wchan;
12004e7f640dSJohn Baldwin 	if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
12014e7f640dSJohn Baldwin 	    sx->lock_object.lo_name != td->td_wmesg)
1202462a7addSJohn Baldwin 		return (0);
1203462a7addSJohn Baldwin 
1204462a7addSJohn Baldwin 	/* We think we have an sx lock, so output some details. */
1205462a7addSJohn Baldwin 	db_printf("blocked on sx \"%s\" ", td->td_wmesg);
12064e7f640dSJohn Baldwin 	*ownerp = sx_xholder(sx);
12074e7f640dSJohn Baldwin 	if (sx->sx_lock & SX_LOCK_SHARED)
12084e7f640dSJohn Baldwin 		db_printf("SLOCK (count %ju)\n",
12094e7f640dSJohn Baldwin 		    (uintmax_t)SX_SHARERS(sx->sx_lock));
12104e7f640dSJohn Baldwin 	else
1211462a7addSJohn Baldwin 		db_printf("XLOCK\n");
1212462a7addSJohn Baldwin 	return (1);
1213462a7addSJohn Baldwin }
1214d272fe53SJohn Baldwin #endif
1215