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, NULL, "mtx debugging");
146 static struct lock_delay_config __read_frequently mtx_delay;
148 SYSCTL_U16(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
150 SYSCTL_U16(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
153 LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
155 #define mtx_delay locks_delay
159 #ifdef MUTEX_SPIN_CUSTOM_BACKOFF
160 static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL,
161 "mtx spin debugging");
163 static struct lock_delay_config __read_frequently mtx_spin_delay;
165 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW,
166 &mtx_spin_delay.base, 0, "");
167 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW,
168 &mtx_spin_delay.max, 0, "");
170 LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
172 #define mtx_spin_delay locks_delay
176 * System-wide mutexes
178 struct mtx blocked_lock;
179 struct mtx __exclusive_cache_line Giant;
181 static void _mtx_lock_indefinite_check(struct mtx *, struct lock_delay_arg *);
184 assert_mtx(const struct lock_object *lock, int what)
188 * Treat LA_LOCKED as if LA_XLOCKED was asserted.
190 * Some callers of lc_assert uses LA_LOCKED to indicate that either
191 * a shared lock or write lock was held, while other callers uses
192 * the more strict LA_XLOCKED (used as MA_OWNED).
194 * Mutex is the only lock class that can not be shared, as a result,
195 * we can reasonably consider the caller really intends to assert
196 * LA_XLOCKED when they are asserting LA_LOCKED on a mutex object.
198 if (what & LA_LOCKED) {
202 mtx_assert((const struct mtx *)lock, what);
206 lock_mtx(struct lock_object *lock, uintptr_t how)
209 mtx_lock((struct mtx *)lock);
213 lock_spin(struct lock_object *lock, uintptr_t how)
216 panic("spin locks can only use msleep_spin");
220 unlock_mtx(struct lock_object *lock)
224 m = (struct mtx *)lock;
225 mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
231 unlock_spin(struct lock_object *lock)
234 panic("spin locks can only use msleep_spin");
239 owner_mtx(const struct lock_object *lock, struct thread **owner)
244 m = (const struct mtx *)lock;
246 *owner = (struct thread *)(x & ~MTX_FLAGMASK);
247 return (*owner != NULL);
252 * Function versions of the inlined __mtx_* macros. These are used by
253 * modules and can also be called from assembly language if needed.
256 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
263 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
264 !TD_IS_IDLETHREAD(curthread),
265 ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
266 curthread, m->lock_object.lo_name, file, line));
267 KASSERT(m->mtx_lock != MTX_DESTROYED,
268 ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
269 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
270 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
272 WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
273 LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
275 tid = (uintptr_t)curthread;
277 if (!_mtx_obtain_lock_fetch(m, &v, tid))
278 _mtx_lock_sleep(m, v, opts, file, line);
280 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
281 m, 0, 0, file, line);
282 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
284 WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
286 TD_LOCKS_INC(curthread);
290 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
296 KASSERT(m->mtx_lock != MTX_DESTROYED,
297 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
298 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
299 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
301 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
302 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
304 mtx_assert(m, MA_OWNED);
306 #ifdef LOCK_PROFILING
307 __mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
309 __mtx_unlock(m, curthread, opts, file, line);
311 TD_LOCKS_DEC(curthread);
315 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
325 KASSERT(m->mtx_lock != MTX_DESTROYED,
326 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
327 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
328 ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
329 m->lock_object.lo_name, file, line));
331 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
332 (opts & MTX_RECURSE) != 0,
333 ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
334 m->lock_object.lo_name, file, line));
335 opts &= ~MTX_RECURSE;
336 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
340 tid = (uintptr_t)curthread;
342 if (!_mtx_obtain_lock_fetch(m, &v, tid))
343 _mtx_lock_spin(m, v, opts, file, line);
345 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire,
346 m, 0, 0, file, line);
348 __mtx_lock_spin(m, curthread, opts, file, line);
350 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
352 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
356 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
361 if (SCHEDULER_STOPPED())
366 KASSERT(m->mtx_lock != MTX_DESTROYED,
367 ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
368 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
369 ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
370 m->lock_object.lo_name, file, line));
371 KASSERT((opts & MTX_RECURSE) == 0,
372 ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
373 m->lock_object.lo_name, file, line));
374 if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
375 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
376 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
379 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
384 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
391 KASSERT(m->mtx_lock != MTX_DESTROYED,
392 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
393 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
394 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
395 m->lock_object.lo_name, file, line));
396 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
397 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
399 mtx_assert(m, MA_OWNED);
401 __mtx_unlock_spin(m);
405 * The important part of mtx_trylock{,_flags}()
406 * Tries to acquire lock `m.' If this function is called on a mutex that
407 * is already owned, it will recursively acquire the lock.
410 _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
414 #ifdef LOCK_PROFILING
415 uint64_t waittime = 0;
423 if (SCHEDULER_STOPPED_TD(td))
426 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
427 ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
428 curthread, m->lock_object.lo_name, file, line));
429 KASSERT(m->mtx_lock != MTX_DESTROYED,
430 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
431 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
432 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
439 if (_mtx_obtain_lock_fetch(m, &v, tid))
441 if (v == MTX_UNOWNED)
444 ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
445 (opts & MTX_RECURSE) != 0)) {
447 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
455 opts &= ~MTX_RECURSE;
457 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
459 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
461 TD_LOCKS_INC(curthread);
463 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
464 m, contested, waittime, file, line);
471 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
476 return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
480 * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
482 * We call this if the lock is either contested (i.e. we need to go to
483 * sleep waiting for it), or if we need to recurse on it.
487 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
491 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
496 struct turnstile *ts;
498 struct thread *owner;
499 #ifdef LOCK_PROFILING
501 uint64_t waittime = 0;
503 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
504 struct lock_delay_arg lda;
508 int64_t sleep_time = 0;
509 int64_t all_time = 0;
511 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
512 int doing_lockprof = 0;
520 if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
521 while (v == MTX_UNOWNED) {
522 if (_mtx_obtain_lock_fetch(m, &v, tid))
526 all_time -= lockstat_nsecs(&m->lock_object);
529 #ifdef LOCK_PROFILING
533 if (SCHEDULER_STOPPED_TD(td))
536 #if defined(ADAPTIVE_MUTEXES)
537 lock_delay_arg_init(&lda, &mtx_delay);
538 #elif defined(KDTRACE_HOOKS)
539 lock_delay_arg_init(&lda, NULL);
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;
564 PMC_SOFT_CALL( , , lock, failed);
566 lock_profile_obtain_lock_failed(&m->lock_object,
567 &contested, &waittime);
568 if (LOCK_LOG_TEST(&m->lock_object, opts))
570 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
571 m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
574 if (v == MTX_UNOWNED) {
575 if (_mtx_obtain_lock_fetch(m, &v, tid))
582 #ifdef ADAPTIVE_MUTEXES
584 * If the owner is running on another CPU, spin until the
585 * owner stops running or the state of the lock changes.
587 owner = lv_mtx_owner(v);
588 if (TD_IS_RUNNING(owner)) {
589 if (LOCK_LOG_TEST(&m->lock_object, 0))
591 "%s: spinning on %p held by %p",
593 KTR_STATE1(KTR_SCHED, "thread",
594 sched_tdname((struct thread *)tid),
595 "spinning", "lockname:\"%s\"",
596 m->lock_object.lo_name);
599 v = MTX_READ_VALUE(m);
600 owner = lv_mtx_owner(v);
601 } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
602 KTR_STATE0(KTR_SCHED, "thread",
603 sched_tdname((struct thread *)tid),
609 ts = turnstile_trywait(&m->lock_object);
610 v = MTX_READ_VALUE(m);
614 * Check if the lock has been released while spinning for
615 * the turnstile chain lock.
617 if (v == MTX_UNOWNED) {
618 turnstile_cancel(ts);
622 #ifdef ADAPTIVE_MUTEXES
624 * The current lock owner might have started executing
625 * on another CPU (or the lock could have changed
626 * owners) while we were waiting on the turnstile
627 * chain lock. If so, drop the turnstile lock and try
630 owner = lv_mtx_owner(v);
631 if (TD_IS_RUNNING(owner)) {
632 turnstile_cancel(ts);
638 * If the mutex isn't already contested and a failure occurs
639 * setting the contested bit, the mutex was either released
640 * or the state of the MTX_RECURSED bit changed.
642 if ((v & MTX_CONTESTED) == 0 &&
643 !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) {
644 goto retry_turnstile;
648 * We definitely must sleep for this lock.
650 mtx_assert(m, MA_NOTOWNED);
653 * Block on the turnstile.
656 sleep_time -= lockstat_nsecs(&m->lock_object);
658 #ifndef ADAPTIVE_MUTEXES
659 owner = mtx_owner(m);
661 MPASS(owner == mtx_owner(m));
662 turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
664 sleep_time += lockstat_nsecs(&m->lock_object);
667 v = MTX_READ_VALUE(m);
669 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
670 if (__predict_true(!doing_lockprof))
674 all_time += lockstat_nsecs(&m->lock_object);
676 LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
679 * Only record the loops spinning and not sleeping.
681 if (lda.spin_cnt > sleep_cnt)
682 LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
685 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
686 waittime, file, line);
691 * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
693 * This is only called if we need to actually spin for the lock. Recursion
698 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
699 const char *file, int line)
702 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
706 struct lock_delay_arg lda;
708 #ifdef LOCK_PROFILING
710 uint64_t waittime = 0;
713 int64_t spin_time = 0;
715 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
716 int doing_lockprof = 0;
719 tid = (uintptr_t)curthread;
723 if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
724 while (v == MTX_UNOWNED) {
725 if (_mtx_obtain_lock_fetch(m, &v, tid))
729 spin_time -= lockstat_nsecs(&m->lock_object);
732 #ifdef LOCK_PROFILING
736 if (__predict_false(v == MTX_UNOWNED))
737 v = MTX_READ_VALUE(m);
739 if (__predict_false(v == tid)) {
744 if (SCHEDULER_STOPPED())
747 lock_delay_arg_init(&lda, &mtx_spin_delay);
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);
755 PMC_SOFT_CALL( , , lock, failed);
757 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
760 if (v == MTX_UNOWNED) {
761 if (_mtx_obtain_lock_fetch(m, &v, tid))
765 /* Give interrupts a chance while we spin. */
768 if (__predict_true(lda.spin_cnt < 10000000)) {
771 _mtx_lock_indefinite_check(m, &lda);
773 v = MTX_READ_VALUE(m);
774 } while (v != MTX_UNOWNED);
778 if (LOCK_LOG_TEST(&m->lock_object, opts))
779 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
780 KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
783 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
784 if (__predict_true(!doing_lockprof))
788 spin_time += lockstat_nsecs(&m->lock_object);
789 if (lda.spin_cnt != 0)
790 LOCKSTAT_RECORD1(spin__spin, m, spin_time);
793 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
794 contested, waittime, file, line);
800 thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
803 KASSERT(m->mtx_lock != MTX_DESTROYED,
804 ("thread_lock() of destroyed mutex @ %s:%d", file, line));
805 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
806 ("thread_lock() of sleep mutex %s @ %s:%d",
807 m->lock_object.lo_name, file, line));
808 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) == 0,
809 ("thread_lock: got a recursive mutex %s @ %s:%d\n",
810 m->lock_object.lo_name, file, line));
811 WITNESS_CHECKORDER(&m->lock_object,
812 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
815 #define thread_lock_validate(m, opts, file, line) do { } while (0)
818 #ifndef LOCK_PROFILING
821 _thread_lock(struct thread *td, int opts, const char *file, int line)
824 _thread_lock(struct thread *td)
830 tid = (uintptr_t)curthread;
832 if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
836 thread_lock_validate(m, 0, file, line);
837 if (__predict_false(m == &blocked_lock))
838 goto slowpath_unlocked;
839 if (__predict_false(!_mtx_obtain_lock(m, tid)))
840 goto slowpath_unlocked;
841 if (__predict_true(m == td->td_lock)) {
842 WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
845 _mtx_release_lock_quick(m);
850 thread_lock_flags_(td, opts, file, line);
852 thread_lock_flags_(td, 0, 0, 0);
858 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
862 struct lock_delay_arg lda;
863 #ifdef LOCK_PROFILING
865 uint64_t waittime = 0;
868 int64_t spin_time = 0;
870 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
871 int doing_lockprof = 1;
874 tid = (uintptr_t)curthread;
876 if (SCHEDULER_STOPPED()) {
878 * Ensure that spinlock sections are balanced even when the
879 * scheduler is stopped, since we may otherwise inadvertently
880 * re-enable interrupts while dumping core.
886 lock_delay_arg_init(&lda, &mtx_spin_delay);
889 PMC_SOFT_CALL( , , lock, failed);
892 #ifdef LOCK_PROFILING
894 #elif defined(KDTRACE_HOOKS)
895 doing_lockprof = lockstat_enabled;
896 if (__predict_false(doing_lockprof))
897 spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
904 thread_lock_validate(m, opts, file, line);
905 v = MTX_READ_VALUE(m);
907 if (v == MTX_UNOWNED) {
908 if (_mtx_obtain_lock_fetch(m, &v, tid))
913 lock_profile_obtain_lock_failed(&m->lock_object,
914 &contested, &waittime);
915 /* Give interrupts a chance while we spin. */
918 if (__predict_true(lda.spin_cnt < 10000000)) {
921 _mtx_lock_indefinite_check(m, &lda);
923 if (m != td->td_lock) {
927 v = MTX_READ_VALUE(m);
928 } while (v != MTX_UNOWNED);
931 if (m == td->td_lock)
933 _mtx_release_lock_quick(m);
935 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
937 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
939 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
940 if (__predict_true(!doing_lockprof))
944 spin_time += lockstat_nsecs(&m->lock_object);
946 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m, contested,
947 waittime, file, line);
949 if (lda.spin_cnt != 0)
950 LOCKSTAT_RECORD1(thread__spin, m, spin_time);
955 thread_lock_block(struct thread *td)
960 mtx_assert(lock, MA_OWNED);
961 td->td_lock = &blocked_lock;
967 thread_lock_unblock(struct thread *td, struct mtx *new)
970 mtx_assert(new, MA_OWNED);
971 KASSERT(td->td_lock == &blocked_lock,
972 ("thread %p lock %p not blocked_lock %p",
973 td, td->td_lock, &blocked_lock));
974 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
978 thread_lock_block_wait(struct thread *td)
981 while (td->td_lock == &blocked_lock)
984 /* Acquire fence to be certain that all thread state is visible. */
985 atomic_thread_fence_acq();
989 thread_lock_set(struct thread *td, struct mtx *new)
993 mtx_assert(new, MA_OWNED);
995 mtx_assert(lock, MA_OWNED);
997 mtx_unlock_spin(lock);
1001 * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
1003 * We are only called here if the lock is recursed, contested (i.e. we
1004 * need to wake up a blocked thread) or lockstat probe is active.
1008 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
1009 const char *file, int line)
1012 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
1016 struct turnstile *ts;
1019 if (SCHEDULER_STOPPED())
1022 tid = (uintptr_t)curthread;
1025 if (__predict_false(v == tid))
1026 v = MTX_READ_VALUE(m);
1028 if (__predict_false(v & MTX_RECURSED)) {
1029 if (--(m->mtx_recurse) == 0)
1030 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1031 if (LOCK_LOG_TEST(&m->lock_object, opts))
1032 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
1036 LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
1037 if (v == tid && _mtx_release_lock(m, tid))
1041 * We have to lock the chain before the turnstile so this turnstile
1042 * can be removed from the hash list if it is empty.
1044 turnstile_chain_lock(&m->lock_object);
1045 _mtx_release_lock_quick(m);
1046 ts = turnstile_lookup(&m->lock_object);
1048 if (LOCK_LOG_TEST(&m->lock_object, opts))
1049 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
1050 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1053 * This turnstile is now no longer associated with the mutex. We can
1054 * unlock the chain lock so a new turnstile may take it's place.
1056 turnstile_unpend(ts);
1057 turnstile_chain_unlock(&m->lock_object);
1061 * All the unlocking of MTX_SPIN locks is done inline.
1062 * See the __mtx_unlock_spin() macro for the details.
1066 * The backing function for the INVARIANTS-enabled mtx_assert()
1068 #ifdef INVARIANT_SUPPORT
1070 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
1072 const struct mtx *m;
1074 if (KERNEL_PANICKED() || dumping || SCHEDULER_STOPPED())
1081 case MA_OWNED | MA_RECURSED:
1082 case MA_OWNED | MA_NOTRECURSED:
1084 panic("mutex %s not owned at %s:%d",
1085 m->lock_object.lo_name, file, line);
1086 if (mtx_recursed(m)) {
1087 if ((what & MA_NOTRECURSED) != 0)
1088 panic("mutex %s recursed at %s:%d",
1089 m->lock_object.lo_name, file, line);
1090 } else if ((what & MA_RECURSED) != 0) {
1091 panic("mutex %s unrecursed at %s:%d",
1092 m->lock_object.lo_name, file, line);
1097 panic("mutex %s owned at %s:%d",
1098 m->lock_object.lo_name, file, line);
1101 panic("unknown mtx_assert at %s:%d", file, line);
1107 * General init routine used by the MTX_SYSINIT() macro.
1110 mtx_sysinit(void *arg)
1112 struct mtx_args *margs = arg;
1114 mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
1119 * Mutex initialization routine; initialize lock `m' of type contained in
1120 * `opts' with options contained in `opts' and name `name.' The optional
1121 * lock type `type' is used as a general lock category name for use with
1125 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
1128 struct lock_class *class;
1133 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1134 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1135 ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1136 ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1139 /* Determine lock class and lock flags. */
1140 if (opts & MTX_SPIN)
1141 class = &lock_class_mtx_spin;
1143 class = &lock_class_mtx_sleep;
1145 if (opts & MTX_QUIET)
1147 if (opts & MTX_RECURSE)
1148 flags |= LO_RECURSABLE;
1149 if ((opts & MTX_NOWITNESS) == 0)
1150 flags |= LO_WITNESS;
1151 if (opts & MTX_DUPOK)
1153 if (opts & MTX_NOPROFILE)
1154 flags |= LO_NOPROFILE;
1158 /* Initialize mutex. */
1159 lock_init(&m->lock_object, class, name, type, flags);
1161 m->mtx_lock = MTX_UNOWNED;
1166 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be
1167 * passed in as a flag here because if the corresponding mtx_init() was
1168 * called with MTX_QUIET set, then it will already be set in the mutex's
1172 _mtx_destroy(volatile uintptr_t *c)
1179 MPASS(mtx_unowned(m));
1181 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
1183 /* Perform the non-mtx related part of mtx_unlock_spin(). */
1184 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1187 TD_LOCKS_DEC(curthread);
1189 lock_profile_release_lock(&m->lock_object);
1190 /* Tell witness this isn't locked to make it happy. */
1191 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1195 m->mtx_lock = MTX_DESTROYED;
1196 lock_destroy(&m->lock_object);
1200 * Intialize the mutex code and system mutexes. This is called from the MD
1201 * startup code prior to mi_startup(). The per-CPU data space needs to be
1202 * setup before this is called.
1208 /* Setup turnstiles so that sleep mutexes work. */
1212 * Initialize mutexes.
1214 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
1215 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
1216 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */
1217 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1218 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
1219 mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
1220 mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
1221 mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
1222 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1226 static void __noinline
1227 _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap)
1232 if (ldap->spin_cnt < 60000000 || kdb_active || KERNEL_PANICKED())
1237 /* If the mutex is unlocked, try again. */
1241 printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
1242 m, m->lock_object.lo_name, td, td->td_tid);
1244 witness_display_spinlock(&m->lock_object, td, printf);
1246 panic("spin lock held too long");
1252 mtx_spin_wait_unlocked(struct mtx *m)
1254 struct lock_delay_arg lda;
1256 KASSERT(m->mtx_lock != MTX_DESTROYED,
1257 ("%s() of destroyed mutex %p", __func__, m));
1258 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
1259 ("%s() of sleep mutex %p (%s)", __func__, m,
1260 m->lock_object.lo_name));
1261 KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1262 m->lock_object.lo_name));
1266 while (atomic_load_acq_ptr(&m->mtx_lock) != MTX_UNOWNED) {
1267 if (__predict_true(lda.spin_cnt < 10000000)) {
1271 _mtx_lock_indefinite_check(m, &lda);
1278 db_show_mtx(const struct lock_object *lock)
1281 const struct mtx *m;
1283 m = (const struct mtx *)lock;
1285 db_printf(" flags: {");
1286 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1290 if (m->lock_object.lo_flags & LO_RECURSABLE)
1291 db_printf(", RECURSE");
1292 if (m->lock_object.lo_flags & LO_DUPOK)
1293 db_printf(", DUPOK");
1295 db_printf(" state: {");
1297 db_printf("UNOWNED");
1298 else if (mtx_destroyed(m))
1299 db_printf("DESTROYED");
1302 if (m->mtx_lock & MTX_CONTESTED)
1303 db_printf(", CONTESTED");
1304 if (m->mtx_lock & MTX_RECURSED)
1305 db_printf(", RECURSED");
1308 if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1310 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1311 td->td_tid, td->td_proc->p_pid, td->td_name);
1312 if (mtx_recursed(m))
1313 db_printf(" recursed: %d\n", m->mtx_recurse);