kern_rwlock.c revision 173960
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 173960 2007-11-26 22:37:35Z attilio $");
36154941Sjhb
37154941Sjhb#include "opt_ddb.h"
38192853Ssson#include "opt_no_adaptive_rwlocks.h"
39167801Sjhb
40154941Sjhb#include <sys/param.h>
41154941Sjhb#include <sys/ktr.h>
42154941Sjhb#include <sys/lock.h>
43177912Sjeff#include <sys/mutex.h>
44154941Sjhb#include <sys/proc.h>
45154941Sjhb#include <sys/rwlock.h>
46154941Sjhb#include <sys/systm.h>
47154941Sjhb#include <sys/turnstile.h>
48177912Sjeff
49154941Sjhb#include <machine/cpu.h>
50154941Sjhb
51171516SattilioCTASSERT((RW_RECURSE & LO_CLASSFLAGS) == RW_RECURSE);
52154941Sjhb
53154941Sjhb#if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
54171052Sattilio#define	ADAPTIVE_RWLOCKS
55171052Sattilio#endif
56167801Sjhb
57167801Sjhb#ifdef DDB
58167801Sjhb#include <ddb/ddb.h>
59167801Sjhb
60177912Sjeffstatic void	db_show_rwlock(struct lock_object *lock);
61177912Sjeff#endif
62177912Sjeffstatic void	assert_rw(struct lock_object *lock, int what);
63177912Sjeffstatic void	lock_rw(struct lock_object *lock, int how);
64177912Sjeffstatic int	unlock_rw(struct lock_object *lock);
65177912Sjeff
66177912Sjeffstruct lock_class lock_class_rw = {
67177912Sjeff	.lc_name = "rw",
68154941Sjhb	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
69154941Sjhb	.lc_assert = assert_rw,
70154941Sjhb#ifdef DDB
71154941Sjhb	.lc_ddb_show = db_show_rwlock,
72154941Sjhb#endif
73173733Sattilio	.lc_lock = lock_rw,
74167368Sjhb	.lc_unlock = unlock_rw,
75192853Ssson};
76192853Ssson
77192853Ssson/*
78167368Sjhb * Return a pointer to the owning thread if the lock is write-locked or
79154941Sjhb * NULL if the lock is unlocked or read-locked.
80154941Sjhb */
81167365Sjhb#define	rw_wowner(rw)							\
82167365Sjhb	((rw)->rw_lock & RW_LOCK_READ ? NULL :				\
83173733Sattilio	    (struct thread *)RW_OWNER((rw)->rw_lock))
84154941Sjhb
85167365Sjhb/*
86154941Sjhb * Returns if a write owner is recursed.  Write ownership is not assured
87167368Sjhb * here and should be previously checked.
88167368Sjhb */
89192853Ssson#define	rw_recursed(rw)		((rw)->rw_recurse != 0)
90192853Ssson
91192853Ssson/*
92154941Sjhb * Return true if curthread helds the lock.
93154941Sjhb */
94157826Sjhb#define	rw_wlocked(rw)		(rw_wowner((rw)) == curthread)
95157826Sjhb
96157826Sjhb/*
97157826Sjhb * Return a pointer to the owning thread for this lock who should receive
98157826Sjhb * any priority lent by threads that block on this lock.  Currently this
99154941Sjhb * is identical to rw_wowner().
100154941Sjhb */
101154941Sjhb#define	rw_owner(rw)		rw_wowner(rw)
102157826Sjhb
103171052Sattilio#ifndef INVARIANTS
104171052Sattilio#define	_rw_assert(rw, what, file, line)
105171052Sattilio#endif
106171052Sattilio
107171052Sattiliovoid
108171052Sattilioassert_rw(struct lock_object *lock, int what)
109171052Sattilio{
110171052Sattilio
111171052Sattilio	rw_assert((struct rwlock *)lock, what);
112171052Sattilio}
113171052Sattilio
114157826Sjhbvoid
115157826Sjhblock_rw(struct lock_object *lock, int how)
116157826Sjhb{
117157826Sjhb	struct rwlock *rw;
118157826Sjhb
119157826Sjhb	rw = (struct rwlock *)lock;
120154941Sjhb	if (how)
121154941Sjhb		rw_wlock(rw);
122154941Sjhb	else
123154941Sjhb		rw_rlock(rw);
124154941Sjhb}
125173733Sattilio
126173733Sattilioint
127173733Sattiliounlock_rw(struct lock_object *lock)
128173733Sattilio{
129173733Sattilio	struct rwlock *rw;
130173733Sattilio
131173733Sattilio	rw = (struct rwlock *)lock;
132167368Sjhb	rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
133167368Sjhb	if (rw->rw_lock & RW_LOCK_READ) {
134167368Sjhb		rw_runlock(rw);
135167368Sjhb		return (0);
136167368Sjhb	} else {
137167368Sjhb		rw_wunlock(rw);
138167368Sjhb		return (1);
139167368Sjhb	}
140167368Sjhb}
141167368Sjhb
142167368Sjhbvoid
143167368Sjhbrw_init_flags(struct rwlock *rw, const char *name, int opts)
144167368Sjhb{
145167368Sjhb	int flags;
146167368Sjhb
147167368Sjhb	MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
148167368Sjhb	    RW_RECURSE)) == 0);
149167368Sjhb
150167368Sjhb	flags = LO_UPGRADABLE | LO_RECURSABLE;
151167368Sjhb	if (opts & RW_DUPOK)
152167368Sjhb		flags |= LO_DUPOK;
153167368Sjhb	if (opts & RW_NOPROFILE)
154167368Sjhb		flags |= LO_NOPROFILE;
155167368Sjhb	if (!(opts & RW_NOWITNESS))
156167368Sjhb		flags |= LO_WITNESS;
157167368Sjhb	if (opts & RW_QUIET)
158167368Sjhb		flags |= LO_QUIET;
159192853Ssson	flags |= opts & RW_RECURSE;
160192853Ssson
161192853Ssson	rw->rw_lock = RW_UNLOCKED;
162192853Ssson	rw->rw_recurse = 0;
163192853Ssson	lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
164192853Ssson}
165192853Ssson
166192853Sssonvoid
167192853Sssonrw_destroy(struct rwlock *rw)
168192853Ssson{
169192853Ssson
170192853Ssson	KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked"));
171192853Ssson	KASSERT(rw->rw_recurse == 0, ("rw lock still recursed"));
172167368Sjhb	rw->rw_lock = RW_DESTROYED;
173171052Sattilio	lock_destroy(&rw->lock_object);
174154941Sjhb}
175171052Sattilio
176154941Sjhbvoid
177171052Sattiliorw_sysinit(void *arg)
178171052Sattilio{
179171052Sattilio	struct rw_args *args = arg;
180171052Sattilio
181171052Sattilio	rw_init(args->ra_rw, args->ra_desc);
182171052Sattilio}
183171052Sattilio
184171052Sattilioint
185171052Sattiliorw_wowned(struct rwlock *rw)
186171052Sattilio{
187171052Sattilio
188171052Sattilio	return (rw_wowner(rw) == curthread);
189171052Sattilio}
190171052Sattilio
191154941Sjhbvoid
192171052Sattilio_rw_wlock(struct rwlock *rw, const char *file, int line)
193171052Sattilio{
194154941Sjhb
195154941Sjhb	MPASS(curthread != NULL);
196154941Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
197154941Sjhb	    ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
198154941Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
199154941Sjhb	    line);
200154941Sjhb	__rw_wlock(rw, curthread, file, line);
201171052Sattilio	LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
202169394Sjhb	WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
203167787Sjhb	curthread->td_locks++;
204154941Sjhb}
205154941Sjhb
206154941Sjhbvoid
207154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line)
208154941Sjhb{
209154941Sjhb
210154941Sjhb	MPASS(curthread != NULL);
211154941Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
212154941Sjhb	    ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
213154941Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
214185778Skmacy	curthread->td_locks--;
215185778Skmacy	WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
216185778Skmacy	LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
217185778Skmacy	    line);
218185778Skmacy	if (!rw_recursed(rw))
219185778Skmacy		lock_profile_release_lock(&rw->lock_object);
220185778Skmacy	__rw_wunlock(rw, curthread, file, line);
221185778Skmacy}
222167024Srwatson
223167024Srwatsonvoid
224167024Srwatson_rw_rlock(struct rwlock *rw, const char *file, int line)
225167024Srwatson{
226167024Srwatson	struct turnstile *ts;
227167024Srwatson#ifdef ADAPTIVE_RWLOCKS
228167024Srwatson	volatile struct thread *owner;
229154941Sjhb#endif
230154941Sjhb#ifdef LOCK_PROFILING_SHARED
231154941Sjhb	uint64_t waittime = 0;
232154941Sjhb	int contested = 0;
233154941Sjhb#endif
234169394Sjhb	uintptr_t x;
235169394Sjhb
236167787Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
237182914Sjhb	    ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
238154941Sjhb	KASSERT(rw_wowner(rw) != curthread,
239171052Sattilio	    ("%s (%s): wlock already held @ %s:%d", __func__,
240167787Sjhb	    rw->lock_object.lo_name, file, line));
241160771Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line);
242154941Sjhb
243154941Sjhb	/*
244177843Sattilio	 * Note that we don't make any attempt to try to block read
245177843Sattilio	 * locks once a writer has blocked on the lock.  The reason is
246177843Sattilio	 * that we currently allow for read locks to recurse and we
247177843Sattilio	 * don't keep track of all the holders of read locks.  Thus, if
248177843Sattilio	 * we were to block readers once a writer blocked and a reader
249177843Sattilio	 * tried to recurse on their reader lock after a writer had
250177843Sattilio	 * blocked we would end up in a deadlock since the reader would
251177843Sattilio	 * be blocked on the writer, and the writer would be blocked
252177843Sattilio	 * waiting for the reader to release its original read lock.
253177843Sattilio	 */
254177843Sattilio	for (;;) {
255177843Sattilio		/*
256177843Sattilio		 * Handle the easy case.  If no other thread has a write
257177843Sattilio		 * lock, then try to bump up the count of read locks.  Note
258177843Sattilio		 * that we have to preserve the current state of the
259177843Sattilio		 * RW_LOCK_WRITE_WAITERS flag.  If we fail to acquire a
260177843Sattilio		 * read lock, then rw_lock must have changed, so restart
261177843Sattilio		 * the loop.  Note that this handles the case of a
262177843Sattilio		 * completely unlocked rwlock since such a lock is encoded
263177843Sattilio		 * as a read lock with no waiters.
264177843Sattilio		 */
265177843Sattilio		x = rw->rw_lock;
266177843Sattilio		if (x & RW_LOCK_READ) {
267177843Sattilio
268154941Sjhb			/*
269154941Sjhb			 * The RW_LOCK_READ_WAITERS flag should only be set
270154941Sjhb			 * if another thread currently holds a write lock,
271154941Sjhb			 * and in that case RW_LOCK_READ should be clear.
272154941Sjhb			 */
273169394Sjhb			MPASS((x & RW_LOCK_READ_WAITERS) == 0);
274169394Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock, x,
275154941Sjhb			    x + RW_ONE_READER)) {
276160771Sjhb#ifdef LOCK_PROFILING_SHARED
277167787Sjhb				if (RW_READERS(x) == 0)
278171052Sattilio					lock_profile_obtain_lock_success(
279171052Sattilio					    &rw->lock_object, contested,
280171052Sattilio					    waittime, file, line);
281192853Ssson#endif
282154941Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
283154941Sjhb					CTR4(KTR_LOCK,
284176017Sjeff					    "%s: %p succeed %p -> %p", __func__,
285176017Sjeff					    rw, (void *)x,
286176017Sjeff					    (void *)(x + RW_ONE_READER));
287176017Sjeff				break;
288176017Sjeff			}
289176017Sjeff			cpu_spinwait();
290176017Sjeff			continue;
291176017Sjeff		}
292176017Sjeff
293176017Sjeff#ifdef ADAPTIVE_RWLOCKS
294176017Sjeff		/*
295154941Sjhb		 * If the owner is running on another CPU, spin until
296154941Sjhb		 * the owner stops running or the state of the lock
297154941Sjhb		 * changes.
298154941Sjhb		 */
299170295Sjeff		owner = (struct thread *)RW_OWNER(x);
300167801Sjhb		if (TD_IS_RUNNING(owner)) {
301157846Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
302177912Sjeff				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
303177912Sjeff				    __func__, rw, owner);
304157851Swkoszek#ifdef LOCK_PROFILING_SHARED
305189846Sjeff			lock_profile_obtain_lock_failed(&rw->lock_object,
306167307Sjhb			    &contested, &waittime);
307167054Skmacy#endif
308189846Sjeff			while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
309176017Sjeff			    TD_IS_RUNNING(owner))
310192853Ssson				cpu_spinwait();
311192853Ssson			continue;
312192853Ssson		}
313192853Ssson#endif
314192853Ssson
315154941Sjhb		/*
316169394Sjhb		 * Okay, now it's the hard case.  Some other thread already
317169394Sjhb		 * has a write lock, so acquire the turnstile lock so we can
318157826Sjhb		 * begin the process of blocking.
319154941Sjhb		 */
320167787Sjhb		ts = turnstile_trywait(&rw->lock_object);
321182914Sjhb
322154941Sjhb		/*
323154941Sjhb		 * The lock might have been released while we spun, so
324192853Ssson		 * recheck its state and restart the loop if there is no
325192853Ssson		 * longer a write lock.
326192853Ssson		 */
327154941Sjhb		x = rw->rw_lock;
328154941Sjhb		if (x & RW_LOCK_READ) {
329154941Sjhb			turnstile_cancel(ts);
330154941Sjhb			cpu_spinwait();
331154941Sjhb			continue;
332154941Sjhb		}
333154941Sjhb
334154941Sjhb#ifdef ADAPTIVE_RWLOCKS
335154941Sjhb		/*
336154941Sjhb		 * If the current owner of the lock is executing on another
337176017Sjeff		 * CPU quit the hard path and try to spin.
338176017Sjeff		 */
339154941Sjhb		owner = (struct thread *)RW_OWNER(x);
340154941Sjhb		if (TD_IS_RUNNING(owner)) {
341176017Sjeff			turnstile_cancel(ts);
342176017Sjeff			cpu_spinwait();
343154941Sjhb			continue;
344176017Sjeff		}
345176017Sjeff#endif
346167787Sjhb
347154941Sjhb		/*
348154941Sjhb		 * Ok, it's still a write lock.  If the RW_LOCK_READ_WAITERS
349176017Sjeff		 * flag is already set, then we can go ahead and block.  If
350176017Sjeff		 * it is not set then try to set it.  If we fail to set it
351154941Sjhb		 * drop the turnstile lock and restart the loop.
352154941Sjhb		 */
353157846Sjhb		if (!(x & RW_LOCK_READ_WAITERS)) {
354154941Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, x,
355154941Sjhb			    x | RW_LOCK_READ_WAITERS)) {
356174629Sjeff				turnstile_cancel(ts);
357174629Sjeff				cpu_spinwait();
358154941Sjhb				continue;
359173960Sattilio			}
360154941Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
361173960Sattilio				CTR2(KTR_LOCK, "%s: %p set read waiters flag",
362173960Sattilio				    __func__, rw);
363173960Sattilio		}
364173960Sattilio
365176017Sjeff		/*
366176017Sjeff		 * We were unable to acquire the lock and the read waiters
367176017Sjeff		 * flag is set, so we must block on the turnstile.
368176017Sjeff		 */
369176017Sjeff		if (LOCK_LOG_TEST(&rw->lock_object, 0))
370176017Sjeff			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
371176017Sjeff			    rw);
372176017Sjeff#ifdef LOCK_PROFILING_SHARED
373192853Ssson		lock_profile_obtain_lock_failed(&rw->lock_object, &contested,
374176017Sjeff		    &waittime);
375192853Ssson#endif
376192853Ssson		turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
377192853Ssson		if (LOCK_LOG_TEST(&rw->lock_object, 0))
378192853Ssson			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
379176017Sjeff			    __func__, rw);
380176017Sjeff	}
381177912Sjeff
382177912Sjeff	/*
383177912Sjeff	 * TODO: acquire "owner of record" here.  Here be turnstile dragons
384177912Sjeff	 * however.  turnstiles don't like owners changing between calls to
385177912Sjeff	 * turnstile_wait() currently.
386177912Sjeff	 */
387177912Sjeff
388177912Sjeff	LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
389177912Sjeff	WITNESS_LOCK(&rw->lock_object, 0, file, line);
390177912Sjeff	curthread->td_locks++;
391173960Sattilio}
392173960Sattilio
393173960Sattiliovoid
394173960Sattilio_rw_runlock(struct rwlock *rw, const char *file, int line)
395154941Sjhb{
396176017Sjeff	struct turnstile *ts;
397176017Sjeff	uintptr_t x;
398176017Sjeff
399154941Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
400170295Sjeff	    ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
401154941Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
402154941Sjhb	curthread->td_locks--;
403154941Sjhb	WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
404176017Sjeff	LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
405154941Sjhb
406176017Sjeff	/* TODO: drop "owner of record" here. */
407176017Sjeff
408170295Sjeff	for (;;) {
409157846Sjhb		/*
410154941Sjhb		 * See if there is more than one read lock held.  If so,
411154941Sjhb		 * just drop one and return.
412154941Sjhb		 */
413173960Sattilio		x = rw->rw_lock;
414154941Sjhb		if (RW_READERS(x) > 1) {
415173960Sattilio			if (atomic_cmpset_ptr(&rw->rw_lock, x,
416173960Sattilio			    x - RW_ONE_READER)) {
417173960Sattilio				if (LOCK_LOG_TEST(&rw->lock_object, 0))
418176017Sjeff					CTR4(KTR_LOCK,
419176017Sjeff					    "%s: %p succeeded %p -> %p",
420176017Sjeff					    __func__, rw, (void *)x,
421176017Sjeff					    (void *)(x - RW_ONE_READER));
422176017Sjeff				break;
423176017Sjeff			}
424176017Sjeff			continue;
425173960Sattilio		}
426173960Sattilio
427173960Sattilio
428173960Sattilio		/*
429176017Sjeff		 * We should never have read waiters while at least one
430154941Sjhb		 * thread holds a read lock.  (See note above)
431176017Sjeff		 */
432176017Sjeff		KASSERT(!(x & RW_LOCK_READ_WAITERS),
433176017Sjeff		    ("%s: waiting readers", __func__));
434176017Sjeff#ifdef LOCK_PROFILING_SHARED
435176017Sjeff		lock_profile_release_lock(&rw->lock_object);
436176017Sjeff#endif
437176017Sjeff
438176017Sjeff		/*
439176017Sjeff		 * If there aren't any waiters for a write lock, then try
440176017Sjeff		 * to drop it quickly.
441176017Sjeff		 */
442170295Sjeff		if (!(x & RW_LOCK_WRITE_WAITERS)) {
443157826Sjhb
444157826Sjhb			/*
445157826Sjhb			 * There shouldn't be any flags set and we should
446167787Sjhb			 * be the only read lock.  If we fail to release
447157826Sjhb			 * the single read lock, then another thread might
448157826Sjhb			 * have just acquired a read lock, so go back up
449154941Sjhb			 * to the multiple read locks case.
450154941Sjhb			 */
451154941Sjhb			MPASS(x == RW_READERS_LOCK(1));
452154941Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1),
453154941Sjhb			    RW_UNLOCKED)) {
454154941Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
455167787Sjhb					CTR2(KTR_LOCK, "%s: %p last succeeded",
456154941Sjhb					    __func__, rw);
457154941Sjhb				break;
458192853Ssson			}
459192853Ssson			continue;
460192853Ssson		}
461170295Sjeff
462192853Ssson		/*
463192853Ssson		 * There should just be one reader with one or more
464192853Ssson		 * writers waiting.
465192853Ssson		 */
466167787Sjhb		MPASS(x == (RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS));
467154941Sjhb
468154941Sjhb		/*
469154941Sjhb		 * Ok, we know we have a waiting writer and we think we
470154941Sjhb		 * are the last reader, so grab the turnstile lock.
471154941Sjhb		 */
472154941Sjhb		turnstile_chain_lock(&rw->lock_object);
473154941Sjhb
474154941Sjhb		/*
475154941Sjhb		 * Try to drop our lock leaving the lock in a unlocked
476192853Ssson		 * state.
477174629Sjeff		 *
478167787Sjhb		 * If you wanted to do explicit lock handoff you'd have to
479167787Sjhb		 * do it here.  You'd also want to use turnstile_signal()
480160771Sjhb		 * and you'd have to handle the race where a higher
481176017Sjeff		 * priority thread blocks on the write lock before the
482192853Ssson		 * thread you wakeup actually runs and have the new thread
483192853Ssson		 * "steal" the lock.  For now it's a lot simpler to just
484192853Ssson		 * wakeup all of the waiters.
485192853Ssson		 *
486192853Ssson		 * As above, if we fail, then another thread might have
487192853Ssson		 * acquired a read lock, so drop the turnstile lock and
488192853Ssson		 * restart.
489192853Ssson		 */
490192853Ssson		if (!atomic_cmpset_ptr(&rw->rw_lock,
491192853Ssson		    RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS, RW_UNLOCKED)) {
492154941Sjhb			turnstile_chain_unlock(&rw->lock_object);
493154941Sjhb			continue;
494177843Sattilio		}
495177843Sattilio		if (LOCK_LOG_TEST(&rw->lock_object, 0))
496177843Sattilio			CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
497177843Sattilio			    __func__, rw);
498177843Sattilio
499177843Sattilio		/*
500177843Sattilio		 * Ok.  The lock is released and all that's left is to
501177843Sattilio		 * wake up the waiters.  Note that the lock might not be
502177843Sattilio		 * free anymore, but in that case the writers will just
503177843Sattilio		 * block again if they run before the new lock holder(s)
504177843Sattilio		 * release the lock.
505177843Sattilio		 */
506177843Sattilio		ts = turnstile_lookup(&rw->lock_object);
507177843Sattilio		MPASS(ts != NULL);
508177843Sattilio		turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
509177843Sattilio		turnstile_unpend(ts, TS_SHARED_LOCK);
510177843Sattilio		turnstile_chain_unlock(&rw->lock_object);
511177843Sattilio		break;
512177843Sattilio	}
513177843Sattilio}
514177843Sattilio
515177843Sattilio/*
516177843Sattilio * This function is called when we are unable to obtain a write lock on the
517177843Sattilio * first try.  This means that at least one other thread holds either a
518177843Sattilio * read or write lock.
519154941Sjhb */
520154941Sjhbvoid
521154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
522154941Sjhb{
523176017Sjeff	struct turnstile *ts;
524154941Sjhb#ifdef ADAPTIVE_RWLOCKS
525169394Sjhb	volatile struct thread *owner;
526169394Sjhb#endif
527154941Sjhb	uint64_t waittime = 0;
528160771Sjhb	uintptr_t v;
529176017Sjeff	int contested = 0;
530167787Sjhb
531167787Sjhb	if (rw_wlocked(rw)) {
532154941Sjhb		KASSERT(rw->lock_object.lo_flags & RW_RECURSE,
533154941Sjhb		    ("%s: recursing but non-recursive rw %s @ %s:%d\n",
534154941Sjhb		    __func__, rw->lock_object.lo_name, file, line));
535154941Sjhb		rw->rw_recurse++;
536154941Sjhb		atomic_set_ptr(&rw->rw_lock, RW_LOCK_RECURSED);
537154941Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
538154941Sjhb			CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
539154941Sjhb		return;
540154941Sjhb	}
541154941Sjhb
542154941Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
543154941Sjhb		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
544167787Sjhb		    rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
545154941Sjhb
546154941Sjhb	while (!_rw_write_lock(rw, tid)) {
547154941Sjhb#ifdef ADAPTIVE_RWLOCKS
548154941Sjhb		/*
549154941Sjhb		 * If the lock is write locked and the owner is
550154941Sjhb		 * running on another CPU, spin until the owner stops
551154941Sjhb		 * running or the state of the lock changes.
552167307Sjhb		 */
553154941Sjhb		v = rw->rw_lock;
554154941Sjhb		owner = (struct thread *)RW_OWNER(v);
555154941Sjhb		if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
556154941Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
557176017Sjeff				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
558176017Sjeff				    __func__, rw, owner);
559176017Sjeff			lock_profile_obtain_lock_failed(&rw->lock_object,
560176017Sjeff			    &contested, &waittime);
561167787Sjhb			while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
562154941Sjhb			    TD_IS_RUNNING(owner))
563154941Sjhb				cpu_spinwait();
564154941Sjhb			continue;
565154941Sjhb		}
566154941Sjhb#endif
567154941Sjhb
568154941Sjhb		ts = turnstile_trywait(&rw->lock_object);
569176017Sjeff		v = rw->rw_lock;
570176017Sjeff
571154941Sjhb		/*
572170295Sjeff		 * If the lock was released while spinning on the
573176017Sjeff		 * turnstile chain lock, try again.
574176017Sjeff		 */
575154941Sjhb		if (v == RW_UNLOCKED) {
576154941Sjhb			turnstile_cancel(ts);
577154941Sjhb			cpu_spinwait();
578154941Sjhb			continue;
579154941Sjhb		}
580154941Sjhb
581154941Sjhb#ifdef ADAPTIVE_RWLOCKS
582154941Sjhb		/*
583154941Sjhb		 * If the current owner of the lock is executing on another
584154941Sjhb		 * CPU quit the hard path and try to spin.
585154941Sjhb		 */
586154941Sjhb		if (!(v & RW_LOCK_READ)) {
587154941Sjhb			owner = (struct thread *)RW_OWNER(v);
588154941Sjhb			if (TD_IS_RUNNING(owner)) {
589154941Sjhb				turnstile_cancel(ts);
590154941Sjhb				cpu_spinwait();
591154941Sjhb				continue;
592176017Sjeff			}
593176017Sjeff		}
594176017Sjeff#endif
595176017Sjeff
596176017Sjeff		/*
597176017Sjeff		 * If the lock was released by a writer with both readers
598176017Sjeff		 * and writers waiting and a reader hasn't woken up and
599176017Sjeff		 * acquired the lock yet, rw_lock will be set to the
600170295Sjeff		 * value RW_UNLOCKED | RW_LOCK_WRITE_WAITERS.  If we see
601154941Sjhb		 * that value, try to acquire it once.  Note that we have
602154941Sjhb		 * to preserve the RW_LOCK_WRITE_WAITERS flag as there are
603167787Sjhb		 * other writers waiting still.  If we fail, restart the
604154941Sjhb		 * loop.
605154941Sjhb		 */
606154941Sjhb		if (v == (RW_UNLOCKED | RW_LOCK_WRITE_WAITERS)) {
607154941Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock,
608154941Sjhb			    RW_UNLOCKED | RW_LOCK_WRITE_WAITERS,
609154941Sjhb			    tid | RW_LOCK_WRITE_WAITERS)) {
610154941Sjhb				turnstile_claim(ts);
611154941Sjhb				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
612154941Sjhb				    __func__, rw);
613154941Sjhb				break;
614167787Sjhb			}
615157846Sjhb			turnstile_cancel(ts);
616176017Sjeff			cpu_spinwait();
617154941Sjhb			continue;
618170295Sjeff		}
619154941Sjhb
620154941Sjhb		/*
621192853Ssson		 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
622154941Sjhb		 * set it.  If we fail to set it, then loop back and try
623154941Sjhb		 * again.
624154941Sjhb		 */
625154941Sjhb		if (!(v & RW_LOCK_WRITE_WAITERS)) {
626154941Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
627154941Sjhb			    v | RW_LOCK_WRITE_WAITERS)) {
628154941Sjhb				turnstile_cancel(ts);
629154941Sjhb				cpu_spinwait();
630154941Sjhb				continue;
631154941Sjhb			}
632170295Sjeff			if (LOCK_LOG_TEST(&rw->lock_object, 0))
633167801Sjhb				CTR2(KTR_LOCK, "%s: %p set write waiters flag",
634157846Sjhb				    __func__, rw);
635176017Sjeff		}
636176017Sjeff
637157851Swkoszek		/*
638189846Sjeff		 * We were unable to acquire the lock and the write waiters
639189846Sjeff		 * flag is set, so we must block on the turnstile.
640171516Sattilio		 */
641171516Sattilio		if (LOCK_LOG_TEST(&rw->lock_object, 0))
642189846Sjeff			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
643192853Ssson			    rw);
644192853Ssson		lock_profile_obtain_lock_failed(&rw->lock_object, &contested,
645192853Ssson		    &waittime);
646192853Ssson		turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
647192853Ssson		if (LOCK_LOG_TEST(&rw->lock_object, 0))
648154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
649171052Sattilio			    __func__, rw);
650171052Sattilio	}
651171052Sattilio	lock_profile_obtain_lock_success(&rw->lock_object, contested, waittime,
652171052Sattilio	    file, line);
653171052Sattilio}
654171052Sattilio
655171052Sattilio/*
656171052Sattilio * This function is called if the first try at releasing a write lock failed.
657171052Sattilio * This means that one of the 2 waiter bits must be set indicating that at
658171052Sattilio * least one thread is waiting on this lock.
659167787Sjhb */
660154941Sjhbvoid
661167787Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
662154941Sjhb{
663154941Sjhb	struct turnstile *ts;
664192853Ssson	uintptr_t v;
665192853Ssson	int queue;
666192853Ssson
667174629Sjeff	if (rw_wlocked(rw) && rw_recursed(rw)) {
668174629Sjeff		if ((--rw->rw_recurse) == 0)
669173960Sattilio			atomic_clear_ptr(&rw->rw_lock, RW_LOCK_RECURSED);
670173960Sattilio		if (LOCK_LOG_TEST(&rw->lock_object, 0))
671173960Sattilio			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
672173960Sattilio		return;
673173960Sattilio	}
674173960Sattilio
675173960Sattilio	KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
676173960Sattilio	    ("%s: neither of the waiter flags are set", __func__));
677173960Sattilio
678173960Sattilio	if (LOCK_LOG_TEST(&rw->lock_object, 0))
679173960Sattilio		CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
680173960Sattilio
681173960Sattilio	turnstile_chain_lock(&rw->lock_object);
682192853Ssson	ts = turnstile_lookup(&rw->lock_object);
683173960Sattilio
684192853Ssson	MPASS(ts != NULL);
685192853Ssson
686192853Ssson	/*
687192853Ssson	 * Use the same algo as sx locks for now.  Prefer waking up shared
688173960Sattilio	 * waiters if we have any over writers.  This is probably not ideal.
689173960Sattilio	 *
690177912Sjeff	 * 'v' is the value we are going to write back to rw_lock.  If we
691177912Sjeff	 * have waiters on both queues, we need to preserve the state of
692176017Sjeff	 * the waiter flag for the queue we don't wake up.  For now this is
693176017Sjeff	 * hardcoded for the algorithm mentioned above.
694176017Sjeff	 *
695176017Sjeff	 * In the case of both readers and writers waiting we wakeup the
696176017Sjeff	 * readers but leave the RW_LOCK_WRITE_WAITERS flag set.  If a
697176017Sjeff	 * new writer comes in before a reader it will claim the lock up
698176017Sjeff	 * above.  There is probably a potential priority inversion in
699176017Sjeff	 * there that could be worked around either by waking both queues
700177912Sjeff	 * of waiters or doing some complicated lock handoff gymnastics.
701176017Sjeff	 */
702176017Sjeff	v = RW_UNLOCKED;
703176017Sjeff	if (rw->rw_lock & RW_LOCK_READ_WAITERS) {
704176017Sjeff		queue = TS_SHARED_QUEUE;
705192853Ssson		v |= (rw->rw_lock & RW_LOCK_WRITE_WAITERS);
706192853Ssson	} else
707192853Ssson		queue = TS_EXCLUSIVE_QUEUE;
708177912Sjeff
709176017Sjeff	/* Wake up all waiters for the specific queue. */
710176017Sjeff	if (LOCK_LOG_TEST(&rw->lock_object, 0))
711173960Sattilio		CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
712170295Sjeff		    queue == TS_SHARED_QUEUE ? "read" : "write");
713154941Sjhb	turnstile_broadcast(ts, queue);
714154941Sjhb	atomic_store_rel_ptr(&rw->rw_lock, v);
715173960Sattilio	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
716154941Sjhb	turnstile_chain_unlock(&rw->lock_object);
717173960Sattilio}
718173960Sattilio
719173960Sattilio/*
720173960Sattilio * Attempt to do a non-blocking upgrade from a read lock to a write
721173960Sattilio * lock.  This will only succeed if this thread holds a single read
722173960Sattilio * lock.  Returns true if the upgrade succeeded and false otherwise.
723173960Sattilio */
724173960Sattilioint
725173960Sattilio_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
726173960Sattilio{
727173960Sattilio	uintptr_t v, tid;
728173960Sattilio	struct turnstile *ts;
729173960Sattilio	int success;
730179334Sattilio
731179334Sattilio	KASSERT(rw->rw_lock != RW_DESTROYED,
732179334Sattilio	    ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
733179334Sattilio	_rw_assert(rw, RA_RLOCKED, file, line);
734179334Sattilio
735154941Sjhb	/*
736176017Sjeff	 * Attempt to switch from one reader to a writer.  If there
737176017Sjeff	 * are any write waiters, then we will have to lock the
738176017Sjeff	 * turnstile first to prevent races with another writer
739176017Sjeff	 * calling turnstile_wait() before we have claimed this
740176017Sjeff	 * turnstile.  So, do the simple case of no waiters first.
741176017Sjeff	 */
742176017Sjeff	tid = (uintptr_t)curthread;
743176017Sjeff	if (!(rw->rw_lock & RW_LOCK_WRITE_WAITERS)) {
744154941Sjhb		success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1),
745154941Sjhb		    tid);
746170295Sjeff		goto out;
747154941Sjhb	}
748154941Sjhb
749154941Sjhb	/*
750154941Sjhb	 * Ok, we think we have write waiters, so lock the
751154941Sjhb	 * turnstile.
752154941Sjhb	 */
753154941Sjhb	ts = turnstile_trywait(&rw->lock_object);
754154941Sjhb
755157826Sjhb	/*
756157826Sjhb	 * Try to switch from one reader to a writer again.  This time
757157826Sjhb	 * we honor the current state of the RW_LOCK_WRITE_WAITERS
758170295Sjeff	 * flag.  If we obtain the lock with the flag set, then claim
759157826Sjhb	 * ownership of the turnstile.
760157826Sjhb	 */
761157826Sjhb	v = rw->rw_lock & RW_LOCK_WRITE_WAITERS;
762167787Sjhb	success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
763157826Sjhb	    tid | v);
764157826Sjhb	if (success && v)
765154941Sjhb		turnstile_claim(ts);
766157846Sjhb	else
767154941Sjhb		turnstile_cancel(ts);
768154941Sjhbout:
769154941Sjhb	LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
770167787Sjhb	if (success)
771154941Sjhb		WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
772154941Sjhb		    file, line);
773192853Ssson	return (success);
774192853Ssson}
775192853Ssson
776170295Sjeff/*
777192853Ssson * Downgrade a write lock into a single read lock.
778192853Ssson */
779192853Sssonvoid
780192853Ssson_rw_downgrade(struct rwlock *rw, const char *file, int line)
781167787Sjhb{
782154941Sjhb	struct turnstile *ts;
783154941Sjhb	uintptr_t tid, v;
784176017Sjeff
785176017Sjeff	KASSERT(rw->rw_lock != RW_DESTROYED,
786176017Sjeff	    ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
787154941Sjhb	_rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
788192853Ssson#ifndef INVARIANTS
789192853Ssson	if (rw_recursed(rw))
790192853Ssson		panic("downgrade of a recursed lock");
791192853Ssson#endif
792192853Ssson
793192853Ssson	WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
794192853Ssson
795192853Ssson	/*
796192853Ssson	 * Convert from a writer to a single reader.  First we handle
797192853Ssson	 * the easy case with no waiters.  If there are any waiters, we
798192853Ssson	 * lock the turnstile, "disown" the lock, and awaken any read
799192853Ssson	 * waiters.
800154941Sjhb	 */
801154941Sjhb	tid = (uintptr_t)curthread;
802154941Sjhb	if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
803154941Sjhb		goto out;
804154941Sjhb
805154941Sjhb	/*
806154941Sjhb	 * Ok, we think we have waiters, so lock the turnstile so we can
807154941Sjhb	 * read the waiter flags without any races.
808154941Sjhb	 */
809154941Sjhb	turnstile_chain_lock(&rw->lock_object);
810154941Sjhb	v = rw->rw_lock;
811154941Sjhb	MPASS(v & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS));
812154941Sjhb
813154941Sjhb	/*
814171052Sattilio	 * Downgrade from a write lock while preserving
815176017Sjeff	 * RW_LOCK_WRITE_WAITERS and give up ownership of the
816171052Sattilio	 * turnstile.  If there are any read waiters, wake them up.
817171052Sattilio	 */
818171052Sattilio	ts = turnstile_lookup(&rw->lock_object);
819171052Sattilio	MPASS(ts != NULL);
820171052Sattilio	if (v & RW_LOCK_READ_WAITERS)
821154941Sjhb		turnstile_broadcast(ts, TS_SHARED_QUEUE);
822154941Sjhb	atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) |
823154941Sjhb	    (v & RW_LOCK_WRITE_WAITERS));
824167787Sjhb	if (v & RW_LOCK_READ_WAITERS)
825154941Sjhb		turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
826154941Sjhb	else if (ts)
827170295Sjeff		turnstile_disown(ts);
828167787Sjhb	turnstile_chain_unlock(&rw->lock_object);
829154941Sjhbout:
830154941Sjhb	LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
831154941Sjhb}
832154941Sjhb
833154941Sjhb#ifdef INVARIANT_SUPPORT
834154941Sjhb#ifndef INVARIANTS
835154941Sjhb#undef _rw_assert
836154941Sjhb#endif
837154941Sjhb
838154941Sjhb/*
839154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least
840154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this*
841154941Sjhb * thread owns an rlock.
842154941Sjhb */
843154941Sjhbvoid
844154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line)
845154941Sjhb{
846154941Sjhb
847157846Sjhb	if (panicstr != NULL)
848176076Sjeff		return;
849176076Sjeff	switch (what) {
850176076Sjeff	case RA_LOCKED:
851176076Sjeff	case RA_LOCKED | RA_RECURSED:
852154941Sjhb	case RA_LOCKED | RA_NOTRECURSED:
853157846Sjhb	case RA_RLOCKED:
854157846Sjhb#ifdef WITNESS
855167787Sjhb		witness_assert(&rw->lock_object, what, file, line);
856154941Sjhb#else
857154941Sjhb		/*
858154941Sjhb		 * If some other thread has a write lock or we have one
859154941Sjhb		 * and are asserting a read lock, fail.  Also, if no one
860154941Sjhb		 * has a lock at all, fail.
861170295Sjeff		 */
862154941Sjhb		if (rw->rw_lock == RW_UNLOCKED ||
863154941Sjhb		    (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
864157882Sjhb		    rw_wowner(rw) != curthread)))
865157882Sjhb			panic("Lock %s not %slocked @ %s:%d\n",
866157882Sjhb			    rw->lock_object.lo_name, (what == RA_RLOCKED) ?
867157882Sjhb			    "read " : "", file, line);
868157882Sjhb
869157882Sjhb		if (!(rw->rw_lock & RW_LOCK_READ)) {
870157882Sjhb			if (rw_recursed(rw)) {
871157882Sjhb				if (what & RA_NOTRECURSED)
872176017Sjeff					panic("Lock %s recursed @ %s:%d\n",
873170295Sjeff					    rw->lock_object.lo_name, file,
874157882Sjhb					    line);
875157882Sjhb			} else if (what & RA_RECURSED)
876169394Sjhb				panic("Lock %s not recursed @ %s:%d\n",
877169394Sjhb				    rw->lock_object.lo_name, file, line);
878157882Sjhb		}
879157882Sjhb#endif
880157882Sjhb		break;
881157882Sjhb	case RA_WLOCKED:
882157882Sjhb	case RA_WLOCKED | RA_RECURSED:
883157882Sjhb	case RA_WLOCKED | RA_NOTRECURSED:
884157882Sjhb		if (rw_wowner(rw) != curthread)
885157882Sjhb			panic("Lock %s not exclusively locked @ %s:%d\n",
886157882Sjhb			    rw->lock_object.lo_name, file, line);
887157882Sjhb		if (rw_recursed(rw)) {
888176017Sjeff			if (what & RA_NOTRECURSED)
889176017Sjeff				panic("Lock %s recursed @ %s:%d\n",
890176017Sjeff				    rw->lock_object.lo_name, file, line);
891176017Sjeff		} else if (what & RA_RECURSED)
892176017Sjeff			panic("Lock %s not recursed @ %s:%d\n",
893176017Sjeff			    rw->lock_object.lo_name, file, line);
894176017Sjeff		break;
895176017Sjeff	case RA_UNLOCKED:
896176017Sjeff#ifdef WITNESS
897176017Sjeff		witness_assert(&rw->lock_object, what, file, line);
898176017Sjeff#else
899157882Sjhb		/*
900176017Sjeff		 * If we hold a write lock fail.  We can't reliably check
901176017Sjeff		 * to see if we hold a read lock or not.
902176017Sjeff		 */
903176017Sjeff		if (rw_wowner(rw) == curthread)
904176017Sjeff			panic("Lock %s exclusively locked @ %s:%d\n",
905176017Sjeff			    rw->lock_object.lo_name, file, line);
906176017Sjeff#endif
907176017Sjeff		break;
908176017Sjeff	default:
909176017Sjeff		panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
910176017Sjeff		    line);
911176017Sjeff	}
912176017Sjeff}
913176017Sjeff#endif /* INVARIANT_SUPPORT */
914176017Sjeff
915176017Sjeff#ifdef DDB
916176017Sjeffvoid
917176017Sjeffdb_show_rwlock(struct lock_object *lock)
918176017Sjeff{
919176017Sjeff	struct rwlock *rw;
920176017Sjeff	struct thread *td;
921176017Sjeff
922176017Sjeff	rw = (struct rwlock *)lock;
923176017Sjeff
924170295Sjeff	db_printf(" state: ");
925176017Sjeff	if (rw->rw_lock == RW_UNLOCKED)
926167787Sjhb		db_printf("UNLOCKED\n");
927176017Sjeff	else if (rw->rw_lock == RW_DESTROYED) {
928176017Sjeff		db_printf("DESTROYED\n");
929167787Sjhb		return;
930157882Sjhb	} else if (rw->rw_lock & RW_LOCK_READ)
931192853Ssson		db_printf("RLOCK: %ju locks\n",
932176017Sjeff		    (uintmax_t)(RW_READERS(rw->rw_lock)));
933157882Sjhb	else {
934157882Sjhb		td = rw_wowner(rw);
935157882Sjhb		db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
936157882Sjhb		    td->td_tid, td->td_proc->p_pid, td->td_name);
937157882Sjhb		if (rw_recursed(rw))
938157882Sjhb			db_printf(" recursed: %u\n", rw->rw_recurse);
939157882Sjhb	}
940157882Sjhb	db_printf(" waiters: ");
941157882Sjhb	switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
942157882Sjhb	case RW_LOCK_READ_WAITERS:
943157882Sjhb		db_printf("readers\n");
944176017Sjeff		break;
945157882Sjhb	case RW_LOCK_WRITE_WAITERS:
946169394Sjhb		db_printf("writers\n");
947169394Sjhb		break;
948171052Sattilio	case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
949171052Sattilio		db_printf("readers and writers\n");
950171052Sattilio		break;
951171052Sattilio	default:
952171052Sattilio		db_printf("none\n");
953157882Sjhb		break;
954167787Sjhb	}
955157882Sjhb}
956157882Sjhb
957157882Sjhb#endif
958157882Sjhb