1 /*- 2 * Copyright (c) 2008 Isilon Systems, Inc. 3 * Copyright (c) 2008 Ilya Maykov <ivmaykov@gmail.com> 4 * Copyright (c) 1998 Berkeley Software Design, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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 17 * written permission. 18 * 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 29 * SUCH DAMAGE. 30 * 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 $ 33 */ 34 35 /* 36 * Implementation of the `witness' lock verifier. Originally implemented for 37 * mutexes in BSD/OS. Extended to handle generic lock objects and lock 38 * classes in FreeBSD. 39 */ 40 41 /* 42 * Main Entry: witness 43 * Pronunciation: 'wit-n&s 44 * Function: noun 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 57 * life -- Pilot> 58 * 6 capitalized : a member of the Jehovah's Witnesses 59 */ 60 61 /* 62 * Special rules concerning Giant and lock orders: 63 * 64 * 1) Giant must be acquired before any other mutexes. Stated another way, 65 * no other mutex may be held when Giant is acquired. 66 * 67 * 2) Giant must be released when blocking on a sleepable lock. 68 * 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 72 * 2). 73 * 74 * 3) Giant may be acquired before or after sleepable locks. 75 * 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. 85 */ 86 87 #include <sys/cdefs.h> 88 __FBSDID("$FreeBSD$"); 89 90 #include "opt_ddb.h" 91 #include "opt_hwpmc_hooks.h" 92 #include "opt_stack.h" 93 #include "opt_witness.h" 94 95 #include <sys/param.h> 96 #include <sys/bus.h> 97 #include <sys/kdb.h> 98 #include <sys/kernel.h> 99 #include <sys/ktr.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/stack.h> 107 #include <sys/sysctl.h> 108 #include <sys/systm.h> 109 110 #ifdef DDB 111 #include <ddb/ddb.h> 112 #endif 113 114 #include <machine/stdarg.h> 115 116 #if !defined(DDB) && !defined(STACK) 117 #error "DDB or STACK options are required for WITNESS" 118 #endif 119 120 /* Note that these traces do not work with KTR_ALQ. */ 121 #if 0 122 #define KTR_WITNESS KTR_SUBSYS 123 #else 124 #define KTR_WITNESS 0 125 #endif 126 127 #define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */ 128 #define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */ 129 130 /* Define this to check for blessed mutexes */ 131 #undef BLESSING 132 133 #define WITNESS_COUNT 1024 134 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 135 #define WITNESS_HASH_SIZE 251 /* Prime, gives load factor < 2 */ 136 #define WITNESS_PENDLIST 512 137 138 /* Allocate 256 KB of stack data space */ 139 #define WITNESS_LO_DATA_COUNT 2048 140 141 /* Prime, gives load factor of ~2 at full load */ 142 #define WITNESS_LO_HASH_SIZE 1021 143 144 /* 145 * XXX: This is somewhat bogus, as we assume here that at most 2048 threads 146 * will hold LOCK_NCHILDREN locks. We handle failure ok, and we should 147 * probably be safe for the most part, but it's still a SWAG. 148 */ 149 #define LOCK_NCHILDREN 5 150 #define LOCK_CHILDCOUNT 2048 151 152 #define MAX_W_NAME 64 153 154 #define BADSTACK_SBUF_SIZE (256 * WITNESS_COUNT) 155 #define CYCLEGRAPH_SBUF_SIZE 8192 156 #define FULLGRAPH_SBUF_SIZE 32768 157 158 /* 159 * These flags go in the witness relationship matrix and describe the 160 * relationship between any two struct witness objects. 161 */ 162 #define WITNESS_UNRELATED 0x00 /* No lock order relation. */ 163 #define WITNESS_PARENT 0x01 /* Parent, aka direct ancestor. */ 164 #define WITNESS_ANCESTOR 0x02 /* Direct or indirect ancestor. */ 165 #define WITNESS_CHILD 0x04 /* Child, aka direct descendant. */ 166 #define WITNESS_DESCENDANT 0x08 /* Direct or indirect descendant. */ 167 #define WITNESS_ANCESTOR_MASK (WITNESS_PARENT | WITNESS_ANCESTOR) 168 #define WITNESS_DESCENDANT_MASK (WITNESS_CHILD | WITNESS_DESCENDANT) 169 #define WITNESS_RELATED_MASK \ 170 (WITNESS_ANCESTOR_MASK | WITNESS_DESCENDANT_MASK) 171 #define WITNESS_REVERSAL 0x10 /* A lock order reversal has been 172 * observed. */ 173 #define WITNESS_RESERVED1 0x20 /* Unused flag, reserved. */ 174 #define WITNESS_RESERVED2 0x40 /* Unused flag, reserved. */ 175 #define WITNESS_LOCK_ORDER_KNOWN 0x80 /* This lock order is known. */ 176 177 /* Descendant to ancestor flags */ 178 #define WITNESS_DTOA(x) (((x) & WITNESS_RELATED_MASK) >> 2) 179 180 /* Ancestor to descendant flags */ 181 #define WITNESS_ATOD(x) (((x) & WITNESS_RELATED_MASK) << 2) 182 183 #define WITNESS_INDEX_ASSERT(i) \ 184 MPASS((i) > 0 && (i) <= w_max_used_index && (i) < WITNESS_COUNT) 185 186 MALLOC_DEFINE(M_WITNESS, "Witness", "Witness"); 187 188 /* 189 * Lock instances. A lock instance is the data associated with a lock while 190 * it is held by witness. For example, a lock instance will hold the 191 * recursion count of a lock. Lock instances are held in lists. Spin locks 192 * are held in a per-cpu list while sleep locks are held in per-thread list. 193 */ 194 struct lock_instance { 195 struct lock_object *li_lock; 196 const char *li_file; 197 int li_line; 198 u_int li_flags; 199 }; 200 201 /* 202 * A simple list type used to build the list of locks held by a thread 203 * or CPU. We can't simply embed the list in struct lock_object since a 204 * lock may be held by more than one thread if it is a shared lock. Locks 205 * are added to the head of the list, so we fill up each list entry from 206 * "the back" logically. To ease some of the arithmetic, we actually fill 207 * in each list entry the normal way (children[0] then children[1], etc.) but 208 * when we traverse the list we read children[count-1] as the first entry 209 * down to children[0] as the final entry. 210 */ 211 struct lock_list_entry { 212 struct lock_list_entry *ll_next; 213 struct lock_instance ll_children[LOCK_NCHILDREN]; 214 u_int ll_count; 215 }; 216 217 /* 218 * The main witness structure. One of these per named lock type in the system 219 * (for example, "vnode interlock"). 220 */ 221 struct witness { 222 char w_name[MAX_W_NAME]; 223 uint32_t w_index; /* Index in the relationship matrix */ 224 struct lock_class *w_class; 225 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 226 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 227 struct witness *w_hash_next; /* Linked list in hash buckets. */ 228 const char *w_file; /* File where last acquired */ 229 uint32_t w_line; /* Line where last acquired */ 230 uint32_t w_refcount; 231 uint16_t w_num_ancestors; /* direct/indirect 232 * ancestor count */ 233 uint16_t w_num_descendants; /* direct/indirect 234 * descendant count */ 235 int16_t w_ddb_level; 236 int w_displayed:1; 237 int w_reversed:1; 238 }; 239 240 STAILQ_HEAD(witness_list, witness); 241 242 /* 243 * The witness hash table. Keys are witness names (const char *), elements are 244 * witness objects (struct witness *). 245 */ 246 struct witness_hash { 247 struct witness *wh_array[WITNESS_HASH_SIZE]; 248 uint32_t wh_size; 249 uint32_t wh_count; 250 }; 251 252 /* 253 * Key type for the lock order data hash table. 254 */ 255 struct witness_lock_order_key { 256 uint16_t from; 257 uint16_t to; 258 }; 259 260 struct witness_lock_order_data { 261 struct stack wlod_stack; 262 struct witness_lock_order_key wlod_key; 263 struct witness_lock_order_data *wlod_next; 264 }; 265 266 /* 267 * The witness lock order data hash table. Keys are witness index tuples 268 * (struct witness_lock_order_key), elements are lock order data objects 269 * (struct witness_lock_order_data). 270 */ 271 struct witness_lock_order_hash { 272 struct witness_lock_order_data *wloh_array[WITNESS_LO_HASH_SIZE]; 273 u_int wloh_size; 274 u_int wloh_count; 275 }; 276 277 #ifdef BLESSING 278 struct witness_blessed { 279 const char *b_lock1; 280 const char *b_lock2; 281 }; 282 #endif 283 284 struct witness_pendhelp { 285 const char *wh_type; 286 struct lock_object *wh_lock; 287 }; 288 289 struct witness_order_list_entry { 290 const char *w_name; 291 struct lock_class *w_class; 292 }; 293 294 /* 295 * Returns 0 if one of the locks is a spin lock and the other is not. 296 * Returns 1 otherwise. 297 */ 298 static __inline int 299 witness_lock_type_equal(struct witness *w1, struct witness *w2) 300 { 301 302 return ((w1->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) == 303 (w2->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))); 304 } 305 306 static __inline int 307 witness_lock_order_key_empty(const struct witness_lock_order_key *key) 308 { 309 310 return (key->from == 0 && key->to == 0); 311 } 312 313 static __inline int 314 witness_lock_order_key_equal(const struct witness_lock_order_key *a, 315 const struct witness_lock_order_key *b) 316 { 317 318 return (a->from == b->from && a->to == b->to); 319 } 320 321 static int _isitmyx(struct witness *w1, struct witness *w2, int rmask, 322 const char *fname); 323 #ifdef KDB 324 static void _witness_debugger(int cond, const char *msg); 325 #endif 326 static void adopt(struct witness *parent, struct witness *child); 327 #ifdef BLESSING 328 static int blessed(struct witness *, struct witness *); 329 #endif 330 static void depart(struct witness *w); 331 static struct witness *enroll(const char *description, 332 struct lock_class *lock_class); 333 static struct lock_instance *find_instance(struct lock_list_entry *list, 334 struct lock_object *lock); 335 static int isitmychild(struct witness *parent, struct witness *child); 336 static int isitmydescendant(struct witness *parent, struct witness *child); 337 static void itismychild(struct witness *parent, struct witness *child); 338 static int sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS); 339 static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 340 static int sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS); 341 static void witness_add_fullgraph(struct sbuf *sb, struct witness *parent); 342 #ifdef DDB 343 static void witness_ddb_compute_levels(void); 344 static void witness_ddb_display(void(*)(const char *fmt, ...)); 345 static void witness_ddb_display_descendants(void(*)(const char *fmt, ...), 346 struct witness *, int indent); 347 static void witness_ddb_display_list(void(*prnt)(const char *fmt, ...), 348 struct witness_list *list); 349 static void witness_ddb_level_descendants(struct witness *parent, int l); 350 static void witness_ddb_list(struct thread *td); 351 #endif 352 static void witness_free(struct witness *m); 353 static struct witness *witness_get(void); 354 static uint32_t witness_hash_djb2(const uint8_t *key, uint32_t size); 355 static struct witness *witness_hash_get(const char *key); 356 static void witness_hash_put(struct witness *w); 357 static void witness_init_hash_tables(void); 358 static void witness_increment_graph_generation(void); 359 static void witness_lock_list_free(struct lock_list_entry *lle); 360 static struct lock_list_entry *witness_lock_list_get(void); 361 static int witness_lock_order_add(struct witness *parent, 362 struct witness *child); 363 static int witness_lock_order_check(struct witness *parent, 364 struct witness *child); 365 static struct witness_lock_order_data *witness_lock_order_get( 366 struct witness *parent, 367 struct witness *child); 368 static void witness_list_lock(struct lock_instance *instance); 369 370 #ifdef KDB 371 #define witness_debugger(c) _witness_debugger(c, __func__) 372 #else 373 #define witness_debugger(c) 374 #endif 375 376 SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking"); 377 378 /* 379 * If set to 0, lock order checking is disabled. If set to -1, 380 * witness is completely disabled. Otherwise witness performs full 381 * lock order checking for all locks. At runtime, lock order checking 382 * may be toggled. However, witness cannot be reenabled once it is 383 * completely disabled. 384 */ 385 static int witness_watch = 1; 386 TUNABLE_INT("debug.witness.watch", &witness_watch); 387 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 388 sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 389 390 #ifdef KDB 391 /* 392 * When KDB is enabled and witness_kdb is 1, it will cause the system 393 * to drop into kdebug() when: 394 * - a lock hierarchy violation occurs 395 * - locks are held when going to sleep. 396 */ 397 #ifdef WITNESS_KDB 398 int witness_kdb = 1; 399 #else 400 int witness_kdb = 0; 401 #endif 402 TUNABLE_INT("debug.witness.kdb", &witness_kdb); 403 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 404 405 /* 406 * When KDB is enabled and witness_trace is 1, it will cause the system 407 * to print a stack trace: 408 * - a lock hierarchy violation occurs 409 * - locks are held when going to sleep. 410 */ 411 int witness_trace = 1; 412 TUNABLE_INT("debug.witness.trace", &witness_trace); 413 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 414 #endif /* KDB */ 415 416 #ifdef WITNESS_SKIPSPIN 417 int witness_skipspin = 1; 418 #else 419 int witness_skipspin = 0; 420 #endif 421 TUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 422 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, &witness_skipspin, 423 0, ""); 424 425 /* 426 * Call this to print out the relations between locks. 427 */ 428 SYSCTL_PROC(_debug_witness, OID_AUTO, fullgraph, CTLTYPE_STRING | CTLFLAG_RD, 429 NULL, 0, sysctl_debug_witness_fullgraph, "A", "Show locks relation graphs"); 430 431 /* 432 * Call this to print out the witness faulty stacks. 433 */ 434 SYSCTL_PROC(_debug_witness, OID_AUTO, badstacks, CTLTYPE_STRING | CTLFLAG_RD, 435 NULL, 0, sysctl_debug_witness_badstacks, "A", "Show bad witness stacks"); 436 437 static struct mtx w_mtx; 438 439 /* w_list */ 440 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 441 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 442 443 /* w_typelist */ 444 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 445 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 446 447 /* lock list */ 448 static struct lock_list_entry *w_lock_list_free = NULL; 449 static struct witness_pendhelp pending_locks[WITNESS_PENDLIST]; 450 static u_int pending_cnt; 451 452 static int w_free_cnt, w_spin_cnt, w_sleep_cnt; 453 SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, ""); 454 SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, ""); 455 SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0, 456 ""); 457 458 static struct witness *w_data; 459 static uint8_t w_rmatrix[WITNESS_COUNT+1][WITNESS_COUNT+1]; 460 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 461 static struct witness_hash w_hash; /* The witness hash table. */ 462 463 /* The lock order data hash */ 464 static struct witness_lock_order_data w_lodata[WITNESS_LO_DATA_COUNT]; 465 static struct witness_lock_order_data *w_lofree = NULL; 466 static struct witness_lock_order_hash w_lohash; 467 static int w_max_used_index = 0; 468 static unsigned int w_generation = 0; 469 static const char *w_notrunning = "Witness not running\n"; 470 static const char *w_stillcold = "Witness is still cold\n"; 471 472 473 static struct witness_order_list_entry order_lists[] = { 474 /* 475 * sx locks 476 */ 477 { "proctree", &lock_class_sx }, 478 { "allproc", &lock_class_sx }, 479 { "allprison", &lock_class_sx }, 480 { NULL, NULL }, 481 /* 482 * Various mutexes 483 */ 484 { "Giant", &lock_class_mtx_sleep }, 485 { "pipe mutex", &lock_class_mtx_sleep }, 486 { "sigio lock", &lock_class_mtx_sleep }, 487 { "process group", &lock_class_mtx_sleep }, 488 { "process lock", &lock_class_mtx_sleep }, 489 { "session", &lock_class_mtx_sleep }, 490 { "uidinfo hash", &lock_class_rw }, 491 #ifdef HWPMC_HOOKS 492 { "pmc-sleep", &lock_class_mtx_sleep }, 493 #endif 494 { NULL, NULL }, 495 /* 496 * Sockets 497 */ 498 { "accept", &lock_class_mtx_sleep }, 499 { "so_snd", &lock_class_mtx_sleep }, 500 { "so_rcv", &lock_class_mtx_sleep }, 501 { "sellck", &lock_class_mtx_sleep }, 502 { NULL, NULL }, 503 /* 504 * Routing 505 */ 506 { "so_rcv", &lock_class_mtx_sleep }, 507 { "radix node head", &lock_class_mtx_sleep }, 508 { "rtentry", &lock_class_mtx_sleep }, 509 { "ifaddr", &lock_class_mtx_sleep }, 510 { NULL, NULL }, 511 /* 512 * Multicast - protocol locks before interface locks, after UDP locks. 513 */ 514 { "udpinp", &lock_class_rw }, 515 { "in_multi_mtx", &lock_class_mtx_sleep }, 516 { "igmp_mtx", &lock_class_mtx_sleep }, 517 { "if_addr_mtx", &lock_class_mtx_sleep }, 518 { NULL, NULL }, 519 /* 520 * UNIX Domain Sockets 521 */ 522 { "unp", &lock_class_mtx_sleep }, 523 { "so_snd", &lock_class_mtx_sleep }, 524 { NULL, NULL }, 525 /* 526 * UDP/IP 527 */ 528 { "udp", &lock_class_rw }, 529 { "udpinp", &lock_class_rw }, 530 { "so_snd", &lock_class_mtx_sleep }, 531 { NULL, NULL }, 532 /* 533 * TCP/IP 534 */ 535 { "tcp", &lock_class_rw }, 536 { "tcpinp", &lock_class_rw }, 537 { "so_snd", &lock_class_mtx_sleep }, 538 { NULL, NULL }, 539 /* 540 * SLIP 541 */ 542 { "slip_mtx", &lock_class_mtx_sleep }, 543 { "slip sc_mtx", &lock_class_mtx_sleep }, 544 { NULL, NULL }, 545 /* 546 * netatalk 547 */ 548 { "ddp_list_mtx", &lock_class_mtx_sleep }, 549 { "ddp_mtx", &lock_class_mtx_sleep }, 550 { NULL, NULL }, 551 /* 552 * BPF 553 */ 554 { "bpf global lock", &lock_class_mtx_sleep }, 555 { "bpf interface lock", &lock_class_mtx_sleep }, 556 { "bpf cdev lock", &lock_class_mtx_sleep }, 557 { NULL, NULL }, 558 /* 559 * NFS server 560 */ 561 { "nfsd_mtx", &lock_class_mtx_sleep }, 562 { "so_snd", &lock_class_mtx_sleep }, 563 { NULL, NULL }, 564 565 /* 566 * IEEE 802.11 567 */ 568 { "802.11 com lock", &lock_class_mtx_sleep}, 569 { NULL, NULL }, 570 /* 571 * Network drivers 572 */ 573 { "network driver", &lock_class_mtx_sleep}, 574 { NULL, NULL }, 575 576 /* 577 * Netgraph 578 */ 579 { "ng_node", &lock_class_mtx_sleep }, 580 { "ng_worklist", &lock_class_mtx_sleep }, 581 { NULL, NULL }, 582 /* 583 * CDEV 584 */ 585 { "system map", &lock_class_mtx_sleep }, 586 { "vm page queue mutex", &lock_class_mtx_sleep }, 587 { "vnode interlock", &lock_class_mtx_sleep }, 588 { "cdev", &lock_class_mtx_sleep }, 589 { NULL, NULL }, 590 /* 591 * kqueue/VFS interaction 592 */ 593 { "kqueue", &lock_class_mtx_sleep }, 594 { "struct mount mtx", &lock_class_mtx_sleep }, 595 { "vnode interlock", &lock_class_mtx_sleep }, 596 { NULL, NULL }, 597 /* 598 * spin locks 599 */ 600 #ifdef SMP 601 { "ap boot", &lock_class_mtx_spin }, 602 #endif 603 { "rm.mutex_mtx", &lock_class_mtx_spin }, 604 { "sio", &lock_class_mtx_spin }, 605 { "scrlock", &lock_class_mtx_spin }, 606 #ifdef __i386__ 607 { "cy", &lock_class_mtx_spin }, 608 #endif 609 #ifdef __sparc64__ 610 { "pcib_mtx", &lock_class_mtx_spin }, 611 { "rtc_mtx", &lock_class_mtx_spin }, 612 #endif 613 { "scc_hwmtx", &lock_class_mtx_spin }, 614 { "uart_hwmtx", &lock_class_mtx_spin }, 615 { "fast_taskqueue", &lock_class_mtx_spin }, 616 { "intr table", &lock_class_mtx_spin }, 617 #ifdef HWPMC_HOOKS 618 { "pmc-per-proc", &lock_class_mtx_spin }, 619 #endif 620 { "process slock", &lock_class_mtx_spin }, 621 { "sleepq chain", &lock_class_mtx_spin }, 622 { "umtx lock", &lock_class_mtx_spin }, 623 { "rm_spinlock", &lock_class_mtx_spin }, 624 { "turnstile chain", &lock_class_mtx_spin }, 625 { "turnstile lock", &lock_class_mtx_spin }, 626 { "sched lock", &lock_class_mtx_spin }, 627 { "td_contested", &lock_class_mtx_spin }, 628 { "callout", &lock_class_mtx_spin }, 629 { "entropy harvest mutex", &lock_class_mtx_spin }, 630 { "syscons video lock", &lock_class_mtx_spin }, 631 { "time lock", &lock_class_mtx_spin }, 632 #ifdef SMP 633 { "smp rendezvous", &lock_class_mtx_spin }, 634 #endif 635 #ifdef __powerpc__ 636 { "tlb0", &lock_class_mtx_spin }, 637 #endif 638 /* 639 * leaf locks 640 */ 641 { "intrcnt", &lock_class_mtx_spin }, 642 { "icu", &lock_class_mtx_spin }, 643 #if defined(SMP) && defined(__sparc64__) 644 { "ipi", &lock_class_mtx_spin }, 645 #endif 646 #ifdef __i386__ 647 { "allpmaps", &lock_class_mtx_spin }, 648 { "descriptor tables", &lock_class_mtx_spin }, 649 #endif 650 { "clk", &lock_class_mtx_spin }, 651 { "cpuset", &lock_class_mtx_spin }, 652 { "mprof lock", &lock_class_mtx_spin }, 653 { "zombie lock", &lock_class_mtx_spin }, 654 { "ALD Queue", &lock_class_mtx_spin }, 655 #ifdef __ia64__ 656 { "MCA spin lock", &lock_class_mtx_spin }, 657 #endif 658 #if defined(__i386__) || defined(__amd64__) 659 { "pcicfg", &lock_class_mtx_spin }, 660 { "NDIS thread lock", &lock_class_mtx_spin }, 661 #endif 662 { "tw_osl_io_lock", &lock_class_mtx_spin }, 663 { "tw_osl_q_lock", &lock_class_mtx_spin }, 664 { "tw_cl_io_lock", &lock_class_mtx_spin }, 665 { "tw_cl_intr_lock", &lock_class_mtx_spin }, 666 { "tw_cl_gen_lock", &lock_class_mtx_spin }, 667 #ifdef HWPMC_HOOKS 668 { "pmc-leaf", &lock_class_mtx_spin }, 669 #endif 670 { "blocked lock", &lock_class_mtx_spin }, 671 { NULL, NULL }, 672 { NULL, NULL } 673 }; 674 675 #ifdef BLESSING 676 /* 677 * Pairs of locks which have been blessed 678 * Don't complain about order problems with blessed locks 679 */ 680 static struct witness_blessed blessed_list[] = { 681 }; 682 static int blessed_count = 683 sizeof(blessed_list) / sizeof(struct witness_blessed); 684 #endif 685 686 /* 687 * This global is set to 0 once it becomes safe to use the witness code. 688 */ 689 static int witness_cold = 1; 690 691 /* 692 * This global is set to 1 once the static lock orders have been enrolled 693 * so that a warning can be issued for any spin locks enrolled later. 694 */ 695 static int witness_spin_warn = 0; 696 697 /* 698 * The WITNESS-enabled diagnostic code. Note that the witness code does 699 * assume that the early boot is single-threaded at least until after this 700 * routine is completed. 701 */ 702 static void 703 witness_initialize(void *dummy __unused) 704 { 705 struct lock_object *lock; 706 struct witness_order_list_entry *order; 707 struct witness *w, *w1; 708 int i; 709 710 MALLOC(w_data, struct witness *, 711 sizeof (struct witness) * WITNESS_COUNT, M_WITNESS, 712 M_NOWAIT | M_ZERO); 713 714 /* 715 * We have to release Giant before initializing its witness 716 * structure so that WITNESS doesn't get confused. 717 */ 718 mtx_unlock(&Giant); 719 mtx_assert(&Giant, MA_NOTOWNED); 720 721 CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 722 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 723 MTX_NOWITNESS | MTX_NOPROFILE); 724 for (i = WITNESS_COUNT - 1; i >= 0; i--) { 725 w = &w_data[i]; 726 memset(w, 0, sizeof(*w)); 727 w_data[i].w_index = i; /* Witness index never changes. */ 728 witness_free(w); 729 } 730 KASSERT(STAILQ_FIRST(&w_free)->w_index == 0, 731 ("%s: Invalid list of free witness objects", __func__)); 732 733 /* Witness with index 0 is not used to aid in debugging. */ 734 STAILQ_REMOVE_HEAD(&w_free, w_list); 735 w_free_cnt--; 736 737 memset(w_rmatrix, 0, 738 (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1))); 739 740 for (i = 0; i < LOCK_CHILDCOUNT; i++) 741 witness_lock_list_free(&w_locklistdata[i]); 742 witness_init_hash_tables(); 743 744 /* First add in all the specified order lists. */ 745 for (order = order_lists; order->w_name != NULL; order++) { 746 w = enroll(order->w_name, order->w_class); 747 if (w == NULL) 748 continue; 749 w->w_file = "order list"; 750 for (order++; order->w_name != NULL; order++) { 751 w1 = enroll(order->w_name, order->w_class); 752 if (w1 == NULL) 753 continue; 754 w1->w_file = "order list"; 755 itismychild(w, w1); 756 w = w1; 757 } 758 } 759 witness_spin_warn = 1; 760 761 /* Iterate through all locks and add them to witness. */ 762 for (i = 0; pending_locks[i].wh_lock != NULL; i++) { 763 lock = pending_locks[i].wh_lock; 764 KASSERT(lock->lo_flags & LO_WITNESS, 765 ("%s: lock %s is on pending list but not LO_WITNESS", 766 __func__, lock->lo_name)); 767 lock->lo_witness = enroll(pending_locks[i].wh_type, 768 LOCK_CLASS(lock)); 769 } 770 771 /* Mark the witness code as being ready for use. */ 772 witness_cold = 0; 773 774 mtx_lock(&Giant); 775 } 776 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, 777 NULL); 778 779 void 780 witness_init(struct lock_object *lock, const char *type) 781 { 782 struct lock_class *class; 783 784 /* Various sanity checks. */ 785 class = LOCK_CLASS(lock); 786 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 787 (class->lc_flags & LC_RECURSABLE) == 0) 788 panic("%s: lock (%s) %s can not be recursable", __func__, 789 class->lc_name, lock->lo_name); 790 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 791 (class->lc_flags & LC_SLEEPABLE) == 0) 792 panic("%s: lock (%s) %s can not be sleepable", __func__, 793 class->lc_name, lock->lo_name); 794 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 795 (class->lc_flags & LC_UPGRADABLE) == 0) 796 panic("%s: lock (%s) %s can not be upgradable", __func__, 797 class->lc_name, lock->lo_name); 798 799 /* 800 * If we shouldn't watch this lock, then just clear lo_witness. 801 * Otherwise, if witness_cold is set, then it is too early to 802 * enroll this lock, so defer it to witness_initialize() by adding 803 * it to the pending_locks list. If it is not too early, then enroll 804 * the lock now. 805 */ 806 if (witness_watch < 1 || panicstr != NULL || 807 (lock->lo_flags & LO_WITNESS) == 0) 808 lock->lo_witness = NULL; 809 else if (witness_cold) { 810 pending_locks[pending_cnt].wh_lock = lock; 811 pending_locks[pending_cnt++].wh_type = type; 812 if (pending_cnt > WITNESS_PENDLIST) 813 panic("%s: pending locks list is too small, bump it\n", 814 __func__); 815 } else 816 lock->lo_witness = enroll(type, class); 817 } 818 819 void 820 witness_destroy(struct lock_object *lock) 821 { 822 struct lock_class *class; 823 struct witness *w; 824 825 class = LOCK_CLASS(lock); 826 827 if (witness_cold) 828 panic("lock (%s) %s destroyed while witness_cold", 829 class->lc_name, lock->lo_name); 830 831 /* XXX: need to verify that no one holds the lock */ 832 if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL) 833 return; 834 w = lock->lo_witness; 835 836 mtx_lock_spin(&w_mtx); 837 MPASS(w->w_refcount > 0); 838 w->w_refcount--; 839 840 if (w->w_refcount == 0) 841 depart(w); 842 mtx_unlock_spin(&w_mtx); 843 } 844 845 #ifdef DDB 846 static void 847 witness_ddb_compute_levels(void) 848 { 849 struct witness *w; 850 851 /* 852 * First clear all levels. 853 */ 854 STAILQ_FOREACH(w, &w_all, w_list) 855 w->w_ddb_level = -1; 856 857 /* 858 * Look for locks with no parents and level all their descendants. 859 */ 860 STAILQ_FOREACH(w, &w_all, w_list) { 861 862 /* If the witness has ancestors (is not a root), skip it. */ 863 if (w->w_num_ancestors > 0) 864 continue; 865 witness_ddb_level_descendants(w, 0); 866 } 867 } 868 869 static void 870 witness_ddb_level_descendants(struct witness *w, int l) 871 { 872 int i; 873 874 if (w->w_ddb_level >= l) 875 return; 876 877 w->w_ddb_level = l; 878 l++; 879 880 for (i = 1; i <= w_max_used_index; i++) { 881 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 882 witness_ddb_level_descendants(&w_data[i], l); 883 } 884 } 885 886 static void 887 witness_ddb_display_descendants(void(*prnt)(const char *fmt, ...), 888 struct witness *w, int indent) 889 { 890 int i; 891 892 for (i = 0; i < indent; i++) 893 prnt(" "); 894 prnt("%s (type: %s, depth: %d, active refs: %d)", 895 w->w_name, w->w_class->lc_name, 896 w->w_ddb_level, w->w_refcount); 897 if (w->w_displayed) { 898 prnt(" -- (already displayed)\n"); 899 return; 900 } 901 w->w_displayed = 1; 902 if (w->w_file != NULL && w->w_line != 0) 903 prnt(" -- last acquired @ %s:%d\n", w->w_file, 904 w->w_line); 905 else 906 prnt(" -- never acquired\n"); 907 indent++; 908 WITNESS_INDEX_ASSERT(w->w_index); 909 for (i = 1; i <= w_max_used_index; i++) { 910 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 911 witness_ddb_display_descendants(prnt, &w_data[i], 912 indent); 913 } 914 } 915 916 static void 917 witness_ddb_display_list(void(*prnt)(const char *fmt, ...), 918 struct witness_list *list) 919 { 920 struct witness *w; 921 922 STAILQ_FOREACH(w, list, w_typelist) { 923 if (w->w_file == NULL || w->w_ddb_level > 0) 924 continue; 925 926 /* This lock has no anscestors - display its descendants. */ 927 witness_ddb_display_descendants(prnt, w, 0); 928 } 929 } 930 931 static void 932 witness_ddb_display(void(*prnt)(const char *fmt, ...)) 933 { 934 struct witness *w; 935 936 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 937 witness_ddb_compute_levels(); 938 939 /* Clear all the displayed flags. */ 940 STAILQ_FOREACH(w, &w_all, w_list) 941 w->w_displayed = 0; 942 943 /* 944 * First, handle sleep locks which have been acquired at least 945 * once. 946 */ 947 prnt("Sleep locks:\n"); 948 witness_ddb_display_list(prnt, &w_sleep); 949 950 /* 951 * Now do spin locks which have been acquired at least once. 952 */ 953 prnt("\nSpin locks:\n"); 954 witness_ddb_display_list(prnt, &w_spin); 955 956 /* 957 * Finally, any locks which have not been acquired yet. 958 */ 959 prnt("\nLocks which were never acquired:\n"); 960 STAILQ_FOREACH(w, &w_all, w_list) { 961 if (w->w_file != NULL || w->w_refcount == 0) 962 continue; 963 prnt("%s (type: %s, depth: %d)\n", w->w_name, 964 w->w_class->lc_name, w->w_ddb_level); 965 } 966 } 967 #endif /* DDB */ 968 969 /* Trim useless garbage from filenames. */ 970 static const char * 971 fixup_filename(const char *file) 972 { 973 974 if (file == NULL) 975 return (NULL); 976 while (strncmp(file, "../", 3) == 0) 977 file += 3; 978 return (file); 979 } 980 981 int 982 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 983 { 984 985 if (witness_watch == -1 || panicstr != NULL) 986 return (0); 987 988 /* Require locks that witness knows about. */ 989 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 990 lock2->lo_witness == NULL) 991 return (EINVAL); 992 993 mtx_assert(&w_mtx, MA_NOTOWNED); 994 mtx_lock_spin(&w_mtx); 995 996 /* 997 * If we already have either an explicit or implied lock order that 998 * is the other way around, then return an error. 999 */ 1000 if (witness_watch && 1001 isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 1002 mtx_unlock_spin(&w_mtx); 1003 return (EDOOFUS); 1004 } 1005 1006 /* Try to add the new order. */ 1007 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1008 lock2->lo_witness->w_name, lock1->lo_witness->w_name); 1009 itismychild(lock1->lo_witness, lock2->lo_witness); 1010 mtx_unlock_spin(&w_mtx); 1011 return (0); 1012 } 1013 1014 void 1015 witness_checkorder(struct lock_object *lock, int flags, const char *file, 1016 int line, struct lock_object *interlock) 1017 { 1018 struct lock_list_entry **lock_list, *lle; 1019 struct lock_instance *lock1, *lock2, *plock; 1020 struct lock_class *class; 1021 struct witness *w, *w1; 1022 struct thread *td; 1023 int i, j; 1024 1025 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL || 1026 panicstr != NULL) 1027 return; 1028 1029 w = lock->lo_witness; 1030 class = LOCK_CLASS(lock); 1031 td = curthread; 1032 file = fixup_filename(file); 1033 1034 if (class->lc_flags & LC_SLEEPLOCK) { 1035 1036 /* 1037 * Since spin locks include a critical section, this check 1038 * implicitly enforces a lock order of all sleep locks before 1039 * all spin locks. 1040 */ 1041 if (td->td_critnest != 0 && !kdb_active) 1042 panic("blockable sleep lock (%s) %s @ %s:%d", 1043 class->lc_name, lock->lo_name, file, line); 1044 1045 /* 1046 * If this is the first lock acquired then just return as 1047 * no order checking is needed. 1048 */ 1049 if (td->td_sleeplocks == NULL) 1050 return; 1051 lock_list = &td->td_sleeplocks; 1052 } else { 1053 1054 /* 1055 * If this is the first lock, just return as no order 1056 * checking is needed. We check this in both if clauses 1057 * here as unifying the check would require us to use a 1058 * critical section to ensure we don't migrate while doing 1059 * the check. Note that if this is not the first lock, we 1060 * are already in a critical section and are safe for the 1061 * rest of the check. 1062 */ 1063 if (PCPU_GET(spinlocks) == NULL) 1064 return; 1065 lock_list = PCPU_PTR(spinlocks); 1066 } 1067 1068 /* Empty list? */ 1069 if ((*lock_list)->ll_count == 0) 1070 return; 1071 1072 /* 1073 * Check to see if we are recursing on a lock we already own. If 1074 * so, make sure that we don't mismatch exclusive and shared lock 1075 * acquires. 1076 */ 1077 lock1 = find_instance(*lock_list, lock); 1078 if (lock1 != NULL) { 1079 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 1080 (flags & LOP_EXCLUSIVE) == 0) { 1081 printf("shared lock of (%s) %s @ %s:%d\n", 1082 class->lc_name, lock->lo_name, file, line); 1083 printf("while exclusively locked from %s:%d\n", 1084 lock1->li_file, lock1->li_line); 1085 panic("share->excl"); 1086 } 1087 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 1088 (flags & LOP_EXCLUSIVE) != 0) { 1089 printf("exclusive lock of (%s) %s @ %s:%d\n", 1090 class->lc_name, lock->lo_name, file, line); 1091 printf("while share locked from %s:%d\n", 1092 lock1->li_file, lock1->li_line); 1093 panic("excl->share"); 1094 } 1095 return; 1096 } 1097 1098 /* 1099 * Find the previously acquired lock, but ignore interlocks. 1100 */ 1101 plock = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 1102 if (interlock != NULL && plock->li_lock == interlock) { 1103 if ((*lock_list)->ll_count == 1) { 1104 1105 /* 1106 * The interlock is the only lock we hold, so 1107 * nothing to do. 1108 */ 1109 return; 1110 } 1111 plock = &(*lock_list)->ll_children[(*lock_list)->ll_count - 2]; 1112 } 1113 1114 /* 1115 * Try to perform most checks without a lock. If this succeeds we 1116 * can skip acquiring the lock and return success. 1117 */ 1118 w1 = plock->li_lock->lo_witness; 1119 if (witness_lock_order_check(w1, w)) 1120 return; 1121 1122 /* 1123 * Check for duplicate locks of the same type. Note that we only 1124 * have to check for this on the last lock we just acquired. Any 1125 * other cases will be caught as lock order violations. 1126 */ 1127 mtx_lock_spin(&w_mtx); 1128 witness_lock_order_add(w1, w); 1129 if (w1 == w) { 1130 i = w->w_index; 1131 if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) && 1132 !(w_rmatrix[i][i] & WITNESS_REVERSAL)) { 1133 w_rmatrix[i][i] |= WITNESS_REVERSAL; 1134 w->w_reversed = 1; 1135 mtx_unlock_spin(&w_mtx); 1136 printf( 1137 "acquiring duplicate lock of same type: \"%s\"\n", 1138 w->w_name); 1139 printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name, 1140 plock->li_file, plock->li_line); 1141 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 1142 witness_debugger(1); 1143 } else 1144 mtx_unlock_spin(&w_mtx); 1145 return; 1146 } 1147 mtx_assert(&w_mtx, MA_OWNED); 1148 1149 /* 1150 * If we know that the the lock we are acquiring comes after 1151 * the lock we most recently acquired in the lock order tree, 1152 * then there is no need for any further checks. 1153 */ 1154 if (isitmychild(w1, w)) 1155 goto out; 1156 1157 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 1158 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 1159 1160 MPASS(j < WITNESS_COUNT); 1161 lock1 = &lle->ll_children[i]; 1162 1163 /* 1164 * Ignore the interlock the first time we see it. 1165 */ 1166 if (interlock != NULL && interlock == lock1->li_lock) { 1167 interlock = NULL; 1168 continue; 1169 } 1170 1171 /* 1172 * If this lock doesn't undergo witness checking, 1173 * then skip it. 1174 */ 1175 w1 = lock1->li_lock->lo_witness; 1176 if (w1 == NULL) { 1177 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 1178 ("lock missing witness structure")); 1179 continue; 1180 } 1181 1182 /* 1183 * If we are locking Giant and this is a sleepable 1184 * lock, then skip it. 1185 */ 1186 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 1187 lock == &Giant.lock_object) 1188 continue; 1189 1190 /* 1191 * If we are locking a sleepable lock and this lock 1192 * is Giant, then skip it. 1193 */ 1194 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 1195 lock1->li_lock == &Giant.lock_object) 1196 continue; 1197 1198 /* 1199 * If we are locking a sleepable lock and this lock 1200 * isn't sleepable, we want to treat it as a lock 1201 * order violation to enfore a general lock order of 1202 * sleepable locks before non-sleepable locks. 1203 */ 1204 if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1205 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1206 goto reversal; 1207 1208 /* 1209 * If we are locking Giant and this is a non-sleepable 1210 * lock, then treat it as a reversal. 1211 */ 1212 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 1213 lock == &Giant.lock_object) 1214 goto reversal; 1215 1216 /* 1217 * Check the lock order hierarchy for a reveresal. 1218 */ 1219 if (!isitmydescendant(w, w1)) 1220 continue; 1221 reversal: 1222 1223 /* 1224 * We have a lock order violation, check to see if it 1225 * is allowed or has already been yelled about. 1226 */ 1227 #ifdef BLESSING 1228 1229 /* 1230 * If the lock order is blessed, just bail. We don't 1231 * look for other lock order violations though, which 1232 * may be a bug. 1233 */ 1234 if (blessed(w, w1)) 1235 goto out; 1236 #endif 1237 1238 /* Bail if this violation is known */ 1239 if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL) 1240 goto out; 1241 1242 /* Record this as a violation */ 1243 w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL; 1244 w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL; 1245 w->w_reversed = w1->w_reversed = 1; 1246 witness_increment_graph_generation(); 1247 mtx_unlock_spin(&w_mtx); 1248 1249 /* 1250 * Ok, yell about it. 1251 */ 1252 if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1253 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1254 printf( 1255 "lock order reversal: (sleepable after non-sleepable)\n"); 1256 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1257 && lock == &Giant.lock_object) 1258 printf( 1259 "lock order reversal: (Giant after non-sleepable)\n"); 1260 else 1261 printf("lock order reversal:\n"); 1262 1263 /* 1264 * Try to locate an earlier lock with 1265 * witness w in our list. 1266 */ 1267 do { 1268 lock2 = &lle->ll_children[i]; 1269 MPASS(lock2->li_lock != NULL); 1270 if (lock2->li_lock->lo_witness == w) 1271 break; 1272 if (i == 0 && lle->ll_next != NULL) { 1273 lle = lle->ll_next; 1274 i = lle->ll_count - 1; 1275 MPASS(i >= 0 && i < LOCK_NCHILDREN); 1276 } else 1277 i--; 1278 } while (i >= 0); 1279 if (i < 0) { 1280 printf(" 1st %p %s (%s) @ %s:%d\n", 1281 lock1->li_lock, lock1->li_lock->lo_name, 1282 w1->w_name, lock1->li_file, lock1->li_line); 1283 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 1284 lock->lo_name, w->w_name, file, line); 1285 } else { 1286 printf(" 1st %p %s (%s) @ %s:%d\n", 1287 lock2->li_lock, lock2->li_lock->lo_name, 1288 lock2->li_lock->lo_witness->w_name, 1289 lock2->li_file, lock2->li_line); 1290 printf(" 2nd %p %s (%s) @ %s:%d\n", 1291 lock1->li_lock, lock1->li_lock->lo_name, 1292 w1->w_name, lock1->li_file, lock1->li_line); 1293 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 1294 lock->lo_name, w->w_name, file, line); 1295 } 1296 witness_debugger(1); 1297 return; 1298 } 1299 } 1300 1301 /* 1302 * If requested, build a new lock order. However, don't build a new 1303 * relationship between a sleepable lock and Giant if it is in the 1304 * wrong direction. The correct lock order is that sleepable locks 1305 * always come before Giant. 1306 */ 1307 if (flags & LOP_NEWORDER && 1308 !(plock->li_lock == &Giant.lock_object && 1309 (lock->lo_flags & LO_SLEEPABLE) != 0)) { 1310 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1311 w->w_name, plock->li_lock->lo_witness->w_name); 1312 itismychild(plock->li_lock->lo_witness, w); 1313 } 1314 out: 1315 mtx_unlock_spin(&w_mtx); 1316 } 1317 1318 void 1319 witness_lock(struct lock_object *lock, int flags, const char *file, int line) 1320 { 1321 struct lock_list_entry **lock_list, *lle; 1322 struct lock_instance *instance; 1323 struct witness *w; 1324 struct thread *td; 1325 1326 if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL || 1327 panicstr != NULL) 1328 return; 1329 w = lock->lo_witness; 1330 td = curthread; 1331 file = fixup_filename(file); 1332 1333 /* Determine lock list for this lock. */ 1334 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1335 lock_list = &td->td_sleeplocks; 1336 else 1337 lock_list = PCPU_PTR(spinlocks); 1338 1339 /* Check to see if we are recursing on a lock we already own. */ 1340 instance = find_instance(*lock_list, lock); 1341 if (instance != NULL) { 1342 instance->li_flags++; 1343 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1344 td->td_proc->p_pid, lock->lo_name, 1345 instance->li_flags & LI_RECURSEMASK); 1346 instance->li_file = file; 1347 instance->li_line = line; 1348 return; 1349 } 1350 1351 /* Update per-witness last file and line acquire. */ 1352 w->w_file = file; 1353 w->w_line = line; 1354 1355 /* Find the next open lock instance in the list and fill it. */ 1356 lle = *lock_list; 1357 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 1358 lle = witness_lock_list_get(); 1359 if (lle == NULL) 1360 return; 1361 lle->ll_next = *lock_list; 1362 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 1363 td->td_proc->p_pid, lle); 1364 *lock_list = lle; 1365 } 1366 instance = &lle->ll_children[lle->ll_count++]; 1367 instance->li_lock = lock; 1368 instance->li_line = line; 1369 instance->li_file = file; 1370 if ((flags & LOP_EXCLUSIVE) != 0) 1371 instance->li_flags = LI_EXCLUSIVE; 1372 else 1373 instance->li_flags = 0; 1374 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 1375 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 1376 } 1377 1378 void 1379 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 1380 { 1381 struct lock_instance *instance; 1382 struct lock_class *class; 1383 1384 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1385 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 1386 return; 1387 class = LOCK_CLASS(lock); 1388 file = fixup_filename(file); 1389 if (witness_watch) { 1390 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1391 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 1392 class->lc_name, lock->lo_name, file, line); 1393 if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1394 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 1395 class->lc_name, lock->lo_name, file, line); 1396 } 1397 instance = find_instance(curthread->td_sleeplocks, lock); 1398 if (instance == NULL) 1399 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 1400 class->lc_name, lock->lo_name, file, line); 1401 if (witness_watch) { 1402 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1403 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 1404 class->lc_name, lock->lo_name, file, line); 1405 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1406 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1407 class->lc_name, lock->lo_name, 1408 instance->li_flags & LI_RECURSEMASK, file, line); 1409 } 1410 instance->li_flags |= LI_EXCLUSIVE; 1411 } 1412 1413 void 1414 witness_downgrade(struct lock_object *lock, int flags, const char *file, 1415 int line) 1416 { 1417 struct lock_instance *instance; 1418 struct lock_class *class; 1419 1420 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1421 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 1422 return; 1423 class = LOCK_CLASS(lock); 1424 file = fixup_filename(file); 1425 if (witness_watch) { 1426 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1427 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 1428 class->lc_name, lock->lo_name, file, line); 1429 if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1430 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 1431 class->lc_name, lock->lo_name, file, line); 1432 } 1433 instance = find_instance(curthread->td_sleeplocks, lock); 1434 if (instance == NULL) 1435 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 1436 class->lc_name, lock->lo_name, file, line); 1437 if (witness_watch) { 1438 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1439 panic("downgrade of shared lock (%s) %s @ %s:%d", 1440 class->lc_name, lock->lo_name, file, line); 1441 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1442 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1443 class->lc_name, lock->lo_name, 1444 instance->li_flags & LI_RECURSEMASK, file, line); 1445 } 1446 instance->li_flags &= ~LI_EXCLUSIVE; 1447 } 1448 1449 void 1450 witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 1451 { 1452 struct lock_list_entry **lock_list, *lle; 1453 struct lock_instance *instance; 1454 struct lock_class *class; 1455 struct thread *td; 1456 register_t s; 1457 int i, j; 1458 1459 if (witness_cold || lock->lo_witness == NULL || panicstr != NULL) 1460 return; 1461 td = curthread; 1462 class = LOCK_CLASS(lock); 1463 file = fixup_filename(file); 1464 1465 /* Find lock instance associated with this lock. */ 1466 if (class->lc_flags & LC_SLEEPLOCK) 1467 lock_list = &td->td_sleeplocks; 1468 else 1469 lock_list = PCPU_PTR(spinlocks); 1470 lle = *lock_list; 1471 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 1472 for (i = 0; i < (*lock_list)->ll_count; i++) { 1473 instance = &(*lock_list)->ll_children[i]; 1474 if (instance->li_lock == lock) 1475 goto found; 1476 } 1477 1478 /* 1479 * When disabling WITNESS through witness_watch we could end up in 1480 * having registered locks in the td_sleeplocks queue. 1481 * We have to make sure we flush these queues, so just search for 1482 * eventual register locks and remove them. 1483 */ 1484 if (witness_watch > 0) 1485 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, 1486 lock->lo_name, file, line); 1487 else 1488 return; 1489 found: 1490 1491 /* First, check for shared/exclusive mismatches. */ 1492 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 && 1493 (flags & LOP_EXCLUSIVE) == 0) { 1494 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1495 lock->lo_name, file, line); 1496 printf("while exclusively locked from %s:%d\n", 1497 instance->li_file, instance->li_line); 1498 panic("excl->ushare"); 1499 } 1500 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 && 1501 (flags & LOP_EXCLUSIVE) != 0) { 1502 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1503 lock->lo_name, file, line); 1504 printf("while share locked from %s:%d\n", instance->li_file, 1505 instance->li_line); 1506 panic("share->uexcl"); 1507 } 1508 1509 /* If we are recursed, unrecurse. */ 1510 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1511 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1512 td->td_proc->p_pid, instance->li_lock->lo_name, 1513 instance->li_flags); 1514 instance->li_flags--; 1515 return; 1516 } 1517 1518 /* Otherwise, remove this item from the list. */ 1519 s = intr_disable(); 1520 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1521 td->td_proc->p_pid, instance->li_lock->lo_name, 1522 (*lock_list)->ll_count - 1); 1523 for (j = i; j < (*lock_list)->ll_count - 1; j++) 1524 (*lock_list)->ll_children[j] = 1525 (*lock_list)->ll_children[j + 1]; 1526 (*lock_list)->ll_count--; 1527 intr_restore(s); 1528 1529 /* 1530 * In order to reduce contention on w_mtx, we want to keep always an 1531 * head object into lists so that frequent allocation from the 1532 * free witness pool (and subsequent locking) is avoided. 1533 * In order to maintain the current code simple, when the head 1534 * object is totally unloaded it means also that we do not have 1535 * further objects in the list, so the list ownership needs to be 1536 * hand over to another object if the current head needs to be freed. 1537 */ 1538 if ((*lock_list)->ll_count == 0) { 1539 if (*lock_list == lle) { 1540 if (lle->ll_next == NULL) 1541 return; 1542 } else 1543 lle = *lock_list; 1544 *lock_list = lle->ll_next; 1545 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1546 td->td_proc->p_pid, lle); 1547 witness_lock_list_free(lle); 1548 } 1549 } 1550 1551 void 1552 witness_thread_exit(struct thread *td) 1553 { 1554 struct lock_list_entry *lle; 1555 int i, n; 1556 1557 lle = td->td_sleeplocks; 1558 if (lle == NULL || panicstr != NULL) 1559 return; 1560 if (lle->ll_count != 0) { 1561 for (n = 0; lle != NULL; lle = lle->ll_next) 1562 for (i = lle->ll_count - 1; i >= 0; i--) { 1563 if (n == 0) 1564 printf("Thread %p exiting with the following locks held:\n", 1565 td); 1566 n++; 1567 witness_list_lock(&lle->ll_children[i]); 1568 1569 } 1570 panic("Thread %p cannot exit while holding sleeplocks\n", td); 1571 } 1572 witness_lock_list_free(lle); 1573 } 1574 1575 /* 1576 * Warn if any locks other than 'lock' are held. Flags can be passed in to 1577 * exempt Giant and sleepable locks from the checks as well. If any 1578 * non-exempt locks are held, then a supplied message is printed to the 1579 * console along with a list of the offending locks. If indicated in the 1580 * flags then a failure results in a panic as well. 1581 */ 1582 int 1583 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 1584 { 1585 struct lock_list_entry **lock_list, *lle; 1586 struct lock_instance *lock1; 1587 struct thread *td; 1588 va_list ap; 1589 int i, n; 1590 1591 if (witness_cold || witness_watch < 1 || panicstr != NULL) 1592 return (0); 1593 n = 0; 1594 td = curthread; 1595 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 1596 for (i = lle->ll_count - 1; i >= 0; i--) { 1597 lock1 = &lle->ll_children[i]; 1598 if (lock1->li_lock == lock) 1599 continue; 1600 if (flags & WARN_GIANTOK && 1601 lock1->li_lock == &Giant.lock_object) 1602 continue; 1603 if (flags & WARN_SLEEPOK && 1604 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 1605 continue; 1606 if (n == 0) { 1607 va_start(ap, fmt); 1608 vprintf(fmt, ap); 1609 va_end(ap); 1610 printf(" with the following"); 1611 if (flags & WARN_SLEEPOK) 1612 printf(" non-sleepable"); 1613 printf(" locks held:\n"); 1614 } 1615 n++; 1616 witness_list_lock(lock1); 1617 } 1618 if (PCPU_GET(spinlocks) != NULL) { 1619 lock_list = PCPU_PTR(spinlocks); 1620 1621 /* Empty list? */ 1622 if ((*lock_list)->ll_count == 0) 1623 return (n); 1624 1625 /* 1626 * Since we already hold a spinlock preemption is 1627 * already blocked. 1628 */ 1629 if (n == 0) { 1630 va_start(ap, fmt); 1631 vprintf(fmt, ap); 1632 va_end(ap); 1633 printf(" with the following"); 1634 if (flags & WARN_SLEEPOK) 1635 printf(" non-sleepable"); 1636 printf(" locks held:\n"); 1637 } 1638 n += witness_list_locks(PCPU_PTR(spinlocks)); 1639 } 1640 if (flags & WARN_PANIC && n) 1641 panic("%s", __func__); 1642 else 1643 witness_debugger(n); 1644 return (n); 1645 } 1646 1647 const char * 1648 witness_file(struct lock_object *lock) 1649 { 1650 struct witness *w; 1651 1652 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1653 return ("?"); 1654 w = lock->lo_witness; 1655 return (w->w_file); 1656 } 1657 1658 int 1659 witness_line(struct lock_object *lock) 1660 { 1661 struct witness *w; 1662 1663 if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1664 return (0); 1665 w = lock->lo_witness; 1666 return (w->w_line); 1667 } 1668 1669 static struct witness * 1670 enroll(const char *description, struct lock_class *lock_class) 1671 { 1672 struct witness *w; 1673 struct witness_list *typelist; 1674 1675 MPASS(description != NULL); 1676 1677 if (witness_watch == -1 || panicstr != NULL) 1678 return (NULL); 1679 if ((lock_class->lc_flags & LC_SPINLOCK)) { 1680 if (witness_skipspin) 1681 return (NULL); 1682 else 1683 typelist = &w_spin; 1684 } else if ((lock_class->lc_flags & LC_SLEEPLOCK)) 1685 typelist = &w_sleep; 1686 else 1687 panic("lock class %s is not sleep or spin", 1688 lock_class->lc_name); 1689 1690 mtx_lock_spin(&w_mtx); 1691 w = witness_hash_get(description); 1692 if (w) 1693 goto found; 1694 if ((w = witness_get()) == NULL) 1695 return (NULL); 1696 MPASS(strlen(description) < MAX_W_NAME); 1697 strcpy(w->w_name, description); 1698 w->w_class = lock_class; 1699 w->w_refcount = 1; 1700 STAILQ_INSERT_HEAD(&w_all, w, w_list); 1701 if (lock_class->lc_flags & LC_SPINLOCK) { 1702 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1703 w_spin_cnt++; 1704 } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 1705 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1706 w_sleep_cnt++; 1707 } 1708 1709 /* Insert new witness into the hash */ 1710 witness_hash_put(w); 1711 witness_increment_graph_generation(); 1712 mtx_unlock_spin(&w_mtx); 1713 return (w); 1714 found: 1715 w->w_refcount++; 1716 mtx_unlock_spin(&w_mtx); 1717 if (lock_class != w->w_class) 1718 panic( 1719 "lock (%s) %s does not match earlier (%s) lock", 1720 description, lock_class->lc_name, 1721 w->w_class->lc_name); 1722 return (w); 1723 } 1724 1725 static void 1726 depart(struct witness *w) 1727 { 1728 struct witness_list *list; 1729 1730 MPASS(w->w_refcount == 0); 1731 if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1732 list = &w_sleep; 1733 w_sleep_cnt--; 1734 } else { 1735 list = &w_spin; 1736 w_spin_cnt--; 1737 } 1738 /* 1739 * Set file to NULL as it may point into a loadable module. 1740 */ 1741 w->w_file = NULL; 1742 w->w_line = 0; 1743 witness_increment_graph_generation(); 1744 } 1745 1746 1747 static void 1748 adopt(struct witness *parent, struct witness *child) 1749 { 1750 int pi, ci, i, j; 1751 1752 if (witness_cold == 0) 1753 mtx_assert(&w_mtx, MA_OWNED); 1754 1755 /* If the relationship is already known, there's no work to be done. */ 1756 if (isitmychild(parent, child)) 1757 return; 1758 1759 /* When the structure of the graph changes, bump up the generation. */ 1760 witness_increment_graph_generation(); 1761 1762 /* 1763 * The hard part ... create the direct relationship, then propagate all 1764 * indirect relationships. 1765 */ 1766 pi = parent->w_index; 1767 ci = child->w_index; 1768 WITNESS_INDEX_ASSERT(pi); 1769 WITNESS_INDEX_ASSERT(ci); 1770 MPASS(pi != ci); 1771 w_rmatrix[pi][ci] |= WITNESS_PARENT; 1772 w_rmatrix[ci][pi] |= WITNESS_CHILD; 1773 1774 /* 1775 * If parent was not already an ancestor of child, 1776 * then we increment the descendant and ancestor counters. 1777 */ 1778 if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) { 1779 parent->w_num_descendants++; 1780 child->w_num_ancestors++; 1781 } 1782 1783 /* 1784 * Find each ancestor of 'pi'. Note that 'pi' itself is counted as 1785 * an ancestor of 'pi' during this loop. 1786 */ 1787 for (i = 1; i <= w_max_used_index; i++) { 1788 if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 && 1789 (i != pi)) 1790 continue; 1791 1792 /* Find each descendant of 'i' and mark it as a descendant. */ 1793 for (j = 1; j <= w_max_used_index; j++) { 1794 1795 /* 1796 * Skip children that are already marked as 1797 * descendants of 'i'. 1798 */ 1799 if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) 1800 continue; 1801 1802 /* 1803 * We are only interested in descendants of 'ci'. Note 1804 * that 'ci' itself is counted as a descendant of 'ci'. 1805 */ 1806 if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 && 1807 (j != ci)) 1808 continue; 1809 w_rmatrix[i][j] |= WITNESS_ANCESTOR; 1810 w_rmatrix[j][i] |= WITNESS_DESCENDANT; 1811 w_data[i].w_num_descendants++; 1812 w_data[j].w_num_ancestors++; 1813 1814 /* 1815 * Make sure we aren't marking a node as both an 1816 * ancestor and descendant. We should have caught 1817 * this as a lock order reversal earlier. 1818 */ 1819 if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) && 1820 (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) { 1821 printf("witness rmatrix paradox! [%d][%d]=%d " 1822 "both ancestor and descendant\n", 1823 i, j, w_rmatrix[i][j]); 1824 kdb_backtrace(); 1825 printf("Witness disabled.\n"); 1826 witness_watch = -1; 1827 } 1828 if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) && 1829 (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) { 1830 printf("witness rmatrix paradox! [%d][%d]=%d " 1831 "both ancestor and descendant\n", 1832 j, i, w_rmatrix[j][i]); 1833 kdb_backtrace(); 1834 printf("Witness disabled.\n"); 1835 witness_watch = -1; 1836 } 1837 } 1838 } 1839 } 1840 1841 static void 1842 itismychild(struct witness *parent, struct witness *child) 1843 { 1844 1845 MPASS(child != NULL && parent != NULL); 1846 if (witness_cold == 0) 1847 mtx_assert(&w_mtx, MA_OWNED); 1848 1849 if (!witness_lock_type_equal(parent, child)) { 1850 if (witness_cold == 0) 1851 mtx_unlock_spin(&w_mtx); 1852 panic("%s: parent \"%s\" (%s) and child \"%s\" (%s) are not " 1853 "the same lock type", __func__, parent->w_name, 1854 parent->w_class->lc_name, child->w_name, 1855 child->w_class->lc_name); 1856 } 1857 adopt(parent, child); 1858 } 1859 1860 /* 1861 * Generic code for the isitmy*() functions. The rmask parameter is the 1862 * expected relationship of w1 to w2. 1863 */ 1864 static int 1865 _isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname) 1866 { 1867 unsigned char r1, r2; 1868 int i1, i2; 1869 1870 i1 = w1->w_index; 1871 i2 = w2->w_index; 1872 WITNESS_INDEX_ASSERT(i1); 1873 WITNESS_INDEX_ASSERT(i2); 1874 r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK; 1875 r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK; 1876 1877 /* The flags on one better be the inverse of the flags on the other */ 1878 if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) || 1879 (WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) { 1880 printf("%s: rmatrix mismatch between %s (index %d) and %s " 1881 "(index %d): w_rmatrix[%d][%d] == %hhx but " 1882 "w_rmatrix[%d][%d] == %hhx\n", 1883 fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1, 1884 i2, i1, r2); 1885 kdb_backtrace(); 1886 printf("Witness disabled.\n"); 1887 witness_watch = -1; 1888 } 1889 return (r1 & rmask); 1890 } 1891 1892 /* 1893 * Checks if @child is a direct child of @parent. 1894 */ 1895 static int 1896 isitmychild(struct witness *parent, struct witness *child) 1897 { 1898 1899 return (_isitmyx(parent, child, WITNESS_PARENT, __func__)); 1900 } 1901 1902 /* 1903 * Checks if @descendant is a direct or inderect descendant of @ancestor. 1904 */ 1905 static int 1906 isitmydescendant(struct witness *ancestor, struct witness *descendant) 1907 { 1908 1909 return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK, 1910 __func__)); 1911 } 1912 1913 #ifdef BLESSING 1914 static int 1915 blessed(struct witness *w1, struct witness *w2) 1916 { 1917 int i; 1918 struct witness_blessed *b; 1919 1920 for (i = 0; i < blessed_count; i++) { 1921 b = &blessed_list[i]; 1922 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1923 if (strcmp(w2->w_name, b->b_lock2) == 0) 1924 return (1); 1925 continue; 1926 } 1927 if (strcmp(w1->w_name, b->b_lock2) == 0) 1928 if (strcmp(w2->w_name, b->b_lock1) == 0) 1929 return (1); 1930 } 1931 return (0); 1932 } 1933 #endif 1934 1935 static struct witness * 1936 witness_get(void) 1937 { 1938 struct witness *w; 1939 int index; 1940 1941 if (witness_cold == 0) 1942 mtx_assert(&w_mtx, MA_OWNED); 1943 1944 if (witness_watch == -1) { 1945 mtx_unlock_spin(&w_mtx); 1946 return (NULL); 1947 } 1948 if (STAILQ_EMPTY(&w_free)) { 1949 witness_watch = -1; 1950 mtx_unlock_spin(&w_mtx); 1951 printf("WITNESS: unable to allocate a new witness object\n"); 1952 return (NULL); 1953 } 1954 w = STAILQ_FIRST(&w_free); 1955 STAILQ_REMOVE_HEAD(&w_free, w_list); 1956 w_free_cnt--; 1957 index = w->w_index; 1958 MPASS(index > 0 && index == w_max_used_index+1 && 1959 index < WITNESS_COUNT); 1960 bzero(w, sizeof(*w)); 1961 w->w_index = index; 1962 if (index > w_max_used_index) 1963 w_max_used_index = index; 1964 return (w); 1965 } 1966 1967 static void 1968 witness_free(struct witness *w) 1969 { 1970 1971 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1972 w_free_cnt++; 1973 } 1974 1975 static struct lock_list_entry * 1976 witness_lock_list_get(void) 1977 { 1978 struct lock_list_entry *lle; 1979 1980 if (witness_watch == -1) 1981 return (NULL); 1982 mtx_lock_spin(&w_mtx); 1983 lle = w_lock_list_free; 1984 if (lle == NULL) { 1985 witness_watch = -1; 1986 mtx_unlock_spin(&w_mtx); 1987 printf("%s: witness exhausted\n", __func__); 1988 return (NULL); 1989 } 1990 w_lock_list_free = lle->ll_next; 1991 mtx_unlock_spin(&w_mtx); 1992 bzero(lle, sizeof(*lle)); 1993 return (lle); 1994 } 1995 1996 static void 1997 witness_lock_list_free(struct lock_list_entry *lle) 1998 { 1999 2000 mtx_lock_spin(&w_mtx); 2001 lle->ll_next = w_lock_list_free; 2002 w_lock_list_free = lle; 2003 mtx_unlock_spin(&w_mtx); 2004 } 2005 2006 static struct lock_instance * 2007 find_instance(struct lock_list_entry *list, struct lock_object *lock) 2008 { 2009 struct lock_list_entry *lle; 2010 struct lock_instance *instance; 2011 int i; 2012 2013 for (lle = list; lle != NULL; lle = lle->ll_next) 2014 for (i = lle->ll_count - 1; i >= 0; i--) { 2015 instance = &lle->ll_children[i]; 2016 if (instance->li_lock == lock) 2017 return (instance); 2018 } 2019 return (NULL); 2020 } 2021 2022 static void 2023 witness_list_lock(struct lock_instance *instance) 2024 { 2025 struct lock_object *lock; 2026 2027 lock = instance->li_lock; 2028 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 2029 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 2030 if (lock->lo_witness->w_name != lock->lo_name) 2031 printf(" (%s)", lock->lo_witness->w_name); 2032 printf(" r = %d (%p) locked @ %s:%d\n", 2033 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 2034 instance->li_line); 2035 } 2036 2037 #ifdef DDB 2038 static int 2039 witness_thread_has_locks(struct thread *td) 2040 { 2041 2042 if (td->td_sleeplocks == NULL) 2043 return (0); 2044 return (td->td_sleeplocks->ll_count != 0); 2045 } 2046 2047 static int 2048 witness_proc_has_locks(struct proc *p) 2049 { 2050 struct thread *td; 2051 2052 FOREACH_THREAD_IN_PROC(p, td) { 2053 if (witness_thread_has_locks(td)) 2054 return (1); 2055 } 2056 return (0); 2057 } 2058 #endif 2059 2060 int 2061 witness_list_locks(struct lock_list_entry **lock_list) 2062 { 2063 struct lock_list_entry *lle; 2064 int i, nheld; 2065 2066 nheld = 0; 2067 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 2068 for (i = lle->ll_count - 1; i >= 0; i--) { 2069 witness_list_lock(&lle->ll_children[i]); 2070 nheld++; 2071 } 2072 return (nheld); 2073 } 2074 2075 /* 2076 * This is a bit risky at best. We call this function when we have timed 2077 * out acquiring a spin lock, and we assume that the other CPU is stuck 2078 * with this lock held. So, we go groveling around in the other CPU's 2079 * per-cpu data to try to find the lock instance for this spin lock to 2080 * see when it was last acquired. 2081 */ 2082 void 2083 witness_display_spinlock(struct lock_object *lock, struct thread *owner) 2084 { 2085 struct lock_instance *instance; 2086 struct pcpu *pc; 2087 2088 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 2089 return; 2090 pc = pcpu_find(owner->td_oncpu); 2091 instance = find_instance(pc->pc_spinlocks, lock); 2092 if (instance != NULL) 2093 witness_list_lock(instance); 2094 } 2095 2096 void 2097 witness_save(struct lock_object *lock, const char **filep, int *linep) 2098 { 2099 struct lock_list_entry *lock_list; 2100 struct lock_instance *instance; 2101 struct lock_class *class; 2102 2103 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2104 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 2105 return; 2106 class = LOCK_CLASS(lock); 2107 if (class->lc_flags & LC_SLEEPLOCK) 2108 lock_list = curthread->td_sleeplocks; 2109 else { 2110 if (witness_skipspin) 2111 return; 2112 lock_list = PCPU_GET(spinlocks); 2113 } 2114 instance = find_instance(lock_list, lock); 2115 if (instance == NULL) 2116 panic("%s: lock (%s) %s not locked", __func__, 2117 class->lc_name, lock->lo_name); 2118 *filep = instance->li_file; 2119 *linep = instance->li_line; 2120 } 2121 2122 void 2123 witness_restore(struct lock_object *lock, const char *file, int line) 2124 { 2125 struct lock_list_entry *lock_list; 2126 struct lock_instance *instance; 2127 struct lock_class *class; 2128 2129 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2130 if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 2131 return; 2132 class = LOCK_CLASS(lock); 2133 if (class->lc_flags & LC_SLEEPLOCK) 2134 lock_list = curthread->td_sleeplocks; 2135 else { 2136 if (witness_skipspin) 2137 return; 2138 lock_list = PCPU_GET(spinlocks); 2139 } 2140 instance = find_instance(lock_list, lock); 2141 if (instance == NULL) 2142 panic("%s: lock (%s) %s not locked", __func__, 2143 class->lc_name, lock->lo_name); 2144 lock->lo_witness->w_file = file; 2145 lock->lo_witness->w_line = line; 2146 instance->li_file = file; 2147 instance->li_line = line; 2148 } 2149 2150 void 2151 witness_assert(struct lock_object *lock, int flags, const char *file, int line) 2152 { 2153 #ifdef INVARIANT_SUPPORT 2154 struct lock_instance *instance; 2155 struct lock_class *class; 2156 2157 if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL) 2158 return; 2159 class = LOCK_CLASS(lock); 2160 if ((class->lc_flags & LC_SLEEPLOCK) != 0) 2161 instance = find_instance(curthread->td_sleeplocks, lock); 2162 else if ((class->lc_flags & LC_SPINLOCK) != 0) 2163 instance = find_instance(PCPU_GET(spinlocks), lock); 2164 else { 2165 panic("Lock (%s) %s is not sleep or spin!", 2166 class->lc_name, lock->lo_name); 2167 } 2168 file = fixup_filename(file); 2169 switch (flags) { 2170 case LA_UNLOCKED: 2171 if (instance != NULL) 2172 panic("Lock (%s) %s locked @ %s:%d.", 2173 class->lc_name, lock->lo_name, file, line); 2174 break; 2175 case LA_LOCKED: 2176 case LA_LOCKED | LA_RECURSED: 2177 case LA_LOCKED | LA_NOTRECURSED: 2178 case LA_SLOCKED: 2179 case LA_SLOCKED | LA_RECURSED: 2180 case LA_SLOCKED | LA_NOTRECURSED: 2181 case LA_XLOCKED: 2182 case LA_XLOCKED | LA_RECURSED: 2183 case LA_XLOCKED | LA_NOTRECURSED: 2184 if (instance == NULL) { 2185 panic("Lock (%s) %s not locked @ %s:%d.", 2186 class->lc_name, lock->lo_name, file, line); 2187 break; 2188 } 2189 if ((flags & LA_XLOCKED) != 0 && 2190 (instance->li_flags & LI_EXCLUSIVE) == 0) 2191 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 2192 class->lc_name, lock->lo_name, file, line); 2193 if ((flags & LA_SLOCKED) != 0 && 2194 (instance->li_flags & LI_EXCLUSIVE) != 0) 2195 panic("Lock (%s) %s exclusively locked @ %s:%d.", 2196 class->lc_name, lock->lo_name, file, line); 2197 if ((flags & LA_RECURSED) != 0 && 2198 (instance->li_flags & LI_RECURSEMASK) == 0) 2199 panic("Lock (%s) %s not recursed @ %s:%d.", 2200 class->lc_name, lock->lo_name, file, line); 2201 if ((flags & LA_NOTRECURSED) != 0 && 2202 (instance->li_flags & LI_RECURSEMASK) != 0) 2203 panic("Lock (%s) %s recursed @ %s:%d.", 2204 class->lc_name, lock->lo_name, file, line); 2205 break; 2206 default: 2207 panic("Invalid lock assertion at %s:%d.", file, line); 2208 2209 } 2210 #endif /* INVARIANT_SUPPORT */ 2211 } 2212 2213 #ifdef DDB 2214 static void 2215 witness_ddb_list(struct thread *td) 2216 { 2217 2218 KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2219 KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 2220 2221 if (witness_watch < 1) 2222 return; 2223 2224 witness_list_locks(&td->td_sleeplocks); 2225 2226 /* 2227 * We only handle spinlocks if td == curthread. This is somewhat broken 2228 * if td is currently executing on some other CPU and holds spin locks 2229 * as we won't display those locks. If we had a MI way of getting 2230 * the per-cpu data for a given cpu then we could use 2231 * td->td_oncpu to get the list of spinlocks for this thread 2232 * and "fix" this. 2233 * 2234 * That still wouldn't really fix this unless we locked the scheduler 2235 * lock or stopped the other CPU to make sure it wasn't changing the 2236 * list out from under us. It is probably best to just not try to 2237 * handle threads on other CPU's for now. 2238 */ 2239 if (td == curthread && PCPU_GET(spinlocks) != NULL) 2240 witness_list_locks(PCPU_PTR(spinlocks)); 2241 } 2242 2243 DB_SHOW_COMMAND(locks, db_witness_list) 2244 { 2245 struct thread *td; 2246 2247 if (have_addr) 2248 td = db_lookup_thread(addr, TRUE); 2249 else 2250 td = kdb_thread; 2251 witness_ddb_list(td); 2252 } 2253 2254 DB_SHOW_ALL_COMMAND(locks, db_witness_list_all) 2255 { 2256 struct thread *td; 2257 struct proc *p; 2258 2259 /* 2260 * It would be nice to list only threads and processes that actually 2261 * held sleep locks, but that information is currently not exported 2262 * by WITNESS. 2263 */ 2264 FOREACH_PROC_IN_SYSTEM(p) { 2265 if (!witness_proc_has_locks(p)) 2266 continue; 2267 FOREACH_THREAD_IN_PROC(p, td) { 2268 if (!witness_thread_has_locks(td)) 2269 continue; 2270 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2271 p->p_comm, td, td->td_tid); 2272 witness_ddb_list(td); 2273 } 2274 } 2275 } 2276 DB_SHOW_ALIAS(alllocks, db_witness_list_all) 2277 2278 DB_SHOW_COMMAND(witness, db_witness_display) 2279 { 2280 2281 witness_ddb_display(db_printf); 2282 } 2283 #endif 2284 2285 static int 2286 sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS) 2287 { 2288 struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2; 2289 struct witness *tmp_w1, *tmp_w2, *w1, *w2; 2290 struct sbuf *sb; 2291 u_int w_rmatrix1, w_rmatrix2; 2292 int error, generation, i, j; 2293 2294 tmp_data1 = NULL; 2295 tmp_data2 = NULL; 2296 tmp_w1 = NULL; 2297 tmp_w2 = NULL; 2298 if (witness_watch < 1) { 2299 error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2300 return (error); 2301 } 2302 if (witness_cold) { 2303 error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2304 return (error); 2305 } 2306 error = 0; 2307 sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND); 2308 if (sb == NULL) 2309 return (ENOMEM); 2310 2311 /* Allocate and init temporary storage space. */ 2312 tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2313 tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2314 tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2315 M_WAITOK | M_ZERO); 2316 tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2317 M_WAITOK | M_ZERO); 2318 stack_zero(&tmp_data1->wlod_stack); 2319 stack_zero(&tmp_data2->wlod_stack); 2320 2321 restart: 2322 mtx_lock_spin(&w_mtx); 2323 generation = w_generation; 2324 mtx_unlock_spin(&w_mtx); 2325 sbuf_printf(sb, "Number of known direct relationships is %d\n", 2326 w_lohash.wloh_count); 2327 for (i = 1; i < w_max_used_index; i++) { 2328 mtx_lock_spin(&w_mtx); 2329 if (generation != w_generation) { 2330 mtx_unlock_spin(&w_mtx); 2331 2332 /* The graph has changed, try again. */ 2333 req->oldidx = 0; 2334 sbuf_clear(sb); 2335 goto restart; 2336 } 2337 2338 w1 = &w_data[i]; 2339 if (w1->w_reversed == 0) { 2340 mtx_unlock_spin(&w_mtx); 2341 continue; 2342 } 2343 2344 /* Copy w1 locally so we can release the spin lock. */ 2345 *tmp_w1 = *w1; 2346 mtx_unlock_spin(&w_mtx); 2347 2348 if (tmp_w1->w_reversed == 0) 2349 continue; 2350 for (j = 1; j < w_max_used_index; j++) { 2351 if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j) 2352 continue; 2353 2354 mtx_lock_spin(&w_mtx); 2355 if (generation != w_generation) { 2356 mtx_unlock_spin(&w_mtx); 2357 2358 /* The graph has changed, try again. */ 2359 req->oldidx = 0; 2360 sbuf_clear(sb); 2361 goto restart; 2362 } 2363 2364 w2 = &w_data[j]; 2365 data1 = witness_lock_order_get(w1, w2); 2366 data2 = witness_lock_order_get(w2, w1); 2367 2368 /* 2369 * Copy information locally so we can release the 2370 * spin lock. 2371 */ 2372 *tmp_w2 = *w2; 2373 w_rmatrix1 = (unsigned int)w_rmatrix[i][j]; 2374 w_rmatrix2 = (unsigned int)w_rmatrix[j][i]; 2375 2376 if (data1) { 2377 stack_zero(&tmp_data1->wlod_stack); 2378 stack_copy(&data1->wlod_stack, 2379 &tmp_data1->wlod_stack); 2380 } 2381 if (data2 && data2 != data1) { 2382 stack_zero(&tmp_data2->wlod_stack); 2383 stack_copy(&data2->wlod_stack, 2384 &tmp_data2->wlod_stack); 2385 } 2386 mtx_unlock_spin(&w_mtx); 2387 2388 sbuf_printf(sb, 2389 "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n", 2390 tmp_w1->w_name, tmp_w1->w_class->lc_name, 2391 tmp_w2->w_name, tmp_w2->w_class->lc_name); 2392 #if 0 2393 sbuf_printf(sb, 2394 "w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n", 2395 tmp_w1->name, tmp_w2->w_name, w_rmatrix1, 2396 tmp_w2->name, tmp_w1->w_name, w_rmatrix2); 2397 #endif 2398 if (data1) { 2399 sbuf_printf(sb, 2400 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2401 tmp_w1->w_name, tmp_w1->w_class->lc_name, 2402 tmp_w2->w_name, tmp_w2->w_class->lc_name); 2403 stack_sbuf_print(sb, &tmp_data1->wlod_stack); 2404 sbuf_printf(sb, "\n"); 2405 } 2406 if (data2 && data2 != data1) { 2407 sbuf_printf(sb, 2408 "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2409 tmp_w2->w_name, tmp_w2->w_class->lc_name, 2410 tmp_w1->w_name, tmp_w1->w_class->lc_name); 2411 stack_sbuf_print(sb, &tmp_data2->wlod_stack); 2412 sbuf_printf(sb, "\n"); 2413 } 2414 } 2415 } 2416 mtx_lock_spin(&w_mtx); 2417 if (generation != w_generation) { 2418 mtx_unlock_spin(&w_mtx); 2419 2420 /* 2421 * The graph changed while we were printing stack data, 2422 * try again. 2423 */ 2424 req->oldidx = 0; 2425 sbuf_clear(sb); 2426 goto restart; 2427 } 2428 mtx_unlock_spin(&w_mtx); 2429 2430 /* Free temporary storage space. */ 2431 free(tmp_data1, M_TEMP); 2432 free(tmp_data2, M_TEMP); 2433 free(tmp_w1, M_TEMP); 2434 free(tmp_w2, M_TEMP); 2435 2436 sbuf_finish(sb); 2437 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2438 sbuf_delete(sb); 2439 2440 return (error); 2441 } 2442 2443 static int 2444 sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS) 2445 { 2446 struct witness *w; 2447 struct sbuf *sb; 2448 int error; 2449 2450 if (witness_watch < 1) { 2451 error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2452 return (error); 2453 } 2454 if (witness_cold) { 2455 error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2456 return (error); 2457 } 2458 error = 0; 2459 sb = sbuf_new(NULL, NULL, FULLGRAPH_SBUF_SIZE, SBUF_FIXEDLEN); 2460 if (sb == NULL) 2461 return (ENOMEM); 2462 sbuf_printf(sb, "\n"); 2463 2464 mtx_lock_spin(&w_mtx); 2465 STAILQ_FOREACH(w, &w_all, w_list) 2466 w->w_displayed = 0; 2467 STAILQ_FOREACH(w, &w_all, w_list) 2468 witness_add_fullgraph(sb, w); 2469 mtx_unlock_spin(&w_mtx); 2470 2471 /* 2472 * While using SBUF_FIXEDLEN, check if the sbuf overflowed. 2473 */ 2474 if (sbuf_overflowed(sb)) { 2475 sbuf_delete(sb); 2476 panic("%s: sbuf overflowed, bump FULLGRAPH_SBUF_SIZE value\n", 2477 __func__); 2478 } 2479 2480 /* 2481 * Close the sbuf and return to userland. 2482 */ 2483 sbuf_finish(sb); 2484 error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2485 sbuf_delete(sb); 2486 2487 return (error); 2488 } 2489 2490 static int 2491 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 2492 { 2493 int error, value; 2494 2495 value = witness_watch; 2496 error = sysctl_handle_int(oidp, &value, 0, req); 2497 if (error != 0 || req->newptr == NULL) 2498 return (error); 2499 if (value > 1 || value < -1 || 2500 (witness_watch == -1 && value != witness_watch)) 2501 return (EINVAL); 2502 witness_watch = value; 2503 return (0); 2504 } 2505 2506 static void 2507 witness_add_fullgraph(struct sbuf *sb, struct witness *w) 2508 { 2509 int i; 2510 2511 if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0)) 2512 return; 2513 w->w_displayed = 1; 2514 2515 WITNESS_INDEX_ASSERT(w->w_index); 2516 for (i = 1; i <= w_max_used_index; i++) { 2517 if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) { 2518 sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name, 2519 w_data[i].w_name); 2520 witness_add_fullgraph(sb, &w_data[i]); 2521 } 2522 } 2523 } 2524 2525 /* 2526 * A simple hash function. Takes a key pointer and a key size. If size == 0, 2527 * interprets the key as a string and reads until the null 2528 * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit 2529 * hash value computed from the key. 2530 */ 2531 static uint32_t 2532 witness_hash_djb2(const uint8_t *key, uint32_t size) 2533 { 2534 unsigned int hash = 5381; 2535 int i; 2536 2537 /* hash = hash * 33 + key[i] */ 2538 if (size) 2539 for (i = 0; i < size; i++) 2540 hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2541 else 2542 for (i = 0; key[i] != 0; i++) 2543 hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2544 2545 return (hash); 2546 } 2547 2548 2549 /* 2550 * Initializes the two witness hash tables. Called exactly once from 2551 * witness_initialize(). 2552 */ 2553 static void 2554 witness_init_hash_tables(void) 2555 { 2556 int i; 2557 2558 MPASS(witness_cold); 2559 2560 /* Initialize the hash tables. */ 2561 for (i = 0; i < WITNESS_HASH_SIZE; i++) 2562 w_hash.wh_array[i] = NULL; 2563 2564 w_hash.wh_size = WITNESS_HASH_SIZE; 2565 w_hash.wh_count = 0; 2566 2567 /* Initialize the lock order data hash. */ 2568 w_lofree = NULL; 2569 for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) { 2570 memset(&w_lodata[i], 0, sizeof(w_lodata[i])); 2571 w_lodata[i].wlod_next = w_lofree; 2572 w_lofree = &w_lodata[i]; 2573 } 2574 w_lohash.wloh_size = WITNESS_LO_HASH_SIZE; 2575 w_lohash.wloh_count = 0; 2576 for (i = 0; i < WITNESS_LO_HASH_SIZE; i++) 2577 w_lohash.wloh_array[i] = NULL; 2578 } 2579 2580 static struct witness * 2581 witness_hash_get(const char *key) 2582 { 2583 struct witness *w; 2584 uint32_t hash; 2585 2586 MPASS(key != NULL); 2587 if (witness_cold == 0) 2588 mtx_assert(&w_mtx, MA_OWNED); 2589 hash = witness_hash_djb2(key, 0) % w_hash.wh_size; 2590 w = w_hash.wh_array[hash]; 2591 while (w != NULL) { 2592 if (strcmp(w->w_name, key) == 0) 2593 goto out; 2594 w = w->w_hash_next; 2595 } 2596 2597 out: 2598 return (w); 2599 } 2600 2601 static void 2602 witness_hash_put(struct witness *w) 2603 { 2604 uint32_t hash; 2605 2606 MPASS(w != NULL); 2607 MPASS(w->w_name != NULL); 2608 if (witness_cold == 0) 2609 mtx_assert(&w_mtx, MA_OWNED); 2610 KASSERT(witness_hash_get(w->w_name) == NULL, 2611 ("%s: trying to add a hash entry that already exists!", __func__)); 2612 KASSERT(w->w_hash_next == NULL, 2613 ("%s: w->w_hash_next != NULL", __func__)); 2614 2615 hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size; 2616 w->w_hash_next = w_hash.wh_array[hash]; 2617 w_hash.wh_array[hash] = w; 2618 w_hash.wh_count++; 2619 } 2620 2621 2622 static struct witness_lock_order_data * 2623 witness_lock_order_get(struct witness *parent, struct witness *child) 2624 { 2625 struct witness_lock_order_data *data = NULL; 2626 struct witness_lock_order_key key; 2627 unsigned int hash; 2628 2629 MPASS(parent != NULL && child != NULL); 2630 key.from = parent->w_index; 2631 key.to = child->w_index; 2632 WITNESS_INDEX_ASSERT(key.from); 2633 WITNESS_INDEX_ASSERT(key.to); 2634 if ((w_rmatrix[parent->w_index][child->w_index] 2635 & WITNESS_LOCK_ORDER_KNOWN) == 0) 2636 goto out; 2637 2638 hash = witness_hash_djb2((const char*)&key, 2639 sizeof(key)) % w_lohash.wloh_size; 2640 data = w_lohash.wloh_array[hash]; 2641 while (data != NULL) { 2642 if (witness_lock_order_key_equal(&data->wlod_key, &key)) 2643 break; 2644 data = data->wlod_next; 2645 } 2646 2647 out: 2648 return (data); 2649 } 2650 2651 /* 2652 * Verify that parent and child have a known relationship, are not the same, 2653 * and child is actually a child of parent. This is done without w_mtx 2654 * to avoid contention in the common case. 2655 */ 2656 static int 2657 witness_lock_order_check(struct witness *parent, struct witness *child) 2658 { 2659 2660 if (parent != child && 2661 w_rmatrix[parent->w_index][child->w_index] 2662 & WITNESS_LOCK_ORDER_KNOWN && 2663 isitmychild(parent, child)) 2664 return (1); 2665 2666 return (0); 2667 } 2668 2669 static int 2670 witness_lock_order_add(struct witness *parent, struct witness *child) 2671 { 2672 struct witness_lock_order_data *data = NULL; 2673 struct witness_lock_order_key key; 2674 unsigned int hash; 2675 2676 MPASS(parent != NULL && child != NULL); 2677 key.from = parent->w_index; 2678 key.to = child->w_index; 2679 WITNESS_INDEX_ASSERT(key.from); 2680 WITNESS_INDEX_ASSERT(key.to); 2681 if (w_rmatrix[parent->w_index][child->w_index] 2682 & WITNESS_LOCK_ORDER_KNOWN) 2683 return (1); 2684 2685 hash = witness_hash_djb2((const char*)&key, 2686 sizeof(key)) % w_lohash.wloh_size; 2687 w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN; 2688 data = w_lofree; 2689 if (data == NULL) 2690 return (0); 2691 w_lofree = data->wlod_next; 2692 data->wlod_next = w_lohash.wloh_array[hash]; 2693 data->wlod_key = key; 2694 w_lohash.wloh_array[hash] = data; 2695 w_lohash.wloh_count++; 2696 stack_zero(&data->wlod_stack); 2697 stack_save(&data->wlod_stack); 2698 return (1); 2699 } 2700 2701 /* Call this whenver the structure of the witness graph changes. */ 2702 static void 2703 witness_increment_graph_generation(void) 2704 { 2705 2706 if (witness_cold == 0) 2707 mtx_assert(&w_mtx, MA_OWNED); 2708 w_generation++; 2709 } 2710 2711 #ifdef KDB 2712 static void 2713 _witness_debugger(int cond, const char *msg) 2714 { 2715 2716 if (witness_trace && cond) 2717 kdb_backtrace(); 2718 if (witness_kdb && cond) 2719 kdb_enter(KDB_WHY_WITNESS, msg); 2720 } 2721 #endif 2722