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>
52 #include <sys/mutex.h>
54 #include <sys/sleepqueue.h>
56 #include <sys/sysctl.h>
58 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
59 #include <machine/cpu.h>
66 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
70 CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE);
73 #include <sys/pmckern.h>
74 PMC_SOFT_DECLARE( , , lock, failed);
77 /* Handy macros for sleep queues. */
78 #define SQ_EXCLUSIVE_QUEUE 0
79 #define SQ_SHARED_QUEUE 1
82 #define ASX_RETRIES 10
83 #define ASX_LOOPS 10000
87 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
88 * drop Giant anytime we have to sleep or if we adaptively spin.
90 #define GIANT_DECLARE \
92 WITNESS_SAVE_DECL(Giant) \
94 #define GIANT_SAVE() do { \
95 if (mtx_owned(&Giant)) { \
96 WITNESS_SAVE(&Giant.lock_object, Giant); \
97 while (mtx_owned(&Giant)) { \
104 #define GIANT_RESTORE() do { \
105 if (_giantcnt > 0) { \
106 mtx_assert(&Giant, MA_NOTOWNED); \
107 while (_giantcnt--) \
109 WITNESS_RESTORE(&Giant.lock_object, Giant); \
114 * Returns true if an exclusive lock is recursed. It assumes
115 * curthread currently has an exclusive lock.
117 #define sx_recurse lock_object.lo_data
118 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
120 static void assert_sx(struct lock_object *lock, int what);
122 static void db_show_sx(struct lock_object *lock);
124 static void lock_sx(struct lock_object *lock, int how);
126 static int owner_sx(struct lock_object *lock, struct thread **owner);
128 static int unlock_sx(struct lock_object *lock);
130 struct lock_class lock_class_sx = {
132 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
133 .lc_assert = assert_sx,
135 .lc_ddb_show = db_show_sx,
138 .lc_unlock = unlock_sx,
140 .lc_owner = owner_sx,
145 #define _sx_assert(sx, what, file, line)
149 assert_sx(struct lock_object *lock, int what)
152 sx_assert((struct sx *)lock, what);
156 lock_sx(struct lock_object *lock, int how)
160 sx = (struct sx *)lock;
168 unlock_sx(struct lock_object *lock)
172 sx = (struct sx *)lock;
173 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
174 if (sx_xlocked(sx)) {
185 owner_sx(struct lock_object *lock, struct thread **owner)
187 struct sx *sx = (struct sx *)lock;
188 uintptr_t x = sx->sx_lock;
190 *owner = (struct thread *)SX_OWNER(x);
191 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
197 sx_sysinit(void *arg)
199 struct sx_args *sargs = arg;
201 sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
205 sx_init_flags(struct sx *sx, const char *description, int opts)
209 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
210 SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
211 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
212 ("%s: sx_lock not aligned for %s: %p", __func__, description,
215 flags = LO_SLEEPABLE | LO_UPGRADABLE;
218 if (opts & SX_NOPROFILE)
219 flags |= LO_NOPROFILE;
220 if (!(opts & SX_NOWITNESS))
222 if (opts & SX_RECURSE)
223 flags |= LO_RECURSABLE;
227 flags |= opts & SX_NOADAPTIVE;
228 sx->sx_lock = SX_LOCK_UNLOCKED;
230 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
234 sx_destroy(struct sx *sx)
237 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
238 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
239 sx->sx_lock = SX_LOCK_DESTROYED;
240 lock_destroy(&sx->lock_object);
244 _sx_slock(struct sx *sx, int opts, const char *file, int line)
248 if (SCHEDULER_STOPPED())
250 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
251 ("sx_slock() by idle thread %p on sx %s @ %s:%d",
252 curthread, sx->lock_object.lo_name, file, line));
253 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
254 ("sx_slock() of destroyed sx @ %s:%d", file, line));
255 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
256 error = __sx_slock(sx, opts, file, line);
258 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
259 WITNESS_LOCK(&sx->lock_object, 0, file, line);
260 curthread->td_locks++;
267 _sx_try_slock(struct sx *sx, const char *file, int line)
271 if (SCHEDULER_STOPPED())
274 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
275 ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
276 curthread, sx->lock_object.lo_name, file, line));
280 KASSERT(x != SX_LOCK_DESTROYED,
281 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
282 if (!(x & SX_LOCK_SHARED))
284 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
285 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
286 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
287 curthread->td_locks++;
292 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
297 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
301 if (SCHEDULER_STOPPED())
303 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
304 ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
305 curthread, sx->lock_object.lo_name, file, line));
306 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
307 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
308 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
310 error = __sx_xlock(sx, curthread, opts, file, line);
312 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
314 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
315 curthread->td_locks++;
322 _sx_try_xlock(struct sx *sx, const char *file, int line)
326 if (SCHEDULER_STOPPED())
329 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
330 ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
331 curthread, sx->lock_object.lo_name, file, line));
332 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
333 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
335 if (sx_xlocked(sx) &&
336 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
338 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
341 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
342 (uintptr_t)curthread);
343 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
345 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
347 curthread->td_locks++;
354 _sx_sunlock(struct sx *sx, const char *file, int line)
357 if (SCHEDULER_STOPPED())
359 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
360 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
361 _sx_assert(sx, SA_SLOCKED, file, line);
362 curthread->td_locks--;
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 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
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 curthread->td_locks--;
379 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
380 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
382 if (!sx_recursed(sx))
383 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
384 __sx_xunlock(sx, curthread, file, line);
388 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
389 * This will only succeed if this thread holds a single shared lock.
390 * Return 1 if if the upgrade succeed, 0 otherwise.
393 _sx_try_upgrade(struct sx *sx, const char *file, int line)
398 if (SCHEDULER_STOPPED())
401 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
402 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
403 _sx_assert(sx, SA_SLOCKED, file, line);
406 * Try to switch from one shared lock to an exclusive lock. We need
407 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
408 * we will wake up the exclusive waiters when we drop the lock.
410 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
411 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
412 (uintptr_t)curthread | x);
413 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
415 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
417 LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
423 * Downgrade an unrecursed exclusive lock into a single shared lock.
426 _sx_downgrade(struct sx *sx, const char *file, int line)
431 if (SCHEDULER_STOPPED())
434 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
435 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
436 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
439 panic("downgrade of a recursed lock");
442 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
445 * Try to switch from an exclusive lock with no shared waiters
446 * to one sharer with no shared waiters. If there are
447 * exclusive waiters, we don't need to lock the sleep queue so
448 * long as we preserve the flag. We do one quick try and if
449 * that fails we grab the sleepq lock to keep the flags from
450 * changing and do it the slow way.
452 * We have to lock the sleep queue if there are shared waiters
453 * so we can wake them up.
456 if (!(x & SX_LOCK_SHARED_WAITERS) &&
457 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
458 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
459 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
464 * Lock the sleep queue so we can read the waiters bits
465 * without any races and wakeup any shared waiters.
467 sleepq_lock(&sx->lock_object);
470 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
471 * shared lock. If there are any shared waiters, wake them up.
475 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
476 (x & SX_LOCK_EXCLUSIVE_WAITERS));
477 if (x & SX_LOCK_SHARED_WAITERS)
478 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
480 sleepq_release(&sx->lock_object);
482 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
483 LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
490 * This function represents the so-called 'hard case' for sx_xlock
491 * operation. All 'easy case' failures are redirected to this. Note
492 * that ideally this would be a static function, but it needs to be
493 * accessible from at least sx.h.
496 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
501 volatile struct thread *owner;
502 u_int i, spintries = 0;
505 #ifdef LOCK_PROFILING
506 uint64_t waittime = 0;
511 uint64_t spin_cnt = 0;
512 uint64_t sleep_cnt = 0;
513 int64_t sleep_time = 0;
516 if (SCHEDULER_STOPPED())
519 /* If we already hold an exclusive lock, then recurse. */
520 if (sx_xlocked(sx)) {
521 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
522 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
523 sx->lock_object.lo_name, file, line));
525 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
526 if (LOCK_LOG_TEST(&sx->lock_object, 0))
527 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
531 if (LOCK_LOG_TEST(&sx->lock_object, 0))
532 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
533 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
535 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
540 PMC_SOFT_CALL( , , lock, failed);
542 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
546 * If the lock is write locked and the owner is
547 * running on another CPU, spin until the owner stops
548 * running or the state of the lock changes.
551 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
552 if ((x & SX_LOCK_SHARED) == 0) {
554 owner = (struct thread *)x;
555 if (TD_IS_RUNNING(owner)) {
556 if (LOCK_LOG_TEST(&sx->lock_object, 0))
558 "%s: spinning on %p held by %p",
559 __func__, sx, owner);
561 while (SX_OWNER(sx->sx_lock) == x &&
562 TD_IS_RUNNING(owner)) {
570 } else if (SX_SHARERS(x) && spintries < ASX_RETRIES) {
573 for (i = 0; i < ASX_LOOPS; i++) {
574 if (LOCK_LOG_TEST(&sx->lock_object, 0))
576 "%s: shared spinning on %p with %u and %u",
577 __func__, sx, spintries, i);
579 if ((x & SX_LOCK_SHARED) == 0 ||
593 sleepq_lock(&sx->lock_object);
597 * If the lock was released while spinning on the
598 * sleep queue chain lock, try again.
600 if (x == SX_LOCK_UNLOCKED) {
601 sleepq_release(&sx->lock_object);
607 * The current lock owner might have started executing
608 * on another CPU (or the lock could have changed
609 * owners) while we were waiting on the sleep queue
610 * chain lock. If so, drop the sleep queue lock and try
613 if (!(x & SX_LOCK_SHARED) &&
614 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
615 owner = (struct thread *)SX_OWNER(x);
616 if (TD_IS_RUNNING(owner)) {
617 sleepq_release(&sx->lock_object);
624 * If an exclusive lock was released with both shared
625 * and exclusive waiters and a shared waiter hasn't
626 * woken up and acquired the lock yet, sx_lock will be
627 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
628 * If we see that value, try to acquire it once. Note
629 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
630 * as there are other exclusive waiters still. If we
631 * fail, restart the loop.
633 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
634 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
635 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
636 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
637 sleepq_release(&sx->lock_object);
638 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
642 sleepq_release(&sx->lock_object);
647 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
648 * than loop back and retry.
650 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
651 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
652 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
653 sleepq_release(&sx->lock_object);
656 if (LOCK_LOG_TEST(&sx->lock_object, 0))
657 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
662 * Since we have been unable to acquire the exclusive
663 * lock and the exclusive waiters flag is set, we have
666 if (LOCK_LOG_TEST(&sx->lock_object, 0))
667 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
671 sleep_time -= lockstat_nsecs();
674 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
675 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
676 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
677 if (!(opts & SX_INTERRUPTIBLE))
678 sleepq_wait(&sx->lock_object, 0);
680 error = sleepq_wait_sig(&sx->lock_object, 0);
682 sleep_time += lockstat_nsecs();
686 if (LOCK_LOG_TEST(&sx->lock_object, 0))
688 "%s: interruptible sleep by %p suspended by signal",
692 if (LOCK_LOG_TEST(&sx->lock_object, 0))
693 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
699 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
700 contested, waittime, file, line);
703 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
704 if (spin_cnt > sleep_cnt)
705 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
711 * This function represents the so-called 'hard case' for sx_xunlock
712 * operation. All 'easy case' failures are redirected to this. Note
713 * that ideally this would be a static function, but it needs to be
714 * accessible from at least sx.h.
717 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
720 int queue, wakeup_swapper;
722 if (SCHEDULER_STOPPED())
725 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
727 /* If the lock is recursed, then unrecurse one level. */
728 if (sx_xlocked(sx) && sx_recursed(sx)) {
729 if ((--sx->sx_recurse) == 0)
730 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
731 if (LOCK_LOG_TEST(&sx->lock_object, 0))
732 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
735 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
736 SX_LOCK_EXCLUSIVE_WAITERS));
737 if (LOCK_LOG_TEST(&sx->lock_object, 0))
738 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
740 sleepq_lock(&sx->lock_object);
741 x = SX_LOCK_UNLOCKED;
744 * The wake up algorithm here is quite simple and probably not
745 * ideal. It gives precedence to shared waiters if they are
746 * present. For this condition, we have to preserve the
747 * state of the exclusive waiters flag.
748 * If interruptible sleeps left the shared queue empty avoid a
749 * starvation for the threads sleeping on the exclusive queue by giving
750 * them precedence and cleaning up the shared waiters bit anyway.
752 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
753 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
754 queue = SQ_SHARED_QUEUE;
755 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
757 queue = SQ_EXCLUSIVE_QUEUE;
759 /* Wake up all the waiters for the specific queue. */
760 if (LOCK_LOG_TEST(&sx->lock_object, 0))
761 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
762 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
764 atomic_store_rel_ptr(&sx->sx_lock, x);
765 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
767 sleepq_release(&sx->lock_object);
773 * This function represents the so-called 'hard case' for sx_slock
774 * operation. All 'easy case' failures are redirected to this. Note
775 * that ideally this would be a static function, but it needs to be
776 * accessible from at least sx.h.
779 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
783 volatile struct thread *owner;
785 #ifdef LOCK_PROFILING
786 uint64_t waittime = 0;
792 uint64_t spin_cnt = 0;
793 uint64_t sleep_cnt = 0;
794 int64_t sleep_time = 0;
797 if (SCHEDULER_STOPPED())
801 * As with rwlocks, we don't make any attempt to try to block
802 * shared locks once there is an exclusive waiter.
811 * If no other thread has an exclusive lock then try to bump up
812 * the count of sharers. Since we have to preserve the state
813 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
814 * shared lock loop back and retry.
816 if (x & SX_LOCK_SHARED) {
817 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
818 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
819 x + SX_ONE_SHARER)) {
820 if (LOCK_LOG_TEST(&sx->lock_object, 0))
822 "%s: %p succeed %p -> %p", __func__,
824 (void *)(x + SX_ONE_SHARER));
830 PMC_SOFT_CALL( , , lock, failed);
832 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
837 * If the owner is running on another CPU, spin until
838 * the owner stops running or the state of the lock
841 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
843 owner = (struct thread *)x;
844 if (TD_IS_RUNNING(owner)) {
845 if (LOCK_LOG_TEST(&sx->lock_object, 0))
847 "%s: spinning on %p held by %p",
848 __func__, sx, owner);
850 while (SX_OWNER(sx->sx_lock) == x &&
851 TD_IS_RUNNING(owner)) {
863 * Some other thread already has an exclusive lock, so
864 * start the process of blocking.
866 sleepq_lock(&sx->lock_object);
870 * The lock could have been released while we spun.
871 * In this case loop back and retry.
873 if (x & SX_LOCK_SHARED) {
874 sleepq_release(&sx->lock_object);
880 * If the owner is running on another CPU, spin until
881 * the owner stops running or the state of the lock
884 if (!(x & SX_LOCK_SHARED) &&
885 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
886 owner = (struct thread *)SX_OWNER(x);
887 if (TD_IS_RUNNING(owner)) {
888 sleepq_release(&sx->lock_object);
895 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
896 * fail to set it drop the sleep queue lock and loop
899 if (!(x & SX_LOCK_SHARED_WAITERS)) {
900 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
901 x | SX_LOCK_SHARED_WAITERS)) {
902 sleepq_release(&sx->lock_object);
905 if (LOCK_LOG_TEST(&sx->lock_object, 0))
906 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
911 * Since we have been unable to acquire the shared lock,
914 if (LOCK_LOG_TEST(&sx->lock_object, 0))
915 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
919 sleep_time -= lockstat_nsecs();
922 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
923 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
924 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
925 if (!(opts & SX_INTERRUPTIBLE))
926 sleepq_wait(&sx->lock_object, 0);
928 error = sleepq_wait_sig(&sx->lock_object, 0);
930 sleep_time += lockstat_nsecs();
934 if (LOCK_LOG_TEST(&sx->lock_object, 0))
936 "%s: interruptible sleep by %p suspended by signal",
940 if (LOCK_LOG_TEST(&sx->lock_object, 0))
941 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
945 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
946 contested, waittime, file, line);
949 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
950 if (spin_cnt > sleep_cnt)
951 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
958 * This function represents the so-called 'hard case' for sx_sunlock
959 * operation. All 'easy case' failures are redirected to this. Note
960 * that ideally this would be a static function, but it needs to be
961 * accessible from at least sx.h.
964 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
969 if (SCHEDULER_STOPPED())
976 * We should never have sharers while at least one thread
977 * holds a shared lock.
979 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
980 ("%s: waiting sharers", __func__));
983 * See if there is more than one shared lock held. If
984 * so, just drop one and return.
986 if (SX_SHARERS(x) > 1) {
987 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
988 x - SX_ONE_SHARER)) {
989 if (LOCK_LOG_TEST(&sx->lock_object, 0))
991 "%s: %p succeeded %p -> %p",
992 __func__, sx, (void *)x,
993 (void *)(x - SX_ONE_SHARER));
1000 * If there aren't any waiters for an exclusive lock,
1001 * then try to drop it quickly.
1003 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
1004 MPASS(x == SX_SHARERS_LOCK(1));
1005 if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1006 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
1007 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1008 CTR2(KTR_LOCK, "%s: %p last succeeded",
1016 * At this point, there should just be one sharer with
1017 * exclusive waiters.
1019 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
1021 sleepq_lock(&sx->lock_object);
1024 * Wake up semantic here is quite simple:
1025 * Just wake up all the exclusive waiters.
1026 * Note that the state of the lock could have changed,
1027 * so if it fails loop back and retry.
1029 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
1030 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
1031 SX_LOCK_UNLOCKED)) {
1032 sleepq_release(&sx->lock_object);
1035 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1036 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
1037 "exclusive queue", __func__, sx);
1038 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1039 0, SQ_EXCLUSIVE_QUEUE);
1040 sleepq_release(&sx->lock_object);
1047 #ifdef INVARIANT_SUPPORT
1053 * In the non-WITNESS case, sx_assert() can only detect that at least
1054 * *some* thread owns an slock, but it cannot guarantee that *this*
1055 * thread owns an slock.
1058 _sx_assert(struct sx *sx, int what, const char *file, int line)
1064 if (panicstr != NULL)
1068 case SA_SLOCKED | SA_NOTRECURSED:
1069 case SA_SLOCKED | SA_RECURSED:
1075 case SA_LOCKED | SA_NOTRECURSED:
1076 case SA_LOCKED | SA_RECURSED:
1078 witness_assert(&sx->lock_object, what, file, line);
1081 * If some other thread has an exclusive lock or we
1082 * have one and are asserting a shared lock, fail.
1083 * Also, if no one has a lock at all, fail.
1085 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1086 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1087 sx_xholder(sx) != curthread)))
1088 panic("Lock %s not %slocked @ %s:%d\n",
1089 sx->lock_object.lo_name, slocked ? "share " : "",
1092 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1093 if (sx_recursed(sx)) {
1094 if (what & SA_NOTRECURSED)
1095 panic("Lock %s recursed @ %s:%d\n",
1096 sx->lock_object.lo_name, file,
1098 } else if (what & SA_RECURSED)
1099 panic("Lock %s not recursed @ %s:%d\n",
1100 sx->lock_object.lo_name, file, line);
1105 case SA_XLOCKED | SA_NOTRECURSED:
1106 case SA_XLOCKED | SA_RECURSED:
1107 if (sx_xholder(sx) != curthread)
1108 panic("Lock %s not exclusively locked @ %s:%d\n",
1109 sx->lock_object.lo_name, file, line);
1110 if (sx_recursed(sx)) {
1111 if (what & SA_NOTRECURSED)
1112 panic("Lock %s recursed @ %s:%d\n",
1113 sx->lock_object.lo_name, file, line);
1114 } else if (what & SA_RECURSED)
1115 panic("Lock %s not recursed @ %s:%d\n",
1116 sx->lock_object.lo_name, file, line);
1120 witness_assert(&sx->lock_object, what, file, line);
1123 * If we hold an exclusve lock fail. We can't
1124 * reliably check to see if we hold a shared lock or
1127 if (sx_xholder(sx) == curthread)
1128 panic("Lock %s exclusively locked @ %s:%d\n",
1129 sx->lock_object.lo_name, file, line);
1133 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1137 #endif /* INVARIANT_SUPPORT */
1141 db_show_sx(struct lock_object *lock)
1146 sx = (struct sx *)lock;
1148 db_printf(" state: ");
1149 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1150 db_printf("UNLOCKED\n");
1151 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1152 db_printf("DESTROYED\n");
1154 } else if (sx->sx_lock & SX_LOCK_SHARED)
1155 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1157 td = sx_xholder(sx);
1158 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1159 td->td_tid, td->td_proc->p_pid, td->td_name);
1160 if (sx_recursed(sx))
1161 db_printf(" recursed: %d\n", sx->sx_recurse);
1164 db_printf(" waiters: ");
1165 switch(sx->sx_lock &
1166 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1167 case SX_LOCK_SHARED_WAITERS:
1168 db_printf("shared\n");
1170 case SX_LOCK_EXCLUSIVE_WAITERS:
1171 db_printf("exclusive\n");
1173 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1174 db_printf("exclusive and shared\n");
1177 db_printf("none\n");
1182 * Check to see if a thread that is blocked on a sleep queue is actually
1183 * blocked on an sx lock. If so, output some details and return true.
1184 * If the lock has an exclusive owner, return that in *ownerp.
1187 sx_chain(struct thread *td, struct thread **ownerp)
1192 * Check to see if this thread is blocked on an sx lock.
1193 * First, we check the lock class. If that is ok, then we
1194 * compare the lock name against the wait message.
1197 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1198 sx->lock_object.lo_name != td->td_wmesg)
1201 /* We think we have an sx lock, so output some details. */
1202 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1203 *ownerp = sx_xholder(sx);
1204 if (sx->sx_lock & SX_LOCK_SHARED)
1205 db_printf("SLOCK (count %ju)\n",
1206 (uintmax_t)SX_SHARERS(sx->sx_lock));
1208 db_printf("XLOCK\n");