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