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