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