subr_witness.c revision 149738
1/*-
2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 *    promote products derived from this software without specific prior
14 *    written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
29 *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
30 */
31
32/*
33 * Implementation of the `witness' lock verifier.  Originally implemented for
34 * mutexes in BSD/OS.  Extended to handle generic lock objects and lock
35 * classes in FreeBSD.
36 */
37
38/*
39 *	Main Entry: witness
40 *	Pronunciation: 'wit-n&s
41 *	Function: noun
42 *	Etymology: Middle English witnesse, from Old English witnes knowledge,
43 *	    testimony, witness, from 2wit
44 *	Date: before 12th century
45 *	1 : attestation of a fact or event : TESTIMONY
46 *	2 : one that gives evidence; specifically : one who testifies in
47 *	    a cause or before a judicial tribunal
48 *	3 : one asked to be present at a transaction so as to be able to
49 *	    testify to its having taken place
50 *	4 : one who has personal knowledge of something
51 *	5 a : something serving as evidence or proof : SIGN
52 *	  b : public affirmation by word or example of usually
53 *	      religious faith or conviction <the heroic witness to divine
54 *	      life -- Pilot>
55 *	6 capitalized : a member of the Jehovah's Witnesses
56 */
57
58/*
59 * Special rules concerning Giant and lock orders:
60 *
61 * 1) Giant must be acquired before any other mutexes.  Stated another way,
62 *    no other mutex may be held when Giant is acquired.
63 *
64 * 2) Giant must be released when blocking on a sleepable lock.
65 *
66 * This rule is less obvious, but is a result of Giant providing the same
67 * semantics as spl().  Basically, when a thread sleeps, it must release
68 * Giant.  When a thread blocks on a sleepable lock, it sleeps.  Hence rule
69 * 2).
70 *
71 * 3) Giant may be acquired before or after sleepable locks.
72 *
73 * This rule is also not quite as obvious.  Giant may be acquired after
74 * a sleepable lock because it is a non-sleepable lock and non-sleepable
75 * locks may always be acquired while holding a sleepable lock.  The second
76 * case, Giant before a sleepable lock, follows from rule 2) above.  Suppose
77 * you have two threads T1 and T2 and a sleepable lock X.  Suppose that T1
78 * acquires X and blocks on Giant.  Then suppose that T2 acquires Giant and
79 * blocks on X.  When T2 blocks on X, T2 will release Giant allowing T1 to
80 * execute.  Thus, acquiring Giant both before and after a sleepable lock
81 * will not result in a lock order reversal.
82 */
83
84#include <sys/cdefs.h>
85__FBSDID("$FreeBSD: head/sys/kern/subr_witness.c 149738 2005-09-02 20:23:49Z jhb $");
86
87#include "opt_ddb.h"
88#include "opt_witness.h"
89
90#include <sys/param.h>
91#include <sys/bus.h>
92#include <sys/kdb.h>
93#include <sys/kernel.h>
94#include <sys/ktr.h>
95#include <sys/lock.h>
96#include <sys/malloc.h>
97#include <sys/mutex.h>
98#include <sys/proc.h>
99#include <sys/sysctl.h>
100#include <sys/systm.h>
101
102#include <ddb/ddb.h>
103
104#include <machine/stdarg.h>
105
106/* Define this to check for blessed mutexes */
107#undef BLESSING
108
109#define WITNESS_COUNT 1024
110#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
111/*
112 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads
113 * will hold LOCK_NCHILDREN * 2 locks.  We handle failure ok, and we should
114 * probably be safe for the most part, but it's still a SWAG.
115 */
116#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
117
118#define	WITNESS_NCHILDREN 6
119
120struct witness_child_list_entry;
121
122struct witness {
123	const	char *w_name;
124	struct	lock_class *w_class;
125	STAILQ_ENTRY(witness) w_list;		/* List of all witnesses. */
126	STAILQ_ENTRY(witness) w_typelist;	/* Witnesses of a type. */
127	struct	witness_child_list_entry *w_children;	/* Great evilness... */
128	const	char *w_file;
129	int	w_line;
130	u_int	w_level;
131	u_int	w_refcount;
132	u_char	w_Giant_squawked:1;
133	u_char	w_other_squawked:1;
134	u_char	w_same_squawked:1;
135	u_char	w_displayed:1;
136};
137
138struct witness_child_list_entry {
139	struct	witness_child_list_entry *wcl_next;
140	struct	witness *wcl_children[WITNESS_NCHILDREN];
141	u_int	wcl_count;
142};
143
144STAILQ_HEAD(witness_list, witness);
145
146#ifdef BLESSING
147struct witness_blessed {
148	const	char *b_lock1;
149	const	char *b_lock2;
150};
151#endif
152
153struct witness_order_list_entry {
154	const	char *w_name;
155	struct	lock_class *w_class;
156};
157
158#ifdef BLESSING
159static int	blessed(struct witness *, struct witness *);
160#endif
161static int	depart(struct witness *w);
162static struct	witness *enroll(const char *description,
163				struct lock_class *lock_class);
164static int	insertchild(struct witness *parent, struct witness *child);
165static int	isitmychild(struct witness *parent, struct witness *child);
166static int	isitmydescendant(struct witness *parent, struct witness *child);
167static int	itismychild(struct witness *parent, struct witness *child);
168static void	removechild(struct witness *parent, struct witness *child);
169static int	sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
170static void	witness_displaydescendants(void(*)(const char *fmt, ...),
171					   struct witness *, int indent);
172static const char *fixup_filename(const char *file);
173static void	witness_leveldescendents(struct witness *parent, int level);
174static void	witness_levelall(void);
175static struct	witness *witness_get(void);
176static void	witness_free(struct witness *m);
177static struct	witness_child_list_entry *witness_child_get(void);
178static void	witness_child_free(struct witness_child_list_entry *wcl);
179static struct	lock_list_entry *witness_lock_list_get(void);
180static void	witness_lock_list_free(struct lock_list_entry *lle);
181static struct	lock_instance *find_instance(struct lock_list_entry *lock_list,
182					     struct lock_object *lock);
183static void	witness_list_lock(struct lock_instance *instance);
184#ifdef DDB
185static void	witness_list(struct thread *td);
186static void	witness_display_list(void(*prnt)(const char *fmt, ...),
187				     struct witness_list *list);
188static void	witness_display(void(*)(const char *fmt, ...));
189#endif
190
191SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking");
192
193/*
194 * If set to 0, witness is disabled.  If set to a non-zero value, witness
195 * performs full lock order checking for all locks.  At runtime, this
196 * value may be set to 0 to turn off witness.  witness is not allowed be
197 * turned on once it is turned off, however.
198 */
199static int witness_watch = 1;
200TUNABLE_INT("debug.witness.watch", &witness_watch);
201SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
202    sysctl_debug_witness_watch, "I", "witness is watching lock operations");
203
204#ifdef KDB
205/*
206 * When KDB is enabled and witness_kdb is set to 1, it will cause the system
207 * to drop into kdebug() when:
208 *	- a lock heirarchy violation occurs
209 *	- locks are held when going to sleep.
210 */
211#ifdef WITNESS_KDB
212int	witness_kdb = 1;
213#else
214int	witness_kdb = 0;
215#endif
216TUNABLE_INT("debug.witness.kdb", &witness_kdb);
217SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, "");
218
219/*
220 * When KDB is enabled and witness_trace is set to 1, it will cause the system
221 * to print a stack trace:
222 *	- a lock heirarchy violation occurs
223 *	- locks are held when going to sleep.
224 */
225int	witness_trace = 1;
226TUNABLE_INT("debug.witness.trace", &witness_trace);
227SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, "");
228#endif /* KDB */
229
230#ifdef WITNESS_SKIPSPIN
231int	witness_skipspin = 1;
232#else
233int	witness_skipspin = 0;
234#endif
235TUNABLE_INT("debug.witness.skipspin", &witness_skipspin);
236SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN,
237    &witness_skipspin, 0, "");
238
239static struct mtx w_mtx;
240static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
241static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
242static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
243static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
244static struct witness_child_list_entry *w_child_free = NULL;
245static struct lock_list_entry *w_lock_list_free = NULL;
246
247static int w_free_cnt, w_spin_cnt, w_sleep_cnt, w_child_free_cnt, w_child_cnt;
248SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, "");
249SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, "");
250SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0,
251    "");
252SYSCTL_INT(_debug_witness, OID_AUTO, child_free_cnt, CTLFLAG_RD,
253    &w_child_free_cnt, 0, "");
254SYSCTL_INT(_debug_witness, OID_AUTO, child_cnt, CTLFLAG_RD, &w_child_cnt, 0,
255    "");
256
257static struct witness w_data[WITNESS_COUNT];
258static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
259static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
260
261static struct witness_order_list_entry order_lists[] = {
262	/*
263	 * sx locks
264	 */
265	{ "proctree", &lock_class_sx },
266	{ "allproc", &lock_class_sx },
267	{ NULL, NULL },
268	/*
269	 * Various mutexes
270	 */
271	{ "Giant", &lock_class_mtx_sleep },
272	{ "filedesc structure", &lock_class_mtx_sleep },
273	{ "pipe mutex", &lock_class_mtx_sleep },
274	{ "sigio lock", &lock_class_mtx_sleep },
275	{ "process group", &lock_class_mtx_sleep },
276	{ "process lock", &lock_class_mtx_sleep },
277	{ "session", &lock_class_mtx_sleep },
278	{ "uidinfo hash", &lock_class_mtx_sleep },
279	{ "uidinfo struct", &lock_class_mtx_sleep },
280	{ "allprison", &lock_class_mtx_sleep },
281	{ NULL, NULL },
282	/*
283	 * Sockets
284	 */
285	{ "filedesc structure", &lock_class_mtx_sleep },
286	{ "accept", &lock_class_mtx_sleep },
287	{ "so_snd", &lock_class_mtx_sleep },
288	{ "so_rcv", &lock_class_mtx_sleep },
289	{ "sellck", &lock_class_mtx_sleep },
290	{ NULL, NULL },
291	/*
292	 * Routing
293	 */
294	{ "so_rcv", &lock_class_mtx_sleep },
295	{ "radix node head", &lock_class_mtx_sleep },
296	{ "rtentry", &lock_class_mtx_sleep },
297	{ "ifaddr", &lock_class_mtx_sleep },
298	{ NULL, NULL },
299	/*
300	 * Multicast - protocol locks before interface locks, after UDP locks.
301	 */
302	{ "udpinp", &lock_class_mtx_sleep },
303	{ "in_multi_mtx", &lock_class_mtx_sleep },
304	{ "igmp_mtx", &lock_class_mtx_sleep },
305	{ "if_addr_mtx", &lock_class_mtx_sleep },
306	{ NULL, NULL },
307	/*
308	 * UNIX Domain Sockets
309	 */
310	{ "unp", &lock_class_mtx_sleep },
311	{ "so_snd", &lock_class_mtx_sleep },
312	{ NULL, NULL },
313	/*
314	 * UDP/IP
315	 */
316	{ "udp", &lock_class_mtx_sleep },
317	{ "udpinp", &lock_class_mtx_sleep },
318	{ "so_snd", &lock_class_mtx_sleep },
319	{ NULL, NULL },
320	/*
321	 * TCP/IP
322	 */
323	{ "tcp", &lock_class_mtx_sleep },
324	{ "tcpinp", &lock_class_mtx_sleep },
325	{ "so_snd", &lock_class_mtx_sleep },
326	{ NULL, NULL },
327	/*
328	 * SLIP
329	 */
330	{ "slip_mtx", &lock_class_mtx_sleep },
331	{ "slip sc_mtx", &lock_class_mtx_sleep },
332	{ NULL, NULL },
333	/*
334	 * netatalk
335	 */
336	{ "ddp_list_mtx", &lock_class_mtx_sleep },
337	{ "ddp_mtx", &lock_class_mtx_sleep },
338	{ NULL, NULL },
339	/*
340	 * BPF
341	 */
342	{ "bpf global lock", &lock_class_mtx_sleep },
343	{ "bpf interface lock", &lock_class_mtx_sleep },
344	{ "bpf cdev lock", &lock_class_mtx_sleep },
345	{ NULL, NULL },
346	/*
347	 * NFS server
348	 */
349	{ "nfsd_mtx", &lock_class_mtx_sleep },
350	{ "so_snd", &lock_class_mtx_sleep },
351	{ NULL, NULL },
352	/*
353	 * CDEV
354	 */
355	{ "system map", &lock_class_mtx_sleep },
356	{ "vm page queue mutex", &lock_class_mtx_sleep },
357	{ "vnode interlock", &lock_class_mtx_sleep },
358	{ "cdev", &lock_class_mtx_sleep },
359	{ NULL, NULL },
360	/*
361	 * spin locks
362	 */
363#ifdef SMP
364	{ "ap boot", &lock_class_mtx_spin },
365#endif
366	{ "sio", &lock_class_mtx_spin },
367#ifdef __i386__
368	{ "cy", &lock_class_mtx_spin },
369#endif
370	{ "uart_hwmtx", &lock_class_mtx_spin },
371	{ "sabtty", &lock_class_mtx_spin },
372	{ "zstty", &lock_class_mtx_spin },
373	{ "ng_node", &lock_class_mtx_spin },
374	{ "ng_worklist", &lock_class_mtx_spin },
375	{ "taskqueue_fast", &lock_class_mtx_spin },
376	{ "intr table", &lock_class_mtx_spin },
377	{ "ithread table lock", &lock_class_mtx_spin },
378	{ "sleepq chain", &lock_class_mtx_spin },
379	{ "sched lock", &lock_class_mtx_spin },
380	{ "turnstile chain", &lock_class_mtx_spin },
381	{ "td_contested", &lock_class_mtx_spin },
382	{ "callout", &lock_class_mtx_spin },
383	{ "entropy harvest mutex", &lock_class_mtx_spin },
384	/*
385	 * leaf locks
386	 */
387	{ "allpmaps", &lock_class_mtx_spin },
388	{ "vm page queue free mutex", &lock_class_mtx_spin },
389	{ "icu", &lock_class_mtx_spin },
390#ifdef SMP
391	{ "smp rendezvous", &lock_class_mtx_spin },
392#if defined(__i386__) || defined(__amd64__)
393	{ "tlb", &lock_class_mtx_spin },
394#endif
395#ifdef __sparc64__
396	{ "ipi", &lock_class_mtx_spin },
397	{ "rtc_mtx", &lock_class_mtx_spin },
398#endif
399#endif
400	{ "clk", &lock_class_mtx_spin },
401	{ "mutex profiling lock", &lock_class_mtx_spin },
402	{ "kse zombie lock", &lock_class_mtx_spin },
403	{ "ALD Queue", &lock_class_mtx_spin },
404#ifdef __ia64__
405	{ "MCA spin lock", &lock_class_mtx_spin },
406#endif
407#if defined(__i386__) || defined(__amd64__)
408	{ "pcicfg", &lock_class_mtx_spin },
409	{ "NDIS thread lock", &lock_class_mtx_spin },
410#endif
411	{ "tw_osl_io_lock", &lock_class_mtx_spin },
412	{ "tw_osl_q_lock", &lock_class_mtx_spin },
413	{ "tw_cl_io_lock", &lock_class_mtx_spin },
414	{ "tw_cl_intr_lock", &lock_class_mtx_spin },
415	{ "tw_cl_gen_lock", &lock_class_mtx_spin },
416	{ NULL, NULL },
417	{ NULL, NULL }
418};
419
420#ifdef BLESSING
421/*
422 * Pairs of locks which have been blessed
423 * Don't complain about order problems with blessed locks
424 */
425static struct witness_blessed blessed_list[] = {
426};
427static int blessed_count =
428	sizeof(blessed_list) / sizeof(struct witness_blessed);
429#endif
430
431/*
432 * List of all locks in the system.
433 */
434TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks);
435
436static struct mtx all_mtx = {
437	{ &lock_class_mtx_sleep,	/* mtx_object.lo_class */
438	  "All locks list",		/* mtx_object.lo_name */
439	  "All locks list",		/* mtx_object.lo_type */
440	  LO_INITIALIZED,		/* mtx_object.lo_flags */
441	  { NULL, NULL },		/* mtx_object.lo_list */
442	  NULL },			/* mtx_object.lo_witness */
443	MTX_UNOWNED, 0			/* mtx_lock, mtx_recurse */
444};
445
446/*
447 * This global is set to 0 once it becomes safe to use the witness code.
448 */
449static int witness_cold = 1;
450
451/*
452 * Global variables for book keeping.
453 */
454static int lock_cur_cnt;
455static int lock_max_cnt;
456
457/*
458 * The WITNESS-enabled diagnostic code.
459 */
460static void
461witness_initialize(void *dummy __unused)
462{
463	struct lock_object *lock;
464	struct witness_order_list_entry *order;
465	struct witness *w, *w1;
466	int i;
467
468	/*
469	 * We have to release Giant before initializing its witness
470	 * structure so that WITNESS doesn't get confused.
471	 */
472	mtx_unlock(&Giant);
473	mtx_assert(&Giant, MA_NOTOWNED);
474
475	CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
476	TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
477	mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
478	    MTX_NOWITNESS);
479	for (i = 0; i < WITNESS_COUNT; i++)
480		witness_free(&w_data[i]);
481	for (i = 0; i < WITNESS_CHILDCOUNT; i++)
482		witness_child_free(&w_childdata[i]);
483	for (i = 0; i < LOCK_CHILDCOUNT; i++)
484		witness_lock_list_free(&w_locklistdata[i]);
485
486	/* First add in all the specified order lists. */
487	for (order = order_lists; order->w_name != NULL; order++) {
488		w = enroll(order->w_name, order->w_class);
489		if (w == NULL)
490			continue;
491		w->w_file = "order list";
492		for (order++; order->w_name != NULL; order++) {
493			w1 = enroll(order->w_name, order->w_class);
494			if (w1 == NULL)
495				continue;
496			w1->w_file = "order list";
497			if (!itismychild(w, w1))
498				panic("Not enough memory for static orders!");
499			w = w1;
500		}
501	}
502
503	/* Iterate through all locks and add them to witness. */
504	mtx_lock(&all_mtx);
505	TAILQ_FOREACH(lock, &all_locks, lo_list) {
506		if (lock->lo_flags & LO_WITNESS)
507			lock->lo_witness = enroll(lock->lo_type,
508			    lock->lo_class);
509		else
510			lock->lo_witness = NULL;
511	}
512	mtx_unlock(&all_mtx);
513
514	/* Mark the witness code as being ready for use. */
515	atomic_store_rel_int(&witness_cold, 0);
516
517	mtx_lock(&Giant);
518}
519SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
520
521static int
522sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
523{
524	int error, value;
525
526	value = witness_watch;
527	error = sysctl_handle_int(oidp, &value, 0, req);
528	if (error != 0 || req->newptr == NULL)
529		return (error);
530	error = suser(req->td);
531	if (error != 0)
532		return (error);
533	if (value == witness_watch)
534		return (0);
535	if (value != 0)
536		return (EINVAL);
537	witness_watch = 0;
538	return (0);
539}
540
541void
542witness_init(struct lock_object *lock)
543{
544	struct lock_class *class;
545
546	class = lock->lo_class;
547	if (lock->lo_flags & LO_INITIALIZED)
548		panic("%s: lock (%s) %s is already initialized", __func__,
549		    class->lc_name, lock->lo_name);
550	if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
551	    (class->lc_flags & LC_RECURSABLE) == 0)
552		panic("%s: lock (%s) %s can not be recursable", __func__,
553		    class->lc_name, lock->lo_name);
554	if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
555	    (class->lc_flags & LC_SLEEPABLE) == 0)
556		panic("%s: lock (%s) %s can not be sleepable", __func__,
557		    class->lc_name, lock->lo_name);
558	if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
559	    (class->lc_flags & LC_UPGRADABLE) == 0)
560		panic("%s: lock (%s) %s can not be upgradable", __func__,
561		    class->lc_name, lock->lo_name);
562
563	mtx_lock(&all_mtx);
564	TAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
565	lock->lo_flags |= LO_INITIALIZED;
566	lock_cur_cnt++;
567	if (lock_cur_cnt > lock_max_cnt)
568		lock_max_cnt = lock_cur_cnt;
569	mtx_unlock(&all_mtx);
570	if (!witness_cold && witness_watch != 0 && panicstr == NULL &&
571	    (lock->lo_flags & LO_WITNESS) != 0)
572		lock->lo_witness = enroll(lock->lo_type, class);
573	else
574		lock->lo_witness = NULL;
575}
576
577void
578witness_destroy(struct lock_object *lock)
579{
580	struct witness *w;
581
582	if (witness_cold)
583		panic("lock (%s) %s destroyed while witness_cold",
584		    lock->lo_class->lc_name, lock->lo_name);
585	if ((lock->lo_flags & LO_INITIALIZED) == 0)
586		panic("%s: lock (%s) %s is not initialized", __func__,
587		    lock->lo_class->lc_name, lock->lo_name);
588
589	/* XXX: need to verify that no one holds the lock */
590	w = lock->lo_witness;
591	if (w != NULL) {
592		mtx_lock_spin(&w_mtx);
593		MPASS(w->w_refcount > 0);
594		w->w_refcount--;
595
596		/*
597		 * Lock is already released if we have an allocation failure
598		 * and depart() fails.
599		 */
600		if (w->w_refcount != 0 || depart(w))
601			mtx_unlock_spin(&w_mtx);
602	}
603
604	mtx_lock(&all_mtx);
605	lock_cur_cnt--;
606	TAILQ_REMOVE(&all_locks, lock, lo_list);
607	lock->lo_flags &= ~LO_INITIALIZED;
608	mtx_unlock(&all_mtx);
609}
610
611#ifdef DDB
612static void
613witness_display_list(void(*prnt)(const char *fmt, ...),
614		     struct witness_list *list)
615{
616	struct witness *w;
617
618	STAILQ_FOREACH(w, list, w_typelist) {
619		if (w->w_file == NULL || w->w_level > 0)
620			continue;
621		/*
622		 * This lock has no anscestors, display its descendants.
623		 */
624		witness_displaydescendants(prnt, w, 0);
625	}
626}
627
628static void
629witness_display(void(*prnt)(const char *fmt, ...))
630{
631	struct witness *w;
632
633	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
634	witness_levelall();
635
636	/* Clear all the displayed flags. */
637	STAILQ_FOREACH(w, &w_all, w_list) {
638		w->w_displayed = 0;
639	}
640
641	/*
642	 * First, handle sleep locks which have been acquired at least
643	 * once.
644	 */
645	prnt("Sleep locks:\n");
646	witness_display_list(prnt, &w_sleep);
647
648	/*
649	 * Now do spin locks which have been acquired at least once.
650	 */
651	prnt("\nSpin locks:\n");
652	witness_display_list(prnt, &w_spin);
653
654	/*
655	 * Finally, any locks which have not been acquired yet.
656	 */
657	prnt("\nLocks which were never acquired:\n");
658	STAILQ_FOREACH(w, &w_all, w_list) {
659		if (w->w_file != NULL || w->w_refcount == 0)
660			continue;
661		prnt("%s\n", w->w_name);
662	}
663}
664#endif /* DDB */
665
666/* Trim useless garbage from filenames. */
667static const char *
668fixup_filename(const char *file)
669{
670
671	if (file == NULL)
672		return (NULL);
673	while (strncmp(file, "../", 3) == 0)
674		file += 3;
675	return (file);
676}
677
678int
679witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
680{
681
682	if (witness_watch == 0 || panicstr != NULL)
683		return (0);
684
685	/* Require locks that witness knows about. */
686	if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
687	    lock2->lo_witness == NULL)
688		return (EINVAL);
689
690	MPASS(!mtx_owned(&w_mtx));
691	mtx_lock_spin(&w_mtx);
692
693	/*
694	 * If we already have either an explicit or implied lock order that
695	 * is the other way around, then return an error.
696	 */
697	if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
698		mtx_unlock_spin(&w_mtx);
699		return (EDOOFUS);
700	}
701
702	/* Try to add the new order. */
703	CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
704	    lock2->lo_type, lock1->lo_type);
705	if (!itismychild(lock1->lo_witness, lock2->lo_witness))
706		return (ENOMEM);
707	mtx_unlock_spin(&w_mtx);
708	return (0);
709}
710
711void
712witness_checkorder(struct lock_object *lock, int flags, const char *file,
713    int line)
714{
715	struct lock_list_entry **lock_list, *lle;
716	struct lock_instance *lock1, *lock2;
717	struct lock_class *class;
718	struct witness *w, *w1;
719	struct thread *td;
720	int i, j;
721
722	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
723	    panicstr != NULL)
724		return;
725
726	/*
727	 * Try locks do not block if they fail to acquire the lock, thus
728	 * there is no danger of deadlocks or of switching while holding a
729	 * spin lock if we acquire a lock via a try operation.  This
730	 * function shouldn't even be called for try locks, so panic if
731	 * that happens.
732	 */
733	if (flags & LOP_TRYLOCK)
734		panic("%s should not be called for try lock operations",
735		    __func__);
736
737	w = lock->lo_witness;
738	class = lock->lo_class;
739	td = curthread;
740	file = fixup_filename(file);
741
742	if (class->lc_flags & LC_SLEEPLOCK) {
743		/*
744		 * Since spin locks include a critical section, this check
745		 * implicitly enforces a lock order of all sleep locks before
746		 * all spin locks.
747		 */
748		if (td->td_critnest != 0 && !kdb_active)
749			panic("blockable sleep lock (%s) %s @ %s:%d",
750			    class->lc_name, lock->lo_name, file, line);
751
752		/*
753		 * If this is the first lock acquired then just return as
754		 * no order checking is needed.
755		 */
756		if (td->td_sleeplocks == NULL)
757			return;
758		lock_list = &td->td_sleeplocks;
759	} else {
760		/*
761		 * If this is the first lock, just return as no order
762		 * checking is needed.  We check this in both if clauses
763		 * here as unifying the check would require us to use a
764		 * critical section to ensure we don't migrate while doing
765		 * the check.  Note that if this is not the first lock, we
766		 * are already in a critical section and are safe for the
767		 * rest of the check.
768		 */
769		if (PCPU_GET(spinlocks) == NULL)
770			return;
771		lock_list = PCPU_PTR(spinlocks);
772	}
773
774	/*
775	 * Check to see if we are recursing on a lock we already own.  If
776	 * so, make sure that we don't mismatch exclusive and shared lock
777	 * acquires.
778	 */
779	lock1 = find_instance(*lock_list, lock);
780	if (lock1 != NULL) {
781		if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
782		    (flags & LOP_EXCLUSIVE) == 0) {
783			printf("shared lock of (%s) %s @ %s:%d\n",
784			    class->lc_name, lock->lo_name, file, line);
785			printf("while exclusively locked from %s:%d\n",
786			    lock1->li_file, lock1->li_line);
787			panic("share->excl");
788		}
789		if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
790		    (flags & LOP_EXCLUSIVE) != 0) {
791			printf("exclusive lock of (%s) %s @ %s:%d\n",
792			    class->lc_name, lock->lo_name, file, line);
793			printf("while share locked from %s:%d\n",
794			    lock1->li_file, lock1->li_line);
795			panic("excl->share");
796		}
797		return;
798	}
799
800	/*
801	 * Try locks do not block if they fail to acquire the lock, thus
802	 * there is no danger of deadlocks or of switching while holding a
803	 * spin lock if we acquire a lock via a try operation.
804	 */
805	if (flags & LOP_TRYLOCK)
806		return;
807
808	/*
809	 * Check for duplicate locks of the same type.  Note that we only
810	 * have to check for this on the last lock we just acquired.  Any
811	 * other cases will be caught as lock order violations.
812	 */
813	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
814	w1 = lock1->li_lock->lo_witness;
815	if (w1 == w) {
816		if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) ||
817		    (flags & LOP_DUPOK))
818			return;
819		w->w_same_squawked = 1;
820		printf("acquiring duplicate lock of same type: \"%s\"\n",
821			lock->lo_type);
822		printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name,
823		    lock1->li_file, lock1->li_line);
824		printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
825#ifdef KDB
826		goto debugger;
827#else
828		return;
829#endif
830	}
831	MPASS(!mtx_owned(&w_mtx));
832	mtx_lock_spin(&w_mtx);
833	/*
834	 * If we know that the the lock we are acquiring comes after
835	 * the lock we most recently acquired in the lock order tree,
836	 * then there is no need for any further checks.
837	 */
838	if (isitmychild(w1, w)) {
839		mtx_unlock_spin(&w_mtx);
840		return;
841	}
842	for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
843		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
844
845			MPASS(j < WITNESS_COUNT);
846			lock1 = &lle->ll_children[i];
847			w1 = lock1->li_lock->lo_witness;
848
849			/*
850			 * If this lock doesn't undergo witness checking,
851			 * then skip it.
852			 */
853			if (w1 == NULL) {
854				KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
855				    ("lock missing witness structure"));
856				continue;
857			}
858			/*
859			 * If we are locking Giant and this is a sleepable
860			 * lock, then skip it.
861			 */
862			if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
863			    lock == &Giant.mtx_object)
864				continue;
865			/*
866			 * If we are locking a sleepable lock and this lock
867			 * is Giant, then skip it.
868			 */
869			if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
870			    lock1->li_lock == &Giant.mtx_object)
871				continue;
872			/*
873			 * If we are locking a sleepable lock and this lock
874			 * isn't sleepable, we want to treat it as a lock
875			 * order violation to enfore a general lock order of
876			 * sleepable locks before non-sleepable locks.
877			 */
878			if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
879			    (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
880				goto reversal;
881			/*
882			 * Check the lock order hierarchy for a reveresal.
883			 */
884			if (!isitmydescendant(w, w1))
885				continue;
886		reversal:
887			/*
888			 * We have a lock order violation, check to see if it
889			 * is allowed or has already been yelled about.
890			 */
891			mtx_unlock_spin(&w_mtx);
892#ifdef BLESSING
893			/*
894			 * If the lock order is blessed, just bail.  We don't
895			 * look for other lock order violations though, which
896			 * may be a bug.
897			 */
898			if (blessed(w, w1))
899				return;
900#endif
901			if (lock1->li_lock == &Giant.mtx_object) {
902				if (w1->w_Giant_squawked)
903					return;
904				else
905					w1->w_Giant_squawked = 1;
906			} else {
907				if (w1->w_other_squawked)
908					return;
909				else
910					w1->w_other_squawked = 1;
911			}
912			/*
913			 * Ok, yell about it.
914			 */
915			printf("lock order reversal\n");
916			/*
917			 * Try to locate an earlier lock with
918			 * witness w in our list.
919			 */
920			do {
921				lock2 = &lle->ll_children[i];
922				MPASS(lock2->li_lock != NULL);
923				if (lock2->li_lock->lo_witness == w)
924					break;
925				if (i == 0 && lle->ll_next != NULL) {
926					lle = lle->ll_next;
927					i = lle->ll_count - 1;
928					MPASS(i >= 0 && i < LOCK_NCHILDREN);
929				} else
930					i--;
931			} while (i >= 0);
932			if (i < 0) {
933				printf(" 1st %p %s (%s) @ %s:%d\n",
934				    lock1->li_lock, lock1->li_lock->lo_name,
935				    lock1->li_lock->lo_type, lock1->li_file,
936				    lock1->li_line);
937				printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
938				    lock->lo_name, lock->lo_type, file, line);
939			} else {
940				printf(" 1st %p %s (%s) @ %s:%d\n",
941				    lock2->li_lock, lock2->li_lock->lo_name,
942				    lock2->li_lock->lo_type, lock2->li_file,
943				    lock2->li_line);
944				printf(" 2nd %p %s (%s) @ %s:%d\n",
945				    lock1->li_lock, lock1->li_lock->lo_name,
946				    lock1->li_lock->lo_type, lock1->li_file,
947				    lock1->li_line);
948				printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
949				    lock->lo_name, lock->lo_type, file, line);
950			}
951#ifdef KDB
952			goto debugger;
953#else
954			return;
955#endif
956		}
957	}
958	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
959	/*
960	 * If requested, build a new lock order.  However, don't build a new
961	 * relationship between a sleepable lock and Giant if it is in the
962	 * wrong direction.  The correct lock order is that sleepable locks
963	 * always come before Giant.
964	 */
965	if (flags & LOP_NEWORDER &&
966	    !(lock1->li_lock == &Giant.mtx_object &&
967	    (lock->lo_flags & LO_SLEEPABLE) != 0)) {
968		CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
969		    lock->lo_type, lock1->li_lock->lo_type);
970		if (!itismychild(lock1->li_lock->lo_witness, w))
971			/* Witness is dead. */
972			return;
973	}
974	mtx_unlock_spin(&w_mtx);
975	return;
976
977#ifdef KDB
978debugger:
979	if (witness_trace)
980		kdb_backtrace();
981	if (witness_kdb)
982		kdb_enter(__func__);
983#endif
984}
985
986void
987witness_lock(struct lock_object *lock, int flags, const char *file, int line)
988{
989	struct lock_list_entry **lock_list, *lle;
990	struct lock_instance *instance;
991	struct witness *w;
992	struct thread *td;
993
994	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
995	    panicstr != NULL)
996		return;
997	w = lock->lo_witness;
998	td = curthread;
999	file = fixup_filename(file);
1000
1001	/* Determine lock list for this lock. */
1002	if (lock->lo_class->lc_flags & LC_SLEEPLOCK)
1003		lock_list = &td->td_sleeplocks;
1004	else
1005		lock_list = PCPU_PTR(spinlocks);
1006
1007	/* Check to see if we are recursing on a lock we already own. */
1008	instance = find_instance(*lock_list, lock);
1009	if (instance != NULL) {
1010		instance->li_flags++;
1011		CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
1012		    td->td_proc->p_pid, lock->lo_name,
1013		    instance->li_flags & LI_RECURSEMASK);
1014		instance->li_file = file;
1015		instance->li_line = line;
1016		return;
1017	}
1018
1019	/* Update per-witness last file and line acquire. */
1020	w->w_file = file;
1021	w->w_line = line;
1022
1023	/* Find the next open lock instance in the list and fill it. */
1024	lle = *lock_list;
1025	if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1026		lle = witness_lock_list_get();
1027		if (lle == NULL)
1028			return;
1029		lle->ll_next = *lock_list;
1030		CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1031		    td->td_proc->p_pid, lle);
1032		*lock_list = lle;
1033	}
1034	instance = &lle->ll_children[lle->ll_count++];
1035	instance->li_lock = lock;
1036	instance->li_line = line;
1037	instance->li_file = file;
1038	if ((flags & LOP_EXCLUSIVE) != 0)
1039		instance->li_flags = LI_EXCLUSIVE;
1040	else
1041		instance->li_flags = 0;
1042	CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1043	    td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1044}
1045
1046void
1047witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1048{
1049	struct lock_instance *instance;
1050	struct lock_class *class;
1051
1052	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1053	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1054		return;
1055	class = lock->lo_class;
1056	file = fixup_filename(file);
1057	if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1058		panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1059		    class->lc_name, lock->lo_name, file, line);
1060	if ((flags & LOP_TRYLOCK) == 0)
1061		panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
1062		    lock->lo_name, file, line);
1063	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1064		panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1065		    class->lc_name, lock->lo_name, file, line);
1066	instance = find_instance(curthread->td_sleeplocks, lock);
1067	if (instance == NULL)
1068		panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1069		    class->lc_name, lock->lo_name, file, line);
1070	if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1071		panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1072		    class->lc_name, lock->lo_name, file, line);
1073	if ((instance->li_flags & LI_RECURSEMASK) != 0)
1074		panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1075		    class->lc_name, lock->lo_name,
1076		    instance->li_flags & LI_RECURSEMASK, file, line);
1077	instance->li_flags |= LI_EXCLUSIVE;
1078}
1079
1080void
1081witness_downgrade(struct lock_object *lock, int flags, const char *file,
1082    int line)
1083{
1084	struct lock_instance *instance;
1085	struct lock_class *class;
1086
1087	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1088	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1089		return;
1090	class = lock->lo_class;
1091	file = fixup_filename(file);
1092	if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1093		panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1094		    class->lc_name, lock->lo_name, file, line);
1095	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1096		panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1097		    class->lc_name, lock->lo_name, file, line);
1098	instance = find_instance(curthread->td_sleeplocks, lock);
1099	if (instance == NULL)
1100		panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1101		    class->lc_name, lock->lo_name, file, line);
1102	if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1103		panic("downgrade of shared lock (%s) %s @ %s:%d",
1104		    class->lc_name, lock->lo_name, file, line);
1105	if ((instance->li_flags & LI_RECURSEMASK) != 0)
1106		panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1107		    class->lc_name, lock->lo_name,
1108		    instance->li_flags & LI_RECURSEMASK, file, line);
1109	instance->li_flags &= ~LI_EXCLUSIVE;
1110}
1111
1112void
1113witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1114{
1115	struct lock_list_entry **lock_list, *lle;
1116	struct lock_instance *instance;
1117	struct lock_class *class;
1118	struct thread *td;
1119	register_t s;
1120	int i, j;
1121
1122	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1123	    panicstr != NULL)
1124		return;
1125	td = curthread;
1126	class = lock->lo_class;
1127	file = fixup_filename(file);
1128
1129	/* Find lock instance associated with this lock. */
1130	if (class->lc_flags & LC_SLEEPLOCK)
1131		lock_list = &td->td_sleeplocks;
1132	else
1133		lock_list = PCPU_PTR(spinlocks);
1134	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1135		for (i = 0; i < (*lock_list)->ll_count; i++) {
1136			instance = &(*lock_list)->ll_children[i];
1137			if (instance->li_lock == lock)
1138				goto found;
1139		}
1140	panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
1141	    file, line);
1142found:
1143
1144	/* First, check for shared/exclusive mismatches. */
1145	if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
1146	    (flags & LOP_EXCLUSIVE) == 0) {
1147		printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1148		    lock->lo_name, file, line);
1149		printf("while exclusively locked from %s:%d\n",
1150		    instance->li_file, instance->li_line);
1151		panic("excl->ushare");
1152	}
1153	if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
1154	    (flags & LOP_EXCLUSIVE) != 0) {
1155		printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1156		    lock->lo_name, file, line);
1157		printf("while share locked from %s:%d\n", instance->li_file,
1158		    instance->li_line);
1159		panic("share->uexcl");
1160	}
1161
1162	/* If we are recursed, unrecurse. */
1163	if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1164		CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1165		    td->td_proc->p_pid, instance->li_lock->lo_name,
1166		    instance->li_flags);
1167		instance->li_flags--;
1168		return;
1169	}
1170
1171	/* Otherwise, remove this item from the list. */
1172	s = intr_disable();
1173	CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1174	    td->td_proc->p_pid, instance->li_lock->lo_name,
1175	    (*lock_list)->ll_count - 1);
1176	for (j = i; j < (*lock_list)->ll_count - 1; j++)
1177		(*lock_list)->ll_children[j] =
1178		    (*lock_list)->ll_children[j + 1];
1179	(*lock_list)->ll_count--;
1180	intr_restore(s);
1181
1182	/* If this lock list entry is now empty, free it. */
1183	if ((*lock_list)->ll_count == 0) {
1184		lle = *lock_list;
1185		*lock_list = lle->ll_next;
1186		CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1187		    td->td_proc->p_pid, lle);
1188		witness_lock_list_free(lle);
1189	}
1190}
1191
1192/*
1193 * Warn if any locks other than 'lock' are held.  Flags can be passed in to
1194 * exempt Giant and sleepable locks from the checks as well.  If any
1195 * non-exempt locks are held, then a supplied message is printed to the
1196 * console along with a list of the offending locks.  If indicated in the
1197 * flags then a failure results in a panic as well.
1198 */
1199int
1200witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1201{
1202	struct lock_list_entry *lle;
1203	struct lock_instance *lock1;
1204	struct thread *td;
1205	va_list ap;
1206	int i, n;
1207
1208	if (witness_cold || witness_watch == 0 || panicstr != NULL)
1209		return (0);
1210	n = 0;
1211	td = curthread;
1212	for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1213		for (i = lle->ll_count - 1; i >= 0; i--) {
1214			lock1 = &lle->ll_children[i];
1215			if (lock1->li_lock == lock)
1216				continue;
1217			if (flags & WARN_GIANTOK &&
1218			    lock1->li_lock == &Giant.mtx_object)
1219				continue;
1220			if (flags & WARN_SLEEPOK &&
1221			    (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1222				continue;
1223			if (n == 0) {
1224				va_start(ap, fmt);
1225				vprintf(fmt, ap);
1226				va_end(ap);
1227				printf(" with the following");
1228				if (flags & WARN_SLEEPOK)
1229					printf(" non-sleepable");
1230				printf(" locks held:\n");
1231			}
1232			n++;
1233			witness_list_lock(lock1);
1234		}
1235	if (PCPU_GET(spinlocks) != NULL) {
1236		/*
1237		 * Since we already hold a spinlock preemption is
1238		 * already blocked.
1239		 */
1240		if (n == 0) {
1241			va_start(ap, fmt);
1242			vprintf(fmt, ap);
1243			va_end(ap);
1244			printf(" with the following");
1245			if (flags & WARN_SLEEPOK)
1246				printf(" non-sleepable");
1247			printf(" locks held:\n");
1248		}
1249		n += witness_list_locks(PCPU_PTR(spinlocks));
1250	}
1251	if (flags & WARN_PANIC && n)
1252		panic("witness_warn");
1253#ifdef KDB
1254	else if (witness_kdb && n)
1255		kdb_enter(__func__);
1256	else if (witness_trace && n)
1257		kdb_backtrace();
1258#endif
1259	return (n);
1260}
1261
1262const char *
1263witness_file(struct lock_object *lock)
1264{
1265	struct witness *w;
1266
1267	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1268		return ("?");
1269	w = lock->lo_witness;
1270	return (w->w_file);
1271}
1272
1273int
1274witness_line(struct lock_object *lock)
1275{
1276	struct witness *w;
1277
1278	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1279		return (0);
1280	w = lock->lo_witness;
1281	return (w->w_line);
1282}
1283
1284static struct witness *
1285enroll(const char *description, struct lock_class *lock_class)
1286{
1287	struct witness *w;
1288
1289	if (witness_watch == 0 || panicstr != NULL)
1290		return (NULL);
1291	if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
1292		return (NULL);
1293	mtx_lock_spin(&w_mtx);
1294	STAILQ_FOREACH(w, &w_all, w_list) {
1295		if (w->w_name == description || (w->w_refcount > 0 &&
1296		    strcmp(description, w->w_name) == 0)) {
1297			w->w_refcount++;
1298			mtx_unlock_spin(&w_mtx);
1299			if (lock_class != w->w_class)
1300				panic(
1301				"lock (%s) %s does not match earlier (%s) lock",
1302				    description, lock_class->lc_name,
1303				    w->w_class->lc_name);
1304			return (w);
1305		}
1306	}
1307	/*
1308	 * This isn't quite right, as witness_cold is still 0 while we
1309	 * enroll all the locks initialized before witness_initialize().
1310	 */
1311	if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
1312		mtx_unlock_spin(&w_mtx);
1313		panic("spin lock %s not in order list", description);
1314	}
1315	if ((w = witness_get()) == NULL)
1316		return (NULL);
1317	w->w_name = description;
1318	w->w_class = lock_class;
1319	w->w_refcount = 1;
1320	STAILQ_INSERT_HEAD(&w_all, w, w_list);
1321	if (lock_class->lc_flags & LC_SPINLOCK) {
1322		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1323		w_spin_cnt++;
1324	} else if (lock_class->lc_flags & LC_SLEEPLOCK) {
1325		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1326		w_sleep_cnt++;
1327	} else {
1328		mtx_unlock_spin(&w_mtx);
1329		panic("lock class %s is not sleep or spin",
1330		    lock_class->lc_name);
1331	}
1332	mtx_unlock_spin(&w_mtx);
1333	return (w);
1334}
1335
1336/* Don't let the door bang you on the way out... */
1337static int
1338depart(struct witness *w)
1339{
1340	struct witness_child_list_entry *wcl, *nwcl;
1341	struct witness_list *list;
1342	struct witness *parent;
1343
1344	MPASS(w->w_refcount == 0);
1345	if (w->w_class->lc_flags & LC_SLEEPLOCK) {
1346		list = &w_sleep;
1347		w_sleep_cnt--;
1348	} else {
1349		list = &w_spin;
1350		w_spin_cnt--;
1351	}
1352	/*
1353	 * First, we run through the entire tree looking for any
1354	 * witnesses that the outgoing witness is a child of.  For
1355	 * each parent that we find, we reparent all the direct
1356	 * children of the outgoing witness to its parent.
1357	 */
1358	STAILQ_FOREACH(parent, list, w_typelist) {
1359		if (!isitmychild(parent, w))
1360			continue;
1361		removechild(parent, w);
1362	}
1363
1364	/*
1365	 * Now we go through and free up the child list of the
1366	 * outgoing witness.
1367	 */
1368	for (wcl = w->w_children; wcl != NULL; wcl = nwcl) {
1369		nwcl = wcl->wcl_next;
1370        	w_child_cnt--;
1371		witness_child_free(wcl);
1372	}
1373
1374	/*
1375	 * Detach from various lists and free.
1376	 */
1377	STAILQ_REMOVE(list, w, witness, w_typelist);
1378	STAILQ_REMOVE(&w_all, w, witness, w_list);
1379	witness_free(w);
1380
1381	return (1);
1382}
1383
1384/*
1385 * Add "child" as a direct child of "parent".  Returns false if
1386 * we fail due to out of memory.
1387 */
1388static int
1389insertchild(struct witness *parent, struct witness *child)
1390{
1391	struct witness_child_list_entry **wcl;
1392
1393	MPASS(child != NULL && parent != NULL);
1394
1395	/*
1396	 * Insert "child" after "parent"
1397	 */
1398	wcl = &parent->w_children;
1399	while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
1400		wcl = &(*wcl)->wcl_next;
1401	if (*wcl == NULL) {
1402		*wcl = witness_child_get();
1403		if (*wcl == NULL)
1404			return (0);
1405        	w_child_cnt++;
1406	}
1407	(*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
1408
1409	return (1);
1410}
1411
1412
1413static int
1414itismychild(struct witness *parent, struct witness *child)
1415{
1416	struct witness_list *list;
1417
1418	MPASS(child != NULL && parent != NULL);
1419	if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
1420	    (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
1421		panic(
1422		"%s: parent (%s) and child (%s) are not the same lock type",
1423		    __func__, parent->w_class->lc_name,
1424		    child->w_class->lc_name);
1425
1426	if (!insertchild(parent, child))
1427		return (0);
1428
1429	if (parent->w_class->lc_flags & LC_SLEEPLOCK)
1430		list = &w_sleep;
1431	else
1432		list = &w_spin;
1433	return (1);
1434}
1435
1436static void
1437removechild(struct witness *parent, struct witness *child)
1438{
1439	struct witness_child_list_entry **wcl, *wcl1;
1440	int i;
1441
1442	for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1443		for (i = 0; i < (*wcl)->wcl_count; i++)
1444			if ((*wcl)->wcl_children[i] == child)
1445				goto found;
1446	return;
1447found:
1448	(*wcl)->wcl_count--;
1449	if ((*wcl)->wcl_count > i)
1450		(*wcl)->wcl_children[i] =
1451		    (*wcl)->wcl_children[(*wcl)->wcl_count];
1452	MPASS((*wcl)->wcl_children[i] != NULL);
1453	if ((*wcl)->wcl_count != 0)
1454		return;
1455	wcl1 = *wcl;
1456	*wcl = wcl1->wcl_next;
1457	w_child_cnt--;
1458	witness_child_free(wcl1);
1459}
1460
1461static int
1462isitmychild(struct witness *parent, struct witness *child)
1463{
1464	struct witness_child_list_entry *wcl;
1465	int i;
1466
1467	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1468		for (i = 0; i < wcl->wcl_count; i++) {
1469			if (wcl->wcl_children[i] == child)
1470				return (1);
1471		}
1472	}
1473	return (0);
1474}
1475
1476static int
1477isitmydescendant(struct witness *parent, struct witness *child)
1478{
1479	struct witness_child_list_entry *wcl;
1480	int i, j;
1481
1482	if (isitmychild(parent, child))
1483		return (1);
1484	j = 0;
1485	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1486		MPASS(j < 1000);
1487		for (i = 0; i < wcl->wcl_count; i++) {
1488			if (isitmydescendant(wcl->wcl_children[i], child))
1489				return (1);
1490		}
1491		j++;
1492	}
1493	return (0);
1494}
1495
1496static void
1497witness_levelall (void)
1498{
1499	struct witness_list *list;
1500	struct witness *w, *w1;
1501
1502	/*
1503	 * First clear all levels.
1504	 */
1505	STAILQ_FOREACH(w, &w_all, w_list) {
1506		w->w_level = 0;
1507	}
1508
1509	/*
1510	 * Look for locks with no parent and level all their descendants.
1511	 */
1512	STAILQ_FOREACH(w, &w_all, w_list) {
1513		/*
1514		 * This is just an optimization, technically we could get
1515		 * away just walking the all list each time.
1516		 */
1517		if (w->w_class->lc_flags & LC_SLEEPLOCK)
1518			list = &w_sleep;
1519		else
1520			list = &w_spin;
1521		STAILQ_FOREACH(w1, list, w_typelist) {
1522			if (isitmychild(w1, w))
1523				goto skip;
1524		}
1525		witness_leveldescendents(w, 0);
1526	skip:
1527		;	/* silence GCC 3.x */
1528	}
1529}
1530
1531static void
1532witness_leveldescendents(struct witness *parent, int level)
1533{
1534	struct witness_child_list_entry *wcl;
1535	int i;
1536
1537	if (parent->w_level < level)
1538		parent->w_level = level;
1539	level++;
1540	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1541		for (i = 0; i < wcl->wcl_count; i++)
1542			witness_leveldescendents(wcl->wcl_children[i], level);
1543}
1544
1545static void
1546witness_displaydescendants(void(*prnt)(const char *fmt, ...),
1547			   struct witness *parent, int indent)
1548{
1549	struct witness_child_list_entry *wcl;
1550	int i, level;
1551
1552	level = parent->w_level;
1553	prnt("%-2d", level);
1554	for (i = 0; i < indent; i++)
1555		prnt(" ");
1556	if (parent->w_refcount > 0)
1557		prnt("%s", parent->w_name);
1558	else
1559		prnt("(dead)");
1560	if (parent->w_displayed) {
1561		prnt(" -- (already displayed)\n");
1562		return;
1563	}
1564	parent->w_displayed = 1;
1565	if (parent->w_refcount > 0) {
1566		if (parent->w_file != NULL)
1567			prnt(" -- last acquired @ %s:%d", parent->w_file,
1568			    parent->w_line);
1569	}
1570	prnt("\n");
1571	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1572		for (i = 0; i < wcl->wcl_count; i++)
1573			    witness_displaydescendants(prnt,
1574				wcl->wcl_children[i], indent + 1);
1575}
1576
1577#ifdef BLESSING
1578static int
1579blessed(struct witness *w1, struct witness *w2)
1580{
1581	int i;
1582	struct witness_blessed *b;
1583
1584	for (i = 0; i < blessed_count; i++) {
1585		b = &blessed_list[i];
1586		if (strcmp(w1->w_name, b->b_lock1) == 0) {
1587			if (strcmp(w2->w_name, b->b_lock2) == 0)
1588				return (1);
1589			continue;
1590		}
1591		if (strcmp(w1->w_name, b->b_lock2) == 0)
1592			if (strcmp(w2->w_name, b->b_lock1) == 0)
1593				return (1);
1594	}
1595	return (0);
1596}
1597#endif
1598
1599static struct witness *
1600witness_get(void)
1601{
1602	struct witness *w;
1603
1604	if (witness_watch == 0) {
1605		mtx_unlock_spin(&w_mtx);
1606		return (NULL);
1607	}
1608	if (STAILQ_EMPTY(&w_free)) {
1609		witness_watch = 0;
1610		mtx_unlock_spin(&w_mtx);
1611		printf("%s: witness exhausted\n", __func__);
1612		return (NULL);
1613	}
1614	w = STAILQ_FIRST(&w_free);
1615	STAILQ_REMOVE_HEAD(&w_free, w_list);
1616	w_free_cnt--;
1617	bzero(w, sizeof(*w));
1618	return (w);
1619}
1620
1621static void
1622witness_free(struct witness *w)
1623{
1624
1625	STAILQ_INSERT_HEAD(&w_free, w, w_list);
1626	w_free_cnt++;
1627}
1628
1629static struct witness_child_list_entry *
1630witness_child_get(void)
1631{
1632	struct witness_child_list_entry *wcl;
1633
1634	if (witness_watch == 0) {
1635		mtx_unlock_spin(&w_mtx);
1636		return (NULL);
1637	}
1638	wcl = w_child_free;
1639	if (wcl == NULL) {
1640		witness_watch = 0;
1641		mtx_unlock_spin(&w_mtx);
1642		printf("%s: witness exhausted\n", __func__);
1643		return (NULL);
1644	}
1645	w_child_free = wcl->wcl_next;
1646	w_child_free_cnt--;
1647	bzero(wcl, sizeof(*wcl));
1648	return (wcl);
1649}
1650
1651static void
1652witness_child_free(struct witness_child_list_entry *wcl)
1653{
1654
1655	wcl->wcl_next = w_child_free;
1656	w_child_free = wcl;
1657	w_child_free_cnt++;
1658}
1659
1660static struct lock_list_entry *
1661witness_lock_list_get(void)
1662{
1663	struct lock_list_entry *lle;
1664
1665	if (witness_watch == 0)
1666		return (NULL);
1667	mtx_lock_spin(&w_mtx);
1668	lle = w_lock_list_free;
1669	if (lle == NULL) {
1670		witness_watch = 0;
1671		mtx_unlock_spin(&w_mtx);
1672		printf("%s: witness exhausted\n", __func__);
1673		return (NULL);
1674	}
1675	w_lock_list_free = lle->ll_next;
1676	mtx_unlock_spin(&w_mtx);
1677	bzero(lle, sizeof(*lle));
1678	return (lle);
1679}
1680
1681static void
1682witness_lock_list_free(struct lock_list_entry *lle)
1683{
1684
1685	mtx_lock_spin(&w_mtx);
1686	lle->ll_next = w_lock_list_free;
1687	w_lock_list_free = lle;
1688	mtx_unlock_spin(&w_mtx);
1689}
1690
1691static struct lock_instance *
1692find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1693{
1694	struct lock_list_entry *lle;
1695	struct lock_instance *instance;
1696	int i;
1697
1698	for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1699		for (i = lle->ll_count - 1; i >= 0; i--) {
1700			instance = &lle->ll_children[i];
1701			if (instance->li_lock == lock)
1702				return (instance);
1703		}
1704	return (NULL);
1705}
1706
1707static void
1708witness_list_lock(struct lock_instance *instance)
1709{
1710	struct lock_object *lock;
1711
1712	lock = instance->li_lock;
1713	printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1714	    "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name);
1715	if (lock->lo_type != lock->lo_name)
1716		printf(" (%s)", lock->lo_type);
1717	printf(" r = %d (%p) locked @ %s:%d\n",
1718	    instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
1719	    instance->li_line);
1720}
1721
1722#ifdef DDB
1723static int
1724witness_thread_has_locks(struct thread *td)
1725{
1726
1727	return (td->td_sleeplocks != NULL);
1728}
1729
1730static int
1731witness_proc_has_locks(struct proc *p)
1732{
1733	struct thread *td;
1734
1735	FOREACH_THREAD_IN_PROC(p, td) {
1736		if (witness_thread_has_locks(td))
1737			return (1);
1738	}
1739	return (0);
1740}
1741#endif
1742
1743int
1744witness_list_locks(struct lock_list_entry **lock_list)
1745{
1746	struct lock_list_entry *lle;
1747	int i, nheld;
1748
1749	nheld = 0;
1750	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1751		for (i = lle->ll_count - 1; i >= 0; i--) {
1752			witness_list_lock(&lle->ll_children[i]);
1753			nheld++;
1754		}
1755	return (nheld);
1756}
1757
1758/*
1759 * This is a bit risky at best.  We call this function when we have timed
1760 * out acquiring a spin lock, and we assume that the other CPU is stuck
1761 * with this lock held.  So, we go groveling around in the other CPU's
1762 * per-cpu data to try to find the lock instance for this spin lock to
1763 * see when it was last acquired.
1764 */
1765void
1766witness_display_spinlock(struct lock_object *lock, struct thread *owner)
1767{
1768	struct lock_instance *instance;
1769	struct pcpu *pc;
1770
1771	if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
1772		return;
1773	pc = pcpu_find(owner->td_oncpu);
1774	instance = find_instance(pc->pc_spinlocks, lock);
1775	if (instance != NULL)
1776		witness_list_lock(instance);
1777}
1778
1779void
1780witness_save(struct lock_object *lock, const char **filep, int *linep)
1781{
1782	struct lock_instance *instance;
1783
1784	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1785	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1786		return;
1787	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1788		panic("%s: lock (%s) %s is not a sleep lock", __func__,
1789		    lock->lo_class->lc_name, lock->lo_name);
1790	instance = find_instance(curthread->td_sleeplocks, lock);
1791	if (instance == NULL)
1792		panic("%s: lock (%s) %s not locked", __func__,
1793		    lock->lo_class->lc_name, lock->lo_name);
1794	*filep = instance->li_file;
1795	*linep = instance->li_line;
1796}
1797
1798void
1799witness_restore(struct lock_object *lock, const char *file, int line)
1800{
1801	struct lock_instance *instance;
1802
1803	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1804	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1805		return;
1806	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1807		panic("%s: lock (%s) %s is not a sleep lock", __func__,
1808		    lock->lo_class->lc_name, lock->lo_name);
1809	instance = find_instance(curthread->td_sleeplocks, lock);
1810	if (instance == NULL)
1811		panic("%s: lock (%s) %s not locked", __func__,
1812		    lock->lo_class->lc_name, lock->lo_name);
1813	lock->lo_witness->w_file = file;
1814	lock->lo_witness->w_line = line;
1815	instance->li_file = file;
1816	instance->li_line = line;
1817}
1818
1819void
1820witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1821{
1822#ifdef INVARIANT_SUPPORT
1823	struct lock_instance *instance;
1824
1825	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1826		return;
1827	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0)
1828		instance = find_instance(curthread->td_sleeplocks, lock);
1829	else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0)
1830		instance = find_instance(PCPU_GET(spinlocks), lock);
1831	else {
1832		panic("Lock (%s) %s is not sleep or spin!",
1833		    lock->lo_class->lc_name, lock->lo_name);
1834	}
1835	file = fixup_filename(file);
1836	switch (flags) {
1837	case LA_UNLOCKED:
1838		if (instance != NULL)
1839			panic("Lock (%s) %s locked @ %s:%d.",
1840			    lock->lo_class->lc_name, lock->lo_name, file, line);
1841		break;
1842	case LA_LOCKED:
1843	case LA_LOCKED | LA_RECURSED:
1844	case LA_LOCKED | LA_NOTRECURSED:
1845	case LA_SLOCKED:
1846	case LA_SLOCKED | LA_RECURSED:
1847	case LA_SLOCKED | LA_NOTRECURSED:
1848	case LA_XLOCKED:
1849	case LA_XLOCKED | LA_RECURSED:
1850	case LA_XLOCKED | LA_NOTRECURSED:
1851		if (instance == NULL) {
1852			panic("Lock (%s) %s not locked @ %s:%d.",
1853			    lock->lo_class->lc_name, lock->lo_name, file, line);
1854			break;
1855		}
1856		if ((flags & LA_XLOCKED) != 0 &&
1857		    (instance->li_flags & LI_EXCLUSIVE) == 0)
1858			panic("Lock (%s) %s not exclusively locked @ %s:%d.",
1859			    lock->lo_class->lc_name, lock->lo_name, file, line);
1860		if ((flags & LA_SLOCKED) != 0 &&
1861		    (instance->li_flags & LI_EXCLUSIVE) != 0)
1862			panic("Lock (%s) %s exclusively locked @ %s:%d.",
1863			    lock->lo_class->lc_name, lock->lo_name, file, line);
1864		if ((flags & LA_RECURSED) != 0 &&
1865		    (instance->li_flags & LI_RECURSEMASK) == 0)
1866			panic("Lock (%s) %s not recursed @ %s:%d.",
1867			    lock->lo_class->lc_name, lock->lo_name, file, line);
1868		if ((flags & LA_NOTRECURSED) != 0 &&
1869		    (instance->li_flags & LI_RECURSEMASK) != 0)
1870			panic("Lock (%s) %s recursed @ %s:%d.",
1871			    lock->lo_class->lc_name, lock->lo_name, file, line);
1872		break;
1873	default:
1874		panic("Invalid lock assertion at %s:%d.", file, line);
1875
1876	}
1877#endif	/* INVARIANT_SUPPORT */
1878}
1879
1880#ifdef DDB
1881static void
1882witness_list(struct thread *td)
1883{
1884
1885	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1886	KASSERT(kdb_active, ("%s: not in the debugger", __func__));
1887
1888	if (witness_watch == 0)
1889		return;
1890
1891	witness_list_locks(&td->td_sleeplocks);
1892
1893	/*
1894	 * We only handle spinlocks if td == curthread.  This is somewhat broken
1895	 * if td is currently executing on some other CPU and holds spin locks
1896	 * as we won't display those locks.  If we had a MI way of getting
1897	 * the per-cpu data for a given cpu then we could use
1898	 * td->td_oncpu to get the list of spinlocks for this thread
1899	 * and "fix" this.
1900	 *
1901	 * That still wouldn't really fix this unless we locked sched_lock
1902	 * or stopped the other CPU to make sure it wasn't changing the list
1903	 * out from under us.  It is probably best to just not try to handle
1904	 * threads on other CPU's for now.
1905	 */
1906	if (td == curthread && PCPU_GET(spinlocks) != NULL)
1907		witness_list_locks(PCPU_PTR(spinlocks));
1908}
1909
1910DB_SHOW_COMMAND(locks, db_witness_list)
1911{
1912	struct thread *td;
1913	pid_t pid;
1914	struct proc *p;
1915
1916	if (have_addr) {
1917		pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1918		    ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1919		    ((addr >> 16) % 16) * 10000;
1920		/* sx_slock(&allproc_lock); */
1921		FOREACH_PROC_IN_SYSTEM(p) {
1922			if (p->p_pid == pid)
1923				break;
1924		}
1925		/* sx_sunlock(&allproc_lock); */
1926		if (p == NULL) {
1927			db_printf("pid %d not found\n", pid);
1928			return;
1929		}
1930		FOREACH_THREAD_IN_PROC(p, td) {
1931			witness_list(td);
1932		}
1933	} else {
1934		td = curthread;
1935		witness_list(td);
1936	}
1937}
1938
1939DB_SHOW_COMMAND(alllocks, db_witness_list_all)
1940{
1941	struct thread *td;
1942	struct proc *p;
1943
1944	/*
1945	 * It would be nice to list only threads and processes that actually
1946	 * held sleep locks, but that information is currently not exported
1947	 * by WITNESS.
1948	 */
1949	FOREACH_PROC_IN_SYSTEM(p) {
1950		if (!witness_proc_has_locks(p))
1951			continue;
1952		FOREACH_THREAD_IN_PROC(p, td) {
1953			if (!witness_thread_has_locks(td))
1954				continue;
1955			printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
1956			    p->p_comm, td, td->td_tid);
1957			witness_list(td);
1958		}
1959	}
1960}
1961
1962DB_SHOW_COMMAND(witness, db_witness_display)
1963{
1964
1965	witness_display(db_printf);
1966}
1967#endif
1968