Home | History | Annotate | Line # | Download | only in kern
kern_rwlock.c revision 1.14
      1 /*	$NetBSD: kern_rwlock.c,v 1.14 2008/01/04 21:52:48 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002, 2006, 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe and Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Kernel reader/writer lock implementation, modeled after those
     41  * found in Solaris, a description of which can be found in:
     42  *
     43  *	Solaris Internals: Core Kernel Architecture, Jim Mauro and
     44  *	    Richard McDougall.
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: kern_rwlock.c,v 1.14 2008/01/04 21:52:48 ad Exp $");
     49 
     50 #include "opt_multiprocessor.h"
     51 
     52 #define	__RWLOCK_PRIVATE
     53 
     54 #include <sys/param.h>
     55 #include <sys/proc.h>
     56 #include <sys/rwlock.h>
     57 #include <sys/sched.h>
     58 #include <sys/sleepq.h>
     59 #include <sys/systm.h>
     60 #include <sys/lockdebug.h>
     61 #include <sys/cpu.h>
     62 #include <sys/atomic.h>
     63 
     64 #include <dev/lockstat.h>
     65 
     66 /*
     67  * LOCKDEBUG
     68  */
     69 
     70 #if defined(LOCKDEBUG)
     71 
     72 #define	RW_WANTLOCK(rw, op)						\
     73 	LOCKDEBUG_WANTLOCK(RW_DEBUG_P(rw), (rw),			\
     74 	    (uintptr_t)__builtin_return_address(0), op == RW_READER);
     75 #define	RW_LOCKED(rw, op)						\
     76 	LOCKDEBUG_LOCKED(RW_DEBUG_P(rw), (rw),				\
     77 	    (uintptr_t)__builtin_return_address(0), op == RW_READER);
     78 #define	RW_UNLOCKED(rw, op)						\
     79 	LOCKDEBUG_UNLOCKED(RW_DEBUG_P(rw), (rw),			\
     80 	    (uintptr_t)__builtin_return_address(0), op == RW_READER);
     81 #define	RW_DASSERT(rw, cond)						\
     82 do {									\
     83 	if (!(cond))							\
     84 		rw_abort(rw, __func__, "assertion failed: " #cond);	\
     85 } while (/* CONSTCOND */ 0);
     86 
     87 #else	/* LOCKDEBUG */
     88 
     89 #define	RW_WANTLOCK(rw, op)	/* nothing */
     90 #define	RW_LOCKED(rw, op)	/* nothing */
     91 #define	RW_UNLOCKED(rw, op)	/* nothing */
     92 #define	RW_DASSERT(rw, cond)	/* nothing */
     93 
     94 #endif	/* LOCKDEBUG */
     95 
     96 /*
     97  * DIAGNOSTIC
     98  */
     99 
    100 #if defined(DIAGNOSTIC)
    101 
    102 #define	RW_ASSERT(rw, cond)						\
    103 do {									\
    104 	if (!(cond))							\
    105 		rw_abort(rw, __func__, "assertion failed: " #cond);	\
    106 } while (/* CONSTCOND */ 0)
    107 
    108 #else
    109 
    110 #define	RW_ASSERT(rw, cond)	/* nothing */
    111 
    112 #endif	/* DIAGNOSTIC */
    113 
    114 /*
    115  * For platforms that use 'simple' RW locks.
    116  */
    117 #ifdef __HAVE_SIMPLE_RW_LOCKS
    118 #define	RW_ACQUIRE(rw, old, new)	RW_CAS1(&(rw)->rw_owner, old, new)
    119 #define	RW_RELEASE(rw, old, new)	RW_CAS1(&(rw)->rw_owner, old, new)
    120 #define	RW_SETDEBUG(rw, on)		((rw)->rw_owner |= (on) ? RW_DEBUG : 0)
    121 #define	RW_DEBUG_P(rw)			(((rw)->rw_owner & RW_DEBUG) != 0)
    122 #if defined(LOCKDEBUG)
    123 #define	RW_INHERITDEBUG(new, old)	(new) |= (old) & RW_DEBUG
    124 #else /* defined(LOCKDEBUG) */
    125 #define	RW_INHERITDEBUG(new, old)	/* nothing */
    126 #endif /* defined(LOCKDEBUG) */
    127 
    128 static inline int
    129 RW_CAS1(volatile uintptr_t *ptr, uintptr_t old, uintptr_t new)
    130 {
    131 
    132 	RW_INHERITDEBUG(new, old);
    133 	return RW_CAS(ptr, old, new);
    134 }
    135 
    136 static inline int
    137 RW_SET_WAITERS(krwlock_t *rw, uintptr_t need, uintptr_t set)
    138 {
    139 	uintptr_t old;
    140 
    141 	if (((old = rw->rw_owner) & need) == 0)
    142 		return 0;
    143 	return RW_CAS(&rw->rw_owner, old, old | set);
    144 }
    145 #endif	/* __HAVE_SIMPLE_RW_LOCKS */
    146 
    147 /*
    148  * For platforms that do not provide stubs, or for the LOCKDEBUG case.
    149  */
    150 #ifdef LOCKDEBUG
    151 #undef	__HAVE_RW_STUBS
    152 #endif
    153 
    154 #ifndef __HAVE_RW_STUBS
    155 __strong_alias(rw_enter,rw_vector_enter);
    156 __strong_alias(rw_exit,rw_vector_exit);
    157 #endif
    158 
    159 static void	rw_dump(volatile void *);
    160 static lwp_t	*rw_owner(wchan_t);
    161 
    162 lockops_t rwlock_lockops = {
    163 	"Reader / writer lock",
    164 	1,
    165 	rw_dump
    166 };
    167 
    168 syncobj_t rw_syncobj = {
    169 	SOBJ_SLEEPQ_SORTED,
    170 	turnstile_unsleep,
    171 	turnstile_changepri,
    172 	sleepq_lendpri,
    173 	rw_owner,
    174 };
    175 
    176 /*
    177  * rw_dump:
    178  *
    179  *	Dump the contents of a rwlock structure.
    180  */
    181 static void
    182 rw_dump(volatile void *cookie)
    183 {
    184 	volatile krwlock_t *rw = cookie;
    185 
    186 	printf_nolog("owner/count  : %#018lx flags    : %#018x\n",
    187 	    (long)RW_OWNER(rw), (int)RW_FLAGS(rw));
    188 }
    189 
    190 /*
    191  * rw_abort:
    192  *
    193  *	Dump information about an error and panic the system.  This
    194  *	generates a lot of machine code in the DIAGNOSTIC case, so
    195  *	we ask the compiler to not inline it.
    196  */
    197 #if __GNUC_PREREQ__(3, 0)
    198 __attribute ((noinline))
    199 #endif
    200 static void
    201 rw_abort(krwlock_t *rw, const char *func, const char *msg)
    202 {
    203 
    204 	if (panicstr != NULL)
    205 		return;
    206 
    207 	LOCKDEBUG_ABORT(rw, &rwlock_lockops, func, msg);
    208 }
    209 
    210 /*
    211  * rw_init:
    212  *
    213  *	Initialize a rwlock for use.
    214  */
    215 void
    216 rw_init(krwlock_t *rw)
    217 {
    218 	bool dodebug;
    219 
    220 	memset(rw, 0, sizeof(*rw));
    221 
    222 	dodebug = LOCKDEBUG_ALLOC(rw, &rwlock_lockops,
    223 	    (uintptr_t)__builtin_return_address(0));
    224 	RW_SETDEBUG(rw, dodebug);
    225 }
    226 
    227 /*
    228  * rw_destroy:
    229  *
    230  *	Tear down a rwlock.
    231  */
    232 void
    233 rw_destroy(krwlock_t *rw)
    234 {
    235 
    236 	RW_ASSERT(rw, (rw->rw_owner & ~RW_DEBUG) == 0);
    237 	LOCKDEBUG_FREE(RW_DEBUG_P(rw), rw);
    238 }
    239 
    240 /*
    241  * rw_vector_enter:
    242  *
    243  *	Acquire a rwlock.
    244  */
    245 void
    246 rw_vector_enter(krwlock_t *rw, const krw_t op)
    247 {
    248 	uintptr_t owner, incr, need_wait, set_wait, curthread;
    249 	turnstile_t *ts;
    250 	int queue;
    251 	lwp_t *l;
    252 	LOCKSTAT_TIMER(slptime);
    253 	LOCKSTAT_FLAG(lsflag);
    254 
    255 	l = curlwp;
    256 	curthread = (uintptr_t)l;
    257 
    258 	RW_ASSERT(rw, !cpu_intr_p());
    259 	RW_ASSERT(rw, curthread != 0);
    260 	RW_WANTLOCK(rw, op);
    261 
    262 	if (panicstr == NULL) {
    263 		LOCKDEBUG_BARRIER(&kernel_lock, 1);
    264 	}
    265 
    266 	/*
    267 	 * We play a slight trick here.  If we're a reader, we want
    268 	 * increment the read count.  If we're a writer, we want to
    269 	 * set the owner field and whe WRITE_LOCKED bit.
    270 	 *
    271 	 * In the latter case, we expect those bits to be zero,
    272 	 * therefore we can use an add operation to set them, which
    273 	 * means an add operation for both cases.
    274 	 */
    275 	if (__predict_true(op == RW_READER)) {
    276 		incr = RW_READ_INCR;
    277 		set_wait = RW_HAS_WAITERS;
    278 		need_wait = RW_WRITE_LOCKED | RW_WRITE_WANTED;
    279 		queue = TS_READER_Q;
    280 	} else {
    281 		RW_DASSERT(rw, op == RW_WRITER);
    282 		incr = curthread | RW_WRITE_LOCKED;
    283 		set_wait = RW_HAS_WAITERS | RW_WRITE_WANTED;
    284 		need_wait = RW_WRITE_LOCKED | RW_THREAD;
    285 		queue = TS_WRITER_Q;
    286 	}
    287 
    288 	LOCKSTAT_ENTER(lsflag);
    289 
    290 	for (;;) {
    291 		/*
    292 		 * Read the lock owner field.  If the need-to-wait
    293 		 * indicator is clear, then try to acquire the lock.
    294 		 */
    295 		owner = rw->rw_owner;
    296 		if ((owner & need_wait) == 0) {
    297 			if (RW_ACQUIRE(rw, owner, owner + incr)) {
    298 				/* Got it! */
    299 				break;
    300 			}
    301 
    302 			/*
    303 			 * Didn't get it -- spin around again (we'll
    304 			 * probably sleep on the next iteration).
    305 			 */
    306 			continue;
    307 		}
    308 
    309 		if (panicstr != NULL)
    310 			return;
    311 		if (RW_OWNER(rw) == curthread)
    312 			rw_abort(rw, __func__, "locking against myself");
    313 
    314 		/*
    315 		 * Grab the turnstile chain lock.  Once we have that, we
    316 		 * can adjust the waiter bits and sleep queue.
    317 		 */
    318 		ts = turnstile_lookup(rw);
    319 
    320 		/*
    321 		 * Mark the rwlock as having waiters.  If the set fails,
    322 		 * then we may not need to sleep and should spin again.
    323 		 */
    324 		if (!RW_SET_WAITERS(rw, need_wait, set_wait)) {
    325 			turnstile_exit(rw);
    326 			continue;
    327 		}
    328 
    329 		LOCKSTAT_START_TIMER(lsflag, slptime);
    330 
    331 		turnstile_block(ts, queue, rw, &rw_syncobj);
    332 
    333 		/* If we wake up and arrive here, we've been handed the lock. */
    334 		RW_RECEIVE(rw);
    335 
    336 		LOCKSTAT_STOP_TIMER(lsflag, slptime);
    337 		LOCKSTAT_EVENT(lsflag, rw,
    338 		    LB_RWLOCK | (op == RW_WRITER ? LB_SLEEP1 : LB_SLEEP2),
    339 		    1, slptime);
    340 
    341 		break;
    342 	}
    343 
    344 	LOCKSTAT_EXIT(lsflag);
    345 
    346 	RW_DASSERT(rw, (op != RW_READER && RW_OWNER(rw) == curthread) ||
    347 	    (op == RW_READER && RW_COUNT(rw) != 0));
    348 	RW_LOCKED(rw, op);
    349 }
    350 
    351 /*
    352  * rw_vector_exit:
    353  *
    354  *	Release a rwlock.
    355  */
    356 void
    357 rw_vector_exit(krwlock_t *rw)
    358 {
    359 	uintptr_t curthread, owner, decr, new;
    360 	turnstile_t *ts;
    361 	int rcnt, wcnt;
    362 	lwp_t *l;
    363 
    364 	curthread = (uintptr_t)curlwp;
    365 	RW_ASSERT(rw, curthread != 0);
    366 
    367 	if (panicstr != NULL)
    368 		return;
    369 
    370 	/*
    371 	 * Again, we use a trick.  Since we used an add operation to
    372 	 * set the required lock bits, we can use a subtract to clear
    373 	 * them, which makes the read-release and write-release path
    374 	 * the same.
    375 	 */
    376 	owner = rw->rw_owner;
    377 	if (__predict_false((owner & RW_WRITE_LOCKED) != 0)) {
    378 		RW_UNLOCKED(rw, RW_WRITER);
    379 		RW_DASSERT(rw, (rw->rw_owner & RW_WRITE_LOCKED) != 0);
    380 		RW_ASSERT(rw, RW_OWNER(rw) == curthread);
    381 		decr = curthread | RW_WRITE_LOCKED;
    382 	} else {
    383 		RW_UNLOCKED(rw, RW_READER);
    384 		RW_ASSERT(rw, (rw->rw_owner & RW_WRITE_LOCKED) == 0);
    385 		RW_ASSERT(rw, RW_COUNT(rw) != 0);
    386 		decr = RW_READ_INCR;
    387 	}
    388 
    389 	/*
    390 	 * Compute what we expect the new value of the lock to be. Only
    391 	 * proceed to do direct handoff if there are waiters, and if the
    392 	 * lock would become unowned.
    393 	 */
    394 	for (;; owner = rw->rw_owner) {
    395 		new = (owner - decr);
    396 		if ((new & (RW_THREAD | RW_HAS_WAITERS)) == RW_HAS_WAITERS)
    397 			break;
    398 		if (RW_RELEASE(rw, owner, new))
    399 			return;
    400 	}
    401 
    402 	for (;;) {
    403 		/*
    404 		 * Grab the turnstile chain lock.  This gets the interlock
    405 		 * on the sleep queue.  Once we have that, we can adjust the
    406 		 * waiter bits.
    407 		 */
    408 		ts = turnstile_lookup(rw);
    409 		RW_DASSERT(rw, ts != NULL);
    410 		RW_DASSERT(rw, (rw->rw_owner & RW_HAS_WAITERS) != 0);
    411 
    412 		owner = rw->rw_owner;
    413 		wcnt = TS_WAITERS(ts, TS_WRITER_Q);
    414 		rcnt = TS_WAITERS(ts, TS_READER_Q);
    415 
    416 		/*
    417 		 * Give the lock away.
    418 		 *
    419 		 * If we are releasing a write lock, then wake all
    420 		 * outstanding readers.  If we are releasing a read
    421 		 * lock, then wake one writer.
    422 		 */
    423 		if (rcnt == 0 || (decr == RW_READ_INCR && wcnt != 0)) {
    424 			RW_DASSERT(rw, wcnt != 0);
    425 			RW_DASSERT(rw, (rw->rw_owner & RW_WRITE_WANTED) != 0);
    426 
    427 			/*
    428 			 * Give the lock to the longest waiting
    429 			 * writer.
    430 			 */
    431 			l = TS_FIRST(ts, TS_WRITER_Q);
    432 			new = (uintptr_t)l | RW_WRITE_LOCKED;
    433 
    434 			if (wcnt > 1)
    435 				new |= RW_HAS_WAITERS | RW_WRITE_WANTED;
    436 			else if (rcnt != 0)
    437 				new |= RW_HAS_WAITERS;
    438 
    439 			RW_GIVE(rw);
    440 			if (!RW_RELEASE(rw, owner, new)) {
    441 				/* Oops, try again. */
    442 				turnstile_exit(rw);
    443 				continue;
    444 			}
    445 
    446 			/* Wake the writer. */
    447 			turnstile_wakeup(ts, TS_WRITER_Q, 1, l);
    448 		} else {
    449 			RW_DASSERT(rw, rcnt != 0);
    450 
    451 			/*
    452 			 * Give the lock to all blocked readers.  If there
    453 			 * is a writer waiting, new readers that arrive
    454 			 * after the release will be blocked out.
    455 			 */
    456 			new = rcnt << RW_READ_COUNT_SHIFT;
    457 			if (wcnt != 0)
    458 				new |= RW_HAS_WAITERS | RW_WRITE_WANTED;
    459 
    460 			RW_GIVE(rw);
    461 			if (!RW_RELEASE(rw, owner, new)) {
    462 				/* Oops, try again. */
    463 				turnstile_exit(rw);
    464 				continue;
    465 			}
    466 
    467 			/* Wake up all sleeping readers. */
    468 			turnstile_wakeup(ts, TS_READER_Q, rcnt, NULL);
    469 		}
    470 
    471 		break;
    472 	}
    473 }
    474 
    475 /*
    476  * rw_tryenter:
    477  *
    478  *	Try to acquire a rwlock.
    479  */
    480 int
    481 rw_tryenter(krwlock_t *rw, const krw_t op)
    482 {
    483 	uintptr_t curthread, owner, incr, need_wait;
    484 
    485 	curthread = (uintptr_t)curlwp;
    486 
    487 	RW_ASSERT(rw, curthread != 0);
    488 	RW_WANTLOCK(rw, op);
    489 
    490 	if (op == RW_READER) {
    491 		incr = RW_READ_INCR;
    492 		need_wait = RW_WRITE_LOCKED | RW_WRITE_WANTED;
    493 	} else {
    494 		RW_DASSERT(rw, op == RW_WRITER);
    495 		incr = curthread | RW_WRITE_LOCKED;
    496 		need_wait = RW_WRITE_LOCKED | RW_THREAD;
    497 	}
    498 
    499 	for (;;) {
    500 		owner = rw->rw_owner;
    501 		if ((owner & need_wait) == 0) {
    502 			if (RW_ACQUIRE(rw, owner, owner + incr)) {
    503 				/* Got it! */
    504 				break;
    505 			}
    506 			continue;
    507 		}
    508 		return 0;
    509 	}
    510 
    511 	RW_LOCKED(rw, op);
    512 	RW_DASSERT(rw, (op != RW_READER && RW_OWNER(rw) == curthread) ||
    513 	    (op == RW_READER && RW_COUNT(rw) != 0));
    514 
    515 	return 1;
    516 }
    517 
    518 /*
    519  * rw_downgrade:
    520  *
    521  *	Downgrade a write lock to a read lock.
    522  */
    523 void
    524 rw_downgrade(krwlock_t *rw)
    525 {
    526 	uintptr_t owner, curthread, new;
    527 	turnstile_t *ts;
    528 	int rcnt, wcnt;
    529 
    530 	curthread = (uintptr_t)curlwp;
    531 	RW_ASSERT(rw, curthread != 0);
    532 	RW_DASSERT(rw, (rw->rw_owner & RW_WRITE_LOCKED) != 0);
    533 	RW_ASSERT(rw, RW_OWNER(rw) == curthread);
    534 	RW_UNLOCKED(rw, RW_WRITER);
    535 
    536 	owner = rw->rw_owner;
    537 	if ((owner & RW_HAS_WAITERS) == 0) {
    538 		/*
    539 		 * There are no waiters, so we can do this the easy way.
    540 		 * Try swapping us down to one read hold.  If it fails, the
    541 		 * lock condition has changed and we most likely now have
    542 		 * waiters.
    543 		 */
    544 		if (RW_RELEASE(rw, owner, RW_READ_INCR)) {
    545 			RW_LOCKED(rw, RW_READER);
    546 			RW_DASSERT(rw, (rw->rw_owner & RW_WRITE_LOCKED) == 0);
    547 			RW_DASSERT(rw, RW_COUNT(rw) != 0);
    548 			return;
    549 		}
    550 	}
    551 
    552 	/*
    553 	 * Grab the turnstile chain lock.  This gets the interlock
    554 	 * on the sleep queue.  Once we have that, we can adjust the
    555 	 * waiter bits.
    556 	 */
    557 	for (;;) {
    558 		ts = turnstile_lookup(rw);
    559 		RW_DASSERT(rw, ts != NULL);
    560 
    561 		owner = rw->rw_owner;
    562 		rcnt = TS_WAITERS(ts, TS_READER_Q);
    563 		wcnt = TS_WAITERS(ts, TS_WRITER_Q);
    564 
    565 		/*
    566 		 * If there are no readers, just preserve the waiters
    567 		 * bits, swap us down to one read hold and return.
    568 		 */
    569 		if (rcnt == 0) {
    570 			RW_DASSERT(rw, wcnt != 0);
    571 			RW_DASSERT(rw, (rw->rw_owner & RW_WRITE_WANTED) != 0);
    572 			RW_DASSERT(rw, (rw->rw_owner & RW_HAS_WAITERS) != 0);
    573 
    574 			new = RW_READ_INCR | RW_HAS_WAITERS | RW_WRITE_WANTED;
    575 			if (!RW_RELEASE(rw, owner, new)) {
    576 				/* Oops, try again. */
    577 				turnstile_exit(ts);
    578 				continue;
    579 			}
    580 			break;
    581 		}
    582 
    583 		/*
    584 		 * Give the lock to all blocked readers.  We may
    585 		 * retain one read hold if downgrading.  If there
    586 		 * is a writer waiting, new readers will be blocked
    587 		 * out.
    588 		 */
    589 		new = (rcnt << RW_READ_COUNT_SHIFT) + RW_READ_INCR;
    590 		if (wcnt != 0)
    591 			new |= RW_HAS_WAITERS | RW_WRITE_WANTED;
    592 
    593 		RW_GIVE(rw);
    594 		if (!RW_RELEASE(rw, owner, new)) {
    595 			/* Oops, try again. */
    596 			turnstile_exit(rw);
    597 			continue;
    598 		}
    599 
    600 		/* Wake up all sleeping readers. */
    601 		turnstile_wakeup(ts, TS_READER_Q, rcnt, NULL);
    602 		break;
    603 	}
    604 
    605 	RW_LOCKED(rw, RW_READER);
    606 	RW_DASSERT(rw, (rw->rw_owner & RW_WRITE_LOCKED) == 0);
    607 	RW_DASSERT(rw, RW_COUNT(rw) != 0);
    608 }
    609 
    610 /*
    611  * rw_tryupgrade:
    612  *
    613  *	Try to upgrade a read lock to a write lock.  We must be the
    614  *	only reader.
    615  */
    616 int
    617 rw_tryupgrade(krwlock_t *rw)
    618 {
    619 	uintptr_t owner, curthread, new;
    620 
    621 	curthread = (uintptr_t)curlwp;
    622 	RW_ASSERT(rw, curthread != 0);
    623 	RW_WANTLOCK(rw, RW_WRITER);
    624 
    625 	for (;;) {
    626 		owner = rw->rw_owner;
    627 		RW_ASSERT(rw, (owner & RW_WRITE_LOCKED) == 0);
    628 		if ((owner & RW_THREAD) != RW_READ_INCR) {
    629 			RW_ASSERT(rw, (owner & RW_THREAD) != 0);
    630 			return 0;
    631 		}
    632 		new = curthread | RW_WRITE_LOCKED | (owner & ~RW_THREAD);
    633 		if (RW_ACQUIRE(rw, owner, new))
    634 			break;
    635 	}
    636 
    637 	RW_UNLOCKED(rw, RW_READER);
    638 	RW_LOCKED(rw, RW_WRITER);
    639 	RW_DASSERT(rw, rw->rw_owner & RW_WRITE_LOCKED);
    640 	RW_DASSERT(rw, RW_OWNER(rw) == curthread);
    641 
    642 	return 1;
    643 }
    644 
    645 /*
    646  * rw_read_held:
    647  *
    648  *	Returns true if the rwlock is held for reading.  Must only be
    649  *	used for diagnostic assertions, and never be used to make
    650  * 	decisions about how to use a rwlock.
    651  */
    652 int
    653 rw_read_held(krwlock_t *rw)
    654 {
    655 	uintptr_t owner;
    656 
    657 	if (panicstr != NULL)
    658 		return 1;
    659 
    660 	owner = rw->rw_owner;
    661 	return (owner & RW_WRITE_LOCKED) == 0 && (owner & RW_THREAD) != 0;
    662 }
    663 
    664 /*
    665  * rw_write_held:
    666  *
    667  *	Returns true if the rwlock is held for writing.  Must only be
    668  *	used for diagnostic assertions, and never be used to make
    669  *	decisions about how to use a rwlock.
    670  */
    671 int
    672 rw_write_held(krwlock_t *rw)
    673 {
    674 
    675 	if (panicstr != NULL)
    676 		return 1;
    677 
    678 	return (rw->rw_owner & RW_WRITE_LOCKED) != 0;
    679 }
    680 
    681 /*
    682  * rw_lock_held:
    683  *
    684  *	Returns true if the rwlock is held for reading or writing.  Must
    685  *	only be used for diagnostic assertions, and never be used to make
    686  *	decisions about how to use a rwlock.
    687  */
    688 int
    689 rw_lock_held(krwlock_t *rw)
    690 {
    691 
    692 	if (panicstr != NULL)
    693 		return 1;
    694 
    695 	return (rw->rw_owner & RW_THREAD) != 0;
    696 }
    697 
    698 /*
    699  * rw_owner:
    700  *
    701  *	Return the current owner of an RW lock, but only if it is write
    702  *	held.  Used for priority inheritance.
    703  */
    704 static lwp_t *
    705 rw_owner(wchan_t obj)
    706 {
    707 	krwlock_t *rw = (void *)(uintptr_t)obj; /* discard qualifiers */
    708 	uintptr_t owner = rw->rw_owner;
    709 
    710 	if ((owner & RW_WRITE_LOCKED) == 0)
    711 		return NULL;
    712 
    713 	return (void *)(owner & RW_THREAD);
    714 }
    715