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_global.h"
42 #include "opt_mutex_wake_all.h"
43 #include "opt_sched.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
50 #include <sys/kernel.h>
53 #include <sys/malloc.h>
54 #include <sys/mutex.h>
56 #include <sys/resourcevar.h>
57 #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>
76 * Force MUTEX_WAKE_ALL for now.
77 * single thread wakeup needs fixes to avoid race conditions with
78 * priority inheritance.
80 #ifndef MUTEX_WAKE_ALL
81 #define MUTEX_WAKE_ALL
85 * Internal utility macros.
87 #define mtx_unowned(m) ((m)->mtx_lock == MTX_UNOWNED)
89 #define mtx_owner(m) ((struct thread *)((m)->mtx_lock & ~MTX_FLAGMASK))
92 static void db_show_mtx(struct lock_object *lock);
96 * Lock classes for sleep and spin mutexes.
98 struct lock_class lock_class_mtx_sleep = {
100 LC_SLEEPLOCK | LC_RECURSABLE,
105 struct lock_class lock_class_mtx_spin = {
107 LC_SPINLOCK | LC_RECURSABLE,
114 * System-wide mutexes
116 struct mtx sched_lock;
120 * Function versions of the inlined __mtx_* macros. These are used by
121 * modules and can also be called from assembly language if needed.
124 _mtx_lock_flags(struct mtx *m, int opts, const char *file, int line)
128 MPASS(curthread != NULL);
129 KASSERT(m->mtx_lock != MTX_DESTROYED,
130 ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
131 KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
132 ("mtx_lock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
134 WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
137 lock_profile_waitstart(&waittime);
138 _get_sleep_lock(m, curthread, opts, file, line);
139 LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
141 WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
142 curthread->td_locks++;
143 lock_profile_obtain_lock_success(&m->mtx_object, waittime, file, line);
147 _mtx_unlock_flags(struct mtx *m, int opts, const char *file, int line)
150 MPASS(curthread != NULL);
151 KASSERT(m->mtx_lock != MTX_DESTROYED,
152 ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
153 KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
154 ("mtx_unlock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
156 curthread->td_locks--;
157 WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
158 LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
160 mtx_assert(m, MA_OWNED);
162 lock_profile_release_lock(&m->mtx_object);
163 _rel_sleep_lock(m, curthread, opts, file, line);
167 _mtx_lock_spin_flags(struct mtx *m, int opts, const char *file, int line)
172 MPASS(curthread != NULL);
173 KASSERT(m->mtx_lock != MTX_DESTROYED,
174 ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
175 KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin,
176 ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
177 m->mtx_object.lo_name, file, line));
178 WITNESS_CHECKORDER(&m->mtx_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
180 lock_profile_waitstart(&waittime);
181 _get_spin_lock(m, curthread, opts, file, line);
182 LOCK_LOG_LOCK("LOCK", &m->mtx_object, opts, m->mtx_recurse, file,
184 WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
185 lock_profile_obtain_lock_success(&m->mtx_object, waittime, file, line);
189 _mtx_unlock_spin_flags(struct mtx *m, int opts, const char *file, int line)
192 MPASS(curthread != NULL);
193 KASSERT(m->mtx_lock != MTX_DESTROYED,
194 ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
195 KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin,
196 ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
197 m->mtx_object.lo_name, file, line));
198 WITNESS_UNLOCK(&m->mtx_object, opts | LOP_EXCLUSIVE, file, line);
199 LOCK_LOG_LOCK("UNLOCK", &m->mtx_object, opts, m->mtx_recurse, file,
201 mtx_assert(m, MA_OWNED);
202 lock_profile_release_lock(&m->mtx_object);
207 * The important part of mtx_trylock{,_flags}()
208 * Tries to acquire lock `m.' If this function is called on a mutex that
209 * is already owned, it will recursively acquire the lock.
212 _mtx_trylock(struct mtx *m, int opts, const char *file, int line)
215 uint64_t waittime = 0;
217 MPASS(curthread != NULL);
218 KASSERT(m->mtx_lock != MTX_DESTROYED,
219 ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
220 KASSERT(LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_sleep,
221 ("mtx_trylock() of spin mutex %s @ %s:%d", m->mtx_object.lo_name,
224 if (mtx_owned(m) && (m->mtx_object.lo_flags & LO_RECURSABLE) != 0) {
226 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
229 rval = _obtain_lock(m, (uintptr_t)curthread);
231 LOCK_LOG_TRY("LOCK", &m->mtx_object, opts, rval, file, line);
233 WITNESS_LOCK(&m->mtx_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
235 curthread->td_locks++;
236 lock_profile_obtain_lock_success(&m->mtx_object, waittime, file, line);
244 * _mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
246 * We call this if the lock is either contested (i.e. we need to go to
247 * sleep waiting for it), or if we need to recurse on it.
250 _mtx_lock_sleep(struct mtx *m, uintptr_t tid, int opts, const char *file,
253 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
254 volatile struct thread *owner;
263 KASSERT((m->mtx_object.lo_flags & LO_RECURSABLE) != 0,
264 ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
265 m->mtx_object.lo_name, file, line));
267 atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
268 if (LOCK_LOG_TEST(&m->mtx_object, opts))
269 CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
273 if (LOCK_LOG_TEST(&m->mtx_object, opts))
275 "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
276 m->mtx_object.lo_name, (void *)m->mtx_lock, file, line);
278 while (!_obtain_lock(m, tid)) {
279 lock_profile_obtain_lock_failed(&m->mtx_object, &contested);
280 turnstile_lock(&m->mtx_object);
284 * Check if the lock has been released while spinning for
285 * the turnstile chain lock.
287 if (v == MTX_UNOWNED) {
288 turnstile_release(&m->mtx_object);
293 #ifdef MUTEX_WAKE_ALL
294 MPASS(v != MTX_CONTESTED);
297 * The mutex was marked contested on release. This means that
298 * there are other threads blocked on it. Grab ownership of
299 * it and propagate its priority to the current thread if
302 if (v == MTX_CONTESTED) {
303 m->mtx_lock = tid | MTX_CONTESTED;
304 turnstile_claim(&m->mtx_object);
310 * If the mutex isn't already contested and a failure occurs
311 * setting the contested bit, the mutex was either released
312 * or the state of the MTX_RECURSED bit changed.
314 if ((v & MTX_CONTESTED) == 0 &&
315 !atomic_cmpset_ptr(&m->mtx_lock, v, v | MTX_CONTESTED)) {
316 turnstile_release(&m->mtx_object);
321 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
323 * If the current owner of the lock is executing on another
324 * CPU, spin instead of blocking.
326 owner = (struct thread *)(v & ~MTX_FLAGMASK);
327 #ifdef ADAPTIVE_GIANT
328 if (TD_IS_RUNNING(owner)) {
330 if (m != &Giant && TD_IS_RUNNING(owner)) {
332 turnstile_release(&m->mtx_object);
333 while (mtx_owner(m) == owner && TD_IS_RUNNING(owner)) {
338 #endif /* SMP && !NO_ADAPTIVE_MUTEXES */
341 * We definitely must sleep for this lock.
343 mtx_assert(m, MA_NOTOWNED);
348 "contention: %p at %s:%d wants %s, taken by %s:%d",
349 (void *)tid, file, line, m->mtx_object.lo_name,
350 WITNESS_FILE(&m->mtx_object),
351 WITNESS_LINE(&m->mtx_object));
357 * Block on the turnstile.
359 turnstile_wait(&m->mtx_object, mtx_owner(m),
366 "contention end: %s acquired by %p at %s:%d",
367 m->mtx_object.lo_name, (void *)tid, file, line);
370 #ifdef LOCK_PROFILING
372 m->mtx_object.lo_profile_obj.lpo_contest_locking++;
373 m->mtx_object.lo_profile_obj.lpo_contest_holding = 0;
380 * _mtx_lock_spin: the tougher part of acquiring an MTX_SPIN lock.
382 * This is only called if we need to actually spin for the lock. Recursion
386 _mtx_lock_spin(struct mtx *m, uintptr_t tid, int opts, const char *file,
390 int contested, i = 0;
392 if (LOCK_LOG_TEST(&m->mtx_object, opts))
393 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
395 while (!_obtain_lock(m, tid)) {
396 lock_profile_obtain_lock_failed(&m->mtx_object, &contested);
398 /* Give interrupts a chance while we spin. */
400 while (m->mtx_lock != MTX_UNOWNED) {
401 if (i++ < 10000000) {
405 if (i < 60000000 || kdb_active || panicstr != NULL)
410 /* If the mutex is unlocked, try again. */
414 "spin lock %p (%s) held by %p (tid %d) too long\n",
415 m, m->mtx_object.lo_name, td, td->td_tid);
417 witness_display_spinlock(&m->mtx_object, td);
419 panic("spin lock held too long");
426 if (LOCK_LOG_TEST(&m->mtx_object, opts))
427 CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
434 * _mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
436 * We are only called here if the lock is recursed or contested (i.e. we
437 * need to wake up a blocked thread).
440 _mtx_unlock_sleep(struct mtx *m, int opts, const char *file, int line)
442 struct turnstile *ts;
444 struct thread *td, *td1;
447 if (mtx_recursed(m)) {
448 if (--(m->mtx_recurse) == 0)
449 atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
450 if (LOCK_LOG_TEST(&m->mtx_object, opts))
451 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
455 turnstile_lock(&m->mtx_object);
456 ts = turnstile_lookup(&m->mtx_object);
457 if (LOCK_LOG_TEST(&m->mtx_object, opts))
458 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
460 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
462 _release_lock_quick(m);
463 if (LOCK_LOG_TEST(&m->mtx_object, opts))
464 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p no sleepers", m);
465 turnstile_release(&m->mtx_object);
473 td1 = turnstile_head(ts, TS_EXCLUSIVE_QUEUE);
475 #ifdef MUTEX_WAKE_ALL
476 turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
477 _release_lock_quick(m);
479 if (turnstile_signal(ts, TS_EXCLUSIVE_QUEUE)) {
480 _release_lock_quick(m);
481 if (LOCK_LOG_TEST(&m->mtx_object, opts))
482 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p not held", m);
484 m->mtx_lock = MTX_CONTESTED;
485 if (LOCK_LOG_TEST(&m->mtx_object, opts))
486 CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p still contested",
490 turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
494 * XXX: This is just a hack until preemption is done. However,
495 * once preemption is done we need to either wrap the
496 * turnstile_signal() and release of the actual lock in an
497 * extra critical section or change the preemption code to
498 * always just set a flag and never do instant-preempts.
501 if (td->td_critnest > 0 || td1->td_priority >= td->td_priority)
503 mtx_lock_spin(&sched_lock);
504 if (!TD_IS_RUNNING(td1)) {
506 if (td->td_ithd != NULL) {
507 struct ithd *it = td->td_ithd;
509 if (it->it_interrupted) {
510 if (LOCK_LOG_TEST(&m->mtx_object, opts))
512 "_mtx_unlock_sleep: %p interrupted %p",
513 it, it->it_interrupted);
518 if (LOCK_LOG_TEST(&m->mtx_object, opts))
520 "_mtx_unlock_sleep: %p switching out lock=%p", m,
521 (void *)m->mtx_lock);
523 mi_switch(SW_INVOL, NULL);
524 if (LOCK_LOG_TEST(&m->mtx_object, opts))
525 CTR2(KTR_LOCK, "_mtx_unlock_sleep: %p resuming lock=%p",
526 m, (void *)m->mtx_lock);
528 mtx_unlock_spin(&sched_lock);
535 * All the unlocking of MTX_SPIN locks is done inline.
536 * See the _rel_spin_lock() macro for the details.
540 * The backing function for the INVARIANTS-enabled mtx_assert()
542 #ifdef INVARIANT_SUPPORT
544 _mtx_assert(struct mtx *m, int what, const char *file, int line)
547 if (panicstr != NULL || dumping)
551 case MA_OWNED | MA_RECURSED:
552 case MA_OWNED | MA_NOTRECURSED:
554 panic("mutex %s not owned at %s:%d",
555 m->mtx_object.lo_name, file, line);
556 if (mtx_recursed(m)) {
557 if ((what & MA_NOTRECURSED) != 0)
558 panic("mutex %s recursed at %s:%d",
559 m->mtx_object.lo_name, file, line);
560 } else if ((what & MA_RECURSED) != 0) {
561 panic("mutex %s unrecursed at %s:%d",
562 m->mtx_object.lo_name, file, line);
567 panic("mutex %s owned at %s:%d",
568 m->mtx_object.lo_name, file, line);
571 panic("unknown mtx_assert at %s:%d", file, line);
577 * The MUTEX_DEBUG-enabled mtx_validate()
579 * Most of these checks have been moved off into the LO_INITIALIZED flag
580 * maintained by the witness code.
584 void mtx_validate(struct mtx *);
587 mtx_validate(struct mtx *m)
591 * XXX: When kernacc() does not require Giant we can reenable this check
595 * Can't call kernacc() from early init386(), especially when
596 * initializing Giant mutex, because some stuff in kernacc()
597 * requires Giant itself.
600 if (!kernacc((caddr_t)m, sizeof(m),
601 VM_PROT_READ | VM_PROT_WRITE))
602 panic("Can't read and write to mutex %p", m);
608 * General init routine used by the MTX_SYSINIT() macro.
611 mtx_sysinit(void *arg)
613 struct mtx_args *margs = arg;
615 mtx_init(margs->ma_mtx, margs->ma_desc, NULL, margs->ma_opts);
619 * Mutex initialization routine; initialize lock `m' of type contained in
620 * `opts' with options contained in `opts' and name `name.' The optional
621 * lock type `type' is used as a general lock category name for use with
625 mtx_init(struct mtx *m, const char *name, const char *type, int opts)
627 struct lock_class *class;
630 MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
631 MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE)) == 0);
634 /* Diagnostic and error correction */
638 /* Determine lock class and lock flags. */
640 class = &lock_class_mtx_spin;
642 class = &lock_class_mtx_sleep;
644 if (opts & MTX_QUIET)
646 if (opts & MTX_RECURSE)
647 flags |= LO_RECURSABLE;
648 if ((opts & MTX_NOWITNESS) == 0)
650 if (opts & MTX_DUPOK)
652 if (opts & MTX_NOPROFILE)
653 flags |= LO_NOPROFILE;
655 /* Initialize mutex. */
656 m->mtx_lock = MTX_UNOWNED;
659 lock_profile_object_init(&m->mtx_object, name);
660 lock_init(&m->mtx_object, class, name, type, flags);
664 * Remove lock `m' from all_mtx queue. We don't allow MTX_QUIET to be
665 * passed in as a flag here because if the corresponding mtx_init() was
666 * called with MTX_QUIET set, then it will already be set in the mutex's
670 mtx_destroy(struct mtx *m)
674 MPASS(mtx_unowned(m));
676 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
678 /* Perform the non-mtx related part of mtx_unlock_spin(). */
679 if (LOCK_CLASS(&m->mtx_object) == &lock_class_mtx_spin)
682 curthread->td_locks--;
684 /* Tell witness this isn't locked to make it happy. */
685 WITNESS_UNLOCK(&m->mtx_object, LOP_EXCLUSIVE, __FILE__,
689 m->mtx_lock = MTX_DESTROYED;
690 lock_profile_object_destroy(&m->mtx_object);
691 lock_destroy(&m->mtx_object);
695 * Intialize the mutex code and system mutexes. This is called from the MD
696 * startup code prior to mi_startup(). The per-CPU data space needs to be
697 * setup before this is called.
703 /* Setup turnstiles so that sleep mutexes work. */
707 * Initialize mutexes.
709 mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
710 mtx_init(&sched_lock, "sched lock", NULL, MTX_SPIN | MTX_RECURSE);
711 mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
712 mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
720 db_show_mtx(struct lock_object *lock)
725 m = (struct mtx *)lock;
727 db_printf(" flags: {");
728 if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
732 if (m->mtx_object.lo_flags & LO_RECURSABLE)
733 db_printf(", RECURSE");
734 if (m->mtx_object.lo_flags & LO_DUPOK)
735 db_printf(", DUPOK");
737 db_printf(" state: {");
739 db_printf("UNOWNED");
742 if (m->mtx_lock & MTX_CONTESTED)
743 db_printf(", CONTESTED");
744 if (m->mtx_lock & MTX_RECURSED)
745 db_printf(", RECURSED");
748 if (!mtx_unowned(m)) {
750 db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
751 td->td_tid, td->td_proc->p_pid, td->td_proc->p_comm);
753 db_printf(" recursed: %d\n", m->mtx_recurse);