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;
169 struct mtx __exclusive_cache_line Giant;
172 assert_mtx(const struct lock_object *lock, int what)
175 mtx_assert((const struct mtx *)lock, what);
179 lock_mtx(struct lock_object *lock, uintptr_t how)
182 mtx_lock((struct mtx *)lock);
186 lock_spin(struct lock_object *lock, uintptr_t how)
189 panic("spin locks can only use msleep_spin");
193 unlock_mtx(struct lock_object *lock)
197 m = (struct mtx *)lock;
198 mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
204 unlock_spin(struct lock_object *lock)
207 panic("spin locks can only use msleep_spin");
212 owner_mtx(const struct lock_object *lock, struct thread **owner)
217 m = (const struct mtx *)lock;
219 *owner = (struct thread *)(x & ~MTX_FLAGMASK);
220 return (*owner != NULL);
225 * Function versions of the inlined __mtx_* macros. These are used by
226 * modules and can also be called from assembly language if needed.
229 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
236 KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
237 !TD_IS_IDLETHREAD(curthread),
238 ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
239 curthread, m->lock_object.lo_name, file, line));
240 KASSERT(m->mtx_lock != MTX_DESTROYED,
241 ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
242 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
243 ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
245 WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
246 LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
248 tid = (uintptr_t)curthread;
250 if (!_mtx_obtain_lock_fetch(m, &v, tid))
251 _mtx_lock_sleep(m, v, opts, file, line);
253 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
254 m, 0, 0, file, line);
255 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
257 WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
259 TD_LOCKS_INC(curthread);
263 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
269 KASSERT(m->mtx_lock != MTX_DESTROYED,
270 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
271 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
272 ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
274 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
275 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
277 mtx_assert(m, MA_OWNED);
279 #ifdef LOCK_PROFILING
280 __mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
282 __mtx_unlock(m, curthread, opts, file, line);
284 TD_LOCKS_DEC(curthread);
288 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
298 KASSERT(m->mtx_lock != MTX_DESTROYED,
299 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
300 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
301 ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
302 m->lock_object.lo_name, file, line));
304 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
305 (opts & MTX_RECURSE) != 0,
306 ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
307 m->lock_object.lo_name, file, line));
308 opts &= ~MTX_RECURSE;
309 WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
313 tid = (uintptr_t)curthread;
315 if (!_mtx_obtain_lock_fetch(m, &v, tid))
316 _mtx_lock_spin(m, v, opts, file, line);
318 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire,
319 m, 0, 0, file, line);
321 __mtx_lock_spin(m, curthread, opts, file, line);
323 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
325 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
329 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
334 if (SCHEDULER_STOPPED())
339 KASSERT(m->mtx_lock != MTX_DESTROYED,
340 ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
341 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
342 ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
343 m->lock_object.lo_name, file, line));
344 KASSERT((opts & MTX_RECURSE) == 0,
345 ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
346 m->lock_object.lo_name, file, line));
347 if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
348 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
349 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
352 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
357 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
364 KASSERT(m->mtx_lock != MTX_DESTROYED,
365 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
366 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
367 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
368 m->lock_object.lo_name, file, line));
369 WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
370 LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
372 mtx_assert(m, MA_OWNED);
374 __mtx_unlock_spin(m);
378 * The important part of mtx_trylock{,_flags}()
379 * Tries to acquire lock `m.' If this function is called on a mutex that
380 * is already owned, it will recursively acquire the lock.
383 _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
387 #ifdef LOCK_PROFILING
388 uint64_t waittime = 0;
396 if (SCHEDULER_STOPPED_TD(td))
399 KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
400 ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
401 curthread, m->lock_object.lo_name, file, line));
402 KASSERT(m->mtx_lock != MTX_DESTROYED,
403 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
404 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
405 ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
412 if (_mtx_obtain_lock_fetch(m, &v, tid))
414 if (v == MTX_UNOWNED)
417 ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
418 (opts & MTX_RECURSE) != 0)) {
420 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
428 opts &= ~MTX_RECURSE;
430 LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
432 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
434 TD_LOCKS_INC(curthread);
436 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
437 m, contested, waittime, file, line);
444 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
449 return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
453 * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
455 * We call this if the lock is either contested (i.e. we need to go to
456 * sleep waiting for it), or if we need to recurse on it.
460 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
464 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
469 struct turnstile *ts;
471 struct thread *owner;
475 #ifdef LOCK_PROFILING
477 uint64_t waittime = 0;
479 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
480 struct lock_delay_arg lda;
484 int64_t sleep_time = 0;
485 int64_t all_time = 0;
487 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
492 if (SCHEDULER_STOPPED_TD(td))
495 #if defined(ADAPTIVE_MUTEXES)
496 lock_delay_arg_init(&lda, &mtx_delay);
497 #elif defined(KDTRACE_HOOKS)
498 lock_delay_arg_init(&lda, NULL);
501 if (__predict_false(v == MTX_UNOWNED))
502 v = MTX_READ_VALUE(m);
504 if (__predict_false(lv_mtx_owner(v) == td)) {
505 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
506 (opts & MTX_RECURSE) != 0,
507 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
508 m->lock_object.lo_name, file, line));
510 opts &= ~MTX_RECURSE;
513 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
514 if (LOCK_LOG_TEST(&m->lock_object, opts))
515 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
519 opts &= ~MTX_RECURSE;
523 PMC_SOFT_CALL( , , lock, failed);
525 lock_profile_obtain_lock_failed(&m->lock_object,
526 &contested, &waittime);
527 if (LOCK_LOG_TEST(&m->lock_object, opts))
529 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
530 m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
531 #ifdef LOCK_PROFILING
533 #elif defined(KDTRACE_HOOKS)
534 doing_lockprof = lockstat_enabled;
535 if (__predict_false(doing_lockprof))
536 all_time -= lockstat_nsecs(&m->lock_object);
540 if (v == MTX_UNOWNED) {
541 if (_mtx_obtain_lock_fetch(m, &v, tid))
548 #ifdef ADAPTIVE_MUTEXES
550 * If the owner is running on another CPU, spin until the
551 * owner stops running or the state of the lock changes.
553 owner = lv_mtx_owner(v);
554 if (TD_IS_RUNNING(owner)) {
555 if (LOCK_LOG_TEST(&m->lock_object, 0))
557 "%s: spinning on %p held by %p",
559 KTR_STATE1(KTR_SCHED, "thread",
560 sched_tdname((struct thread *)tid),
561 "spinning", "lockname:\"%s\"",
562 m->lock_object.lo_name);
565 v = MTX_READ_VALUE(m);
566 owner = lv_mtx_owner(v);
567 } while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
568 KTR_STATE0(KTR_SCHED, "thread",
569 sched_tdname((struct thread *)tid),
575 ts = turnstile_trywait(&m->lock_object);
576 v = MTX_READ_VALUE(m);
579 * Check if the lock has been released while spinning for
580 * the turnstile chain lock.
582 if (v == MTX_UNOWNED) {
583 turnstile_cancel(ts);
587 #ifdef ADAPTIVE_MUTEXES
589 * The current lock owner might have started executing
590 * on another CPU (or the lock could have changed
591 * owners) while we were waiting on the turnstile
592 * chain lock. If so, drop the turnstile lock and try
595 owner = lv_mtx_owner(v);
596 if (TD_IS_RUNNING(owner)) {
597 turnstile_cancel(ts);
603 * If the mutex isn't already contested and a failure occurs
604 * setting the contested bit, the mutex was either released
605 * or the state of the MTX_RECURSED bit changed.
607 if ((v & MTX_CONTESTED) == 0 &&
608 !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
609 turnstile_cancel(ts);
610 v = MTX_READ_VALUE(m);
615 * We definitely must sleep for this lock.
617 mtx_assert(m, MA_NOTOWNED);
622 "contention: %p at %s:%d wants %s, taken by %s:%d",
623 (void *)tid, file, line, m->lock_object.lo_name,
624 WITNESS_FILE(&m->lock_object),
625 WITNESS_LINE(&m->lock_object));
631 * Block on the turnstile.
634 sleep_time -= lockstat_nsecs(&m->lock_object);
636 #ifndef ADAPTIVE_MUTEXES
637 owner = mtx_owner(m);
639 MPASS(owner == mtx_owner(m));
640 turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
642 sleep_time += lockstat_nsecs(&m->lock_object);
645 v = MTX_READ_VALUE(m);
650 "contention end: %s acquired by %p at %s:%d",
651 m->lock_object.lo_name, (void *)tid, file, line);
654 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
655 if (__predict_true(!doing_lockprof))
659 all_time += lockstat_nsecs(&m->lock_object);
661 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
662 waittime, file, line);
665 LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
668 * Only record the loops spinning and not sleeping.
670 if (lda.spin_cnt > sleep_cnt)
671 LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
676 _mtx_lock_spin_failed(struct mtx *m)
682 /* If the mutex is unlocked, try again. */
686 printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
687 m, m->lock_object.lo_name, td, td->td_tid);
689 witness_display_spinlock(&m->lock_object, td, printf);
691 panic("spin lock held too long");
696 * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
698 * This is only called if we need to actually spin for the lock. Recursion
703 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
704 const char *file, int line)
707 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
711 struct lock_delay_arg lda;
713 #ifdef LOCK_PROFILING
715 uint64_t waittime = 0;
718 int64_t spin_time = 0;
720 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
724 tid = (uintptr_t)curthread;
727 if (__predict_false(v == MTX_UNOWNED))
728 v = MTX_READ_VALUE(m);
730 if (__predict_false(v == tid)) {
735 if (SCHEDULER_STOPPED())
738 lock_delay_arg_init(&lda, &mtx_spin_delay);
740 if (LOCK_LOG_TEST(&m->lock_object, opts))
741 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
742 KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
743 "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
746 PMC_SOFT_CALL( , , lock, failed);
748 lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
749 #ifdef LOCK_PROFILING
751 #elif defined(KDTRACE_HOOKS)
752 doing_lockprof = lockstat_enabled;
753 if (__predict_false(doing_lockprof))
754 spin_time -= lockstat_nsecs(&m->lock_object);
757 if (v == MTX_UNOWNED) {
758 if (_mtx_obtain_lock_fetch(m, &v, tid))
762 /* Give interrupts a chance while we spin. */
765 if (lda.spin_cnt < 10000000) {
769 if (lda.spin_cnt < 60000000 || kdb_active ||
773 _mtx_lock_spin_failed(m);
776 v = MTX_READ_VALUE(m);
777 } while (v != MTX_UNOWNED);
781 if (LOCK_LOG_TEST(&m->lock_object, opts))
782 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
783 KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
786 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
787 if (__predict_true(!doing_lockprof))
791 spin_time += lockstat_nsecs(&m->lock_object);
793 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
794 contested, waittime, file, line);
796 if (lda.spin_cnt != 0)
797 LOCKSTAT_RECORD1(spin__spin, m, spin_time);
804 thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
807 KASSERT(m->mtx_lock != MTX_DESTROYED,
808 ("thread_lock() of destroyed mutex @ %s:%d", file, line));
809 KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
810 ("thread_lock() of sleep mutex %s @ %s:%d",
811 m->lock_object.lo_name, file, line));
813 KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
814 ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
815 m->lock_object.lo_name, file, line));
816 WITNESS_CHECKORDER(&m->lock_object,
817 opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
820 #define thread_lock_validate(m, opts, file, line) do { } while (0)
823 #ifndef LOCK_PROFILING
826 _thread_lock(struct thread *td, int opts, const char *file, int line)
829 _thread_lock(struct thread *td)
835 tid = (uintptr_t)curthread;
839 thread_lock_validate(m, 0, file, line);
840 v = MTX_READ_VALUE(m);
841 if (__predict_true(v == MTX_UNOWNED)) {
842 if (__predict_false(!_mtx_obtain_lock(m, tid)))
843 goto slowpath_unlocked;
844 } else if (v == tid) {
847 goto slowpath_unlocked;
848 if (__predict_true(m == td->td_lock)) {
849 WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
852 if (m->mtx_recurse != 0)
855 _mtx_release_lock_quick(m);
858 thread_lock_flags_(td, 0, 0, 0);
863 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
867 struct lock_delay_arg lda;
868 #ifdef LOCK_PROFILING
870 uint64_t waittime = 0;
873 int64_t spin_time = 0;
875 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
876 int doing_lockprof = 1;
879 tid = (uintptr_t)curthread;
881 if (SCHEDULER_STOPPED()) {
883 * Ensure that spinlock sections are balanced even when the
884 * scheduler is stopped, since we may otherwise inadvertently
885 * re-enable interrupts while dumping core.
891 lock_delay_arg_init(&lda, &mtx_spin_delay);
893 #ifdef LOCK_PROFILING
895 #elif defined(KDTRACE_HOOKS)
896 doing_lockprof = lockstat_enabled;
897 if (__predict_false(doing_lockprof))
898 spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
905 thread_lock_validate(m, opts, file, line);
907 if (_mtx_obtain_lock_fetch(m, &v, tid))
909 if (v == MTX_UNOWNED)
916 PMC_SOFT_CALL( , , lock, failed);
918 lock_profile_obtain_lock_failed(&m->lock_object,
919 &contested, &waittime);
920 /* Give interrupts a chance while we spin. */
923 if (lda.spin_cnt < 10000000) {
927 if (lda.spin_cnt < 60000000 ||
928 kdb_active || panicstr != NULL)
931 _mtx_lock_spin_failed(m);
934 if (m != td->td_lock)
936 v = MTX_READ_VALUE(m);
937 } while (v != MTX_UNOWNED);
940 if (m == td->td_lock)
942 __mtx_unlock_spin(m); /* does spinlock_exit() */
944 LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
946 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
948 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
949 if (__predict_true(!doing_lockprof))
953 spin_time += lockstat_nsecs(&m->lock_object);
955 if (m->mtx_recurse == 0)
956 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
957 contested, waittime, file, line);
959 if (lda.spin_cnt != 0)
960 LOCKSTAT_RECORD1(thread__spin, m, spin_time);
965 thread_lock_block(struct thread *td)
969 THREAD_LOCK_ASSERT(td, MA_OWNED);
971 td->td_lock = &blocked_lock;
972 mtx_unlock_spin(lock);
978 thread_lock_unblock(struct thread *td, struct mtx *new)
980 mtx_assert(new, MA_OWNED);
981 MPASS(td->td_lock == &blocked_lock);
982 atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
986 thread_lock_set(struct thread *td, struct mtx *new)
990 mtx_assert(new, MA_OWNED);
991 THREAD_LOCK_ASSERT(td, MA_OWNED);
994 mtx_unlock_spin(lock);
998 * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
1000 * We are only called here if the lock is recursed, contested (i.e. we
1001 * need to wake up a blocked thread) or lockstat probe is active.
1005 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
1006 const char *file, int line)
1009 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
1013 struct turnstile *ts;
1016 if (SCHEDULER_STOPPED())
1019 tid = (uintptr_t)curthread;
1022 if (__predict_false(v == tid))
1023 v = MTX_READ_VALUE(m);
1025 if (__predict_false(v & MTX_RECURSED)) {
1026 if (--(m->mtx_recurse) == 0)
1027 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1028 if (LOCK_LOG_TEST(&m->lock_object, opts))
1029 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
1033 LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
1034 if (v == tid && _mtx_release_lock(m, tid))
1038 * We have to lock the chain before the turnstile so this turnstile
1039 * can be removed from the hash list if it is empty.
1041 turnstile_chain_lock(&m->lock_object);
1042 _mtx_release_lock_quick(m);
1043 ts = turnstile_lookup(&m->lock_object);
1045 if (LOCK_LOG_TEST(&m->lock_object, opts))
1046 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
1047 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1050 * This turnstile is now no longer associated with the mutex. We can
1051 * unlock the chain lock so a new turnstile may take it's place.
1053 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
1054 turnstile_chain_unlock(&m->lock_object);
1058 * All the unlocking of MTX_SPIN locks is done inline.
1059 * See the __mtx_unlock_spin() macro for the details.
1063 * The backing function for the INVARIANTS-enabled mtx_assert()
1065 #ifdef INVARIANT_SUPPORT
1067 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
1069 const struct mtx *m;
1071 if (panicstr != NULL || dumping || SCHEDULER_STOPPED())
1078 case MA_OWNED | MA_RECURSED:
1079 case MA_OWNED | MA_NOTRECURSED:
1081 panic("mutex %s not owned at %s:%d",
1082 m->lock_object.lo_name, file, line);
1083 if (mtx_recursed(m)) {
1084 if ((what & MA_NOTRECURSED) != 0)
1085 panic("mutex %s recursed at %s:%d",
1086 m->lock_object.lo_name, file, line);
1087 } else if ((what & MA_RECURSED) != 0) {
1088 panic("mutex %s unrecursed at %s:%d",
1089 m->lock_object.lo_name, file, line);
1094 panic("mutex %s owned at %s:%d",
1095 m->lock_object.lo_name, file, line);
1098 panic("unknown mtx_assert at %s:%d", file, line);
1104 * General init routine used by the MTX_SYSINIT() macro.
1107 mtx_sysinit(void *arg)
1109 struct mtx_args *margs = arg;
1111 mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
1116 * Mutex initialization routine; initialize lock `m' of type contained in
1117 * `opts' with options contained in `opts' and name `name.' The optional
1118 * lock type `type' is used as a general lock category name for use with
1122 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
1125 struct lock_class *class;
1130 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1131 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1132 ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1133 ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1136 /* Determine lock class and lock flags. */
1137 if (opts & MTX_SPIN)
1138 class = &lock_class_mtx_spin;
1140 class = &lock_class_mtx_sleep;
1142 if (opts & MTX_QUIET)
1144 if (opts & MTX_RECURSE)
1145 flags |= LO_RECURSABLE;
1146 if ((opts & MTX_NOWITNESS) == 0)
1147 flags |= LO_WITNESS;
1148 if (opts & MTX_DUPOK)
1150 if (opts & MTX_NOPROFILE)
1151 flags |= LO_NOPROFILE;
1155 /* Initialize mutex. */
1156 lock_init(&m->lock_object, class, name, type, flags);
1158 m->mtx_lock = MTX_UNOWNED;
1163 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be
1164 * passed in as a flag here because if the corresponding mtx_init() was
1165 * called with MTX_QUIET set, then it will already be set in the mutex's
1169 _mtx_destroy(volatile uintptr_t *c)
1176 MPASS(mtx_unowned(m));
1178 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
1180 /* Perform the non-mtx related part of mtx_unlock_spin(). */
1181 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1184 TD_LOCKS_DEC(curthread);
1186 lock_profile_release_lock(&m->lock_object);
1187 /* Tell witness this isn't locked to make it happy. */
1188 WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1192 m->mtx_lock = MTX_DESTROYED;
1193 lock_destroy(&m->lock_object);
1197 * Intialize the mutex code and system mutexes. This is called from the MD
1198 * startup code prior to mi_startup(). The per-CPU data space needs to be
1199 * setup before this is called.
1205 /* Setup turnstiles so that sleep mutexes work. */
1209 * Initialize mutexes.
1211 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
1212 mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
1213 blocked_lock.mtx_lock = 0xdeadc0de; /* Always blocked. */
1214 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1215 mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
1216 mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
1217 mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
1218 mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
1219 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1225 db_show_mtx(const struct lock_object *lock)
1228 const struct mtx *m;
1230 m = (const struct mtx *)lock;
1232 db_printf(" flags: {");
1233 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1237 if (m->lock_object.lo_flags & LO_RECURSABLE)
1238 db_printf(", RECURSE");
1239 if (m->lock_object.lo_flags & LO_DUPOK)
1240 db_printf(", DUPOK");
1242 db_printf(" state: {");
1244 db_printf("UNOWNED");
1245 else if (mtx_destroyed(m))
1246 db_printf("DESTROYED");
1249 if (m->mtx_lock & MTX_CONTESTED)
1250 db_printf(", CONTESTED");
1251 if (m->mtx_lock & MTX_RECURSED)
1252 db_printf(", RECURSED");
1255 if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1257 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1258 td->td_tid, td->td_proc->p_pid, td->td_name);
1259 if (mtx_recursed(m))
1260 db_printf(" recursed: %d\n", m->mtx_recurse);