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