2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007 Attilio Rao <attilio@freebsd.org>
5 * Copyright (c) 2001 Jason Evans <jasone@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice(s), this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified other than the possible
14 * addition of one or more copyright notices.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice(s), this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
33 * Shared/exclusive locks. This implementation attempts to ensure
34 * deterministic lock granting behavior, so that slocks and xlocks are
37 * Priority propagation will not generally raise the priority of lock holders,
38 * so should not be relied upon in combination with sx locks.
42 #include "opt_hwpmc_hooks.h"
43 #include "opt_no_adaptive_sx.h"
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/kernel.h>
54 #include <sys/mutex.h>
56 #include <sys/sched.h>
57 #include <sys/sleepqueue.h>
60 #include <sys/sysctl.h>
62 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
63 #include <machine/cpu.h>
70 #if defined(SMP) && !defined(NO_ADAPTIVE_SX)
75 #include <sys/pmckern.h>
76 PMC_SOFT_DECLARE( , , lock, failed);
79 /* Handy macros for sleep queues. */
80 #define SQ_EXCLUSIVE_QUEUE 0
81 #define SQ_SHARED_QUEUE 1
84 * Variations on DROP_GIANT()/PICKUP_GIANT() for use in this file. We
85 * drop Giant anytime we have to sleep or if we adaptively spin.
87 #define GIANT_DECLARE \
89 WITNESS_SAVE_DECL(Giant) \
91 #define GIANT_SAVE(work) do { \
92 if (__predict_false(mtx_owned(&Giant))) { \
94 WITNESS_SAVE(&Giant.lock_object, Giant); \
95 while (mtx_owned(&Giant)) { \
102 #define GIANT_RESTORE() do { \
103 if (_giantcnt > 0) { \
104 mtx_assert(&Giant, MA_NOTOWNED); \
105 while (_giantcnt--) \
107 WITNESS_RESTORE(&Giant.lock_object, Giant); \
112 * Returns true if an exclusive lock is recursed. It assumes
113 * curthread currently has an exclusive lock.
115 #define sx_recursed(sx) ((sx)->sx_recurse != 0)
117 static void assert_sx(const struct lock_object *lock, int what);
119 static void db_show_sx(const struct lock_object *lock);
121 static void lock_sx(struct lock_object *lock, uintptr_t how);
123 static int owner_sx(const struct lock_object *lock, struct thread **owner);
125 static uintptr_t unlock_sx(struct lock_object *lock);
127 struct lock_class lock_class_sx = {
129 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE | LC_UPGRADABLE,
130 .lc_assert = assert_sx,
132 .lc_ddb_show = db_show_sx,
135 .lc_unlock = unlock_sx,
137 .lc_owner = owner_sx,
142 #define _sx_assert(sx, what, file, line)
146 #ifdef SX_CUSTOM_BACKOFF
147 static u_short __read_frequently asx_retries;
148 static u_short __read_frequently asx_loops;
149 static SYSCTL_NODE(_debug, OID_AUTO, sx, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
151 SYSCTL_U16(_debug_sx, OID_AUTO, retries, CTLFLAG_RW, &asx_retries, 0, "");
152 SYSCTL_U16(_debug_sx, OID_AUTO, loops, CTLFLAG_RW, &asx_loops, 0, "");
154 static struct lock_delay_config __read_frequently sx_delay;
156 SYSCTL_U16(_debug_sx, OID_AUTO, delay_base, CTLFLAG_RW, &sx_delay.base,
158 SYSCTL_U16(_debug_sx, OID_AUTO, delay_max, CTLFLAG_RW, &sx_delay.max,
162 sx_lock_delay_init(void *arg __unused)
165 lock_delay_default_init(&sx_delay);
167 asx_loops = max(10000, sx_delay.max);
169 LOCK_DELAY_SYSINIT(sx_lock_delay_init);
171 #define sx_delay locks_delay
172 #define asx_retries locks_delay_retries
173 #define asx_loops locks_delay_loops
178 assert_sx(const struct lock_object *lock, int what)
181 sx_assert((const struct sx *)lock, what);
185 lock_sx(struct lock_object *lock, uintptr_t how)
189 sx = (struct sx *)lock;
197 unlock_sx(struct lock_object *lock)
201 sx = (struct sx *)lock;
202 sx_assert(sx, SA_LOCKED | SA_NOTRECURSED);
203 if (sx_xlocked(sx)) {
214 owner_sx(const struct lock_object *lock, struct thread **owner)
219 sx = (const struct sx *)lock;
222 return ((x & SX_LOCK_SHARED) != 0 ? (SX_SHARERS(x) != 0) :
223 ((*owner = (struct thread *)SX_OWNER(x)) != NULL));
228 sx_sysinit(void *arg)
230 struct sx_args *sargs = arg;
232 sx_init_flags(sargs->sa_sx, sargs->sa_desc, sargs->sa_flags);
236 sx_init_flags(struct sx *sx, const char *description, int opts)
240 MPASS((opts & ~(SX_QUIET | SX_RECURSE | SX_NOWITNESS | SX_DUPOK |
241 SX_NOPROFILE | SX_NEW)) == 0);
242 ASSERT_ATOMIC_LOAD_PTR(sx->sx_lock,
243 ("%s: sx_lock not aligned for %s: %p", __func__, description,
246 flags = LO_SLEEPABLE | LO_UPGRADABLE;
249 if (opts & SX_NOPROFILE)
250 flags |= LO_NOPROFILE;
251 if (!(opts & SX_NOWITNESS))
253 if (opts & SX_RECURSE)
254 flags |= LO_RECURSABLE;
260 lock_init(&sx->lock_object, &lock_class_sx, description, NULL, flags);
261 sx->sx_lock = SX_LOCK_UNLOCKED;
266 sx_destroy(struct sx *sx)
269 KASSERT(sx->sx_lock == SX_LOCK_UNLOCKED, ("sx lock still held"));
270 KASSERT(sx->sx_recurse == 0, ("sx lock still recursed"));
271 sx->sx_lock = SX_LOCK_DESTROYED;
272 lock_destroy(&sx->lock_object);
276 sx_try_slock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF)
280 if (SCHEDULER_STOPPED())
283 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
284 ("sx_try_slock() by idle thread %p on sx %s @ %s:%d",
285 curthread, sx->lock_object.lo_name, file, line));
289 KASSERT(x != SX_LOCK_DESTROYED,
290 ("sx_try_slock() of destroyed sx @ %s:%d", file, line));
291 if (!(x & SX_LOCK_SHARED))
293 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, x + SX_ONE_SHARER)) {
294 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 1, file, line);
295 WITNESS_LOCK(&sx->lock_object, LOP_TRYLOCK, file, line);
296 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire,
297 sx, 0, 0, file, line, LOCKSTAT_READER);
298 TD_LOCKS_INC(curthread);
299 curthread->td_sx_slocks++;
304 LOCK_LOG_TRY("SLOCK", &sx->lock_object, 0, 0, file, line);
309 sx_try_slock_(struct sx *sx, const char *file, int line)
312 return (sx_try_slock_int(sx LOCK_FILE_LINE_ARG));
316 _sx_xlock(struct sx *sx, int opts, const char *file, int line)
321 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
322 !TD_IS_IDLETHREAD(curthread),
323 ("sx_xlock() by idle thread %p on sx %s @ %s:%d",
324 curthread, sx->lock_object.lo_name, file, line));
325 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
326 ("sx_xlock() of destroyed sx @ %s:%d", file, line));
327 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
329 tid = (uintptr_t)curthread;
330 x = SX_LOCK_UNLOCKED;
331 if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid))
332 error = _sx_xlock_hard(sx, x, opts LOCK_FILE_LINE_ARG);
334 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx,
335 0, 0, file, line, LOCKSTAT_WRITER);
337 LOCK_LOG_LOCK("XLOCK", &sx->lock_object, 0, sx->sx_recurse,
339 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
340 TD_LOCKS_INC(curthread);
347 sx_try_xlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF)
356 if (SCHEDULER_STOPPED_TD(td))
359 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
360 ("sx_try_xlock() by idle thread %p on sx %s @ %s:%d",
361 curthread, sx->lock_object.lo_name, file, line));
362 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
363 ("sx_try_xlock() of destroyed sx @ %s:%d", file, line));
367 x = SX_LOCK_UNLOCKED;
369 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid))
371 if (x == SX_LOCK_UNLOCKED)
373 if (x == tid && (sx->lock_object.lo_flags & LO_RECURSABLE)) {
375 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
382 LOCK_LOG_TRY("XLOCK", &sx->lock_object, 0, rval, file, line);
384 WITNESS_LOCK(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
387 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire,
388 sx, 0, 0, file, line, LOCKSTAT_WRITER);
389 TD_LOCKS_INC(curthread);
396 sx_try_xlock_(struct sx *sx, const char *file, int line)
399 return (sx_try_xlock_int(sx LOCK_FILE_LINE_ARG));
403 _sx_xunlock(struct sx *sx, const char *file, int line)
406 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
407 ("sx_xunlock() of destroyed sx @ %s:%d", file, line));
408 _sx_assert(sx, SA_XLOCKED, file, line);
409 WITNESS_UNLOCK(&sx->lock_object, LOP_EXCLUSIVE, file, line);
410 LOCK_LOG_LOCK("XUNLOCK", &sx->lock_object, 0, sx->sx_recurse, file,
413 _sx_xunlock_hard(sx, (uintptr_t)curthread, file, line);
415 __sx_xunlock(sx, curthread, file, line);
417 TD_LOCKS_DEC(curthread);
421 * Try to do a non-blocking upgrade from a shared lock to an exclusive lock.
422 * This will only succeed if this thread holds a single shared lock.
423 * Return 1 if if the upgrade succeed, 0 otherwise.
426 sx_try_upgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF)
432 if (SCHEDULER_STOPPED())
435 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
436 ("sx_try_upgrade() of destroyed sx @ %s:%d", file, line));
437 _sx_assert(sx, SA_SLOCKED, file, line);
440 * Try to switch from one shared lock to an exclusive lock. We need
441 * to maintain the SX_LOCK_EXCLUSIVE_WAITERS flag if set so that
442 * we will wake up the exclusive waiters when we drop the lock.
445 x = SX_READ_VALUE(sx);
447 if (SX_SHARERS(x) > 1)
449 waiters = (x & SX_LOCK_WAITERS);
450 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x,
451 (uintptr_t)curthread | waiters)) {
456 LOCK_LOG_TRY("XUPGRADE", &sx->lock_object, 0, success, file, line);
458 curthread->td_sx_slocks--;
459 WITNESS_UPGRADE(&sx->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
461 LOCKSTAT_RECORD0(sx__upgrade, sx);
467 sx_try_upgrade_(struct sx *sx, const char *file, int line)
470 return (sx_try_upgrade_int(sx LOCK_FILE_LINE_ARG));
474 * Downgrade an unrecursed exclusive lock into a single shared lock.
477 sx_downgrade_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF)
482 if (SCHEDULER_STOPPED())
485 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
486 ("sx_downgrade() of destroyed sx @ %s:%d", file, line));
487 _sx_assert(sx, SA_XLOCKED | SA_NOTRECURSED, file, line);
490 panic("downgrade of a recursed lock");
493 WITNESS_DOWNGRADE(&sx->lock_object, 0, file, line);
496 * Try to switch from an exclusive lock with no shared waiters
497 * to one sharer with no shared waiters. If there are
498 * exclusive waiters, we don't need to lock the sleep queue so
499 * long as we preserve the flag. We do one quick try and if
500 * that fails we grab the sleepq lock to keep the flags from
501 * changing and do it the slow way.
503 * We have to lock the sleep queue if there are shared waiters
504 * so we can wake them up.
507 if (!(x & SX_LOCK_SHARED_WAITERS) &&
508 atomic_cmpset_rel_ptr(&sx->sx_lock, x, SX_SHARERS_LOCK(1) |
509 (x & SX_LOCK_EXCLUSIVE_WAITERS)))
513 * Lock the sleep queue so we can read the waiters bits
514 * without any races and wakeup any shared waiters.
516 sleepq_lock(&sx->lock_object);
519 * Preserve SX_LOCK_EXCLUSIVE_WAITERS while downgraded to a single
520 * shared lock. If there are any shared waiters, wake them up.
524 atomic_store_rel_ptr(&sx->sx_lock, SX_SHARERS_LOCK(1) |
525 (x & SX_LOCK_EXCLUSIVE_WAITERS));
526 if (x & SX_LOCK_SHARED_WAITERS)
527 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
529 sleepq_release(&sx->lock_object);
535 curthread->td_sx_slocks++;
536 LOCK_LOG_LOCK("XDOWNGRADE", &sx->lock_object, 0, 0, file, line);
537 LOCKSTAT_RECORD0(sx__downgrade, sx);
541 sx_downgrade_(struct sx *sx, const char *file, int line)
544 sx_downgrade_int(sx LOCK_FILE_LINE_ARG);
549 sx_drop_critical(uintptr_t x, bool *in_critical, int *extra_work)
552 if (x & SX_LOCK_WRITE_SPINNER)
556 *in_critical = false;
561 #define sx_drop_critical(x, in_critical, extra_work) do { } while(0)
565 * This function represents the so-called 'hard case' for sx_xlock
566 * operation. All 'easy case' failures are redirected to this. Note
567 * that ideally this would be a static function, but it needs to be
568 * accessible from at least sx.h.
571 _sx_xlock_hard(struct sx *sx, uintptr_t x, int opts LOCK_FILE_LINE_ARG_DEF)
576 volatile struct thread *owner;
577 u_int i, n, spintries = 0;
578 enum { READERS, WRITER } sleep_reason = READERS;
579 bool in_critical = false;
581 #ifdef LOCK_PROFILING
582 uint64_t waittime = 0;
586 #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS)
587 struct lock_delay_arg lda;
591 int64_t sleep_time = 0;
592 int64_t all_time = 0;
594 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
596 int doing_lockprof = 0;
600 tid = (uintptr_t)curthread;
603 if (LOCKSTAT_PROFILE_ENABLED(sx__acquire)) {
604 while (x == SX_LOCK_UNLOCKED) {
605 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid))
610 all_time -= lockstat_nsecs(&sx->lock_object);
614 #ifdef LOCK_PROFILING
620 if (SCHEDULER_STOPPED())
623 #if defined(ADAPTIVE_SX)
624 lock_delay_arg_init(&lda, &sx_delay);
625 #elif defined(KDTRACE_HOOKS)
626 lock_delay_arg_init_noadapt(&lda);
629 if (__predict_false(x == SX_LOCK_UNLOCKED))
630 x = SX_READ_VALUE(sx);
632 /* If we already hold an exclusive lock, then recurse. */
633 if (__predict_false(lv_sx_owner(x) == (struct thread *)tid)) {
634 KASSERT((sx->lock_object.lo_flags & LO_RECURSABLE) != 0,
635 ("_sx_xlock_hard: recursed on non-recursive sx %s @ %s:%d\n",
636 sx->lock_object.lo_name, file, line));
638 atomic_set_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
639 if (LOCK_LOG_TEST(&sx->lock_object, 0))
640 CTR2(KTR_LOCK, "%s: %p recursing", __func__, sx);
644 if (LOCK_LOG_TEST(&sx->lock_object, 0))
645 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
646 sx->lock_object.lo_name, (void *)sx->sx_lock, file, line);
649 PMC_SOFT_CALL( , , lock, failed);
651 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
655 GIANT_SAVE(extra_work);
659 if (x == SX_LOCK_UNLOCKED) {
660 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid))
665 GIANT_SAVE(extra_work);
671 if (x == (SX_LOCK_SHARED | SX_LOCK_WRITE_SPINNER)) {
672 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid))
678 * If the lock is write locked and the owner is
679 * running on another CPU, spin until the owner stops
680 * running or the state of the lock changes.
682 if ((x & SX_LOCK_SHARED) == 0) {
683 sx_drop_critical(x, &in_critical, &extra_work);
684 sleep_reason = WRITER;
685 owner = lv_sx_owner(x);
686 if (!TD_IS_RUNNING(owner))
688 if (LOCK_LOG_TEST(&sx->lock_object, 0))
689 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
690 __func__, sx, owner);
691 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
692 "spinning", "lockname:\"%s\"",
693 sx->lock_object.lo_name);
696 x = SX_READ_VALUE(sx);
697 owner = lv_sx_owner(x);
698 } while (owner != NULL && TD_IS_RUNNING(owner));
699 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
702 } else if (SX_SHARERS(x) > 0) {
703 sleep_reason = READERS;
704 if (spintries == asx_retries)
706 if (!(x & SX_LOCK_WRITE_SPINNER)) {
712 if (!atomic_fcmpset_ptr(&sx->sx_lock, &x,
713 x | SX_LOCK_WRITE_SPINNER)) {
721 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
722 "spinning", "lockname:\"%s\"",
723 sx->lock_object.lo_name);
725 for (i = 0; i < asx_loops; i += n) {
727 x = SX_READ_VALUE(sx);
728 if (!(x & SX_LOCK_WRITE_SPINNER))
730 if (!(x & SX_LOCK_SHARED))
739 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
746 sleepq_lock(&sx->lock_object);
747 x = SX_READ_VALUE(sx);
751 * If the lock was released while spinning on the
752 * sleep queue chain lock, try again.
754 if (x == SX_LOCK_UNLOCKED) {
755 sleepq_release(&sx->lock_object);
756 sx_drop_critical(x, &in_critical, &extra_work);
762 * The current lock owner might have started executing
763 * on another CPU (or the lock could have changed
764 * owners) while we were waiting on the sleep queue
765 * chain lock. If so, drop the sleep queue lock and try
768 if (!(x & SX_LOCK_SHARED)) {
769 owner = (struct thread *)SX_OWNER(x);
770 if (TD_IS_RUNNING(owner)) {
771 sleepq_release(&sx->lock_object);
772 sx_drop_critical(x, &in_critical,
776 } else if (SX_SHARERS(x) > 0 && sleep_reason == WRITER) {
777 sleepq_release(&sx->lock_object);
778 sx_drop_critical(x, &in_critical, &extra_work);
784 * If an exclusive lock was released with both shared
785 * and exclusive waiters and a shared waiter hasn't
786 * woken up and acquired the lock yet, sx_lock will be
787 * set to SX_LOCK_UNLOCKED | SX_LOCK_EXCLUSIVE_WAITERS.
788 * If we see that value, try to acquire it once. Note
789 * that we have to preserve SX_LOCK_EXCLUSIVE_WAITERS
790 * as there are other exclusive waiters still. If we
791 * fail, restart the loop.
793 setx = x & (SX_LOCK_WAITERS | SX_LOCK_WRITE_SPINNER);
794 if ((x & ~setx) == SX_LOCK_SHARED) {
795 setx &= ~SX_LOCK_WRITE_SPINNER;
796 if (!atomic_fcmpset_acq_ptr(&sx->sx_lock, &x, tid | setx))
798 sleepq_release(&sx->lock_object);
799 CTR2(KTR_LOCK, "%s: %p claimed by new writer",
806 * It is possible we set the SX_LOCK_WRITE_SPINNER bit.
807 * It is an invariant that when the bit is set, there is
808 * a writer ready to grab the lock. Thus clear the bit since
809 * we are going to sleep.
812 if ((x & SX_LOCK_WRITE_SPINNER) ||
813 !((x & SX_LOCK_EXCLUSIVE_WAITERS))) {
814 setx = x & ~SX_LOCK_WRITE_SPINNER;
815 setx |= SX_LOCK_EXCLUSIVE_WAITERS;
816 if (!atomic_fcmpset_ptr(&sx->sx_lock, &x,
826 * Try to set the SX_LOCK_EXCLUSIVE_WAITERS. If we fail,
827 * than loop back and retry.
829 if (!(x & SX_LOCK_EXCLUSIVE_WAITERS)) {
830 if (!atomic_fcmpset_ptr(&sx->sx_lock, &x,
831 x | SX_LOCK_EXCLUSIVE_WAITERS)) {
834 if (LOCK_LOG_TEST(&sx->lock_object, 0))
835 CTR2(KTR_LOCK, "%s: %p set excl waiters flag",
843 * Since we have been unable to acquire the exclusive
844 * lock and the exclusive waiters flag is set, we have
847 if (LOCK_LOG_TEST(&sx->lock_object, 0))
848 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
852 sleep_time -= lockstat_nsecs(&sx->lock_object);
854 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
855 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
856 SLEEPQ_INTERRUPTIBLE : 0), SQ_EXCLUSIVE_QUEUE);
857 if (!(opts & SX_INTERRUPTIBLE))
858 sleepq_wait(&sx->lock_object, 0);
860 error = sleepq_wait_sig(&sx->lock_object, 0);
862 sleep_time += lockstat_nsecs(&sx->lock_object);
866 if (LOCK_LOG_TEST(&sx->lock_object, 0))
868 "%s: interruptible sleep by %p suspended by signal",
872 if (LOCK_LOG_TEST(&sx->lock_object, 0))
873 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
875 x = SX_READ_VALUE(sx);
877 if (__predict_true(!extra_work))
884 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
885 if (__predict_true(!doing_lockprof))
889 all_time += lockstat_nsecs(&sx->lock_object);
891 LOCKSTAT_RECORD4(sx__block, sx, sleep_time,
892 LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0,
893 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
894 if (lda.spin_cnt > sleep_cnt)
895 LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time,
896 LOCKSTAT_WRITER, (state & SX_LOCK_SHARED) == 0,
897 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
901 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx,
902 contested, waittime, file, line, LOCKSTAT_WRITER);
907 * This function represents the so-called 'hard case' for sx_xunlock
908 * operation. All 'easy case' failures are redirected to this. Note
909 * that ideally this would be a static function, but it needs to be
910 * accessible from at least sx.h.
913 _sx_xunlock_hard(struct sx *sx, uintptr_t x LOCK_FILE_LINE_ARG_DEF)
916 int queue, wakeup_swapper;
918 if (SCHEDULER_STOPPED())
921 tid = (uintptr_t)curthread;
923 if (__predict_false(x == tid))
924 x = SX_READ_VALUE(sx);
926 MPASS(!(x & SX_LOCK_SHARED));
928 if (__predict_false(x & SX_LOCK_RECURSED)) {
929 /* The lock is recursed, unrecurse one level. */
930 if ((--sx->sx_recurse) == 0)
931 atomic_clear_ptr(&sx->sx_lock, SX_LOCK_RECURSED);
932 if (LOCK_LOG_TEST(&sx->lock_object, 0))
933 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, sx);
937 LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_WRITER);
939 atomic_cmpset_rel_ptr(&sx->sx_lock, tid, SX_LOCK_UNLOCKED))
942 if (LOCK_LOG_TEST(&sx->lock_object, 0))
943 CTR2(KTR_LOCK, "%s: %p contested", __func__, sx);
945 sleepq_lock(&sx->lock_object);
946 x = SX_READ_VALUE(sx);
947 MPASS(x & (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS));
950 * The wake up algorithm here is quite simple and probably not
951 * ideal. It gives precedence to shared waiters if they are
952 * present. For this condition, we have to preserve the
953 * state of the exclusive waiters flag.
954 * If interruptible sleeps left the shared queue empty avoid a
955 * starvation for the threads sleeping on the exclusive queue by giving
956 * them precedence and cleaning up the shared waiters bit anyway.
958 setx = SX_LOCK_UNLOCKED;
959 queue = SQ_SHARED_QUEUE;
960 if ((x & SX_LOCK_EXCLUSIVE_WAITERS) != 0 &&
961 sleepq_sleepcnt(&sx->lock_object, SQ_EXCLUSIVE_QUEUE) != 0) {
962 queue = SQ_EXCLUSIVE_QUEUE;
963 setx |= (x & SX_LOCK_SHARED_WAITERS);
965 atomic_store_rel_ptr(&sx->sx_lock, setx);
967 /* Wake up all the waiters for the specific queue. */
968 if (LOCK_LOG_TEST(&sx->lock_object, 0))
969 CTR3(KTR_LOCK, "%s: %p waking up all threads on %s queue",
970 __func__, sx, queue == SQ_SHARED_QUEUE ? "shared" :
973 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX, 0,
975 sleepq_release(&sx->lock_object);
980 static bool __always_inline
981 __sx_can_read(struct thread *td, uintptr_t x, bool fp)
984 if ((x & (SX_LOCK_SHARED | SX_LOCK_EXCLUSIVE_WAITERS | SX_LOCK_WRITE_SPINNER))
987 if (!fp && td->td_sx_slocks && (x & SX_LOCK_SHARED))
992 static bool __always_inline
993 __sx_slock_try(struct sx *sx, struct thread *td, uintptr_t *xp, bool fp
994 LOCK_FILE_LINE_ARG_DEF)
998 * If no other thread has an exclusive lock then try to bump up
999 * the count of sharers. Since we have to preserve the state
1000 * of SX_LOCK_EXCLUSIVE_WAITERS, if we fail to acquire the
1001 * shared lock loop back and retry.
1003 while (__sx_can_read(td, *xp, fp)) {
1004 if (atomic_fcmpset_acq_ptr(&sx->sx_lock, xp,
1005 *xp + SX_ONE_SHARER)) {
1006 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1007 CTR4(KTR_LOCK, "%s: %p succeed %p -> %p",
1008 __func__, sx, (void *)*xp,
1009 (void *)(*xp + SX_ONE_SHARER));
1017 static int __noinline
1018 _sx_slock_hard(struct sx *sx, int opts, uintptr_t x LOCK_FILE_LINE_ARG_DEF)
1023 volatile struct thread *owner;
1024 u_int i, n, spintries = 0;
1026 #ifdef LOCK_PROFILING
1027 uint64_t waittime = 0;
1031 #if defined(ADAPTIVE_SX) || defined(KDTRACE_HOOKS)
1032 struct lock_delay_arg lda;
1034 #ifdef KDTRACE_HOOKS
1035 u_int sleep_cnt = 0;
1036 int64_t sleep_time = 0;
1037 int64_t all_time = 0;
1039 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
1040 uintptr_t state = 0;
1046 #ifdef KDTRACE_HOOKS
1047 if (LOCKSTAT_PROFILE_ENABLED(sx__acquire)) {
1048 if (__sx_slock_try(sx, td, &x, false LOCK_FILE_LINE_ARG))
1051 all_time -= lockstat_nsecs(&sx->lock_object);
1055 #ifdef LOCK_PROFILING
1060 if (SCHEDULER_STOPPED())
1063 #if defined(ADAPTIVE_SX)
1064 lock_delay_arg_init(&lda, &sx_delay);
1065 #elif defined(KDTRACE_HOOKS)
1066 lock_delay_arg_init_noadapt(&lda);
1070 PMC_SOFT_CALL( , , lock, failed);
1072 lock_profile_obtain_lock_failed(&sx->lock_object, &contested,
1076 GIANT_SAVE(extra_work);
1080 * As with rwlocks, we don't make any attempt to try to block
1081 * shared locks once there is an exclusive waiter.
1084 if (__sx_slock_try(sx, td, &x, false LOCK_FILE_LINE_ARG))
1087 GIANT_SAVE(extra_work);
1089 #ifdef KDTRACE_HOOKS
1095 * If the owner is running on another CPU, spin until
1096 * the owner stops running or the state of the lock
1099 if ((x & SX_LOCK_SHARED) == 0) {
1100 owner = lv_sx_owner(x);
1101 if (TD_IS_RUNNING(owner)) {
1102 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1104 "%s: spinning on %p held by %p",
1105 __func__, sx, owner);
1106 KTR_STATE1(KTR_SCHED, "thread",
1107 sched_tdname(curthread), "spinning",
1108 "lockname:\"%s\"", sx->lock_object.lo_name);
1111 x = SX_READ_VALUE(sx);
1112 owner = lv_sx_owner(x);
1113 } while (owner != NULL && TD_IS_RUNNING(owner));
1114 KTR_STATE0(KTR_SCHED, "thread",
1115 sched_tdname(curthread), "running");
1119 if ((x & SX_LOCK_WRITE_SPINNER) && SX_SHARERS(x) == 0) {
1120 MPASS(!__sx_can_read(td, x, false));
1122 x = SX_READ_VALUE(sx);
1125 if (spintries < asx_retries) {
1126 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
1127 "spinning", "lockname:\"%s\"",
1128 sx->lock_object.lo_name);
1130 for (i = 0; i < asx_loops; i += n) {
1132 x = SX_READ_VALUE(sx);
1133 if (!(x & SX_LOCK_SHARED))
1138 if (__sx_can_read(td, x, false))
1141 #ifdef KDTRACE_HOOKS
1144 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
1153 * Some other thread already has an exclusive lock, so
1154 * start the process of blocking.
1156 sleepq_lock(&sx->lock_object);
1157 x = SX_READ_VALUE(sx);
1159 if (((x & SX_LOCK_WRITE_SPINNER) && SX_SHARERS(x) == 0) ||
1160 __sx_can_read(td, x, false)) {
1161 sleepq_release(&sx->lock_object);
1167 * If the owner is running on another CPU, spin until
1168 * the owner stops running or the state of the lock
1171 if (!(x & SX_LOCK_SHARED)) {
1172 owner = (struct thread *)SX_OWNER(x);
1173 if (TD_IS_RUNNING(owner)) {
1174 sleepq_release(&sx->lock_object);
1175 x = SX_READ_VALUE(sx);
1182 * Try to set the SX_LOCK_SHARED_WAITERS flag. If we
1183 * fail to set it drop the sleep queue lock and loop
1186 if (!(x & SX_LOCK_SHARED_WAITERS)) {
1187 if (!atomic_fcmpset_ptr(&sx->sx_lock, &x,
1188 x | SX_LOCK_SHARED_WAITERS))
1190 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1191 CTR2(KTR_LOCK, "%s: %p set shared waiters flag",
1196 * Since we have been unable to acquire the shared lock,
1199 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1200 CTR2(KTR_LOCK, "%s: %p blocking on sleep queue",
1203 #ifdef KDTRACE_HOOKS
1204 sleep_time -= lockstat_nsecs(&sx->lock_object);
1206 sleepq_add(&sx->lock_object, NULL, sx->lock_object.lo_name,
1207 SLEEPQ_SX | ((opts & SX_INTERRUPTIBLE) ?
1208 SLEEPQ_INTERRUPTIBLE : 0), SQ_SHARED_QUEUE);
1209 if (!(opts & SX_INTERRUPTIBLE))
1210 sleepq_wait(&sx->lock_object, 0);
1212 error = sleepq_wait_sig(&sx->lock_object, 0);
1213 #ifdef KDTRACE_HOOKS
1214 sleep_time += lockstat_nsecs(&sx->lock_object);
1218 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1220 "%s: interruptible sleep by %p suspended by signal",
1224 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1225 CTR2(KTR_LOCK, "%s: %p resuming from sleep queue",
1227 x = SX_READ_VALUE(sx);
1229 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
1230 if (__predict_true(!extra_work))
1233 #ifdef KDTRACE_HOOKS
1234 all_time += lockstat_nsecs(&sx->lock_object);
1236 LOCKSTAT_RECORD4(sx__block, sx, sleep_time,
1237 LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0,
1238 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
1239 if (lda.spin_cnt > sleep_cnt)
1240 LOCKSTAT_RECORD4(sx__spin, sx, all_time - sleep_time,
1241 LOCKSTAT_READER, (state & SX_LOCK_SHARED) == 0,
1242 (state & SX_LOCK_SHARED) == 0 ? 0 : SX_SHARERS(state));
1246 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(sx__acquire, sx,
1247 contested, waittime, file, line, LOCKSTAT_READER);
1254 _sx_slock_int(struct sx *sx, int opts LOCK_FILE_LINE_ARG_DEF)
1260 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
1261 !TD_IS_IDLETHREAD(curthread),
1262 ("sx_slock() by idle thread %p on sx %s @ %s:%d",
1263 curthread, sx->lock_object.lo_name, file, line));
1264 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
1265 ("sx_slock() of destroyed sx @ %s:%d", file, line));
1266 WITNESS_CHECKORDER(&sx->lock_object, LOP_NEWORDER, file, line, NULL);
1270 x = SX_READ_VALUE(sx);
1271 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(sx__acquire) ||
1272 !__sx_slock_try(sx, td, &x, true LOCK_FILE_LINE_ARG)))
1273 error = _sx_slock_hard(sx, opts, x LOCK_FILE_LINE_ARG);
1275 lock_profile_obtain_lock_success(&sx->lock_object, 0, 0,
1278 LOCK_LOG_LOCK("SLOCK", &sx->lock_object, 0, 0, file, line);
1279 WITNESS_LOCK(&sx->lock_object, 0, file, line);
1280 TD_LOCKS_INC(curthread);
1286 _sx_slock(struct sx *sx, int opts, const char *file, int line)
1289 return (_sx_slock_int(sx, opts LOCK_FILE_LINE_ARG));
1292 static bool __always_inline
1293 _sx_sunlock_try(struct sx *sx, struct thread *td, uintptr_t *xp)
1297 if (SX_SHARERS(*xp) > 1 || !(*xp & SX_LOCK_WAITERS)) {
1298 if (atomic_fcmpset_rel_ptr(&sx->sx_lock, xp,
1299 *xp - SX_ONE_SHARER)) {
1300 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1302 "%s: %p succeeded %p -> %p",
1303 __func__, sx, (void *)*xp,
1304 (void *)(*xp - SX_ONE_SHARER));
1315 static void __noinline
1316 _sx_sunlock_hard(struct sx *sx, struct thread *td, uintptr_t x
1317 LOCK_FILE_LINE_ARG_DEF)
1319 int wakeup_swapper = 0;
1320 uintptr_t setx, queue;
1322 if (SCHEDULER_STOPPED())
1325 if (_sx_sunlock_try(sx, td, &x))
1328 sleepq_lock(&sx->lock_object);
1329 x = SX_READ_VALUE(sx);
1331 if (_sx_sunlock_try(sx, td, &x))
1335 * Wake up semantic here is quite simple:
1336 * Just wake up all the exclusive waiters.
1337 * Note that the state of the lock could have changed,
1338 * so if it fails loop back and retry.
1340 setx = SX_LOCK_UNLOCKED;
1341 queue = SQ_SHARED_QUEUE;
1342 if (x & SX_LOCK_EXCLUSIVE_WAITERS) {
1343 setx |= (x & SX_LOCK_SHARED_WAITERS);
1344 queue = SQ_EXCLUSIVE_QUEUE;
1346 setx |= (x & SX_LOCK_WRITE_SPINNER);
1347 if (!atomic_fcmpset_rel_ptr(&sx->sx_lock, &x, setx))
1349 if (LOCK_LOG_TEST(&sx->lock_object, 0))
1350 CTR2(KTR_LOCK, "%s: %p waking up all thread on"
1351 "exclusive queue", __func__, sx);
1352 wakeup_swapper = sleepq_broadcast(&sx->lock_object, SLEEPQ_SX,
1357 sleepq_release(&sx->lock_object);
1361 LOCKSTAT_PROFILE_RELEASE_RWLOCK(sx__release, sx, LOCKSTAT_READER);
1365 _sx_sunlock_int(struct sx *sx LOCK_FILE_LINE_ARG_DEF)
1370 KASSERT(sx->sx_lock != SX_LOCK_DESTROYED,
1371 ("sx_sunlock() of destroyed sx @ %s:%d", file, line));
1372 _sx_assert(sx, SA_SLOCKED, file, line);
1373 WITNESS_UNLOCK(&sx->lock_object, 0, file, line);
1374 LOCK_LOG_LOCK("SUNLOCK", &sx->lock_object, 0, 0, file, line);
1377 x = SX_READ_VALUE(sx);
1378 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(sx__release) ||
1379 !_sx_sunlock_try(sx, td, &x)))
1380 _sx_sunlock_hard(sx, td, x LOCK_FILE_LINE_ARG);
1382 lock_profile_release_lock(&sx->lock_object);
1384 TD_LOCKS_DEC(curthread);
1388 _sx_sunlock(struct sx *sx, const char *file, int line)
1391 _sx_sunlock_int(sx LOCK_FILE_LINE_ARG);
1394 #ifdef INVARIANT_SUPPORT
1400 * In the non-WITNESS case, sx_assert() can only detect that at least
1401 * *some* thread owns an slock, but it cannot guarantee that *this*
1402 * thread owns an slock.
1405 _sx_assert(const struct sx *sx, int what, const char *file, int line)
1411 if (SCHEDULER_STOPPED())
1415 case SA_SLOCKED | SA_NOTRECURSED:
1416 case SA_SLOCKED | SA_RECURSED:
1422 case SA_LOCKED | SA_NOTRECURSED:
1423 case SA_LOCKED | SA_RECURSED:
1425 witness_assert(&sx->lock_object, what, file, line);
1428 * If some other thread has an exclusive lock or we
1429 * have one and are asserting a shared lock, fail.
1430 * Also, if no one has a lock at all, fail.
1432 if (sx->sx_lock == SX_LOCK_UNLOCKED ||
1433 (!(sx->sx_lock & SX_LOCK_SHARED) && (slocked ||
1434 sx_xholder(sx) != curthread)))
1435 panic("Lock %s not %slocked @ %s:%d\n",
1436 sx->lock_object.lo_name, slocked ? "share " : "",
1439 if (!(sx->sx_lock & SX_LOCK_SHARED)) {
1440 if (sx_recursed(sx)) {
1441 if (what & SA_NOTRECURSED)
1442 panic("Lock %s recursed @ %s:%d\n",
1443 sx->lock_object.lo_name, file,
1445 } else if (what & SA_RECURSED)
1446 panic("Lock %s not recursed @ %s:%d\n",
1447 sx->lock_object.lo_name, file, line);
1452 case SA_XLOCKED | SA_NOTRECURSED:
1453 case SA_XLOCKED | SA_RECURSED:
1454 if (sx_xholder(sx) != curthread)
1455 panic("Lock %s not exclusively locked @ %s:%d\n",
1456 sx->lock_object.lo_name, file, line);
1457 if (sx_recursed(sx)) {
1458 if (what & SA_NOTRECURSED)
1459 panic("Lock %s recursed @ %s:%d\n",
1460 sx->lock_object.lo_name, file, line);
1461 } else if (what & SA_RECURSED)
1462 panic("Lock %s not recursed @ %s:%d\n",
1463 sx->lock_object.lo_name, file, line);
1467 witness_assert(&sx->lock_object, what, file, line);
1470 * If we hold an exclusve lock fail. We can't
1471 * reliably check to see if we hold a shared lock or
1474 if (sx_xholder(sx) == curthread)
1475 panic("Lock %s exclusively locked @ %s:%d\n",
1476 sx->lock_object.lo_name, file, line);
1480 panic("Unknown sx lock assertion: %d @ %s:%d", what, file,
1484 #endif /* INVARIANT_SUPPORT */
1488 db_show_sx(const struct lock_object *lock)
1491 const struct sx *sx;
1493 sx = (const struct sx *)lock;
1495 db_printf(" state: ");
1496 if (sx->sx_lock == SX_LOCK_UNLOCKED)
1497 db_printf("UNLOCKED\n");
1498 else if (sx->sx_lock == SX_LOCK_DESTROYED) {
1499 db_printf("DESTROYED\n");
1501 } else if (sx->sx_lock & SX_LOCK_SHARED)
1502 db_printf("SLOCK: %ju\n", (uintmax_t)SX_SHARERS(sx->sx_lock));
1504 td = sx_xholder(sx);
1505 db_printf("XLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1506 td->td_tid, td->td_proc->p_pid, td->td_name);
1507 if (sx_recursed(sx))
1508 db_printf(" recursed: %d\n", sx->sx_recurse);
1511 db_printf(" waiters: ");
1512 switch(sx->sx_lock &
1513 (SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS)) {
1514 case SX_LOCK_SHARED_WAITERS:
1515 db_printf("shared\n");
1517 case SX_LOCK_EXCLUSIVE_WAITERS:
1518 db_printf("exclusive\n");
1520 case SX_LOCK_SHARED_WAITERS | SX_LOCK_EXCLUSIVE_WAITERS:
1521 db_printf("exclusive and shared\n");
1524 db_printf("none\n");
1529 * Check to see if a thread that is blocked on a sleep queue is actually
1530 * blocked on an sx lock. If so, output some details and return true.
1531 * If the lock has an exclusive owner, return that in *ownerp.
1534 sx_chain(struct thread *td, struct thread **ownerp)
1536 const struct sx *sx;
1539 * Check to see if this thread is blocked on an sx lock.
1540 * First, we check the lock class. If that is ok, then we
1541 * compare the lock name against the wait message.
1544 if (LOCK_CLASS(&sx->lock_object) != &lock_class_sx ||
1545 sx->lock_object.lo_name != td->td_wmesg)
1548 /* We think we have an sx lock, so output some details. */
1549 db_printf("blocked on sx \"%s\" ", td->td_wmesg);
1550 *ownerp = sx_xholder(sx);
1551 if (sx->sx_lock & SX_LOCK_SHARED)
1552 db_printf("SLOCK (count %ju)\n",
1553 (uintmax_t)SX_SHARERS(sx->sx_lock));
1555 db_printf("XLOCK\n");