2 * Copyright (c) 2008 Isilon Systems, Inc.
3 * Copyright (c) 2008 Ilya Maykov <ivmaykov@gmail.com>
4 * Copyright (c) 1998 Berkeley Software Design, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Berkeley Software Design Inc's name may not be used to endorse or
16 * promote products derived from this software without specific prior
19 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $
32 * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $
36 * Implementation of the `witness' lock verifier. Originally implemented for
37 * mutexes in BSD/OS. Extended to handle generic lock objects and lock
43 * Pronunciation: 'wit-n&s
45 * Etymology: Middle English witnesse, from Old English witnes knowledge,
46 * testimony, witness, from 2wit
47 * Date: before 12th century
48 * 1 : attestation of a fact or event : TESTIMONY
49 * 2 : one that gives evidence; specifically : one who testifies in
50 * a cause or before a judicial tribunal
51 * 3 : one asked to be present at a transaction so as to be able to
52 * testify to its having taken place
53 * 4 : one who has personal knowledge of something
54 * 5 a : something serving as evidence or proof : SIGN
55 * b : public affirmation by word or example of usually
56 * religious faith or conviction <the heroic witness to divine
58 * 6 capitalized : a member of the Jehovah's Witnesses
62 * Special rules concerning Giant and lock orders:
64 * 1) Giant must be acquired before any other mutexes. Stated another way,
65 * no other mutex may be held when Giant is acquired.
67 * 2) Giant must be released when blocking on a sleepable lock.
69 * This rule is less obvious, but is a result of Giant providing the same
70 * semantics as spl(). Basically, when a thread sleeps, it must release
71 * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule
74 * 3) Giant may be acquired before or after sleepable locks.
76 * This rule is also not quite as obvious. Giant may be acquired after
77 * a sleepable lock because it is a non-sleepable lock and non-sleepable
78 * locks may always be acquired while holding a sleepable lock. The second
79 * case, Giant before a sleepable lock, follows from rule 2) above. Suppose
80 * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1
81 * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and
82 * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to
83 * execute. Thus, acquiring Giant both before and after a sleepable lock
84 * will not result in a lock order reversal.
87 #include <sys/cdefs.h>
88 __FBSDID("$FreeBSD$");
91 #include "opt_hwpmc_hooks.h"
92 #include "opt_stack.h"
93 #include "opt_witness.h"
95 #include <sys/param.h>
98 #include <sys/kernel.h>
100 #include <sys/lock.h>
101 #include <sys/malloc.h>
102 #include <sys/mutex.h>
103 #include <sys/priv.h>
104 #include <sys/proc.h>
105 #include <sys/sbuf.h>
106 #include <sys/sched.h>
107 #include <sys/stack.h>
108 #include <sys/sysctl.h>
109 #include <sys/systm.h>
115 #include <machine/stdarg.h>
117 #if !defined(DDB) && !defined(STACK)
118 #error "DDB or STACK options are required for WITNESS"
121 /* Note that these traces do not work with KTR_ALQ. */
123 #define KTR_WITNESS KTR_SUBSYS
125 #define KTR_WITNESS 0
128 #define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */
129 #define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */
131 /* Define this to check for blessed mutexes */
134 #define WITNESS_COUNT 1024
135 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
136 #define WITNESS_HASH_SIZE 251 /* Prime, gives load factor < 2 */
137 #define WITNESS_PENDLIST 512
139 /* Allocate 256 KB of stack data space */
140 #define WITNESS_LO_DATA_COUNT 2048
142 /* Prime, gives load factor of ~2 at full load */
143 #define WITNESS_LO_HASH_SIZE 1021
146 * XXX: This is somewhat bogus, as we assume here that at most 2048 threads
147 * will hold LOCK_NCHILDREN locks. We handle failure ok, and we should
148 * probably be safe for the most part, but it's still a SWAG.
150 #define LOCK_NCHILDREN 5
151 #define LOCK_CHILDCOUNT 2048
153 #define MAX_W_NAME 64
155 #define BADSTACK_SBUF_SIZE (256 * WITNESS_COUNT)
156 #define CYCLEGRAPH_SBUF_SIZE 8192
157 #define FULLGRAPH_SBUF_SIZE 32768
160 * These flags go in the witness relationship matrix and describe the
161 * relationship between any two struct witness objects.
163 #define WITNESS_UNRELATED 0x00 /* No lock order relation. */
164 #define WITNESS_PARENT 0x01 /* Parent, aka direct ancestor. */
165 #define WITNESS_ANCESTOR 0x02 /* Direct or indirect ancestor. */
166 #define WITNESS_CHILD 0x04 /* Child, aka direct descendant. */
167 #define WITNESS_DESCENDANT 0x08 /* Direct or indirect descendant. */
168 #define WITNESS_ANCESTOR_MASK (WITNESS_PARENT | WITNESS_ANCESTOR)
169 #define WITNESS_DESCENDANT_MASK (WITNESS_CHILD | WITNESS_DESCENDANT)
170 #define WITNESS_RELATED_MASK \
171 (WITNESS_ANCESTOR_MASK | WITNESS_DESCENDANT_MASK)
172 #define WITNESS_REVERSAL 0x10 /* A lock order reversal has been
174 #define WITNESS_RESERVED1 0x20 /* Unused flag, reserved. */
175 #define WITNESS_RESERVED2 0x40 /* Unused flag, reserved. */
176 #define WITNESS_LOCK_ORDER_KNOWN 0x80 /* This lock order is known. */
178 /* Descendant to ancestor flags */
179 #define WITNESS_DTOA(x) (((x) & WITNESS_RELATED_MASK) >> 2)
181 /* Ancestor to descendant flags */
182 #define WITNESS_ATOD(x) (((x) & WITNESS_RELATED_MASK) << 2)
184 #define WITNESS_INDEX_ASSERT(i) \
185 MPASS((i) > 0 && (i) <= w_max_used_index && (i) < WITNESS_COUNT)
187 MALLOC_DEFINE(M_WITNESS, "Witness", "Witness");
190 * Lock instances. A lock instance is the data associated with a lock while
191 * it is held by witness. For example, a lock instance will hold the
192 * recursion count of a lock. Lock instances are held in lists. Spin locks
193 * are held in a per-cpu list while sleep locks are held in per-thread list.
195 struct lock_instance {
196 struct lock_object *li_lock;
203 * A simple list type used to build the list of locks held by a thread
204 * or CPU. We can't simply embed the list in struct lock_object since a
205 * lock may be held by more than one thread if it is a shared lock. Locks
206 * are added to the head of the list, so we fill up each list entry from
207 * "the back" logically. To ease some of the arithmetic, we actually fill
208 * in each list entry the normal way (children[0] then children[1], etc.) but
209 * when we traverse the list we read children[count-1] as the first entry
210 * down to children[0] as the final entry.
212 struct lock_list_entry {
213 struct lock_list_entry *ll_next;
214 struct lock_instance ll_children[LOCK_NCHILDREN];
219 * The main witness structure. One of these per named lock type in the system
220 * (for example, "vnode interlock").
223 char w_name[MAX_W_NAME];
224 uint32_t w_index; /* Index in the relationship matrix */
225 struct lock_class *w_class;
226 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */
227 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */
228 struct witness *w_hash_next; /* Linked list in hash buckets. */
229 const char *w_file; /* File where last acquired */
230 uint32_t w_line; /* Line where last acquired */
232 uint16_t w_num_ancestors; /* direct/indirect
234 uint16_t w_num_descendants; /* direct/indirect
235 * descendant count */
241 STAILQ_HEAD(witness_list, witness);
244 * The witness hash table. Keys are witness names (const char *), elements are
245 * witness objects (struct witness *).
247 struct witness_hash {
248 struct witness *wh_array[WITNESS_HASH_SIZE];
254 * Key type for the lock order data hash table.
256 struct witness_lock_order_key {
261 struct witness_lock_order_data {
262 struct stack wlod_stack;
263 struct witness_lock_order_key wlod_key;
264 struct witness_lock_order_data *wlod_next;
268 * The witness lock order data hash table. Keys are witness index tuples
269 * (struct witness_lock_order_key), elements are lock order data objects
270 * (struct witness_lock_order_data).
272 struct witness_lock_order_hash {
273 struct witness_lock_order_data *wloh_array[WITNESS_LO_HASH_SIZE];
279 struct witness_blessed {
285 struct witness_pendhelp {
287 struct lock_object *wh_lock;
290 struct witness_order_list_entry {
292 struct lock_class *w_class;
296 * Returns 0 if one of the locks is a spin lock and the other is not.
297 * Returns 1 otherwise.
300 witness_lock_type_equal(struct witness *w1, struct witness *w2)
303 return ((w1->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) ==
304 (w2->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)));
308 witness_lock_order_key_empty(const struct witness_lock_order_key *key)
311 return (key->from == 0 && key->to == 0);
315 witness_lock_order_key_equal(const struct witness_lock_order_key *a,
316 const struct witness_lock_order_key *b)
319 return (a->from == b->from && a->to == b->to);
322 static int _isitmyx(struct witness *w1, struct witness *w2, int rmask,
325 static void _witness_debugger(int cond, const char *msg);
327 static void adopt(struct witness *parent, struct witness *child);
329 static int blessed(struct witness *, struct witness *);
331 static void depart(struct witness *w);
332 static struct witness *enroll(const char *description,
333 struct lock_class *lock_class);
334 static struct lock_instance *find_instance(struct lock_list_entry *list,
335 struct lock_object *lock);
336 static int isitmychild(struct witness *parent, struct witness *child);
337 static int isitmydescendant(struct witness *parent, struct witness *child);
338 static void itismychild(struct witness *parent, struct witness *child);
339 static int sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS);
340 static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
341 static int sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS);
342 static void witness_add_fullgraph(struct sbuf *sb, struct witness *parent);
344 static void witness_ddb_compute_levels(void);
345 static void witness_ddb_display(void(*)(const char *fmt, ...));
346 static void witness_ddb_display_descendants(void(*)(const char *fmt, ...),
347 struct witness *, int indent);
348 static void witness_ddb_display_list(void(*prnt)(const char *fmt, ...),
349 struct witness_list *list);
350 static void witness_ddb_level_descendants(struct witness *parent, int l);
351 static void witness_ddb_list(struct thread *td);
353 static void witness_free(struct witness *m);
354 static struct witness *witness_get(void);
355 static uint32_t witness_hash_djb2(const uint8_t *key, uint32_t size);
356 static struct witness *witness_hash_get(const char *key);
357 static void witness_hash_put(struct witness *w);
358 static void witness_init_hash_tables(void);
359 static void witness_increment_graph_generation(void);
360 static void witness_lock_list_free(struct lock_list_entry *lle);
361 static struct lock_list_entry *witness_lock_list_get(void);
362 static int witness_lock_order_add(struct witness *parent,
363 struct witness *child);
364 static int witness_lock_order_check(struct witness *parent,
365 struct witness *child);
366 static struct witness_lock_order_data *witness_lock_order_get(
367 struct witness *parent,
368 struct witness *child);
369 static void witness_list_lock(struct lock_instance *instance);
372 #define witness_debugger(c) _witness_debugger(c, __func__)
374 #define witness_debugger(c)
377 SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking");
380 * If set to 0, lock order checking is disabled. If set to -1,
381 * witness is completely disabled. Otherwise witness performs full
382 * lock order checking for all locks. At runtime, lock order checking
383 * may be toggled. However, witness cannot be reenabled once it is
384 * completely disabled.
386 static int witness_watch = 1;
387 TUNABLE_INT("debug.witness.watch", &witness_watch);
388 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
389 sysctl_debug_witness_watch, "I", "witness is watching lock operations");
393 * When KDB is enabled and witness_kdb is 1, it will cause the system
394 * to drop into kdebug() when:
395 * - a lock hierarchy violation occurs
396 * - locks are held when going to sleep.
403 TUNABLE_INT("debug.witness.kdb", &witness_kdb);
404 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, "");
407 * When KDB is enabled and witness_trace is 1, it will cause the system
408 * to print a stack trace:
409 * - a lock hierarchy violation occurs
410 * - locks are held when going to sleep.
412 int witness_trace = 1;
413 TUNABLE_INT("debug.witness.trace", &witness_trace);
414 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, "");
417 #ifdef WITNESS_SKIPSPIN
418 int witness_skipspin = 1;
420 int witness_skipspin = 0;
422 TUNABLE_INT("debug.witness.skipspin", &witness_skipspin);
423 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, &witness_skipspin,
427 * Call this to print out the relations between locks.
429 SYSCTL_PROC(_debug_witness, OID_AUTO, fullgraph, CTLTYPE_STRING | CTLFLAG_RD,
430 NULL, 0, sysctl_debug_witness_fullgraph, "A", "Show locks relation graphs");
433 * Call this to print out the witness faulty stacks.
435 SYSCTL_PROC(_debug_witness, OID_AUTO, badstacks, CTLTYPE_STRING | CTLFLAG_RD,
436 NULL, 0, sysctl_debug_witness_badstacks, "A", "Show bad witness stacks");
438 static struct mtx w_mtx;
441 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
442 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
445 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
446 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
449 static struct lock_list_entry *w_lock_list_free = NULL;
450 static struct witness_pendhelp pending_locks[WITNESS_PENDLIST];
451 static u_int pending_cnt;
453 static int w_free_cnt, w_spin_cnt, w_sleep_cnt;
454 SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, "");
455 SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, "");
456 SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0,
459 static struct witness *w_data;
460 static uint8_t w_rmatrix[WITNESS_COUNT+1][WITNESS_COUNT+1];
461 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
462 static struct witness_hash w_hash; /* The witness hash table. */
464 /* The lock order data hash */
465 static struct witness_lock_order_data w_lodata[WITNESS_LO_DATA_COUNT];
466 static struct witness_lock_order_data *w_lofree = NULL;
467 static struct witness_lock_order_hash w_lohash;
468 static int w_max_used_index = 0;
469 static unsigned int w_generation = 0;
470 static const char *w_notrunning = "Witness not running\n";
471 static const char *w_stillcold = "Witness is still cold\n";
474 static struct witness_order_list_entry order_lists[] = {
478 { "proctree", &lock_class_sx },
479 { "allproc", &lock_class_sx },
480 { "allprison", &lock_class_sx },
485 { "Giant", &lock_class_mtx_sleep },
486 { "pipe mutex", &lock_class_mtx_sleep },
487 { "sigio lock", &lock_class_mtx_sleep },
488 { "process group", &lock_class_mtx_sleep },
489 { "process lock", &lock_class_mtx_sleep },
490 { "session", &lock_class_mtx_sleep },
491 { "uidinfo hash", &lock_class_rw },
493 { "pmc-sleep", &lock_class_mtx_sleep },
499 { "accept", &lock_class_mtx_sleep },
500 { "so_snd", &lock_class_mtx_sleep },
501 { "so_rcv", &lock_class_mtx_sleep },
502 { "sellck", &lock_class_mtx_sleep },
507 { "so_rcv", &lock_class_mtx_sleep },
508 { "radix node head", &lock_class_mtx_sleep },
509 { "rtentry", &lock_class_mtx_sleep },
510 { "ifaddr", &lock_class_mtx_sleep },
513 * Multicast - protocol locks before interface locks, after UDP locks.
515 { "udpinp", &lock_class_rw },
516 { "in_multi_mtx", &lock_class_mtx_sleep },
517 { "igmp_mtx", &lock_class_mtx_sleep },
518 { "if_addr_mtx", &lock_class_mtx_sleep },
521 * UNIX Domain Sockets
523 { "unp", &lock_class_mtx_sleep },
524 { "so_snd", &lock_class_mtx_sleep },
529 { "udp", &lock_class_rw },
530 { "udpinp", &lock_class_rw },
531 { "so_snd", &lock_class_mtx_sleep },
536 { "tcp", &lock_class_rw },
537 { "tcpinp", &lock_class_rw },
538 { "so_snd", &lock_class_mtx_sleep },
543 { "slip_mtx", &lock_class_mtx_sleep },
544 { "slip sc_mtx", &lock_class_mtx_sleep },
549 { "ddp_list_mtx", &lock_class_mtx_sleep },
550 { "ddp_mtx", &lock_class_mtx_sleep },
555 { "bpf global lock", &lock_class_mtx_sleep },
556 { "bpf interface lock", &lock_class_mtx_sleep },
557 { "bpf cdev lock", &lock_class_mtx_sleep },
562 { "nfsd_mtx", &lock_class_mtx_sleep },
563 { "so_snd", &lock_class_mtx_sleep },
569 { "802.11 com lock", &lock_class_mtx_sleep},
574 { "network driver", &lock_class_mtx_sleep},
580 { "ng_node", &lock_class_mtx_sleep },
581 { "ng_worklist", &lock_class_mtx_sleep },
586 { "system map", &lock_class_mtx_sleep },
587 { "vm page queue mutex", &lock_class_mtx_sleep },
588 { "vnode interlock", &lock_class_mtx_sleep },
589 { "cdev", &lock_class_mtx_sleep },
592 * kqueue/VFS interaction
594 { "kqueue", &lock_class_mtx_sleep },
595 { "struct mount mtx", &lock_class_mtx_sleep },
596 { "vnode interlock", &lock_class_mtx_sleep },
602 { "ap boot", &lock_class_mtx_spin },
604 { "rm.mutex_mtx", &lock_class_mtx_spin },
605 { "sio", &lock_class_mtx_spin },
606 { "scrlock", &lock_class_mtx_spin },
608 { "cy", &lock_class_mtx_spin },
611 { "pcib_mtx", &lock_class_mtx_spin },
612 { "rtc_mtx", &lock_class_mtx_spin },
614 { "scc_hwmtx", &lock_class_mtx_spin },
615 { "uart_hwmtx", &lock_class_mtx_spin },
616 { "fast_taskqueue", &lock_class_mtx_spin },
617 { "intr table", &lock_class_mtx_spin },
619 { "pmc-per-proc", &lock_class_mtx_spin },
621 { "process slock", &lock_class_mtx_spin },
622 { "sleepq chain", &lock_class_mtx_spin },
623 { "umtx lock", &lock_class_mtx_spin },
624 { "rm_spinlock", &lock_class_mtx_spin },
625 { "turnstile chain", &lock_class_mtx_spin },
626 { "turnstile lock", &lock_class_mtx_spin },
627 { "sched lock", &lock_class_mtx_spin },
628 { "td_contested", &lock_class_mtx_spin },
629 { "callout", &lock_class_mtx_spin },
630 { "entropy harvest mutex", &lock_class_mtx_spin },
631 { "syscons video lock", &lock_class_mtx_spin },
632 { "time lock", &lock_class_mtx_spin },
634 { "smp rendezvous", &lock_class_mtx_spin },
637 { "tlb0", &lock_class_mtx_spin },
642 { "intrcnt", &lock_class_mtx_spin },
643 { "icu", &lock_class_mtx_spin },
644 #if defined(SMP) && defined(__sparc64__)
645 { "ipi", &lock_class_mtx_spin },
648 { "allpmaps", &lock_class_mtx_spin },
649 { "descriptor tables", &lock_class_mtx_spin },
651 { "clk", &lock_class_mtx_spin },
652 { "cpuset", &lock_class_mtx_spin },
653 { "mprof lock", &lock_class_mtx_spin },
654 { "zombie lock", &lock_class_mtx_spin },
655 { "ALD Queue", &lock_class_mtx_spin },
657 { "MCA spin lock", &lock_class_mtx_spin },
659 #if defined(__i386__) || defined(__amd64__)
660 { "pcicfg", &lock_class_mtx_spin },
661 { "NDIS thread lock", &lock_class_mtx_spin },
663 { "tw_osl_io_lock", &lock_class_mtx_spin },
664 { "tw_osl_q_lock", &lock_class_mtx_spin },
665 { "tw_cl_io_lock", &lock_class_mtx_spin },
666 { "tw_cl_intr_lock", &lock_class_mtx_spin },
667 { "tw_cl_gen_lock", &lock_class_mtx_spin },
669 { "pmc-leaf", &lock_class_mtx_spin },
671 { "blocked lock", &lock_class_mtx_spin },
678 * Pairs of locks which have been blessed
679 * Don't complain about order problems with blessed locks
681 static struct witness_blessed blessed_list[] = {
683 static int blessed_count =
684 sizeof(blessed_list) / sizeof(struct witness_blessed);
688 * This global is set to 0 once it becomes safe to use the witness code.
690 static int witness_cold = 1;
693 * This global is set to 1 once the static lock orders have been enrolled
694 * so that a warning can be issued for any spin locks enrolled later.
696 static int witness_spin_warn = 0;
699 * The WITNESS-enabled diagnostic code. Note that the witness code does
700 * assume that the early boot is single-threaded at least until after this
701 * routine is completed.
704 witness_initialize(void *dummy __unused)
706 struct lock_object *lock;
707 struct witness_order_list_entry *order;
708 struct witness *w, *w1;
711 MALLOC(w_data, struct witness *,
712 sizeof (struct witness) * WITNESS_COUNT, M_WITNESS,
716 * We have to release Giant before initializing its witness
717 * structure so that WITNESS doesn't get confused.
720 mtx_assert(&Giant, MA_NOTOWNED);
722 CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
723 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
724 MTX_NOWITNESS | MTX_NOPROFILE);
725 for (i = WITNESS_COUNT - 1; i >= 0; i--) {
727 memset(w, 0, sizeof(*w));
728 w_data[i].w_index = i; /* Witness index never changes. */
731 KASSERT(STAILQ_FIRST(&w_free)->w_index == 0,
732 ("%s: Invalid list of free witness objects", __func__));
734 /* Witness with index 0 is not used to aid in debugging. */
735 STAILQ_REMOVE_HEAD(&w_free, w_list);
739 (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1)));
741 for (i = 0; i < LOCK_CHILDCOUNT; i++)
742 witness_lock_list_free(&w_locklistdata[i]);
743 witness_init_hash_tables();
745 /* First add in all the specified order lists. */
746 for (order = order_lists; order->w_name != NULL; order++) {
747 w = enroll(order->w_name, order->w_class);
750 w->w_file = "order list";
751 for (order++; order->w_name != NULL; order++) {
752 w1 = enroll(order->w_name, order->w_class);
755 w1->w_file = "order list";
760 witness_spin_warn = 1;
762 /* Iterate through all locks and add them to witness. */
763 for (i = 0; pending_locks[i].wh_lock != NULL; i++) {
764 lock = pending_locks[i].wh_lock;
765 KASSERT(lock->lo_flags & LO_WITNESS,
766 ("%s: lock %s is on pending list but not LO_WITNESS",
767 __func__, lock->lo_name));
768 lock->lo_witness = enroll(pending_locks[i].wh_type,
772 /* Mark the witness code as being ready for use. */
777 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize,
781 witness_init(struct lock_object *lock, const char *type)
783 struct lock_class *class;
785 /* Various sanity checks. */
786 class = LOCK_CLASS(lock);
787 if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
788 (class->lc_flags & LC_RECURSABLE) == 0)
789 panic("%s: lock (%s) %s can not be recursable", __func__,
790 class->lc_name, lock->lo_name);
791 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
792 (class->lc_flags & LC_SLEEPABLE) == 0)
793 panic("%s: lock (%s) %s can not be sleepable", __func__,
794 class->lc_name, lock->lo_name);
795 if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
796 (class->lc_flags & LC_UPGRADABLE) == 0)
797 panic("%s: lock (%s) %s can not be upgradable", __func__,
798 class->lc_name, lock->lo_name);
801 * If we shouldn't watch this lock, then just clear lo_witness.
802 * Otherwise, if witness_cold is set, then it is too early to
803 * enroll this lock, so defer it to witness_initialize() by adding
804 * it to the pending_locks list. If it is not too early, then enroll
807 if (witness_watch < 1 || panicstr != NULL ||
808 (lock->lo_flags & LO_WITNESS) == 0)
809 lock->lo_witness = NULL;
810 else if (witness_cold) {
811 pending_locks[pending_cnt].wh_lock = lock;
812 pending_locks[pending_cnt++].wh_type = type;
813 if (pending_cnt > WITNESS_PENDLIST)
814 panic("%s: pending locks list is too small, bump it\n",
817 lock->lo_witness = enroll(type, class);
821 witness_destroy(struct lock_object *lock)
823 struct lock_class *class;
826 class = LOCK_CLASS(lock);
829 panic("lock (%s) %s destroyed while witness_cold",
830 class->lc_name, lock->lo_name);
832 /* XXX: need to verify that no one holds the lock */
833 if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL)
835 w = lock->lo_witness;
837 mtx_lock_spin(&w_mtx);
838 MPASS(w->w_refcount > 0);
841 if (w->w_refcount == 0)
843 mtx_unlock_spin(&w_mtx);
848 witness_ddb_compute_levels(void)
853 * First clear all levels.
855 STAILQ_FOREACH(w, &w_all, w_list)
859 * Look for locks with no parents and level all their descendants.
861 STAILQ_FOREACH(w, &w_all, w_list) {
863 /* If the witness has ancestors (is not a root), skip it. */
864 if (w->w_num_ancestors > 0)
866 witness_ddb_level_descendants(w, 0);
871 witness_ddb_level_descendants(struct witness *w, int l)
875 if (w->w_ddb_level >= l)
881 for (i = 1; i <= w_max_used_index; i++) {
882 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT)
883 witness_ddb_level_descendants(&w_data[i], l);
888 witness_ddb_display_descendants(void(*prnt)(const char *fmt, ...),
889 struct witness *w, int indent)
893 for (i = 0; i < indent; i++)
895 prnt("%s (type: %s, depth: %d, active refs: %d)",
896 w->w_name, w->w_class->lc_name,
897 w->w_ddb_level, w->w_refcount);
898 if (w->w_displayed) {
899 prnt(" -- (already displayed)\n");
903 if (w->w_file != NULL && w->w_line != 0)
904 prnt(" -- last acquired @ %s:%d\n", w->w_file,
907 prnt(" -- never acquired\n");
909 WITNESS_INDEX_ASSERT(w->w_index);
910 for (i = 1; i <= w_max_used_index; i++) {
911 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT)
912 witness_ddb_display_descendants(prnt, &w_data[i],
918 witness_ddb_display_list(void(*prnt)(const char *fmt, ...),
919 struct witness_list *list)
923 STAILQ_FOREACH(w, list, w_typelist) {
924 if (w->w_file == NULL || w->w_ddb_level > 0)
927 /* This lock has no anscestors - display its descendants. */
928 witness_ddb_display_descendants(prnt, w, 0);
933 witness_ddb_display(void(*prnt)(const char *fmt, ...))
937 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
938 witness_ddb_compute_levels();
940 /* Clear all the displayed flags. */
941 STAILQ_FOREACH(w, &w_all, w_list)
945 * First, handle sleep locks which have been acquired at least
948 prnt("Sleep locks:\n");
949 witness_ddb_display_list(prnt, &w_sleep);
952 * Now do spin locks which have been acquired at least once.
954 prnt("\nSpin locks:\n");
955 witness_ddb_display_list(prnt, &w_spin);
958 * Finally, any locks which have not been acquired yet.
960 prnt("\nLocks which were never acquired:\n");
961 STAILQ_FOREACH(w, &w_all, w_list) {
962 if (w->w_file != NULL || w->w_refcount == 0)
964 prnt("%s (type: %s, depth: %d)\n", w->w_name,
965 w->w_class->lc_name, w->w_ddb_level);
970 /* Trim useless garbage from filenames. */
972 fixup_filename(const char *file)
977 while (strncmp(file, "../", 3) == 0)
983 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
986 if (witness_watch == -1 || panicstr != NULL)
989 /* Require locks that witness knows about. */
990 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
991 lock2->lo_witness == NULL)
994 mtx_assert(&w_mtx, MA_NOTOWNED);
995 mtx_lock_spin(&w_mtx);
998 * If we already have either an explicit or implied lock order that
999 * is the other way around, then return an error.
1001 if (witness_watch &&
1002 isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
1003 mtx_unlock_spin(&w_mtx);
1007 /* Try to add the new order. */
1008 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1009 lock2->lo_witness->w_name, lock1->lo_witness->w_name);
1010 itismychild(lock1->lo_witness, lock2->lo_witness);
1011 mtx_unlock_spin(&w_mtx);
1016 witness_checkorder(struct lock_object *lock, int flags, const char *file,
1017 int line, struct lock_object *interlock)
1019 struct lock_list_entry *lock_list, *lle;
1020 struct lock_instance *lock1, *lock2, *plock;
1021 struct lock_class *class;
1022 struct witness *w, *w1;
1026 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL ||
1030 w = lock->lo_witness;
1031 class = LOCK_CLASS(lock);
1033 file = fixup_filename(file);
1035 if (class->lc_flags & LC_SLEEPLOCK) {
1038 * Since spin locks include a critical section, this check
1039 * implicitly enforces a lock order of all sleep locks before
1042 if (td->td_critnest != 0 && !kdb_active)
1043 panic("blockable sleep lock (%s) %s @ %s:%d",
1044 class->lc_name, lock->lo_name, file, line);
1047 * If this is the first lock acquired then just return as
1048 * no order checking is needed.
1050 lock_list = td->td_sleeplocks;
1051 if (lock_list == NULL || lock_list->ll_count == 0)
1056 * If this is the first lock, just return as no order
1057 * checking is needed. Avoid problems with thread
1058 * migration pinning the thread while checking if
1059 * spinlocks are held. If at least one spinlock is held
1060 * the thread is in a safe path and it is allowed to
1064 lock_list = PCPU_GET(spinlocks);
1065 if (lock_list == NULL || lock_list->ll_count == 0) {
1073 * Check to see if we are recursing on a lock we already own. If
1074 * so, make sure that we don't mismatch exclusive and shared lock
1077 lock1 = find_instance(lock_list, lock);
1078 if (lock1 != NULL) {
1079 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
1080 (flags & LOP_EXCLUSIVE) == 0) {
1081 printf("shared lock of (%s) %s @ %s:%d\n",
1082 class->lc_name, lock->lo_name, file, line);
1083 printf("while exclusively locked from %s:%d\n",
1084 lock1->li_file, lock1->li_line);
1085 panic("share->excl");
1087 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
1088 (flags & LOP_EXCLUSIVE) != 0) {
1089 printf("exclusive lock of (%s) %s @ %s:%d\n",
1090 class->lc_name, lock->lo_name, file, line);
1091 printf("while share locked from %s:%d\n",
1092 lock1->li_file, lock1->li_line);
1093 panic("excl->share");
1099 * Find the previously acquired lock, but ignore interlocks.
1101 plock = &lock_list->ll_children[lock_list->ll_count - 1];
1102 if (interlock != NULL && plock->li_lock == interlock) {
1103 if (lock_list->ll_count > 1)
1105 &lock_list->ll_children[lock_list->ll_count - 2];
1107 lle = lock_list->ll_next;
1110 * The interlock is the only lock we hold, so
1115 plock = &lle->ll_children[lle->ll_count - 1];
1120 * Try to perform most checks without a lock. If this succeeds we
1121 * can skip acquiring the lock and return success.
1123 w1 = plock->li_lock->lo_witness;
1124 if (witness_lock_order_check(w1, w))
1128 * Check for duplicate locks of the same type. Note that we only
1129 * have to check for this on the last lock we just acquired. Any
1130 * other cases will be caught as lock order violations.
1132 mtx_lock_spin(&w_mtx);
1133 witness_lock_order_add(w1, w);
1136 if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) &&
1137 !(w_rmatrix[i][i] & WITNESS_REVERSAL)) {
1138 w_rmatrix[i][i] |= WITNESS_REVERSAL;
1140 mtx_unlock_spin(&w_mtx);
1142 "acquiring duplicate lock of same type: \"%s\"\n",
1144 printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name,
1145 plock->li_file, plock->li_line);
1146 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line);
1147 witness_debugger(1);
1149 mtx_unlock_spin(&w_mtx);
1152 mtx_assert(&w_mtx, MA_OWNED);
1155 * If we know that the the lock we are acquiring comes after
1156 * the lock we most recently acquired in the lock order tree,
1157 * then there is no need for any further checks.
1159 if (isitmychild(w1, w))
1162 for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) {
1163 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
1165 MPASS(j < WITNESS_COUNT);
1166 lock1 = &lle->ll_children[i];
1169 * Ignore the interlock the first time we see it.
1171 if (interlock != NULL && interlock == lock1->li_lock) {
1177 * If this lock doesn't undergo witness checking,
1180 w1 = lock1->li_lock->lo_witness;
1182 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
1183 ("lock missing witness structure"));
1188 * If we are locking Giant and this is a sleepable
1189 * lock, then skip it.
1191 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
1192 lock == &Giant.lock_object)
1196 * If we are locking a sleepable lock and this lock
1197 * is Giant, then skip it.
1199 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1200 lock1->li_lock == &Giant.lock_object)
1204 * If we are locking a sleepable lock and this lock
1205 * isn't sleepable, we want to treat it as a lock
1206 * order violation to enfore a general lock order of
1207 * sleepable locks before non-sleepable locks.
1209 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1210 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1214 * If we are locking Giant and this is a non-sleepable
1215 * lock, then treat it as a reversal.
1217 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 &&
1218 lock == &Giant.lock_object)
1222 * Check the lock order hierarchy for a reveresal.
1224 if (!isitmydescendant(w, w1))
1229 * We have a lock order violation, check to see if it
1230 * is allowed or has already been yelled about.
1235 * If the lock order is blessed, just bail. We don't
1236 * look for other lock order violations though, which
1243 /* Bail if this violation is known */
1244 if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL)
1247 /* Record this as a violation */
1248 w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL;
1249 w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL;
1250 w->w_reversed = w1->w_reversed = 1;
1251 witness_increment_graph_generation();
1252 mtx_unlock_spin(&w_mtx);
1255 * Ok, yell about it.
1257 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1258 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1260 "lock order reversal: (sleepable after non-sleepable)\n");
1261 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0
1262 && lock == &Giant.lock_object)
1264 "lock order reversal: (Giant after non-sleepable)\n");
1266 printf("lock order reversal:\n");
1269 * Try to locate an earlier lock with
1270 * witness w in our list.
1273 lock2 = &lle->ll_children[i];
1274 MPASS(lock2->li_lock != NULL);
1275 if (lock2->li_lock->lo_witness == w)
1277 if (i == 0 && lle->ll_next != NULL) {
1279 i = lle->ll_count - 1;
1280 MPASS(i >= 0 && i < LOCK_NCHILDREN);
1285 printf(" 1st %p %s (%s) @ %s:%d\n",
1286 lock1->li_lock, lock1->li_lock->lo_name,
1287 w1->w_name, lock1->li_file, lock1->li_line);
1288 printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
1289 lock->lo_name, w->w_name, file, line);
1291 printf(" 1st %p %s (%s) @ %s:%d\n",
1292 lock2->li_lock, lock2->li_lock->lo_name,
1293 lock2->li_lock->lo_witness->w_name,
1294 lock2->li_file, lock2->li_line);
1295 printf(" 2nd %p %s (%s) @ %s:%d\n",
1296 lock1->li_lock, lock1->li_lock->lo_name,
1297 w1->w_name, lock1->li_file, lock1->li_line);
1298 printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
1299 lock->lo_name, w->w_name, file, line);
1301 witness_debugger(1);
1307 * If requested, build a new lock order. However, don't build a new
1308 * relationship between a sleepable lock and Giant if it is in the
1309 * wrong direction. The correct lock order is that sleepable locks
1310 * always come before Giant.
1312 if (flags & LOP_NEWORDER &&
1313 !(plock->li_lock == &Giant.lock_object &&
1314 (lock->lo_flags & LO_SLEEPABLE) != 0)) {
1315 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1316 w->w_name, plock->li_lock->lo_witness->w_name);
1317 itismychild(plock->li_lock->lo_witness, w);
1320 mtx_unlock_spin(&w_mtx);
1324 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
1326 struct lock_list_entry **lock_list, *lle;
1327 struct lock_instance *instance;
1331 if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL ||
1334 w = lock->lo_witness;
1336 file = fixup_filename(file);
1338 /* Determine lock list for this lock. */
1339 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK)
1340 lock_list = &td->td_sleeplocks;
1342 lock_list = PCPU_PTR(spinlocks);
1344 /* Check to see if we are recursing on a lock we already own. */
1345 instance = find_instance(*lock_list, lock);
1346 if (instance != NULL) {
1347 instance->li_flags++;
1348 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
1349 td->td_proc->p_pid, lock->lo_name,
1350 instance->li_flags & LI_RECURSEMASK);
1351 instance->li_file = file;
1352 instance->li_line = line;
1356 /* Update per-witness last file and line acquire. */
1360 /* Find the next open lock instance in the list and fill it. */
1362 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1363 lle = witness_lock_list_get();
1366 lle->ll_next = *lock_list;
1367 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1368 td->td_proc->p_pid, lle);
1371 instance = &lle->ll_children[lle->ll_count++];
1372 instance->li_lock = lock;
1373 instance->li_line = line;
1374 instance->li_file = file;
1375 if ((flags & LOP_EXCLUSIVE) != 0)
1376 instance->li_flags = LI_EXCLUSIVE;
1378 instance->li_flags = 0;
1379 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1380 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1384 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1386 struct lock_instance *instance;
1387 struct lock_class *class;
1389 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
1390 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
1392 class = LOCK_CLASS(lock);
1393 file = fixup_filename(file);
1394 if (witness_watch) {
1395 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1396 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1397 class->lc_name, lock->lo_name, file, line);
1398 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1399 panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1400 class->lc_name, lock->lo_name, file, line);
1402 instance = find_instance(curthread->td_sleeplocks, lock);
1403 if (instance == NULL)
1404 panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1405 class->lc_name, lock->lo_name, file, line);
1406 if (witness_watch) {
1407 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1408 panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1409 class->lc_name, lock->lo_name, file, line);
1410 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1411 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1412 class->lc_name, lock->lo_name,
1413 instance->li_flags & LI_RECURSEMASK, file, line);
1415 instance->li_flags |= LI_EXCLUSIVE;
1419 witness_downgrade(struct lock_object *lock, int flags, const char *file,
1422 struct lock_instance *instance;
1423 struct lock_class *class;
1425 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
1426 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
1428 class = LOCK_CLASS(lock);
1429 file = fixup_filename(file);
1430 if (witness_watch) {
1431 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1432 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1433 class->lc_name, lock->lo_name, file, line);
1434 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1435 panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1436 class->lc_name, lock->lo_name, file, line);
1438 instance = find_instance(curthread->td_sleeplocks, lock);
1439 if (instance == NULL)
1440 panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1441 class->lc_name, lock->lo_name, file, line);
1442 if (witness_watch) {
1443 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1444 panic("downgrade of shared lock (%s) %s @ %s:%d",
1445 class->lc_name, lock->lo_name, file, line);
1446 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1447 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1448 class->lc_name, lock->lo_name,
1449 instance->li_flags & LI_RECURSEMASK, file, line);
1451 instance->li_flags &= ~LI_EXCLUSIVE;
1455 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1457 struct lock_list_entry **lock_list, *lle;
1458 struct lock_instance *instance;
1459 struct lock_class *class;
1464 if (witness_cold || lock->lo_witness == NULL || panicstr != NULL)
1467 class = LOCK_CLASS(lock);
1468 file = fixup_filename(file);
1470 /* Find lock instance associated with this lock. */
1471 if (class->lc_flags & LC_SLEEPLOCK)
1472 lock_list = &td->td_sleeplocks;
1474 lock_list = PCPU_PTR(spinlocks);
1476 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1477 for (i = 0; i < (*lock_list)->ll_count; i++) {
1478 instance = &(*lock_list)->ll_children[i];
1479 if (instance->li_lock == lock)
1484 * When disabling WITNESS through witness_watch we could end up in
1485 * having registered locks in the td_sleeplocks queue.
1486 * We have to make sure we flush these queues, so just search for
1487 * eventual register locks and remove them.
1489 if (witness_watch > 0)
1490 panic("lock (%s) %s not locked @ %s:%d", class->lc_name,
1491 lock->lo_name, file, line);
1496 /* First, check for shared/exclusive mismatches. */
1497 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 &&
1498 (flags & LOP_EXCLUSIVE) == 0) {
1499 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1500 lock->lo_name, file, line);
1501 printf("while exclusively locked from %s:%d\n",
1502 instance->li_file, instance->li_line);
1503 panic("excl->ushare");
1505 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 &&
1506 (flags & LOP_EXCLUSIVE) != 0) {
1507 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1508 lock->lo_name, file, line);
1509 printf("while share locked from %s:%d\n", instance->li_file,
1511 panic("share->uexcl");
1514 /* If we are recursed, unrecurse. */
1515 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1516 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1517 td->td_proc->p_pid, instance->li_lock->lo_name,
1518 instance->li_flags);
1519 instance->li_flags--;
1523 /* Otherwise, remove this item from the list. */
1525 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1526 td->td_proc->p_pid, instance->li_lock->lo_name,
1527 (*lock_list)->ll_count - 1);
1528 for (j = i; j < (*lock_list)->ll_count - 1; j++)
1529 (*lock_list)->ll_children[j] =
1530 (*lock_list)->ll_children[j + 1];
1531 (*lock_list)->ll_count--;
1535 * In order to reduce contention on w_mtx, we want to keep always an
1536 * head object into lists so that frequent allocation from the
1537 * free witness pool (and subsequent locking) is avoided.
1538 * In order to maintain the current code simple, when the head
1539 * object is totally unloaded it means also that we do not have
1540 * further objects in the list, so the list ownership needs to be
1541 * hand over to another object if the current head needs to be freed.
1543 if ((*lock_list)->ll_count == 0) {
1544 if (*lock_list == lle) {
1545 if (lle->ll_next == NULL)
1549 *lock_list = lle->ll_next;
1550 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1551 td->td_proc->p_pid, lle);
1552 witness_lock_list_free(lle);
1557 witness_thread_exit(struct thread *td)
1559 struct lock_list_entry *lle;
1562 lle = td->td_sleeplocks;
1563 if (lle == NULL || panicstr != NULL)
1565 if (lle->ll_count != 0) {
1566 for (n = 0; lle != NULL; lle = lle->ll_next)
1567 for (i = lle->ll_count - 1; i >= 0; i--) {
1569 printf("Thread %p exiting with the following locks held:\n",
1572 witness_list_lock(&lle->ll_children[i]);
1575 panic("Thread %p cannot exit while holding sleeplocks\n", td);
1577 witness_lock_list_free(lle);
1581 * Warn if any locks other than 'lock' are held. Flags can be passed in to
1582 * exempt Giant and sleepable locks from the checks as well. If any
1583 * non-exempt locks are held, then a supplied message is printed to the
1584 * console along with a list of the offending locks. If indicated in the
1585 * flags then a failure results in a panic as well.
1588 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1590 struct lock_list_entry *lock_list, *lle;
1591 struct lock_instance *lock1;
1596 if (witness_cold || witness_watch < 1 || panicstr != NULL)
1600 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1601 for (i = lle->ll_count - 1; i >= 0; i--) {
1602 lock1 = &lle->ll_children[i];
1603 if (lock1->li_lock == lock)
1605 if (flags & WARN_GIANTOK &&
1606 lock1->li_lock == &Giant.lock_object)
1608 if (flags & WARN_SLEEPOK &&
1609 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1615 printf(" with the following");
1616 if (flags & WARN_SLEEPOK)
1617 printf(" non-sleepable");
1618 printf(" locks held:\n");
1621 witness_list_lock(lock1);
1625 * Pin the thread in order to avoid problems with thread migration.
1626 * Once that all verifies are passed about spinlocks ownership,
1627 * the thread is in a safe path and it can be unpinned.
1630 lock_list = PCPU_GET(spinlocks);
1631 if (lock_list != NULL) {
1634 if (lock_list->ll_count == 0) {
1641 * We should only have one spinlock and as long as
1642 * the flags cannot match for this locks class,
1643 * check if the first spinlock is the one curthread
1646 lock1 = &lock_list->ll_children[lock_list->ll_count - 1];
1647 if (lock1->li_lock == lock)
1654 printf(" with the following");
1655 if (flags & WARN_SLEEPOK)
1656 printf(" non-sleepable");
1657 printf(" locks held:\n");
1659 n += witness_list_locks(&lock_list);
1662 if (flags & WARN_PANIC && n)
1663 panic("%s", __func__);
1665 witness_debugger(n);
1670 witness_file(struct lock_object *lock)
1674 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL)
1676 w = lock->lo_witness;
1681 witness_line(struct lock_object *lock)
1685 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL)
1687 w = lock->lo_witness;
1691 static struct witness *
1692 enroll(const char *description, struct lock_class *lock_class)
1695 struct witness_list *typelist;
1697 MPASS(description != NULL);
1699 if (witness_watch == -1 || panicstr != NULL)
1701 if ((lock_class->lc_flags & LC_SPINLOCK)) {
1702 if (witness_skipspin)
1706 } else if ((lock_class->lc_flags & LC_SLEEPLOCK))
1707 typelist = &w_sleep;
1709 panic("lock class %s is not sleep or spin",
1710 lock_class->lc_name);
1712 mtx_lock_spin(&w_mtx);
1713 w = witness_hash_get(description);
1716 if ((w = witness_get()) == NULL)
1718 MPASS(strlen(description) < MAX_W_NAME);
1719 strcpy(w->w_name, description);
1720 w->w_class = lock_class;
1722 STAILQ_INSERT_HEAD(&w_all, w, w_list);
1723 if (lock_class->lc_flags & LC_SPINLOCK) {
1724 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1726 } else if (lock_class->lc_flags & LC_SLEEPLOCK) {
1727 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1731 /* Insert new witness into the hash */
1732 witness_hash_put(w);
1733 witness_increment_graph_generation();
1734 mtx_unlock_spin(&w_mtx);
1738 mtx_unlock_spin(&w_mtx);
1739 if (lock_class != w->w_class)
1741 "lock (%s) %s does not match earlier (%s) lock",
1742 description, lock_class->lc_name,
1743 w->w_class->lc_name);
1748 depart(struct witness *w)
1750 struct witness_list *list;
1752 MPASS(w->w_refcount == 0);
1753 if (w->w_class->lc_flags & LC_SLEEPLOCK) {
1761 * Set file to NULL as it may point into a loadable module.
1765 witness_increment_graph_generation();
1770 adopt(struct witness *parent, struct witness *child)
1774 if (witness_cold == 0)
1775 mtx_assert(&w_mtx, MA_OWNED);
1777 /* If the relationship is already known, there's no work to be done. */
1778 if (isitmychild(parent, child))
1781 /* When the structure of the graph changes, bump up the generation. */
1782 witness_increment_graph_generation();
1785 * The hard part ... create the direct relationship, then propagate all
1786 * indirect relationships.
1788 pi = parent->w_index;
1789 ci = child->w_index;
1790 WITNESS_INDEX_ASSERT(pi);
1791 WITNESS_INDEX_ASSERT(ci);
1793 w_rmatrix[pi][ci] |= WITNESS_PARENT;
1794 w_rmatrix[ci][pi] |= WITNESS_CHILD;
1797 * If parent was not already an ancestor of child,
1798 * then we increment the descendant and ancestor counters.
1800 if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) {
1801 parent->w_num_descendants++;
1802 child->w_num_ancestors++;
1806 * Find each ancestor of 'pi'. Note that 'pi' itself is counted as
1807 * an ancestor of 'pi' during this loop.
1809 for (i = 1; i <= w_max_used_index; i++) {
1810 if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 &&
1814 /* Find each descendant of 'i' and mark it as a descendant. */
1815 for (j = 1; j <= w_max_used_index; j++) {
1818 * Skip children that are already marked as
1819 * descendants of 'i'.
1821 if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK)
1825 * We are only interested in descendants of 'ci'. Note
1826 * that 'ci' itself is counted as a descendant of 'ci'.
1828 if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 &&
1831 w_rmatrix[i][j] |= WITNESS_ANCESTOR;
1832 w_rmatrix[j][i] |= WITNESS_DESCENDANT;
1833 w_data[i].w_num_descendants++;
1834 w_data[j].w_num_ancestors++;
1837 * Make sure we aren't marking a node as both an
1838 * ancestor and descendant. We should have caught
1839 * this as a lock order reversal earlier.
1841 if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) &&
1842 (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) {
1843 printf("witness rmatrix paradox! [%d][%d]=%d "
1844 "both ancestor and descendant\n",
1845 i, j, w_rmatrix[i][j]);
1847 printf("Witness disabled.\n");
1850 if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) &&
1851 (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) {
1852 printf("witness rmatrix paradox! [%d][%d]=%d "
1853 "both ancestor and descendant\n",
1854 j, i, w_rmatrix[j][i]);
1856 printf("Witness disabled.\n");
1864 itismychild(struct witness *parent, struct witness *child)
1867 MPASS(child != NULL && parent != NULL);
1868 if (witness_cold == 0)
1869 mtx_assert(&w_mtx, MA_OWNED);
1871 if (!witness_lock_type_equal(parent, child)) {
1872 if (witness_cold == 0)
1873 mtx_unlock_spin(&w_mtx);
1874 panic("%s: parent \"%s\" (%s) and child \"%s\" (%s) are not "
1875 "the same lock type", __func__, parent->w_name,
1876 parent->w_class->lc_name, child->w_name,
1877 child->w_class->lc_name);
1879 adopt(parent, child);
1883 * Generic code for the isitmy*() functions. The rmask parameter is the
1884 * expected relationship of w1 to w2.
1887 _isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname)
1889 unsigned char r1, r2;
1894 WITNESS_INDEX_ASSERT(i1);
1895 WITNESS_INDEX_ASSERT(i2);
1896 r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK;
1897 r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK;
1899 /* The flags on one better be the inverse of the flags on the other */
1900 if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) ||
1901 (WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) {
1902 printf("%s: rmatrix mismatch between %s (index %d) and %s "
1903 "(index %d): w_rmatrix[%d][%d] == %hhx but "
1904 "w_rmatrix[%d][%d] == %hhx\n",
1905 fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1,
1908 printf("Witness disabled.\n");
1911 return (r1 & rmask);
1915 * Checks if @child is a direct child of @parent.
1918 isitmychild(struct witness *parent, struct witness *child)
1921 return (_isitmyx(parent, child, WITNESS_PARENT, __func__));
1925 * Checks if @descendant is a direct or inderect descendant of @ancestor.
1928 isitmydescendant(struct witness *ancestor, struct witness *descendant)
1931 return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK,
1937 blessed(struct witness *w1, struct witness *w2)
1940 struct witness_blessed *b;
1942 for (i = 0; i < blessed_count; i++) {
1943 b = &blessed_list[i];
1944 if (strcmp(w1->w_name, b->b_lock1) == 0) {
1945 if (strcmp(w2->w_name, b->b_lock2) == 0)
1949 if (strcmp(w1->w_name, b->b_lock2) == 0)
1950 if (strcmp(w2->w_name, b->b_lock1) == 0)
1957 static struct witness *
1963 if (witness_cold == 0)
1964 mtx_assert(&w_mtx, MA_OWNED);
1966 if (witness_watch == -1) {
1967 mtx_unlock_spin(&w_mtx);
1970 if (STAILQ_EMPTY(&w_free)) {
1972 mtx_unlock_spin(&w_mtx);
1973 printf("WITNESS: unable to allocate a new witness object\n");
1976 w = STAILQ_FIRST(&w_free);
1977 STAILQ_REMOVE_HEAD(&w_free, w_list);
1980 MPASS(index > 0 && index == w_max_used_index+1 &&
1981 index < WITNESS_COUNT);
1982 bzero(w, sizeof(*w));
1984 if (index > w_max_used_index)
1985 w_max_used_index = index;
1990 witness_free(struct witness *w)
1993 STAILQ_INSERT_HEAD(&w_free, w, w_list);
1997 static struct lock_list_entry *
1998 witness_lock_list_get(void)
2000 struct lock_list_entry *lle;
2002 if (witness_watch == -1)
2004 mtx_lock_spin(&w_mtx);
2005 lle = w_lock_list_free;
2008 mtx_unlock_spin(&w_mtx);
2009 printf("%s: witness exhausted\n", __func__);
2012 w_lock_list_free = lle->ll_next;
2013 mtx_unlock_spin(&w_mtx);
2014 bzero(lle, sizeof(*lle));
2019 witness_lock_list_free(struct lock_list_entry *lle)
2022 mtx_lock_spin(&w_mtx);
2023 lle->ll_next = w_lock_list_free;
2024 w_lock_list_free = lle;
2025 mtx_unlock_spin(&w_mtx);
2028 static struct lock_instance *
2029 find_instance(struct lock_list_entry *list, struct lock_object *lock)
2031 struct lock_list_entry *lle;
2032 struct lock_instance *instance;
2035 for (lle = list; lle != NULL; lle = lle->ll_next)
2036 for (i = lle->ll_count - 1; i >= 0; i--) {
2037 instance = &lle->ll_children[i];
2038 if (instance->li_lock == lock)
2045 witness_list_lock(struct lock_instance *instance)
2047 struct lock_object *lock;
2049 lock = instance->li_lock;
2050 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
2051 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name);
2052 if (lock->lo_witness->w_name != lock->lo_name)
2053 printf(" (%s)", lock->lo_witness->w_name);
2054 printf(" r = %d (%p) locked @ %s:%d\n",
2055 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file,
2061 witness_thread_has_locks(struct thread *td)
2064 if (td->td_sleeplocks == NULL)
2066 return (td->td_sleeplocks->ll_count != 0);
2070 witness_proc_has_locks(struct proc *p)
2074 FOREACH_THREAD_IN_PROC(p, td) {
2075 if (witness_thread_has_locks(td))
2083 witness_list_locks(struct lock_list_entry **lock_list)
2085 struct lock_list_entry *lle;
2089 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
2090 for (i = lle->ll_count - 1; i >= 0; i--) {
2091 witness_list_lock(&lle->ll_children[i]);
2098 * This is a bit risky at best. We call this function when we have timed
2099 * out acquiring a spin lock, and we assume that the other CPU is stuck
2100 * with this lock held. So, we go groveling around in the other CPU's
2101 * per-cpu data to try to find the lock instance for this spin lock to
2102 * see when it was last acquired.
2105 witness_display_spinlock(struct lock_object *lock, struct thread *owner)
2107 struct lock_instance *instance;
2110 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
2112 pc = pcpu_find(owner->td_oncpu);
2113 instance = find_instance(pc->pc_spinlocks, lock);
2114 if (instance != NULL)
2115 witness_list_lock(instance);
2119 witness_save(struct lock_object *lock, const char **filep, int *linep)
2121 struct lock_list_entry *lock_list;
2122 struct lock_instance *instance;
2123 struct lock_class *class;
2125 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2126 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2128 class = LOCK_CLASS(lock);
2129 if (class->lc_flags & LC_SLEEPLOCK)
2130 lock_list = curthread->td_sleeplocks;
2132 if (witness_skipspin)
2134 lock_list = PCPU_GET(spinlocks);
2136 instance = find_instance(lock_list, lock);
2137 if (instance == NULL)
2138 panic("%s: lock (%s) %s not locked", __func__,
2139 class->lc_name, lock->lo_name);
2140 *filep = instance->li_file;
2141 *linep = instance->li_line;
2145 witness_restore(struct lock_object *lock, const char *file, int line)
2147 struct lock_list_entry *lock_list;
2148 struct lock_instance *instance;
2149 struct lock_class *class;
2151 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2152 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2154 class = LOCK_CLASS(lock);
2155 if (class->lc_flags & LC_SLEEPLOCK)
2156 lock_list = curthread->td_sleeplocks;
2158 if (witness_skipspin)
2160 lock_list = PCPU_GET(spinlocks);
2162 instance = find_instance(lock_list, lock);
2163 if (instance == NULL)
2164 panic("%s: lock (%s) %s not locked", __func__,
2165 class->lc_name, lock->lo_name);
2166 lock->lo_witness->w_file = file;
2167 lock->lo_witness->w_line = line;
2168 instance->li_file = file;
2169 instance->li_line = line;
2173 witness_assert(struct lock_object *lock, int flags, const char *file, int line)
2175 #ifdef INVARIANT_SUPPORT
2176 struct lock_instance *instance;
2177 struct lock_class *class;
2179 if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL)
2181 class = LOCK_CLASS(lock);
2182 if ((class->lc_flags & LC_SLEEPLOCK) != 0)
2183 instance = find_instance(curthread->td_sleeplocks, lock);
2184 else if ((class->lc_flags & LC_SPINLOCK) != 0)
2185 instance = find_instance(PCPU_GET(spinlocks), lock);
2187 panic("Lock (%s) %s is not sleep or spin!",
2188 class->lc_name, lock->lo_name);
2190 file = fixup_filename(file);
2193 if (instance != NULL)
2194 panic("Lock (%s) %s locked @ %s:%d.",
2195 class->lc_name, lock->lo_name, file, line);
2198 case LA_LOCKED | LA_RECURSED:
2199 case LA_LOCKED | LA_NOTRECURSED:
2201 case LA_SLOCKED | LA_RECURSED:
2202 case LA_SLOCKED | LA_NOTRECURSED:
2204 case LA_XLOCKED | LA_RECURSED:
2205 case LA_XLOCKED | LA_NOTRECURSED:
2206 if (instance == NULL) {
2207 panic("Lock (%s) %s not locked @ %s:%d.",
2208 class->lc_name, lock->lo_name, file, line);
2211 if ((flags & LA_XLOCKED) != 0 &&
2212 (instance->li_flags & LI_EXCLUSIVE) == 0)
2213 panic("Lock (%s) %s not exclusively locked @ %s:%d.",
2214 class->lc_name, lock->lo_name, file, line);
2215 if ((flags & LA_SLOCKED) != 0 &&
2216 (instance->li_flags & LI_EXCLUSIVE) != 0)
2217 panic("Lock (%s) %s exclusively locked @ %s:%d.",
2218 class->lc_name, lock->lo_name, file, line);
2219 if ((flags & LA_RECURSED) != 0 &&
2220 (instance->li_flags & LI_RECURSEMASK) == 0)
2221 panic("Lock (%s) %s not recursed @ %s:%d.",
2222 class->lc_name, lock->lo_name, file, line);
2223 if ((flags & LA_NOTRECURSED) != 0 &&
2224 (instance->li_flags & LI_RECURSEMASK) != 0)
2225 panic("Lock (%s) %s recursed @ %s:%d.",
2226 class->lc_name, lock->lo_name, file, line);
2229 panic("Invalid lock assertion at %s:%d.", file, line);
2232 #endif /* INVARIANT_SUPPORT */
2237 witness_ddb_list(struct thread *td)
2240 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2241 KASSERT(kdb_active, ("%s: not in the debugger", __func__));
2243 if (witness_watch < 1)
2246 witness_list_locks(&td->td_sleeplocks);
2249 * We only handle spinlocks if td == curthread. This is somewhat broken
2250 * if td is currently executing on some other CPU and holds spin locks
2251 * as we won't display those locks. If we had a MI way of getting
2252 * the per-cpu data for a given cpu then we could use
2253 * td->td_oncpu to get the list of spinlocks for this thread
2256 * That still wouldn't really fix this unless we locked the scheduler
2257 * lock or stopped the other CPU to make sure it wasn't changing the
2258 * list out from under us. It is probably best to just not try to
2259 * handle threads on other CPU's for now.
2261 if (td == curthread && PCPU_GET(spinlocks) != NULL)
2262 witness_list_locks(PCPU_PTR(spinlocks));
2265 DB_SHOW_COMMAND(locks, db_witness_list)
2270 td = db_lookup_thread(addr, TRUE);
2273 witness_ddb_list(td);
2276 DB_SHOW_ALL_COMMAND(locks, db_witness_list_all)
2282 * It would be nice to list only threads and processes that actually
2283 * held sleep locks, but that information is currently not exported
2286 FOREACH_PROC_IN_SYSTEM(p) {
2287 if (!witness_proc_has_locks(p))
2289 FOREACH_THREAD_IN_PROC(p, td) {
2290 if (!witness_thread_has_locks(td))
2292 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
2293 p->p_comm, td, td->td_tid);
2294 witness_ddb_list(td);
2298 DB_SHOW_ALIAS(alllocks, db_witness_list_all)
2300 DB_SHOW_COMMAND(witness, db_witness_display)
2303 witness_ddb_display(db_printf);
2308 sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS)
2310 struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2;
2311 struct witness *tmp_w1, *tmp_w2, *w1, *w2;
2313 u_int w_rmatrix1, w_rmatrix2;
2314 int error, generation, i, j;
2320 if (witness_watch < 1) {
2321 error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning));
2325 error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold));
2329 sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND);
2333 /* Allocate and init temporary storage space. */
2334 tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO);
2335 tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO);
2336 tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP,
2338 tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP,
2340 stack_zero(&tmp_data1->wlod_stack);
2341 stack_zero(&tmp_data2->wlod_stack);
2344 mtx_lock_spin(&w_mtx);
2345 generation = w_generation;
2346 mtx_unlock_spin(&w_mtx);
2347 sbuf_printf(sb, "Number of known direct relationships is %d\n",
2348 w_lohash.wloh_count);
2349 for (i = 1; i < w_max_used_index; i++) {
2350 mtx_lock_spin(&w_mtx);
2351 if (generation != w_generation) {
2352 mtx_unlock_spin(&w_mtx);
2354 /* The graph has changed, try again. */
2361 if (w1->w_reversed == 0) {
2362 mtx_unlock_spin(&w_mtx);
2366 /* Copy w1 locally so we can release the spin lock. */
2368 mtx_unlock_spin(&w_mtx);
2370 if (tmp_w1->w_reversed == 0)
2372 for (j = 1; j < w_max_used_index; j++) {
2373 if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j)
2376 mtx_lock_spin(&w_mtx);
2377 if (generation != w_generation) {
2378 mtx_unlock_spin(&w_mtx);
2380 /* The graph has changed, try again. */
2387 data1 = witness_lock_order_get(w1, w2);
2388 data2 = witness_lock_order_get(w2, w1);
2391 * Copy information locally so we can release the
2395 w_rmatrix1 = (unsigned int)w_rmatrix[i][j];
2396 w_rmatrix2 = (unsigned int)w_rmatrix[j][i];
2399 stack_zero(&tmp_data1->wlod_stack);
2400 stack_copy(&data1->wlod_stack,
2401 &tmp_data1->wlod_stack);
2403 if (data2 && data2 != data1) {
2404 stack_zero(&tmp_data2->wlod_stack);
2405 stack_copy(&data2->wlod_stack,
2406 &tmp_data2->wlod_stack);
2408 mtx_unlock_spin(&w_mtx);
2411 "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n",
2412 tmp_w1->w_name, tmp_w1->w_class->lc_name,
2413 tmp_w2->w_name, tmp_w2->w_class->lc_name);
2416 "w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n",
2417 tmp_w1->name, tmp_w2->w_name, w_rmatrix1,
2418 tmp_w2->name, tmp_w1->w_name, w_rmatrix2);
2422 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n",
2423 tmp_w1->w_name, tmp_w1->w_class->lc_name,
2424 tmp_w2->w_name, tmp_w2->w_class->lc_name);
2425 stack_sbuf_print(sb, &tmp_data1->wlod_stack);
2426 sbuf_printf(sb, "\n");
2428 if (data2 && data2 != data1) {
2430 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n",
2431 tmp_w2->w_name, tmp_w2->w_class->lc_name,
2432 tmp_w1->w_name, tmp_w1->w_class->lc_name);
2433 stack_sbuf_print(sb, &tmp_data2->wlod_stack);
2434 sbuf_printf(sb, "\n");
2438 mtx_lock_spin(&w_mtx);
2439 if (generation != w_generation) {
2440 mtx_unlock_spin(&w_mtx);
2443 * The graph changed while we were printing stack data,
2450 mtx_unlock_spin(&w_mtx);
2452 /* Free temporary storage space. */
2453 free(tmp_data1, M_TEMP);
2454 free(tmp_data2, M_TEMP);
2455 free(tmp_w1, M_TEMP);
2456 free(tmp_w2, M_TEMP);
2459 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2466 sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS)
2472 if (witness_watch < 1) {
2473 error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning));
2477 error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold));
2481 sb = sbuf_new(NULL, NULL, FULLGRAPH_SBUF_SIZE, SBUF_FIXEDLEN);
2484 sbuf_printf(sb, "\n");
2486 mtx_lock_spin(&w_mtx);
2487 STAILQ_FOREACH(w, &w_all, w_list)
2489 STAILQ_FOREACH(w, &w_all, w_list)
2490 witness_add_fullgraph(sb, w);
2491 mtx_unlock_spin(&w_mtx);
2494 * While using SBUF_FIXEDLEN, check if the sbuf overflowed.
2496 if (sbuf_overflowed(sb)) {
2498 panic("%s: sbuf overflowed, bump FULLGRAPH_SBUF_SIZE value\n",
2503 * Close the sbuf and return to userland.
2506 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2513 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
2517 value = witness_watch;
2518 error = sysctl_handle_int(oidp, &value, 0, req);
2519 if (error != 0 || req->newptr == NULL)
2521 if (value > 1 || value < -1 ||
2522 (witness_watch == -1 && value != witness_watch))
2524 witness_watch = value;
2529 witness_add_fullgraph(struct sbuf *sb, struct witness *w)
2533 if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0))
2537 WITNESS_INDEX_ASSERT(w->w_index);
2538 for (i = 1; i <= w_max_used_index; i++) {
2539 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) {
2540 sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name,
2542 witness_add_fullgraph(sb, &w_data[i]);
2548 * A simple hash function. Takes a key pointer and a key size. If size == 0,
2549 * interprets the key as a string and reads until the null
2550 * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit
2551 * hash value computed from the key.
2554 witness_hash_djb2(const uint8_t *key, uint32_t size)
2556 unsigned int hash = 5381;
2559 /* hash = hash * 33 + key[i] */
2561 for (i = 0; i < size; i++)
2562 hash = ((hash << 5) + hash) + (unsigned int)key[i];
2564 for (i = 0; key[i] != 0; i++)
2565 hash = ((hash << 5) + hash) + (unsigned int)key[i];
2572 * Initializes the two witness hash tables. Called exactly once from
2573 * witness_initialize().
2576 witness_init_hash_tables(void)
2580 MPASS(witness_cold);
2582 /* Initialize the hash tables. */
2583 for (i = 0; i < WITNESS_HASH_SIZE; i++)
2584 w_hash.wh_array[i] = NULL;
2586 w_hash.wh_size = WITNESS_HASH_SIZE;
2587 w_hash.wh_count = 0;
2589 /* Initialize the lock order data hash. */
2591 for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) {
2592 memset(&w_lodata[i], 0, sizeof(w_lodata[i]));
2593 w_lodata[i].wlod_next = w_lofree;
2594 w_lofree = &w_lodata[i];
2596 w_lohash.wloh_size = WITNESS_LO_HASH_SIZE;
2597 w_lohash.wloh_count = 0;
2598 for (i = 0; i < WITNESS_LO_HASH_SIZE; i++)
2599 w_lohash.wloh_array[i] = NULL;
2602 static struct witness *
2603 witness_hash_get(const char *key)
2609 if (witness_cold == 0)
2610 mtx_assert(&w_mtx, MA_OWNED);
2611 hash = witness_hash_djb2(key, 0) % w_hash.wh_size;
2612 w = w_hash.wh_array[hash];
2614 if (strcmp(w->w_name, key) == 0)
2624 witness_hash_put(struct witness *w)
2629 MPASS(w->w_name != NULL);
2630 if (witness_cold == 0)
2631 mtx_assert(&w_mtx, MA_OWNED);
2632 KASSERT(witness_hash_get(w->w_name) == NULL,
2633 ("%s: trying to add a hash entry that already exists!", __func__));
2634 KASSERT(w->w_hash_next == NULL,
2635 ("%s: w->w_hash_next != NULL", __func__));
2637 hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size;
2638 w->w_hash_next = w_hash.wh_array[hash];
2639 w_hash.wh_array[hash] = w;
2644 static struct witness_lock_order_data *
2645 witness_lock_order_get(struct witness *parent, struct witness *child)
2647 struct witness_lock_order_data *data = NULL;
2648 struct witness_lock_order_key key;
2651 MPASS(parent != NULL && child != NULL);
2652 key.from = parent->w_index;
2653 key.to = child->w_index;
2654 WITNESS_INDEX_ASSERT(key.from);
2655 WITNESS_INDEX_ASSERT(key.to);
2656 if ((w_rmatrix[parent->w_index][child->w_index]
2657 & WITNESS_LOCK_ORDER_KNOWN) == 0)
2660 hash = witness_hash_djb2((const char*)&key,
2661 sizeof(key)) % w_lohash.wloh_size;
2662 data = w_lohash.wloh_array[hash];
2663 while (data != NULL) {
2664 if (witness_lock_order_key_equal(&data->wlod_key, &key))
2666 data = data->wlod_next;
2674 * Verify that parent and child have a known relationship, are not the same,
2675 * and child is actually a child of parent. This is done without w_mtx
2676 * to avoid contention in the common case.
2679 witness_lock_order_check(struct witness *parent, struct witness *child)
2682 if (parent != child &&
2683 w_rmatrix[parent->w_index][child->w_index]
2684 & WITNESS_LOCK_ORDER_KNOWN &&
2685 isitmychild(parent, child))
2692 witness_lock_order_add(struct witness *parent, struct witness *child)
2694 struct witness_lock_order_data *data = NULL;
2695 struct witness_lock_order_key key;
2698 MPASS(parent != NULL && child != NULL);
2699 key.from = parent->w_index;
2700 key.to = child->w_index;
2701 WITNESS_INDEX_ASSERT(key.from);
2702 WITNESS_INDEX_ASSERT(key.to);
2703 if (w_rmatrix[parent->w_index][child->w_index]
2704 & WITNESS_LOCK_ORDER_KNOWN)
2707 hash = witness_hash_djb2((const char*)&key,
2708 sizeof(key)) % w_lohash.wloh_size;
2709 w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN;
2713 w_lofree = data->wlod_next;
2714 data->wlod_next = w_lohash.wloh_array[hash];
2715 data->wlod_key = key;
2716 w_lohash.wloh_array[hash] = data;
2717 w_lohash.wloh_count++;
2718 stack_zero(&data->wlod_stack);
2719 stack_save(&data->wlod_stack);
2723 /* Call this whenver the structure of the witness graph changes. */
2725 witness_increment_graph_generation(void)
2728 if (witness_cold == 0)
2729 mtx_assert(&w_mtx, MA_OWNED);
2735 _witness_debugger(int cond, const char *msg)
2738 if (witness_trace && cond)
2740 if (witness_kdb && cond)
2741 kdb_enter(KDB_WHY_WITNESS, msg);