2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Berkeley Software Design Inc's name may not be used to endorse or
15 * promote products derived from this software without specific prior
18 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
31 * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
35 * Machine independent bits of mutex implementation.
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "opt_adaptive_mutexes.h"
43 #include "opt_hwpmc_hooks.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 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 #ifdef MUTEX_CUSTOM_BACKOFF
144 static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
147 static struct lock_delay_config __read_frequently mtx_delay;
149 SYSCTL_U16(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
151 SYSCTL_U16(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
154 LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
156 #define mtx_delay locks_delay
160 #ifdef MUTEX_SPIN_CUSTOM_BACKOFF
161 static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin,
162 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
163 "mtx spin debugging");
165 static struct lock_delay_config __read_frequently mtx_spin_delay;
167 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW,
168 &mtx_spin_delay.base, 0, "");
169 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW,
170 &mtx_spin_delay.max, 0, "");
172 LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
174 #define mtx_spin_delay locks_delay
178 * System-wide mutexes
180 struct mtx blocked_lock;
181 struct mtx __exclusive_cache_line Giant;
183 static void _mtx_lock_indefinite_check(struct mtx *, struct lock_delay_arg *);
186 assert_mtx(const struct lock_object *lock, int what)
190 * Treat LA_LOCKED as if LA_XLOCKED was asserted.
192 * Some callers of lc_assert uses LA_LOCKED to indicate that either
193 * a shared lock or write lock was held, while other callers uses
194 * the more strict LA_XLOCKED (used as MA_OWNED).
196 * Mutex is the only lock class that can not be shared, as a result,
197 * we can reasonably consider the caller really intends to assert
198 * LA_XLOCKED when they are asserting LA_LOCKED on a mutex object.
200 if (what & LA_LOCKED) {
204 mtx_assert((const struct mtx *)lock, what);
208 lock_mtx(struct lock_object *lock, uintptr_t how)
211 mtx_lock((struct mtx *)lock);
215 lock_spin(struct lock_object *lock, uintptr_t how)
218 mtx_lock_spin((struct mtx *)lock);
222 unlock_mtx(struct lock_object *lock)
226 m = (struct mtx *)lock;
227 mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
233 unlock_spin(struct lock_object *lock)
237 m = (struct mtx *)lock;
238 mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
245 owner_mtx(const struct lock_object *lock, struct thread **owner)
250 m = (const struct mtx *)lock;
252 *owner = (struct thread *)(x & ~MTX_FLAGMASK);
253 return (*owner != NULL);
258 * Function versions of the inlined __mtx_* macros. These are used by
259 * modules and can also be called from assembly language if needed.
262 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
269 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
270 !TD_IS_IDLETHREAD(curthread),
271 ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
272 curthread, m->lock_object.lo_name, file, line));
273 KASSERT(m->mtx_lock != MTX_DESTROYED,
274 ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
275 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
276 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
278 WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
279 LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
281 tid = (uintptr_t)curthread;
283 if (!_mtx_obtain_lock_fetch(m, &v, tid))
284 _mtx_lock_sleep(m, v, opts, file, line);
286 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
287 m, 0, 0, file, line);
288 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
290 WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
292 TD_LOCKS_INC(curthread);
296 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
302 KASSERT(m->mtx_lock != MTX_DESTROYED,
303 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
304 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
305 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
307 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
308 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
310 mtx_assert(m, MA_OWNED);
312 #ifdef LOCK_PROFILING
313 __mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
315 __mtx_unlock(m, curthread, opts, file, line);
317 TD_LOCKS_DEC(curthread);
321 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
331 KASSERT(m->mtx_lock != MTX_DESTROYED,
332 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
333 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
334 ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
335 m->lock_object.lo_name, file, line));
337 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
338 (opts & MTX_RECURSE) != 0,
339 ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
340 m->lock_object.lo_name, file, line));
341 opts &= ~MTX_RECURSE;
342 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
346 tid = (uintptr_t)curthread;
348 if (!_mtx_obtain_lock_fetch(m, &v, tid))
349 _mtx_lock_spin(m, v, opts, file, line);
351 LOCKSTAT_PROFILE_OBTAIN_SPIN_LOCK_SUCCESS(spin__acquire,
352 m, 0, 0, file, line);
354 __mtx_lock_spin(m, curthread, opts, file, line);
356 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
358 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
362 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
367 if (SCHEDULER_STOPPED())
372 KASSERT(m->mtx_lock != MTX_DESTROYED,
373 ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
374 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
375 ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
376 m->lock_object.lo_name, file, line));
377 KASSERT((opts & MTX_RECURSE) == 0,
378 ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
379 m->lock_object.lo_name, file, line));
380 if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
381 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
382 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
385 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
390 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
397 KASSERT(m->mtx_lock != MTX_DESTROYED,
398 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
399 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
400 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
401 m->lock_object.lo_name, file, line));
402 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
403 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
405 mtx_assert(m, MA_OWNED);
407 __mtx_unlock_spin(m);
411 * The important part of mtx_trylock{,_flags}()
412 * Tries to acquire lock `m.' If this function is called on a mutex that
413 * is already owned, it will recursively acquire the lock.
416 _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
420 #ifdef LOCK_PROFILING
421 uint64_t waittime = 0;
429 if (SCHEDULER_STOPPED_TD(td))
432 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
433 ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
434 curthread, m->lock_object.lo_name, file, line));
435 KASSERT(m->mtx_lock != MTX_DESTROYED,
436 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
437 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
438 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
445 if (_mtx_obtain_lock_fetch(m, &v, tid))
447 if (v == MTX_UNOWNED)
450 ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
451 (opts & MTX_RECURSE) != 0)) {
453 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
461 opts &= ~MTX_RECURSE;
463 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
465 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
467 TD_LOCKS_INC(curthread);
469 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
470 m, contested, waittime, file, line);
477 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
482 return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
486 * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
488 * We call this if the lock is either contested (i.e. we need to go to
489 * sleep waiting for it), or if we need to recurse on it.
493 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
497 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
502 struct turnstile *ts;
504 struct thread *owner;
505 #ifdef LOCK_PROFILING
507 uint64_t waittime = 0;
509 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
510 struct lock_delay_arg lda;
514 int64_t sleep_time = 0;
515 int64_t all_time = 0;
517 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
518 int doing_lockprof = 0;
526 if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
527 while (v == MTX_UNOWNED) {
528 if (_mtx_obtain_lock_fetch(m, &v, tid))
532 all_time -= lockstat_nsecs(&m->lock_object);
535 #ifdef LOCK_PROFILING
539 if (SCHEDULER_STOPPED_TD(td))
542 if (__predict_false(v == MTX_UNOWNED))
543 v = MTX_READ_VALUE(m);
545 if (__predict_false(lv_mtx_owner(v) == td)) {
546 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
547 (opts & MTX_RECURSE) != 0,
548 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
549 m->lock_object.lo_name, file, line));
551 opts &= ~MTX_RECURSE;
554 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
555 if (LOCK_LOG_TEST(&m->lock_object, opts))
556 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
560 opts &= ~MTX_RECURSE;
563 #if defined(ADAPTIVE_MUTEXES)
564 lock_delay_arg_init(&lda, &mtx_delay);
565 #elif defined(KDTRACE_HOOKS)
566 lock_delay_arg_init_noadapt(&lda);
570 PMC_SOFT_CALL( , , lock, failed);
572 lock_profile_obtain_lock_failed(&m->lock_object, false,
573 &contested, &waittime);
574 if (LOCK_LOG_TEST(&m->lock_object, opts))
576 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
577 m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
580 if (v == MTX_UNOWNED) {
581 if (_mtx_obtain_lock_fetch(m, &v, tid))
588 #ifdef ADAPTIVE_MUTEXES
590 * If the owner is running on another CPU, spin until the
591 * owner stops running or the state of the lock changes.
593 owner = lv_mtx_owner(v);
594 if (TD_IS_RUNNING(owner)) {
595 if (LOCK_LOG_TEST(&m->lock_object, 0))
597 "%s: spinning on %p held by %p",
599 KTR_STATE1(KTR_SCHED, "thread",
600 sched_tdname((struct thread *)tid),
601 "spinning", "lockname:\"%s\"",
602 m->lock_object.lo_name);
605 v = MTX_READ_VALUE(m);
606 owner = lv_mtx_owner(v);
607 } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
608 KTR_STATE0(KTR_SCHED, "thread",
609 sched_tdname((struct thread *)tid),
615 ts = turnstile_trywait(&m->lock_object);
616 v = MTX_READ_VALUE(m);
620 * Check if the lock has been released while spinning for
621 * the turnstile chain lock.
623 if (v == MTX_UNOWNED) {
624 turnstile_cancel(ts);
628 #ifdef ADAPTIVE_MUTEXES
630 * The current lock owner might have started executing
631 * on another CPU (or the lock could have changed
632 * owners) while we were waiting on the turnstile
633 * chain lock. If so, drop the turnstile lock and try
636 owner = lv_mtx_owner(v);
637 if (TD_IS_RUNNING(owner)) {
638 turnstile_cancel(ts);
644 * If the mutex isn't already contested and a failure occurs
645 * setting the contested bit, the mutex was either released
646 * or the state of the MTX_RECURSED bit changed.
648 if ((v & MTX_CONTESTED) == 0 &&
649 !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) {
650 goto retry_turnstile;
654 * We definitely must sleep for this lock.
656 mtx_assert(m, MA_NOTOWNED);
659 * Block on the turnstile.
662 sleep_time -= lockstat_nsecs(&m->lock_object);
664 #ifndef ADAPTIVE_MUTEXES
665 owner = mtx_owner(m);
667 MPASS(owner == mtx_owner(m));
668 turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
670 sleep_time += lockstat_nsecs(&m->lock_object);
673 v = MTX_READ_VALUE(m);
675 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
676 if (__predict_true(!doing_lockprof))
680 all_time += lockstat_nsecs(&m->lock_object);
682 LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
685 * Only record the loops spinning and not sleeping.
687 if (lda.spin_cnt > sleep_cnt)
688 LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
691 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
692 waittime, file, line);
697 * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
699 * This is only called if we need to actually spin for the lock. Recursion
704 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
705 const char *file, int line)
708 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
712 struct lock_delay_arg lda;
714 #ifdef LOCK_PROFILING
716 uint64_t waittime = 0;
719 int64_t spin_time = 0;
721 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
722 int doing_lockprof = 0;
725 tid = (uintptr_t)curthread;
729 if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
730 while (v == MTX_UNOWNED) {
731 if (_mtx_obtain_lock_fetch(m, &v, tid))
735 spin_time -= lockstat_nsecs(&m->lock_object);
738 #ifdef LOCK_PROFILING
742 if (__predict_false(v == MTX_UNOWNED))
743 v = MTX_READ_VALUE(m);
745 if (__predict_false(v == tid)) {
750 if (SCHEDULER_STOPPED())
753 if (LOCK_LOG_TEST(&m->lock_object, opts))
754 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
755 KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
756 "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
758 lock_delay_arg_init(&lda, &mtx_spin_delay);
761 PMC_SOFT_CALL( , , lock, failed);
763 lock_profile_obtain_lock_failed(&m->lock_object, true, &contested, &waittime);
766 if (v == MTX_UNOWNED) {
767 if (_mtx_obtain_lock_fetch(m, &v, tid))
771 /* Give interrupts a chance while we spin. */
774 if (__predict_true(lda.spin_cnt < 10000000)) {
777 _mtx_lock_indefinite_check(m, &lda);
779 v = MTX_READ_VALUE(m);
780 } while (v != MTX_UNOWNED);
784 if (LOCK_LOG_TEST(&m->lock_object, opts))
785 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
786 KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
789 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
790 if (__predict_true(!doing_lockprof))
794 spin_time += lockstat_nsecs(&m->lock_object);
795 if (lda.spin_cnt != 0)
796 LOCKSTAT_RECORD1(spin__spin, m, spin_time);
799 LOCKSTAT_PROFILE_OBTAIN_SPIN_LOCK_SUCCESS(spin__acquire, m,
800 contested, waittime, file, line);
806 thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
809 KASSERT(m->mtx_lock != MTX_DESTROYED,
810 ("thread_lock() of destroyed mutex @ %s:%d", file, line));
811 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
812 ("thread_lock() of sleep mutex %s @ %s:%d",
813 m->lock_object.lo_name, file, line));
814 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) == 0,
815 ("thread_lock: got a recursive mutex %s @ %s:%d\n",
816 m->lock_object.lo_name, file, line));
817 WITNESS_CHECKORDER(&m->lock_object,
818 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
821 #define thread_lock_validate(m, opts, file, line) do { } while (0)
824 #ifndef LOCK_PROFILING
827 _thread_lock(struct thread *td, int opts, const char *file, int line)
830 _thread_lock(struct thread *td)
836 tid = (uintptr_t)curthread;
838 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
842 thread_lock_validate(m, 0, file, line);
843 if (__predict_false(m == &blocked_lock))
844 goto slowpath_unlocked;
845 if (__predict_false(!_mtx_obtain_lock(m, tid)))
846 goto slowpath_unlocked;
847 if (__predict_true(m == td->td_lock)) {
848 WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
851 _mtx_release_lock_quick(m);
856 thread_lock_flags_(td, opts, file, line);
858 thread_lock_flags_(td, 0, 0, 0);
864 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
868 struct lock_delay_arg lda;
869 #ifdef LOCK_PROFILING
871 uint64_t waittime = 0;
874 int64_t spin_time = 0;
876 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
877 int doing_lockprof = 1;
880 tid = (uintptr_t)curthread;
882 if (SCHEDULER_STOPPED()) {
884 * Ensure that spinlock sections are balanced even when the
885 * scheduler is stopped, since we may otherwise inadvertently
886 * re-enable interrupts while dumping core.
892 lock_delay_arg_init(&lda, &mtx_spin_delay);
895 PMC_SOFT_CALL( , , lock, failed);
898 #ifdef LOCK_PROFILING
900 #elif defined(KDTRACE_HOOKS)
901 doing_lockprof = lockstat_enabled;
902 if (__predict_false(doing_lockprof))
903 spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
910 thread_lock_validate(m, opts, file, line);
911 v = MTX_READ_VALUE(m);
913 if (v == MTX_UNOWNED) {
914 if (_mtx_obtain_lock_fetch(m, &v, tid))
919 lock_profile_obtain_lock_failed(&m->lock_object, true,
920 &contested, &waittime);
921 /* Give interrupts a chance while we spin. */
924 if (__predict_true(lda.spin_cnt < 10000000)) {
927 _mtx_lock_indefinite_check(m, &lda);
929 if (m != td->td_lock) {
933 v = MTX_READ_VALUE(m);
934 } while (v != MTX_UNOWNED);
937 if (m == td->td_lock)
939 _mtx_release_lock_quick(m);
941 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
943 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
945 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
946 if (__predict_true(!doing_lockprof))
950 spin_time += lockstat_nsecs(&m->lock_object);
952 LOCKSTAT_PROFILE_OBTAIN_SPIN_LOCK_SUCCESS(spin__acquire, m, contested,
953 waittime, file, line);
955 if (lda.spin_cnt != 0)
956 LOCKSTAT_RECORD1(thread__spin, m, spin_time);
961 thread_lock_block(struct thread *td)
966 mtx_assert(lock, MA_OWNED);
967 td->td_lock = &blocked_lock;
973 thread_lock_unblock(struct thread *td, struct mtx *new)
976 mtx_assert(new, MA_OWNED);
977 KASSERT(td->td_lock == &blocked_lock,
978 ("thread %p lock %p not blocked_lock %p",
979 td, td->td_lock, &blocked_lock));
980 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
984 thread_lock_block_wait(struct thread *td)
987 while (td->td_lock == &blocked_lock)
990 /* Acquire fence to be certain that all thread state is visible. */
991 atomic_thread_fence_acq();
995 thread_lock_set(struct thread *td, struct mtx *new)
999 mtx_assert(new, MA_OWNED);
1001 mtx_assert(lock, MA_OWNED);
1003 mtx_unlock_spin(lock);
1007 * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
1009 * We are only called here if the lock is recursed, contested (i.e. we
1010 * need to wake up a blocked thread) or lockstat probe is active.
1014 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
1015 const char *file, int line)
1018 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
1022 struct turnstile *ts;
1025 if (SCHEDULER_STOPPED())
1028 tid = (uintptr_t)curthread;
1031 if (__predict_false(v == tid))
1032 v = MTX_READ_VALUE(m);
1034 if (__predict_false(v & MTX_RECURSED)) {
1035 if (--(m->mtx_recurse) == 0)
1036 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1037 if (LOCK_LOG_TEST(&m->lock_object, opts))
1038 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
1042 LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
1043 if (v == tid && _mtx_release_lock(m, tid))
1047 * We have to lock the chain before the turnstile so this turnstile
1048 * can be removed from the hash list if it is empty.
1050 turnstile_chain_lock(&m->lock_object);
1051 _mtx_release_lock_quick(m);
1052 ts = turnstile_lookup(&m->lock_object);
1054 if (LOCK_LOG_TEST(&m->lock_object, opts))
1055 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
1056 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1059 * This turnstile is now no longer associated with the mutex. We can
1060 * unlock the chain lock so a new turnstile may take it's place.
1062 turnstile_unpend(ts);
1063 turnstile_chain_unlock(&m->lock_object);
1067 * All the unlocking of MTX_SPIN locks is done inline.
1068 * See the __mtx_unlock_spin() macro for the details.
1072 * The backing function for the INVARIANTS-enabled mtx_assert()
1074 #ifdef INVARIANT_SUPPORT
1076 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
1078 const struct mtx *m;
1080 if (KERNEL_PANICKED() || dumping || SCHEDULER_STOPPED())
1087 case MA_OWNED | MA_RECURSED:
1088 case MA_OWNED | MA_NOTRECURSED:
1090 panic("mutex %s not owned at %s:%d",
1091 m->lock_object.lo_name, file, line);
1092 if (mtx_recursed(m)) {
1093 if ((what & MA_NOTRECURSED) != 0)
1094 panic("mutex %s recursed at %s:%d",
1095 m->lock_object.lo_name, file, line);
1096 } else if ((what & MA_RECURSED) != 0) {
1097 panic("mutex %s unrecursed at %s:%d",
1098 m->lock_object.lo_name, file, line);
1103 panic("mutex %s owned at %s:%d",
1104 m->lock_object.lo_name, file, line);
1107 panic("unknown mtx_assert at %s:%d", file, line);
1113 * General init routine used by the MTX_SYSINIT() macro.
1116 mtx_sysinit(void *arg)
1118 struct mtx_args *margs = arg;
1120 mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
1125 * Mutex initialization routine; initialize lock `m' of type contained in
1126 * `opts' with options contained in `opts' and name `name.' The optional
1127 * lock type `type' is used as a general lock category name for use with
1131 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
1134 struct lock_class *class;
1139 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1140 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1141 ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1142 ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1145 /* Determine lock class and lock flags. */
1146 if (opts & MTX_SPIN)
1147 class = &lock_class_mtx_spin;
1149 class = &lock_class_mtx_sleep;
1151 if (opts & MTX_QUIET)
1153 if (opts & MTX_RECURSE)
1154 flags |= LO_RECURSABLE;
1155 if ((opts & MTX_NOWITNESS) == 0)
1156 flags |= LO_WITNESS;
1157 if (opts & MTX_DUPOK)
1159 if (opts & MTX_NOPROFILE)
1160 flags |= LO_NOPROFILE;
1164 /* Initialize mutex. */
1165 lock_init(&m->lock_object, class, name, type, flags);
1167 m->mtx_lock = MTX_UNOWNED;
1172 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be
1173 * passed in as a flag here because if the corresponding mtx_init() was
1174 * called with MTX_QUIET set, then it will already be set in the mutex's
1178 _mtx_destroy(volatile uintptr_t *c)
1185 MPASS(mtx_unowned(m));
1187 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
1189 /* Perform the non-mtx related part of mtx_unlock_spin(). */
1190 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin) {
1191 lock_profile_release_lock(&m->lock_object, true);
1194 TD_LOCKS_DEC(curthread);
1195 lock_profile_release_lock(&m->lock_object, false);
1198 /* Tell witness this isn't locked to make it happy. */
1199 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1203 m->mtx_lock = MTX_DESTROYED;
1204 lock_destroy(&m->lock_object);
1208 * Intialize the mutex code and system mutexes. This is called from the MD
1209 * startup code prior to mi_startup(). The per-CPU data space needs to be
1210 * setup before this is called.
1216 /* Setup turnstiles so that sleep mutexes work. */
1220 * Initialize mutexes.
1222 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
1223 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
1224 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */
1225 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1226 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
1227 mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
1228 mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
1229 mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
1230 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1234 static void __noinline
1235 _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap)
1240 if (ldap->spin_cnt < 60000000 || kdb_active || KERNEL_PANICKED())
1245 /* If the mutex is unlocked, try again. */
1249 printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
1250 m, m->lock_object.lo_name, td, td->td_tid);
1252 witness_display_spinlock(&m->lock_object, td, printf);
1254 panic("spin lock held too long");
1260 mtx_spin_wait_unlocked(struct mtx *m)
1262 struct lock_delay_arg lda;
1264 KASSERT(m->mtx_lock != MTX_DESTROYED,
1265 ("%s() of destroyed mutex %p", __func__, m));
1266 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
1267 ("%s() of sleep mutex %p (%s)", __func__, m,
1268 m->lock_object.lo_name));
1269 KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1270 m->lock_object.lo_name));
1274 while (atomic_load_acq_ptr(&m->mtx_lock) != MTX_UNOWNED) {
1275 if (__predict_true(lda.spin_cnt < 10000000)) {
1279 _mtx_lock_indefinite_check(m, &lda);
1285 mtx_wait_unlocked(struct mtx *m)
1287 struct thread *owner;
1290 KASSERT(m->mtx_lock != MTX_DESTROYED,
1291 ("%s() of destroyed mutex %p", __func__, m));
1292 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
1293 ("%s() not a sleep mutex %p (%s)", __func__, m,
1294 m->lock_object.lo_name));
1295 KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1296 m->lock_object.lo_name));
1299 v = atomic_load_acq_ptr(&m->mtx_lock);
1300 if (v == MTX_UNOWNED) {
1303 owner = lv_mtx_owner(v);
1304 if (!TD_IS_RUNNING(owner)) {
1315 db_show_mtx(const struct lock_object *lock)
1318 const struct mtx *m;
1320 m = (const struct mtx *)lock;
1322 db_printf(" flags: {");
1323 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1327 if (m->lock_object.lo_flags & LO_RECURSABLE)
1328 db_printf(", RECURSE");
1329 if (m->lock_object.lo_flags & LO_DUPOK)
1330 db_printf(", DUPOK");
1332 db_printf(" state: {");
1334 db_printf("UNOWNED");
1335 else if (mtx_destroyed(m))
1336 db_printf("DESTROYED");
1339 if (m->mtx_lock & MTX_CONTESTED)
1340 db_printf(", CONTESTED");
1341 if (m->mtx_lock & MTX_RECURSED)
1342 db_printf(", RECURSED");
1345 if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1347 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1348 td->td_tid, td->td_proc->p_pid, td->td_name);
1349 if (mtx_recursed(m))
1350 db_printf(" recursed: %d\n", m->mtx_recurse);