2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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
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
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 $
33 * Implementation of the `witness' lock verifier. Originally implemented for
34 * mutexes in BSD/OS. Extended to handle generic lock objects and lock
40 * Pronunciation: 'wit-n&s
42 * Etymology: Middle English witnesse, from Old English witnes knowledge,
43 * testimony, witness, from 2wit
44 * Date: before 12th century
45 * 1 : attestation of a fact or event : TESTIMONY
46 * 2 : one that gives evidence; specifically : one who testifies in
47 * a cause or before a judicial tribunal
48 * 3 : one asked to be present at a transaction so as to be able to
49 * testify to its having taken place
50 * 4 : one who has personal knowledge of something
51 * 5 a : something serving as evidence or proof : SIGN
52 * b : public affirmation by word or example of usually
53 * religious faith or conviction <the heroic witness to divine
55 * 6 capitalized : a member of the Jehovah's Witnesses
59 * Special rules concerning Giant and lock orders:
61 * 1) Giant must be acquired before any other mutexes. Stated another way,
62 * no other mutex may be held when Giant is acquired.
64 * 2) Giant must be released when blocking on a sleepable lock.
66 * This rule is less obvious, but is a result of Giant providing the same
67 * semantics as spl(). Basically, when a thread sleeps, it must release
68 * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule
71 * 3) Giant may be acquired before or after sleepable locks.
73 * This rule is also not quite as obvious. Giant may be acquired after
74 * a sleepable lock because it is a non-sleepable lock and non-sleepable
75 * locks may always be acquired while holding a sleepable lock. The second
76 * case, Giant before a sleepable lock, follows from rule 2) above. Suppose
77 * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1
78 * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and
79 * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to
80 * execute. Thus, acquiring Giant both before and after a sleepable lock
81 * will not result in a lock order reversal.
84 #include <sys/cdefs.h>
85 __FBSDID("$FreeBSD$");
88 #include "opt_hwpmc_hooks.h"
89 #include "opt_witness.h"
91 #include <sys/param.h>
94 #include <sys/kernel.h>
97 #include <sys/malloc.h>
98 #include <sys/mutex.h>
100 #include <sys/proc.h>
101 #include <sys/sbuf.h>
102 #include <sys/sysctl.h>
103 #include <sys/systm.h>
107 #include <machine/stdarg.h>
109 /* Note that these traces do not work with KTR_ALQ. */
111 #define KTR_WITNESS KTR_SUBSYS
113 #define KTR_WITNESS 0
116 #define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */
117 #define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */
119 /* Define this to check for blessed mutexes */
122 #define WITNESS_COUNT 1024
123 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
124 #define WITNESS_SBUFSIZE 32768
125 #define WITNESS_PENDLIST 512
127 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads
128 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should
129 * probably be safe for the most part, but it's still a SWAG.
131 #define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2
133 #define WITNESS_NCHILDREN 6
135 #define LOCK_NCHILDREN 3
137 struct witness_child_list_entry;
140 * Lock instances. A lock instance is the data associated with a lock while
141 * it is held by witness. For example, a lock instance will hold the
142 * recursion count of a lock. Lock instances are held in lists. Spin locks
143 * are held in a per-cpu list while sleep locks are held in per-thread list.
145 struct lock_instance {
146 struct lock_object *li_lock;
149 u_int li_flags; /* Recursion count and LI_* flags. */
153 * A simple list type used to build the list of locks held by a thread
154 * or CPU. We can't simply embed the list in struct lock_object since a
155 * lock may be held by more than one thread if it is a shared lock. Locks
156 * are added to the head of the list, so we fill up each list entry from
157 * "the back" logically. To ease some of the arithmetic, we actually fill
158 * in each list entry the normal way (children[0] then children[1], etc.) but
159 * when we traverse the list we read children[count-1] as the first entry
160 * down to children[0] as the final entry.
162 struct lock_list_entry {
163 struct lock_list_entry *ll_next;
164 struct lock_instance ll_children[LOCK_NCHILDREN];
170 struct lock_class *w_class;
171 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */
172 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */
173 struct witness_child_list_entry *w_children; /* Great evilness... */
178 u_char w_Giant_squawked:1;
179 u_char w_other_squawked:1;
180 u_char w_same_squawked:1;
181 u_char w_displayed:1;
184 struct witness_child_list_entry {
185 struct witness_child_list_entry *wcl_next;
186 struct witness *wcl_children[WITNESS_NCHILDREN];
190 STAILQ_HEAD(witness_list, witness);
193 struct witness_blessed {
199 struct witness_order_list_entry {
201 struct lock_class *w_class;
204 struct witness_pendhelp {
206 struct lock_object *wh_lock;
210 static int blessed(struct witness *, struct witness *);
212 static void depart(struct witness *w);
213 static struct witness *enroll(const char *description,
214 struct lock_class *lock_class);
215 static int insertchild(struct witness *parent, struct witness *child);
216 static int isitmychild(struct witness *parent, struct witness *child);
217 static int isitmydescendant(struct witness *parent, struct witness *child);
218 static int itismychild(struct witness *parent, struct witness *child);
219 static void removechild(struct witness *parent, struct witness *child);
220 static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
221 static int sysctl_debug_witness_graphs(SYSCTL_HANDLER_ARGS);
222 static const char *fixup_filename(const char *file);
223 static void witness_addgraph(struct sbuf *sb, struct witness *parent);
224 static struct witness *witness_get(void);
225 static void witness_free(struct witness *m);
226 static struct witness_child_list_entry *witness_child_get(void);
227 static void witness_child_free(struct witness_child_list_entry *wcl);
228 static struct lock_list_entry *witness_lock_list_get(void);
229 static void witness_lock_list_free(struct lock_list_entry *lle);
230 static struct lock_instance *find_instance(struct lock_list_entry *lock_list,
231 struct lock_object *lock);
232 static void witness_list_lock(struct lock_instance *instance);
234 static void witness_leveldescendents(struct witness *parent, int level);
235 static void witness_levelall(void);
236 static void witness_displaydescendants(void(*)(const char *fmt, ...),
237 struct witness *, int indent);
238 static void witness_display_list(void(*prnt)(const char *fmt, ...),
239 struct witness_list *list);
240 static void witness_display(void(*)(const char *fmt, ...));
241 static void witness_list(struct thread *td);
244 SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking");
247 * If set to 0, witness is disabled. If set to a non-zero value, witness
248 * performs full lock order checking for all locks. At runtime, this
249 * value may be set to 0 to turn off witness. witness is not allowed be
250 * turned on once it is turned off, however.
252 static int witness_watch = 1;
253 TUNABLE_INT("debug.witness.watch", &witness_watch);
254 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
255 sysctl_debug_witness_watch, "I", "witness is watching lock operations");
256 SYSCTL_PROC(_debug_witness, OID_AUTO, graphs, CTLTYPE_STRING | CTLFLAG_RD,
257 NULL, 0, sysctl_debug_witness_graphs, "A", "Show locks relation graphs");
261 * When KDB is enabled and witness_kdb is set to 1, it will cause the system
262 * to drop into kdebug() when:
263 * - a lock hierarchy violation occurs
264 * - locks are held when going to sleep.
271 TUNABLE_INT("debug.witness.kdb", &witness_kdb);
272 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, "");
275 * When KDB is enabled and witness_trace is set to 1, it will cause the system
276 * to print a stack trace:
277 * - a lock hierarchy violation occurs
278 * - locks are held when going to sleep.
280 int witness_trace = 1;
281 TUNABLE_INT("debug.witness.trace", &witness_trace);
282 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, "");
285 #ifdef WITNESS_SKIPSPIN
286 int witness_skipspin = 1;
288 int witness_skipspin = 0;
290 TUNABLE_INT("debug.witness.skipspin", &witness_skipspin);
291 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN,
292 &witness_skipspin, 0, "");
294 static struct mtx w_mtx;
295 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
296 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
297 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
298 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
299 static struct witness_child_list_entry *w_child_free = NULL;
300 static struct lock_list_entry *w_lock_list_free = NULL;
301 static struct witness_pendhelp pending_locks[WITNESS_PENDLIST];
302 static u_int pending_cnt;
304 static int w_free_cnt, w_spin_cnt, w_sleep_cnt, w_child_free_cnt, w_child_cnt;
305 SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, "");
306 SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, "");
307 SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0,
309 SYSCTL_INT(_debug_witness, OID_AUTO, child_free_cnt, CTLFLAG_RD,
310 &w_child_free_cnt, 0, "");
311 SYSCTL_INT(_debug_witness, OID_AUTO, child_cnt, CTLFLAG_RD, &w_child_cnt, 0,
314 static struct witness w_data[WITNESS_COUNT];
315 static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT];
316 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
318 static struct witness_order_list_entry order_lists[] = {
322 { "proctree", &lock_class_sx },
323 { "allproc", &lock_class_sx },
324 { "allprison", &lock_class_sx },
329 { "Giant", &lock_class_mtx_sleep },
330 { "pipe mutex", &lock_class_mtx_sleep },
331 { "sigio lock", &lock_class_mtx_sleep },
332 { "process group", &lock_class_mtx_sleep },
333 { "process lock", &lock_class_mtx_sleep },
334 { "session", &lock_class_mtx_sleep },
335 { "uidinfo hash", &lock_class_rw },
337 { "pmc-sleep", &lock_class_mtx_sleep },
343 { "accept", &lock_class_mtx_sleep },
344 { "so_snd", &lock_class_mtx_sleep },
345 { "so_rcv", &lock_class_mtx_sleep },
346 { "sellck", &lock_class_mtx_sleep },
351 { "so_rcv", &lock_class_mtx_sleep },
352 { "radix node head", &lock_class_mtx_sleep },
353 { "rtentry", &lock_class_mtx_sleep },
354 { "ifaddr", &lock_class_mtx_sleep },
357 * Multicast - protocol locks before interface locks, after UDP locks.
359 { "udpinp", &lock_class_rw },
360 { "in_multi_mtx", &lock_class_mtx_sleep },
361 { "igmp_mtx", &lock_class_mtx_sleep },
362 { "if_addr_mtx", &lock_class_mtx_sleep },
365 * UNIX Domain Sockets
367 { "unp", &lock_class_mtx_sleep },
368 { "so_snd", &lock_class_mtx_sleep },
373 { "udp", &lock_class_rw },
374 { "udpinp", &lock_class_rw },
375 { "so_snd", &lock_class_mtx_sleep },
380 { "tcp", &lock_class_rw },
381 { "tcpinp", &lock_class_rw },
382 { "so_snd", &lock_class_mtx_sleep },
387 { "slip_mtx", &lock_class_mtx_sleep },
388 { "slip sc_mtx", &lock_class_mtx_sleep },
393 { "ddp_list_mtx", &lock_class_mtx_sleep },
394 { "ddp_mtx", &lock_class_mtx_sleep },
399 { "bpf global lock", &lock_class_mtx_sleep },
400 { "bpf interface lock", &lock_class_mtx_sleep },
401 { "bpf cdev lock", &lock_class_mtx_sleep },
406 { "nfsd_mtx", &lock_class_mtx_sleep },
407 { "so_snd", &lock_class_mtx_sleep },
413 { "802.11 com lock", &lock_class_mtx_sleep},
418 { "network driver", &lock_class_mtx_sleep},
424 { "ng_node", &lock_class_mtx_sleep },
425 { "ng_worklist", &lock_class_mtx_sleep },
430 { "system map", &lock_class_mtx_sleep },
431 { "vm page queue mutex", &lock_class_mtx_sleep },
432 { "vnode interlock", &lock_class_mtx_sleep },
433 { "cdev", &lock_class_mtx_sleep },
436 * kqueue/VFS interaction
438 { "kqueue", &lock_class_mtx_sleep },
439 { "struct mount mtx", &lock_class_mtx_sleep },
440 { "vnode interlock", &lock_class_mtx_sleep },
446 { "ap boot", &lock_class_mtx_spin },
448 { "rm.mutex_mtx", &lock_class_mtx_spin },
449 { "sio", &lock_class_mtx_spin },
450 { "scrlock", &lock_class_mtx_spin },
452 { "cy", &lock_class_mtx_spin },
455 { "pcib_mtx", &lock_class_mtx_spin },
456 { "rtc_mtx", &lock_class_mtx_spin },
458 { "scc_hwmtx", &lock_class_mtx_spin },
459 { "uart_hwmtx", &lock_class_mtx_spin },
460 { "fast_taskqueue", &lock_class_mtx_spin },
461 { "intr table", &lock_class_mtx_spin },
463 { "pmc-per-proc", &lock_class_mtx_spin },
465 { "process slock", &lock_class_mtx_spin },
466 { "sleepq chain", &lock_class_mtx_spin },
467 { "umtx lock", &lock_class_mtx_spin },
468 { "rm_spinlock", &lock_class_mtx_spin },
469 { "turnstile chain", &lock_class_mtx_spin },
470 { "turnstile lock", &lock_class_mtx_spin },
471 { "sched lock", &lock_class_mtx_spin },
472 { "td_contested", &lock_class_mtx_spin },
473 { "callout", &lock_class_mtx_spin },
474 { "entropy harvest mutex", &lock_class_mtx_spin },
475 { "syscons video lock", &lock_class_mtx_spin },
476 { "time lock", &lock_class_mtx_spin },
478 { "smp rendezvous", &lock_class_mtx_spin },
481 { "tlb0", &lock_class_mtx_spin },
486 { "intrcnt", &lock_class_mtx_spin },
487 { "icu", &lock_class_mtx_spin },
488 #if defined(SMP) && defined(__sparc64__)
489 { "ipi", &lock_class_mtx_spin },
492 { "allpmaps", &lock_class_mtx_spin },
493 { "descriptor tables", &lock_class_mtx_spin },
495 { "clk", &lock_class_mtx_spin },
496 { "cpuset", &lock_class_mtx_spin },
497 { "mprof lock", &lock_class_mtx_spin },
498 { "zombie lock", &lock_class_mtx_spin },
499 { "ALD Queue", &lock_class_mtx_spin },
501 { "MCA spin lock", &lock_class_mtx_spin },
503 #if defined(__i386__) || defined(__amd64__)
504 { "pcicfg", &lock_class_mtx_spin },
505 { "NDIS thread lock", &lock_class_mtx_spin },
507 { "tw_osl_io_lock", &lock_class_mtx_spin },
508 { "tw_osl_q_lock", &lock_class_mtx_spin },
509 { "tw_cl_io_lock", &lock_class_mtx_spin },
510 { "tw_cl_intr_lock", &lock_class_mtx_spin },
511 { "tw_cl_gen_lock", &lock_class_mtx_spin },
513 { "pmc-leaf", &lock_class_mtx_spin },
515 { "blocked lock", &lock_class_mtx_spin },
522 * Pairs of locks which have been blessed
523 * Don't complain about order problems with blessed locks
525 static struct witness_blessed blessed_list[] = {
527 static int blessed_count =
528 sizeof(blessed_list) / sizeof(struct witness_blessed);
532 * This global is set to 0 once it becomes safe to use the witness code.
534 static int witness_cold = 1;
537 * This global is set to 1 once the static lock orders have been enrolled
538 * so that a warning can be issued for any spin locks enrolled later.
540 static int witness_spin_warn = 0;
543 * The WITNESS-enabled diagnostic code. Note that the witness code does
544 * assume that the early boot is single-threaded at least until after this
545 * routine is completed.
548 witness_initialize(void *dummy __unused)
550 struct lock_object *lock;
551 struct witness_order_list_entry *order;
552 struct witness *w, *w1;
556 * We have to release Giant before initializing its witness
557 * structure so that WITNESS doesn't get confused.
560 mtx_assert(&Giant, MA_NOTOWNED);
562 CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
563 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
564 MTX_NOWITNESS | MTX_NOPROFILE);
565 for (i = 0; i < WITNESS_COUNT; i++)
566 witness_free(&w_data[i]);
567 for (i = 0; i < WITNESS_CHILDCOUNT; i++)
568 witness_child_free(&w_childdata[i]);
569 for (i = 0; i < LOCK_CHILDCOUNT; i++)
570 witness_lock_list_free(&w_locklistdata[i]);
572 /* First add in all the specified order lists. */
573 for (order = order_lists; order->w_name != NULL; order++) {
574 w = enroll(order->w_name, order->w_class);
577 w->w_file = "order list";
578 for (order++; order->w_name != NULL; order++) {
579 w1 = enroll(order->w_name, order->w_class);
582 w1->w_file = "order list";
583 if (!itismychild(w, w1))
584 panic("Not enough memory for static orders!");
588 witness_spin_warn = 1;
590 /* Iterate through all locks and add them to witness. */
591 for (i = 0; pending_locks[i].wh_lock != NULL; i++) {
592 lock = pending_locks[i].wh_lock;
593 KASSERT(lock->lo_flags & LO_WITNESS,
594 ("%s: lock %s is on pending list but not LO_WITNESS",
595 __func__, lock->lo_name));
596 lock->lo_witness = enroll(pending_locks[i].wh_type,
600 /* Mark the witness code as being ready for use. */
605 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize,
609 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
613 value = witness_watch;
614 error = sysctl_handle_int(oidp, &value, 0, req);
615 if (error != 0 || req->newptr == NULL)
617 if (value == witness_watch)
626 sysctl_debug_witness_graphs(SYSCTL_HANDLER_ARGS)
632 KASSERT(witness_cold == 0, ("%s: witness is still cold", __func__));
634 sb = sbuf_new(NULL, NULL, WITNESS_SBUFSIZE, SBUF_FIXEDLEN);
638 mtx_lock_spin(&w_mtx);
639 STAILQ_FOREACH(w, &w_all, w_list)
641 STAILQ_FOREACH(w, &w_all, w_list)
642 witness_addgraph(sb, w);
643 mtx_unlock_spin(&w_mtx);
645 if (sbuf_overflowed(sb)) {
647 panic("%s: sbuf overflowed, bump the static buffer size\n",
652 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
659 witness_init(struct lock_object *lock, const char *type)
661 struct lock_class *class;
663 /* Various sanity checks. */
664 class = LOCK_CLASS(lock);
665 if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
666 (class->lc_flags & LC_RECURSABLE) == 0)
667 panic("%s: lock (%s) %s can not be recursable", __func__,
668 class->lc_name, lock->lo_name);
669 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
670 (class->lc_flags & LC_SLEEPABLE) == 0)
671 panic("%s: lock (%s) %s can not be sleepable", __func__,
672 class->lc_name, lock->lo_name);
673 if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
674 (class->lc_flags & LC_UPGRADABLE) == 0)
675 panic("%s: lock (%s) %s can not be upgradable", __func__,
676 class->lc_name, lock->lo_name);
679 * If we shouldn't watch this lock, then just clear lo_witness.
680 * Otherwise, if witness_cold is set, then it is too early to
681 * enroll this lock, so defer it to witness_initialize() by adding
682 * it to the pending_locks list. If it is not too early, then enroll
685 if (witness_watch == 0 || panicstr != NULL ||
686 (lock->lo_flags & LO_WITNESS) == 0)
687 lock->lo_witness = NULL;
688 else if (witness_cold) {
689 pending_locks[pending_cnt].wh_lock = lock;
690 pending_locks[pending_cnt++].wh_type = type;
691 if (pending_cnt > WITNESS_PENDLIST)
692 panic("%s: pending locks list is too small, bump it\n",
695 lock->lo_witness = enroll(type, class);
699 witness_destroy(struct lock_object *lock)
701 struct lock_class *class;
704 class = LOCK_CLASS(lock);
706 panic("lock (%s) %s destroyed while witness_cold",
707 class->lc_name, lock->lo_name);
709 /* XXX: need to verify that no one holds the lock */
710 if ((lock->lo_flags & LO_WITNESS) && lock->lo_witness != NULL) {
711 w = lock->lo_witness;
712 mtx_lock_spin(&w_mtx);
713 MPASS(w->w_refcount > 0);
716 if (w->w_refcount == 0)
718 mtx_unlock_spin(&w_mtx);
724 witness_levelall (void)
726 struct witness_list *list;
727 struct witness *w, *w1;
730 * First clear all levels.
732 STAILQ_FOREACH(w, &w_all, w_list) {
737 * Look for locks with no parent and level all their descendants.
739 STAILQ_FOREACH(w, &w_all, w_list) {
741 * This is just an optimization, technically we could get
742 * away just walking the all list each time.
744 if (w->w_class->lc_flags & LC_SLEEPLOCK)
748 STAILQ_FOREACH(w1, list, w_typelist) {
749 if (isitmychild(w1, w))
752 witness_leveldescendents(w, 0);
754 ; /* silence GCC 3.x */
759 witness_leveldescendents(struct witness *parent, int level)
761 struct witness_child_list_entry *wcl;
764 if (parent->w_level < level)
765 parent->w_level = level;
767 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
768 for (i = 0; i < wcl->wcl_count; i++)
769 witness_leveldescendents(wcl->wcl_children[i], level);
773 witness_displaydescendants(void(*prnt)(const char *fmt, ...),
774 struct witness *parent, int indent)
776 struct witness_child_list_entry *wcl;
779 level = parent->w_level;
781 for (i = 0; i < indent; i++)
783 if (parent->w_refcount > 0)
784 prnt("%s", parent->w_name);
787 if (parent->w_displayed) {
788 prnt(" -- (already displayed)\n");
791 parent->w_displayed = 1;
792 if (parent->w_refcount > 0) {
793 if (parent->w_file != NULL)
794 prnt(" -- last acquired @ %s:%d", parent->w_file,
798 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
799 for (i = 0; i < wcl->wcl_count; i++)
800 witness_displaydescendants(prnt,
801 wcl->wcl_children[i], indent + 1);
805 witness_display_list(void(*prnt)(const char *fmt, ...),
806 struct witness_list *list)
810 STAILQ_FOREACH(w, list, w_typelist) {
811 if (w->w_file == NULL || w->w_level > 0)
814 * This lock has no anscestors, display its descendants.
816 witness_displaydescendants(prnt, w, 0);
821 witness_addgraph(struct sbuf *sb, struct witness *parent)
823 struct witness_child_list_entry *wcl;
826 if (parent->w_displayed != 0 || parent->w_refcount == 0 ||
827 parent->w_file == NULL)
830 parent->w_displayed = 1;
831 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
832 for (i = 0; i < wcl->wcl_count; i++) {
833 sbuf_printf(sb, "\"%s\",\"%s\"\n", parent->w_name,
834 wcl->wcl_children[i]->w_name);
835 witness_addgraph(sb, wcl->wcl_children[i]);
840 witness_display(void(*prnt)(const char *fmt, ...))
844 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
847 /* Clear all the displayed flags. */
848 STAILQ_FOREACH(w, &w_all, w_list) {
853 * First, handle sleep locks which have been acquired at least
856 prnt("Sleep locks:\n");
857 witness_display_list(prnt, &w_sleep);
860 * Now do spin locks which have been acquired at least once.
862 prnt("\nSpin locks:\n");
863 witness_display_list(prnt, &w_spin);
866 * Finally, any locks which have not been acquired yet.
868 prnt("\nLocks which were never acquired:\n");
869 STAILQ_FOREACH(w, &w_all, w_list) {
870 if (w->w_file != NULL || w->w_refcount == 0)
872 prnt("%s\n", w->w_name);
877 /* Trim useless garbage from filenames. */
879 fixup_filename(const char *file)
884 while (strncmp(file, "../", 3) == 0)
890 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
893 if (witness_watch == 0 || panicstr != NULL)
896 /* Require locks that witness knows about. */
897 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
898 lock2->lo_witness == NULL)
901 MPASS(!mtx_owned(&w_mtx));
902 mtx_lock_spin(&w_mtx);
905 * If we already have either an explicit or implied lock order that
906 * is the other way around, then return an error.
908 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
909 mtx_unlock_spin(&w_mtx);
913 /* Try to add the new order. */
914 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
915 lock2->lo_witness->w_name, lock1->lo_witness->w_name);
916 if (!itismychild(lock1->lo_witness, lock2->lo_witness))
918 mtx_unlock_spin(&w_mtx);
923 witness_checkorder(struct lock_object *lock, int flags, const char *file,
926 struct lock_list_entry **lock_list, *lle;
927 struct lock_instance *lock1, *lock2;
928 struct lock_class *class;
929 struct witness *w, *w1;
933 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
938 * Try locks do not block if they fail to acquire the lock, thus
939 * there is no danger of deadlocks or of switching while holding a
940 * spin lock if we acquire a lock via a try operation. This
941 * function shouldn't even be called for try locks, so panic if
944 if (flags & LOP_TRYLOCK)
945 panic("%s should not be called for try lock operations",
948 w = lock->lo_witness;
949 class = LOCK_CLASS(lock);
951 file = fixup_filename(file);
953 if (class->lc_flags & LC_SLEEPLOCK) {
955 * Since spin locks include a critical section, this check
956 * implicitly enforces a lock order of all sleep locks before
959 if (td->td_critnest != 0 && !kdb_active)
960 panic("blockable sleep lock (%s) %s @ %s:%d",
961 class->lc_name, lock->lo_name, file, line);
964 * If this is the first lock acquired then just return as
965 * no order checking is needed.
967 if (td->td_sleeplocks == NULL)
969 lock_list = &td->td_sleeplocks;
972 * If this is the first lock, just return as no order
973 * checking is needed. We check this in both if clauses
974 * here as unifying the check would require us to use a
975 * critical section to ensure we don't migrate while doing
976 * the check. Note that if this is not the first lock, we
977 * are already in a critical section and are safe for the
980 if (PCPU_GET(spinlocks) == NULL)
982 lock_list = PCPU_PTR(spinlocks);
986 * Check to see if we are recursing on a lock we already own. If
987 * so, make sure that we don't mismatch exclusive and shared lock
990 lock1 = find_instance(*lock_list, lock);
992 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
993 (flags & LOP_EXCLUSIVE) == 0) {
994 printf("shared lock of (%s) %s @ %s:%d\n",
995 class->lc_name, lock->lo_name, file, line);
996 printf("while exclusively locked from %s:%d\n",
997 lock1->li_file, lock1->li_line);
998 panic("share->excl");
1000 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
1001 (flags & LOP_EXCLUSIVE) != 0) {
1002 printf("exclusive lock of (%s) %s @ %s:%d\n",
1003 class->lc_name, lock->lo_name, file, line);
1004 printf("while share locked from %s:%d\n",
1005 lock1->li_file, lock1->li_line);
1006 panic("excl->share");
1012 * Check for duplicate locks of the same type. Note that we only
1013 * have to check for this on the last lock we just acquired. Any
1014 * other cases will be caught as lock order violations.
1016 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
1017 w1 = lock1->li_lock->lo_witness;
1019 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) ||
1020 (flags & LOP_DUPOK))
1022 w->w_same_squawked = 1;
1023 printf("acquiring duplicate lock of same type: \"%s\"\n",
1025 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name,
1026 lock1->li_file, lock1->li_line);
1027 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
1034 MPASS(!mtx_owned(&w_mtx));
1035 mtx_lock_spin(&w_mtx);
1037 * If we know that the the lock we are acquiring comes after
1038 * the lock we most recently acquired in the lock order tree,
1039 * then there is no need for any further checks.
1041 if (isitmychild(w1, w)) {
1042 mtx_unlock_spin(&w_mtx);
1045 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
1046 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
1048 MPASS(j < WITNESS_COUNT);
1049 lock1 = &lle->ll_children[i];
1050 w1 = lock1->li_lock->lo_witness;
1053 * If this lock doesn't undergo witness checking,
1057 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
1058 ("lock missing witness structure"));
1062 * If we are locking Giant and this is a sleepable
1063 * lock, then skip it.
1065 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
1066 lock == &Giant.lock_object)
1069 * If we are locking a sleepable lock and this lock
1070 * is Giant, then skip it.
1072 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1073 lock1->li_lock == &Giant.lock_object)
1076 * If we are locking a sleepable lock and this lock
1077 * isn't sleepable, we want to treat it as a lock
1078 * order violation to enfore a general lock order of
1079 * sleepable locks before non-sleepable locks.
1081 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1082 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1085 * If we are locking Giant and this is a non-sleepable
1086 * lock, then treat it as a reversal.
1088 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 &&
1089 lock == &Giant.lock_object)
1092 * Check the lock order hierarchy for a reveresal.
1094 if (!isitmydescendant(w, w1))
1098 * We have a lock order violation, check to see if it
1099 * is allowed or has already been yelled about.
1101 mtx_unlock_spin(&w_mtx);
1104 * If the lock order is blessed, just bail. We don't
1105 * look for other lock order violations though, which
1111 if (lock1->li_lock == &Giant.lock_object) {
1112 if (w1->w_Giant_squawked)
1115 w1->w_Giant_squawked = 1;
1117 if (w1->w_other_squawked)
1120 w1->w_other_squawked = 1;
1123 * Ok, yell about it.
1125 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1126 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1128 "lock order reversal: (sleepable after non-sleepable)\n");
1129 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0
1130 && lock == &Giant.lock_object)
1132 "lock order reversal: (Giant after non-sleepable)\n");
1134 printf("lock order reversal:\n");
1136 * Try to locate an earlier lock with
1137 * witness w in our list.
1140 lock2 = &lle->ll_children[i];
1141 MPASS(lock2->li_lock != NULL);
1142 if (lock2->li_lock->lo_witness == w)
1144 if (i == 0 && lle->ll_next != NULL) {
1146 i = lle->ll_count - 1;
1147 MPASS(i >= 0 && i < LOCK_NCHILDREN);
1152 printf(" 1st %p %s (%s) @ %s:%d\n",
1153 lock1->li_lock, lock1->li_lock->lo_name,
1154 w1->w_name, lock1->li_file, lock1->li_line);
1155 printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
1156 lock->lo_name, w->w_name, file, line);
1158 printf(" 1st %p %s (%s) @ %s:%d\n",
1159 lock2->li_lock, lock2->li_lock->lo_name,
1160 lock2->li_lock->lo_witness->w_name,
1161 lock2->li_file, lock2->li_line);
1162 printf(" 2nd %p %s (%s) @ %s:%d\n",
1163 lock1->li_lock, lock1->li_lock->lo_name,
1164 w1->w_name, lock1->li_file, lock1->li_line);
1165 printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
1166 lock->lo_name, w->w_name, file, line);
1175 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
1177 * If requested, build a new lock order. However, don't build a new
1178 * relationship between a sleepable lock and Giant if it is in the
1179 * wrong direction. The correct lock order is that sleepable locks
1180 * always come before Giant.
1182 if (flags & LOP_NEWORDER &&
1183 !(lock1->li_lock == &Giant.lock_object &&
1184 (lock->lo_flags & LO_SLEEPABLE) != 0)) {
1185 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1186 w->w_name, lock1->li_lock->lo_witness->w_name);
1187 if (!itismychild(lock1->li_lock->lo_witness, w))
1188 /* Witness is dead. */
1191 mtx_unlock_spin(&w_mtx);
1199 kdb_enter(KDB_WHY_WITNESS, __func__);
1204 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
1206 struct lock_list_entry **lock_list, *lle;
1207 struct lock_instance *instance;
1211 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1214 w = lock->lo_witness;
1216 file = fixup_filename(file);
1218 /* Determine lock list for this lock. */
1219 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK)
1220 lock_list = &td->td_sleeplocks;
1222 lock_list = PCPU_PTR(spinlocks);
1224 /* Check to see if we are recursing on a lock we already own. */
1225 instance = find_instance(*lock_list, lock);
1226 if (instance != NULL) {
1227 instance->li_flags++;
1228 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
1229 td->td_proc->p_pid, lock->lo_name,
1230 instance->li_flags & LI_RECURSEMASK);
1231 instance->li_file = file;
1232 instance->li_line = line;
1236 /* Update per-witness last file and line acquire. */
1240 /* Find the next open lock instance in the list and fill it. */
1242 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1243 lle = witness_lock_list_get();
1246 lle->ll_next = *lock_list;
1247 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1248 td->td_proc->p_pid, lle);
1251 instance = &lle->ll_children[lle->ll_count++];
1252 instance->li_lock = lock;
1253 instance->li_line = line;
1254 instance->li_file = file;
1255 if ((flags & LOP_EXCLUSIVE) != 0)
1256 instance->li_flags = LI_EXCLUSIVE;
1258 instance->li_flags = 0;
1259 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1260 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1264 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1266 struct lock_instance *instance;
1267 struct lock_class *class;
1269 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1270 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1272 class = LOCK_CLASS(lock);
1273 file = fixup_filename(file);
1274 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1275 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1276 class->lc_name, lock->lo_name, file, line);
1277 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1278 panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1279 class->lc_name, lock->lo_name, file, line);
1280 instance = find_instance(curthread->td_sleeplocks, lock);
1281 if (instance == NULL)
1282 panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1283 class->lc_name, lock->lo_name, file, line);
1284 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1285 panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1286 class->lc_name, lock->lo_name, file, line);
1287 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1288 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1289 class->lc_name, lock->lo_name,
1290 instance->li_flags & LI_RECURSEMASK, file, line);
1291 instance->li_flags |= LI_EXCLUSIVE;
1295 witness_downgrade(struct lock_object *lock, int flags, const char *file,
1298 struct lock_instance *instance;
1299 struct lock_class *class;
1301 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1302 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1304 class = LOCK_CLASS(lock);
1305 file = fixup_filename(file);
1306 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1307 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1308 class->lc_name, lock->lo_name, file, line);
1309 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1310 panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1311 class->lc_name, lock->lo_name, file, line);
1312 instance = find_instance(curthread->td_sleeplocks, lock);
1313 if (instance == NULL)
1314 panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1315 class->lc_name, lock->lo_name, file, line);
1316 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1317 panic("downgrade of shared lock (%s) %s @ %s:%d",
1318 class->lc_name, lock->lo_name, file, line);
1319 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1320 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1321 class->lc_name, lock->lo_name,
1322 instance->li_flags & LI_RECURSEMASK, file, line);
1323 instance->li_flags &= ~LI_EXCLUSIVE;
1327 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1329 struct lock_list_entry **lock_list, *lle;
1330 struct lock_instance *instance;
1331 struct lock_class *class;
1336 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1340 class = LOCK_CLASS(lock);
1341 file = fixup_filename(file);
1343 /* Find lock instance associated with this lock. */
1344 if (class->lc_flags & LC_SLEEPLOCK)
1345 lock_list = &td->td_sleeplocks;
1347 lock_list = PCPU_PTR(spinlocks);
1348 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1349 for (i = 0; i < (*lock_list)->ll_count; i++) {
1350 instance = &(*lock_list)->ll_children[i];
1351 if (instance->li_lock == lock)
1354 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
1358 /* First, check for shared/exclusive mismatches. */
1359 if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
1360 (flags & LOP_EXCLUSIVE) == 0) {
1361 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1362 lock->lo_name, file, line);
1363 printf("while exclusively locked from %s:%d\n",
1364 instance->li_file, instance->li_line);
1365 panic("excl->ushare");
1367 if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
1368 (flags & LOP_EXCLUSIVE) != 0) {
1369 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1370 lock->lo_name, file, line);
1371 printf("while share locked from %s:%d\n", instance->li_file,
1373 panic("share->uexcl");
1376 /* If we are recursed, unrecurse. */
1377 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1378 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1379 td->td_proc->p_pid, instance->li_lock->lo_name,
1380 instance->li_flags);
1381 instance->li_flags--;
1385 /* Otherwise, remove this item from the list. */
1387 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1388 td->td_proc->p_pid, instance->li_lock->lo_name,
1389 (*lock_list)->ll_count - 1);
1390 for (j = i; j < (*lock_list)->ll_count - 1; j++)
1391 (*lock_list)->ll_children[j] =
1392 (*lock_list)->ll_children[j + 1];
1393 (*lock_list)->ll_count--;
1396 /* If this lock list entry is now empty, free it. */
1397 if ((*lock_list)->ll_count == 0) {
1399 *lock_list = lle->ll_next;
1400 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1401 td->td_proc->p_pid, lle);
1402 witness_lock_list_free(lle);
1407 * Warn if any locks other than 'lock' are held. Flags can be passed in to
1408 * exempt Giant and sleepable locks from the checks as well. If any
1409 * non-exempt locks are held, then a supplied message is printed to the
1410 * console along with a list of the offending locks. If indicated in the
1411 * flags then a failure results in a panic as well.
1414 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1416 struct lock_list_entry *lle;
1417 struct lock_instance *lock1;
1422 if (witness_cold || witness_watch == 0 || panicstr != NULL)
1426 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1427 for (i = lle->ll_count - 1; i >= 0; i--) {
1428 lock1 = &lle->ll_children[i];
1429 if (lock1->li_lock == lock)
1431 if (flags & WARN_GIANTOK &&
1432 lock1->li_lock == &Giant.lock_object)
1434 if (flags & WARN_SLEEPOK &&
1435 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1441 printf(" with the following");
1442 if (flags & WARN_SLEEPOK)
1443 printf(" non-sleepable");
1444 printf(" locks held:\n");
1447 witness_list_lock(lock1);
1449 if (PCPU_GET(spinlocks) != NULL) {
1451 * Since we already hold a spinlock preemption is
1458 printf(" with the following");
1459 if (flags & WARN_SLEEPOK)
1460 printf(" non-sleepable");
1461 printf(" locks held:\n");
1463 n += witness_list_locks(PCPU_PTR(spinlocks));
1465 if (flags & WARN_PANIC && n)
1466 panic("witness_warn");
1468 else if (witness_kdb && n)
1469 kdb_enter(KDB_WHY_WITNESS, __func__);
1470 else if (witness_trace && n)
1477 witness_file(struct lock_object *lock)
1481 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1483 w = lock->lo_witness;
1488 witness_line(struct lock_object *lock)
1492 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1494 w = lock->lo_witness;
1498 static struct witness *
1499 enroll(const char *description, struct lock_class *lock_class)
1503 if (witness_watch == 0 || panicstr != NULL)
1505 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
1507 mtx_lock_spin(&w_mtx);
1508 STAILQ_FOREACH(w, &w_all, w_list) {
1509 if (w->w_name == description || (w->w_refcount > 0 &&
1510 strcmp(description, w->w_name) == 0)) {
1512 mtx_unlock_spin(&w_mtx);
1513 if (lock_class != w->w_class)
1515 "lock (%s) %s does not match earlier (%s) lock",
1516 description, lock_class->lc_name,
1517 w->w_class->lc_name);
1521 if ((w = witness_get()) == NULL) {
1522 printf("WITNESS: unable to allocate a new witness object\n");
1525 w->w_name = description;
1526 w->w_class = lock_class;
1528 STAILQ_INSERT_HEAD(&w_all, w, w_list);
1529 if (lock_class->lc_flags & LC_SPINLOCK) {
1530 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1532 } else if (lock_class->lc_flags & LC_SLEEPLOCK) {
1533 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1536 mtx_unlock_spin(&w_mtx);
1537 panic("lock class %s is not sleep or spin",
1538 lock_class->lc_name);
1540 mtx_unlock_spin(&w_mtx);
1543 * We issue a warning for any spin locks not defined in the static
1544 * order list as a way to discourage their use (folks should really
1545 * be using non-spin mutexes most of the time). However, several
1546 * 3rd part device drivers use spin locks because that is all they
1547 * have available on Windows and Linux and they think that normal
1548 * mutexes are insufficient.
1550 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_spin_warn)
1551 printf("WITNESS: spin lock %s not in order list\n",
1556 /* Don't let the door bang you on the way out... */
1558 depart(struct witness *w)
1560 struct witness_child_list_entry *wcl, *nwcl;
1561 struct witness_list *list;
1562 struct witness *parent;
1564 MPASS(w->w_refcount == 0);
1565 if (w->w_class->lc_flags & LC_SLEEPLOCK) {
1573 * First, we run through the entire tree looking for any
1574 * witnesses that the outgoing witness is a child of. For
1575 * each parent that we find, we reparent all the direct
1576 * children of the outgoing witness to its parent.
1578 STAILQ_FOREACH(parent, list, w_typelist) {
1579 if (!isitmychild(parent, w))
1581 removechild(parent, w);
1585 * Now we go through and free up the child list of the
1588 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) {
1589 nwcl = wcl->wcl_next;
1591 witness_child_free(wcl);
1595 * Detach from various lists and free.
1597 STAILQ_REMOVE(list, w, witness, w_typelist);
1598 STAILQ_REMOVE(&w_all, w, witness, w_list);
1603 * Add "child" as a direct child of "parent". Returns false if
1604 * we fail due to out of memory.
1607 insertchild(struct witness *parent, struct witness *child)
1609 struct witness_child_list_entry **wcl;
1611 MPASS(child != NULL && parent != NULL);
1614 * Insert "child" after "parent"
1616 wcl = &parent->w_children;
1617 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
1618 wcl = &(*wcl)->wcl_next;
1620 *wcl = witness_child_get();
1625 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
1632 itismychild(struct witness *parent, struct witness *child)
1635 MPASS(child != NULL && parent != NULL);
1636 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
1637 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
1639 "%s: parent (%s) and child (%s) are not the same lock type",
1640 __func__, parent->w_class->lc_name,
1641 child->w_class->lc_name);
1643 return (insertchild(parent, child));
1647 removechild(struct witness *parent, struct witness *child)
1649 struct witness_child_list_entry **wcl, *wcl1;
1652 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1653 for (i = 0; i < (*wcl)->wcl_count; i++)
1654 if ((*wcl)->wcl_children[i] == child)
1658 (*wcl)->wcl_count--;
1659 if ((*wcl)->wcl_count > i)
1660 (*wcl)->wcl_children[i] =
1661 (*wcl)->wcl_children[(*wcl)->wcl_count];
1662 MPASS((*wcl)->wcl_children[i] != NULL);
1663 if ((*wcl)->wcl_count != 0)
1666 *wcl = wcl1->wcl_next;
1668 witness_child_free(wcl1);
1672 isitmychild(struct witness *parent, struct witness *child)
1674 struct witness_child_list_entry *wcl;
1677 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1678 for (i = 0; i < wcl->wcl_count; i++) {
1679 if (wcl->wcl_children[i] == child)
1687 isitmydescendant(struct witness *parent, struct witness *child)
1689 struct witness_child_list_entry *wcl;
1692 if (isitmychild(parent, child))
1695 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1697 for (i = 0; i < wcl->wcl_count; i++) {
1698 if (isitmydescendant(wcl->wcl_children[i], child))
1708 blessed(struct witness *w1, struct witness *w2)
1711 struct witness_blessed *b;
1713 for (i = 0; i < blessed_count; i++) {
1714 b = &blessed_list[i];
1715 if (strcmp(w1->w_name, b->b_lock1) == 0) {
1716 if (strcmp(w2->w_name, b->b_lock2) == 0)
1720 if (strcmp(w1->w_name, b->b_lock2) == 0)
1721 if (strcmp(w2->w_name, b->b_lock1) == 0)
1728 static struct witness *
1733 if (witness_watch == 0) {
1734 mtx_unlock_spin(&w_mtx);
1737 if (STAILQ_EMPTY(&w_free)) {
1739 mtx_unlock_spin(&w_mtx);
1740 printf("%s: witness exhausted\n", __func__);
1743 w = STAILQ_FIRST(&w_free);
1744 STAILQ_REMOVE_HEAD(&w_free, w_list);
1746 bzero(w, sizeof(*w));
1751 witness_free(struct witness *w)
1754 STAILQ_INSERT_HEAD(&w_free, w, w_list);
1758 static struct witness_child_list_entry *
1759 witness_child_get(void)
1761 struct witness_child_list_entry *wcl;
1763 if (witness_watch == 0) {
1764 mtx_unlock_spin(&w_mtx);
1770 mtx_unlock_spin(&w_mtx);
1771 printf("%s: witness exhausted\n", __func__);
1774 w_child_free = wcl->wcl_next;
1776 bzero(wcl, sizeof(*wcl));
1781 witness_child_free(struct witness_child_list_entry *wcl)
1784 wcl->wcl_next = w_child_free;
1789 static struct lock_list_entry *
1790 witness_lock_list_get(void)
1792 struct lock_list_entry *lle;
1794 if (witness_watch == 0)
1796 mtx_lock_spin(&w_mtx);
1797 lle = w_lock_list_free;
1800 mtx_unlock_spin(&w_mtx);
1801 printf("%s: witness exhausted\n", __func__);
1804 w_lock_list_free = lle->ll_next;
1805 mtx_unlock_spin(&w_mtx);
1806 bzero(lle, sizeof(*lle));
1811 witness_lock_list_free(struct lock_list_entry *lle)
1814 mtx_lock_spin(&w_mtx);
1815 lle->ll_next = w_lock_list_free;
1816 w_lock_list_free = lle;
1817 mtx_unlock_spin(&w_mtx);
1820 static struct lock_instance *
1821 find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1823 struct lock_list_entry *lle;
1824 struct lock_instance *instance;
1827 for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1828 for (i = lle->ll_count - 1; i >= 0; i--) {
1829 instance = &lle->ll_children[i];
1830 if (instance->li_lock == lock)
1837 witness_list_lock(struct lock_instance *instance)
1839 struct lock_object *lock;
1841 lock = instance->li_lock;
1842 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1843 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name);
1844 if (lock->lo_witness->w_name != lock->lo_name)
1845 printf(" (%s)", lock->lo_witness->w_name);
1846 printf(" r = %d (%p) locked @ %s:%d\n",
1847 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
1853 witness_thread_has_locks(struct thread *td)
1856 return (td->td_sleeplocks != NULL);
1860 witness_proc_has_locks(struct proc *p)
1864 FOREACH_THREAD_IN_PROC(p, td) {
1865 if (witness_thread_has_locks(td))
1873 witness_list_locks(struct lock_list_entry **lock_list)
1875 struct lock_list_entry *lle;
1879 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1880 for (i = lle->ll_count - 1; i >= 0; i--) {
1881 witness_list_lock(&lle->ll_children[i]);
1888 * This is a bit risky at best. We call this function when we have timed
1889 * out acquiring a spin lock, and we assume that the other CPU is stuck
1890 * with this lock held. So, we go groveling around in the other CPU's
1891 * per-cpu data to try to find the lock instance for this spin lock to
1892 * see when it was last acquired.
1895 witness_display_spinlock(struct lock_object *lock, struct thread *owner)
1897 struct lock_instance *instance;
1900 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
1902 pc = pcpu_find(owner->td_oncpu);
1903 instance = find_instance(pc->pc_spinlocks, lock);
1904 if (instance != NULL)
1905 witness_list_lock(instance);
1909 witness_save(struct lock_object *lock, const char **filep, int *linep)
1911 struct lock_list_entry *lock_list;
1912 struct lock_instance *instance;
1913 struct lock_class *class;
1915 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1916 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1918 class = LOCK_CLASS(lock);
1919 if (class->lc_flags & LC_SLEEPLOCK)
1920 lock_list = curthread->td_sleeplocks;
1922 if (witness_skipspin)
1924 lock_list = PCPU_GET(spinlocks);
1926 instance = find_instance(lock_list, lock);
1927 if (instance == NULL)
1928 panic("%s: lock (%s) %s not locked", __func__,
1929 class->lc_name, lock->lo_name);
1930 *filep = instance->li_file;
1931 *linep = instance->li_line;
1935 witness_restore(struct lock_object *lock, const char *file, int line)
1937 struct lock_list_entry *lock_list;
1938 struct lock_instance *instance;
1939 struct lock_class *class;
1941 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1942 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1944 class = LOCK_CLASS(lock);
1945 if (class->lc_flags & LC_SLEEPLOCK)
1946 lock_list = curthread->td_sleeplocks;
1948 if (witness_skipspin)
1950 lock_list = PCPU_GET(spinlocks);
1952 instance = find_instance(lock_list, lock);
1953 if (instance == NULL)
1954 panic("%s: lock (%s) %s not locked", __func__,
1955 class->lc_name, lock->lo_name);
1956 lock->lo_witness->w_file = file;
1957 lock->lo_witness->w_line = line;
1958 instance->li_file = file;
1959 instance->li_line = line;
1963 witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1965 #ifdef INVARIANT_SUPPORT
1966 struct lock_instance *instance;
1967 struct lock_class *class;
1969 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1971 class = LOCK_CLASS(lock);
1972 if ((class->lc_flags & LC_SLEEPLOCK) != 0)
1973 instance = find_instance(curthread->td_sleeplocks, lock);
1974 else if ((class->lc_flags & LC_SPINLOCK) != 0)
1975 instance = find_instance(PCPU_GET(spinlocks), lock);
1977 panic("Lock (%s) %s is not sleep or spin!",
1978 class->lc_name, lock->lo_name);
1980 file = fixup_filename(file);
1983 if (instance != NULL)
1984 panic("Lock (%s) %s locked @ %s:%d.",
1985 class->lc_name, lock->lo_name, file, line);
1988 case LA_LOCKED | LA_RECURSED:
1989 case LA_LOCKED | LA_NOTRECURSED:
1991 case LA_SLOCKED | LA_RECURSED:
1992 case LA_SLOCKED | LA_NOTRECURSED:
1994 case LA_XLOCKED | LA_RECURSED:
1995 case LA_XLOCKED | LA_NOTRECURSED:
1996 if (instance == NULL) {
1997 panic("Lock (%s) %s not locked @ %s:%d.",
1998 class->lc_name, lock->lo_name, file, line);
2001 if ((flags & LA_XLOCKED) != 0 &&
2002 (instance->li_flags & LI_EXCLUSIVE) == 0)
2003 panic("Lock (%s) %s not exclusively locked @ %s:%d.",
2004 class->lc_name, lock->lo_name, file, line);
2005 if ((flags & LA_SLOCKED) != 0 &&
2006 (instance->li_flags & LI_EXCLUSIVE) != 0)
2007 panic("Lock (%s) %s exclusively locked @ %s:%d.",
2008 class->lc_name, lock->lo_name, file, line);
2009 if ((flags & LA_RECURSED) != 0 &&
2010 (instance->li_flags & LI_RECURSEMASK) == 0)
2011 panic("Lock (%s) %s not recursed @ %s:%d.",
2012 class->lc_name, lock->lo_name, file, line);
2013 if ((flags & LA_NOTRECURSED) != 0 &&
2014 (instance->li_flags & LI_RECURSEMASK) != 0)
2015 panic("Lock (%s) %s recursed @ %s:%d.",
2016 class->lc_name, lock->lo_name, file, line);
2019 panic("Invalid lock assertion at %s:%d.", file, line);
2022 #endif /* INVARIANT_SUPPORT */
2027 witness_list(struct thread *td)
2030 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
2031 KASSERT(kdb_active, ("%s: not in the debugger", __func__));
2033 if (witness_watch == 0)
2036 witness_list_locks(&td->td_sleeplocks);
2039 * We only handle spinlocks if td == curthread. This is somewhat broken
2040 * if td is currently executing on some other CPU and holds spin locks
2041 * as we won't display those locks. If we had a MI way of getting
2042 * the per-cpu data for a given cpu then we could use
2043 * td->td_oncpu to get the list of spinlocks for this thread
2046 * That still wouldn't really fix this unless we locked the scheduler
2047 * lock or stopped the other CPU to make sure it wasn't changing the
2048 * list out from under us. It is probably best to just not try to
2049 * handle threads on other CPU's for now.
2051 if (td == curthread && PCPU_GET(spinlocks) != NULL)
2052 witness_list_locks(PCPU_PTR(spinlocks));
2055 DB_SHOW_COMMAND(locks, db_witness_list)
2060 td = db_lookup_thread(addr, TRUE);
2066 DB_SHOW_COMMAND(alllocks, db_witness_list_all)
2072 * It would be nice to list only threads and processes that actually
2073 * held sleep locks, but that information is currently not exported
2076 FOREACH_PROC_IN_SYSTEM(p) {
2077 if (!witness_proc_has_locks(p))
2079 FOREACH_THREAD_IN_PROC(p, td) {
2080 if (!witness_thread_has_locks(td))
2082 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
2083 td->td_name, td, td->td_tid);
2089 DB_SHOW_COMMAND(witness, db_witness_display)
2092 witness_display(db_printf);