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