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.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Machine independent bits of reader/writer lock implementation.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_hwpmc_hooks.h"
39 #include "opt_kdtrace.h"
40 #include "opt_no_adaptive_rwlocks.h"
42 #include <sys/param.h>
45 #include <sys/kernel.h>
47 #include <sys/mutex.h>
49 #include <sys/rwlock.h>
50 #include <sys/sysctl.h>
51 #include <sys/systm.h>
52 #include <sys/turnstile.h>
54 #include <machine/cpu.h>
56 #if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
57 #define ADAPTIVE_RWLOCKS
61 #include <sys/pmckern.h>
62 PMC_SOFT_DECLARE( , , lock, failed);
65 #ifdef ADAPTIVE_RWLOCKS
66 #define ROWNER_RETRIES 10
67 #define ROWNER_LOOPS 10000
73 static void db_show_rwlock(struct lock_object *lock);
75 static void assert_rw(struct lock_object *lock, int what);
76 static void lock_rw(struct lock_object *lock, int how);
78 static int owner_rw(struct lock_object *lock, struct thread **owner);
80 static int unlock_rw(struct lock_object *lock);
82 struct lock_class lock_class_rw = {
84 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
85 .lc_assert = assert_rw,
87 .lc_ddb_show = db_show_rwlock,
90 .lc_unlock = unlock_rw,
97 * Return a pointer to the owning thread if the lock is write-locked or
98 * NULL if the lock is unlocked or read-locked.
100 #define rw_wowner(rw) \
101 ((rw)->rw_lock & RW_LOCK_READ ? NULL : \
102 (struct thread *)RW_OWNER((rw)->rw_lock))
105 * Returns if a write owner is recursed. Write ownership is not assured
106 * here and should be previously checked.
108 #define rw_recursed(rw) ((rw)->rw_recurse != 0)
111 * Return true if curthread helds the lock.
113 #define rw_wlocked(rw) (rw_wowner((rw)) == curthread)
116 * Return a pointer to the owning thread for this lock who should receive
117 * any priority lent by threads that block on this lock. Currently this
118 * is identical to rw_wowner().
120 #define rw_owner(rw) rw_wowner(rw)
123 #define _rw_assert(rw, what, file, line)
127 assert_rw(struct lock_object *lock, int what)
130 rw_assert((struct rwlock *)lock, what);
134 lock_rw(struct lock_object *lock, int how)
138 rw = (struct rwlock *)lock;
146 unlock_rw(struct lock_object *lock)
150 rw = (struct rwlock *)lock;
151 rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
152 if (rw->rw_lock & RW_LOCK_READ) {
163 owner_rw(struct lock_object *lock, struct thread **owner)
165 struct rwlock *rw = (struct rwlock *)lock;
166 uintptr_t x = rw->rw_lock;
168 *owner = rw_wowner(rw);
169 return ((x & RW_LOCK_READ) != 0 ? (RW_READERS(x) != 0) :
175 rw_init_flags(struct rwlock *rw, const char *name, int opts)
179 MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
181 ASSERT_ATOMIC_LOAD_PTR(rw->rw_lock,
182 ("%s: rw_lock not aligned for %s: %p", __func__, name,
185 flags = LO_UPGRADABLE;
188 if (opts & RW_NOPROFILE)
189 flags |= LO_NOPROFILE;
190 if (!(opts & RW_NOWITNESS))
192 if (opts & RW_RECURSE)
193 flags |= LO_RECURSABLE;
197 rw->rw_lock = RW_UNLOCKED;
199 lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
203 rw_destroy(struct rwlock *rw)
206 KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock %p not unlocked", rw));
207 KASSERT(rw->rw_recurse == 0, ("rw lock %p still recursed", rw));
208 rw->rw_lock = RW_DESTROYED;
209 lock_destroy(&rw->lock_object);
213 rw_sysinit(void *arg)
215 struct rw_args *args = arg;
217 rw_init(args->ra_rw, args->ra_desc);
221 rw_sysinit_flags(void *arg)
223 struct rw_args_flags *args = arg;
225 rw_init_flags(args->ra_rw, args->ra_desc, args->ra_flags);
229 rw_wowned(struct rwlock *rw)
232 return (rw_wowner(rw) == curthread);
236 _rw_wlock(struct rwlock *rw, const char *file, int line)
239 if (SCHEDULER_STOPPED())
241 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
242 ("rw_wlock() by idle thread %p on rwlock %s @ %s:%d",
243 curthread, rw->lock_object.lo_name, file, line));
244 KASSERT(rw->rw_lock != RW_DESTROYED,
245 ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
246 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
248 __rw_wlock(rw, curthread, file, line);
249 LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
250 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
251 curthread->td_locks++;
255 _rw_try_wlock(struct rwlock *rw, const char *file, int line)
259 if (SCHEDULER_STOPPED())
262 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
263 ("rw_try_wlock() by idle thread %p on rwlock %s @ %s:%d",
264 curthread, rw->lock_object.lo_name, file, line));
265 KASSERT(rw->rw_lock != RW_DESTROYED,
266 ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
268 if (rw_wlocked(rw) &&
269 (rw->lock_object.lo_flags & LO_RECURSABLE) != 0) {
273 rval = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_UNLOCKED,
274 (uintptr_t)curthread);
276 LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
278 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
280 curthread->td_locks++;
286 _rw_wunlock(struct rwlock *rw, const char *file, int line)
289 if (SCHEDULER_STOPPED())
291 KASSERT(rw->rw_lock != RW_DESTROYED,
292 ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
293 _rw_assert(rw, RA_WLOCKED, file, line);
294 curthread->td_locks--;
295 WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
296 LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
298 if (!rw_recursed(rw))
299 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_RW_WUNLOCK_RELEASE, rw);
300 __rw_wunlock(rw, curthread, file, line);
303 * Determines whether a new reader can acquire a lock. Succeeds if the
304 * reader already owns a read lock and the lock is locked for read to
305 * prevent deadlock from reader recursion. Also succeeds if the lock
306 * is unlocked and has no writer waiters or spinners. Failing otherwise
307 * prioritizes writers before readers.
309 #define RW_CAN_READ(_rw) \
310 ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) & \
311 (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) == \
315 _rw_rlock(struct rwlock *rw, const char *file, int line)
317 struct turnstile *ts;
318 #ifdef ADAPTIVE_RWLOCKS
319 volatile struct thread *owner;
323 #ifdef LOCK_PROFILING
324 uint64_t waittime = 0;
329 uint64_t spin_cnt = 0;
330 uint64_t sleep_cnt = 0;
331 int64_t sleep_time = 0;
334 if (SCHEDULER_STOPPED())
337 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
338 ("rw_rlock() by idle thread %p on rwlock %s @ %s:%d",
339 curthread, rw->lock_object.lo_name, file, line));
340 KASSERT(rw->rw_lock != RW_DESTROYED,
341 ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
342 KASSERT(rw_wowner(rw) != curthread,
343 ("rw_rlock: wlock already held for %s @ %s:%d",
344 rw->lock_object.lo_name, file, line));
345 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line, NULL);
352 * Handle the easy case. If no other thread has a write
353 * lock, then try to bump up the count of read locks. Note
354 * that we have to preserve the current state of the
355 * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a
356 * read lock, then rw_lock must have changed, so restart
357 * the loop. Note that this handles the case of a
358 * completely unlocked rwlock since such a lock is encoded
359 * as a read lock with no waiters.
362 if (RW_CAN_READ(v)) {
364 * The RW_LOCK_READ_WAITERS flag should only be set
365 * if the lock has been unlocked and write waiters
368 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v,
369 v + RW_ONE_READER)) {
370 if (LOCK_LOG_TEST(&rw->lock_object, 0))
372 "%s: %p succeed %p -> %p", __func__,
374 (void *)(v + RW_ONE_READER));
380 PMC_SOFT_CALL( , , lock, failed);
382 lock_profile_obtain_lock_failed(&rw->lock_object,
383 &contested, &waittime);
385 #ifdef ADAPTIVE_RWLOCKS
387 * If the owner is running on another CPU, spin until
388 * the owner stops running or the state of the lock
391 if ((v & RW_LOCK_READ) == 0) {
392 owner = (struct thread *)RW_OWNER(v);
393 if (TD_IS_RUNNING(owner)) {
394 if (LOCK_LOG_TEST(&rw->lock_object, 0))
396 "%s: spinning on %p held by %p",
397 __func__, rw, owner);
398 while ((struct thread*)RW_OWNER(rw->rw_lock) ==
399 owner && TD_IS_RUNNING(owner)) {
407 } else if (spintries < ROWNER_RETRIES) {
409 for (i = 0; i < ROWNER_LOOPS; i++) {
411 if ((v & RW_LOCK_READ) == 0 || RW_CAN_READ(v))
415 if (i != ROWNER_LOOPS)
421 * Okay, now it's the hard case. Some other thread already
422 * has a write lock or there are write waiters present,
423 * acquire the turnstile lock so we can begin the process
426 ts = turnstile_trywait(&rw->lock_object);
429 * The lock might have been released while we spun, so
430 * recheck its state and restart the loop if needed.
433 if (RW_CAN_READ(v)) {
434 turnstile_cancel(ts);
438 #ifdef ADAPTIVE_RWLOCKS
440 * The current lock owner might have started executing
441 * on another CPU (or the lock could have changed
442 * owners) while we were waiting on the turnstile
443 * chain lock. If so, drop the turnstile lock and try
446 if ((v & RW_LOCK_READ) == 0) {
447 owner = (struct thread *)RW_OWNER(v);
448 if (TD_IS_RUNNING(owner)) {
449 turnstile_cancel(ts);
456 * The lock is held in write mode or it already has waiters.
458 MPASS(!RW_CAN_READ(v));
461 * If the RW_LOCK_READ_WAITERS flag is already set, then
462 * we can go ahead and block. If it is not set then try
463 * to set it. If we fail to set it drop the turnstile
464 * lock and restart the loop.
466 if (!(v & RW_LOCK_READ_WAITERS)) {
467 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
468 v | RW_LOCK_READ_WAITERS)) {
469 turnstile_cancel(ts);
472 if (LOCK_LOG_TEST(&rw->lock_object, 0))
473 CTR2(KTR_LOCK, "%s: %p set read waiters flag",
478 * We were unable to acquire the lock and the read waiters
479 * flag is set, so we must block on the turnstile.
481 if (LOCK_LOG_TEST(&rw->lock_object, 0))
482 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
485 sleep_time -= lockstat_nsecs();
487 turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
489 sleep_time += lockstat_nsecs();
492 if (LOCK_LOG_TEST(&rw->lock_object, 0))
493 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
498 * TODO: acquire "owner of record" here. Here be turnstile dragons
499 * however. turnstiles don't like owners changing between calls to
500 * turnstile_wait() currently.
502 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_RLOCK_ACQUIRE, rw, contested,
503 waittime, file, line);
504 LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
505 WITNESS_LOCK(&rw->lock_object, 0, file, line);
506 curthread->td_locks++;
507 curthread->td_rw_rlocks++;
510 LOCKSTAT_RECORD1(LS_RW_RLOCK_BLOCK, rw, sleep_time);
513 * Record only the loops spinning and not sleeping.
515 if (spin_cnt > sleep_cnt)
516 LOCKSTAT_RECORD1(LS_RW_RLOCK_SPIN, rw, (spin_cnt - sleep_cnt));
521 _rw_try_rlock(struct rwlock *rw, const char *file, int line)
525 if (SCHEDULER_STOPPED())
528 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
529 ("rw_try_rlock() by idle thread %p on rwlock %s @ %s:%d",
530 curthread, rw->lock_object.lo_name, file, line));
534 KASSERT(rw->rw_lock != RW_DESTROYED,
535 ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
536 if (!(x & RW_LOCK_READ))
538 if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, x + RW_ONE_READER)) {
539 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
541 WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
542 curthread->td_locks++;
543 curthread->td_rw_rlocks++;
548 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
553 _rw_runlock(struct rwlock *rw, const char *file, int line)
555 struct turnstile *ts;
556 uintptr_t x, v, queue;
558 if (SCHEDULER_STOPPED())
561 KASSERT(rw->rw_lock != RW_DESTROYED,
562 ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
563 _rw_assert(rw, RA_RLOCKED, file, line);
564 curthread->td_locks--;
565 curthread->td_rw_rlocks--;
566 WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
567 LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
569 /* TODO: drop "owner of record" here. */
573 * See if there is more than one read lock held. If so,
574 * just drop one and return.
577 if (RW_READERS(x) > 1) {
578 if (atomic_cmpset_rel_ptr(&rw->rw_lock, x,
579 x - RW_ONE_READER)) {
580 if (LOCK_LOG_TEST(&rw->lock_object, 0))
582 "%s: %p succeeded %p -> %p",
583 __func__, rw, (void *)x,
584 (void *)(x - RW_ONE_READER));
590 * If there aren't any waiters for a write lock, then try
591 * to drop it quickly.
593 if (!(x & RW_LOCK_WAITERS)) {
594 MPASS((x & ~RW_LOCK_WRITE_SPINNER) ==
596 if (atomic_cmpset_rel_ptr(&rw->rw_lock, x,
598 if (LOCK_LOG_TEST(&rw->lock_object, 0))
599 CTR2(KTR_LOCK, "%s: %p last succeeded",
606 * Ok, we know we have waiters and we think we are the
607 * last reader, so grab the turnstile lock.
609 turnstile_chain_lock(&rw->lock_object);
610 v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
611 MPASS(v & RW_LOCK_WAITERS);
614 * Try to drop our lock leaving the lock in a unlocked
617 * If you wanted to do explicit lock handoff you'd have to
618 * do it here. You'd also want to use turnstile_signal()
619 * and you'd have to handle the race where a higher
620 * priority thread blocks on the write lock before the
621 * thread you wakeup actually runs and have the new thread
622 * "steal" the lock. For now it's a lot simpler to just
623 * wakeup all of the waiters.
625 * As above, if we fail, then another thread might have
626 * acquired a read lock, so drop the turnstile lock and
630 if (v & RW_LOCK_WRITE_WAITERS) {
631 queue = TS_EXCLUSIVE_QUEUE;
632 x |= (v & RW_LOCK_READ_WAITERS);
634 queue = TS_SHARED_QUEUE;
635 if (!atomic_cmpset_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
637 turnstile_chain_unlock(&rw->lock_object);
640 if (LOCK_LOG_TEST(&rw->lock_object, 0))
641 CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
645 * Ok. The lock is released and all that's left is to
646 * wake up the waiters. Note that the lock might not be
647 * free anymore, but in that case the writers will just
648 * block again if they run before the new lock holder(s)
651 ts = turnstile_lookup(&rw->lock_object);
653 turnstile_broadcast(ts, queue);
654 turnstile_unpend(ts, TS_SHARED_LOCK);
655 turnstile_chain_unlock(&rw->lock_object);
658 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_RW_RUNLOCK_RELEASE, rw);
662 * This function is called when we are unable to obtain a write lock on the
663 * first try. This means that at least one other thread holds either a
664 * read or write lock.
667 _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
669 struct turnstile *ts;
670 #ifdef ADAPTIVE_RWLOCKS
671 volatile struct thread *owner;
676 #ifdef LOCK_PROFILING
677 uint64_t waittime = 0;
681 uint64_t spin_cnt = 0;
682 uint64_t sleep_cnt = 0;
683 int64_t sleep_time = 0;
686 if (SCHEDULER_STOPPED())
689 if (rw_wlocked(rw)) {
690 KASSERT(rw->lock_object.lo_flags & LO_RECURSABLE,
691 ("%s: recursing but non-recursive rw %s @ %s:%d\n",
692 __func__, rw->lock_object.lo_name, file, line));
694 if (LOCK_LOG_TEST(&rw->lock_object, 0))
695 CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
699 if (LOCK_LOG_TEST(&rw->lock_object, 0))
700 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
701 rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
703 while (!_rw_write_lock(rw, tid)) {
708 PMC_SOFT_CALL( , , lock, failed);
710 lock_profile_obtain_lock_failed(&rw->lock_object,
711 &contested, &waittime);
712 #ifdef ADAPTIVE_RWLOCKS
714 * If the lock is write locked and the owner is
715 * running on another CPU, spin until the owner stops
716 * running or the state of the lock changes.
719 owner = (struct thread *)RW_OWNER(v);
720 if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
721 if (LOCK_LOG_TEST(&rw->lock_object, 0))
722 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
723 __func__, rw, owner);
724 while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
725 TD_IS_RUNNING(owner)) {
733 if ((v & RW_LOCK_READ) && RW_READERS(v) &&
734 spintries < ROWNER_RETRIES) {
735 if (!(v & RW_LOCK_WRITE_SPINNER)) {
736 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
737 v | RW_LOCK_WRITE_SPINNER)) {
742 for (i = 0; i < ROWNER_LOOPS; i++) {
743 if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0)
748 spin_cnt += ROWNER_LOOPS - i;
750 if (i != ROWNER_LOOPS)
754 ts = turnstile_trywait(&rw->lock_object);
757 #ifdef ADAPTIVE_RWLOCKS
759 * The current lock owner might have started executing
760 * on another CPU (or the lock could have changed
761 * owners) while we were waiting on the turnstile
762 * chain lock. If so, drop the turnstile lock and try
765 if (!(v & RW_LOCK_READ)) {
766 owner = (struct thread *)RW_OWNER(v);
767 if (TD_IS_RUNNING(owner)) {
768 turnstile_cancel(ts);
774 * Check for the waiters flags about this rwlock.
775 * If the lock was released, without maintain any pending
776 * waiters queue, simply try to acquire it.
777 * If a pending waiters queue is present, claim the lock
778 * ownership and maintain the pending queue.
780 x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
781 if ((v & ~x) == RW_UNLOCKED) {
782 x &= ~RW_LOCK_WRITE_SPINNER;
783 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) {
787 turnstile_cancel(ts);
790 turnstile_cancel(ts);
794 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
795 * set it. If we fail to set it, then loop back and try
798 if (!(v & RW_LOCK_WRITE_WAITERS)) {
799 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
800 v | RW_LOCK_WRITE_WAITERS)) {
801 turnstile_cancel(ts);
804 if (LOCK_LOG_TEST(&rw->lock_object, 0))
805 CTR2(KTR_LOCK, "%s: %p set write waiters flag",
809 * We were unable to acquire the lock and the write waiters
810 * flag is set, so we must block on the turnstile.
812 if (LOCK_LOG_TEST(&rw->lock_object, 0))
813 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
816 sleep_time -= lockstat_nsecs();
818 turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
820 sleep_time += lockstat_nsecs();
823 if (LOCK_LOG_TEST(&rw->lock_object, 0))
824 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
826 #ifdef ADAPTIVE_RWLOCKS
830 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_WLOCK_ACQUIRE, rw, contested,
831 waittime, file, line);
834 LOCKSTAT_RECORD1(LS_RW_WLOCK_BLOCK, rw, sleep_time);
837 * Record only the loops spinning and not sleeping.
839 if (spin_cnt > sleep_cnt)
840 LOCKSTAT_RECORD1(LS_RW_WLOCK_SPIN, rw, (spin_cnt - sleep_cnt));
845 * This function is called if the first try at releasing a write lock failed.
846 * This means that one of the 2 waiter bits must be set indicating that at
847 * least one thread is waiting on this lock.
850 _rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
852 struct turnstile *ts;
856 if (SCHEDULER_STOPPED())
859 if (rw_wlocked(rw) && rw_recursed(rw)) {
861 if (LOCK_LOG_TEST(&rw->lock_object, 0))
862 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
866 KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
867 ("%s: neither of the waiter flags are set", __func__));
869 if (LOCK_LOG_TEST(&rw->lock_object, 0))
870 CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
872 turnstile_chain_lock(&rw->lock_object);
873 ts = turnstile_lookup(&rw->lock_object);
877 * Use the same algo as sx locks for now. Prefer waking up shared
878 * waiters if we have any over writers. This is probably not ideal.
880 * 'v' is the value we are going to write back to rw_lock. If we
881 * have waiters on both queues, we need to preserve the state of
882 * the waiter flag for the queue we don't wake up. For now this is
883 * hardcoded for the algorithm mentioned above.
885 * In the case of both readers and writers waiting we wakeup the
886 * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a
887 * new writer comes in before a reader it will claim the lock up
888 * above. There is probably a potential priority inversion in
889 * there that could be worked around either by waking both queues
890 * of waiters or doing some complicated lock handoff gymnastics.
893 if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) {
894 queue = TS_EXCLUSIVE_QUEUE;
895 v |= (rw->rw_lock & RW_LOCK_READ_WAITERS);
897 queue = TS_SHARED_QUEUE;
899 /* Wake up all waiters for the specific queue. */
900 if (LOCK_LOG_TEST(&rw->lock_object, 0))
901 CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
902 queue == TS_SHARED_QUEUE ? "read" : "write");
903 turnstile_broadcast(ts, queue);
904 atomic_store_rel_ptr(&rw->rw_lock, v);
905 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
906 turnstile_chain_unlock(&rw->lock_object);
910 * Attempt to do a non-blocking upgrade from a read lock to a write
911 * lock. This will only succeed if this thread holds a single read
912 * lock. Returns true if the upgrade succeeded and false otherwise.
915 _rw_try_upgrade(struct rwlock *rw, const char *file, int line)
918 struct turnstile *ts;
921 if (SCHEDULER_STOPPED())
924 KASSERT(rw->rw_lock != RW_DESTROYED,
925 ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
926 _rw_assert(rw, RA_RLOCKED, file, line);
929 * Attempt to switch from one reader to a writer. If there
930 * are any write waiters, then we will have to lock the
931 * turnstile first to prevent races with another writer
932 * calling turnstile_wait() before we have claimed this
933 * turnstile. So, do the simple case of no waiters first.
935 tid = (uintptr_t)curthread;
939 if (RW_READERS(v) > 1)
941 if (!(v & RW_LOCK_WAITERS)) {
942 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid);
949 * Ok, we think we have waiters, so lock the turnstile.
951 ts = turnstile_trywait(&rw->lock_object);
953 if (RW_READERS(v) > 1) {
954 turnstile_cancel(ts);
958 * Try to switch from one reader to a writer again. This time
959 * we honor the current state of the waiters flags.
960 * If we obtain the lock with the flags set, then claim
961 * ownership of the turnstile.
963 x = rw->rw_lock & RW_LOCK_WAITERS;
964 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x);
969 turnstile_cancel(ts);
972 turnstile_cancel(ts);
974 LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
976 curthread->td_rw_rlocks--;
977 WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
979 LOCKSTAT_RECORD0(LS_RW_TRYUPGRADE_UPGRADE, rw);
985 * Downgrade a write lock into a single read lock.
988 _rw_downgrade(struct rwlock *rw, const char *file, int line)
990 struct turnstile *ts;
994 if (SCHEDULER_STOPPED())
997 KASSERT(rw->rw_lock != RW_DESTROYED,
998 ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
999 _rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
1001 if (rw_recursed(rw))
1002 panic("downgrade of a recursed lock");
1005 WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
1008 * Convert from a writer to a single reader. First we handle
1009 * the easy case with no waiters. If there are any waiters, we
1010 * lock the turnstile and "disown" the lock.
1012 tid = (uintptr_t)curthread;
1013 if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
1017 * Ok, we think we have waiters, so lock the turnstile so we can
1018 * read the waiter flags without any races.
1020 turnstile_chain_lock(&rw->lock_object);
1021 v = rw->rw_lock & RW_LOCK_WAITERS;
1022 rwait = v & RW_LOCK_READ_WAITERS;
1023 wwait = v & RW_LOCK_WRITE_WAITERS;
1024 MPASS(rwait | wwait);
1027 * Downgrade from a write lock while preserving waiters flag
1028 * and give up ownership of the turnstile.
1030 ts = turnstile_lookup(&rw->lock_object);
1033 v &= ~RW_LOCK_READ_WAITERS;
1034 atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
1036 * Wake other readers if there are no writers pending. Otherwise they
1037 * won't be able to acquire the lock anyway.
1039 if (rwait && !wwait) {
1040 turnstile_broadcast(ts, TS_SHARED_QUEUE);
1041 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
1043 turnstile_disown(ts);
1044 turnstile_chain_unlock(&rw->lock_object);
1046 curthread->td_rw_rlocks++;
1047 LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
1048 LOCKSTAT_RECORD0(LS_RW_DOWNGRADE_DOWNGRADE, rw);
1051 #ifdef INVARIANT_SUPPORT
1057 * In the non-WITNESS case, rw_assert() can only detect that at least
1058 * *some* thread owns an rlock, but it cannot guarantee that *this*
1059 * thread owns an rlock.
1062 _rw_assert(struct rwlock *rw, int what, const char *file, int line)
1065 if (panicstr != NULL)
1069 case RA_LOCKED | RA_RECURSED:
1070 case RA_LOCKED | RA_NOTRECURSED:
1072 case RA_RLOCKED | RA_RECURSED:
1073 case RA_RLOCKED | RA_NOTRECURSED:
1075 witness_assert(&rw->lock_object, what, file, line);
1078 * If some other thread has a write lock or we have one
1079 * and are asserting a read lock, fail. Also, if no one
1080 * has a lock at all, fail.
1082 if (rw->rw_lock == RW_UNLOCKED ||
1083 (!(rw->rw_lock & RW_LOCK_READ) && (what & RA_RLOCKED ||
1084 rw_wowner(rw) != curthread)))
1085 panic("Lock %s not %slocked @ %s:%d\n",
1086 rw->lock_object.lo_name, (what & RA_RLOCKED) ?
1087 "read " : "", file, line);
1089 if (!(rw->rw_lock & RW_LOCK_READ) && !(what & RA_RLOCKED)) {
1090 if (rw_recursed(rw)) {
1091 if (what & RA_NOTRECURSED)
1092 panic("Lock %s recursed @ %s:%d\n",
1093 rw->lock_object.lo_name, file,
1095 } else if (what & RA_RECURSED)
1096 panic("Lock %s not recursed @ %s:%d\n",
1097 rw->lock_object.lo_name, file, line);
1102 case RA_WLOCKED | RA_RECURSED:
1103 case RA_WLOCKED | RA_NOTRECURSED:
1104 if (rw_wowner(rw) != curthread)
1105 panic("Lock %s not exclusively locked @ %s:%d\n",
1106 rw->lock_object.lo_name, file, line);
1107 if (rw_recursed(rw)) {
1108 if (what & RA_NOTRECURSED)
1109 panic("Lock %s recursed @ %s:%d\n",
1110 rw->lock_object.lo_name, file, line);
1111 } else if (what & RA_RECURSED)
1112 panic("Lock %s not recursed @ %s:%d\n",
1113 rw->lock_object.lo_name, file, line);
1117 witness_assert(&rw->lock_object, what, file, line);
1120 * If we hold a write lock fail. We can't reliably check
1121 * to see if we hold a read lock or not.
1123 if (rw_wowner(rw) == curthread)
1124 panic("Lock %s exclusively locked @ %s:%d\n",
1125 rw->lock_object.lo_name, file, line);
1129 panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
1133 #endif /* INVARIANT_SUPPORT */
1137 db_show_rwlock(struct lock_object *lock)
1142 rw = (struct rwlock *)lock;
1144 db_printf(" state: ");
1145 if (rw->rw_lock == RW_UNLOCKED)
1146 db_printf("UNLOCKED\n");
1147 else if (rw->rw_lock == RW_DESTROYED) {
1148 db_printf("DESTROYED\n");
1150 } else if (rw->rw_lock & RW_LOCK_READ)
1151 db_printf("RLOCK: %ju locks\n",
1152 (uintmax_t)(RW_READERS(rw->rw_lock)));
1155 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1156 td->td_tid, td->td_proc->p_pid, td->td_name);
1157 if (rw_recursed(rw))
1158 db_printf(" recursed: %u\n", rw->rw_recurse);
1160 db_printf(" waiters: ");
1161 switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
1162 case RW_LOCK_READ_WAITERS:
1163 db_printf("readers\n");
1165 case RW_LOCK_WRITE_WAITERS:
1166 db_printf("writers\n");
1168 case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
1169 db_printf("readers and writers\n");
1172 db_printf("none\n");