xref: /freebsd/sys/kern/kern_mutex.c (revision 74fe6c29fb7eef3418d7919dcd41dc1a04a982a1)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Berkeley Software Design Inc's name may not be used to endorse or
15  *    promote products derived from this software without specific prior
16  *    written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
31  *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
32  */
33 
34 /*
35  * Machine independent bits of mutex implementation.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include "opt_adaptive_mutexes.h"
42 #include "opt_ddb.h"
43 #include "opt_hwpmc_hooks.h"
44 #include "opt_sched.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/bus.h>
49 #include <sys/conf.h>
50 #include <sys/kdb.h>
51 #include <sys/kernel.h>
52 #include <sys/ktr.h>
53 #include <sys/lock.h>
54 #include <sys/malloc.h>
55 #include <sys/mutex.h>
56 #include <sys/proc.h>
57 #include <sys/resourcevar.h>
58 #include <sys/sched.h>
59 #include <sys/sbuf.h>
60 #include <sys/smp.h>
61 #include <sys/sysctl.h>
62 #include <sys/turnstile.h>
63 #include <sys/vmmeter.h>
64 #include <sys/lock_profile.h>
65 
66 #include <machine/atomic.h>
67 #include <machine/bus.h>
68 #include <machine/cpu.h>
69 
70 #include <ddb/ddb.h>
71 
72 #include <fs/devfs/devfs_int.h>
73 
74 #include <vm/vm.h>
75 #include <vm/vm_extern.h>
76 
77 #if defined(SMP) && !defined(NO_ADAPTIVE_MUTEXES)
78 #define	ADAPTIVE_MUTEXES
79 #endif
80 
81 #ifdef HWPMC_HOOKS
82 #include <sys/pmckern.h>
83 PMC_SOFT_DEFINE( , , lock, failed);
84 #endif
85 
86 /*
87  * Return the mutex address when the lock cookie address is provided.
88  * This functionality assumes that struct mtx* have a member named mtx_lock.
89  */
90 #define	mtxlock2mtx(c)	(__containerof(c, struct mtx, mtx_lock))
91 
92 /*
93  * Internal utility macros.
94  */
95 #define mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
96 
97 #define	mtx_destroyed(m) ((m)->mtx_lock == MTX_DESTROYED)
98 
99 static void	assert_mtx(const struct lock_object *lock, int what);
100 #ifdef DDB
101 static void	db_show_mtx(const struct lock_object *lock);
102 #endif
103 static void	lock_mtx(struct lock_object *lock, uintptr_t how);
104 static void	lock_spin(struct lock_object *lock, uintptr_t how);
105 #ifdef KDTRACE_HOOKS
106 static int	owner_mtx(const struct lock_object *lock,
107 		    struct thread **owner);
108 #endif
109 static uintptr_t unlock_mtx(struct lock_object *lock);
110 static uintptr_t unlock_spin(struct lock_object *lock);
111 
112 /*
113  * Lock classes for sleep and spin mutexes.
114  */
115 struct lock_class lock_class_mtx_sleep = {
116 	.lc_name = "sleep mutex",
117 	.lc_flags = LC_SLEEPLOCK | LC_RECURSABLE,
118 	.lc_assert = assert_mtx,
119 #ifdef DDB
120 	.lc_ddb_show = db_show_mtx,
121 #endif
122 	.lc_lock = lock_mtx,
123 	.lc_unlock = unlock_mtx,
124 #ifdef KDTRACE_HOOKS
125 	.lc_owner = owner_mtx,
126 #endif
127 };
128 struct lock_class lock_class_mtx_spin = {
129 	.lc_name = "spin mutex",
130 	.lc_flags = LC_SPINLOCK | LC_RECURSABLE,
131 	.lc_assert = assert_mtx,
132 #ifdef DDB
133 	.lc_ddb_show = db_show_mtx,
134 #endif
135 	.lc_lock = lock_spin,
136 	.lc_unlock = unlock_spin,
137 #ifdef KDTRACE_HOOKS
138 	.lc_owner = owner_mtx,
139 #endif
140 };
141 
142 #ifdef ADAPTIVE_MUTEXES
143 static SYSCTL_NODE(_debug, OID_AUTO, mtx, CTLFLAG_RD, NULL, "mtx debugging");
144 
145 static struct lock_delay_config __read_frequently mtx_delay;
146 
147 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_base, CTLFLAG_RW, &mtx_delay.base,
148     0, "");
149 SYSCTL_INT(_debug_mtx, OID_AUTO, delay_max, CTLFLAG_RW, &mtx_delay.max,
150     0, "");
151 
152 LOCK_DELAY_SYSINIT_DEFAULT(mtx_delay);
153 #endif
154 
155 static SYSCTL_NODE(_debug, OID_AUTO, mtx_spin, CTLFLAG_RD, NULL,
156     "mtx spin debugging");
157 
158 static struct lock_delay_config __read_frequently mtx_spin_delay;
159 
160 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_base, CTLFLAG_RW,
161     &mtx_spin_delay.base, 0, "");
162 SYSCTL_INT(_debug_mtx_spin, OID_AUTO, delay_max, CTLFLAG_RW,
163     &mtx_spin_delay.max, 0, "");
164 
165 LOCK_DELAY_SYSINIT_DEFAULT(mtx_spin_delay);
166 
167 /*
168  * System-wide mutexes
169  */
170 struct mtx blocked_lock;
171 struct mtx __exclusive_cache_line Giant;
172 
173 static void _mtx_lock_indefinite_check(struct mtx *, struct lock_delay_arg *);
174 
175 void
176 assert_mtx(const struct lock_object *lock, int what)
177 {
178 
179 	mtx_assert((const struct mtx *)lock, what);
180 }
181 
182 void
183 lock_mtx(struct lock_object *lock, uintptr_t how)
184 {
185 
186 	mtx_lock((struct mtx *)lock);
187 }
188 
189 void
190 lock_spin(struct lock_object *lock, uintptr_t how)
191 {
192 
193 	panic("spin locks can only use msleep_spin");
194 }
195 
196 uintptr_t
197 unlock_mtx(struct lock_object *lock)
198 {
199 	struct mtx *m;
200 
201 	m = (struct mtx *)lock;
202 	mtx_assert(m, MA_OWNED | MA_NOTRECURSED);
203 	mtx_unlock(m);
204 	return (0);
205 }
206 
207 uintptr_t
208 unlock_spin(struct lock_object *lock)
209 {
210 
211 	panic("spin locks can only use msleep_spin");
212 }
213 
214 #ifdef KDTRACE_HOOKS
215 int
216 owner_mtx(const struct lock_object *lock, struct thread **owner)
217 {
218 	const struct mtx *m;
219 	uintptr_t x;
220 
221 	m = (const struct mtx *)lock;
222 	x = m->mtx_lock;
223 	*owner = (struct thread *)(x & ~MTX_FLAGMASK);
224 	return (*owner != NULL);
225 }
226 #endif
227 
228 /*
229  * Function versions of the inlined __mtx_* macros.  These are used by
230  * modules and can also be called from assembly language if needed.
231  */
232 void
233 __mtx_lock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
234 {
235 	struct mtx *m;
236 	uintptr_t tid, v;
237 
238 	m = mtxlock2mtx(c);
239 
240 	KASSERT(kdb_active != 0 || SCHEDULER_STOPPED() ||
241 	    !TD_IS_IDLETHREAD(curthread),
242 	    ("mtx_lock() by idle thread %p on sleep mutex %s @ %s:%d",
243 	    curthread, m->lock_object.lo_name, file, line));
244 	KASSERT(m->mtx_lock != MTX_DESTROYED,
245 	    ("mtx_lock() of destroyed mutex @ %s:%d", file, line));
246 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
247 	    ("mtx_lock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
248 	    file, line));
249 	WITNESS_CHECKORDER(&m->lock_object, (opts & ~MTX_RECURSE) |
250 	    LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
251 
252 	tid = (uintptr_t)curthread;
253 	v = MTX_UNOWNED;
254 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
255 		_mtx_lock_sleep(m, v, opts, file, line);
256 	else
257 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
258 		    m, 0, 0, file, line);
259 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
260 	    line);
261 	WITNESS_LOCK(&m->lock_object, (opts & ~MTX_RECURSE) | LOP_EXCLUSIVE,
262 	    file, line);
263 	TD_LOCKS_INC(curthread);
264 }
265 
266 void
267 __mtx_unlock_flags(volatile uintptr_t *c, int opts, const char *file, int line)
268 {
269 	struct mtx *m;
270 
271 	m = mtxlock2mtx(c);
272 
273 	KASSERT(m->mtx_lock != MTX_DESTROYED,
274 	    ("mtx_unlock() of destroyed mutex @ %s:%d", file, line));
275 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
276 	    ("mtx_unlock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
277 	    file, line));
278 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
279 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
280 	    line);
281 	mtx_assert(m, MA_OWNED);
282 
283 #ifdef LOCK_PROFILING
284 	__mtx_unlock_sleep(c, (uintptr_t)curthread, opts, file, line);
285 #else
286 	__mtx_unlock(m, curthread, opts, file, line);
287 #endif
288 	TD_LOCKS_DEC(curthread);
289 }
290 
291 void
292 __mtx_lock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
293     int line)
294 {
295 	struct mtx *m;
296 #ifdef SMP
297 	uintptr_t tid, v;
298 #endif
299 
300 	m = mtxlock2mtx(c);
301 
302 	KASSERT(m->mtx_lock != MTX_DESTROYED,
303 	    ("mtx_lock_spin() of destroyed mutex @ %s:%d", file, line));
304 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
305 	    ("mtx_lock_spin() of sleep mutex %s @ %s:%d",
306 	    m->lock_object.lo_name, file, line));
307 	if (mtx_owned(m))
308 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
309 		    (opts & MTX_RECURSE) != 0,
310 	    ("mtx_lock_spin: recursed on non-recursive mutex %s @ %s:%d\n",
311 		    m->lock_object.lo_name, file, line));
312 	opts &= ~MTX_RECURSE;
313 	WITNESS_CHECKORDER(&m->lock_object, opts | LOP_NEWORDER | LOP_EXCLUSIVE,
314 	    file, line, NULL);
315 #ifdef SMP
316 	spinlock_enter();
317 	tid = (uintptr_t)curthread;
318 	v = MTX_UNOWNED;
319 	if (!_mtx_obtain_lock_fetch(m, &v, tid))
320 		_mtx_lock_spin(m, v, opts, file, line);
321 	else
322 		LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire,
323 		    m, 0, 0, file, line);
324 #else
325 	__mtx_lock_spin(m, curthread, opts, file, line);
326 #endif
327 	LOCK_LOG_LOCK("LOCK", &m->lock_object, opts, m->mtx_recurse, file,
328 	    line);
329 	WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
330 }
331 
332 int
333 __mtx_trylock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
334     int line)
335 {
336 	struct mtx *m;
337 
338 	if (SCHEDULER_STOPPED())
339 		return (1);
340 
341 	m = mtxlock2mtx(c);
342 
343 	KASSERT(m->mtx_lock != MTX_DESTROYED,
344 	    ("mtx_trylock_spin() of destroyed mutex @ %s:%d", file, line));
345 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
346 	    ("mtx_trylock_spin() of sleep mutex %s @ %s:%d",
347 	    m->lock_object.lo_name, file, line));
348 	KASSERT((opts & MTX_RECURSE) == 0,
349 	    ("mtx_trylock_spin: unsupp. opt MTX_RECURSE on mutex %s @ %s:%d\n",
350 	    m->lock_object.lo_name, file, line));
351 	if (__mtx_trylock_spin(m, curthread, opts, file, line)) {
352 		LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 1, file, line);
353 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
354 		return (1);
355 	}
356 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, 0, file, line);
357 	return (0);
358 }
359 
360 void
361 __mtx_unlock_spin_flags(volatile uintptr_t *c, int opts, const char *file,
362     int line)
363 {
364 	struct mtx *m;
365 
366 	m = mtxlock2mtx(c);
367 
368 	KASSERT(m->mtx_lock != MTX_DESTROYED,
369 	    ("mtx_unlock_spin() of destroyed mutex @ %s:%d", file, line));
370 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
371 	    ("mtx_unlock_spin() of sleep mutex %s @ %s:%d",
372 	    m->lock_object.lo_name, file, line));
373 	WITNESS_UNLOCK(&m->lock_object, opts | LOP_EXCLUSIVE, file, line);
374 	LOCK_LOG_LOCK("UNLOCK", &m->lock_object, opts, m->mtx_recurse, file,
375 	    line);
376 	mtx_assert(m, MA_OWNED);
377 
378 	__mtx_unlock_spin(m);
379 }
380 
381 /*
382  * The important part of mtx_trylock{,_flags}()
383  * Tries to acquire lock `m.'  If this function is called on a mutex that
384  * is already owned, it will recursively acquire the lock.
385  */
386 int
387 _mtx_trylock_flags_int(struct mtx *m, int opts LOCK_FILE_LINE_ARG_DEF)
388 {
389 	struct thread *td;
390 	uintptr_t tid, v;
391 #ifdef LOCK_PROFILING
392 	uint64_t waittime = 0;
393 	int contested = 0;
394 #endif
395 	int rval;
396 	bool recursed;
397 
398 	td = curthread;
399 	tid = (uintptr_t)td;
400 	if (SCHEDULER_STOPPED_TD(td))
401 		return (1);
402 
403 	KASSERT(kdb_active != 0 || !TD_IS_IDLETHREAD(td),
404 	    ("mtx_trylock() by idle thread %p on sleep mutex %s @ %s:%d",
405 	    curthread, m->lock_object.lo_name, file, line));
406 	KASSERT(m->mtx_lock != MTX_DESTROYED,
407 	    ("mtx_trylock() of destroyed mutex @ %s:%d", file, line));
408 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_sleep,
409 	    ("mtx_trylock() of spin mutex %s @ %s:%d", m->lock_object.lo_name,
410 	    file, line));
411 
412 	rval = 1;
413 	recursed = false;
414 	v = MTX_UNOWNED;
415 	for (;;) {
416 		if (_mtx_obtain_lock_fetch(m, &v, tid))
417 			break;
418 		if (v == MTX_UNOWNED)
419 			continue;
420 		if (v == tid &&
421 		    ((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
422 		    (opts & MTX_RECURSE) != 0)) {
423 			m->mtx_recurse++;
424 			atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
425 			recursed = true;
426 			break;
427 		}
428 		rval = 0;
429 		break;
430 	}
431 
432 	opts &= ~MTX_RECURSE;
433 
434 	LOCK_LOG_TRY("LOCK", &m->lock_object, opts, rval, file, line);
435 	if (rval) {
436 		WITNESS_LOCK(&m->lock_object, opts | LOP_EXCLUSIVE | LOP_TRYLOCK,
437 		    file, line);
438 		TD_LOCKS_INC(curthread);
439 		if (!recursed)
440 			LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire,
441 			    m, contested, waittime, file, line);
442 	}
443 
444 	return (rval);
445 }
446 
447 int
448 _mtx_trylock_flags_(volatile uintptr_t *c, int opts, const char *file, int line)
449 {
450 	struct mtx *m;
451 
452 	m = mtxlock2mtx(c);
453 	return (_mtx_trylock_flags_int(m, opts LOCK_FILE_LINE_ARG));
454 }
455 
456 /*
457  * __mtx_lock_sleep: the tougher part of acquiring an MTX_DEF lock.
458  *
459  * We call this if the lock is either contested (i.e. we need to go to
460  * sleep waiting for it), or if we need to recurse on it.
461  */
462 #if LOCK_DEBUG > 0
463 void
464 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v, int opts, const char *file,
465     int line)
466 #else
467 void
468 __mtx_lock_sleep(volatile uintptr_t *c, uintptr_t v)
469 #endif
470 {
471 	struct thread *td;
472 	struct mtx *m;
473 	struct turnstile *ts;
474 	uintptr_t tid;
475 	struct thread *owner;
476 #ifdef KTR
477 	int cont_logged = 0;
478 #endif
479 #ifdef LOCK_PROFILING
480 	int contested = 0;
481 	uint64_t waittime = 0;
482 #endif
483 #if defined(ADAPTIVE_MUTEXES) || defined(KDTRACE_HOOKS)
484 	struct lock_delay_arg lda;
485 #endif
486 #ifdef KDTRACE_HOOKS
487 	u_int sleep_cnt = 0;
488 	int64_t sleep_time = 0;
489 	int64_t all_time = 0;
490 #endif
491 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
492 	int doing_lockprof;
493 #endif
494 
495 	td = curthread;
496 	tid = (uintptr_t)td;
497 	m = mtxlock2mtx(c);
498 
499 #ifdef KDTRACE_HOOKS
500 	if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
501 		while (v == MTX_UNOWNED) {
502 			if (_mtx_obtain_lock_fetch(m, &v, tid))
503 				goto out_lockstat;
504 		}
505 		doing_lockprof = 1;
506 		all_time -= lockstat_nsecs(&m->lock_object);
507 	}
508 #endif
509 #ifdef LOCK_PROFILING
510 	doing_lockprof = 1;
511 #endif
512 
513 	if (SCHEDULER_STOPPED_TD(td))
514 		return;
515 
516 #if defined(ADAPTIVE_MUTEXES)
517 	lock_delay_arg_init(&lda, &mtx_delay);
518 #elif defined(KDTRACE_HOOKS)
519 	lock_delay_arg_init(&lda, NULL);
520 #endif
521 
522 	if (__predict_false(v == MTX_UNOWNED))
523 		v = MTX_READ_VALUE(m);
524 
525 	if (__predict_false(lv_mtx_owner(v) == td)) {
526 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0 ||
527 		    (opts & MTX_RECURSE) != 0,
528 	    ("_mtx_lock_sleep: recursed on non-recursive mutex %s @ %s:%d\n",
529 		    m->lock_object.lo_name, file, line));
530 #if LOCK_DEBUG > 0
531 		opts &= ~MTX_RECURSE;
532 #endif
533 		m->mtx_recurse++;
534 		atomic_set_ptr(&m->mtx_lock, MTX_RECURSED);
535 		if (LOCK_LOG_TEST(&m->lock_object, opts))
536 			CTR1(KTR_LOCK, "_mtx_lock_sleep: %p recursing", m);
537 		return;
538 	}
539 #if LOCK_DEBUG > 0
540 	opts &= ~MTX_RECURSE;
541 #endif
542 
543 #ifdef HWPMC_HOOKS
544 	PMC_SOFT_CALL( , , lock, failed);
545 #endif
546 	lock_profile_obtain_lock_failed(&m->lock_object,
547 		    &contested, &waittime);
548 	if (LOCK_LOG_TEST(&m->lock_object, opts))
549 		CTR4(KTR_LOCK,
550 		    "_mtx_lock_sleep: %s contested (lock=%p) at %s:%d",
551 		    m->lock_object.lo_name, (void *)m->mtx_lock, file, line);
552 
553 	for (;;) {
554 		if (v == MTX_UNOWNED) {
555 			if (_mtx_obtain_lock_fetch(m, &v, tid))
556 				break;
557 			continue;
558 		}
559 #ifdef KDTRACE_HOOKS
560 		lda.spin_cnt++;
561 #endif
562 #ifdef ADAPTIVE_MUTEXES
563 		/*
564 		 * If the owner is running on another CPU, spin until the
565 		 * owner stops running or the state of the lock changes.
566 		 */
567 		owner = lv_mtx_owner(v);
568 		if (TD_IS_RUNNING(owner)) {
569 			if (LOCK_LOG_TEST(&m->lock_object, 0))
570 				CTR3(KTR_LOCK,
571 				    "%s: spinning on %p held by %p",
572 				    __func__, m, owner);
573 			KTR_STATE1(KTR_SCHED, "thread",
574 			    sched_tdname((struct thread *)tid),
575 			    "spinning", "lockname:\"%s\"",
576 			    m->lock_object.lo_name);
577 			do {
578 				lock_delay(&lda);
579 				v = MTX_READ_VALUE(m);
580 				owner = lv_mtx_owner(v);
581 			} while (v != MTX_UNOWNED && TD_IS_RUNNING(owner));
582 			KTR_STATE0(KTR_SCHED, "thread",
583 			    sched_tdname((struct thread *)tid),
584 			    "running");
585 			continue;
586 		}
587 #endif
588 
589 		ts = turnstile_trywait(&m->lock_object);
590 		v = MTX_READ_VALUE(m);
591 retry_turnstile:
592 
593 		/*
594 		 * Check if the lock has been released while spinning for
595 		 * the turnstile chain lock.
596 		 */
597 		if (v == MTX_UNOWNED) {
598 			turnstile_cancel(ts);
599 			continue;
600 		}
601 
602 #ifdef ADAPTIVE_MUTEXES
603 		/*
604 		 * The current lock owner might have started executing
605 		 * on another CPU (or the lock could have changed
606 		 * owners) while we were waiting on the turnstile
607 		 * chain lock.  If so, drop the turnstile lock and try
608 		 * again.
609 		 */
610 		owner = lv_mtx_owner(v);
611 		if (TD_IS_RUNNING(owner)) {
612 			turnstile_cancel(ts);
613 			continue;
614 		}
615 #endif
616 
617 		/*
618 		 * If the mutex isn't already contested and a failure occurs
619 		 * setting the contested bit, the mutex was either released
620 		 * or the state of the MTX_RECURSED bit changed.
621 		 */
622 		if ((v & MTX_CONTESTED) == 0 &&
623 		    !atomic_fcmpset_ptr(&m->mtx_lock, &v, v | MTX_CONTESTED)) {
624 			goto retry_turnstile;
625 		}
626 
627 		/*
628 		 * We definitely must sleep for this lock.
629 		 */
630 		mtx_assert(m, MA_NOTOWNED);
631 
632 #ifdef KTR
633 		if (!cont_logged) {
634 			CTR6(KTR_CONTENTION,
635 			    "contention: %p at %s:%d wants %s, taken by %s:%d",
636 			    (void *)tid, file, line, m->lock_object.lo_name,
637 			    WITNESS_FILE(&m->lock_object),
638 			    WITNESS_LINE(&m->lock_object));
639 			cont_logged = 1;
640 		}
641 #endif
642 
643 		/*
644 		 * Block on the turnstile.
645 		 */
646 #ifdef KDTRACE_HOOKS
647 		sleep_time -= lockstat_nsecs(&m->lock_object);
648 #endif
649 #ifndef ADAPTIVE_MUTEXES
650 		owner = mtx_owner(m);
651 #endif
652 		MPASS(owner == mtx_owner(m));
653 		turnstile_wait(ts, owner, TS_EXCLUSIVE_QUEUE);
654 #ifdef KDTRACE_HOOKS
655 		sleep_time += lockstat_nsecs(&m->lock_object);
656 		sleep_cnt++;
657 #endif
658 		v = MTX_READ_VALUE(m);
659 	}
660 #ifdef KTR
661 	if (cont_logged) {
662 		CTR4(KTR_CONTENTION,
663 		    "contention end: %s acquired by %p at %s:%d",
664 		    m->lock_object.lo_name, (void *)tid, file, line);
665 	}
666 #endif
667 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
668 	if (__predict_true(!doing_lockprof))
669 		return;
670 #endif
671 #ifdef KDTRACE_HOOKS
672 	all_time += lockstat_nsecs(&m->lock_object);
673 	if (sleep_time)
674 		LOCKSTAT_RECORD1(adaptive__block, m, sleep_time);
675 
676 	/*
677 	 * Only record the loops spinning and not sleeping.
678 	 */
679 	if (lda.spin_cnt > sleep_cnt)
680 		LOCKSTAT_RECORD1(adaptive__spin, m, all_time - sleep_time);
681 out_lockstat:
682 #endif
683 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(adaptive__acquire, m, contested,
684 	    waittime, file, line);
685 }
686 
687 #ifdef SMP
688 /*
689  * _mtx_lock_spin_cookie: the tougher part of acquiring an MTX_SPIN lock.
690  *
691  * This is only called if we need to actually spin for the lock. Recursion
692  * is handled inline.
693  */
694 #if LOCK_DEBUG > 0
695 void
696 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v, int opts,
697     const char *file, int line)
698 #else
699 void
700 _mtx_lock_spin_cookie(volatile uintptr_t *c, uintptr_t v)
701 #endif
702 {
703 	struct mtx *m;
704 	struct lock_delay_arg lda;
705 	uintptr_t tid;
706 #ifdef LOCK_PROFILING
707 	int contested = 0;
708 	uint64_t waittime = 0;
709 #endif
710 #ifdef KDTRACE_HOOKS
711 	int64_t spin_time = 0;
712 #endif
713 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
714 	int doing_lockprof;
715 #endif
716 
717 	tid = (uintptr_t)curthread;
718 	m = mtxlock2mtx(c);
719 
720 #ifdef KDTRACE_HOOKS
721 	if (LOCKSTAT_PROFILE_ENABLED(adaptive__acquire)) {
722 		while (v == MTX_UNOWNED) {
723 			if (_mtx_obtain_lock_fetch(m, &v, tid))
724 				goto out_lockstat;
725 		}
726 		doing_lockprof = 1;
727 		spin_time -= lockstat_nsecs(&m->lock_object);
728 	}
729 #endif
730 #ifdef LOCK_PROFILING
731 	doing_lockprof = 1;
732 #endif
733 
734 	if (__predict_false(v == MTX_UNOWNED))
735 		v = MTX_READ_VALUE(m);
736 
737 	if (__predict_false(v == tid)) {
738 		m->mtx_recurse++;
739 		return;
740 	}
741 
742 	if (SCHEDULER_STOPPED())
743 		return;
744 
745 	lock_delay_arg_init(&lda, &mtx_spin_delay);
746 
747 	if (LOCK_LOG_TEST(&m->lock_object, opts))
748 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spinning", m);
749 	KTR_STATE1(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
750 	    "spinning", "lockname:\"%s\"", m->lock_object.lo_name);
751 
752 #ifdef HWPMC_HOOKS
753 	PMC_SOFT_CALL( , , lock, failed);
754 #endif
755 	lock_profile_obtain_lock_failed(&m->lock_object, &contested, &waittime);
756 
757 	for (;;) {
758 		if (v == MTX_UNOWNED) {
759 			if (_mtx_obtain_lock_fetch(m, &v, tid))
760 				break;
761 			continue;
762 		}
763 		/* Give interrupts a chance while we spin. */
764 		spinlock_exit();
765 		do {
766 			if (__predict_true(lda.spin_cnt < 10000000)) {
767 				lock_delay(&lda);
768 			} else {
769 				_mtx_lock_indefinite_check(m, &lda);
770 			}
771 			v = MTX_READ_VALUE(m);
772 		} while (v != MTX_UNOWNED);
773 		spinlock_enter();
774 	}
775 
776 	if (LOCK_LOG_TEST(&m->lock_object, opts))
777 		CTR1(KTR_LOCK, "_mtx_lock_spin: %p spin done", m);
778 	KTR_STATE0(KTR_SCHED, "thread", sched_tdname((struct thread *)tid),
779 	    "running");
780 
781 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
782 	if (__predict_true(!doing_lockprof))
783 		return;
784 #endif
785 #ifdef KDTRACE_HOOKS
786 	spin_time += lockstat_nsecs(&m->lock_object);
787 	if (lda.spin_cnt != 0)
788 		LOCKSTAT_RECORD1(spin__spin, m, spin_time);
789 out_lockstat:
790 #endif
791 	LOCKSTAT_PROFILE_OBTAIN_LOCK_SUCCESS(spin__acquire, m,
792 	    contested, waittime, file, line);
793 }
794 #endif /* SMP */
795 
796 #ifdef INVARIANTS
797 static void
798 thread_lock_validate(struct mtx *m, int opts, const char *file, int line)
799 {
800 
801 	KASSERT(m->mtx_lock != MTX_DESTROYED,
802 	    ("thread_lock() of destroyed mutex @ %s:%d", file, line));
803 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
804 	    ("thread_lock() of sleep mutex %s @ %s:%d",
805 	    m->lock_object.lo_name, file, line));
806 	if (mtx_owned(m))
807 		KASSERT((m->lock_object.lo_flags & LO_RECURSABLE) != 0,
808 		    ("thread_lock: recursed on non-recursive mutex %s @ %s:%d\n",
809 		    m->lock_object.lo_name, file, line));
810 	WITNESS_CHECKORDER(&m->lock_object,
811 	    opts | LOP_NEWORDER | LOP_EXCLUSIVE, file, line, NULL);
812 }
813 #else
814 #define thread_lock_validate(m, opts, file, line) do { } while (0)
815 #endif
816 
817 #ifndef LOCK_PROFILING
818 #if LOCK_DEBUG > 0
819 void
820 _thread_lock(struct thread *td, int opts, const char *file, int line)
821 #else
822 void
823 _thread_lock(struct thread *td)
824 #endif
825 {
826 	struct mtx *m;
827 	uintptr_t tid, v;
828 
829 	tid = (uintptr_t)curthread;
830 
831 	if (__predict_false(LOCKSTAT_PROFILE_ENABLED(spin__acquire)))
832 		goto slowpath_noirq;
833 	spinlock_enter();
834 	m = td->td_lock;
835 	thread_lock_validate(m, 0, file, line);
836 	v = MTX_READ_VALUE(m);
837 	if (__predict_true(v == MTX_UNOWNED)) {
838 		if (__predict_false(!_mtx_obtain_lock(m, tid)))
839 			goto slowpath_unlocked;
840 	} else if (v == tid) {
841 		m->mtx_recurse++;
842 	} else
843 		goto slowpath_unlocked;
844 	if (__predict_true(m == td->td_lock)) {
845 		WITNESS_LOCK(&m->lock_object, LOP_EXCLUSIVE, file, line);
846 		return;
847 	}
848 	MPASS(m->mtx_recurse == 0);
849 	_mtx_release_lock_quick(m);
850 slowpath_unlocked:
851 	spinlock_exit();
852 slowpath_noirq:
853 #if LOCK_DEBUG > 0
854 	thread_lock_flags_(td, opts, file, line);
855 #else
856 	thread_lock_flags_(td, 0, 0, 0);
857 #endif
858 }
859 #endif
860 
861 void
862 thread_lock_flags_(struct thread *td, int opts, const char *file, int line)
863 {
864 	struct mtx *m;
865 	uintptr_t tid, v;
866 	struct lock_delay_arg lda;
867 #ifdef LOCK_PROFILING
868 	int contested = 0;
869 	uint64_t waittime = 0;
870 #endif
871 #ifdef KDTRACE_HOOKS
872 	int64_t spin_time = 0;
873 #endif
874 #if defined(KDTRACE_HOOKS) || defined(LOCK_PROFILING)
875 	int doing_lockprof = 1;
876 #endif
877 
878 	tid = (uintptr_t)curthread;
879 
880 	if (SCHEDULER_STOPPED()) {
881 		/*
882 		 * Ensure that spinlock sections are balanced even when the
883 		 * scheduler is stopped, since we may otherwise inadvertently
884 		 * re-enable interrupts while dumping core.
885 		 */
886 		spinlock_enter();
887 		return;
888 	}
889 
890 	lock_delay_arg_init(&lda, &mtx_spin_delay);
891 
892 #ifdef HWPMC_HOOKS
893 	PMC_SOFT_CALL( , , lock, failed);
894 #endif
895 
896 #ifdef LOCK_PROFILING
897 	doing_lockprof = 1;
898 #elif defined(KDTRACE_HOOKS)
899 	doing_lockprof = lockstat_enabled;
900 	if (__predict_false(doing_lockprof))
901 		spin_time -= lockstat_nsecs(&td->td_lock->lock_object);
902 #endif
903 	spinlock_enter();
904 
905 	for (;;) {
906 retry:
907 		m = td->td_lock;
908 		thread_lock_validate(m, opts, file, line);
909 		v = MTX_READ_VALUE(m);
910 		for (;;) {
911 			if (v == MTX_UNOWNED) {
912 				if (_mtx_obtain_lock_fetch(m, &v, tid))
913 					break;
914 				continue;
915 			}
916 			if (v == tid) {
917 				m->mtx_recurse++;
918 				MPASS(m == td->td_lock);
919 				break;
920 			}
921 			lock_profile_obtain_lock_failed(&m->lock_object,
922 			    &contested, &waittime);
923 			/* Give interrupts a chance while we spin. */
924 			spinlock_exit();
925 			do {
926 				if (__predict_true(lda.spin_cnt < 10000000)) {
927 					lock_delay(&lda);
928 				} else {
929 					_mtx_lock_indefinite_check(m, &lda);
930 				}
931 				if (m != td->td_lock) {
932 					spinlock_enter();
933 					goto retry;
934 				}
935 				v = MTX_READ_VALUE(m);
936 			} while (v != MTX_UNOWNED);
937 			spinlock_enter();
938 		}
939 		if (m == td->td_lock)
940 			break;
941 		MPASS(m->mtx_recurse == 0);
942 		_mtx_release_lock_quick(m);
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 static void __noinline
1224 _mtx_lock_indefinite_check(struct mtx *m, struct lock_delay_arg *ldap)
1225 {
1226 	struct thread *td;
1227 
1228 	ldap->spin_cnt++;
1229 	if (ldap->spin_cnt < 60000000 || kdb_active || panicstr != NULL)
1230 		DELAY(1);
1231 	else {
1232 		td = mtx_owner(m);
1233 
1234 		/* If the mutex is unlocked, try again. */
1235 		if (td == NULL)
1236 			return;
1237 
1238 		printf( "spin lock %p (%s) held by %p (tid %d) too long\n",
1239 		    m, m->lock_object.lo_name, td, td->td_tid);
1240 #ifdef WITNESS
1241 		witness_display_spinlock(&m->lock_object, td, printf);
1242 #endif
1243 		panic("spin lock held too long");
1244 	}
1245 	cpu_spinwait();
1246 }
1247 
1248 void
1249 mtx_spin_wait_unlocked(struct mtx *m)
1250 {
1251 	struct lock_delay_arg lda;
1252 
1253 	KASSERT(m->mtx_lock != MTX_DESTROYED,
1254 	    ("%s() of destroyed mutex %p", __func__, m));
1255 	KASSERT(LOCK_CLASS(&m->lock_object) == &lock_class_mtx_spin,
1256 	    ("%s() of sleep mutex %p (%s)", __func__, m,
1257 	    m->lock_object.lo_name));
1258 	KASSERT(!mtx_owned(m), ("%s() waiting on myself on lock %p (%s)", __func__, m,
1259 	    m->lock_object.lo_name));
1260 
1261 	lda.spin_cnt = 0;
1262 
1263 	while (atomic_load_acq_ptr(&m->mtx_lock) != MTX_UNOWNED) {
1264 		if (__predict_true(lda.spin_cnt < 10000000)) {
1265 			cpu_spinwait();
1266 			lda.spin_cnt++;
1267 		} else {
1268 			_mtx_lock_indefinite_check(m, &lda);
1269 		}
1270 	}
1271 }
1272 
1273 #ifdef DDB
1274 void
1275 db_show_mtx(const struct lock_object *lock)
1276 {
1277 	struct thread *td;
1278 	const struct mtx *m;
1279 
1280 	m = (const struct mtx *)lock;
1281 
1282 	db_printf(" flags: {");
1283 	if (LOCK_CLASS(lock) == &lock_class_mtx_spin)
1284 		db_printf("SPIN");
1285 	else
1286 		db_printf("DEF");
1287 	if (m->lock_object.lo_flags & LO_RECURSABLE)
1288 		db_printf(", RECURSE");
1289 	if (m->lock_object.lo_flags & LO_DUPOK)
1290 		db_printf(", DUPOK");
1291 	db_printf("}\n");
1292 	db_printf(" state: {");
1293 	if (mtx_unowned(m))
1294 		db_printf("UNOWNED");
1295 	else if (mtx_destroyed(m))
1296 		db_printf("DESTROYED");
1297 	else {
1298 		db_printf("OWNED");
1299 		if (m->mtx_lock & MTX_CONTESTED)
1300 			db_printf(", CONTESTED");
1301 		if (m->mtx_lock & MTX_RECURSED)
1302 			db_printf(", RECURSED");
1303 	}
1304 	db_printf("}\n");
1305 	if (!mtx_unowned(m) && !mtx_destroyed(m)) {
1306 		td = mtx_owner(m);
1307 		db_printf(" owner: %p (tid %d, pid %d, \"%s\")\n", td,
1308 		    td->td_tid, td->td_proc->p_pid, td->td_name);
1309 		if (mtx_recursed(m))
1310 			db_printf(" recursed: %d\n", m->mtx_recurse);
1311 	}
1312 }
1313 #endif
1314