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);
822 witness_addgraph(struct sbuf *sb, struct witness *parent)
824 struct witness_child_list_entry *wcl;
827 if (parent->w_displayed != 0 || parent->w_refcount == 0 ||
828 parent->w_file == NULL)
831 parent->w_displayed = 1;
832 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next)
833 for (i = 0; i < wcl->wcl_count; i++) {
834 sbuf_printf(sb, "\"%s\",\"%s\"\n", parent->w_name,
835 wcl->wcl_children[i]->w_name);
836 witness_addgraph(sb, wcl->wcl_children[i]);
842 witness_display(void(*prnt)(const char *fmt, ...))
846 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
849 /* Clear all the displayed flags. */
850 STAILQ_FOREACH(w, &w_all, w_list) {
855 * First, handle sleep locks which have been acquired at least
858 prnt("Sleep locks:\n");
859 witness_display_list(prnt, &w_sleep);
862 * Now do spin locks which have been acquired at least once.
864 prnt("\nSpin locks:\n");
865 witness_display_list(prnt, &w_spin);
868 * Finally, any locks which have not been acquired yet.
870 prnt("\nLocks which were never acquired:\n");
871 STAILQ_FOREACH(w, &w_all, w_list) {
872 if (w->w_file != NULL || w->w_refcount == 0)
874 prnt("%s\n", w->w_name);
879 /* Trim useless garbage from filenames. */
881 fixup_filename(const char *file)
886 while (strncmp(file, "../", 3) == 0)
892 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
895 if (witness_watch == 0 || panicstr != NULL)
898 /* Require locks that witness knows about. */
899 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
900 lock2->lo_witness == NULL)
903 MPASS(!mtx_owned(&w_mtx));
904 mtx_lock_spin(&w_mtx);
907 * If we already have either an explicit or implied lock order that
908 * is the other way around, then return an error.
910 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
911 mtx_unlock_spin(&w_mtx);
915 /* Try to add the new order. */
916 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
917 lock2->lo_witness->w_name, lock1->lo_witness->w_name);
918 if (!itismychild(lock1->lo_witness, lock2->lo_witness))
920 mtx_unlock_spin(&w_mtx);
925 witness_checkorder(struct lock_object *lock, int flags, const char *file,
928 struct lock_list_entry **lock_list, *lle;
929 struct lock_instance *lock1, *lock2;
930 struct lock_class *class;
931 struct witness *w, *w1;
935 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
940 * Try locks do not block if they fail to acquire the lock, thus
941 * there is no danger of deadlocks or of switching while holding a
942 * spin lock if we acquire a lock via a try operation. This
943 * function shouldn't even be called for try locks, so panic if
946 if (flags & LOP_TRYLOCK)
947 panic("%s should not be called for try lock operations",
950 w = lock->lo_witness;
951 class = LOCK_CLASS(lock);
953 file = fixup_filename(file);
955 if (class->lc_flags & LC_SLEEPLOCK) {
957 * Since spin locks include a critical section, this check
958 * implicitly enforces a lock order of all sleep locks before
961 if (td->td_critnest != 0 && !kdb_active)
962 panic("blockable sleep lock (%s) %s @ %s:%d",
963 class->lc_name, lock->lo_name, file, line);
966 * If this is the first lock acquired then just return as
967 * no order checking is needed.
969 if (td->td_sleeplocks == NULL)
971 lock_list = &td->td_sleeplocks;
974 * If this is the first lock, just return as no order
975 * checking is needed. We check this in both if clauses
976 * here as unifying the check would require us to use a
977 * critical section to ensure we don't migrate while doing
978 * the check. Note that if this is not the first lock, we
979 * are already in a critical section and are safe for the
982 if (PCPU_GET(spinlocks) == NULL)
984 lock_list = PCPU_PTR(spinlocks);
988 * Check to see if we are recursing on a lock we already own. If
989 * so, make sure that we don't mismatch exclusive and shared lock
992 lock1 = find_instance(*lock_list, lock);
994 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
995 (flags & LOP_EXCLUSIVE) == 0) {
996 printf("shared lock of (%s) %s @ %s:%d\n",
997 class->lc_name, lock->lo_name, file, line);
998 printf("while exclusively locked from %s:%d\n",
999 lock1->li_file, lock1->li_line);
1000 panic("share->excl");
1002 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
1003 (flags & LOP_EXCLUSIVE) != 0) {
1004 printf("exclusive lock of (%s) %s @ %s:%d\n",
1005 class->lc_name, lock->lo_name, file, line);
1006 printf("while share locked from %s:%d\n",
1007 lock1->li_file, lock1->li_line);
1008 panic("excl->share");
1014 * Check for duplicate locks of the same type. Note that we only
1015 * have to check for this on the last lock we just acquired. Any
1016 * other cases will be caught as lock order violations.
1018 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
1019 w1 = lock1->li_lock->lo_witness;
1021 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) ||
1022 (flags & LOP_DUPOK))
1024 w->w_same_squawked = 1;
1025 printf("acquiring duplicate lock of same type: \"%s\"\n",
1027 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name,
1028 lock1->li_file, lock1->li_line);
1029 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
1036 MPASS(!mtx_owned(&w_mtx));
1037 mtx_lock_spin(&w_mtx);
1039 * If we know that the the lock we are acquiring comes after
1040 * the lock we most recently acquired in the lock order tree,
1041 * then there is no need for any further checks.
1043 if (isitmychild(w1, w)) {
1044 mtx_unlock_spin(&w_mtx);
1047 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) {
1048 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
1050 MPASS(j < WITNESS_COUNT);
1051 lock1 = &lle->ll_children[i];
1052 w1 = lock1->li_lock->lo_witness;
1055 * If this lock doesn't undergo witness checking,
1059 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
1060 ("lock missing witness structure"));
1064 * If we are locking Giant and this is a sleepable
1065 * lock, then skip it.
1067 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
1068 lock == &Giant.lock_object)
1071 * If we are locking a sleepable lock and this lock
1072 * is Giant, then skip it.
1074 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1075 lock1->li_lock == &Giant.lock_object)
1078 * If we are locking a sleepable lock and this lock
1079 * isn't sleepable, we want to treat it as a lock
1080 * order violation to enfore a general lock order of
1081 * sleepable locks before non-sleepable locks.
1083 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1084 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1087 * If we are locking Giant and this is a non-sleepable
1088 * lock, then treat it as a reversal.
1090 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 &&
1091 lock == &Giant.lock_object)
1094 * Check the lock order hierarchy for a reveresal.
1096 if (!isitmydescendant(w, w1))
1100 * We have a lock order violation, check to see if it
1101 * is allowed or has already been yelled about.
1103 mtx_unlock_spin(&w_mtx);
1106 * If the lock order is blessed, just bail. We don't
1107 * look for other lock order violations though, which
1113 if (lock1->li_lock == &Giant.lock_object) {
1114 if (w1->w_Giant_squawked)
1117 w1->w_Giant_squawked = 1;
1119 if (w1->w_other_squawked)
1122 w1->w_other_squawked = 1;
1125 * Ok, yell about it.
1127 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1128 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1130 "lock order reversal: (sleepable after non-sleepable)\n");
1131 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0
1132 && lock == &Giant.lock_object)
1134 "lock order reversal: (Giant after non-sleepable)\n");
1136 printf("lock order reversal:\n");
1138 * Try to locate an earlier lock with
1139 * witness w in our list.
1142 lock2 = &lle->ll_children[i];
1143 MPASS(lock2->li_lock != NULL);
1144 if (lock2->li_lock->lo_witness == w)
1146 if (i == 0 && lle->ll_next != NULL) {
1148 i = lle->ll_count - 1;
1149 MPASS(i >= 0 && i < LOCK_NCHILDREN);
1154 printf(" 1st %p %s (%s) @ %s:%d\n",
1155 lock1->li_lock, lock1->li_lock->lo_name,
1156 w1->w_name, lock1->li_file, lock1->li_line);
1157 printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
1158 lock->lo_name, w->w_name, file, line);
1160 printf(" 1st %p %s (%s) @ %s:%d\n",
1161 lock2->li_lock, lock2->li_lock->lo_name,
1162 lock2->li_lock->lo_witness->w_name,
1163 lock2->li_file, lock2->li_line);
1164 printf(" 2nd %p %s (%s) @ %s:%d\n",
1165 lock1->li_lock, lock1->li_lock->lo_name,
1166 w1->w_name, lock1->li_file, lock1->li_line);
1167 printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
1168 lock->lo_name, w->w_name, file, line);
1177 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1];
1179 * If requested, build a new lock order. However, don't build a new
1180 * relationship between a sleepable lock and Giant if it is in the
1181 * wrong direction. The correct lock order is that sleepable locks
1182 * always come before Giant.
1184 if (flags & LOP_NEWORDER &&
1185 !(lock1->li_lock == &Giant.lock_object &&
1186 (lock->lo_flags & LO_SLEEPABLE) != 0)) {
1187 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1188 w->w_name, lock1->li_lock->lo_witness->w_name);
1189 if (!itismychild(lock1->li_lock->lo_witness, w))
1190 /* Witness is dead. */
1193 mtx_unlock_spin(&w_mtx);
1201 kdb_enter(KDB_WHY_WITNESS, __func__);
1206 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
1208 struct lock_list_entry **lock_list, *lle;
1209 struct lock_instance *instance;
1213 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1216 w = lock->lo_witness;
1218 file = fixup_filename(file);
1220 /* Determine lock list for this lock. */
1221 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK)
1222 lock_list = &td->td_sleeplocks;
1224 lock_list = PCPU_PTR(spinlocks);
1226 /* Check to see if we are recursing on a lock we already own. */
1227 instance = find_instance(*lock_list, lock);
1228 if (instance != NULL) {
1229 instance->li_flags++;
1230 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
1231 td->td_proc->p_pid, lock->lo_name,
1232 instance->li_flags & LI_RECURSEMASK);
1233 instance->li_file = file;
1234 instance->li_line = line;
1238 /* Update per-witness last file and line acquire. */
1242 /* Find the next open lock instance in the list and fill it. */
1244 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1245 lle = witness_lock_list_get();
1248 lle->ll_next = *lock_list;
1249 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1250 td->td_proc->p_pid, lle);
1253 instance = &lle->ll_children[lle->ll_count++];
1254 instance->li_lock = lock;
1255 instance->li_line = line;
1256 instance->li_file = file;
1257 if ((flags & LOP_EXCLUSIVE) != 0)
1258 instance->li_flags = LI_EXCLUSIVE;
1260 instance->li_flags = 0;
1261 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1262 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1266 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1268 struct lock_instance *instance;
1269 struct lock_class *class;
1271 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1272 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1274 class = LOCK_CLASS(lock);
1275 file = fixup_filename(file);
1276 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1277 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1278 class->lc_name, lock->lo_name, file, line);
1279 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1280 panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1281 class->lc_name, lock->lo_name, file, line);
1282 instance = find_instance(curthread->td_sleeplocks, lock);
1283 if (instance == NULL)
1284 panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1285 class->lc_name, lock->lo_name, file, line);
1286 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1287 panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1288 class->lc_name, lock->lo_name, file, line);
1289 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1290 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1291 class->lc_name, lock->lo_name,
1292 instance->li_flags & LI_RECURSEMASK, file, line);
1293 instance->li_flags |= LI_EXCLUSIVE;
1297 witness_downgrade(struct lock_object *lock, int flags, const char *file,
1300 struct lock_instance *instance;
1301 struct lock_class *class;
1303 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1304 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1306 class = LOCK_CLASS(lock);
1307 file = fixup_filename(file);
1308 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1309 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1310 class->lc_name, lock->lo_name, file, line);
1311 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1312 panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1313 class->lc_name, lock->lo_name, file, line);
1314 instance = find_instance(curthread->td_sleeplocks, lock);
1315 if (instance == NULL)
1316 panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1317 class->lc_name, lock->lo_name, file, line);
1318 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1319 panic("downgrade of shared lock (%s) %s @ %s:%d",
1320 class->lc_name, lock->lo_name, file, line);
1321 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1322 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1323 class->lc_name, lock->lo_name,
1324 instance->li_flags & LI_RECURSEMASK, file, line);
1325 instance->li_flags &= ~LI_EXCLUSIVE;
1329 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1331 struct lock_list_entry **lock_list, *lle;
1332 struct lock_instance *instance;
1333 struct lock_class *class;
1338 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL ||
1342 class = LOCK_CLASS(lock);
1343 file = fixup_filename(file);
1345 /* Find lock instance associated with this lock. */
1346 if (class->lc_flags & LC_SLEEPLOCK)
1347 lock_list = &td->td_sleeplocks;
1349 lock_list = PCPU_PTR(spinlocks);
1350 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1351 for (i = 0; i < (*lock_list)->ll_count; i++) {
1352 instance = &(*lock_list)->ll_children[i];
1353 if (instance->li_lock == lock)
1356 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name,
1360 /* First, check for shared/exclusive mismatches. */
1361 if ((instance->li_flags & LI_EXCLUSIVE) != 0 &&
1362 (flags & LOP_EXCLUSIVE) == 0) {
1363 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1364 lock->lo_name, file, line);
1365 printf("while exclusively locked from %s:%d\n",
1366 instance->li_file, instance->li_line);
1367 panic("excl->ushare");
1369 if ((instance->li_flags & LI_EXCLUSIVE) == 0 &&
1370 (flags & LOP_EXCLUSIVE) != 0) {
1371 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1372 lock->lo_name, file, line);
1373 printf("while share locked from %s:%d\n", instance->li_file,
1375 panic("share->uexcl");
1378 /* If we are recursed, unrecurse. */
1379 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1380 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1381 td->td_proc->p_pid, instance->li_lock->lo_name,
1382 instance->li_flags);
1383 instance->li_flags--;
1387 /* Otherwise, remove this item from the list. */
1389 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1390 td->td_proc->p_pid, instance->li_lock->lo_name,
1391 (*lock_list)->ll_count - 1);
1392 for (j = i; j < (*lock_list)->ll_count - 1; j++)
1393 (*lock_list)->ll_children[j] =
1394 (*lock_list)->ll_children[j + 1];
1395 (*lock_list)->ll_count--;
1398 /* If this lock list entry is now empty, free it. */
1399 if ((*lock_list)->ll_count == 0) {
1401 *lock_list = lle->ll_next;
1402 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1403 td->td_proc->p_pid, lle);
1404 witness_lock_list_free(lle);
1409 * Warn if any locks other than 'lock' are held. Flags can be passed in to
1410 * exempt Giant and sleepable locks from the checks as well. If any
1411 * non-exempt locks are held, then a supplied message is printed to the
1412 * console along with a list of the offending locks. If indicated in the
1413 * flags then a failure results in a panic as well.
1416 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1418 struct lock_list_entry *lle;
1419 struct lock_instance *lock1;
1424 if (witness_cold || witness_watch == 0 || panicstr != NULL)
1428 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1429 for (i = lle->ll_count - 1; i >= 0; i--) {
1430 lock1 = &lle->ll_children[i];
1431 if (lock1->li_lock == lock)
1433 if (flags & WARN_GIANTOK &&
1434 lock1->li_lock == &Giant.lock_object)
1436 if (flags & WARN_SLEEPOK &&
1437 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1443 printf(" with the following");
1444 if (flags & WARN_SLEEPOK)
1445 printf(" non-sleepable");
1446 printf(" locks held:\n");
1449 witness_list_lock(lock1);
1451 if (PCPU_GET(spinlocks) != NULL) {
1453 * Since we already hold a spinlock preemption is
1460 printf(" with the following");
1461 if (flags & WARN_SLEEPOK)
1462 printf(" non-sleepable");
1463 printf(" locks held:\n");
1465 n += witness_list_locks(PCPU_PTR(spinlocks));
1467 if (flags & WARN_PANIC && n)
1468 panic("witness_warn");
1470 else if (witness_kdb && n)
1471 kdb_enter(KDB_WHY_WITNESS, __func__);
1472 else if (witness_trace && n)
1479 witness_file(struct lock_object *lock)
1483 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1485 w = lock->lo_witness;
1490 witness_line(struct lock_object *lock)
1494 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL)
1496 w = lock->lo_witness;
1500 static struct witness *
1501 enroll(const char *description, struct lock_class *lock_class)
1505 if (witness_watch == 0 || panicstr != NULL)
1507 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin)
1509 mtx_lock_spin(&w_mtx);
1510 STAILQ_FOREACH(w, &w_all, w_list) {
1511 if (w->w_name == description || (w->w_refcount > 0 &&
1512 strcmp(description, w->w_name) == 0)) {
1514 mtx_unlock_spin(&w_mtx);
1515 if (lock_class != w->w_class)
1517 "lock (%s) %s does not match earlier (%s) lock",
1518 description, lock_class->lc_name,
1519 w->w_class->lc_name);
1523 if ((w = witness_get()) == NULL) {
1524 printf("WITNESS: unable to allocate a new witness object\n");
1527 w->w_name = description;
1528 w->w_class = lock_class;
1530 STAILQ_INSERT_HEAD(&w_all, w, w_list);
1531 if (lock_class->lc_flags & LC_SPINLOCK) {
1532 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1534 } else if (lock_class->lc_flags & LC_SLEEPLOCK) {
1535 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1538 mtx_unlock_spin(&w_mtx);
1539 panic("lock class %s is not sleep or spin",
1540 lock_class->lc_name);
1542 mtx_unlock_spin(&w_mtx);
1545 * We issue a warning for any spin locks not defined in the static
1546 * order list as a way to discourage their use (folks should really
1547 * be using non-spin mutexes most of the time). However, several
1548 * 3rd part device drivers use spin locks because that is all they
1549 * have available on Windows and Linux and they think that normal
1550 * mutexes are insufficient.
1552 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_spin_warn)
1553 printf("WITNESS: spin lock %s not in order list\n",
1558 /* Don't let the door bang you on the way out... */
1560 depart(struct witness *w)
1562 struct witness_child_list_entry *wcl, *nwcl;
1563 struct witness_list *list;
1564 struct witness *parent;
1566 MPASS(w->w_refcount == 0);
1567 if (w->w_class->lc_flags & LC_SLEEPLOCK) {
1575 * First, we run through the entire tree looking for any
1576 * witnesses that the outgoing witness is a child of. For
1577 * each parent that we find, we reparent all the direct
1578 * children of the outgoing witness to its parent.
1580 STAILQ_FOREACH(parent, list, w_typelist) {
1581 if (!isitmychild(parent, w))
1583 removechild(parent, w);
1587 * Now we go through and free up the child list of the
1590 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) {
1591 nwcl = wcl->wcl_next;
1593 witness_child_free(wcl);
1597 * Detach from various lists and free.
1599 STAILQ_REMOVE(list, w, witness, w_typelist);
1600 STAILQ_REMOVE(&w_all, w, witness, w_list);
1605 * Add "child" as a direct child of "parent". Returns false if
1606 * we fail due to out of memory.
1609 insertchild(struct witness *parent, struct witness *child)
1611 struct witness_child_list_entry **wcl;
1613 MPASS(child != NULL && parent != NULL);
1616 * Insert "child" after "parent"
1618 wcl = &parent->w_children;
1619 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN)
1620 wcl = &(*wcl)->wcl_next;
1622 *wcl = witness_child_get();
1627 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child;
1634 itismychild(struct witness *parent, struct witness *child)
1637 MPASS(child != NULL && parent != NULL);
1638 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) !=
1639 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)))
1641 "%s: parent (%s) and child (%s) are not the same lock type",
1642 __func__, parent->w_class->lc_name,
1643 child->w_class->lc_name);
1645 return (insertchild(parent, child));
1649 removechild(struct witness *parent, struct witness *child)
1651 struct witness_child_list_entry **wcl, *wcl1;
1654 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next)
1655 for (i = 0; i < (*wcl)->wcl_count; i++)
1656 if ((*wcl)->wcl_children[i] == child)
1660 (*wcl)->wcl_count--;
1661 if ((*wcl)->wcl_count > i)
1662 (*wcl)->wcl_children[i] =
1663 (*wcl)->wcl_children[(*wcl)->wcl_count];
1664 MPASS((*wcl)->wcl_children[i] != NULL);
1665 if ((*wcl)->wcl_count != 0)
1668 *wcl = wcl1->wcl_next;
1670 witness_child_free(wcl1);
1674 isitmychild(struct witness *parent, struct witness *child)
1676 struct witness_child_list_entry *wcl;
1679 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1680 for (i = 0; i < wcl->wcl_count; i++) {
1681 if (wcl->wcl_children[i] == child)
1689 isitmydescendant(struct witness *parent, struct witness *child)
1691 struct witness_child_list_entry *wcl;
1694 if (isitmychild(parent, child))
1697 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) {
1699 for (i = 0; i < wcl->wcl_count; i++) {
1700 if (isitmydescendant(wcl->wcl_children[i], child))
1710 blessed(struct witness *w1, struct witness *w2)
1713 struct witness_blessed *b;
1715 for (i = 0; i < blessed_count; i++) {
1716 b = &blessed_list[i];
1717 if (strcmp(w1->w_name, b->b_lock1) == 0) {
1718 if (strcmp(w2->w_name, b->b_lock2) == 0)
1722 if (strcmp(w1->w_name, b->b_lock2) == 0)
1723 if (strcmp(w2->w_name, b->b_lock1) == 0)
1730 static struct witness *
1735 if (witness_watch == 0) {
1736 mtx_unlock_spin(&w_mtx);
1739 if (STAILQ_EMPTY(&w_free)) {
1741 mtx_unlock_spin(&w_mtx);
1742 printf("%s: witness exhausted\n", __func__);
1745 w = STAILQ_FIRST(&w_free);
1746 STAILQ_REMOVE_HEAD(&w_free, w_list);
1748 bzero(w, sizeof(*w));
1753 witness_free(struct witness *w)
1756 STAILQ_INSERT_HEAD(&w_free, w, w_list);
1760 static struct witness_child_list_entry *
1761 witness_child_get(void)
1763 struct witness_child_list_entry *wcl;
1765 if (witness_watch == 0) {
1766 mtx_unlock_spin(&w_mtx);
1772 mtx_unlock_spin(&w_mtx);
1773 printf("%s: witness exhausted\n", __func__);
1776 w_child_free = wcl->wcl_next;
1778 bzero(wcl, sizeof(*wcl));
1783 witness_child_free(struct witness_child_list_entry *wcl)
1786 wcl->wcl_next = w_child_free;
1791 static struct lock_list_entry *
1792 witness_lock_list_get(void)
1794 struct lock_list_entry *lle;
1796 if (witness_watch == 0)
1798 mtx_lock_spin(&w_mtx);
1799 lle = w_lock_list_free;
1802 mtx_unlock_spin(&w_mtx);
1803 printf("%s: witness exhausted\n", __func__);
1806 w_lock_list_free = lle->ll_next;
1807 mtx_unlock_spin(&w_mtx);
1808 bzero(lle, sizeof(*lle));
1813 witness_lock_list_free(struct lock_list_entry *lle)
1816 mtx_lock_spin(&w_mtx);
1817 lle->ll_next = w_lock_list_free;
1818 w_lock_list_free = lle;
1819 mtx_unlock_spin(&w_mtx);
1822 static struct lock_instance *
1823 find_instance(struct lock_list_entry *lock_list, struct lock_object *lock)
1825 struct lock_list_entry *lle;
1826 struct lock_instance *instance;
1829 for (lle = lock_list; lle != NULL; lle = lle->ll_next)
1830 for (i = lle->ll_count - 1; i >= 0; i--) {
1831 instance = &lle->ll_children[i];
1832 if (instance->li_lock == lock)
1839 witness_list_lock(struct lock_instance *instance)
1841 struct lock_object *lock;
1843 lock = instance->li_lock;
1844 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
1845 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name);
1846 if (lock->lo_witness->w_name != lock->lo_name)
1847 printf(" (%s)", lock->lo_witness->w_name);
1848 printf(" r = %d (%p) locked @ %s:%d\n",
1849 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
1855 witness_thread_has_locks(struct thread *td)
1858 return (td->td_sleeplocks != NULL);
1862 witness_proc_has_locks(struct proc *p)
1866 FOREACH_THREAD_IN_PROC(p, td) {
1867 if (witness_thread_has_locks(td))
1875 witness_list_locks(struct lock_list_entry **lock_list)
1877 struct lock_list_entry *lle;
1881 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
1882 for (i = lle->ll_count - 1; i >= 0; i--) {
1883 witness_list_lock(&lle->ll_children[i]);
1890 * This is a bit risky at best. We call this function when we have timed
1891 * out acquiring a spin lock, and we assume that the other CPU is stuck
1892 * with this lock held. So, we go groveling around in the other CPU's
1893 * per-cpu data to try to find the lock instance for this spin lock to
1894 * see when it was last acquired.
1897 witness_display_spinlock(struct lock_object *lock, struct thread *owner)
1899 struct lock_instance *instance;
1902 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
1904 pc = pcpu_find(owner->td_oncpu);
1905 instance = find_instance(pc->pc_spinlocks, lock);
1906 if (instance != NULL)
1907 witness_list_lock(instance);
1911 witness_save(struct lock_object *lock, const char **filep, int *linep)
1913 struct lock_list_entry *lock_list;
1914 struct lock_instance *instance;
1915 struct lock_class *class;
1917 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1918 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1920 class = LOCK_CLASS(lock);
1921 if (class->lc_flags & LC_SLEEPLOCK)
1922 lock_list = curthread->td_sleeplocks;
1924 if (witness_skipspin)
1926 lock_list = PCPU_GET(spinlocks);
1928 instance = find_instance(lock_list, lock);
1929 if (instance == NULL)
1930 panic("%s: lock (%s) %s not locked", __func__,
1931 class->lc_name, lock->lo_name);
1932 *filep = instance->li_file;
1933 *linep = instance->li_line;
1937 witness_restore(struct lock_object *lock, const char *file, int line)
1939 struct lock_list_entry *lock_list;
1940 struct lock_instance *instance;
1941 struct lock_class *class;
1943 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
1944 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1946 class = LOCK_CLASS(lock);
1947 if (class->lc_flags & LC_SLEEPLOCK)
1948 lock_list = curthread->td_sleeplocks;
1950 if (witness_skipspin)
1952 lock_list = PCPU_GET(spinlocks);
1954 instance = find_instance(lock_list, lock);
1955 if (instance == NULL)
1956 panic("%s: lock (%s) %s not locked", __func__,
1957 class->lc_name, lock->lo_name);
1958 lock->lo_witness->w_file = file;
1959 lock->lo_witness->w_line = line;
1960 instance->li_file = file;
1961 instance->li_line = line;
1965 witness_assert(struct lock_object *lock, int flags, const char *file, int line)
1967 #ifdef INVARIANT_SUPPORT
1968 struct lock_instance *instance;
1969 struct lock_class *class;
1971 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL)
1973 class = LOCK_CLASS(lock);
1974 if ((class->lc_flags & LC_SLEEPLOCK) != 0)
1975 instance = find_instance(curthread->td_sleeplocks, lock);
1976 else if ((class->lc_flags & LC_SPINLOCK) != 0)
1977 instance = find_instance(PCPU_GET(spinlocks), lock);
1979 panic("Lock (%s) %s is not sleep or spin!",
1980 class->lc_name, lock->lo_name);
1982 file = fixup_filename(file);
1985 if (instance != NULL)
1986 panic("Lock (%s) %s locked @ %s:%d.",
1987 class->lc_name, lock->lo_name, file, line);
1990 case LA_LOCKED | LA_RECURSED:
1991 case LA_LOCKED | LA_NOTRECURSED:
1993 case LA_SLOCKED | LA_RECURSED:
1994 case LA_SLOCKED | LA_NOTRECURSED:
1996 case LA_XLOCKED | LA_RECURSED:
1997 case LA_XLOCKED | LA_NOTRECURSED:
1998 if (instance == NULL) {
1999 panic("Lock (%s) %s not locked @ %s:%d.",
2000 class->lc_name, lock->lo_name, file, line);
2003 if ((flags & LA_XLOCKED) != 0 &&
2004 (instance->li_flags & LI_EXCLUSIVE) == 0)
2005 panic("Lock (%s) %s not exclusively locked @ %s:%d.",
2006 class->lc_name, lock->lo_name, file, line);
2007 if ((flags & LA_SLOCKED) != 0 &&
2008 (instance->li_flags & LI_EXCLUSIVE) != 0)
2009 panic("Lock (%s) %s exclusively locked @ %s:%d.",
2010 class->lc_name, lock->lo_name, file, line);
2011 if ((flags & LA_RECURSED) != 0 &&
2012 (instance->li_flags & LI_RECURSEMASK) == 0)
2013 panic("Lock (%s) %s not recursed @ %s:%d.",
2014 class->lc_name, lock->lo_name, file, line);
2015 if ((flags & LA_NOTRECURSED) != 0 &&
2016 (instance->li_flags & LI_RECURSEMASK) != 0)
2017 panic("Lock (%s) %s recursed @ %s:%d.",
2018 class->lc_name, lock->lo_name, file, line);
2021 panic("Invalid lock assertion at %s:%d.", file, line);
2024 #endif /* INVARIANT_SUPPORT */
2029 witness_list(struct thread *td)
2032 KASSERT(!witness_cold, ("%s: witness_cold", __func__));
2033 KASSERT(kdb_active, ("%s: not in the debugger", __func__));
2035 if (witness_watch == 0)
2038 witness_list_locks(&td->td_sleeplocks);
2041 * We only handle spinlocks if td == curthread. This is somewhat broken
2042 * if td is currently executing on some other CPU and holds spin locks
2043 * as we won't display those locks. If we had a MI way of getting
2044 * the per-cpu data for a given cpu then we could use
2045 * td->td_oncpu to get the list of spinlocks for this thread
2048 * That still wouldn't really fix this unless we locked the scheduler
2049 * lock or stopped the other CPU to make sure it wasn't changing the
2050 * list out from under us. It is probably best to just not try to
2051 * handle threads on other CPU's for now.
2053 if (td == curthread && PCPU_GET(spinlocks) != NULL)
2054 witness_list_locks(PCPU_PTR(spinlocks));
2057 DB_SHOW_COMMAND(locks, db_witness_list)
2062 td = db_lookup_thread(addr, TRUE);
2068 DB_SHOW_COMMAND(alllocks, db_witness_list_all)
2074 * It would be nice to list only threads and processes that actually
2075 * held sleep locks, but that information is currently not exported
2078 FOREACH_PROC_IN_SYSTEM(p) {
2079 if (!witness_proc_has_locks(p))
2081 FOREACH_THREAD_IN_PROC(p, td) {
2082 if (!witness_thread_has_locks(td))
2084 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
2085 td->td_name, td, td->td_tid);
2091 DB_SHOW_COMMAND(witness, db_witness_display)
2094 witness_display(db_printf);