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_hwpmc_hooks.h"
42 #include "opt_sched.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
49 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
55 #include <sys/resourcevar.h>
56 #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>
75 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
76 #define ADAPTIVE_MUTEXES
80 #include <sys/pmckern.h>
81 PMC_SOFT_DEFINE( , , lock, failed);
85 * Return the mutex address when the lock cookie address is provided.
86 * This functionality assumes that struct mtx* have a member named mtx_lock.
88 #define mtxlock2mtx(c) (__containerof(c, struct mtx, mtx_lock))
91 * Internal utility macros.
93 #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED)
95 #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
97 #define mtx_owner(m) ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
99 static void assert_mtx(const struct lock_object *lock, int what);
101 static void db_show_mtx(const struct lock_object *lock);
103 static void lock_mtx(struct lock_object *lock, uintptr_t how);
104 static void lock_spin(struct lock_object *lock, uintptr_t how);
106 static int owner_mtx(const struct lock_object *lock,
107 struct thread **owner);
109 static uintptr_t unlock_mtx(struct lock_object *lock);
110 static uintptr_t unlock_spin(struct lock_object *lock);
113 * Lock classes for sleep and spin mutexes.
115 struct lock_class lock_class_mtx_sleep = {
116 .lc_name = "sleep mutex",
117 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
118 .lc_assert = assert_mtx,
120 .lc_ddb_show = db_show_mtx,
123 .lc_unlock = unlock_mtx,
125 .lc_owner = owner_mtx,
128 struct lock_class lock_class_mtx_spin = {
129 .lc_name = "spin mutex",
130 .lc_flags = LC_SPINLOCK | LC_RECURSABLE,
131 .lc_assert = assert_mtx,
133 .lc_ddb_show = db_show_mtx,
135 .lc_lock = lock_spin,
136 .lc_unlock = unlock_spin,
138 .lc_owner = owner_mtx,
142 #ifdef ADAPTIVE_MUTEXES
143 static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging");
145 static struct lock_delay_config mtx_delay = {
152 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_initial, CTLFLAG_RW, &mtx_delay.initial,
154 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_step, CTLFLAG_RW, &mtx_delay.step,
156 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_min, CTLFLAG_RW, &mtx_delay.min,
158 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
162 mtx_delay_sysinit(void *dummy)
165 mtx_delay.initial = mp_ncpus * 25;
166 mtx_delay.step = (mp_ncpus * 25) / 2;
167 mtx_delay.min = mp_ncpus * 5;
168 mtx_delay.max = mp_ncpus * 25 * 10;
170 LOCK_DELAY_SYSINIT(mtx_delay_sysinit);
174 * System-wide mutexes
176 struct mtx blocked_lock;
180 assert_mtx(const struct lock_object *lock, int what)
183 mtx_assert((const struct mtx *)lock, what);
187 lock_mtx(struct lock_object *lock, uintptr_t how)
190 mtx_lock((struct mtx *)lock);
194 lock_spin(struct lock_object *lock, uintptr_t how)
197 panic("spin locks can only use msleep_spin");
201 unlock_mtx(struct lock_object *lock)
205 m = (struct mtx *)lock;
206 mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
212 unlock_spin(struct lock_object *lock)
215 panic("spin locks can only use msleep_spin");
220 owner_mtx(const struct lock_object *lock, struct thread **owner)
222 const struct mtx *m = (const struct mtx *)lock;
224 *owner = mtx_owner(m);
225 return (mtx_unowned(m) == 0);
230 * Function versions of the inlined __mtx_* macros. These are used by
231 * modules and can also be called from assembly language if needed.
234 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
238 if (SCHEDULER_STOPPED())
243 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
244 ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
245 curthread, m->lock_object.lo_name, file, line));
246 KASSERT(m->mtx_lock != MTX_DESTROYED,
247 ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
248 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
249 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
251 WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
252 LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
254 __mtx_lock(m, curthread, opts, file, line);
255 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
257 WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
259 TD_LOCKS_INC(curthread);
263 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
267 if (SCHEDULER_STOPPED())
272 KASSERT(m->mtx_lock != MTX_DESTROYED,
273 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
274 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
275 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
277 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
278 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
280 mtx_assert(m, MA_OWNED);
282 __mtx_unlock(m, curthread, opts, file, line);
283 TD_LOCKS_DEC(curthread);
287 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
292 if (SCHEDULER_STOPPED())
297 KASSERT(m->mtx_lock != MTX_DESTROYED,
298 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
299 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
300 ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
301 m->lock_object.lo_name, file, line));
303 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
304 (opts & MTX_RECURSE) != 0,
305 ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
306 m->lock_object.lo_name, file, line));
307 opts &= ~MTX_RECURSE;
308 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
310 __mtx_lock_spin(m, curthread, opts, file, line);
311 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
313 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
317 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
322 if (SCHEDULER_STOPPED())
327 KASSERT(m->mtx_lock != MTX_DESTROYED,
328 ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
329 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
330 ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
331 m->lock_object.lo_name, file, line));
332 KASSERT((opts & MTX_RECURSE) == 0,
333 ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
334 m->lock_object.lo_name, file, line));
335 if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
336 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
337 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
340 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
345 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
350 if (SCHEDULER_STOPPED())
355 KASSERT(m->mtx_lock != MTX_DESTROYED,
356 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
357 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
358 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
359 m->lock_object.lo_name, file, line));
360 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
361 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
363 mtx_assert(m, MA_OWNED);
365 __mtx_unlock_spin(m);
369 * The important part of mtx_trylock{,_flags}()
370 * Tries to acquire lock `m.' If this function is called on a mutex that
371 * is already owned, it will recursively acquire the lock.
374 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
377 #ifdef LOCK_PROFILING
378 uint64_t waittime = 0;
383 if (SCHEDULER_STOPPED())
388 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
389 ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
390 curthread, m->lock_object.lo_name, file, line));
391 KASSERT(m->mtx_lock != MTX_DESTROYED,
392 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
393 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
394 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
397 if (mtx_owned(m) && ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
398 (opts & MTX_RECURSE) != 0)) {
400 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
403 rval = _mtx_obtain_lock(m, (uintptr_t)curthread);
404 opts &= ~MTX_RECURSE;
406 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
408 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
410 TD_LOCKS_INC(curthread);
411 if (m->mtx_recurse == 0)
412 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
413 m, contested, waittime, file, line);
421 * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
423 * We call this if the lock is either contested (i.e. we need to go to
424 * sleep waiting for it), or if we need to recurse on it.
427 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t tid, int opts,
428 const char *file, int line)
431 struct turnstile *ts;
433 #ifdef ADAPTIVE_MUTEXES
434 volatile struct thread *owner;
439 #ifdef LOCK_PROFILING
441 uint64_t waittime = 0;
443 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
444 struct lock_delay_arg lda;
448 int64_t sleep_time = 0;
449 int64_t all_time = 0;
452 if (SCHEDULER_STOPPED())
455 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
456 lock_delay_arg_init(&lda, &mtx_delay);
461 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
462 (opts & MTX_RECURSE) != 0,
463 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
464 m->lock_object.lo_name, file, line));
465 opts &= ~MTX_RECURSE;
467 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
468 if (LOCK_LOG_TEST(&m->lock_object, opts))
469 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
472 opts &= ~MTX_RECURSE;
475 PMC_SOFT_CALL( , , lock, failed);
477 lock_profile_obtain_lock_failed(&m->lock_object,
478 &contested, &waittime);
479 if (LOCK_LOG_TEST(&m->lock_object, opts))
481 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
482 m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
484 all_time -= lockstat_nsecs(&m->lock_object);
488 if (m->mtx_lock == MTX_UNOWNED && _mtx_obtain_lock(m, tid))
493 #ifdef ADAPTIVE_MUTEXES
495 * If the owner is running on another CPU, spin until the
496 * owner stops running or the state of the lock changes.
499 if (v != MTX_UNOWNED) {
500 owner = (struct thread *)(v & ~MTX_FLAGMASK);
501 if (TD_IS_RUNNING(owner)) {
502 if (LOCK_LOG_TEST(&m->lock_object, 0))
504 "%s: spinning on %p held by %p",
506 KTR_STATE1(KTR_SCHED, "thread",
507 sched_tdname((struct thread *)tid),
508 "spinning", "lockname:\"%s\"",
509 m->lock_object.lo_name);
510 while (mtx_owner(m) == owner &&
511 TD_IS_RUNNING(owner))
513 KTR_STATE0(KTR_SCHED, "thread",
514 sched_tdname((struct thread *)tid),
521 ts = turnstile_trywait(&m->lock_object);
525 * Check if the lock has been released while spinning for
526 * the turnstile chain lock.
528 if (v == MTX_UNOWNED) {
529 turnstile_cancel(ts);
533 #ifdef ADAPTIVE_MUTEXES
535 * The current lock owner might have started executing
536 * on another CPU (or the lock could have changed
537 * owners) while we were waiting on the turnstile
538 * chain lock. If so, drop the turnstile lock and try
541 owner = (struct thread *)(v & ~MTX_FLAGMASK);
542 if (TD_IS_RUNNING(owner)) {
543 turnstile_cancel(ts);
549 * If the mutex isn't already contested and a failure occurs
550 * setting the contested bit, the mutex was either released
551 * or the state of the MTX_RECURSED bit changed.
553 if ((v & MTX_CONTESTED) == 0 &&
554 !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
555 turnstile_cancel(ts);
560 * We definitely must sleep for this lock.
562 mtx_assert(m, MA_NOTOWNED);
567 "contention: %p at %s:%d wants %s, taken by %s:%d",
568 (void *)tid, file, line, m->lock_object.lo_name,
569 WITNESS_FILE(&m->lock_object),
570 WITNESS_LINE(&m->lock_object));
576 * Block on the turnstile.
579 sleep_time -= lockstat_nsecs(&m->lock_object);
581 turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE);
583 sleep_time += lockstat_nsecs(&m->lock_object);
588 all_time += lockstat_nsecs(&m->lock_object);
593 "contention end: %s acquired by %p at %s:%d",
594 m->lock_object.lo_name, (void *)tid, file, line);
597 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
598 waittime, file, line);
601 LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
604 * Only record the loops spinning and not sleeping.
606 if (lda.spin_cnt > sleep_cnt)
607 LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
612 _mtx_lock_spin_failed(struct mtx *m)
618 /* If the mutex is unlocked, try again. */
622 printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
623 m, m->lock_object.lo_name, td, td->td_tid);
625 witness_display_spinlock(&m->lock_object, td, printf);
627 panic("spin lock held too long");
632 * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
634 * This is only called if we need to actually spin for the lock. Recursion
638 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t tid, int opts,
639 const char *file, int line)
643 #ifdef LOCK_PROFILING
645 uint64_t waittime = 0;
648 int64_t spin_time = 0;
651 if (SCHEDULER_STOPPED())
656 if (LOCK_LOG_TEST(&m->lock_object, opts))
657 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
658 KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
659 "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
662 PMC_SOFT_CALL( , , lock, failed);
664 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
666 spin_time -= lockstat_nsecs(&m->lock_object);
669 if (m->mtx_lock == MTX_UNOWNED && _mtx_obtain_lock(m, tid))
671 /* Give interrupts a chance while we spin. */
673 while (m->mtx_lock != MTX_UNOWNED) {
674 if (i++ < 10000000) {
678 if (i < 60000000 || kdb_active || panicstr != NULL)
681 _mtx_lock_spin_failed(m);
687 spin_time += lockstat_nsecs(&m->lock_object);
690 if (LOCK_LOG_TEST(&m->lock_object, opts))
691 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
692 KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
696 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
697 contested, waittime, file, line);
699 LOCKSTAT_RECORD1(spin__spin, m, spin_time);
705 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
710 #ifdef LOCK_PROFILING
712 uint64_t waittime = 0;
715 int64_t spin_time = 0;
719 tid = (uintptr_t)curthread;
721 if (SCHEDULER_STOPPED()) {
723 * Ensure that spinlock sections are balanced even when the
724 * scheduler is stopped, since we may otherwise inadvertently
725 * re-enable interrupts while dumping core.
732 spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
738 KASSERT(m->mtx_lock != MTX_DESTROYED,
739 ("thread_lock() of destroyed mutex @ %s:%d", file, line));
740 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
741 ("thread_lock() of sleep mutex %s @ %s:%d",
742 m->lock_object.lo_name, file, line));
744 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
745 ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
746 m->lock_object.lo_name, file, line));
747 WITNESS_CHECKORDER(&m->lock_object,
748 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
750 if (m->mtx_lock == MTX_UNOWNED && _mtx_obtain_lock(m, tid))
752 if (m->mtx_lock == tid) {
757 PMC_SOFT_CALL( , , lock, failed);
759 lock_profile_obtain_lock_failed(&m->lock_object,
760 &contested, &waittime);
761 /* Give interrupts a chance while we spin. */
763 while (m->mtx_lock != MTX_UNOWNED) {
766 else if (i < 60000000 ||
767 kdb_active || panicstr != NULL)
770 _mtx_lock_spin_failed(m);
772 if (m != td->td_lock)
777 if (m == td->td_lock)
779 __mtx_unlock_spin(m); /* does spinlock_exit() */
782 spin_time += lockstat_nsecs(&m->lock_object);
784 if (m->mtx_recurse == 0)
785 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
786 contested, waittime, file, line);
787 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
789 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
792 LOCKSTAT_RECORD1(thread__spin, m, spin_time);
797 thread_lock_block(struct thread *td)
801 THREAD_LOCK_ASSERT(td, MA_OWNED);
803 td->td_lock = &blocked_lock;
804 mtx_unlock_spin(lock);
810 thread_lock_unblock(struct thread *td, struct mtx *new)
812 mtx_assert(new, MA_OWNED);
813 MPASS(td->td_lock == &blocked_lock);
814 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
818 thread_lock_set(struct thread *td, struct mtx *new)
822 mtx_assert(new, MA_OWNED);
823 THREAD_LOCK_ASSERT(td, MA_OWNED);
826 mtx_unlock_spin(lock);
830 * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
832 * We are only called here if the lock is recursed or contested (i.e. we
833 * need to wake up a blocked thread).
836 __mtx_unlock_sleep(volatile uintptr_t *c, int opts, const char *file, int line)
839 struct turnstile *ts;
841 if (SCHEDULER_STOPPED())
846 if (mtx_recursed(m)) {
847 if (--(m->mtx_recurse) == 0)
848 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
849 if (LOCK_LOG_TEST(&m->lock_object, opts))
850 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
855 * We have to lock the chain before the turnstile so this turnstile
856 * can be removed from the hash list if it is empty.
858 turnstile_chain_lock(&m->lock_object);
859 ts = turnstile_lookup(&m->lock_object);
860 if (LOCK_LOG_TEST(&m->lock_object, opts))
861 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
863 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
864 _mtx_release_lock_quick(m);
867 * This turnstile is now no longer associated with the mutex. We can
868 * unlock the chain lock so a new turnstile may take it's place.
870 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
871 turnstile_chain_unlock(&m->lock_object);
875 * All the unlocking of MTX_SPIN locks is done inline.
876 * See the __mtx_unlock_spin() macro for the details.
880 * The backing function for the INVARIANTS-enabled mtx_assert()
882 #ifdef INVARIANT_SUPPORT
884 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
888 if (panicstr != NULL || dumping)
895 case MA_OWNED | MA_RECURSED:
896 case MA_OWNED | MA_NOTRECURSED:
898 panic("mutex %s not owned at %s:%d",
899 m->lock_object.lo_name, file, line);
900 if (mtx_recursed(m)) {
901 if ((what & MA_NOTRECURSED) != 0)
902 panic("mutex %s recursed at %s:%d",
903 m->lock_object.lo_name, file, line);
904 } else if ((what & MA_RECURSED) != 0) {
905 panic("mutex %s unrecursed at %s:%d",
906 m->lock_object.lo_name, file, line);
911 panic("mutex %s owned at %s:%d",
912 m->lock_object.lo_name, file, line);
915 panic("unknown mtx_assert at %s:%d", file, line);
921 * General init routine used by the MTX_SYSINIT() macro.
924 mtx_sysinit(void *arg)
926 struct mtx_args *margs = arg;
928 mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
933 * Mutex initialization routine; initialize lock `m' of type contained in
934 * `opts' with options contained in `opts' and name `name.' The optional
935 * lock type `type' is used as a general lock category name for use with
939 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
942 struct lock_class *class;
947 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
948 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
949 ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
950 ("%s: mtx_lock not aligned for %s: %p", __func__, name,
953 /* Determine lock class and lock flags. */
955 class = &lock_class_mtx_spin;
957 class = &lock_class_mtx_sleep;
959 if (opts & MTX_QUIET)
961 if (opts & MTX_RECURSE)
962 flags |= LO_RECURSABLE;
963 if ((opts & MTX_NOWITNESS) == 0)
965 if (opts & MTX_DUPOK)
967 if (opts & MTX_NOPROFILE)
968 flags |= LO_NOPROFILE;
972 /* Initialize mutex. */
973 lock_init(&m->lock_object, class, name, type, flags);
975 m->mtx_lock = MTX_UNOWNED;
980 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be
981 * passed in as a flag here because if the corresponding mtx_init() was
982 * called with MTX_QUIET set, then it will already be set in the mutex's
986 _mtx_destroy(volatile uintptr_t *c)
993 MPASS(mtx_unowned(m));
995 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
997 /* Perform the non-mtx related part of mtx_unlock_spin(). */
998 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1001 TD_LOCKS_DEC(curthread);
1003 lock_profile_release_lock(&m->lock_object);
1004 /* Tell witness this isn't locked to make it happy. */
1005 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1009 m->mtx_lock = MTX_DESTROYED;
1010 lock_destroy(&m->lock_object);
1014 * Intialize the mutex code and system mutexes. This is called from the MD
1015 * startup code prior to mi_startup(). The per-CPU data space needs to be
1016 * setup before this is called.
1022 /* Setup turnstiles so that sleep mutexes work. */
1026 * Initialize mutexes.
1028 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
1029 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
1030 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */
1031 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1032 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
1033 mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
1034 mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
1035 mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
1036 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1042 db_show_mtx(const struct lock_object *lock)
1045 const struct mtx *m;
1047 m = (const struct mtx *)lock;
1049 db_printf(" flags: {");
1050 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1054 if (m->lock_object.lo_flags & LO_RECURSABLE)
1055 db_printf(", RECURSE");
1056 if (m->lock_object.lo_flags & LO_DUPOK)
1057 db_printf(", DUPOK");
1059 db_printf(" state: {");
1061 db_printf("UNOWNED");
1062 else if (mtx_destroyed(m))
1063 db_printf("DESTROYED");
1066 if (m->mtx_lock & MTX_CONTESTED)
1067 db_printf(", CONTESTED");
1068 if (m->mtx_lock & MTX_RECURSED)
1069 db_printf(", RECURSED");
1072 if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1074 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1075 td->td_tid, td->td_proc->p_pid, td->td_name);
1076 if (mtx_recursed(m))
1077 db_printf(" recursed: %d\n", m->mtx_recurse);