kern_rwlock.c revision 173733
1154941Sjhb/*- 2154941Sjhb * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org> 3154941Sjhb * All rights reserved. 4154941Sjhb * 5154941Sjhb * Redistribution and use in source and binary forms, with or without 6154941Sjhb * modification, are permitted provided that the following conditions 7154941Sjhb * are met: 8154941Sjhb * 1. Redistributions of source code must retain the above copyright 9154941Sjhb * notice, this list of conditions and the following disclaimer. 10154941Sjhb * 2. Redistributions in binary form must reproduce the above copyright 11154941Sjhb * notice, this list of conditions and the following disclaimer in the 12154941Sjhb * documentation and/or other materials provided with the distribution. 13154941Sjhb * 3. Neither the name of the author nor the names of any co-contributors 14154941Sjhb * may be used to endorse or promote products derived from this software 15154941Sjhb * without specific prior written permission. 16154941Sjhb * 17154941Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18154941Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19154941Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20154941Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21154941Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22154941Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23154941Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24154941Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25154941Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26154941Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27154941Sjhb * SUCH DAMAGE. 28154941Sjhb */ 29154941Sjhb 30154941Sjhb/* 31154941Sjhb * Machine independent bits of reader/writer lock implementation. 32154941Sjhb */ 33154941Sjhb 34154941Sjhb#include <sys/cdefs.h> 35154941Sjhb__FBSDID("$FreeBSD: head/sys/kern/kern_rwlock.c 173733 2007-11-18 14:43:53Z attilio $"); 36154941Sjhb 37154941Sjhb#include "opt_ddb.h" 38167801Sjhb#include "opt_no_adaptive_rwlocks.h" 39154941Sjhb 40154941Sjhb#include <sys/param.h> 41154941Sjhb#include <sys/ktr.h> 42154941Sjhb#include <sys/lock.h> 43154941Sjhb#include <sys/mutex.h> 44154941Sjhb#include <sys/proc.h> 45154941Sjhb#include <sys/rwlock.h> 46154941Sjhb#include <sys/systm.h> 47154941Sjhb#include <sys/turnstile.h> 48171516Sattilio 49154941Sjhb#include <machine/cpu.h> 50154941Sjhb 51171052SattilioCTASSERT((RW_RECURSE & LO_CLASSFLAGS) == RW_RECURSE); 52171052Sattilio 53167801Sjhb#if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS) 54167801Sjhb#define ADAPTIVE_RWLOCKS 55167801Sjhb#endif 56167801Sjhb 57154941Sjhb#ifdef DDB 58154941Sjhb#include <ddb/ddb.h> 59154941Sjhb 60154941Sjhbstatic void db_show_rwlock(struct lock_object *lock); 61154941Sjhb#endif 62173733Sattiliostatic void assert_rw(struct lock_object *lock, int what); 63167368Sjhbstatic void lock_rw(struct lock_object *lock, int how); 64167368Sjhbstatic int unlock_rw(struct lock_object *lock); 65154941Sjhb 66154941Sjhbstruct lock_class lock_class_rw = { 67167365Sjhb .lc_name = "rw", 68167365Sjhb .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE, 69173733Sattilio .lc_assert = assert_rw, 70154941Sjhb#ifdef DDB 71167365Sjhb .lc_ddb_show = db_show_rwlock, 72154941Sjhb#endif 73167368Sjhb .lc_lock = lock_rw, 74167368Sjhb .lc_unlock = unlock_rw, 75154941Sjhb}; 76154941Sjhb 77157826Sjhb/* 78157826Sjhb * Return a pointer to the owning thread if the lock is write-locked or 79157826Sjhb * NULL if the lock is unlocked or read-locked. 80157826Sjhb */ 81157826Sjhb#define rw_wowner(rw) \ 82154941Sjhb ((rw)->rw_lock & RW_LOCK_READ ? NULL : \ 83154941Sjhb (struct thread *)RW_OWNER((rw)->rw_lock)) 84154941Sjhb 85157826Sjhb/* 86171052Sattilio * Returns if a write owner is recursed. Write ownership is not assured 87171052Sattilio * here and should be previously checked. 88171052Sattilio */ 89171052Sattilio#define rw_recursed(rw) ((rw)->rw_recurse != 0) 90171052Sattilio 91171052Sattilio/* 92171052Sattilio * Return true if curthread helds the lock. 93171052Sattilio */ 94171052Sattilio#define rw_wlocked(rw) (rw_wowner((rw)) == curthread) 95171052Sattilio 96171052Sattilio/* 97157826Sjhb * Return a pointer to the owning thread for this lock who should receive 98157826Sjhb * any priority lent by threads that block on this lock. Currently this 99157826Sjhb * is identical to rw_wowner(). 100157826Sjhb */ 101157826Sjhb#define rw_owner(rw) rw_wowner(rw) 102157826Sjhb 103154941Sjhb#ifndef INVARIANTS 104154941Sjhb#define _rw_assert(rw, what, file, line) 105154941Sjhb#endif 106154941Sjhb 107154941Sjhbvoid 108173733Sattilioassert_rw(struct lock_object *lock, int what) 109173733Sattilio{ 110173733Sattilio 111173733Sattilio rw_assert((struct rwlock *)lock, what); 112173733Sattilio} 113173733Sattilio 114173733Sattiliovoid 115167368Sjhblock_rw(struct lock_object *lock, int how) 116167368Sjhb{ 117167368Sjhb struct rwlock *rw; 118167368Sjhb 119167368Sjhb rw = (struct rwlock *)lock; 120167368Sjhb if (how) 121167368Sjhb rw_wlock(rw); 122167368Sjhb else 123167368Sjhb rw_rlock(rw); 124167368Sjhb} 125167368Sjhb 126167368Sjhbint 127167368Sjhbunlock_rw(struct lock_object *lock) 128167368Sjhb{ 129167368Sjhb struct rwlock *rw; 130167368Sjhb 131167368Sjhb rw = (struct rwlock *)lock; 132167368Sjhb rw_assert(rw, RA_LOCKED | LA_NOTRECURSED); 133167368Sjhb if (rw->rw_lock & RW_LOCK_READ) { 134167368Sjhb rw_runlock(rw); 135167368Sjhb return (0); 136167368Sjhb } else { 137167368Sjhb rw_wunlock(rw); 138167368Sjhb return (1); 139167368Sjhb } 140167368Sjhb} 141167368Sjhb 142167368Sjhbvoid 143171052Sattiliorw_init_flags(struct rwlock *rw, const char *name, int opts) 144154941Sjhb{ 145171052Sattilio int flags; 146154941Sjhb 147171052Sattilio MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET | 148171052Sattilio RW_RECURSE)) == 0); 149171052Sattilio 150171052Sattilio flags = LO_UPGRADABLE | LO_RECURSABLE; 151171052Sattilio if (opts & RW_DUPOK) 152171052Sattilio flags |= LO_DUPOK; 153171052Sattilio if (opts & RW_NOPROFILE) 154171052Sattilio flags |= LO_NOPROFILE; 155171052Sattilio if (!(opts & RW_NOWITNESS)) 156171052Sattilio flags |= LO_WITNESS; 157171052Sattilio if (opts & RW_QUIET) 158171052Sattilio flags |= LO_QUIET; 159171052Sattilio flags |= opts & RW_RECURSE; 160171052Sattilio 161154941Sjhb rw->rw_lock = RW_UNLOCKED; 162171052Sattilio rw->rw_recurse = 0; 163171052Sattilio lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags); 164154941Sjhb} 165154941Sjhb 166154941Sjhbvoid 167154941Sjhbrw_destroy(struct rwlock *rw) 168154941Sjhb{ 169154941Sjhb 170154941Sjhb KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked")); 171171052Sattilio KASSERT(rw->rw_recurse == 0, ("rw lock still recursed")); 172169394Sjhb rw->rw_lock = RW_DESTROYED; 173167787Sjhb lock_destroy(&rw->lock_object); 174154941Sjhb} 175154941Sjhb 176154941Sjhbvoid 177154941Sjhbrw_sysinit(void *arg) 178154941Sjhb{ 179154941Sjhb struct rw_args *args = arg; 180154941Sjhb 181154941Sjhb rw_init(args->ra_rw, args->ra_desc); 182154941Sjhb} 183154941Sjhb 184167024Srwatsonint 185167024Srwatsonrw_wowned(struct rwlock *rw) 186167024Srwatson{ 187167024Srwatson 188167024Srwatson return (rw_wowner(rw) == curthread); 189167024Srwatson} 190167024Srwatson 191154941Sjhbvoid 192154941Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line) 193154941Sjhb{ 194154941Sjhb 195154941Sjhb MPASS(curthread != NULL); 196169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 197169394Sjhb ("rw_wlock() of destroyed rwlock @ %s:%d", file, line)); 198167787Sjhb WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 199154941Sjhb line); 200154941Sjhb __rw_wlock(rw, curthread, file, line); 201171052Sattilio LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line); 202167787Sjhb WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line); 203160771Sjhb curthread->td_locks++; 204154941Sjhb} 205154941Sjhb 206154941Sjhbvoid 207154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line) 208154941Sjhb{ 209154941Sjhb 210154941Sjhb MPASS(curthread != NULL); 211169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 212169394Sjhb ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line)); 213154941Sjhb _rw_assert(rw, RA_WLOCKED, file, line); 214160771Sjhb curthread->td_locks--; 215167787Sjhb WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line); 216171052Sattilio LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file, 217171052Sattilio line); 218171052Sattilio if (!rw_recursed(rw)) 219171052Sattilio lock_profile_release_lock(&rw->lock_object); 220154941Sjhb __rw_wunlock(rw, curthread, file, line); 221154941Sjhb} 222154941Sjhb 223154941Sjhbvoid 224154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line) 225154941Sjhb{ 226170295Sjeff struct turnstile *ts; 227167801Sjhb#ifdef ADAPTIVE_RWLOCKS 228157846Sjhb volatile struct thread *owner; 229157851Swkoszek#endif 230171516Sattilio#ifdef LOCK_PROFILING_SHARED 231167307Sjhb uint64_t waittime = 0; 232167054Skmacy int contested = 0; 233171516Sattilio#endif 234154941Sjhb uintptr_t x; 235154941Sjhb 236169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 237169394Sjhb ("rw_rlock() of destroyed rwlock @ %s:%d", file, line)); 238157826Sjhb KASSERT(rw_wowner(rw) != curthread, 239154941Sjhb ("%s (%s): wlock already held @ %s:%d", __func__, 240167787Sjhb rw->lock_object.lo_name, file, line)); 241167787Sjhb WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line); 242154941Sjhb 243154941Sjhb /* 244154941Sjhb * Note that we don't make any attempt to try to block read 245154941Sjhb * locks once a writer has blocked on the lock. The reason is 246154941Sjhb * that we currently allow for read locks to recurse and we 247154941Sjhb * don't keep track of all the holders of read locks. Thus, if 248154941Sjhb * we were to block readers once a writer blocked and a reader 249154941Sjhb * tried to recurse on their reader lock after a writer had 250154941Sjhb * blocked we would end up in a deadlock since the reader would 251154941Sjhb * be blocked on the writer, and the writer would be blocked 252154941Sjhb * waiting for the reader to release its original read lock. 253154941Sjhb */ 254154941Sjhb for (;;) { 255154941Sjhb /* 256154941Sjhb * Handle the easy case. If no other thread has a write 257154941Sjhb * lock, then try to bump up the count of read locks. Note 258154941Sjhb * that we have to preserve the current state of the 259154941Sjhb * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a 260154941Sjhb * read lock, then rw_lock must have changed, so restart 261154941Sjhb * the loop. Note that this handles the case of a 262154941Sjhb * completely unlocked rwlock since such a lock is encoded 263154941Sjhb * as a read lock with no waiters. 264154941Sjhb */ 265154941Sjhb x = rw->rw_lock; 266154941Sjhb if (x & RW_LOCK_READ) { 267154941Sjhb 268154941Sjhb /* 269154941Sjhb * The RW_LOCK_READ_WAITERS flag should only be set 270154941Sjhb * if another thread currently holds a write lock, 271154941Sjhb * and in that case RW_LOCK_READ should be clear. 272154941Sjhb */ 273154941Sjhb MPASS((x & RW_LOCK_READ_WAITERS) == 0); 274154941Sjhb if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, 275154941Sjhb x + RW_ONE_READER)) { 276171516Sattilio#ifdef LOCK_PROFILING_SHARED 277171516Sattilio if (RW_READERS(x) == 0) 278171516Sattilio lock_profile_obtain_lock_success( 279171516Sattilio &rw->lock_object, contested, 280171516Sattilio waittime, file, line); 281171516Sattilio#endif 282167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 283154941Sjhb CTR4(KTR_LOCK, 284154941Sjhb "%s: %p succeed %p -> %p", __func__, 285154941Sjhb rw, (void *)x, 286154941Sjhb (void *)(x + RW_ONE_READER)); 287154941Sjhb break; 288154941Sjhb } 289157846Sjhb cpu_spinwait(); 290154941Sjhb continue; 291154941Sjhb } 292154941Sjhb 293154941Sjhb /* 294154941Sjhb * Okay, now it's the hard case. Some other thread already 295154941Sjhb * has a write lock, so acquire the turnstile lock so we can 296154941Sjhb * begin the process of blocking. 297154941Sjhb */ 298170295Sjeff ts = turnstile_trywait(&rw->lock_object); 299154941Sjhb 300154941Sjhb /* 301154941Sjhb * The lock might have been released while we spun, so 302154941Sjhb * recheck its state and restart the loop if there is no 303154941Sjhb * longer a write lock. 304154941Sjhb */ 305154941Sjhb x = rw->rw_lock; 306154941Sjhb if (x & RW_LOCK_READ) { 307170295Sjeff turnstile_cancel(ts); 308157846Sjhb cpu_spinwait(); 309154941Sjhb continue; 310154941Sjhb } 311154941Sjhb 312154941Sjhb /* 313154941Sjhb * Ok, it's still a write lock. If the RW_LOCK_READ_WAITERS 314154941Sjhb * flag is already set, then we can go ahead and block. If 315154941Sjhb * it is not set then try to set it. If we fail to set it 316154941Sjhb * drop the turnstile lock and restart the loop. 317154941Sjhb */ 318157826Sjhb if (!(x & RW_LOCK_READ_WAITERS)) { 319157826Sjhb if (!atomic_cmpset_ptr(&rw->rw_lock, x, 320157826Sjhb x | RW_LOCK_READ_WAITERS)) { 321170295Sjeff turnstile_cancel(ts); 322157826Sjhb cpu_spinwait(); 323157826Sjhb continue; 324157826Sjhb } 325167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 326157826Sjhb CTR2(KTR_LOCK, "%s: %p set read waiters flag", 327157826Sjhb __func__, rw); 328154941Sjhb } 329154941Sjhb 330167801Sjhb#ifdef ADAPTIVE_RWLOCKS 331154941Sjhb /* 332157846Sjhb * If the owner is running on another CPU, spin until 333157846Sjhb * the owner stops running or the state of the lock 334157846Sjhb * changes. 335157846Sjhb */ 336157846Sjhb owner = (struct thread *)RW_OWNER(x); 337157846Sjhb if (TD_IS_RUNNING(owner)) { 338170295Sjeff turnstile_cancel(ts); 339167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 340157846Sjhb CTR3(KTR_LOCK, "%s: spinning on %p held by %p", 341157846Sjhb __func__, rw, owner); 342171516Sattilio#ifdef LOCK_PROFILING_SHARED 343171516Sattilio lock_profile_obtain_lock_failed(&rw->lock_object, 344171516Sattilio &contested, &waittime); 345171516Sattilio#endif 346157846Sjhb while ((struct thread*)RW_OWNER(rw->rw_lock)== owner && 347157846Sjhb TD_IS_RUNNING(owner)) 348157846Sjhb cpu_spinwait(); 349157846Sjhb continue; 350157846Sjhb } 351157846Sjhb#endif 352157846Sjhb 353157846Sjhb /* 354154941Sjhb * We were unable to acquire the lock and the read waiters 355154941Sjhb * flag is set, so we must block on the turnstile. 356154941Sjhb */ 357167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 358154941Sjhb CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 359154941Sjhb rw); 360171516Sattilio#ifdef LOCK_PROFILING_SHARED 361171516Sattilio lock_profile_obtain_lock_failed(&rw->lock_object, &contested, 362171516Sattilio &waittime); 363171516Sattilio#endif 364170295Sjeff turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE); 365167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 366154941Sjhb CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 367154941Sjhb __func__, rw); 368154941Sjhb } 369154941Sjhb 370154941Sjhb /* 371154941Sjhb * TODO: acquire "owner of record" here. Here be turnstile dragons 372154941Sjhb * however. turnstiles don't like owners changing between calls to 373154941Sjhb * turnstile_wait() currently. 374154941Sjhb */ 375154941Sjhb 376167787Sjhb LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line); 377167787Sjhb WITNESS_LOCK(&rw->lock_object, 0, file, line); 378160771Sjhb curthread->td_locks++; 379154941Sjhb} 380154941Sjhb 381154941Sjhbvoid 382154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line) 383154941Sjhb{ 384154941Sjhb struct turnstile *ts; 385154941Sjhb uintptr_t x; 386154941Sjhb 387169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 388169394Sjhb ("rw_runlock() of destroyed rwlock @ %s:%d", file, line)); 389154941Sjhb _rw_assert(rw, RA_RLOCKED, file, line); 390160771Sjhb curthread->td_locks--; 391167787Sjhb WITNESS_UNLOCK(&rw->lock_object, 0, file, line); 392167787Sjhb LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line); 393154941Sjhb 394154941Sjhb /* TODO: drop "owner of record" here. */ 395154941Sjhb 396154941Sjhb for (;;) { 397154941Sjhb /* 398154941Sjhb * See if there is more than one read lock held. If so, 399154941Sjhb * just drop one and return. 400154941Sjhb */ 401154941Sjhb x = rw->rw_lock; 402154941Sjhb if (RW_READERS(x) > 1) { 403154941Sjhb if (atomic_cmpset_ptr(&rw->rw_lock, x, 404154941Sjhb x - RW_ONE_READER)) { 405167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 406154941Sjhb CTR4(KTR_LOCK, 407154941Sjhb "%s: %p succeeded %p -> %p", 408154941Sjhb __func__, rw, (void *)x, 409154941Sjhb (void *)(x - RW_ONE_READER)); 410154941Sjhb break; 411154941Sjhb } 412154941Sjhb continue; 413167307Sjhb } 414154941Sjhb 415164159Skmacy 416154941Sjhb /* 417154941Sjhb * We should never have read waiters while at least one 418154941Sjhb * thread holds a read lock. (See note above) 419154941Sjhb */ 420154941Sjhb KASSERT(!(x & RW_LOCK_READ_WAITERS), 421154941Sjhb ("%s: waiting readers", __func__)); 422171516Sattilio#ifdef LOCK_PROFILING_SHARED 423171516Sattilio lock_profile_release_lock(&rw->lock_object); 424171516Sattilio#endif 425154941Sjhb 426154941Sjhb /* 427154941Sjhb * If there aren't any waiters for a write lock, then try 428154941Sjhb * to drop it quickly. 429154941Sjhb */ 430154941Sjhb if (!(x & RW_LOCK_WRITE_WAITERS)) { 431154941Sjhb 432154941Sjhb /* 433154941Sjhb * There shouldn't be any flags set and we should 434154941Sjhb * be the only read lock. If we fail to release 435154941Sjhb * the single read lock, then another thread might 436154941Sjhb * have just acquired a read lock, so go back up 437154941Sjhb * to the multiple read locks case. 438154941Sjhb */ 439154941Sjhb MPASS(x == RW_READERS_LOCK(1)); 440154941Sjhb if (atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1), 441154941Sjhb RW_UNLOCKED)) { 442167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 443154941Sjhb CTR2(KTR_LOCK, "%s: %p last succeeded", 444154941Sjhb __func__, rw); 445154941Sjhb break; 446154941Sjhb } 447154941Sjhb continue; 448154941Sjhb } 449154941Sjhb 450154941Sjhb /* 451154941Sjhb * There should just be one reader with one or more 452154941Sjhb * writers waiting. 453154941Sjhb */ 454154941Sjhb MPASS(x == (RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS)); 455154941Sjhb 456154941Sjhb /* 457154941Sjhb * Ok, we know we have a waiting writer and we think we 458154941Sjhb * are the last reader, so grab the turnstile lock. 459154941Sjhb */ 460170295Sjeff turnstile_chain_lock(&rw->lock_object); 461154941Sjhb 462154941Sjhb /* 463154941Sjhb * Try to drop our lock leaving the lock in a unlocked 464154941Sjhb * state. 465154941Sjhb * 466154941Sjhb * If you wanted to do explicit lock handoff you'd have to 467154941Sjhb * do it here. You'd also want to use turnstile_signal() 468154941Sjhb * and you'd have to handle the race where a higher 469154941Sjhb * priority thread blocks on the write lock before the 470154941Sjhb * thread you wakeup actually runs and have the new thread 471154941Sjhb * "steal" the lock. For now it's a lot simpler to just 472154941Sjhb * wakeup all of the waiters. 473154941Sjhb * 474154941Sjhb * As above, if we fail, then another thread might have 475154941Sjhb * acquired a read lock, so drop the turnstile lock and 476154941Sjhb * restart. 477154941Sjhb */ 478154941Sjhb if (!atomic_cmpset_ptr(&rw->rw_lock, 479154941Sjhb RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS, RW_UNLOCKED)) { 480170295Sjeff turnstile_chain_unlock(&rw->lock_object); 481154941Sjhb continue; 482154941Sjhb } 483167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 484154941Sjhb CTR2(KTR_LOCK, "%s: %p last succeeded with waiters", 485154941Sjhb __func__, rw); 486154941Sjhb 487154941Sjhb /* 488154941Sjhb * Ok. The lock is released and all that's left is to 489154941Sjhb * wake up the waiters. Note that the lock might not be 490154941Sjhb * free anymore, but in that case the writers will just 491154941Sjhb * block again if they run before the new lock holder(s) 492154941Sjhb * release the lock. 493154941Sjhb */ 494167787Sjhb ts = turnstile_lookup(&rw->lock_object); 495157846Sjhb MPASS(ts != NULL); 496154941Sjhb turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 497154941Sjhb turnstile_unpend(ts, TS_SHARED_LOCK); 498170295Sjeff turnstile_chain_unlock(&rw->lock_object); 499154941Sjhb break; 500154941Sjhb } 501154941Sjhb} 502154941Sjhb 503154941Sjhb/* 504154941Sjhb * This function is called when we are unable to obtain a write lock on the 505154941Sjhb * first try. This means that at least one other thread holds either a 506154941Sjhb * read or write lock. 507154941Sjhb */ 508154941Sjhbvoid 509154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line) 510154941Sjhb{ 511170295Sjeff struct turnstile *ts; 512167801Sjhb#ifdef ADAPTIVE_RWLOCKS 513157846Sjhb volatile struct thread *owner; 514157851Swkoszek#endif 515171516Sattilio uint64_t waittime = 0; 516154941Sjhb uintptr_t v; 517171516Sattilio int contested = 0; 518154941Sjhb 519171052Sattilio if (rw_wlocked(rw)) { 520171052Sattilio KASSERT(rw->lock_object.lo_flags & RW_RECURSE, 521171052Sattilio ("%s: recursing but non-recursive rw %s @ %s:%d\n", 522171052Sattilio __func__, rw->lock_object.lo_name, file, line)); 523171052Sattilio rw->rw_recurse++; 524171052Sattilio atomic_set_ptr(&rw->rw_lock, RW_LOCK_RECURSED); 525171052Sattilio if (LOCK_LOG_TEST(&rw->lock_object, 0)) 526171052Sattilio CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw); 527171052Sattilio return; 528171052Sattilio } 529171052Sattilio 530167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 531154941Sjhb CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 532167787Sjhb rw->lock_object.lo_name, (void *)rw->rw_lock, file, line); 533154941Sjhb 534154941Sjhb while (!_rw_write_lock(rw, tid)) { 535170295Sjeff ts = turnstile_trywait(&rw->lock_object); 536154941Sjhb v = rw->rw_lock; 537154941Sjhb 538154941Sjhb /* 539154941Sjhb * If the lock was released while spinning on the 540154941Sjhb * turnstile chain lock, try again. 541154941Sjhb */ 542154941Sjhb if (v == RW_UNLOCKED) { 543170295Sjeff turnstile_cancel(ts); 544154941Sjhb cpu_spinwait(); 545154941Sjhb continue; 546154941Sjhb } 547154941Sjhb 548154941Sjhb /* 549154941Sjhb * If the lock was released by a writer with both readers 550154941Sjhb * and writers waiting and a reader hasn't woken up and 551154941Sjhb * acquired the lock yet, rw_lock will be set to the 552154941Sjhb * value RW_UNLOCKED | RW_LOCK_WRITE_WAITERS. If we see 553154941Sjhb * that value, try to acquire it once. Note that we have 554154941Sjhb * to preserve the RW_LOCK_WRITE_WAITERS flag as there are 555168073Sjhb * other writers waiting still. If we fail, restart the 556154941Sjhb * loop. 557154941Sjhb */ 558154941Sjhb if (v == (RW_UNLOCKED | RW_LOCK_WRITE_WAITERS)) { 559154941Sjhb if (atomic_cmpset_acq_ptr(&rw->rw_lock, 560154941Sjhb RW_UNLOCKED | RW_LOCK_WRITE_WAITERS, 561154941Sjhb tid | RW_LOCK_WRITE_WAITERS)) { 562170295Sjeff turnstile_claim(ts); 563154941Sjhb CTR2(KTR_LOCK, "%s: %p claimed by new writer", 564154941Sjhb __func__, rw); 565154941Sjhb break; 566154941Sjhb } 567170295Sjeff turnstile_cancel(ts); 568154941Sjhb cpu_spinwait(); 569154941Sjhb continue; 570154941Sjhb } 571154941Sjhb 572154941Sjhb /* 573154941Sjhb * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to 574154941Sjhb * set it. If we fail to set it, then loop back and try 575154941Sjhb * again. 576154941Sjhb */ 577157826Sjhb if (!(v & RW_LOCK_WRITE_WAITERS)) { 578157826Sjhb if (!atomic_cmpset_ptr(&rw->rw_lock, v, 579157826Sjhb v | RW_LOCK_WRITE_WAITERS)) { 580170295Sjeff turnstile_cancel(ts); 581157826Sjhb cpu_spinwait(); 582157826Sjhb continue; 583157826Sjhb } 584167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 585157826Sjhb CTR2(KTR_LOCK, "%s: %p set write waiters flag", 586157826Sjhb __func__, rw); 587154941Sjhb } 588154941Sjhb 589167801Sjhb#ifdef ADAPTIVE_RWLOCKS 590157846Sjhb /* 591157846Sjhb * If the lock is write locked and the owner is 592157846Sjhb * running on another CPU, spin until the owner stops 593157846Sjhb * running or the state of the lock changes. 594157846Sjhb */ 595157846Sjhb owner = (struct thread *)RW_OWNER(v); 596157846Sjhb if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) { 597170295Sjeff turnstile_cancel(ts); 598167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 599157846Sjhb CTR3(KTR_LOCK, "%s: spinning on %p held by %p", 600157846Sjhb __func__, rw, owner); 601171516Sattilio lock_profile_obtain_lock_failed(&rw->lock_object, 602171516Sattilio &contested, &waittime); 603157846Sjhb while ((struct thread*)RW_OWNER(rw->rw_lock)== owner && 604157846Sjhb TD_IS_RUNNING(owner)) 605157846Sjhb cpu_spinwait(); 606157846Sjhb continue; 607157846Sjhb } 608157846Sjhb#endif 609154941Sjhb 610154941Sjhb /* 611154941Sjhb * We were unable to acquire the lock and the write waiters 612154941Sjhb * flag is set, so we must block on the turnstile. 613154941Sjhb */ 614167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 615154941Sjhb CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 616154941Sjhb rw); 617171516Sattilio lock_profile_obtain_lock_failed(&rw->lock_object, &contested, 618171516Sattilio &waittime); 619170295Sjeff turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE); 620167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 621154941Sjhb CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 622154941Sjhb __func__, rw); 623154941Sjhb } 624171516Sattilio lock_profile_obtain_lock_success(&rw->lock_object, contested, waittime, 625171516Sattilio file, line); 626154941Sjhb} 627154941Sjhb 628154941Sjhb/* 629154941Sjhb * This function is called if the first try at releasing a write lock failed. 630154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at 631154941Sjhb * least one thread is waiting on this lock. 632154941Sjhb */ 633154941Sjhbvoid 634154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line) 635154941Sjhb{ 636154941Sjhb struct turnstile *ts; 637154941Sjhb uintptr_t v; 638154941Sjhb int queue; 639154941Sjhb 640171052Sattilio if (rw_wlocked(rw) && rw_recursed(rw)) { 641171052Sattilio if ((--rw->rw_recurse) == 0) 642171052Sattilio atomic_clear_ptr(&rw->rw_lock, RW_LOCK_RECURSED); 643171052Sattilio if (LOCK_LOG_TEST(&rw->lock_object, 0)) 644171052Sattilio CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw); 645171052Sattilio return; 646171052Sattilio } 647171052Sattilio 648154941Sjhb KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS), 649154941Sjhb ("%s: neither of the waiter flags are set", __func__)); 650154941Sjhb 651167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 652154941Sjhb CTR2(KTR_LOCK, "%s: %p contested", __func__, rw); 653154941Sjhb 654170295Sjeff turnstile_chain_lock(&rw->lock_object); 655167787Sjhb ts = turnstile_lookup(&rw->lock_object); 656154941Sjhb 657167801Sjhb#ifdef ADAPTIVE_RWLOCKS 658157846Sjhb /* 659157846Sjhb * There might not be a turnstile for this lock if all of 660157846Sjhb * the waiters are adaptively spinning. In that case, just 661157846Sjhb * reset the lock to the unlocked state and return. 662157846Sjhb */ 663157846Sjhb if (ts == NULL) { 664157846Sjhb atomic_store_rel_ptr(&rw->rw_lock, RW_UNLOCKED); 665167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 666157846Sjhb CTR2(KTR_LOCK, "%s: %p no sleepers", __func__, rw); 667170295Sjeff turnstile_chain_unlock(&rw->lock_object); 668157846Sjhb return; 669157846Sjhb } 670157846Sjhb#else 671154941Sjhb MPASS(ts != NULL); 672157846Sjhb#endif 673154941Sjhb 674154941Sjhb /* 675154941Sjhb * Use the same algo as sx locks for now. Prefer waking up shared 676154941Sjhb * waiters if we have any over writers. This is probably not ideal. 677154941Sjhb * 678154941Sjhb * 'v' is the value we are going to write back to rw_lock. If we 679154941Sjhb * have waiters on both queues, we need to preserve the state of 680154941Sjhb * the waiter flag for the queue we don't wake up. For now this is 681154941Sjhb * hardcoded for the algorithm mentioned above. 682154941Sjhb * 683154941Sjhb * In the case of both readers and writers waiting we wakeup the 684154941Sjhb * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a 685154941Sjhb * new writer comes in before a reader it will claim the lock up 686154941Sjhb * above. There is probably a potential priority inversion in 687154941Sjhb * there that could be worked around either by waking both queues 688154941Sjhb * of waiters or doing some complicated lock handoff gymnastics. 689157846Sjhb * 690167801Sjhb * Note that in the ADAPTIVE_RWLOCKS case, if both flags are 691167801Sjhb * set, there might not be any actual writers on the turnstile 692167801Sjhb * as they might all be spinning. In that case, we don't want 693167801Sjhb * to preserve the RW_LOCK_WRITE_WAITERS flag as the turnstile 694167801Sjhb * is going to go away once we wakeup all the readers. 695154941Sjhb */ 696157846Sjhb v = RW_UNLOCKED; 697154941Sjhb if (rw->rw_lock & RW_LOCK_READ_WAITERS) { 698154941Sjhb queue = TS_SHARED_QUEUE; 699167801Sjhb#ifdef ADAPTIVE_RWLOCKS 700157846Sjhb if (rw->rw_lock & RW_LOCK_WRITE_WAITERS && 701157846Sjhb !turnstile_empty(ts, TS_EXCLUSIVE_QUEUE)) 702157846Sjhb v |= RW_LOCK_WRITE_WAITERS; 703157846Sjhb#else 704157846Sjhb v |= (rw->rw_lock & RW_LOCK_WRITE_WAITERS); 705157846Sjhb#endif 706157846Sjhb } else 707154941Sjhb queue = TS_EXCLUSIVE_QUEUE; 708157846Sjhb 709167801Sjhb#ifdef ADAPTIVE_RWLOCKS 710157846Sjhb /* 711157846Sjhb * We have to make sure that we actually have waiters to 712157846Sjhb * wakeup. If they are all spinning, then we just need to 713157846Sjhb * disown the turnstile and return. 714157846Sjhb */ 715157846Sjhb if (turnstile_empty(ts, queue)) { 716167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 717157846Sjhb CTR2(KTR_LOCK, "%s: %p no sleepers 2", __func__, rw); 718157846Sjhb atomic_store_rel_ptr(&rw->rw_lock, v); 719157846Sjhb turnstile_disown(ts); 720170295Sjeff turnstile_chain_unlock(&rw->lock_object); 721157846Sjhb return; 722154941Sjhb } 723157846Sjhb#endif 724157846Sjhb 725157846Sjhb /* Wake up all waiters for the specific queue. */ 726167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 727154941Sjhb CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw, 728154941Sjhb queue == TS_SHARED_QUEUE ? "read" : "write"); 729154941Sjhb turnstile_broadcast(ts, queue); 730154941Sjhb atomic_store_rel_ptr(&rw->rw_lock, v); 731154941Sjhb turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 732170295Sjeff turnstile_chain_unlock(&rw->lock_object); 733154941Sjhb} 734154941Sjhb 735157882Sjhb/* 736157882Sjhb * Attempt to do a non-blocking upgrade from a read lock to a write 737157882Sjhb * lock. This will only succeed if this thread holds a single read 738157882Sjhb * lock. Returns true if the upgrade succeeded and false otherwise. 739157882Sjhb */ 740157882Sjhbint 741157882Sjhb_rw_try_upgrade(struct rwlock *rw, const char *file, int line) 742157882Sjhb{ 743157882Sjhb uintptr_t v, tid; 744170295Sjeff struct turnstile *ts; 745157882Sjhb int success; 746157882Sjhb 747169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 748169394Sjhb ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line)); 749157882Sjhb _rw_assert(rw, RA_RLOCKED, file, line); 750157882Sjhb 751157882Sjhb /* 752157882Sjhb * Attempt to switch from one reader to a writer. If there 753157882Sjhb * are any write waiters, then we will have to lock the 754157882Sjhb * turnstile first to prevent races with another writer 755157882Sjhb * calling turnstile_wait() before we have claimed this 756157882Sjhb * turnstile. So, do the simple case of no waiters first. 757157882Sjhb */ 758157882Sjhb tid = (uintptr_t)curthread; 759157882Sjhb if (!(rw->rw_lock & RW_LOCK_WRITE_WAITERS)) { 760168073Sjhb success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1), 761168073Sjhb tid); 762157882Sjhb goto out; 763157882Sjhb } 764157882Sjhb 765157882Sjhb /* 766157882Sjhb * Ok, we think we have write waiters, so lock the 767157882Sjhb * turnstile. 768157882Sjhb */ 769170295Sjeff ts = turnstile_trywait(&rw->lock_object); 770157882Sjhb 771157882Sjhb /* 772157882Sjhb * Try to switch from one reader to a writer again. This time 773157882Sjhb * we honor the current state of the RW_LOCK_WRITE_WAITERS 774157882Sjhb * flag. If we obtain the lock with the flag set, then claim 775167801Sjhb * ownership of the turnstile. In the ADAPTIVE_RWLOCKS case 776167801Sjhb * it is possible for there to not be an associated turnstile 777167801Sjhb * even though there are waiters if all of the waiters are 778167801Sjhb * spinning. 779157882Sjhb */ 780157882Sjhb v = rw->rw_lock & RW_LOCK_WRITE_WAITERS; 781168073Sjhb success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v, 782157882Sjhb tid | v); 783167801Sjhb#ifdef ADAPTIVE_RWLOCKS 784167787Sjhb if (success && v && turnstile_lookup(&rw->lock_object) != NULL) 785157882Sjhb#else 786157882Sjhb if (success && v) 787157882Sjhb#endif 788170295Sjeff turnstile_claim(ts); 789157882Sjhb else 790170295Sjeff turnstile_cancel(ts); 791157882Sjhbout: 792167787Sjhb LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line); 793157882Sjhb if (success) 794167787Sjhb WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 795157882Sjhb file, line); 796157882Sjhb return (success); 797157882Sjhb} 798157882Sjhb 799157882Sjhb/* 800157882Sjhb * Downgrade a write lock into a single read lock. 801157882Sjhb */ 802157882Sjhbvoid 803157882Sjhb_rw_downgrade(struct rwlock *rw, const char *file, int line) 804157882Sjhb{ 805157882Sjhb struct turnstile *ts; 806157882Sjhb uintptr_t tid, v; 807157882Sjhb 808169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 809169394Sjhb ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line)); 810171052Sattilio _rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line); 811171052Sattilio#ifndef INVARIANTS 812171052Sattilio if (rw_recursed(rw)) 813171052Sattilio panic("downgrade of a recursed lock"); 814171052Sattilio#endif 815157882Sjhb 816167787Sjhb WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line); 817157882Sjhb 818157882Sjhb /* 819157882Sjhb * Convert from a writer to a single reader. First we handle 820157882Sjhb * the easy case with no waiters. If there are any waiters, we 821157882Sjhb * lock the turnstile, "disown" the lock, and awaken any read 822157882Sjhb * waiters. 823157882Sjhb */ 824157882Sjhb tid = (uintptr_t)curthread; 825157882Sjhb if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1))) 826157882Sjhb goto out; 827157882Sjhb 828157882Sjhb /* 829157882Sjhb * Ok, we think we have waiters, so lock the turnstile so we can 830157882Sjhb * read the waiter flags without any races. 831157882Sjhb */ 832170295Sjeff turnstile_chain_lock(&rw->lock_object); 833157882Sjhb v = rw->rw_lock; 834157882Sjhb MPASS(v & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)); 835157882Sjhb 836157882Sjhb /* 837157882Sjhb * Downgrade from a write lock while preserving 838157882Sjhb * RW_LOCK_WRITE_WAITERS and give up ownership of the 839157882Sjhb * turnstile. If there are any read waiters, wake them up. 840157882Sjhb * 841167801Sjhb * For ADAPTIVE_RWLOCKS, we have to allow for the fact that 842167801Sjhb * all of the read waiters might be spinning. In that case, 843167801Sjhb * act as if RW_LOCK_READ_WAITERS is not set. Also, only 844167801Sjhb * preserve the RW_LOCK_WRITE_WAITERS flag if at least one 845167801Sjhb * writer is blocked on the turnstile. 846157882Sjhb */ 847167787Sjhb ts = turnstile_lookup(&rw->lock_object); 848167801Sjhb#ifdef ADAPTIVE_RWLOCKS 849157882Sjhb if (ts == NULL) 850157882Sjhb v &= ~(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS); 851157882Sjhb else if (v & RW_LOCK_READ_WAITERS && 852157882Sjhb turnstile_empty(ts, TS_SHARED_QUEUE)) 853157882Sjhb v &= ~RW_LOCK_READ_WAITERS; 854157882Sjhb else if (v & RW_LOCK_WRITE_WAITERS && 855157882Sjhb turnstile_empty(ts, TS_EXCLUSIVE_QUEUE)) 856157882Sjhb v &= ~RW_LOCK_WRITE_WAITERS; 857157882Sjhb#else 858157882Sjhb MPASS(ts != NULL); 859157882Sjhb#endif 860157882Sjhb if (v & RW_LOCK_READ_WAITERS) 861157882Sjhb turnstile_broadcast(ts, TS_SHARED_QUEUE); 862157882Sjhb atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | 863157882Sjhb (v & RW_LOCK_WRITE_WAITERS)); 864157882Sjhb if (v & RW_LOCK_READ_WAITERS) 865157882Sjhb turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 866170295Sjeff else if (ts) 867157882Sjhb turnstile_disown(ts); 868170295Sjeff turnstile_chain_unlock(&rw->lock_object); 869157882Sjhbout: 870167787Sjhb LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line); 871157882Sjhb} 872157882Sjhb 873154941Sjhb#ifdef INVARIANT_SUPPORT 874155162Sscottl#ifndef INVARIANTS 875154941Sjhb#undef _rw_assert 876154941Sjhb#endif 877154941Sjhb 878154941Sjhb/* 879154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least 880154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this* 881154941Sjhb * thread owns an rlock. 882154941Sjhb */ 883154941Sjhbvoid 884154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line) 885154941Sjhb{ 886154941Sjhb 887154941Sjhb if (panicstr != NULL) 888154941Sjhb return; 889154941Sjhb switch (what) { 890154941Sjhb case RA_LOCKED: 891171052Sattilio case RA_LOCKED | RA_RECURSED: 892171052Sattilio case RA_LOCKED | RA_NOTRECURSED: 893154941Sjhb case RA_RLOCKED: 894154941Sjhb#ifdef WITNESS 895167787Sjhb witness_assert(&rw->lock_object, what, file, line); 896154941Sjhb#else 897154941Sjhb /* 898154941Sjhb * If some other thread has a write lock or we have one 899154941Sjhb * and are asserting a read lock, fail. Also, if no one 900154941Sjhb * has a lock at all, fail. 901154941Sjhb */ 902155061Sscottl if (rw->rw_lock == RW_UNLOCKED || 903155061Sscottl (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED || 904157826Sjhb rw_wowner(rw) != curthread))) 905154941Sjhb panic("Lock %s not %slocked @ %s:%d\n", 906167787Sjhb rw->lock_object.lo_name, (what == RA_RLOCKED) ? 907154941Sjhb "read " : "", file, line); 908171052Sattilio 909171052Sattilio if (!(rw->rw_lock & RW_LOCK_READ)) { 910171052Sattilio if (rw_recursed(rw)) { 911171052Sattilio if (what & RA_NOTRECURSED) 912171052Sattilio panic("Lock %s recursed @ %s:%d\n", 913171052Sattilio rw->lock_object.lo_name, file, 914171052Sattilio line); 915171052Sattilio } else if (what & RA_RECURSED) 916171052Sattilio panic("Lock %s not recursed @ %s:%d\n", 917171052Sattilio rw->lock_object.lo_name, file, line); 918171052Sattilio } 919154941Sjhb#endif 920154941Sjhb break; 921154941Sjhb case RA_WLOCKED: 922171052Sattilio case RA_WLOCKED | RA_RECURSED: 923171052Sattilio case RA_WLOCKED | RA_NOTRECURSED: 924157826Sjhb if (rw_wowner(rw) != curthread) 925154941Sjhb panic("Lock %s not exclusively locked @ %s:%d\n", 926167787Sjhb rw->lock_object.lo_name, file, line); 927171052Sattilio if (rw_recursed(rw)) { 928171052Sattilio if (what & RA_NOTRECURSED) 929171052Sattilio panic("Lock %s recursed @ %s:%d\n", 930171052Sattilio rw->lock_object.lo_name, file, line); 931171052Sattilio } else if (what & RA_RECURSED) 932171052Sattilio panic("Lock %s not recursed @ %s:%d\n", 933171052Sattilio rw->lock_object.lo_name, file, line); 934154941Sjhb break; 935154941Sjhb case RA_UNLOCKED: 936154941Sjhb#ifdef WITNESS 937167787Sjhb witness_assert(&rw->lock_object, what, file, line); 938154941Sjhb#else 939154941Sjhb /* 940154941Sjhb * If we hold a write lock fail. We can't reliably check 941154941Sjhb * to see if we hold a read lock or not. 942154941Sjhb */ 943157826Sjhb if (rw_wowner(rw) == curthread) 944154941Sjhb panic("Lock %s exclusively locked @ %s:%d\n", 945167787Sjhb rw->lock_object.lo_name, file, line); 946154941Sjhb#endif 947154941Sjhb break; 948154941Sjhb default: 949154941Sjhb panic("Unknown rw lock assertion: %d @ %s:%d", what, file, 950154941Sjhb line); 951154941Sjhb } 952154941Sjhb} 953154941Sjhb#endif /* INVARIANT_SUPPORT */ 954154941Sjhb 955154941Sjhb#ifdef DDB 956154941Sjhbvoid 957154941Sjhbdb_show_rwlock(struct lock_object *lock) 958154941Sjhb{ 959154941Sjhb struct rwlock *rw; 960154941Sjhb struct thread *td; 961154941Sjhb 962154941Sjhb rw = (struct rwlock *)lock; 963154941Sjhb 964154941Sjhb db_printf(" state: "); 965154941Sjhb if (rw->rw_lock == RW_UNLOCKED) 966154941Sjhb db_printf("UNLOCKED\n"); 967169394Sjhb else if (rw->rw_lock == RW_DESTROYED) { 968169394Sjhb db_printf("DESTROYED\n"); 969169394Sjhb return; 970169394Sjhb } else if (rw->rw_lock & RW_LOCK_READ) 971167504Sjhb db_printf("RLOCK: %ju locks\n", 972167504Sjhb (uintmax_t)(RW_READERS(rw->rw_lock))); 973154941Sjhb else { 974157826Sjhb td = rw_wowner(rw); 975154941Sjhb db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 976173600Sjulian td->td_tid, td->td_proc->p_pid, td->td_name); 977171052Sattilio if (rw_recursed(rw)) 978171052Sattilio db_printf(" recursed: %u\n", rw->rw_recurse); 979154941Sjhb } 980154941Sjhb db_printf(" waiters: "); 981154941Sjhb switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) { 982154941Sjhb case RW_LOCK_READ_WAITERS: 983154941Sjhb db_printf("readers\n"); 984154941Sjhb break; 985154941Sjhb case RW_LOCK_WRITE_WAITERS: 986154941Sjhb db_printf("writers\n"); 987154941Sjhb break; 988154941Sjhb case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS: 989167492Sjhb db_printf("readers and writers\n"); 990154941Sjhb break; 991154941Sjhb default: 992154941Sjhb db_printf("none\n"); 993154941Sjhb break; 994154941Sjhb } 995154941Sjhb} 996154941Sjhb 997154941Sjhb#endif 998