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