]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_mutex.c
locks: pass the found lock value to unlock slow path
[FreeBSD/FreeBSD.git] / sys / kern / kern_mutex.c
1 /*-
2  * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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
14  *    written permission.
15  *
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
26  * SUCH DAMAGE.
27  *
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 $
30  */
31
32 /*
33  * Machine independent bits of mutex implementation.
34  */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "opt_adaptive_mutexes.h"
40 #include "opt_ddb.h"
41 #include "opt_hwpmc_hooks.h"
42 #include "opt_sched.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/bus.h>
47 #include <sys/conf.h>
48 #include <sys/kdb.h>
49 #include <sys/kernel.h>
50 #include <sys/ktr.h>
51 #include <sys/lock.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 #include <sys/proc.h>
55 #include <sys/resourcevar.h>
56 #include <sys/sched.h>
57 #include <sys/sbuf.h>
58 #include <sys/smp.h>
59 #include <sys/sysctl.h>
60 #include <sys/turnstile.h>
61 #include <sys/vmmeter.h>
62 #include <sys/lock_profile.h>
63
64 #include <machine/atomic.h>
65 #include <machine/bus.h>
66 #include <machine/cpu.h>
67
68 #include <ddb/ddb.h>
69
70 #include <fs/devfs/devfs_int.h>
71
72 #include <vm/vm.h>
73 #include <vm/vm_extern.h>
74
75 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
76 #define ADAPTIVE_MUTEXES
77 #endif
78
79 #ifdef HWPMC_HOOKS
80 #include <sys/pmckern.h>
81 PMC_SOFT_DEFINE( , , lock, failed);
82 #endif
83
84 /*
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.
87  */
88 #define mtxlock2mtx(c)  (__containerof(c, struct mtx, mtx_lock))
89
90 /*
91  * Internal utility macros.
92  */
93 #define mtx_unowned(m)  ((m)->mtx_lock == MTX_UNOWNED)
94
95 #define mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
96
97 static void     assert_mtx(const struct lock_object *lock, int what);
98 #ifdef DDB
99 static void     db_show_mtx(const struct lock_object *lock);
100 #endif
101 static void     lock_mtx(struct lock_object *lock, uintptr_t how);
102 static void     lock_spin(struct lock_object *lock, uintptr_t how);
103 #ifdef KDTRACE_HOOKS
104 static int      owner_mtx(const struct lock_object *lock,
105                     struct thread **owner);
106 #endif
107 static uintptr_t unlock_mtx(struct lock_object *lock);
108 static uintptr_t unlock_spin(struct lock_object *lock);
109
110 /*
111  * Lock classes for sleep and spin mutexes.
112  */
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,
117 #ifdef DDB
118         .lc_ddb_show = db_show_mtx,
119 #endif
120         .lc_lock = lock_mtx,
121         .lc_unlock = unlock_mtx,
122 #ifdef KDTRACE_HOOKS
123         .lc_owner = owner_mtx,
124 #endif
125 };
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,
130 #ifdef DDB
131         .lc_ddb_show = db_show_mtx,
132 #endif
133         .lc_lock = lock_spin,
134         .lc_unlock = unlock_spin,
135 #ifdef KDTRACE_HOOKS
136         .lc_owner = owner_mtx,
137 #endif
138 };
139
140 #ifdef ADAPTIVE_MUTEXES
141 static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging");
142
143 static struct lock_delay_config __read_frequently mtx_delay;
144
145 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
146     0, "");
147 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
148     0, "");
149
150 LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
151 #endif
152
153 static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL,
154     "mtx spin debugging");
155
156 static struct lock_delay_config __read_frequently mtx_spin_delay;
157
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, "");
162
163 LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
164
165 /*
166  * System-wide mutexes
167  */
168 struct mtx blocked_lock;
169 struct mtx __exclusive_cache_line Giant;
170
171 void
172 assert_mtx(const struct lock_object *lock, int what)
173 {
174
175         mtx_assert((const struct mtx *)lock, what);
176 }
177
178 void
179 lock_mtx(struct lock_object *lock, uintptr_t how)
180 {
181
182         mtx_lock((struct mtx *)lock);
183 }
184
185 void
186 lock_spin(struct lock_object *lock, uintptr_t how)
187 {
188
189         panic("spin locks can only use msleep_spin");
190 }
191
192 uintptr_t
193 unlock_mtx(struct lock_object *lock)
194 {
195         struct mtx *m;
196
197         m = (struct mtx *)lock;
198         mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
199         mtx_unlock(m);
200         return (0);
201 }
202
203 uintptr_t
204 unlock_spin(struct lock_object *lock)
205 {
206
207         panic("spin locks can only use msleep_spin");
208 }
209
210 #ifdef KDTRACE_HOOKS
211 int
212 owner_mtx(const struct lock_object *lock, struct thread **owner)
213 {
214         const struct mtx *m;
215         uintptr_t x;
216
217         m = (const struct mtx *)lock;
218         x = m->mtx_lock;
219         *owner = (struct thread *)(x & ~MTX_FLAGMASK);
220         return (*owner != NULL);
221 }
222 #endif
223
224 /*
225  * Function versions of the inlined __mtx_* macros.  These are used by
226  * modules and can also be called from assembly language if needed.
227  */
228 void
229 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
230 {
231         struct mtx *m;
232         uintptr_t tid, v;
233
234         m = mtxlock2mtx(c);
235
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,
244             file, line));
245         WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
246             LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
247
248         tid = (uintptr_t)curthread;
249         v = MTX_UNOWNED;
250         if (!_mtx_obtain_lock_fetch(m, &v, tid))
251                 _mtx_lock_sleep(m, v, opts, file, line);
252         else
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,
256             line);
257         WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
258             file, line);
259         TD_LOCKS_INC(curthread);
260 }
261
262 void
263 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
264 {
265         struct mtx *m;
266
267         m = mtxlock2mtx(c);
268
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,
273             file, line));
274         WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
275         LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
276             line);
277         mtx_assert(m, MA_OWNED);
278
279 #ifdef LOCK_PROFILING
280         __mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
281 #else
282         __mtx_unlock(m, curthread, opts, file, line);
283 #endif
284         TD_LOCKS_DEC(curthread);
285 }
286
287 void
288 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
289     int line)
290 {
291         struct mtx *m;
292 #ifdef SMP
293         uintptr_t tid, v;
294 #endif
295
296         m = mtxlock2mtx(c);
297
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));
303         if (mtx_owned(m))
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,
310             file, line, NULL);
311 #ifdef SMP
312         spinlock_enter();
313         tid = (uintptr_t)curthread;
314         v = MTX_UNOWNED;
315         if (!_mtx_obtain_lock_fetch(m, &v, tid))
316                 _mtx_lock_spin(m, v, opts, file, line);
317         else
318                 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire,
319                     m, 0, 0, file, line);
320 #else
321         __mtx_lock_spin(m, curthread, opts, file, line);
322 #endif
323         LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
324             line);
325         WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
326 }
327
328 int
329 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
330     int line)
331 {
332         struct mtx *m;
333
334         if (SCHEDULER_STOPPED())
335                 return (1);
336
337         m = mtxlock2mtx(c);
338
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);
350                 return (1);
351         }
352         LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
353         return (0);
354 }
355
356 void
357 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
358     int line)
359 {
360         struct mtx *m;
361
362         m = mtxlock2mtx(c);
363
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,
371             line);
372         mtx_assert(m, MA_OWNED);
373
374         __mtx_unlock_spin(m);
375 }
376
377 /*
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.
381  */
382 int
383 _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
384 {
385         struct thread *td;
386         uintptr_t tid, v;
387 #ifdef LOCK_PROFILING
388         uint64_t waittime = 0;
389         int contested = 0;
390 #endif
391         int rval;
392         bool recursed;
393
394         td = curthread;
395         tid = (uintptr_t)td;
396         if (SCHEDULER_STOPPED_TD(td))
397                 return (1);
398
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,
406             file, line));
407
408         rval = 1;
409         recursed = false;
410         v = MTX_UNOWNED;
411         for (;;) {
412                 if (_mtx_obtain_lock_fetch(m, &v, tid))
413                         break;
414                 if (v == MTX_UNOWNED)
415                         continue;
416                 if (v == tid &&
417                     ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
418                     (opts & MTX_RECURSE) != 0)) {
419                         m->mtx_recurse++;
420                         atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
421                         recursed = true;
422                         break;
423                 }
424                 rval = 0;
425                 break;
426         }
427
428         opts &= ~MTX_RECURSE;
429
430         LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
431         if (rval) {
432                 WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
433                     file, line);
434                 TD_LOCKS_INC(curthread);
435                 if (!recursed)
436                         LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
437                             m, contested, waittime, file, line);
438         }
439
440         return (rval);
441 }
442
443 int
444 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
445 {
446         struct mtx *m;
447
448         m = mtxlock2mtx(c);
449         return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
450 }
451
452 /*
453  * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
454  *
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.
457  */
458 #if LOCK_DEBUG > 0
459 void
460 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
461     int line)
462 #else
463 void
464 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
465 #endif
466 {
467         struct thread *td;
468         struct mtx *m;
469         struct turnstile *ts;
470         uintptr_t tid;
471         struct thread *owner;
472 #ifdef KTR
473         int cont_logged = 0;
474 #endif
475 #ifdef LOCK_PROFILING
476         int contested = 0;
477         uint64_t waittime = 0;
478 #endif
479 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
480         struct lock_delay_arg lda;
481 #endif
482 #ifdef KDTRACE_HOOKS
483         u_int sleep_cnt = 0;
484         int64_t sleep_time = 0;
485         int64_t all_time = 0;
486 #endif
487 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
488         int doing_lockprof;
489 #endif
490         td = curthread;
491         tid = (uintptr_t)td;
492         if (SCHEDULER_STOPPED_TD(td))
493                 return;
494
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);
499 #endif
500         m = mtxlock2mtx(c);
501         if (__predict_false(v == MTX_UNOWNED))
502                 v = MTX_READ_VALUE(m);
503
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));
509 #if LOCK_DEBUG > 0
510                 opts &= ~MTX_RECURSE;
511 #endif
512                 m->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);
516                 return;
517         }
518 #if LOCK_DEBUG > 0
519         opts &= ~MTX_RECURSE;
520 #endif
521
522 #ifdef HWPMC_HOOKS
523         PMC_SOFT_CALL( , , lock, failed);
524 #endif
525         lock_profile_obtain_lock_failed(&m->lock_object,
526                     &contested, &waittime);
527         if (LOCK_LOG_TEST(&m->lock_object, opts))
528                 CTR4(KTR_LOCK,
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
532         doing_lockprof = 1;
533 #elif defined(KDTRACE_HOOKS)
534         doing_lockprof = lockstat_enabled;
535         if (__predict_false(doing_lockprof))
536                 all_time -= lockstat_nsecs(&m->lock_object);
537 #endif
538
539         for (;;) {
540                 if (v == MTX_UNOWNED) {
541                         if (_mtx_obtain_lock_fetch(m, &v, tid))
542                                 break;
543                         continue;
544                 }
545 #ifdef KDTRACE_HOOKS
546                 lda.spin_cnt++;
547 #endif
548 #ifdef ADAPTIVE_MUTEXES
549                 /*
550                  * If the owner is running on another CPU, spin until the
551                  * owner stops running or the state of the lock changes.
552                  */
553                 owner = lv_mtx_owner(v);
554                 if (TD_IS_RUNNING(owner)) {
555                         if (LOCK_LOG_TEST(&m->lock_object, 0))
556                                 CTR3(KTR_LOCK,
557                                     "%s: spinning on %p held by %p",
558                                     __func__, m, owner);
559                         KTR_STATE1(KTR_SCHED, "thread",
560                             sched_tdname((struct thread *)tid),
561                             "spinning", "lockname:\"%s\"",
562                             m->lock_object.lo_name);
563                         do {
564                                 lock_delay(&lda);
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),
570                             "running");
571                         continue;
572                 }
573 #endif
574
575                 ts = turnstile_trywait(&m->lock_object);
576                 v = MTX_READ_VALUE(m);
577
578                 /*
579                  * Check if the lock has been released while spinning for
580                  * the turnstile chain lock.
581                  */
582                 if (v == MTX_UNOWNED) {
583                         turnstile_cancel(ts);
584                         continue;
585                 }
586
587 #ifdef ADAPTIVE_MUTEXES
588                 /*
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
593                  * again.
594                  */
595                 owner = lv_mtx_owner(v);
596                 if (TD_IS_RUNNING(owner)) {
597                         turnstile_cancel(ts);
598                         continue;
599                 }
600 #endif
601
602                 /*
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.
606                  */
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);
611                         continue;
612                 }
613
614                 /*
615                  * We definitely must sleep for this lock.
616                  */
617                 mtx_assert(m, MA_NOTOWNED);
618
619 #ifdef KTR
620                 if (!cont_logged) {
621                         CTR6(KTR_CONTENTION,
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));
626                         cont_logged = 1;
627                 }
628 #endif
629
630                 /*
631                  * Block on the turnstile.
632                  */
633 #ifdef KDTRACE_HOOKS
634                 sleep_time -= lockstat_nsecs(&m->lock_object);
635 #endif
636 #ifndef ADAPTIVE_MUTEXES
637                 owner = mtx_owner(m);
638 #endif
639                 MPASS(owner == mtx_owner(m));
640                 turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
641 #ifdef KDTRACE_HOOKS
642                 sleep_time += lockstat_nsecs(&m->lock_object);
643                 sleep_cnt++;
644 #endif
645                 v = MTX_READ_VALUE(m);
646         }
647 #ifdef KTR
648         if (cont_logged) {
649                 CTR4(KTR_CONTENTION,
650                     "contention end: %s acquired by %p at %s:%d",
651                     m->lock_object.lo_name, (void *)tid, file, line);
652         }
653 #endif
654 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
655         if (__predict_true(!doing_lockprof))
656                 return;
657 #endif
658 #ifdef KDTRACE_HOOKS
659         all_time += lockstat_nsecs(&m->lock_object);
660 #endif
661         LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
662             waittime, file, line);
663 #ifdef KDTRACE_HOOKS
664         if (sleep_time)
665                 LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
666
667         /*
668          * Only record the loops spinning and not sleeping.
669          */
670         if (lda.spin_cnt > sleep_cnt)
671                 LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
672 #endif
673 }
674
675 static void
676 _mtx_lock_spin_failed(struct mtx *m)
677 {
678         struct thread *td;
679
680         td = mtx_owner(m);
681
682         /* If the mutex is unlocked, try again. */
683         if (td == NULL)
684                 return;
685
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);
688 #ifdef WITNESS
689         witness_display_spinlock(&m->lock_object, td, printf);
690 #endif
691         panic("spin lock held too long");
692 }
693
694 #ifdef SMP
695 /*
696  * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
697  *
698  * This is only called if we need to actually spin for the lock. Recursion
699  * is handled inline.
700  */
701 #if LOCK_DEBUG > 0
702 void
703 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
704     const char *file, int line)
705 #else
706 void
707 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
708 #endif
709 {
710         struct mtx *m;
711         struct lock_delay_arg lda;
712         uintptr_t tid;
713 #ifdef LOCK_PROFILING
714         int contested = 0;
715         uint64_t waittime = 0;
716 #endif
717 #ifdef KDTRACE_HOOKS
718         int64_t spin_time = 0;
719 #endif
720 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
721         int doing_lockprof;
722 #endif
723
724         tid = (uintptr_t)curthread;
725         m = mtxlock2mtx(c);
726
727         if (__predict_false(v == MTX_UNOWNED))
728                 v = MTX_READ_VALUE(m);
729
730         if (__predict_false(v == tid)) {
731                 m->mtx_recurse++;
732                 return;
733         }
734
735         if (SCHEDULER_STOPPED())
736                 return;
737
738         lock_delay_arg_init(&lda, &mtx_spin_delay);
739
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);
744
745 #ifdef HWPMC_HOOKS
746         PMC_SOFT_CALL( , , lock, failed);
747 #endif
748         lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
749 #ifdef LOCK_PROFILING
750         doing_lockprof = 1;
751 #elif defined(KDTRACE_HOOKS)
752         doing_lockprof = lockstat_enabled;
753         if (__predict_false(doing_lockprof))
754                 spin_time -= lockstat_nsecs(&m->lock_object);
755 #endif
756         for (;;) {
757                 if (v == MTX_UNOWNED) {
758                         if (_mtx_obtain_lock_fetch(m, &v, tid))
759                                 break;
760                         continue;
761                 }
762                 /* Give interrupts a chance while we spin. */
763                 spinlock_exit();
764                 do {
765                         if (lda.spin_cnt < 10000000) {
766                                 lock_delay(&lda);
767                         } else {
768                                 lda.spin_cnt++;
769                                 if (lda.spin_cnt < 60000000 || kdb_active ||
770                                     panicstr != NULL)
771                                         DELAY(1);
772                                 else
773                                         _mtx_lock_spin_failed(m);
774                                 cpu_spinwait();
775                         }
776                         v = MTX_READ_VALUE(m);
777                 } while (v != MTX_UNOWNED);
778                 spinlock_enter();
779         }
780
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),
784             "running");
785
786 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
787         if (__predict_true(!doing_lockprof))
788                 return;
789 #endif
790 #ifdef KDTRACE_HOOKS
791         spin_time += lockstat_nsecs(&m->lock_object);
792 #endif
793         LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
794             contested, waittime, file, line);
795 #ifdef KDTRACE_HOOKS
796         if (lda.spin_cnt != 0)
797                 LOCKSTAT_RECORD1(spin__spin, m, spin_time);
798 #endif
799 }
800 #endif /* SMP */
801
802 #ifdef INVARIANTS
803 static void
804 thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
805 {
806
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));
812         if (mtx_owned(m))
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);
818 }
819 #else
820 #define thread_lock_validate(m, opts, file, line) do { } while (0)
821 #endif
822
823 #ifndef LOCK_PROFILING
824 #if LOCK_DEBUG > 0
825 void
826 _thread_lock(struct thread *td, int opts, const char *file, int line)
827 #else
828 void
829 _thread_lock(struct thread *td)
830 #endif
831 {
832         struct mtx *m;
833         uintptr_t tid, v;
834
835         tid = (uintptr_t)curthread;
836
837         spinlock_enter();
838         m = td->td_lock;
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) {
845                 m->mtx_recurse++;
846         } else
847                 goto slowpath_unlocked;
848         if (__predict_true(m == td->td_lock)) {
849                 WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
850                 return;
851         }
852         if (m->mtx_recurse != 0)
853                 m->mtx_recurse--;
854         else
855                 _mtx_release_lock_quick(m);
856 slowpath_unlocked:
857         spinlock_exit();
858         thread_lock_flags_(td, 0, 0, 0);
859 }
860 #endif
861
862 void
863 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
864 {
865         struct mtx *m;
866         uintptr_t tid, v;
867         struct lock_delay_arg lda;
868 #ifdef LOCK_PROFILING
869         int contested = 0;
870         uint64_t waittime = 0;
871 #endif
872 #ifdef KDTRACE_HOOKS
873         int64_t spin_time = 0;
874 #endif
875 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
876         int doing_lockprof = 1;
877 #endif
878
879         tid = (uintptr_t)curthread;
880
881         if (SCHEDULER_STOPPED()) {
882                 /*
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.
886                  */
887                 spinlock_enter();
888                 return;
889         }
890
891         lock_delay_arg_init(&lda, &mtx_spin_delay);
892
893 #ifdef LOCK_PROFILING
894         doing_lockprof = 1;
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);
899 #endif
900         for (;;) {
901 retry:
902                 v = MTX_UNOWNED;
903                 spinlock_enter();
904                 m = td->td_lock;
905                 thread_lock_validate(m, opts, file, line);
906                 for (;;) {
907                         if (_mtx_obtain_lock_fetch(m, &v, tid))
908                                 break;
909                         if (v == MTX_UNOWNED)
910                                 continue;
911                         if (v == tid) {
912                                 m->mtx_recurse++;
913                                 break;
914                         }
915 #ifdef HWPMC_HOOKS
916                         PMC_SOFT_CALL( , , lock, failed);
917 #endif
918                         lock_profile_obtain_lock_failed(&m->lock_object,
919                             &contested, &waittime);
920                         /* Give interrupts a chance while we spin. */
921                         spinlock_exit();
922                         do {
923                                 if (lda.spin_cnt < 10000000) {
924                                         lock_delay(&lda);
925                                 } else {
926                                         lda.spin_cnt++;
927                                         if (lda.spin_cnt < 60000000 ||
928                                             kdb_active || panicstr != NULL)
929                                                 DELAY(1);
930                                         else
931                                                 _mtx_lock_spin_failed(m);
932                                         cpu_spinwait();
933                                 }
934                                 if (m != td->td_lock)
935                                         goto retry;
936                                 v = MTX_READ_VALUE(m);
937                         } while (v != MTX_UNOWNED);
938                         spinlock_enter();
939                 }
940                 if (m == td->td_lock)
941                         break;
942                 __mtx_unlock_spin(m);   /* does spinlock_exit() */
943         }
944         LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
945             line);
946         WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
947
948 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
949         if (__predict_true(!doing_lockprof))
950                 return;
951 #endif
952 #ifdef KDTRACE_HOOKS
953         spin_time += lockstat_nsecs(&m->lock_object);
954 #endif
955         if (m->mtx_recurse == 0)
956                 LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
957                     contested, waittime, file, line);
958 #ifdef KDTRACE_HOOKS
959         if (lda.spin_cnt != 0)
960                 LOCKSTAT_RECORD1(thread__spin, m, spin_time);
961 #endif
962 }
963
964 struct mtx *
965 thread_lock_block(struct thread *td)
966 {
967         struct mtx *lock;
968
969         THREAD_LOCK_ASSERT(td, MA_OWNED);
970         lock = td->td_lock;
971         td->td_lock = &blocked_lock;
972         mtx_unlock_spin(lock);
973
974         return (lock);
975 }
976
977 void
978 thread_lock_unblock(struct thread *td, struct mtx *new)
979 {
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);
983 }
984
985 void
986 thread_lock_set(struct thread *td, struct mtx *new)
987 {
988         struct mtx *lock;
989
990         mtx_assert(new, MA_OWNED);
991         THREAD_LOCK_ASSERT(td, MA_OWNED);
992         lock = td->td_lock;
993         td->td_lock = new;
994         mtx_unlock_spin(lock);
995 }
996
997 /*
998  * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
999  *
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.
1002  */
1003 #if LOCK_DEBUG > 0
1004 void
1005 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
1006     const char *file, int line)
1007 #else
1008 void
1009 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
1010 #endif
1011 {
1012         struct mtx *m;
1013         struct turnstile *ts;
1014         uintptr_t tid;
1015
1016         if (SCHEDULER_STOPPED())
1017                 return;
1018
1019         tid = (uintptr_t)curthread;
1020         m = mtxlock2mtx(c);
1021
1022         if (__predict_false(v == tid))
1023                 v = MTX_READ_VALUE(m);
1024
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);
1030                 return;
1031         }
1032
1033         LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
1034         if (v == tid && _mtx_release_lock(m, tid))
1035                 return;
1036
1037         /*
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.
1040          */
1041         turnstile_chain_lock(&m->lock_object);
1042         _mtx_release_lock_quick(m);
1043         ts = turnstile_lookup(&m->lock_object);
1044         MPASS(ts != NULL);
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);
1048
1049         /*
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.
1052          */
1053         turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
1054         turnstile_chain_unlock(&m->lock_object);
1055 }
1056
1057 /*
1058  * All the unlocking of MTX_SPIN locks is done inline.
1059  * See the __mtx_unlock_spin() macro for the details.
1060  */
1061
1062 /*
1063  * The backing function for the INVARIANTS-enabled mtx_assert()
1064  */
1065 #ifdef INVARIANT_SUPPORT
1066 void
1067 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
1068 {
1069         const struct mtx *m;
1070
1071         if (panicstr != NULL || dumping || SCHEDULER_STOPPED())
1072                 return;
1073
1074         m = mtxlock2mtx(c);
1075
1076         switch (what) {
1077         case MA_OWNED:
1078         case MA_OWNED | MA_RECURSED:
1079         case MA_OWNED | MA_NOTRECURSED:
1080                 if (!mtx_owned(m))
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);
1090                 }
1091                 break;
1092         case MA_NOTOWNED:
1093                 if (mtx_owned(m))
1094                         panic("mutex %s owned at %s:%d",
1095                             m->lock_object.lo_name, file, line);
1096                 break;
1097         default:
1098                 panic("unknown mtx_assert at %s:%d", file, line);
1099         }
1100 }
1101 #endif
1102
1103 /*
1104  * General init routine used by the MTX_SYSINIT() macro.
1105  */
1106 void
1107 mtx_sysinit(void *arg)
1108 {
1109         struct mtx_args *margs = arg;
1110
1111         mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
1112             margs->ma_opts);
1113 }
1114
1115 /*
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
1119  * witness.
1120  */
1121 void
1122 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
1123 {
1124         struct mtx *m;
1125         struct lock_class *class;
1126         int flags;
1127
1128         m = mtxlock2mtx(c);
1129
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,
1134             &m->mtx_lock));
1135
1136         /* Determine lock class and lock flags. */
1137         if (opts & MTX_SPIN)
1138                 class = &lock_class_mtx_spin;
1139         else
1140                 class = &lock_class_mtx_sleep;
1141         flags = 0;
1142         if (opts & MTX_QUIET)
1143                 flags |= LO_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)
1149                 flags |= LO_DUPOK;
1150         if (opts & MTX_NOPROFILE)
1151                 flags |= LO_NOPROFILE;
1152         if (opts & MTX_NEW)
1153                 flags |= LO_NEW;
1154
1155         /* Initialize mutex. */
1156         lock_init(&m->lock_object, class, name, type, flags);
1157
1158         m->mtx_lock = MTX_UNOWNED;
1159         m->mtx_recurse = 0;
1160 }
1161
1162 /*
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
1166  * flags.
1167  */
1168 void
1169 _mtx_destroy(volatile uintptr_t *c)
1170 {
1171         struct mtx *m;
1172
1173         m = mtxlock2mtx(c);
1174
1175         if (!mtx_owned(m))
1176                 MPASS(mtx_unowned(m));
1177         else {
1178                 MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
1179
1180                 /* Perform the non-mtx related part of mtx_unlock_spin(). */
1181                 if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1182                         spinlock_exit();
1183                 else
1184                         TD_LOCKS_DEC(curthread);
1185
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__,
1189                     __LINE__);
1190         }
1191
1192         m->mtx_lock = MTX_DESTROYED;
1193         lock_destroy(&m->lock_object);
1194 }
1195
1196 /*
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.
1200  */
1201 void
1202 mutex_init(void)
1203 {
1204
1205         /* Setup turnstiles so that sleep mutexes work. */
1206         init_turnstiles();
1207
1208         /*
1209          * Initialize mutexes.
1210          */
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);
1220         mtx_lock(&Giant);
1221 }
1222
1223 #ifdef DDB
1224 void
1225 db_show_mtx(const struct lock_object *lock)
1226 {
1227         struct thread *td;
1228         const struct mtx *m;
1229
1230         m = (const struct mtx *)lock;
1231
1232         db_printf(" flags: {");
1233         if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1234                 db_printf("SPIN");
1235         else
1236                 db_printf("DEF");
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");
1241         db_printf("}\n");
1242         db_printf(" state: {");
1243         if (mtx_unowned(m))
1244                 db_printf("UNOWNED");
1245         else if (mtx_destroyed(m))
1246                 db_printf("DESTROYED");
1247         else {
1248                 db_printf("OWNED");
1249                 if (m->mtx_lock & MTX_CONTESTED)
1250                         db_printf(", CONTESTED");
1251                 if (m->mtx_lock & MTX_RECURSED)
1252                         db_printf(", RECURSED");
1253         }
1254         db_printf("}\n");
1255         if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1256                 td = mtx_owner(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);
1261         }
1262 }
1263 #endif