subr_witness.c revision 127323
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 */
3165557Sjasone
3265557Sjasone/*
3374912Sjhb * Implementation of the `witness' lock verifier.  Originally implemented for
3474912Sjhb * mutexes in BSD/OS.  Extended to handle generic lock objects and lock
3574912Sjhb * classes in FreeBSD.
3672200Sbmilekic */
3772200Sbmilekic
3872200Sbmilekic/*
3965557Sjasone *	Main Entry: witness
4065557Sjasone *	Pronunciation: 'wit-n&s
4165557Sjasone *	Function: noun
4265557Sjasone *	Etymology: Middle English witnesse, from Old English witnes knowledge,
4365557Sjasone *	    testimony, witness, from 2wit
4465557Sjasone *	Date: before 12th century
4565557Sjasone *	1 : attestation of a fact or event : TESTIMONY
4665557Sjasone *	2 : one that gives evidence; specifically : one who testifies in
4765557Sjasone *	    a cause or before a judicial tribunal
4865557Sjasone *	3 : one asked to be present at a transaction so as to be able to
4965557Sjasone *	    testify to its having taken place
5065557Sjasone *	4 : one who has personal knowledge of something
5165557Sjasone *	5 a : something serving as evidence or proof : SIGN
5265557Sjasone *	  b : public affirmation by word or example of usually
5365557Sjasone *	      religious faith or conviction <the heroic witness to divine
5465557Sjasone *	      life -- Pilot>
5565557Sjasone *	6 capitalized : a member of the Jehovah's Witnesses
5665557Sjasone */
5765557Sjasone
58111881Sjhb/*
59111881Sjhb * Special rules concerning Giant and lock orders:
60111881Sjhb *
61111881Sjhb * 1) Giant must be acquired before any other mutexes.  Stated another way,
62111881Sjhb *    no other mutex may be held when Giant is acquired.
63111881Sjhb *
64111881Sjhb * 2) Giant must be released when blocking on a sleepable lock.
65111881Sjhb *
66111881Sjhb * This rule is less obvious, but is a result of Giant providing the same
67111881Sjhb * semantics as spl().  Basically, when a thread sleeps, it must release
68111881Sjhb * Giant.  When a thread blocks on a sleepable lock, it sleeps.  Hence rule
69111881Sjhb * 2).
70111881Sjhb *
71111881Sjhb * 3) Giant may be acquired before or after sleepable locks.
72111881Sjhb *
73111881Sjhb * This rule is also not quite as obvious.  Giant may be acquired after
74111881Sjhb * a sleepable lock because it is a non-sleepable lock and non-sleepable
75111881Sjhb * locks may always be acquired while holding a sleepable lock.  The second
76111881Sjhb * case, Giant before a sleepable lock, follows from rule 2) above.  Suppose
77111881Sjhb * you have two threads T1 and T2 and a sleepable lock X.  Suppose that T1
78111881Sjhb * acquires X and blocks on Giant.  Then suppose that T2 acquires Giant and
79111881Sjhb * blocks on X.  When T2 blocks on X, T2 will release Giant allowing T1 to
80111881Sjhb * execute.  Thus, acquiring Giant both before and after a sleepable lock
81111881Sjhb * will not result in a lock order reversal.
82111881Sjhb */
83111881Sjhb
84116182Sobrien#include <sys/cdefs.h>
85116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_witness.c 127323 2004-03-23 00:32:27Z alfred $");
86116182Sobrien
8768790Sjhb#include "opt_ddb.h"
8867676Sjhb#include "opt_witness.h"
8967676Sjhb
9065557Sjasone#include <sys/param.h>
9167352Sjhb#include <sys/bus.h>
9267352Sjhb#include <sys/kernel.h>
9374912Sjhb#include <sys/ktr.h>
9474912Sjhb#include <sys/lock.h>
9567352Sjhb#include <sys/malloc.h>
9674912Sjhb#include <sys/mutex.h>
9765557Sjasone#include <sys/proc.h>
9867676Sjhb#include <sys/sysctl.h>
9965557Sjasone#include <sys/systm.h>
10065557Sjasone
10168790Sjhb#include <ddb/ddb.h>
10268790Sjhb
103111881Sjhb#include <machine/stdarg.h>
104111881Sjhb
105105508Sphk/* Define this to check for blessed mutexes */
106105508Sphk#undef BLESSING
107105508Sphk
10874912Sjhb#define WITNESS_COUNT 200
10974912Sjhb#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
11065557Sjasone/*
11183798Sjhb * XXX: This is somewhat bogus, as we assume here that at most 1024 threads
11274912Sjhb * will hold LOCK_NCHILDREN * 2 locks.  We handle failure ok, and we should
11374912Sjhb * probably be safe for the most part, but it's still a SWAG.
11467352Sjhb */
11574912Sjhb#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
11671352Sjasone
11774912Sjhb#define	WITNESS_NCHILDREN 6
11871352Sjasone
11974912Sjhbstruct witness_child_list_entry;
12071352Sjasone
12174912Sjhbstruct witness {
12274912Sjhb	const	char *w_name;
12374912Sjhb	struct	lock_class *w_class;
12474912Sjhb	STAILQ_ENTRY(witness) w_list;		/* List of all witnesses. */
12574912Sjhb	STAILQ_ENTRY(witness) w_typelist;	/* Witnesses of a type. */
12674912Sjhb	struct	witness_child_list_entry *w_children;	/* Great evilness... */
12774912Sjhb	const	char *w_file;
12874912Sjhb	int	w_line;
12974912Sjhb	u_int	w_level;
13074912Sjhb	u_int	w_refcount;
13174912Sjhb	u_char	w_Giant_squawked:1;
13274912Sjhb	u_char	w_other_squawked:1;
13374912Sjhb	u_char	w_same_squawked:1;
134112118Sjhb	u_char	w_displayed:1;
13574912Sjhb};
13671352Sjasone
13774912Sjhbstruct witness_child_list_entry {
13874912Sjhb	struct	witness_child_list_entry *wcl_next;
13974912Sjhb	struct	witness *wcl_children[WITNESS_NCHILDREN];
14074912Sjhb	u_int	wcl_count;
14174912Sjhb};
14271352Sjasone
14374912SjhbSTAILQ_HEAD(witness_list, witness);
14471352Sjasone
145105508Sphk#ifdef BLESSING
14674912Sjhbstruct witness_blessed {
14774912Sjhb	const	char *b_lock1;
14874912Sjhb	const	char *b_lock2;
14974912Sjhb};
150105508Sphk#endif
15171352Sjasone
15274912Sjhbstruct witness_order_list_entry {
15374912Sjhb	const	char *w_name;
15474912Sjhb	struct	lock_class *w_class;
15574912Sjhb};
15671352Sjasone
157112117Sjhb#ifdef BLESSING
158112117Sjhbstatic int	blessed(struct witness *, struct witness *);
159112117Sjhb#endif
160112117Sjhbstatic int	depart(struct witness *w);
16174912Sjhbstatic struct	witness *enroll(const char *description,
16274912Sjhb				struct lock_class *lock_class);
163112117Sjhbstatic int	insertchild(struct witness *parent, struct witness *child);
164112117Sjhbstatic int	isitmychild(struct witness *parent, struct witness *child);
165112117Sjhbstatic int	isitmydescendant(struct witness *parent, struct witness *child);
16674912Sjhbstatic int	itismychild(struct witness *parent, struct witness *child);
167112117Sjhbstatic int	rebalancetree(struct witness_list *list);
16874912Sjhbstatic void	removechild(struct witness *parent, struct witness *child);
169112117Sjhbstatic int	reparentchildren(struct witness *newparent,
170112117Sjhb		    struct witness *oldparent);
171112562Sjhbstatic int	sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
17274912Sjhbstatic void	witness_displaydescendants(void(*)(const char *fmt, ...),
173112118Sjhb					   struct witness *, int indent);
174112116Sjhbstatic const char *fixup_filename(const char *file);
17574912Sjhbstatic void	witness_leveldescendents(struct witness *parent, int level);
17674912Sjhbstatic void	witness_levelall(void);
17774912Sjhbstatic struct	witness *witness_get(void);
17874912Sjhbstatic void	witness_free(struct witness *m);
17974912Sjhbstatic struct	witness_child_list_entry *witness_child_get(void);
18074912Sjhbstatic void	witness_child_free(struct witness_child_list_entry *wcl);
18174912Sjhbstatic struct	lock_list_entry *witness_lock_list_get(void);
18274912Sjhbstatic void	witness_lock_list_free(struct lock_list_entry *lle);
18376272Sjhbstatic struct	lock_instance *find_instance(struct lock_list_entry *lock_list,
18476272Sjhb					     struct lock_object *lock);
185111881Sjhbstatic void	witness_list_lock(struct lock_instance *instance);
186112115Sjhb#ifdef DDB
187112061Sjhbstatic void	witness_list(struct thread *td);
188100011Smpstatic void	witness_display_list(void(*prnt)(const char *fmt, ...),
189100011Smp				     struct witness_list *list);
190100011Smpstatic void	witness_display(void(*)(const char *fmt, ...));
191100011Smp#endif
19272200Sbmilekic
19374912SjhbMALLOC_DEFINE(M_WITNESS, "witness", "witness structure");
19472200Sbmilekic
195112562Sjhb/*
196112562Sjhb * If set to 0, witness is disabled.  If set to 1, witness performs full lock
197112562Sjhb * order checking for all locks.  If set to 2 or higher, then witness skips
198112562Sjhb * the full lock order check if the lock being acquired is at a higher level
199112562Sjhb * (i.e. farther down in the tree) than the current lock.  This last mode is
200112562Sjhb * somewhat experimental and not considered fully safe.  At runtime, this
201112562Sjhb * value may be set to 0 to turn off witness.  witness is not allowed be
202112562Sjhb * turned on once it is turned off, however.
203112562Sjhb */
20477843Speterstatic int witness_watch = 1;
20577900SpeterTUNABLE_INT("debug.witness_watch", &witness_watch);
206112562SjhbSYSCTL_PROC(_debug, OID_AUTO, witness_watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
207112562Sjhb    sysctl_debug_witness_watch, "I", "witness is watching lock operations");
20871352Sjasone
20967352Sjhb#ifdef DDB
21072200Sbmilekic/*
21167676Sjhb * When DDB is enabled and witness_ddb is set to 1, it will cause the system to
21265557Sjasone * drop into kdebug() when:
21365557Sjasone *	- a lock heirarchy violation occurs
21465557Sjasone *	- locks are held when going to sleep.
21565557Sjasone */
21667676Sjhb#ifdef WITNESS_DDB
21777843Speterint	witness_ddb = 1;
21867676Sjhb#else
21977843Speterint	witness_ddb = 0;
22065557Sjasone#endif
22177900SpeterTUNABLE_INT("debug.witness_ddb", &witness_ddb);
22267676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, "");
223110779Speter
224110779Speter/*
225110779Speter * When DDB is enabled and witness_trace is set to 1, it will cause the system
226110779Speter * to print a stack trace:
227110779Speter *	- a lock heirarchy violation occurs
228110779Speter *	- locks are held when going to sleep.
229110779Speter */
230110779Speterint	witness_trace = 1;
231110779SpeterTUNABLE_INT("debug.witness_trace", &witness_trace);
232110779SpeterSYSCTL_INT(_debug, OID_AUTO, witness_trace, CTLFLAG_RW, &witness_trace, 0, "");
23367676Sjhb#endif /* DDB */
23465557Sjasone
23567676Sjhb#ifdef WITNESS_SKIPSPIN
23677843Speterint	witness_skipspin = 1;
23767676Sjhb#else
23877843Speterint	witness_skipspin = 0;
23965557Sjasone#endif
24077900SpeterTUNABLE_INT("debug.witness_skipspin", &witness_skipspin);
241121307SsilbySYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RDTUN, &witness_skipspin, 0,
24267676Sjhb    "");
24365557Sjasone
24474912Sjhbstatic struct mtx w_mtx;
24574912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
24674912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
24774912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
24874912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
24974912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL;
25074912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL;
25165557Sjasone
25274912Sjhbstatic struct witness w_data[WITNESS_COUNT];
25374912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
25474912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
25565557Sjasone
25674912Sjhbstatic struct witness_order_list_entry order_lists[] = {
25774912Sjhb	{ "proctree", &lock_class_sx },
25874912Sjhb	{ "allproc", &lock_class_sx },
259111951Sjhb	{ "Giant", &lock_class_mtx_sleep },
260108184Skris	{ "filedesc structure", &lock_class_mtx_sleep },
261108184Skris	{ "pipe mutex", &lock_class_mtx_sleep },
26296122Salfred	{ "sigio lock", &lock_class_mtx_sleep },
26391140Stanimura	{ "process group", &lock_class_mtx_sleep },
26474912Sjhb	{ "process lock", &lock_class_mtx_sleep },
26591140Stanimura	{ "session", &lock_class_mtx_sleep },
26674912Sjhb	{ "uidinfo hash", &lock_class_mtx_sleep },
26774912Sjhb	{ "uidinfo struct", &lock_class_mtx_sleep },
268113275Smike	{ "allprison", &lock_class_mtx_sleep },
26974912Sjhb	{ NULL, NULL },
27075464Sjhb	/*
27175464Sjhb	 * spin locks
27275464Sjhb	 */
27384331Sjhb#ifdef SMP
27484331Sjhb	{ "ap boot", &lock_class_mtx_spin },
27572224Sjhb#endif
27674912Sjhb	{ "sio", &lock_class_mtx_spin },
27772224Sjhb#ifdef __i386__
27874912Sjhb	{ "cy", &lock_class_mtx_spin },
27972224Sjhb#endif
280124972Sru	{ "uart_hwmtx", &lock_class_mtx_spin },
281103091Sjake	{ "sabtty", &lock_class_mtx_spin },
282109015Sjake	{ "zstty", &lock_class_mtx_spin },
28374912Sjhb	{ "ng_node", &lock_class_mtx_spin },
28474912Sjhb	{ "ng_worklist", &lock_class_mtx_spin },
285119813Ssam	{ "taskqueue_fast", &lock_class_mtx_spin },
286122001Sjhb	{ "intr table", &lock_class_mtx_spin },
28774912Sjhb	{ "ithread table lock", &lock_class_mtx_spin },
288126324Sjhb	{ "sleepq chain", &lock_class_mtx_spin },
28974912Sjhb	{ "sched lock", &lock_class_mtx_spin },
290122514Sjhb	{ "turnstile chain", &lock_class_mtx_spin },
291122514Sjhb	{ "td_contested", &lock_class_mtx_spin },
29274912Sjhb	{ "callout", &lock_class_mtx_spin },
293122917Smarkm	{ "entropy harvest", &lock_class_mtx_spin },
294122917Smarkm	{ "entropy harvest buffers", &lock_class_mtx_spin },
29565557Sjasone	/*
29665557Sjasone	 * leaf locks
29765557Sjasone	 */
29890278Sjhb	{ "allpmaps", &lock_class_mtx_spin },
29999416Salc	{ "vm page queue free mutex", &lock_class_mtx_spin },
30088322Sjhb	{ "icu", &lock_class_mtx_spin },
30172224Sjhb#ifdef SMP
30274912Sjhb	{ "smp rendezvous", &lock_class_mtx_spin },
303122849Speter#if defined(__i386__) || defined(__amd64__)
30499862Speter	{ "tlb", &lock_class_mtx_spin },
305112993Speter	{ "lazypmap", &lock_class_mtx_spin },
306112993Speter#endif
307108187Sjake#ifdef __sparc64__
308108187Sjake	{ "ipi", &lock_class_mtx_spin },
30999862Speter#endif
310108187Sjake#endif
31178785Sjhb	{ "clk", &lock_class_mtx_spin },
31295473Sdes	{ "mutex profiling lock", &lock_class_mtx_spin },
313111028Sjeff	{ "kse zombie lock", &lock_class_mtx_spin },
314103786Sjeff	{ "ALD Queue", &lock_class_mtx_spin },
315104951Speter#ifdef __ia64__
316104951Speter	{ "MCA spin lock", &lock_class_mtx_spin },
317104951Speter#endif
318115425Speter#if defined(__i386__) || defined(__amd64__)
319111068Speter	{ "pcicfg", &lock_class_mtx_spin },
320111068Speter#endif
32174912Sjhb	{ NULL, NULL },
32274912Sjhb	{ NULL, NULL }
32365557Sjasone};
32465557Sjasone
325105508Sphk#ifdef BLESSING
32665557Sjasone/*
32765557Sjasone * Pairs of locks which have been blessed
32865557Sjasone * Don't complain about order problems with blessed locks
32965557Sjasone */
33065856Sjhbstatic struct witness_blessed blessed_list[] = {
33165557Sjasone};
33272200Sbmilekicstatic int blessed_count =
33372200Sbmilekic	sizeof(blessed_list) / sizeof(struct witness_blessed);
334105508Sphk#endif
33565557Sjasone
33674912Sjhb/*
33774912Sjhb * List of all locks in the system.
33874912Sjhb */
33997963SjhbTAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks);
34074912Sjhb
34174912Sjhbstatic struct mtx all_mtx = {
34274912Sjhb	{ &lock_class_mtx_sleep,	/* mtx_object.lo_class */
34374912Sjhb	  "All locks list",		/* mtx_object.lo_name */
34493811Sjhb	  "All locks list",		/* mtx_object.lo_type */
34574912Sjhb	  LO_INITIALIZED,		/* mtx_object.lo_flags */
34697963Sjhb	  { NULL, NULL },		/* mtx_object.lo_list */
34774912Sjhb	  NULL },			/* mtx_object.lo_witness */
348122514Sjhb	MTX_UNOWNED, 0			/* mtx_lock, mtx_recurse */
34974912Sjhb};
35074912Sjhb
35174912Sjhb/*
35274912Sjhb * This global is set to 0 once it becomes safe to use the witness code.
35374912Sjhb */
35474912Sjhbstatic int witness_cold = 1;
35574912Sjhb
35674912Sjhb/*
35774912Sjhb * Global variables for book keeping.
35874912Sjhb */
35974912Sjhbstatic int lock_cur_cnt;
36074912Sjhbstatic int lock_max_cnt;
36174912Sjhb
36274912Sjhb/*
36374912Sjhb * The WITNESS-enabled diagnostic code.
36474912Sjhb */
36571352Sjasonestatic void
36674912Sjhbwitness_initialize(void *dummy __unused)
36765557Sjasone{
36874912Sjhb	struct lock_object *lock;
36974912Sjhb	struct witness_order_list_entry *order;
37074912Sjhb	struct witness *w, *w1;
37174912Sjhb	int i;
37265557Sjasone
37374912Sjhb	/*
37474912Sjhb	 * We have to release Giant before initializing its witness
37574912Sjhb	 * structure so that WITNESS doesn't get confused.
37674912Sjhb	 */
37774912Sjhb	mtx_unlock(&Giant);
37874912Sjhb	mtx_assert(&Giant, MA_NOTOWNED);
37974912Sjhb
38087593Sobrien	CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
38197963Sjhb	TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
38293811Sjhb	mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
38393811Sjhb	    MTX_NOWITNESS);
38474912Sjhb	for (i = 0; i < WITNESS_COUNT; i++)
38574912Sjhb		witness_free(&w_data[i]);
38674912Sjhb	for (i = 0; i < WITNESS_CHILDCOUNT; i++)
38774912Sjhb		witness_child_free(&w_childdata[i]);
38874912Sjhb	for (i = 0; i < LOCK_CHILDCOUNT; i++)
38974912Sjhb		witness_lock_list_free(&w_locklistdata[i]);
39074912Sjhb
39174912Sjhb	/* First add in all the specified order lists. */
39274912Sjhb	for (order = order_lists; order->w_name != NULL; order++) {
39374912Sjhb		w = enroll(order->w_name, order->w_class);
39475569Sjhb		if (w == NULL)
39575569Sjhb			continue;
39674912Sjhb		w->w_file = "order list";
39774912Sjhb		for (order++; order->w_name != NULL; order++) {
39874912Sjhb			w1 = enroll(order->w_name, order->w_class);
39975569Sjhb			if (w1 == NULL)
40075569Sjhb				continue;
40174912Sjhb			w1->w_file = "order list";
402112117Sjhb			if (!itismychild(w, w1))
403112117Sjhb				panic("Not enough memory for static orders!");
40474912Sjhb			w = w1;
40565557Sjasone		}
40665557Sjasone	}
40765557Sjasone
40874912Sjhb	/* Iterate through all locks and add them to witness. */
40974912Sjhb	mtx_lock(&all_mtx);
41097963Sjhb	TAILQ_FOREACH(lock, &all_locks, lo_list) {
41174912Sjhb		if (lock->lo_flags & LO_WITNESS)
41293811Sjhb			lock->lo_witness = enroll(lock->lo_type,
41374912Sjhb			    lock->lo_class);
41474912Sjhb		else
41574912Sjhb			lock->lo_witness = NULL;
41674912Sjhb	}
41774912Sjhb	mtx_unlock(&all_mtx);
41874912Sjhb
41974912Sjhb	/* Mark the witness code as being ready for use. */
42074912Sjhb	atomic_store_rel_int(&witness_cold, 0);
42174912Sjhb
42274912Sjhb	mtx_lock(&Giant);
42365557Sjasone}
42474912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
42565557Sjasone
426112562Sjhbstatic int
427112562Sjhbsysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
428112562Sjhb{
429112562Sjhb	int error, value;
430112562Sjhb
431112562Sjhb	value = witness_watch;
432112562Sjhb	error = sysctl_handle_int(oidp, &value, 0, req);
433112562Sjhb	if (error != 0 || req->newptr == NULL)
434112562Sjhb		return (error);
435112562Sjhb	error = suser(req->td);
436112562Sjhb	if (error != 0)
437112562Sjhb		return (error);
438112562Sjhb	if (value == witness_watch)
439112562Sjhb		return (0);
440112562Sjhb	if (value != 0)
441112562Sjhb		return (EINVAL);
442112562Sjhb	witness_watch = 0;
443112562Sjhb	return (0);
444112562Sjhb}
445112562Sjhb
44674912Sjhbvoid
44774912Sjhbwitness_init(struct lock_object *lock)
44874912Sjhb{
44974912Sjhb	struct lock_class *class;
45074912Sjhb
45174912Sjhb	class = lock->lo_class;
45274912Sjhb	if (lock->lo_flags & LO_INITIALIZED)
45382284Sjhb		panic("%s: lock (%s) %s is already initialized", __func__,
45474912Sjhb		    class->lc_name, lock->lo_name);
45574912Sjhb	if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
45674912Sjhb	    (class->lc_flags & LC_RECURSABLE) == 0)
45782284Sjhb		panic("%s: lock (%s) %s can not be recursable", __func__,
45874912Sjhb		    class->lc_name, lock->lo_name);
45974912Sjhb	if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
46074912Sjhb	    (class->lc_flags & LC_SLEEPABLE) == 0)
46182284Sjhb		panic("%s: lock (%s) %s can not be sleepable", __func__,
46274912Sjhb		    class->lc_name, lock->lo_name);
46382244Sjhb	if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
46482244Sjhb	    (class->lc_flags & LC_UPGRADABLE) == 0)
46582284Sjhb		panic("%s: lock (%s) %s can not be upgradable", __func__,
46682244Sjhb		    class->lc_name, lock->lo_name);
46782244Sjhb
46874912Sjhb	mtx_lock(&all_mtx);
46997963Sjhb	TAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
47074912Sjhb	lock->lo_flags |= LO_INITIALIZED;
47174912Sjhb	lock_cur_cnt++;
47274912Sjhb	if (lock_cur_cnt > lock_max_cnt)
47374912Sjhb		lock_max_cnt = lock_cur_cnt;
47474912Sjhb	mtx_unlock(&all_mtx);
475112562Sjhb	if (!witness_cold && witness_watch != 0 && panicstr == NULL &&
47674912Sjhb	    (lock->lo_flags & LO_WITNESS) != 0)
47793811Sjhb		lock->lo_witness = enroll(lock->lo_type, class);
47874912Sjhb	else
47974912Sjhb		lock->lo_witness = NULL;
48074912Sjhb}
48174912Sjhb
48274912Sjhbvoid
48374912Sjhbwitness_destroy(struct lock_object *lock)
48474912Sjhb{
48575362Sjhb	struct witness *w;
48674912Sjhb
48774912Sjhb	if (witness_cold)
48874912Sjhb		panic("lock (%s) %s destroyed while witness_cold",
48974912Sjhb		    lock->lo_class->lc_name, lock->lo_name);
49074912Sjhb	if ((lock->lo_flags & LO_INITIALIZED) == 0)
49182284Sjhb		panic("%s: lock (%s) %s is not initialized", __func__,
49274912Sjhb		    lock->lo_class->lc_name, lock->lo_name);
49374912Sjhb
49476272Sjhb	/* XXX: need to verify that no one holds the lock */
49575362Sjhb	w = lock->lo_witness;
49675362Sjhb	if (w != NULL) {
49775362Sjhb		mtx_lock_spin(&w_mtx);
49897948Sjhb		MPASS(w->w_refcount > 0);
49975362Sjhb		w->w_refcount--;
500112117Sjhb
501112117Sjhb		/*
502112117Sjhb		 * Lock is already released if we have an allocation failure
503112117Sjhb		 * and depart() fails.
504112117Sjhb		 */
505112117Sjhb		if (w->w_refcount != 0 || depart(w))
506112117Sjhb			mtx_unlock_spin(&w_mtx);
50775362Sjhb	}
50875362Sjhb
50974912Sjhb	mtx_lock(&all_mtx);
51074912Sjhb	lock_cur_cnt--;
51197963Sjhb	TAILQ_REMOVE(&all_locks, lock, lo_list);
51280055Sjhb	lock->lo_flags &= ~LO_INITIALIZED;
51374912Sjhb	mtx_unlock(&all_mtx);
51474912Sjhb}
51574912Sjhb
516112115Sjhb#ifdef DDB
51771352Sjasonestatic void
51874912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...),
51974912Sjhb		     struct witness_list *list)
52071352Sjasone{
521112118Sjhb	struct witness *w;
52271352Sjasone
52374912Sjhb	STAILQ_FOREACH(w, list, w_typelist) {
524112118Sjhb		if (w->w_file == NULL || w->w_level > 0)
52571352Sjasone			continue;
52671352Sjasone		/*
52771352Sjasone		 * This lock has no anscestors, display its descendants.
52871352Sjasone		 */
529112118Sjhb		witness_displaydescendants(prnt, w, 0);
53071352Sjasone	}
53174912Sjhb}
53272224Sjhb
53374912Sjhbstatic void
53474912Sjhbwitness_display(void(*prnt)(const char *fmt, ...))
53574912Sjhb{
53674912Sjhb	struct witness *w;
53774912Sjhb
53882284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
53974912Sjhb	witness_levelall();
54074912Sjhb
541112118Sjhb	/* Clear all the displayed flags. */
542112118Sjhb	STAILQ_FOREACH(w, &w_all, w_list) {
543112118Sjhb		w->w_displayed = 0;
544112118Sjhb	}
545112118Sjhb
54672224Sjhb	/*
54774930Sjhb	 * First, handle sleep locks which have been acquired at least
54874912Sjhb	 * once.
54974912Sjhb	 */
55074912Sjhb	prnt("Sleep locks:\n");
55174912Sjhb	witness_display_list(prnt, &w_sleep);
55274912Sjhb
55374912Sjhb	/*
55474930Sjhb	 * Now do spin locks which have been acquired at least once.
55572224Sjhb	 */
55674912Sjhb	prnt("\nSpin locks:\n");
55774912Sjhb	witness_display_list(prnt, &w_spin);
55872224Sjhb
55972224Sjhb	/*
56074930Sjhb	 * Finally, any locks which have not been acquired yet.
56172224Sjhb	 */
56274912Sjhb	prnt("\nLocks which were never acquired:\n");
56374912Sjhb	STAILQ_FOREACH(w, &w_all, w_list) {
56497948Sjhb		if (w->w_file != NULL || w->w_refcount == 0)
56571352Sjasone			continue;
56674912Sjhb		prnt("%s\n", w->w_name);
56771352Sjasone	}
56871352Sjasone}
569112115Sjhb#endif /* DDB */
57071352Sjasone
571112116Sjhb/* Trim useless garbage from filenames. */
572112116Sjhbstatic const char *
573112116Sjhbfixup_filename(const char *file)
574112116Sjhb{
575112116Sjhb
576112116Sjhb	if (file == NULL)
577112116Sjhb		return (NULL);
578112116Sjhb	while (strncmp(file, "../", 3) == 0)
579112116Sjhb		file += 3;
580112116Sjhb	return (file);
581112116Sjhb}
582112116Sjhb
583125160Sjhbint
584125160Sjhbwitness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
585125160Sjhb{
586125160Sjhb
587125160Sjhb	if (witness_watch == 0 || panicstr != NULL)
588125160Sjhb		return (0);
589125160Sjhb
590125160Sjhb	/* Require locks that witness knows about. */
591125160Sjhb	if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
592125160Sjhb	    lock2->lo_witness == NULL)
593125160Sjhb		return (EINVAL);
594125160Sjhb
595125160Sjhb	MPASS(!mtx_owned(&w_mtx));
596125160Sjhb	mtx_lock_spin(&w_mtx);
597125160Sjhb
598125160Sjhb	/*
599125160Sjhb	 * If we already have either an explicit or implied lock order that
600125160Sjhb	 * is the other way around, then return an error.
601125160Sjhb	 */
602125160Sjhb	if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
603125160Sjhb		mtx_unlock_spin(&w_mtx);
604125160Sjhb		return (EDOOFUS);
605125160Sjhb	}
606125160Sjhb
607125160Sjhb	/* Try to add the new order. */
608125160Sjhb	CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
609125160Sjhb	    lock2->lo_type, lock1->lo_type);
610125160Sjhb	if (!itismychild(lock1->lo_witness, lock2->lo_witness))
611125160Sjhb		return (ENOMEM);
612125160Sjhb	mtx_unlock_spin(&w_mtx);
613125160Sjhb	return (0);
614125160Sjhb}
615125160Sjhb
61665557Sjasonevoid
617125160Sjhbwitness_checkorder(struct lock_object *lock, int flags, const char *file,
618125160Sjhb    int line)
61965557Sjasone{
62074912Sjhb	struct lock_list_entry **lock_list, *lle;
62176272Sjhb	struct lock_instance *lock1, *lock2;
62274912Sjhb	struct lock_class *class;
62365856Sjhb	struct witness *w, *w1;
62483366Sjulian	struct thread *td;
62574912Sjhb	int i, j;
62665557Sjasone
627112562Sjhb	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
62880747Sjhb	    panicstr != NULL)
62971320Sjasone		return;
630125160Sjhb
631125160Sjhb	/*
632125160Sjhb	 * Try locks do not block if they fail to acquire the lock, thus
633125160Sjhb	 * there is no danger of deadlocks or of switching while holding a
634125160Sjhb	 * spin lock if we acquire a lock via a try operation.  This
635125160Sjhb	 * function shouldn't even be called for try locks, so panic if
636125160Sjhb	 * that happens.
637125160Sjhb	 */
638125160Sjhb	if (flags & LOP_TRYLOCK)
639125160Sjhb		panic("%s should not be called for try lock operations",
640125160Sjhb		    __func__);
641125160Sjhb
64274912Sjhb	w = lock->lo_witness;
64374912Sjhb	class = lock->lo_class;
64483366Sjulian	td = curthread;
645112116Sjhb	file = fixup_filename(file);
64665557Sjasone
64774912Sjhb	if (class->lc_flags & LC_SLEEPLOCK) {
64893676Sjhb		/*
64993676Sjhb		 * Since spin locks include a critical section, this check
65093676Sjhb		 * impliclty enforces a lock order of all sleep locks before
65193676Sjhb		 * all spin locks.
65293676Sjhb		 */
653125160Sjhb		if (td->td_critnest != 0)
65474912Sjhb			panic("blockable sleep lock (%s) %s @ %s:%d",
65574912Sjhb			    class->lc_name, lock->lo_name, file, line);
65683366Sjulian		lock_list = &td->td_sleeplocks;
65788899Sjhb	} else
65888899Sjhb		lock_list = PCPU_PTR(spinlocks);
65965557Sjasone
66076772Sjhb	/*
66174912Sjhb	 * Is this the first lock acquired?  If so, then no order checking
66274912Sjhb	 * is needed.
66365557Sjasone	 */
66474912Sjhb	if (*lock_list == NULL)
665125160Sjhb		return;
66665557Sjasone
66774912Sjhb	/*
668125160Sjhb	 * Check to see if we are recursing on a lock we already own.  If
669125160Sjhb	 * so, make sure that we don't mismatch exclusive and shared lock
670125160Sjhb	 * acquires.
67176272Sjhb	 */
67276272Sjhb	lock1 = find_instance(*lock_list, lock);
67376272Sjhb	if (lock1 != NULL) {
67476272Sjhb		if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
67576272Sjhb		    (flags & LOP_EXCLUSIVE) == 0) {
67676272Sjhb			printf("shared lock of (%s) %s @ %s:%d\n",
67776272Sjhb			    class->lc_name, lock->lo_name, file, line);
67876272Sjhb			printf("while exclusively locked from %s:%d\n",
67976272Sjhb			    lock1->li_file, lock1->li_line);
68076272Sjhb			panic("share->excl");
68176272Sjhb		}
68276272Sjhb		if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
68376272Sjhb		    (flags & LOP_EXCLUSIVE) != 0) {
68476272Sjhb			printf("exclusive lock of (%s) %s @ %s:%d\n",
68576272Sjhb			    class->lc_name, lock->lo_name, file, line);
68676272Sjhb			printf("while share locked from %s:%d\n",
68776272Sjhb			    lock1->li_file, lock1->li_line);
68876272Sjhb			panic("excl->share");
68976272Sjhb		}
69076272Sjhb		return;
69176272Sjhb	}
69276272Sjhb
69376272Sjhb	/*
694112112Sjhb	 * Try locks do not block if they fail to acquire the lock, thus
695112112Sjhb	 * there is no danger of deadlocks or of switching while holding a
696112112Sjhb	 * spin lock if we acquire a lock via a try operation.
697112112Sjhb	 */
698112112Sjhb	if (flags & LOP_TRYLOCK)
699125160Sjhb		return;
700112112Sjhb
701112112Sjhb	/*
70274912Sjhb	 * Check for duplicate locks of the same type.  Note that we only
70374912Sjhb	 * have to check for this on the last lock we just acquired.  Any
70474912Sjhb	 * other cases will be caught as lock order violations.
70574912Sjhb	 */
70676272Sjhb	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
70776272Sjhb	w1 = lock1->li_lock->lo_witness;
70874912Sjhb	if (w1 == w) {
70993273Sjeff		if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK))
710125160Sjhb			return;
71165557Sjasone		w->w_same_squawked = 1;
71275755Sjhb		printf("acquiring duplicate lock of same type: \"%s\"\n",
71393811Sjhb			lock->lo_type);
71493811Sjhb		printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name,
71593811Sjhb		    lock1->li_file, lock1->li_line);
71693811Sjhb		printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
71767676Sjhb#ifdef DDB
718125160Sjhb		goto debugger;
719125160Sjhb#else
720125160Sjhb		return;
721112115Sjhb#endif
72265557Sjasone	}
72365557Sjasone	MPASS(!mtx_owned(&w_mtx));
72474912Sjhb	mtx_lock_spin(&w_mtx);
72565557Sjasone	/*
72665557Sjasone	 * If we have a known higher number just say ok
72765557Sjasone	 */
72865557Sjasone	if (witness_watch > 1 && w->w_level > w1->w_level) {
72974912Sjhb		mtx_unlock_spin(&w_mtx);
730125160Sjhb		return;
73165557Sjasone	}
732111881Sjhb	/*
733111881Sjhb	 * If we know that the the lock we are acquiring comes after
734111881Sjhb	 * the lock we most recently acquired in the lock order tree,
735111881Sjhb	 * then there is no need for any further checks.
736111881Sjhb	 */
73774912Sjhb	if (isitmydescendant(w1, w)) {
73874912Sjhb		mtx_unlock_spin(&w_mtx);
739125160Sjhb		return;
74065557Sjasone	}
74174912Sjhb	for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
74274912Sjhb		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
74365557Sjasone
74474912Sjhb			MPASS(j < WITNESS_COUNT);
74576272Sjhb			lock1 = &lle->ll_children[i];
74676272Sjhb			w1 = lock1->li_lock->lo_witness;
74774912Sjhb
74874912Sjhb			/*
74974912Sjhb			 * If this lock doesn't undergo witness checking,
75074912Sjhb			 * then skip it.
75174912Sjhb			 */
75274912Sjhb			if (w1 == NULL) {
75376272Sjhb				KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
75474912Sjhb				    ("lock missing witness structure"));
75574912Sjhb				continue;
75674912Sjhb			}
75776272Sjhb			/*
758111881Sjhb			 * If we are locking Giant and this is a sleepable
75976272Sjhb			 * lock, then skip it.
76076272Sjhb			 */
761111881Sjhb			if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
76276272Sjhb			    lock == &Giant.mtx_object)
76376272Sjhb				continue;
76493690Sjhb			/*
76593690Sjhb			 * If we are locking a sleepable lock and this lock
766111881Sjhb			 * is Giant, then skip it.
76793690Sjhb			 */
768111881Sjhb			if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
769111887Sjhb			    lock1->li_lock == &Giant.mtx_object)
770111881Sjhb				continue;
771111881Sjhb			/*
772111881Sjhb			 * If we are locking a sleepable lock and this lock
773111881Sjhb			 * isn't sleepable, we want to treat it as a lock
774111881Sjhb			 * order violation to enfore a general lock order of
775111881Sjhb			 * sleepable locks before non-sleepable locks.
776111881Sjhb			 */
77793690Sjhb			if (!((lock->lo_flags & LO_SLEEPABLE) != 0 &&
778111881Sjhb			    (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
779111881Sjhb			    /*
780111881Sjhb			     * Check the lock order hierarchy for a reveresal.
781111881Sjhb			     */
782111881Sjhb			    if (!isitmydescendant(w, w1))
78374912Sjhb				continue;
78474912Sjhb			/*
78574912Sjhb			 * We have a lock order violation, check to see if it
78674912Sjhb			 * is allowed or has already been yelled about.
78774912Sjhb			 */
78874912Sjhb			mtx_unlock_spin(&w_mtx);
789105508Sphk#ifdef BLESSING
790125160Sjhb			/*
791125160Sjhb			 * If the lock order is blessed, just bail.  We don't
792125160Sjhb			 * look for other lock order violations though, which
793125160Sjhb			 * may be a bug.
794125160Sjhb			 */
79565557Sjasone			if (blessed(w, w1))
796125160Sjhb				return;
797105508Sphk#endif
79876272Sjhb			if (lock1->li_lock == &Giant.mtx_object) {
79965557Sjasone				if (w1->w_Giant_squawked)
800125160Sjhb					return;
80165557Sjasone				else
80265557Sjasone					w1->w_Giant_squawked = 1;
80365557Sjasone			} else {
80465557Sjasone				if (w1->w_other_squawked)
805125160Sjhb					return;
80665557Sjasone				else
80765557Sjasone					w1->w_other_squawked = 1;
80865557Sjasone			}
80974912Sjhb			/*
81074912Sjhb			 * Ok, yell about it.
81174912Sjhb			 */
81265557Sjasone			printf("lock order reversal\n");
81374912Sjhb			/*
81474912Sjhb			 * Try to locate an earlier lock with
81574912Sjhb			 * witness w in our list.
81674912Sjhb			 */
81774912Sjhb			do {
81876272Sjhb				lock2 = &lle->ll_children[i];
81976272Sjhb				MPASS(lock2->li_lock != NULL);
82076272Sjhb				if (lock2->li_lock->lo_witness == w)
82174912Sjhb					break;
82274912Sjhb				if (i == 0 && lle->ll_next != NULL) {
82374912Sjhb					lle = lle->ll_next;
82474912Sjhb					i = lle->ll_count - 1;
825106781Sjhb					MPASS(i >= 0 && i < LOCK_NCHILDREN);
826125160Sjhb				} else
827125160Sjhb					i--;
82874912Sjhb			} while (i >= 0);
82976272Sjhb			if (i < 0) {
83093811Sjhb				printf(" 1st %p %s (%s) @ %s:%d\n",
83193811Sjhb				    lock1->li_lock, lock1->li_lock->lo_name,
83293811Sjhb				    lock1->li_lock->lo_type, lock1->li_file,
83376272Sjhb				    lock1->li_line);
83493811Sjhb				printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
83593811Sjhb				    lock->lo_name, lock->lo_type, file, line);
83676272Sjhb			} else {
83793811Sjhb				printf(" 1st %p %s (%s) @ %s:%d\n",
83893811Sjhb				    lock2->li_lock, lock2->li_lock->lo_name,
83993811Sjhb				    lock2->li_lock->lo_type, lock2->li_file,
84076272Sjhb				    lock2->li_line);
84193811Sjhb				printf(" 2nd %p %s (%s) @ %s:%d\n",
84293811Sjhb				    lock1->li_lock, lock1->li_lock->lo_name,
84393811Sjhb				    lock1->li_lock->lo_type, lock1->li_file,
84476272Sjhb				    lock1->li_line);
84593811Sjhb				printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
84693811Sjhb				    lock->lo_name, lock->lo_type, file, line);
84776272Sjhb			}
84867676Sjhb#ifdef DDB
849125160Sjhb			goto debugger;
850125160Sjhb#else
851125160Sjhb			return;
852112115Sjhb#endif
85365557Sjasone		}
85465557Sjasone	}
85576272Sjhb	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
85678871Sjhb	/*
857125160Sjhb	 * If requested, build a new lock order.  However, don't build a new
858125160Sjhb	 * relationship between a sleepable lock and Giant if it is in the
859125160Sjhb	 * wrong direction.  The correct lock order is that sleepable locks
860125160Sjhb	 * always come before Giant.
86178871Sjhb	 */
862125160Sjhb	if (flags & LOP_NEWORDER &&
863125160Sjhb	    !(lock1->li_lock == &Giant.mtx_object &&
864112117Sjhb	    (lock->lo_flags & LO_SLEEPABLE) != 0)) {
86587593Sobrien		CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
86693811Sjhb		    lock->lo_type, lock1->li_lock->lo_type);
86778871Sjhb		if (!itismychild(lock1->li_lock->lo_witness, w))
868112117Sjhb			/* Witness is dead. */
869112117Sjhb			return;
87078871Sjhb	}
871112117Sjhb	mtx_unlock_spin(&w_mtx);
872125160Sjhb	return;
87365557Sjasone
87467676Sjhb#ifdef DDB
875125160Sjhbdebugger:
876125160Sjhb	if (witness_trace)
877125160Sjhb		backtrace();
878125160Sjhb	if (witness_ddb)
879125160Sjhb		Debugger(__func__);
880125160Sjhb#endif
881125160Sjhb}
882125160Sjhb
883125160Sjhbvoid
884125160Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line)
885125160Sjhb{
886125160Sjhb	struct lock_list_entry **lock_list, *lle;
887125160Sjhb	struct lock_instance *instance;
888125160Sjhb	struct witness *w;
889125160Sjhb	struct thread *td;
890125160Sjhb
891125160Sjhb	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
892125160Sjhb	    panicstr != NULL)
893125160Sjhb		return;
894125160Sjhb	w = lock->lo_witness;
895125160Sjhb	td = curthread;
896125160Sjhb	file = fixup_filename(file);
897125160Sjhb
898125160Sjhb	/* Determine lock list for this lock. */
899125160Sjhb	if (lock->lo_class->lc_flags & LC_SLEEPLOCK)
900125160Sjhb		lock_list = &td->td_sleeplocks;
901125160Sjhb	else
902125160Sjhb		lock_list = PCPU_PTR(spinlocks);
903125160Sjhb
904125160Sjhb	/* Check to see if we are recursing on a lock we already own. */
905125160Sjhb	instance = find_instance(*lock_list, lock);
906125160Sjhb	if (instance != NULL) {
907125160Sjhb		instance->li_flags++;
908125160Sjhb		CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
909125160Sjhb		    td->td_proc->p_pid, lock->lo_name,
910125160Sjhb		    instance->li_flags & LI_RECURSEMASK);
911125160Sjhb		instance->li_file = file;
912125160Sjhb		instance->li_line = line;
913125160Sjhb		return;
914110779Speter	}
915125160Sjhb
916125160Sjhb	/* Update per-witness last file and line acquire. */
91765557Sjasone	w->w_file = file;
91865557Sjasone	w->w_line = line;
919125160Sjhb
920125160Sjhb	/* Find the next open lock instance in the list and fill it. */
92174912Sjhb	lle = *lock_list;
92276272Sjhb	if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
92378785Sjhb		lle = witness_lock_list_get();
92478785Sjhb		if (lle == NULL)
92565557Sjasone			return;
92678785Sjhb		lle->ll_next = *lock_list;
92787593Sobrien		CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
92884680Sjhb		    td->td_proc->p_pid, lle);
92978785Sjhb		*lock_list = lle;
93065557Sjasone	}
931125160Sjhb	instance = &lle->ll_children[lle->ll_count++];
932125160Sjhb	instance->li_lock = lock;
933125160Sjhb	instance->li_line = line;
934125160Sjhb	instance->li_file = file;
93576272Sjhb	if ((flags & LOP_EXCLUSIVE) != 0)
936125160Sjhb		instance->li_flags = LI_EXCLUSIVE;
93776272Sjhb	else
938125160Sjhb		instance->li_flags = 0;
93987593Sobrien	CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
94084680Sjhb	    td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
94165557Sjasone}
94265557Sjasone
94365557Sjasonevoid
94482244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
94582244Sjhb{
94682244Sjhb	struct lock_instance *instance;
94782244Sjhb	struct lock_class *class;
94882244Sjhb
94982284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
950112562Sjhb	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
95182244Sjhb		return;
95282244Sjhb	class = lock->lo_class;
953112116Sjhb	file = fixup_filename(file);
95482244Sjhb	if ((lock->lo_flags & LO_UPGRADABLE) == 0)
95582244Sjhb		panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
95682244Sjhb		    class->lc_name, lock->lo_name, file, line);
95782244Sjhb	if ((flags & LOP_TRYLOCK) == 0)
95882244Sjhb		panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
95982244Sjhb		    lock->lo_name, file, line);
96082244Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
96182244Sjhb		panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
96282244Sjhb		    class->lc_name, lock->lo_name, file, line);
96383366Sjulian	instance = find_instance(curthread->td_sleeplocks, lock);
96482244Sjhb	if (instance == NULL)
96582244Sjhb		panic("upgrade of unlocked lock (%s) %s @ %s:%d",
96682244Sjhb		    class->lc_name, lock->lo_name, file, line);
96782244Sjhb	if ((instance->li_flags & LI_EXCLUSIVE) != 0)
96882244Sjhb		panic("upgrade of exclusive lock (%s) %s @ %s:%d",
96982244Sjhb		    class->lc_name, lock->lo_name, file, line);
97082244Sjhb	if ((instance->li_flags & LI_RECURSEMASK) != 0)
97182244Sjhb		panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
97282244Sjhb		    class->lc_name, lock->lo_name,
97382244Sjhb		    instance->li_flags & LI_RECURSEMASK, file, line);
97482244Sjhb	instance->li_flags |= LI_EXCLUSIVE;
97582244Sjhb}
97682244Sjhb
97782244Sjhbvoid
97882244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file,
97982244Sjhb    int line)
98082244Sjhb{
98182244Sjhb	struct lock_instance *instance;
98282244Sjhb	struct lock_class *class;
98382244Sjhb
98482284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
985112562Sjhb	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
98682244Sjhb		return;
98782244Sjhb	class = lock->lo_class;
988112116Sjhb	file = fixup_filename(file);
98982244Sjhb	if ((lock->lo_flags & LO_UPGRADABLE) == 0)
99082244Sjhb		panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
99182244Sjhb		    class->lc_name, lock->lo_name, file, line);
99282244Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
99382244Sjhb		panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
99482244Sjhb		    class->lc_name, lock->lo_name, file, line);
99583366Sjulian	instance = find_instance(curthread->td_sleeplocks, lock);
99682244Sjhb	if (instance == NULL)
99782244Sjhb		panic("downgrade of unlocked lock (%s) %s @ %s:%d",
99882244Sjhb		    class->lc_name, lock->lo_name, file, line);
99982244Sjhb	if ((instance->li_flags & LI_EXCLUSIVE) == 0)
100082244Sjhb		panic("downgrade of shared lock (%s) %s @ %s:%d",
100182244Sjhb		    class->lc_name, lock->lo_name, file, line);
100282244Sjhb	if ((instance->li_flags & LI_RECURSEMASK) != 0)
100382244Sjhb		panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
100482244Sjhb		    class->lc_name, lock->lo_name,
100582244Sjhb		    instance->li_flags & LI_RECURSEMASK, file, line);
100682244Sjhb	instance->li_flags &= ~LI_EXCLUSIVE;
100782244Sjhb}
100882244Sjhb
100982244Sjhbvoid
101074912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line)
101165557Sjasone{
101274912Sjhb	struct lock_list_entry **lock_list, *lle;
101376272Sjhb	struct lock_instance *instance;
101474912Sjhb	struct lock_class *class;
101583366Sjulian	struct thread *td;
101692858Simp	register_t s;
101774912Sjhb	int i, j;
101865557Sjasone
1019112562Sjhb	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
102080747Sjhb	    panicstr != NULL)
102171352Sjasone		return;
102283366Sjulian	td = curthread;
102374912Sjhb	class = lock->lo_class;
1024112116Sjhb	file = fixup_filename(file);
1025125160Sjhb
1026125160Sjhb	/* Find lock instance associated with this lock. */
102776272Sjhb	if (class->lc_flags & LC_SLEEPLOCK)
102883366Sjulian		lock_list = &td->td_sleeplocks;
102976272Sjhb	else
103074912Sjhb		lock_list = PCPU_PTR(spinlocks);
103174912Sjhb	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
103276272Sjhb		for (i = 0; i < (*lock_list)->ll_count; i++) {
103376272Sjhb			instance = &(*lock_list)->ll_children[i];
1034125160Sjhb			if (instance->li_lock == lock)
1035125160Sjhb				goto found;
103676272Sjhb		}
103776272Sjhb	panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
103876272Sjhb	    file, line);
1039125160Sjhbfound:
1040125160Sjhb
1041125160Sjhb	/* First, check for shared/exclusive mismatches. */
1042125160Sjhb	if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
1043125160Sjhb	    (flags & LOP_EXCLUSIVE) == 0) {
1044125160Sjhb		printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1045125160Sjhb		    lock->lo_name, file, line);
1046125160Sjhb		printf("while exclusively locked from %s:%d\n",
1047125160Sjhb		    instance->li_file, instance->li_line);
1048125160Sjhb		panic("excl->ushare");
1049125160Sjhb	}
1050125160Sjhb	if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
1051125160Sjhb	    (flags & LOP_EXCLUSIVE) != 0) {
1052125160Sjhb		printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1053125160Sjhb		    lock->lo_name, file, line);
1054125160Sjhb		printf("while share locked from %s:%d\n", instance->li_file,
1055125160Sjhb		    instance->li_line);
1056125160Sjhb		panic("share->uexcl");
1057125160Sjhb	}
1058125160Sjhb
1059125160Sjhb	/* If we are recursed, unrecurse. */
1060125160Sjhb	if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1061125160Sjhb		CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1062125160Sjhb		    td->td_proc->p_pid, instance->li_lock->lo_name,
1063125160Sjhb		    instance->li_flags);
1064125160Sjhb		instance->li_flags--;
1065125160Sjhb		return;
1066125160Sjhb	}
1067125160Sjhb
1068125160Sjhb	/* Otherwise, remove this item from the list. */
1069125160Sjhb	s = intr_disable();
1070125160Sjhb	CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1071125160Sjhb	    td->td_proc->p_pid, instance->li_lock->lo_name,
1072125160Sjhb	    (*lock_list)->ll_count - 1);
1073125160Sjhb	for (j = i; j < (*lock_list)->ll_count - 1; j++)
1074125160Sjhb		(*lock_list)->ll_children[j] =
1075125160Sjhb		    (*lock_list)->ll_children[j + 1];
1076125160Sjhb	(*lock_list)->ll_count--;
1077125160Sjhb	intr_restore(s);
1078125160Sjhb
1079125160Sjhb	/* If this lock list entry is now empty, free it. */
1080125160Sjhb	if ((*lock_list)->ll_count == 0) {
1081125160Sjhb		lle = *lock_list;
1082125160Sjhb		*lock_list = lle->ll_next;
1083125160Sjhb		CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1084125160Sjhb		    td->td_proc->p_pid, lle);
1085125160Sjhb		witness_lock_list_free(lle);
1086125160Sjhb	}
108765557Sjasone}
108865557Sjasone
108974912Sjhb/*
1090111881Sjhb * Warn if any locks other than 'lock' are held.  Flags can be passed in to
1091111881Sjhb * exempt Giant and sleepable locks from the checks as well.  If any
1092111881Sjhb * non-exempt locks are held, then a supplied message is printed to the
1093111881Sjhb * console along with a list of the offending locks.  If indicated in the
1094111881Sjhb * flags then a failure results in a panic as well.
109574912Sjhb */
109665557Sjasoneint
1097111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
109865557Sjasone{
1099111881Sjhb	struct lock_list_entry *lle;
110076272Sjhb	struct lock_instance *lock1;
110183366Sjulian	struct thread *td;
1102111881Sjhb	va_list ap;
110374912Sjhb	int i, n;
110465557Sjasone
1105112562Sjhb	if (witness_cold || witness_watch == 0 || panicstr != NULL)
110674912Sjhb		return (0);
110774912Sjhb	n = 0;
110883366Sjulian	td = curthread;
1109111881Sjhb	for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
111074912Sjhb		for (i = lle->ll_count - 1; i >= 0; i--) {
111176272Sjhb			lock1 = &lle->ll_children[i];
1112111881Sjhb			if (lock1->li_lock == lock)
1113111881Sjhb				continue;
1114111881Sjhb			if (flags & WARN_GIANTOK &&
111576272Sjhb			    lock1->li_lock == &Giant.mtx_object)
111674912Sjhb				continue;
1117111881Sjhb			if (flags & WARN_SLEEPOK &&
1118111881Sjhb			    (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
111976272Sjhb				continue;
1120111881Sjhb			if (n == 0) {
1121111881Sjhb				va_start(ap, fmt);
1122111881Sjhb				vprintf(fmt, ap);
1123111881Sjhb				va_end(ap);
1124111881Sjhb				printf(" with the following");
1125111881Sjhb				if (flags & WARN_SLEEPOK)
1126111881Sjhb					printf(" non-sleepable");
1127118441Sjhb				printf(" locks held:\n");
112876272Sjhb			}
112974912Sjhb			n++;
1130111881Sjhb			witness_list_lock(lock1);
113174912Sjhb		}
1132111881Sjhb	if (PCPU_GET(spinlocks) != NULL) {
113397006Sjhb		/*
113497006Sjhb		 * Since we already hold a spinlock preemption is
113597006Sjhb		 * already blocked.
113697006Sjhb		 */
1137111881Sjhb		if (n == 0) {
1138111881Sjhb			va_start(ap, fmt);
1139111881Sjhb			vprintf(fmt, ap);
1140111881Sjhb			va_end(ap);
1141111881Sjhb			printf(" with the following");
1142111881Sjhb			if (flags & WARN_SLEEPOK)
1143111881Sjhb				printf(" non-sleepable");
1144118441Sjhb			printf(" locks held:\n");
1145111881Sjhb		}
1146111881Sjhb		n += witness_list_locks(PCPU_PTR(spinlocks));
114765557Sjasone	}
1148111881Sjhb	if (flags & WARN_PANIC && n)
1149111881Sjhb		panic("witness_warn");
115067676Sjhb#ifdef DDB
1151111881Sjhb	else if (witness_ddb && n)
115275711Sjhb		Debugger(__func__);
1153127323Salfred	else if (witness_trace && n)
1154127323Salfred		backtrace();
1155111881Sjhb#endif
115665557Sjasone	return (n);
115765557Sjasone}
115865557Sjasone
1159102448Siedowseconst char *
1160102448Siedowsewitness_file(struct lock_object *lock)
1161102448Siedowse{
1162102448Siedowse	struct witness *w;
1163102448Siedowse
1164112562Sjhb	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1165102448Siedowse		return ("?");
1166102448Siedowse	w = lock->lo_witness;
1167102448Siedowse	return (w->w_file);
1168102448Siedowse}
1169102448Siedowse
1170102448Siedowseint
1171102448Siedowsewitness_line(struct lock_object *lock)
1172102448Siedowse{
1173102448Siedowse	struct witness *w;
1174102448Siedowse
1175112562Sjhb	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1176102448Siedowse		return (0);
1177102448Siedowse	w = lock->lo_witness;
1178102448Siedowse	return (w->w_line);
1179102448Siedowse}
1180102448Siedowse
118165856Sjhbstatic struct witness *
118274912Sjhbenroll(const char *description, struct lock_class *lock_class)
118365557Sjasone{
118474912Sjhb	struct witness *w;
118565557Sjasone
1186125348Sjhb	if (witness_watch == 0 || panicstr != NULL)
118765557Sjasone		return (NULL);
118874912Sjhb	if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
118965557Sjasone		return (NULL);
119074912Sjhb	mtx_lock_spin(&w_mtx);
119174912Sjhb	STAILQ_FOREACH(w, &w_all, w_list) {
119297948Sjhb		if (w->w_name == description || (w->w_refcount > 0 &&
119397948Sjhb		    strcmp(description, w->w_name) == 0)) {
119475362Sjhb			w->w_refcount++;
119574912Sjhb			mtx_unlock_spin(&w_mtx);
119674912Sjhb			if (lock_class != w->w_class)
119774912Sjhb				panic(
119874912Sjhb				"lock (%s) %s does not match earlier (%s) lock",
119974912Sjhb				    description, lock_class->lc_name,
120074912Sjhb				    w->w_class->lc_name);
120165557Sjasone			return (w);
120265557Sjasone		}
120365557Sjasone	}
120474912Sjhb	/*
120574912Sjhb	 * This isn't quite right, as witness_cold is still 0 while we
120674912Sjhb	 * enroll all the locks initialized before witness_initialize().
120774912Sjhb	 */
120875364Sbp	if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
120975364Sbp		mtx_unlock_spin(&w_mtx);
121074912Sjhb		panic("spin lock %s not in order list", description);
121175364Sbp	}
121265557Sjasone	if ((w = witness_get()) == NULL)
121365557Sjasone		return (NULL);
121474912Sjhb	w->w_name = description;
121574912Sjhb	w->w_class = lock_class;
121675362Sjhb	w->w_refcount = 1;
121774912Sjhb	STAILQ_INSERT_HEAD(&w_all, w, w_list);
121874912Sjhb	if (lock_class->lc_flags & LC_SPINLOCK)
121974912Sjhb		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
122074912Sjhb	else if (lock_class->lc_flags & LC_SLEEPLOCK)
122174912Sjhb		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
122275364Sbp	else {
122375364Sbp		mtx_unlock_spin(&w_mtx);
122474912Sjhb		panic("lock class %s is not sleep or spin",
122574912Sjhb		    lock_class->lc_name);
122675364Sbp	}
122774912Sjhb	mtx_unlock_spin(&w_mtx);
122865557Sjasone	return (w);
122965557Sjasone}
123065557Sjasone
1231112117Sjhb/* Don't let the door bang you on the way out... */
123265557Sjasonestatic int
1233112117Sjhbdepart(struct witness *w)
123465557Sjasone{
1235112117Sjhb	struct witness_child_list_entry *wcl, *nwcl;
123674912Sjhb	struct witness_list *list;
1237112117Sjhb	struct witness *parent;
123865557Sjasone
1239112117Sjhb	MPASS(w->w_refcount == 0);
1240112117Sjhb	if (w->w_class->lc_flags & LC_SLEEPLOCK)
1241112117Sjhb		list = &w_sleep;
1242112117Sjhb	else
1243112117Sjhb		list = &w_spin;
1244112117Sjhb	/*
1245112117Sjhb	 * First, we run through the entire tree looking for any
1246112117Sjhb	 * witnesses that the outgoing witness is a child of.  For
1247112117Sjhb	 * each parent that we find, we reparent all the direct
1248112117Sjhb	 * children of the outgoing witness to its parent.
1249112117Sjhb	 */
1250112117Sjhb	STAILQ_FOREACH(parent, list, w_typelist) {
1251112117Sjhb		if (!isitmychild(parent, w))
1252112117Sjhb			continue;
1253112117Sjhb		removechild(parent, w);
1254112117Sjhb		if (!reparentchildren(parent, w))
1255112117Sjhb			return (0);
1256112117Sjhb	}
1257112117Sjhb
1258112117Sjhb	/*
1259112117Sjhb	 * Now we go through and free up the child list of the
1260112117Sjhb	 * outgoing witness.
1261112117Sjhb	 */
1262112117Sjhb	for (wcl = w->w_children; wcl != NULL; wcl = nwcl) {
1263112117Sjhb		nwcl = wcl->wcl_next;
1264112117Sjhb		witness_child_free(wcl);
1265112117Sjhb	}
1266112117Sjhb
1267112117Sjhb	/*
1268112117Sjhb	 * Detach from various lists and free.
1269112117Sjhb	 */
1270112117Sjhb	STAILQ_REMOVE(list, w, witness, w_typelist);
1271112117Sjhb	STAILQ_REMOVE(&w_all, w, witness, w_list);
1272112117Sjhb	witness_free(w);
1273112117Sjhb
1274112117Sjhb	/* Finally, fixup the tree. */
1275112117Sjhb	return (rebalancetree(list));
1276112117Sjhb}
1277112117Sjhb
1278112117Sjhb/*
1279112117Sjhb * Prune an entire lock order tree.  We look for cases where a lock
1280112117Sjhb * is now both a descendant and a direct child of a given lock.  In
1281112117Sjhb * that case, we want to remove the direct child link from the tree.
1282112117Sjhb *
1283112117Sjhb * Returns false if insertchild() fails.
1284112117Sjhb */
1285112117Sjhbstatic int
1286112117Sjhbrebalancetree(struct witness_list *list)
1287112117Sjhb{
1288112117Sjhb	struct witness *child, *parent;
1289112117Sjhb
1290112117Sjhb	STAILQ_FOREACH(child, list, w_typelist) {
1291112117Sjhb		STAILQ_FOREACH(parent, list, w_typelist) {
1292112117Sjhb			if (!isitmychild(parent, child))
1293112117Sjhb				continue;
1294112117Sjhb			removechild(parent, child);
1295112117Sjhb			if (isitmydescendant(parent, child))
1296112117Sjhb				continue;
1297112117Sjhb			if (!insertchild(parent, child))
1298112117Sjhb				return (0);
1299112117Sjhb		}
1300112117Sjhb	}
1301112117Sjhb	witness_levelall();
1302112117Sjhb	return (1);
1303112117Sjhb}
1304112117Sjhb
1305112117Sjhb/*
1306112117Sjhb * Add "child" as a direct child of "parent".  Returns false if
1307112117Sjhb * we fail due to out of memory.
1308112117Sjhb */
1309112117Sjhbstatic int
1310112117Sjhbinsertchild(struct witness *parent, struct witness *child)
1311112117Sjhb{
1312112117Sjhb	struct witness_child_list_entry **wcl;
1313112117Sjhb
131474912Sjhb	MPASS(child != NULL && parent != NULL);
131574912Sjhb
131665557Sjasone	/*
131765557Sjasone	 * Insert "child" after "parent"
131865557Sjasone	 */
131974912Sjhb	wcl = &parent->w_children;
132074912Sjhb	while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
132174912Sjhb		wcl = &(*wcl)->wcl_next;
132274912Sjhb	if (*wcl == NULL) {
132374912Sjhb		*wcl = witness_child_get();
132474912Sjhb		if (*wcl == NULL)
1325112117Sjhb			return (0);
132665557Sjasone	}
132774912Sjhb	(*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
132874912Sjhb
1329112117Sjhb	return (1);
1330112117Sjhb}
1331112117Sjhb
1332112117Sjhb/*
1333112117Sjhb * Make all the direct descendants of oldparent be direct descendants
1334112117Sjhb * of newparent.
1335112117Sjhb */
1336112117Sjhbstatic int
1337112117Sjhbreparentchildren(struct witness *newparent, struct witness *oldparent)
1338112117Sjhb{
1339112117Sjhb	struct witness_child_list_entry *wcl;
1340112117Sjhb	int i;
1341112117Sjhb
1342112117Sjhb	/* Avoid making a witness a child of itself. */
1343112117Sjhb	MPASS(!isitmychild(oldparent, newparent));
1344112117Sjhb
1345112117Sjhb	for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1346112117Sjhb		for (i = 0; i < wcl->wcl_count; i++)
1347112117Sjhb			if (!insertchild(newparent, wcl->wcl_children[i]))
1348112117Sjhb				return (0);
1349112117Sjhb	return (1);
1350112117Sjhb}
1351112117Sjhb
1352112117Sjhbstatic int
1353112117Sjhbitismychild(struct witness *parent, struct witness *child)
1354112117Sjhb{
1355112117Sjhb	struct witness_list *list;
1356112117Sjhb
1357112117Sjhb	MPASS(child != NULL && parent != NULL);
1358112117Sjhb	if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
1359112117Sjhb	    (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
1360112117Sjhb		panic(
1361112117Sjhb		"%s: parent (%s) and child (%s) are not the same lock type",
1362112117Sjhb		    __func__, parent->w_class->lc_name,
1363112117Sjhb		    child->w_class->lc_name);
1364112117Sjhb
1365112117Sjhb	if (!insertchild(parent, child))
136665557Sjasone		return (0);
1367112117Sjhb
136874912Sjhb	if (parent->w_class->lc_flags & LC_SLEEPLOCK)
136974912Sjhb		list = &w_sleep;
137074912Sjhb	else
137174912Sjhb		list = &w_spin;
1372112117Sjhb	return (rebalancetree(list));
137365557Sjasone}
137465557Sjasone
137565557Sjasonestatic void
137665856Sjhbremovechild(struct witness *parent, struct witness *child)
137765557Sjasone{
137874912Sjhb	struct witness_child_list_entry **wcl, *wcl1;
137965557Sjasone	int i;
138065557Sjasone
138174912Sjhb	for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
138274912Sjhb		for (i = 0; i < (*wcl)->wcl_count; i++)
138374912Sjhb			if ((*wcl)->wcl_children[i] == child)
138465557Sjasone				goto found;
138565557Sjasone	return;
138665557Sjasonefound:
138774912Sjhb	(*wcl)->wcl_count--;
138874912Sjhb	if ((*wcl)->wcl_count > i)
138974912Sjhb		(*wcl)->wcl_children[i] =
139074912Sjhb		    (*wcl)->wcl_children[(*wcl)->wcl_count];
139174912Sjhb	MPASS((*wcl)->wcl_children[i] != NULL);
139274912Sjhb	if ((*wcl)->wcl_count != 0)
139365557Sjasone		return;
139474912Sjhb	wcl1 = *wcl;
139574912Sjhb	*wcl = wcl1->wcl_next;
139674912Sjhb	witness_child_free(wcl1);
139765557Sjasone}
139865557Sjasone
139965557Sjasonestatic int
140065856Sjhbisitmychild(struct witness *parent, struct witness *child)
140165557Sjasone{
140274912Sjhb	struct witness_child_list_entry *wcl;
140365557Sjasone	int i;
140465557Sjasone
140574912Sjhb	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
140674912Sjhb		for (i = 0; i < wcl->wcl_count; i++) {
140774912Sjhb			if (wcl->wcl_children[i] == child)
140865557Sjasone				return (1);
140965557Sjasone		}
141065557Sjasone	}
141165557Sjasone	return (0);
141265557Sjasone}
141365557Sjasone
141465557Sjasonestatic int
141565856Sjhbisitmydescendant(struct witness *parent, struct witness *child)
141665557Sjasone{
141774912Sjhb	struct witness_child_list_entry *wcl;
141874912Sjhb	int i, j;
141965557Sjasone
142074912Sjhb	if (isitmychild(parent, child))
142174912Sjhb		return (1);
142274912Sjhb	j = 0;
142374912Sjhb	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
142467352Sjhb		MPASS(j < 1000);
142574912Sjhb		for (i = 0; i < wcl->wcl_count; i++) {
142674912Sjhb			if (isitmydescendant(wcl->wcl_children[i], child))
142765557Sjasone				return (1);
142865557Sjasone		}
142974912Sjhb		j++;
143065557Sjasone	}
143165557Sjasone	return (0);
143265557Sjasone}
143365557Sjasone
1434104094Sphkstatic void
143565557Sjasonewitness_levelall (void)
143665557Sjasone{
143774912Sjhb	struct witness_list *list;
143865856Sjhb	struct witness *w, *w1;
143965557Sjasone
144074912Sjhb	/*
144174912Sjhb	 * First clear all levels.
144274912Sjhb	 */
144374912Sjhb	STAILQ_FOREACH(w, &w_all, w_list) {
144474912Sjhb		w->w_level = 0;
144574912Sjhb	}
144674912Sjhb
144774912Sjhb	/*
144874912Sjhb	 * Look for locks with no parent and level all their descendants.
144974912Sjhb	 */
145074912Sjhb	STAILQ_FOREACH(w, &w_all, w_list) {
145174912Sjhb		/*
145274912Sjhb		 * This is just an optimization, technically we could get
145374912Sjhb		 * away just walking the all list each time.
145474912Sjhb		 */
145574912Sjhb		if (w->w_class->lc_flags & LC_SLEEPLOCK)
145674912Sjhb			list = &w_sleep;
145774912Sjhb		else
145874912Sjhb			list = &w_spin;
145974912Sjhb		STAILQ_FOREACH(w1, list, w_typelist) {
146065557Sjasone			if (isitmychild(w1, w))
146174912Sjhb				goto skip;
146265557Sjasone		}
146365557Sjasone		witness_leveldescendents(w, 0);
146474912Sjhb	skip:
146595541Smarcel		;	/* silence GCC 3.x */
146665557Sjasone	}
146765557Sjasone}
146865557Sjasone
146965557Sjasonestatic void
147065856Sjhbwitness_leveldescendents(struct witness *parent, int level)
147165557Sjasone{
147274912Sjhb	struct witness_child_list_entry *wcl;
147365557Sjasone	int i;
147465557Sjasone
147565557Sjasone	if (parent->w_level < level)
147665557Sjasone		parent->w_level = level;
147765557Sjasone	level++;
147874912Sjhb	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
147974912Sjhb		for (i = 0; i < wcl->wcl_count; i++)
148074912Sjhb			witness_leveldescendents(wcl->wcl_children[i], level);
148165557Sjasone}
148265557Sjasone
148365557Sjasonestatic void
148465856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...),
1485112118Sjhb			   struct witness *parent, int indent)
148665557Sjasone{
148774912Sjhb	struct witness_child_list_entry *wcl;
148874912Sjhb	int i, level;
148965557Sjasone
149095543Sjhb	level = parent->w_level;
149174912Sjhb	prnt("%-2d", level);
1492112118Sjhb	for (i = 0; i < indent; i++)
149365557Sjasone		prnt(" ");
1494112118Sjhb	if (parent->w_refcount > 0)
1495112118Sjhb		prnt("%s", parent->w_name);
1496112118Sjhb	else
1497112118Sjhb		prnt("(dead)");
1498112118Sjhb	if (parent->w_displayed) {
1499112118Sjhb		prnt(" -- (already displayed)\n");
1500112118Sjhb		return;
1501112118Sjhb	}
1502112118Sjhb	parent->w_displayed = 1;
150397948Sjhb	if (parent->w_refcount > 0) {
150497948Sjhb		if (parent->w_file != NULL)
1505112118Sjhb			prnt(" -- last acquired @ %s:%d", parent->w_file,
150697948Sjhb			    parent->w_line);
1507112118Sjhb	}
1508112118Sjhb	prnt("\n");
150974912Sjhb	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
151074912Sjhb		for (i = 0; i < wcl->wcl_count; i++)
151174912Sjhb			    witness_displaydescendants(prnt,
1512112118Sjhb				wcl->wcl_children[i], indent + 1);
151374912Sjhb}
151465557Sjasone
1515105508Sphk#ifdef BLESSING
151665557Sjasonestatic int
151765856Sjhbblessed(struct witness *w1, struct witness *w2)
151865557Sjasone{
151965557Sjasone	int i;
152065856Sjhb	struct witness_blessed *b;
152165557Sjasone
152265557Sjasone	for (i = 0; i < blessed_count; i++) {
152365557Sjasone		b = &blessed_list[i];
152474912Sjhb		if (strcmp(w1->w_name, b->b_lock1) == 0) {
152574912Sjhb			if (strcmp(w2->w_name, b->b_lock2) == 0)
152665557Sjasone				return (1);
152765557Sjasone			continue;
152865557Sjasone		}
152974912Sjhb		if (strcmp(w1->w_name, b->b_lock2) == 0)
153074912Sjhb			if (strcmp(w2->w_name, b->b_lock1) == 0)
153165557Sjasone				return (1);
153265557Sjasone	}
153365557Sjasone	return (0);
153465557Sjasone}
1535105508Sphk#endif
153665557Sjasone
153765856Sjhbstatic struct witness *
153874912Sjhbwitness_get(void)
153965557Sjasone{
154065856Sjhb	struct witness *w;
154165557Sjasone
1542112562Sjhb	if (witness_watch == 0) {
154376481Sjhb		mtx_unlock_spin(&w_mtx);
154476481Sjhb		return (NULL);
154576481Sjhb	}
154674912Sjhb	if (STAILQ_EMPTY(&w_free)) {
1547112562Sjhb		witness_watch = 0;
154874912Sjhb		mtx_unlock_spin(&w_mtx);
154974912Sjhb		printf("%s: witness exhausted\n", __func__);
155065557Sjasone		return (NULL);
155165557Sjasone	}
155274912Sjhb	w = STAILQ_FIRST(&w_free);
155374912Sjhb	STAILQ_REMOVE_HEAD(&w_free, w_list);
155465856Sjhb	bzero(w, sizeof(*w));
155565557Sjasone	return (w);
155665557Sjasone}
155765557Sjasone
155865557Sjasonestatic void
155965856Sjhbwitness_free(struct witness *w)
156065557Sjasone{
156174912Sjhb
156274912Sjhb	STAILQ_INSERT_HEAD(&w_free, w, w_list);
156365557Sjasone}
156465557Sjasone
156574912Sjhbstatic struct witness_child_list_entry *
156674912Sjhbwitness_child_get(void)
156765557Sjasone{
156874912Sjhb	struct witness_child_list_entry *wcl;
156965557Sjasone
1570112562Sjhb	if (witness_watch == 0) {
157176481Sjhb		mtx_unlock_spin(&w_mtx);
157276481Sjhb		return (NULL);
157376481Sjhb	}
157474912Sjhb	wcl = w_child_free;
157574912Sjhb	if (wcl == NULL) {
1576112562Sjhb		witness_watch = 0;
157774912Sjhb		mtx_unlock_spin(&w_mtx);
157874912Sjhb		printf("%s: witness exhausted\n", __func__);
157974912Sjhb		return (NULL);
158065557Sjasone	}
158174912Sjhb	w_child_free = wcl->wcl_next;
158274912Sjhb	bzero(wcl, sizeof(*wcl));
158374912Sjhb	return (wcl);
158474912Sjhb}
158569881Sjake
158674912Sjhbstatic void
158774912Sjhbwitness_child_free(struct witness_child_list_entry *wcl)
158874912Sjhb{
158974912Sjhb
159074912Sjhb	wcl->wcl_next = w_child_free;
159174912Sjhb	w_child_free = wcl;
159265557Sjasone}
159365557Sjasone
159474912Sjhbstatic struct lock_list_entry *
159574912Sjhbwitness_lock_list_get(void)
159674912Sjhb{
159774912Sjhb	struct lock_list_entry *lle;
159871709Sjhb
1599112562Sjhb	if (witness_watch == 0)
160076481Sjhb		return (NULL);
160174912Sjhb	mtx_lock_spin(&w_mtx);
160274912Sjhb	lle = w_lock_list_free;
160374912Sjhb	if (lle == NULL) {
1604112562Sjhb		witness_watch = 0;
160574912Sjhb		mtx_unlock_spin(&w_mtx);
160674912Sjhb		printf("%s: witness exhausted\n", __func__);
160774912Sjhb		return (NULL);
160874912Sjhb	}
160974912Sjhb	w_lock_list_free = lle->ll_next;
161074912Sjhb	mtx_unlock_spin(&w_mtx);
161174912Sjhb	bzero(lle, sizeof(*lle));
161274912Sjhb	return (lle);
161374912Sjhb}
161474912Sjhb
161574912Sjhbstatic void
161674912Sjhbwitness_lock_list_free(struct lock_list_entry *lle)
161771709Sjhb{
161871709Sjhb
161974912Sjhb	mtx_lock_spin(&w_mtx);
162074912Sjhb	lle->ll_next = w_lock_list_free;
162174912Sjhb	w_lock_list_free = lle;
162274912Sjhb	mtx_unlock_spin(&w_mtx);
162371709Sjhb}
162471709Sjhb
162576272Sjhbstatic struct lock_instance *
162676272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
162776272Sjhb{
162876272Sjhb	struct lock_list_entry *lle;
162976272Sjhb	struct lock_instance *instance;
163076272Sjhb	int i;
163176272Sjhb
163276272Sjhb	for (lle = lock_list; lle != NULL; lle = lle->ll_next)
163376272Sjhb		for (i = lle->ll_count - 1; i >= 0; i--) {
163476272Sjhb			instance = &lle->ll_children[i];
163576272Sjhb			if (instance->li_lock == lock)
163676272Sjhb				return (instance);
163776272Sjhb		}
163876272Sjhb	return (NULL);
163976272Sjhb}
164076272Sjhb
1641111881Sjhbstatic void
1642111881Sjhbwitness_list_lock(struct lock_instance *instance)
1643111881Sjhb{
1644111881Sjhb	struct lock_object *lock;
1645111881Sjhb
1646111881Sjhb	lock = instance->li_lock;
1647111881Sjhb	printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1648111881Sjhb	    "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name);
1649111881Sjhb	if (lock->lo_type != lock->lo_name)
1650111881Sjhb		printf(" (%s)", lock->lo_type);
1651111881Sjhb	printf(" r = %d (%p) locked @ %s:%d\n",
1652111881Sjhb	    instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
1653111881Sjhb	    instance->li_line);
1654111881Sjhb}
1655111881Sjhb
165674912Sjhbint
165775273Sjhbwitness_list_locks(struct lock_list_entry **lock_list)
165872224Sjhb{
165975273Sjhb	struct lock_list_entry *lle;
166074912Sjhb	int i, nheld;
166172224Sjhb
166274912Sjhb	nheld = 0;
166374912Sjhb	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
166474912Sjhb		for (i = lle->ll_count - 1; i >= 0; i--) {
1665111881Sjhb			witness_list_lock(&lle->ll_children[i]);
166674912Sjhb			nheld++;
166774912Sjhb		}
166875273Sjhb	return (nheld);
166975273Sjhb}
167075273Sjhb
1671118271Sjhb/*
1672118271Sjhb * This is a bit risky at best.  We call this function when we have timed
1673118271Sjhb * out acquiring a spin lock, and we assume that the other CPU is stuck
1674118271Sjhb * with this lock held.  So, we go groveling around in the other CPU's
1675118271Sjhb * per-cpu data to try to find the lock instance for this spin lock to
1676118271Sjhb * see when it was last acquired.
1677118271Sjhb */
167865557Sjasonevoid
1679118271Sjhbwitness_display_spinlock(struct lock_object *lock, struct thread *owner)
1680118271Sjhb{
1681118271Sjhb	struct lock_instance *instance;
1682118271Sjhb	struct pcpu *pc;
1683118271Sjhb
1684118271Sjhb	if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
1685118271Sjhb		return;
1686118271Sjhb	pc = pcpu_find(owner->td_oncpu);
1687118271Sjhb	instance = find_instance(pc->pc_spinlocks, lock);
1688118271Sjhb	if (instance != NULL)
1689118271Sjhb		witness_list_lock(instance);
1690118271Sjhb}
1691118271Sjhb
1692118271Sjhbvoid
169374912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep)
169465557Sjasone{
169576272Sjhb	struct lock_instance *instance;
169671320Sjasone
169782284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1698112562Sjhb	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
169971352Sjasone		return;
170082243Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
170182243Sjhb		panic("%s: lock (%s) %s is not a sleep lock", __func__,
170282243Sjhb		    lock->lo_class->lc_name, lock->lo_name);
170383366Sjulian	instance = find_instance(curthread->td_sleeplocks, lock);
170482243Sjhb	if (instance == NULL)
170582243Sjhb		panic("%s: lock (%s) %s not locked", __func__,
170682243Sjhb		    lock->lo_class->lc_name, lock->lo_name);
170776272Sjhb	*filep = instance->li_file;
170876272Sjhb	*linep = instance->li_line;
170965557Sjasone}
171065557Sjasone
171165557Sjasonevoid
171274912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line)
171365557Sjasone{
171476272Sjhb	struct lock_instance *instance;
171571320Sjasone
171682284Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1717112562Sjhb	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
171871352Sjasone		return;
171982243Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
172082243Sjhb		panic("%s: lock (%s) %s is not a sleep lock", __func__,
172182243Sjhb		    lock->lo_class->lc_name, lock->lo_name);
172283366Sjulian	instance = find_instance(curthread->td_sleeplocks, lock);
172382243Sjhb	if (instance == NULL)
172482243Sjhb		panic("%s: lock (%s) %s not locked", __func__,
172582243Sjhb		    lock->lo_class->lc_name, lock->lo_name);
172674912Sjhb	lock->lo_witness->w_file = file;
172774912Sjhb	lock->lo_witness->w_line = line;
172876272Sjhb	instance->li_file = file;
172976272Sjhb	instance->li_line = line;
173065557Sjasone}
173165557Sjasone
173278871Sjhbvoid
173378871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line)
173478871Sjhb{
173578871Sjhb#ifdef INVARIANT_SUPPORT
173678871Sjhb	struct lock_instance *instance;
173778871Sjhb
1738112562Sjhb	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
173978941Sjhb		return;
174078871Sjhb	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0)
174183366Sjulian		instance = find_instance(curthread->td_sleeplocks, lock);
174278871Sjhb	else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0)
174378871Sjhb		instance = find_instance(PCPU_GET(spinlocks), lock);
174486422Sjhb	else {
174578871Sjhb		panic("Lock (%s) %s is not sleep or spin!",
174678871Sjhb		    lock->lo_class->lc_name, lock->lo_name);
174786422Sjhb	}
1748112116Sjhb	file = fixup_filename(file);
174978871Sjhb	switch (flags) {
175078871Sjhb	case LA_UNLOCKED:
175178871Sjhb		if (instance != NULL)
175278871Sjhb			panic("Lock (%s) %s locked @ %s:%d.",
175378871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
175478871Sjhb		break;
175578871Sjhb	case LA_LOCKED:
175678871Sjhb	case LA_LOCKED | LA_RECURSED:
175778871Sjhb	case LA_LOCKED | LA_NOTRECURSED:
175878871Sjhb	case LA_SLOCKED:
175978871Sjhb	case LA_SLOCKED | LA_RECURSED:
176078871Sjhb	case LA_SLOCKED | LA_NOTRECURSED:
176178871Sjhb	case LA_XLOCKED:
176278871Sjhb	case LA_XLOCKED | LA_RECURSED:
176378871Sjhb	case LA_XLOCKED | LA_NOTRECURSED:
176486422Sjhb		if (instance == NULL) {
176578871Sjhb			panic("Lock (%s) %s not locked @ %s:%d.",
176678871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
176786422Sjhb			break;
176886422Sjhb		}
176978871Sjhb		if ((flags & LA_XLOCKED) != 0 &&
177078871Sjhb		    (instance->li_flags & LI_EXCLUSIVE) == 0)
177178871Sjhb			panic("Lock (%s) %s not exclusively locked @ %s:%d.",
177278871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
177378871Sjhb		if ((flags & LA_SLOCKED) != 0 &&
177478871Sjhb		    (instance->li_flags & LI_EXCLUSIVE) != 0)
177578871Sjhb			panic("Lock (%s) %s exclusively locked @ %s:%d.",
177678871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
177778871Sjhb		if ((flags & LA_RECURSED) != 0 &&
177878871Sjhb		    (instance->li_flags & LI_RECURSEMASK) == 0)
177978871Sjhb			panic("Lock (%s) %s not recursed @ %s:%d.",
178078871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
178178871Sjhb		if ((flags & LA_NOTRECURSED) != 0 &&
178278871Sjhb		    (instance->li_flags & LI_RECURSEMASK) != 0)
178378871Sjhb			panic("Lock (%s) %s recursed @ %s:%d.",
178478871Sjhb			    lock->lo_class->lc_name, lock->lo_name, file, line);
178578871Sjhb		break;
178678871Sjhb	default:
178778871Sjhb		panic("Invalid lock assertion at %s:%d.", file, line);
178878871Sjhb
178978871Sjhb	}
179078871Sjhb#endif	/* INVARIANT_SUPPORT */
179178871Sjhb}
179278871Sjhb
179374912Sjhb#ifdef DDB
1794112061Sjhbstatic void
1795112061Sjhbwitness_list(struct thread *td)
1796112061Sjhb{
179774912Sjhb
1798112061Sjhb	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1799112061Sjhb	KASSERT(db_active, ("%s: not in the debugger", __func__));
1800112061Sjhb
1801112562Sjhb	if (witness_watch == 0)
1802112061Sjhb		return;
1803112061Sjhb
1804112061Sjhb	witness_list_locks(&td->td_sleeplocks);
1805112061Sjhb
1806112061Sjhb	/*
1807112061Sjhb	 * We only handle spinlocks if td == curthread.  This is somewhat broken
1808112061Sjhb	 * if td is currently executing on some other CPU and holds spin locks
1809112061Sjhb	 * as we won't display those locks.  If we had a MI way of getting
1810112061Sjhb	 * the per-cpu data for a given cpu then we could use
1811113339Sjulian	 * td->td_oncpu to get the list of spinlocks for this thread
1812112061Sjhb	 * and "fix" this.
1813112061Sjhb	 *
1814112061Sjhb	 * That still wouldn't really fix this unless we locked sched_lock
1815112061Sjhb	 * or stopped the other CPU to make sure it wasn't changing the list
1816112061Sjhb	 * out from under us.  It is probably best to just not try to handle
1817112061Sjhb	 * threads on other CPU's for now.
1818112061Sjhb	 */
1819112061Sjhb	if (td == curthread && PCPU_GET(spinlocks) != NULL)
1820112061Sjhb		witness_list_locks(PCPU_PTR(spinlocks));
1821112061Sjhb}
1822112061Sjhb
182374930SjhbDB_SHOW_COMMAND(locks, db_witness_list)
182474912Sjhb{
182583366Sjulian	struct thread *td;
182683366Sjulian	pid_t pid;
182775273Sjhb	struct proc *p;
182874912Sjhb
182975273Sjhb	if (have_addr) {
183075273Sjhb		pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
183175273Sjhb		    ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
183275273Sjhb		    ((addr >> 16) % 16) * 10000;
183375273Sjhb		/* sx_slock(&allproc_lock); */
183483366Sjulian		FOREACH_PROC_IN_SYSTEM(p) {
183575273Sjhb			if (p->p_pid == pid)
183675273Sjhb				break;
183775273Sjhb		}
183875273Sjhb		/* sx_sunlock(&allproc_lock); */
183975273Sjhb		if (p == NULL) {
184075273Sjhb			db_printf("pid %d not found\n", pid);
184175273Sjhb			return;
184275273Sjhb		}
184390361Sjulian		FOREACH_THREAD_IN_PROC(p, td) {
184490361Sjulian			witness_list(td);
184590361Sjulian		}
184683366Sjulian	} else {
184783366Sjulian		td = curthread;
184890361Sjulian		witness_list(td);
184983366Sjulian	}
185074912Sjhb}
185174912Sjhb
185274912SjhbDB_SHOW_COMMAND(witness, db_witness_display)
185374912Sjhb{
185474912Sjhb
185574912Sjhb	witness_display(db_printf);
185674912Sjhb}
185774912Sjhb#endif
1858