1 /*- 2 * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Berkeley Software Design Inc's name may not be used to endorse or 13 * promote products derived from this software without specific prior 14 * written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 29 * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 30 */ 31 32 /* 33 * Implementation of the `witness' lock verifier. Originally implemented for 34 * mutexes in BSD/OS. Extended to handle generic lock objects and lock 35 * classes in FreeBSD. 36 */ 37 38 /* 39 * Main Entry: witness 40 * Pronunciation: 'wit-n&s 41 * Function: noun 42 * Etymology: Middle English witnesse, from Old English witnes knowledge, 43 * testimony, witness, from 2wit 44 * Date: before 12th century 45 * 1 : attestation of a fact or event : TESTIMONY 46 * 2 : one that gives evidence; specifically : one who testifies in 47 * a cause or before a judicial tribunal 48 * 3 : one asked to be present at a transaction so as to be able to 49 * testify to its having taken place 50 * 4 : one who has personal knowledge of something 51 * 5 a : something serving as evidence or proof : SIGN 52 * b : public affirmation by word or example of usually 53 * religious faith or conviction <the heroic witness to divine 54 * life -- Pilot> 55 * 6 capitalized : a member of the Jehovah's Witnesses 56 */ 57 58 /* 59 * Special rules concerning Giant and lock orders: 60 * 61 * 1) Giant must be acquired before any other mutexes. Stated another way, 62 * no other mutex may be held when Giant is acquired. 63 * 64 * 2) Giant must be released when blocking on a sleepable lock. 65 * 66 * This rule is less obvious, but is a result of Giant providing the same 67 * semantics as spl(). Basically, when a thread sleeps, it must release 68 * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule 69 * 2). 70 * 71 * 3) Giant may be acquired before or after sleepable locks. 72 * 73 * This rule is also not quite as obvious. Giant may be acquired after 74 * a sleepable lock because it is a non-sleepable lock and non-sleepable 75 * locks may always be acquired while holding a sleepable lock. The second 76 * case, Giant before a sleepable lock, follows from rule 2) above. Suppose 77 * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1 78 * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and 79 * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to 80 * execute. Thus, acquiring Giant both before and after a sleepable lock 81 * will not result in a lock order reversal. 82 */ 83 84 #include <sys/cdefs.h> 85 __FBSDID("$FreeBSD$"); 86 87 #include "opt_ddb.h" 88 #include "opt_hwpmc_hooks.h" 89 #include "opt_witness.h" 90 91 #include <sys/param.h> 92 #include <sys/bus.h> 93 #include <sys/kdb.h> 94 #include <sys/kernel.h> 95 #include <sys/ktr.h> 96 #include <sys/lock.h> 97 #include <sys/malloc.h> 98 #include <sys/mutex.h> 99 #include <sys/priv.h> 100 #include <sys/proc.h> 101 #include <sys/sysctl.h> 102 #include <sys/systm.h> 103 104 #include <ddb/ddb.h> 105 106 #include <machine/stdarg.h> 107 108 /* Note that these traces do not work with KTR_ALQ. */ 109 #if 0 110 #define KTR_WITNESS KTR_SUBSYS 111 #else 112 #define KTR_WITNESS 0 113 #endif 114 115 /* Easier to stay with the old names. */ 116 #define lo_list lo_witness_data.lod_list 117 #define lo_witness lo_witness_data.lod_witness 118 119 /* Define this to check for blessed mutexes */ 120 #undef BLESSING 121 122 #define WITNESS_COUNT 1024 123 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 124 /* 125 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 126 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 127 * probably be safe for the most part, but it's still a SWAG. 128 */ 129 #define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 130 131 #define WITNESS_NCHILDREN 6 132 133 struct witness_child_list_entry; 134 135 struct witness { 136 const char *w_name; 137 struct lock_class *w_class; 138 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 139 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 140 struct witness_child_list_entry *w_children; /* Great evilness... */ 141 const char *w_file; 142 int w_line; 143 u_int w_level; 144 u_int w_refcount; 145 u_char w_Giant_squawked:1; 146 u_char w_other_squawked:1; 147 u_char w_same_squawked:1; 148 u_char w_displayed:1; 149 }; 150 151 struct witness_child_list_entry { 152 struct witness_child_list_entry *wcl_next; 153 struct witness *wcl_children[WITNESS_NCHILDREN]; 154 u_int wcl_count; 155 }; 156 157 STAILQ_HEAD(witness_list, witness); 158 159 #ifdef BLESSING 160 struct witness_blessed { 161 const char *b_lock1; 162 const char *b_lock2; 163 }; 164 #endif 165 166 struct witness_order_list_entry { 167 const char *w_name; 168 struct lock_class *w_class; 169 }; 170 171 #ifdef BLESSING 172 static int blessed(struct witness *, struct witness *); 173 #endif 174 static int depart(struct witness *w); 175 static struct witness *enroll(const char *description, 176 struct lock_class *lock_class); 177 static int insertchild(struct witness *parent, struct witness *child); 178 static int isitmychild(struct witness *parent, struct witness *child); 179 static int isitmydescendant(struct witness *parent, struct witness *child); 180 static int itismychild(struct witness *parent, struct witness *child); 181 static void removechild(struct witness *parent, struct witness *child); 182 static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 183 static const char *fixup_filename(const char *file); 184 static struct witness *witness_get(void); 185 static void witness_free(struct witness *m); 186 static struct witness_child_list_entry *witness_child_get(void); 187 static void witness_child_free(struct witness_child_list_entry *wcl); 188 static struct lock_list_entry *witness_lock_list_get(void); 189 static void witness_lock_list_free(struct lock_list_entry *lle); 190 static struct lock_instance *find_instance(struct lock_list_entry *lock_list, 191 struct lock_object *lock); 192 static void witness_list_lock(struct lock_instance *instance); 193 #ifdef DDB 194 static void witness_leveldescendents(struct witness *parent, int level); 195 static void witness_levelall(void); 196 static void witness_displaydescendants(void(*)(const char *fmt, ...), 197 struct witness *, int indent); 198 static void witness_display_list(void(*prnt)(const char *fmt, ...), 199 struct witness_list *list); 200 static void witness_display(void(*)(const char *fmt, ...)); 201 static void witness_list(struct thread *td); 202 #endif 203 204 SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking"); 205 206 /* 207 * If set to 0, witness is disabled. If set to a non-zero value, witness 208 * performs full lock order checking for all locks. At runtime, this 209 * value may be set to 0 to turn off witness. witness is not allowed be 210 * turned on once it is turned off, however. 211 */ 212 static int witness_watch = 1; 213 TUNABLE_INT("debug.witness.watch", &witness_watch); 214 SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 215 sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 216 217 #ifdef KDB 218 /* 219 * When KDB is enabled and witness_kdb is set to 1, it will cause the system 220 * to drop into kdebug() when: 221 * - a lock hierarchy violation occurs 222 * - locks are held when going to sleep. 223 */ 224 #ifdef WITNESS_KDB 225 int witness_kdb = 1; 226 #else 227 int witness_kdb = 0; 228 #endif 229 TUNABLE_INT("debug.witness.kdb", &witness_kdb); 230 SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 231 232 /* 233 * When KDB is enabled and witness_trace is set to 1, it will cause the system 234 * to print a stack trace: 235 * - a lock hierarchy violation occurs 236 * - locks are held when going to sleep. 237 */ 238 int witness_trace = 1; 239 TUNABLE_INT("debug.witness.trace", &witness_trace); 240 SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 241 #endif /* KDB */ 242 243 #ifdef WITNESS_SKIPSPIN 244 int witness_skipspin = 1; 245 #else 246 int witness_skipspin = 0; 247 #endif 248 TUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 249 SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, 250 &witness_skipspin, 0, ""); 251 252 static struct mtx w_mtx; 253 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 254 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 255 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 256 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 257 static struct witness_child_list_entry *w_child_free = NULL; 258 static struct lock_list_entry *w_lock_list_free = NULL; 259 260 static int w_free_cnt, w_spin_cnt, w_sleep_cnt, w_child_free_cnt, w_child_cnt; 261 SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, ""); 262 SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, ""); 263 SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0, 264 ""); 265 SYSCTL_INT(_debug_witness, OID_AUTO, child_free_cnt, CTLFLAG_RD, 266 &w_child_free_cnt, 0, ""); 267 SYSCTL_INT(_debug_witness, OID_AUTO, child_cnt, CTLFLAG_RD, &w_child_cnt, 0, 268 ""); 269 270 static struct witness w_data[WITNESS_COUNT]; 271 static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 272 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 273 274 static struct witness_order_list_entry order_lists[] = { 275 /* 276 * sx locks 277 */ 278 { "proctree", &lock_class_sx }, 279 { "allproc", &lock_class_sx }, 280 { "allprison", &lock_class_sx }, 281 { NULL, NULL }, 282 /* 283 * Various mutexes 284 */ 285 { "Giant", &lock_class_mtx_sleep }, 286 { "pipe mutex", &lock_class_mtx_sleep }, 287 { "sigio lock", &lock_class_mtx_sleep }, 288 { "process group", &lock_class_mtx_sleep }, 289 { "process lock", &lock_class_mtx_sleep }, 290 { "session", &lock_class_mtx_sleep }, 291 { "uidinfo hash", &lock_class_mtx_sleep }, 292 { "uidinfo struct", &lock_class_mtx_sleep }, 293 #ifdef HWPMC_HOOKS 294 { "pmc-sleep", &lock_class_mtx_sleep }, 295 #endif 296 { NULL, NULL }, 297 /* 298 * Sockets 299 */ 300 { "accept", &lock_class_mtx_sleep }, 301 { "so_snd", &lock_class_mtx_sleep }, 302 { "so_rcv", &lock_class_mtx_sleep }, 303 { "sellck", &lock_class_mtx_sleep }, 304 { NULL, NULL }, 305 /* 306 * Routing 307 */ 308 { "so_rcv", &lock_class_mtx_sleep }, 309 { "radix node head", &lock_class_mtx_sleep }, 310 { "rtentry", &lock_class_mtx_sleep }, 311 { "ifaddr", &lock_class_mtx_sleep }, 312 { NULL, NULL }, 313 /* 314 * Multicast - protocol locks before interface locks, after UDP locks. 315 */ 316 { "udpinp", &lock_class_mtx_sleep }, 317 { "in_multi_mtx", &lock_class_mtx_sleep }, 318 { "igmp_mtx", &lock_class_mtx_sleep }, 319 { "if_addr_mtx", &lock_class_mtx_sleep }, 320 { NULL, NULL }, 321 /* 322 * UNIX Domain Sockets 323 */ 324 { "unp", &lock_class_mtx_sleep }, 325 { "so_snd", &lock_class_mtx_sleep }, 326 { NULL, NULL }, 327 /* 328 * UDP/IP 329 */ 330 { "udp", &lock_class_mtx_sleep }, 331 { "udpinp", &lock_class_mtx_sleep }, 332 { "so_snd", &lock_class_mtx_sleep }, 333 { NULL, NULL }, 334 /* 335 * TCP/IP 336 */ 337 { "tcp", &lock_class_mtx_sleep }, 338 { "tcpinp", &lock_class_mtx_sleep }, 339 { "so_snd", &lock_class_mtx_sleep }, 340 { NULL, NULL }, 341 /* 342 * SLIP 343 */ 344 { "slip_mtx", &lock_class_mtx_sleep }, 345 { "slip sc_mtx", &lock_class_mtx_sleep }, 346 { NULL, NULL }, 347 /* 348 * netatalk 349 */ 350 { "ddp_list_mtx", &lock_class_mtx_sleep }, 351 { "ddp_mtx", &lock_class_mtx_sleep }, 352 { NULL, NULL }, 353 /* 354 * BPF 355 */ 356 { "bpf global lock", &lock_class_mtx_sleep }, 357 { "bpf interface lock", &lock_class_mtx_sleep }, 358 { "bpf cdev lock", &lock_class_mtx_sleep }, 359 { NULL, NULL }, 360 /* 361 * NFS server 362 */ 363 { "nfsd_mtx", &lock_class_mtx_sleep }, 364 { "so_snd", &lock_class_mtx_sleep }, 365 { NULL, NULL }, 366 367 /* 368 * IEEE 802.11 369 */ 370 { "802.11 com lock", &lock_class_mtx_sleep}, 371 { NULL, NULL }, 372 /* 373 * Network drivers 374 */ 375 { "network driver", &lock_class_mtx_sleep}, 376 { NULL, NULL }, 377 378 /* 379 * Netgraph 380 */ 381 { "ng_node", &lock_class_mtx_sleep }, 382 { "ng_worklist", &lock_class_mtx_sleep }, 383 { NULL, NULL }, 384 /* 385 * CDEV 386 */ 387 { "system map", &lock_class_mtx_sleep }, 388 { "vm page queue mutex", &lock_class_mtx_sleep }, 389 { "vnode interlock", &lock_class_mtx_sleep }, 390 { "cdev", &lock_class_mtx_sleep }, 391 { NULL, NULL }, 392 /* 393 * kqueue/VFS interaction 394 */ 395 { "kqueue", &lock_class_mtx_sleep }, 396 { "struct mount mtx", &lock_class_mtx_sleep }, 397 { "vnode interlock", &lock_class_mtx_sleep }, 398 { NULL, NULL }, 399 /* 400 * spin locks 401 */ 402 #ifdef SMP 403 { "ap boot", &lock_class_mtx_spin }, 404 #endif 405 { "rm.mutex_mtx", &lock_class_mtx_spin }, 406 { "sio", &lock_class_mtx_spin }, 407 #ifdef __i386__ 408 { "cy", &lock_class_mtx_spin }, 409 { "descriptor tables", &lock_class_mtx_spin }, 410 #endif 411 #ifdef __sparc64__ 412 { "pcib_mtx", &lock_class_mtx_spin }, 413 { "rtc_mtx", &lock_class_mtx_spin }, 414 #endif 415 { "scc_hwmtx", &lock_class_mtx_spin }, 416 { "uart_hwmtx", &lock_class_mtx_spin }, 417 { "fast_taskqueue", &lock_class_mtx_spin }, 418 { "intr table", &lock_class_mtx_spin }, 419 #ifdef HWPMC_HOOKS 420 { "pmc-per-proc", &lock_class_mtx_spin }, 421 #endif 422 { "process slock", &lock_class_mtx_spin }, 423 { "sleepq chain", &lock_class_mtx_spin }, 424 { "umtx lock", &lock_class_mtx_spin }, 425 { "turnstile chain", &lock_class_mtx_spin }, 426 { "turnstile lock", &lock_class_mtx_spin }, 427 { "sched lock", &lock_class_mtx_spin }, 428 { "td_contested", &lock_class_mtx_spin }, 429 { "callout", &lock_class_mtx_spin }, 430 { "entropy harvest mutex", &lock_class_mtx_spin }, 431 { "syscons video lock", &lock_class_mtx_spin }, 432 { "time lock", &lock_class_mtx_spin }, 433 /* 434 * leaf locks 435 */ 436 { "allpmaps", &lock_class_mtx_spin }, 437 { "icu", &lock_class_mtx_spin }, 438 #ifdef SMP 439 { "smp rendezvous", &lock_class_mtx_spin }, 440 #if defined(__i386__) || defined(__amd64__) 441 { "tlb", &lock_class_mtx_spin }, 442 #endif 443 #ifdef __sparc64__ 444 { "ipi", &lock_class_mtx_spin }, 445 #endif 446 #endif 447 { "clk", &lock_class_mtx_spin }, 448 { "mutex profiling lock", &lock_class_mtx_spin }, 449 { "kse lock", &lock_class_mtx_spin }, 450 { "zombie lock", &lock_class_mtx_spin }, 451 { "ALD Queue", &lock_class_mtx_spin }, 452 #ifdef __ia64__ 453 { "MCA spin lock", &lock_class_mtx_spin }, 454 #endif 455 #if defined(__i386__) || defined(__amd64__) 456 { "pcicfg", &lock_class_mtx_spin }, 457 { "NDIS thread lock", &lock_class_mtx_spin }, 458 #endif 459 { "tw_osl_io_lock", &lock_class_mtx_spin }, 460 { "tw_osl_q_lock", &lock_class_mtx_spin }, 461 { "tw_cl_io_lock", &lock_class_mtx_spin }, 462 { "tw_cl_intr_lock", &lock_class_mtx_spin }, 463 { "tw_cl_gen_lock", &lock_class_mtx_spin }, 464 #ifdef HWPMC_HOOKS 465 { "pmc-leaf", &lock_class_mtx_spin }, 466 #endif 467 { "blocked lock", &lock_class_mtx_spin }, 468 { NULL, NULL }, 469 { NULL, NULL } 470 }; 471 472 #ifdef BLESSING 473 /* 474 * Pairs of locks which have been blessed 475 * Don't complain about order problems with blessed locks 476 */ 477 static struct witness_blessed blessed_list[] = { 478 }; 479 static int blessed_count = 480 sizeof(blessed_list) / sizeof(struct witness_blessed); 481 #endif 482 483 /* 484 * List of locks initialized prior to witness being initialized whose 485 * enrollment is currently deferred. 486 */ 487 STAILQ_HEAD(, lock_object) pending_locks = 488 STAILQ_HEAD_INITIALIZER(pending_locks); 489 490 /* 491 * This global is set to 0 once it becomes safe to use the witness code. 492 */ 493 static int witness_cold = 1; 494 495 /* 496 * This global is set to 1 once the static lock orders have been enrolled 497 * so that a warning can be issued for any spin locks enrolled later. 498 */ 499 static int witness_spin_warn = 0; 500 501 /* 502 * The WITNESS-enabled diagnostic code. Note that the witness code does 503 * assume that the early boot is single-threaded at least until after this 504 * routine is completed. 505 */ 506 static void 507 witness_initialize(void *dummy __unused) 508 { 509 struct lock_object *lock; 510 struct witness_order_list_entry *order; 511 struct witness *w, *w1; 512 int i; 513 514 /* 515 * We have to release Giant before initializing its witness 516 * structure so that WITNESS doesn't get confused. 517 */ 518 mtx_unlock(&Giant); 519 mtx_assert(&Giant, MA_NOTOWNED); 520 521 CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 522 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 523 MTX_NOWITNESS | MTX_NOPROFILE); 524 for (i = 0; i < WITNESS_COUNT; i++) 525 witness_free(&w_data[i]); 526 for (i = 0; i < WITNESS_CHILDCOUNT; i++) 527 witness_child_free(&w_childdata[i]); 528 for (i = 0; i < LOCK_CHILDCOUNT; i++) 529 witness_lock_list_free(&w_locklistdata[i]); 530 531 /* First add in all the specified order lists. */ 532 for (order = order_lists; order->w_name != NULL; order++) { 533 w = enroll(order->w_name, order->w_class); 534 if (w == NULL) 535 continue; 536 w->w_file = "order list"; 537 for (order++; order->w_name != NULL; order++) { 538 w1 = enroll(order->w_name, order->w_class); 539 if (w1 == NULL) 540 continue; 541 w1->w_file = "order list"; 542 if (!itismychild(w, w1)) 543 panic("Not enough memory for static orders!"); 544 w = w1; 545 } 546 } 547 witness_spin_warn = 1; 548 549 /* Iterate through all locks and add them to witness. */ 550 while (!STAILQ_EMPTY(&pending_locks)) { 551 lock = STAILQ_FIRST(&pending_locks); 552 STAILQ_REMOVE_HEAD(&pending_locks, lo_list); 553 KASSERT(lock->lo_flags & LO_WITNESS, 554 ("%s: lock %s is on pending list but not LO_WITNESS", 555 __func__, lock->lo_name)); 556 lock->lo_witness = enroll(lock->lo_type, LOCK_CLASS(lock)); 557 } 558 559 /* Mark the witness code as being ready for use. */ 560 witness_cold = 0; 561 562 mtx_lock(&Giant); 563 } 564 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 565 566 static int 567 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 568 { 569 int error, value; 570 571 value = witness_watch; 572 error = sysctl_handle_int(oidp, &value, 0, req); 573 if (error != 0 || req->newptr == NULL) 574 return (error); 575 if (value == witness_watch) 576 return (0); 577 if (value != 0) 578 return (EINVAL); 579 witness_watch = 0; 580 return (0); 581 } 582 583 void 584 witness_init(struct lock_object *lock) 585 { 586 struct lock_class *class; 587 588 /* Various sanity checks. */ 589 class = LOCK_CLASS(lock); 590 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 591 (class->lc_flags & LC_RECURSABLE) == 0) 592 panic("%s: lock (%s) %s can not be recursable", __func__, 593 class->lc_name, lock->lo_name); 594 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 595 (class->lc_flags & LC_SLEEPABLE) == 0) 596 panic("%s: lock (%s) %s can not be sleepable", __func__, 597 class->lc_name, lock->lo_name); 598 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 599 (class->lc_flags & LC_UPGRADABLE) == 0) 600 panic("%s: lock (%s) %s can not be upgradable", __func__, 601 class->lc_name, lock->lo_name); 602 603 /* 604 * If we shouldn't watch this lock, then just clear lo_witness. 605 * Otherwise, if witness_cold is set, then it is too early to 606 * enroll this lock, so defer it to witness_initialize() by adding 607 * it to the pending_locks list. If it is not too early, then enroll 608 * the lock now. 609 */ 610 if (witness_watch == 0 || panicstr != NULL || 611 (lock->lo_flags & LO_WITNESS) == 0) 612 lock->lo_witness = NULL; 613 else if (witness_cold) { 614 STAILQ_INSERT_TAIL(&pending_locks, lock, lo_list); 615 lock->lo_flags |= LO_ENROLLPEND; 616 } else 617 lock->lo_witness = enroll(lock->lo_type, class); 618 } 619 620 void 621 witness_destroy(struct lock_object *lock) 622 { 623 struct lock_class *class; 624 struct witness *w; 625 626 class = LOCK_CLASS(lock); 627 if (witness_cold) 628 panic("lock (%s) %s destroyed while witness_cold", 629 class->lc_name, lock->lo_name); 630 631 /* XXX: need to verify that no one holds the lock */ 632 if ((lock->lo_flags & (LO_WITNESS | LO_ENROLLPEND)) == LO_WITNESS && 633 lock->lo_witness != NULL) { 634 w = lock->lo_witness; 635 mtx_lock_spin(&w_mtx); 636 MPASS(w->w_refcount > 0); 637 w->w_refcount--; 638 639 /* 640 * Lock is already released if we have an allocation failure 641 * and depart() fails. 642 */ 643 if (w->w_refcount != 0 || depart(w)) 644 mtx_unlock_spin(&w_mtx); 645 } 646 647 /* 648 * If this lock is destroyed before witness is up and running, 649 * remove it from the pending list. 650 */ 651 if (lock->lo_flags & LO_ENROLLPEND) { 652 STAILQ_REMOVE(&pending_locks, lock, lock_object, lo_list); 653 lock->lo_flags &= ~LO_ENROLLPEND; 654 } 655 } 656 657 #ifdef DDB 658 static void 659 witness_levelall (void) 660 { 661 struct witness_list *list; 662 struct witness *w, *w1; 663 664 /* 665 * First clear all levels. 666 */ 667 STAILQ_FOREACH(w, &w_all, w_list) { 668 w->w_level = 0; 669 } 670 671 /* 672 * Look for locks with no parent and level all their descendants. 673 */ 674 STAILQ_FOREACH(w, &w_all, w_list) { 675 /* 676 * This is just an optimization, technically we could get 677 * away just walking the all list each time. 678 */ 679 if (w->w_class->lc_flags & LC_SLEEPLOCK) 680 list = &w_sleep; 681 else 682 list = &w_spin; 683 STAILQ_FOREACH(w1, list, w_typelist) { 684 if (isitmychild(w1, w)) 685 goto skip; 686 } 687 witness_leveldescendents(w, 0); 688 skip: 689 ; /* silence GCC 3.x */ 690 } 691 } 692 693 static void 694 witness_leveldescendents(struct witness *parent, int level) 695 { 696 struct witness_child_list_entry *wcl; 697 int i; 698 699 if (parent->w_level < level) 700 parent->w_level = level; 701 level++; 702 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 703 for (i = 0; i < wcl->wcl_count; i++) 704 witness_leveldescendents(wcl->wcl_children[i], level); 705 } 706 707 static void 708 witness_displaydescendants(void(*prnt)(const char *fmt, ...), 709 struct witness *parent, int indent) 710 { 711 struct witness_child_list_entry *wcl; 712 int i, level; 713 714 level = parent->w_level; 715 prnt("%-2d", level); 716 for (i = 0; i < indent; i++) 717 prnt(" "); 718 if (parent->w_refcount > 0) 719 prnt("%s", parent->w_name); 720 else 721 prnt("(dead)"); 722 if (parent->w_displayed) { 723 prnt(" -- (already displayed)\n"); 724 return; 725 } 726 parent->w_displayed = 1; 727 if (parent->w_refcount > 0) { 728 if (parent->w_file != NULL) 729 prnt(" -- last acquired @ %s:%d", parent->w_file, 730 parent->w_line); 731 } 732 prnt("\n"); 733 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 734 for (i = 0; i < wcl->wcl_count; i++) 735 witness_displaydescendants(prnt, 736 wcl->wcl_children[i], indent + 1); 737 } 738 739 static void 740 witness_display_list(void(*prnt)(const char *fmt, ...), 741 struct witness_list *list) 742 { 743 struct witness *w; 744 745 STAILQ_FOREACH(w, list, w_typelist) { 746 if (w->w_file == NULL || w->w_level > 0) 747 continue; 748 /* 749 * This lock has no anscestors, display its descendants. 750 */ 751 witness_displaydescendants(prnt, w, 0); 752 } 753 } 754 755 static void 756 witness_display(void(*prnt)(const char *fmt, ...)) 757 { 758 struct witness *w; 759 760 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 761 witness_levelall(); 762 763 /* Clear all the displayed flags. */ 764 STAILQ_FOREACH(w, &w_all, w_list) { 765 w->w_displayed = 0; 766 } 767 768 /* 769 * First, handle sleep locks which have been acquired at least 770 * once. 771 */ 772 prnt("Sleep locks:\n"); 773 witness_display_list(prnt, &w_sleep); 774 775 /* 776 * Now do spin locks which have been acquired at least once. 777 */ 778 prnt("\nSpin locks:\n"); 779 witness_display_list(prnt, &w_spin); 780 781 /* 782 * Finally, any locks which have not been acquired yet. 783 */ 784 prnt("\nLocks which were never acquired:\n"); 785 STAILQ_FOREACH(w, &w_all, w_list) { 786 if (w->w_file != NULL || w->w_refcount == 0) 787 continue; 788 prnt("%s\n", w->w_name); 789 } 790 } 791 #endif /* DDB */ 792 793 /* Trim useless garbage from filenames. */ 794 static const char * 795 fixup_filename(const char *file) 796 { 797 798 if (file == NULL) 799 return (NULL); 800 while (strncmp(file, "../", 3) == 0) 801 file += 3; 802 return (file); 803 } 804 805 int 806 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 807 { 808 809 if (witness_watch == 0 || panicstr != NULL) 810 return (0); 811 812 /* Require locks that witness knows about. */ 813 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 814 lock2->lo_witness == NULL) 815 return (EINVAL); 816 817 MPASS(!mtx_owned(&w_mtx)); 818 mtx_lock_spin(&w_mtx); 819 820 /* 821 * If we already have either an explicit or implied lock order that 822 * is the other way around, then return an error. 823 */ 824 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 825 mtx_unlock_spin(&w_mtx); 826 return (EDOOFUS); 827 } 828 829 /* Try to add the new order. */ 830 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 831 lock2->lo_type, lock1->lo_type); 832 if (!itismychild(lock1->lo_witness, lock2->lo_witness)) 833 return (ENOMEM); 834 mtx_unlock_spin(&w_mtx); 835 return (0); 836 } 837 838 void 839 witness_checkorder(struct lock_object *lock, int flags, const char *file, 840 int line) 841 { 842 struct lock_list_entry **lock_list, *lle; 843 struct lock_instance *lock1, *lock2; 844 struct lock_class *class; 845 struct witness *w, *w1; 846 struct thread *td; 847 int i, j; 848 849 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 850 panicstr != NULL) 851 return; 852 853 /* 854 * Try locks do not block if they fail to acquire the lock, thus 855 * there is no danger of deadlocks or of switching while holding a 856 * spin lock if we acquire a lock via a try operation. This 857 * function shouldn't even be called for try locks, so panic if 858 * that happens. 859 */ 860 if (flags & LOP_TRYLOCK) 861 panic("%s should not be called for try lock operations", 862 __func__); 863 864 w = lock->lo_witness; 865 class = LOCK_CLASS(lock); 866 td = curthread; 867 file = fixup_filename(file); 868 869 if (class->lc_flags & LC_SLEEPLOCK) { 870 /* 871 * Since spin locks include a critical section, this check 872 * implicitly enforces a lock order of all sleep locks before 873 * all spin locks. 874 */ 875 if (td->td_critnest != 0 && !kdb_active) 876 panic("blockable sleep lock (%s) %s @ %s:%d", 877 class->lc_name, lock->lo_name, file, line); 878 879 /* 880 * If this is the first lock acquired then just return as 881 * no order checking is needed. 882 */ 883 if (td->td_sleeplocks == NULL) 884 return; 885 lock_list = &td->td_sleeplocks; 886 } else { 887 /* 888 * If this is the first lock, just return as no order 889 * checking is needed. We check this in both if clauses 890 * here as unifying the check would require us to use a 891 * critical section to ensure we don't migrate while doing 892 * the check. Note that if this is not the first lock, we 893 * are already in a critical section and are safe for the 894 * rest of the check. 895 */ 896 if (PCPU_GET(spinlocks) == NULL) 897 return; 898 lock_list = PCPU_PTR(spinlocks); 899 } 900 901 /* 902 * Check to see if we are recursing on a lock we already own. If 903 * so, make sure that we don't mismatch exclusive and shared lock 904 * acquires. 905 */ 906 lock1 = find_instance(*lock_list, lock); 907 if (lock1 != NULL) { 908 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 909 (flags & LOP_EXCLUSIVE) == 0) { 910 printf("shared lock of (%s) %s @ %s:%d\n", 911 class->lc_name, lock->lo_name, file, line); 912 printf("while exclusively locked from %s:%d\n", 913 lock1->li_file, lock1->li_line); 914 panic("share->excl"); 915 } 916 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 917 (flags & LOP_EXCLUSIVE) != 0) { 918 printf("exclusive lock of (%s) %s @ %s:%d\n", 919 class->lc_name, lock->lo_name, file, line); 920 printf("while share locked from %s:%d\n", 921 lock1->li_file, lock1->li_line); 922 panic("excl->share"); 923 } 924 return; 925 } 926 927 /* 928 * Try locks do not block if they fail to acquire the lock, thus 929 * there is no danger of deadlocks or of switching while holding a 930 * spin lock if we acquire a lock via a try operation. 931 */ 932 if (flags & LOP_TRYLOCK) 933 return; 934 935 /* 936 * Check for duplicate locks of the same type. Note that we only 937 * have to check for this on the last lock we just acquired. Any 938 * other cases will be caught as lock order violations. 939 */ 940 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 941 w1 = lock1->li_lock->lo_witness; 942 if (w1 == w) { 943 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) || 944 (flags & LOP_DUPOK)) 945 return; 946 w->w_same_squawked = 1; 947 printf("acquiring duplicate lock of same type: \"%s\"\n", 948 lock->lo_type); 949 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 950 lock1->li_file, lock1->li_line); 951 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 952 #ifdef KDB 953 goto debugger; 954 #else 955 return; 956 #endif 957 } 958 MPASS(!mtx_owned(&w_mtx)); 959 mtx_lock_spin(&w_mtx); 960 /* 961 * If we know that the the lock we are acquiring comes after 962 * the lock we most recently acquired in the lock order tree, 963 * then there is no need for any further checks. 964 */ 965 if (isitmychild(w1, w)) { 966 mtx_unlock_spin(&w_mtx); 967 return; 968 } 969 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 970 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 971 972 MPASS(j < WITNESS_COUNT); 973 lock1 = &lle->ll_children[i]; 974 w1 = lock1->li_lock->lo_witness; 975 976 /* 977 * If this lock doesn't undergo witness checking, 978 * then skip it. 979 */ 980 if (w1 == NULL) { 981 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 982 ("lock missing witness structure")); 983 continue; 984 } 985 /* 986 * If we are locking Giant and this is a sleepable 987 * lock, then skip it. 988 */ 989 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 990 lock == &Giant.lock_object) 991 continue; 992 /* 993 * If we are locking a sleepable lock and this lock 994 * is Giant, then skip it. 995 */ 996 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 997 lock1->li_lock == &Giant.lock_object) 998 continue; 999 /* 1000 * If we are locking a sleepable lock and this lock 1001 * isn't sleepable, we want to treat it as a lock 1002 * order violation to enfore a general lock order of 1003 * sleepable locks before non-sleepable locks. 1004 */ 1005 if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1006 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1007 goto reversal; 1008 /* 1009 * If we are locking Giant and this is a non-sleepable 1010 * lock, then treat it as a reversal. 1011 */ 1012 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 1013 lock == &Giant.lock_object) 1014 goto reversal; 1015 /* 1016 * Check the lock order hierarchy for a reveresal. 1017 */ 1018 if (!isitmydescendant(w, w1)) 1019 continue; 1020 reversal: 1021 /* 1022 * We have a lock order violation, check to see if it 1023 * is allowed or has already been yelled about. 1024 */ 1025 mtx_unlock_spin(&w_mtx); 1026 #ifdef BLESSING 1027 /* 1028 * If the lock order is blessed, just bail. We don't 1029 * look for other lock order violations though, which 1030 * may be a bug. 1031 */ 1032 if (blessed(w, w1)) 1033 return; 1034 #endif 1035 if (lock1->li_lock == &Giant.lock_object) { 1036 if (w1->w_Giant_squawked) 1037 return; 1038 else 1039 w1->w_Giant_squawked = 1; 1040 } else { 1041 if (w1->w_other_squawked) 1042 return; 1043 else 1044 w1->w_other_squawked = 1; 1045 } 1046 /* 1047 * Ok, yell about it. 1048 */ 1049 if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1050 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1051 printf( 1052 "lock order reversal: (sleepable after non-sleepable)\n"); 1053 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1054 && lock == &Giant.lock_object) 1055 printf( 1056 "lock order reversal: (Giant after non-sleepable)\n"); 1057 else 1058 printf("lock order reversal:\n"); 1059 /* 1060 * Try to locate an earlier lock with 1061 * witness w in our list. 1062 */ 1063 do { 1064 lock2 = &lle->ll_children[i]; 1065 MPASS(lock2->li_lock != NULL); 1066 if (lock2->li_lock->lo_witness == w) 1067 break; 1068 if (i == 0 && lle->ll_next != NULL) { 1069 lle = lle->ll_next; 1070 i = lle->ll_count - 1; 1071 MPASS(i >= 0 && i < LOCK_NCHILDREN); 1072 } else 1073 i--; 1074 } while (i >= 0); 1075 if (i < 0) { 1076 printf(" 1st %p %s (%s) @ %s:%d\n", 1077 lock1->li_lock, lock1->li_lock->lo_name, 1078 lock1->li_lock->lo_type, lock1->li_file, 1079 lock1->li_line); 1080 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 1081 lock->lo_name, lock->lo_type, file, line); 1082 } else { 1083 printf(" 1st %p %s (%s) @ %s:%d\n", 1084 lock2->li_lock, lock2->li_lock->lo_name, 1085 lock2->li_lock->lo_type, lock2->li_file, 1086 lock2->li_line); 1087 printf(" 2nd %p %s (%s) @ %s:%d\n", 1088 lock1->li_lock, lock1->li_lock->lo_name, 1089 lock1->li_lock->lo_type, lock1->li_file, 1090 lock1->li_line); 1091 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 1092 lock->lo_name, lock->lo_type, file, line); 1093 } 1094 #ifdef KDB 1095 goto debugger; 1096 #else 1097 return; 1098 #endif 1099 } 1100 } 1101 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 1102 /* 1103 * If requested, build a new lock order. However, don't build a new 1104 * relationship between a sleepable lock and Giant if it is in the 1105 * wrong direction. The correct lock order is that sleepable locks 1106 * always come before Giant. 1107 */ 1108 if (flags & LOP_NEWORDER && 1109 !(lock1->li_lock == &Giant.lock_object && 1110 (lock->lo_flags & LO_SLEEPABLE) != 0)) { 1111 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1112 lock->lo_type, lock1->li_lock->lo_type); 1113 if (!itismychild(lock1->li_lock->lo_witness, w)) 1114 /* Witness is dead. */ 1115 return; 1116 } 1117 mtx_unlock_spin(&w_mtx); 1118 return; 1119 1120 #ifdef KDB 1121 debugger: 1122 if (witness_trace) 1123 kdb_backtrace(); 1124 if (witness_kdb) 1125 kdb_enter(__func__); 1126 #endif 1127 } 1128 1129 void 1130 witness_lock(struct lock_object *lock, int flags, const char *file, int line) 1131 { 1132 struct lock_list_entry **lock_list, *lle; 1133 struct lock_instance *instance; 1134 struct witness *w; 1135 struct thread *td; 1136 1137 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1138 panicstr != NULL) 1139 return; 1140 w = lock->lo_witness; 1141 td = curthread; 1142 file = fixup_filename(file); 1143 1144 /* Determine lock list for this lock. */ 1145 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1146 lock_list = &td->td_sleeplocks; 1147 else 1148 lock_list = PCPU_PTR(spinlocks); 1149 1150 /* Check to see if we are recursing on a lock we already own. */ 1151 instance = find_instance(*lock_list, lock); 1152 if (instance != NULL) { 1153 instance->li_flags++; 1154 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1155 td->td_proc->p_pid, lock->lo_name, 1156 instance->li_flags & LI_RECURSEMASK); 1157 instance->li_file = file; 1158 instance->li_line = line; 1159 return; 1160 } 1161 1162 /* Update per-witness last file and line acquire. */ 1163 w->w_file = file; 1164 w->w_line = line; 1165 1166 /* Find the next open lock instance in the list and fill it. */ 1167 lle = *lock_list; 1168 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 1169 lle = witness_lock_list_get(); 1170 if (lle == NULL) 1171 return; 1172 lle->ll_next = *lock_list; 1173 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 1174 td->td_proc->p_pid, lle); 1175 *lock_list = lle; 1176 } 1177 instance = &lle->ll_children[lle->ll_count++]; 1178 instance->li_lock = lock; 1179 instance->li_line = line; 1180 instance->li_file = file; 1181 if ((flags & LOP_EXCLUSIVE) != 0) 1182 instance->li_flags = LI_EXCLUSIVE; 1183 else 1184 instance->li_flags = 0; 1185 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 1186 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 1187 } 1188 1189 void 1190 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 1191 { 1192 struct lock_instance *instance; 1193 struct lock_class *class; 1194 1195 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1196 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1197 return; 1198 class = LOCK_CLASS(lock); 1199 file = fixup_filename(file); 1200 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1201 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 1202 class->lc_name, lock->lo_name, file, line); 1203 if ((flags & LOP_TRYLOCK) == 0) 1204 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 1205 lock->lo_name, file, line); 1206 if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1207 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 1208 class->lc_name, lock->lo_name, file, line); 1209 instance = find_instance(curthread->td_sleeplocks, lock); 1210 if (instance == NULL) 1211 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 1212 class->lc_name, lock->lo_name, file, line); 1213 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1214 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 1215 class->lc_name, lock->lo_name, file, line); 1216 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1217 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1218 class->lc_name, lock->lo_name, 1219 instance->li_flags & LI_RECURSEMASK, file, line); 1220 instance->li_flags |= LI_EXCLUSIVE; 1221 } 1222 1223 void 1224 witness_downgrade(struct lock_object *lock, int flags, const char *file, 1225 int line) 1226 { 1227 struct lock_instance *instance; 1228 struct lock_class *class; 1229 1230 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1231 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1232 return; 1233 class = LOCK_CLASS(lock); 1234 file = fixup_filename(file); 1235 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1236 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 1237 class->lc_name, lock->lo_name, file, line); 1238 if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1239 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 1240 class->lc_name, lock->lo_name, file, line); 1241 instance = find_instance(curthread->td_sleeplocks, lock); 1242 if (instance == NULL) 1243 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 1244 class->lc_name, lock->lo_name, file, line); 1245 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1246 panic("downgrade of shared lock (%s) %s @ %s:%d", 1247 class->lc_name, lock->lo_name, file, line); 1248 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1249 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1250 class->lc_name, lock->lo_name, 1251 instance->li_flags & LI_RECURSEMASK, file, line); 1252 instance->li_flags &= ~LI_EXCLUSIVE; 1253 } 1254 1255 void 1256 witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 1257 { 1258 struct lock_list_entry **lock_list, *lle; 1259 struct lock_instance *instance; 1260 struct lock_class *class; 1261 struct thread *td; 1262 register_t s; 1263 int i, j; 1264 1265 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1266 panicstr != NULL) 1267 return; 1268 td = curthread; 1269 class = LOCK_CLASS(lock); 1270 file = fixup_filename(file); 1271 1272 /* Find lock instance associated with this lock. */ 1273 if (class->lc_flags & LC_SLEEPLOCK) 1274 lock_list = &td->td_sleeplocks; 1275 else 1276 lock_list = PCPU_PTR(spinlocks); 1277 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 1278 for (i = 0; i < (*lock_list)->ll_count; i++) { 1279 instance = &(*lock_list)->ll_children[i]; 1280 if (instance->li_lock == lock) 1281 goto found; 1282 } 1283 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 1284 file, line); 1285 found: 1286 1287 /* First, check for shared/exclusive mismatches. */ 1288 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 1289 (flags & LOP_EXCLUSIVE) == 0) { 1290 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1291 lock->lo_name, file, line); 1292 printf("while exclusively locked from %s:%d\n", 1293 instance->li_file, instance->li_line); 1294 panic("excl->ushare"); 1295 } 1296 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 1297 (flags & LOP_EXCLUSIVE) != 0) { 1298 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1299 lock->lo_name, file, line); 1300 printf("while share locked from %s:%d\n", instance->li_file, 1301 instance->li_line); 1302 panic("share->uexcl"); 1303 } 1304 1305 /* If we are recursed, unrecurse. */ 1306 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1307 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1308 td->td_proc->p_pid, instance->li_lock->lo_name, 1309 instance->li_flags); 1310 instance->li_flags--; 1311 return; 1312 } 1313 1314 /* Otherwise, remove this item from the list. */ 1315 s = intr_disable(); 1316 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1317 td->td_proc->p_pid, instance->li_lock->lo_name, 1318 (*lock_list)->ll_count - 1); 1319 for (j = i; j < (*lock_list)->ll_count - 1; j++) 1320 (*lock_list)->ll_children[j] = 1321 (*lock_list)->ll_children[j + 1]; 1322 (*lock_list)->ll_count--; 1323 intr_restore(s); 1324 1325 /* If this lock list entry is now empty, free it. */ 1326 if ((*lock_list)->ll_count == 0) { 1327 lle = *lock_list; 1328 *lock_list = lle->ll_next; 1329 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1330 td->td_proc->p_pid, lle); 1331 witness_lock_list_free(lle); 1332 } 1333 } 1334 1335 /* 1336 * Warn if any locks other than 'lock' are held. Flags can be passed in to 1337 * exempt Giant and sleepable locks from the checks as well. If any 1338 * non-exempt locks are held, then a supplied message is printed to the 1339 * console along with a list of the offending locks. If indicated in the 1340 * flags then a failure results in a panic as well. 1341 */ 1342 int 1343 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 1344 { 1345 struct lock_list_entry *lle; 1346 struct lock_instance *lock1; 1347 struct thread *td; 1348 va_list ap; 1349 int i, n; 1350 1351 if (witness_cold || witness_watch == 0 || panicstr != NULL) 1352 return (0); 1353 n = 0; 1354 td = curthread; 1355 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 1356 for (i = lle->ll_count - 1; i >= 0; i--) { 1357 lock1 = &lle->ll_children[i]; 1358 if (lock1->li_lock == lock) 1359 continue; 1360 if (flags & WARN_GIANTOK && 1361 lock1->li_lock == &Giant.lock_object) 1362 continue; 1363 if (flags & WARN_SLEEPOK && 1364 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 1365 continue; 1366 if (n == 0) { 1367 va_start(ap, fmt); 1368 vprintf(fmt, ap); 1369 va_end(ap); 1370 printf(" with the following"); 1371 if (flags & WARN_SLEEPOK) 1372 printf(" non-sleepable"); 1373 printf(" locks held:\n"); 1374 } 1375 n++; 1376 witness_list_lock(lock1); 1377 } 1378 if (PCPU_GET(spinlocks) != NULL) { 1379 /* 1380 * Since we already hold a spinlock preemption is 1381 * already blocked. 1382 */ 1383 if (n == 0) { 1384 va_start(ap, fmt); 1385 vprintf(fmt, ap); 1386 va_end(ap); 1387 printf(" with the following"); 1388 if (flags & WARN_SLEEPOK) 1389 printf(" non-sleepable"); 1390 printf(" locks held:\n"); 1391 } 1392 n += witness_list_locks(PCPU_PTR(spinlocks)); 1393 } 1394 if (flags & WARN_PANIC && n) 1395 panic("witness_warn"); 1396 #ifdef KDB 1397 else if (witness_kdb && n) 1398 kdb_enter(__func__); 1399 else if (witness_trace && n) 1400 kdb_backtrace(); 1401 #endif 1402 return (n); 1403 } 1404 1405 const char * 1406 witness_file(struct lock_object *lock) 1407 { 1408 struct witness *w; 1409 1410 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1411 return ("?"); 1412 w = lock->lo_witness; 1413 return (w->w_file); 1414 } 1415 1416 int 1417 witness_line(struct lock_object *lock) 1418 { 1419 struct witness *w; 1420 1421 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1422 return (0); 1423 w = lock->lo_witness; 1424 return (w->w_line); 1425 } 1426 1427 static struct witness * 1428 enroll(const char *description, struct lock_class *lock_class) 1429 { 1430 struct witness *w; 1431 1432 if (witness_watch == 0 || panicstr != NULL) 1433 return (NULL); 1434 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 1435 return (NULL); 1436 mtx_lock_spin(&w_mtx); 1437 STAILQ_FOREACH(w, &w_all, w_list) { 1438 if (w->w_name == description || (w->w_refcount > 0 && 1439 strcmp(description, w->w_name) == 0)) { 1440 w->w_refcount++; 1441 mtx_unlock_spin(&w_mtx); 1442 if (lock_class != w->w_class) 1443 panic( 1444 "lock (%s) %s does not match earlier (%s) lock", 1445 description, lock_class->lc_name, 1446 w->w_class->lc_name); 1447 return (w); 1448 } 1449 } 1450 if ((w = witness_get()) == NULL) 1451 goto out; 1452 w->w_name = description; 1453 w->w_class = lock_class; 1454 w->w_refcount = 1; 1455 STAILQ_INSERT_HEAD(&w_all, w, w_list); 1456 if (lock_class->lc_flags & LC_SPINLOCK) { 1457 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1458 w_spin_cnt++; 1459 } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 1460 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1461 w_sleep_cnt++; 1462 } else { 1463 mtx_unlock_spin(&w_mtx); 1464 panic("lock class %s is not sleep or spin", 1465 lock_class->lc_name); 1466 } 1467 mtx_unlock_spin(&w_mtx); 1468 out: 1469 /* 1470 * We issue a warning for any spin locks not defined in the static 1471 * order list as a way to discourage their use (folks should really 1472 * be using non-spin mutexes most of the time). However, several 1473 * 3rd part device drivers use spin locks because that is all they 1474 * have available on Windows and Linux and they think that normal 1475 * mutexes are insufficient. 1476 */ 1477 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_spin_warn) 1478 printf("WITNESS: spin lock %s not in order list\n", 1479 description); 1480 return (w); 1481 } 1482 1483 /* Don't let the door bang you on the way out... */ 1484 static int 1485 depart(struct witness *w) 1486 { 1487 struct witness_child_list_entry *wcl, *nwcl; 1488 struct witness_list *list; 1489 struct witness *parent; 1490 1491 MPASS(w->w_refcount == 0); 1492 if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1493 list = &w_sleep; 1494 w_sleep_cnt--; 1495 } else { 1496 list = &w_spin; 1497 w_spin_cnt--; 1498 } 1499 /* 1500 * First, we run through the entire tree looking for any 1501 * witnesses that the outgoing witness is a child of. For 1502 * each parent that we find, we reparent all the direct 1503 * children of the outgoing witness to its parent. 1504 */ 1505 STAILQ_FOREACH(parent, list, w_typelist) { 1506 if (!isitmychild(parent, w)) 1507 continue; 1508 removechild(parent, w); 1509 } 1510 1511 /* 1512 * Now we go through and free up the child list of the 1513 * outgoing witness. 1514 */ 1515 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1516 nwcl = wcl->wcl_next; 1517 w_child_cnt--; 1518 witness_child_free(wcl); 1519 } 1520 1521 /* 1522 * Detach from various lists and free. 1523 */ 1524 STAILQ_REMOVE(list, w, witness, w_typelist); 1525 STAILQ_REMOVE(&w_all, w, witness, w_list); 1526 witness_free(w); 1527 1528 return (1); 1529 } 1530 1531 /* 1532 * Add "child" as a direct child of "parent". Returns false if 1533 * we fail due to out of memory. 1534 */ 1535 static int 1536 insertchild(struct witness *parent, struct witness *child) 1537 { 1538 struct witness_child_list_entry **wcl; 1539 1540 MPASS(child != NULL && parent != NULL); 1541 1542 /* 1543 * Insert "child" after "parent" 1544 */ 1545 wcl = &parent->w_children; 1546 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 1547 wcl = &(*wcl)->wcl_next; 1548 if (*wcl == NULL) { 1549 *wcl = witness_child_get(); 1550 if (*wcl == NULL) 1551 return (0); 1552 w_child_cnt++; 1553 } 1554 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1555 1556 return (1); 1557 } 1558 1559 1560 static int 1561 itismychild(struct witness *parent, struct witness *child) 1562 { 1563 struct witness_list *list; 1564 1565 MPASS(child != NULL && parent != NULL); 1566 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1567 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1568 panic( 1569 "%s: parent (%s) and child (%s) are not the same lock type", 1570 __func__, parent->w_class->lc_name, 1571 child->w_class->lc_name); 1572 1573 if (!insertchild(parent, child)) 1574 return (0); 1575 1576 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1577 list = &w_sleep; 1578 else 1579 list = &w_spin; 1580 return (1); 1581 } 1582 1583 static void 1584 removechild(struct witness *parent, struct witness *child) 1585 { 1586 struct witness_child_list_entry **wcl, *wcl1; 1587 int i; 1588 1589 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1590 for (i = 0; i < (*wcl)->wcl_count; i++) 1591 if ((*wcl)->wcl_children[i] == child) 1592 goto found; 1593 return; 1594 found: 1595 (*wcl)->wcl_count--; 1596 if ((*wcl)->wcl_count > i) 1597 (*wcl)->wcl_children[i] = 1598 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1599 MPASS((*wcl)->wcl_children[i] != NULL); 1600 if ((*wcl)->wcl_count != 0) 1601 return; 1602 wcl1 = *wcl; 1603 *wcl = wcl1->wcl_next; 1604 w_child_cnt--; 1605 witness_child_free(wcl1); 1606 } 1607 1608 static int 1609 isitmychild(struct witness *parent, struct witness *child) 1610 { 1611 struct witness_child_list_entry *wcl; 1612 int i; 1613 1614 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1615 for (i = 0; i < wcl->wcl_count; i++) { 1616 if (wcl->wcl_children[i] == child) 1617 return (1); 1618 } 1619 } 1620 return (0); 1621 } 1622 1623 static int 1624 isitmydescendant(struct witness *parent, struct witness *child) 1625 { 1626 struct witness_child_list_entry *wcl; 1627 int i, j; 1628 1629 if (isitmychild(parent, child)) 1630 return (1); 1631 j = 0; 1632 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1633 MPASS(j < 1000); 1634 for (i = 0; i < wcl->wcl_count; i++) { 1635 if (isitmydescendant(wcl->wcl_children[i], child)) 1636 return (1); 1637 } 1638 j++; 1639 } 1640 return (0); 1641 } 1642 1643 #ifdef BLESSING 1644 static int 1645 blessed(struct witness *w1, struct witness *w2) 1646 { 1647 int i; 1648 struct witness_blessed *b; 1649 1650 for (i = 0; i < blessed_count; i++) { 1651 b = &blessed_list[i]; 1652 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1653 if (strcmp(w2->w_name, b->b_lock2) == 0) 1654 return (1); 1655 continue; 1656 } 1657 if (strcmp(w1->w_name, b->b_lock2) == 0) 1658 if (strcmp(w2->w_name, b->b_lock1) == 0) 1659 return (1); 1660 } 1661 return (0); 1662 } 1663 #endif 1664 1665 static struct witness * 1666 witness_get(void) 1667 { 1668 struct witness *w; 1669 1670 if (witness_watch == 0) { 1671 mtx_unlock_spin(&w_mtx); 1672 return (NULL); 1673 } 1674 if (STAILQ_EMPTY(&w_free)) { 1675 witness_watch = 0; 1676 mtx_unlock_spin(&w_mtx); 1677 printf("%s: witness exhausted\n", __func__); 1678 return (NULL); 1679 } 1680 w = STAILQ_FIRST(&w_free); 1681 STAILQ_REMOVE_HEAD(&w_free, w_list); 1682 w_free_cnt--; 1683 bzero(w, sizeof(*w)); 1684 return (w); 1685 } 1686 1687 static void 1688 witness_free(struct witness *w) 1689 { 1690 1691 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1692 w_free_cnt++; 1693 } 1694 1695 static struct witness_child_list_entry * 1696 witness_child_get(void) 1697 { 1698 struct witness_child_list_entry *wcl; 1699 1700 if (witness_watch == 0) { 1701 mtx_unlock_spin(&w_mtx); 1702 return (NULL); 1703 } 1704 wcl = w_child_free; 1705 if (wcl == NULL) { 1706 witness_watch = 0; 1707 mtx_unlock_spin(&w_mtx); 1708 printf("%s: witness exhausted\n", __func__); 1709 return (NULL); 1710 } 1711 w_child_free = wcl->wcl_next; 1712 w_child_free_cnt--; 1713 bzero(wcl, sizeof(*wcl)); 1714 return (wcl); 1715 } 1716 1717 static void 1718 witness_child_free(struct witness_child_list_entry *wcl) 1719 { 1720 1721 wcl->wcl_next = w_child_free; 1722 w_child_free = wcl; 1723 w_child_free_cnt++; 1724 } 1725 1726 static struct lock_list_entry * 1727 witness_lock_list_get(void) 1728 { 1729 struct lock_list_entry *lle; 1730 1731 if (witness_watch == 0) 1732 return (NULL); 1733 mtx_lock_spin(&w_mtx); 1734 lle = w_lock_list_free; 1735 if (lle == NULL) { 1736 witness_watch = 0; 1737 mtx_unlock_spin(&w_mtx); 1738 printf("%s: witness exhausted\n", __func__); 1739 return (NULL); 1740 } 1741 w_lock_list_free = lle->ll_next; 1742 mtx_unlock_spin(&w_mtx); 1743 bzero(lle, sizeof(*lle)); 1744 return (lle); 1745 } 1746 1747 static void 1748 witness_lock_list_free(struct lock_list_entry *lle) 1749 { 1750 1751 mtx_lock_spin(&w_mtx); 1752 lle->ll_next = w_lock_list_free; 1753 w_lock_list_free = lle; 1754 mtx_unlock_spin(&w_mtx); 1755 } 1756 1757 static struct lock_instance * 1758 find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1759 { 1760 struct lock_list_entry *lle; 1761 struct lock_instance *instance; 1762 int i; 1763 1764 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1765 for (i = lle->ll_count - 1; i >= 0; i--) { 1766 instance = &lle->ll_children[i]; 1767 if (instance->li_lock == lock) 1768 return (instance); 1769 } 1770 return (NULL); 1771 } 1772 1773 static void 1774 witness_list_lock(struct lock_instance *instance) 1775 { 1776 struct lock_object *lock; 1777 1778 lock = instance->li_lock; 1779 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1780 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 1781 if (lock->lo_type != lock->lo_name) 1782 printf(" (%s)", lock->lo_type); 1783 printf(" r = %d (%p) locked @ %s:%d\n", 1784 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1785 instance->li_line); 1786 } 1787 1788 #ifdef DDB 1789 static int 1790 witness_thread_has_locks(struct thread *td) 1791 { 1792 1793 return (td->td_sleeplocks != NULL); 1794 } 1795 1796 static int 1797 witness_proc_has_locks(struct proc *p) 1798 { 1799 struct thread *td; 1800 1801 FOREACH_THREAD_IN_PROC(p, td) { 1802 if (witness_thread_has_locks(td)) 1803 return (1); 1804 } 1805 return (0); 1806 } 1807 #endif 1808 1809 int 1810 witness_list_locks(struct lock_list_entry **lock_list) 1811 { 1812 struct lock_list_entry *lle; 1813 int i, nheld; 1814 1815 nheld = 0; 1816 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1817 for (i = lle->ll_count - 1; i >= 0; i--) { 1818 witness_list_lock(&lle->ll_children[i]); 1819 nheld++; 1820 } 1821 return (nheld); 1822 } 1823 1824 /* 1825 * This is a bit risky at best. We call this function when we have timed 1826 * out acquiring a spin lock, and we assume that the other CPU is stuck 1827 * with this lock held. So, we go groveling around in the other CPU's 1828 * per-cpu data to try to find the lock instance for this spin lock to 1829 * see when it was last acquired. 1830 */ 1831 void 1832 witness_display_spinlock(struct lock_object *lock, struct thread *owner) 1833 { 1834 struct lock_instance *instance; 1835 struct pcpu *pc; 1836 1837 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 1838 return; 1839 pc = pcpu_find(owner->td_oncpu); 1840 instance = find_instance(pc->pc_spinlocks, lock); 1841 if (instance != NULL) 1842 witness_list_lock(instance); 1843 } 1844 1845 void 1846 witness_save(struct lock_object *lock, const char **filep, int *linep) 1847 { 1848 struct lock_list_entry *lock_list; 1849 struct lock_instance *instance; 1850 struct lock_class *class; 1851 1852 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1853 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1854 return; 1855 class = LOCK_CLASS(lock); 1856 if (class->lc_flags & LC_SLEEPLOCK) 1857 lock_list = curthread->td_sleeplocks; 1858 else { 1859 if (witness_skipspin) 1860 return; 1861 lock_list = PCPU_GET(spinlocks); 1862 } 1863 instance = find_instance(lock_list, lock); 1864 if (instance == NULL) 1865 panic("%s: lock (%s) %s not locked", __func__, 1866 class->lc_name, lock->lo_name); 1867 *filep = instance->li_file; 1868 *linep = instance->li_line; 1869 } 1870 1871 void 1872 witness_restore(struct lock_object *lock, const char *file, int line) 1873 { 1874 struct lock_list_entry *lock_list; 1875 struct lock_instance *instance; 1876 struct lock_class *class; 1877 1878 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1879 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1880 return; 1881 class = LOCK_CLASS(lock); 1882 if (class->lc_flags & LC_SLEEPLOCK) 1883 lock_list = curthread->td_sleeplocks; 1884 else { 1885 if (witness_skipspin) 1886 return; 1887 lock_list = PCPU_GET(spinlocks); 1888 } 1889 instance = find_instance(lock_list, lock); 1890 if (instance == NULL) 1891 panic("%s: lock (%s) %s not locked", __func__, 1892 class->lc_name, lock->lo_name); 1893 lock->lo_witness->w_file = file; 1894 lock->lo_witness->w_line = line; 1895 instance->li_file = file; 1896 instance->li_line = line; 1897 } 1898 1899 void 1900 witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1901 { 1902 #ifdef INVARIANT_SUPPORT 1903 struct lock_instance *instance; 1904 struct lock_class *class; 1905 1906 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1907 return; 1908 class = LOCK_CLASS(lock); 1909 if ((class->lc_flags & LC_SLEEPLOCK) != 0) 1910 instance = find_instance(curthread->td_sleeplocks, lock); 1911 else if ((class->lc_flags & LC_SPINLOCK) != 0) 1912 instance = find_instance(PCPU_GET(spinlocks), lock); 1913 else { 1914 panic("Lock (%s) %s is not sleep or spin!", 1915 class->lc_name, lock->lo_name); 1916 } 1917 file = fixup_filename(file); 1918 switch (flags) { 1919 case LA_UNLOCKED: 1920 if (instance != NULL) 1921 panic("Lock (%s) %s locked @ %s:%d.", 1922 class->lc_name, lock->lo_name, file, line); 1923 break; 1924 case LA_LOCKED: 1925 case LA_LOCKED | LA_RECURSED: 1926 case LA_LOCKED | LA_NOTRECURSED: 1927 case LA_SLOCKED: 1928 case LA_SLOCKED | LA_RECURSED: 1929 case LA_SLOCKED | LA_NOTRECURSED: 1930 case LA_XLOCKED: 1931 case LA_XLOCKED | LA_RECURSED: 1932 case LA_XLOCKED | LA_NOTRECURSED: 1933 if (instance == NULL) { 1934 panic("Lock (%s) %s not locked @ %s:%d.", 1935 class->lc_name, lock->lo_name, file, line); 1936 break; 1937 } 1938 if ((flags & LA_XLOCKED) != 0 && 1939 (instance->li_flags & LI_EXCLUSIVE) == 0) 1940 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1941 class->lc_name, lock->lo_name, file, line); 1942 if ((flags & LA_SLOCKED) != 0 && 1943 (instance->li_flags & LI_EXCLUSIVE) != 0) 1944 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1945 class->lc_name, lock->lo_name, file, line); 1946 if ((flags & LA_RECURSED) != 0 && 1947 (instance->li_flags & LI_RECURSEMASK) == 0) 1948 panic("Lock (%s) %s not recursed @ %s:%d.", 1949 class->lc_name, lock->lo_name, file, line); 1950 if ((flags & LA_NOTRECURSED) != 0 && 1951 (instance->li_flags & LI_RECURSEMASK) != 0) 1952 panic("Lock (%s) %s recursed @ %s:%d.", 1953 class->lc_name, lock->lo_name, file, line); 1954 break; 1955 default: 1956 panic("Invalid lock assertion at %s:%d.", file, line); 1957 1958 } 1959 #endif /* INVARIANT_SUPPORT */ 1960 } 1961 1962 #ifdef DDB 1963 static void 1964 witness_list(struct thread *td) 1965 { 1966 1967 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1968 KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 1969 1970 if (witness_watch == 0) 1971 return; 1972 1973 witness_list_locks(&td->td_sleeplocks); 1974 1975 /* 1976 * We only handle spinlocks if td == curthread. This is somewhat broken 1977 * if td is currently executing on some other CPU and holds spin locks 1978 * as we won't display those locks. If we had a MI way of getting 1979 * the per-cpu data for a given cpu then we could use 1980 * td->td_oncpu to get the list of spinlocks for this thread 1981 * and "fix" this. 1982 * 1983 * That still wouldn't really fix this unless we locked the scheduler 1984 * lock or stopped the other CPU to make sure it wasn't changing the 1985 * list out from under us. It is probably best to just not try to 1986 * handle threads on other CPU's for now. 1987 */ 1988 if (td == curthread && PCPU_GET(spinlocks) != NULL) 1989 witness_list_locks(PCPU_PTR(spinlocks)); 1990 } 1991 1992 DB_SHOW_COMMAND(locks, db_witness_list) 1993 { 1994 struct thread *td; 1995 1996 if (have_addr) 1997 td = db_lookup_thread(addr, TRUE); 1998 else 1999 td = kdb_thread; 2000 witness_list(td); 2001 } 2002 2003 DB_SHOW_COMMAND(alllocks, db_witness_list_all) 2004 { 2005 struct thread *td; 2006 struct proc *p; 2007 2008 /* 2009 * It would be nice to list only threads and processes that actually 2010 * held sleep locks, but that information is currently not exported 2011 * by WITNESS. 2012 */ 2013 FOREACH_PROC_IN_SYSTEM(p) { 2014 if (!witness_proc_has_locks(p)) 2015 continue; 2016 FOREACH_THREAD_IN_PROC(p, td) { 2017 if (!witness_thread_has_locks(td)) 2018 continue; 2019 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2020 p->p_comm, td, td->td_tid); 2021 witness_list(td); 2022 } 2023 } 2024 } 2025 2026 DB_SHOW_COMMAND(witness, db_witness_display) 2027 { 2028 2029 witness_display(db_printf); 2030 } 2031 #endif 2032