1f53d15feSStephan Uphoff /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4f53d15feSStephan Uphoff * Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org> 5f53d15feSStephan Uphoff * All rights reserved. 6f53d15feSStephan Uphoff * 7f53d15feSStephan Uphoff * Redistribution and use in source and binary forms, with or without 8f53d15feSStephan Uphoff * modification, are permitted provided that the following conditions 9f53d15feSStephan Uphoff * are met: 10f53d15feSStephan Uphoff * 1. Redistributions of source code must retain the above copyright 11f53d15feSStephan Uphoff * notice, this list of conditions and the following disclaimer. 12f53d15feSStephan Uphoff * 2. Redistributions in binary form must reproduce the above copyright 13f53d15feSStephan Uphoff * notice, this list of conditions and the following disclaimer in the 14f53d15feSStephan Uphoff * documentation and/or other materials provided with the distribution. 15f53d15feSStephan Uphoff * 3. Neither the name of the author nor the names of any co-contributors 16f53d15feSStephan Uphoff * may be used to endorse or promote products derived from this software 17f53d15feSStephan Uphoff * without specific prior written permission. 18f53d15feSStephan Uphoff * 19f53d15feSStephan Uphoff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20f53d15feSStephan Uphoff * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21f53d15feSStephan Uphoff * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22f53d15feSStephan Uphoff * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23f53d15feSStephan Uphoff * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24f53d15feSStephan Uphoff * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25f53d15feSStephan Uphoff * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26f53d15feSStephan Uphoff * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27f53d15feSStephan Uphoff * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28f53d15feSStephan Uphoff * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29f53d15feSStephan Uphoff * SUCH DAMAGE. 30f53d15feSStephan Uphoff */ 31f53d15feSStephan Uphoff 32f53d15feSStephan Uphoff /* 33f53d15feSStephan Uphoff * Machine independent bits of reader/writer lock implementation. 34f53d15feSStephan Uphoff */ 35f53d15feSStephan Uphoff 36f53d15feSStephan Uphoff #include <sys/cdefs.h> 37f53d15feSStephan Uphoff __FBSDID("$FreeBSD$"); 38f53d15feSStephan Uphoff 39f53d15feSStephan Uphoff #include "opt_ddb.h" 40f53d15feSStephan Uphoff 41f53d15feSStephan Uphoff #include <sys/param.h> 42f53d15feSStephan Uphoff #include <sys/systm.h> 43f53d15feSStephan Uphoff 44f53d15feSStephan Uphoff #include <sys/kernel.h> 45cd2fe4e6SAttilio Rao #include <sys/kdb.h> 46f53d15feSStephan Uphoff #include <sys/ktr.h> 47f53d15feSStephan Uphoff #include <sys/lock.h> 48f53d15feSStephan Uphoff #include <sys/mutex.h> 49f53d15feSStephan Uphoff #include <sys/proc.h> 50f53d15feSStephan Uphoff #include <sys/rmlock.h> 51f53d15feSStephan Uphoff #include <sys/sched.h> 52f53d15feSStephan Uphoff #include <sys/smp.h> 53f53d15feSStephan Uphoff #include <sys/turnstile.h> 54f53d15feSStephan Uphoff #include <sys/lock_profile.h> 55f53d15feSStephan Uphoff #include <machine/cpu.h> 561f162fefSMateusz Guzik #include <vm/uma.h> 57f53d15feSStephan Uphoff 58f53d15feSStephan Uphoff #ifdef DDB 59f53d15feSStephan Uphoff #include <ddb/ddb.h> 60f53d15feSStephan Uphoff #endif 61f53d15feSStephan Uphoff 62cd32bd7aSJohn Baldwin /* 63cd32bd7aSJohn Baldwin * A cookie to mark destroyed rmlocks. This is stored in the head of 64cd32bd7aSJohn Baldwin * rm_activeReaders. 65cd32bd7aSJohn Baldwin */ 66cd32bd7aSJohn Baldwin #define RM_DESTROYED ((void *)0xdead) 67cd32bd7aSJohn Baldwin 68cd32bd7aSJohn Baldwin #define rm_destroyed(rm) \ 69cd32bd7aSJohn Baldwin (LIST_FIRST(&(rm)->rm_activeReaders) == RM_DESTROYED) 70cd32bd7aSJohn Baldwin 71f53d15feSStephan Uphoff #define RMPF_ONQUEUE 1 72f53d15feSStephan Uphoff #define RMPF_SIGNAL 2 73f53d15feSStephan Uphoff 74cd32bd7aSJohn Baldwin #ifndef INVARIANTS 75cd32bd7aSJohn Baldwin #define _rm_assert(c, what, file, line) 76cd32bd7aSJohn Baldwin #endif 77f53d15feSStephan Uphoff 78d576deedSPawel Jakub Dawidek static void assert_rm(const struct lock_object *lock, int what); 79cd32bd7aSJohn Baldwin #ifdef DDB 80cd32bd7aSJohn Baldwin static void db_show_rm(const struct lock_object *lock); 81cd32bd7aSJohn Baldwin #endif 827faf4d90SDavide Italiano static void lock_rm(struct lock_object *lock, uintptr_t how); 83a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 84d576deedSPawel Jakub Dawidek static int owner_rm(const struct lock_object *lock, struct thread **owner); 85a5aedd68SStacey Son #endif 867faf4d90SDavide Italiano static uintptr_t unlock_rm(struct lock_object *lock); 87f53d15feSStephan Uphoff 88f53d15feSStephan Uphoff struct lock_class lock_class_rm = { 89f53d15feSStephan Uphoff .lc_name = "rm", 90f53d15feSStephan Uphoff .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE, 91f9721b43SAttilio Rao .lc_assert = assert_rm, 92f53d15feSStephan Uphoff #ifdef DDB 93cd32bd7aSJohn Baldwin .lc_ddb_show = db_show_rm, 94f53d15feSStephan Uphoff #endif 95cd32bd7aSJohn Baldwin .lc_lock = lock_rm, 96cd32bd7aSJohn Baldwin .lc_unlock = unlock_rm, 97cd32bd7aSJohn Baldwin #ifdef KDTRACE_HOOKS 98cd32bd7aSJohn Baldwin .lc_owner = owner_rm, 99cd32bd7aSJohn Baldwin #endif 100cd32bd7aSJohn Baldwin }; 101cd32bd7aSJohn Baldwin 102cd32bd7aSJohn Baldwin struct lock_class lock_class_rm_sleepable = { 103cd32bd7aSJohn Baldwin .lc_name = "sleepable rm", 104cd32bd7aSJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE, 105cd32bd7aSJohn Baldwin .lc_assert = assert_rm, 106cd32bd7aSJohn Baldwin #ifdef DDB 107cd32bd7aSJohn Baldwin .lc_ddb_show = db_show_rm, 108f53d15feSStephan Uphoff #endif 109f53d15feSStephan Uphoff .lc_lock = lock_rm, 110f53d15feSStephan Uphoff .lc_unlock = unlock_rm, 111a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 112a5aedd68SStacey Son .lc_owner = owner_rm, 113a5aedd68SStacey Son #endif 114f53d15feSStephan Uphoff }; 115f53d15feSStephan Uphoff 116f53d15feSStephan Uphoff static void 117d576deedSPawel Jakub Dawidek assert_rm(const struct lock_object *lock, int what) 118f9721b43SAttilio Rao { 119f9721b43SAttilio Rao 120cd32bd7aSJohn Baldwin rm_assert((const struct rmlock *)lock, what); 121f9721b43SAttilio Rao } 122f9721b43SAttilio Rao 123f9721b43SAttilio Rao static void 1247faf4d90SDavide Italiano lock_rm(struct lock_object *lock, uintptr_t how) 125d02add54SRobert Watson { 126cd32bd7aSJohn Baldwin struct rmlock *rm; 1277faf4d90SDavide Italiano struct rm_priotracker *tracker; 128d02add54SRobert Watson 129cd32bd7aSJohn Baldwin rm = (struct rmlock *)lock; 1307faf4d90SDavide Italiano if (how == 0) 131cd32bd7aSJohn Baldwin rm_wlock(rm); 1327faf4d90SDavide Italiano else { 1337faf4d90SDavide Italiano tracker = (struct rm_priotracker *)how; 1347faf4d90SDavide Italiano rm_rlock(rm, tracker); 1357faf4d90SDavide Italiano } 136f53d15feSStephan Uphoff } 137f53d15feSStephan Uphoff 1387faf4d90SDavide Italiano static uintptr_t 139d02add54SRobert Watson unlock_rm(struct lock_object *lock) 140d02add54SRobert Watson { 1417faf4d90SDavide Italiano struct thread *td; 1427faf4d90SDavide Italiano struct pcpu *pc; 143cd32bd7aSJohn Baldwin struct rmlock *rm; 1447faf4d90SDavide Italiano struct rm_queue *queue; 1457faf4d90SDavide Italiano struct rm_priotracker *tracker; 1467faf4d90SDavide Italiano uintptr_t how; 147d02add54SRobert Watson 148cd32bd7aSJohn Baldwin rm = (struct rmlock *)lock; 1497faf4d90SDavide Italiano tracker = NULL; 1507faf4d90SDavide Italiano how = 0; 1517faf4d90SDavide Italiano rm_assert(rm, RA_LOCKED | RA_NOTRECURSED); 1527faf4d90SDavide Italiano if (rm_wowned(rm)) 153cd32bd7aSJohn Baldwin rm_wunlock(rm); 1547faf4d90SDavide Italiano else { 1557faf4d90SDavide Italiano /* 1567faf4d90SDavide Italiano * Find the right rm_priotracker structure for curthread. 1577faf4d90SDavide Italiano * The guarantee about its uniqueness is given by the fact 1587faf4d90SDavide Italiano * we already asserted the lock wasn't recursively acquired. 1597faf4d90SDavide Italiano */ 1607faf4d90SDavide Italiano critical_enter(); 1617faf4d90SDavide Italiano td = curthread; 162e2a8d178SJason A. Harmening pc = get_pcpu(); 1637faf4d90SDavide Italiano for (queue = pc->pc_rm_queue.rmq_next; 1647faf4d90SDavide Italiano queue != &pc->pc_rm_queue; queue = queue->rmq_next) { 1657faf4d90SDavide Italiano tracker = (struct rm_priotracker *)queue; 1667faf4d90SDavide Italiano if ((tracker->rmp_rmlock == rm) && 1677faf4d90SDavide Italiano (tracker->rmp_thread == td)) { 1687faf4d90SDavide Italiano how = (uintptr_t)tracker; 1697faf4d90SDavide Italiano break; 1707faf4d90SDavide Italiano } 1717faf4d90SDavide Italiano } 1727faf4d90SDavide Italiano KASSERT(tracker != NULL, 1737faf4d90SDavide Italiano ("rm_priotracker is non-NULL when lock held in read mode")); 1747faf4d90SDavide Italiano critical_exit(); 1757faf4d90SDavide Italiano rm_runlock(rm, tracker); 1767faf4d90SDavide Italiano } 1777faf4d90SDavide Italiano return (how); 178f53d15feSStephan Uphoff } 179f53d15feSStephan Uphoff 180a5aedd68SStacey Son #ifdef KDTRACE_HOOKS 181a5aedd68SStacey Son static int 182d576deedSPawel Jakub Dawidek owner_rm(const struct lock_object *lock, struct thread **owner) 183a5aedd68SStacey Son { 184cd32bd7aSJohn Baldwin const struct rmlock *rm; 185cd32bd7aSJohn Baldwin struct lock_class *lc; 186a5aedd68SStacey Son 187cd32bd7aSJohn Baldwin rm = (const struct rmlock *)lock; 188cd32bd7aSJohn Baldwin lc = LOCK_CLASS(&rm->rm_wlock_object); 189cd32bd7aSJohn Baldwin return (lc->lc_owner(&rm->rm_wlock_object, owner)); 190a5aedd68SStacey Son } 191a5aedd68SStacey Son #endif 192a5aedd68SStacey Son 193f53d15feSStephan Uphoff static struct mtx rm_spinlock; 194f53d15feSStephan Uphoff 195f53d15feSStephan Uphoff MTX_SYSINIT(rm_spinlock, &rm_spinlock, "rm_spinlock", MTX_SPIN); 196f53d15feSStephan Uphoff 197f53d15feSStephan Uphoff /* 198c7ca33d1SRobert Watson * Add or remove tracker from per-cpu list. 199d02add54SRobert Watson * 200c7ca33d1SRobert Watson * The per-cpu list can be traversed at any time in forward direction from an 201d02add54SRobert Watson * interrupt on the *local* cpu. 202f53d15feSStephan Uphoff */ 203f53d15feSStephan Uphoff static void inline 204d02add54SRobert Watson rm_tracker_add(struct pcpu *pc, struct rm_priotracker *tracker) 205d02add54SRobert Watson { 206f53d15feSStephan Uphoff struct rm_queue *next; 207d02add54SRobert Watson 208f53d15feSStephan Uphoff /* Initialize all tracker pointers */ 209f53d15feSStephan Uphoff tracker->rmp_cpuQueue.rmq_prev = &pc->pc_rm_queue; 210f53d15feSStephan Uphoff next = pc->pc_rm_queue.rmq_next; 211f53d15feSStephan Uphoff tracker->rmp_cpuQueue.rmq_next = next; 212d02add54SRobert Watson 213d02add54SRobert Watson /* rmq_prev is not used during froward traversal. */ 214f53d15feSStephan Uphoff next->rmq_prev = &tracker->rmp_cpuQueue; 215d02add54SRobert Watson 216d02add54SRobert Watson /* Update pointer to first element. */ 217f53d15feSStephan Uphoff pc->pc_rm_queue.rmq_next = &tracker->rmp_cpuQueue; 218f53d15feSStephan Uphoff } 219f53d15feSStephan Uphoff 220cd32bd7aSJohn Baldwin /* 221cd32bd7aSJohn Baldwin * Return a count of the number of trackers the thread 'td' already 222cd32bd7aSJohn Baldwin * has on this CPU for the lock 'rm'. 223cd32bd7aSJohn Baldwin */ 224cd32bd7aSJohn Baldwin static int 225cd32bd7aSJohn Baldwin rm_trackers_present(const struct pcpu *pc, const struct rmlock *rm, 226cd32bd7aSJohn Baldwin const struct thread *td) 227cd32bd7aSJohn Baldwin { 228cd32bd7aSJohn Baldwin struct rm_queue *queue; 229cd32bd7aSJohn Baldwin struct rm_priotracker *tracker; 230cd32bd7aSJohn Baldwin int count; 231cd32bd7aSJohn Baldwin 232cd32bd7aSJohn Baldwin count = 0; 233cd32bd7aSJohn Baldwin for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue; 234cd32bd7aSJohn Baldwin queue = queue->rmq_next) { 235cd32bd7aSJohn Baldwin tracker = (struct rm_priotracker *)queue; 236cd32bd7aSJohn Baldwin if ((tracker->rmp_rmlock == rm) && (tracker->rmp_thread == td)) 237cd32bd7aSJohn Baldwin count++; 238cd32bd7aSJohn Baldwin } 239cd32bd7aSJohn Baldwin return (count); 240cd32bd7aSJohn Baldwin } 241cd32bd7aSJohn Baldwin 242f53d15feSStephan Uphoff static void inline 243d02add54SRobert Watson rm_tracker_remove(struct pcpu *pc, struct rm_priotracker *tracker) 244d02add54SRobert Watson { 245f53d15feSStephan Uphoff struct rm_queue *next, *prev; 246d02add54SRobert Watson 247f53d15feSStephan Uphoff next = tracker->rmp_cpuQueue.rmq_next; 248f53d15feSStephan Uphoff prev = tracker->rmp_cpuQueue.rmq_prev; 249d02add54SRobert Watson 250d02add54SRobert Watson /* Not used during forward traversal. */ 251f53d15feSStephan Uphoff next->rmq_prev = prev; 252d02add54SRobert Watson 253d02add54SRobert Watson /* Remove from list. */ 254f53d15feSStephan Uphoff prev->rmq_next = next; 255f53d15feSStephan Uphoff } 256f53d15feSStephan Uphoff 257d02add54SRobert Watson static void 258d02add54SRobert Watson rm_cleanIPI(void *arg) 259d02add54SRobert Watson { 260f53d15feSStephan Uphoff struct pcpu *pc; 261f53d15feSStephan Uphoff struct rmlock *rm = arg; 262f53d15feSStephan Uphoff struct rm_priotracker *tracker; 263f53d15feSStephan Uphoff struct rm_queue *queue; 264e2a8d178SJason A. Harmening pc = get_pcpu(); 265f53d15feSStephan Uphoff 266d02add54SRobert Watson for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue; 267f53d15feSStephan Uphoff queue = queue->rmq_next) { 268f53d15feSStephan Uphoff tracker = (struct rm_priotracker *)queue; 269f53d15feSStephan Uphoff if (tracker->rmp_rmlock == rm && tracker->rmp_flags == 0) { 270f53d15feSStephan Uphoff tracker->rmp_flags = RMPF_ONQUEUE; 271f53d15feSStephan Uphoff mtx_lock_spin(&rm_spinlock); 272f53d15feSStephan Uphoff LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker, 273f53d15feSStephan Uphoff rmp_qentry); 274f53d15feSStephan Uphoff mtx_unlock_spin(&rm_spinlock); 275f53d15feSStephan Uphoff } 276f53d15feSStephan Uphoff } 277f53d15feSStephan Uphoff } 278f53d15feSStephan Uphoff 279f53d15feSStephan Uphoff void 2801a109c1cSRobert Watson rm_init_flags(struct rmlock *rm, const char *name, int opts) 281f53d15feSStephan Uphoff { 282cd32bd7aSJohn Baldwin struct lock_class *lc; 283fd07ddcfSDmitry Chagin int liflags, xflags; 284d02add54SRobert Watson 2851a109c1cSRobert Watson liflags = 0; 2861a109c1cSRobert Watson if (!(opts & RM_NOWITNESS)) 2871a109c1cSRobert Watson liflags |= LO_WITNESS; 2881a109c1cSRobert Watson if (opts & RM_RECURSE) 2891a109c1cSRobert Watson liflags |= LO_RECURSABLE; 290fd07ddcfSDmitry Chagin if (opts & RM_NEW) 291fd07ddcfSDmitry Chagin liflags |= LO_NEW; 29236058c09SMax Laier rm->rm_writecpus = all_cpus; 293f53d15feSStephan Uphoff LIST_INIT(&rm->rm_activeReaders); 29436058c09SMax Laier if (opts & RM_SLEEPABLE) { 295cd32bd7aSJohn Baldwin liflags |= LO_SLEEPABLE; 296cd32bd7aSJohn Baldwin lc = &lock_class_rm_sleepable; 297fd07ddcfSDmitry Chagin xflags = (opts & RM_NEW ? SX_NEW : 0); 298fd07ddcfSDmitry Chagin sx_init_flags(&rm->rm_lock_sx, "rmlock_sx", 299fd07ddcfSDmitry Chagin xflags | SX_NOWITNESS); 300cd32bd7aSJohn Baldwin } else { 301cd32bd7aSJohn Baldwin lc = &lock_class_rm; 302fd07ddcfSDmitry Chagin xflags = (opts & RM_NEW ? MTX_NEW : 0); 303fd07ddcfSDmitry Chagin mtx_init(&rm->rm_lock_mtx, name, "rmlock_mtx", 304fd07ddcfSDmitry Chagin xflags | MTX_NOWITNESS); 305cd32bd7aSJohn Baldwin } 306cd32bd7aSJohn Baldwin lock_init(&rm->lock_object, lc, name, NULL, liflags); 3071a109c1cSRobert Watson } 3081a109c1cSRobert Watson 3091a109c1cSRobert Watson void 3101a109c1cSRobert Watson rm_init(struct rmlock *rm, const char *name) 3111a109c1cSRobert Watson { 3121a109c1cSRobert Watson 3131a109c1cSRobert Watson rm_init_flags(rm, name, 0); 314f53d15feSStephan Uphoff } 315f53d15feSStephan Uphoff 316f53d15feSStephan Uphoff void 317f53d15feSStephan Uphoff rm_destroy(struct rmlock *rm) 318f53d15feSStephan Uphoff { 319d02add54SRobert Watson 320cd32bd7aSJohn Baldwin rm_assert(rm, RA_UNLOCKED); 321cd32bd7aSJohn Baldwin LIST_FIRST(&rm->rm_activeReaders) = RM_DESTROYED; 322cd32bd7aSJohn Baldwin if (rm->lock_object.lo_flags & LO_SLEEPABLE) 32336058c09SMax Laier sx_destroy(&rm->rm_lock_sx); 32436058c09SMax Laier else 32536058c09SMax Laier mtx_destroy(&rm->rm_lock_mtx); 326f53d15feSStephan Uphoff lock_destroy(&rm->lock_object); 327f53d15feSStephan Uphoff } 328f53d15feSStephan Uphoff 329433ea89aSRobert Watson int 330d576deedSPawel Jakub Dawidek rm_wowned(const struct rmlock *rm) 331433ea89aSRobert Watson { 332433ea89aSRobert Watson 333cd32bd7aSJohn Baldwin if (rm->lock_object.lo_flags & LO_SLEEPABLE) 33436058c09SMax Laier return (sx_xlocked(&rm->rm_lock_sx)); 33536058c09SMax Laier else 33636058c09SMax Laier return (mtx_owned(&rm->rm_lock_mtx)); 337433ea89aSRobert Watson } 338433ea89aSRobert Watson 339f53d15feSStephan Uphoff void 340f53d15feSStephan Uphoff rm_sysinit(void *arg) 341f53d15feSStephan Uphoff { 342755230ebSMark Johnston struct rm_args *args; 3431a109c1cSRobert Watson 344755230ebSMark Johnston args = arg; 345755230ebSMark Johnston rm_init_flags(args->ra_rm, args->ra_desc, args->ra_flags); 346f53d15feSStephan Uphoff } 347f53d15feSStephan Uphoff 34885c1b3c1SMateusz Guzik static __noinline int 34936058c09SMax Laier _rm_rlock_hard(struct rmlock *rm, struct rm_priotracker *tracker, int trylock) 350f53d15feSStephan Uphoff { 351f53d15feSStephan Uphoff struct pcpu *pc; 352f53d15feSStephan Uphoff 353f53d15feSStephan Uphoff critical_enter(); 354e2a8d178SJason A. Harmening pc = get_pcpu(); 355f53d15feSStephan Uphoff 356d02add54SRobert Watson /* Check if we just need to do a proper critical_exit. */ 357a38f1f26SAttilio Rao if (!CPU_ISSET(pc->pc_cpuid, &rm->rm_writecpus)) { 358f53d15feSStephan Uphoff critical_exit(); 35936058c09SMax Laier return (1); 360f53d15feSStephan Uphoff } 361f53d15feSStephan Uphoff 362c7ca33d1SRobert Watson /* Remove our tracker from the per-cpu list. */ 363f53d15feSStephan Uphoff rm_tracker_remove(pc, tracker); 364f53d15feSStephan Uphoff 3651d44514fSMark Johnston /* 3661d44514fSMark Johnston * Check to see if the IPI granted us the lock after all. The load of 3671d44514fSMark Johnston * rmp_flags must happen after the tracker is removed from the list. 3681d44514fSMark Johnston */ 369*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 370f53d15feSStephan Uphoff if (tracker->rmp_flags) { 371d02add54SRobert Watson /* Just add back tracker - we hold the lock. */ 372f53d15feSStephan Uphoff rm_tracker_add(pc, tracker); 373f53d15feSStephan Uphoff critical_exit(); 37436058c09SMax Laier return (1); 375f53d15feSStephan Uphoff } 376f53d15feSStephan Uphoff 377f53d15feSStephan Uphoff /* 378e3043798SPedro F. Giffuni * We allow readers to acquire a lock even if a writer is blocked if 379d02add54SRobert Watson * the lock is recursive and the reader already holds the lock. 380f53d15feSStephan Uphoff */ 381f53d15feSStephan Uphoff if ((rm->lock_object.lo_flags & LO_RECURSABLE) != 0) { 382f53d15feSStephan Uphoff /* 383c7ca33d1SRobert Watson * Just grant the lock if this thread already has a tracker 384c7ca33d1SRobert Watson * for this lock on the per-cpu queue. 385f53d15feSStephan Uphoff */ 386cd32bd7aSJohn Baldwin if (rm_trackers_present(pc, rm, curthread) != 0) { 387f53d15feSStephan Uphoff mtx_lock_spin(&rm_spinlock); 388cd32bd7aSJohn Baldwin LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker, 389cd32bd7aSJohn Baldwin rmp_qentry); 390f53d15feSStephan Uphoff tracker->rmp_flags = RMPF_ONQUEUE; 391f53d15feSStephan Uphoff mtx_unlock_spin(&rm_spinlock); 392f53d15feSStephan Uphoff rm_tracker_add(pc, tracker); 393f53d15feSStephan Uphoff critical_exit(); 39436058c09SMax Laier return (1); 395f53d15feSStephan Uphoff } 396f53d15feSStephan Uphoff } 397f53d15feSStephan Uphoff 398f53d15feSStephan Uphoff sched_unpin(); 399f53d15feSStephan Uphoff critical_exit(); 400f53d15feSStephan Uphoff 40136058c09SMax Laier if (trylock) { 402cd32bd7aSJohn Baldwin if (rm->lock_object.lo_flags & LO_SLEEPABLE) { 40336058c09SMax Laier if (!sx_try_xlock(&rm->rm_lock_sx)) 40436058c09SMax Laier return (0); 40536058c09SMax Laier } else { 40636058c09SMax Laier if (!mtx_trylock(&rm->rm_lock_mtx)) 40736058c09SMax Laier return (0); 40836058c09SMax Laier } 40936058c09SMax Laier } else { 410e89d5f43SJohn Baldwin if (rm->lock_object.lo_flags & LO_SLEEPABLE) { 411e89d5f43SJohn Baldwin THREAD_SLEEPING_OK(); 41236058c09SMax Laier sx_xlock(&rm->rm_lock_sx); 413e89d5f43SJohn Baldwin THREAD_NO_SLEEPING(); 414e89d5f43SJohn Baldwin } else 41536058c09SMax Laier mtx_lock(&rm->rm_lock_mtx); 41636058c09SMax Laier } 417f53d15feSStephan Uphoff 41836058c09SMax Laier critical_enter(); 419e2a8d178SJason A. Harmening pc = get_pcpu(); 420a38f1f26SAttilio Rao CPU_CLR(pc->pc_cpuid, &rm->rm_writecpus); 421f53d15feSStephan Uphoff rm_tracker_add(pc, tracker); 422f53d15feSStephan Uphoff sched_pin(); 423f53d15feSStephan Uphoff critical_exit(); 424f53d15feSStephan Uphoff 425cd32bd7aSJohn Baldwin if (rm->lock_object.lo_flags & LO_SLEEPABLE) 42636058c09SMax Laier sx_xunlock(&rm->rm_lock_sx); 42736058c09SMax Laier else 42836058c09SMax Laier mtx_unlock(&rm->rm_lock_mtx); 42936058c09SMax Laier 43036058c09SMax Laier return (1); 431f53d15feSStephan Uphoff } 432f53d15feSStephan Uphoff 43336058c09SMax Laier int 43436058c09SMax Laier _rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker, int trylock) 435f53d15feSStephan Uphoff { 436f53d15feSStephan Uphoff struct thread *td = curthread; 437f53d15feSStephan Uphoff struct pcpu *pc; 438f53d15feSStephan Uphoff 43935370593SAndriy Gapon if (SCHEDULER_STOPPED()) 44035370593SAndriy Gapon return (1); 44135370593SAndriy Gapon 442f53d15feSStephan Uphoff tracker->rmp_flags = 0; 443f53d15feSStephan Uphoff tracker->rmp_thread = td; 444f53d15feSStephan Uphoff tracker->rmp_rmlock = rm; 445f53d15feSStephan Uphoff 446cd32bd7aSJohn Baldwin if (rm->lock_object.lo_flags & LO_SLEEPABLE) 447cd32bd7aSJohn Baldwin THREAD_NO_SLEEPING(); 448cd32bd7aSJohn Baldwin 449f53d15feSStephan Uphoff td->td_critnest++; /* critical_enter(); */ 450f53d15feSStephan Uphoff 451*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 452f53d15feSStephan Uphoff 453f53d15feSStephan Uphoff pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */ 454f53d15feSStephan Uphoff 455f53d15feSStephan Uphoff rm_tracker_add(pc, tracker); 456f53d15feSStephan Uphoff 45782b7a39cSRobert Watson sched_pin(); 458f53d15feSStephan Uphoff 459*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 460f53d15feSStephan Uphoff 461f53d15feSStephan Uphoff td->td_critnest--; 462f53d15feSStephan Uphoff 463f53d15feSStephan Uphoff /* 464d02add54SRobert Watson * Fast path to combine two common conditions into a single 465d02add54SRobert Watson * conditional jump. 466f53d15feSStephan Uphoff */ 46785c1b3c1SMateusz Guzik if (__predict_true(0 == (td->td_owepreempt | 46885c1b3c1SMateusz Guzik CPU_ISSET(pc->pc_cpuid, &rm->rm_writecpus)))) 46936058c09SMax Laier return (1); 470f53d15feSStephan Uphoff 471d02add54SRobert Watson /* We do not have a read token and need to acquire one. */ 47236058c09SMax Laier return _rm_rlock_hard(rm, tracker, trylock); 473f53d15feSStephan Uphoff } 474f53d15feSStephan Uphoff 47585c1b3c1SMateusz Guzik static __noinline void 476f53d15feSStephan Uphoff _rm_unlock_hard(struct thread *td,struct rm_priotracker *tracker) 477f53d15feSStephan Uphoff { 478f53d15feSStephan Uphoff 479f53d15feSStephan Uphoff if (td->td_owepreempt) { 480f53d15feSStephan Uphoff td->td_critnest++; 481f53d15feSStephan Uphoff critical_exit(); 482f53d15feSStephan Uphoff } 483f53d15feSStephan Uphoff 484d02add54SRobert Watson if (!tracker->rmp_flags) 485f53d15feSStephan Uphoff return; 486f53d15feSStephan Uphoff 487f53d15feSStephan Uphoff mtx_lock_spin(&rm_spinlock); 488f53d15feSStephan Uphoff LIST_REMOVE(tracker, rmp_qentry); 489f53d15feSStephan Uphoff 490f53d15feSStephan Uphoff if (tracker->rmp_flags & RMPF_SIGNAL) { 491f53d15feSStephan Uphoff struct rmlock *rm; 492f53d15feSStephan Uphoff struct turnstile *ts; 493f53d15feSStephan Uphoff 494f53d15feSStephan Uphoff rm = tracker->rmp_rmlock; 495f53d15feSStephan Uphoff 496f53d15feSStephan Uphoff turnstile_chain_lock(&rm->lock_object); 497f53d15feSStephan Uphoff mtx_unlock_spin(&rm_spinlock); 498f53d15feSStephan Uphoff 499f53d15feSStephan Uphoff ts = turnstile_lookup(&rm->lock_object); 500f53d15feSStephan Uphoff 501f53d15feSStephan Uphoff turnstile_signal(ts, TS_EXCLUSIVE_QUEUE); 502d0a22279SMateusz Guzik turnstile_unpend(ts); 503f53d15feSStephan Uphoff turnstile_chain_unlock(&rm->lock_object); 504f53d15feSStephan Uphoff } else 505f53d15feSStephan Uphoff mtx_unlock_spin(&rm_spinlock); 506f53d15feSStephan Uphoff } 507f53d15feSStephan Uphoff 508f53d15feSStephan Uphoff void 509f53d15feSStephan Uphoff _rm_runlock(struct rmlock *rm, struct rm_priotracker *tracker) 510f53d15feSStephan Uphoff { 511f53d15feSStephan Uphoff struct pcpu *pc; 512f53d15feSStephan Uphoff struct thread *td = tracker->rmp_thread; 513f53d15feSStephan Uphoff 51435370593SAndriy Gapon if (SCHEDULER_STOPPED()) 51535370593SAndriy Gapon return; 51635370593SAndriy Gapon 517f53d15feSStephan Uphoff td->td_critnest++; /* critical_enter(); */ 518f53d15feSStephan Uphoff pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */ 519f53d15feSStephan Uphoff rm_tracker_remove(pc, tracker); 520f53d15feSStephan Uphoff td->td_critnest--; 52182b7a39cSRobert Watson sched_unpin(); 522f53d15feSStephan Uphoff 523cd32bd7aSJohn Baldwin if (rm->lock_object.lo_flags & LO_SLEEPABLE) 524cd32bd7aSJohn Baldwin THREAD_SLEEPING_OK(); 525cd32bd7aSJohn Baldwin 52685c1b3c1SMateusz Guzik if (__predict_true(0 == (td->td_owepreempt | tracker->rmp_flags))) 527f53d15feSStephan Uphoff return; 528f53d15feSStephan Uphoff 529f53d15feSStephan Uphoff _rm_unlock_hard(td, tracker); 530f53d15feSStephan Uphoff } 531f53d15feSStephan Uphoff 532f53d15feSStephan Uphoff void 533f53d15feSStephan Uphoff _rm_wlock(struct rmlock *rm) 534f53d15feSStephan Uphoff { 535f53d15feSStephan Uphoff struct rm_priotracker *prio; 536f53d15feSStephan Uphoff struct turnstile *ts; 53771a19bdcSAttilio Rao cpuset_t readcpus; 538f53d15feSStephan Uphoff 53935370593SAndriy Gapon if (SCHEDULER_STOPPED()) 54035370593SAndriy Gapon return; 54135370593SAndriy Gapon 542cd32bd7aSJohn Baldwin if (rm->lock_object.lo_flags & LO_SLEEPABLE) 54336058c09SMax Laier sx_xlock(&rm->rm_lock_sx); 54436058c09SMax Laier else 54536058c09SMax Laier mtx_lock(&rm->rm_lock_mtx); 546f53d15feSStephan Uphoff 54771a19bdcSAttilio Rao if (CPU_CMP(&rm->rm_writecpus, &all_cpus)) { 548f53d15feSStephan Uphoff /* Get all read tokens back */ 54971a19bdcSAttilio Rao readcpus = all_cpus; 5509825eadfSRyan Libby CPU_ANDNOT(&readcpus, &rm->rm_writecpus); 55136058c09SMax Laier rm->rm_writecpus = all_cpus; 552f53d15feSStephan Uphoff 553f53d15feSStephan Uphoff /* 55436058c09SMax Laier * Assumes rm->rm_writecpus update is visible on other CPUs 555d02add54SRobert Watson * before rm_cleanIPI is called. 556f53d15feSStephan Uphoff */ 557f53d15feSStephan Uphoff #ifdef SMP 55836058c09SMax Laier smp_rendezvous_cpus(readcpus, 55967d955aaSPatrick Kelsey smp_no_rendezvous_barrier, 560f53d15feSStephan Uphoff rm_cleanIPI, 56167d955aaSPatrick Kelsey smp_no_rendezvous_barrier, 562d02add54SRobert Watson rm); 563f53d15feSStephan Uphoff 564f53d15feSStephan Uphoff #else 565f53d15feSStephan Uphoff rm_cleanIPI(rm); 566f53d15feSStephan Uphoff #endif 567f53d15feSStephan Uphoff 568f53d15feSStephan Uphoff mtx_lock_spin(&rm_spinlock); 569f53d15feSStephan Uphoff while ((prio = LIST_FIRST(&rm->rm_activeReaders)) != NULL) { 570f53d15feSStephan Uphoff ts = turnstile_trywait(&rm->lock_object); 571f53d15feSStephan Uphoff prio->rmp_flags = RMPF_ONQUEUE | RMPF_SIGNAL; 572f53d15feSStephan Uphoff mtx_unlock_spin(&rm_spinlock); 573f53d15feSStephan Uphoff turnstile_wait(ts, prio->rmp_thread, 574f53d15feSStephan Uphoff TS_EXCLUSIVE_QUEUE); 575f53d15feSStephan Uphoff mtx_lock_spin(&rm_spinlock); 576f53d15feSStephan Uphoff } 577f53d15feSStephan Uphoff mtx_unlock_spin(&rm_spinlock); 578f53d15feSStephan Uphoff } 579f53d15feSStephan Uphoff } 580f53d15feSStephan Uphoff 581f53d15feSStephan Uphoff void 582f53d15feSStephan Uphoff _rm_wunlock(struct rmlock *rm) 583f53d15feSStephan Uphoff { 584d02add54SRobert Watson 585cd32bd7aSJohn Baldwin if (rm->lock_object.lo_flags & LO_SLEEPABLE) 58636058c09SMax Laier sx_xunlock(&rm->rm_lock_sx); 58736058c09SMax Laier else 58836058c09SMax Laier mtx_unlock(&rm->rm_lock_mtx); 589f53d15feSStephan Uphoff } 590f53d15feSStephan Uphoff 59141f5f69fSAndrey V. Elsukov #if LOCK_DEBUG > 0 592f53d15feSStephan Uphoff 593cd32bd7aSJohn Baldwin void 594cd32bd7aSJohn Baldwin _rm_wlock_debug(struct rmlock *rm, const char *file, int line) 595f53d15feSStephan Uphoff { 596f53d15feSStephan Uphoff 59735370593SAndriy Gapon if (SCHEDULER_STOPPED()) 59835370593SAndriy Gapon return; 59935370593SAndriy Gapon 600cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 601e3ae0dfeSAttilio Rao ("rm_wlock() by idle thread %p on rmlock %s @ %s:%d", 602e3ae0dfeSAttilio Rao curthread, rm->lock_object.lo_name, file, line)); 603cd32bd7aSJohn Baldwin KASSERT(!rm_destroyed(rm), 604cd32bd7aSJohn Baldwin ("rm_wlock() of destroyed rmlock @ %s:%d", file, line)); 605cd32bd7aSJohn Baldwin _rm_assert(rm, RA_UNLOCKED, file, line); 606cd32bd7aSJohn Baldwin 607f53d15feSStephan Uphoff WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE, 60841313430SJohn Baldwin file, line, NULL); 609f53d15feSStephan Uphoff 610f53d15feSStephan Uphoff _rm_wlock(rm); 611f53d15feSStephan Uphoff 612f53d15feSStephan Uphoff LOCK_LOG_LOCK("RMWLOCK", &rm->lock_object, 0, 0, file, line); 613f53d15feSStephan Uphoff WITNESS_LOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line); 614ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 615f53d15feSStephan Uphoff } 616f53d15feSStephan Uphoff 617d02add54SRobert Watson void 618d02add54SRobert Watson _rm_wunlock_debug(struct rmlock *rm, const char *file, int line) 619f53d15feSStephan Uphoff { 620d02add54SRobert Watson 62135370593SAndriy Gapon if (SCHEDULER_STOPPED()) 62235370593SAndriy Gapon return; 62335370593SAndriy Gapon 624cd32bd7aSJohn Baldwin KASSERT(!rm_destroyed(rm), 625cd32bd7aSJohn Baldwin ("rm_wunlock() of destroyed rmlock @ %s:%d", file, line)); 626cd32bd7aSJohn Baldwin _rm_assert(rm, RA_WLOCKED, file, line); 627f53d15feSStephan Uphoff WITNESS_UNLOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line); 628f53d15feSStephan Uphoff LOCK_LOG_LOCK("RMWUNLOCK", &rm->lock_object, 0, 0, file, line); 629f53d15feSStephan Uphoff _rm_wunlock(rm); 630ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 631f53d15feSStephan Uphoff } 632f53d15feSStephan Uphoff 63336058c09SMax Laier int 634f53d15feSStephan Uphoff _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker, 63536058c09SMax Laier int trylock, const char *file, int line) 636f53d15feSStephan Uphoff { 63735370593SAndriy Gapon 63835370593SAndriy Gapon if (SCHEDULER_STOPPED()) 63935370593SAndriy Gapon return (1); 64035370593SAndriy Gapon 641cd32bd7aSJohn Baldwin #ifdef INVARIANTS 642cd32bd7aSJohn Baldwin if (!(rm->lock_object.lo_flags & LO_RECURSABLE) && !trylock) { 643cd32bd7aSJohn Baldwin critical_enter(); 644e2a8d178SJason A. Harmening KASSERT(rm_trackers_present(get_pcpu(), rm, 645cd32bd7aSJohn Baldwin curthread) == 0, 646cd32bd7aSJohn Baldwin ("rm_rlock: recursed on non-recursive rmlock %s @ %s:%d\n", 647cd32bd7aSJohn Baldwin rm->lock_object.lo_name, file, line)); 648cd32bd7aSJohn Baldwin critical_exit(); 649cd32bd7aSJohn Baldwin } 650cd32bd7aSJohn Baldwin #endif 651cd2fe4e6SAttilio Rao KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread), 652e3ae0dfeSAttilio Rao ("rm_rlock() by idle thread %p on rmlock %s @ %s:%d", 653e3ae0dfeSAttilio Rao curthread, rm->lock_object.lo_name, file, line)); 654cd32bd7aSJohn Baldwin KASSERT(!rm_destroyed(rm), 655cd32bd7aSJohn Baldwin ("rm_rlock() of destroyed rmlock @ %s:%d", file, line)); 656cd32bd7aSJohn Baldwin if (!trylock) { 657cd32bd7aSJohn Baldwin KASSERT(!rm_wowned(rm), 658cd32bd7aSJohn Baldwin ("rm_rlock: wlock already held for %s @ %s:%d", 659cd32bd7aSJohn Baldwin rm->lock_object.lo_name, file, line)); 66059fb4a95SRyan Libby WITNESS_CHECKORDER(&rm->lock_object, 66159fb4a95SRyan Libby LOP_NEWORDER | LOP_NOSLEEP, file, line, NULL); 662cd32bd7aSJohn Baldwin } 663f53d15feSStephan Uphoff 66436058c09SMax Laier if (_rm_rlock(rm, tracker, trylock)) { 665cd32bd7aSJohn Baldwin if (trylock) 666cd32bd7aSJohn Baldwin LOCK_LOG_TRY("RMRLOCK", &rm->lock_object, 0, 1, file, 667cd32bd7aSJohn Baldwin line); 668cd32bd7aSJohn Baldwin else 669cd32bd7aSJohn Baldwin LOCK_LOG_LOCK("RMRLOCK", &rm->lock_object, 0, 0, file, 670cd32bd7aSJohn Baldwin line); 67159fb4a95SRyan Libby WITNESS_LOCK(&rm->lock_object, LOP_NOSLEEP, file, line); 672ce1c953eSMark Johnston TD_LOCKS_INC(curthread); 67336058c09SMax Laier return (1); 674cd32bd7aSJohn Baldwin } else if (trylock) 675cd32bd7aSJohn Baldwin LOCK_LOG_TRY("RMRLOCK", &rm->lock_object, 0, 0, file, line); 67636058c09SMax Laier 67736058c09SMax Laier return (0); 678f53d15feSStephan Uphoff } 679f53d15feSStephan Uphoff 680f53d15feSStephan Uphoff void 681f53d15feSStephan Uphoff _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker, 682d02add54SRobert Watson const char *file, int line) 683d02add54SRobert Watson { 684d02add54SRobert Watson 68535370593SAndriy Gapon if (SCHEDULER_STOPPED()) 68635370593SAndriy Gapon return; 68735370593SAndriy Gapon 688cd32bd7aSJohn Baldwin KASSERT(!rm_destroyed(rm), 689cd32bd7aSJohn Baldwin ("rm_runlock() of destroyed rmlock @ %s:%d", file, line)); 690cd32bd7aSJohn Baldwin _rm_assert(rm, RA_RLOCKED, file, line); 691f53d15feSStephan Uphoff WITNESS_UNLOCK(&rm->lock_object, 0, file, line); 692f53d15feSStephan Uphoff LOCK_LOG_LOCK("RMRUNLOCK", &rm->lock_object, 0, 0, file, line); 693f53d15feSStephan Uphoff _rm_runlock(rm, tracker); 694ce1c953eSMark Johnston TD_LOCKS_DEC(curthread); 695f53d15feSStephan Uphoff } 696f53d15feSStephan Uphoff 697f53d15feSStephan Uphoff #else 698d02add54SRobert Watson 699f53d15feSStephan Uphoff /* 700d02add54SRobert Watson * Just strip out file and line arguments if no lock debugging is enabled in 701d02add54SRobert Watson * the kernel - we are called from a kernel module. 702f53d15feSStephan Uphoff */ 703d02add54SRobert Watson void 704d02add54SRobert Watson _rm_wlock_debug(struct rmlock *rm, const char *file, int line) 705f53d15feSStephan Uphoff { 706d02add54SRobert Watson 707f53d15feSStephan Uphoff _rm_wlock(rm); 708f53d15feSStephan Uphoff } 709f53d15feSStephan Uphoff 710d02add54SRobert Watson void 711d02add54SRobert Watson _rm_wunlock_debug(struct rmlock *rm, const char *file, int line) 712f53d15feSStephan Uphoff { 713d02add54SRobert Watson 714f53d15feSStephan Uphoff _rm_wunlock(rm); 715f53d15feSStephan Uphoff } 716f53d15feSStephan Uphoff 71736058c09SMax Laier int 718f53d15feSStephan Uphoff _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker, 71936058c09SMax Laier int trylock, const char *file, int line) 720f53d15feSStephan Uphoff { 721d02add54SRobert Watson 72236058c09SMax Laier return _rm_rlock(rm, tracker, trylock); 723f53d15feSStephan Uphoff } 724f53d15feSStephan Uphoff 725f53d15feSStephan Uphoff void 726f53d15feSStephan Uphoff _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker, 7271191932aSRobert Watson const char *file, int line) 7281191932aSRobert Watson { 729d02add54SRobert Watson 730f53d15feSStephan Uphoff _rm_runlock(rm, tracker); 731f53d15feSStephan Uphoff } 732f53d15feSStephan Uphoff 733f53d15feSStephan Uphoff #endif 734cd32bd7aSJohn Baldwin 735cd32bd7aSJohn Baldwin #ifdef INVARIANT_SUPPORT 736c64bc3a0SJohn Baldwin #ifndef INVARIANTS 737c64bc3a0SJohn Baldwin #undef _rm_assert 738c64bc3a0SJohn Baldwin #endif 739c64bc3a0SJohn Baldwin 740cd32bd7aSJohn Baldwin /* 741cd32bd7aSJohn Baldwin * Note that this does not need to use witness_assert() for read lock 742cd32bd7aSJohn Baldwin * assertions since an exact count of read locks held by this thread 743cd32bd7aSJohn Baldwin * is computable. 744cd32bd7aSJohn Baldwin */ 745cd32bd7aSJohn Baldwin void 746cd32bd7aSJohn Baldwin _rm_assert(const struct rmlock *rm, int what, const char *file, int line) 747cd32bd7aSJohn Baldwin { 748cd32bd7aSJohn Baldwin int count; 749cd32bd7aSJohn Baldwin 750d54474e6SEric van Gyzen if (SCHEDULER_STOPPED()) 751cd32bd7aSJohn Baldwin return; 752cd32bd7aSJohn Baldwin switch (what) { 753cd32bd7aSJohn Baldwin case RA_LOCKED: 754cd32bd7aSJohn Baldwin case RA_LOCKED | RA_RECURSED: 755cd32bd7aSJohn Baldwin case RA_LOCKED | RA_NOTRECURSED: 756cd32bd7aSJohn Baldwin case RA_RLOCKED: 757cd32bd7aSJohn Baldwin case RA_RLOCKED | RA_RECURSED: 758cd32bd7aSJohn Baldwin case RA_RLOCKED | RA_NOTRECURSED: 759cd32bd7aSJohn Baldwin /* 760cd32bd7aSJohn Baldwin * Handle the write-locked case. Unlike other 761cd32bd7aSJohn Baldwin * primitives, writers can never recurse. 762cd32bd7aSJohn Baldwin */ 763cd32bd7aSJohn Baldwin if (rm_wowned(rm)) { 764cd32bd7aSJohn Baldwin if (what & RA_RLOCKED) 765cd32bd7aSJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 766cd32bd7aSJohn Baldwin rm->lock_object.lo_name, file, line); 767cd32bd7aSJohn Baldwin if (what & RA_RECURSED) 768cd32bd7aSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 769cd32bd7aSJohn Baldwin rm->lock_object.lo_name, file, line); 770cd32bd7aSJohn Baldwin break; 771cd32bd7aSJohn Baldwin } 772cd32bd7aSJohn Baldwin 773cd32bd7aSJohn Baldwin critical_enter(); 774e2a8d178SJason A. Harmening count = rm_trackers_present(get_pcpu(), rm, curthread); 775cd32bd7aSJohn Baldwin critical_exit(); 776cd32bd7aSJohn Baldwin 777cd32bd7aSJohn Baldwin if (count == 0) 778cd32bd7aSJohn Baldwin panic("Lock %s not %slocked @ %s:%d\n", 779cd32bd7aSJohn Baldwin rm->lock_object.lo_name, (what & RA_RLOCKED) ? 780cd32bd7aSJohn Baldwin "read " : "", file, line); 781cd32bd7aSJohn Baldwin if (count > 1) { 782cd32bd7aSJohn Baldwin if (what & RA_NOTRECURSED) 783cd32bd7aSJohn Baldwin panic("Lock %s recursed @ %s:%d\n", 784cd32bd7aSJohn Baldwin rm->lock_object.lo_name, file, line); 785cd32bd7aSJohn Baldwin } else if (what & RA_RECURSED) 786cd32bd7aSJohn Baldwin panic("Lock %s not recursed @ %s:%d\n", 787cd32bd7aSJohn Baldwin rm->lock_object.lo_name, file, line); 788cd32bd7aSJohn Baldwin break; 789cd32bd7aSJohn Baldwin case RA_WLOCKED: 790cd32bd7aSJohn Baldwin if (!rm_wowned(rm)) 791cd32bd7aSJohn Baldwin panic("Lock %s not exclusively locked @ %s:%d\n", 792cd32bd7aSJohn Baldwin rm->lock_object.lo_name, file, line); 793cd32bd7aSJohn Baldwin break; 794cd32bd7aSJohn Baldwin case RA_UNLOCKED: 795cd32bd7aSJohn Baldwin if (rm_wowned(rm)) 796cd32bd7aSJohn Baldwin panic("Lock %s exclusively locked @ %s:%d\n", 797cd32bd7aSJohn Baldwin rm->lock_object.lo_name, file, line); 798cd32bd7aSJohn Baldwin 799cd32bd7aSJohn Baldwin critical_enter(); 800e2a8d178SJason A. Harmening count = rm_trackers_present(get_pcpu(), rm, curthread); 801cd32bd7aSJohn Baldwin critical_exit(); 802cd32bd7aSJohn Baldwin 803cd32bd7aSJohn Baldwin if (count != 0) 804cd32bd7aSJohn Baldwin panic("Lock %s read locked @ %s:%d\n", 805cd32bd7aSJohn Baldwin rm->lock_object.lo_name, file, line); 806cd32bd7aSJohn Baldwin break; 807cd32bd7aSJohn Baldwin default: 808cd32bd7aSJohn Baldwin panic("Unknown rm lock assertion: %d @ %s:%d", what, file, 809cd32bd7aSJohn Baldwin line); 810cd32bd7aSJohn Baldwin } 811cd32bd7aSJohn Baldwin } 812cd32bd7aSJohn Baldwin #endif /* INVARIANT_SUPPORT */ 813cd32bd7aSJohn Baldwin 814cd32bd7aSJohn Baldwin #ifdef DDB 815cd32bd7aSJohn Baldwin static void 816cd32bd7aSJohn Baldwin print_tracker(struct rm_priotracker *tr) 817cd32bd7aSJohn Baldwin { 818cd32bd7aSJohn Baldwin struct thread *td; 819cd32bd7aSJohn Baldwin 820cd32bd7aSJohn Baldwin td = tr->rmp_thread; 821cd32bd7aSJohn Baldwin db_printf(" thread %p (tid %d, pid %d, \"%s\") {", td, td->td_tid, 822cd32bd7aSJohn Baldwin td->td_proc->p_pid, td->td_name); 823cd32bd7aSJohn Baldwin if (tr->rmp_flags & RMPF_ONQUEUE) { 824cd32bd7aSJohn Baldwin db_printf("ONQUEUE"); 825cd32bd7aSJohn Baldwin if (tr->rmp_flags & RMPF_SIGNAL) 826cd32bd7aSJohn Baldwin db_printf(",SIGNAL"); 827cd32bd7aSJohn Baldwin } else 828cd32bd7aSJohn Baldwin db_printf("0"); 829cd32bd7aSJohn Baldwin db_printf("}\n"); 830cd32bd7aSJohn Baldwin } 831cd32bd7aSJohn Baldwin 832cd32bd7aSJohn Baldwin static void 833cd32bd7aSJohn Baldwin db_show_rm(const struct lock_object *lock) 834cd32bd7aSJohn Baldwin { 835cd32bd7aSJohn Baldwin struct rm_priotracker *tr; 836cd32bd7aSJohn Baldwin struct rm_queue *queue; 837cd32bd7aSJohn Baldwin const struct rmlock *rm; 838cd32bd7aSJohn Baldwin struct lock_class *lc; 839cd32bd7aSJohn Baldwin struct pcpu *pc; 840cd32bd7aSJohn Baldwin 841cd32bd7aSJohn Baldwin rm = (const struct rmlock *)lock; 842cd32bd7aSJohn Baldwin db_printf(" writecpus: "); 843cd32bd7aSJohn Baldwin ddb_display_cpuset(__DEQUALIFY(const cpuset_t *, &rm->rm_writecpus)); 844cd32bd7aSJohn Baldwin db_printf("\n"); 845cd32bd7aSJohn Baldwin db_printf(" per-CPU readers:\n"); 846cd32bd7aSJohn Baldwin STAILQ_FOREACH(pc, &cpuhead, pc_allcpu) 847cd32bd7aSJohn Baldwin for (queue = pc->pc_rm_queue.rmq_next; 848cd32bd7aSJohn Baldwin queue != &pc->pc_rm_queue; queue = queue->rmq_next) { 849cd32bd7aSJohn Baldwin tr = (struct rm_priotracker *)queue; 850cd32bd7aSJohn Baldwin if (tr->rmp_rmlock == rm) 851cd32bd7aSJohn Baldwin print_tracker(tr); 852cd32bd7aSJohn Baldwin } 853cd32bd7aSJohn Baldwin db_printf(" active readers:\n"); 854cd32bd7aSJohn Baldwin LIST_FOREACH(tr, &rm->rm_activeReaders, rmp_qentry) 855cd32bd7aSJohn Baldwin print_tracker(tr); 856cd32bd7aSJohn Baldwin lc = LOCK_CLASS(&rm->rm_wlock_object); 857cd32bd7aSJohn Baldwin db_printf("Backing write-lock (%s):\n", lc->lc_name); 858cd32bd7aSJohn Baldwin lc->lc_ddb_show(&rm->rm_wlock_object); 859cd32bd7aSJohn Baldwin } 860cd32bd7aSJohn Baldwin #endif 8611f162fefSMateusz Guzik 8621f162fefSMateusz Guzik /* 8631f162fefSMateusz Guzik * Read-mostly sleepable locks. 8641f162fefSMateusz Guzik * 8651f162fefSMateusz Guzik * These primitives allow both readers and writers to sleep. However, neither 8661f162fefSMateusz Guzik * readers nor writers are tracked and subsequently there is no priority 8671f162fefSMateusz Guzik * propagation. 8681f162fefSMateusz Guzik * 8691f162fefSMateusz Guzik * They are intended to be only used when write-locking is almost never needed 8701f162fefSMateusz Guzik * (e.g., they can guard against unloading a kernel module) while read-locking 8711f162fefSMateusz Guzik * happens all the time. 8721f162fefSMateusz Guzik * 8731f162fefSMateusz Guzik * Concurrent writers take turns taking the lock while going off cpu. If this is 8741f162fefSMateusz Guzik * of concern for your usecase, this is not the right primitive. 8751f162fefSMateusz Guzik * 876*b5449c92SKonstantin Belousov * Neither rms_rlock nor rms_runlock use thread fences. Instead interrupt 877*b5449c92SKonstantin Belousov * fences are inserted to ensure ordering with the code executed in the IPI 878*b5449c92SKonstantin Belousov * handler. 8793211e783SMateusz Guzik * 8803211e783SMateusz Guzik * No attempt is made to track which CPUs read locked at least once, 8813211e783SMateusz Guzik * consequently write locking sends IPIs to all of them. This will become a 8828541ae04SMateusz Guzik * problem at some point. The easiest way to lessen it is to provide a bitmap. 8831f162fefSMateusz Guzik */ 8841f162fefSMateusz Guzik 8856fc2b069SMateusz Guzik #define RMS_NOOWNER ((void *)0x1) 8866fc2b069SMateusz Guzik #define RMS_TRANSIENT ((void *)0x2) 8876fc2b069SMateusz Guzik #define RMS_FLAGMASK 0xf 8886fc2b069SMateusz Guzik 88942e7abd5SMateusz Guzik struct rmslock_pcpu { 89042e7abd5SMateusz Guzik int influx; 89142e7abd5SMateusz Guzik int readers; 89242e7abd5SMateusz Guzik }; 89342e7abd5SMateusz Guzik 89442e7abd5SMateusz Guzik _Static_assert(sizeof(struct rmslock_pcpu) == 8, "bad size"); 89542e7abd5SMateusz Guzik 89642e7abd5SMateusz Guzik /* 89742e7abd5SMateusz Guzik * Internal routines 89842e7abd5SMateusz Guzik */ 89942e7abd5SMateusz Guzik static struct rmslock_pcpu * 90042e7abd5SMateusz Guzik rms_int_pcpu(struct rmslock *rms) 90142e7abd5SMateusz Guzik { 90242e7abd5SMateusz Guzik 90342e7abd5SMateusz Guzik CRITICAL_ASSERT(curthread); 90442e7abd5SMateusz Guzik return (zpcpu_get(rms->pcpu)); 90542e7abd5SMateusz Guzik } 90642e7abd5SMateusz Guzik 90742e7abd5SMateusz Guzik static struct rmslock_pcpu * 90842e7abd5SMateusz Guzik rms_int_remote_pcpu(struct rmslock *rms, int cpu) 90942e7abd5SMateusz Guzik { 91042e7abd5SMateusz Guzik 91142e7abd5SMateusz Guzik return (zpcpu_get_cpu(rms->pcpu, cpu)); 91242e7abd5SMateusz Guzik } 91342e7abd5SMateusz Guzik 91442e7abd5SMateusz Guzik static void 91542e7abd5SMateusz Guzik rms_int_influx_enter(struct rmslock *rms, struct rmslock_pcpu *pcpu) 91642e7abd5SMateusz Guzik { 91742e7abd5SMateusz Guzik 91842e7abd5SMateusz Guzik CRITICAL_ASSERT(curthread); 91942e7abd5SMateusz Guzik MPASS(pcpu->influx == 0); 92042e7abd5SMateusz Guzik pcpu->influx = 1; 92142e7abd5SMateusz Guzik } 92242e7abd5SMateusz Guzik 92342e7abd5SMateusz Guzik static void 92442e7abd5SMateusz Guzik rms_int_influx_exit(struct rmslock *rms, struct rmslock_pcpu *pcpu) 92542e7abd5SMateusz Guzik { 92642e7abd5SMateusz Guzik 92742e7abd5SMateusz Guzik CRITICAL_ASSERT(curthread); 92842e7abd5SMateusz Guzik MPASS(pcpu->influx == 1); 92942e7abd5SMateusz Guzik pcpu->influx = 0; 93042e7abd5SMateusz Guzik } 93142e7abd5SMateusz Guzik 93242e7abd5SMateusz Guzik #ifdef INVARIANTS 93342e7abd5SMateusz Guzik static void 93442e7abd5SMateusz Guzik rms_int_debug_readers_inc(struct rmslock *rms) 93542e7abd5SMateusz Guzik { 93642e7abd5SMateusz Guzik int old; 93742e7abd5SMateusz Guzik old = atomic_fetchadd_int(&rms->debug_readers, 1); 93842e7abd5SMateusz Guzik KASSERT(old >= 0, ("%s: bad readers count %d\n", __func__, old)); 93942e7abd5SMateusz Guzik } 94042e7abd5SMateusz Guzik 94142e7abd5SMateusz Guzik static void 94242e7abd5SMateusz Guzik rms_int_debug_readers_dec(struct rmslock *rms) 94342e7abd5SMateusz Guzik { 94442e7abd5SMateusz Guzik int old; 94542e7abd5SMateusz Guzik 94642e7abd5SMateusz Guzik old = atomic_fetchadd_int(&rms->debug_readers, -1); 94742e7abd5SMateusz Guzik KASSERT(old > 0, ("%s: bad readers count %d\n", __func__, old)); 94842e7abd5SMateusz Guzik } 94942e7abd5SMateusz Guzik #else 95042e7abd5SMateusz Guzik static void 95142e7abd5SMateusz Guzik rms_int_debug_readers_inc(struct rmslock *rms) 95242e7abd5SMateusz Guzik { 95342e7abd5SMateusz Guzik } 95442e7abd5SMateusz Guzik 95542e7abd5SMateusz Guzik static void 95642e7abd5SMateusz Guzik rms_int_debug_readers_dec(struct rmslock *rms) 95742e7abd5SMateusz Guzik { 95842e7abd5SMateusz Guzik } 95942e7abd5SMateusz Guzik #endif 96042e7abd5SMateusz Guzik 96142e7abd5SMateusz Guzik static void 96242e7abd5SMateusz Guzik rms_int_readers_inc(struct rmslock *rms, struct rmslock_pcpu *pcpu) 96342e7abd5SMateusz Guzik { 96442e7abd5SMateusz Guzik 96542e7abd5SMateusz Guzik CRITICAL_ASSERT(curthread); 96642e7abd5SMateusz Guzik rms_int_debug_readers_inc(rms); 96742e7abd5SMateusz Guzik pcpu->readers++; 96842e7abd5SMateusz Guzik } 96942e7abd5SMateusz Guzik 97042e7abd5SMateusz Guzik static void 97142e7abd5SMateusz Guzik rms_int_readers_dec(struct rmslock *rms, struct rmslock_pcpu *pcpu) 97242e7abd5SMateusz Guzik { 97342e7abd5SMateusz Guzik 97442e7abd5SMateusz Guzik CRITICAL_ASSERT(curthread); 97542e7abd5SMateusz Guzik rms_int_debug_readers_dec(rms); 97642e7abd5SMateusz Guzik pcpu->readers--; 97742e7abd5SMateusz Guzik } 97842e7abd5SMateusz Guzik 97942e7abd5SMateusz Guzik /* 98042e7abd5SMateusz Guzik * Public API 98142e7abd5SMateusz Guzik */ 9821f162fefSMateusz Guzik void 9831f162fefSMateusz Guzik rms_init(struct rmslock *rms, const char *name) 9841f162fefSMateusz Guzik { 9851f162fefSMateusz Guzik 9866fc2b069SMateusz Guzik rms->owner = RMS_NOOWNER; 9871f162fefSMateusz Guzik rms->writers = 0; 9881f162fefSMateusz Guzik rms->readers = 0; 98942e7abd5SMateusz Guzik rms->debug_readers = 0; 9901f162fefSMateusz Guzik mtx_init(&rms->mtx, name, NULL, MTX_DEF | MTX_NEW); 99142e7abd5SMateusz Guzik rms->pcpu = uma_zalloc_pcpu(pcpu_zone_8, M_WAITOK | M_ZERO); 9921f162fefSMateusz Guzik } 9931f162fefSMateusz Guzik 9941f162fefSMateusz Guzik void 9951f162fefSMateusz Guzik rms_destroy(struct rmslock *rms) 9961f162fefSMateusz Guzik { 9971f162fefSMateusz Guzik 9981f162fefSMateusz Guzik MPASS(rms->writers == 0); 9991f162fefSMateusz Guzik MPASS(rms->readers == 0); 10001f162fefSMateusz Guzik mtx_destroy(&rms->mtx); 100142e7abd5SMateusz Guzik uma_zfree_pcpu(pcpu_zone_8, rms->pcpu); 10021f162fefSMateusz Guzik } 10031f162fefSMateusz Guzik 10041f162fefSMateusz Guzik static void __noinline 10051f162fefSMateusz Guzik rms_rlock_fallback(struct rmslock *rms) 10061f162fefSMateusz Guzik { 10071f162fefSMateusz Guzik 100842e7abd5SMateusz Guzik rms_int_influx_exit(rms, rms_int_pcpu(rms)); 10091f162fefSMateusz Guzik critical_exit(); 10101f162fefSMateusz Guzik 10111f162fefSMateusz Guzik mtx_lock(&rms->mtx); 10121f162fefSMateusz Guzik while (rms->writers > 0) 10131f162fefSMateusz Guzik msleep(&rms->readers, &rms->mtx, PUSER - 1, mtx_name(&rms->mtx), 0); 1014ea77ce6eSMateusz Guzik critical_enter(); 101542e7abd5SMateusz Guzik rms_int_readers_inc(rms, rms_int_pcpu(rms)); 10161f162fefSMateusz Guzik mtx_unlock(&rms->mtx); 1017ea77ce6eSMateusz Guzik critical_exit(); 10181f162fefSMateusz Guzik } 10191f162fefSMateusz Guzik 10201f162fefSMateusz Guzik void 10211f162fefSMateusz Guzik rms_rlock(struct rmslock *rms) 10221f162fefSMateusz Guzik { 102342e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu; 10241f162fefSMateusz Guzik 10251f162fefSMateusz Guzik WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 10266fc2b069SMateusz Guzik MPASS(atomic_load_ptr(&rms->owner) != curthread); 10271f162fefSMateusz Guzik 10281f162fefSMateusz Guzik critical_enter(); 102942e7abd5SMateusz Guzik pcpu = rms_int_pcpu(rms); 103042e7abd5SMateusz Guzik rms_int_influx_enter(rms, pcpu); 1031*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 10321f162fefSMateusz Guzik if (__predict_false(rms->writers > 0)) { 10331f162fefSMateusz Guzik rms_rlock_fallback(rms); 10341f162fefSMateusz Guzik return; 10351f162fefSMateusz Guzik } 1036*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 103742e7abd5SMateusz Guzik rms_int_readers_inc(rms, pcpu); 1038*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 103942e7abd5SMateusz Guzik rms_int_influx_exit(rms, pcpu); 10401f162fefSMateusz Guzik critical_exit(); 10411f162fefSMateusz Guzik } 10421f162fefSMateusz Guzik 10431a78ac24SMateusz Guzik int 10441a78ac24SMateusz Guzik rms_try_rlock(struct rmslock *rms) 10451a78ac24SMateusz Guzik { 104642e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu; 10471a78ac24SMateusz Guzik 10486fc2b069SMateusz Guzik MPASS(atomic_load_ptr(&rms->owner) != curthread); 10496fc2b069SMateusz Guzik 10501a78ac24SMateusz Guzik critical_enter(); 105142e7abd5SMateusz Guzik pcpu = rms_int_pcpu(rms); 105242e7abd5SMateusz Guzik rms_int_influx_enter(rms, pcpu); 1053*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 10541a78ac24SMateusz Guzik if (__predict_false(rms->writers > 0)) { 105542e7abd5SMateusz Guzik rms_int_influx_exit(rms, pcpu); 10561a78ac24SMateusz Guzik critical_exit(); 10571a78ac24SMateusz Guzik return (0); 10581a78ac24SMateusz Guzik } 1059*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 106042e7abd5SMateusz Guzik rms_int_readers_inc(rms, pcpu); 1061*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 106242e7abd5SMateusz Guzik rms_int_influx_exit(rms, pcpu); 10631a78ac24SMateusz Guzik critical_exit(); 10641a78ac24SMateusz Guzik return (1); 10651a78ac24SMateusz Guzik } 10661a78ac24SMateusz Guzik 10671f162fefSMateusz Guzik static void __noinline 10681f162fefSMateusz Guzik rms_runlock_fallback(struct rmslock *rms) 10691f162fefSMateusz Guzik { 10701f162fefSMateusz Guzik 107142e7abd5SMateusz Guzik rms_int_influx_exit(rms, rms_int_pcpu(rms)); 10721f162fefSMateusz Guzik critical_exit(); 10731f162fefSMateusz Guzik 10741f162fefSMateusz Guzik mtx_lock(&rms->mtx); 10751f162fefSMateusz Guzik MPASS(rms->writers > 0); 10761f162fefSMateusz Guzik MPASS(rms->readers > 0); 107742e7abd5SMateusz Guzik MPASS(rms->debug_readers == rms->readers); 107842e7abd5SMateusz Guzik rms_int_debug_readers_dec(rms); 10791f162fefSMateusz Guzik rms->readers--; 10801f162fefSMateusz Guzik if (rms->readers == 0) 10811f162fefSMateusz Guzik wakeup_one(&rms->writers); 10821f162fefSMateusz Guzik mtx_unlock(&rms->mtx); 10831f162fefSMateusz Guzik } 10841f162fefSMateusz Guzik 10851f162fefSMateusz Guzik void 10861f162fefSMateusz Guzik rms_runlock(struct rmslock *rms) 10871f162fefSMateusz Guzik { 108842e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu; 10891f162fefSMateusz Guzik 10901f162fefSMateusz Guzik critical_enter(); 109142e7abd5SMateusz Guzik pcpu = rms_int_pcpu(rms); 109242e7abd5SMateusz Guzik rms_int_influx_enter(rms, pcpu); 1093*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 10941f162fefSMateusz Guzik if (__predict_false(rms->writers > 0)) { 10951f162fefSMateusz Guzik rms_runlock_fallback(rms); 10961f162fefSMateusz Guzik return; 10971f162fefSMateusz Guzik } 1098*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 109942e7abd5SMateusz Guzik rms_int_readers_dec(rms, pcpu); 1100*b5449c92SKonstantin Belousov atomic_interrupt_fence(); 110142e7abd5SMateusz Guzik rms_int_influx_exit(rms, pcpu); 11021f162fefSMateusz Guzik critical_exit(); 11031f162fefSMateusz Guzik } 11041f162fefSMateusz Guzik 11051f162fefSMateusz Guzik struct rmslock_ipi { 11061f162fefSMateusz Guzik struct rmslock *rms; 110700ac9d26SMateusz Guzik struct smp_rendezvous_cpus_retry_arg srcra; 11081f162fefSMateusz Guzik }; 11091f162fefSMateusz Guzik 11101f162fefSMateusz Guzik static void 111100ac9d26SMateusz Guzik rms_action_func(void *arg) 11121f162fefSMateusz Guzik { 11131f162fefSMateusz Guzik struct rmslock_ipi *rmsipi; 111442e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu; 11151f162fefSMateusz Guzik struct rmslock *rms; 11161f162fefSMateusz Guzik 111700ac9d26SMateusz Guzik rmsipi = __containerof(arg, struct rmslock_ipi, srcra); 11181f162fefSMateusz Guzik rms = rmsipi->rms; 111942e7abd5SMateusz Guzik pcpu = rms_int_pcpu(rms); 11201f162fefSMateusz Guzik 112142e7abd5SMateusz Guzik if (pcpu->influx) 11221f162fefSMateusz Guzik return; 112342e7abd5SMateusz Guzik if (pcpu->readers != 0) { 112442e7abd5SMateusz Guzik atomic_add_int(&rms->readers, pcpu->readers); 112542e7abd5SMateusz Guzik pcpu->readers = 0; 112642e7abd5SMateusz Guzik } 112700ac9d26SMateusz Guzik smp_rendezvous_cpus_done(arg); 112800ac9d26SMateusz Guzik } 112900ac9d26SMateusz Guzik 113000ac9d26SMateusz Guzik static void 113100ac9d26SMateusz Guzik rms_wait_func(void *arg, int cpu) 113200ac9d26SMateusz Guzik { 113300ac9d26SMateusz Guzik struct rmslock_ipi *rmsipi; 113442e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu; 113500ac9d26SMateusz Guzik struct rmslock *rms; 113600ac9d26SMateusz Guzik 113700ac9d26SMateusz Guzik rmsipi = __containerof(arg, struct rmslock_ipi, srcra); 113800ac9d26SMateusz Guzik rms = rmsipi->rms; 113942e7abd5SMateusz Guzik pcpu = rms_int_remote_pcpu(rms, cpu); 114000ac9d26SMateusz Guzik 114142e7abd5SMateusz Guzik while (atomic_load_int(&pcpu->influx)) 114200ac9d26SMateusz Guzik cpu_spinwait(); 11431f162fefSMateusz Guzik } 11441f162fefSMateusz Guzik 114542e7abd5SMateusz Guzik #ifdef INVARIANTS 114642e7abd5SMateusz Guzik static void 114742e7abd5SMateusz Guzik rms_assert_no_pcpu_readers(struct rmslock *rms) 114842e7abd5SMateusz Guzik { 114942e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu; 115042e7abd5SMateusz Guzik int cpu; 115142e7abd5SMateusz Guzik 115242e7abd5SMateusz Guzik CPU_FOREACH(cpu) { 115342e7abd5SMateusz Guzik pcpu = rms_int_remote_pcpu(rms, cpu); 115442e7abd5SMateusz Guzik if (pcpu->readers != 0) { 115542e7abd5SMateusz Guzik panic("%s: got %d readers on cpu %d\n", __func__, 115642e7abd5SMateusz Guzik pcpu->readers, cpu); 115742e7abd5SMateusz Guzik } 115842e7abd5SMateusz Guzik } 115942e7abd5SMateusz Guzik } 116042e7abd5SMateusz Guzik #else 116142e7abd5SMateusz Guzik static void 116242e7abd5SMateusz Guzik rms_assert_no_pcpu_readers(struct rmslock *rms) 116342e7abd5SMateusz Guzik { 116442e7abd5SMateusz Guzik } 116542e7abd5SMateusz Guzik #endif 116642e7abd5SMateusz Guzik 11671f162fefSMateusz Guzik static void 11681f162fefSMateusz Guzik rms_wlock_switch(struct rmslock *rms) 11691f162fefSMateusz Guzik { 11701f162fefSMateusz Guzik struct rmslock_ipi rmsipi; 11711f162fefSMateusz Guzik 11721f162fefSMateusz Guzik MPASS(rms->readers == 0); 11731f162fefSMateusz Guzik MPASS(rms->writers == 1); 11741f162fefSMateusz Guzik 11751f162fefSMateusz Guzik rmsipi.rms = rms; 11761f162fefSMateusz Guzik 117700ac9d26SMateusz Guzik smp_rendezvous_cpus_retry(all_cpus, 11781f162fefSMateusz Guzik smp_no_rendezvous_barrier, 117900ac9d26SMateusz Guzik rms_action_func, 11801f162fefSMateusz Guzik smp_no_rendezvous_barrier, 118100ac9d26SMateusz Guzik rms_wait_func, 118200ac9d26SMateusz Guzik &rmsipi.srcra); 11831f162fefSMateusz Guzik } 11841f162fefSMateusz Guzik 11851f162fefSMateusz Guzik void 11861f162fefSMateusz Guzik rms_wlock(struct rmslock *rms) 11871f162fefSMateusz Guzik { 11881f162fefSMateusz Guzik 11891f162fefSMateusz Guzik WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__); 11906fc2b069SMateusz Guzik MPASS(atomic_load_ptr(&rms->owner) != curthread); 11911f162fefSMateusz Guzik 11921f162fefSMateusz Guzik mtx_lock(&rms->mtx); 11931f162fefSMateusz Guzik rms->writers++; 11941f162fefSMateusz Guzik if (rms->writers > 1) { 11956fc2b069SMateusz Guzik msleep(&rms->owner, &rms->mtx, (PUSER - 1), 11963983dc32SMateusz Guzik mtx_name(&rms->mtx), 0); 11971f162fefSMateusz Guzik MPASS(rms->readers == 0); 11986fc2b069SMateusz Guzik KASSERT(rms->owner == RMS_TRANSIENT, 11996fc2b069SMateusz Guzik ("%s: unexpected owner value %p\n", __func__, 12006fc2b069SMateusz Guzik rms->owner)); 12016fc2b069SMateusz Guzik goto out_grab; 12021f162fefSMateusz Guzik } 12031f162fefSMateusz Guzik 12046fc2b069SMateusz Guzik KASSERT(rms->owner == RMS_NOOWNER, 12056fc2b069SMateusz Guzik ("%s: unexpected owner value %p\n", __func__, rms->owner)); 12066fc2b069SMateusz Guzik 12071f162fefSMateusz Guzik rms_wlock_switch(rms); 120842e7abd5SMateusz Guzik rms_assert_no_pcpu_readers(rms); 12091f162fefSMateusz Guzik 12106fc2b069SMateusz Guzik if (rms->readers > 0) { 12116fc2b069SMateusz Guzik msleep(&rms->writers, &rms->mtx, (PUSER - 1), 12123983dc32SMateusz Guzik mtx_name(&rms->mtx), 0); 12136fc2b069SMateusz Guzik } 12146fc2b069SMateusz Guzik 12156fc2b069SMateusz Guzik out_grab: 12166fc2b069SMateusz Guzik rms->owner = curthread; 121742e7abd5SMateusz Guzik rms_assert_no_pcpu_readers(rms); 12181f162fefSMateusz Guzik mtx_unlock(&rms->mtx); 12191f162fefSMateusz Guzik MPASS(rms->readers == 0); 12201f162fefSMateusz Guzik } 12211f162fefSMateusz Guzik 12221f162fefSMateusz Guzik void 12231f162fefSMateusz Guzik rms_wunlock(struct rmslock *rms) 12241f162fefSMateusz Guzik { 12251f162fefSMateusz Guzik 12261f162fefSMateusz Guzik mtx_lock(&rms->mtx); 12276fc2b069SMateusz Guzik KASSERT(rms->owner == curthread, 12286fc2b069SMateusz Guzik ("%s: unexpected owner value %p\n", __func__, rms->owner)); 12291f162fefSMateusz Guzik MPASS(rms->writers >= 1); 12301f162fefSMateusz Guzik MPASS(rms->readers == 0); 12311f162fefSMateusz Guzik rms->writers--; 12326fc2b069SMateusz Guzik if (rms->writers > 0) { 12336fc2b069SMateusz Guzik wakeup_one(&rms->owner); 12346fc2b069SMateusz Guzik rms->owner = RMS_TRANSIENT; 12356fc2b069SMateusz Guzik } else { 12361f162fefSMateusz Guzik wakeup(&rms->readers); 12376fc2b069SMateusz Guzik rms->owner = RMS_NOOWNER; 12386fc2b069SMateusz Guzik } 12391f162fefSMateusz Guzik mtx_unlock(&rms->mtx); 12401f162fefSMateusz Guzik } 12416fc2b069SMateusz Guzik 12426fc2b069SMateusz Guzik void 12436fc2b069SMateusz Guzik rms_unlock(struct rmslock *rms) 12446fc2b069SMateusz Guzik { 12456fc2b069SMateusz Guzik 12466fc2b069SMateusz Guzik if (rms_wowned(rms)) 12476fc2b069SMateusz Guzik rms_wunlock(rms); 12486fc2b069SMateusz Guzik else 12496fc2b069SMateusz Guzik rms_runlock(rms); 12506fc2b069SMateusz Guzik } 1251