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