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