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