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