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 | SX_NEW)) == 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;
231 flags |= opts & SX_NOADAPTIVE;
232 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
233 sx->sx_lock = SX_LOCK_UNLOCKED;
238 sx_destroy(struct sx *sx)
241 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
242 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
243 sx->sx_lock = SX_LOCK_DESTROYED;
244 lock_destroy(&sx->lock_object);
248 _sx_slock(struct sx *sx, int opts, const char *file, int line)
252 if (SCHEDULER_STOPPED())
254 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
255 ("sx_slock() by idle thread %p on sx %s @ %s:%d",
256 curthread, sx->lock_object.lo_name, file, line));
257 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
258 ("sx_slock() of destroyed sx @ %s:%d", file, line));
259 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
260 error = __sx_slock(sx, opts, file, line);
262 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
263 WITNESS_LOCK(&sx->lock_object, 0, file, line);
264 TD_LOCKS_INC(curthread);
271 sx_try_slock_(struct sx *sx, const char *file, int line)
275 if (SCHEDULER_STOPPED())
278 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
279 ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
280 curthread, sx->lock_object.lo_name, file, line));
284 KASSERT(x != SX_LOCK_DESTROYED,
285 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
286 if (!(x & SX_LOCK_SHARED))
288 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x, x + SX_ONE_SHARER)) {
289 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
290 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
291 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire,
292 sx, 0, 0, file, line, LOCKSTAT_READER);
293 TD_LOCKS_INC(curthread);
298 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
303 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
307 if (SCHEDULER_STOPPED())
309 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
310 ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
311 curthread, sx->lock_object.lo_name, file, line));
312 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
313 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
314 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
316 error = __sx_xlock(sx, curthread, opts, file, line);
318 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
320 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
321 TD_LOCKS_INC(curthread);
328 sx_try_xlock_(struct sx *sx, const char *file, int line)
332 if (SCHEDULER_STOPPED())
335 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
336 ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
337 curthread, sx->lock_object.lo_name, file, line));
338 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
339 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
341 if (sx_xlocked(sx) &&
342 (sx->lock_object.lo_flags & LO_RECURSABLE) != 0) {
344 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
347 rval = atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED,
348 (uintptr_t)curthread);
349 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
351 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
353 if (!sx_recursed(sx))
354 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire,
355 sx, 0, 0, file, line, LOCKSTAT_WRITER);
356 TD_LOCKS_INC(curthread);
363 _sx_sunlock(struct sx *sx, const char *file, int line)
366 if (SCHEDULER_STOPPED())
368 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
369 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
370 _sx_assert(sx, SA_SLOCKED, file, line);
371 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
372 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
373 __sx_sunlock(sx, file, line);
374 TD_LOCKS_DEC(curthread);
378 _sx_xunlock(struct sx *sx, const char *file, int line)
381 if (SCHEDULER_STOPPED())
383 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
384 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
385 _sx_assert(sx, SA_XLOCKED, file, line);
386 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
387 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
389 __sx_xunlock(sx, curthread, file, line);
390 TD_LOCKS_DEC(curthread);
394 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
395 * This will only succeed if this thread holds a single shared lock.
396 * Return 1 if if the upgrade succeed, 0 otherwise.
399 sx_try_upgrade_(struct sx *sx, const char *file, int line)
404 if (SCHEDULER_STOPPED())
407 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
408 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
409 _sx_assert(sx, SA_SLOCKED, file, line);
412 * Try to switch from one shared lock to an exclusive lock. We need
413 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
414 * we will wake up the exclusive waiters when we drop the lock.
416 x = sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS;
417 success = atomic_cmpset_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) | x,
418 (uintptr_t)curthread | x);
419 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
421 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
423 LOCKSTAT_RECORD0(sx__upgrade, sx);
429 * Downgrade an unrecursed exclusive lock into a single shared lock.
432 sx_downgrade_(struct sx *sx, const char *file, int line)
437 if (SCHEDULER_STOPPED())
440 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
441 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
442 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
445 panic("downgrade of a recursed lock");
448 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
451 * Try to switch from an exclusive lock with no shared waiters
452 * to one sharer with no shared waiters. If there are
453 * exclusive waiters, we don't need to lock the sleep queue so
454 * long as we preserve the flag. We do one quick try and if
455 * that fails we grab the sleepq lock to keep the flags from
456 * changing and do it the slow way.
458 * We have to lock the sleep queue if there are shared waiters
459 * so we can wake them up.
462 if (!(x & SX_LOCK_SHARED_WAITERS) &&
463 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
464 (x & SX_LOCK_EXCLUSIVE_WAITERS))) {
465 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
470 * Lock the sleep queue so we can read the waiters bits
471 * without any races and wakeup any shared waiters.
473 sleepq_lock(&sx->lock_object);
476 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
477 * shared lock. If there are any shared waiters, wake them up.
481 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
482 (x & SX_LOCK_EXCLUSIVE_WAITERS));
483 if (x & SX_LOCK_SHARED_WAITERS)
484 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
486 sleepq_release(&sx->lock_object);
488 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
489 LOCKSTAT_RECORD0(sx__downgrade, sx);
496 * This function represents the so-called 'hard case' for sx_xlock
497 * operation. All 'easy case' failures are redirected to this. Note
498 * that ideally this would be a static function, but it needs to be
499 * accessible from at least sx.h.
502 _sx_xlock_hard(struct sx *sx, uintptr_t tid, int opts, const char *file,
507 volatile struct thread *owner;
508 u_int i, spintries = 0;
511 #ifdef LOCK_PROFILING
512 uint64_t waittime = 0;
518 uint64_t spin_cnt = 0;
519 uint64_t sleep_cnt = 0;
520 int64_t sleep_time = 0;
521 int64_t all_time = 0;
524 if (SCHEDULER_STOPPED())
527 /* If we already hold an exclusive lock, then recurse. */
528 if (sx_xlocked(sx)) {
529 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
530 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
531 sx->lock_object.lo_name, file, line));
533 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
534 if (LOCK_LOG_TEST(&sx->lock_object, 0))
535 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
539 if (LOCK_LOG_TEST(&sx->lock_object, 0))
540 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
541 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
544 all_time -= lockstat_nsecs(&sx->lock_object);
547 while (!atomic_cmpset_acq_ptr(&sx->sx_lock, SX_LOCK_UNLOCKED, tid)) {
552 PMC_SOFT_CALL( , , lock, failed);
554 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
558 * If the lock is write locked and the owner is
559 * running on another CPU, spin until the owner stops
560 * running or the state of the lock changes.
563 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
564 if ((x & SX_LOCK_SHARED) == 0) {
566 owner = (struct thread *)x;
567 if (TD_IS_RUNNING(owner)) {
568 if (LOCK_LOG_TEST(&sx->lock_object, 0))
570 "%s: spinning on %p held by %p",
571 __func__, sx, owner);
572 KTR_STATE1(KTR_SCHED, "thread",
573 sched_tdname(curthread), "spinning",
575 sx->lock_object.lo_name);
577 while (SX_OWNER(sx->sx_lock) == x &&
578 TD_IS_RUNNING(owner)) {
584 KTR_STATE0(KTR_SCHED, "thread",
585 sched_tdname(curthread), "running");
588 } else if (SX_SHARERS(x) && spintries < asx_retries) {
589 KTR_STATE1(KTR_SCHED, "thread",
590 sched_tdname(curthread), "spinning",
591 "lockname:\"%s\"", sx->lock_object.lo_name);
594 for (i = 0; i < asx_loops; i++) {
595 if (LOCK_LOG_TEST(&sx->lock_object, 0))
597 "%s: shared spinning on %p with %u and %u",
598 __func__, sx, spintries, i);
600 if ((x & SX_LOCK_SHARED) == 0 ||
608 KTR_STATE0(KTR_SCHED, "thread",
609 sched_tdname(curthread), "running");
616 sleepq_lock(&sx->lock_object);
620 * If the lock was released while spinning on the
621 * sleep queue chain lock, try again.
623 if (x == SX_LOCK_UNLOCKED) {
624 sleepq_release(&sx->lock_object);
630 * The current lock owner might have started executing
631 * on another CPU (or the lock could have changed
632 * owners) while we were waiting on the sleep queue
633 * chain lock. If so, drop the sleep queue lock and try
636 if (!(x & SX_LOCK_SHARED) &&
637 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
638 owner = (struct thread *)SX_OWNER(x);
639 if (TD_IS_RUNNING(owner)) {
640 sleepq_release(&sx->lock_object);
647 * If an exclusive lock was released with both shared
648 * and exclusive waiters and a shared waiter hasn't
649 * woken up and acquired the lock yet, sx_lock will be
650 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
651 * If we see that value, try to acquire it once. Note
652 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
653 * as there are other exclusive waiters still. If we
654 * fail, restart the loop.
656 if (x == (SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS)) {
657 if (atomic_cmpset_acq_ptr(&sx->sx_lock,
658 SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS,
659 tid | SX_LOCK_EXCLUSIVE_WAITERS)) {
660 sleepq_release(&sx->lock_object);
661 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
665 sleepq_release(&sx->lock_object);
670 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
671 * than loop back and retry.
673 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
674 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
675 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
676 sleepq_release(&sx->lock_object);
679 if (LOCK_LOG_TEST(&sx->lock_object, 0))
680 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
685 * Since we have been unable to acquire the exclusive
686 * lock and the exclusive waiters flag is set, we have
689 if (LOCK_LOG_TEST(&sx->lock_object, 0))
690 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
694 sleep_time -= lockstat_nsecs(&sx->lock_object);
697 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
698 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
699 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
700 if (!(opts & SX_INTERRUPTIBLE))
701 sleepq_wait(&sx->lock_object, 0);
703 error = sleepq_wait_sig(&sx->lock_object, 0);
705 sleep_time += lockstat_nsecs(&sx->lock_object);
709 if (LOCK_LOG_TEST(&sx->lock_object, 0))
711 "%s: interruptible sleep by %p suspended by signal",
715 if (LOCK_LOG_TEST(&sx->lock_object, 0))
716 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
720 all_time += lockstat_nsecs(&sx->lock_object);
722 LOCKSTAT_RECORD4(sx__block, sx, sleep_time,
723 LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0,
724 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
725 if (spin_cnt > sleep_cnt)
726 LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time,
727 LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0,
728 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
731 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx,
732 contested, waittime, file, line, LOCKSTAT_WRITER);
738 * This function represents the so-called 'hard case' for sx_xunlock
739 * operation. All 'easy case' failures are redirected to this. Note
740 * that ideally this would be a static function, but it needs to be
741 * accessible from at least sx.h.
744 _sx_xunlock_hard(struct sx *sx, uintptr_t tid, const char *file, int line)
747 int queue, wakeup_swapper;
749 if (SCHEDULER_STOPPED())
752 MPASS(!(sx->sx_lock & SX_LOCK_SHARED));
754 /* If the lock is recursed, then unrecurse one level. */
755 if (sx_xlocked(sx) && sx_recursed(sx)) {
756 if ((--sx->sx_recurse) == 0)
757 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
758 if (LOCK_LOG_TEST(&sx->lock_object, 0))
759 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
762 MPASS(sx->sx_lock & (SX_LOCK_SHARED_WAITERS |
763 SX_LOCK_EXCLUSIVE_WAITERS));
764 if (LOCK_LOG_TEST(&sx->lock_object, 0))
765 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
767 sleepq_lock(&sx->lock_object);
768 x = SX_LOCK_UNLOCKED;
771 * The wake up algorithm here is quite simple and probably not
772 * ideal. It gives precedence to shared waiters if they are
773 * present. For this condition, we have to preserve the
774 * state of the exclusive waiters flag.
775 * If interruptible sleeps left the shared queue empty avoid a
776 * starvation for the threads sleeping on the exclusive queue by giving
777 * them precedence and cleaning up the shared waiters bit anyway.
779 if ((sx->sx_lock & SX_LOCK_SHARED_WAITERS) != 0 &&
780 sleepq_sleepcnt(&sx->lock_object, SQ_SHARED_QUEUE) != 0) {
781 queue = SQ_SHARED_QUEUE;
782 x |= (sx->sx_lock & SX_LOCK_EXCLUSIVE_WAITERS);
784 queue = SQ_EXCLUSIVE_QUEUE;
786 /* Wake up all the waiters for the specific queue. */
787 if (LOCK_LOG_TEST(&sx->lock_object, 0))
788 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
789 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
791 atomic_store_rel_ptr(&sx->sx_lock, x);
792 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
794 sleepq_release(&sx->lock_object);
800 * This function represents the so-called 'hard case' for sx_slock
801 * operation. All 'easy case' failures are redirected to this. Note
802 * that ideally this would be a static function, but it needs to be
803 * accessible from at least sx.h.
806 _sx_slock_hard(struct sx *sx, int opts, const char *file, int line)
810 volatile struct thread *owner;
812 #ifdef LOCK_PROFILING
813 uint64_t waittime = 0;
820 uint64_t spin_cnt = 0;
821 uint64_t sleep_cnt = 0;
822 int64_t sleep_time = 0;
823 int64_t all_time = 0;
826 if (SCHEDULER_STOPPED())
831 all_time -= lockstat_nsecs(&sx->lock_object);
835 * As with rwlocks, we don't make any attempt to try to block
836 * shared locks once there is an exclusive waiter.
845 * If no other thread has an exclusive lock then try to bump up
846 * the count of sharers. Since we have to preserve the state
847 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
848 * shared lock loop back and retry.
850 if (x & SX_LOCK_SHARED) {
851 MPASS(!(x & SX_LOCK_SHARED_WAITERS));
852 if (atomic_cmpset_acq_ptr(&sx->sx_lock, x,
853 x + SX_ONE_SHARER)) {
854 if (LOCK_LOG_TEST(&sx->lock_object, 0))
856 "%s: %p succeed %p -> %p", __func__,
858 (void *)(x + SX_ONE_SHARER));
864 PMC_SOFT_CALL( , , lock, failed);
866 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
871 * If the owner is running on another CPU, spin until
872 * the owner stops running or the state of the lock
875 if ((sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
877 owner = (struct thread *)x;
878 if (TD_IS_RUNNING(owner)) {
879 if (LOCK_LOG_TEST(&sx->lock_object, 0))
881 "%s: spinning on %p held by %p",
882 __func__, sx, owner);
883 KTR_STATE1(KTR_SCHED, "thread",
884 sched_tdname(curthread), "spinning",
885 "lockname:\"%s\"", sx->lock_object.lo_name);
887 while (SX_OWNER(sx->sx_lock) == x &&
888 TD_IS_RUNNING(owner)) {
894 KTR_STATE0(KTR_SCHED, "thread",
895 sched_tdname(curthread), "running");
902 * Some other thread already has an exclusive lock, so
903 * start the process of blocking.
905 sleepq_lock(&sx->lock_object);
909 * The lock could have been released while we spun.
910 * In this case loop back and retry.
912 if (x & SX_LOCK_SHARED) {
913 sleepq_release(&sx->lock_object);
919 * If the owner is running on another CPU, spin until
920 * the owner stops running or the state of the lock
923 if (!(x & SX_LOCK_SHARED) &&
924 (sx->lock_object.lo_flags & SX_NOADAPTIVE) == 0) {
925 owner = (struct thread *)SX_OWNER(x);
926 if (TD_IS_RUNNING(owner)) {
927 sleepq_release(&sx->lock_object);
934 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
935 * fail to set it drop the sleep queue lock and loop
938 if (!(x & SX_LOCK_SHARED_WAITERS)) {
939 if (!atomic_cmpset_ptr(&sx->sx_lock, x,
940 x | SX_LOCK_SHARED_WAITERS)) {
941 sleepq_release(&sx->lock_object);
944 if (LOCK_LOG_TEST(&sx->lock_object, 0))
945 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
950 * Since we have been unable to acquire the shared lock,
953 if (LOCK_LOG_TEST(&sx->lock_object, 0))
954 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
958 sleep_time -= lockstat_nsecs(&sx->lock_object);
961 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
962 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
963 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
964 if (!(opts & SX_INTERRUPTIBLE))
965 sleepq_wait(&sx->lock_object, 0);
967 error = sleepq_wait_sig(&sx->lock_object, 0);
969 sleep_time += lockstat_nsecs(&sx->lock_object);
973 if (LOCK_LOG_TEST(&sx->lock_object, 0))
975 "%s: interruptible sleep by %p suspended by signal",
979 if (LOCK_LOG_TEST(&sx->lock_object, 0))
980 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
984 all_time += lockstat_nsecs(&sx->lock_object);
986 LOCKSTAT_RECORD4(sx__block, sx, sleep_time,
987 LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0,
988 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
989 if (spin_cnt > sleep_cnt)
990 LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time,
991 LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0,
992 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
995 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx,
996 contested, waittime, file, line, LOCKSTAT_READER);
1002 * This function represents the so-called 'hard case' for sx_sunlock
1003 * operation. All 'easy case' failures are redirected to this. Note
1004 * that ideally this would be a static function, but it needs to be
1005 * accessible from at least sx.h.
1008 _sx_sunlock_hard(struct sx *sx, const char *file, int line)
1013 if (SCHEDULER_STOPPED())
1020 * We should never have sharers while at least one thread
1021 * holds a shared lock.
1023 KASSERT(!(x & SX_LOCK_SHARED_WAITERS),
1024 ("%s: waiting sharers", __func__));
1027 * See if there is more than one shared lock held. If
1028 * so, just drop one and return.
1030 if (SX_SHARERS(x) > 1) {
1031 if (atomic_cmpset_rel_ptr(&sx->sx_lock, x,
1032 x - SX_ONE_SHARER)) {
1033 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1035 "%s: %p succeeded %p -> %p",
1036 __func__, sx, (void *)x,
1037 (void *)(x - SX_ONE_SHARER));
1044 * If there aren't any waiters for an exclusive lock,
1045 * then try to drop it quickly.
1047 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
1048 MPASS(x == SX_SHARERS_LOCK(1));
1049 if (atomic_cmpset_rel_ptr(&sx->sx_lock,
1050 SX_SHARERS_LOCK(1), SX_LOCK_UNLOCKED)) {
1051 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1052 CTR2(KTR_LOCK, "%s: %p last succeeded",
1060 * At this point, there should just be one sharer with
1061 * exclusive waiters.
1063 MPASS(x == (SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS));
1065 sleepq_lock(&sx->lock_object);
1068 * Wake up semantic here is quite simple:
1069 * Just wake up all the exclusive waiters.
1070 * Note that the state of the lock could have changed,
1071 * so if it fails loop back and retry.
1073 if (!atomic_cmpset_rel_ptr(&sx->sx_lock,
1074 SX_SHARERS_LOCK(1) | SX_LOCK_EXCLUSIVE_WAITERS,
1075 SX_LOCK_UNLOCKED)) {
1076 sleepq_release(&sx->lock_object);
1079 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1080 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
1081 "exclusive queue", __func__, sx);
1082 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1083 0, SQ_EXCLUSIVE_QUEUE);
1084 sleepq_release(&sx->lock_object);
1091 #ifdef INVARIANT_SUPPORT
1097 * In the non-WITNESS case, sx_assert() can only detect that at least
1098 * *some* thread owns an slock, but it cannot guarantee that *this*
1099 * thread owns an slock.
1102 _sx_assert(const struct sx *sx, int what, const char *file, int line)
1108 if (panicstr != NULL)
1112 case SA_SLOCKED | SA_NOTRECURSED:
1113 case SA_SLOCKED | SA_RECURSED:
1119 case SA_LOCKED | SA_NOTRECURSED:
1120 case SA_LOCKED | SA_RECURSED:
1122 witness_assert(&sx->lock_object, what, file, line);
1125 * If some other thread has an exclusive lock or we
1126 * have one and are asserting a shared lock, fail.
1127 * Also, if no one has a lock at all, fail.
1129 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1130 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1131 sx_xholder(sx) != curthread)))
1132 panic("Lock %s not %slocked @ %s:%d\n",
1133 sx->lock_object.lo_name, slocked ? "share " : "",
1136 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1137 if (sx_recursed(sx)) {
1138 if (what & SA_NOTRECURSED)
1139 panic("Lock %s recursed @ %s:%d\n",
1140 sx->lock_object.lo_name, file,
1142 } else if (what & SA_RECURSED)
1143 panic("Lock %s not recursed @ %s:%d\n",
1144 sx->lock_object.lo_name, file, line);
1149 case SA_XLOCKED | SA_NOTRECURSED:
1150 case SA_XLOCKED | SA_RECURSED:
1151 if (sx_xholder(sx) != curthread)
1152 panic("Lock %s not exclusively locked @ %s:%d\n",
1153 sx->lock_object.lo_name, file, line);
1154 if (sx_recursed(sx)) {
1155 if (what & SA_NOTRECURSED)
1156 panic("Lock %s recursed @ %s:%d\n",
1157 sx->lock_object.lo_name, file, line);
1158 } else if (what & SA_RECURSED)
1159 panic("Lock %s not recursed @ %s:%d\n",
1160 sx->lock_object.lo_name, file, line);
1164 witness_assert(&sx->lock_object, what, file, line);
1167 * If we hold an exclusve lock fail. We can't
1168 * reliably check to see if we hold a shared lock or
1171 if (sx_xholder(sx) == curthread)
1172 panic("Lock %s exclusively locked @ %s:%d\n",
1173 sx->lock_object.lo_name, file, line);
1177 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1181 #endif /* INVARIANT_SUPPORT */
1185 db_show_sx(const struct lock_object *lock)
1188 const struct sx *sx;
1190 sx = (const struct sx *)lock;
1192 db_printf(" state: ");
1193 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1194 db_printf("UNLOCKED\n");
1195 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1196 db_printf("DESTROYED\n");
1198 } else if (sx->sx_lock & SX_LOCK_SHARED)
1199 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1201 td = sx_xholder(sx);
1202 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1203 td->td_tid, td->td_proc->p_pid, td->td_name);
1204 if (sx_recursed(sx))
1205 db_printf(" recursed: %d\n", sx->sx_recurse);
1208 db_printf(" waiters: ");
1209 switch(sx->sx_lock &
1210 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1211 case SX_LOCK_SHARED_WAITERS:
1212 db_printf("shared\n");
1214 case SX_LOCK_EXCLUSIVE_WAITERS:
1215 db_printf("exclusive\n");
1217 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1218 db_printf("exclusive and shared\n");
1221 db_printf("none\n");
1226 * Check to see if a thread that is blocked on a sleep queue is actually
1227 * blocked on an sx lock. If so, output some details and return true.
1228 * If the lock has an exclusive owner, return that in *ownerp.
1231 sx_chain(struct thread *td, struct thread **ownerp)
1236 * Check to see if this thread is blocked on an sx lock.
1237 * First, we check the lock class. If that is ok, then we
1238 * compare the lock name against the wait message.
1241 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1242 sx->lock_object.lo_name != td->td_wmesg)
1245 /* We think we have an sx lock, so output some details. */
1246 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1247 *ownerp = sx_xholder(sx);
1248 if (sx->sx_lock & SX_LOCK_SHARED)
1249 db_printf("SLOCK (count %ju)\n",
1250 (uintmax_t)SX_SHARERS(sx->sx_lock));
1252 db_printf("XLOCK\n");