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