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_hwpmc_hooks.h"
43 #include "opt_kdtrace.h"
44 #include "opt_sched.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
51 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
57 #include <sys/resourcevar.h>
58 #include <sys/sched.h>
61 #include <sys/sysctl.h>
62 #include <sys/turnstile.h>
63 #include <sys/vmmeter.h>
64 #include <sys/lock_profile.h>
66 #include <machine/atomic.h>
67 #include <machine/bus.h>
68 #include <machine/cpu.h>
72 #include <fs/devfs/devfs_int.h>
75 #include <vm/vm_extern.h>
77 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
78 #define ADAPTIVE_MUTEXES
82 #include <sys/pmckern.h>
83 PMC_SOFT_DEFINE( , , lock, failed);
87 * Return the mutex address when the lock cookie address is provided.
88 * This functionality assumes that struct mtx* have a member named mtx_lock.
90 #define mtxlock2mtx(c) (__containerof(c, struct mtx, mtx_lock))
93 * Internal utility macros.
95 #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED)
97 #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
99 #define mtx_owner(m) ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
101 static void assert_mtx(const struct lock_object *lock, int what);
103 static void db_show_mtx(const struct lock_object *lock);
105 static void lock_mtx(struct lock_object *lock, uintptr_t how);
106 static void lock_spin(struct lock_object *lock, uintptr_t how);
108 static int owner_mtx(const struct lock_object *lock,
109 struct thread **owner);
111 static uintptr_t unlock_mtx(struct lock_object *lock);
112 static uintptr_t unlock_spin(struct lock_object *lock);
115 * Lock classes for sleep and spin mutexes.
117 struct lock_class lock_class_mtx_sleep = {
118 .lc_name = "sleep mutex",
119 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
120 .lc_assert = assert_mtx,
122 .lc_ddb_show = db_show_mtx,
125 .lc_unlock = unlock_mtx,
127 .lc_owner = owner_mtx,
130 struct lock_class lock_class_mtx_spin = {
131 .lc_name = "spin mutex",
132 .lc_flags = LC_SPINLOCK | LC_RECURSABLE,
133 .lc_assert = assert_mtx,
135 .lc_ddb_show = db_show_mtx,
137 .lc_lock = lock_spin,
138 .lc_unlock = unlock_spin,
140 .lc_owner = owner_mtx,
144 #ifdef ADAPTIVE_MUTEXES
145 static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging");
147 static struct lock_delay_config mtx_delay = {
154 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_initial, CTLFLAG_RW, &mtx_delay.initial,
156 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_step, CTLFLAG_RW, &mtx_delay.step,
158 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_min, CTLFLAG_RW, &mtx_delay.min,
160 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
164 mtx_delay_sysinit(void *dummy)
167 mtx_delay.initial = mp_ncpus * 25;
168 mtx_delay.step = (mp_ncpus * 25) / 2;
169 mtx_delay.min = mp_ncpus * 5;
170 mtx_delay.max = mp_ncpus * 25 * 10;
172 LOCK_DELAY_SYSINIT(mtx_delay_sysinit);
176 * System-wide mutexes
178 struct mtx blocked_lock;
182 assert_mtx(const struct lock_object *lock, int what)
185 mtx_assert((const struct mtx *)lock, what);
189 lock_mtx(struct lock_object *lock, uintptr_t how)
192 mtx_lock((struct mtx *)lock);
196 lock_spin(struct lock_object *lock, uintptr_t how)
199 panic("spin locks can only use msleep_spin");
203 unlock_mtx(struct lock_object *lock)
207 m = (struct mtx *)lock;
208 mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
214 unlock_spin(struct lock_object *lock)
217 panic("spin locks can only use msleep_spin");
222 owner_mtx(const struct lock_object *lock, struct thread **owner)
224 const struct mtx *m = (const struct mtx *)lock;
226 *owner = mtx_owner(m);
227 return (mtx_unowned(m) == 0);
232 * Function versions of the inlined __mtx_* macros. These are used by
233 * modules and can also be called from assembly language if needed.
236 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
240 if (SCHEDULER_STOPPED())
245 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
246 ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
247 curthread, m->lock_object.lo_name, file, line));
248 KASSERT(m->mtx_lock != MTX_DESTROYED,
249 ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
250 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
251 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
253 WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
254 LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
256 __mtx_lock(m, curthread, opts, file, line);
257 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
259 WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
261 curthread->td_locks++;
265 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
269 if (SCHEDULER_STOPPED())
274 KASSERT(m->mtx_lock != MTX_DESTROYED,
275 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
276 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
277 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
279 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
280 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
282 mtx_assert(m, MA_OWNED);
284 if (m->mtx_recurse == 0)
285 LOCKSTAT_PROFILE_RELEASE_LOCK(LS_MTX_UNLOCK_RELEASE, m);
286 __mtx_unlock(m, curthread, opts, file, line);
287 curthread->td_locks--;
291 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
296 if (SCHEDULER_STOPPED())
301 KASSERT(m->mtx_lock != MTX_DESTROYED,
302 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
303 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
304 ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
305 m->lock_object.lo_name, file, line));
307 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
308 (opts & MTX_RECURSE) != 0,
309 ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
310 m->lock_object.lo_name, file, line));
311 opts &= ~MTX_RECURSE;
312 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
314 __mtx_lock_spin(m, curthread, opts, file, line);
315 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
317 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
321 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
326 if (SCHEDULER_STOPPED())
331 KASSERT(m->mtx_lock != MTX_DESTROYED,
332 ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
333 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
334 ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
335 m->lock_object.lo_name, file, line));
336 KASSERT((opts & MTX_RECURSE) == 0,
337 ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
338 m->lock_object.lo_name, file, line));
339 if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
340 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
341 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
344 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
349 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
354 if (SCHEDULER_STOPPED())
359 KASSERT(m->mtx_lock != MTX_DESTROYED,
360 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
361 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
362 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
363 m->lock_object.lo_name, file, line));
364 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
365 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
367 mtx_assert(m, MA_OWNED);
369 __mtx_unlock_spin(m);
373 * The important part of mtx_trylock{,_flags}()
374 * Tries to acquire lock `m.' If this function is called on a mutex that
375 * is already owned, it will recursively acquire the lock.
378 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
381 #ifdef LOCK_PROFILING
382 uint64_t waittime = 0;
387 if (SCHEDULER_STOPPED())
392 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(curthread),
393 ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
394 curthread, m->lock_object.lo_name, file, line));
395 KASSERT(m->mtx_lock != MTX_DESTROYED,
396 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
397 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
398 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
401 if (mtx_owned(m) && ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
402 (opts & MTX_RECURSE) != 0)) {
404 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
407 rval = _mtx_obtain_lock(m, (uintptr_t)curthread);
408 opts &= ~MTX_RECURSE;
410 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
412 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
414 curthread->td_locks++;
415 if (m->mtx_recurse == 0)
416 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_LOCK_ACQUIRE,
417 m, contested, waittime, file, line);
425 * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
427 * We call this if the lock is either contested (i.e. we need to go to
428 * sleep waiting for it), or if we need to recurse on it.
431 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t tid, int opts,
432 const char *file, int line)
435 struct turnstile *ts;
437 #ifdef ADAPTIVE_MUTEXES
438 volatile struct thread *owner;
443 #ifdef LOCK_PROFILING
445 uint64_t waittime = 0;
447 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
448 struct lock_delay_arg lda;
452 int64_t sleep_time = 0;
453 int64_t all_time = 0;
456 if (SCHEDULER_STOPPED())
459 #if defined(ADAPTIVE_MUTEXES)
460 lock_delay_arg_init(&lda, &mtx_delay);
461 #elif defined(KDTRACE_HOOKS)
462 lock_delay_arg_init(&lda, NULL);
467 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
468 (opts & MTX_RECURSE) != 0,
469 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
470 m->lock_object.lo_name, file, line));
471 opts &= ~MTX_RECURSE;
473 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
474 if (LOCK_LOG_TEST(&m->lock_object, opts))
475 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
478 opts &= ~MTX_RECURSE;
481 PMC_SOFT_CALL( , , lock, failed);
483 lock_profile_obtain_lock_failed(&m->lock_object,
484 &contested, &waittime);
485 if (LOCK_LOG_TEST(&m->lock_object, opts))
487 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
488 m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
490 all_time -= lockstat_nsecs(&m->lock_object);
494 if (m->mtx_lock == MTX_UNOWNED && _mtx_obtain_lock(m, tid))
499 #ifdef ADAPTIVE_MUTEXES
501 * If the owner is running on another CPU, spin until the
502 * owner stops running or the state of the lock changes.
505 if (v != MTX_UNOWNED) {
506 owner = (struct thread *)(v & ~MTX_FLAGMASK);
507 if (TD_IS_RUNNING(owner)) {
508 if (LOCK_LOG_TEST(&m->lock_object, 0))
510 "%s: spinning on %p held by %p",
512 KTR_STATE1(KTR_SCHED, "thread",
513 sched_tdname((struct thread *)tid),
514 "spinning", "lockname:\"%s\"",
515 m->lock_object.lo_name);
516 while (mtx_owner(m) == owner &&
517 TD_IS_RUNNING(owner))
519 KTR_STATE0(KTR_SCHED, "thread",
520 sched_tdname((struct thread *)tid),
527 ts = turnstile_trywait(&m->lock_object);
531 * Check if the lock has been released while spinning for
532 * the turnstile chain lock.
534 if (v == MTX_UNOWNED) {
535 turnstile_cancel(ts);
539 #ifdef ADAPTIVE_MUTEXES
541 * The current lock owner might have started executing
542 * on another CPU (or the lock could have changed
543 * owners) while we were waiting on the turnstile
544 * chain lock. If so, drop the turnstile lock and try
547 owner = (struct thread *)(v & ~MTX_FLAGMASK);
548 if (TD_IS_RUNNING(owner)) {
549 turnstile_cancel(ts);
555 * If the mutex isn't already contested and a failure occurs
556 * setting the contested bit, the mutex was either released
557 * or the state of the MTX_RECURSED bit changed.
559 if ((v & MTX_CONTESTED) == 0 &&
560 !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
561 turnstile_cancel(ts);
566 * We definitely must sleep for this lock.
568 mtx_assert(m, MA_NOTOWNED);
573 "contention: %p at %s:%d wants %s, taken by %s:%d",
574 (void *)tid, file, line, m->lock_object.lo_name,
575 WITNESS_FILE(&m->lock_object),
576 WITNESS_LINE(&m->lock_object));
582 * Block on the turnstile.
585 sleep_time -= lockstat_nsecs(&m->lock_object);
587 turnstile_wait(ts, mtx_owner(m), TS_EXCLUSIVE_QUEUE);
589 sleep_time += lockstat_nsecs(&m->lock_object);
594 all_time += lockstat_nsecs(&m->lock_object);
599 "contention end: %s acquired by %p at %s:%d",
600 m->lock_object.lo_name, (void *)tid, file, line);
603 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_LOCK_ACQUIRE, m, contested,
604 waittime, file, line);
607 LOCKSTAT_RECORD1(LS_MTX_LOCK_BLOCK, m, sleep_time);
610 * Only record the loops spinning and not sleeping.
612 if (lda.spin_cnt > sleep_cnt)
613 LOCKSTAT_RECORD1(LS_MTX_LOCK_SPIN, m, (all_time - sleep_time));
618 _mtx_lock_spin_failed(struct mtx *m)
624 /* If the mutex is unlocked, try again. */
628 printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
629 m, m->lock_object.lo_name, td, td->td_tid);
631 witness_display_spinlock(&m->lock_object, td, printf);
633 panic("spin lock held too long");
638 * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
640 * This is only called if we need to actually spin for the lock. Recursion
644 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t tid, int opts,
645 const char *file, int line)
649 #ifdef LOCK_PROFILING
651 uint64_t waittime = 0;
654 int64_t spin_time = 0;
657 if (SCHEDULER_STOPPED())
662 if (LOCK_LOG_TEST(&m->lock_object, opts))
663 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
664 KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
665 "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
668 PMC_SOFT_CALL( , , lock, failed);
670 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
672 spin_time -= lockstat_nsecs(&m->lock_object);
675 if (m->mtx_lock == MTX_UNOWNED && _mtx_obtain_lock(m, tid))
677 /* Give interrupts a chance while we spin. */
679 while (m->mtx_lock != MTX_UNOWNED) {
680 if (i++ < 10000000) {
684 if (i < 60000000 || kdb_active || panicstr != NULL)
687 _mtx_lock_spin_failed(m);
693 spin_time += lockstat_nsecs(&m->lock_object);
696 if (LOCK_LOG_TEST(&m->lock_object, opts))
697 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
698 KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
701 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_SPIN_LOCK_ACQUIRE, m,
702 contested, waittime, (file), (line));
705 LOCKSTAT_RECORD1(LS_MTX_SPIN_LOCK_SPIN, m, spin_time);
711 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
716 #ifdef LOCK_PROFILING
718 uint64_t waittime = 0;
721 int64_t spin_time = 0;
725 tid = (uintptr_t)curthread;
727 if (SCHEDULER_STOPPED()) {
729 * Ensure that spinlock sections are balanced even when the
730 * scheduler is stopped, since we may otherwise inadvertently
731 * re-enable interrupts while dumping core.
738 spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
744 KASSERT(m->mtx_lock != MTX_DESTROYED,
745 ("thread_lock() of destroyed mutex @ %s:%d", file, line));
746 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
747 ("thread_lock() of sleep mutex %s @ %s:%d",
748 m->lock_object.lo_name, file, line));
750 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
751 ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
752 m->lock_object.lo_name, file, line));
753 WITNESS_CHECKORDER(&m->lock_object,
754 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
756 if (m->mtx_lock == MTX_UNOWNED && _mtx_obtain_lock(m, tid))
758 if (m->mtx_lock == tid) {
763 PMC_SOFT_CALL( , , lock, failed);
765 lock_profile_obtain_lock_failed(&m->lock_object,
766 &contested, &waittime);
767 /* Give interrupts a chance while we spin. */
769 while (m->mtx_lock != MTX_UNOWNED) {
772 else if (i < 60000000 ||
773 kdb_active || panicstr != NULL)
776 _mtx_lock_spin_failed(m);
778 if (m != td->td_lock)
783 if (m == td->td_lock)
785 __mtx_unlock_spin(m); /* does spinlock_exit() */
788 spin_time += lockstat_nsecs(&m->lock_object);
790 if (m->mtx_recurse == 0)
791 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(LS_MTX_SPIN_LOCK_ACQUIRE,
792 m, contested, waittime, (file), (line));
793 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
795 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
796 LOCKSTAT_RECORD1(LS_THREAD_LOCK_SPIN, m, spin_time);
800 thread_lock_block(struct thread *td)
804 THREAD_LOCK_ASSERT(td, MA_OWNED);
806 td->td_lock = &blocked_lock;
807 mtx_unlock_spin(lock);
813 thread_lock_unblock(struct thread *td, struct mtx *new)
815 mtx_assert(new, MA_OWNED);
816 MPASS(td->td_lock == &blocked_lock);
817 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
821 thread_lock_set(struct thread *td, struct mtx *new)
825 mtx_assert(new, MA_OWNED);
826 THREAD_LOCK_ASSERT(td, MA_OWNED);
829 mtx_unlock_spin(lock);
833 * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
835 * We are only called here if the lock is recursed or contested (i.e. we
836 * need to wake up a blocked thread).
839 __mtx_unlock_sleep(volatile uintptr_t *c, int opts, const char *file, int line)
842 struct turnstile *ts;
844 if (SCHEDULER_STOPPED())
849 if (mtx_recursed(m)) {
850 if (--(m->mtx_recurse) == 0)
851 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
852 if (LOCK_LOG_TEST(&m->lock_object, opts))
853 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
858 * We have to lock the chain before the turnstile so this turnstile
859 * can be removed from the hash list if it is empty.
861 turnstile_chain_lock(&m->lock_object);
862 ts = turnstile_lookup(&m->lock_object);
863 if (LOCK_LOG_TEST(&m->lock_object, opts))
864 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
866 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
867 _mtx_release_lock_quick(m);
870 * This turnstile is now no longer associated with the mutex. We can
871 * unlock the chain lock so a new turnstile may take it's place.
873 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
874 turnstile_chain_unlock(&m->lock_object);
878 * All the unlocking of MTX_SPIN locks is done inline.
879 * See the __mtx_unlock_spin() macro for the details.
883 * The backing function for the INVARIANTS-enabled mtx_assert()
885 #ifdef INVARIANT_SUPPORT
887 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
891 if (panicstr != NULL || dumping || SCHEDULER_STOPPED())
898 case MA_OWNED | MA_RECURSED:
899 case MA_OWNED | MA_NOTRECURSED:
901 panic("mutex %s not owned at %s:%d",
902 m->lock_object.lo_name, file, line);
903 if (mtx_recursed(m)) {
904 if ((what & MA_NOTRECURSED) != 0)
905 panic("mutex %s recursed at %s:%d",
906 m->lock_object.lo_name, file, line);
907 } else if ((what & MA_RECURSED) != 0) {
908 panic("mutex %s unrecursed at %s:%d",
909 m->lock_object.lo_name, file, line);
914 panic("mutex %s owned at %s:%d",
915 m->lock_object.lo_name, file, line);
918 panic("unknown mtx_assert at %s:%d", file, line);
924 * The MUTEX_DEBUG-enabled mtx_validate()
926 * Most of these checks have been moved off into the LO_INITIALIZED flag
927 * maintained by the witness code.
931 void mtx_validate(struct mtx *);
934 mtx_validate(struct mtx *m)
938 * XXX: When kernacc() does not require Giant we can reenable this check
942 * Can't call kernacc() from early init386(), especially when
943 * initializing Giant mutex, because some stuff in kernacc()
944 * requires Giant itself.
947 if (!kernacc((caddr_t)m, sizeof(m),
948 VM_PROT_READ | VM_PROT_WRITE))
949 panic("Can't read and write to mutex %p", m);
955 * General init routine used by the MTX_SYSINIT() macro.
958 mtx_sysinit(void *arg)
960 struct mtx_args *margs = arg;
962 mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
967 * Mutex initialization routine; initialize lock `m' of type contained in
968 * `opts' with options contained in `opts' and name `name.' The optional
969 * lock type `type' is used as a general lock category name for use with
973 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
976 struct lock_class *class;
981 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
982 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
983 ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
984 ("%s: mtx_lock not aligned for %s: %p", __func__, name,
988 /* Diagnostic and error correction */
992 /* Determine lock class and lock flags. */
994 class = &lock_class_mtx_spin;
996 class = &lock_class_mtx_sleep;
998 if (opts & MTX_QUIET)
1000 if (opts & MTX_RECURSE)
1001 flags |= LO_RECURSABLE;
1002 if ((opts & MTX_NOWITNESS) == 0)
1003 flags |= LO_WITNESS;
1004 if (opts & MTX_DUPOK)
1006 if (opts & MTX_NOPROFILE)
1007 flags |= LO_NOPROFILE;
1011 /* Initialize mutex. */
1012 lock_init(&m->lock_object, class, name, type, flags);
1014 m->mtx_lock = MTX_UNOWNED;
1019 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be
1020 * passed in as a flag here because if the corresponding mtx_init() was
1021 * called with MTX_QUIET set, then it will already be set in the mutex's
1025 _mtx_destroy(volatile uintptr_t *c)
1032 MPASS(mtx_unowned(m));
1034 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
1036 /* Perform the non-mtx related part of mtx_unlock_spin(). */
1037 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1040 curthread->td_locks--;
1042 lock_profile_release_lock(&m->lock_object);
1043 /* Tell witness this isn't locked to make it happy. */
1044 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1048 m->mtx_lock = MTX_DESTROYED;
1049 lock_destroy(&m->lock_object);
1053 * Intialize the mutex code and system mutexes. This is called from the MD
1054 * startup code prior to mi_startup(). The per-CPU data space needs to be
1055 * setup before this is called.
1061 /* Setup turnstiles so that sleep mutexes work. */
1065 * Initialize mutexes.
1067 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
1068 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
1069 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */
1070 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1071 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN | MTX_RECURSE);
1072 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1078 db_show_mtx(const struct lock_object *lock)
1081 const struct mtx *m;
1083 m = (const struct mtx *)lock;
1085 db_printf(" flags: {");
1086 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1090 if (m->lock_object.lo_flags & LO_RECURSABLE)
1091 db_printf(", RECURSE");
1092 if (m->lock_object.lo_flags & LO_DUPOK)
1093 db_printf(", DUPOK");
1095 db_printf(" state: {");
1097 db_printf("UNOWNED");
1098 else if (mtx_destroyed(m))
1099 db_printf("DESTROYED");
1102 if (m->mtx_lock & MTX_CONTESTED)
1103 db_printf(", CONTESTED");
1104 if (m->mtx_lock & MTX_RECURSED)
1105 db_printf(", RECURSED");
1108 if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1110 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1111 td->td_tid, td->td_proc->p_pid, td->td_name);
1112 if (mtx_recursed(m))
1113 db_printf(" recursed: %d\n", m->mtx_recurse);