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