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 static void assert_mtx(const struct lock_object *lock, int what);
99 static void db_show_mtx(const struct lock_object *lock);
101 static void lock_mtx(struct lock_object *lock, uintptr_t how);
102 static void lock_spin(struct lock_object *lock, uintptr_t how);
104 static int owner_mtx(const struct lock_object *lock,
105 struct thread **owner);
107 static uintptr_t unlock_mtx(struct lock_object *lock);
108 static uintptr_t unlock_spin(struct lock_object *lock);
111 * Lock classes for sleep and spin mutexes.
113 struct lock_class lock_class_mtx_sleep = {
114 .lc_name = "sleep mutex",
115 .lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
116 .lc_assert = assert_mtx,
118 .lc_ddb_show = db_show_mtx,
121 .lc_unlock = unlock_mtx,
123 .lc_owner = owner_mtx,
126 struct lock_class lock_class_mtx_spin = {
127 .lc_name = "spin mutex",
128 .lc_flags = LC_SPINLOCK | LC_RECURSABLE,
129 .lc_assert = assert_mtx,
131 .lc_ddb_show = db_show_mtx,
133 .lc_lock = lock_spin,
134 .lc_unlock = unlock_spin,
136 .lc_owner = owner_mtx,
140 #ifdef ADAPTIVE_MUTEXES
141 static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging");
143 static struct lock_delay_config __read_frequently mtx_delay;
145 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
147 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
150 LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
153 static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL,
154 "mtx spin debugging");
156 static struct lock_delay_config __read_frequently mtx_spin_delay;
158 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW,
159 &mtx_spin_delay.base, 0, "");
160 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW,
161 &mtx_spin_delay.max, 0, "");
163 LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
166 * System-wide mutexes
168 struct mtx blocked_lock;
171 static void _mtx_lock_indefinite_check(struct mtx *, struct lock_delay_arg *);
174 assert_mtx(const struct lock_object *lock, int what)
177 mtx_assert((const struct mtx *)lock, what);
181 lock_mtx(struct lock_object *lock, uintptr_t how)
184 mtx_lock((struct mtx *)lock);
188 lock_spin(struct lock_object *lock, uintptr_t how)
191 panic("spin locks can only use msleep_spin");
195 unlock_mtx(struct lock_object *lock)
199 m = (struct mtx *)lock;
200 mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
206 unlock_spin(struct lock_object *lock)
209 panic("spin locks can only use msleep_spin");
214 owner_mtx(const struct lock_object *lock, struct thread **owner)
219 m = (const struct mtx *)lock;
221 *owner = (struct thread *)(x & ~MTX_FLAGMASK);
222 return (*owner != NULL);
227 * Function versions of the inlined __mtx_* macros. These are used by
228 * modules and can also be called from assembly language if needed.
231 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
238 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
239 !TD_IS_IDLETHREAD(curthread),
240 ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
241 curthread, m->lock_object.lo_name, file, line));
242 KASSERT(m->mtx_lock != MTX_DESTROYED,
243 ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
244 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
245 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
247 WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
248 LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
250 tid = (uintptr_t)curthread;
252 if (!_mtx_obtain_lock_fetch(m, &v, tid))
253 _mtx_lock_sleep(m, v, opts, file, line);
255 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
256 m, 0, 0, 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 TD_LOCKS_INC(curthread);
265 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
271 KASSERT(m->mtx_lock != MTX_DESTROYED,
272 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
273 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
274 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
276 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
277 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
279 mtx_assert(m, MA_OWNED);
281 #ifdef LOCK_PROFILING
282 __mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
284 __mtx_unlock(m, curthread, opts, file, line);
286 TD_LOCKS_DEC(curthread);
290 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
300 KASSERT(m->mtx_lock != MTX_DESTROYED,
301 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
302 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
303 ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
304 m->lock_object.lo_name, file, line));
306 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
307 (opts & MTX_RECURSE) != 0,
308 ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
309 m->lock_object.lo_name, file, line));
310 opts &= ~MTX_RECURSE;
311 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
315 tid = (uintptr_t)curthread;
317 if (!_mtx_obtain_lock_fetch(m, &v, tid))
318 _mtx_lock_spin(m, v, opts, file, line);
320 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire,
321 m, 0, 0, file, line);
323 __mtx_lock_spin(m, curthread, opts, file, line);
325 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
327 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
331 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
336 if (SCHEDULER_STOPPED())
341 KASSERT(m->mtx_lock != MTX_DESTROYED,
342 ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
343 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
344 ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
345 m->lock_object.lo_name, file, line));
346 KASSERT((opts & MTX_RECURSE) == 0,
347 ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
348 m->lock_object.lo_name, file, line));
349 if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
350 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
351 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
354 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
359 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
366 KASSERT(m->mtx_lock != MTX_DESTROYED,
367 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
368 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
369 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
370 m->lock_object.lo_name, file, line));
371 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
372 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
374 mtx_assert(m, MA_OWNED);
376 __mtx_unlock_spin(m);
380 * The important part of mtx_trylock{,_flags}()
381 * Tries to acquire lock `m.' If this function is called on a mutex that
382 * is already owned, it will recursively acquire the lock.
385 _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
389 #ifdef LOCK_PROFILING
390 uint64_t waittime = 0;
398 if (SCHEDULER_STOPPED_TD(td))
401 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
402 ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
403 curthread, m->lock_object.lo_name, file, line));
404 KASSERT(m->mtx_lock != MTX_DESTROYED,
405 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
406 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
407 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
414 if (_mtx_obtain_lock_fetch(m, &v, tid))
416 if (v == MTX_UNOWNED)
419 ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
420 (opts & MTX_RECURSE) != 0)) {
422 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
430 opts &= ~MTX_RECURSE;
432 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
434 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
436 TD_LOCKS_INC(curthread);
438 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
439 m, contested, waittime, file, line);
446 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
451 return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
455 * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
457 * We call this if the lock is either contested (i.e. we need to go to
458 * sleep waiting for it), or if we need to recurse on it.
462 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
466 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
471 struct turnstile *ts;
473 struct thread *owner;
474 #ifdef LOCK_PROFILING
476 uint64_t waittime = 0;
478 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
479 struct lock_delay_arg lda;
483 int64_t sleep_time = 0;
484 int64_t all_time = 0;
486 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
495 if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
496 while (v == MTX_UNOWNED) {
497 if (_mtx_obtain_lock_fetch(m, &v, tid))
501 all_time -= lockstat_nsecs(&m->lock_object);
504 #ifdef LOCK_PROFILING
508 if (SCHEDULER_STOPPED_TD(td))
511 #if defined(ADAPTIVE_MUTEXES)
512 lock_delay_arg_init(&lda, &mtx_delay);
513 #elif defined(KDTRACE_HOOKS)
514 lock_delay_arg_init(&lda, NULL);
517 if (__predict_false(v == MTX_UNOWNED))
518 v = MTX_READ_VALUE(m);
520 if (__predict_false(lv_mtx_owner(v) == td)) {
521 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
522 (opts & MTX_RECURSE) != 0,
523 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
524 m->lock_object.lo_name, file, line));
526 opts &= ~MTX_RECURSE;
529 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
530 if (LOCK_LOG_TEST(&m->lock_object, opts))
531 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
535 opts &= ~MTX_RECURSE;
539 PMC_SOFT_CALL( , , lock, failed);
541 lock_profile_obtain_lock_failed(&m->lock_object,
542 &contested, &waittime);
543 if (LOCK_LOG_TEST(&m->lock_object, opts))
545 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
546 m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
549 if (v == MTX_UNOWNED) {
550 if (_mtx_obtain_lock_fetch(m, &v, tid))
557 #ifdef ADAPTIVE_MUTEXES
559 * If the owner is running on another CPU, spin until the
560 * owner stops running or the state of the lock changes.
562 owner = lv_mtx_owner(v);
563 if (TD_IS_RUNNING(owner)) {
564 if (LOCK_LOG_TEST(&m->lock_object, 0))
566 "%s: spinning on %p held by %p",
568 KTR_STATE1(KTR_SCHED, "thread",
569 sched_tdname((struct thread *)tid),
570 "spinning", "lockname:\"%s\"",
571 m->lock_object.lo_name);
574 v = MTX_READ_VALUE(m);
575 owner = lv_mtx_owner(v);
576 } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
577 KTR_STATE0(KTR_SCHED, "thread",
578 sched_tdname((struct thread *)tid),
584 ts = turnstile_trywait(&m->lock_object);
585 v = MTX_READ_VALUE(m);
589 * Check if the lock has been released while spinning for
590 * the turnstile chain lock.
592 if (v == MTX_UNOWNED) {
593 turnstile_cancel(ts);
597 #ifdef ADAPTIVE_MUTEXES
599 * The current lock owner might have started executing
600 * on another CPU (or the lock could have changed
601 * owners) while we were waiting on the turnstile
602 * chain lock. If so, drop the turnstile lock and try
605 owner = lv_mtx_owner(v);
606 if (TD_IS_RUNNING(owner)) {
607 turnstile_cancel(ts);
613 * If the mutex isn't already contested and a failure occurs
614 * setting the contested bit, the mutex was either released
615 * or the state of the MTX_RECURSED bit changed.
617 if ((v & MTX_CONTESTED) == 0 &&
618 !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) {
619 goto retry_turnstile;
623 * We definitely must sleep for this lock.
625 mtx_assert(m, MA_NOTOWNED);
628 * Block on the turnstile.
631 sleep_time -= lockstat_nsecs(&m->lock_object);
633 #ifndef ADAPTIVE_MUTEXES
634 owner = mtx_owner(m);
636 MPASS(owner == mtx_owner(m));
637 turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
639 sleep_time += lockstat_nsecs(&m->lock_object);
642 v = MTX_READ_VALUE(m);
644 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
645 if (__predict_true(!doing_lockprof))
649 all_time += lockstat_nsecs(&m->lock_object);
651 LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
654 * Only record the loops spinning and not sleeping.
656 if (lda.spin_cnt > sleep_cnt)
657 LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
660 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
661 waittime, file, line);
666 * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
668 * This is only called if we need to actually spin for the lock. Recursion
673 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
674 const char *file, int line)
677 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
681 struct lock_delay_arg lda;
683 #ifdef LOCK_PROFILING
685 uint64_t waittime = 0;
688 int64_t spin_time = 0;
690 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
694 tid = (uintptr_t)curthread;
698 if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
699 while (v == MTX_UNOWNED) {
700 if (_mtx_obtain_lock_fetch(m, &v, tid))
704 spin_time -= lockstat_nsecs(&m->lock_object);
707 #ifdef LOCK_PROFILING
711 if (__predict_false(v == MTX_UNOWNED))
712 v = MTX_READ_VALUE(m);
714 if (__predict_false(v == tid)) {
719 if (SCHEDULER_STOPPED())
722 lock_delay_arg_init(&lda, &mtx_spin_delay);
724 if (LOCK_LOG_TEST(&m->lock_object, opts))
725 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
726 KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
727 "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
730 PMC_SOFT_CALL( , , lock, failed);
732 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
735 if (v == MTX_UNOWNED) {
736 if (_mtx_obtain_lock_fetch(m, &v, tid))
740 /* Give interrupts a chance while we spin. */
743 if (__predict_true(lda.spin_cnt < 10000000)) {
746 _mtx_lock_indefinite_check(m, &lda);
748 v = MTX_READ_VALUE(m);
749 } while (v != MTX_UNOWNED);
753 if (LOCK_LOG_TEST(&m->lock_object, opts))
754 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
755 KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
758 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
759 if (__predict_true(!doing_lockprof))
763 spin_time += lockstat_nsecs(&m->lock_object);
764 if (lda.spin_cnt != 0)
765 LOCKSTAT_RECORD1(spin__spin, m, spin_time);
768 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
769 contested, waittime, file, line);
775 thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
778 KASSERT(m->mtx_lock != MTX_DESTROYED,
779 ("thread_lock() of destroyed mutex @ %s:%d", file, line));
780 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
781 ("thread_lock() of sleep mutex %s @ %s:%d",
782 m->lock_object.lo_name, file, line));
784 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
785 ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
786 m->lock_object.lo_name, file, line));
787 WITNESS_CHECKORDER(&m->lock_object,
788 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
791 #define thread_lock_validate(m, opts, file, line) do { } while (0)
794 #ifndef LOCK_PROFILING
797 _thread_lock(struct thread *td, int opts, const char *file, int line)
800 _thread_lock(struct thread *td)
806 tid = (uintptr_t)curthread;
808 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
812 thread_lock_validate(m, 0, file, line);
813 v = MTX_READ_VALUE(m);
814 if (__predict_true(v == MTX_UNOWNED)) {
815 if (__predict_false(!_mtx_obtain_lock(m, tid)))
816 goto slowpath_unlocked;
817 } else if (v == tid) {
820 goto slowpath_unlocked;
821 if (__predict_true(m == td->td_lock)) {
822 WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
825 MPASS(m->mtx_recurse == 0);
826 _mtx_release_lock_quick(m);
831 thread_lock_flags_(td, opts, file, line);
833 thread_lock_flags_(td, 0, 0, 0);
839 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
843 struct lock_delay_arg lda;
844 #ifdef LOCK_PROFILING
846 uint64_t waittime = 0;
849 int64_t spin_time = 0;
851 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
852 int doing_lockprof = 1;
855 tid = (uintptr_t)curthread;
857 if (SCHEDULER_STOPPED()) {
859 * Ensure that spinlock sections are balanced even when the
860 * scheduler is stopped, since we may otherwise inadvertently
861 * re-enable interrupts while dumping core.
867 lock_delay_arg_init(&lda, &mtx_spin_delay);
870 PMC_SOFT_CALL( , , lock, failed);
873 #ifdef LOCK_PROFILING
875 #elif defined(KDTRACE_HOOKS)
876 doing_lockprof = lockstat_enabled;
877 if (__predict_false(doing_lockprof))
878 spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
885 thread_lock_validate(m, opts, file, line);
886 v = MTX_READ_VALUE(m);
888 if (v == MTX_UNOWNED) {
889 if (_mtx_obtain_lock_fetch(m, &v, tid))
895 MPASS(m == td->td_lock);
898 lock_profile_obtain_lock_failed(&m->lock_object,
899 &contested, &waittime);
900 /* Give interrupts a chance while we spin. */
903 if (__predict_true(lda.spin_cnt < 10000000)) {
906 _mtx_lock_indefinite_check(m, &lda);
908 if (m != td->td_lock) {
912 v = MTX_READ_VALUE(m);
913 } while (v != MTX_UNOWNED);
916 if (m == td->td_lock)
918 MPASS(m->mtx_recurse == 0);
919 _mtx_release_lock_quick(m);
921 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
923 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
925 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
926 if (__predict_true(!doing_lockprof))
930 spin_time += lockstat_nsecs(&m->lock_object);
932 if (m->mtx_recurse == 0)
933 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
934 contested, waittime, file, line);
936 if (lda.spin_cnt != 0)
937 LOCKSTAT_RECORD1(thread__spin, m, spin_time);
942 thread_lock_block(struct thread *td)
946 THREAD_LOCK_ASSERT(td, MA_OWNED);
948 td->td_lock = &blocked_lock;
949 mtx_unlock_spin(lock);
955 thread_lock_unblock(struct thread *td, struct mtx *new)
957 mtx_assert(new, MA_OWNED);
958 MPASS(td->td_lock == &blocked_lock);
959 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
963 thread_lock_set(struct thread *td, struct mtx *new)
967 mtx_assert(new, MA_OWNED);
968 THREAD_LOCK_ASSERT(td, MA_OWNED);
971 mtx_unlock_spin(lock);
975 * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
977 * We are only called here if the lock is recursed, contested (i.e. we
978 * need to wake up a blocked thread) or lockstat probe is active.
982 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
983 const char *file, int line)
986 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
990 struct turnstile *ts;
993 if (SCHEDULER_STOPPED())
996 tid = (uintptr_t)curthread;
999 if (__predict_false(v == tid))
1000 v = MTX_READ_VALUE(m);
1002 if (__predict_false(v & MTX_RECURSED)) {
1003 if (--(m->mtx_recurse) == 0)
1004 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1005 if (LOCK_LOG_TEST(&m->lock_object, opts))
1006 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
1010 LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
1011 if (v == tid && _mtx_release_lock(m, tid))
1015 * We have to lock the chain before the turnstile so this turnstile
1016 * can be removed from the hash list if it is empty.
1018 turnstile_chain_lock(&m->lock_object);
1019 _mtx_release_lock_quick(m);
1020 ts = turnstile_lookup(&m->lock_object);
1022 if (LOCK_LOG_TEST(&m->lock_object, opts))
1023 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
1024 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1027 * This turnstile is now no longer associated with the mutex. We can
1028 * unlock the chain lock so a new turnstile may take it's place.
1030 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
1031 turnstile_chain_unlock(&m->lock_object);
1035 * All the unlocking of MTX_SPIN locks is done inline.
1036 * See the __mtx_unlock_spin() macro for the details.
1040 * The backing function for the INVARIANTS-enabled mtx_assert()
1042 #ifdef INVARIANT_SUPPORT
1044 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
1046 const struct mtx *m;
1048 if (panicstr != NULL || dumping || SCHEDULER_STOPPED())
1055 case MA_OWNED | MA_RECURSED:
1056 case MA_OWNED | MA_NOTRECURSED:
1058 panic("mutex %s not owned at %s:%d",
1059 m->lock_object.lo_name, file, line);
1060 if (mtx_recursed(m)) {
1061 if ((what & MA_NOTRECURSED) != 0)
1062 panic("mutex %s recursed at %s:%d",
1063 m->lock_object.lo_name, file, line);
1064 } else if ((what & MA_RECURSED) != 0) {
1065 panic("mutex %s unrecursed at %s:%d",
1066 m->lock_object.lo_name, file, line);
1071 panic("mutex %s owned at %s:%d",
1072 m->lock_object.lo_name, file, line);
1075 panic("unknown mtx_assert at %s:%d", file, line);
1081 * General init routine used by the MTX_SYSINIT() macro.
1084 mtx_sysinit(void *arg)
1086 struct mtx_args *margs = arg;
1088 mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
1093 * Mutex initialization routine; initialize lock `m' of type contained in
1094 * `opts' with options contained in `opts' and name `name.' The optional
1095 * lock type `type' is used as a general lock category name for use with
1099 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
1102 struct lock_class *class;
1107 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1108 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1109 ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1110 ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1113 /* Determine lock class and lock flags. */
1114 if (opts & MTX_SPIN)
1115 class = &lock_class_mtx_spin;
1117 class = &lock_class_mtx_sleep;
1119 if (opts & MTX_QUIET)
1121 if (opts & MTX_RECURSE)
1122 flags |= LO_RECURSABLE;
1123 if ((opts & MTX_NOWITNESS) == 0)
1124 flags |= LO_WITNESS;
1125 if (opts & MTX_DUPOK)
1127 if (opts & MTX_NOPROFILE)
1128 flags |= LO_NOPROFILE;
1132 /* Initialize mutex. */
1133 lock_init(&m->lock_object, class, name, type, flags);
1135 m->mtx_lock = MTX_UNOWNED;
1140 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be
1141 * passed in as a flag here because if the corresponding mtx_init() was
1142 * called with MTX_QUIET set, then it will already be set in the mutex's
1146 _mtx_destroy(volatile uintptr_t *c)
1153 MPASS(mtx_unowned(m));
1155 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
1157 /* Perform the non-mtx related part of mtx_unlock_spin(). */
1158 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1161 TD_LOCKS_DEC(curthread);
1163 lock_profile_release_lock(&m->lock_object);
1164 /* Tell witness this isn't locked to make it happy. */
1165 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1169 m->mtx_lock = MTX_DESTROYED;
1170 lock_destroy(&m->lock_object);
1174 * Intialize the mutex code and system mutexes. This is called from the MD
1175 * startup code prior to mi_startup(). The per-CPU data space needs to be
1176 * setup before this is called.
1182 /* Setup turnstiles so that sleep mutexes work. */
1186 * Initialize mutexes.
1188 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
1189 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
1190 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */
1191 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1192 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
1193 mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
1194 mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
1195 mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
1196 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1200 static void __noinline
1201 _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap)
1206 if (ldap->spin_cnt < 60000000 || kdb_active || panicstr != NULL)
1211 /* If the mutex is unlocked, try again. */
1215 printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
1216 m, m->lock_object.lo_name, td, td->td_tid);
1218 witness_display_spinlock(&m->lock_object, td, printf);
1220 panic("spin lock held too long");
1227 db_show_mtx(const struct lock_object *lock)
1230 const struct mtx *m;
1232 m = (const struct mtx *)lock;
1234 db_printf(" flags: {");
1235 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1239 if (m->lock_object.lo_flags & LO_RECURSABLE)
1240 db_printf(", RECURSE");
1241 if (m->lock_object.lo_flags & LO_DUPOK)
1242 db_printf(", DUPOK");
1244 db_printf(" state: {");
1246 db_printf("UNOWNED");
1247 else if (mtx_destroyed(m))
1248 db_printf("DESTROYED");
1251 if (m->mtx_lock & MTX_CONTESTED)
1252 db_printf(", CONTESTED");
1253 if (m->mtx_lock & MTX_RECURSED)
1254 db_printf(", RECURSED");
1257 if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1259 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1260 td->td_tid, td->td_proc->p_pid, td->td_name);
1261 if (mtx_recursed(m))
1262 db_printf(" recursed: %d\n", m->mtx_recurse);