subr_witness.c revision 82243
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 82243 2001-08-23 22:44:47Z 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!\n", __func__,
339		    class->lc_name, lock->lo_name);
340
341	if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
342	    (class->lc_flags & LC_RECURSABLE) == 0)
343		panic("%s: lock (%s) %s can not be recursable!\n", __func__,
344		    class->lc_name, lock->lo_name);
345
346	if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
347	    (class->lc_flags & LC_SLEEPABLE) == 0)
348		panic("%s: lock (%s) %s can not be sleepable!\n", __func__,
349		    class->lc_name, lock->lo_name);
350
351	mtx_lock(&all_mtx);
352	STAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
353	lock->lo_flags |= LO_INITIALIZED;
354	lock_cur_cnt++;
355	if (lock_cur_cnt > lock_max_cnt)
356		lock_max_cnt = lock_cur_cnt;
357	mtx_unlock(&all_mtx);
358	if (!witness_cold && !witness_dead && panicstr == NULL &&
359	    (lock->lo_flags & LO_WITNESS) != 0)
360		lock->lo_witness = enroll(lock->lo_name, class);
361	else
362		lock->lo_witness = NULL;
363}
364
365void
366witness_destroy(struct lock_object *lock)
367{
368	struct witness *w;
369
370	if (witness_cold)
371		panic("lock (%s) %s destroyed while witness_cold",
372		    lock->lo_class->lc_name, lock->lo_name);
373
374	if ((lock->lo_flags & LO_INITIALIZED) == 0)
375		panic("%s: lock (%s) %s is not initialized!\n", __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			CTR1(KTR_WITNESS,
385			    __func__ ": marking witness %s as dead", 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\n", __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 proc *p;
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	p = curproc;
477
478	/*
479	 * We have to hold a spinlock to keep lock_list valid across the check
480	 * in the LC_SLEEPLOCK case.  In the LC_SPINLOCK case, it is already
481	 * protected by the spinlock we are currently performing the witness
482	 * checks on, so it is ok to release the lock after performing this
483	 * check.  All we have to protect is the LC_SLEEPLOCK case when no
484	 * spinlocks are held as we may get preempted during this check and
485	 * lock_list could end up pointing to some other CPU's spinlock list.
486	 */
487	mtx_lock_spin(&w_mtx);
488	lock_list = PCPU_PTR(spinlocks);
489	if (class->lc_flags & LC_SLEEPLOCK) {
490		if (*lock_list != NULL && (flags & LOP_TRYLOCK) == 0) {
491			mtx_unlock_spin(&w_mtx);
492			panic("blockable sleep lock (%s) %s @ %s:%d",
493			    class->lc_name, lock->lo_name, file, line);
494		}
495		lock_list = &p->p_sleeplocks;
496	}
497	mtx_unlock_spin(&w_mtx);
498
499	/*
500	 * Try locks do not block if they fail to acquire the lock, thus
501	 * there is no danger of deadlocks or of switching while holding a
502	 * spin lock if we acquire a lock via a try operation.
503	 */
504	if (flags & LOP_TRYLOCK)
505		goto out;
506
507	/*
508	 * Is this the first lock acquired?  If so, then no order checking
509	 * is needed.
510	 */
511	if (*lock_list == NULL)
512		goto out;
513
514	/*
515	 * Check to see if we are recursing on a lock we already own.
516	 */
517	lock1 = find_instance(*lock_list, lock);
518	if (lock1 != NULL) {
519		if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
520		    (flags & LOP_EXCLUSIVE) == 0) {
521			printf("shared lock of (%s) %s @ %s:%d\n",
522			    class->lc_name, lock->lo_name, file, line);
523			printf("while exclusively locked from %s:%d\n",
524			    lock1->li_file, lock1->li_line);
525			panic("share->excl");
526		}
527		if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
528		    (flags & LOP_EXCLUSIVE) != 0) {
529			printf("exclusive lock of (%s) %s @ %s:%d\n",
530			    class->lc_name, lock->lo_name, file, line);
531			printf("while share locked from %s:%d\n",
532			    lock1->li_file, lock1->li_line);
533			panic("excl->share");
534		}
535		lock1->li_flags++;
536		if ((lock->lo_flags & LO_RECURSABLE) == 0) {
537			printf(
538			"recursed on non-recursive lock (%s) %s @ %s:%d\n",
539			    class->lc_name, lock->lo_name, file, line);
540			printf("first acquired @ %s:%d\n", lock1->li_file,
541			    lock1->li_line);
542			panic("recurse");
543		}
544		CTR3(KTR_WITNESS, __func__ ": pid %d recursed on %s r=%d",
545		    curproc->p_pid, lock->lo_name,
546		    lock1->li_flags & LI_RECURSEMASK);
547		lock1->li_file = file;
548		lock1->li_line = line;
549		return;
550	}
551
552	/*
553	 * Check for duplicate locks of the same type.  Note that we only
554	 * have to check for this on the last lock we just acquired.  Any
555	 * other cases will be caught as lock order violations.
556	 */
557	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
558	w1 = lock1->li_lock->lo_witness;
559	if (w1 == w) {
560		if (w->w_same_squawked || dup_ok(w))
561			goto out;
562		w->w_same_squawked = 1;
563		printf("acquiring duplicate lock of same type: \"%s\"\n",
564			lock->lo_name);
565		printf(" 1st @ %s:%d\n", lock1->li_file, lock1->li_line);
566		printf(" 2nd @ %s:%d\n", file, line);
567#ifdef DDB
568		go_into_ddb = 1;
569#endif /* DDB */
570		goto out;
571	}
572	MPASS(!mtx_owned(&w_mtx));
573	mtx_lock_spin(&w_mtx);
574	/*
575	 * If we have a known higher number just say ok
576	 */
577	if (witness_watch > 1 && w->w_level > w1->w_level) {
578		mtx_unlock_spin(&w_mtx);
579		goto out;
580	}
581	if (isitmydescendant(w1, w)) {
582		mtx_unlock_spin(&w_mtx);
583		goto out;
584	}
585	for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
586		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
587
588			MPASS(j < WITNESS_COUNT);
589			lock1 = &lle->ll_children[i];
590			w1 = lock1->li_lock->lo_witness;
591
592			/*
593			 * If this lock doesn't undergo witness checking,
594			 * then skip it.
595			 */
596			if (w1 == NULL) {
597				KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
598				    ("lock missing witness structure"));
599				continue;
600			}
601			/*
602			 * If we are locking Giant and we slept with this
603			 * lock, then skip it.
604			 */
605			if ((lock1->li_flags & LI_SLEPT) != 0 &&
606			    lock == &Giant.mtx_object)
607				continue;
608			if (!isitmydescendant(w, w1))
609				continue;
610			/*
611			 * We have a lock order violation, check to see if it
612			 * is allowed or has already been yelled about.
613			 */
614			mtx_unlock_spin(&w_mtx);
615			if (blessed(w, w1))
616				goto out;
617			if (lock1->li_lock == &Giant.mtx_object) {
618				if (w1->w_Giant_squawked)
619					goto out;
620				else
621					w1->w_Giant_squawked = 1;
622			} else {
623				if (w1->w_other_squawked)
624					goto out;
625				else
626					w1->w_other_squawked = 1;
627			}
628			/*
629			 * Ok, yell about it.
630			 */
631			printf("lock order reversal\n");
632			/*
633			 * Try to locate an earlier lock with
634			 * witness w in our list.
635			 */
636			do {
637				lock2 = &lle->ll_children[i];
638				MPASS(lock2->li_lock != NULL);
639				if (lock2->li_lock->lo_witness == w)
640					break;
641				i--;
642				if (i == 0 && lle->ll_next != NULL) {
643					lle = lle->ll_next;
644					i = lle->ll_count - 1;
645					MPASS(i != 0);
646				}
647			} while (i >= 0);
648			if (i < 0) {
649				printf(" 1st %p %s @ %s:%d\n", lock1->li_lock,
650				    lock1->li_lock->lo_name, lock1->li_file,
651				    lock1->li_line);
652				printf(" 2nd %p %s @ %s:%d\n", lock,
653				    lock->lo_name, file, line);
654			} else {
655				printf(" 1st %p %s @ %s:%d\n", lock2->li_lock,
656				    lock2->li_lock->lo_name, lock2->li_file,
657				    lock2->li_line);
658				printf(" 2nd %p %s @ %s:%d\n", lock1->li_lock,
659				    lock1->li_lock->lo_name, lock1->li_file,
660				    lock1->li_line);
661				printf(" 3rd %p %s @ %s:%d\n", lock,
662				    lock->lo_name, file, line);
663			}
664#ifdef DDB
665			go_into_ddb = 1;
666#endif /* DDB */
667			goto out;
668		}
669	}
670	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
671	/*
672	 * Don't build a new relationship if we are locking Giant just
673	 * after waking up and the previous lock in the list was acquired
674	 * prior to blocking.
675	 */
676	if (lock == &Giant.mtx_object && (lock1->li_flags & LI_SLEPT) != 0)
677		mtx_unlock_spin(&w_mtx);
678	else {
679		CTR2(KTR_WITNESS, __func__ ": adding %s as a child of %s",
680		    lock->lo_name, lock1->li_lock->lo_name);
681		if (!itismychild(lock1->li_lock->lo_witness, w))
682			mtx_unlock_spin(&w_mtx);
683	}
684
685out:
686#ifdef DDB
687	if (witness_ddb && go_into_ddb)
688		Debugger(__func__);
689#endif /* DDB */
690	w->w_file = file;
691	w->w_line = line;
692
693	lle = *lock_list;
694	if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
695		lle = witness_lock_list_get();
696		if (lle == NULL)
697			return;
698		lle->ll_next = *lock_list;
699		CTR2(KTR_WITNESS, __func__ ": pid %d added lle %p",
700		    curproc->p_pid, lle);
701		*lock_list = lle;
702	}
703	lock1 = &lle->ll_children[lle->ll_count++];
704	lock1->li_lock = lock;
705	lock1->li_line = line;
706	lock1->li_file = file;
707	if ((flags & LOP_EXCLUSIVE) != 0)
708		lock1->li_flags = LI_EXCLUSIVE;
709	else
710		lock1->li_flags = 0;
711	CTR3(KTR_WITNESS, __func__ ": pid %d added %s as lle[%d]",
712	    curproc->p_pid, lock->lo_name, lle->ll_count - 1);
713}
714
715void
716witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
717{
718	struct lock_list_entry **lock_list, *lle;
719	struct lock_instance *instance;
720	struct lock_class *class;
721	struct proc *p;
722	critical_t s;
723	int i, j;
724
725	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
726	    panicstr != NULL)
727		return;
728	p = curproc;
729	class = lock->lo_class;
730	if (class->lc_flags & LC_SLEEPLOCK)
731		lock_list = &p->p_sleeplocks;
732	else
733		lock_list = PCPU_PTR(spinlocks);
734	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
735		for (i = 0; i < (*lock_list)->ll_count; i++) {
736			instance = &(*lock_list)->ll_children[i];
737			if (instance->li_lock == lock) {
738				if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
739				    (flags & LOP_EXCLUSIVE) == 0) {
740					printf(
741					"shared unlock of (%s) %s @ %s:%d\n",
742					    class->lc_name, lock->lo_name,
743					    file, line);
744					printf(
745					"while exclusively locked from %s:%d\n",
746					    instance->li_file,
747					    instance->li_line);
748					panic("excl->ushare");
749				}
750				if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
751				    (flags & LOP_EXCLUSIVE) != 0) {
752					printf(
753					"exclusive unlock of (%s) %s @ %s:%d\n",
754					    class->lc_name, lock->lo_name,
755					    file, line);
756					printf(
757					"while share locked from %s:%d\n",
758					    instance->li_file,
759					    instance->li_line);
760					panic("share->uexcl");
761				}
762				/* If we are recursed, unrecurse. */
763				if ((instance->li_flags & LI_RECURSEMASK) > 0) {
764					CTR3(KTR_WITNESS,
765				    __func__ ": pid %d unrecursed on %s r=%d",
766					    curproc->p_pid,
767					    instance->li_lock->lo_name,
768					    instance->li_flags);
769					instance->li_flags--;
770					goto out;
771				}
772				s = critical_enter();
773				CTR3(KTR_WITNESS,
774				    __func__ ": pid %d removed %s from lle[%d]",
775				    curproc->p_pid, instance->li_lock->lo_name,
776				    (*lock_list)->ll_count - 1);
777				(*lock_list)->ll_count--;
778				for (j = i; j < (*lock_list)->ll_count; j++)
779					(*lock_list)->ll_children[j] =
780					    (*lock_list)->ll_children[j + 1];
781				critical_exit(s);
782				if ((*lock_list)->ll_count == 0) {
783					lle = *lock_list;
784					*lock_list = lle->ll_next;
785					CTR2(KTR_WITNESS,
786					    __func__ ": pid %d removed lle %p",
787					    curproc->p_pid, lle);
788					witness_lock_list_free(lle);
789				}
790				goto out;
791			}
792		}
793	panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
794	    file, line);
795out:
796	/*
797	 * We don't need to protect this PCPU_GET() here against preemption
798	 * because if we hold any spinlocks then we are already protected,
799	 * and if we don't we will get NULL if we hold no spinlocks even if
800	 * we switch CPU's while reading it.
801	 */
802	if (class->lc_flags & LC_SLEEPLOCK) {
803		if ((flags & LOP_NOSWITCH) == 0 && PCPU_GET(spinlocks) != NULL)
804			panic("switchable sleep unlock (%s) %s @ %s:%d",
805			    class->lc_name, lock->lo_name, file, line);
806	}
807}
808
809/*
810 * Warn if any held locks are not sleepable.  Note that Giant and the lock
811 * passed in are both special cases since they are both released during the
812 * sleep process and aren't actually held while the process is asleep.
813 */
814int
815witness_sleep(int check_only, struct lock_object *lock, const char *file,
816	      int line)
817{
818	struct lock_list_entry **lock_list, *lle;
819	struct lock_instance *lock1;
820	struct proc *p;
821	critical_t savecrit;
822	int i, n;
823
824	if (witness_dead || panicstr != NULL)
825		return (0);
826	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
827	n = 0;
828	/*
829	 * Preemption bad because we need PCPU_PTR(spinlocks) to not change.
830	 */
831	savecrit = critical_enter();
832	p = curproc;
833	lock_list = &p->p_sleeplocks;
834again:
835	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
836		for (i = lle->ll_count - 1; i >= 0; i--) {
837			lock1 = &lle->ll_children[i];
838			if (lock1->li_lock == lock ||
839			    lock1->li_lock == &Giant.mtx_object)
840				continue;
841			if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) {
842				if (check_only == 0) {
843					CTR3(KTR_WITNESS,
844				    "pid %d: sleeping with lock (%s) %s held",
845					    curproc->p_pid,
846					    lock1->li_lock->lo_class->lc_name,
847					    lock1->li_lock->lo_name);
848					lock1->li_flags |= LI_SLEPT;
849				}
850				continue;
851			}
852			n++;
853			printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
854			    file, line, check_only ? "could sleep" : "sleeping",
855			    lock1->li_lock->lo_name, lock1->li_file,
856			    lock1->li_line);
857		}
858	if (lock_list == &p->p_sleeplocks) {
859		lock_list = PCPU_PTR(spinlocks);
860		goto again;
861	}
862#ifdef DDB
863	if (witness_ddb && n)
864		Debugger(__func__);
865#endif /* DDB */
866	critical_exit(savecrit);
867	return (n);
868}
869
870static struct witness *
871enroll(const char *description, struct lock_class *lock_class)
872{
873	struct witness *w;
874
875	if (!witness_watch || witness_dead || panicstr != NULL)
876		return (NULL);
877
878	if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
879		return (NULL);
880	mtx_lock_spin(&w_mtx);
881	STAILQ_FOREACH(w, &w_all, w_list) {
882		if (strcmp(description, w->w_name) == 0) {
883			w->w_refcount++;
884			mtx_unlock_spin(&w_mtx);
885			if (lock_class != w->w_class)
886				panic(
887				"lock (%s) %s does not match earlier (%s) lock",
888				    description, lock_class->lc_name,
889				    w->w_class->lc_name);
890			return (w);
891		}
892	}
893	/*
894	 * This isn't quite right, as witness_cold is still 0 while we
895	 * enroll all the locks initialized before witness_initialize().
896	 */
897	if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
898		mtx_unlock_spin(&w_mtx);
899		panic("spin lock %s not in order list", description);
900	}
901	if ((w = witness_get()) == NULL)
902		return (NULL);
903	w->w_name = description;
904	w->w_class = lock_class;
905	w->w_refcount = 1;
906	STAILQ_INSERT_HEAD(&w_all, w, w_list);
907	if (lock_class->lc_flags & LC_SPINLOCK)
908		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
909	else if (lock_class->lc_flags & LC_SLEEPLOCK)
910		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
911	else {
912		mtx_unlock_spin(&w_mtx);
913		panic("lock class %s is not sleep or spin",
914		    lock_class->lc_name);
915	}
916	mtx_unlock_spin(&w_mtx);
917
918	return (w);
919}
920
921static int
922itismychild(struct witness *parent, struct witness *child)
923{
924	static int recursed;
925	struct witness_child_list_entry **wcl;
926	struct witness_list *list;
927
928	MPASS(child != NULL && parent != NULL);
929	if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
930	    (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
931		panic(
932		"%s: parent (%s) and child (%s) are not the same lock type",
933		    __func__, parent->w_class->lc_name,
934		    child->w_class->lc_name);
935
936	/*
937	 * Insert "child" after "parent"
938	 */
939	wcl = &parent->w_children;
940	while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
941		wcl = &(*wcl)->wcl_next;
942
943	if (*wcl == NULL) {
944		*wcl = witness_child_get();
945		if (*wcl == NULL)
946			return (1);
947	}
948
949	(*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
950
951	/*
952	 * Now prune whole tree.  We look for cases where a lock is now
953	 * both a descendant and a direct child of a given lock.  In that
954	 * case, we want to remove the direct child link from the tree.
955	 */
956	if (recursed)
957		return (0);
958	recursed = 1;
959	if (parent->w_class->lc_flags & LC_SLEEPLOCK)
960		list = &w_sleep;
961	else
962		list = &w_spin;
963	STAILQ_FOREACH(child, list, w_typelist) {
964		STAILQ_FOREACH(parent, list, w_typelist) {
965			if (!isitmychild(parent, child))
966				continue;
967			removechild(parent, child);
968			if (isitmydescendant(parent, child))
969				continue;
970			itismychild(parent, child);
971		}
972	}
973	recursed = 0;
974	witness_levelall();
975	return (0);
976}
977
978static void
979removechild(struct witness *parent, struct witness *child)
980{
981	struct witness_child_list_entry **wcl, *wcl1;
982	int i;
983
984	for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
985		for (i = 0; i < (*wcl)->wcl_count; i++)
986			if ((*wcl)->wcl_children[i] == child)
987				goto found;
988	return;
989found:
990	(*wcl)->wcl_count--;
991	if ((*wcl)->wcl_count > i)
992		(*wcl)->wcl_children[i] =
993		    (*wcl)->wcl_children[(*wcl)->wcl_count];
994	MPASS((*wcl)->wcl_children[i] != NULL);
995
996	if ((*wcl)->wcl_count != 0)
997		return;
998
999	wcl1 = *wcl;
1000	*wcl = wcl1->wcl_next;
1001	witness_child_free(wcl1);
1002}
1003
1004static int
1005isitmychild(struct witness *parent, struct witness *child)
1006{
1007	struct witness_child_list_entry *wcl;
1008	int i;
1009
1010	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1011		for (i = 0; i < wcl->wcl_count; i++) {
1012			if (wcl->wcl_children[i] == child)
1013				return (1);
1014		}
1015	}
1016	return (0);
1017}
1018
1019static int
1020isitmydescendant(struct witness *parent, struct witness *child)
1021{
1022	struct witness_child_list_entry *wcl;
1023	int i, j;
1024
1025	if (isitmychild(parent, child))
1026		return (1);
1027	j = 0;
1028	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1029		MPASS(j < 1000);
1030		for (i = 0; i < wcl->wcl_count; i++) {
1031			if (isitmydescendant(wcl->wcl_children[i], child))
1032				return (1);
1033		}
1034		j++;
1035	}
1036	return (0);
1037}
1038
1039void
1040witness_levelall (void)
1041{
1042	struct witness_list *list;
1043	struct witness *w, *w1;
1044
1045	/*
1046	 * First clear all levels.
1047	 */
1048	STAILQ_FOREACH(w, &w_all, w_list) {
1049		w->w_level = 0;
1050	}
1051
1052	/*
1053	 * Look for locks with no parent and level all their descendants.
1054	 */
1055	STAILQ_FOREACH(w, &w_all, w_list) {
1056		/*
1057		 * This is just an optimization, technically we could get
1058		 * away just walking the all list each time.
1059		 */
1060		if (w->w_class->lc_flags & LC_SLEEPLOCK)
1061			list = &w_sleep;
1062		else
1063			list = &w_spin;
1064		STAILQ_FOREACH(w1, list, w_typelist) {
1065			if (isitmychild(w1, w))
1066				goto skip;
1067		}
1068		witness_leveldescendents(w, 0);
1069	skip:
1070	}
1071}
1072
1073static void
1074witness_leveldescendents(struct witness *parent, int level)
1075{
1076	struct witness_child_list_entry *wcl;
1077	int i;
1078
1079	if (parent->w_level < level)
1080		parent->w_level = level;
1081	level++;
1082	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1083		for (i = 0; i < wcl->wcl_count; i++)
1084			witness_leveldescendents(wcl->wcl_children[i], level);
1085}
1086
1087static void
1088witness_displaydescendants(void(*prnt)(const char *fmt, ...),
1089			   struct witness *parent)
1090{
1091	struct witness_child_list_entry *wcl;
1092	int i, level;
1093
1094	level =  parent->w_level;
1095
1096	prnt("%-2d", level);
1097	for (i = 0; i < level; i++)
1098		prnt(" ");
1099	prnt("%s", parent->w_name);
1100	if (parent->w_file != NULL)
1101		prnt(" -- last acquired @ %s:%d\n", parent->w_file,
1102		    parent->w_line);
1103
1104	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1105		for (i = 0; i < wcl->wcl_count; i++)
1106			    witness_displaydescendants(prnt,
1107				wcl->wcl_children[i]);
1108}
1109
1110static int
1111dup_ok(struct witness *w)
1112{
1113	const char **dup;
1114
1115	for (dup = dup_list; *dup != NULL; dup++)
1116		if (strcmp(w->w_name, *dup) == 0)
1117			return (1);
1118	return (0);
1119}
1120
1121static int
1122blessed(struct witness *w1, struct witness *w2)
1123{
1124	int i;
1125	struct witness_blessed *b;
1126
1127	for (i = 0; i < blessed_count; i++) {
1128		b = &blessed_list[i];
1129		if (strcmp(w1->w_name, b->b_lock1) == 0) {
1130			if (strcmp(w2->w_name, b->b_lock2) == 0)
1131				return (1);
1132			continue;
1133		}
1134		if (strcmp(w1->w_name, b->b_lock2) == 0)
1135			if (strcmp(w2->w_name, b->b_lock1) == 0)
1136				return (1);
1137	}
1138	return (0);
1139}
1140
1141static struct witness *
1142witness_get(void)
1143{
1144	struct witness *w;
1145
1146	if (witness_dead) {
1147		mtx_unlock_spin(&w_mtx);
1148		return (NULL);
1149	}
1150	if (STAILQ_EMPTY(&w_free)) {
1151		witness_dead = 1;
1152		mtx_unlock_spin(&w_mtx);
1153		printf("%s: witness exhausted\n", __func__);
1154		return (NULL);
1155	}
1156	w = STAILQ_FIRST(&w_free);
1157	STAILQ_REMOVE_HEAD(&w_free, w_list);
1158	bzero(w, sizeof(*w));
1159	return (w);
1160}
1161
1162static void
1163witness_free(struct witness *w)
1164{
1165
1166	STAILQ_INSERT_HEAD(&w_free, w, w_list);
1167}
1168
1169static struct witness_child_list_entry *
1170witness_child_get(void)
1171{
1172	struct witness_child_list_entry *wcl;
1173
1174	if (witness_dead) {
1175		mtx_unlock_spin(&w_mtx);
1176		return (NULL);
1177	}
1178	wcl = w_child_free;
1179	if (wcl == NULL) {
1180		witness_dead = 1;
1181		mtx_unlock_spin(&w_mtx);
1182		printf("%s: witness exhausted\n", __func__);
1183		return (NULL);
1184	}
1185	w_child_free = wcl->wcl_next;
1186	bzero(wcl, sizeof(*wcl));
1187	return (wcl);
1188}
1189
1190static void
1191witness_child_free(struct witness_child_list_entry *wcl)
1192{
1193
1194	wcl->wcl_next = w_child_free;
1195	w_child_free = wcl;
1196}
1197
1198static struct lock_list_entry *
1199witness_lock_list_get(void)
1200{
1201	struct lock_list_entry *lle;
1202
1203	if (witness_dead)
1204		return (NULL);
1205	mtx_lock_spin(&w_mtx);
1206	lle = w_lock_list_free;
1207	if (lle == NULL) {
1208		witness_dead = 1;
1209		mtx_unlock_spin(&w_mtx);
1210		printf("%s: witness exhausted\n", __func__);
1211		return (NULL);
1212	}
1213	w_lock_list_free = lle->ll_next;
1214	mtx_unlock_spin(&w_mtx);
1215	bzero(lle, sizeof(*lle));
1216	return (lle);
1217}
1218
1219static void
1220witness_lock_list_free(struct lock_list_entry *lle)
1221{
1222
1223	mtx_lock_spin(&w_mtx);
1224	lle->ll_next = w_lock_list_free;
1225	w_lock_list_free = lle;
1226	mtx_unlock_spin(&w_mtx);
1227}
1228
1229static struct lock_instance *
1230find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1231{
1232	struct lock_list_entry *lle;
1233	struct lock_instance *instance;
1234	int i;
1235
1236	for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1237		for (i = lle->ll_count - 1; i >= 0; i--) {
1238			instance = &lle->ll_children[i];
1239			if (instance->li_lock == lock)
1240				return (instance);
1241		}
1242	return (NULL);
1243}
1244
1245int
1246witness_list_locks(struct lock_list_entry **lock_list)
1247{
1248	struct lock_list_entry *lle;
1249	struct lock_instance *instance;
1250	struct lock_object *lock;
1251	int i, nheld;
1252
1253	nheld = 0;
1254	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1255		for (i = lle->ll_count - 1; i >= 0; i--) {
1256			instance = &lle->ll_children[i];
1257			lock = instance->li_lock;
1258			printf("%s (%s) %s (%p) locked @ %s:%d\n",
1259			    (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1260			    "exclusive" : "shared",
1261			    lock->lo_class->lc_name, lock->lo_name, lock,
1262			    instance->li_file, instance->li_line);
1263			nheld++;
1264		}
1265	return (nheld);
1266}
1267
1268/*
1269 * Calling this on p != curproc is bad unless we are in ddb.
1270 */
1271int
1272witness_list(struct proc *p)
1273{
1274	critical_t savecrit;
1275	int nheld;
1276
1277	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1278#ifdef DDB
1279	KASSERT(p == curproc || db_active,
1280	    ("%s: p != curproc and we aren't in the debugger", __func__));
1281
1282	if (!db_active && witness_dead)
1283		return (0);
1284#else
1285	KASSERT(p == curproc, ("%s: p != curproc", __func__));
1286
1287	if (witness_dead)
1288		return (0);
1289#endif
1290	nheld = witness_list_locks(&p->p_sleeplocks);
1291
1292	/*
1293	 * We only handle spinlocks if p == curproc.  This is somewhat broken
1294	 * if p is currently executing on some other CPU and holds spin locks
1295	 * as we won't display those locks.  If we had a MI way of getting
1296	 * the per-cpu data for a given cpu then we could use p->p_oncpu to
1297	 * get the list of spinlocks for this process and "fix" this.
1298	 */
1299	if (p == curproc) {
1300		/*
1301		 * Preemption bad because we need PCPU_PTR(spinlocks) to not
1302		 * change.
1303		 */
1304		savecrit = critical_enter();
1305		nheld += witness_list_locks(PCPU_PTR(spinlocks));
1306		critical_exit(savecrit);
1307	}
1308
1309	return (nheld);
1310}
1311
1312void
1313witness_save(struct lock_object *lock, const char **filep, int *linep)
1314{
1315	struct lock_instance *instance;
1316
1317	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
1318	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1319		return;
1320
1321	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1322		panic("%s: lock (%s) %s is not a sleep lock", __func__,
1323		    lock->lo_class->lc_name, lock->lo_name);
1324	instance = find_instance(curproc->p_sleeplocks, lock);
1325	if (instance == NULL)
1326		panic("%s: lock (%s) %s not locked", __func__,
1327		    lock->lo_class->lc_name, lock->lo_name);
1328
1329	*filep = instance->li_file;
1330	*linep = instance->li_line;
1331}
1332
1333void
1334witness_restore(struct lock_object *lock, const char *file, int line)
1335{
1336	struct lock_instance *instance;
1337
1338	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
1339	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1340		return;
1341
1342	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0)
1343		panic("%s: lock (%s) %s is not a sleep lock", __func__,
1344		    lock->lo_class->lc_name, lock->lo_name);
1345	instance = find_instance(curproc->p_sleeplocks, lock);
1346	if (instance == NULL)
1347		panic("%s: lock (%s) %s not locked", __func__,
1348		    lock->lo_class->lc_name, lock->lo_name);
1349
1350	lock->lo_witness->w_file = file;
1351	lock->lo_witness->w_line = line;
1352	instance->li_file = file;
1353	instance->li_line = line;
1354}
1355
1356void
1357witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1358{
1359#ifdef INVARIANT_SUPPORT
1360	struct lock_instance *instance;
1361
1362	if (lock->lo_witness == NULL || witness_dead || panicstr != NULL)
1363		return;
1364
1365	if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0)
1366		instance = find_instance(curproc->p_sleeplocks, lock);
1367	else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0)
1368		instance = find_instance(PCPU_GET(spinlocks), lock);
1369	else
1370		panic("Lock (%s) %s is not sleep or spin!",
1371		    lock->lo_class->lc_name, lock->lo_name);
1372	switch (flags) {
1373	case LA_UNLOCKED:
1374		if (instance != NULL)
1375			panic("Lock (%s) %s locked @ %s:%d.",
1376			    lock->lo_class->lc_name, lock->lo_name, file, line);
1377		break;
1378	case LA_LOCKED:
1379	case LA_LOCKED | LA_RECURSED:
1380	case LA_LOCKED | LA_NOTRECURSED:
1381	case LA_SLOCKED:
1382	case LA_SLOCKED | LA_RECURSED:
1383	case LA_SLOCKED | LA_NOTRECURSED:
1384	case LA_XLOCKED:
1385	case LA_XLOCKED | LA_RECURSED:
1386	case LA_XLOCKED | LA_NOTRECURSED:
1387		if (instance == NULL)
1388			panic("Lock (%s) %s not locked @ %s:%d.",
1389			    lock->lo_class->lc_name, lock->lo_name, file, line);
1390		if ((flags & LA_XLOCKED) != 0 &&
1391		    (instance->li_flags & LI_EXCLUSIVE) == 0)
1392			panic("Lock (%s) %s not exclusively locked @ %s:%d.",
1393			    lock->lo_class->lc_name, lock->lo_name, file, line);
1394		if ((flags & LA_SLOCKED) != 0 &&
1395		    (instance->li_flags & LI_EXCLUSIVE) != 0)
1396			panic("Lock (%s) %s exclusively locked @ %s:%d.",
1397			    lock->lo_class->lc_name, lock->lo_name, file, line);
1398		if ((flags & LA_RECURSED) != 0 &&
1399		    (instance->li_flags & LI_RECURSEMASK) == 0)
1400			panic("Lock (%s) %s not recursed @ %s:%d.",
1401			    lock->lo_class->lc_name, lock->lo_name, file, line);
1402		if ((flags & LA_NOTRECURSED) != 0 &&
1403		    (instance->li_flags & LI_RECURSEMASK) != 0)
1404			panic("Lock (%s) %s recursed @ %s:%d.",
1405			    lock->lo_class->lc_name, lock->lo_name, file, line);
1406		break;
1407	default:
1408		panic("Invalid lock assertion at %s:%d.", file, line);
1409
1410	}
1411#endif	/* INVARIANT_SUPPORT */
1412}
1413
1414#ifdef DDB
1415
1416DB_SHOW_COMMAND(locks, db_witness_list)
1417{
1418	struct proc *p;
1419	pid_t pid;
1420
1421	if (have_addr) {
1422		pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1423		    ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1424		    ((addr >> 16) % 16) * 10000;
1425
1426		/* sx_slock(&allproc_lock); */
1427		LIST_FOREACH(p, &allproc, p_list) {
1428			if (p->p_pid == pid)
1429				break;
1430		}
1431		/* sx_sunlock(&allproc_lock); */
1432		if (p == NULL) {
1433			db_printf("pid %d not found\n", pid);
1434			return;
1435		}
1436	} else
1437		p = curproc;
1438
1439	witness_list(p);
1440}
1441
1442DB_SHOW_COMMAND(witness, db_witness_display)
1443{
1444
1445	witness_display(db_printf);
1446}
1447#endif
1448