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 #include "opt_ddb.h"
38f53d15feSStephan Uphoff
39f53d15feSStephan Uphoff #include <sys/param.h>
40f53d15feSStephan Uphoff #include <sys/systm.h>
41f53d15feSStephan Uphoff
42f53d15feSStephan Uphoff #include <sys/kernel.h>
43cd2fe4e6SAttilio Rao #include <sys/kdb.h>
44f53d15feSStephan Uphoff #include <sys/ktr.h>
45f53d15feSStephan Uphoff #include <sys/lock.h>
46f53d15feSStephan Uphoff #include <sys/mutex.h>
47f53d15feSStephan Uphoff #include <sys/proc.h>
48f53d15feSStephan Uphoff #include <sys/rmlock.h>
49f53d15feSStephan Uphoff #include <sys/sched.h>
50f53d15feSStephan Uphoff #include <sys/smp.h>
51f53d15feSStephan Uphoff #include <sys/turnstile.h>
52f53d15feSStephan Uphoff #include <sys/lock_profile.h>
53f53d15feSStephan Uphoff #include <machine/cpu.h>
541f162fefSMateusz Guzik #include <vm/uma.h>
55f53d15feSStephan Uphoff
56f53d15feSStephan Uphoff #ifdef DDB
57f53d15feSStephan Uphoff #include <ddb/ddb.h>
58f53d15feSStephan Uphoff #endif
59f53d15feSStephan Uphoff
60cd32bd7aSJohn Baldwin /*
61cd32bd7aSJohn Baldwin * A cookie to mark destroyed rmlocks. This is stored in the head of
62cd32bd7aSJohn Baldwin * rm_activeReaders.
63cd32bd7aSJohn Baldwin */
64cd32bd7aSJohn Baldwin #define RM_DESTROYED ((void *)0xdead)
65cd32bd7aSJohn Baldwin
66cd32bd7aSJohn Baldwin #define rm_destroyed(rm) \
67cd32bd7aSJohn Baldwin (LIST_FIRST(&(rm)->rm_activeReaders) == RM_DESTROYED)
68cd32bd7aSJohn Baldwin
69f53d15feSStephan Uphoff #define RMPF_ONQUEUE 1
70f53d15feSStephan Uphoff #define RMPF_SIGNAL 2
71f53d15feSStephan Uphoff
72cd32bd7aSJohn Baldwin #ifndef INVARIANTS
73cd32bd7aSJohn Baldwin #define _rm_assert(c, what, file, line)
74cd32bd7aSJohn Baldwin #endif
75f53d15feSStephan Uphoff
76d576deedSPawel Jakub Dawidek static void assert_rm(const struct lock_object *lock, int what);
77cd32bd7aSJohn Baldwin #ifdef DDB
78cd32bd7aSJohn Baldwin static void db_show_rm(const struct lock_object *lock);
79cd32bd7aSJohn Baldwin #endif
807faf4d90SDavide Italiano static void lock_rm(struct lock_object *lock, uintptr_t how);
81a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
82d576deedSPawel Jakub Dawidek static int owner_rm(const struct lock_object *lock, struct thread **owner);
83a5aedd68SStacey Son #endif
847faf4d90SDavide Italiano static uintptr_t unlock_rm(struct lock_object *lock);
85f53d15feSStephan Uphoff
86f53d15feSStephan Uphoff struct lock_class lock_class_rm = {
87f53d15feSStephan Uphoff .lc_name = "rm",
88f53d15feSStephan Uphoff .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
89f9721b43SAttilio Rao .lc_assert = assert_rm,
90f53d15feSStephan Uphoff #ifdef DDB
91cd32bd7aSJohn Baldwin .lc_ddb_show = db_show_rm,
92f53d15feSStephan Uphoff #endif
93cd32bd7aSJohn Baldwin .lc_lock = lock_rm,
94cd32bd7aSJohn Baldwin .lc_unlock = unlock_rm,
95cd32bd7aSJohn Baldwin #ifdef KDTRACE_HOOKS
96cd32bd7aSJohn Baldwin .lc_owner = owner_rm,
97cd32bd7aSJohn Baldwin #endif
98cd32bd7aSJohn Baldwin };
99cd32bd7aSJohn Baldwin
100cd32bd7aSJohn Baldwin struct lock_class lock_class_rm_sleepable = {
101cd32bd7aSJohn Baldwin .lc_name = "sleepable rm",
102cd32bd7aSJohn Baldwin .lc_flags = LC_SLEEPLOCK | LC_SLEEPABLE | LC_RECURSABLE,
103cd32bd7aSJohn Baldwin .lc_assert = assert_rm,
104cd32bd7aSJohn Baldwin #ifdef DDB
105cd32bd7aSJohn Baldwin .lc_ddb_show = db_show_rm,
106f53d15feSStephan Uphoff #endif
107f53d15feSStephan Uphoff .lc_lock = lock_rm,
108f53d15feSStephan Uphoff .lc_unlock = unlock_rm,
109a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
110a5aedd68SStacey Son .lc_owner = owner_rm,
111a5aedd68SStacey Son #endif
112f53d15feSStephan Uphoff };
113f53d15feSStephan Uphoff
114f53d15feSStephan Uphoff static void
assert_rm(const struct lock_object * lock,int what)115d576deedSPawel Jakub Dawidek assert_rm(const struct lock_object *lock, int what)
116f9721b43SAttilio Rao {
117f9721b43SAttilio Rao
118cd32bd7aSJohn Baldwin rm_assert((const struct rmlock *)lock, what);
119f9721b43SAttilio Rao }
120f9721b43SAttilio Rao
121f9721b43SAttilio Rao static void
lock_rm(struct lock_object * lock,uintptr_t how)1227faf4d90SDavide Italiano lock_rm(struct lock_object *lock, uintptr_t how)
123d02add54SRobert Watson {
124cd32bd7aSJohn Baldwin struct rmlock *rm;
1257faf4d90SDavide Italiano struct rm_priotracker *tracker;
126d02add54SRobert Watson
127cd32bd7aSJohn Baldwin rm = (struct rmlock *)lock;
1287faf4d90SDavide Italiano if (how == 0)
129cd32bd7aSJohn Baldwin rm_wlock(rm);
1307faf4d90SDavide Italiano else {
1317faf4d90SDavide Italiano tracker = (struct rm_priotracker *)how;
1327faf4d90SDavide Italiano rm_rlock(rm, tracker);
1337faf4d90SDavide Italiano }
134f53d15feSStephan Uphoff }
135f53d15feSStephan Uphoff
1367faf4d90SDavide Italiano static uintptr_t
unlock_rm(struct lock_object * lock)137d02add54SRobert Watson unlock_rm(struct lock_object *lock)
138d02add54SRobert Watson {
1397faf4d90SDavide Italiano struct thread *td;
1407faf4d90SDavide Italiano struct pcpu *pc;
141cd32bd7aSJohn Baldwin struct rmlock *rm;
1427faf4d90SDavide Italiano struct rm_queue *queue;
1437faf4d90SDavide Italiano struct rm_priotracker *tracker;
1447faf4d90SDavide Italiano uintptr_t how;
145d02add54SRobert Watson
146cd32bd7aSJohn Baldwin rm = (struct rmlock *)lock;
1477faf4d90SDavide Italiano tracker = NULL;
1487faf4d90SDavide Italiano how = 0;
1497faf4d90SDavide Italiano rm_assert(rm, RA_LOCKED | RA_NOTRECURSED);
1507faf4d90SDavide Italiano if (rm_wowned(rm))
151cd32bd7aSJohn Baldwin rm_wunlock(rm);
1527faf4d90SDavide Italiano else {
1537faf4d90SDavide Italiano /*
1547faf4d90SDavide Italiano * Find the right rm_priotracker structure for curthread.
1557faf4d90SDavide Italiano * The guarantee about its uniqueness is given by the fact
1567faf4d90SDavide Italiano * we already asserted the lock wasn't recursively acquired.
1577faf4d90SDavide Italiano */
1587faf4d90SDavide Italiano critical_enter();
1597faf4d90SDavide Italiano td = curthread;
160e2a8d178SJason A. Harmening pc = get_pcpu();
1617faf4d90SDavide Italiano for (queue = pc->pc_rm_queue.rmq_next;
1627faf4d90SDavide Italiano queue != &pc->pc_rm_queue; queue = queue->rmq_next) {
1637faf4d90SDavide Italiano tracker = (struct rm_priotracker *)queue;
1647faf4d90SDavide Italiano if ((tracker->rmp_rmlock == rm) &&
1657faf4d90SDavide Italiano (tracker->rmp_thread == td)) {
1667faf4d90SDavide Italiano how = (uintptr_t)tracker;
1677faf4d90SDavide Italiano break;
1687faf4d90SDavide Italiano }
1697faf4d90SDavide Italiano }
1707faf4d90SDavide Italiano KASSERT(tracker != NULL,
1717faf4d90SDavide Italiano ("rm_priotracker is non-NULL when lock held in read mode"));
1727faf4d90SDavide Italiano critical_exit();
1737faf4d90SDavide Italiano rm_runlock(rm, tracker);
1747faf4d90SDavide Italiano }
1757faf4d90SDavide Italiano return (how);
176f53d15feSStephan Uphoff }
177f53d15feSStephan Uphoff
178a5aedd68SStacey Son #ifdef KDTRACE_HOOKS
179a5aedd68SStacey Son static int
owner_rm(const struct lock_object * lock,struct thread ** owner)180d576deedSPawel Jakub Dawidek owner_rm(const struct lock_object *lock, struct thread **owner)
181a5aedd68SStacey Son {
182cd32bd7aSJohn Baldwin const struct rmlock *rm;
183cd32bd7aSJohn Baldwin struct lock_class *lc;
184a5aedd68SStacey Son
185cd32bd7aSJohn Baldwin rm = (const struct rmlock *)lock;
186cd32bd7aSJohn Baldwin lc = LOCK_CLASS(&rm->rm_wlock_object);
187cd32bd7aSJohn Baldwin return (lc->lc_owner(&rm->rm_wlock_object, owner));
188a5aedd68SStacey Son }
189a5aedd68SStacey Son #endif
190a5aedd68SStacey Son
191f53d15feSStephan Uphoff static struct mtx rm_spinlock;
192f53d15feSStephan Uphoff
193f53d15feSStephan Uphoff MTX_SYSINIT(rm_spinlock, &rm_spinlock, "rm_spinlock", MTX_SPIN);
194f53d15feSStephan Uphoff
195f53d15feSStephan Uphoff /*
196c7ca33d1SRobert Watson * Add or remove tracker from per-cpu list.
197d02add54SRobert Watson *
198c7ca33d1SRobert Watson * The per-cpu list can be traversed at any time in forward direction from an
199d02add54SRobert Watson * interrupt on the *local* cpu.
200f53d15feSStephan Uphoff */
201f53d15feSStephan Uphoff static void inline
rm_tracker_add(struct pcpu * pc,struct rm_priotracker * tracker)202d02add54SRobert Watson rm_tracker_add(struct pcpu *pc, struct rm_priotracker *tracker)
203d02add54SRobert Watson {
204f53d15feSStephan Uphoff struct rm_queue *next;
205d02add54SRobert Watson
206f53d15feSStephan Uphoff /* Initialize all tracker pointers */
207f53d15feSStephan Uphoff tracker->rmp_cpuQueue.rmq_prev = &pc->pc_rm_queue;
208f53d15feSStephan Uphoff next = pc->pc_rm_queue.rmq_next;
209f53d15feSStephan Uphoff tracker->rmp_cpuQueue.rmq_next = next;
210d02add54SRobert Watson
211d02add54SRobert Watson /* rmq_prev is not used during froward traversal. */
212f53d15feSStephan Uphoff next->rmq_prev = &tracker->rmp_cpuQueue;
213d02add54SRobert Watson
214d02add54SRobert Watson /* Update pointer to first element. */
215f53d15feSStephan Uphoff pc->pc_rm_queue.rmq_next = &tracker->rmp_cpuQueue;
216f53d15feSStephan Uphoff }
217f53d15feSStephan Uphoff
218cd32bd7aSJohn Baldwin /*
219cd32bd7aSJohn Baldwin * Return a count of the number of trackers the thread 'td' already
220cd32bd7aSJohn Baldwin * has on this CPU for the lock 'rm'.
221cd32bd7aSJohn Baldwin */
222cd32bd7aSJohn Baldwin static int
rm_trackers_present(const struct pcpu * pc,const struct rmlock * rm,const struct thread * td)223cd32bd7aSJohn Baldwin rm_trackers_present(const struct pcpu *pc, const struct rmlock *rm,
224cd32bd7aSJohn Baldwin const struct thread *td)
225cd32bd7aSJohn Baldwin {
226cd32bd7aSJohn Baldwin struct rm_queue *queue;
227cd32bd7aSJohn Baldwin struct rm_priotracker *tracker;
228cd32bd7aSJohn Baldwin int count;
229cd32bd7aSJohn Baldwin
230cd32bd7aSJohn Baldwin count = 0;
231cd32bd7aSJohn Baldwin for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
232cd32bd7aSJohn Baldwin queue = queue->rmq_next) {
233cd32bd7aSJohn Baldwin tracker = (struct rm_priotracker *)queue;
234cd32bd7aSJohn Baldwin if ((tracker->rmp_rmlock == rm) && (tracker->rmp_thread == td))
235cd32bd7aSJohn Baldwin count++;
236cd32bd7aSJohn Baldwin }
237cd32bd7aSJohn Baldwin return (count);
238cd32bd7aSJohn Baldwin }
239cd32bd7aSJohn Baldwin
240f53d15feSStephan Uphoff static void inline
rm_tracker_remove(struct pcpu * pc,struct rm_priotracker * tracker)241d02add54SRobert Watson rm_tracker_remove(struct pcpu *pc, struct rm_priotracker *tracker)
242d02add54SRobert Watson {
243f53d15feSStephan Uphoff struct rm_queue *next, *prev;
244d02add54SRobert Watson
245f53d15feSStephan Uphoff next = tracker->rmp_cpuQueue.rmq_next;
246f53d15feSStephan Uphoff prev = tracker->rmp_cpuQueue.rmq_prev;
247d02add54SRobert Watson
248d02add54SRobert Watson /* Not used during forward traversal. */
249f53d15feSStephan Uphoff next->rmq_prev = prev;
250d02add54SRobert Watson
251d02add54SRobert Watson /* Remove from list. */
252f53d15feSStephan Uphoff prev->rmq_next = next;
253f53d15feSStephan Uphoff }
254f53d15feSStephan Uphoff
255d02add54SRobert Watson static void
rm_cleanIPI(void * arg)256d02add54SRobert Watson rm_cleanIPI(void *arg)
257d02add54SRobert Watson {
258f53d15feSStephan Uphoff struct pcpu *pc;
259f53d15feSStephan Uphoff struct rmlock *rm = arg;
260f53d15feSStephan Uphoff struct rm_priotracker *tracker;
261f53d15feSStephan Uphoff struct rm_queue *queue;
262e2a8d178SJason A. Harmening pc = get_pcpu();
263f53d15feSStephan Uphoff
264d02add54SRobert Watson for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
265f53d15feSStephan Uphoff queue = queue->rmq_next) {
266f53d15feSStephan Uphoff tracker = (struct rm_priotracker *)queue;
267f53d15feSStephan Uphoff if (tracker->rmp_rmlock == rm && tracker->rmp_flags == 0) {
268f53d15feSStephan Uphoff tracker->rmp_flags = RMPF_ONQUEUE;
269f53d15feSStephan Uphoff mtx_lock_spin(&rm_spinlock);
270f53d15feSStephan Uphoff LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
271f53d15feSStephan Uphoff rmp_qentry);
272f53d15feSStephan Uphoff mtx_unlock_spin(&rm_spinlock);
273f53d15feSStephan Uphoff }
274f53d15feSStephan Uphoff }
275f53d15feSStephan Uphoff }
276f53d15feSStephan Uphoff
277f53d15feSStephan Uphoff void
rm_init_flags(struct rmlock * rm,const char * name,int opts)2781a109c1cSRobert Watson rm_init_flags(struct rmlock *rm, const char *name, int opts)
279f53d15feSStephan Uphoff {
280cd32bd7aSJohn Baldwin struct lock_class *lc;
281fd07ddcfSDmitry Chagin int liflags, xflags;
282d02add54SRobert Watson
2831a109c1cSRobert Watson liflags = 0;
2841a109c1cSRobert Watson if (!(opts & RM_NOWITNESS))
2851a109c1cSRobert Watson liflags |= LO_WITNESS;
2861a109c1cSRobert Watson if (opts & RM_RECURSE)
2871a109c1cSRobert Watson liflags |= LO_RECURSABLE;
288fd07ddcfSDmitry Chagin if (opts & RM_NEW)
289fd07ddcfSDmitry Chagin liflags |= LO_NEW;
2902816bd84SMitchell Horne if (opts & RM_DUPOK)
2912816bd84SMitchell Horne liflags |= LO_DUPOK;
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
rm_init(struct rmlock * rm,const char * name)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
rm_destroy(struct rmlock * rm)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
rm_wowned(const struct rmlock * rm)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
rm_sysinit(void * arg)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
_rm_rlock_hard(struct rmlock * rm,struct rm_priotracker * tracker,int trylock)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 */
369b5449c92SKonstantin 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
_rm_rlock(struct rmlock * rm,struct rm_priotracker * tracker,int trylock)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(); */
450b5449c92SKonstantin Belousov atomic_interrupt_fence();
451f53d15feSStephan Uphoff
452afb44cb0SMark Johnston pc = cpuid_to_pcpu[td->td_oncpu];
453f53d15feSStephan Uphoff rm_tracker_add(pc, tracker);
45482b7a39cSRobert Watson sched_pin();
455f53d15feSStephan Uphoff
456b5449c92SKonstantin Belousov atomic_interrupt_fence();
457f53d15feSStephan Uphoff td->td_critnest--;
458f53d15feSStephan Uphoff
459f53d15feSStephan Uphoff /*
460d02add54SRobert Watson * Fast path to combine two common conditions into a single
461d02add54SRobert Watson * conditional jump.
462f53d15feSStephan Uphoff */
46385c1b3c1SMateusz Guzik if (__predict_true(0 == (td->td_owepreempt |
46485c1b3c1SMateusz Guzik CPU_ISSET(pc->pc_cpuid, &rm->rm_writecpus))))
46536058c09SMax Laier return (1);
466f53d15feSStephan Uphoff
467d02add54SRobert Watson /* We do not have a read token and need to acquire one. */
46836058c09SMax Laier return _rm_rlock_hard(rm, tracker, trylock);
469f53d15feSStephan Uphoff }
470f53d15feSStephan Uphoff
47185c1b3c1SMateusz Guzik static __noinline void
_rm_unlock_hard(struct thread * td,struct rm_priotracker * tracker)472f53d15feSStephan Uphoff _rm_unlock_hard(struct thread *td,struct rm_priotracker *tracker)
473f53d15feSStephan Uphoff {
474f53d15feSStephan Uphoff
475f53d15feSStephan Uphoff if (td->td_owepreempt) {
476f53d15feSStephan Uphoff td->td_critnest++;
477f53d15feSStephan Uphoff critical_exit();
478f53d15feSStephan Uphoff }
479f53d15feSStephan Uphoff
480d02add54SRobert Watson if (!tracker->rmp_flags)
481f53d15feSStephan Uphoff return;
482f53d15feSStephan Uphoff
483f53d15feSStephan Uphoff mtx_lock_spin(&rm_spinlock);
484f53d15feSStephan Uphoff LIST_REMOVE(tracker, rmp_qentry);
485f53d15feSStephan Uphoff
486f53d15feSStephan Uphoff if (tracker->rmp_flags & RMPF_SIGNAL) {
487f53d15feSStephan Uphoff struct rmlock *rm;
488f53d15feSStephan Uphoff struct turnstile *ts;
489f53d15feSStephan Uphoff
490f53d15feSStephan Uphoff rm = tracker->rmp_rmlock;
491f53d15feSStephan Uphoff
492f53d15feSStephan Uphoff turnstile_chain_lock(&rm->lock_object);
493f53d15feSStephan Uphoff mtx_unlock_spin(&rm_spinlock);
494f53d15feSStephan Uphoff
495f53d15feSStephan Uphoff ts = turnstile_lookup(&rm->lock_object);
496f53d15feSStephan Uphoff
497f53d15feSStephan Uphoff turnstile_signal(ts, TS_EXCLUSIVE_QUEUE);
498d0a22279SMateusz Guzik turnstile_unpend(ts);
499f53d15feSStephan Uphoff turnstile_chain_unlock(&rm->lock_object);
500f53d15feSStephan Uphoff } else
501f53d15feSStephan Uphoff mtx_unlock_spin(&rm_spinlock);
502f53d15feSStephan Uphoff }
503f53d15feSStephan Uphoff
504f53d15feSStephan Uphoff void
_rm_runlock(struct rmlock * rm,struct rm_priotracker * tracker)505f53d15feSStephan Uphoff _rm_runlock(struct rmlock *rm, struct rm_priotracker *tracker)
506f53d15feSStephan Uphoff {
507f53d15feSStephan Uphoff struct pcpu *pc;
508f53d15feSStephan Uphoff struct thread *td = tracker->rmp_thread;
509f53d15feSStephan Uphoff
51035370593SAndriy Gapon if (SCHEDULER_STOPPED())
51135370593SAndriy Gapon return;
51235370593SAndriy Gapon
513f53d15feSStephan Uphoff td->td_critnest++; /* critical_enter(); */
51489ae8eb7SMark Johnston atomic_interrupt_fence();
51589ae8eb7SMark Johnston
516afb44cb0SMark Johnston pc = cpuid_to_pcpu[td->td_oncpu];
517f53d15feSStephan Uphoff rm_tracker_remove(pc, tracker);
51889ae8eb7SMark Johnston
51989ae8eb7SMark Johnston atomic_interrupt_fence();
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
_rm_wlock(struct rmlock * rm)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 */
549a19bd8e3SStefan Eßer readcpus = all_cpus;
550a19bd8e3SStefan Eßer CPU_ANDNOT(&readcpus, &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
_rm_wunlock(struct rmlock * rm)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
_rm_wlock_debug(struct rmlock * rm,const char * file,int line)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
_rm_wunlock_debug(struct rmlock * rm,const char * file,int line)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
_rm_rlock_debug(struct rmlock * rm,struct rm_priotracker * tracker,int trylock,const char * file,int line)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
_rm_runlock_debug(struct rmlock * rm,struct rm_priotracker * tracker,const char * file,int line)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
_rm_wlock_debug(struct rmlock * rm,const char * file,int line)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
_rm_wunlock_debug(struct rmlock * rm,const char * file,int line)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
_rm_rlock_debug(struct rmlock * rm,struct rm_priotracker * tracker,int trylock,const char * file,int line)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
_rm_runlock_debug(struct rmlock * rm,struct rm_priotracker * tracker,const char * file,int line)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
_rm_assert(const struct rmlock * rm,int what,const char * file,int line)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
print_tracker(struct rm_priotracker * tr)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
db_show_rm(const struct lock_object * lock)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 *
876b5449c92SKonstantin Belousov * Neither rms_rlock nor rms_runlock use thread fences. Instead interrupt
877b5449c92SKonstantin Belousov * fences are inserted to ensure ordering with the code executed in the IPI
878b5449c92SKonstantin 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 *
rms_int_pcpu(struct rmslock * rms)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 *
rms_int_remote_pcpu(struct rmslock * rms,int cpu)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
rms_int_influx_enter(struct rmslock * rms,struct rmslock_pcpu * pcpu)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
rms_int_influx_exit(struct rmslock * rms,struct rmslock_pcpu * pcpu)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
rms_int_debug_readers_inc(struct rmslock * rms)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
rms_int_debug_readers_dec(struct rmslock * rms)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
rms_int_debug_readers_inc(struct rmslock * rms)95142e7abd5SMateusz Guzik rms_int_debug_readers_inc(struct rmslock *rms)
95242e7abd5SMateusz Guzik {
95342e7abd5SMateusz Guzik }
95442e7abd5SMateusz Guzik
95542e7abd5SMateusz Guzik static void
rms_int_debug_readers_dec(struct rmslock * rms)95642e7abd5SMateusz Guzik rms_int_debug_readers_dec(struct rmslock *rms)
95742e7abd5SMateusz Guzik {
95842e7abd5SMateusz Guzik }
95942e7abd5SMateusz Guzik #endif
96042e7abd5SMateusz Guzik
96142e7abd5SMateusz Guzik static void
rms_int_readers_inc(struct rmslock * rms,struct rmslock_pcpu * pcpu)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
rms_int_readers_dec(struct rmslock * rms,struct rmslock_pcpu * pcpu)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
rms_init(struct rmslock * rms,const char * name)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
rms_destroy(struct rmslock * rms)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
rms_rlock_fallback(struct rmslock * rms)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();
101871f31d78SMark Johnston TD_LOCKS_INC(curthread);
10191f162fefSMateusz Guzik }
10201f162fefSMateusz Guzik
10211f162fefSMateusz Guzik void
rms_rlock(struct rmslock * rms)10221f162fefSMateusz Guzik rms_rlock(struct rmslock *rms)
10231f162fefSMateusz Guzik {
102442e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu;
10251f162fefSMateusz Guzik
1026*94882626SMateusz Guzik rms_assert_rlock_ok(rms);
10276fc2b069SMateusz Guzik MPASS(atomic_load_ptr(&rms->owner) != curthread);
10281f162fefSMateusz Guzik
10291f162fefSMateusz Guzik critical_enter();
103042e7abd5SMateusz Guzik pcpu = rms_int_pcpu(rms);
103142e7abd5SMateusz Guzik rms_int_influx_enter(rms, pcpu);
1032b5449c92SKonstantin Belousov atomic_interrupt_fence();
10331f162fefSMateusz Guzik if (__predict_false(rms->writers > 0)) {
10341f162fefSMateusz Guzik rms_rlock_fallback(rms);
10351f162fefSMateusz Guzik return;
10361f162fefSMateusz Guzik }
1037b5449c92SKonstantin Belousov atomic_interrupt_fence();
103842e7abd5SMateusz Guzik rms_int_readers_inc(rms, pcpu);
1039b5449c92SKonstantin Belousov atomic_interrupt_fence();
104042e7abd5SMateusz Guzik rms_int_influx_exit(rms, pcpu);
10411f162fefSMateusz Guzik critical_exit();
104271f31d78SMark Johnston TD_LOCKS_INC(curthread);
10431f162fefSMateusz Guzik }
10441f162fefSMateusz Guzik
10451a78ac24SMateusz Guzik int
rms_try_rlock(struct rmslock * rms)10461a78ac24SMateusz Guzik rms_try_rlock(struct rmslock *rms)
10471a78ac24SMateusz Guzik {
104842e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu;
10491a78ac24SMateusz Guzik
10506fc2b069SMateusz Guzik MPASS(atomic_load_ptr(&rms->owner) != curthread);
10516fc2b069SMateusz Guzik
10521a78ac24SMateusz Guzik critical_enter();
105342e7abd5SMateusz Guzik pcpu = rms_int_pcpu(rms);
105442e7abd5SMateusz Guzik rms_int_influx_enter(rms, pcpu);
1055b5449c92SKonstantin Belousov atomic_interrupt_fence();
10561a78ac24SMateusz Guzik if (__predict_false(rms->writers > 0)) {
105742e7abd5SMateusz Guzik rms_int_influx_exit(rms, pcpu);
10581a78ac24SMateusz Guzik critical_exit();
10591a78ac24SMateusz Guzik return (0);
10601a78ac24SMateusz Guzik }
1061b5449c92SKonstantin Belousov atomic_interrupt_fence();
106242e7abd5SMateusz Guzik rms_int_readers_inc(rms, pcpu);
1063b5449c92SKonstantin Belousov atomic_interrupt_fence();
106442e7abd5SMateusz Guzik rms_int_influx_exit(rms, pcpu);
10651a78ac24SMateusz Guzik critical_exit();
106671f31d78SMark Johnston TD_LOCKS_INC(curthread);
10671a78ac24SMateusz Guzik return (1);
10681a78ac24SMateusz Guzik }
10691a78ac24SMateusz Guzik
10701f162fefSMateusz Guzik static void __noinline
rms_runlock_fallback(struct rmslock * rms)10711f162fefSMateusz Guzik rms_runlock_fallback(struct rmslock *rms)
10721f162fefSMateusz Guzik {
10731f162fefSMateusz Guzik
107442e7abd5SMateusz Guzik rms_int_influx_exit(rms, rms_int_pcpu(rms));
10751f162fefSMateusz Guzik critical_exit();
10761f162fefSMateusz Guzik
10771f162fefSMateusz Guzik mtx_lock(&rms->mtx);
10781f162fefSMateusz Guzik MPASS(rms->writers > 0);
10791f162fefSMateusz Guzik MPASS(rms->readers > 0);
108042e7abd5SMateusz Guzik MPASS(rms->debug_readers == rms->readers);
108142e7abd5SMateusz Guzik rms_int_debug_readers_dec(rms);
10821f162fefSMateusz Guzik rms->readers--;
10831f162fefSMateusz Guzik if (rms->readers == 0)
10841f162fefSMateusz Guzik wakeup_one(&rms->writers);
10851f162fefSMateusz Guzik mtx_unlock(&rms->mtx);
108671f31d78SMark Johnston TD_LOCKS_DEC(curthread);
10871f162fefSMateusz Guzik }
10881f162fefSMateusz Guzik
10891f162fefSMateusz Guzik void
rms_runlock(struct rmslock * rms)10901f162fefSMateusz Guzik rms_runlock(struct rmslock *rms)
10911f162fefSMateusz Guzik {
109242e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu;
10931f162fefSMateusz Guzik
10941f162fefSMateusz Guzik critical_enter();
109542e7abd5SMateusz Guzik pcpu = rms_int_pcpu(rms);
109642e7abd5SMateusz Guzik rms_int_influx_enter(rms, pcpu);
1097b5449c92SKonstantin Belousov atomic_interrupt_fence();
10981f162fefSMateusz Guzik if (__predict_false(rms->writers > 0)) {
10991f162fefSMateusz Guzik rms_runlock_fallback(rms);
11001f162fefSMateusz Guzik return;
11011f162fefSMateusz Guzik }
1102b5449c92SKonstantin Belousov atomic_interrupt_fence();
110342e7abd5SMateusz Guzik rms_int_readers_dec(rms, pcpu);
1104b5449c92SKonstantin Belousov atomic_interrupt_fence();
110542e7abd5SMateusz Guzik rms_int_influx_exit(rms, pcpu);
11061f162fefSMateusz Guzik critical_exit();
110771f31d78SMark Johnston TD_LOCKS_DEC(curthread);
11081f162fefSMateusz Guzik }
11091f162fefSMateusz Guzik
11101f162fefSMateusz Guzik struct rmslock_ipi {
11111f162fefSMateusz Guzik struct rmslock *rms;
111200ac9d26SMateusz Guzik struct smp_rendezvous_cpus_retry_arg srcra;
11131f162fefSMateusz Guzik };
11141f162fefSMateusz Guzik
11151f162fefSMateusz Guzik static void
rms_action_func(void * arg)111600ac9d26SMateusz Guzik rms_action_func(void *arg)
11171f162fefSMateusz Guzik {
11181f162fefSMateusz Guzik struct rmslock_ipi *rmsipi;
111942e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu;
11201f162fefSMateusz Guzik struct rmslock *rms;
11211f162fefSMateusz Guzik
112200ac9d26SMateusz Guzik rmsipi = __containerof(arg, struct rmslock_ipi, srcra);
11231f162fefSMateusz Guzik rms = rmsipi->rms;
112442e7abd5SMateusz Guzik pcpu = rms_int_pcpu(rms);
11251f162fefSMateusz Guzik
112642e7abd5SMateusz Guzik if (pcpu->influx)
11271f162fefSMateusz Guzik return;
112842e7abd5SMateusz Guzik if (pcpu->readers != 0) {
112942e7abd5SMateusz Guzik atomic_add_int(&rms->readers, pcpu->readers);
113042e7abd5SMateusz Guzik pcpu->readers = 0;
113142e7abd5SMateusz Guzik }
113200ac9d26SMateusz Guzik smp_rendezvous_cpus_done(arg);
113300ac9d26SMateusz Guzik }
113400ac9d26SMateusz Guzik
113500ac9d26SMateusz Guzik static void
rms_wait_func(void * arg,int cpu)113600ac9d26SMateusz Guzik rms_wait_func(void *arg, int cpu)
113700ac9d26SMateusz Guzik {
113800ac9d26SMateusz Guzik struct rmslock_ipi *rmsipi;
113942e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu;
114000ac9d26SMateusz Guzik struct rmslock *rms;
114100ac9d26SMateusz Guzik
114200ac9d26SMateusz Guzik rmsipi = __containerof(arg, struct rmslock_ipi, srcra);
114300ac9d26SMateusz Guzik rms = rmsipi->rms;
114442e7abd5SMateusz Guzik pcpu = rms_int_remote_pcpu(rms, cpu);
114500ac9d26SMateusz Guzik
114642e7abd5SMateusz Guzik while (atomic_load_int(&pcpu->influx))
114700ac9d26SMateusz Guzik cpu_spinwait();
11481f162fefSMateusz Guzik }
11491f162fefSMateusz Guzik
115042e7abd5SMateusz Guzik #ifdef INVARIANTS
115142e7abd5SMateusz Guzik static void
rms_assert_no_pcpu_readers(struct rmslock * rms)115242e7abd5SMateusz Guzik rms_assert_no_pcpu_readers(struct rmslock *rms)
115342e7abd5SMateusz Guzik {
115442e7abd5SMateusz Guzik struct rmslock_pcpu *pcpu;
115542e7abd5SMateusz Guzik int cpu;
115642e7abd5SMateusz Guzik
115742e7abd5SMateusz Guzik CPU_FOREACH(cpu) {
115842e7abd5SMateusz Guzik pcpu = rms_int_remote_pcpu(rms, cpu);
115942e7abd5SMateusz Guzik if (pcpu->readers != 0) {
116042e7abd5SMateusz Guzik panic("%s: got %d readers on cpu %d\n", __func__,
116142e7abd5SMateusz Guzik pcpu->readers, cpu);
116242e7abd5SMateusz Guzik }
116342e7abd5SMateusz Guzik }
116442e7abd5SMateusz Guzik }
116542e7abd5SMateusz Guzik #else
116642e7abd5SMateusz Guzik static void
rms_assert_no_pcpu_readers(struct rmslock * rms)116742e7abd5SMateusz Guzik rms_assert_no_pcpu_readers(struct rmslock *rms)
116842e7abd5SMateusz Guzik {
116942e7abd5SMateusz Guzik }
117042e7abd5SMateusz Guzik #endif
117142e7abd5SMateusz Guzik
11721f162fefSMateusz Guzik static void
rms_wlock_switch(struct rmslock * rms)11731f162fefSMateusz Guzik rms_wlock_switch(struct rmslock *rms)
11741f162fefSMateusz Guzik {
11751f162fefSMateusz Guzik struct rmslock_ipi rmsipi;
11761f162fefSMateusz Guzik
11771f162fefSMateusz Guzik MPASS(rms->readers == 0);
11781f162fefSMateusz Guzik MPASS(rms->writers == 1);
11791f162fefSMateusz Guzik
11801f162fefSMateusz Guzik rmsipi.rms = rms;
11811f162fefSMateusz Guzik
118200ac9d26SMateusz Guzik smp_rendezvous_cpus_retry(all_cpus,
11831f162fefSMateusz Guzik smp_no_rendezvous_barrier,
118400ac9d26SMateusz Guzik rms_action_func,
11851f162fefSMateusz Guzik smp_no_rendezvous_barrier,
118600ac9d26SMateusz Guzik rms_wait_func,
118700ac9d26SMateusz Guzik &rmsipi.srcra);
11881f162fefSMateusz Guzik }
11891f162fefSMateusz Guzik
11901f162fefSMateusz Guzik void
rms_wlock(struct rmslock * rms)11911f162fefSMateusz Guzik rms_wlock(struct rmslock *rms)
11921f162fefSMateusz Guzik {
11931f162fefSMateusz Guzik
11941f162fefSMateusz Guzik WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, __func__);
11956fc2b069SMateusz Guzik MPASS(atomic_load_ptr(&rms->owner) != curthread);
11961f162fefSMateusz Guzik
11971f162fefSMateusz Guzik mtx_lock(&rms->mtx);
11981f162fefSMateusz Guzik rms->writers++;
11991f162fefSMateusz Guzik if (rms->writers > 1) {
12006fc2b069SMateusz Guzik msleep(&rms->owner, &rms->mtx, (PUSER - 1),
12013983dc32SMateusz Guzik mtx_name(&rms->mtx), 0);
12021f162fefSMateusz Guzik MPASS(rms->readers == 0);
12036fc2b069SMateusz Guzik KASSERT(rms->owner == RMS_TRANSIENT,
12046fc2b069SMateusz Guzik ("%s: unexpected owner value %p\n", __func__,
12056fc2b069SMateusz Guzik rms->owner));
12066fc2b069SMateusz Guzik goto out_grab;
12071f162fefSMateusz Guzik }
12081f162fefSMateusz Guzik
12096fc2b069SMateusz Guzik KASSERT(rms->owner == RMS_NOOWNER,
12106fc2b069SMateusz Guzik ("%s: unexpected owner value %p\n", __func__, rms->owner));
12116fc2b069SMateusz Guzik
12121f162fefSMateusz Guzik rms_wlock_switch(rms);
121342e7abd5SMateusz Guzik rms_assert_no_pcpu_readers(rms);
12141f162fefSMateusz Guzik
12156fc2b069SMateusz Guzik if (rms->readers > 0) {
12166fc2b069SMateusz Guzik msleep(&rms->writers, &rms->mtx, (PUSER - 1),
12173983dc32SMateusz Guzik mtx_name(&rms->mtx), 0);
12186fc2b069SMateusz Guzik }
12196fc2b069SMateusz Guzik
12206fc2b069SMateusz Guzik out_grab:
12216fc2b069SMateusz Guzik rms->owner = curthread;
122242e7abd5SMateusz Guzik rms_assert_no_pcpu_readers(rms);
12231f162fefSMateusz Guzik mtx_unlock(&rms->mtx);
12241f162fefSMateusz Guzik MPASS(rms->readers == 0);
122571f31d78SMark Johnston TD_LOCKS_INC(curthread);
12261f162fefSMateusz Guzik }
12271f162fefSMateusz Guzik
12281f162fefSMateusz Guzik void
rms_wunlock(struct rmslock * rms)12291f162fefSMateusz Guzik rms_wunlock(struct rmslock *rms)
12301f162fefSMateusz Guzik {
12311f162fefSMateusz Guzik
12321f162fefSMateusz Guzik mtx_lock(&rms->mtx);
12336fc2b069SMateusz Guzik KASSERT(rms->owner == curthread,
12346fc2b069SMateusz Guzik ("%s: unexpected owner value %p\n", __func__, rms->owner));
12351f162fefSMateusz Guzik MPASS(rms->writers >= 1);
12361f162fefSMateusz Guzik MPASS(rms->readers == 0);
12371f162fefSMateusz Guzik rms->writers--;
12386fc2b069SMateusz Guzik if (rms->writers > 0) {
12396fc2b069SMateusz Guzik wakeup_one(&rms->owner);
12406fc2b069SMateusz Guzik rms->owner = RMS_TRANSIENT;
12416fc2b069SMateusz Guzik } else {
12421f162fefSMateusz Guzik wakeup(&rms->readers);
12436fc2b069SMateusz Guzik rms->owner = RMS_NOOWNER;
12446fc2b069SMateusz Guzik }
12451f162fefSMateusz Guzik mtx_unlock(&rms->mtx);
124671f31d78SMark Johnston TD_LOCKS_DEC(curthread);
12471f162fefSMateusz Guzik }
12486fc2b069SMateusz Guzik
12496fc2b069SMateusz Guzik void
rms_unlock(struct rmslock * rms)12506fc2b069SMateusz Guzik rms_unlock(struct rmslock *rms)
12516fc2b069SMateusz Guzik {
12526fc2b069SMateusz Guzik
12536fc2b069SMateusz Guzik if (rms_wowned(rms))
12546fc2b069SMateusz Guzik rms_wunlock(rms);
12556fc2b069SMateusz Guzik else
12566fc2b069SMateusz Guzik rms_runlock(rms);
12576fc2b069SMateusz Guzik }
1258