xref: /freebsd/sys/kern/subr_witness.c (revision 5773cccf19ef7b97e56c1101aa481c43149224da)
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  * $FreeBSD$
31  */
32 
33 /*
34  * Implementation of the `witness' lock verifier.  Originally implemented for
35  * mutexes in BSD/OS.  Extended to handle generic lock objects and lock
36  * classes in FreeBSD.
37  */
38 
39 /*
40  *	Main Entry: witness
41  *	Pronunciation: 'wit-n&s
42  *	Function: noun
43  *	Etymology: Middle English witnesse, from Old English witnes knowledge,
44  *	    testimony, witness, from 2wit
45  *	Date: before 12th century
46  *	1 : attestation of a fact or event : TESTIMONY
47  *	2 : one that gives evidence; specifically : one who testifies in
48  *	    a cause or before a judicial tribunal
49  *	3 : one asked to be present at a transaction so as to be able to
50  *	    testify to its having taken place
51  *	4 : one who has personal knowledge of something
52  *	5 a : something serving as evidence or proof : SIGN
53  *	  b : public affirmation by word or example of usually
54  *	      religious faith or conviction <the heroic witness to divine
55  *	      life -- Pilot>
56  *	6 capitalized : a member of the Jehovah's Witnesses
57  */
58 
59 #include "opt_ddb.h"
60 #include "opt_witness.h"
61 
62 #include <sys/param.h>
63 #include <sys/bus.h>
64 #include <sys/kernel.h>
65 #include <sys/ktr.h>
66 #include <sys/lock.h>
67 #include <sys/malloc.h>
68 #include <sys/mutex.h>
69 #include <sys/proc.h>
70 #include <sys/sysctl.h>
71 #include <sys/systm.h>
72 
73 #include <ddb/ddb.h>
74 
75 /* Define this to check for blessed mutexes */
76 #undef BLESSING
77 
78 #define WITNESS_COUNT 200
79 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
80 /*
81  * XXX: This is somewhat bogus, as we assume here that at most 1024 threads
82  * will hold LOCK_NCHILDREN * 2 locks.  We handle failure ok, and we should
83  * probably be safe for the most part, but it's still a SWAG.
84  */
85 #define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
86 
87 #define	WITNESS_NCHILDREN 6
88 
89 struct witness_child_list_entry;
90 
91 struct witness {
92 	const	char *w_name;
93 	struct	lock_class *w_class;
94 	STAILQ_ENTRY(witness) w_list;		/* List of all witnesses. */
95 	STAILQ_ENTRY(witness) w_typelist;	/* Witnesses of a type. */
96 	struct	witness_child_list_entry *w_children;	/* Great evilness... */
97 	const	char *w_file;
98 	int	w_line;
99 	u_int	w_level;
100 	u_int	w_refcount;
101 	u_char	w_Giant_squawked:1;
102 	u_char	w_other_squawked:1;
103 	u_char	w_same_squawked:1;
104 };
105 
106 struct witness_child_list_entry {
107 	struct	witness_child_list_entry *wcl_next;
108 	struct	witness *wcl_children[WITNESS_NCHILDREN];
109 	u_int	wcl_count;
110 };
111 
112 STAILQ_HEAD(witness_list, witness);
113 
114 #ifdef BLESSING
115 struct witness_blessed {
116 	const	char *b_lock1;
117 	const	char *b_lock2;
118 };
119 #endif
120 
121 struct witness_order_list_entry {
122 	const	char *w_name;
123 	struct	lock_class *w_class;
124 };
125 
126 static struct	witness *enroll(const char *description,
127 				struct lock_class *lock_class);
128 static int	itismychild(struct witness *parent, struct witness *child);
129 static void	removechild(struct witness *parent, struct witness *child);
130 static int	isitmychild(struct witness *parent, struct witness *child);
131 static int	isitmydescendant(struct witness *parent, struct witness *child);
132 #ifdef BLESSING
133 static int	blessed(struct witness *, struct witness *);
134 #endif
135 static void	witness_displaydescendants(void(*)(const char *fmt, ...),
136 					   struct witness *);
137 static void	witness_leveldescendents(struct witness *parent, int level);
138 static void	witness_levelall(void);
139 static struct	witness *witness_get(void);
140 static void	witness_free(struct witness *m);
141 static struct	witness_child_list_entry *witness_child_get(void);
142 static void	witness_child_free(struct witness_child_list_entry *wcl);
143 static struct	lock_list_entry *witness_lock_list_get(void);
144 static void	witness_lock_list_free(struct lock_list_entry *lle);
145 static struct	lock_instance *find_instance(struct lock_list_entry *lock_list,
146 					     struct lock_object *lock);
147 #if defined(DDB)
148 static void	witness_display_list(void(*prnt)(const char *fmt, ...),
149 				     struct witness_list *list);
150 static void	witness_display(void(*)(const char *fmt, ...));
151 #endif
152 
153 MALLOC_DEFINE(M_WITNESS, "witness", "witness structure");
154 
155 static int witness_watch = 1;
156 TUNABLE_INT("debug.witness_watch", &witness_watch);
157 SYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, "");
158 
159 #ifdef DDB
160 /*
161  * When DDB is enabled and witness_ddb is set to 1, it will cause the system to
162  * drop into kdebug() when:
163  *	- a lock heirarchy violation occurs
164  *	- locks are held when going to sleep.
165  */
166 #ifdef WITNESS_DDB
167 int	witness_ddb = 1;
168 #else
169 int	witness_ddb = 0;
170 #endif
171 TUNABLE_INT("debug.witness_ddb", &witness_ddb);
172 SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, "");
173 #endif /* DDB */
174 
175 #ifdef WITNESS_SKIPSPIN
176 int	witness_skipspin = 1;
177 #else
178 int	witness_skipspin = 0;
179 #endif
180 TUNABLE_INT("debug.witness_skipspin", &witness_skipspin);
181 SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0,
182     "");
183 
184 static struct mtx w_mtx;
185 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
186 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
187 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
188 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
189 static struct witness_child_list_entry *w_child_free = NULL;
190 static struct lock_list_entry *w_lock_list_free = NULL;
191 static int witness_dead;	/* fatal error, probably no memory */
192 
193 static struct witness w_data[WITNESS_COUNT];
194 static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
195 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
196 
197 static struct witness_order_list_entry order_lists[] = {
198 	{ "Giant", &lock_class_mtx_sleep },
199 	{ "proctree", &lock_class_sx },
200 	{ "allproc", &lock_class_sx },
201 	{ "sigio lock", &lock_class_mtx_sleep },
202 	{ "process group", &lock_class_mtx_sleep },
203 	{ "process lock", &lock_class_mtx_sleep },
204 	{ "session", &lock_class_mtx_sleep },
205 	{ "uidinfo hash", &lock_class_mtx_sleep },
206 	{ "uidinfo struct", &lock_class_mtx_sleep },
207 	{ NULL, NULL },
208 	/*
209 	 * spin locks
210 	 */
211 #ifdef SMP
212 	{ "ap boot", &lock_class_mtx_spin },
213 #ifdef __i386__
214 	{ "com", &lock_class_mtx_spin },
215 #endif
216 #endif
217 	{ "sio", &lock_class_mtx_spin },
218 #ifdef __i386__
219 	{ "cy", &lock_class_mtx_spin },
220 #endif
221 	{ "sabtty", &lock_class_mtx_spin },
222 	{ "ng_node", &lock_class_mtx_spin },
223 	{ "ng_worklist", &lock_class_mtx_spin },
224 	{ "ithread table lock", &lock_class_mtx_spin },
225 	{ "sched lock", &lock_class_mtx_spin },
226 	{ "callout", &lock_class_mtx_spin },
227 	/*
228 	 * leaf locks
229 	 */
230 	{ "allpmaps", &lock_class_mtx_spin },
231 	{ "vm page queue free mutex", &lock_class_mtx_spin },
232 	{ "icu", &lock_class_mtx_spin },
233 #ifdef SMP
234 	{ "smp rendezvous", &lock_class_mtx_spin },
235 #if defined(__i386__) && defined(APIC_IO)
236 	{ "tlb", &lock_class_mtx_spin },
237 #endif
238 #endif
239 	{ "clk", &lock_class_mtx_spin },
240 	{ "mutex profiling lock", &lock_class_mtx_spin },
241 	{ "zombie_thread_lock", &lock_class_mtx_spin },
242 	{ "ALD Queue", &lock_class_mtx_spin },
243 #ifdef __ia64__
244 	{ "MCA spin lock", &lock_class_mtx_spin },
245 #endif
246 	{ NULL, NULL },
247 	{ NULL, NULL }
248 };
249 
250 #ifdef BLESSING
251 /*
252  * Pairs of locks which have been blessed
253  * Don't complain about order problems with blessed locks
254  */
255 static struct witness_blessed blessed_list[] = {
256 };
257 static int blessed_count =
258 	sizeof(blessed_list) / sizeof(struct witness_blessed);
259 #endif
260 
261 /*
262  * List of all locks in the system.
263  */
264 TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks);
265 
266 static struct mtx all_mtx = {
267 	{ &lock_class_mtx_sleep,	/* mtx_object.lo_class */
268 	  "All locks list",		/* mtx_object.lo_name */
269 	  "All locks list",		/* mtx_object.lo_type */
270 	  LO_INITIALIZED,		/* mtx_object.lo_flags */
271 	  { NULL, NULL },		/* mtx_object.lo_list */
272 	  NULL },			/* mtx_object.lo_witness */
273 	MTX_UNOWNED, 0,			/* mtx_lock, mtx_recurse */
274 	TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked),
275 	{ NULL, NULL }			/* mtx_contested */
276 };
277 
278 /*
279  * This global is set to 0 once it becomes safe to use the witness code.
280  */
281 static int witness_cold = 1;
282 
283 /*
284  * Global variables for book keeping.
285  */
286 static int lock_cur_cnt;
287 static int lock_max_cnt;
288 
289 /*
290  * The WITNESS-enabled diagnostic code.
291  */
292 static void
293 witness_initialize(void *dummy __unused)
294 {
295 	struct lock_object *lock;
296 	struct witness_order_list_entry *order;
297 	struct witness *w, *w1;
298 	int i;
299 
300 	/*
301 	 * We have to release Giant before initializing its witness
302 	 * structure so that WITNESS doesn't get confused.
303 	 */
304 	mtx_unlock(&Giant);
305 	mtx_assert(&Giant, MA_NOTOWNED);
306 
307 	CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
308 	TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
309 	mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
310 	    MTX_NOWITNESS);
311 	for (i = 0; i < WITNESS_COUNT; i++)
312 		witness_free(&w_data[i]);
313 	for (i = 0; i < WITNESS_CHILDCOUNT; i++)
314 		witness_child_free(&w_childdata[i]);
315 	for (i = 0; i < LOCK_CHILDCOUNT; i++)
316 		witness_lock_list_free(&w_locklistdata[i]);
317 
318 	/* First add in all the specified order lists. */
319 	for (order = order_lists; order->w_name != NULL; order++) {
320 		w = enroll(order->w_name, order->w_class);
321 		if (w == NULL)
322 			continue;
323 		w->w_file = "order list";
324 		for (order++; order->w_name != NULL; order++) {
325 			w1 = enroll(order->w_name, order->w_class);
326 			if (w1 == NULL)
327 				continue;
328 			w1->w_file = "order list";
329 			itismychild(w, w1);
330 			w = w1;
331 		}
332 	}
333 
334 	/* Iterate through all locks and add them to witness. */
335 	mtx_lock(&all_mtx);
336 	TAILQ_FOREACH(lock, &all_locks, lo_list) {
337 		if (lock->lo_flags & LO_WITNESS)
338 			lock->lo_witness = enroll(lock->lo_type,
339 			    lock->lo_class);
340 		else
341 			lock->lo_witness = NULL;
342 	}
343 	mtx_unlock(&all_mtx);
344 
345 	/* Mark the witness code as being ready for use. */
346 	atomic_store_rel_int(&witness_cold, 0);
347 
348 	mtx_lock(&Giant);
349 }
350 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
351 
352 void
353 witness_init(struct lock_object *lock)
354 {
355 	struct lock_class *class;
356 
357 	class = lock->lo_class;
358 	if (lock->lo_flags & LO_INITIALIZED)
359 		panic("%s: lock (%s) %s is already initialized", __func__,
360 		    class->lc_name, lock->lo_name);
361 	if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
362 	    (class->lc_flags & LC_RECURSABLE) == 0)
363 		panic("%s: lock (%s) %s can not be recursable", __func__,
364 		    class->lc_name, lock->lo_name);
365 	if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
366 	    (class->lc_flags & LC_SLEEPABLE) == 0)
367 		panic("%s: lock (%s) %s can not be sleepable", __func__,
368 		    class->lc_name, lock->lo_name);
369 	if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
370 	    (class->lc_flags & LC_UPGRADABLE) == 0)
371 		panic("%s: lock (%s) %s can not be upgradable", __func__,
372 		    class->lc_name, lock->lo_name);
373 
374 	mtx_lock(&all_mtx);
375 	TAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
376 	lock->lo_flags |= LO_INITIALIZED;
377 	lock_cur_cnt++;
378 	if (lock_cur_cnt > lock_max_cnt)
379 		lock_max_cnt = lock_cur_cnt;
380 	mtx_unlock(&all_mtx);
381 	if (!witness_cold && !witness_dead && panicstr == NULL &&
382 	    (lock->lo_flags & LO_WITNESS) != 0)
383 		lock->lo_witness = enroll(lock->lo_type, class);
384 	else
385 		lock->lo_witness = NULL;
386 }
387 
388 void
389 witness_destroy(struct lock_object *lock)
390 {
391 	struct witness *w;
392 
393 	if (witness_cold)
394 		panic("lock (%s) %s destroyed while witness_cold",
395 		    lock->lo_class->lc_name, lock->lo_name);
396 	if ((lock->lo_flags & LO_INITIALIZED) == 0)
397 		panic("%s: lock (%s) %s is not initialized", __func__,
398 		    lock->lo_class->lc_name, lock->lo_name);
399 
400 	/* XXX: need to verify that no one holds the lock */
401 	w = lock->lo_witness;
402 	if (w != NULL) {
403 		mtx_lock_spin(&w_mtx);
404 		MPASS(w->w_refcount > 0);
405 		w->w_refcount--;
406 		mtx_unlock_spin(&w_mtx);
407 	}
408 
409 	mtx_lock(&all_mtx);
410 	lock_cur_cnt--;
411 	TAILQ_REMOVE(&all_locks, lock, lo_list);
412 	lock->lo_flags &= ~LO_INITIALIZED;
413 	mtx_unlock(&all_mtx);
414 }
415 
416 #if defined(DDB)
417 static void
418 witness_display_list(void(*prnt)(const char *fmt, ...),
419 		     struct witness_list *list)
420 {
421 	struct witness *w, *w1;
422 	int found;
423 
424 	STAILQ_FOREACH(w, list, w_typelist) {
425 		if (w->w_file == NULL)
426 			continue;
427 		found = 0;
428 		STAILQ_FOREACH(w1, list, w_typelist) {
429 			if (isitmychild(w1, w)) {
430 				found++;
431 				break;
432 			}
433 		}
434 		if (found)
435 			continue;
436 		/*
437 		 * This lock has no anscestors, display its descendants.
438 		 */
439 		witness_displaydescendants(prnt, w);
440 	}
441 }
442 
443 static void
444 witness_display(void(*prnt)(const char *fmt, ...))
445 {
446 	struct witness *w;
447 
448 	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
449 	witness_levelall();
450 
451 	/*
452 	 * First, handle sleep locks which have been acquired at least
453 	 * once.
454 	 */
455 	prnt("Sleep locks:\n");
456 	witness_display_list(prnt, &w_sleep);
457 
458 	/*
459 	 * Now do spin locks which have been acquired at least once.
460 	 */
461 	prnt("\nSpin locks:\n");
462 	witness_display_list(prnt, &w_spin);
463 
464 	/*
465 	 * Finally, any locks which have not been acquired yet.
466 	 */
467 	prnt("\nLocks which were never acquired:\n");
468 	STAILQ_FOREACH(w, &w_all, w_list) {
469 		if (w->w_file != NULL || w->w_refcount == 0)
470 			continue;
471 		prnt("%s\n", w->w_name);
472 	}
473 }
474 #endif
475 
476 void
477 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
478 {
479 	struct lock_list_entry **lock_list, *lle;
480 	struct lock_instance *lock1, *lock2;
481 	struct lock_class *class;
482 	struct witness *w, *w1;
483 	struct thread *td;
484 	int i, j;
485 #ifdef DDB
486 	int go_into_ddb = 0;
487 #endif /* DDB */
488 
489 	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
490 	    panicstr != NULL)
491 		return;
492 	w = lock->lo_witness;
493 	class = lock->lo_class;
494 	td = curthread;
495 
496 	if (class->lc_flags & LC_SLEEPLOCK) {
497 		/*
498 		 * Since spin locks include a critical section, this check
499 		 * impliclty enforces a lock order of all sleep locks before
500 		 * all spin locks.
501 		 */
502 		if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0)
503 			panic("blockable sleep lock (%s) %s @ %s:%d",
504 			    class->lc_name, lock->lo_name, file, line);
505 		lock_list = &td->td_sleeplocks;
506 	} else
507 		lock_list = PCPU_PTR(spinlocks);
508 
509 	/*
510 	 * Try locks do not block if they fail to acquire the lock, thus
511 	 * there is no danger of deadlocks or of switching while holding a
512 	 * spin lock if we acquire a lock via a try operation.
513 	 */
514 	if (flags & LOP_TRYLOCK)
515 		goto out;
516 
517 	/*
518 	 * Is this the first lock acquired?  If so, then no order checking
519 	 * is needed.
520 	 */
521 	if (*lock_list == NULL)
522 		goto out;
523 
524 	/*
525 	 * Check to see if we are recursing on a lock we already own.
526 	 */
527 	lock1 = find_instance(*lock_list, lock);
528 	if (lock1 != NULL) {
529 		if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
530 		    (flags & LOP_EXCLUSIVE) == 0) {
531 			printf("shared lock of (%s) %s @ %s:%d\n",
532 			    class->lc_name, lock->lo_name, file, line);
533 			printf("while exclusively locked from %s:%d\n",
534 			    lock1->li_file, lock1->li_line);
535 			panic("share->excl");
536 		}
537 		if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
538 		    (flags & LOP_EXCLUSIVE) != 0) {
539 			printf("exclusive lock of (%s) %s @ %s:%d\n",
540 			    class->lc_name, lock->lo_name, file, line);
541 			printf("while share locked from %s:%d\n",
542 			    lock1->li_file, lock1->li_line);
543 			panic("excl->share");
544 		}
545 		lock1->li_flags++;
546 		if ((lock->lo_flags & LO_RECURSABLE) == 0) {
547 			printf(
548 			"recursed on non-recursive lock (%s) %s @ %s:%d\n",
549 			    class->lc_name, lock->lo_name, file, line);
550 			printf("first acquired @ %s:%d\n", lock1->li_file,
551 			    lock1->li_line);
552 			panic("recurse");
553 		}
554 		CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
555 		    td->td_proc->p_pid, lock->lo_name,
556 		    lock1->li_flags & LI_RECURSEMASK);
557 		lock1->li_file = file;
558 		lock1->li_line = line;
559 		return;
560 	}
561 
562 	/*
563 	 * Check for duplicate locks of the same type.  Note that we only
564 	 * have to check for this on the last lock we just acquired.  Any
565 	 * other cases will be caught as lock order violations.
566 	 */
567 	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
568 	w1 = lock1->li_lock->lo_witness;
569 	if (w1 == w) {
570 		if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK))
571 			goto out;
572 		w->w_same_squawked = 1;
573 		printf("acquiring duplicate lock of same type: \"%s\"\n",
574 			lock->lo_type);
575 		printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name,
576 		    lock1->li_file, lock1->li_line);
577 		printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
578 #ifdef DDB
579 		go_into_ddb = 1;
580 #endif /* DDB */
581 		goto out;
582 	}
583 	MPASS(!mtx_owned(&w_mtx));
584 	mtx_lock_spin(&w_mtx);
585 	/*
586 	 * If we have a known higher number just say ok
587 	 */
588 	if (witness_watch > 1 && w->w_level > w1->w_level) {
589 		mtx_unlock_spin(&w_mtx);
590 		goto out;
591 	}
592 	if (isitmydescendant(w1, w)) {
593 		mtx_unlock_spin(&w_mtx);
594 		goto out;
595 	}
596 	for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
597 		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
598 
599 			MPASS(j < WITNESS_COUNT);
600 			lock1 = &lle->ll_children[i];
601 			w1 = lock1->li_lock->lo_witness;
602 
603 			/*
604 			 * If this lock doesn't undergo witness checking,
605 			 * then skip it.
606 			 */
607 			if (w1 == NULL) {
608 				KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
609 				    ("lock missing witness structure"));
610 				continue;
611 			}
612 			/*
613 			 * If we are locking Giant and we slept with this
614 			 * lock, then skip it.
615 			 */
616 			if ((lock1->li_flags & LI_SLEPT) != 0 &&
617 			    lock == &Giant.mtx_object)
618 				continue;
619 			/*
620 			 * If we are locking a sleepable lock and this lock
621 			 * isn't sleepable and isn't Giant, we want to treat
622 			 * it as a lock order violation to enfore a general
623 			 * lock order of sleepable locks before non-sleepable
624 			 * locks.  Thus, we only bother checking the lock
625 			 * order hierarchy if we pass the initial test.
626 			 */
627 			if (!((lock->lo_flags & LO_SLEEPABLE) != 0 &&
628 			    ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 &&
629 			    lock1->li_lock != &Giant.mtx_object)) &&
630 			    !isitmydescendant(w, w1))
631 				continue;
632 			/*
633 			 * We have a lock order violation, check to see if it
634 			 * is allowed or has already been yelled about.
635 			 */
636 			mtx_unlock_spin(&w_mtx);
637 #ifdef BLESSING
638 			if (blessed(w, w1))
639 				goto out;
640 #endif
641 			if (lock1->li_lock == &Giant.mtx_object) {
642 				if (w1->w_Giant_squawked)
643 					goto out;
644 				else
645 					w1->w_Giant_squawked = 1;
646 			} else {
647 				if (w1->w_other_squawked)
648 					goto out;
649 				else
650 					w1->w_other_squawked = 1;
651 			}
652 			/*
653 			 * Ok, yell about it.
654 			 */
655 			printf("lock order reversal\n");
656 			/*
657 			 * Try to locate an earlier lock with
658 			 * witness w in our list.
659 			 */
660 			do {
661 				lock2 = &lle->ll_children[i];
662 				MPASS(lock2->li_lock != NULL);
663 				if (lock2->li_lock->lo_witness == w)
664 					break;
665 				i--;
666 				if (i == 0 && lle->ll_next != NULL) {
667 					lle = lle->ll_next;
668 					i = lle->ll_count - 1;
669 					MPASS(i >= 0 && i < LOCK_NCHILDREN);
670 				}
671 			} while (i >= 0);
672 			if (i < 0) {
673 				printf(" 1st %p %s (%s) @ %s:%d\n",
674 				    lock1->li_lock, lock1->li_lock->lo_name,
675 				    lock1->li_lock->lo_type, lock1->li_file,
676 				    lock1->li_line);
677 				printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
678 				    lock->lo_name, lock->lo_type, file, line);
679 			} else {
680 				printf(" 1st %p %s (%s) @ %s:%d\n",
681 				    lock2->li_lock, lock2->li_lock->lo_name,
682 				    lock2->li_lock->lo_type, lock2->li_file,
683 				    lock2->li_line);
684 				printf(" 2nd %p %s (%s) @ %s:%d\n",
685 				    lock1->li_lock, lock1->li_lock->lo_name,
686 				    lock1->li_lock->lo_type, lock1->li_file,
687 				    lock1->li_line);
688 				printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
689 				    lock->lo_name, lock->lo_type, file, line);
690 			}
691 #ifdef DDB
692 			go_into_ddb = 1;
693 #endif /* DDB */
694 			goto out;
695 		}
696 	}
697 	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
698 	/*
699 	 * Don't build a new relationship if we are locking Giant just
700 	 * after waking up and the previous lock in the list was acquired
701 	 * prior to blocking.
702 	 */
703 	if (lock == &Giant.mtx_object && (lock1->li_flags & LI_SLEPT) != 0)
704 		mtx_unlock_spin(&w_mtx);
705 	else {
706 		CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
707 		    lock->lo_type, lock1->li_lock->lo_type);
708 		if (!itismychild(lock1->li_lock->lo_witness, w))
709 			mtx_unlock_spin(&w_mtx);
710 	}
711 
712 out:
713 #ifdef DDB
714 	if (witness_ddb && go_into_ddb)
715 		Debugger(__func__);
716 #endif /* DDB */
717 	w->w_file = file;
718 	w->w_line = line;
719 
720 	lle = *lock_list;
721 	if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
722 		lle = witness_lock_list_get();
723 		if (lle == NULL)
724 			return;
725 		lle->ll_next = *lock_list;
726 		CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
727 		    td->td_proc->p_pid, lle);
728 		*lock_list = lle;
729 	}
730 	lock1 = &lle->ll_children[lle->ll_count++];
731 	lock1->li_lock = lock;
732 	lock1->li_line = line;
733 	lock1->li_file = file;
734 	if ((flags & LOP_EXCLUSIVE) != 0)
735 		lock1->li_flags = LI_EXCLUSIVE;
736 	else
737 		lock1->li_flags = 0;
738 	CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
739 	    td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
740 }
741 
742 void
743 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
744 {
745 	struct lock_instance *instance;
746 	struct lock_class *class;
747 
748 	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
749 	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
750 		return;
751 	class = lock->lo_class;
752 	if ((lock->lo_flags & LO_UPGRADABLE) == 0)
753 		panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
754 		    class->lc_name, lock->lo_name, file, line);
755 	if ((flags & LOP_TRYLOCK) == 0)
756 		panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name,
757 		    lock->lo_name, file, line);
758 	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
759 		panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
760 		    class->lc_name, lock->lo_name, file, line);
761 	instance = find_instance(curthread->td_sleeplocks, lock);
762 	if (instance == NULL)
763 		panic("upgrade of unlocked lock (%s) %s @ %s:%d",
764 		    class->lc_name, lock->lo_name, file, line);
765 	if ((instance->li_flags & LI_EXCLUSIVE) != 0)
766 		panic("upgrade of exclusive lock (%s) %s @ %s:%d",
767 		    class->lc_name, lock->lo_name, file, line);
768 	if ((instance->li_flags & LI_RECURSEMASK) != 0)
769 		panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
770 		    class->lc_name, lock->lo_name,
771 		    instance->li_flags & LI_RECURSEMASK, file, line);
772 	instance->li_flags |= LI_EXCLUSIVE;
773 }
774 
775 void
776 witness_downgrade(struct lock_object *lock, int flags, const char *file,
777     int line)
778 {
779 	struct lock_instance *instance;
780 	struct lock_class *class;
781 
782 	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
783 	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
784 		return;
785 	class = lock->lo_class;
786 	if ((lock->lo_flags & LO_UPGRADABLE) == 0)
787 		panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
788 		    class->lc_name, lock->lo_name, file, line);
789 	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
790 		panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
791 		    class->lc_name, lock->lo_name, file, line);
792 	instance = find_instance(curthread->td_sleeplocks, lock);
793 	if (instance == NULL)
794 		panic("downgrade of unlocked lock (%s) %s @ %s:%d",
795 		    class->lc_name, lock->lo_name, file, line);
796 	if ((instance->li_flags & LI_EXCLUSIVE) == 0)
797 		panic("downgrade of shared lock (%s) %s @ %s:%d",
798 		    class->lc_name, lock->lo_name, file, line);
799 	if ((instance->li_flags & LI_RECURSEMASK) != 0)
800 		panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
801 		    class->lc_name, lock->lo_name,
802 		    instance->li_flags & LI_RECURSEMASK, file, line);
803 	instance->li_flags &= ~LI_EXCLUSIVE;
804 }
805 
806 void
807 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
808 {
809 	struct lock_list_entry **lock_list, *lle;
810 	struct lock_instance *instance;
811 	struct lock_class *class;
812 	struct thread *td;
813 	register_t s;
814 	int i, j;
815 
816 	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
817 	    panicstr != NULL)
818 		return;
819 	td = curthread;
820 	class = lock->lo_class;
821 	if (class->lc_flags & LC_SLEEPLOCK)
822 		lock_list = &td->td_sleeplocks;
823 	else
824 		lock_list = PCPU_PTR(spinlocks);
825 	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
826 		for (i = 0; i < (*lock_list)->ll_count; i++) {
827 			instance = &(*lock_list)->ll_children[i];
828 			if (instance->li_lock == lock) {
829 				if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
830 				    (flags & LOP_EXCLUSIVE) == 0) {
831 					printf(
832 					"shared unlock of (%s) %s @ %s:%d\n",
833 					    class->lc_name, lock->lo_name,
834 					    file, line);
835 					printf(
836 					"while exclusively locked from %s:%d\n",
837 					    instance->li_file,
838 					    instance->li_line);
839 					panic("excl->ushare");
840 				}
841 				if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
842 				    (flags & LOP_EXCLUSIVE) != 0) {
843 					printf(
844 					"exclusive unlock of (%s) %s @ %s:%d\n",
845 					    class->lc_name, lock->lo_name,
846 					    file, line);
847 					printf(
848 					"while share locked from %s:%d\n",
849 					    instance->li_file,
850 					    instance->li_line);
851 					panic("share->uexcl");
852 				}
853 				/* If we are recursed, unrecurse. */
854 				if ((instance->li_flags & LI_RECURSEMASK) > 0) {
855 					CTR4(KTR_WITNESS,
856 				    "%s: pid %d unrecursed on %s r=%d", __func__,
857 					    td->td_proc->p_pid,
858 					    instance->li_lock->lo_name,
859 					    instance->li_flags);
860 					instance->li_flags--;
861 					return;
862 				}
863 				s = intr_disable();
864 				CTR4(KTR_WITNESS,
865 				    "%s: pid %d removed %s from lle[%d]", __func__,
866 				    td->td_proc->p_pid,
867 				    instance->li_lock->lo_name,
868 				    (*lock_list)->ll_count - 1);
869 				for (j = i; j < (*lock_list)->ll_count - 1; j++)
870 					(*lock_list)->ll_children[j] =
871 					    (*lock_list)->ll_children[j + 1];
872 				(*lock_list)->ll_count--;
873 				intr_restore(s);
874 				if ((*lock_list)->ll_count == 0) {
875 					lle = *lock_list;
876 					*lock_list = lle->ll_next;
877 					CTR3(KTR_WITNESS,
878 					    "%s: pid %d removed lle %p", __func__,
879 					    td->td_proc->p_pid, lle);
880 					witness_lock_list_free(lle);
881 				}
882 				return;
883 			}
884 		}
885 	panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
886 	    file, line);
887 }
888 
889 /*
890  * Warn if any held locks are not sleepable.  Note that Giant and the lock
891  * passed in are both special cases since they are both released during the
892  * sleep process and aren't actually held while the thread is asleep.
893  */
894 int
895 witness_sleep(int check_only, struct lock_object *lock, const char *file,
896 	      int line)
897 {
898 	struct lock_list_entry **lock_list, *lle;
899 	struct lock_instance *lock1;
900 	struct thread *td;
901 	int i, n;
902 
903 	if (witness_cold || witness_dead || panicstr != NULL)
904 		return (0);
905 	n = 0;
906 	td = curthread;
907 	lock_list = &td->td_sleeplocks;
908 again:
909 	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
910 		for (i = lle->ll_count - 1; i >= 0; i--) {
911 			lock1 = &lle->ll_children[i];
912 			if (lock1->li_lock == lock ||
913 			    lock1->li_lock == &Giant.mtx_object)
914 				continue;
915 			if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) {
916 				if (check_only == 0) {
917 					CTR3(KTR_WITNESS,
918 				    "pid %d: sleeping with lock (%s) %s held",
919 					    td->td_proc->p_pid,
920 					    lock1->li_lock->lo_class->lc_name,
921 					    lock1->li_lock->lo_name);
922 					lock1->li_flags |= LI_SLEPT;
923 				}
924 				continue;
925 			}
926 			n++;
927 			printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
928 			    file, line, check_only ? "could sleep" : "sleeping",
929 			    lock1->li_lock->lo_name, lock1->li_file,
930 			    lock1->li_line);
931 		}
932 	if (lock_list == &td->td_sleeplocks && PCPU_GET(spinlocks) != NULL) {
933 		/*
934 		 * Since we already hold a spinlock preemption is
935 		 * already blocked.
936 		 */
937 		lock_list = PCPU_PTR(spinlocks);
938 		goto again;
939 	}
940 #ifdef DDB
941 	if (witness_ddb && n)
942 		Debugger(__func__);
943 #endif /* DDB */
944 	return (n);
945 }
946 
947 const char *
948 witness_file(struct lock_object *lock)
949 {
950 	struct witness *w;
951 
952 	if (witness_cold || witness_dead || lock->lo_witness == NULL)
953 		return ("?");
954 	w = lock->lo_witness;
955 	return (w->w_file);
956 }
957 
958 int
959 witness_line(struct lock_object *lock)
960 {
961 	struct witness *w;
962 
963 	if (witness_cold || witness_dead || lock->lo_witness == NULL)
964 		return (0);
965 	w = lock->lo_witness;
966 	return (w->w_line);
967 }
968 
969 static struct witness *
970 enroll(const char *description, struct lock_class *lock_class)
971 {
972 	struct witness *w;
973 
974 	if (!witness_watch || witness_dead || panicstr != NULL)
975 		return (NULL);
976 	if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
977 		return (NULL);
978 	mtx_lock_spin(&w_mtx);
979 	STAILQ_FOREACH(w, &w_all, w_list) {
980 		if (w->w_name == description || (w->w_refcount > 0 &&
981 		    strcmp(description, w->w_name) == 0)) {
982 			w->w_refcount++;
983 			mtx_unlock_spin(&w_mtx);
984 			if (lock_class != w->w_class)
985 				panic(
986 				"lock (%s) %s does not match earlier (%s) lock",
987 				    description, lock_class->lc_name,
988 				    w->w_class->lc_name);
989 			return (w);
990 		}
991 	}
992 	/*
993 	 * This isn't quite right, as witness_cold is still 0 while we
994 	 * enroll all the locks initialized before witness_initialize().
995 	 */
996 	if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
997 		mtx_unlock_spin(&w_mtx);
998 		panic("spin lock %s not in order list", description);
999 	}
1000 	if ((w = witness_get()) == NULL)
1001 		return (NULL);
1002 	w->w_name = description;
1003 	w->w_class = lock_class;
1004 	w->w_refcount = 1;
1005 	STAILQ_INSERT_HEAD(&w_all, w, w_list);
1006 	if (lock_class->lc_flags & LC_SPINLOCK)
1007 		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1008 	else if (lock_class->lc_flags & LC_SLEEPLOCK)
1009 		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1010 	else {
1011 		mtx_unlock_spin(&w_mtx);
1012 		panic("lock class %s is not sleep or spin",
1013 		    lock_class->lc_name);
1014 	}
1015 	mtx_unlock_spin(&w_mtx);
1016 	return (w);
1017 }
1018 
1019 static int
1020 itismychild(struct witness *parent, struct witness *child)
1021 {
1022 	static int recursed;
1023 	struct witness_child_list_entry **wcl;
1024 	struct witness_list *list;
1025 
1026 	MPASS(child != NULL && parent != NULL);
1027 	if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
1028 	    (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
1029 		panic(
1030 		"%s: parent (%s) and child (%s) are not the same lock type",
1031 		    __func__, parent->w_class->lc_name,
1032 		    child->w_class->lc_name);
1033 
1034 	/*
1035 	 * Insert "child" after "parent"
1036 	 */
1037 	wcl = &parent->w_children;
1038 	while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
1039 		wcl = &(*wcl)->wcl_next;
1040 	if (*wcl == NULL) {
1041 		*wcl = witness_child_get();
1042 		if (*wcl == NULL)
1043 			return (1);
1044 	}
1045 	(*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
1046 
1047 	/*
1048 	 * Now prune whole tree.  We look for cases where a lock is now
1049 	 * both a descendant and a direct child of a given lock.  In that
1050 	 * case, we want to remove the direct child link from the tree.
1051 	 */
1052 	if (recursed)
1053 		return (0);
1054 	recursed = 1;
1055 	if (parent->w_class->lc_flags & LC_SLEEPLOCK)
1056 		list = &w_sleep;
1057 	else
1058 		list = &w_spin;
1059 	STAILQ_FOREACH(child, list, w_typelist) {
1060 		STAILQ_FOREACH(parent, list, w_typelist) {
1061 			if (!isitmychild(parent, child))
1062 				continue;
1063 			removechild(parent, child);
1064 			if (isitmydescendant(parent, child))
1065 				continue;
1066 			itismychild(parent, child);
1067 		}
1068 	}
1069 	recursed = 0;
1070 	witness_levelall();
1071 	return (0);
1072 }
1073 
1074 static void
1075 removechild(struct witness *parent, struct witness *child)
1076 {
1077 	struct witness_child_list_entry **wcl, *wcl1;
1078 	int i;
1079 
1080 	for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1081 		for (i = 0; i < (*wcl)->wcl_count; i++)
1082 			if ((*wcl)->wcl_children[i] == child)
1083 				goto found;
1084 	return;
1085 found:
1086 	(*wcl)->wcl_count--;
1087 	if ((*wcl)->wcl_count > i)
1088 		(*wcl)->wcl_children[i] =
1089 		    (*wcl)->wcl_children[(*wcl)->wcl_count];
1090 	MPASS((*wcl)->wcl_children[i] != NULL);
1091 	if ((*wcl)->wcl_count != 0)
1092 		return;
1093 	wcl1 = *wcl;
1094 	*wcl = wcl1->wcl_next;
1095 	witness_child_free(wcl1);
1096 }
1097 
1098 static int
1099 isitmychild(struct witness *parent, struct witness *child)
1100 {
1101 	struct witness_child_list_entry *wcl;
1102 	int i;
1103 
1104 	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1105 		for (i = 0; i < wcl->wcl_count; i++) {
1106 			if (wcl->wcl_children[i] == child)
1107 				return (1);
1108 		}
1109 	}
1110 	return (0);
1111 }
1112 
1113 static int
1114 isitmydescendant(struct witness *parent, struct witness *child)
1115 {
1116 	struct witness_child_list_entry *wcl;
1117 	int i, j;
1118 
1119 	if (isitmychild(parent, child))
1120 		return (1);
1121 	j = 0;
1122 	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1123 		MPASS(j < 1000);
1124 		for (i = 0; i < wcl->wcl_count; i++) {
1125 			if (isitmydescendant(wcl->wcl_children[i], child))
1126 				return (1);
1127 		}
1128 		j++;
1129 	}
1130 	return (0);
1131 }
1132 
1133 static void
1134 witness_levelall (void)
1135 {
1136 	struct witness_list *list;
1137 	struct witness *w, *w1;
1138 
1139 	/*
1140 	 * First clear all levels.
1141 	 */
1142 	STAILQ_FOREACH(w, &w_all, w_list) {
1143 		w->w_level = 0;
1144 	}
1145 
1146 	/*
1147 	 * Look for locks with no parent and level all their descendants.
1148 	 */
1149 	STAILQ_FOREACH(w, &w_all, w_list) {
1150 		/*
1151 		 * This is just an optimization, technically we could get
1152 		 * away just walking the all list each time.
1153 		 */
1154 		if (w->w_class->lc_flags & LC_SLEEPLOCK)
1155 			list = &w_sleep;
1156 		else
1157 			list = &w_spin;
1158 		STAILQ_FOREACH(w1, list, w_typelist) {
1159 			if (isitmychild(w1, w))
1160 				goto skip;
1161 		}
1162 		witness_leveldescendents(w, 0);
1163 	skip:
1164 		;	/* silence GCC 3.x */
1165 	}
1166 }
1167 
1168 static void
1169 witness_leveldescendents(struct witness *parent, int level)
1170 {
1171 	struct witness_child_list_entry *wcl;
1172 	int i;
1173 
1174 	if (parent->w_level < level)
1175 		parent->w_level = level;
1176 	level++;
1177 	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1178 		for (i = 0; i < wcl->wcl_count; i++)
1179 			witness_leveldescendents(wcl->wcl_children[i], level);
1180 }
1181 
1182 static void
1183 witness_displaydescendants(void(*prnt)(const char *fmt, ...),
1184 			   struct witness *parent)
1185 {
1186 	struct witness_child_list_entry *wcl;
1187 	int i, level;
1188 
1189 	level = parent->w_level;
1190 	prnt("%-2d", level);
1191 	for (i = 0; i < level; i++)
1192 		prnt(" ");
1193 	if (parent->w_refcount > 0) {
1194 		prnt("%s", parent->w_name);
1195 		if (parent->w_file != NULL)
1196 			prnt(" -- last acquired @ %s:%d\n", parent->w_file,
1197 			    parent->w_line);
1198 	} else
1199 		prnt("(dead)\n");
1200 	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1201 		for (i = 0; i < wcl->wcl_count; i++)
1202 			    witness_displaydescendants(prnt,
1203 				wcl->wcl_children[i]);
1204 }
1205 
1206 #ifdef BLESSING
1207 static int
1208 blessed(struct witness *w1, struct witness *w2)
1209 {
1210 	int i;
1211 	struct witness_blessed *b;
1212 
1213 	for (i = 0; i < blessed_count; i++) {
1214 		b = &blessed_list[i];
1215 		if (strcmp(w1->w_name, b->b_lock1) == 0) {
1216 			if (strcmp(w2->w_name, b->b_lock2) == 0)
1217 				return (1);
1218 			continue;
1219 		}
1220 		if (strcmp(w1->w_name, b->b_lock2) == 0)
1221 			if (strcmp(w2->w_name, b->b_lock1) == 0)
1222 				return (1);
1223 	}
1224 	return (0);
1225 }
1226 #endif
1227 
1228 static struct witness *
1229 witness_get(void)
1230 {
1231 	struct witness *w;
1232 
1233 	if (witness_dead) {
1234 		mtx_unlock_spin(&w_mtx);
1235 		return (NULL);
1236 	}
1237 	if (STAILQ_EMPTY(&w_free)) {
1238 		witness_dead = 1;
1239 		mtx_unlock_spin(&w_mtx);
1240 		printf("%s: witness exhausted\n", __func__);
1241 		return (NULL);
1242 	}
1243 	w = STAILQ_FIRST(&w_free);
1244 	STAILQ_REMOVE_HEAD(&w_free, w_list);
1245 	bzero(w, sizeof(*w));
1246 	return (w);
1247 }
1248 
1249 static void
1250 witness_free(struct witness *w)
1251 {
1252 
1253 	STAILQ_INSERT_HEAD(&w_free, w, w_list);
1254 }
1255 
1256 static struct witness_child_list_entry *
1257 witness_child_get(void)
1258 {
1259 	struct witness_child_list_entry *wcl;
1260 
1261 	if (witness_dead) {
1262 		mtx_unlock_spin(&w_mtx);
1263 		return (NULL);
1264 	}
1265 	wcl = w_child_free;
1266 	if (wcl == NULL) {
1267 		witness_dead = 1;
1268 		mtx_unlock_spin(&w_mtx);
1269 		printf("%s: witness exhausted\n", __func__);
1270 		return (NULL);
1271 	}
1272 	w_child_free = wcl->wcl_next;
1273 	bzero(wcl, sizeof(*wcl));
1274 	return (wcl);
1275 }
1276 
1277 static void
1278 witness_child_free(struct witness_child_list_entry *wcl)
1279 {
1280 
1281 	wcl->wcl_next = w_child_free;
1282 	w_child_free = wcl;
1283 }
1284 
1285 static struct lock_list_entry *
1286 witness_lock_list_get(void)
1287 {
1288 	struct lock_list_entry *lle;
1289 
1290 	if (witness_dead)
1291 		return (NULL);
1292 	mtx_lock_spin(&w_mtx);
1293 	lle = w_lock_list_free;
1294 	if (lle == NULL) {
1295 		witness_dead = 1;
1296 		mtx_unlock_spin(&w_mtx);
1297 		printf("%s: witness exhausted\n", __func__);
1298 		return (NULL);
1299 	}
1300 	w_lock_list_free = lle->ll_next;
1301 	mtx_unlock_spin(&w_mtx);
1302 	bzero(lle, sizeof(*lle));
1303 	return (lle);
1304 }
1305 
1306 static void
1307 witness_lock_list_free(struct lock_list_entry *lle)
1308 {
1309 
1310 	mtx_lock_spin(&w_mtx);
1311 	lle->ll_next = w_lock_list_free;
1312 	w_lock_list_free = lle;
1313 	mtx_unlock_spin(&w_mtx);
1314 }
1315 
1316 static struct lock_instance *
1317 find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1318 {
1319 	struct lock_list_entry *lle;
1320 	struct lock_instance *instance;
1321 	int i;
1322 
1323 	for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1324 		for (i = lle->ll_count - 1; i >= 0; i--) {
1325 			instance = &lle->ll_children[i];
1326 			if (instance->li_lock == lock)
1327 				return (instance);
1328 		}
1329 	return (NULL);
1330 }
1331 
1332 int
1333 witness_list_locks(struct lock_list_entry **lock_list)
1334 {
1335 	struct lock_list_entry *lle;
1336 	struct lock_instance *instance;
1337 	struct lock_object *lock;
1338 	int i, nheld;
1339 
1340 	nheld = 0;
1341 	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1342 		for (i = lle->ll_count - 1; i >= 0; i--) {
1343 			instance = &lle->ll_children[i];
1344 			lock = instance->li_lock;
1345 			printf("%s %s %s",
1346 			    (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1347 			    "exclusive" : "shared",
1348 			    lock->lo_class->lc_name, lock->lo_name);
1349 			if (lock->lo_type != lock->lo_name)
1350 				printf(" (%s)", lock->lo_type);
1351 			printf(" r = %d (%p) locked @ %s:%d\n",
1352 			    instance->li_flags & LI_RECURSEMASK, lock,
1353 			    instance->li_file, instance->li_line);
1354 			nheld++;
1355 		}
1356 	return (nheld);
1357 }
1358 
1359 /*
1360  * Calling this on td != curthread is bad unless we are in ddb.
1361  */
1362 int
1363 witness_list(struct thread *td)
1364 {
1365 	int nheld;
1366 
1367 	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1368 #ifdef DDB
1369 	KASSERT(td == curthread || db_active,
1370 	    ("%s: td != curthread and we aren't in the debugger", __func__));
1371 	if (!db_active && witness_dead)
1372 		return (0);
1373 #else
1374 	KASSERT(td == curthread, ("%s: p != curthread", __func__));
1375 	if (witness_dead)
1376 		return (0);
1377 #endif
1378 	nheld = witness_list_locks(&td->td_sleeplocks);
1379 
1380 	/*
1381 	 * We only handle spinlocks if td == curthread.  This is somewhat broken
1382 	 * if td is currently executing on some other CPU and holds spin locks
1383 	 * as we won't display those locks.  If we had a MI way of getting
1384 	 * the per-cpu data for a given cpu then we could use
1385 	 * td->td_kse->ke_oncpu to get the list of spinlocks for this thread
1386 	 * and "fix" this.
1387 	 *
1388 	 * That still wouldn't really fix this unless we locked sched_lock
1389 	 * or stopped the other CPU to make sure it wasn't changing the list
1390 	 * out from under us.  It is probably best to just not try to handle
1391 	 * threads on other CPU's for now.
1392 	 */
1393 	if (td == curthread && PCPU_GET(spinlocks) != NULL)
1394 		nheld += witness_list_locks(PCPU_PTR(spinlocks));
1395 
1396 	return (nheld);
1397 }
1398 
1399 void
1400 witness_save(struct lock_object *lock, const char **filep, int *linep)
1401 {
1402 	struct lock_instance *instance;
1403 
1404 	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1405 	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1406 		return;
1407 	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1408 		panic("%s: lock (%s) %s is not a sleep lock", __func__,
1409 		    lock->lo_class->lc_name, lock->lo_name);
1410 	instance = find_instance(curthread->td_sleeplocks, lock);
1411 	if (instance == NULL)
1412 		panic("%s: lock (%s) %s not locked", __func__,
1413 		    lock->lo_class->lc_name, lock->lo_name);
1414 	*filep = instance->li_file;
1415 	*linep = instance->li_line;
1416 }
1417 
1418 void
1419 witness_restore(struct lock_object *lock, const char *file, int line)
1420 {
1421 	struct lock_instance *instance;
1422 
1423 	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1424 	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1425 		return;
1426 	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1427 		panic("%s: lock (%s) %s is not a sleep lock", __func__,
1428 		    lock->lo_class->lc_name, lock->lo_name);
1429 	instance = find_instance(curthread->td_sleeplocks, lock);
1430 	if (instance == NULL)
1431 		panic("%s: lock (%s) %s not locked", __func__,
1432 		    lock->lo_class->lc_name, lock->lo_name);
1433 	lock->lo_witness->w_file = file;
1434 	lock->lo_witness->w_line = line;
1435 	instance->li_file = file;
1436 	instance->li_line = line;
1437 }
1438 
1439 void
1440 witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1441 {
1442 #ifdef INVARIANT_SUPPORT
1443 	struct lock_instance *instance;
1444 
1445 	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1446 		return;
1447 	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0)
1448 		instance = find_instance(curthread->td_sleeplocks, lock);
1449 	else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0)
1450 		instance = find_instance(PCPU_GET(spinlocks), lock);
1451 	else {
1452 		panic("Lock (%s) %s is not sleep or spin!",
1453 		    lock->lo_class->lc_name, lock->lo_name);
1454 		return;
1455 	}
1456 	switch (flags) {
1457 	case LA_UNLOCKED:
1458 		if (instance != NULL)
1459 			panic("Lock (%s) %s locked @ %s:%d.",
1460 			    lock->lo_class->lc_name, lock->lo_name, file, line);
1461 		break;
1462 	case LA_LOCKED:
1463 	case LA_LOCKED | LA_RECURSED:
1464 	case LA_LOCKED | LA_NOTRECURSED:
1465 	case LA_SLOCKED:
1466 	case LA_SLOCKED | LA_RECURSED:
1467 	case LA_SLOCKED | LA_NOTRECURSED:
1468 	case LA_XLOCKED:
1469 	case LA_XLOCKED | LA_RECURSED:
1470 	case LA_XLOCKED | LA_NOTRECURSED:
1471 		if (instance == NULL) {
1472 			panic("Lock (%s) %s not locked @ %s:%d.",
1473 			    lock->lo_class->lc_name, lock->lo_name, file, line);
1474 			break;
1475 		}
1476 		if ((flags & LA_XLOCKED) != 0 &&
1477 		    (instance->li_flags & LI_EXCLUSIVE) == 0)
1478 			panic("Lock (%s) %s not exclusively locked @ %s:%d.",
1479 			    lock->lo_class->lc_name, lock->lo_name, file, line);
1480 		if ((flags & LA_SLOCKED) != 0 &&
1481 		    (instance->li_flags & LI_EXCLUSIVE) != 0)
1482 			panic("Lock (%s) %s exclusively locked @ %s:%d.",
1483 			    lock->lo_class->lc_name, lock->lo_name, file, line);
1484 		if ((flags & LA_RECURSED) != 0 &&
1485 		    (instance->li_flags & LI_RECURSEMASK) == 0)
1486 			panic("Lock (%s) %s not recursed @ %s:%d.",
1487 			    lock->lo_class->lc_name, lock->lo_name, file, line);
1488 		if ((flags & LA_NOTRECURSED) != 0 &&
1489 		    (instance->li_flags & LI_RECURSEMASK) != 0)
1490 			panic("Lock (%s) %s recursed @ %s:%d.",
1491 			    lock->lo_class->lc_name, lock->lo_name, file, line);
1492 		break;
1493 	default:
1494 		panic("Invalid lock assertion at %s:%d.", file, line);
1495 
1496 	}
1497 #endif	/* INVARIANT_SUPPORT */
1498 }
1499 
1500 #ifdef DDB
1501 
1502 DB_SHOW_COMMAND(locks, db_witness_list)
1503 {
1504 	struct thread *td;
1505 	pid_t pid;
1506 	struct proc *p;
1507 
1508 	if (have_addr) {
1509 		pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1510 		    ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1511 		    ((addr >> 16) % 16) * 10000;
1512 		/* sx_slock(&allproc_lock); */
1513 		FOREACH_PROC_IN_SYSTEM(p) {
1514 			if (p->p_pid == pid)
1515 				break;
1516 		}
1517 		/* sx_sunlock(&allproc_lock); */
1518 		if (p == NULL) {
1519 			db_printf("pid %d not found\n", pid);
1520 			return;
1521 		}
1522 		FOREACH_THREAD_IN_PROC(p, td) {
1523 			witness_list(td);
1524 		}
1525 	} else {
1526 		td = curthread;
1527 		witness_list(td);
1528 	}
1529 }
1530 
1531 DB_SHOW_COMMAND(witness, db_witness_display)
1532 {
1533 
1534 	witness_display(db_printf);
1535 }
1536 #endif
1537