xref: /freebsd/sys/kern/kern_mutex.c (revision 718cf2ccb9956613756ab15d7a0e28f2c8e91cab)
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 	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
838 		goto slowpath_noirq;
839 	spinlock_enter();
840 	m = td->td_lock;
841 	thread_lock_validate(m, 0, file, line);
842 	v = MTX_READ_VALUE(m);
843 	if (__predict_true(v == MTX_UNOWNED)) {
844 		if (__predict_false(!_mtx_obtain_lock(m, tid)))
845 			goto slowpath_unlocked;
846 	} else if (v == tid) {
847 		m->mtx_recurse++;
848 	} else
849 		goto slowpath_unlocked;
850 	if (__predict_true(m == td->td_lock)) {
851 		WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
852 		return;
853 	}
854 	if (m->mtx_recurse != 0)
855 		m->mtx_recurse--;
856 	else
857 		_mtx_release_lock_quick(m);
858 slowpath_unlocked:
859 	spinlock_exit();
860 slowpath_noirq:
861 #if LOCK_DEBUG > 0
862 	thread_lock_flags_(td, opts, file, line);
863 #else
864 	thread_lock_flags_(td, 0, 0, 0);
865 #endif
866 }
867 #endif
868 
869 void
870 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
871 {
872 	struct mtx *m;
873 	uintptr_t tid, v;
874 	struct lock_delay_arg lda;
875 #ifdef LOCK_PROFILING
876 	int contested = 0;
877 	uint64_t waittime = 0;
878 #endif
879 #ifdef KDTRACE_HOOKS
880 	int64_t spin_time = 0;
881 #endif
882 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
883 	int doing_lockprof = 1;
884 #endif
885 
886 	tid = (uintptr_t)curthread;
887 
888 	if (SCHEDULER_STOPPED()) {
889 		/*
890 		 * Ensure that spinlock sections are balanced even when the
891 		 * scheduler is stopped, since we may otherwise inadvertently
892 		 * re-enable interrupts while dumping core.
893 		 */
894 		spinlock_enter();
895 		return;
896 	}
897 
898 	lock_delay_arg_init(&lda, &mtx_spin_delay);
899 
900 #ifdef LOCK_PROFILING
901 	doing_lockprof = 1;
902 #elif defined(KDTRACE_HOOKS)
903 	doing_lockprof = lockstat_enabled;
904 	if (__predict_false(doing_lockprof))
905 		spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
906 #endif
907 	for (;;) {
908 retry:
909 		v = MTX_UNOWNED;
910 		spinlock_enter();
911 		m = td->td_lock;
912 		thread_lock_validate(m, opts, file, line);
913 		for (;;) {
914 			if (_mtx_obtain_lock_fetch(m, &v, tid))
915 				break;
916 			if (v == MTX_UNOWNED)
917 				continue;
918 			if (v == tid) {
919 				m->mtx_recurse++;
920 				break;
921 			}
922 #ifdef HWPMC_HOOKS
923 			PMC_SOFT_CALL( , , lock, failed);
924 #endif
925 			lock_profile_obtain_lock_failed(&m->lock_object,
926 			    &contested, &waittime);
927 			/* Give interrupts a chance while we spin. */
928 			spinlock_exit();
929 			do {
930 				if (lda.spin_cnt < 10000000) {
931 					lock_delay(&lda);
932 				} else {
933 					lda.spin_cnt++;
934 					if (lda.spin_cnt < 60000000 ||
935 					    kdb_active || panicstr != NULL)
936 						DELAY(1);
937 					else
938 						_mtx_lock_spin_failed(m);
939 					cpu_spinwait();
940 				}
941 				if (m != td->td_lock)
942 					goto retry;
943 				v = MTX_READ_VALUE(m);
944 			} while (v != MTX_UNOWNED);
945 			spinlock_enter();
946 		}
947 		if (m == td->td_lock)
948 			break;
949 		__mtx_unlock_spin(m);	/* does spinlock_exit() */
950 	}
951 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
952 	    line);
953 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
954 
955 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
956 	if (__predict_true(!doing_lockprof))
957 		return;
958 #endif
959 #ifdef KDTRACE_HOOKS
960 	spin_time += lockstat_nsecs(&m->lock_object);
961 #endif
962 	if (m->mtx_recurse == 0)
963 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
964 		    contested, waittime, file, line);
965 #ifdef KDTRACE_HOOKS
966 	if (lda.spin_cnt != 0)
967 		LOCKSTAT_RECORD1(thread__spin, m, spin_time);
968 #endif
969 }
970 
971 struct mtx *
972 thread_lock_block(struct thread *td)
973 {
974 	struct mtx *lock;
975 
976 	THREAD_LOCK_ASSERT(td, MA_OWNED);
977 	lock = td->td_lock;
978 	td->td_lock = &blocked_lock;
979 	mtx_unlock_spin(lock);
980 
981 	return (lock);
982 }
983 
984 void
985 thread_lock_unblock(struct thread *td, struct mtx *new)
986 {
987 	mtx_assert(new, MA_OWNED);
988 	MPASS(td->td_lock == &blocked_lock);
989 	atomic_store_rel_ptr((volatile void *)&td->td_lock, (uintptr_t)new);
990 }
991 
992 void
993 thread_lock_set(struct thread *td, struct mtx *new)
994 {
995 	struct mtx *lock;
996 
997 	mtx_assert(new, MA_OWNED);
998 	THREAD_LOCK_ASSERT(td, MA_OWNED);
999 	lock = td->td_lock;
1000 	td->td_lock = new;
1001 	mtx_unlock_spin(lock);
1002 }
1003 
1004 /*
1005  * __mtx_unlock_sleep: the tougher part of releasing an MTX_DEF lock.
1006  *
1007  * We are only called here if the lock is recursed, contested (i.e. we
1008  * need to wake up a blocked thread) or lockstat probe is active.
1009  */
1010 #if LOCK_DEBUG > 0
1011 void
1012 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v, int opts,
1013     const char *file, int line)
1014 #else
1015 void
1016 __mtx_unlock_sleep(volatile uintptr_t *c, uintptr_t v)
1017 #endif
1018 {
1019 	struct mtx *m;
1020 	struct turnstile *ts;
1021 	uintptr_t tid;
1022 
1023 	if (SCHEDULER_STOPPED())
1024 		return;
1025 
1026 	tid = (uintptr_t)curthread;
1027 	m = mtxlock2mtx(c);
1028 
1029 	if (__predict_false(v == tid))
1030 		v = MTX_READ_VALUE(m);
1031 
1032 	if (__predict_false(v & MTX_RECURSED)) {
1033 		if (--(m->mtx_recurse) == 0)
1034 			atomic_clear_ptr(&m->mtx_lock, MTX_RECURSED);
1035 		if (LOCK_LOG_TEST(&m->lock_object, opts))
1036 			CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p unrecurse", m);
1037 		return;
1038 	}
1039 
1040 	LOCKSTAT_PROFILE_RELEASE_LOCK(adaptive__release, m);
1041 	if (v == tid && _mtx_release_lock(m, tid))
1042 		return;
1043 
1044 	/*
1045 	 * We have to lock the chain before the turnstile so this turnstile
1046 	 * can be removed from the hash list if it is empty.
1047 	 */
1048 	turnstile_chain_lock(&m->lock_object);
1049 	_mtx_release_lock_quick(m);
1050 	ts = turnstile_lookup(&m->lock_object);
1051 	MPASS(ts != NULL);
1052 	if (LOCK_LOG_TEST(&m->lock_object, opts))
1053 		CTR1(KTR_LOCK, "_mtx_unlock_sleep: %p contested", m);
1054 	turnstile_broadcast(ts, TS_EXCLUSIVE_QUEUE);
1055 
1056 	/*
1057 	 * This turnstile is now no longer associated with the mutex.  We can
1058 	 * unlock the chain lock so a new turnstile may take it's place.
1059 	 */
1060 	turnstile_unpend(ts, TS_EXCLUSIVE_LOCK);
1061 	turnstile_chain_unlock(&m->lock_object);
1062 }
1063 
1064 /*
1065  * All the unlocking of MTX_SPIN locks is done inline.
1066  * See the __mtx_unlock_spin() macro for the details.
1067  */
1068 
1069 /*
1070  * The backing function for the INVARIANTS-enabled mtx_assert()
1071  */
1072 #ifdef INVARIANT_SUPPORT
1073 void
1074 __mtx_assert(const volatile uintptr_t *c, int what, const char *file, int line)
1075 {
1076 	const struct mtx *m;
1077 
1078 	if (panicstr != NULL || dumping || SCHEDULER_STOPPED())
1079 		return;
1080 
1081 	m = mtxlock2mtx(c);
1082 
1083 	switch (what) {
1084 	case MA_OWNED:
1085 	case MA_OWNED | MA_RECURSED:
1086 	case MA_OWNED | MA_NOTRECURSED:
1087 		if (!mtx_owned(m))
1088 			panic("mutex %s not owned at %s:%d",
1089 			    m->lock_object.lo_name, file, line);
1090 		if (mtx_recursed(m)) {
1091 			if ((what & MA_NOTRECURSED) != 0)
1092 				panic("mutex %s recursed at %s:%d",
1093 				    m->lock_object.lo_name, file, line);
1094 		} else if ((what & MA_RECURSED) != 0) {
1095 			panic("mutex %s unrecursed at %s:%d",
1096 			    m->lock_object.lo_name, file, line);
1097 		}
1098 		break;
1099 	case MA_NOTOWNED:
1100 		if (mtx_owned(m))
1101 			panic("mutex %s owned at %s:%d",
1102 			    m->lock_object.lo_name, file, line);
1103 		break;
1104 	default:
1105 		panic("unknown mtx_assert at %s:%d", file, line);
1106 	}
1107 }
1108 #endif
1109 
1110 /*
1111  * General init routine used by the MTX_SYSINIT() macro.
1112  */
1113 void
1114 mtx_sysinit(void *arg)
1115 {
1116 	struct mtx_args *margs = arg;
1117 
1118 	mtx_init((struct mtx *)margs->ma_mtx, margs->ma_desc, NULL,
1119 	    margs->ma_opts);
1120 }
1121 
1122 /*
1123  * Mutex initialization routine; initialize lock `m' of type contained in
1124  * `opts' with options contained in `opts' and name `name.'  The optional
1125  * lock type `type' is used as a general lock category name for use with
1126  * witness.
1127  */
1128 void
1129 _mtx_init(volatile uintptr_t *c, const char *name, const char *type, int opts)
1130 {
1131 	struct mtx *m;
1132 	struct lock_class *class;
1133 	int flags;
1134 
1135 	m = mtxlock2mtx(c);
1136 
1137 	MPASS((opts & ~(MTX_SPIN | MTX_QUIET | MTX_RECURSE |
1138 	    MTX_NOWITNESS | MTX_DUPOK | MTX_NOPROFILE | MTX_NEW)) == 0);
1139 	ASSERT_ATOMIC_LOAD_PTR(m->mtx_lock,
1140 	    ("%s: mtx_lock not aligned for %s: %p", __func__, name,
1141 	    &m->mtx_lock));
1142 
1143 	/* Determine lock class and lock flags. */
1144 	if (opts & MTX_SPIN)
1145 		class = &lock_class_mtx_spin;
1146 	else
1147 		class = &lock_class_mtx_sleep;
1148 	flags = 0;
1149 	if (opts & MTX_QUIET)
1150 		flags |= LO_QUIET;
1151 	if (opts & MTX_RECURSE)
1152 		flags |= LO_RECURSABLE;
1153 	if ((opts & MTX_NOWITNESS) == 0)
1154 		flags |= LO_WITNESS;
1155 	if (opts & MTX_DUPOK)
1156 		flags |= LO_DUPOK;
1157 	if (opts & MTX_NOPROFILE)
1158 		flags |= LO_NOPROFILE;
1159 	if (opts & MTX_NEW)
1160 		flags |= LO_NEW;
1161 
1162 	/* Initialize mutex. */
1163 	lock_init(&m->lock_object, class, name, type, flags);
1164 
1165 	m->mtx_lock = MTX_UNOWNED;
1166 	m->mtx_recurse = 0;
1167 }
1168 
1169 /*
1170  * Remove lock `m' from all_mtx queue.  We don't allow MTX_QUIET to be
1171  * passed in as a flag here because if the corresponding mtx_init() was
1172  * called with MTX_QUIET set, then it will already be set in the mutex's
1173  * flags.
1174  */
1175 void
1176 _mtx_destroy(volatile uintptr_t *c)
1177 {
1178 	struct mtx *m;
1179 
1180 	m = mtxlock2mtx(c);
1181 
1182 	if (!mtx_owned(m))
1183 		MPASS(mtx_unowned(m));
1184 	else {
1185 		MPASS((m->mtx_lock & (MTX_RECURSED|MTX_CONTESTED)) == 0);
1186 
1187 		/* Perform the non-mtx related part of mtx_unlock_spin(). */
1188 		if (LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin)
1189 			spinlock_exit();
1190 		else
1191 			TD_LOCKS_DEC(curthread);
1192 
1193 		lock_profile_release_lock(&m->lock_object);
1194 		/* Tell witness this isn't locked to make it happy. */
1195 		WITNESS_UNLOCK(&m->lock_object, LOP_EXCLUSIVE, __FILE__,
1196 		    __LINE__);
1197 	}
1198 
1199 	m->mtx_lock = MTX_DESTROYED;
1200 	lock_destroy(&m->lock_object);
1201 }
1202 
1203 /*
1204  * Intialize the mutex code and system mutexes.  This is called from the MD
1205  * startup code prior to mi_startup().  The per-CPU data space needs to be
1206  * setup before this is called.
1207  */
1208 void
1209 mutex_init(void)
1210 {
1211 
1212 	/* Setup turnstiles so that sleep mutexes work. */
1213 	init_turnstiles();
1214 
1215 	/*
1216 	 * Initialize mutexes.
1217 	 */
1218 	mtx_init(&Giant, "Giant", NULL, MTX_DEF | MTX_RECURSE);
1219 	mtx_init(&blocked_lock, "blocked lock", NULL, MTX_SPIN);
1220 	blocked_lock.mtx_lock = 0xdeadc0de;	/* Always blocked. */
1221 	mtx_init(&proc0.p_mtx, "process lock", NULL, MTX_DEF | MTX_DUPOK);
1222 	mtx_init(&proc0.p_slock, "process slock", NULL, MTX_SPIN);
1223 	mtx_init(&proc0.p_statmtx, "pstatl", NULL, MTX_SPIN);
1224 	mtx_init(&proc0.p_itimmtx, "pitiml", NULL, MTX_SPIN);
1225 	mtx_init(&proc0.p_profmtx, "pprofl", NULL, MTX_SPIN);
1226 	mtx_init(&devmtx, "cdev", NULL, MTX_DEF);
1227 	mtx_lock(&Giant);
1228 }
1229 
1230 #ifdef DDB
1231 void
1232 db_show_mtx(const struct lock_object *lock)
1233 {
1234 	struct thread *td;
1235 	const struct mtx *m;
1236 
1237 	m = (const struct mtx *)lock;
1238 
1239 	db_printf(" flags: {");
1240 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1241 		db_printf("SPIN");
1242 	else
1243 		db_printf("DEF");
1244 	if (m->lock_object.lo_flags & LO_RECURSABLE)
1245 		db_printf(", RECURSE");
1246 	if (m->lock_object.lo_flags & LO_DUPOK)
1247 		db_printf(", DUPOK");
1248 	db_printf("}\n");
1249 	db_printf(" state: {");
1250 	if (mtx_unowned(m))
1251 		db_printf("UNOWNED");
1252 	else if (mtx_destroyed(m))
1253 		db_printf("DESTROYED");
1254 	else {
1255 		db_printf("OWNED");
1256 		if (m->mtx_lock & MTX_CONTESTED)
1257 			db_printf(", CONTESTED");
1258 		if (m->mtx_lock & MTX_RECURSED)
1259 			db_printf(", RECURSED");
1260 	}
1261 	db_printf("}\n");
1262 	if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1263 		td = mtx_owner(m);
1264 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1265 		    td->td_tid, td->td_proc->p_pid, td->td_name);
1266 		if (mtx_recursed(m))
1267 			db_printf(" recursed: %d\n", m->mtx_recurse);
1268 	}
1269 }
1270 #endif
1271