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