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 panic("spin locks can only use msleep_spin");
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)
236 panic("spin locks can only use msleep_spin");
241 owner_mtx(const struct lock_object *lock, struct thread **owner)
246 m = (const struct mtx *)lock;
248 *owner = (struct thread *)(x & ~MTX_FLAGMASK);
249 return (*owner != NULL);
254 * Function versions of the inlined __mtx_* macros. These are used by
255 * modules and can also be called from assembly language if needed.
258 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
265 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
266 !TD_IS_IDLETHREAD(curthread),
267 ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
268 curthread, m->lock_object.lo_name, file, line));
269 KASSERT(m->mtx_lock != MTX_DESTROYED,
270 ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
271 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
272 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
274 WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
275 LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
277 tid = (uintptr_t)curthread;
279 if (!_mtx_obtain_lock_fetch(m, &v, tid))
280 _mtx_lock_sleep(m, v, opts, file, line);
282 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
283 m, 0, 0, file, line);
284 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
286 WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
288 TD_LOCKS_INC(curthread);
292 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
298 KASSERT(m->mtx_lock != MTX_DESTROYED,
299 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
300 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
301 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
303 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
304 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
306 mtx_assert(m, MA_OWNED);
308 #ifdef LOCK_PROFILING
309 __mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
311 __mtx_unlock(m, curthread, opts, file, line);
313 TD_LOCKS_DEC(curthread);
317 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
327 KASSERT(m->mtx_lock != MTX_DESTROYED,
328 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
329 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
330 ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
331 m->lock_object.lo_name, file, line));
333 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
334 (opts & MTX_RECURSE) != 0,
335 ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
336 m->lock_object.lo_name, file, line));
337 opts &= ~MTX_RECURSE;
338 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
342 tid = (uintptr_t)curthread;
344 if (!_mtx_obtain_lock_fetch(m, &v, tid))
345 _mtx_lock_spin(m, v, opts, file, line);
347 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire,
348 m, 0, 0, file, line);
350 __mtx_lock_spin(m, curthread, opts, file, line);
352 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
354 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
358 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
363 if (SCHEDULER_STOPPED())
368 KASSERT(m->mtx_lock != MTX_DESTROYED,
369 ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
370 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
371 ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
372 m->lock_object.lo_name, file, line));
373 KASSERT((opts & MTX_RECURSE) == 0,
374 ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
375 m->lock_object.lo_name, file, line));
376 if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
377 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
378 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
381 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
386 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
393 KASSERT(m->mtx_lock != MTX_DESTROYED,
394 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
395 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
396 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
397 m->lock_object.lo_name, file, line));
398 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
399 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
401 mtx_assert(m, MA_OWNED);
403 __mtx_unlock_spin(m);
407 * The important part of mtx_trylock{,_flags}()
408 * Tries to acquire lock `m.' If this function is called on a mutex that
409 * is already owned, it will recursively acquire the lock.
412 _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
416 #ifdef LOCK_PROFILING
417 uint64_t waittime = 0;
425 if (SCHEDULER_STOPPED_TD(td))
428 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
429 ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
430 curthread, m->lock_object.lo_name, file, line));
431 KASSERT(m->mtx_lock != MTX_DESTROYED,
432 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
433 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
434 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
441 if (_mtx_obtain_lock_fetch(m, &v, tid))
443 if (v == MTX_UNOWNED)
446 ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
447 (opts & MTX_RECURSE) != 0)) {
449 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
457 opts &= ~MTX_RECURSE;
459 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
461 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
463 TD_LOCKS_INC(curthread);
465 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
466 m, contested, waittime, file, line);
473 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
478 return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
482 * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
484 * We call this if the lock is either contested (i.e. we need to go to
485 * sleep waiting for it), or if we need to recurse on it.
489 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
493 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
498 struct turnstile *ts;
500 struct thread *owner;
501 #ifdef LOCK_PROFILING
503 uint64_t waittime = 0;
505 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
506 struct lock_delay_arg lda;
510 int64_t sleep_time = 0;
511 int64_t all_time = 0;
513 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
514 int doing_lockprof = 0;
522 if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
523 while (v == MTX_UNOWNED) {
524 if (_mtx_obtain_lock_fetch(m, &v, tid))
528 all_time -= lockstat_nsecs(&m->lock_object);
531 #ifdef LOCK_PROFILING
535 if (SCHEDULER_STOPPED_TD(td))
538 if (__predict_false(v == MTX_UNOWNED))
539 v = MTX_READ_VALUE(m);
541 if (__predict_false(lv_mtx_owner(v) == td)) {
542 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
543 (opts & MTX_RECURSE) != 0,
544 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
545 m->lock_object.lo_name, file, line));
547 opts &= ~MTX_RECURSE;
550 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
551 if (LOCK_LOG_TEST(&m->lock_object, opts))
552 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
556 opts &= ~MTX_RECURSE;
559 #if defined(ADAPTIVE_MUTEXES)
560 lock_delay_arg_init(&lda, &mtx_delay);
561 #elif defined(KDTRACE_HOOKS)
562 lock_delay_arg_init_noadapt(&lda);
566 PMC_SOFT_CALL( , , lock, failed);
568 lock_profile_obtain_lock_failed(&m->lock_object,
569 &contested, &waittime);
570 if (LOCK_LOG_TEST(&m->lock_object, opts))
572 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
573 m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
576 if (v == MTX_UNOWNED) {
577 if (_mtx_obtain_lock_fetch(m, &v, tid))
584 #ifdef ADAPTIVE_MUTEXES
586 * If the owner is running on another CPU, spin until the
587 * owner stops running or the state of the lock changes.
589 owner = lv_mtx_owner(v);
590 if (TD_IS_RUNNING(owner)) {
591 if (LOCK_LOG_TEST(&m->lock_object, 0))
593 "%s: spinning on %p held by %p",
595 KTR_STATE1(KTR_SCHED, "thread",
596 sched_tdname((struct thread *)tid),
597 "spinning", "lockname:\"%s\"",
598 m->lock_object.lo_name);
601 v = MTX_READ_VALUE(m);
602 owner = lv_mtx_owner(v);
603 } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
604 KTR_STATE0(KTR_SCHED, "thread",
605 sched_tdname((struct thread *)tid),
611 ts = turnstile_trywait(&m->lock_object);
612 v = MTX_READ_VALUE(m);
616 * Check if the lock has been released while spinning for
617 * the turnstile chain lock.
619 if (v == MTX_UNOWNED) {
620 turnstile_cancel(ts);
624 #ifdef ADAPTIVE_MUTEXES
626 * The current lock owner might have started executing
627 * on another CPU (or the lock could have changed
628 * owners) while we were waiting on the turnstile
629 * chain lock. If so, drop the turnstile lock and try
632 owner = lv_mtx_owner(v);
633 if (TD_IS_RUNNING(owner)) {
634 turnstile_cancel(ts);
640 * If the mutex isn't already contested and a failure occurs
641 * setting the contested bit, the mutex was either released
642 * or the state of the MTX_RECURSED bit changed.
644 if ((v & MTX_CONTESTED) == 0 &&
645 !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) {
646 goto retry_turnstile;
650 * We definitely must sleep for this lock.
652 mtx_assert(m, MA_NOTOWNED);
655 * Block on the turnstile.
658 sleep_time -= lockstat_nsecs(&m->lock_object);
660 #ifndef ADAPTIVE_MUTEXES
661 owner = mtx_owner(m);
663 MPASS(owner == mtx_owner(m));
664 turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
666 sleep_time += lockstat_nsecs(&m->lock_object);
669 v = MTX_READ_VALUE(m);
671 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
672 if (__predict_true(!doing_lockprof))
676 all_time += lockstat_nsecs(&m->lock_object);
678 LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
681 * Only record the loops spinning and not sleeping.
683 if (lda.spin_cnt > sleep_cnt)
684 LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
687 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
688 waittime, file, line);
693 * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
695 * This is only called if we need to actually spin for the lock. Recursion
700 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
701 const char *file, int line)
704 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
708 struct lock_delay_arg lda;
710 #ifdef LOCK_PROFILING
712 uint64_t waittime = 0;
715 int64_t spin_time = 0;
717 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
718 int doing_lockprof = 0;
721 tid = (uintptr_t)curthread;
725 if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
726 while (v == MTX_UNOWNED) {
727 if (_mtx_obtain_lock_fetch(m, &v, tid))
731 spin_time -= lockstat_nsecs(&m->lock_object);
734 #ifdef LOCK_PROFILING
738 if (__predict_false(v == MTX_UNOWNED))
739 v = MTX_READ_VALUE(m);
741 if (__predict_false(v == tid)) {
746 if (SCHEDULER_STOPPED())
749 if (LOCK_LOG_TEST(&m->lock_object, opts))
750 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
751 KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
752 "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
754 lock_delay_arg_init(&lda, &mtx_spin_delay);
757 PMC_SOFT_CALL( , , lock, failed);
759 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
762 if (v == MTX_UNOWNED) {
763 if (_mtx_obtain_lock_fetch(m, &v, tid))
767 /* Give interrupts a chance while we spin. */
770 if (__predict_true(lda.spin_cnt < 10000000)) {
773 _mtx_lock_indefinite_check(m, &lda);
775 v = MTX_READ_VALUE(m);
776 } while (v != MTX_UNOWNED);
780 if (LOCK_LOG_TEST(&m->lock_object, opts))
781 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
782 KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
785 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
786 if (__predict_true(!doing_lockprof))
790 spin_time += lockstat_nsecs(&m->lock_object);
791 if (lda.spin_cnt != 0)
792 LOCKSTAT_RECORD1(spin__spin, m, spin_time);
795 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
796 contested, waittime, file, line);
802 thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
805 KASSERT(m->mtx_lock != MTX_DESTROYED,
806 ("thread_lock() of destroyed mutex @ %s:%d", file, line));
807 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
808 ("thread_lock() of sleep mutex %s @ %s:%d",
809 m->lock_object.lo_name, file, line));
810 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) == 0,
811 ("thread_lock: got a recursive mutex %s @ %s:%d\n",
812 m->lock_object.lo_name, file, line));
813 WITNESS_CHECKORDER(&m->lock_object,
814 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
817 #define thread_lock_validate(m, opts, file, line) do { } while (0)
820 #ifndef LOCK_PROFILING
823 _thread_lock(struct thread *td, int opts, const char *file, int line)
826 _thread_lock(struct thread *td)
832 tid = (uintptr_t)curthread;
834 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
838 thread_lock_validate(m, 0, file, line);
839 if (__predict_false(m == &blocked_lock))
840 goto slowpath_unlocked;
841 if (__predict_false(!_mtx_obtain_lock(m, tid)))
842 goto slowpath_unlocked;
843 if (__predict_true(m == td->td_lock)) {
844 WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
847 _mtx_release_lock_quick(m);
852 thread_lock_flags_(td, opts, file, line);
854 thread_lock_flags_(td, 0, 0, 0);
860 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
864 struct lock_delay_arg lda;
865 #ifdef LOCK_PROFILING
867 uint64_t waittime = 0;
870 int64_t spin_time = 0;
872 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
873 int doing_lockprof = 1;
876 tid = (uintptr_t)curthread;
878 if (SCHEDULER_STOPPED()) {
880 * Ensure that spinlock sections are balanced even when the
881 * scheduler is stopped, since we may otherwise inadvertently
882 * re-enable interrupts while dumping core.
888 lock_delay_arg_init(&lda, &mtx_spin_delay);
891 PMC_SOFT_CALL( , , lock, failed);
894 #ifdef LOCK_PROFILING
896 #elif defined(KDTRACE_HOOKS)
897 doing_lockprof = lockstat_enabled;
898 if (__predict_false(doing_lockprof))
899 spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
906 thread_lock_validate(m, opts, file, line);
907 v = MTX_READ_VALUE(m);
909 if (v == MTX_UNOWNED) {
910 if (_mtx_obtain_lock_fetch(m, &v, tid))
915 lock_profile_obtain_lock_failed(&m->lock_object,
916 &contested, &waittime);
917 /* Give interrupts a chance while we spin. */
920 if (__predict_true(lda.spin_cnt < 10000000)) {
923 _mtx_lock_indefinite_check(m, &lda);
925 if (m != td->td_lock) {
929 v = MTX_READ_VALUE(m);
930 } while (v != MTX_UNOWNED);
933 if (m == td->td_lock)
935 _mtx_release_lock_quick(m);
937 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
939 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
941 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
942 if (__predict_true(!doing_lockprof))
946 spin_time += lockstat_nsecs(&m->lock_object);
948 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, contested,
949 waittime, file, line);
951 if (lda.spin_cnt != 0)
952 LOCKSTAT_RECORD1(thread__spin, m, spin_time);
957 thread_lock_block(struct thread *td)
962 mtx_assert(lock, MA_OWNED);
963 td->td_lock = &blocked_lock;
969 thread_lock_unblock(struct thread *td, struct mtx *new)
972 mtx_assert(new, MA_OWNED);
973 KASSERT(td->td_lock == &blocked_lock,
974 ("thread %p lock %p not blocked_lock %p",
975 td, td->td_lock, &blocked_lock));
976 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
980 thread_lock_block_wait(struct thread *td)
983 while (td->td_lock == &blocked_lock)
986 /* Acquire fence to be certain that all thread state is visible. */
987 atomic_thread_fence_acq();
991 thread_lock_set(struct thread *td, struct mtx *new)
995 mtx_assert(new, MA_OWNED);
997 mtx_assert(lock, MA_OWNED);
999 mtx_unlock_spin(lock);
1003 * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
1005 * We are only called here if the lock is recursed, contested (i.e. we
1006 * need to wake up a blocked thread) or lockstat probe is active.
1010 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
1011 const char *file, int line)
1014 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
1018 struct turnstile *ts;
1021 if (SCHEDULER_STOPPED())
1024 tid = (uintptr_t)curthread;
1027 if (__predict_false(v == tid))
1028 v = MTX_READ_VALUE(m);
1030 if (__predict_false(v & MTX_RECURSED)) {
1031 if (--(m->mtx_recurse) == 0)
1032 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1033 if (LOCK_LOG_TEST(&m->lock_object, opts))
1034 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
1038 LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
1039 if (v == tid && _mtx_release_lock(m, tid))
1043 * We have to lock the chain before the turnstile so this turnstile
1044 * can be removed from the hash list if it is empty.
1046 turnstile_chain_lock(&m->lock_object);
1047 _mtx_release_lock_quick(m);
1048 ts = turnstile_lookup(&m->lock_object);
1050 if (LOCK_LOG_TEST(&m->lock_object, opts))
1051 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
1052 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1055 * This turnstile is now no longer associated with the mutex. We can
1056 * unlock the chain lock so a new turnstile may take it's place.
1058 turnstile_unpend(ts);
1059 turnstile_chain_unlock(&m->lock_object);
1063 * All the unlocking of MTX_SPIN locks is done inline.
1064 * See the __mtx_unlock_spin() macro for the details.
1068 * The backing function for the INVARIANTS-enabled mtx_assert()
1070 #ifdef INVARIANT_SUPPORT
1072 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
1074 const struct mtx *m;
1076 if (KERNEL_PANICKED() || dumping || SCHEDULER_STOPPED())
1083 case MA_OWNED | MA_RECURSED:
1084 case MA_OWNED | MA_NOTRECURSED:
1086 panic("mutex %s not owned at %s:%d",
1087 m->lock_object.lo_name, file, line);
1088 if (mtx_recursed(m)) {
1089 if ((what & MA_NOTRECURSED) != 0)
1090 panic("mutex %s recursed at %s:%d",
1091 m->lock_object.lo_name, file, line);
1092 } else if ((what & MA_RECURSED) != 0) {
1093 panic("mutex %s unrecursed at %s:%d",
1094 m->lock_object.lo_name, file, line);
1099 panic("mutex %s owned at %s:%d",
1100 m->lock_object.lo_name, file, line);
1103 panic("unknown mtx_assert at %s:%d", file, line);
1109 * General init routine used by the MTX_SYSINIT() macro.
1112 mtx_sysinit(void *arg)
1114 struct mtx_args *margs = arg;
1116 mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
1121 * Mutex initialization routine; initialize lock `m' of type contained in
1122 * `opts' with options contained in `opts' and name `name.' The optional
1123 * lock type `type' is used as a general lock category name for use with
1127 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
1130 struct lock_class *class;
1135 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1136 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1137 ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1138 ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1141 /* Determine lock class and lock flags. */
1142 if (opts & MTX_SPIN)
1143 class = &lock_class_mtx_spin;
1145 class = &lock_class_mtx_sleep;
1147 if (opts & MTX_QUIET)
1149 if (opts & MTX_RECURSE)
1150 flags |= LO_RECURSABLE;
1151 if ((opts & MTX_NOWITNESS) == 0)
1152 flags |= LO_WITNESS;
1153 if (opts & MTX_DUPOK)
1155 if (opts & MTX_NOPROFILE)
1156 flags |= LO_NOPROFILE;
1160 /* Initialize mutex. */
1161 lock_init(&m->lock_object, class, name, type, flags);
1163 m->mtx_lock = MTX_UNOWNED;
1168 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be
1169 * passed in as a flag here because if the corresponding mtx_init() was
1170 * called with MTX_QUIET set, then it will already be set in the mutex's
1174 _mtx_destroy(volatile uintptr_t *c)
1181 MPASS(mtx_unowned(m));
1183 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
1185 /* Perform the non-mtx related part of mtx_unlock_spin(). */
1186 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1189 TD_LOCKS_DEC(curthread);
1191 lock_profile_release_lock(&m->lock_object);
1192 /* Tell witness this isn't locked to make it happy. */
1193 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1197 m->mtx_lock = MTX_DESTROYED;
1198 lock_destroy(&m->lock_object);
1202 * Intialize the mutex code and system mutexes. This is called from the MD
1203 * startup code prior to mi_startup(). The per-CPU data space needs to be
1204 * setup before this is called.
1210 /* Setup turnstiles so that sleep mutexes work. */
1214 * Initialize mutexes.
1216 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
1217 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
1218 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */
1219 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1220 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
1221 mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
1222 mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
1223 mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
1224 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1228 static void __noinline
1229 _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap)
1234 if (ldap->spin_cnt < 60000000 || kdb_active || KERNEL_PANICKED())
1239 /* If the mutex is unlocked, try again. */
1243 printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
1244 m, m->lock_object.lo_name, td, td->td_tid);
1246 witness_display_spinlock(&m->lock_object, td, printf);
1248 panic("spin lock held too long");
1254 mtx_spin_wait_unlocked(struct mtx *m)
1256 struct lock_delay_arg lda;
1258 KASSERT(m->mtx_lock != MTX_DESTROYED,
1259 ("%s() of destroyed mutex %p", __func__, m));
1260 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
1261 ("%s() of sleep mutex %p (%s)", __func__, m,
1262 m->lock_object.lo_name));
1263 KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1264 m->lock_object.lo_name));
1268 while (atomic_load_acq_ptr(&m->mtx_lock) != MTX_UNOWNED) {
1269 if (__predict_true(lda.spin_cnt < 10000000)) {
1273 _mtx_lock_indefinite_check(m, &lda);
1279 mtx_wait_unlocked(struct mtx *m)
1281 struct thread *owner;
1284 KASSERT(m->mtx_lock != MTX_DESTROYED,
1285 ("%s() of destroyed mutex %p", __func__, m));
1286 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
1287 ("%s() not a sleep mutex %p (%s)", __func__, m,
1288 m->lock_object.lo_name));
1289 KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1290 m->lock_object.lo_name));
1293 v = atomic_load_acq_ptr(&m->mtx_lock);
1294 if (v == MTX_UNOWNED) {
1297 owner = lv_mtx_owner(v);
1298 if (!TD_IS_RUNNING(owner)) {
1309 db_show_mtx(const struct lock_object *lock)
1312 const struct mtx *m;
1314 m = (const struct mtx *)lock;
1316 db_printf(" flags: {");
1317 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1321 if (m->lock_object.lo_flags & LO_RECURSABLE)
1322 db_printf(", RECURSE");
1323 if (m->lock_object.lo_flags & LO_DUPOK)
1324 db_printf(", DUPOK");
1326 db_printf(" state: {");
1328 db_printf("UNOWNED");
1329 else if (mtx_destroyed(m))
1330 db_printf("DESTROYED");
1333 if (m->mtx_lock & MTX_CONTESTED)
1334 db_printf(", CONTESTED");
1335 if (m->mtx_lock & MTX_RECURSED)
1336 db_printf(", RECURSED");
1339 if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1341 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1342 td->td_tid, td->td_proc->p_pid, td->td_name);
1343 if (mtx_recursed(m))
1344 db_printf(" recursed: %d\n", m->mtx_recurse);