2 * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org>
3 * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
31 * Shared/exclusive locks. This implementation attempts to ensure
32 * deterministic lock granting behavior, so that slocks and xlocks are
35 * Priority propagation will not generally raise the priority of lock holders,
36 * so should not be relied upon in combination with sx locks.
40 #include "opt_hwpmc_hooks.h"
41 #include "opt_no_adaptive_sx.h"
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
51 #include <sys/mutex.h>
53 #include <sys/sleepqueue.h>
55 #include <sys/sysctl.h>
57 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
58 #include <machine/cpu.h>
65 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
69 CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE);
72 #include <sys/pmckern.h>
73 PMC_SOFT_DECLARE( , , lock, failed);
76 /* Handy macros for sleep queues. */
77 #define SQ_EXCLUSIVE_QUEUE 0
78 #define SQ_SHARED_QUEUE 1
81 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
82 * drop Giant anytime we have to sleep or if we adaptively spin.
84 #define GIANT_DECLARE \
86 WITNESS_SAVE_DECL(Giant) \
88 #define GIANT_SAVE() do { \
89 if (mtx_owned(&Giant)) { \
90 WITNESS_SAVE(&Giant.lock_object, Giant); \
91 while (mtx_owned(&Giant)) { \
98 #define GIANT_RESTORE() do { \
99 if (_giantcnt > 0) { \
100 mtx_assert(&Giant, MA_NOTOWNED); \
101 while (_giantcnt--) \
103 WITNESS_RESTORE(&Giant.lock_object, Giant); \
108 * Returns true if an exclusive lock is recursed. It assumes
109 * curthread currently has an exclusive lock.
111 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
113 static void assert_sx(const struct lock_object *lock, int what);
115 static void db_show_sx(const struct lock_object *lock);
117 static void lock_sx(struct lock_object *lock, uintptr_t how);
119 static int owner_sx(const struct lock_object *lock, struct thread **owner);
121 static uintptr_t unlock_sx(struct lock_object *lock);
123 struct lock_class lock_class_sx = {
125 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
126 .lc_assert = assert_sx,
128 .lc_ddb_show = db_show_sx,
131 .lc_unlock = unlock_sx,
133 .lc_owner = owner_sx,
138 #define _sx_assert(sx, what, file, line)
142 static u_int asx_retries = 10;
143 static u_int asx_loops = 10000;
144 static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
145 SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
146 SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
150 assert_sx(const struct lock_object *lock, int what)
153 sx_assert((const struct sx *)lock, what);
157 lock_sx(struct lock_object *lock, uintptr_t how)
161 sx = (struct sx *)lock;
169 unlock_sx(struct lock_object *lock)
173 sx = (struct sx *)lock;
174 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
175 if (sx_xlocked(sx)) {
186 owner_sx(const struct lock_object *lock, struct thread **owner)
188 const struct sx *sx = (const struct sx *)lock;
189 uintptr_t x = sx->sx_lock;
191 *owner = (struct thread *)SX_OWNER(x);
192 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
198 sx_sysinit(void *arg)
200 struct sx_args *sargs = arg;
202 sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
206 sx_init_flags(struct sx *sx, const char *description, int opts)
210 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
211 SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
212 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
213 ("%s: sx_lock not aligned for %s: %p", __func__, description,
216 flags = LO_SLEEPABLE | LO_UPGRADABLE;
219 if (opts & SX_NOPROFILE)
220 flags |= LO_NOPROFILE;
221 if (!(opts & SX_NOWITNESS))
223 if (opts & SX_RECURSE)
224 flags |= LO_RECURSABLE;
228 flags |= opts & SX_NOADAPTIVE;
229 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
230 sx->sx_lock = SX_LOCK_UNLOCKED;
235 sx_destroy(struct sx *sx)
238 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
239 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
240 sx->sx_lock = SX_LOCK_DESTROYED;
241 lock_destroy(&sx->lock_object);
245 _sx_slock(struct sx *sx, int opts, const char *file, int line)
249 if (SCHEDULER_STOPPED())
251 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
252 ("sx_slock() by idle thread %p on sx %s @ %s:%d",
253 curthread, sx->lock_object.lo_name, file, line));
254 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
255 ("sx_slock() of destroyed sx @ %s:%d", file, line));
256 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
257 error = __sx_slock(sx, opts, file, line);
259 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
260 WITNESS_LOCK(&sx->lock_object, 0, file, line);
261 curthread->td_locks++;
268 sx_try_slock_(struct sx *sx, const char *file, int line)
272 if (SCHEDULER_STOPPED())
275 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
276 ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
277 curthread, sx->lock_object.lo_name, file, line));
281 KASSERT(x != SX_LOCK_DESTROYED,
282 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
283 if (!(x & SX_LOCK_SHARED))
285 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
286 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
287 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
288 curthread->td_locks++;
293 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
298 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
302 if (SCHEDULER_STOPPED())
304 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
305 ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
306 curthread, sx->lock_object.lo_name, file, line));
307 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
308 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
309 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
311 error = __sx_xlock(sx, curthread, opts, file, line);
313 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
315 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
316 curthread->td_locks++;
323 sx_try_xlock_(struct sx *sx, const char *file, int line)
327 if (SCHEDULER_STOPPED())
330 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
331 ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
332 curthread, sx->lock_object.lo_name, file, line));
333 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
334 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
336 if (sx_xlocked(sx) &&
337 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
339 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
342 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
343 (uintptr_t)curthread);
344 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
346 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
348 curthread->td_locks++;
355 _sx_sunlock(struct sx *sx, const char *file, int line)
358 if (SCHEDULER_STOPPED())
360 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
361 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
362 _sx_assert(sx, SA_SLOCKED, file, line);
363 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
364 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
365 __sx_sunlock(sx, file, line);
366 curthread->td_locks--;
370 _sx_xunlock(struct sx *sx, const char *file, int line)
373 if (SCHEDULER_STOPPED())
375 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
376 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
377 _sx_assert(sx, SA_XLOCKED, file, line);
378 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
379 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
381 __sx_xunlock(sx, curthread, file, line);
382 curthread->td_locks--;
386 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
387 * This will only succeed if this thread holds a single shared lock.
388 * Return 1 if if the upgrade succeed, 0 otherwise.
391 sx_try_upgrade_(struct sx *sx, const char *file, int line)
396 if (SCHEDULER_STOPPED())
399 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
400 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
401 _sx_assert(sx, SA_SLOCKED, file, line);
404 * Try to switch from one shared lock to an exclusive lock. We need
405 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
406 * we will wake up the exclusive waiters when we drop the lock.
408 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
409 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
410 (uintptr_t)curthread | x);
411 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
413 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
415 LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
421 * Downgrade an unrecursed exclusive lock into a single shared lock.
424 sx_downgrade_(struct sx *sx, const char *file, int line)
429 if (SCHEDULER_STOPPED())
432 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
433 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
434 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
437 panic("downgrade of a recursed lock");
440 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
443 * Try to switch from an exclusive lock with no shared waiters
444 * to one sharer with no shared waiters. If there are
445 * exclusive waiters, we don't need to lock the sleep queue so
446 * long as we preserve the flag. We do one quick try and if
447 * that fails we grab the sleepq lock to keep the flags from
448 * changing and do it the slow way.
450 * We have to lock the sleep queue if there are shared waiters
451 * so we can wake them up.
454 if (!(x & SX_LOCK_SHARED_WAITERS) &&
455 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
456 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
457 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
462 * Lock the sleep queue so we can read the waiters bits
463 * without any races and wakeup any shared waiters.
465 sleepq_lock(&sx->lock_object);
468 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
469 * shared lock. If there are any shared waiters, wake them up.
473 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
474 (x & SX_LOCK_EXCLUSIVE_WAITERS));
475 if (x & SX_LOCK_SHARED_WAITERS)
476 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
478 sleepq_release(&sx->lock_object);
480 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
481 LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
488 * This function represents the so-called 'hard case' for sx_xlock
489 * operation. All 'easy case' failures are redirected to this. Note
490 * that ideally this would be a static function, but it needs to be
491 * accessible from at least sx.h.
494 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
499 volatile struct thread *owner;
500 u_int i, spintries = 0;
503 #ifdef LOCK_PROFILING
504 uint64_t waittime = 0;
509 uint64_t spin_cnt = 0;
510 uint64_t sleep_cnt = 0;
511 int64_t sleep_time = 0;
514 if (SCHEDULER_STOPPED())
517 /* If we already hold an exclusive lock, then recurse. */
518 if (sx_xlocked(sx)) {
519 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
520 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
521 sx->lock_object.lo_name, file, line));
523 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
524 if (LOCK_LOG_TEST(&sx->lock_object, 0))
525 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
529 if (LOCK_LOG_TEST(&sx->lock_object, 0))
530 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
531 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
533 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
538 PMC_SOFT_CALL( , , lock, failed);
540 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
544 * If the lock is write locked and the owner is
545 * running on another CPU, spin until the owner stops
546 * running or the state of the lock changes.
549 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
550 if ((x & SX_LOCK_SHARED) == 0) {
552 owner = (struct thread *)x;
553 if (TD_IS_RUNNING(owner)) {
554 if (LOCK_LOG_TEST(&sx->lock_object, 0))
556 "%s: spinning on %p held by %p",
557 __func__, sx, owner);
559 while (SX_OWNER(sx->sx_lock) == x &&
560 TD_IS_RUNNING(owner)) {
568 } else if (SX_SHARERS(x) && spintries < asx_retries) {
571 for (i = 0; i < asx_loops; i++) {
572 if (LOCK_LOG_TEST(&sx->lock_object, 0))
574 "%s: shared spinning on %p with %u and %u",
575 __func__, sx, spintries, i);
577 if ((x & SX_LOCK_SHARED) == 0 ||
591 sleepq_lock(&sx->lock_object);
595 * If the lock was released while spinning on the
596 * sleep queue chain lock, try again.
598 if (x == SX_LOCK_UNLOCKED) {
599 sleepq_release(&sx->lock_object);
605 * The current lock owner might have started executing
606 * on another CPU (or the lock could have changed
607 * owners) while we were waiting on the sleep queue
608 * chain lock. If so, drop the sleep queue lock and try
611 if (!(x & SX_LOCK_SHARED) &&
612 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
613 owner = (struct thread *)SX_OWNER(x);
614 if (TD_IS_RUNNING(owner)) {
615 sleepq_release(&sx->lock_object);
622 * If an exclusive lock was released with both shared
623 * and exclusive waiters and a shared waiter hasn't
624 * woken up and acquired the lock yet, sx_lock will be
625 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
626 * If we see that value, try to acquire it once. Note
627 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
628 * as there are other exclusive waiters still. If we
629 * fail, restart the loop.
631 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
632 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
633 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
634 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
635 sleepq_release(&sx->lock_object);
636 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
640 sleepq_release(&sx->lock_object);
645 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
646 * than loop back and retry.
648 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
649 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
650 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
651 sleepq_release(&sx->lock_object);
654 if (LOCK_LOG_TEST(&sx->lock_object, 0))
655 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
660 * Since we have been unable to acquire the exclusive
661 * lock and the exclusive waiters flag is set, we have
664 if (LOCK_LOG_TEST(&sx->lock_object, 0))
665 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
669 sleep_time -= lockstat_nsecs();
672 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
673 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
674 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
675 if (!(opts & SX_INTERRUPTIBLE))
676 sleepq_wait(&sx->lock_object, 0);
678 error = sleepq_wait_sig(&sx->lock_object, 0);
680 sleep_time += lockstat_nsecs();
684 if (LOCK_LOG_TEST(&sx->lock_object, 0))
686 "%s: interruptible sleep by %p suspended by signal",
690 if (LOCK_LOG_TEST(&sx->lock_object, 0))
691 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
697 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
698 contested, waittime, file, line);
701 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
702 if (spin_cnt > sleep_cnt)
703 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
709 * This function represents the so-called 'hard case' for sx_xunlock
710 * operation. All 'easy case' failures are redirected to this. Note
711 * that ideally this would be a static function, but it needs to be
712 * accessible from at least sx.h.
715 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
718 int queue, wakeup_swapper;
720 if (SCHEDULER_STOPPED())
723 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
725 /* If the lock is recursed, then unrecurse one level. */
726 if (sx_xlocked(sx) && sx_recursed(sx)) {
727 if ((--sx->sx_recurse) == 0)
728 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
729 if (LOCK_LOG_TEST(&sx->lock_object, 0))
730 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
733 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
734 SX_LOCK_EXCLUSIVE_WAITERS));
735 if (LOCK_LOG_TEST(&sx->lock_object, 0))
736 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
738 sleepq_lock(&sx->lock_object);
739 x = SX_LOCK_UNLOCKED;
742 * The wake up algorithm here is quite simple and probably not
743 * ideal. It gives precedence to shared waiters if they are
744 * present. For this condition, we have to preserve the
745 * state of the exclusive waiters flag.
746 * If interruptible sleeps left the shared queue empty avoid a
747 * starvation for the threads sleeping on the exclusive queue by giving
748 * them precedence and cleaning up the shared waiters bit anyway.
750 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
751 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
752 queue = SQ_SHARED_QUEUE;
753 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
755 queue = SQ_EXCLUSIVE_QUEUE;
757 /* Wake up all the waiters for the specific queue. */
758 if (LOCK_LOG_TEST(&sx->lock_object, 0))
759 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
760 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
762 atomic_store_rel_ptr(&sx->sx_lock, x);
763 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
765 sleepq_release(&sx->lock_object);
771 * This function represents the so-called 'hard case' for sx_slock
772 * operation. All 'easy case' failures are redirected to this. Note
773 * that ideally this would be a static function, but it needs to be
774 * accessible from at least sx.h.
777 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
781 volatile struct thread *owner;
783 #ifdef LOCK_PROFILING
784 uint64_t waittime = 0;
790 uint64_t spin_cnt = 0;
791 uint64_t sleep_cnt = 0;
792 int64_t sleep_time = 0;
795 if (SCHEDULER_STOPPED())
799 * As with rwlocks, we don't make any attempt to try to block
800 * shared locks once there is an exclusive waiter.
809 * If no other thread has an exclusive lock then try to bump up
810 * the count of sharers. Since we have to preserve the state
811 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
812 * shared lock loop back and retry.
814 if (x & SX_LOCK_SHARED) {
815 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
816 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
817 x + SX_ONE_SHARER)) {
818 if (LOCK_LOG_TEST(&sx->lock_object, 0))
820 "%s: %p succeed %p -> %p", __func__,
822 (void *)(x + SX_ONE_SHARER));
828 PMC_SOFT_CALL( , , lock, failed);
830 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
835 * If the owner is running on another CPU, spin until
836 * the owner stops running or the state of the lock
839 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
841 owner = (struct thread *)x;
842 if (TD_IS_RUNNING(owner)) {
843 if (LOCK_LOG_TEST(&sx->lock_object, 0))
845 "%s: spinning on %p held by %p",
846 __func__, sx, owner);
848 while (SX_OWNER(sx->sx_lock) == x &&
849 TD_IS_RUNNING(owner)) {
861 * Some other thread already has an exclusive lock, so
862 * start the process of blocking.
864 sleepq_lock(&sx->lock_object);
868 * The lock could have been released while we spun.
869 * In this case loop back and retry.
871 if (x & SX_LOCK_SHARED) {
872 sleepq_release(&sx->lock_object);
878 * If the owner is running on another CPU, spin until
879 * the owner stops running or the state of the lock
882 if (!(x & SX_LOCK_SHARED) &&
883 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
884 owner = (struct thread *)SX_OWNER(x);
885 if (TD_IS_RUNNING(owner)) {
886 sleepq_release(&sx->lock_object);
893 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
894 * fail to set it drop the sleep queue lock and loop
897 if (!(x & SX_LOCK_SHARED_WAITERS)) {
898 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
899 x | SX_LOCK_SHARED_WAITERS)) {
900 sleepq_release(&sx->lock_object);
903 if (LOCK_LOG_TEST(&sx->lock_object, 0))
904 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
909 * Since we have been unable to acquire the shared lock,
912 if (LOCK_LOG_TEST(&sx->lock_object, 0))
913 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
917 sleep_time -= lockstat_nsecs();
920 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
921 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
922 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
923 if (!(opts & SX_INTERRUPTIBLE))
924 sleepq_wait(&sx->lock_object, 0);
926 error = sleepq_wait_sig(&sx->lock_object, 0);
928 sleep_time += lockstat_nsecs();
932 if (LOCK_LOG_TEST(&sx->lock_object, 0))
934 "%s: interruptible sleep by %p suspended by signal",
938 if (LOCK_LOG_TEST(&sx->lock_object, 0))
939 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
943 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
944 contested, waittime, file, line);
947 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
948 if (spin_cnt > sleep_cnt)
949 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
956 * This function represents the so-called 'hard case' for sx_sunlock
957 * operation. All 'easy case' failures are redirected to this. Note
958 * that ideally this would be a static function, but it needs to be
959 * accessible from at least sx.h.
962 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
967 if (SCHEDULER_STOPPED())
974 * We should never have sharers while at least one thread
975 * holds a shared lock.
977 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
978 ("%s: waiting sharers", __func__));
981 * See if there is more than one shared lock held. If
982 * so, just drop one and return.
984 if (SX_SHARERS(x) > 1) {
985 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
986 x - SX_ONE_SHARER)) {
987 if (LOCK_LOG_TEST(&sx->lock_object, 0))
989 "%s: %p succeeded %p -> %p",
990 __func__, sx, (void *)x,
991 (void *)(x - SX_ONE_SHARER));
998 * If there aren't any waiters for an exclusive lock,
999 * then try to drop it quickly.
1001 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
1002 MPASS(x == SX_SHARERS_LOCK(1));
1003 if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1004 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
1005 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1006 CTR2(KTR_LOCK, "%s: %p last succeeded",
1014 * At this point, there should just be one sharer with
1015 * exclusive waiters.
1017 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
1019 sleepq_lock(&sx->lock_object);
1022 * Wake up semantic here is quite simple:
1023 * Just wake up all the exclusive waiters.
1024 * Note that the state of the lock could have changed,
1025 * so if it fails loop back and retry.
1027 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
1028 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
1029 SX_LOCK_UNLOCKED)) {
1030 sleepq_release(&sx->lock_object);
1033 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1034 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
1035 "exclusive queue", __func__, sx);
1036 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1037 0, SQ_EXCLUSIVE_QUEUE);
1038 sleepq_release(&sx->lock_object);
1045 #ifdef INVARIANT_SUPPORT
1051 * In the non-WITNESS case, sx_assert() can only detect that at least
1052 * *some* thread owns an slock, but it cannot guarantee that *this*
1053 * thread owns an slock.
1056 _sx_assert(const struct sx *sx, int what, const char *file, int line)
1062 if (panicstr != NULL)
1066 case SA_SLOCKED | SA_NOTRECURSED:
1067 case SA_SLOCKED | SA_RECURSED:
1073 case SA_LOCKED | SA_NOTRECURSED:
1074 case SA_LOCKED | SA_RECURSED:
1076 witness_assert(&sx->lock_object, what, file, line);
1079 * If some other thread has an exclusive lock or we
1080 * have one and are asserting a shared lock, fail.
1081 * Also, if no one has a lock at all, fail.
1083 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1084 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1085 sx_xholder(sx) != curthread)))
1086 panic("Lock %s not %slocked @ %s:%d\n",
1087 sx->lock_object.lo_name, slocked ? "share " : "",
1090 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1091 if (sx_recursed(sx)) {
1092 if (what & SA_NOTRECURSED)
1093 panic("Lock %s recursed @ %s:%d\n",
1094 sx->lock_object.lo_name, file,
1096 } else if (what & SA_RECURSED)
1097 panic("Lock %s not recursed @ %s:%d\n",
1098 sx->lock_object.lo_name, file, line);
1103 case SA_XLOCKED | SA_NOTRECURSED:
1104 case SA_XLOCKED | SA_RECURSED:
1105 if (sx_xholder(sx) != curthread)
1106 panic("Lock %s not exclusively locked @ %s:%d\n",
1107 sx->lock_object.lo_name, file, line);
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, line);
1112 } else if (what & SA_RECURSED)
1113 panic("Lock %s not recursed @ %s:%d\n",
1114 sx->lock_object.lo_name, file, line);
1118 witness_assert(&sx->lock_object, what, file, line);
1121 * If we hold an exclusve lock fail. We can't
1122 * reliably check to see if we hold a shared lock or
1125 if (sx_xholder(sx) == curthread)
1126 panic("Lock %s exclusively locked @ %s:%d\n",
1127 sx->lock_object.lo_name, file, line);
1131 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1135 #endif /* INVARIANT_SUPPORT */
1139 db_show_sx(const struct lock_object *lock)
1142 const struct sx *sx;
1144 sx = (const struct sx *)lock;
1146 db_printf(" state: ");
1147 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1148 db_printf("UNLOCKED\n");
1149 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1150 db_printf("DESTROYED\n");
1152 } else if (sx->sx_lock & SX_LOCK_SHARED)
1153 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1155 td = sx_xholder(sx);
1156 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1157 td->td_tid, td->td_proc->p_pid, td->td_name);
1158 if (sx_recursed(sx))
1159 db_printf(" recursed: %d\n", sx->sx_recurse);
1162 db_printf(" waiters: ");
1163 switch(sx->sx_lock &
1164 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1165 case SX_LOCK_SHARED_WAITERS:
1166 db_printf("shared\n");
1168 case SX_LOCK_EXCLUSIVE_WAITERS:
1169 db_printf("exclusive\n");
1171 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1172 db_printf("exclusive and shared\n");
1175 db_printf("none\n");
1180 * Check to see if a thread that is blocked on a sleep queue is actually
1181 * blocked on an sx lock. If so, output some details and return true.
1182 * If the lock has an exclusive owner, return that in *ownerp.
1185 sx_chain(struct thread *td, struct thread **ownerp)
1190 * Check to see if this thread is blocked on an sx lock.
1191 * First, we check the lock class. If that is ok, then we
1192 * compare the lock name against the wait message.
1195 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1196 sx->lock_object.lo_name != td->td_wmesg)
1199 /* We think we have an sx lock, so output some details. */
1200 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1201 *ownerp = sx_xholder(sx);
1202 if (sx->sx_lock & SX_LOCK_SHARED)
1203 db_printf("SLOCK (count %ju)\n",
1204 (uintmax_t)SX_SHARERS(sx->sx_lock));
1206 db_printf("XLOCK\n");