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_no_adaptive_sx.h"
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
51 #include <sys/mutex.h>
53 #include <sys/sched.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_recursed(sx) ((sx)->sx_recurse != 0)
114 static void assert_sx(const struct lock_object *lock, int what);
116 static void db_show_sx(const struct lock_object *lock);
118 static void lock_sx(struct lock_object *lock, uintptr_t how);
120 static int owner_sx(const struct lock_object *lock, struct thread **owner);
122 static uintptr_t unlock_sx(struct lock_object *lock);
124 struct lock_class lock_class_sx = {
126 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
127 .lc_assert = assert_sx,
129 .lc_ddb_show = db_show_sx,
132 .lc_unlock = unlock_sx,
134 .lc_owner = owner_sx,
139 #define _sx_assert(sx, what, file, line)
143 static u_int asx_retries = 10;
144 static u_int asx_loops = 10000;
145 static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD, NULL, "sxlock debugging");
146 SYSCTL_UINT(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
147 SYSCTL_UINT(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
151 assert_sx(const struct lock_object *lock, int what)
154 sx_assert((const struct sx *)lock, what);
158 lock_sx(struct lock_object *lock, uintptr_t how)
162 sx = (struct sx *)lock;
170 unlock_sx(struct lock_object *lock)
174 sx = (struct sx *)lock;
175 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
176 if (sx_xlocked(sx)) {
187 owner_sx(const struct lock_object *lock, struct thread **owner)
189 const struct sx *sx = (const struct sx *)lock;
190 uintptr_t x = sx->sx_lock;
192 *owner = (struct thread *)SX_OWNER(x);
193 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
199 sx_sysinit(void *arg)
201 struct sx_args *sargs = arg;
203 sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
207 sx_init_flags(struct sx *sx, const char *description, int opts)
211 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
212 SX_NOPROFILE | SX_NOADAPTIVE)) == 0);
213 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
214 ("%s: sx_lock not aligned for %s: %p", __func__, description,
217 flags = LO_SLEEPABLE | LO_UPGRADABLE;
220 if (opts & SX_NOPROFILE)
221 flags |= LO_NOPROFILE;
222 if (!(opts & SX_NOWITNESS))
224 if (opts & SX_RECURSE)
225 flags |= LO_RECURSABLE;
229 flags |= opts & SX_NOADAPTIVE;
230 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
231 sx->sx_lock = SX_LOCK_UNLOCKED;
236 sx_destroy(struct sx *sx)
239 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
240 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
241 sx->sx_lock = SX_LOCK_DESTROYED;
242 lock_destroy(&sx->lock_object);
246 _sx_slock(struct sx *sx, int opts, const char *file, int line)
250 if (SCHEDULER_STOPPED())
252 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
253 ("sx_slock() by idle thread %p on sx %s @ %s:%d",
254 curthread, sx->lock_object.lo_name, file, line));
255 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
256 ("sx_slock() of destroyed sx @ %s:%d", file, line));
257 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
258 error = __sx_slock(sx, opts, file, line);
260 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
261 WITNESS_LOCK(&sx->lock_object, 0, file, line);
262 curthread->td_locks++;
269 sx_try_slock_(struct sx *sx, const char *file, int line)
273 if (SCHEDULER_STOPPED())
276 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
277 ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
278 curthread, sx->lock_object.lo_name, file, line));
282 KASSERT(x != SX_LOCK_DESTROYED,
283 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
284 if (!(x & SX_LOCK_SHARED))
286 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
287 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
288 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
289 curthread->td_locks++;
294 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
299 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
303 if (SCHEDULER_STOPPED())
305 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
306 ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
307 curthread, sx->lock_object.lo_name, file, line));
308 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
309 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
310 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
312 error = __sx_xlock(sx, curthread, opts, file, line);
314 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
316 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
317 curthread->td_locks++;
324 sx_try_xlock_(struct sx *sx, const char *file, int line)
328 if (SCHEDULER_STOPPED())
331 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
332 ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
333 curthread, sx->lock_object.lo_name, file, line));
334 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
335 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
337 if (sx_xlocked(sx) &&
338 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
340 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
343 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
344 (uintptr_t)curthread);
345 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
347 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
349 curthread->td_locks++;
356 _sx_sunlock(struct sx *sx, const char *file, int line)
359 if (SCHEDULER_STOPPED())
361 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
362 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
363 _sx_assert(sx, SA_SLOCKED, file, line);
364 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
365 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
366 __sx_sunlock(sx, file, line);
367 curthread->td_locks--;
371 _sx_xunlock(struct sx *sx, const char *file, int line)
374 if (SCHEDULER_STOPPED())
376 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
377 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
378 _sx_assert(sx, SA_XLOCKED, file, line);
379 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
380 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
382 __sx_xunlock(sx, curthread, file, line);
383 curthread->td_locks--;
387 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
388 * This will only succeed if this thread holds a single shared lock.
389 * Return 1 if if the upgrade succeed, 0 otherwise.
392 sx_try_upgrade_(struct sx *sx, const char *file, int line)
397 if (SCHEDULER_STOPPED())
400 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
401 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
402 _sx_assert(sx, SA_SLOCKED, file, line);
405 * Try to switch from one shared lock to an exclusive lock. We need
406 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
407 * we will wake up the exclusive waiters when we drop the lock.
409 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
410 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
411 (uintptr_t)curthread | x);
412 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
414 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
416 LOCKSTAT_RECORD0(LS_SX_TRYUPGRADE_UPGRADE, sx);
422 * Downgrade an unrecursed exclusive lock into a single shared lock.
425 sx_downgrade_(struct sx *sx, const char *file, int line)
430 if (SCHEDULER_STOPPED())
433 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
434 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
435 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
438 panic("downgrade of a recursed lock");
441 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
444 * Try to switch from an exclusive lock with no shared waiters
445 * to one sharer with no shared waiters. If there are
446 * exclusive waiters, we don't need to lock the sleep queue so
447 * long as we preserve the flag. We do one quick try and if
448 * that fails we grab the sleepq lock to keep the flags from
449 * changing and do it the slow way.
451 * We have to lock the sleep queue if there are shared waiters
452 * so we can wake them up.
455 if (!(x & SX_LOCK_SHARED_WAITERS) &&
456 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
457 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
458 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
463 * Lock the sleep queue so we can read the waiters bits
464 * without any races and wakeup any shared waiters.
466 sleepq_lock(&sx->lock_object);
469 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
470 * shared lock. If there are any shared waiters, wake them up.
474 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
475 (x & SX_LOCK_EXCLUSIVE_WAITERS));
476 if (x & SX_LOCK_SHARED_WAITERS)
477 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
479 sleepq_release(&sx->lock_object);
481 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
482 LOCKSTAT_RECORD0(LS_SX_DOWNGRADE_DOWNGRADE, sx);
489 * This function represents the so-called 'hard case' for sx_xlock
490 * operation. All 'easy case' failures are redirected to this. Note
491 * that ideally this would be a static function, but it needs to be
492 * accessible from at least sx.h.
495 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
500 volatile struct thread *owner;
501 u_int i, spintries = 0;
504 #ifdef LOCK_PROFILING
505 uint64_t waittime = 0;
510 uint64_t spin_cnt = 0;
511 uint64_t sleep_cnt = 0;
512 int64_t sleep_time = 0;
515 if (SCHEDULER_STOPPED())
518 /* If we already hold an exclusive lock, then recurse. */
519 if (sx_xlocked(sx)) {
520 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
521 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
522 sx->lock_object.lo_name, file, line));
524 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
525 if (LOCK_LOG_TEST(&sx->lock_object, 0))
526 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
530 if (LOCK_LOG_TEST(&sx->lock_object, 0))
531 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
532 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
534 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
539 PMC_SOFT_CALL( , , lock, failed);
541 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
545 * If the lock is write locked and the owner is
546 * running on another CPU, spin until the owner stops
547 * running or the state of the lock changes.
550 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
551 if ((x & SX_LOCK_SHARED) == 0) {
553 owner = (struct thread *)x;
554 if (TD_IS_RUNNING(owner)) {
555 if (LOCK_LOG_TEST(&sx->lock_object, 0))
557 "%s: spinning on %p held by %p",
558 __func__, sx, owner);
559 KTR_STATE1(KTR_SCHED, "thread",
560 sched_tdname(curthread), "spinning",
562 sx->lock_object.lo_name);
564 while (SX_OWNER(sx->sx_lock) == x &&
565 TD_IS_RUNNING(owner)) {
571 KTR_STATE0(KTR_SCHED, "thread",
572 sched_tdname(curthread), "running");
575 } else if (SX_SHARERS(x) && spintries < asx_retries) {
576 KTR_STATE1(KTR_SCHED, "thread",
577 sched_tdname(curthread), "spinning",
578 "lockname:\"%s\"", sx->lock_object.lo_name);
581 for (i = 0; i < asx_loops; i++) {
582 if (LOCK_LOG_TEST(&sx->lock_object, 0))
584 "%s: shared spinning on %p with %u and %u",
585 __func__, sx, spintries, i);
587 if ((x & SX_LOCK_SHARED) == 0 ||
595 KTR_STATE0(KTR_SCHED, "thread",
596 sched_tdname(curthread), "running");
603 sleepq_lock(&sx->lock_object);
607 * If the lock was released while spinning on the
608 * sleep queue chain lock, try again.
610 if (x == SX_LOCK_UNLOCKED) {
611 sleepq_release(&sx->lock_object);
617 * The current lock owner might have started executing
618 * on another CPU (or the lock could have changed
619 * owners) while we were waiting on the sleep queue
620 * chain lock. If so, drop the sleep queue lock and try
623 if (!(x & SX_LOCK_SHARED) &&
624 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
625 owner = (struct thread *)SX_OWNER(x);
626 if (TD_IS_RUNNING(owner)) {
627 sleepq_release(&sx->lock_object);
634 * If an exclusive lock was released with both shared
635 * and exclusive waiters and a shared waiter hasn't
636 * woken up and acquired the lock yet, sx_lock will be
637 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
638 * If we see that value, try to acquire it once. Note
639 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
640 * as there are other exclusive waiters still. If we
641 * fail, restart the loop.
643 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
644 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
645 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
646 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
647 sleepq_release(&sx->lock_object);
648 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
652 sleepq_release(&sx->lock_object);
657 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
658 * than loop back and retry.
660 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
661 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
662 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
663 sleepq_release(&sx->lock_object);
666 if (LOCK_LOG_TEST(&sx->lock_object, 0))
667 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
672 * Since we have been unable to acquire the exclusive
673 * lock and the exclusive waiters flag is set, we have
676 if (LOCK_LOG_TEST(&sx->lock_object, 0))
677 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
681 sleep_time -= lockstat_nsecs();
684 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
685 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
686 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
687 if (!(opts & SX_INTERRUPTIBLE))
688 sleepq_wait(&sx->lock_object, 0);
690 error = sleepq_wait_sig(&sx->lock_object, 0);
692 sleep_time += lockstat_nsecs();
696 if (LOCK_LOG_TEST(&sx->lock_object, 0))
698 "%s: interruptible sleep by %p suspended by signal",
702 if (LOCK_LOG_TEST(&sx->lock_object, 0))
703 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
709 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_XLOCK_ACQUIRE, sx,
710 contested, waittime, file, line);
713 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
714 if (spin_cnt > sleep_cnt)
715 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
721 * This function represents the so-called 'hard case' for sx_xunlock
722 * operation. All 'easy case' failures are redirected to this. Note
723 * that ideally this would be a static function, but it needs to be
724 * accessible from at least sx.h.
727 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
730 int queue, wakeup_swapper;
732 if (SCHEDULER_STOPPED())
735 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
737 /* If the lock is recursed, then unrecurse one level. */
738 if (sx_xlocked(sx) && sx_recursed(sx)) {
739 if ((--sx->sx_recurse) == 0)
740 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
741 if (LOCK_LOG_TEST(&sx->lock_object, 0))
742 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
745 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
746 SX_LOCK_EXCLUSIVE_WAITERS));
747 if (LOCK_LOG_TEST(&sx->lock_object, 0))
748 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
750 sleepq_lock(&sx->lock_object);
751 x = SX_LOCK_UNLOCKED;
754 * The wake up algorithm here is quite simple and probably not
755 * ideal. It gives precedence to shared waiters if they are
756 * present. For this condition, we have to preserve the
757 * state of the exclusive waiters flag.
758 * If interruptible sleeps left the shared queue empty avoid a
759 * starvation for the threads sleeping on the exclusive queue by giving
760 * them precedence and cleaning up the shared waiters bit anyway.
762 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
763 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
764 queue = SQ_SHARED_QUEUE;
765 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
767 queue = SQ_EXCLUSIVE_QUEUE;
769 /* Wake up all the waiters for the specific queue. */
770 if (LOCK_LOG_TEST(&sx->lock_object, 0))
771 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
772 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
774 atomic_store_rel_ptr(&sx->sx_lock, x);
775 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
777 sleepq_release(&sx->lock_object);
783 * This function represents the so-called 'hard case' for sx_slock
784 * operation. All 'easy case' failures are redirected to this. Note
785 * that ideally this would be a static function, but it needs to be
786 * accessible from at least sx.h.
789 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
793 volatile struct thread *owner;
795 #ifdef LOCK_PROFILING
796 uint64_t waittime = 0;
802 uint64_t spin_cnt = 0;
803 uint64_t sleep_cnt = 0;
804 int64_t sleep_time = 0;
807 if (SCHEDULER_STOPPED())
811 * As with rwlocks, we don't make any attempt to try to block
812 * shared locks once there is an exclusive waiter.
821 * If no other thread has an exclusive lock then try to bump up
822 * the count of sharers. Since we have to preserve the state
823 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
824 * shared lock loop back and retry.
826 if (x & SX_LOCK_SHARED) {
827 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
828 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
829 x + SX_ONE_SHARER)) {
830 if (LOCK_LOG_TEST(&sx->lock_object, 0))
832 "%s: %p succeed %p -> %p", __func__,
834 (void *)(x + SX_ONE_SHARER));
840 PMC_SOFT_CALL( , , lock, failed);
842 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
847 * If the owner is running on another CPU, spin until
848 * the owner stops running or the state of the lock
851 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
853 owner = (struct thread *)x;
854 if (TD_IS_RUNNING(owner)) {
855 if (LOCK_LOG_TEST(&sx->lock_object, 0))
857 "%s: spinning on %p held by %p",
858 __func__, sx, owner);
859 KTR_STATE1(KTR_SCHED, "thread",
860 sched_tdname(curthread), "spinning",
861 "lockname:\"%s\"", sx->lock_object.lo_name);
863 while (SX_OWNER(sx->sx_lock) == x &&
864 TD_IS_RUNNING(owner)) {
870 KTR_STATE0(KTR_SCHED, "thread",
871 sched_tdname(curthread), "running");
878 * Some other thread already has an exclusive lock, so
879 * start the process of blocking.
881 sleepq_lock(&sx->lock_object);
885 * The lock could have been released while we spun.
886 * In this case loop back and retry.
888 if (x & SX_LOCK_SHARED) {
889 sleepq_release(&sx->lock_object);
895 * If the owner is running on another CPU, spin until
896 * the owner stops running or the state of the lock
899 if (!(x & SX_LOCK_SHARED) &&
900 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
901 owner = (struct thread *)SX_OWNER(x);
902 if (TD_IS_RUNNING(owner)) {
903 sleepq_release(&sx->lock_object);
910 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
911 * fail to set it drop the sleep queue lock and loop
914 if (!(x & SX_LOCK_SHARED_WAITERS)) {
915 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
916 x | SX_LOCK_SHARED_WAITERS)) {
917 sleepq_release(&sx->lock_object);
920 if (LOCK_LOG_TEST(&sx->lock_object, 0))
921 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
926 * Since we have been unable to acquire the shared lock,
929 if (LOCK_LOG_TEST(&sx->lock_object, 0))
930 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
934 sleep_time -= lockstat_nsecs();
937 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
938 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
939 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
940 if (!(opts & SX_INTERRUPTIBLE))
941 sleepq_wait(&sx->lock_object, 0);
943 error = sleepq_wait_sig(&sx->lock_object, 0);
945 sleep_time += lockstat_nsecs();
949 if (LOCK_LOG_TEST(&sx->lock_object, 0))
951 "%s: interruptible sleep by %p suspended by signal",
955 if (LOCK_LOG_TEST(&sx->lock_object, 0))
956 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
960 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_SX_SLOCK_ACQUIRE, sx,
961 contested, waittime, file, line);
964 LOCKSTAT_RECORD1(LS_SX_XLOCK_BLOCK, sx, sleep_time);
965 if (spin_cnt > sleep_cnt)
966 LOCKSTAT_RECORD1(LS_SX_XLOCK_SPIN, sx, (spin_cnt - sleep_cnt));
973 * This function represents the so-called 'hard case' for sx_sunlock
974 * operation. All 'easy case' failures are redirected to this. Note
975 * that ideally this would be a static function, but it needs to be
976 * accessible from at least sx.h.
979 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
984 if (SCHEDULER_STOPPED())
991 * We should never have sharers while at least one thread
992 * holds a shared lock.
994 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
995 ("%s: waiting sharers", __func__));
998 * See if there is more than one shared lock held. If
999 * so, just drop one and return.
1001 if (SX_SHARERS(x) > 1) {
1002 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
1003 x - SX_ONE_SHARER)) {
1004 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1006 "%s: %p succeeded %p -> %p",
1007 __func__, sx, (void *)x,
1008 (void *)(x - SX_ONE_SHARER));
1015 * If there aren't any waiters for an exclusive lock,
1016 * then try to drop it quickly.
1018 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
1019 MPASS(x == SX_SHARERS_LOCK(1));
1020 if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1021 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
1022 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1023 CTR2(KTR_LOCK, "%s: %p last succeeded",
1031 * At this point, there should just be one sharer with
1032 * exclusive waiters.
1034 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
1036 sleepq_lock(&sx->lock_object);
1039 * Wake up semantic here is quite simple:
1040 * Just wake up all the exclusive waiters.
1041 * Note that the state of the lock could have changed,
1042 * so if it fails loop back and retry.
1044 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
1045 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
1046 SX_LOCK_UNLOCKED)) {
1047 sleepq_release(&sx->lock_object);
1050 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1051 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
1052 "exclusive queue", __func__, sx);
1053 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1054 0, SQ_EXCLUSIVE_QUEUE);
1055 sleepq_release(&sx->lock_object);
1062 #ifdef INVARIANT_SUPPORT
1068 * In the non-WITNESS case, sx_assert() can only detect that at least
1069 * *some* thread owns an slock, but it cannot guarantee that *this*
1070 * thread owns an slock.
1073 _sx_assert(const struct sx *sx, int what, const char *file, int line)
1079 if (panicstr != NULL)
1083 case SA_SLOCKED | SA_NOTRECURSED:
1084 case SA_SLOCKED | SA_RECURSED:
1090 case SA_LOCKED | SA_NOTRECURSED:
1091 case SA_LOCKED | SA_RECURSED:
1093 witness_assert(&sx->lock_object, what, file, line);
1096 * If some other thread has an exclusive lock or we
1097 * have one and are asserting a shared lock, fail.
1098 * Also, if no one has a lock at all, fail.
1100 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1101 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1102 sx_xholder(sx) != curthread)))
1103 panic("Lock %s not %slocked @ %s:%d\n",
1104 sx->lock_object.lo_name, slocked ? "share " : "",
1107 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1108 if (sx_recursed(sx)) {
1109 if (what & SA_NOTRECURSED)
1110 panic("Lock %s recursed @ %s:%d\n",
1111 sx->lock_object.lo_name, file,
1113 } else if (what & SA_RECURSED)
1114 panic("Lock %s not recursed @ %s:%d\n",
1115 sx->lock_object.lo_name, file, line);
1120 case SA_XLOCKED | SA_NOTRECURSED:
1121 case SA_XLOCKED | SA_RECURSED:
1122 if (sx_xholder(sx) != curthread)
1123 panic("Lock %s not exclusively locked @ %s:%d\n",
1124 sx->lock_object.lo_name, file, line);
1125 if (sx_recursed(sx)) {
1126 if (what & SA_NOTRECURSED)
1127 panic("Lock %s recursed @ %s:%d\n",
1128 sx->lock_object.lo_name, file, line);
1129 } else if (what & SA_RECURSED)
1130 panic("Lock %s not recursed @ %s:%d\n",
1131 sx->lock_object.lo_name, file, line);
1135 witness_assert(&sx->lock_object, what, file, line);
1138 * If we hold an exclusve lock fail. We can't
1139 * reliably check to see if we hold a shared lock or
1142 if (sx_xholder(sx) == curthread)
1143 panic("Lock %s exclusively locked @ %s:%d\n",
1144 sx->lock_object.lo_name, file, line);
1148 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1152 #endif /* INVARIANT_SUPPORT */
1156 db_show_sx(const struct lock_object *lock)
1159 const struct sx *sx;
1161 sx = (const struct sx *)lock;
1163 db_printf(" state: ");
1164 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1165 db_printf("UNLOCKED\n");
1166 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1167 db_printf("DESTROYED\n");
1169 } else if (sx->sx_lock & SX_LOCK_SHARED)
1170 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1172 td = sx_xholder(sx);
1173 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1174 td->td_tid, td->td_proc->p_pid, td->td_name);
1175 if (sx_recursed(sx))
1176 db_printf(" recursed: %d\n", sx->sx_recurse);
1179 db_printf(" waiters: ");
1180 switch(sx->sx_lock &
1181 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1182 case SX_LOCK_SHARED_WAITERS:
1183 db_printf("shared\n");
1185 case SX_LOCK_EXCLUSIVE_WAITERS:
1186 db_printf("exclusive\n");
1188 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1189 db_printf("exclusive and shared\n");
1192 db_printf("none\n");
1197 * Check to see if a thread that is blocked on a sleep queue is actually
1198 * blocked on an sx lock. If so, output some details and return true.
1199 * If the lock has an exclusive owner, return that in *ownerp.
1202 sx_chain(struct thread *td, struct thread **ownerp)
1207 * Check to see if this thread is blocked on an sx lock.
1208 * First, we check the lock class. If that is ok, then we
1209 * compare the lock name against the wait message.
1212 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1213 sx->lock_object.lo_name != td->td_wmesg)
1216 /* We think we have an sx lock, so output some details. */
1217 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1218 *ownerp = sx_xholder(sx);
1219 if (sx->sx_lock & SX_LOCK_SHARED)
1220 db_printf("SLOCK (count %ju)\n",
1221 (uintmax_t)SX_SHARERS(sx->sx_lock));
1223 db_printf("XLOCK\n");