1/*-
2 * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * Machine independent bits of reader/writer lock implementation.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/kern/kern_rwlock.c 176076 2008-02-07 06:16:54Z jeff $");
36
37#include "opt_ddb.h"
38#include "opt_no_adaptive_rwlocks.h"
39
40#include <sys/param.h>
41#include <sys/ktr.h>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/proc.h>
45#include <sys/rwlock.h>
46#include <sys/systm.h>
47#include <sys/turnstile.h>
48
49#include <machine/cpu.h>
50
51CTASSERT((RW_RECURSE & LO_CLASSFLAGS) == RW_RECURSE);
52
53#if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
54#define ADAPTIVE_RWLOCKS
55#endif
56
57#ifdef DDB
58#include <ddb/ddb.h>
59
60static void db_show_rwlock(struct lock_object *lock);
61#endif
62static void assert_rw(struct lock_object *lock, int what);
63static void lock_rw(struct lock_object *lock, int how);
64static int unlock_rw(struct lock_object *lock);
65
66struct lock_class lock_class_rw = {
67 .lc_name = "rw",
68 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
69 .lc_assert = assert_rw,
70#ifdef DDB
71 .lc_ddb_show = db_show_rwlock,
72#endif
73 .lc_lock = lock_rw,
74 .lc_unlock = unlock_rw,
75};
76
77/*
78 * Return a pointer to the owning thread if the lock is write-locked or
79 * NULL if the lock is unlocked or read-locked.
80 */
81#define rw_wowner(rw) \
82 ((rw)->rw_lock & RW_LOCK_READ ? NULL : \
83 (struct thread *)RW_OWNER((rw)->rw_lock))
84
85/*
86 * Returns if a write owner is recursed. Write ownership is not assured
87 * here and should be previously checked.
88 */
89#define rw_recursed(rw) ((rw)->rw_recurse != 0)
90
91/*
92 * Return true if curthread helds the lock.
93 */
94#define rw_wlocked(rw) (rw_wowner((rw)) == curthread)
95
96/*
97 * Return a pointer to the owning thread for this lock who should receive
98 * any priority lent by threads that block on this lock. Currently this
99 * is identical to rw_wowner().
100 */
101#define rw_owner(rw) rw_wowner(rw)
102
103#ifndef INVARIANTS
104#define _rw_assert(rw, what, file, line)
105#endif
106
107void
108assert_rw(struct lock_object *lock, int what)
109{
110
111 rw_assert((struct rwlock *)lock, what);
112}
113
114void
115lock_rw(struct lock_object *lock, int how)
116{
117 struct rwlock *rw;
118
119 rw = (struct rwlock *)lock;
120 if (how)
121 rw_wlock(rw);
122 else
123 rw_rlock(rw);
124}
125
126int
127unlock_rw(struct lock_object *lock)
128{
129 struct rwlock *rw;
130
131 rw = (struct rwlock *)lock;
132 rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
133 if (rw->rw_lock & RW_LOCK_READ) {
134 rw_runlock(rw);
135 return (0);
136 } else {
137 rw_wunlock(rw);
138 return (1);
139 }
140}
141
142void
143rw_init_flags(struct rwlock *rw, const char *name, int opts)
144{
145 int flags;
146
147 MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
148 RW_RECURSE)) == 0);
149
150 flags = LO_UPGRADABLE | LO_RECURSABLE;
151 if (opts & RW_DUPOK)
152 flags |= LO_DUPOK;
153 if (opts & RW_NOPROFILE)
154 flags |= LO_NOPROFILE;
155 if (!(opts & RW_NOWITNESS))
156 flags |= LO_WITNESS;
157 if (opts & RW_QUIET)
158 flags |= LO_QUIET;
159 flags |= opts & RW_RECURSE;
160
161 rw->rw_lock = RW_UNLOCKED;
162 rw->rw_recurse = 0;
163 lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
164}
165
166void
167rw_destroy(struct rwlock *rw)
168{
169
170 KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked"));
171 KASSERT(rw->rw_recurse == 0, ("rw lock still recursed"));
172 rw->rw_lock = RW_DESTROYED;
173 lock_destroy(&rw->lock_object);
174}
175
176void
177rw_sysinit(void *arg)
178{
179 struct rw_args *args = arg;
180
181 rw_init(args->ra_rw, args->ra_desc);
182}
183
184int
185rw_wowned(struct rwlock *rw)
186{
187
188 return (rw_wowner(rw) == curthread);
189}
190
191void
192_rw_wlock(struct rwlock *rw, const char *file, int line)
193{
194
195 MPASS(curthread != NULL);
196 KASSERT(rw->rw_lock != RW_DESTROYED,
197 ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
198 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
199 line);
200 __rw_wlock(rw, curthread, file, line);
201 LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
202 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
203 curthread->td_locks++;
204}
205
206void
207_rw_wunlock(struct rwlock *rw, const char *file, int line)
208{
209
210 MPASS(curthread != NULL);
211 KASSERT(rw->rw_lock != RW_DESTROYED,
212 ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
213 _rw_assert(rw, RA_WLOCKED, file, line);
214 curthread->td_locks--;
215 WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
216 LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
217 line);
218 if (!rw_recursed(rw))
219 lock_profile_release_lock(&rw->lock_object);
220 __rw_wunlock(rw, curthread, file, line);
221}
222/*
223 * Determines whether a new reader can acquire a lock. Succeeds if the
224 * reader already owns a read lock and the lock is locked for read to
225 * prevent deadlock from reader recursion. Also succeeds if the lock
226 * is unlocked and has no writer waiters or spinners. Failing otherwise
227 * prioritizes writers before readers.
228 */
229#define RW_CAN_READ(_rw) \
230 ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) & \
231 (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) == \
232 RW_LOCK_READ)
233
234void
235_rw_rlock(struct rwlock *rw, const char *file, int line)
236{
237 struct turnstile *ts;
238#ifdef ADAPTIVE_RWLOCKS
239 volatile struct thread *owner;
240#endif
241 uint64_t waittime = 0;
242 int contested = 0;
243 uintptr_t v;
244
245 KASSERT(rw->rw_lock != RW_DESTROYED,
246 ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
247 KASSERT(rw_wowner(rw) != curthread,
248 ("%s (%s): wlock already held @ %s:%d", __func__,
249 rw->lock_object.lo_name, file, line));
250 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line);
251
252 for (;;) {
253 /*
254 * Handle the easy case. If no other thread has a write
255 * lock, then try to bump up the count of read locks. Note
256 * that we have to preserve the current state of the
257 * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a
258 * read lock, then rw_lock must have changed, so restart
259 * the loop. Note that this handles the case of a
260 * completely unlocked rwlock since such a lock is encoded
261 * as a read lock with no waiters.
262 */
263 v = rw->rw_lock;
264 if (RW_CAN_READ(v)) {
265 /*
266 * The RW_LOCK_READ_WAITERS flag should only be set
267 * if the lock has been unlocked and write waiters
268 * were present.
269 */
270 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v,
271 v + RW_ONE_READER)) {
272 if (LOCK_LOG_TEST(&rw->lock_object, 0))
273 CTR4(KTR_LOCK,
274 "%s: %p succeed %p -> %p", __func__,
275 rw, (void *)v,
276 (void *)(v + RW_ONE_READER));
277 break;
278 }
279 cpu_spinwait();
280 continue;
281 }
282 lock_profile_obtain_lock_failed(&rw->lock_object,
283 &contested, &waittime);
284
285#ifdef ADAPTIVE_RWLOCKS
286 /*
287 * If the owner is running on another CPU, spin until
288 * the owner stops running or the state of the lock
289 * changes.
290 */
291 if ((v & RW_LOCK_READ) == 0) {
292 owner = (struct thread *)RW_OWNER(v);
293 if (TD_IS_RUNNING(owner)) {
294 if (LOCK_LOG_TEST(&rw->lock_object, 0))
295 CTR3(KTR_LOCK,
296 "%s: spinning on %p held by %p",
297 __func__, rw, owner);
298 while ((struct thread*)RW_OWNER(rw->rw_lock) ==
299 owner && TD_IS_RUNNING(owner))
300 cpu_spinwait();
301 continue;
302 }
303 }
304#endif
305
306 /*
307 * Okay, now it's the hard case. Some other thread already
308 * has a write lock or there are write waiters present,
309 * acquire the turnstile lock so we can begin the process
310 * of blocking.
311 */
312 ts = turnstile_trywait(&rw->lock_object);
313
314 /*
315 * The lock might have been released while we spun, so
316 * recheck its state and restart the loop if needed.
317 */
318 v = rw->rw_lock;
319 if (RW_CAN_READ(v)) {
320 turnstile_cancel(ts);
321 cpu_spinwait();
322 continue;
323 }
324
325#ifdef ADAPTIVE_RWLOCKS
326 /*
327 * If the current owner of the lock is executing on another
328 * CPU quit the hard path and try to spin.
329 */
330 if ((v & RW_LOCK_READ) == 0) {
331 owner = (struct thread *)RW_OWNER(v);
332 if (TD_IS_RUNNING(owner)) {
333 turnstile_cancel(ts);
334 cpu_spinwait();
335 continue;
336 }
337 }
338#endif
339
340 /*
341 * The lock is held in write mode or it already has waiters.
342 */
343 MPASS(!RW_CAN_READ(v));
344
345 /*
346 * If the RW_LOCK_READ_WAITERS flag is already set, then
347 * we can go ahead and block. If it is not set then try
348 * to set it. If we fail to set it drop the turnstile
349 * lock and restart the loop.
350 */
351 if (!(v & RW_LOCK_READ_WAITERS)) {
352 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
353 v | RW_LOCK_READ_WAITERS)) {
354 turnstile_cancel(ts);
355 cpu_spinwait();
356 continue;
357 }
358 if (LOCK_LOG_TEST(&rw->lock_object, 0))
359 CTR2(KTR_LOCK, "%s: %p set read waiters flag",
360 __func__, rw);
361 }
362
363 /*
364 * We were unable to acquire the lock and the read waiters
365 * flag is set, so we must block on the turnstile.
366 */
367 if (LOCK_LOG_TEST(&rw->lock_object, 0))
368 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
369 rw);
370 turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
371 if (LOCK_LOG_TEST(&rw->lock_object, 0))
372 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
373 __func__, rw);
374 }
375
376 /*
377 * TODO: acquire "owner of record" here. Here be turnstile dragons
378 * however. turnstiles don't like owners changing between calls to
379 * turnstile_wait() currently.
380 */
381 lock_profile_obtain_lock_success( &rw->lock_object, contested,
382 waittime, file, line);
383 LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
384 WITNESS_LOCK(&rw->lock_object, 0, file, line);
385 curthread->td_locks++;
386 curthread->td_rw_rlocks++;
387}
388
389void
390_rw_runlock(struct rwlock *rw, const char *file, int line)
391{
392 struct turnstile *ts;
393 uintptr_t x, v, queue;
394
395 KASSERT(rw->rw_lock != RW_DESTROYED,
396 ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
397 _rw_assert(rw, RA_RLOCKED, file, line);
398 curthread->td_locks--;
399 curthread->td_rw_rlocks--;
400 WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
401 LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
402
403 /* TODO: drop "owner of record" here. */
404
405 for (;;) {
406 /*
407 * See if there is more than one read lock held. If so,
408 * just drop one and return.
409 */
410 x = rw->rw_lock;
411 if (RW_READERS(x) > 1) {
412 if (atomic_cmpset_ptr(&rw->rw_lock, x,
413 x - RW_ONE_READER)) {
414 if (LOCK_LOG_TEST(&rw->lock_object, 0))
415 CTR4(KTR_LOCK,
416 "%s: %p succeeded %p -> %p",
417 __func__, rw, (void *)x,
418 (void *)(x - RW_ONE_READER));
419 break;
420 }
421 continue;
422 }
423 /*
424 * If there aren't any waiters for a write lock, then try
425 * to drop it quickly.
426 */
427 if (!(x & RW_LOCK_WAITERS)) {
428 MPASS((x & ~RW_LOCK_WRITE_SPINNER) ==
429 RW_READERS_LOCK(1));
430 if (atomic_cmpset_ptr(&rw->rw_lock, x, RW_UNLOCKED)) {
431 if (LOCK_LOG_TEST(&rw->lock_object, 0))
432 CTR2(KTR_LOCK, "%s: %p last succeeded",
433 __func__, rw);
434 break;
435 }
436 continue;
437 }
438 /*
439 * Ok, we know we have waiters and we think we are the
440 * last reader, so grab the turnstile lock.
441 */
442 turnstile_chain_lock(&rw->lock_object);
443 v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
444 MPASS(v & RW_LOCK_WAITERS);
445
446 /*
447 * Try to drop our lock leaving the lock in a unlocked
448 * state.
449 *
450 * If you wanted to do explicit lock handoff you'd have to
451 * do it here. You'd also want to use turnstile_signal()
452 * and you'd have to handle the race where a higher
453 * priority thread blocks on the write lock before the
454 * thread you wakeup actually runs and have the new thread
455 * "steal" the lock. For now it's a lot simpler to just
456 * wakeup all of the waiters.
457 *
458 * As above, if we fail, then another thread might have
459 * acquired a read lock, so drop the turnstile lock and
460 * restart.
461 */
462 x = RW_UNLOCKED;
463 if (v & RW_LOCK_WRITE_WAITERS) {
464 queue = TS_EXCLUSIVE_QUEUE;
465 x |= (v & RW_LOCK_READ_WAITERS);
466 } else
467 queue = TS_SHARED_QUEUE;
468 if (!atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
469 x)) {
470 turnstile_chain_unlock(&rw->lock_object);
471 continue;
472 }
473 if (LOCK_LOG_TEST(&rw->lock_object, 0))
474 CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
475 __func__, rw);
476
477 /*
478 * Ok. The lock is released and all that's left is to
479 * wake up the waiters. Note that the lock might not be
480 * free anymore, but in that case the writers will just
481 * block again if they run before the new lock holder(s)
482 * release the lock.
483 */
484 ts = turnstile_lookup(&rw->lock_object);
485 MPASS(ts != NULL);
486 turnstile_broadcast(ts, queue);
487 turnstile_unpend(ts, TS_SHARED_LOCK);
488 turnstile_chain_unlock(&rw->lock_object);
489 break;
490 }
491 lock_profile_release_lock(&rw->lock_object);
492}
493
494/*
495 * This function is called when we are unable to obtain a write lock on the
496 * first try. This means that at least one other thread holds either a
497 * read or write lock.
498 */
499void
500_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
501{
502 struct turnstile *ts;
503#ifdef ADAPTIVE_RWLOCKS
504 volatile struct thread *owner;
505 int spintries = 0;
506 int i;
507#endif
508 uint64_t waittime = 0;
509 uintptr_t v, x;
510 int contested = 0;
511
512 if (rw_wlocked(rw)) {
513 KASSERT(rw->lock_object.lo_flags & RW_RECURSE,
514 ("%s: recursing but non-recursive rw %s @ %s:%d\n",
515 __func__, rw->lock_object.lo_name, file, line));
516 rw->rw_recurse++;
517 if (LOCK_LOG_TEST(&rw->lock_object, 0))
518 CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
519 return;
520 }
521
522 if (LOCK_LOG_TEST(&rw->lock_object, 0))
523 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
524 rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
525
526 while (!_rw_write_lock(rw, tid)) {
527 lock_profile_obtain_lock_failed(&rw->lock_object,
528 &contested, &waittime);
529#ifdef ADAPTIVE_RWLOCKS
530 /*
531 * If the lock is write locked and the owner is
532 * running on another CPU, spin until the owner stops
533 * running or the state of the lock changes.
534 */
535 v = rw->rw_lock;
536 owner = (struct thread *)RW_OWNER(v);
537 if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
538 if (LOCK_LOG_TEST(&rw->lock_object, 0))
539 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
540 __func__, rw, owner);
541 while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
542 TD_IS_RUNNING(owner))
543 cpu_spinwait();
544 continue;
545 }
546 if ((v & RW_LOCK_READ) && RW_READERS(v) && spintries < 100) {
547 if (!(v & RW_LOCK_WRITE_SPINNER)) {
548 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
549 v | RW_LOCK_WRITE_SPINNER)) {
550 cpu_spinwait();
551 continue;
552 }
553 }
554 spintries++;
555 for (i = 100000; i > 0; i--) {
556 if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0)
557 break;
558 cpu_spinwait();
559 }
560 if (i)
561 continue;
562 }
563#endif
564 ts = turnstile_trywait(&rw->lock_object);
565 v = rw->rw_lock;
566
567#ifdef ADAPTIVE_RWLOCKS
568 /*
569 * If the current owner of the lock is executing on another
570 * CPU quit the hard path and try to spin.
571 */
572 if (!(v & RW_LOCK_READ)) {
573 owner = (struct thread *)RW_OWNER(v);
574 if (TD_IS_RUNNING(owner)) {
575 turnstile_cancel(ts);
576 cpu_spinwait();
577 continue;
578 }
579 }
580#endif
581 /*
582 * If the lock was released while waiting for the turnstile
583 * chain lock retry.
584 */
585 x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
586 if ((v & ~x) == RW_UNLOCKED) {
587 x &= ~RW_LOCK_WRITE_SPINNER;
588 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) {
589 if (x)
590 turnstile_claim(ts);
591 else
592 turnstile_cancel(ts);
593 break;
594 }
595 turnstile_cancel(ts);
596 cpu_spinwait();
597 continue;
598 }
599 /*
600 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
601 * set it. If we fail to set it, then loop back and try
602 * again.
603 */
604 if (!(v & RW_LOCK_WRITE_WAITERS)) {
605 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
606 v | RW_LOCK_WRITE_WAITERS)) {
607 turnstile_cancel(ts);
608 cpu_spinwait();
609 continue;
610 }
611 if (LOCK_LOG_TEST(&rw->lock_object, 0))
612 CTR2(KTR_LOCK, "%s: %p set write waiters flag",
613 __func__, rw);
614 }
615 /*
616 * We were unable to acquire the lock and the write waiters
617 * flag is set, so we must block on the turnstile.
618 */
619 if (LOCK_LOG_TEST(&rw->lock_object, 0))
620 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
621 rw);
622 turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
623 if (LOCK_LOG_TEST(&rw->lock_object, 0))
624 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
625 __func__, rw);
626#ifdef ADAPTIVE_RWLOCKS
627 spintries = 0;
628#endif
629 }
630 lock_profile_obtain_lock_success(&rw->lock_object, contested, waittime,
631 file, line);
632}
633
634/*
635 * This function is called if the first try at releasing a write lock failed.
636 * This means that one of the 2 waiter bits must be set indicating that at
637 * least one thread is waiting on this lock.
638 */
639void
640_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
641{
642 struct turnstile *ts;
643 uintptr_t v;
644 int queue;
645
646 if (rw_wlocked(rw) && rw_recursed(rw)) {
647 rw->rw_recurse--;
648 if (LOCK_LOG_TEST(&rw->lock_object, 0))
649 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
650 return;
651 }
652 v = rw->rw_lock;
653
654 KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
655 ("%s: neither of the waiter flags are set", __func__));
656
657 if (LOCK_LOG_TEST(&rw->lock_object, 0))
658 CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
659
660 turnstile_chain_lock(&rw->lock_object);
661 ts = turnstile_lookup(&rw->lock_object);
662
663 MPASS(ts != NULL);
664
665 /*
666 * Use the same algo as sx locks for now. Prefer waking up shared
667 * waiters if we have any over writers. This is probably not ideal.
668 *
669 * 'v' is the value we are going to write back to rw_lock. If we
670 * have waiters on both queues, we need to preserve the state of
671 * the waiter flag for the queue we don't wake up. For now this is
672 * hardcoded for the algorithm mentioned above.
673 *
674 * In the case of both readers and writers waiting we wakeup the
675 * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a
676 * new writer comes in before a reader it will claim the lock up
677 * above. There is probably a potential priority inversion in
678 * there that could be worked around either by waking both queues
679 * of waiters or doing some complicated lock handoff gymnastics.
680 */
681 v = RW_UNLOCKED;
682 if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) {
683 queue = TS_EXCLUSIVE_QUEUE;
684 v |= (rw->rw_lock & RW_LOCK_READ_WAITERS);
685 } else
686 queue = TS_SHARED_QUEUE;
687
688 /* Wake up all waiters for the specific queue. */
689 if (LOCK_LOG_TEST(&rw->lock_object, 0))
690 CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
691 queue == TS_SHARED_QUEUE ? "read" : "write");
692 turnstile_broadcast(ts, queue);
693 atomic_store_rel_ptr(&rw->rw_lock, v);
694 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
695 turnstile_chain_unlock(&rw->lock_object);
696}
697
698/*
699 * Attempt to do a non-blocking upgrade from a read lock to a write
700 * lock. This will only succeed if this thread holds a single read
701 * lock. Returns true if the upgrade succeeded and false otherwise.
702 */
703int
704_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
705{
706 uintptr_t v, x, tid;
707 struct turnstile *ts;
708 int success;
709
710 KASSERT(rw->rw_lock != RW_DESTROYED,
711 ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
712 _rw_assert(rw, RA_RLOCKED, file, line);
713
714 /*
715 * Attempt to switch from one reader to a writer. If there
716 * are any write waiters, then we will have to lock the
717 * turnstile first to prevent races with another writer
718 * calling turnstile_wait() before we have claimed this
719 * turnstile. So, do the simple case of no waiters first.
720 */
721 tid = (uintptr_t)curthread;
722 success = 0;
723 for (;;) {
724 v = rw->rw_lock;
725 if (RW_READERS(v) > 1)
726 break;
727 if (!(v & RW_LOCK_WAITERS)) {
728 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid);
729 if (!success)
730 continue;
731 break;
732 }
733
734 /*
735 * Ok, we think we have waiters, so lock the turnstile.
736 */
737 ts = turnstile_trywait(&rw->lock_object);
738 v = rw->rw_lock;
739 if (RW_READERS(v) > 1) {
740 turnstile_cancel(ts);
741 break;
742 }
743 /*
744 * Try to switch from one reader to a writer again. This time
745 * we honor the current state of the waiters flags.
746 * If we obtain the lock with the flags set, then claim
747 * ownership of the turnstile.
748 */
749 x = rw->rw_lock & RW_LOCK_WAITERS;
750 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x);
751 if (success) {
752 if (x)
753 turnstile_claim(ts);
754 else
755 turnstile_cancel(ts);
756 break;
757 }
758 turnstile_cancel(ts);
759 }
760 LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
761 if (success) {
762 curthread->td_rw_rlocks--;
763 WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
764 file, line);
765 }
766 return (success);
767}
768
769/*
770 * Downgrade a write lock into a single read lock.
771 */
772void
773_rw_downgrade(struct rwlock *rw, const char *file, int line)
774{
775 struct turnstile *ts;
776 uintptr_t tid, v;
777 int rwait, wwait;
778
779 KASSERT(rw->rw_lock != RW_DESTROYED,
780 ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
781 _rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
782#ifndef INVARIANTS
783 if (rw_recursed(rw))
784 panic("downgrade of a recursed lock");
785#endif
786
787 WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
788
789 /*
790 * Convert from a writer to a single reader. First we handle
791 * the easy case with no waiters. If there are any waiters, we
792 * lock the turnstile and "disown" the lock.
793 */
794 tid = (uintptr_t)curthread;
795 if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
796 goto out;
797
798 /*
799 * Ok, we think we have waiters, so lock the turnstile so we can
800 * read the waiter flags without any races.
801 */
802 turnstile_chain_lock(&rw->lock_object);
803 v = rw->rw_lock & RW_LOCK_WAITERS;
804 rwait = v & RW_LOCK_READ_WAITERS;
805 wwait = v & RW_LOCK_WRITE_WAITERS;
806 MPASS(rwait | wwait);
807
808 /*
809 * Downgrade from a write lock while preserving waiters flag
810 * and give up ownership of the turnstile.
811 */
812 ts = turnstile_lookup(&rw->lock_object);
813 MPASS(ts != NULL);
814 if (!wwait)
815 v &= ~RW_LOCK_READ_WAITERS;
816 atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
817 /*
818 * Wake other readers if there are no writers pending. Otherwise they
819 * won't be able to acquire the lock anyway.
820 */
821 if (rwait && !wwait) {
822 turnstile_broadcast(ts, TS_SHARED_QUEUE);
823 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
824 } else
825 turnstile_disown(ts);
826 turnstile_chain_unlock(&rw->lock_object);
827out:
828 curthread->td_rw_rlocks++;
829 LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
830}
831
832#ifdef INVARIANT_SUPPORT
833#ifndef INVARIANTS
834#undef _rw_assert
835#endif
836
837/*
838 * In the non-WITNESS case, rw_assert() can only detect that at least
839 * *some* thread owns an rlock, but it cannot guarantee that *this*
840 * thread owns an rlock.
841 */
842void
843_rw_assert(struct rwlock *rw, int what, const char *file, int line)
844{
845
846 if (panicstr != NULL)
847 return;
848 switch (what) {
849 case RA_LOCKED:
850 case RA_LOCKED | RA_RECURSED:
851 case RA_LOCKED | RA_NOTRECURSED:
852 case RA_RLOCKED:
853#ifdef WITNESS
854 witness_assert(&rw->lock_object, what, file, line);
855#else
856 /*
857 * If some other thread has a write lock or we have one
858 * and are asserting a read lock, fail. Also, if no one
859 * has a lock at all, fail.
860 */
861 if (rw->rw_lock == RW_UNLOCKED ||
862 (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
863 rw_wowner(rw) != curthread)))
864 panic("Lock %s not %slocked @ %s:%d\n",
865 rw->lock_object.lo_name, (what == RA_RLOCKED) ?
866 "read " : "", file, line);
867
868 if (!(rw->rw_lock & RW_LOCK_READ)) {
869 if (rw_recursed(rw)) {
870 if (what & RA_NOTRECURSED)
871 panic("Lock %s recursed @ %s:%d\n",
872 rw->lock_object.lo_name, file,
873 line);
874 } else if (what & RA_RECURSED)
875 panic("Lock %s not recursed @ %s:%d\n",
876 rw->lock_object.lo_name, file, line);
877 }
878#endif
879 break;
880 case RA_WLOCKED:
881 case RA_WLOCKED | RA_RECURSED:
882 case RA_WLOCKED | RA_NOTRECURSED:
883 if (rw_wowner(rw) != curthread)
884 panic("Lock %s not exclusively locked @ %s:%d\n",
885 rw->lock_object.lo_name, file, line);
886 if (rw_recursed(rw)) {
887 if (what & RA_NOTRECURSED)
888 panic("Lock %s recursed @ %s:%d\n",
889 rw->lock_object.lo_name, file, line);
890 } else if (what & RA_RECURSED)
891 panic("Lock %s not recursed @ %s:%d\n",
892 rw->lock_object.lo_name, file, line);
893 break;
894 case RA_UNLOCKED:
895#ifdef WITNESS
896 witness_assert(&rw->lock_object, what, file, line);
897#else
898 /*
899 * If we hold a write lock fail. We can't reliably check
900 * to see if we hold a read lock or not.
901 */
902 if (rw_wowner(rw) == curthread)
903 panic("Lock %s exclusively locked @ %s:%d\n",
904 rw->lock_object.lo_name, file, line);
905#endif
906 break;
907 default:
908 panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
909 line);
910 }
911}
912#endif /* INVARIANT_SUPPORT */
913
914#ifdef DDB
915void
916db_show_rwlock(struct lock_object *lock)
917{
918 struct rwlock *rw;
919 struct thread *td;
920
921 rw = (struct rwlock *)lock;
922
923 db_printf(" state: ");
924 if (rw->rw_lock == RW_UNLOCKED)
925 db_printf("UNLOCKED\n");
926 else if (rw->rw_lock == RW_DESTROYED) {
927 db_printf("DESTROYED\n");
928 return;
929 } else if (rw->rw_lock & RW_LOCK_READ)
930 db_printf("RLOCK: %ju locks\n",
931 (uintmax_t)(RW_READERS(rw->rw_lock)));
932 else {
933 td = rw_wowner(rw);
934 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
935 td->td_tid, td->td_proc->p_pid, td->td_name);
936 if (rw_recursed(rw))
937 db_printf(" recursed: %u\n", rw->rw_recurse);
938 }
939 db_printf(" waiters: ");
940 switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
941 case RW_LOCK_READ_WAITERS:
942 db_printf("readers\n");
943 break;
944 case RW_LOCK_WRITE_WAITERS:
945 db_printf("writers\n");
946 break;
947 case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
948 db_printf("readers and writers\n");
949 break;
950 default:
951 db_printf("none\n");
952 break;
953 }
954}
955
956#endif
2 * Copyright (c) 2006 John Baldwin <jhb@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/*
31 * Machine independent bits of reader/writer lock implementation.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/kern/kern_rwlock.c 176076 2008-02-07 06:16:54Z jeff $");
36
37#include "opt_ddb.h"
38#include "opt_no_adaptive_rwlocks.h"
39
40#include <sys/param.h>
41#include <sys/ktr.h>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/proc.h>
45#include <sys/rwlock.h>
46#include <sys/systm.h>
47#include <sys/turnstile.h>
48
49#include <machine/cpu.h>
50
51CTASSERT((RW_RECURSE & LO_CLASSFLAGS) == RW_RECURSE);
52
53#if defined(SMP) && !defined(NO_ADAPTIVE_RWLOCKS)
54#define ADAPTIVE_RWLOCKS
55#endif
56
57#ifdef DDB
58#include <ddb/ddb.h>
59
60static void db_show_rwlock(struct lock_object *lock);
61#endif
62static void assert_rw(struct lock_object *lock, int what);
63static void lock_rw(struct lock_object *lock, int how);
64static int unlock_rw(struct lock_object *lock);
65
66struct lock_class lock_class_rw = {
67 .lc_name = "rw",
68 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE | LC_UPGRADABLE,
69 .lc_assert = assert_rw,
70#ifdef DDB
71 .lc_ddb_show = db_show_rwlock,
72#endif
73 .lc_lock = lock_rw,
74 .lc_unlock = unlock_rw,
75};
76
77/*
78 * Return a pointer to the owning thread if the lock is write-locked or
79 * NULL if the lock is unlocked or read-locked.
80 */
81#define rw_wowner(rw) \
82 ((rw)->rw_lock & RW_LOCK_READ ? NULL : \
83 (struct thread *)RW_OWNER((rw)->rw_lock))
84
85/*
86 * Returns if a write owner is recursed. Write ownership is not assured
87 * here and should be previously checked.
88 */
89#define rw_recursed(rw) ((rw)->rw_recurse != 0)
90
91/*
92 * Return true if curthread helds the lock.
93 */
94#define rw_wlocked(rw) (rw_wowner((rw)) == curthread)
95
96/*
97 * Return a pointer to the owning thread for this lock who should receive
98 * any priority lent by threads that block on this lock. Currently this
99 * is identical to rw_wowner().
100 */
101#define rw_owner(rw) rw_wowner(rw)
102
103#ifndef INVARIANTS
104#define _rw_assert(rw, what, file, line)
105#endif
106
107void
108assert_rw(struct lock_object *lock, int what)
109{
110
111 rw_assert((struct rwlock *)lock, what);
112}
113
114void
115lock_rw(struct lock_object *lock, int how)
116{
117 struct rwlock *rw;
118
119 rw = (struct rwlock *)lock;
120 if (how)
121 rw_wlock(rw);
122 else
123 rw_rlock(rw);
124}
125
126int
127unlock_rw(struct lock_object *lock)
128{
129 struct rwlock *rw;
130
131 rw = (struct rwlock *)lock;
132 rw_assert(rw, RA_LOCKED | LA_NOTRECURSED);
133 if (rw->rw_lock & RW_LOCK_READ) {
134 rw_runlock(rw);
135 return (0);
136 } else {
137 rw_wunlock(rw);
138 return (1);
139 }
140}
141
142void
143rw_init_flags(struct rwlock *rw, const char *name, int opts)
144{
145 int flags;
146
147 MPASS((opts & ~(RW_DUPOK | RW_NOPROFILE | RW_NOWITNESS | RW_QUIET |
148 RW_RECURSE)) == 0);
149
150 flags = LO_UPGRADABLE | LO_RECURSABLE;
151 if (opts & RW_DUPOK)
152 flags |= LO_DUPOK;
153 if (opts & RW_NOPROFILE)
154 flags |= LO_NOPROFILE;
155 if (!(opts & RW_NOWITNESS))
156 flags |= LO_WITNESS;
157 if (opts & RW_QUIET)
158 flags |= LO_QUIET;
159 flags |= opts & RW_RECURSE;
160
161 rw->rw_lock = RW_UNLOCKED;
162 rw->rw_recurse = 0;
163 lock_init(&rw->lock_object, &lock_class_rw, name, NULL, flags);
164}
165
166void
167rw_destroy(struct rwlock *rw)
168{
169
170 KASSERT(rw->rw_lock == RW_UNLOCKED, ("rw lock not unlocked"));
171 KASSERT(rw->rw_recurse == 0, ("rw lock still recursed"));
172 rw->rw_lock = RW_DESTROYED;
173 lock_destroy(&rw->lock_object);
174}
175
176void
177rw_sysinit(void *arg)
178{
179 struct rw_args *args = arg;
180
181 rw_init(args->ra_rw, args->ra_desc);
182}
183
184int
185rw_wowned(struct rwlock *rw)
186{
187
188 return (rw_wowner(rw) == curthread);
189}
190
191void
192_rw_wlock(struct rwlock *rw, const char *file, int line)
193{
194
195 MPASS(curthread != NULL);
196 KASSERT(rw->rw_lock != RW_DESTROYED,
197 ("rw_wlock() of destroyed rwlock @ %s:%d", file, line));
198 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, file,
199 line);
200 __rw_wlock(rw, curthread, file, line);
201 LOCK_LOG_LOCK("WLOCK", &rw->lock_object, 0, rw->rw_recurse, file, line);
202 WITNESS_LOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
203 curthread->td_locks++;
204}
205
206void
207_rw_wunlock(struct rwlock *rw, const char *file, int line)
208{
209
210 MPASS(curthread != NULL);
211 KASSERT(rw->rw_lock != RW_DESTROYED,
212 ("rw_wunlock() of destroyed rwlock @ %s:%d", file, line));
213 _rw_assert(rw, RA_WLOCKED, file, line);
214 curthread->td_locks--;
215 WITNESS_UNLOCK(&rw->lock_object, LOP_EXCLUSIVE, file, line);
216 LOCK_LOG_LOCK("WUNLOCK", &rw->lock_object, 0, rw->rw_recurse, file,
217 line);
218 if (!rw_recursed(rw))
219 lock_profile_release_lock(&rw->lock_object);
220 __rw_wunlock(rw, curthread, file, line);
221}
222/*
223 * Determines whether a new reader can acquire a lock. Succeeds if the
224 * reader already owns a read lock and the lock is locked for read to
225 * prevent deadlock from reader recursion. Also succeeds if the lock
226 * is unlocked and has no writer waiters or spinners. Failing otherwise
227 * prioritizes writers before readers.
228 */
229#define RW_CAN_READ(_rw) \
230 ((curthread->td_rw_rlocks && (_rw) & RW_LOCK_READ) || ((_rw) & \
231 (RW_LOCK_READ | RW_LOCK_WRITE_WAITERS | RW_LOCK_WRITE_SPINNER)) == \
232 RW_LOCK_READ)
233
234void
235_rw_rlock(struct rwlock *rw, const char *file, int line)
236{
237 struct turnstile *ts;
238#ifdef ADAPTIVE_RWLOCKS
239 volatile struct thread *owner;
240#endif
241 uint64_t waittime = 0;
242 int contested = 0;
243 uintptr_t v;
244
245 KASSERT(rw->rw_lock != RW_DESTROYED,
246 ("rw_rlock() of destroyed rwlock @ %s:%d", file, line));
247 KASSERT(rw_wowner(rw) != curthread,
248 ("%s (%s): wlock already held @ %s:%d", __func__,
249 rw->lock_object.lo_name, file, line));
250 WITNESS_CHECKORDER(&rw->lock_object, LOP_NEWORDER, file, line);
251
252 for (;;) {
253 /*
254 * Handle the easy case. If no other thread has a write
255 * lock, then try to bump up the count of read locks. Note
256 * that we have to preserve the current state of the
257 * RW_LOCK_WRITE_WAITERS flag. If we fail to acquire a
258 * read lock, then rw_lock must have changed, so restart
259 * the loop. Note that this handles the case of a
260 * completely unlocked rwlock since such a lock is encoded
261 * as a read lock with no waiters.
262 */
263 v = rw->rw_lock;
264 if (RW_CAN_READ(v)) {
265 /*
266 * The RW_LOCK_READ_WAITERS flag should only be set
267 * if the lock has been unlocked and write waiters
268 * were present.
269 */
270 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v,
271 v + RW_ONE_READER)) {
272 if (LOCK_LOG_TEST(&rw->lock_object, 0))
273 CTR4(KTR_LOCK,
274 "%s: %p succeed %p -> %p", __func__,
275 rw, (void *)v,
276 (void *)(v + RW_ONE_READER));
277 break;
278 }
279 cpu_spinwait();
280 continue;
281 }
282 lock_profile_obtain_lock_failed(&rw->lock_object,
283 &contested, &waittime);
284
285#ifdef ADAPTIVE_RWLOCKS
286 /*
287 * If the owner is running on another CPU, spin until
288 * the owner stops running or the state of the lock
289 * changes.
290 */
291 if ((v & RW_LOCK_READ) == 0) {
292 owner = (struct thread *)RW_OWNER(v);
293 if (TD_IS_RUNNING(owner)) {
294 if (LOCK_LOG_TEST(&rw->lock_object, 0))
295 CTR3(KTR_LOCK,
296 "%s: spinning on %p held by %p",
297 __func__, rw, owner);
298 while ((struct thread*)RW_OWNER(rw->rw_lock) ==
299 owner && TD_IS_RUNNING(owner))
300 cpu_spinwait();
301 continue;
302 }
303 }
304#endif
305
306 /*
307 * Okay, now it's the hard case. Some other thread already
308 * has a write lock or there are write waiters present,
309 * acquire the turnstile lock so we can begin the process
310 * of blocking.
311 */
312 ts = turnstile_trywait(&rw->lock_object);
313
314 /*
315 * The lock might have been released while we spun, so
316 * recheck its state and restart the loop if needed.
317 */
318 v = rw->rw_lock;
319 if (RW_CAN_READ(v)) {
320 turnstile_cancel(ts);
321 cpu_spinwait();
322 continue;
323 }
324
325#ifdef ADAPTIVE_RWLOCKS
326 /*
327 * If the current owner of the lock is executing on another
328 * CPU quit the hard path and try to spin.
329 */
330 if ((v & RW_LOCK_READ) == 0) {
331 owner = (struct thread *)RW_OWNER(v);
332 if (TD_IS_RUNNING(owner)) {
333 turnstile_cancel(ts);
334 cpu_spinwait();
335 continue;
336 }
337 }
338#endif
339
340 /*
341 * The lock is held in write mode or it already has waiters.
342 */
343 MPASS(!RW_CAN_READ(v));
344
345 /*
346 * If the RW_LOCK_READ_WAITERS flag is already set, then
347 * we can go ahead and block. If it is not set then try
348 * to set it. If we fail to set it drop the turnstile
349 * lock and restart the loop.
350 */
351 if (!(v & RW_LOCK_READ_WAITERS)) {
352 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
353 v | RW_LOCK_READ_WAITERS)) {
354 turnstile_cancel(ts);
355 cpu_spinwait();
356 continue;
357 }
358 if (LOCK_LOG_TEST(&rw->lock_object, 0))
359 CTR2(KTR_LOCK, "%s: %p set read waiters flag",
360 __func__, rw);
361 }
362
363 /*
364 * We were unable to acquire the lock and the read waiters
365 * flag is set, so we must block on the turnstile.
366 */
367 if (LOCK_LOG_TEST(&rw->lock_object, 0))
368 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
369 rw);
370 turnstile_wait(ts, rw_owner(rw), TS_SHARED_QUEUE);
371 if (LOCK_LOG_TEST(&rw->lock_object, 0))
372 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
373 __func__, rw);
374 }
375
376 /*
377 * TODO: acquire "owner of record" here. Here be turnstile dragons
378 * however. turnstiles don't like owners changing between calls to
379 * turnstile_wait() currently.
380 */
381 lock_profile_obtain_lock_success( &rw->lock_object, contested,
382 waittime, file, line);
383 LOCK_LOG_LOCK("RLOCK", &rw->lock_object, 0, 0, file, line);
384 WITNESS_LOCK(&rw->lock_object, 0, file, line);
385 curthread->td_locks++;
386 curthread->td_rw_rlocks++;
387}
388
389void
390_rw_runlock(struct rwlock *rw, const char *file, int line)
391{
392 struct turnstile *ts;
393 uintptr_t x, v, queue;
394
395 KASSERT(rw->rw_lock != RW_DESTROYED,
396 ("rw_runlock() of destroyed rwlock @ %s:%d", file, line));
397 _rw_assert(rw, RA_RLOCKED, file, line);
398 curthread->td_locks--;
399 curthread->td_rw_rlocks--;
400 WITNESS_UNLOCK(&rw->lock_object, 0, file, line);
401 LOCK_LOG_LOCK("RUNLOCK", &rw->lock_object, 0, 0, file, line);
402
403 /* TODO: drop "owner of record" here. */
404
405 for (;;) {
406 /*
407 * See if there is more than one read lock held. If so,
408 * just drop one and return.
409 */
410 x = rw->rw_lock;
411 if (RW_READERS(x) > 1) {
412 if (atomic_cmpset_ptr(&rw->rw_lock, x,
413 x - RW_ONE_READER)) {
414 if (LOCK_LOG_TEST(&rw->lock_object, 0))
415 CTR4(KTR_LOCK,
416 "%s: %p succeeded %p -> %p",
417 __func__, rw, (void *)x,
418 (void *)(x - RW_ONE_READER));
419 break;
420 }
421 continue;
422 }
423 /*
424 * If there aren't any waiters for a write lock, then try
425 * to drop it quickly.
426 */
427 if (!(x & RW_LOCK_WAITERS)) {
428 MPASS((x & ~RW_LOCK_WRITE_SPINNER) ==
429 RW_READERS_LOCK(1));
430 if (atomic_cmpset_ptr(&rw->rw_lock, x, RW_UNLOCKED)) {
431 if (LOCK_LOG_TEST(&rw->lock_object, 0))
432 CTR2(KTR_LOCK, "%s: %p last succeeded",
433 __func__, rw);
434 break;
435 }
436 continue;
437 }
438 /*
439 * Ok, we know we have waiters and we think we are the
440 * last reader, so grab the turnstile lock.
441 */
442 turnstile_chain_lock(&rw->lock_object);
443 v = rw->rw_lock & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
444 MPASS(v & RW_LOCK_WAITERS);
445
446 /*
447 * Try to drop our lock leaving the lock in a unlocked
448 * state.
449 *
450 * If you wanted to do explicit lock handoff you'd have to
451 * do it here. You'd also want to use turnstile_signal()
452 * and you'd have to handle the race where a higher
453 * priority thread blocks on the write lock before the
454 * thread you wakeup actually runs and have the new thread
455 * "steal" the lock. For now it's a lot simpler to just
456 * wakeup all of the waiters.
457 *
458 * As above, if we fail, then another thread might have
459 * acquired a read lock, so drop the turnstile lock and
460 * restart.
461 */
462 x = RW_UNLOCKED;
463 if (v & RW_LOCK_WRITE_WAITERS) {
464 queue = TS_EXCLUSIVE_QUEUE;
465 x |= (v & RW_LOCK_READ_WAITERS);
466 } else
467 queue = TS_SHARED_QUEUE;
468 if (!atomic_cmpset_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v,
469 x)) {
470 turnstile_chain_unlock(&rw->lock_object);
471 continue;
472 }
473 if (LOCK_LOG_TEST(&rw->lock_object, 0))
474 CTR2(KTR_LOCK, "%s: %p last succeeded with waiters",
475 __func__, rw);
476
477 /*
478 * Ok. The lock is released and all that's left is to
479 * wake up the waiters. Note that the lock might not be
480 * free anymore, but in that case the writers will just
481 * block again if they run before the new lock holder(s)
482 * release the lock.
483 */
484 ts = turnstile_lookup(&rw->lock_object);
485 MPASS(ts != NULL);
486 turnstile_broadcast(ts, queue);
487 turnstile_unpend(ts, TS_SHARED_LOCK);
488 turnstile_chain_unlock(&rw->lock_object);
489 break;
490 }
491 lock_profile_release_lock(&rw->lock_object);
492}
493
494/*
495 * This function is called when we are unable to obtain a write lock on the
496 * first try. This means that at least one other thread holds either a
497 * read or write lock.
498 */
499void
500_rw_wlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
501{
502 struct turnstile *ts;
503#ifdef ADAPTIVE_RWLOCKS
504 volatile struct thread *owner;
505 int spintries = 0;
506 int i;
507#endif
508 uint64_t waittime = 0;
509 uintptr_t v, x;
510 int contested = 0;
511
512 if (rw_wlocked(rw)) {
513 KASSERT(rw->lock_object.lo_flags & RW_RECURSE,
514 ("%s: recursing but non-recursive rw %s @ %s:%d\n",
515 __func__, rw->lock_object.lo_name, file, line));
516 rw->rw_recurse++;
517 if (LOCK_LOG_TEST(&rw->lock_object, 0))
518 CTR2(KTR_LOCK, "%s: %p recursing", __func__, rw);
519 return;
520 }
521
522 if (LOCK_LOG_TEST(&rw->lock_object, 0))
523 CTR5(KTR_LOCK, "%s: %s contested (lock=%p) at %s:%d", __func__,
524 rw->lock_object.lo_name, (void *)rw->rw_lock, file, line);
525
526 while (!_rw_write_lock(rw, tid)) {
527 lock_profile_obtain_lock_failed(&rw->lock_object,
528 &contested, &waittime);
529#ifdef ADAPTIVE_RWLOCKS
530 /*
531 * If the lock is write locked and the owner is
532 * running on another CPU, spin until the owner stops
533 * running or the state of the lock changes.
534 */
535 v = rw->rw_lock;
536 owner = (struct thread *)RW_OWNER(v);
537 if (!(v & RW_LOCK_READ) && TD_IS_RUNNING(owner)) {
538 if (LOCK_LOG_TEST(&rw->lock_object, 0))
539 CTR3(KTR_LOCK, "%s: spinning on %p held by %p",
540 __func__, rw, owner);
541 while ((struct thread*)RW_OWNER(rw->rw_lock) == owner &&
542 TD_IS_RUNNING(owner))
543 cpu_spinwait();
544 continue;
545 }
546 if ((v & RW_LOCK_READ) && RW_READERS(v) && spintries < 100) {
547 if (!(v & RW_LOCK_WRITE_SPINNER)) {
548 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
549 v | RW_LOCK_WRITE_SPINNER)) {
550 cpu_spinwait();
551 continue;
552 }
553 }
554 spintries++;
555 for (i = 100000; i > 0; i--) {
556 if ((rw->rw_lock & RW_LOCK_WRITE_SPINNER) == 0)
557 break;
558 cpu_spinwait();
559 }
560 if (i)
561 continue;
562 }
563#endif
564 ts = turnstile_trywait(&rw->lock_object);
565 v = rw->rw_lock;
566
567#ifdef ADAPTIVE_RWLOCKS
568 /*
569 * If the current owner of the lock is executing on another
570 * CPU quit the hard path and try to spin.
571 */
572 if (!(v & RW_LOCK_READ)) {
573 owner = (struct thread *)RW_OWNER(v);
574 if (TD_IS_RUNNING(owner)) {
575 turnstile_cancel(ts);
576 cpu_spinwait();
577 continue;
578 }
579 }
580#endif
581 /*
582 * If the lock was released while waiting for the turnstile
583 * chain lock retry.
584 */
585 x = v & (RW_LOCK_WAITERS | RW_LOCK_WRITE_SPINNER);
586 if ((v & ~x) == RW_UNLOCKED) {
587 x &= ~RW_LOCK_WRITE_SPINNER;
588 if (atomic_cmpset_acq_ptr(&rw->rw_lock, v, tid | x)) {
589 if (x)
590 turnstile_claim(ts);
591 else
592 turnstile_cancel(ts);
593 break;
594 }
595 turnstile_cancel(ts);
596 cpu_spinwait();
597 continue;
598 }
599 /*
600 * If the RW_LOCK_WRITE_WAITERS flag isn't set, then try to
601 * set it. If we fail to set it, then loop back and try
602 * again.
603 */
604 if (!(v & RW_LOCK_WRITE_WAITERS)) {
605 if (!atomic_cmpset_ptr(&rw->rw_lock, v,
606 v | RW_LOCK_WRITE_WAITERS)) {
607 turnstile_cancel(ts);
608 cpu_spinwait();
609 continue;
610 }
611 if (LOCK_LOG_TEST(&rw->lock_object, 0))
612 CTR2(KTR_LOCK, "%s: %p set write waiters flag",
613 __func__, rw);
614 }
615 /*
616 * We were unable to acquire the lock and the write waiters
617 * flag is set, so we must block on the turnstile.
618 */
619 if (LOCK_LOG_TEST(&rw->lock_object, 0))
620 CTR2(KTR_LOCK, "%s: %p blocking on turnstile", __func__,
621 rw);
622 turnstile_wait(ts, rw_owner(rw), TS_EXCLUSIVE_QUEUE);
623 if (LOCK_LOG_TEST(&rw->lock_object, 0))
624 CTR2(KTR_LOCK, "%s: %p resuming from turnstile",
625 __func__, rw);
626#ifdef ADAPTIVE_RWLOCKS
627 spintries = 0;
628#endif
629 }
630 lock_profile_obtain_lock_success(&rw->lock_object, contested, waittime,
631 file, line);
632}
633
634/*
635 * This function is called if the first try at releasing a write lock failed.
636 * This means that one of the 2 waiter bits must be set indicating that at
637 * least one thread is waiting on this lock.
638 */
639void
640_rw_wunlock_hard(struct rwlock *rw, uintptr_t tid, const char *file, int line)
641{
642 struct turnstile *ts;
643 uintptr_t v;
644 int queue;
645
646 if (rw_wlocked(rw) && rw_recursed(rw)) {
647 rw->rw_recurse--;
648 if (LOCK_LOG_TEST(&rw->lock_object, 0))
649 CTR2(KTR_LOCK, "%s: %p unrecursing", __func__, rw);
650 return;
651 }
652 v = rw->rw_lock;
653
654 KASSERT(rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS),
655 ("%s: neither of the waiter flags are set", __func__));
656
657 if (LOCK_LOG_TEST(&rw->lock_object, 0))
658 CTR2(KTR_LOCK, "%s: %p contested", __func__, rw);
659
660 turnstile_chain_lock(&rw->lock_object);
661 ts = turnstile_lookup(&rw->lock_object);
662
663 MPASS(ts != NULL);
664
665 /*
666 * Use the same algo as sx locks for now. Prefer waking up shared
667 * waiters if we have any over writers. This is probably not ideal.
668 *
669 * 'v' is the value we are going to write back to rw_lock. If we
670 * have waiters on both queues, we need to preserve the state of
671 * the waiter flag for the queue we don't wake up. For now this is
672 * hardcoded for the algorithm mentioned above.
673 *
674 * In the case of both readers and writers waiting we wakeup the
675 * readers but leave the RW_LOCK_WRITE_WAITERS flag set. If a
676 * new writer comes in before a reader it will claim the lock up
677 * above. There is probably a potential priority inversion in
678 * there that could be worked around either by waking both queues
679 * of waiters or doing some complicated lock handoff gymnastics.
680 */
681 v = RW_UNLOCKED;
682 if (rw->rw_lock & RW_LOCK_WRITE_WAITERS) {
683 queue = TS_EXCLUSIVE_QUEUE;
684 v |= (rw->rw_lock & RW_LOCK_READ_WAITERS);
685 } else
686 queue = TS_SHARED_QUEUE;
687
688 /* Wake up all waiters for the specific queue. */
689 if (LOCK_LOG_TEST(&rw->lock_object, 0))
690 CTR3(KTR_LOCK, "%s: %p waking up %s waiters", __func__, rw,
691 queue == TS_SHARED_QUEUE ? "read" : "write");
692 turnstile_broadcast(ts, queue);
693 atomic_store_rel_ptr(&rw->rw_lock, v);
694 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
695 turnstile_chain_unlock(&rw->lock_object);
696}
697
698/*
699 * Attempt to do a non-blocking upgrade from a read lock to a write
700 * lock. This will only succeed if this thread holds a single read
701 * lock. Returns true if the upgrade succeeded and false otherwise.
702 */
703int
704_rw_try_upgrade(struct rwlock *rw, const char *file, int line)
705{
706 uintptr_t v, x, tid;
707 struct turnstile *ts;
708 int success;
709
710 KASSERT(rw->rw_lock != RW_DESTROYED,
711 ("rw_try_upgrade() of destroyed rwlock @ %s:%d", file, line));
712 _rw_assert(rw, RA_RLOCKED, file, line);
713
714 /*
715 * Attempt to switch from one reader to a writer. If there
716 * are any write waiters, then we will have to lock the
717 * turnstile first to prevent races with another writer
718 * calling turnstile_wait() before we have claimed this
719 * turnstile. So, do the simple case of no waiters first.
720 */
721 tid = (uintptr_t)curthread;
722 success = 0;
723 for (;;) {
724 v = rw->rw_lock;
725 if (RW_READERS(v) > 1)
726 break;
727 if (!(v & RW_LOCK_WAITERS)) {
728 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid);
729 if (!success)
730 continue;
731 break;
732 }
733
734 /*
735 * Ok, we think we have waiters, so lock the turnstile.
736 */
737 ts = turnstile_trywait(&rw->lock_object);
738 v = rw->rw_lock;
739 if (RW_READERS(v) > 1) {
740 turnstile_cancel(ts);
741 break;
742 }
743 /*
744 * Try to switch from one reader to a writer again. This time
745 * we honor the current state of the waiters flags.
746 * If we obtain the lock with the flags set, then claim
747 * ownership of the turnstile.
748 */
749 x = rw->rw_lock & RW_LOCK_WAITERS;
750 success = atomic_cmpset_ptr(&rw->rw_lock, v, tid | x);
751 if (success) {
752 if (x)
753 turnstile_claim(ts);
754 else
755 turnstile_cancel(ts);
756 break;
757 }
758 turnstile_cancel(ts);
759 }
760 LOCK_LOG_TRY("WUPGRADE", &rw->lock_object, 0, success, file, line);
761 if (success) {
762 curthread->td_rw_rlocks--;
763 WITNESS_UPGRADE(&rw->lock_object, LOP_EXCLUSIVE | LOP_TRYLOCK,
764 file, line);
765 }
766 return (success);
767}
768
769/*
770 * Downgrade a write lock into a single read lock.
771 */
772void
773_rw_downgrade(struct rwlock *rw, const char *file, int line)
774{
775 struct turnstile *ts;
776 uintptr_t tid, v;
777 int rwait, wwait;
778
779 KASSERT(rw->rw_lock != RW_DESTROYED,
780 ("rw_downgrade() of destroyed rwlock @ %s:%d", file, line));
781 _rw_assert(rw, RA_WLOCKED | RA_NOTRECURSED, file, line);
782#ifndef INVARIANTS
783 if (rw_recursed(rw))
784 panic("downgrade of a recursed lock");
785#endif
786
787 WITNESS_DOWNGRADE(&rw->lock_object, 0, file, line);
788
789 /*
790 * Convert from a writer to a single reader. First we handle
791 * the easy case with no waiters. If there are any waiters, we
792 * lock the turnstile and "disown" the lock.
793 */
794 tid = (uintptr_t)curthread;
795 if (atomic_cmpset_rel_ptr(&rw->rw_lock, tid, RW_READERS_LOCK(1)))
796 goto out;
797
798 /*
799 * Ok, we think we have waiters, so lock the turnstile so we can
800 * read the waiter flags without any races.
801 */
802 turnstile_chain_lock(&rw->lock_object);
803 v = rw->rw_lock & RW_LOCK_WAITERS;
804 rwait = v & RW_LOCK_READ_WAITERS;
805 wwait = v & RW_LOCK_WRITE_WAITERS;
806 MPASS(rwait | wwait);
807
808 /*
809 * Downgrade from a write lock while preserving waiters flag
810 * and give up ownership of the turnstile.
811 */
812 ts = turnstile_lookup(&rw->lock_object);
813 MPASS(ts != NULL);
814 if (!wwait)
815 v &= ~RW_LOCK_READ_WAITERS;
816 atomic_store_rel_ptr(&rw->rw_lock, RW_READERS_LOCK(1) | v);
817 /*
818 * Wake other readers if there are no writers pending. Otherwise they
819 * won't be able to acquire the lock anyway.
820 */
821 if (rwait && !wwait) {
822 turnstile_broadcast(ts, TS_SHARED_QUEUE);
823 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
824 } else
825 turnstile_disown(ts);
826 turnstile_chain_unlock(&rw->lock_object);
827out:
828 curthread->td_rw_rlocks++;
829 LOCK_LOG_LOCK("WDOWNGRADE", &rw->lock_object, 0, 0, file, line);
830}
831
832#ifdef INVARIANT_SUPPORT
833#ifndef INVARIANTS
834#undef _rw_assert
835#endif
836
837/*
838 * In the non-WITNESS case, rw_assert() can only detect that at least
839 * *some* thread owns an rlock, but it cannot guarantee that *this*
840 * thread owns an rlock.
841 */
842void
843_rw_assert(struct rwlock *rw, int what, const char *file, int line)
844{
845
846 if (panicstr != NULL)
847 return;
848 switch (what) {
849 case RA_LOCKED:
850 case RA_LOCKED | RA_RECURSED:
851 case RA_LOCKED | RA_NOTRECURSED:
852 case RA_RLOCKED:
853#ifdef WITNESS
854 witness_assert(&rw->lock_object, what, file, line);
855#else
856 /*
857 * If some other thread has a write lock or we have one
858 * and are asserting a read lock, fail. Also, if no one
859 * has a lock at all, fail.
860 */
861 if (rw->rw_lock == RW_UNLOCKED ||
862 (!(rw->rw_lock & RW_LOCK_READ) && (what == RA_RLOCKED ||
863 rw_wowner(rw) != curthread)))
864 panic("Lock %s not %slocked @ %s:%d\n",
865 rw->lock_object.lo_name, (what == RA_RLOCKED) ?
866 "read " : "", file, line);
867
868 if (!(rw->rw_lock & RW_LOCK_READ)) {
869 if (rw_recursed(rw)) {
870 if (what & RA_NOTRECURSED)
871 panic("Lock %s recursed @ %s:%d\n",
872 rw->lock_object.lo_name, file,
873 line);
874 } else if (what & RA_RECURSED)
875 panic("Lock %s not recursed @ %s:%d\n",
876 rw->lock_object.lo_name, file, line);
877 }
878#endif
879 break;
880 case RA_WLOCKED:
881 case RA_WLOCKED | RA_RECURSED:
882 case RA_WLOCKED | RA_NOTRECURSED:
883 if (rw_wowner(rw) != curthread)
884 panic("Lock %s not exclusively locked @ %s:%d\n",
885 rw->lock_object.lo_name, file, line);
886 if (rw_recursed(rw)) {
887 if (what & RA_NOTRECURSED)
888 panic("Lock %s recursed @ %s:%d\n",
889 rw->lock_object.lo_name, file, line);
890 } else if (what & RA_RECURSED)
891 panic("Lock %s not recursed @ %s:%d\n",
892 rw->lock_object.lo_name, file, line);
893 break;
894 case RA_UNLOCKED:
895#ifdef WITNESS
896 witness_assert(&rw->lock_object, what, file, line);
897#else
898 /*
899 * If we hold a write lock fail. We can't reliably check
900 * to see if we hold a read lock or not.
901 */
902 if (rw_wowner(rw) == curthread)
903 panic("Lock %s exclusively locked @ %s:%d\n",
904 rw->lock_object.lo_name, file, line);
905#endif
906 break;
907 default:
908 panic("Unknown rw lock assertion: %d @ %s:%d", what, file,
909 line);
910 }
911}
912#endif /* INVARIANT_SUPPORT */
913
914#ifdef DDB
915void
916db_show_rwlock(struct lock_object *lock)
917{
918 struct rwlock *rw;
919 struct thread *td;
920
921 rw = (struct rwlock *)lock;
922
923 db_printf(" state: ");
924 if (rw->rw_lock == RW_UNLOCKED)
925 db_printf("UNLOCKED\n");
926 else if (rw->rw_lock == RW_DESTROYED) {
927 db_printf("DESTROYED\n");
928 return;
929 } else if (rw->rw_lock & RW_LOCK_READ)
930 db_printf("RLOCK: %ju locks\n",
931 (uintmax_t)(RW_READERS(rw->rw_lock)));
932 else {
933 td = rw_wowner(rw);
934 db_printf("WLOCK: %p (tid %d, pid %d, \"%s\")\n", td,
935 td->td_tid, td->td_proc->p_pid, td->td_name);
936 if (rw_recursed(rw))
937 db_printf(" recursed: %u\n", rw->rw_recurse);
938 }
939 db_printf(" waiters: ");
940 switch (rw->rw_lock & (RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS)) {
941 case RW_LOCK_READ_WAITERS:
942 db_printf("readers\n");
943 break;
944 case RW_LOCK_WRITE_WAITERS:
945 db_printf("writers\n");
946 break;
947 case RW_LOCK_READ_WAITERS | RW_LOCK_WRITE_WAITERS:
948 db_printf("readers and writers\n");
949 break;
950 default:
951 db_printf("none\n");
952 break;
953 }
954}
955
956#endif