kern_rwlock.c revision 157882
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 157882 2006-04-19 21:06:52Z jhb $");
36154941Sjhb
37154941Sjhb#include "opt_ddb.h"
38154941Sjhb
39154941Sjhb#include <sys/param.h>
40154941Sjhb#include <sys/ktr.h>
41154941Sjhb#include <sys/lock.h>
42154941Sjhb#include <sys/mutex.h>
43154941Sjhb#include <sys/proc.h>
44154941Sjhb#include <sys/rwlock.h>
45154941Sjhb#include <sys/systm.h>
46154941Sjhb#include <sys/turnstile.h>
47154941Sjhb
48154941Sjhb#include <machine/cpu.h>
49154941Sjhb
50154941Sjhb#ifdef DDB
51154941Sjhb#include <ddb/ddb.h>
52154941Sjhb
53154941Sjhbstatic void	db_show_rwlock(struct lock_object *lock);
54154941Sjhb#endif
55154941Sjhb
56154941Sjhbstruct lock_class lock_class_rw = {
57154941Sjhb	"rw",
58157882Sjhb	LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
59154941Sjhb#ifdef DDB
60154941Sjhb	db_show_rwlock
61154941Sjhb#endif
62154941Sjhb};
63154941Sjhb
64157826Sjhb/*
65157826Sjhb * Return a pointer to the owning thread if the lock is write-locked or
66157826Sjhb * NULL if the lock is unlocked or read-locked.
67157826Sjhb */
68157826Sjhb#define	rw_wowner(rw)							\
69154941Sjhb	((rw)->rw_lock & RW_LOCK_READ ? NULL :				\
70154941Sjhb	    (struct thread *)RW_OWNER((rw)->rw_lock))
71154941Sjhb
72157826Sjhb/*
73157826Sjhb * Return a pointer to the owning thread for this lock who should receive
74157826Sjhb * any priority lent by threads that block on this lock.  Currently this
75157826Sjhb * is identical to rw_wowner().
76157826Sjhb */
77157826Sjhb#define	rw_owner(rw)		rw_wowner(rw)
78157826Sjhb
79154941Sjhb#ifndef INVARIANTS
80154941Sjhb#define	_rw_assert(rw, what, file, line)
81154941Sjhb#endif
82154941Sjhb
83154941Sjhbvoid
84154941Sjhbrw_init(struct rwlock *rw, const char *name)
85154941Sjhb{
86154941Sjhb
87154941Sjhb	rw->rw_lock = RW_UNLOCKED;
88154941Sjhb
89154941Sjhb	lock_init(&rw->rw_object, &lock_class_rw, name, NULL, LO_WITNESS |
90157882Sjhb	    LO_RECURSABLE | LO_UPGRADABLE);
91154941Sjhb}
92154941Sjhb
93154941Sjhbvoid
94154941Sjhbrw_destroy(struct rwlock *rw)
95154941Sjhb{
96154941Sjhb
97154941Sjhb	KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked"));
98154941Sjhb	lock_destroy(&rw->rw_object);
99154941Sjhb}
100154941Sjhb
101154941Sjhbvoid
102154941Sjhbrw_sysinit(void *arg)
103154941Sjhb{
104154941Sjhb	struct rw_args *args = arg;
105154941Sjhb
106154941Sjhb	rw_init(args->ra_rw, args->ra_desc);
107154941Sjhb}
108154941Sjhb
109154941Sjhbvoid
110154941Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line)
111154941Sjhb{
112154941Sjhb
113154941Sjhb	MPASS(curthread != NULL);
114157826Sjhb	KASSERT(rw_wowner(rw) != curthread,
115154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
116154941Sjhb	    rw->rw_object.lo_name, file, line));
117154941Sjhb	WITNESS_CHECKORDER(&rw->rw_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
118154941Sjhb	    line);
119154941Sjhb	__rw_wlock(rw, curthread, file, line);
120154941Sjhb	LOCK_LOG_LOCK("WLOCK", &rw->rw_object, 0, 0, file, line);
121154941Sjhb	WITNESS_LOCK(&rw->rw_object, LOP_EXCLUSIVE, file, line);
122154941Sjhb}
123154941Sjhb
124154941Sjhbvoid
125154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line)
126154941Sjhb{
127154941Sjhb
128154941Sjhb	MPASS(curthread != NULL);
129154941Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
130154941Sjhb	WITNESS_UNLOCK(&rw->rw_object, LOP_EXCLUSIVE, file, line);
131154941Sjhb	LOCK_LOG_LOCK("WUNLOCK", &rw->rw_object, 0, 0, file, line);
132154941Sjhb	__rw_wunlock(rw, curthread, file, line);
133154941Sjhb}
134154941Sjhb
135154941Sjhbvoid
136154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line)
137154941Sjhb{
138157851Swkoszek#ifdef SMP
139157846Sjhb	volatile struct thread *owner;
140157851Swkoszek#endif
141154941Sjhb	uintptr_t x;
142154941Sjhb
143157826Sjhb	KASSERT(rw_wowner(rw) != curthread,
144154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
145154941Sjhb	    rw->rw_object.lo_name, file, line));
146154941Sjhb	WITNESS_CHECKORDER(&rw->rw_object, LOP_NEWORDER, file, line);
147154941Sjhb
148154941Sjhb	/*
149154941Sjhb	 * Note that we don't make any attempt to try to block read
150154941Sjhb	 * locks once a writer has blocked on the lock.  The reason is
151154941Sjhb	 * that we currently allow for read locks to recurse and we
152154941Sjhb	 * don't keep track of all the holders of read locks.  Thus, if
153154941Sjhb	 * we were to block readers once a writer blocked and a reader
154154941Sjhb	 * tried to recurse on their reader lock after a writer had
155154941Sjhb	 * blocked we would end up in a deadlock since the reader would
156154941Sjhb	 * be blocked on the writer, and the writer would be blocked
157154941Sjhb	 * waiting for the reader to release its original read lock.
158154941Sjhb	 */
159154941Sjhb	for (;;) {
160154941Sjhb		/*
161154941Sjhb		 * Handle the easy case.  If no other thread has a write
162154941Sjhb		 * lock, then try to bump up the count of read locks.  Note
163154941Sjhb		 * that we have to preserve the current state of the
164154941Sjhb		 * RW_LOCK_WRITE_WAITERS flag.  If we fail to acquire a
165154941Sjhb		 * read lock, then rw_lock must have changed, so restart
166154941Sjhb		 * the loop.  Note that this handles the case of a
167154941Sjhb		 * completely unlocked rwlock since such a lock is encoded
168154941Sjhb		 * as a read lock with no waiters.
169154941Sjhb		 */
170154941Sjhb		x = rw->rw_lock;
171154941Sjhb		if (x & RW_LOCK_READ) {
172154941Sjhb
173154941Sjhb			/*
174154941Sjhb			 * The RW_LOCK_READ_WAITERS flag should only be set
175154941Sjhb			 * if another thread currently holds a write lock,
176154941Sjhb			 * and in that case RW_LOCK_READ should be clear.
177154941Sjhb			 */
178154941Sjhb			MPASS((x & RW_LOCK_READ_WAITERS) == 0);
179154941Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock, x,
180154941Sjhb			    x + RW_ONE_READER)) {
181154941Sjhb				if (LOCK_LOG_TEST(&rw->rw_object, 0))
182154941Sjhb					CTR4(KTR_LOCK,
183154941Sjhb					    "%s: %p succeed %p -> %p", __func__,
184154941Sjhb					    rw, (void *)x,
185154941Sjhb					    (void *)(x + RW_ONE_READER));
186154941Sjhb				break;
187154941Sjhb			}
188157846Sjhb			cpu_spinwait();
189154941Sjhb			continue;
190154941Sjhb		}
191154941Sjhb
192154941Sjhb		/*
193154941Sjhb		 * Okay, now it's the hard case.  Some other thread already
194154941Sjhb		 * has a write lock, so acquire the turnstile lock so we can
195154941Sjhb		 * begin the process of blocking.
196154941Sjhb		 */
197154941Sjhb		turnstile_lock(&rw->rw_object);
198154941Sjhb
199154941Sjhb		/*
200154941Sjhb		 * The lock might have been released while we spun, so
201154941Sjhb		 * recheck its state and restart the loop if there is no
202154941Sjhb		 * longer a write lock.
203154941Sjhb		 */
204154941Sjhb		x = rw->rw_lock;
205154941Sjhb		if (x & RW_LOCK_READ) {
206154941Sjhb			turnstile_release(&rw->rw_object);
207157846Sjhb			cpu_spinwait();
208154941Sjhb			continue;
209154941Sjhb		}
210154941Sjhb
211154941Sjhb		/*
212154941Sjhb		 * Ok, it's still a write lock.  If the RW_LOCK_READ_WAITERS
213154941Sjhb		 * flag is already set, then we can go ahead and block.  If
214154941Sjhb		 * it is not set then try to set it.  If we fail to set it
215154941Sjhb		 * drop the turnstile lock and restart the loop.
216154941Sjhb		 */
217157826Sjhb		if (!(x & RW_LOCK_READ_WAITERS)) {
218157826Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, x,
219157826Sjhb			    x | RW_LOCK_READ_WAITERS)) {
220157826Sjhb				turnstile_release(&rw->rw_object);
221157826Sjhb				cpu_spinwait();
222157826Sjhb				continue;
223157826Sjhb			}
224157826Sjhb			if (LOCK_LOG_TEST(&rw->rw_object, 0))
225157826Sjhb				CTR2(KTR_LOCK, "%s: %p set read waiters flag",
226157826Sjhb				    __func__, rw);
227154941Sjhb		}
228154941Sjhb
229157846Sjhb#ifdef SMP
230154941Sjhb		/*
231157846Sjhb		 * If the owner is running on another CPU, spin until
232157846Sjhb		 * the owner stops running or the state of the lock
233157846Sjhb		 * changes.
234157846Sjhb		 */
235157846Sjhb		owner = (struct thread *)RW_OWNER(x);
236157846Sjhb		if (TD_IS_RUNNING(owner)) {
237157846Sjhb			turnstile_release(&rw->rw_object);
238157846Sjhb			if (LOCK_LOG_TEST(&rw->rw_object, 0))
239157846Sjhb				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
240157846Sjhb				    __func__, rw, owner);
241157846Sjhb			while ((struct thread*)RW_OWNER(rw->rw_lock)== owner &&
242157846Sjhb			    TD_IS_RUNNING(owner))
243157846Sjhb				cpu_spinwait();
244157846Sjhb			continue;
245157846Sjhb		}
246157846Sjhb#endif
247157846Sjhb
248157846Sjhb		/*
249154941Sjhb		 * We were unable to acquire the lock and the read waiters
250154941Sjhb		 * flag is set, so we must block on the turnstile.
251154941Sjhb		 */
252154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
253154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
254154941Sjhb			    rw);
255154941Sjhb		turnstile_wait(&rw->rw_object, rw_owner(rw), TS_SHARED_QUEUE);
256154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
257154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
258154941Sjhb			    __func__, rw);
259154941Sjhb	}
260154941Sjhb
261154941Sjhb	/*
262154941Sjhb	 * TODO: acquire "owner of record" here.  Here be turnstile dragons
263154941Sjhb	 * however.  turnstiles don't like owners changing between calls to
264154941Sjhb	 * turnstile_wait() currently.
265154941Sjhb	 */
266154941Sjhb
267154941Sjhb	LOCK_LOG_LOCK("RLOCK", &rw->rw_object, 0, 0, file, line);
268154941Sjhb	WITNESS_LOCK(&rw->rw_object, 0, file, line);
269154941Sjhb}
270154941Sjhb
271154941Sjhbvoid
272154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line)
273154941Sjhb{
274154941Sjhb	struct turnstile *ts;
275154941Sjhb	uintptr_t x;
276154941Sjhb
277154941Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
278154941Sjhb	WITNESS_UNLOCK(&rw->rw_object, 0, file, line);
279154941Sjhb	LOCK_LOG_LOCK("RUNLOCK", &rw->rw_object, 0, 0, file, line);
280154941Sjhb
281154941Sjhb	/* TODO: drop "owner of record" here. */
282154941Sjhb
283154941Sjhb	for (;;) {
284154941Sjhb		/*
285154941Sjhb		 * See if there is more than one read lock held.  If so,
286154941Sjhb		 * just drop one and return.
287154941Sjhb		 */
288154941Sjhb		x = rw->rw_lock;
289154941Sjhb		if (RW_READERS(x) > 1) {
290154941Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, x,
291154941Sjhb			    x - RW_ONE_READER)) {
292154941Sjhb				if (LOCK_LOG_TEST(&rw->rw_object, 0))
293154941Sjhb					CTR4(KTR_LOCK,
294154941Sjhb					    "%s: %p succeeded %p -> %p",
295154941Sjhb					    __func__, rw, (void *)x,
296154941Sjhb					    (void *)(x - RW_ONE_READER));
297154941Sjhb				break;
298154941Sjhb			}
299154941Sjhb			continue;
300154941Sjhb		}
301154941Sjhb
302154941Sjhb		/*
303154941Sjhb		 * We should never have read waiters while at least one
304154941Sjhb		 * thread holds a read lock.  (See note above)
305154941Sjhb		 */
306154941Sjhb		KASSERT(!(x & RW_LOCK_READ_WAITERS),
307154941Sjhb		    ("%s: waiting readers", __func__));
308154941Sjhb
309154941Sjhb		/*
310154941Sjhb		 * If there aren't any waiters for a write lock, then try
311154941Sjhb		 * to drop it quickly.
312154941Sjhb		 */
313154941Sjhb		if (!(x & RW_LOCK_WRITE_WAITERS)) {
314154941Sjhb
315154941Sjhb			/*
316154941Sjhb			 * There shouldn't be any flags set and we should
317154941Sjhb			 * be the only read lock.  If we fail to release
318154941Sjhb			 * the single read lock, then another thread might
319154941Sjhb			 * have just acquired a read lock, so go back up
320154941Sjhb			 * to the multiple read locks case.
321154941Sjhb			 */
322154941Sjhb			MPASS(x == RW_READERS_LOCK(1));
323154941Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1),
324154941Sjhb			    RW_UNLOCKED)) {
325154941Sjhb				if (LOCK_LOG_TEST(&rw->rw_object, 0))
326154941Sjhb					CTR2(KTR_LOCK, "%s: %p last succeeded",
327154941Sjhb					    __func__, rw);
328154941Sjhb				break;
329154941Sjhb			}
330154941Sjhb			continue;
331154941Sjhb		}
332154941Sjhb
333154941Sjhb		/*
334154941Sjhb		 * There should just be one reader with one or more
335154941Sjhb		 * writers waiting.
336154941Sjhb		 */
337154941Sjhb		MPASS(x == (RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS));
338154941Sjhb
339154941Sjhb		/*
340154941Sjhb		 * Ok, we know we have a waiting writer and we think we
341154941Sjhb		 * are the last reader, so grab the turnstile lock.
342154941Sjhb		 */
343154941Sjhb		turnstile_lock(&rw->rw_object);
344154941Sjhb
345154941Sjhb		/*
346154941Sjhb		 * Try to drop our lock leaving the lock in a unlocked
347154941Sjhb		 * state.
348154941Sjhb		 *
349154941Sjhb		 * If you wanted to do explicit lock handoff you'd have to
350154941Sjhb		 * do it here.  You'd also want to use turnstile_signal()
351154941Sjhb		 * and you'd have to handle the race where a higher
352154941Sjhb		 * priority thread blocks on the write lock before the
353154941Sjhb		 * thread you wakeup actually runs and have the new thread
354154941Sjhb		 * "steal" the lock.  For now it's a lot simpler to just
355154941Sjhb		 * wakeup all of the waiters.
356154941Sjhb		 *
357154941Sjhb		 * As above, if we fail, then another thread might have
358154941Sjhb		 * acquired a read lock, so drop the turnstile lock and
359154941Sjhb		 * restart.
360154941Sjhb		 */
361154941Sjhb		if (!atomic_cmpset_ptr(&rw->rw_lock,
362154941Sjhb		    RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS, RW_UNLOCKED)) {
363154941Sjhb			turnstile_release(&rw->rw_object);
364154941Sjhb			continue;
365154941Sjhb		}
366154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
367154941Sjhb			CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
368154941Sjhb			    __func__, rw);
369154941Sjhb
370154941Sjhb		/*
371154941Sjhb		 * Ok.  The lock is released and all that's left is to
372154941Sjhb		 * wake up the waiters.  Note that the lock might not be
373154941Sjhb		 * free anymore, but in that case the writers will just
374154941Sjhb		 * block again if they run before the new lock holder(s)
375154941Sjhb		 * release the lock.
376154941Sjhb		 */
377154941Sjhb		ts = turnstile_lookup(&rw->rw_object);
378157846Sjhb		MPASS(ts != NULL);
379154941Sjhb		turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
380154941Sjhb		turnstile_unpend(ts, TS_SHARED_LOCK);
381154941Sjhb		break;
382154941Sjhb	}
383154941Sjhb}
384154941Sjhb
385154941Sjhb/*
386154941Sjhb * This function is called when we are unable to obtain a write lock on the
387154941Sjhb * first try.  This means that at least one other thread holds either a
388154941Sjhb * read or write lock.
389154941Sjhb */
390154941Sjhbvoid
391154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
392154941Sjhb{
393157851Swkoszek#ifdef SMP
394157846Sjhb	volatile struct thread *owner;
395157851Swkoszek#endif
396154941Sjhb	uintptr_t v;
397154941Sjhb
398154941Sjhb	if (LOCK_LOG_TEST(&rw->rw_object, 0))
399154941Sjhb		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
400154941Sjhb		    rw->rw_object.lo_name, (void *)rw->rw_lock, file, line);
401154941Sjhb
402154941Sjhb	while (!_rw_write_lock(rw, tid)) {
403154941Sjhb		turnstile_lock(&rw->rw_object);
404154941Sjhb		v = rw->rw_lock;
405154941Sjhb
406154941Sjhb		/*
407154941Sjhb		 * If the lock was released while spinning on the
408154941Sjhb		 * turnstile chain lock, try again.
409154941Sjhb		 */
410154941Sjhb		if (v == RW_UNLOCKED) {
411154941Sjhb			turnstile_release(&rw->rw_object);
412154941Sjhb			cpu_spinwait();
413154941Sjhb			continue;
414154941Sjhb		}
415154941Sjhb
416154941Sjhb		/*
417154941Sjhb		 * If the lock was released by a writer with both readers
418154941Sjhb		 * and writers waiting and a reader hasn't woken up and
419154941Sjhb		 * acquired the lock yet, rw_lock will be set to the
420154941Sjhb		 * value RW_UNLOCKED | RW_LOCK_WRITE_WAITERS.  If we see
421154941Sjhb		 * that value, try to acquire it once.  Note that we have
422154941Sjhb		 * to preserve the RW_LOCK_WRITE_WAITERS flag as there are
423154941Sjhb		 * other writers waiting still. If we fail, restart the
424154941Sjhb		 * loop.
425154941Sjhb		 */
426154941Sjhb		if (v == (RW_UNLOCKED | RW_LOCK_WRITE_WAITERS)) {
427154941Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock,
428154941Sjhb			    RW_UNLOCKED | RW_LOCK_WRITE_WAITERS,
429154941Sjhb			    tid | RW_LOCK_WRITE_WAITERS)) {
430154941Sjhb				turnstile_claim(&rw->rw_object);
431154941Sjhb				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
432154941Sjhb				    __func__, rw);
433154941Sjhb				break;
434154941Sjhb			}
435154941Sjhb			turnstile_release(&rw->rw_object);
436154941Sjhb			cpu_spinwait();
437154941Sjhb			continue;
438154941Sjhb		}
439154941Sjhb
440154941Sjhb		/*
441154941Sjhb		 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
442154941Sjhb		 * set it.  If we fail to set it, then loop back and try
443154941Sjhb		 * again.
444154941Sjhb		 */
445157826Sjhb		if (!(v & RW_LOCK_WRITE_WAITERS)) {
446157826Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
447157826Sjhb			    v | RW_LOCK_WRITE_WAITERS)) {
448157826Sjhb				turnstile_release(&rw->rw_object);
449157826Sjhb				cpu_spinwait();
450157826Sjhb				continue;
451157826Sjhb			}
452157826Sjhb			if (LOCK_LOG_TEST(&rw->rw_object, 0))
453157826Sjhb				CTR2(KTR_LOCK, "%s: %p set write waiters flag",
454157826Sjhb				    __func__, rw);
455154941Sjhb		}
456154941Sjhb
457157846Sjhb#ifdef SMP
458157846Sjhb		/*
459157846Sjhb		 * If the lock is write locked and the owner is
460157846Sjhb		 * running on another CPU, spin until the owner stops
461157846Sjhb		 * running or the state of the lock changes.
462157846Sjhb		 */
463157846Sjhb		owner = (struct thread *)RW_OWNER(v);
464157846Sjhb		if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
465157846Sjhb			turnstile_release(&rw->rw_object);
466157846Sjhb			if (LOCK_LOG_TEST(&rw->rw_object, 0))
467157846Sjhb				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
468157846Sjhb				    __func__, rw, owner);
469157846Sjhb			while ((struct thread*)RW_OWNER(rw->rw_lock)== owner &&
470157846Sjhb			    TD_IS_RUNNING(owner))
471157846Sjhb				cpu_spinwait();
472157846Sjhb			continue;
473157846Sjhb		}
474157846Sjhb#endif
475154941Sjhb
476154941Sjhb		/*
477154941Sjhb		 * We were unable to acquire the lock and the write waiters
478154941Sjhb		 * flag is set, so we must block on the turnstile.
479154941Sjhb		 */
480154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
481154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
482154941Sjhb			    rw);
483154941Sjhb		turnstile_wait(&rw->rw_object, rw_owner(rw),
484154941Sjhb		    TS_EXCLUSIVE_QUEUE);
485154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
486154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
487154941Sjhb			    __func__, rw);
488154941Sjhb	}
489154941Sjhb}
490154941Sjhb
491154941Sjhb/*
492154941Sjhb * This function is called if the first try at releasing a write lock failed.
493154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at
494154941Sjhb * least one thread is waiting on this lock.
495154941Sjhb */
496154941Sjhbvoid
497154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
498154941Sjhb{
499154941Sjhb	struct turnstile *ts;
500154941Sjhb	uintptr_t v;
501154941Sjhb	int queue;
502154941Sjhb
503154941Sjhb	KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
504154941Sjhb	    ("%s: neither of the waiter flags are set", __func__));
505154941Sjhb
506154941Sjhb	if (LOCK_LOG_TEST(&rw->rw_object, 0))
507154941Sjhb		CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
508154941Sjhb
509154941Sjhb	turnstile_lock(&rw->rw_object);
510154941Sjhb	ts = turnstile_lookup(&rw->rw_object);
511154941Sjhb
512157846Sjhb#ifdef SMP
513157846Sjhb	/*
514157846Sjhb	 * There might not be a turnstile for this lock if all of
515157846Sjhb	 * the waiters are adaptively spinning.  In that case, just
516157846Sjhb	 * reset the lock to the unlocked state and return.
517157846Sjhb	 */
518157846Sjhb	if (ts == NULL) {
519157846Sjhb		atomic_store_rel_ptr(&rw->rw_lock, RW_UNLOCKED);
520157846Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
521157846Sjhb			CTR2(KTR_LOCK, "%s: %p no sleepers", __func__, rw);
522157846Sjhb		turnstile_release(&rw->rw_object);
523157846Sjhb		return;
524157846Sjhb	}
525157846Sjhb#else
526154941Sjhb	MPASS(ts != NULL);
527157846Sjhb#endif
528154941Sjhb
529154941Sjhb	/*
530154941Sjhb	 * Use the same algo as sx locks for now.  Prefer waking up shared
531154941Sjhb	 * waiters if we have any over writers.  This is probably not ideal.
532154941Sjhb	 *
533154941Sjhb	 * 'v' is the value we are going to write back to rw_lock.  If we
534154941Sjhb	 * have waiters on both queues, we need to preserve the state of
535154941Sjhb	 * the waiter flag for the queue we don't wake up.  For now this is
536154941Sjhb	 * hardcoded for the algorithm mentioned above.
537154941Sjhb	 *
538154941Sjhb	 * In the case of both readers and writers waiting we wakeup the
539154941Sjhb	 * readers but leave the RW_LOCK_WRITE_WAITERS flag set.  If a
540154941Sjhb	 * new writer comes in before a reader it will claim the lock up
541154941Sjhb	 * above.  There is probably a potential priority inversion in
542154941Sjhb	 * there that could be worked around either by waking both queues
543154941Sjhb	 * of waiters or doing some complicated lock handoff gymnastics.
544157846Sjhb	 *
545157846Sjhb	 * Note that in the SMP case, if both flags are set, there might
546157846Sjhb	 * not be any actual writers on the turnstile as they might all
547157846Sjhb	 * be spinning.  In that case, we don't want to preserve the
548157846Sjhb	 * RW_LOCK_WRITE_WAITERS flag as the turnstile is going to go
549157846Sjhb	 * away once we wakeup all the readers.
550154941Sjhb	 */
551157846Sjhb	v = RW_UNLOCKED;
552154941Sjhb	if (rw->rw_lock & RW_LOCK_READ_WAITERS) {
553154941Sjhb		queue = TS_SHARED_QUEUE;
554157846Sjhb#ifdef SMP
555157846Sjhb		if (rw->rw_lock & RW_LOCK_WRITE_WAITERS &&
556157846Sjhb		    !turnstile_empty(ts, TS_EXCLUSIVE_QUEUE))
557157846Sjhb			v |= RW_LOCK_WRITE_WAITERS;
558157846Sjhb#else
559157846Sjhb		v |= (rw->rw_lock & RW_LOCK_WRITE_WAITERS);
560157846Sjhb#endif
561157846Sjhb	} else
562154941Sjhb		queue = TS_EXCLUSIVE_QUEUE;
563157846Sjhb
564157846Sjhb#ifdef SMP
565157846Sjhb	/*
566157846Sjhb	 * We have to make sure that we actually have waiters to
567157846Sjhb	 * wakeup.  If they are all spinning, then we just need to
568157846Sjhb	 * disown the turnstile and return.
569157846Sjhb	 */
570157846Sjhb	if (turnstile_empty(ts, queue)) {
571157846Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
572157846Sjhb			CTR2(KTR_LOCK, "%s: %p no sleepers 2", __func__, rw);
573157846Sjhb		atomic_store_rel_ptr(&rw->rw_lock, v);
574157846Sjhb		turnstile_disown(ts);
575157846Sjhb		return;
576154941Sjhb	}
577157846Sjhb#endif
578157846Sjhb
579157846Sjhb	/* Wake up all waiters for the specific queue. */
580154941Sjhb	if (LOCK_LOG_TEST(&rw->rw_object, 0))
581154941Sjhb		CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
582154941Sjhb		    queue == TS_SHARED_QUEUE ? "read" : "write");
583154941Sjhb	turnstile_broadcast(ts, queue);
584154941Sjhb	atomic_store_rel_ptr(&rw->rw_lock, v);
585154941Sjhb	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
586154941Sjhb}
587154941Sjhb
588157882Sjhb/*
589157882Sjhb * Attempt to do a non-blocking upgrade from a read lock to a write
590157882Sjhb * lock.  This will only succeed if this thread holds a single read
591157882Sjhb * lock.  Returns true if the upgrade succeeded and false otherwise.
592157882Sjhb */
593157882Sjhbint
594157882Sjhb_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
595157882Sjhb{
596157882Sjhb	uintptr_t v, tid;
597157882Sjhb	int success;
598157882Sjhb
599157882Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
600157882Sjhb
601157882Sjhb	/*
602157882Sjhb	 * Attempt to switch from one reader to a writer.  If there
603157882Sjhb	 * are any write waiters, then we will have to lock the
604157882Sjhb	 * turnstile first to prevent races with another writer
605157882Sjhb	 * calling turnstile_wait() before we have claimed this
606157882Sjhb	 * turnstile.  So, do the simple case of no waiters first.
607157882Sjhb	 */
608157882Sjhb	tid = (uintptr_t)curthread;
609157882Sjhb	if (!(rw->rw_lock & RW_LOCK_WRITE_WAITERS)) {
610157882Sjhb		success = atomic_cmpset_acq_ptr(&rw->rw_lock,
611157882Sjhb		    RW_READERS_LOCK(1), tid);
612157882Sjhb		goto out;
613157882Sjhb	}
614157882Sjhb
615157882Sjhb	/*
616157882Sjhb	 * Ok, we think we have write waiters, so lock the
617157882Sjhb	 * turnstile.
618157882Sjhb	 */
619157882Sjhb	turnstile_lock(&rw->rw_object);
620157882Sjhb
621157882Sjhb	/*
622157882Sjhb	 * Try to switch from one reader to a writer again.  This time
623157882Sjhb	 * we honor the current state of the RW_LOCK_WRITE_WAITERS
624157882Sjhb	 * flag.  If we obtain the lock with the flag set, then claim
625157882Sjhb	 * ownership of the turnstile.  In the SMP case it is possible
626157882Sjhb	 * for there to not be an associated turnstile even though there
627157882Sjhb	 * are waiters if all of the waiters are spinning.
628157882Sjhb	 */
629157882Sjhb	v = rw->rw_lock & RW_LOCK_WRITE_WAITERS;
630157882Sjhb	success = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
631157882Sjhb	    tid | v);
632157882Sjhb#ifdef SMP
633157882Sjhb	if (success && v && turnstile_lookup(&rw->rw_object) != NULL)
634157882Sjhb#else
635157882Sjhb	if (success && v)
636157882Sjhb#endif
637157882Sjhb		turnstile_claim(&rw->rw_object);
638157882Sjhb	else
639157882Sjhb		turnstile_release(&rw->rw_object);
640157882Sjhbout:
641157882Sjhb	LOCK_LOG_TRY("WUPGRADE", &rw->rw_object, 0, success, file, line);
642157882Sjhb	if (success)
643157882Sjhb		WITNESS_UPGRADE(&rw->rw_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
644157882Sjhb		    file, line);
645157882Sjhb	return (success);
646157882Sjhb}
647157882Sjhb
648157882Sjhb/*
649157882Sjhb * Downgrade a write lock into a single read lock.
650157882Sjhb */
651157882Sjhbvoid
652157882Sjhb_rw_downgrade(struct rwlock *rw, const char *file, int line)
653157882Sjhb{
654157882Sjhb	struct turnstile *ts;
655157882Sjhb	uintptr_t tid, v;
656157882Sjhb
657157882Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
658157882Sjhb
659157882Sjhb	WITNESS_DOWNGRADE(&rw->rw_object, 0, file, line);
660157882Sjhb
661157882Sjhb	/*
662157882Sjhb	 * Convert from a writer to a single reader.  First we handle
663157882Sjhb	 * the easy case with no waiters.  If there are any waiters, we
664157882Sjhb	 * lock the turnstile, "disown" the lock, and awaken any read
665157882Sjhb	 * waiters.
666157882Sjhb	 */
667157882Sjhb	tid = (uintptr_t)curthread;
668157882Sjhb	if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
669157882Sjhb		goto out;
670157882Sjhb
671157882Sjhb	/*
672157882Sjhb	 * Ok, we think we have waiters, so lock the turnstile so we can
673157882Sjhb	 * read the waiter flags without any races.
674157882Sjhb	 */
675157882Sjhb	turnstile_lock(&rw->rw_object);
676157882Sjhb	v = rw->rw_lock;
677157882Sjhb	MPASS(v & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS));
678157882Sjhb
679157882Sjhb	/*
680157882Sjhb	 * Downgrade from a write lock while preserving
681157882Sjhb	 * RW_LOCK_WRITE_WAITERS and give up ownership of the
682157882Sjhb	 * turnstile.  If there are any read waiters, wake them up.
683157882Sjhb	 *
684157882Sjhb	 * For SMP, we have to allow for the fact that all of the
685157882Sjhb	 * read waiters might be spinning.  In that case, act as if
686157882Sjhb	 * RW_LOCK_READ_WAITERS is not set.  Also, only preserve
687157882Sjhb	 * the RW_LOCK_WRITE_WAITERS flag if at least one writer is
688157882Sjhb	 * blocked on the turnstile.
689157882Sjhb	 */
690157882Sjhb	ts = turnstile_lookup(&rw->rw_object);
691157882Sjhb#ifdef SMP
692157882Sjhb	if (ts == NULL)
693157882Sjhb		v &= ~(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS);
694157882Sjhb	else if (v & RW_LOCK_READ_WAITERS &&
695157882Sjhb	    turnstile_empty(ts, TS_SHARED_QUEUE))
696157882Sjhb		v &= ~RW_LOCK_READ_WAITERS;
697157882Sjhb	else if (v & RW_LOCK_WRITE_WAITERS &&
698157882Sjhb	    turnstile_empty(ts, TS_EXCLUSIVE_QUEUE))
699157882Sjhb		v &= ~RW_LOCK_WRITE_WAITERS;
700157882Sjhb#else
701157882Sjhb	MPASS(ts != NULL);
702157882Sjhb#endif
703157882Sjhb	if (v & RW_LOCK_READ_WAITERS)
704157882Sjhb		turnstile_broadcast(ts, TS_SHARED_QUEUE);
705157882Sjhb	atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) |
706157882Sjhb	    (v & RW_LOCK_WRITE_WAITERS));
707157882Sjhb	if (v & RW_LOCK_READ_WAITERS)
708157882Sjhb		turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
709157882Sjhb#ifdef SMP
710157882Sjhb	else if (ts == NULL)
711157882Sjhb		turnstile_release(&rw->rw_object);
712157882Sjhb#endif
713157882Sjhb	else
714157882Sjhb		turnstile_disown(ts);
715157882Sjhbout:
716157882Sjhb	LOCK_LOG_LOCK("WDOWNGRADE", &rw->rw_object, 0, 0, file, line);
717157882Sjhb}
718157882Sjhb
719154941Sjhb#ifdef INVARIANT_SUPPORT
720155162Sscottl#ifndef INVARIANTS
721154941Sjhb#undef _rw_assert
722154941Sjhb#endif
723154941Sjhb
724154941Sjhb/*
725154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least
726154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this*
727154941Sjhb * thread owns an rlock.
728154941Sjhb */
729154941Sjhbvoid
730154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line)
731154941Sjhb{
732154941Sjhb
733154941Sjhb	if (panicstr != NULL)
734154941Sjhb		return;
735154941Sjhb	switch (what) {
736154941Sjhb	case RA_LOCKED:
737154941Sjhb	case RA_RLOCKED:
738154941Sjhb#ifdef WITNESS
739154941Sjhb		witness_assert(&rw->rw_object, what, file, line);
740154941Sjhb#else
741154941Sjhb		/*
742154941Sjhb		 * If some other thread has a write lock or we have one
743154941Sjhb		 * and are asserting a read lock, fail.  Also, if no one
744154941Sjhb		 * has a lock at all, fail.
745154941Sjhb		 */
746155061Sscottl		if (rw->rw_lock == RW_UNLOCKED ||
747155061Sscottl		    (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
748157826Sjhb		    rw_wowner(rw) != curthread)))
749154941Sjhb			panic("Lock %s not %slocked @ %s:%d\n",
750155012Sscottl			    rw->rw_object.lo_name, (what == RA_RLOCKED) ?
751154941Sjhb			    "read " : "", file, line);
752154941Sjhb#endif
753154941Sjhb		break;
754154941Sjhb	case RA_WLOCKED:
755157826Sjhb		if (rw_wowner(rw) != curthread)
756154941Sjhb			panic("Lock %s not exclusively locked @ %s:%d\n",
757154941Sjhb			    rw->rw_object.lo_name, file, line);
758154941Sjhb		break;
759154941Sjhb	case RA_UNLOCKED:
760154941Sjhb#ifdef WITNESS
761154941Sjhb		witness_assert(&rw->rw_object, what, file, line);
762154941Sjhb#else
763154941Sjhb		/*
764154941Sjhb		 * If we hold a write lock fail.  We can't reliably check
765154941Sjhb		 * to see if we hold a read lock or not.
766154941Sjhb		 */
767157826Sjhb		if (rw_wowner(rw) == curthread)
768154941Sjhb			panic("Lock %s exclusively locked @ %s:%d\n",
769154941Sjhb			    rw->rw_object.lo_name, file, line);
770154941Sjhb#endif
771154941Sjhb		break;
772154941Sjhb	default:
773154941Sjhb		panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
774154941Sjhb		    line);
775154941Sjhb	}
776154941Sjhb}
777154941Sjhb#endif /* INVARIANT_SUPPORT */
778154941Sjhb
779154941Sjhb#ifdef DDB
780154941Sjhbvoid
781154941Sjhbdb_show_rwlock(struct lock_object *lock)
782154941Sjhb{
783154941Sjhb	struct rwlock *rw;
784154941Sjhb	struct thread *td;
785154941Sjhb
786154941Sjhb	rw = (struct rwlock *)lock;
787154941Sjhb
788154941Sjhb	db_printf(" state: ");
789154941Sjhb	if (rw->rw_lock == RW_UNLOCKED)
790154941Sjhb		db_printf("UNLOCKED\n");
791154941Sjhb	else if (rw->rw_lock & RW_LOCK_READ)
792154973Smlaier		db_printf("RLOCK: %jd locks\n",
793154973Smlaier		    (intmax_t)(RW_READERS(rw->rw_lock)));
794154941Sjhb	else {
795157826Sjhb		td = rw_wowner(rw);
796154941Sjhb		db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
797154941Sjhb		    td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
798154941Sjhb	}
799154941Sjhb	db_printf(" waiters: ");
800154941Sjhb	switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
801154941Sjhb	case RW_LOCK_READ_WAITERS:
802154941Sjhb		db_printf("readers\n");
803154941Sjhb		break;
804154941Sjhb	case RW_LOCK_WRITE_WAITERS:
805154941Sjhb		db_printf("writers\n");
806154941Sjhb		break;
807154941Sjhb	case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
808154941Sjhb		db_printf("readers and waiters\n");
809154941Sjhb		break;
810154941Sjhb	default:
811154941Sjhb		db_printf("none\n");
812154941Sjhb		break;
813154941Sjhb	}
814154941Sjhb}
815154941Sjhb
816154941Sjhb#endif
817