kern_rwlock.c revision 171052
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 171052 2007-06-26 21:31:56Z 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> 48164159Skmacy#include <sys/lock_profile.h> 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 62167368Sjhbstatic void lock_rw(struct lock_object *lock, int how); 63167368Sjhbstatic int unlock_rw(struct lock_object *lock); 64154941Sjhb 65154941Sjhbstruct lock_class lock_class_rw = { 66167365Sjhb .lc_name = "rw", 67167365Sjhb .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE, 68154941Sjhb#ifdef DDB 69167365Sjhb .lc_ddb_show = db_show_rwlock, 70154941Sjhb#endif 71167368Sjhb .lc_lock = lock_rw, 72167368Sjhb .lc_unlock = unlock_rw, 73154941Sjhb}; 74154941Sjhb 75157826Sjhb/* 76157826Sjhb * Return a pointer to the owning thread if the lock is write-locked or 77157826Sjhb * NULL if the lock is unlocked or read-locked. 78157826Sjhb */ 79157826Sjhb#define rw_wowner(rw) \ 80154941Sjhb ((rw)->rw_lock & RW_LOCK_READ ? NULL : \ 81154941Sjhb (struct thread *)RW_OWNER((rw)->rw_lock)) 82154941Sjhb 83157826Sjhb/* 84171052Sattilio * Returns if a write owner is recursed. Write ownership is not assured 85171052Sattilio * here and should be previously checked. 86171052Sattilio */ 87171052Sattilio#define rw_recursed(rw) ((rw)->rw_recurse != 0) 88171052Sattilio 89171052Sattilio/* 90171052Sattilio * Return true if curthread helds the lock. 91171052Sattilio */ 92171052Sattilio#define rw_wlocked(rw) (rw_wowner((rw)) == curthread) 93171052Sattilio 94171052Sattilio/* 95157826Sjhb * Return a pointer to the owning thread for this lock who should receive 96157826Sjhb * any priority lent by threads that block on this lock. Currently this 97157826Sjhb * is identical to rw_wowner(). 98157826Sjhb */ 99157826Sjhb#define rw_owner(rw) rw_wowner(rw) 100157826Sjhb 101154941Sjhb#ifndef INVARIANTS 102154941Sjhb#define _rw_assert(rw, what, file, line) 103154941Sjhb#endif 104154941Sjhb 105154941Sjhbvoid 106167368Sjhblock_rw(struct lock_object *lock, int how) 107167368Sjhb{ 108167368Sjhb struct rwlock *rw; 109167368Sjhb 110167368Sjhb rw = (struct rwlock *)lock; 111167368Sjhb if (how) 112167368Sjhb rw_wlock(rw); 113167368Sjhb else 114167368Sjhb rw_rlock(rw); 115167368Sjhb} 116167368Sjhb 117167368Sjhbint 118167368Sjhbunlock_rw(struct lock_object *lock) 119167368Sjhb{ 120167368Sjhb struct rwlock *rw; 121167368Sjhb 122167368Sjhb rw = (struct rwlock *)lock; 123167368Sjhb rw_assert(rw, RA_LOCKED | LA_NOTRECURSED); 124167368Sjhb if (rw->rw_lock & RW_LOCK_READ) { 125167368Sjhb rw_runlock(rw); 126167368Sjhb return (0); 127167368Sjhb } else { 128167368Sjhb rw_wunlock(rw); 129167368Sjhb return (1); 130167368Sjhb } 131167368Sjhb} 132167368Sjhb 133167368Sjhbvoid 134171052Sattiliorw_init_flags(struct rwlock *rw, const char *name, int opts) 135154941Sjhb{ 136171052Sattilio int flags; 137154941Sjhb 138171052Sattilio MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET | 139171052Sattilio RW_RECURSE)) == 0); 140171052Sattilio 141171052Sattilio flags = LO_UPGRADABLE | LO_RECURSABLE; 142171052Sattilio if (opts & RW_DUPOK) 143171052Sattilio flags |= LO_DUPOK; 144171052Sattilio if (opts & RW_NOPROFILE) 145171052Sattilio flags |= LO_NOPROFILE; 146171052Sattilio if (!(opts & RW_NOWITNESS)) 147171052Sattilio flags |= LO_WITNESS; 148171052Sattilio if (opts & RW_QUIET) 149171052Sattilio flags |= LO_QUIET; 150171052Sattilio flags |= opts & RW_RECURSE; 151171052Sattilio 152154941Sjhb rw->rw_lock = RW_UNLOCKED; 153171052Sattilio rw->rw_recurse = 0; 154171052Sattilio lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags); 155154941Sjhb} 156154941Sjhb 157154941Sjhbvoid 158154941Sjhbrw_destroy(struct rwlock *rw) 159154941Sjhb{ 160154941Sjhb 161154941Sjhb KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked")); 162171052Sattilio KASSERT(rw->rw_recurse == 0, ("rw lock still recursed")); 163169394Sjhb rw->rw_lock = RW_DESTROYED; 164167787Sjhb lock_destroy(&rw->lock_object); 165154941Sjhb} 166154941Sjhb 167154941Sjhbvoid 168154941Sjhbrw_sysinit(void *arg) 169154941Sjhb{ 170154941Sjhb struct rw_args *args = arg; 171154941Sjhb 172154941Sjhb rw_init(args->ra_rw, args->ra_desc); 173154941Sjhb} 174154941Sjhb 175167024Srwatsonint 176167024Srwatsonrw_wowned(struct rwlock *rw) 177167024Srwatson{ 178167024Srwatson 179167024Srwatson return (rw_wowner(rw) == curthread); 180167024Srwatson} 181167024Srwatson 182154941Sjhbvoid 183154941Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line) 184154941Sjhb{ 185154941Sjhb 186154941Sjhb MPASS(curthread != NULL); 187169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 188169394Sjhb ("rw_wlock() of destroyed rwlock @ %s:%d", file, line)); 189157826Sjhb KASSERT(rw_wowner(rw) != curthread, 190154941Sjhb ("%s (%s): wlock already held @ %s:%d", __func__, 191167787Sjhb rw->lock_object.lo_name, file, line)); 192167787Sjhb WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file, 193154941Sjhb line); 194154941Sjhb __rw_wlock(rw, curthread, file, line); 195171052Sattilio LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line); 196167787Sjhb WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line); 197160771Sjhb curthread->td_locks++; 198154941Sjhb} 199154941Sjhb 200154941Sjhbvoid 201154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line) 202154941Sjhb{ 203154941Sjhb 204154941Sjhb MPASS(curthread != NULL); 205169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 206169394Sjhb ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line)); 207154941Sjhb _rw_assert(rw, RA_WLOCKED, file, line); 208160771Sjhb curthread->td_locks--; 209167787Sjhb WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line); 210171052Sattilio LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file, 211171052Sattilio line); 212171052Sattilio if (!rw_recursed(rw)) 213171052Sattilio lock_profile_release_lock(&rw->lock_object); 214154941Sjhb __rw_wunlock(rw, curthread, file, line); 215154941Sjhb} 216154941Sjhb 217154941Sjhbvoid 218154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line) 219154941Sjhb{ 220170295Sjeff struct turnstile *ts; 221167801Sjhb#ifdef ADAPTIVE_RWLOCKS 222157846Sjhb volatile struct thread *owner; 223157851Swkoszek#endif 224167307Sjhb uint64_t waittime = 0; 225167054Skmacy int contested = 0; 226154941Sjhb uintptr_t x; 227154941Sjhb 228169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 229169394Sjhb ("rw_rlock() of destroyed rwlock @ %s:%d", file, line)); 230157826Sjhb KASSERT(rw_wowner(rw) != curthread, 231154941Sjhb ("%s (%s): wlock already held @ %s:%d", __func__, 232167787Sjhb rw->lock_object.lo_name, file, line)); 233167787Sjhb WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line); 234154941Sjhb 235154941Sjhb /* 236154941Sjhb * Note that we don't make any attempt to try to block read 237154941Sjhb * locks once a writer has blocked on the lock. The reason is 238154941Sjhb * that we currently allow for read locks to recurse and we 239154941Sjhb * don't keep track of all the holders of read locks. Thus, if 240154941Sjhb * we were to block readers once a writer blocked and a reader 241154941Sjhb * tried to recurse on their reader lock after a writer had 242154941Sjhb * blocked we would end up in a deadlock since the reader would 243154941Sjhb * be blocked on the writer, and the writer would be blocked 244154941Sjhb * waiting for the reader to release its original read lock. 245154941Sjhb */ 246154941Sjhb for (;;) { 247154941Sjhb /* 248154941Sjhb * Handle the easy case. If no other thread has a write 249154941Sjhb * lock, then try to bump up the count of read locks. Note 250154941Sjhb * that we have to preserve the current state of the 251154941Sjhb * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a 252154941Sjhb * read lock, then rw_lock must have changed, so restart 253154941Sjhb * the loop. Note that this handles the case of a 254154941Sjhb * completely unlocked rwlock since such a lock is encoded 255154941Sjhb * as a read lock with no waiters. 256154941Sjhb */ 257154941Sjhb x = rw->rw_lock; 258154941Sjhb if (x & RW_LOCK_READ) { 259154941Sjhb 260154941Sjhb /* 261154941Sjhb * The RW_LOCK_READ_WAITERS flag should only be set 262154941Sjhb * if another thread currently holds a write lock, 263154941Sjhb * and in that case RW_LOCK_READ should be clear. 264154941Sjhb */ 265154941Sjhb MPASS((x & RW_LOCK_READ_WAITERS) == 0); 266154941Sjhb if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, 267154941Sjhb x + RW_ONE_READER)) { 268167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 269154941Sjhb CTR4(KTR_LOCK, 270154941Sjhb "%s: %p succeed %p -> %p", __func__, 271154941Sjhb rw, (void *)x, 272154941Sjhb (void *)(x + RW_ONE_READER)); 273167307Sjhb if (RW_READERS(x) == 0) 274167307Sjhb lock_profile_obtain_lock_success( 275167787Sjhb &rw->lock_object, contested, waittime, 276167307Sjhb file, line); 277154941Sjhb break; 278154941Sjhb } 279157846Sjhb cpu_spinwait(); 280154941Sjhb continue; 281154941Sjhb } 282167787Sjhb lock_profile_obtain_lock_failed(&rw->lock_object, &contested, 283167307Sjhb &waittime); 284154941Sjhb 285154941Sjhb /* 286154941Sjhb * Okay, now it's the hard case. Some other thread already 287154941Sjhb * has a write lock, so acquire the turnstile lock so we can 288154941Sjhb * begin the process of blocking. 289154941Sjhb */ 290170295Sjeff ts = turnstile_trywait(&rw->lock_object); 291154941Sjhb 292154941Sjhb /* 293154941Sjhb * The lock might have been released while we spun, so 294154941Sjhb * recheck its state and restart the loop if there is no 295154941Sjhb * longer a write lock. 296154941Sjhb */ 297154941Sjhb x = rw->rw_lock; 298154941Sjhb if (x & RW_LOCK_READ) { 299170295Sjeff turnstile_cancel(ts); 300157846Sjhb cpu_spinwait(); 301154941Sjhb continue; 302154941Sjhb } 303154941Sjhb 304154941Sjhb /* 305154941Sjhb * Ok, it's still a write lock. If the RW_LOCK_READ_WAITERS 306154941Sjhb * flag is already set, then we can go ahead and block. If 307154941Sjhb * it is not set then try to set it. If we fail to set it 308154941Sjhb * drop the turnstile lock and restart the loop. 309154941Sjhb */ 310157826Sjhb if (!(x & RW_LOCK_READ_WAITERS)) { 311157826Sjhb if (!atomic_cmpset_ptr(&rw->rw_lock, x, 312157826Sjhb x | RW_LOCK_READ_WAITERS)) { 313170295Sjeff turnstile_cancel(ts); 314157826Sjhb cpu_spinwait(); 315157826Sjhb continue; 316157826Sjhb } 317167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 318157826Sjhb CTR2(KTR_LOCK, "%s: %p set read waiters flag", 319157826Sjhb __func__, rw); 320154941Sjhb } 321154941Sjhb 322167801Sjhb#ifdef ADAPTIVE_RWLOCKS 323154941Sjhb /* 324157846Sjhb * If the owner is running on another CPU, spin until 325157846Sjhb * the owner stops running or the state of the lock 326157846Sjhb * changes. 327157846Sjhb */ 328157846Sjhb owner = (struct thread *)RW_OWNER(x); 329157846Sjhb if (TD_IS_RUNNING(owner)) { 330170295Sjeff turnstile_cancel(ts); 331167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 332157846Sjhb CTR3(KTR_LOCK, "%s: spinning on %p held by %p", 333157846Sjhb __func__, rw, owner); 334157846Sjhb while ((struct thread*)RW_OWNER(rw->rw_lock)== owner && 335157846Sjhb TD_IS_RUNNING(owner)) 336157846Sjhb cpu_spinwait(); 337157846Sjhb continue; 338157846Sjhb } 339157846Sjhb#endif 340157846Sjhb 341157846Sjhb /* 342154941Sjhb * We were unable to acquire the lock and the read waiters 343154941Sjhb * flag is set, so we must block on the turnstile. 344154941Sjhb */ 345167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 346154941Sjhb CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 347154941Sjhb rw); 348170295Sjeff turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE); 349167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 350154941Sjhb CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 351154941Sjhb __func__, rw); 352154941Sjhb } 353154941Sjhb 354154941Sjhb /* 355154941Sjhb * TODO: acquire "owner of record" here. Here be turnstile dragons 356154941Sjhb * however. turnstiles don't like owners changing between calls to 357154941Sjhb * turnstile_wait() currently. 358154941Sjhb */ 359154941Sjhb 360167787Sjhb LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line); 361167787Sjhb WITNESS_LOCK(&rw->lock_object, 0, file, line); 362160771Sjhb curthread->td_locks++; 363154941Sjhb} 364154941Sjhb 365154941Sjhbvoid 366154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line) 367154941Sjhb{ 368154941Sjhb struct turnstile *ts; 369154941Sjhb uintptr_t x; 370154941Sjhb 371169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 372169394Sjhb ("rw_runlock() of destroyed rwlock @ %s:%d", file, line)); 373154941Sjhb _rw_assert(rw, RA_RLOCKED, file, line); 374160771Sjhb curthread->td_locks--; 375167787Sjhb WITNESS_UNLOCK(&rw->lock_object, 0, file, line); 376167787Sjhb LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line); 377154941Sjhb 378154941Sjhb /* TODO: drop "owner of record" here. */ 379154941Sjhb 380154941Sjhb for (;;) { 381154941Sjhb /* 382154941Sjhb * See if there is more than one read lock held. If so, 383154941Sjhb * just drop one and return. 384154941Sjhb */ 385154941Sjhb x = rw->rw_lock; 386154941Sjhb if (RW_READERS(x) > 1) { 387154941Sjhb if (atomic_cmpset_ptr(&rw->rw_lock, x, 388154941Sjhb x - RW_ONE_READER)) { 389167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 390154941Sjhb CTR4(KTR_LOCK, 391154941Sjhb "%s: %p succeeded %p -> %p", 392154941Sjhb __func__, rw, (void *)x, 393154941Sjhb (void *)(x - RW_ONE_READER)); 394154941Sjhb break; 395154941Sjhb } 396154941Sjhb continue; 397167307Sjhb } 398154941Sjhb 399164159Skmacy 400154941Sjhb /* 401154941Sjhb * We should never have read waiters while at least one 402154941Sjhb * thread holds a read lock. (See note above) 403154941Sjhb */ 404154941Sjhb KASSERT(!(x & RW_LOCK_READ_WAITERS), 405154941Sjhb ("%s: waiting readers", __func__)); 406154941Sjhb 407154941Sjhb /* 408154941Sjhb * If there aren't any waiters for a write lock, then try 409154941Sjhb * to drop it quickly. 410154941Sjhb */ 411154941Sjhb if (!(x & RW_LOCK_WRITE_WAITERS)) { 412154941Sjhb 413154941Sjhb /* 414154941Sjhb * There shouldn't be any flags set and we should 415154941Sjhb * be the only read lock. If we fail to release 416154941Sjhb * the single read lock, then another thread might 417154941Sjhb * have just acquired a read lock, so go back up 418154941Sjhb * to the multiple read locks case. 419154941Sjhb */ 420154941Sjhb MPASS(x == RW_READERS_LOCK(1)); 421154941Sjhb if (atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1), 422154941Sjhb RW_UNLOCKED)) { 423167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 424154941Sjhb CTR2(KTR_LOCK, "%s: %p last succeeded", 425154941Sjhb __func__, rw); 426154941Sjhb break; 427154941Sjhb } 428154941Sjhb continue; 429154941Sjhb } 430154941Sjhb 431154941Sjhb /* 432154941Sjhb * There should just be one reader with one or more 433154941Sjhb * writers waiting. 434154941Sjhb */ 435154941Sjhb MPASS(x == (RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS)); 436154941Sjhb 437154941Sjhb /* 438154941Sjhb * Ok, we know we have a waiting writer and we think we 439154941Sjhb * are the last reader, so grab the turnstile lock. 440154941Sjhb */ 441170295Sjeff turnstile_chain_lock(&rw->lock_object); 442154941Sjhb 443154941Sjhb /* 444154941Sjhb * Try to drop our lock leaving the lock in a unlocked 445154941Sjhb * state. 446154941Sjhb * 447154941Sjhb * If you wanted to do explicit lock handoff you'd have to 448154941Sjhb * do it here. You'd also want to use turnstile_signal() 449154941Sjhb * and you'd have to handle the race where a higher 450154941Sjhb * priority thread blocks on the write lock before the 451154941Sjhb * thread you wakeup actually runs and have the new thread 452154941Sjhb * "steal" the lock. For now it's a lot simpler to just 453154941Sjhb * wakeup all of the waiters. 454154941Sjhb * 455154941Sjhb * As above, if we fail, then another thread might have 456154941Sjhb * acquired a read lock, so drop the turnstile lock and 457154941Sjhb * restart. 458154941Sjhb */ 459154941Sjhb if (!atomic_cmpset_ptr(&rw->rw_lock, 460154941Sjhb RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS, RW_UNLOCKED)) { 461170295Sjeff turnstile_chain_unlock(&rw->lock_object); 462154941Sjhb continue; 463154941Sjhb } 464167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 465154941Sjhb CTR2(KTR_LOCK, "%s: %p last succeeded with waiters", 466154941Sjhb __func__, rw); 467154941Sjhb 468154941Sjhb /* 469154941Sjhb * Ok. The lock is released and all that's left is to 470154941Sjhb * wake up the waiters. Note that the lock might not be 471154941Sjhb * free anymore, but in that case the writers will just 472154941Sjhb * block again if they run before the new lock holder(s) 473154941Sjhb * release the lock. 474154941Sjhb */ 475167787Sjhb ts = turnstile_lookup(&rw->lock_object); 476157846Sjhb MPASS(ts != NULL); 477154941Sjhb turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE); 478154941Sjhb turnstile_unpend(ts, TS_SHARED_LOCK); 479170295Sjeff turnstile_chain_unlock(&rw->lock_object); 480154941Sjhb break; 481154941Sjhb } 482167787Sjhb lock_profile_release_lock(&rw->lock_object); 483154941Sjhb} 484154941Sjhb 485154941Sjhb/* 486154941Sjhb * This function is called when we are unable to obtain a write lock on the 487154941Sjhb * first try. This means that at least one other thread holds either a 488154941Sjhb * read or write lock. 489154941Sjhb */ 490154941Sjhbvoid 491154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line) 492154941Sjhb{ 493170295Sjeff struct turnstile *ts; 494167801Sjhb#ifdef ADAPTIVE_RWLOCKS 495157846Sjhb volatile struct thread *owner; 496157851Swkoszek#endif 497154941Sjhb uintptr_t v; 498154941Sjhb 499171052Sattilio if (rw_wlocked(rw)) { 500171052Sattilio KASSERT(rw->lock_object.lo_flags & RW_RECURSE, 501171052Sattilio ("%s: recursing but non-recursive rw %s @ %s:%d\n", 502171052Sattilio __func__, rw->lock_object.lo_name, file, line)); 503171052Sattilio rw->rw_recurse++; 504171052Sattilio atomic_set_ptr(&rw->rw_lock, RW_LOCK_RECURSED); 505171052Sattilio if (LOCK_LOG_TEST(&rw->lock_object, 0)) 506171052Sattilio CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw); 507171052Sattilio return; 508171052Sattilio } 509171052Sattilio 510167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 511154941Sjhb CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__, 512167787Sjhb rw->lock_object.lo_name, (void *)rw->rw_lock, file, line); 513154941Sjhb 514154941Sjhb while (!_rw_write_lock(rw, tid)) { 515170295Sjeff ts = turnstile_trywait(&rw->lock_object); 516154941Sjhb v = rw->rw_lock; 517154941Sjhb 518154941Sjhb /* 519154941Sjhb * If the lock was released while spinning on the 520154941Sjhb * turnstile chain lock, try again. 521154941Sjhb */ 522154941Sjhb if (v == RW_UNLOCKED) { 523170295Sjeff turnstile_cancel(ts); 524154941Sjhb cpu_spinwait(); 525154941Sjhb continue; 526154941Sjhb } 527154941Sjhb 528154941Sjhb /* 529154941Sjhb * If the lock was released by a writer with both readers 530154941Sjhb * and writers waiting and a reader hasn't woken up and 531154941Sjhb * acquired the lock yet, rw_lock will be set to the 532154941Sjhb * value RW_UNLOCKED | RW_LOCK_WRITE_WAITERS. If we see 533154941Sjhb * that value, try to acquire it once. Note that we have 534154941Sjhb * to preserve the RW_LOCK_WRITE_WAITERS flag as there are 535168073Sjhb * other writers waiting still. If we fail, restart the 536154941Sjhb * loop. 537154941Sjhb */ 538154941Sjhb if (v == (RW_UNLOCKED | RW_LOCK_WRITE_WAITERS)) { 539154941Sjhb if (atomic_cmpset_acq_ptr(&rw->rw_lock, 540154941Sjhb RW_UNLOCKED | RW_LOCK_WRITE_WAITERS, 541154941Sjhb tid | RW_LOCK_WRITE_WAITERS)) { 542170295Sjeff turnstile_claim(ts); 543154941Sjhb CTR2(KTR_LOCK, "%s: %p claimed by new writer", 544154941Sjhb __func__, rw); 545154941Sjhb break; 546154941Sjhb } 547170295Sjeff turnstile_cancel(ts); 548154941Sjhb cpu_spinwait(); 549154941Sjhb continue; 550154941Sjhb } 551154941Sjhb 552154941Sjhb /* 553154941Sjhb * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to 554154941Sjhb * set it. If we fail to set it, then loop back and try 555154941Sjhb * again. 556154941Sjhb */ 557157826Sjhb if (!(v & RW_LOCK_WRITE_WAITERS)) { 558157826Sjhb if (!atomic_cmpset_ptr(&rw->rw_lock, v, 559157826Sjhb v | RW_LOCK_WRITE_WAITERS)) { 560170295Sjeff turnstile_cancel(ts); 561157826Sjhb cpu_spinwait(); 562157826Sjhb continue; 563157826Sjhb } 564167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 565157826Sjhb CTR2(KTR_LOCK, "%s: %p set write waiters flag", 566157826Sjhb __func__, rw); 567154941Sjhb } 568154941Sjhb 569167801Sjhb#ifdef ADAPTIVE_RWLOCKS 570157846Sjhb /* 571157846Sjhb * If the lock is write locked and the owner is 572157846Sjhb * running on another CPU, spin until the owner stops 573157846Sjhb * running or the state of the lock changes. 574157846Sjhb */ 575157846Sjhb owner = (struct thread *)RW_OWNER(v); 576157846Sjhb if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) { 577170295Sjeff turnstile_cancel(ts); 578167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 579157846Sjhb CTR3(KTR_LOCK, "%s: spinning on %p held by %p", 580157846Sjhb __func__, rw, owner); 581157846Sjhb while ((struct thread*)RW_OWNER(rw->rw_lock)== owner && 582157846Sjhb TD_IS_RUNNING(owner)) 583157846Sjhb cpu_spinwait(); 584157846Sjhb continue; 585157846Sjhb } 586157846Sjhb#endif 587154941Sjhb 588154941Sjhb /* 589154941Sjhb * We were unable to acquire the lock and the write waiters 590154941Sjhb * flag is set, so we must block on the turnstile. 591154941Sjhb */ 592167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 593154941Sjhb CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__, 594154941Sjhb rw); 595170295Sjeff turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE); 596167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 597154941Sjhb CTR2(KTR_LOCK, "%s: %p resuming from turnstile", 598154941Sjhb __func__, rw); 599154941Sjhb } 600154941Sjhb} 601154941Sjhb 602154941Sjhb/* 603154941Sjhb * This function is called if the first try at releasing a write lock failed. 604154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at 605154941Sjhb * least one thread is waiting on this lock. 606154941Sjhb */ 607154941Sjhbvoid 608154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line) 609154941Sjhb{ 610154941Sjhb struct turnstile *ts; 611154941Sjhb uintptr_t v; 612154941Sjhb int queue; 613154941Sjhb 614171052Sattilio if (rw_wlocked(rw) && rw_recursed(rw)) { 615171052Sattilio if ((--rw->rw_recurse) == 0) 616171052Sattilio atomic_clear_ptr(&rw->rw_lock, RW_LOCK_RECURSED); 617171052Sattilio if (LOCK_LOG_TEST(&rw->lock_object, 0)) 618171052Sattilio CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw); 619171052Sattilio return; 620171052Sattilio } 621171052Sattilio 622154941Sjhb KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS), 623154941Sjhb ("%s: neither of the waiter flags are set", __func__)); 624154941Sjhb 625167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 626154941Sjhb CTR2(KTR_LOCK, "%s: %p contested", __func__, rw); 627154941Sjhb 628170295Sjeff turnstile_chain_lock(&rw->lock_object); 629167787Sjhb ts = turnstile_lookup(&rw->lock_object); 630154941Sjhb 631167801Sjhb#ifdef ADAPTIVE_RWLOCKS 632157846Sjhb /* 633157846Sjhb * There might not be a turnstile for this lock if all of 634157846Sjhb * the waiters are adaptively spinning. In that case, just 635157846Sjhb * reset the lock to the unlocked state and return. 636157846Sjhb */ 637157846Sjhb if (ts == NULL) { 638157846Sjhb atomic_store_rel_ptr(&rw->rw_lock, RW_UNLOCKED); 639167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 640157846Sjhb CTR2(KTR_LOCK, "%s: %p no sleepers", __func__, rw); 641170295Sjeff turnstile_chain_unlock(&rw->lock_object); 642157846Sjhb return; 643157846Sjhb } 644157846Sjhb#else 645154941Sjhb MPASS(ts != NULL); 646157846Sjhb#endif 647154941Sjhb 648154941Sjhb /* 649154941Sjhb * Use the same algo as sx locks for now. Prefer waking up shared 650154941Sjhb * waiters if we have any over writers. This is probably not ideal. 651154941Sjhb * 652154941Sjhb * 'v' is the value we are going to write back to rw_lock. If we 653154941Sjhb * have waiters on both queues, we need to preserve the state of 654154941Sjhb * the waiter flag for the queue we don't wake up. For now this is 655154941Sjhb * hardcoded for the algorithm mentioned above. 656154941Sjhb * 657154941Sjhb * In the case of both readers and writers waiting we wakeup the 658154941Sjhb * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a 659154941Sjhb * new writer comes in before a reader it will claim the lock up 660154941Sjhb * above. There is probably a potential priority inversion in 661154941Sjhb * there that could be worked around either by waking both queues 662154941Sjhb * of waiters or doing some complicated lock handoff gymnastics. 663157846Sjhb * 664167801Sjhb * Note that in the ADAPTIVE_RWLOCKS case, if both flags are 665167801Sjhb * set, there might not be any actual writers on the turnstile 666167801Sjhb * as they might all be spinning. In that case, we don't want 667167801Sjhb * to preserve the RW_LOCK_WRITE_WAITERS flag as the turnstile 668167801Sjhb * is going to go away once we wakeup all the readers. 669154941Sjhb */ 670157846Sjhb v = RW_UNLOCKED; 671154941Sjhb if (rw->rw_lock & RW_LOCK_READ_WAITERS) { 672154941Sjhb queue = TS_SHARED_QUEUE; 673167801Sjhb#ifdef ADAPTIVE_RWLOCKS 674157846Sjhb if (rw->rw_lock & RW_LOCK_WRITE_WAITERS && 675157846Sjhb !turnstile_empty(ts, TS_EXCLUSIVE_QUEUE)) 676157846Sjhb v |= RW_LOCK_WRITE_WAITERS; 677157846Sjhb#else 678157846Sjhb v |= (rw->rw_lock & RW_LOCK_WRITE_WAITERS); 679157846Sjhb#endif 680157846Sjhb } else 681154941Sjhb queue = TS_EXCLUSIVE_QUEUE; 682157846Sjhb 683167801Sjhb#ifdef ADAPTIVE_RWLOCKS 684157846Sjhb /* 685157846Sjhb * We have to make sure that we actually have waiters to 686157846Sjhb * wakeup. If they are all spinning, then we just need to 687157846Sjhb * disown the turnstile and return. 688157846Sjhb */ 689157846Sjhb if (turnstile_empty(ts, queue)) { 690167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 691157846Sjhb CTR2(KTR_LOCK, "%s: %p no sleepers 2", __func__, rw); 692157846Sjhb atomic_store_rel_ptr(&rw->rw_lock, v); 693157846Sjhb turnstile_disown(ts); 694170295Sjeff turnstile_chain_unlock(&rw->lock_object); 695157846Sjhb return; 696154941Sjhb } 697157846Sjhb#endif 698157846Sjhb 699157846Sjhb /* Wake up all waiters for the specific queue. */ 700167787Sjhb if (LOCK_LOG_TEST(&rw->lock_object, 0)) 701154941Sjhb CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw, 702154941Sjhb queue == TS_SHARED_QUEUE ? "read" : "write"); 703154941Sjhb turnstile_broadcast(ts, queue); 704154941Sjhb atomic_store_rel_ptr(&rw->rw_lock, v); 705154941Sjhb turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 706170295Sjeff turnstile_chain_unlock(&rw->lock_object); 707154941Sjhb} 708154941Sjhb 709157882Sjhb/* 710157882Sjhb * Attempt to do a non-blocking upgrade from a read lock to a write 711157882Sjhb * lock. This will only succeed if this thread holds a single read 712157882Sjhb * lock. Returns true if the upgrade succeeded and false otherwise. 713157882Sjhb */ 714157882Sjhbint 715157882Sjhb_rw_try_upgrade(struct rwlock *rw, const char *file, int line) 716157882Sjhb{ 717157882Sjhb uintptr_t v, tid; 718170295Sjeff struct turnstile *ts; 719157882Sjhb int success; 720157882Sjhb 721169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 722169394Sjhb ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line)); 723157882Sjhb _rw_assert(rw, RA_RLOCKED, file, line); 724157882Sjhb 725157882Sjhb /* 726157882Sjhb * Attempt to switch from one reader to a writer. If there 727157882Sjhb * are any write waiters, then we will have to lock the 728157882Sjhb * turnstile first to prevent races with another writer 729157882Sjhb * calling turnstile_wait() before we have claimed this 730157882Sjhb * turnstile. So, do the simple case of no waiters first. 731157882Sjhb */ 732157882Sjhb tid = (uintptr_t)curthread; 733157882Sjhb if (!(rw->rw_lock & RW_LOCK_WRITE_WAITERS)) { 734168073Sjhb success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1), 735168073Sjhb tid); 736157882Sjhb goto out; 737157882Sjhb } 738157882Sjhb 739157882Sjhb /* 740157882Sjhb * Ok, we think we have write waiters, so lock the 741157882Sjhb * turnstile. 742157882Sjhb */ 743170295Sjeff ts = turnstile_trywait(&rw->lock_object); 744157882Sjhb 745157882Sjhb /* 746157882Sjhb * Try to switch from one reader to a writer again. This time 747157882Sjhb * we honor the current state of the RW_LOCK_WRITE_WAITERS 748157882Sjhb * flag. If we obtain the lock with the flag set, then claim 749167801Sjhb * ownership of the turnstile. In the ADAPTIVE_RWLOCKS case 750167801Sjhb * it is possible for there to not be an associated turnstile 751167801Sjhb * even though there are waiters if all of the waiters are 752167801Sjhb * spinning. 753157882Sjhb */ 754157882Sjhb v = rw->rw_lock & RW_LOCK_WRITE_WAITERS; 755168073Sjhb success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v, 756157882Sjhb tid | v); 757167801Sjhb#ifdef ADAPTIVE_RWLOCKS 758167787Sjhb if (success && v && turnstile_lookup(&rw->lock_object) != NULL) 759157882Sjhb#else 760157882Sjhb if (success && v) 761157882Sjhb#endif 762170295Sjeff turnstile_claim(ts); 763157882Sjhb else 764170295Sjeff turnstile_cancel(ts); 765157882Sjhbout: 766167787Sjhb LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line); 767157882Sjhb if (success) 768167787Sjhb WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK, 769157882Sjhb file, line); 770157882Sjhb return (success); 771157882Sjhb} 772157882Sjhb 773157882Sjhb/* 774157882Sjhb * Downgrade a write lock into a single read lock. 775157882Sjhb */ 776157882Sjhbvoid 777157882Sjhb_rw_downgrade(struct rwlock *rw, const char *file, int line) 778157882Sjhb{ 779157882Sjhb struct turnstile *ts; 780157882Sjhb uintptr_t tid, v; 781157882Sjhb 782169394Sjhb KASSERT(rw->rw_lock != RW_DESTROYED, 783169394Sjhb ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line)); 784171052Sattilio _rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line); 785171052Sattilio#ifndef INVARIANTS 786171052Sattilio if (rw_recursed(rw)) 787171052Sattilio panic("downgrade of a recursed lock"); 788171052Sattilio#endif 789157882Sjhb 790167787Sjhb WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line); 791157882Sjhb 792157882Sjhb /* 793157882Sjhb * Convert from a writer to a single reader. First we handle 794157882Sjhb * the easy case with no waiters. If there are any waiters, we 795157882Sjhb * lock the turnstile, "disown" the lock, and awaken any read 796157882Sjhb * waiters. 797157882Sjhb */ 798157882Sjhb tid = (uintptr_t)curthread; 799157882Sjhb if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1))) 800157882Sjhb goto out; 801157882Sjhb 802157882Sjhb /* 803157882Sjhb * Ok, we think we have waiters, so lock the turnstile so we can 804157882Sjhb * read the waiter flags without any races. 805157882Sjhb */ 806170295Sjeff turnstile_chain_lock(&rw->lock_object); 807157882Sjhb v = rw->rw_lock; 808157882Sjhb MPASS(v & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)); 809157882Sjhb 810157882Sjhb /* 811157882Sjhb * Downgrade from a write lock while preserving 812157882Sjhb * RW_LOCK_WRITE_WAITERS and give up ownership of the 813157882Sjhb * turnstile. If there are any read waiters, wake them up. 814157882Sjhb * 815167801Sjhb * For ADAPTIVE_RWLOCKS, we have to allow for the fact that 816167801Sjhb * all of the read waiters might be spinning. In that case, 817167801Sjhb * act as if RW_LOCK_READ_WAITERS is not set. Also, only 818167801Sjhb * preserve the RW_LOCK_WRITE_WAITERS flag if at least one 819167801Sjhb * writer is blocked on the turnstile. 820157882Sjhb */ 821167787Sjhb ts = turnstile_lookup(&rw->lock_object); 822167801Sjhb#ifdef ADAPTIVE_RWLOCKS 823157882Sjhb if (ts == NULL) 824157882Sjhb v &= ~(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS); 825157882Sjhb else if (v & RW_LOCK_READ_WAITERS && 826157882Sjhb turnstile_empty(ts, TS_SHARED_QUEUE)) 827157882Sjhb v &= ~RW_LOCK_READ_WAITERS; 828157882Sjhb else if (v & RW_LOCK_WRITE_WAITERS && 829157882Sjhb turnstile_empty(ts, TS_EXCLUSIVE_QUEUE)) 830157882Sjhb v &= ~RW_LOCK_WRITE_WAITERS; 831157882Sjhb#else 832157882Sjhb MPASS(ts != NULL); 833157882Sjhb#endif 834157882Sjhb if (v & RW_LOCK_READ_WAITERS) 835157882Sjhb turnstile_broadcast(ts, TS_SHARED_QUEUE); 836157882Sjhb atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | 837157882Sjhb (v & RW_LOCK_WRITE_WAITERS)); 838157882Sjhb if (v & RW_LOCK_READ_WAITERS) 839157882Sjhb turnstile_unpend(ts, TS_EXCLUSIVE_LOCK); 840170295Sjeff else if (ts) 841157882Sjhb turnstile_disown(ts); 842170295Sjeff turnstile_chain_unlock(&rw->lock_object); 843157882Sjhbout: 844167787Sjhb LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line); 845157882Sjhb} 846157882Sjhb 847154941Sjhb#ifdef INVARIANT_SUPPORT 848155162Sscottl#ifndef INVARIANTS 849154941Sjhb#undef _rw_assert 850154941Sjhb#endif 851154941Sjhb 852154941Sjhb/* 853154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least 854154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this* 855154941Sjhb * thread owns an rlock. 856154941Sjhb */ 857154941Sjhbvoid 858154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line) 859154941Sjhb{ 860154941Sjhb 861154941Sjhb if (panicstr != NULL) 862154941Sjhb return; 863154941Sjhb switch (what) { 864154941Sjhb case RA_LOCKED: 865171052Sattilio case RA_LOCKED | RA_RECURSED: 866171052Sattilio case RA_LOCKED | RA_NOTRECURSED: 867154941Sjhb case RA_RLOCKED: 868154941Sjhb#ifdef WITNESS 869167787Sjhb witness_assert(&rw->lock_object, what, file, line); 870154941Sjhb#else 871154941Sjhb /* 872154941Sjhb * If some other thread has a write lock or we have one 873154941Sjhb * and are asserting a read lock, fail. Also, if no one 874154941Sjhb * has a lock at all, fail. 875154941Sjhb */ 876155061Sscottl if (rw->rw_lock == RW_UNLOCKED || 877155061Sscottl (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED || 878157826Sjhb rw_wowner(rw) != curthread))) 879154941Sjhb panic("Lock %s not %slocked @ %s:%d\n", 880167787Sjhb rw->lock_object.lo_name, (what == RA_RLOCKED) ? 881154941Sjhb "read " : "", file, line); 882171052Sattilio 883171052Sattilio if (!(rw->rw_lock & RW_LOCK_READ)) { 884171052Sattilio if (rw_recursed(rw)) { 885171052Sattilio if (what & RA_NOTRECURSED) 886171052Sattilio panic("Lock %s recursed @ %s:%d\n", 887171052Sattilio rw->lock_object.lo_name, file, 888171052Sattilio line); 889171052Sattilio } else if (what & RA_RECURSED) 890171052Sattilio panic("Lock %s not recursed @ %s:%d\n", 891171052Sattilio rw->lock_object.lo_name, file, line); 892171052Sattilio } 893154941Sjhb#endif 894154941Sjhb break; 895154941Sjhb case RA_WLOCKED: 896171052Sattilio case RA_WLOCKED | RA_RECURSED: 897171052Sattilio case RA_WLOCKED | RA_NOTRECURSED: 898157826Sjhb if (rw_wowner(rw) != curthread) 899154941Sjhb panic("Lock %s not exclusively locked @ %s:%d\n", 900167787Sjhb rw->lock_object.lo_name, file, line); 901171052Sattilio if (rw_recursed(rw)) { 902171052Sattilio if (what & RA_NOTRECURSED) 903171052Sattilio panic("Lock %s recursed @ %s:%d\n", 904171052Sattilio rw->lock_object.lo_name, file, line); 905171052Sattilio } else if (what & RA_RECURSED) 906171052Sattilio panic("Lock %s not recursed @ %s:%d\n", 907171052Sattilio rw->lock_object.lo_name, file, line); 908154941Sjhb break; 909154941Sjhb case RA_UNLOCKED: 910154941Sjhb#ifdef WITNESS 911167787Sjhb witness_assert(&rw->lock_object, what, file, line); 912154941Sjhb#else 913154941Sjhb /* 914154941Sjhb * If we hold a write lock fail. We can't reliably check 915154941Sjhb * to see if we hold a read lock or not. 916154941Sjhb */ 917157826Sjhb if (rw_wowner(rw) == curthread) 918154941Sjhb panic("Lock %s exclusively locked @ %s:%d\n", 919167787Sjhb rw->lock_object.lo_name, file, line); 920154941Sjhb#endif 921154941Sjhb break; 922154941Sjhb default: 923154941Sjhb panic("Unknown rw lock assertion: %d @ %s:%d", what, file, 924154941Sjhb line); 925154941Sjhb } 926154941Sjhb} 927154941Sjhb#endif /* INVARIANT_SUPPORT */ 928154941Sjhb 929154941Sjhb#ifdef DDB 930154941Sjhbvoid 931154941Sjhbdb_show_rwlock(struct lock_object *lock) 932154941Sjhb{ 933154941Sjhb struct rwlock *rw; 934154941Sjhb struct thread *td; 935154941Sjhb 936154941Sjhb rw = (struct rwlock *)lock; 937154941Sjhb 938154941Sjhb db_printf(" state: "); 939154941Sjhb if (rw->rw_lock == RW_UNLOCKED) 940154941Sjhb db_printf("UNLOCKED\n"); 941169394Sjhb else if (rw->rw_lock == RW_DESTROYED) { 942169394Sjhb db_printf("DESTROYED\n"); 943169394Sjhb return; 944169394Sjhb } else if (rw->rw_lock & RW_LOCK_READ) 945167504Sjhb db_printf("RLOCK: %ju locks\n", 946167504Sjhb (uintmax_t)(RW_READERS(rw->rw_lock))); 947154941Sjhb else { 948157826Sjhb td = rw_wowner(rw); 949154941Sjhb db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td, 950154941Sjhb td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm); 951171052Sattilio if (rw_recursed(rw)) 952171052Sattilio db_printf(" recursed: %u\n", rw->rw_recurse); 953154941Sjhb } 954154941Sjhb db_printf(" waiters: "); 955154941Sjhb switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) { 956154941Sjhb case RW_LOCK_READ_WAITERS: 957154941Sjhb db_printf("readers\n"); 958154941Sjhb break; 959154941Sjhb case RW_LOCK_WRITE_WAITERS: 960154941Sjhb db_printf("writers\n"); 961154941Sjhb break; 962154941Sjhb case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS: 963167492Sjhb db_printf("readers and writers\n"); 964154941Sjhb break; 965154941Sjhb default: 966154941Sjhb db_printf("none\n"); 967154941Sjhb break; 968154941Sjhb } 969154941Sjhb} 970154941Sjhb 971154941Sjhb#endif 972