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