kern_rwlock.c revision 185778
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 185778 2008-12-08 21:46:55Z kmacy $");
36154941Sjhb
37154941Sjhb#include "opt_ddb.h"
38167801Sjhb#include "opt_no_adaptive_rwlocks.h"
39154941Sjhb
40154941Sjhb#include <sys/param.h>
41154941Sjhb#include <sys/ktr.h>
42177912Sjeff#include <sys/kernel.h>
43154941Sjhb#include <sys/lock.h>
44154941Sjhb#include <sys/mutex.h>
45154941Sjhb#include <sys/proc.h>
46154941Sjhb#include <sys/rwlock.h>
47177912Sjeff#include <sys/sysctl.h>
48154941Sjhb#include <sys/systm.h>
49154941Sjhb#include <sys/turnstile.h>
50171516Sattilio
51154941Sjhb#include <machine/cpu.h>
52154941Sjhb
53171052SattilioCTASSERT((RW_RECURSE & LO_CLASSFLAGS) == RW_RECURSE);
54171052Sattilio
55167801Sjhb#if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
56167801Sjhb#define	ADAPTIVE_RWLOCKS
57167801Sjhb#endif
58167801Sjhb
59177912Sjeff#ifdef ADAPTIVE_RWLOCKS
60177912Sjeffstatic int rowner_retries = 10;
61177912Sjeffstatic int rowner_loops = 10000;
62177912SjeffSYSCTL_NODE(_debug, OID_AUTO, rwlock, CTLFLAG_RD, NULL, "rwlock debugging");
63177912SjeffSYSCTL_INT(_debug_rwlock, OID_AUTO, retry, CTLFLAG_RW, &rowner_retries, 0, "");
64177912SjeffSYSCTL_INT(_debug_rwlock, OID_AUTO, loops, CTLFLAG_RW, &rowner_loops, 0, "");
65177912Sjeff#endif
66177912Sjeff
67154941Sjhb#ifdef DDB
68154941Sjhb#include <ddb/ddb.h>
69154941Sjhb
70154941Sjhbstatic void	db_show_rwlock(struct lock_object *lock);
71154941Sjhb#endif
72173733Sattiliostatic void	assert_rw(struct lock_object *lock, int what);
73167368Sjhbstatic void	lock_rw(struct lock_object *lock, int how);
74167368Sjhbstatic int	unlock_rw(struct lock_object *lock);
75154941Sjhb
76154941Sjhbstruct lock_class lock_class_rw = {
77167365Sjhb	.lc_name = "rw",
78167365Sjhb	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
79173733Sattilio	.lc_assert = assert_rw,
80154941Sjhb#ifdef DDB
81167365Sjhb	.lc_ddb_show = db_show_rwlock,
82154941Sjhb#endif
83167368Sjhb	.lc_lock = lock_rw,
84167368Sjhb	.lc_unlock = unlock_rw,
85154941Sjhb};
86154941Sjhb
87157826Sjhb/*
88157826Sjhb * Return a pointer to the owning thread if the lock is write-locked or
89157826Sjhb * NULL if the lock is unlocked or read-locked.
90157826Sjhb */
91157826Sjhb#define	rw_wowner(rw)							\
92154941Sjhb	((rw)->rw_lock & RW_LOCK_READ ? NULL :				\
93154941Sjhb	    (struct thread *)RW_OWNER((rw)->rw_lock))
94154941Sjhb
95157826Sjhb/*
96171052Sattilio * Returns if a write owner is recursed.  Write ownership is not assured
97171052Sattilio * here and should be previously checked.
98171052Sattilio */
99171052Sattilio#define	rw_recursed(rw)		((rw)->rw_recurse != 0)
100171052Sattilio
101171052Sattilio/*
102171052Sattilio * Return true if curthread helds the lock.
103171052Sattilio */
104171052Sattilio#define	rw_wlocked(rw)		(rw_wowner((rw)) == curthread)
105171052Sattilio
106171052Sattilio/*
107157826Sjhb * Return a pointer to the owning thread for this lock who should receive
108157826Sjhb * any priority lent by threads that block on this lock.  Currently this
109157826Sjhb * is identical to rw_wowner().
110157826Sjhb */
111157826Sjhb#define	rw_owner(rw)		rw_wowner(rw)
112157826Sjhb
113154941Sjhb#ifndef INVARIANTS
114154941Sjhb#define	_rw_assert(rw, what, file, line)
115154941Sjhb#endif
116154941Sjhb
117154941Sjhbvoid
118173733Sattilioassert_rw(struct lock_object *lock, int what)
119173733Sattilio{
120173733Sattilio
121173733Sattilio	rw_assert((struct rwlock *)lock, what);
122173733Sattilio}
123173733Sattilio
124173733Sattiliovoid
125167368Sjhblock_rw(struct lock_object *lock, int how)
126167368Sjhb{
127167368Sjhb	struct rwlock *rw;
128167368Sjhb
129167368Sjhb	rw = (struct rwlock *)lock;
130167368Sjhb	if (how)
131167368Sjhb		rw_wlock(rw);
132167368Sjhb	else
133167368Sjhb		rw_rlock(rw);
134167368Sjhb}
135167368Sjhb
136167368Sjhbint
137167368Sjhbunlock_rw(struct lock_object *lock)
138167368Sjhb{
139167368Sjhb	struct rwlock *rw;
140167368Sjhb
141167368Sjhb	rw = (struct rwlock *)lock;
142167368Sjhb	rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
143167368Sjhb	if (rw->rw_lock & RW_LOCK_READ) {
144167368Sjhb		rw_runlock(rw);
145167368Sjhb		return (0);
146167368Sjhb	} else {
147167368Sjhb		rw_wunlock(rw);
148167368Sjhb		return (1);
149167368Sjhb	}
150167368Sjhb}
151167368Sjhb
152167368Sjhbvoid
153171052Sattiliorw_init_flags(struct rwlock *rw, const char *name, int opts)
154154941Sjhb{
155171052Sattilio	int flags;
156154941Sjhb
157171052Sattilio	MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
158171052Sattilio	    RW_RECURSE)) == 0);
159171052Sattilio
160171052Sattilio	flags = LO_UPGRADABLE | LO_RECURSABLE;
161171052Sattilio	if (opts & RW_DUPOK)
162171052Sattilio		flags |= LO_DUPOK;
163171052Sattilio	if (opts & RW_NOPROFILE)
164171052Sattilio		flags |= LO_NOPROFILE;
165171052Sattilio	if (!(opts & RW_NOWITNESS))
166171052Sattilio		flags |= LO_WITNESS;
167171052Sattilio	if (opts & RW_QUIET)
168171052Sattilio		flags |= LO_QUIET;
169171052Sattilio	flags |= opts & RW_RECURSE;
170171052Sattilio
171154941Sjhb	rw->rw_lock = RW_UNLOCKED;
172171052Sattilio	rw->rw_recurse = 0;
173171052Sattilio	lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
174154941Sjhb}
175154941Sjhb
176154941Sjhbvoid
177154941Sjhbrw_destroy(struct rwlock *rw)
178154941Sjhb{
179154941Sjhb
180154941Sjhb	KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked"));
181171052Sattilio	KASSERT(rw->rw_recurse == 0, ("rw lock still recursed"));
182169394Sjhb	rw->rw_lock = RW_DESTROYED;
183167787Sjhb	lock_destroy(&rw->lock_object);
184154941Sjhb}
185154941Sjhb
186154941Sjhbvoid
187154941Sjhbrw_sysinit(void *arg)
188154941Sjhb{
189154941Sjhb	struct rw_args *args = arg;
190154941Sjhb
191154941Sjhb	rw_init(args->ra_rw, args->ra_desc);
192154941Sjhb}
193154941Sjhb
194185778Skmacyvoid
195185778Skmacyrw_sysinit_flags(void *arg)
196185778Skmacy{
197185778Skmacy	struct rw_args_flags *args = arg;
198185778Skmacy
199185778Skmacy	rw_init_flags(args->ra_rw, args->ra_desc, args->ra_flags);
200185778Skmacy}
201185778Skmacy
202167024Srwatsonint
203167024Srwatsonrw_wowned(struct rwlock *rw)
204167024Srwatson{
205167024Srwatson
206167024Srwatson	return (rw_wowner(rw) == curthread);
207167024Srwatson}
208167024Srwatson
209154941Sjhbvoid
210154941Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line)
211154941Sjhb{
212154941Sjhb
213154941Sjhb	MPASS(curthread != NULL);
214169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
215169394Sjhb	    ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
216167787Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
217182914Sjhb	    line, NULL);
218154941Sjhb	__rw_wlock(rw, curthread, file, line);
219171052Sattilio	LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
220167787Sjhb	WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
221160771Sjhb	curthread->td_locks++;
222154941Sjhb}
223154941Sjhb
224177843Sattilioint
225177843Sattilio_rw_try_wlock(struct rwlock *rw, const char *file, int line)
226177843Sattilio{
227177843Sattilio	int rval;
228177843Sattilio
229177843Sattilio	KASSERT(rw->rw_lock != RW_DESTROYED,
230177843Sattilio	    ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
231177843Sattilio
232177843Sattilio	if (rw_wlocked(rw) && (rw->lock_object.lo_flags & RW_RECURSE) != 0) {
233177843Sattilio		rw->rw_recurse++;
234177843Sattilio		rval = 1;
235177843Sattilio	} else
236177843Sattilio		rval = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_UNLOCKED,
237177843Sattilio		    (uintptr_t)curthread);
238177843Sattilio
239177843Sattilio	LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
240177843Sattilio	if (rval) {
241177843Sattilio		WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
242177843Sattilio		    file, line);
243177843Sattilio		curthread->td_locks++;
244177843Sattilio	}
245177843Sattilio	return (rval);
246177843Sattilio}
247177843Sattilio
248154941Sjhbvoid
249154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line)
250154941Sjhb{
251154941Sjhb
252154941Sjhb	MPASS(curthread != NULL);
253169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
254169394Sjhb	    ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
255154941Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
256160771Sjhb	curthread->td_locks--;
257167787Sjhb	WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
258171052Sattilio	LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
259171052Sattilio	    line);
260171052Sattilio	if (!rw_recursed(rw))
261171052Sattilio		lock_profile_release_lock(&rw->lock_object);
262154941Sjhb	__rw_wunlock(rw, curthread, file, line);
263154941Sjhb}
264176017Sjeff/*
265176017Sjeff * Determines whether a new reader can acquire a lock.  Succeeds if the
266176017Sjeff * reader already owns a read lock and the lock is locked for read to
267176017Sjeff * prevent deadlock from reader recursion.  Also succeeds if the lock
268176017Sjeff * is unlocked and has no writer waiters or spinners.  Failing otherwise
269176017Sjeff * prioritizes writers before readers.
270176017Sjeff */
271176017Sjeff#define	RW_CAN_READ(_rw)						\
272176017Sjeff    ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) &	\
273176017Sjeff    (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) ==	\
274176017Sjeff    RW_LOCK_READ)
275154941Sjhb
276154941Sjhbvoid
277154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line)
278154941Sjhb{
279170295Sjeff	struct turnstile *ts;
280167801Sjhb#ifdef ADAPTIVE_RWLOCKS
281157846Sjhb	volatile struct thread *owner;
282177912Sjeff	int spintries = 0;
283177912Sjeff	int i;
284157851Swkoszek#endif
285167307Sjhb	uint64_t waittime = 0;
286167054Skmacy	int contested = 0;
287176017Sjeff	uintptr_t v;
288154941Sjhb
289169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
290169394Sjhb	    ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
291157826Sjhb	KASSERT(rw_wowner(rw) != curthread,
292154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
293167787Sjhb	    rw->lock_object.lo_name, file, line));
294182914Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line, NULL);
295154941Sjhb
296154941Sjhb	for (;;) {
297154941Sjhb		/*
298154941Sjhb		 * Handle the easy case.  If no other thread has a write
299154941Sjhb		 * lock, then try to bump up the count of read locks.  Note
300154941Sjhb		 * that we have to preserve the current state of the
301154941Sjhb		 * RW_LOCK_WRITE_WAITERS flag.  If we fail to acquire a
302154941Sjhb		 * read lock, then rw_lock must have changed, so restart
303154941Sjhb		 * the loop.  Note that this handles the case of a
304154941Sjhb		 * completely unlocked rwlock since such a lock is encoded
305154941Sjhb		 * as a read lock with no waiters.
306154941Sjhb		 */
307176017Sjeff		v = rw->rw_lock;
308176017Sjeff		if (RW_CAN_READ(v)) {
309154941Sjhb			/*
310154941Sjhb			 * The RW_LOCK_READ_WAITERS flag should only be set
311176017Sjeff			 * if the lock has been unlocked and write waiters
312176017Sjeff			 * were present.
313154941Sjhb			 */
314176017Sjeff			if (atomic_cmpset_acq_ptr(&rw->rw_lock, v,
315176017Sjeff			    v + RW_ONE_READER)) {
316167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
317154941Sjhb					CTR4(KTR_LOCK,
318154941Sjhb					    "%s: %p succeed %p -> %p", __func__,
319176017Sjeff					    rw, (void *)v,
320176017Sjeff					    (void *)(v + RW_ONE_READER));
321154941Sjhb				break;
322154941Sjhb			}
323157846Sjhb			cpu_spinwait();
324154941Sjhb			continue;
325154941Sjhb		}
326174629Sjeff		lock_profile_obtain_lock_failed(&rw->lock_object,
327174629Sjeff		    &contested, &waittime);
328154941Sjhb
329173960Sattilio#ifdef ADAPTIVE_RWLOCKS
330154941Sjhb		/*
331173960Sattilio		 * If the owner is running on another CPU, spin until
332173960Sattilio		 * the owner stops running or the state of the lock
333173960Sattilio		 * changes.
334173960Sattilio		 */
335176017Sjeff		if ((v & RW_LOCK_READ) == 0) {
336176017Sjeff			owner = (struct thread *)RW_OWNER(v);
337176017Sjeff			if (TD_IS_RUNNING(owner)) {
338176017Sjeff				if (LOCK_LOG_TEST(&rw->lock_object, 0))
339176017Sjeff					CTR3(KTR_LOCK,
340176017Sjeff					    "%s: spinning on %p held by %p",
341176017Sjeff					    __func__, rw, owner);
342176017Sjeff				while ((struct thread*)RW_OWNER(rw->rw_lock) ==
343176017Sjeff				    owner && TD_IS_RUNNING(owner))
344176017Sjeff					cpu_spinwait();
345176017Sjeff				continue;
346176017Sjeff			}
347177912Sjeff		} else if (spintries < rowner_retries) {
348177912Sjeff			spintries++;
349177912Sjeff			for (i = 0; i < rowner_loops; i++) {
350177912Sjeff				v = rw->rw_lock;
351177912Sjeff				if ((v & RW_LOCK_READ) == 0 || RW_CAN_READ(v))
352177912Sjeff					break;
353177912Sjeff				cpu_spinwait();
354177912Sjeff			}
355177912Sjeff			if (i != rowner_loops)
356177912Sjeff				continue;
357173960Sattilio		}
358173960Sattilio#endif
359173960Sattilio
360173960Sattilio		/*
361154941Sjhb		 * Okay, now it's the hard case.  Some other thread already
362176017Sjeff		 * has a write lock or there are write waiters present,
363176017Sjeff		 * acquire the turnstile lock so we can begin the process
364176017Sjeff		 * of blocking.
365154941Sjhb		 */
366170295Sjeff		ts = turnstile_trywait(&rw->lock_object);
367154941Sjhb
368154941Sjhb		/*
369154941Sjhb		 * The lock might have been released while we spun, so
370176017Sjeff		 * recheck its state and restart the loop if needed.
371154941Sjhb		 */
372176017Sjeff		v = rw->rw_lock;
373176017Sjeff		if (RW_CAN_READ(v)) {
374170295Sjeff			turnstile_cancel(ts);
375157846Sjhb			cpu_spinwait();
376154941Sjhb			continue;
377154941Sjhb		}
378154941Sjhb
379173960Sattilio#ifdef ADAPTIVE_RWLOCKS
380154941Sjhb		/*
381173960Sattilio		 * If the current owner of the lock is executing on another
382173960Sattilio		 * CPU quit the hard path and try to spin.
383173960Sattilio		 */
384176017Sjeff		if ((v & RW_LOCK_READ) == 0) {
385176017Sjeff			owner = (struct thread *)RW_OWNER(v);
386176017Sjeff			if (TD_IS_RUNNING(owner)) {
387176017Sjeff				turnstile_cancel(ts);
388176017Sjeff				cpu_spinwait();
389176017Sjeff				continue;
390176017Sjeff			}
391173960Sattilio		}
392173960Sattilio#endif
393173960Sattilio
394173960Sattilio		/*
395176017Sjeff		 * The lock is held in write mode or it already has waiters.
396154941Sjhb		 */
397176017Sjeff		MPASS(!RW_CAN_READ(v));
398176017Sjeff
399176017Sjeff		/*
400176017Sjeff		 * If the RW_LOCK_READ_WAITERS flag is already set, then
401176017Sjeff		 * we can go ahead and block.  If it is not set then try
402176017Sjeff		 * to set it.  If we fail to set it drop the turnstile
403176017Sjeff		 * lock and restart the loop.
404176017Sjeff		 */
405176017Sjeff		if (!(v & RW_LOCK_READ_WAITERS)) {
406176017Sjeff			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
407176017Sjeff			    v | RW_LOCK_READ_WAITERS)) {
408170295Sjeff				turnstile_cancel(ts);
409157826Sjhb				cpu_spinwait();
410157826Sjhb				continue;
411157826Sjhb			}
412167787Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
413157826Sjhb				CTR2(KTR_LOCK, "%s: %p set read waiters flag",
414157826Sjhb				    __func__, rw);
415154941Sjhb		}
416154941Sjhb
417154941Sjhb		/*
418154941Sjhb		 * We were unable to acquire the lock and the read waiters
419154941Sjhb		 * flag is set, so we must block on the turnstile.
420154941Sjhb		 */
421167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
422154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
423154941Sjhb			    rw);
424170295Sjeff		turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
425167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
426154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
427154941Sjhb			    __func__, rw);
428154941Sjhb	}
429154941Sjhb
430154941Sjhb	/*
431154941Sjhb	 * TODO: acquire "owner of record" here.  Here be turnstile dragons
432154941Sjhb	 * however.  turnstiles don't like owners changing between calls to
433154941Sjhb	 * turnstile_wait() currently.
434154941Sjhb	 */
435174629Sjeff	lock_profile_obtain_lock_success( &rw->lock_object, contested,
436174629Sjeff	    waittime, file, line);
437167787Sjhb	LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
438167787Sjhb	WITNESS_LOCK(&rw->lock_object, 0, file, line);
439160771Sjhb	curthread->td_locks++;
440176017Sjeff	curthread->td_rw_rlocks++;
441154941Sjhb}
442154941Sjhb
443177843Sattilioint
444177843Sattilio_rw_try_rlock(struct rwlock *rw, const char *file, int line)
445177843Sattilio{
446177843Sattilio	uintptr_t x;
447177843Sattilio
448177843Sattilio	for (;;) {
449177843Sattilio		x = rw->rw_lock;
450177843Sattilio		KASSERT(rw->rw_lock != RW_DESTROYED,
451177843Sattilio		    ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
452177843Sattilio		if (!(x & RW_LOCK_READ))
453177843Sattilio			break;
454177843Sattilio		if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, x + RW_ONE_READER)) {
455177843Sattilio			LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
456177843Sattilio			    line);
457177843Sattilio			WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
458177843Sattilio			curthread->td_locks++;
459177843Sattilio			curthread->td_rw_rlocks++;
460177843Sattilio			return (1);
461177843Sattilio		}
462177843Sattilio	}
463177843Sattilio
464177843Sattilio	LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
465177843Sattilio	return (0);
466177843Sattilio}
467177843Sattilio
468154941Sjhbvoid
469154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line)
470154941Sjhb{
471154941Sjhb	struct turnstile *ts;
472176017Sjeff	uintptr_t x, v, queue;
473154941Sjhb
474169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
475169394Sjhb	    ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
476154941Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
477160771Sjhb	curthread->td_locks--;
478176017Sjeff	curthread->td_rw_rlocks--;
479167787Sjhb	WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
480167787Sjhb	LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
481154941Sjhb
482154941Sjhb	/* TODO: drop "owner of record" here. */
483154941Sjhb
484154941Sjhb	for (;;) {
485154941Sjhb		/*
486154941Sjhb		 * See if there is more than one read lock held.  If so,
487154941Sjhb		 * just drop one and return.
488154941Sjhb		 */
489154941Sjhb		x = rw->rw_lock;
490154941Sjhb		if (RW_READERS(x) > 1) {
491154941Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, x,
492154941Sjhb			    x - RW_ONE_READER)) {
493167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
494154941Sjhb					CTR4(KTR_LOCK,
495154941Sjhb					    "%s: %p succeeded %p -> %p",
496154941Sjhb					    __func__, rw, (void *)x,
497154941Sjhb					    (void *)(x - RW_ONE_READER));
498154941Sjhb				break;
499154941Sjhb			}
500154941Sjhb			continue;
501167307Sjhb		}
502154941Sjhb		/*
503154941Sjhb		 * If there aren't any waiters for a write lock, then try
504154941Sjhb		 * to drop it quickly.
505154941Sjhb		 */
506176017Sjeff		if (!(x & RW_LOCK_WAITERS)) {
507176017Sjeff			MPASS((x & ~RW_LOCK_WRITE_SPINNER) ==
508176017Sjeff			    RW_READERS_LOCK(1));
509176017Sjeff			if (atomic_cmpset_ptr(&rw->rw_lock, x, RW_UNLOCKED)) {
510167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
511154941Sjhb					CTR2(KTR_LOCK, "%s: %p last succeeded",
512154941Sjhb					    __func__, rw);
513154941Sjhb				break;
514154941Sjhb			}
515154941Sjhb			continue;
516154941Sjhb		}
517154941Sjhb		/*
518176017Sjeff		 * Ok, we know we have waiters and we think we are the
519176017Sjeff		 * last reader, so grab the turnstile lock.
520154941Sjhb		 */
521170295Sjeff		turnstile_chain_lock(&rw->lock_object);
522176017Sjeff		v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
523176017Sjeff		MPASS(v & RW_LOCK_WAITERS);
524154941Sjhb
525154941Sjhb		/*
526154941Sjhb		 * Try to drop our lock leaving the lock in a unlocked
527154941Sjhb		 * state.
528154941Sjhb		 *
529154941Sjhb		 * If you wanted to do explicit lock handoff you'd have to
530154941Sjhb		 * do it here.  You'd also want to use turnstile_signal()
531154941Sjhb		 * and you'd have to handle the race where a higher
532154941Sjhb		 * priority thread blocks on the write lock before the
533154941Sjhb		 * thread you wakeup actually runs and have the new thread
534154941Sjhb		 * "steal" the lock.  For now it's a lot simpler to just
535154941Sjhb		 * wakeup all of the waiters.
536154941Sjhb		 *
537154941Sjhb		 * As above, if we fail, then another thread might have
538154941Sjhb		 * acquired a read lock, so drop the turnstile lock and
539154941Sjhb		 * restart.
540154941Sjhb		 */
541176017Sjeff		x = RW_UNLOCKED;
542176017Sjeff		if (v & RW_LOCK_WRITE_WAITERS) {
543176017Sjeff			queue = TS_EXCLUSIVE_QUEUE;
544176017Sjeff			x |= (v & RW_LOCK_READ_WAITERS);
545176017Sjeff		} else
546176017Sjeff			queue = TS_SHARED_QUEUE;
547176017Sjeff		if (!atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
548176017Sjeff		    x)) {
549170295Sjeff			turnstile_chain_unlock(&rw->lock_object);
550154941Sjhb			continue;
551154941Sjhb		}
552167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
553154941Sjhb			CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
554154941Sjhb			    __func__, rw);
555154941Sjhb
556154941Sjhb		/*
557154941Sjhb		 * Ok.  The lock is released and all that's left is to
558154941Sjhb		 * wake up the waiters.  Note that the lock might not be
559154941Sjhb		 * free anymore, but in that case the writers will just
560154941Sjhb		 * block again if they run before the new lock holder(s)
561154941Sjhb		 * release the lock.
562154941Sjhb		 */
563167787Sjhb		ts = turnstile_lookup(&rw->lock_object);
564157846Sjhb		MPASS(ts != NULL);
565176017Sjeff		turnstile_broadcast(ts, queue);
566154941Sjhb		turnstile_unpend(ts, TS_SHARED_LOCK);
567170295Sjeff		turnstile_chain_unlock(&rw->lock_object);
568154941Sjhb		break;
569154941Sjhb	}
570174629Sjeff	lock_profile_release_lock(&rw->lock_object);
571154941Sjhb}
572154941Sjhb
573154941Sjhb/*
574154941Sjhb * This function is called when we are unable to obtain a write lock on the
575154941Sjhb * first try.  This means that at least one other thread holds either a
576154941Sjhb * read or write lock.
577154941Sjhb */
578154941Sjhbvoid
579154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
580154941Sjhb{
581170295Sjeff	struct turnstile *ts;
582167801Sjhb#ifdef ADAPTIVE_RWLOCKS
583157846Sjhb	volatile struct thread *owner;
584176017Sjeff	int spintries = 0;
585176017Sjeff	int i;
586157851Swkoszek#endif
587171516Sattilio	uint64_t waittime = 0;
588176017Sjeff	uintptr_t v, x;
589171516Sattilio	int contested = 0;
590154941Sjhb
591171052Sattilio	if (rw_wlocked(rw)) {
592171052Sattilio		KASSERT(rw->lock_object.lo_flags & RW_RECURSE,
593171052Sattilio		    ("%s: recursing but non-recursive rw %s @ %s:%d\n",
594171052Sattilio		    __func__, rw->lock_object.lo_name, file, line));
595171052Sattilio		rw->rw_recurse++;
596171052Sattilio		if (LOCK_LOG_TEST(&rw->lock_object, 0))
597171052Sattilio			CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
598171052Sattilio		return;
599171052Sattilio	}
600171052Sattilio
601167787Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
602154941Sjhb		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
603167787Sjhb		    rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
604154941Sjhb
605154941Sjhb	while (!_rw_write_lock(rw, tid)) {
606174629Sjeff		lock_profile_obtain_lock_failed(&rw->lock_object,
607174629Sjeff		    &contested, &waittime);
608173960Sattilio#ifdef ADAPTIVE_RWLOCKS
609173960Sattilio		/*
610173960Sattilio		 * If the lock is write locked and the owner is
611173960Sattilio		 * running on another CPU, spin until the owner stops
612173960Sattilio		 * running or the state of the lock changes.
613173960Sattilio		 */
614173960Sattilio		v = rw->rw_lock;
615173960Sattilio		owner = (struct thread *)RW_OWNER(v);
616173960Sattilio		if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
617173960Sattilio			if (LOCK_LOG_TEST(&rw->lock_object, 0))
618173960Sattilio				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
619173960Sattilio				    __func__, rw, owner);
620173960Sattilio			while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
621173960Sattilio			    TD_IS_RUNNING(owner))
622173960Sattilio				cpu_spinwait();
623173960Sattilio			continue;
624173960Sattilio		}
625177912Sjeff		if ((v & RW_LOCK_READ) && RW_READERS(v) &&
626177912Sjeff		    spintries < rowner_retries) {
627176017Sjeff			if (!(v & RW_LOCK_WRITE_SPINNER)) {
628176017Sjeff				if (!atomic_cmpset_ptr(&rw->rw_lock, v,
629176017Sjeff				    v | RW_LOCK_WRITE_SPINNER)) {
630176017Sjeff					cpu_spinwait();
631176017Sjeff					continue;
632176017Sjeff				}
633176017Sjeff			}
634176017Sjeff			spintries++;
635177912Sjeff			for (i = 0; i < rowner_loops; i++) {
636176017Sjeff				if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0)
637176017Sjeff					break;
638176017Sjeff				cpu_spinwait();
639176017Sjeff			}
640177912Sjeff			if (i != rowner_loops)
641176017Sjeff				continue;
642176017Sjeff		}
643173960Sattilio#endif
644170295Sjeff		ts = turnstile_trywait(&rw->lock_object);
645154941Sjhb		v = rw->rw_lock;
646154941Sjhb
647173960Sattilio#ifdef ADAPTIVE_RWLOCKS
648154941Sjhb		/*
649173960Sattilio		 * If the current owner of the lock is executing on another
650173960Sattilio		 * CPU quit the hard path and try to spin.
651173960Sattilio		 */
652173960Sattilio		if (!(v & RW_LOCK_READ)) {
653173960Sattilio			owner = (struct thread *)RW_OWNER(v);
654173960Sattilio			if (TD_IS_RUNNING(owner)) {
655173960Sattilio				turnstile_cancel(ts);
656173960Sattilio				cpu_spinwait();
657173960Sattilio				continue;
658173960Sattilio			}
659173960Sattilio		}
660173960Sattilio#endif
661173960Sattilio		/*
662179334Sattilio		 * Check for the waiters flags about this rwlock.
663179334Sattilio		 * If the lock was released, without maintain any pending
664179334Sattilio		 * waiters queue, simply try to acquire it.
665179334Sattilio		 * If a pending waiters queue is present, claim the lock
666179334Sattilio		 * ownership and maintain the pending queue.
667154941Sjhb		 */
668176017Sjeff		x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
669176017Sjeff		if ((v & ~x) == RW_UNLOCKED) {
670176017Sjeff			x &= ~RW_LOCK_WRITE_SPINNER;
671176017Sjeff			if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) {
672176017Sjeff				if (x)
673176017Sjeff					turnstile_claim(ts);
674176017Sjeff				else
675176017Sjeff					turnstile_cancel(ts);
676154941Sjhb				break;
677154941Sjhb			}
678170295Sjeff			turnstile_cancel(ts);
679154941Sjhb			cpu_spinwait();
680154941Sjhb			continue;
681154941Sjhb		}
682154941Sjhb		/*
683154941Sjhb		 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
684154941Sjhb		 * set it.  If we fail to set it, then loop back and try
685154941Sjhb		 * again.
686154941Sjhb		 */
687157826Sjhb		if (!(v & RW_LOCK_WRITE_WAITERS)) {
688157826Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
689157826Sjhb			    v | RW_LOCK_WRITE_WAITERS)) {
690170295Sjeff				turnstile_cancel(ts);
691157826Sjhb				cpu_spinwait();
692157826Sjhb				continue;
693157826Sjhb			}
694167787Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
695157826Sjhb				CTR2(KTR_LOCK, "%s: %p set write waiters flag",
696157826Sjhb				    __func__, rw);
697154941Sjhb		}
698157846Sjhb		/*
699154941Sjhb		 * We were unable to acquire the lock and the write waiters
700154941Sjhb		 * flag is set, so we must block on the turnstile.
701154941Sjhb		 */
702167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
703154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
704154941Sjhb			    rw);
705170295Sjeff		turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
706167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
707154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
708154941Sjhb			    __func__, rw);
709176017Sjeff#ifdef ADAPTIVE_RWLOCKS
710176017Sjeff		spintries = 0;
711176017Sjeff#endif
712154941Sjhb	}
713171516Sattilio	lock_profile_obtain_lock_success(&rw->lock_object, contested, waittime,
714171516Sattilio	    file, line);
715154941Sjhb}
716154941Sjhb
717154941Sjhb/*
718154941Sjhb * This function is called if the first try at releasing a write lock failed.
719154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at
720154941Sjhb * least one thread is waiting on this lock.
721154941Sjhb */
722154941Sjhbvoid
723154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
724154941Sjhb{
725154941Sjhb	struct turnstile *ts;
726154941Sjhb	uintptr_t v;
727154941Sjhb	int queue;
728154941Sjhb
729171052Sattilio	if (rw_wlocked(rw) && rw_recursed(rw)) {
730176017Sjeff		rw->rw_recurse--;
731171052Sattilio		if (LOCK_LOG_TEST(&rw->lock_object, 0))
732171052Sattilio			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
733171052Sattilio		return;
734171052Sattilio	}
735176017Sjeff	v = rw->rw_lock;
736171052Sattilio
737154941Sjhb	KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
738154941Sjhb	    ("%s: neither of the waiter flags are set", __func__));
739154941Sjhb
740167787Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
741154941Sjhb		CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
742154941Sjhb
743170295Sjeff	turnstile_chain_lock(&rw->lock_object);
744167787Sjhb	ts = turnstile_lookup(&rw->lock_object);
745154941Sjhb	MPASS(ts != NULL);
746154941Sjhb
747154941Sjhb	/*
748154941Sjhb	 * Use the same algo as sx locks for now.  Prefer waking up shared
749154941Sjhb	 * waiters if we have any over writers.  This is probably not ideal.
750154941Sjhb	 *
751154941Sjhb	 * 'v' is the value we are going to write back to rw_lock.  If we
752154941Sjhb	 * have waiters on both queues, we need to preserve the state of
753154941Sjhb	 * the waiter flag for the queue we don't wake up.  For now this is
754154941Sjhb	 * hardcoded for the algorithm mentioned above.
755154941Sjhb	 *
756154941Sjhb	 * In the case of both readers and writers waiting we wakeup the
757154941Sjhb	 * readers but leave the RW_LOCK_WRITE_WAITERS flag set.  If a
758154941Sjhb	 * new writer comes in before a reader it will claim the lock up
759154941Sjhb	 * above.  There is probably a potential priority inversion in
760154941Sjhb	 * there that could be worked around either by waking both queues
761154941Sjhb	 * of waiters or doing some complicated lock handoff gymnastics.
762154941Sjhb	 */
763157846Sjhb	v = RW_UNLOCKED;
764176076Sjeff	if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) {
765176076Sjeff		queue = TS_EXCLUSIVE_QUEUE;
766176076Sjeff		v |= (rw->rw_lock & RW_LOCK_READ_WAITERS);
767176076Sjeff	} else
768154941Sjhb		queue = TS_SHARED_QUEUE;
769157846Sjhb
770157846Sjhb	/* Wake up all waiters for the specific queue. */
771167787Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
772154941Sjhb		CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
773154941Sjhb		    queue == TS_SHARED_QUEUE ? "read" : "write");
774154941Sjhb	turnstile_broadcast(ts, queue);
775154941Sjhb	atomic_store_rel_ptr(&rw->rw_lock, v);
776154941Sjhb	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
777170295Sjeff	turnstile_chain_unlock(&rw->lock_object);
778154941Sjhb}
779154941Sjhb
780157882Sjhb/*
781157882Sjhb * Attempt to do a non-blocking upgrade from a read lock to a write
782157882Sjhb * lock.  This will only succeed if this thread holds a single read
783157882Sjhb * lock.  Returns true if the upgrade succeeded and false otherwise.
784157882Sjhb */
785157882Sjhbint
786157882Sjhb_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
787157882Sjhb{
788176017Sjeff	uintptr_t v, x, tid;
789170295Sjeff	struct turnstile *ts;
790157882Sjhb	int success;
791157882Sjhb
792169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
793169394Sjhb	    ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
794157882Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
795157882Sjhb
796157882Sjhb	/*
797157882Sjhb	 * Attempt to switch from one reader to a writer.  If there
798157882Sjhb	 * are any write waiters, then we will have to lock the
799157882Sjhb	 * turnstile first to prevent races with another writer
800157882Sjhb	 * calling turnstile_wait() before we have claimed this
801157882Sjhb	 * turnstile.  So, do the simple case of no waiters first.
802157882Sjhb	 */
803157882Sjhb	tid = (uintptr_t)curthread;
804176017Sjeff	success = 0;
805176017Sjeff	for (;;) {
806176017Sjeff		v = rw->rw_lock;
807176017Sjeff		if (RW_READERS(v) > 1)
808176017Sjeff			break;
809176017Sjeff		if (!(v & RW_LOCK_WAITERS)) {
810176017Sjeff			success = atomic_cmpset_ptr(&rw->rw_lock, v, tid);
811176017Sjeff			if (!success)
812176017Sjeff				continue;
813176017Sjeff			break;
814176017Sjeff		}
815157882Sjhb
816176017Sjeff		/*
817176017Sjeff		 * Ok, we think we have waiters, so lock the turnstile.
818176017Sjeff		 */
819176017Sjeff		ts = turnstile_trywait(&rw->lock_object);
820176017Sjeff		v = rw->rw_lock;
821176017Sjeff		if (RW_READERS(v) > 1) {
822176017Sjeff			turnstile_cancel(ts);
823176017Sjeff			break;
824176017Sjeff		}
825176017Sjeff		/*
826176017Sjeff		 * Try to switch from one reader to a writer again.  This time
827176017Sjeff		 * we honor the current state of the waiters flags.
828176017Sjeff		 * If we obtain the lock with the flags set, then claim
829176017Sjeff		 * ownership of the turnstile.
830176017Sjeff		 */
831176017Sjeff		x = rw->rw_lock & RW_LOCK_WAITERS;
832176017Sjeff		success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x);
833176017Sjeff		if (success) {
834176017Sjeff			if (x)
835176017Sjeff				turnstile_claim(ts);
836176017Sjeff			else
837176017Sjeff				turnstile_cancel(ts);
838176017Sjeff			break;
839176017Sjeff		}
840170295Sjeff		turnstile_cancel(ts);
841176017Sjeff	}
842167787Sjhb	LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
843176017Sjeff	if (success) {
844176017Sjeff		curthread->td_rw_rlocks--;
845167787Sjhb		WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
846157882Sjhb		    file, line);
847176017Sjeff	}
848157882Sjhb	return (success);
849157882Sjhb}
850157882Sjhb
851157882Sjhb/*
852157882Sjhb * Downgrade a write lock into a single read lock.
853157882Sjhb */
854157882Sjhbvoid
855157882Sjhb_rw_downgrade(struct rwlock *rw, const char *file, int line)
856157882Sjhb{
857157882Sjhb	struct turnstile *ts;
858157882Sjhb	uintptr_t tid, v;
859176017Sjeff	int rwait, wwait;
860157882Sjhb
861169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
862169394Sjhb	    ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
863171052Sattilio	_rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
864171052Sattilio#ifndef INVARIANTS
865171052Sattilio	if (rw_recursed(rw))
866171052Sattilio		panic("downgrade of a recursed lock");
867171052Sattilio#endif
868157882Sjhb
869167787Sjhb	WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
870157882Sjhb
871157882Sjhb	/*
872157882Sjhb	 * Convert from a writer to a single reader.  First we handle
873157882Sjhb	 * the easy case with no waiters.  If there are any waiters, we
874176017Sjeff	 * lock the turnstile and "disown" the lock.
875157882Sjhb	 */
876157882Sjhb	tid = (uintptr_t)curthread;
877157882Sjhb	if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
878157882Sjhb		goto out;
879157882Sjhb
880157882Sjhb	/*
881157882Sjhb	 * Ok, we think we have waiters, so lock the turnstile so we can
882157882Sjhb	 * read the waiter flags without any races.
883157882Sjhb	 */
884170295Sjeff	turnstile_chain_lock(&rw->lock_object);
885176017Sjeff	v = rw->rw_lock & RW_LOCK_WAITERS;
886176017Sjeff	rwait = v & RW_LOCK_READ_WAITERS;
887176017Sjeff	wwait = v & RW_LOCK_WRITE_WAITERS;
888176017Sjeff	MPASS(rwait | wwait);
889157882Sjhb
890157882Sjhb	/*
891176017Sjeff	 * Downgrade from a write lock while preserving waiters flag
892176017Sjeff	 * and give up ownership of the turnstile.
893157882Sjhb	 */
894167787Sjhb	ts = turnstile_lookup(&rw->lock_object);
895157882Sjhb	MPASS(ts != NULL);
896176017Sjeff	if (!wwait)
897176017Sjeff		v &= ~RW_LOCK_READ_WAITERS;
898176017Sjeff	atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
899176017Sjeff	/*
900176017Sjeff	 * Wake other readers if there are no writers pending.  Otherwise they
901176017Sjeff	 * won't be able to acquire the lock anyway.
902176017Sjeff	 */
903176017Sjeff	if (rwait && !wwait) {
904157882Sjhb		turnstile_broadcast(ts, TS_SHARED_QUEUE);
905157882Sjhb		turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
906176017Sjeff	} else
907157882Sjhb		turnstile_disown(ts);
908170295Sjeff	turnstile_chain_unlock(&rw->lock_object);
909157882Sjhbout:
910176017Sjeff	curthread->td_rw_rlocks++;
911167787Sjhb	LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
912157882Sjhb}
913157882Sjhb
914154941Sjhb#ifdef INVARIANT_SUPPORT
915155162Sscottl#ifndef INVARIANTS
916154941Sjhb#undef _rw_assert
917154941Sjhb#endif
918154941Sjhb
919154941Sjhb/*
920154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least
921154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this*
922154941Sjhb * thread owns an rlock.
923154941Sjhb */
924154941Sjhbvoid
925154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line)
926154941Sjhb{
927154941Sjhb
928154941Sjhb	if (panicstr != NULL)
929154941Sjhb		return;
930154941Sjhb	switch (what) {
931154941Sjhb	case RA_LOCKED:
932171052Sattilio	case RA_LOCKED | RA_RECURSED:
933171052Sattilio	case RA_LOCKED | RA_NOTRECURSED:
934154941Sjhb	case RA_RLOCKED:
935154941Sjhb#ifdef WITNESS
936167787Sjhb		witness_assert(&rw->lock_object, what, file, line);
937154941Sjhb#else
938154941Sjhb		/*
939154941Sjhb		 * If some other thread has a write lock or we have one
940154941Sjhb		 * and are asserting a read lock, fail.  Also, if no one
941154941Sjhb		 * has a lock at all, fail.
942154941Sjhb		 */
943155061Sscottl		if (rw->rw_lock == RW_UNLOCKED ||
944155061Sscottl		    (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
945157826Sjhb		    rw_wowner(rw) != curthread)))
946154941Sjhb			panic("Lock %s not %slocked @ %s:%d\n",
947167787Sjhb			    rw->lock_object.lo_name, (what == RA_RLOCKED) ?
948154941Sjhb			    "read " : "", file, line);
949171052Sattilio
950171052Sattilio		if (!(rw->rw_lock & RW_LOCK_READ)) {
951171052Sattilio			if (rw_recursed(rw)) {
952171052Sattilio				if (what & RA_NOTRECURSED)
953171052Sattilio					panic("Lock %s recursed @ %s:%d\n",
954171052Sattilio					    rw->lock_object.lo_name, file,
955171052Sattilio					    line);
956171052Sattilio			} else if (what & RA_RECURSED)
957171052Sattilio				panic("Lock %s not recursed @ %s:%d\n",
958171052Sattilio				    rw->lock_object.lo_name, file, line);
959171052Sattilio		}
960154941Sjhb#endif
961154941Sjhb		break;
962154941Sjhb	case RA_WLOCKED:
963171052Sattilio	case RA_WLOCKED | RA_RECURSED:
964171052Sattilio	case RA_WLOCKED | RA_NOTRECURSED:
965157826Sjhb		if (rw_wowner(rw) != curthread)
966154941Sjhb			panic("Lock %s not exclusively locked @ %s:%d\n",
967167787Sjhb			    rw->lock_object.lo_name, file, line);
968171052Sattilio		if (rw_recursed(rw)) {
969171052Sattilio			if (what & RA_NOTRECURSED)
970171052Sattilio				panic("Lock %s recursed @ %s:%d\n",
971171052Sattilio				    rw->lock_object.lo_name, file, line);
972171052Sattilio		} else if (what & RA_RECURSED)
973171052Sattilio			panic("Lock %s not recursed @ %s:%d\n",
974171052Sattilio			    rw->lock_object.lo_name, file, line);
975154941Sjhb		break;
976154941Sjhb	case RA_UNLOCKED:
977154941Sjhb#ifdef WITNESS
978167787Sjhb		witness_assert(&rw->lock_object, what, file, line);
979154941Sjhb#else
980154941Sjhb		/*
981154941Sjhb		 * If we hold a write lock fail.  We can't reliably check
982154941Sjhb		 * to see if we hold a read lock or not.
983154941Sjhb		 */
984157826Sjhb		if (rw_wowner(rw) == curthread)
985154941Sjhb			panic("Lock %s exclusively locked @ %s:%d\n",
986167787Sjhb			    rw->lock_object.lo_name, file, line);
987154941Sjhb#endif
988154941Sjhb		break;
989154941Sjhb	default:
990154941Sjhb		panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
991154941Sjhb		    line);
992154941Sjhb	}
993154941Sjhb}
994154941Sjhb#endif /* INVARIANT_SUPPORT */
995154941Sjhb
996154941Sjhb#ifdef DDB
997154941Sjhbvoid
998154941Sjhbdb_show_rwlock(struct lock_object *lock)
999154941Sjhb{
1000154941Sjhb	struct rwlock *rw;
1001154941Sjhb	struct thread *td;
1002154941Sjhb
1003154941Sjhb	rw = (struct rwlock *)lock;
1004154941Sjhb
1005154941Sjhb	db_printf(" state: ");
1006154941Sjhb	if (rw->rw_lock == RW_UNLOCKED)
1007154941Sjhb		db_printf("UNLOCKED\n");
1008169394Sjhb	else if (rw->rw_lock == RW_DESTROYED) {
1009169394Sjhb		db_printf("DESTROYED\n");
1010169394Sjhb		return;
1011169394Sjhb	} else if (rw->rw_lock & RW_LOCK_READ)
1012167504Sjhb		db_printf("RLOCK: %ju locks\n",
1013167504Sjhb		    (uintmax_t)(RW_READERS(rw->rw_lock)));
1014154941Sjhb	else {
1015157826Sjhb		td = rw_wowner(rw);
1016154941Sjhb		db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1017173600Sjulian		    td->td_tid, td->td_proc->p_pid, td->td_name);
1018171052Sattilio		if (rw_recursed(rw))
1019171052Sattilio			db_printf(" recursed: %u\n", rw->rw_recurse);
1020154941Sjhb	}
1021154941Sjhb	db_printf(" waiters: ");
1022154941Sjhb	switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
1023154941Sjhb	case RW_LOCK_READ_WAITERS:
1024154941Sjhb		db_printf("readers\n");
1025154941Sjhb		break;
1026154941Sjhb	case RW_LOCK_WRITE_WAITERS:
1027154941Sjhb		db_printf("writers\n");
1028154941Sjhb		break;
1029154941Sjhb	case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
1030167492Sjhb		db_printf("readers and writers\n");
1031154941Sjhb		break;
1032154941Sjhb	default:
1033154941Sjhb		db_printf("none\n");
1034154941Sjhb		break;
1035154941Sjhb	}
1036154941Sjhb}
1037154941Sjhb
1038154941Sjhb#endif
1039