subr_witness.c revision 75569
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 75569 2001-04-17 03:35:38Z jhb $
31 */
32
33/*
34 * Implementation of the `witness' lock verifier.  Originally implemented for
35 * mutexes in BSD/OS.  Extended to handle generic lock objects and lock
36 * classes in FreeBSD.
37 */
38
39/*
40 *	Main Entry: witness
41 *	Pronunciation: 'wit-n&s
42 *	Function: noun
43 *	Etymology: Middle English witnesse, from Old English witnes knowledge,
44 *	    testimony, witness, from 2wit
45 *	Date: before 12th century
46 *	1 : attestation of a fact or event : TESTIMONY
47 *	2 : one that gives evidence; specifically : one who testifies in
48 *	    a cause or before a judicial tribunal
49 *	3 : one asked to be present at a transaction so as to be able to
50 *	    testify to its having taken place
51 *	4 : one who has personal knowledge of something
52 *	5 a : something serving as evidence or proof : SIGN
53 *	  b : public affirmation by word or example of usually
54 *	      religious faith or conviction <the heroic witness to divine
55 *	      life -- Pilot>
56 *	6 capitalized : a member of the Jehovah's Witnesses
57 */
58
59#include "opt_ddb.h"
60#include "opt_witness.h"
61
62#include <sys/param.h>
63#include <sys/bus.h>
64#include <sys/kernel.h>
65#include <sys/ktr.h>
66#include <sys/lock.h>
67#include <sys/malloc.h>
68#include <sys/mutex.h>
69#include <sys/proc.h>
70#include <sys/sysctl.h>
71#include <sys/systm.h>
72
73#include <ddb/ddb.h>
74
75#define WITNESS_COUNT 200
76#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
77/*
78 * XXX: This is somewhat bogus, as we assume here that at most 1024 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, ...));
142
143MALLOC_DEFINE(M_WITNESS, "witness", "witness structure");
144
145static int witness_watch;
146TUNABLE_INT_DECL("debug.witness_watch", 1, witness_watch);
147SYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, "");
148
149#ifdef DDB
150/*
151 * When DDB is enabled and witness_ddb is set to 1, it will cause the system to
152 * drop into kdebug() when:
153 *	- a lock heirarchy violation occurs
154 *	- locks are held when going to sleep.
155 */
156int	witness_ddb;
157#ifdef WITNESS_DDB
158TUNABLE_INT_DECL("debug.witness_ddb", 1, witness_ddb);
159#else
160TUNABLE_INT_DECL("debug.witness_ddb", 0, witness_ddb);
161#endif
162SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, "");
163#endif /* DDB */
164
165int	witness_skipspin;
166#ifdef WITNESS_SKIPSPIN
167TUNABLE_INT_DECL("debug.witness_skipspin", 1, witness_skipspin);
168#else
169TUNABLE_INT_DECL("debug.witness_skipspin", 0, witness_skipspin);
170#endif
171SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0,
172    "");
173
174static struct mtx w_mtx;
175static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
176static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
177static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
178static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
179static struct witness_child_list_entry *w_child_free = NULL;
180static struct lock_list_entry *w_lock_list_free = NULL;
181static int witness_dead;	/* fatal error, probably no memory */
182
183static struct witness w_data[WITNESS_COUNT];
184static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
185static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
186
187static struct witness_order_list_entry order_lists[] = {
188	{ "Giant", &lock_class_mtx_sleep },
189	{ "proctree", &lock_class_sx },
190	{ "allproc", &lock_class_sx },
191	{ "process lock", &lock_class_mtx_sleep },
192	{ "uidinfo hash", &lock_class_mtx_sleep },
193	{ "uidinfo struct", &lock_class_mtx_sleep },
194	{ NULL, NULL },
195	/*
196	 * spin locks
197	 */
198#if defined(__i386__) && defined (SMP)
199	{ "com", &lock_class_mtx_spin },
200#endif
201	{ "sio", &lock_class_mtx_spin },
202#ifdef __i386__
203	{ "cy", &lock_class_mtx_spin },
204#endif
205	{ "ng_node", &lock_class_mtx_spin },
206	{ "ng_worklist", &lock_class_mtx_spin },
207	{ "ithread table lock", &lock_class_mtx_spin },
208	{ "ithread list lock", &lock_class_mtx_spin },
209	{ "sched lock", &lock_class_mtx_spin },
210#ifdef __i386__
211	{ "clk", &lock_class_mtx_spin },
212#endif
213	{ "callout", &lock_class_mtx_spin },
214	/*
215	 * leaf locks
216	 */
217#ifdef SMP
218	{ "ap boot", &lock_class_mtx_spin },
219#ifdef __i386__
220	{ "imen", &lock_class_mtx_spin },
221#endif
222	{ "smp rendezvous", &lock_class_mtx_spin },
223#endif
224	{ NULL, NULL },
225	{ NULL, NULL }
226};
227
228static const char *dup_list[] = {
229	"process lock",
230	NULL
231};
232
233/*
234 * Pairs of locks which have been blessed
235 * Don't complain about order problems with blessed locks
236 */
237static struct witness_blessed blessed_list[] = {
238};
239static int blessed_count =
240	sizeof(blessed_list) / sizeof(struct witness_blessed);
241
242/*
243 * List of all locks in the system.
244 */
245STAILQ_HEAD(, lock_object) all_locks = STAILQ_HEAD_INITIALIZER(all_locks);
246
247static struct mtx all_mtx = {
248	{ &lock_class_mtx_sleep,	/* mtx_object.lo_class */
249	  "All locks list",		/* mtx_object.lo_name */
250	  NULL,				/* mtx_object.lo_file */
251	  0,				/* mtx_object.lo_line */
252	  LO_INITIALIZED,		/* mtx_object.lo_flags */
253	  { NULL },			/* mtx_object.lo_list */
254	  NULL },			/* mtx_object.lo_witness */
255	MTX_UNOWNED, 0,			/* mtx_lock, mtx_recurse */
256	0,				/* mtx_savecrit */
257	TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked),
258	{ NULL, NULL }			/* mtx_contested */
259};
260
261/*
262 * This global is set to 0 once it becomes safe to use the witness code.
263 */
264static int witness_cold = 1;
265
266/*
267 * Global variables for book keeping.
268 */
269static int lock_cur_cnt;
270static int lock_max_cnt;
271
272/*
273 * The WITNESS-enabled diagnostic code.
274 */
275static void
276witness_initialize(void *dummy __unused)
277{
278	struct lock_object *lock;
279	struct witness_order_list_entry *order;
280	struct witness *w, *w1;
281	int i;
282
283	/*
284	 * We have to release Giant before initializing its witness
285	 * structure so that WITNESS doesn't get confused.
286	 */
287	mtx_unlock(&Giant);
288	mtx_assert(&Giant, MA_NOTOWNED);
289
290	STAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
291	mtx_init(&w_mtx, "witness lock", MTX_SPIN | MTX_QUIET | MTX_NOWITNESS);
292	for (i = 0; i < WITNESS_COUNT; i++)
293		witness_free(&w_data[i]);
294	for (i = 0; i < WITNESS_CHILDCOUNT; i++)
295		witness_child_free(&w_childdata[i]);
296	for (i = 0; i < LOCK_CHILDCOUNT; i++)
297		witness_lock_list_free(&w_locklistdata[i]);
298
299	/* First add in all the specified order lists. */
300	for (order = order_lists; order->w_name != NULL; order++) {
301		w = enroll(order->w_name, order->w_class);
302		if (w == NULL)
303			continue;
304		w->w_file = "order list";
305		for (order++; order->w_name != NULL; order++) {
306			w1 = enroll(order->w_name, order->w_class);
307			if (w1 == NULL)
308				continue;
309			w1->w_file = "order list";
310			itismychild(w, w1);
311			w = w1;
312		}
313	}
314
315	/* Iterate through all locks and add them to witness. */
316	mtx_lock(&all_mtx);
317	STAILQ_FOREACH(lock, &all_locks, lo_list) {
318		if (lock->lo_flags & LO_WITNESS)
319			lock->lo_witness = enroll(lock->lo_name,
320			    lock->lo_class);
321		else
322			lock->lo_witness = NULL;
323	}
324	mtx_unlock(&all_mtx);
325
326	/* Mark the witness code as being ready for use. */
327	atomic_store_rel_int(&witness_cold, 0);
328
329	mtx_lock(&Giant);
330}
331SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
332
333void
334witness_init(struct lock_object *lock)
335{
336	struct lock_class *class;
337
338	class = lock->lo_class;
339	if (lock->lo_flags & LO_INITIALIZED)
340		panic("%s: lock (%s) %s is already initialized!\n", __func__,
341		    class->lc_name, lock->lo_name);
342
343	if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
344	    (class->lc_flags & LC_RECURSABLE) == 0)
345		panic("%s: lock (%s) %s can not be recursable!\n", __func__,
346		    class->lc_name, lock->lo_name);
347
348	if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
349	    (class->lc_flags & LC_SLEEPABLE) == 0)
350		panic("%s: lock (%s) %s can not be sleepable!\n", __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 &&
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
376	if ((lock->lo_flags & LO_INITIALIZED) == 0)
377		panic("%s: lock (%s) %s is not initialized!\n", __func__,
378		    lock->lo_class->lc_name, lock->lo_name);
379
380	if (lock->lo_flags & LO_LOCKED)
381		panic("lock (%s) %s destroyed while held",
382		    lock->lo_class->lc_name, lock->lo_name);
383
384	w = lock->lo_witness;
385	if (w != NULL) {
386		mtx_lock_spin(&w_mtx);
387		w->w_refcount--;
388		if (w->w_refcount == 0) {
389			w->w_name = "(dead)";
390			w->w_file = "(dead)";
391			w->w_line = 0;
392		}
393		mtx_unlock_spin(&w_mtx);
394	}
395
396	mtx_lock(&all_mtx);
397	lock_cur_cnt--;
398	STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list);
399	lock->lo_flags &= LO_INITIALIZED;
400	mtx_unlock(&all_mtx);
401}
402
403static void
404witness_display_list(void(*prnt)(const char *fmt, ...),
405		     struct witness_list *list)
406{
407	struct witness *w, *w1;
408	int found;
409
410	STAILQ_FOREACH(w, list, w_typelist) {
411		if (w->w_file == NULL)
412			continue;
413		found = 0;
414		STAILQ_FOREACH(w1, list, w_typelist) {
415			if (isitmychild(w1, w)) {
416				found++;
417				break;
418			}
419		}
420		if (found)
421			continue;
422		/*
423		 * This lock has no anscestors, display its descendants.
424		 */
425		witness_displaydescendants(prnt, w);
426	}
427}
428
429static void
430witness_display(void(*prnt)(const char *fmt, ...))
431{
432	struct witness *w;
433
434	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
435	witness_levelall();
436
437	/*
438	 * First, handle sleep locks which have been acquired at least
439	 * once.
440	 */
441	prnt("Sleep locks:\n");
442	witness_display_list(prnt, &w_sleep);
443
444	/*
445	 * Now do spin locks which have been acquired at least once.
446	 */
447	prnt("\nSpin locks:\n");
448	witness_display_list(prnt, &w_spin);
449
450	/*
451	 * Finally, any locks which have not been acquired yet.
452	 */
453	prnt("\nLocks which were never acquired:\n");
454	STAILQ_FOREACH(w, &w_all, w_list) {
455		if (w->w_file != NULL)
456			continue;
457		prnt("%s\n", w->w_name);
458	}
459}
460
461void
462witness_lock(struct lock_object *lock, int flags, const char *file, int line)
463{
464	struct lock_list_entry **lock_list, *lle;
465	struct lock_object *lock1, *lock2;
466	struct lock_class *class;
467	struct witness *w, *w1;
468	struct proc *p;
469	int i, j;
470#ifdef DDB
471	int go_into_ddb = 0;
472#endif /* DDB */
473
474	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
475	    panicstr)
476		return;
477	w = lock->lo_witness;
478	class = lock->lo_class;
479	p = curproc;
480
481	if ((lock->lo_flags & LO_LOCKED) == 0)
482		panic("%s: lock (%s) %s is not locked @ %s:%d", __func__,
483		    class->lc_name, lock->lo_name, file, line);
484
485	if ((lock->lo_flags & LO_RECURSED) != 0) {
486		if ((lock->lo_flags & LO_RECURSABLE) == 0)
487			panic(
488			"%s: recursed on non-recursive lock (%s) %s @ %s:%d",
489			    __func__, class->lc_name, lock->lo_name, file,
490			    line);
491		return;
492	}
493
494	/*
495	 * We have to hold a spinlock to keep lock_list valid across the check
496	 * in the LC_SLEEPLOCK case.  In the LC_SPINLOCK case, it is already
497	 * protected by the spinlock we are currently performing the witness
498	 * checks on, so it is ok to release the lock after performing this
499	 * check.  All we have to protect is the LC_SLEEPLOCK case when no
500	 * spinlocks are held as we may get preempted during this check and
501	 * lock_list could end up pointing to some other CPU's spinlock list.
502	 */
503	mtx_lock_spin(&w_mtx);
504	lock_list = PCPU_PTR(spinlocks);
505	if (class->lc_flags & LC_SLEEPLOCK) {
506		if (*lock_list != NULL) {
507			mtx_unlock_spin(&w_mtx);
508			panic("blockable sleep lock (%s) %s @ %s:%d",
509			    class->lc_name, lock->lo_name, file, line);
510		}
511		lock_list = &p->p_sleeplocks;
512	}
513	mtx_unlock_spin(&w_mtx);
514
515	if (flags & LOP_TRYLOCK)
516		goto out;
517
518	/*
519	 * Is this the first lock acquired?  If so, then no order checking
520	 * is needed.
521	 */
522	if (*lock_list == NULL)
523		goto out;
524
525	/*
526	 * Check for duplicate locks of the same type.  Note that we only
527	 * have to check for this on the last lock we just acquired.  Any
528	 * other cases will be caught as lock order violations.
529	 */
530	lock1 = (*lock_list)->ll_children[(*lock_list)->ll_count - 1];
531	w1 = lock1->lo_witness;
532	if (w1 == w) {
533		if (w->w_same_squawked || dup_ok(w))
534			goto out;
535		w->w_same_squawked = 1;
536		printf("acquring duplicate lock of same type: \"%s\"\n",
537			lock->lo_name);
538		printf(" 1st @ %s:%d\n", w->w_file, w->w_line);
539		printf(" 2nd @ %s:%d\n", file, line);
540#ifdef DDB
541		go_into_ddb = 1;
542#endif /* DDB */
543		goto out;
544	}
545	MPASS(!mtx_owned(&w_mtx));
546	mtx_lock_spin(&w_mtx);
547	/*
548	 * If we have a known higher number just say ok
549	 */
550	if (witness_watch > 1 && w->w_level > w1->w_level) {
551		mtx_unlock_spin(&w_mtx);
552		goto out;
553	}
554	if (isitmydescendant(w1, w)) {
555		mtx_unlock_spin(&w_mtx);
556		goto out;
557	}
558	for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
559		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
560
561			MPASS(j < WITNESS_COUNT);
562			lock1 = lle->ll_children[i];
563			w1 = lock1->lo_witness;
564
565			/*
566			 * If this lock doesn't undergo witness checking,
567			 * then skip it.
568			 */
569			if (w1 == NULL) {
570				KASSERT((lock1->lo_flags & LO_WITNESS) == 0,
571				    ("lock missing witness structure"));
572				continue;
573			}
574			if (!isitmydescendant(w, w1))
575				continue;
576			/*
577			 * We have a lock order violation, check to see if it
578			 * is allowed or has already been yelled about.
579			 */
580			mtx_unlock_spin(&w_mtx);
581			if (blessed(w, w1))
582				goto out;
583			if (lock1 == &Giant.mtx_object) {
584				if (w1->w_Giant_squawked)
585					goto out;
586				else
587					w1->w_Giant_squawked = 1;
588			} else {
589				if (w1->w_other_squawked)
590					goto out;
591				else
592					w1->w_other_squawked = 1;
593			}
594			/*
595			 * Ok, yell about it.
596			 */
597			printf("lock order reversal\n");
598			/*
599			 * Try to locate an earlier lock with
600			 * witness w in our list.
601			 */
602			do {
603				lock2 = lle->ll_children[i];
604				MPASS(lock2 != NULL);
605				if (lock2->lo_witness == w)
606					break;
607				i--;
608				if (i == 0 && lle->ll_next != NULL) {
609					lle = lle->ll_next;
610					i = lle->ll_count - 1;
611					MPASS(i != 0);
612				}
613			} while (i >= 0);
614			if (i < 0)
615				/*
616				 * We are very likely bogus in this case.
617				 */
618				printf(" 1st %s last acquired @ %s:%d\n",
619				    w->w_name, w->w_file, w->w_line);
620			else
621				printf(" 1st %p %s @ %s:%d\n", lock2,
622				    lock2->lo_name, lock2->lo_file,
623				    lock2->lo_line);
624			printf(" 2nd %p %s @ %s:%d\n",
625			    lock1, lock1->lo_name, lock1->lo_file,
626			    lock1->lo_line);
627			printf(" 3rd %p %s @ %s:%d\n",
628			    lock, lock->lo_name, file, line);
629#ifdef DDB
630			go_into_ddb = 1;
631#endif /* DDB */
632			goto out;
633		}
634	}
635	lock1 = (*lock_list)->ll_children[(*lock_list)->ll_count - 1];
636	if (!itismychild(lock1->lo_witness, w))
637		mtx_unlock_spin(&w_mtx);
638
639out:
640#ifdef DDB
641	if (witness_ddb && go_into_ddb)
642		Debugger("witness_enter");
643#endif /* DDB */
644	w->w_file = file;
645	w->w_line = line;
646	lock->lo_line = line;
647	lock->lo_file = file;
648
649	lle = *lock_list;
650	if (lle == NULL || lle->ll_count == LOCK_CHILDCOUNT) {
651		*lock_list = witness_lock_list_get();
652		if (*lock_list == NULL)
653			return;
654		(*lock_list)->ll_next = lle;
655		lle = *lock_list;
656	}
657	lle->ll_children[lle->ll_count++] = lock;
658}
659
660void
661witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
662{
663	struct lock_list_entry **lock_list, *lle;
664	struct lock_class *class;
665	struct proc *p;
666	int i, j;
667
668	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
669	    panicstr)
670		return;
671	p = curproc;
672	class = lock->lo_class;
673
674	if (lock->lo_flags & LO_RECURSED) {
675		if ((lock->lo_flags & LO_LOCKED) == 0)
676			panic("%s: recursed lock (%s) %s is not locked @ %s:%d",
677			    __func__, class->lc_name, lock->lo_name, file,
678			    line);
679		return;
680	}
681
682	/*
683	 * We don't need to protect this PCPU_GET() here against preemption
684	 * because if we hold any spinlocks then we are already protected,
685	 * and if we don't we will get NULL if we hold no spinlocks even if
686	 * we switch CPU's while reading it.
687	 */
688	if (class->lc_flags & LC_SLEEPLOCK) {
689		if ((flags & LOP_NOSWITCH) == 0 && PCPU_GET(spinlocks) != NULL)
690			panic("switchable sleep unlock (%s) %s @ %s:%d",
691			    class->lc_name, lock->lo_name, file, line);
692		lock_list = &p->p_sleeplocks;
693	} else
694		lock_list = PCPU_PTR(spinlocks);
695
696	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
697		for (i = 0; i < (*lock_list)->ll_count; i++)
698			if ((*lock_list)->ll_children[i] == lock) {
699				(*lock_list)->ll_count--;
700				for (j = i; j < (*lock_list)->ll_count; j++)
701					(*lock_list)->ll_children[j] =
702					    (*lock_list)->ll_children[j + 1];
703				if ((*lock_list)->ll_count == 0) {
704					lle = *lock_list;
705					*lock_list = lle->ll_next;
706					witness_lock_list_free(lle);
707				}
708				return;
709			}
710}
711
712/*
713 * Warn if any held locks are not sleepable.  Note that Giant and the lock
714 * passed in are both special cases since they are both released during the
715 * sleep process and aren't actually held while the process is asleep.
716 */
717int
718witness_sleep(int check_only, struct lock_object *lock, const char *file,
719	      int line)
720{
721	struct lock_list_entry **lock_list, *lle;
722	struct lock_object *lock1;
723	struct proc *p;
724	critical_t savecrit;
725	int i, n;
726
727	if (witness_dead || panicstr)
728		return (0);
729	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
730	n = 0;
731	/*
732	 * Preemption bad because we need PCPU_PTR(spinlocks) to not change.
733	 */
734	savecrit = critical_enter();
735	p = curproc;
736	lock_list = &p->p_sleeplocks;
737again:
738	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
739		for (i = lle->ll_count - 1; i >= 0; i--) {
740			lock1 = lle->ll_children[i];
741			if (lock1 == lock || lock1 == &Giant.mtx_object ||
742			    (lock1->lo_flags & LO_SLEEPABLE))
743				continue;
744			n++;
745			printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
746			    file, line, check_only ? "could sleep" : "sleeping",
747			    lock1->lo_name, lock1->lo_file, lock1->lo_line);
748		}
749	if (lock_list == &p->p_sleeplocks) {
750		lock_list = PCPU_PTR(spinlocks);
751		goto again;
752	}
753#ifdef DDB
754	if (witness_ddb && n)
755		Debugger("witness_sleep");
756#endif /* DDB */
757	critical_exit(savecrit);
758	return (n);
759}
760
761static struct witness *
762enroll(const char *description, struct lock_class *lock_class)
763{
764	struct witness *w;
765
766	if (!witness_watch)
767		return (NULL);
768
769	if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
770		return (NULL);
771	mtx_lock_spin(&w_mtx);
772	STAILQ_FOREACH(w, &w_all, w_list) {
773		if (strcmp(description, w->w_name) == 0) {
774			w->w_refcount++;
775			mtx_unlock_spin(&w_mtx);
776			if (lock_class != w->w_class)
777				panic(
778				"lock (%s) %s does not match earlier (%s) lock",
779				    description, lock_class->lc_name,
780				    w->w_class->lc_name);
781			return (w);
782		}
783	}
784	/*
785	 * This isn't quite right, as witness_cold is still 0 while we
786	 * enroll all the locks initialized before witness_initialize().
787	 */
788	if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
789		mtx_unlock_spin(&w_mtx);
790		panic("spin lock %s not in order list", description);
791	}
792	if ((w = witness_get()) == NULL)
793		return (NULL);
794	w->w_name = description;
795	w->w_class = lock_class;
796	w->w_refcount = 1;
797	STAILQ_INSERT_HEAD(&w_all, w, w_list);
798	if (lock_class->lc_flags & LC_SPINLOCK)
799		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
800	else if (lock_class->lc_flags & LC_SLEEPLOCK)
801		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
802	else {
803		mtx_unlock_spin(&w_mtx);
804		panic("lock class %s is not sleep or spin",
805		    lock_class->lc_name);
806	}
807	mtx_unlock_spin(&w_mtx);
808
809	return (w);
810}
811
812static int
813itismychild(struct witness *parent, struct witness *child)
814{
815	static int recursed;
816	struct witness_child_list_entry **wcl;
817	struct witness_list *list;
818
819	MPASS(child != NULL && parent != NULL);
820	if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
821	    (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
822		panic(
823		"%s: parent (%s) and child (%s) are not the same lock type",
824		    __func__, parent->w_class->lc_name,
825		    child->w_class->lc_name);
826
827	/*
828	 * Insert "child" after "parent"
829	 */
830	wcl = &parent->w_children;
831	while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
832		wcl = &(*wcl)->wcl_next;
833
834	if (*wcl == NULL) {
835		*wcl = witness_child_get();
836		if (*wcl == NULL)
837			return (1);
838	}
839
840	(*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
841
842	/*
843	 * Now prune whole tree.  We look for cases where a lock is now
844	 * both a descendant and a direct child of a given lock.  In that
845	 * case, we want to remove the direct child link from the tree.
846	 */
847	if (recursed)
848		return (0);
849	recursed = 1;
850	if (parent->w_class->lc_flags & LC_SLEEPLOCK)
851		list = &w_sleep;
852	else
853		list = &w_spin;
854	STAILQ_FOREACH(child, list, w_typelist) {
855		STAILQ_FOREACH(parent, list, w_typelist) {
856			if (!isitmychild(parent, child))
857				continue;
858			removechild(parent, child);
859			if (isitmydescendant(parent, child))
860				continue;
861			itismychild(parent, child);
862		}
863	}
864	recursed = 0;
865	witness_levelall();
866	return (0);
867}
868
869static void
870removechild(struct witness *parent, struct witness *child)
871{
872	struct witness_child_list_entry **wcl, *wcl1;
873	int i;
874
875	for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
876		for (i = 0; i < (*wcl)->wcl_count; i++)
877			if ((*wcl)->wcl_children[i] == child)
878				goto found;
879	return;
880found:
881	(*wcl)->wcl_count--;
882	if ((*wcl)->wcl_count > i)
883		(*wcl)->wcl_children[i] =
884		    (*wcl)->wcl_children[(*wcl)->wcl_count];
885	MPASS((*wcl)->wcl_children[i] != NULL);
886
887	if ((*wcl)->wcl_count != 0)
888		return;
889
890	wcl1 = *wcl;
891	*wcl = wcl1->wcl_next;
892	witness_child_free(wcl1);
893}
894
895static int
896isitmychild(struct witness *parent, struct witness *child)
897{
898	struct witness_child_list_entry *wcl;
899	int i;
900
901	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
902		for (i = 0; i < wcl->wcl_count; i++) {
903			if (wcl->wcl_children[i] == child)
904				return (1);
905		}
906	}
907	return (0);
908}
909
910static int
911isitmydescendant(struct witness *parent, struct witness *child)
912{
913	struct witness_child_list_entry *wcl;
914	int i, j;
915
916	if (isitmychild(parent, child))
917		return (1);
918	j = 0;
919	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
920		MPASS(j < 1000);
921		for (i = 0; i < wcl->wcl_count; i++) {
922			if (isitmydescendant(wcl->wcl_children[i], child))
923				return (1);
924		}
925		j++;
926	}
927	return (0);
928}
929
930void
931witness_levelall (void)
932{
933	struct witness_list *list;
934	struct witness *w, *w1;
935
936	/*
937	 * First clear all levels.
938	 */
939	STAILQ_FOREACH(w, &w_all, w_list) {
940		w->w_level = 0;
941	}
942
943	/*
944	 * Look for locks with no parent and level all their descendants.
945	 */
946	STAILQ_FOREACH(w, &w_all, w_list) {
947		/*
948		 * This is just an optimization, technically we could get
949		 * away just walking the all list each time.
950		 */
951		if (w->w_class->lc_flags & LC_SLEEPLOCK)
952			list = &w_sleep;
953		else
954			list = &w_spin;
955		STAILQ_FOREACH(w1, list, w_typelist) {
956			if (isitmychild(w1, w))
957				goto skip;
958		}
959		witness_leveldescendents(w, 0);
960	skip:
961	}
962}
963
964static void
965witness_leveldescendents(struct witness *parent, int level)
966{
967	struct witness_child_list_entry *wcl;
968	int i;
969
970	if (parent->w_level < level)
971		parent->w_level = level;
972	level++;
973	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
974		for (i = 0; i < wcl->wcl_count; i++)
975			witness_leveldescendents(wcl->wcl_children[i], level);
976}
977
978static void
979witness_displaydescendants(void(*prnt)(const char *fmt, ...),
980			   struct witness *parent)
981{
982	struct witness_child_list_entry *wcl;
983	int i, level;
984
985	level =  parent->w_level;
986
987	prnt("%-2d", level);
988	for (i = 0; i < level; i++)
989		prnt(" ");
990	prnt("%s", parent->w_name);
991	if (parent->w_file != NULL)
992		prnt(" -- last acquired @ %s:%d\n", parent->w_file,
993		    parent->w_line);
994
995	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
996		for (i = 0; i < wcl->wcl_count; i++)
997			    witness_displaydescendants(prnt,
998				wcl->wcl_children[i]);
999}
1000
1001static int
1002dup_ok(struct witness *w)
1003{
1004	const char **dup;
1005
1006	for (dup = dup_list; *dup != NULL; dup++)
1007		if (strcmp(w->w_name, *dup) == 0)
1008			return (1);
1009	return (0);
1010}
1011
1012static int
1013blessed(struct witness *w1, struct witness *w2)
1014{
1015	int i;
1016	struct witness_blessed *b;
1017
1018	for (i = 0; i < blessed_count; i++) {
1019		b = &blessed_list[i];
1020		if (strcmp(w1->w_name, b->b_lock1) == 0) {
1021			if (strcmp(w2->w_name, b->b_lock2) == 0)
1022				return (1);
1023			continue;
1024		}
1025		if (strcmp(w1->w_name, b->b_lock2) == 0)
1026			if (strcmp(w2->w_name, b->b_lock1) == 0)
1027				return (1);
1028	}
1029	return (0);
1030}
1031
1032static struct witness *
1033witness_get(void)
1034{
1035	struct witness *w;
1036
1037	if (STAILQ_EMPTY(&w_free)) {
1038		witness_dead = 1;
1039		mtx_unlock_spin(&w_mtx);
1040		printf("%s: witness exhausted\n", __func__);
1041		return (NULL);
1042	}
1043	w = STAILQ_FIRST(&w_free);
1044	STAILQ_REMOVE_HEAD(&w_free, w_list);
1045	bzero(w, sizeof(*w));
1046	return (w);
1047}
1048
1049static void
1050witness_free(struct witness *w)
1051{
1052
1053	STAILQ_INSERT_HEAD(&w_free, w, w_list);
1054}
1055
1056static struct witness_child_list_entry *
1057witness_child_get(void)
1058{
1059	struct witness_child_list_entry *wcl;
1060
1061	wcl = w_child_free;
1062	if (wcl == NULL) {
1063		witness_dead = 1;
1064		mtx_unlock_spin(&w_mtx);
1065		printf("%s: witness exhausted\n", __func__);
1066		return (NULL);
1067	}
1068	w_child_free = wcl->wcl_next;
1069	bzero(wcl, sizeof(*wcl));
1070	return (wcl);
1071}
1072
1073static void
1074witness_child_free(struct witness_child_list_entry *wcl)
1075{
1076
1077	wcl->wcl_next = w_child_free;
1078	w_child_free = wcl;
1079}
1080
1081static struct lock_list_entry *
1082witness_lock_list_get(void)
1083{
1084	struct lock_list_entry *lle;
1085
1086	mtx_lock_spin(&w_mtx);
1087	lle = w_lock_list_free;
1088	if (lle == NULL) {
1089		witness_dead = 1;
1090		mtx_unlock_spin(&w_mtx);
1091		printf("%s: witness exhausted\n", __func__);
1092		return (NULL);
1093	}
1094	w_lock_list_free = lle->ll_next;
1095	mtx_unlock_spin(&w_mtx);
1096	bzero(lle, sizeof(*lle));
1097	return (lle);
1098}
1099
1100static void
1101witness_lock_list_free(struct lock_list_entry *lle)
1102{
1103
1104	mtx_lock_spin(&w_mtx);
1105	lle->ll_next = w_lock_list_free;
1106	w_lock_list_free = lle;
1107	mtx_unlock_spin(&w_mtx);
1108}
1109
1110int
1111witness_list_locks(struct lock_list_entry **lock_list)
1112{
1113	struct lock_list_entry *lle;
1114	struct lock_object *lock;
1115	int i, nheld;
1116
1117	nheld = 0;
1118	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1119		for (i = lle->ll_count - 1; i >= 0; i--) {
1120			lock = lle->ll_children[i];
1121			printf("\t(%s) %s (%p) locked at %s:%d\n",
1122			    lock->lo_class->lc_name, lock->lo_name, lock,
1123			    lock->lo_file, lock->lo_line);
1124			nheld++;
1125		}
1126	return (nheld);
1127}
1128
1129/*
1130 * Calling this on p != curproc is bad unless we are in ddb.
1131 */
1132int
1133witness_list(struct proc *p)
1134{
1135	critical_t savecrit;
1136	int nheld;
1137
1138	KASSERT(p == curproc || db_active,
1139	    ("%s: p != curproc and we aren't in the debugger", __func__));
1140	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1141
1142	nheld = witness_list_locks(&p->p_sleeplocks);
1143
1144	/*
1145	 * We only handle spinlocks if p == curproc.  This is somewhat broken
1146	 * if p is currently executing on some other CPU and holds spin locks
1147	 * as we won't display those locks.  If we had a MI way of getting
1148	 * the per-cpu data for a given cpu then we could use p->p_oncpu to
1149	 * get the list of spinlocks for this process and "fix" this.
1150	 */
1151	if (p == curproc) {
1152		/*
1153		 * Preemption bad because we need PCPU_PTR(spinlocks) to not
1154		 * change.
1155		 */
1156		savecrit = critical_enter();
1157		nheld += witness_list_locks(PCPU_PTR(spinlocks));
1158		critical_exit(savecrit);
1159	}
1160
1161	return (nheld);
1162}
1163
1164void
1165witness_save(struct lock_object *lock, const char **filep, int *linep)
1166{
1167
1168	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
1169	if (lock->lo_witness == NULL)
1170		return;
1171
1172	*filep = lock->lo_file;
1173	*linep = lock->lo_line;
1174}
1175
1176void
1177witness_restore(struct lock_object *lock, const char *file, int line)
1178{
1179
1180	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
1181	if (lock->lo_witness == NULL)
1182		return;
1183
1184	lock->lo_witness->w_file = file;
1185	lock->lo_witness->w_line = line;
1186	lock->lo_file = file;
1187	lock->lo_line = line;
1188}
1189
1190#ifdef DDB
1191
1192DB_SHOW_COMMAND(locks, db_witness_list)
1193{
1194	struct proc *p;
1195	pid_t pid;
1196
1197	if (have_addr) {
1198		pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1199		    ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1200		    ((addr >> 16) % 16) * 10000;
1201
1202		/* sx_slock(&allproc_lock); */
1203		LIST_FOREACH(p, &allproc, p_list) {
1204			if (p->p_pid == pid)
1205				break;
1206		}
1207		/* sx_sunlock(&allproc_lock); */
1208		if (p == NULL) {
1209			db_printf("pid %d not found\n", pid);
1210			return;
1211		}
1212	} else
1213		p = curproc;
1214
1215	witness_list(p);
1216}
1217
1218DB_SHOW_COMMAND(witness, db_witness_display)
1219{
1220
1221	witness_display(db_printf);
1222}
1223#endif
1224