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/turnstile.h>
52 #include <sys/lock_profile.h>
53 #include <machine/cpu.h>
59 #define RMPF_ONQUEUE 1
63 * To support usage of rmlock in CVs and msleep yet another list for the
64 * priority tracker would be needed. Using this lock for cv and msleep also
65 * does not seem very useful
68 static __inline void compiler_memory_barrier(void) {
69 __asm __volatile("":::"memory");
72 static void assert_rm(struct lock_object *lock, int what);
73 static void lock_rm(struct lock_object *lock, int how);
75 static int owner_rm(struct lock_object *lock, struct thread **owner);
77 static int unlock_rm(struct lock_object *lock);
79 struct lock_class lock_class_rm = {
81 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
82 .lc_assert = assert_rm,
85 .lc_ddb_show = db_show_rwlock,
89 .lc_unlock = unlock_rm,
96 assert_rm(struct lock_object *lock, int what)
99 panic("assert_rm called");
103 lock_rm(struct lock_object *lock, int how)
106 panic("lock_rm called");
110 unlock_rm(struct lock_object *lock)
113 panic("unlock_rm called");
118 owner_rm(struct lock_object *lock, struct thread **owner)
121 panic("owner_rm called");
125 static struct mtx rm_spinlock;
127 MTX_SYSINIT(rm_spinlock, &rm_spinlock, "rm_spinlock", MTX_SPIN);
130 * Add or remove tracker from per-cpu list.
132 * The per-cpu list can be traversed at any time in forward direction from an
133 * interrupt on the *local* cpu.
136 rm_tracker_add(struct pcpu *pc, struct rm_priotracker *tracker)
138 struct rm_queue *next;
140 /* Initialize all tracker pointers */
141 tracker->rmp_cpuQueue.rmq_prev = &pc->pc_rm_queue;
142 next = pc->pc_rm_queue.rmq_next;
143 tracker->rmp_cpuQueue.rmq_next = next;
145 /* rmq_prev is not used during froward traversal. */
146 next->rmq_prev = &tracker->rmp_cpuQueue;
148 /* Update pointer to first element. */
149 pc->pc_rm_queue.rmq_next = &tracker->rmp_cpuQueue;
153 rm_tracker_remove(struct pcpu *pc, struct rm_priotracker *tracker)
155 struct rm_queue *next, *prev;
157 next = tracker->rmp_cpuQueue.rmq_next;
158 prev = tracker->rmp_cpuQueue.rmq_prev;
160 /* Not used during forward traversal. */
161 next->rmq_prev = prev;
163 /* Remove from list. */
164 prev->rmq_next = next;
168 rm_cleanIPI(void *arg)
171 struct rmlock *rm = arg;
172 struct rm_priotracker *tracker;
173 struct rm_queue *queue;
174 pc = pcpu_find(curcpu);
176 for (queue = pc->pc_rm_queue.rmq_next; queue != &pc->pc_rm_queue;
177 queue = queue->rmq_next) {
178 tracker = (struct rm_priotracker *)queue;
179 if (tracker->rmp_rmlock == rm && tracker->rmp_flags == 0) {
180 tracker->rmp_flags = RMPF_ONQUEUE;
181 mtx_lock_spin(&rm_spinlock);
182 LIST_INSERT_HEAD(&rm->rm_activeReaders, tracker,
184 mtx_unlock_spin(&rm_spinlock);
189 CTASSERT((RM_SLEEPABLE & LO_CLASSFLAGS) == RM_SLEEPABLE);
192 rm_init_flags(struct rmlock *rm, const char *name, int opts)
197 if (!(opts & RM_NOWITNESS))
198 liflags |= LO_WITNESS;
199 if (opts & RM_RECURSE)
200 liflags |= LO_RECURSABLE;
201 rm->rm_writecpus = all_cpus;
202 LIST_INIT(&rm->rm_activeReaders);
203 if (opts & RM_SLEEPABLE) {
204 liflags |= RM_SLEEPABLE;
205 sx_init_flags(&rm->rm_lock_sx, "rmlock_sx", SX_RECURSE);
207 mtx_init(&rm->rm_lock_mtx, name, "rmlock_mtx", MTX_NOWITNESS);
208 lock_init(&rm->lock_object, &lock_class_rm, name, NULL, liflags);
212 rm_init(struct rmlock *rm, const char *name)
215 rm_init_flags(rm, name, 0);
219 rm_destroy(struct rmlock *rm)
222 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
223 sx_destroy(&rm->rm_lock_sx);
225 mtx_destroy(&rm->rm_lock_mtx);
226 lock_destroy(&rm->lock_object);
230 rm_wowned(struct rmlock *rm)
233 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
234 return (sx_xlocked(&rm->rm_lock_sx));
236 return (mtx_owned(&rm->rm_lock_mtx));
240 rm_sysinit(void *arg)
242 struct rm_args *args = arg;
244 rm_init(args->ra_rm, args->ra_desc);
248 rm_sysinit_flags(void *arg)
250 struct rm_args_flags *args = arg;
252 rm_init_flags(args->ra_rm, args->ra_desc, args->ra_opts);
256 _rm_rlock_hard(struct rmlock *rm, struct rm_priotracker *tracker, int trylock)
259 struct rm_queue *queue;
260 struct rm_priotracker *atracker;
263 pc = pcpu_find(curcpu);
265 /* Check if we just need to do a proper critical_exit. */
266 if (!CPU_ISSET(pc->pc_cpuid, &rm->rm_writecpus)) {
271 /* Remove our tracker from the per-cpu list. */
272 rm_tracker_remove(pc, tracker);
274 /* Check to see if the IPI granted us the lock after all. */
275 if (tracker->rmp_flags) {
276 /* Just add back tracker - we hold the lock. */
277 rm_tracker_add(pc, tracker);
283 * We allow readers to aquire a lock even if a writer is blocked if
284 * the lock is recursive and the reader already holds the lock.
286 if ((rm->lock_object.lo_flags & LO_RECURSABLE) != 0) {
288 * Just grant the lock if this thread already has a tracker
289 * for this lock on the per-cpu queue.
291 for (queue = pc->pc_rm_queue.rmq_next;
292 queue != &pc->pc_rm_queue; queue = queue->rmq_next) {
293 atracker = (struct rm_priotracker *)queue;
294 if ((atracker->rmp_rmlock == rm) &&
295 (atracker->rmp_thread == tracker->rmp_thread)) {
296 mtx_lock_spin(&rm_spinlock);
297 LIST_INSERT_HEAD(&rm->rm_activeReaders,
298 tracker, rmp_qentry);
299 tracker->rmp_flags = RMPF_ONQUEUE;
300 mtx_unlock_spin(&rm_spinlock);
301 rm_tracker_add(pc, tracker);
312 if (rm->lock_object.lo_flags & RM_SLEEPABLE) {
313 if (!sx_try_xlock(&rm->rm_lock_sx))
316 if (!mtx_trylock(&rm->rm_lock_mtx))
320 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
321 sx_xlock(&rm->rm_lock_sx);
323 mtx_lock(&rm->rm_lock_mtx);
327 pc = pcpu_find(curcpu);
328 CPU_CLR(pc->pc_cpuid, &rm->rm_writecpus);
329 rm_tracker_add(pc, tracker);
333 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
334 sx_xunlock(&rm->rm_lock_sx);
336 mtx_unlock(&rm->rm_lock_mtx);
342 _rm_rlock(struct rmlock *rm, struct rm_priotracker *tracker, int trylock)
344 struct thread *td = curthread;
347 if (SCHEDULER_STOPPED())
350 tracker->rmp_flags = 0;
351 tracker->rmp_thread = td;
352 tracker->rmp_rmlock = rm;
354 td->td_critnest++; /* critical_enter(); */
356 compiler_memory_barrier();
358 pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
360 rm_tracker_add(pc, tracker);
364 compiler_memory_barrier();
369 * Fast path to combine two common conditions into a single
372 if (0 == (td->td_owepreempt |
373 CPU_ISSET(pc->pc_cpuid, &rm->rm_writecpus)))
376 /* We do not have a read token and need to acquire one. */
377 return _rm_rlock_hard(rm, tracker, trylock);
381 _rm_unlock_hard(struct thread *td,struct rm_priotracker *tracker)
384 if (td->td_owepreempt) {
389 if (!tracker->rmp_flags)
392 mtx_lock_spin(&rm_spinlock);
393 LIST_REMOVE(tracker, rmp_qentry);
395 if (tracker->rmp_flags & RMPF_SIGNAL) {
397 struct turnstile *ts;
399 rm = tracker->rmp_rmlock;
401 turnstile_chain_lock(&rm->lock_object);
402 mtx_unlock_spin(&rm_spinlock);
404 ts = turnstile_lookup(&rm->lock_object);
406 turnstile_signal(ts, TS_EXCLUSIVE_QUEUE);
407 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
408 turnstile_chain_unlock(&rm->lock_object);
410 mtx_unlock_spin(&rm_spinlock);
414 _rm_runlock(struct rmlock *rm, struct rm_priotracker *tracker)
417 struct thread *td = tracker->rmp_thread;
419 if (SCHEDULER_STOPPED())
422 td->td_critnest++; /* critical_enter(); */
423 pc = cpuid_to_pcpu[td->td_oncpu]; /* pcpu_find(td->td_oncpu); */
424 rm_tracker_remove(pc, tracker);
428 if (0 == (td->td_owepreempt | tracker->rmp_flags))
431 _rm_unlock_hard(td, tracker);
435 _rm_wlock(struct rmlock *rm)
437 struct rm_priotracker *prio;
438 struct turnstile *ts;
441 if (SCHEDULER_STOPPED())
444 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
445 sx_xlock(&rm->rm_lock_sx);
447 mtx_lock(&rm->rm_lock_mtx);
449 if (CPU_CMP(&rm->rm_writecpus, &all_cpus)) {
450 /* Get all read tokens back */
452 CPU_NAND(&readcpus, &rm->rm_writecpus);
453 rm->rm_writecpus = all_cpus;
456 * Assumes rm->rm_writecpus update is visible on other CPUs
457 * before rm_cleanIPI is called.
460 smp_rendezvous_cpus(readcpus,
461 smp_no_rendevous_barrier,
463 smp_no_rendevous_barrier,
470 mtx_lock_spin(&rm_spinlock);
471 while ((prio = LIST_FIRST(&rm->rm_activeReaders)) != NULL) {
472 ts = turnstile_trywait(&rm->lock_object);
473 prio->rmp_flags = RMPF_ONQUEUE | RMPF_SIGNAL;
474 mtx_unlock_spin(&rm_spinlock);
475 turnstile_wait(ts, prio->rmp_thread,
477 mtx_lock_spin(&rm_spinlock);
479 mtx_unlock_spin(&rm_spinlock);
484 _rm_wunlock(struct rmlock *rm)
487 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
488 sx_xunlock(&rm->rm_lock_sx);
490 mtx_unlock(&rm->rm_lock_mtx);
495 void _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
498 if (SCHEDULER_STOPPED())
501 WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER | LOP_EXCLUSIVE,
506 LOCK_LOG_LOCK("RMWLOCK", &rm->lock_object, 0, 0, file, line);
508 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
509 WITNESS_LOCK(&rm->rm_lock_sx.lock_object, LOP_EXCLUSIVE,
512 WITNESS_LOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
514 curthread->td_locks++;
519 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
522 if (SCHEDULER_STOPPED())
525 curthread->td_locks--;
526 if (rm->lock_object.lo_flags & RM_SLEEPABLE)
527 WITNESS_UNLOCK(&rm->rm_lock_sx.lock_object, LOP_EXCLUSIVE,
530 WITNESS_UNLOCK(&rm->lock_object, LOP_EXCLUSIVE, file, line);
531 LOCK_LOG_LOCK("RMWUNLOCK", &rm->lock_object, 0, 0, file, line);
536 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
537 int trylock, const char *file, int line)
540 if (SCHEDULER_STOPPED())
543 if (!trylock && (rm->lock_object.lo_flags & RM_SLEEPABLE))
544 WITNESS_CHECKORDER(&rm->rm_lock_sx.lock_object, LOP_NEWORDER,
546 WITNESS_CHECKORDER(&rm->lock_object, LOP_NEWORDER, file, line, NULL);
548 if (_rm_rlock(rm, tracker, trylock)) {
549 LOCK_LOG_LOCK("RMRLOCK", &rm->lock_object, 0, 0, file, line);
551 WITNESS_LOCK(&rm->lock_object, 0, file, line);
553 curthread->td_locks++;
562 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
563 const char *file, int line)
566 if (SCHEDULER_STOPPED())
569 curthread->td_locks--;
570 WITNESS_UNLOCK(&rm->lock_object, 0, file, line);
571 LOCK_LOG_LOCK("RMRUNLOCK", &rm->lock_object, 0, 0, file, line);
572 _rm_runlock(rm, tracker);
578 * Just strip out file and line arguments if no lock debugging is enabled in
579 * the kernel - we are called from a kernel module.
582 _rm_wlock_debug(struct rmlock *rm, const char *file, int line)
589 _rm_wunlock_debug(struct rmlock *rm, const char *file, int line)
596 _rm_rlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
597 int trylock, const char *file, int line)
600 return _rm_rlock(rm, tracker, trylock);
604 _rm_runlock_debug(struct rmlock *rm, struct rm_priotracker *tracker,
605 const char *file, int line)
608 _rm_runlock(rm, tracker);