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 * $FreeBSD$ 31 */ 32 33 /* 34 * Implementation of the `witness' lock verifier. Originally implemented for 35 * mutexes in BSD/OS. Extended to handle generic lock objects and lock 36 * classes in FreeBSD. 37 */ 38 39 /* 40 * Main Entry: witness 41 * Pronunciation: 'wit-n&s 42 * Function: noun 43 * Etymology: Middle English witnesse, from Old English witnes knowledge, 44 * testimony, witness, from 2wit 45 * Date: before 12th century 46 * 1 : attestation of a fact or event : TESTIMONY 47 * 2 : one that gives evidence; specifically : one who testifies in 48 * a cause or before a judicial tribunal 49 * 3 : one asked to be present at a transaction so as to be able to 50 * testify to its having taken place 51 * 4 : one who has personal knowledge of something 52 * 5 a : something serving as evidence or proof : SIGN 53 * b : public affirmation by word or example of usually 54 * religious faith or conviction <the heroic witness to divine 55 * life -- Pilot> 56 * 6 capitalized : a member of the Jehovah's Witnesses 57 */ 58 59 #include "opt_ddb.h" 60 #include "opt_witness.h" 61 62 #include <sys/param.h> 63 #include <sys/bus.h> 64 #include <sys/kernel.h> 65 #include <sys/ktr.h> 66 #include <sys/lock.h> 67 #include <sys/malloc.h> 68 #include <sys/mutex.h> 69 #include <sys/proc.h> 70 #include <sys/sysctl.h> 71 #include <sys/systm.h> 72 73 #include <ddb/ddb.h> 74 75 #define WITNESS_COUNT 200 76 #define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 77 /* 78 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 79 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 80 * probably be safe for the most part, but it's still a SWAG. 81 */ 82 #define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 83 84 #define WITNESS_NCHILDREN 6 85 86 struct witness_child_list_entry; 87 88 struct witness { 89 const char *w_name; 90 struct lock_class *w_class; 91 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 92 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 93 struct witness_child_list_entry *w_children; /* Great evilness... */ 94 const char *w_file; 95 int w_line; 96 u_int w_level; 97 u_int w_refcount; 98 u_char w_Giant_squawked:1; 99 u_char w_other_squawked:1; 100 u_char w_same_squawked:1; 101 }; 102 103 struct witness_child_list_entry { 104 struct witness_child_list_entry *wcl_next; 105 struct witness *wcl_children[WITNESS_NCHILDREN]; 106 u_int wcl_count; 107 }; 108 109 STAILQ_HEAD(witness_list, witness); 110 111 struct witness_blessed { 112 const char *b_lock1; 113 const char *b_lock2; 114 }; 115 116 struct witness_order_list_entry { 117 const char *w_name; 118 struct lock_class *w_class; 119 }; 120 121 static struct witness *enroll(const char *description, 122 struct lock_class *lock_class); 123 static int itismychild(struct witness *parent, struct witness *child); 124 static void removechild(struct witness *parent, struct witness *child); 125 static int isitmychild(struct witness *parent, struct witness *child); 126 static int isitmydescendant(struct witness *parent, struct witness *child); 127 static int blessed(struct witness *, struct witness *); 128 static void witness_display_list(void(*prnt)(const char *fmt, ...), 129 struct witness_list *list); 130 static void witness_displaydescendants(void(*)(const char *fmt, ...), 131 struct witness *); 132 static void witness_leveldescendents(struct witness *parent, int level); 133 static void witness_levelall(void); 134 static struct witness *witness_get(void); 135 static void witness_free(struct witness *m); 136 static struct witness_child_list_entry *witness_child_get(void); 137 static void witness_child_free(struct witness_child_list_entry *wcl); 138 static struct lock_list_entry *witness_lock_list_get(void); 139 static void witness_lock_list_free(struct lock_list_entry *lle); 140 static void witness_display(void(*)(const char *fmt, ...)); 141 static struct lock_instance *find_instance(struct lock_list_entry *lock_list, 142 struct lock_object *lock); 143 144 MALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 145 146 static int witness_watch = 1; 147 TUNABLE_INT("debug.witness_watch", &witness_watch); 148 SYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 149 150 #ifdef DDB 151 /* 152 * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 153 * drop into kdebug() when: 154 * - a lock heirarchy violation occurs 155 * - locks are held when going to sleep. 156 */ 157 #ifdef WITNESS_DDB 158 int witness_ddb = 1; 159 #else 160 int witness_ddb = 0; 161 #endif 162 TUNABLE_INT("debug.witness_ddb", &witness_ddb); 163 SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 164 #endif /* DDB */ 165 166 #ifdef WITNESS_SKIPSPIN 167 int witness_skipspin = 1; 168 #else 169 int witness_skipspin = 0; 170 #endif 171 TUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 172 SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 173 ""); 174 175 static struct mtx w_mtx; 176 static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 177 static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 178 static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 179 static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 180 static struct witness_child_list_entry *w_child_free = NULL; 181 static struct lock_list_entry *w_lock_list_free = NULL; 182 static int witness_dead; /* fatal error, probably no memory */ 183 184 static struct witness w_data[WITNESS_COUNT]; 185 static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 186 static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 187 188 static struct witness_order_list_entry order_lists[] = { 189 { "Giant", &lock_class_mtx_sleep }, 190 { "proctree", &lock_class_sx }, 191 { "allproc", &lock_class_sx }, 192 { "sigio lock", &lock_class_mtx_sleep }, 193 { "process group", &lock_class_mtx_sleep }, 194 { "process lock", &lock_class_mtx_sleep }, 195 { "session", &lock_class_mtx_sleep }, 196 { "uidinfo hash", &lock_class_mtx_sleep }, 197 { "uidinfo struct", &lock_class_mtx_sleep }, 198 { NULL, NULL }, 199 /* 200 * spin locks 201 */ 202 #ifdef SMP 203 { "ap boot", &lock_class_mtx_spin }, 204 #ifdef __i386__ 205 { "com", &lock_class_mtx_spin }, 206 #endif 207 #endif 208 { "sio", &lock_class_mtx_spin }, 209 #ifdef __i386__ 210 { "cy", &lock_class_mtx_spin }, 211 #endif 212 { "ng_node", &lock_class_mtx_spin }, 213 { "ng_worklist", &lock_class_mtx_spin }, 214 { "ithread table lock", &lock_class_mtx_spin }, 215 { "sched lock", &lock_class_mtx_spin }, 216 { "callout", &lock_class_mtx_spin }, 217 /* 218 * leaf locks 219 */ 220 { "allpmaps", &lock_class_mtx_spin }, 221 { "vm page buckets mutex", &lock_class_mtx_spin }, 222 { "vm page queue free mutex", &lock_class_mtx_spin }, 223 { "icu", &lock_class_mtx_spin }, 224 #ifdef SMP 225 { "smp rendezvous", &lock_class_mtx_spin }, 226 #endif 227 { "clk", &lock_class_mtx_spin }, 228 { "mutex profiling lock", &lock_class_mtx_spin }, 229 { "zombie_thread_lock", &lock_class_mtx_spin }, 230 { NULL, NULL }, 231 { NULL, NULL } 232 }; 233 234 /* 235 * Pairs of locks which have been blessed 236 * Don't complain about order problems with blessed locks 237 */ 238 static struct witness_blessed blessed_list[] = { 239 }; 240 static int blessed_count = 241 sizeof(blessed_list) / sizeof(struct witness_blessed); 242 243 /* 244 * List of all locks in the system. 245 */ 246 TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 247 248 static struct mtx all_mtx = { 249 { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 250 "All locks list", /* mtx_object.lo_name */ 251 "All locks list", /* mtx_object.lo_type */ 252 LO_INITIALIZED, /* mtx_object.lo_flags */ 253 { NULL, NULL }, /* mtx_object.lo_list */ 254 NULL }, /* mtx_object.lo_witness */ 255 MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 256 TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 257 { NULL, NULL } /* mtx_contested */ 258 }; 259 260 /* 261 * This global is set to 0 once it becomes safe to use the witness code. 262 */ 263 static int witness_cold = 1; 264 265 /* 266 * Global variables for book keeping. 267 */ 268 static int lock_cur_cnt; 269 static int lock_max_cnt; 270 271 /* 272 * The WITNESS-enabled diagnostic code. 273 */ 274 static void 275 witness_initialize(void *dummy __unused) 276 { 277 struct lock_object *lock; 278 struct witness_order_list_entry *order; 279 struct witness *w, *w1; 280 int i; 281 282 /* 283 * We have to release Giant before initializing its witness 284 * structure so that WITNESS doesn't get confused. 285 */ 286 mtx_unlock(&Giant); 287 mtx_assert(&Giant, MA_NOTOWNED); 288 289 CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 290 TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 291 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 292 MTX_NOWITNESS); 293 for (i = 0; i < WITNESS_COUNT; i++) 294 witness_free(&w_data[i]); 295 for (i = 0; i < WITNESS_CHILDCOUNT; i++) 296 witness_child_free(&w_childdata[i]); 297 for (i = 0; i < LOCK_CHILDCOUNT; i++) 298 witness_lock_list_free(&w_locklistdata[i]); 299 300 /* First add in all the specified order lists. */ 301 for (order = order_lists; order->w_name != NULL; order++) { 302 w = enroll(order->w_name, order->w_class); 303 if (w == NULL) 304 continue; 305 w->w_file = "order list"; 306 for (order++; order->w_name != NULL; order++) { 307 w1 = enroll(order->w_name, order->w_class); 308 if (w1 == NULL) 309 continue; 310 w1->w_file = "order list"; 311 itismychild(w, w1); 312 w = w1; 313 } 314 } 315 316 /* Iterate through all locks and add them to witness. */ 317 mtx_lock(&all_mtx); 318 TAILQ_FOREACH(lock, &all_locks, lo_list) { 319 if (lock->lo_flags & LO_WITNESS) 320 lock->lo_witness = enroll(lock->lo_type, 321 lock->lo_class); 322 else 323 lock->lo_witness = NULL; 324 } 325 mtx_unlock(&all_mtx); 326 327 /* Mark the witness code as being ready for use. */ 328 atomic_store_rel_int(&witness_cold, 0); 329 330 mtx_lock(&Giant); 331 } 332 SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 333 334 void 335 witness_init(struct lock_object *lock) 336 { 337 struct lock_class *class; 338 339 class = lock->lo_class; 340 if (lock->lo_flags & LO_INITIALIZED) 341 panic("%s: lock (%s) %s is already initialized", __func__, 342 class->lc_name, lock->lo_name); 343 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 344 (class->lc_flags & LC_RECURSABLE) == 0) 345 panic("%s: lock (%s) %s can not be recursable", __func__, 346 class->lc_name, lock->lo_name); 347 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 348 (class->lc_flags & LC_SLEEPABLE) == 0) 349 panic("%s: lock (%s) %s can not be sleepable", __func__, 350 class->lc_name, lock->lo_name); 351 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 352 (class->lc_flags & LC_UPGRADABLE) == 0) 353 panic("%s: lock (%s) %s can not be upgradable", __func__, 354 class->lc_name, lock->lo_name); 355 356 mtx_lock(&all_mtx); 357 TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 358 lock->lo_flags |= LO_INITIALIZED; 359 lock_cur_cnt++; 360 if (lock_cur_cnt > lock_max_cnt) 361 lock_max_cnt = lock_cur_cnt; 362 mtx_unlock(&all_mtx); 363 if (!witness_cold && !witness_dead && panicstr == NULL && 364 (lock->lo_flags & LO_WITNESS) != 0) 365 lock->lo_witness = enroll(lock->lo_type, class); 366 else 367 lock->lo_witness = NULL; 368 } 369 370 void 371 witness_destroy(struct lock_object *lock) 372 { 373 struct witness *w; 374 375 if (witness_cold) 376 panic("lock (%s) %s destroyed while witness_cold", 377 lock->lo_class->lc_name, lock->lo_name); 378 if ((lock->lo_flags & LO_INITIALIZED) == 0) 379 panic("%s: lock (%s) %s is not initialized", __func__, 380 lock->lo_class->lc_name, lock->lo_name); 381 382 /* XXX: need to verify that no one holds the lock */ 383 w = lock->lo_witness; 384 if (w != NULL) { 385 mtx_lock_spin(&w_mtx); 386 MPASS(w->w_refcount > 0); 387 w->w_refcount--; 388 mtx_unlock_spin(&w_mtx); 389 } 390 391 mtx_lock(&all_mtx); 392 lock_cur_cnt--; 393 TAILQ_REMOVE(&all_locks, lock, lo_list); 394 lock->lo_flags &= ~LO_INITIALIZED; 395 mtx_unlock(&all_mtx); 396 } 397 398 static void 399 witness_display_list(void(*prnt)(const char *fmt, ...), 400 struct witness_list *list) 401 { 402 struct witness *w, *w1; 403 int found; 404 405 STAILQ_FOREACH(w, list, w_typelist) { 406 if (w->w_file == NULL) 407 continue; 408 found = 0; 409 STAILQ_FOREACH(w1, list, w_typelist) { 410 if (isitmychild(w1, w)) { 411 found++; 412 break; 413 } 414 } 415 if (found) 416 continue; 417 /* 418 * This lock has no anscestors, display its descendants. 419 */ 420 witness_displaydescendants(prnt, w); 421 } 422 } 423 424 static void 425 witness_display(void(*prnt)(const char *fmt, ...)) 426 { 427 struct witness *w; 428 429 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 430 witness_levelall(); 431 432 /* 433 * First, handle sleep locks which have been acquired at least 434 * once. 435 */ 436 prnt("Sleep locks:\n"); 437 witness_display_list(prnt, &w_sleep); 438 439 /* 440 * Now do spin locks which have been acquired at least once. 441 */ 442 prnt("\nSpin locks:\n"); 443 witness_display_list(prnt, &w_spin); 444 445 /* 446 * Finally, any locks which have not been acquired yet. 447 */ 448 prnt("\nLocks which were never acquired:\n"); 449 STAILQ_FOREACH(w, &w_all, w_list) { 450 if (w->w_file != NULL || w->w_refcount == 0) 451 continue; 452 prnt("%s\n", w->w_name); 453 } 454 } 455 456 void 457 witness_lock(struct lock_object *lock, int flags, const char *file, int line) 458 { 459 struct lock_list_entry **lock_list, *lle; 460 struct lock_instance *lock1, *lock2; 461 struct lock_class *class; 462 struct witness *w, *w1; 463 struct thread *td; 464 int i, j; 465 #ifdef DDB 466 int go_into_ddb = 0; 467 #endif /* DDB */ 468 469 if (witness_cold || witness_dead || lock->lo_witness == NULL || 470 panicstr != NULL) 471 return; 472 w = lock->lo_witness; 473 class = lock->lo_class; 474 td = curthread; 475 476 if (class->lc_flags & LC_SLEEPLOCK) { 477 /* 478 * Since spin locks include a critical section, this check 479 * impliclty enforces a lock order of all sleep locks before 480 * all spin locks. 481 */ 482 if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0) 483 panic("blockable sleep lock (%s) %s @ %s:%d", 484 class->lc_name, lock->lo_name, file, line); 485 lock_list = &td->td_sleeplocks; 486 } else 487 lock_list = PCPU_PTR(spinlocks); 488 489 /* 490 * Try locks do not block if they fail to acquire the lock, thus 491 * there is no danger of deadlocks or of switching while holding a 492 * spin lock if we acquire a lock via a try operation. 493 */ 494 if (flags & LOP_TRYLOCK) 495 goto out; 496 497 /* 498 * Is this the first lock acquired? If so, then no order checking 499 * is needed. 500 */ 501 if (*lock_list == NULL) 502 goto out; 503 504 /* 505 * Check to see if we are recursing on a lock we already own. 506 */ 507 lock1 = find_instance(*lock_list, lock); 508 if (lock1 != NULL) { 509 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 510 (flags & LOP_EXCLUSIVE) == 0) { 511 printf("shared lock of (%s) %s @ %s:%d\n", 512 class->lc_name, lock->lo_name, file, line); 513 printf("while exclusively locked from %s:%d\n", 514 lock1->li_file, lock1->li_line); 515 panic("share->excl"); 516 } 517 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 518 (flags & LOP_EXCLUSIVE) != 0) { 519 printf("exclusive lock of (%s) %s @ %s:%d\n", 520 class->lc_name, lock->lo_name, file, line); 521 printf("while share locked from %s:%d\n", 522 lock1->li_file, lock1->li_line); 523 panic("excl->share"); 524 } 525 lock1->li_flags++; 526 if ((lock->lo_flags & LO_RECURSABLE) == 0) { 527 printf( 528 "recursed on non-recursive lock (%s) %s @ %s:%d\n", 529 class->lc_name, lock->lo_name, file, line); 530 printf("first acquired @ %s:%d\n", lock1->li_file, 531 lock1->li_line); 532 panic("recurse"); 533 } 534 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 535 td->td_proc->p_pid, lock->lo_name, 536 lock1->li_flags & LI_RECURSEMASK); 537 lock1->li_file = file; 538 lock1->li_line = line; 539 return; 540 } 541 542 /* 543 * Check for duplicate locks of the same type. Note that we only 544 * have to check for this on the last lock we just acquired. Any 545 * other cases will be caught as lock order violations. 546 */ 547 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 548 w1 = lock1->li_lock->lo_witness; 549 if (w1 == w) { 550 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK)) 551 goto out; 552 w->w_same_squawked = 1; 553 printf("acquiring duplicate lock of same type: \"%s\"\n", 554 lock->lo_type); 555 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 556 lock1->li_file, lock1->li_line); 557 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 558 #ifdef DDB 559 go_into_ddb = 1; 560 #endif /* DDB */ 561 goto out; 562 } 563 MPASS(!mtx_owned(&w_mtx)); 564 mtx_lock_spin(&w_mtx); 565 /* 566 * If we have a known higher number just say ok 567 */ 568 if (witness_watch > 1 && w->w_level > w1->w_level) { 569 mtx_unlock_spin(&w_mtx); 570 goto out; 571 } 572 if (isitmydescendant(w1, w)) { 573 mtx_unlock_spin(&w_mtx); 574 goto out; 575 } 576 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 577 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 578 579 MPASS(j < WITNESS_COUNT); 580 lock1 = &lle->ll_children[i]; 581 w1 = lock1->li_lock->lo_witness; 582 583 /* 584 * If this lock doesn't undergo witness checking, 585 * then skip it. 586 */ 587 if (w1 == NULL) { 588 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 589 ("lock missing witness structure")); 590 continue; 591 } 592 /* 593 * If we are locking Giant and we slept with this 594 * lock, then skip it. 595 */ 596 if ((lock1->li_flags & LI_SLEPT) != 0 && 597 lock == &Giant.mtx_object) 598 continue; 599 /* 600 * If we are locking a sleepable lock and this lock 601 * isn't sleepable and isn't Giant, we want to treat 602 * it as a lock order violation to enfore a general 603 * lock order of sleepable locks before non-sleepable 604 * locks. Thus, we only bother checking the lock 605 * order hierarchy if we pass the initial test. 606 */ 607 if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 608 ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 609 lock1->li_lock != &Giant.mtx_object)) && 610 !isitmydescendant(w, w1)) 611 continue; 612 /* 613 * We have a lock order violation, check to see if it 614 * is allowed or has already been yelled about. 615 */ 616 mtx_unlock_spin(&w_mtx); 617 if (blessed(w, w1)) 618 goto out; 619 if (lock1->li_lock == &Giant.mtx_object) { 620 if (w1->w_Giant_squawked) 621 goto out; 622 else 623 w1->w_Giant_squawked = 1; 624 } else { 625 if (w1->w_other_squawked) 626 goto out; 627 else 628 w1->w_other_squawked = 1; 629 } 630 /* 631 * Ok, yell about it. 632 */ 633 printf("lock order reversal\n"); 634 /* 635 * Try to locate an earlier lock with 636 * witness w in our list. 637 */ 638 do { 639 lock2 = &lle->ll_children[i]; 640 MPASS(lock2->li_lock != NULL); 641 if (lock2->li_lock->lo_witness == w) 642 break; 643 i--; 644 if (i == 0 && lle->ll_next != NULL) { 645 lle = lle->ll_next; 646 i = lle->ll_count - 1; 647 MPASS(i != 0); 648 } 649 } while (i >= 0); 650 if (i < 0) { 651 printf(" 1st %p %s (%s) @ %s:%d\n", 652 lock1->li_lock, lock1->li_lock->lo_name, 653 lock1->li_lock->lo_type, lock1->li_file, 654 lock1->li_line); 655 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 656 lock->lo_name, lock->lo_type, file, line); 657 } else { 658 printf(" 1st %p %s (%s) @ %s:%d\n", 659 lock2->li_lock, lock2->li_lock->lo_name, 660 lock2->li_lock->lo_type, lock2->li_file, 661 lock2->li_line); 662 printf(" 2nd %p %s (%s) @ %s:%d\n", 663 lock1->li_lock, lock1->li_lock->lo_name, 664 lock1->li_lock->lo_type, lock1->li_file, 665 lock1->li_line); 666 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 667 lock->lo_name, lock->lo_type, file, line); 668 } 669 #ifdef DDB 670 go_into_ddb = 1; 671 #endif /* DDB */ 672 goto out; 673 } 674 } 675 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 676 /* 677 * Don't build a new relationship if we are locking Giant just 678 * after waking up and the previous lock in the list was acquired 679 * prior to blocking. 680 */ 681 if (lock == &Giant.mtx_object && (lock1->li_flags & LI_SLEPT) != 0) 682 mtx_unlock_spin(&w_mtx); 683 else { 684 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 685 lock->lo_type, lock1->li_lock->lo_type); 686 if (!itismychild(lock1->li_lock->lo_witness, w)) 687 mtx_unlock_spin(&w_mtx); 688 } 689 690 out: 691 #ifdef DDB 692 if (witness_ddb && go_into_ddb) 693 Debugger(__func__); 694 #endif /* DDB */ 695 w->w_file = file; 696 w->w_line = line; 697 698 lle = *lock_list; 699 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 700 lle = witness_lock_list_get(); 701 if (lle == NULL) 702 return; 703 lle->ll_next = *lock_list; 704 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 705 td->td_proc->p_pid, lle); 706 *lock_list = lle; 707 } 708 lock1 = &lle->ll_children[lle->ll_count++]; 709 lock1->li_lock = lock; 710 lock1->li_line = line; 711 lock1->li_file = file; 712 if ((flags & LOP_EXCLUSIVE) != 0) 713 lock1->li_flags = LI_EXCLUSIVE; 714 else 715 lock1->li_flags = 0; 716 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 717 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 718 } 719 720 void 721 witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 722 { 723 struct lock_instance *instance; 724 struct lock_class *class; 725 726 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 727 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 728 return; 729 class = lock->lo_class; 730 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 731 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 732 class->lc_name, lock->lo_name, file, line); 733 if ((flags & LOP_TRYLOCK) == 0) 734 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 735 lock->lo_name, file, line); 736 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 737 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 738 class->lc_name, lock->lo_name, file, line); 739 instance = find_instance(curthread->td_sleeplocks, lock); 740 if (instance == NULL) 741 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 742 class->lc_name, lock->lo_name, file, line); 743 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 744 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 745 class->lc_name, lock->lo_name, file, line); 746 if ((instance->li_flags & LI_RECURSEMASK) != 0) 747 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 748 class->lc_name, lock->lo_name, 749 instance->li_flags & LI_RECURSEMASK, file, line); 750 instance->li_flags |= LI_EXCLUSIVE; 751 } 752 753 void 754 witness_downgrade(struct lock_object *lock, int flags, const char *file, 755 int line) 756 { 757 struct lock_instance *instance; 758 struct lock_class *class; 759 760 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 761 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 762 return; 763 class = lock->lo_class; 764 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 765 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 766 class->lc_name, lock->lo_name, file, line); 767 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 768 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 769 class->lc_name, lock->lo_name, file, line); 770 instance = find_instance(curthread->td_sleeplocks, lock); 771 if (instance == NULL) 772 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 773 class->lc_name, lock->lo_name, file, line); 774 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 775 panic("downgrade of shared lock (%s) %s @ %s:%d", 776 class->lc_name, lock->lo_name, file, line); 777 if ((instance->li_flags & LI_RECURSEMASK) != 0) 778 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 779 class->lc_name, lock->lo_name, 780 instance->li_flags & LI_RECURSEMASK, file, line); 781 instance->li_flags &= ~LI_EXCLUSIVE; 782 } 783 784 void 785 witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 786 { 787 struct lock_list_entry **lock_list, *lle; 788 struct lock_instance *instance; 789 struct lock_class *class; 790 struct thread *td; 791 register_t s; 792 int i, j; 793 794 if (witness_cold || witness_dead || lock->lo_witness == NULL || 795 panicstr != NULL) 796 return; 797 td = curthread; 798 class = lock->lo_class; 799 if (class->lc_flags & LC_SLEEPLOCK) 800 lock_list = &td->td_sleeplocks; 801 else 802 lock_list = PCPU_PTR(spinlocks); 803 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 804 for (i = 0; i < (*lock_list)->ll_count; i++) { 805 instance = &(*lock_list)->ll_children[i]; 806 if (instance->li_lock == lock) { 807 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 808 (flags & LOP_EXCLUSIVE) == 0) { 809 printf( 810 "shared unlock of (%s) %s @ %s:%d\n", 811 class->lc_name, lock->lo_name, 812 file, line); 813 printf( 814 "while exclusively locked from %s:%d\n", 815 instance->li_file, 816 instance->li_line); 817 panic("excl->ushare"); 818 } 819 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 820 (flags & LOP_EXCLUSIVE) != 0) { 821 printf( 822 "exclusive unlock of (%s) %s @ %s:%d\n", 823 class->lc_name, lock->lo_name, 824 file, line); 825 printf( 826 "while share locked from %s:%d\n", 827 instance->li_file, 828 instance->li_line); 829 panic("share->uexcl"); 830 } 831 /* If we are recursed, unrecurse. */ 832 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 833 CTR4(KTR_WITNESS, 834 "%s: pid %d unrecursed on %s r=%d", __func__, 835 td->td_proc->p_pid, 836 instance->li_lock->lo_name, 837 instance->li_flags); 838 instance->li_flags--; 839 return; 840 } 841 s = intr_disable(); 842 CTR4(KTR_WITNESS, 843 "%s: pid %d removed %s from lle[%d]", __func__, 844 td->td_proc->p_pid, 845 instance->li_lock->lo_name, 846 (*lock_list)->ll_count - 1); 847 for (j = i; j < (*lock_list)->ll_count - 1; j++) 848 (*lock_list)->ll_children[j] = 849 (*lock_list)->ll_children[j + 1]; 850 (*lock_list)->ll_count--; 851 intr_restore(s); 852 if ((*lock_list)->ll_count == 0) { 853 lle = *lock_list; 854 *lock_list = lle->ll_next; 855 CTR3(KTR_WITNESS, 856 "%s: pid %d removed lle %p", __func__, 857 td->td_proc->p_pid, lle); 858 witness_lock_list_free(lle); 859 } 860 return; 861 } 862 } 863 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 864 file, line); 865 } 866 867 /* 868 * Warn if any held locks are not sleepable. Note that Giant and the lock 869 * passed in are both special cases since they are both released during the 870 * sleep process and aren't actually held while the thread is asleep. 871 */ 872 int 873 witness_sleep(int check_only, struct lock_object *lock, const char *file, 874 int line) 875 { 876 struct lock_list_entry **lock_list, *lle; 877 struct lock_instance *lock1; 878 struct thread *td; 879 int i, n; 880 881 if (witness_cold || witness_dead || panicstr != NULL) 882 return (0); 883 n = 0; 884 td = curthread; 885 lock_list = &td->td_sleeplocks; 886 again: 887 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 888 for (i = lle->ll_count - 1; i >= 0; i--) { 889 lock1 = &lle->ll_children[i]; 890 if (lock1->li_lock == lock || 891 lock1->li_lock == &Giant.mtx_object) 892 continue; 893 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) { 894 if (check_only == 0) { 895 CTR3(KTR_WITNESS, 896 "pid %d: sleeping with lock (%s) %s held", 897 td->td_proc->p_pid, 898 lock1->li_lock->lo_class->lc_name, 899 lock1->li_lock->lo_name); 900 lock1->li_flags |= LI_SLEPT; 901 } 902 continue; 903 } 904 n++; 905 printf("%s:%d: %s with \"%s\" locked from %s:%d\n", 906 file, line, check_only ? "could sleep" : "sleeping", 907 lock1->li_lock->lo_name, lock1->li_file, 908 lock1->li_line); 909 } 910 if (lock_list == &td->td_sleeplocks && PCPU_GET(spinlocks) != NULL) { 911 /* 912 * Since we already hold a spinlock preemption is 913 * already blocked. 914 */ 915 lock_list = PCPU_PTR(spinlocks); 916 goto again; 917 } 918 #ifdef DDB 919 if (witness_ddb && n) 920 Debugger(__func__); 921 #endif /* DDB */ 922 return (n); 923 } 924 925 static struct witness * 926 enroll(const char *description, struct lock_class *lock_class) 927 { 928 struct witness *w; 929 930 if (!witness_watch || witness_dead || panicstr != NULL) 931 return (NULL); 932 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 933 return (NULL); 934 mtx_lock_spin(&w_mtx); 935 STAILQ_FOREACH(w, &w_all, w_list) { 936 if (w->w_name == description || (w->w_refcount > 0 && 937 strcmp(description, w->w_name) == 0)) { 938 w->w_refcount++; 939 mtx_unlock_spin(&w_mtx); 940 if (lock_class != w->w_class) 941 panic( 942 "lock (%s) %s does not match earlier (%s) lock", 943 description, lock_class->lc_name, 944 w->w_class->lc_name); 945 return (w); 946 } 947 } 948 /* 949 * This isn't quite right, as witness_cold is still 0 while we 950 * enroll all the locks initialized before witness_initialize(). 951 */ 952 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 953 mtx_unlock_spin(&w_mtx); 954 panic("spin lock %s not in order list", description); 955 } 956 if ((w = witness_get()) == NULL) 957 return (NULL); 958 w->w_name = description; 959 w->w_class = lock_class; 960 w->w_refcount = 1; 961 STAILQ_INSERT_HEAD(&w_all, w, w_list); 962 if (lock_class->lc_flags & LC_SPINLOCK) 963 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 964 else if (lock_class->lc_flags & LC_SLEEPLOCK) 965 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 966 else { 967 mtx_unlock_spin(&w_mtx); 968 panic("lock class %s is not sleep or spin", 969 lock_class->lc_name); 970 } 971 mtx_unlock_spin(&w_mtx); 972 return (w); 973 } 974 975 static int 976 itismychild(struct witness *parent, struct witness *child) 977 { 978 static int recursed; 979 struct witness_child_list_entry **wcl; 980 struct witness_list *list; 981 982 MPASS(child != NULL && parent != NULL); 983 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 984 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 985 panic( 986 "%s: parent (%s) and child (%s) are not the same lock type", 987 __func__, parent->w_class->lc_name, 988 child->w_class->lc_name); 989 990 /* 991 * Insert "child" after "parent" 992 */ 993 wcl = &parent->w_children; 994 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 995 wcl = &(*wcl)->wcl_next; 996 if (*wcl == NULL) { 997 *wcl = witness_child_get(); 998 if (*wcl == NULL) 999 return (1); 1000 } 1001 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1002 1003 /* 1004 * Now prune whole tree. We look for cases where a lock is now 1005 * both a descendant and a direct child of a given lock. In that 1006 * case, we want to remove the direct child link from the tree. 1007 */ 1008 if (recursed) 1009 return (0); 1010 recursed = 1; 1011 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1012 list = &w_sleep; 1013 else 1014 list = &w_spin; 1015 STAILQ_FOREACH(child, list, w_typelist) { 1016 STAILQ_FOREACH(parent, list, w_typelist) { 1017 if (!isitmychild(parent, child)) 1018 continue; 1019 removechild(parent, child); 1020 if (isitmydescendant(parent, child)) 1021 continue; 1022 itismychild(parent, child); 1023 } 1024 } 1025 recursed = 0; 1026 witness_levelall(); 1027 return (0); 1028 } 1029 1030 static void 1031 removechild(struct witness *parent, struct witness *child) 1032 { 1033 struct witness_child_list_entry **wcl, *wcl1; 1034 int i; 1035 1036 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1037 for (i = 0; i < (*wcl)->wcl_count; i++) 1038 if ((*wcl)->wcl_children[i] == child) 1039 goto found; 1040 return; 1041 found: 1042 (*wcl)->wcl_count--; 1043 if ((*wcl)->wcl_count > i) 1044 (*wcl)->wcl_children[i] = 1045 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1046 MPASS((*wcl)->wcl_children[i] != NULL); 1047 if ((*wcl)->wcl_count != 0) 1048 return; 1049 wcl1 = *wcl; 1050 *wcl = wcl1->wcl_next; 1051 witness_child_free(wcl1); 1052 } 1053 1054 static int 1055 isitmychild(struct witness *parent, struct witness *child) 1056 { 1057 struct witness_child_list_entry *wcl; 1058 int i; 1059 1060 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1061 for (i = 0; i < wcl->wcl_count; i++) { 1062 if (wcl->wcl_children[i] == child) 1063 return (1); 1064 } 1065 } 1066 return (0); 1067 } 1068 1069 static int 1070 isitmydescendant(struct witness *parent, struct witness *child) 1071 { 1072 struct witness_child_list_entry *wcl; 1073 int i, j; 1074 1075 if (isitmychild(parent, child)) 1076 return (1); 1077 j = 0; 1078 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1079 MPASS(j < 1000); 1080 for (i = 0; i < wcl->wcl_count; i++) { 1081 if (isitmydescendant(wcl->wcl_children[i], child)) 1082 return (1); 1083 } 1084 j++; 1085 } 1086 return (0); 1087 } 1088 1089 void 1090 witness_levelall (void) 1091 { 1092 struct witness_list *list; 1093 struct witness *w, *w1; 1094 1095 /* 1096 * First clear all levels. 1097 */ 1098 STAILQ_FOREACH(w, &w_all, w_list) { 1099 w->w_level = 0; 1100 } 1101 1102 /* 1103 * Look for locks with no parent and level all their descendants. 1104 */ 1105 STAILQ_FOREACH(w, &w_all, w_list) { 1106 /* 1107 * This is just an optimization, technically we could get 1108 * away just walking the all list each time. 1109 */ 1110 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1111 list = &w_sleep; 1112 else 1113 list = &w_spin; 1114 STAILQ_FOREACH(w1, list, w_typelist) { 1115 if (isitmychild(w1, w)) 1116 goto skip; 1117 } 1118 witness_leveldescendents(w, 0); 1119 skip: 1120 ; /* silence GCC 3.x */ 1121 } 1122 } 1123 1124 static void 1125 witness_leveldescendents(struct witness *parent, int level) 1126 { 1127 struct witness_child_list_entry *wcl; 1128 int i; 1129 1130 if (parent->w_level < level) 1131 parent->w_level = level; 1132 level++; 1133 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1134 for (i = 0; i < wcl->wcl_count; i++) 1135 witness_leveldescendents(wcl->wcl_children[i], level); 1136 } 1137 1138 static void 1139 witness_displaydescendants(void(*prnt)(const char *fmt, ...), 1140 struct witness *parent) 1141 { 1142 struct witness_child_list_entry *wcl; 1143 int i, level; 1144 1145 level = parent->w_level; 1146 prnt("%-2d", level); 1147 for (i = 0; i < level; i++) 1148 prnt(" "); 1149 if (parent->w_refcount > 0) { 1150 prnt("%s", parent->w_name); 1151 if (parent->w_file != NULL) 1152 prnt(" -- last acquired @ %s:%d\n", parent->w_file, 1153 parent->w_line); 1154 } else 1155 prnt("(dead)\n"); 1156 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1157 for (i = 0; i < wcl->wcl_count; i++) 1158 witness_displaydescendants(prnt, 1159 wcl->wcl_children[i]); 1160 } 1161 1162 static int 1163 blessed(struct witness *w1, struct witness *w2) 1164 { 1165 int i; 1166 struct witness_blessed *b; 1167 1168 for (i = 0; i < blessed_count; i++) { 1169 b = &blessed_list[i]; 1170 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1171 if (strcmp(w2->w_name, b->b_lock2) == 0) 1172 return (1); 1173 continue; 1174 } 1175 if (strcmp(w1->w_name, b->b_lock2) == 0) 1176 if (strcmp(w2->w_name, b->b_lock1) == 0) 1177 return (1); 1178 } 1179 return (0); 1180 } 1181 1182 static struct witness * 1183 witness_get(void) 1184 { 1185 struct witness *w; 1186 1187 if (witness_dead) { 1188 mtx_unlock_spin(&w_mtx); 1189 return (NULL); 1190 } 1191 if (STAILQ_EMPTY(&w_free)) { 1192 witness_dead = 1; 1193 mtx_unlock_spin(&w_mtx); 1194 printf("%s: witness exhausted\n", __func__); 1195 return (NULL); 1196 } 1197 w = STAILQ_FIRST(&w_free); 1198 STAILQ_REMOVE_HEAD(&w_free, w_list); 1199 bzero(w, sizeof(*w)); 1200 return (w); 1201 } 1202 1203 static void 1204 witness_free(struct witness *w) 1205 { 1206 1207 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1208 } 1209 1210 static struct witness_child_list_entry * 1211 witness_child_get(void) 1212 { 1213 struct witness_child_list_entry *wcl; 1214 1215 if (witness_dead) { 1216 mtx_unlock_spin(&w_mtx); 1217 return (NULL); 1218 } 1219 wcl = w_child_free; 1220 if (wcl == NULL) { 1221 witness_dead = 1; 1222 mtx_unlock_spin(&w_mtx); 1223 printf("%s: witness exhausted\n", __func__); 1224 return (NULL); 1225 } 1226 w_child_free = wcl->wcl_next; 1227 bzero(wcl, sizeof(*wcl)); 1228 return (wcl); 1229 } 1230 1231 static void 1232 witness_child_free(struct witness_child_list_entry *wcl) 1233 { 1234 1235 wcl->wcl_next = w_child_free; 1236 w_child_free = wcl; 1237 } 1238 1239 static struct lock_list_entry * 1240 witness_lock_list_get(void) 1241 { 1242 struct lock_list_entry *lle; 1243 1244 if (witness_dead) 1245 return (NULL); 1246 mtx_lock_spin(&w_mtx); 1247 lle = w_lock_list_free; 1248 if (lle == NULL) { 1249 witness_dead = 1; 1250 mtx_unlock_spin(&w_mtx); 1251 printf("%s: witness exhausted\n", __func__); 1252 return (NULL); 1253 } 1254 w_lock_list_free = lle->ll_next; 1255 mtx_unlock_spin(&w_mtx); 1256 bzero(lle, sizeof(*lle)); 1257 return (lle); 1258 } 1259 1260 static void 1261 witness_lock_list_free(struct lock_list_entry *lle) 1262 { 1263 1264 mtx_lock_spin(&w_mtx); 1265 lle->ll_next = w_lock_list_free; 1266 w_lock_list_free = lle; 1267 mtx_unlock_spin(&w_mtx); 1268 } 1269 1270 static struct lock_instance * 1271 find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1272 { 1273 struct lock_list_entry *lle; 1274 struct lock_instance *instance; 1275 int i; 1276 1277 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1278 for (i = lle->ll_count - 1; i >= 0; i--) { 1279 instance = &lle->ll_children[i]; 1280 if (instance->li_lock == lock) 1281 return (instance); 1282 } 1283 return (NULL); 1284 } 1285 1286 int 1287 witness_list_locks(struct lock_list_entry **lock_list) 1288 { 1289 struct lock_list_entry *lle; 1290 struct lock_instance *instance; 1291 struct lock_object *lock; 1292 int i, nheld; 1293 1294 nheld = 0; 1295 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1296 for (i = lle->ll_count - 1; i >= 0; i--) { 1297 instance = &lle->ll_children[i]; 1298 lock = instance->li_lock; 1299 printf("%s %s %s", 1300 (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1301 "exclusive" : "shared", 1302 lock->lo_class->lc_name, lock->lo_name); 1303 if (lock->lo_type != lock->lo_name) 1304 printf(" (%s)", lock->lo_type); 1305 printf(" r = %d (%p) locked @ %s:%d\n", 1306 instance->li_flags & LI_RECURSEMASK, lock, 1307 instance->li_file, instance->li_line); 1308 nheld++; 1309 } 1310 return (nheld); 1311 } 1312 1313 /* 1314 * Calling this on td != curthread is bad unless we are in ddb. 1315 */ 1316 int 1317 witness_list(struct thread *td) 1318 { 1319 int nheld; 1320 1321 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1322 #ifdef DDB 1323 KASSERT(td == curthread || db_active, 1324 ("%s: td != curthread and we aren't in the debugger", __func__)); 1325 if (!db_active && witness_dead) 1326 return (0); 1327 #else 1328 KASSERT(td == curthread, ("%s: p != curthread", __func__)); 1329 if (witness_dead) 1330 return (0); 1331 #endif 1332 nheld = witness_list_locks(&td->td_sleeplocks); 1333 1334 /* 1335 * We only handle spinlocks if td == curthread. This is somewhat broken 1336 * if td is currently executing on some other CPU and holds spin locks 1337 * as we won't display those locks. If we had a MI way of getting 1338 * the per-cpu data for a given cpu then we could use 1339 * td->td_kse->ke_oncpu to get the list of spinlocks for this thread 1340 * and "fix" this. 1341 * 1342 * That still wouldn't really fix this unless we locked sched_lock 1343 * or stopped the other CPU to make sure it wasn't changing the list 1344 * out from under us. It is probably best to just not try to handle 1345 * threads on other CPU's for now. 1346 */ 1347 if (td == curthread && PCPU_GET(spinlocks) != NULL) 1348 nheld += witness_list_locks(PCPU_PTR(spinlocks)); 1349 1350 return (nheld); 1351 } 1352 1353 void 1354 witness_save(struct lock_object *lock, const char **filep, int *linep) 1355 { 1356 struct lock_instance *instance; 1357 1358 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1359 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1360 return; 1361 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1362 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1363 lock->lo_class->lc_name, lock->lo_name); 1364 instance = find_instance(curthread->td_sleeplocks, lock); 1365 if (instance == NULL) 1366 panic("%s: lock (%s) %s not locked", __func__, 1367 lock->lo_class->lc_name, lock->lo_name); 1368 *filep = instance->li_file; 1369 *linep = instance->li_line; 1370 } 1371 1372 void 1373 witness_restore(struct lock_object *lock, const char *file, int line) 1374 { 1375 struct lock_instance *instance; 1376 1377 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1378 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1379 return; 1380 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1381 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1382 lock->lo_class->lc_name, lock->lo_name); 1383 instance = find_instance(curthread->td_sleeplocks, lock); 1384 if (instance == NULL) 1385 panic("%s: lock (%s) %s not locked", __func__, 1386 lock->lo_class->lc_name, lock->lo_name); 1387 lock->lo_witness->w_file = file; 1388 lock->lo_witness->w_line = line; 1389 instance->li_file = file; 1390 instance->li_line = line; 1391 } 1392 1393 void 1394 witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1395 { 1396 #ifdef INVARIANT_SUPPORT 1397 struct lock_instance *instance; 1398 1399 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1400 return; 1401 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 1402 instance = find_instance(curthread->td_sleeplocks, lock); 1403 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 1404 instance = find_instance(PCPU_GET(spinlocks), lock); 1405 else { 1406 panic("Lock (%s) %s is not sleep or spin!", 1407 lock->lo_class->lc_name, lock->lo_name); 1408 return; 1409 } 1410 switch (flags) { 1411 case LA_UNLOCKED: 1412 if (instance != NULL) 1413 panic("Lock (%s) %s locked @ %s:%d.", 1414 lock->lo_class->lc_name, lock->lo_name, file, line); 1415 break; 1416 case LA_LOCKED: 1417 case LA_LOCKED | LA_RECURSED: 1418 case LA_LOCKED | LA_NOTRECURSED: 1419 case LA_SLOCKED: 1420 case LA_SLOCKED | LA_RECURSED: 1421 case LA_SLOCKED | LA_NOTRECURSED: 1422 case LA_XLOCKED: 1423 case LA_XLOCKED | LA_RECURSED: 1424 case LA_XLOCKED | LA_NOTRECURSED: 1425 if (instance == NULL) { 1426 panic("Lock (%s) %s not locked @ %s:%d.", 1427 lock->lo_class->lc_name, lock->lo_name, file, line); 1428 break; 1429 } 1430 if ((flags & LA_XLOCKED) != 0 && 1431 (instance->li_flags & LI_EXCLUSIVE) == 0) 1432 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1433 lock->lo_class->lc_name, lock->lo_name, file, line); 1434 if ((flags & LA_SLOCKED) != 0 && 1435 (instance->li_flags & LI_EXCLUSIVE) != 0) 1436 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1437 lock->lo_class->lc_name, lock->lo_name, file, line); 1438 if ((flags & LA_RECURSED) != 0 && 1439 (instance->li_flags & LI_RECURSEMASK) == 0) 1440 panic("Lock (%s) %s not recursed @ %s:%d.", 1441 lock->lo_class->lc_name, lock->lo_name, file, line); 1442 if ((flags & LA_NOTRECURSED) != 0 && 1443 (instance->li_flags & LI_RECURSEMASK) != 0) 1444 panic("Lock (%s) %s recursed @ %s:%d.", 1445 lock->lo_class->lc_name, lock->lo_name, file, line); 1446 break; 1447 default: 1448 panic("Invalid lock assertion at %s:%d.", file, line); 1449 1450 } 1451 #endif /* INVARIANT_SUPPORT */ 1452 } 1453 1454 #ifdef DDB 1455 1456 DB_SHOW_COMMAND(locks, db_witness_list) 1457 { 1458 struct thread *td; 1459 pid_t pid; 1460 struct proc *p; 1461 1462 if (have_addr) { 1463 pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 1464 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 1465 ((addr >> 16) % 16) * 10000; 1466 /* sx_slock(&allproc_lock); */ 1467 FOREACH_PROC_IN_SYSTEM(p) { 1468 if (p->p_pid == pid) 1469 break; 1470 } 1471 /* sx_sunlock(&allproc_lock); */ 1472 if (p == NULL) { 1473 db_printf("pid %d not found\n", pid); 1474 return; 1475 } 1476 FOREACH_THREAD_IN_PROC(p, td) { 1477 witness_list(td); 1478 } 1479 } else { 1480 td = curthread; 1481 witness_list(td); 1482 } 1483 } 1484 1485 DB_SHOW_COMMAND(witness, db_witness_display) 1486 { 1487 1488 witness_display(db_printf); 1489 } 1490 #endif 1491