subr_witness.c revision 112118
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 112118 2003-03-11 22:14: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/* 60 * Special rules concerning Giant and lock orders: 61 * 62 * 1) Giant must be acquired before any other mutexes. Stated another way, 63 * no other mutex may be held when Giant is acquired. 64 * 65 * 2) Giant must be released when blocking on a sleepable lock. 66 * 67 * This rule is less obvious, but is a result of Giant providing the same 68 * semantics as spl(). Basically, when a thread sleeps, it must release 69 * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule 70 * 2). 71 * 72 * 3) Giant may be acquired before or after sleepable locks. 73 * 74 * This rule is also not quite as obvious. Giant may be acquired after 75 * a sleepable lock because it is a non-sleepable lock and non-sleepable 76 * locks may always be acquired while holding a sleepable lock. The second 77 * case, Giant before a sleepable lock, follows from rule 2) above. Suppose 78 * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1 79 * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and 80 * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to 81 * execute. Thus, acquiring Giant both before and after a sleepable lock 82 * will not result in a lock order reversal. 83 */ 84 85#include "opt_ddb.h" 86#include "opt_witness.h" 87 88#include <sys/param.h> 89#include <sys/bus.h> 90#include <sys/kernel.h> 91#include <sys/ktr.h> 92#include <sys/lock.h> 93#include <sys/malloc.h> 94#include <sys/mutex.h> 95#include <sys/proc.h> 96#include <sys/sysctl.h> 97#include <sys/systm.h> 98 99#include <ddb/ddb.h> 100 101#include <machine/stdarg.h> 102 103/* Define this to check for blessed mutexes */ 104#undef BLESSING 105 106#define WITNESS_COUNT 200 107#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 108/* 109 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 110 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 111 * probably be safe for the most part, but it's still a SWAG. 112 */ 113#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 114 115#define WITNESS_NCHILDREN 6 116 117struct witness_child_list_entry; 118 119struct witness { 120 const char *w_name; 121 struct lock_class *w_class; 122 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 123 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 124 struct witness_child_list_entry *w_children; /* Great evilness... */ 125 const char *w_file; 126 int w_line; 127 u_int w_level; 128 u_int w_refcount; 129 u_char w_Giant_squawked:1; 130 u_char w_other_squawked:1; 131 u_char w_same_squawked:1; 132 u_char w_displayed:1; 133}; 134 135struct witness_child_list_entry { 136 struct witness_child_list_entry *wcl_next; 137 struct witness *wcl_children[WITNESS_NCHILDREN]; 138 u_int wcl_count; 139}; 140 141STAILQ_HEAD(witness_list, witness); 142 143#ifdef BLESSING 144struct witness_blessed { 145 const char *b_lock1; 146 const char *b_lock2; 147}; 148#endif 149 150struct witness_order_list_entry { 151 const char *w_name; 152 struct lock_class *w_class; 153}; 154 155#ifdef BLESSING 156static int blessed(struct witness *, struct witness *); 157#endif 158static int depart(struct witness *w); 159static struct witness *enroll(const char *description, 160 struct lock_class *lock_class); 161static int insertchild(struct witness *parent, struct witness *child); 162static int isitmychild(struct witness *parent, struct witness *child); 163static int isitmydescendant(struct witness *parent, struct witness *child); 164static int itismychild(struct witness *parent, struct witness *child); 165static int rebalancetree(struct witness_list *list); 166static void removechild(struct witness *parent, struct witness *child); 167static int reparentchildren(struct witness *newparent, 168 struct witness *oldparent); 169static void witness_displaydescendants(void(*)(const char *fmt, ...), 170 struct witness *, int indent); 171static const char *fixup_filename(const char *file); 172static void witness_leveldescendents(struct witness *parent, int level); 173static void witness_levelall(void); 174static struct witness *witness_get(void); 175static void witness_free(struct witness *m); 176static struct witness_child_list_entry *witness_child_get(void); 177static void witness_child_free(struct witness_child_list_entry *wcl); 178static struct lock_list_entry *witness_lock_list_get(void); 179static void witness_lock_list_free(struct lock_list_entry *lle); 180static struct lock_instance *find_instance(struct lock_list_entry *lock_list, 181 struct lock_object *lock); 182static void witness_list_lock(struct lock_instance *instance); 183#ifdef DDB 184static void witness_list(struct thread *td); 185static void witness_display_list(void(*prnt)(const char *fmt, ...), 186 struct witness_list *list); 187static void witness_display(void(*)(const char *fmt, ...)); 188#endif 189 190MALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 191 192static int witness_watch = 1; 193TUNABLE_INT("debug.witness_watch", &witness_watch); 194SYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 195 196#ifdef DDB 197/* 198 * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 199 * drop into kdebug() when: 200 * - a lock heirarchy violation occurs 201 * - locks are held when going to sleep. 202 */ 203#ifdef WITNESS_DDB 204int witness_ddb = 1; 205#else 206int witness_ddb = 0; 207#endif 208TUNABLE_INT("debug.witness_ddb", &witness_ddb); 209SYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 210 211/* 212 * When DDB is enabled and witness_trace is set to 1, it will cause the system 213 * to print a stack trace: 214 * - a lock heirarchy violation occurs 215 * - locks are held when going to sleep. 216 */ 217int witness_trace = 1; 218TUNABLE_INT("debug.witness_trace", &witness_trace); 219SYSCTL_INT(_debug, OID_AUTO, witness_trace, CTLFLAG_RW, &witness_trace, 0, ""); 220#endif /* DDB */ 221 222#ifdef WITNESS_SKIPSPIN 223int witness_skipspin = 1; 224#else 225int witness_skipspin = 0; 226#endif 227TUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 228SYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 229 ""); 230 231static struct mtx w_mtx; 232static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 233static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 234static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 235static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 236static struct witness_child_list_entry *w_child_free = NULL; 237static struct lock_list_entry *w_lock_list_free = NULL; 238static int witness_dead; /* fatal error, probably no memory */ 239 240static struct witness w_data[WITNESS_COUNT]; 241static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 242static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 243 244static struct witness_order_list_entry order_lists[] = { 245 { "proctree", &lock_class_sx }, 246 { "allproc", &lock_class_sx }, 247 { "Giant", &lock_class_mtx_sleep }, 248 { "filedesc structure", &lock_class_mtx_sleep }, 249 { "pipe mutex", &lock_class_mtx_sleep }, 250 { "sigio lock", &lock_class_mtx_sleep }, 251 { "process group", &lock_class_mtx_sleep }, 252 { "process lock", &lock_class_mtx_sleep }, 253 { "session", &lock_class_mtx_sleep }, 254 { "uidinfo hash", &lock_class_mtx_sleep }, 255 { "uidinfo struct", &lock_class_mtx_sleep }, 256 { NULL, NULL }, 257 /* 258 * spin locks 259 */ 260#ifdef SMP 261 { "ap boot", &lock_class_mtx_spin }, 262#ifdef __i386__ 263 { "com", &lock_class_mtx_spin }, 264#endif 265#endif 266 { "sio", &lock_class_mtx_spin }, 267#ifdef __i386__ 268 { "cy", &lock_class_mtx_spin }, 269#endif 270 { "sabtty", &lock_class_mtx_spin }, 271 { "zstty", &lock_class_mtx_spin }, 272 { "ng_node", &lock_class_mtx_spin }, 273 { "ng_worklist", &lock_class_mtx_spin }, 274 { "ithread table lock", &lock_class_mtx_spin }, 275 { "sched lock", &lock_class_mtx_spin }, 276 { "callout", &lock_class_mtx_spin }, 277 /* 278 * leaf locks 279 */ 280 { "allpmaps", &lock_class_mtx_spin }, 281 { "vm page queue free mutex", &lock_class_mtx_spin }, 282 { "icu", &lock_class_mtx_spin }, 283#ifdef SMP 284 { "smp rendezvous", &lock_class_mtx_spin }, 285#if defined(__i386__) && defined(APIC_IO) 286 { "tlb", &lock_class_mtx_spin }, 287#endif 288#ifdef __sparc64__ 289 { "ipi", &lock_class_mtx_spin }, 290#endif 291#endif 292 { "clk", &lock_class_mtx_spin }, 293 { "mutex profiling lock", &lock_class_mtx_spin }, 294 { "kse zombie lock", &lock_class_mtx_spin }, 295 { "ALD Queue", &lock_class_mtx_spin }, 296#ifdef __ia64__ 297 { "MCA spin lock", &lock_class_mtx_spin }, 298#endif 299#ifdef __i386__ 300 { "pcicfg", &lock_class_mtx_spin }, 301#endif 302 { NULL, NULL }, 303 { NULL, NULL } 304}; 305 306#ifdef BLESSING 307/* 308 * Pairs of locks which have been blessed 309 * Don't complain about order problems with blessed locks 310 */ 311static struct witness_blessed blessed_list[] = { 312}; 313static int blessed_count = 314 sizeof(blessed_list) / sizeof(struct witness_blessed); 315#endif 316 317/* 318 * List of all locks in the system. 319 */ 320TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 321 322static struct mtx all_mtx = { 323 { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 324 "All locks list", /* mtx_object.lo_name */ 325 "All locks list", /* mtx_object.lo_type */ 326 LO_INITIALIZED, /* mtx_object.lo_flags */ 327 { NULL, NULL }, /* mtx_object.lo_list */ 328 NULL }, /* mtx_object.lo_witness */ 329 MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 330 TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 331 { NULL, NULL } /* mtx_contested */ 332}; 333 334/* 335 * This global is set to 0 once it becomes safe to use the witness code. 336 */ 337static int witness_cold = 1; 338 339/* 340 * Global variables for book keeping. 341 */ 342static int lock_cur_cnt; 343static int lock_max_cnt; 344 345/* 346 * The WITNESS-enabled diagnostic code. 347 */ 348static void 349witness_initialize(void *dummy __unused) 350{ 351 struct lock_object *lock; 352 struct witness_order_list_entry *order; 353 struct witness *w, *w1; 354 int i; 355 356 /* 357 * We have to release Giant before initializing its witness 358 * structure so that WITNESS doesn't get confused. 359 */ 360 mtx_unlock(&Giant); 361 mtx_assert(&Giant, MA_NOTOWNED); 362 363 CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 364 TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 365 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 366 MTX_NOWITNESS); 367 for (i = 0; i < WITNESS_COUNT; i++) 368 witness_free(&w_data[i]); 369 for (i = 0; i < WITNESS_CHILDCOUNT; i++) 370 witness_child_free(&w_childdata[i]); 371 for (i = 0; i < LOCK_CHILDCOUNT; i++) 372 witness_lock_list_free(&w_locklistdata[i]); 373 374 /* First add in all the specified order lists. */ 375 for (order = order_lists; order->w_name != NULL; order++) { 376 w = enroll(order->w_name, order->w_class); 377 if (w == NULL) 378 continue; 379 w->w_file = "order list"; 380 for (order++; order->w_name != NULL; order++) { 381 w1 = enroll(order->w_name, order->w_class); 382 if (w1 == NULL) 383 continue; 384 w1->w_file = "order list"; 385 if (!itismychild(w, w1)) 386 panic("Not enough memory for static orders!"); 387 w = w1; 388 } 389 } 390 391 /* Iterate through all locks and add them to witness. */ 392 mtx_lock(&all_mtx); 393 TAILQ_FOREACH(lock, &all_locks, lo_list) { 394 if (lock->lo_flags & LO_WITNESS) 395 lock->lo_witness = enroll(lock->lo_type, 396 lock->lo_class); 397 else 398 lock->lo_witness = NULL; 399 } 400 mtx_unlock(&all_mtx); 401 402 /* Mark the witness code as being ready for use. */ 403 atomic_store_rel_int(&witness_cold, 0); 404 405 mtx_lock(&Giant); 406} 407SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 408 409void 410witness_init(struct lock_object *lock) 411{ 412 struct lock_class *class; 413 414 class = lock->lo_class; 415 if (lock->lo_flags & LO_INITIALIZED) 416 panic("%s: lock (%s) %s is already initialized", __func__, 417 class->lc_name, lock->lo_name); 418 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 419 (class->lc_flags & LC_RECURSABLE) == 0) 420 panic("%s: lock (%s) %s can not be recursable", __func__, 421 class->lc_name, lock->lo_name); 422 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 423 (class->lc_flags & LC_SLEEPABLE) == 0) 424 panic("%s: lock (%s) %s can not be sleepable", __func__, 425 class->lc_name, lock->lo_name); 426 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 427 (class->lc_flags & LC_UPGRADABLE) == 0) 428 panic("%s: lock (%s) %s can not be upgradable", __func__, 429 class->lc_name, lock->lo_name); 430 431 mtx_lock(&all_mtx); 432 TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 433 lock->lo_flags |= LO_INITIALIZED; 434 lock_cur_cnt++; 435 if (lock_cur_cnt > lock_max_cnt) 436 lock_max_cnt = lock_cur_cnt; 437 mtx_unlock(&all_mtx); 438 if (!witness_cold && !witness_dead && panicstr == NULL && 439 (lock->lo_flags & LO_WITNESS) != 0) 440 lock->lo_witness = enroll(lock->lo_type, class); 441 else 442 lock->lo_witness = NULL; 443} 444 445void 446witness_destroy(struct lock_object *lock) 447{ 448 struct witness *w; 449 450 if (witness_cold) 451 panic("lock (%s) %s destroyed while witness_cold", 452 lock->lo_class->lc_name, lock->lo_name); 453 if ((lock->lo_flags & LO_INITIALIZED) == 0) 454 panic("%s: lock (%s) %s is not initialized", __func__, 455 lock->lo_class->lc_name, lock->lo_name); 456 457 /* XXX: need to verify that no one holds the lock */ 458 w = lock->lo_witness; 459 if (w != NULL) { 460 mtx_lock_spin(&w_mtx); 461 MPASS(w->w_refcount > 0); 462 w->w_refcount--; 463 464 /* 465 * Lock is already released if we have an allocation failure 466 * and depart() fails. 467 */ 468 if (w->w_refcount != 0 || depart(w)) 469 mtx_unlock_spin(&w_mtx); 470 } 471 472 mtx_lock(&all_mtx); 473 lock_cur_cnt--; 474 TAILQ_REMOVE(&all_locks, lock, lo_list); 475 lock->lo_flags &= ~LO_INITIALIZED; 476 mtx_unlock(&all_mtx); 477} 478 479#ifdef DDB 480static void 481witness_display_list(void(*prnt)(const char *fmt, ...), 482 struct witness_list *list) 483{ 484 struct witness *w; 485 486 STAILQ_FOREACH(w, list, w_typelist) { 487 if (w->w_file == NULL || w->w_level > 0) 488 continue; 489 /* 490 * This lock has no anscestors, display its descendants. 491 */ 492 witness_displaydescendants(prnt, w, 0); 493 } 494} 495 496static void 497witness_display(void(*prnt)(const char *fmt, ...)) 498{ 499 struct witness *w; 500 501 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 502 witness_levelall(); 503 504 /* Clear all the displayed flags. */ 505 STAILQ_FOREACH(w, &w_all, w_list) { 506 w->w_displayed = 0; 507 } 508 509 /* 510 * First, handle sleep locks which have been acquired at least 511 * once. 512 */ 513 prnt("Sleep locks:\n"); 514 witness_display_list(prnt, &w_sleep); 515 516 /* 517 * Now do spin locks which have been acquired at least once. 518 */ 519 prnt("\nSpin locks:\n"); 520 witness_display_list(prnt, &w_spin); 521 522 /* 523 * Finally, any locks which have not been acquired yet. 524 */ 525 prnt("\nLocks which were never acquired:\n"); 526 STAILQ_FOREACH(w, &w_all, w_list) { 527 if (w->w_file != NULL || w->w_refcount == 0) 528 continue; 529 prnt("%s\n", w->w_name); 530 } 531} 532#endif /* DDB */ 533 534/* Trim useless garbage from filenames. */ 535static const char * 536fixup_filename(const char *file) 537{ 538 539 if (file == NULL) 540 return (NULL); 541 while (strncmp(file, "../", 3) == 0) 542 file += 3; 543 return (file); 544} 545 546void 547witness_lock(struct lock_object *lock, int flags, const char *file, int line) 548{ 549 struct lock_list_entry **lock_list, *lle; 550 struct lock_instance *lock1, *lock2; 551 struct lock_class *class; 552 struct witness *w, *w1; 553 struct thread *td; 554 int i, j; 555#ifdef DDB 556 int go_into_ddb = 0; 557#endif 558 559 if (witness_cold || witness_dead || lock->lo_witness == NULL || 560 panicstr != NULL) 561 return; 562 w = lock->lo_witness; 563 class = lock->lo_class; 564 td = curthread; 565 file = fixup_filename(file); 566 567 if (class->lc_flags & LC_SLEEPLOCK) { 568 /* 569 * Since spin locks include a critical section, this check 570 * impliclty enforces a lock order of all sleep locks before 571 * all spin locks. 572 */ 573 if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0) 574 panic("blockable sleep lock (%s) %s @ %s:%d", 575 class->lc_name, lock->lo_name, file, line); 576 lock_list = &td->td_sleeplocks; 577 } else 578 lock_list = PCPU_PTR(spinlocks); 579 580 /* 581 * Is this the first lock acquired? If so, then no order checking 582 * is needed. 583 */ 584 if (*lock_list == NULL) 585 goto out; 586 587 /* 588 * Check to see if we are recursing on a lock we already own. 589 */ 590 lock1 = find_instance(*lock_list, lock); 591 if (lock1 != NULL) { 592 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 593 (flags & LOP_EXCLUSIVE) == 0) { 594 printf("shared lock of (%s) %s @ %s:%d\n", 595 class->lc_name, lock->lo_name, file, line); 596 printf("while exclusively locked from %s:%d\n", 597 lock1->li_file, lock1->li_line); 598 panic("share->excl"); 599 } 600 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 601 (flags & LOP_EXCLUSIVE) != 0) { 602 printf("exclusive lock of (%s) %s @ %s:%d\n", 603 class->lc_name, lock->lo_name, file, line); 604 printf("while share locked from %s:%d\n", 605 lock1->li_file, lock1->li_line); 606 panic("excl->share"); 607 } 608 lock1->li_flags++; 609 if ((lock->lo_flags & LO_RECURSABLE) == 0) { 610 printf( 611 "recursed on non-recursive lock (%s) %s @ %s:%d\n", 612 class->lc_name, lock->lo_name, file, line); 613 printf("first acquired @ %s:%d\n", lock1->li_file, 614 lock1->li_line); 615 panic("recurse"); 616 } 617 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 618 td->td_proc->p_pid, lock->lo_name, 619 lock1->li_flags & LI_RECURSEMASK); 620 lock1->li_file = file; 621 lock1->li_line = line; 622 return; 623 } 624 625 /* 626 * Try locks do not block if they fail to acquire the lock, thus 627 * there is no danger of deadlocks or of switching while holding a 628 * spin lock if we acquire a lock via a try operation. 629 */ 630 if (flags & LOP_TRYLOCK) 631 goto out; 632 633 /* 634 * Check for duplicate locks of the same type. Note that we only 635 * have to check for this on the last lock we just acquired. Any 636 * other cases will be caught as lock order violations. 637 */ 638 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 639 w1 = lock1->li_lock->lo_witness; 640 if (w1 == w) { 641 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK)) 642 goto out; 643 w->w_same_squawked = 1; 644 printf("acquiring duplicate lock of same type: \"%s\"\n", 645 lock->lo_type); 646 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 647 lock1->li_file, lock1->li_line); 648 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 649#ifdef DDB 650 go_into_ddb = 1; 651#endif 652 goto out; 653 } 654 MPASS(!mtx_owned(&w_mtx)); 655 mtx_lock_spin(&w_mtx); 656 /* 657 * If we have a known higher number just say ok 658 */ 659 if (witness_watch > 1 && w->w_level > w1->w_level) { 660 mtx_unlock_spin(&w_mtx); 661 goto out; 662 } 663 /* 664 * If we know that the the lock we are acquiring comes after 665 * the lock we most recently acquired in the lock order tree, 666 * then there is no need for any further checks. 667 */ 668 if (isitmydescendant(w1, w)) { 669 mtx_unlock_spin(&w_mtx); 670 goto out; 671 } 672 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 673 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 674 675 MPASS(j < WITNESS_COUNT); 676 lock1 = &lle->ll_children[i]; 677 w1 = lock1->li_lock->lo_witness; 678 679 /* 680 * If this lock doesn't undergo witness checking, 681 * then skip it. 682 */ 683 if (w1 == NULL) { 684 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 685 ("lock missing witness structure")); 686 continue; 687 } 688 /* 689 * If we are locking Giant and this is a sleepable 690 * lock, then skip it. 691 */ 692 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 693 lock == &Giant.mtx_object) 694 continue; 695 /* 696 * If we are locking a sleepable lock and this lock 697 * is Giant, then skip it. 698 */ 699 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 700 lock1->li_lock == &Giant.mtx_object) 701 continue; 702 /* 703 * If we are locking a sleepable lock and this lock 704 * isn't sleepable, we want to treat it as a lock 705 * order violation to enfore a general lock order of 706 * sleepable locks before non-sleepable locks. 707 */ 708 if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 709 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 710 /* 711 * Check the lock order hierarchy for a reveresal. 712 */ 713 if (!isitmydescendant(w, w1)) 714 continue; 715 /* 716 * We have a lock order violation, check to see if it 717 * is allowed or has already been yelled about. 718 */ 719 mtx_unlock_spin(&w_mtx); 720#ifdef BLESSING 721 if (blessed(w, w1)) 722 goto out; 723#endif 724 if (lock1->li_lock == &Giant.mtx_object) { 725 if (w1->w_Giant_squawked) 726 goto out; 727 else 728 w1->w_Giant_squawked = 1; 729 } else { 730 if (w1->w_other_squawked) 731 goto out; 732 else 733 w1->w_other_squawked = 1; 734 } 735 /* 736 * Ok, yell about it. 737 */ 738 printf("lock order reversal\n"); 739 /* 740 * Try to locate an earlier lock with 741 * witness w in our list. 742 */ 743 do { 744 lock2 = &lle->ll_children[i]; 745 MPASS(lock2->li_lock != NULL); 746 if (lock2->li_lock->lo_witness == w) 747 break; 748 i--; 749 if (i == 0 && lle->ll_next != NULL) { 750 lle = lle->ll_next; 751 i = lle->ll_count - 1; 752 MPASS(i >= 0 && i < LOCK_NCHILDREN); 753 } 754 } while (i >= 0); 755 if (i < 0) { 756 printf(" 1st %p %s (%s) @ %s:%d\n", 757 lock1->li_lock, lock1->li_lock->lo_name, 758 lock1->li_lock->lo_type, lock1->li_file, 759 lock1->li_line); 760 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 761 lock->lo_name, lock->lo_type, file, line); 762 } else { 763 printf(" 1st %p %s (%s) @ %s:%d\n", 764 lock2->li_lock, lock2->li_lock->lo_name, 765 lock2->li_lock->lo_type, lock2->li_file, 766 lock2->li_line); 767 printf(" 2nd %p %s (%s) @ %s:%d\n", 768 lock1->li_lock, lock1->li_lock->lo_name, 769 lock1->li_lock->lo_type, lock1->li_file, 770 lock1->li_line); 771 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 772 lock->lo_name, lock->lo_type, file, line); 773 } 774#ifdef DDB 775 go_into_ddb = 1; 776#endif 777 goto out; 778 } 779 } 780 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 781 /* 782 * Don't build a new relationship between a sleepable lock and 783 * Giant if it is the wrong direction. The real lock order is that 784 * sleepable locks come before Giant. 785 */ 786 if (!(lock1->li_lock == &Giant.mtx_object && 787 (lock->lo_flags & LO_SLEEPABLE) != 0)) { 788 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 789 lock->lo_type, lock1->li_lock->lo_type); 790 if (!itismychild(lock1->li_lock->lo_witness, w)) 791 /* Witness is dead. */ 792 return; 793 } 794 mtx_unlock_spin(&w_mtx); 795 796out: 797#ifdef DDB 798 if (go_into_ddb) { 799 if (witness_trace) 800 backtrace(); 801 if (witness_ddb) 802 Debugger(__func__); 803 } 804#endif 805 w->w_file = file; 806 w->w_line = line; 807 808 lle = *lock_list; 809 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 810 lle = witness_lock_list_get(); 811 if (lle == NULL) 812 return; 813 lle->ll_next = *lock_list; 814 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 815 td->td_proc->p_pid, lle); 816 *lock_list = lle; 817 } 818 lock1 = &lle->ll_children[lle->ll_count++]; 819 lock1->li_lock = lock; 820 lock1->li_line = line; 821 lock1->li_file = file; 822 if ((flags & LOP_EXCLUSIVE) != 0) 823 lock1->li_flags = LI_EXCLUSIVE; 824 else 825 lock1->li_flags = 0; 826 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 827 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 828} 829 830void 831witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 832{ 833 struct lock_instance *instance; 834 struct lock_class *class; 835 836 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 837 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 838 return; 839 class = lock->lo_class; 840 file = fixup_filename(file); 841 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 842 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 843 class->lc_name, lock->lo_name, file, line); 844 if ((flags & LOP_TRYLOCK) == 0) 845 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 846 lock->lo_name, file, line); 847 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 848 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 849 class->lc_name, lock->lo_name, file, line); 850 instance = find_instance(curthread->td_sleeplocks, lock); 851 if (instance == NULL) 852 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 853 class->lc_name, lock->lo_name, file, line); 854 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 855 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 856 class->lc_name, lock->lo_name, file, line); 857 if ((instance->li_flags & LI_RECURSEMASK) != 0) 858 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 859 class->lc_name, lock->lo_name, 860 instance->li_flags & LI_RECURSEMASK, file, line); 861 instance->li_flags |= LI_EXCLUSIVE; 862} 863 864void 865witness_downgrade(struct lock_object *lock, int flags, const char *file, 866 int line) 867{ 868 struct lock_instance *instance; 869 struct lock_class *class; 870 871 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 872 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 873 return; 874 class = lock->lo_class; 875 file = fixup_filename(file); 876 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 877 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 878 class->lc_name, lock->lo_name, file, line); 879 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 880 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 881 class->lc_name, lock->lo_name, file, line); 882 instance = find_instance(curthread->td_sleeplocks, lock); 883 if (instance == NULL) 884 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 885 class->lc_name, lock->lo_name, file, line); 886 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 887 panic("downgrade of shared lock (%s) %s @ %s:%d", 888 class->lc_name, lock->lo_name, file, line); 889 if ((instance->li_flags & LI_RECURSEMASK) != 0) 890 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 891 class->lc_name, lock->lo_name, 892 instance->li_flags & LI_RECURSEMASK, file, line); 893 instance->li_flags &= ~LI_EXCLUSIVE; 894} 895 896void 897witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 898{ 899 struct lock_list_entry **lock_list, *lle; 900 struct lock_instance *instance; 901 struct lock_class *class; 902 struct thread *td; 903 register_t s; 904 int i, j; 905 906 if (witness_cold || witness_dead || lock->lo_witness == NULL || 907 panicstr != NULL) 908 return; 909 td = curthread; 910 class = lock->lo_class; 911 file = fixup_filename(file); 912 if (class->lc_flags & LC_SLEEPLOCK) 913 lock_list = &td->td_sleeplocks; 914 else 915 lock_list = PCPU_PTR(spinlocks); 916 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 917 for (i = 0; i < (*lock_list)->ll_count; i++) { 918 instance = &(*lock_list)->ll_children[i]; 919 if (instance->li_lock == lock) { 920 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 921 (flags & LOP_EXCLUSIVE) == 0) { 922 printf( 923 "shared unlock of (%s) %s @ %s:%d\n", 924 class->lc_name, lock->lo_name, 925 file, line); 926 printf( 927 "while exclusively locked from %s:%d\n", 928 instance->li_file, 929 instance->li_line); 930 panic("excl->ushare"); 931 } 932 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 933 (flags & LOP_EXCLUSIVE) != 0) { 934 printf( 935 "exclusive unlock of (%s) %s @ %s:%d\n", 936 class->lc_name, lock->lo_name, 937 file, line); 938 printf( 939 "while share locked from %s:%d\n", 940 instance->li_file, 941 instance->li_line); 942 panic("share->uexcl"); 943 } 944 /* If we are recursed, unrecurse. */ 945 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 946 CTR4(KTR_WITNESS, 947 "%s: pid %d unrecursed on %s r=%d", __func__, 948 td->td_proc->p_pid, 949 instance->li_lock->lo_name, 950 instance->li_flags); 951 instance->li_flags--; 952 return; 953 } 954 s = intr_disable(); 955 CTR4(KTR_WITNESS, 956 "%s: pid %d removed %s from lle[%d]", __func__, 957 td->td_proc->p_pid, 958 instance->li_lock->lo_name, 959 (*lock_list)->ll_count - 1); 960 for (j = i; j < (*lock_list)->ll_count - 1; j++) 961 (*lock_list)->ll_children[j] = 962 (*lock_list)->ll_children[j + 1]; 963 (*lock_list)->ll_count--; 964 intr_restore(s); 965 if ((*lock_list)->ll_count == 0) { 966 lle = *lock_list; 967 *lock_list = lle->ll_next; 968 CTR3(KTR_WITNESS, 969 "%s: pid %d removed lle %p", __func__, 970 td->td_proc->p_pid, lle); 971 witness_lock_list_free(lle); 972 } 973 return; 974 } 975 } 976 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 977 file, line); 978} 979 980/* 981 * Warn if any locks other than 'lock' are held. Flags can be passed in to 982 * exempt Giant and sleepable locks from the checks as well. If any 983 * non-exempt locks are held, then a supplied message is printed to the 984 * console along with a list of the offending locks. If indicated in the 985 * flags then a failure results in a panic as well. 986 */ 987int 988witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 989{ 990 struct lock_list_entry *lle; 991 struct lock_instance *lock1; 992 struct thread *td; 993 va_list ap; 994 int i, n; 995 996 if (witness_cold || witness_dead || panicstr != NULL) 997 return (0); 998 n = 0; 999 td = curthread; 1000 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 1001 for (i = lle->ll_count - 1; i >= 0; i--) { 1002 lock1 = &lle->ll_children[i]; 1003 if (lock1->li_lock == lock) 1004 continue; 1005 if (flags & WARN_GIANTOK && 1006 lock1->li_lock == &Giant.mtx_object) 1007 continue; 1008 if (flags & WARN_SLEEPOK && 1009 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 1010 continue; 1011 if (n == 0) { 1012 va_start(ap, fmt); 1013 vprintf(fmt, ap); 1014 va_end(ap); 1015 printf(" with the following"); 1016 if (flags & WARN_SLEEPOK) 1017 printf(" non-sleepable"); 1018 printf("locks held:\n"); 1019 } 1020 n++; 1021 witness_list_lock(lock1); 1022 } 1023 if (PCPU_GET(spinlocks) != NULL) { 1024 /* 1025 * Since we already hold a spinlock preemption is 1026 * already blocked. 1027 */ 1028 if (n == 0) { 1029 va_start(ap, fmt); 1030 vprintf(fmt, ap); 1031 va_end(ap); 1032 printf(" with the following"); 1033 if (flags & WARN_SLEEPOK) 1034 printf(" non-sleepable"); 1035 printf("locks held:\n"); 1036 } 1037 n += witness_list_locks(PCPU_PTR(spinlocks)); 1038 } 1039 if (flags & WARN_PANIC && n) 1040 panic("witness_warn"); 1041#ifdef DDB 1042 else if (witness_ddb && n) 1043 Debugger(__func__); 1044#endif 1045 return (n); 1046} 1047 1048const char * 1049witness_file(struct lock_object *lock) 1050{ 1051 struct witness *w; 1052 1053 if (witness_cold || witness_dead || lock->lo_witness == NULL) 1054 return ("?"); 1055 w = lock->lo_witness; 1056 return (w->w_file); 1057} 1058 1059int 1060witness_line(struct lock_object *lock) 1061{ 1062 struct witness *w; 1063 1064 if (witness_cold || witness_dead || lock->lo_witness == NULL) 1065 return (0); 1066 w = lock->lo_witness; 1067 return (w->w_line); 1068} 1069 1070static struct witness * 1071enroll(const char *description, struct lock_class *lock_class) 1072{ 1073 struct witness *w; 1074 1075 if (!witness_watch || witness_dead || panicstr != NULL) 1076 return (NULL); 1077 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 1078 return (NULL); 1079 mtx_lock_spin(&w_mtx); 1080 STAILQ_FOREACH(w, &w_all, w_list) { 1081 if (w->w_name == description || (w->w_refcount > 0 && 1082 strcmp(description, w->w_name) == 0)) { 1083 w->w_refcount++; 1084 mtx_unlock_spin(&w_mtx); 1085 if (lock_class != w->w_class) 1086 panic( 1087 "lock (%s) %s does not match earlier (%s) lock", 1088 description, lock_class->lc_name, 1089 w->w_class->lc_name); 1090 return (w); 1091 } 1092 } 1093 /* 1094 * This isn't quite right, as witness_cold is still 0 while we 1095 * enroll all the locks initialized before witness_initialize(). 1096 */ 1097 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 1098 mtx_unlock_spin(&w_mtx); 1099 panic("spin lock %s not in order list", description); 1100 } 1101 if ((w = witness_get()) == NULL) 1102 return (NULL); 1103 w->w_name = description; 1104 w->w_class = lock_class; 1105 w->w_refcount = 1; 1106 STAILQ_INSERT_HEAD(&w_all, w, w_list); 1107 if (lock_class->lc_flags & LC_SPINLOCK) 1108 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1109 else if (lock_class->lc_flags & LC_SLEEPLOCK) 1110 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1111 else { 1112 mtx_unlock_spin(&w_mtx); 1113 panic("lock class %s is not sleep or spin", 1114 lock_class->lc_name); 1115 } 1116 mtx_unlock_spin(&w_mtx); 1117 return (w); 1118} 1119 1120/* Don't let the door bang you on the way out... */ 1121static int 1122depart(struct witness *w) 1123{ 1124 struct witness_child_list_entry *wcl, *nwcl; 1125 struct witness_list *list; 1126 struct witness *parent; 1127 1128 MPASS(w->w_refcount == 0); 1129 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1130 list = &w_sleep; 1131 else 1132 list = &w_spin; 1133 /* 1134 * First, we run through the entire tree looking for any 1135 * witnesses that the outgoing witness is a child of. For 1136 * each parent that we find, we reparent all the direct 1137 * children of the outgoing witness to its parent. 1138 */ 1139 STAILQ_FOREACH(parent, list, w_typelist) { 1140 if (!isitmychild(parent, w)) 1141 continue; 1142 removechild(parent, w); 1143 if (!reparentchildren(parent, w)) 1144 return (0); 1145 } 1146 1147 /* 1148 * Now we go through and free up the child list of the 1149 * outgoing witness. 1150 */ 1151 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1152 nwcl = wcl->wcl_next; 1153 witness_child_free(wcl); 1154 } 1155 1156 /* 1157 * Detach from various lists and free. 1158 */ 1159 STAILQ_REMOVE(list, w, witness, w_typelist); 1160 STAILQ_REMOVE(&w_all, w, witness, w_list); 1161 witness_free(w); 1162 1163 /* Finally, fixup the tree. */ 1164 return (rebalancetree(list)); 1165} 1166 1167 1168/* 1169 * Prune an entire lock order tree. We look for cases where a lock 1170 * is now both a descendant and a direct child of a given lock. In 1171 * that case, we want to remove the direct child link from the tree. 1172 * 1173 * Returns false if insertchild() fails. 1174 */ 1175static int 1176rebalancetree(struct witness_list *list) 1177{ 1178 struct witness *child, *parent; 1179 1180 STAILQ_FOREACH(child, list, w_typelist) { 1181 STAILQ_FOREACH(parent, list, w_typelist) { 1182 if (!isitmychild(parent, child)) 1183 continue; 1184 removechild(parent, child); 1185 if (isitmydescendant(parent, child)) 1186 continue; 1187 if (!insertchild(parent, child)) 1188 return (0); 1189 } 1190 } 1191 witness_levelall(); 1192 return (1); 1193} 1194 1195/* 1196 * Add "child" as a direct child of "parent". Returns false if 1197 * we fail due to out of memory. 1198 */ 1199static int 1200insertchild(struct witness *parent, struct witness *child) 1201{ 1202 struct witness_child_list_entry **wcl; 1203 1204 MPASS(child != NULL && parent != NULL); 1205 1206 /* 1207 * Insert "child" after "parent" 1208 */ 1209 wcl = &parent->w_children; 1210 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 1211 wcl = &(*wcl)->wcl_next; 1212 if (*wcl == NULL) { 1213 *wcl = witness_child_get(); 1214 if (*wcl == NULL) 1215 return (0); 1216 } 1217 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1218 1219 return (1); 1220} 1221 1222/* 1223 * Make all the direct descendants of oldparent be direct descendants 1224 * of newparent. 1225 */ 1226static int 1227reparentchildren(struct witness *newparent, struct witness *oldparent) 1228{ 1229 struct witness_child_list_entry *wcl; 1230 int i; 1231 1232 /* Avoid making a witness a child of itself. */ 1233 MPASS(!isitmychild(oldparent, newparent)); 1234 1235 for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1236 for (i = 0; i < wcl->wcl_count; i++) 1237 if (!insertchild(newparent, wcl->wcl_children[i])) 1238 return (0); 1239 return (1); 1240} 1241 1242static int 1243itismychild(struct witness *parent, struct witness *child) 1244{ 1245 struct witness_list *list; 1246 1247 MPASS(child != NULL && parent != NULL); 1248 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1249 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1250 panic( 1251 "%s: parent (%s) and child (%s) are not the same lock type", 1252 __func__, parent->w_class->lc_name, 1253 child->w_class->lc_name); 1254 1255 if (!insertchild(parent, child)) 1256 return (0); 1257 1258 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1259 list = &w_sleep; 1260 else 1261 list = &w_spin; 1262 return (rebalancetree(list)); 1263} 1264 1265static void 1266removechild(struct witness *parent, struct witness *child) 1267{ 1268 struct witness_child_list_entry **wcl, *wcl1; 1269 int i; 1270 1271 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1272 for (i = 0; i < (*wcl)->wcl_count; i++) 1273 if ((*wcl)->wcl_children[i] == child) 1274 goto found; 1275 return; 1276found: 1277 (*wcl)->wcl_count--; 1278 if ((*wcl)->wcl_count > i) 1279 (*wcl)->wcl_children[i] = 1280 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1281 MPASS((*wcl)->wcl_children[i] != NULL); 1282 if ((*wcl)->wcl_count != 0) 1283 return; 1284 wcl1 = *wcl; 1285 *wcl = wcl1->wcl_next; 1286 witness_child_free(wcl1); 1287} 1288 1289static int 1290isitmychild(struct witness *parent, struct witness *child) 1291{ 1292 struct witness_child_list_entry *wcl; 1293 int i; 1294 1295 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1296 for (i = 0; i < wcl->wcl_count; i++) { 1297 if (wcl->wcl_children[i] == child) 1298 return (1); 1299 } 1300 } 1301 return (0); 1302} 1303 1304static int 1305isitmydescendant(struct witness *parent, struct witness *child) 1306{ 1307 struct witness_child_list_entry *wcl; 1308 int i, j; 1309 1310 if (isitmychild(parent, child)) 1311 return (1); 1312 j = 0; 1313 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1314 MPASS(j < 1000); 1315 for (i = 0; i < wcl->wcl_count; i++) { 1316 if (isitmydescendant(wcl->wcl_children[i], child)) 1317 return (1); 1318 } 1319 j++; 1320 } 1321 return (0); 1322} 1323 1324static void 1325witness_levelall (void) 1326{ 1327 struct witness_list *list; 1328 struct witness *w, *w1; 1329 1330 /* 1331 * First clear all levels. 1332 */ 1333 STAILQ_FOREACH(w, &w_all, w_list) { 1334 w->w_level = 0; 1335 } 1336 1337 /* 1338 * Look for locks with no parent and level all their descendants. 1339 */ 1340 STAILQ_FOREACH(w, &w_all, w_list) { 1341 /* 1342 * This is just an optimization, technically we could get 1343 * away just walking the all list each time. 1344 */ 1345 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1346 list = &w_sleep; 1347 else 1348 list = &w_spin; 1349 STAILQ_FOREACH(w1, list, w_typelist) { 1350 if (isitmychild(w1, w)) 1351 goto skip; 1352 } 1353 witness_leveldescendents(w, 0); 1354 skip: 1355 ; /* silence GCC 3.x */ 1356 } 1357} 1358 1359static void 1360witness_leveldescendents(struct witness *parent, int level) 1361{ 1362 struct witness_child_list_entry *wcl; 1363 int i; 1364 1365 if (parent->w_level < level) 1366 parent->w_level = level; 1367 level++; 1368 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1369 for (i = 0; i < wcl->wcl_count; i++) 1370 witness_leveldescendents(wcl->wcl_children[i], level); 1371} 1372 1373static void 1374witness_displaydescendants(void(*prnt)(const char *fmt, ...), 1375 struct witness *parent, int indent) 1376{ 1377 struct witness_child_list_entry *wcl; 1378 int i, level; 1379 1380 level = parent->w_level; 1381 prnt("%-2d", level); 1382 for (i = 0; i < indent; i++) 1383 prnt(" "); 1384 if (parent->w_refcount > 0) 1385 prnt("%s", parent->w_name); 1386 else 1387 prnt("(dead)"); 1388 if (parent->w_displayed) { 1389 prnt(" -- (already displayed)\n"); 1390 return; 1391 } 1392 parent->w_displayed = 1; 1393 if (parent->w_refcount > 0) { 1394 if (parent->w_file != NULL) 1395 prnt(" -- last acquired @ %s:%d", parent->w_file, 1396 parent->w_line); 1397 } 1398 prnt("\n"); 1399 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1400 for (i = 0; i < wcl->wcl_count; i++) 1401 witness_displaydescendants(prnt, 1402 wcl->wcl_children[i], indent + 1); 1403} 1404 1405#ifdef BLESSING 1406static int 1407blessed(struct witness *w1, struct witness *w2) 1408{ 1409 int i; 1410 struct witness_blessed *b; 1411 1412 for (i = 0; i < blessed_count; i++) { 1413 b = &blessed_list[i]; 1414 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1415 if (strcmp(w2->w_name, b->b_lock2) == 0) 1416 return (1); 1417 continue; 1418 } 1419 if (strcmp(w1->w_name, b->b_lock2) == 0) 1420 if (strcmp(w2->w_name, b->b_lock1) == 0) 1421 return (1); 1422 } 1423 return (0); 1424} 1425#endif 1426 1427static struct witness * 1428witness_get(void) 1429{ 1430 struct witness *w; 1431 1432 if (witness_dead) { 1433 mtx_unlock_spin(&w_mtx); 1434 return (NULL); 1435 } 1436 if (STAILQ_EMPTY(&w_free)) { 1437 witness_dead = 1; 1438 mtx_unlock_spin(&w_mtx); 1439 printf("%s: witness exhausted\n", __func__); 1440 return (NULL); 1441 } 1442 w = STAILQ_FIRST(&w_free); 1443 STAILQ_REMOVE_HEAD(&w_free, w_list); 1444 bzero(w, sizeof(*w)); 1445 return (w); 1446} 1447 1448static void 1449witness_free(struct witness *w) 1450{ 1451 1452 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1453} 1454 1455static struct witness_child_list_entry * 1456witness_child_get(void) 1457{ 1458 struct witness_child_list_entry *wcl; 1459 1460 if (witness_dead) { 1461 mtx_unlock_spin(&w_mtx); 1462 return (NULL); 1463 } 1464 wcl = w_child_free; 1465 if (wcl == NULL) { 1466 witness_dead = 1; 1467 mtx_unlock_spin(&w_mtx); 1468 printf("%s: witness exhausted\n", __func__); 1469 return (NULL); 1470 } 1471 w_child_free = wcl->wcl_next; 1472 bzero(wcl, sizeof(*wcl)); 1473 return (wcl); 1474} 1475 1476static void 1477witness_child_free(struct witness_child_list_entry *wcl) 1478{ 1479 1480 wcl->wcl_next = w_child_free; 1481 w_child_free = wcl; 1482} 1483 1484static struct lock_list_entry * 1485witness_lock_list_get(void) 1486{ 1487 struct lock_list_entry *lle; 1488 1489 if (witness_dead) 1490 return (NULL); 1491 mtx_lock_spin(&w_mtx); 1492 lle = w_lock_list_free; 1493 if (lle == NULL) { 1494 witness_dead = 1; 1495 mtx_unlock_spin(&w_mtx); 1496 printf("%s: witness exhausted\n", __func__); 1497 return (NULL); 1498 } 1499 w_lock_list_free = lle->ll_next; 1500 mtx_unlock_spin(&w_mtx); 1501 bzero(lle, sizeof(*lle)); 1502 return (lle); 1503} 1504 1505static void 1506witness_lock_list_free(struct lock_list_entry *lle) 1507{ 1508 1509 mtx_lock_spin(&w_mtx); 1510 lle->ll_next = w_lock_list_free; 1511 w_lock_list_free = lle; 1512 mtx_unlock_spin(&w_mtx); 1513} 1514 1515static struct lock_instance * 1516find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1517{ 1518 struct lock_list_entry *lle; 1519 struct lock_instance *instance; 1520 int i; 1521 1522 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1523 for (i = lle->ll_count - 1; i >= 0; i--) { 1524 instance = &lle->ll_children[i]; 1525 if (instance->li_lock == lock) 1526 return (instance); 1527 } 1528 return (NULL); 1529} 1530 1531static void 1532witness_list_lock(struct lock_instance *instance) 1533{ 1534 struct lock_object *lock; 1535 1536 lock = instance->li_lock; 1537 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1538 "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name); 1539 if (lock->lo_type != lock->lo_name) 1540 printf(" (%s)", lock->lo_type); 1541 printf(" r = %d (%p) locked @ %s:%d\n", 1542 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1543 instance->li_line); 1544} 1545 1546int 1547witness_list_locks(struct lock_list_entry **lock_list) 1548{ 1549 struct lock_list_entry *lle; 1550 int i, nheld; 1551 1552 nheld = 0; 1553 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1554 for (i = lle->ll_count - 1; i >= 0; i--) { 1555 witness_list_lock(&lle->ll_children[i]); 1556 nheld++; 1557 } 1558 return (nheld); 1559} 1560 1561void 1562witness_save(struct lock_object *lock, const char **filep, int *linep) 1563{ 1564 struct lock_instance *instance; 1565 1566 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1567 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1568 return; 1569 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1570 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1571 lock->lo_class->lc_name, lock->lo_name); 1572 instance = find_instance(curthread->td_sleeplocks, lock); 1573 if (instance == NULL) 1574 panic("%s: lock (%s) %s not locked", __func__, 1575 lock->lo_class->lc_name, lock->lo_name); 1576 *filep = instance->li_file; 1577 *linep = instance->li_line; 1578} 1579 1580void 1581witness_restore(struct lock_object *lock, const char *file, int line) 1582{ 1583 struct lock_instance *instance; 1584 1585 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1586 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1587 return; 1588 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1589 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1590 lock->lo_class->lc_name, lock->lo_name); 1591 instance = find_instance(curthread->td_sleeplocks, lock); 1592 if (instance == NULL) 1593 panic("%s: lock (%s) %s not locked", __func__, 1594 lock->lo_class->lc_name, lock->lo_name); 1595 lock->lo_witness->w_file = file; 1596 lock->lo_witness->w_line = line; 1597 instance->li_file = file; 1598 instance->li_line = line; 1599} 1600 1601void 1602witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1603{ 1604#ifdef INVARIANT_SUPPORT 1605 struct lock_instance *instance; 1606 1607 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1608 return; 1609 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 1610 instance = find_instance(curthread->td_sleeplocks, lock); 1611 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 1612 instance = find_instance(PCPU_GET(spinlocks), lock); 1613 else { 1614 panic("Lock (%s) %s is not sleep or spin!", 1615 lock->lo_class->lc_name, lock->lo_name); 1616 return; 1617 } 1618 file = fixup_filename(file); 1619 switch (flags) { 1620 case LA_UNLOCKED: 1621 if (instance != NULL) 1622 panic("Lock (%s) %s locked @ %s:%d.", 1623 lock->lo_class->lc_name, lock->lo_name, file, line); 1624 break; 1625 case LA_LOCKED: 1626 case LA_LOCKED | LA_RECURSED: 1627 case LA_LOCKED | LA_NOTRECURSED: 1628 case LA_SLOCKED: 1629 case LA_SLOCKED | LA_RECURSED: 1630 case LA_SLOCKED | LA_NOTRECURSED: 1631 case LA_XLOCKED: 1632 case LA_XLOCKED | LA_RECURSED: 1633 case LA_XLOCKED | LA_NOTRECURSED: 1634 if (instance == NULL) { 1635 panic("Lock (%s) %s not locked @ %s:%d.", 1636 lock->lo_class->lc_name, lock->lo_name, file, line); 1637 break; 1638 } 1639 if ((flags & LA_XLOCKED) != 0 && 1640 (instance->li_flags & LI_EXCLUSIVE) == 0) 1641 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1642 lock->lo_class->lc_name, lock->lo_name, file, line); 1643 if ((flags & LA_SLOCKED) != 0 && 1644 (instance->li_flags & LI_EXCLUSIVE) != 0) 1645 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1646 lock->lo_class->lc_name, lock->lo_name, file, line); 1647 if ((flags & LA_RECURSED) != 0 && 1648 (instance->li_flags & LI_RECURSEMASK) == 0) 1649 panic("Lock (%s) %s not recursed @ %s:%d.", 1650 lock->lo_class->lc_name, lock->lo_name, file, line); 1651 if ((flags & LA_NOTRECURSED) != 0 && 1652 (instance->li_flags & LI_RECURSEMASK) != 0) 1653 panic("Lock (%s) %s recursed @ %s:%d.", 1654 lock->lo_class->lc_name, lock->lo_name, file, line); 1655 break; 1656 default: 1657 panic("Invalid lock assertion at %s:%d.", file, line); 1658 1659 } 1660#endif /* INVARIANT_SUPPORT */ 1661} 1662 1663#ifdef DDB 1664static void 1665witness_list(struct thread *td) 1666{ 1667 1668 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1669 KASSERT(db_active, ("%s: not in the debugger", __func__)); 1670 1671 if (witness_dead) 1672 return; 1673 1674 witness_list_locks(&td->td_sleeplocks); 1675 1676 /* 1677 * We only handle spinlocks if td == curthread. This is somewhat broken 1678 * if td is currently executing on some other CPU and holds spin locks 1679 * as we won't display those locks. If we had a MI way of getting 1680 * the per-cpu data for a given cpu then we could use 1681 * td->td_kse->ke_oncpu to get the list of spinlocks for this thread 1682 * and "fix" this. 1683 * 1684 * That still wouldn't really fix this unless we locked sched_lock 1685 * or stopped the other CPU to make sure it wasn't changing the list 1686 * out from under us. It is probably best to just not try to handle 1687 * threads on other CPU's for now. 1688 */ 1689 if (td == curthread && PCPU_GET(spinlocks) != NULL) 1690 witness_list_locks(PCPU_PTR(spinlocks)); 1691} 1692 1693DB_SHOW_COMMAND(locks, db_witness_list) 1694{ 1695 struct thread *td; 1696 pid_t pid; 1697 struct proc *p; 1698 1699 if (have_addr) { 1700 pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 1701 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 1702 ((addr >> 16) % 16) * 10000; 1703 /* sx_slock(&allproc_lock); */ 1704 FOREACH_PROC_IN_SYSTEM(p) { 1705 if (p->p_pid == pid) 1706 break; 1707 } 1708 /* sx_sunlock(&allproc_lock); */ 1709 if (p == NULL) { 1710 db_printf("pid %d not found\n", pid); 1711 return; 1712 } 1713 FOREACH_THREAD_IN_PROC(p, td) { 1714 witness_list(td); 1715 } 1716 } else { 1717 td = curthread; 1718 witness_list(td); 1719 } 1720} 1721 1722DB_SHOW_COMMAND(witness, db_witness_display) 1723{ 1724 1725 witness_display(db_printf); 1726} 1727#endif 1728