kern_rwlock.c revision 227588
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 227588 2011-11-16 21:51:17Z pjd $");
36154941Sjhb
37154941Sjhb#include "opt_ddb.h"
38154941Sjhb#include "opt_kdtrace.h"
39154941Sjhb#include "opt_no_adaptive_rwlocks.h"
40154941Sjhb
41154941Sjhb#include <sys/param.h>
42154941Sjhb#include <sys/ktr.h>
43154941Sjhb#include <sys/kernel.h>
44154941Sjhb#include <sys/lock.h>
45154941Sjhb#include <sys/mutex.h>
46154941Sjhb#include <sys/proc.h>
47164159Skmacy#include <sys/rwlock.h>
48154941Sjhb#include <sys/sysctl.h>
49154941Sjhb#include <sys/systm.h>
50154941Sjhb#include <sys/turnstile.h>
51154941Sjhb
52154941Sjhb#include <machine/cpu.h>
53154941Sjhb
54154941Sjhb#if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
55167368Sjhb#define	ADAPTIVE_RWLOCKS
56167368Sjhb#endif
57154941Sjhb
58154941Sjhb#ifdef ADAPTIVE_RWLOCKS
59167365Sjhbstatic int rowner_retries = 10;
60167365Sjhbstatic int rowner_loops = 10000;
61154941Sjhbstatic SYSCTL_NODE(_debug, OID_AUTO, rwlock, CTLFLAG_RD, NULL,
62167365Sjhb    "rwlock debugging");
63154941SjhbSYSCTL_INT(_debug_rwlock, OID_AUTO, retry, CTLFLAG_RW, &rowner_retries, 0, "");
64167368SjhbSYSCTL_INT(_debug_rwlock, OID_AUTO, loops, CTLFLAG_RW, &rowner_loops, 0, "");
65167368Sjhb#endif
66154941Sjhb
67154941Sjhb#ifdef DDB
68157826Sjhb#include <ddb/ddb.h>
69157826Sjhb
70157826Sjhbstatic void	db_show_rwlock(const struct lock_object *lock);
71157826Sjhb#endif
72157826Sjhbstatic void	assert_rw(const struct lock_object *lock, int what);
73154941Sjhbstatic void	lock_rw(struct lock_object *lock, int how);
74154941Sjhb#ifdef KDTRACE_HOOKS
75154941Sjhbstatic int	owner_rw(const struct lock_object *lock, struct thread **owner);
76157826Sjhb#endif
77157826Sjhbstatic int	unlock_rw(struct lock_object *lock);
78157826Sjhb
79157826Sjhbstruct lock_class lock_class_rw = {
80157826Sjhb	.lc_name = "rw",
81157826Sjhb	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
82157826Sjhb	.lc_assert = assert_rw,
83154941Sjhb#ifdef DDB
84154941Sjhb	.lc_ddb_show = db_show_rwlock,
85154941Sjhb#endif
86154941Sjhb	.lc_lock = lock_rw,
87154941Sjhb	.lc_unlock = unlock_rw,
88167368Sjhb#ifdef KDTRACE_HOOKS
89167368Sjhb	.lc_owner = owner_rw,
90167368Sjhb#endif
91167368Sjhb};
92167368Sjhb
93167368Sjhb/*
94167368Sjhb * Return a pointer to the owning thread if the lock is write-locked or
95167368Sjhb * NULL if the lock is unlocked or read-locked.
96167368Sjhb */
97167368Sjhb#define	rw_wowner(rw)							\
98167368Sjhb	((rw)->rw_lock & RW_LOCK_READ ? NULL :				\
99167368Sjhb	    (struct thread *)RW_OWNER((rw)->rw_lock))
100167368Sjhb
101167368Sjhb/*
102167368Sjhb * Returns if a write owner is recursed.  Write ownership is not assured
103167368Sjhb * here and should be previously checked.
104167368Sjhb */
105167368Sjhb#define	rw_recursed(rw)		((rw)->rw_recurse != 0)
106167368Sjhb
107167368Sjhb/*
108167368Sjhb * Return true if curthread helds the lock.
109167368Sjhb */
110167368Sjhb#define	rw_wlocked(rw)		(rw_wowner((rw)) == curthread)
111167368Sjhb
112167368Sjhb/*
113167368Sjhb * Return a pointer to the owning thread for this lock who should receive
114167368Sjhb * any priority lent by threads that block on this lock.  Currently this
115167368Sjhb * is identical to rw_wowner().
116154941Sjhb */
117154941Sjhb#define	rw_owner(rw)		rw_wowner(rw)
118154941Sjhb
119154941Sjhb#ifndef INVARIANTS
120154941Sjhb#define	_rw_assert(rw, what, file, line)
121167787Sjhb#endif
122167787Sjhb
123157882Sjhbvoid
124154941Sjhbassert_rw(const struct lock_object *lock, int what)
125154941Sjhb{
126154941Sjhb
127154941Sjhb	rw_assert((const struct rwlock *)lock, what);
128154941Sjhb}
129154941Sjhb
130154941Sjhbvoid
131167787Sjhblock_rw(struct lock_object *lock, int how)
132167787Sjhb{
133154941Sjhb	struct rwlock *rw;
134154941Sjhb
135154941Sjhb	rw = (struct rwlock *)lock;
136154941Sjhb	if (how)
137154941Sjhb		rw_wlock(rw);
138154941Sjhb	else
139154941Sjhb		rw_rlock(rw);
140154941Sjhb}
141154941Sjhb
142154941Sjhbint
143167024Srwatsonunlock_rw(struct lock_object *lock)
144167024Srwatson{
145167024Srwatson	struct rwlock *rw;
146167024Srwatson
147167024Srwatson	rw = (struct rwlock *)lock;
148167024Srwatson	rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
149167024Srwatson	if (rw->rw_lock & RW_LOCK_READ) {
150154941Sjhb		rw_runlock(rw);
151154941Sjhb		return (0);
152154941Sjhb	} else {
153154941Sjhb		rw_wunlock(rw);
154154941Sjhb		return (1);
155157826Sjhb	}
156154941Sjhb}
157167787Sjhb
158167787Sjhb#ifdef KDTRACE_HOOKS
159154941Sjhbint
160154941Sjhbowner_rw(const struct lock_object *lock, struct thread **owner)
161167787Sjhb{
162167787Sjhb	const struct rwlock *rw = (const struct rwlock *)lock;
163160771Sjhb	uintptr_t x = rw->rw_lock;
164154941Sjhb
165154941Sjhb	*owner = rw_wowner(rw);
166154941Sjhb	return ((x & RW_LOCK_READ) != 0 ?  (RW_READERS(x) != 0) :
167154941Sjhb	    (*owner != NULL));
168154941Sjhb}
169154941Sjhb#endif
170154941Sjhb
171154941Sjhbvoid
172160771Sjhbrw_init_flags(struct rwlock *rw, const char *name, int opts)
173167787Sjhb{
174167787Sjhb	int flags;
175167787Sjhb
176154941Sjhb	MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
177154941Sjhb	    RW_RECURSE)) == 0);
178154941Sjhb	ASSERT_ATOMIC_LOAD_PTR(rw->rw_lock,
179154941Sjhb	    ("%s: rw_lock not aligned for %s: %p", __func__, name,
180154941Sjhb	    &rw->rw_lock));
181154941Sjhb
182157851Swkoszek	flags = LO_UPGRADABLE;
183157846Sjhb	if (opts & RW_DUPOK)
184157851Swkoszek		flags |= LO_DUPOK;
185167307Sjhb	if (opts & RW_NOPROFILE)
186167054Skmacy		flags |= LO_NOPROFILE;
187154941Sjhb	if (!(opts & RW_NOWITNESS))
188154941Sjhb		flags |= LO_WITNESS;
189157826Sjhb	if (opts & RW_RECURSE)
190154941Sjhb		flags |= LO_RECURSABLE;
191167787Sjhb	if (opts & RW_QUIET)
192167787Sjhb		flags |= LO_QUIET;
193154941Sjhb
194154941Sjhb	rw->rw_lock = RW_UNLOCKED;
195154941Sjhb	rw->rw_recurse = 0;
196154941Sjhb	lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
197154941Sjhb}
198154941Sjhb
199154941Sjhbvoid
200154941Sjhbrw_destroy(struct rwlock *rw)
201154941Sjhb{
202154941Sjhb
203154941Sjhb	KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock %p not unlocked", rw));
204154941Sjhb	KASSERT(rw->rw_recurse == 0, ("rw lock %p still recursed", rw));
205154941Sjhb	rw->rw_lock = RW_DESTROYED;
206154941Sjhb	lock_destroy(&rw->lock_object);
207154941Sjhb}
208154941Sjhb
209154941Sjhbvoid
210154941Sjhbrw_sysinit(void *arg)
211154941Sjhb{
212154941Sjhb	struct rw_args *args = arg;
213154941Sjhb
214154941Sjhb	rw_init(args->ra_rw, args->ra_desc);
215154941Sjhb}
216154941Sjhb
217154941Sjhbvoid
218154941Sjhbrw_sysinit_flags(void *arg)
219154941Sjhb{
220154941Sjhb	struct rw_args_flags *args = arg;
221154941Sjhb
222154941Sjhb	rw_init_flags(args->ra_rw, args->ra_desc, args->ra_flags);
223154941Sjhb}
224154941Sjhb
225154941Sjhbint
226154941Sjhbrw_wowned(const struct rwlock *rw)
227167787Sjhb{
228154941Sjhb
229154941Sjhb	return (rw_wowner(rw) == curthread);
230154941Sjhb}
231154941Sjhb
232167307Sjhbvoid
233167307Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line)
234167787Sjhb{
235167307Sjhb
236154941Sjhb	MPASS(curthread != NULL);
237154941Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
238157846Sjhb	    ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
239154941Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
240154941Sjhb	    line, NULL);
241167787Sjhb	__rw_wlock(rw, curthread, file, line);
242167307Sjhb	LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
243154941Sjhb	WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
244154941Sjhb	curthread->td_locks++;
245154941Sjhb}
246154941Sjhb
247154941Sjhbint
248154941Sjhb_rw_try_wlock(struct rwlock *rw, const char *file, int line)
249167787Sjhb{
250154941Sjhb	int rval;
251154941Sjhb
252154941Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
253154941Sjhb	    ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
254154941Sjhb
255154941Sjhb	if (rw_wlocked(rw) &&
256154941Sjhb	    (rw->lock_object.lo_flags & LO_RECURSABLE) != 0) {
257154941Sjhb		rw->rw_recurse++;
258167787Sjhb		rval = 1;
259157846Sjhb	} else
260154941Sjhb		rval = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_UNLOCKED,
261154941Sjhb		    (uintptr_t)curthread);
262154941Sjhb
263154941Sjhb	LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
264154941Sjhb	if (rval) {
265154941Sjhb		WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
266154941Sjhb		    file, line);
267154941Sjhb		curthread->td_locks++;
268154941Sjhb	}
269157826Sjhb	return (rval);
270157826Sjhb}
271157826Sjhb
272167787Sjhbvoid
273157826Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line)
274157826Sjhb{
275157826Sjhb
276167787Sjhb	MPASS(curthread != NULL);
277157826Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
278157826Sjhb	    ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
279154941Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
280154941Sjhb	curthread->td_locks--;
281157846Sjhb	WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
282154941Sjhb	LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
283157846Sjhb	    line);
284157846Sjhb	if (!rw_recursed(rw))
285157846Sjhb		LOCKSTAT_PROFILE_RELEASE_LOCK(LS_RW_WUNLOCK_RELEASE, rw);
286157846Sjhb	__rw_wunlock(rw, curthread, file, line);
287157846Sjhb}
288157846Sjhb/*
289167787Sjhb * Determines whether a new reader can acquire a lock.  Succeeds if the
290167787Sjhb * reader already owns a read lock and the lock is locked for read to
291157846Sjhb * prevent deadlock from reader recursion.  Also succeeds if the lock
292157846Sjhb * is unlocked and has no writer waiters or spinners.  Failing otherwise
293157846Sjhb * prioritizes writers before readers.
294157846Sjhb */
295157846Sjhb#define	RW_CAN_READ(_rw)						\
296157846Sjhb    ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) &	\
297157846Sjhb    (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) ==	\
298157846Sjhb    RW_LOCK_READ)
299157846Sjhb
300157846Sjhbvoid
301154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line)
302154941Sjhb{
303154941Sjhb	struct turnstile *ts;
304167787Sjhb#ifdef ADAPTIVE_RWLOCKS
305154941Sjhb	volatile struct thread *owner;
306154941Sjhb	int spintries = 0;
307167787Sjhb	int i;
308167787Sjhb#endif
309154941Sjhb#ifdef LOCK_PROFILING
310154941Sjhb	uint64_t waittime = 0;
311154941Sjhb	int contested = 0;
312154941Sjhb#endif
313154941Sjhb	uintptr_t v;
314154941Sjhb#ifdef KDTRACE_HOOKS
315154941Sjhb	uint64_t spin_cnt = 0;
316154941Sjhb	uint64_t sleep_cnt = 0;
317154941Sjhb	int64_t sleep_time = 0;
318154941Sjhb#endif
319167787Sjhb
320167787Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
321160771Sjhb	    ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
322154941Sjhb	KASSERT(rw_wowner(rw) != curthread,
323154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
324154941Sjhb	    rw->lock_object.lo_name, file, line));
325154941Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line, NULL);
326154941Sjhb
327154941Sjhb	for (;;) {
328154941Sjhb#ifdef KDTRACE_HOOKS
329154941Sjhb		spin_cnt++;
330154941Sjhb#endif
331160771Sjhb		/*
332167787Sjhb		 * Handle the easy case.  If no other thread has a write
333167787Sjhb		 * lock, then try to bump up the count of read locks.  Note
334154941Sjhb		 * that we have to preserve the current state of the
335154941Sjhb		 * RW_LOCK_WRITE_WAITERS flag.  If we fail to acquire a
336154941Sjhb		 * read lock, then rw_lock must have changed, so restart
337154941Sjhb		 * the loop.  Note that this handles the case of a
338154941Sjhb		 * completely unlocked rwlock since such a lock is encoded
339154941Sjhb		 * as a read lock with no waiters.
340154941Sjhb		 */
341154941Sjhb		v = rw->rw_lock;
342154941Sjhb		if (RW_CAN_READ(v)) {
343154941Sjhb			/*
344154941Sjhb			 * The RW_LOCK_READ_WAITERS flag should only be set
345154941Sjhb			 * if the lock has been unlocked and write waiters
346167787Sjhb			 * were present.
347154941Sjhb			 */
348154941Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock, v,
349154941Sjhb			    v + RW_ONE_READER)) {
350154941Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
351154941Sjhb					CTR4(KTR_LOCK,
352154941Sjhb					    "%s: %p succeed %p -> %p", __func__,
353154941Sjhb					    rw, (void *)v,
354167307Sjhb					    (void *)(v + RW_ONE_READER));
355154941Sjhb				break;
356164159Skmacy			}
357154941Sjhb			continue;
358154941Sjhb		}
359154941Sjhb		lock_profile_obtain_lock_failed(&rw->lock_object,
360154941Sjhb		    &contested, &waittime);
361154941Sjhb
362154941Sjhb#ifdef ADAPTIVE_RWLOCKS
363154941Sjhb		/*
364154941Sjhb		 * If the owner is running on another CPU, spin until
365154941Sjhb		 * the owner stops running or the state of the lock
366154941Sjhb		 * changes.
367154941Sjhb		 */
368154941Sjhb		if ((v & RW_LOCK_READ) == 0) {
369154941Sjhb			owner = (struct thread *)RW_OWNER(v);
370154941Sjhb			if (TD_IS_RUNNING(owner)) {
371154941Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
372154941Sjhb					CTR3(KTR_LOCK,
373154941Sjhb					    "%s: spinning on %p held by %p",
374154941Sjhb					    __func__, rw, owner);
375154941Sjhb				while ((struct thread*)RW_OWNER(rw->rw_lock) ==
376154941Sjhb				    owner && TD_IS_RUNNING(owner)) {
377154941Sjhb					cpu_spinwait();
378154941Sjhb#ifdef KDTRACE_HOOKS
379154941Sjhb					spin_cnt++;
380167787Sjhb#endif
381154941Sjhb				}
382154941Sjhb				continue;
383154941Sjhb			}
384154941Sjhb		} else if (spintries < rowner_retries) {
385154941Sjhb			spintries++;
386154941Sjhb			for (i = 0; i < rowner_loops; i++) {
387154941Sjhb				v = rw->rw_lock;
388154941Sjhb				if ((v & RW_LOCK_READ) == 0 || RW_CAN_READ(v))
389154941Sjhb					break;
390154941Sjhb				cpu_spinwait();
391154941Sjhb			}
392154941Sjhb			if (i != rowner_loops)
393154941Sjhb				continue;
394154941Sjhb		}
395154941Sjhb#endif
396154941Sjhb
397154941Sjhb		/*
398167787Sjhb		 * Okay, now it's the hard case.  Some other thread already
399154941Sjhb		 * has a write lock or there are write waiters present,
400154941Sjhb		 * acquire the turnstile lock so we can begin the process
401154941Sjhb		 * of blocking.
402154941Sjhb		 */
403154941Sjhb		ts = turnstile_trywait(&rw->lock_object);
404154941Sjhb
405154941Sjhb		/*
406154941Sjhb		 * The lock might have been released while we spun, so
407154941Sjhb		 * recheck its state and restart the loop if needed.
408154941Sjhb		 */
409154941Sjhb		v = rw->rw_lock;
410154941Sjhb		if (RW_CAN_READ(v)) {
411154941Sjhb			turnstile_cancel(ts);
412154941Sjhb			continue;
413154941Sjhb		}
414154941Sjhb
415154941Sjhb#ifdef ADAPTIVE_RWLOCKS
416154941Sjhb		/*
417154941Sjhb		 * The current lock owner might have started executing
418167787Sjhb		 * on another CPU (or the lock could have changed
419154941Sjhb		 * owners) while we were waiting on the turnstile
420154941Sjhb		 * chain lock.  If so, drop the turnstile lock and try
421167787Sjhb		 * again.
422154941Sjhb		 */
423154941Sjhb		if ((v & RW_LOCK_READ) == 0) {
424154941Sjhb			owner = (struct thread *)RW_OWNER(v);
425154941Sjhb			if (TD_IS_RUNNING(owner)) {
426154941Sjhb				turnstile_cancel(ts);
427154941Sjhb				continue;
428154941Sjhb			}
429154941Sjhb		}
430154941Sjhb#endif
431154941Sjhb
432167787Sjhb		/*
433157846Sjhb		 * The lock is held in write mode or it already has waiters.
434154941Sjhb		 */
435154941Sjhb		MPASS(!RW_CAN_READ(v));
436154941Sjhb
437154941Sjhb		/*
438167787Sjhb		 * If the RW_LOCK_READ_WAITERS flag is already set, then
439154941Sjhb		 * we can go ahead and block.  If it is not set then try
440154941Sjhb		 * to set it.  If we fail to set it drop the turnstile
441154941Sjhb		 * lock and restart the loop.
442154941Sjhb		 */
443154941Sjhb		if (!(v & RW_LOCK_READ_WAITERS)) {
444154941Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
445154941Sjhb			    v | RW_LOCK_READ_WAITERS)) {
446154941Sjhb				turnstile_cancel(ts);
447154941Sjhb				continue;
448154941Sjhb			}
449157851Swkoszek			if (LOCK_LOG_TEST(&rw->lock_object, 0))
450157846Sjhb				CTR2(KTR_LOCK, "%s: %p set read waiters flag",
451157851Swkoszek				    __func__, rw);
452154941Sjhb		}
453154941Sjhb
454167787Sjhb		/*
455154941Sjhb		 * We were unable to acquire the lock and the read waiters
456167787Sjhb		 * flag is set, so we must block on the turnstile.
457154941Sjhb		 */
458154941Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
459167787Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
460154941Sjhb			    rw);
461154941Sjhb#ifdef KDTRACE_HOOKS
462154941Sjhb		sleep_time -= lockstat_nsecs();
463154941Sjhb#endif
464154941Sjhb		turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
465154941Sjhb#ifdef KDTRACE_HOOKS
466154941Sjhb		sleep_time += lockstat_nsecs();
467167787Sjhb		sleep_cnt++;
468154941Sjhb#endif
469154941Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
470154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
471154941Sjhb			    __func__, rw);
472154941Sjhb	}
473154941Sjhb
474154941Sjhb	/*
475154941Sjhb	 * TODO: acquire "owner of record" here.  Here be turnstile dragons
476154941Sjhb	 * however.  turnstiles don't like owners changing between calls to
477154941Sjhb	 * turnstile_wait() currently.
478154941Sjhb	 */
479154941Sjhb	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_RLOCK_ACQUIRE, rw, contested,
480154941Sjhb	    waittime, file, line);
481154941Sjhb	LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
482154941Sjhb	WITNESS_LOCK(&rw->lock_object, 0, file, line);
483154941Sjhb	curthread->td_locks++;
484154941Sjhb	curthread->td_rw_rlocks++;
485154941Sjhb#ifdef KDTRACE_HOOKS
486167787Sjhb	if (sleep_time)
487154941Sjhb		LOCKSTAT_RECORD1(LS_RW_RLOCK_BLOCK, rw, sleep_time);
488154941Sjhb
489154941Sjhb	/*
490154941Sjhb	 * Record only the loops spinning and not sleeping.
491167787Sjhb	 */
492154941Sjhb	if (spin_cnt > sleep_cnt)
493154941Sjhb		LOCKSTAT_RECORD1(LS_RW_RLOCK_SPIN, rw, (spin_cnt - sleep_cnt));
494154941Sjhb#endif
495154941Sjhb}
496154941Sjhb
497154941Sjhbint
498154941Sjhb_rw_try_rlock(struct rwlock *rw, const char *file, int line)
499154941Sjhb{
500154941Sjhb	uintptr_t x;
501157826Sjhb
502157826Sjhb	for (;;) {
503157826Sjhb		x = rw->rw_lock;
504167787Sjhb		KASSERT(rw->rw_lock != RW_DESTROYED,
505157826Sjhb		    ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
506157826Sjhb		if (!(x & RW_LOCK_READ))
507157826Sjhb			break;
508167787Sjhb		if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, x + RW_ONE_READER)) {
509157826Sjhb			LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
510157826Sjhb			    line);
511154941Sjhb			WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
512154941Sjhb			curthread->td_locks++;
513157846Sjhb			curthread->td_rw_rlocks++;
514157846Sjhb			return (1);
515157846Sjhb		}
516157846Sjhb	}
517157846Sjhb
518157846Sjhb	LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
519157846Sjhb	return (0);
520157846Sjhb}
521167787Sjhb
522167787Sjhbvoid
523157846Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line)
524157846Sjhb{
525157846Sjhb	struct turnstile *ts;
526157846Sjhb	uintptr_t x, v, queue;
527157846Sjhb
528157846Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
529157846Sjhb	    ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
530157846Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
531154941Sjhb	curthread->td_locks--;
532154941Sjhb	curthread->td_rw_rlocks--;
533154941Sjhb	WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
534154941Sjhb	LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
535154941Sjhb
536167787Sjhb	/* TODO: drop "owner of record" here. */
537154941Sjhb
538154941Sjhb	for (;;) {
539167787Sjhb		/*
540154941Sjhb		 * See if there is more than one read lock held.  If so,
541167787Sjhb		 * just drop one and return.
542154941Sjhb		 */
543154941Sjhb		x = rw->rw_lock;
544154941Sjhb		if (RW_READERS(x) > 1) {
545154941Sjhb			if (atomic_cmpset_rel_ptr(&rw->rw_lock, x,
546154941Sjhb			    x - RW_ONE_READER)) {
547154941Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
548154941Sjhb					CTR4(KTR_LOCK,
549154941Sjhb					    "%s: %p succeeded %p -> %p",
550154941Sjhb					    __func__, rw, (void *)x,
551154941Sjhb					    (void *)(x - RW_ONE_READER));
552154941Sjhb				break;
553154941Sjhb			}
554154941Sjhb			continue;
555154941Sjhb		}
556154941Sjhb		/*
557154941Sjhb		 * If there aren't any waiters for a write lock, then try
558154941Sjhb		 * to drop it quickly.
559154941Sjhb		 */
560154941Sjhb		if (!(x & RW_LOCK_WAITERS)) {
561154941Sjhb			MPASS((x & ~RW_LOCK_WRITE_SPINNER) ==
562167787Sjhb			    RW_READERS_LOCK(1));
563154941Sjhb			if (atomic_cmpset_rel_ptr(&rw->rw_lock, x,
564154941Sjhb			    RW_UNLOCKED)) {
565167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
566167787Sjhb					CTR2(KTR_LOCK, "%s: %p last succeeded",
567154941Sjhb					    __func__, rw);
568157846Sjhb				break;
569157846Sjhb			}
570157846Sjhb			continue;
571157846Sjhb		}
572157846Sjhb		/*
573157846Sjhb		 * Ok, we know we have waiters and we think we are the
574157846Sjhb		 * last reader, so grab the turnstile lock.
575157846Sjhb		 */
576167787Sjhb		turnstile_chain_lock(&rw->lock_object);
577157846Sjhb		v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
578167787Sjhb		MPASS(v & RW_LOCK_WAITERS);
579157846Sjhb
580157846Sjhb		/*
581157846Sjhb		 * Try to drop our lock leaving the lock in a unlocked
582154941Sjhb		 * state.
583157846Sjhb		 *
584154941Sjhb		 * If you wanted to do explicit lock handoff you'd have to
585154941Sjhb		 * do it here.  You'd also want to use turnstile_signal()
586154941Sjhb		 * and you'd have to handle the race where a higher
587154941Sjhb		 * priority thread blocks on the write lock before the
588154941Sjhb		 * thread you wakeup actually runs and have the new thread
589154941Sjhb		 * "steal" the lock.  For now it's a lot simpler to just
590154941Sjhb		 * wakeup all of the waiters.
591154941Sjhb		 *
592154941Sjhb		 * As above, if we fail, then another thread might have
593154941Sjhb		 * acquired a read lock, so drop the turnstile lock and
594154941Sjhb		 * restart.
595154941Sjhb		 */
596154941Sjhb		x = RW_UNLOCKED;
597154941Sjhb		if (v & RW_LOCK_WRITE_WAITERS) {
598154941Sjhb			queue = TS_EXCLUSIVE_QUEUE;
599154941Sjhb			x |= (v & RW_LOCK_READ_WAITERS);
600157846Sjhb		} else
601157846Sjhb			queue = TS_SHARED_QUEUE;
602157846Sjhb		if (!atomic_cmpset_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
603157846Sjhb		    x)) {
604157846Sjhb			turnstile_chain_unlock(&rw->lock_object);
605157846Sjhb			continue;
606154941Sjhb		}
607157846Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
608154941Sjhb			CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
609154941Sjhb			    __func__, rw);
610157846Sjhb
611157846Sjhb		/*
612157846Sjhb		 * Ok.  The lock is released and all that's left is to
613157846Sjhb		 * wake up the waiters.  Note that the lock might not be
614157846Sjhb		 * free anymore, but in that case the writers will just
615157846Sjhb		 * block again if they run before the new lock holder(s)
616157846Sjhb		 * release the lock.
617157846Sjhb		 */
618154941Sjhb		ts = turnstile_lookup(&rw->lock_object);
619157846Sjhb		MPASS(ts != NULL);
620157846Sjhb		turnstile_broadcast(ts, queue);
621157846Sjhb		turnstile_unpend(ts, TS_SHARED_LOCK);
622157846Sjhb		turnstile_chain_unlock(&rw->lock_object);
623157846Sjhb		break;
624157846Sjhb	}
625157846Sjhb	LOCKSTAT_PROFILE_RELEASE_LOCK(LS_RW_RUNLOCK_RELEASE, rw);
626157846Sjhb}
627167787Sjhb
628157846Sjhb/*
629157846Sjhb * This function is called when we are unable to obtain a write lock on the
630157846Sjhb * first try.  This means that at least one other thread holds either a
631157846Sjhb * read or write lock.
632154941Sjhb */
633157846Sjhbvoid
634157846Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
635157846Sjhb{
636167787Sjhb	struct turnstile *ts;
637154941Sjhb#ifdef ADAPTIVE_RWLOCKS
638154941Sjhb	volatile struct thread *owner;
639154941Sjhb	int spintries = 0;
640154941Sjhb	int i;
641154941Sjhb#endif
642154941Sjhb	uintptr_t v, x;
643154941Sjhb#ifdef LOCK_PROFILING
644157882Sjhb	uint64_t waittime = 0;
645157882Sjhb	int contested = 0;
646157882Sjhb#endif
647157882Sjhb#ifdef KDTRACE_HOOKS
648157882Sjhb	uint64_t spin_cnt = 0;
649157882Sjhb	uint64_t sleep_cnt = 0;
650157882Sjhb	int64_t sleep_time = 0;
651157882Sjhb#endif
652157882Sjhb
653157882Sjhb	if (rw_wlocked(rw)) {
654157882Sjhb		KASSERT(rw->lock_object.lo_flags & LO_RECURSABLE,
655157882Sjhb		    ("%s: recursing but non-recursive rw %s @ %s:%d\n",
656157882Sjhb		    __func__, rw->lock_object.lo_name, file, line));
657157882Sjhb		rw->rw_recurse++;
658157882Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
659157882Sjhb			CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
660157882Sjhb		return;
661157882Sjhb	}
662157882Sjhb
663157882Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
664157882Sjhb		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
665157882Sjhb		    rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
666157882Sjhb
667157882Sjhb	while (!_rw_write_lock(rw, tid)) {
668157882Sjhb#ifdef KDTRACE_HOOKS
669157882Sjhb		spin_cnt++;
670157882Sjhb#endif
671157882Sjhb		lock_profile_obtain_lock_failed(&rw->lock_object,
672157882Sjhb		    &contested, &waittime);
673157882Sjhb#ifdef ADAPTIVE_RWLOCKS
674157882Sjhb		/*
675167787Sjhb		 * If the lock is write locked and the owner is
676157882Sjhb		 * running on another CPU, spin until the owner stops
677157882Sjhb		 * running or the state of the lock changes.
678157882Sjhb		 */
679157882Sjhb		v = rw->rw_lock;
680157882Sjhb		owner = (struct thread *)RW_OWNER(v);
681157882Sjhb		if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
682157882Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
683157882Sjhb				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
684157882Sjhb				    __func__, rw, owner);
685157882Sjhb			while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
686157882Sjhb			    TD_IS_RUNNING(owner)) {
687157882Sjhb				cpu_spinwait();
688157882Sjhb#ifdef KDTRACE_HOOKS
689167787Sjhb				spin_cnt++;
690157882Sjhb#endif
691157882Sjhb			}
692157882Sjhb			continue;
693167787Sjhb		}
694157882Sjhb		if ((v & RW_LOCK_READ) && RW_READERS(v) &&
695167787Sjhb		    spintries < rowner_retries) {
696157882Sjhb			if (!(v & RW_LOCK_WRITE_SPINNER)) {
697167787Sjhb				if (!atomic_cmpset_ptr(&rw->rw_lock, v,
698157882Sjhb				    v | RW_LOCK_WRITE_SPINNER)) {
699167787Sjhb					continue;
700157882Sjhb				}
701157882Sjhb			}
702157882Sjhb			spintries++;
703157882Sjhb			for (i = 0; i < rowner_loops; i++) {
704157882Sjhb				if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0)
705157882Sjhb					break;
706157882Sjhb				cpu_spinwait();
707157882Sjhb			}
708157882Sjhb#ifdef KDTRACE_HOOKS
709157882Sjhb			spin_cnt += rowner_loops - i;
710157882Sjhb#endif
711157882Sjhb			if (i != rowner_loops)
712157882Sjhb				continue;
713157882Sjhb		}
714157882Sjhb#endif
715167787Sjhb		ts = turnstile_trywait(&rw->lock_object);
716157882Sjhb		v = rw->rw_lock;
717157882Sjhb
718157882Sjhb#ifdef ADAPTIVE_RWLOCKS
719157882Sjhb		/*
720157882Sjhb		 * The current lock owner might have started executing
721157882Sjhb		 * on another CPU (or the lock could have changed
722157882Sjhb		 * owners) while we were waiting on the turnstile
723157882Sjhb		 * chain lock.  If so, drop the turnstile lock and try
724157882Sjhb		 * again.
725157882Sjhb		 */
726157882Sjhb		if (!(v & RW_LOCK_READ)) {
727157882Sjhb			owner = (struct thread *)RW_OWNER(v);
728157882Sjhb			if (TD_IS_RUNNING(owner)) {
729157882Sjhb				turnstile_cancel(ts);
730157882Sjhb				continue;
731167787Sjhb			}
732157882Sjhb		}
733157882Sjhb#endif
734157882Sjhb		/*
735157882Sjhb		 * Check for the waiters flags about this rwlock.
736157882Sjhb		 * If the lock was released, without maintain any pending
737157882Sjhb		 * waiters queue, simply try to acquire it.
738157882Sjhb		 * If a pending waiters queue is present, claim the lock
739157882Sjhb		 * ownership and maintain the pending queue.
740157882Sjhb		 */
741157882Sjhb		x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
742157882Sjhb		if ((v & ~x) == RW_UNLOCKED) {
743157882Sjhb			x &= ~RW_LOCK_WRITE_SPINNER;
744157882Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) {
745157882Sjhb				if (x)
746167787Sjhb					turnstile_claim(ts);
747157882Sjhb				else
748157882Sjhb					turnstile_cancel(ts);
749157882Sjhb				break;
750157882Sjhb			}
751157882Sjhb			turnstile_cancel(ts);
752157882Sjhb			continue;
753157882Sjhb		}
754157882Sjhb		/*
755157882Sjhb		 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
756157882Sjhb		 * set it.  If we fail to set it, then loop back and try
757157882Sjhb		 * again.
758157882Sjhb		 */
759157882Sjhb		if (!(v & RW_LOCK_WRITE_WAITERS)) {
760157882Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
761157882Sjhb			    v | RW_LOCK_WRITE_WAITERS)) {
762157882Sjhb				turnstile_cancel(ts);
763157882Sjhb				continue;
764157882Sjhb			}
765157882Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
766157882Sjhb				CTR2(KTR_LOCK, "%s: %p set write waiters flag",
767167787Sjhb				    __func__, rw);
768157882Sjhb		}
769157882Sjhb		/*
770157882Sjhb		 * We were unable to acquire the lock and the write waiters
771157882Sjhb		 * flag is set, so we must block on the turnstile.
772167787Sjhb		 */
773157882Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
774157882Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
775154941Sjhb			    rw);
776155162Sscottl#ifdef KDTRACE_HOOKS
777154941Sjhb		sleep_time -= lockstat_nsecs();
778154941Sjhb#endif
779154941Sjhb		turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
780154941Sjhb#ifdef KDTRACE_HOOKS
781154941Sjhb		sleep_time += lockstat_nsecs();
782154941Sjhb		sleep_cnt++;
783154941Sjhb#endif
784154941Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
785154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
786154941Sjhb			    __func__, rw);
787154941Sjhb#ifdef ADAPTIVE_RWLOCKS
788154941Sjhb		spintries = 0;
789154941Sjhb#endif
790154941Sjhb	}
791154941Sjhb	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_RW_WLOCK_ACQUIRE, rw, contested,
792154941Sjhb	    waittime, file, line);
793167368Sjhb#ifdef KDTRACE_HOOKS
794154941Sjhb	if (sleep_time)
795154941Sjhb		LOCKSTAT_RECORD1(LS_RW_WLOCK_BLOCK, rw, sleep_time);
796167787Sjhb
797154941Sjhb	/*
798154941Sjhb	 * Record only the loops spinning and not sleeping.
799154941Sjhb	 */
800154941Sjhb	if (spin_cnt > sleep_cnt)
801154941Sjhb		LOCKSTAT_RECORD1(LS_RW_WLOCK_SPIN, rw, (spin_cnt - sleep_cnt));
802154941Sjhb#endif
803155061Sscottl}
804155061Sscottl
805157826Sjhb/*
806154941Sjhb * This function is called if the first try at releasing a write lock failed.
807167787Sjhb * This means that one of the 2 waiter bits must be set indicating that at
808154941Sjhb * least one thread is waiting on this lock.
809154941Sjhb */
810154941Sjhbvoid
811154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
812157826Sjhb{
813154941Sjhb	struct turnstile *ts;
814167787Sjhb	uintptr_t v;
815154941Sjhb	int queue;
816154941Sjhb
817154941Sjhb	if (rw_wlocked(rw) && rw_recursed(rw)) {
818167787Sjhb		rw->rw_recurse--;
819154941Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
820154941Sjhb			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
821154941Sjhb		return;
822154941Sjhb	}
823154941Sjhb
824157826Sjhb	KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
825154941Sjhb	    ("%s: neither of the waiter flags are set", __func__));
826167787Sjhb
827154941Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
828154941Sjhb		CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
829154941Sjhb
830154941Sjhb	turnstile_chain_lock(&rw->lock_object);
831154941Sjhb	ts = turnstile_lookup(&rw->lock_object);
832154941Sjhb	MPASS(ts != NULL);
833154941Sjhb
834154941Sjhb	/*
835154941Sjhb	 * Use the same algo as sx locks for now.  Prefer waking up shared
836154941Sjhb	 * waiters if we have any over writers.  This is probably not ideal.
837154941Sjhb	 *
838154941Sjhb	 * 'v' is the value we are going to write back to rw_lock.  If we
839154941Sjhb	 * have waiters on both queues, we need to preserve the state of
840154941Sjhb	 * the waiter flag for the queue we don't wake up.  For now this is
841154941Sjhb	 * hardcoded for the algorithm mentioned above.
842154941Sjhb	 *
843154941Sjhb	 * In the case of both readers and writers waiting we wakeup the
844154941Sjhb	 * readers but leave the RW_LOCK_WRITE_WAITERS flag set.  If a
845154941Sjhb	 * new writer comes in before a reader it will claim the lock up
846154941Sjhb	 * above.  There is probably a potential priority inversion in
847154941Sjhb	 * there that could be worked around either by waking both queues
848154941Sjhb	 * of waiters or doing some complicated lock handoff gymnastics.
849167504Sjhb	 */
850167504Sjhb	v = RW_UNLOCKED;
851154941Sjhb	if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) {
852157826Sjhb		queue = TS_EXCLUSIVE_QUEUE;
853154941Sjhb		v |= (rw->rw_lock & RW_LOCK_READ_WAITERS);
854154941Sjhb	} else
855154941Sjhb		queue = TS_SHARED_QUEUE;
856154941Sjhb
857154941Sjhb	/* Wake up all waiters for the specific queue. */
858154941Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
859154941Sjhb		CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
860154941Sjhb		    queue == TS_SHARED_QUEUE ? "read" : "write");
861154941Sjhb	turnstile_broadcast(ts, queue);
862154941Sjhb	atomic_store_rel_ptr(&rw->rw_lock, v);
863154941Sjhb	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
864154941Sjhb	turnstile_chain_unlock(&rw->lock_object);
865167492Sjhb}
866154941Sjhb
867154941Sjhb/*
868154941Sjhb * Attempt to do a non-blocking upgrade from a read lock to a write
869154941Sjhb * lock.  This will only succeed if this thread holds a single read
870154941Sjhb * lock.  Returns true if the upgrade succeeded and false otherwise.
871154941Sjhb */
872154941Sjhbint
873154941Sjhb_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
874{
875	uintptr_t v, x, tid;
876	struct turnstile *ts;
877	int success;
878
879	KASSERT(rw->rw_lock != RW_DESTROYED,
880	    ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
881	_rw_assert(rw, RA_RLOCKED, file, line);
882
883	/*
884	 * Attempt to switch from one reader to a writer.  If there
885	 * are any write waiters, then we will have to lock the
886	 * turnstile first to prevent races with another writer
887	 * calling turnstile_wait() before we have claimed this
888	 * turnstile.  So, do the simple case of no waiters first.
889	 */
890	tid = (uintptr_t)curthread;
891	success = 0;
892	for (;;) {
893		v = rw->rw_lock;
894		if (RW_READERS(v) > 1)
895			break;
896		if (!(v & RW_LOCK_WAITERS)) {
897			success = atomic_cmpset_ptr(&rw->rw_lock, v, tid);
898			if (!success)
899				continue;
900			break;
901		}
902
903		/*
904		 * Ok, we think we have waiters, so lock the turnstile.
905		 */
906		ts = turnstile_trywait(&rw->lock_object);
907		v = rw->rw_lock;
908		if (RW_READERS(v) > 1) {
909			turnstile_cancel(ts);
910			break;
911		}
912		/*
913		 * Try to switch from one reader to a writer again.  This time
914		 * we honor the current state of the waiters flags.
915		 * If we obtain the lock with the flags set, then claim
916		 * ownership of the turnstile.
917		 */
918		x = rw->rw_lock & RW_LOCK_WAITERS;
919		success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x);
920		if (success) {
921			if (x)
922				turnstile_claim(ts);
923			else
924				turnstile_cancel(ts);
925			break;
926		}
927		turnstile_cancel(ts);
928	}
929	LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
930	if (success) {
931		curthread->td_rw_rlocks--;
932		WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
933		    file, line);
934		LOCKSTAT_RECORD0(LS_RW_TRYUPGRADE_UPGRADE, rw);
935	}
936	return (success);
937}
938
939/*
940 * Downgrade a write lock into a single read lock.
941 */
942void
943_rw_downgrade(struct rwlock *rw, const char *file, int line)
944{
945	struct turnstile *ts;
946	uintptr_t tid, v;
947	int rwait, wwait;
948
949	KASSERT(rw->rw_lock != RW_DESTROYED,
950	    ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
951	_rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
952#ifndef INVARIANTS
953	if (rw_recursed(rw))
954		panic("downgrade of a recursed lock");
955#endif
956
957	WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
958
959	/*
960	 * Convert from a writer to a single reader.  First we handle
961	 * the easy case with no waiters.  If there are any waiters, we
962	 * lock the turnstile and "disown" the lock.
963	 */
964	tid = (uintptr_t)curthread;
965	if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
966		goto out;
967
968	/*
969	 * Ok, we think we have waiters, so lock the turnstile so we can
970	 * read the waiter flags without any races.
971	 */
972	turnstile_chain_lock(&rw->lock_object);
973	v = rw->rw_lock & RW_LOCK_WAITERS;
974	rwait = v & RW_LOCK_READ_WAITERS;
975	wwait = v & RW_LOCK_WRITE_WAITERS;
976	MPASS(rwait | wwait);
977
978	/*
979	 * Downgrade from a write lock while preserving waiters flag
980	 * and give up ownership of the turnstile.
981	 */
982	ts = turnstile_lookup(&rw->lock_object);
983	MPASS(ts != NULL);
984	if (!wwait)
985		v &= ~RW_LOCK_READ_WAITERS;
986	atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
987	/*
988	 * Wake other readers if there are no writers pending.  Otherwise they
989	 * won't be able to acquire the lock anyway.
990	 */
991	if (rwait && !wwait) {
992		turnstile_broadcast(ts, TS_SHARED_QUEUE);
993		turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
994	} else
995		turnstile_disown(ts);
996	turnstile_chain_unlock(&rw->lock_object);
997out:
998	curthread->td_rw_rlocks++;
999	LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
1000	LOCKSTAT_RECORD0(LS_RW_DOWNGRADE_DOWNGRADE, rw);
1001}
1002
1003#ifdef INVARIANT_SUPPORT
1004#ifndef INVARIANTS
1005#undef _rw_assert
1006#endif
1007
1008/*
1009 * In the non-WITNESS case, rw_assert() can only detect that at least
1010 * *some* thread owns an rlock, but it cannot guarantee that *this*
1011 * thread owns an rlock.
1012 */
1013void
1014_rw_assert(const struct rwlock *rw, int what, const char *file, int line)
1015{
1016
1017	if (panicstr != NULL)
1018		return;
1019	switch (what) {
1020	case RA_LOCKED:
1021	case RA_LOCKED | RA_RECURSED:
1022	case RA_LOCKED | RA_NOTRECURSED:
1023	case RA_RLOCKED:
1024#ifdef WITNESS
1025		witness_assert(&rw->lock_object, what, file, line);
1026#else
1027		/*
1028		 * If some other thread has a write lock or we have one
1029		 * and are asserting a read lock, fail.  Also, if no one
1030		 * has a lock at all, fail.
1031		 */
1032		if (rw->rw_lock == RW_UNLOCKED ||
1033		    (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
1034		    rw_wowner(rw) != curthread)))
1035			panic("Lock %s not %slocked @ %s:%d\n",
1036			    rw->lock_object.lo_name, (what == RA_RLOCKED) ?
1037			    "read " : "", file, line);
1038
1039		if (!(rw->rw_lock & RW_LOCK_READ)) {
1040			if (rw_recursed(rw)) {
1041				if (what & RA_NOTRECURSED)
1042					panic("Lock %s recursed @ %s:%d\n",
1043					    rw->lock_object.lo_name, file,
1044					    line);
1045			} else if (what & RA_RECURSED)
1046				panic("Lock %s not recursed @ %s:%d\n",
1047				    rw->lock_object.lo_name, file, line);
1048		}
1049#endif
1050		break;
1051	case RA_WLOCKED:
1052	case RA_WLOCKED | RA_RECURSED:
1053	case RA_WLOCKED | RA_NOTRECURSED:
1054		if (rw_wowner(rw) != curthread)
1055			panic("Lock %s not exclusively locked @ %s:%d\n",
1056			    rw->lock_object.lo_name, file, line);
1057		if (rw_recursed(rw)) {
1058			if (what & RA_NOTRECURSED)
1059				panic("Lock %s recursed @ %s:%d\n",
1060				    rw->lock_object.lo_name, file, line);
1061		} else if (what & RA_RECURSED)
1062			panic("Lock %s not recursed @ %s:%d\n",
1063			    rw->lock_object.lo_name, file, line);
1064		break;
1065	case RA_UNLOCKED:
1066#ifdef WITNESS
1067		witness_assert(&rw->lock_object, what, file, line);
1068#else
1069		/*
1070		 * If we hold a write lock fail.  We can't reliably check
1071		 * to see if we hold a read lock or not.
1072		 */
1073		if (rw_wowner(rw) == curthread)
1074			panic("Lock %s exclusively locked @ %s:%d\n",
1075			    rw->lock_object.lo_name, file, line);
1076#endif
1077		break;
1078	default:
1079		panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
1080		    line);
1081	}
1082}
1083#endif /* INVARIANT_SUPPORT */
1084
1085#ifdef DDB
1086void
1087db_show_rwlock(const struct lock_object *lock)
1088{
1089	const struct rwlock *rw;
1090	struct thread *td;
1091
1092	rw = (const struct rwlock *)lock;
1093
1094	db_printf(" state: ");
1095	if (rw->rw_lock == RW_UNLOCKED)
1096		db_printf("UNLOCKED\n");
1097	else if (rw->rw_lock == RW_DESTROYED) {
1098		db_printf("DESTROYED\n");
1099		return;
1100	} else if (rw->rw_lock & RW_LOCK_READ)
1101		db_printf("RLOCK: %ju locks\n",
1102		    (uintmax_t)(RW_READERS(rw->rw_lock)));
1103	else {
1104		td = rw_wowner(rw);
1105		db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1106		    td->td_tid, td->td_proc->p_pid, td->td_name);
1107		if (rw_recursed(rw))
1108			db_printf(" recursed: %u\n", rw->rw_recurse);
1109	}
1110	db_printf(" waiters: ");
1111	switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
1112	case RW_LOCK_READ_WAITERS:
1113		db_printf("readers\n");
1114		break;
1115	case RW_LOCK_WRITE_WAITERS:
1116		db_printf("writers\n");
1117		break;
1118	case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
1119		db_printf("readers and writers\n");
1120		break;
1121	default:
1122		db_printf("none\n");
1123		break;
1124	}
1125}
1126
1127#endif
1128