subr_witness.c revision 74944
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: head/sys/kern/subr_witness.c 74944 2001-03-28 16:11:51Z jhb $ 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 processes 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 86struct witness_child_list_entry; 87 88struct 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 103struct 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 109STAILQ_HEAD(witness_list, witness); 110 111struct witness_blessed { 112 const char *b_lock1; 113 const char *b_lock2; 114}; 115 116struct witness_order_list_entry { 117 const char *w_name; 118 struct lock_class *w_class; 119}; 120 121static struct witness *enroll(const char *description, 122 struct lock_class *lock_class); 123static int itismychild(struct witness *parent, struct witness *child); 124static void removechild(struct witness *parent, struct witness *child); 125static int isitmychild(struct witness *parent, struct witness *child); 126static int isitmydescendant(struct witness *parent, struct witness *child); 127static int dup_ok(struct witness *); 128static int blessed(struct witness *, struct witness *); 129static void witness_display_list(void(*prnt)(const char *fmt, ...), 130 struct witness_list *list); 131static void witness_displaydescendants(void(*)(const char *fmt, ...), 132 struct witness *); 133static void witness_leveldescendents(struct witness *parent, int level); 134static void witness_levelall(void); 135static struct witness *witness_get(void); 136static void witness_free(struct witness *m); 137static struct witness_child_list_entry *witness_child_get(void); 138static void witness_child_free(struct witness_child_list_entry *wcl); 139static struct lock_list_entry *witness_lock_list_get(void); 140static void witness_lock_list_free(struct lock_list_entry *lle); 141static void witness_display(void(*)(const char *fmt, ...)); 142 143MALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 144 145static int witness_watch; 146TUNABLE_INT_DECL("debug.witness_watch", 1, witness_watch); 147SYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 148 149#ifdef DDB 150/* 151 * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 152 * drop into kdebug() when: 153 * - a lock heirarchy violation occurs 154 * - locks are held when going to sleep. 155 */ 156int witness_ddb; 157#ifdef WITNESS_DDB 158TUNABLE_INT_DECL("debug.witness_ddb", 1, witness_ddb); 159#else 160TUNABLE_INT_DECL("debug.witness_ddb", 0, witness_ddb); 161#endif 162SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 163#endif /* DDB */ 164 165int witness_skipspin; 166#ifdef WITNESS_SKIPSPIN 167TUNABLE_INT_DECL("debug.witness_skipspin", 1, witness_skipspin); 168#else 169TUNABLE_INT_DECL("debug.witness_skipspin", 0, witness_skipspin); 170#endif 171SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 172 ""); 173 174static struct mtx w_mtx; 175static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 176static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 177static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 178static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 179static struct witness_child_list_entry *w_child_free = NULL; 180static struct lock_list_entry *w_lock_list_free = NULL; 181static int witness_dead; /* fatal error, probably no memory */ 182 183static struct witness w_data[WITNESS_COUNT]; 184static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 185static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 186 187static struct witness_order_list_entry order_lists[] = { 188 { "Giant", &lock_class_mtx_sleep }, 189 { "proctree", &lock_class_sx }, 190 { "allproc", &lock_class_sx }, 191 { "process lock", &lock_class_mtx_sleep }, 192 { "uidinfo hash", &lock_class_mtx_sleep }, 193 { "uidinfo struct", &lock_class_mtx_sleep }, 194 { NULL, NULL }, 195#if defined(__i386__) && defined (SMP) 196 { "com", &lock_class_mtx_spin }, 197#endif 198 { "sio", &lock_class_mtx_spin }, 199#ifdef __i386__ 200 { "cy", &lock_class_mtx_spin }, 201#endif 202 { "ng_node", &lock_class_mtx_spin }, 203 { "ng_worklist", &lock_class_mtx_spin }, 204 { "ithread table lock", &lock_class_mtx_spin }, 205 { "ithread list lock", &lock_class_mtx_spin }, 206 { "sched lock", &lock_class_mtx_spin }, 207#ifdef __i386__ 208 { "clk", &lock_class_mtx_spin }, 209#endif 210 { "callout", &lock_class_mtx_spin }, 211 /* 212 * leaf locks 213 */ 214#ifdef SMP 215#ifdef __i386__ 216 { "ap boot", &lock_class_mtx_spin }, 217 { "imen", &lock_class_mtx_spin }, 218#endif 219 { "smp rendezvous", &lock_class_mtx_spin }, 220#endif 221 { NULL, NULL }, 222 { NULL, NULL } 223}; 224 225static const char *dup_list[] = { 226 "process lock", 227 NULL 228}; 229 230/* 231 * Pairs of locks which have been blessed 232 * Don't complain about order problems with blessed locks 233 */ 234static struct witness_blessed blessed_list[] = { 235}; 236static int blessed_count = 237 sizeof(blessed_list) / sizeof(struct witness_blessed); 238 239/* 240 * List of all locks in the system. 241 */ 242STAILQ_HEAD(, lock_object) all_locks = STAILQ_HEAD_INITIALIZER(all_locks); 243 244static struct mtx all_mtx = { 245 { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 246 "All locks list", /* mtx_object.lo_name */ 247 NULL, /* mtx_object.lo_file */ 248 0, /* mtx_object.lo_line */ 249 LO_INITIALIZED, /* mtx_object.lo_flags */ 250 { NULL }, /* mtx_object.lo_list */ 251 NULL }, /* mtx_object.lo_witness */ 252 MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 253 0, /* mtx_savecrit */ 254 TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 255 { NULL, NULL } /* mtx_contested */ 256}; 257 258/* 259 * This global is set to 0 once it becomes safe to use the witness code. 260 */ 261static int witness_cold = 1; 262 263/* 264 * Global variables for book keeping. 265 */ 266static int lock_cur_cnt; 267static int lock_max_cnt; 268 269/* 270 * The WITNESS-enabled diagnostic code. 271 */ 272static void 273witness_initialize(void *dummy __unused) 274{ 275 struct lock_object *lock; 276 struct witness_order_list_entry *order; 277 struct witness *w, *w1; 278 int i; 279 280 /* 281 * We have to release Giant before initializing its witness 282 * structure so that WITNESS doesn't get confused. 283 */ 284 mtx_unlock(&Giant); 285 mtx_assert(&Giant, MA_NOTOWNED); 286 287 STAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 288 mtx_init(&w_mtx, "witness lock", MTX_SPIN | MTX_QUIET | MTX_NOWITNESS); 289 for (i = 0; i < WITNESS_COUNT; i++) 290 witness_free(&w_data[i]); 291 for (i = 0; i < WITNESS_CHILDCOUNT; i++) 292 witness_child_free(&w_childdata[i]); 293 for (i = 0; i < LOCK_CHILDCOUNT; i++) 294 witness_lock_list_free(&w_locklistdata[i]); 295 296 /* First add in all the specified order lists. */ 297 for (order = order_lists; order->w_name != NULL; order++) { 298 w = enroll(order->w_name, order->w_class); 299 w->w_file = "order list"; 300 for (order++; order->w_name != NULL; order++) { 301 w1 = enroll(order->w_name, order->w_class); 302 w1->w_file = "order list"; 303 itismychild(w, w1); 304 w = w1; 305 } 306 } 307 308 /* Iterate through all locks and add them to witness. */ 309 mtx_lock(&all_mtx); 310 STAILQ_FOREACH(lock, &all_locks, lo_list) { 311 if (lock->lo_flags & LO_WITNESS) 312 lock->lo_witness = enroll(lock->lo_name, 313 lock->lo_class); 314 else 315 lock->lo_witness = NULL; 316 } 317 mtx_unlock(&all_mtx); 318 319 /* Mark the witness code as being ready for use. */ 320 atomic_store_rel_int(&witness_cold, 0); 321 322 mtx_lock(&Giant); 323} 324SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 325 326void 327witness_init(struct lock_object *lock) 328{ 329 struct lock_class *class; 330 331 class = lock->lo_class; 332 if (lock->lo_flags & LO_INITIALIZED) 333 panic("%s: lock (%s) %s is already initialized!\n", __func__, 334 class->lc_name, lock->lo_name); 335 336 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 337 (class->lc_flags & LC_RECURSABLE) == 0) 338 panic("%s: lock (%s) %s can not be recursable!\n", __func__, 339 class->lc_name, lock->lo_name); 340 341 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 342 (class->lc_flags & LC_SLEEPABLE) == 0) 343 panic("%s: lock (%s) %s can not be sleepable!\n", __func__, 344 class->lc_name, lock->lo_name); 345 346 mtx_lock(&all_mtx); 347 STAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 348 lock->lo_flags |= LO_INITIALIZED; 349 lock_cur_cnt++; 350 if (lock_cur_cnt > lock_max_cnt) 351 lock_max_cnt = lock_cur_cnt; 352 mtx_unlock(&all_mtx); 353 if (!witness_cold && !witness_dead && 354 (lock->lo_flags & LO_WITNESS) != 0) 355 lock->lo_witness = enroll(lock->lo_name, class); 356 else 357 lock->lo_witness = NULL; 358} 359 360void 361witness_destroy(struct lock_object *lock) 362{ 363 364 if (witness_cold) 365 panic("lock (%s) %s destroyed while witness_cold", 366 lock->lo_class->lc_name, lock->lo_name); 367 368 if ((lock->lo_flags & LO_INITIALIZED) == 0) 369 panic("%s: lock (%s) %s is not initialized!\n", __func__, 370 lock->lo_class->lc_name, lock->lo_name); 371 372 if (lock->lo_flags & LO_LOCKED) 373 panic("lock (%s) %s destroyed while held", 374 lock->lo_class->lc_name, lock->lo_name); 375 376 mtx_lock(&all_mtx); 377 lock_cur_cnt--; 378 STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list); 379 lock->lo_flags &= LO_INITIALIZED; 380 mtx_unlock(&all_mtx); 381} 382 383static void 384witness_display_list(void(*prnt)(const char *fmt, ...), 385 struct witness_list *list) 386{ 387 struct witness *w, *w1; 388 int found; 389 390 STAILQ_FOREACH(w, list, w_typelist) { 391 if (w->w_file == NULL) 392 continue; 393 found = 0; 394 STAILQ_FOREACH(w1, list, w_typelist) { 395 if (isitmychild(w1, w)) { 396 found++; 397 break; 398 } 399 } 400 if (found) 401 continue; 402 /* 403 * This lock has no anscestors, display its descendants. 404 */ 405 witness_displaydescendants(prnt, w); 406 } 407} 408 409static void 410witness_display(void(*prnt)(const char *fmt, ...)) 411{ 412 struct witness *w; 413 414 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 415 witness_levelall(); 416 417 /* 418 * First, handle sleep locks which have been acquired at least 419 * once. 420 */ 421 prnt("Sleep locks:\n"); 422 witness_display_list(prnt, &w_sleep); 423 424 /* 425 * Now do spin locks which have been acquired at least once. 426 */ 427 prnt("\nSpin locks:\n"); 428 witness_display_list(prnt, &w_spin); 429 430 /* 431 * Finally, any locks which have not been acquired yet. 432 */ 433 prnt("\nLocks which were never acquired:\n"); 434 STAILQ_FOREACH(w, &w_all, w_list) { 435 if (w->w_file != NULL) 436 continue; 437 prnt("%s\n", w->w_name); 438 } 439} 440 441void 442witness_lock(struct lock_object *lock, int flags, const char *file, int line) 443{ 444 struct lock_list_entry **lock_list, *lle; 445 struct lock_object *lock1, *lock2; 446 struct lock_class *class; 447 struct witness *w, *w1; 448 struct proc *p; 449 int i, j; 450#ifdef DDB 451 int go_into_ddb = 0; 452#endif /* DDB */ 453 454 if (witness_cold || witness_dead || lock->lo_witness == NULL || 455 panicstr) 456 return; 457 w = lock->lo_witness; 458 class = lock->lo_class; 459 p = curproc; 460 461 if ((lock->lo_flags & LO_LOCKED) == 0) 462 panic("%s: lock (%s) %s is not locked @ %s:%d", __func__, 463 class->lc_name, lock->lo_name, file, line); 464 465 if ((lock->lo_flags & LO_RECURSED) != 0) { 466 if ((lock->lo_flags & LO_RECURSABLE) == 0) 467 panic( 468 "%s: recursed on non-recursive lock (%s) %s @ %s:%d", 469 __func__, class->lc_name, lock->lo_name, file, 470 line); 471 return; 472 } 473 474 /* 475 * We have to hold a spinlock to keep lock_list valid across the check 476 * in the LC_SLEEPLOCK case. In the LC_SPINLOCK case, it is already 477 * protected by the spinlock we are currently performing the witness 478 * checks on, so it is ok to release the lock after performing this 479 * check. All we have to protect is the LC_SLEEPLOCK case when no 480 * spinlocks are held as we may get preempted during this check and 481 * lock_list could end up pointing to some other CPU's spinlock list. 482 */ 483 mtx_lock_spin(&w_mtx); 484 lock_list = PCPU_PTR(spinlocks); 485 if (class->lc_flags & LC_SLEEPLOCK) { 486 if (*lock_list != NULL) { 487 mtx_unlock_spin(&w_mtx); 488 panic("blockable sleep lock (%s) %s @ %s:%d", 489 class->lc_name, lock->lo_name, file, line); 490 } 491 lock_list = &p->p_sleeplocks; 492 } 493 mtx_unlock_spin(&w_mtx); 494 495 if (flags & LOP_TRYLOCK) 496 goto out; 497 498 /* 499 * Is this the first lock acquired? If so, then no order checking 500 * is needed. 501 */ 502 if (*lock_list == NULL) 503 goto out; 504 505 /* 506 * Check for duplicate locks of the same type. Note that we only 507 * have to check for this on the last lock we just acquired. Any 508 * other cases will be caught as lock order violations. 509 */ 510 lock1 = (*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 511 w1 = lock1->lo_witness; 512 if (w1 == w) { 513 if (w->w_same_squawked || dup_ok(w)) 514 goto out; 515 w->w_same_squawked = 1; 516 printf("acquring duplicate lock of same type: \"%s\"\n", 517 lock->lo_name); 518 printf(" 1st @ %s:%d\n", w->w_file, w->w_line); 519 printf(" 2nd @ %s:%d\n", file, line); 520#ifdef DDB 521 go_into_ddb = 1; 522#endif /* DDB */ 523 goto out; 524 } 525 MPASS(!mtx_owned(&w_mtx)); 526 mtx_lock_spin(&w_mtx); 527 /* 528 * If we have a known higher number just say ok 529 */ 530 if (witness_watch > 1 && w->w_level > w1->w_level) { 531 mtx_unlock_spin(&w_mtx); 532 goto out; 533 } 534 if (isitmydescendant(w1, w)) { 535 mtx_unlock_spin(&w_mtx); 536 goto out; 537 } 538 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 539 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 540 541 MPASS(j < WITNESS_COUNT); 542 lock1 = lle->ll_children[i]; 543 w1 = lock1->lo_witness; 544 545 /* 546 * If this lock doesn't undergo witness checking, 547 * then skip it. 548 */ 549 if (w1 == NULL) { 550 KASSERT((lock1->lo_flags & LO_WITNESS) == 0, 551 ("lock missing witness structure")); 552 continue; 553 } 554 if (!isitmydescendant(w, w1)) 555 continue; 556 /* 557 * We have a lock order violation, check to see if it 558 * is allowed or has already been yelled about. 559 */ 560 mtx_unlock_spin(&w_mtx); 561 if (blessed(w, w1)) 562 goto out; 563 if (lock1 == &Giant.mtx_object) { 564 if (w1->w_Giant_squawked) 565 goto out; 566 else 567 w1->w_Giant_squawked = 1; 568 } else { 569 if (w1->w_other_squawked) 570 goto out; 571 else 572 w1->w_other_squawked = 1; 573 } 574 /* 575 * Ok, yell about it. 576 */ 577 printf("lock order reversal\n"); 578 /* 579 * Try to locate an earlier lock with 580 * witness w in our list. 581 */ 582 do { 583 lock2 = lle->ll_children[i]; 584 MPASS(lock2 != NULL); 585 if (lock2->lo_witness == w) 586 break; 587 i--; 588 if (i == 0 && lle->ll_next != NULL) { 589 lle = lle->ll_next; 590 i = lle->ll_count - 1; 591 MPASS(i != 0); 592 } 593 } while (i >= 0); 594 if (i < 0) 595 /* 596 * We are very likely bogus in this case. 597 */ 598 printf(" 1st %s last acquired @ %s:%d\n", 599 w->w_name, w->w_file, w->w_line); 600 else 601 printf(" 1st %p %s @ %s:%d\n", lock2, 602 lock2->lo_name, lock2->lo_file, 603 lock2->lo_line); 604 printf(" 2nd %p %s @ %s:%d\n", 605 lock1, lock1->lo_name, lock1->lo_file, 606 lock1->lo_line); 607 printf(" 3rd %p %s @ %s:%d\n", 608 lock, lock->lo_name, file, line); 609#ifdef DDB 610 go_into_ddb = 1; 611#endif /* DDB */ 612 goto out; 613 } 614 } 615 lock1 = (*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 616 if (!itismychild(lock1->lo_witness, w)) 617 mtx_unlock_spin(&w_mtx); 618 619out: 620#ifdef DDB 621 if (witness_ddb && go_into_ddb) 622 Debugger("witness_enter"); 623#endif /* DDB */ 624 w->w_file = file; 625 w->w_line = line; 626 lock->lo_line = line; 627 lock->lo_file = file; 628 629 lle = *lock_list; 630 if (lle == NULL || lle->ll_count == LOCK_CHILDCOUNT) { 631 *lock_list = witness_lock_list_get(); 632 if (*lock_list == NULL) 633 return; 634 (*lock_list)->ll_next = lle; 635 lle = *lock_list; 636 } 637 lle->ll_children[lle->ll_count++] = lock; 638} 639 640void 641witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 642{ 643 struct lock_list_entry **lock_list, *lle; 644 struct lock_class *class; 645 struct proc *p; 646 int i, j; 647 648 if (witness_cold || witness_dead || lock->lo_witness == NULL || 649 panicstr) 650 return; 651 p = curproc; 652 class = lock->lo_class; 653 654 if (lock->lo_flags & LO_RECURSED) { 655 if ((lock->lo_flags & LO_LOCKED) == 0) 656 panic("%s: recursed lock (%s) %s is not locked @ %s:%d", 657 __func__, class->lc_name, lock->lo_name, file, 658 line); 659 return; 660 } 661 662 /* 663 * We don't need to protect this PCPU_GET() here against preemption 664 * because if we hold any spinlocks then we are already protected, 665 * and if we don't we will get NULL if we hold no spinlocks even if 666 * we switch CPU's while reading it. 667 */ 668 if (class->lc_flags & LC_SLEEPLOCK) { 669 if ((flags & LOP_NOSWITCH) == 0 && PCPU_GET(spinlocks) != NULL) 670 panic("switchable sleep unlock (%s) %s @ %s:%d", 671 class->lc_name, lock->lo_name, file, line); 672 lock_list = &p->p_sleeplocks; 673 } else 674 lock_list = PCPU_PTR(spinlocks); 675 676 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 677 for (i = 0; i < (*lock_list)->ll_count; i++) 678 if ((*lock_list)->ll_children[i] == lock) { 679 (*lock_list)->ll_count--; 680 for (j = i; j < (*lock_list)->ll_count; j++) 681 (*lock_list)->ll_children[j] = 682 (*lock_list)->ll_children[j + 1]; 683 if ((*lock_list)->ll_count == 0) { 684 lle = *lock_list; 685 *lock_list = lle->ll_next; 686 witness_lock_list_free(lle); 687 } 688 return; 689 } 690} 691 692/* 693 * Warn if any held locks are not sleepable. Note that Giant and the lock 694 * passed in are both special cases since they are both released during the 695 * sleep process and aren't actually held while the process is asleep. 696 */ 697int 698witness_sleep(int check_only, struct lock_object *lock, const char *file, 699 int line) 700{ 701 struct lock_list_entry **lock_list, *lle; 702 struct lock_object *lock1; 703 struct proc *p; 704 critical_t savecrit; 705 int i, n; 706 707 if (witness_dead || panicstr) 708 return (0); 709 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 710 n = 0; 711 /* 712 * Preemption bad because we need PCPU_PTR(spinlocks) to not change. 713 */ 714 savecrit = critical_enter(); 715 p = curproc; 716 lock_list = &p->p_sleeplocks; 717again: 718 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 719 for (i = lle->ll_count - 1; i >= 0; i--) { 720 lock1 = lle->ll_children[i]; 721 if (lock1 == lock || lock1 == &Giant.mtx_object || 722 (lock1->lo_flags & LO_SLEEPABLE)) 723 continue; 724 n++; 725 printf("%s:%d: %s with \"%s\" locked from %s:%d\n", 726 file, line, check_only ? "could sleep" : "sleeping", 727 lock1->lo_name, lock1->lo_file, lock1->lo_line); 728 } 729 if (lock_list == &p->p_sleeplocks) { 730 lock_list = PCPU_PTR(spinlocks); 731 goto again; 732 } 733#ifdef DDB 734 if (witness_ddb && n) 735 Debugger("witness_sleep"); 736#endif /* DDB */ 737 critical_exit(savecrit); 738 return (n); 739} 740 741static struct witness * 742enroll(const char *description, struct lock_class *lock_class) 743{ 744 struct witness *w; 745 746 if (!witness_watch) 747 return (NULL); 748 749 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 750 return (NULL); 751 mtx_lock_spin(&w_mtx); 752 STAILQ_FOREACH(w, &w_all, w_list) { 753 if (strcmp(description, w->w_name) == 0) { 754 mtx_unlock_spin(&w_mtx); 755 if (lock_class != w->w_class) 756 panic( 757 "lock (%s) %s does not match earlier (%s) lock", 758 description, lock_class->lc_name, 759 w->w_class->lc_name); 760 return (w); 761 } 762 } 763 /* 764 * This isn't quite right, as witness_cold is still 0 while we 765 * enroll all the locks initialized before witness_initialize(). 766 */ 767 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) 768 panic("spin lock %s not in order list", description); 769 if ((w = witness_get()) == NULL) 770 return (NULL); 771 w->w_name = description; 772 w->w_class = lock_class; 773 STAILQ_INSERT_HEAD(&w_all, w, w_list); 774 if (lock_class->lc_flags & LC_SPINLOCK) 775 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 776 else if (lock_class->lc_flags & LC_SLEEPLOCK) 777 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 778 else 779 panic("lock class %s is not sleep or spin", 780 lock_class->lc_name); 781 mtx_unlock_spin(&w_mtx); 782 783 return (w); 784} 785 786static int 787itismychild(struct witness *parent, struct witness *child) 788{ 789 static int recursed; 790 struct witness_child_list_entry **wcl; 791 struct witness_list *list; 792 793 MPASS(child != NULL && parent != NULL); 794 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 795 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 796 panic( 797 "%s: parent (%s) and child (%s) are not the same lock type", 798 __func__, parent->w_class->lc_name, 799 child->w_class->lc_name); 800 801 /* 802 * Insert "child" after "parent" 803 */ 804 wcl = &parent->w_children; 805 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 806 wcl = &(*wcl)->wcl_next; 807 808 if (*wcl == NULL) { 809 *wcl = witness_child_get(); 810 if (*wcl == NULL) 811 return (1); 812 } 813 814 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 815 816 /* 817 * Now prune whole tree. We look for cases where a lock is now 818 * both a descendant and a direct child of a given lock. In that 819 * case, we want to remove the direct child link from the tree. 820 */ 821 if (recursed) 822 return (0); 823 recursed = 1; 824 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 825 list = &w_sleep; 826 else 827 list = &w_spin; 828 STAILQ_FOREACH(child, list, w_typelist) { 829 STAILQ_FOREACH(parent, list, w_typelist) { 830 if (!isitmychild(parent, child)) 831 continue; 832 removechild(parent, child); 833 if (isitmydescendant(parent, child)) 834 continue; 835 itismychild(parent, child); 836 } 837 } 838 recursed = 0; 839 witness_levelall(); 840 return (0); 841} 842 843static void 844removechild(struct witness *parent, struct witness *child) 845{ 846 struct witness_child_list_entry **wcl, *wcl1; 847 int i; 848 849 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 850 for (i = 0; i < (*wcl)->wcl_count; i++) 851 if ((*wcl)->wcl_children[i] == child) 852 goto found; 853 return; 854found: 855 (*wcl)->wcl_count--; 856 if ((*wcl)->wcl_count > i) 857 (*wcl)->wcl_children[i] = 858 (*wcl)->wcl_children[(*wcl)->wcl_count]; 859 MPASS((*wcl)->wcl_children[i] != NULL); 860 861 if ((*wcl)->wcl_count != 0) 862 return; 863 864 wcl1 = *wcl; 865 *wcl = wcl1->wcl_next; 866 witness_child_free(wcl1); 867} 868 869static int 870isitmychild(struct witness *parent, struct witness *child) 871{ 872 struct witness_child_list_entry *wcl; 873 int i; 874 875 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 876 for (i = 0; i < wcl->wcl_count; i++) { 877 if (wcl->wcl_children[i] == child) 878 return (1); 879 } 880 } 881 return (0); 882} 883 884static int 885isitmydescendant(struct witness *parent, struct witness *child) 886{ 887 struct witness_child_list_entry *wcl; 888 int i, j; 889 890 if (isitmychild(parent, child)) 891 return (1); 892 j = 0; 893 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 894 MPASS(j < 1000); 895 for (i = 0; i < wcl->wcl_count; i++) { 896 if (isitmydescendant(wcl->wcl_children[i], child)) 897 return (1); 898 } 899 j++; 900 } 901 return (0); 902} 903 904void 905witness_levelall (void) 906{ 907 struct witness_list *list; 908 struct witness *w, *w1; 909 910 /* 911 * First clear all levels. 912 */ 913 STAILQ_FOREACH(w, &w_all, w_list) { 914 w->w_level = 0; 915 } 916 917 /* 918 * Look for locks with no parent and level all their descendants. 919 */ 920 STAILQ_FOREACH(w, &w_all, w_list) { 921 /* 922 * This is just an optimization, technically we could get 923 * away just walking the all list each time. 924 */ 925 if (w->w_class->lc_flags & LC_SLEEPLOCK) 926 list = &w_sleep; 927 else 928 list = &w_spin; 929 STAILQ_FOREACH(w1, list, w_typelist) { 930 if (isitmychild(w1, w)) 931 goto skip; 932 } 933 witness_leveldescendents(w, 0); 934 skip: 935 } 936} 937 938static void 939witness_leveldescendents(struct witness *parent, int level) 940{ 941 struct witness_child_list_entry *wcl; 942 int i; 943 944 if (parent->w_level < level) 945 parent->w_level = level; 946 level++; 947 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 948 for (i = 0; i < wcl->wcl_count; i++) 949 witness_leveldescendents(wcl->wcl_children[i], level); 950} 951 952static void 953witness_displaydescendants(void(*prnt)(const char *fmt, ...), 954 struct witness *parent) 955{ 956 struct witness_child_list_entry *wcl; 957 int i, level; 958 959 level = parent->w_level; 960 961 prnt("%-2d", level); 962 for (i = 0; i < level; i++) 963 prnt(" "); 964 prnt("%s", parent->w_name); 965 if (parent->w_file != NULL) 966 prnt(" -- last acquired @ %s:%d\n", parent->w_file, 967 parent->w_line); 968 969 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 970 for (i = 0; i < wcl->wcl_count; i++) 971 witness_displaydescendants(prnt, 972 wcl->wcl_children[i]); 973} 974 975static int 976dup_ok(struct witness *w) 977{ 978 const char **dup; 979 980 for (dup = dup_list; *dup != NULL; dup++) 981 if (strcmp(w->w_name, *dup) == 0) 982 return (1); 983 return (0); 984} 985 986static int 987blessed(struct witness *w1, struct witness *w2) 988{ 989 int i; 990 struct witness_blessed *b; 991 992 for (i = 0; i < blessed_count; i++) { 993 b = &blessed_list[i]; 994 if (strcmp(w1->w_name, b->b_lock1) == 0) { 995 if (strcmp(w2->w_name, b->b_lock2) == 0) 996 return (1); 997 continue; 998 } 999 if (strcmp(w1->w_name, b->b_lock2) == 0) 1000 if (strcmp(w2->w_name, b->b_lock1) == 0) 1001 return (1); 1002 } 1003 return (0); 1004} 1005 1006static struct witness * 1007witness_get(void) 1008{ 1009 struct witness *w; 1010 1011 if (STAILQ_EMPTY(&w_free)) { 1012 witness_dead = 1; 1013 mtx_unlock_spin(&w_mtx); 1014 printf("%s: witness exhausted\n", __func__); 1015 return (NULL); 1016 } 1017 w = STAILQ_FIRST(&w_free); 1018 STAILQ_REMOVE_HEAD(&w_free, w_list); 1019 bzero(w, sizeof(*w)); 1020 return (w); 1021} 1022 1023static void 1024witness_free(struct witness *w) 1025{ 1026 1027 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1028} 1029 1030static struct witness_child_list_entry * 1031witness_child_get(void) 1032{ 1033 struct witness_child_list_entry *wcl; 1034 1035 wcl = w_child_free; 1036 if (wcl == NULL) { 1037 witness_dead = 1; 1038 mtx_unlock_spin(&w_mtx); 1039 printf("%s: witness exhausted\n", __func__); 1040 return (NULL); 1041 } 1042 w_child_free = wcl->wcl_next; 1043 bzero(wcl, sizeof(*wcl)); 1044 return (wcl); 1045} 1046 1047static void 1048witness_child_free(struct witness_child_list_entry *wcl) 1049{ 1050 1051 wcl->wcl_next = w_child_free; 1052 w_child_free = wcl; 1053} 1054 1055static struct lock_list_entry * 1056witness_lock_list_get(void) 1057{ 1058 struct lock_list_entry *lle; 1059 1060 mtx_lock_spin(&w_mtx); 1061 lle = w_lock_list_free; 1062 if (lle == NULL) { 1063 witness_dead = 1; 1064 mtx_unlock_spin(&w_mtx); 1065 printf("%s: witness exhausted\n", __func__); 1066 return (NULL); 1067 } 1068 w_lock_list_free = lle->ll_next; 1069 mtx_unlock_spin(&w_mtx); 1070 bzero(lle, sizeof(*lle)); 1071 return (lle); 1072} 1073 1074static void 1075witness_lock_list_free(struct lock_list_entry *lle) 1076{ 1077 1078 mtx_lock_spin(&w_mtx); 1079 lle->ll_next = w_lock_list_free; 1080 w_lock_list_free = lle; 1081 mtx_unlock_spin(&w_mtx); 1082} 1083 1084/* 1085 * Calling this on p != curproc is bad unless we are in ddb. 1086 */ 1087int 1088witness_list(struct proc *p) 1089{ 1090 struct lock_list_entry **lock_list, *lle; 1091 struct lock_object *lock; 1092 critical_t savecrit; 1093 int i, nheld; 1094 1095 KASSERT(p == curproc || db_active, 1096 ("%s: p != curproc and we aren't in the debugger", __func__)); 1097 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1098 nheld = 0; 1099 /* 1100 * Preemption bad because we need PCPU_PTR(spinlocks) to not change. 1101 */ 1102 savecrit = critical_enter(); 1103 lock_list = &p->p_sleeplocks; 1104again: 1105 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1106 for (i = lle->ll_count - 1; i >= 0; i--) { 1107 lock = lle->ll_children[i]; 1108 printf("\t(%s) %s (%p) locked at %s:%d\n", 1109 lock->lo_class->lc_name, lock->lo_name, lock, 1110 lock->lo_file, lock->lo_line); 1111 nheld++; 1112 } 1113 /* 1114 * We only handle spinlocks if p == curproc. This is somewhat broken 1115 * if p is currently executing on some other CPU and holds spin locks 1116 * as we won't display those locks. 1117 */ 1118 if (lock_list == &p->p_sleeplocks && p == curproc) { 1119 lock_list = PCPU_PTR(spinlocks); 1120 goto again; 1121 } 1122 critical_exit(savecrit); 1123 1124 return (nheld); 1125} 1126 1127void 1128witness_save(struct lock_object *lock, const char **filep, int *linep) 1129{ 1130 1131 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 1132 if (lock->lo_witness == NULL) 1133 return; 1134 1135 *filep = lock->lo_file; 1136 *linep = lock->lo_line; 1137} 1138 1139void 1140witness_restore(struct lock_object *lock, const char *file, int line) 1141{ 1142 1143 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 1144 if (lock->lo_witness == NULL) 1145 return; 1146 1147 lock->lo_witness->w_file = file; 1148 lock->lo_witness->w_line = line; 1149 lock->lo_file = file; 1150 lock->lo_line = line; 1151} 1152 1153#ifdef DDB 1154 1155DB_SHOW_COMMAND(locks, db_witness_list) 1156{ 1157 1158 witness_list(curproc); 1159} 1160 1161DB_SHOW_COMMAND(witness, db_witness_display) 1162{ 1163 1164 witness_display(db_printf); 1165} 1166#endif 1167