subr_witness.c revision 82244
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 82244 2001-08-23 22:47:05Z 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, ...)); 142static struct lock_instance *find_instance(struct lock_list_entry *lock_list, 143 struct lock_object *lock); 144 145MALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 146 147static int witness_watch = 1; 148TUNABLE_INT("debug.witness_watch", &witness_watch); 149SYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 150 151#ifdef DDB 152/* 153 * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 154 * drop into kdebug() when: 155 * - a lock heirarchy violation occurs 156 * - locks are held when going to sleep. 157 */ 158#ifdef WITNESS_DDB 159int witness_ddb = 1; 160#else 161int witness_ddb = 0; 162#endif 163TUNABLE_INT("debug.witness_ddb", &witness_ddb); 164SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 165#endif /* DDB */ 166 167#ifdef WITNESS_SKIPSPIN 168int witness_skipspin = 1; 169#else 170int witness_skipspin = 0; 171#endif 172TUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 173SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 174 ""); 175 176static struct mtx w_mtx; 177static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 178static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 179static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 180static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 181static struct witness_child_list_entry *w_child_free = NULL; 182static struct lock_list_entry *w_lock_list_free = NULL; 183static int witness_dead; /* fatal error, probably no memory */ 184 185static struct witness w_data[WITNESS_COUNT]; 186static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 187static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 188 189static struct witness_order_list_entry order_lists[] = { 190 { "Giant", &lock_class_mtx_sleep }, 191 { "proctree", &lock_class_sx }, 192 { "allproc", &lock_class_sx }, 193 { "process lock", &lock_class_mtx_sleep }, 194 { "uidinfo hash", &lock_class_mtx_sleep }, 195 { "uidinfo struct", &lock_class_mtx_sleep }, 196 { NULL, NULL }, 197 /* 198 * spin locks 199 */ 200#if defined(__i386__) && defined (SMP) 201 { "com", &lock_class_mtx_spin }, 202#endif 203 { "sio", &lock_class_mtx_spin }, 204#ifdef __i386__ 205 { "cy", &lock_class_mtx_spin }, 206#endif 207 { "ng_node", &lock_class_mtx_spin }, 208 { "ng_worklist", &lock_class_mtx_spin }, 209 { "ithread table lock", &lock_class_mtx_spin }, 210 { "sched lock", &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 { "clk", &lock_class_mtx_spin }, 223 { NULL, NULL }, 224 { NULL, NULL } 225}; 226 227static const char *dup_list[] = { 228 "process lock", 229 NULL 230}; 231 232/* 233 * Pairs of locks which have been blessed 234 * Don't complain about order problems with blessed locks 235 */ 236static struct witness_blessed blessed_list[] = { 237}; 238static int blessed_count = 239 sizeof(blessed_list) / sizeof(struct witness_blessed); 240 241/* 242 * List of all locks in the system. 243 */ 244STAILQ_HEAD(, lock_object) all_locks = STAILQ_HEAD_INITIALIZER(all_locks); 245 246static struct mtx all_mtx = { 247 { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 248 "All locks list", /* mtx_object.lo_name */ 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 CTR0(KTR_WITNESS, __func__ ": initializing witness"); 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 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 352 (class->lc_flags & LC_UPGRADABLE) == 0) 353 panic("%s: lock (%s) %s can not be upgradable!\n", __func__, 354 class->lc_name, lock->lo_name); 355 356 mtx_lock(&all_mtx); 357 STAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 358 lock->lo_flags |= LO_INITIALIZED; 359 lock_cur_cnt++; 360 if (lock_cur_cnt > lock_max_cnt) 361 lock_max_cnt = lock_cur_cnt; 362 mtx_unlock(&all_mtx); 363 if (!witness_cold && !witness_dead && panicstr == NULL && 364 (lock->lo_flags & LO_WITNESS) != 0) 365 lock->lo_witness = enroll(lock->lo_name, class); 366 else 367 lock->lo_witness = NULL; 368} 369 370void 371witness_destroy(struct lock_object *lock) 372{ 373 struct witness *w; 374 375 if (witness_cold) 376 panic("lock (%s) %s destroyed while witness_cold", 377 lock->lo_class->lc_name, lock->lo_name); 378 379 if ((lock->lo_flags & LO_INITIALIZED) == 0) 380 panic("%s: lock (%s) %s is not initialized!\n", __func__, 381 lock->lo_class->lc_name, lock->lo_name); 382 383 /* XXX: need to verify that no one holds the lock */ 384 w = lock->lo_witness; 385 if (w != NULL) { 386 mtx_lock_spin(&w_mtx); 387 w->w_refcount--; 388 if (w->w_refcount == 0) { 389 CTR1(KTR_WITNESS, 390 __func__ ": marking witness %s as dead", w->w_name); 391 w->w_name = "(dead)"; 392 w->w_file = "(dead)"; 393 w->w_line = 0; 394 } 395 mtx_unlock_spin(&w_mtx); 396 } 397 398 mtx_lock(&all_mtx); 399 lock_cur_cnt--; 400 STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list); 401 lock->lo_flags &= ~LO_INITIALIZED; 402 mtx_unlock(&all_mtx); 403} 404 405static void 406witness_display_list(void(*prnt)(const char *fmt, ...), 407 struct witness_list *list) 408{ 409 struct witness *w, *w1; 410 int found; 411 412 STAILQ_FOREACH(w, list, w_typelist) { 413 if (w->w_file == NULL) 414 continue; 415 found = 0; 416 STAILQ_FOREACH(w1, list, w_typelist) { 417 if (isitmychild(w1, w)) { 418 found++; 419 break; 420 } 421 } 422 if (found) 423 continue; 424 /* 425 * This lock has no anscestors, display its descendants. 426 */ 427 witness_displaydescendants(prnt, w); 428 } 429} 430 431static void 432witness_display(void(*prnt)(const char *fmt, ...)) 433{ 434 struct witness *w; 435 436 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 437 witness_levelall(); 438 439 /* 440 * First, handle sleep locks which have been acquired at least 441 * once. 442 */ 443 prnt("Sleep locks:\n"); 444 witness_display_list(prnt, &w_sleep); 445 446 /* 447 * Now do spin locks which have been acquired at least once. 448 */ 449 prnt("\nSpin locks:\n"); 450 witness_display_list(prnt, &w_spin); 451 452 /* 453 * Finally, any locks which have not been acquired yet. 454 */ 455 prnt("\nLocks which were never acquired:\n"); 456 STAILQ_FOREACH(w, &w_all, w_list) { 457 if (w->w_file != NULL) 458 continue; 459 prnt("%s\n", w->w_name); 460 } 461} 462 463void 464witness_lock(struct lock_object *lock, int flags, const char *file, int line) 465{ 466 struct lock_list_entry **lock_list, *lle; 467 struct lock_instance *lock1, *lock2; 468 struct lock_class *class; 469 struct witness *w, *w1; 470 struct proc *p; 471 int i, j; 472#ifdef DDB 473 int go_into_ddb = 0; 474#endif /* DDB */ 475 476 if (witness_cold || witness_dead || lock->lo_witness == NULL || 477 panicstr != NULL) 478 return; 479 w = lock->lo_witness; 480 class = lock->lo_class; 481 p = curproc; 482 483 /* 484 * We have to hold a spinlock to keep lock_list valid across the check 485 * in the LC_SLEEPLOCK case. In the LC_SPINLOCK case, it is already 486 * protected by the spinlock we are currently performing the witness 487 * checks on, so it is ok to release the lock after performing this 488 * check. All we have to protect is the LC_SLEEPLOCK case when no 489 * spinlocks are held as we may get preempted during this check and 490 * lock_list could end up pointing to some other CPU's spinlock list. 491 */ 492 mtx_lock_spin(&w_mtx); 493 lock_list = PCPU_PTR(spinlocks); 494 if (class->lc_flags & LC_SLEEPLOCK) { 495 if (*lock_list != NULL && (flags & LOP_TRYLOCK) == 0) { 496 mtx_unlock_spin(&w_mtx); 497 panic("blockable sleep lock (%s) %s @ %s:%d", 498 class->lc_name, lock->lo_name, file, line); 499 } 500 lock_list = &p->p_sleeplocks; 501 } 502 mtx_unlock_spin(&w_mtx); 503 504 /* 505 * Try locks do not block if they fail to acquire the lock, thus 506 * there is no danger of deadlocks or of switching while holding a 507 * spin lock if we acquire a lock via a try operation. 508 */ 509 if (flags & LOP_TRYLOCK) 510 goto out; 511 512 /* 513 * Is this the first lock acquired? If so, then no order checking 514 * is needed. 515 */ 516 if (*lock_list == NULL) 517 goto out; 518 519 /* 520 * Check to see if we are recursing on a lock we already own. 521 */ 522 lock1 = find_instance(*lock_list, lock); 523 if (lock1 != NULL) { 524 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 525 (flags & LOP_EXCLUSIVE) == 0) { 526 printf("shared lock of (%s) %s @ %s:%d\n", 527 class->lc_name, lock->lo_name, file, line); 528 printf("while exclusively locked from %s:%d\n", 529 lock1->li_file, lock1->li_line); 530 panic("share->excl"); 531 } 532 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 533 (flags & LOP_EXCLUSIVE) != 0) { 534 printf("exclusive lock of (%s) %s @ %s:%d\n", 535 class->lc_name, lock->lo_name, file, line); 536 printf("while share locked from %s:%d\n", 537 lock1->li_file, lock1->li_line); 538 panic("excl->share"); 539 } 540 lock1->li_flags++; 541 if ((lock->lo_flags & LO_RECURSABLE) == 0) { 542 printf( 543 "recursed on non-recursive lock (%s) %s @ %s:%d\n", 544 class->lc_name, lock->lo_name, file, line); 545 printf("first acquired @ %s:%d\n", lock1->li_file, 546 lock1->li_line); 547 panic("recurse"); 548 } 549 CTR3(KTR_WITNESS, __func__ ": pid %d recursed on %s r=%d", 550 curproc->p_pid, lock->lo_name, 551 lock1->li_flags & LI_RECURSEMASK); 552 lock1->li_file = file; 553 lock1->li_line = line; 554 return; 555 } 556 557 /* 558 * Check for duplicate locks of the same type. Note that we only 559 * have to check for this on the last lock we just acquired. Any 560 * other cases will be caught as lock order violations. 561 */ 562 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 563 w1 = lock1->li_lock->lo_witness; 564 if (w1 == w) { 565 if (w->w_same_squawked || dup_ok(w)) 566 goto out; 567 w->w_same_squawked = 1; 568 printf("acquiring duplicate lock of same type: \"%s\"\n", 569 lock->lo_name); 570 printf(" 1st @ %s:%d\n", lock1->li_file, lock1->li_line); 571 printf(" 2nd @ %s:%d\n", file, line); 572#ifdef DDB 573 go_into_ddb = 1; 574#endif /* DDB */ 575 goto out; 576 } 577 MPASS(!mtx_owned(&w_mtx)); 578 mtx_lock_spin(&w_mtx); 579 /* 580 * If we have a known higher number just say ok 581 */ 582 if (witness_watch > 1 && w->w_level > w1->w_level) { 583 mtx_unlock_spin(&w_mtx); 584 goto out; 585 } 586 if (isitmydescendant(w1, w)) { 587 mtx_unlock_spin(&w_mtx); 588 goto out; 589 } 590 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 591 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 592 593 MPASS(j < WITNESS_COUNT); 594 lock1 = &lle->ll_children[i]; 595 w1 = lock1->li_lock->lo_witness; 596 597 /* 598 * If this lock doesn't undergo witness checking, 599 * then skip it. 600 */ 601 if (w1 == NULL) { 602 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 603 ("lock missing witness structure")); 604 continue; 605 } 606 /* 607 * If we are locking Giant and we slept with this 608 * lock, then skip it. 609 */ 610 if ((lock1->li_flags & LI_SLEPT) != 0 && 611 lock == &Giant.mtx_object) 612 continue; 613 if (!isitmydescendant(w, w1)) 614 continue; 615 /* 616 * We have a lock order violation, check to see if it 617 * is allowed or has already been yelled about. 618 */ 619 mtx_unlock_spin(&w_mtx); 620 if (blessed(w, w1)) 621 goto out; 622 if (lock1->li_lock == &Giant.mtx_object) { 623 if (w1->w_Giant_squawked) 624 goto out; 625 else 626 w1->w_Giant_squawked = 1; 627 } else { 628 if (w1->w_other_squawked) 629 goto out; 630 else 631 w1->w_other_squawked = 1; 632 } 633 /* 634 * Ok, yell about it. 635 */ 636 printf("lock order reversal\n"); 637 /* 638 * Try to locate an earlier lock with 639 * witness w in our list. 640 */ 641 do { 642 lock2 = &lle->ll_children[i]; 643 MPASS(lock2->li_lock != NULL); 644 if (lock2->li_lock->lo_witness == w) 645 break; 646 i--; 647 if (i == 0 && lle->ll_next != NULL) { 648 lle = lle->ll_next; 649 i = lle->ll_count - 1; 650 MPASS(i != 0); 651 } 652 } while (i >= 0); 653 if (i < 0) { 654 printf(" 1st %p %s @ %s:%d\n", lock1->li_lock, 655 lock1->li_lock->lo_name, lock1->li_file, 656 lock1->li_line); 657 printf(" 2nd %p %s @ %s:%d\n", lock, 658 lock->lo_name, file, line); 659 } else { 660 printf(" 1st %p %s @ %s:%d\n", lock2->li_lock, 661 lock2->li_lock->lo_name, lock2->li_file, 662 lock2->li_line); 663 printf(" 2nd %p %s @ %s:%d\n", lock1->li_lock, 664 lock1->li_lock->lo_name, lock1->li_file, 665 lock1->li_line); 666 printf(" 3rd %p %s @ %s:%d\n", lock, 667 lock->lo_name, file, line); 668 } 669#ifdef DDB 670 go_into_ddb = 1; 671#endif /* DDB */ 672 goto out; 673 } 674 } 675 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 676 /* 677 * Don't build a new relationship if we are locking Giant just 678 * after waking up and the previous lock in the list was acquired 679 * prior to blocking. 680 */ 681 if (lock == &Giant.mtx_object && (lock1->li_flags & LI_SLEPT) != 0) 682 mtx_unlock_spin(&w_mtx); 683 else { 684 CTR2(KTR_WITNESS, __func__ ": adding %s as a child of %s", 685 lock->lo_name, lock1->li_lock->lo_name); 686 if (!itismychild(lock1->li_lock->lo_witness, w)) 687 mtx_unlock_spin(&w_mtx); 688 } 689 690out: 691#ifdef DDB 692 if (witness_ddb && go_into_ddb) 693 Debugger(__func__); 694#endif /* DDB */ 695 w->w_file = file; 696 w->w_line = line; 697 698 lle = *lock_list; 699 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 700 lle = witness_lock_list_get(); 701 if (lle == NULL) 702 return; 703 lle->ll_next = *lock_list; 704 CTR2(KTR_WITNESS, __func__ ": pid %d added lle %p", 705 curproc->p_pid, lle); 706 *lock_list = lle; 707 } 708 lock1 = &lle->ll_children[lle->ll_count++]; 709 lock1->li_lock = lock; 710 lock1->li_line = line; 711 lock1->li_file = file; 712 if ((flags & LOP_EXCLUSIVE) != 0) 713 lock1->li_flags = LI_EXCLUSIVE; 714 else 715 lock1->li_flags = 0; 716 CTR3(KTR_WITNESS, __func__ ": pid %d added %s as lle[%d]", 717 curproc->p_pid, lock->lo_name, lle->ll_count - 1); 718} 719 720void 721witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 722{ 723 struct lock_instance *instance; 724 struct lock_class *class; 725 726 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 727 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 728 return; 729 730 class = lock->lo_class; 731 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 732 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 733 class->lc_name, lock->lo_name, file, line); 734 if ((flags & LOP_TRYLOCK) == 0) 735 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 736 lock->lo_name, file, line); 737 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 738 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 739 class->lc_name, lock->lo_name, file, line); 740 instance = find_instance(curproc->p_sleeplocks, lock); 741 if (instance == NULL) 742 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 743 class->lc_name, lock->lo_name, file, line); 744 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 745 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 746 class->lc_name, lock->lo_name, file, line); 747 if ((instance->li_flags & LI_RECURSEMASK) != 0) 748 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 749 class->lc_name, lock->lo_name, 750 instance->li_flags & LI_RECURSEMASK, file, line); 751 instance->li_flags |= LI_EXCLUSIVE; 752} 753 754void 755witness_downgrade(struct lock_object *lock, int flags, const char *file, 756 int line) 757{ 758 struct lock_instance *instance; 759 struct lock_class *class; 760 761 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 762 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 763 return; 764 765 class = lock->lo_class; 766 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 767 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 768 class->lc_name, lock->lo_name, file, line); 769 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 770 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 771 class->lc_name, lock->lo_name, file, line); 772 instance = find_instance(curproc->p_sleeplocks, lock); 773 if (instance == NULL) 774 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 775 class->lc_name, lock->lo_name, file, line); 776 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 777 panic("downgrade of shared lock (%s) %s @ %s:%d", 778 class->lc_name, lock->lo_name, file, line); 779 if ((instance->li_flags & LI_RECURSEMASK) != 0) 780 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 781 class->lc_name, lock->lo_name, 782 instance->li_flags & LI_RECURSEMASK, file, line); 783 instance->li_flags &= ~LI_EXCLUSIVE; 784} 785 786void 787witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 788{ 789 struct lock_list_entry **lock_list, *lle; 790 struct lock_instance *instance; 791 struct lock_class *class; 792 struct proc *p; 793 critical_t s; 794 int i, j; 795 796 if (witness_cold || witness_dead || lock->lo_witness == NULL || 797 panicstr != NULL) 798 return; 799 p = curproc; 800 class = lock->lo_class; 801 if (class->lc_flags & LC_SLEEPLOCK) 802 lock_list = &p->p_sleeplocks; 803 else 804 lock_list = PCPU_PTR(spinlocks); 805 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 806 for (i = 0; i < (*lock_list)->ll_count; i++) { 807 instance = &(*lock_list)->ll_children[i]; 808 if (instance->li_lock == lock) { 809 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 810 (flags & LOP_EXCLUSIVE) == 0) { 811 printf( 812 "shared unlock of (%s) %s @ %s:%d\n", 813 class->lc_name, lock->lo_name, 814 file, line); 815 printf( 816 "while exclusively locked from %s:%d\n", 817 instance->li_file, 818 instance->li_line); 819 panic("excl->ushare"); 820 } 821 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 822 (flags & LOP_EXCLUSIVE) != 0) { 823 printf( 824 "exclusive unlock of (%s) %s @ %s:%d\n", 825 class->lc_name, lock->lo_name, 826 file, line); 827 printf( 828 "while share locked from %s:%d\n", 829 instance->li_file, 830 instance->li_line); 831 panic("share->uexcl"); 832 } 833 /* If we are recursed, unrecurse. */ 834 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 835 CTR3(KTR_WITNESS, 836 __func__ ": pid %d unrecursed on %s r=%d", 837 curproc->p_pid, 838 instance->li_lock->lo_name, 839 instance->li_flags); 840 instance->li_flags--; 841 goto out; 842 } 843 s = critical_enter(); 844 CTR3(KTR_WITNESS, 845 __func__ ": pid %d removed %s from lle[%d]", 846 curproc->p_pid, instance->li_lock->lo_name, 847 (*lock_list)->ll_count - 1); 848 (*lock_list)->ll_count--; 849 for (j = i; j < (*lock_list)->ll_count; j++) 850 (*lock_list)->ll_children[j] = 851 (*lock_list)->ll_children[j + 1]; 852 critical_exit(s); 853 if ((*lock_list)->ll_count == 0) { 854 lle = *lock_list; 855 *lock_list = lle->ll_next; 856 CTR2(KTR_WITNESS, 857 __func__ ": pid %d removed lle %p", 858 curproc->p_pid, lle); 859 witness_lock_list_free(lle); 860 } 861 goto out; 862 } 863 } 864 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 865 file, line); 866out: 867 /* 868 * We don't need to protect this PCPU_GET() here against preemption 869 * because if we hold any spinlocks then we are already protected, 870 * and if we don't we will get NULL if we hold no spinlocks even if 871 * we switch CPU's while reading it. 872 */ 873 if (class->lc_flags & LC_SLEEPLOCK) { 874 if ((flags & LOP_NOSWITCH) == 0 && PCPU_GET(spinlocks) != NULL) 875 panic("switchable sleep unlock (%s) %s @ %s:%d", 876 class->lc_name, lock->lo_name, file, line); 877 } 878} 879 880/* 881 * Warn if any held locks are not sleepable. Note that Giant and the lock 882 * passed in are both special cases since they are both released during the 883 * sleep process and aren't actually held while the process is asleep. 884 */ 885int 886witness_sleep(int check_only, struct lock_object *lock, const char *file, 887 int line) 888{ 889 struct lock_list_entry **lock_list, *lle; 890 struct lock_instance *lock1; 891 struct proc *p; 892 critical_t savecrit; 893 int i, n; 894 895 if (witness_dead || panicstr != NULL) 896 return (0); 897 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 898 n = 0; 899 /* 900 * Preemption bad because we need PCPU_PTR(spinlocks) to not change. 901 */ 902 savecrit = critical_enter(); 903 p = curproc; 904 lock_list = &p->p_sleeplocks; 905again: 906 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 907 for (i = lle->ll_count - 1; i >= 0; i--) { 908 lock1 = &lle->ll_children[i]; 909 if (lock1->li_lock == lock || 910 lock1->li_lock == &Giant.mtx_object) 911 continue; 912 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) { 913 if (check_only == 0) { 914 CTR3(KTR_WITNESS, 915 "pid %d: sleeping with lock (%s) %s held", 916 curproc->p_pid, 917 lock1->li_lock->lo_class->lc_name, 918 lock1->li_lock->lo_name); 919 lock1->li_flags |= LI_SLEPT; 920 } 921 continue; 922 } 923 n++; 924 printf("%s:%d: %s with \"%s\" locked from %s:%d\n", 925 file, line, check_only ? "could sleep" : "sleeping", 926 lock1->li_lock->lo_name, lock1->li_file, 927 lock1->li_line); 928 } 929 if (lock_list == &p->p_sleeplocks) { 930 lock_list = PCPU_PTR(spinlocks); 931 goto again; 932 } 933#ifdef DDB 934 if (witness_ddb && n) 935 Debugger(__func__); 936#endif /* DDB */ 937 critical_exit(savecrit); 938 return (n); 939} 940 941static struct witness * 942enroll(const char *description, struct lock_class *lock_class) 943{ 944 struct witness *w; 945 946 if (!witness_watch || witness_dead || panicstr != NULL) 947 return (NULL); 948 949 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 950 return (NULL); 951 mtx_lock_spin(&w_mtx); 952 STAILQ_FOREACH(w, &w_all, w_list) { 953 if (strcmp(description, w->w_name) == 0) { 954 w->w_refcount++; 955 mtx_unlock_spin(&w_mtx); 956 if (lock_class != w->w_class) 957 panic( 958 "lock (%s) %s does not match earlier (%s) lock", 959 description, lock_class->lc_name, 960 w->w_class->lc_name); 961 return (w); 962 } 963 } 964 /* 965 * This isn't quite right, as witness_cold is still 0 while we 966 * enroll all the locks initialized before witness_initialize(). 967 */ 968 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 969 mtx_unlock_spin(&w_mtx); 970 panic("spin lock %s not in order list", description); 971 } 972 if ((w = witness_get()) == NULL) 973 return (NULL); 974 w->w_name = description; 975 w->w_class = lock_class; 976 w->w_refcount = 1; 977 STAILQ_INSERT_HEAD(&w_all, w, w_list); 978 if (lock_class->lc_flags & LC_SPINLOCK) 979 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 980 else if (lock_class->lc_flags & LC_SLEEPLOCK) 981 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 982 else { 983 mtx_unlock_spin(&w_mtx); 984 panic("lock class %s is not sleep or spin", 985 lock_class->lc_name); 986 } 987 mtx_unlock_spin(&w_mtx); 988 989 return (w); 990} 991 992static int 993itismychild(struct witness *parent, struct witness *child) 994{ 995 static int recursed; 996 struct witness_child_list_entry **wcl; 997 struct witness_list *list; 998 999 MPASS(child != NULL && parent != NULL); 1000 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1001 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1002 panic( 1003 "%s: parent (%s) and child (%s) are not the same lock type", 1004 __func__, parent->w_class->lc_name, 1005 child->w_class->lc_name); 1006 1007 /* 1008 * Insert "child" after "parent" 1009 */ 1010 wcl = &parent->w_children; 1011 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 1012 wcl = &(*wcl)->wcl_next; 1013 1014 if (*wcl == NULL) { 1015 *wcl = witness_child_get(); 1016 if (*wcl == NULL) 1017 return (1); 1018 } 1019 1020 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1021 1022 /* 1023 * Now prune whole tree. We look for cases where a lock is now 1024 * both a descendant and a direct child of a given lock. In that 1025 * case, we want to remove the direct child link from the tree. 1026 */ 1027 if (recursed) 1028 return (0); 1029 recursed = 1; 1030 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1031 list = &w_sleep; 1032 else 1033 list = &w_spin; 1034 STAILQ_FOREACH(child, list, w_typelist) { 1035 STAILQ_FOREACH(parent, list, w_typelist) { 1036 if (!isitmychild(parent, child)) 1037 continue; 1038 removechild(parent, child); 1039 if (isitmydescendant(parent, child)) 1040 continue; 1041 itismychild(parent, child); 1042 } 1043 } 1044 recursed = 0; 1045 witness_levelall(); 1046 return (0); 1047} 1048 1049static void 1050removechild(struct witness *parent, struct witness *child) 1051{ 1052 struct witness_child_list_entry **wcl, *wcl1; 1053 int i; 1054 1055 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1056 for (i = 0; i < (*wcl)->wcl_count; i++) 1057 if ((*wcl)->wcl_children[i] == child) 1058 goto found; 1059 return; 1060found: 1061 (*wcl)->wcl_count--; 1062 if ((*wcl)->wcl_count > i) 1063 (*wcl)->wcl_children[i] = 1064 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1065 MPASS((*wcl)->wcl_children[i] != NULL); 1066 1067 if ((*wcl)->wcl_count != 0) 1068 return; 1069 1070 wcl1 = *wcl; 1071 *wcl = wcl1->wcl_next; 1072 witness_child_free(wcl1); 1073} 1074 1075static int 1076isitmychild(struct witness *parent, struct witness *child) 1077{ 1078 struct witness_child_list_entry *wcl; 1079 int i; 1080 1081 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1082 for (i = 0; i < wcl->wcl_count; i++) { 1083 if (wcl->wcl_children[i] == child) 1084 return (1); 1085 } 1086 } 1087 return (0); 1088} 1089 1090static int 1091isitmydescendant(struct witness *parent, struct witness *child) 1092{ 1093 struct witness_child_list_entry *wcl; 1094 int i, j; 1095 1096 if (isitmychild(parent, child)) 1097 return (1); 1098 j = 0; 1099 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1100 MPASS(j < 1000); 1101 for (i = 0; i < wcl->wcl_count; i++) { 1102 if (isitmydescendant(wcl->wcl_children[i], child)) 1103 return (1); 1104 } 1105 j++; 1106 } 1107 return (0); 1108} 1109 1110void 1111witness_levelall (void) 1112{ 1113 struct witness_list *list; 1114 struct witness *w, *w1; 1115 1116 /* 1117 * First clear all levels. 1118 */ 1119 STAILQ_FOREACH(w, &w_all, w_list) { 1120 w->w_level = 0; 1121 } 1122 1123 /* 1124 * Look for locks with no parent and level all their descendants. 1125 */ 1126 STAILQ_FOREACH(w, &w_all, w_list) { 1127 /* 1128 * This is just an optimization, technically we could get 1129 * away just walking the all list each time. 1130 */ 1131 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1132 list = &w_sleep; 1133 else 1134 list = &w_spin; 1135 STAILQ_FOREACH(w1, list, w_typelist) { 1136 if (isitmychild(w1, w)) 1137 goto skip; 1138 } 1139 witness_leveldescendents(w, 0); 1140 skip: 1141 } 1142} 1143 1144static void 1145witness_leveldescendents(struct witness *parent, int level) 1146{ 1147 struct witness_child_list_entry *wcl; 1148 int i; 1149 1150 if (parent->w_level < level) 1151 parent->w_level = level; 1152 level++; 1153 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1154 for (i = 0; i < wcl->wcl_count; i++) 1155 witness_leveldescendents(wcl->wcl_children[i], level); 1156} 1157 1158static void 1159witness_displaydescendants(void(*prnt)(const char *fmt, ...), 1160 struct witness *parent) 1161{ 1162 struct witness_child_list_entry *wcl; 1163 int i, level; 1164 1165 level = parent->w_level; 1166 1167 prnt("%-2d", level); 1168 for (i = 0; i < level; i++) 1169 prnt(" "); 1170 prnt("%s", parent->w_name); 1171 if (parent->w_file != NULL) 1172 prnt(" -- last acquired @ %s:%d\n", parent->w_file, 1173 parent->w_line); 1174 1175 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1176 for (i = 0; i < wcl->wcl_count; i++) 1177 witness_displaydescendants(prnt, 1178 wcl->wcl_children[i]); 1179} 1180 1181static int 1182dup_ok(struct witness *w) 1183{ 1184 const char **dup; 1185 1186 for (dup = dup_list; *dup != NULL; dup++) 1187 if (strcmp(w->w_name, *dup) == 0) 1188 return (1); 1189 return (0); 1190} 1191 1192static int 1193blessed(struct witness *w1, struct witness *w2) 1194{ 1195 int i; 1196 struct witness_blessed *b; 1197 1198 for (i = 0; i < blessed_count; i++) { 1199 b = &blessed_list[i]; 1200 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1201 if (strcmp(w2->w_name, b->b_lock2) == 0) 1202 return (1); 1203 continue; 1204 } 1205 if (strcmp(w1->w_name, b->b_lock2) == 0) 1206 if (strcmp(w2->w_name, b->b_lock1) == 0) 1207 return (1); 1208 } 1209 return (0); 1210} 1211 1212static struct witness * 1213witness_get(void) 1214{ 1215 struct witness *w; 1216 1217 if (witness_dead) { 1218 mtx_unlock_spin(&w_mtx); 1219 return (NULL); 1220 } 1221 if (STAILQ_EMPTY(&w_free)) { 1222 witness_dead = 1; 1223 mtx_unlock_spin(&w_mtx); 1224 printf("%s: witness exhausted\n", __func__); 1225 return (NULL); 1226 } 1227 w = STAILQ_FIRST(&w_free); 1228 STAILQ_REMOVE_HEAD(&w_free, w_list); 1229 bzero(w, sizeof(*w)); 1230 return (w); 1231} 1232 1233static void 1234witness_free(struct witness *w) 1235{ 1236 1237 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1238} 1239 1240static struct witness_child_list_entry * 1241witness_child_get(void) 1242{ 1243 struct witness_child_list_entry *wcl; 1244 1245 if (witness_dead) { 1246 mtx_unlock_spin(&w_mtx); 1247 return (NULL); 1248 } 1249 wcl = w_child_free; 1250 if (wcl == NULL) { 1251 witness_dead = 1; 1252 mtx_unlock_spin(&w_mtx); 1253 printf("%s: witness exhausted\n", __func__); 1254 return (NULL); 1255 } 1256 w_child_free = wcl->wcl_next; 1257 bzero(wcl, sizeof(*wcl)); 1258 return (wcl); 1259} 1260 1261static void 1262witness_child_free(struct witness_child_list_entry *wcl) 1263{ 1264 1265 wcl->wcl_next = w_child_free; 1266 w_child_free = wcl; 1267} 1268 1269static struct lock_list_entry * 1270witness_lock_list_get(void) 1271{ 1272 struct lock_list_entry *lle; 1273 1274 if (witness_dead) 1275 return (NULL); 1276 mtx_lock_spin(&w_mtx); 1277 lle = w_lock_list_free; 1278 if (lle == NULL) { 1279 witness_dead = 1; 1280 mtx_unlock_spin(&w_mtx); 1281 printf("%s: witness exhausted\n", __func__); 1282 return (NULL); 1283 } 1284 w_lock_list_free = lle->ll_next; 1285 mtx_unlock_spin(&w_mtx); 1286 bzero(lle, sizeof(*lle)); 1287 return (lle); 1288} 1289 1290static void 1291witness_lock_list_free(struct lock_list_entry *lle) 1292{ 1293 1294 mtx_lock_spin(&w_mtx); 1295 lle->ll_next = w_lock_list_free; 1296 w_lock_list_free = lle; 1297 mtx_unlock_spin(&w_mtx); 1298} 1299 1300static struct lock_instance * 1301find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1302{ 1303 struct lock_list_entry *lle; 1304 struct lock_instance *instance; 1305 int i; 1306 1307 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1308 for (i = lle->ll_count - 1; i >= 0; i--) { 1309 instance = &lle->ll_children[i]; 1310 if (instance->li_lock == lock) 1311 return (instance); 1312 } 1313 return (NULL); 1314} 1315 1316int 1317witness_list_locks(struct lock_list_entry **lock_list) 1318{ 1319 struct lock_list_entry *lle; 1320 struct lock_instance *instance; 1321 struct lock_object *lock; 1322 int i, nheld; 1323 1324 nheld = 0; 1325 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1326 for (i = lle->ll_count - 1; i >= 0; i--) { 1327 instance = &lle->ll_children[i]; 1328 lock = instance->li_lock; 1329 printf("%s (%s) %s (%p) locked @ %s:%d\n", 1330 (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1331 "exclusive" : "shared", 1332 lock->lo_class->lc_name, lock->lo_name, lock, 1333 instance->li_file, instance->li_line); 1334 nheld++; 1335 } 1336 return (nheld); 1337} 1338 1339/* 1340 * Calling this on p != curproc is bad unless we are in ddb. 1341 */ 1342int 1343witness_list(struct proc *p) 1344{ 1345 critical_t savecrit; 1346 int nheld; 1347 1348 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1349#ifdef DDB 1350 KASSERT(p == curproc || db_active, 1351 ("%s: p != curproc and we aren't in the debugger", __func__)); 1352 1353 if (!db_active && witness_dead) 1354 return (0); 1355#else 1356 KASSERT(p == curproc, ("%s: p != curproc", __func__)); 1357 1358 if (witness_dead) 1359 return (0); 1360#endif 1361 nheld = witness_list_locks(&p->p_sleeplocks); 1362 1363 /* 1364 * We only handle spinlocks if p == curproc. This is somewhat broken 1365 * if p is currently executing on some other CPU and holds spin locks 1366 * as we won't display those locks. If we had a MI way of getting 1367 * the per-cpu data for a given cpu then we could use p->p_oncpu to 1368 * get the list of spinlocks for this process and "fix" this. 1369 */ 1370 if (p == curproc) { 1371 /* 1372 * Preemption bad because we need PCPU_PTR(spinlocks) to not 1373 * change. 1374 */ 1375 savecrit = critical_enter(); 1376 nheld += witness_list_locks(PCPU_PTR(spinlocks)); 1377 critical_exit(savecrit); 1378 } 1379 1380 return (nheld); 1381} 1382 1383void 1384witness_save(struct lock_object *lock, const char **filep, int *linep) 1385{ 1386 struct lock_instance *instance; 1387 1388 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 1389 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1390 return; 1391 1392 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1393 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1394 lock->lo_class->lc_name, lock->lo_name); 1395 instance = find_instance(curproc->p_sleeplocks, lock); 1396 if (instance == NULL) 1397 panic("%s: lock (%s) %s not locked", __func__, 1398 lock->lo_class->lc_name, lock->lo_name); 1399 1400 *filep = instance->li_file; 1401 *linep = instance->li_line; 1402} 1403 1404void 1405witness_restore(struct lock_object *lock, const char *file, int line) 1406{ 1407 struct lock_instance *instance; 1408 1409 KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 1410 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1411 return; 1412 1413 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1414 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1415 lock->lo_class->lc_name, lock->lo_name); 1416 instance = find_instance(curproc->p_sleeplocks, lock); 1417 if (instance == NULL) 1418 panic("%s: lock (%s) %s not locked", __func__, 1419 lock->lo_class->lc_name, lock->lo_name); 1420 1421 lock->lo_witness->w_file = file; 1422 lock->lo_witness->w_line = line; 1423 instance->li_file = file; 1424 instance->li_line = line; 1425} 1426 1427void 1428witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1429{ 1430#ifdef INVARIANT_SUPPORT 1431 struct lock_instance *instance; 1432 1433 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1434 return; 1435 1436 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 1437 instance = find_instance(curproc->p_sleeplocks, lock); 1438 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 1439 instance = find_instance(PCPU_GET(spinlocks), lock); 1440 else 1441 panic("Lock (%s) %s is not sleep or spin!", 1442 lock->lo_class->lc_name, lock->lo_name); 1443 switch (flags) { 1444 case LA_UNLOCKED: 1445 if (instance != NULL) 1446 panic("Lock (%s) %s locked @ %s:%d.", 1447 lock->lo_class->lc_name, lock->lo_name, file, line); 1448 break; 1449 case LA_LOCKED: 1450 case LA_LOCKED | LA_RECURSED: 1451 case LA_LOCKED | LA_NOTRECURSED: 1452 case LA_SLOCKED: 1453 case LA_SLOCKED | LA_RECURSED: 1454 case LA_SLOCKED | LA_NOTRECURSED: 1455 case LA_XLOCKED: 1456 case LA_XLOCKED | LA_RECURSED: 1457 case LA_XLOCKED | LA_NOTRECURSED: 1458 if (instance == NULL) 1459 panic("Lock (%s) %s not locked @ %s:%d.", 1460 lock->lo_class->lc_name, lock->lo_name, file, line); 1461 if ((flags & LA_XLOCKED) != 0 && 1462 (instance->li_flags & LI_EXCLUSIVE) == 0) 1463 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1464 lock->lo_class->lc_name, lock->lo_name, file, line); 1465 if ((flags & LA_SLOCKED) != 0 && 1466 (instance->li_flags & LI_EXCLUSIVE) != 0) 1467 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1468 lock->lo_class->lc_name, lock->lo_name, file, line); 1469 if ((flags & LA_RECURSED) != 0 && 1470 (instance->li_flags & LI_RECURSEMASK) == 0) 1471 panic("Lock (%s) %s not recursed @ %s:%d.", 1472 lock->lo_class->lc_name, lock->lo_name, file, line); 1473 if ((flags & LA_NOTRECURSED) != 0 && 1474 (instance->li_flags & LI_RECURSEMASK) != 0) 1475 panic("Lock (%s) %s recursed @ %s:%d.", 1476 lock->lo_class->lc_name, lock->lo_name, file, line); 1477 break; 1478 default: 1479 panic("Invalid lock assertion at %s:%d.", file, line); 1480 1481 } 1482#endif /* INVARIANT_SUPPORT */ 1483} 1484 1485#ifdef DDB 1486 1487DB_SHOW_COMMAND(locks, db_witness_list) 1488{ 1489 struct proc *p; 1490 pid_t pid; 1491 1492 if (have_addr) { 1493 pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 1494 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 1495 ((addr >> 16) % 16) * 10000; 1496 1497 /* sx_slock(&allproc_lock); */ 1498 LIST_FOREACH(p, &allproc, p_list) { 1499 if (p->p_pid == pid) 1500 break; 1501 } 1502 /* sx_sunlock(&allproc_lock); */ 1503 if (p == NULL) { 1504 db_printf("pid %d not found\n", pid); 1505 return; 1506 } 1507 } else 1508 p = curproc; 1509 1510 witness_list(p); 1511} 1512 1513DB_SHOW_COMMAND(witness, db_witness_display) 1514{ 1515 1516 witness_display(db_printf); 1517} 1518#endif 1519