kern_rwlock.c revision 182914
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 182914 2008-09-10 19:13:30Z jhb $");
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
194167024Srwatsonint
195167024Srwatsonrw_wowned(struct rwlock *rw)
196167024Srwatson{
197167024Srwatson
198167024Srwatson	return (rw_wowner(rw) == curthread);
199167024Srwatson}
200167024Srwatson
201154941Sjhbvoid
202154941Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line)
203154941Sjhb{
204154941Sjhb
205154941Sjhb	MPASS(curthread != NULL);
206169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
207169394Sjhb	    ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
208167787Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
209182914Sjhb	    line, NULL);
210154941Sjhb	__rw_wlock(rw, curthread, file, line);
211171052Sattilio	LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
212167787Sjhb	WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
213160771Sjhb	curthread->td_locks++;
214154941Sjhb}
215154941Sjhb
216177843Sattilioint
217177843Sattilio_rw_try_wlock(struct rwlock *rw, const char *file, int line)
218177843Sattilio{
219177843Sattilio	int rval;
220177843Sattilio
221177843Sattilio	KASSERT(rw->rw_lock != RW_DESTROYED,
222177843Sattilio	    ("rw_try_wlock() of destroyed rwlock @ %s:%d", file, line));
223177843Sattilio
224177843Sattilio	if (rw_wlocked(rw) && (rw->lock_object.lo_flags & RW_RECURSE) != 0) {
225177843Sattilio		rw->rw_recurse++;
226177843Sattilio		rval = 1;
227177843Sattilio	} else
228177843Sattilio		rval = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_UNLOCKED,
229177843Sattilio		    (uintptr_t)curthread);
230177843Sattilio
231177843Sattilio	LOCK_LOG_TRY("WLOCK", &rw->lock_object, 0, rval, file, line);
232177843Sattilio	if (rval) {
233177843Sattilio		WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
234177843Sattilio		    file, line);
235177843Sattilio		curthread->td_locks++;
236177843Sattilio	}
237177843Sattilio	return (rval);
238177843Sattilio}
239177843Sattilio
240154941Sjhbvoid
241154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line)
242154941Sjhb{
243154941Sjhb
244154941Sjhb	MPASS(curthread != NULL);
245169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
246169394Sjhb	    ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
247154941Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
248160771Sjhb	curthread->td_locks--;
249167787Sjhb	WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
250171052Sattilio	LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
251171052Sattilio	    line);
252171052Sattilio	if (!rw_recursed(rw))
253171052Sattilio		lock_profile_release_lock(&rw->lock_object);
254154941Sjhb	__rw_wunlock(rw, curthread, file, line);
255154941Sjhb}
256176017Sjeff/*
257176017Sjeff * Determines whether a new reader can acquire a lock.  Succeeds if the
258176017Sjeff * reader already owns a read lock and the lock is locked for read to
259176017Sjeff * prevent deadlock from reader recursion.  Also succeeds if the lock
260176017Sjeff * is unlocked and has no writer waiters or spinners.  Failing otherwise
261176017Sjeff * prioritizes writers before readers.
262176017Sjeff */
263176017Sjeff#define	RW_CAN_READ(_rw)						\
264176017Sjeff    ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) &	\
265176017Sjeff    (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) ==	\
266176017Sjeff    RW_LOCK_READ)
267154941Sjhb
268154941Sjhbvoid
269154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line)
270154941Sjhb{
271170295Sjeff	struct turnstile *ts;
272167801Sjhb#ifdef ADAPTIVE_RWLOCKS
273157846Sjhb	volatile struct thread *owner;
274177912Sjeff	int spintries = 0;
275177912Sjeff	int i;
276157851Swkoszek#endif
277167307Sjhb	uint64_t waittime = 0;
278167054Skmacy	int contested = 0;
279176017Sjeff	uintptr_t v;
280154941Sjhb
281169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
282169394Sjhb	    ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
283157826Sjhb	KASSERT(rw_wowner(rw) != curthread,
284154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
285167787Sjhb	    rw->lock_object.lo_name, file, line));
286182914Sjhb	WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line, NULL);
287154941Sjhb
288154941Sjhb	for (;;) {
289154941Sjhb		/*
290154941Sjhb		 * Handle the easy case.  If no other thread has a write
291154941Sjhb		 * lock, then try to bump up the count of read locks.  Note
292154941Sjhb		 * that we have to preserve the current state of the
293154941Sjhb		 * RW_LOCK_WRITE_WAITERS flag.  If we fail to acquire a
294154941Sjhb		 * read lock, then rw_lock must have changed, so restart
295154941Sjhb		 * the loop.  Note that this handles the case of a
296154941Sjhb		 * completely unlocked rwlock since such a lock is encoded
297154941Sjhb		 * as a read lock with no waiters.
298154941Sjhb		 */
299176017Sjeff		v = rw->rw_lock;
300176017Sjeff		if (RW_CAN_READ(v)) {
301154941Sjhb			/*
302154941Sjhb			 * The RW_LOCK_READ_WAITERS flag should only be set
303176017Sjeff			 * if the lock has been unlocked and write waiters
304176017Sjeff			 * were present.
305154941Sjhb			 */
306176017Sjeff			if (atomic_cmpset_acq_ptr(&rw->rw_lock, v,
307176017Sjeff			    v + RW_ONE_READER)) {
308167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
309154941Sjhb					CTR4(KTR_LOCK,
310154941Sjhb					    "%s: %p succeed %p -> %p", __func__,
311176017Sjeff					    rw, (void *)v,
312176017Sjeff					    (void *)(v + RW_ONE_READER));
313154941Sjhb				break;
314154941Sjhb			}
315157846Sjhb			cpu_spinwait();
316154941Sjhb			continue;
317154941Sjhb		}
318174629Sjeff		lock_profile_obtain_lock_failed(&rw->lock_object,
319174629Sjeff		    &contested, &waittime);
320154941Sjhb
321173960Sattilio#ifdef ADAPTIVE_RWLOCKS
322154941Sjhb		/*
323173960Sattilio		 * If the owner is running on another CPU, spin until
324173960Sattilio		 * the owner stops running or the state of the lock
325173960Sattilio		 * changes.
326173960Sattilio		 */
327176017Sjeff		if ((v & RW_LOCK_READ) == 0) {
328176017Sjeff			owner = (struct thread *)RW_OWNER(v);
329176017Sjeff			if (TD_IS_RUNNING(owner)) {
330176017Sjeff				if (LOCK_LOG_TEST(&rw->lock_object, 0))
331176017Sjeff					CTR3(KTR_LOCK,
332176017Sjeff					    "%s: spinning on %p held by %p",
333176017Sjeff					    __func__, rw, owner);
334176017Sjeff				while ((struct thread*)RW_OWNER(rw->rw_lock) ==
335176017Sjeff				    owner && TD_IS_RUNNING(owner))
336176017Sjeff					cpu_spinwait();
337176017Sjeff				continue;
338176017Sjeff			}
339177912Sjeff		} else if (spintries < rowner_retries) {
340177912Sjeff			spintries++;
341177912Sjeff			for (i = 0; i < rowner_loops; i++) {
342177912Sjeff				v = rw->rw_lock;
343177912Sjeff				if ((v & RW_LOCK_READ) == 0 || RW_CAN_READ(v))
344177912Sjeff					break;
345177912Sjeff				cpu_spinwait();
346177912Sjeff			}
347177912Sjeff			if (i != rowner_loops)
348177912Sjeff				continue;
349173960Sattilio		}
350173960Sattilio#endif
351173960Sattilio
352173960Sattilio		/*
353154941Sjhb		 * Okay, now it's the hard case.  Some other thread already
354176017Sjeff		 * has a write lock or there are write waiters present,
355176017Sjeff		 * acquire the turnstile lock so we can begin the process
356176017Sjeff		 * of blocking.
357154941Sjhb		 */
358170295Sjeff		ts = turnstile_trywait(&rw->lock_object);
359154941Sjhb
360154941Sjhb		/*
361154941Sjhb		 * The lock might have been released while we spun, so
362176017Sjeff		 * recheck its state and restart the loop if needed.
363154941Sjhb		 */
364176017Sjeff		v = rw->rw_lock;
365176017Sjeff		if (RW_CAN_READ(v)) {
366170295Sjeff			turnstile_cancel(ts);
367157846Sjhb			cpu_spinwait();
368154941Sjhb			continue;
369154941Sjhb		}
370154941Sjhb
371173960Sattilio#ifdef ADAPTIVE_RWLOCKS
372154941Sjhb		/*
373173960Sattilio		 * If the current owner of the lock is executing on another
374173960Sattilio		 * CPU quit the hard path and try to spin.
375173960Sattilio		 */
376176017Sjeff		if ((v & RW_LOCK_READ) == 0) {
377176017Sjeff			owner = (struct thread *)RW_OWNER(v);
378176017Sjeff			if (TD_IS_RUNNING(owner)) {
379176017Sjeff				turnstile_cancel(ts);
380176017Sjeff				cpu_spinwait();
381176017Sjeff				continue;
382176017Sjeff			}
383173960Sattilio		}
384173960Sattilio#endif
385173960Sattilio
386173960Sattilio		/*
387176017Sjeff		 * The lock is held in write mode or it already has waiters.
388154941Sjhb		 */
389176017Sjeff		MPASS(!RW_CAN_READ(v));
390176017Sjeff
391176017Sjeff		/*
392176017Sjeff		 * If the RW_LOCK_READ_WAITERS flag is already set, then
393176017Sjeff		 * we can go ahead and block.  If it is not set then try
394176017Sjeff		 * to set it.  If we fail to set it drop the turnstile
395176017Sjeff		 * lock and restart the loop.
396176017Sjeff		 */
397176017Sjeff		if (!(v & RW_LOCK_READ_WAITERS)) {
398176017Sjeff			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
399176017Sjeff			    v | RW_LOCK_READ_WAITERS)) {
400170295Sjeff				turnstile_cancel(ts);
401157826Sjhb				cpu_spinwait();
402157826Sjhb				continue;
403157826Sjhb			}
404167787Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
405157826Sjhb				CTR2(KTR_LOCK, "%s: %p set read waiters flag",
406157826Sjhb				    __func__, rw);
407154941Sjhb		}
408154941Sjhb
409154941Sjhb		/*
410154941Sjhb		 * We were unable to acquire the lock and the read waiters
411154941Sjhb		 * flag is set, so we must block on the turnstile.
412154941Sjhb		 */
413167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
414154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
415154941Sjhb			    rw);
416170295Sjeff		turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
417167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
418154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
419154941Sjhb			    __func__, rw);
420154941Sjhb	}
421154941Sjhb
422154941Sjhb	/*
423154941Sjhb	 * TODO: acquire "owner of record" here.  Here be turnstile dragons
424154941Sjhb	 * however.  turnstiles don't like owners changing between calls to
425154941Sjhb	 * turnstile_wait() currently.
426154941Sjhb	 */
427174629Sjeff	lock_profile_obtain_lock_success( &rw->lock_object, contested,
428174629Sjeff	    waittime, file, line);
429167787Sjhb	LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
430167787Sjhb	WITNESS_LOCK(&rw->lock_object, 0, file, line);
431160771Sjhb	curthread->td_locks++;
432176017Sjeff	curthread->td_rw_rlocks++;
433154941Sjhb}
434154941Sjhb
435177843Sattilioint
436177843Sattilio_rw_try_rlock(struct rwlock *rw, const char *file, int line)
437177843Sattilio{
438177843Sattilio	uintptr_t x;
439177843Sattilio
440177843Sattilio	for (;;) {
441177843Sattilio		x = rw->rw_lock;
442177843Sattilio		KASSERT(rw->rw_lock != RW_DESTROYED,
443177843Sattilio		    ("rw_try_rlock() of destroyed rwlock @ %s:%d", file, line));
444177843Sattilio		if (!(x & RW_LOCK_READ))
445177843Sattilio			break;
446177843Sattilio		if (atomic_cmpset_acq_ptr(&rw->rw_lock, x, x + RW_ONE_READER)) {
447177843Sattilio			LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 1, file,
448177843Sattilio			    line);
449177843Sattilio			WITNESS_LOCK(&rw->lock_object, LOP_TRYLOCK, file, line);
450177843Sattilio			curthread->td_locks++;
451177843Sattilio			curthread->td_rw_rlocks++;
452177843Sattilio			return (1);
453177843Sattilio		}
454177843Sattilio	}
455177843Sattilio
456177843Sattilio	LOCK_LOG_TRY("RLOCK", &rw->lock_object, 0, 0, file, line);
457177843Sattilio	return (0);
458177843Sattilio}
459177843Sattilio
460154941Sjhbvoid
461154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line)
462154941Sjhb{
463154941Sjhb	struct turnstile *ts;
464176017Sjeff	uintptr_t x, v, queue;
465154941Sjhb
466169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
467169394Sjhb	    ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
468154941Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
469160771Sjhb	curthread->td_locks--;
470176017Sjeff	curthread->td_rw_rlocks--;
471167787Sjhb	WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
472167787Sjhb	LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
473154941Sjhb
474154941Sjhb	/* TODO: drop "owner of record" here. */
475154941Sjhb
476154941Sjhb	for (;;) {
477154941Sjhb		/*
478154941Sjhb		 * See if there is more than one read lock held.  If so,
479154941Sjhb		 * just drop one and return.
480154941Sjhb		 */
481154941Sjhb		x = rw->rw_lock;
482154941Sjhb		if (RW_READERS(x) > 1) {
483154941Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, x,
484154941Sjhb			    x - RW_ONE_READER)) {
485167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
486154941Sjhb					CTR4(KTR_LOCK,
487154941Sjhb					    "%s: %p succeeded %p -> %p",
488154941Sjhb					    __func__, rw, (void *)x,
489154941Sjhb					    (void *)(x - RW_ONE_READER));
490154941Sjhb				break;
491154941Sjhb			}
492154941Sjhb			continue;
493167307Sjhb		}
494154941Sjhb		/*
495154941Sjhb		 * If there aren't any waiters for a write lock, then try
496154941Sjhb		 * to drop it quickly.
497154941Sjhb		 */
498176017Sjeff		if (!(x & RW_LOCK_WAITERS)) {
499176017Sjeff			MPASS((x & ~RW_LOCK_WRITE_SPINNER) ==
500176017Sjeff			    RW_READERS_LOCK(1));
501176017Sjeff			if (atomic_cmpset_ptr(&rw->rw_lock, x, RW_UNLOCKED)) {
502167787Sjhb				if (LOCK_LOG_TEST(&rw->lock_object, 0))
503154941Sjhb					CTR2(KTR_LOCK, "%s: %p last succeeded",
504154941Sjhb					    __func__, rw);
505154941Sjhb				break;
506154941Sjhb			}
507154941Sjhb			continue;
508154941Sjhb		}
509154941Sjhb		/*
510176017Sjeff		 * Ok, we know we have waiters and we think we are the
511176017Sjeff		 * last reader, so grab the turnstile lock.
512154941Sjhb		 */
513170295Sjeff		turnstile_chain_lock(&rw->lock_object);
514176017Sjeff		v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
515176017Sjeff		MPASS(v & RW_LOCK_WAITERS);
516154941Sjhb
517154941Sjhb		/*
518154941Sjhb		 * Try to drop our lock leaving the lock in a unlocked
519154941Sjhb		 * state.
520154941Sjhb		 *
521154941Sjhb		 * If you wanted to do explicit lock handoff you'd have to
522154941Sjhb		 * do it here.  You'd also want to use turnstile_signal()
523154941Sjhb		 * and you'd have to handle the race where a higher
524154941Sjhb		 * priority thread blocks on the write lock before the
525154941Sjhb		 * thread you wakeup actually runs and have the new thread
526154941Sjhb		 * "steal" the lock.  For now it's a lot simpler to just
527154941Sjhb		 * wakeup all of the waiters.
528154941Sjhb		 *
529154941Sjhb		 * As above, if we fail, then another thread might have
530154941Sjhb		 * acquired a read lock, so drop the turnstile lock and
531154941Sjhb		 * restart.
532154941Sjhb		 */
533176017Sjeff		x = RW_UNLOCKED;
534176017Sjeff		if (v & RW_LOCK_WRITE_WAITERS) {
535176017Sjeff			queue = TS_EXCLUSIVE_QUEUE;
536176017Sjeff			x |= (v & RW_LOCK_READ_WAITERS);
537176017Sjeff		} else
538176017Sjeff			queue = TS_SHARED_QUEUE;
539176017Sjeff		if (!atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
540176017Sjeff		    x)) {
541170295Sjeff			turnstile_chain_unlock(&rw->lock_object);
542154941Sjhb			continue;
543154941Sjhb		}
544167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
545154941Sjhb			CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
546154941Sjhb			    __func__, rw);
547154941Sjhb
548154941Sjhb		/*
549154941Sjhb		 * Ok.  The lock is released and all that's left is to
550154941Sjhb		 * wake up the waiters.  Note that the lock might not be
551154941Sjhb		 * free anymore, but in that case the writers will just
552154941Sjhb		 * block again if they run before the new lock holder(s)
553154941Sjhb		 * release the lock.
554154941Sjhb		 */
555167787Sjhb		ts = turnstile_lookup(&rw->lock_object);
556157846Sjhb		MPASS(ts != NULL);
557176017Sjeff		turnstile_broadcast(ts, queue);
558154941Sjhb		turnstile_unpend(ts, TS_SHARED_LOCK);
559170295Sjeff		turnstile_chain_unlock(&rw->lock_object);
560154941Sjhb		break;
561154941Sjhb	}
562174629Sjeff	lock_profile_release_lock(&rw->lock_object);
563154941Sjhb}
564154941Sjhb
565154941Sjhb/*
566154941Sjhb * This function is called when we are unable to obtain a write lock on the
567154941Sjhb * first try.  This means that at least one other thread holds either a
568154941Sjhb * read or write lock.
569154941Sjhb */
570154941Sjhbvoid
571154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
572154941Sjhb{
573170295Sjeff	struct turnstile *ts;
574167801Sjhb#ifdef ADAPTIVE_RWLOCKS
575157846Sjhb	volatile struct thread *owner;
576176017Sjeff	int spintries = 0;
577176017Sjeff	int i;
578157851Swkoszek#endif
579171516Sattilio	uint64_t waittime = 0;
580176017Sjeff	uintptr_t v, x;
581171516Sattilio	int contested = 0;
582154941Sjhb
583171052Sattilio	if (rw_wlocked(rw)) {
584171052Sattilio		KASSERT(rw->lock_object.lo_flags & RW_RECURSE,
585171052Sattilio		    ("%s: recursing but non-recursive rw %s @ %s:%d\n",
586171052Sattilio		    __func__, rw->lock_object.lo_name, file, line));
587171052Sattilio		rw->rw_recurse++;
588171052Sattilio		if (LOCK_LOG_TEST(&rw->lock_object, 0))
589171052Sattilio			CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
590171052Sattilio		return;
591171052Sattilio	}
592171052Sattilio
593167787Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
594154941Sjhb		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
595167787Sjhb		    rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
596154941Sjhb
597154941Sjhb	while (!_rw_write_lock(rw, tid)) {
598174629Sjeff		lock_profile_obtain_lock_failed(&rw->lock_object,
599174629Sjeff		    &contested, &waittime);
600173960Sattilio#ifdef ADAPTIVE_RWLOCKS
601173960Sattilio		/*
602173960Sattilio		 * If the lock is write locked and the owner is
603173960Sattilio		 * running on another CPU, spin until the owner stops
604173960Sattilio		 * running or the state of the lock changes.
605173960Sattilio		 */
606173960Sattilio		v = rw->rw_lock;
607173960Sattilio		owner = (struct thread *)RW_OWNER(v);
608173960Sattilio		if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
609173960Sattilio			if (LOCK_LOG_TEST(&rw->lock_object, 0))
610173960Sattilio				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
611173960Sattilio				    __func__, rw, owner);
612173960Sattilio			while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
613173960Sattilio			    TD_IS_RUNNING(owner))
614173960Sattilio				cpu_spinwait();
615173960Sattilio			continue;
616173960Sattilio		}
617177912Sjeff		if ((v & RW_LOCK_READ) && RW_READERS(v) &&
618177912Sjeff		    spintries < rowner_retries) {
619176017Sjeff			if (!(v & RW_LOCK_WRITE_SPINNER)) {
620176017Sjeff				if (!atomic_cmpset_ptr(&rw->rw_lock, v,
621176017Sjeff				    v | RW_LOCK_WRITE_SPINNER)) {
622176017Sjeff					cpu_spinwait();
623176017Sjeff					continue;
624176017Sjeff				}
625176017Sjeff			}
626176017Sjeff			spintries++;
627177912Sjeff			for (i = 0; i < rowner_loops; i++) {
628176017Sjeff				if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0)
629176017Sjeff					break;
630176017Sjeff				cpu_spinwait();
631176017Sjeff			}
632177912Sjeff			if (i != rowner_loops)
633176017Sjeff				continue;
634176017Sjeff		}
635173960Sattilio#endif
636170295Sjeff		ts = turnstile_trywait(&rw->lock_object);
637154941Sjhb		v = rw->rw_lock;
638154941Sjhb
639173960Sattilio#ifdef ADAPTIVE_RWLOCKS
640154941Sjhb		/*
641173960Sattilio		 * If the current owner of the lock is executing on another
642173960Sattilio		 * CPU quit the hard path and try to spin.
643173960Sattilio		 */
644173960Sattilio		if (!(v & RW_LOCK_READ)) {
645173960Sattilio			owner = (struct thread *)RW_OWNER(v);
646173960Sattilio			if (TD_IS_RUNNING(owner)) {
647173960Sattilio				turnstile_cancel(ts);
648173960Sattilio				cpu_spinwait();
649173960Sattilio				continue;
650173960Sattilio			}
651173960Sattilio		}
652173960Sattilio#endif
653173960Sattilio		/*
654179334Sattilio		 * Check for the waiters flags about this rwlock.
655179334Sattilio		 * If the lock was released, without maintain any pending
656179334Sattilio		 * waiters queue, simply try to acquire it.
657179334Sattilio		 * If a pending waiters queue is present, claim the lock
658179334Sattilio		 * ownership and maintain the pending queue.
659154941Sjhb		 */
660176017Sjeff		x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
661176017Sjeff		if ((v & ~x) == RW_UNLOCKED) {
662176017Sjeff			x &= ~RW_LOCK_WRITE_SPINNER;
663176017Sjeff			if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) {
664176017Sjeff				if (x)
665176017Sjeff					turnstile_claim(ts);
666176017Sjeff				else
667176017Sjeff					turnstile_cancel(ts);
668154941Sjhb				break;
669154941Sjhb			}
670170295Sjeff			turnstile_cancel(ts);
671154941Sjhb			cpu_spinwait();
672154941Sjhb			continue;
673154941Sjhb		}
674154941Sjhb		/*
675154941Sjhb		 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
676154941Sjhb		 * set it.  If we fail to set it, then loop back and try
677154941Sjhb		 * again.
678154941Sjhb		 */
679157826Sjhb		if (!(v & RW_LOCK_WRITE_WAITERS)) {
680157826Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
681157826Sjhb			    v | RW_LOCK_WRITE_WAITERS)) {
682170295Sjeff				turnstile_cancel(ts);
683157826Sjhb				cpu_spinwait();
684157826Sjhb				continue;
685157826Sjhb			}
686167787Sjhb			if (LOCK_LOG_TEST(&rw->lock_object, 0))
687157826Sjhb				CTR2(KTR_LOCK, "%s: %p set write waiters flag",
688157826Sjhb				    __func__, rw);
689154941Sjhb		}
690157846Sjhb		/*
691154941Sjhb		 * We were unable to acquire the lock and the write waiters
692154941Sjhb		 * flag is set, so we must block on the turnstile.
693154941Sjhb		 */
694167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
695154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
696154941Sjhb			    rw);
697170295Sjeff		turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
698167787Sjhb		if (LOCK_LOG_TEST(&rw->lock_object, 0))
699154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
700154941Sjhb			    __func__, rw);
701176017Sjeff#ifdef ADAPTIVE_RWLOCKS
702176017Sjeff		spintries = 0;
703176017Sjeff#endif
704154941Sjhb	}
705171516Sattilio	lock_profile_obtain_lock_success(&rw->lock_object, contested, waittime,
706171516Sattilio	    file, line);
707154941Sjhb}
708154941Sjhb
709154941Sjhb/*
710154941Sjhb * This function is called if the first try at releasing a write lock failed.
711154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at
712154941Sjhb * least one thread is waiting on this lock.
713154941Sjhb */
714154941Sjhbvoid
715154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
716154941Sjhb{
717154941Sjhb	struct turnstile *ts;
718154941Sjhb	uintptr_t v;
719154941Sjhb	int queue;
720154941Sjhb
721171052Sattilio	if (rw_wlocked(rw) && rw_recursed(rw)) {
722176017Sjeff		rw->rw_recurse--;
723171052Sattilio		if (LOCK_LOG_TEST(&rw->lock_object, 0))
724171052Sattilio			CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
725171052Sattilio		return;
726171052Sattilio	}
727176017Sjeff	v = rw->rw_lock;
728171052Sattilio
729154941Sjhb	KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
730154941Sjhb	    ("%s: neither of the waiter flags are set", __func__));
731154941Sjhb
732167787Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
733154941Sjhb		CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
734154941Sjhb
735170295Sjeff	turnstile_chain_lock(&rw->lock_object);
736167787Sjhb	ts = turnstile_lookup(&rw->lock_object);
737154941Sjhb	MPASS(ts != NULL);
738154941Sjhb
739154941Sjhb	/*
740154941Sjhb	 * Use the same algo as sx locks for now.  Prefer waking up shared
741154941Sjhb	 * waiters if we have any over writers.  This is probably not ideal.
742154941Sjhb	 *
743154941Sjhb	 * 'v' is the value we are going to write back to rw_lock.  If we
744154941Sjhb	 * have waiters on both queues, we need to preserve the state of
745154941Sjhb	 * the waiter flag for the queue we don't wake up.  For now this is
746154941Sjhb	 * hardcoded for the algorithm mentioned above.
747154941Sjhb	 *
748154941Sjhb	 * In the case of both readers and writers waiting we wakeup the
749154941Sjhb	 * readers but leave the RW_LOCK_WRITE_WAITERS flag set.  If a
750154941Sjhb	 * new writer comes in before a reader it will claim the lock up
751154941Sjhb	 * above.  There is probably a potential priority inversion in
752154941Sjhb	 * there that could be worked around either by waking both queues
753154941Sjhb	 * of waiters or doing some complicated lock handoff gymnastics.
754154941Sjhb	 */
755157846Sjhb	v = RW_UNLOCKED;
756176076Sjeff	if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) {
757176076Sjeff		queue = TS_EXCLUSIVE_QUEUE;
758176076Sjeff		v |= (rw->rw_lock & RW_LOCK_READ_WAITERS);
759176076Sjeff	} else
760154941Sjhb		queue = TS_SHARED_QUEUE;
761157846Sjhb
762157846Sjhb	/* Wake up all waiters for the specific queue. */
763167787Sjhb	if (LOCK_LOG_TEST(&rw->lock_object, 0))
764154941Sjhb		CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
765154941Sjhb		    queue == TS_SHARED_QUEUE ? "read" : "write");
766154941Sjhb	turnstile_broadcast(ts, queue);
767154941Sjhb	atomic_store_rel_ptr(&rw->rw_lock, v);
768154941Sjhb	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
769170295Sjeff	turnstile_chain_unlock(&rw->lock_object);
770154941Sjhb}
771154941Sjhb
772157882Sjhb/*
773157882Sjhb * Attempt to do a non-blocking upgrade from a read lock to a write
774157882Sjhb * lock.  This will only succeed if this thread holds a single read
775157882Sjhb * lock.  Returns true if the upgrade succeeded and false otherwise.
776157882Sjhb */
777157882Sjhbint
778157882Sjhb_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
779157882Sjhb{
780176017Sjeff	uintptr_t v, x, tid;
781170295Sjeff	struct turnstile *ts;
782157882Sjhb	int success;
783157882Sjhb
784169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
785169394Sjhb	    ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
786157882Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
787157882Sjhb
788157882Sjhb	/*
789157882Sjhb	 * Attempt to switch from one reader to a writer.  If there
790157882Sjhb	 * are any write waiters, then we will have to lock the
791157882Sjhb	 * turnstile first to prevent races with another writer
792157882Sjhb	 * calling turnstile_wait() before we have claimed this
793157882Sjhb	 * turnstile.  So, do the simple case of no waiters first.
794157882Sjhb	 */
795157882Sjhb	tid = (uintptr_t)curthread;
796176017Sjeff	success = 0;
797176017Sjeff	for (;;) {
798176017Sjeff		v = rw->rw_lock;
799176017Sjeff		if (RW_READERS(v) > 1)
800176017Sjeff			break;
801176017Sjeff		if (!(v & RW_LOCK_WAITERS)) {
802176017Sjeff			success = atomic_cmpset_ptr(&rw->rw_lock, v, tid);
803176017Sjeff			if (!success)
804176017Sjeff				continue;
805176017Sjeff			break;
806176017Sjeff		}
807157882Sjhb
808176017Sjeff		/*
809176017Sjeff		 * Ok, we think we have waiters, so lock the turnstile.
810176017Sjeff		 */
811176017Sjeff		ts = turnstile_trywait(&rw->lock_object);
812176017Sjeff		v = rw->rw_lock;
813176017Sjeff		if (RW_READERS(v) > 1) {
814176017Sjeff			turnstile_cancel(ts);
815176017Sjeff			break;
816176017Sjeff		}
817176017Sjeff		/*
818176017Sjeff		 * Try to switch from one reader to a writer again.  This time
819176017Sjeff		 * we honor the current state of the waiters flags.
820176017Sjeff		 * If we obtain the lock with the flags set, then claim
821176017Sjeff		 * ownership of the turnstile.
822176017Sjeff		 */
823176017Sjeff		x = rw->rw_lock & RW_LOCK_WAITERS;
824176017Sjeff		success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x);
825176017Sjeff		if (success) {
826176017Sjeff			if (x)
827176017Sjeff				turnstile_claim(ts);
828176017Sjeff			else
829176017Sjeff				turnstile_cancel(ts);
830176017Sjeff			break;
831176017Sjeff		}
832170295Sjeff		turnstile_cancel(ts);
833176017Sjeff	}
834167787Sjhb	LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
835176017Sjeff	if (success) {
836176017Sjeff		curthread->td_rw_rlocks--;
837167787Sjhb		WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
838157882Sjhb		    file, line);
839176017Sjeff	}
840157882Sjhb	return (success);
841157882Sjhb}
842157882Sjhb
843157882Sjhb/*
844157882Sjhb * Downgrade a write lock into a single read lock.
845157882Sjhb */
846157882Sjhbvoid
847157882Sjhb_rw_downgrade(struct rwlock *rw, const char *file, int line)
848157882Sjhb{
849157882Sjhb	struct turnstile *ts;
850157882Sjhb	uintptr_t tid, v;
851176017Sjeff	int rwait, wwait;
852157882Sjhb
853169394Sjhb	KASSERT(rw->rw_lock != RW_DESTROYED,
854169394Sjhb	    ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
855171052Sattilio	_rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
856171052Sattilio#ifndef INVARIANTS
857171052Sattilio	if (rw_recursed(rw))
858171052Sattilio		panic("downgrade of a recursed lock");
859171052Sattilio#endif
860157882Sjhb
861167787Sjhb	WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
862157882Sjhb
863157882Sjhb	/*
864157882Sjhb	 * Convert from a writer to a single reader.  First we handle
865157882Sjhb	 * the easy case with no waiters.  If there are any waiters, we
866176017Sjeff	 * lock the turnstile and "disown" the lock.
867157882Sjhb	 */
868157882Sjhb	tid = (uintptr_t)curthread;
869157882Sjhb	if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
870157882Sjhb		goto out;
871157882Sjhb
872157882Sjhb	/*
873157882Sjhb	 * Ok, we think we have waiters, so lock the turnstile so we can
874157882Sjhb	 * read the waiter flags without any races.
875157882Sjhb	 */
876170295Sjeff	turnstile_chain_lock(&rw->lock_object);
877176017Sjeff	v = rw->rw_lock & RW_LOCK_WAITERS;
878176017Sjeff	rwait = v & RW_LOCK_READ_WAITERS;
879176017Sjeff	wwait = v & RW_LOCK_WRITE_WAITERS;
880176017Sjeff	MPASS(rwait | wwait);
881157882Sjhb
882157882Sjhb	/*
883176017Sjeff	 * Downgrade from a write lock while preserving waiters flag
884176017Sjeff	 * and give up ownership of the turnstile.
885157882Sjhb	 */
886167787Sjhb	ts = turnstile_lookup(&rw->lock_object);
887157882Sjhb	MPASS(ts != NULL);
888176017Sjeff	if (!wwait)
889176017Sjeff		v &= ~RW_LOCK_READ_WAITERS;
890176017Sjeff	atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
891176017Sjeff	/*
892176017Sjeff	 * Wake other readers if there are no writers pending.  Otherwise they
893176017Sjeff	 * won't be able to acquire the lock anyway.
894176017Sjeff	 */
895176017Sjeff	if (rwait && !wwait) {
896157882Sjhb		turnstile_broadcast(ts, TS_SHARED_QUEUE);
897157882Sjhb		turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
898176017Sjeff	} else
899157882Sjhb		turnstile_disown(ts);
900170295Sjeff	turnstile_chain_unlock(&rw->lock_object);
901157882Sjhbout:
902176017Sjeff	curthread->td_rw_rlocks++;
903167787Sjhb	LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
904157882Sjhb}
905157882Sjhb
906154941Sjhb#ifdef INVARIANT_SUPPORT
907155162Sscottl#ifndef INVARIANTS
908154941Sjhb#undef _rw_assert
909154941Sjhb#endif
910154941Sjhb
911154941Sjhb/*
912154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least
913154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this*
914154941Sjhb * thread owns an rlock.
915154941Sjhb */
916154941Sjhbvoid
917154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line)
918154941Sjhb{
919154941Sjhb
920154941Sjhb	if (panicstr != NULL)
921154941Sjhb		return;
922154941Sjhb	switch (what) {
923154941Sjhb	case RA_LOCKED:
924171052Sattilio	case RA_LOCKED | RA_RECURSED:
925171052Sattilio	case RA_LOCKED | RA_NOTRECURSED:
926154941Sjhb	case RA_RLOCKED:
927154941Sjhb#ifdef WITNESS
928167787Sjhb		witness_assert(&rw->lock_object, what, file, line);
929154941Sjhb#else
930154941Sjhb		/*
931154941Sjhb		 * If some other thread has a write lock or we have one
932154941Sjhb		 * and are asserting a read lock, fail.  Also, if no one
933154941Sjhb		 * has a lock at all, fail.
934154941Sjhb		 */
935155061Sscottl		if (rw->rw_lock == RW_UNLOCKED ||
936155061Sscottl		    (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
937157826Sjhb		    rw_wowner(rw) != curthread)))
938154941Sjhb			panic("Lock %s not %slocked @ %s:%d\n",
939167787Sjhb			    rw->lock_object.lo_name, (what == RA_RLOCKED) ?
940154941Sjhb			    "read " : "", file, line);
941171052Sattilio
942171052Sattilio		if (!(rw->rw_lock & RW_LOCK_READ)) {
943171052Sattilio			if (rw_recursed(rw)) {
944171052Sattilio				if (what & RA_NOTRECURSED)
945171052Sattilio					panic("Lock %s recursed @ %s:%d\n",
946171052Sattilio					    rw->lock_object.lo_name, file,
947171052Sattilio					    line);
948171052Sattilio			} else if (what & RA_RECURSED)
949171052Sattilio				panic("Lock %s not recursed @ %s:%d\n",
950171052Sattilio				    rw->lock_object.lo_name, file, line);
951171052Sattilio		}
952154941Sjhb#endif
953154941Sjhb		break;
954154941Sjhb	case RA_WLOCKED:
955171052Sattilio	case RA_WLOCKED | RA_RECURSED:
956171052Sattilio	case RA_WLOCKED | RA_NOTRECURSED:
957157826Sjhb		if (rw_wowner(rw) != curthread)
958154941Sjhb			panic("Lock %s not exclusively locked @ %s:%d\n",
959167787Sjhb			    rw->lock_object.lo_name, file, line);
960171052Sattilio		if (rw_recursed(rw)) {
961171052Sattilio			if (what & RA_NOTRECURSED)
962171052Sattilio				panic("Lock %s recursed @ %s:%d\n",
963171052Sattilio				    rw->lock_object.lo_name, file, line);
964171052Sattilio		} else if (what & RA_RECURSED)
965171052Sattilio			panic("Lock %s not recursed @ %s:%d\n",
966171052Sattilio			    rw->lock_object.lo_name, file, line);
967154941Sjhb		break;
968154941Sjhb	case RA_UNLOCKED:
969154941Sjhb#ifdef WITNESS
970167787Sjhb		witness_assert(&rw->lock_object, what, file, line);
971154941Sjhb#else
972154941Sjhb		/*
973154941Sjhb		 * If we hold a write lock fail.  We can't reliably check
974154941Sjhb		 * to see if we hold a read lock or not.
975154941Sjhb		 */
976157826Sjhb		if (rw_wowner(rw) == curthread)
977154941Sjhb			panic("Lock %s exclusively locked @ %s:%d\n",
978167787Sjhb			    rw->lock_object.lo_name, file, line);
979154941Sjhb#endif
980154941Sjhb		break;
981154941Sjhb	default:
982154941Sjhb		panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
983154941Sjhb		    line);
984154941Sjhb	}
985154941Sjhb}
986154941Sjhb#endif /* INVARIANT_SUPPORT */
987154941Sjhb
988154941Sjhb#ifdef DDB
989154941Sjhbvoid
990154941Sjhbdb_show_rwlock(struct lock_object *lock)
991154941Sjhb{
992154941Sjhb	struct rwlock *rw;
993154941Sjhb	struct thread *td;
994154941Sjhb
995154941Sjhb	rw = (struct rwlock *)lock;
996154941Sjhb
997154941Sjhb	db_printf(" state: ");
998154941Sjhb	if (rw->rw_lock == RW_UNLOCKED)
999154941Sjhb		db_printf("UNLOCKED\n");
1000169394Sjhb	else if (rw->rw_lock == RW_DESTROYED) {
1001169394Sjhb		db_printf("DESTROYED\n");
1002169394Sjhb		return;
1003169394Sjhb	} else if (rw->rw_lock & RW_LOCK_READ)
1004167504Sjhb		db_printf("RLOCK: %ju locks\n",
1005167504Sjhb		    (uintmax_t)(RW_READERS(rw->rw_lock)));
1006154941Sjhb	else {
1007157826Sjhb		td = rw_wowner(rw);
1008154941Sjhb		db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
1009173600Sjulian		    td->td_tid, td->td_proc->p_pid, td->td_name);
1010171052Sattilio		if (rw_recursed(rw))
1011171052Sattilio			db_printf(" recursed: %u\n", rw->rw_recurse);
1012154941Sjhb	}
1013154941Sjhb	db_printf(" waiters: ");
1014154941Sjhb	switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
1015154941Sjhb	case RW_LOCK_READ_WAITERS:
1016154941Sjhb		db_printf("readers\n");
1017154941Sjhb		break;
1018154941Sjhb	case RW_LOCK_WRITE_WAITERS:
1019154941Sjhb		db_printf("writers\n");
1020154941Sjhb		break;
1021154941Sjhb	case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
1022167492Sjhb		db_printf("readers and writers\n");
1023154941Sjhb		break;
1024154941Sjhb	default:
1025154941Sjhb		db_printf("none\n");
1026154941Sjhb		break;
1027154941Sjhb	}
1028154941Sjhb}
1029154941Sjhb
1030154941Sjhb#endif
1031