2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 * promote products derived from this software without specific prior
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
29 * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
33 * Machine independent bits of mutex implementation.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "opt_adaptive_mutexes.h"
41 #include "opt_global.h"
42 #include "opt_mutex_wake_all.h"
43 #include "opt_sched.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
50 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
56 #include <sys/resourcevar.h>
57 #include <sys/sched.h>
59 #include <sys/sysctl.h>
60 #include <sys/turnstile.h>
61 #include <sys/vmmeter.h>
62 #include <sys/lock_profile.h>
64 #include <machine/atomic.h>
65 #include <machine/bus.h>
66 #include <machine/cpu.h>
70 #include <fs/devfs/devfs_int.h>
73 #include <vm/vm_extern.h>
76 * Force MUTEX_WAKE_ALL for now.
77 * single thread wakeup needs fixes to avoid race conditions with
78 * priority inheritance.
80 #ifndef MUTEX_WAKE_ALL
81 #define MUTEX_WAKE_ALL
84 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
85 #define ADAPTIVE_MUTEXES
89 * Internal utility macros.
91 #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED)
93 #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
95 #define mtx_owner(m) ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
98 static void db_show_mtx(struct lock_object *lock);
100 static void lock_mtx(struct lock_object *lock, int how);
101 static void lock_spin(struct lock_object *lock, int how);
102 static int unlock_mtx(struct lock_object *lock);
103 static int unlock_spin(struct lock_object *lock);
106 * Lock classes for sleep and spin mutexes.
108 struct lock_class lock_class_mtx_sleep = {
109 .lc_name = "sleep mutex",
110 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
112 .lc_ddb_show = db_show_mtx,
115 .lc_unlock = unlock_mtx,
117 struct lock_class lock_class_mtx_spin = {
118 .lc_name = "spin mutex",
119 .lc_flags = LC_SPINLOCK | LC_RECURSABLE,
121 .lc_ddb_show = db_show_mtx,
123 .lc_lock = lock_spin,
124 .lc_unlock = unlock_spin,
128 * System-wide mutexes
130 struct mtx sched_lock;
133 #ifdef LOCK_PROFILING
134 static inline void lock_profile_init(void)
137 /* Initialize the mutex profiling locks */
138 for (i = 0; i < LPROF_LOCK_SIZE; i++) {
139 mtx_init(&lprof_locks[i], "mprof lock",
140 NULL, MTX_SPIN|MTX_QUIET|MTX_NOPROFILE);
144 static inline void lock_profile_init(void) {;}
148 lock_mtx(struct lock_object *lock, int how)
151 mtx_lock((struct mtx *)lock);
155 lock_spin(struct lock_object *lock, int how)
158 panic("spin locks can only use msleep_spin");
162 unlock_mtx(struct lock_object *lock)
166 m = (struct mtx *)lock;
167 mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
173 unlock_spin(struct lock_object *lock)
176 panic("spin locks can only use msleep_spin");
180 * Function versions of the inlined __mtx_* macros. These are used by
181 * modules and can also be called from assembly language if needed.
184 _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
187 MPASS(curthread != NULL);
188 KASSERT(m->mtx_lock != MTX_DESTROYED,
189 ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
190 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
191 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
193 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
196 _get_sleep_lock(m, curthread, opts, file, line);
197 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
199 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
200 curthread->td_locks++;
204 _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
206 MPASS(curthread != NULL);
207 KASSERT(m->mtx_lock != MTX_DESTROYED,
208 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
209 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
210 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
212 curthread->td_locks--;
213 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
214 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
216 mtx_assert(m, MA_OWNED);
218 if (m->mtx_recurse == 0)
219 lock_profile_release_lock(&m->lock_object);
220 _rel_sleep_lock(m, curthread, opts, file, line);
224 _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
227 MPASS(curthread != NULL);
228 KASSERT(m->mtx_lock != MTX_DESTROYED,
229 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
230 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
231 ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
232 m->lock_object.lo_name, file, line));
233 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
235 _get_spin_lock(m, curthread, opts, file, line);
236 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
238 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
242 _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
245 MPASS(curthread != NULL);
246 KASSERT(m->mtx_lock != MTX_DESTROYED,
247 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
248 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
249 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
250 m->lock_object.lo_name, file, line));
251 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
252 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
254 mtx_assert(m, MA_OWNED);
260 * The important part of mtx_trylock{,_flags}()
261 * Tries to acquire lock `m.' If this function is called on a mutex that
262 * is already owned, it will recursively acquire the lock.
265 _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
267 int rval, contested = 0;
268 uint64_t waittime = 0;
270 MPASS(curthread != NULL);
271 KASSERT(m->mtx_lock != MTX_DESTROYED,
272 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
273 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
274 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
277 if (mtx_owned(m) && (m->lock_object.lo_flags & LO_RECURSABLE) != 0) {
279 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
282 rval = _obtain_lock(m, (uintptr_t)curthread);
284 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
286 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
288 curthread->td_locks++;
289 if (m->mtx_recurse == 0)
290 lock_profile_obtain_lock_success(&m->lock_object, contested,
291 waittime, file, line);
299 * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
301 * We call this if the lock is either contested (i.e. we need to go to
302 * sleep waiting for it), or if we need to recurse on it.
305 _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
308 #ifdef ADAPTIVE_MUTEXES
309 volatile struct thread *owner;
315 uint64_t waittime = 0;
319 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
320 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
321 m->lock_object.lo_name, file, line));
323 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
324 if (LOCK_LOG_TEST(&m->lock_object, opts))
325 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
329 lock_profile_obtain_lock_failed(&m->lock_object,
330 &contested, &waittime);
331 if (LOCK_LOG_TEST(&m->lock_object, opts))
333 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
334 m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
336 while (!_obtain_lock(m, tid)) {
337 turnstile_lock(&m->lock_object);
341 * Check if the lock has been released while spinning for
342 * the turnstile chain lock.
344 if (v == MTX_UNOWNED) {
345 turnstile_release(&m->lock_object);
350 #ifdef MUTEX_WAKE_ALL
351 MPASS(v != MTX_CONTESTED);
354 * The mutex was marked contested on release. This means that
355 * there are other threads blocked on it. Grab ownership of
356 * it and propagate its priority to the current thread if
359 if (v == MTX_CONTESTED) {
360 m->mtx_lock = tid | MTX_CONTESTED;
361 turnstile_claim(&m->lock_object);
367 * If the mutex isn't already contested and a failure occurs
368 * setting the contested bit, the mutex was either released
369 * or the state of the MTX_RECURSED bit changed.
371 if ((v & MTX_CONTESTED) == 0 &&
372 !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
373 turnstile_release(&m->lock_object);
378 #ifdef ADAPTIVE_MUTEXES
380 * If the current owner of the lock is executing on another
381 * CPU, spin instead of blocking.
383 owner = (struct thread *)(v & ~MTX_FLAGMASK);
384 #ifdef ADAPTIVE_GIANT
385 if (TD_IS_RUNNING(owner))
387 if (m != &Giant && TD_IS_RUNNING(owner))
390 turnstile_release(&m->lock_object);
391 while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
396 #endif /* ADAPTIVE_MUTEXES */
399 * We definitely must sleep for this lock.
401 mtx_assert(m, MA_NOTOWNED);
406 "contention: %p at %s:%d wants %s, taken by %s:%d",
407 (void *)tid, file, line, m->lock_object.lo_name,
408 WITNESS_FILE(&m->lock_object),
409 WITNESS_LINE(&m->lock_object));
415 * Block on the turnstile.
417 turnstile_wait(&m->lock_object, mtx_owner(m),
423 "contention end: %s acquired by %p at %s:%d",
424 m->lock_object.lo_name, (void *)tid, file, line);
427 lock_profile_obtain_lock_success(&m->lock_object, contested,
428 waittime, (file), (line));
433 * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
435 * This is only called if we need to actually spin for the lock. Recursion
439 _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file,
442 int i = 0, contested = 0;
444 uint64_t waittime = 0;
446 if (LOCK_LOG_TEST(&m->lock_object, opts))
447 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
449 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
450 while (!_obtain_lock(m, tid)) {
452 /* Give interrupts a chance while we spin. */
454 while (m->mtx_lock != MTX_UNOWNED) {
455 if (i++ < 10000000) {
459 if (i < 60000000 || kdb_active || panicstr != NULL)
464 /* If the mutex is unlocked, try again. */
468 "spin lock %p (%s) held by %p (tid %d) too long\n",
469 m, m->lock_object.lo_name, td, td->td_tid);
471 witness_display_spinlock(&m->lock_object, td);
473 panic("spin lock held too long");
480 if (LOCK_LOG_TEST(&m->lock_object, opts))
481 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
483 lock_profile_obtain_lock_success(&m->lock_object, contested,
484 waittime, (file), (line));
490 * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
492 * We are only called here if the lock is recursed or contested (i.e. we
493 * need to wake up a blocked thread).
496 _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
498 struct turnstile *ts;
500 struct thread *td, *td1;
503 if (mtx_recursed(m)) {
504 if (--(m->mtx_recurse) == 0)
505 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
506 if (LOCK_LOG_TEST(&m->lock_object, opts))
507 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
511 turnstile_lock(&m->lock_object);
512 ts = turnstile_lookup(&m->lock_object);
513 if (LOCK_LOG_TEST(&m->lock_object, opts))
514 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
516 #ifdef ADAPTIVE_MUTEXES
518 _release_lock_quick(m);
519 if (LOCK_LOG_TEST(&m->lock_object, opts))
520 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
521 turnstile_release(&m->lock_object);
529 td1 = turnstile_head(ts, TS_EXCLUSIVE_QUEUE);
531 #ifdef MUTEX_WAKE_ALL
532 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
533 _release_lock_quick(m);
535 if (turnstile_signal(ts, TS_EXCLUSIVE_QUEUE)) {
536 _release_lock_quick(m);
537 if (LOCK_LOG_TEST(&m->lock_object, opts))
538 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
540 m->mtx_lock = MTX_CONTESTED;
541 if (LOCK_LOG_TEST(&m->lock_object, opts))
542 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
546 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
550 * XXX: This is just a hack until preemption is done. However,
551 * once preemption is done we need to either wrap the
552 * turnstile_signal() and release of the actual lock in an
553 * extra critical section or change the preemption code to
554 * always just set a flag and never do instant-preempts.
557 if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
560 mtx_lock_spin(&sched_lock);
561 if (!TD_IS_RUNNING(td1)) {
563 if (td->td_ithd != NULL) {
564 struct ithd *it = td->td_ithd;
566 if (it->it_interrupted) {
567 if (LOCK_LOG_TEST(&m->lock_object, opts))
569 "_mtx_unlock_sleep: %p interrupted %p",
570 it, it->it_interrupted);
575 if (LOCK_LOG_TEST(&m->lock_object, opts))
577 "_mtx_unlock_sleep: %p switching out lock=%p", m,
578 (void *)m->mtx_lock);
580 mi_switch(SW_INVOL, NULL);
581 if (LOCK_LOG_TEST(&m->lock_object, opts))
582 CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
583 m, (void *)m->mtx_lock);
585 mtx_unlock_spin(&sched_lock);
590 * All the unlocking of MTX_SPIN locks is done inline.
591 * See the _rel_spin_lock() macro for the details.
595 * The backing function for the INVARIANTS-enabled mtx_assert()
597 #ifdef INVARIANT_SUPPORT
599 _mtx_assert(struct mtx *m, int what, const char *file, int line)
602 if (panicstr != NULL || dumping)
606 case MA_OWNED | MA_RECURSED:
607 case MA_OWNED | MA_NOTRECURSED:
609 panic("mutex %s not owned at %s:%d",
610 m->lock_object.lo_name, file, line);
611 if (mtx_recursed(m)) {
612 if ((what & MA_NOTRECURSED) != 0)
613 panic("mutex %s recursed at %s:%d",
614 m->lock_object.lo_name, file, line);
615 } else if ((what & MA_RECURSED) != 0) {
616 panic("mutex %s unrecursed at %s:%d",
617 m->lock_object.lo_name, file, line);
622 panic("mutex %s owned at %s:%d",
623 m->lock_object.lo_name, file, line);
626 panic("unknown mtx_assert at %s:%d", file, line);
632 * The MUTEX_DEBUG-enabled mtx_validate()
634 * Most of these checks have been moved off into the LO_INITIALIZED flag
635 * maintained by the witness code.
639 void mtx_validate(struct mtx *);
642 mtx_validate(struct mtx *m)
646 * XXX: When kernacc() does not require Giant we can reenable this check
650 * Can't call kernacc() from early init386(), especially when
651 * initializing Giant mutex, because some stuff in kernacc()
652 * requires Giant itself.
655 if (!kernacc((caddr_t)m, sizeof(m),
656 VM_PROT_READ | VM_PROT_WRITE))
657 panic("Can't read and write to mutex %p", m);
663 * General init routine used by the MTX_SYSINIT() macro.
666 mtx_sysinit(void *arg)
668 struct mtx_args *margs = arg;
670 mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
674 * Mutex initialization routine; initialize lock `m' of type contained in
675 * `opts' with options contained in `opts' and name `name.' The optional
676 * lock type `type' is used as a general lock category name for use with
680 mtx_init(struct mtx *m, const char *name, const char *type, int opts)
682 struct lock_class *class;
685 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
686 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0);
689 /* Diagnostic and error correction */
693 /* Determine lock class and lock flags. */
695 class = &lock_class_mtx_spin;
697 class = &lock_class_mtx_sleep;
699 if (opts & MTX_QUIET)
701 if (opts & MTX_RECURSE)
702 flags |= LO_RECURSABLE;
703 if ((opts & MTX_NOWITNESS) == 0)
705 if (opts & MTX_DUPOK)
707 if (opts & MTX_NOPROFILE)
708 flags |= LO_NOPROFILE;
710 /* Initialize mutex. */
711 m->mtx_lock = MTX_UNOWNED;
714 lock_init(&m->lock_object, class, name, type, flags);
718 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be
719 * passed in as a flag here because if the corresponding mtx_init() was
720 * called with MTX_QUIET set, then it will already be set in the mutex's
724 mtx_destroy(struct mtx *m)
728 MPASS(mtx_unowned(m));
730 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
732 /* Perform the non-mtx related part of mtx_unlock_spin(). */
733 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
736 curthread->td_locks--;
738 /* Tell witness this isn't locked to make it happy. */
739 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
743 m->mtx_lock = MTX_DESTROYED;
744 lock_destroy(&m->lock_object);
748 * Intialize the mutex code and system mutexes. This is called from the MD
749 * startup code prior to mi_startup(). The per-CPU data space needs to be
750 * setup before this is called.
756 /* Setup turnstiles so that sleep mutexes work. */
760 * Initialize mutexes.
762 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
763 mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
764 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
765 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
773 db_show_mtx(struct lock_object *lock)
778 m = (struct mtx *)lock;
780 db_printf(" flags: {");
781 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
785 if (m->lock_object.lo_flags & LO_RECURSABLE)
786 db_printf(", RECURSE");
787 if (m->lock_object.lo_flags & LO_DUPOK)
788 db_printf(", DUPOK");
790 db_printf(" state: {");
792 db_printf("UNOWNED");
793 else if (mtx_destroyed(m))
794 db_printf("DESTROYED");
797 if (m->mtx_lock & MTX_CONTESTED)
798 db_printf(", CONTESTED");
799 if (m->mtx_lock & MTX_RECURSED)
800 db_printf(", RECURSED");
803 if (!mtx_unowned(m) && !mtx_destroyed(m)) {
805 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
806 td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
808 db_printf(" recursed: %d\n", m->mtx_recurse);