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>
47 #include <sys/systm.h>
50 #include <sys/mutex.h>
52 #include <sys/sleepqueue.h>
54 #include <sys/sysctl.h>
56 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
57 #include <machine/cpu.h>
64 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
68 CTASSERT((SX_NOADAPTIVE & LO_CLASSFLAGS) == SX_NOADAPTIVE);
70 /* Handy macros for sleep queues. */
71 #define SQ_EXCLUSIVE_QUEUE 0
72 #define SQ_SHARED_QUEUE 1
75 #define ASX_RETRIES 10
76 #define ASX_LOOPS 10000
80 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
81 * drop Giant anytime we have to sleep or if we adaptively spin.
83 #define GIANT_DECLARE \
85 WITNESS_SAVE_DECL(Giant) \
87 #define GIANT_SAVE() do { \
88 if (mtx_owned(&Giant)) { \
89 WITNESS_SAVE(&Giant.lock_object, Giant); \
90 while (mtx_owned(&Giant)) { \
97 #define GIANT_RESTORE() do { \
98 if (_giantcnt > 0) { \
99 mtx_assert(&Giant, MA_NOTOWNED); \
100 while (_giantcnt--) \
102 WITNESS_RESTORE(&Giant.lock_object, Giant); \
107 * Returns true if an exclusive lock is recursed. It assumes
108 * curthread currently has an exclusive lock.
110 #define sx_recurse lock_object.lo_data
111 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
113 static void assert_sx(struct lock_object *lock, int what);
115 static void db_show_sx(struct lock_object *lock);
117 static void lock_sx(struct lock_object *lock, int how);
119 static int owner_sx(struct lock_object *lock, struct thread **owner);
121 static int 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 assert_sx(struct lock_object *lock, int what)
145 sx_assert((struct sx *)lock, what);
149 lock_sx(struct lock_object *lock, int how)
153 sx = (struct sx *)lock;
161 unlock_sx(struct lock_object *lock)
165 sx = (struct sx *)lock;
166 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
167 if (sx_xlocked(sx)) {
178 owner_sx(struct lock_object *lock, struct thread **owner)
180 struct sx *sx = (struct sx *)lock;
181 uintptr_t x = sx->sx_lock;
183 *owner = (struct thread *)SX_OWNER(x);
184 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
190 sx_sysinit(void *arg)
192 struct sx_args *sargs = arg;
194 sx_init(sargs->sa_sx, sargs->sa_desc);
198 sx_init_flags(struct sx *sx, const char *description, int opts)
202 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
203 SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
204 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
205 ("%s: sx_lock not aligned for %s: %p", __func__, description,
208 flags = LO_SLEEPABLE | LO_UPGRADABLE;
211 if (opts & SX_NOPROFILE)
212 flags |= LO_NOPROFILE;
213 if (!(opts & SX_NOWITNESS))
215 if (opts & SX_RECURSE)
216 flags |= LO_RECURSABLE;
220 flags |= opts & SX_NOADAPTIVE;
221 sx->sx_lock = SX_LOCK_UNLOCKED;
223 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
227 sx_destroy(struct sx *sx)
230 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
231 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
232 sx->sx_lock = SX_LOCK_DESTROYED;
233 lock_destroy(&sx->lock_object);
237 _sx_slock(struct sx *sx, int opts, const char *file, int line)
241 if (SCHEDULER_STOPPED())
243 MPASS(curthread != NULL);
244 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
245 ("sx_slock() of destroyed sx @ %s:%d", file, line));
246 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
247 error = __sx_slock(sx, opts, file, line);
249 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
250 WITNESS_LOCK(&sx->lock_object, 0, file, line);
251 curthread->td_locks++;
258 _sx_try_slock(struct sx *sx, const char *file, int line)
262 if (SCHEDULER_STOPPED())
267 KASSERT(x != SX_LOCK_DESTROYED,
268 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
269 if (!(x & SX_LOCK_SHARED))
271 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
272 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
273 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
274 curthread->td_locks++;
279 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
284 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
288 if (SCHEDULER_STOPPED())
290 MPASS(curthread != NULL);
291 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
292 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
293 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
295 error = __sx_xlock(sx, curthread, opts, file, line);
297 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
299 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
300 curthread->td_locks++;
307 _sx_try_xlock(struct sx *sx, const char *file, int line)
311 if (SCHEDULER_STOPPED())
314 MPASS(curthread != NULL);
315 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
316 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
318 if (sx_xlocked(sx) &&
319 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
321 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
324 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
325 (uintptr_t)curthread);
326 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
328 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
330 curthread->td_locks++;
337 _sx_sunlock(struct sx *sx, const char *file, int line)
340 if (SCHEDULER_STOPPED())
342 MPASS(curthread != NULL);
343 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
344 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
345 _sx_assert(sx, SA_SLOCKED, file, line);
346 curthread->td_locks--;
347 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
348 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
349 __sx_sunlock(sx, file, line);
350 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
354 _sx_xunlock(struct sx *sx, const char *file, int line)
357 if (SCHEDULER_STOPPED())
359 MPASS(curthread != NULL);
360 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
361 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
362 _sx_assert(sx, SA_XLOCKED, file, line);
363 curthread->td_locks--;
364 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
365 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
367 if (!sx_recursed(sx))
368 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
369 __sx_xunlock(sx, curthread, file, line);
373 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
374 * This will only succeed if this thread holds a single shared lock.
375 * Return 1 if if the upgrade succeed, 0 otherwise.
378 _sx_try_upgrade(struct sx *sx, const char *file, int line)
383 if (SCHEDULER_STOPPED())
386 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
387 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
388 _sx_assert(sx, SA_SLOCKED, file, line);
391 * Try to switch from one shared lock to an exclusive lock. We need
392 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
393 * we will wake up the exclusive waiters when we drop the lock.
395 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
396 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
397 (uintptr_t)curthread | x);
398 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
400 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
402 LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
408 * Downgrade an unrecursed exclusive lock into a single shared lock.
411 _sx_downgrade(struct sx *sx, const char *file, int line)
416 if (SCHEDULER_STOPPED())
419 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
420 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
421 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
424 panic("downgrade of a recursed lock");
427 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
430 * Try to switch from an exclusive lock with no shared waiters
431 * to one sharer with no shared waiters. If there are
432 * exclusive waiters, we don't need to lock the sleep queue so
433 * long as we preserve the flag. We do one quick try and if
434 * that fails we grab the sleepq lock to keep the flags from
435 * changing and do it the slow way.
437 * We have to lock the sleep queue if there are shared waiters
438 * so we can wake them up.
441 if (!(x & SX_LOCK_SHARED_WAITERS) &&
442 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
443 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
444 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
449 * Lock the sleep queue so we can read the waiters bits
450 * without any races and wakeup any shared waiters.
452 sleepq_lock(&sx->lock_object);
455 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
456 * shared lock. If there are any shared waiters, wake them up.
460 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
461 (x & SX_LOCK_EXCLUSIVE_WAITERS));
462 if (x & SX_LOCK_SHARED_WAITERS)
463 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
465 sleepq_release(&sx->lock_object);
467 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
468 LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
475 * This function represents the so-called 'hard case' for sx_xlock
476 * operation. All 'easy case' failures are redirected to this. Note
477 * that ideally this would be a static function, but it needs to be
478 * accessible from at least sx.h.
481 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
486 volatile struct thread *owner;
487 u_int i, spintries = 0;
490 #ifdef LOCK_PROFILING
491 uint64_t waittime = 0;
496 uint64_t spin_cnt = 0;
497 uint64_t sleep_cnt = 0;
498 int64_t sleep_time = 0;
501 if (SCHEDULER_STOPPED())
504 /* If we already hold an exclusive lock, then recurse. */
505 if (sx_xlocked(sx)) {
506 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
507 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
508 sx->lock_object.lo_name, file, line));
510 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
511 if (LOCK_LOG_TEST(&sx->lock_object, 0))
512 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
516 if (LOCK_LOG_TEST(&sx->lock_object, 0))
517 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
518 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
520 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
524 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
528 * If the lock is write locked and the owner is
529 * running on another CPU, spin until the owner stops
530 * running or the state of the lock changes.
533 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
534 if ((x & SX_LOCK_SHARED) == 0) {
536 owner = (struct thread *)x;
537 if (TD_IS_RUNNING(owner)) {
538 if (LOCK_LOG_TEST(&sx->lock_object, 0))
540 "%s: spinning on %p held by %p",
541 __func__, sx, owner);
543 while (SX_OWNER(sx->sx_lock) == x &&
544 TD_IS_RUNNING(owner)) {
552 } else if (SX_SHARERS(x) && spintries < ASX_RETRIES) {
555 for (i = 0; i < ASX_LOOPS; i++) {
556 if (LOCK_LOG_TEST(&sx->lock_object, 0))
558 "%s: shared spinning on %p with %u and %u",
559 __func__, sx, spintries, i);
561 if ((x & SX_LOCK_SHARED) == 0 ||
575 sleepq_lock(&sx->lock_object);
579 * If the lock was released while spinning on the
580 * sleep queue chain lock, try again.
582 if (x == SX_LOCK_UNLOCKED) {
583 sleepq_release(&sx->lock_object);
589 * The current lock owner might have started executing
590 * on another CPU (or the lock could have changed
591 * owners) while we were waiting on the sleep queue
592 * chain lock. If so, drop the sleep queue lock and try
595 if (!(x & SX_LOCK_SHARED) &&
596 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
597 owner = (struct thread *)SX_OWNER(x);
598 if (TD_IS_RUNNING(owner)) {
599 sleepq_release(&sx->lock_object);
606 * If an exclusive lock was released with both shared
607 * and exclusive waiters and a shared waiter hasn't
608 * woken up and acquired the lock yet, sx_lock will be
609 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
610 * If we see that value, try to acquire it once. Note
611 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
612 * as there are other exclusive waiters still. If we
613 * fail, restart the loop.
615 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
616 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
617 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
618 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
619 sleepq_release(&sx->lock_object);
620 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
624 sleepq_release(&sx->lock_object);
629 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
630 * than loop back and retry.
632 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
633 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
634 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
635 sleepq_release(&sx->lock_object);
638 if (LOCK_LOG_TEST(&sx->lock_object, 0))
639 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
644 * Since we have been unable to acquire the exclusive
645 * lock and the exclusive waiters flag is set, we have
648 if (LOCK_LOG_TEST(&sx->lock_object, 0))
649 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
653 sleep_time -= lockstat_nsecs();
656 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
657 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
658 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
659 if (!(opts & SX_INTERRUPTIBLE))
660 sleepq_wait(&sx->lock_object, 0);
662 error = sleepq_wait_sig(&sx->lock_object, 0);
664 sleep_time += lockstat_nsecs();
668 if (LOCK_LOG_TEST(&sx->lock_object, 0))
670 "%s: interruptible sleep by %p suspended by signal",
674 if (LOCK_LOG_TEST(&sx->lock_object, 0))
675 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
681 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
682 contested, waittime, file, line);
685 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
686 if (spin_cnt > sleep_cnt)
687 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
693 * This function represents the so-called 'hard case' for sx_xunlock
694 * operation. All 'easy case' failures are redirected to this. Note
695 * that ideally this would be a static function, but it needs to be
696 * accessible from at least sx.h.
699 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
702 int queue, wakeup_swapper;
704 if (SCHEDULER_STOPPED())
707 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
709 /* If the lock is recursed, then unrecurse one level. */
710 if (sx_xlocked(sx) && sx_recursed(sx)) {
711 if ((--sx->sx_recurse) == 0)
712 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
713 if (LOCK_LOG_TEST(&sx->lock_object, 0))
714 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
717 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
718 SX_LOCK_EXCLUSIVE_WAITERS));
719 if (LOCK_LOG_TEST(&sx->lock_object, 0))
720 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
722 sleepq_lock(&sx->lock_object);
723 x = SX_LOCK_UNLOCKED;
726 * The wake up algorithm here is quite simple and probably not
727 * ideal. It gives precedence to shared waiters if they are
728 * present. For this condition, we have to preserve the
729 * state of the exclusive waiters flag.
730 * If interruptible sleeps left the shared queue empty avoid a
731 * starvation for the threads sleeping on the exclusive queue by giving
732 * them precedence and cleaning up the shared waiters bit anyway.
734 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
735 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
736 queue = SQ_SHARED_QUEUE;
737 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
739 queue = SQ_EXCLUSIVE_QUEUE;
741 /* Wake up all the waiters for the specific queue. */
742 if (LOCK_LOG_TEST(&sx->lock_object, 0))
743 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
744 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
746 atomic_store_rel_ptr(&sx->sx_lock, x);
747 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
749 sleepq_release(&sx->lock_object);
755 * This function represents the so-called 'hard case' for sx_slock
756 * operation. All 'easy case' failures are redirected to this. Note
757 * that ideally this would be a static function, but it needs to be
758 * accessible from at least sx.h.
761 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
765 volatile struct thread *owner;
767 #ifdef LOCK_PROFILING
768 uint64_t waittime = 0;
774 uint64_t spin_cnt = 0;
775 uint64_t sleep_cnt = 0;
776 int64_t sleep_time = 0;
779 if (SCHEDULER_STOPPED())
783 * As with rwlocks, we don't make any attempt to try to block
784 * shared locks once there is an exclusive waiter.
793 * If no other thread has an exclusive lock then try to bump up
794 * the count of sharers. Since we have to preserve the state
795 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
796 * shared lock loop back and retry.
798 if (x & SX_LOCK_SHARED) {
799 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
800 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
801 x + SX_ONE_SHARER)) {
802 if (LOCK_LOG_TEST(&sx->lock_object, 0))
804 "%s: %p succeed %p -> %p", __func__,
806 (void *)(x + SX_ONE_SHARER));
811 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
816 * If the owner is running on another CPU, spin until
817 * the owner stops running or the state of the lock
820 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
822 owner = (struct thread *)x;
823 if (TD_IS_RUNNING(owner)) {
824 if (LOCK_LOG_TEST(&sx->lock_object, 0))
826 "%s: spinning on %p held by %p",
827 __func__, sx, owner);
829 while (SX_OWNER(sx->sx_lock) == x &&
830 TD_IS_RUNNING(owner)) {
842 * Some other thread already has an exclusive lock, so
843 * start the process of blocking.
845 sleepq_lock(&sx->lock_object);
849 * The lock could have been released while we spun.
850 * In this case loop back and retry.
852 if (x & SX_LOCK_SHARED) {
853 sleepq_release(&sx->lock_object);
859 * If the owner is running on another CPU, spin until
860 * the owner stops running or the state of the lock
863 if (!(x & SX_LOCK_SHARED) &&
864 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
865 owner = (struct thread *)SX_OWNER(x);
866 if (TD_IS_RUNNING(owner)) {
867 sleepq_release(&sx->lock_object);
874 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
875 * fail to set it drop the sleep queue lock and loop
878 if (!(x & SX_LOCK_SHARED_WAITERS)) {
879 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
880 x | SX_LOCK_SHARED_WAITERS)) {
881 sleepq_release(&sx->lock_object);
884 if (LOCK_LOG_TEST(&sx->lock_object, 0))
885 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
890 * Since we have been unable to acquire the shared lock,
893 if (LOCK_LOG_TEST(&sx->lock_object, 0))
894 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
898 sleep_time -= lockstat_nsecs();
901 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
902 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
903 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
904 if (!(opts & SX_INTERRUPTIBLE))
905 sleepq_wait(&sx->lock_object, 0);
907 error = sleepq_wait_sig(&sx->lock_object, 0);
909 sleep_time += lockstat_nsecs();
913 if (LOCK_LOG_TEST(&sx->lock_object, 0))
915 "%s: interruptible sleep by %p suspended by signal",
919 if (LOCK_LOG_TEST(&sx->lock_object, 0))
920 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
924 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
925 contested, waittime, file, line);
928 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
929 if (spin_cnt > sleep_cnt)
930 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
937 * This function represents the so-called 'hard case' for sx_sunlock
938 * operation. All 'easy case' failures are redirected to this. Note
939 * that ideally this would be a static function, but it needs to be
940 * accessible from at least sx.h.
943 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
948 if (SCHEDULER_STOPPED())
955 * We should never have sharers while at least one thread
956 * holds a shared lock.
958 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
959 ("%s: waiting sharers", __func__));
962 * See if there is more than one shared lock held. If
963 * so, just drop one and return.
965 if (SX_SHARERS(x) > 1) {
966 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
967 x - SX_ONE_SHARER)) {
968 if (LOCK_LOG_TEST(&sx->lock_object, 0))
970 "%s: %p succeeded %p -> %p",
971 __func__, sx, (void *)x,
972 (void *)(x - SX_ONE_SHARER));
979 * If there aren't any waiters for an exclusive lock,
980 * then try to drop it quickly.
982 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
983 MPASS(x == SX_SHARERS_LOCK(1));
984 if (atomic_cmpset_rel_ptr(&sx->sx_lock,
985 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
986 if (LOCK_LOG_TEST(&sx->lock_object, 0))
987 CTR2(KTR_LOCK, "%s: %p last succeeded",
995 * At this point, there should just be one sharer with
998 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
1000 sleepq_lock(&sx->lock_object);
1003 * Wake up semantic here is quite simple:
1004 * Just wake up all the exclusive waiters.
1005 * Note that the state of the lock could have changed,
1006 * so if it fails loop back and retry.
1008 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
1009 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
1010 SX_LOCK_UNLOCKED)) {
1011 sleepq_release(&sx->lock_object);
1014 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1015 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
1016 "exclusive queue", __func__, sx);
1017 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1018 0, SQ_EXCLUSIVE_QUEUE);
1019 sleepq_release(&sx->lock_object);
1026 #ifdef INVARIANT_SUPPORT
1032 * In the non-WITNESS case, sx_assert() can only detect that at least
1033 * *some* thread owns an slock, but it cannot guarantee that *this*
1034 * thread owns an slock.
1037 _sx_assert(struct sx *sx, int what, const char *file, int line)
1043 if (panicstr != NULL)
1047 case SA_SLOCKED | SA_NOTRECURSED:
1048 case SA_SLOCKED | SA_RECURSED:
1054 case SA_LOCKED | SA_NOTRECURSED:
1055 case SA_LOCKED | SA_RECURSED:
1057 witness_assert(&sx->lock_object, what, file, line);
1060 * If some other thread has an exclusive lock or we
1061 * have one and are asserting a shared lock, fail.
1062 * Also, if no one has a lock at all, fail.
1064 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1065 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1066 sx_xholder(sx) != curthread)))
1067 panic("Lock %s not %slocked @ %s:%d\n",
1068 sx->lock_object.lo_name, slocked ? "share " : "",
1071 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1072 if (sx_recursed(sx)) {
1073 if (what & SA_NOTRECURSED)
1074 panic("Lock %s recursed @ %s:%d\n",
1075 sx->lock_object.lo_name, file,
1077 } else if (what & SA_RECURSED)
1078 panic("Lock %s not recursed @ %s:%d\n",
1079 sx->lock_object.lo_name, file, line);
1084 case SA_XLOCKED | SA_NOTRECURSED:
1085 case SA_XLOCKED | SA_RECURSED:
1086 if (sx_xholder(sx) != curthread)
1087 panic("Lock %s not exclusively locked @ %s:%d\n",
1088 sx->lock_object.lo_name, file, line);
1089 if (sx_recursed(sx)) {
1090 if (what & SA_NOTRECURSED)
1091 panic("Lock %s recursed @ %s:%d\n",
1092 sx->lock_object.lo_name, file, line);
1093 } else if (what & SA_RECURSED)
1094 panic("Lock %s not recursed @ %s:%d\n",
1095 sx->lock_object.lo_name, file, line);
1099 witness_assert(&sx->lock_object, what, file, line);
1102 * If we hold an exclusve lock fail. We can't
1103 * reliably check to see if we hold a shared lock or
1106 if (sx_xholder(sx) == curthread)
1107 panic("Lock %s exclusively locked @ %s:%d\n",
1108 sx->lock_object.lo_name, file, line);
1112 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1116 #endif /* INVARIANT_SUPPORT */
1120 db_show_sx(struct lock_object *lock)
1125 sx = (struct sx *)lock;
1127 db_printf(" state: ");
1128 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1129 db_printf("UNLOCKED\n");
1130 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1131 db_printf("DESTROYED\n");
1133 } else if (sx->sx_lock & SX_LOCK_SHARED)
1134 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1136 td = sx_xholder(sx);
1137 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1138 td->td_tid, td->td_proc->p_pid, td->td_name);
1139 if (sx_recursed(sx))
1140 db_printf(" recursed: %d\n", sx->sx_recurse);
1143 db_printf(" waiters: ");
1144 switch(sx->sx_lock &
1145 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1146 case SX_LOCK_SHARED_WAITERS:
1147 db_printf("shared\n");
1149 case SX_LOCK_EXCLUSIVE_WAITERS:
1150 db_printf("exclusive\n");
1152 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1153 db_printf("exclusive and shared\n");
1156 db_printf("none\n");
1161 * Check to see if a thread that is blocked on a sleep queue is actually
1162 * blocked on an sx lock. If so, output some details and return true.
1163 * If the lock has an exclusive owner, return that in *ownerp.
1166 sx_chain(struct thread *td, struct thread **ownerp)
1171 * Check to see if this thread is blocked on an sx lock.
1172 * First, we check the lock class. If that is ok, then we
1173 * compare the lock name against the wait message.
1176 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1177 sx->lock_object.lo_name != td->td_wmesg)
1180 /* We think we have an sx lock, so output some details. */
1181 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1182 *ownerp = sx_xholder(sx);
1183 if (sx->sx_lock & SX_LOCK_SHARED)
1184 db_printf("SLOCK (count %ju)\n",
1185 (uintmax_t)SX_SHARERS(sx->sx_lock));
1187 db_printf("XLOCK\n");