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>
49 #include <sys/mutex.h>
51 #include <sys/sleepqueue.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.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 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
76 * drop Giant anytime we have to sleep or if we adaptively spin.
78 #define GIANT_DECLARE \
80 WITNESS_SAVE_DECL(Giant) \
82 #define GIANT_SAVE() do { \
83 if (mtx_owned(&Giant)) { \
84 WITNESS_SAVE(&Giant.lock_object, Giant); \
85 while (mtx_owned(&Giant)) { \
92 #define GIANT_RESTORE() do { \
93 if (_giantcnt > 0) { \
94 mtx_assert(&Giant, MA_NOTOWNED); \
97 WITNESS_RESTORE(&Giant.lock_object, Giant); \
102 * Returns true if an exclusive lock is recursed. It assumes
103 * curthread currently has an exclusive lock.
105 #define sx_recurse lock_object.lo_data
106 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
108 static void assert_sx(struct lock_object *lock, int what);
110 static void db_show_sx(struct lock_object *lock);
112 static void lock_sx(struct lock_object *lock, int how);
114 static int owner_sx(struct lock_object *lock, struct thread **owner);
116 static int unlock_sx(struct lock_object *lock);
118 struct lock_class lock_class_sx = {
120 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
121 .lc_assert = assert_sx,
123 .lc_ddb_show = db_show_sx,
126 .lc_unlock = unlock_sx,
128 .lc_owner = owner_sx,
133 #define _sx_assert(sx, what, file, line)
137 static u_int asx_retries = 10;
138 static u_int asx_loops = 10000;
139 SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
140 SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
141 SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
145 assert_sx(struct lock_object *lock, int what)
148 sx_assert((struct sx *)lock, what);
152 lock_sx(struct lock_object *lock, int how)
156 sx = (struct sx *)lock;
164 unlock_sx(struct lock_object *lock)
168 sx = (struct sx *)lock;
169 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
170 if (sx_xlocked(sx)) {
181 owner_sx(struct lock_object *lock, struct thread **owner)
183 struct sx *sx = (struct sx *)lock;
184 uintptr_t x = sx->sx_lock;
186 *owner = (struct thread *)SX_OWNER(x);
187 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
193 sx_sysinit(void *arg)
195 struct sx_args *sargs = arg;
197 sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
201 sx_init_flags(struct sx *sx, const char *description, int opts)
205 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
206 SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
207 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
208 ("%s: sx_lock not aligned for %s: %p", __func__, description,
211 flags = LO_SLEEPABLE | LO_UPGRADABLE;
214 if (opts & SX_NOPROFILE)
215 flags |= LO_NOPROFILE;
216 if (!(opts & SX_NOWITNESS))
218 if (opts & SX_RECURSE)
219 flags |= LO_RECURSABLE;
223 flags |= opts & SX_NOADAPTIVE;
224 sx->sx_lock = SX_LOCK_UNLOCKED;
226 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
230 sx_destroy(struct sx *sx)
233 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
234 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
235 sx->sx_lock = SX_LOCK_DESTROYED;
236 lock_destroy(&sx->lock_object);
240 _sx_slock(struct sx *sx, int opts, const char *file, int line)
244 MPASS(curthread != NULL);
245 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
246 ("sx_slock() of destroyed sx @ %s:%d", file, line));
247 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
248 error = __sx_slock(sx, opts, file, line);
250 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
251 WITNESS_LOCK(&sx->lock_object, 0, file, line);
252 curthread->td_locks++;
259 _sx_try_slock(struct sx *sx, const char *file, int line)
265 KASSERT(x != SX_LOCK_DESTROYED,
266 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
267 if (!(x & SX_LOCK_SHARED))
269 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
270 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
271 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
272 curthread->td_locks++;
277 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
282 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
286 MPASS(curthread != NULL);
287 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
288 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
289 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
291 error = __sx_xlock(sx, curthread, opts, file, line);
293 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
295 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
296 curthread->td_locks++;
303 _sx_try_xlock(struct sx *sx, const char *file, int line)
307 MPASS(curthread != NULL);
308 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
309 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
311 if (sx_xlocked(sx) &&
312 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
314 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
317 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
318 (uintptr_t)curthread);
319 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
321 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
323 curthread->td_locks++;
330 _sx_sunlock(struct sx *sx, const char *file, int line)
333 MPASS(curthread != NULL);
334 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
335 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
336 _sx_assert(sx, SA_SLOCKED, file, line);
337 curthread->td_locks--;
338 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
339 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
340 __sx_sunlock(sx, file, line);
341 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
345 _sx_xunlock(struct sx *sx, const char *file, int line)
348 MPASS(curthread != NULL);
349 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
350 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
351 _sx_assert(sx, SA_XLOCKED, file, line);
352 curthread->td_locks--;
353 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
354 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
356 if (!sx_recursed(sx))
357 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
358 __sx_xunlock(sx, curthread, file, line);
362 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
363 * This will only succeed if this thread holds a single shared lock.
364 * Return 1 if if the upgrade succeed, 0 otherwise.
367 _sx_try_upgrade(struct sx *sx, const char *file, int line)
372 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
373 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
374 _sx_assert(sx, SA_SLOCKED, file, line);
377 * Try to switch from one shared lock to an exclusive lock. We need
378 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
379 * we will wake up the exclusive waiters when we drop the lock.
381 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
382 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
383 (uintptr_t)curthread | x);
384 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
386 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
388 LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
394 * Downgrade an unrecursed exclusive lock into a single shared lock.
397 _sx_downgrade(struct sx *sx, const char *file, int line)
402 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
403 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
404 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
407 panic("downgrade of a recursed lock");
410 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
413 * Try to switch from an exclusive lock with no shared waiters
414 * to one sharer with no shared waiters. If there are
415 * exclusive waiters, we don't need to lock the sleep queue so
416 * long as we preserve the flag. We do one quick try and if
417 * that fails we grab the sleepq lock to keep the flags from
418 * changing and do it the slow way.
420 * We have to lock the sleep queue if there are shared waiters
421 * so we can wake them up.
424 if (!(x & SX_LOCK_SHARED_WAITERS) &&
425 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
426 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
427 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
432 * Lock the sleep queue so we can read the waiters bits
433 * without any races and wakeup any shared waiters.
435 sleepq_lock(&sx->lock_object);
438 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
439 * shared lock. If there are any shared waiters, wake them up.
443 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
444 (x & SX_LOCK_EXCLUSIVE_WAITERS));
445 if (x & SX_LOCK_SHARED_WAITERS)
446 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
448 sleepq_release(&sx->lock_object);
450 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
451 LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
458 * This function represents the so-called 'hard case' for sx_xlock
459 * operation. All 'easy case' failures are redirected to this. Note
460 * that ideally this would be a static function, but it needs to be
461 * accessible from at least sx.h.
464 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
469 volatile struct thread *owner;
470 u_int i, spintries = 0;
473 #ifdef LOCK_PROFILING
474 uint64_t waittime = 0;
479 uint64_t spin_cnt = 0;
480 uint64_t sleep_cnt = 0;
481 int64_t sleep_time = 0;
484 /* If we already hold an exclusive lock, then recurse. */
485 if (sx_xlocked(sx)) {
486 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
487 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
488 sx->lock_object.lo_name, file, line));
490 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
491 if (LOCK_LOG_TEST(&sx->lock_object, 0))
492 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
496 if (LOCK_LOG_TEST(&sx->lock_object, 0))
497 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
498 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
500 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
504 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
508 * If the lock is write locked and the owner is
509 * running on another CPU, spin until the owner stops
510 * running or the state of the lock changes.
513 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
514 if ((x & SX_LOCK_SHARED) == 0) {
516 owner = (struct thread *)x;
517 if (TD_IS_RUNNING(owner)) {
518 if (LOCK_LOG_TEST(&sx->lock_object, 0))
520 "%s: spinning on %p held by %p",
521 __func__, sx, owner);
523 while (SX_OWNER(sx->sx_lock) == x &&
524 TD_IS_RUNNING(owner)) {
532 } else if (SX_SHARERS(x) && spintries < asx_retries) {
535 for (i = 0; i < asx_loops; i++) {
536 if (LOCK_LOG_TEST(&sx->lock_object, 0))
538 "%s: shared spinning on %p with %u and %u",
539 __func__, sx, spintries, i);
541 if ((x & SX_LOCK_SHARED) == 0 ||
555 sleepq_lock(&sx->lock_object);
559 * If the lock was released while spinning on the
560 * sleep queue chain lock, try again.
562 if (x == SX_LOCK_UNLOCKED) {
563 sleepq_release(&sx->lock_object);
569 * The current lock owner might have started executing
570 * on another CPU (or the lock could have changed
571 * owners) while we were waiting on the sleep queue
572 * chain lock. If so, drop the sleep queue lock and try
575 if (!(x & SX_LOCK_SHARED) &&
576 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
577 owner = (struct thread *)SX_OWNER(x);
578 if (TD_IS_RUNNING(owner)) {
579 sleepq_release(&sx->lock_object);
586 * If an exclusive lock was released with both shared
587 * and exclusive waiters and a shared waiter hasn't
588 * woken up and acquired the lock yet, sx_lock will be
589 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
590 * If we see that value, try to acquire it once. Note
591 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
592 * as there are other exclusive waiters still. If we
593 * fail, restart the loop.
595 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
596 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
597 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
598 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
599 sleepq_release(&sx->lock_object);
600 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
604 sleepq_release(&sx->lock_object);
609 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
610 * than loop back and retry.
612 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
613 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
614 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
615 sleepq_release(&sx->lock_object);
618 if (LOCK_LOG_TEST(&sx->lock_object, 0))
619 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
624 * Since we have been unable to acquire the exclusive
625 * lock and the exclusive waiters flag is set, we have
628 if (LOCK_LOG_TEST(&sx->lock_object, 0))
629 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
633 sleep_time -= lockstat_nsecs();
636 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
637 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
638 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
639 if (!(opts & SX_INTERRUPTIBLE))
640 sleepq_wait(&sx->lock_object, 0);
642 error = sleepq_wait_sig(&sx->lock_object, 0);
644 sleep_time += lockstat_nsecs();
648 if (LOCK_LOG_TEST(&sx->lock_object, 0))
650 "%s: interruptible sleep by %p suspended by signal",
654 if (LOCK_LOG_TEST(&sx->lock_object, 0))
655 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
661 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
662 contested, waittime, file, line);
665 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
666 if (spin_cnt > sleep_cnt)
667 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
673 * This function represents the so-called 'hard case' for sx_xunlock
674 * operation. All 'easy case' failures are redirected to this. Note
675 * that ideally this would be a static function, but it needs to be
676 * accessible from at least sx.h.
679 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
682 int queue, wakeup_swapper;
684 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
686 /* If the lock is recursed, then unrecurse one level. */
687 if (sx_xlocked(sx) && sx_recursed(sx)) {
688 if ((--sx->sx_recurse) == 0)
689 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
690 if (LOCK_LOG_TEST(&sx->lock_object, 0))
691 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
694 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
695 SX_LOCK_EXCLUSIVE_WAITERS));
696 if (LOCK_LOG_TEST(&sx->lock_object, 0))
697 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
699 sleepq_lock(&sx->lock_object);
700 x = SX_LOCK_UNLOCKED;
703 * The wake up algorithm here is quite simple and probably not
704 * ideal. It gives precedence to shared waiters if they are
705 * present. For this condition, we have to preserve the
706 * state of the exclusive waiters flag.
707 * If interruptible sleeps left the shared queue empty avoid a
708 * starvation for the threads sleeping on the exclusive queue by giving
709 * them precedence and cleaning up the shared waiters bit anyway.
711 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
712 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
713 queue = SQ_SHARED_QUEUE;
714 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
716 queue = SQ_EXCLUSIVE_QUEUE;
718 /* Wake up all the waiters for the specific queue. */
719 if (LOCK_LOG_TEST(&sx->lock_object, 0))
720 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
721 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
723 atomic_store_rel_ptr(&sx->sx_lock, x);
724 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
726 sleepq_release(&sx->lock_object);
732 * This function represents the so-called 'hard case' for sx_slock
733 * operation. All 'easy case' failures are redirected to this. Note
734 * that ideally this would be a static function, but it needs to be
735 * accessible from at least sx.h.
738 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
742 volatile struct thread *owner;
744 #ifdef LOCK_PROFILING
745 uint64_t waittime = 0;
751 uint64_t spin_cnt = 0;
752 uint64_t sleep_cnt = 0;
753 int64_t sleep_time = 0;
757 * As with rwlocks, we don't make any attempt to try to block
758 * shared locks once there is an exclusive waiter.
767 * If no other thread has an exclusive lock then try to bump up
768 * the count of sharers. Since we have to preserve the state
769 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
770 * shared lock loop back and retry.
772 if (x & SX_LOCK_SHARED) {
773 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
774 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
775 x + SX_ONE_SHARER)) {
776 if (LOCK_LOG_TEST(&sx->lock_object, 0))
778 "%s: %p succeed %p -> %p", __func__,
780 (void *)(x + SX_ONE_SHARER));
785 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
790 * If the owner is running on another CPU, spin until
791 * the owner stops running or the state of the lock
794 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
796 owner = (struct thread *)x;
797 if (TD_IS_RUNNING(owner)) {
798 if (LOCK_LOG_TEST(&sx->lock_object, 0))
800 "%s: spinning on %p held by %p",
801 __func__, sx, owner);
803 while (SX_OWNER(sx->sx_lock) == x &&
804 TD_IS_RUNNING(owner)) {
816 * Some other thread already has an exclusive lock, so
817 * start the process of blocking.
819 sleepq_lock(&sx->lock_object);
823 * The lock could have been released while we spun.
824 * In this case loop back and retry.
826 if (x & SX_LOCK_SHARED) {
827 sleepq_release(&sx->lock_object);
833 * If the owner is running on another CPU, spin until
834 * the owner stops running or the state of the lock
837 if (!(x & SX_LOCK_SHARED) &&
838 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
839 owner = (struct thread *)SX_OWNER(x);
840 if (TD_IS_RUNNING(owner)) {
841 sleepq_release(&sx->lock_object);
848 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
849 * fail to set it drop the sleep queue lock and loop
852 if (!(x & SX_LOCK_SHARED_WAITERS)) {
853 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
854 x | SX_LOCK_SHARED_WAITERS)) {
855 sleepq_release(&sx->lock_object);
858 if (LOCK_LOG_TEST(&sx->lock_object, 0))
859 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
864 * Since we have been unable to acquire the shared lock,
867 if (LOCK_LOG_TEST(&sx->lock_object, 0))
868 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
872 sleep_time -= lockstat_nsecs();
875 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
876 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
877 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
878 if (!(opts & SX_INTERRUPTIBLE))
879 sleepq_wait(&sx->lock_object, 0);
881 error = sleepq_wait_sig(&sx->lock_object, 0);
883 sleep_time += lockstat_nsecs();
887 if (LOCK_LOG_TEST(&sx->lock_object, 0))
889 "%s: interruptible sleep by %p suspended by signal",
893 if (LOCK_LOG_TEST(&sx->lock_object, 0))
894 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
898 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
899 contested, waittime, file, line);
902 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
903 if (spin_cnt > sleep_cnt)
904 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
911 * This function represents the so-called 'hard case' for sx_sunlock
912 * operation. All 'easy case' failures are redirected to this. Note
913 * that ideally this would be a static function, but it needs to be
914 * accessible from at least sx.h.
917 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
926 * We should never have sharers while at least one thread
927 * holds a shared lock.
929 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
930 ("%s: waiting sharers", __func__));
933 * See if there is more than one shared lock held. If
934 * so, just drop one and return.
936 if (SX_SHARERS(x) > 1) {
937 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
938 x - SX_ONE_SHARER)) {
939 if (LOCK_LOG_TEST(&sx->lock_object, 0))
941 "%s: %p succeeded %p -> %p",
942 __func__, sx, (void *)x,
943 (void *)(x - SX_ONE_SHARER));
950 * If there aren't any waiters for an exclusive lock,
951 * then try to drop it quickly.
953 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
954 MPASS(x == SX_SHARERS_LOCK(1));
955 if (atomic_cmpset_rel_ptr(&sx->sx_lock,
956 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
957 if (LOCK_LOG_TEST(&sx->lock_object, 0))
958 CTR2(KTR_LOCK, "%s: %p last succeeded",
966 * At this point, there should just be one sharer with
969 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
971 sleepq_lock(&sx->lock_object);
974 * Wake up semantic here is quite simple:
975 * Just wake up all the exclusive waiters.
976 * Note that the state of the lock could have changed,
977 * so if it fails loop back and retry.
979 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
980 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
982 sleepq_release(&sx->lock_object);
985 if (LOCK_LOG_TEST(&sx->lock_object, 0))
986 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
987 "exclusive queue", __func__, sx);
988 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
989 0, SQ_EXCLUSIVE_QUEUE);
990 sleepq_release(&sx->lock_object);
997 #ifdef INVARIANT_SUPPORT
1003 * In the non-WITNESS case, sx_assert() can only detect that at least
1004 * *some* thread owns an slock, but it cannot guarantee that *this*
1005 * thread owns an slock.
1008 _sx_assert(struct sx *sx, int what, const char *file, int line)
1014 if (panicstr != NULL)
1018 case SA_SLOCKED | SA_NOTRECURSED:
1019 case SA_SLOCKED | SA_RECURSED:
1025 case SA_LOCKED | SA_NOTRECURSED:
1026 case SA_LOCKED | SA_RECURSED:
1028 witness_assert(&sx->lock_object, what, file, line);
1031 * If some other thread has an exclusive lock or we
1032 * have one and are asserting a shared lock, fail.
1033 * Also, if no one has a lock at all, fail.
1035 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1036 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1037 sx_xholder(sx) != curthread)))
1038 panic("Lock %s not %slocked @ %s:%d\n",
1039 sx->lock_object.lo_name, slocked ? "share " : "",
1042 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1043 if (sx_recursed(sx)) {
1044 if (what & SA_NOTRECURSED)
1045 panic("Lock %s recursed @ %s:%d\n",
1046 sx->lock_object.lo_name, file,
1048 } else if (what & SA_RECURSED)
1049 panic("Lock %s not recursed @ %s:%d\n",
1050 sx->lock_object.lo_name, file, line);
1055 case SA_XLOCKED | SA_NOTRECURSED:
1056 case SA_XLOCKED | SA_RECURSED:
1057 if (sx_xholder(sx) != curthread)
1058 panic("Lock %s not exclusively locked @ %s:%d\n",
1059 sx->lock_object.lo_name, file, line);
1060 if (sx_recursed(sx)) {
1061 if (what & SA_NOTRECURSED)
1062 panic("Lock %s recursed @ %s:%d\n",
1063 sx->lock_object.lo_name, file, line);
1064 } else if (what & SA_RECURSED)
1065 panic("Lock %s not recursed @ %s:%d\n",
1066 sx->lock_object.lo_name, file, line);
1070 witness_assert(&sx->lock_object, what, file, line);
1073 * If we hold an exclusve lock fail. We can't
1074 * reliably check to see if we hold a shared lock or
1077 if (sx_xholder(sx) == curthread)
1078 panic("Lock %s exclusively locked @ %s:%d\n",
1079 sx->lock_object.lo_name, file, line);
1083 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1087 #endif /* INVARIANT_SUPPORT */
1091 db_show_sx(struct lock_object *lock)
1096 sx = (struct sx *)lock;
1098 db_printf(" state: ");
1099 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1100 db_printf("UNLOCKED\n");
1101 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1102 db_printf("DESTROYED\n");
1104 } else if (sx->sx_lock & SX_LOCK_SHARED)
1105 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1107 td = sx_xholder(sx);
1108 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1109 td->td_tid, td->td_proc->p_pid, td->td_name);
1110 if (sx_recursed(sx))
1111 db_printf(" recursed: %d\n", sx->sx_recurse);
1114 db_printf(" waiters: ");
1115 switch(sx->sx_lock &
1116 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1117 case SX_LOCK_SHARED_WAITERS:
1118 db_printf("shared\n");
1120 case SX_LOCK_EXCLUSIVE_WAITERS:
1121 db_printf("exclusive\n");
1123 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1124 db_printf("exclusive and shared\n");
1127 db_printf("none\n");
1132 * Check to see if a thread that is blocked on a sleep queue is actually
1133 * blocked on an sx lock. If so, output some details and return true.
1134 * If the lock has an exclusive owner, return that in *ownerp.
1137 sx_chain(struct thread *td, struct thread **ownerp)
1142 * Check to see if this thread is blocked on an sx lock.
1143 * First, we check the lock class. If that is ok, then we
1144 * compare the lock name against the wait message.
1147 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1148 sx->lock_object.lo_name != td->td_wmesg)
1151 /* We think we have an sx lock, so output some details. */
1152 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1153 *ownerp = sx_xholder(sx);
1154 if (sx->sx_lock & SX_LOCK_SHARED)
1155 db_printf("SLOCK (count %ju)\n",
1156 (uintmax_t)SX_SHARERS(sx->sx_lock));
1158 db_printf("XLOCK\n");