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