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 static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging");
145 static struct lock_delay_config __read_frequently mtx_delay;
147 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
149 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
152 LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
155 static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL,
156 "mtx spin debugging");
158 static struct lock_delay_config __read_frequently mtx_spin_delay;
160 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW,
161 &mtx_spin_delay.base, 0, "");
162 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW,
163 &mtx_spin_delay.max, 0, "");
165 LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
168 * System-wide mutexes
170 struct mtx blocked_lock;
171 struct mtx __exclusive_cache_line Giant;
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;
477 #ifdef LOCK_PROFILING
479 uint64_t waittime = 0;
481 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
482 struct lock_delay_arg lda;
486 int64_t sleep_time = 0;
487 int64_t all_time = 0;
489 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
494 if (SCHEDULER_STOPPED_TD(td))
497 #if defined(ADAPTIVE_MUTEXES)
498 lock_delay_arg_init(&lda, &mtx_delay);
499 #elif defined(KDTRACE_HOOKS)
500 lock_delay_arg_init(&lda, NULL);
503 if (__predict_false(v == MTX_UNOWNED))
504 v = MTX_READ_VALUE(m);
506 if (__predict_false(lv_mtx_owner(v) == td)) {
507 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
508 (opts & MTX_RECURSE) != 0,
509 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
510 m->lock_object.lo_name, file, line));
512 opts &= ~MTX_RECURSE;
515 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
516 if (LOCK_LOG_TEST(&m->lock_object, opts))
517 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
521 opts &= ~MTX_RECURSE;
525 PMC_SOFT_CALL( , , lock, failed);
527 lock_profile_obtain_lock_failed(&m->lock_object,
528 &contested, &waittime);
529 if (LOCK_LOG_TEST(&m->lock_object, opts))
531 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
532 m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
533 #ifdef LOCK_PROFILING
535 #elif defined(KDTRACE_HOOKS)
536 doing_lockprof = lockstat_enabled;
537 if (__predict_false(doing_lockprof))
538 all_time -= lockstat_nsecs(&m->lock_object);
542 if (v == MTX_UNOWNED) {
543 if (_mtx_obtain_lock_fetch(m, &v, tid))
550 #ifdef ADAPTIVE_MUTEXES
552 * If the owner is running on another CPU, spin until the
553 * owner stops running or the state of the lock changes.
555 owner = lv_mtx_owner(v);
556 if (TD_IS_RUNNING(owner)) {
557 if (LOCK_LOG_TEST(&m->lock_object, 0))
559 "%s: spinning on %p held by %p",
561 KTR_STATE1(KTR_SCHED, "thread",
562 sched_tdname((struct thread *)tid),
563 "spinning", "lockname:\"%s\"",
564 m->lock_object.lo_name);
567 v = MTX_READ_VALUE(m);
568 owner = lv_mtx_owner(v);
569 } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
570 KTR_STATE0(KTR_SCHED, "thread",
571 sched_tdname((struct thread *)tid),
577 ts = turnstile_trywait(&m->lock_object);
578 v = MTX_READ_VALUE(m);
581 * Check if the lock has been released while spinning for
582 * the turnstile chain lock.
584 if (v == MTX_UNOWNED) {
585 turnstile_cancel(ts);
589 #ifdef ADAPTIVE_MUTEXES
591 * The current lock owner might have started executing
592 * on another CPU (or the lock could have changed
593 * owners) while we were waiting on the turnstile
594 * chain lock. If so, drop the turnstile lock and try
597 owner = lv_mtx_owner(v);
598 if (TD_IS_RUNNING(owner)) {
599 turnstile_cancel(ts);
605 * If the mutex isn't already contested and a failure occurs
606 * setting the contested bit, the mutex was either released
607 * or the state of the MTX_RECURSED bit changed.
609 if ((v & MTX_CONTESTED) == 0 &&
610 !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
611 turnstile_cancel(ts);
612 v = MTX_READ_VALUE(m);
617 * We definitely must sleep for this lock.
619 mtx_assert(m, MA_NOTOWNED);
624 "contention: %p at %s:%d wants %s, taken by %s:%d",
625 (void *)tid, file, line, m->lock_object.lo_name,
626 WITNESS_FILE(&m->lock_object),
627 WITNESS_LINE(&m->lock_object));
633 * Block on the turnstile.
636 sleep_time -= lockstat_nsecs(&m->lock_object);
638 #ifndef ADAPTIVE_MUTEXES
639 owner = mtx_owner(m);
641 MPASS(owner == mtx_owner(m));
642 turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
644 sleep_time += lockstat_nsecs(&m->lock_object);
647 v = MTX_READ_VALUE(m);
652 "contention end: %s acquired by %p at %s:%d",
653 m->lock_object.lo_name, (void *)tid, file, line);
656 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
657 if (__predict_true(!doing_lockprof))
661 all_time += lockstat_nsecs(&m->lock_object);
663 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
664 waittime, file, line);
667 LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
670 * Only record the loops spinning and not sleeping.
672 if (lda.spin_cnt > sleep_cnt)
673 LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
678 _mtx_lock_spin_failed(struct mtx *m)
684 /* If the mutex is unlocked, try again. */
688 printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
689 m, m->lock_object.lo_name, td, td->td_tid);
691 witness_display_spinlock(&m->lock_object, td, printf);
693 panic("spin lock held too long");
698 * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
700 * This is only called if we need to actually spin for the lock. Recursion
705 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
706 const char *file, int line)
709 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
713 struct lock_delay_arg lda;
715 #ifdef LOCK_PROFILING
717 uint64_t waittime = 0;
720 int64_t spin_time = 0;
722 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
726 tid = (uintptr_t)curthread;
729 if (__predict_false(v == MTX_UNOWNED))
730 v = MTX_READ_VALUE(m);
732 if (__predict_false(v == tid)) {
737 if (SCHEDULER_STOPPED())
740 lock_delay_arg_init(&lda, &mtx_spin_delay);
742 if (LOCK_LOG_TEST(&m->lock_object, opts))
743 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
744 KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
745 "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
748 PMC_SOFT_CALL( , , lock, failed);
750 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
751 #ifdef LOCK_PROFILING
753 #elif defined(KDTRACE_HOOKS)
754 doing_lockprof = lockstat_enabled;
755 if (__predict_false(doing_lockprof))
756 spin_time -= lockstat_nsecs(&m->lock_object);
759 if (v == MTX_UNOWNED) {
760 if (_mtx_obtain_lock_fetch(m, &v, tid))
764 /* Give interrupts a chance while we spin. */
767 if (lda.spin_cnt < 10000000) {
771 if (lda.spin_cnt < 60000000 || kdb_active ||
775 _mtx_lock_spin_failed(m);
778 v = MTX_READ_VALUE(m);
779 } while (v != MTX_UNOWNED);
783 if (LOCK_LOG_TEST(&m->lock_object, opts))
784 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
785 KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
788 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
789 if (__predict_true(!doing_lockprof))
793 spin_time += lockstat_nsecs(&m->lock_object);
795 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
796 contested, waittime, file, line);
798 if (lda.spin_cnt != 0)
799 LOCKSTAT_RECORD1(spin__spin, m, spin_time);
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));
815 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
816 ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
817 m->lock_object.lo_name, file, line));
818 WITNESS_CHECKORDER(&m->lock_object,
819 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
822 #define thread_lock_validate(m, opts, file, line) do { } while (0)
825 #ifndef LOCK_PROFILING
828 _thread_lock(struct thread *td, int opts, const char *file, int line)
831 _thread_lock(struct thread *td)
837 tid = (uintptr_t)curthread;
839 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
843 thread_lock_validate(m, 0, file, line);
844 v = MTX_READ_VALUE(m);
845 if (__predict_true(v == MTX_UNOWNED)) {
846 if (__predict_false(!_mtx_obtain_lock(m, tid)))
847 goto slowpath_unlocked;
848 } else if (v == tid) {
851 goto slowpath_unlocked;
852 if (__predict_true(m == td->td_lock)) {
853 WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
856 if (m->mtx_recurse != 0)
859 _mtx_release_lock_quick(m);
864 thread_lock_flags_(td, opts, file, line);
866 thread_lock_flags_(td, 0, 0, 0);
872 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
876 struct lock_delay_arg lda;
877 #ifdef LOCK_PROFILING
879 uint64_t waittime = 0;
882 int64_t spin_time = 0;
884 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
885 int doing_lockprof = 1;
888 tid = (uintptr_t)curthread;
890 if (SCHEDULER_STOPPED()) {
892 * Ensure that spinlock sections are balanced even when the
893 * scheduler is stopped, since we may otherwise inadvertently
894 * re-enable interrupts while dumping core.
900 lock_delay_arg_init(&lda, &mtx_spin_delay);
902 #ifdef LOCK_PROFILING
904 #elif defined(KDTRACE_HOOKS)
905 doing_lockprof = lockstat_enabled;
906 if (__predict_false(doing_lockprof))
907 spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
914 thread_lock_validate(m, opts, file, line);
916 if (_mtx_obtain_lock_fetch(m, &v, tid))
918 if (v == MTX_UNOWNED)
925 PMC_SOFT_CALL( , , lock, failed);
927 lock_profile_obtain_lock_failed(&m->lock_object,
928 &contested, &waittime);
929 /* Give interrupts a chance while we spin. */
932 if (lda.spin_cnt < 10000000) {
936 if (lda.spin_cnt < 60000000 ||
937 kdb_active || panicstr != NULL)
940 _mtx_lock_spin_failed(m);
943 if (m != td->td_lock)
945 v = MTX_READ_VALUE(m);
946 } while (v != MTX_UNOWNED);
949 if (m == td->td_lock)
951 __mtx_unlock_spin(m); /* does spinlock_exit() */
953 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
955 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
957 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
958 if (__predict_true(!doing_lockprof))
962 spin_time += lockstat_nsecs(&m->lock_object);
964 if (m->mtx_recurse == 0)
965 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
966 contested, waittime, file, line);
968 if (lda.spin_cnt != 0)
969 LOCKSTAT_RECORD1(thread__spin, m, spin_time);
974 thread_lock_block(struct thread *td)
978 THREAD_LOCK_ASSERT(td, MA_OWNED);
980 td->td_lock = &blocked_lock;
981 mtx_unlock_spin(lock);
987 thread_lock_unblock(struct thread *td, struct mtx *new)
989 mtx_assert(new, MA_OWNED);
990 MPASS(td->td_lock == &blocked_lock);
991 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
995 thread_lock_set(struct thread *td, struct mtx *new)
999 mtx_assert(new, MA_OWNED);
1000 THREAD_LOCK_ASSERT(td, 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, TS_EXCLUSIVE_LOCK);
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 (panicstr != NULL || 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)
1193 TD_LOCKS_DEC(curthread);
1195 lock_profile_release_lock(&m->lock_object);
1196 /* Tell witness this isn't locked to make it happy. */
1197 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1201 m->mtx_lock = MTX_DESTROYED;
1202 lock_destroy(&m->lock_object);
1206 * Intialize the mutex code and system mutexes. This is called from the MD
1207 * startup code prior to mi_startup(). The per-CPU data space needs to be
1208 * setup before this is called.
1214 /* Setup turnstiles so that sleep mutexes work. */
1218 * Initialize mutexes.
1220 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
1221 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
1222 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */
1223 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1224 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
1225 mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
1226 mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
1227 mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
1228 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1234 db_show_mtx(const struct lock_object *lock)
1237 const struct mtx *m;
1239 m = (const struct mtx *)lock;
1241 db_printf(" flags: {");
1242 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1246 if (m->lock_object.lo_flags & LO_RECURSABLE)
1247 db_printf(", RECURSE");
1248 if (m->lock_object.lo_flags & LO_DUPOK)
1249 db_printf(", DUPOK");
1251 db_printf(" state: {");
1253 db_printf("UNOWNED");
1254 else if (mtx_destroyed(m))
1255 db_printf("DESTROYED");
1258 if (m->mtx_lock & MTX_CONTESTED)
1259 db_printf(", CONTESTED");
1260 if (m->mtx_lock & MTX_RECURSED)
1261 db_printf(", RECURSED");
1264 if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1266 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1267 td->td_tid, td->td_proc->p_pid, td->td_name);
1268 if (mtx_recursed(m))
1269 db_printf(" recursed: %d\n", m->mtx_recurse);