kern_rwlock.c revision 173600
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 173600 2007-11-14 06:21:24Z julian $");
36154941Sjhb
37154941Sjhb#include "opt_ddb.h"
38167801Sjhb#include "opt_no_adaptive_rwlocks.h"
39154941Sjhb
40154941Sjhb#include <sys/param.h>
41154941Sjhb#include <sys/ktr.h>
42154941Sjhb#include <sys/lock.h>
43154941Sjhb#include <sys/mutex.h>
44154941Sjhb#include <sys/proc.h>
45154941Sjhb#include <sys/rwlock.h>
46154941Sjhb#include <sys/systm.h>
47154941Sjhb#include <sys/turnstile.h>
48171516Sattilio
49154941Sjhb#include <machine/cpu.h>
50154941Sjhb
51171052SattilioCTASSERT((RW_RECURSE & LO_CLASSFLAGS) == RW_RECURSE);
52171052Sattilio
53167801Sjhb#if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
54167801Sjhb#define	ADAPTIVE_RWLOCKS
55167801Sjhb#endif
56167801Sjhb
57154941Sjhb#ifdef DDB
58154941Sjhb#include <ddb/ddb.h>
59154941Sjhb
60154941Sjhbstatic void	db_show_rwlock(struct lock_object *lock);
61154941Sjhb#endif
62167368Sjhbstatic void	lock_rw(struct lock_object *lock, int how);
63167368Sjhbstatic int	unlock_rw(struct lock_object *lock);
64154941Sjhb
65154941Sjhbstruct lock_class lock_class_rw = {
66167365Sjhb	.lc_name = "rw",
67167365Sjhb	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
68154941Sjhb#ifdef DDB
69167365Sjhb	.lc_ddb_show = db_show_rwlock,
70154941Sjhb#endif
71167368Sjhb	.lc_lock = lock_rw,
72167368Sjhb	.lc_unlock = unlock_rw,
73154941Sjhb};
74154941Sjhb
75157826Sjhb/*
76157826Sjhb * Return a pointer to the owning thread if the lock is write-locked or
77157826Sjhb * NULL if the lock is unlocked or read-locked.
78157826Sjhb */
79157826Sjhb#define	rw_wowner(rw)							\
80154941Sjhb	((rw)->rw_lock & RW_LOCK_READ ? NULL :				\
81154941Sjhb	    (struct thread *)RW_OWNER((rw)->rw_lock))
82154941Sjhb
83157826Sjhb/*
84171052Sattilio * Returns if a write owner is recursed.  Write ownership is not assured
85171052Sattilio * here and should be previously checked.
86171052Sattilio */
87171052Sattilio#define	rw_recursed(rw)		((rw)->rw_recurse != 0)
88171052Sattilio
89171052Sattilio/*
90171052Sattilio * Return true if curthread helds the lock.
91171052Sattilio */
92171052Sattilio#define	rw_wlocked(rw)		(rw_wowner((rw)) == curthread)
93171052Sattilio
94171052Sattilio/*
95157826Sjhb * Return a pointer to the owning thread for this lock who should receive
96157826Sjhb * any priority lent by threads that block on this lock.  Currently this
97157826Sjhb * is identical to rw_wowner().
98157826Sjhb */
99157826Sjhb#define	rw_owner(rw)		rw_wowner(rw)
100157826Sjhb
101154941Sjhb#ifndef INVARIANTS
102154941Sjhb#define	_rw_assert(rw, what, file, line)
103154941Sjhb#endif
104154941Sjhb
105154941Sjhbvoid
106167368Sjhblock_rw(struct lock_object *lock, int how)
107167368Sjhb{
108167368Sjhb	struct rwlock *rw;
109167368Sjhb
110167368Sjhb	rw = (struct rwlock *)lock;
111167368Sjhb	if (how)
112167368Sjhb		rw_wlock(rw);
113167368Sjhb	else
114167368Sjhb		rw_rlock(rw);
115167368Sjhb}
116167368Sjhb
117167368Sjhbint
118167368Sjhbunlock_rw(struct lock_object *lock)
119167368Sjhb{
120167368Sjhb	struct rwlock *rw;
121167368Sjhb
122167368Sjhb	rw = (struct rwlock *)lock;
123167368Sjhb	rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
124167368Sjhb	if (rw->rw_lock & RW_LOCK_READ) {
125167368Sjhb		rw_runlock(rw);
126167368Sjhb		return (0);
127167368Sjhb	} else {
128167368Sjhb		rw_wunlock(rw);
129167368Sjhb		return (1);
130167368Sjhb	}
131167368Sjhb}
132167368Sjhb
133167368Sjhbvoid
134171052Sattiliorw_init_flags(struct rwlock *rw, const char *name, int opts)
135154941Sjhb{
136171052Sattilio	int flags;
137154941Sjhb
138171052Sattilio	MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
139171052Sattilio	    RW_RECURSE)) == 0);
140171052Sattilio
141171052Sattilio	flags = LO_UPGRADABLE | LO_RECURSABLE;
142171052Sattilio	if (opts & RW_DUPOK)
143171052Sattilio		flags |= LO_DUPOK;
144171052Sattilio	if (opts & RW_NOPROFILE)
145171052Sattilio		flags |= LO_NOPROFILE;
146171052Sattilio	if (!(opts & RW_NOWITNESS))
147171052Sattilio		flags |= LO_WITNESS;
148171052Sattilio	if (opts & RW_QUIET)
149171052Sattilio		flags |= LO_QUIET;
150171052Sattilio	flags |= opts & RW_RECURSE;
151171052Sattilio
152154941Sjhb	rw->rw_lock = RW_UNLOCKED;
153171052Sattilio	rw->rw_recurse = 0;
154171052Sattilio	lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
155154941Sjhb}
156154941Sjhb
157154941Sjhbvoid
158154941Sjhbrw_destroy(struct rwlock *rw)
159154941Sjhb{
160154941Sjhb
161154941Sjhb	KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked"));
162171052Sattilio	KASSERT(rw->rw_recurse == 0, ("rw lock still recursed"));
163169394Sjhb	rw->rw_lock = RW_DESTROYED;
164167787Sjhb	lock_destroy(&rw->lock_object);
165154941Sjhb}
166154941Sjhb
167154941Sjhbvoid
168154941Sjhbrw_sysinit(void *arg)
169154941Sjhb{
170154941Sjhb	struct rw_args *args = arg;
171154941Sjhb
172154941Sjhb	rw_init(args->ra_rw, args->ra_desc);
173154941Sjhb}
174154941Sjhb
175167024Srwatsonint
176167024Srwatsonrw_wowned(struct rwlock *rw)
177167024Srwatson{
178167024Srwatson
179167024Srwatson	return (rw_wowner(rw) == curthread);
180167024Srwatson}
181167024Srwatson
182154941Sjhbvoid
183154941Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line)
184154941Sjhb{
185154941Sjhb
186154941Sjhb	MPASS(curthread != NULL);
187169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
188169394Sjhb	    ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
189157826Sjhb	KASSERT(rw_wowner(rw) != curthread,
190154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
191167787Sjhb	    rw->lock_object.lo_name, file, line));
192167787Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
193154941Sjhb	    line);
194154941Sjhb	__rw_wlock(rw, curthread, file, line);
195171052Sattilio	LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
196167787Sjhb	WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
197160771Sjhb	curthread->td_locks++;
198154941Sjhb}
199154941Sjhb
200154941Sjhbvoid
201154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line)
202154941Sjhb{
203154941Sjhb
204154941Sjhb	MPASS(curthread != NULL);
205169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
206169394Sjhb	    ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
207154941Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
208160771Sjhb	curthread->td_locks--;
209167787Sjhb	WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
210171052Sattilio	LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
211171052Sattilio	    line);
212171052Sattilio	if (!rw_recursed(rw))
213171052Sattilio		lock_profile_release_lock(&rw->lock_object);
214154941Sjhb	__rw_wunlock(rw, curthread, file, line);
215154941Sjhb}
216154941Sjhb
217154941Sjhbvoid
218154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line)
219154941Sjhb{
220170295Sjeff	struct turnstile *ts;
221167801Sjhb#ifdef ADAPTIVE_RWLOCKS
222157846Sjhb	volatile struct thread *owner;
223157851Swkoszek#endif
224171516Sattilio#ifdef LOCK_PROFILING_SHARED
225167307Sjhb	uint64_t waittime = 0;
226167054Skmacy	int contested = 0;
227171516Sattilio#endif
228154941Sjhb	uintptr_t x;
229154941Sjhb
230169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
231169394Sjhb	    ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
232157826Sjhb	KASSERT(rw_wowner(rw) != curthread,
233154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
234167787Sjhb	    rw->lock_object.lo_name, file, line));
235167787Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line);
236154941Sjhb
237154941Sjhb	/*
238154941Sjhb	 * Note that we don't make any attempt to try to block read
239154941Sjhb	 * locks once a writer has blocked on the lock.  The reason is
240154941Sjhb	 * that we currently allow for read locks to recurse and we
241154941Sjhb	 * don't keep track of all the holders of read locks.  Thus, if
242154941Sjhb	 * we were to block readers once a writer blocked and a reader
243154941Sjhb	 * tried to recurse on their reader lock after a writer had
244154941Sjhb	 * blocked we would end up in a deadlock since the reader would
245154941Sjhb	 * be blocked on the writer, and the writer would be blocked
246154941Sjhb	 * waiting for the reader to release its original read lock.
247154941Sjhb	 */
248154941Sjhb	for (;;) {
249154941Sjhb		/*
250154941Sjhb		 * Handle the easy case.  If no other thread has a write
251154941Sjhb		 * lock, then try to bump up the count of read locks.  Note
252154941Sjhb		 * that we have to preserve the current state of the
253154941Sjhb		 * RW_LOCK_WRITE_WAITERS flag.  If we fail to acquire a
254154941Sjhb		 * read lock, then rw_lock must have changed, so restart
255154941Sjhb		 * the loop.  Note that this handles the case of a
256154941Sjhb		 * completely unlocked rwlock since such a lock is encoded
257154941Sjhb		 * as a read lock with no waiters.
258154941Sjhb		 */
259154941Sjhb		x = rw->rw_lock;
260154941Sjhb		if (x & RW_LOCK_READ) {
261154941Sjhb
262154941Sjhb			/*
263154941Sjhb			 * The RW_LOCK_READ_WAITERS flag should only be set
264154941Sjhb			 * if another thread currently holds a write lock,
265154941Sjhb			 * and in that case RW_LOCK_READ should be clear.
266154941Sjhb			 */
267154941Sjhb			MPASS((x & RW_LOCK_READ_WAITERS) == 0);
268154941Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock, x,
269154941Sjhb			    x + RW_ONE_READER)) {
270171516Sattilio#ifdef LOCK_PROFILING_SHARED
271171516Sattilio				if (RW_READERS(x) == 0)
272171516Sattilio					lock_profile_obtain_lock_success(
273171516Sattilio					    &rw->lock_object, contested,
274171516Sattilio					    waittime, file, line);
275171516Sattilio#endif
276167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
277154941Sjhb					CTR4(KTR_LOCK,
278154941Sjhb					    "%s: %p succeed %p -> %p", __func__,
279154941Sjhb					    rw, (void *)x,
280154941Sjhb					    (void *)(x + RW_ONE_READER));
281154941Sjhb				break;
282154941Sjhb			}
283157846Sjhb			cpu_spinwait();
284154941Sjhb			continue;
285154941Sjhb		}
286154941Sjhb
287154941Sjhb		/*
288154941Sjhb		 * Okay, now it's the hard case.  Some other thread already
289154941Sjhb		 * has a write lock, so acquire the turnstile lock so we can
290154941Sjhb		 * begin the process of blocking.
291154941Sjhb		 */
292170295Sjeff		ts = turnstile_trywait(&rw->lock_object);
293154941Sjhb
294154941Sjhb		/*
295154941Sjhb		 * The lock might have been released while we spun, so
296154941Sjhb		 * recheck its state and restart the loop if there is no
297154941Sjhb		 * longer a write lock.
298154941Sjhb		 */
299154941Sjhb		x = rw->rw_lock;
300154941Sjhb		if (x & RW_LOCK_READ) {
301170295Sjeff			turnstile_cancel(ts);
302157846Sjhb			cpu_spinwait();
303154941Sjhb			continue;
304154941Sjhb		}
305154941Sjhb
306154941Sjhb		/*
307154941Sjhb		 * Ok, it's still a write lock.  If the RW_LOCK_READ_WAITERS
308154941Sjhb		 * flag is already set, then we can go ahead and block.  If
309154941Sjhb		 * it is not set then try to set it.  If we fail to set it
310154941Sjhb		 * drop the turnstile lock and restart the loop.
311154941Sjhb		 */
312157826Sjhb		if (!(x & RW_LOCK_READ_WAITERS)) {
313157826Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, x,
314157826Sjhb			    x | RW_LOCK_READ_WAITERS)) {
315170295Sjeff				turnstile_cancel(ts);
316157826Sjhb				cpu_spinwait();
317157826Sjhb				continue;
318157826Sjhb			}
319167787Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
320157826Sjhb				CTR2(KTR_LOCK, "%s: %p set read waiters flag",
321157826Sjhb				    __func__, rw);
322154941Sjhb		}
323154941Sjhb
324167801Sjhb#ifdef ADAPTIVE_RWLOCKS
325154941Sjhb		/*
326157846Sjhb		 * If the owner is running on another CPU, spin until
327157846Sjhb		 * the owner stops running or the state of the lock
328157846Sjhb		 * changes.
329157846Sjhb		 */
330157846Sjhb		owner = (struct thread *)RW_OWNER(x);
331157846Sjhb		if (TD_IS_RUNNING(owner)) {
332170295Sjeff			turnstile_cancel(ts);
333167787Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
334157846Sjhb				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
335157846Sjhb				    __func__, rw, owner);
336171516Sattilio#ifdef LOCK_PROFILING_SHARED
337171516Sattilio			lock_profile_obtain_lock_failed(&rw->lock_object,
338171516Sattilio			    &contested, &waittime);
339171516Sattilio#endif
340157846Sjhb			while ((struct thread*)RW_OWNER(rw->rw_lock)== owner &&
341157846Sjhb			    TD_IS_RUNNING(owner))
342157846Sjhb				cpu_spinwait();
343157846Sjhb			continue;
344157846Sjhb		}
345157846Sjhb#endif
346157846Sjhb
347157846Sjhb		/*
348154941Sjhb		 * We were unable to acquire the lock and the read waiters
349154941Sjhb		 * flag is set, so we must block on the turnstile.
350154941Sjhb		 */
351167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
352154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
353154941Sjhb			    rw);
354171516Sattilio#ifdef LOCK_PROFILING_SHARED
355171516Sattilio		lock_profile_obtain_lock_failed(&rw->lock_object, &contested,
356171516Sattilio		    &waittime);
357171516Sattilio#endif
358170295Sjeff		turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
359167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
360154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
361154941Sjhb			    __func__, rw);
362154941Sjhb	}
363154941Sjhb
364154941Sjhb	/*
365154941Sjhb	 * TODO: acquire "owner of record" here.  Here be turnstile dragons
366154941Sjhb	 * however.  turnstiles don't like owners changing between calls to
367154941Sjhb	 * turnstile_wait() currently.
368154941Sjhb	 */
369154941Sjhb
370167787Sjhb	LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
371167787Sjhb	WITNESS_LOCK(&rw->lock_object, 0, file, line);
372160771Sjhb	curthread->td_locks++;
373154941Sjhb}
374154941Sjhb
375154941Sjhbvoid
376154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line)
377154941Sjhb{
378154941Sjhb	struct turnstile *ts;
379154941Sjhb	uintptr_t x;
380154941Sjhb
381169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
382169394Sjhb	    ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
383154941Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
384160771Sjhb	curthread->td_locks--;
385167787Sjhb	WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
386167787Sjhb	LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
387154941Sjhb
388154941Sjhb	/* TODO: drop "owner of record" here. */
389154941Sjhb
390154941Sjhb	for (;;) {
391154941Sjhb		/*
392154941Sjhb		 * See if there is more than one read lock held.  If so,
393154941Sjhb		 * just drop one and return.
394154941Sjhb		 */
395154941Sjhb		x = rw->rw_lock;
396154941Sjhb		if (RW_READERS(x) > 1) {
397154941Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, x,
398154941Sjhb			    x - RW_ONE_READER)) {
399167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
400154941Sjhb					CTR4(KTR_LOCK,
401154941Sjhb					    "%s: %p succeeded %p -> %p",
402154941Sjhb					    __func__, rw, (void *)x,
403154941Sjhb					    (void *)(x - RW_ONE_READER));
404154941Sjhb				break;
405154941Sjhb			}
406154941Sjhb			continue;
407167307Sjhb		}
408154941Sjhb
409164159Skmacy
410154941Sjhb		/*
411154941Sjhb		 * We should never have read waiters while at least one
412154941Sjhb		 * thread holds a read lock.  (See note above)
413154941Sjhb		 */
414154941Sjhb		KASSERT(!(x & RW_LOCK_READ_WAITERS),
415154941Sjhb		    ("%s: waiting readers", __func__));
416171516Sattilio#ifdef LOCK_PROFILING_SHARED
417171516Sattilio		lock_profile_release_lock(&rw->lock_object);
418171516Sattilio#endif
419154941Sjhb
420154941Sjhb		/*
421154941Sjhb		 * If there aren't any waiters for a write lock, then try
422154941Sjhb		 * to drop it quickly.
423154941Sjhb		 */
424154941Sjhb		if (!(x & RW_LOCK_WRITE_WAITERS)) {
425154941Sjhb
426154941Sjhb			/*
427154941Sjhb			 * There shouldn't be any flags set and we should
428154941Sjhb			 * be the only read lock.  If we fail to release
429154941Sjhb			 * the single read lock, then another thread might
430154941Sjhb			 * have just acquired a read lock, so go back up
431154941Sjhb			 * to the multiple read locks case.
432154941Sjhb			 */
433154941Sjhb			MPASS(x == RW_READERS_LOCK(1));
434154941Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1),
435154941Sjhb			    RW_UNLOCKED)) {
436167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
437154941Sjhb					CTR2(KTR_LOCK, "%s: %p last succeeded",
438154941Sjhb					    __func__, rw);
439154941Sjhb				break;
440154941Sjhb			}
441154941Sjhb			continue;
442154941Sjhb		}
443154941Sjhb
444154941Sjhb		/*
445154941Sjhb		 * There should just be one reader with one or more
446154941Sjhb		 * writers waiting.
447154941Sjhb		 */
448154941Sjhb		MPASS(x == (RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS));
449154941Sjhb
450154941Sjhb		/*
451154941Sjhb		 * Ok, we know we have a waiting writer and we think we
452154941Sjhb		 * are the last reader, so grab the turnstile lock.
453154941Sjhb		 */
454170295Sjeff		turnstile_chain_lock(&rw->lock_object);
455154941Sjhb
456154941Sjhb		/*
457154941Sjhb		 * Try to drop our lock leaving the lock in a unlocked
458154941Sjhb		 * state.
459154941Sjhb		 *
460154941Sjhb		 * If you wanted to do explicit lock handoff you'd have to
461154941Sjhb		 * do it here.  You'd also want to use turnstile_signal()
462154941Sjhb		 * and you'd have to handle the race where a higher
463154941Sjhb		 * priority thread blocks on the write lock before the
464154941Sjhb		 * thread you wakeup actually runs and have the new thread
465154941Sjhb		 * "steal" the lock.  For now it's a lot simpler to just
466154941Sjhb		 * wakeup all of the waiters.
467154941Sjhb		 *
468154941Sjhb		 * As above, if we fail, then another thread might have
469154941Sjhb		 * acquired a read lock, so drop the turnstile lock and
470154941Sjhb		 * restart.
471154941Sjhb		 */
472154941Sjhb		if (!atomic_cmpset_ptr(&rw->rw_lock,
473154941Sjhb		    RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS, RW_UNLOCKED)) {
474170295Sjeff			turnstile_chain_unlock(&rw->lock_object);
475154941Sjhb			continue;
476154941Sjhb		}
477167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
478154941Sjhb			CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
479154941Sjhb			    __func__, rw);
480154941Sjhb
481154941Sjhb		/*
482154941Sjhb		 * Ok.  The lock is released and all that's left is to
483154941Sjhb		 * wake up the waiters.  Note that the lock might not be
484154941Sjhb		 * free anymore, but in that case the writers will just
485154941Sjhb		 * block again if they run before the new lock holder(s)
486154941Sjhb		 * release the lock.
487154941Sjhb		 */
488167787Sjhb		ts = turnstile_lookup(&rw->lock_object);
489157846Sjhb		MPASS(ts != NULL);
490154941Sjhb		turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
491154941Sjhb		turnstile_unpend(ts, TS_SHARED_LOCK);
492170295Sjeff		turnstile_chain_unlock(&rw->lock_object);
493154941Sjhb		break;
494154941Sjhb	}
495154941Sjhb}
496154941Sjhb
497154941Sjhb/*
498154941Sjhb * This function is called when we are unable to obtain a write lock on the
499154941Sjhb * first try.  This means that at least one other thread holds either a
500154941Sjhb * read or write lock.
501154941Sjhb */
502154941Sjhbvoid
503154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
504154941Sjhb{
505170295Sjeff	struct turnstile *ts;
506167801Sjhb#ifdef ADAPTIVE_RWLOCKS
507157846Sjhb	volatile struct thread *owner;
508157851Swkoszek#endif
509171516Sattilio	uint64_t waittime = 0;
510154941Sjhb	uintptr_t v;
511171516Sattilio	int contested = 0;
512154941Sjhb
513171052Sattilio	if (rw_wlocked(rw)) {
514171052Sattilio		KASSERT(rw->lock_object.lo_flags & RW_RECURSE,
515171052Sattilio		    ("%s: recursing but non-recursive rw %s @ %s:%d\n",
516171052Sattilio		    __func__, rw->lock_object.lo_name, file, line));
517171052Sattilio		rw->rw_recurse++;
518171052Sattilio		atomic_set_ptr(&rw->rw_lock, RW_LOCK_RECURSED);
519171052Sattilio		if (LOCK_LOG_TEST(&rw->lock_object, 0))
520171052Sattilio			CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
521171052Sattilio		return;
522171052Sattilio	}
523171052Sattilio
524167787Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
525154941Sjhb		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
526167787Sjhb		    rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
527154941Sjhb
528154941Sjhb	while (!_rw_write_lock(rw, tid)) {
529170295Sjeff		ts = turnstile_trywait(&rw->lock_object);
530154941Sjhb		v = rw->rw_lock;
531154941Sjhb
532154941Sjhb		/*
533154941Sjhb		 * If the lock was released while spinning on the
534154941Sjhb		 * turnstile chain lock, try again.
535154941Sjhb		 */
536154941Sjhb		if (v == RW_UNLOCKED) {
537170295Sjeff			turnstile_cancel(ts);
538154941Sjhb			cpu_spinwait();
539154941Sjhb			continue;
540154941Sjhb		}
541154941Sjhb
542154941Sjhb		/*
543154941Sjhb		 * If the lock was released by a writer with both readers
544154941Sjhb		 * and writers waiting and a reader hasn't woken up and
545154941Sjhb		 * acquired the lock yet, rw_lock will be set to the
546154941Sjhb		 * value RW_UNLOCKED | RW_LOCK_WRITE_WAITERS.  If we see
547154941Sjhb		 * that value, try to acquire it once.  Note that we have
548154941Sjhb		 * to preserve the RW_LOCK_WRITE_WAITERS flag as there are
549168073Sjhb		 * other writers waiting still.  If we fail, restart the
550154941Sjhb		 * loop.
551154941Sjhb		 */
552154941Sjhb		if (v == (RW_UNLOCKED | RW_LOCK_WRITE_WAITERS)) {
553154941Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock,
554154941Sjhb			    RW_UNLOCKED | RW_LOCK_WRITE_WAITERS,
555154941Sjhb			    tid | RW_LOCK_WRITE_WAITERS)) {
556170295Sjeff				turnstile_claim(ts);
557154941Sjhb				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
558154941Sjhb				    __func__, rw);
559154941Sjhb				break;
560154941Sjhb			}
561170295Sjeff			turnstile_cancel(ts);
562154941Sjhb			cpu_spinwait();
563154941Sjhb			continue;
564154941Sjhb		}
565154941Sjhb
566154941Sjhb		/*
567154941Sjhb		 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
568154941Sjhb		 * set it.  If we fail to set it, then loop back and try
569154941Sjhb		 * again.
570154941Sjhb		 */
571157826Sjhb		if (!(v & RW_LOCK_WRITE_WAITERS)) {
572157826Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
573157826Sjhb			    v | RW_LOCK_WRITE_WAITERS)) {
574170295Sjeff				turnstile_cancel(ts);
575157826Sjhb				cpu_spinwait();
576157826Sjhb				continue;
577157826Sjhb			}
578167787Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
579157826Sjhb				CTR2(KTR_LOCK, "%s: %p set write waiters flag",
580157826Sjhb				    __func__, rw);
581154941Sjhb		}
582154941Sjhb
583167801Sjhb#ifdef ADAPTIVE_RWLOCKS
584157846Sjhb		/*
585157846Sjhb		 * If the lock is write locked and the owner is
586157846Sjhb		 * running on another CPU, spin until the owner stops
587157846Sjhb		 * running or the state of the lock changes.
588157846Sjhb		 */
589157846Sjhb		owner = (struct thread *)RW_OWNER(v);
590157846Sjhb		if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
591170295Sjeff			turnstile_cancel(ts);
592167787Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
593157846Sjhb				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
594157846Sjhb				    __func__, rw, owner);
595171516Sattilio			lock_profile_obtain_lock_failed(&rw->lock_object,
596171516Sattilio			    &contested, &waittime);
597157846Sjhb			while ((struct thread*)RW_OWNER(rw->rw_lock)== owner &&
598157846Sjhb			    TD_IS_RUNNING(owner))
599157846Sjhb				cpu_spinwait();
600157846Sjhb			continue;
601157846Sjhb		}
602157846Sjhb#endif
603154941Sjhb
604154941Sjhb		/*
605154941Sjhb		 * We were unable to acquire the lock and the write waiters
606154941Sjhb		 * flag is set, so we must block on the turnstile.
607154941Sjhb		 */
608167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
609154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
610154941Sjhb			    rw);
611171516Sattilio		lock_profile_obtain_lock_failed(&rw->lock_object, &contested,
612171516Sattilio		    &waittime);
613170295Sjeff		turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
614167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
615154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
616154941Sjhb			    __func__, rw);
617154941Sjhb	}
618171516Sattilio	lock_profile_obtain_lock_success(&rw->lock_object, contested, waittime,
619171516Sattilio	    file, line);
620154941Sjhb}
621154941Sjhb
622154941Sjhb/*
623154941Sjhb * This function is called if the first try at releasing a write lock failed.
624154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at
625154941Sjhb * least one thread is waiting on this lock.
626154941Sjhb */
627154941Sjhbvoid
628154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
629154941Sjhb{
630154941Sjhb	struct turnstile *ts;
631154941Sjhb	uintptr_t v;
632154941Sjhb	int queue;
633154941Sjhb
634171052Sattilio	if (rw_wlocked(rw) && rw_recursed(rw)) {
635171052Sattilio		if ((--rw->rw_recurse) == 0)
636171052Sattilio			atomic_clear_ptr(&rw->rw_lock, RW_LOCK_RECURSED);
637171052Sattilio		if (LOCK_LOG_TEST(&rw->lock_object, 0))
638171052Sattilio			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
639171052Sattilio		return;
640171052Sattilio	}
641171052Sattilio
642154941Sjhb	KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
643154941Sjhb	    ("%s: neither of the waiter flags are set", __func__));
644154941Sjhb
645167787Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
646154941Sjhb		CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
647154941Sjhb
648170295Sjeff	turnstile_chain_lock(&rw->lock_object);
649167787Sjhb	ts = turnstile_lookup(&rw->lock_object);
650154941Sjhb
651167801Sjhb#ifdef ADAPTIVE_RWLOCKS
652157846Sjhb	/*
653157846Sjhb	 * There might not be a turnstile for this lock if all of
654157846Sjhb	 * the waiters are adaptively spinning.  In that case, just
655157846Sjhb	 * reset the lock to the unlocked state and return.
656157846Sjhb	 */
657157846Sjhb	if (ts == NULL) {
658157846Sjhb		atomic_store_rel_ptr(&rw->rw_lock, RW_UNLOCKED);
659167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
660157846Sjhb			CTR2(KTR_LOCK, "%s: %p no sleepers", __func__, rw);
661170295Sjeff		turnstile_chain_unlock(&rw->lock_object);
662157846Sjhb		return;
663157846Sjhb	}
664157846Sjhb#else
665154941Sjhb	MPASS(ts != NULL);
666157846Sjhb#endif
667154941Sjhb
668154941Sjhb	/*
669154941Sjhb	 * Use the same algo as sx locks for now.  Prefer waking up shared
670154941Sjhb	 * waiters if we have any over writers.  This is probably not ideal.
671154941Sjhb	 *
672154941Sjhb	 * 'v' is the value we are going to write back to rw_lock.  If we
673154941Sjhb	 * have waiters on both queues, we need to preserve the state of
674154941Sjhb	 * the waiter flag for the queue we don't wake up.  For now this is
675154941Sjhb	 * hardcoded for the algorithm mentioned above.
676154941Sjhb	 *
677154941Sjhb	 * In the case of both readers and writers waiting we wakeup the
678154941Sjhb	 * readers but leave the RW_LOCK_WRITE_WAITERS flag set.  If a
679154941Sjhb	 * new writer comes in before a reader it will claim the lock up
680154941Sjhb	 * above.  There is probably a potential priority inversion in
681154941Sjhb	 * there that could be worked around either by waking both queues
682154941Sjhb	 * of waiters or doing some complicated lock handoff gymnastics.
683157846Sjhb	 *
684167801Sjhb	 * Note that in the ADAPTIVE_RWLOCKS case, if both flags are
685167801Sjhb	 * set, there might not be any actual writers on the turnstile
686167801Sjhb	 * as they might all be spinning.  In that case, we don't want
687167801Sjhb	 * to preserve the RW_LOCK_WRITE_WAITERS flag as the turnstile
688167801Sjhb	 * is going to go away once we wakeup all the readers.
689154941Sjhb	 */
690157846Sjhb	v = RW_UNLOCKED;
691154941Sjhb	if (rw->rw_lock & RW_LOCK_READ_WAITERS) {
692154941Sjhb		queue = TS_SHARED_QUEUE;
693167801Sjhb#ifdef ADAPTIVE_RWLOCKS
694157846Sjhb		if (rw->rw_lock & RW_LOCK_WRITE_WAITERS &&
695157846Sjhb		    !turnstile_empty(ts, TS_EXCLUSIVE_QUEUE))
696157846Sjhb			v |= RW_LOCK_WRITE_WAITERS;
697157846Sjhb#else
698157846Sjhb		v |= (rw->rw_lock & RW_LOCK_WRITE_WAITERS);
699157846Sjhb#endif
700157846Sjhb	} else
701154941Sjhb		queue = TS_EXCLUSIVE_QUEUE;
702157846Sjhb
703167801Sjhb#ifdef ADAPTIVE_RWLOCKS
704157846Sjhb	/*
705157846Sjhb	 * We have to make sure that we actually have waiters to
706157846Sjhb	 * wakeup.  If they are all spinning, then we just need to
707157846Sjhb	 * disown the turnstile and return.
708157846Sjhb	 */
709157846Sjhb	if (turnstile_empty(ts, queue)) {
710167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
711157846Sjhb			CTR2(KTR_LOCK, "%s: %p no sleepers 2", __func__, rw);
712157846Sjhb		atomic_store_rel_ptr(&rw->rw_lock, v);
713157846Sjhb		turnstile_disown(ts);
714170295Sjeff		turnstile_chain_unlock(&rw->lock_object);
715157846Sjhb		return;
716154941Sjhb	}
717157846Sjhb#endif
718157846Sjhb
719157846Sjhb	/* Wake up all waiters for the specific queue. */
720167787Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
721154941Sjhb		CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
722154941Sjhb		    queue == TS_SHARED_QUEUE ? "read" : "write");
723154941Sjhb	turnstile_broadcast(ts, queue);
724154941Sjhb	atomic_store_rel_ptr(&rw->rw_lock, v);
725154941Sjhb	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
726170295Sjeff	turnstile_chain_unlock(&rw->lock_object);
727154941Sjhb}
728154941Sjhb
729157882Sjhb/*
730157882Sjhb * Attempt to do a non-blocking upgrade from a read lock to a write
731157882Sjhb * lock.  This will only succeed if this thread holds a single read
732157882Sjhb * lock.  Returns true if the upgrade succeeded and false otherwise.
733157882Sjhb */
734157882Sjhbint
735157882Sjhb_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
736157882Sjhb{
737157882Sjhb	uintptr_t v, tid;
738170295Sjeff	struct turnstile *ts;
739157882Sjhb	int success;
740157882Sjhb
741169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
742169394Sjhb	    ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
743157882Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
744157882Sjhb
745157882Sjhb	/*
746157882Sjhb	 * Attempt to switch from one reader to a writer.  If there
747157882Sjhb	 * are any write waiters, then we will have to lock the
748157882Sjhb	 * turnstile first to prevent races with another writer
749157882Sjhb	 * calling turnstile_wait() before we have claimed this
750157882Sjhb	 * turnstile.  So, do the simple case of no waiters first.
751157882Sjhb	 */
752157882Sjhb	tid = (uintptr_t)curthread;
753157882Sjhb	if (!(rw->rw_lock & RW_LOCK_WRITE_WAITERS)) {
754168073Sjhb		success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1),
755168073Sjhb		    tid);
756157882Sjhb		goto out;
757157882Sjhb	}
758157882Sjhb
759157882Sjhb	/*
760157882Sjhb	 * Ok, we think we have write waiters, so lock the
761157882Sjhb	 * turnstile.
762157882Sjhb	 */
763170295Sjeff	ts = turnstile_trywait(&rw->lock_object);
764157882Sjhb
765157882Sjhb	/*
766157882Sjhb	 * Try to switch from one reader to a writer again.  This time
767157882Sjhb	 * we honor the current state of the RW_LOCK_WRITE_WAITERS
768157882Sjhb	 * flag.  If we obtain the lock with the flag set, then claim
769167801Sjhb	 * ownership of the turnstile.  In the ADAPTIVE_RWLOCKS case
770167801Sjhb	 * it is possible for there to not be an associated turnstile
771167801Sjhb	 * even though there are waiters if all of the waiters are
772167801Sjhb	 * spinning.
773157882Sjhb	 */
774157882Sjhb	v = rw->rw_lock & RW_LOCK_WRITE_WAITERS;
775168073Sjhb	success = atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
776157882Sjhb	    tid | v);
777167801Sjhb#ifdef ADAPTIVE_RWLOCKS
778167787Sjhb	if (success && v && turnstile_lookup(&rw->lock_object) != NULL)
779157882Sjhb#else
780157882Sjhb	if (success && v)
781157882Sjhb#endif
782170295Sjeff		turnstile_claim(ts);
783157882Sjhb	else
784170295Sjeff		turnstile_cancel(ts);
785157882Sjhbout:
786167787Sjhb	LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
787157882Sjhb	if (success)
788167787Sjhb		WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
789157882Sjhb		    file, line);
790157882Sjhb	return (success);
791157882Sjhb}
792157882Sjhb
793157882Sjhb/*
794157882Sjhb * Downgrade a write lock into a single read lock.
795157882Sjhb */
796157882Sjhbvoid
797157882Sjhb_rw_downgrade(struct rwlock *rw, const char *file, int line)
798157882Sjhb{
799157882Sjhb	struct turnstile *ts;
800157882Sjhb	uintptr_t tid, v;
801157882Sjhb
802169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
803169394Sjhb	    ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
804171052Sattilio	_rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
805171052Sattilio#ifndef INVARIANTS
806171052Sattilio	if (rw_recursed(rw))
807171052Sattilio		panic("downgrade of a recursed lock");
808171052Sattilio#endif
809157882Sjhb
810167787Sjhb	WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
811157882Sjhb
812157882Sjhb	/*
813157882Sjhb	 * Convert from a writer to a single reader.  First we handle
814157882Sjhb	 * the easy case with no waiters.  If there are any waiters, we
815157882Sjhb	 * lock the turnstile, "disown" the lock, and awaken any read
816157882Sjhb	 * waiters.
817157882Sjhb	 */
818157882Sjhb	tid = (uintptr_t)curthread;
819157882Sjhb	if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
820157882Sjhb		goto out;
821157882Sjhb
822157882Sjhb	/*
823157882Sjhb	 * Ok, we think we have waiters, so lock the turnstile so we can
824157882Sjhb	 * read the waiter flags without any races.
825157882Sjhb	 */
826170295Sjeff	turnstile_chain_lock(&rw->lock_object);
827157882Sjhb	v = rw->rw_lock;
828157882Sjhb	MPASS(v & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS));
829157882Sjhb
830157882Sjhb	/*
831157882Sjhb	 * Downgrade from a write lock while preserving
832157882Sjhb	 * RW_LOCK_WRITE_WAITERS and give up ownership of the
833157882Sjhb	 * turnstile.  If there are any read waiters, wake them up.
834157882Sjhb	 *
835167801Sjhb	 * For ADAPTIVE_RWLOCKS, we have to allow for the fact that
836167801Sjhb	 * all of the read waiters might be spinning.  In that case,
837167801Sjhb	 * act as if RW_LOCK_READ_WAITERS is not set.  Also, only
838167801Sjhb	 * preserve the RW_LOCK_WRITE_WAITERS flag if at least one
839167801Sjhb	 * writer is blocked on the turnstile.
840157882Sjhb	 */
841167787Sjhb	ts = turnstile_lookup(&rw->lock_object);
842167801Sjhb#ifdef ADAPTIVE_RWLOCKS
843157882Sjhb	if (ts == NULL)
844157882Sjhb		v &= ~(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS);
845157882Sjhb	else if (v & RW_LOCK_READ_WAITERS &&
846157882Sjhb	    turnstile_empty(ts, TS_SHARED_QUEUE))
847157882Sjhb		v &= ~RW_LOCK_READ_WAITERS;
848157882Sjhb	else if (v & RW_LOCK_WRITE_WAITERS &&
849157882Sjhb	    turnstile_empty(ts, TS_EXCLUSIVE_QUEUE))
850157882Sjhb		v &= ~RW_LOCK_WRITE_WAITERS;
851157882Sjhb#else
852157882Sjhb	MPASS(ts != NULL);
853157882Sjhb#endif
854157882Sjhb	if (v & RW_LOCK_READ_WAITERS)
855157882Sjhb		turnstile_broadcast(ts, TS_SHARED_QUEUE);
856157882Sjhb	atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) |
857157882Sjhb	    (v & RW_LOCK_WRITE_WAITERS));
858157882Sjhb	if (v & RW_LOCK_READ_WAITERS)
859157882Sjhb		turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
860170295Sjeff	else if (ts)
861157882Sjhb		turnstile_disown(ts);
862170295Sjeff	turnstile_chain_unlock(&rw->lock_object);
863157882Sjhbout:
864167787Sjhb	LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
865157882Sjhb}
866157882Sjhb
867154941Sjhb#ifdef INVARIANT_SUPPORT
868155162Sscottl#ifndef INVARIANTS
869154941Sjhb#undef _rw_assert
870154941Sjhb#endif
871154941Sjhb
872154941Sjhb/*
873154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least
874154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this*
875154941Sjhb * thread owns an rlock.
876154941Sjhb */
877154941Sjhbvoid
878154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line)
879154941Sjhb{
880154941Sjhb
881154941Sjhb	if (panicstr != NULL)
882154941Sjhb		return;
883154941Sjhb	switch (what) {
884154941Sjhb	case RA_LOCKED:
885171052Sattilio	case RA_LOCKED | RA_RECURSED:
886171052Sattilio	case RA_LOCKED | RA_NOTRECURSED:
887154941Sjhb	case RA_RLOCKED:
888154941Sjhb#ifdef WITNESS
889167787Sjhb		witness_assert(&rw->lock_object, what, file, line);
890154941Sjhb#else
891154941Sjhb		/*
892154941Sjhb		 * If some other thread has a write lock or we have one
893154941Sjhb		 * and are asserting a read lock, fail.  Also, if no one
894154941Sjhb		 * has a lock at all, fail.
895154941Sjhb		 */
896155061Sscottl		if (rw->rw_lock == RW_UNLOCKED ||
897155061Sscottl		    (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
898157826Sjhb		    rw_wowner(rw) != curthread)))
899154941Sjhb			panic("Lock %s not %slocked @ %s:%d\n",
900167787Sjhb			    rw->lock_object.lo_name, (what == RA_RLOCKED) ?
901154941Sjhb			    "read " : "", file, line);
902171052Sattilio
903171052Sattilio		if (!(rw->rw_lock & RW_LOCK_READ)) {
904171052Sattilio			if (rw_recursed(rw)) {
905171052Sattilio				if (what & RA_NOTRECURSED)
906171052Sattilio					panic("Lock %s recursed @ %s:%d\n",
907171052Sattilio					    rw->lock_object.lo_name, file,
908171052Sattilio					    line);
909171052Sattilio			} else if (what & RA_RECURSED)
910171052Sattilio				panic("Lock %s not recursed @ %s:%d\n",
911171052Sattilio				    rw->lock_object.lo_name, file, line);
912171052Sattilio		}
913154941Sjhb#endif
914154941Sjhb		break;
915154941Sjhb	case RA_WLOCKED:
916171052Sattilio	case RA_WLOCKED | RA_RECURSED:
917171052Sattilio	case RA_WLOCKED | RA_NOTRECURSED:
918157826Sjhb		if (rw_wowner(rw) != curthread)
919154941Sjhb			panic("Lock %s not exclusively locked @ %s:%d\n",
920167787Sjhb			    rw->lock_object.lo_name, file, line);
921171052Sattilio		if (rw_recursed(rw)) {
922171052Sattilio			if (what & RA_NOTRECURSED)
923171052Sattilio				panic("Lock %s recursed @ %s:%d\n",
924171052Sattilio				    rw->lock_object.lo_name, file, line);
925171052Sattilio		} else if (what & RA_RECURSED)
926171052Sattilio			panic("Lock %s not recursed @ %s:%d\n",
927171052Sattilio			    rw->lock_object.lo_name, file, line);
928154941Sjhb		break;
929154941Sjhb	case RA_UNLOCKED:
930154941Sjhb#ifdef WITNESS
931167787Sjhb		witness_assert(&rw->lock_object, what, file, line);
932154941Sjhb#else
933154941Sjhb		/*
934154941Sjhb		 * If we hold a write lock fail.  We can't reliably check
935154941Sjhb		 * to see if we hold a read lock or not.
936154941Sjhb		 */
937157826Sjhb		if (rw_wowner(rw) == curthread)
938154941Sjhb			panic("Lock %s exclusively locked @ %s:%d\n",
939167787Sjhb			    rw->lock_object.lo_name, file, line);
940154941Sjhb#endif
941154941Sjhb		break;
942154941Sjhb	default:
943154941Sjhb		panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
944154941Sjhb		    line);
945154941Sjhb	}
946154941Sjhb}
947154941Sjhb#endif /* INVARIANT_SUPPORT */
948154941Sjhb
949154941Sjhb#ifdef DDB
950154941Sjhbvoid
951154941Sjhbdb_show_rwlock(struct lock_object *lock)
952154941Sjhb{
953154941Sjhb	struct rwlock *rw;
954154941Sjhb	struct thread *td;
955154941Sjhb
956154941Sjhb	rw = (struct rwlock *)lock;
957154941Sjhb
958154941Sjhb	db_printf(" state: ");
959154941Sjhb	if (rw->rw_lock == RW_UNLOCKED)
960154941Sjhb		db_printf("UNLOCKED\n");
961169394Sjhb	else if (rw->rw_lock == RW_DESTROYED) {
962169394Sjhb		db_printf("DESTROYED\n");
963169394Sjhb		return;
964169394Sjhb	} else if (rw->rw_lock & RW_LOCK_READ)
965167504Sjhb		db_printf("RLOCK: %ju locks\n",
966167504Sjhb		    (uintmax_t)(RW_READERS(rw->rw_lock)));
967154941Sjhb	else {
968157826Sjhb		td = rw_wowner(rw);
969154941Sjhb		db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
970173600Sjulian		    td->td_tid, td->td_proc->p_pid, td->td_name);
971171052Sattilio		if (rw_recursed(rw))
972171052Sattilio			db_printf(" recursed: %u\n", rw->rw_recurse);
973154941Sjhb	}
974154941Sjhb	db_printf(" waiters: ");
975154941Sjhb	switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
976154941Sjhb	case RW_LOCK_READ_WAITERS:
977154941Sjhb		db_printf("readers\n");
978154941Sjhb		break;
979154941Sjhb	case RW_LOCK_WRITE_WAITERS:
980154941Sjhb		db_printf("writers\n");
981154941Sjhb		break;
982154941Sjhb	case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
983167492Sjhb		db_printf("readers and writers\n");
984154941Sjhb		break;
985154941Sjhb	default:
986154941Sjhb		db_printf("none\n");
987154941Sjhb		break;
988154941Sjhb	}
989154941Sjhb}
990154941Sjhb
991154941Sjhb#endif
992