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