xref: /freebsd/sys/kern/subr_witness.c (revision c0020399a650364d0134f79f3fa319f84064372d)
1 /*-
2  * Copyright (c) 2008 Isilon Systems, Inc.
3  * Copyright (c) 2008 Ilya Maykov <ivmaykov@gmail.com>
4  * Copyright (c) 1998 Berkeley Software Design, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Berkeley Software Design Inc's name may not be used to endorse or
16  *    promote products derived from this software without specific prior
17  *    written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
32  *	and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
33  */
34 
35 /*
36  * Implementation of the `witness' lock verifier.  Originally implemented for
37  * mutexes in BSD/OS.  Extended to handle generic lock objects and lock
38  * classes in FreeBSD.
39  */
40 
41 /*
42  *	Main Entry: witness
43  *	Pronunciation: 'wit-n&s
44  *	Function: noun
45  *	Etymology: Middle English witnesse, from Old English witnes knowledge,
46  *	    testimony, witness, from 2wit
47  *	Date: before 12th century
48  *	1 : attestation of a fact or event : TESTIMONY
49  *	2 : one that gives evidence; specifically : one who testifies in
50  *	    a cause or before a judicial tribunal
51  *	3 : one asked to be present at a transaction so as to be able to
52  *	    testify to its having taken place
53  *	4 : one who has personal knowledge of something
54  *	5 a : something serving as evidence or proof : SIGN
55  *	  b : public affirmation by word or example of usually
56  *	      religious faith or conviction <the heroic witness to divine
57  *	      life -- Pilot>
58  *	6 capitalized : a member of the Jehovah's Witnesses
59  */
60 
61 /*
62  * Special rules concerning Giant and lock orders:
63  *
64  * 1) Giant must be acquired before any other mutexes.  Stated another way,
65  *    no other mutex may be held when Giant is acquired.
66  *
67  * 2) Giant must be released when blocking on a sleepable lock.
68  *
69  * This rule is less obvious, but is a result of Giant providing the same
70  * semantics as spl().  Basically, when a thread sleeps, it must release
71  * Giant.  When a thread blocks on a sleepable lock, it sleeps.  Hence rule
72  * 2).
73  *
74  * 3) Giant may be acquired before or after sleepable locks.
75  *
76  * This rule is also not quite as obvious.  Giant may be acquired after
77  * a sleepable lock because it is a non-sleepable lock and non-sleepable
78  * locks may always be acquired while holding a sleepable lock.  The second
79  * case, Giant before a sleepable lock, follows from rule 2) above.  Suppose
80  * you have two threads T1 and T2 and a sleepable lock X.  Suppose that T1
81  * acquires X and blocks on Giant.  Then suppose that T2 acquires Giant and
82  * blocks on X.  When T2 blocks on X, T2 will release Giant allowing T1 to
83  * execute.  Thus, acquiring Giant both before and after a sleepable lock
84  * will not result in a lock order reversal.
85  */
86 
87 #include <sys/cdefs.h>
88 __FBSDID("$FreeBSD$");
89 
90 #include "opt_ddb.h"
91 #include "opt_hwpmc_hooks.h"
92 #include "opt_stack.h"
93 #include "opt_witness.h"
94 
95 #include <sys/param.h>
96 #include <sys/bus.h>
97 #include <sys/kdb.h>
98 #include <sys/kernel.h>
99 #include <sys/ktr.h>
100 #include <sys/lock.h>
101 #include <sys/malloc.h>
102 #include <sys/mutex.h>
103 #include <sys/priv.h>
104 #include <sys/proc.h>
105 #include <sys/sbuf.h>
106 #include <sys/sched.h>
107 #include <sys/stack.h>
108 #include <sys/sysctl.h>
109 #include <sys/systm.h>
110 
111 #ifdef DDB
112 #include <ddb/ddb.h>
113 #endif
114 
115 #include <machine/stdarg.h>
116 
117 #if !defined(DDB) && !defined(STACK)
118 #error "DDB or STACK options are required for WITNESS"
119 #endif
120 
121 /* Note that these traces do not work with KTR_ALQ. */
122 #if 0
123 #define	KTR_WITNESS	KTR_SUBSYS
124 #else
125 #define	KTR_WITNESS	0
126 #endif
127 
128 #define	LI_RECURSEMASK	0x0000ffff	/* Recursion depth of lock instance. */
129 #define	LI_EXCLUSIVE	0x00010000	/* Exclusive lock instance. */
130 #define	LI_NORELEASE	0x00020000	/* Lock not allowed to be released. */
131 
132 /* Define this to check for blessed mutexes */
133 #undef BLESSING
134 
135 #define	WITNESS_COUNT 		1024
136 #define	WITNESS_CHILDCOUNT 	(WITNESS_COUNT * 4)
137 #define	WITNESS_HASH_SIZE	251	/* Prime, gives load factor < 2 */
138 #define	WITNESS_PENDLIST	512
139 
140 /* Allocate 256 KB of stack data space */
141 #define	WITNESS_LO_DATA_COUNT	2048
142 
143 /* Prime, gives load factor of ~2 at full load */
144 #define	WITNESS_LO_HASH_SIZE	1021
145 
146 /*
147  * XXX: This is somewhat bogus, as we assume here that at most 2048 threads
148  * will hold LOCK_NCHILDREN locks.  We handle failure ok, and we should
149  * probably be safe for the most part, but it's still a SWAG.
150  */
151 #define	LOCK_NCHILDREN	5
152 #define	LOCK_CHILDCOUNT	2048
153 
154 #define	MAX_W_NAME	64
155 
156 #define	BADSTACK_SBUF_SIZE	(256 * WITNESS_COUNT)
157 #define	CYCLEGRAPH_SBUF_SIZE	8192
158 #define	FULLGRAPH_SBUF_SIZE	32768
159 
160 /*
161  * These flags go in the witness relationship matrix and describe the
162  * relationship between any two struct witness objects.
163  */
164 #define	WITNESS_UNRELATED        0x00    /* No lock order relation. */
165 #define	WITNESS_PARENT           0x01    /* Parent, aka direct ancestor. */
166 #define	WITNESS_ANCESTOR         0x02    /* Direct or indirect ancestor. */
167 #define	WITNESS_CHILD            0x04    /* Child, aka direct descendant. */
168 #define	WITNESS_DESCENDANT       0x08    /* Direct or indirect descendant. */
169 #define	WITNESS_ANCESTOR_MASK    (WITNESS_PARENT | WITNESS_ANCESTOR)
170 #define	WITNESS_DESCENDANT_MASK  (WITNESS_CHILD | WITNESS_DESCENDANT)
171 #define	WITNESS_RELATED_MASK						\
172 	(WITNESS_ANCESTOR_MASK | WITNESS_DESCENDANT_MASK)
173 #define	WITNESS_REVERSAL         0x10    /* A lock order reversal has been
174 					  * observed. */
175 #define	WITNESS_RESERVED1        0x20    /* Unused flag, reserved. */
176 #define	WITNESS_RESERVED2        0x40    /* Unused flag, reserved. */
177 #define	WITNESS_LOCK_ORDER_KNOWN 0x80    /* This lock order is known. */
178 
179 /* Descendant to ancestor flags */
180 #define	WITNESS_DTOA(x)	(((x) & WITNESS_RELATED_MASK) >> 2)
181 
182 /* Ancestor to descendant flags */
183 #define	WITNESS_ATOD(x)	(((x) & WITNESS_RELATED_MASK) << 2)
184 
185 #define	WITNESS_INDEX_ASSERT(i)						\
186 	MPASS((i) > 0 && (i) <= w_max_used_index && (i) < WITNESS_COUNT)
187 
188 MALLOC_DEFINE(M_WITNESS, "Witness", "Witness");
189 
190 /*
191  * Lock instances.  A lock instance is the data associated with a lock while
192  * it is held by witness.  For example, a lock instance will hold the
193  * recursion count of a lock.  Lock instances are held in lists.  Spin locks
194  * are held in a per-cpu list while sleep locks are held in per-thread list.
195  */
196 struct lock_instance {
197 	struct lock_object	*li_lock;
198 	const char		*li_file;
199 	int			li_line;
200 	u_int			li_flags;
201 };
202 
203 /*
204  * A simple list type used to build the list of locks held by a thread
205  * or CPU.  We can't simply embed the list in struct lock_object since a
206  * lock may be held by more than one thread if it is a shared lock.  Locks
207  * are added to the head of the list, so we fill up each list entry from
208  * "the back" logically.  To ease some of the arithmetic, we actually fill
209  * in each list entry the normal way (children[0] then children[1], etc.) but
210  * when we traverse the list we read children[count-1] as the first entry
211  * down to children[0] as the final entry.
212  */
213 struct lock_list_entry {
214 	struct lock_list_entry	*ll_next;
215 	struct lock_instance	ll_children[LOCK_NCHILDREN];
216 	u_int			ll_count;
217 };
218 
219 /*
220  * The main witness structure. One of these per named lock type in the system
221  * (for example, "vnode interlock").
222  */
223 struct witness {
224 	char  			w_name[MAX_W_NAME];
225 	uint32_t 		w_index;  /* Index in the relationship matrix */
226 	struct lock_class	*w_class;
227 	STAILQ_ENTRY(witness) 	w_list;		/* List of all witnesses. */
228 	STAILQ_ENTRY(witness) 	w_typelist;	/* Witnesses of a type. */
229 	struct witness		*w_hash_next; /* Linked list in hash buckets. */
230 	const char		*w_file; /* File where last acquired */
231 	uint32_t 		w_line; /* Line where last acquired */
232 	uint32_t 		w_refcount;
233 	uint16_t 		w_num_ancestors; /* direct/indirect
234 						  * ancestor count */
235 	uint16_t 		w_num_descendants; /* direct/indirect
236 						    * descendant count */
237 	int16_t 		w_ddb_level;
238 	unsigned		w_displayed:1;
239 	unsigned		w_reversed:1;
240 };
241 
242 STAILQ_HEAD(witness_list, witness);
243 
244 /*
245  * The witness hash table. Keys are witness names (const char *), elements are
246  * witness objects (struct witness *).
247  */
248 struct witness_hash {
249 	struct witness	*wh_array[WITNESS_HASH_SIZE];
250 	uint32_t	wh_size;
251 	uint32_t	wh_count;
252 };
253 
254 /*
255  * Key type for the lock order data hash table.
256  */
257 struct witness_lock_order_key {
258 	uint16_t	from;
259 	uint16_t	to;
260 };
261 
262 struct witness_lock_order_data {
263 	struct stack			wlod_stack;
264 	struct witness_lock_order_key	wlod_key;
265 	struct witness_lock_order_data	*wlod_next;
266 };
267 
268 /*
269  * The witness lock order data hash table. Keys are witness index tuples
270  * (struct witness_lock_order_key), elements are lock order data objects
271  * (struct witness_lock_order_data).
272  */
273 struct witness_lock_order_hash {
274 	struct witness_lock_order_data	*wloh_array[WITNESS_LO_HASH_SIZE];
275 	u_int	wloh_size;
276 	u_int	wloh_count;
277 };
278 
279 #ifdef BLESSING
280 struct witness_blessed {
281 	const char	*b_lock1;
282 	const char	*b_lock2;
283 };
284 #endif
285 
286 struct witness_pendhelp {
287 	const char		*wh_type;
288 	struct lock_object	*wh_lock;
289 };
290 
291 struct witness_order_list_entry {
292 	const char		*w_name;
293 	struct lock_class	*w_class;
294 };
295 
296 /*
297  * Returns 0 if one of the locks is a spin lock and the other is not.
298  * Returns 1 otherwise.
299  */
300 static __inline int
301 witness_lock_type_equal(struct witness *w1, struct witness *w2)
302 {
303 
304 	return ((w1->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) ==
305 		(w2->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)));
306 }
307 
308 static __inline int
309 witness_lock_order_key_empty(const struct witness_lock_order_key *key)
310 {
311 
312 	return (key->from == 0 && key->to == 0);
313 }
314 
315 static __inline int
316 witness_lock_order_key_equal(const struct witness_lock_order_key *a,
317     const struct witness_lock_order_key *b)
318 {
319 
320 	return (a->from == b->from && a->to == b->to);
321 }
322 
323 static int	_isitmyx(struct witness *w1, struct witness *w2, int rmask,
324 		    const char *fname);
325 #ifdef KDB
326 static void	_witness_debugger(int cond, const char *msg);
327 #endif
328 static void	adopt(struct witness *parent, struct witness *child);
329 #ifdef BLESSING
330 static int	blessed(struct witness *, struct witness *);
331 #endif
332 static void	depart(struct witness *w);
333 static struct witness	*enroll(const char *description,
334 			    struct lock_class *lock_class);
335 static struct lock_instance	*find_instance(struct lock_list_entry *list,
336 				    struct lock_object *lock);
337 static int	isitmychild(struct witness *parent, struct witness *child);
338 static int	isitmydescendant(struct witness *parent, struct witness *child);
339 static void	itismychild(struct witness *parent, struct witness *child);
340 static int	sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS);
341 static int	sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
342 static int	sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS);
343 static void	witness_add_fullgraph(struct sbuf *sb, struct witness *parent);
344 #ifdef DDB
345 static void	witness_ddb_compute_levels(void);
346 static void	witness_ddb_display(void(*)(const char *fmt, ...));
347 static void	witness_ddb_display_descendants(void(*)(const char *fmt, ...),
348 		    struct witness *, int indent);
349 static void	witness_ddb_display_list(void(*prnt)(const char *fmt, ...),
350 		    struct witness_list *list);
351 static void	witness_ddb_level_descendants(struct witness *parent, int l);
352 static void	witness_ddb_list(struct thread *td);
353 #endif
354 static void	witness_free(struct witness *m);
355 static struct witness	*witness_get(void);
356 static uint32_t	witness_hash_djb2(const uint8_t *key, uint32_t size);
357 static struct witness	*witness_hash_get(const char *key);
358 static void	witness_hash_put(struct witness *w);
359 static void	witness_init_hash_tables(void);
360 static void	witness_increment_graph_generation(void);
361 static void	witness_lock_list_free(struct lock_list_entry *lle);
362 static struct lock_list_entry	*witness_lock_list_get(void);
363 static int	witness_lock_order_add(struct witness *parent,
364 		    struct witness *child);
365 static int	witness_lock_order_check(struct witness *parent,
366 		    struct witness *child);
367 static struct witness_lock_order_data	*witness_lock_order_get(
368 					    struct witness *parent,
369 					    struct witness *child);
370 static void	witness_list_lock(struct lock_instance *instance);
371 static void	witness_setflag(struct lock_object *lock, int flag, int set);
372 
373 #ifdef KDB
374 #define	witness_debugger(c)	_witness_debugger(c, __func__)
375 #else
376 #define	witness_debugger(c)
377 #endif
378 
379 SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, NULL, "Witness Locking");
380 
381 /*
382  * If set to 0, lock order checking is disabled.  If set to -1,
383  * witness is completely disabled.  Otherwise witness performs full
384  * lock order checking for all locks.  At runtime, lock order checking
385  * may be toggled.  However, witness cannot be reenabled once it is
386  * completely disabled.
387  */
388 static int witness_watch = 1;
389 TUNABLE_INT("debug.witness.watch", &witness_watch);
390 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
391     sysctl_debug_witness_watch, "I", "witness is watching lock operations");
392 
393 #ifdef KDB
394 /*
395  * When KDB is enabled and witness_kdb is 1, it will cause the system
396  * to drop into kdebug() when:
397  *	- a lock hierarchy violation occurs
398  *	- locks are held when going to sleep.
399  */
400 #ifdef WITNESS_KDB
401 int	witness_kdb = 1;
402 #else
403 int	witness_kdb = 0;
404 #endif
405 TUNABLE_INT("debug.witness.kdb", &witness_kdb);
406 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, "");
407 
408 /*
409  * When KDB is enabled and witness_trace is 1, it will cause the system
410  * to print a stack trace:
411  *	- a lock hierarchy violation occurs
412  *	- locks are held when going to sleep.
413  */
414 int	witness_trace = 1;
415 TUNABLE_INT("debug.witness.trace", &witness_trace);
416 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, "");
417 #endif /* KDB */
418 
419 #ifdef WITNESS_SKIPSPIN
420 int	witness_skipspin = 1;
421 #else
422 int	witness_skipspin = 0;
423 #endif
424 TUNABLE_INT("debug.witness.skipspin", &witness_skipspin);
425 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, &witness_skipspin,
426     0, "");
427 
428 /*
429  * Call this to print out the relations between locks.
430  */
431 SYSCTL_PROC(_debug_witness, OID_AUTO, fullgraph, CTLTYPE_STRING | CTLFLAG_RD,
432     NULL, 0, sysctl_debug_witness_fullgraph, "A", "Show locks relation graphs");
433 
434 /*
435  * Call this to print out the witness faulty stacks.
436  */
437 SYSCTL_PROC(_debug_witness, OID_AUTO, badstacks, CTLTYPE_STRING | CTLFLAG_RD,
438     NULL, 0, sysctl_debug_witness_badstacks, "A", "Show bad witness stacks");
439 
440 static struct mtx w_mtx;
441 
442 /* w_list */
443 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
444 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
445 
446 /* w_typelist */
447 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
448 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
449 
450 /* lock list */
451 static struct lock_list_entry *w_lock_list_free = NULL;
452 static struct witness_pendhelp pending_locks[WITNESS_PENDLIST];
453 static u_int pending_cnt;
454 
455 static int w_free_cnt, w_spin_cnt, w_sleep_cnt;
456 SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, "");
457 SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, "");
458 SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0,
459     "");
460 
461 static struct witness *w_data;
462 static uint8_t w_rmatrix[WITNESS_COUNT+1][WITNESS_COUNT+1];
463 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
464 static struct witness_hash w_hash;	/* The witness hash table. */
465 
466 /* The lock order data hash */
467 static struct witness_lock_order_data w_lodata[WITNESS_LO_DATA_COUNT];
468 static struct witness_lock_order_data *w_lofree = NULL;
469 static struct witness_lock_order_hash w_lohash;
470 static int w_max_used_index = 0;
471 static unsigned int w_generation = 0;
472 static const char *w_notrunning = "Witness not running\n";
473 static const char *w_stillcold = "Witness is still cold\n";
474 
475 
476 static struct witness_order_list_entry order_lists[] = {
477 	/*
478 	 * sx locks
479 	 */
480 	{ "proctree", &lock_class_sx },
481 	{ "allproc", &lock_class_sx },
482 	{ "allprison", &lock_class_sx },
483 	{ NULL, NULL },
484 	/*
485 	 * Various mutexes
486 	 */
487 	{ "Giant", &lock_class_mtx_sleep },
488 	{ "pipe mutex", &lock_class_mtx_sleep },
489 	{ "sigio lock", &lock_class_mtx_sleep },
490 	{ "process group", &lock_class_mtx_sleep },
491 	{ "process lock", &lock_class_mtx_sleep },
492 	{ "session", &lock_class_mtx_sleep },
493 	{ "uidinfo hash", &lock_class_rw },
494 #ifdef	HWPMC_HOOKS
495 	{ "pmc-sleep", &lock_class_mtx_sleep },
496 #endif
497 	{ NULL, NULL },
498 	/*
499 	 * Sockets
500 	 */
501 	{ "accept", &lock_class_mtx_sleep },
502 	{ "so_snd", &lock_class_mtx_sleep },
503 	{ "so_rcv", &lock_class_mtx_sleep },
504 	{ "sellck", &lock_class_mtx_sleep },
505 	{ NULL, NULL },
506 	/*
507 	 * Routing
508 	 */
509 	{ "so_rcv", &lock_class_mtx_sleep },
510 	{ "radix node head", &lock_class_rw },
511 	{ "rtentry", &lock_class_mtx_sleep },
512 	{ "ifaddr", &lock_class_mtx_sleep },
513 	{ NULL, NULL },
514 	/*
515 	 * Multicast - protocol locks before interface locks, after UDP locks.
516 	 */
517 	{ "udpinp", &lock_class_rw },
518 	{ "in_multi_mtx", &lock_class_mtx_sleep },
519 	{ "igmp_mtx", &lock_class_mtx_sleep },
520 	{ "if_addr_mtx", &lock_class_mtx_sleep },
521 	{ NULL, NULL },
522 	/*
523 	 * UNIX Domain Sockets
524 	 */
525 	{ "unp_global_rwlock", &lock_class_rw },
526 	{ "unp_list_lock", &lock_class_mtx_sleep },
527 	{ "unp", &lock_class_mtx_sleep },
528 	{ "so_snd", &lock_class_mtx_sleep },
529 	{ NULL, NULL },
530 	/*
531 	 * UDP/IP
532 	 */
533 	{ "udp", &lock_class_rw },
534 	{ "udpinp", &lock_class_rw },
535 	{ "so_snd", &lock_class_mtx_sleep },
536 	{ NULL, NULL },
537 	/*
538 	 * TCP/IP
539 	 */
540 	{ "tcp", &lock_class_rw },
541 	{ "tcpinp", &lock_class_rw },
542 	{ "so_snd", &lock_class_mtx_sleep },
543 	{ NULL, NULL },
544 	/*
545 	 * SLIP
546 	 */
547 	{ "slip_mtx", &lock_class_mtx_sleep },
548 	{ "slip sc_mtx", &lock_class_mtx_sleep },
549 	{ NULL, NULL },
550 	/*
551 	 * netatalk
552 	 */
553 	{ "ddp_list_mtx", &lock_class_mtx_sleep },
554 	{ "ddp_mtx", &lock_class_mtx_sleep },
555 	{ NULL, NULL },
556 	/*
557 	 * BPF
558 	 */
559 	{ "bpf global lock", &lock_class_mtx_sleep },
560 	{ "bpf interface lock", &lock_class_mtx_sleep },
561 	{ "bpf cdev lock", &lock_class_mtx_sleep },
562 	{ NULL, NULL },
563 	/*
564 	 * NFS server
565 	 */
566 	{ "nfsd_mtx", &lock_class_mtx_sleep },
567 	{ "so_snd", &lock_class_mtx_sleep },
568 	{ NULL, NULL },
569 
570 	/*
571 	 * IEEE 802.11
572 	 */
573 	{ "802.11 com lock", &lock_class_mtx_sleep},
574 	{ NULL, NULL },
575 	/*
576 	 * Network drivers
577 	 */
578 	{ "network driver", &lock_class_mtx_sleep},
579 	{ NULL, NULL },
580 
581 	/*
582 	 * Netgraph
583 	 */
584 	{ "ng_node", &lock_class_mtx_sleep },
585 	{ "ng_worklist", &lock_class_mtx_sleep },
586 	{ NULL, NULL },
587 	/*
588 	 * CDEV
589 	 */
590 	{ "system map", &lock_class_mtx_sleep },
591 	{ "vm page queue mutex", &lock_class_mtx_sleep },
592 	{ "vnode interlock", &lock_class_mtx_sleep },
593 	{ "cdev", &lock_class_mtx_sleep },
594 	{ NULL, NULL },
595 	/*
596 	 * kqueue/VFS interaction
597 	 */
598 	{ "kqueue", &lock_class_mtx_sleep },
599 	{ "struct mount mtx", &lock_class_mtx_sleep },
600 	{ "vnode interlock", &lock_class_mtx_sleep },
601 	{ NULL, NULL },
602 	/*
603 	 * spin locks
604 	 */
605 #ifdef SMP
606 	{ "ap boot", &lock_class_mtx_spin },
607 #endif
608 	{ "rm.mutex_mtx", &lock_class_mtx_spin },
609 	{ "sio", &lock_class_mtx_spin },
610 	{ "scrlock", &lock_class_mtx_spin },
611 #ifdef __i386__
612 	{ "cy", &lock_class_mtx_spin },
613 #endif
614 #ifdef __sparc64__
615 	{ "pcib_mtx", &lock_class_mtx_spin },
616 	{ "rtc_mtx", &lock_class_mtx_spin },
617 #endif
618 	{ "scc_hwmtx", &lock_class_mtx_spin },
619 	{ "uart_hwmtx", &lock_class_mtx_spin },
620 	{ "fast_taskqueue", &lock_class_mtx_spin },
621 	{ "intr table", &lock_class_mtx_spin },
622 #ifdef	HWPMC_HOOKS
623 	{ "pmc-per-proc", &lock_class_mtx_spin },
624 #endif
625 	{ "process slock", &lock_class_mtx_spin },
626 	{ "sleepq chain", &lock_class_mtx_spin },
627 	{ "umtx lock", &lock_class_mtx_spin },
628 	{ "rm_spinlock", &lock_class_mtx_spin },
629 	{ "turnstile chain", &lock_class_mtx_spin },
630 	{ "turnstile lock", &lock_class_mtx_spin },
631 	{ "sched lock", &lock_class_mtx_spin },
632 	{ "td_contested", &lock_class_mtx_spin },
633 	{ "callout", &lock_class_mtx_spin },
634 	{ "entropy harvest mutex", &lock_class_mtx_spin },
635 	{ "syscons video lock", &lock_class_mtx_spin },
636 	{ "time lock", &lock_class_mtx_spin },
637 #ifdef SMP
638 	{ "smp rendezvous", &lock_class_mtx_spin },
639 #endif
640 #ifdef __powerpc__
641 	{ "tlb0", &lock_class_mtx_spin },
642 #endif
643 	/*
644 	 * leaf locks
645 	 */
646 	{ "intrcnt", &lock_class_mtx_spin },
647 	{ "icu", &lock_class_mtx_spin },
648 #if defined(SMP) && defined(__sparc64__)
649 	{ "ipi", &lock_class_mtx_spin },
650 #endif
651 #ifdef __i386__
652 	{ "allpmaps", &lock_class_mtx_spin },
653 	{ "descriptor tables", &lock_class_mtx_spin },
654 #endif
655 	{ "clk", &lock_class_mtx_spin },
656 	{ "cpuset", &lock_class_mtx_spin },
657 	{ "mprof lock", &lock_class_mtx_spin },
658 	{ "zombie lock", &lock_class_mtx_spin },
659 	{ "ALD Queue", &lock_class_mtx_spin },
660 #ifdef __ia64__
661 	{ "MCA spin lock", &lock_class_mtx_spin },
662 #endif
663 #if defined(__i386__) || defined(__amd64__)
664 	{ "pcicfg", &lock_class_mtx_spin },
665 	{ "NDIS thread lock", &lock_class_mtx_spin },
666 #endif
667 	{ "tw_osl_io_lock", &lock_class_mtx_spin },
668 	{ "tw_osl_q_lock", &lock_class_mtx_spin },
669 	{ "tw_cl_io_lock", &lock_class_mtx_spin },
670 	{ "tw_cl_intr_lock", &lock_class_mtx_spin },
671 	{ "tw_cl_gen_lock", &lock_class_mtx_spin },
672 #ifdef	HWPMC_HOOKS
673 	{ "pmc-leaf", &lock_class_mtx_spin },
674 #endif
675 	{ "blocked lock", &lock_class_mtx_spin },
676 	{ NULL, NULL },
677 	{ NULL, NULL }
678 };
679 
680 #ifdef BLESSING
681 /*
682  * Pairs of locks which have been blessed
683  * Don't complain about order problems with blessed locks
684  */
685 static struct witness_blessed blessed_list[] = {
686 };
687 static int blessed_count =
688 	sizeof(blessed_list) / sizeof(struct witness_blessed);
689 #endif
690 
691 /*
692  * This global is set to 0 once it becomes safe to use the witness code.
693  */
694 static int witness_cold = 1;
695 
696 /*
697  * This global is set to 1 once the static lock orders have been enrolled
698  * so that a warning can be issued for any spin locks enrolled later.
699  */
700 static int witness_spin_warn = 0;
701 
702 /*
703  * The WITNESS-enabled diagnostic code.  Note that the witness code does
704  * assume that the early boot is single-threaded at least until after this
705  * routine is completed.
706  */
707 static void
708 witness_initialize(void *dummy __unused)
709 {
710 	struct lock_object *lock;
711 	struct witness_order_list_entry *order;
712 	struct witness *w, *w1;
713 	int i;
714 
715 	w_data = malloc(sizeof (struct witness) * WITNESS_COUNT, M_WITNESS,
716 	    M_NOWAIT | M_ZERO);
717 
718 	/*
719 	 * We have to release Giant before initializing its witness
720 	 * structure so that WITNESS doesn't get confused.
721 	 */
722 	mtx_unlock(&Giant);
723 	mtx_assert(&Giant, MA_NOTOWNED);
724 
725 	CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
726 	mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
727 	    MTX_NOWITNESS | MTX_NOPROFILE);
728 	for (i = WITNESS_COUNT - 1; i >= 0; i--) {
729 		w = &w_data[i];
730 		memset(w, 0, sizeof(*w));
731 		w_data[i].w_index = i;	/* Witness index never changes. */
732 		witness_free(w);
733 	}
734 	KASSERT(STAILQ_FIRST(&w_free)->w_index == 0,
735 	    ("%s: Invalid list of free witness objects", __func__));
736 
737 	/* Witness with index 0 is not used to aid in debugging. */
738 	STAILQ_REMOVE_HEAD(&w_free, w_list);
739 	w_free_cnt--;
740 
741 	memset(w_rmatrix, 0,
742 	    (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1)));
743 
744 	for (i = 0; i < LOCK_CHILDCOUNT; i++)
745 		witness_lock_list_free(&w_locklistdata[i]);
746 	witness_init_hash_tables();
747 
748 	/* First add in all the specified order lists. */
749 	for (order = order_lists; order->w_name != NULL; order++) {
750 		w = enroll(order->w_name, order->w_class);
751 		if (w == NULL)
752 			continue;
753 		w->w_file = "order list";
754 		for (order++; order->w_name != NULL; order++) {
755 			w1 = enroll(order->w_name, order->w_class);
756 			if (w1 == NULL)
757 				continue;
758 			w1->w_file = "order list";
759 			itismychild(w, w1);
760 			w = w1;
761 		}
762 	}
763 	witness_spin_warn = 1;
764 
765 	/* Iterate through all locks and add them to witness. */
766 	for (i = 0; pending_locks[i].wh_lock != NULL; i++) {
767 		lock = pending_locks[i].wh_lock;
768 		KASSERT(lock->lo_flags & LO_WITNESS,
769 		    ("%s: lock %s is on pending list but not LO_WITNESS",
770 		    __func__, lock->lo_name));
771 		lock->lo_witness = enroll(pending_locks[i].wh_type,
772 		    LOCK_CLASS(lock));
773 	}
774 
775 	/* Mark the witness code as being ready for use. */
776 	witness_cold = 0;
777 
778 	mtx_lock(&Giant);
779 }
780 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize,
781     NULL);
782 
783 void
784 witness_init(struct lock_object *lock, const char *type)
785 {
786 	struct lock_class *class;
787 
788 	/* Various sanity checks. */
789 	class = LOCK_CLASS(lock);
790 	if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
791 	    (class->lc_flags & LC_RECURSABLE) == 0)
792 		panic("%s: lock (%s) %s can not be recursable", __func__,
793 		    class->lc_name, lock->lo_name);
794 	if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
795 	    (class->lc_flags & LC_SLEEPABLE) == 0)
796 		panic("%s: lock (%s) %s can not be sleepable", __func__,
797 		    class->lc_name, lock->lo_name);
798 	if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
799 	    (class->lc_flags & LC_UPGRADABLE) == 0)
800 		panic("%s: lock (%s) %s can not be upgradable", __func__,
801 		    class->lc_name, lock->lo_name);
802 
803 	/*
804 	 * If we shouldn't watch this lock, then just clear lo_witness.
805 	 * Otherwise, if witness_cold is set, then it is too early to
806 	 * enroll this lock, so defer it to witness_initialize() by adding
807 	 * it to the pending_locks list.  If it is not too early, then enroll
808 	 * the lock now.
809 	 */
810 	if (witness_watch < 1 || panicstr != NULL ||
811 	    (lock->lo_flags & LO_WITNESS) == 0)
812 		lock->lo_witness = NULL;
813 	else if (witness_cold) {
814 		pending_locks[pending_cnt].wh_lock = lock;
815 		pending_locks[pending_cnt++].wh_type = type;
816 		if (pending_cnt > WITNESS_PENDLIST)
817 			panic("%s: pending locks list is too small, bump it\n",
818 			    __func__);
819 	} else
820 		lock->lo_witness = enroll(type, class);
821 }
822 
823 void
824 witness_destroy(struct lock_object *lock)
825 {
826 	struct lock_class *class;
827 	struct witness *w;
828 
829 	class = LOCK_CLASS(lock);
830 
831 	if (witness_cold)
832 		panic("lock (%s) %s destroyed while witness_cold",
833 		    class->lc_name, lock->lo_name);
834 
835 	/* XXX: need to verify that no one holds the lock */
836 	if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL)
837 		return;
838 	w = lock->lo_witness;
839 
840 	mtx_lock_spin(&w_mtx);
841 	MPASS(w->w_refcount > 0);
842 	w->w_refcount--;
843 
844 	if (w->w_refcount == 0)
845 		depart(w);
846 	mtx_unlock_spin(&w_mtx);
847 }
848 
849 #ifdef DDB
850 static void
851 witness_ddb_compute_levels(void)
852 {
853 	struct witness *w;
854 
855 	/*
856 	 * First clear all levels.
857 	 */
858 	STAILQ_FOREACH(w, &w_all, w_list)
859 		w->w_ddb_level = -1;
860 
861 	/*
862 	 * Look for locks with no parents and level all their descendants.
863 	 */
864 	STAILQ_FOREACH(w, &w_all, w_list) {
865 
866 		/* If the witness has ancestors (is not a root), skip it. */
867 		if (w->w_num_ancestors > 0)
868 			continue;
869 		witness_ddb_level_descendants(w, 0);
870 	}
871 }
872 
873 static void
874 witness_ddb_level_descendants(struct witness *w, int l)
875 {
876 	int i;
877 
878 	if (w->w_ddb_level >= l)
879 		return;
880 
881 	w->w_ddb_level = l;
882 	l++;
883 
884 	for (i = 1; i <= w_max_used_index; i++) {
885 		if (w_rmatrix[w->w_index][i] & WITNESS_PARENT)
886 			witness_ddb_level_descendants(&w_data[i], l);
887 	}
888 }
889 
890 static void
891 witness_ddb_display_descendants(void(*prnt)(const char *fmt, ...),
892     struct witness *w, int indent)
893 {
894 	int i;
895 
896  	for (i = 0; i < indent; i++)
897  		prnt(" ");
898 	prnt("%s (type: %s, depth: %d, active refs: %d)",
899 	     w->w_name, w->w_class->lc_name,
900 	     w->w_ddb_level, w->w_refcount);
901  	if (w->w_displayed) {
902  		prnt(" -- (already displayed)\n");
903  		return;
904  	}
905  	w->w_displayed = 1;
906 	if (w->w_file != NULL && w->w_line != 0)
907 		prnt(" -- last acquired @ %s:%d\n", w->w_file,
908 		    w->w_line);
909 	else
910 		prnt(" -- never acquired\n");
911 	indent++;
912 	WITNESS_INDEX_ASSERT(w->w_index);
913 	for (i = 1; i <= w_max_used_index; i++) {
914 		if (w_rmatrix[w->w_index][i] & WITNESS_PARENT)
915 			witness_ddb_display_descendants(prnt, &w_data[i],
916 			    indent);
917 	}
918 }
919 
920 static void
921 witness_ddb_display_list(void(*prnt)(const char *fmt, ...),
922     struct witness_list *list)
923 {
924 	struct witness *w;
925 
926 	STAILQ_FOREACH(w, list, w_typelist) {
927 		if (w->w_file == NULL || w->w_ddb_level > 0)
928 			continue;
929 
930 		/* This lock has no anscestors - display its descendants. */
931 		witness_ddb_display_descendants(prnt, w, 0);
932 	}
933 }
934 
935 static void
936 witness_ddb_display(void(*prnt)(const char *fmt, ...))
937 {
938 	struct witness *w;
939 
940 	KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
941 	witness_ddb_compute_levels();
942 
943 	/* Clear all the displayed flags. */
944 	STAILQ_FOREACH(w, &w_all, w_list)
945 		w->w_displayed = 0;
946 
947 	/*
948 	 * First, handle sleep locks which have been acquired at least
949 	 * once.
950 	 */
951 	prnt("Sleep locks:\n");
952 	witness_ddb_display_list(prnt, &w_sleep);
953 
954 	/*
955 	 * Now do spin locks which have been acquired at least once.
956 	 */
957 	prnt("\nSpin locks:\n");
958 	witness_ddb_display_list(prnt, &w_spin);
959 
960 	/*
961 	 * Finally, any locks which have not been acquired yet.
962 	 */
963 	prnt("\nLocks which were never acquired:\n");
964 	STAILQ_FOREACH(w, &w_all, w_list) {
965 		if (w->w_file != NULL || w->w_refcount == 0)
966 			continue;
967 		prnt("%s (type: %s, depth: %d)\n", w->w_name,
968 		    w->w_class->lc_name, w->w_ddb_level);
969 	}
970 }
971 #endif /* DDB */
972 
973 /* Trim useless garbage from filenames. */
974 static const char *
975 fixup_filename(const char *file)
976 {
977 
978 	if (file == NULL)
979 		return (NULL);
980 	while (strncmp(file, "../", 3) == 0)
981 		file += 3;
982 	return (file);
983 }
984 
985 int
986 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
987 {
988 
989 	if (witness_watch == -1 || panicstr != NULL)
990 		return (0);
991 
992 	/* Require locks that witness knows about. */
993 	if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
994 	    lock2->lo_witness == NULL)
995 		return (EINVAL);
996 
997 	mtx_assert(&w_mtx, MA_NOTOWNED);
998 	mtx_lock_spin(&w_mtx);
999 
1000 	/*
1001 	 * If we already have either an explicit or implied lock order that
1002 	 * is the other way around, then return an error.
1003 	 */
1004 	if (witness_watch &&
1005 	    isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
1006 		mtx_unlock_spin(&w_mtx);
1007 		return (EDOOFUS);
1008 	}
1009 
1010 	/* Try to add the new order. */
1011 	CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1012 	    lock2->lo_witness->w_name, lock1->lo_witness->w_name);
1013 	itismychild(lock1->lo_witness, lock2->lo_witness);
1014 	mtx_unlock_spin(&w_mtx);
1015 	return (0);
1016 }
1017 
1018 void
1019 witness_checkorder(struct lock_object *lock, int flags, const char *file,
1020     int line, struct lock_object *interlock)
1021 {
1022 	struct lock_list_entry *lock_list, *lle;
1023 	struct lock_instance *lock1, *lock2, *plock;
1024 	struct lock_class *class;
1025 	struct witness *w, *w1;
1026 	struct thread *td;
1027 	int i, j;
1028 
1029 	if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL ||
1030 	    panicstr != NULL)
1031 		return;
1032 
1033 	w = lock->lo_witness;
1034 	class = LOCK_CLASS(lock);
1035 	td = curthread;
1036 	file = fixup_filename(file);
1037 
1038 	if (class->lc_flags & LC_SLEEPLOCK) {
1039 
1040 		/*
1041 		 * Since spin locks include a critical section, this check
1042 		 * implicitly enforces a lock order of all sleep locks before
1043 		 * all spin locks.
1044 		 */
1045 		if (td->td_critnest != 0 && !kdb_active)
1046 			panic("blockable sleep lock (%s) %s @ %s:%d",
1047 			    class->lc_name, lock->lo_name, file, line);
1048 
1049 		/*
1050 		 * If this is the first lock acquired then just return as
1051 		 * no order checking is needed.
1052 		 */
1053 		lock_list = td->td_sleeplocks;
1054 		if (lock_list == NULL || lock_list->ll_count == 0)
1055 			return;
1056 	} else {
1057 
1058 		/*
1059 		 * If this is the first lock, just return as no order
1060 		 * checking is needed.  Avoid problems with thread
1061 		 * migration pinning the thread while checking if
1062 		 * spinlocks are held.  If at least one spinlock is held
1063 		 * the thread is in a safe path and it is allowed to
1064 		 * unpin it.
1065 		 */
1066 		sched_pin();
1067 		lock_list = PCPU_GET(spinlocks);
1068 		if (lock_list == NULL || lock_list->ll_count == 0) {
1069 			sched_unpin();
1070 			return;
1071 		}
1072 		sched_unpin();
1073 	}
1074 
1075 	/*
1076 	 * Check to see if we are recursing on a lock we already own.  If
1077 	 * so, make sure that we don't mismatch exclusive and shared lock
1078 	 * acquires.
1079 	 */
1080 	lock1 = find_instance(lock_list, lock);
1081 	if (lock1 != NULL) {
1082 		if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
1083 		    (flags & LOP_EXCLUSIVE) == 0) {
1084 			printf("shared lock of (%s) %s @ %s:%d\n",
1085 			    class->lc_name, lock->lo_name, file, line);
1086 			printf("while exclusively locked from %s:%d\n",
1087 			    lock1->li_file, lock1->li_line);
1088 			panic("share->excl");
1089 		}
1090 		if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
1091 		    (flags & LOP_EXCLUSIVE) != 0) {
1092 			printf("exclusive lock of (%s) %s @ %s:%d\n",
1093 			    class->lc_name, lock->lo_name, file, line);
1094 			printf("while share locked from %s:%d\n",
1095 			    lock1->li_file, lock1->li_line);
1096 			panic("excl->share");
1097 		}
1098 		return;
1099 	}
1100 
1101 	/*
1102 	 * Find the previously acquired lock, but ignore interlocks.
1103 	 */
1104 	plock = &lock_list->ll_children[lock_list->ll_count - 1];
1105 	if (interlock != NULL && plock->li_lock == interlock) {
1106 		if (lock_list->ll_count > 1)
1107 			plock =
1108 			    &lock_list->ll_children[lock_list->ll_count - 2];
1109 		else {
1110 			lle = lock_list->ll_next;
1111 
1112 			/*
1113 			 * The interlock is the only lock we hold, so
1114 			 * simply return.
1115 			 */
1116 			if (lle == NULL)
1117 				return;
1118 			plock = &lle->ll_children[lle->ll_count - 1];
1119 		}
1120 	}
1121 
1122 	/*
1123 	 * Try to perform most checks without a lock.  If this succeeds we
1124 	 * can skip acquiring the lock and return success.
1125 	 */
1126 	w1 = plock->li_lock->lo_witness;
1127 	if (witness_lock_order_check(w1, w))
1128 		return;
1129 
1130 	/*
1131 	 * Check for duplicate locks of the same type.  Note that we only
1132 	 * have to check for this on the last lock we just acquired.  Any
1133 	 * other cases will be caught as lock order violations.
1134 	 */
1135 	mtx_lock_spin(&w_mtx);
1136 	witness_lock_order_add(w1, w);
1137 	if (w1 == w) {
1138 		i = w->w_index;
1139 		if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) &&
1140 		    !(w_rmatrix[i][i] & WITNESS_REVERSAL)) {
1141 		    w_rmatrix[i][i] |= WITNESS_REVERSAL;
1142 			w->w_reversed = 1;
1143 			mtx_unlock_spin(&w_mtx);
1144 			printf(
1145 			    "acquiring duplicate lock of same type: \"%s\"\n",
1146 			    w->w_name);
1147 			printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name,
1148 			       plock->li_file, plock->li_line);
1149 			printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
1150 			witness_debugger(1);
1151 		    } else
1152 			    mtx_unlock_spin(&w_mtx);
1153 		return;
1154 	}
1155 	mtx_assert(&w_mtx, MA_OWNED);
1156 
1157 	/*
1158 	 * If we know that the the lock we are acquiring comes after
1159 	 * the lock we most recently acquired in the lock order tree,
1160 	 * then there is no need for any further checks.
1161 	 */
1162 	if (isitmychild(w1, w))
1163 		goto out;
1164 
1165 	for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) {
1166 		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
1167 
1168 			MPASS(j < WITNESS_COUNT);
1169 			lock1 = &lle->ll_children[i];
1170 
1171 			/*
1172 			 * Ignore the interlock the first time we see it.
1173 			 */
1174 			if (interlock != NULL && interlock == lock1->li_lock) {
1175 				interlock = NULL;
1176 				continue;
1177 			}
1178 
1179 			/*
1180 			 * If this lock doesn't undergo witness checking,
1181 			 * then skip it.
1182 			 */
1183 			w1 = lock1->li_lock->lo_witness;
1184 			if (w1 == NULL) {
1185 				KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
1186 				    ("lock missing witness structure"));
1187 				continue;
1188 			}
1189 
1190 			/*
1191 			 * If we are locking Giant and this is a sleepable
1192 			 * lock, then skip it.
1193 			 */
1194 			if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
1195 			    lock == &Giant.lock_object)
1196 				continue;
1197 
1198 			/*
1199 			 * If we are locking a sleepable lock and this lock
1200 			 * is Giant, then skip it.
1201 			 */
1202 			if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1203 			    lock1->li_lock == &Giant.lock_object)
1204 				continue;
1205 
1206 			/*
1207 			 * If we are locking a sleepable lock and this lock
1208 			 * isn't sleepable, we want to treat it as a lock
1209 			 * order violation to enfore a general lock order of
1210 			 * sleepable locks before non-sleepable locks.
1211 			 */
1212 			if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1213 			    (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1214 				goto reversal;
1215 
1216 			/*
1217 			 * If we are locking Giant and this is a non-sleepable
1218 			 * lock, then treat it as a reversal.
1219 			 */
1220 			if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 &&
1221 			    lock == &Giant.lock_object)
1222 				goto reversal;
1223 
1224 			/*
1225 			 * Check the lock order hierarchy for a reveresal.
1226 			 */
1227 			if (!isitmydescendant(w, w1))
1228 				continue;
1229 		reversal:
1230 
1231 			/*
1232 			 * We have a lock order violation, check to see if it
1233 			 * is allowed or has already been yelled about.
1234 			 */
1235 #ifdef BLESSING
1236 
1237 			/*
1238 			 * If the lock order is blessed, just bail.  We don't
1239 			 * look for other lock order violations though, which
1240 			 * may be a bug.
1241 			 */
1242 			if (blessed(w, w1))
1243 				goto out;
1244 #endif
1245 
1246 			/* Bail if this violation is known */
1247 			if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL)
1248 				goto out;
1249 
1250 			/* Record this as a violation */
1251 			w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL;
1252 			w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL;
1253 			w->w_reversed = w1->w_reversed = 1;
1254 			witness_increment_graph_generation();
1255 			mtx_unlock_spin(&w_mtx);
1256 
1257 			/*
1258 			 * Ok, yell about it.
1259 			 */
1260 			if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1261 			    (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1262 				printf(
1263 		"lock order reversal: (sleepable after non-sleepable)\n");
1264 			else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0
1265 			    && lock == &Giant.lock_object)
1266 				printf(
1267 		"lock order reversal: (Giant after non-sleepable)\n");
1268 			else
1269 				printf("lock order reversal:\n");
1270 
1271 			/*
1272 			 * Try to locate an earlier lock with
1273 			 * witness w in our list.
1274 			 */
1275 			do {
1276 				lock2 = &lle->ll_children[i];
1277 				MPASS(lock2->li_lock != NULL);
1278 				if (lock2->li_lock->lo_witness == w)
1279 					break;
1280 				if (i == 0 && lle->ll_next != NULL) {
1281 					lle = lle->ll_next;
1282 					i = lle->ll_count - 1;
1283 					MPASS(i >= 0 && i < LOCK_NCHILDREN);
1284 				} else
1285 					i--;
1286 			} while (i >= 0);
1287 			if (i < 0) {
1288 				printf(" 1st %p %s (%s) @ %s:%d\n",
1289 				    lock1->li_lock, lock1->li_lock->lo_name,
1290 				    w1->w_name, lock1->li_file, lock1->li_line);
1291 				printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
1292 				    lock->lo_name, w->w_name, file, line);
1293 			} else {
1294 				printf(" 1st %p %s (%s) @ %s:%d\n",
1295 				    lock2->li_lock, lock2->li_lock->lo_name,
1296 				    lock2->li_lock->lo_witness->w_name,
1297 				    lock2->li_file, lock2->li_line);
1298 				printf(" 2nd %p %s (%s) @ %s:%d\n",
1299 				    lock1->li_lock, lock1->li_lock->lo_name,
1300 				    w1->w_name, lock1->li_file, lock1->li_line);
1301 				printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
1302 				    lock->lo_name, w->w_name, file, line);
1303 			}
1304 			witness_debugger(1);
1305 			return;
1306 		}
1307 	}
1308 
1309 	/*
1310 	 * If requested, build a new lock order.  However, don't build a new
1311 	 * relationship between a sleepable lock and Giant if it is in the
1312 	 * wrong direction.  The correct lock order is that sleepable locks
1313 	 * always come before Giant.
1314 	 */
1315 	if (flags & LOP_NEWORDER &&
1316 	    !(plock->li_lock == &Giant.lock_object &&
1317 	    (lock->lo_flags & LO_SLEEPABLE) != 0)) {
1318 		CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1319 		    w->w_name, plock->li_lock->lo_witness->w_name);
1320 		itismychild(plock->li_lock->lo_witness, w);
1321 	}
1322 out:
1323 	mtx_unlock_spin(&w_mtx);
1324 }
1325 
1326 void
1327 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
1328 {
1329 	struct lock_list_entry **lock_list, *lle;
1330 	struct lock_instance *instance;
1331 	struct witness *w;
1332 	struct thread *td;
1333 
1334 	if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL ||
1335 	    panicstr != NULL)
1336 		return;
1337 	w = lock->lo_witness;
1338 	td = curthread;
1339 	file = fixup_filename(file);
1340 
1341 	/* Determine lock list for this lock. */
1342 	if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK)
1343 		lock_list = &td->td_sleeplocks;
1344 	else
1345 		lock_list = PCPU_PTR(spinlocks);
1346 
1347 	/* Check to see if we are recursing on a lock we already own. */
1348 	instance = find_instance(*lock_list, lock);
1349 	if (instance != NULL) {
1350 		instance->li_flags++;
1351 		CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
1352 		    td->td_proc->p_pid, lock->lo_name,
1353 		    instance->li_flags & LI_RECURSEMASK);
1354 		instance->li_file = file;
1355 		instance->li_line = line;
1356 		return;
1357 	}
1358 
1359 	/* Update per-witness last file and line acquire. */
1360 	w->w_file = file;
1361 	w->w_line = line;
1362 
1363 	/* Find the next open lock instance in the list and fill it. */
1364 	lle = *lock_list;
1365 	if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1366 		lle = witness_lock_list_get();
1367 		if (lle == NULL)
1368 			return;
1369 		lle->ll_next = *lock_list;
1370 		CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1371 		    td->td_proc->p_pid, lle);
1372 		*lock_list = lle;
1373 	}
1374 	instance = &lle->ll_children[lle->ll_count++];
1375 	instance->li_lock = lock;
1376 	instance->li_line = line;
1377 	instance->li_file = file;
1378 	if ((flags & LOP_EXCLUSIVE) != 0)
1379 		instance->li_flags = LI_EXCLUSIVE;
1380 	else
1381 		instance->li_flags = 0;
1382 	CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1383 	    td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1384 }
1385 
1386 void
1387 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1388 {
1389 	struct lock_instance *instance;
1390 	struct lock_class *class;
1391 
1392 	KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
1393 	if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
1394 		return;
1395 	class = LOCK_CLASS(lock);
1396 	file = fixup_filename(file);
1397 	if (witness_watch) {
1398 		if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1399 			panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1400 			    class->lc_name, lock->lo_name, file, line);
1401 		if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1402 			panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1403 			    class->lc_name, lock->lo_name, file, line);
1404 	}
1405 	instance = find_instance(curthread->td_sleeplocks, lock);
1406 	if (instance == NULL)
1407 		panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1408 		    class->lc_name, lock->lo_name, file, line);
1409 	if (witness_watch) {
1410 		if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1411 			panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1412 			    class->lc_name, lock->lo_name, file, line);
1413 		if ((instance->li_flags & LI_RECURSEMASK) != 0)
1414 			panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1415 			    class->lc_name, lock->lo_name,
1416 			    instance->li_flags & LI_RECURSEMASK, file, line);
1417 	}
1418 	instance->li_flags |= LI_EXCLUSIVE;
1419 }
1420 
1421 void
1422 witness_downgrade(struct lock_object *lock, int flags, const char *file,
1423     int line)
1424 {
1425 	struct lock_instance *instance;
1426 	struct lock_class *class;
1427 
1428 	KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
1429 	if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
1430 		return;
1431 	class = LOCK_CLASS(lock);
1432 	file = fixup_filename(file);
1433 	if (witness_watch) {
1434 		if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1435 		panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1436 			    class->lc_name, lock->lo_name, file, line);
1437 		if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1438 			panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1439 			    class->lc_name, lock->lo_name, file, line);
1440 	}
1441 	instance = find_instance(curthread->td_sleeplocks, lock);
1442 	if (instance == NULL)
1443 		panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1444 		    class->lc_name, lock->lo_name, file, line);
1445 	if (witness_watch) {
1446 		if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1447 			panic("downgrade of shared lock (%s) %s @ %s:%d",
1448 			    class->lc_name, lock->lo_name, file, line);
1449 		if ((instance->li_flags & LI_RECURSEMASK) != 0)
1450 			panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1451 			    class->lc_name, lock->lo_name,
1452 			    instance->li_flags & LI_RECURSEMASK, file, line);
1453 	}
1454 	instance->li_flags &= ~LI_EXCLUSIVE;
1455 }
1456 
1457 void
1458 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1459 {
1460 	struct lock_list_entry **lock_list, *lle;
1461 	struct lock_instance *instance;
1462 	struct lock_class *class;
1463 	struct thread *td;
1464 	register_t s;
1465 	int i, j;
1466 
1467 	if (witness_cold || lock->lo_witness == NULL || panicstr != NULL)
1468 		return;
1469 	td = curthread;
1470 	class = LOCK_CLASS(lock);
1471 	file = fixup_filename(file);
1472 
1473 	/* Find lock instance associated with this lock. */
1474 	if (class->lc_flags & LC_SLEEPLOCK)
1475 		lock_list = &td->td_sleeplocks;
1476 	else
1477 		lock_list = PCPU_PTR(spinlocks);
1478 	lle = *lock_list;
1479 	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1480 		for (i = 0; i < (*lock_list)->ll_count; i++) {
1481 			instance = &(*lock_list)->ll_children[i];
1482 			if (instance->li_lock == lock)
1483 				goto found;
1484 		}
1485 
1486 	/*
1487 	 * When disabling WITNESS through witness_watch we could end up in
1488 	 * having registered locks in the td_sleeplocks queue.
1489 	 * We have to make sure we flush these queues, so just search for
1490 	 * eventual register locks and remove them.
1491 	 */
1492 	if (witness_watch > 0)
1493 		panic("lock (%s) %s not locked @ %s:%d", class->lc_name,
1494 		    lock->lo_name, file, line);
1495 	else
1496 		return;
1497 found:
1498 
1499 	/* First, check for shared/exclusive mismatches. */
1500 	if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 &&
1501 	    (flags & LOP_EXCLUSIVE) == 0) {
1502 		printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1503 		    lock->lo_name, file, line);
1504 		printf("while exclusively locked from %s:%d\n",
1505 		    instance->li_file, instance->li_line);
1506 		panic("excl->ushare");
1507 	}
1508 	if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 &&
1509 	    (flags & LOP_EXCLUSIVE) != 0) {
1510 		printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1511 		    lock->lo_name, file, line);
1512 		printf("while share locked from %s:%d\n", instance->li_file,
1513 		    instance->li_line);
1514 		panic("share->uexcl");
1515 	}
1516 	/* If we are recursed, unrecurse. */
1517 	if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1518 		CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1519 		    td->td_proc->p_pid, instance->li_lock->lo_name,
1520 		    instance->li_flags);
1521 		instance->li_flags--;
1522 		return;
1523 	}
1524 	/* The lock is now being dropped, check for NORELEASE flag */
1525 	if ((instance->li_flags & LI_NORELEASE) != 0 && witness_watch > 0) {
1526 		printf("forbidden unlock of (%s) %s @ %s:%d\n", class->lc_name,
1527 		    lock->lo_name, file, line);
1528 		panic("lock marked norelease");
1529 	}
1530 
1531 	/* Otherwise, remove this item from the list. */
1532 	s = intr_disable();
1533 	CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1534 	    td->td_proc->p_pid, instance->li_lock->lo_name,
1535 	    (*lock_list)->ll_count - 1);
1536 	for (j = i; j < (*lock_list)->ll_count - 1; j++)
1537 		(*lock_list)->ll_children[j] =
1538 		    (*lock_list)->ll_children[j + 1];
1539 	(*lock_list)->ll_count--;
1540 	intr_restore(s);
1541 
1542 	/*
1543 	 * In order to reduce contention on w_mtx, we want to keep always an
1544 	 * head object into lists so that frequent allocation from the
1545 	 * free witness pool (and subsequent locking) is avoided.
1546 	 * In order to maintain the current code simple, when the head
1547 	 * object is totally unloaded it means also that we do not have
1548 	 * further objects in the list, so the list ownership needs to be
1549 	 * hand over to another object if the current head needs to be freed.
1550 	 */
1551 	if ((*lock_list)->ll_count == 0) {
1552 		if (*lock_list == lle) {
1553 			if (lle->ll_next == NULL)
1554 				return;
1555 		} else
1556 			lle = *lock_list;
1557 		*lock_list = lle->ll_next;
1558 		CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1559 		    td->td_proc->p_pid, lle);
1560 		witness_lock_list_free(lle);
1561 	}
1562 }
1563 
1564 void
1565 witness_thread_exit(struct thread *td)
1566 {
1567 	struct lock_list_entry *lle;
1568 	int i, n;
1569 
1570 	lle = td->td_sleeplocks;
1571 	if (lle == NULL || panicstr != NULL)
1572 		return;
1573 	if (lle->ll_count != 0) {
1574 		for (n = 0; lle != NULL; lle = lle->ll_next)
1575 			for (i = lle->ll_count - 1; i >= 0; i--) {
1576 				if (n == 0)
1577 		printf("Thread %p exiting with the following locks held:\n",
1578 					    td);
1579 				n++;
1580 				witness_list_lock(&lle->ll_children[i]);
1581 
1582 			}
1583 		panic("Thread %p cannot exit while holding sleeplocks\n", td);
1584 	}
1585 	witness_lock_list_free(lle);
1586 }
1587 
1588 /*
1589  * Warn if any locks other than 'lock' are held.  Flags can be passed in to
1590  * exempt Giant and sleepable locks from the checks as well.  If any
1591  * non-exempt locks are held, then a supplied message is printed to the
1592  * console along with a list of the offending locks.  If indicated in the
1593  * flags then a failure results in a panic as well.
1594  */
1595 int
1596 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1597 {
1598 	struct lock_list_entry *lock_list, *lle;
1599 	struct lock_instance *lock1;
1600 	struct thread *td;
1601 	va_list ap;
1602 	int i, n;
1603 
1604 	if (witness_cold || witness_watch < 1 || panicstr != NULL)
1605 		return (0);
1606 	n = 0;
1607 	td = curthread;
1608 	for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1609 		for (i = lle->ll_count - 1; i >= 0; i--) {
1610 			lock1 = &lle->ll_children[i];
1611 			if (lock1->li_lock == lock)
1612 				continue;
1613 			if (flags & WARN_GIANTOK &&
1614 			    lock1->li_lock == &Giant.lock_object)
1615 				continue;
1616 			if (flags & WARN_SLEEPOK &&
1617 			    (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1618 				continue;
1619 			if (n == 0) {
1620 				va_start(ap, fmt);
1621 				vprintf(fmt, ap);
1622 				va_end(ap);
1623 				printf(" with the following");
1624 				if (flags & WARN_SLEEPOK)
1625 					printf(" non-sleepable");
1626 				printf(" locks held:\n");
1627 			}
1628 			n++;
1629 			witness_list_lock(lock1);
1630 		}
1631 
1632 	/*
1633 	 * Pin the thread in order to avoid problems with thread migration.
1634 	 * Once that all verifies are passed about spinlocks ownership,
1635 	 * the thread is in a safe path and it can be unpinned.
1636 	 */
1637 	sched_pin();
1638 	lock_list = PCPU_GET(spinlocks);
1639 	if (lock_list != NULL && lock_list->ll_count != 0) {
1640 		sched_unpin();
1641 
1642 		/*
1643 		 * We should only have one spinlock and as long as
1644 		 * the flags cannot match for this locks class,
1645 		 * check if the first spinlock is the one curthread
1646 		 * should hold.
1647 		 */
1648 		lock1 = &lock_list->ll_children[lock_list->ll_count - 1];
1649 		if (lock_list->ll_count == 1 && lock_list->ll_next == NULL &&
1650 		    lock1->li_lock == lock && n == 0)
1651 			return (0);
1652 
1653 		va_start(ap, fmt);
1654 		vprintf(fmt, ap);
1655 		va_end(ap);
1656 		printf(" with the following");
1657 		if (flags & WARN_SLEEPOK)
1658 			printf(" non-sleepable");
1659 		printf(" locks held:\n");
1660 		n += witness_list_locks(&lock_list);
1661 	} else
1662 		sched_unpin();
1663 	if (flags & WARN_PANIC && n)
1664 		panic("%s", __func__);
1665 	else
1666 		witness_debugger(n);
1667 	return (n);
1668 }
1669 
1670 const char *
1671 witness_file(struct lock_object *lock)
1672 {
1673 	struct witness *w;
1674 
1675 	if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL)
1676 		return ("?");
1677 	w = lock->lo_witness;
1678 	return (w->w_file);
1679 }
1680 
1681 int
1682 witness_line(struct lock_object *lock)
1683 {
1684 	struct witness *w;
1685 
1686 	if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL)
1687 		return (0);
1688 	w = lock->lo_witness;
1689 	return (w->w_line);
1690 }
1691 
1692 static struct witness *
1693 enroll(const char *description, struct lock_class *lock_class)
1694 {
1695 	struct witness *w;
1696 	struct witness_list *typelist;
1697 
1698 	MPASS(description != NULL);
1699 
1700 	if (witness_watch == -1 || panicstr != NULL)
1701 		return (NULL);
1702 	if ((lock_class->lc_flags & LC_SPINLOCK)) {
1703 		if (witness_skipspin)
1704 			return (NULL);
1705 		else
1706 			typelist = &w_spin;
1707 	} else if ((lock_class->lc_flags & LC_SLEEPLOCK))
1708 		typelist = &w_sleep;
1709 	else
1710 		panic("lock class %s is not sleep or spin",
1711 		    lock_class->lc_name);
1712 
1713 	mtx_lock_spin(&w_mtx);
1714 	w = witness_hash_get(description);
1715 	if (w)
1716 		goto found;
1717 	if ((w = witness_get()) == NULL)
1718 		return (NULL);
1719 	MPASS(strlen(description) < MAX_W_NAME);
1720 	strcpy(w->w_name, description);
1721 	w->w_class = lock_class;
1722 	w->w_refcount = 1;
1723 	STAILQ_INSERT_HEAD(&w_all, w, w_list);
1724 	if (lock_class->lc_flags & LC_SPINLOCK) {
1725 		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1726 		w_spin_cnt++;
1727 	} else if (lock_class->lc_flags & LC_SLEEPLOCK) {
1728 		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1729 		w_sleep_cnt++;
1730 	}
1731 
1732 	/* Insert new witness into the hash */
1733 	witness_hash_put(w);
1734 	witness_increment_graph_generation();
1735 	mtx_unlock_spin(&w_mtx);
1736 	return (w);
1737 found:
1738 	w->w_refcount++;
1739 	mtx_unlock_spin(&w_mtx);
1740 	if (lock_class != w->w_class)
1741 		panic(
1742 			"lock (%s) %s does not match earlier (%s) lock",
1743 			description, lock_class->lc_name,
1744 			w->w_class->lc_name);
1745 	return (w);
1746 }
1747 
1748 static void
1749 depart(struct witness *w)
1750 {
1751 	struct witness_list *list;
1752 
1753 	MPASS(w->w_refcount == 0);
1754 	if (w->w_class->lc_flags & LC_SLEEPLOCK) {
1755 		list = &w_sleep;
1756 		w_sleep_cnt--;
1757 	} else {
1758 		list = &w_spin;
1759 		w_spin_cnt--;
1760 	}
1761 	/*
1762 	 * Set file to NULL as it may point into a loadable module.
1763 	 */
1764 	w->w_file = NULL;
1765 	w->w_line = 0;
1766 	witness_increment_graph_generation();
1767 }
1768 
1769 
1770 static void
1771 adopt(struct witness *parent, struct witness *child)
1772 {
1773 	int pi, ci, i, j;
1774 
1775 	if (witness_cold == 0)
1776 		mtx_assert(&w_mtx, MA_OWNED);
1777 
1778 	/* If the relationship is already known, there's no work to be done. */
1779 	if (isitmychild(parent, child))
1780 		return;
1781 
1782 	/* When the structure of the graph changes, bump up the generation. */
1783 	witness_increment_graph_generation();
1784 
1785 	/*
1786 	 * The hard part ... create the direct relationship, then propagate all
1787 	 * indirect relationships.
1788 	 */
1789 	pi = parent->w_index;
1790 	ci = child->w_index;
1791 	WITNESS_INDEX_ASSERT(pi);
1792 	WITNESS_INDEX_ASSERT(ci);
1793 	MPASS(pi != ci);
1794 	w_rmatrix[pi][ci] |= WITNESS_PARENT;
1795 	w_rmatrix[ci][pi] |= WITNESS_CHILD;
1796 
1797 	/*
1798 	 * If parent was not already an ancestor of child,
1799 	 * then we increment the descendant and ancestor counters.
1800 	 */
1801 	if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) {
1802 		parent->w_num_descendants++;
1803 		child->w_num_ancestors++;
1804 	}
1805 
1806 	/*
1807 	 * Find each ancestor of 'pi'. Note that 'pi' itself is counted as
1808 	 * an ancestor of 'pi' during this loop.
1809 	 */
1810 	for (i = 1; i <= w_max_used_index; i++) {
1811 		if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 &&
1812 		    (i != pi))
1813 			continue;
1814 
1815 		/* Find each descendant of 'i' and mark it as a descendant. */
1816 		for (j = 1; j <= w_max_used_index; j++) {
1817 
1818 			/*
1819 			 * Skip children that are already marked as
1820 			 * descendants of 'i'.
1821 			 */
1822 			if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK)
1823 				continue;
1824 
1825 			/*
1826 			 * We are only interested in descendants of 'ci'. Note
1827 			 * that 'ci' itself is counted as a descendant of 'ci'.
1828 			 */
1829 			if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 &&
1830 			    (j != ci))
1831 				continue;
1832 			w_rmatrix[i][j] |= WITNESS_ANCESTOR;
1833 			w_rmatrix[j][i] |= WITNESS_DESCENDANT;
1834 			w_data[i].w_num_descendants++;
1835 			w_data[j].w_num_ancestors++;
1836 
1837 			/*
1838 			 * Make sure we aren't marking a node as both an
1839 			 * ancestor and descendant. We should have caught
1840 			 * this as a lock order reversal earlier.
1841 			 */
1842 			if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) &&
1843 			    (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) {
1844 				printf("witness rmatrix paradox! [%d][%d]=%d "
1845 				    "both ancestor and descendant\n",
1846 				    i, j, w_rmatrix[i][j]);
1847 				kdb_backtrace();
1848 				printf("Witness disabled.\n");
1849 				witness_watch = -1;
1850 			}
1851 			if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) &&
1852 			    (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) {
1853 				printf("witness rmatrix paradox! [%d][%d]=%d "
1854 				    "both ancestor and descendant\n",
1855 				    j, i, w_rmatrix[j][i]);
1856 				kdb_backtrace();
1857 				printf("Witness disabled.\n");
1858 				witness_watch = -1;
1859 			}
1860 		}
1861 	}
1862 }
1863 
1864 static void
1865 itismychild(struct witness *parent, struct witness *child)
1866 {
1867 
1868 	MPASS(child != NULL && parent != NULL);
1869 	if (witness_cold == 0)
1870 		mtx_assert(&w_mtx, MA_OWNED);
1871 
1872 	if (!witness_lock_type_equal(parent, child)) {
1873 		if (witness_cold == 0)
1874 			mtx_unlock_spin(&w_mtx);
1875 		panic("%s: parent \"%s\" (%s) and child \"%s\" (%s) are not "
1876 		    "the same lock type", __func__, parent->w_name,
1877 		    parent->w_class->lc_name, child->w_name,
1878 		    child->w_class->lc_name);
1879 	}
1880 	adopt(parent, child);
1881 }
1882 
1883 /*
1884  * Generic code for the isitmy*() functions. The rmask parameter is the
1885  * expected relationship of w1 to w2.
1886  */
1887 static int
1888 _isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname)
1889 {
1890 	unsigned char r1, r2;
1891 	int i1, i2;
1892 
1893 	i1 = w1->w_index;
1894 	i2 = w2->w_index;
1895 	WITNESS_INDEX_ASSERT(i1);
1896 	WITNESS_INDEX_ASSERT(i2);
1897 	r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK;
1898 	r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK;
1899 
1900 	/* The flags on one better be the inverse of the flags on the other */
1901 	if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) ||
1902 		(WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) {
1903 		printf("%s: rmatrix mismatch between %s (index %d) and %s "
1904 		    "(index %d): w_rmatrix[%d][%d] == %hhx but "
1905 		    "w_rmatrix[%d][%d] == %hhx\n",
1906 		    fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1,
1907 		    i2, i1, r2);
1908 		kdb_backtrace();
1909 		printf("Witness disabled.\n");
1910 		witness_watch = -1;
1911 	}
1912 	return (r1 & rmask);
1913 }
1914 
1915 /*
1916  * Checks if @child is a direct child of @parent.
1917  */
1918 static int
1919 isitmychild(struct witness *parent, struct witness *child)
1920 {
1921 
1922 	return (_isitmyx(parent, child, WITNESS_PARENT, __func__));
1923 }
1924 
1925 /*
1926  * Checks if @descendant is a direct or inderect descendant of @ancestor.
1927  */
1928 static int
1929 isitmydescendant(struct witness *ancestor, struct witness *descendant)
1930 {
1931 
1932 	return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK,
1933 	    __func__));
1934 }
1935 
1936 #ifdef BLESSING
1937 static int
1938 blessed(struct witness *w1, struct witness *w2)
1939 {
1940 	int i;
1941 	struct witness_blessed *b;
1942 
1943 	for (i = 0; i < blessed_count; i++) {
1944 		b = &blessed_list[i];
1945 		if (strcmp(w1->w_name, b->b_lock1) == 0) {
1946 			if (strcmp(w2->w_name, b->b_lock2) == 0)
1947 				return (1);
1948 			continue;
1949 		}
1950 		if (strcmp(w1->w_name, b->b_lock2) == 0)
1951 			if (strcmp(w2->w_name, b->b_lock1) == 0)
1952 				return (1);
1953 	}
1954 	return (0);
1955 }
1956 #endif
1957 
1958 static struct witness *
1959 witness_get(void)
1960 {
1961 	struct witness *w;
1962 	int index;
1963 
1964 	if (witness_cold == 0)
1965 		mtx_assert(&w_mtx, MA_OWNED);
1966 
1967 	if (witness_watch == -1) {
1968 		mtx_unlock_spin(&w_mtx);
1969 		return (NULL);
1970 	}
1971 	if (STAILQ_EMPTY(&w_free)) {
1972 		witness_watch = -1;
1973 		mtx_unlock_spin(&w_mtx);
1974 		printf("WITNESS: unable to allocate a new witness object\n");
1975 		return (NULL);
1976 	}
1977 	w = STAILQ_FIRST(&w_free);
1978 	STAILQ_REMOVE_HEAD(&w_free, w_list);
1979 	w_free_cnt--;
1980 	index = w->w_index;
1981 	MPASS(index > 0 && index == w_max_used_index+1 &&
1982 	    index < WITNESS_COUNT);
1983 	bzero(w, sizeof(*w));
1984 	w->w_index = index;
1985 	if (index > w_max_used_index)
1986 		w_max_used_index = index;
1987 	return (w);
1988 }
1989 
1990 static void
1991 witness_free(struct witness *w)
1992 {
1993 
1994 	STAILQ_INSERT_HEAD(&w_free, w, w_list);
1995 	w_free_cnt++;
1996 }
1997 
1998 static struct lock_list_entry *
1999 witness_lock_list_get(void)
2000 {
2001 	struct lock_list_entry *lle;
2002 
2003 	if (witness_watch == -1)
2004 		return (NULL);
2005 	mtx_lock_spin(&w_mtx);
2006 	lle = w_lock_list_free;
2007 	if (lle == NULL) {
2008 		witness_watch = -1;
2009 		mtx_unlock_spin(&w_mtx);
2010 		printf("%s: witness exhausted\n", __func__);
2011 		return (NULL);
2012 	}
2013 	w_lock_list_free = lle->ll_next;
2014 	mtx_unlock_spin(&w_mtx);
2015 	bzero(lle, sizeof(*lle));
2016 	return (lle);
2017 }
2018 
2019 static void
2020 witness_lock_list_free(struct lock_list_entry *lle)
2021 {
2022 
2023 	mtx_lock_spin(&w_mtx);
2024 	lle->ll_next = w_lock_list_free;
2025 	w_lock_list_free = lle;
2026 	mtx_unlock_spin(&w_mtx);
2027 }
2028 
2029 static struct lock_instance *
2030 find_instance(struct lock_list_entry *list, struct lock_object *lock)
2031 {
2032 	struct lock_list_entry *lle;
2033 	struct lock_instance *instance;
2034 	int i;
2035 
2036 	for (lle = list; lle != NULL; lle = lle->ll_next)
2037 		for (i = lle->ll_count - 1; i >= 0; i--) {
2038 			instance = &lle->ll_children[i];
2039 			if (instance->li_lock == lock)
2040 				return (instance);
2041 		}
2042 	return (NULL);
2043 }
2044 
2045 static void
2046 witness_list_lock(struct lock_instance *instance)
2047 {
2048 	struct lock_object *lock;
2049 
2050 	lock = instance->li_lock;
2051 	printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
2052 	    "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name);
2053 	if (lock->lo_witness->w_name != lock->lo_name)
2054 		printf(" (%s)", lock->lo_witness->w_name);
2055 	printf(" r = %d (%p) locked @ %s:%d\n",
2056 	    instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
2057 	    instance->li_line);
2058 }
2059 
2060 #ifdef DDB
2061 static int
2062 witness_thread_has_locks(struct thread *td)
2063 {
2064 
2065 	if (td->td_sleeplocks == NULL)
2066 		return (0);
2067 	return (td->td_sleeplocks->ll_count != 0);
2068 }
2069 
2070 static int
2071 witness_proc_has_locks(struct proc *p)
2072 {
2073 	struct thread *td;
2074 
2075 	FOREACH_THREAD_IN_PROC(p, td) {
2076 		if (witness_thread_has_locks(td))
2077 			return (1);
2078 	}
2079 	return (0);
2080 }
2081 #endif
2082 
2083 int
2084 witness_list_locks(struct lock_list_entry **lock_list)
2085 {
2086 	struct lock_list_entry *lle;
2087 	int i, nheld;
2088 
2089 	nheld = 0;
2090 	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
2091 		for (i = lle->ll_count - 1; i >= 0; i--) {
2092 			witness_list_lock(&lle->ll_children[i]);
2093 			nheld++;
2094 		}
2095 	return (nheld);
2096 }
2097 
2098 /*
2099  * This is a bit risky at best.  We call this function when we have timed
2100  * out acquiring a spin lock, and we assume that the other CPU is stuck
2101  * with this lock held.  So, we go groveling around in the other CPU's
2102  * per-cpu data to try to find the lock instance for this spin lock to
2103  * see when it was last acquired.
2104  */
2105 void
2106 witness_display_spinlock(struct lock_object *lock, struct thread *owner)
2107 {
2108 	struct lock_instance *instance;
2109 	struct pcpu *pc;
2110 
2111 	if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
2112 		return;
2113 	pc = pcpu_find(owner->td_oncpu);
2114 	instance = find_instance(pc->pc_spinlocks, lock);
2115 	if (instance != NULL)
2116 		witness_list_lock(instance);
2117 }
2118 
2119 void
2120 witness_save(struct lock_object *lock, const char **filep, int *linep)
2121 {
2122 	struct lock_list_entry *lock_list;
2123 	struct lock_instance *instance;
2124 	struct lock_class *class;
2125 
2126 	KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2127 	if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2128 		return;
2129 	class = LOCK_CLASS(lock);
2130 	if (class->lc_flags & LC_SLEEPLOCK)
2131 		lock_list = curthread->td_sleeplocks;
2132 	else {
2133 		if (witness_skipspin)
2134 			return;
2135 		lock_list = PCPU_GET(spinlocks);
2136 	}
2137 	instance = find_instance(lock_list, lock);
2138 	if (instance == NULL)
2139 		panic("%s: lock (%s) %s not locked", __func__,
2140 		    class->lc_name, lock->lo_name);
2141 	*filep = instance->li_file;
2142 	*linep = instance->li_line;
2143 }
2144 
2145 void
2146 witness_restore(struct lock_object *lock, const char *file, int line)
2147 {
2148 	struct lock_list_entry *lock_list;
2149 	struct lock_instance *instance;
2150 	struct lock_class *class;
2151 
2152 	KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2153 	if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2154 		return;
2155 	class = LOCK_CLASS(lock);
2156 	if (class->lc_flags & LC_SLEEPLOCK)
2157 		lock_list = curthread->td_sleeplocks;
2158 	else {
2159 		if (witness_skipspin)
2160 			return;
2161 		lock_list = PCPU_GET(spinlocks);
2162 	}
2163 	instance = find_instance(lock_list, lock);
2164 	if (instance == NULL)
2165 		panic("%s: lock (%s) %s not locked", __func__,
2166 		    class->lc_name, lock->lo_name);
2167 	lock->lo_witness->w_file = file;
2168 	lock->lo_witness->w_line = line;
2169 	instance->li_file = file;
2170 	instance->li_line = line;
2171 }
2172 
2173 void
2174 witness_assert(struct lock_object *lock, int flags, const char *file, int line)
2175 {
2176 #ifdef INVARIANT_SUPPORT
2177 	struct lock_instance *instance;
2178 	struct lock_class *class;
2179 
2180 	if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL)
2181 		return;
2182 	class = LOCK_CLASS(lock);
2183 	if ((class->lc_flags & LC_SLEEPLOCK) != 0)
2184 		instance = find_instance(curthread->td_sleeplocks, lock);
2185 	else if ((class->lc_flags & LC_SPINLOCK) != 0)
2186 		instance = find_instance(PCPU_GET(spinlocks), lock);
2187 	else {
2188 		panic("Lock (%s) %s is not sleep or spin!",
2189 		    class->lc_name, lock->lo_name);
2190 	}
2191 	file = fixup_filename(file);
2192 	switch (flags) {
2193 	case LA_UNLOCKED:
2194 		if (instance != NULL)
2195 			panic("Lock (%s) %s locked @ %s:%d.",
2196 			    class->lc_name, lock->lo_name, file, line);
2197 		break;
2198 	case LA_LOCKED:
2199 	case LA_LOCKED | LA_RECURSED:
2200 	case LA_LOCKED | LA_NOTRECURSED:
2201 	case LA_SLOCKED:
2202 	case LA_SLOCKED | LA_RECURSED:
2203 	case LA_SLOCKED | LA_NOTRECURSED:
2204 	case LA_XLOCKED:
2205 	case LA_XLOCKED | LA_RECURSED:
2206 	case LA_XLOCKED | LA_NOTRECURSED:
2207 		if (instance == NULL) {
2208 			panic("Lock (%s) %s not locked @ %s:%d.",
2209 			    class->lc_name, lock->lo_name, file, line);
2210 			break;
2211 		}
2212 		if ((flags & LA_XLOCKED) != 0 &&
2213 		    (instance->li_flags & LI_EXCLUSIVE) == 0)
2214 			panic("Lock (%s) %s not exclusively locked @ %s:%d.",
2215 			    class->lc_name, lock->lo_name, file, line);
2216 		if ((flags & LA_SLOCKED) != 0 &&
2217 		    (instance->li_flags & LI_EXCLUSIVE) != 0)
2218 			panic("Lock (%s) %s exclusively locked @ %s:%d.",
2219 			    class->lc_name, lock->lo_name, file, line);
2220 		if ((flags & LA_RECURSED) != 0 &&
2221 		    (instance->li_flags & LI_RECURSEMASK) == 0)
2222 			panic("Lock (%s) %s not recursed @ %s:%d.",
2223 			    class->lc_name, lock->lo_name, file, line);
2224 		if ((flags & LA_NOTRECURSED) != 0 &&
2225 		    (instance->li_flags & LI_RECURSEMASK) != 0)
2226 			panic("Lock (%s) %s recursed @ %s:%d.",
2227 			    class->lc_name, lock->lo_name, file, line);
2228 		break;
2229 	default:
2230 		panic("Invalid lock assertion at %s:%d.", file, line);
2231 
2232 	}
2233 #endif	/* INVARIANT_SUPPORT */
2234 }
2235 
2236 static void
2237 witness_setflag(struct lock_object *lock, int flag, int set)
2238 {
2239 	struct lock_list_entry *lock_list;
2240 	struct lock_instance *instance;
2241 	struct lock_class *class;
2242 
2243 	if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2244 		return;
2245 	class = LOCK_CLASS(lock);
2246 	if (class->lc_flags & LC_SLEEPLOCK)
2247 		lock_list = curthread->td_sleeplocks;
2248 	else {
2249 		if (witness_skipspin)
2250 			return;
2251 		lock_list = PCPU_GET(spinlocks);
2252 	}
2253 	instance = find_instance(lock_list, lock);
2254 	if (instance == NULL)
2255 		panic("%s: lock (%s) %s not locked", __func__,
2256 		    class->lc_name, lock->lo_name);
2257 
2258 	if (set)
2259 		instance->li_flags |= flag;
2260 	else
2261 		instance->li_flags &= ~flag;
2262 }
2263 
2264 void
2265 witness_norelease(struct lock_object *lock)
2266 {
2267 
2268 	witness_setflag(lock, LI_NORELEASE, 1);
2269 }
2270 
2271 void
2272 witness_releaseok(struct lock_object *lock)
2273 {
2274 
2275 	witness_setflag(lock, LI_NORELEASE, 0);
2276 }
2277 
2278 #ifdef DDB
2279 static void
2280 witness_ddb_list(struct thread *td)
2281 {
2282 
2283 	KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2284 	KASSERT(kdb_active, ("%s: not in the debugger", __func__));
2285 
2286 	if (witness_watch < 1)
2287 		return;
2288 
2289 	witness_list_locks(&td->td_sleeplocks);
2290 
2291 	/*
2292 	 * We only handle spinlocks if td == curthread.  This is somewhat broken
2293 	 * if td is currently executing on some other CPU and holds spin locks
2294 	 * as we won't display those locks.  If we had a MI way of getting
2295 	 * the per-cpu data for a given cpu then we could use
2296 	 * td->td_oncpu to get the list of spinlocks for this thread
2297 	 * and "fix" this.
2298 	 *
2299 	 * That still wouldn't really fix this unless we locked the scheduler
2300 	 * lock or stopped the other CPU to make sure it wasn't changing the
2301 	 * list out from under us.  It is probably best to just not try to
2302 	 * handle threads on other CPU's for now.
2303 	 */
2304 	if (td == curthread && PCPU_GET(spinlocks) != NULL)
2305 		witness_list_locks(PCPU_PTR(spinlocks));
2306 }
2307 
2308 DB_SHOW_COMMAND(locks, db_witness_list)
2309 {
2310 	struct thread *td;
2311 
2312 	if (have_addr)
2313 		td = db_lookup_thread(addr, TRUE);
2314 	else
2315 		td = kdb_thread;
2316 	witness_ddb_list(td);
2317 }
2318 
2319 DB_SHOW_ALL_COMMAND(locks, db_witness_list_all)
2320 {
2321 	struct thread *td;
2322 	struct proc *p;
2323 
2324 	/*
2325 	 * It would be nice to list only threads and processes that actually
2326 	 * held sleep locks, but that information is currently not exported
2327 	 * by WITNESS.
2328 	 */
2329 	FOREACH_PROC_IN_SYSTEM(p) {
2330 		if (!witness_proc_has_locks(p))
2331 			continue;
2332 		FOREACH_THREAD_IN_PROC(p, td) {
2333 			if (!witness_thread_has_locks(td))
2334 				continue;
2335 			db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
2336 			    p->p_comm, td, td->td_tid);
2337 			witness_ddb_list(td);
2338 		}
2339 	}
2340 }
2341 DB_SHOW_ALIAS(alllocks, db_witness_list_all)
2342 
2343 DB_SHOW_COMMAND(witness, db_witness_display)
2344 {
2345 
2346 	witness_ddb_display(db_printf);
2347 }
2348 #endif
2349 
2350 static int
2351 sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS)
2352 {
2353 	struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2;
2354 	struct witness *tmp_w1, *tmp_w2, *w1, *w2;
2355 	struct sbuf *sb;
2356 	u_int w_rmatrix1, w_rmatrix2;
2357 	int error, generation, i, j;
2358 
2359 	tmp_data1 = NULL;
2360 	tmp_data2 = NULL;
2361 	tmp_w1 = NULL;
2362 	tmp_w2 = NULL;
2363 	if (witness_watch < 1) {
2364 		error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning));
2365 		return (error);
2366 	}
2367 	if (witness_cold) {
2368 		error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold));
2369 		return (error);
2370 	}
2371 	error = 0;
2372 	sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND);
2373 	if (sb == NULL)
2374 		return (ENOMEM);
2375 
2376 	/* Allocate and init temporary storage space. */
2377 	tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO);
2378 	tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO);
2379 	tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP,
2380 	    M_WAITOK | M_ZERO);
2381 	tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP,
2382 	    M_WAITOK | M_ZERO);
2383 	stack_zero(&tmp_data1->wlod_stack);
2384 	stack_zero(&tmp_data2->wlod_stack);
2385 
2386 restart:
2387 	mtx_lock_spin(&w_mtx);
2388 	generation = w_generation;
2389 	mtx_unlock_spin(&w_mtx);
2390 	sbuf_printf(sb, "Number of known direct relationships is %d\n",
2391 	    w_lohash.wloh_count);
2392 	for (i = 1; i < w_max_used_index; i++) {
2393 		mtx_lock_spin(&w_mtx);
2394 		if (generation != w_generation) {
2395 			mtx_unlock_spin(&w_mtx);
2396 
2397 			/* The graph has changed, try again. */
2398 			req->oldidx = 0;
2399 			sbuf_clear(sb);
2400 			goto restart;
2401 		}
2402 
2403 		w1 = &w_data[i];
2404 		if (w1->w_reversed == 0) {
2405 			mtx_unlock_spin(&w_mtx);
2406 			continue;
2407 		}
2408 
2409 		/* Copy w1 locally so we can release the spin lock. */
2410 		*tmp_w1 = *w1;
2411 		mtx_unlock_spin(&w_mtx);
2412 
2413 		if (tmp_w1->w_reversed == 0)
2414 			continue;
2415 		for (j = 1; j < w_max_used_index; j++) {
2416 			if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j)
2417 				continue;
2418 
2419 			mtx_lock_spin(&w_mtx);
2420 			if (generation != w_generation) {
2421 				mtx_unlock_spin(&w_mtx);
2422 
2423 				/* The graph has changed, try again. */
2424 				req->oldidx = 0;
2425 				sbuf_clear(sb);
2426 				goto restart;
2427 			}
2428 
2429 			w2 = &w_data[j];
2430 			data1 = witness_lock_order_get(w1, w2);
2431 			data2 = witness_lock_order_get(w2, w1);
2432 
2433 			/*
2434 			 * Copy information locally so we can release the
2435 			 * spin lock.
2436 			 */
2437 			*tmp_w2 = *w2;
2438 			w_rmatrix1 = (unsigned int)w_rmatrix[i][j];
2439 			w_rmatrix2 = (unsigned int)w_rmatrix[j][i];
2440 
2441 			if (data1) {
2442 				stack_zero(&tmp_data1->wlod_stack);
2443 				stack_copy(&data1->wlod_stack,
2444 				    &tmp_data1->wlod_stack);
2445 			}
2446 			if (data2 && data2 != data1) {
2447 				stack_zero(&tmp_data2->wlod_stack);
2448 				stack_copy(&data2->wlod_stack,
2449 				    &tmp_data2->wlod_stack);
2450 			}
2451 			mtx_unlock_spin(&w_mtx);
2452 
2453 			sbuf_printf(sb,
2454 	    "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n",
2455 			    tmp_w1->w_name, tmp_w1->w_class->lc_name,
2456 			    tmp_w2->w_name, tmp_w2->w_class->lc_name);
2457 #if 0
2458  			sbuf_printf(sb,
2459 			"w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n",
2460  			    tmp_w1->name, tmp_w2->w_name, w_rmatrix1,
2461  			    tmp_w2->name, tmp_w1->w_name, w_rmatrix2);
2462 #endif
2463 			if (data1) {
2464 				sbuf_printf(sb,
2465 			"Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n",
2466 				    tmp_w1->w_name, tmp_w1->w_class->lc_name,
2467 				    tmp_w2->w_name, tmp_w2->w_class->lc_name);
2468 				stack_sbuf_print(sb, &tmp_data1->wlod_stack);
2469 				sbuf_printf(sb, "\n");
2470 			}
2471 			if (data2 && data2 != data1) {
2472 				sbuf_printf(sb,
2473 			"Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n",
2474 				    tmp_w2->w_name, tmp_w2->w_class->lc_name,
2475 				    tmp_w1->w_name, tmp_w1->w_class->lc_name);
2476 				stack_sbuf_print(sb, &tmp_data2->wlod_stack);
2477 				sbuf_printf(sb, "\n");
2478 			}
2479 		}
2480 	}
2481 	mtx_lock_spin(&w_mtx);
2482 	if (generation != w_generation) {
2483 		mtx_unlock_spin(&w_mtx);
2484 
2485 		/*
2486 		 * The graph changed while we were printing stack data,
2487 		 * try again.
2488 		 */
2489 		req->oldidx = 0;
2490 		sbuf_clear(sb);
2491 		goto restart;
2492 	}
2493 	mtx_unlock_spin(&w_mtx);
2494 
2495 	/* Free temporary storage space. */
2496 	free(tmp_data1, M_TEMP);
2497 	free(tmp_data2, M_TEMP);
2498 	free(tmp_w1, M_TEMP);
2499 	free(tmp_w2, M_TEMP);
2500 
2501 	sbuf_finish(sb);
2502 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2503 	sbuf_delete(sb);
2504 
2505 	return (error);
2506 }
2507 
2508 static int
2509 sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS)
2510 {
2511 	struct witness *w;
2512 	struct sbuf *sb;
2513 	int error;
2514 
2515 	if (witness_watch < 1) {
2516 		error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning));
2517 		return (error);
2518 	}
2519 	if (witness_cold) {
2520 		error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold));
2521 		return (error);
2522 	}
2523 	error = 0;
2524 	sb = sbuf_new(NULL, NULL, FULLGRAPH_SBUF_SIZE, SBUF_FIXEDLEN);
2525 	if (sb == NULL)
2526 		return (ENOMEM);
2527 	sbuf_printf(sb, "\n");
2528 
2529 	mtx_lock_spin(&w_mtx);
2530 	STAILQ_FOREACH(w, &w_all, w_list)
2531 		w->w_displayed = 0;
2532 	STAILQ_FOREACH(w, &w_all, w_list)
2533 		witness_add_fullgraph(sb, w);
2534 	mtx_unlock_spin(&w_mtx);
2535 
2536 	/*
2537 	 * While using SBUF_FIXEDLEN, check if the sbuf overflowed.
2538 	 */
2539 	if (sbuf_overflowed(sb)) {
2540 		sbuf_delete(sb);
2541 		panic("%s: sbuf overflowed, bump FULLGRAPH_SBUF_SIZE value\n",
2542 		    __func__);
2543 	}
2544 
2545 	/*
2546 	 * Close the sbuf and return to userland.
2547 	 */
2548 	sbuf_finish(sb);
2549 	error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2550 	sbuf_delete(sb);
2551 
2552 	return (error);
2553 }
2554 
2555 static int
2556 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
2557 {
2558 	int error, value;
2559 
2560 	value = witness_watch;
2561 	error = sysctl_handle_int(oidp, &value, 0, req);
2562 	if (error != 0 || req->newptr == NULL)
2563 		return (error);
2564 	if (value > 1 || value < -1 ||
2565 	    (witness_watch == -1 && value != witness_watch))
2566 		return (EINVAL);
2567 	witness_watch = value;
2568 	return (0);
2569 }
2570 
2571 static void
2572 witness_add_fullgraph(struct sbuf *sb, struct witness *w)
2573 {
2574 	int i;
2575 
2576 	if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0))
2577 		return;
2578 	w->w_displayed = 1;
2579 
2580 	WITNESS_INDEX_ASSERT(w->w_index);
2581 	for (i = 1; i <= w_max_used_index; i++) {
2582 		if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) {
2583 			sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name,
2584 			    w_data[i].w_name);
2585 			witness_add_fullgraph(sb, &w_data[i]);
2586 		}
2587 	}
2588 }
2589 
2590 /*
2591  * A simple hash function. Takes a key pointer and a key size. If size == 0,
2592  * interprets the key as a string and reads until the null
2593  * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit
2594  * hash value computed from the key.
2595  */
2596 static uint32_t
2597 witness_hash_djb2(const uint8_t *key, uint32_t size)
2598 {
2599 	unsigned int hash = 5381;
2600 	int i;
2601 
2602 	/* hash = hash * 33 + key[i] */
2603 	if (size)
2604 		for (i = 0; i < size; i++)
2605 			hash = ((hash << 5) + hash) + (unsigned int)key[i];
2606 	else
2607 		for (i = 0; key[i] != 0; i++)
2608 			hash = ((hash << 5) + hash) + (unsigned int)key[i];
2609 
2610 	return (hash);
2611 }
2612 
2613 
2614 /*
2615  * Initializes the two witness hash tables. Called exactly once from
2616  * witness_initialize().
2617  */
2618 static void
2619 witness_init_hash_tables(void)
2620 {
2621 	int i;
2622 
2623 	MPASS(witness_cold);
2624 
2625 	/* Initialize the hash tables. */
2626 	for (i = 0; i < WITNESS_HASH_SIZE; i++)
2627 		w_hash.wh_array[i] = NULL;
2628 
2629 	w_hash.wh_size = WITNESS_HASH_SIZE;
2630 	w_hash.wh_count = 0;
2631 
2632 	/* Initialize the lock order data hash. */
2633 	w_lofree = NULL;
2634 	for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) {
2635 		memset(&w_lodata[i], 0, sizeof(w_lodata[i]));
2636 		w_lodata[i].wlod_next = w_lofree;
2637 		w_lofree = &w_lodata[i];
2638 	}
2639 	w_lohash.wloh_size = WITNESS_LO_HASH_SIZE;
2640 	w_lohash.wloh_count = 0;
2641 	for (i = 0; i < WITNESS_LO_HASH_SIZE; i++)
2642 		w_lohash.wloh_array[i] = NULL;
2643 }
2644 
2645 static struct witness *
2646 witness_hash_get(const char *key)
2647 {
2648 	struct witness *w;
2649 	uint32_t hash;
2650 
2651 	MPASS(key != NULL);
2652 	if (witness_cold == 0)
2653 		mtx_assert(&w_mtx, MA_OWNED);
2654 	hash = witness_hash_djb2(key, 0) % w_hash.wh_size;
2655 	w = w_hash.wh_array[hash];
2656 	while (w != NULL) {
2657 		if (strcmp(w->w_name, key) == 0)
2658 			goto out;
2659 		w = w->w_hash_next;
2660 	}
2661 
2662 out:
2663 	return (w);
2664 }
2665 
2666 static void
2667 witness_hash_put(struct witness *w)
2668 {
2669 	uint32_t hash;
2670 
2671 	MPASS(w != NULL);
2672 	MPASS(w->w_name != NULL);
2673 	if (witness_cold == 0)
2674 		mtx_assert(&w_mtx, MA_OWNED);
2675 	KASSERT(witness_hash_get(w->w_name) == NULL,
2676 	    ("%s: trying to add a hash entry that already exists!", __func__));
2677 	KASSERT(w->w_hash_next == NULL,
2678 	    ("%s: w->w_hash_next != NULL", __func__));
2679 
2680 	hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size;
2681 	w->w_hash_next = w_hash.wh_array[hash];
2682 	w_hash.wh_array[hash] = w;
2683 	w_hash.wh_count++;
2684 }
2685 
2686 
2687 static struct witness_lock_order_data *
2688 witness_lock_order_get(struct witness *parent, struct witness *child)
2689 {
2690 	struct witness_lock_order_data *data = NULL;
2691 	struct witness_lock_order_key key;
2692 	unsigned int hash;
2693 
2694 	MPASS(parent != NULL && child != NULL);
2695 	key.from = parent->w_index;
2696 	key.to = child->w_index;
2697 	WITNESS_INDEX_ASSERT(key.from);
2698 	WITNESS_INDEX_ASSERT(key.to);
2699 	if ((w_rmatrix[parent->w_index][child->w_index]
2700 	    & WITNESS_LOCK_ORDER_KNOWN) == 0)
2701 		goto out;
2702 
2703 	hash = witness_hash_djb2((const char*)&key,
2704 	    sizeof(key)) % w_lohash.wloh_size;
2705 	data = w_lohash.wloh_array[hash];
2706 	while (data != NULL) {
2707 		if (witness_lock_order_key_equal(&data->wlod_key, &key))
2708 			break;
2709 		data = data->wlod_next;
2710 	}
2711 
2712 out:
2713 	return (data);
2714 }
2715 
2716 /*
2717  * Verify that parent and child have a known relationship, are not the same,
2718  * and child is actually a child of parent.  This is done without w_mtx
2719  * to avoid contention in the common case.
2720  */
2721 static int
2722 witness_lock_order_check(struct witness *parent, struct witness *child)
2723 {
2724 
2725 	if (parent != child &&
2726 	    w_rmatrix[parent->w_index][child->w_index]
2727 	    & WITNESS_LOCK_ORDER_KNOWN &&
2728 	    isitmychild(parent, child))
2729 		return (1);
2730 
2731 	return (0);
2732 }
2733 
2734 static int
2735 witness_lock_order_add(struct witness *parent, struct witness *child)
2736 {
2737 	struct witness_lock_order_data *data = NULL;
2738 	struct witness_lock_order_key key;
2739 	unsigned int hash;
2740 
2741 	MPASS(parent != NULL && child != NULL);
2742 	key.from = parent->w_index;
2743 	key.to = child->w_index;
2744 	WITNESS_INDEX_ASSERT(key.from);
2745 	WITNESS_INDEX_ASSERT(key.to);
2746 	if (w_rmatrix[parent->w_index][child->w_index]
2747 	    & WITNESS_LOCK_ORDER_KNOWN)
2748 		return (1);
2749 
2750 	hash = witness_hash_djb2((const char*)&key,
2751 	    sizeof(key)) % w_lohash.wloh_size;
2752 	w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN;
2753 	data = w_lofree;
2754 	if (data == NULL)
2755 		return (0);
2756 	w_lofree = data->wlod_next;
2757 	data->wlod_next = w_lohash.wloh_array[hash];
2758 	data->wlod_key = key;
2759 	w_lohash.wloh_array[hash] = data;
2760 	w_lohash.wloh_count++;
2761 	stack_zero(&data->wlod_stack);
2762 	stack_save(&data->wlod_stack);
2763 	return (1);
2764 }
2765 
2766 /* Call this whenver the structure of the witness graph changes. */
2767 static void
2768 witness_increment_graph_generation(void)
2769 {
2770 
2771 	if (witness_cold == 0)
2772 		mtx_assert(&w_mtx, MA_OWNED);
2773 	w_generation++;
2774 }
2775 
2776 #ifdef KDB
2777 static void
2778 _witness_debugger(int cond, const char *msg)
2779 {
2780 
2781 	if (witness_trace && cond)
2782 		kdb_backtrace();
2783 	if (witness_kdb && cond)
2784 		kdb_enter(KDB_WHY_WITNESS, msg);
2785 }
2786 #endif
2787