kern_rwlock.c revision 164159
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 164159 2006-11-11 03:18:07Z kmacy $");
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>
47164159Skmacy#include <sys/lock_profile.h>
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
89164159Skmacy	lock_profile_object_init(&rw->rw_object, name);
90154941Sjhb	lock_init(&rw->rw_object, &lock_class_rw, name, NULL, LO_WITNESS |
91157882Sjhb	    LO_RECURSABLE | LO_UPGRADABLE);
92154941Sjhb}
93154941Sjhb
94154941Sjhbvoid
95154941Sjhbrw_destroy(struct rwlock *rw)
96154941Sjhb{
97154941Sjhb
98154941Sjhb	KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked"));
99164159Skmacy	lock_profile_object_destroy(&rw->rw_object);
100154941Sjhb	lock_destroy(&rw->rw_object);
101154941Sjhb}
102154941Sjhb
103154941Sjhbvoid
104154941Sjhbrw_sysinit(void *arg)
105154941Sjhb{
106154941Sjhb	struct rw_args *args = arg;
107154941Sjhb
108154941Sjhb	rw_init(args->ra_rw, args->ra_desc);
109154941Sjhb}
110154941Sjhb
111154941Sjhbvoid
112154941Sjhb_rw_wlock(struct rwlock *rw, const char *file, int line)
113154941Sjhb{
114164159Skmacy	uint64_t waitstart;
115154941Sjhb
116154941Sjhb	MPASS(curthread != NULL);
117157826Sjhb	KASSERT(rw_wowner(rw) != curthread,
118154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
119154941Sjhb	    rw->rw_object.lo_name, file, line));
120154941Sjhb	WITNESS_CHECKORDER(&rw->rw_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
121154941Sjhb	    line);
122164159Skmacy	lock_profile_waitstart(&waitstart);
123154941Sjhb	__rw_wlock(rw, curthread, file, line);
124164159Skmacy	lock_profile_obtain_lock_success(&rw->rw_object, waitstart, file, line);
125154941Sjhb	LOCK_LOG_LOCK("WLOCK", &rw->rw_object, 0, 0, file, line);
126154941Sjhb	WITNESS_LOCK(&rw->rw_object, LOP_EXCLUSIVE, file, line);
127160771Sjhb	curthread->td_locks++;
128154941Sjhb}
129154941Sjhb
130154941Sjhbvoid
131154941Sjhb_rw_wunlock(struct rwlock *rw, const char *file, int line)
132154941Sjhb{
133154941Sjhb
134154941Sjhb	MPASS(curthread != NULL);
135154941Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
136160771Sjhb	curthread->td_locks--;
137154941Sjhb	WITNESS_UNLOCK(&rw->rw_object, LOP_EXCLUSIVE, file, line);
138154941Sjhb	LOCK_LOG_LOCK("WUNLOCK", &rw->rw_object, 0, 0, file, line);
139164159Skmacy	lock_profile_release_lock(&rw->rw_object);
140154941Sjhb	__rw_wunlock(rw, curthread, file, line);
141154941Sjhb}
142154941Sjhb
143154941Sjhbvoid
144154941Sjhb_rw_rlock(struct rwlock *rw, const char *file, int line)
145154941Sjhb{
146157851Swkoszek#ifdef SMP
147157846Sjhb	volatile struct thread *owner;
148157851Swkoszek#endif
149164159Skmacy	uint64_t waitstart;
150164159Skmacy	int contested;
151154941Sjhb	uintptr_t x;
152154941Sjhb
153157826Sjhb	KASSERT(rw_wowner(rw) != curthread,
154154941Sjhb	    ("%s (%s): wlock already held @ %s:%d", __func__,
155154941Sjhb	    rw->rw_object.lo_name, file, line));
156154941Sjhb	WITNESS_CHECKORDER(&rw->rw_object, LOP_NEWORDER, file, line);
157154941Sjhb
158154941Sjhb	/*
159154941Sjhb	 * Note that we don't make any attempt to try to block read
160154941Sjhb	 * locks once a writer has blocked on the lock.  The reason is
161154941Sjhb	 * that we currently allow for read locks to recurse and we
162154941Sjhb	 * don't keep track of all the holders of read locks.  Thus, if
163154941Sjhb	 * we were to block readers once a writer blocked and a reader
164154941Sjhb	 * tried to recurse on their reader lock after a writer had
165154941Sjhb	 * blocked we would end up in a deadlock since the reader would
166154941Sjhb	 * be blocked on the writer, and the writer would be blocked
167154941Sjhb	 * waiting for the reader to release its original read lock.
168154941Sjhb	 */
169164159Skmacy	lock_profile_waitstart(&waitstart);
170154941Sjhb	for (;;) {
171154941Sjhb		/*
172154941Sjhb		 * Handle the easy case.  If no other thread has a write
173154941Sjhb		 * lock, then try to bump up the count of read locks.  Note
174154941Sjhb		 * that we have to preserve the current state of the
175154941Sjhb		 * RW_LOCK_WRITE_WAITERS flag.  If we fail to acquire a
176154941Sjhb		 * read lock, then rw_lock must have changed, so restart
177154941Sjhb		 * the loop.  Note that this handles the case of a
178154941Sjhb		 * completely unlocked rwlock since such a lock is encoded
179154941Sjhb		 * as a read lock with no waiters.
180154941Sjhb		 */
181154941Sjhb		x = rw->rw_lock;
182154941Sjhb		if (x & RW_LOCK_READ) {
183154941Sjhb
184154941Sjhb			/*
185154941Sjhb			 * The RW_LOCK_READ_WAITERS flag should only be set
186154941Sjhb			 * if another thread currently holds a write lock,
187154941Sjhb			 * and in that case RW_LOCK_READ should be clear.
188154941Sjhb			 */
189154941Sjhb			MPASS((x & RW_LOCK_READ_WAITERS) == 0);
190154941Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock, x,
191154941Sjhb			    x + RW_ONE_READER)) {
192164159Skmacy				lock_profile_obtain_lock_success(&rw->rw_object, waitstart, file, line);
193154941Sjhb				if (LOCK_LOG_TEST(&rw->rw_object, 0))
194154941Sjhb					CTR4(KTR_LOCK,
195154941Sjhb					    "%s: %p succeed %p -> %p", __func__,
196154941Sjhb					    rw, (void *)x,
197154941Sjhb					    (void *)(x + RW_ONE_READER));
198154941Sjhb				break;
199154941Sjhb			}
200157846Sjhb			cpu_spinwait();
201164159Skmacy			lock_profile_obtain_lock_failed(&rw->rw_object, &contested);
202154941Sjhb			continue;
203154941Sjhb		}
204154941Sjhb
205154941Sjhb		/*
206154941Sjhb		 * Okay, now it's the hard case.  Some other thread already
207154941Sjhb		 * has a write lock, so acquire the turnstile lock so we can
208154941Sjhb		 * begin the process of blocking.
209154941Sjhb		 */
210154941Sjhb		turnstile_lock(&rw->rw_object);
211154941Sjhb
212154941Sjhb		/*
213154941Sjhb		 * The lock might have been released while we spun, so
214154941Sjhb		 * recheck its state and restart the loop if there is no
215154941Sjhb		 * longer a write lock.
216154941Sjhb		 */
217154941Sjhb		x = rw->rw_lock;
218154941Sjhb		if (x & RW_LOCK_READ) {
219154941Sjhb			turnstile_release(&rw->rw_object);
220157846Sjhb			cpu_spinwait();
221154941Sjhb			continue;
222154941Sjhb		}
223154941Sjhb
224154941Sjhb		/*
225154941Sjhb		 * Ok, it's still a write lock.  If the RW_LOCK_READ_WAITERS
226154941Sjhb		 * flag is already set, then we can go ahead and block.  If
227154941Sjhb		 * it is not set then try to set it.  If we fail to set it
228154941Sjhb		 * drop the turnstile lock and restart the loop.
229154941Sjhb		 */
230157826Sjhb		if (!(x & RW_LOCK_READ_WAITERS)) {
231157826Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, x,
232157826Sjhb			    x | RW_LOCK_READ_WAITERS)) {
233157826Sjhb				turnstile_release(&rw->rw_object);
234157826Sjhb				cpu_spinwait();
235157826Sjhb				continue;
236157826Sjhb			}
237157826Sjhb			if (LOCK_LOG_TEST(&rw->rw_object, 0))
238157826Sjhb				CTR2(KTR_LOCK, "%s: %p set read waiters flag",
239157826Sjhb				    __func__, rw);
240154941Sjhb		}
241154941Sjhb
242157846Sjhb#ifdef SMP
243154941Sjhb		/*
244157846Sjhb		 * If the owner is running on another CPU, spin until
245157846Sjhb		 * the owner stops running or the state of the lock
246157846Sjhb		 * changes.
247157846Sjhb		 */
248157846Sjhb		owner = (struct thread *)RW_OWNER(x);
249157846Sjhb		if (TD_IS_RUNNING(owner)) {
250164159Skmacy			lock_profile_obtain_lock_failed(&rw->rw_object, &contested);
251157846Sjhb			turnstile_release(&rw->rw_object);
252157846Sjhb			if (LOCK_LOG_TEST(&rw->rw_object, 0))
253157846Sjhb				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
254157846Sjhb				    __func__, rw, owner);
255157846Sjhb			while ((struct thread*)RW_OWNER(rw->rw_lock)== owner &&
256157846Sjhb			    TD_IS_RUNNING(owner))
257157846Sjhb				cpu_spinwait();
258157846Sjhb			continue;
259157846Sjhb		}
260157846Sjhb#endif
261157846Sjhb
262157846Sjhb		/*
263154941Sjhb		 * We were unable to acquire the lock and the read waiters
264154941Sjhb		 * flag is set, so we must block on the turnstile.
265154941Sjhb		 */
266154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
267154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
268154941Sjhb			    rw);
269154941Sjhb		turnstile_wait(&rw->rw_object, rw_owner(rw), TS_SHARED_QUEUE);
270154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
271154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
272154941Sjhb			    __func__, rw);
273154941Sjhb	}
274154941Sjhb
275154941Sjhb	/*
276154941Sjhb	 * TODO: acquire "owner of record" here.  Here be turnstile dragons
277154941Sjhb	 * however.  turnstiles don't like owners changing between calls to
278154941Sjhb	 * turnstile_wait() currently.
279154941Sjhb	 */
280154941Sjhb
281154941Sjhb	LOCK_LOG_LOCK("RLOCK", &rw->rw_object, 0, 0, file, line);
282154941Sjhb	WITNESS_LOCK(&rw->rw_object, 0, file, line);
283160771Sjhb	curthread->td_locks++;
284154941Sjhb}
285154941Sjhb
286154941Sjhbvoid
287154941Sjhb_rw_runlock(struct rwlock *rw, const char *file, int line)
288154941Sjhb{
289154941Sjhb	struct turnstile *ts;
290154941Sjhb	uintptr_t x;
291154941Sjhb
292154941Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
293160771Sjhb	curthread->td_locks--;
294154941Sjhb	WITNESS_UNLOCK(&rw->rw_object, 0, file, line);
295154941Sjhb	LOCK_LOG_LOCK("RUNLOCK", &rw->rw_object, 0, 0, file, line);
296154941Sjhb
297154941Sjhb	/* TODO: drop "owner of record" here. */
298154941Sjhb
299154941Sjhb	for (;;) {
300154941Sjhb		/*
301154941Sjhb		 * See if there is more than one read lock held.  If so,
302154941Sjhb		 * just drop one and return.
303154941Sjhb		 */
304154941Sjhb		x = rw->rw_lock;
305154941Sjhb		if (RW_READERS(x) > 1) {
306154941Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, x,
307154941Sjhb			    x - RW_ONE_READER)) {
308154941Sjhb				if (LOCK_LOG_TEST(&rw->rw_object, 0))
309154941Sjhb					CTR4(KTR_LOCK,
310154941Sjhb					    "%s: %p succeeded %p -> %p",
311154941Sjhb					    __func__, rw, (void *)x,
312154941Sjhb					    (void *)(x - RW_ONE_READER));
313154941Sjhb				break;
314154941Sjhb			}
315154941Sjhb			continue;
316164159Skmacy		} else
317164159Skmacy			lock_profile_release_lock(&rw->rw_object);
318154941Sjhb
319164159Skmacy
320154941Sjhb		/*
321154941Sjhb		 * We should never have read waiters while at least one
322154941Sjhb		 * thread holds a read lock.  (See note above)
323154941Sjhb		 */
324154941Sjhb		KASSERT(!(x & RW_LOCK_READ_WAITERS),
325154941Sjhb		    ("%s: waiting readers", __func__));
326154941Sjhb
327154941Sjhb		/*
328154941Sjhb		 * If there aren't any waiters for a write lock, then try
329154941Sjhb		 * to drop it quickly.
330154941Sjhb		 */
331154941Sjhb		if (!(x & RW_LOCK_WRITE_WAITERS)) {
332154941Sjhb
333154941Sjhb			/*
334154941Sjhb			 * There shouldn't be any flags set and we should
335154941Sjhb			 * be the only read lock.  If we fail to release
336154941Sjhb			 * the single read lock, then another thread might
337154941Sjhb			 * have just acquired a read lock, so go back up
338154941Sjhb			 * to the multiple read locks case.
339154941Sjhb			 */
340154941Sjhb			MPASS(x == RW_READERS_LOCK(1));
341154941Sjhb			if (atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1),
342154941Sjhb			    RW_UNLOCKED)) {
343154941Sjhb				if (LOCK_LOG_TEST(&rw->rw_object, 0))
344154941Sjhb					CTR2(KTR_LOCK, "%s: %p last succeeded",
345154941Sjhb					    __func__, rw);
346154941Sjhb				break;
347154941Sjhb			}
348154941Sjhb			continue;
349154941Sjhb		}
350154941Sjhb
351154941Sjhb		/*
352154941Sjhb		 * There should just be one reader with one or more
353154941Sjhb		 * writers waiting.
354154941Sjhb		 */
355154941Sjhb		MPASS(x == (RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS));
356154941Sjhb
357154941Sjhb		/*
358154941Sjhb		 * Ok, we know we have a waiting writer and we think we
359154941Sjhb		 * are the last reader, so grab the turnstile lock.
360154941Sjhb		 */
361154941Sjhb		turnstile_lock(&rw->rw_object);
362154941Sjhb
363154941Sjhb		/*
364154941Sjhb		 * Try to drop our lock leaving the lock in a unlocked
365154941Sjhb		 * state.
366154941Sjhb		 *
367154941Sjhb		 * If you wanted to do explicit lock handoff you'd have to
368154941Sjhb		 * do it here.  You'd also want to use turnstile_signal()
369154941Sjhb		 * and you'd have to handle the race where a higher
370154941Sjhb		 * priority thread blocks on the write lock before the
371154941Sjhb		 * thread you wakeup actually runs and have the new thread
372154941Sjhb		 * "steal" the lock.  For now it's a lot simpler to just
373154941Sjhb		 * wakeup all of the waiters.
374154941Sjhb		 *
375154941Sjhb		 * As above, if we fail, then another thread might have
376154941Sjhb		 * acquired a read lock, so drop the turnstile lock and
377154941Sjhb		 * restart.
378154941Sjhb		 */
379154941Sjhb		if (!atomic_cmpset_ptr(&rw->rw_lock,
380154941Sjhb		    RW_READERS_LOCK(1) | RW_LOCK_WRITE_WAITERS, RW_UNLOCKED)) {
381154941Sjhb			turnstile_release(&rw->rw_object);
382154941Sjhb			continue;
383154941Sjhb		}
384154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
385154941Sjhb			CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
386154941Sjhb			    __func__, rw);
387154941Sjhb
388154941Sjhb		/*
389154941Sjhb		 * Ok.  The lock is released and all that's left is to
390154941Sjhb		 * wake up the waiters.  Note that the lock might not be
391154941Sjhb		 * free anymore, but in that case the writers will just
392154941Sjhb		 * block again if they run before the new lock holder(s)
393154941Sjhb		 * release the lock.
394154941Sjhb		 */
395154941Sjhb		ts = turnstile_lookup(&rw->rw_object);
396157846Sjhb		MPASS(ts != NULL);
397154941Sjhb		turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
398154941Sjhb		turnstile_unpend(ts, TS_SHARED_LOCK);
399154941Sjhb		break;
400154941Sjhb	}
401154941Sjhb}
402154941Sjhb
403154941Sjhb/*
404154941Sjhb * This function is called when we are unable to obtain a write lock on the
405154941Sjhb * first try.  This means that at least one other thread holds either a
406154941Sjhb * read or write lock.
407154941Sjhb */
408154941Sjhbvoid
409154941Sjhb_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
410154941Sjhb{
411157851Swkoszek#ifdef SMP
412157846Sjhb	volatile struct thread *owner;
413157851Swkoszek#endif
414164159Skmacy	int contested;
415154941Sjhb	uintptr_t v;
416154941Sjhb
417154941Sjhb	if (LOCK_LOG_TEST(&rw->rw_object, 0))
418154941Sjhb		CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
419154941Sjhb		    rw->rw_object.lo_name, (void *)rw->rw_lock, file, line);
420154941Sjhb
421154941Sjhb	while (!_rw_write_lock(rw, tid)) {
422154941Sjhb		turnstile_lock(&rw->rw_object);
423154941Sjhb		v = rw->rw_lock;
424154941Sjhb
425154941Sjhb		/*
426154941Sjhb		 * If the lock was released while spinning on the
427154941Sjhb		 * turnstile chain lock, try again.
428154941Sjhb		 */
429154941Sjhb		if (v == RW_UNLOCKED) {
430154941Sjhb			turnstile_release(&rw->rw_object);
431154941Sjhb			cpu_spinwait();
432154941Sjhb			continue;
433154941Sjhb		}
434154941Sjhb
435154941Sjhb		/*
436154941Sjhb		 * If the lock was released by a writer with both readers
437154941Sjhb		 * and writers waiting and a reader hasn't woken up and
438154941Sjhb		 * acquired the lock yet, rw_lock will be set to the
439154941Sjhb		 * value RW_UNLOCKED | RW_LOCK_WRITE_WAITERS.  If we see
440154941Sjhb		 * that value, try to acquire it once.  Note that we have
441154941Sjhb		 * to preserve the RW_LOCK_WRITE_WAITERS flag as there are
442154941Sjhb		 * other writers waiting still. If we fail, restart the
443154941Sjhb		 * loop.
444154941Sjhb		 */
445154941Sjhb		if (v == (RW_UNLOCKED | RW_LOCK_WRITE_WAITERS)) {
446154941Sjhb			if (atomic_cmpset_acq_ptr(&rw->rw_lock,
447154941Sjhb			    RW_UNLOCKED | RW_LOCK_WRITE_WAITERS,
448154941Sjhb			    tid | RW_LOCK_WRITE_WAITERS)) {
449154941Sjhb				turnstile_claim(&rw->rw_object);
450154941Sjhb				CTR2(KTR_LOCK, "%s: %p claimed by new writer",
451154941Sjhb				    __func__, rw);
452154941Sjhb				break;
453154941Sjhb			}
454154941Sjhb			turnstile_release(&rw->rw_object);
455154941Sjhb			cpu_spinwait();
456164159Skmacy			lock_profile_obtain_lock_failed(&rw->rw_object, &contested);
457154941Sjhb			continue;
458154941Sjhb		}
459154941Sjhb
460154941Sjhb		/*
461154941Sjhb		 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
462154941Sjhb		 * set it.  If we fail to set it, then loop back and try
463154941Sjhb		 * again.
464154941Sjhb		 */
465157826Sjhb		if (!(v & RW_LOCK_WRITE_WAITERS)) {
466157826Sjhb			if (!atomic_cmpset_ptr(&rw->rw_lock, v,
467157826Sjhb			    v | RW_LOCK_WRITE_WAITERS)) {
468157826Sjhb				turnstile_release(&rw->rw_object);
469157826Sjhb				cpu_spinwait();
470164159Skmacy				lock_profile_obtain_lock_failed(&rw->rw_object, &contested);
471157826Sjhb				continue;
472157826Sjhb			}
473157826Sjhb			if (LOCK_LOG_TEST(&rw->rw_object, 0))
474157826Sjhb				CTR2(KTR_LOCK, "%s: %p set write waiters flag",
475157826Sjhb				    __func__, rw);
476154941Sjhb		}
477154941Sjhb
478157846Sjhb#ifdef SMP
479157846Sjhb		/*
480157846Sjhb		 * If the lock is write locked and the owner is
481157846Sjhb		 * running on another CPU, spin until the owner stops
482157846Sjhb		 * running or the state of the lock changes.
483157846Sjhb		 */
484157846Sjhb		owner = (struct thread *)RW_OWNER(v);
485157846Sjhb		if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
486164159Skmacy			lock_profile_obtain_lock_failed(&rw->rw_object, &contested);
487157846Sjhb			turnstile_release(&rw->rw_object);
488157846Sjhb			if (LOCK_LOG_TEST(&rw->rw_object, 0))
489157846Sjhb				CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
490157846Sjhb				    __func__, rw, owner);
491157846Sjhb			while ((struct thread*)RW_OWNER(rw->rw_lock)== owner &&
492157846Sjhb			    TD_IS_RUNNING(owner))
493157846Sjhb				cpu_spinwait();
494157846Sjhb			continue;
495157846Sjhb		}
496157846Sjhb#endif
497154941Sjhb
498154941Sjhb		/*
499154941Sjhb		 * We were unable to acquire the lock and the write waiters
500154941Sjhb		 * flag is set, so we must block on the turnstile.
501154941Sjhb		 */
502154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
503154941Sjhb			CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
504154941Sjhb			    rw);
505154941Sjhb		turnstile_wait(&rw->rw_object, rw_owner(rw),
506154941Sjhb		    TS_EXCLUSIVE_QUEUE);
507154941Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
508154941Sjhb			CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
509154941Sjhb			    __func__, rw);
510154941Sjhb	}
511154941Sjhb}
512154941Sjhb
513154941Sjhb/*
514154941Sjhb * This function is called if the first try at releasing a write lock failed.
515154941Sjhb * This means that one of the 2 waiter bits must be set indicating that at
516154941Sjhb * least one thread is waiting on this lock.
517154941Sjhb */
518154941Sjhbvoid
519154941Sjhb_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
520154941Sjhb{
521154941Sjhb	struct turnstile *ts;
522154941Sjhb	uintptr_t v;
523154941Sjhb	int queue;
524154941Sjhb
525154941Sjhb	KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
526154941Sjhb	    ("%s: neither of the waiter flags are set", __func__));
527154941Sjhb
528154941Sjhb	if (LOCK_LOG_TEST(&rw->rw_object, 0))
529154941Sjhb		CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
530154941Sjhb
531154941Sjhb	turnstile_lock(&rw->rw_object);
532154941Sjhb	ts = turnstile_lookup(&rw->rw_object);
533154941Sjhb
534157846Sjhb#ifdef SMP
535157846Sjhb	/*
536157846Sjhb	 * There might not be a turnstile for this lock if all of
537157846Sjhb	 * the waiters are adaptively spinning.  In that case, just
538157846Sjhb	 * reset the lock to the unlocked state and return.
539157846Sjhb	 */
540157846Sjhb	if (ts == NULL) {
541157846Sjhb		atomic_store_rel_ptr(&rw->rw_lock, RW_UNLOCKED);
542157846Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
543157846Sjhb			CTR2(KTR_LOCK, "%s: %p no sleepers", __func__, rw);
544157846Sjhb		turnstile_release(&rw->rw_object);
545157846Sjhb		return;
546157846Sjhb	}
547157846Sjhb#else
548154941Sjhb	MPASS(ts != NULL);
549157846Sjhb#endif
550154941Sjhb
551154941Sjhb	/*
552154941Sjhb	 * Use the same algo as sx locks for now.  Prefer waking up shared
553154941Sjhb	 * waiters if we have any over writers.  This is probably not ideal.
554154941Sjhb	 *
555154941Sjhb	 * 'v' is the value we are going to write back to rw_lock.  If we
556154941Sjhb	 * have waiters on both queues, we need to preserve the state of
557154941Sjhb	 * the waiter flag for the queue we don't wake up.  For now this is
558154941Sjhb	 * hardcoded for the algorithm mentioned above.
559154941Sjhb	 *
560154941Sjhb	 * In the case of both readers and writers waiting we wakeup the
561154941Sjhb	 * readers but leave the RW_LOCK_WRITE_WAITERS flag set.  If a
562154941Sjhb	 * new writer comes in before a reader it will claim the lock up
563154941Sjhb	 * above.  There is probably a potential priority inversion in
564154941Sjhb	 * there that could be worked around either by waking both queues
565154941Sjhb	 * of waiters or doing some complicated lock handoff gymnastics.
566157846Sjhb	 *
567157846Sjhb	 * Note that in the SMP case, if both flags are set, there might
568157846Sjhb	 * not be any actual writers on the turnstile as they might all
569157846Sjhb	 * be spinning.  In that case, we don't want to preserve the
570157846Sjhb	 * RW_LOCK_WRITE_WAITERS flag as the turnstile is going to go
571157846Sjhb	 * away once we wakeup all the readers.
572154941Sjhb	 */
573157846Sjhb	v = RW_UNLOCKED;
574154941Sjhb	if (rw->rw_lock & RW_LOCK_READ_WAITERS) {
575154941Sjhb		queue = TS_SHARED_QUEUE;
576157846Sjhb#ifdef SMP
577157846Sjhb		if (rw->rw_lock & RW_LOCK_WRITE_WAITERS &&
578157846Sjhb		    !turnstile_empty(ts, TS_EXCLUSIVE_QUEUE))
579157846Sjhb			v |= RW_LOCK_WRITE_WAITERS;
580157846Sjhb#else
581157846Sjhb		v |= (rw->rw_lock & RW_LOCK_WRITE_WAITERS);
582157846Sjhb#endif
583157846Sjhb	} else
584154941Sjhb		queue = TS_EXCLUSIVE_QUEUE;
585157846Sjhb
586157846Sjhb#ifdef SMP
587157846Sjhb	/*
588157846Sjhb	 * We have to make sure that we actually have waiters to
589157846Sjhb	 * wakeup.  If they are all spinning, then we just need to
590157846Sjhb	 * disown the turnstile and return.
591157846Sjhb	 */
592157846Sjhb	if (turnstile_empty(ts, queue)) {
593157846Sjhb		if (LOCK_LOG_TEST(&rw->rw_object, 0))
594157846Sjhb			CTR2(KTR_LOCK, "%s: %p no sleepers 2", __func__, rw);
595157846Sjhb		atomic_store_rel_ptr(&rw->rw_lock, v);
596157846Sjhb		turnstile_disown(ts);
597157846Sjhb		return;
598154941Sjhb	}
599157846Sjhb#endif
600157846Sjhb
601157846Sjhb	/* Wake up all waiters for the specific queue. */
602154941Sjhb	if (LOCK_LOG_TEST(&rw->rw_object, 0))
603154941Sjhb		CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
604154941Sjhb		    queue == TS_SHARED_QUEUE ? "read" : "write");
605154941Sjhb	turnstile_broadcast(ts, queue);
606154941Sjhb	atomic_store_rel_ptr(&rw->rw_lock, v);
607154941Sjhb	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
608154941Sjhb}
609154941Sjhb
610157882Sjhb/*
611157882Sjhb * Attempt to do a non-blocking upgrade from a read lock to a write
612157882Sjhb * lock.  This will only succeed if this thread holds a single read
613157882Sjhb * lock.  Returns true if the upgrade succeeded and false otherwise.
614157882Sjhb */
615157882Sjhbint
616157882Sjhb_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
617157882Sjhb{
618157882Sjhb	uintptr_t v, tid;
619157882Sjhb	int success;
620157882Sjhb
621157882Sjhb	_rw_assert(rw, RA_RLOCKED, file, line);
622157882Sjhb
623157882Sjhb	/*
624157882Sjhb	 * Attempt to switch from one reader to a writer.  If there
625157882Sjhb	 * are any write waiters, then we will have to lock the
626157882Sjhb	 * turnstile first to prevent races with another writer
627157882Sjhb	 * calling turnstile_wait() before we have claimed this
628157882Sjhb	 * turnstile.  So, do the simple case of no waiters first.
629157882Sjhb	 */
630157882Sjhb	tid = (uintptr_t)curthread;
631157882Sjhb	if (!(rw->rw_lock & RW_LOCK_WRITE_WAITERS)) {
632157882Sjhb		success = atomic_cmpset_acq_ptr(&rw->rw_lock,
633157882Sjhb		    RW_READERS_LOCK(1), tid);
634157882Sjhb		goto out;
635157882Sjhb	}
636157882Sjhb
637157882Sjhb	/*
638157882Sjhb	 * Ok, we think we have write waiters, so lock the
639157882Sjhb	 * turnstile.
640157882Sjhb	 */
641157882Sjhb	turnstile_lock(&rw->rw_object);
642157882Sjhb
643157882Sjhb	/*
644157882Sjhb	 * Try to switch from one reader to a writer again.  This time
645157882Sjhb	 * we honor the current state of the RW_LOCK_WRITE_WAITERS
646157882Sjhb	 * flag.  If we obtain the lock with the flag set, then claim
647157882Sjhb	 * ownership of the turnstile.  In the SMP case it is possible
648157882Sjhb	 * for there to not be an associated turnstile even though there
649157882Sjhb	 * are waiters if all of the waiters are spinning.
650157882Sjhb	 */
651157882Sjhb	v = rw->rw_lock & RW_LOCK_WRITE_WAITERS;
652157882Sjhb	success = atomic_cmpset_acq_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
653157882Sjhb	    tid | v);
654157882Sjhb#ifdef SMP
655157882Sjhb	if (success && v && turnstile_lookup(&rw->rw_object) != NULL)
656157882Sjhb#else
657157882Sjhb	if (success && v)
658157882Sjhb#endif
659157882Sjhb		turnstile_claim(&rw->rw_object);
660157882Sjhb	else
661157882Sjhb		turnstile_release(&rw->rw_object);
662157882Sjhbout:
663157882Sjhb	LOCK_LOG_TRY("WUPGRADE", &rw->rw_object, 0, success, file, line);
664157882Sjhb	if (success)
665157882Sjhb		WITNESS_UPGRADE(&rw->rw_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
666157882Sjhb		    file, line);
667157882Sjhb	return (success);
668157882Sjhb}
669157882Sjhb
670157882Sjhb/*
671157882Sjhb * Downgrade a write lock into a single read lock.
672157882Sjhb */
673157882Sjhbvoid
674157882Sjhb_rw_downgrade(struct rwlock *rw, const char *file, int line)
675157882Sjhb{
676157882Sjhb	struct turnstile *ts;
677157882Sjhb	uintptr_t tid, v;
678157882Sjhb
679157882Sjhb	_rw_assert(rw, RA_WLOCKED, file, line);
680157882Sjhb
681157882Sjhb	WITNESS_DOWNGRADE(&rw->rw_object, 0, file, line);
682157882Sjhb
683157882Sjhb	/*
684157882Sjhb	 * Convert from a writer to a single reader.  First we handle
685157882Sjhb	 * the easy case with no waiters.  If there are any waiters, we
686157882Sjhb	 * lock the turnstile, "disown" the lock, and awaken any read
687157882Sjhb	 * waiters.
688157882Sjhb	 */
689157882Sjhb	tid = (uintptr_t)curthread;
690157882Sjhb	if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
691157882Sjhb		goto out;
692157882Sjhb
693157882Sjhb	/*
694157882Sjhb	 * Ok, we think we have waiters, so lock the turnstile so we can
695157882Sjhb	 * read the waiter flags without any races.
696157882Sjhb	 */
697157882Sjhb	turnstile_lock(&rw->rw_object);
698157882Sjhb	v = rw->rw_lock;
699157882Sjhb	MPASS(v & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS));
700157882Sjhb
701157882Sjhb	/*
702157882Sjhb	 * Downgrade from a write lock while preserving
703157882Sjhb	 * RW_LOCK_WRITE_WAITERS and give up ownership of the
704157882Sjhb	 * turnstile.  If there are any read waiters, wake them up.
705157882Sjhb	 *
706157882Sjhb	 * For SMP, we have to allow for the fact that all of the
707157882Sjhb	 * read waiters might be spinning.  In that case, act as if
708157882Sjhb	 * RW_LOCK_READ_WAITERS is not set.  Also, only preserve
709157882Sjhb	 * the RW_LOCK_WRITE_WAITERS flag if at least one writer is
710157882Sjhb	 * blocked on the turnstile.
711157882Sjhb	 */
712157882Sjhb	ts = turnstile_lookup(&rw->rw_object);
713157882Sjhb#ifdef SMP
714157882Sjhb	if (ts == NULL)
715157882Sjhb		v &= ~(RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS);
716157882Sjhb	else if (v & RW_LOCK_READ_WAITERS &&
717157882Sjhb	    turnstile_empty(ts, TS_SHARED_QUEUE))
718157882Sjhb		v &= ~RW_LOCK_READ_WAITERS;
719157882Sjhb	else if (v & RW_LOCK_WRITE_WAITERS &&
720157882Sjhb	    turnstile_empty(ts, TS_EXCLUSIVE_QUEUE))
721157882Sjhb		v &= ~RW_LOCK_WRITE_WAITERS;
722157882Sjhb#else
723157882Sjhb	MPASS(ts != NULL);
724157882Sjhb#endif
725157882Sjhb	if (v & RW_LOCK_READ_WAITERS)
726157882Sjhb		turnstile_broadcast(ts, TS_SHARED_QUEUE);
727157882Sjhb	atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) |
728157882Sjhb	    (v & RW_LOCK_WRITE_WAITERS));
729157882Sjhb	if (v & RW_LOCK_READ_WAITERS)
730157882Sjhb		turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
731157882Sjhb#ifdef SMP
732157882Sjhb	else if (ts == NULL)
733157882Sjhb		turnstile_release(&rw->rw_object);
734157882Sjhb#endif
735157882Sjhb	else
736157882Sjhb		turnstile_disown(ts);
737157882Sjhbout:
738157882Sjhb	LOCK_LOG_LOCK("WDOWNGRADE", &rw->rw_object, 0, 0, file, line);
739157882Sjhb}
740157882Sjhb
741154941Sjhb#ifdef INVARIANT_SUPPORT
742155162Sscottl#ifndef INVARIANTS
743154941Sjhb#undef _rw_assert
744154941Sjhb#endif
745154941Sjhb
746154941Sjhb/*
747154941Sjhb * In the non-WITNESS case, rw_assert() can only detect that at least
748154941Sjhb * *some* thread owns an rlock, but it cannot guarantee that *this*
749154941Sjhb * thread owns an rlock.
750154941Sjhb */
751154941Sjhbvoid
752154941Sjhb_rw_assert(struct rwlock *rw, int what, const char *file, int line)
753154941Sjhb{
754154941Sjhb
755154941Sjhb	if (panicstr != NULL)
756154941Sjhb		return;
757154941Sjhb	switch (what) {
758154941Sjhb	case RA_LOCKED:
759154941Sjhb	case RA_RLOCKED:
760154941Sjhb#ifdef WITNESS
761154941Sjhb		witness_assert(&rw->rw_object, what, file, line);
762154941Sjhb#else
763154941Sjhb		/*
764154941Sjhb		 * If some other thread has a write lock or we have one
765154941Sjhb		 * and are asserting a read lock, fail.  Also, if no one
766154941Sjhb		 * has a lock at all, fail.
767154941Sjhb		 */
768155061Sscottl		if (rw->rw_lock == RW_UNLOCKED ||
769155061Sscottl		    (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
770157826Sjhb		    rw_wowner(rw) != curthread)))
771154941Sjhb			panic("Lock %s not %slocked @ %s:%d\n",
772155012Sscottl			    rw->rw_object.lo_name, (what == RA_RLOCKED) ?
773154941Sjhb			    "read " : "", file, line);
774154941Sjhb#endif
775154941Sjhb		break;
776154941Sjhb	case RA_WLOCKED:
777157826Sjhb		if (rw_wowner(rw) != curthread)
778154941Sjhb			panic("Lock %s not exclusively locked @ %s:%d\n",
779154941Sjhb			    rw->rw_object.lo_name, file, line);
780154941Sjhb		break;
781154941Sjhb	case RA_UNLOCKED:
782154941Sjhb#ifdef WITNESS
783154941Sjhb		witness_assert(&rw->rw_object, what, file, line);
784154941Sjhb#else
785154941Sjhb		/*
786154941Sjhb		 * If we hold a write lock fail.  We can't reliably check
787154941Sjhb		 * to see if we hold a read lock or not.
788154941Sjhb		 */
789157826Sjhb		if (rw_wowner(rw) == curthread)
790154941Sjhb			panic("Lock %s exclusively locked @ %s:%d\n",
791154941Sjhb			    rw->rw_object.lo_name, file, line);
792154941Sjhb#endif
793154941Sjhb		break;
794154941Sjhb	default:
795154941Sjhb		panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
796154941Sjhb		    line);
797154941Sjhb	}
798154941Sjhb}
799154941Sjhb#endif /* INVARIANT_SUPPORT */
800154941Sjhb
801154941Sjhb#ifdef DDB
802154941Sjhbvoid
803154941Sjhbdb_show_rwlock(struct lock_object *lock)
804154941Sjhb{
805154941Sjhb	struct rwlock *rw;
806154941Sjhb	struct thread *td;
807154941Sjhb
808154941Sjhb	rw = (struct rwlock *)lock;
809154941Sjhb
810154941Sjhb	db_printf(" state: ");
811154941Sjhb	if (rw->rw_lock == RW_UNLOCKED)
812154941Sjhb		db_printf("UNLOCKED\n");
813154941Sjhb	else if (rw->rw_lock & RW_LOCK_READ)
814154973Smlaier		db_printf("RLOCK: %jd locks\n",
815154973Smlaier		    (intmax_t)(RW_READERS(rw->rw_lock)));
816154941Sjhb	else {
817157826Sjhb		td = rw_wowner(rw);
818154941Sjhb		db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
819154941Sjhb		    td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
820154941Sjhb	}
821154941Sjhb	db_printf(" waiters: ");
822154941Sjhb	switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
823154941Sjhb	case RW_LOCK_READ_WAITERS:
824154941Sjhb		db_printf("readers\n");
825154941Sjhb		break;
826154941Sjhb	case RW_LOCK_WRITE_WAITERS:
827154941Sjhb		db_printf("writers\n");
828154941Sjhb		break;
829154941Sjhb	case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
830154941Sjhb		db_printf("readers and waiters\n");
831154941Sjhb		break;
832154941Sjhb	default:
833154941Sjhb		db_printf("none\n");
834154941Sjhb		break;
835154941Sjhb	}
836154941Sjhb}
837154941Sjhb
838154941Sjhb#endif
839