subr_turnstile.c revision 69215
11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
31573Srgrimes *
41573Srgrimes * Redistribution and use in source and binary forms, with or without
51573Srgrimes * modification, are permitted provided that the following conditions
61573Srgrimes * are met:
71573Srgrimes * 1. Redistributions of source code must retain the above copyright
81573Srgrimes *    notice, this list of conditions and the following disclaimer.
91573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
101573Srgrimes *    notice, this list of conditions and the following disclaimer in the
111573Srgrimes *    documentation and/or other materials provided with the distribution.
121573Srgrimes * 3. Berkeley Software Design Inc's name may not be used to endorse or
131573Srgrimes *    promote products derived from this software without specific prior
141573Srgrimes *    written permission.
151573Srgrimes *
161573Srgrimes * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
171573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
201573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261573Srgrimes * SUCH DAMAGE.
271573Srgrimes *
2814272Spst *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
2950476Speter *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
301573Srgrimes * $FreeBSD: head/sys/kern/subr_turnstile.c 69215 2000-11-26 21:14:17Z alfred $
3170936Sru */
3270936Sru
3370936Sru/*
3470936Sru *	Main Entry: witness
3570936Sru *	Pronunciation: 'wit-n&s
3670936Sru *	Function: noun
3770936Sru *	Etymology: Middle English witnesse, from Old English witnes knowledge,
3884306Sru *	    testimony, witness, from 2wit
3984306Sru *	Date: before 12th century
4070936Sru *	1 : attestation of a fact or event : TESTIMONY
411573Srgrimes *	2 : one that gives evidence; specifically : one who testifies in
4270936Sru *	    a cause or before a judicial tribunal
431573Srgrimes *	3 : one asked to be present at a transaction so as to be able to
4470936Sru *	    testify to its having taken place
4570936Sru *	4 : one who has personal knowledge of something
4670936Sru *	5 a : something serving as evidence or proof : SIGN
471573Srgrimes *	  b : public affirmation by word or example of usually
4870936Sru *	      religious faith or conviction <the heroic witness to divine
4970936Sru *	      life -- Pilot>
5070936Sru *	6 capitalized : a member of the Jehovah's Witnesses
5170936Sru */
5270936Sru
5370936Sru#include "opt_ddb.h"
5470936Sru#include "opt_witness.h"
5570936Sru
561573Srgrimes/*
5770936Sru * Cause non-inlined mtx_*() to be compiled.
5870936Sru * Must be defined early because other system headers may include mutex.h.
5970936Sru */
6070936Sru#define _KERN_MUTEX_C_
6170936Sru
6270936Sru#include <sys/param.h>
63119893Sru#include <sys/bus.h>
6470936Sru#include <sys/kernel.h>
6570936Sru#include <sys/malloc.h>
661573Srgrimes#include <sys/proc.h>
6770936Sru#include <sys/sysctl.h>
6870936Sru#include <sys/systm.h>
6970936Sru#include <sys/vmmeter.h>
7070936Sru#include <sys/ktr.h>
7170936Sru
7270936Sru#include <machine/atomic.h>
7370936Sru#include <machine/bus.h>
7470936Sru#include <machine/clock.h>
751573Srgrimes#include <machine/cpu.h>
7670936Sru
7770936Sru#include <ddb/ddb.h>
781573Srgrimes
7970936Sru#include <vm/vm.h>
8070936Sru#include <vm/vm_extern.h>
811573Srgrimes
8270936Sru#include <sys/mutex.h>
831573Srgrimes
8470936Sru/*
8570936Sru * Machine independent bits of the mutex implementation
86131504Sru */
871573Srgrimes/* All mutexes in system (used for debug/panic) */
881573Srgrimes#ifdef MUTEX_DEBUG
8970936Srustatic struct mtx_debug all_mtx_debug = { NULL, {NULL, NULL}, NULL, 0,
901573Srgrimes	"All mutexes queue head" };
9170936Srustatic struct mtx all_mtx = { MTX_UNOWNED, 0, 0, &all_mtx_debug,
9270936Sru	TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked),
9370936Sru	{ NULL, NULL }, &all_mtx, &all_mtx };
9470936Sru#else	/* MUTEX_DEBUG */
9570936Srustatic struct mtx all_mtx = { MTX_UNOWNED, 0, 0, "All mutexes queue head",
9670936Sru	TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked),
9770936Sru	{ NULL, NULL }, &all_mtx, &all_mtx };
9870936Sru#endif	/* MUTEX_DEBUG */
9970936Sru
10070936Srustatic int	mtx_cur_cnt;
1011573Srgrimesstatic int	mtx_max_cnt;
10270936Sru
1031573Srgrimesvoid	_mtx_enter_giant_def(void);
1041573Srgrimesvoid	_mtx_exit_giant_def(void);
10570936Srustatic void propagate_priority(struct proc *) __unused;
1061573Srgrimes
10770936Sru#define	mtx_unowned(m)	((m)->mtx_lock == MTX_UNOWNED)
10870936Sru#define	mtx_owner(m)	(mtx_unowned(m) ? NULL \
10970936Sru			    : (struct proc *)((m)->mtx_lock & MTX_FLAGMASK))
11070936Sru
11170936Sru#define RETIP(x)		*(((uintptr_t *)(&x)) - 1)
11270936Sru#define	SET_PRIO(p, pri)	(p)->p_priority = (pri)
11370936Sru
11470936Sru/*
1151573Srgrimes * XXX Temporary, for use from assembly language
1161573Srgrimes */
11770936Sru
1181573Srgrimesvoid
1191573Srgrimes_mtx_enter_giant_def(void)
1201573Srgrimes{
1211573Srgrimes
1221573Srgrimes	mtx_enter(&Giant, MTX_DEF);
1231573Srgrimes}
1241573Srgrimes
1251573Srgrimesvoid
12670936Sru_mtx_exit_giant_def(void)
1271573Srgrimes{
12870936Sru
1291573Srgrimes	mtx_exit(&Giant, MTX_DEF);
1301573Srgrimes}
1311573Srgrimes
13270936Srustatic void
13370936Srupropagate_priority(struct proc *p)
13470936Sru{
13570936Sru	int pri = p->p_priority;
1361573Srgrimes	struct mtx *m = p->p_blocked;
1371573Srgrimes
13870936Sru	for (;;) {
1391573Srgrimes		struct proc *p1;
1401573Srgrimes
14170936Sru		p = mtx_owner(m);
1421573Srgrimes
1431573Srgrimes		if (p == NULL) {
144131504Sru			/*
1451573Srgrimes			 * This really isn't quite right. Really
1461573Srgrimes			 * ought to bump priority of process that
1471573Srgrimes			 * next acquires the mutex.
14870936Sru			 */
1491573Srgrimes			MPASS(m->mtx_lock == MTX_CONTESTED);
15070936Sru			return;
1511573Srgrimes		}
1521573Srgrimes		MPASS(p->p_magic == P_MAGIC);
1531573Srgrimes		if (p->p_priority <= pri)
15470936Sru			return;
1551573Srgrimes		/*
1561573Srgrimes		 * If lock holder is actually running, just bump priority.
15770936Sru		 */
1581573Srgrimes		if (TAILQ_NEXT(p, p_procq) == NULL) {
1591573Srgrimes			MPASS(p->p_stat == SRUN || p->p_stat == SZOMB);
1601573Srgrimes			SET_PRIO(p, pri);
1611573Srgrimes			return;
1621573Srgrimes		}
1631573Srgrimes		/*
1641573Srgrimes		 * If on run queue move to new run queue, and
16570936Sru		 * quit.
16670936Sru		 */
16770936Sru		if (p->p_stat == SRUN) {
16870936Sru			MPASS(p->p_blocked == NULL);
1691573Srgrimes			remrunqueue(p);
17070936Sru			SET_PRIO(p, pri);
171108087Sru			setrunqueue(p);
172108087Sru			return;
173108087Sru		}
17470936Sru
1751573Srgrimes		/*
1761573Srgrimes		 * If we aren't blocked on a mutex, give up and quit.
1771573Srgrimes		 */
1781573Srgrimes		if (p->p_stat != SMTX) {
1791573Srgrimes			printf(
1801573Srgrimes	"XXX: process %d(%s):%d holds %s but isn't blocked on a mutex\n",
1811573Srgrimes			    p->p_pid, p->p_comm, p->p_stat, m->mtx_description);
18270936Sru			return;
18370936Sru		}
18470936Sru
18570936Sru		/*
18670936Sru		 * Pick up the mutex that p is blocked on.
1871573Srgrimes		 */
1881573Srgrimes		m = p->p_blocked;
1891573Srgrimes		MPASS(m != NULL);
19070936Sru
19170936Sru		printf("XXX: process %d(%s) is blocked on %s\n", p->p_pid,
19270936Sru		    p->p_comm, m->mtx_description);
19370936Sru		/*
1941573Srgrimes		 * Check if the proc needs to be moved up on
19570936Sru		 * the blocked chain
1961573Srgrimes		 */
19770936Sru		if ((p1 = TAILQ_PREV(p, rq, p_procq)) == NULL ||
1981573Srgrimes		    p1->p_priority <= pri) {
1991573Srgrimes			if (p1)
20070936Sru				printf(
2011573Srgrimes	"XXX: previous process %d(%s) has higher priority\n",
20270936Sru				    p->p_pid, p->p_comm);
20370936Sru			else
20470936Sru				printf("XXX: process at head of run queue\n");
20570936Sru			continue;
20670936Sru		}
207108087Sru
20870936Sru		/*
20970936Sru		 * Remove proc from blocked chain
21070936Sru		 */
211108087Sru		TAILQ_REMOVE(&m->mtx_blocked, p, p_procq);
21270936Sru		TAILQ_FOREACH(p1, &m->mtx_blocked, p_procq) {
2131573Srgrimes			MPASS(p1->p_magic == P_MAGIC);
21470936Sru			if (p1->p_priority > pri)
2151573Srgrimes				break;
21670936Sru		}
2171573Srgrimes		if (p1)
2181573Srgrimes			TAILQ_INSERT_BEFORE(p1, p, p_procq);
21970936Sru		else
22070936Sru			TAILQ_INSERT_TAIL(&m->mtx_blocked, p, p_procq);
22170936Sru		CTR4(KTR_LOCK,
2221573Srgrimes		    "propagate priority: p 0x%p moved before 0x%p on [0x%p] %s",
2231573Srgrimes		    p, p1, m, m->mtx_description);
22470936Sru	}
22570936Sru}
22670936Sru
22770936Sruvoid
2281573Srgrimesmtx_enter_hard(struct mtx *m, int type, int saveintr)
22970936Sru{
23070936Sru	struct proc *p = CURPROC;
23170936Sru
2321573Srgrimes	KASSERT(p != NULL, ("curproc is NULL in mutex"));
2331573Srgrimes
23470936Sru	switch (type) {
23514272Spst	case MTX_DEF:
23670936Sru		if ((m->mtx_lock & MTX_FLAGMASK) == (uintptr_t)p) {
23714272Spst			m->mtx_recurse++;
23870936Sru			atomic_set_ptr(&m->mtx_lock, MTX_RECURSE);
23914272Spst			CTR1(KTR_LOCK, "mtx_enter: 0x%p recurse", m);
24070936Sru			return;
24170936Sru		}
24270936Sru		CTR3(KTR_LOCK, "mtx_enter: 0x%p contested (lock=%p) [0x%p]",
24370936Sru		    m, (void *)m->mtx_lock, (void *)RETIP(m));
24470936Sru		while (!_obtain_lock(m, p)) {
24570936Sru			uintptr_t v;
24670936Sru			struct proc *p1;
24770936Sru
24870936Sru			mtx_enter(&sched_lock, MTX_SPIN | MTX_RLIKELY);
24970936Sru			/*
25070936Sru			 * check if the lock has been released while
25170936Sru			 * waiting for the schedlock.
25270936Sru			 */
25370936Sru			if ((v = m->mtx_lock) == MTX_UNOWNED) {
25470936Sru				mtx_exit(&sched_lock, MTX_SPIN);
25570936Sru				continue;
25670936Sru			}
25770936Sru			/*
25870936Sru			 * The mutex was marked contested on release. This
25970936Sru			 * means that there are processes blocked on it.
26070936Sru			 */
26170936Sru			if (v == MTX_CONTESTED) {
26270936Sru				p1 = TAILQ_FIRST(&m->mtx_blocked);
26370936Sru				KASSERT(p1 != NULL, ("contested mutex has no contesters"));
26470936Sru				KASSERT(p != NULL, ("curproc is NULL for contested mutex"));
26570936Sru				m->mtx_lock = (uintptr_t)p | MTX_CONTESTED;
26670936Sru				if (p1->p_priority < p->p_priority) {
26770936Sru					SET_PRIO(p, p1->p_priority);
26870936Sru				}
26970936Sru				mtx_exit(&sched_lock, MTX_SPIN);
27070936Sru				return;
2711573Srgrimes			}
272			/*
273			 * If the mutex isn't already contested and
274			 * a failure occurs setting the contested bit the
275			 * mutex was either release or the
276			 * state of the RECURSION bit changed.
277			 */
278			if ((v & MTX_CONTESTED) == 0 &&
279			    !atomic_cmpset_ptr(&m->mtx_lock, (void *)v,
280				               (void *)(v | MTX_CONTESTED))) {
281				mtx_exit(&sched_lock, MTX_SPIN);
282				continue;
283			}
284
285			/* We definitely have to sleep for this lock */
286			mtx_assert(m, MA_NOTOWNED);
287
288#ifdef notyet
289			/*
290			 * If we're borrowing an interrupted thread's VM
291			 * context must clean up before going to sleep.
292			 */
293			if (p->p_flag & (P_ITHD | P_SITHD)) {
294				ithd_t *it = (ithd_t *)p;
295
296				if (it->it_interrupted) {
297					CTR2(KTR_LOCK,
298					    "mtx_enter: 0x%x interrupted 0x%x",
299					    it, it->it_interrupted);
300					intr_thd_fixup(it);
301				}
302			}
303#endif
304
305			/* Put us on the list of procs blocked on this mutex */
306			if (TAILQ_EMPTY(&m->mtx_blocked)) {
307				p1 = (struct proc *)(m->mtx_lock &
308						     MTX_FLAGMASK);
309				LIST_INSERT_HEAD(&p1->p_contested, m,
310						 mtx_contested);
311				TAILQ_INSERT_TAIL(&m->mtx_blocked, p, p_procq);
312			} else {
313				TAILQ_FOREACH(p1, &m->mtx_blocked, p_procq)
314					if (p1->p_priority > p->p_priority)
315						break;
316				if (p1)
317					TAILQ_INSERT_BEFORE(p1, p, p_procq);
318				else
319					TAILQ_INSERT_TAIL(&m->mtx_blocked, p,
320							  p_procq);
321			}
322
323			p->p_blocked = m;	/* Who we're blocked on */
324			p->p_stat = SMTX;
325#if 0
326			propagate_priority(p);
327#endif
328			CTR3(KTR_LOCK, "mtx_enter: p 0x%p blocked on [0x%p] %s",
329			    p, m, m->mtx_description);
330			mi_switch();
331			CTR3(KTR_LOCK,
332			    "mtx_enter: p 0x%p free from blocked on [0x%p] %s",
333			    p, m, m->mtx_description);
334			mtx_exit(&sched_lock, MTX_SPIN);
335		}
336		return;
337	case MTX_SPIN:
338	case MTX_SPIN | MTX_FIRST:
339	case MTX_SPIN | MTX_TOPHALF:
340	    {
341		int i = 0;
342
343		if (m->mtx_lock == (uintptr_t)p) {
344			m->mtx_recurse++;
345			return;
346		}
347		CTR1(KTR_LOCK, "mtx_enter: %p spinning", m);
348		for (;;) {
349			if (_obtain_lock(m, p))
350				break;
351			while (m->mtx_lock != MTX_UNOWNED) {
352				if (i++ < 1000000)
353					continue;
354				if (i++ < 6000000)
355					DELAY (1);
356#ifdef DDB
357				else if (!db_active)
358#else
359				else
360#endif
361					panic(
362				"spin lock %s held by 0x%p for > 5 seconds",
363					    m->mtx_description,
364					    (void *)m->mtx_lock);
365			}
366		}
367
368#ifdef MUTEX_DEBUG
369		if (type != MTX_SPIN)
370			m->mtx_saveintr = 0xbeefface;
371		else
372#endif
373			m->mtx_saveintr = saveintr;
374		CTR1(KTR_LOCK, "mtx_enter: 0x%p spin done", m);
375		return;
376	    }
377	}
378}
379
380void
381mtx_exit_hard(struct mtx *m, int type)
382{
383	struct proc *p, *p1;
384	struct mtx *m1;
385	int pri;
386
387	p = CURPROC;
388	switch (type) {
389	case MTX_DEF:
390	case MTX_DEF | MTX_NOSWITCH:
391		if (m->mtx_recurse != 0) {
392			if (--(m->mtx_recurse) == 0)
393				atomic_clear_ptr(&m->mtx_lock, MTX_RECURSE);
394			CTR1(KTR_LOCK, "mtx_exit: 0x%p unrecurse", m);
395			return;
396		}
397		mtx_enter(&sched_lock, MTX_SPIN);
398		CTR1(KTR_LOCK, "mtx_exit: 0x%p contested", m);
399		p1 = TAILQ_FIRST(&m->mtx_blocked);
400		MPASS(p->p_magic == P_MAGIC);
401		MPASS(p1->p_magic == P_MAGIC);
402		TAILQ_REMOVE(&m->mtx_blocked, p1, p_procq);
403		if (TAILQ_EMPTY(&m->mtx_blocked)) {
404			LIST_REMOVE(m, mtx_contested);
405			_release_lock_quick(m);
406			CTR1(KTR_LOCK, "mtx_exit: 0x%p not held", m);
407		} else
408			m->mtx_lock = MTX_CONTESTED;
409		pri = MAXPRI;
410		LIST_FOREACH(m1, &p->p_contested, mtx_contested) {
411			int cp = TAILQ_FIRST(&m1->mtx_blocked)->p_priority;
412			if (cp < pri)
413				pri = cp;
414		}
415		if (pri > p->p_nativepri)
416			pri = p->p_nativepri;
417		SET_PRIO(p, pri);
418		CTR2(KTR_LOCK, "mtx_exit: 0x%p contested setrunqueue 0x%p",
419		    m, p1);
420		p1->p_blocked = NULL;
421		p1->p_stat = SRUN;
422		setrunqueue(p1);
423		if ((type & MTX_NOSWITCH) == 0 && p1->p_priority < pri) {
424#ifdef notyet
425			if (p->p_flag & (P_ITHD | P_SITHD)) {
426				ithd_t *it = (ithd_t *)p;
427
428				if (it->it_interrupted) {
429					CTR2(KTR_LOCK,
430					    "mtx_exit: 0x%x interruped 0x%x",
431					    it, it->it_interrupted);
432					intr_thd_fixup(it);
433				}
434			}
435#endif
436			setrunqueue(p);
437			CTR2(KTR_LOCK, "mtx_exit: 0x%p switching out lock=0x%p",
438			    m, (void *)m->mtx_lock);
439			mi_switch();
440			CTR2(KTR_LOCK, "mtx_exit: 0x%p resuming lock=0x%p",
441			    m, (void *)m->mtx_lock);
442		}
443		mtx_exit(&sched_lock, MTX_SPIN);
444		break;
445	case MTX_SPIN:
446	case MTX_SPIN | MTX_FIRST:
447		if (m->mtx_recurse != 0) {
448			m->mtx_recurse--;
449			return;
450		}
451		MPASS(mtx_owned(m));
452		_release_lock_quick(m);
453		if (type & MTX_FIRST)
454			enable_intr();	/* XXX is this kosher? */
455		else {
456			MPASS(m->mtx_saveintr != 0xbeefface);
457			restore_intr(m->mtx_saveintr);
458		}
459		break;
460	case MTX_SPIN | MTX_TOPHALF:
461		if (m->mtx_recurse != 0) {
462			m->mtx_recurse--;
463			return;
464		}
465		MPASS(mtx_owned(m));
466		_release_lock_quick(m);
467		break;
468	default:
469		panic("mtx_exit_hard: unsupported type 0x%x\n", type);
470	}
471}
472
473#define MV_DESTROY	0	/* validate before destory */
474#define MV_INIT		1	/* validate before init */
475
476#ifdef MUTEX_DEBUG
477
478int mtx_validate __P((struct mtx *, int));
479
480int
481mtx_validate(struct mtx *m, int when)
482{
483	struct mtx *mp;
484	int i;
485	int retval = 0;
486
487	if (m == &all_mtx || cold)
488		return 0;
489
490	mtx_enter(&all_mtx, MTX_DEF);
491/*
492 * XXX - When kernacc() is fixed on the alpha to handle K0_SEG memory properly
493 * we can re-enable the kernacc() checks.
494 */
495#ifndef __alpha__
496	MPASS(kernacc((caddr_t)all_mtx.mtx_next, sizeof(uintptr_t),
497	    VM_PROT_READ) == 1);
498#endif
499	MPASS(all_mtx.mtx_next->mtx_prev == &all_mtx);
500	for (i = 0, mp = all_mtx.mtx_next; mp != &all_mtx; mp = mp->mtx_next) {
501#ifndef __alpha__
502		if (kernacc((caddr_t)mp->mtx_next, sizeof(uintptr_t),
503		    VM_PROT_READ) != 1) {
504			panic("mtx_validate: mp=%p mp->mtx_next=%p",
505			    mp, mp->mtx_next);
506		}
507#endif
508		i++;
509		if (i > mtx_cur_cnt) {
510			panic("mtx_validate: too many in chain, known=%d\n",
511			    mtx_cur_cnt);
512		}
513	}
514	MPASS(i == mtx_cur_cnt);
515	switch (when) {
516	case MV_DESTROY:
517		for (mp = all_mtx.mtx_next; mp != &all_mtx; mp = mp->mtx_next)
518			if (mp == m)
519				break;
520		MPASS(mp == m);
521		break;
522	case MV_INIT:
523		for (mp = all_mtx.mtx_next; mp != &all_mtx; mp = mp->mtx_next)
524		if (mp == m) {
525			/*
526			 * Not good. This mutex already exists.
527			 */
528			printf("re-initing existing mutex %s\n",
529			    m->mtx_description);
530			MPASS(m->mtx_lock == MTX_UNOWNED);
531			retval = 1;
532		}
533	}
534	mtx_exit(&all_mtx, MTX_DEF);
535	return (retval);
536}
537#endif
538
539void
540mtx_init(struct mtx *m, const char *t, int flag)
541{
542#ifdef MUTEX_DEBUG
543	struct mtx_debug *debug;
544#endif
545
546	CTR2(KTR_LOCK, "mtx_init 0x%p (%s)", m, t);
547#ifdef MUTEX_DEBUG
548	if (mtx_validate(m, MV_INIT))	/* diagnostic and error correction */
549		return;
550	if (flag & MTX_COLD)
551		debug = m->mtx_debug;
552	else
553		debug = NULL;
554	if (debug == NULL) {
555#ifdef DIAGNOSTIC
556		if(cold && bootverbose)
557			printf("malloc'ing mtx_debug while cold for %s\n", t);
558#endif
559
560		/* XXX - should not use DEVBUF */
561		debug = malloc(sizeof(struct mtx_debug), M_DEVBUF, M_NOWAIT);
562		MPASS(debug != NULL);
563		bzero(debug, sizeof(struct mtx_debug));
564	}
565#endif
566	bzero((void *)m, sizeof *m);
567	TAILQ_INIT(&m->mtx_blocked);
568#ifdef MUTEX_DEBUG
569	m->mtx_debug = debug;
570#endif
571	m->mtx_description = t;
572	m->mtx_lock = MTX_UNOWNED;
573	/* Put on all mutex queue */
574	mtx_enter(&all_mtx, MTX_DEF);
575	m->mtx_next = &all_mtx;
576	m->mtx_prev = all_mtx.mtx_prev;
577	m->mtx_prev->mtx_next = m;
578	all_mtx.mtx_prev = m;
579	if (++mtx_cur_cnt > mtx_max_cnt)
580		mtx_max_cnt = mtx_cur_cnt;
581	mtx_exit(&all_mtx, MTX_DEF);
582	witness_init(m, flag);
583}
584
585void
586mtx_destroy(struct mtx *m)
587{
588
589	CTR2(KTR_LOCK, "mtx_destroy 0x%p (%s)", m, m->mtx_description);
590#ifdef MUTEX_DEBUG
591	if (m->mtx_next == NULL)
592		panic("mtx_destroy: %p (%s) already destroyed",
593		    m, m->mtx_description);
594
595	if (!mtx_owned(m)) {
596		MPASS(m->mtx_lock == MTX_UNOWNED);
597	} else {
598		MPASS((m->mtx_lock & (MTX_RECURSE|MTX_CONTESTED)) == 0);
599	}
600	mtx_validate(m, MV_DESTROY);		/* diagnostic */
601#endif
602
603#ifdef WITNESS
604	if (m->mtx_witness)
605		witness_destroy(m);
606#endif /* WITNESS */
607
608	/* Remove from the all mutex queue */
609	mtx_enter(&all_mtx, MTX_DEF);
610	m->mtx_next->mtx_prev = m->mtx_prev;
611	m->mtx_prev->mtx_next = m->mtx_next;
612#ifdef MUTEX_DEBUG
613	m->mtx_next = m->mtx_prev = NULL;
614	free(m->mtx_debug, M_DEVBUF);
615	m->mtx_debug = NULL;
616#endif
617	mtx_cur_cnt--;
618	mtx_exit(&all_mtx, MTX_DEF);
619}
620
621/*
622 * The non-inlined versions of the mtx_*() functions are always built (above),
623 * but the witness code depends on the MUTEX_DEBUG and WITNESS kernel options
624 * being specified.
625 */
626#if (defined(MUTEX_DEBUG) && defined(WITNESS))
627
628#define WITNESS_COUNT 200
629#define	WITNESS_NCHILDREN 2
630
631int witness_watch = 1;
632
633struct witness {
634	struct witness	*w_next;
635	const char	*w_description;
636	const char	*w_file;
637	int		 w_line;
638	struct witness	*w_morechildren;
639	u_char		 w_childcnt;
640	u_char		 w_Giant_squawked:1;
641	u_char		 w_other_squawked:1;
642	u_char		 w_same_squawked:1;
643	u_char		 w_sleep:1;
644	u_char		 w_spin:1;	/* this is a spin mutex */
645	u_int		 w_level;
646	struct witness	*w_children[WITNESS_NCHILDREN];
647};
648
649struct witness_blessed {
650	char 	*b_lock1;
651	char	*b_lock2;
652};
653
654#ifdef DDB
655/*
656 * When DDB is enabled and witness_ddb is set to 1, it will cause the system to
657 * drop into kdebug() when:
658 *	- a lock heirarchy violation occurs
659 *	- locks are held when going to sleep.
660 */
661#ifdef WITNESS_DDB
662int	witness_ddb = 1;
663#else
664int	witness_ddb = 0;
665#endif
666SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, "");
667#endif /* DDB */
668
669#ifdef WITNESS_SKIPSPIN
670int	witness_skipspin = 1;
671#else
672int	witness_skipspin = 0;
673#endif
674SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0,
675    "");
676
677MUTEX_DECLARE(static,w_mtx);
678static struct witness	*w_free;
679static struct witness	*w_all;
680static int		 w_inited;
681static int		 witness_dead;	/* fatal error, probably no memory */
682
683static struct witness	 w_data[WITNESS_COUNT];
684
685static struct witness	 *enroll __P((const char *description, int flag));
686static int itismychild __P((struct witness *parent, struct witness *child));
687static void removechild __P((struct witness *parent, struct witness *child));
688static int isitmychild __P((struct witness *parent, struct witness *child));
689static int isitmydescendant __P((struct witness *parent, struct witness *child));
690static int dup_ok __P((struct witness *));
691static int blessed __P((struct witness *, struct witness *));
692static void witness_displaydescendants
693    __P((void(*)(const char *fmt, ...), struct witness *));
694static void witness_leveldescendents __P((struct witness *parent, int level));
695static void witness_levelall __P((void));
696static struct witness * witness_get __P((void));
697static void witness_free __P((struct witness *m));
698
699
700static char *ignore_list[] = {
701	"witness lock",
702	NULL
703};
704
705static char *spin_order_list[] = {
706	"sched lock",
707	"sio",
708#ifdef __i386__
709	"clk",
710#endif
711	"callout",
712	/*
713	 * leaf locks
714	 */
715	NULL
716};
717
718static char *order_list[] = {
719	"uidinfo hash", "uidinfo struct", NULL,
720	NULL
721};
722
723static char *dup_list[] = {
724	NULL
725};
726
727static char *sleep_list[] = {
728	"Giant",
729	NULL
730};
731
732/*
733 * Pairs of locks which have been blessed
734 * Don't complain about order problems with blessed locks
735 */
736static struct witness_blessed blessed_list[] = {
737};
738static int blessed_count = sizeof(blessed_list) / sizeof(struct witness_blessed);
739
740void
741witness_init(struct mtx *m, int flag)
742{
743	m->mtx_witness = enroll(m->mtx_description, flag);
744}
745
746void
747witness_destroy(struct mtx *m)
748{
749	struct mtx *m1;
750	struct proc *p;
751	p = CURPROC;
752	for ((m1 = LIST_FIRST(&p->p_heldmtx)); m1 != NULL;
753		m1 = LIST_NEXT(m1, mtx_held)) {
754		if (m1 == m) {
755			LIST_REMOVE(m, mtx_held);
756			break;
757		}
758	}
759	return;
760
761}
762
763void
764witness_enter(struct mtx *m, int flags, const char *file, int line)
765{
766	struct witness *w, *w1;
767	struct mtx *m1;
768	struct proc *p;
769	int i;
770#ifdef DDB
771	int go_into_ddb = 0;
772#endif /* DDB */
773
774	w = m->mtx_witness;
775	p = CURPROC;
776
777	if (flags & MTX_SPIN) {
778		if (!w->w_spin)
779			panic("mutex_enter: MTX_SPIN on MTX_DEF mutex %s @"
780			    " %s:%d", m->mtx_description, file, line);
781		if (m->mtx_recurse != 0)
782			return;
783		mtx_enter(&w_mtx, MTX_SPIN);
784		i = witness_spin_check;
785		if (i != 0 && w->w_level < i) {
786			mtx_exit(&w_mtx, MTX_SPIN);
787			panic("mutex_enter(%s:%x, MTX_SPIN) out of order @"
788			    " %s:%d already holding %s:%x",
789			    m->mtx_description, w->w_level, file, line,
790			    spin_order_list[ffs(i)-1], i);
791		}
792		PCPU_SET(witness_spin_check, i | w->w_level);
793		mtx_exit(&w_mtx, MTX_SPIN);
794		return;
795	}
796	if (w->w_spin)
797		panic("mutex_enter: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
798		    m->mtx_description, file, line);
799
800	if (m->mtx_recurse != 0)
801		return;
802	if (witness_dead)
803		goto out;
804	if (cold || panicstr)
805		goto out;
806
807	if (!mtx_legal2block())
808		panic("blockable mtx_enter() of %s when not legal @ %s:%d",
809			    m->mtx_description, file, line);
810	/*
811	 * Is this the first mutex acquired
812	 */
813	if ((m1 = LIST_FIRST(&p->p_heldmtx)) == NULL)
814		goto out;
815
816	if ((w1 = m1->mtx_witness) == w) {
817		if (w->w_same_squawked || dup_ok(w))
818			goto out;
819		w->w_same_squawked = 1;
820		printf("acquring duplicate lock of same type: \"%s\"\n",
821			m->mtx_description);
822		printf(" 1st @ %s:%d\n", w->w_file, w->w_line);
823		printf(" 2nd @ %s:%d\n", file, line);
824#ifdef DDB
825		go_into_ddb = 1;
826#endif /* DDB */
827		goto out;
828	}
829	MPASS(!mtx_owned(&w_mtx));
830	mtx_enter(&w_mtx, MTX_SPIN);
831	/*
832	 * If we have a known higher number just say ok
833	 */
834	if (witness_watch > 1 && w->w_level > w1->w_level) {
835		mtx_exit(&w_mtx, MTX_SPIN);
836		goto out;
837	}
838	if (isitmydescendant(m1->mtx_witness, w)) {
839		mtx_exit(&w_mtx, MTX_SPIN);
840		goto out;
841	}
842	for (i = 0; m1 != NULL; m1 = LIST_NEXT(m1, mtx_held), i++) {
843
844		MPASS(i < 200);
845		w1 = m1->mtx_witness;
846		if (isitmydescendant(w, w1)) {
847			mtx_exit(&w_mtx, MTX_SPIN);
848			if (blessed(w, w1))
849				goto out;
850			if (m1 == &Giant) {
851				if (w1->w_Giant_squawked)
852					goto out;
853				else
854					w1->w_Giant_squawked = 1;
855			} else {
856				if (w1->w_other_squawked)
857					goto out;
858				else
859					w1->w_other_squawked = 1;
860			}
861			printf("lock order reversal\n");
862			printf(" 1st %s last acquired @ %s:%d\n",
863			    w->w_description, w->w_file, w->w_line);
864			printf(" 2nd %p %s @ %s:%d\n",
865			    m1, w1->w_description, w1->w_file, w1->w_line);
866			printf(" 3rd %p %s @ %s:%d\n",
867			    m, w->w_description, file, line);
868#ifdef DDB
869			go_into_ddb = 1;
870#endif /* DDB */
871			goto out;
872		}
873	}
874	m1 = LIST_FIRST(&p->p_heldmtx);
875	if (!itismychild(m1->mtx_witness, w))
876		mtx_exit(&w_mtx, MTX_SPIN);
877
878out:
879#ifdef DDB
880	if (witness_ddb && go_into_ddb)
881		Debugger("witness_enter");
882#endif /* DDB */
883	w->w_file = file;
884	w->w_line = line;
885	m->mtx_line = line;
886	m->mtx_file = file;
887
888	/*
889	 * If this pays off it likely means that a mutex being witnessed
890	 * is acquired in hardclock. Put it in the ignore list. It is
891	 * likely not the mutex this assert fails on.
892	 */
893	MPASS(m->mtx_held.le_prev == NULL);
894	LIST_INSERT_HEAD(&p->p_heldmtx, (struct mtx*)m, mtx_held);
895}
896
897void
898witness_exit(struct mtx *m, int flags, const char *file, int line)
899{
900	struct witness *w;
901
902	w = m->mtx_witness;
903
904	if (flags & MTX_SPIN) {
905		if (!w->w_spin)
906			panic("mutex_exit: MTX_SPIN on MTX_DEF mutex %s @"
907			    " %s:%d", m->mtx_description, file, line);
908		if (m->mtx_recurse != 0)
909			return;
910		mtx_enter(&w_mtx, MTX_SPIN);
911		PCPU_SET(witness_spin_check, witness_spin_check & ~w->w_level);
912		mtx_exit(&w_mtx, MTX_SPIN);
913		return;
914	}
915	if (w->w_spin)
916		panic("mutex_exit: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
917		    m->mtx_description, file, line);
918
919	if (m->mtx_recurse != 0)
920		return;
921
922	if ((flags & MTX_NOSWITCH) == 0 && !mtx_legal2block() && !cold)
923		panic("switchable mtx_exit() of %s when not legal @ %s:%d",
924			    m->mtx_description, file, line);
925	LIST_REMOVE(m, mtx_held);
926	m->mtx_held.le_prev = NULL;
927}
928
929void
930witness_try_enter(struct mtx *m, int flags, const char *file, int line)
931{
932	struct proc *p;
933	struct witness *w = m->mtx_witness;
934
935	if (flags & MTX_SPIN) {
936		if (!w->w_spin)
937			panic("mutex_try_enter: "
938			    "MTX_SPIN on MTX_DEF mutex %s @ %s:%d",
939			    m->mtx_description, file, line);
940		if (m->mtx_recurse != 0)
941			return;
942		mtx_enter(&w_mtx, MTX_SPIN);
943		PCPU_SET(witness_spin_check, witness_spin_check | w->w_level);
944		mtx_exit(&w_mtx, MTX_SPIN);
945		return;
946	}
947
948	if (w->w_spin)
949		panic("mutex_try_enter: MTX_DEF on MTX_SPIN mutex %s @ %s:%d",
950		    m->mtx_description, file, line);
951
952	if (m->mtx_recurse != 0)
953		return;
954
955	w->w_file = file;
956	w->w_line = line;
957	m->mtx_line = line;
958	m->mtx_file = file;
959	p = CURPROC;
960	MPASS(m->mtx_held.le_prev == NULL);
961	LIST_INSERT_HEAD(&p->p_heldmtx, (struct mtx*)m, mtx_held);
962}
963
964void
965witness_display(void(*prnt)(const char *fmt, ...))
966{
967	struct witness *w, *w1;
968
969	witness_levelall();
970
971	for (w = w_all; w; w = w->w_next) {
972		if (w->w_file == NULL)
973			continue;
974		for (w1 = w_all; w1; w1 = w1->w_next) {
975			if (isitmychild(w1, w))
976				break;
977		}
978		if (w1 != NULL)
979			continue;
980		/*
981		 * This lock has no anscestors, display its descendants.
982		 */
983		witness_displaydescendants(prnt, w);
984	}
985	prnt("\nMutex which were never acquired\n");
986	for (w = w_all; w; w = w->w_next) {
987		if (w->w_file != NULL)
988			continue;
989		prnt("%s\n", w->w_description);
990	}
991}
992
993int
994witness_sleep(int check_only, struct mtx *mtx, const char *file, int line)
995{
996	struct mtx *m;
997	struct proc *p;
998	char **sleep;
999	int n = 0;
1000
1001	p = CURPROC;
1002	for ((m = LIST_FIRST(&p->p_heldmtx)); m != NULL;
1003	    m = LIST_NEXT(m, mtx_held)) {
1004		if (m == mtx)
1005			continue;
1006		for (sleep = sleep_list; *sleep!= NULL; sleep++)
1007			if (strcmp(m->mtx_description, *sleep) == 0)
1008				goto next;
1009		printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
1010			file, line, check_only ? "could sleep" : "sleeping",
1011			m->mtx_description,
1012			m->mtx_witness->w_file, m->mtx_witness->w_line);
1013		n++;
1014	next:
1015	}
1016#ifdef DDB
1017	if (witness_ddb && n)
1018		Debugger("witness_sleep");
1019#endif /* DDB */
1020	return (n);
1021}
1022
1023static struct witness *
1024enroll(const char *description, int flag)
1025{
1026	int i;
1027	struct witness *w, *w1;
1028	char **ignore;
1029	char **order;
1030
1031	if (!witness_watch)
1032		return (NULL);
1033	for (ignore = ignore_list; *ignore != NULL; ignore++)
1034		if (strcmp(description, *ignore) == 0)
1035			return (NULL);
1036
1037	if (w_inited == 0) {
1038		mtx_init(&w_mtx, "witness lock", MTX_COLD | MTX_DEF);
1039		for (i = 0; i < WITNESS_COUNT; i++) {
1040			w = &w_data[i];
1041			witness_free(w);
1042		}
1043		w_inited = 1;
1044		for (order = order_list; *order != NULL; order++) {
1045			w = enroll(*order, MTX_DEF);
1046			w->w_file = "order list";
1047			for (order++; *order != NULL; order++) {
1048				w1 = enroll(*order, MTX_DEF);
1049				w1->w_file = "order list";
1050				itismychild(w, w1);
1051				w = w1;
1052    	    	    	}
1053		}
1054	}
1055	if ((flag & MTX_SPIN) && witness_skipspin)
1056		return (NULL);
1057	mtx_enter(&w_mtx, MTX_SPIN);
1058	for (w = w_all; w; w = w->w_next) {
1059		if (strcmp(description, w->w_description) == 0) {
1060			mtx_exit(&w_mtx, MTX_SPIN);
1061			return (w);
1062		}
1063	}
1064	if ((w = witness_get()) == NULL)
1065		return (NULL);
1066	w->w_next = w_all;
1067	w_all = w;
1068	w->w_description = description;
1069	mtx_exit(&w_mtx, MTX_SPIN);
1070	if (flag & MTX_SPIN) {
1071		w->w_spin = 1;
1072
1073		i = 1;
1074		for (order = spin_order_list; *order != NULL; order++) {
1075			if (strcmp(description, *order) == 0)
1076				break;
1077			i <<= 1;
1078		}
1079		if (*order == NULL)
1080			panic("spin lock %s not in order list", description);
1081		w->w_level = i;
1082	}
1083	return (w);
1084}
1085
1086static int
1087itismychild(struct witness *parent, struct witness *child)
1088{
1089	static int recursed;
1090
1091	/*
1092	 * Insert "child" after "parent"
1093	 */
1094	while (parent->w_morechildren)
1095		parent = parent->w_morechildren;
1096
1097	if (parent->w_childcnt == WITNESS_NCHILDREN) {
1098		if ((parent->w_morechildren = witness_get()) == NULL)
1099			return (1);
1100		parent = parent->w_morechildren;
1101	}
1102	MPASS(child != NULL);
1103	parent->w_children[parent->w_childcnt++] = child;
1104	/*
1105	 * now prune whole tree
1106	 */
1107	if (recursed)
1108		return (0);
1109	recursed = 1;
1110	for (child = w_all; child != NULL; child = child->w_next) {
1111		for (parent = w_all; parent != NULL;
1112		    parent = parent->w_next) {
1113			if (!isitmychild(parent, child))
1114				continue;
1115			removechild(parent, child);
1116			if (isitmydescendant(parent, child))
1117				continue;
1118			itismychild(parent, child);
1119		}
1120	}
1121	recursed = 0;
1122	witness_levelall();
1123	return (0);
1124}
1125
1126static void
1127removechild(struct witness *parent, struct witness *child)
1128{
1129	struct witness *w, *w1;
1130	int i;
1131
1132	for (w = parent; w != NULL; w = w->w_morechildren)
1133		for (i = 0; i < w->w_childcnt; i++)
1134			if (w->w_children[i] == child)
1135				goto found;
1136	return;
1137found:
1138	for (w1 = w; w1->w_morechildren != NULL; w1 = w1->w_morechildren)
1139		continue;
1140	w->w_children[i] = w1->w_children[--w1->w_childcnt];
1141	MPASS(w->w_children[i] != NULL);
1142
1143	if (w1->w_childcnt != 0)
1144		return;
1145
1146	if (w1 == parent)
1147		return;
1148	for (w = parent; w->w_morechildren != w1; w = w->w_morechildren)
1149		continue;
1150	w->w_morechildren = 0;
1151	witness_free(w1);
1152}
1153
1154static int
1155isitmychild(struct witness *parent, struct witness *child)
1156{
1157	struct witness *w;
1158	int i;
1159
1160	for (w = parent; w != NULL; w = w->w_morechildren) {
1161		for (i = 0; i < w->w_childcnt; i++) {
1162			if (w->w_children[i] == child)
1163				return (1);
1164		}
1165	}
1166	return (0);
1167}
1168
1169static int
1170isitmydescendant(struct witness *parent, struct witness *child)
1171{
1172	struct witness *w;
1173	int i;
1174	int j;
1175
1176	for (j = 0, w = parent; w != NULL; w = w->w_morechildren, j++) {
1177		MPASS(j < 1000);
1178		for (i = 0; i < w->w_childcnt; i++) {
1179			if (w->w_children[i] == child)
1180				return (1);
1181		}
1182		for (i = 0; i < w->w_childcnt; i++) {
1183			if (isitmydescendant(w->w_children[i], child))
1184				return (1);
1185		}
1186	}
1187	return (0);
1188}
1189
1190void
1191witness_levelall (void)
1192{
1193	struct witness *w, *w1;
1194
1195	for (w = w_all; w; w = w->w_next)
1196		if (!w->w_spin)
1197			w->w_level = 0;
1198	for (w = w_all; w; w = w->w_next) {
1199		if (w->w_spin)
1200			continue;
1201		for (w1 = w_all; w1; w1 = w1->w_next) {
1202			if (isitmychild(w1, w))
1203				break;
1204		}
1205		if (w1 != NULL)
1206			continue;
1207		witness_leveldescendents(w, 0);
1208	}
1209}
1210
1211static void
1212witness_leveldescendents(struct witness *parent, int level)
1213{
1214	int i;
1215	struct witness *w;
1216
1217	if (parent->w_level < level)
1218		parent->w_level = level;
1219	level++;
1220	for (w = parent; w != NULL; w = w->w_morechildren)
1221		for (i = 0; i < w->w_childcnt; i++)
1222			witness_leveldescendents(w->w_children[i], level);
1223}
1224
1225static void
1226witness_displaydescendants(void(*prnt)(const char *fmt, ...),
1227			   struct witness *parent)
1228{
1229	struct witness *w;
1230	int i;
1231	int level = parent->w_level;
1232
1233	prnt("%d", level);
1234	if (level < 10)
1235		prnt(" ");
1236	for (i = 0; i < level; i++)
1237		prnt(" ");
1238	prnt("%s", parent->w_description);
1239	if (parent->w_file != NULL) {
1240		prnt(" -- last acquired @ %s", parent->w_file);
1241#ifndef W_USE_WHERE
1242		prnt(":%d", parent->w_line);
1243#endif
1244		prnt("\n");
1245	}
1246
1247	for (w = parent; w != NULL; w = w->w_morechildren)
1248		for (i = 0; i < w->w_childcnt; i++)
1249			    witness_displaydescendants(prnt, w->w_children[i]);
1250    }
1251
1252static int
1253dup_ok(struct witness *w)
1254{
1255	char **dup;
1256
1257	for (dup = dup_list; *dup!= NULL; dup++)
1258		if (strcmp(w->w_description, *dup) == 0)
1259			return (1);
1260	return (0);
1261}
1262
1263static int
1264blessed(struct witness *w1, struct witness *w2)
1265{
1266	int i;
1267	struct witness_blessed *b;
1268
1269	for (i = 0; i < blessed_count; i++) {
1270		b = &blessed_list[i];
1271		if (strcmp(w1->w_description, b->b_lock1) == 0) {
1272			if (strcmp(w2->w_description, b->b_lock2) == 0)
1273				return (1);
1274			continue;
1275		}
1276		if (strcmp(w1->w_description, b->b_lock2) == 0)
1277			if (strcmp(w2->w_description, b->b_lock1) == 0)
1278				return (1);
1279	}
1280	return (0);
1281}
1282
1283static struct witness *
1284witness_get()
1285{
1286	struct witness *w;
1287
1288	if ((w = w_free) == NULL) {
1289		witness_dead = 1;
1290		mtx_exit(&w_mtx, MTX_SPIN);
1291		printf("witness exhausted\n");
1292		return (NULL);
1293	}
1294	w_free = w->w_next;
1295	bzero(w, sizeof(*w));
1296	return (w);
1297}
1298
1299static void
1300witness_free(struct witness *w)
1301{
1302	w->w_next = w_free;
1303	w_free = w;
1304}
1305
1306void
1307witness_list(struct proc *p)
1308{
1309	struct mtx *m;
1310
1311	for ((m = LIST_FIRST(&p->p_heldmtx)); m != NULL;
1312	    m = LIST_NEXT(m, mtx_held)) {
1313		printf("\t\"%s\" (%p) locked at %s:%d\n",
1314		    m->mtx_description, m,
1315		    m->mtx_witness->w_file, m->mtx_witness->w_line);
1316	}
1317}
1318
1319void
1320witness_save(struct mtx *m, const char **filep, int *linep)
1321{
1322	*filep = m->mtx_witness->w_file;
1323	*linep = m->mtx_witness->w_line;
1324}
1325
1326void
1327witness_restore(struct mtx *m, const char *file, int line)
1328{
1329	m->mtx_witness->w_file = file;
1330	m->mtx_witness->w_line = line;
1331}
1332
1333#endif	/* (defined(MUTEX_DEBUG) && defined(WITNESS)) */
1334