subr_witness.c revision 139348
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 139348 2004-12-27 10:47:08Z rwatson $");
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 200
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 int	rebalancetree(struct witness_list *list);
169static void	removechild(struct witness *parent, struct witness *child);
170static int	reparentchildren(struct witness *newparent,
171		    struct witness *oldparent);
172static int	sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
173static void	witness_displaydescendants(void(*)(const char *fmt, ...),
174					   struct witness *, int indent);
175static const char *fixup_filename(const char *file);
176static void	witness_leveldescendents(struct witness *parent, int level);
177static void	witness_levelall(void);
178static struct	witness *witness_get(void);
179static void	witness_free(struct witness *m);
180static struct	witness_child_list_entry *witness_child_get(void);
181static void	witness_child_free(struct witness_child_list_entry *wcl);
182static struct	lock_list_entry *witness_lock_list_get(void);
183static void	witness_lock_list_free(struct lock_list_entry *lle);
184static struct	lock_instance *find_instance(struct lock_list_entry *lock_list,
185					     struct lock_object *lock);
186static void	witness_list_lock(struct lock_instance *instance);
187#ifdef DDB
188static void	witness_list(struct thread *td);
189static void	witness_display_list(void(*prnt)(const char *fmt, ...),
190				     struct witness_list *list);
191static void	witness_display(void(*)(const char *fmt, ...));
192#endif
193
194MALLOC_DEFINE(M_WITNESS, "witness", "witness structure");
195SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking");
196
197/*
198 * If set to 0, witness is disabled.  If set to 1, witness performs full lock
199 * order checking for all locks.  If set to 2 or higher, then witness skips
200 * the full lock order check if the lock being acquired is at a higher level
201 * (i.e. farther down in the tree) than the current lock.  This last mode is
202 * somewhat experimental and not considered fully safe.  At runtime, this
203 * value may be set to 0 to turn off witness.  witness is not allowed be
204 * turned on once it is turned off, however.
205 */
206static int witness_watch = 1;
207TUNABLE_INT("debug.witness.watch", &witness_watch);
208SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
209    sysctl_debug_witness_watch, "I", "witness is watching lock operations");
210
211#ifdef KDB
212/*
213 * When KDB is enabled and witness_kdb is set to 1, it will cause the system
214 * to drop into kdebug() when:
215 *	- a lock heirarchy violation occurs
216 *	- locks are held when going to sleep.
217 */
218#ifdef WITNESS_KDB
219int	witness_kdb = 1;
220#else
221int	witness_kdb = 0;
222#endif
223TUNABLE_INT("debug.witness.kdb", &witness_kdb);
224SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, "");
225
226/*
227 * When KDB is enabled and witness_trace is set to 1, it will cause the system
228 * to print a stack trace:
229 *	- a lock heirarchy violation occurs
230 *	- locks are held when going to sleep.
231 */
232int	witness_trace = 1;
233TUNABLE_INT("debug.witness.trace", &witness_trace);
234SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, "");
235#endif /* KDB */
236
237#ifdef WITNESS_SKIPSPIN
238int	witness_skipspin = 1;
239#else
240int	witness_skipspin = 0;
241#endif
242TUNABLE_INT("debug.witness.skipspin", &witness_skipspin);
243SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN,
244    &witness_skipspin, 0, "");
245
246static struct mtx w_mtx;
247static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
248static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
249static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
250static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
251static struct witness_child_list_entry *w_child_free = NULL;
252static struct lock_list_entry *w_lock_list_free = NULL;
253
254static struct witness w_data[WITNESS_COUNT];
255static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
256static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
257
258static struct witness_order_list_entry order_lists[] = {
259	{ "proctree", &lock_class_sx },
260	{ "allproc", &lock_class_sx },
261	{ "Giant", &lock_class_mtx_sleep },
262	{ "filedesc structure", &lock_class_mtx_sleep },
263	{ "pipe mutex", &lock_class_mtx_sleep },
264	{ "sigio lock", &lock_class_mtx_sleep },
265	{ "process group", &lock_class_mtx_sleep },
266	{ "process lock", &lock_class_mtx_sleep },
267	{ "session", &lock_class_mtx_sleep },
268	{ "uidinfo hash", &lock_class_mtx_sleep },
269	{ "uidinfo struct", &lock_class_mtx_sleep },
270	{ "allprison", &lock_class_mtx_sleep },
271	{ NULL, NULL },
272	/*
273	 * Sockets
274	 */
275	{ "filedesc structure", &lock_class_mtx_sleep },
276	{ "accept", &lock_class_mtx_sleep },
277	{ "so_snd", &lock_class_mtx_sleep },
278	{ "so_rcv", &lock_class_mtx_sleep },
279	{ "sellck", &lock_class_mtx_sleep },
280	{ NULL, NULL },
281	/*
282	 * Routing
283	 */
284	{ "so_rcv", &lock_class_mtx_sleep },
285	{ "radix node head", &lock_class_mtx_sleep },
286	{ "rtentry", &lock_class_mtx_sleep },
287	{ "ifaddr", &lock_class_mtx_sleep },
288	{ NULL, NULL },
289	/*
290	 * UNIX Domain Sockets
291	 */
292	{ "unp", &lock_class_mtx_sleep },
293	{ "so_snd", &lock_class_mtx_sleep },
294	{ NULL, NULL },
295	/*
296	 * UDP/IP
297	 */
298	{ "udp", &lock_class_mtx_sleep },
299	{ "udpinp", &lock_class_mtx_sleep },
300	{ "so_snd", &lock_class_mtx_sleep },
301	{ NULL, NULL },
302	/*
303	 * TCP/IP
304	 */
305	{ "tcp", &lock_class_mtx_sleep },
306	{ "tcpinp", &lock_class_mtx_sleep },
307	{ "so_snd", &lock_class_mtx_sleep },
308	{ NULL, NULL },
309	/*
310	 * SLIP
311	 */
312	{ "slip_mtx", &lock_class_mtx_sleep },
313	{ "slip sc_mtx", &lock_class_mtx_sleep },
314	{ NULL, NULL },
315	/*
316	 * netatalk
317	 */
318	{ "ddp_list_mtx", &lock_class_mtx_sleep },
319	{ "ddp_mtx", &lock_class_mtx_sleep },
320	{ NULL, NULL },
321	/*
322	 * BPF
323	 */
324	{ "bpf global lock", &lock_class_mtx_sleep },
325	{ "bpf interface lock", &lock_class_mtx_sleep },
326	{ "bpf cdev lock", &lock_class_mtx_sleep },
327	{ NULL, NULL },
328	/*
329	 * spin locks
330	 */
331#ifdef SMP
332	{ "ap boot", &lock_class_mtx_spin },
333#endif
334	{ "sio", &lock_class_mtx_spin },
335#ifdef __i386__
336	{ "cy", &lock_class_mtx_spin },
337#endif
338	{ "uart_hwmtx", &lock_class_mtx_spin },
339	{ "sabtty", &lock_class_mtx_spin },
340	{ "zstty", &lock_class_mtx_spin },
341	{ "ng_node", &lock_class_mtx_spin },
342	{ "ng_worklist", &lock_class_mtx_spin },
343	{ "taskqueue_fast", &lock_class_mtx_spin },
344	{ "intr table", &lock_class_mtx_spin },
345	{ "ithread table lock", &lock_class_mtx_spin },
346	{ "sleepq chain", &lock_class_mtx_spin },
347	{ "sched lock", &lock_class_mtx_spin },
348	{ "turnstile chain", &lock_class_mtx_spin },
349	{ "td_contested", &lock_class_mtx_spin },
350	{ "callout", &lock_class_mtx_spin },
351	{ "entropy harvest mutex", &lock_class_mtx_spin },
352	/*
353	 * leaf locks
354	 */
355	{ "allpmaps", &lock_class_mtx_spin },
356	{ "vm page queue free mutex", &lock_class_mtx_spin },
357	{ "icu", &lock_class_mtx_spin },
358#ifdef SMP
359	{ "smp rendezvous", &lock_class_mtx_spin },
360#if defined(__i386__) || defined(__amd64__)
361	{ "tlb", &lock_class_mtx_spin },
362#endif
363#ifdef __sparc64__
364	{ "ipi", &lock_class_mtx_spin },
365#endif
366#endif
367	{ "clk", &lock_class_mtx_spin },
368	{ "mutex profiling lock", &lock_class_mtx_spin },
369	{ "kse zombie lock", &lock_class_mtx_spin },
370	{ "ALD Queue", &lock_class_mtx_spin },
371#ifdef __ia64__
372	{ "MCA spin lock", &lock_class_mtx_spin },
373#endif
374#if defined(__i386__) || defined(__amd64__)
375	{ "pcicfg", &lock_class_mtx_spin },
376#endif
377	{ NULL, NULL },
378	{ NULL, NULL }
379};
380
381#ifdef BLESSING
382/*
383 * Pairs of locks which have been blessed
384 * Don't complain about order problems with blessed locks
385 */
386static struct witness_blessed blessed_list[] = {
387};
388static int blessed_count =
389	sizeof(blessed_list) / sizeof(struct witness_blessed);
390#endif
391
392/*
393 * List of all locks in the system.
394 */
395TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks);
396
397static struct mtx all_mtx = {
398	{ &lock_class_mtx_sleep,	/* mtx_object.lo_class */
399	  "All locks list",		/* mtx_object.lo_name */
400	  "All locks list",		/* mtx_object.lo_type */
401	  LO_INITIALIZED,		/* mtx_object.lo_flags */
402	  { NULL, NULL },		/* mtx_object.lo_list */
403	  NULL },			/* mtx_object.lo_witness */
404	MTX_UNOWNED, 0			/* mtx_lock, mtx_recurse */
405};
406
407/*
408 * This global is set to 0 once it becomes safe to use the witness code.
409 */
410static int witness_cold = 1;
411
412/*
413 * Global variables for book keeping.
414 */
415static int lock_cur_cnt;
416static int lock_max_cnt;
417
418/*
419 * The WITNESS-enabled diagnostic code.
420 */
421static void
422witness_initialize(void *dummy __unused)
423{
424	struct lock_object *lock;
425	struct witness_order_list_entry *order;
426	struct witness *w, *w1;
427	int i;
428
429	/*
430	 * We have to release Giant before initializing its witness
431	 * structure so that WITNESS doesn't get confused.
432	 */
433	mtx_unlock(&Giant);
434	mtx_assert(&Giant, MA_NOTOWNED);
435
436	CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
437	TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
438	mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
439	    MTX_NOWITNESS);
440	for (i = 0; i < WITNESS_COUNT; i++)
441		witness_free(&w_data[i]);
442	for (i = 0; i < WITNESS_CHILDCOUNT; i++)
443		witness_child_free(&w_childdata[i]);
444	for (i = 0; i < LOCK_CHILDCOUNT; i++)
445		witness_lock_list_free(&w_locklistdata[i]);
446
447	/* First add in all the specified order lists. */
448	for (order = order_lists; order->w_name != NULL; order++) {
449		w = enroll(order->w_name, order->w_class);
450		if (w == NULL)
451			continue;
452		w->w_file = "order list";
453		for (order++; order->w_name != NULL; order++) {
454			w1 = enroll(order->w_name, order->w_class);
455			if (w1 == NULL)
456				continue;
457			w1->w_file = "order list";
458			if (!itismychild(w, w1))
459				panic("Not enough memory for static orders!");
460			w = w1;
461		}
462	}
463
464	/* Iterate through all locks and add them to witness. */
465	mtx_lock(&all_mtx);
466	TAILQ_FOREACH(lock, &all_locks, lo_list) {
467		if (lock->lo_flags & LO_WITNESS)
468			lock->lo_witness = enroll(lock->lo_type,
469			    lock->lo_class);
470		else
471			lock->lo_witness = NULL;
472	}
473	mtx_unlock(&all_mtx);
474
475	/* Mark the witness code as being ready for use. */
476	atomic_store_rel_int(&witness_cold, 0);
477
478	mtx_lock(&Giant);
479}
480SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
481
482static int
483sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
484{
485	int error, value;
486
487	value = witness_watch;
488	error = sysctl_handle_int(oidp, &value, 0, req);
489	if (error != 0 || req->newptr == NULL)
490		return (error);
491	error = suser(req->td);
492	if (error != 0)
493		return (error);
494	if (value == witness_watch)
495		return (0);
496	if (value != 0)
497		return (EINVAL);
498	witness_watch = 0;
499	return (0);
500}
501
502void
503witness_init(struct lock_object *lock)
504{
505	struct lock_class *class;
506
507	class = lock->lo_class;
508	if (lock->lo_flags & LO_INITIALIZED)
509		panic("%s: lock (%s) %s is already initialized", __func__,
510		    class->lc_name, lock->lo_name);
511	if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
512	    (class->lc_flags & LC_RECURSABLE) == 0)
513		panic("%s: lock (%s) %s can not be recursable", __func__,
514		    class->lc_name, lock->lo_name);
515	if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
516	    (class->lc_flags & LC_SLEEPABLE) == 0)
517		panic("%s: lock (%s) %s can not be sleepable", __func__,
518		    class->lc_name, lock->lo_name);
519	if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
520	    (class->lc_flags & LC_UPGRADABLE) == 0)
521		panic("%s: lock (%s) %s can not be upgradable", __func__,
522		    class->lc_name, lock->lo_name);
523
524	mtx_lock(&all_mtx);
525	TAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
526	lock->lo_flags |= LO_INITIALIZED;
527	lock_cur_cnt++;
528	if (lock_cur_cnt > lock_max_cnt)
529		lock_max_cnt = lock_cur_cnt;
530	mtx_unlock(&all_mtx);
531	if (!witness_cold && witness_watch != 0 && panicstr == NULL &&
532	    (lock->lo_flags & LO_WITNESS) != 0)
533		lock->lo_witness = enroll(lock->lo_type, class);
534	else
535		lock->lo_witness = NULL;
536}
537
538void
539witness_destroy(struct lock_object *lock)
540{
541	struct witness *w;
542
543	if (witness_cold)
544		panic("lock (%s) %s destroyed while witness_cold",
545		    lock->lo_class->lc_name, lock->lo_name);
546	if ((lock->lo_flags & LO_INITIALIZED) == 0)
547		panic("%s: lock (%s) %s is not initialized", __func__,
548		    lock->lo_class->lc_name, lock->lo_name);
549
550	/* XXX: need to verify that no one holds the lock */
551	w = lock->lo_witness;
552	if (w != NULL) {
553		mtx_lock_spin(&w_mtx);
554		MPASS(w->w_refcount > 0);
555		w->w_refcount--;
556
557		/*
558		 * Lock is already released if we have an allocation failure
559		 * and depart() fails.
560		 */
561		if (w->w_refcount != 0 || depart(w))
562			mtx_unlock_spin(&w_mtx);
563	}
564
565	mtx_lock(&all_mtx);
566	lock_cur_cnt--;
567	TAILQ_REMOVE(&all_locks, lock, lo_list);
568	lock->lo_flags &= ~LO_INITIALIZED;
569	mtx_unlock(&all_mtx);
570}
571
572#ifdef DDB
573static void
574witness_display_list(void(*prnt)(const char *fmt, ...),
575		     struct witness_list *list)
576{
577	struct witness *w;
578
579	STAILQ_FOREACH(w, list, w_typelist) {
580		if (w->w_file == NULL || w->w_level > 0)
581			continue;
582		/*
583		 * This lock has no anscestors, display its descendants.
584		 */
585		witness_displaydescendants(prnt, w, 0);
586	}
587}
588
589static void
590witness_display(void(*prnt)(const char *fmt, ...))
591{
592	struct witness *w;
593
594	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
595	witness_levelall();
596
597	/* Clear all the displayed flags. */
598	STAILQ_FOREACH(w, &w_all, w_list) {
599		w->w_displayed = 0;
600	}
601
602	/*
603	 * First, handle sleep locks which have been acquired at least
604	 * once.
605	 */
606	prnt("Sleep locks:\n");
607	witness_display_list(prnt, &w_sleep);
608
609	/*
610	 * Now do spin locks which have been acquired at least once.
611	 */
612	prnt("\nSpin locks:\n");
613	witness_display_list(prnt, &w_spin);
614
615	/*
616	 * Finally, any locks which have not been acquired yet.
617	 */
618	prnt("\nLocks which were never acquired:\n");
619	STAILQ_FOREACH(w, &w_all, w_list) {
620		if (w->w_file != NULL || w->w_refcount == 0)
621			continue;
622		prnt("%s\n", w->w_name);
623	}
624}
625#endif /* DDB */
626
627/* Trim useless garbage from filenames. */
628static const char *
629fixup_filename(const char *file)
630{
631
632	if (file == NULL)
633		return (NULL);
634	while (strncmp(file, "../", 3) == 0)
635		file += 3;
636	return (file);
637}
638
639int
640witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
641{
642
643	if (witness_watch == 0 || panicstr != NULL)
644		return (0);
645
646	/* Require locks that witness knows about. */
647	if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
648	    lock2->lo_witness == NULL)
649		return (EINVAL);
650
651	MPASS(!mtx_owned(&w_mtx));
652	mtx_lock_spin(&w_mtx);
653
654	/*
655	 * If we already have either an explicit or implied lock order that
656	 * is the other way around, then return an error.
657	 */
658	if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
659		mtx_unlock_spin(&w_mtx);
660		return (EDOOFUS);
661	}
662
663	/* Try to add the new order. */
664	CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
665	    lock2->lo_type, lock1->lo_type);
666	if (!itismychild(lock1->lo_witness, lock2->lo_witness))
667		return (ENOMEM);
668	mtx_unlock_spin(&w_mtx);
669	return (0);
670}
671
672void
673witness_checkorder(struct lock_object *lock, int flags, const char *file,
674    int line)
675{
676	struct lock_list_entry **lock_list, *lle;
677	struct lock_instance *lock1, *lock2;
678	struct lock_class *class;
679	struct witness *w, *w1;
680	struct thread *td;
681	int i, j;
682
683	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
684	    panicstr != NULL)
685		return;
686
687	/*
688	 * Try locks do not block if they fail to acquire the lock, thus
689	 * there is no danger of deadlocks or of switching while holding a
690	 * spin lock if we acquire a lock via a try operation.  This
691	 * function shouldn't even be called for try locks, so panic if
692	 * that happens.
693	 */
694	if (flags & LOP_TRYLOCK)
695		panic("%s should not be called for try lock operations",
696		    __func__);
697
698	w = lock->lo_witness;
699	class = lock->lo_class;
700	td = curthread;
701	file = fixup_filename(file);
702
703	if (class->lc_flags & LC_SLEEPLOCK) {
704		/*
705		 * Since spin locks include a critical section, this check
706		 * implicitly enforces a lock order of all sleep locks before
707		 * all spin locks.
708		 */
709		if (td->td_critnest != 0 && !kdb_active)
710			panic("blockable sleep lock (%s) %s @ %s:%d",
711			    class->lc_name, lock->lo_name, file, line);
712
713		/*
714		 * If this is the first lock acquired then just return as
715		 * no order checking is needed.
716		 */
717		if (td->td_sleeplocks == NULL)
718			return;
719		lock_list = &td->td_sleeplocks;
720	} else {
721		/*
722		 * If this is the first lock, just return as no order
723		 * checking is needed.  We check this in both if clauses
724		 * here as unifying the check would require us to use a
725		 * critical section to ensure we don't migrate while doing
726		 * the check.  Note that if this is not the first lock, we
727		 * are already in a critical section and are safe for the
728		 * rest of the check.
729		 */
730		if (PCPU_GET(spinlocks) == NULL)
731			return;
732		lock_list = PCPU_PTR(spinlocks);
733	}
734
735	/*
736	 * Check to see if we are recursing on a lock we already own.  If
737	 * so, make sure that we don't mismatch exclusive and shared lock
738	 * acquires.
739	 */
740	lock1 = find_instance(*lock_list, lock);
741	if (lock1 != NULL) {
742		if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
743		    (flags & LOP_EXCLUSIVE) == 0) {
744			printf("shared lock of (%s) %s @ %s:%d\n",
745			    class->lc_name, lock->lo_name, file, line);
746			printf("while exclusively locked from %s:%d\n",
747			    lock1->li_file, lock1->li_line);
748			panic("share->excl");
749		}
750		if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
751		    (flags & LOP_EXCLUSIVE) != 0) {
752			printf("exclusive lock of (%s) %s @ %s:%d\n",
753			    class->lc_name, lock->lo_name, file, line);
754			printf("while share locked from %s:%d\n",
755			    lock1->li_file, lock1->li_line);
756			panic("excl->share");
757		}
758		return;
759	}
760
761	/*
762	 * Try locks do not block if they fail to acquire the lock, thus
763	 * there is no danger of deadlocks or of switching while holding a
764	 * spin lock if we acquire a lock via a try operation.
765	 */
766	if (flags & LOP_TRYLOCK)
767		return;
768
769	/*
770	 * Check for duplicate locks of the same type.  Note that we only
771	 * have to check for this on the last lock we just acquired.  Any
772	 * other cases will be caught as lock order violations.
773	 */
774	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
775	w1 = lock1->li_lock->lo_witness;
776	if (w1 == w) {
777		if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK))
778			return;
779		w->w_same_squawked = 1;
780		printf("acquiring duplicate lock of same type: \"%s\"\n",
781			lock->lo_type);
782		printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name,
783		    lock1->li_file, lock1->li_line);
784		printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
785#ifdef KDB
786		goto debugger;
787#else
788		return;
789#endif
790	}
791	MPASS(!mtx_owned(&w_mtx));
792	mtx_lock_spin(&w_mtx);
793	/*
794	 * If we have a known higher number just say ok
795	 */
796	if (witness_watch > 1 && w->w_level > w1->w_level) {
797		mtx_unlock_spin(&w_mtx);
798		return;
799	}
800	/*
801	 * If we know that the the lock we are acquiring comes after
802	 * the lock we most recently acquired in the lock order tree,
803	 * then there is no need for any further checks.
804	 */
805	if (isitmydescendant(w1, w)) {
806		mtx_unlock_spin(&w_mtx);
807		return;
808	}
809	for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
810		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
811
812			MPASS(j < WITNESS_COUNT);
813			lock1 = &lle->ll_children[i];
814			w1 = lock1->li_lock->lo_witness;
815
816			/*
817			 * If this lock doesn't undergo witness checking,
818			 * then skip it.
819			 */
820			if (w1 == NULL) {
821				KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
822				    ("lock missing witness structure"));
823				continue;
824			}
825			/*
826			 * If we are locking Giant and this is a sleepable
827			 * lock, then skip it.
828			 */
829			if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
830			    lock == &Giant.mtx_object)
831				continue;
832			/*
833			 * If we are locking a sleepable lock and this lock
834			 * is Giant, then skip it.
835			 */
836			if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
837			    lock1->li_lock == &Giant.mtx_object)
838				continue;
839			/*
840			 * If we are locking a sleepable lock and this lock
841			 * isn't sleepable, we want to treat it as a lock
842			 * order violation to enfore a general lock order of
843			 * sleepable locks before non-sleepable locks.
844			 */
845			if (!((lock->lo_flags & LO_SLEEPABLE) != 0 &&
846			    (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
847			    /*
848			     * Check the lock order hierarchy for a reveresal.
849			     */
850			    if (!isitmydescendant(w, w1))
851				continue;
852			/*
853			 * We have a lock order violation, check to see if it
854			 * is allowed or has already been yelled about.
855			 */
856			mtx_unlock_spin(&w_mtx);
857#ifdef BLESSING
858			/*
859			 * If the lock order is blessed, just bail.  We don't
860			 * look for other lock order violations though, which
861			 * may be a bug.
862			 */
863			if (blessed(w, w1))
864				return;
865#endif
866			if (lock1->li_lock == &Giant.mtx_object) {
867				if (w1->w_Giant_squawked)
868					return;
869				else
870					w1->w_Giant_squawked = 1;
871			} else {
872				if (w1->w_other_squawked)
873					return;
874				else
875					w1->w_other_squawked = 1;
876			}
877			/*
878			 * Ok, yell about it.
879			 */
880			printf("lock order reversal\n");
881			/*
882			 * Try to locate an earlier lock with
883			 * witness w in our list.
884			 */
885			do {
886				lock2 = &lle->ll_children[i];
887				MPASS(lock2->li_lock != NULL);
888				if (lock2->li_lock->lo_witness == w)
889					break;
890				if (i == 0 && lle->ll_next != NULL) {
891					lle = lle->ll_next;
892					i = lle->ll_count - 1;
893					MPASS(i >= 0 && i < LOCK_NCHILDREN);
894				} else
895					i--;
896			} while (i >= 0);
897			if (i < 0) {
898				printf(" 1st %p %s (%s) @ %s:%d\n",
899				    lock1->li_lock, lock1->li_lock->lo_name,
900				    lock1->li_lock->lo_type, lock1->li_file,
901				    lock1->li_line);
902				printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
903				    lock->lo_name, lock->lo_type, file, line);
904			} else {
905				printf(" 1st %p %s (%s) @ %s:%d\n",
906				    lock2->li_lock, lock2->li_lock->lo_name,
907				    lock2->li_lock->lo_type, lock2->li_file,
908				    lock2->li_line);
909				printf(" 2nd %p %s (%s) @ %s:%d\n",
910				    lock1->li_lock, lock1->li_lock->lo_name,
911				    lock1->li_lock->lo_type, lock1->li_file,
912				    lock1->li_line);
913				printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
914				    lock->lo_name, lock->lo_type, file, line);
915			}
916#ifdef KDB
917			goto debugger;
918#else
919			return;
920#endif
921		}
922	}
923	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
924	/*
925	 * If requested, build a new lock order.  However, don't build a new
926	 * relationship between a sleepable lock and Giant if it is in the
927	 * wrong direction.  The correct lock order is that sleepable locks
928	 * always come before Giant.
929	 */
930	if (flags & LOP_NEWORDER &&
931	    !(lock1->li_lock == &Giant.mtx_object &&
932	    (lock->lo_flags & LO_SLEEPABLE) != 0)) {
933		CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
934		    lock->lo_type, lock1->li_lock->lo_type);
935		if (!itismychild(lock1->li_lock->lo_witness, w))
936			/* Witness is dead. */
937			return;
938	}
939	mtx_unlock_spin(&w_mtx);
940	return;
941
942#ifdef KDB
943debugger:
944	if (witness_trace)
945		kdb_backtrace();
946	if (witness_kdb)
947		kdb_enter(__func__);
948#endif
949}
950
951void
952witness_lock(struct lock_object *lock, int flags, const char *file, int line)
953{
954	struct lock_list_entry **lock_list, *lle;
955	struct lock_instance *instance;
956	struct witness *w;
957	struct thread *td;
958
959	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
960	    panicstr != NULL)
961		return;
962	w = lock->lo_witness;
963	td = curthread;
964	file = fixup_filename(file);
965
966	/* Determine lock list for this lock. */
967	if (lock->lo_class->lc_flags & LC_SLEEPLOCK)
968		lock_list = &td->td_sleeplocks;
969	else
970		lock_list = PCPU_PTR(spinlocks);
971
972	/* Check to see if we are recursing on a lock we already own. */
973	instance = find_instance(*lock_list, lock);
974	if (instance != NULL) {
975		instance->li_flags++;
976		CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
977		    td->td_proc->p_pid, lock->lo_name,
978		    instance->li_flags & LI_RECURSEMASK);
979		instance->li_file = file;
980		instance->li_line = line;
981		return;
982	}
983
984	/* Update per-witness last file and line acquire. */
985	w->w_file = file;
986	w->w_line = line;
987
988	/* Find the next open lock instance in the list and fill it. */
989	lle = *lock_list;
990	if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
991		lle = witness_lock_list_get();
992		if (lle == NULL)
993			return;
994		lle->ll_next = *lock_list;
995		CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
996		    td->td_proc->p_pid, lle);
997		*lock_list = lle;
998	}
999	instance = &lle->ll_children[lle->ll_count++];
1000	instance->li_lock = lock;
1001	instance->li_line = line;
1002	instance->li_file = file;
1003	if ((flags & LOP_EXCLUSIVE) != 0)
1004		instance->li_flags = LI_EXCLUSIVE;
1005	else
1006		instance->li_flags = 0;
1007	CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1008	    td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1009}
1010
1011void
1012witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1013{
1014	struct lock_instance *instance;
1015	struct lock_class *class;
1016
1017	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1018	if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1019		return;
1020	class = lock->lo_class;
1021	file = fixup_filename(file);
1022	if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1023		panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1024		    class->lc_name, lock->lo_name, file, line);
1025	if ((flags & LOP_TRYLOCK) == 0)
1026		panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
1027		    lock->lo_name, file, line);
1028	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1029		panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1030		    class->lc_name, lock->lo_name, file, line);
1031	instance = find_instance(curthread->td_sleeplocks, lock);
1032	if (instance == NULL)
1033		panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1034		    class->lc_name, lock->lo_name, file, line);
1035	if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1036		panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1037		    class->lc_name, lock->lo_name, file, line);
1038	if ((instance->li_flags & LI_RECURSEMASK) != 0)
1039		panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1040		    class->lc_name, lock->lo_name,
1041		    instance->li_flags & LI_RECURSEMASK, file, line);
1042	instance->li_flags |= LI_EXCLUSIVE;
1043}
1044
1045void
1046witness_downgrade(struct lock_object *lock, int flags, const char *file,
1047    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("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1059		    class->lc_name, lock->lo_name, file, line);
1060	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1061		panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1062		    class->lc_name, lock->lo_name, file, line);
1063	instance = find_instance(curthread->td_sleeplocks, lock);
1064	if (instance == NULL)
1065		panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1066		    class->lc_name, lock->lo_name, file, line);
1067	if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1068		panic("downgrade of shared lock (%s) %s @ %s:%d",
1069		    class->lc_name, lock->lo_name, file, line);
1070	if ((instance->li_flags & LI_RECURSEMASK) != 0)
1071		panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1072		    class->lc_name, lock->lo_name,
1073		    instance->li_flags & LI_RECURSEMASK, file, line);
1074	instance->li_flags &= ~LI_EXCLUSIVE;
1075}
1076
1077void
1078witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1079{
1080	struct lock_list_entry **lock_list, *lle;
1081	struct lock_instance *instance;
1082	struct lock_class *class;
1083	struct thread *td;
1084	register_t s;
1085	int i, j;
1086
1087	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1088	    panicstr != NULL)
1089		return;
1090	td = curthread;
1091	class = lock->lo_class;
1092	file = fixup_filename(file);
1093
1094	/* Find lock instance associated with this lock. */
1095	if (class->lc_flags & LC_SLEEPLOCK)
1096		lock_list = &td->td_sleeplocks;
1097	else
1098		lock_list = PCPU_PTR(spinlocks);
1099	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1100		for (i = 0; i < (*lock_list)->ll_count; i++) {
1101			instance = &(*lock_list)->ll_children[i];
1102			if (instance->li_lock == lock)
1103				goto found;
1104		}
1105	panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
1106	    file, line);
1107found:
1108
1109	/* First, check for shared/exclusive mismatches. */
1110	if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
1111	    (flags & LOP_EXCLUSIVE) == 0) {
1112		printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1113		    lock->lo_name, file, line);
1114		printf("while exclusively locked from %s:%d\n",
1115		    instance->li_file, instance->li_line);
1116		panic("excl->ushare");
1117	}
1118	if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
1119	    (flags & LOP_EXCLUSIVE) != 0) {
1120		printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1121		    lock->lo_name, file, line);
1122		printf("while share locked from %s:%d\n", instance->li_file,
1123		    instance->li_line);
1124		panic("share->uexcl");
1125	}
1126
1127	/* If we are recursed, unrecurse. */
1128	if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1129		CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1130		    td->td_proc->p_pid, instance->li_lock->lo_name,
1131		    instance->li_flags);
1132		instance->li_flags--;
1133		return;
1134	}
1135
1136	/* Otherwise, remove this item from the list. */
1137	s = intr_disable();
1138	CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1139	    td->td_proc->p_pid, instance->li_lock->lo_name,
1140	    (*lock_list)->ll_count - 1);
1141	for (j = i; j < (*lock_list)->ll_count - 1; j++)
1142		(*lock_list)->ll_children[j] =
1143		    (*lock_list)->ll_children[j + 1];
1144	(*lock_list)->ll_count--;
1145	intr_restore(s);
1146
1147	/* If this lock list entry is now empty, free it. */
1148	if ((*lock_list)->ll_count == 0) {
1149		lle = *lock_list;
1150		*lock_list = lle->ll_next;
1151		CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1152		    td->td_proc->p_pid, lle);
1153		witness_lock_list_free(lle);
1154	}
1155}
1156
1157/*
1158 * Warn if any locks other than 'lock' are held.  Flags can be passed in to
1159 * exempt Giant and sleepable locks from the checks as well.  If any
1160 * non-exempt locks are held, then a supplied message is printed to the
1161 * console along with a list of the offending locks.  If indicated in the
1162 * flags then a failure results in a panic as well.
1163 */
1164int
1165witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1166{
1167	struct lock_list_entry *lle;
1168	struct lock_instance *lock1;
1169	struct thread *td;
1170	va_list ap;
1171	int i, n;
1172
1173	if (witness_cold || witness_watch == 0 || panicstr != NULL)
1174		return (0);
1175	n = 0;
1176	td = curthread;
1177	for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1178		for (i = lle->ll_count - 1; i >= 0; i--) {
1179			lock1 = &lle->ll_children[i];
1180			if (lock1->li_lock == lock)
1181				continue;
1182			if (flags & WARN_GIANTOK &&
1183			    lock1->li_lock == &Giant.mtx_object)
1184				continue;
1185			if (flags & WARN_SLEEPOK &&
1186			    (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1187				continue;
1188			if (n == 0) {
1189				va_start(ap, fmt);
1190				vprintf(fmt, ap);
1191				va_end(ap);
1192				printf(" with the following");
1193				if (flags & WARN_SLEEPOK)
1194					printf(" non-sleepable");
1195				printf(" locks held:\n");
1196			}
1197			n++;
1198			witness_list_lock(lock1);
1199		}
1200	if (PCPU_GET(spinlocks) != NULL) {
1201		/*
1202		 * Since we already hold a spinlock preemption is
1203		 * already blocked.
1204		 */
1205		if (n == 0) {
1206			va_start(ap, fmt);
1207			vprintf(fmt, ap);
1208			va_end(ap);
1209			printf(" with the following");
1210			if (flags & WARN_SLEEPOK)
1211				printf(" non-sleepable");
1212			printf(" locks held:\n");
1213		}
1214		n += witness_list_locks(PCPU_PTR(spinlocks));
1215	}
1216	if (flags & WARN_PANIC && n)
1217		panic("witness_warn");
1218#ifdef KDB
1219	else if (witness_kdb && n)
1220		kdb_enter(__func__);
1221	else if (witness_trace && n)
1222		kdb_backtrace();
1223#endif
1224	return (n);
1225}
1226
1227const char *
1228witness_file(struct lock_object *lock)
1229{
1230	struct witness *w;
1231
1232	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1233		return ("?");
1234	w = lock->lo_witness;
1235	return (w->w_file);
1236}
1237
1238int
1239witness_line(struct lock_object *lock)
1240{
1241	struct witness *w;
1242
1243	if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1244		return (0);
1245	w = lock->lo_witness;
1246	return (w->w_line);
1247}
1248
1249static struct witness *
1250enroll(const char *description, struct lock_class *lock_class)
1251{
1252	struct witness *w;
1253
1254	if (witness_watch == 0 || panicstr != NULL)
1255		return (NULL);
1256	if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
1257		return (NULL);
1258	mtx_lock_spin(&w_mtx);
1259	STAILQ_FOREACH(w, &w_all, w_list) {
1260		if (w->w_name == description || (w->w_refcount > 0 &&
1261		    strcmp(description, w->w_name) == 0)) {
1262			w->w_refcount++;
1263			mtx_unlock_spin(&w_mtx);
1264			if (lock_class != w->w_class)
1265				panic(
1266				"lock (%s) %s does not match earlier (%s) lock",
1267				    description, lock_class->lc_name,
1268				    w->w_class->lc_name);
1269			return (w);
1270		}
1271	}
1272	/*
1273	 * This isn't quite right, as witness_cold is still 0 while we
1274	 * enroll all the locks initialized before witness_initialize().
1275	 */
1276	if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
1277		mtx_unlock_spin(&w_mtx);
1278		panic("spin lock %s not in order list", description);
1279	}
1280	if ((w = witness_get()) == NULL)
1281		return (NULL);
1282	w->w_name = description;
1283	w->w_class = lock_class;
1284	w->w_refcount = 1;
1285	STAILQ_INSERT_HEAD(&w_all, w, w_list);
1286	if (lock_class->lc_flags & LC_SPINLOCK)
1287		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1288	else if (lock_class->lc_flags & LC_SLEEPLOCK)
1289		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1290	else {
1291		mtx_unlock_spin(&w_mtx);
1292		panic("lock class %s is not sleep or spin",
1293		    lock_class->lc_name);
1294	}
1295	mtx_unlock_spin(&w_mtx);
1296	return (w);
1297}
1298
1299/* Don't let the door bang you on the way out... */
1300static int
1301depart(struct witness *w)
1302{
1303	struct witness_child_list_entry *wcl, *nwcl;
1304	struct witness_list *list;
1305	struct witness *parent;
1306
1307	MPASS(w->w_refcount == 0);
1308	if (w->w_class->lc_flags & LC_SLEEPLOCK)
1309		list = &w_sleep;
1310	else
1311		list = &w_spin;
1312	/*
1313	 * First, we run through the entire tree looking for any
1314	 * witnesses that the outgoing witness is a child of.  For
1315	 * each parent that we find, we reparent all the direct
1316	 * children of the outgoing witness to its parent.
1317	 */
1318	STAILQ_FOREACH(parent, list, w_typelist) {
1319		if (!isitmychild(parent, w))
1320			continue;
1321		removechild(parent, w);
1322		if (!reparentchildren(parent, w))
1323			return (0);
1324	}
1325
1326	/*
1327	 * Now we go through and free up the child list of the
1328	 * outgoing witness.
1329	 */
1330	for (wcl = w->w_children; wcl != NULL; wcl = nwcl) {
1331		nwcl = wcl->wcl_next;
1332		witness_child_free(wcl);
1333	}
1334
1335	/*
1336	 * Detach from various lists and free.
1337	 */
1338	STAILQ_REMOVE(list, w, witness, w_typelist);
1339	STAILQ_REMOVE(&w_all, w, witness, w_list);
1340	witness_free(w);
1341
1342	/* Finally, fixup the tree. */
1343	return (rebalancetree(list));
1344}
1345
1346/*
1347 * Prune an entire lock order tree.  We look for cases where a lock
1348 * is now both a descendant and a direct child of a given lock.  In
1349 * that case, we want to remove the direct child link from the tree.
1350 *
1351 * Returns false if insertchild() fails.
1352 */
1353static int
1354rebalancetree(struct witness_list *list)
1355{
1356	struct witness *child, *parent;
1357
1358	STAILQ_FOREACH(child, list, w_typelist) {
1359		STAILQ_FOREACH(parent, list, w_typelist) {
1360			if (!isitmychild(parent, child))
1361				continue;
1362			removechild(parent, child);
1363			if (isitmydescendant(parent, child))
1364				continue;
1365			if (!insertchild(parent, child))
1366				return (0);
1367		}
1368	}
1369	witness_levelall();
1370	return (1);
1371}
1372
1373/*
1374 * Add "child" as a direct child of "parent".  Returns false if
1375 * we fail due to out of memory.
1376 */
1377static int
1378insertchild(struct witness *parent, struct witness *child)
1379{
1380	struct witness_child_list_entry **wcl;
1381
1382	MPASS(child != NULL && parent != NULL);
1383
1384	/*
1385	 * Insert "child" after "parent"
1386	 */
1387	wcl = &parent->w_children;
1388	while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
1389		wcl = &(*wcl)->wcl_next;
1390	if (*wcl == NULL) {
1391		*wcl = witness_child_get();
1392		if (*wcl == NULL)
1393			return (0);
1394	}
1395	(*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
1396
1397	return (1);
1398}
1399
1400/*
1401 * Make all the direct descendants of oldparent be direct descendants
1402 * of newparent.
1403 */
1404static int
1405reparentchildren(struct witness *newparent, struct witness *oldparent)
1406{
1407	struct witness_child_list_entry *wcl;
1408	int i;
1409
1410	/* Avoid making a witness a child of itself. */
1411	MPASS(!isitmychild(oldparent, newparent));
1412
1413	for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1414		for (i = 0; i < wcl->wcl_count; i++)
1415			if (!insertchild(newparent, wcl->wcl_children[i]))
1416				return (0);
1417	return (1);
1418}
1419
1420static int
1421itismychild(struct witness *parent, struct witness *child)
1422{
1423	struct witness_list *list;
1424
1425	MPASS(child != NULL && parent != NULL);
1426	if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
1427	    (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
1428		panic(
1429		"%s: parent (%s) and child (%s) are not the same lock type",
1430		    __func__, parent->w_class->lc_name,
1431		    child->w_class->lc_name);
1432
1433	if (!insertchild(parent, child))
1434		return (0);
1435
1436	if (parent->w_class->lc_flags & LC_SLEEPLOCK)
1437		list = &w_sleep;
1438	else
1439		list = &w_spin;
1440	return (rebalancetree(list));
1441}
1442
1443static void
1444removechild(struct witness *parent, struct witness *child)
1445{
1446	struct witness_child_list_entry **wcl, *wcl1;
1447	int i;
1448
1449	for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1450		for (i = 0; i < (*wcl)->wcl_count; i++)
1451			if ((*wcl)->wcl_children[i] == child)
1452				goto found;
1453	return;
1454found:
1455	(*wcl)->wcl_count--;
1456	if ((*wcl)->wcl_count > i)
1457		(*wcl)->wcl_children[i] =
1458		    (*wcl)->wcl_children[(*wcl)->wcl_count];
1459	MPASS((*wcl)->wcl_children[i] != NULL);
1460	if ((*wcl)->wcl_count != 0)
1461		return;
1462	wcl1 = *wcl;
1463	*wcl = wcl1->wcl_next;
1464	witness_child_free(wcl1);
1465}
1466
1467static int
1468isitmychild(struct witness *parent, struct witness *child)
1469{
1470	struct witness_child_list_entry *wcl;
1471	int i;
1472
1473	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1474		for (i = 0; i < wcl->wcl_count; i++) {
1475			if (wcl->wcl_children[i] == child)
1476				return (1);
1477		}
1478	}
1479	return (0);
1480}
1481
1482static int
1483isitmydescendant(struct witness *parent, struct witness *child)
1484{
1485	struct witness_child_list_entry *wcl;
1486	int i, j;
1487
1488	if (isitmychild(parent, child))
1489		return (1);
1490	j = 0;
1491	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1492		MPASS(j < 1000);
1493		for (i = 0; i < wcl->wcl_count; i++) {
1494			if (isitmydescendant(wcl->wcl_children[i], child))
1495				return (1);
1496		}
1497		j++;
1498	}
1499	return (0);
1500}
1501
1502static void
1503witness_levelall (void)
1504{
1505	struct witness_list *list;
1506	struct witness *w, *w1;
1507
1508	/*
1509	 * First clear all levels.
1510	 */
1511	STAILQ_FOREACH(w, &w_all, w_list) {
1512		w->w_level = 0;
1513	}
1514
1515	/*
1516	 * Look for locks with no parent and level all their descendants.
1517	 */
1518	STAILQ_FOREACH(w, &w_all, w_list) {
1519		/*
1520		 * This is just an optimization, technically we could get
1521		 * away just walking the all list each time.
1522		 */
1523		if (w->w_class->lc_flags & LC_SLEEPLOCK)
1524			list = &w_sleep;
1525		else
1526			list = &w_spin;
1527		STAILQ_FOREACH(w1, list, w_typelist) {
1528			if (isitmychild(w1, w))
1529				goto skip;
1530		}
1531		witness_leveldescendents(w, 0);
1532	skip:
1533		;	/* silence GCC 3.x */
1534	}
1535}
1536
1537static void
1538witness_leveldescendents(struct witness *parent, int level)
1539{
1540	struct witness_child_list_entry *wcl;
1541	int i;
1542
1543	if (parent->w_level < level)
1544		parent->w_level = level;
1545	level++;
1546	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1547		for (i = 0; i < wcl->wcl_count; i++)
1548			witness_leveldescendents(wcl->wcl_children[i], level);
1549}
1550
1551static void
1552witness_displaydescendants(void(*prnt)(const char *fmt, ...),
1553			   struct witness *parent, int indent)
1554{
1555	struct witness_child_list_entry *wcl;
1556	int i, level;
1557
1558	level = parent->w_level;
1559	prnt("%-2d", level);
1560	for (i = 0; i < indent; i++)
1561		prnt(" ");
1562	if (parent->w_refcount > 0)
1563		prnt("%s", parent->w_name);
1564	else
1565		prnt("(dead)");
1566	if (parent->w_displayed) {
1567		prnt(" -- (already displayed)\n");
1568		return;
1569	}
1570	parent->w_displayed = 1;
1571	if (parent->w_refcount > 0) {
1572		if (parent->w_file != NULL)
1573			prnt(" -- last acquired @ %s:%d", parent->w_file,
1574			    parent->w_line);
1575	}
1576	prnt("\n");
1577	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1578		for (i = 0; i < wcl->wcl_count; i++)
1579			    witness_displaydescendants(prnt,
1580				wcl->wcl_children[i], indent + 1);
1581}
1582
1583#ifdef BLESSING
1584static int
1585blessed(struct witness *w1, struct witness *w2)
1586{
1587	int i;
1588	struct witness_blessed *b;
1589
1590	for (i = 0; i < blessed_count; i++) {
1591		b = &blessed_list[i];
1592		if (strcmp(w1->w_name, b->b_lock1) == 0) {
1593			if (strcmp(w2->w_name, b->b_lock2) == 0)
1594				return (1);
1595			continue;
1596		}
1597		if (strcmp(w1->w_name, b->b_lock2) == 0)
1598			if (strcmp(w2->w_name, b->b_lock1) == 0)
1599				return (1);
1600	}
1601	return (0);
1602}
1603#endif
1604
1605static struct witness *
1606witness_get(void)
1607{
1608	struct witness *w;
1609
1610	if (witness_watch == 0) {
1611		mtx_unlock_spin(&w_mtx);
1612		return (NULL);
1613	}
1614	if (STAILQ_EMPTY(&w_free)) {
1615		witness_watch = 0;
1616		mtx_unlock_spin(&w_mtx);
1617		printf("%s: witness exhausted\n", __func__);
1618		return (NULL);
1619	}
1620	w = STAILQ_FIRST(&w_free);
1621	STAILQ_REMOVE_HEAD(&w_free, w_list);
1622	bzero(w, sizeof(*w));
1623	return (w);
1624}
1625
1626static void
1627witness_free(struct witness *w)
1628{
1629
1630	STAILQ_INSERT_HEAD(&w_free, w, w_list);
1631}
1632
1633static struct witness_child_list_entry *
1634witness_child_get(void)
1635{
1636	struct witness_child_list_entry *wcl;
1637
1638	if (witness_watch == 0) {
1639		mtx_unlock_spin(&w_mtx);
1640		return (NULL);
1641	}
1642	wcl = w_child_free;
1643	if (wcl == NULL) {
1644		witness_watch = 0;
1645		mtx_unlock_spin(&w_mtx);
1646		printf("%s: witness exhausted\n", __func__);
1647		return (NULL);
1648	}
1649	w_child_free = wcl->wcl_next;
1650	bzero(wcl, sizeof(*wcl));
1651	return (wcl);
1652}
1653
1654static void
1655witness_child_free(struct witness_child_list_entry *wcl)
1656{
1657
1658	wcl->wcl_next = w_child_free;
1659	w_child_free = wcl;
1660}
1661
1662static struct lock_list_entry *
1663witness_lock_list_get(void)
1664{
1665	struct lock_list_entry *lle;
1666
1667	if (witness_watch == 0)
1668		return (NULL);
1669	mtx_lock_spin(&w_mtx);
1670	lle = w_lock_list_free;
1671	if (lle == NULL) {
1672		witness_watch = 0;
1673		mtx_unlock_spin(&w_mtx);
1674		printf("%s: witness exhausted\n", __func__);
1675		return (NULL);
1676	}
1677	w_lock_list_free = lle->ll_next;
1678	mtx_unlock_spin(&w_mtx);
1679	bzero(lle, sizeof(*lle));
1680	return (lle);
1681}
1682
1683static void
1684witness_lock_list_free(struct lock_list_entry *lle)
1685{
1686
1687	mtx_lock_spin(&w_mtx);
1688	lle->ll_next = w_lock_list_free;
1689	w_lock_list_free = lle;
1690	mtx_unlock_spin(&w_mtx);
1691}
1692
1693static struct lock_instance *
1694find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1695{
1696	struct lock_list_entry *lle;
1697	struct lock_instance *instance;
1698	int i;
1699
1700	for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1701		for (i = lle->ll_count - 1; i >= 0; i--) {
1702			instance = &lle->ll_children[i];
1703			if (instance->li_lock == lock)
1704				return (instance);
1705		}
1706	return (NULL);
1707}
1708
1709static void
1710witness_list_lock(struct lock_instance *instance)
1711{
1712	struct lock_object *lock;
1713
1714	lock = instance->li_lock;
1715	printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1716	    "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name);
1717	if (lock->lo_type != lock->lo_name)
1718		printf(" (%s)", lock->lo_type);
1719	printf(" r = %d (%p) locked @ %s:%d\n",
1720	    instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
1721	    instance->li_line);
1722}
1723
1724static int
1725witness_thread_has_locks(struct thread *td)
1726{
1727
1728	return (td->td_sleeplocks != NULL);
1729}
1730
1731static int
1732witness_proc_has_locks(struct proc *p)
1733{
1734	struct thread *td;
1735
1736	FOREACH_THREAD_IN_PROC(p, td) {
1737		if (witness_thread_has_locks(td))
1738			return (1);
1739	}
1740	return (0);
1741}
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