subr_witness.c revision 75362
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 75362 2001-04-09 22:34:05Z 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#if defined(__i386__) && defined (SMP)
196	{ "com", &lock_class_mtx_spin },
197#endif
198	{ "sio", &lock_class_mtx_spin },
199#ifdef __i386__
200	{ "cy", &lock_class_mtx_spin },
201#endif
202	{ "ng_node", &lock_class_mtx_spin },
203	{ "ng_worklist", &lock_class_mtx_spin },
204	{ "ithread table lock", &lock_class_mtx_spin },
205	{ "ithread list lock", &lock_class_mtx_spin },
206	{ "sched lock", &lock_class_mtx_spin },
207#ifdef __i386__
208	{ "clk", &lock_class_mtx_spin },
209#endif
210	{ "callout", &lock_class_mtx_spin },
211	/*
212	 * leaf locks
213	 */
214#ifdef SMP
215#ifdef __i386__
216	{ "ap boot", &lock_class_mtx_spin },
217	{ "imen", &lock_class_mtx_spin },
218#endif
219	{ "smp rendezvous", &lock_class_mtx_spin },
220#endif
221	{ NULL, NULL },
222	{ NULL, NULL }
223};
224
225static const char *dup_list[] = {
226	"process lock",
227	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	  NULL,				/* mtx_object.lo_file */
248	  0,				/* mtx_object.lo_line */
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	STAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list);
288	mtx_init(&w_mtx, "witness lock", MTX_SPIN | MTX_QUIET | 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		w->w_file = "order list";
300		for (order++; order->w_name != NULL; order++) {
301			w1 = enroll(order->w_name, order->w_class);
302			w1->w_file = "order list";
303			itismychild(w, w1);
304			w = w1;
305		}
306	}
307
308	/* Iterate through all locks and add them to witness. */
309	mtx_lock(&all_mtx);
310	STAILQ_FOREACH(lock, &all_locks, lo_list) {
311		if (lock->lo_flags & LO_WITNESS)
312			lock->lo_witness = enroll(lock->lo_name,
313			    lock->lo_class);
314		else
315			lock->lo_witness = NULL;
316	}
317	mtx_unlock(&all_mtx);
318
319	/* Mark the witness code as being ready for use. */
320	atomic_store_rel_int(&witness_cold, 0);
321
322	mtx_lock(&Giant);
323}
324SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL)
325
326void
327witness_init(struct lock_object *lock)
328{
329	struct lock_class *class;
330
331	class = lock->lo_class;
332	if (lock->lo_flags & LO_INITIALIZED)
333		panic("%s: lock (%s) %s is already initialized!\n", __func__,
334		    class->lc_name, lock->lo_name);
335
336	if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
337	    (class->lc_flags & LC_RECURSABLE) == 0)
338		panic("%s: lock (%s) %s can not be recursable!\n", __func__,
339		    class->lc_name, lock->lo_name);
340
341	if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
342	    (class->lc_flags & LC_SLEEPABLE) == 0)
343		panic("%s: lock (%s) %s can not be sleepable!\n", __func__,
344		    class->lc_name, lock->lo_name);
345
346	mtx_lock(&all_mtx);
347	STAILQ_INSERT_TAIL(&all_locks, lock, lo_list);
348	lock->lo_flags |= LO_INITIALIZED;
349	lock_cur_cnt++;
350	if (lock_cur_cnt > lock_max_cnt)
351		lock_max_cnt = lock_cur_cnt;
352	mtx_unlock(&all_mtx);
353	if (!witness_cold && !witness_dead &&
354	    (lock->lo_flags & LO_WITNESS) != 0)
355		lock->lo_witness = enroll(lock->lo_name, class);
356	else
357		lock->lo_witness = NULL;
358}
359
360void
361witness_destroy(struct lock_object *lock)
362{
363	struct witness *w;
364
365	if (witness_cold)
366		panic("lock (%s) %s destroyed while witness_cold",
367		    lock->lo_class->lc_name, lock->lo_name);
368
369	if ((lock->lo_flags & LO_INITIALIZED) == 0)
370		panic("%s: lock (%s) %s is not initialized!\n", __func__,
371		    lock->lo_class->lc_name, lock->lo_name);
372
373	if (lock->lo_flags & LO_LOCKED)
374		panic("lock (%s) %s destroyed while held",
375		    lock->lo_class->lc_name, lock->lo_name);
376
377	w = lock->lo_witness;
378	if (w != NULL) {
379		mtx_lock_spin(&w_mtx);
380		w->w_refcount--;
381		if (w->w_refcount == 0) {
382			w->w_name = "(dead)";
383			w->w_file = "(dead)";
384			w->w_line = 0;
385		}
386		mtx_unlock_spin(&w_mtx);
387	}
388
389	mtx_lock(&all_mtx);
390	lock_cur_cnt--;
391	STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list);
392	lock->lo_flags &= LO_INITIALIZED;
393	mtx_unlock(&all_mtx);
394}
395
396static void
397witness_display_list(void(*prnt)(const char *fmt, ...),
398		     struct witness_list *list)
399{
400	struct witness *w, *w1;
401	int found;
402
403	STAILQ_FOREACH(w, list, w_typelist) {
404		if (w->w_file == NULL)
405			continue;
406		found = 0;
407		STAILQ_FOREACH(w1, list, w_typelist) {
408			if (isitmychild(w1, w)) {
409				found++;
410				break;
411			}
412		}
413		if (found)
414			continue;
415		/*
416		 * This lock has no anscestors, display its descendants.
417		 */
418		witness_displaydescendants(prnt, w);
419	}
420}
421
422static void
423witness_display(void(*prnt)(const char *fmt, ...))
424{
425	struct witness *w;
426
427	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
428	witness_levelall();
429
430	/*
431	 * First, handle sleep locks which have been acquired at least
432	 * once.
433	 */
434	prnt("Sleep locks:\n");
435	witness_display_list(prnt, &w_sleep);
436
437	/*
438	 * Now do spin locks which have been acquired at least once.
439	 */
440	prnt("\nSpin locks:\n");
441	witness_display_list(prnt, &w_spin);
442
443	/*
444	 * Finally, any locks which have not been acquired yet.
445	 */
446	prnt("\nLocks which were never acquired:\n");
447	STAILQ_FOREACH(w, &w_all, w_list) {
448		if (w->w_file != NULL)
449			continue;
450		prnt("%s\n", w->w_name);
451	}
452}
453
454void
455witness_lock(struct lock_object *lock, int flags, const char *file, int line)
456{
457	struct lock_list_entry **lock_list, *lle;
458	struct lock_object *lock1, *lock2;
459	struct lock_class *class;
460	struct witness *w, *w1;
461	struct proc *p;
462	int i, j;
463#ifdef DDB
464	int go_into_ddb = 0;
465#endif /* DDB */
466
467	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
468	    panicstr)
469		return;
470	w = lock->lo_witness;
471	class = lock->lo_class;
472	p = curproc;
473
474	if ((lock->lo_flags & LO_LOCKED) == 0)
475		panic("%s: lock (%s) %s is not locked @ %s:%d", __func__,
476		    class->lc_name, lock->lo_name, file, line);
477
478	if ((lock->lo_flags & LO_RECURSED) != 0) {
479		if ((lock->lo_flags & LO_RECURSABLE) == 0)
480			panic(
481			"%s: recursed on non-recursive lock (%s) %s @ %s:%d",
482			    __func__, class->lc_name, lock->lo_name, file,
483			    line);
484		return;
485	}
486
487	/*
488	 * We have to hold a spinlock to keep lock_list valid across the check
489	 * in the LC_SLEEPLOCK case.  In the LC_SPINLOCK case, it is already
490	 * protected by the spinlock we are currently performing the witness
491	 * checks on, so it is ok to release the lock after performing this
492	 * check.  All we have to protect is the LC_SLEEPLOCK case when no
493	 * spinlocks are held as we may get preempted during this check and
494	 * lock_list could end up pointing to some other CPU's spinlock list.
495	 */
496	mtx_lock_spin(&w_mtx);
497	lock_list = PCPU_PTR(spinlocks);
498	if (class->lc_flags & LC_SLEEPLOCK) {
499		if (*lock_list != NULL) {
500			mtx_unlock_spin(&w_mtx);
501			panic("blockable sleep lock (%s) %s @ %s:%d",
502			    class->lc_name, lock->lo_name, file, line);
503		}
504		lock_list = &p->p_sleeplocks;
505	}
506	mtx_unlock_spin(&w_mtx);
507
508	if (flags & LOP_TRYLOCK)
509		goto out;
510
511	/*
512	 * Is this the first lock acquired?  If so, then no order checking
513	 * is needed.
514	 */
515	if (*lock_list == NULL)
516		goto out;
517
518	/*
519	 * Check for duplicate locks of the same type.  Note that we only
520	 * have to check for this on the last lock we just acquired.  Any
521	 * other cases will be caught as lock order violations.
522	 */
523	lock1 = (*lock_list)->ll_children[(*lock_list)->ll_count - 1];
524	w1 = lock1->lo_witness;
525	if (w1 == w) {
526		if (w->w_same_squawked || dup_ok(w))
527			goto out;
528		w->w_same_squawked = 1;
529		printf("acquring duplicate lock of same type: \"%s\"\n",
530			lock->lo_name);
531		printf(" 1st @ %s:%d\n", w->w_file, w->w_line);
532		printf(" 2nd @ %s:%d\n", file, line);
533#ifdef DDB
534		go_into_ddb = 1;
535#endif /* DDB */
536		goto out;
537	}
538	MPASS(!mtx_owned(&w_mtx));
539	mtx_lock_spin(&w_mtx);
540	/*
541	 * If we have a known higher number just say ok
542	 */
543	if (witness_watch > 1 && w->w_level > w1->w_level) {
544		mtx_unlock_spin(&w_mtx);
545		goto out;
546	}
547	if (isitmydescendant(w1, w)) {
548		mtx_unlock_spin(&w_mtx);
549		goto out;
550	}
551	for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
552		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
553
554			MPASS(j < WITNESS_COUNT);
555			lock1 = lle->ll_children[i];
556			w1 = lock1->lo_witness;
557
558			/*
559			 * If this lock doesn't undergo witness checking,
560			 * then skip it.
561			 */
562			if (w1 == NULL) {
563				KASSERT((lock1->lo_flags & LO_WITNESS) == 0,
564				    ("lock missing witness structure"));
565				continue;
566			}
567			if (!isitmydescendant(w, w1))
568				continue;
569			/*
570			 * We have a lock order violation, check to see if it
571			 * is allowed or has already been yelled about.
572			 */
573			mtx_unlock_spin(&w_mtx);
574			if (blessed(w, w1))
575				goto out;
576			if (lock1 == &Giant.mtx_object) {
577				if (w1->w_Giant_squawked)
578					goto out;
579				else
580					w1->w_Giant_squawked = 1;
581			} else {
582				if (w1->w_other_squawked)
583					goto out;
584				else
585					w1->w_other_squawked = 1;
586			}
587			/*
588			 * Ok, yell about it.
589			 */
590			printf("lock order reversal\n");
591			/*
592			 * Try to locate an earlier lock with
593			 * witness w in our list.
594			 */
595			do {
596				lock2 = lle->ll_children[i];
597				MPASS(lock2 != NULL);
598				if (lock2->lo_witness == w)
599					break;
600				i--;
601				if (i == 0 && lle->ll_next != NULL) {
602					lle = lle->ll_next;
603					i = lle->ll_count - 1;
604					MPASS(i != 0);
605				}
606			} while (i >= 0);
607			if (i < 0)
608				/*
609				 * We are very likely bogus in this case.
610				 */
611				printf(" 1st %s last acquired @ %s:%d\n",
612				    w->w_name, w->w_file, w->w_line);
613			else
614				printf(" 1st %p %s @ %s:%d\n", lock2,
615				    lock2->lo_name, lock2->lo_file,
616				    lock2->lo_line);
617			printf(" 2nd %p %s @ %s:%d\n",
618			    lock1, lock1->lo_name, lock1->lo_file,
619			    lock1->lo_line);
620			printf(" 3rd %p %s @ %s:%d\n",
621			    lock, lock->lo_name, file, line);
622#ifdef DDB
623			go_into_ddb = 1;
624#endif /* DDB */
625			goto out;
626		}
627	}
628	lock1 = (*lock_list)->ll_children[(*lock_list)->ll_count - 1];
629	if (!itismychild(lock1->lo_witness, w))
630		mtx_unlock_spin(&w_mtx);
631
632out:
633#ifdef DDB
634	if (witness_ddb && go_into_ddb)
635		Debugger("witness_enter");
636#endif /* DDB */
637	w->w_file = file;
638	w->w_line = line;
639	lock->lo_line = line;
640	lock->lo_file = file;
641
642	lle = *lock_list;
643	if (lle == NULL || lle->ll_count == LOCK_CHILDCOUNT) {
644		*lock_list = witness_lock_list_get();
645		if (*lock_list == NULL)
646			return;
647		(*lock_list)->ll_next = lle;
648		lle = *lock_list;
649	}
650	lle->ll_children[lle->ll_count++] = lock;
651}
652
653void
654witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
655{
656	struct lock_list_entry **lock_list, *lle;
657	struct lock_class *class;
658	struct proc *p;
659	int i, j;
660
661	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
662	    panicstr)
663		return;
664	p = curproc;
665	class = lock->lo_class;
666
667	if (lock->lo_flags & LO_RECURSED) {
668		if ((lock->lo_flags & LO_LOCKED) == 0)
669			panic("%s: recursed lock (%s) %s is not locked @ %s:%d",
670			    __func__, class->lc_name, lock->lo_name, file,
671			    line);
672		return;
673	}
674
675	/*
676	 * We don't need to protect this PCPU_GET() here against preemption
677	 * because if we hold any spinlocks then we are already protected,
678	 * and if we don't we will get NULL if we hold no spinlocks even if
679	 * we switch CPU's while reading it.
680	 */
681	if (class->lc_flags & LC_SLEEPLOCK) {
682		if ((flags & LOP_NOSWITCH) == 0 && PCPU_GET(spinlocks) != NULL)
683			panic("switchable sleep unlock (%s) %s @ %s:%d",
684			    class->lc_name, lock->lo_name, file, line);
685		lock_list = &p->p_sleeplocks;
686	} else
687		lock_list = PCPU_PTR(spinlocks);
688
689	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
690		for (i = 0; i < (*lock_list)->ll_count; i++)
691			if ((*lock_list)->ll_children[i] == lock) {
692				(*lock_list)->ll_count--;
693				for (j = i; j < (*lock_list)->ll_count; j++)
694					(*lock_list)->ll_children[j] =
695					    (*lock_list)->ll_children[j + 1];
696				if ((*lock_list)->ll_count == 0) {
697					lle = *lock_list;
698					*lock_list = lle->ll_next;
699					witness_lock_list_free(lle);
700				}
701				return;
702			}
703}
704
705/*
706 * Warn if any held locks are not sleepable.  Note that Giant and the lock
707 * passed in are both special cases since they are both released during the
708 * sleep process and aren't actually held while the process is asleep.
709 */
710int
711witness_sleep(int check_only, struct lock_object *lock, const char *file,
712	      int line)
713{
714	struct lock_list_entry **lock_list, *lle;
715	struct lock_object *lock1;
716	struct proc *p;
717	critical_t savecrit;
718	int i, n;
719
720	if (witness_dead || panicstr)
721		return (0);
722	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
723	n = 0;
724	/*
725	 * Preemption bad because we need PCPU_PTR(spinlocks) to not change.
726	 */
727	savecrit = critical_enter();
728	p = curproc;
729	lock_list = &p->p_sleeplocks;
730again:
731	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
732		for (i = lle->ll_count - 1; i >= 0; i--) {
733			lock1 = lle->ll_children[i];
734			if (lock1 == lock || lock1 == &Giant.mtx_object ||
735			    (lock1->lo_flags & LO_SLEEPABLE))
736				continue;
737			n++;
738			printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
739			    file, line, check_only ? "could sleep" : "sleeping",
740			    lock1->lo_name, lock1->lo_file, lock1->lo_line);
741		}
742	if (lock_list == &p->p_sleeplocks) {
743		lock_list = PCPU_PTR(spinlocks);
744		goto again;
745	}
746#ifdef DDB
747	if (witness_ddb && n)
748		Debugger("witness_sleep");
749#endif /* DDB */
750	critical_exit(savecrit);
751	return (n);
752}
753
754static struct witness *
755enroll(const char *description, struct lock_class *lock_class)
756{
757	struct witness *w;
758
759	if (!witness_watch)
760		return (NULL);
761
762	if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
763		return (NULL);
764	mtx_lock_spin(&w_mtx);
765	STAILQ_FOREACH(w, &w_all, w_list) {
766		if (strcmp(description, w->w_name) == 0) {
767			w->w_refcount++;
768			mtx_unlock_spin(&w_mtx);
769			if (lock_class != w->w_class)
770				panic(
771				"lock (%s) %s does not match earlier (%s) lock",
772				    description, lock_class->lc_name,
773				    w->w_class->lc_name);
774			return (w);
775		}
776	}
777	/*
778	 * This isn't quite right, as witness_cold is still 0 while we
779	 * enroll all the locks initialized before witness_initialize().
780	 */
781	if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold)
782		panic("spin lock %s not in order list", description);
783	if ((w = witness_get()) == NULL)
784		return (NULL);
785	w->w_name = description;
786	w->w_class = lock_class;
787	w->w_refcount = 1;
788	STAILQ_INSERT_HEAD(&w_all, w, w_list);
789	if (lock_class->lc_flags & LC_SPINLOCK)
790		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
791	else if (lock_class->lc_flags & LC_SLEEPLOCK)
792		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
793	else
794		panic("lock class %s is not sleep or spin",
795		    lock_class->lc_name);
796	mtx_unlock_spin(&w_mtx);
797
798	return (w);
799}
800
801static int
802itismychild(struct witness *parent, struct witness *child)
803{
804	static int recursed;
805	struct witness_child_list_entry **wcl;
806	struct witness_list *list;
807
808	MPASS(child != NULL && parent != NULL);
809	if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
810	    (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
811		panic(
812		"%s: parent (%s) and child (%s) are not the same lock type",
813		    __func__, parent->w_class->lc_name,
814		    child->w_class->lc_name);
815
816	/*
817	 * Insert "child" after "parent"
818	 */
819	wcl = &parent->w_children;
820	while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
821		wcl = &(*wcl)->wcl_next;
822
823	if (*wcl == NULL) {
824		*wcl = witness_child_get();
825		if (*wcl == NULL)
826			return (1);
827	}
828
829	(*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
830
831	/*
832	 * Now prune whole tree.  We look for cases where a lock is now
833	 * both a descendant and a direct child of a given lock.  In that
834	 * case, we want to remove the direct child link from the tree.
835	 */
836	if (recursed)
837		return (0);
838	recursed = 1;
839	if (parent->w_class->lc_flags & LC_SLEEPLOCK)
840		list = &w_sleep;
841	else
842		list = &w_spin;
843	STAILQ_FOREACH(child, list, w_typelist) {
844		STAILQ_FOREACH(parent, list, w_typelist) {
845			if (!isitmychild(parent, child))
846				continue;
847			removechild(parent, child);
848			if (isitmydescendant(parent, child))
849				continue;
850			itismychild(parent, child);
851		}
852	}
853	recursed = 0;
854	witness_levelall();
855	return (0);
856}
857
858static void
859removechild(struct witness *parent, struct witness *child)
860{
861	struct witness_child_list_entry **wcl, *wcl1;
862	int i;
863
864	for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
865		for (i = 0; i < (*wcl)->wcl_count; i++)
866			if ((*wcl)->wcl_children[i] == child)
867				goto found;
868	return;
869found:
870	(*wcl)->wcl_count--;
871	if ((*wcl)->wcl_count > i)
872		(*wcl)->wcl_children[i] =
873		    (*wcl)->wcl_children[(*wcl)->wcl_count];
874	MPASS((*wcl)->wcl_children[i] != NULL);
875
876	if ((*wcl)->wcl_count != 0)
877		return;
878
879	wcl1 = *wcl;
880	*wcl = wcl1->wcl_next;
881	witness_child_free(wcl1);
882}
883
884static int
885isitmychild(struct witness *parent, struct witness *child)
886{
887	struct witness_child_list_entry *wcl;
888	int i;
889
890	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
891		for (i = 0; i < wcl->wcl_count; i++) {
892			if (wcl->wcl_children[i] == child)
893				return (1);
894		}
895	}
896	return (0);
897}
898
899static int
900isitmydescendant(struct witness *parent, struct witness *child)
901{
902	struct witness_child_list_entry *wcl;
903	int i, j;
904
905	if (isitmychild(parent, child))
906		return (1);
907	j = 0;
908	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
909		MPASS(j < 1000);
910		for (i = 0; i < wcl->wcl_count; i++) {
911			if (isitmydescendant(wcl->wcl_children[i], child))
912				return (1);
913		}
914		j++;
915	}
916	return (0);
917}
918
919void
920witness_levelall (void)
921{
922	struct witness_list *list;
923	struct witness *w, *w1;
924
925	/*
926	 * First clear all levels.
927	 */
928	STAILQ_FOREACH(w, &w_all, w_list) {
929		w->w_level = 0;
930	}
931
932	/*
933	 * Look for locks with no parent and level all their descendants.
934	 */
935	STAILQ_FOREACH(w, &w_all, w_list) {
936		/*
937		 * This is just an optimization, technically we could get
938		 * away just walking the all list each time.
939		 */
940		if (w->w_class->lc_flags & LC_SLEEPLOCK)
941			list = &w_sleep;
942		else
943			list = &w_spin;
944		STAILQ_FOREACH(w1, list, w_typelist) {
945			if (isitmychild(w1, w))
946				goto skip;
947		}
948		witness_leveldescendents(w, 0);
949	skip:
950	}
951}
952
953static void
954witness_leveldescendents(struct witness *parent, int level)
955{
956	struct witness_child_list_entry *wcl;
957	int i;
958
959	if (parent->w_level < level)
960		parent->w_level = level;
961	level++;
962	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
963		for (i = 0; i < wcl->wcl_count; i++)
964			witness_leveldescendents(wcl->wcl_children[i], level);
965}
966
967static void
968witness_displaydescendants(void(*prnt)(const char *fmt, ...),
969			   struct witness *parent)
970{
971	struct witness_child_list_entry *wcl;
972	int i, level;
973
974	level =  parent->w_level;
975
976	prnt("%-2d", level);
977	for (i = 0; i < level; i++)
978		prnt(" ");
979	prnt("%s", parent->w_name);
980	if (parent->w_file != NULL)
981		prnt(" -- last acquired @ %s:%d\n", parent->w_file,
982		    parent->w_line);
983
984	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
985		for (i = 0; i < wcl->wcl_count; i++)
986			    witness_displaydescendants(prnt,
987				wcl->wcl_children[i]);
988}
989
990static int
991dup_ok(struct witness *w)
992{
993	const char **dup;
994
995	for (dup = dup_list; *dup != NULL; dup++)
996		if (strcmp(w->w_name, *dup) == 0)
997			return (1);
998	return (0);
999}
1000
1001static int
1002blessed(struct witness *w1, struct witness *w2)
1003{
1004	int i;
1005	struct witness_blessed *b;
1006
1007	for (i = 0; i < blessed_count; i++) {
1008		b = &blessed_list[i];
1009		if (strcmp(w1->w_name, b->b_lock1) == 0) {
1010			if (strcmp(w2->w_name, b->b_lock2) == 0)
1011				return (1);
1012			continue;
1013		}
1014		if (strcmp(w1->w_name, b->b_lock2) == 0)
1015			if (strcmp(w2->w_name, b->b_lock1) == 0)
1016				return (1);
1017	}
1018	return (0);
1019}
1020
1021static struct witness *
1022witness_get(void)
1023{
1024	struct witness *w;
1025
1026	if (STAILQ_EMPTY(&w_free)) {
1027		witness_dead = 1;
1028		mtx_unlock_spin(&w_mtx);
1029		printf("%s: witness exhausted\n", __func__);
1030		return (NULL);
1031	}
1032	w = STAILQ_FIRST(&w_free);
1033	STAILQ_REMOVE_HEAD(&w_free, w_list);
1034	bzero(w, sizeof(*w));
1035	return (w);
1036}
1037
1038static void
1039witness_free(struct witness *w)
1040{
1041
1042	STAILQ_INSERT_HEAD(&w_free, w, w_list);
1043}
1044
1045static struct witness_child_list_entry *
1046witness_child_get(void)
1047{
1048	struct witness_child_list_entry *wcl;
1049
1050	wcl = w_child_free;
1051	if (wcl == NULL) {
1052		witness_dead = 1;
1053		mtx_unlock_spin(&w_mtx);
1054		printf("%s: witness exhausted\n", __func__);
1055		return (NULL);
1056	}
1057	w_child_free = wcl->wcl_next;
1058	bzero(wcl, sizeof(*wcl));
1059	return (wcl);
1060}
1061
1062static void
1063witness_child_free(struct witness_child_list_entry *wcl)
1064{
1065
1066	wcl->wcl_next = w_child_free;
1067	w_child_free = wcl;
1068}
1069
1070static struct lock_list_entry *
1071witness_lock_list_get(void)
1072{
1073	struct lock_list_entry *lle;
1074
1075	mtx_lock_spin(&w_mtx);
1076	lle = w_lock_list_free;
1077	if (lle == NULL) {
1078		witness_dead = 1;
1079		mtx_unlock_spin(&w_mtx);
1080		printf("%s: witness exhausted\n", __func__);
1081		return (NULL);
1082	}
1083	w_lock_list_free = lle->ll_next;
1084	mtx_unlock_spin(&w_mtx);
1085	bzero(lle, sizeof(*lle));
1086	return (lle);
1087}
1088
1089static void
1090witness_lock_list_free(struct lock_list_entry *lle)
1091{
1092
1093	mtx_lock_spin(&w_mtx);
1094	lle->ll_next = w_lock_list_free;
1095	w_lock_list_free = lle;
1096	mtx_unlock_spin(&w_mtx);
1097}
1098
1099int
1100witness_list_locks(struct lock_list_entry **lock_list)
1101{
1102	struct lock_list_entry *lle;
1103	struct lock_object *lock;
1104	int i, nheld;
1105
1106	nheld = 0;
1107	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1108		for (i = lle->ll_count - 1; i >= 0; i--) {
1109			lock = lle->ll_children[i];
1110			printf("\t(%s) %s (%p) locked at %s:%d\n",
1111			    lock->lo_class->lc_name, lock->lo_name, lock,
1112			    lock->lo_file, lock->lo_line);
1113			nheld++;
1114		}
1115	return (nheld);
1116}
1117
1118/*
1119 * Calling this on p != curproc is bad unless we are in ddb.
1120 */
1121int
1122witness_list(struct proc *p)
1123{
1124	critical_t savecrit;
1125	int nheld;
1126
1127	KASSERT(p == curproc || db_active,
1128	    ("%s: p != curproc and we aren't in the debugger", __func__));
1129	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1130
1131	nheld = witness_list_locks(&p->p_sleeplocks);
1132
1133	/*
1134	 * We only handle spinlocks if p == curproc.  This is somewhat broken
1135	 * if p is currently executing on some other CPU and holds spin locks
1136	 * as we won't display those locks.  If we had a MI way of getting
1137	 * the per-cpu data for a given cpu then we could use p->p_oncpu to
1138	 * get the list of spinlocks for this process and "fix" this.
1139	 */
1140	if (p == curproc) {
1141		/*
1142		 * Preemption bad because we need PCPU_PTR(spinlocks) to not
1143		 * change.
1144		 */
1145		savecrit = critical_enter();
1146		nheld += witness_list_locks(PCPU_PTR(spinlocks));
1147		critical_exit(savecrit);
1148	}
1149
1150	return (nheld);
1151}
1152
1153void
1154witness_save(struct lock_object *lock, const char **filep, int *linep)
1155{
1156
1157	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
1158	if (lock->lo_witness == NULL)
1159		return;
1160
1161	*filep = lock->lo_file;
1162	*linep = lock->lo_line;
1163}
1164
1165void
1166witness_restore(struct lock_object *lock, const char *file, int line)
1167{
1168
1169	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
1170	if (lock->lo_witness == NULL)
1171		return;
1172
1173	lock->lo_witness->w_file = file;
1174	lock->lo_witness->w_line = line;
1175	lock->lo_file = file;
1176	lock->lo_line = line;
1177}
1178
1179#ifdef DDB
1180
1181DB_SHOW_COMMAND(locks, db_witness_list)
1182{
1183	struct proc *p;
1184	pid_t pid;
1185
1186	if (have_addr) {
1187		pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1188		    ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1189		    ((addr >> 16) % 16) * 10000;
1190
1191		/* sx_slock(&allproc_lock); */
1192		LIST_FOREACH(p, &allproc, p_list) {
1193			if (p->p_pid == pid)
1194				break;
1195		}
1196		/* sx_sunlock(&allproc_lock); */
1197		if (p == NULL) {
1198			db_printf("pid %d not found\n", pid);
1199			return;
1200		}
1201	} else
1202		p = curproc;
1203
1204	witness_list(p);
1205}
1206
1207DB_SHOW_COMMAND(witness, db_witness_display)
1208{
1209
1210	witness_display(db_printf);
1211}
1212#endif
1213