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>
37 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/kernel.h>
48 #include <sys/mutex.h>
50 #include <sys/rmlock.h>
51 #include <sys/sched.h>
53 #include <sys/turnstile.h>
54 #include <sys/lock_profile.h>
55 #include <machine/cpu.h>
63 * A cookie to mark destroyed rmlocks. This is stored in the head of
66 #define RM_DESTROYED ((void *)0xdead)
68 #define rm_destroyed(rm) \
69 (LIST_FIRST(&(rm)->rm_activeReaders) == RM_DESTROYED)
71 #define RMPF_ONQUEUE 1
75 #define _rm_assert(c, what, file, line)
78 static void assert_rm(const struct lock_object *lock, int what);
80 static void db_show_rm(const struct lock_object *lock);
82 static void lock_rm(struct lock_object *lock, uintptr_t how);
84 static int owner_rm(const struct lock_object *lock, struct thread **owner);
86 static uintptr_t unlock_rm(struct lock_object *lock);
88 struct lock_class lock_class_rm = {
90 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
91 .lc_assert = assert_rm,
93 .lc_ddb_show = db_show_rm,
96 .lc_unlock = unlock_rm,
102 struct lock_class lock_class_rm_sleepable = {
103 .lc_name = "sleepable rm",
104 .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE,
105 .lc_assert = assert_rm,
107 .lc_ddb_show = db_show_rm,
110 .lc_unlock = unlock_rm,
112 .lc_owner = owner_rm,
117 assert_rm(const struct lock_object *lock, int what)
120 rm_assert((const struct rmlock *)lock, what);
124 lock_rm(struct lock_object *lock, uintptr_t how)
127 struct rm_priotracker *tracker;
129 rm = (struct rmlock *)lock;
133 tracker = (struct rm_priotracker *)how;
134 rm_rlock(rm, tracker);
139 unlock_rm(struct lock_object *lock)
144 struct rm_queue *queue;
145 struct rm_priotracker *tracker;
148 rm = (struct rmlock *)lock;
151 rm_assert(rm, RA_LOCKED | RA_NOTRECURSED);
156 * Find the right rm_priotracker structure for curthread.
157 * The guarantee about its uniqueness is given by the fact
158 * we already asserted the lock wasn't recursively acquired.
163 for (queue = pc->pc_rm_queue.rmq_next;
164 queue != &pc->pc_rm_queue; queue = queue->rmq_next) {
165 tracker = (struct rm_priotracker *)queue;
166 if ((tracker->rmp_rmlock == rm) &&
167 (tracker->rmp_thread == td)) {
168 how = (uintptr_t)tracker;
172 KASSERT(tracker != NULL,
173 ("rm_priotracker is non-NULL when lock held in read mode"));
175 rm_runlock(rm, tracker);
182 owner_rm(const struct lock_object *lock, struct thread **owner)
184 const struct rmlock *rm;
185 struct lock_class *lc;
187 rm = (const struct rmlock *)lock;
188 lc = LOCK_CLASS(&rm->rm_wlock_object);
189 return (lc->lc_owner(&rm->rm_wlock_object, owner));
193 static struct mtx rm_spinlock;
195 MTX_SYSINIT(rm_spinlock, &rm_spinlock, "rm_spinlock", MTX_SPIN);
198 * Add or remove tracker from per-cpu list.
200 * The per-cpu list can be traversed at any time in forward direction from an
201 * interrupt on the *local* cpu.
204 rm_tracker_add(struct pcpu *pc, struct rm_priotracker *tracker)
206 struct rm_queue *next;
208 /* Initialize all tracker pointers */
209 tracker->rmp_cpuQueue.rmq_prev = &pc->pc_rm_queue;
210 next = pc->pc_rm_queue.rmq_next;
211 tracker->rmp_cpuQueue.rmq_next = next;
213 /* rmq_prev is not used during froward traversal. */
214 next->rmq_prev = &tracker->rmp_cpuQueue;
216 /* Update pointer to first element. */
217 pc->pc_rm_queue.rmq_next = &tracker->rmp_cpuQueue;
221 * Return a count of the number of trackers the thread 'td' already
222 * has on this CPU for the lock 'rm'.
225 rm_trackers_present(const struct pcpu *pc, const struct rmlock *rm,
226 const struct thread *td)
228 struct rm_queue *queue;
229 struct rm_priotracker *tracker;
233 for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
234 queue = queue->rmq_next) {
235 tracker = (struct rm_priotracker *)queue;
236 if ((tracker->rmp_rmlock == rm) && (tracker->rmp_thread == td))
243 rm_tracker_remove(struct pcpu *pc, struct rm_priotracker *tracker)
245 struct rm_queue *next, *prev;
247 next = tracker->rmp_cpuQueue.rmq_next;
248 prev = tracker->rmp_cpuQueue.rmq_prev;
250 /* Not used during forward traversal. */
251 next->rmq_prev = prev;
253 /* Remove from list. */
254 prev->rmq_next = next;
258 rm_cleanIPI(void *arg)
261 struct rmlock *rm = arg;
262 struct rm_priotracker *tracker;
263 struct rm_queue *queue;
266 for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
267 queue = queue->rmq_next) {
268 tracker = (struct rm_priotracker *)queue;
269 if (tracker->rmp_rmlock == rm && tracker->rmp_flags == 0) {
270 tracker->rmp_flags = RMPF_ONQUEUE;
271 mtx_lock_spin(&rm_spinlock);
272 LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
274 mtx_unlock_spin(&rm_spinlock);
280 rm_init_flags(struct rmlock *rm, const char *name, int opts)
282 struct lock_class *lc;
286 if (!(opts & RM_NOWITNESS))
287 liflags |= LO_WITNESS;
288 if (opts & RM_RECURSE)
289 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);
365 /* Check to see if the IPI granted us the lock after all. */
366 if (tracker->rmp_flags) {
367 /* Just add back tracker - we hold the lock. */
368 rm_tracker_add(pc, tracker);
374 * We allow readers to acquire a lock even if a writer is blocked if
375 * the lock is recursive and the reader already holds the lock.
377 if ((rm->lock_object.lo_flags & LO_RECURSABLE) != 0) {
379 * Just grant the lock if this thread already has a tracker
380 * for this lock on the per-cpu queue.
382 if (rm_trackers_present(pc, rm, curthread) != 0) {
383 mtx_lock_spin(&rm_spinlock);
384 LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
386 tracker->rmp_flags = RMPF_ONQUEUE;
387 mtx_unlock_spin(&rm_spinlock);
388 rm_tracker_add(pc, tracker);
398 if (rm->lock_object.lo_flags & LO_SLEEPABLE) {
399 if (!sx_try_xlock(&rm->rm_lock_sx))
402 if (!mtx_trylock(&rm->rm_lock_mtx))
406 if (rm->lock_object.lo_flags & LO_SLEEPABLE) {
407 THREAD_SLEEPING_OK();
408 sx_xlock(&rm->rm_lock_sx);
409 THREAD_NO_SLEEPING();
411 mtx_lock(&rm->rm_lock_mtx);
416 CPU_CLR(pc->pc_cpuid, &rm->rm_writecpus);
417 rm_tracker_add(pc, tracker);
421 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
422 sx_xunlock(&rm->rm_lock_sx);
424 mtx_unlock(&rm->rm_lock_mtx);
430 _rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker, int trylock)
432 struct thread *td = curthread;
435 if (SCHEDULER_STOPPED())
438 tracker->rmp_flags = 0;
439 tracker->rmp_thread = td;
440 tracker->rmp_rmlock = rm;
442 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
443 THREAD_NO_SLEEPING();
445 td->td_critnest++; /* critical_enter(); */
449 pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
451 rm_tracker_add(pc, tracker);
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 pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
515 rm_tracker_remove(pc, tracker);
519 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
520 THREAD_SLEEPING_OK();
522 if (__predict_true(0 == (td->td_owepreempt | tracker->rmp_flags)))
525 _rm_unlock_hard(td, tracker);
529 _rm_wlock(struct rmlock *rm)
531 struct rm_priotracker *prio;
532 struct turnstile *ts;
535 if (SCHEDULER_STOPPED())
538 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
539 sx_xlock(&rm->rm_lock_sx);
541 mtx_lock(&rm->rm_lock_mtx);
543 if (CPU_CMP(&rm->rm_writecpus, &all_cpus)) {
544 /* Get all read tokens back */
546 CPU_ANDNOT(&readcpus, &rm->rm_writecpus);
547 rm->rm_writecpus = all_cpus;
550 * Assumes rm->rm_writecpus update is visible on other CPUs
551 * before rm_cleanIPI is called.
554 smp_rendezvous_cpus(readcpus,
555 smp_no_rendezvous_barrier,
557 smp_no_rendezvous_barrier,
564 mtx_lock_spin(&rm_spinlock);
565 while ((prio = LIST_FIRST(&rm->rm_activeReaders)) != NULL) {
566 ts = turnstile_trywait(&rm->lock_object);
567 prio->rmp_flags = RMPF_ONQUEUE | RMPF_SIGNAL;
568 mtx_unlock_spin(&rm_spinlock);
569 turnstile_wait(ts, prio->rmp_thread,
571 mtx_lock_spin(&rm_spinlock);
573 mtx_unlock_spin(&rm_spinlock);
578 _rm_wunlock(struct rmlock *rm)
581 if (rm->lock_object.lo_flags & LO_SLEEPABLE)
582 sx_xunlock(&rm->rm_lock_sx);
584 mtx_unlock(&rm->rm_lock_mtx);
590 _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
593 if (SCHEDULER_STOPPED())
596 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
597 ("rm_wlock() by idle thread %p on rmlock %s @ %s:%d",
598 curthread, rm->lock_object.lo_name, file, line));
599 KASSERT(!rm_destroyed(rm),
600 ("rm_wlock() of destroyed rmlock @ %s:%d", file, line));
601 _rm_assert(rm, RA_UNLOCKED, file, line);
603 WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE,
608 LOCK_LOG_LOCK("RMWLOCK", &rm->lock_object, 0, 0, file, line);
609 WITNESS_LOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
610 TD_LOCKS_INC(curthread);
614 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
617 if (SCHEDULER_STOPPED())
620 KASSERT(!rm_destroyed(rm),
621 ("rm_wunlock() of destroyed rmlock @ %s:%d", file, line));
622 _rm_assert(rm, RA_WLOCKED, file, line);
623 WITNESS_UNLOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
624 LOCK_LOG_LOCK("RMWUNLOCK", &rm->lock_object, 0, 0, file, line);
626 TD_LOCKS_DEC(curthread);
630 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
631 int trylock, const char *file, int line)
634 if (SCHEDULER_STOPPED())
638 if (!(rm->lock_object.lo_flags & LO_RECURSABLE) && !trylock) {
640 KASSERT(rm_trackers_present(get_pcpu(), rm,
642 ("rm_rlock: recursed on non-recursive rmlock %s @ %s:%d\n",
643 rm->lock_object.lo_name, file, line));
647 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
648 ("rm_rlock() by idle thread %p on rmlock %s @ %s:%d",
649 curthread, rm->lock_object.lo_name, file, line));
650 KASSERT(!rm_destroyed(rm),
651 ("rm_rlock() of destroyed rmlock @ %s:%d", file, line));
653 KASSERT(!rm_wowned(rm),
654 ("rm_rlock: wlock already held for %s @ %s:%d",
655 rm->lock_object.lo_name, file, line));
656 WITNESS_CHECKORDER(&rm->lock_object,
657 LOP_NEWORDER | LOP_NOSLEEP, file, line, NULL);
660 if (_rm_rlock(rm, tracker, trylock)) {
662 LOCK_LOG_TRY("RMRLOCK", &rm->lock_object, 0, 1, file,
665 LOCK_LOG_LOCK("RMRLOCK", &rm->lock_object, 0, 0, file,
667 WITNESS_LOCK(&rm->lock_object, LOP_NOSLEEP, file, line);
668 TD_LOCKS_INC(curthread);
671 LOCK_LOG_TRY("RMRLOCK", &rm->lock_object, 0, 0, file, line);
677 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
678 const char *file, int line)
681 if (SCHEDULER_STOPPED())
684 KASSERT(!rm_destroyed(rm),
685 ("rm_runlock() of destroyed rmlock @ %s:%d", file, line));
686 _rm_assert(rm, RA_RLOCKED, file, line);
687 WITNESS_UNLOCK(&rm->lock_object, 0, file, line);
688 LOCK_LOG_LOCK("RMRUNLOCK", &rm->lock_object, 0, 0, file, line);
689 _rm_runlock(rm, tracker);
690 TD_LOCKS_DEC(curthread);
696 * Just strip out file and line arguments if no lock debugging is enabled in
697 * the kernel - we are called from a kernel module.
700 _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
707 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
714 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
715 int trylock, const char *file, int line)
718 return _rm_rlock(rm, tracker, trylock);
722 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
723 const char *file, int line)
726 _rm_runlock(rm, tracker);
731 #ifdef INVARIANT_SUPPORT
737 * Note that this does not need to use witness_assert() for read lock
738 * assertions since an exact count of read locks held by this thread
742 _rm_assert(const struct rmlock *rm, int what, const char *file, int line)
746 if (SCHEDULER_STOPPED())
750 case RA_LOCKED | RA_RECURSED:
751 case RA_LOCKED | RA_NOTRECURSED:
753 case RA_RLOCKED | RA_RECURSED:
754 case RA_RLOCKED | RA_NOTRECURSED:
756 * Handle the write-locked case. Unlike other
757 * primitives, writers can never recurse.
760 if (what & RA_RLOCKED)
761 panic("Lock %s exclusively locked @ %s:%d\n",
762 rm->lock_object.lo_name, file, line);
763 if (what & RA_RECURSED)
764 panic("Lock %s not recursed @ %s:%d\n",
765 rm->lock_object.lo_name, file, line);
770 count = rm_trackers_present(get_pcpu(), rm, curthread);
774 panic("Lock %s not %slocked @ %s:%d\n",
775 rm->lock_object.lo_name, (what & RA_RLOCKED) ?
776 "read " : "", file, line);
778 if (what & RA_NOTRECURSED)
779 panic("Lock %s recursed @ %s:%d\n",
780 rm->lock_object.lo_name, file, line);
781 } else if (what & RA_RECURSED)
782 panic("Lock %s not recursed @ %s:%d\n",
783 rm->lock_object.lo_name, file, line);
787 panic("Lock %s not exclusively locked @ %s:%d\n",
788 rm->lock_object.lo_name, file, line);
792 panic("Lock %s exclusively locked @ %s:%d\n",
793 rm->lock_object.lo_name, file, line);
796 count = rm_trackers_present(get_pcpu(), rm, curthread);
800 panic("Lock %s read locked @ %s:%d\n",
801 rm->lock_object.lo_name, file, line);
804 panic("Unknown rm lock assertion: %d @ %s:%d", what, file,
808 #endif /* INVARIANT_SUPPORT */
812 print_tracker(struct rm_priotracker *tr)
817 db_printf(" thread %p (tid %d, pid %d, \"%s\") {", td, td->td_tid,
818 td->td_proc->p_pid, td->td_name);
819 if (tr->rmp_flags & RMPF_ONQUEUE) {
820 db_printf("ONQUEUE");
821 if (tr->rmp_flags & RMPF_SIGNAL)
822 db_printf(",SIGNAL");
829 db_show_rm(const struct lock_object *lock)
831 struct rm_priotracker *tr;
832 struct rm_queue *queue;
833 const struct rmlock *rm;
834 struct lock_class *lc;
837 rm = (const struct rmlock *)lock;
838 db_printf(" writecpus: ");
839 ddb_display_cpuset(__DEQUALIFY(const cpuset_t *, &rm->rm_writecpus));
841 db_printf(" per-CPU readers:\n");
842 STAILQ_FOREACH(pc, &cpuhead, pc_allcpu)
843 for (queue = pc->pc_rm_queue.rmq_next;
844 queue != &pc->pc_rm_queue; queue = queue->rmq_next) {
845 tr = (struct rm_priotracker *)queue;
846 if (tr->rmp_rmlock == rm)
849 db_printf(" active readers:\n");
850 LIST_FOREACH(tr, &rm->rm_activeReaders, rmp_qentry)
852 lc = LOCK_CLASS(&rm->rm_wlock_object);
853 db_printf("Backing write-lock (%s):\n", lc->lc_name);
854 lc->lc_ddb_show(&rm->rm_wlock_object);
859 * Read-mostly sleepable locks.
861 * These primitives allow both readers and writers to sleep. However, neither
862 * readers nor writers are tracked and subsequently there is no priority
865 * They are intended to be only used when write-locking is almost never needed
866 * (e.g., they can guard against unloading a kernel module) while read-locking
867 * happens all the time.
869 * Concurrent writers take turns taking the lock while going off cpu. If this is
870 * of concern for your usecase, this is not the right primitive.
872 * Neither rms_rlock nor rms_runlock use fences. Instead compiler barriers are
873 * inserted to prevert reordering of generated code. Execution ordering is
874 * provided with the use of an IPI handler.
878 rms_init(struct rmslock *rms, const char *name)
883 mtx_init(&rms->mtx, name, NULL, MTX_DEF | MTX_NEW);
884 rms->readers_pcpu = uma_zalloc_pcpu(pcpu_zone_int, M_WAITOK | M_ZERO);
885 rms->readers_influx = uma_zalloc_pcpu(pcpu_zone_int, M_WAITOK | M_ZERO);
889 rms_destroy(struct rmslock *rms)
892 MPASS(rms->writers == 0);
893 MPASS(rms->readers == 0);
894 mtx_destroy(&rms->mtx);
895 uma_zfree_pcpu(pcpu_zone_int, rms->readers_pcpu);
896 uma_zfree_pcpu(pcpu_zone_int, rms->readers_influx);
899 static void __noinline
900 rms_rlock_fallback(struct rmslock *rms)
903 zpcpu_set_protected(rms->readers_influx, 0);
907 MPASS(*zpcpu_get(rms->readers_pcpu) == 0);
908 while (rms->writers > 0)
909 msleep(&rms->readers, &rms->mtx, PUSER - 1, mtx_name(&rms->mtx), 0);
911 zpcpu_add_protected(rms->readers_pcpu, 1);
912 mtx_unlock(&rms->mtx);
917 rms_rlock(struct rmslock *rms)
920 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
923 zpcpu_set_protected(rms->readers_influx, 1);
925 if (__predict_false(rms->writers > 0)) {
926 rms_rlock_fallback(rms);
930 zpcpu_add_protected(rms->readers_pcpu, 1);
932 zpcpu_set_protected(rms->readers_influx, 0);
937 rms_try_rlock(struct rmslock *rms)
941 zpcpu_set_protected(rms->readers_influx, 1);
943 if (__predict_false(rms->writers > 0)) {
945 zpcpu_set_protected(rms->readers_influx, 0);
950 zpcpu_add_protected(rms->readers_pcpu, 1);
952 zpcpu_set_protected(rms->readers_influx, 0);
957 static void __noinline
958 rms_runlock_fallback(struct rmslock *rms)
961 zpcpu_set_protected(rms->readers_influx, 0);
965 MPASS(*zpcpu_get(rms->readers_pcpu) == 0);
966 MPASS(rms->writers > 0);
967 MPASS(rms->readers > 0);
969 if (rms->readers == 0)
970 wakeup_one(&rms->writers);
971 mtx_unlock(&rms->mtx);
975 rms_runlock(struct rmslock *rms)
979 zpcpu_set_protected(rms->readers_influx, 1);
981 if (__predict_false(rms->writers > 0)) {
982 rms_runlock_fallback(rms);
986 zpcpu_sub_protected(rms->readers_pcpu, 1);
988 zpcpu_set_protected(rms->readers_influx, 0);
994 struct smp_rendezvous_cpus_retry_arg srcra;
998 rms_action_func(void *arg)
1000 struct rmslock_ipi *rmsipi;
1001 struct rmslock *rms;
1004 rmsipi = __containerof(arg, struct rmslock_ipi, srcra);
1007 if (*zpcpu_get(rms->readers_influx))
1009 readers = zpcpu_replace(rms->readers_pcpu, 0);
1011 atomic_add_int(&rms->readers, readers);
1012 smp_rendezvous_cpus_done(arg);
1016 rms_wait_func(void *arg, int cpu)
1018 struct rmslock_ipi *rmsipi;
1019 struct rmslock *rms;
1022 rmsipi = __containerof(arg, struct rmslock_ipi, srcra);
1025 in_op = zpcpu_get_cpu(rms->readers_influx, cpu);
1026 while (atomic_load_int(in_op))
1031 rms_wlock_switch(struct rmslock *rms)
1033 struct rmslock_ipi rmsipi;
1035 MPASS(rms->readers == 0);
1036 MPASS(rms->writers == 1);
1040 smp_rendezvous_cpus_retry(all_cpus,
1041 smp_no_rendezvous_barrier,
1043 smp_no_rendezvous_barrier,
1049 rms_wlock(struct rmslock *rms)
1052 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
1054 mtx_lock(&rms->mtx);
1056 if (rms->writers > 1) {
1057 msleep(&rms->writers, &rms->mtx, (PUSER - 1) | PDROP,
1058 mtx_name(&rms->mtx), 0);
1059 MPASS(rms->readers == 0);
1063 rms_wlock_switch(rms);
1065 if (rms->readers > 0)
1066 msleep(&rms->writers, &rms->mtx, (PUSER - 1) | PDROP,
1067 mtx_name(&rms->mtx), 0);
1069 mtx_unlock(&rms->mtx);
1070 MPASS(rms->readers == 0);
1074 rms_wunlock(struct rmslock *rms)
1077 mtx_lock(&rms->mtx);
1078 MPASS(rms->writers >= 1);
1079 MPASS(rms->readers == 0);
1081 if (rms->writers > 0)
1082 wakeup_one(&rms->writers);
1084 wakeup(&rms->readers);
1085 mtx_unlock(&rms->mtx);