xref: /freebsd/sys/kern/kern_sx.c (revision 4b63a7c678a1291c8056de3770cfa393773b1f94)
1 /*-
2  * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org>
3  * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice(s), this list of conditions and the following disclaimer as
11  *    the first lines of this file unmodified other than the possible
12  *    addition of one or more copyright notices.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice(s), this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
27  * DAMAGE.
28  */
29 
30 /*
31  * Shared/exclusive locks.  This implementation attempts to ensure
32  * deterministic lock granting behavior, so that slocks and xlocks are
33  * interleaved.
34  *
35  * Priority propagation will not generally raise the priority of lock holders,
36  * so should not be relied upon in combination with sx locks.
37  */
38 
39 #include "opt_ddb.h"
40 #include "opt_hwpmc_hooks.h"
41 #include "opt_no_adaptive_sx.h"
42 
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kdb.h>
49 #include <sys/ktr.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/proc.h>
53 #include <sys/sched.h>
54 #include <sys/sleepqueue.h>
55 #include <sys/sx.h>
56 #include <sys/sysctl.h>
57 
58 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
59 #include <machine/cpu.h>
60 #endif
61 
62 #ifdef DDB
63 #include <ddb/ddb.h>
64 #endif
65 
66 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
67 #define	ADAPTIVE_SX
68 #endif
69 
70 CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE);
71 
72 #ifdef HWPMC_HOOKS
73 #include <sys/pmckern.h>
74 PMC_SOFT_DECLARE( , , lock, failed);
75 #endif
76 
77 /* Handy macros for sleep queues. */
78 #define	SQ_EXCLUSIVE_QUEUE	0
79 #define	SQ_SHARED_QUEUE		1
80 
81 /*
82  * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file.  We
83  * drop Giant anytime we have to sleep or if we adaptively spin.
84  */
85 #define	GIANT_DECLARE							\
86 	int _giantcnt = 0;						\
87 	WITNESS_SAVE_DECL(Giant)					\
88 
89 #define	GIANT_SAVE() do {						\
90 	if (mtx_owned(&Giant)) {					\
91 		WITNESS_SAVE(&Giant.lock_object, Giant);		\
92 		while (mtx_owned(&Giant)) {				\
93 			_giantcnt++;					\
94 			mtx_unlock(&Giant);				\
95 		}							\
96 	}								\
97 } while (0)
98 
99 #define GIANT_RESTORE() do {						\
100 	if (_giantcnt > 0) {						\
101 		mtx_assert(&Giant, MA_NOTOWNED);			\
102 		while (_giantcnt--)					\
103 			mtx_lock(&Giant);				\
104 		WITNESS_RESTORE(&Giant.lock_object, Giant);		\
105 	}								\
106 } while (0)
107 
108 /*
109  * Returns true if an exclusive lock is recursed.  It assumes
110  * curthread currently has an exclusive lock.
111  */
112 #define	sx_recursed(sx)		((sx)->sx_recurse != 0)
113 
114 static void	assert_sx(const struct lock_object *lock, int what);
115 #ifdef DDB
116 static void	db_show_sx(const struct lock_object *lock);
117 #endif
118 static void	lock_sx(struct lock_object *lock, uintptr_t how);
119 #ifdef KDTRACE_HOOKS
120 static int	owner_sx(const struct lock_object *lock, struct thread **owner);
121 #endif
122 static uintptr_t unlock_sx(struct lock_object *lock);
123 
124 struct lock_class lock_class_sx = {
125 	.lc_name = "sx",
126 	.lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
127 	.lc_assert = assert_sx,
128 #ifdef DDB
129 	.lc_ddb_show = db_show_sx,
130 #endif
131 	.lc_lock = lock_sx,
132 	.lc_unlock = unlock_sx,
133 #ifdef KDTRACE_HOOKS
134 	.lc_owner = owner_sx,
135 #endif
136 };
137 
138 #ifndef INVARIANTS
139 #define	_sx_assert(sx, what, file, line)
140 #endif
141 
142 #ifdef ADAPTIVE_SX
143 static u_int asx_retries = 10;
144 static u_int asx_loops = 10000;
145 static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
146 SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
147 SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
148 #endif
149 
150 void
151 assert_sx(const struct lock_object *lock, int what)
152 {
153 
154 	sx_assert((const struct sx *)lock, what);
155 }
156 
157 void
158 lock_sx(struct lock_object *lock, uintptr_t how)
159 {
160 	struct sx *sx;
161 
162 	sx = (struct sx *)lock;
163 	if (how)
164 		sx_slock(sx);
165 	else
166 		sx_xlock(sx);
167 }
168 
169 uintptr_t
170 unlock_sx(struct lock_object *lock)
171 {
172 	struct sx *sx;
173 
174 	sx = (struct sx *)lock;
175 	sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
176 	if (sx_xlocked(sx)) {
177 		sx_xunlock(sx);
178 		return (0);
179 	} else {
180 		sx_sunlock(sx);
181 		return (1);
182 	}
183 }
184 
185 #ifdef KDTRACE_HOOKS
186 int
187 owner_sx(const struct lock_object *lock, struct thread **owner)
188 {
189         const struct sx *sx = (const struct sx *)lock;
190 	uintptr_t x = sx->sx_lock;
191 
192         *owner = (struct thread *)SX_OWNER(x);
193         return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
194 	    (*owner != NULL));
195 }
196 #endif
197 
198 void
199 sx_sysinit(void *arg)
200 {
201 	struct sx_args *sargs = arg;
202 
203 	sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
204 }
205 
206 void
207 sx_init_flags(struct sx *sx, const char *description, int opts)
208 {
209 	int flags;
210 
211 	MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
212 	    SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
213 	ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
214 	    ("%s: sx_lock not aligned for %s: %p", __func__, description,
215 	    &sx->sx_lock));
216 
217 	flags = LO_SLEEPABLE | LO_UPGRADABLE;
218 	if (opts & SX_DUPOK)
219 		flags |= LO_DUPOK;
220 	if (opts & SX_NOPROFILE)
221 		flags |= LO_NOPROFILE;
222 	if (!(opts & SX_NOWITNESS))
223 		flags |= LO_WITNESS;
224 	if (opts & SX_RECURSE)
225 		flags |= LO_RECURSABLE;
226 	if (opts & SX_QUIET)
227 		flags |= LO_QUIET;
228 
229 	flags |= opts & SX_NOADAPTIVE;
230 	lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
231 	sx->sx_lock = SX_LOCK_UNLOCKED;
232 	sx->sx_recurse = 0;
233 }
234 
235 void
236 sx_destroy(struct sx *sx)
237 {
238 
239 	KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
240 	KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
241 	sx->sx_lock = SX_LOCK_DESTROYED;
242 	lock_destroy(&sx->lock_object);
243 }
244 
245 int
246 _sx_slock(struct sx *sx, int opts, const char *file, int line)
247 {
248 	int error = 0;
249 
250 	if (SCHEDULER_STOPPED())
251 		return (0);
252 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
253 	    ("sx_slock() by idle thread %p on sx %s @ %s:%d",
254 	    curthread, sx->lock_object.lo_name, file, line));
255 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
256 	    ("sx_slock() of destroyed sx @ %s:%d", file, line));
257 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
258 	error = __sx_slock(sx, opts, file, line);
259 	if (!error) {
260 		LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
261 		WITNESS_LOCK(&sx->lock_object, 0, file, line);
262 		curthread->td_locks++;
263 	}
264 
265 	return (error);
266 }
267 
268 int
269 sx_try_slock_(struct sx *sx, const char *file, int line)
270 {
271 	uintptr_t x;
272 
273 	if (SCHEDULER_STOPPED())
274 		return (1);
275 
276 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
277 	    ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
278 	    curthread, sx->lock_object.lo_name, file, line));
279 
280 	for (;;) {
281 		x = sx->sx_lock;
282 		KASSERT(x != SX_LOCK_DESTROYED,
283 		    ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
284 		if (!(x & SX_LOCK_SHARED))
285 			break;
286 		if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
287 			LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
288 			WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
289 			curthread->td_locks++;
290 			return (1);
291 		}
292 	}
293 
294 	LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
295 	return (0);
296 }
297 
298 int
299 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
300 {
301 	int error = 0;
302 
303 	if (SCHEDULER_STOPPED())
304 		return (0);
305 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
306 	    ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
307 	    curthread, sx->lock_object.lo_name, file, line));
308 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
309 	    ("sx_xlock() of destroyed sx @ %s:%d", file, line));
310 	WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
311 	    line, NULL);
312 	error = __sx_xlock(sx, curthread, opts, file, line);
313 	if (!error) {
314 		LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
315 		    file, line);
316 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
317 		curthread->td_locks++;
318 	}
319 
320 	return (error);
321 }
322 
323 int
324 sx_try_xlock_(struct sx *sx, const char *file, int line)
325 {
326 	int rval;
327 
328 	if (SCHEDULER_STOPPED())
329 		return (1);
330 
331 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
332 	    ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
333 	    curthread, sx->lock_object.lo_name, file, line));
334 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
335 	    ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
336 
337 	if (sx_xlocked(sx) &&
338 	    (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
339 		sx->sx_recurse++;
340 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
341 		rval = 1;
342 	} else
343 		rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
344 		    (uintptr_t)curthread);
345 	LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
346 	if (rval) {
347 		WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
348 		    file, line);
349 		curthread->td_locks++;
350 	}
351 
352 	return (rval);
353 }
354 
355 void
356 _sx_sunlock(struct sx *sx, const char *file, int line)
357 {
358 
359 	if (SCHEDULER_STOPPED())
360 		return;
361 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
362 	    ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
363 	_sx_assert(sx, SA_SLOCKED, file, line);
364 	WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
365 	LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
366 	__sx_sunlock(sx, file, line);
367 	curthread->td_locks--;
368 }
369 
370 void
371 _sx_xunlock(struct sx *sx, const char *file, int line)
372 {
373 
374 	if (SCHEDULER_STOPPED())
375 		return;
376 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
377 	    ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
378 	_sx_assert(sx, SA_XLOCKED, file, line);
379 	WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
380 	LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
381 	    line);
382 	__sx_xunlock(sx, curthread, file, line);
383 	curthread->td_locks--;
384 }
385 
386 /*
387  * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
388  * This will only succeed if this thread holds a single shared lock.
389  * Return 1 if if the upgrade succeed, 0 otherwise.
390  */
391 int
392 sx_try_upgrade_(struct sx *sx, const char *file, int line)
393 {
394 	uintptr_t x;
395 	int success;
396 
397 	if (SCHEDULER_STOPPED())
398 		return (1);
399 
400 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
401 	    ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
402 	_sx_assert(sx, SA_SLOCKED, file, line);
403 
404 	/*
405 	 * Try to switch from one shared lock to an exclusive lock.  We need
406 	 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
407 	 * we will wake up the exclusive waiters when we drop the lock.
408 	 */
409 	x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
410 	success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
411 	    (uintptr_t)curthread | x);
412 	LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
413 	if (success) {
414 		WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
415 		    file, line);
416 		LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
417 	}
418 	return (success);
419 }
420 
421 /*
422  * Downgrade an unrecursed exclusive lock into a single shared lock.
423  */
424 void
425 sx_downgrade_(struct sx *sx, const char *file, int line)
426 {
427 	uintptr_t x;
428 	int wakeup_swapper;
429 
430 	if (SCHEDULER_STOPPED())
431 		return;
432 
433 	KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
434 	    ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
435 	_sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
436 #ifndef INVARIANTS
437 	if (sx_recursed(sx))
438 		panic("downgrade of a recursed lock");
439 #endif
440 
441 	WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
442 
443 	/*
444 	 * Try to switch from an exclusive lock with no shared waiters
445 	 * to one sharer with no shared waiters.  If there are
446 	 * exclusive waiters, we don't need to lock the sleep queue so
447 	 * long as we preserve the flag.  We do one quick try and if
448 	 * that fails we grab the sleepq lock to keep the flags from
449 	 * changing and do it the slow way.
450 	 *
451 	 * We have to lock the sleep queue if there are shared waiters
452 	 * so we can wake them up.
453 	 */
454 	x = sx->sx_lock;
455 	if (!(x & SX_LOCK_SHARED_WAITERS) &&
456 	    atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
457 	    (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
458 		LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
459 		return;
460 	}
461 
462 	/*
463 	 * Lock the sleep queue so we can read the waiters bits
464 	 * without any races and wakeup any shared waiters.
465 	 */
466 	sleepq_lock(&sx->lock_object);
467 
468 	/*
469 	 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
470 	 * shared lock.  If there are any shared waiters, wake them up.
471 	 */
472 	wakeup_swapper = 0;
473 	x = sx->sx_lock;
474 	atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
475 	    (x & SX_LOCK_EXCLUSIVE_WAITERS));
476 	if (x & SX_LOCK_SHARED_WAITERS)
477 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
478 		    0, SQ_SHARED_QUEUE);
479 	sleepq_release(&sx->lock_object);
480 
481 	LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
482 	LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
483 
484 	if (wakeup_swapper)
485 		kick_proc0();
486 }
487 
488 /*
489  * This function represents the so-called 'hard case' for sx_xlock
490  * operation.  All 'easy case' failures are redirected to this.  Note
491  * that ideally this would be a static function, but it needs to be
492  * accessible from at least sx.h.
493  */
494 int
495 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
496     int line)
497 {
498 	GIANT_DECLARE;
499 #ifdef ADAPTIVE_SX
500 	volatile struct thread *owner;
501 	u_int i, spintries = 0;
502 #endif
503 	uintptr_t x;
504 #ifdef LOCK_PROFILING
505 	uint64_t waittime = 0;
506 	int contested = 0;
507 #endif
508 	int error = 0;
509 #ifdef	KDTRACE_HOOKS
510 	uint64_t spin_cnt = 0;
511 	uint64_t sleep_cnt = 0;
512 	int64_t sleep_time = 0;
513 #endif
514 
515 	if (SCHEDULER_STOPPED())
516 		return (0);
517 
518 	/* If we already hold an exclusive lock, then recurse. */
519 	if (sx_xlocked(sx)) {
520 		KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
521 	    ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
522 		    sx->lock_object.lo_name, file, line));
523 		sx->sx_recurse++;
524 		atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
525 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
526 			CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
527 		return (0);
528 	}
529 
530 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
531 		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
532 		    sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
533 
534 	while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
535 #ifdef KDTRACE_HOOKS
536 		spin_cnt++;
537 #endif
538 #ifdef HWPMC_HOOKS
539 		PMC_SOFT_CALL( , , lock, failed);
540 #endif
541 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
542 		    &waittime);
543 #ifdef ADAPTIVE_SX
544 		/*
545 		 * If the lock is write locked and the owner is
546 		 * running on another CPU, spin until the owner stops
547 		 * running or the state of the lock changes.
548 		 */
549 		x = sx->sx_lock;
550 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
551 			if ((x & SX_LOCK_SHARED) == 0) {
552 				x = SX_OWNER(x);
553 				owner = (struct thread *)x;
554 				if (TD_IS_RUNNING(owner)) {
555 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
556 						CTR3(KTR_LOCK,
557 					    "%s: spinning on %p held by %p",
558 						    __func__, sx, owner);
559 					KTR_STATE1(KTR_SCHED, "thread",
560 					    sched_tdname(curthread), "spinning",
561 					    "lockname:\"%s\"",
562 					    sx->lock_object.lo_name);
563 					GIANT_SAVE();
564 					while (SX_OWNER(sx->sx_lock) == x &&
565 					    TD_IS_RUNNING(owner)) {
566 						cpu_spinwait();
567 #ifdef KDTRACE_HOOKS
568 						spin_cnt++;
569 #endif
570 					}
571 					KTR_STATE0(KTR_SCHED, "thread",
572 					    sched_tdname(curthread), "running");
573 					continue;
574 				}
575 			} else if (SX_SHARERS(x) && spintries < asx_retries) {
576 				KTR_STATE1(KTR_SCHED, "thread",
577 				    sched_tdname(curthread), "spinning",
578 				    "lockname:\"%s\"", sx->lock_object.lo_name);
579 				GIANT_SAVE();
580 				spintries++;
581 				for (i = 0; i < asx_loops; i++) {
582 					if (LOCK_LOG_TEST(&sx->lock_object, 0))
583 						CTR4(KTR_LOCK,
584 				    "%s: shared spinning on %p with %u and %u",
585 						    __func__, sx, spintries, i);
586 					x = sx->sx_lock;
587 					if ((x & SX_LOCK_SHARED) == 0 ||
588 					    SX_SHARERS(x) == 0)
589 						break;
590 					cpu_spinwait();
591 #ifdef KDTRACE_HOOKS
592 					spin_cnt++;
593 #endif
594 				}
595 				KTR_STATE0(KTR_SCHED, "thread",
596 				    sched_tdname(curthread), "running");
597 				if (i != asx_loops)
598 					continue;
599 			}
600 		}
601 #endif
602 
603 		sleepq_lock(&sx->lock_object);
604 		x = sx->sx_lock;
605 
606 		/*
607 		 * If the lock was released while spinning on the
608 		 * sleep queue chain lock, try again.
609 		 */
610 		if (x == SX_LOCK_UNLOCKED) {
611 			sleepq_release(&sx->lock_object);
612 			continue;
613 		}
614 
615 #ifdef ADAPTIVE_SX
616 		/*
617 		 * The current lock owner might have started executing
618 		 * on another CPU (or the lock could have changed
619 		 * owners) while we were waiting on the sleep queue
620 		 * chain lock.  If so, drop the sleep queue lock and try
621 		 * again.
622 		 */
623 		if (!(x & SX_LOCK_SHARED) &&
624 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
625 			owner = (struct thread *)SX_OWNER(x);
626 			if (TD_IS_RUNNING(owner)) {
627 				sleepq_release(&sx->lock_object);
628 				continue;
629 			}
630 		}
631 #endif
632 
633 		/*
634 		 * If an exclusive lock was released with both shared
635 		 * and exclusive waiters and a shared waiter hasn't
636 		 * woken up and acquired the lock yet, sx_lock will be
637 		 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
638 		 * If we see that value, try to acquire it once.  Note
639 		 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
640 		 * as there are other exclusive waiters still.  If we
641 		 * fail, restart the loop.
642 		 */
643 		if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
644 			if (atomic_cmpset_acq_ptr(&sx->sx_lock,
645 			    SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
646 			    tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
647 				sleepq_release(&sx->lock_object);
648 				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
649 				    __func__, sx);
650 				break;
651 			}
652 			sleepq_release(&sx->lock_object);
653 			continue;
654 		}
655 
656 		/*
657 		 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS.  If we fail,
658 		 * than loop back and retry.
659 		 */
660 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
661 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
662 			    x | SX_LOCK_EXCLUSIVE_WAITERS)) {
663 				sleepq_release(&sx->lock_object);
664 				continue;
665 			}
666 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
667 				CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
668 				    __func__, sx);
669 		}
670 
671 		/*
672 		 * Since we have been unable to acquire the exclusive
673 		 * lock and the exclusive waiters flag is set, we have
674 		 * to sleep.
675 		 */
676 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
677 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
678 			    __func__, sx);
679 
680 #ifdef KDTRACE_HOOKS
681 		sleep_time -= lockstat_nsecs();
682 #endif
683 		GIANT_SAVE();
684 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
685 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
686 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
687 		if (!(opts & SX_INTERRUPTIBLE))
688 			sleepq_wait(&sx->lock_object, 0);
689 		else
690 			error = sleepq_wait_sig(&sx->lock_object, 0);
691 #ifdef KDTRACE_HOOKS
692 		sleep_time += lockstat_nsecs();
693 		sleep_cnt++;
694 #endif
695 		if (error) {
696 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
697 				CTR2(KTR_LOCK,
698 			"%s: interruptible sleep by %p suspended by signal",
699 				    __func__, sx);
700 			break;
701 		}
702 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
703 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
704 			    __func__, sx);
705 	}
706 
707 	GIANT_RESTORE();
708 	if (!error)
709 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
710 		    contested, waittime, file, line);
711 #ifdef KDTRACE_HOOKS
712 	if (sleep_time)
713 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
714 	if (spin_cnt > sleep_cnt)
715 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
716 #endif
717 	return (error);
718 }
719 
720 /*
721  * This function represents the so-called 'hard case' for sx_xunlock
722  * operation.  All 'easy case' failures are redirected to this.  Note
723  * that ideally this would be a static function, but it needs to be
724  * accessible from at least sx.h.
725  */
726 void
727 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
728 {
729 	uintptr_t x;
730 	int queue, wakeup_swapper;
731 
732 	if (SCHEDULER_STOPPED())
733 		return;
734 
735 	MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
736 
737 	/* If the lock is recursed, then unrecurse one level. */
738 	if (sx_xlocked(sx) && sx_recursed(sx)) {
739 		if ((--sx->sx_recurse) == 0)
740 			atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
741 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
742 			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
743 		return;
744 	}
745 	MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
746 	    SX_LOCK_EXCLUSIVE_WAITERS));
747 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
748 		CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
749 
750 	sleepq_lock(&sx->lock_object);
751 	x = SX_LOCK_UNLOCKED;
752 
753 	/*
754 	 * The wake up algorithm here is quite simple and probably not
755 	 * ideal.  It gives precedence to shared waiters if they are
756 	 * present.  For this condition, we have to preserve the
757 	 * state of the exclusive waiters flag.
758 	 * If interruptible sleeps left the shared queue empty avoid a
759 	 * starvation for the threads sleeping on the exclusive queue by giving
760 	 * them precedence and cleaning up the shared waiters bit anyway.
761 	 */
762 	if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
763 	    sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
764 		queue = SQ_SHARED_QUEUE;
765 		x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
766 	} else
767 		queue = SQ_EXCLUSIVE_QUEUE;
768 
769 	/* Wake up all the waiters for the specific queue. */
770 	if (LOCK_LOG_TEST(&sx->lock_object, 0))
771 		CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
772 		    __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
773 		    "exclusive");
774 	atomic_store_rel_ptr(&sx->sx_lock, x);
775 	wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
776 	    queue);
777 	sleepq_release(&sx->lock_object);
778 	if (wakeup_swapper)
779 		kick_proc0();
780 }
781 
782 /*
783  * This function represents the so-called 'hard case' for sx_slock
784  * operation.  All 'easy case' failures are redirected to this.  Note
785  * that ideally this would be a static function, but it needs to be
786  * accessible from at least sx.h.
787  */
788 int
789 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
790 {
791 	GIANT_DECLARE;
792 #ifdef ADAPTIVE_SX
793 	volatile struct thread *owner;
794 #endif
795 #ifdef LOCK_PROFILING
796 	uint64_t waittime = 0;
797 	int contested = 0;
798 #endif
799 	uintptr_t x;
800 	int error = 0;
801 #ifdef KDTRACE_HOOKS
802 	uint64_t spin_cnt = 0;
803 	uint64_t sleep_cnt = 0;
804 	int64_t sleep_time = 0;
805 #endif
806 
807 	if (SCHEDULER_STOPPED())
808 		return (0);
809 
810 	/*
811 	 * As with rwlocks, we don't make any attempt to try to block
812 	 * shared locks once there is an exclusive waiter.
813 	 */
814 	for (;;) {
815 #ifdef KDTRACE_HOOKS
816 		spin_cnt++;
817 #endif
818 		x = sx->sx_lock;
819 
820 		/*
821 		 * If no other thread has an exclusive lock then try to bump up
822 		 * the count of sharers.  Since we have to preserve the state
823 		 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
824 		 * shared lock loop back and retry.
825 		 */
826 		if (x & SX_LOCK_SHARED) {
827 			MPASS(!(x & SX_LOCK_SHARED_WAITERS));
828 			if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
829 			    x + SX_ONE_SHARER)) {
830 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
831 					CTR4(KTR_LOCK,
832 					    "%s: %p succeed %p -> %p", __func__,
833 					    sx, (void *)x,
834 					    (void *)(x + SX_ONE_SHARER));
835 				break;
836 			}
837 			continue;
838 		}
839 #ifdef HWPMC_HOOKS
840 		PMC_SOFT_CALL( , , lock, failed);
841 #endif
842 		lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
843 		    &waittime);
844 
845 #ifdef ADAPTIVE_SX
846 		/*
847 		 * If the owner is running on another CPU, spin until
848 		 * the owner stops running or the state of the lock
849 		 * changes.
850 		 */
851 		if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
852 			x = SX_OWNER(x);
853 			owner = (struct thread *)x;
854 			if (TD_IS_RUNNING(owner)) {
855 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
856 					CTR3(KTR_LOCK,
857 					    "%s: spinning on %p held by %p",
858 					    __func__, sx, owner);
859 				KTR_STATE1(KTR_SCHED, "thread",
860 				    sched_tdname(curthread), "spinning",
861 				    "lockname:\"%s\"", sx->lock_object.lo_name);
862 				GIANT_SAVE();
863 				while (SX_OWNER(sx->sx_lock) == x &&
864 				    TD_IS_RUNNING(owner)) {
865 #ifdef KDTRACE_HOOKS
866 					spin_cnt++;
867 #endif
868 					cpu_spinwait();
869 				}
870 				KTR_STATE0(KTR_SCHED, "thread",
871 				    sched_tdname(curthread), "running");
872 				continue;
873 			}
874 		}
875 #endif
876 
877 		/*
878 		 * Some other thread already has an exclusive lock, so
879 		 * start the process of blocking.
880 		 */
881 		sleepq_lock(&sx->lock_object);
882 		x = sx->sx_lock;
883 
884 		/*
885 		 * The lock could have been released while we spun.
886 		 * In this case loop back and retry.
887 		 */
888 		if (x & SX_LOCK_SHARED) {
889 			sleepq_release(&sx->lock_object);
890 			continue;
891 		}
892 
893 #ifdef ADAPTIVE_SX
894 		/*
895 		 * If the owner is running on another CPU, spin until
896 		 * the owner stops running or the state of the lock
897 		 * changes.
898 		 */
899 		if (!(x & SX_LOCK_SHARED) &&
900 		    (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
901 			owner = (struct thread *)SX_OWNER(x);
902 			if (TD_IS_RUNNING(owner)) {
903 				sleepq_release(&sx->lock_object);
904 				continue;
905 			}
906 		}
907 #endif
908 
909 		/*
910 		 * Try to set the SX_LOCK_SHARED_WAITERS flag.  If we
911 		 * fail to set it drop the sleep queue lock and loop
912 		 * back.
913 		 */
914 		if (!(x & SX_LOCK_SHARED_WAITERS)) {
915 			if (!atomic_cmpset_ptr(&sx->sx_lock, x,
916 			    x | SX_LOCK_SHARED_WAITERS)) {
917 				sleepq_release(&sx->lock_object);
918 				continue;
919 			}
920 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
921 				CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
922 				    __func__, sx);
923 		}
924 
925 		/*
926 		 * Since we have been unable to acquire the shared lock,
927 		 * we have to sleep.
928 		 */
929 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
930 			CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
931 			    __func__, sx);
932 
933 #ifdef KDTRACE_HOOKS
934 		sleep_time -= lockstat_nsecs();
935 #endif
936 		GIANT_SAVE();
937 		sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
938 		    SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
939 		    SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
940 		if (!(opts & SX_INTERRUPTIBLE))
941 			sleepq_wait(&sx->lock_object, 0);
942 		else
943 			error = sleepq_wait_sig(&sx->lock_object, 0);
944 #ifdef KDTRACE_HOOKS
945 		sleep_time += lockstat_nsecs();
946 		sleep_cnt++;
947 #endif
948 		if (error) {
949 			if (LOCK_LOG_TEST(&sx->lock_object, 0))
950 				CTR2(KTR_LOCK,
951 			"%s: interruptible sleep by %p suspended by signal",
952 				    __func__, sx);
953 			break;
954 		}
955 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
956 			CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
957 			    __func__, sx);
958 	}
959 	if (error == 0)
960 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
961 		    contested, waittime, file, line);
962 #ifdef KDTRACE_HOOKS
963 	if (sleep_time)
964 		LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
965 	if (spin_cnt > sleep_cnt)
966 		LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
967 #endif
968 	GIANT_RESTORE();
969 	return (error);
970 }
971 
972 /*
973  * This function represents the so-called 'hard case' for sx_sunlock
974  * operation.  All 'easy case' failures are redirected to this.  Note
975  * that ideally this would be a static function, but it needs to be
976  * accessible from at least sx.h.
977  */
978 void
979 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
980 {
981 	uintptr_t x;
982 	int wakeup_swapper;
983 
984 	if (SCHEDULER_STOPPED())
985 		return;
986 
987 	for (;;) {
988 		x = sx->sx_lock;
989 
990 		/*
991 		 * We should never have sharers while at least one thread
992 		 * holds a shared lock.
993 		 */
994 		KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
995 		    ("%s: waiting sharers", __func__));
996 
997 		/*
998 		 * See if there is more than one shared lock held.  If
999 		 * so, just drop one and return.
1000 		 */
1001 		if (SX_SHARERS(x) > 1) {
1002 			if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
1003 			    x - SX_ONE_SHARER)) {
1004 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
1005 					CTR4(KTR_LOCK,
1006 					    "%s: %p succeeded %p -> %p",
1007 					    __func__, sx, (void *)x,
1008 					    (void *)(x - SX_ONE_SHARER));
1009 				break;
1010 			}
1011 			continue;
1012 		}
1013 
1014 		/*
1015 		 * If there aren't any waiters for an exclusive lock,
1016 		 * then try to drop it quickly.
1017 		 */
1018 		if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
1019 			MPASS(x == SX_SHARERS_LOCK(1));
1020 			if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1021 			    SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
1022 				if (LOCK_LOG_TEST(&sx->lock_object, 0))
1023 					CTR2(KTR_LOCK, "%s: %p last succeeded",
1024 					    __func__, sx);
1025 				break;
1026 			}
1027 			continue;
1028 		}
1029 
1030 		/*
1031 		 * At this point, there should just be one sharer with
1032 		 * exclusive waiters.
1033 		 */
1034 		MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
1035 
1036 		sleepq_lock(&sx->lock_object);
1037 
1038 		/*
1039 		 * Wake up semantic here is quite simple:
1040 		 * Just wake up all the exclusive waiters.
1041 		 * Note that the state of the lock could have changed,
1042 		 * so if it fails loop back and retry.
1043 		 */
1044 		if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
1045 		    SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
1046 		    SX_LOCK_UNLOCKED)) {
1047 			sleepq_release(&sx->lock_object);
1048 			continue;
1049 		}
1050 		if (LOCK_LOG_TEST(&sx->lock_object, 0))
1051 			CTR2(KTR_LOCK, "%s: %p waking up all thread on"
1052 			    "exclusive queue", __func__, sx);
1053 		wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1054 		    0, SQ_EXCLUSIVE_QUEUE);
1055 		sleepq_release(&sx->lock_object);
1056 		if (wakeup_swapper)
1057 			kick_proc0();
1058 		break;
1059 	}
1060 }
1061 
1062 #ifdef INVARIANT_SUPPORT
1063 #ifndef INVARIANTS
1064 #undef	_sx_assert
1065 #endif
1066 
1067 /*
1068  * In the non-WITNESS case, sx_assert() can only detect that at least
1069  * *some* thread owns an slock, but it cannot guarantee that *this*
1070  * thread owns an slock.
1071  */
1072 void
1073 _sx_assert(const struct sx *sx, int what, const char *file, int line)
1074 {
1075 #ifndef WITNESS
1076 	int slocked = 0;
1077 #endif
1078 
1079 	if (panicstr != NULL)
1080 		return;
1081 	switch (what) {
1082 	case SA_SLOCKED:
1083 	case SA_SLOCKED | SA_NOTRECURSED:
1084 	case SA_SLOCKED | SA_RECURSED:
1085 #ifndef WITNESS
1086 		slocked = 1;
1087 		/* FALLTHROUGH */
1088 #endif
1089 	case SA_LOCKED:
1090 	case SA_LOCKED | SA_NOTRECURSED:
1091 	case SA_LOCKED | SA_RECURSED:
1092 #ifdef WITNESS
1093 		witness_assert(&sx->lock_object, what, file, line);
1094 #else
1095 		/*
1096 		 * If some other thread has an exclusive lock or we
1097 		 * have one and are asserting a shared lock, fail.
1098 		 * Also, if no one has a lock at all, fail.
1099 		 */
1100 		if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1101 		    (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1102 		    sx_xholder(sx) != curthread)))
1103 			panic("Lock %s not %slocked @ %s:%d\n",
1104 			    sx->lock_object.lo_name, slocked ? "share " : "",
1105 			    file, line);
1106 
1107 		if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1108 			if (sx_recursed(sx)) {
1109 				if (what & SA_NOTRECURSED)
1110 					panic("Lock %s recursed @ %s:%d\n",
1111 					    sx->lock_object.lo_name, file,
1112 					    line);
1113 			} else if (what & SA_RECURSED)
1114 				panic("Lock %s not recursed @ %s:%d\n",
1115 				    sx->lock_object.lo_name, file, line);
1116 		}
1117 #endif
1118 		break;
1119 	case SA_XLOCKED:
1120 	case SA_XLOCKED | SA_NOTRECURSED:
1121 	case SA_XLOCKED | SA_RECURSED:
1122 		if (sx_xholder(sx) != curthread)
1123 			panic("Lock %s not exclusively locked @ %s:%d\n",
1124 			    sx->lock_object.lo_name, file, line);
1125 		if (sx_recursed(sx)) {
1126 			if (what & SA_NOTRECURSED)
1127 				panic("Lock %s recursed @ %s:%d\n",
1128 				    sx->lock_object.lo_name, file, line);
1129 		} else if (what & SA_RECURSED)
1130 			panic("Lock %s not recursed @ %s:%d\n",
1131 			    sx->lock_object.lo_name, file, line);
1132 		break;
1133 	case SA_UNLOCKED:
1134 #ifdef WITNESS
1135 		witness_assert(&sx->lock_object, what, file, line);
1136 #else
1137 		/*
1138 		 * If we hold an exclusve lock fail.  We can't
1139 		 * reliably check to see if we hold a shared lock or
1140 		 * not.
1141 		 */
1142 		if (sx_xholder(sx) == curthread)
1143 			panic("Lock %s exclusively locked @ %s:%d\n",
1144 			    sx->lock_object.lo_name, file, line);
1145 #endif
1146 		break;
1147 	default:
1148 		panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1149 		    line);
1150 	}
1151 }
1152 #endif	/* INVARIANT_SUPPORT */
1153 
1154 #ifdef DDB
1155 static void
1156 db_show_sx(const struct lock_object *lock)
1157 {
1158 	struct thread *td;
1159 	const struct sx *sx;
1160 
1161 	sx = (const struct sx *)lock;
1162 
1163 	db_printf(" state: ");
1164 	if (sx->sx_lock == SX_LOCK_UNLOCKED)
1165 		db_printf("UNLOCKED\n");
1166 	else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1167 		db_printf("DESTROYED\n");
1168 		return;
1169 	} else if (sx->sx_lock & SX_LOCK_SHARED)
1170 		db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1171 	else {
1172 		td = sx_xholder(sx);
1173 		db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1174 		    td->td_tid, td->td_proc->p_pid, td->td_name);
1175 		if (sx_recursed(sx))
1176 			db_printf(" recursed: %d\n", sx->sx_recurse);
1177 	}
1178 
1179 	db_printf(" waiters: ");
1180 	switch(sx->sx_lock &
1181 	    (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1182 	case SX_LOCK_SHARED_WAITERS:
1183 		db_printf("shared\n");
1184 		break;
1185 	case SX_LOCK_EXCLUSIVE_WAITERS:
1186 		db_printf("exclusive\n");
1187 		break;
1188 	case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1189 		db_printf("exclusive and shared\n");
1190 		break;
1191 	default:
1192 		db_printf("none\n");
1193 	}
1194 }
1195 
1196 /*
1197  * Check to see if a thread that is blocked on a sleep queue is actually
1198  * blocked on an sx lock.  If so, output some details and return true.
1199  * If the lock has an exclusive owner, return that in *ownerp.
1200  */
1201 int
1202 sx_chain(struct thread *td, struct thread **ownerp)
1203 {
1204 	struct sx *sx;
1205 
1206 	/*
1207 	 * Check to see if this thread is blocked on an sx lock.
1208 	 * First, we check the lock class.  If that is ok, then we
1209 	 * compare the lock name against the wait message.
1210 	 */
1211 	sx = td->td_wchan;
1212 	if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1213 	    sx->lock_object.lo_name != td->td_wmesg)
1214 		return (0);
1215 
1216 	/* We think we have an sx lock, so output some details. */
1217 	db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1218 	*ownerp = sx_xholder(sx);
1219 	if (sx->sx_lock & SX_LOCK_SHARED)
1220 		db_printf("SLOCK (count %ju)\n",
1221 		    (uintmax_t)SX_SHARERS(sx->sx_lock));
1222 	else
1223 		db_printf("XLOCK\n");
1224 	return (1);
1225 }
1226 #endif
1227