subr_witness.c revision 177253
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 */ 31 32/* 33 * Implementation of the `witness' lock verifier. Originally implemented for 34 * mutexes in BSD/OS. Extended to handle generic lock objects and lock 35 * classes in FreeBSD. 36 */ 37 38/* 39 * Main Entry: witness 40 * Pronunciation: 'wit-n&s 41 * Function: noun 42 * Etymology: Middle English witnesse, from Old English witnes knowledge, 43 * testimony, witness, from 2wit 44 * Date: before 12th century 45 * 1 : attestation of a fact or event : TESTIMONY 46 * 2 : one that gives evidence; specifically : one who testifies in 47 * a cause or before a judicial tribunal 48 * 3 : one asked to be present at a transaction so as to be able to 49 * testify to its having taken place 50 * 4 : one who has personal knowledge of something 51 * 5 a : something serving as evidence or proof : SIGN 52 * b : public affirmation by word or example of usually 53 * religious faith or conviction <the heroic witness to divine 54 * life -- Pilot> 55 * 6 capitalized : a member of the Jehovah's Witnesses 56 */ 57 58/* 59 * Special rules concerning Giant and lock orders: 60 * 61 * 1) Giant must be acquired before any other mutexes. Stated another way, 62 * no other mutex may be held when Giant is acquired. 63 * 64 * 2) Giant must be released when blocking on a sleepable lock. 65 * 66 * This rule is less obvious, but is a result of Giant providing the same 67 * semantics as spl(). Basically, when a thread sleeps, it must release 68 * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule 69 * 2). 70 * 71 * 3) Giant may be acquired before or after sleepable locks. 72 * 73 * This rule is also not quite as obvious. Giant may be acquired after 74 * a sleepable lock because it is a non-sleepable lock and non-sleepable 75 * locks may always be acquired while holding a sleepable lock. The second 76 * case, Giant before a sleepable lock, follows from rule 2) above. Suppose 77 * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1 78 * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and 79 * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to 80 * execute. Thus, acquiring Giant both before and after a sleepable lock 81 * will not result in a lock order reversal. 82 */ 83 84#include <sys/cdefs.h> 85__FBSDID("$FreeBSD: head/sys/kern/subr_witness.c 177253 2008-03-16 10:58:09Z rwatson $"); 86 87#include "opt_ddb.h" 88#include "opt_hwpmc_hooks.h" 89#include "opt_witness.h" 90 91#include <sys/param.h> 92#include <sys/bus.h> 93#include <sys/kdb.h> 94#include <sys/kernel.h> 95#include <sys/ktr.h> 96#include <sys/lock.h> 97#include <sys/malloc.h> 98#include <sys/mutex.h> 99#include <sys/priv.h> 100#include <sys/proc.h> 101#include <sys/sysctl.h> 102#include <sys/systm.h> 103 104#include <ddb/ddb.h> 105 106#include <machine/stdarg.h> 107 108/* Note that these traces do not work with KTR_ALQ. */ 109#if 0 110#define KTR_WITNESS KTR_SUBSYS 111#else 112#define KTR_WITNESS 0 113#endif 114 115/* Easier to stay with the old names. */ 116#define lo_list lo_witness_data.lod_list 117#define lo_witness lo_witness_data.lod_witness 118 119/* Define this to check for blessed mutexes */ 120#undef BLESSING 121 122#define WITNESS_COUNT 1024 123#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 124/* 125 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 126 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 127 * probably be safe for the most part, but it's still a SWAG. 128 */ 129#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 130 131#define WITNESS_NCHILDREN 6 132 133struct witness_child_list_entry; 134 135struct witness { 136 const char *w_name; 137 struct lock_class *w_class; 138 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 139 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 140 struct witness_child_list_entry *w_children; /* Great evilness... */ 141 const char *w_file; 142 int w_line; 143 u_int w_level; 144 u_int w_refcount; 145 u_char w_Giant_squawked:1; 146 u_char w_other_squawked:1; 147 u_char w_same_squawked:1; 148 u_char w_displayed:1; 149}; 150 151struct witness_child_list_entry { 152 struct witness_child_list_entry *wcl_next; 153 struct witness *wcl_children[WITNESS_NCHILDREN]; 154 u_int wcl_count; 155}; 156 157STAILQ_HEAD(witness_list, witness); 158 159#ifdef BLESSING 160struct witness_blessed { 161 const char *b_lock1; 162 const char *b_lock2; 163}; 164#endif 165 166struct witness_order_list_entry { 167 const char *w_name; 168 struct lock_class *w_class; 169}; 170 171#ifdef BLESSING 172static int blessed(struct witness *, struct witness *); 173#endif 174static int depart(struct witness *w); 175static struct witness *enroll(const char *description, 176 struct lock_class *lock_class); 177static int insertchild(struct witness *parent, struct witness *child); 178static int isitmychild(struct witness *parent, struct witness *child); 179static int isitmydescendant(struct witness *parent, struct witness *child); 180static int itismychild(struct witness *parent, struct witness *child); 181static void removechild(struct witness *parent, struct witness *child); 182static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 183static const char *fixup_filename(const char *file); 184static struct witness *witness_get(void); 185static void witness_free(struct witness *m); 186static struct witness_child_list_entry *witness_child_get(void); 187static void witness_child_free(struct witness_child_list_entry *wcl); 188static struct lock_list_entry *witness_lock_list_get(void); 189static void witness_lock_list_free(struct lock_list_entry *lle); 190static struct lock_instance *find_instance(struct lock_list_entry *lock_list, 191 struct lock_object *lock); 192static void witness_list_lock(struct lock_instance *instance); 193#ifdef DDB 194static void witness_leveldescendents(struct witness *parent, int level); 195static void witness_levelall(void); 196static void witness_displaydescendants(void(*)(const char *fmt, ...), 197 struct witness *, int indent); 198static void witness_display_list(void(*prnt)(const char *fmt, ...), 199 struct witness_list *list); 200static void witness_display(void(*)(const char *fmt, ...)); 201static void witness_list(struct thread *td); 202#endif 203 204SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking"); 205 206/* 207 * If set to 0, witness is disabled. If set to a non-zero value, witness 208 * performs full lock order checking for all locks. At runtime, this 209 * value may be set to 0 to turn off witness. witness is not allowed be 210 * turned on once it is turned off, however. 211 */ 212static int witness_watch = 1; 213TUNABLE_INT("debug.witness.watch", &witness_watch); 214SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 215 sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 216 217#ifdef KDB 218/* 219 * When KDB is enabled and witness_kdb is set to 1, it will cause the system 220 * to drop into kdebug() when: 221 * - a lock hierarchy violation occurs 222 * - locks are held when going to sleep. 223 */ 224#ifdef WITNESS_KDB 225int witness_kdb = 1; 226#else 227int witness_kdb = 0; 228#endif 229TUNABLE_INT("debug.witness.kdb", &witness_kdb); 230SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 231 232/* 233 * When KDB is enabled and witness_trace is set to 1, it will cause the system 234 * to print a stack trace: 235 * - a lock hierarchy violation occurs 236 * - locks are held when going to sleep. 237 */ 238int witness_trace = 1; 239TUNABLE_INT("debug.witness.trace", &witness_trace); 240SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 241#endif /* KDB */ 242 243#ifdef WITNESS_SKIPSPIN 244int witness_skipspin = 1; 245#else 246int witness_skipspin = 0; 247#endif 248TUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 249SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, 250 &witness_skipspin, 0, ""); 251 252static struct mtx w_mtx; 253static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 254static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 255static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 256static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 257static struct witness_child_list_entry *w_child_free = NULL; 258static struct lock_list_entry *w_lock_list_free = NULL; 259 260static int w_free_cnt, w_spin_cnt, w_sleep_cnt, w_child_free_cnt, w_child_cnt; 261SYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, ""); 262SYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, ""); 263SYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0, 264 ""); 265SYSCTL_INT(_debug_witness, OID_AUTO, child_free_cnt, CTLFLAG_RD, 266 &w_child_free_cnt, 0, ""); 267SYSCTL_INT(_debug_witness, OID_AUTO, child_cnt, CTLFLAG_RD, &w_child_cnt, 0, 268 ""); 269 270static struct witness w_data[WITNESS_COUNT]; 271static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 272static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 273 274static struct witness_order_list_entry order_lists[] = { 275 /* 276 * sx locks 277 */ 278 { "proctree", &lock_class_sx }, 279 { "allproc", &lock_class_sx }, 280 { "allprison", &lock_class_sx }, 281 { NULL, NULL }, 282 /* 283 * Various mutexes 284 */ 285 { "Giant", &lock_class_mtx_sleep }, 286 { "pipe mutex", &lock_class_mtx_sleep }, 287 { "sigio lock", &lock_class_mtx_sleep }, 288 { "process group", &lock_class_mtx_sleep }, 289 { "process lock", &lock_class_mtx_sleep }, 290 { "session", &lock_class_mtx_sleep }, 291 { "uidinfo hash", &lock_class_mtx_sleep }, 292 { "uidinfo struct", &lock_class_mtx_sleep }, 293#ifdef HWPMC_HOOKS 294 { "pmc-sleep", &lock_class_mtx_sleep }, 295#endif 296 { NULL, NULL }, 297 /* 298 * Sockets 299 */ 300 { "accept", &lock_class_mtx_sleep }, 301 { "so_snd", &lock_class_mtx_sleep }, 302 { "so_rcv", &lock_class_mtx_sleep }, 303 { "sellck", &lock_class_mtx_sleep }, 304 { NULL, NULL }, 305 /* 306 * Routing 307 */ 308 { "so_rcv", &lock_class_mtx_sleep }, 309 { "radix node head", &lock_class_mtx_sleep }, 310 { "rtentry", &lock_class_mtx_sleep }, 311 { "ifaddr", &lock_class_mtx_sleep }, 312 { NULL, NULL }, 313 /* 314 * Multicast - protocol locks before interface locks, after UDP locks. 315 */ 316 { "udpinp", &lock_class_mtx_sleep }, 317 { "in_multi_mtx", &lock_class_mtx_sleep }, 318 { "igmp_mtx", &lock_class_mtx_sleep }, 319 { "if_addr_mtx", &lock_class_mtx_sleep }, 320 { NULL, NULL }, 321 /* 322 * UNIX Domain Sockets 323 */ 324 { "unp", &lock_class_mtx_sleep }, 325 { "so_snd", &lock_class_mtx_sleep }, 326 { NULL, NULL }, 327 /* 328 * UDP/IP 329 */ 330 { "udp", &lock_class_mtx_sleep }, 331 { "udpinp", &lock_class_mtx_sleep }, 332 { "so_snd", &lock_class_mtx_sleep }, 333 { NULL, NULL }, 334 /* 335 * TCP/IP 336 */ 337 { "tcp", &lock_class_mtx_sleep }, 338 { "tcpinp", &lock_class_mtx_sleep }, 339 { "so_snd", &lock_class_mtx_sleep }, 340 { NULL, NULL }, 341 /* 342 * SLIP 343 */ 344 { "slip_mtx", &lock_class_mtx_sleep }, 345 { "slip sc_mtx", &lock_class_mtx_sleep }, 346 { NULL, NULL }, 347 /* 348 * netatalk 349 */ 350 { "ddp_list_mtx", &lock_class_mtx_sleep }, 351 { "ddp_mtx", &lock_class_mtx_sleep }, 352 { NULL, NULL }, 353 /* 354 * BPF 355 */ 356 { "bpf global lock", &lock_class_mtx_sleep }, 357 { "bpf interface lock", &lock_class_mtx_sleep }, 358 { "bpf cdev lock", &lock_class_mtx_sleep }, 359 { NULL, NULL }, 360 /* 361 * NFS server 362 */ 363 { "nfsd_mtx", &lock_class_mtx_sleep }, 364 { "so_snd", &lock_class_mtx_sleep }, 365 { NULL, NULL }, 366 367 /* 368 * IEEE 802.11 369 */ 370 { "802.11 com lock", &lock_class_mtx_sleep}, 371 { NULL, NULL }, 372 /* 373 * Network drivers 374 */ 375 { "network driver", &lock_class_mtx_sleep}, 376 { NULL, NULL }, 377 378 /* 379 * Netgraph 380 */ 381 { "ng_node", &lock_class_mtx_sleep }, 382 { "ng_worklist", &lock_class_mtx_sleep }, 383 { NULL, NULL }, 384 /* 385 * CDEV 386 */ 387 { "system map", &lock_class_mtx_sleep }, 388 { "vm page queue mutex", &lock_class_mtx_sleep }, 389 { "vnode interlock", &lock_class_mtx_sleep }, 390 { "cdev", &lock_class_mtx_sleep }, 391 { NULL, NULL }, 392 /* 393 * kqueue/VFS interaction 394 */ 395 { "kqueue", &lock_class_mtx_sleep }, 396 { "struct mount mtx", &lock_class_mtx_sleep }, 397 { "vnode interlock", &lock_class_mtx_sleep }, 398 { NULL, NULL }, 399 /* 400 * spin locks 401 */ 402#ifdef SMP 403 { "ap boot", &lock_class_mtx_spin }, 404#endif 405 { "rm.mutex_mtx", &lock_class_mtx_spin }, 406 { "sio", &lock_class_mtx_spin }, 407 { "scrlock", &lock_class_mtx_spin }, 408#ifdef __i386__ 409 { "cy", &lock_class_mtx_spin }, 410#endif 411#ifdef __sparc64__ 412 { "pcib_mtx", &lock_class_mtx_spin }, 413 { "rtc_mtx", &lock_class_mtx_spin }, 414#endif 415 { "scc_hwmtx", &lock_class_mtx_spin }, 416 { "uart_hwmtx", &lock_class_mtx_spin }, 417 { "fast_taskqueue", &lock_class_mtx_spin }, 418 { "intr table", &lock_class_mtx_spin }, 419#ifdef HWPMC_HOOKS 420 { "pmc-per-proc", &lock_class_mtx_spin }, 421#endif 422 { "process slock", &lock_class_mtx_spin }, 423 { "sleepq chain", &lock_class_mtx_spin }, 424 { "umtx lock", &lock_class_mtx_spin }, 425 { "rm_spinlock", &lock_class_mtx_spin }, 426 { "turnstile chain", &lock_class_mtx_spin }, 427 { "turnstile lock", &lock_class_mtx_spin }, 428 { "sched lock", &lock_class_mtx_spin }, 429 { "td_contested", &lock_class_mtx_spin }, 430 { "callout", &lock_class_mtx_spin }, 431 { "entropy harvest mutex", &lock_class_mtx_spin }, 432 { "syscons video lock", &lock_class_mtx_spin }, 433 { "time lock", &lock_class_mtx_spin }, 434#ifdef SMP 435 { "smp rendezvous", &lock_class_mtx_spin }, 436#endif 437#ifdef __powerpc__ 438 { "tlb0", &lock_class_mtx_spin }, 439#endif 440 /* 441 * leaf locks 442 */ 443 { "icu", &lock_class_mtx_spin }, 444#if defined(SMP) && defined(__sparc64__) 445 { "ipi", &lock_class_mtx_spin }, 446#endif 447#ifdef __i386__ 448 { "allpmaps", &lock_class_mtx_spin }, 449 { "descriptor tables", &lock_class_mtx_spin }, 450#endif 451 { "clk", &lock_class_mtx_spin }, 452 { "mprof lock", &lock_class_mtx_spin }, 453 { "zombie lock", &lock_class_mtx_spin }, 454 { "ALD Queue", &lock_class_mtx_spin }, 455#ifdef __ia64__ 456 { "MCA spin lock", &lock_class_mtx_spin }, 457#endif 458#if defined(__i386__) || defined(__amd64__) 459 { "pcicfg", &lock_class_mtx_spin }, 460 { "NDIS thread lock", &lock_class_mtx_spin }, 461#endif 462 { "tw_osl_io_lock", &lock_class_mtx_spin }, 463 { "tw_osl_q_lock", &lock_class_mtx_spin }, 464 { "tw_cl_io_lock", &lock_class_mtx_spin }, 465 { "tw_cl_intr_lock", &lock_class_mtx_spin }, 466 { "tw_cl_gen_lock", &lock_class_mtx_spin }, 467#ifdef HWPMC_HOOKS 468 { "pmc-leaf", &lock_class_mtx_spin }, 469#endif 470 { "blocked lock", &lock_class_mtx_spin }, 471 { NULL, NULL }, 472 { NULL, NULL } 473}; 474 475#ifdef BLESSING 476/* 477 * Pairs of locks which have been blessed 478 * Don't complain about order problems with blessed locks 479 */ 480static struct witness_blessed blessed_list[] = { 481}; 482static int blessed_count = 483 sizeof(blessed_list) / sizeof(struct witness_blessed); 484#endif 485 486/* 487 * List of locks initialized prior to witness being initialized whose 488 * enrollment is currently deferred. 489 */ 490STAILQ_HEAD(, lock_object) pending_locks = 491 STAILQ_HEAD_INITIALIZER(pending_locks); 492 493/* 494 * This global is set to 0 once it becomes safe to use the witness code. 495 */ 496static int witness_cold = 1; 497 498/* 499 * This global is set to 1 once the static lock orders have been enrolled 500 * so that a warning can be issued for any spin locks enrolled later. 501 */ 502static int witness_spin_warn = 0; 503 504/* 505 * The WITNESS-enabled diagnostic code. Note that the witness code does 506 * assume that the early boot is single-threaded at least until after this 507 * routine is completed. 508 */ 509static void 510witness_initialize(void *dummy __unused) 511{ 512 struct lock_object *lock; 513 struct witness_order_list_entry *order; 514 struct witness *w, *w1; 515 int i; 516 517 /* 518 * We have to release Giant before initializing its witness 519 * structure so that WITNESS doesn't get confused. 520 */ 521 mtx_unlock(&Giant); 522 mtx_assert(&Giant, MA_NOTOWNED); 523 524 CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 525 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 526 MTX_NOWITNESS | MTX_NOPROFILE); 527 for (i = 0; i < WITNESS_COUNT; i++) 528 witness_free(&w_data[i]); 529 for (i = 0; i < WITNESS_CHILDCOUNT; i++) 530 witness_child_free(&w_childdata[i]); 531 for (i = 0; i < LOCK_CHILDCOUNT; i++) 532 witness_lock_list_free(&w_locklistdata[i]); 533 534 /* First add in all the specified order lists. */ 535 for (order = order_lists; order->w_name != NULL; order++) { 536 w = enroll(order->w_name, order->w_class); 537 if (w == NULL) 538 continue; 539 w->w_file = "order list"; 540 for (order++; order->w_name != NULL; order++) { 541 w1 = enroll(order->w_name, order->w_class); 542 if (w1 == NULL) 543 continue; 544 w1->w_file = "order list"; 545 if (!itismychild(w, w1)) 546 panic("Not enough memory for static orders!"); 547 w = w1; 548 } 549 } 550 witness_spin_warn = 1; 551 552 /* Iterate through all locks and add them to witness. */ 553 while (!STAILQ_EMPTY(&pending_locks)) { 554 lock = STAILQ_FIRST(&pending_locks); 555 STAILQ_REMOVE_HEAD(&pending_locks, lo_list); 556 KASSERT(lock->lo_flags & LO_WITNESS, 557 ("%s: lock %s is on pending list but not LO_WITNESS", 558 __func__, lock->lo_name)); 559 lock->lo_witness = enroll(lock->lo_type, LOCK_CLASS(lock)); 560 } 561 562 /* Mark the witness code as being ready for use. */ 563 witness_cold = 0; 564 565 mtx_lock(&Giant); 566} 567SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, 568 NULL); 569 570static int 571sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 572{ 573 int error, value; 574 575 value = witness_watch; 576 error = sysctl_handle_int(oidp, &value, 0, req); 577 if (error != 0 || req->newptr == NULL) 578 return (error); 579 if (value == witness_watch) 580 return (0); 581 if (value != 0) 582 return (EINVAL); 583 witness_watch = 0; 584 return (0); 585} 586 587void 588witness_init(struct lock_object *lock) 589{ 590 struct lock_class *class; 591 592 /* Various sanity checks. */ 593 class = LOCK_CLASS(lock); 594 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 595 (class->lc_flags & LC_RECURSABLE) == 0) 596 panic("%s: lock (%s) %s can not be recursable", __func__, 597 class->lc_name, lock->lo_name); 598 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 599 (class->lc_flags & LC_SLEEPABLE) == 0) 600 panic("%s: lock (%s) %s can not be sleepable", __func__, 601 class->lc_name, lock->lo_name); 602 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 603 (class->lc_flags & LC_UPGRADABLE) == 0) 604 panic("%s: lock (%s) %s can not be upgradable", __func__, 605 class->lc_name, lock->lo_name); 606 607 /* 608 * If we shouldn't watch this lock, then just clear lo_witness. 609 * Otherwise, if witness_cold is set, then it is too early to 610 * enroll this lock, so defer it to witness_initialize() by adding 611 * it to the pending_locks list. If it is not too early, then enroll 612 * the lock now. 613 */ 614 if (witness_watch == 0 || panicstr != NULL || 615 (lock->lo_flags & LO_WITNESS) == 0) 616 lock->lo_witness = NULL; 617 else if (witness_cold) { 618 STAILQ_INSERT_TAIL(&pending_locks, lock, lo_list); 619 lock->lo_flags |= LO_ENROLLPEND; 620 } else 621 lock->lo_witness = enroll(lock->lo_type, class); 622} 623 624void 625witness_destroy(struct lock_object *lock) 626{ 627 struct lock_class *class; 628 struct witness *w; 629 630 class = LOCK_CLASS(lock); 631 if (witness_cold) 632 panic("lock (%s) %s destroyed while witness_cold", 633 class->lc_name, lock->lo_name); 634 635 /* XXX: need to verify that no one holds the lock */ 636 if ((lock->lo_flags & (LO_WITNESS | LO_ENROLLPEND)) == LO_WITNESS && 637 lock->lo_witness != NULL) { 638 w = lock->lo_witness; 639 mtx_lock_spin(&w_mtx); 640 MPASS(w->w_refcount > 0); 641 w->w_refcount--; 642 643 /* 644 * Lock is already released if we have an allocation failure 645 * and depart() fails. 646 */ 647 if (w->w_refcount != 0 || depart(w)) 648 mtx_unlock_spin(&w_mtx); 649 } 650 651 /* 652 * If this lock is destroyed before witness is up and running, 653 * remove it from the pending list. 654 */ 655 if (lock->lo_flags & LO_ENROLLPEND) { 656 STAILQ_REMOVE(&pending_locks, lock, lock_object, lo_list); 657 lock->lo_flags &= ~LO_ENROLLPEND; 658 } 659} 660 661#ifdef DDB 662static void 663witness_levelall (void) 664{ 665 struct witness_list *list; 666 struct witness *w, *w1; 667 668 /* 669 * First clear all levels. 670 */ 671 STAILQ_FOREACH(w, &w_all, w_list) { 672 w->w_level = 0; 673 } 674 675 /* 676 * Look for locks with no parent and level all their descendants. 677 */ 678 STAILQ_FOREACH(w, &w_all, w_list) { 679 /* 680 * This is just an optimization, technically we could get 681 * away just walking the all list each time. 682 */ 683 if (w->w_class->lc_flags & LC_SLEEPLOCK) 684 list = &w_sleep; 685 else 686 list = &w_spin; 687 STAILQ_FOREACH(w1, list, w_typelist) { 688 if (isitmychild(w1, w)) 689 goto skip; 690 } 691 witness_leveldescendents(w, 0); 692 skip: 693 ; /* silence GCC 3.x */ 694 } 695} 696 697static void 698witness_leveldescendents(struct witness *parent, int level) 699{ 700 struct witness_child_list_entry *wcl; 701 int i; 702 703 if (parent->w_level < level) 704 parent->w_level = level; 705 level++; 706 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 707 for (i = 0; i < wcl->wcl_count; i++) 708 witness_leveldescendents(wcl->wcl_children[i], level); 709} 710 711static void 712witness_displaydescendants(void(*prnt)(const char *fmt, ...), 713 struct witness *parent, int indent) 714{ 715 struct witness_child_list_entry *wcl; 716 int i, level; 717 718 level = parent->w_level; 719 prnt("%-2d", level); 720 for (i = 0; i < indent; i++) 721 prnt(" "); 722 if (parent->w_refcount > 0) 723 prnt("%s", parent->w_name); 724 else 725 prnt("(dead)"); 726 if (parent->w_displayed) { 727 prnt(" -- (already displayed)\n"); 728 return; 729 } 730 parent->w_displayed = 1; 731 if (parent->w_refcount > 0) { 732 if (parent->w_file != NULL) 733 prnt(" -- last acquired @ %s:%d", parent->w_file, 734 parent->w_line); 735 } 736 prnt("\n"); 737 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 738 for (i = 0; i < wcl->wcl_count; i++) 739 witness_displaydescendants(prnt, 740 wcl->wcl_children[i], indent + 1); 741} 742 743static void 744witness_display_list(void(*prnt)(const char *fmt, ...), 745 struct witness_list *list) 746{ 747 struct witness *w; 748 749 STAILQ_FOREACH(w, list, w_typelist) { 750 if (w->w_file == NULL || w->w_level > 0) 751 continue; 752 /* 753 * This lock has no anscestors, display its descendants. 754 */ 755 witness_displaydescendants(prnt, w, 0); 756 } 757} 758 759static void 760witness_display(void(*prnt)(const char *fmt, ...)) 761{ 762 struct witness *w; 763 764 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 765 witness_levelall(); 766 767 /* Clear all the displayed flags. */ 768 STAILQ_FOREACH(w, &w_all, w_list) { 769 w->w_displayed = 0; 770 } 771 772 /* 773 * First, handle sleep locks which have been acquired at least 774 * once. 775 */ 776 prnt("Sleep locks:\n"); 777 witness_display_list(prnt, &w_sleep); 778 779 /* 780 * Now do spin locks which have been acquired at least once. 781 */ 782 prnt("\nSpin locks:\n"); 783 witness_display_list(prnt, &w_spin); 784 785 /* 786 * Finally, any locks which have not been acquired yet. 787 */ 788 prnt("\nLocks which were never acquired:\n"); 789 STAILQ_FOREACH(w, &w_all, w_list) { 790 if (w->w_file != NULL || w->w_refcount == 0) 791 continue; 792 prnt("%s\n", w->w_name); 793 } 794} 795#endif /* DDB */ 796 797/* Trim useless garbage from filenames. */ 798static const char * 799fixup_filename(const char *file) 800{ 801 802 if (file == NULL) 803 return (NULL); 804 while (strncmp(file, "../", 3) == 0) 805 file += 3; 806 return (file); 807} 808 809int 810witness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 811{ 812 813 if (witness_watch == 0 || panicstr != NULL) 814 return (0); 815 816 /* Require locks that witness knows about. */ 817 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 818 lock2->lo_witness == NULL) 819 return (EINVAL); 820 821 MPASS(!mtx_owned(&w_mtx)); 822 mtx_lock_spin(&w_mtx); 823 824 /* 825 * If we already have either an explicit or implied lock order that 826 * is the other way around, then return an error. 827 */ 828 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 829 mtx_unlock_spin(&w_mtx); 830 return (EDOOFUS); 831 } 832 833 /* Try to add the new order. */ 834 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 835 lock2->lo_type, lock1->lo_type); 836 if (!itismychild(lock1->lo_witness, lock2->lo_witness)) 837 return (ENOMEM); 838 mtx_unlock_spin(&w_mtx); 839 return (0); 840} 841 842void 843witness_checkorder(struct lock_object *lock, int flags, const char *file, 844 int line) 845{ 846 struct lock_list_entry **lock_list, *lle; 847 struct lock_instance *lock1, *lock2; 848 struct lock_class *class; 849 struct witness *w, *w1; 850 struct thread *td; 851 int i, j; 852 853 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 854 panicstr != NULL) 855 return; 856 857 /* 858 * Try locks do not block if they fail to acquire the lock, thus 859 * there is no danger of deadlocks or of switching while holding a 860 * spin lock if we acquire a lock via a try operation. This 861 * function shouldn't even be called for try locks, so panic if 862 * that happens. 863 */ 864 if (flags & LOP_TRYLOCK) 865 panic("%s should not be called for try lock operations", 866 __func__); 867 868 w = lock->lo_witness; 869 class = LOCK_CLASS(lock); 870 td = curthread; 871 file = fixup_filename(file); 872 873 if (class->lc_flags & LC_SLEEPLOCK) { 874 /* 875 * Since spin locks include a critical section, this check 876 * implicitly enforces a lock order of all sleep locks before 877 * all spin locks. 878 */ 879 if (td->td_critnest != 0 && !kdb_active) 880 panic("blockable sleep lock (%s) %s @ %s:%d", 881 class->lc_name, lock->lo_name, file, line); 882 883 /* 884 * If this is the first lock acquired then just return as 885 * no order checking is needed. 886 */ 887 if (td->td_sleeplocks == NULL) 888 return; 889 lock_list = &td->td_sleeplocks; 890 } else { 891 /* 892 * If this is the first lock, just return as no order 893 * checking is needed. We check this in both if clauses 894 * here as unifying the check would require us to use a 895 * critical section to ensure we don't migrate while doing 896 * the check. Note that if this is not the first lock, we 897 * are already in a critical section and are safe for the 898 * rest of the check. 899 */ 900 if (PCPU_GET(spinlocks) == NULL) 901 return; 902 lock_list = PCPU_PTR(spinlocks); 903 } 904 905 /* 906 * Check to see if we are recursing on a lock we already own. If 907 * so, make sure that we don't mismatch exclusive and shared lock 908 * acquires. 909 */ 910 lock1 = find_instance(*lock_list, lock); 911 if (lock1 != NULL) { 912 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 913 (flags & LOP_EXCLUSIVE) == 0) { 914 printf("shared lock of (%s) %s @ %s:%d\n", 915 class->lc_name, lock->lo_name, file, line); 916 printf("while exclusively locked from %s:%d\n", 917 lock1->li_file, lock1->li_line); 918 panic("share->excl"); 919 } 920 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 921 (flags & LOP_EXCLUSIVE) != 0) { 922 printf("exclusive lock of (%s) %s @ %s:%d\n", 923 class->lc_name, lock->lo_name, file, line); 924 printf("while share locked from %s:%d\n", 925 lock1->li_file, lock1->li_line); 926 panic("excl->share"); 927 } 928 return; 929 } 930 931 /* 932 * Try locks do not block if they fail to acquire the lock, thus 933 * there is no danger of deadlocks or of switching while holding a 934 * spin lock if we acquire a lock via a try operation. 935 */ 936 if (flags & LOP_TRYLOCK) 937 return; 938 939 /* 940 * Check for duplicate locks of the same type. Note that we only 941 * have to check for this on the last lock we just acquired. Any 942 * other cases will be caught as lock order violations. 943 */ 944 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 945 w1 = lock1->li_lock->lo_witness; 946 if (w1 == w) { 947 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) || 948 (flags & LOP_DUPOK)) 949 return; 950 w->w_same_squawked = 1; 951 printf("acquiring duplicate lock of same type: \"%s\"\n", 952 lock->lo_type); 953 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 954 lock1->li_file, lock1->li_line); 955 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 956#ifdef KDB 957 goto debugger; 958#else 959 return; 960#endif 961 } 962 MPASS(!mtx_owned(&w_mtx)); 963 mtx_lock_spin(&w_mtx); 964 /* 965 * If we know that the the lock we are acquiring comes after 966 * the lock we most recently acquired in the lock order tree, 967 * then there is no need for any further checks. 968 */ 969 if (isitmychild(w1, w)) { 970 mtx_unlock_spin(&w_mtx); 971 return; 972 } 973 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 974 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 975 976 MPASS(j < WITNESS_COUNT); 977 lock1 = &lle->ll_children[i]; 978 w1 = lock1->li_lock->lo_witness; 979 980 /* 981 * If this lock doesn't undergo witness checking, 982 * then skip it. 983 */ 984 if (w1 == NULL) { 985 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 986 ("lock missing witness structure")); 987 continue; 988 } 989 /* 990 * If we are locking Giant and this is a sleepable 991 * lock, then skip it. 992 */ 993 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 994 lock == &Giant.lock_object) 995 continue; 996 /* 997 * If we are locking a sleepable lock and this lock 998 * is Giant, then skip it. 999 */ 1000 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 1001 lock1->li_lock == &Giant.lock_object) 1002 continue; 1003 /* 1004 * If we are locking a sleepable lock and this lock 1005 * isn't sleepable, we want to treat it as a lock 1006 * order violation to enfore a general lock order of 1007 * sleepable locks before non-sleepable locks. 1008 */ 1009 if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1010 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1011 goto reversal; 1012 /* 1013 * If we are locking Giant and this is a non-sleepable 1014 * lock, then treat it as a reversal. 1015 */ 1016 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 1017 lock == &Giant.lock_object) 1018 goto reversal; 1019 /* 1020 * Check the lock order hierarchy for a reveresal. 1021 */ 1022 if (!isitmydescendant(w, w1)) 1023 continue; 1024 reversal: 1025 /* 1026 * We have a lock order violation, check to see if it 1027 * is allowed or has already been yelled about. 1028 */ 1029 mtx_unlock_spin(&w_mtx); 1030#ifdef BLESSING 1031 /* 1032 * If the lock order is blessed, just bail. We don't 1033 * look for other lock order violations though, which 1034 * may be a bug. 1035 */ 1036 if (blessed(w, w1)) 1037 return; 1038#endif 1039 if (lock1->li_lock == &Giant.lock_object) { 1040 if (w1->w_Giant_squawked) 1041 return; 1042 else 1043 w1->w_Giant_squawked = 1; 1044 } else { 1045 if (w1->w_other_squawked) 1046 return; 1047 else 1048 w1->w_other_squawked = 1; 1049 } 1050 /* 1051 * Ok, yell about it. 1052 */ 1053 if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1054 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1055 printf( 1056 "lock order reversal: (sleepable after non-sleepable)\n"); 1057 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1058 && lock == &Giant.lock_object) 1059 printf( 1060 "lock order reversal: (Giant after non-sleepable)\n"); 1061 else 1062 printf("lock order reversal:\n"); 1063 /* 1064 * Try to locate an earlier lock with 1065 * witness w in our list. 1066 */ 1067 do { 1068 lock2 = &lle->ll_children[i]; 1069 MPASS(lock2->li_lock != NULL); 1070 if (lock2->li_lock->lo_witness == w) 1071 break; 1072 if (i == 0 && lle->ll_next != NULL) { 1073 lle = lle->ll_next; 1074 i = lle->ll_count - 1; 1075 MPASS(i >= 0 && i < LOCK_NCHILDREN); 1076 } else 1077 i--; 1078 } while (i >= 0); 1079 if (i < 0) { 1080 printf(" 1st %p %s (%s) @ %s:%d\n", 1081 lock1->li_lock, lock1->li_lock->lo_name, 1082 lock1->li_lock->lo_type, lock1->li_file, 1083 lock1->li_line); 1084 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 1085 lock->lo_name, lock->lo_type, file, line); 1086 } else { 1087 printf(" 1st %p %s (%s) @ %s:%d\n", 1088 lock2->li_lock, lock2->li_lock->lo_name, 1089 lock2->li_lock->lo_type, lock2->li_file, 1090 lock2->li_line); 1091 printf(" 2nd %p %s (%s) @ %s:%d\n", 1092 lock1->li_lock, lock1->li_lock->lo_name, 1093 lock1->li_lock->lo_type, lock1->li_file, 1094 lock1->li_line); 1095 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 1096 lock->lo_name, lock->lo_type, file, line); 1097 } 1098#ifdef KDB 1099 goto debugger; 1100#else 1101 return; 1102#endif 1103 } 1104 } 1105 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 1106 /* 1107 * If requested, build a new lock order. However, don't build a new 1108 * relationship between a sleepable lock and Giant if it is in the 1109 * wrong direction. The correct lock order is that sleepable locks 1110 * always come before Giant. 1111 */ 1112 if (flags & LOP_NEWORDER && 1113 !(lock1->li_lock == &Giant.lock_object && 1114 (lock->lo_flags & LO_SLEEPABLE) != 0)) { 1115 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1116 lock->lo_type, lock1->li_lock->lo_type); 1117 if (!itismychild(lock1->li_lock->lo_witness, w)) 1118 /* Witness is dead. */ 1119 return; 1120 } 1121 mtx_unlock_spin(&w_mtx); 1122 return; 1123 1124#ifdef KDB 1125debugger: 1126 if (witness_trace) 1127 kdb_backtrace(); 1128 if (witness_kdb) 1129 kdb_enter(KDB_WHY_WITNESS, __func__); 1130#endif 1131} 1132 1133void 1134witness_lock(struct lock_object *lock, int flags, const char *file, int line) 1135{ 1136 struct lock_list_entry **lock_list, *lle; 1137 struct lock_instance *instance; 1138 struct witness *w; 1139 struct thread *td; 1140 1141 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1142 panicstr != NULL) 1143 return; 1144 w = lock->lo_witness; 1145 td = curthread; 1146 file = fixup_filename(file); 1147 1148 /* Determine lock list for this lock. */ 1149 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1150 lock_list = &td->td_sleeplocks; 1151 else 1152 lock_list = PCPU_PTR(spinlocks); 1153 1154 /* Check to see if we are recursing on a lock we already own. */ 1155 instance = find_instance(*lock_list, lock); 1156 if (instance != NULL) { 1157 instance->li_flags++; 1158 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1159 td->td_proc->p_pid, lock->lo_name, 1160 instance->li_flags & LI_RECURSEMASK); 1161 instance->li_file = file; 1162 instance->li_line = line; 1163 return; 1164 } 1165 1166 /* Update per-witness last file and line acquire. */ 1167 w->w_file = file; 1168 w->w_line = line; 1169 1170 /* Find the next open lock instance in the list and fill it. */ 1171 lle = *lock_list; 1172 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 1173 lle = witness_lock_list_get(); 1174 if (lle == NULL) 1175 return; 1176 lle->ll_next = *lock_list; 1177 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 1178 td->td_proc->p_pid, lle); 1179 *lock_list = lle; 1180 } 1181 instance = &lle->ll_children[lle->ll_count++]; 1182 instance->li_lock = lock; 1183 instance->li_line = line; 1184 instance->li_file = file; 1185 if ((flags & LOP_EXCLUSIVE) != 0) 1186 instance->li_flags = LI_EXCLUSIVE; 1187 else 1188 instance->li_flags = 0; 1189 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 1190 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 1191} 1192 1193void 1194witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 1195{ 1196 struct lock_instance *instance; 1197 struct lock_class *class; 1198 1199 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1200 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1201 return; 1202 class = LOCK_CLASS(lock); 1203 file = fixup_filename(file); 1204 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1205 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 1206 class->lc_name, lock->lo_name, file, line); 1207 if ((flags & LOP_TRYLOCK) == 0) 1208 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 1209 lock->lo_name, file, line); 1210 if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1211 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 1212 class->lc_name, lock->lo_name, file, line); 1213 instance = find_instance(curthread->td_sleeplocks, lock); 1214 if (instance == NULL) 1215 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 1216 class->lc_name, lock->lo_name, file, line); 1217 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1218 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 1219 class->lc_name, lock->lo_name, file, line); 1220 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1221 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1222 class->lc_name, lock->lo_name, 1223 instance->li_flags & LI_RECURSEMASK, file, line); 1224 instance->li_flags |= LI_EXCLUSIVE; 1225} 1226 1227void 1228witness_downgrade(struct lock_object *lock, int flags, const char *file, 1229 int line) 1230{ 1231 struct lock_instance *instance; 1232 struct lock_class *class; 1233 1234 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1235 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1236 return; 1237 class = LOCK_CLASS(lock); 1238 file = fixup_filename(file); 1239 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1240 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 1241 class->lc_name, lock->lo_name, file, line); 1242 if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1243 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 1244 class->lc_name, lock->lo_name, file, line); 1245 instance = find_instance(curthread->td_sleeplocks, lock); 1246 if (instance == NULL) 1247 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 1248 class->lc_name, lock->lo_name, file, line); 1249 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1250 panic("downgrade of shared lock (%s) %s @ %s:%d", 1251 class->lc_name, lock->lo_name, file, line); 1252 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1253 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1254 class->lc_name, lock->lo_name, 1255 instance->li_flags & LI_RECURSEMASK, file, line); 1256 instance->li_flags &= ~LI_EXCLUSIVE; 1257} 1258 1259void 1260witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 1261{ 1262 struct lock_list_entry **lock_list, *lle; 1263 struct lock_instance *instance; 1264 struct lock_class *class; 1265 struct thread *td; 1266 register_t s; 1267 int i, j; 1268 1269 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1270 panicstr != NULL) 1271 return; 1272 td = curthread; 1273 class = LOCK_CLASS(lock); 1274 file = fixup_filename(file); 1275 1276 /* Find lock instance associated with this lock. */ 1277 if (class->lc_flags & LC_SLEEPLOCK) 1278 lock_list = &td->td_sleeplocks; 1279 else 1280 lock_list = PCPU_PTR(spinlocks); 1281 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 1282 for (i = 0; i < (*lock_list)->ll_count; i++) { 1283 instance = &(*lock_list)->ll_children[i]; 1284 if (instance->li_lock == lock) 1285 goto found; 1286 } 1287 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 1288 file, line); 1289found: 1290 1291 /* First, check for shared/exclusive mismatches. */ 1292 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 1293 (flags & LOP_EXCLUSIVE) == 0) { 1294 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1295 lock->lo_name, file, line); 1296 printf("while exclusively locked from %s:%d\n", 1297 instance->li_file, instance->li_line); 1298 panic("excl->ushare"); 1299 } 1300 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 1301 (flags & LOP_EXCLUSIVE) != 0) { 1302 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1303 lock->lo_name, file, line); 1304 printf("while share locked from %s:%d\n", instance->li_file, 1305 instance->li_line); 1306 panic("share->uexcl"); 1307 } 1308 1309 /* If we are recursed, unrecurse. */ 1310 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1311 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1312 td->td_proc->p_pid, instance->li_lock->lo_name, 1313 instance->li_flags); 1314 instance->li_flags--; 1315 return; 1316 } 1317 1318 /* Otherwise, remove this item from the list. */ 1319 s = intr_disable(); 1320 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1321 td->td_proc->p_pid, instance->li_lock->lo_name, 1322 (*lock_list)->ll_count - 1); 1323 for (j = i; j < (*lock_list)->ll_count - 1; j++) 1324 (*lock_list)->ll_children[j] = 1325 (*lock_list)->ll_children[j + 1]; 1326 (*lock_list)->ll_count--; 1327 intr_restore(s); 1328 1329 /* If this lock list entry is now empty, free it. */ 1330 if ((*lock_list)->ll_count == 0) { 1331 lle = *lock_list; 1332 *lock_list = lle->ll_next; 1333 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1334 td->td_proc->p_pid, lle); 1335 witness_lock_list_free(lle); 1336 } 1337} 1338 1339/* 1340 * Warn if any locks other than 'lock' are held. Flags can be passed in to 1341 * exempt Giant and sleepable locks from the checks as well. If any 1342 * non-exempt locks are held, then a supplied message is printed to the 1343 * console along with a list of the offending locks. If indicated in the 1344 * flags then a failure results in a panic as well. 1345 */ 1346int 1347witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 1348{ 1349 struct lock_list_entry *lle; 1350 struct lock_instance *lock1; 1351 struct thread *td; 1352 va_list ap; 1353 int i, n; 1354 1355 if (witness_cold || witness_watch == 0 || panicstr != NULL) 1356 return (0); 1357 n = 0; 1358 td = curthread; 1359 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 1360 for (i = lle->ll_count - 1; i >= 0; i--) { 1361 lock1 = &lle->ll_children[i]; 1362 if (lock1->li_lock == lock) 1363 continue; 1364 if (flags & WARN_GIANTOK && 1365 lock1->li_lock == &Giant.lock_object) 1366 continue; 1367 if (flags & WARN_SLEEPOK && 1368 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 1369 continue; 1370 if (n == 0) { 1371 va_start(ap, fmt); 1372 vprintf(fmt, ap); 1373 va_end(ap); 1374 printf(" with the following"); 1375 if (flags & WARN_SLEEPOK) 1376 printf(" non-sleepable"); 1377 printf(" locks held:\n"); 1378 } 1379 n++; 1380 witness_list_lock(lock1); 1381 } 1382 if (PCPU_GET(spinlocks) != NULL) { 1383 /* 1384 * Since we already hold a spinlock preemption is 1385 * already blocked. 1386 */ 1387 if (n == 0) { 1388 va_start(ap, fmt); 1389 vprintf(fmt, ap); 1390 va_end(ap); 1391 printf(" with the following"); 1392 if (flags & WARN_SLEEPOK) 1393 printf(" non-sleepable"); 1394 printf(" locks held:\n"); 1395 } 1396 n += witness_list_locks(PCPU_PTR(spinlocks)); 1397 } 1398 if (flags & WARN_PANIC && n) 1399 panic("witness_warn"); 1400#ifdef KDB 1401 else if (witness_kdb && n) 1402 kdb_enter(KDB_WHY_WITNESS, __func__); 1403 else if (witness_trace && n) 1404 kdb_backtrace(); 1405#endif 1406 return (n); 1407} 1408 1409const char * 1410witness_file(struct lock_object *lock) 1411{ 1412 struct witness *w; 1413 1414 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1415 return ("?"); 1416 w = lock->lo_witness; 1417 return (w->w_file); 1418} 1419 1420int 1421witness_line(struct lock_object *lock) 1422{ 1423 struct witness *w; 1424 1425 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1426 return (0); 1427 w = lock->lo_witness; 1428 return (w->w_line); 1429} 1430 1431static struct witness * 1432enroll(const char *description, struct lock_class *lock_class) 1433{ 1434 struct witness *w; 1435 1436 if (witness_watch == 0 || panicstr != NULL) 1437 return (NULL); 1438 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 1439 return (NULL); 1440 mtx_lock_spin(&w_mtx); 1441 STAILQ_FOREACH(w, &w_all, w_list) { 1442 if (w->w_name == description || (w->w_refcount > 0 && 1443 strcmp(description, w->w_name) == 0)) { 1444 w->w_refcount++; 1445 mtx_unlock_spin(&w_mtx); 1446 if (lock_class != w->w_class) 1447 panic( 1448 "lock (%s) %s does not match earlier (%s) lock", 1449 description, lock_class->lc_name, 1450 w->w_class->lc_name); 1451 return (w); 1452 } 1453 } 1454 if ((w = witness_get()) == NULL) 1455 goto out; 1456 w->w_name = description; 1457 w->w_class = lock_class; 1458 w->w_refcount = 1; 1459 STAILQ_INSERT_HEAD(&w_all, w, w_list); 1460 if (lock_class->lc_flags & LC_SPINLOCK) { 1461 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1462 w_spin_cnt++; 1463 } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 1464 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1465 w_sleep_cnt++; 1466 } else { 1467 mtx_unlock_spin(&w_mtx); 1468 panic("lock class %s is not sleep or spin", 1469 lock_class->lc_name); 1470 } 1471 mtx_unlock_spin(&w_mtx); 1472out: 1473 /* 1474 * We issue a warning for any spin locks not defined in the static 1475 * order list as a way to discourage their use (folks should really 1476 * be using non-spin mutexes most of the time). However, several 1477 * 3rd part device drivers use spin locks because that is all they 1478 * have available on Windows and Linux and they think that normal 1479 * mutexes are insufficient. 1480 */ 1481 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_spin_warn) 1482 printf("WITNESS: spin lock %s not in order list\n", 1483 description); 1484 return (w); 1485} 1486 1487/* Don't let the door bang you on the way out... */ 1488static int 1489depart(struct witness *w) 1490{ 1491 struct witness_child_list_entry *wcl, *nwcl; 1492 struct witness_list *list; 1493 struct witness *parent; 1494 1495 MPASS(w->w_refcount == 0); 1496 if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1497 list = &w_sleep; 1498 w_sleep_cnt--; 1499 } else { 1500 list = &w_spin; 1501 w_spin_cnt--; 1502 } 1503 /* 1504 * First, we run through the entire tree looking for any 1505 * witnesses that the outgoing witness is a child of. For 1506 * each parent that we find, we reparent all the direct 1507 * children of the outgoing witness to its parent. 1508 */ 1509 STAILQ_FOREACH(parent, list, w_typelist) { 1510 if (!isitmychild(parent, w)) 1511 continue; 1512 removechild(parent, w); 1513 } 1514 1515 /* 1516 * Now we go through and free up the child list of the 1517 * outgoing witness. 1518 */ 1519 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1520 nwcl = wcl->wcl_next; 1521 w_child_cnt--; 1522 witness_child_free(wcl); 1523 } 1524 1525 /* 1526 * Detach from various lists and free. 1527 */ 1528 STAILQ_REMOVE(list, w, witness, w_typelist); 1529 STAILQ_REMOVE(&w_all, w, witness, w_list); 1530 witness_free(w); 1531 1532 return (1); 1533} 1534 1535/* 1536 * Add "child" as a direct child of "parent". Returns false if 1537 * we fail due to out of memory. 1538 */ 1539static int 1540insertchild(struct witness *parent, struct witness *child) 1541{ 1542 struct witness_child_list_entry **wcl; 1543 1544 MPASS(child != NULL && parent != NULL); 1545 1546 /* 1547 * Insert "child" after "parent" 1548 */ 1549 wcl = &parent->w_children; 1550 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 1551 wcl = &(*wcl)->wcl_next; 1552 if (*wcl == NULL) { 1553 *wcl = witness_child_get(); 1554 if (*wcl == NULL) 1555 return (0); 1556 w_child_cnt++; 1557 } 1558 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1559 1560 return (1); 1561} 1562 1563 1564static int 1565itismychild(struct witness *parent, struct witness *child) 1566{ 1567 struct witness_list *list; 1568 1569 MPASS(child != NULL && parent != NULL); 1570 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1571 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1572 panic( 1573 "%s: parent (%s) and child (%s) are not the same lock type", 1574 __func__, parent->w_class->lc_name, 1575 child->w_class->lc_name); 1576 1577 if (!insertchild(parent, child)) 1578 return (0); 1579 1580 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1581 list = &w_sleep; 1582 else 1583 list = &w_spin; 1584 return (1); 1585} 1586 1587static void 1588removechild(struct witness *parent, struct witness *child) 1589{ 1590 struct witness_child_list_entry **wcl, *wcl1; 1591 int i; 1592 1593 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1594 for (i = 0; i < (*wcl)->wcl_count; i++) 1595 if ((*wcl)->wcl_children[i] == child) 1596 goto found; 1597 return; 1598found: 1599 (*wcl)->wcl_count--; 1600 if ((*wcl)->wcl_count > i) 1601 (*wcl)->wcl_children[i] = 1602 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1603 MPASS((*wcl)->wcl_children[i] != NULL); 1604 if ((*wcl)->wcl_count != 0) 1605 return; 1606 wcl1 = *wcl; 1607 *wcl = wcl1->wcl_next; 1608 w_child_cnt--; 1609 witness_child_free(wcl1); 1610} 1611 1612static int 1613isitmychild(struct witness *parent, struct witness *child) 1614{ 1615 struct witness_child_list_entry *wcl; 1616 int i; 1617 1618 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1619 for (i = 0; i < wcl->wcl_count; i++) { 1620 if (wcl->wcl_children[i] == child) 1621 return (1); 1622 } 1623 } 1624 return (0); 1625} 1626 1627static int 1628isitmydescendant(struct witness *parent, struct witness *child) 1629{ 1630 struct witness_child_list_entry *wcl; 1631 int i, j; 1632 1633 if (isitmychild(parent, child)) 1634 return (1); 1635 j = 0; 1636 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1637 MPASS(j < 1000); 1638 for (i = 0; i < wcl->wcl_count; i++) { 1639 if (isitmydescendant(wcl->wcl_children[i], child)) 1640 return (1); 1641 } 1642 j++; 1643 } 1644 return (0); 1645} 1646 1647#ifdef BLESSING 1648static int 1649blessed(struct witness *w1, struct witness *w2) 1650{ 1651 int i; 1652 struct witness_blessed *b; 1653 1654 for (i = 0; i < blessed_count; i++) { 1655 b = &blessed_list[i]; 1656 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1657 if (strcmp(w2->w_name, b->b_lock2) == 0) 1658 return (1); 1659 continue; 1660 } 1661 if (strcmp(w1->w_name, b->b_lock2) == 0) 1662 if (strcmp(w2->w_name, b->b_lock1) == 0) 1663 return (1); 1664 } 1665 return (0); 1666} 1667#endif 1668 1669static struct witness * 1670witness_get(void) 1671{ 1672 struct witness *w; 1673 1674 if (witness_watch == 0) { 1675 mtx_unlock_spin(&w_mtx); 1676 return (NULL); 1677 } 1678 if (STAILQ_EMPTY(&w_free)) { 1679 witness_watch = 0; 1680 mtx_unlock_spin(&w_mtx); 1681 printf("%s: witness exhausted\n", __func__); 1682 return (NULL); 1683 } 1684 w = STAILQ_FIRST(&w_free); 1685 STAILQ_REMOVE_HEAD(&w_free, w_list); 1686 w_free_cnt--; 1687 bzero(w, sizeof(*w)); 1688 return (w); 1689} 1690 1691static void 1692witness_free(struct witness *w) 1693{ 1694 1695 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1696 w_free_cnt++; 1697} 1698 1699static struct witness_child_list_entry * 1700witness_child_get(void) 1701{ 1702 struct witness_child_list_entry *wcl; 1703 1704 if (witness_watch == 0) { 1705 mtx_unlock_spin(&w_mtx); 1706 return (NULL); 1707 } 1708 wcl = w_child_free; 1709 if (wcl == NULL) { 1710 witness_watch = 0; 1711 mtx_unlock_spin(&w_mtx); 1712 printf("%s: witness exhausted\n", __func__); 1713 return (NULL); 1714 } 1715 w_child_free = wcl->wcl_next; 1716 w_child_free_cnt--; 1717 bzero(wcl, sizeof(*wcl)); 1718 return (wcl); 1719} 1720 1721static void 1722witness_child_free(struct witness_child_list_entry *wcl) 1723{ 1724 1725 wcl->wcl_next = w_child_free; 1726 w_child_free = wcl; 1727 w_child_free_cnt++; 1728} 1729 1730static struct lock_list_entry * 1731witness_lock_list_get(void) 1732{ 1733 struct lock_list_entry *lle; 1734 1735 if (witness_watch == 0) 1736 return (NULL); 1737 mtx_lock_spin(&w_mtx); 1738 lle = w_lock_list_free; 1739 if (lle == NULL) { 1740 witness_watch = 0; 1741 mtx_unlock_spin(&w_mtx); 1742 printf("%s: witness exhausted\n", __func__); 1743 return (NULL); 1744 } 1745 w_lock_list_free = lle->ll_next; 1746 mtx_unlock_spin(&w_mtx); 1747 bzero(lle, sizeof(*lle)); 1748 return (lle); 1749} 1750 1751static void 1752witness_lock_list_free(struct lock_list_entry *lle) 1753{ 1754 1755 mtx_lock_spin(&w_mtx); 1756 lle->ll_next = w_lock_list_free; 1757 w_lock_list_free = lle; 1758 mtx_unlock_spin(&w_mtx); 1759} 1760 1761static struct lock_instance * 1762find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1763{ 1764 struct lock_list_entry *lle; 1765 struct lock_instance *instance; 1766 int i; 1767 1768 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1769 for (i = lle->ll_count - 1; i >= 0; i--) { 1770 instance = &lle->ll_children[i]; 1771 if (instance->li_lock == lock) 1772 return (instance); 1773 } 1774 return (NULL); 1775} 1776 1777static void 1778witness_list_lock(struct lock_instance *instance) 1779{ 1780 struct lock_object *lock; 1781 1782 lock = instance->li_lock; 1783 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1784 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 1785 if (lock->lo_type != lock->lo_name) 1786 printf(" (%s)", lock->lo_type); 1787 printf(" r = %d (%p) locked @ %s:%d\n", 1788 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1789 instance->li_line); 1790} 1791 1792#ifdef DDB 1793static int 1794witness_thread_has_locks(struct thread *td) 1795{ 1796 1797 return (td->td_sleeplocks != NULL); 1798} 1799 1800static int 1801witness_proc_has_locks(struct proc *p) 1802{ 1803 struct thread *td; 1804 1805 FOREACH_THREAD_IN_PROC(p, td) { 1806 if (witness_thread_has_locks(td)) 1807 return (1); 1808 } 1809 return (0); 1810} 1811#endif 1812 1813int 1814witness_list_locks(struct lock_list_entry **lock_list) 1815{ 1816 struct lock_list_entry *lle; 1817 int i, nheld; 1818 1819 nheld = 0; 1820 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1821 for (i = lle->ll_count - 1; i >= 0; i--) { 1822 witness_list_lock(&lle->ll_children[i]); 1823 nheld++; 1824 } 1825 return (nheld); 1826} 1827 1828/* 1829 * This is a bit risky at best. We call this function when we have timed 1830 * out acquiring a spin lock, and we assume that the other CPU is stuck 1831 * with this lock held. So, we go groveling around in the other CPU's 1832 * per-cpu data to try to find the lock instance for this spin lock to 1833 * see when it was last acquired. 1834 */ 1835void 1836witness_display_spinlock(struct lock_object *lock, struct thread *owner) 1837{ 1838 struct lock_instance *instance; 1839 struct pcpu *pc; 1840 1841 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 1842 return; 1843 pc = pcpu_find(owner->td_oncpu); 1844 instance = find_instance(pc->pc_spinlocks, lock); 1845 if (instance != NULL) 1846 witness_list_lock(instance); 1847} 1848 1849void 1850witness_save(struct lock_object *lock, const char **filep, int *linep) 1851{ 1852 struct lock_list_entry *lock_list; 1853 struct lock_instance *instance; 1854 struct lock_class *class; 1855 1856 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1857 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1858 return; 1859 class = LOCK_CLASS(lock); 1860 if (class->lc_flags & LC_SLEEPLOCK) 1861 lock_list = curthread->td_sleeplocks; 1862 else { 1863 if (witness_skipspin) 1864 return; 1865 lock_list = PCPU_GET(spinlocks); 1866 } 1867 instance = find_instance(lock_list, lock); 1868 if (instance == NULL) 1869 panic("%s: lock (%s) %s not locked", __func__, 1870 class->lc_name, lock->lo_name); 1871 *filep = instance->li_file; 1872 *linep = instance->li_line; 1873} 1874 1875void 1876witness_restore(struct lock_object *lock, const char *file, int line) 1877{ 1878 struct lock_list_entry *lock_list; 1879 struct lock_instance *instance; 1880 struct lock_class *class; 1881 1882 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1883 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1884 return; 1885 class = LOCK_CLASS(lock); 1886 if (class->lc_flags & LC_SLEEPLOCK) 1887 lock_list = curthread->td_sleeplocks; 1888 else { 1889 if (witness_skipspin) 1890 return; 1891 lock_list = PCPU_GET(spinlocks); 1892 } 1893 instance = find_instance(lock_list, lock); 1894 if (instance == NULL) 1895 panic("%s: lock (%s) %s not locked", __func__, 1896 class->lc_name, lock->lo_name); 1897 lock->lo_witness->w_file = file; 1898 lock->lo_witness->w_line = line; 1899 instance->li_file = file; 1900 instance->li_line = line; 1901} 1902 1903void 1904witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1905{ 1906#ifdef INVARIANT_SUPPORT 1907 struct lock_instance *instance; 1908 struct lock_class *class; 1909 1910 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1911 return; 1912 class = LOCK_CLASS(lock); 1913 if ((class->lc_flags & LC_SLEEPLOCK) != 0) 1914 instance = find_instance(curthread->td_sleeplocks, lock); 1915 else if ((class->lc_flags & LC_SPINLOCK) != 0) 1916 instance = find_instance(PCPU_GET(spinlocks), lock); 1917 else { 1918 panic("Lock (%s) %s is not sleep or spin!", 1919 class->lc_name, lock->lo_name); 1920 } 1921 file = fixup_filename(file); 1922 switch (flags) { 1923 case LA_UNLOCKED: 1924 if (instance != NULL) 1925 panic("Lock (%s) %s locked @ %s:%d.", 1926 class->lc_name, lock->lo_name, file, line); 1927 break; 1928 case LA_LOCKED: 1929 case LA_LOCKED | LA_RECURSED: 1930 case LA_LOCKED | LA_NOTRECURSED: 1931 case LA_SLOCKED: 1932 case LA_SLOCKED | LA_RECURSED: 1933 case LA_SLOCKED | LA_NOTRECURSED: 1934 case LA_XLOCKED: 1935 case LA_XLOCKED | LA_RECURSED: 1936 case LA_XLOCKED | LA_NOTRECURSED: 1937 if (instance == NULL) { 1938 panic("Lock (%s) %s not locked @ %s:%d.", 1939 class->lc_name, lock->lo_name, file, line); 1940 break; 1941 } 1942 if ((flags & LA_XLOCKED) != 0 && 1943 (instance->li_flags & LI_EXCLUSIVE) == 0) 1944 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1945 class->lc_name, lock->lo_name, file, line); 1946 if ((flags & LA_SLOCKED) != 0 && 1947 (instance->li_flags & LI_EXCLUSIVE) != 0) 1948 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1949 class->lc_name, lock->lo_name, file, line); 1950 if ((flags & LA_RECURSED) != 0 && 1951 (instance->li_flags & LI_RECURSEMASK) == 0) 1952 panic("Lock (%s) %s not recursed @ %s:%d.", 1953 class->lc_name, lock->lo_name, file, line); 1954 if ((flags & LA_NOTRECURSED) != 0 && 1955 (instance->li_flags & LI_RECURSEMASK) != 0) 1956 panic("Lock (%s) %s recursed @ %s:%d.", 1957 class->lc_name, lock->lo_name, file, line); 1958 break; 1959 default: 1960 panic("Invalid lock assertion at %s:%d.", file, line); 1961 1962 } 1963#endif /* INVARIANT_SUPPORT */ 1964} 1965 1966#ifdef DDB 1967static void 1968witness_list(struct thread *td) 1969{ 1970 1971 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1972 KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 1973 1974 if (witness_watch == 0) 1975 return; 1976 1977 witness_list_locks(&td->td_sleeplocks); 1978 1979 /* 1980 * We only handle spinlocks if td == curthread. This is somewhat broken 1981 * if td is currently executing on some other CPU and holds spin locks 1982 * as we won't display those locks. If we had a MI way of getting 1983 * the per-cpu data for a given cpu then we could use 1984 * td->td_oncpu to get the list of spinlocks for this thread 1985 * and "fix" this. 1986 * 1987 * That still wouldn't really fix this unless we locked the scheduler 1988 * lock or stopped the other CPU to make sure it wasn't changing the 1989 * list out from under us. It is probably best to just not try to 1990 * handle threads on other CPU's for now. 1991 */ 1992 if (td == curthread && PCPU_GET(spinlocks) != NULL) 1993 witness_list_locks(PCPU_PTR(spinlocks)); 1994} 1995 1996DB_SHOW_COMMAND(locks, db_witness_list) 1997{ 1998 struct thread *td; 1999 2000 if (have_addr) 2001 td = db_lookup_thread(addr, TRUE); 2002 else 2003 td = kdb_thread; 2004 witness_list(td); 2005} 2006 2007DB_SHOW_COMMAND(alllocks, db_witness_list_all) 2008{ 2009 struct thread *td; 2010 struct proc *p; 2011 2012 /* 2013 * It would be nice to list only threads and processes that actually 2014 * held sleep locks, but that information is currently not exported 2015 * by WITNESS. 2016 */ 2017 FOREACH_PROC_IN_SYSTEM(p) { 2018 if (!witness_proc_has_locks(p)) 2019 continue; 2020 FOREACH_THREAD_IN_PROC(p, td) { 2021 if (!witness_thread_has_locks(td)) 2022 continue; 2023 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2024 td->td_name, td, td->td_tid); 2025 witness_list(td); 2026 } 2027 } 2028} 2029 2030DB_SHOW_COMMAND(witness, db_witness_display) 2031{ 2032 2033 witness_display(db_printf); 2034} 2035#endif 2036