2 * Copyright (c) 2007 Stephan Uphoff <ups@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Machine independent bits of reader/writer lock implementation.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_kdtrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/kernel.h>
46 #include <sys/mutex.h>
48 #include <sys/rmlock.h>
49 #include <sys/sched.h>
51 #include <sys/systm.h>
52 #include <sys/turnstile.h>
53 #include <sys/lock_profile.h>
54 #include <machine/cpu.h>
60 #define RMPF_ONQUEUE 1
64 * To support usage of rmlock in CVs and msleep yet another list for the
65 * priority tracker would be needed. Using this lock for cv and msleep also
66 * does not seem very useful
69 static __inline void compiler_memory_barrier(void) {
70 __asm __volatile("":::"memory");
73 static void assert_rm(struct lock_object *lock, int what);
74 static void lock_rm(struct lock_object *lock, int how);
76 static int owner_rm(struct lock_object *lock, struct thread **owner);
78 static int unlock_rm(struct lock_object *lock);
80 struct lock_class lock_class_rm = {
82 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
83 .lc_assert = assert_rm,
86 .lc_ddb_show = db_show_rwlock,
90 .lc_unlock = unlock_rm,
97 assert_rm(struct lock_object *lock, int what)
100 panic("assert_rm called");
104 lock_rm(struct lock_object *lock, int how)
107 panic("lock_rm called");
111 unlock_rm(struct lock_object *lock)
114 panic("unlock_rm called");
119 owner_rm(struct lock_object *lock, struct thread **owner)
122 panic("owner_rm called");
126 static struct mtx rm_spinlock;
128 MTX_SYSINIT(rm_spinlock, &rm_spinlock, "rm_spinlock", MTX_SPIN);
131 * Add or remove tracker from per-cpu list.
133 * The per-cpu list can be traversed at any time in forward direction from an
134 * interrupt on the *local* cpu.
137 rm_tracker_add(struct pcpu *pc, struct rm_priotracker *tracker)
139 struct rm_queue *next;
141 /* Initialize all tracker pointers */
142 tracker->rmp_cpuQueue.rmq_prev = &pc->pc_rm_queue;
143 next = pc->pc_rm_queue.rmq_next;
144 tracker->rmp_cpuQueue.rmq_next = next;
146 /* rmq_prev is not used during froward traversal. */
147 next->rmq_prev = &tracker->rmp_cpuQueue;
149 /* Update pointer to first element. */
150 pc->pc_rm_queue.rmq_next = &tracker->rmp_cpuQueue;
154 rm_tracker_remove(struct pcpu *pc, struct rm_priotracker *tracker)
156 struct rm_queue *next, *prev;
158 next = tracker->rmp_cpuQueue.rmq_next;
159 prev = tracker->rmp_cpuQueue.rmq_prev;
161 /* Not used during forward traversal. */
162 next->rmq_prev = prev;
164 /* Remove from list. */
165 prev->rmq_next = next;
169 rm_cleanIPI(void *arg)
172 struct rmlock *rm = arg;
173 struct rm_priotracker *tracker;
174 struct rm_queue *queue;
175 pc = pcpu_find(curcpu);
177 for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
178 queue = queue->rmq_next) {
179 tracker = (struct rm_priotracker *)queue;
180 if (tracker->rmp_rmlock == rm && tracker->rmp_flags == 0) {
181 tracker->rmp_flags = RMPF_ONQUEUE;
182 mtx_lock_spin(&rm_spinlock);
183 LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
185 mtx_unlock_spin(&rm_spinlock);
190 CTASSERT((RM_SLEEPABLE & LO_CLASSFLAGS) == RM_SLEEPABLE);
193 rm_init_flags(struct rmlock *rm, const char *name, int opts)
198 if (!(opts & RM_NOWITNESS))
199 liflags |= LO_WITNESS;
200 if (opts & RM_RECURSE)
201 liflags |= LO_RECURSABLE;
202 rm->rm_writecpus = all_cpus;
203 LIST_INIT(&rm->rm_activeReaders);
204 if (opts & RM_SLEEPABLE) {
205 liflags |= RM_SLEEPABLE;
206 sx_init_flags(&rm->rm_lock_sx, "rmlock_sx", SX_RECURSE);
208 mtx_init(&rm->rm_lock_mtx, name, "rmlock_mtx", MTX_NOWITNESS);
209 lock_init(&rm->lock_object, &lock_class_rm, name, NULL, liflags);
213 rm_init(struct rmlock *rm, const char *name)
216 rm_init_flags(rm, name, 0);
220 rm_destroy(struct rmlock *rm)
223 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
224 sx_destroy(&rm->rm_lock_sx);
226 mtx_destroy(&rm->rm_lock_mtx);
227 lock_destroy(&rm->lock_object);
231 rm_wowned(struct rmlock *rm)
234 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
235 return (sx_xlocked(&rm->rm_lock_sx));
237 return (mtx_owned(&rm->rm_lock_mtx));
241 rm_sysinit(void *arg)
243 struct rm_args *args = arg;
245 rm_init(args->ra_rm, args->ra_desc);
249 rm_sysinit_flags(void *arg)
251 struct rm_args_flags *args = arg;
253 rm_init_flags(args->ra_rm, args->ra_desc, args->ra_opts);
257 _rm_rlock_hard(struct rmlock *rm, struct rm_priotracker *tracker, int trylock)
260 struct rm_queue *queue;
261 struct rm_priotracker *atracker;
264 pc = pcpu_find(curcpu);
266 /* Check if we just need to do a proper critical_exit. */
267 if (!(pc->pc_cpumask & rm->rm_writecpus)) {
272 /* Remove our tracker from the per-cpu list. */
273 rm_tracker_remove(pc, tracker);
275 /* Check to see if the IPI granted us the lock after all. */
276 if (tracker->rmp_flags) {
277 /* Just add back tracker - we hold the lock. */
278 rm_tracker_add(pc, tracker);
284 * We allow readers to aquire a lock even if a writer is blocked if
285 * the lock is recursive and the reader already holds the lock.
287 if ((rm->lock_object.lo_flags & LO_RECURSABLE) != 0) {
289 * Just grant the lock if this thread already has a tracker
290 * for this lock on the per-cpu queue.
292 for (queue = pc->pc_rm_queue.rmq_next;
293 queue != &pc->pc_rm_queue; queue = queue->rmq_next) {
294 atracker = (struct rm_priotracker *)queue;
295 if ((atracker->rmp_rmlock == rm) &&
296 (atracker->rmp_thread == tracker->rmp_thread)) {
297 mtx_lock_spin(&rm_spinlock);
298 LIST_INSERT_HEAD(&rm->rm_activeReaders,
299 tracker, rmp_qentry);
300 tracker->rmp_flags = RMPF_ONQUEUE;
301 mtx_unlock_spin(&rm_spinlock);
302 rm_tracker_add(pc, tracker);
313 if (rm->lock_object.lo_flags & RM_SLEEPABLE) {
314 if (!sx_try_xlock(&rm->rm_lock_sx))
317 if (!mtx_trylock(&rm->rm_lock_mtx))
321 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
322 sx_xlock(&rm->rm_lock_sx);
324 mtx_lock(&rm->rm_lock_mtx);
328 pc = pcpu_find(curcpu);
329 rm->rm_writecpus &= ~pc->pc_cpumask;
330 rm_tracker_add(pc, tracker);
334 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
335 sx_xunlock(&rm->rm_lock_sx);
337 mtx_unlock(&rm->rm_lock_mtx);
343 _rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker, int trylock)
345 struct thread *td = curthread;
348 tracker->rmp_flags = 0;
349 tracker->rmp_thread = td;
350 tracker->rmp_rmlock = rm;
352 td->td_critnest++; /* critical_enter(); */
354 compiler_memory_barrier();
356 pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
358 rm_tracker_add(pc, tracker);
362 compiler_memory_barrier();
367 * Fast path to combine two common conditions into a single
370 if (0 == (td->td_owepreempt | (rm->rm_writecpus & pc->pc_cpumask)))
373 /* We do not have a read token and need to acquire one. */
374 return _rm_rlock_hard(rm, tracker, trylock);
378 _rm_unlock_hard(struct thread *td,struct rm_priotracker *tracker)
381 if (td->td_owepreempt) {
386 if (!tracker->rmp_flags)
389 mtx_lock_spin(&rm_spinlock);
390 LIST_REMOVE(tracker, rmp_qentry);
392 if (tracker->rmp_flags & RMPF_SIGNAL) {
394 struct turnstile *ts;
396 rm = tracker->rmp_rmlock;
398 turnstile_chain_lock(&rm->lock_object);
399 mtx_unlock_spin(&rm_spinlock);
401 ts = turnstile_lookup(&rm->lock_object);
403 turnstile_signal(ts, TS_EXCLUSIVE_QUEUE);
404 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
405 turnstile_chain_unlock(&rm->lock_object);
407 mtx_unlock_spin(&rm_spinlock);
411 _rm_runlock(struct rmlock *rm, struct rm_priotracker *tracker)
414 struct thread *td = tracker->rmp_thread;
416 td->td_critnest++; /* critical_enter(); */
417 pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
418 rm_tracker_remove(pc, tracker);
422 if (0 == (td->td_owepreempt | tracker->rmp_flags))
425 _rm_unlock_hard(td, tracker);
429 _rm_wlock(struct rmlock *rm)
431 struct rm_priotracker *prio;
432 struct turnstile *ts;
435 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
436 sx_xlock(&rm->rm_lock_sx);
438 mtx_lock(&rm->rm_lock_mtx);
440 if (rm->rm_writecpus != all_cpus) {
441 /* Get all read tokens back */
443 readcpus = all_cpus & (all_cpus & ~rm->rm_writecpus);
444 rm->rm_writecpus = all_cpus;
447 * Assumes rm->rm_writecpus update is visible on other CPUs
448 * before rm_cleanIPI is called.
451 smp_rendezvous_cpus(readcpus,
452 smp_no_rendevous_barrier,
454 smp_no_rendevous_barrier,
461 mtx_lock_spin(&rm_spinlock);
462 while ((prio = LIST_FIRST(&rm->rm_activeReaders)) != NULL) {
463 ts = turnstile_trywait(&rm->lock_object);
464 prio->rmp_flags = RMPF_ONQUEUE | RMPF_SIGNAL;
465 mtx_unlock_spin(&rm_spinlock);
466 turnstile_wait(ts, prio->rmp_thread,
468 mtx_lock_spin(&rm_spinlock);
470 mtx_unlock_spin(&rm_spinlock);
475 _rm_wunlock(struct rmlock *rm)
478 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
479 sx_xunlock(&rm->rm_lock_sx);
481 mtx_unlock(&rm->rm_lock_mtx);
486 void _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
489 WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE,
494 LOCK_LOG_LOCK("RMWLOCK", &rm->lock_object, 0, 0, file, line);
496 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
497 WITNESS_LOCK(&rm->rm_lock_sx.lock_object, LOP_EXCLUSIVE,
500 WITNESS_LOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
502 curthread->td_locks++;
507 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
510 curthread->td_locks--;
511 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
512 WITNESS_UNLOCK(&rm->rm_lock_sx.lock_object, LOP_EXCLUSIVE,
515 WITNESS_UNLOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
516 LOCK_LOG_LOCK("RMWUNLOCK", &rm->lock_object, 0, 0, file, line);
521 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
522 int trylock, const char *file, int line)
524 if (!trylock && (rm->lock_object.lo_flags & RM_SLEEPABLE))
525 WITNESS_CHECKORDER(&rm->rm_lock_sx.lock_object, LOP_NEWORDER,
527 WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER, file, line, NULL);
529 if (_rm_rlock(rm, tracker, trylock)) {
530 LOCK_LOG_LOCK("RMRLOCK", &rm->lock_object, 0, 0, file, line);
532 WITNESS_LOCK(&rm->lock_object, 0, file, line);
534 curthread->td_locks++;
543 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
544 const char *file, int line)
547 curthread->td_locks--;
548 WITNESS_UNLOCK(&rm->lock_object, 0, file, line);
549 LOCK_LOG_LOCK("RMRUNLOCK", &rm->lock_object, 0, 0, file, line);
550 _rm_runlock(rm, tracker);
556 * Just strip out file and line arguments if no lock debugging is enabled in
557 * the kernel - we are called from a kernel module.
560 _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
567 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
574 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
575 int trylock, const char *file, int line)
578 return _rm_rlock(rm, tracker, trylock);
582 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
583 const char *file, int line)
586 _rm_runlock(rm, tracker);