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_kdtrace.h"
42 #include "opt_no_adaptive_sx.h"
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #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 #define ASX_RETRIES 10
82 #define ASX_LOOPS 10000
86 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
87 * drop Giant anytime we have to sleep or if we adaptively spin.
89 #define GIANT_DECLARE \
91 WITNESS_SAVE_DECL(Giant) \
93 #define GIANT_SAVE() do { \
94 if (mtx_owned(&Giant)) { \
95 WITNESS_SAVE(&Giant.lock_object, Giant); \
96 while (mtx_owned(&Giant)) { \
103 #define GIANT_RESTORE() do { \
104 if (_giantcnt > 0) { \
105 mtx_assert(&Giant, MA_NOTOWNED); \
106 while (_giantcnt--) \
108 WITNESS_RESTORE(&Giant.lock_object, Giant); \
113 * Returns true if an exclusive lock is recursed. It assumes
114 * curthread currently has an exclusive lock.
116 #define sx_recurse lock_object.lo_data
117 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
119 static void assert_sx(struct lock_object *lock, int what);
121 static void db_show_sx(struct lock_object *lock);
123 static void lock_sx(struct lock_object *lock, int how);
125 static int owner_sx(struct lock_object *lock, struct thread **owner);
127 static int unlock_sx(struct lock_object *lock);
129 struct lock_class lock_class_sx = {
131 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
132 .lc_assert = assert_sx,
134 .lc_ddb_show = db_show_sx,
137 .lc_unlock = unlock_sx,
139 .lc_owner = owner_sx,
144 #define _sx_assert(sx, what, file, line)
148 assert_sx(struct lock_object *lock, int what)
151 sx_assert((struct sx *)lock, what);
155 lock_sx(struct lock_object *lock, int how)
159 sx = (struct sx *)lock;
167 unlock_sx(struct lock_object *lock)
171 sx = (struct sx *)lock;
172 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
173 if (sx_xlocked(sx)) {
184 owner_sx(struct lock_object *lock, struct thread **owner)
186 struct sx *sx = (struct sx *)lock;
187 uintptr_t x = sx->sx_lock;
189 *owner = (struct thread *)SX_OWNER(x);
190 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
196 sx_sysinit(void *arg)
198 struct sx_args *sargs = arg;
200 sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
204 sx_init_flags(struct sx *sx, const char *description, int opts)
208 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
209 SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
210 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
211 ("%s: sx_lock not aligned for %s: %p", __func__, description,
214 flags = LO_SLEEPABLE | LO_UPGRADABLE;
217 if (opts & SX_NOPROFILE)
218 flags |= LO_NOPROFILE;
219 if (!(opts & SX_NOWITNESS))
221 if (opts & SX_RECURSE)
222 flags |= LO_RECURSABLE;
226 flags |= opts & SX_NOADAPTIVE;
227 sx->sx_lock = SX_LOCK_UNLOCKED;
229 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
233 sx_destroy(struct sx *sx)
236 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
237 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
238 sx->sx_lock = SX_LOCK_DESTROYED;
239 lock_destroy(&sx->lock_object);
243 _sx_slock(struct sx *sx, int opts, const char *file, int line)
247 if (SCHEDULER_STOPPED())
249 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
250 ("sx_slock() of destroyed sx @ %s:%d", file, line));
251 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
252 error = __sx_slock(sx, opts, file, line);
254 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
255 WITNESS_LOCK(&sx->lock_object, 0, file, line);
256 curthread->td_locks++;
263 _sx_try_slock(struct sx *sx, const char *file, int line)
267 if (SCHEDULER_STOPPED())
272 KASSERT(x != SX_LOCK_DESTROYED,
273 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
274 if (!(x & SX_LOCK_SHARED))
276 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
277 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
278 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
279 curthread->td_locks++;
284 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
289 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
293 if (SCHEDULER_STOPPED())
295 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
296 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
297 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
299 error = __sx_xlock(sx, curthread, opts, file, line);
301 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
303 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
304 curthread->td_locks++;
311 _sx_try_xlock(struct sx *sx, const char *file, int line)
315 if (SCHEDULER_STOPPED())
318 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
319 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
321 if (sx_xlocked(sx) &&
322 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
324 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
327 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
328 (uintptr_t)curthread);
329 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
331 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
333 curthread->td_locks++;
340 _sx_sunlock(struct sx *sx, const char *file, int line)
343 if (SCHEDULER_STOPPED())
345 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
346 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
347 _sx_assert(sx, SA_SLOCKED, file, line);
348 curthread->td_locks--;
349 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
350 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
351 __sx_sunlock(sx, file, line);
352 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
356 _sx_xunlock(struct sx *sx, const char *file, int line)
359 if (SCHEDULER_STOPPED())
361 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
362 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
363 _sx_assert(sx, SA_XLOCKED, file, line);
364 curthread->td_locks--;
365 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
366 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
368 if (!sx_recursed(sx))
369 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
370 __sx_xunlock(sx, curthread, file, line);
374 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
375 * This will only succeed if this thread holds a single shared lock.
376 * Return 1 if if the upgrade succeed, 0 otherwise.
379 _sx_try_upgrade(struct sx *sx, const char *file, int line)
384 if (SCHEDULER_STOPPED())
387 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
388 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
389 _sx_assert(sx, SA_SLOCKED, file, line);
392 * Try to switch from one shared lock to an exclusive lock. We need
393 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
394 * we will wake up the exclusive waiters when we drop the lock.
396 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
397 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
398 (uintptr_t)curthread | x);
399 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
401 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
403 LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
409 * Downgrade an unrecursed exclusive lock into a single shared lock.
412 _sx_downgrade(struct sx *sx, const char *file, int line)
417 if (SCHEDULER_STOPPED())
420 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
421 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
422 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
425 panic("downgrade of a recursed lock");
428 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
431 * Try to switch from an exclusive lock with no shared waiters
432 * to one sharer with no shared waiters. If there are
433 * exclusive waiters, we don't need to lock the sleep queue so
434 * long as we preserve the flag. We do one quick try and if
435 * that fails we grab the sleepq lock to keep the flags from
436 * changing and do it the slow way.
438 * We have to lock the sleep queue if there are shared waiters
439 * so we can wake them up.
442 if (!(x & SX_LOCK_SHARED_WAITERS) &&
443 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
444 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
445 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
450 * Lock the sleep queue so we can read the waiters bits
451 * without any races and wakeup any shared waiters.
453 sleepq_lock(&sx->lock_object);
456 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
457 * shared lock. If there are any shared waiters, wake them up.
461 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
462 (x & SX_LOCK_EXCLUSIVE_WAITERS));
463 if (x & SX_LOCK_SHARED_WAITERS)
464 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
466 sleepq_release(&sx->lock_object);
468 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
469 LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
476 * This function represents the so-called 'hard case' for sx_xlock
477 * operation. All 'easy case' failures are redirected to this. Note
478 * that ideally this would be a static function, but it needs to be
479 * accessible from at least sx.h.
482 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
487 volatile struct thread *owner;
488 u_int i, spintries = 0;
491 #ifdef LOCK_PROFILING
492 uint64_t waittime = 0;
497 uint64_t spin_cnt = 0;
498 uint64_t sleep_cnt = 0;
499 int64_t sleep_time = 0;
502 if (SCHEDULER_STOPPED())
505 /* If we already hold an exclusive lock, then recurse. */
506 if (sx_xlocked(sx)) {
507 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
508 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
509 sx->lock_object.lo_name, file, line));
511 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
512 if (LOCK_LOG_TEST(&sx->lock_object, 0))
513 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
517 if (LOCK_LOG_TEST(&sx->lock_object, 0))
518 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
519 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
521 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
526 PMC_SOFT_CALL( , , lock, failed);
528 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
532 * If the lock is write locked and the owner is
533 * running on another CPU, spin until the owner stops
534 * running or the state of the lock changes.
537 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
538 if ((x & SX_LOCK_SHARED) == 0) {
540 owner = (struct thread *)x;
541 if (TD_IS_RUNNING(owner)) {
542 if (LOCK_LOG_TEST(&sx->lock_object, 0))
544 "%s: spinning on %p held by %p",
545 __func__, sx, owner);
547 while (SX_OWNER(sx->sx_lock) == x &&
548 TD_IS_RUNNING(owner)) {
556 } else if (SX_SHARERS(x) && spintries < ASX_RETRIES) {
559 for (i = 0; i < ASX_LOOPS; i++) {
560 if (LOCK_LOG_TEST(&sx->lock_object, 0))
562 "%s: shared spinning on %p with %u and %u",
563 __func__, sx, spintries, i);
565 if ((x & SX_LOCK_SHARED) == 0 ||
579 sleepq_lock(&sx->lock_object);
583 * If the lock was released while spinning on the
584 * sleep queue chain lock, try again.
586 if (x == SX_LOCK_UNLOCKED) {
587 sleepq_release(&sx->lock_object);
593 * The current lock owner might have started executing
594 * on another CPU (or the lock could have changed
595 * owners) while we were waiting on the sleep queue
596 * chain lock. If so, drop the sleep queue lock and try
599 if (!(x & SX_LOCK_SHARED) &&
600 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
601 owner = (struct thread *)SX_OWNER(x);
602 if (TD_IS_RUNNING(owner)) {
603 sleepq_release(&sx->lock_object);
610 * If an exclusive lock was released with both shared
611 * and exclusive waiters and a shared waiter hasn't
612 * woken up and acquired the lock yet, sx_lock will be
613 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
614 * If we see that value, try to acquire it once. Note
615 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
616 * as there are other exclusive waiters still. If we
617 * fail, restart the loop.
619 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
620 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
621 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
622 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
623 sleepq_release(&sx->lock_object);
624 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
628 sleepq_release(&sx->lock_object);
633 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
634 * than loop back and retry.
636 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
637 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
638 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
639 sleepq_release(&sx->lock_object);
642 if (LOCK_LOG_TEST(&sx->lock_object, 0))
643 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
648 * Since we have been unable to acquire the exclusive
649 * lock and the exclusive waiters flag is set, we have
652 if (LOCK_LOG_TEST(&sx->lock_object, 0))
653 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
657 sleep_time -= lockstat_nsecs();
660 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
661 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
662 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
663 if (!(opts & SX_INTERRUPTIBLE))
664 sleepq_wait(&sx->lock_object, 0);
666 error = sleepq_wait_sig(&sx->lock_object, 0);
668 sleep_time += lockstat_nsecs();
672 if (LOCK_LOG_TEST(&sx->lock_object, 0))
674 "%s: interruptible sleep by %p suspended by signal",
678 if (LOCK_LOG_TEST(&sx->lock_object, 0))
679 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
685 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
686 contested, waittime, file, line);
689 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
690 if (spin_cnt > sleep_cnt)
691 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
697 * This function represents the so-called 'hard case' for sx_xunlock
698 * operation. All 'easy case' failures are redirected to this. Note
699 * that ideally this would be a static function, but it needs to be
700 * accessible from at least sx.h.
703 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
706 int queue, wakeup_swapper;
708 if (SCHEDULER_STOPPED())
711 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
713 /* If the lock is recursed, then unrecurse one level. */
714 if (sx_xlocked(sx) && sx_recursed(sx)) {
715 if ((--sx->sx_recurse) == 0)
716 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
717 if (LOCK_LOG_TEST(&sx->lock_object, 0))
718 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
721 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
722 SX_LOCK_EXCLUSIVE_WAITERS));
723 if (LOCK_LOG_TEST(&sx->lock_object, 0))
724 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
726 sleepq_lock(&sx->lock_object);
727 x = SX_LOCK_UNLOCKED;
730 * The wake up algorithm here is quite simple and probably not
731 * ideal. It gives precedence to shared waiters if they are
732 * present. For this condition, we have to preserve the
733 * state of the exclusive waiters flag.
734 * If interruptible sleeps left the shared queue empty avoid a
735 * starvation for the threads sleeping on the exclusive queue by giving
736 * them precedence and cleaning up the shared waiters bit anyway.
738 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
739 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
740 queue = SQ_SHARED_QUEUE;
741 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
743 queue = SQ_EXCLUSIVE_QUEUE;
745 /* Wake up all the waiters for the specific queue. */
746 if (LOCK_LOG_TEST(&sx->lock_object, 0))
747 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
748 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
750 atomic_store_rel_ptr(&sx->sx_lock, x);
751 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
753 sleepq_release(&sx->lock_object);
759 * This function represents the so-called 'hard case' for sx_slock
760 * operation. All 'easy case' failures are redirected to this. Note
761 * that ideally this would be a static function, but it needs to be
762 * accessible from at least sx.h.
765 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
769 volatile struct thread *owner;
771 #ifdef LOCK_PROFILING
772 uint64_t waittime = 0;
778 uint64_t spin_cnt = 0;
779 uint64_t sleep_cnt = 0;
780 int64_t sleep_time = 0;
783 if (SCHEDULER_STOPPED())
787 * As with rwlocks, we don't make any attempt to try to block
788 * shared locks once there is an exclusive waiter.
797 * If no other thread has an exclusive lock then try to bump up
798 * the count of sharers. Since we have to preserve the state
799 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
800 * shared lock loop back and retry.
802 if (x & SX_LOCK_SHARED) {
803 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
804 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
805 x + SX_ONE_SHARER)) {
806 if (LOCK_LOG_TEST(&sx->lock_object, 0))
808 "%s: %p succeed %p -> %p", __func__,
810 (void *)(x + SX_ONE_SHARER));
816 PMC_SOFT_CALL( , , lock, failed);
818 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
823 * If the owner is running on another CPU, spin until
824 * the owner stops running or the state of the lock
827 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
829 owner = (struct thread *)x;
830 if (TD_IS_RUNNING(owner)) {
831 if (LOCK_LOG_TEST(&sx->lock_object, 0))
833 "%s: spinning on %p held by %p",
834 __func__, sx, owner);
836 while (SX_OWNER(sx->sx_lock) == x &&
837 TD_IS_RUNNING(owner)) {
849 * Some other thread already has an exclusive lock, so
850 * start the process of blocking.
852 sleepq_lock(&sx->lock_object);
856 * The lock could have been released while we spun.
857 * In this case loop back and retry.
859 if (x & SX_LOCK_SHARED) {
860 sleepq_release(&sx->lock_object);
866 * If the owner is running on another CPU, spin until
867 * the owner stops running or the state of the lock
870 if (!(x & SX_LOCK_SHARED) &&
871 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
872 owner = (struct thread *)SX_OWNER(x);
873 if (TD_IS_RUNNING(owner)) {
874 sleepq_release(&sx->lock_object);
881 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
882 * fail to set it drop the sleep queue lock and loop
885 if (!(x & SX_LOCK_SHARED_WAITERS)) {
886 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
887 x | SX_LOCK_SHARED_WAITERS)) {
888 sleepq_release(&sx->lock_object);
891 if (LOCK_LOG_TEST(&sx->lock_object, 0))
892 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
897 * Since we have been unable to acquire the shared lock,
900 if (LOCK_LOG_TEST(&sx->lock_object, 0))
901 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
905 sleep_time -= lockstat_nsecs();
908 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
909 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
910 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
911 if (!(opts & SX_INTERRUPTIBLE))
912 sleepq_wait(&sx->lock_object, 0);
914 error = sleepq_wait_sig(&sx->lock_object, 0);
916 sleep_time += lockstat_nsecs();
920 if (LOCK_LOG_TEST(&sx->lock_object, 0))
922 "%s: interruptible sleep by %p suspended by signal",
926 if (LOCK_LOG_TEST(&sx->lock_object, 0))
927 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
931 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
932 contested, waittime, file, line);
935 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
936 if (spin_cnt > sleep_cnt)
937 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
944 * This function represents the so-called 'hard case' for sx_sunlock
945 * operation. All 'easy case' failures are redirected to this. Note
946 * that ideally this would be a static function, but it needs to be
947 * accessible from at least sx.h.
950 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
955 if (SCHEDULER_STOPPED())
962 * We should never have sharers while at least one thread
963 * holds a shared lock.
965 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
966 ("%s: waiting sharers", __func__));
969 * See if there is more than one shared lock held. If
970 * so, just drop one and return.
972 if (SX_SHARERS(x) > 1) {
973 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
974 x - SX_ONE_SHARER)) {
975 if (LOCK_LOG_TEST(&sx->lock_object, 0))
977 "%s: %p succeeded %p -> %p",
978 __func__, sx, (void *)x,
979 (void *)(x - SX_ONE_SHARER));
986 * If there aren't any waiters for an exclusive lock,
987 * then try to drop it quickly.
989 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
990 MPASS(x == SX_SHARERS_LOCK(1));
991 if (atomic_cmpset_rel_ptr(&sx->sx_lock,
992 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
993 if (LOCK_LOG_TEST(&sx->lock_object, 0))
994 CTR2(KTR_LOCK, "%s: %p last succeeded",
1002 * At this point, there should just be one sharer with
1003 * exclusive waiters.
1005 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
1007 sleepq_lock(&sx->lock_object);
1010 * Wake up semantic here is quite simple:
1011 * Just wake up all the exclusive waiters.
1012 * Note that the state of the lock could have changed,
1013 * so if it fails loop back and retry.
1015 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
1016 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
1017 SX_LOCK_UNLOCKED)) {
1018 sleepq_release(&sx->lock_object);
1021 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1022 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
1023 "exclusive queue", __func__, sx);
1024 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1025 0, SQ_EXCLUSIVE_QUEUE);
1026 sleepq_release(&sx->lock_object);
1033 #ifdef INVARIANT_SUPPORT
1039 * In the non-WITNESS case, sx_assert() can only detect that at least
1040 * *some* thread owns an slock, but it cannot guarantee that *this*
1041 * thread owns an slock.
1044 _sx_assert(struct sx *sx, int what, const char *file, int line)
1050 if (panicstr != NULL)
1054 case SA_SLOCKED | SA_NOTRECURSED:
1055 case SA_SLOCKED | SA_RECURSED:
1061 case SA_LOCKED | SA_NOTRECURSED:
1062 case SA_LOCKED | SA_RECURSED:
1064 witness_assert(&sx->lock_object, what, file, line);
1067 * If some other thread has an exclusive lock or we
1068 * have one and are asserting a shared lock, fail.
1069 * Also, if no one has a lock at all, fail.
1071 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1072 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1073 sx_xholder(sx) != curthread)))
1074 panic("Lock %s not %slocked @ %s:%d\n",
1075 sx->lock_object.lo_name, slocked ? "share " : "",
1078 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1079 if (sx_recursed(sx)) {
1080 if (what & SA_NOTRECURSED)
1081 panic("Lock %s recursed @ %s:%d\n",
1082 sx->lock_object.lo_name, file,
1084 } else if (what & SA_RECURSED)
1085 panic("Lock %s not recursed @ %s:%d\n",
1086 sx->lock_object.lo_name, file, line);
1091 case SA_XLOCKED | SA_NOTRECURSED:
1092 case SA_XLOCKED | SA_RECURSED:
1093 if (sx_xholder(sx) != curthread)
1094 panic("Lock %s not exclusively locked @ %s:%d\n",
1095 sx->lock_object.lo_name, file, line);
1096 if (sx_recursed(sx)) {
1097 if (what & SA_NOTRECURSED)
1098 panic("Lock %s recursed @ %s:%d\n",
1099 sx->lock_object.lo_name, file, line);
1100 } else if (what & SA_RECURSED)
1101 panic("Lock %s not recursed @ %s:%d\n",
1102 sx->lock_object.lo_name, file, line);
1106 witness_assert(&sx->lock_object, what, file, line);
1109 * If we hold an exclusve lock fail. We can't
1110 * reliably check to see if we hold a shared lock or
1113 if (sx_xholder(sx) == curthread)
1114 panic("Lock %s exclusively locked @ %s:%d\n",
1115 sx->lock_object.lo_name, file, line);
1119 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1123 #endif /* INVARIANT_SUPPORT */
1127 db_show_sx(struct lock_object *lock)
1132 sx = (struct sx *)lock;
1134 db_printf(" state: ");
1135 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1136 db_printf("UNLOCKED\n");
1137 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1138 db_printf("DESTROYED\n");
1140 } else if (sx->sx_lock & SX_LOCK_SHARED)
1141 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1143 td = sx_xholder(sx);
1144 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1145 td->td_tid, td->td_proc->p_pid, td->td_name);
1146 if (sx_recursed(sx))
1147 db_printf(" recursed: %d\n", sx->sx_recurse);
1150 db_printf(" waiters: ");
1151 switch(sx->sx_lock &
1152 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1153 case SX_LOCK_SHARED_WAITERS:
1154 db_printf("shared\n");
1156 case SX_LOCK_EXCLUSIVE_WAITERS:
1157 db_printf("exclusive\n");
1159 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1160 db_printf("exclusive and shared\n");
1163 db_printf("none\n");
1168 * Check to see if a thread that is blocked on a sleep queue is actually
1169 * blocked on an sx lock. If so, output some details and return true.
1170 * If the lock has an exclusive owner, return that in *ownerp.
1173 sx_chain(struct thread *td, struct thread **ownerp)
1178 * Check to see if this thread is blocked on an sx lock.
1179 * First, we check the lock class. If that is ok, then we
1180 * compare the lock name against the wait message.
1183 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1184 sx->lock_object.lo_name != td->td_wmesg)
1187 /* We think we have an sx lock, so output some details. */
1188 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1189 *ownerp = sx_xholder(sx);
1190 if (sx->sx_lock & SX_LOCK_SHARED)
1191 db_printf("SLOCK (count %ju)\n",
1192 (uintmax_t)SX_SHARERS(sx->sx_lock));
1194 db_printf("XLOCK\n");