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