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_kdtrace.h"
41 #include "opt_no_adaptive_sx.h"
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
48 #include <sys/linker_set.h>
50 #include <sys/mutex.h>
52 #include <sys/sleepqueue.h>
54 #include <sys/sysctl.h>
55 #include <sys/systm.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);
71 /* Handy macros for sleep queues. */
72 #define SQ_EXCLUSIVE_QUEUE 0
73 #define SQ_SHARED_QUEUE 1
76 #define ASX_RETRIES 10
77 #define ASX_LOOPS 10000
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_recurse lock_object.lo_data
112 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
114 static void assert_sx(struct lock_object *lock, int what);
116 static void db_show_sx(struct lock_object *lock);
118 static void lock_sx(struct lock_object *lock, int how);
120 static int owner_sx(struct lock_object *lock, struct thread **owner);
122 static int unlock_sx(struct lock_object *lock);
124 struct lock_class lock_class_sx = {
126 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
127 .lc_assert = assert_sx,
129 .lc_ddb_show = db_show_sx,
132 .lc_unlock = unlock_sx,
134 .lc_owner = owner_sx,
139 #define _sx_assert(sx, what, file, line)
143 assert_sx(struct lock_object *lock, int what)
146 sx_assert((struct sx *)lock, what);
150 lock_sx(struct lock_object *lock, int how)
154 sx = (struct sx *)lock;
162 unlock_sx(struct lock_object *lock)
166 sx = (struct sx *)lock;
167 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
168 if (sx_xlocked(sx)) {
179 owner_sx(struct lock_object *lock, struct thread **owner)
181 struct sx *sx = (struct sx *)lock;
182 uintptr_t x = sx->sx_lock;
184 *owner = (struct thread *)SX_OWNER(x);
185 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
191 sx_sysinit(void *arg)
193 struct sx_args *sargs = arg;
195 sx_init(sargs->sa_sx, sargs->sa_desc);
199 sx_init_flags(struct sx *sx, const char *description, int opts)
203 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
204 SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
205 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
206 ("%s: sx_lock not aligned for %s: %p", __func__, description,
209 flags = LO_SLEEPABLE | LO_UPGRADABLE;
212 if (opts & SX_NOPROFILE)
213 flags |= LO_NOPROFILE;
214 if (!(opts & SX_NOWITNESS))
216 if (opts & SX_RECURSE)
217 flags |= LO_RECURSABLE;
221 flags |= opts & SX_NOADAPTIVE;
222 sx->sx_lock = SX_LOCK_UNLOCKED;
224 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
228 sx_destroy(struct sx *sx)
231 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
232 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
233 sx->sx_lock = SX_LOCK_DESTROYED;
234 lock_destroy(&sx->lock_object);
238 _sx_slock(struct sx *sx, int opts, const char *file, int line)
242 MPASS(curthread != NULL);
243 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
244 ("sx_slock() of destroyed sx @ %s:%d", file, line));
245 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
246 error = __sx_slock(sx, opts, file, line);
248 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
249 WITNESS_LOCK(&sx->lock_object, 0, file, line);
250 curthread->td_locks++;
257 _sx_try_slock(struct sx *sx, const char *file, int line)
263 KASSERT(x != SX_LOCK_DESTROYED,
264 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
265 if (!(x & SX_LOCK_SHARED))
267 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
268 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
269 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
270 curthread->td_locks++;
275 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
280 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
284 MPASS(curthread != NULL);
285 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
286 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
287 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
289 error = __sx_xlock(sx, curthread, opts, file, line);
291 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
293 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
294 curthread->td_locks++;
301 _sx_try_xlock(struct sx *sx, const char *file, int line)
305 MPASS(curthread != NULL);
306 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
307 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
309 if (sx_xlocked(sx) &&
310 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
312 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
315 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
316 (uintptr_t)curthread);
317 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
319 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
321 curthread->td_locks++;
328 _sx_sunlock(struct sx *sx, const char *file, int line)
331 MPASS(curthread != NULL);
332 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
333 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
334 _sx_assert(sx, SA_SLOCKED, file, line);
335 curthread->td_locks--;
336 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
337 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
338 __sx_sunlock(sx, file, line);
339 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
343 _sx_xunlock(struct sx *sx, const char *file, int line)
346 MPASS(curthread != NULL);
347 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
348 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
349 _sx_assert(sx, SA_XLOCKED, file, line);
350 curthread->td_locks--;
351 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
352 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
354 if (!sx_recursed(sx))
355 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
356 __sx_xunlock(sx, curthread, file, line);
360 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
361 * This will only succeed if this thread holds a single shared lock.
362 * Return 1 if if the upgrade succeed, 0 otherwise.
365 _sx_try_upgrade(struct sx *sx, const char *file, int line)
370 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
371 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
372 _sx_assert(sx, SA_SLOCKED, file, line);
375 * Try to switch from one shared lock to an exclusive lock. We need
376 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
377 * we will wake up the exclusive waiters when we drop the lock.
379 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
380 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
381 (uintptr_t)curthread | x);
382 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
384 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
386 LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
392 * Downgrade an unrecursed exclusive lock into a single shared lock.
395 _sx_downgrade(struct sx *sx, const char *file, int line)
400 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
401 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
402 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
405 panic("downgrade of a recursed lock");
408 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
411 * Try to switch from an exclusive lock with no shared waiters
412 * to one sharer with no shared waiters. If there are
413 * exclusive waiters, we don't need to lock the sleep queue so
414 * long as we preserve the flag. We do one quick try and if
415 * that fails we grab the sleepq lock to keep the flags from
416 * changing and do it the slow way.
418 * We have to lock the sleep queue if there are shared waiters
419 * so we can wake them up.
422 if (!(x & SX_LOCK_SHARED_WAITERS) &&
423 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
424 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
425 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
430 * Lock the sleep queue so we can read the waiters bits
431 * without any races and wakeup any shared waiters.
433 sleepq_lock(&sx->lock_object);
436 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
437 * shared lock. If there are any shared waiters, wake them up.
441 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
442 (x & SX_LOCK_EXCLUSIVE_WAITERS));
443 if (x & SX_LOCK_SHARED_WAITERS)
444 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
446 sleepq_release(&sx->lock_object);
448 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
449 LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
456 * This function represents the so-called 'hard case' for sx_xlock
457 * operation. All 'easy case' failures are redirected to this. Note
458 * that ideally this would be a static function, but it needs to be
459 * accessible from at least sx.h.
462 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
467 volatile struct thread *owner;
468 u_int i, spintries = 0;
471 #ifdef LOCK_PROFILING
472 uint64_t waittime = 0;
477 uint64_t spin_cnt = 0;
478 uint64_t sleep_cnt = 0;
479 int64_t sleep_time = 0;
482 /* If we already hold an exclusive lock, then recurse. */
483 if (sx_xlocked(sx)) {
484 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
485 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
486 sx->lock_object.lo_name, file, line));
488 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
489 if (LOCK_LOG_TEST(&sx->lock_object, 0))
490 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
494 if (LOCK_LOG_TEST(&sx->lock_object, 0))
495 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
496 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
498 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
502 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
506 * If the lock is write locked and the owner is
507 * running on another CPU, spin until the owner stops
508 * running or the state of the lock changes.
511 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
512 if ((x & SX_LOCK_SHARED) == 0) {
514 owner = (struct thread *)x;
515 if (TD_IS_RUNNING(owner)) {
516 if (LOCK_LOG_TEST(&sx->lock_object, 0))
518 "%s: spinning on %p held by %p",
519 __func__, sx, owner);
521 while (SX_OWNER(sx->sx_lock) == x &&
522 TD_IS_RUNNING(owner)) {
530 } else if (SX_SHARERS(x) && spintries < ASX_RETRIES) {
533 for (i = 0; i < ASX_LOOPS; i++) {
534 if (LOCK_LOG_TEST(&sx->lock_object, 0))
536 "%s: shared spinning on %p with %u and %u",
537 __func__, sx, spintries, i);
539 if ((x & SX_LOCK_SHARED) == 0 ||
553 sleepq_lock(&sx->lock_object);
557 * If the lock was released while spinning on the
558 * sleep queue chain lock, try again.
560 if (x == SX_LOCK_UNLOCKED) {
561 sleepq_release(&sx->lock_object);
567 * The current lock owner might have started executing
568 * on another CPU (or the lock could have changed
569 * owners) while we were waiting on the sleep queue
570 * chain lock. If so, drop the sleep queue lock and try
573 if (!(x & SX_LOCK_SHARED) &&
574 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
575 owner = (struct thread *)SX_OWNER(x);
576 if (TD_IS_RUNNING(owner)) {
577 sleepq_release(&sx->lock_object);
584 * If an exclusive lock was released with both shared
585 * and exclusive waiters and a shared waiter hasn't
586 * woken up and acquired the lock yet, sx_lock will be
587 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
588 * If we see that value, try to acquire it once. Note
589 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
590 * as there are other exclusive waiters still. If we
591 * fail, restart the loop.
593 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
594 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
595 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
596 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
597 sleepq_release(&sx->lock_object);
598 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
602 sleepq_release(&sx->lock_object);
607 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
608 * than loop back and retry.
610 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
611 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
612 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
613 sleepq_release(&sx->lock_object);
616 if (LOCK_LOG_TEST(&sx->lock_object, 0))
617 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
622 * Since we have been unable to acquire the exclusive
623 * lock and the exclusive waiters flag is set, we have
626 if (LOCK_LOG_TEST(&sx->lock_object, 0))
627 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
631 sleep_time -= lockstat_nsecs();
634 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
635 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
636 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
637 if (!(opts & SX_INTERRUPTIBLE))
638 sleepq_wait(&sx->lock_object, 0);
640 error = sleepq_wait_sig(&sx->lock_object, 0);
642 sleep_time += lockstat_nsecs();
646 if (LOCK_LOG_TEST(&sx->lock_object, 0))
648 "%s: interruptible sleep by %p suspended by signal",
652 if (LOCK_LOG_TEST(&sx->lock_object, 0))
653 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
659 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
660 contested, waittime, file, line);
663 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
664 if (spin_cnt > sleep_cnt)
665 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
671 * This function represents the so-called 'hard case' for sx_xunlock
672 * operation. All 'easy case' failures are redirected to this. Note
673 * that ideally this would be a static function, but it needs to be
674 * accessible from at least sx.h.
677 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
680 int queue, wakeup_swapper;
682 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
684 /* If the lock is recursed, then unrecurse one level. */
685 if (sx_xlocked(sx) && sx_recursed(sx)) {
686 if ((--sx->sx_recurse) == 0)
687 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
688 if (LOCK_LOG_TEST(&sx->lock_object, 0))
689 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
692 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
693 SX_LOCK_EXCLUSIVE_WAITERS));
694 if (LOCK_LOG_TEST(&sx->lock_object, 0))
695 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
697 sleepq_lock(&sx->lock_object);
698 x = SX_LOCK_UNLOCKED;
701 * The wake up algorithm here is quite simple and probably not
702 * ideal. It gives precedence to shared waiters if they are
703 * present. For this condition, we have to preserve the
704 * state of the exclusive waiters flag.
705 * If interruptible sleeps left the shared queue empty avoid a
706 * starvation for the threads sleeping on the exclusive queue by giving
707 * them precedence and cleaning up the shared waiters bit anyway.
709 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
710 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
711 queue = SQ_SHARED_QUEUE;
712 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
714 queue = SQ_EXCLUSIVE_QUEUE;
716 /* Wake up all the waiters for the specific queue. */
717 if (LOCK_LOG_TEST(&sx->lock_object, 0))
718 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
719 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
721 atomic_store_rel_ptr(&sx->sx_lock, x);
722 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
724 sleepq_release(&sx->lock_object);
730 * This function represents the so-called 'hard case' for sx_slock
731 * operation. All 'easy case' failures are redirected to this. Note
732 * that ideally this would be a static function, but it needs to be
733 * accessible from at least sx.h.
736 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
740 volatile struct thread *owner;
742 #ifdef LOCK_PROFILING
743 uint64_t waittime = 0;
749 uint64_t spin_cnt = 0;
750 uint64_t sleep_cnt = 0;
751 int64_t sleep_time = 0;
755 * As with rwlocks, we don't make any attempt to try to block
756 * shared locks once there is an exclusive waiter.
765 * If no other thread has an exclusive lock then try to bump up
766 * the count of sharers. Since we have to preserve the state
767 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
768 * shared lock loop back and retry.
770 if (x & SX_LOCK_SHARED) {
771 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
772 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
773 x + SX_ONE_SHARER)) {
774 if (LOCK_LOG_TEST(&sx->lock_object, 0))
776 "%s: %p succeed %p -> %p", __func__,
778 (void *)(x + SX_ONE_SHARER));
783 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
788 * If the owner is running on another CPU, spin until
789 * the owner stops running or the state of the lock
792 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
794 owner = (struct thread *)x;
795 if (TD_IS_RUNNING(owner)) {
796 if (LOCK_LOG_TEST(&sx->lock_object, 0))
798 "%s: spinning on %p held by %p",
799 __func__, sx, owner);
801 while (SX_OWNER(sx->sx_lock) == x &&
802 TD_IS_RUNNING(owner)) {
814 * Some other thread already has an exclusive lock, so
815 * start the process of blocking.
817 sleepq_lock(&sx->lock_object);
821 * The lock could have been released while we spun.
822 * In this case loop back and retry.
824 if (x & SX_LOCK_SHARED) {
825 sleepq_release(&sx->lock_object);
831 * If the owner is running on another CPU, spin until
832 * the owner stops running or the state of the lock
835 if (!(x & SX_LOCK_SHARED) &&
836 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
837 owner = (struct thread *)SX_OWNER(x);
838 if (TD_IS_RUNNING(owner)) {
839 sleepq_release(&sx->lock_object);
846 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
847 * fail to set it drop the sleep queue lock and loop
850 if (!(x & SX_LOCK_SHARED_WAITERS)) {
851 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
852 x | SX_LOCK_SHARED_WAITERS)) {
853 sleepq_release(&sx->lock_object);
856 if (LOCK_LOG_TEST(&sx->lock_object, 0))
857 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
862 * Since we have been unable to acquire the shared lock,
865 if (LOCK_LOG_TEST(&sx->lock_object, 0))
866 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
870 sleep_time -= lockstat_nsecs();
873 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
874 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
875 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
876 if (!(opts & SX_INTERRUPTIBLE))
877 sleepq_wait(&sx->lock_object, 0);
879 error = sleepq_wait_sig(&sx->lock_object, 0);
881 sleep_time += lockstat_nsecs();
885 if (LOCK_LOG_TEST(&sx->lock_object, 0))
887 "%s: interruptible sleep by %p suspended by signal",
891 if (LOCK_LOG_TEST(&sx->lock_object, 0))
892 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
896 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
897 contested, waittime, file, line);
900 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
901 if (spin_cnt > sleep_cnt)
902 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
909 * This function represents the so-called 'hard case' for sx_sunlock
910 * operation. All 'easy case' failures are redirected to this. Note
911 * that ideally this would be a static function, but it needs to be
912 * accessible from at least sx.h.
915 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
924 * We should never have sharers while at least one thread
925 * holds a shared lock.
927 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
928 ("%s: waiting sharers", __func__));
931 * See if there is more than one shared lock held. If
932 * so, just drop one and return.
934 if (SX_SHARERS(x) > 1) {
935 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
936 x - SX_ONE_SHARER)) {
937 if (LOCK_LOG_TEST(&sx->lock_object, 0))
939 "%s: %p succeeded %p -> %p",
940 __func__, sx, (void *)x,
941 (void *)(x - SX_ONE_SHARER));
948 * If there aren't any waiters for an exclusive lock,
949 * then try to drop it quickly.
951 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
952 MPASS(x == SX_SHARERS_LOCK(1));
953 if (atomic_cmpset_rel_ptr(&sx->sx_lock,
954 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
955 if (LOCK_LOG_TEST(&sx->lock_object, 0))
956 CTR2(KTR_LOCK, "%s: %p last succeeded",
964 * At this point, there should just be one sharer with
967 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
969 sleepq_lock(&sx->lock_object);
972 * Wake up semantic here is quite simple:
973 * Just wake up all the exclusive waiters.
974 * Note that the state of the lock could have changed,
975 * so if it fails loop back and retry.
977 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
978 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
980 sleepq_release(&sx->lock_object);
983 if (LOCK_LOG_TEST(&sx->lock_object, 0))
984 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
985 "exclusive queue", __func__, sx);
986 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
987 0, SQ_EXCLUSIVE_QUEUE);
988 sleepq_release(&sx->lock_object);
995 #ifdef INVARIANT_SUPPORT
1001 * In the non-WITNESS case, sx_assert() can only detect that at least
1002 * *some* thread owns an slock, but it cannot guarantee that *this*
1003 * thread owns an slock.
1006 _sx_assert(struct sx *sx, int what, const char *file, int line)
1012 if (panicstr != NULL)
1016 case SA_SLOCKED | SA_NOTRECURSED:
1017 case SA_SLOCKED | SA_RECURSED:
1023 case SA_LOCKED | SA_NOTRECURSED:
1024 case SA_LOCKED | SA_RECURSED:
1026 witness_assert(&sx->lock_object, what, file, line);
1029 * If some other thread has an exclusive lock or we
1030 * have one and are asserting a shared lock, fail.
1031 * Also, if no one has a lock at all, fail.
1033 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1034 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1035 sx_xholder(sx) != curthread)))
1036 panic("Lock %s not %slocked @ %s:%d\n",
1037 sx->lock_object.lo_name, slocked ? "share " : "",
1040 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1041 if (sx_recursed(sx)) {
1042 if (what & SA_NOTRECURSED)
1043 panic("Lock %s recursed @ %s:%d\n",
1044 sx->lock_object.lo_name, file,
1046 } else if (what & SA_RECURSED)
1047 panic("Lock %s not recursed @ %s:%d\n",
1048 sx->lock_object.lo_name, file, line);
1053 case SA_XLOCKED | SA_NOTRECURSED:
1054 case SA_XLOCKED | SA_RECURSED:
1055 if (sx_xholder(sx) != curthread)
1056 panic("Lock %s not exclusively locked @ %s:%d\n",
1057 sx->lock_object.lo_name, file, line);
1058 if (sx_recursed(sx)) {
1059 if (what & SA_NOTRECURSED)
1060 panic("Lock %s recursed @ %s:%d\n",
1061 sx->lock_object.lo_name, file, line);
1062 } else if (what & SA_RECURSED)
1063 panic("Lock %s not recursed @ %s:%d\n",
1064 sx->lock_object.lo_name, file, line);
1068 witness_assert(&sx->lock_object, what, file, line);
1071 * If we hold an exclusve lock fail. We can't
1072 * reliably check to see if we hold a shared lock or
1075 if (sx_xholder(sx) == curthread)
1076 panic("Lock %s exclusively locked @ %s:%d\n",
1077 sx->lock_object.lo_name, file, line);
1081 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1085 #endif /* INVARIANT_SUPPORT */
1089 db_show_sx(struct lock_object *lock)
1094 sx = (struct sx *)lock;
1096 db_printf(" state: ");
1097 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1098 db_printf("UNLOCKED\n");
1099 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1100 db_printf("DESTROYED\n");
1102 } else if (sx->sx_lock & SX_LOCK_SHARED)
1103 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1105 td = sx_xholder(sx);
1106 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1107 td->td_tid, td->td_proc->p_pid, td->td_name);
1108 if (sx_recursed(sx))
1109 db_printf(" recursed: %d\n", sx->sx_recurse);
1112 db_printf(" waiters: ");
1113 switch(sx->sx_lock &
1114 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1115 case SX_LOCK_SHARED_WAITERS:
1116 db_printf("shared\n");
1118 case SX_LOCK_EXCLUSIVE_WAITERS:
1119 db_printf("exclusive\n");
1121 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1122 db_printf("exclusive and shared\n");
1125 db_printf("none\n");
1130 * Check to see if a thread that is blocked on a sleep queue is actually
1131 * blocked on an sx lock. If so, output some details and return true.
1132 * If the lock has an exclusive owner, return that in *ownerp.
1135 sx_chain(struct thread *td, struct thread **ownerp)
1140 * Check to see if this thread is blocked on an sx lock.
1141 * First, we check the lock class. If that is ok, then we
1142 * compare the lock name against the wait message.
1145 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1146 sx->lock_object.lo_name != td->td_wmesg)
1149 /* We think we have an sx lock, so output some details. */
1150 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1151 *ownerp = sx_xholder(sx);
1152 if (sx->sx_lock & SX_LOCK_SHARED)
1153 db_printf("SLOCK (count %ju)\n",
1154 (uintmax_t)SX_SHARERS(sx->sx_lock));
1156 db_printf("XLOCK\n");