2 * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Machine independent bits of reader/writer lock implementation.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 #include "opt_hwpmc_hooks.h"
36 #include "opt_no_adaptive_rwlocks.h"
38 #include <sys/param.h>
41 #include <sys/kernel.h>
43 #include <sys/mutex.h>
45 #include <sys/rwlock.h>
46 #include <sys/sched.h>
48 #include <sys/sysctl.h>
49 #include <sys/systm.h>
50 #include <sys/turnstile.h>
52 #include <machine/cpu.h>
54 #if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
55 #define ADAPTIVE_RWLOCKS
59 #include <sys/pmckern.h>
60 PMC_SOFT_DECLARE( , , lock, failed);
64 * Return the rwlock address when the lock cookie address is provided.
65 * This functionality assumes that struct rwlock* have a member named rw_lock.
67 #define rwlock2rw(c) (__containerof(c, struct rwlock, rw_lock))
72 static void db_show_rwlock(const struct lock_object *lock);
74 static void assert_rw(const struct lock_object *lock, int what);
75 static void lock_rw(struct lock_object *lock, uintptr_t how);
77 static int owner_rw(const struct lock_object *lock, struct thread **owner);
79 static uintptr_t unlock_rw(struct lock_object *lock);
81 struct lock_class lock_class_rw = {
83 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
84 .lc_assert = assert_rw,
86 .lc_ddb_show = db_show_rwlock,
89 .lc_unlock = unlock_rw,
95 #ifdef ADAPTIVE_RWLOCKS
96 static int rowner_retries = 10;
97 static int rowner_loops = 10000;
98 static SYSCTL_NODE(_debug, OID_AUTO, rwlock, CTLFLAG_RD, NULL,
100 SYSCTL_INT(_debug_rwlock, OID_AUTO, retry, CTLFLAG_RW, &rowner_retries, 0, "");
101 SYSCTL_INT(_debug_rwlock, OID_AUTO, loops, CTLFLAG_RW, &rowner_loops, 0, "");
103 static struct lock_delay_config rw_delay = {
110 SYSCTL_INT(_debug_rwlock, OID_AUTO, delay_initial, CTLFLAG_RW, &rw_delay.initial,
112 SYSCTL_INT(_debug_rwlock, OID_AUTO, delay_step, CTLFLAG_RW, &rw_delay.step,
114 SYSCTL_INT(_debug_rwlock, OID_AUTO, delay_min, CTLFLAG_RW, &rw_delay.min,
116 SYSCTL_INT(_debug_rwlock, OID_AUTO, delay_max, CTLFLAG_RW, &rw_delay.max,
120 rw_delay_sysinit(void *dummy)
123 rw_delay.initial = mp_ncpus * 25;
124 rw_delay.step = (mp_ncpus * 25) / 2;
125 rw_delay.min = mp_ncpus * 5;
126 rw_delay.max = mp_ncpus * 25 * 10;
128 LOCK_DELAY_SYSINIT(rw_delay_sysinit);
132 * Return a pointer to the owning thread if the lock is write-locked or
133 * NULL if the lock is unlocked or read-locked.
136 #define lv_rw_wowner(v) \
137 ((v) & RW_LOCK_READ ? NULL : \
138 (struct thread *)RW_OWNER((v)))
140 #define rw_wowner(rw) lv_rw_wowner(RW_READ_VALUE(rw))
143 * Returns if a write owner is recursed. Write ownership is not assured
144 * here and should be previously checked.
146 #define rw_recursed(rw) ((rw)->rw_recurse != 0)
149 * Return true if curthread helds the lock.
151 #define rw_wlocked(rw) (rw_wowner((rw)) == curthread)
154 * Return a pointer to the owning thread for this lock who should receive
155 * any priority lent by threads that block on this lock. Currently this
156 * is identical to rw_wowner().
158 #define rw_owner(rw) rw_wowner(rw)
161 #define __rw_assert(c, what, file, line)
165 assert_rw(const struct lock_object *lock, int what)
168 rw_assert((const struct rwlock *)lock, what);
172 lock_rw(struct lock_object *lock, uintptr_t how)
176 rw = (struct rwlock *)lock;
184 unlock_rw(struct lock_object *lock)
188 rw = (struct rwlock *)lock;
189 rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
190 if (rw->rw_lock & RW_LOCK_READ) {
201 owner_rw(const struct lock_object *lock, struct thread **owner)
203 const struct rwlock *rw = (const struct rwlock *)lock;
204 uintptr_t x = rw->rw_lock;
206 *owner = rw_wowner(rw);
207 return ((x & RW_LOCK_READ) != 0 ? (RW_READERS(x) != 0) :
213 _rw_init_flags(volatile uintptr_t *c, const char *name, int opts)
220 MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
221 RW_RECURSE | RW_NEW)) == 0);
222 ASSERT_ATOMIC_LOAD_PTR(rw->rw_lock,
223 ("%s: rw_lock not aligned for %s: %p", __func__, name,
226 flags = LO_UPGRADABLE;
229 if (opts & RW_NOPROFILE)
230 flags |= LO_NOPROFILE;
231 if (!(opts & RW_NOWITNESS))
233 if (opts & RW_RECURSE)
234 flags |= LO_RECURSABLE;
240 lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
241 rw->rw_lock = RW_UNLOCKED;
246 _rw_destroy(volatile uintptr_t *c)
252 KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock %p not unlocked", rw));
253 KASSERT(rw->rw_recurse == 0, ("rw lock %p still recursed", rw));
254 rw->rw_lock = RW_DESTROYED;
255 lock_destroy(&rw->lock_object);
259 rw_sysinit(void *arg)
261 struct rw_args *args = arg;
263 rw_init((struct rwlock *)args->ra_rw, args->ra_desc);
267 rw_sysinit_flags(void *arg)
269 struct rw_args_flags *args = arg;
271 rw_init_flags((struct rwlock *)args->ra_rw, args->ra_desc,
276 _rw_wowned(const volatile uintptr_t *c)
279 return (rw_wowner(rwlock2rw(c)) == curthread);
283 _rw_wlock_cookie(volatile uintptr_t *c, const char *file, int line)
287 if (SCHEDULER_STOPPED())
292 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
293 ("rw_wlock() by idle thread %p on rwlock %s @ %s:%d",
294 curthread, rw->lock_object.lo_name, file, line));
295 KASSERT(rw->rw_lock != RW_DESTROYED,
296 ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
297 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
299 __rw_wlock(rw, curthread, file, line);
300 LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
301 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
302 TD_LOCKS_INC(curthread);
306 __rw_try_wlock(volatile uintptr_t *c, const char *file, int line)
311 if (SCHEDULER_STOPPED())
316 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
317 ("rw_try_wlock() by idle thread %p on rwlock %s @ %s:%d",
318 curthread, rw->lock_object.lo_name, file, line));
319 KASSERT(rw->rw_lock != RW_DESTROYED,
320 ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
322 if (rw_wlocked(rw) &&
323 (rw->lock_object.lo_flags & LO_RECURSABLE) != 0) {
327 rval = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_UNLOCKED,
328 (uintptr_t)curthread);
330 LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
332 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
334 if (!rw_recursed(rw))
335 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire,
336 rw, 0, 0, file, line, LOCKSTAT_WRITER);
337 TD_LOCKS_INC(curthread);
343 _rw_wunlock_cookie(volatile uintptr_t *c, const char *file, int line)
347 if (SCHEDULER_STOPPED())
352 KASSERT(rw->rw_lock != RW_DESTROYED,
353 ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
354 __rw_assert(c, RA_WLOCKED, file, line);
355 WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
356 LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
358 __rw_wunlock(rw, curthread, file, line);
359 TD_LOCKS_DEC(curthread);
363 * Determines whether a new reader can acquire a lock. Succeeds if the
364 * reader already owns a read lock and the lock is locked for read to
365 * prevent deadlock from reader recursion. Also succeeds if the lock
366 * is unlocked and has no writer waiters or spinners. Failing otherwise
367 * prioritizes writers before readers.
369 #define RW_CAN_READ(_rw) \
370 ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) & \
371 (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) == \
375 __rw_rlock(volatile uintptr_t *c, const char *file, int line)
378 struct turnstile *ts;
379 #ifdef ADAPTIVE_RWLOCKS
380 volatile struct thread *owner;
384 #ifdef LOCK_PROFILING
385 uint64_t waittime = 0;
389 #if defined(ADAPTIVE_RWLOCKS) || defined(KDTRACE_HOOKS)
390 struct lock_delay_arg lda;
395 int64_t sleep_time = 0;
396 int64_t all_time = 0;
399 if (SCHEDULER_STOPPED())
402 #if defined(ADAPTIVE_RWLOCKS)
403 lock_delay_arg_init(&lda, &rw_delay);
404 #elif defined(KDTRACE_HOOKS)
405 lock_delay_arg_init(&lda, NULL);
409 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
410 ("rw_rlock() by idle thread %p on rwlock %s @ %s:%d",
411 curthread, rw->lock_object.lo_name, file, line));
412 KASSERT(rw->rw_lock != RW_DESTROYED,
413 ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
414 KASSERT(rw_wowner(rw) != curthread,
415 ("rw_rlock: wlock already held for %s @ %s:%d",
416 rw->lock_object.lo_name, file, line));
417 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line, NULL);
420 all_time -= lockstat_nsecs(&rw->lock_object);
422 v = RW_READ_VALUE(rw);
428 * Handle the easy case. If no other thread has a write
429 * lock, then try to bump up the count of read locks. Note
430 * that we have to preserve the current state of the
431 * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a
432 * read lock, then rw_lock must have changed, so restart
433 * the loop. Note that this handles the case of a
434 * completely unlocked rwlock since such a lock is encoded
435 * as a read lock with no waiters.
437 if (RW_CAN_READ(v)) {
439 * The RW_LOCK_READ_WAITERS flag should only be set
440 * if the lock has been unlocked and write waiters
443 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v,
444 v + RW_ONE_READER)) {
445 if (LOCK_LOG_TEST(&rw->lock_object, 0))
447 "%s: %p succeed %p -> %p", __func__,
449 (void *)(v + RW_ONE_READER));
452 v = RW_READ_VALUE(rw);
459 PMC_SOFT_CALL( , , lock, failed);
461 lock_profile_obtain_lock_failed(&rw->lock_object,
462 &contested, &waittime);
464 #ifdef ADAPTIVE_RWLOCKS
466 * If the owner is running on another CPU, spin until
467 * the owner stops running or the state of the lock
470 if ((v & RW_LOCK_READ) == 0) {
471 owner = (struct thread *)RW_OWNER(v);
472 if (TD_IS_RUNNING(owner)) {
473 if (LOCK_LOG_TEST(&rw->lock_object, 0))
475 "%s: spinning on %p held by %p",
476 __func__, rw, owner);
477 KTR_STATE1(KTR_SCHED, "thread",
478 sched_tdname(curthread), "spinning",
479 "lockname:\"%s\"", rw->lock_object.lo_name);
482 v = RW_READ_VALUE(rw);
483 owner = lv_rw_wowner(v);
484 } while (owner != NULL && TD_IS_RUNNING(owner));
485 KTR_STATE0(KTR_SCHED, "thread",
486 sched_tdname(curthread), "running");
489 } else if (spintries < rowner_retries) {
491 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
492 "spinning", "lockname:\"%s\"",
493 rw->lock_object.lo_name);
494 for (i = 0; i < rowner_loops; i++) {
495 v = RW_READ_VALUE(rw);
496 if ((v & RW_LOCK_READ) == 0 || RW_CAN_READ(v))
500 v = RW_READ_VALUE(rw);
502 lda.spin_cnt += rowner_loops - i;
504 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
506 if (i != rowner_loops)
512 * Okay, now it's the hard case. Some other thread already
513 * has a write lock or there are write waiters present,
514 * acquire the turnstile lock so we can begin the process
517 ts = turnstile_trywait(&rw->lock_object);
520 * The lock might have been released while we spun, so
521 * recheck its state and restart the loop if needed.
523 v = RW_READ_VALUE(rw);
524 if (RW_CAN_READ(v)) {
525 turnstile_cancel(ts);
529 #ifdef ADAPTIVE_RWLOCKS
531 * The current lock owner might have started executing
532 * on another CPU (or the lock could have changed
533 * owners) while we were waiting on the turnstile
534 * chain lock. If so, drop the turnstile lock and try
537 if ((v & RW_LOCK_READ) == 0) {
538 owner = (struct thread *)RW_OWNER(v);
539 if (TD_IS_RUNNING(owner)) {
540 turnstile_cancel(ts);
547 * The lock is held in write mode or it already has waiters.
549 MPASS(!RW_CAN_READ(v));
552 * If the RW_LOCK_READ_WAITERS flag is already set, then
553 * we can go ahead and block. If it is not set then try
554 * to set it. If we fail to set it drop the turnstile
555 * lock and restart the loop.
557 if (!(v & RW_LOCK_READ_WAITERS)) {
558 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
559 v | RW_LOCK_READ_WAITERS)) {
560 turnstile_cancel(ts);
561 v = RW_READ_VALUE(rw);
564 if (LOCK_LOG_TEST(&rw->lock_object, 0))
565 CTR2(KTR_LOCK, "%s: %p set read waiters flag",
570 * We were unable to acquire the lock and the read waiters
571 * flag is set, so we must block on the turnstile.
573 if (LOCK_LOG_TEST(&rw->lock_object, 0))
574 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
577 sleep_time -= lockstat_nsecs(&rw->lock_object);
579 turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
581 sleep_time += lockstat_nsecs(&rw->lock_object);
584 if (LOCK_LOG_TEST(&rw->lock_object, 0))
585 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
587 v = RW_READ_VALUE(rw);
590 all_time += lockstat_nsecs(&rw->lock_object);
592 LOCKSTAT_RECORD4(rw__block, rw, sleep_time,
593 LOCKSTAT_READER, (state & RW_LOCK_READ) == 0,
594 (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
596 /* Record only the loops spinning and not sleeping. */
597 if (lda.spin_cnt > sleep_cnt)
598 LOCKSTAT_RECORD4(rw__spin, rw, all_time - sleep_time,
599 LOCKSTAT_READER, (state & RW_LOCK_READ) == 0,
600 (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
603 * TODO: acquire "owner of record" here. Here be turnstile dragons
604 * however. turnstiles don't like owners changing between calls to
605 * turnstile_wait() currently.
607 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire, rw, contested,
608 waittime, file, line, LOCKSTAT_READER);
609 LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
610 WITNESS_LOCK(&rw->lock_object, 0, file, line);
611 TD_LOCKS_INC(curthread);
612 curthread->td_rw_rlocks++;
616 __rw_try_rlock(volatile uintptr_t *c, const char *file, int line)
621 if (SCHEDULER_STOPPED())
626 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
627 ("rw_try_rlock() by idle thread %p on rwlock %s @ %s:%d",
628 curthread, rw->lock_object.lo_name, file, line));
632 KASSERT(rw->rw_lock != RW_DESTROYED,
633 ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
634 if (!(x & RW_LOCK_READ))
636 if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, x + RW_ONE_READER)) {
637 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
639 WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
640 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire,
641 rw, 0, 0, file, line, LOCKSTAT_READER);
642 TD_LOCKS_INC(curthread);
643 curthread->td_rw_rlocks++;
648 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
653 _rw_runlock_cookie(volatile uintptr_t *c, const char *file, int line)
656 struct turnstile *ts;
657 uintptr_t x, v, queue;
659 if (SCHEDULER_STOPPED())
664 KASSERT(rw->rw_lock != RW_DESTROYED,
665 ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
666 __rw_assert(c, RA_RLOCKED, file, line);
667 WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
668 LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
670 /* TODO: drop "owner of record" here. */
671 x = RW_READ_VALUE(rw);
674 * See if there is more than one read lock held. If so,
675 * just drop one and return.
677 if (RW_READERS(x) > 1) {
678 if (atomic_cmpset_rel_ptr(&rw->rw_lock, x,
679 x - RW_ONE_READER)) {
680 if (LOCK_LOG_TEST(&rw->lock_object, 0))
682 "%s: %p succeeded %p -> %p",
683 __func__, rw, (void *)x,
684 (void *)(x - RW_ONE_READER));
687 x = RW_READ_VALUE(rw);
691 * If there aren't any waiters for a write lock, then try
692 * to drop it quickly.
694 if (!(x & RW_LOCK_WAITERS)) {
695 MPASS((x & ~RW_LOCK_WRITE_SPINNER) ==
697 if (atomic_cmpset_rel_ptr(&rw->rw_lock, x,
699 if (LOCK_LOG_TEST(&rw->lock_object, 0))
700 CTR2(KTR_LOCK, "%s: %p last succeeded",
704 x = RW_READ_VALUE(rw);
708 * Ok, we know we have waiters and we think we are the
709 * last reader, so grab the turnstile lock.
711 turnstile_chain_lock(&rw->lock_object);
712 v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
713 MPASS(v & RW_LOCK_WAITERS);
716 * Try to drop our lock leaving the lock in a unlocked
719 * If you wanted to do explicit lock handoff you'd have to
720 * do it here. You'd also want to use turnstile_signal()
721 * and you'd have to handle the race where a higher
722 * priority thread blocks on the write lock before the
723 * thread you wakeup actually runs and have the new thread
724 * "steal" the lock. For now it's a lot simpler to just
725 * wakeup all of the waiters.
727 * As above, if we fail, then another thread might have
728 * acquired a read lock, so drop the turnstile lock and
732 if (v & RW_LOCK_WRITE_WAITERS) {
733 queue = TS_EXCLUSIVE_QUEUE;
734 x |= (v & RW_LOCK_READ_WAITERS);
736 queue = TS_SHARED_QUEUE;
737 if (!atomic_cmpset_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
739 turnstile_chain_unlock(&rw->lock_object);
740 x = RW_READ_VALUE(rw);
743 if (LOCK_LOG_TEST(&rw->lock_object, 0))
744 CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
748 * Ok. The lock is released and all that's left is to
749 * wake up the waiters. Note that the lock might not be
750 * free anymore, but in that case the writers will just
751 * block again if they run before the new lock holder(s)
754 ts = turnstile_lookup(&rw->lock_object);
756 turnstile_broadcast(ts, queue);
757 turnstile_unpend(ts, TS_SHARED_LOCK);
758 turnstile_chain_unlock(&rw->lock_object);
761 LOCKSTAT_PROFILE_RELEASE_RWLOCK(rw__release, rw, LOCKSTAT_READER);
762 TD_LOCKS_DEC(curthread);
763 curthread->td_rw_rlocks--;
767 * This function is called when we are unable to obtain a write lock on the
768 * first try. This means that at least one other thread holds either a
769 * read or write lock.
772 __rw_wlock_hard(volatile uintptr_t *c, uintptr_t tid, const char *file,
776 struct turnstile *ts;
777 #ifdef ADAPTIVE_RWLOCKS
778 volatile struct thread *owner;
783 #ifdef LOCK_PROFILING
784 uint64_t waittime = 0;
787 #if defined(ADAPTIVE_RWLOCKS) || defined(KDTRACE_HOOKS)
788 struct lock_delay_arg lda;
793 int64_t sleep_time = 0;
794 int64_t all_time = 0;
797 if (SCHEDULER_STOPPED())
800 #if defined(ADAPTIVE_RWLOCKS)
801 lock_delay_arg_init(&lda, &rw_delay);
802 #elif defined(KDTRACE_HOOKS)
803 lock_delay_arg_init(&lda, NULL);
806 v = RW_READ_VALUE(rw);
808 if (__predict_false(lv_rw_wowner(v) == (struct thread *)tid)) {
809 KASSERT(rw->lock_object.lo_flags & LO_RECURSABLE,
810 ("%s: recursing but non-recursive rw %s @ %s:%d\n",
811 __func__, rw->lock_object.lo_name, file, line));
813 if (LOCK_LOG_TEST(&rw->lock_object, 0))
814 CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
818 if (LOCK_LOG_TEST(&rw->lock_object, 0))
819 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
820 rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
823 all_time -= lockstat_nsecs(&rw->lock_object);
827 if (v == RW_UNLOCKED) {
828 if (_rw_write_lock(rw, tid))
830 v = RW_READ_VALUE(rw);
837 PMC_SOFT_CALL( , , lock, failed);
839 lock_profile_obtain_lock_failed(&rw->lock_object,
840 &contested, &waittime);
841 #ifdef ADAPTIVE_RWLOCKS
843 * If the lock is write locked and the owner is
844 * running on another CPU, spin until the owner stops
845 * running or the state of the lock changes.
847 owner = lv_rw_wowner(v);
848 if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
849 if (LOCK_LOG_TEST(&rw->lock_object, 0))
850 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
851 __func__, rw, owner);
852 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
853 "spinning", "lockname:\"%s\"",
854 rw->lock_object.lo_name);
857 v = RW_READ_VALUE(rw);
858 owner = lv_rw_wowner(v);
859 } while (owner != NULL && TD_IS_RUNNING(owner));
860 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
864 if ((v & RW_LOCK_READ) && RW_READERS(v) &&
865 spintries < rowner_retries) {
866 if (!(v & RW_LOCK_WRITE_SPINNER)) {
867 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
868 v | RW_LOCK_WRITE_SPINNER)) {
869 v = RW_READ_VALUE(rw);
874 KTR_STATE1(KTR_SCHED, "thread", sched_tdname(curthread),
875 "spinning", "lockname:\"%s\"",
876 rw->lock_object.lo_name);
877 for (i = 0; i < rowner_loops; i++) {
878 if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0)
882 KTR_STATE0(KTR_SCHED, "thread", sched_tdname(curthread),
884 v = RW_READ_VALUE(rw);
886 lda.spin_cnt += rowner_loops - i;
888 if (i != rowner_loops)
892 ts = turnstile_trywait(&rw->lock_object);
893 v = RW_READ_VALUE(rw);
895 #ifdef ADAPTIVE_RWLOCKS
897 * The current lock owner might have started executing
898 * on another CPU (or the lock could have changed
899 * owners) while we were waiting on the turnstile
900 * chain lock. If so, drop the turnstile lock and try
903 if (!(v & RW_LOCK_READ)) {
904 owner = (struct thread *)RW_OWNER(v);
905 if (TD_IS_RUNNING(owner)) {
906 turnstile_cancel(ts);
912 * Check for the waiters flags about this rwlock.
913 * If the lock was released, without maintain any pending
914 * waiters queue, simply try to acquire it.
915 * If a pending waiters queue is present, claim the lock
916 * ownership and maintain the pending queue.
918 x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
919 if ((v & ~x) == RW_UNLOCKED) {
920 x &= ~RW_LOCK_WRITE_SPINNER;
921 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) {
925 turnstile_cancel(ts);
928 turnstile_cancel(ts);
929 v = RW_READ_VALUE(rw);
933 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
934 * set it. If we fail to set it, then loop back and try
937 if (!(v & RW_LOCK_WRITE_WAITERS)) {
938 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
939 v | RW_LOCK_WRITE_WAITERS)) {
940 turnstile_cancel(ts);
941 v = RW_READ_VALUE(rw);
944 if (LOCK_LOG_TEST(&rw->lock_object, 0))
945 CTR2(KTR_LOCK, "%s: %p set write waiters flag",
949 * We were unable to acquire the lock and the write waiters
950 * flag is set, so we must block on the turnstile.
952 if (LOCK_LOG_TEST(&rw->lock_object, 0))
953 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
956 sleep_time -= lockstat_nsecs(&rw->lock_object);
958 turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
960 sleep_time += lockstat_nsecs(&rw->lock_object);
963 if (LOCK_LOG_TEST(&rw->lock_object, 0))
964 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
966 #ifdef ADAPTIVE_RWLOCKS
969 v = RW_READ_VALUE(rw);
972 all_time += lockstat_nsecs(&rw->lock_object);
974 LOCKSTAT_RECORD4(rw__block, rw, sleep_time,
975 LOCKSTAT_WRITER, (state & RW_LOCK_READ) == 0,
976 (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
978 /* Record only the loops spinning and not sleeping. */
979 if (lda.spin_cnt > sleep_cnt)
980 LOCKSTAT_RECORD4(rw__spin, rw, all_time - sleep_time,
981 LOCKSTAT_WRITER, (state & RW_LOCK_READ) == 0,
982 (state & RW_LOCK_READ) == 0 ? 0 : RW_READERS(state));
984 LOCKSTAT_PROFILE_OBTAIN_RWLOCK_SUCCESS(rw__acquire, rw, contested,
985 waittime, file, line, LOCKSTAT_WRITER);
989 * This function is called if the first try at releasing a write lock failed.
990 * This means that one of the 2 waiter bits must be set indicating that at
991 * least one thread is waiting on this lock.
994 __rw_wunlock_hard(volatile uintptr_t *c, uintptr_t tid, const char *file,
998 struct turnstile *ts;
1002 if (SCHEDULER_STOPPED())
1007 if (rw_wlocked(rw) && rw_recursed(rw)) {
1009 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1010 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
1014 KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
1015 ("%s: neither of the waiter flags are set", __func__));
1017 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1018 CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
1020 turnstile_chain_lock(&rw->lock_object);
1021 ts = turnstile_lookup(&rw->lock_object);
1025 * Use the same algo as sx locks for now. Prefer waking up shared
1026 * waiters if we have any over writers. This is probably not ideal.
1028 * 'v' is the value we are going to write back to rw_lock. If we
1029 * have waiters on both queues, we need to preserve the state of
1030 * the waiter flag for the queue we don't wake up. For now this is
1031 * hardcoded for the algorithm mentioned above.
1033 * In the case of both readers and writers waiting we wakeup the
1034 * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a
1035 * new writer comes in before a reader it will claim the lock up
1036 * above. There is probably a potential priority inversion in
1037 * there that could be worked around either by waking both queues
1038 * of waiters or doing some complicated lock handoff gymnastics.
1041 if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) {
1042 queue = TS_EXCLUSIVE_QUEUE;
1043 v |= (rw->rw_lock & RW_LOCK_READ_WAITERS);
1045 queue = TS_SHARED_QUEUE;
1047 /* Wake up all waiters for the specific queue. */
1048 if (LOCK_LOG_TEST(&rw->lock_object, 0))
1049 CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
1050 queue == TS_SHARED_QUEUE ? "read" : "write");
1051 turnstile_broadcast(ts, queue);
1052 atomic_store_rel_ptr(&rw->rw_lock, v);
1053 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
1054 turnstile_chain_unlock(&rw->lock_object);
1058 * Attempt to do a non-blocking upgrade from a read lock to a write
1059 * lock. This will only succeed if this thread holds a single read
1060 * lock. Returns true if the upgrade succeeded and false otherwise.
1063 __rw_try_upgrade(volatile uintptr_t *c, const char *file, int line)
1066 uintptr_t v, x, tid;
1067 struct turnstile *ts;
1070 if (SCHEDULER_STOPPED())
1075 KASSERT(rw->rw_lock != RW_DESTROYED,
1076 ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
1077 __rw_assert(c, RA_RLOCKED, file, line);
1080 * Attempt to switch from one reader to a writer. If there
1081 * are any write waiters, then we will have to lock the
1082 * turnstile first to prevent races with another writer
1083 * calling turnstile_wait() before we have claimed this
1084 * turnstile. So, do the simple case of no waiters first.
1086 tid = (uintptr_t)curthread;
1090 if (RW_READERS(v) > 1)
1092 if (!(v & RW_LOCK_WAITERS)) {
1093 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid);
1100 * Ok, we think we have waiters, so lock the turnstile.
1102 ts = turnstile_trywait(&rw->lock_object);
1104 if (RW_READERS(v) > 1) {
1105 turnstile_cancel(ts);
1109 * Try to switch from one reader to a writer again. This time
1110 * we honor the current state of the waiters flags.
1111 * If we obtain the lock with the flags set, then claim
1112 * ownership of the turnstile.
1114 x = rw->rw_lock & RW_LOCK_WAITERS;
1115 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x);
1118 turnstile_claim(ts);
1120 turnstile_cancel(ts);
1123 turnstile_cancel(ts);
1125 LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
1127 curthread->td_rw_rlocks--;
1128 WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
1130 LOCKSTAT_RECORD0(rw__upgrade, rw);
1136 * Downgrade a write lock into a single read lock.
1139 __rw_downgrade(volatile uintptr_t *c, const char *file, int line)
1142 struct turnstile *ts;
1146 if (SCHEDULER_STOPPED())
1151 KASSERT(rw->rw_lock != RW_DESTROYED,
1152 ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
1153 __rw_assert(c, RA_WLOCKED | RA_NOTRECURSED, file, line);
1155 if (rw_recursed(rw))
1156 panic("downgrade of a recursed lock");
1159 WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
1162 * Convert from a writer to a single reader. First we handle
1163 * the easy case with no waiters. If there are any waiters, we
1164 * lock the turnstile and "disown" the lock.
1166 tid = (uintptr_t)curthread;
1167 if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
1171 * Ok, we think we have waiters, so lock the turnstile so we can
1172 * read the waiter flags without any races.
1174 turnstile_chain_lock(&rw->lock_object);
1175 v = rw->rw_lock & RW_LOCK_WAITERS;
1176 rwait = v & RW_LOCK_READ_WAITERS;
1177 wwait = v & RW_LOCK_WRITE_WAITERS;
1178 MPASS(rwait | wwait);
1181 * Downgrade from a write lock while preserving waiters flag
1182 * and give up ownership of the turnstile.
1184 ts = turnstile_lookup(&rw->lock_object);
1187 v &= ~RW_LOCK_READ_WAITERS;
1188 atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
1190 * Wake other readers if there are no writers pending. Otherwise they
1191 * won't be able to acquire the lock anyway.
1193 if (rwait && !wwait) {
1194 turnstile_broadcast(ts, TS_SHARED_QUEUE);
1195 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
1197 turnstile_disown(ts);
1198 turnstile_chain_unlock(&rw->lock_object);
1200 curthread->td_rw_rlocks++;
1201 LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
1202 LOCKSTAT_RECORD0(rw__downgrade, rw);
1205 #ifdef INVARIANT_SUPPORT
1211 * In the non-WITNESS case, rw_assert() can only detect that at least
1212 * *some* thread owns an rlock, but it cannot guarantee that *this*
1213 * thread owns an rlock.
1216 __rw_assert(const volatile uintptr_t *c, int what, const char *file, int line)
1218 const struct rwlock *rw;
1220 if (panicstr != NULL)
1227 case RA_LOCKED | RA_RECURSED:
1228 case RA_LOCKED | RA_NOTRECURSED:
1230 case RA_RLOCKED | RA_RECURSED:
1231 case RA_RLOCKED | RA_NOTRECURSED:
1233 witness_assert(&rw->lock_object, what, file, line);
1236 * If some other thread has a write lock or we have one
1237 * and are asserting a read lock, fail. Also, if no one
1238 * has a lock at all, fail.
1240 if (rw->rw_lock == RW_UNLOCKED ||
1241 (!(rw->rw_lock & RW_LOCK_READ) && (what & RA_RLOCKED ||
1242 rw_wowner(rw) != curthread)))
1243 panic("Lock %s not %slocked @ %s:%d\n",
1244 rw->lock_object.lo_name, (what & RA_RLOCKED) ?
1245 "read " : "", file, line);
1247 if (!(rw->rw_lock & RW_LOCK_READ) && !(what & RA_RLOCKED)) {
1248 if (rw_recursed(rw)) {
1249 if (what & RA_NOTRECURSED)
1250 panic("Lock %s recursed @ %s:%d\n",
1251 rw->lock_object.lo_name, file,
1253 } else if (what & RA_RECURSED)
1254 panic("Lock %s not recursed @ %s:%d\n",
1255 rw->lock_object.lo_name, file, line);
1260 case RA_WLOCKED | RA_RECURSED:
1261 case RA_WLOCKED | RA_NOTRECURSED:
1262 if (rw_wowner(rw) != curthread)
1263 panic("Lock %s not exclusively locked @ %s:%d\n",
1264 rw->lock_object.lo_name, file, line);
1265 if (rw_recursed(rw)) {
1266 if (what & RA_NOTRECURSED)
1267 panic("Lock %s recursed @ %s:%d\n",
1268 rw->lock_object.lo_name, file, line);
1269 } else if (what & RA_RECURSED)
1270 panic("Lock %s not recursed @ %s:%d\n",
1271 rw->lock_object.lo_name, file, line);
1275 witness_assert(&rw->lock_object, what, file, line);
1278 * If we hold a write lock fail. We can't reliably check
1279 * to see if we hold a read lock or not.
1281 if (rw_wowner(rw) == curthread)
1282 panic("Lock %s exclusively locked @ %s:%d\n",
1283 rw->lock_object.lo_name, file, line);
1287 panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
1291 #endif /* INVARIANT_SUPPORT */
1295 db_show_rwlock(const struct lock_object *lock)
1297 const struct rwlock *rw;
1300 rw = (const struct rwlock *)lock;
1302 db_printf(" state: ");
1303 if (rw->rw_lock == RW_UNLOCKED)
1304 db_printf("UNLOCKED\n");
1305 else if (rw->rw_lock == RW_DESTROYED) {
1306 db_printf("DESTROYED\n");
1308 } else if (rw->rw_lock & RW_LOCK_READ)
1309 db_printf("RLOCK: %ju locks\n",
1310 (uintmax_t)(RW_READERS(rw->rw_lock)));
1313 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1314 td->td_tid, td->td_proc->p_pid, td->td_name);
1315 if (rw_recursed(rw))
1316 db_printf(" recursed: %u\n", rw->rw_recurse);
1318 db_printf(" waiters: ");
1319 switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
1320 case RW_LOCK_READ_WAITERS:
1321 db_printf("readers\n");
1323 case RW_LOCK_WRITE_WAITERS:
1324 db_printf("writers\n");
1326 case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
1327 db_printf("readers and writers\n");
1330 db_printf("none\n");