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_witness.h" 89 90 #include <sys/param.h> 91 #include <sys/bus.h> 92 #include <sys/kernel.h> 93 #include <sys/ktr.h> 94 #include <sys/lock.h> 95 #include <sys/malloc.h> 96 #include <sys/mutex.h> 97 #include <sys/proc.h> 98 #include <sys/sysctl.h> 99 #include <sys/systm.h> 100 101 #include <ddb/ddb.h> 102 103 #include <machine/stdarg.h> 104 105 /* Define this to check for blessed mutexes */ 106 #undef BLESSING 107 108 #define WITNESS_COUNT 200 109 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 110 /* 111 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 112 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 113 * probably be safe for the most part, but it's still a SWAG. 114 */ 115 #define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 116 117 #define WITNESS_NCHILDREN 6 118 119 struct witness_child_list_entry; 120 121 struct witness { 122 const char *w_name; 123 struct lock_class *w_class; 124 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 125 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 126 struct witness_child_list_entry *w_children; /* Great evilness... */ 127 const char *w_file; 128 int w_line; 129 u_int w_level; 130 u_int w_refcount; 131 u_char w_Giant_squawked:1; 132 u_char w_other_squawked:1; 133 u_char w_same_squawked:1; 134 u_char w_displayed:1; 135 }; 136 137 struct witness_child_list_entry { 138 struct witness_child_list_entry *wcl_next; 139 struct witness *wcl_children[WITNESS_NCHILDREN]; 140 u_int wcl_count; 141 }; 142 143 STAILQ_HEAD(witness_list, witness); 144 145 #ifdef BLESSING 146 struct witness_blessed { 147 const char *b_lock1; 148 const char *b_lock2; 149 }; 150 #endif 151 152 struct witness_order_list_entry { 153 const char *w_name; 154 struct lock_class *w_class; 155 }; 156 157 #ifdef BLESSING 158 static int blessed(struct witness *, struct witness *); 159 #endif 160 static int depart(struct witness *w); 161 static struct witness *enroll(const char *description, 162 struct lock_class *lock_class); 163 static int insertchild(struct witness *parent, struct witness *child); 164 static int isitmychild(struct witness *parent, struct witness *child); 165 static int isitmydescendant(struct witness *parent, struct witness *child); 166 static int itismychild(struct witness *parent, struct witness *child); 167 static int rebalancetree(struct witness_list *list); 168 static void removechild(struct witness *parent, struct witness *child); 169 static int reparentchildren(struct witness *newparent, 170 struct witness *oldparent); 171 static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 172 static void witness_displaydescendants(void(*)(const char *fmt, ...), 173 struct witness *, int indent); 174 static const char *fixup_filename(const char *file); 175 static void witness_leveldescendents(struct witness *parent, int level); 176 static void witness_levelall(void); 177 static struct witness *witness_get(void); 178 static void witness_free(struct witness *m); 179 static struct witness_child_list_entry *witness_child_get(void); 180 static void witness_child_free(struct witness_child_list_entry *wcl); 181 static struct lock_list_entry *witness_lock_list_get(void); 182 static void witness_lock_list_free(struct lock_list_entry *lle); 183 static struct lock_instance *find_instance(struct lock_list_entry *lock_list, 184 struct lock_object *lock); 185 static void witness_list_lock(struct lock_instance *instance); 186 #ifdef DDB 187 static void witness_list(struct thread *td); 188 static void witness_display_list(void(*prnt)(const char *fmt, ...), 189 struct witness_list *list); 190 static void witness_display(void(*)(const char *fmt, ...)); 191 #endif 192 193 MALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 194 195 /* 196 * If set to 0, witness is disabled. If set to 1, witness performs full lock 197 * order checking for all locks. If set to 2 or higher, then witness skips 198 * the full lock order check if the lock being acquired is at a higher level 199 * (i.e. farther down in the tree) than the current lock. This last mode is 200 * somewhat experimental and not considered fully safe. At runtime, this 201 * value may be set to 0 to turn off witness. witness is not allowed be 202 * turned on once it is turned off, however. 203 */ 204 static int witness_watch = 1; 205 TUNABLE_INT("debug.witness_watch", &witness_watch); 206 SYSCTL_PROC(_debug, OID_AUTO, witness_watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 207 sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 208 209 #ifdef DDB 210 /* 211 * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 212 * drop into kdebug() when: 213 * - a lock heirarchy violation occurs 214 * - locks are held when going to sleep. 215 */ 216 #ifdef WITNESS_DDB 217 int witness_ddb = 1; 218 #else 219 int witness_ddb = 0; 220 #endif 221 TUNABLE_INT("debug.witness_ddb", &witness_ddb); 222 SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 223 224 /* 225 * When DDB is enabled and witness_trace is set to 1, it will cause the system 226 * to print a stack trace: 227 * - a lock heirarchy violation occurs 228 * - locks are held when going to sleep. 229 */ 230 int witness_trace = 1; 231 TUNABLE_INT("debug.witness_trace", &witness_trace); 232 SYSCTL_INT(_debug, OID_AUTO, witness_trace, CTLFLAG_RW, &witness_trace, 0, ""); 233 #endif /* DDB */ 234 235 #ifdef WITNESS_SKIPSPIN 236 int witness_skipspin = 1; 237 #else 238 int witness_skipspin = 0; 239 #endif 240 TUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 241 SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RDTUN, &witness_skipspin, 0, 242 ""); 243 244 static struct mtx w_mtx; 245 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 246 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 247 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 248 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 249 static struct witness_child_list_entry *w_child_free = NULL; 250 static struct lock_list_entry *w_lock_list_free = NULL; 251 252 static struct witness w_data[WITNESS_COUNT]; 253 static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 254 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 255 256 static struct witness_order_list_entry order_lists[] = { 257 { "proctree", &lock_class_sx }, 258 { "allproc", &lock_class_sx }, 259 { "Giant", &lock_class_mtx_sleep }, 260 { "filedesc structure", &lock_class_mtx_sleep }, 261 { "pipe mutex", &lock_class_mtx_sleep }, 262 { "sigio lock", &lock_class_mtx_sleep }, 263 { "process group", &lock_class_mtx_sleep }, 264 { "process lock", &lock_class_mtx_sleep }, 265 { "session", &lock_class_mtx_sleep }, 266 { "uidinfo hash", &lock_class_mtx_sleep }, 267 { "uidinfo struct", &lock_class_mtx_sleep }, 268 { "allprison", &lock_class_mtx_sleep }, 269 { NULL, NULL }, 270 /* 271 * spin locks 272 */ 273 #ifdef SMP 274 { "ap boot", &lock_class_mtx_spin }, 275 #endif 276 { "sio", &lock_class_mtx_spin }, 277 #ifdef __i386__ 278 { "cy", &lock_class_mtx_spin }, 279 #endif 280 { "uart_hwmtx", &lock_class_mtx_spin }, 281 { "sabtty", &lock_class_mtx_spin }, 282 { "zstty", &lock_class_mtx_spin }, 283 { "ng_node", &lock_class_mtx_spin }, 284 { "ng_worklist", &lock_class_mtx_spin }, 285 { "taskqueue_fast", &lock_class_mtx_spin }, 286 { "intr table", &lock_class_mtx_spin }, 287 { "ithread table lock", &lock_class_mtx_spin }, 288 { "sleepq chain", &lock_class_mtx_spin }, 289 { "sched lock", &lock_class_mtx_spin }, 290 { "turnstile chain", &lock_class_mtx_spin }, 291 { "td_contested", &lock_class_mtx_spin }, 292 { "callout", &lock_class_mtx_spin }, 293 { "entropy harvest", &lock_class_mtx_spin }, 294 { "entropy harvest buffers", &lock_class_mtx_spin }, 295 /* 296 * leaf locks 297 */ 298 { "allpmaps", &lock_class_mtx_spin }, 299 { "vm page queue free mutex", &lock_class_mtx_spin }, 300 { "icu", &lock_class_mtx_spin }, 301 #ifdef SMP 302 { "smp rendezvous", &lock_class_mtx_spin }, 303 #if defined(__i386__) || defined(__amd64__) 304 { "tlb", &lock_class_mtx_spin }, 305 { "lazypmap", &lock_class_mtx_spin }, 306 #endif 307 #ifdef __sparc64__ 308 { "ipi", &lock_class_mtx_spin }, 309 #endif 310 #endif 311 { "clk", &lock_class_mtx_spin }, 312 { "mutex profiling lock", &lock_class_mtx_spin }, 313 { "kse zombie lock", &lock_class_mtx_spin }, 314 { "ALD Queue", &lock_class_mtx_spin }, 315 #ifdef __ia64__ 316 { "MCA spin lock", &lock_class_mtx_spin }, 317 #endif 318 #if defined(__i386__) || defined(__amd64__) 319 { "pcicfg", &lock_class_mtx_spin }, 320 #endif 321 { NULL, NULL }, 322 { NULL, NULL } 323 }; 324 325 #ifdef BLESSING 326 /* 327 * Pairs of locks which have been blessed 328 * Don't complain about order problems with blessed locks 329 */ 330 static struct witness_blessed blessed_list[] = { 331 }; 332 static int blessed_count = 333 sizeof(blessed_list) / sizeof(struct witness_blessed); 334 #endif 335 336 /* 337 * List of all locks in the system. 338 */ 339 TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 340 341 static struct mtx all_mtx = { 342 { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 343 "All locks list", /* mtx_object.lo_name */ 344 "All locks list", /* mtx_object.lo_type */ 345 LO_INITIALIZED, /* mtx_object.lo_flags */ 346 { NULL, NULL }, /* mtx_object.lo_list */ 347 NULL }, /* mtx_object.lo_witness */ 348 MTX_UNOWNED, 0 /* mtx_lock, mtx_recurse */ 349 }; 350 351 /* 352 * This global is set to 0 once it becomes safe to use the witness code. 353 */ 354 static int witness_cold = 1; 355 356 /* 357 * Global variables for book keeping. 358 */ 359 static int lock_cur_cnt; 360 static int lock_max_cnt; 361 362 /* 363 * The WITNESS-enabled diagnostic code. 364 */ 365 static void 366 witness_initialize(void *dummy __unused) 367 { 368 struct lock_object *lock; 369 struct witness_order_list_entry *order; 370 struct witness *w, *w1; 371 int i; 372 373 /* 374 * We have to release Giant before initializing its witness 375 * structure so that WITNESS doesn't get confused. 376 */ 377 mtx_unlock(&Giant); 378 mtx_assert(&Giant, MA_NOTOWNED); 379 380 CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 381 TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 382 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 383 MTX_NOWITNESS); 384 for (i = 0; i < WITNESS_COUNT; i++) 385 witness_free(&w_data[i]); 386 for (i = 0; i < WITNESS_CHILDCOUNT; i++) 387 witness_child_free(&w_childdata[i]); 388 for (i = 0; i < LOCK_CHILDCOUNT; i++) 389 witness_lock_list_free(&w_locklistdata[i]); 390 391 /* First add in all the specified order lists. */ 392 for (order = order_lists; order->w_name != NULL; order++) { 393 w = enroll(order->w_name, order->w_class); 394 if (w == NULL) 395 continue; 396 w->w_file = "order list"; 397 for (order++; order->w_name != NULL; order++) { 398 w1 = enroll(order->w_name, order->w_class); 399 if (w1 == NULL) 400 continue; 401 w1->w_file = "order list"; 402 if (!itismychild(w, w1)) 403 panic("Not enough memory for static orders!"); 404 w = w1; 405 } 406 } 407 408 /* Iterate through all locks and add them to witness. */ 409 mtx_lock(&all_mtx); 410 TAILQ_FOREACH(lock, &all_locks, lo_list) { 411 if (lock->lo_flags & LO_WITNESS) 412 lock->lo_witness = enroll(lock->lo_type, 413 lock->lo_class); 414 else 415 lock->lo_witness = NULL; 416 } 417 mtx_unlock(&all_mtx); 418 419 /* Mark the witness code as being ready for use. */ 420 atomic_store_rel_int(&witness_cold, 0); 421 422 mtx_lock(&Giant); 423 } 424 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 425 426 static int 427 sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 428 { 429 int error, value; 430 431 value = witness_watch; 432 error = sysctl_handle_int(oidp, &value, 0, req); 433 if (error != 0 || req->newptr == NULL) 434 return (error); 435 error = suser(req->td); 436 if (error != 0) 437 return (error); 438 if (value == witness_watch) 439 return (0); 440 if (value != 0) 441 return (EINVAL); 442 witness_watch = 0; 443 return (0); 444 } 445 446 void 447 witness_init(struct lock_object *lock) 448 { 449 struct lock_class *class; 450 451 class = lock->lo_class; 452 if (lock->lo_flags & LO_INITIALIZED) 453 panic("%s: lock (%s) %s is already initialized", __func__, 454 class->lc_name, lock->lo_name); 455 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 456 (class->lc_flags & LC_RECURSABLE) == 0) 457 panic("%s: lock (%s) %s can not be recursable", __func__, 458 class->lc_name, lock->lo_name); 459 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 460 (class->lc_flags & LC_SLEEPABLE) == 0) 461 panic("%s: lock (%s) %s can not be sleepable", __func__, 462 class->lc_name, lock->lo_name); 463 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 464 (class->lc_flags & LC_UPGRADABLE) == 0) 465 panic("%s: lock (%s) %s can not be upgradable", __func__, 466 class->lc_name, lock->lo_name); 467 468 mtx_lock(&all_mtx); 469 TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 470 lock->lo_flags |= LO_INITIALIZED; 471 lock_cur_cnt++; 472 if (lock_cur_cnt > lock_max_cnt) 473 lock_max_cnt = lock_cur_cnt; 474 mtx_unlock(&all_mtx); 475 if (!witness_cold && witness_watch != 0 && panicstr == NULL && 476 (lock->lo_flags & LO_WITNESS) != 0) 477 lock->lo_witness = enroll(lock->lo_type, class); 478 else 479 lock->lo_witness = NULL; 480 } 481 482 void 483 witness_destroy(struct lock_object *lock) 484 { 485 struct witness *w; 486 487 if (witness_cold) 488 panic("lock (%s) %s destroyed while witness_cold", 489 lock->lo_class->lc_name, lock->lo_name); 490 if ((lock->lo_flags & LO_INITIALIZED) == 0) 491 panic("%s: lock (%s) %s is not initialized", __func__, 492 lock->lo_class->lc_name, lock->lo_name); 493 494 /* XXX: need to verify that no one holds the lock */ 495 w = lock->lo_witness; 496 if (w != NULL) { 497 mtx_lock_spin(&w_mtx); 498 MPASS(w->w_refcount > 0); 499 w->w_refcount--; 500 501 /* 502 * Lock is already released if we have an allocation failure 503 * and depart() fails. 504 */ 505 if (w->w_refcount != 0 || depart(w)) 506 mtx_unlock_spin(&w_mtx); 507 } 508 509 mtx_lock(&all_mtx); 510 lock_cur_cnt--; 511 TAILQ_REMOVE(&all_locks, lock, lo_list); 512 lock->lo_flags &= ~LO_INITIALIZED; 513 mtx_unlock(&all_mtx); 514 } 515 516 #ifdef DDB 517 static void 518 witness_display_list(void(*prnt)(const char *fmt, ...), 519 struct witness_list *list) 520 { 521 struct witness *w; 522 523 STAILQ_FOREACH(w, list, w_typelist) { 524 if (w->w_file == NULL || w->w_level > 0) 525 continue; 526 /* 527 * This lock has no anscestors, display its descendants. 528 */ 529 witness_displaydescendants(prnt, w, 0); 530 } 531 } 532 533 static void 534 witness_display(void(*prnt)(const char *fmt, ...)) 535 { 536 struct witness *w; 537 538 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 539 witness_levelall(); 540 541 /* Clear all the displayed flags. */ 542 STAILQ_FOREACH(w, &w_all, w_list) { 543 w->w_displayed = 0; 544 } 545 546 /* 547 * First, handle sleep locks which have been acquired at least 548 * once. 549 */ 550 prnt("Sleep locks:\n"); 551 witness_display_list(prnt, &w_sleep); 552 553 /* 554 * Now do spin locks which have been acquired at least once. 555 */ 556 prnt("\nSpin locks:\n"); 557 witness_display_list(prnt, &w_spin); 558 559 /* 560 * Finally, any locks which have not been acquired yet. 561 */ 562 prnt("\nLocks which were never acquired:\n"); 563 STAILQ_FOREACH(w, &w_all, w_list) { 564 if (w->w_file != NULL || w->w_refcount == 0) 565 continue; 566 prnt("%s\n", w->w_name); 567 } 568 } 569 #endif /* DDB */ 570 571 /* Trim useless garbage from filenames. */ 572 static const char * 573 fixup_filename(const char *file) 574 { 575 576 if (file == NULL) 577 return (NULL); 578 while (strncmp(file, "../", 3) == 0) 579 file += 3; 580 return (file); 581 } 582 583 int 584 witness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 585 { 586 587 if (witness_watch == 0 || panicstr != NULL) 588 return (0); 589 590 /* Require locks that witness knows about. */ 591 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 592 lock2->lo_witness == NULL) 593 return (EINVAL); 594 595 MPASS(!mtx_owned(&w_mtx)); 596 mtx_lock_spin(&w_mtx); 597 598 /* 599 * If we already have either an explicit or implied lock order that 600 * is the other way around, then return an error. 601 */ 602 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 603 mtx_unlock_spin(&w_mtx); 604 return (EDOOFUS); 605 } 606 607 /* Try to add the new order. */ 608 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 609 lock2->lo_type, lock1->lo_type); 610 if (!itismychild(lock1->lo_witness, lock2->lo_witness)) 611 return (ENOMEM); 612 mtx_unlock_spin(&w_mtx); 613 return (0); 614 } 615 616 void 617 witness_checkorder(struct lock_object *lock, int flags, const char *file, 618 int line) 619 { 620 struct lock_list_entry **lock_list, *lle; 621 struct lock_instance *lock1, *lock2; 622 struct lock_class *class; 623 struct witness *w, *w1; 624 struct thread *td; 625 int i, j; 626 627 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 628 panicstr != NULL) 629 return; 630 631 /* 632 * Try locks do not block if they fail to acquire the lock, thus 633 * there is no danger of deadlocks or of switching while holding a 634 * spin lock if we acquire a lock via a try operation. This 635 * function shouldn't even be called for try locks, so panic if 636 * that happens. 637 */ 638 if (flags & LOP_TRYLOCK) 639 panic("%s should not be called for try lock operations", 640 __func__); 641 642 w = lock->lo_witness; 643 class = lock->lo_class; 644 td = curthread; 645 file = fixup_filename(file); 646 647 if (class->lc_flags & LC_SLEEPLOCK) { 648 /* 649 * Since spin locks include a critical section, this check 650 * impliclty enforces a lock order of all sleep locks before 651 * all spin locks. 652 */ 653 if (td->td_critnest != 0) 654 panic("blockable sleep lock (%s) %s @ %s:%d", 655 class->lc_name, lock->lo_name, file, line); 656 lock_list = &td->td_sleeplocks; 657 } else 658 lock_list = PCPU_PTR(spinlocks); 659 660 /* 661 * Is this the first lock acquired? If so, then no order checking 662 * is needed. 663 */ 664 if (*lock_list == NULL) 665 return; 666 667 /* 668 * Check to see if we are recursing on a lock we already own. If 669 * so, make sure that we don't mismatch exclusive and shared lock 670 * acquires. 671 */ 672 lock1 = find_instance(*lock_list, lock); 673 if (lock1 != NULL) { 674 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 675 (flags & LOP_EXCLUSIVE) == 0) { 676 printf("shared lock of (%s) %s @ %s:%d\n", 677 class->lc_name, lock->lo_name, file, line); 678 printf("while exclusively locked from %s:%d\n", 679 lock1->li_file, lock1->li_line); 680 panic("share->excl"); 681 } 682 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 683 (flags & LOP_EXCLUSIVE) != 0) { 684 printf("exclusive lock of (%s) %s @ %s:%d\n", 685 class->lc_name, lock->lo_name, file, line); 686 printf("while share locked from %s:%d\n", 687 lock1->li_file, lock1->li_line); 688 panic("excl->share"); 689 } 690 return; 691 } 692 693 /* 694 * Try locks do not block if they fail to acquire the lock, thus 695 * there is no danger of deadlocks or of switching while holding a 696 * spin lock if we acquire a lock via a try operation. 697 */ 698 if (flags & LOP_TRYLOCK) 699 return; 700 701 /* 702 * Check for duplicate locks of the same type. Note that we only 703 * have to check for this on the last lock we just acquired. Any 704 * other cases will be caught as lock order violations. 705 */ 706 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 707 w1 = lock1->li_lock->lo_witness; 708 if (w1 == w) { 709 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK)) 710 return; 711 w->w_same_squawked = 1; 712 printf("acquiring duplicate lock of same type: \"%s\"\n", 713 lock->lo_type); 714 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 715 lock1->li_file, lock1->li_line); 716 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 717 #ifdef DDB 718 goto debugger; 719 #else 720 return; 721 #endif 722 } 723 MPASS(!mtx_owned(&w_mtx)); 724 mtx_lock_spin(&w_mtx); 725 /* 726 * If we have a known higher number just say ok 727 */ 728 if (witness_watch > 1 && w->w_level > w1->w_level) { 729 mtx_unlock_spin(&w_mtx); 730 return; 731 } 732 /* 733 * If we know that the the lock we are acquiring comes after 734 * the lock we most recently acquired in the lock order tree, 735 * then there is no need for any further checks. 736 */ 737 if (isitmydescendant(w1, w)) { 738 mtx_unlock_spin(&w_mtx); 739 return; 740 } 741 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 742 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 743 744 MPASS(j < WITNESS_COUNT); 745 lock1 = &lle->ll_children[i]; 746 w1 = lock1->li_lock->lo_witness; 747 748 /* 749 * If this lock doesn't undergo witness checking, 750 * then skip it. 751 */ 752 if (w1 == NULL) { 753 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 754 ("lock missing witness structure")); 755 continue; 756 } 757 /* 758 * If we are locking Giant and this is a sleepable 759 * lock, then skip it. 760 */ 761 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 762 lock == &Giant.mtx_object) 763 continue; 764 /* 765 * If we are locking a sleepable lock and this lock 766 * is Giant, then skip it. 767 */ 768 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 769 lock1->li_lock == &Giant.mtx_object) 770 continue; 771 /* 772 * If we are locking a sleepable lock and this lock 773 * isn't sleepable, we want to treat it as a lock 774 * order violation to enfore a general lock order of 775 * sleepable locks before non-sleepable locks. 776 */ 777 if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 778 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 779 /* 780 * Check the lock order hierarchy for a reveresal. 781 */ 782 if (!isitmydescendant(w, w1)) 783 continue; 784 /* 785 * We have a lock order violation, check to see if it 786 * is allowed or has already been yelled about. 787 */ 788 mtx_unlock_spin(&w_mtx); 789 #ifdef BLESSING 790 /* 791 * If the lock order is blessed, just bail. We don't 792 * look for other lock order violations though, which 793 * may be a bug. 794 */ 795 if (blessed(w, w1)) 796 return; 797 #endif 798 if (lock1->li_lock == &Giant.mtx_object) { 799 if (w1->w_Giant_squawked) 800 return; 801 else 802 w1->w_Giant_squawked = 1; 803 } else { 804 if (w1->w_other_squawked) 805 return; 806 else 807 w1->w_other_squawked = 1; 808 } 809 /* 810 * Ok, yell about it. 811 */ 812 printf("lock order reversal\n"); 813 /* 814 * Try to locate an earlier lock with 815 * witness w in our list. 816 */ 817 do { 818 lock2 = &lle->ll_children[i]; 819 MPASS(lock2->li_lock != NULL); 820 if (lock2->li_lock->lo_witness == w) 821 break; 822 if (i == 0 && lle->ll_next != NULL) { 823 lle = lle->ll_next; 824 i = lle->ll_count - 1; 825 MPASS(i >= 0 && i < LOCK_NCHILDREN); 826 } else 827 i--; 828 } while (i >= 0); 829 if (i < 0) { 830 printf(" 1st %p %s (%s) @ %s:%d\n", 831 lock1->li_lock, lock1->li_lock->lo_name, 832 lock1->li_lock->lo_type, lock1->li_file, 833 lock1->li_line); 834 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 835 lock->lo_name, lock->lo_type, file, line); 836 } else { 837 printf(" 1st %p %s (%s) @ %s:%d\n", 838 lock2->li_lock, lock2->li_lock->lo_name, 839 lock2->li_lock->lo_type, lock2->li_file, 840 lock2->li_line); 841 printf(" 2nd %p %s (%s) @ %s:%d\n", 842 lock1->li_lock, lock1->li_lock->lo_name, 843 lock1->li_lock->lo_type, lock1->li_file, 844 lock1->li_line); 845 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 846 lock->lo_name, lock->lo_type, file, line); 847 } 848 #ifdef DDB 849 goto debugger; 850 #else 851 return; 852 #endif 853 } 854 } 855 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 856 /* 857 * If requested, build a new lock order. However, don't build a new 858 * relationship between a sleepable lock and Giant if it is in the 859 * wrong direction. The correct lock order is that sleepable locks 860 * always come before Giant. 861 */ 862 if (flags & LOP_NEWORDER && 863 !(lock1->li_lock == &Giant.mtx_object && 864 (lock->lo_flags & LO_SLEEPABLE) != 0)) { 865 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 866 lock->lo_type, lock1->li_lock->lo_type); 867 if (!itismychild(lock1->li_lock->lo_witness, w)) 868 /* Witness is dead. */ 869 return; 870 } 871 mtx_unlock_spin(&w_mtx); 872 return; 873 874 #ifdef DDB 875 debugger: 876 if (witness_trace) 877 backtrace(); 878 if (witness_ddb) 879 Debugger(__func__); 880 #endif 881 } 882 883 void 884 witness_lock(struct lock_object *lock, int flags, const char *file, int line) 885 { 886 struct lock_list_entry **lock_list, *lle; 887 struct lock_instance *instance; 888 struct witness *w; 889 struct thread *td; 890 891 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 892 panicstr != NULL) 893 return; 894 w = lock->lo_witness; 895 td = curthread; 896 file = fixup_filename(file); 897 898 /* Determine lock list for this lock. */ 899 if (lock->lo_class->lc_flags & LC_SLEEPLOCK) 900 lock_list = &td->td_sleeplocks; 901 else 902 lock_list = PCPU_PTR(spinlocks); 903 904 /* Check to see if we are recursing on a lock we already own. */ 905 instance = find_instance(*lock_list, lock); 906 if (instance != NULL) { 907 instance->li_flags++; 908 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 909 td->td_proc->p_pid, lock->lo_name, 910 instance->li_flags & LI_RECURSEMASK); 911 instance->li_file = file; 912 instance->li_line = line; 913 return; 914 } 915 916 /* Update per-witness last file and line acquire. */ 917 w->w_file = file; 918 w->w_line = line; 919 920 /* Find the next open lock instance in the list and fill it. */ 921 lle = *lock_list; 922 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 923 lle = witness_lock_list_get(); 924 if (lle == NULL) 925 return; 926 lle->ll_next = *lock_list; 927 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 928 td->td_proc->p_pid, lle); 929 *lock_list = lle; 930 } 931 instance = &lle->ll_children[lle->ll_count++]; 932 instance->li_lock = lock; 933 instance->li_line = line; 934 instance->li_file = file; 935 if ((flags & LOP_EXCLUSIVE) != 0) 936 instance->li_flags = LI_EXCLUSIVE; 937 else 938 instance->li_flags = 0; 939 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 940 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 941 } 942 943 void 944 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 945 { 946 struct lock_instance *instance; 947 struct lock_class *class; 948 949 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 950 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 951 return; 952 class = lock->lo_class; 953 file = fixup_filename(file); 954 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 955 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 956 class->lc_name, lock->lo_name, file, line); 957 if ((flags & LOP_TRYLOCK) == 0) 958 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 959 lock->lo_name, file, line); 960 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 961 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 962 class->lc_name, lock->lo_name, file, line); 963 instance = find_instance(curthread->td_sleeplocks, lock); 964 if (instance == NULL) 965 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 966 class->lc_name, lock->lo_name, file, line); 967 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 968 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 969 class->lc_name, lock->lo_name, file, line); 970 if ((instance->li_flags & LI_RECURSEMASK) != 0) 971 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 972 class->lc_name, lock->lo_name, 973 instance->li_flags & LI_RECURSEMASK, file, line); 974 instance->li_flags |= LI_EXCLUSIVE; 975 } 976 977 void 978 witness_downgrade(struct lock_object *lock, int flags, const char *file, 979 int line) 980 { 981 struct lock_instance *instance; 982 struct lock_class *class; 983 984 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 985 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 986 return; 987 class = lock->lo_class; 988 file = fixup_filename(file); 989 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 990 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 991 class->lc_name, lock->lo_name, file, line); 992 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 993 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 994 class->lc_name, lock->lo_name, file, line); 995 instance = find_instance(curthread->td_sleeplocks, lock); 996 if (instance == NULL) 997 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 998 class->lc_name, lock->lo_name, file, line); 999 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1000 panic("downgrade of shared lock (%s) %s @ %s:%d", 1001 class->lc_name, lock->lo_name, file, line); 1002 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1003 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1004 class->lc_name, lock->lo_name, 1005 instance->li_flags & LI_RECURSEMASK, file, line); 1006 instance->li_flags &= ~LI_EXCLUSIVE; 1007 } 1008 1009 void 1010 witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 1011 { 1012 struct lock_list_entry **lock_list, *lle; 1013 struct lock_instance *instance; 1014 struct lock_class *class; 1015 struct thread *td; 1016 register_t s; 1017 int i, j; 1018 1019 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1020 panicstr != NULL) 1021 return; 1022 td = curthread; 1023 class = lock->lo_class; 1024 file = fixup_filename(file); 1025 1026 /* Find lock instance associated with this lock. */ 1027 if (class->lc_flags & LC_SLEEPLOCK) 1028 lock_list = &td->td_sleeplocks; 1029 else 1030 lock_list = PCPU_PTR(spinlocks); 1031 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 1032 for (i = 0; i < (*lock_list)->ll_count; i++) { 1033 instance = &(*lock_list)->ll_children[i]; 1034 if (instance->li_lock == lock) 1035 goto found; 1036 } 1037 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 1038 file, line); 1039 found: 1040 1041 /* First, check for shared/exclusive mismatches. */ 1042 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 1043 (flags & LOP_EXCLUSIVE) == 0) { 1044 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1045 lock->lo_name, file, line); 1046 printf("while exclusively locked from %s:%d\n", 1047 instance->li_file, instance->li_line); 1048 panic("excl->ushare"); 1049 } 1050 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 1051 (flags & LOP_EXCLUSIVE) != 0) { 1052 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1053 lock->lo_name, file, line); 1054 printf("while share locked from %s:%d\n", instance->li_file, 1055 instance->li_line); 1056 panic("share->uexcl"); 1057 } 1058 1059 /* If we are recursed, unrecurse. */ 1060 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1061 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1062 td->td_proc->p_pid, instance->li_lock->lo_name, 1063 instance->li_flags); 1064 instance->li_flags--; 1065 return; 1066 } 1067 1068 /* Otherwise, remove this item from the list. */ 1069 s = intr_disable(); 1070 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1071 td->td_proc->p_pid, instance->li_lock->lo_name, 1072 (*lock_list)->ll_count - 1); 1073 for (j = i; j < (*lock_list)->ll_count - 1; j++) 1074 (*lock_list)->ll_children[j] = 1075 (*lock_list)->ll_children[j + 1]; 1076 (*lock_list)->ll_count--; 1077 intr_restore(s); 1078 1079 /* If this lock list entry is now empty, free it. */ 1080 if ((*lock_list)->ll_count == 0) { 1081 lle = *lock_list; 1082 *lock_list = lle->ll_next; 1083 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1084 td->td_proc->p_pid, lle); 1085 witness_lock_list_free(lle); 1086 } 1087 } 1088 1089 /* 1090 * Warn if any locks other than 'lock' are held. Flags can be passed in to 1091 * exempt Giant and sleepable locks from the checks as well. If any 1092 * non-exempt locks are held, then a supplied message is printed to the 1093 * console along with a list of the offending locks. If indicated in the 1094 * flags then a failure results in a panic as well. 1095 */ 1096 int 1097 witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 1098 { 1099 struct lock_list_entry *lle; 1100 struct lock_instance *lock1; 1101 struct thread *td; 1102 va_list ap; 1103 int i, n; 1104 1105 if (witness_cold || witness_watch == 0 || panicstr != NULL) 1106 return (0); 1107 n = 0; 1108 td = curthread; 1109 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 1110 for (i = lle->ll_count - 1; i >= 0; i--) { 1111 lock1 = &lle->ll_children[i]; 1112 if (lock1->li_lock == lock) 1113 continue; 1114 if (flags & WARN_GIANTOK && 1115 lock1->li_lock == &Giant.mtx_object) 1116 continue; 1117 if (flags & WARN_SLEEPOK && 1118 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 1119 continue; 1120 if (n == 0) { 1121 va_start(ap, fmt); 1122 vprintf(fmt, ap); 1123 va_end(ap); 1124 printf(" with the following"); 1125 if (flags & WARN_SLEEPOK) 1126 printf(" non-sleepable"); 1127 printf(" locks held:\n"); 1128 } 1129 n++; 1130 witness_list_lock(lock1); 1131 } 1132 if (PCPU_GET(spinlocks) != NULL) { 1133 /* 1134 * Since we already hold a spinlock preemption is 1135 * already blocked. 1136 */ 1137 if (n == 0) { 1138 va_start(ap, fmt); 1139 vprintf(fmt, ap); 1140 va_end(ap); 1141 printf(" with the following"); 1142 if (flags & WARN_SLEEPOK) 1143 printf(" non-sleepable"); 1144 printf(" locks held:\n"); 1145 } 1146 n += witness_list_locks(PCPU_PTR(spinlocks)); 1147 } 1148 if (flags & WARN_PANIC && n) 1149 panic("witness_warn"); 1150 #ifdef DDB 1151 else if (witness_ddb && n) 1152 Debugger(__func__); 1153 else if (witness_trace && n) 1154 backtrace(); 1155 #endif 1156 return (n); 1157 } 1158 1159 const char * 1160 witness_file(struct lock_object *lock) 1161 { 1162 struct witness *w; 1163 1164 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1165 return ("?"); 1166 w = lock->lo_witness; 1167 return (w->w_file); 1168 } 1169 1170 int 1171 witness_line(struct lock_object *lock) 1172 { 1173 struct witness *w; 1174 1175 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1176 return (0); 1177 w = lock->lo_witness; 1178 return (w->w_line); 1179 } 1180 1181 static struct witness * 1182 enroll(const char *description, struct lock_class *lock_class) 1183 { 1184 struct witness *w; 1185 1186 if (witness_watch == 0 || panicstr != NULL) 1187 return (NULL); 1188 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 1189 return (NULL); 1190 mtx_lock_spin(&w_mtx); 1191 STAILQ_FOREACH(w, &w_all, w_list) { 1192 if (w->w_name == description || (w->w_refcount > 0 && 1193 strcmp(description, w->w_name) == 0)) { 1194 w->w_refcount++; 1195 mtx_unlock_spin(&w_mtx); 1196 if (lock_class != w->w_class) 1197 panic( 1198 "lock (%s) %s does not match earlier (%s) lock", 1199 description, lock_class->lc_name, 1200 w->w_class->lc_name); 1201 return (w); 1202 } 1203 } 1204 /* 1205 * This isn't quite right, as witness_cold is still 0 while we 1206 * enroll all the locks initialized before witness_initialize(). 1207 */ 1208 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 1209 mtx_unlock_spin(&w_mtx); 1210 panic("spin lock %s not in order list", description); 1211 } 1212 if ((w = witness_get()) == NULL) 1213 return (NULL); 1214 w->w_name = description; 1215 w->w_class = lock_class; 1216 w->w_refcount = 1; 1217 STAILQ_INSERT_HEAD(&w_all, w, w_list); 1218 if (lock_class->lc_flags & LC_SPINLOCK) 1219 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1220 else if (lock_class->lc_flags & LC_SLEEPLOCK) 1221 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1222 else { 1223 mtx_unlock_spin(&w_mtx); 1224 panic("lock class %s is not sleep or spin", 1225 lock_class->lc_name); 1226 } 1227 mtx_unlock_spin(&w_mtx); 1228 return (w); 1229 } 1230 1231 /* Don't let the door bang you on the way out... */ 1232 static int 1233 depart(struct witness *w) 1234 { 1235 struct witness_child_list_entry *wcl, *nwcl; 1236 struct witness_list *list; 1237 struct witness *parent; 1238 1239 MPASS(w->w_refcount == 0); 1240 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1241 list = &w_sleep; 1242 else 1243 list = &w_spin; 1244 /* 1245 * First, we run through the entire tree looking for any 1246 * witnesses that the outgoing witness is a child of. For 1247 * each parent that we find, we reparent all the direct 1248 * children of the outgoing witness to its parent. 1249 */ 1250 STAILQ_FOREACH(parent, list, w_typelist) { 1251 if (!isitmychild(parent, w)) 1252 continue; 1253 removechild(parent, w); 1254 if (!reparentchildren(parent, w)) 1255 return (0); 1256 } 1257 1258 /* 1259 * Now we go through and free up the child list of the 1260 * outgoing witness. 1261 */ 1262 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1263 nwcl = wcl->wcl_next; 1264 witness_child_free(wcl); 1265 } 1266 1267 /* 1268 * Detach from various lists and free. 1269 */ 1270 STAILQ_REMOVE(list, w, witness, w_typelist); 1271 STAILQ_REMOVE(&w_all, w, witness, w_list); 1272 witness_free(w); 1273 1274 /* Finally, fixup the tree. */ 1275 return (rebalancetree(list)); 1276 } 1277 1278 /* 1279 * Prune an entire lock order tree. We look for cases where a lock 1280 * is now both a descendant and a direct child of a given lock. In 1281 * that case, we want to remove the direct child link from the tree. 1282 * 1283 * Returns false if insertchild() fails. 1284 */ 1285 static int 1286 rebalancetree(struct witness_list *list) 1287 { 1288 struct witness *child, *parent; 1289 1290 STAILQ_FOREACH(child, list, w_typelist) { 1291 STAILQ_FOREACH(parent, list, w_typelist) { 1292 if (!isitmychild(parent, child)) 1293 continue; 1294 removechild(parent, child); 1295 if (isitmydescendant(parent, child)) 1296 continue; 1297 if (!insertchild(parent, child)) 1298 return (0); 1299 } 1300 } 1301 witness_levelall(); 1302 return (1); 1303 } 1304 1305 /* 1306 * Add "child" as a direct child of "parent". Returns false if 1307 * we fail due to out of memory. 1308 */ 1309 static int 1310 insertchild(struct witness *parent, struct witness *child) 1311 { 1312 struct witness_child_list_entry **wcl; 1313 1314 MPASS(child != NULL && parent != NULL); 1315 1316 /* 1317 * Insert "child" after "parent" 1318 */ 1319 wcl = &parent->w_children; 1320 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 1321 wcl = &(*wcl)->wcl_next; 1322 if (*wcl == NULL) { 1323 *wcl = witness_child_get(); 1324 if (*wcl == NULL) 1325 return (0); 1326 } 1327 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1328 1329 return (1); 1330 } 1331 1332 /* 1333 * Make all the direct descendants of oldparent be direct descendants 1334 * of newparent. 1335 */ 1336 static int 1337 reparentchildren(struct witness *newparent, struct witness *oldparent) 1338 { 1339 struct witness_child_list_entry *wcl; 1340 int i; 1341 1342 /* Avoid making a witness a child of itself. */ 1343 MPASS(!isitmychild(oldparent, newparent)); 1344 1345 for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1346 for (i = 0; i < wcl->wcl_count; i++) 1347 if (!insertchild(newparent, wcl->wcl_children[i])) 1348 return (0); 1349 return (1); 1350 } 1351 1352 static int 1353 itismychild(struct witness *parent, struct witness *child) 1354 { 1355 struct witness_list *list; 1356 1357 MPASS(child != NULL && parent != NULL); 1358 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1359 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1360 panic( 1361 "%s: parent (%s) and child (%s) are not the same lock type", 1362 __func__, parent->w_class->lc_name, 1363 child->w_class->lc_name); 1364 1365 if (!insertchild(parent, child)) 1366 return (0); 1367 1368 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1369 list = &w_sleep; 1370 else 1371 list = &w_spin; 1372 return (rebalancetree(list)); 1373 } 1374 1375 static void 1376 removechild(struct witness *parent, struct witness *child) 1377 { 1378 struct witness_child_list_entry **wcl, *wcl1; 1379 int i; 1380 1381 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1382 for (i = 0; i < (*wcl)->wcl_count; i++) 1383 if ((*wcl)->wcl_children[i] == child) 1384 goto found; 1385 return; 1386 found: 1387 (*wcl)->wcl_count--; 1388 if ((*wcl)->wcl_count > i) 1389 (*wcl)->wcl_children[i] = 1390 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1391 MPASS((*wcl)->wcl_children[i] != NULL); 1392 if ((*wcl)->wcl_count != 0) 1393 return; 1394 wcl1 = *wcl; 1395 *wcl = wcl1->wcl_next; 1396 witness_child_free(wcl1); 1397 } 1398 1399 static int 1400 isitmychild(struct witness *parent, struct witness *child) 1401 { 1402 struct witness_child_list_entry *wcl; 1403 int i; 1404 1405 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1406 for (i = 0; i < wcl->wcl_count; i++) { 1407 if (wcl->wcl_children[i] == child) 1408 return (1); 1409 } 1410 } 1411 return (0); 1412 } 1413 1414 static int 1415 isitmydescendant(struct witness *parent, struct witness *child) 1416 { 1417 struct witness_child_list_entry *wcl; 1418 int i, j; 1419 1420 if (isitmychild(parent, child)) 1421 return (1); 1422 j = 0; 1423 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1424 MPASS(j < 1000); 1425 for (i = 0; i < wcl->wcl_count; i++) { 1426 if (isitmydescendant(wcl->wcl_children[i], child)) 1427 return (1); 1428 } 1429 j++; 1430 } 1431 return (0); 1432 } 1433 1434 static void 1435 witness_levelall (void) 1436 { 1437 struct witness_list *list; 1438 struct witness *w, *w1; 1439 1440 /* 1441 * First clear all levels. 1442 */ 1443 STAILQ_FOREACH(w, &w_all, w_list) { 1444 w->w_level = 0; 1445 } 1446 1447 /* 1448 * Look for locks with no parent and level all their descendants. 1449 */ 1450 STAILQ_FOREACH(w, &w_all, w_list) { 1451 /* 1452 * This is just an optimization, technically we could get 1453 * away just walking the all list each time. 1454 */ 1455 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1456 list = &w_sleep; 1457 else 1458 list = &w_spin; 1459 STAILQ_FOREACH(w1, list, w_typelist) { 1460 if (isitmychild(w1, w)) 1461 goto skip; 1462 } 1463 witness_leveldescendents(w, 0); 1464 skip: 1465 ; /* silence GCC 3.x */ 1466 } 1467 } 1468 1469 static void 1470 witness_leveldescendents(struct witness *parent, int level) 1471 { 1472 struct witness_child_list_entry *wcl; 1473 int i; 1474 1475 if (parent->w_level < level) 1476 parent->w_level = level; 1477 level++; 1478 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1479 for (i = 0; i < wcl->wcl_count; i++) 1480 witness_leveldescendents(wcl->wcl_children[i], level); 1481 } 1482 1483 static void 1484 witness_displaydescendants(void(*prnt)(const char *fmt, ...), 1485 struct witness *parent, int indent) 1486 { 1487 struct witness_child_list_entry *wcl; 1488 int i, level; 1489 1490 level = parent->w_level; 1491 prnt("%-2d", level); 1492 for (i = 0; i < indent; i++) 1493 prnt(" "); 1494 if (parent->w_refcount > 0) 1495 prnt("%s", parent->w_name); 1496 else 1497 prnt("(dead)"); 1498 if (parent->w_displayed) { 1499 prnt(" -- (already displayed)\n"); 1500 return; 1501 } 1502 parent->w_displayed = 1; 1503 if (parent->w_refcount > 0) { 1504 if (parent->w_file != NULL) 1505 prnt(" -- last acquired @ %s:%d", parent->w_file, 1506 parent->w_line); 1507 } 1508 prnt("\n"); 1509 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1510 for (i = 0; i < wcl->wcl_count; i++) 1511 witness_displaydescendants(prnt, 1512 wcl->wcl_children[i], indent + 1); 1513 } 1514 1515 #ifdef BLESSING 1516 static int 1517 blessed(struct witness *w1, struct witness *w2) 1518 { 1519 int i; 1520 struct witness_blessed *b; 1521 1522 for (i = 0; i < blessed_count; i++) { 1523 b = &blessed_list[i]; 1524 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1525 if (strcmp(w2->w_name, b->b_lock2) == 0) 1526 return (1); 1527 continue; 1528 } 1529 if (strcmp(w1->w_name, b->b_lock2) == 0) 1530 if (strcmp(w2->w_name, b->b_lock1) == 0) 1531 return (1); 1532 } 1533 return (0); 1534 } 1535 #endif 1536 1537 static struct witness * 1538 witness_get(void) 1539 { 1540 struct witness *w; 1541 1542 if (witness_watch == 0) { 1543 mtx_unlock_spin(&w_mtx); 1544 return (NULL); 1545 } 1546 if (STAILQ_EMPTY(&w_free)) { 1547 witness_watch = 0; 1548 mtx_unlock_spin(&w_mtx); 1549 printf("%s: witness exhausted\n", __func__); 1550 return (NULL); 1551 } 1552 w = STAILQ_FIRST(&w_free); 1553 STAILQ_REMOVE_HEAD(&w_free, w_list); 1554 bzero(w, sizeof(*w)); 1555 return (w); 1556 } 1557 1558 static void 1559 witness_free(struct witness *w) 1560 { 1561 1562 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1563 } 1564 1565 static struct witness_child_list_entry * 1566 witness_child_get(void) 1567 { 1568 struct witness_child_list_entry *wcl; 1569 1570 if (witness_watch == 0) { 1571 mtx_unlock_spin(&w_mtx); 1572 return (NULL); 1573 } 1574 wcl = w_child_free; 1575 if (wcl == NULL) { 1576 witness_watch = 0; 1577 mtx_unlock_spin(&w_mtx); 1578 printf("%s: witness exhausted\n", __func__); 1579 return (NULL); 1580 } 1581 w_child_free = wcl->wcl_next; 1582 bzero(wcl, sizeof(*wcl)); 1583 return (wcl); 1584 } 1585 1586 static void 1587 witness_child_free(struct witness_child_list_entry *wcl) 1588 { 1589 1590 wcl->wcl_next = w_child_free; 1591 w_child_free = wcl; 1592 } 1593 1594 static struct lock_list_entry * 1595 witness_lock_list_get(void) 1596 { 1597 struct lock_list_entry *lle; 1598 1599 if (witness_watch == 0) 1600 return (NULL); 1601 mtx_lock_spin(&w_mtx); 1602 lle = w_lock_list_free; 1603 if (lle == NULL) { 1604 witness_watch = 0; 1605 mtx_unlock_spin(&w_mtx); 1606 printf("%s: witness exhausted\n", __func__); 1607 return (NULL); 1608 } 1609 w_lock_list_free = lle->ll_next; 1610 mtx_unlock_spin(&w_mtx); 1611 bzero(lle, sizeof(*lle)); 1612 return (lle); 1613 } 1614 1615 static void 1616 witness_lock_list_free(struct lock_list_entry *lle) 1617 { 1618 1619 mtx_lock_spin(&w_mtx); 1620 lle->ll_next = w_lock_list_free; 1621 w_lock_list_free = lle; 1622 mtx_unlock_spin(&w_mtx); 1623 } 1624 1625 static struct lock_instance * 1626 find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1627 { 1628 struct lock_list_entry *lle; 1629 struct lock_instance *instance; 1630 int i; 1631 1632 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1633 for (i = lle->ll_count - 1; i >= 0; i--) { 1634 instance = &lle->ll_children[i]; 1635 if (instance->li_lock == lock) 1636 return (instance); 1637 } 1638 return (NULL); 1639 } 1640 1641 static void 1642 witness_list_lock(struct lock_instance *instance) 1643 { 1644 struct lock_object *lock; 1645 1646 lock = instance->li_lock; 1647 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1648 "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name); 1649 if (lock->lo_type != lock->lo_name) 1650 printf(" (%s)", lock->lo_type); 1651 printf(" r = %d (%p) locked @ %s:%d\n", 1652 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1653 instance->li_line); 1654 } 1655 1656 int 1657 witness_list_locks(struct lock_list_entry **lock_list) 1658 { 1659 struct lock_list_entry *lle; 1660 int i, nheld; 1661 1662 nheld = 0; 1663 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1664 for (i = lle->ll_count - 1; i >= 0; i--) { 1665 witness_list_lock(&lle->ll_children[i]); 1666 nheld++; 1667 } 1668 return (nheld); 1669 } 1670 1671 /* 1672 * This is a bit risky at best. We call this function when we have timed 1673 * out acquiring a spin lock, and we assume that the other CPU is stuck 1674 * with this lock held. So, we go groveling around in the other CPU's 1675 * per-cpu data to try to find the lock instance for this spin lock to 1676 * see when it was last acquired. 1677 */ 1678 void 1679 witness_display_spinlock(struct lock_object *lock, struct thread *owner) 1680 { 1681 struct lock_instance *instance; 1682 struct pcpu *pc; 1683 1684 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 1685 return; 1686 pc = pcpu_find(owner->td_oncpu); 1687 instance = find_instance(pc->pc_spinlocks, lock); 1688 if (instance != NULL) 1689 witness_list_lock(instance); 1690 } 1691 1692 void 1693 witness_save(struct lock_object *lock, const char **filep, int *linep) 1694 { 1695 struct lock_instance *instance; 1696 1697 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1698 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1699 return; 1700 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1701 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1702 lock->lo_class->lc_name, lock->lo_name); 1703 instance = find_instance(curthread->td_sleeplocks, lock); 1704 if (instance == NULL) 1705 panic("%s: lock (%s) %s not locked", __func__, 1706 lock->lo_class->lc_name, lock->lo_name); 1707 *filep = instance->li_file; 1708 *linep = instance->li_line; 1709 } 1710 1711 void 1712 witness_restore(struct lock_object *lock, const char *file, int line) 1713 { 1714 struct lock_instance *instance; 1715 1716 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1717 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1718 return; 1719 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1720 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1721 lock->lo_class->lc_name, lock->lo_name); 1722 instance = find_instance(curthread->td_sleeplocks, lock); 1723 if (instance == NULL) 1724 panic("%s: lock (%s) %s not locked", __func__, 1725 lock->lo_class->lc_name, lock->lo_name); 1726 lock->lo_witness->w_file = file; 1727 lock->lo_witness->w_line = line; 1728 instance->li_file = file; 1729 instance->li_line = line; 1730 } 1731 1732 void 1733 witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1734 { 1735 #ifdef INVARIANT_SUPPORT 1736 struct lock_instance *instance; 1737 1738 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1739 return; 1740 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 1741 instance = find_instance(curthread->td_sleeplocks, lock); 1742 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 1743 instance = find_instance(PCPU_GET(spinlocks), lock); 1744 else { 1745 panic("Lock (%s) %s is not sleep or spin!", 1746 lock->lo_class->lc_name, lock->lo_name); 1747 } 1748 file = fixup_filename(file); 1749 switch (flags) { 1750 case LA_UNLOCKED: 1751 if (instance != NULL) 1752 panic("Lock (%s) %s locked @ %s:%d.", 1753 lock->lo_class->lc_name, lock->lo_name, file, line); 1754 break; 1755 case LA_LOCKED: 1756 case LA_LOCKED | LA_RECURSED: 1757 case LA_LOCKED | LA_NOTRECURSED: 1758 case LA_SLOCKED: 1759 case LA_SLOCKED | LA_RECURSED: 1760 case LA_SLOCKED | LA_NOTRECURSED: 1761 case LA_XLOCKED: 1762 case LA_XLOCKED | LA_RECURSED: 1763 case LA_XLOCKED | LA_NOTRECURSED: 1764 if (instance == NULL) { 1765 panic("Lock (%s) %s not locked @ %s:%d.", 1766 lock->lo_class->lc_name, lock->lo_name, file, line); 1767 break; 1768 } 1769 if ((flags & LA_XLOCKED) != 0 && 1770 (instance->li_flags & LI_EXCLUSIVE) == 0) 1771 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1772 lock->lo_class->lc_name, lock->lo_name, file, line); 1773 if ((flags & LA_SLOCKED) != 0 && 1774 (instance->li_flags & LI_EXCLUSIVE) != 0) 1775 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1776 lock->lo_class->lc_name, lock->lo_name, file, line); 1777 if ((flags & LA_RECURSED) != 0 && 1778 (instance->li_flags & LI_RECURSEMASK) == 0) 1779 panic("Lock (%s) %s not recursed @ %s:%d.", 1780 lock->lo_class->lc_name, lock->lo_name, file, line); 1781 if ((flags & LA_NOTRECURSED) != 0 && 1782 (instance->li_flags & LI_RECURSEMASK) != 0) 1783 panic("Lock (%s) %s recursed @ %s:%d.", 1784 lock->lo_class->lc_name, lock->lo_name, file, line); 1785 break; 1786 default: 1787 panic("Invalid lock assertion at %s:%d.", file, line); 1788 1789 } 1790 #endif /* INVARIANT_SUPPORT */ 1791 } 1792 1793 #ifdef DDB 1794 static void 1795 witness_list(struct thread *td) 1796 { 1797 1798 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1799 KASSERT(db_active, ("%s: not in the debugger", __func__)); 1800 1801 if (witness_watch == 0) 1802 return; 1803 1804 witness_list_locks(&td->td_sleeplocks); 1805 1806 /* 1807 * We only handle spinlocks if td == curthread. This is somewhat broken 1808 * if td is currently executing on some other CPU and holds spin locks 1809 * as we won't display those locks. If we had a MI way of getting 1810 * the per-cpu data for a given cpu then we could use 1811 * td->td_oncpu to get the list of spinlocks for this thread 1812 * and "fix" this. 1813 * 1814 * That still wouldn't really fix this unless we locked sched_lock 1815 * or stopped the other CPU to make sure it wasn't changing the list 1816 * out from under us. It is probably best to just not try to handle 1817 * threads on other CPU's for now. 1818 */ 1819 if (td == curthread && PCPU_GET(spinlocks) != NULL) 1820 witness_list_locks(PCPU_PTR(spinlocks)); 1821 } 1822 1823 DB_SHOW_COMMAND(locks, db_witness_list) 1824 { 1825 struct thread *td; 1826 pid_t pid; 1827 struct proc *p; 1828 1829 if (have_addr) { 1830 pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 1831 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 1832 ((addr >> 16) % 16) * 10000; 1833 /* sx_slock(&allproc_lock); */ 1834 FOREACH_PROC_IN_SYSTEM(p) { 1835 if (p->p_pid == pid) 1836 break; 1837 } 1838 /* sx_sunlock(&allproc_lock); */ 1839 if (p == NULL) { 1840 db_printf("pid %d not found\n", pid); 1841 return; 1842 } 1843 FOREACH_THREAD_IN_PROC(p, td) { 1844 witness_list(td); 1845 } 1846 } else { 1847 td = curthread; 1848 witness_list(td); 1849 } 1850 } 1851 1852 DB_SHOW_COMMAND(witness, db_witness_display) 1853 { 1854 1855 witness_display(db_printf); 1856 } 1857 #endif 1858