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 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
83 * drop Giant anytime we have to sleep or if we adaptively spin.
85 #define GIANT_DECLARE \
87 WITNESS_SAVE_DECL(Giant) \
89 #define GIANT_SAVE() do { \
90 if (mtx_owned(&Giant)) { \
91 WITNESS_SAVE(&Giant.lock_object, Giant); \
92 while (mtx_owned(&Giant)) { \
99 #define GIANT_RESTORE() do { \
100 if (_giantcnt > 0) { \
101 mtx_assert(&Giant, MA_NOTOWNED); \
102 while (_giantcnt--) \
104 WITNESS_RESTORE(&Giant.lock_object, Giant); \
109 * Returns true if an exclusive lock is recursed. It assumes
110 * curthread currently has an exclusive lock.
112 #define sx_recurse lock_object.lo_data
113 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
115 static void assert_sx(const struct lock_object *lock, int what);
117 static void db_show_sx(const struct lock_object *lock);
119 static void lock_sx(struct lock_object *lock, uintptr_t how);
121 static int owner_sx(const struct lock_object *lock, struct thread **owner);
123 static uintptr_t unlock_sx(struct lock_object *lock);
125 struct lock_class lock_class_sx = {
127 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
128 .lc_assert = assert_sx,
130 .lc_ddb_show = db_show_sx,
133 .lc_unlock = unlock_sx,
135 .lc_owner = owner_sx,
140 #define _sx_assert(sx, what, file, line)
144 static u_int asx_retries = 10;
145 static u_int asx_loops = 10000;
146 static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
147 SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
148 SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
152 assert_sx(const struct lock_object *lock, int what)
155 sx_assert((const struct sx *)lock, what);
159 lock_sx(struct lock_object *lock, uintptr_t how)
163 sx = (struct sx *)lock;
171 unlock_sx(struct lock_object *lock)
175 sx = (struct sx *)lock;
176 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
177 if (sx_xlocked(sx)) {
188 owner_sx(const struct lock_object *lock, struct thread **owner)
190 const struct sx *sx = (const struct sx *)lock;
191 uintptr_t x = sx->sx_lock;
193 *owner = (struct thread *)SX_OWNER(x);
194 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
200 sx_sysinit(void *arg)
202 struct sx_args *sargs = arg;
204 sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
208 sx_init_flags(struct sx *sx, const char *description, int opts)
212 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
213 SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
214 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
215 ("%s: sx_lock not aligned for %s: %p", __func__, description,
218 flags = LO_SLEEPABLE | LO_UPGRADABLE;
221 if (opts & SX_NOPROFILE)
222 flags |= LO_NOPROFILE;
223 if (!(opts & SX_NOWITNESS))
225 if (opts & SX_RECURSE)
226 flags |= LO_RECURSABLE;
230 flags |= opts & SX_NOADAPTIVE;
231 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
232 sx->sx_lock = SX_LOCK_UNLOCKED;
237 sx_destroy(struct sx *sx)
240 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
241 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
242 sx->sx_lock = SX_LOCK_DESTROYED;
243 lock_destroy(&sx->lock_object);
247 _sx_slock(struct sx *sx, int opts, const char *file, int line)
251 if (SCHEDULER_STOPPED())
253 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
254 ("sx_slock() by idle thread %p on sx %s @ %s:%d",
255 curthread, sx->lock_object.lo_name, file, line));
256 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
257 ("sx_slock() of destroyed sx @ %s:%d", file, line));
258 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
259 error = __sx_slock(sx, opts, file, line);
261 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
262 WITNESS_LOCK(&sx->lock_object, 0, file, line);
263 curthread->td_locks++;
270 sx_try_slock_(struct sx *sx, const char *file, int line)
274 if (SCHEDULER_STOPPED())
277 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
278 ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
279 curthread, sx->lock_object.lo_name, file, line));
283 KASSERT(x != SX_LOCK_DESTROYED,
284 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
285 if (!(x & SX_LOCK_SHARED))
287 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
288 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
289 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
290 curthread->td_locks++;
295 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
300 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
304 if (SCHEDULER_STOPPED())
306 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
307 ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
308 curthread, sx->lock_object.lo_name, file, line));
309 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
310 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
311 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
313 error = __sx_xlock(sx, curthread, opts, file, line);
315 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
317 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
318 curthread->td_locks++;
325 sx_try_xlock_(struct sx *sx, const char *file, int line)
329 if (SCHEDULER_STOPPED())
332 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
333 ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
334 curthread, sx->lock_object.lo_name, file, line));
335 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
336 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
338 if (sx_xlocked(sx) &&
339 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
341 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
344 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
345 (uintptr_t)curthread);
346 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
348 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
350 curthread->td_locks++;
357 _sx_sunlock(struct sx *sx, const char *file, int line)
360 if (SCHEDULER_STOPPED())
362 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
363 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
364 _sx_assert(sx, SA_SLOCKED, file, line);
365 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
366 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
367 __sx_sunlock(sx, file, line);
368 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_SUNLOCK_RELEASE, sx);
369 curthread->td_locks--;
373 _sx_xunlock(struct sx *sx, const char *file, int line)
376 if (SCHEDULER_STOPPED())
378 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
379 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
380 _sx_assert(sx, SA_XLOCKED, file, line);
381 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
382 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
384 if (!sx_recursed(sx))
385 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_SX_XUNLOCK_RELEASE, sx);
386 __sx_xunlock(sx, curthread, file, line);
387 curthread->td_locks--;
391 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
392 * This will only succeed if this thread holds a single shared lock.
393 * Return 1 if if the upgrade succeed, 0 otherwise.
396 sx_try_upgrade_(struct sx *sx, const char *file, int line)
401 if (SCHEDULER_STOPPED())
404 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
405 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
406 _sx_assert(sx, SA_SLOCKED, file, line);
409 * Try to switch from one shared lock to an exclusive lock. We need
410 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
411 * we will wake up the exclusive waiters when we drop the lock.
413 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
414 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
415 (uintptr_t)curthread | x);
416 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
418 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
420 LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
426 * Downgrade an unrecursed exclusive lock into a single shared lock.
429 sx_downgrade_(struct sx *sx, const char *file, int line)
434 if (SCHEDULER_STOPPED())
437 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
438 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
439 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
442 panic("downgrade of a recursed lock");
445 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
448 * Try to switch from an exclusive lock with no shared waiters
449 * to one sharer with no shared waiters. If there are
450 * exclusive waiters, we don't need to lock the sleep queue so
451 * long as we preserve the flag. We do one quick try and if
452 * that fails we grab the sleepq lock to keep the flags from
453 * changing and do it the slow way.
455 * We have to lock the sleep queue if there are shared waiters
456 * so we can wake them up.
459 if (!(x & SX_LOCK_SHARED_WAITERS) &&
460 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
461 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
462 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
467 * Lock the sleep queue so we can read the waiters bits
468 * without any races and wakeup any shared waiters.
470 sleepq_lock(&sx->lock_object);
473 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
474 * shared lock. If there are any shared waiters, wake them up.
478 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
479 (x & SX_LOCK_EXCLUSIVE_WAITERS));
480 if (x & SX_LOCK_SHARED_WAITERS)
481 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
483 sleepq_release(&sx->lock_object);
485 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
486 LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
493 * This function represents the so-called 'hard case' for sx_xlock
494 * operation. All 'easy case' failures are redirected to this. Note
495 * that ideally this would be a static function, but it needs to be
496 * accessible from at least sx.h.
499 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
504 volatile struct thread *owner;
505 u_int i, spintries = 0;
508 #ifdef LOCK_PROFILING
509 uint64_t waittime = 0;
514 uint64_t spin_cnt = 0;
515 uint64_t sleep_cnt = 0;
516 int64_t sleep_time = 0;
519 if (SCHEDULER_STOPPED())
522 /* If we already hold an exclusive lock, then recurse. */
523 if (sx_xlocked(sx)) {
524 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
525 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
526 sx->lock_object.lo_name, file, line));
528 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
529 if (LOCK_LOG_TEST(&sx->lock_object, 0))
530 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
534 if (LOCK_LOG_TEST(&sx->lock_object, 0))
535 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
536 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
538 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
543 PMC_SOFT_CALL( , , lock, failed);
545 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
549 * If the lock is write locked and the owner is
550 * running on another CPU, spin until the owner stops
551 * running or the state of the lock changes.
554 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
555 if ((x & SX_LOCK_SHARED) == 0) {
557 owner = (struct thread *)x;
558 if (TD_IS_RUNNING(owner)) {
559 if (LOCK_LOG_TEST(&sx->lock_object, 0))
561 "%s: spinning on %p held by %p",
562 __func__, sx, owner);
564 while (SX_OWNER(sx->sx_lock) == x &&
565 TD_IS_RUNNING(owner)) {
573 } else if (SX_SHARERS(x) && spintries < asx_retries) {
576 for (i = 0; i < asx_loops; i++) {
577 if (LOCK_LOG_TEST(&sx->lock_object, 0))
579 "%s: shared spinning on %p with %u and %u",
580 __func__, sx, spintries, i);
582 if ((x & SX_LOCK_SHARED) == 0 ||
596 sleepq_lock(&sx->lock_object);
600 * If the lock was released while spinning on the
601 * sleep queue chain lock, try again.
603 if (x == SX_LOCK_UNLOCKED) {
604 sleepq_release(&sx->lock_object);
610 * The current lock owner might have started executing
611 * on another CPU (or the lock could have changed
612 * owners) while we were waiting on the sleep queue
613 * chain lock. If so, drop the sleep queue lock and try
616 if (!(x & SX_LOCK_SHARED) &&
617 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
618 owner = (struct thread *)SX_OWNER(x);
619 if (TD_IS_RUNNING(owner)) {
620 sleepq_release(&sx->lock_object);
627 * If an exclusive lock was released with both shared
628 * and exclusive waiters and a shared waiter hasn't
629 * woken up and acquired the lock yet, sx_lock will be
630 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
631 * If we see that value, try to acquire it once. Note
632 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
633 * as there are other exclusive waiters still. If we
634 * fail, restart the loop.
636 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
637 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
638 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
639 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
640 sleepq_release(&sx->lock_object);
641 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
645 sleepq_release(&sx->lock_object);
650 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
651 * than loop back and retry.
653 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
654 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
655 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
656 sleepq_release(&sx->lock_object);
659 if (LOCK_LOG_TEST(&sx->lock_object, 0))
660 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
665 * Since we have been unable to acquire the exclusive
666 * lock and the exclusive waiters flag is set, we have
669 if (LOCK_LOG_TEST(&sx->lock_object, 0))
670 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
674 sleep_time -= lockstat_nsecs();
677 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
678 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
679 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
680 if (!(opts & SX_INTERRUPTIBLE))
681 sleepq_wait(&sx->lock_object, 0);
683 error = sleepq_wait_sig(&sx->lock_object, 0);
685 sleep_time += lockstat_nsecs();
689 if (LOCK_LOG_TEST(&sx->lock_object, 0))
691 "%s: interruptible sleep by %p suspended by signal",
695 if (LOCK_LOG_TEST(&sx->lock_object, 0))
696 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
702 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
703 contested, waittime, file, line);
706 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
707 if (spin_cnt > sleep_cnt)
708 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
714 * This function represents the so-called 'hard case' for sx_xunlock
715 * operation. All 'easy case' failures are redirected to this. Note
716 * that ideally this would be a static function, but it needs to be
717 * accessible from at least sx.h.
720 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
723 int queue, wakeup_swapper;
725 if (SCHEDULER_STOPPED())
728 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
730 /* If the lock is recursed, then unrecurse one level. */
731 if (sx_xlocked(sx) && sx_recursed(sx)) {
732 if ((--sx->sx_recurse) == 0)
733 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
734 if (LOCK_LOG_TEST(&sx->lock_object, 0))
735 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
738 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
739 SX_LOCK_EXCLUSIVE_WAITERS));
740 if (LOCK_LOG_TEST(&sx->lock_object, 0))
741 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
743 sleepq_lock(&sx->lock_object);
744 x = SX_LOCK_UNLOCKED;
747 * The wake up algorithm here is quite simple and probably not
748 * ideal. It gives precedence to shared waiters if they are
749 * present. For this condition, we have to preserve the
750 * state of the exclusive waiters flag.
751 * If interruptible sleeps left the shared queue empty avoid a
752 * starvation for the threads sleeping on the exclusive queue by giving
753 * them precedence and cleaning up the shared waiters bit anyway.
755 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
756 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
757 queue = SQ_SHARED_QUEUE;
758 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
760 queue = SQ_EXCLUSIVE_QUEUE;
762 /* Wake up all the waiters for the specific queue. */
763 if (LOCK_LOG_TEST(&sx->lock_object, 0))
764 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
765 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
767 atomic_store_rel_ptr(&sx->sx_lock, x);
768 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
770 sleepq_release(&sx->lock_object);
776 * This function represents the so-called 'hard case' for sx_slock
777 * operation. All 'easy case' failures are redirected to this. Note
778 * that ideally this would be a static function, but it needs to be
779 * accessible from at least sx.h.
782 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
786 volatile struct thread *owner;
788 #ifdef LOCK_PROFILING
789 uint64_t waittime = 0;
795 uint64_t spin_cnt = 0;
796 uint64_t sleep_cnt = 0;
797 int64_t sleep_time = 0;
800 if (SCHEDULER_STOPPED())
804 * As with rwlocks, we don't make any attempt to try to block
805 * shared locks once there is an exclusive waiter.
814 * If no other thread has an exclusive lock then try to bump up
815 * the count of sharers. Since we have to preserve the state
816 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
817 * shared lock loop back and retry.
819 if (x & SX_LOCK_SHARED) {
820 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
821 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
822 x + SX_ONE_SHARER)) {
823 if (LOCK_LOG_TEST(&sx->lock_object, 0))
825 "%s: %p succeed %p -> %p", __func__,
827 (void *)(x + SX_ONE_SHARER));
833 PMC_SOFT_CALL( , , lock, failed);
835 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
840 * If the owner is running on another CPU, spin until
841 * the owner stops running or the state of the lock
844 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
846 owner = (struct thread *)x;
847 if (TD_IS_RUNNING(owner)) {
848 if (LOCK_LOG_TEST(&sx->lock_object, 0))
850 "%s: spinning on %p held by %p",
851 __func__, sx, owner);
853 while (SX_OWNER(sx->sx_lock) == x &&
854 TD_IS_RUNNING(owner)) {
866 * Some other thread already has an exclusive lock, so
867 * start the process of blocking.
869 sleepq_lock(&sx->lock_object);
873 * The lock could have been released while we spun.
874 * In this case loop back and retry.
876 if (x & SX_LOCK_SHARED) {
877 sleepq_release(&sx->lock_object);
883 * If the owner is running on another CPU, spin until
884 * the owner stops running or the state of the lock
887 if (!(x & SX_LOCK_SHARED) &&
888 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
889 owner = (struct thread *)SX_OWNER(x);
890 if (TD_IS_RUNNING(owner)) {
891 sleepq_release(&sx->lock_object);
898 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
899 * fail to set it drop the sleep queue lock and loop
902 if (!(x & SX_LOCK_SHARED_WAITERS)) {
903 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
904 x | SX_LOCK_SHARED_WAITERS)) {
905 sleepq_release(&sx->lock_object);
908 if (LOCK_LOG_TEST(&sx->lock_object, 0))
909 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
914 * Since we have been unable to acquire the shared lock,
917 if (LOCK_LOG_TEST(&sx->lock_object, 0))
918 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
922 sleep_time -= lockstat_nsecs();
925 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
926 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
927 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
928 if (!(opts & SX_INTERRUPTIBLE))
929 sleepq_wait(&sx->lock_object, 0);
931 error = sleepq_wait_sig(&sx->lock_object, 0);
933 sleep_time += lockstat_nsecs();
937 if (LOCK_LOG_TEST(&sx->lock_object, 0))
939 "%s: interruptible sleep by %p suspended by signal",
943 if (LOCK_LOG_TEST(&sx->lock_object, 0))
944 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
948 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
949 contested, waittime, file, line);
952 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
953 if (spin_cnt > sleep_cnt)
954 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
961 * This function represents the so-called 'hard case' for sx_sunlock
962 * operation. All 'easy case' failures are redirected to this. Note
963 * that ideally this would be a static function, but it needs to be
964 * accessible from at least sx.h.
967 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
972 if (SCHEDULER_STOPPED())
979 * We should never have sharers while at least one thread
980 * holds a shared lock.
982 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
983 ("%s: waiting sharers", __func__));
986 * See if there is more than one shared lock held. If
987 * so, just drop one and return.
989 if (SX_SHARERS(x) > 1) {
990 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
991 x - SX_ONE_SHARER)) {
992 if (LOCK_LOG_TEST(&sx->lock_object, 0))
994 "%s: %p succeeded %p -> %p",
995 __func__, sx, (void *)x,
996 (void *)(x - SX_ONE_SHARER));
1003 * If there aren't any waiters for an exclusive lock,
1004 * then try to drop it quickly.
1006 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
1007 MPASS(x == SX_SHARERS_LOCK(1));
1008 if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1009 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
1010 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1011 CTR2(KTR_LOCK, "%s: %p last succeeded",
1019 * At this point, there should just be one sharer with
1020 * exclusive waiters.
1022 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
1024 sleepq_lock(&sx->lock_object);
1027 * Wake up semantic here is quite simple:
1028 * Just wake up all the exclusive waiters.
1029 * Note that the state of the lock could have changed,
1030 * so if it fails loop back and retry.
1032 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
1033 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
1034 SX_LOCK_UNLOCKED)) {
1035 sleepq_release(&sx->lock_object);
1038 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1039 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
1040 "exclusive queue", __func__, sx);
1041 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1042 0, SQ_EXCLUSIVE_QUEUE);
1043 sleepq_release(&sx->lock_object);
1050 #ifdef INVARIANT_SUPPORT
1056 * In the non-WITNESS case, sx_assert() can only detect that at least
1057 * *some* thread owns an slock, but it cannot guarantee that *this*
1058 * thread owns an slock.
1061 _sx_assert(const struct sx *sx, int what, const char *file, int line)
1067 if (panicstr != NULL)
1071 case SA_SLOCKED | SA_NOTRECURSED:
1072 case SA_SLOCKED | SA_RECURSED:
1078 case SA_LOCKED | SA_NOTRECURSED:
1079 case SA_LOCKED | SA_RECURSED:
1081 witness_assert(&sx->lock_object, what, file, line);
1084 * If some other thread has an exclusive lock or we
1085 * have one and are asserting a shared lock, fail.
1086 * Also, if no one has a lock at all, fail.
1088 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1089 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1090 sx_xholder(sx) != curthread)))
1091 panic("Lock %s not %slocked @ %s:%d\n",
1092 sx->lock_object.lo_name, slocked ? "share " : "",
1095 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1096 if (sx_recursed(sx)) {
1097 if (what & SA_NOTRECURSED)
1098 panic("Lock %s recursed @ %s:%d\n",
1099 sx->lock_object.lo_name, file,
1101 } else if (what & SA_RECURSED)
1102 panic("Lock %s not recursed @ %s:%d\n",
1103 sx->lock_object.lo_name, file, line);
1108 case SA_XLOCKED | SA_NOTRECURSED:
1109 case SA_XLOCKED | SA_RECURSED:
1110 if (sx_xholder(sx) != curthread)
1111 panic("Lock %s not exclusively locked @ %s:%d\n",
1112 sx->lock_object.lo_name, file, line);
1113 if (sx_recursed(sx)) {
1114 if (what & SA_NOTRECURSED)
1115 panic("Lock %s recursed @ %s:%d\n",
1116 sx->lock_object.lo_name, file, line);
1117 } else if (what & SA_RECURSED)
1118 panic("Lock %s not recursed @ %s:%d\n",
1119 sx->lock_object.lo_name, file, line);
1123 witness_assert(&sx->lock_object, what, file, line);
1126 * If we hold an exclusve lock fail. We can't
1127 * reliably check to see if we hold a shared lock or
1130 if (sx_xholder(sx) == curthread)
1131 panic("Lock %s exclusively locked @ %s:%d\n",
1132 sx->lock_object.lo_name, file, line);
1136 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1140 #endif /* INVARIANT_SUPPORT */
1144 db_show_sx(const struct lock_object *lock)
1147 const struct sx *sx;
1149 sx = (const struct sx *)lock;
1151 db_printf(" state: ");
1152 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1153 db_printf("UNLOCKED\n");
1154 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1155 db_printf("DESTROYED\n");
1157 } else if (sx->sx_lock & SX_LOCK_SHARED)
1158 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1160 td = sx_xholder(sx);
1161 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1162 td->td_tid, td->td_proc->p_pid, td->td_name);
1163 if (sx_recursed(sx))
1164 db_printf(" recursed: %d\n", sx->sx_recurse);
1167 db_printf(" waiters: ");
1168 switch(sx->sx_lock &
1169 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1170 case SX_LOCK_SHARED_WAITERS:
1171 db_printf("shared\n");
1173 case SX_LOCK_EXCLUSIVE_WAITERS:
1174 db_printf("exclusive\n");
1176 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1177 db_printf("exclusive and shared\n");
1180 db_printf("none\n");
1185 * Check to see if a thread that is blocked on a sleep queue is actually
1186 * blocked on an sx lock. If so, output some details and return true.
1187 * If the lock has an exclusive owner, return that in *ownerp.
1190 sx_chain(struct thread *td, struct thread **ownerp)
1195 * Check to see if this thread is blocked on an sx lock.
1196 * First, we check the lock class. If that is ok, then we
1197 * compare the lock name against the wait message.
1200 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1201 sx->lock_object.lo_name != td->td_wmesg)
1204 /* We think we have an sx lock, so output some details. */
1205 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1206 *ownerp = sx_xholder(sx);
1207 if (sx->sx_lock & SX_LOCK_SHARED)
1208 db_printf("SLOCK (count %ju)\n",
1209 (uintmax_t)SX_SHARERS(sx->sx_lock));
1211 db_printf("XLOCK\n");