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