subr_witness.c revision 83366
165557Sjasone/*-
265557Sjasone * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
365557Sjasone *
465557Sjasone * Redistribution and use in source and binary forms, with or without
565557Sjasone * modification, are permitted provided that the following conditions
665557Sjasone * are met:
765557Sjasone * 1. Redistributions of source code must retain the above copyright
865557Sjasone *    notice, this list of conditions and the following disclaimer.
965557Sjasone * 2. Redistributions in binary form must reproduce the above copyright
1065557Sjasone *    notice, this list of conditions and the following disclaimer in the
1165557Sjasone *    documentation and/or other materials provided with the distribution.
1265557Sjasone * 3. Berkeley Software Design Inc's name may not be used to endorse or
1365557Sjasone *    promote products derived from this software without specific prior
1465557Sjasone *    written permission.
1565557Sjasone *
1665557Sjasone * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
1765557Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1865557Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1965557Sjasone * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
2065557Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2165557Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2265557Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2365557Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2465557Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2565557Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2665557Sjasone * SUCH DAMAGE.
2765557Sjasone *
2865557Sjasone *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
2967352Sjhb *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
3065557Sjasone * $FreeBSD: head/sys/kern/subr_witness.c 83366 2001-09-12 08:38:13Z julian $
3165557Sjasone */
3265557Sjasone
3365557Sjasone/*
3474912Sjhb * Implementation of the `witness' lock verifier.  Originally implemented for
3574912Sjhb * mutexes in BSD/OS.  Extended to handle generic lock objects and lock
3674912Sjhb * classes in FreeBSD.
3772200Sbmilekic */
3872200Sbmilekic
3972200Sbmilekic/*
4065557Sjasone *	Main Entry: witness
4165557Sjasone *	Pronunciation: 'wit-n&s
4265557Sjasone *	Function: noun
4365557Sjasone *	Etymology: Middle English witnesse, from Old English witnes knowledge,
4465557Sjasone *	    testimony, witness, from 2wit
4565557Sjasone *	Date: before 12th century
4665557Sjasone *	1 : attestation of a fact or event : TESTIMONY
4765557Sjasone *	2 : one that gives evidence; specifically : one who testifies in
4865557Sjasone *	    a cause or before a judicial tribunal
4965557Sjasone *	3 : one asked to be present at a transaction so as to be able to
5065557Sjasone *	    testify to its having taken place
5165557Sjasone *	4 : one who has personal knowledge of something
5265557Sjasone *	5 a : something serving as evidence or proof : SIGN
5365557Sjasone *	  b : public affirmation by word or example of usually
5465557Sjasone *	      religious faith or conviction <the heroic witness to divine
5565557Sjasone *	      life -- Pilot>
5665557Sjasone *	6 capitalized : a member of the Jehovah's Witnesses
5765557Sjasone */
5865557Sjasone
5968790Sjhb#include "opt_ddb.h"
6067676Sjhb#include "opt_witness.h"
6167676Sjhb
6265557Sjasone#include <sys/param.h>
6367352Sjhb#include <sys/bus.h>
6467352Sjhb#include <sys/kernel.h>
6574912Sjhb#include <sys/ktr.h>
6674912Sjhb#include <sys/lock.h>
6767352Sjhb#include <sys/malloc.h>
6874912Sjhb#include <sys/mutex.h>
6965557Sjasone#include <sys/proc.h>
7067676Sjhb#include <sys/sysctl.h>
7165557Sjasone#include <sys/systm.h>
7265557Sjasone
7368790Sjhb#include <ddb/ddb.h>
7468790Sjhb
7574912Sjhb#define WITNESS_COUNT 200
7674912Sjhb#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
7765557Sjasone/*
7874912Sjhb * XXX: This is somewhat bogus, as we assume here that at most 1024 processes
7974912Sjhb * will hold LOCK_NCHILDREN * 2 locks.  We handle failure ok, and we should
8074912Sjhb * probably be safe for the most part, but it's still a SWAG.
8167352Sjhb */
8274912Sjhb#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
8371352Sjasone
8474912Sjhb#define	WITNESS_NCHILDREN 6
8571352Sjasone
8674912Sjhbstruct witness_child_list_entry;
8771352Sjasone
8874912Sjhbstruct witness {
8974912Sjhb	const	char *w_name;
9074912Sjhb	struct	lock_class *w_class;
9174912Sjhb	STAILQ_ENTRY(witness) w_list;		/* List of all witnesses. */
9274912Sjhb	STAILQ_ENTRY(witness) w_typelist;	/* Witnesses of a type. */
9374912Sjhb	struct	witness_child_list_entry *w_children;	/* Great evilness... */
9474912Sjhb	const	char *w_file;
9574912Sjhb	int	w_line;
9674912Sjhb	u_int	w_level;
9774912Sjhb	u_int	w_refcount;
9874912Sjhb	u_char	w_Giant_squawked:1;
9974912Sjhb	u_char	w_other_squawked:1;
10074912Sjhb	u_char	w_same_squawked:1;
10174912Sjhb};
10271352Sjasone
10374912Sjhbstruct witness_child_list_entry {
10474912Sjhb	struct	witness_child_list_entry *wcl_next;
10574912Sjhb	struct	witness *wcl_children[WITNESS_NCHILDREN];
10674912Sjhb	u_int	wcl_count;
10774912Sjhb};
10871352Sjasone
10974912SjhbSTAILQ_HEAD(witness_list, witness);
11071352Sjasone
11174912Sjhbstruct witness_blessed {
11274912Sjhb	const	char *b_lock1;
11374912Sjhb	const	char *b_lock2;
11474912Sjhb};
11571352Sjasone
11674912Sjhbstruct witness_order_list_entry {
11774912Sjhb	const	char *w_name;
11874912Sjhb	struct	lock_class *w_class;
11974912Sjhb};
12071352Sjasone
12174912Sjhbstatic struct	witness *enroll(const char *description,
12274912Sjhb				struct lock_class *lock_class);
12374912Sjhbstatic int	itismychild(struct witness *parent, struct witness *child);
12474912Sjhbstatic void	removechild(struct witness *parent, struct witness *child);
12574912Sjhbstatic int	isitmychild(struct witness *parent, struct witness *child);
12674912Sjhbstatic int	isitmydescendant(struct witness *parent, struct witness *child);
12774912Sjhbstatic int	dup_ok(struct witness *);
12874912Sjhbstatic int	blessed(struct witness *, struct witness *);
12974912Sjhbstatic void	witness_display_list(void(*prnt)(const char *fmt, ...),
13074912Sjhb				     struct witness_list *list);
13174912Sjhbstatic void	witness_displaydescendants(void(*)(const char *fmt, ...),
13274912Sjhb					   struct witness *);
13374912Sjhbstatic void	witness_leveldescendents(struct witness *parent, int level);
13474912Sjhbstatic void	witness_levelall(void);
13574912Sjhbstatic struct	witness *witness_get(void);
13674912Sjhbstatic void	witness_free(struct witness *m);
13774912Sjhbstatic struct	witness_child_list_entry *witness_child_get(void);
13874912Sjhbstatic void	witness_child_free(struct witness_child_list_entry *wcl);
13974912Sjhbstatic struct	lock_list_entry *witness_lock_list_get(void);
14074912Sjhbstatic void	witness_lock_list_free(struct lock_list_entry *lle);
14174912Sjhbstatic void	witness_display(void(*)(const char *fmt, ...));
14276272Sjhbstatic struct	lock_instance *find_instance(struct lock_list_entry *lock_list,
14376272Sjhb					     struct lock_object *lock);
14472200Sbmilekic
14574912SjhbMALLOC_DEFINE(M_WITNESS, "witness", "witness structure");
14672200Sbmilekic
14777843Speterstatic int witness_watch = 1;
14877900SpeterTUNABLE_INT("debug.witness_watch", &witness_watch);
14974912SjhbSYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, "");
15071352Sjasone
15167352Sjhb#ifdef DDB
15272200Sbmilekic/*
15367676Sjhb * When DDB is enabled and witness_ddb is set to 1, it will cause the system to
15465557Sjasone * drop into kdebug() when:
15565557Sjasone *	- a lock heirarchy violation occurs
15665557Sjasone *	- locks are held when going to sleep.
15765557Sjasone */
15867676Sjhb#ifdef WITNESS_DDB
15977843Speterint	witness_ddb = 1;
16067676Sjhb#else
16177843Speterint	witness_ddb = 0;
16265557Sjasone#endif
16377900SpeterTUNABLE_INT("debug.witness_ddb", &witness_ddb);
16467676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, "");
16567676Sjhb#endif /* DDB */
16665557Sjasone
16767676Sjhb#ifdef WITNESS_SKIPSPIN
16877843Speterint	witness_skipspin = 1;
16967676Sjhb#else
17077843Speterint	witness_skipspin = 0;
17165557Sjasone#endif
17277900SpeterTUNABLE_INT("debug.witness_skipspin", &witness_skipspin);
17367676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0,
17467676Sjhb    "");
17565557Sjasone
17674912Sjhbstatic struct mtx w_mtx;
17774912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
17874912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
17974912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
18074912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
18174912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL;
18274912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL;
18374912Sjhbstatic int witness_dead;	/* fatal error, probably no memory */
18465557Sjasone
18574912Sjhbstatic struct witness w_data[WITNESS_COUNT];
18674912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
18774912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
18865557Sjasone
18974912Sjhbstatic struct witness_order_list_entry order_lists[] = {
19074912Sjhb	{ "Giant", &lock_class_mtx_sleep },
19174912Sjhb	{ "proctree", &lock_class_sx },
19274912Sjhb	{ "allproc", &lock_class_sx },
19374912Sjhb	{ "process lock", &lock_class_mtx_sleep },
19474912Sjhb	{ "uidinfo hash", &lock_class_mtx_sleep },
19574912Sjhb	{ "uidinfo struct", &lock_class_mtx_sleep },
19674912Sjhb	{ NULL, NULL },
19775464Sjhb	/*
19875464Sjhb	 * spin locks
19975464Sjhb	 */
20072224Sjhb#if defined(__i386__) && defined (SMP)
20174912Sjhb	{ "com", &lock_class_mtx_spin },
20272224Sjhb#endif
20374912Sjhb	{ "sio", &lock_class_mtx_spin },
20472224Sjhb#ifdef __i386__
20574912Sjhb	{ "cy", &lock_class_mtx_spin },
20672224Sjhb#endif
20774912Sjhb	{ "ng_node", &lock_class_mtx_spin },
20874912Sjhb	{ "ng_worklist", &lock_class_mtx_spin },
20974912Sjhb	{ "ithread table lock", &lock_class_mtx_spin },
21074912Sjhb	{ "sched lock", &lock_class_mtx_spin },
21174912Sjhb	{ "callout", &lock_class_mtx_spin },
21265557Sjasone	/*
21365557Sjasone	 * leaf locks
21465557Sjasone	 */
21572224Sjhb#ifdef SMP
21675464Sjhb	{ "ap boot", &lock_class_mtx_spin },
21771576Sjasone#ifdef __i386__
21874912Sjhb	{ "imen", &lock_class_mtx_spin },
21971576Sjasone#endif
22074912Sjhb	{ "smp rendezvous", &lock_class_mtx_spin },
22172224Sjhb#endif
22278785Sjhb	{ "clk", &lock_class_mtx_spin },
22374912Sjhb	{ NULL, NULL },
22474912Sjhb	{ NULL, NULL }
22565557Sjasone};
22665557Sjasone
22774912Sjhbstatic const char *dup_list[] = {
22873912Sjhb	"process lock",
22965557Sjasone	NULL
23065557Sjasone};
23165557Sjasone
23265557Sjasone/*
23365557Sjasone * Pairs of locks which have been blessed
23465557Sjasone * Don't complain about order problems with blessed locks
23565557Sjasone */
23665856Sjhbstatic struct witness_blessed blessed_list[] = {
23765557Sjasone};
23872200Sbmilekicstatic int blessed_count =
23972200Sbmilekic	sizeof(blessed_list) / sizeof(struct witness_blessed);
24065557Sjasone
24174912Sjhb/*
24274912Sjhb * List of all locks in the system.
24374912Sjhb */
24474912SjhbSTAILQ_HEAD(, lock_object) all_locks = STAILQ_HEAD_INITIALIZER(all_locks);
24574912Sjhb
24674912Sjhbstatic struct mtx all_mtx = {
24774912Sjhb	{ &lock_class_mtx_sleep,	/* mtx_object.lo_class */
24874912Sjhb	  "All locks list",		/* mtx_object.lo_name */
24974912Sjhb	  LO_INITIALIZED,		/* mtx_object.lo_flags */
25074912Sjhb	  { NULL },			/* mtx_object.lo_list */
25174912Sjhb	  NULL },			/* mtx_object.lo_witness */
25274912Sjhb	MTX_UNOWNED, 0,			/* mtx_lock, mtx_recurse */
25374912Sjhb	0,				/* mtx_savecrit */
25474912Sjhb	TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked),
25574912Sjhb	{ NULL, NULL }			/* mtx_contested */
25674912Sjhb};
25774912Sjhb
25874912Sjhb/*
25974912Sjhb * This global is set to 0 once it becomes safe to use the witness code.
26074912Sjhb */
26174912Sjhbstatic int witness_cold = 1;
26274912Sjhb
26374912Sjhb/*
26474912Sjhb * Global variables for book keeping.
26574912Sjhb */
26674912Sjhbstatic int lock_cur_cnt;
26774912Sjhbstatic int lock_max_cnt;
26874912Sjhb
26974912Sjhb/*
27074912Sjhb * The WITNESS-enabled diagnostic code.
27174912Sjhb */
27271352Sjasonestatic void
27374912Sjhbwitness_initialize(void *dummy __unused)
27465557Sjasone{
27574912Sjhb	struct lock_object *lock;
27674912Sjhb	struct witness_order_list_entry *order;
27774912Sjhb	struct witness *w, *w1;
27874912Sjhb	int i;
27965557Sjasone
28074912Sjhb	/*
28174912Sjhb	 * We have to release Giant before initializing its witness
28274912Sjhb	 * structure so that WITNESS doesn't get confused.
28374912Sjhb	 */
28474912Sjhb	mtx_unlock(&Giant);
28574912Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
28674912Sjhb
28778871Sjhb	CTR0(KTR_WITNESS, __func__ ": initializing witness");
28874912Sjhb	STAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
28974912Sjhb	mtx_init(&w_mtx, "witness lock", MTX_SPIN | MTX_QUIET | MTX_NOWITNESS);
29074912Sjhb	for (i = 0; i < WITNESS_COUNT; i++)
29174912Sjhb		witness_free(&w_data[i]);
29274912Sjhb	for (i = 0; i < WITNESS_CHILDCOUNT; i++)
29374912Sjhb		witness_child_free(&w_childdata[i]);
29474912Sjhb	for (i = 0; i < LOCK_CHILDCOUNT; i++)
29574912Sjhb		witness_lock_list_free(&w_locklistdata[i]);
29674912Sjhb
29774912Sjhb	/* First add in all the specified order lists. */
29874912Sjhb	for (order = order_lists; order->w_name != NULL; order++) {
29974912Sjhb		w = enroll(order->w_name, order->w_class);
30075569Sjhb		if (w == NULL)
30175569Sjhb			continue;
30274912Sjhb		w->w_file = "order list";
30374912Sjhb		for (order++; order->w_name != NULL; order++) {
30474912Sjhb			w1 = enroll(order->w_name, order->w_class);
30575569Sjhb			if (w1 == NULL)
30675569Sjhb				continue;
30774912Sjhb			w1->w_file = "order list";
30874912Sjhb			itismychild(w, w1);
30974912Sjhb			w = w1;
31065557Sjasone		}
31165557Sjasone	}
31265557Sjasone
31374912Sjhb	/* Iterate through all locks and add them to witness. */
31474912Sjhb	mtx_lock(&all_mtx);
31574912Sjhb	STAILQ_FOREACH(lock, &all_locks, lo_list) {
31674912Sjhb		if (lock->lo_flags & LO_WITNESS)
31774912Sjhb			lock->lo_witness = enroll(lock->lo_name,
31874912Sjhb			    lock->lo_class);
31974912Sjhb		else
32074912Sjhb			lock->lo_witness = NULL;
32174912Sjhb	}
32274912Sjhb	mtx_unlock(&all_mtx);
32374912Sjhb
32474912Sjhb	/* Mark the witness code as being ready for use. */
32574912Sjhb	atomic_store_rel_int(&witness_cold, 0);
32674912Sjhb
32774912Sjhb	mtx_lock(&Giant);
32865557Sjasone}
32974912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
33065557Sjasone
33174912Sjhbvoid
33274912Sjhbwitness_init(struct lock_object *lock)
33374912Sjhb{
33474912Sjhb	struct lock_class *class;
33574912Sjhb
33674912Sjhb	class = lock->lo_class;
33774912Sjhb	if (lock->lo_flags & LO_INITIALIZED)
33882284Sjhb		panic("%s: lock (%s) %s is already initialized", __func__,
33974912Sjhb		    class->lc_name, lock->lo_name);
34074912Sjhb	if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
34174912Sjhb	    (class->lc_flags & LC_RECURSABLE) == 0)
34282284Sjhb		panic("%s: lock (%s) %s can not be recursable", __func__,
34374912Sjhb		    class->lc_name, lock->lo_name);
34474912Sjhb	if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
34574912Sjhb	    (class->lc_flags & LC_SLEEPABLE) == 0)
34682284Sjhb		panic("%s: lock (%s) %s can not be sleepable", __func__,
34774912Sjhb		    class->lc_name, lock->lo_name);
34882244Sjhb	if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
34982244Sjhb	    (class->lc_flags & LC_UPGRADABLE) == 0)
35082284Sjhb		panic("%s: lock (%s) %s can not be upgradable", __func__,
35182244Sjhb		    class->lc_name, lock->lo_name);
35282244Sjhb
35374912Sjhb	mtx_lock(&all_mtx);
35474912Sjhb	STAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
35574912Sjhb	lock->lo_flags |= LO_INITIALIZED;
35674912Sjhb	lock_cur_cnt++;
35774912Sjhb	if (lock_cur_cnt > lock_max_cnt)
35874912Sjhb		lock_max_cnt = lock_cur_cnt;
35974912Sjhb	mtx_unlock(&all_mtx);
36080747Sjhb	if (!witness_cold && !witness_dead && panicstr == NULL &&
36174912Sjhb	    (lock->lo_flags & LO_WITNESS) != 0)
36274912Sjhb		lock->lo_witness = enroll(lock->lo_name, class);
36374912Sjhb	else
36474912Sjhb		lock->lo_witness = NULL;
36574912Sjhb}
36674912Sjhb
36774912Sjhbvoid
36874912Sjhbwitness_destroy(struct lock_object *lock)
36974912Sjhb{
37075362Sjhb	struct witness *w;
37174912Sjhb
37274912Sjhb	if (witness_cold)
37374912Sjhb		panic("lock (%s) %s destroyed while witness_cold",
37474912Sjhb		    lock->lo_class->lc_name, lock->lo_name);
37574912Sjhb	if ((lock->lo_flags & LO_INITIALIZED) == 0)
37682284Sjhb		panic("%s: lock (%s) %s is not initialized", __func__,
37774912Sjhb		    lock->lo_class->lc_name, lock->lo_name);
37874912Sjhb
37976272Sjhb	/* XXX: need to verify that no one holds the lock */
38075362Sjhb	w = lock->lo_witness;
38175362Sjhb	if (w != NULL) {
38275362Sjhb		mtx_lock_spin(&w_mtx);
38375362Sjhb		w->w_refcount--;
38475362Sjhb		if (w->w_refcount == 0) {
38578871Sjhb			CTR1(KTR_WITNESS,
38678871Sjhb			    __func__ ": marking witness %s as dead", w->w_name);
38775362Sjhb			w->w_name = "(dead)";
38875362Sjhb			w->w_file = "(dead)";
38975362Sjhb			w->w_line = 0;
39075362Sjhb		}
39175362Sjhb		mtx_unlock_spin(&w_mtx);
39275362Sjhb	}
39375362Sjhb
39474912Sjhb	mtx_lock(&all_mtx);
39574912Sjhb	lock_cur_cnt--;
39674912Sjhb	STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list);
39780055Sjhb	lock->lo_flags &= ~LO_INITIALIZED;
39874912Sjhb	mtx_unlock(&all_mtx);
39974912Sjhb}
40074912Sjhb
40171352Sjasonestatic void
40274912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...),
40374912Sjhb		     struct witness_list *list)
40471352Sjasone{
40571352Sjasone	struct witness *w, *w1;
40674912Sjhb	int found;
40771352Sjasone
40874912Sjhb	STAILQ_FOREACH(w, list, w_typelist) {
40974912Sjhb		if (w->w_file == NULL)
41071352Sjasone			continue;
41174912Sjhb		found = 0;
41274912Sjhb		STAILQ_FOREACH(w1, list, w_typelist) {
41374912Sjhb			if (isitmychild(w1, w)) {
41474912Sjhb				found++;
41571352Sjasone				break;
41674912Sjhb			}
41771352Sjasone		}
41874912Sjhb		if (found)
41971352Sjasone			continue;
42071352Sjasone		/*
42171352Sjasone		 * This lock has no anscestors, display its descendants.
42271352Sjasone		 */
42371352Sjasone		witness_displaydescendants(prnt, w);
42471352Sjasone	}
42574912Sjhb}
42672224Sjhb
42774912Sjhbstatic void
42874912Sjhbwitness_display(void(*prnt)(const char *fmt, ...))
42974912Sjhb{
43074912Sjhb	struct witness *w;
43174912Sjhb
43282284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
43374912Sjhb	witness_levelall();
43474912Sjhb
43572224Sjhb	/*
43674930Sjhb	 * First, handle sleep locks which have been acquired at least
43774912Sjhb	 * once.
43874912Sjhb	 */
43974912Sjhb	prnt("Sleep locks:\n");
44074912Sjhb	witness_display_list(prnt, &w_sleep);
44174912Sjhb
44274912Sjhb	/*
44374930Sjhb	 * Now do spin locks which have been acquired at least once.
44472224Sjhb	 */
44574912Sjhb	prnt("\nSpin locks:\n");
44674912Sjhb	witness_display_list(prnt, &w_spin);
44772224Sjhb
44872224Sjhb	/*
44974930Sjhb	 * Finally, any locks which have not been acquired yet.
45072224Sjhb	 */
45174912Sjhb	prnt("\nLocks which were never acquired:\n");
45274912Sjhb	STAILQ_FOREACH(w, &w_all, w_list) {
45371352Sjasone		if (w->w_file != NULL)
45471352Sjasone			continue;
45574912Sjhb		prnt("%s\n", w->w_name);
45671352Sjasone	}
45771352Sjasone}
45871352Sjasone
45965557Sjasonevoid
46074912Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line)
46165557Sjasone{
46274912Sjhb	struct lock_list_entry **lock_list, *lle;
46376272Sjhb	struct lock_instance *lock1, *lock2;
46474912Sjhb	struct lock_class *class;
46565856Sjhb	struct witness *w, *w1;
46665557Sjasone	struct proc *p;
46783366Sjulian	struct thread *td;
46874912Sjhb	int i, j;
46967676Sjhb#ifdef DDB
47067676Sjhb	int go_into_ddb = 0;
47167676Sjhb#endif /* DDB */
47265557Sjasone
47374912Sjhb	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
47480747Sjhb	    panicstr != NULL)
47571320Sjasone		return;
47674912Sjhb	w = lock->lo_witness;
47774912Sjhb	class = lock->lo_class;
47883366Sjulian	td = curthread;
47983366Sjulian	p = td->td_proc;
48065557Sjasone
48174944Sjhb	/*
48274944Sjhb	 * We have to hold a spinlock to keep lock_list valid across the check
48374944Sjhb	 * in the LC_SLEEPLOCK case.  In the LC_SPINLOCK case, it is already
48474944Sjhb	 * protected by the spinlock we are currently performing the witness
48574944Sjhb	 * checks on, so it is ok to release the lock after performing this
48674944Sjhb	 * check.  All we have to protect is the LC_SLEEPLOCK case when no
48774944Sjhb	 * spinlocks are held as we may get preempted during this check and
48874944Sjhb	 * lock_list could end up pointing to some other CPU's spinlock list.
48974944Sjhb	 */
49074944Sjhb	mtx_lock_spin(&w_mtx);
49174912Sjhb	lock_list = PCPU_PTR(spinlocks);
49274912Sjhb	if (class->lc_flags & LC_SLEEPLOCK) {
49376772Sjhb		if (*lock_list != NULL && (flags & LOP_TRYLOCK) == 0) {
49474944Sjhb			mtx_unlock_spin(&w_mtx);
49574912Sjhb			panic("blockable sleep lock (%s) %s @ %s:%d",
49674912Sjhb			    class->lc_name, lock->lo_name, file, line);
49774944Sjhb		}
49883366Sjulian		lock_list = &td->td_sleeplocks;
49974912Sjhb	}
50074944Sjhb	mtx_unlock_spin(&w_mtx);
50165557Sjasone
50276772Sjhb	/*
50376772Sjhb	 * Try locks do not block if they fail to acquire the lock, thus
50476772Sjhb	 * there is no danger of deadlocks or of switching while holding a
50576772Sjhb	 * spin lock if we acquire a lock via a try operation.
50676772Sjhb	 */
50774912Sjhb	if (flags & LOP_TRYLOCK)
50865557Sjasone		goto out;
50965557Sjasone
51065557Sjasone	/*
51174912Sjhb	 * Is this the first lock acquired?  If so, then no order checking
51274912Sjhb	 * is needed.
51365557Sjasone	 */
51474912Sjhb	if (*lock_list == NULL)
51565557Sjasone		goto out;
51665557Sjasone
51774912Sjhb	/*
51876272Sjhb	 * Check to see if we are recursing on a lock we already own.
51976272Sjhb	 */
52076272Sjhb	lock1 = find_instance(*lock_list, lock);
52176272Sjhb	if (lock1 != NULL) {
52276272Sjhb		if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
52376272Sjhb		    (flags & LOP_EXCLUSIVE) == 0) {
52476272Sjhb			printf("shared lock of (%s) %s @ %s:%d\n",
52576272Sjhb			    class->lc_name, lock->lo_name, file, line);
52676272Sjhb			printf("while exclusively locked from %s:%d\n",
52776272Sjhb			    lock1->li_file, lock1->li_line);
52876272Sjhb			panic("share->excl");
52976272Sjhb		}
53076272Sjhb		if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
53176272Sjhb		    (flags & LOP_EXCLUSIVE) != 0) {
53276272Sjhb			printf("exclusive lock of (%s) %s @ %s:%d\n",
53376272Sjhb			    class->lc_name, lock->lo_name, file, line);
53476272Sjhb			printf("while share locked from %s:%d\n",
53576272Sjhb			    lock1->li_file, lock1->li_line);
53676272Sjhb			panic("excl->share");
53776272Sjhb		}
53876272Sjhb		lock1->li_flags++;
53976272Sjhb		if ((lock->lo_flags & LO_RECURSABLE) == 0) {
54076272Sjhb			printf(
54176272Sjhb			"recursed on non-recursive lock (%s) %s @ %s:%d\n",
54276272Sjhb			    class->lc_name, lock->lo_name, file, line);
54376272Sjhb			printf("first acquired @ %s:%d\n", lock1->li_file,
54476272Sjhb			    lock1->li_line);
54576272Sjhb			panic("recurse");
54676272Sjhb		}
54778871Sjhb		CTR3(KTR_WITNESS, __func__ ": pid %d recursed on %s r=%d",
54878785Sjhb		    curproc->p_pid, lock->lo_name,
54978785Sjhb		    lock1->li_flags & LI_RECURSEMASK);
55076272Sjhb		lock1->li_file = file;
55176272Sjhb		lock1->li_line = line;
55276272Sjhb		return;
55376272Sjhb	}
55476272Sjhb
55576272Sjhb	/*
55674912Sjhb	 * Check for duplicate locks of the same type.  Note that we only
55774912Sjhb	 * have to check for this on the last lock we just acquired.  Any
55874912Sjhb	 * other cases will be caught as lock order violations.
55974912Sjhb	 */
56076272Sjhb	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
56176272Sjhb	w1 = lock1->li_lock->lo_witness;
56274912Sjhb	if (w1 == w) {
56365557Sjasone		if (w->w_same_squawked || dup_ok(w))
56465557Sjasone			goto out;
56565557Sjasone		w->w_same_squawked = 1;
56675755Sjhb		printf("acquiring duplicate lock of same type: \"%s\"\n",
56774912Sjhb			lock->lo_name);
56876272Sjhb		printf(" 1st @ %s:%d\n", lock1->li_file, lock1->li_line);
56965557Sjasone		printf(" 2nd @ %s:%d\n", file, line);
57067676Sjhb#ifdef DDB
57167676Sjhb		go_into_ddb = 1;
57267676Sjhb#endif /* DDB */
57365557Sjasone		goto out;
57465557Sjasone	}
57565557Sjasone	MPASS(!mtx_owned(&w_mtx));
57674912Sjhb	mtx_lock_spin(&w_mtx);
57765557Sjasone	/*
57865557Sjasone	 * If we have a known higher number just say ok
57965557Sjasone	 */
58065557Sjasone	if (witness_watch > 1 && w->w_level > w1->w_level) {
58174912Sjhb		mtx_unlock_spin(&w_mtx);
58265557Sjasone		goto out;
58365557Sjasone	}
58474912Sjhb	if (isitmydescendant(w1, w)) {
58574912Sjhb		mtx_unlock_spin(&w_mtx);
58665557Sjasone		goto out;
58765557Sjasone	}
58874912Sjhb	for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
58974912Sjhb		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
59065557Sjasone
59174912Sjhb			MPASS(j < WITNESS_COUNT);
59276272Sjhb			lock1 = &lle->ll_children[i];
59376272Sjhb			w1 = lock1->li_lock->lo_witness;
59474912Sjhb
59574912Sjhb			/*
59674912Sjhb			 * If this lock doesn't undergo witness checking,
59774912Sjhb			 * then skip it.
59874912Sjhb			 */
59974912Sjhb			if (w1 == NULL) {
60076272Sjhb				KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
60174912Sjhb				    ("lock missing witness structure"));
60274912Sjhb				continue;
60374912Sjhb			}
60476272Sjhb			/*
60576272Sjhb			 * If we are locking Giant and we slept with this
60676272Sjhb			 * lock, then skip it.
60776272Sjhb			 */
60876272Sjhb			if ((lock1->li_flags & LI_SLEPT) != 0 &&
60976272Sjhb			    lock == &Giant.mtx_object)
61076272Sjhb				continue;
61174912Sjhb			if (!isitmydescendant(w, w1))
61274912Sjhb				continue;
61374912Sjhb			/*
61474912Sjhb			 * We have a lock order violation, check to see if it
61574912Sjhb			 * is allowed or has already been yelled about.
61674912Sjhb			 */
61774912Sjhb			mtx_unlock_spin(&w_mtx);
61865557Sjasone			if (blessed(w, w1))
61965557Sjasone				goto out;
62076272Sjhb			if (lock1->li_lock == &Giant.mtx_object) {
62165557Sjasone				if (w1->w_Giant_squawked)
62265557Sjasone					goto out;
62365557Sjasone				else
62465557Sjasone					w1->w_Giant_squawked = 1;
62565557Sjasone			} else {
62665557Sjasone				if (w1->w_other_squawked)
62765557Sjasone					goto out;
62865557Sjasone				else
62965557Sjasone					w1->w_other_squawked = 1;
63065557Sjasone			}
63174912Sjhb			/*
63274912Sjhb			 * Ok, yell about it.
63374912Sjhb			 */
63465557Sjasone			printf("lock order reversal\n");
63574912Sjhb			/*
63674912Sjhb			 * Try to locate an earlier lock with
63774912Sjhb			 * witness w in our list.
63874912Sjhb			 */
63974912Sjhb			do {
64076272Sjhb				lock2 = &lle->ll_children[i];
64176272Sjhb				MPASS(lock2->li_lock != NULL);
64276272Sjhb				if (lock2->li_lock->lo_witness == w)
64374912Sjhb					break;
64474912Sjhb				i--;
64574912Sjhb				if (i == 0 && lle->ll_next != NULL) {
64674912Sjhb					lle = lle->ll_next;
64774912Sjhb					i = lle->ll_count - 1;
64874912Sjhb					MPASS(i != 0);
64974912Sjhb				}
65074912Sjhb			} while (i >= 0);
65176272Sjhb			if (i < 0) {
65276272Sjhb				printf(" 1st %p %s @ %s:%d\n", lock1->li_lock,
65376272Sjhb				    lock1->li_lock->lo_name, lock1->li_file,
65476272Sjhb				    lock1->li_line);
65576272Sjhb				printf(" 2nd %p %s @ %s:%d\n", lock,
65676272Sjhb				    lock->lo_name, file, line);
65776272Sjhb			} else {
65876272Sjhb				printf(" 1st %p %s @ %s:%d\n", lock2->li_lock,
65976272Sjhb				    lock2->li_lock->lo_name, lock2->li_file,
66076272Sjhb				    lock2->li_line);
66176272Sjhb				printf(" 2nd %p %s @ %s:%d\n", lock1->li_lock,
66276272Sjhb				    lock1->li_lock->lo_name, lock1->li_file,
66376272Sjhb				    lock1->li_line);
66476272Sjhb				printf(" 3rd %p %s @ %s:%d\n", lock,
66576272Sjhb				    lock->lo_name, file, line);
66676272Sjhb			}
66767676Sjhb#ifdef DDB
66867676Sjhb			go_into_ddb = 1;
66967676Sjhb#endif /* DDB */
67065557Sjasone			goto out;
67165557Sjasone		}
67265557Sjasone	}
67376272Sjhb	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
67478871Sjhb	/*
67578871Sjhb	 * Don't build a new relationship if we are locking Giant just
67678871Sjhb	 * after waking up and the previous lock in the list was acquired
67778871Sjhb	 * prior to blocking.
67878871Sjhb	 */
67978871Sjhb	if (lock == &Giant.mtx_object && (lock1->li_flags & LI_SLEPT) != 0)
68074912Sjhb		mtx_unlock_spin(&w_mtx);
68178871Sjhb	else {
68278871Sjhb		CTR2(KTR_WITNESS, __func__ ": adding %s as a child of %s",
68378871Sjhb		    lock->lo_name, lock1->li_lock->lo_name);
68478871Sjhb		if (!itismychild(lock1->li_lock->lo_witness, w))
68578871Sjhb			mtx_unlock_spin(&w_mtx);
68678871Sjhb	}
68765557Sjasone
68865557Sjasoneout:
68967676Sjhb#ifdef DDB
69067676Sjhb	if (witness_ddb && go_into_ddb)
69175711Sjhb		Debugger(__func__);
69267676Sjhb#endif /* DDB */
69365557Sjasone	w->w_file = file;
69465557Sjasone	w->w_line = line;
69574912Sjhb
69674912Sjhb	lle = *lock_list;
69776272Sjhb	if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
69878785Sjhb		lle = witness_lock_list_get();
69978785Sjhb		if (lle == NULL)
70065557Sjasone			return;
70178785Sjhb		lle->ll_next = *lock_list;
70278871Sjhb		CTR2(KTR_WITNESS, __func__ ": pid %d added lle %p",
70378785Sjhb		    curproc->p_pid, lle);
70478785Sjhb		*lock_list = lle;
70565557Sjasone	}
70676272Sjhb	lock1 = &lle->ll_children[lle->ll_count++];
70776272Sjhb	lock1->li_lock = lock;
70876272Sjhb	lock1->li_line = line;
70976272Sjhb	lock1->li_file = file;
71076272Sjhb	if ((flags & LOP_EXCLUSIVE) != 0)
71176272Sjhb		lock1->li_flags = LI_EXCLUSIVE;
71276272Sjhb	else
71376272Sjhb		lock1->li_flags = 0;
71478871Sjhb	CTR3(KTR_WITNESS, __func__ ": pid %d added %s as lle[%d]",
71578785Sjhb	    curproc->p_pid, lock->lo_name, lle->ll_count - 1);
71665557Sjasone}
71765557Sjasone
71865557Sjasonevoid
71982244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
72082244Sjhb{
72182244Sjhb	struct lock_instance *instance;
72282244Sjhb	struct lock_class *class;
72382244Sjhb
72482284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
72582244Sjhb	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
72682244Sjhb		return;
72782244Sjhb	class = lock->lo_class;
72882244Sjhb	if ((lock->lo_flags & LO_UPGRADABLE) == 0)
72982244Sjhb		panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
73082244Sjhb		    class->lc_name, lock->lo_name, file, line);
73182244Sjhb	if ((flags & LOP_TRYLOCK) == 0)
73282244Sjhb		panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
73382244Sjhb		    lock->lo_name, file, line);
73482244Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
73582244Sjhb		panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
73682244Sjhb		    class->lc_name, lock->lo_name, file, line);
73783366Sjulian	instance = find_instance(curthread->td_sleeplocks, lock);
73882244Sjhb	if (instance == NULL)
73982244Sjhb		panic("upgrade of unlocked lock (%s) %s @ %s:%d",
74082244Sjhb		    class->lc_name, lock->lo_name, file, line);
74182244Sjhb	if ((instance->li_flags & LI_EXCLUSIVE) != 0)
74282244Sjhb		panic("upgrade of exclusive lock (%s) %s @ %s:%d",
74382244Sjhb		    class->lc_name, lock->lo_name, file, line);
74482244Sjhb	if ((instance->li_flags & LI_RECURSEMASK) != 0)
74582244Sjhb		panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
74682244Sjhb		    class->lc_name, lock->lo_name,
74782244Sjhb		    instance->li_flags & LI_RECURSEMASK, file, line);
74882244Sjhb	instance->li_flags |= LI_EXCLUSIVE;
74982244Sjhb}
75082244Sjhb
75182244Sjhbvoid
75282244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file,
75382244Sjhb    int line)
75482244Sjhb{
75582244Sjhb	struct lock_instance *instance;
75682244Sjhb	struct lock_class *class;
75782244Sjhb
75882284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
75982244Sjhb	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
76082244Sjhb		return;
76182244Sjhb	class = lock->lo_class;
76282244Sjhb	if ((lock->lo_flags & LO_UPGRADABLE) == 0)
76382244Sjhb		panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
76482244Sjhb		    class->lc_name, lock->lo_name, file, line);
76582244Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
76682244Sjhb		panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
76782244Sjhb		    class->lc_name, lock->lo_name, file, line);
76883366Sjulian	instance = find_instance(curthread->td_sleeplocks, lock);
76982244Sjhb	if (instance == NULL)
77082244Sjhb		panic("downgrade of unlocked lock (%s) %s @ %s:%d",
77182244Sjhb		    class->lc_name, lock->lo_name, file, line);
77282244Sjhb	if ((instance->li_flags & LI_EXCLUSIVE) == 0)
77382244Sjhb		panic("downgrade of shared lock (%s) %s @ %s:%d",
77482244Sjhb		    class->lc_name, lock->lo_name, file, line);
77582244Sjhb	if ((instance->li_flags & LI_RECURSEMASK) != 0)
77682244Sjhb		panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
77782244Sjhb		    class->lc_name, lock->lo_name,
77882244Sjhb		    instance->li_flags & LI_RECURSEMASK, file, line);
77982244Sjhb	instance->li_flags &= ~LI_EXCLUSIVE;
78082244Sjhb}
78182244Sjhb
78282244Sjhbvoid
78374912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line)
78465557Sjasone{
78574912Sjhb	struct lock_list_entry **lock_list, *lle;
78676272Sjhb	struct lock_instance *instance;
78774912Sjhb	struct lock_class *class;
78874016Sjhb	struct proc *p;
78983366Sjulian	struct thread *td;
79078785Sjhb	critical_t s;
79174912Sjhb	int i, j;
79265557Sjasone
79374912Sjhb	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
79480747Sjhb	    panicstr != NULL)
79571352Sjasone		return;
79683366Sjulian	td = curthread;
79783366Sjulian	p = td->td_proc;
79874912Sjhb	class = lock->lo_class;
79976272Sjhb	if (class->lc_flags & LC_SLEEPLOCK)
80083366Sjulian		lock_list = &td->td_sleeplocks;
80176272Sjhb	else
80274912Sjhb		lock_list = PCPU_PTR(spinlocks);
80374912Sjhb	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
80476272Sjhb		for (i = 0; i < (*lock_list)->ll_count; i++) {
80576272Sjhb			instance = &(*lock_list)->ll_children[i];
80676272Sjhb			if (instance->li_lock == lock) {
80776272Sjhb				if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
80876272Sjhb				    (flags & LOP_EXCLUSIVE) == 0) {
80976272Sjhb					printf(
81076272Sjhb					"shared unlock of (%s) %s @ %s:%d\n",
81176272Sjhb					    class->lc_name, lock->lo_name,
81276272Sjhb					    file, line);
81376272Sjhb					printf(
81476272Sjhb					"while exclusively locked from %s:%d\n",
81576272Sjhb					    instance->li_file,
81676272Sjhb					    instance->li_line);
81776272Sjhb					panic("excl->ushare");
81876272Sjhb				}
81976272Sjhb				if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
82076272Sjhb				    (flags & LOP_EXCLUSIVE) != 0) {
82176272Sjhb					printf(
82276272Sjhb					"exclusive unlock of (%s) %s @ %s:%d\n",
82376272Sjhb					    class->lc_name, lock->lo_name,
82476272Sjhb					    file, line);
82576272Sjhb					printf(
82676272Sjhb					"while share locked from %s:%d\n",
82776272Sjhb					    instance->li_file,
82876272Sjhb					    instance->li_line);
82976272Sjhb					panic("share->uexcl");
83076272Sjhb				}
83176272Sjhb				/* If we are recursed, unrecurse. */
83276272Sjhb				if ((instance->li_flags & LI_RECURSEMASK) > 0) {
83378785Sjhb					CTR3(KTR_WITNESS,
83478871Sjhb				    __func__ ": pid %d unrecursed on %s r=%d",
83578785Sjhb					    curproc->p_pid,
83678785Sjhb					    instance->li_lock->lo_name,
83778785Sjhb					    instance->li_flags);
83876272Sjhb					instance->li_flags--;
83976272Sjhb					goto out;
84076272Sjhb				}
84178785Sjhb				s = critical_enter();
84278785Sjhb				CTR3(KTR_WITNESS,
84378871Sjhb				    __func__ ": pid %d removed %s from lle[%d]",
84478785Sjhb				    curproc->p_pid, instance->li_lock->lo_name,
84578785Sjhb				    (*lock_list)->ll_count - 1);
84674912Sjhb				(*lock_list)->ll_count--;
84774912Sjhb				for (j = i; j < (*lock_list)->ll_count; j++)
84874912Sjhb					(*lock_list)->ll_children[j] =
84974912Sjhb					    (*lock_list)->ll_children[j + 1];
85078785Sjhb				critical_exit(s);
85174912Sjhb				if ((*lock_list)->ll_count == 0) {
85274912Sjhb					lle = *lock_list;
85374912Sjhb					*lock_list = lle->ll_next;
85478785Sjhb					CTR2(KTR_WITNESS,
85578871Sjhb					    __func__ ": pid %d removed lle %p",
85678785Sjhb					    curproc->p_pid, lle);
85774912Sjhb					witness_lock_list_free(lle);
85874912Sjhb				}
85976272Sjhb				goto out;
86074912Sjhb			}
86176272Sjhb		}
86276272Sjhb	panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
86376272Sjhb	    file, line);
86476272Sjhbout:
86576272Sjhb	/*
86676272Sjhb	 * We don't need to protect this PCPU_GET() here against preemption
86776272Sjhb	 * because if we hold any spinlocks then we are already protected,
86876272Sjhb	 * and if we don't we will get NULL if we hold no spinlocks even if
86976272Sjhb	 * we switch CPU's while reading it.
87076272Sjhb	 */
87176272Sjhb	if (class->lc_flags & LC_SLEEPLOCK) {
87276272Sjhb		if ((flags & LOP_NOSWITCH) == 0 && PCPU_GET(spinlocks) != NULL)
87376272Sjhb			panic("switchable sleep unlock (%s) %s @ %s:%d",
87476272Sjhb			    class->lc_name, lock->lo_name, file, line);
87576272Sjhb	}
87665557Sjasone}
87765557Sjasone
87874912Sjhb/*
87974912Sjhb * Warn if any held locks are not sleepable.  Note that Giant and the lock
88074912Sjhb * passed in are both special cases since they are both released during the
88174912Sjhb * sleep process and aren't actually held while the process is asleep.
88274912Sjhb */
88365557Sjasoneint
88474912Sjhbwitness_sleep(int check_only, struct lock_object *lock, const char *file,
88574912Sjhb	      int line)
88665557Sjasone{
88774912Sjhb	struct lock_list_entry **lock_list, *lle;
88876272Sjhb	struct lock_instance *lock1;
88965557Sjasone	struct proc *p;
89083366Sjulian	struct thread *td;
89174912Sjhb	critical_t savecrit;
89274912Sjhb	int i, n;
89365557Sjasone
89480747Sjhb	if (witness_dead || panicstr != NULL)
89574912Sjhb		return (0);
89682284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
89774912Sjhb	n = 0;
89874912Sjhb	/*
89974912Sjhb	 * Preemption bad because we need PCPU_PTR(spinlocks) to not change.
90074912Sjhb	 */
90174912Sjhb	savecrit = critical_enter();
90283366Sjulian	td = curthread;
90383366Sjulian	p = td->td_proc;
90483366Sjulian	lock_list = &td->td_sleeplocks;
90574912Sjhbagain:
90674912Sjhb	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
90774912Sjhb		for (i = lle->ll_count - 1; i >= 0; i--) {
90876272Sjhb			lock1 = &lle->ll_children[i];
90976272Sjhb			if (lock1->li_lock == lock ||
91076272Sjhb			    lock1->li_lock == &Giant.mtx_object)
91174912Sjhb				continue;
91276272Sjhb			if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) {
91378785Sjhb				if (check_only == 0) {
91478785Sjhb					CTR3(KTR_WITNESS,
91578871Sjhb				    "pid %d: sleeping with lock (%s) %s held",
91678785Sjhb					    curproc->p_pid,
91778785Sjhb					    lock1->li_lock->lo_class->lc_name,
91878785Sjhb					    lock1->li_lock->lo_name);
91976272Sjhb					lock1->li_flags |= LI_SLEPT;
92078785Sjhb				}
92176272Sjhb				continue;
92276272Sjhb			}
92374912Sjhb			n++;
92474912Sjhb			printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
92574912Sjhb			    file, line, check_only ? "could sleep" : "sleeping",
92676272Sjhb			    lock1->li_lock->lo_name, lock1->li_file,
92776272Sjhb			    lock1->li_line);
92874912Sjhb		}
92983366Sjulian	if (lock_list == &td->td_sleeplocks) {
93074912Sjhb		lock_list = PCPU_PTR(spinlocks);
93174912Sjhb		goto again;
93265557Sjasone	}
93367676Sjhb#ifdef DDB
93467676Sjhb	if (witness_ddb && n)
93575711Sjhb		Debugger(__func__);
93667676Sjhb#endif /* DDB */
93774912Sjhb	critical_exit(savecrit);
93865557Sjasone	return (n);
93965557Sjasone}
94065557Sjasone
94165856Sjhbstatic struct witness *
94274912Sjhbenroll(const char *description, struct lock_class *lock_class)
94365557Sjasone{
94474912Sjhb	struct witness *w;
94565557Sjasone
94680747Sjhb	if (!witness_watch || witness_dead || panicstr != NULL)
94765557Sjasone		return (NULL);
94874912Sjhb	if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
94965557Sjasone		return (NULL);
95074912Sjhb	mtx_lock_spin(&w_mtx);
95174912Sjhb	STAILQ_FOREACH(w, &w_all, w_list) {
95274912Sjhb		if (strcmp(description, w->w_name) == 0) {
95375362Sjhb			w->w_refcount++;
95474912Sjhb			mtx_unlock_spin(&w_mtx);
95574912Sjhb			if (lock_class != w->w_class)
95674912Sjhb				panic(
95774912Sjhb				"lock (%s) %s does not match earlier (%s) lock",
95874912Sjhb				    description, lock_class->lc_name,
95974912Sjhb				    w->w_class->lc_name);
96065557Sjasone			return (w);
96165557Sjasone		}
96265557Sjasone	}
96374912Sjhb	/*
96474912Sjhb	 * This isn't quite right, as witness_cold is still 0 while we
96574912Sjhb	 * enroll all the locks initialized before witness_initialize().
96674912Sjhb	 */
96775364Sbp	if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
96875364Sbp		mtx_unlock_spin(&w_mtx);
96974912Sjhb		panic("spin lock %s not in order list", description);
97075364Sbp	}
97165557Sjasone	if ((w = witness_get()) == NULL)
97265557Sjasone		return (NULL);
97374912Sjhb	w->w_name = description;
97474912Sjhb	w->w_class = lock_class;
97575362Sjhb	w->w_refcount = 1;
97674912Sjhb	STAILQ_INSERT_HEAD(&w_all, w, w_list);
97774912Sjhb	if (lock_class->lc_flags & LC_SPINLOCK)
97874912Sjhb		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
97974912Sjhb	else if (lock_class->lc_flags & LC_SLEEPLOCK)
98074912Sjhb		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
98175364Sbp	else {
98275364Sbp		mtx_unlock_spin(&w_mtx);
98374912Sjhb		panic("lock class %s is not sleep or spin",
98474912Sjhb		    lock_class->lc_name);
98575364Sbp	}
98674912Sjhb	mtx_unlock_spin(&w_mtx);
98765557Sjasone	return (w);
98865557Sjasone}
98965557Sjasone
99065557Sjasonestatic int
99165856Sjhbitismychild(struct witness *parent, struct witness *child)
99265557Sjasone{
99365557Sjasone	static int recursed;
99474912Sjhb	struct witness_child_list_entry **wcl;
99574912Sjhb	struct witness_list *list;
99665557Sjasone
99774912Sjhb	MPASS(child != NULL && parent != NULL);
99874912Sjhb	if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
99974912Sjhb	    (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
100074912Sjhb		panic(
100174912Sjhb		"%s: parent (%s) and child (%s) are not the same lock type",
100274912Sjhb		    __func__, parent->w_class->lc_name,
100374912Sjhb		    child->w_class->lc_name);
100474912Sjhb
100565557Sjasone	/*
100665557Sjasone	 * Insert "child" after "parent"
100765557Sjasone	 */
100874912Sjhb	wcl = &parent->w_children;
100974912Sjhb	while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
101074912Sjhb		wcl = &(*wcl)->wcl_next;
101174912Sjhb	if (*wcl == NULL) {
101274912Sjhb		*wcl = witness_child_get();
101374912Sjhb		if (*wcl == NULL)
101465557Sjasone			return (1);
101565557Sjasone	}
101674912Sjhb	(*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
101774912Sjhb
101865557Sjasone	/*
101974912Sjhb	 * Now prune whole tree.  We look for cases where a lock is now
102074912Sjhb	 * both a descendant and a direct child of a given lock.  In that
102174912Sjhb	 * case, we want to remove the direct child link from the tree.
102265557Sjasone	 */
102365557Sjasone	if (recursed)
102465557Sjasone		return (0);
102565557Sjasone	recursed = 1;
102674912Sjhb	if (parent->w_class->lc_flags & LC_SLEEPLOCK)
102774912Sjhb		list = &w_sleep;
102874912Sjhb	else
102974912Sjhb		list = &w_spin;
103074912Sjhb	STAILQ_FOREACH(child, list, w_typelist) {
103174912Sjhb		STAILQ_FOREACH(parent, list, w_typelist) {
103265557Sjasone			if (!isitmychild(parent, child))
103365557Sjasone				continue;
103465557Sjasone			removechild(parent, child);
103565557Sjasone			if (isitmydescendant(parent, child))
103665557Sjasone				continue;
103765557Sjasone			itismychild(parent, child);
103865557Sjasone		}
103965557Sjasone	}
104065557Sjasone	recursed = 0;
104165557Sjasone	witness_levelall();
104265557Sjasone	return (0);
104365557Sjasone}
104465557Sjasone
104565557Sjasonestatic void
104665856Sjhbremovechild(struct witness *parent, struct witness *child)
104765557Sjasone{
104874912Sjhb	struct witness_child_list_entry **wcl, *wcl1;
104965557Sjasone	int i;
105065557Sjasone
105174912Sjhb	for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
105274912Sjhb		for (i = 0; i < (*wcl)->wcl_count; i++)
105374912Sjhb			if ((*wcl)->wcl_children[i] == child)
105465557Sjasone				goto found;
105565557Sjasone	return;
105665557Sjasonefound:
105774912Sjhb	(*wcl)->wcl_count--;
105874912Sjhb	if ((*wcl)->wcl_count > i)
105974912Sjhb		(*wcl)->wcl_children[i] =
106074912Sjhb		    (*wcl)->wcl_children[(*wcl)->wcl_count];
106174912Sjhb	MPASS((*wcl)->wcl_children[i] != NULL);
106274912Sjhb	if ((*wcl)->wcl_count != 0)
106365557Sjasone		return;
106474912Sjhb	wcl1 = *wcl;
106574912Sjhb	*wcl = wcl1->wcl_next;
106674912Sjhb	witness_child_free(wcl1);
106765557Sjasone}
106865557Sjasone
106965557Sjasonestatic int
107065856Sjhbisitmychild(struct witness *parent, struct witness *child)
107165557Sjasone{
107274912Sjhb	struct witness_child_list_entry *wcl;
107365557Sjasone	int i;
107465557Sjasone
107574912Sjhb	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
107674912Sjhb		for (i = 0; i < wcl->wcl_count; i++) {
107774912Sjhb			if (wcl->wcl_children[i] == child)
107865557Sjasone				return (1);
107965557Sjasone		}
108065557Sjasone	}
108165557Sjasone	return (0);
108265557Sjasone}
108365557Sjasone
108465557Sjasonestatic int
108565856Sjhbisitmydescendant(struct witness *parent, struct witness *child)
108665557Sjasone{
108774912Sjhb	struct witness_child_list_entry *wcl;
108874912Sjhb	int i, j;
108965557Sjasone
109074912Sjhb	if (isitmychild(parent, child))
109174912Sjhb		return (1);
109274912Sjhb	j = 0;
109374912Sjhb	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
109467352Sjhb		MPASS(j < 1000);
109574912Sjhb		for (i = 0; i < wcl->wcl_count; i++) {
109674912Sjhb			if (isitmydescendant(wcl->wcl_children[i], child))
109765557Sjasone				return (1);
109865557Sjasone		}
109974912Sjhb		j++;
110065557Sjasone	}
110165557Sjasone	return (0);
110265557Sjasone}
110365557Sjasone
110465557Sjasonevoid
110565557Sjasonewitness_levelall (void)
110665557Sjasone{
110774912Sjhb	struct witness_list *list;
110865856Sjhb	struct witness *w, *w1;
110965557Sjasone
111074912Sjhb	/*
111174912Sjhb	 * First clear all levels.
111274912Sjhb	 */
111374912Sjhb	STAILQ_FOREACH(w, &w_all, w_list) {
111474912Sjhb		w->w_level = 0;
111574912Sjhb	}
111674912Sjhb
111774912Sjhb	/*
111874912Sjhb	 * Look for locks with no parent and level all their descendants.
111974912Sjhb	 */
112074912Sjhb	STAILQ_FOREACH(w, &w_all, w_list) {
112174912Sjhb		/*
112274912Sjhb		 * This is just an optimization, technically we could get
112374912Sjhb		 * away just walking the all list each time.
112474912Sjhb		 */
112574912Sjhb		if (w->w_class->lc_flags & LC_SLEEPLOCK)
112674912Sjhb			list = &w_sleep;
112774912Sjhb		else
112874912Sjhb			list = &w_spin;
112974912Sjhb		STAILQ_FOREACH(w1, list, w_typelist) {
113065557Sjasone			if (isitmychild(w1, w))
113174912Sjhb				goto skip;
113265557Sjasone		}
113365557Sjasone		witness_leveldescendents(w, 0);
113474912Sjhb	skip:
113565557Sjasone	}
113665557Sjasone}
113765557Sjasone
113865557Sjasonestatic void
113965856Sjhbwitness_leveldescendents(struct witness *parent, int level)
114065557Sjasone{
114174912Sjhb	struct witness_child_list_entry *wcl;
114265557Sjasone	int i;
114365557Sjasone
114465557Sjasone	if (parent->w_level < level)
114565557Sjasone		parent->w_level = level;
114665557Sjasone	level++;
114774912Sjhb	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
114874912Sjhb		for (i = 0; i < wcl->wcl_count; i++)
114974912Sjhb			witness_leveldescendents(wcl->wcl_children[i], level);
115065557Sjasone}
115165557Sjasone
115265557Sjasonestatic void
115365856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...),
115465856Sjhb			   struct witness *parent)
115565557Sjasone{
115674912Sjhb	struct witness_child_list_entry *wcl;
115774912Sjhb	int i, level;
115865557Sjasone
115974912Sjhb	level =  parent->w_level;
116074912Sjhb	prnt("%-2d", level);
116165557Sjasone	for (i = 0; i < level; i++)
116265557Sjasone		prnt(" ");
116374912Sjhb	prnt("%s", parent->w_name);
116472224Sjhb	if (parent->w_file != NULL)
116572224Sjhb		prnt(" -- last acquired @ %s:%d\n", parent->w_file,
116672224Sjhb		    parent->w_line);
116774912Sjhb	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
116874912Sjhb		for (i = 0; i < wcl->wcl_count; i++)
116974912Sjhb			    witness_displaydescendants(prnt,
117074912Sjhb				wcl->wcl_children[i]);
117174912Sjhb}
117265557Sjasone
117365557Sjasonestatic int
117465856Sjhbdup_ok(struct witness *w)
117565557Sjasone{
117674912Sjhb	const char **dup;
117765557Sjasone
117874912Sjhb	for (dup = dup_list; *dup != NULL; dup++)
117974912Sjhb		if (strcmp(w->w_name, *dup) == 0)
118065557Sjasone			return (1);
118165557Sjasone	return (0);
118265557Sjasone}
118365557Sjasone
118465557Sjasonestatic int
118565856Sjhbblessed(struct witness *w1, struct witness *w2)
118665557Sjasone{
118765557Sjasone	int i;
118865856Sjhb	struct witness_blessed *b;
118965557Sjasone
119065557Sjasone	for (i = 0; i < blessed_count; i++) {
119165557Sjasone		b = &blessed_list[i];
119274912Sjhb		if (strcmp(w1->w_name, b->b_lock1) == 0) {
119374912Sjhb			if (strcmp(w2->w_name, b->b_lock2) == 0)
119465557Sjasone				return (1);
119565557Sjasone			continue;
119665557Sjasone		}
119774912Sjhb		if (strcmp(w1->w_name, b->b_lock2) == 0)
119874912Sjhb			if (strcmp(w2->w_name, b->b_lock1) == 0)
119965557Sjasone				return (1);
120065557Sjasone	}
120165557Sjasone	return (0);
120265557Sjasone}
120365557Sjasone
120465856Sjhbstatic struct witness *
120574912Sjhbwitness_get(void)
120665557Sjasone{
120765856Sjhb	struct witness *w;
120865557Sjasone
120976481Sjhb	if (witness_dead) {
121076481Sjhb		mtx_unlock_spin(&w_mtx);
121176481Sjhb		return (NULL);
121276481Sjhb	}
121374912Sjhb	if (STAILQ_EMPTY(&w_free)) {
121465557Sjasone		witness_dead = 1;
121574912Sjhb		mtx_unlock_spin(&w_mtx);
121674912Sjhb		printf("%s: witness exhausted\n", __func__);
121765557Sjasone		return (NULL);
121865557Sjasone	}
121974912Sjhb	w = STAILQ_FIRST(&w_free);
122074912Sjhb	STAILQ_REMOVE_HEAD(&w_free, w_list);
122165856Sjhb	bzero(w, sizeof(*w));
122265557Sjasone	return (w);
122365557Sjasone}
122465557Sjasone
122565557Sjasonestatic void
122665856Sjhbwitness_free(struct witness *w)
122765557Sjasone{
122874912Sjhb
122974912Sjhb	STAILQ_INSERT_HEAD(&w_free, w, w_list);
123065557Sjasone}
123165557Sjasone
123274912Sjhbstatic struct witness_child_list_entry *
123374912Sjhbwitness_child_get(void)
123465557Sjasone{
123574912Sjhb	struct witness_child_list_entry *wcl;
123665557Sjasone
123776481Sjhb	if (witness_dead) {
123876481Sjhb		mtx_unlock_spin(&w_mtx);
123976481Sjhb		return (NULL);
124076481Sjhb	}
124174912Sjhb	wcl = w_child_free;
124274912Sjhb	if (wcl == NULL) {
124374912Sjhb		witness_dead = 1;
124474912Sjhb		mtx_unlock_spin(&w_mtx);
124574912Sjhb		printf("%s: witness exhausted\n", __func__);
124674912Sjhb		return (NULL);
124765557Sjasone	}
124874912Sjhb	w_child_free = wcl->wcl_next;
124974912Sjhb	bzero(wcl, sizeof(*wcl));
125074912Sjhb	return (wcl);
125174912Sjhb}
125269881Sjake
125374912Sjhbstatic void
125474912Sjhbwitness_child_free(struct witness_child_list_entry *wcl)
125574912Sjhb{
125674912Sjhb
125774912Sjhb	wcl->wcl_next = w_child_free;
125874912Sjhb	w_child_free = wcl;
125965557Sjasone}
126065557Sjasone
126174912Sjhbstatic struct lock_list_entry *
126274912Sjhbwitness_lock_list_get(void)
126374912Sjhb{
126474912Sjhb	struct lock_list_entry *lle;
126571709Sjhb
126676481Sjhb	if (witness_dead)
126776481Sjhb		return (NULL);
126874912Sjhb	mtx_lock_spin(&w_mtx);
126974912Sjhb	lle = w_lock_list_free;
127074912Sjhb	if (lle == NULL) {
127174912Sjhb		witness_dead = 1;
127274912Sjhb		mtx_unlock_spin(&w_mtx);
127374912Sjhb		printf("%s: witness exhausted\n", __func__);
127474912Sjhb		return (NULL);
127574912Sjhb	}
127674912Sjhb	w_lock_list_free = lle->ll_next;
127774912Sjhb	mtx_unlock_spin(&w_mtx);
127874912Sjhb	bzero(lle, sizeof(*lle));
127974912Sjhb	return (lle);
128074912Sjhb}
128174912Sjhb
128274912Sjhbstatic void
128374912Sjhbwitness_lock_list_free(struct lock_list_entry *lle)
128471709Sjhb{
128571709Sjhb
128674912Sjhb	mtx_lock_spin(&w_mtx);
128774912Sjhb	lle->ll_next = w_lock_list_free;
128874912Sjhb	w_lock_list_free = lle;
128974912Sjhb	mtx_unlock_spin(&w_mtx);
129071709Sjhb}
129171709Sjhb
129276272Sjhbstatic struct lock_instance *
129376272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
129476272Sjhb{
129576272Sjhb	struct lock_list_entry *lle;
129676272Sjhb	struct lock_instance *instance;
129776272Sjhb	int i;
129876272Sjhb
129976272Sjhb	for (lle = lock_list; lle != NULL; lle = lle->ll_next)
130076272Sjhb		for (i = lle->ll_count - 1; i >= 0; i--) {
130176272Sjhb			instance = &lle->ll_children[i];
130276272Sjhb			if (instance->li_lock == lock)
130376272Sjhb				return (instance);
130476272Sjhb		}
130576272Sjhb	return (NULL);
130676272Sjhb}
130776272Sjhb
130874912Sjhbint
130975273Sjhbwitness_list_locks(struct lock_list_entry **lock_list)
131072224Sjhb{
131175273Sjhb	struct lock_list_entry *lle;
131276272Sjhb	struct lock_instance *instance;
131374912Sjhb	struct lock_object *lock;
131474912Sjhb	int i, nheld;
131572224Sjhb
131674912Sjhb	nheld = 0;
131774912Sjhb	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
131874912Sjhb		for (i = lle->ll_count - 1; i >= 0; i--) {
131976272Sjhb			instance = &lle->ll_children[i];
132076272Sjhb			lock = instance->li_lock;
132176272Sjhb			printf("%s (%s) %s (%p) locked @ %s:%d\n",
132276272Sjhb			    (instance->li_flags & LI_EXCLUSIVE) != 0 ?
132376272Sjhb			    "exclusive" : "shared",
132474912Sjhb			    lock->lo_class->lc_name, lock->lo_name, lock,
132576272Sjhb			    instance->li_file, instance->li_line);
132674912Sjhb			nheld++;
132774912Sjhb		}
132875273Sjhb	return (nheld);
132975273Sjhb}
133075273Sjhb
133175273Sjhb/*
133283366Sjulian * Calling this on td != curthread is bad unless we are in ddb.
133375273Sjhb */
133475273Sjhbint
133583366Sjulianwitness_list(struct thread *td)
133675273Sjhb{
133775273Sjhb	critical_t savecrit;
133875273Sjhb	int nheld;
133975273Sjhb
134081489Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
134181489Sjhb#ifdef DDB
134283366Sjulian	KASSERT(td == curthread || db_active,
134383366Sjulian	    ("%s: td != curthread and we aren't in the debugger", __func__));
134476481Sjhb	if (!db_active && witness_dead)
134576481Sjhb		return (0);
134681489Sjhb#else
134783366Sjulian	KASSERT(td == curthread, ("%s: p != curthread", __func__));
134881489Sjhb	if (witness_dead)
134981489Sjhb		return (0);
135081489Sjhb#endif
135183366Sjulian	nheld = witness_list_locks(&td->td_sleeplocks);
135275273Sjhb
135374912Sjhb	/*
135483366Sjulian	 * We only handle spinlocks if td == curthread.  This is somewhat broken
135574912Sjhb	 * if p is currently executing on some other CPU and holds spin locks
135675273Sjhb	 * as we won't display those locks.  If we had a MI way of getting
135775273Sjhb	 * the per-cpu data for a given cpu then we could use p->p_oncpu to
135875273Sjhb	 * get the list of spinlocks for this process and "fix" this.
135974912Sjhb	 */
136083366Sjulian	if (td == curthread) {
136175273Sjhb		/*
136275273Sjhb		 * Preemption bad because we need PCPU_PTR(spinlocks) to not
136375273Sjhb		 * change.
136475273Sjhb		 */
136575273Sjhb		savecrit = critical_enter();
136675273Sjhb		nheld += witness_list_locks(PCPU_PTR(spinlocks));
136775273Sjhb		critical_exit(savecrit);
136874912Sjhb	}
136974912Sjhb	return (nheld);
137072224Sjhb}
137171709Sjhb
137265557Sjasonevoid
137374912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep)
137465557Sjasone{
137576272Sjhb	struct lock_instance *instance;
137671320Sjasone
137782284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
137880747Sjhb	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
137971352Sjasone		return;
138082243Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
138182243Sjhb		panic("%s: lock (%s) %s is not a sleep lock", __func__,
138282243Sjhb		    lock->lo_class->lc_name, lock->lo_name);
138383366Sjulian	instance = find_instance(curthread->td_sleeplocks, lock);
138482243Sjhb	if (instance == NULL)
138582243Sjhb		panic("%s: lock (%s) %s not locked", __func__,
138682243Sjhb		    lock->lo_class->lc_name, lock->lo_name);
138776272Sjhb	*filep = instance->li_file;
138876272Sjhb	*linep = instance->li_line;
138965557Sjasone}
139065557Sjasone
139165557Sjasonevoid
139274912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line)
139365557Sjasone{
139476272Sjhb	struct lock_instance *instance;
139571320Sjasone
139682284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
139780747Sjhb	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
139871352Sjasone		return;
139982243Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
140082243Sjhb		panic("%s: lock (%s) %s is not a sleep lock", __func__,
140182243Sjhb		    lock->lo_class->lc_name, lock->lo_name);
140283366Sjulian	instance = find_instance(curthread->td_sleeplocks, lock);
140382243Sjhb	if (instance == NULL)
140482243Sjhb		panic("%s: lock (%s) %s not locked", __func__,
140582243Sjhb		    lock->lo_class->lc_name, lock->lo_name);
140674912Sjhb	lock->lo_witness->w_file = file;
140774912Sjhb	lock->lo_witness->w_line = line;
140876272Sjhb	instance->li_file = file;
140976272Sjhb	instance->li_line = line;
141065557Sjasone}
141165557Sjasone
141278871Sjhbvoid
141378871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line)
141478871Sjhb{
141578871Sjhb#ifdef INVARIANT_SUPPORT
141678871Sjhb	struct lock_instance *instance;
141778871Sjhb
141880747Sjhb	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
141978941Sjhb		return;
142078871Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0)
142183366Sjulian		instance = find_instance(curthread->td_sleeplocks, lock);
142278871Sjhb	else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0)
142378871Sjhb		instance = find_instance(PCPU_GET(spinlocks), lock);
142478871Sjhb	else
142578871Sjhb		panic("Lock (%s) %s is not sleep or spin!",
142678871Sjhb		    lock->lo_class->lc_name, lock->lo_name);
142778871Sjhb	switch (flags) {
142878871Sjhb	case LA_UNLOCKED:
142978871Sjhb		if (instance != NULL)
143078871Sjhb			panic("Lock (%s) %s locked @ %s:%d.",
143178871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
143278871Sjhb		break;
143378871Sjhb	case LA_LOCKED:
143478871Sjhb	case LA_LOCKED | LA_RECURSED:
143578871Sjhb	case LA_LOCKED | LA_NOTRECURSED:
143678871Sjhb	case LA_SLOCKED:
143778871Sjhb	case LA_SLOCKED | LA_RECURSED:
143878871Sjhb	case LA_SLOCKED | LA_NOTRECURSED:
143978871Sjhb	case LA_XLOCKED:
144078871Sjhb	case LA_XLOCKED | LA_RECURSED:
144178871Sjhb	case LA_XLOCKED | LA_NOTRECURSED:
144278871Sjhb		if (instance == NULL)
144378871Sjhb			panic("Lock (%s) %s not locked @ %s:%d.",
144478871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
144578871Sjhb		if ((flags & LA_XLOCKED) != 0 &&
144678871Sjhb		    (instance->li_flags & LI_EXCLUSIVE) == 0)
144778871Sjhb			panic("Lock (%s) %s not exclusively locked @ %s:%d.",
144878871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
144978871Sjhb		if ((flags & LA_SLOCKED) != 0 &&
145078871Sjhb		    (instance->li_flags & LI_EXCLUSIVE) != 0)
145178871Sjhb			panic("Lock (%s) %s exclusively locked @ %s:%d.",
145278871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
145378871Sjhb		if ((flags & LA_RECURSED) != 0 &&
145478871Sjhb		    (instance->li_flags & LI_RECURSEMASK) == 0)
145578871Sjhb			panic("Lock (%s) %s not recursed @ %s:%d.",
145678871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
145778871Sjhb		if ((flags & LA_NOTRECURSED) != 0 &&
145878871Sjhb		    (instance->li_flags & LI_RECURSEMASK) != 0)
145978871Sjhb			panic("Lock (%s) %s recursed @ %s:%d.",
146078871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
146178871Sjhb		break;
146278871Sjhb	default:
146378871Sjhb		panic("Invalid lock assertion at %s:%d.", file, line);
146478871Sjhb
146578871Sjhb	}
146678871Sjhb#endif	/* INVARIANT_SUPPORT */
146778871Sjhb}
146878871Sjhb
146974912Sjhb#ifdef DDB
147074912Sjhb
147174930SjhbDB_SHOW_COMMAND(locks, db_witness_list)
147274912Sjhb{
147383366Sjulian	struct thread *td;
147483366Sjulian	pid_t pid;
147575273Sjhb	struct proc *p;
147674912Sjhb
147775273Sjhb	if (have_addr) {
147875273Sjhb		pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
147975273Sjhb		    ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
148075273Sjhb		    ((addr >> 16) % 16) * 10000;
148175273Sjhb		/* sx_slock(&allproc_lock); */
148283366Sjulian		FOREACH_PROC_IN_SYSTEM(p) {
148375273Sjhb			if (p->p_pid == pid)
148475273Sjhb				break;
148575273Sjhb		}
148675273Sjhb		/* sx_sunlock(&allproc_lock); */
148775273Sjhb		if (p == NULL) {
148875273Sjhb			db_printf("pid %d not found\n", pid);
148975273Sjhb			return;
149075273Sjhb		}
149183366Sjulian		td = &p->p_thread; /* XXXKSE */
149283366Sjulian	} else {
149383366Sjulian		td = curthread;
149483366Sjulian	}
149583366Sjulian	witness_list(td);
149674912Sjhb}
149774912Sjhb
149874912SjhbDB_SHOW_COMMAND(witness, db_witness_display)
149974912Sjhb{
150074912Sjhb
150174912Sjhb	witness_display(db_printf);
150274912Sjhb}
150374912Sjhb#endif
1504