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