2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2006 John Baldwin <jhb@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, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Machine independent bits of reader/writer lock implementation.
32 #include <sys/cdefs.h>
34 #include "opt_hwpmc_hooks.h"
35 #include "opt_no_adaptive_rwlocks.h"
37 #include <sys/param.h>
40 #include <sys/kernel.h>
42 #include <sys/mutex.h>
44 #include <sys/rwlock.h>
45 #include <sys/sched.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/turnstile.h>
51 #include <machine/cpu.h>
53 #if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
54 #define ADAPTIVE_RWLOCKS
58 #include <sys/pmckern.h>
59 PMC_SOFT_DECLARE( , , lock, failed);
63 * Return the rwlock address when the lock cookie address is provided.
64 * This functionality assumes that struct rwlock* have a member named rw_lock.
66 #define rwlock2rw(c) (__containerof(c, struct rwlock, rw_lock))
71 static void db_show_rwlock(const struct lock_object *lock);
73 static void assert_rw(const struct lock_object *lock, int what);
74 static void lock_rw(struct lock_object *lock, uintptr_t how);
76 static int owner_rw(const struct lock_object *lock, struct thread **owner);
78 static uintptr_t unlock_rw(struct lock_object *lock);
80 struct lock_class lock_class_rw = {
82 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
83 .lc_assert = assert_rw,
85 .lc_ddb_show = db_show_rwlock,
88 .lc_unlock = unlock_rw,
94 #ifdef ADAPTIVE_RWLOCKS
95 #ifdef RWLOCK_CUSTOM_BACKOFF
96 static u_short __read_frequently rowner_retries;
97 static u_short __read_frequently rowner_loops;
98 static SYSCTL_NODE(_debug, OID_AUTO, rwlock,
99 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
101 SYSCTL_U16(_debug_rwlock, OID_AUTO, retry, CTLFLAG_RW, &rowner_retries, 0, "");
102 SYSCTL_U16(_debug_rwlock, OID_AUTO, loops, CTLFLAG_RW, &rowner_loops, 0, "");
104 static struct lock_delay_config __read_frequently rw_delay;
106 SYSCTL_U16(_debug_rwlock, OID_AUTO, delay_base, CTLFLAG_RW, &rw_delay.base,
108 SYSCTL_U16(_debug_rwlock, OID_AUTO, delay_max, CTLFLAG_RW, &rw_delay.max,
112 rw_lock_delay_init(void *arg __unused)
115 lock_delay_default_init(&rw_delay);
117 rowner_loops = max(10000, rw_delay.max);
119 LOCK_DELAY_SYSINIT(rw_lock_delay_init);
121 #define rw_delay locks_delay
122 #define rowner_retries locks_delay_retries
123 #define rowner_loops locks_delay_loops
128 * Return a pointer to the owning thread if the lock is write-locked or
129 * NULL if the lock is unlocked or read-locked.
132 #define lv_rw_wowner(v) \
133 ((v) & RW_LOCK_READ ? NULL : \
134 (struct thread *)RW_OWNER((v)))
136 #define rw_wowner(rw) lv_rw_wowner(RW_READ_VALUE(rw))
139 * Returns if a write owner is recursed. Write ownership is not assured
140 * here and should be previously checked.
142 #define rw_recursed(rw) ((rw)->rw_recurse != 0)
145 * Return true if curthread helds the lock.
147 #define rw_wlocked(rw) (rw_wowner((rw)) == curthread)
150 * Return a pointer to the owning thread for this lock who should receive
151 * any priority lent by threads that block on this lock. Currently this
152 * is identical to rw_wowner().
154 #define rw_owner(rw) rw_wowner(rw)
157 #define __rw_assert(c, what, file, line)
161 assert_rw(const struct lock_object *lock, int what)
164 rw_assert((const struct rwlock *)lock, what);
168 lock_rw(struct lock_object *lock, uintptr_t how)
172 rw = (struct rwlock *)lock;
180 unlock_rw(struct lock_object *lock)
184 rw = (struct rwlock *)lock;
185 rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
186 if (rw->rw_lock & RW_LOCK_READ) {
197 owner_rw(const struct lock_object *lock, struct thread **owner)
199 const struct rwlock *rw = (const struct rwlock *)lock;
200 uintptr_t x = rw->rw_lock;
202 *owner = rw_wowner(rw);
203 return ((x & RW_LOCK_READ) != 0 ? (RW_READERS(x) != 0) :
209 _rw_init_flags(volatile uintptr_t *c, const char *name, int opts)
216 MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
217 RW_RECURSE | RW_NEW)) == 0);
218 ASSERT_ATOMIC_LOAD_PTR(rw->rw_lock,
219 ("%s: rw_lock not aligned for %s: %p", __func__, name,
222 flags = LO_UPGRADABLE;
225 if (opts & RW_NOPROFILE)
226 flags |= LO_NOPROFILE;
227 if (!(opts & RW_NOWITNESS))
229 if (opts & RW_RECURSE)
230 flags |= LO_RECURSABLE;
236 lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
237 rw->rw_lock = RW_UNLOCKED;
242 _rw_destroy(volatile uintptr_t *c)
248 KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock %p not unlocked", rw));
249 KASSERT(rw->rw_recurse == 0, ("rw lock %p still recursed", rw));
250 rw->rw_lock = RW_DESTROYED;
251 lock_destroy(&rw->lock_object);
255 rw_sysinit(void *arg)
257 struct rw_args *args;
260 rw_init_flags((struct rwlock *)args->ra_rw, args->ra_desc,
265 _rw_wowned(const volatile uintptr_t *c)
268 return (rw_wowner(rwlock2rw(c)) == curthread);
272 _rw_wlock_cookie(volatile uintptr_t *c, const char *file, int line)
279 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
280 !TD_IS_IDLETHREAD(curthread),
281 ("rw_wlock() by idle thread %p on rwlock %s @ %s:%d",
282 curthread, rw->lock_object.lo_name, file, line));
283 KASSERT(rw->rw_lock != RW_DESTROYED,
284 ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
285 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
287 tid = (uintptr_t)curthread;
289 if (!_rw_write_lock_fetch(rw, &v, tid))
290 _rw_wlock_hard(rw, v, file, line);
292 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire, rw,
293 0, 0, file, line, LOCKSTAT_WRITER);
295 LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
296 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
297 TD_LOCKS_INC(curthread);
301 __rw_try_wlock_int(struct rwlock *rw LOCK_FILE_LINE_ARG_DEF)
310 if (SCHEDULER_STOPPED_TD(td))
313 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
314 ("rw_try_wlock() by idle thread %p on rwlock %s @ %s:%d",
315 curthread, rw->lock_object.lo_name, file, line));
316 KASSERT(rw->rw_lock != RW_DESTROYED,
317 ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
323 if (atomic_fcmpset_acq_ptr(&rw->rw_lock, &v, tid))
325 if (v == RW_UNLOCKED)
327 if (v == tid && (rw->lock_object.lo_flags & LO_RECURSABLE)) {
329 atomic_set_ptr(&rw->rw_lock, RW_LOCK_WRITER_RECURSED);
336 LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
338 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
341 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire,
342 rw, 0, 0, file, line, LOCKSTAT_WRITER);
343 TD_LOCKS_INC(curthread);
349 __rw_try_wlock(volatile uintptr_t *c, const char *file, int line)
354 return (__rw_try_wlock_int(rw LOCK_FILE_LINE_ARG));
358 _rw_wunlock_cookie(volatile uintptr_t *c, const char *file, int line)
364 KASSERT(rw->rw_lock != RW_DESTROYED,
365 ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
366 __rw_assert(c, RA_WLOCKED, file, line);
367 WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
368 LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
371 #ifdef LOCK_PROFILING
372 _rw_wunlock_hard(rw, (uintptr_t)curthread, file, line);
374 __rw_wunlock(rw, curthread, file, line);
377 TD_LOCKS_DEC(curthread);
381 * Determines whether a new reader can acquire a lock. Succeeds if the
382 * reader already owns a read lock and the lock is locked for read to
383 * prevent deadlock from reader recursion. Also succeeds if the lock
384 * is unlocked and has no writer waiters or spinners. Failing otherwise
385 * prioritizes writers before readers.
387 static bool __always_inline
388 __rw_can_read(struct thread *td, uintptr_t v, bool fp)
391 if ((v & (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER))
394 if (!fp && td->td_rw_rlocks && (v & RW_LOCK_READ))
399 static bool __always_inline
400 __rw_rlock_try(struct rwlock *rw, struct thread *td, uintptr_t *vp, bool fp
401 LOCK_FILE_LINE_ARG_DEF)
405 * Handle the easy case. If no other thread has a write
406 * lock, then try to bump up the count of read locks. Note
407 * that we have to preserve the current state of the
408 * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a
409 * read lock, then rw_lock must have changed, so restart
410 * the loop. Note that this handles the case of a
411 * completely unlocked rwlock since such a lock is encoded
412 * as a read lock with no waiters.
414 while (__rw_can_read(td, *vp, fp)) {
415 if (atomic_fcmpset_acq_ptr(&rw->rw_lock, vp,
416 *vp + RW_ONE_READER)) {
417 if (LOCK_LOG_TEST(&rw->lock_object, 0))
419 "%s: %p succeed %p -> %p", __func__,
421 (void *)(*vp + RW_ONE_READER));
429 static void __noinline
430 __rw_rlock_hard(struct rwlock *rw, struct thread *td, uintptr_t v
431 LOCK_FILE_LINE_ARG_DEF)
433 struct turnstile *ts;
434 struct thread *owner;
435 #ifdef ADAPTIVE_RWLOCKS
439 #ifdef LOCK_PROFILING
440 uint64_t waittime = 0;
443 #if defined(ADAPTIVE_RWLOCKS) || defined(KDTRACE_HOOKS)
444 struct lock_delay_arg lda;
448 int64_t sleep_time = 0;
449 int64_t all_time = 0;
451 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
453 int doing_lockprof = 0;
457 if (LOCKSTAT_PROFILE_ENABLED(rw__acquire)) {
458 if (__rw_rlock_try(rw, td, &v, false LOCK_FILE_LINE_ARG))
461 all_time -= lockstat_nsecs(&rw->lock_object);
465 #ifdef LOCK_PROFILING
470 if (SCHEDULER_STOPPED())
473 #if defined(ADAPTIVE_RWLOCKS)
474 lock_delay_arg_init(&lda, &rw_delay);
475 #elif defined(KDTRACE_HOOKS)
476 lock_delay_arg_init_noadapt(&lda);
480 PMC_SOFT_CALL( , , lock, failed);
482 lock_profile_obtain_lock_failed(&rw->lock_object, false,
483 &contested, &waittime);
485 THREAD_CONTENDS_ON_LOCK(&rw->lock_object);
488 if (__rw_rlock_try(rw, td, &v, false LOCK_FILE_LINE_ARG))
494 #ifdef ADAPTIVE_RWLOCKS
496 * If the owner is running on another CPU, spin until
497 * the owner stops running or the state of the lock
500 if ((v & RW_LOCK_READ) == 0) {
501 owner = (struct thread *)RW_OWNER(v);
502 if (TD_IS_RUNNING(owner)) {
503 if (LOCK_LOG_TEST(&rw->lock_object, 0))
505 "%s: spinning on %p held by %p",
506 __func__, rw, owner);
507 KTR_STATE1(KTR_SCHED, "thread",
508 sched_tdname(curthread), "spinning",
509 "lockname:\"%s\"", rw->lock_object.lo_name);
512 v = RW_READ_VALUE(rw);
513 owner = lv_rw_wowner(v);
514 } while (owner != NULL && TD_IS_RUNNING(owner));
515 KTR_STATE0(KTR_SCHED, "thread",
516 sched_tdname(curthread), "running");
520 if ((v & RW_LOCK_WRITE_SPINNER) && RW_READERS(v) == 0) {
521 MPASS(!__rw_can_read(td, v, false));
523 v = RW_READ_VALUE(rw);
526 if (spintries < rowner_retries) {
528 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
529 "spinning", "lockname:\"%s\"",
530 rw->lock_object.lo_name);
532 for (i = 0; i < rowner_loops; i += n) {
534 v = RW_READ_VALUE(rw);
535 if (!(v & RW_LOCK_READ))
540 if (__rw_can_read(td, v, false))
544 lda.spin_cnt += rowner_loops - i;
546 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
548 if (i < rowner_loops)
555 * Okay, now it's the hard case. Some other thread already
556 * has a write lock or there are write waiters present,
557 * acquire the turnstile lock so we can begin the process
560 ts = turnstile_trywait(&rw->lock_object);
563 * The lock might have been released while we spun, so
564 * recheck its state and restart the loop if needed.
566 v = RW_READ_VALUE(rw);
568 if (((v & RW_LOCK_WRITE_SPINNER) && RW_READERS(v) == 0) ||
569 __rw_can_read(td, v, false)) {
570 turnstile_cancel(ts);
574 owner = lv_rw_wowner(v);
576 #ifdef ADAPTIVE_RWLOCKS
578 * The current lock owner might have started executing
579 * on another CPU (or the lock could have changed
580 * owners) while we were waiting on the turnstile
581 * chain lock. If so, drop the turnstile lock and try
585 if (TD_IS_RUNNING(owner)) {
586 turnstile_cancel(ts);
593 * The lock is held in write mode or it already has waiters.
595 MPASS(!__rw_can_read(td, v, false));
598 * If the RW_LOCK_READ_WAITERS flag is already set, then
599 * we can go ahead and block. If it is not set then try
600 * to set it. If we fail to set it drop the turnstile
601 * lock and restart the loop.
603 if (!(v & RW_LOCK_READ_WAITERS)) {
604 if (!atomic_fcmpset_ptr(&rw->rw_lock, &v,
605 v | RW_LOCK_READ_WAITERS))
607 if (LOCK_LOG_TEST(&rw->lock_object, 0))
608 CTR2(KTR_LOCK, "%s: %p set read waiters flag",
613 * We were unable to acquire the lock and the read waiters
614 * flag is set, so we must block on the turnstile.
616 if (LOCK_LOG_TEST(&rw->lock_object, 0))
617 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
620 sleep_time -= lockstat_nsecs(&rw->lock_object);
622 MPASS(owner == rw_owner(rw));
623 turnstile_wait(ts, owner, TS_SHARED_QUEUE);
625 sleep_time += lockstat_nsecs(&rw->lock_object);
628 if (LOCK_LOG_TEST(&rw->lock_object, 0))
629 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
631 v = RW_READ_VALUE(rw);
633 THREAD_CONTENTION_DONE(&rw->lock_object);
634 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
635 if (__predict_true(!doing_lockprof))
639 all_time += lockstat_nsecs(&rw->lock_object);
641 LOCKSTAT_RECORD4(rw__block, rw, sleep_time,
642 LOCKSTAT_READER, (state & RW_LOCK_READ) == 0,
643 (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
645 /* Record only the loops spinning and not sleeping. */
646 if (lda.spin_cnt > sleep_cnt)
647 LOCKSTAT_RECORD4(rw__spin, rw, all_time - sleep_time,
648 LOCKSTAT_READER, (state & RW_LOCK_READ) == 0,
649 (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
653 * TODO: acquire "owner of record" here. Here be turnstile dragons
654 * however. turnstiles don't like owners changing between calls to
655 * turnstile_wait() currently.
657 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire, rw, contested,
658 waittime, file, line, LOCKSTAT_READER);
662 __rw_rlock_int(struct rwlock *rw LOCK_FILE_LINE_ARG_DEF)
669 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED_TD(td) ||
670 !TD_IS_IDLETHREAD(td),
671 ("rw_rlock() by idle thread %p on rwlock %s @ %s:%d",
672 td, rw->lock_object.lo_name, file, line));
673 KASSERT(rw->rw_lock != RW_DESTROYED,
674 ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
675 KASSERT(rw_wowner(rw) != td,
676 ("rw_rlock: wlock already held for %s @ %s:%d",
677 rw->lock_object.lo_name, file, line));
678 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line, NULL);
680 v = RW_READ_VALUE(rw);
681 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(rw__acquire) ||
682 !__rw_rlock_try(rw, td, &v, true LOCK_FILE_LINE_ARG)))
683 __rw_rlock_hard(rw, td, v LOCK_FILE_LINE_ARG);
685 lock_profile_obtain_lock_success(&rw->lock_object, false, 0, 0,
688 LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
689 WITNESS_LOCK(&rw->lock_object, 0, file, line);
690 TD_LOCKS_INC(curthread);
694 __rw_rlock(volatile uintptr_t *c, const char *file, int line)
699 __rw_rlock_int(rw LOCK_FILE_LINE_ARG);
703 __rw_try_rlock_int(struct rwlock *rw LOCK_FILE_LINE_ARG_DEF)
707 if (SCHEDULER_STOPPED())
710 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
711 ("rw_try_rlock() by idle thread %p on rwlock %s @ %s:%d",
712 curthread, rw->lock_object.lo_name, file, line));
716 KASSERT(rw->rw_lock != RW_DESTROYED,
717 ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
718 if (!(x & RW_LOCK_READ))
720 if (atomic_fcmpset_acq_ptr(&rw->rw_lock, &x, x + RW_ONE_READER)) {
721 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
723 WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
724 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire,
725 rw, 0, 0, file, line, LOCKSTAT_READER);
726 TD_LOCKS_INC(curthread);
727 curthread->td_rw_rlocks++;
732 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
737 __rw_try_rlock(volatile uintptr_t *c, const char *file, int line)
742 return (__rw_try_rlock_int(rw LOCK_FILE_LINE_ARG));
745 static bool __always_inline
746 __rw_runlock_try(struct rwlock *rw, struct thread *td, uintptr_t *vp)
750 if (RW_READERS(*vp) > 1 || !(*vp & RW_LOCK_WAITERS)) {
751 if (atomic_fcmpset_rel_ptr(&rw->rw_lock, vp,
752 *vp - RW_ONE_READER)) {
753 if (LOCK_LOG_TEST(&rw->lock_object, 0))
755 "%s: %p succeeded %p -> %p",
756 __func__, rw, (void *)*vp,
757 (void *)(*vp - RW_ONE_READER));
768 static void __noinline
769 __rw_runlock_hard(struct rwlock *rw, struct thread *td, uintptr_t v
770 LOCK_FILE_LINE_ARG_DEF)
772 struct turnstile *ts;
773 uintptr_t setv, queue;
775 if (SCHEDULER_STOPPED())
778 if (__rw_runlock_try(rw, td, &v))
782 * Ok, we know we have waiters and we think we are the
783 * last reader, so grab the turnstile lock.
785 turnstile_chain_lock(&rw->lock_object);
786 v = RW_READ_VALUE(rw);
788 if (__rw_runlock_try(rw, td, &v))
791 MPASS(v & RW_LOCK_WAITERS);
794 * Try to drop our lock leaving the lock in a unlocked
797 * If you wanted to do explicit lock handoff you'd have to
798 * do it here. You'd also want to use turnstile_signal()
799 * and you'd have to handle the race where a higher
800 * priority thread blocks on the write lock before the
801 * thread you wakeup actually runs and have the new thread
802 * "steal" the lock. For now it's a lot simpler to just
803 * wakeup all of the waiters.
805 * As above, if we fail, then another thread might have
806 * acquired a read lock, so drop the turnstile lock and
810 queue = TS_SHARED_QUEUE;
811 if (v & RW_LOCK_WRITE_WAITERS) {
812 queue = TS_EXCLUSIVE_QUEUE;
813 setv |= (v & RW_LOCK_READ_WAITERS);
815 setv |= (v & RW_LOCK_WRITE_SPINNER);
816 if (!atomic_fcmpset_rel_ptr(&rw->rw_lock, &v, setv))
818 if (LOCK_LOG_TEST(&rw->lock_object, 0))
819 CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
823 * Ok. The lock is released and all that's left is to
824 * wake up the waiters. Note that the lock might not be
825 * free anymore, but in that case the writers will just
826 * block again if they run before the new lock holder(s)
829 ts = turnstile_lookup(&rw->lock_object);
831 turnstile_broadcast(ts, queue);
832 turnstile_unpend(ts);
836 turnstile_chain_unlock(&rw->lock_object);
838 LOCKSTAT_PROFILE_RELEASE_RWLOCK(rw__release, rw, LOCKSTAT_READER);
842 _rw_runlock_cookie_int(struct rwlock *rw LOCK_FILE_LINE_ARG_DEF)
847 KASSERT(rw->rw_lock != RW_DESTROYED,
848 ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
849 __rw_assert(&rw->rw_lock, RA_RLOCKED, file, line);
850 WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
851 LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
854 v = RW_READ_VALUE(rw);
856 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(rw__release) ||
857 !__rw_runlock_try(rw, td, &v)))
858 __rw_runlock_hard(rw, td, v LOCK_FILE_LINE_ARG);
860 lock_profile_release_lock(&rw->lock_object, false);
862 TD_LOCKS_DEC(curthread);
866 _rw_runlock_cookie(volatile uintptr_t *c, const char *file, int line)
871 _rw_runlock_cookie_int(rw LOCK_FILE_LINE_ARG);
874 #ifdef ADAPTIVE_RWLOCKS
876 rw_drop_critical(uintptr_t v, bool *in_critical, int *extra_work)
879 if (v & RW_LOCK_WRITE_SPINNER)
883 *in_critical = false;
888 #define rw_drop_critical(v, in_critical, extra_work) do { } while (0)
892 * This function is called when we are unable to obtain a write lock on the
893 * first try. This means that at least one other thread holds either a
894 * read or write lock.
897 __rw_wlock_hard(volatile uintptr_t *c, uintptr_t v LOCK_FILE_LINE_ARG_DEF)
901 struct turnstile *ts;
902 struct thread *owner;
903 #ifdef ADAPTIVE_RWLOCKS
906 enum { READERS, WRITER } sleep_reason = READERS;
907 bool in_critical = false;
910 #ifdef LOCK_PROFILING
911 uint64_t waittime = 0;
914 #if defined(ADAPTIVE_RWLOCKS) || defined(KDTRACE_HOOKS)
915 struct lock_delay_arg lda;
919 int64_t sleep_time = 0;
920 int64_t all_time = 0;
922 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
924 int doing_lockprof = 0;
928 tid = (uintptr_t)curthread;
932 if (LOCKSTAT_PROFILE_ENABLED(rw__acquire)) {
933 while (v == RW_UNLOCKED) {
934 if (_rw_write_lock_fetch(rw, &v, tid))
939 all_time -= lockstat_nsecs(&rw->lock_object);
943 #ifdef LOCK_PROFILING
949 if (SCHEDULER_STOPPED())
952 if (__predict_false(v == RW_UNLOCKED))
953 v = RW_READ_VALUE(rw);
955 if (__predict_false(lv_rw_wowner(v) == (struct thread *)tid)) {
956 KASSERT(rw->lock_object.lo_flags & LO_RECURSABLE,
957 ("%s: recursing but non-recursive rw %s @ %s:%d\n",
958 __func__, rw->lock_object.lo_name, file, line));
960 atomic_set_ptr(&rw->rw_lock, RW_LOCK_WRITER_RECURSED);
961 if (LOCK_LOG_TEST(&rw->lock_object, 0))
962 CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
966 if (LOCK_LOG_TEST(&rw->lock_object, 0))
967 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
968 rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
970 #if defined(ADAPTIVE_RWLOCKS)
971 lock_delay_arg_init(&lda, &rw_delay);
972 #elif defined(KDTRACE_HOOKS)
973 lock_delay_arg_init_noadapt(&lda);
977 PMC_SOFT_CALL( , , lock, failed);
979 lock_profile_obtain_lock_failed(&rw->lock_object, false,
980 &contested, &waittime);
982 THREAD_CONTENDS_ON_LOCK(&rw->lock_object);
985 if (v == RW_UNLOCKED) {
986 if (_rw_write_lock_fetch(rw, &v, tid))
994 #ifdef ADAPTIVE_RWLOCKS
995 if (v == (RW_LOCK_READ | RW_LOCK_WRITE_SPINNER)) {
996 if (atomic_fcmpset_acq_ptr(&rw->rw_lock, &v, tid))
1002 * If the lock is write locked and the owner is
1003 * running on another CPU, spin until the owner stops
1004 * running or the state of the lock changes.
1006 if (!(v & RW_LOCK_READ)) {
1007 rw_drop_critical(v, &in_critical, &extra_work);
1008 sleep_reason = WRITER;
1009 owner = lv_rw_wowner(v);
1010 if (!TD_IS_RUNNING(owner))
1012 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1013 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
1014 __func__, rw, owner);
1015 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
1016 "spinning", "lockname:\"%s\"",
1017 rw->lock_object.lo_name);
1020 v = RW_READ_VALUE(rw);
1021 owner = lv_rw_wowner(v);
1022 } while (owner != NULL && TD_IS_RUNNING(owner));
1023 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
1026 } else if (RW_READERS(v) > 0) {
1027 sleep_reason = READERS;
1028 if (spintries == rowner_retries)
1030 if (!(v & RW_LOCK_WRITE_SPINNER)) {
1036 if (!atomic_fcmpset_ptr(&rw->rw_lock, &v,
1037 v | RW_LOCK_WRITE_SPINNER)) {
1039 in_critical = false;
1045 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
1046 "spinning", "lockname:\"%s\"",
1047 rw->lock_object.lo_name);
1049 for (i = 0; i < rowner_loops; i += n) {
1051 v = RW_READ_VALUE(rw);
1052 if (!(v & RW_LOCK_WRITE_SPINNER))
1054 if (!(v & RW_LOCK_READ))
1060 #ifdef KDTRACE_HOOKS
1063 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
1065 if (i < rowner_loops)
1070 ts = turnstile_trywait(&rw->lock_object);
1071 v = RW_READ_VALUE(rw);
1073 owner = lv_rw_wowner(v);
1075 #ifdef ADAPTIVE_RWLOCKS
1077 * The current lock owner might have started executing
1078 * on another CPU (or the lock could have changed
1079 * owners) while we were waiting on the turnstile
1080 * chain lock. If so, drop the turnstile lock and try
1083 if (owner != NULL) {
1084 if (TD_IS_RUNNING(owner)) {
1085 turnstile_cancel(ts);
1086 rw_drop_critical(v, &in_critical, &extra_work);
1089 } else if (RW_READERS(v) > 0 && sleep_reason == WRITER) {
1090 turnstile_cancel(ts);
1091 rw_drop_critical(v, &in_critical, &extra_work);
1096 * Check for the waiters flags about this rwlock.
1097 * If the lock was released, without maintain any pending
1098 * waiters queue, simply try to acquire it.
1099 * If a pending waiters queue is present, claim the lock
1100 * ownership and maintain the pending queue.
1102 setv = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
1103 if ((v & ~setv) == RW_UNLOCKED) {
1104 setv &= ~RW_LOCK_WRITE_SPINNER;
1105 if (atomic_fcmpset_acq_ptr(&rw->rw_lock, &v, tid | setv)) {
1107 turnstile_claim(ts);
1109 turnstile_cancel(ts);
1115 #ifdef ADAPTIVE_RWLOCKS
1117 if ((v & RW_LOCK_WRITE_SPINNER) ||
1118 !((v & RW_LOCK_WRITE_WAITERS))) {
1119 setv = v & ~RW_LOCK_WRITE_SPINNER;
1120 setv |= RW_LOCK_WRITE_WAITERS;
1121 if (!atomic_fcmpset_ptr(&rw->rw_lock, &v, setv))
1125 in_critical = false;
1130 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
1131 * set it. If we fail to set it, then loop back and try
1134 if (!(v & RW_LOCK_WRITE_WAITERS)) {
1135 if (!atomic_fcmpset_ptr(&rw->rw_lock, &v,
1136 v | RW_LOCK_WRITE_WAITERS))
1138 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1139 CTR2(KTR_LOCK, "%s: %p set write waiters flag",
1142 #ifdef ADAPTIVE_RWLOCKS
1146 * We were unable to acquire the lock and the write waiters
1147 * flag is set, so we must block on the turnstile.
1149 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1150 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
1152 #ifdef KDTRACE_HOOKS
1153 sleep_time -= lockstat_nsecs(&rw->lock_object);
1155 MPASS(owner == rw_owner(rw));
1156 turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
1157 #ifdef KDTRACE_HOOKS
1158 sleep_time += lockstat_nsecs(&rw->lock_object);
1161 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1162 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
1164 #ifdef ADAPTIVE_RWLOCKS
1167 v = RW_READ_VALUE(rw);
1169 THREAD_CONTENTION_DONE(&rw->lock_object);
1170 if (__predict_true(!extra_work))
1172 #ifdef ADAPTIVE_RWLOCKS
1176 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
1177 if (__predict_true(!doing_lockprof))
1180 #ifdef KDTRACE_HOOKS
1181 all_time += lockstat_nsecs(&rw->lock_object);
1183 LOCKSTAT_RECORD4(rw__block, rw, sleep_time,
1184 LOCKSTAT_WRITER, (state & RW_LOCK_READ) == 0,
1185 (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
1187 /* Record only the loops spinning and not sleeping. */
1188 if (lda.spin_cnt > sleep_cnt)
1189 LOCKSTAT_RECORD4(rw__spin, rw, all_time - sleep_time,
1190 LOCKSTAT_WRITER, (state & RW_LOCK_READ) == 0,
1191 (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
1194 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire, rw, contested,
1195 waittime, file, line, LOCKSTAT_WRITER);
1199 * This function is called if lockstat is active or the first try at releasing
1200 * a write lock failed. The latter means that the lock is recursed or one of
1201 * the 2 waiter bits must be set indicating that at least one thread is waiting
1205 __rw_wunlock_hard(volatile uintptr_t *c, uintptr_t v LOCK_FILE_LINE_ARG_DEF)
1208 struct turnstile *ts;
1209 uintptr_t tid, setv;
1212 tid = (uintptr_t)curthread;
1213 if (SCHEDULER_STOPPED())
1217 if (__predict_false(v == tid))
1218 v = RW_READ_VALUE(rw);
1220 if (v & RW_LOCK_WRITER_RECURSED) {
1221 if (--(rw->rw_recurse) == 0)
1222 atomic_clear_ptr(&rw->rw_lock, RW_LOCK_WRITER_RECURSED);
1223 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1224 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
1228 LOCKSTAT_PROFILE_RELEASE_RWLOCK(rw__release, rw, LOCKSTAT_WRITER);
1229 if (v == tid && _rw_write_unlock(rw, tid))
1232 KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
1233 ("%s: neither of the waiter flags are set", __func__));
1235 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1236 CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
1238 turnstile_chain_lock(&rw->lock_object);
1241 * Use the same algo as sx locks for now. Prefer waking up shared
1242 * waiters if we have any over writers. This is probably not ideal.
1244 * 'v' is the value we are going to write back to rw_lock. If we
1245 * have waiters on both queues, we need to preserve the state of
1246 * the waiter flag for the queue we don't wake up. For now this is
1247 * hardcoded for the algorithm mentioned above.
1249 * In the case of both readers and writers waiting we wakeup the
1250 * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a
1251 * new writer comes in before a reader it will claim the lock up
1252 * above. There is probably a potential priority inversion in
1253 * there that could be worked around either by waking both queues
1254 * of waiters or doing some complicated lock handoff gymnastics.
1257 v = RW_READ_VALUE(rw);
1258 queue = TS_SHARED_QUEUE;
1259 if (v & RW_LOCK_WRITE_WAITERS) {
1260 queue = TS_EXCLUSIVE_QUEUE;
1261 setv |= (v & RW_LOCK_READ_WAITERS);
1263 atomic_store_rel_ptr(&rw->rw_lock, setv);
1265 /* Wake up all waiters for the specific queue. */
1266 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1267 CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
1268 queue == TS_SHARED_QUEUE ? "read" : "write");
1270 ts = turnstile_lookup(&rw->lock_object);
1272 turnstile_broadcast(ts, queue);
1273 turnstile_unpend(ts);
1274 turnstile_chain_unlock(&rw->lock_object);
1278 * Attempt to do a non-blocking upgrade from a read lock to a write
1279 * lock. This will only succeed if this thread holds a single read
1280 * lock. Returns true if the upgrade succeeded and false otherwise.
1283 __rw_try_upgrade_int(struct rwlock *rw LOCK_FILE_LINE_ARG_DEF)
1285 uintptr_t v, setv, tid;
1286 struct turnstile *ts;
1289 if (SCHEDULER_STOPPED())
1292 KASSERT(rw->rw_lock != RW_DESTROYED,
1293 ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
1294 __rw_assert(&rw->rw_lock, RA_RLOCKED, file, line);
1297 * Attempt to switch from one reader to a writer. If there
1298 * are any write waiters, then we will have to lock the
1299 * turnstile first to prevent races with another writer
1300 * calling turnstile_wait() before we have claimed this
1301 * turnstile. So, do the simple case of no waiters first.
1303 tid = (uintptr_t)curthread;
1305 v = RW_READ_VALUE(rw);
1307 if (RW_READERS(v) > 1)
1309 if (!(v & RW_LOCK_WAITERS)) {
1310 success = atomic_fcmpset_acq_ptr(&rw->rw_lock, &v, tid);
1317 * Ok, we think we have waiters, so lock the turnstile.
1319 ts = turnstile_trywait(&rw->lock_object);
1320 v = RW_READ_VALUE(rw);
1322 if (RW_READERS(v) > 1) {
1323 turnstile_cancel(ts);
1327 * Try to switch from one reader to a writer again. This time
1328 * we honor the current state of the waiters flags.
1329 * If we obtain the lock with the flags set, then claim
1330 * ownership of the turnstile.
1332 setv = tid | (v & RW_LOCK_WAITERS);
1333 success = atomic_fcmpset_ptr(&rw->rw_lock, &v, setv);
1335 if (v & RW_LOCK_WAITERS)
1336 turnstile_claim(ts);
1338 turnstile_cancel(ts);
1343 LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
1345 curthread->td_rw_rlocks--;
1346 WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
1348 LOCKSTAT_RECORD0(rw__upgrade, rw);
1354 __rw_try_upgrade(volatile uintptr_t *c, const char *file, int line)
1359 return (__rw_try_upgrade_int(rw LOCK_FILE_LINE_ARG));
1363 * Downgrade a write lock into a single read lock.
1366 __rw_downgrade_int(struct rwlock *rw LOCK_FILE_LINE_ARG_DEF)
1368 struct turnstile *ts;
1372 if (SCHEDULER_STOPPED())
1375 KASSERT(rw->rw_lock != RW_DESTROYED,
1376 ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
1377 __rw_assert(&rw->rw_lock, RA_WLOCKED | RA_NOTRECURSED, file, line);
1379 if (rw_recursed(rw))
1380 panic("downgrade of a recursed lock");
1383 WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
1386 * Convert from a writer to a single reader. First we handle
1387 * the easy case with no waiters. If there are any waiters, we
1388 * lock the turnstile and "disown" the lock.
1390 tid = (uintptr_t)curthread;
1391 if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
1395 * Ok, we think we have waiters, so lock the turnstile so we can
1396 * read the waiter flags without any races.
1398 turnstile_chain_lock(&rw->lock_object);
1399 v = rw->rw_lock & RW_LOCK_WAITERS;
1400 rwait = v & RW_LOCK_READ_WAITERS;
1401 wwait = v & RW_LOCK_WRITE_WAITERS;
1402 MPASS(rwait | wwait);
1405 * Downgrade from a write lock while preserving waiters flag
1406 * and give up ownership of the turnstile.
1408 ts = turnstile_lookup(&rw->lock_object);
1411 v &= ~RW_LOCK_READ_WAITERS;
1412 atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
1414 * Wake other readers if there are no writers pending. Otherwise they
1415 * won't be able to acquire the lock anyway.
1417 if (rwait && !wwait) {
1418 turnstile_broadcast(ts, TS_SHARED_QUEUE);
1419 turnstile_unpend(ts);
1421 turnstile_disown(ts);
1422 turnstile_chain_unlock(&rw->lock_object);
1424 curthread->td_rw_rlocks++;
1425 LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
1426 LOCKSTAT_RECORD0(rw__downgrade, rw);
1430 __rw_downgrade(volatile uintptr_t *c, const char *file, int line)
1435 __rw_downgrade_int(rw LOCK_FILE_LINE_ARG);
1438 #ifdef INVARIANT_SUPPORT
1444 * In the non-WITNESS case, rw_assert() can only detect that at least
1445 * *some* thread owns an rlock, but it cannot guarantee that *this*
1446 * thread owns an rlock.
1449 __rw_assert(const volatile uintptr_t *c, int what, const char *file, int line)
1451 const struct rwlock *rw;
1453 if (SCHEDULER_STOPPED())
1460 case RA_LOCKED | RA_RECURSED:
1461 case RA_LOCKED | RA_NOTRECURSED:
1463 case RA_RLOCKED | RA_RECURSED:
1464 case RA_RLOCKED | RA_NOTRECURSED:
1466 witness_assert(&rw->lock_object, what, file, line);
1469 * If some other thread has a write lock or we have one
1470 * and are asserting a read lock, fail. Also, if no one
1471 * has a lock at all, fail.
1473 if (rw->rw_lock == RW_UNLOCKED ||
1474 (!(rw->rw_lock & RW_LOCK_READ) && (what & RA_RLOCKED ||
1475 rw_wowner(rw) != curthread)))
1476 panic("Lock %s not %slocked @ %s:%d\n",
1477 rw->lock_object.lo_name, (what & RA_RLOCKED) ?
1478 "read " : "", file, line);
1480 if (!(rw->rw_lock & RW_LOCK_READ) && !(what & RA_RLOCKED)) {
1481 if (rw_recursed(rw)) {
1482 if (what & RA_NOTRECURSED)
1483 panic("Lock %s recursed @ %s:%d\n",
1484 rw->lock_object.lo_name, file,
1486 } else if (what & RA_RECURSED)
1487 panic("Lock %s not recursed @ %s:%d\n",
1488 rw->lock_object.lo_name, file, line);
1493 case RA_WLOCKED | RA_RECURSED:
1494 case RA_WLOCKED | RA_NOTRECURSED:
1495 if (rw_wowner(rw) != curthread)
1496 panic("Lock %s not exclusively locked @ %s:%d\n",
1497 rw->lock_object.lo_name, file, line);
1498 if (rw_recursed(rw)) {
1499 if (what & RA_NOTRECURSED)
1500 panic("Lock %s recursed @ %s:%d\n",
1501 rw->lock_object.lo_name, file, line);
1502 } else if (what & RA_RECURSED)
1503 panic("Lock %s not recursed @ %s:%d\n",
1504 rw->lock_object.lo_name, file, line);
1508 witness_assert(&rw->lock_object, what, file, line);
1511 * If we hold a write lock fail. We can't reliably check
1512 * to see if we hold a read lock or not.
1514 if (rw_wowner(rw) == curthread)
1515 panic("Lock %s exclusively locked @ %s:%d\n",
1516 rw->lock_object.lo_name, file, line);
1520 panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
1524 #endif /* INVARIANT_SUPPORT */
1528 db_show_rwlock(const struct lock_object *lock)
1530 const struct rwlock *rw;
1533 rw = (const struct rwlock *)lock;
1535 db_printf(" state: ");
1536 if (rw->rw_lock == RW_UNLOCKED)
1537 db_printf("UNLOCKED\n");
1538 else if (rw->rw_lock == RW_DESTROYED) {
1539 db_printf("DESTROYED\n");
1541 } else if (rw->rw_lock & RW_LOCK_READ)
1542 db_printf("RLOCK: %ju locks\n",
1543 (uintmax_t)(RW_READERS(rw->rw_lock)));
1546 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1547 td->td_tid, td->td_proc->p_pid, td->td_name);
1548 if (rw_recursed(rw))
1549 db_printf(" recursed: %u\n", rw->rw_recurse);
1551 db_printf(" waiters: ");
1552 switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
1553 case RW_LOCK_READ_WAITERS:
1554 db_printf("readers\n");
1556 case RW_LOCK_WRITE_WAITERS:
1557 db_printf("writers\n");
1559 case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
1560 db_printf("readers and writers\n");
1563 db_printf("none\n");