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