subr_witness.c revision 76272
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 76272 2001-05-04 17:15:16Z 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;
148TUNABLE_INT_DECL("debug.witness_watch", 1, 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 */
158int	witness_ddb;
159#ifdef WITNESS_DDB
160TUNABLE_INT_DECL("debug.witness_ddb", 1, witness_ddb);
161#else
162TUNABLE_INT_DECL("debug.witness_ddb", 0, witness_ddb);
163#endif
164SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, "");
165#endif /* DDB */
166
167int	witness_skipspin;
168#ifdef WITNESS_SKIPSPIN
169TUNABLE_INT_DECL("debug.witness_skipspin", 1, witness_skipspin);
170#else
171TUNABLE_INT_DECL("debug.witness_skipspin", 0, witness_skipspin);
172#endif
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	{ "ithread list lock", &lock_class_mtx_spin },
211	{ "sched lock", &lock_class_mtx_spin },
212	{ "clk", &lock_class_mtx_spin },
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	  LO_INITIALIZED,		/* mtx_object.lo_flags */
251	  { NULL },			/* mtx_object.lo_list */
252	  NULL },			/* mtx_object.lo_witness */
253	MTX_UNOWNED, 0,			/* mtx_lock, mtx_recurse */
254	0,				/* mtx_savecrit */
255	TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked),
256	{ NULL, NULL }			/* mtx_contested */
257};
258
259/*
260 * This global is set to 0 once it becomes safe to use the witness code.
261 */
262static int witness_cold = 1;
263
264/*
265 * Global variables for book keeping.
266 */
267static int lock_cur_cnt;
268static int lock_max_cnt;
269
270/*
271 * The WITNESS-enabled diagnostic code.
272 */
273static void
274witness_initialize(void *dummy __unused)
275{
276	struct lock_object *lock;
277	struct witness_order_list_entry *order;
278	struct witness *w, *w1;
279	int i;
280
281	/*
282	 * We have to release Giant before initializing its witness
283	 * structure so that WITNESS doesn't get confused.
284	 */
285	mtx_unlock(&Giant);
286	mtx_assert(&Giant, MA_NOTOWNED);
287
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 &&
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			w->w_name = "(dead)";
385			w->w_file = "(dead)";
386			w->w_line = 0;
387		}
388		mtx_unlock_spin(&w_mtx);
389	}
390
391	mtx_lock(&all_mtx);
392	lock_cur_cnt--;
393	STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list);
394	lock->lo_flags &= LO_INITIALIZED;
395	mtx_unlock(&all_mtx);
396}
397
398static void
399witness_display_list(void(*prnt)(const char *fmt, ...),
400		     struct witness_list *list)
401{
402	struct witness *w, *w1;
403	int found;
404
405	STAILQ_FOREACH(w, list, w_typelist) {
406		if (w->w_file == NULL)
407			continue;
408		found = 0;
409		STAILQ_FOREACH(w1, list, w_typelist) {
410			if (isitmychild(w1, w)) {
411				found++;
412				break;
413			}
414		}
415		if (found)
416			continue;
417		/*
418		 * This lock has no anscestors, display its descendants.
419		 */
420		witness_displaydescendants(prnt, w);
421	}
422}
423
424static void
425witness_display(void(*prnt)(const char *fmt, ...))
426{
427	struct witness *w;
428
429	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
430	witness_levelall();
431
432	/*
433	 * First, handle sleep locks which have been acquired at least
434	 * once.
435	 */
436	prnt("Sleep locks:\n");
437	witness_display_list(prnt, &w_sleep);
438
439	/*
440	 * Now do spin locks which have been acquired at least once.
441	 */
442	prnt("\nSpin locks:\n");
443	witness_display_list(prnt, &w_spin);
444
445	/*
446	 * Finally, any locks which have not been acquired yet.
447	 */
448	prnt("\nLocks which were never acquired:\n");
449	STAILQ_FOREACH(w, &w_all, w_list) {
450		if (w->w_file != NULL)
451			continue;
452		prnt("%s\n", w->w_name);
453	}
454}
455
456void
457witness_lock(struct lock_object *lock, int flags, const char *file, int line)
458{
459	struct lock_list_entry **lock_list, *lle;
460	struct lock_instance *lock1, *lock2;
461	struct lock_class *class;
462	struct witness *w, *w1;
463	struct proc *p;
464	int i, j;
465#ifdef DDB
466	int go_into_ddb = 0;
467#endif /* DDB */
468
469	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
470	    panicstr)
471		return;
472	w = lock->lo_witness;
473	class = lock->lo_class;
474	p = curproc;
475
476	/*
477	 * We have to hold a spinlock to keep lock_list valid across the check
478	 * in the LC_SLEEPLOCK case.  In the LC_SPINLOCK case, it is already
479	 * protected by the spinlock we are currently performing the witness
480	 * checks on, so it is ok to release the lock after performing this
481	 * check.  All we have to protect is the LC_SLEEPLOCK case when no
482	 * spinlocks are held as we may get preempted during this check and
483	 * lock_list could end up pointing to some other CPU's spinlock list.
484	 */
485	mtx_lock_spin(&w_mtx);
486	lock_list = PCPU_PTR(spinlocks);
487	if (class->lc_flags & LC_SLEEPLOCK) {
488		if (*lock_list != NULL) {
489			mtx_unlock_spin(&w_mtx);
490			panic("blockable sleep lock (%s) %s @ %s:%d",
491			    class->lc_name, lock->lo_name, file, line);
492		}
493		lock_list = &p->p_sleeplocks;
494	}
495	mtx_unlock_spin(&w_mtx);
496
497	if (flags & LOP_TRYLOCK)
498		goto out;
499
500	/*
501	 * Is this the first lock acquired?  If so, then no order checking
502	 * is needed.
503	 */
504	if (*lock_list == NULL)
505		goto out;
506
507	/*
508	 * Check to see if we are recursing on a lock we already own.
509	 */
510	lock1 = find_instance(*lock_list, lock);
511	if (lock1 != NULL) {
512		if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
513		    (flags & LOP_EXCLUSIVE) == 0) {
514			printf("shared lock of (%s) %s @ %s:%d\n",
515			    class->lc_name, lock->lo_name, file, line);
516			printf("while exclusively locked from %s:%d\n",
517			    lock1->li_file, lock1->li_line);
518			panic("share->excl");
519		}
520		if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
521		    (flags & LOP_EXCLUSIVE) != 0) {
522			printf("exclusive lock of (%s) %s @ %s:%d\n",
523			    class->lc_name, lock->lo_name, file, line);
524			printf("while share locked from %s:%d\n",
525			    lock1->li_file, lock1->li_line);
526			panic("excl->share");
527		}
528		lock1->li_flags++;
529		if ((lock->lo_flags & LO_RECURSABLE) == 0) {
530			printf(
531			"recursed on non-recursive lock (%s) %s @ %s:%d\n",
532			    class->lc_name, lock->lo_name, file, line);
533			printf("first acquired @ %s:%d\n", lock1->li_file,
534			    lock1->li_line);
535			panic("recurse");
536		}
537		lock1->li_file = file;
538		lock1->li_line = line;
539		return;
540	}
541
542	/*
543	 * Check for duplicate locks of the same type.  Note that we only
544	 * have to check for this on the last lock we just acquired.  Any
545	 * other cases will be caught as lock order violations.
546	 */
547	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
548	w1 = lock1->li_lock->lo_witness;
549	if (w1 == w) {
550		if (w->w_same_squawked || dup_ok(w))
551			goto out;
552		w->w_same_squawked = 1;
553		printf("acquiring duplicate lock of same type: \"%s\"\n",
554			lock->lo_name);
555		printf(" 1st @ %s:%d\n", lock1->li_file, lock1->li_line);
556		printf(" 2nd @ %s:%d\n", file, line);
557#ifdef DDB
558		go_into_ddb = 1;
559#endif /* DDB */
560		goto out;
561	}
562	MPASS(!mtx_owned(&w_mtx));
563	mtx_lock_spin(&w_mtx);
564	/*
565	 * If we have a known higher number just say ok
566	 */
567	if (witness_watch > 1 && w->w_level > w1->w_level) {
568		mtx_unlock_spin(&w_mtx);
569		goto out;
570	}
571	if (isitmydescendant(w1, w)) {
572		mtx_unlock_spin(&w_mtx);
573		goto out;
574	}
575	for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
576		for (i = lle->ll_count - 1; i >= 0; i--, j++) {
577
578			MPASS(j < WITNESS_COUNT);
579			lock1 = &lle->ll_children[i];
580			w1 = lock1->li_lock->lo_witness;
581
582			/*
583			 * If this lock doesn't undergo witness checking,
584			 * then skip it.
585			 */
586			if (w1 == NULL) {
587				KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
588				    ("lock missing witness structure"));
589				continue;
590			}
591			/*
592			 * If we are locking Giant and we slept with this
593			 * lock, then skip it.
594			 */
595			if ((lock1->li_flags & LI_SLEPT) != 0 &&
596			    lock == &Giant.mtx_object)
597				continue;
598			if (!isitmydescendant(w, w1))
599				continue;
600			/*
601			 * We have a lock order violation, check to see if it
602			 * is allowed or has already been yelled about.
603			 */
604			mtx_unlock_spin(&w_mtx);
605			if (blessed(w, w1))
606				goto out;
607			if (lock1->li_lock == &Giant.mtx_object) {
608				if (w1->w_Giant_squawked)
609					goto out;
610				else
611					w1->w_Giant_squawked = 1;
612			} else {
613				if (w1->w_other_squawked)
614					goto out;
615				else
616					w1->w_other_squawked = 1;
617			}
618			/*
619			 * Ok, yell about it.
620			 */
621			printf("lock order reversal\n");
622			/*
623			 * Try to locate an earlier lock with
624			 * witness w in our list.
625			 */
626			do {
627				lock2 = &lle->ll_children[i];
628				MPASS(lock2->li_lock != NULL);
629				if (lock2->li_lock->lo_witness == w)
630					break;
631				i--;
632				if (i == 0 && lle->ll_next != NULL) {
633					lle = lle->ll_next;
634					i = lle->ll_count - 1;
635					MPASS(i != 0);
636				}
637			} while (i >= 0);
638			if (i < 0) {
639				printf(" 1st %p %s @ %s:%d\n", lock1->li_lock,
640				    lock1->li_lock->lo_name, lock1->li_file,
641				    lock1->li_line);
642				printf(" 2nd %p %s @ %s:%d\n", lock,
643				    lock->lo_name, file, line);
644			} else {
645				printf(" 1st %p %s @ %s:%d\n", lock2->li_lock,
646				    lock2->li_lock->lo_name, lock2->li_file,
647				    lock2->li_line);
648				printf(" 2nd %p %s @ %s:%d\n", lock1->li_lock,
649				    lock1->li_lock->lo_name, lock1->li_file,
650				    lock1->li_line);
651				printf(" 3rd %p %s @ %s:%d\n", lock,
652				    lock->lo_name, file, line);
653			}
654#ifdef DDB
655			go_into_ddb = 1;
656#endif /* DDB */
657			goto out;
658		}
659	}
660	lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
661	if (!itismychild(lock1->li_lock->lo_witness, w))
662		mtx_unlock_spin(&w_mtx);
663
664out:
665#ifdef DDB
666	if (witness_ddb && go_into_ddb)
667		Debugger(__func__);
668#endif /* DDB */
669	w->w_file = file;
670	w->w_line = line;
671
672	lle = *lock_list;
673	if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
674		*lock_list = witness_lock_list_get();
675		if (*lock_list == NULL)
676			return;
677		(*lock_list)->ll_next = lle;
678		lle = *lock_list;
679	}
680	lock1 = &lle->ll_children[lle->ll_count++];
681	lock1->li_lock = lock;
682	lock1->li_line = line;
683	lock1->li_file = file;
684	if ((flags & LOP_EXCLUSIVE) != 0)
685		lock1->li_flags = LI_EXCLUSIVE;
686	else
687		lock1->li_flags = 0;
688}
689
690void
691witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
692{
693	struct lock_list_entry **lock_list, *lle;
694	struct lock_instance *instance;
695	struct lock_class *class;
696	struct proc *p;
697	int i, j;
698
699	if (witness_cold || witness_dead || lock->lo_witness == NULL ||
700	    panicstr)
701		return;
702	p = curproc;
703	class = lock->lo_class;
704	if (class->lc_flags & LC_SLEEPLOCK)
705		lock_list = &p->p_sleeplocks;
706	else
707		lock_list = PCPU_PTR(spinlocks);
708	for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
709		for (i = 0; i < (*lock_list)->ll_count; i++) {
710			instance = &(*lock_list)->ll_children[i];
711			if (instance->li_lock == lock) {
712				if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
713				    (flags & LOP_EXCLUSIVE) == 0) {
714					printf(
715					"shared unlock of (%s) %s @ %s:%d\n",
716					    class->lc_name, lock->lo_name,
717					    file, line);
718					printf(
719					"while exclusively locked from %s:%d\n",
720					    instance->li_file,
721					    instance->li_line);
722					panic("excl->ushare");
723				}
724				if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
725				    (flags & LOP_EXCLUSIVE) != 0) {
726					printf(
727					"exclusive unlock of (%s) %s @ %s:%d\n",
728					    class->lc_name, lock->lo_name,
729					    file, line);
730					printf(
731					"while share locked from %s:%d\n",
732					    instance->li_file,
733					    instance->li_line);
734					panic("share->uexcl");
735				}
736				/* If we are recursed, unrecurse. */
737				if ((instance->li_flags & LI_RECURSEMASK) > 0) {
738					instance->li_flags--;
739					goto out;
740				}
741				(*lock_list)->ll_count--;
742				for (j = i; j < (*lock_list)->ll_count; j++)
743					(*lock_list)->ll_children[j] =
744					    (*lock_list)->ll_children[j + 1];
745				if ((*lock_list)->ll_count == 0) {
746					lle = *lock_list;
747					*lock_list = lle->ll_next;
748					witness_lock_list_free(lle);
749				}
750				goto out;
751			}
752		}
753	panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
754	    file, line);
755out:
756	/*
757	 * We don't need to protect this PCPU_GET() here against preemption
758	 * because if we hold any spinlocks then we are already protected,
759	 * and if we don't we will get NULL if we hold no spinlocks even if
760	 * we switch CPU's while reading it.
761	 */
762	if (class->lc_flags & LC_SLEEPLOCK) {
763		if ((flags & LOP_NOSWITCH) == 0 && PCPU_GET(spinlocks) != NULL)
764			panic("switchable sleep unlock (%s) %s @ %s:%d",
765			    class->lc_name, lock->lo_name, file, line);
766	}
767}
768
769/*
770 * Warn if any held locks are not sleepable.  Note that Giant and the lock
771 * passed in are both special cases since they are both released during the
772 * sleep process and aren't actually held while the process is asleep.
773 */
774int
775witness_sleep(int check_only, struct lock_object *lock, const char *file,
776	      int line)
777{
778	struct lock_list_entry **lock_list, *lle;
779	struct lock_instance *lock1;
780	struct proc *p;
781	critical_t savecrit;
782	int i, n;
783
784	if (witness_dead || panicstr)
785		return (0);
786	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
787	n = 0;
788	/*
789	 * Preemption bad because we need PCPU_PTR(spinlocks) to not change.
790	 */
791	savecrit = critical_enter();
792	p = curproc;
793	lock_list = &p->p_sleeplocks;
794again:
795	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
796		for (i = lle->ll_count - 1; i >= 0; i--) {
797			lock1 = &lle->ll_children[i];
798			if (lock1->li_lock == lock ||
799			    lock1->li_lock == &Giant.mtx_object)
800				continue;
801			if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) {
802				if (check_only == 0)
803					lock1->li_flags |= LI_SLEPT;
804				continue;
805			}
806			n++;
807			printf("%s:%d: %s with \"%s\" locked from %s:%d\n",
808			    file, line, check_only ? "could sleep" : "sleeping",
809			    lock1->li_lock->lo_name, lock1->li_file,
810			    lock1->li_line);
811		}
812	if (lock_list == &p->p_sleeplocks) {
813		lock_list = PCPU_PTR(spinlocks);
814		goto again;
815	}
816#ifdef DDB
817	if (witness_ddb && n)
818		Debugger(__func__);
819#endif /* DDB */
820	critical_exit(savecrit);
821	return (n);
822}
823
824static struct witness *
825enroll(const char *description, struct lock_class *lock_class)
826{
827	struct witness *w;
828
829	if (!witness_watch)
830		return (NULL);
831
832	if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
833		return (NULL);
834	mtx_lock_spin(&w_mtx);
835	STAILQ_FOREACH(w, &w_all, w_list) {
836		if (strcmp(description, w->w_name) == 0) {
837			w->w_refcount++;
838			mtx_unlock_spin(&w_mtx);
839			if (lock_class != w->w_class)
840				panic(
841				"lock (%s) %s does not match earlier (%s) lock",
842				    description, lock_class->lc_name,
843				    w->w_class->lc_name);
844			return (w);
845		}
846	}
847	/*
848	 * This isn't quite right, as witness_cold is still 0 while we
849	 * enroll all the locks initialized before witness_initialize().
850	 */
851	if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) {
852		mtx_unlock_spin(&w_mtx);
853		panic("spin lock %s not in order list", description);
854	}
855	if ((w = witness_get()) == NULL)
856		return (NULL);
857	w->w_name = description;
858	w->w_class = lock_class;
859	w->w_refcount = 1;
860	STAILQ_INSERT_HEAD(&w_all, w, w_list);
861	if (lock_class->lc_flags & LC_SPINLOCK)
862		STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
863	else if (lock_class->lc_flags & LC_SLEEPLOCK)
864		STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
865	else {
866		mtx_unlock_spin(&w_mtx);
867		panic("lock class %s is not sleep or spin",
868		    lock_class->lc_name);
869	}
870	mtx_unlock_spin(&w_mtx);
871
872	return (w);
873}
874
875static int
876itismychild(struct witness *parent, struct witness *child)
877{
878	static int recursed;
879	struct witness_child_list_entry **wcl;
880	struct witness_list *list;
881
882	MPASS(child != NULL && parent != NULL);
883	if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
884	    (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
885		panic(
886		"%s: parent (%s) and child (%s) are not the same lock type",
887		    __func__, parent->w_class->lc_name,
888		    child->w_class->lc_name);
889
890	/*
891	 * Insert "child" after "parent"
892	 */
893	wcl = &parent->w_children;
894	while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
895		wcl = &(*wcl)->wcl_next;
896
897	if (*wcl == NULL) {
898		*wcl = witness_child_get();
899		if (*wcl == NULL)
900			return (1);
901	}
902
903	(*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
904
905	/*
906	 * Now prune whole tree.  We look for cases where a lock is now
907	 * both a descendant and a direct child of a given lock.  In that
908	 * case, we want to remove the direct child link from the tree.
909	 */
910	if (recursed)
911		return (0);
912	recursed = 1;
913	if (parent->w_class->lc_flags & LC_SLEEPLOCK)
914		list = &w_sleep;
915	else
916		list = &w_spin;
917	STAILQ_FOREACH(child, list, w_typelist) {
918		STAILQ_FOREACH(parent, list, w_typelist) {
919			if (!isitmychild(parent, child))
920				continue;
921			removechild(parent, child);
922			if (isitmydescendant(parent, child))
923				continue;
924			itismychild(parent, child);
925		}
926	}
927	recursed = 0;
928	witness_levelall();
929	return (0);
930}
931
932static void
933removechild(struct witness *parent, struct witness *child)
934{
935	struct witness_child_list_entry **wcl, *wcl1;
936	int i;
937
938	for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
939		for (i = 0; i < (*wcl)->wcl_count; i++)
940			if ((*wcl)->wcl_children[i] == child)
941				goto found;
942	return;
943found:
944	(*wcl)->wcl_count--;
945	if ((*wcl)->wcl_count > i)
946		(*wcl)->wcl_children[i] =
947		    (*wcl)->wcl_children[(*wcl)->wcl_count];
948	MPASS((*wcl)->wcl_children[i] != NULL);
949
950	if ((*wcl)->wcl_count != 0)
951		return;
952
953	wcl1 = *wcl;
954	*wcl = wcl1->wcl_next;
955	witness_child_free(wcl1);
956}
957
958static int
959isitmychild(struct witness *parent, struct witness *child)
960{
961	struct witness_child_list_entry *wcl;
962	int i;
963
964	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
965		for (i = 0; i < wcl->wcl_count; i++) {
966			if (wcl->wcl_children[i] == child)
967				return (1);
968		}
969	}
970	return (0);
971}
972
973static int
974isitmydescendant(struct witness *parent, struct witness *child)
975{
976	struct witness_child_list_entry *wcl;
977	int i, j;
978
979	if (isitmychild(parent, child))
980		return (1);
981	j = 0;
982	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
983		MPASS(j < 1000);
984		for (i = 0; i < wcl->wcl_count; i++) {
985			if (isitmydescendant(wcl->wcl_children[i], child))
986				return (1);
987		}
988		j++;
989	}
990	return (0);
991}
992
993void
994witness_levelall (void)
995{
996	struct witness_list *list;
997	struct witness *w, *w1;
998
999	/*
1000	 * First clear all levels.
1001	 */
1002	STAILQ_FOREACH(w, &w_all, w_list) {
1003		w->w_level = 0;
1004	}
1005
1006	/*
1007	 * Look for locks with no parent and level all their descendants.
1008	 */
1009	STAILQ_FOREACH(w, &w_all, w_list) {
1010		/*
1011		 * This is just an optimization, technically we could get
1012		 * away just walking the all list each time.
1013		 */
1014		if (w->w_class->lc_flags & LC_SLEEPLOCK)
1015			list = &w_sleep;
1016		else
1017			list = &w_spin;
1018		STAILQ_FOREACH(w1, list, w_typelist) {
1019			if (isitmychild(w1, w))
1020				goto skip;
1021		}
1022		witness_leveldescendents(w, 0);
1023	skip:
1024	}
1025}
1026
1027static void
1028witness_leveldescendents(struct witness *parent, int level)
1029{
1030	struct witness_child_list_entry *wcl;
1031	int i;
1032
1033	if (parent->w_level < level)
1034		parent->w_level = level;
1035	level++;
1036	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1037		for (i = 0; i < wcl->wcl_count; i++)
1038			witness_leveldescendents(wcl->wcl_children[i], level);
1039}
1040
1041static void
1042witness_displaydescendants(void(*prnt)(const char *fmt, ...),
1043			   struct witness *parent)
1044{
1045	struct witness_child_list_entry *wcl;
1046	int i, level;
1047
1048	level =  parent->w_level;
1049
1050	prnt("%-2d", level);
1051	for (i = 0; i < level; i++)
1052		prnt(" ");
1053	prnt("%s", parent->w_name);
1054	if (parent->w_file != NULL)
1055		prnt(" -- last acquired @ %s:%d\n", parent->w_file,
1056		    parent->w_line);
1057
1058	for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
1059		for (i = 0; i < wcl->wcl_count; i++)
1060			    witness_displaydescendants(prnt,
1061				wcl->wcl_children[i]);
1062}
1063
1064static int
1065dup_ok(struct witness *w)
1066{
1067	const char **dup;
1068
1069	for (dup = dup_list; *dup != NULL; dup++)
1070		if (strcmp(w->w_name, *dup) == 0)
1071			return (1);
1072	return (0);
1073}
1074
1075static int
1076blessed(struct witness *w1, struct witness *w2)
1077{
1078	int i;
1079	struct witness_blessed *b;
1080
1081	for (i = 0; i < blessed_count; i++) {
1082		b = &blessed_list[i];
1083		if (strcmp(w1->w_name, b->b_lock1) == 0) {
1084			if (strcmp(w2->w_name, b->b_lock2) == 0)
1085				return (1);
1086			continue;
1087		}
1088		if (strcmp(w1->w_name, b->b_lock2) == 0)
1089			if (strcmp(w2->w_name, b->b_lock1) == 0)
1090				return (1);
1091	}
1092	return (0);
1093}
1094
1095static struct witness *
1096witness_get(void)
1097{
1098	struct witness *w;
1099
1100	if (STAILQ_EMPTY(&w_free)) {
1101		witness_dead = 1;
1102		mtx_unlock_spin(&w_mtx);
1103		printf("%s: witness exhausted\n", __func__);
1104		return (NULL);
1105	}
1106	w = STAILQ_FIRST(&w_free);
1107	STAILQ_REMOVE_HEAD(&w_free, w_list);
1108	bzero(w, sizeof(*w));
1109	return (w);
1110}
1111
1112static void
1113witness_free(struct witness *w)
1114{
1115
1116	STAILQ_INSERT_HEAD(&w_free, w, w_list);
1117}
1118
1119static struct witness_child_list_entry *
1120witness_child_get(void)
1121{
1122	struct witness_child_list_entry *wcl;
1123
1124	wcl = w_child_free;
1125	if (wcl == NULL) {
1126		witness_dead = 1;
1127		mtx_unlock_spin(&w_mtx);
1128		printf("%s: witness exhausted\n", __func__);
1129		return (NULL);
1130	}
1131	w_child_free = wcl->wcl_next;
1132	bzero(wcl, sizeof(*wcl));
1133	return (wcl);
1134}
1135
1136static void
1137witness_child_free(struct witness_child_list_entry *wcl)
1138{
1139
1140	wcl->wcl_next = w_child_free;
1141	w_child_free = wcl;
1142}
1143
1144static struct lock_list_entry *
1145witness_lock_list_get(void)
1146{
1147	struct lock_list_entry *lle;
1148
1149	mtx_lock_spin(&w_mtx);
1150	lle = w_lock_list_free;
1151	if (lle == NULL) {
1152		witness_dead = 1;
1153		mtx_unlock_spin(&w_mtx);
1154		printf("%s: witness exhausted\n", __func__);
1155		return (NULL);
1156	}
1157	w_lock_list_free = lle->ll_next;
1158	mtx_unlock_spin(&w_mtx);
1159	bzero(lle, sizeof(*lle));
1160	return (lle);
1161}
1162
1163static void
1164witness_lock_list_free(struct lock_list_entry *lle)
1165{
1166
1167	mtx_lock_spin(&w_mtx);
1168	lle->ll_next = w_lock_list_free;
1169	w_lock_list_free = lle;
1170	mtx_unlock_spin(&w_mtx);
1171}
1172
1173static struct lock_instance *
1174find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1175{
1176	struct lock_list_entry *lle;
1177	struct lock_instance *instance;
1178	int i;
1179
1180	for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1181		for (i = lle->ll_count - 1; i >= 0; i--) {
1182			instance = &lle->ll_children[i];
1183			if (instance->li_lock == lock)
1184				return (instance);
1185		}
1186	return (NULL);
1187}
1188
1189int
1190witness_list_locks(struct lock_list_entry **lock_list)
1191{
1192	struct lock_list_entry *lle;
1193	struct lock_instance *instance;
1194	struct lock_object *lock;
1195	int i, nheld;
1196
1197	nheld = 0;
1198	for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1199		for (i = lle->ll_count - 1; i >= 0; i--) {
1200			instance = &lle->ll_children[i];
1201			lock = instance->li_lock;
1202			printf("%s (%s) %s (%p) locked @ %s:%d\n",
1203			    (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1204			    "exclusive" : "shared",
1205			    lock->lo_class->lc_name, lock->lo_name, lock,
1206			    instance->li_file, instance->li_line);
1207			nheld++;
1208		}
1209	return (nheld);
1210}
1211
1212/*
1213 * Calling this on p != curproc is bad unless we are in ddb.
1214 */
1215int
1216witness_list(struct proc *p)
1217{
1218	critical_t savecrit;
1219	int nheld;
1220
1221	KASSERT(p == curproc || db_active,
1222	    ("%s: p != curproc and we aren't in the debugger", __func__));
1223	KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1224
1225	nheld = witness_list_locks(&p->p_sleeplocks);
1226
1227	/*
1228	 * We only handle spinlocks if p == curproc.  This is somewhat broken
1229	 * if p is currently executing on some other CPU and holds spin locks
1230	 * as we won't display those locks.  If we had a MI way of getting
1231	 * the per-cpu data for a given cpu then we could use p->p_oncpu to
1232	 * get the list of spinlocks for this process and "fix" this.
1233	 */
1234	if (p == curproc) {
1235		/*
1236		 * Preemption bad because we need PCPU_PTR(spinlocks) to not
1237		 * change.
1238		 */
1239		savecrit = critical_enter();
1240		nheld += witness_list_locks(PCPU_PTR(spinlocks));
1241		critical_exit(savecrit);
1242	}
1243
1244	return (nheld);
1245}
1246
1247void
1248witness_save(struct lock_object *lock, const char **filep, int *linep)
1249{
1250	struct lock_instance *instance;
1251
1252	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
1253	if (lock->lo_witness == NULL)
1254		return;
1255
1256	KASSERT(lock->lo_class->lc_flags & LC_SLEEPLOCK,
1257	    ("%s: lock (%s) %s is not a sleep lock", __func__,
1258	    lock->lo_class->lc_name, lock->lo_name));
1259	instance = find_instance(curproc->p_sleeplocks, lock);
1260	KASSERT(instance != NULL, ("%s: lock (%s) %s not locked", __func__,
1261	    lock->lo_class->lc_name, lock->lo_name));
1262
1263	*filep = instance->li_file;
1264	*linep = instance->li_line;
1265}
1266
1267void
1268witness_restore(struct lock_object *lock, const char *file, int line)
1269{
1270	struct lock_instance *instance;
1271
1272	KASSERT(!witness_cold, ("%s: witness_cold\n", __func__));
1273	if (lock->lo_witness == NULL)
1274		return;
1275
1276	KASSERT(lock->lo_class->lc_flags & LC_SLEEPLOCK,
1277	    ("%s: lock (%s) %s is not a sleep lock", __func__,
1278	    lock->lo_class->lc_name, lock->lo_name));
1279	instance = find_instance(curproc->p_sleeplocks, lock);
1280	KASSERT(instance != NULL, ("%s: lock (%s) %s not locked", __func__,
1281	    lock->lo_class->lc_name, lock->lo_name));
1282
1283	lock->lo_witness->w_file = file;
1284	lock->lo_witness->w_line = line;
1285	instance->li_file = file;
1286	instance->li_line = line;
1287}
1288
1289#ifdef DDB
1290
1291DB_SHOW_COMMAND(locks, db_witness_list)
1292{
1293	struct proc *p;
1294	pid_t pid;
1295
1296	if (have_addr) {
1297		pid = (addr % 16) + ((addr >> 4) % 16) * 10 +
1298		    ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 +
1299		    ((addr >> 16) % 16) * 10000;
1300
1301		/* sx_slock(&allproc_lock); */
1302		LIST_FOREACH(p, &allproc, p_list) {
1303			if (p->p_pid == pid)
1304				break;
1305		}
1306		/* sx_sunlock(&allproc_lock); */
1307		if (p == NULL) {
1308			db_printf("pid %d not found\n", pid);
1309			return;
1310		}
1311	} else
1312		p = curproc;
1313
1314	witness_list(p);
1315}
1316
1317DB_SHOW_COMMAND(witness, db_witness_display)
1318{
1319
1320	witness_display(db_printf);
1321}
1322#endif
1323