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. */
130 #define LI_NORELEASE 0x00020000 /* Lock not allowed to be released. */
132 /* Define this to check for blessed mutexes */
135 #define WITNESS_COUNT 1536
136 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
137 #define WITNESS_HASH_SIZE 251 /* Prime, gives load factor < 2 */
138 #define WITNESS_PENDLIST (1024 + MAXCPU)
140 /* Allocate 256 KB of stack data space */
141 #define WITNESS_LO_DATA_COUNT 2048
143 /* Prime, gives load factor of ~2 at full load */
144 #define WITNESS_LO_HASH_SIZE 1021
147 * XXX: This is somewhat bogus, as we assume here that at most 2048 threads
148 * will hold LOCK_NCHILDREN locks. We handle failure ok, and we should
149 * probably be safe for the most part, but it's still a SWAG.
151 #define LOCK_NCHILDREN 5
152 #define LOCK_CHILDCOUNT 2048
154 #define MAX_W_NAME 64
156 #define BADSTACK_SBUF_SIZE (256 * WITNESS_COUNT)
157 #define FULLGRAPH_SBUF_SIZE 512
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 static 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 */
237 unsigned w_displayed:1;
238 unsigned w_reversed:1;
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_equal(const struct witness_lock_order_key *a,
309 const struct witness_lock_order_key *b)
312 return (a->from == b->from && a->to == b->to);
315 static int _isitmyx(struct witness *w1, struct witness *w2, int rmask,
318 static void _witness_debugger(int cond, const char *msg);
320 static void adopt(struct witness *parent, struct witness *child);
322 static int blessed(struct witness *, struct witness *);
324 static void depart(struct witness *w);
325 static struct witness *enroll(const char *description,
326 struct lock_class *lock_class);
327 static struct lock_instance *find_instance(struct lock_list_entry *list,
328 const struct lock_object *lock);
329 static int isitmychild(struct witness *parent, struct witness *child);
330 static int isitmydescendant(struct witness *parent, struct witness *child);
331 static void itismychild(struct witness *parent, struct witness *child);
332 static int sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS);
333 static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS);
334 static int sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS);
335 static void witness_add_fullgraph(struct sbuf *sb, struct witness *parent);
337 static void witness_ddb_compute_levels(void);
338 static void witness_ddb_display(int(*)(const char *fmt, ...));
339 static void witness_ddb_display_descendants(int(*)(const char *fmt, ...),
340 struct witness *, int indent);
341 static void witness_ddb_display_list(int(*prnt)(const char *fmt, ...),
342 struct witness_list *list);
343 static void witness_ddb_level_descendants(struct witness *parent, int l);
344 static void witness_ddb_list(struct thread *td);
346 static void witness_free(struct witness *m);
347 static struct witness *witness_get(void);
348 static uint32_t witness_hash_djb2(const uint8_t *key, uint32_t size);
349 static struct witness *witness_hash_get(const char *key);
350 static void witness_hash_put(struct witness *w);
351 static void witness_init_hash_tables(void);
352 static void witness_increment_graph_generation(void);
353 static void witness_lock_list_free(struct lock_list_entry *lle);
354 static struct lock_list_entry *witness_lock_list_get(void);
355 static int witness_lock_order_add(struct witness *parent,
356 struct witness *child);
357 static int witness_lock_order_check(struct witness *parent,
358 struct witness *child);
359 static struct witness_lock_order_data *witness_lock_order_get(
360 struct witness *parent,
361 struct witness *child);
362 static void witness_list_lock(struct lock_instance *instance,
363 int (*prnt)(const char *fmt, ...));
364 static void witness_setflag(struct lock_object *lock, int flag, int set);
367 #define witness_debugger(c) _witness_debugger(c, __func__)
369 #define witness_debugger(c)
372 static SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, NULL,
376 * If set to 0, lock order checking is disabled. If set to -1,
377 * witness is completely disabled. Otherwise witness performs full
378 * lock order checking for all locks. At runtime, lock order checking
379 * may be toggled. However, witness cannot be reenabled once it is
380 * completely disabled.
382 static int witness_watch = 1;
383 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RWTUN | CTLTYPE_INT, NULL, 0,
384 sysctl_debug_witness_watch, "I", "witness is watching lock operations");
388 * When KDB is enabled and witness_kdb is 1, it will cause the system
389 * to drop into kdebug() when:
390 * - a lock hierarchy violation occurs
391 * - locks are held when going to sleep.
398 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RWTUN, &witness_kdb, 0, "");
401 * When KDB is enabled and witness_trace is 1, it will cause the system
402 * to print a stack trace:
403 * - a lock hierarchy violation occurs
404 * - locks are held when going to sleep.
406 int witness_trace = 1;
407 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RWTUN, &witness_trace, 0, "");
410 #ifdef WITNESS_SKIPSPIN
411 int witness_skipspin = 1;
413 int witness_skipspin = 0;
415 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, &witness_skipspin, 0, "");
418 * Call this to print out the relations between locks.
420 SYSCTL_PROC(_debug_witness, OID_AUTO, fullgraph, CTLTYPE_STRING | CTLFLAG_RD,
421 NULL, 0, sysctl_debug_witness_fullgraph, "A", "Show locks relation graphs");
424 * Call this to print out the witness faulty stacks.
426 SYSCTL_PROC(_debug_witness, OID_AUTO, badstacks, CTLTYPE_STRING | CTLFLAG_RD,
427 NULL, 0, sysctl_debug_witness_badstacks, "A", "Show bad witness stacks");
429 static struct mtx w_mtx;
432 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
433 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
436 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
437 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
440 static struct lock_list_entry *w_lock_list_free = NULL;
441 static struct witness_pendhelp pending_locks[WITNESS_PENDLIST];
442 static u_int pending_cnt;
444 static int w_free_cnt, w_spin_cnt, w_sleep_cnt;
445 SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, "");
446 SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, "");
447 SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0,
450 static struct witness *w_data;
451 static uint8_t w_rmatrix[WITNESS_COUNT+1][WITNESS_COUNT+1];
452 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
453 static struct witness_hash w_hash; /* The witness hash table. */
455 /* The lock order data hash */
456 static struct witness_lock_order_data w_lodata[WITNESS_LO_DATA_COUNT];
457 static struct witness_lock_order_data *w_lofree = NULL;
458 static struct witness_lock_order_hash w_lohash;
459 static int w_max_used_index = 0;
460 static unsigned int w_generation = 0;
461 static const char w_notrunning[] = "Witness not running\n";
462 static const char w_stillcold[] = "Witness is still cold\n";
465 static struct witness_order_list_entry order_lists[] = {
469 { "proctree", &lock_class_sx },
470 { "allproc", &lock_class_sx },
471 { "allprison", &lock_class_sx },
476 { "Giant", &lock_class_mtx_sleep },
477 { "pipe mutex", &lock_class_mtx_sleep },
478 { "sigio lock", &lock_class_mtx_sleep },
479 { "process group", &lock_class_mtx_sleep },
480 { "process lock", &lock_class_mtx_sleep },
481 { "session", &lock_class_mtx_sleep },
482 { "uidinfo hash", &lock_class_rw },
484 { "pmc-sleep", &lock_class_mtx_sleep },
486 { "time lock", &lock_class_mtx_sleep },
491 { "accept", &lock_class_mtx_sleep },
492 { "so_snd", &lock_class_mtx_sleep },
493 { "so_rcv", &lock_class_mtx_sleep },
494 { "sellck", &lock_class_mtx_sleep },
499 { "so_rcv", &lock_class_mtx_sleep },
500 { "radix node head", &lock_class_rw },
501 { "rtentry", &lock_class_mtx_sleep },
502 { "ifaddr", &lock_class_mtx_sleep },
506 * protocol locks before interface locks, after UDP locks.
508 { "udpinp", &lock_class_rw },
509 { "in_multi_mtx", &lock_class_mtx_sleep },
510 { "igmp_mtx", &lock_class_mtx_sleep },
511 { "if_addr_lock", &lock_class_rw },
515 * protocol locks before interface locks, after UDP locks.
517 { "udpinp", &lock_class_rw },
518 { "in6_multi_mtx", &lock_class_mtx_sleep },
519 { "mld_mtx", &lock_class_mtx_sleep },
520 { "if_addr_lock", &lock_class_rw },
523 * UNIX Domain Sockets
525 { "unp_global_rwlock", &lock_class_rw },
526 { "unp_list_lock", &lock_class_mtx_sleep },
527 { "unp", &lock_class_mtx_sleep },
528 { "so_snd", &lock_class_mtx_sleep },
533 { "udp", &lock_class_rw },
534 { "udpinp", &lock_class_rw },
535 { "so_snd", &lock_class_mtx_sleep },
540 { "tcp", &lock_class_rw },
541 { "tcpinp", &lock_class_rw },
542 { "so_snd", &lock_class_mtx_sleep },
547 { "bpf global lock", &lock_class_mtx_sleep },
548 { "bpf interface lock", &lock_class_rw },
549 { "bpf cdev lock", &lock_class_mtx_sleep },
554 { "nfsd_mtx", &lock_class_mtx_sleep },
555 { "so_snd", &lock_class_mtx_sleep },
561 { "802.11 com lock", &lock_class_mtx_sleep},
566 { "network driver", &lock_class_mtx_sleep},
572 { "ng_node", &lock_class_mtx_sleep },
573 { "ng_worklist", &lock_class_mtx_sleep },
578 { "vm map (system)", &lock_class_mtx_sleep },
579 { "vm page queue", &lock_class_mtx_sleep },
580 { "vnode interlock", &lock_class_mtx_sleep },
581 { "cdev", &lock_class_mtx_sleep },
586 { "vm map (user)", &lock_class_sx },
587 { "vm object", &lock_class_rw },
588 { "vm page", &lock_class_mtx_sleep },
589 { "vm page queue", &lock_class_mtx_sleep },
590 { "pmap pv global", &lock_class_rw },
591 { "pmap", &lock_class_mtx_sleep },
592 { "pmap pv list", &lock_class_rw },
593 { "vm page free queue", &lock_class_mtx_sleep },
596 * kqueue/VFS interaction
598 { "kqueue", &lock_class_mtx_sleep },
599 { "struct mount mtx", &lock_class_mtx_sleep },
600 { "vnode interlock", &lock_class_mtx_sleep },
605 { "dn->dn_mtx", &lock_class_sx },
606 { "dr->dt.di.dr_mtx", &lock_class_sx },
607 { "db->db_mtx", &lock_class_sx },
613 { "ap boot", &lock_class_mtx_spin },
615 { "rm.mutex_mtx", &lock_class_mtx_spin },
616 { "sio", &lock_class_mtx_spin },
617 { "scrlock", &lock_class_mtx_spin },
619 { "cy", &lock_class_mtx_spin },
622 { "pcib_mtx", &lock_class_mtx_spin },
623 { "rtc_mtx", &lock_class_mtx_spin },
625 { "scc_hwmtx", &lock_class_mtx_spin },
626 { "uart_hwmtx", &lock_class_mtx_spin },
627 { "fast_taskqueue", &lock_class_mtx_spin },
628 { "intr table", &lock_class_mtx_spin },
630 { "pmc-per-proc", &lock_class_mtx_spin },
632 { "process slock", &lock_class_mtx_spin },
633 { "sleepq chain", &lock_class_mtx_spin },
634 { "umtx lock", &lock_class_mtx_spin },
635 { "rm_spinlock", &lock_class_mtx_spin },
636 { "turnstile chain", &lock_class_mtx_spin },
637 { "turnstile lock", &lock_class_mtx_spin },
638 { "sched lock", &lock_class_mtx_spin },
639 { "td_contested", &lock_class_mtx_spin },
640 { "callout", &lock_class_mtx_spin },
641 { "entropy harvest mutex", &lock_class_mtx_spin },
642 { "syscons video lock", &lock_class_mtx_spin },
644 { "smp rendezvous", &lock_class_mtx_spin },
647 { "tlb0", &lock_class_mtx_spin },
652 { "intrcnt", &lock_class_mtx_spin },
653 { "icu", &lock_class_mtx_spin },
655 { "allpmaps", &lock_class_mtx_spin },
656 { "descriptor tables", &lock_class_mtx_spin },
658 { "clk", &lock_class_mtx_spin },
659 { "cpuset", &lock_class_mtx_spin },
660 { "mprof lock", &lock_class_mtx_spin },
661 { "zombie lock", &lock_class_mtx_spin },
662 { "ALD Queue", &lock_class_mtx_spin },
663 #if defined(__i386__) || defined(__amd64__)
664 { "pcicfg", &lock_class_mtx_spin },
665 { "NDIS thread lock", &lock_class_mtx_spin },
667 { "tw_osl_io_lock", &lock_class_mtx_spin },
668 { "tw_osl_q_lock", &lock_class_mtx_spin },
669 { "tw_cl_io_lock", &lock_class_mtx_spin },
670 { "tw_cl_intr_lock", &lock_class_mtx_spin },
671 { "tw_cl_gen_lock", &lock_class_mtx_spin },
673 { "pmc-leaf", &lock_class_mtx_spin },
675 { "blocked lock", &lock_class_mtx_spin },
682 * Pairs of locks which have been blessed
683 * Don't complain about order problems with blessed locks
685 static struct witness_blessed blessed_list[] = {
687 static int blessed_count =
688 sizeof(blessed_list) / sizeof(struct witness_blessed);
692 * This global is set to 0 once it becomes safe to use the witness code.
694 static int witness_cold = 1;
697 * This global is set to 1 once the static lock orders have been enrolled
698 * so that a warning can be issued for any spin locks enrolled later.
700 static int witness_spin_warn = 0;
702 /* Trim useless garbage from filenames. */
704 fixup_filename(const char *file)
709 while (strncmp(file, "../", 3) == 0)
715 * The WITNESS-enabled diagnostic code. Note that the witness code does
716 * assume that the early boot is single-threaded at least until after this
717 * routine is completed.
720 witness_initialize(void *dummy __unused)
722 struct lock_object *lock;
723 struct witness_order_list_entry *order;
724 struct witness *w, *w1;
727 w_data = malloc(sizeof (struct witness) * WITNESS_COUNT, M_WITNESS,
731 * We have to release Giant before initializing its witness
732 * structure so that WITNESS doesn't get confused.
735 mtx_assert(&Giant, MA_NOTOWNED);
737 CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
738 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
739 MTX_NOWITNESS | MTX_NOPROFILE);
740 for (i = WITNESS_COUNT - 1; i >= 0; i--) {
742 memset(w, 0, sizeof(*w));
743 w_data[i].w_index = i; /* Witness index never changes. */
746 KASSERT(STAILQ_FIRST(&w_free)->w_index == 0,
747 ("%s: Invalid list of free witness objects", __func__));
749 /* Witness with index 0 is not used to aid in debugging. */
750 STAILQ_REMOVE_HEAD(&w_free, w_list);
754 (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1)));
756 for (i = 0; i < LOCK_CHILDCOUNT; i++)
757 witness_lock_list_free(&w_locklistdata[i]);
758 witness_init_hash_tables();
760 /* First add in all the specified order lists. */
761 for (order = order_lists; order->w_name != NULL; order++) {
762 w = enroll(order->w_name, order->w_class);
765 w->w_file = "order list";
766 for (order++; order->w_name != NULL; order++) {
767 w1 = enroll(order->w_name, order->w_class);
770 w1->w_file = "order list";
775 witness_spin_warn = 1;
777 /* Iterate through all locks and add them to witness. */
778 for (i = 0; pending_locks[i].wh_lock != NULL; i++) {
779 lock = pending_locks[i].wh_lock;
780 KASSERT(lock->lo_flags & LO_WITNESS,
781 ("%s: lock %s is on pending list but not LO_WITNESS",
782 __func__, lock->lo_name));
783 lock->lo_witness = enroll(pending_locks[i].wh_type,
787 /* Mark the witness code as being ready for use. */
792 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize,
796 witness_init(struct lock_object *lock, const char *type)
798 struct lock_class *class;
800 /* Various sanity checks. */
801 class = LOCK_CLASS(lock);
802 if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
803 (class->lc_flags & LC_RECURSABLE) == 0)
804 kassert_panic("%s: lock (%s) %s can not be recursable",
805 __func__, class->lc_name, lock->lo_name);
806 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
807 (class->lc_flags & LC_SLEEPABLE) == 0)
808 kassert_panic("%s: lock (%s) %s can not be sleepable",
809 __func__, class->lc_name, lock->lo_name);
810 if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
811 (class->lc_flags & LC_UPGRADABLE) == 0)
812 kassert_panic("%s: lock (%s) %s can not be upgradable",
813 __func__, class->lc_name, lock->lo_name);
816 * If we shouldn't watch this lock, then just clear lo_witness.
817 * Otherwise, if witness_cold is set, then it is too early to
818 * enroll this lock, so defer it to witness_initialize() by adding
819 * it to the pending_locks list. If it is not too early, then enroll
822 if (witness_watch < 1 || panicstr != NULL ||
823 (lock->lo_flags & LO_WITNESS) == 0)
824 lock->lo_witness = NULL;
825 else if (witness_cold) {
826 pending_locks[pending_cnt].wh_lock = lock;
827 pending_locks[pending_cnt++].wh_type = type;
828 if (pending_cnt > WITNESS_PENDLIST)
829 panic("%s: pending locks list is too small, "
830 "increase WITNESS_PENDLIST\n",
833 lock->lo_witness = enroll(type, class);
837 witness_destroy(struct lock_object *lock)
839 struct lock_class *class;
842 class = LOCK_CLASS(lock);
845 panic("lock (%s) %s destroyed while witness_cold",
846 class->lc_name, lock->lo_name);
848 /* XXX: need to verify that no one holds the lock */
849 if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL)
851 w = lock->lo_witness;
853 mtx_lock_spin(&w_mtx);
854 MPASS(w->w_refcount > 0);
857 if (w->w_refcount == 0)
859 mtx_unlock_spin(&w_mtx);
864 witness_ddb_compute_levels(void)
869 * First clear all levels.
871 STAILQ_FOREACH(w, &w_all, w_list)
875 * Look for locks with no parents and level all their descendants.
877 STAILQ_FOREACH(w, &w_all, w_list) {
879 /* If the witness has ancestors (is not a root), skip it. */
880 if (w->w_num_ancestors > 0)
882 witness_ddb_level_descendants(w, 0);
887 witness_ddb_level_descendants(struct witness *w, int l)
891 if (w->w_ddb_level >= l)
897 for (i = 1; i <= w_max_used_index; i++) {
898 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT)
899 witness_ddb_level_descendants(&w_data[i], l);
904 witness_ddb_display_descendants(int(*prnt)(const char *fmt, ...),
905 struct witness *w, int indent)
909 for (i = 0; i < indent; i++)
911 prnt("%s (type: %s, depth: %d, active refs: %d)",
912 w->w_name, w->w_class->lc_name,
913 w->w_ddb_level, w->w_refcount);
914 if (w->w_displayed) {
915 prnt(" -- (already displayed)\n");
919 if (w->w_file != NULL && w->w_line != 0)
920 prnt(" -- last acquired @ %s:%d\n", fixup_filename(w->w_file),
923 prnt(" -- never acquired\n");
925 WITNESS_INDEX_ASSERT(w->w_index);
926 for (i = 1; i <= w_max_used_index; i++) {
929 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT)
930 witness_ddb_display_descendants(prnt, &w_data[i],
936 witness_ddb_display_list(int(*prnt)(const char *fmt, ...),
937 struct witness_list *list)
941 STAILQ_FOREACH(w, list, w_typelist) {
942 if (w->w_file == NULL || w->w_ddb_level > 0)
945 /* This lock has no anscestors - display its descendants. */
946 witness_ddb_display_descendants(prnt, w, 0);
953 witness_ddb_display(int(*prnt)(const char *fmt, ...))
957 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
958 witness_ddb_compute_levels();
960 /* Clear all the displayed flags. */
961 STAILQ_FOREACH(w, &w_all, w_list)
965 * First, handle sleep locks which have been acquired at least
968 prnt("Sleep locks:\n");
969 witness_ddb_display_list(prnt, &w_sleep);
974 * Now do spin locks which have been acquired at least once.
976 prnt("\nSpin locks:\n");
977 witness_ddb_display_list(prnt, &w_spin);
982 * Finally, any locks which have not been acquired yet.
984 prnt("\nLocks which were never acquired:\n");
985 STAILQ_FOREACH(w, &w_all, w_list) {
986 if (w->w_file != NULL || w->w_refcount == 0)
988 prnt("%s (type: %s, depth: %d)\n", w->w_name,
989 w->w_class->lc_name, w->w_ddb_level);
997 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
1000 if (witness_watch == -1 || panicstr != NULL)
1003 /* Require locks that witness knows about. */
1004 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
1005 lock2->lo_witness == NULL)
1008 mtx_assert(&w_mtx, MA_NOTOWNED);
1009 mtx_lock_spin(&w_mtx);
1012 * If we already have either an explicit or implied lock order that
1013 * is the other way around, then return an error.
1015 if (witness_watch &&
1016 isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
1017 mtx_unlock_spin(&w_mtx);
1021 /* Try to add the new order. */
1022 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1023 lock2->lo_witness->w_name, lock1->lo_witness->w_name);
1024 itismychild(lock1->lo_witness, lock2->lo_witness);
1025 mtx_unlock_spin(&w_mtx);
1030 witness_checkorder(struct lock_object *lock, int flags, const char *file,
1031 int line, struct lock_object *interlock)
1033 struct lock_list_entry *lock_list, *lle;
1034 struct lock_instance *lock1, *lock2, *plock;
1035 struct lock_class *class, *iclass;
1036 struct witness *w, *w1;
1040 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL ||
1044 w = lock->lo_witness;
1045 class = LOCK_CLASS(lock);
1048 if (class->lc_flags & LC_SLEEPLOCK) {
1051 * Since spin locks include a critical section, this check
1052 * implicitly enforces a lock order of all sleep locks before
1055 if (td->td_critnest != 0 && !kdb_active)
1056 kassert_panic("acquiring blockable sleep lock with "
1057 "spinlock or critical section held (%s) %s @ %s:%d",
1058 class->lc_name, lock->lo_name,
1059 fixup_filename(file), line);
1062 * If this is the first lock acquired then just return as
1063 * no order checking is needed.
1065 lock_list = td->td_sleeplocks;
1066 if (lock_list == NULL || lock_list->ll_count == 0)
1071 * If this is the first lock, just return as no order
1072 * checking is needed. Avoid problems with thread
1073 * migration pinning the thread while checking if
1074 * spinlocks are held. If at least one spinlock is held
1075 * the thread is in a safe path and it is allowed to
1079 lock_list = PCPU_GET(spinlocks);
1080 if (lock_list == NULL || lock_list->ll_count == 0) {
1088 * Check to see if we are recursing on a lock we already own. If
1089 * so, make sure that we don't mismatch exclusive and shared lock
1092 lock1 = find_instance(lock_list, lock);
1093 if (lock1 != NULL) {
1094 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
1095 (flags & LOP_EXCLUSIVE) == 0) {
1096 printf("shared lock of (%s) %s @ %s:%d\n",
1097 class->lc_name, lock->lo_name,
1098 fixup_filename(file), line);
1099 printf("while exclusively locked from %s:%d\n",
1100 fixup_filename(lock1->li_file), lock1->li_line);
1101 kassert_panic("excl->share");
1103 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
1104 (flags & LOP_EXCLUSIVE) != 0) {
1105 printf("exclusive lock of (%s) %s @ %s:%d\n",
1106 class->lc_name, lock->lo_name,
1107 fixup_filename(file), line);
1108 printf("while share locked from %s:%d\n",
1109 fixup_filename(lock1->li_file), lock1->li_line);
1110 kassert_panic("share->excl");
1115 /* Warn if the interlock is not locked exactly once. */
1116 if (interlock != NULL) {
1117 iclass = LOCK_CLASS(interlock);
1118 lock1 = find_instance(lock_list, interlock);
1120 kassert_panic("interlock (%s) %s not locked @ %s:%d",
1121 iclass->lc_name, interlock->lo_name,
1122 fixup_filename(file), line);
1123 else if ((lock1->li_flags & LI_RECURSEMASK) != 0)
1124 kassert_panic("interlock (%s) %s recursed @ %s:%d",
1125 iclass->lc_name, interlock->lo_name,
1126 fixup_filename(file), line);
1130 * Find the previously acquired lock, but ignore interlocks.
1132 plock = &lock_list->ll_children[lock_list->ll_count - 1];
1133 if (interlock != NULL && plock->li_lock == interlock) {
1134 if (lock_list->ll_count > 1)
1136 &lock_list->ll_children[lock_list->ll_count - 2];
1138 lle = lock_list->ll_next;
1141 * The interlock is the only lock we hold, so
1146 plock = &lle->ll_children[lle->ll_count - 1];
1151 * Try to perform most checks without a lock. If this succeeds we
1152 * can skip acquiring the lock and return success.
1154 w1 = plock->li_lock->lo_witness;
1155 if (witness_lock_order_check(w1, w))
1159 * Check for duplicate locks of the same type. Note that we only
1160 * have to check for this on the last lock we just acquired. Any
1161 * other cases will be caught as lock order violations.
1163 mtx_lock_spin(&w_mtx);
1164 witness_lock_order_add(w1, w);
1167 if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) &&
1168 !(w_rmatrix[i][i] & WITNESS_REVERSAL)) {
1169 w_rmatrix[i][i] |= WITNESS_REVERSAL;
1171 mtx_unlock_spin(&w_mtx);
1173 "acquiring duplicate lock of same type: \"%s\"\n",
1175 printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name,
1176 fixup_filename(plock->li_file), plock->li_line);
1177 printf(" 2nd %s @ %s:%d\n", lock->lo_name,
1178 fixup_filename(file), line);
1179 witness_debugger(1);
1181 mtx_unlock_spin(&w_mtx);
1184 mtx_assert(&w_mtx, MA_OWNED);
1187 * If we know that the lock we are acquiring comes after
1188 * the lock we most recently acquired in the lock order tree,
1189 * then there is no need for any further checks.
1191 if (isitmychild(w1, w))
1194 for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) {
1195 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
1197 MPASS(j < WITNESS_COUNT);
1198 lock1 = &lle->ll_children[i];
1201 * Ignore the interlock.
1203 if (interlock == lock1->li_lock)
1207 * If this lock doesn't undergo witness checking,
1210 w1 = lock1->li_lock->lo_witness;
1212 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
1213 ("lock missing witness structure"));
1218 * If we are locking Giant and this is a sleepable
1219 * lock, then skip it.
1221 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
1222 lock == &Giant.lock_object)
1226 * If we are locking a sleepable lock and this lock
1227 * is Giant, then skip it.
1229 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1230 lock1->li_lock == &Giant.lock_object)
1234 * If we are locking a sleepable lock and this lock
1235 * isn't sleepable, we want to treat it as a lock
1236 * order violation to enfore a general lock order of
1237 * sleepable locks before non-sleepable locks.
1239 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1240 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1244 * If we are locking Giant and this is a non-sleepable
1245 * lock, then treat it as a reversal.
1247 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 &&
1248 lock == &Giant.lock_object)
1252 * Check the lock order hierarchy for a reveresal.
1254 if (!isitmydescendant(w, w1))
1259 * We have a lock order violation, check to see if it
1260 * is allowed or has already been yelled about.
1265 * If the lock order is blessed, just bail. We don't
1266 * look for other lock order violations though, which
1273 /* Bail if this violation is known */
1274 if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL)
1277 /* Record this as a violation */
1278 w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL;
1279 w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL;
1280 w->w_reversed = w1->w_reversed = 1;
1281 witness_increment_graph_generation();
1282 mtx_unlock_spin(&w_mtx);
1284 #ifdef WITNESS_NO_VNODE
1286 * There are known LORs between VNODE locks. They are
1287 * not an indication of a bug. VNODE locks are flagged
1288 * as such (LO_IS_VNODE) and we don't yell if the LOR
1289 * is between 2 VNODE locks.
1291 if ((lock->lo_flags & LO_IS_VNODE) != 0 &&
1292 (lock1->li_lock->lo_flags & LO_IS_VNODE) != 0)
1297 * Ok, yell about it.
1299 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1300 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1302 "lock order reversal: (sleepable after non-sleepable)\n");
1303 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0
1304 && lock == &Giant.lock_object)
1306 "lock order reversal: (Giant after non-sleepable)\n");
1308 printf("lock order reversal:\n");
1311 * Try to locate an earlier lock with
1312 * witness w in our list.
1315 lock2 = &lle->ll_children[i];
1316 MPASS(lock2->li_lock != NULL);
1317 if (lock2->li_lock->lo_witness == w)
1319 if (i == 0 && lle->ll_next != NULL) {
1321 i = lle->ll_count - 1;
1322 MPASS(i >= 0 && i < LOCK_NCHILDREN);
1327 printf(" 1st %p %s (%s) @ %s:%d\n",
1328 lock1->li_lock, lock1->li_lock->lo_name,
1329 w1->w_name, fixup_filename(lock1->li_file),
1331 printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
1332 lock->lo_name, w->w_name,
1333 fixup_filename(file), line);
1335 printf(" 1st %p %s (%s) @ %s:%d\n",
1336 lock2->li_lock, lock2->li_lock->lo_name,
1337 lock2->li_lock->lo_witness->w_name,
1338 fixup_filename(lock2->li_file),
1340 printf(" 2nd %p %s (%s) @ %s:%d\n",
1341 lock1->li_lock, lock1->li_lock->lo_name,
1342 w1->w_name, fixup_filename(lock1->li_file),
1344 printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
1345 lock->lo_name, w->w_name,
1346 fixup_filename(file), line);
1348 witness_debugger(1);
1354 * If requested, build a new lock order. However, don't build a new
1355 * relationship between a sleepable lock and Giant if it is in the
1356 * wrong direction. The correct lock order is that sleepable locks
1357 * always come before Giant.
1359 if (flags & LOP_NEWORDER &&
1360 !(plock->li_lock == &Giant.lock_object &&
1361 (lock->lo_flags & LO_SLEEPABLE) != 0)) {
1362 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1363 w->w_name, plock->li_lock->lo_witness->w_name);
1364 itismychild(plock->li_lock->lo_witness, w);
1367 mtx_unlock_spin(&w_mtx);
1371 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
1373 struct lock_list_entry **lock_list, *lle;
1374 struct lock_instance *instance;
1378 if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL ||
1381 w = lock->lo_witness;
1384 /* Determine lock list for this lock. */
1385 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK)
1386 lock_list = &td->td_sleeplocks;
1388 lock_list = PCPU_PTR(spinlocks);
1390 /* Check to see if we are recursing on a lock we already own. */
1391 instance = find_instance(*lock_list, lock);
1392 if (instance != NULL) {
1393 instance->li_flags++;
1394 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
1395 td->td_proc->p_pid, lock->lo_name,
1396 instance->li_flags & LI_RECURSEMASK);
1397 instance->li_file = file;
1398 instance->li_line = line;
1402 /* Update per-witness last file and line acquire. */
1406 /* Find the next open lock instance in the list and fill it. */
1408 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1409 lle = witness_lock_list_get();
1412 lle->ll_next = *lock_list;
1413 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1414 td->td_proc->p_pid, lle);
1417 instance = &lle->ll_children[lle->ll_count++];
1418 instance->li_lock = lock;
1419 instance->li_line = line;
1420 instance->li_file = file;
1421 if ((flags & LOP_EXCLUSIVE) != 0)
1422 instance->li_flags = LI_EXCLUSIVE;
1424 instance->li_flags = 0;
1425 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1426 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1430 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1432 struct lock_instance *instance;
1433 struct lock_class *class;
1435 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
1436 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
1438 class = LOCK_CLASS(lock);
1439 if (witness_watch) {
1440 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1442 "upgrade of non-upgradable lock (%s) %s @ %s:%d",
1443 class->lc_name, lock->lo_name,
1444 fixup_filename(file), line);
1445 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1447 "upgrade of non-sleep lock (%s) %s @ %s:%d",
1448 class->lc_name, lock->lo_name,
1449 fixup_filename(file), line);
1451 instance = find_instance(curthread->td_sleeplocks, lock);
1452 if (instance == NULL) {
1453 kassert_panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1454 class->lc_name, lock->lo_name,
1455 fixup_filename(file), line);
1458 if (witness_watch) {
1459 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1461 "upgrade of exclusive lock (%s) %s @ %s:%d",
1462 class->lc_name, lock->lo_name,
1463 fixup_filename(file), line);
1464 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1466 "upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1467 class->lc_name, lock->lo_name,
1468 instance->li_flags & LI_RECURSEMASK,
1469 fixup_filename(file), line);
1471 instance->li_flags |= LI_EXCLUSIVE;
1475 witness_downgrade(struct lock_object *lock, int flags, const char *file,
1478 struct lock_instance *instance;
1479 struct lock_class *class;
1481 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
1482 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
1484 class = LOCK_CLASS(lock);
1485 if (witness_watch) {
1486 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1488 "downgrade of non-upgradable lock (%s) %s @ %s:%d",
1489 class->lc_name, lock->lo_name,
1490 fixup_filename(file), line);
1491 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1493 "downgrade of non-sleep lock (%s) %s @ %s:%d",
1494 class->lc_name, lock->lo_name,
1495 fixup_filename(file), line);
1497 instance = find_instance(curthread->td_sleeplocks, lock);
1498 if (instance == NULL) {
1499 kassert_panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1500 class->lc_name, lock->lo_name,
1501 fixup_filename(file), line);
1504 if (witness_watch) {
1505 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1507 "downgrade of shared lock (%s) %s @ %s:%d",
1508 class->lc_name, lock->lo_name,
1509 fixup_filename(file), line);
1510 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1512 "downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1513 class->lc_name, lock->lo_name,
1514 instance->li_flags & LI_RECURSEMASK,
1515 fixup_filename(file), line);
1517 instance->li_flags &= ~LI_EXCLUSIVE;
1521 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1523 struct lock_list_entry **lock_list, *lle;
1524 struct lock_instance *instance;
1525 struct lock_class *class;
1530 if (witness_cold || lock->lo_witness == NULL || panicstr != NULL)
1533 class = LOCK_CLASS(lock);
1535 /* Find lock instance associated with this lock. */
1536 if (class->lc_flags & LC_SLEEPLOCK)
1537 lock_list = &td->td_sleeplocks;
1539 lock_list = PCPU_PTR(spinlocks);
1541 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1542 for (i = 0; i < (*lock_list)->ll_count; i++) {
1543 instance = &(*lock_list)->ll_children[i];
1544 if (instance->li_lock == lock)
1549 * When disabling WITNESS through witness_watch we could end up in
1550 * having registered locks in the td_sleeplocks queue.
1551 * We have to make sure we flush these queues, so just search for
1552 * eventual register locks and remove them.
1554 if (witness_watch > 0) {
1555 kassert_panic("lock (%s) %s not locked @ %s:%d", class->lc_name,
1556 lock->lo_name, fixup_filename(file), line);
1563 /* First, check for shared/exclusive mismatches. */
1564 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 &&
1565 (flags & LOP_EXCLUSIVE) == 0) {
1566 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1567 lock->lo_name, fixup_filename(file), line);
1568 printf("while exclusively locked from %s:%d\n",
1569 fixup_filename(instance->li_file), instance->li_line);
1570 kassert_panic("excl->ushare");
1572 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 &&
1573 (flags & LOP_EXCLUSIVE) != 0) {
1574 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1575 lock->lo_name, fixup_filename(file), line);
1576 printf("while share locked from %s:%d\n",
1577 fixup_filename(instance->li_file),
1579 kassert_panic("share->uexcl");
1581 /* If we are recursed, unrecurse. */
1582 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1583 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1584 td->td_proc->p_pid, instance->li_lock->lo_name,
1585 instance->li_flags);
1586 instance->li_flags--;
1589 /* The lock is now being dropped, check for NORELEASE flag */
1590 if ((instance->li_flags & LI_NORELEASE) != 0 && witness_watch > 0) {
1591 printf("forbidden unlock of (%s) %s @ %s:%d\n", class->lc_name,
1592 lock->lo_name, fixup_filename(file), line);
1593 kassert_panic("lock marked norelease");
1596 /* Otherwise, remove this item from the list. */
1598 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1599 td->td_proc->p_pid, instance->li_lock->lo_name,
1600 (*lock_list)->ll_count - 1);
1601 for (j = i; j < (*lock_list)->ll_count - 1; j++)
1602 (*lock_list)->ll_children[j] =
1603 (*lock_list)->ll_children[j + 1];
1604 (*lock_list)->ll_count--;
1608 * In order to reduce contention on w_mtx, we want to keep always an
1609 * head object into lists so that frequent allocation from the
1610 * free witness pool (and subsequent locking) is avoided.
1611 * In order to maintain the current code simple, when the head
1612 * object is totally unloaded it means also that we do not have
1613 * further objects in the list, so the list ownership needs to be
1614 * hand over to another object if the current head needs to be freed.
1616 if ((*lock_list)->ll_count == 0) {
1617 if (*lock_list == lle) {
1618 if (lle->ll_next == NULL)
1622 *lock_list = lle->ll_next;
1623 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1624 td->td_proc->p_pid, lle);
1625 witness_lock_list_free(lle);
1630 witness_thread_exit(struct thread *td)
1632 struct lock_list_entry *lle;
1635 lle = td->td_sleeplocks;
1636 if (lle == NULL || panicstr != NULL)
1638 if (lle->ll_count != 0) {
1639 for (n = 0; lle != NULL; lle = lle->ll_next)
1640 for (i = lle->ll_count - 1; i >= 0; i--) {
1642 printf("Thread %p exiting with the following locks held:\n",
1645 witness_list_lock(&lle->ll_children[i], printf);
1649 "Thread %p cannot exit while holding sleeplocks\n", td);
1651 witness_lock_list_free(lle);
1655 * Warn if any locks other than 'lock' are held. Flags can be passed in to
1656 * exempt Giant and sleepable locks from the checks as well. If any
1657 * non-exempt locks are held, then a supplied message is printed to the
1658 * console along with a list of the offending locks. If indicated in the
1659 * flags then a failure results in a panic as well.
1662 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1664 struct lock_list_entry *lock_list, *lle;
1665 struct lock_instance *lock1;
1670 if (witness_cold || witness_watch < 1 || panicstr != NULL)
1674 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1675 for (i = lle->ll_count - 1; i >= 0; i--) {
1676 lock1 = &lle->ll_children[i];
1677 if (lock1->li_lock == lock)
1679 if (flags & WARN_GIANTOK &&
1680 lock1->li_lock == &Giant.lock_object)
1682 if (flags & WARN_SLEEPOK &&
1683 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1689 printf(" with the following");
1690 if (flags & WARN_SLEEPOK)
1691 printf(" non-sleepable");
1692 printf(" locks held:\n");
1695 witness_list_lock(lock1, printf);
1699 * Pin the thread in order to avoid problems with thread migration.
1700 * Once that all verifies are passed about spinlocks ownership,
1701 * the thread is in a safe path and it can be unpinned.
1704 lock_list = PCPU_GET(spinlocks);
1705 if (lock_list != NULL && lock_list->ll_count != 0) {
1709 * We should only have one spinlock and as long as
1710 * the flags cannot match for this locks class,
1711 * check if the first spinlock is the one curthread
1714 lock1 = &lock_list->ll_children[lock_list->ll_count - 1];
1715 if (lock_list->ll_count == 1 && lock_list->ll_next == NULL &&
1716 lock1->li_lock == lock && n == 0)
1722 printf(" with the following");
1723 if (flags & WARN_SLEEPOK)
1724 printf(" non-sleepable");
1725 printf(" locks held:\n");
1726 n += witness_list_locks(&lock_list, printf);
1729 if (flags & WARN_PANIC && n)
1730 kassert_panic("%s", __func__);
1732 witness_debugger(n);
1737 witness_file(struct lock_object *lock)
1741 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL)
1743 w = lock->lo_witness;
1748 witness_line(struct lock_object *lock)
1752 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL)
1754 w = lock->lo_witness;
1758 static struct witness *
1759 enroll(const char *description, struct lock_class *lock_class)
1762 struct witness_list *typelist;
1764 MPASS(description != NULL);
1766 if (witness_watch == -1 || panicstr != NULL)
1768 if ((lock_class->lc_flags & LC_SPINLOCK)) {
1769 if (witness_skipspin)
1773 } else if ((lock_class->lc_flags & LC_SLEEPLOCK)) {
1774 typelist = &w_sleep;
1776 kassert_panic("lock class %s is not sleep or spin",
1777 lock_class->lc_name);
1781 mtx_lock_spin(&w_mtx);
1782 w = witness_hash_get(description);
1785 if ((w = witness_get()) == NULL)
1787 MPASS(strlen(description) < MAX_W_NAME);
1788 strcpy(w->w_name, description);
1789 w->w_class = lock_class;
1791 STAILQ_INSERT_HEAD(&w_all, w, w_list);
1792 if (lock_class->lc_flags & LC_SPINLOCK) {
1793 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1795 } else if (lock_class->lc_flags & LC_SLEEPLOCK) {
1796 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1800 /* Insert new witness into the hash */
1801 witness_hash_put(w);
1802 witness_increment_graph_generation();
1803 mtx_unlock_spin(&w_mtx);
1807 mtx_unlock_spin(&w_mtx);
1808 if (lock_class != w->w_class)
1810 "lock (%s) %s does not match earlier (%s) lock",
1811 description, lock_class->lc_name,
1812 w->w_class->lc_name);
1817 depart(struct witness *w)
1819 struct witness_list *list;
1821 MPASS(w->w_refcount == 0);
1822 if (w->w_class->lc_flags & LC_SLEEPLOCK) {
1830 * Set file to NULL as it may point into a loadable module.
1834 witness_increment_graph_generation();
1839 adopt(struct witness *parent, struct witness *child)
1843 if (witness_cold == 0)
1844 mtx_assert(&w_mtx, MA_OWNED);
1846 /* If the relationship is already known, there's no work to be done. */
1847 if (isitmychild(parent, child))
1850 /* When the structure of the graph changes, bump up the generation. */
1851 witness_increment_graph_generation();
1854 * The hard part ... create the direct relationship, then propagate all
1855 * indirect relationships.
1857 pi = parent->w_index;
1858 ci = child->w_index;
1859 WITNESS_INDEX_ASSERT(pi);
1860 WITNESS_INDEX_ASSERT(ci);
1862 w_rmatrix[pi][ci] |= WITNESS_PARENT;
1863 w_rmatrix[ci][pi] |= WITNESS_CHILD;
1866 * If parent was not already an ancestor of child,
1867 * then we increment the descendant and ancestor counters.
1869 if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) {
1870 parent->w_num_descendants++;
1871 child->w_num_ancestors++;
1875 * Find each ancestor of 'pi'. Note that 'pi' itself is counted as
1876 * an ancestor of 'pi' during this loop.
1878 for (i = 1; i <= w_max_used_index; i++) {
1879 if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 &&
1883 /* Find each descendant of 'i' and mark it as a descendant. */
1884 for (j = 1; j <= w_max_used_index; j++) {
1887 * Skip children that are already marked as
1888 * descendants of 'i'.
1890 if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK)
1894 * We are only interested in descendants of 'ci'. Note
1895 * that 'ci' itself is counted as a descendant of 'ci'.
1897 if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 &&
1900 w_rmatrix[i][j] |= WITNESS_ANCESTOR;
1901 w_rmatrix[j][i] |= WITNESS_DESCENDANT;
1902 w_data[i].w_num_descendants++;
1903 w_data[j].w_num_ancestors++;
1906 * Make sure we aren't marking a node as both an
1907 * ancestor and descendant. We should have caught
1908 * this as a lock order reversal earlier.
1910 if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) &&
1911 (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) {
1912 printf("witness rmatrix paradox! [%d][%d]=%d "
1913 "both ancestor and descendant\n",
1914 i, j, w_rmatrix[i][j]);
1916 printf("Witness disabled.\n");
1919 if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) &&
1920 (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) {
1921 printf("witness rmatrix paradox! [%d][%d]=%d "
1922 "both ancestor and descendant\n",
1923 j, i, w_rmatrix[j][i]);
1925 printf("Witness disabled.\n");
1933 itismychild(struct witness *parent, struct witness *child)
1937 MPASS(child != NULL && parent != NULL);
1938 if (witness_cold == 0)
1939 mtx_assert(&w_mtx, MA_OWNED);
1941 if (!witness_lock_type_equal(parent, child)) {
1942 if (witness_cold == 0) {
1944 mtx_unlock_spin(&w_mtx);
1949 "%s: parent \"%s\" (%s) and child \"%s\" (%s) are not "
1950 "the same lock type", __func__, parent->w_name,
1951 parent->w_class->lc_name, child->w_name,
1952 child->w_class->lc_name);
1954 mtx_lock_spin(&w_mtx);
1956 adopt(parent, child);
1960 * Generic code for the isitmy*() functions. The rmask parameter is the
1961 * expected relationship of w1 to w2.
1964 _isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname)
1966 unsigned char r1, r2;
1971 WITNESS_INDEX_ASSERT(i1);
1972 WITNESS_INDEX_ASSERT(i2);
1973 r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK;
1974 r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK;
1976 /* The flags on one better be the inverse of the flags on the other */
1977 if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) ||
1978 (WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) {
1979 printf("%s: rmatrix mismatch between %s (index %d) and %s "
1980 "(index %d): w_rmatrix[%d][%d] == %hhx but "
1981 "w_rmatrix[%d][%d] == %hhx\n",
1982 fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1,
1985 printf("Witness disabled.\n");
1988 return (r1 & rmask);
1992 * Checks if @child is a direct child of @parent.
1995 isitmychild(struct witness *parent, struct witness *child)
1998 return (_isitmyx(parent, child, WITNESS_PARENT, __func__));
2002 * Checks if @descendant is a direct or inderect descendant of @ancestor.
2005 isitmydescendant(struct witness *ancestor, struct witness *descendant)
2008 return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK,
2014 blessed(struct witness *w1, struct witness *w2)
2017 struct witness_blessed *b;
2019 for (i = 0; i < blessed_count; i++) {
2020 b = &blessed_list[i];
2021 if (strcmp(w1->w_name, b->b_lock1) == 0) {
2022 if (strcmp(w2->w_name, b->b_lock2) == 0)
2026 if (strcmp(w1->w_name, b->b_lock2) == 0)
2027 if (strcmp(w2->w_name, b->b_lock1) == 0)
2034 static struct witness *
2040 if (witness_cold == 0)
2041 mtx_assert(&w_mtx, MA_OWNED);
2043 if (witness_watch == -1) {
2044 mtx_unlock_spin(&w_mtx);
2047 if (STAILQ_EMPTY(&w_free)) {
2049 mtx_unlock_spin(&w_mtx);
2050 printf("WITNESS: unable to allocate a new witness object\n");
2053 w = STAILQ_FIRST(&w_free);
2054 STAILQ_REMOVE_HEAD(&w_free, w_list);
2057 MPASS(index > 0 && index == w_max_used_index+1 &&
2058 index < WITNESS_COUNT);
2059 bzero(w, sizeof(*w));
2061 if (index > w_max_used_index)
2062 w_max_used_index = index;
2067 witness_free(struct witness *w)
2070 STAILQ_INSERT_HEAD(&w_free, w, w_list);
2074 static struct lock_list_entry *
2075 witness_lock_list_get(void)
2077 struct lock_list_entry *lle;
2079 if (witness_watch == -1)
2081 mtx_lock_spin(&w_mtx);
2082 lle = w_lock_list_free;
2085 mtx_unlock_spin(&w_mtx);
2086 printf("%s: witness exhausted\n", __func__);
2089 w_lock_list_free = lle->ll_next;
2090 mtx_unlock_spin(&w_mtx);
2091 bzero(lle, sizeof(*lle));
2096 witness_lock_list_free(struct lock_list_entry *lle)
2099 mtx_lock_spin(&w_mtx);
2100 lle->ll_next = w_lock_list_free;
2101 w_lock_list_free = lle;
2102 mtx_unlock_spin(&w_mtx);
2105 static struct lock_instance *
2106 find_instance(struct lock_list_entry *list, const struct lock_object *lock)
2108 struct lock_list_entry *lle;
2109 struct lock_instance *instance;
2112 for (lle = list; lle != NULL; lle = lle->ll_next)
2113 for (i = lle->ll_count - 1; i >= 0; i--) {
2114 instance = &lle->ll_children[i];
2115 if (instance->li_lock == lock)
2122 witness_list_lock(struct lock_instance *instance,
2123 int (*prnt)(const char *fmt, ...))
2125 struct lock_object *lock;
2127 lock = instance->li_lock;
2128 prnt("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
2129 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name);
2130 if (lock->lo_witness->w_name != lock->lo_name)
2131 prnt(" (%s)", lock->lo_witness->w_name);
2132 prnt(" r = %d (%p) locked @ %s:%d\n",
2133 instance->li_flags & LI_RECURSEMASK, lock,
2134 fixup_filename(instance->li_file), instance->li_line);
2139 witness_thread_has_locks(struct thread *td)
2142 if (td->td_sleeplocks == NULL)
2144 return (td->td_sleeplocks->ll_count != 0);
2148 witness_proc_has_locks(struct proc *p)
2152 FOREACH_THREAD_IN_PROC(p, td) {
2153 if (witness_thread_has_locks(td))
2161 witness_list_locks(struct lock_list_entry **lock_list,
2162 int (*prnt)(const char *fmt, ...))
2164 struct lock_list_entry *lle;
2168 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
2169 for (i = lle->ll_count - 1; i >= 0; i--) {
2170 witness_list_lock(&lle->ll_children[i], prnt);
2177 * This is a bit risky at best. We call this function when we have timed
2178 * out acquiring a spin lock, and we assume that the other CPU is stuck
2179 * with this lock held. So, we go groveling around in the other CPU's
2180 * per-cpu data to try to find the lock instance for this spin lock to
2181 * see when it was last acquired.
2184 witness_display_spinlock(struct lock_object *lock, struct thread *owner,
2185 int (*prnt)(const char *fmt, ...))
2187 struct lock_instance *instance;
2190 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
2192 pc = pcpu_find(owner->td_oncpu);
2193 instance = find_instance(pc->pc_spinlocks, lock);
2194 if (instance != NULL)
2195 witness_list_lock(instance, prnt);
2199 witness_save(struct lock_object *lock, const char **filep, int *linep)
2201 struct lock_list_entry *lock_list;
2202 struct lock_instance *instance;
2203 struct lock_class *class;
2206 * This function is used independently in locking code to deal with
2207 * Giant, SCHEDULER_STOPPED() check can be removed here after Giant
2210 if (SCHEDULER_STOPPED())
2212 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2213 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2215 class = LOCK_CLASS(lock);
2216 if (class->lc_flags & LC_SLEEPLOCK)
2217 lock_list = curthread->td_sleeplocks;
2219 if (witness_skipspin)
2221 lock_list = PCPU_GET(spinlocks);
2223 instance = find_instance(lock_list, lock);
2224 if (instance == NULL) {
2225 kassert_panic("%s: lock (%s) %s not locked", __func__,
2226 class->lc_name, lock->lo_name);
2229 *filep = instance->li_file;
2230 *linep = instance->li_line;
2234 witness_restore(struct lock_object *lock, const char *file, int line)
2236 struct lock_list_entry *lock_list;
2237 struct lock_instance *instance;
2238 struct lock_class *class;
2241 * This function is used independently in locking code to deal with
2242 * Giant, SCHEDULER_STOPPED() check can be removed here after Giant
2245 if (SCHEDULER_STOPPED())
2247 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2248 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2250 class = LOCK_CLASS(lock);
2251 if (class->lc_flags & LC_SLEEPLOCK)
2252 lock_list = curthread->td_sleeplocks;
2254 if (witness_skipspin)
2256 lock_list = PCPU_GET(spinlocks);
2258 instance = find_instance(lock_list, lock);
2259 if (instance == NULL)
2260 kassert_panic("%s: lock (%s) %s not locked", __func__,
2261 class->lc_name, lock->lo_name);
2262 lock->lo_witness->w_file = file;
2263 lock->lo_witness->w_line = line;
2264 if (instance == NULL)
2266 instance->li_file = file;
2267 instance->li_line = line;
2271 witness_assert(const struct lock_object *lock, int flags, const char *file,
2274 #ifdef INVARIANT_SUPPORT
2275 struct lock_instance *instance;
2276 struct lock_class *class;
2278 if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL)
2280 class = LOCK_CLASS(lock);
2281 if ((class->lc_flags & LC_SLEEPLOCK) != 0)
2282 instance = find_instance(curthread->td_sleeplocks, lock);
2283 else if ((class->lc_flags & LC_SPINLOCK) != 0)
2284 instance = find_instance(PCPU_GET(spinlocks), lock);
2286 kassert_panic("Lock (%s) %s is not sleep or spin!",
2287 class->lc_name, lock->lo_name);
2292 if (instance != NULL)
2293 kassert_panic("Lock (%s) %s locked @ %s:%d.",
2294 class->lc_name, lock->lo_name,
2295 fixup_filename(file), line);
2298 case LA_LOCKED | LA_RECURSED:
2299 case LA_LOCKED | LA_NOTRECURSED:
2301 case LA_SLOCKED | LA_RECURSED:
2302 case LA_SLOCKED | LA_NOTRECURSED:
2304 case LA_XLOCKED | LA_RECURSED:
2305 case LA_XLOCKED | LA_NOTRECURSED:
2306 if (instance == NULL) {
2307 kassert_panic("Lock (%s) %s not locked @ %s:%d.",
2308 class->lc_name, lock->lo_name,
2309 fixup_filename(file), line);
2312 if ((flags & LA_XLOCKED) != 0 &&
2313 (instance->li_flags & LI_EXCLUSIVE) == 0)
2315 "Lock (%s) %s not exclusively locked @ %s:%d.",
2316 class->lc_name, lock->lo_name,
2317 fixup_filename(file), line);
2318 if ((flags & LA_SLOCKED) != 0 &&
2319 (instance->li_flags & LI_EXCLUSIVE) != 0)
2321 "Lock (%s) %s exclusively locked @ %s:%d.",
2322 class->lc_name, lock->lo_name,
2323 fixup_filename(file), line);
2324 if ((flags & LA_RECURSED) != 0 &&
2325 (instance->li_flags & LI_RECURSEMASK) == 0)
2326 kassert_panic("Lock (%s) %s not recursed @ %s:%d.",
2327 class->lc_name, lock->lo_name,
2328 fixup_filename(file), line);
2329 if ((flags & LA_NOTRECURSED) != 0 &&
2330 (instance->li_flags & LI_RECURSEMASK) != 0)
2331 kassert_panic("Lock (%s) %s recursed @ %s:%d.",
2332 class->lc_name, lock->lo_name,
2333 fixup_filename(file), line);
2336 kassert_panic("Invalid lock assertion at %s:%d.",
2337 fixup_filename(file), line);
2340 #endif /* INVARIANT_SUPPORT */
2344 witness_setflag(struct lock_object *lock, int flag, int set)
2346 struct lock_list_entry *lock_list;
2347 struct lock_instance *instance;
2348 struct lock_class *class;
2350 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2352 class = LOCK_CLASS(lock);
2353 if (class->lc_flags & LC_SLEEPLOCK)
2354 lock_list = curthread->td_sleeplocks;
2356 if (witness_skipspin)
2358 lock_list = PCPU_GET(spinlocks);
2360 instance = find_instance(lock_list, lock);
2361 if (instance == NULL) {
2362 kassert_panic("%s: lock (%s) %s not locked", __func__,
2363 class->lc_name, lock->lo_name);
2368 instance->li_flags |= flag;
2370 instance->li_flags &= ~flag;
2374 witness_norelease(struct lock_object *lock)
2377 witness_setflag(lock, LI_NORELEASE, 1);
2381 witness_releaseok(struct lock_object *lock)
2384 witness_setflag(lock, LI_NORELEASE, 0);
2389 witness_ddb_list(struct thread *td)
2392 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2393 KASSERT(kdb_active, ("%s: not in the debugger", __func__));
2395 if (witness_watch < 1)
2398 witness_list_locks(&td->td_sleeplocks, db_printf);
2401 * We only handle spinlocks if td == curthread. This is somewhat broken
2402 * if td is currently executing on some other CPU and holds spin locks
2403 * as we won't display those locks. If we had a MI way of getting
2404 * the per-cpu data for a given cpu then we could use
2405 * td->td_oncpu to get the list of spinlocks for this thread
2408 * That still wouldn't really fix this unless we locked the scheduler
2409 * lock or stopped the other CPU to make sure it wasn't changing the
2410 * list out from under us. It is probably best to just not try to
2411 * handle threads on other CPU's for now.
2413 if (td == curthread && PCPU_GET(spinlocks) != NULL)
2414 witness_list_locks(PCPU_PTR(spinlocks), db_printf);
2417 DB_SHOW_COMMAND(locks, db_witness_list)
2422 td = db_lookup_thread(addr, TRUE);
2425 witness_ddb_list(td);
2428 DB_SHOW_ALL_COMMAND(locks, db_witness_list_all)
2434 * It would be nice to list only threads and processes that actually
2435 * held sleep locks, but that information is currently not exported
2438 FOREACH_PROC_IN_SYSTEM(p) {
2439 if (!witness_proc_has_locks(p))
2441 FOREACH_THREAD_IN_PROC(p, td) {
2442 if (!witness_thread_has_locks(td))
2444 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
2445 p->p_comm, td, td->td_tid);
2446 witness_ddb_list(td);
2452 DB_SHOW_ALIAS(alllocks, db_witness_list_all)
2454 DB_SHOW_COMMAND(witness, db_witness_display)
2457 witness_ddb_display(db_printf);
2462 sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS)
2464 struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2;
2465 struct witness *tmp_w1, *tmp_w2, *w1, *w2;
2467 u_int w_rmatrix1, w_rmatrix2;
2468 int error, generation, i, j;
2474 if (witness_watch < 1) {
2475 error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning));
2479 error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold));
2483 sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND);
2487 /* Allocate and init temporary storage space. */
2488 tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO);
2489 tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO);
2490 tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP,
2492 tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP,
2494 stack_zero(&tmp_data1->wlod_stack);
2495 stack_zero(&tmp_data2->wlod_stack);
2498 mtx_lock_spin(&w_mtx);
2499 generation = w_generation;
2500 mtx_unlock_spin(&w_mtx);
2501 sbuf_printf(sb, "Number of known direct relationships is %d\n",
2502 w_lohash.wloh_count);
2503 for (i = 1; i < w_max_used_index; i++) {
2504 mtx_lock_spin(&w_mtx);
2505 if (generation != w_generation) {
2506 mtx_unlock_spin(&w_mtx);
2508 /* The graph has changed, try again. */
2515 if (w1->w_reversed == 0) {
2516 mtx_unlock_spin(&w_mtx);
2520 /* Copy w1 locally so we can release the spin lock. */
2522 mtx_unlock_spin(&w_mtx);
2524 if (tmp_w1->w_reversed == 0)
2526 for (j = 1; j < w_max_used_index; j++) {
2527 if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j)
2530 mtx_lock_spin(&w_mtx);
2531 if (generation != w_generation) {
2532 mtx_unlock_spin(&w_mtx);
2534 /* The graph has changed, try again. */
2541 data1 = witness_lock_order_get(w1, w2);
2542 data2 = witness_lock_order_get(w2, w1);
2545 * Copy information locally so we can release the
2549 w_rmatrix1 = (unsigned int)w_rmatrix[i][j];
2550 w_rmatrix2 = (unsigned int)w_rmatrix[j][i];
2553 stack_zero(&tmp_data1->wlod_stack);
2554 stack_copy(&data1->wlod_stack,
2555 &tmp_data1->wlod_stack);
2557 if (data2 && data2 != data1) {
2558 stack_zero(&tmp_data2->wlod_stack);
2559 stack_copy(&data2->wlod_stack,
2560 &tmp_data2->wlod_stack);
2562 mtx_unlock_spin(&w_mtx);
2565 "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n",
2566 tmp_w1->w_name, tmp_w1->w_class->lc_name,
2567 tmp_w2->w_name, tmp_w2->w_class->lc_name);
2570 "w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n",
2571 tmp_w1->name, tmp_w2->w_name, w_rmatrix1,
2572 tmp_w2->name, tmp_w1->w_name, w_rmatrix2);
2576 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n",
2577 tmp_w1->w_name, tmp_w1->w_class->lc_name,
2578 tmp_w2->w_name, tmp_w2->w_class->lc_name);
2579 stack_sbuf_print(sb, &tmp_data1->wlod_stack);
2580 sbuf_printf(sb, "\n");
2582 if (data2 && data2 != data1) {
2584 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n",
2585 tmp_w2->w_name, tmp_w2->w_class->lc_name,
2586 tmp_w1->w_name, tmp_w1->w_class->lc_name);
2587 stack_sbuf_print(sb, &tmp_data2->wlod_stack);
2588 sbuf_printf(sb, "\n");
2592 mtx_lock_spin(&w_mtx);
2593 if (generation != w_generation) {
2594 mtx_unlock_spin(&w_mtx);
2597 * The graph changed while we were printing stack data,
2604 mtx_unlock_spin(&w_mtx);
2606 /* Free temporary storage space. */
2607 free(tmp_data1, M_TEMP);
2608 free(tmp_data2, M_TEMP);
2609 free(tmp_w1, M_TEMP);
2610 free(tmp_w2, M_TEMP);
2613 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2620 sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS)
2626 if (witness_watch < 1) {
2627 error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning));
2631 error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold));
2636 error = sysctl_wire_old_buffer(req, 0);
2639 sb = sbuf_new_for_sysctl(NULL, NULL, FULLGRAPH_SBUF_SIZE, req);
2642 sbuf_printf(sb, "\n");
2644 mtx_lock_spin(&w_mtx);
2645 STAILQ_FOREACH(w, &w_all, w_list)
2647 STAILQ_FOREACH(w, &w_all, w_list)
2648 witness_add_fullgraph(sb, w);
2649 mtx_unlock_spin(&w_mtx);
2652 * Close the sbuf and return to userland.
2654 error = sbuf_finish(sb);
2661 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
2665 value = witness_watch;
2666 error = sysctl_handle_int(oidp, &value, 0, req);
2667 if (error != 0 || req->newptr == NULL)
2669 if (value > 1 || value < -1 ||
2670 (witness_watch == -1 && value != witness_watch))
2672 witness_watch = value;
2677 witness_add_fullgraph(struct sbuf *sb, struct witness *w)
2681 if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0))
2685 WITNESS_INDEX_ASSERT(w->w_index);
2686 for (i = 1; i <= w_max_used_index; i++) {
2687 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) {
2688 sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name,
2690 witness_add_fullgraph(sb, &w_data[i]);
2696 * A simple hash function. Takes a key pointer and a key size. If size == 0,
2697 * interprets the key as a string and reads until the null
2698 * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit
2699 * hash value computed from the key.
2702 witness_hash_djb2(const uint8_t *key, uint32_t size)
2704 unsigned int hash = 5381;
2707 /* hash = hash * 33 + key[i] */
2709 for (i = 0; i < size; i++)
2710 hash = ((hash << 5) + hash) + (unsigned int)key[i];
2712 for (i = 0; key[i] != 0; i++)
2713 hash = ((hash << 5) + hash) + (unsigned int)key[i];
2720 * Initializes the two witness hash tables. Called exactly once from
2721 * witness_initialize().
2724 witness_init_hash_tables(void)
2728 MPASS(witness_cold);
2730 /* Initialize the hash tables. */
2731 for (i = 0; i < WITNESS_HASH_SIZE; i++)
2732 w_hash.wh_array[i] = NULL;
2734 w_hash.wh_size = WITNESS_HASH_SIZE;
2735 w_hash.wh_count = 0;
2737 /* Initialize the lock order data hash. */
2739 for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) {
2740 memset(&w_lodata[i], 0, sizeof(w_lodata[i]));
2741 w_lodata[i].wlod_next = w_lofree;
2742 w_lofree = &w_lodata[i];
2744 w_lohash.wloh_size = WITNESS_LO_HASH_SIZE;
2745 w_lohash.wloh_count = 0;
2746 for (i = 0; i < WITNESS_LO_HASH_SIZE; i++)
2747 w_lohash.wloh_array[i] = NULL;
2750 static struct witness *
2751 witness_hash_get(const char *key)
2757 if (witness_cold == 0)
2758 mtx_assert(&w_mtx, MA_OWNED);
2759 hash = witness_hash_djb2(key, 0) % w_hash.wh_size;
2760 w = w_hash.wh_array[hash];
2762 if (strcmp(w->w_name, key) == 0)
2772 witness_hash_put(struct witness *w)
2777 MPASS(w->w_name != NULL);
2778 if (witness_cold == 0)
2779 mtx_assert(&w_mtx, MA_OWNED);
2780 KASSERT(witness_hash_get(w->w_name) == NULL,
2781 ("%s: trying to add a hash entry that already exists!", __func__));
2782 KASSERT(w->w_hash_next == NULL,
2783 ("%s: w->w_hash_next != NULL", __func__));
2785 hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size;
2786 w->w_hash_next = w_hash.wh_array[hash];
2787 w_hash.wh_array[hash] = w;
2792 static struct witness_lock_order_data *
2793 witness_lock_order_get(struct witness *parent, struct witness *child)
2795 struct witness_lock_order_data *data = NULL;
2796 struct witness_lock_order_key key;
2799 MPASS(parent != NULL && child != NULL);
2800 key.from = parent->w_index;
2801 key.to = child->w_index;
2802 WITNESS_INDEX_ASSERT(key.from);
2803 WITNESS_INDEX_ASSERT(key.to);
2804 if ((w_rmatrix[parent->w_index][child->w_index]
2805 & WITNESS_LOCK_ORDER_KNOWN) == 0)
2808 hash = witness_hash_djb2((const char*)&key,
2809 sizeof(key)) % w_lohash.wloh_size;
2810 data = w_lohash.wloh_array[hash];
2811 while (data != NULL) {
2812 if (witness_lock_order_key_equal(&data->wlod_key, &key))
2814 data = data->wlod_next;
2822 * Verify that parent and child have a known relationship, are not the same,
2823 * and child is actually a child of parent. This is done without w_mtx
2824 * to avoid contention in the common case.
2827 witness_lock_order_check(struct witness *parent, struct witness *child)
2830 if (parent != child &&
2831 w_rmatrix[parent->w_index][child->w_index]
2832 & WITNESS_LOCK_ORDER_KNOWN &&
2833 isitmychild(parent, child))
2840 witness_lock_order_add(struct witness *parent, struct witness *child)
2842 struct witness_lock_order_data *data = NULL;
2843 struct witness_lock_order_key key;
2846 MPASS(parent != NULL && child != NULL);
2847 key.from = parent->w_index;
2848 key.to = child->w_index;
2849 WITNESS_INDEX_ASSERT(key.from);
2850 WITNESS_INDEX_ASSERT(key.to);
2851 if (w_rmatrix[parent->w_index][child->w_index]
2852 & WITNESS_LOCK_ORDER_KNOWN)
2855 hash = witness_hash_djb2((const char*)&key,
2856 sizeof(key)) % w_lohash.wloh_size;
2857 w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN;
2861 w_lofree = data->wlod_next;
2862 data->wlod_next = w_lohash.wloh_array[hash];
2863 data->wlod_key = key;
2864 w_lohash.wloh_array[hash] = data;
2865 w_lohash.wloh_count++;
2866 stack_zero(&data->wlod_stack);
2867 stack_save(&data->wlod_stack);
2871 /* Call this whenver the structure of the witness graph changes. */
2873 witness_increment_graph_generation(void)
2876 if (witness_cold == 0)
2877 mtx_assert(&w_mtx, MA_OWNED);
2883 _witness_debugger(int cond, const char *msg)
2886 if (witness_trace && cond)
2888 if (witness_kdb && cond)
2889 kdb_enter(KDB_WHY_WITNESS, msg);