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 1024
136 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4)
137 #define WITNESS_HASH_SIZE 251 /* Prime, gives load factor < 2 */
138 #define WITNESS_PENDLIST 768
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 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 TUNABLE_INT("debug.witness.watch", &witness_watch);
384 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0,
385 sysctl_debug_witness_watch, "I", "witness is watching lock operations");
389 * When KDB is enabled and witness_kdb is 1, it will cause the system
390 * to drop into kdebug() when:
391 * - a lock hierarchy violation occurs
392 * - locks are held when going to sleep.
399 TUNABLE_INT("debug.witness.kdb", &witness_kdb);
400 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, "");
403 * When KDB is enabled and witness_trace is 1, it will cause the system
404 * to print a stack trace:
405 * - a lock hierarchy violation occurs
406 * - locks are held when going to sleep.
408 int witness_trace = 1;
409 TUNABLE_INT("debug.witness.trace", &witness_trace);
410 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, "");
413 #ifdef WITNESS_SKIPSPIN
414 int witness_skipspin = 1;
416 int witness_skipspin = 0;
418 TUNABLE_INT("debug.witness.skipspin", &witness_skipspin);
419 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, &witness_skipspin,
423 * Call this to print out the relations between locks.
425 SYSCTL_PROC(_debug_witness, OID_AUTO, fullgraph, CTLTYPE_STRING | CTLFLAG_RD,
426 NULL, 0, sysctl_debug_witness_fullgraph, "A", "Show locks relation graphs");
429 * Call this to print out the witness faulty stacks.
431 SYSCTL_PROC(_debug_witness, OID_AUTO, badstacks, CTLTYPE_STRING | CTLFLAG_RD,
432 NULL, 0, sysctl_debug_witness_badstacks, "A", "Show bad witness stacks");
434 static struct mtx w_mtx;
437 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free);
438 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all);
441 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin);
442 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep);
445 static struct lock_list_entry *w_lock_list_free = NULL;
446 static struct witness_pendhelp pending_locks[WITNESS_PENDLIST];
447 static u_int pending_cnt;
449 static int w_free_cnt, w_spin_cnt, w_sleep_cnt;
450 SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, "");
451 SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, "");
452 SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0,
455 static struct witness *w_data;
456 static uint8_t w_rmatrix[WITNESS_COUNT+1][WITNESS_COUNT+1];
457 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT];
458 static struct witness_hash w_hash; /* The witness hash table. */
460 /* The lock order data hash */
461 static struct witness_lock_order_data w_lodata[WITNESS_LO_DATA_COUNT];
462 static struct witness_lock_order_data *w_lofree = NULL;
463 static struct witness_lock_order_hash w_lohash;
464 static int w_max_used_index = 0;
465 static unsigned int w_generation = 0;
466 static const char w_notrunning[] = "Witness not running\n";
467 static const char w_stillcold[] = "Witness is still cold\n";
470 static struct witness_order_list_entry order_lists[] = {
474 { "proctree", &lock_class_sx },
475 { "allproc", &lock_class_sx },
476 { "allprison", &lock_class_sx },
481 { "Giant", &lock_class_mtx_sleep },
482 { "pipe mutex", &lock_class_mtx_sleep },
483 { "sigio lock", &lock_class_mtx_sleep },
484 { "process group", &lock_class_mtx_sleep },
485 { "process lock", &lock_class_mtx_sleep },
486 { "session", &lock_class_mtx_sleep },
487 { "uidinfo hash", &lock_class_rw },
489 { "pmc-sleep", &lock_class_mtx_sleep },
491 { "time lock", &lock_class_mtx_sleep },
496 { "accept", &lock_class_mtx_sleep },
497 { "so_snd", &lock_class_mtx_sleep },
498 { "so_rcv", &lock_class_mtx_sleep },
499 { "sellck", &lock_class_mtx_sleep },
504 { "so_rcv", &lock_class_mtx_sleep },
505 { "radix node head", &lock_class_rw },
506 { "rtentry", &lock_class_mtx_sleep },
507 { "ifaddr", &lock_class_mtx_sleep },
511 * protocol locks before interface locks, after UDP locks.
513 { "udpinp", &lock_class_rw },
514 { "in_multi_mtx", &lock_class_mtx_sleep },
515 { "igmp_mtx", &lock_class_mtx_sleep },
516 { "if_addr_mtx", &lock_class_mtx_sleep },
520 * protocol locks before interface locks, after UDP locks.
522 { "udpinp", &lock_class_rw },
523 { "in6_multi_mtx", &lock_class_mtx_sleep },
524 { "mld_mtx", &lock_class_mtx_sleep },
525 { "if_addr_mtx", &lock_class_mtx_sleep },
528 * UNIX Domain Sockets
530 { "unp_link_rwlock", &lock_class_rw },
531 { "unp_list_lock", &lock_class_mtx_sleep },
532 { "unp", &lock_class_mtx_sleep },
533 { "so_snd", &lock_class_mtx_sleep },
538 { "udp", &lock_class_rw },
539 { "udpinp", &lock_class_rw },
540 { "so_snd", &lock_class_mtx_sleep },
545 { "tcp", &lock_class_rw },
546 { "tcpinp", &lock_class_rw },
547 { "so_snd", &lock_class_mtx_sleep },
552 { "ddp_list_mtx", &lock_class_mtx_sleep },
553 { "ddp_mtx", &lock_class_mtx_sleep },
558 { "bpf global lock", &lock_class_mtx_sleep },
559 { "bpf interface lock", &lock_class_rw },
560 { "bpf cdev lock", &lock_class_mtx_sleep },
565 { "nfsd_mtx", &lock_class_mtx_sleep },
566 { "so_snd", &lock_class_mtx_sleep },
572 { "802.11 com lock", &lock_class_mtx_sleep},
577 { "network driver", &lock_class_mtx_sleep},
583 { "ng_node", &lock_class_mtx_sleep },
584 { "ng_worklist", &lock_class_mtx_sleep },
589 { "vm map (system)", &lock_class_mtx_sleep },
590 { "vm page queue", &lock_class_mtx_sleep },
591 { "vnode interlock", &lock_class_mtx_sleep },
592 { "cdev", &lock_class_mtx_sleep },
597 { "vm map (user)", &lock_class_sx },
598 { "vm object", &lock_class_mtx_sleep },
599 { "vm page", &lock_class_mtx_sleep },
600 { "vm page queue", &lock_class_mtx_sleep },
601 { "pmap pv global", &lock_class_rw },
602 { "pmap", &lock_class_mtx_sleep },
603 { "pmap pv list", &lock_class_rw },
604 { "vm page free queue", &lock_class_mtx_sleep },
607 * kqueue/VFS interaction
609 { "kqueue", &lock_class_mtx_sleep },
610 { "struct mount mtx", &lock_class_mtx_sleep },
611 { "vnode interlock", &lock_class_mtx_sleep },
616 { "dn->dn_mtx", &lock_class_sx },
617 { "dr->dt.di.dr_mtx", &lock_class_sx },
618 { "db->db_mtx", &lock_class_sx },
624 { "ap boot", &lock_class_mtx_spin },
626 { "rm.mutex_mtx", &lock_class_mtx_spin },
627 { "sio", &lock_class_mtx_spin },
628 { "scrlock", &lock_class_mtx_spin },
630 { "cy", &lock_class_mtx_spin },
633 { "pcib_mtx", &lock_class_mtx_spin },
634 { "rtc_mtx", &lock_class_mtx_spin },
636 { "scc_hwmtx", &lock_class_mtx_spin },
637 { "uart_hwmtx", &lock_class_mtx_spin },
638 { "fast_taskqueue", &lock_class_mtx_spin },
639 { "intr table", &lock_class_mtx_spin },
641 { "pmc-per-proc", &lock_class_mtx_spin },
643 { "process slock", &lock_class_mtx_spin },
644 { "sleepq chain", &lock_class_mtx_spin },
645 { "umtx lock", &lock_class_mtx_spin },
646 { "rm_spinlock", &lock_class_mtx_spin },
647 { "turnstile chain", &lock_class_mtx_spin },
648 { "turnstile lock", &lock_class_mtx_spin },
649 { "sched lock", &lock_class_mtx_spin },
650 { "td_contested", &lock_class_mtx_spin },
651 { "callout", &lock_class_mtx_spin },
652 { "entropy harvest mutex", &lock_class_mtx_spin },
653 { "syscons video lock", &lock_class_mtx_spin },
655 { "smp rendezvous", &lock_class_mtx_spin },
658 { "tlb0", &lock_class_mtx_spin },
663 { "intrcnt", &lock_class_mtx_spin },
664 { "icu", &lock_class_mtx_spin },
665 #if defined(SMP) && defined(__sparc64__)
666 { "ipi", &lock_class_mtx_spin },
669 { "allpmaps", &lock_class_mtx_spin },
670 { "descriptor tables", &lock_class_mtx_spin },
672 { "clk", &lock_class_mtx_spin },
673 { "cpuset", &lock_class_mtx_spin },
674 { "mprof lock", &lock_class_mtx_spin },
675 { "zombie lock", &lock_class_mtx_spin },
676 { "ALD Queue", &lock_class_mtx_spin },
678 { "MCA spin lock", &lock_class_mtx_spin },
680 #if defined(__i386__) || defined(__amd64__)
681 { "pcicfg", &lock_class_mtx_spin },
682 { "NDIS thread lock", &lock_class_mtx_spin },
684 { "tw_osl_io_lock", &lock_class_mtx_spin },
685 { "tw_osl_q_lock", &lock_class_mtx_spin },
686 { "tw_cl_io_lock", &lock_class_mtx_spin },
687 { "tw_cl_intr_lock", &lock_class_mtx_spin },
688 { "tw_cl_gen_lock", &lock_class_mtx_spin },
690 { "pmc-leaf", &lock_class_mtx_spin },
692 { "blocked lock", &lock_class_mtx_spin },
699 * Pairs of locks which have been blessed
700 * Don't complain about order problems with blessed locks
702 static struct witness_blessed blessed_list[] = {
704 static int blessed_count =
705 sizeof(blessed_list) / sizeof(struct witness_blessed);
709 * This global is set to 0 once it becomes safe to use the witness code.
711 static int witness_cold = 1;
714 * This global is set to 1 once the static lock orders have been enrolled
715 * so that a warning can be issued for any spin locks enrolled later.
717 static int witness_spin_warn = 0;
719 /* Trim useless garbage from filenames. */
721 fixup_filename(const char *file)
726 while (strncmp(file, "../", 3) == 0)
732 * The WITNESS-enabled diagnostic code. Note that the witness code does
733 * assume that the early boot is single-threaded at least until after this
734 * routine is completed.
737 witness_initialize(void *dummy __unused)
739 struct lock_object *lock;
740 struct witness_order_list_entry *order;
741 struct witness *w, *w1;
744 w_data = malloc(sizeof (struct witness) * WITNESS_COUNT, M_WITNESS,
748 * We have to release Giant before initializing its witness
749 * structure so that WITNESS doesn't get confused.
752 mtx_assert(&Giant, MA_NOTOWNED);
754 CTR1(KTR_WITNESS, "%s: initializing witness", __func__);
755 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET |
756 MTX_NOWITNESS | MTX_NOPROFILE);
757 for (i = WITNESS_COUNT - 1; i >= 0; i--) {
759 memset(w, 0, sizeof(*w));
760 w_data[i].w_index = i; /* Witness index never changes. */
763 KASSERT(STAILQ_FIRST(&w_free)->w_index == 0,
764 ("%s: Invalid list of free witness objects", __func__));
766 /* Witness with index 0 is not used to aid in debugging. */
767 STAILQ_REMOVE_HEAD(&w_free, w_list);
771 (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1)));
773 for (i = 0; i < LOCK_CHILDCOUNT; i++)
774 witness_lock_list_free(&w_locklistdata[i]);
775 witness_init_hash_tables();
777 /* First add in all the specified order lists. */
778 for (order = order_lists; order->w_name != NULL; order++) {
779 w = enroll(order->w_name, order->w_class);
782 w->w_file = "order list";
783 for (order++; order->w_name != NULL; order++) {
784 w1 = enroll(order->w_name, order->w_class);
787 w1->w_file = "order list";
792 witness_spin_warn = 1;
794 /* Iterate through all locks and add them to witness. */
795 for (i = 0; pending_locks[i].wh_lock != NULL; i++) {
796 lock = pending_locks[i].wh_lock;
797 KASSERT(lock->lo_flags & LO_WITNESS,
798 ("%s: lock %s is on pending list but not LO_WITNESS",
799 __func__, lock->lo_name));
800 lock->lo_witness = enroll(pending_locks[i].wh_type,
804 /* Mark the witness code as being ready for use. */
809 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize,
813 witness_init(struct lock_object *lock, const char *type)
815 struct lock_class *class;
817 /* Various sanity checks. */
818 class = LOCK_CLASS(lock);
819 if ((lock->lo_flags & LO_RECURSABLE) != 0 &&
820 (class->lc_flags & LC_RECURSABLE) == 0)
821 panic("%s: lock (%s) %s can not be recursable", __func__,
822 class->lc_name, lock->lo_name);
823 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
824 (class->lc_flags & LC_SLEEPABLE) == 0)
825 panic("%s: lock (%s) %s can not be sleepable", __func__,
826 class->lc_name, lock->lo_name);
827 if ((lock->lo_flags & LO_UPGRADABLE) != 0 &&
828 (class->lc_flags & LC_UPGRADABLE) == 0)
829 panic("%s: lock (%s) %s can not be upgradable", __func__,
830 class->lc_name, lock->lo_name);
833 * If we shouldn't watch this lock, then just clear lo_witness.
834 * Otherwise, if witness_cold is set, then it is too early to
835 * enroll this lock, so defer it to witness_initialize() by adding
836 * it to the pending_locks list. If it is not too early, then enroll
839 if (witness_watch < 1 || panicstr != NULL ||
840 (lock->lo_flags & LO_WITNESS) == 0)
841 lock->lo_witness = NULL;
842 else if (witness_cold) {
843 pending_locks[pending_cnt].wh_lock = lock;
844 pending_locks[pending_cnt++].wh_type = type;
845 if (pending_cnt > WITNESS_PENDLIST)
846 panic("%s: pending locks list is too small, bump it\n",
849 lock->lo_witness = enroll(type, class);
853 witness_destroy(struct lock_object *lock)
855 struct lock_class *class;
858 class = LOCK_CLASS(lock);
861 panic("lock (%s) %s destroyed while witness_cold",
862 class->lc_name, lock->lo_name);
864 /* XXX: need to verify that no one holds the lock */
865 if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL)
867 w = lock->lo_witness;
869 mtx_lock_spin(&w_mtx);
870 MPASS(w->w_refcount > 0);
873 if (w->w_refcount == 0)
875 mtx_unlock_spin(&w_mtx);
880 witness_ddb_compute_levels(void)
885 * First clear all levels.
887 STAILQ_FOREACH(w, &w_all, w_list)
891 * Look for locks with no parents and level all their descendants.
893 STAILQ_FOREACH(w, &w_all, w_list) {
895 /* If the witness has ancestors (is not a root), skip it. */
896 if (w->w_num_ancestors > 0)
898 witness_ddb_level_descendants(w, 0);
903 witness_ddb_level_descendants(struct witness *w, int l)
907 if (w->w_ddb_level >= l)
913 for (i = 1; i <= w_max_used_index; i++) {
914 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT)
915 witness_ddb_level_descendants(&w_data[i], l);
920 witness_ddb_display_descendants(int(*prnt)(const char *fmt, ...),
921 struct witness *w, int indent)
925 for (i = 0; i < indent; i++)
927 prnt("%s (type: %s, depth: %d, active refs: %d)",
928 w->w_name, w->w_class->lc_name,
929 w->w_ddb_level, w->w_refcount);
930 if (w->w_displayed) {
931 prnt(" -- (already displayed)\n");
935 if (w->w_file != NULL && w->w_line != 0)
936 prnt(" -- last acquired @ %s:%d\n", fixup_filename(w->w_file),
939 prnt(" -- never acquired\n");
941 WITNESS_INDEX_ASSERT(w->w_index);
942 for (i = 1; i <= w_max_used_index; i++) {
945 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT)
946 witness_ddb_display_descendants(prnt, &w_data[i],
952 witness_ddb_display_list(int(*prnt)(const char *fmt, ...),
953 struct witness_list *list)
957 STAILQ_FOREACH(w, list, w_typelist) {
958 if (w->w_file == NULL || w->w_ddb_level > 0)
961 /* This lock has no anscestors - display its descendants. */
962 witness_ddb_display_descendants(prnt, w, 0);
969 witness_ddb_display(int(*prnt)(const char *fmt, ...))
973 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
974 witness_ddb_compute_levels();
976 /* Clear all the displayed flags. */
977 STAILQ_FOREACH(w, &w_all, w_list)
981 * First, handle sleep locks which have been acquired at least
984 prnt("Sleep locks:\n");
985 witness_ddb_display_list(prnt, &w_sleep);
990 * Now do spin locks which have been acquired at least once.
992 prnt("\nSpin locks:\n");
993 witness_ddb_display_list(prnt, &w_spin);
998 * Finally, any locks which have not been acquired yet.
1000 prnt("\nLocks which were never acquired:\n");
1001 STAILQ_FOREACH(w, &w_all, w_list) {
1002 if (w->w_file != NULL || w->w_refcount == 0)
1004 prnt("%s (type: %s, depth: %d)\n", w->w_name,
1005 w->w_class->lc_name, w->w_ddb_level);
1013 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2)
1016 if (witness_watch == -1 || panicstr != NULL)
1019 /* Require locks that witness knows about. */
1020 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL ||
1021 lock2->lo_witness == NULL)
1024 mtx_assert(&w_mtx, MA_NOTOWNED);
1025 mtx_lock_spin(&w_mtx);
1028 * If we already have either an explicit or implied lock order that
1029 * is the other way around, then return an error.
1031 if (witness_watch &&
1032 isitmydescendant(lock2->lo_witness, lock1->lo_witness)) {
1033 mtx_unlock_spin(&w_mtx);
1037 /* Try to add the new order. */
1038 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1039 lock2->lo_witness->w_name, lock1->lo_witness->w_name);
1040 itismychild(lock1->lo_witness, lock2->lo_witness);
1041 mtx_unlock_spin(&w_mtx);
1046 witness_checkorder(struct lock_object *lock, int flags, const char *file,
1047 int line, struct lock_object *interlock)
1049 struct lock_list_entry *lock_list, *lle;
1050 struct lock_instance *lock1, *lock2, *plock;
1051 struct lock_class *class;
1052 struct witness *w, *w1;
1056 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL ||
1060 w = lock->lo_witness;
1061 class = LOCK_CLASS(lock);
1064 if (class->lc_flags & LC_SLEEPLOCK) {
1067 * Since spin locks include a critical section, this check
1068 * implicitly enforces a lock order of all sleep locks before
1071 if (td->td_critnest != 0 && !kdb_active)
1072 panic("blockable sleep lock (%s) %s @ %s:%d",
1073 class->lc_name, lock->lo_name,
1074 fixup_filename(file), line);
1077 * If this is the first lock acquired then just return as
1078 * no order checking is needed.
1080 lock_list = td->td_sleeplocks;
1081 if (lock_list == NULL || lock_list->ll_count == 0)
1086 * If this is the first lock, just return as no order
1087 * checking is needed. Avoid problems with thread
1088 * migration pinning the thread while checking if
1089 * spinlocks are held. If at least one spinlock is held
1090 * the thread is in a safe path and it is allowed to
1094 lock_list = PCPU_GET(spinlocks);
1095 if (lock_list == NULL || lock_list->ll_count == 0) {
1103 * Check to see if we are recursing on a lock we already own. If
1104 * so, make sure that we don't mismatch exclusive and shared lock
1107 lock1 = find_instance(lock_list, lock);
1108 if (lock1 != NULL) {
1109 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 &&
1110 (flags & LOP_EXCLUSIVE) == 0) {
1111 printf("shared lock of (%s) %s @ %s:%d\n",
1112 class->lc_name, lock->lo_name,
1113 fixup_filename(file), line);
1114 printf("while exclusively locked from %s:%d\n",
1115 fixup_filename(lock1->li_file), lock1->li_line);
1116 panic("share->excl");
1118 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 &&
1119 (flags & LOP_EXCLUSIVE) != 0) {
1120 printf("exclusive lock of (%s) %s @ %s:%d\n",
1121 class->lc_name, lock->lo_name,
1122 fixup_filename(file), line);
1123 printf("while share locked from %s:%d\n",
1124 fixup_filename(lock1->li_file), lock1->li_line);
1125 panic("excl->share");
1131 * Find the previously acquired lock, but ignore interlocks.
1133 plock = &lock_list->ll_children[lock_list->ll_count - 1];
1134 if (interlock != NULL && plock->li_lock == interlock) {
1135 if (lock_list->ll_count > 1)
1137 &lock_list->ll_children[lock_list->ll_count - 2];
1139 lle = lock_list->ll_next;
1142 * The interlock is the only lock we hold, so
1147 plock = &lle->ll_children[lle->ll_count - 1];
1152 * Try to perform most checks without a lock. If this succeeds we
1153 * can skip acquiring the lock and return success.
1155 w1 = plock->li_lock->lo_witness;
1156 if (witness_lock_order_check(w1, w))
1160 * Check for duplicate locks of the same type. Note that we only
1161 * have to check for this on the last lock we just acquired. Any
1162 * other cases will be caught as lock order violations.
1164 mtx_lock_spin(&w_mtx);
1165 witness_lock_order_add(w1, w);
1168 if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) &&
1169 !(w_rmatrix[i][i] & WITNESS_REVERSAL)) {
1170 w_rmatrix[i][i] |= WITNESS_REVERSAL;
1172 mtx_unlock_spin(&w_mtx);
1174 "acquiring duplicate lock of same type: \"%s\"\n",
1176 printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name,
1177 fixup_filename(plock->li_file), plock->li_line);
1178 printf(" 2nd %s @ %s:%d\n", lock->lo_name,
1179 fixup_filename(file), line);
1180 witness_debugger(1);
1182 mtx_unlock_spin(&w_mtx);
1185 mtx_assert(&w_mtx, MA_OWNED);
1188 * If we know that the lock we are acquiring comes after
1189 * the lock we most recently acquired in the lock order tree,
1190 * then there is no need for any further checks.
1192 if (isitmychild(w1, w))
1195 for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) {
1196 for (i = lle->ll_count - 1; i >= 0; i--, j++) {
1198 MPASS(j < WITNESS_COUNT);
1199 lock1 = &lle->ll_children[i];
1202 * Ignore the interlock the first time we see it.
1204 if (interlock != NULL && interlock == lock1->li_lock) {
1210 * If this lock doesn't undergo witness checking,
1213 w1 = lock1->li_lock->lo_witness;
1215 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0,
1216 ("lock missing witness structure"));
1221 * If we are locking Giant and this is a sleepable
1222 * lock, then skip it.
1224 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 &&
1225 lock == &Giant.lock_object)
1229 * If we are locking a sleepable lock and this lock
1230 * is Giant, then skip it.
1232 if ((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1233 lock1->li_lock == &Giant.lock_object)
1237 * If we are locking a sleepable lock and this lock
1238 * isn't sleepable, we want to treat it as a lock
1239 * order violation to enfore a general lock order of
1240 * sleepable locks before non-sleepable locks.
1242 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1243 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1247 * If we are locking Giant and this is a non-sleepable
1248 * lock, then treat it as a reversal.
1250 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 &&
1251 lock == &Giant.lock_object)
1255 * Check the lock order hierarchy for a reveresal.
1257 if (!isitmydescendant(w, w1))
1262 * We have a lock order violation, check to see if it
1263 * is allowed or has already been yelled about.
1268 * If the lock order is blessed, just bail. We don't
1269 * look for other lock order violations though, which
1276 /* Bail if this violation is known */
1277 if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL)
1280 /* Record this as a violation */
1281 w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL;
1282 w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL;
1283 w->w_reversed = w1->w_reversed = 1;
1284 witness_increment_graph_generation();
1285 mtx_unlock_spin(&w_mtx);
1288 * Ok, yell about it.
1290 if (((lock->lo_flags & LO_SLEEPABLE) != 0 &&
1291 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0))
1293 "lock order reversal: (sleepable after non-sleepable)\n");
1294 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0
1295 && lock == &Giant.lock_object)
1297 "lock order reversal: (Giant after non-sleepable)\n");
1299 printf("lock order reversal:\n");
1302 * Try to locate an earlier lock with
1303 * witness w in our list.
1306 lock2 = &lle->ll_children[i];
1307 MPASS(lock2->li_lock != NULL);
1308 if (lock2->li_lock->lo_witness == w)
1310 if (i == 0 && lle->ll_next != NULL) {
1312 i = lle->ll_count - 1;
1313 MPASS(i >= 0 && i < LOCK_NCHILDREN);
1318 printf(" 1st %p %s (%s) @ %s:%d\n",
1319 lock1->li_lock, lock1->li_lock->lo_name,
1320 w1->w_name, fixup_filename(lock1->li_file),
1322 printf(" 2nd %p %s (%s) @ %s:%d\n", lock,
1323 lock->lo_name, w->w_name,
1324 fixup_filename(file), line);
1326 printf(" 1st %p %s (%s) @ %s:%d\n",
1327 lock2->li_lock, lock2->li_lock->lo_name,
1328 lock2->li_lock->lo_witness->w_name,
1329 fixup_filename(lock2->li_file),
1331 printf(" 2nd %p %s (%s) @ %s:%d\n",
1332 lock1->li_lock, lock1->li_lock->lo_name,
1333 w1->w_name, fixup_filename(lock1->li_file),
1335 printf(" 3rd %p %s (%s) @ %s:%d\n", lock,
1336 lock->lo_name, w->w_name,
1337 fixup_filename(file), line);
1339 witness_debugger(1);
1345 * If requested, build a new lock order. However, don't build a new
1346 * relationship between a sleepable lock and Giant if it is in the
1347 * wrong direction. The correct lock order is that sleepable locks
1348 * always come before Giant.
1350 if (flags & LOP_NEWORDER &&
1351 !(plock->li_lock == &Giant.lock_object &&
1352 (lock->lo_flags & LO_SLEEPABLE) != 0)) {
1353 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__,
1354 w->w_name, plock->li_lock->lo_witness->w_name);
1355 itismychild(plock->li_lock->lo_witness, w);
1358 mtx_unlock_spin(&w_mtx);
1362 witness_lock(struct lock_object *lock, int flags, const char *file, int line)
1364 struct lock_list_entry **lock_list, *lle;
1365 struct lock_instance *instance;
1369 if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL ||
1372 w = lock->lo_witness;
1375 /* Determine lock list for this lock. */
1376 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK)
1377 lock_list = &td->td_sleeplocks;
1379 lock_list = PCPU_PTR(spinlocks);
1381 /* Check to see if we are recursing on a lock we already own. */
1382 instance = find_instance(*lock_list, lock);
1383 if (instance != NULL) {
1384 instance->li_flags++;
1385 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__,
1386 td->td_proc->p_pid, lock->lo_name,
1387 instance->li_flags & LI_RECURSEMASK);
1388 instance->li_file = file;
1389 instance->li_line = line;
1393 /* Update per-witness last file and line acquire. */
1397 /* Find the next open lock instance in the list and fill it. */
1399 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) {
1400 lle = witness_lock_list_get();
1403 lle->ll_next = *lock_list;
1404 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__,
1405 td->td_proc->p_pid, lle);
1408 instance = &lle->ll_children[lle->ll_count++];
1409 instance->li_lock = lock;
1410 instance->li_line = line;
1411 instance->li_file = file;
1412 if ((flags & LOP_EXCLUSIVE) != 0)
1413 instance->li_flags = LI_EXCLUSIVE;
1415 instance->li_flags = 0;
1416 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__,
1417 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1);
1421 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line)
1423 struct lock_instance *instance;
1424 struct lock_class *class;
1426 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
1427 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
1429 class = LOCK_CLASS(lock);
1430 if (witness_watch) {
1431 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1432 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d",
1433 class->lc_name, lock->lo_name,
1434 fixup_filename(file), line);
1435 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1436 panic("upgrade of non-sleep lock (%s) %s @ %s:%d",
1437 class->lc_name, lock->lo_name,
1438 fixup_filename(file), line);
1440 instance = find_instance(curthread->td_sleeplocks, lock);
1441 if (instance == NULL)
1442 panic("upgrade of unlocked lock (%s) %s @ %s:%d",
1443 class->lc_name, lock->lo_name,
1444 fixup_filename(file), line);
1445 if (witness_watch) {
1446 if ((instance->li_flags & LI_EXCLUSIVE) != 0)
1447 panic("upgrade of exclusive lock (%s) %s @ %s:%d",
1448 class->lc_name, lock->lo_name,
1449 fixup_filename(file), line);
1450 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1451 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d",
1452 class->lc_name, lock->lo_name,
1453 instance->li_flags & LI_RECURSEMASK,
1454 fixup_filename(file), line);
1456 instance->li_flags |= LI_EXCLUSIVE;
1460 witness_downgrade(struct lock_object *lock, int flags, const char *file,
1463 struct lock_instance *instance;
1464 struct lock_class *class;
1466 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
1467 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
1469 class = LOCK_CLASS(lock);
1470 if (witness_watch) {
1471 if ((lock->lo_flags & LO_UPGRADABLE) == 0)
1472 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d",
1473 class->lc_name, lock->lo_name,
1474 fixup_filename(file), line);
1475 if ((class->lc_flags & LC_SLEEPLOCK) == 0)
1476 panic("downgrade of non-sleep lock (%s) %s @ %s:%d",
1477 class->lc_name, lock->lo_name,
1478 fixup_filename(file), line);
1480 instance = find_instance(curthread->td_sleeplocks, lock);
1481 if (instance == NULL)
1482 panic("downgrade of unlocked lock (%s) %s @ %s:%d",
1483 class->lc_name, lock->lo_name,
1484 fixup_filename(file), line);
1485 if (witness_watch) {
1486 if ((instance->li_flags & LI_EXCLUSIVE) == 0)
1487 panic("downgrade of shared lock (%s) %s @ %s:%d",
1488 class->lc_name, lock->lo_name,
1489 fixup_filename(file), line);
1490 if ((instance->li_flags & LI_RECURSEMASK) != 0)
1491 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d",
1492 class->lc_name, lock->lo_name,
1493 instance->li_flags & LI_RECURSEMASK,
1494 fixup_filename(file), line);
1496 instance->li_flags &= ~LI_EXCLUSIVE;
1500 witness_unlock(struct lock_object *lock, int flags, const char *file, int line)
1502 struct lock_list_entry **lock_list, *lle;
1503 struct lock_instance *instance;
1504 struct lock_class *class;
1509 if (witness_cold || lock->lo_witness == NULL || panicstr != NULL)
1512 class = LOCK_CLASS(lock);
1514 /* Find lock instance associated with this lock. */
1515 if (class->lc_flags & LC_SLEEPLOCK)
1516 lock_list = &td->td_sleeplocks;
1518 lock_list = PCPU_PTR(spinlocks);
1520 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next)
1521 for (i = 0; i < (*lock_list)->ll_count; i++) {
1522 instance = &(*lock_list)->ll_children[i];
1523 if (instance->li_lock == lock)
1528 * When disabling WITNESS through witness_watch we could end up in
1529 * having registered locks in the td_sleeplocks queue.
1530 * We have to make sure we flush these queues, so just search for
1531 * eventual register locks and remove them.
1533 if (witness_watch > 0)
1534 panic("lock (%s) %s not locked @ %s:%d", class->lc_name,
1535 lock->lo_name, fixup_filename(file), line);
1540 /* First, check for shared/exclusive mismatches. */
1541 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 &&
1542 (flags & LOP_EXCLUSIVE) == 0) {
1543 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name,
1544 lock->lo_name, fixup_filename(file), line);
1545 printf("while exclusively locked from %s:%d\n",
1546 fixup_filename(instance->li_file), instance->li_line);
1547 panic("excl->ushare");
1549 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 &&
1550 (flags & LOP_EXCLUSIVE) != 0) {
1551 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name,
1552 lock->lo_name, fixup_filename(file), line);
1553 printf("while share locked from %s:%d\n",
1554 fixup_filename(instance->li_file),
1556 panic("share->uexcl");
1558 /* If we are recursed, unrecurse. */
1559 if ((instance->li_flags & LI_RECURSEMASK) > 0) {
1560 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__,
1561 td->td_proc->p_pid, instance->li_lock->lo_name,
1562 instance->li_flags);
1563 instance->li_flags--;
1566 /* The lock is now being dropped, check for NORELEASE flag */
1567 if ((instance->li_flags & LI_NORELEASE) != 0 && witness_watch > 0) {
1568 printf("forbidden unlock of (%s) %s @ %s:%d\n", class->lc_name,
1569 lock->lo_name, fixup_filename(file), line);
1570 panic("lock marked norelease");
1573 /* Otherwise, remove this item from the list. */
1575 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__,
1576 td->td_proc->p_pid, instance->li_lock->lo_name,
1577 (*lock_list)->ll_count - 1);
1578 for (j = i; j < (*lock_list)->ll_count - 1; j++)
1579 (*lock_list)->ll_children[j] =
1580 (*lock_list)->ll_children[j + 1];
1581 (*lock_list)->ll_count--;
1585 * In order to reduce contention on w_mtx, we want to keep always an
1586 * head object into lists so that frequent allocation from the
1587 * free witness pool (and subsequent locking) is avoided.
1588 * In order to maintain the current code simple, when the head
1589 * object is totally unloaded it means also that we do not have
1590 * further objects in the list, so the list ownership needs to be
1591 * hand over to another object if the current head needs to be freed.
1593 if ((*lock_list)->ll_count == 0) {
1594 if (*lock_list == lle) {
1595 if (lle->ll_next == NULL)
1599 *lock_list = lle->ll_next;
1600 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__,
1601 td->td_proc->p_pid, lle);
1602 witness_lock_list_free(lle);
1607 witness_thread_exit(struct thread *td)
1609 struct lock_list_entry *lle;
1612 lle = td->td_sleeplocks;
1613 if (lle == NULL || panicstr != NULL)
1615 if (lle->ll_count != 0) {
1616 for (n = 0; lle != NULL; lle = lle->ll_next)
1617 for (i = lle->ll_count - 1; i >= 0; i--) {
1619 printf("Thread %p exiting with the following locks held:\n",
1622 witness_list_lock(&lle->ll_children[i], printf);
1625 panic("Thread %p cannot exit while holding sleeplocks\n", td);
1627 witness_lock_list_free(lle);
1631 * Warn if any locks other than 'lock' are held. Flags can be passed in to
1632 * exempt Giant and sleepable locks from the checks as well. If any
1633 * non-exempt locks are held, then a supplied message is printed to the
1634 * console along with a list of the offending locks. If indicated in the
1635 * flags then a failure results in a panic as well.
1638 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...)
1640 struct lock_list_entry *lock_list, *lle;
1641 struct lock_instance *lock1;
1646 if (witness_cold || witness_watch < 1 || panicstr != NULL)
1650 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next)
1651 for (i = lle->ll_count - 1; i >= 0; i--) {
1652 lock1 = &lle->ll_children[i];
1653 if (lock1->li_lock == lock)
1655 if (flags & WARN_GIANTOK &&
1656 lock1->li_lock == &Giant.lock_object)
1658 if (flags & WARN_SLEEPOK &&
1659 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0)
1665 printf(" with the following");
1666 if (flags & WARN_SLEEPOK)
1667 printf(" non-sleepable");
1668 printf(" locks held:\n");
1671 witness_list_lock(lock1, printf);
1675 * Pin the thread in order to avoid problems with thread migration.
1676 * Once that all verifies are passed about spinlocks ownership,
1677 * the thread is in a safe path and it can be unpinned.
1680 lock_list = PCPU_GET(spinlocks);
1681 if (lock_list != NULL && lock_list->ll_count != 0) {
1685 * We should only have one spinlock and as long as
1686 * the flags cannot match for this locks class,
1687 * check if the first spinlock is the one curthread
1690 lock1 = &lock_list->ll_children[lock_list->ll_count - 1];
1691 if (lock_list->ll_count == 1 && lock_list->ll_next == NULL &&
1692 lock1->li_lock == lock && n == 0)
1698 printf(" with the following");
1699 if (flags & WARN_SLEEPOK)
1700 printf(" non-sleepable");
1701 printf(" locks held:\n");
1702 n += witness_list_locks(&lock_list, printf);
1705 if (flags & WARN_PANIC && n)
1706 panic("%s", __func__);
1708 witness_debugger(n);
1713 witness_file(struct lock_object *lock)
1717 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL)
1719 w = lock->lo_witness;
1724 witness_line(struct lock_object *lock)
1728 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL)
1730 w = lock->lo_witness;
1734 static struct witness *
1735 enroll(const char *description, struct lock_class *lock_class)
1738 struct witness_list *typelist;
1740 MPASS(description != NULL);
1742 if (witness_watch == -1 || panicstr != NULL)
1744 if ((lock_class->lc_flags & LC_SPINLOCK)) {
1745 if (witness_skipspin)
1749 } else if ((lock_class->lc_flags & LC_SLEEPLOCK))
1750 typelist = &w_sleep;
1752 panic("lock class %s is not sleep or spin",
1753 lock_class->lc_name);
1755 mtx_lock_spin(&w_mtx);
1756 w = witness_hash_get(description);
1759 if ((w = witness_get()) == NULL)
1761 MPASS(strlen(description) < MAX_W_NAME);
1762 strcpy(w->w_name, description);
1763 w->w_class = lock_class;
1765 STAILQ_INSERT_HEAD(&w_all, w, w_list);
1766 if (lock_class->lc_flags & LC_SPINLOCK) {
1767 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist);
1769 } else if (lock_class->lc_flags & LC_SLEEPLOCK) {
1770 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist);
1774 /* Insert new witness into the hash */
1775 witness_hash_put(w);
1776 witness_increment_graph_generation();
1777 mtx_unlock_spin(&w_mtx);
1781 mtx_unlock_spin(&w_mtx);
1782 if (lock_class != w->w_class)
1784 "lock (%s) %s does not match earlier (%s) lock",
1785 description, lock_class->lc_name,
1786 w->w_class->lc_name);
1791 depart(struct witness *w)
1793 struct witness_list *list;
1795 MPASS(w->w_refcount == 0);
1796 if (w->w_class->lc_flags & LC_SLEEPLOCK) {
1804 * Set file to NULL as it may point into a loadable module.
1808 witness_increment_graph_generation();
1813 adopt(struct witness *parent, struct witness *child)
1817 if (witness_cold == 0)
1818 mtx_assert(&w_mtx, MA_OWNED);
1820 /* If the relationship is already known, there's no work to be done. */
1821 if (isitmychild(parent, child))
1824 /* When the structure of the graph changes, bump up the generation. */
1825 witness_increment_graph_generation();
1828 * The hard part ... create the direct relationship, then propagate all
1829 * indirect relationships.
1831 pi = parent->w_index;
1832 ci = child->w_index;
1833 WITNESS_INDEX_ASSERT(pi);
1834 WITNESS_INDEX_ASSERT(ci);
1836 w_rmatrix[pi][ci] |= WITNESS_PARENT;
1837 w_rmatrix[ci][pi] |= WITNESS_CHILD;
1840 * If parent was not already an ancestor of child,
1841 * then we increment the descendant and ancestor counters.
1843 if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) {
1844 parent->w_num_descendants++;
1845 child->w_num_ancestors++;
1849 * Find each ancestor of 'pi'. Note that 'pi' itself is counted as
1850 * an ancestor of 'pi' during this loop.
1852 for (i = 1; i <= w_max_used_index; i++) {
1853 if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 &&
1857 /* Find each descendant of 'i' and mark it as a descendant. */
1858 for (j = 1; j <= w_max_used_index; j++) {
1861 * Skip children that are already marked as
1862 * descendants of 'i'.
1864 if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK)
1868 * We are only interested in descendants of 'ci'. Note
1869 * that 'ci' itself is counted as a descendant of 'ci'.
1871 if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 &&
1874 w_rmatrix[i][j] |= WITNESS_ANCESTOR;
1875 w_rmatrix[j][i] |= WITNESS_DESCENDANT;
1876 w_data[i].w_num_descendants++;
1877 w_data[j].w_num_ancestors++;
1880 * Make sure we aren't marking a node as both an
1881 * ancestor and descendant. We should have caught
1882 * this as a lock order reversal earlier.
1884 if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) &&
1885 (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) {
1886 printf("witness rmatrix paradox! [%d][%d]=%d "
1887 "both ancestor and descendant\n",
1888 i, j, w_rmatrix[i][j]);
1890 printf("Witness disabled.\n");
1893 if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) &&
1894 (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) {
1895 printf("witness rmatrix paradox! [%d][%d]=%d "
1896 "both ancestor and descendant\n",
1897 j, i, w_rmatrix[j][i]);
1899 printf("Witness disabled.\n");
1907 itismychild(struct witness *parent, struct witness *child)
1910 MPASS(child != NULL && parent != NULL);
1911 if (witness_cold == 0)
1912 mtx_assert(&w_mtx, MA_OWNED);
1914 if (!witness_lock_type_equal(parent, child)) {
1915 if (witness_cold == 0)
1916 mtx_unlock_spin(&w_mtx);
1917 panic("%s: parent \"%s\" (%s) and child \"%s\" (%s) are not "
1918 "the same lock type", __func__, parent->w_name,
1919 parent->w_class->lc_name, child->w_name,
1920 child->w_class->lc_name);
1922 adopt(parent, child);
1926 * Generic code for the isitmy*() functions. The rmask parameter is the
1927 * expected relationship of w1 to w2.
1930 _isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname)
1932 unsigned char r1, r2;
1937 WITNESS_INDEX_ASSERT(i1);
1938 WITNESS_INDEX_ASSERT(i2);
1939 r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK;
1940 r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK;
1942 /* The flags on one better be the inverse of the flags on the other */
1943 if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) ||
1944 (WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) {
1945 printf("%s: rmatrix mismatch between %s (index %d) and %s "
1946 "(index %d): w_rmatrix[%d][%d] == %hhx but "
1947 "w_rmatrix[%d][%d] == %hhx\n",
1948 fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1,
1951 printf("Witness disabled.\n");
1954 return (r1 & rmask);
1958 * Checks if @child is a direct child of @parent.
1961 isitmychild(struct witness *parent, struct witness *child)
1964 return (_isitmyx(parent, child, WITNESS_PARENT, __func__));
1968 * Checks if @descendant is a direct or inderect descendant of @ancestor.
1971 isitmydescendant(struct witness *ancestor, struct witness *descendant)
1974 return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK,
1980 blessed(struct witness *w1, struct witness *w2)
1983 struct witness_blessed *b;
1985 for (i = 0; i < blessed_count; i++) {
1986 b = &blessed_list[i];
1987 if (strcmp(w1->w_name, b->b_lock1) == 0) {
1988 if (strcmp(w2->w_name, b->b_lock2) == 0)
1992 if (strcmp(w1->w_name, b->b_lock2) == 0)
1993 if (strcmp(w2->w_name, b->b_lock1) == 0)
2000 static struct witness *
2006 if (witness_cold == 0)
2007 mtx_assert(&w_mtx, MA_OWNED);
2009 if (witness_watch == -1) {
2010 mtx_unlock_spin(&w_mtx);
2013 if (STAILQ_EMPTY(&w_free)) {
2015 mtx_unlock_spin(&w_mtx);
2016 printf("WITNESS: unable to allocate a new witness object\n");
2019 w = STAILQ_FIRST(&w_free);
2020 STAILQ_REMOVE_HEAD(&w_free, w_list);
2023 MPASS(index > 0 && index == w_max_used_index+1 &&
2024 index < WITNESS_COUNT);
2025 bzero(w, sizeof(*w));
2027 if (index > w_max_used_index)
2028 w_max_used_index = index;
2033 witness_free(struct witness *w)
2036 STAILQ_INSERT_HEAD(&w_free, w, w_list);
2040 static struct lock_list_entry *
2041 witness_lock_list_get(void)
2043 struct lock_list_entry *lle;
2045 if (witness_watch == -1)
2047 mtx_lock_spin(&w_mtx);
2048 lle = w_lock_list_free;
2051 mtx_unlock_spin(&w_mtx);
2052 printf("%s: witness exhausted\n", __func__);
2055 w_lock_list_free = lle->ll_next;
2056 mtx_unlock_spin(&w_mtx);
2057 bzero(lle, sizeof(*lle));
2062 witness_lock_list_free(struct lock_list_entry *lle)
2065 mtx_lock_spin(&w_mtx);
2066 lle->ll_next = w_lock_list_free;
2067 w_lock_list_free = lle;
2068 mtx_unlock_spin(&w_mtx);
2071 static struct lock_instance *
2072 find_instance(struct lock_list_entry *list, struct lock_object *lock)
2074 struct lock_list_entry *lle;
2075 struct lock_instance *instance;
2078 for (lle = list; lle != NULL; lle = lle->ll_next)
2079 for (i = lle->ll_count - 1; i >= 0; i--) {
2080 instance = &lle->ll_children[i];
2081 if (instance->li_lock == lock)
2088 witness_list_lock(struct lock_instance *instance,
2089 int (*prnt)(const char *fmt, ...))
2091 struct lock_object *lock;
2093 lock = instance->li_lock;
2094 prnt("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ?
2095 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name);
2096 if (lock->lo_witness->w_name != lock->lo_name)
2097 prnt(" (%s)", lock->lo_witness->w_name);
2098 prnt(" r = %d (%p) locked @ %s:%d\n",
2099 instance->li_flags & LI_RECURSEMASK, lock,
2100 fixup_filename(instance->li_file), instance->li_line);
2105 witness_thread_has_locks(struct thread *td)
2108 if (td->td_sleeplocks == NULL)
2110 return (td->td_sleeplocks->ll_count != 0);
2114 witness_proc_has_locks(struct proc *p)
2118 FOREACH_THREAD_IN_PROC(p, td) {
2119 if (witness_thread_has_locks(td))
2127 witness_list_locks(struct lock_list_entry **lock_list,
2128 int (*prnt)(const char *fmt, ...))
2130 struct lock_list_entry *lle;
2134 for (lle = *lock_list; lle != NULL; lle = lle->ll_next)
2135 for (i = lle->ll_count - 1; i >= 0; i--) {
2136 witness_list_lock(&lle->ll_children[i], prnt);
2143 * This is a bit risky at best. We call this function when we have timed
2144 * out acquiring a spin lock, and we assume that the other CPU is stuck
2145 * with this lock held. So, we go groveling around in the other CPU's
2146 * per-cpu data to try to find the lock instance for this spin lock to
2147 * see when it was last acquired.
2150 witness_display_spinlock(struct lock_object *lock, struct thread *owner,
2151 int (*prnt)(const char *fmt, ...))
2153 struct lock_instance *instance;
2156 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU)
2158 pc = pcpu_find(owner->td_oncpu);
2159 instance = find_instance(pc->pc_spinlocks, lock);
2160 if (instance != NULL)
2161 witness_list_lock(instance, prnt);
2165 witness_save(struct lock_object *lock, const char **filep, int *linep)
2167 struct lock_list_entry *lock_list;
2168 struct lock_instance *instance;
2169 struct lock_class *class;
2172 * This function is used independently in locking code to deal with
2173 * Giant, SCHEDULER_STOPPED() check can be removed here after Giant
2176 if (SCHEDULER_STOPPED())
2178 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2179 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2181 class = LOCK_CLASS(lock);
2182 if (class->lc_flags & LC_SLEEPLOCK)
2183 lock_list = curthread->td_sleeplocks;
2185 if (witness_skipspin)
2187 lock_list = PCPU_GET(spinlocks);
2189 instance = find_instance(lock_list, lock);
2190 if (instance == NULL)
2191 panic("%s: lock (%s) %s not locked", __func__,
2192 class->lc_name, lock->lo_name);
2193 *filep = instance->li_file;
2194 *linep = instance->li_line;
2198 witness_restore(struct lock_object *lock, const char *file, int line)
2200 struct lock_list_entry *lock_list;
2201 struct lock_instance *instance;
2202 struct lock_class *class;
2205 * This function is used independently in locking code to deal with
2206 * Giant, SCHEDULER_STOPPED() check can be removed here after Giant
2209 if (SCHEDULER_STOPPED())
2211 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2212 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2214 class = LOCK_CLASS(lock);
2215 if (class->lc_flags & LC_SLEEPLOCK)
2216 lock_list = curthread->td_sleeplocks;
2218 if (witness_skipspin)
2220 lock_list = PCPU_GET(spinlocks);
2222 instance = find_instance(lock_list, lock);
2223 if (instance == NULL)
2224 panic("%s: lock (%s) %s not locked", __func__,
2225 class->lc_name, lock->lo_name);
2226 lock->lo_witness->w_file = file;
2227 lock->lo_witness->w_line = line;
2228 instance->li_file = file;
2229 instance->li_line = line;
2233 witness_assert(struct lock_object *lock, int flags, const char *file, int line)
2235 #ifdef INVARIANT_SUPPORT
2236 struct lock_instance *instance;
2237 struct lock_class *class;
2239 if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL)
2241 class = LOCK_CLASS(lock);
2242 if ((class->lc_flags & LC_SLEEPLOCK) != 0)
2243 instance = find_instance(curthread->td_sleeplocks, lock);
2244 else if ((class->lc_flags & LC_SPINLOCK) != 0)
2245 instance = find_instance(PCPU_GET(spinlocks), lock);
2247 panic("Lock (%s) %s is not sleep or spin!",
2248 class->lc_name, lock->lo_name);
2252 if (instance != NULL)
2253 panic("Lock (%s) %s locked @ %s:%d.",
2254 class->lc_name, lock->lo_name,
2255 fixup_filename(file), line);
2258 case LA_LOCKED | LA_RECURSED:
2259 case LA_LOCKED | LA_NOTRECURSED:
2261 case LA_SLOCKED | LA_RECURSED:
2262 case LA_SLOCKED | LA_NOTRECURSED:
2264 case LA_XLOCKED | LA_RECURSED:
2265 case LA_XLOCKED | LA_NOTRECURSED:
2266 if (instance == NULL) {
2267 panic("Lock (%s) %s not locked @ %s:%d.",
2268 class->lc_name, lock->lo_name,
2269 fixup_filename(file), line);
2272 if ((flags & LA_XLOCKED) != 0 &&
2273 (instance->li_flags & LI_EXCLUSIVE) == 0)
2274 panic("Lock (%s) %s not exclusively locked @ %s:%d.",
2275 class->lc_name, lock->lo_name,
2276 fixup_filename(file), line);
2277 if ((flags & LA_SLOCKED) != 0 &&
2278 (instance->li_flags & LI_EXCLUSIVE) != 0)
2279 panic("Lock (%s) %s exclusively locked @ %s:%d.",
2280 class->lc_name, lock->lo_name,
2281 fixup_filename(file), line);
2282 if ((flags & LA_RECURSED) != 0 &&
2283 (instance->li_flags & LI_RECURSEMASK) == 0)
2284 panic("Lock (%s) %s not recursed @ %s:%d.",
2285 class->lc_name, lock->lo_name,
2286 fixup_filename(file), line);
2287 if ((flags & LA_NOTRECURSED) != 0 &&
2288 (instance->li_flags & LI_RECURSEMASK) != 0)
2289 panic("Lock (%s) %s recursed @ %s:%d.",
2290 class->lc_name, lock->lo_name,
2291 fixup_filename(file), line);
2294 panic("Invalid lock assertion at %s:%d.",
2295 fixup_filename(file), line);
2298 #endif /* INVARIANT_SUPPORT */
2302 witness_setflag(struct lock_object *lock, int flag, int set)
2304 struct lock_list_entry *lock_list;
2305 struct lock_instance *instance;
2306 struct lock_class *class;
2308 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL)
2310 class = LOCK_CLASS(lock);
2311 if (class->lc_flags & LC_SLEEPLOCK)
2312 lock_list = curthread->td_sleeplocks;
2314 if (witness_skipspin)
2316 lock_list = PCPU_GET(spinlocks);
2318 instance = find_instance(lock_list, lock);
2319 if (instance == NULL)
2320 panic("%s: lock (%s) %s not locked", __func__,
2321 class->lc_name, lock->lo_name);
2324 instance->li_flags |= flag;
2326 instance->li_flags &= ~flag;
2330 witness_norelease(struct lock_object *lock)
2333 witness_setflag(lock, LI_NORELEASE, 1);
2337 witness_releaseok(struct lock_object *lock)
2340 witness_setflag(lock, LI_NORELEASE, 0);
2345 witness_ddb_list(struct thread *td)
2348 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__));
2349 KASSERT(kdb_active, ("%s: not in the debugger", __func__));
2351 if (witness_watch < 1)
2354 witness_list_locks(&td->td_sleeplocks, db_printf);
2357 * We only handle spinlocks if td == curthread. This is somewhat broken
2358 * if td is currently executing on some other CPU and holds spin locks
2359 * as we won't display those locks. If we had a MI way of getting
2360 * the per-cpu data for a given cpu then we could use
2361 * td->td_oncpu to get the list of spinlocks for this thread
2364 * That still wouldn't really fix this unless we locked the scheduler
2365 * lock or stopped the other CPU to make sure it wasn't changing the
2366 * list out from under us. It is probably best to just not try to
2367 * handle threads on other CPU's for now.
2369 if (td == curthread && PCPU_GET(spinlocks) != NULL)
2370 witness_list_locks(PCPU_PTR(spinlocks), db_printf);
2373 DB_SHOW_COMMAND(locks, db_witness_list)
2378 td = db_lookup_thread(addr, TRUE);
2381 witness_ddb_list(td);
2384 DB_SHOW_ALL_COMMAND(locks, db_witness_list_all)
2390 * It would be nice to list only threads and processes that actually
2391 * held sleep locks, but that information is currently not exported
2394 FOREACH_PROC_IN_SYSTEM(p) {
2395 if (!witness_proc_has_locks(p))
2397 FOREACH_THREAD_IN_PROC(p, td) {
2398 if (!witness_thread_has_locks(td))
2400 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid,
2401 p->p_comm, td, td->td_tid);
2402 witness_ddb_list(td);
2408 DB_SHOW_ALIAS(alllocks, db_witness_list_all)
2410 DB_SHOW_COMMAND(witness, db_witness_display)
2413 witness_ddb_display(db_printf);
2418 sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS)
2420 struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2;
2421 struct witness *tmp_w1, *tmp_w2, *w1, *w2;
2423 u_int w_rmatrix1, w_rmatrix2;
2424 int error, generation, i, j;
2430 if (witness_watch < 1) {
2431 error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning));
2435 error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold));
2439 sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND);
2443 /* Allocate and init temporary storage space. */
2444 tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO);
2445 tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO);
2446 tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP,
2448 tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP,
2450 stack_zero(&tmp_data1->wlod_stack);
2451 stack_zero(&tmp_data2->wlod_stack);
2454 mtx_lock_spin(&w_mtx);
2455 generation = w_generation;
2456 mtx_unlock_spin(&w_mtx);
2457 sbuf_printf(sb, "Number of known direct relationships is %d\n",
2458 w_lohash.wloh_count);
2459 for (i = 1; i < w_max_used_index; i++) {
2460 mtx_lock_spin(&w_mtx);
2461 if (generation != w_generation) {
2462 mtx_unlock_spin(&w_mtx);
2464 /* The graph has changed, try again. */
2471 if (w1->w_reversed == 0) {
2472 mtx_unlock_spin(&w_mtx);
2476 /* Copy w1 locally so we can release the spin lock. */
2478 mtx_unlock_spin(&w_mtx);
2480 if (tmp_w1->w_reversed == 0)
2482 for (j = 1; j < w_max_used_index; j++) {
2483 if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j)
2486 mtx_lock_spin(&w_mtx);
2487 if (generation != w_generation) {
2488 mtx_unlock_spin(&w_mtx);
2490 /* The graph has changed, try again. */
2497 data1 = witness_lock_order_get(w1, w2);
2498 data2 = witness_lock_order_get(w2, w1);
2501 * Copy information locally so we can release the
2505 w_rmatrix1 = (unsigned int)w_rmatrix[i][j];
2506 w_rmatrix2 = (unsigned int)w_rmatrix[j][i];
2509 stack_zero(&tmp_data1->wlod_stack);
2510 stack_copy(&data1->wlod_stack,
2511 &tmp_data1->wlod_stack);
2513 if (data2 && data2 != data1) {
2514 stack_zero(&tmp_data2->wlod_stack);
2515 stack_copy(&data2->wlod_stack,
2516 &tmp_data2->wlod_stack);
2518 mtx_unlock_spin(&w_mtx);
2521 "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n",
2522 tmp_w1->w_name, tmp_w1->w_class->lc_name,
2523 tmp_w2->w_name, tmp_w2->w_class->lc_name);
2526 "w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n",
2527 tmp_w1->name, tmp_w2->w_name, w_rmatrix1,
2528 tmp_w2->name, tmp_w1->w_name, w_rmatrix2);
2532 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n",
2533 tmp_w1->w_name, tmp_w1->w_class->lc_name,
2534 tmp_w2->w_name, tmp_w2->w_class->lc_name);
2535 stack_sbuf_print(sb, &tmp_data1->wlod_stack);
2536 sbuf_printf(sb, "\n");
2538 if (data2 && data2 != data1) {
2540 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n",
2541 tmp_w2->w_name, tmp_w2->w_class->lc_name,
2542 tmp_w1->w_name, tmp_w1->w_class->lc_name);
2543 stack_sbuf_print(sb, &tmp_data2->wlod_stack);
2544 sbuf_printf(sb, "\n");
2548 mtx_lock_spin(&w_mtx);
2549 if (generation != w_generation) {
2550 mtx_unlock_spin(&w_mtx);
2553 * The graph changed while we were printing stack data,
2560 mtx_unlock_spin(&w_mtx);
2562 /* Free temporary storage space. */
2563 free(tmp_data1, M_TEMP);
2564 free(tmp_data2, M_TEMP);
2565 free(tmp_w1, M_TEMP);
2566 free(tmp_w2, M_TEMP);
2569 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1);
2576 sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS)
2582 if (witness_watch < 1) {
2583 error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning));
2587 error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold));
2592 error = sysctl_wire_old_buffer(req, 0);
2595 sb = sbuf_new_for_sysctl(NULL, NULL, FULLGRAPH_SBUF_SIZE, req);
2598 sbuf_printf(sb, "\n");
2600 mtx_lock_spin(&w_mtx);
2601 STAILQ_FOREACH(w, &w_all, w_list)
2603 STAILQ_FOREACH(w, &w_all, w_list)
2604 witness_add_fullgraph(sb, w);
2605 mtx_unlock_spin(&w_mtx);
2608 * Close the sbuf and return to userland.
2610 error = sbuf_finish(sb);
2617 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS)
2621 value = witness_watch;
2622 error = sysctl_handle_int(oidp, &value, 0, req);
2623 if (error != 0 || req->newptr == NULL)
2625 if (value > 1 || value < -1 ||
2626 (witness_watch == -1 && value != witness_watch))
2628 witness_watch = value;
2633 witness_add_fullgraph(struct sbuf *sb, struct witness *w)
2637 if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0))
2641 WITNESS_INDEX_ASSERT(w->w_index);
2642 for (i = 1; i <= w_max_used_index; i++) {
2643 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) {
2644 sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name,
2646 witness_add_fullgraph(sb, &w_data[i]);
2652 * A simple hash function. Takes a key pointer and a key size. If size == 0,
2653 * interprets the key as a string and reads until the null
2654 * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit
2655 * hash value computed from the key.
2658 witness_hash_djb2(const uint8_t *key, uint32_t size)
2660 unsigned int hash = 5381;
2663 /* hash = hash * 33 + key[i] */
2665 for (i = 0; i < size; i++)
2666 hash = ((hash << 5) + hash) + (unsigned int)key[i];
2668 for (i = 0; key[i] != 0; i++)
2669 hash = ((hash << 5) + hash) + (unsigned int)key[i];
2676 * Initializes the two witness hash tables. Called exactly once from
2677 * witness_initialize().
2680 witness_init_hash_tables(void)
2684 MPASS(witness_cold);
2686 /* Initialize the hash tables. */
2687 for (i = 0; i < WITNESS_HASH_SIZE; i++)
2688 w_hash.wh_array[i] = NULL;
2690 w_hash.wh_size = WITNESS_HASH_SIZE;
2691 w_hash.wh_count = 0;
2693 /* Initialize the lock order data hash. */
2695 for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) {
2696 memset(&w_lodata[i], 0, sizeof(w_lodata[i]));
2697 w_lodata[i].wlod_next = w_lofree;
2698 w_lofree = &w_lodata[i];
2700 w_lohash.wloh_size = WITNESS_LO_HASH_SIZE;
2701 w_lohash.wloh_count = 0;
2702 for (i = 0; i < WITNESS_LO_HASH_SIZE; i++)
2703 w_lohash.wloh_array[i] = NULL;
2706 static struct witness *
2707 witness_hash_get(const char *key)
2713 if (witness_cold == 0)
2714 mtx_assert(&w_mtx, MA_OWNED);
2715 hash = witness_hash_djb2(key, 0) % w_hash.wh_size;
2716 w = w_hash.wh_array[hash];
2718 if (strcmp(w->w_name, key) == 0)
2728 witness_hash_put(struct witness *w)
2733 MPASS(w->w_name != NULL);
2734 if (witness_cold == 0)
2735 mtx_assert(&w_mtx, MA_OWNED);
2736 KASSERT(witness_hash_get(w->w_name) == NULL,
2737 ("%s: trying to add a hash entry that already exists!", __func__));
2738 KASSERT(w->w_hash_next == NULL,
2739 ("%s: w->w_hash_next != NULL", __func__));
2741 hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size;
2742 w->w_hash_next = w_hash.wh_array[hash];
2743 w_hash.wh_array[hash] = w;
2748 static struct witness_lock_order_data *
2749 witness_lock_order_get(struct witness *parent, struct witness *child)
2751 struct witness_lock_order_data *data = NULL;
2752 struct witness_lock_order_key key;
2755 MPASS(parent != NULL && child != NULL);
2756 key.from = parent->w_index;
2757 key.to = child->w_index;
2758 WITNESS_INDEX_ASSERT(key.from);
2759 WITNESS_INDEX_ASSERT(key.to);
2760 if ((w_rmatrix[parent->w_index][child->w_index]
2761 & WITNESS_LOCK_ORDER_KNOWN) == 0)
2764 hash = witness_hash_djb2((const char*)&key,
2765 sizeof(key)) % w_lohash.wloh_size;
2766 data = w_lohash.wloh_array[hash];
2767 while (data != NULL) {
2768 if (witness_lock_order_key_equal(&data->wlod_key, &key))
2770 data = data->wlod_next;
2778 * Verify that parent and child have a known relationship, are not the same,
2779 * and child is actually a child of parent. This is done without w_mtx
2780 * to avoid contention in the common case.
2783 witness_lock_order_check(struct witness *parent, struct witness *child)
2786 if (parent != child &&
2787 w_rmatrix[parent->w_index][child->w_index]
2788 & WITNESS_LOCK_ORDER_KNOWN &&
2789 isitmychild(parent, child))
2796 witness_lock_order_add(struct witness *parent, struct witness *child)
2798 struct witness_lock_order_data *data = NULL;
2799 struct witness_lock_order_key key;
2802 MPASS(parent != NULL && child != NULL);
2803 key.from = parent->w_index;
2804 key.to = child->w_index;
2805 WITNESS_INDEX_ASSERT(key.from);
2806 WITNESS_INDEX_ASSERT(key.to);
2807 if (w_rmatrix[parent->w_index][child->w_index]
2808 & WITNESS_LOCK_ORDER_KNOWN)
2811 hash = witness_hash_djb2((const char*)&key,
2812 sizeof(key)) % w_lohash.wloh_size;
2813 w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN;
2817 w_lofree = data->wlod_next;
2818 data->wlod_next = w_lohash.wloh_array[hash];
2819 data->wlod_key = key;
2820 w_lohash.wloh_array[hash] = data;
2821 w_lohash.wloh_count++;
2822 stack_zero(&data->wlod_stack);
2823 stack_save(&data->wlod_stack);
2827 /* Call this whenver the structure of the witness graph changes. */
2829 witness_increment_graph_generation(void)
2832 if (witness_cold == 0)
2833 mtx_assert(&w_mtx, MA_OWNED);
2839 _witness_debugger(int cond, const char *msg)
2842 if (witness_trace && cond)
2844 if (witness_kdb && cond)
2845 kdb_enter(KDB_WHY_WITNESS, msg);