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_kdtrace.h"
39 #include "opt_no_adaptive_rwlocks.h"
41 #include <sys/param.h>
43 #include <sys/kernel.h>
45 #include <sys/mutex.h>
47 #include <sys/rwlock.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
58 #ifdef ADAPTIVE_RWLOCKS
59 #define ROWNER_RETRIES 10
60 #define ROWNER_LOOPS 10000
66 static void db_show_rwlock(struct lock_object *lock);
68 static void assert_rw(struct lock_object *lock, int what);
69 static void lock_rw(struct lock_object *lock, int how);
71 static int owner_rw(struct lock_object *lock, struct thread **owner);
73 static int unlock_rw(struct lock_object *lock);
75 struct lock_class lock_class_rw = {
77 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
78 .lc_assert = assert_rw,
80 .lc_ddb_show = db_show_rwlock,
83 .lc_unlock = unlock_rw,
90 * Return a pointer to the owning thread if the lock is write-locked or
91 * NULL if the lock is unlocked or read-locked.
93 #define rw_wowner(rw) \
94 ((rw)->rw_lock & RW_LOCK_READ ? NULL : \
95 (struct thread *)RW_OWNER((rw)->rw_lock))
98 * Returns if a write owner is recursed. Write ownership is not assured
99 * here and should be previously checked.
101 #define rw_recursed(rw) ((rw)->rw_recurse != 0)
104 * Return true if curthread helds the lock.
106 #define rw_wlocked(rw) (rw_wowner((rw)) == curthread)
109 * Return a pointer to the owning thread for this lock who should receive
110 * any priority lent by threads that block on this lock. Currently this
111 * is identical to rw_wowner().
113 #define rw_owner(rw) rw_wowner(rw)
116 #define _rw_assert(rw, what, file, line)
120 assert_rw(struct lock_object *lock, int what)
123 rw_assert((struct rwlock *)lock, what);
127 lock_rw(struct lock_object *lock, int how)
131 rw = (struct rwlock *)lock;
139 unlock_rw(struct lock_object *lock)
143 rw = (struct rwlock *)lock;
144 rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
145 if (rw->rw_lock & RW_LOCK_READ) {
156 owner_rw(struct lock_object *lock, struct thread **owner)
158 struct rwlock *rw = (struct rwlock *)lock;
159 uintptr_t x = rw->rw_lock;
161 *owner = rw_wowner(rw);
162 return ((x & RW_LOCK_READ) != 0 ? (RW_READERS(x) != 0) :
168 rw_init_flags(struct rwlock *rw, const char *name, int opts)
172 MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
174 ASSERT_ATOMIC_LOAD_PTR(rw->rw_lock,
175 ("%s: rw_lock not aligned for %s: %p", __func__, name,
178 flags = LO_UPGRADABLE;
181 if (opts & RW_NOPROFILE)
182 flags |= LO_NOPROFILE;
183 if (!(opts & RW_NOWITNESS))
185 if (opts & RW_RECURSE)
186 flags |= LO_RECURSABLE;
190 rw->rw_lock = RW_UNLOCKED;
192 lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
196 rw_destroy(struct rwlock *rw)
199 KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock %p not unlocked", rw));
200 KASSERT(rw->rw_recurse == 0, ("rw lock %p still recursed", rw));
201 rw->rw_lock = RW_DESTROYED;
202 lock_destroy(&rw->lock_object);
206 rw_sysinit(void *arg)
208 struct rw_args *args = arg;
210 rw_init(args->ra_rw, args->ra_desc);
214 rw_sysinit_flags(void *arg)
216 struct rw_args_flags *args = arg;
218 rw_init_flags(args->ra_rw, args->ra_desc, args->ra_flags);
222 rw_wowned(struct rwlock *rw)
225 return (rw_wowner(rw) == curthread);
229 _rw_wlock(struct rwlock *rw, const char *file, int line)
232 MPASS(curthread != NULL);
233 KASSERT(rw->rw_lock != RW_DESTROYED,
234 ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
235 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
237 __rw_wlock(rw, curthread, file, line);
238 LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
239 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
240 curthread->td_locks++;
244 _rw_try_wlock(struct rwlock *rw, const char *file, int line)
248 KASSERT(rw->rw_lock != RW_DESTROYED,
249 ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
251 if (rw_wlocked(rw) &&
252 (rw->lock_object.lo_flags & LO_RECURSABLE) != 0) {
256 rval = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_UNLOCKED,
257 (uintptr_t)curthread);
259 LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
261 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
263 curthread->td_locks++;
269 _rw_wunlock(struct rwlock *rw, const char *file, int line)
272 MPASS(curthread != NULL);
273 KASSERT(rw->rw_lock != RW_DESTROYED,
274 ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
275 _rw_assert(rw, RA_WLOCKED, file, line);
276 curthread->td_locks--;
277 WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
278 LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
280 if (!rw_recursed(rw))
281 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_RW_WUNLOCK_RELEASE, rw);
282 __rw_wunlock(rw, curthread, file, line);
285 * Determines whether a new reader can acquire a lock. Succeeds if the
286 * reader already owns a read lock and the lock is locked for read to
287 * prevent deadlock from reader recursion. Also succeeds if the lock
288 * is unlocked and has no writer waiters or spinners. Failing otherwise
289 * prioritizes writers before readers.
291 #define RW_CAN_READ(_rw) \
292 ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) & \
293 (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) == \
297 _rw_rlock(struct rwlock *rw, const char *file, int line)
299 struct turnstile *ts;
300 #ifdef ADAPTIVE_RWLOCKS
301 volatile struct thread *owner;
305 #ifdef LOCK_PROFILING
306 uint64_t waittime = 0;
311 uint64_t spin_cnt = 0;
312 uint64_t sleep_cnt = 0;
313 int64_t sleep_time = 0;
316 KASSERT(rw->rw_lock != RW_DESTROYED,
317 ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
318 KASSERT(rw_wowner(rw) != curthread,
319 ("%s (%s): wlock already held @ %s:%d", __func__,
320 rw->lock_object.lo_name, file, line));
321 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line, NULL);
328 * Handle the easy case. If no other thread has a write
329 * lock, then try to bump up the count of read locks. Note
330 * that we have to preserve the current state of the
331 * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a
332 * read lock, then rw_lock must have changed, so restart
333 * the loop. Note that this handles the case of a
334 * completely unlocked rwlock since such a lock is encoded
335 * as a read lock with no waiters.
338 if (RW_CAN_READ(v)) {
340 * The RW_LOCK_READ_WAITERS flag should only be set
341 * if the lock has been unlocked and write waiters
344 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v,
345 v + RW_ONE_READER)) {
346 if (LOCK_LOG_TEST(&rw->lock_object, 0))
348 "%s: %p succeed %p -> %p", __func__,
350 (void *)(v + RW_ONE_READER));
355 lock_profile_obtain_lock_failed(&rw->lock_object,
356 &contested, &waittime);
358 #ifdef ADAPTIVE_RWLOCKS
360 * If the owner is running on another CPU, spin until
361 * the owner stops running or the state of the lock
364 if ((v & RW_LOCK_READ) == 0) {
365 owner = (struct thread *)RW_OWNER(v);
366 if (TD_IS_RUNNING(owner)) {
367 if (LOCK_LOG_TEST(&rw->lock_object, 0))
369 "%s: spinning on %p held by %p",
370 __func__, rw, owner);
371 while ((struct thread*)RW_OWNER(rw->rw_lock) ==
372 owner && TD_IS_RUNNING(owner)) {
380 } else if (spintries < ROWNER_RETRIES) {
382 for (i = 0; i < ROWNER_LOOPS; i++) {
384 if ((v & RW_LOCK_READ) == 0 || RW_CAN_READ(v))
388 if (i != ROWNER_LOOPS)
394 * Okay, now it's the hard case. Some other thread already
395 * has a write lock or there are write waiters present,
396 * acquire the turnstile lock so we can begin the process
399 ts = turnstile_trywait(&rw->lock_object);
402 * The lock might have been released while we spun, so
403 * recheck its state and restart the loop if needed.
406 if (RW_CAN_READ(v)) {
407 turnstile_cancel(ts);
411 #ifdef ADAPTIVE_RWLOCKS
413 * The current lock owner might have started executing
414 * on another CPU (or the lock could have changed
415 * owners) while we were waiting on the turnstile
416 * chain lock. If so, drop the turnstile lock and try
419 if ((v & RW_LOCK_READ) == 0) {
420 owner = (struct thread *)RW_OWNER(v);
421 if (TD_IS_RUNNING(owner)) {
422 turnstile_cancel(ts);
429 * The lock is held in write mode or it already has waiters.
431 MPASS(!RW_CAN_READ(v));
434 * If the RW_LOCK_READ_WAITERS flag is already set, then
435 * we can go ahead and block. If it is not set then try
436 * to set it. If we fail to set it drop the turnstile
437 * lock and restart the loop.
439 if (!(v & RW_LOCK_READ_WAITERS)) {
440 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
441 v | RW_LOCK_READ_WAITERS)) {
442 turnstile_cancel(ts);
445 if (LOCK_LOG_TEST(&rw->lock_object, 0))
446 CTR2(KTR_LOCK, "%s: %p set read waiters flag",
451 * We were unable to acquire the lock and the read waiters
452 * flag is set, so we must block on the turnstile.
454 if (LOCK_LOG_TEST(&rw->lock_object, 0))
455 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
458 sleep_time -= lockstat_nsecs();
460 turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
462 sleep_time += lockstat_nsecs();
465 if (LOCK_LOG_TEST(&rw->lock_object, 0))
466 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
471 * TODO: acquire "owner of record" here. Here be turnstile dragons
472 * however. turnstiles don't like owners changing between calls to
473 * turnstile_wait() currently.
475 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_RLOCK_ACQUIRE, rw, contested,
476 waittime, file, line);
477 LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
478 WITNESS_LOCK(&rw->lock_object, 0, file, line);
479 curthread->td_locks++;
480 curthread->td_rw_rlocks++;
483 LOCKSTAT_RECORD1(LS_RW_RLOCK_BLOCK, rw, sleep_time);
486 * Record only the loops spinning and not sleeping.
488 if (spin_cnt > sleep_cnt)
489 LOCKSTAT_RECORD1(LS_RW_RLOCK_SPIN, rw, (spin_cnt - sleep_cnt));
494 _rw_try_rlock(struct rwlock *rw, const char *file, int line)
500 KASSERT(rw->rw_lock != RW_DESTROYED,
501 ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
502 if (!(x & RW_LOCK_READ))
504 if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, x + RW_ONE_READER)) {
505 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
507 WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
508 curthread->td_locks++;
509 curthread->td_rw_rlocks++;
514 LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
519 _rw_runlock(struct rwlock *rw, const char *file, int line)
521 struct turnstile *ts;
522 uintptr_t x, v, queue;
524 KASSERT(rw->rw_lock != RW_DESTROYED,
525 ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
526 _rw_assert(rw, RA_RLOCKED, file, line);
527 curthread->td_locks--;
528 curthread->td_rw_rlocks--;
529 WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
530 LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
532 /* TODO: drop "owner of record" here. */
536 * See if there is more than one read lock held. If so,
537 * just drop one and return.
540 if (RW_READERS(x) > 1) {
541 if (atomic_cmpset_rel_ptr(&rw->rw_lock, x,
542 x - RW_ONE_READER)) {
543 if (LOCK_LOG_TEST(&rw->lock_object, 0))
545 "%s: %p succeeded %p -> %p",
546 __func__, rw, (void *)x,
547 (void *)(x - RW_ONE_READER));
553 * If there aren't any waiters for a write lock, then try
554 * to drop it quickly.
556 if (!(x & RW_LOCK_WAITERS)) {
557 MPASS((x & ~RW_LOCK_WRITE_SPINNER) ==
559 if (atomic_cmpset_rel_ptr(&rw->rw_lock, x,
561 if (LOCK_LOG_TEST(&rw->lock_object, 0))
562 CTR2(KTR_LOCK, "%s: %p last succeeded",
569 * Ok, we know we have waiters and we think we are the
570 * last reader, so grab the turnstile lock.
572 turnstile_chain_lock(&rw->lock_object);
573 v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
574 MPASS(v & RW_LOCK_WAITERS);
577 * Try to drop our lock leaving the lock in a unlocked
580 * If you wanted to do explicit lock handoff you'd have to
581 * do it here. You'd also want to use turnstile_signal()
582 * and you'd have to handle the race where a higher
583 * priority thread blocks on the write lock before the
584 * thread you wakeup actually runs and have the new thread
585 * "steal" the lock. For now it's a lot simpler to just
586 * wakeup all of the waiters.
588 * As above, if we fail, then another thread might have
589 * acquired a read lock, so drop the turnstile lock and
593 if (v & RW_LOCK_WRITE_WAITERS) {
594 queue = TS_EXCLUSIVE_QUEUE;
595 x |= (v & RW_LOCK_READ_WAITERS);
597 queue = TS_SHARED_QUEUE;
598 if (!atomic_cmpset_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
600 turnstile_chain_unlock(&rw->lock_object);
603 if (LOCK_LOG_TEST(&rw->lock_object, 0))
604 CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
608 * Ok. The lock is released and all that's left is to
609 * wake up the waiters. Note that the lock might not be
610 * free anymore, but in that case the writers will just
611 * block again if they run before the new lock holder(s)
614 ts = turnstile_lookup(&rw->lock_object);
616 turnstile_broadcast(ts, queue);
617 turnstile_unpend(ts, TS_SHARED_LOCK);
618 turnstile_chain_unlock(&rw->lock_object);
621 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_RW_RUNLOCK_RELEASE, rw);
625 * This function is called when we are unable to obtain a write lock on the
626 * first try. This means that at least one other thread holds either a
627 * read or write lock.
630 _rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
632 struct turnstile *ts;
633 #ifdef ADAPTIVE_RWLOCKS
634 volatile struct thread *owner;
639 #ifdef LOCK_PROFILING
640 uint64_t waittime = 0;
644 uint64_t spin_cnt = 0;
645 uint64_t sleep_cnt = 0;
646 int64_t sleep_time = 0;
649 if (rw_wlocked(rw)) {
650 KASSERT(rw->lock_object.lo_flags & LO_RECURSABLE,
651 ("%s: recursing but non-recursive rw %s @ %s:%d\n",
652 __func__, rw->lock_object.lo_name, file, line));
654 if (LOCK_LOG_TEST(&rw->lock_object, 0))
655 CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
659 if (LOCK_LOG_TEST(&rw->lock_object, 0))
660 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
661 rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
663 while (!_rw_write_lock(rw, tid)) {
667 lock_profile_obtain_lock_failed(&rw->lock_object,
668 &contested, &waittime);
669 #ifdef ADAPTIVE_RWLOCKS
671 * If the lock is write locked and the owner is
672 * running on another CPU, spin until the owner stops
673 * running or the state of the lock changes.
676 owner = (struct thread *)RW_OWNER(v);
677 if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
678 if (LOCK_LOG_TEST(&rw->lock_object, 0))
679 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
680 __func__, rw, owner);
681 while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
682 TD_IS_RUNNING(owner)) {
690 if ((v & RW_LOCK_READ) && RW_READERS(v) &&
691 spintries < ROWNER_RETRIES) {
692 if (!(v & RW_LOCK_WRITE_SPINNER)) {
693 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
694 v | RW_LOCK_WRITE_SPINNER)) {
699 for (i = 0; i < ROWNER_LOOPS; i++) {
700 if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0)
705 spin_cnt += ROWNER_LOOPS - i;
707 if (i != ROWNER_LOOPS)
711 ts = turnstile_trywait(&rw->lock_object);
714 #ifdef ADAPTIVE_RWLOCKS
716 * The current lock owner might have started executing
717 * on another CPU (or the lock could have changed
718 * owners) while we were waiting on the turnstile
719 * chain lock. If so, drop the turnstile lock and try
722 if (!(v & RW_LOCK_READ)) {
723 owner = (struct thread *)RW_OWNER(v);
724 if (TD_IS_RUNNING(owner)) {
725 turnstile_cancel(ts);
731 * Check for the waiters flags about this rwlock.
732 * If the lock was released, without maintain any pending
733 * waiters queue, simply try to acquire it.
734 * If a pending waiters queue is present, claim the lock
735 * ownership and maintain the pending queue.
737 x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
738 if ((v & ~x) == RW_UNLOCKED) {
739 x &= ~RW_LOCK_WRITE_SPINNER;
740 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) {
744 turnstile_cancel(ts);
747 turnstile_cancel(ts);
751 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
752 * set it. If we fail to set it, then loop back and try
755 if (!(v & RW_LOCK_WRITE_WAITERS)) {
756 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
757 v | RW_LOCK_WRITE_WAITERS)) {
758 turnstile_cancel(ts);
761 if (LOCK_LOG_TEST(&rw->lock_object, 0))
762 CTR2(KTR_LOCK, "%s: %p set write waiters flag",
766 * We were unable to acquire the lock and the write waiters
767 * flag is set, so we must block on the turnstile.
769 if (LOCK_LOG_TEST(&rw->lock_object, 0))
770 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
773 sleep_time -= lockstat_nsecs();
775 turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
777 sleep_time += lockstat_nsecs();
780 if (LOCK_LOG_TEST(&rw->lock_object, 0))
781 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
783 #ifdef ADAPTIVE_RWLOCKS
787 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_WLOCK_ACQUIRE, rw, contested,
788 waittime, file, line);
791 LOCKSTAT_RECORD1(LS_RW_WLOCK_BLOCK, rw, sleep_time);
794 * Record only the loops spinning and not sleeping.
796 if (spin_cnt > sleep_cnt)
797 LOCKSTAT_RECORD1(LS_RW_WLOCK_SPIN, rw, (spin_cnt - sleep_cnt));
802 * This function is called if the first try at releasing a write lock failed.
803 * This means that one of the 2 waiter bits must be set indicating that at
804 * least one thread is waiting on this lock.
807 _rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
809 struct turnstile *ts;
813 if (rw_wlocked(rw) && rw_recursed(rw)) {
815 if (LOCK_LOG_TEST(&rw->lock_object, 0))
816 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
820 KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
821 ("%s: neither of the waiter flags are set", __func__));
823 if (LOCK_LOG_TEST(&rw->lock_object, 0))
824 CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
826 turnstile_chain_lock(&rw->lock_object);
827 ts = turnstile_lookup(&rw->lock_object);
831 * Use the same algo as sx locks for now. Prefer waking up shared
832 * waiters if we have any over writers. This is probably not ideal.
834 * 'v' is the value we are going to write back to rw_lock. If we
835 * have waiters on both queues, we need to preserve the state of
836 * the waiter flag for the queue we don't wake up. For now this is
837 * hardcoded for the algorithm mentioned above.
839 * In the case of both readers and writers waiting we wakeup the
840 * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a
841 * new writer comes in before a reader it will claim the lock up
842 * above. There is probably a potential priority inversion in
843 * there that could be worked around either by waking both queues
844 * of waiters or doing some complicated lock handoff gymnastics.
847 if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) {
848 queue = TS_EXCLUSIVE_QUEUE;
849 v |= (rw->rw_lock & RW_LOCK_READ_WAITERS);
851 queue = TS_SHARED_QUEUE;
853 /* Wake up all waiters for the specific queue. */
854 if (LOCK_LOG_TEST(&rw->lock_object, 0))
855 CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
856 queue == TS_SHARED_QUEUE ? "read" : "write");
857 turnstile_broadcast(ts, queue);
858 atomic_store_rel_ptr(&rw->rw_lock, v);
859 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
860 turnstile_chain_unlock(&rw->lock_object);
864 * Attempt to do a non-blocking upgrade from a read lock to a write
865 * lock. This will only succeed if this thread holds a single read
866 * lock. Returns true if the upgrade succeeded and false otherwise.
869 _rw_try_upgrade(struct rwlock *rw, const char *file, int line)
872 struct turnstile *ts;
875 KASSERT(rw->rw_lock != RW_DESTROYED,
876 ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
877 _rw_assert(rw, RA_RLOCKED, file, line);
880 * Attempt to switch from one reader to a writer. If there
881 * are any write waiters, then we will have to lock the
882 * turnstile first to prevent races with another writer
883 * calling turnstile_wait() before we have claimed this
884 * turnstile. So, do the simple case of no waiters first.
886 tid = (uintptr_t)curthread;
890 if (RW_READERS(v) > 1)
892 if (!(v & RW_LOCK_WAITERS)) {
893 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid);
900 * Ok, we think we have waiters, so lock the turnstile.
902 ts = turnstile_trywait(&rw->lock_object);
904 if (RW_READERS(v) > 1) {
905 turnstile_cancel(ts);
909 * Try to switch from one reader to a writer again. This time
910 * we honor the current state of the waiters flags.
911 * If we obtain the lock with the flags set, then claim
912 * ownership of the turnstile.
914 x = rw->rw_lock & RW_LOCK_WAITERS;
915 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x);
920 turnstile_cancel(ts);
923 turnstile_cancel(ts);
925 LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
927 curthread->td_rw_rlocks--;
928 WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
930 LOCKSTAT_RECORD0(LS_RW_TRYUPGRADE_UPGRADE, rw);
936 * Downgrade a write lock into a single read lock.
939 _rw_downgrade(struct rwlock *rw, const char *file, int line)
941 struct turnstile *ts;
945 KASSERT(rw->rw_lock != RW_DESTROYED,
946 ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
947 _rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
950 panic("downgrade of a recursed lock");
953 WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
956 * Convert from a writer to a single reader. First we handle
957 * the easy case with no waiters. If there are any waiters, we
958 * lock the turnstile and "disown" the lock.
960 tid = (uintptr_t)curthread;
961 if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
965 * Ok, we think we have waiters, so lock the turnstile so we can
966 * read the waiter flags without any races.
968 turnstile_chain_lock(&rw->lock_object);
969 v = rw->rw_lock & RW_LOCK_WAITERS;
970 rwait = v & RW_LOCK_READ_WAITERS;
971 wwait = v & RW_LOCK_WRITE_WAITERS;
972 MPASS(rwait | wwait);
975 * Downgrade from a write lock while preserving waiters flag
976 * and give up ownership of the turnstile.
978 ts = turnstile_lookup(&rw->lock_object);
981 v &= ~RW_LOCK_READ_WAITERS;
982 atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
984 * Wake other readers if there are no writers pending. Otherwise they
985 * won't be able to acquire the lock anyway.
987 if (rwait && !wwait) {
988 turnstile_broadcast(ts, TS_SHARED_QUEUE);
989 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
991 turnstile_disown(ts);
992 turnstile_chain_unlock(&rw->lock_object);
994 curthread->td_rw_rlocks++;
995 LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
996 LOCKSTAT_RECORD0(LS_RW_DOWNGRADE_DOWNGRADE, rw);
999 #ifdef INVARIANT_SUPPORT
1005 * In the non-WITNESS case, rw_assert() can only detect that at least
1006 * *some* thread owns an rlock, but it cannot guarantee that *this*
1007 * thread owns an rlock.
1010 _rw_assert(struct rwlock *rw, int what, const char *file, int line)
1013 if (panicstr != NULL)
1017 case RA_LOCKED | RA_RECURSED:
1018 case RA_LOCKED | RA_NOTRECURSED:
1021 witness_assert(&rw->lock_object, what, file, line);
1024 * If some other thread has a write lock or we have one
1025 * and are asserting a read lock, fail. Also, if no one
1026 * has a lock at all, fail.
1028 if (rw->rw_lock == RW_UNLOCKED ||
1029 (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
1030 rw_wowner(rw) != curthread)))
1031 panic("Lock %s not %slocked @ %s:%d\n",
1032 rw->lock_object.lo_name, (what == RA_RLOCKED) ?
1033 "read " : "", file, line);
1035 if (!(rw->rw_lock & RW_LOCK_READ)) {
1036 if (rw_recursed(rw)) {
1037 if (what & RA_NOTRECURSED)
1038 panic("Lock %s recursed @ %s:%d\n",
1039 rw->lock_object.lo_name, file,
1041 } else if (what & RA_RECURSED)
1042 panic("Lock %s not recursed @ %s:%d\n",
1043 rw->lock_object.lo_name, file, line);
1048 case RA_WLOCKED | RA_RECURSED:
1049 case RA_WLOCKED | RA_NOTRECURSED:
1050 if (rw_wowner(rw) != curthread)
1051 panic("Lock %s not exclusively locked @ %s:%d\n",
1052 rw->lock_object.lo_name, file, line);
1053 if (rw_recursed(rw)) {
1054 if (what & RA_NOTRECURSED)
1055 panic("Lock %s recursed @ %s:%d\n",
1056 rw->lock_object.lo_name, file, line);
1057 } else if (what & RA_RECURSED)
1058 panic("Lock %s not recursed @ %s:%d\n",
1059 rw->lock_object.lo_name, file, line);
1063 witness_assert(&rw->lock_object, what, file, line);
1066 * If we hold a write lock fail. We can't reliably check
1067 * to see if we hold a read lock or not.
1069 if (rw_wowner(rw) == curthread)
1070 panic("Lock %s exclusively locked @ %s:%d\n",
1071 rw->lock_object.lo_name, file, line);
1075 panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
1079 #endif /* INVARIANT_SUPPORT */
1083 db_show_rwlock(struct lock_object *lock)
1088 rw = (struct rwlock *)lock;
1090 db_printf(" state: ");
1091 if (rw->rw_lock == RW_UNLOCKED)
1092 db_printf("UNLOCKED\n");
1093 else if (rw->rw_lock == RW_DESTROYED) {
1094 db_printf("DESTROYED\n");
1096 } else if (rw->rw_lock & RW_LOCK_READ)
1097 db_printf("RLOCK: %ju locks\n",
1098 (uintmax_t)(RW_READERS(rw->rw_lock)));
1101 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1102 td->td_tid, td->td_proc->p_pid, td->td_name);
1103 if (rw_recursed(rw))
1104 db_printf(" recursed: %u\n", rw->rw_recurse);
1106 db_printf(" waiters: ");
1107 switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
1108 case RW_LOCK_READ_WAITERS:
1109 db_printf("readers\n");
1111 case RW_LOCK_WRITE_WAITERS:
1112 db_printf("writers\n");
1114 case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
1115 db_printf("readers and writers\n");
1118 db_printf("none\n");