2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the author nor the names of any co-contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Machine independent bits of reader/writer lock implementation.
36 #include <sys/cdefs.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
42 #include <sys/kernel.h>
46 #include <sys/mutex.h>
48 #include <sys/rmlock.h>
49 #include <sys/sched.h>
51 #include <sys/turnstile.h>
52 #include <sys/lock_profile.h>
53 #include <machine/cpu.h>
61 * A cookie to mark destroyed rmlocks. This is stored in the head of
64 #define RM_DESTROYED ((void *)0xdead)
66 #define rm_destroyed(rm) \
67 (LIST_FIRST(&(rm)->rm_activeReaders) == RM_DESTROYED)
69 #define RMPF_ONQUEUE 1
73 #define _rm_assert(c, what, file, line)
76 static void assert_rm(const struct lock_object *lock, int what);
78 static void db_show_rm(const struct lock_object *lock);
80 static void lock_rm(struct lock_object *lock, uintptr_t how);
82 static int owner_rm(const struct lock_object *lock, struct thread **owner);
84 static uintptr_t unlock_rm(struct lock_object *lock);
86 struct lock_class lock_class_rm = {
88 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
89 .lc_assert = assert_rm,
91 .lc_ddb_show = db_show_rm,
94 .lc_unlock = unlock_rm,
100 struct lock_class lock_class_rm_sleepable = {
101 .lc_name = "sleepable rm",
102 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE,
103 .lc_assert = assert_rm,
105 .lc_ddb_show = db_show_rm,
108 .lc_unlock = unlock_rm,
110 .lc_owner = owner_rm,
115 assert_rm(const struct lock_object *lock, int what)
118 rm_assert((const struct rmlock *)lock, what);
122 lock_rm(struct lock_object *lock, uintptr_t how)
125 struct rm_priotracker *tracker;
127 rm = (struct rmlock *)lock;
131 tracker = (struct rm_priotracker *)how;
132 rm_rlock(rm, tracker);
137 unlock_rm(struct lock_object *lock)
142 struct rm_queue *queue;
143 struct rm_priotracker *tracker;
146 rm = (struct rmlock *)lock;
149 rm_assert(rm, RA_LOCKED | RA_NOTRECURSED);
154 * Find the right rm_priotracker structure for curthread.
155 * The guarantee about its uniqueness is given by the fact
156 * we already asserted the lock wasn't recursively acquired.
161 for (queue = pc->pc_rm_queue.rmq_next;
162 queue != &pc->pc_rm_queue; queue = queue->rmq_next) {
163 tracker = (struct rm_priotracker *)queue;
164 if ((tracker->rmp_rmlock == rm) &&
165 (tracker->rmp_thread == td)) {
166 how = (uintptr_t)tracker;
170 KASSERT(tracker != NULL,
171 ("rm_priotracker is non-NULL when lock held in read mode"));
173 rm_runlock(rm, tracker);
180 owner_rm(const struct lock_object *lock, struct thread **owner)
182 const struct rmlock *rm;
183 struct lock_class *lc;
185 rm = (const struct rmlock *)lock;
186 lc = LOCK_CLASS(&rm->rm_wlock_object);
187 return (lc->lc_owner(&rm->rm_wlock_object, owner));
191 static struct mtx rm_spinlock;
193 MTX_SYSINIT(rm_spinlock, &rm_spinlock, "rm_spinlock", MTX_SPIN);
196 * Add or remove tracker from per-cpu list.
198 * The per-cpu list can be traversed at any time in forward direction from an
199 * interrupt on the *local* cpu.
202 rm_tracker_add(struct pcpu *pc, struct rm_priotracker *tracker)
204 struct rm_queue *next;
206 /* Initialize all tracker pointers */
207 tracker->rmp_cpuQueue.rmq_prev = &pc->pc_rm_queue;
208 next = pc->pc_rm_queue.rmq_next;
209 tracker->rmp_cpuQueue.rmq_next = next;
211 /* rmq_prev is not used during froward traversal. */
212 next->rmq_prev = &tracker->rmp_cpuQueue;
214 /* Update pointer to first element. */
215 pc->pc_rm_queue.rmq_next = &tracker->rmp_cpuQueue;
219 * Return a count of the number of trackers the thread 'td' already
220 * has on this CPU for the lock 'rm'.
223 rm_trackers_present(const struct pcpu *pc, const struct rmlock *rm,
224 const struct thread *td)
226 struct rm_queue *queue;
227 struct rm_priotracker *tracker;
231 for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
232 queue = queue->rmq_next) {
233 tracker = (struct rm_priotracker *)queue;
234 if ((tracker->rmp_rmlock == rm) && (tracker->rmp_thread == td))
241 rm_tracker_remove(struct pcpu *pc, struct rm_priotracker *tracker)
243 struct rm_queue *next, *prev;
245 next = tracker->rmp_cpuQueue.rmq_next;
246 prev = tracker->rmp_cpuQueue.rmq_prev;
248 /* Not used during forward traversal. */
249 next->rmq_prev = prev;
251 /* Remove from list. */
252 prev->rmq_next = next;
256 rm_cleanIPI(void *arg)
259 struct rmlock *rm = arg;
260 struct rm_priotracker *tracker;
261 struct rm_queue *queue;
264 for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
265 queue = queue->rmq_next) {
266 tracker = (struct rm_priotracker *)queue;
267 if (tracker->rmp_rmlock == rm && tracker->rmp_flags == 0) {
268 tracker->rmp_flags = RMPF_ONQUEUE;
269 mtx_lock_spin(&rm_spinlock);
270 LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
272 mtx_unlock_spin(&rm_spinlock);
278 rm_init_flags(struct rmlock *rm, const char *name, int opts)
280 struct lock_class *lc;
284 if (!(opts & RM_NOWITNESS))
285 liflags |= LO_WITNESS;
286 if (opts & RM_RECURSE)
287 liflags |= LO_RECURSABLE;
292 rm->rm_writecpus = all_cpus;
293 LIST_INIT(&rm->rm_activeReaders);
294 if (opts & RM_SLEEPABLE) {
295 liflags |= LO_SLEEPABLE;
296 lc = &lock_class_rm_sleepable;
297 xflags = (opts & RM_NEW ? SX_NEW : 0);
298 sx_init_flags(&rm->rm_lock_sx, "rmlock_sx",
299 xflags | SX_NOWITNESS);
302 xflags = (opts & RM_NEW ? MTX_NEW : 0);
303 mtx_init(&rm->rm_lock_mtx, name, "rmlock_mtx",
304 xflags | MTX_NOWITNESS);
306 lock_init(&rm->lock_object, lc, name, NULL, liflags);
310 rm_init(struct rmlock *rm, const char *name)
313 rm_init_flags(rm, name, 0);
317 rm_destroy(struct rmlock *rm)
320 rm_assert(rm, RA_UNLOCKED);
321 LIST_FIRST(&rm->rm_activeReaders) = RM_DESTROYED;
322 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
323 sx_destroy(&rm->rm_lock_sx);
325 mtx_destroy(&rm->rm_lock_mtx);
326 lock_destroy(&rm->lock_object);
330 rm_wowned(const struct rmlock *rm)
333 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
334 return (sx_xlocked(&rm->rm_lock_sx));
336 return (mtx_owned(&rm->rm_lock_mtx));
340 rm_sysinit(void *arg)
342 struct rm_args *args;
345 rm_init_flags(args->ra_rm, args->ra_desc, args->ra_flags);
348 static __noinline int
349 _rm_rlock_hard(struct rmlock *rm, struct rm_priotracker *tracker, int trylock)
356 /* Check if we just need to do a proper critical_exit. */
357 if (!CPU_ISSET(pc->pc_cpuid, &rm->rm_writecpus)) {
362 /* Remove our tracker from the per-cpu list. */
363 rm_tracker_remove(pc, tracker);
366 * Check to see if the IPI granted us the lock after all. The load of
367 * rmp_flags must happen after the tracker is removed from the list.
369 atomic_interrupt_fence();
370 if (tracker->rmp_flags) {
371 /* Just add back tracker - we hold the lock. */
372 rm_tracker_add(pc, tracker);
378 * We allow readers to acquire a lock even if a writer is blocked if
379 * the lock is recursive and the reader already holds the lock.
381 if ((rm->lock_object.lo_flags & LO_RECURSABLE) != 0) {
383 * Just grant the lock if this thread already has a tracker
384 * for this lock on the per-cpu queue.
386 if (rm_trackers_present(pc, rm, curthread) != 0) {
387 mtx_lock_spin(&rm_spinlock);
388 LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
390 tracker->rmp_flags = RMPF_ONQUEUE;
391 mtx_unlock_spin(&rm_spinlock);
392 rm_tracker_add(pc, tracker);
402 if (rm->lock_object.lo_flags & LO_SLEEPABLE) {
403 if (!sx_try_xlock(&rm->rm_lock_sx))
406 if (!mtx_trylock(&rm->rm_lock_mtx))
410 if (rm->lock_object.lo_flags & LO_SLEEPABLE) {
411 THREAD_SLEEPING_OK();
412 sx_xlock(&rm->rm_lock_sx);
413 THREAD_NO_SLEEPING();
415 mtx_lock(&rm->rm_lock_mtx);
420 CPU_CLR(pc->pc_cpuid, &rm->rm_writecpus);
421 rm_tracker_add(pc, tracker);
425 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
426 sx_xunlock(&rm->rm_lock_sx);
428 mtx_unlock(&rm->rm_lock_mtx);
434 _rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker, int trylock)
436 struct thread *td = curthread;
439 if (SCHEDULER_STOPPED())
442 tracker->rmp_flags = 0;
443 tracker->rmp_thread = td;
444 tracker->rmp_rmlock = rm;
446 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
447 THREAD_NO_SLEEPING();
449 td->td_critnest++; /* critical_enter(); */
450 atomic_interrupt_fence();
452 pc = cpuid_to_pcpu[td->td_oncpu];
453 rm_tracker_add(pc, tracker);
456 atomic_interrupt_fence();
460 * Fast path to combine two common conditions into a single
463 if (__predict_true(0 == (td->td_owepreempt |
464 CPU_ISSET(pc->pc_cpuid, &rm->rm_writecpus))))
467 /* We do not have a read token and need to acquire one. */
468 return _rm_rlock_hard(rm, tracker, trylock);
471 static __noinline void
472 _rm_unlock_hard(struct thread *td,struct rm_priotracker *tracker)
475 if (td->td_owepreempt) {
480 if (!tracker->rmp_flags)
483 mtx_lock_spin(&rm_spinlock);
484 LIST_REMOVE(tracker, rmp_qentry);
486 if (tracker->rmp_flags & RMPF_SIGNAL) {
488 struct turnstile *ts;
490 rm = tracker->rmp_rmlock;
492 turnstile_chain_lock(&rm->lock_object);
493 mtx_unlock_spin(&rm_spinlock);
495 ts = turnstile_lookup(&rm->lock_object);
497 turnstile_signal(ts, TS_EXCLUSIVE_QUEUE);
498 turnstile_unpend(ts);
499 turnstile_chain_unlock(&rm->lock_object);
501 mtx_unlock_spin(&rm_spinlock);
505 _rm_runlock(struct rmlock *rm, struct rm_priotracker *tracker)
508 struct thread *td = tracker->rmp_thread;
510 if (SCHEDULER_STOPPED())
513 td->td_critnest++; /* critical_enter(); */
514 atomic_interrupt_fence();
516 pc = cpuid_to_pcpu[td->td_oncpu];
517 rm_tracker_remove(pc, tracker);
519 atomic_interrupt_fence();
523 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
524 THREAD_SLEEPING_OK();
526 if (__predict_true(0 == (td->td_owepreempt | tracker->rmp_flags)))
529 _rm_unlock_hard(td, tracker);
533 _rm_wlock(struct rmlock *rm)
535 struct rm_priotracker *prio;
536 struct turnstile *ts;
539 if (SCHEDULER_STOPPED())
542 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
543 sx_xlock(&rm->rm_lock_sx);
545 mtx_lock(&rm->rm_lock_mtx);
547 if (CPU_CMP(&rm->rm_writecpus, &all_cpus)) {
548 /* Get all read tokens back */
550 CPU_ANDNOT(&readcpus, &readcpus, &rm->rm_writecpus);
551 rm->rm_writecpus = all_cpus;
554 * Assumes rm->rm_writecpus update is visible on other CPUs
555 * before rm_cleanIPI is called.
558 smp_rendezvous_cpus(readcpus,
559 smp_no_rendezvous_barrier,
561 smp_no_rendezvous_barrier,
568 mtx_lock_spin(&rm_spinlock);
569 while ((prio = LIST_FIRST(&rm->rm_activeReaders)) != NULL) {
570 ts = turnstile_trywait(&rm->lock_object);
571 prio->rmp_flags = RMPF_ONQUEUE | RMPF_SIGNAL;
572 mtx_unlock_spin(&rm_spinlock);
573 turnstile_wait(ts, prio->rmp_thread,
575 mtx_lock_spin(&rm_spinlock);
577 mtx_unlock_spin(&rm_spinlock);
582 _rm_wunlock(struct rmlock *rm)
585 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
586 sx_xunlock(&rm->rm_lock_sx);
588 mtx_unlock(&rm->rm_lock_mtx);
594 _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
597 if (SCHEDULER_STOPPED())
600 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
601 ("rm_wlock() by idle thread %p on rmlock %s @ %s:%d",
602 curthread, rm->lock_object.lo_name, file, line));
603 KASSERT(!rm_destroyed(rm),
604 ("rm_wlock() of destroyed rmlock @ %s:%d", file, line));
605 _rm_assert(rm, RA_UNLOCKED, file, line);
607 WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE,
612 LOCK_LOG_LOCK("RMWLOCK", &rm->lock_object, 0, 0, file, line);
613 WITNESS_LOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
614 TD_LOCKS_INC(curthread);
618 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
621 if (SCHEDULER_STOPPED())
624 KASSERT(!rm_destroyed(rm),
625 ("rm_wunlock() of destroyed rmlock @ %s:%d", file, line));
626 _rm_assert(rm, RA_WLOCKED, file, line);
627 WITNESS_UNLOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
628 LOCK_LOG_LOCK("RMWUNLOCK", &rm->lock_object, 0, 0, file, line);
630 TD_LOCKS_DEC(curthread);
634 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
635 int trylock, const char *file, int line)
638 if (SCHEDULER_STOPPED())
642 if (!(rm->lock_object.lo_flags & LO_RECURSABLE) && !trylock) {
644 KASSERT(rm_trackers_present(get_pcpu(), rm,
646 ("rm_rlock: recursed on non-recursive rmlock %s @ %s:%d\n",
647 rm->lock_object.lo_name, file, line));
651 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
652 ("rm_rlock() by idle thread %p on rmlock %s @ %s:%d",
653 curthread, rm->lock_object.lo_name, file, line));
654 KASSERT(!rm_destroyed(rm),
655 ("rm_rlock() of destroyed rmlock @ %s:%d", file, line));
657 KASSERT(!rm_wowned(rm),
658 ("rm_rlock: wlock already held for %s @ %s:%d",
659 rm->lock_object.lo_name, file, line));
660 WITNESS_CHECKORDER(&rm->lock_object,
661 LOP_NEWORDER | LOP_NOSLEEP, file, line, NULL);
664 if (_rm_rlock(rm, tracker, trylock)) {
666 LOCK_LOG_TRY("RMRLOCK", &rm->lock_object, 0, 1, file,
669 LOCK_LOG_LOCK("RMRLOCK", &rm->lock_object, 0, 0, file,
671 WITNESS_LOCK(&rm->lock_object, LOP_NOSLEEP, file, line);
672 TD_LOCKS_INC(curthread);
675 LOCK_LOG_TRY("RMRLOCK", &rm->lock_object, 0, 0, file, line);
681 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
682 const char *file, int line)
685 if (SCHEDULER_STOPPED())
688 KASSERT(!rm_destroyed(rm),
689 ("rm_runlock() of destroyed rmlock @ %s:%d", file, line));
690 _rm_assert(rm, RA_RLOCKED, file, line);
691 WITNESS_UNLOCK(&rm->lock_object, 0, file, line);
692 LOCK_LOG_LOCK("RMRUNLOCK", &rm->lock_object, 0, 0, file, line);
693 _rm_runlock(rm, tracker);
694 TD_LOCKS_DEC(curthread);
700 * Just strip out file and line arguments if no lock debugging is enabled in
701 * the kernel - we are called from a kernel module.
704 _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
711 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
718 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
719 int trylock, const char *file, int line)
722 return _rm_rlock(rm, tracker, trylock);
726 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
727 const char *file, int line)
730 _rm_runlock(rm, tracker);
735 #ifdef INVARIANT_SUPPORT
741 * Note that this does not need to use witness_assert() for read lock
742 * assertions since an exact count of read locks held by this thread
746 _rm_assert(const struct rmlock *rm, int what, const char *file, int line)
750 if (SCHEDULER_STOPPED())
754 case RA_LOCKED | RA_RECURSED:
755 case RA_LOCKED | RA_NOTRECURSED:
757 case RA_RLOCKED | RA_RECURSED:
758 case RA_RLOCKED | RA_NOTRECURSED:
760 * Handle the write-locked case. Unlike other
761 * primitives, writers can never recurse.
764 if (what & RA_RLOCKED)
765 panic("Lock %s exclusively locked @ %s:%d\n",
766 rm->lock_object.lo_name, file, line);
767 if (what & RA_RECURSED)
768 panic("Lock %s not recursed @ %s:%d\n",
769 rm->lock_object.lo_name, file, line);
774 count = rm_trackers_present(get_pcpu(), rm, curthread);
778 panic("Lock %s not %slocked @ %s:%d\n",
779 rm->lock_object.lo_name, (what & RA_RLOCKED) ?
780 "read " : "", file, line);
782 if (what & RA_NOTRECURSED)
783 panic("Lock %s recursed @ %s:%d\n",
784 rm->lock_object.lo_name, file, line);
785 } else if (what & RA_RECURSED)
786 panic("Lock %s not recursed @ %s:%d\n",
787 rm->lock_object.lo_name, file, line);
791 panic("Lock %s not exclusively locked @ %s:%d\n",
792 rm->lock_object.lo_name, file, line);
796 panic("Lock %s exclusively locked @ %s:%d\n",
797 rm->lock_object.lo_name, file, line);
800 count = rm_trackers_present(get_pcpu(), rm, curthread);
804 panic("Lock %s read locked @ %s:%d\n",
805 rm->lock_object.lo_name, file, line);
808 panic("Unknown rm lock assertion: %d @ %s:%d", what, file,
812 #endif /* INVARIANT_SUPPORT */
816 print_tracker(struct rm_priotracker *tr)
821 db_printf(" thread %p (tid %d, pid %d, \"%s\") {", td, td->td_tid,
822 td->td_proc->p_pid, td->td_name);
823 if (tr->rmp_flags & RMPF_ONQUEUE) {
824 db_printf("ONQUEUE");
825 if (tr->rmp_flags & RMPF_SIGNAL)
826 db_printf(",SIGNAL");
833 db_show_rm(const struct lock_object *lock)
835 struct rm_priotracker *tr;
836 struct rm_queue *queue;
837 const struct rmlock *rm;
838 struct lock_class *lc;
841 rm = (const struct rmlock *)lock;
842 db_printf(" writecpus: ");
843 ddb_display_cpuset(__DEQUALIFY(const cpuset_t *, &rm->rm_writecpus));
845 db_printf(" per-CPU readers:\n");
846 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)
847 for (queue = pc->pc_rm_queue.rmq_next;
848 queue != &pc->pc_rm_queue; queue = queue->rmq_next) {
849 tr = (struct rm_priotracker *)queue;
850 if (tr->rmp_rmlock == rm)
853 db_printf(" active readers:\n");
854 LIST_FOREACH(tr, &rm->rm_activeReaders, rmp_qentry)
856 lc = LOCK_CLASS(&rm->rm_wlock_object);
857 db_printf("Backing write-lock (%s):\n", lc->lc_name);
858 lc->lc_ddb_show(&rm->rm_wlock_object);
863 * Read-mostly sleepable locks.
865 * These primitives allow both readers and writers to sleep. However, neither
866 * readers nor writers are tracked and subsequently there is no priority
869 * They are intended to be only used when write-locking is almost never needed
870 * (e.g., they can guard against unloading a kernel module) while read-locking
871 * happens all the time.
873 * Concurrent writers take turns taking the lock while going off cpu. If this is
874 * of concern for your usecase, this is not the right primitive.
876 * Neither rms_rlock nor rms_runlock use thread fences. Instead interrupt
877 * fences are inserted to ensure ordering with the code executed in the IPI
880 * No attempt is made to track which CPUs read locked at least once,
881 * consequently write locking sends IPIs to all of them. This will become a
882 * problem at some point. The easiest way to lessen it is to provide a bitmap.
885 #define RMS_NOOWNER ((void *)0x1)
886 #define RMS_TRANSIENT ((void *)0x2)
887 #define RMS_FLAGMASK 0xf
889 struct rmslock_pcpu {
894 _Static_assert(sizeof(struct rmslock_pcpu) == 8, "bad size");
899 static struct rmslock_pcpu *
900 rms_int_pcpu(struct rmslock *rms)
903 CRITICAL_ASSERT(curthread);
904 return (zpcpu_get(rms->pcpu));
907 static struct rmslock_pcpu *
908 rms_int_remote_pcpu(struct rmslock *rms, int cpu)
911 return (zpcpu_get_cpu(rms->pcpu, cpu));
915 rms_int_influx_enter(struct rmslock *rms, struct rmslock_pcpu *pcpu)
918 CRITICAL_ASSERT(curthread);
919 MPASS(pcpu->influx == 0);
924 rms_int_influx_exit(struct rmslock *rms, struct rmslock_pcpu *pcpu)
927 CRITICAL_ASSERT(curthread);
928 MPASS(pcpu->influx == 1);
934 rms_int_debug_readers_inc(struct rmslock *rms)
937 old = atomic_fetchadd_int(&rms->debug_readers, 1);
938 KASSERT(old >= 0, ("%s: bad readers count %d\n", __func__, old));
942 rms_int_debug_readers_dec(struct rmslock *rms)
946 old = atomic_fetchadd_int(&rms->debug_readers, -1);
947 KASSERT(old > 0, ("%s: bad readers count %d\n", __func__, old));
951 rms_int_debug_readers_inc(struct rmslock *rms)
956 rms_int_debug_readers_dec(struct rmslock *rms)
962 rms_int_readers_inc(struct rmslock *rms, struct rmslock_pcpu *pcpu)
965 CRITICAL_ASSERT(curthread);
966 rms_int_debug_readers_inc(rms);
971 rms_int_readers_dec(struct rmslock *rms, struct rmslock_pcpu *pcpu)
974 CRITICAL_ASSERT(curthread);
975 rms_int_debug_readers_dec(rms);
983 rms_init(struct rmslock *rms, const char *name)
986 rms->owner = RMS_NOOWNER;
989 rms->debug_readers = 0;
990 mtx_init(&rms->mtx, name, NULL, MTX_DEF | MTX_NEW);
991 rms->pcpu = uma_zalloc_pcpu(pcpu_zone_8, M_WAITOK | M_ZERO);
995 rms_destroy(struct rmslock *rms)
998 MPASS(rms->writers == 0);
999 MPASS(rms->readers == 0);
1000 mtx_destroy(&rms->mtx);
1001 uma_zfree_pcpu(pcpu_zone_8, rms->pcpu);
1004 static void __noinline
1005 rms_rlock_fallback(struct rmslock *rms)
1008 rms_int_influx_exit(rms, rms_int_pcpu(rms));
1011 mtx_lock(&rms->mtx);
1012 while (rms->writers > 0)
1013 msleep(&rms->readers, &rms->mtx, PUSER - 1, mtx_name(&rms->mtx), 0);
1015 rms_int_readers_inc(rms, rms_int_pcpu(rms));
1016 mtx_unlock(&rms->mtx);
1018 TD_LOCKS_INC(curthread);
1022 rms_rlock(struct rmslock *rms)
1024 struct rmslock_pcpu *pcpu;
1026 rms_assert_rlock_ok(rms);
1027 MPASS(atomic_load_ptr(&rms->owner) != curthread);
1030 pcpu = rms_int_pcpu(rms);
1031 rms_int_influx_enter(rms, pcpu);
1032 atomic_interrupt_fence();
1033 if (__predict_false(rms->writers > 0)) {
1034 rms_rlock_fallback(rms);
1037 atomic_interrupt_fence();
1038 rms_int_readers_inc(rms, pcpu);
1039 atomic_interrupt_fence();
1040 rms_int_influx_exit(rms, pcpu);
1042 TD_LOCKS_INC(curthread);
1046 rms_try_rlock(struct rmslock *rms)
1048 struct rmslock_pcpu *pcpu;
1050 MPASS(atomic_load_ptr(&rms->owner) != curthread);
1053 pcpu = rms_int_pcpu(rms);
1054 rms_int_influx_enter(rms, pcpu);
1055 atomic_interrupt_fence();
1056 if (__predict_false(rms->writers > 0)) {
1057 rms_int_influx_exit(rms, pcpu);
1061 atomic_interrupt_fence();
1062 rms_int_readers_inc(rms, pcpu);
1063 atomic_interrupt_fence();
1064 rms_int_influx_exit(rms, pcpu);
1066 TD_LOCKS_INC(curthread);
1070 static void __noinline
1071 rms_runlock_fallback(struct rmslock *rms)
1074 rms_int_influx_exit(rms, rms_int_pcpu(rms));
1077 mtx_lock(&rms->mtx);
1078 MPASS(rms->writers > 0);
1079 MPASS(rms->readers > 0);
1080 MPASS(rms->debug_readers == rms->readers);
1081 rms_int_debug_readers_dec(rms);
1083 if (rms->readers == 0)
1084 wakeup_one(&rms->writers);
1085 mtx_unlock(&rms->mtx);
1086 TD_LOCKS_DEC(curthread);
1090 rms_runlock(struct rmslock *rms)
1092 struct rmslock_pcpu *pcpu;
1095 pcpu = rms_int_pcpu(rms);
1096 rms_int_influx_enter(rms, pcpu);
1097 atomic_interrupt_fence();
1098 if (__predict_false(rms->writers > 0)) {
1099 rms_runlock_fallback(rms);
1102 atomic_interrupt_fence();
1103 rms_int_readers_dec(rms, pcpu);
1104 atomic_interrupt_fence();
1105 rms_int_influx_exit(rms, pcpu);
1107 TD_LOCKS_DEC(curthread);
1110 struct rmslock_ipi {
1111 struct rmslock *rms;
1112 struct smp_rendezvous_cpus_retry_arg srcra;
1116 rms_action_func(void *arg)
1118 struct rmslock_ipi *rmsipi;
1119 struct rmslock_pcpu *pcpu;
1120 struct rmslock *rms;
1122 rmsipi = __containerof(arg, struct rmslock_ipi, srcra);
1124 pcpu = rms_int_pcpu(rms);
1128 if (pcpu->readers != 0) {
1129 atomic_add_int(&rms->readers, pcpu->readers);
1132 smp_rendezvous_cpus_done(arg);
1136 rms_wait_func(void *arg, int cpu)
1138 struct rmslock_ipi *rmsipi;
1139 struct rmslock_pcpu *pcpu;
1140 struct rmslock *rms;
1142 rmsipi = __containerof(arg, struct rmslock_ipi, srcra);
1144 pcpu = rms_int_remote_pcpu(rms, cpu);
1146 while (atomic_load_int(&pcpu->influx))
1152 rms_assert_no_pcpu_readers(struct rmslock *rms)
1154 struct rmslock_pcpu *pcpu;
1158 pcpu = rms_int_remote_pcpu(rms, cpu);
1159 if (pcpu->readers != 0) {
1160 panic("%s: got %d readers on cpu %d\n", __func__,
1161 pcpu->readers, cpu);
1167 rms_assert_no_pcpu_readers(struct rmslock *rms)
1173 rms_wlock_switch(struct rmslock *rms)
1175 struct rmslock_ipi rmsipi;
1177 MPASS(rms->readers == 0);
1178 MPASS(rms->writers == 1);
1182 smp_rendezvous_cpus_retry(all_cpus,
1183 smp_no_rendezvous_barrier,
1185 smp_no_rendezvous_barrier,
1191 rms_wlock(struct rmslock *rms)
1194 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
1195 MPASS(atomic_load_ptr(&rms->owner) != curthread);
1197 mtx_lock(&rms->mtx);
1199 if (rms->writers > 1) {
1200 msleep(&rms->owner, &rms->mtx, (PUSER - 1),
1201 mtx_name(&rms->mtx), 0);
1202 MPASS(rms->readers == 0);
1203 KASSERT(rms->owner == RMS_TRANSIENT,
1204 ("%s: unexpected owner value %p\n", __func__,
1209 KASSERT(rms->owner == RMS_NOOWNER,
1210 ("%s: unexpected owner value %p\n", __func__, rms->owner));
1212 rms_wlock_switch(rms);
1213 rms_assert_no_pcpu_readers(rms);
1215 if (rms->readers > 0) {
1216 msleep(&rms->writers, &rms->mtx, (PUSER - 1),
1217 mtx_name(&rms->mtx), 0);
1221 rms->owner = curthread;
1222 rms_assert_no_pcpu_readers(rms);
1223 mtx_unlock(&rms->mtx);
1224 MPASS(rms->readers == 0);
1225 TD_LOCKS_INC(curthread);
1229 rms_wunlock(struct rmslock *rms)
1232 mtx_lock(&rms->mtx);
1233 KASSERT(rms->owner == curthread,
1234 ("%s: unexpected owner value %p\n", __func__, rms->owner));
1235 MPASS(rms->writers >= 1);
1236 MPASS(rms->readers == 0);
1238 if (rms->writers > 0) {
1239 wakeup_one(&rms->owner);
1240 rms->owner = RMS_TRANSIENT;
1242 wakeup(&rms->readers);
1243 rms->owner = RMS_NOOWNER;
1245 mtx_unlock(&rms->mtx);
1246 TD_LOCKS_DEC(curthread);
1250 rms_unlock(struct rmslock *rms)
1253 if (rms_wowned(rms))