subr_witness.c revision 170302
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 170302 2007-06-04 23:55:45Z jeff $"); 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 * Netgraph 368 */ 369 { "ng_node", &lock_class_mtx_sleep }, 370 { "ng_worklist", &lock_class_mtx_sleep }, 371 { NULL, NULL }, 372 /* 373 * CDEV 374 */ 375 { "system map", &lock_class_mtx_sleep }, 376 { "vm page queue mutex", &lock_class_mtx_sleep }, 377 { "vnode interlock", &lock_class_mtx_sleep }, 378 { "cdev", &lock_class_mtx_sleep }, 379 { NULL, NULL }, 380 /* 381 * kqueue/VFS interaction 382 */ 383 { "kqueue", &lock_class_mtx_sleep }, 384 { "struct mount mtx", &lock_class_mtx_sleep }, 385 { "vnode interlock", &lock_class_mtx_sleep }, 386 { NULL, NULL }, 387 /* 388 * spin locks 389 */ 390#ifdef SMP 391 { "ap boot", &lock_class_mtx_spin }, 392#endif 393 { "rm.mutex_mtx", &lock_class_mtx_spin }, 394 { "sio", &lock_class_mtx_spin }, 395#ifdef __i386__ 396 { "cy", &lock_class_mtx_spin }, 397 { "descriptor tables", &lock_class_mtx_spin }, 398#endif 399 { "scc_hwmtx", &lock_class_mtx_spin }, 400 { "uart_hwmtx", &lock_class_mtx_spin }, 401 { "zstty", &lock_class_mtx_spin }, 402 { "fast_taskqueue", &lock_class_mtx_spin }, 403 { "intr table", &lock_class_mtx_spin }, 404#ifdef HWPMC_HOOKS 405 { "pmc-per-proc", &lock_class_mtx_spin }, 406#endif 407 { "process slock", &lock_class_mtx_spin }, 408 { "sleepq chain", &lock_class_mtx_spin }, 409 { "umtx lock", &lock_class_mtx_spin }, 410 { "turnstile chain", &lock_class_mtx_spin }, 411 { "turnstile lock", &lock_class_mtx_spin }, 412 { "sched lock", &lock_class_mtx_spin }, 413 { "td_contested", &lock_class_mtx_spin }, 414 { "callout", &lock_class_mtx_spin }, 415 { "entropy harvest mutex", &lock_class_mtx_spin }, 416 { "syscons video lock", &lock_class_mtx_spin }, 417 { "time lock", &lock_class_mtx_spin }, 418 /* 419 * leaf locks 420 */ 421 { "allpmaps", &lock_class_mtx_spin }, 422 { "icu", &lock_class_mtx_spin }, 423#ifdef SMP 424 { "smp rendezvous", &lock_class_mtx_spin }, 425#if defined(__i386__) || defined(__amd64__) 426 { "tlb", &lock_class_mtx_spin }, 427#endif 428#ifdef __sparc64__ 429 { "ipi", &lock_class_mtx_spin }, 430 { "rtc_mtx", &lock_class_mtx_spin }, 431#endif 432#endif 433 { "clk", &lock_class_mtx_spin }, 434 { "mutex profiling lock", &lock_class_mtx_spin }, 435 { "kse lock", &lock_class_mtx_spin }, 436 { "zombie lock", &lock_class_mtx_spin }, 437 { "ALD Queue", &lock_class_mtx_spin }, 438#ifdef __ia64__ 439 { "MCA spin lock", &lock_class_mtx_spin }, 440#endif 441#if defined(__i386__) || defined(__amd64__) 442 { "pcicfg", &lock_class_mtx_spin }, 443 { "NDIS thread lock", &lock_class_mtx_spin }, 444#endif 445 { "tw_osl_io_lock", &lock_class_mtx_spin }, 446 { "tw_osl_q_lock", &lock_class_mtx_spin }, 447 { "tw_cl_io_lock", &lock_class_mtx_spin }, 448 { "tw_cl_intr_lock", &lock_class_mtx_spin }, 449 { "tw_cl_gen_lock", &lock_class_mtx_spin }, 450#ifdef HWPMC_HOOKS 451 { "pmc-leaf", &lock_class_mtx_spin }, 452#endif 453 { "blocked lock", &lock_class_mtx_spin }, 454 { NULL, NULL }, 455 { NULL, NULL } 456}; 457 458#ifdef BLESSING 459/* 460 * Pairs of locks which have been blessed 461 * Don't complain about order problems with blessed locks 462 */ 463static struct witness_blessed blessed_list[] = { 464}; 465static int blessed_count = 466 sizeof(blessed_list) / sizeof(struct witness_blessed); 467#endif 468 469/* 470 * List of locks initialized prior to witness being initialized whose 471 * enrollment is currently deferred. 472 */ 473STAILQ_HEAD(, lock_object) pending_locks = 474 STAILQ_HEAD_INITIALIZER(pending_locks); 475 476/* 477 * This global is set to 0 once it becomes safe to use the witness code. 478 */ 479static int witness_cold = 1; 480 481/* 482 * This global is set to 1 once the static lock orders have been enrolled 483 * so that a warning can be issued for any spin locks enrolled later. 484 */ 485static int witness_spin_warn = 0; 486 487/* 488 * The WITNESS-enabled diagnostic code. Note that the witness code does 489 * assume that the early boot is single-threaded at least until after this 490 * routine is completed. 491 */ 492static void 493witness_initialize(void *dummy __unused) 494{ 495 struct lock_object *lock; 496 struct witness_order_list_entry *order; 497 struct witness *w, *w1; 498 int i; 499 500 /* 501 * We have to release Giant before initializing its witness 502 * structure so that WITNESS doesn't get confused. 503 */ 504 mtx_unlock(&Giant); 505 mtx_assert(&Giant, MA_NOTOWNED); 506 507 CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 508 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 509 MTX_NOWITNESS | MTX_NOPROFILE); 510 for (i = 0; i < WITNESS_COUNT; i++) 511 witness_free(&w_data[i]); 512 for (i = 0; i < WITNESS_CHILDCOUNT; i++) 513 witness_child_free(&w_childdata[i]); 514 for (i = 0; i < LOCK_CHILDCOUNT; i++) 515 witness_lock_list_free(&w_locklistdata[i]); 516 517 /* First add in all the specified order lists. */ 518 for (order = order_lists; order->w_name != NULL; order++) { 519 w = enroll(order->w_name, order->w_class); 520 if (w == NULL) 521 continue; 522 w->w_file = "order list"; 523 for (order++; order->w_name != NULL; order++) { 524 w1 = enroll(order->w_name, order->w_class); 525 if (w1 == NULL) 526 continue; 527 w1->w_file = "order list"; 528 if (!itismychild(w, w1)) 529 panic("Not enough memory for static orders!"); 530 w = w1; 531 } 532 } 533 witness_spin_warn = 1; 534 535 /* Iterate through all locks and add them to witness. */ 536 while (!STAILQ_EMPTY(&pending_locks)) { 537 lock = STAILQ_FIRST(&pending_locks); 538 STAILQ_REMOVE_HEAD(&pending_locks, lo_list); 539 KASSERT(lock->lo_flags & LO_WITNESS, 540 ("%s: lock %s is on pending list but not LO_WITNESS", 541 __func__, lock->lo_name)); 542 lock->lo_witness = enroll(lock->lo_type, LOCK_CLASS(lock)); 543 } 544 545 /* Mark the witness code as being ready for use. */ 546 witness_cold = 0; 547 548 mtx_lock(&Giant); 549} 550SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 551 552static int 553sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 554{ 555 int error, value; 556 557 value = witness_watch; 558 error = sysctl_handle_int(oidp, &value, 0, req); 559 if (error != 0 || req->newptr == NULL) 560 return (error); 561 if (value == witness_watch) 562 return (0); 563 if (value != 0) 564 return (EINVAL); 565 witness_watch = 0; 566 return (0); 567} 568 569void 570witness_init(struct lock_object *lock) 571{ 572 struct lock_class *class; 573 574 /* Various sanity checks. */ 575 class = LOCK_CLASS(lock); 576 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 577 (class->lc_flags & LC_RECURSABLE) == 0) 578 panic("%s: lock (%s) %s can not be recursable", __func__, 579 class->lc_name, lock->lo_name); 580 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 581 (class->lc_flags & LC_SLEEPABLE) == 0) 582 panic("%s: lock (%s) %s can not be sleepable", __func__, 583 class->lc_name, lock->lo_name); 584 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 585 (class->lc_flags & LC_UPGRADABLE) == 0) 586 panic("%s: lock (%s) %s can not be upgradable", __func__, 587 class->lc_name, lock->lo_name); 588 589 /* 590 * If we shouldn't watch this lock, then just clear lo_witness. 591 * Otherwise, if witness_cold is set, then it is too early to 592 * enroll this lock, so defer it to witness_initialize() by adding 593 * it to the pending_locks list. If it is not too early, then enroll 594 * the lock now. 595 */ 596 if (witness_watch == 0 || panicstr != NULL || 597 (lock->lo_flags & LO_WITNESS) == 0) 598 lock->lo_witness = NULL; 599 else if (witness_cold) { 600 STAILQ_INSERT_TAIL(&pending_locks, lock, lo_list); 601 lock->lo_flags |= LO_ENROLLPEND; 602 } else 603 lock->lo_witness = enroll(lock->lo_type, class); 604} 605 606void 607witness_destroy(struct lock_object *lock) 608{ 609 struct lock_class *class; 610 struct witness *w; 611 612 class = LOCK_CLASS(lock); 613 if (witness_cold) 614 panic("lock (%s) %s destroyed while witness_cold", 615 class->lc_name, lock->lo_name); 616 617 /* XXX: need to verify that no one holds the lock */ 618 if ((lock->lo_flags & (LO_WITNESS | LO_ENROLLPEND)) == LO_WITNESS && 619 lock->lo_witness != NULL) { 620 w = lock->lo_witness; 621 mtx_lock_spin(&w_mtx); 622 MPASS(w->w_refcount > 0); 623 w->w_refcount--; 624 625 /* 626 * Lock is already released if we have an allocation failure 627 * and depart() fails. 628 */ 629 if (w->w_refcount != 0 || depart(w)) 630 mtx_unlock_spin(&w_mtx); 631 } 632 633 /* 634 * If this lock is destroyed before witness is up and running, 635 * remove it from the pending list. 636 */ 637 if (lock->lo_flags & LO_ENROLLPEND) { 638 STAILQ_REMOVE(&pending_locks, lock, lock_object, lo_list); 639 lock->lo_flags &= ~LO_ENROLLPEND; 640 } 641} 642 643#ifdef DDB 644static void 645witness_levelall (void) 646{ 647 struct witness_list *list; 648 struct witness *w, *w1; 649 650 /* 651 * First clear all levels. 652 */ 653 STAILQ_FOREACH(w, &w_all, w_list) { 654 w->w_level = 0; 655 } 656 657 /* 658 * Look for locks with no parent and level all their descendants. 659 */ 660 STAILQ_FOREACH(w, &w_all, w_list) { 661 /* 662 * This is just an optimization, technically we could get 663 * away just walking the all list each time. 664 */ 665 if (w->w_class->lc_flags & LC_SLEEPLOCK) 666 list = &w_sleep; 667 else 668 list = &w_spin; 669 STAILQ_FOREACH(w1, list, w_typelist) { 670 if (isitmychild(w1, w)) 671 goto skip; 672 } 673 witness_leveldescendents(w, 0); 674 skip: 675 ; /* silence GCC 3.x */ 676 } 677} 678 679static void 680witness_leveldescendents(struct witness *parent, int level) 681{ 682 struct witness_child_list_entry *wcl; 683 int i; 684 685 if (parent->w_level < level) 686 parent->w_level = level; 687 level++; 688 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 689 for (i = 0; i < wcl->wcl_count; i++) 690 witness_leveldescendents(wcl->wcl_children[i], level); 691} 692 693static void 694witness_displaydescendants(void(*prnt)(const char *fmt, ...), 695 struct witness *parent, int indent) 696{ 697 struct witness_child_list_entry *wcl; 698 int i, level; 699 700 level = parent->w_level; 701 prnt("%-2d", level); 702 for (i = 0; i < indent; i++) 703 prnt(" "); 704 if (parent->w_refcount > 0) 705 prnt("%s", parent->w_name); 706 else 707 prnt("(dead)"); 708 if (parent->w_displayed) { 709 prnt(" -- (already displayed)\n"); 710 return; 711 } 712 parent->w_displayed = 1; 713 if (parent->w_refcount > 0) { 714 if (parent->w_file != NULL) 715 prnt(" -- last acquired @ %s:%d", parent->w_file, 716 parent->w_line); 717 } 718 prnt("\n"); 719 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 720 for (i = 0; i < wcl->wcl_count; i++) 721 witness_displaydescendants(prnt, 722 wcl->wcl_children[i], indent + 1); 723} 724 725static void 726witness_display_list(void(*prnt)(const char *fmt, ...), 727 struct witness_list *list) 728{ 729 struct witness *w; 730 731 STAILQ_FOREACH(w, list, w_typelist) { 732 if (w->w_file == NULL || w->w_level > 0) 733 continue; 734 /* 735 * This lock has no anscestors, display its descendants. 736 */ 737 witness_displaydescendants(prnt, w, 0); 738 } 739} 740 741static void 742witness_display(void(*prnt)(const char *fmt, ...)) 743{ 744 struct witness *w; 745 746 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 747 witness_levelall(); 748 749 /* Clear all the displayed flags. */ 750 STAILQ_FOREACH(w, &w_all, w_list) { 751 w->w_displayed = 0; 752 } 753 754 /* 755 * First, handle sleep locks which have been acquired at least 756 * once. 757 */ 758 prnt("Sleep locks:\n"); 759 witness_display_list(prnt, &w_sleep); 760 761 /* 762 * Now do spin locks which have been acquired at least once. 763 */ 764 prnt("\nSpin locks:\n"); 765 witness_display_list(prnt, &w_spin); 766 767 /* 768 * Finally, any locks which have not been acquired yet. 769 */ 770 prnt("\nLocks which were never acquired:\n"); 771 STAILQ_FOREACH(w, &w_all, w_list) { 772 if (w->w_file != NULL || w->w_refcount == 0) 773 continue; 774 prnt("%s\n", w->w_name); 775 } 776} 777#endif /* DDB */ 778 779/* Trim useless garbage from filenames. */ 780static const char * 781fixup_filename(const char *file) 782{ 783 784 if (file == NULL) 785 return (NULL); 786 while (strncmp(file, "../", 3) == 0) 787 file += 3; 788 return (file); 789} 790 791int 792witness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 793{ 794 795 if (witness_watch == 0 || panicstr != NULL) 796 return (0); 797 798 /* Require locks that witness knows about. */ 799 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 800 lock2->lo_witness == NULL) 801 return (EINVAL); 802 803 MPASS(!mtx_owned(&w_mtx)); 804 mtx_lock_spin(&w_mtx); 805 806 /* 807 * If we already have either an explicit or implied lock order that 808 * is the other way around, then return an error. 809 */ 810 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 811 mtx_unlock_spin(&w_mtx); 812 return (EDOOFUS); 813 } 814 815 /* Try to add the new order. */ 816 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 817 lock2->lo_type, lock1->lo_type); 818 if (!itismychild(lock1->lo_witness, lock2->lo_witness)) 819 return (ENOMEM); 820 mtx_unlock_spin(&w_mtx); 821 return (0); 822} 823 824void 825witness_checkorder(struct lock_object *lock, int flags, const char *file, 826 int line) 827{ 828 struct lock_list_entry **lock_list, *lle; 829 struct lock_instance *lock1, *lock2; 830 struct lock_class *class; 831 struct witness *w, *w1; 832 struct thread *td; 833 int i, j; 834 835 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 836 panicstr != NULL) 837 return; 838 839 /* 840 * Try locks do not block if they fail to acquire the lock, thus 841 * there is no danger of deadlocks or of switching while holding a 842 * spin lock if we acquire a lock via a try operation. This 843 * function shouldn't even be called for try locks, so panic if 844 * that happens. 845 */ 846 if (flags & LOP_TRYLOCK) 847 panic("%s should not be called for try lock operations", 848 __func__); 849 850 w = lock->lo_witness; 851 class = LOCK_CLASS(lock); 852 td = curthread; 853 file = fixup_filename(file); 854 855 if (class->lc_flags & LC_SLEEPLOCK) { 856 /* 857 * Since spin locks include a critical section, this check 858 * implicitly enforces a lock order of all sleep locks before 859 * all spin locks. 860 */ 861 if (td->td_critnest != 0 && !kdb_active) 862 panic("blockable sleep lock (%s) %s @ %s:%d", 863 class->lc_name, lock->lo_name, file, line); 864 865 /* 866 * If this is the first lock acquired then just return as 867 * no order checking is needed. 868 */ 869 if (td->td_sleeplocks == NULL) 870 return; 871 lock_list = &td->td_sleeplocks; 872 } else { 873 /* 874 * If this is the first lock, just return as no order 875 * checking is needed. We check this in both if clauses 876 * here as unifying the check would require us to use a 877 * critical section to ensure we don't migrate while doing 878 * the check. Note that if this is not the first lock, we 879 * are already in a critical section and are safe for the 880 * rest of the check. 881 */ 882 if (PCPU_GET(spinlocks) == NULL) 883 return; 884 lock_list = PCPU_PTR(spinlocks); 885 } 886 887 /* 888 * Check to see if we are recursing on a lock we already own. If 889 * so, make sure that we don't mismatch exclusive and shared lock 890 * acquires. 891 */ 892 lock1 = find_instance(*lock_list, lock); 893 if (lock1 != NULL) { 894 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 895 (flags & LOP_EXCLUSIVE) == 0) { 896 printf("shared lock of (%s) %s @ %s:%d\n", 897 class->lc_name, lock->lo_name, file, line); 898 printf("while exclusively locked from %s:%d\n", 899 lock1->li_file, lock1->li_line); 900 panic("share->excl"); 901 } 902 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 903 (flags & LOP_EXCLUSIVE) != 0) { 904 printf("exclusive lock of (%s) %s @ %s:%d\n", 905 class->lc_name, lock->lo_name, file, line); 906 printf("while share locked from %s:%d\n", 907 lock1->li_file, lock1->li_line); 908 panic("excl->share"); 909 } 910 return; 911 } 912 913 /* 914 * Try locks do not block if they fail to acquire the lock, thus 915 * there is no danger of deadlocks or of switching while holding a 916 * spin lock if we acquire a lock via a try operation. 917 */ 918 if (flags & LOP_TRYLOCK) 919 return; 920 921 /* 922 * Check for duplicate locks of the same type. Note that we only 923 * have to check for this on the last lock we just acquired. Any 924 * other cases will be caught as lock order violations. 925 */ 926 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 927 w1 = lock1->li_lock->lo_witness; 928 if (w1 == w) { 929 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) || 930 (flags & LOP_DUPOK)) 931 return; 932 w->w_same_squawked = 1; 933 printf("acquiring duplicate lock of same type: \"%s\"\n", 934 lock->lo_type); 935 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 936 lock1->li_file, lock1->li_line); 937 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 938#ifdef KDB 939 goto debugger; 940#else 941 return; 942#endif 943 } 944 MPASS(!mtx_owned(&w_mtx)); 945 mtx_lock_spin(&w_mtx); 946 /* 947 * If we know that the the lock we are acquiring comes after 948 * the lock we most recently acquired in the lock order tree, 949 * then there is no need for any further checks. 950 */ 951 if (isitmychild(w1, w)) { 952 mtx_unlock_spin(&w_mtx); 953 return; 954 } 955 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 956 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 957 958 MPASS(j < WITNESS_COUNT); 959 lock1 = &lle->ll_children[i]; 960 w1 = lock1->li_lock->lo_witness; 961 962 /* 963 * If this lock doesn't undergo witness checking, 964 * then skip it. 965 */ 966 if (w1 == NULL) { 967 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 968 ("lock missing witness structure")); 969 continue; 970 } 971 /* 972 * If we are locking Giant and this is a sleepable 973 * lock, then skip it. 974 */ 975 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 976 lock == &Giant.lock_object) 977 continue; 978 /* 979 * If we are locking a sleepable lock and this lock 980 * is Giant, then skip it. 981 */ 982 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 983 lock1->li_lock == &Giant.lock_object) 984 continue; 985 /* 986 * If we are locking a sleepable lock and this lock 987 * isn't sleepable, we want to treat it as a lock 988 * order violation to enfore a general lock order of 989 * sleepable locks before non-sleepable locks. 990 */ 991 if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 992 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 993 goto reversal; 994 /* 995 * If we are locking Giant and this is a non-sleepable 996 * lock, then treat it as a reversal. 997 */ 998 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 999 lock == &Giant.lock_object) 1000 goto reversal; 1001 /* 1002 * Check the lock order hierarchy for a reveresal. 1003 */ 1004 if (!isitmydescendant(w, w1)) 1005 continue; 1006 reversal: 1007 /* 1008 * We have a lock order violation, check to see if it 1009 * is allowed or has already been yelled about. 1010 */ 1011 mtx_unlock_spin(&w_mtx); 1012#ifdef BLESSING 1013 /* 1014 * If the lock order is blessed, just bail. We don't 1015 * look for other lock order violations though, which 1016 * may be a bug. 1017 */ 1018 if (blessed(w, w1)) 1019 return; 1020#endif 1021 if (lock1->li_lock == &Giant.lock_object) { 1022 if (w1->w_Giant_squawked) 1023 return; 1024 else 1025 w1->w_Giant_squawked = 1; 1026 } else { 1027 if (w1->w_other_squawked) 1028 return; 1029 else 1030 w1->w_other_squawked = 1; 1031 } 1032 /* 1033 * Ok, yell about it. 1034 */ 1035 if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1036 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1037 printf( 1038 "lock order reversal: (sleepable after non-sleepable)\n"); 1039 else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1040 && lock == &Giant.lock_object) 1041 printf( 1042 "lock order reversal: (Giant after non-sleepable)\n"); 1043 else 1044 printf("lock order reversal:\n"); 1045 /* 1046 * Try to locate an earlier lock with 1047 * witness w in our list. 1048 */ 1049 do { 1050 lock2 = &lle->ll_children[i]; 1051 MPASS(lock2->li_lock != NULL); 1052 if (lock2->li_lock->lo_witness == w) 1053 break; 1054 if (i == 0 && lle->ll_next != NULL) { 1055 lle = lle->ll_next; 1056 i = lle->ll_count - 1; 1057 MPASS(i >= 0 && i < LOCK_NCHILDREN); 1058 } else 1059 i--; 1060 } while (i >= 0); 1061 if (i < 0) { 1062 printf(" 1st %p %s (%s) @ %s:%d\n", 1063 lock1->li_lock, lock1->li_lock->lo_name, 1064 lock1->li_lock->lo_type, lock1->li_file, 1065 lock1->li_line); 1066 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 1067 lock->lo_name, lock->lo_type, file, line); 1068 } else { 1069 printf(" 1st %p %s (%s) @ %s:%d\n", 1070 lock2->li_lock, lock2->li_lock->lo_name, 1071 lock2->li_lock->lo_type, lock2->li_file, 1072 lock2->li_line); 1073 printf(" 2nd %p %s (%s) @ %s:%d\n", 1074 lock1->li_lock, lock1->li_lock->lo_name, 1075 lock1->li_lock->lo_type, lock1->li_file, 1076 lock1->li_line); 1077 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 1078 lock->lo_name, lock->lo_type, file, line); 1079 } 1080#ifdef KDB 1081 goto debugger; 1082#else 1083 return; 1084#endif 1085 } 1086 } 1087 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 1088 /* 1089 * If requested, build a new lock order. However, don't build a new 1090 * relationship between a sleepable lock and Giant if it is in the 1091 * wrong direction. The correct lock order is that sleepable locks 1092 * always come before Giant. 1093 */ 1094 if (flags & LOP_NEWORDER && 1095 !(lock1->li_lock == &Giant.lock_object && 1096 (lock->lo_flags & LO_SLEEPABLE) != 0)) { 1097 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1098 lock->lo_type, lock1->li_lock->lo_type); 1099 if (!itismychild(lock1->li_lock->lo_witness, w)) 1100 /* Witness is dead. */ 1101 return; 1102 } 1103 mtx_unlock_spin(&w_mtx); 1104 return; 1105 1106#ifdef KDB 1107debugger: 1108 if (witness_trace) 1109 kdb_backtrace(); 1110 if (witness_kdb) 1111 kdb_enter(__func__); 1112#endif 1113} 1114 1115void 1116witness_lock(struct lock_object *lock, int flags, const char *file, int line) 1117{ 1118 struct lock_list_entry **lock_list, *lle; 1119 struct lock_instance *instance; 1120 struct witness *w; 1121 struct thread *td; 1122 1123 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1124 panicstr != NULL) 1125 return; 1126 w = lock->lo_witness; 1127 td = curthread; 1128 file = fixup_filename(file); 1129 1130 /* Determine lock list for this lock. */ 1131 if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1132 lock_list = &td->td_sleeplocks; 1133 else 1134 lock_list = PCPU_PTR(spinlocks); 1135 1136 /* Check to see if we are recursing on a lock we already own. */ 1137 instance = find_instance(*lock_list, lock); 1138 if (instance != NULL) { 1139 instance->li_flags++; 1140 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1141 td->td_proc->p_pid, lock->lo_name, 1142 instance->li_flags & LI_RECURSEMASK); 1143 instance->li_file = file; 1144 instance->li_line = line; 1145 return; 1146 } 1147 1148 /* Update per-witness last file and line acquire. */ 1149 w->w_file = file; 1150 w->w_line = line; 1151 1152 /* Find the next open lock instance in the list and fill it. */ 1153 lle = *lock_list; 1154 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 1155 lle = witness_lock_list_get(); 1156 if (lle == NULL) 1157 return; 1158 lle->ll_next = *lock_list; 1159 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 1160 td->td_proc->p_pid, lle); 1161 *lock_list = lle; 1162 } 1163 instance = &lle->ll_children[lle->ll_count++]; 1164 instance->li_lock = lock; 1165 instance->li_line = line; 1166 instance->li_file = file; 1167 if ((flags & LOP_EXCLUSIVE) != 0) 1168 instance->li_flags = LI_EXCLUSIVE; 1169 else 1170 instance->li_flags = 0; 1171 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 1172 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 1173} 1174 1175void 1176witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 1177{ 1178 struct lock_instance *instance; 1179 struct lock_class *class; 1180 1181 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1182 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1183 return; 1184 class = LOCK_CLASS(lock); 1185 file = fixup_filename(file); 1186 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1187 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 1188 class->lc_name, lock->lo_name, file, line); 1189 if ((flags & LOP_TRYLOCK) == 0) 1190 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 1191 lock->lo_name, file, line); 1192 if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1193 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 1194 class->lc_name, lock->lo_name, file, line); 1195 instance = find_instance(curthread->td_sleeplocks, lock); 1196 if (instance == NULL) 1197 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 1198 class->lc_name, lock->lo_name, file, line); 1199 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1200 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 1201 class->lc_name, lock->lo_name, file, line); 1202 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1203 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1204 class->lc_name, lock->lo_name, 1205 instance->li_flags & LI_RECURSEMASK, file, line); 1206 instance->li_flags |= LI_EXCLUSIVE; 1207} 1208 1209void 1210witness_downgrade(struct lock_object *lock, int flags, const char *file, 1211 int line) 1212{ 1213 struct lock_instance *instance; 1214 struct lock_class *class; 1215 1216 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1217 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1218 return; 1219 class = LOCK_CLASS(lock); 1220 file = fixup_filename(file); 1221 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1222 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 1223 class->lc_name, lock->lo_name, file, line); 1224 if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1225 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 1226 class->lc_name, lock->lo_name, file, line); 1227 instance = find_instance(curthread->td_sleeplocks, lock); 1228 if (instance == NULL) 1229 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 1230 class->lc_name, lock->lo_name, file, line); 1231 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1232 panic("downgrade of shared lock (%s) %s @ %s:%d", 1233 class->lc_name, lock->lo_name, file, line); 1234 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1235 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1236 class->lc_name, lock->lo_name, 1237 instance->li_flags & LI_RECURSEMASK, file, line); 1238 instance->li_flags &= ~LI_EXCLUSIVE; 1239} 1240 1241void 1242witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 1243{ 1244 struct lock_list_entry **lock_list, *lle; 1245 struct lock_instance *instance; 1246 struct lock_class *class; 1247 struct thread *td; 1248 register_t s; 1249 int i, j; 1250 1251 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1252 panicstr != NULL) 1253 return; 1254 td = curthread; 1255 class = LOCK_CLASS(lock); 1256 file = fixup_filename(file); 1257 1258 /* Find lock instance associated with this lock. */ 1259 if (class->lc_flags & LC_SLEEPLOCK) 1260 lock_list = &td->td_sleeplocks; 1261 else 1262 lock_list = PCPU_PTR(spinlocks); 1263 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 1264 for (i = 0; i < (*lock_list)->ll_count; i++) { 1265 instance = &(*lock_list)->ll_children[i]; 1266 if (instance->li_lock == lock) 1267 goto found; 1268 } 1269 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 1270 file, line); 1271found: 1272 1273 /* First, check for shared/exclusive mismatches. */ 1274 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 1275 (flags & LOP_EXCLUSIVE) == 0) { 1276 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1277 lock->lo_name, file, line); 1278 printf("while exclusively locked from %s:%d\n", 1279 instance->li_file, instance->li_line); 1280 panic("excl->ushare"); 1281 } 1282 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 1283 (flags & LOP_EXCLUSIVE) != 0) { 1284 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1285 lock->lo_name, file, line); 1286 printf("while share locked from %s:%d\n", instance->li_file, 1287 instance->li_line); 1288 panic("share->uexcl"); 1289 } 1290 1291 /* If we are recursed, unrecurse. */ 1292 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1293 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1294 td->td_proc->p_pid, instance->li_lock->lo_name, 1295 instance->li_flags); 1296 instance->li_flags--; 1297 return; 1298 } 1299 1300 /* Otherwise, remove this item from the list. */ 1301 s = intr_disable(); 1302 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1303 td->td_proc->p_pid, instance->li_lock->lo_name, 1304 (*lock_list)->ll_count - 1); 1305 for (j = i; j < (*lock_list)->ll_count - 1; j++) 1306 (*lock_list)->ll_children[j] = 1307 (*lock_list)->ll_children[j + 1]; 1308 (*lock_list)->ll_count--; 1309 intr_restore(s); 1310 1311 /* If this lock list entry is now empty, free it. */ 1312 if ((*lock_list)->ll_count == 0) { 1313 lle = *lock_list; 1314 *lock_list = lle->ll_next; 1315 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1316 td->td_proc->p_pid, lle); 1317 witness_lock_list_free(lle); 1318 } 1319} 1320 1321/* 1322 * Warn if any locks other than 'lock' are held. Flags can be passed in to 1323 * exempt Giant and sleepable locks from the checks as well. If any 1324 * non-exempt locks are held, then a supplied message is printed to the 1325 * console along with a list of the offending locks. If indicated in the 1326 * flags then a failure results in a panic as well. 1327 */ 1328int 1329witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 1330{ 1331 struct lock_list_entry *lle; 1332 struct lock_instance *lock1; 1333 struct thread *td; 1334 va_list ap; 1335 int i, n; 1336 1337 if (witness_cold || witness_watch == 0 || panicstr != NULL) 1338 return (0); 1339 n = 0; 1340 td = curthread; 1341 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 1342 for (i = lle->ll_count - 1; i >= 0; i--) { 1343 lock1 = &lle->ll_children[i]; 1344 if (lock1->li_lock == lock) 1345 continue; 1346 if (flags & WARN_GIANTOK && 1347 lock1->li_lock == &Giant.lock_object) 1348 continue; 1349 if (flags & WARN_SLEEPOK && 1350 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 1351 continue; 1352 if (n == 0) { 1353 va_start(ap, fmt); 1354 vprintf(fmt, ap); 1355 va_end(ap); 1356 printf(" with the following"); 1357 if (flags & WARN_SLEEPOK) 1358 printf(" non-sleepable"); 1359 printf(" locks held:\n"); 1360 } 1361 n++; 1362 witness_list_lock(lock1); 1363 } 1364 if (PCPU_GET(spinlocks) != NULL) { 1365 /* 1366 * Since we already hold a spinlock preemption is 1367 * already blocked. 1368 */ 1369 if (n == 0) { 1370 va_start(ap, fmt); 1371 vprintf(fmt, ap); 1372 va_end(ap); 1373 printf(" with the following"); 1374 if (flags & WARN_SLEEPOK) 1375 printf(" non-sleepable"); 1376 printf(" locks held:\n"); 1377 } 1378 n += witness_list_locks(PCPU_PTR(spinlocks)); 1379 } 1380 if (flags & WARN_PANIC && n) 1381 panic("witness_warn"); 1382#ifdef KDB 1383 else if (witness_kdb && n) 1384 kdb_enter(__func__); 1385 else if (witness_trace && n) 1386 kdb_backtrace(); 1387#endif 1388 return (n); 1389} 1390 1391const char * 1392witness_file(struct lock_object *lock) 1393{ 1394 struct witness *w; 1395 1396 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1397 return ("?"); 1398 w = lock->lo_witness; 1399 return (w->w_file); 1400} 1401 1402int 1403witness_line(struct lock_object *lock) 1404{ 1405 struct witness *w; 1406 1407 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1408 return (0); 1409 w = lock->lo_witness; 1410 return (w->w_line); 1411} 1412 1413static struct witness * 1414enroll(const char *description, struct lock_class *lock_class) 1415{ 1416 struct witness *w; 1417 1418 if (witness_watch == 0 || panicstr != NULL) 1419 return (NULL); 1420 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 1421 return (NULL); 1422 mtx_lock_spin(&w_mtx); 1423 STAILQ_FOREACH(w, &w_all, w_list) { 1424 if (w->w_name == description || (w->w_refcount > 0 && 1425 strcmp(description, w->w_name) == 0)) { 1426 w->w_refcount++; 1427 mtx_unlock_spin(&w_mtx); 1428 if (lock_class != w->w_class) 1429 panic( 1430 "lock (%s) %s does not match earlier (%s) lock", 1431 description, lock_class->lc_name, 1432 w->w_class->lc_name); 1433 return (w); 1434 } 1435 } 1436 if ((w = witness_get()) == NULL) 1437 goto out; 1438 w->w_name = description; 1439 w->w_class = lock_class; 1440 w->w_refcount = 1; 1441 STAILQ_INSERT_HEAD(&w_all, w, w_list); 1442 if (lock_class->lc_flags & LC_SPINLOCK) { 1443 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1444 w_spin_cnt++; 1445 } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 1446 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1447 w_sleep_cnt++; 1448 } else { 1449 mtx_unlock_spin(&w_mtx); 1450 panic("lock class %s is not sleep or spin", 1451 lock_class->lc_name); 1452 } 1453 mtx_unlock_spin(&w_mtx); 1454out: 1455 /* 1456 * We issue a warning for any spin locks not defined in the static 1457 * order list as a way to discourage their use (folks should really 1458 * be using non-spin mutexes most of the time). However, several 1459 * 3rd part device drivers use spin locks because that is all they 1460 * have available on Windows and Linux and they think that normal 1461 * mutexes are insufficient. 1462 */ 1463 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_spin_warn) 1464 printf("WITNESS: spin lock %s not in order list\n", 1465 description); 1466 return (w); 1467} 1468 1469/* Don't let the door bang you on the way out... */ 1470static int 1471depart(struct witness *w) 1472{ 1473 struct witness_child_list_entry *wcl, *nwcl; 1474 struct witness_list *list; 1475 struct witness *parent; 1476 1477 MPASS(w->w_refcount == 0); 1478 if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1479 list = &w_sleep; 1480 w_sleep_cnt--; 1481 } else { 1482 list = &w_spin; 1483 w_spin_cnt--; 1484 } 1485 /* 1486 * First, we run through the entire tree looking for any 1487 * witnesses that the outgoing witness is a child of. For 1488 * each parent that we find, we reparent all the direct 1489 * children of the outgoing witness to its parent. 1490 */ 1491 STAILQ_FOREACH(parent, list, w_typelist) { 1492 if (!isitmychild(parent, w)) 1493 continue; 1494 removechild(parent, w); 1495 } 1496 1497 /* 1498 * Now we go through and free up the child list of the 1499 * outgoing witness. 1500 */ 1501 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1502 nwcl = wcl->wcl_next; 1503 w_child_cnt--; 1504 witness_child_free(wcl); 1505 } 1506 1507 /* 1508 * Detach from various lists and free. 1509 */ 1510 STAILQ_REMOVE(list, w, witness, w_typelist); 1511 STAILQ_REMOVE(&w_all, w, witness, w_list); 1512 witness_free(w); 1513 1514 return (1); 1515} 1516 1517/* 1518 * Add "child" as a direct child of "parent". Returns false if 1519 * we fail due to out of memory. 1520 */ 1521static int 1522insertchild(struct witness *parent, struct witness *child) 1523{ 1524 struct witness_child_list_entry **wcl; 1525 1526 MPASS(child != NULL && parent != NULL); 1527 1528 /* 1529 * Insert "child" after "parent" 1530 */ 1531 wcl = &parent->w_children; 1532 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 1533 wcl = &(*wcl)->wcl_next; 1534 if (*wcl == NULL) { 1535 *wcl = witness_child_get(); 1536 if (*wcl == NULL) 1537 return (0); 1538 w_child_cnt++; 1539 } 1540 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1541 1542 return (1); 1543} 1544 1545 1546static int 1547itismychild(struct witness *parent, struct witness *child) 1548{ 1549 struct witness_list *list; 1550 1551 MPASS(child != NULL && parent != NULL); 1552 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1553 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1554 panic( 1555 "%s: parent (%s) and child (%s) are not the same lock type", 1556 __func__, parent->w_class->lc_name, 1557 child->w_class->lc_name); 1558 1559 if (!insertchild(parent, child)) 1560 return (0); 1561 1562 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1563 list = &w_sleep; 1564 else 1565 list = &w_spin; 1566 return (1); 1567} 1568 1569static void 1570removechild(struct witness *parent, struct witness *child) 1571{ 1572 struct witness_child_list_entry **wcl, *wcl1; 1573 int i; 1574 1575 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1576 for (i = 0; i < (*wcl)->wcl_count; i++) 1577 if ((*wcl)->wcl_children[i] == child) 1578 goto found; 1579 return; 1580found: 1581 (*wcl)->wcl_count--; 1582 if ((*wcl)->wcl_count > i) 1583 (*wcl)->wcl_children[i] = 1584 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1585 MPASS((*wcl)->wcl_children[i] != NULL); 1586 if ((*wcl)->wcl_count != 0) 1587 return; 1588 wcl1 = *wcl; 1589 *wcl = wcl1->wcl_next; 1590 w_child_cnt--; 1591 witness_child_free(wcl1); 1592} 1593 1594static int 1595isitmychild(struct witness *parent, struct witness *child) 1596{ 1597 struct witness_child_list_entry *wcl; 1598 int i; 1599 1600 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1601 for (i = 0; i < wcl->wcl_count; i++) { 1602 if (wcl->wcl_children[i] == child) 1603 return (1); 1604 } 1605 } 1606 return (0); 1607} 1608 1609static int 1610isitmydescendant(struct witness *parent, struct witness *child) 1611{ 1612 struct witness_child_list_entry *wcl; 1613 int i, j; 1614 1615 if (isitmychild(parent, child)) 1616 return (1); 1617 j = 0; 1618 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1619 MPASS(j < 1000); 1620 for (i = 0; i < wcl->wcl_count; i++) { 1621 if (isitmydescendant(wcl->wcl_children[i], child)) 1622 return (1); 1623 } 1624 j++; 1625 } 1626 return (0); 1627} 1628 1629#ifdef BLESSING 1630static int 1631blessed(struct witness *w1, struct witness *w2) 1632{ 1633 int i; 1634 struct witness_blessed *b; 1635 1636 for (i = 0; i < blessed_count; i++) { 1637 b = &blessed_list[i]; 1638 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1639 if (strcmp(w2->w_name, b->b_lock2) == 0) 1640 return (1); 1641 continue; 1642 } 1643 if (strcmp(w1->w_name, b->b_lock2) == 0) 1644 if (strcmp(w2->w_name, b->b_lock1) == 0) 1645 return (1); 1646 } 1647 return (0); 1648} 1649#endif 1650 1651static struct witness * 1652witness_get(void) 1653{ 1654 struct witness *w; 1655 1656 if (witness_watch == 0) { 1657 mtx_unlock_spin(&w_mtx); 1658 return (NULL); 1659 } 1660 if (STAILQ_EMPTY(&w_free)) { 1661 witness_watch = 0; 1662 mtx_unlock_spin(&w_mtx); 1663 printf("%s: witness exhausted\n", __func__); 1664 return (NULL); 1665 } 1666 w = STAILQ_FIRST(&w_free); 1667 STAILQ_REMOVE_HEAD(&w_free, w_list); 1668 w_free_cnt--; 1669 bzero(w, sizeof(*w)); 1670 return (w); 1671} 1672 1673static void 1674witness_free(struct witness *w) 1675{ 1676 1677 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1678 w_free_cnt++; 1679} 1680 1681static struct witness_child_list_entry * 1682witness_child_get(void) 1683{ 1684 struct witness_child_list_entry *wcl; 1685 1686 if (witness_watch == 0) { 1687 mtx_unlock_spin(&w_mtx); 1688 return (NULL); 1689 } 1690 wcl = w_child_free; 1691 if (wcl == NULL) { 1692 witness_watch = 0; 1693 mtx_unlock_spin(&w_mtx); 1694 printf("%s: witness exhausted\n", __func__); 1695 return (NULL); 1696 } 1697 w_child_free = wcl->wcl_next; 1698 w_child_free_cnt--; 1699 bzero(wcl, sizeof(*wcl)); 1700 return (wcl); 1701} 1702 1703static void 1704witness_child_free(struct witness_child_list_entry *wcl) 1705{ 1706 1707 wcl->wcl_next = w_child_free; 1708 w_child_free = wcl; 1709 w_child_free_cnt++; 1710} 1711 1712static struct lock_list_entry * 1713witness_lock_list_get(void) 1714{ 1715 struct lock_list_entry *lle; 1716 1717 if (witness_watch == 0) 1718 return (NULL); 1719 mtx_lock_spin(&w_mtx); 1720 lle = w_lock_list_free; 1721 if (lle == NULL) { 1722 witness_watch = 0; 1723 mtx_unlock_spin(&w_mtx); 1724 printf("%s: witness exhausted\n", __func__); 1725 return (NULL); 1726 } 1727 w_lock_list_free = lle->ll_next; 1728 mtx_unlock_spin(&w_mtx); 1729 bzero(lle, sizeof(*lle)); 1730 return (lle); 1731} 1732 1733static void 1734witness_lock_list_free(struct lock_list_entry *lle) 1735{ 1736 1737 mtx_lock_spin(&w_mtx); 1738 lle->ll_next = w_lock_list_free; 1739 w_lock_list_free = lle; 1740 mtx_unlock_spin(&w_mtx); 1741} 1742 1743static struct lock_instance * 1744find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1745{ 1746 struct lock_list_entry *lle; 1747 struct lock_instance *instance; 1748 int i; 1749 1750 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1751 for (i = lle->ll_count - 1; i >= 0; i--) { 1752 instance = &lle->ll_children[i]; 1753 if (instance->li_lock == lock) 1754 return (instance); 1755 } 1756 return (NULL); 1757} 1758 1759static void 1760witness_list_lock(struct lock_instance *instance) 1761{ 1762 struct lock_object *lock; 1763 1764 lock = instance->li_lock; 1765 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1766 "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 1767 if (lock->lo_type != lock->lo_name) 1768 printf(" (%s)", lock->lo_type); 1769 printf(" r = %d (%p) locked @ %s:%d\n", 1770 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1771 instance->li_line); 1772} 1773 1774#ifdef DDB 1775static int 1776witness_thread_has_locks(struct thread *td) 1777{ 1778 1779 return (td->td_sleeplocks != NULL); 1780} 1781 1782static int 1783witness_proc_has_locks(struct proc *p) 1784{ 1785 struct thread *td; 1786 1787 FOREACH_THREAD_IN_PROC(p, td) { 1788 if (witness_thread_has_locks(td)) 1789 return (1); 1790 } 1791 return (0); 1792} 1793#endif 1794 1795int 1796witness_list_locks(struct lock_list_entry **lock_list) 1797{ 1798 struct lock_list_entry *lle; 1799 int i, nheld; 1800 1801 nheld = 0; 1802 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1803 for (i = lle->ll_count - 1; i >= 0; i--) { 1804 witness_list_lock(&lle->ll_children[i]); 1805 nheld++; 1806 } 1807 return (nheld); 1808} 1809 1810/* 1811 * This is a bit risky at best. We call this function when we have timed 1812 * out acquiring a spin lock, and we assume that the other CPU is stuck 1813 * with this lock held. So, we go groveling around in the other CPU's 1814 * per-cpu data to try to find the lock instance for this spin lock to 1815 * see when it was last acquired. 1816 */ 1817void 1818witness_display_spinlock(struct lock_object *lock, struct thread *owner) 1819{ 1820 struct lock_instance *instance; 1821 struct pcpu *pc; 1822 1823 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 1824 return; 1825 pc = pcpu_find(owner->td_oncpu); 1826 instance = find_instance(pc->pc_spinlocks, lock); 1827 if (instance != NULL) 1828 witness_list_lock(instance); 1829} 1830 1831void 1832witness_save(struct lock_object *lock, const char **filep, int *linep) 1833{ 1834 struct lock_list_entry *lock_list; 1835 struct lock_instance *instance; 1836 struct lock_class *class; 1837 1838 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1839 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1840 return; 1841 class = LOCK_CLASS(lock); 1842 if (class->lc_flags & LC_SLEEPLOCK) 1843 lock_list = curthread->td_sleeplocks; 1844 else { 1845 if (witness_skipspin) 1846 return; 1847 lock_list = PCPU_GET(spinlocks); 1848 } 1849 instance = find_instance(lock_list, lock); 1850 if (instance == NULL) 1851 panic("%s: lock (%s) %s not locked", __func__, 1852 class->lc_name, lock->lo_name); 1853 *filep = instance->li_file; 1854 *linep = instance->li_line; 1855} 1856 1857void 1858witness_restore(struct lock_object *lock, const char *file, int line) 1859{ 1860 struct lock_list_entry *lock_list; 1861 struct lock_instance *instance; 1862 struct lock_class *class; 1863 1864 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1865 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1866 return; 1867 class = LOCK_CLASS(lock); 1868 if (class->lc_flags & LC_SLEEPLOCK) 1869 lock_list = curthread->td_sleeplocks; 1870 else { 1871 if (witness_skipspin) 1872 return; 1873 lock_list = PCPU_GET(spinlocks); 1874 } 1875 instance = find_instance(lock_list, lock); 1876 if (instance == NULL) 1877 panic("%s: lock (%s) %s not locked", __func__, 1878 class->lc_name, lock->lo_name); 1879 lock->lo_witness->w_file = file; 1880 lock->lo_witness->w_line = line; 1881 instance->li_file = file; 1882 instance->li_line = line; 1883} 1884 1885void 1886witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1887{ 1888#ifdef INVARIANT_SUPPORT 1889 struct lock_instance *instance; 1890 struct lock_class *class; 1891 1892 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1893 return; 1894 class = LOCK_CLASS(lock); 1895 if ((class->lc_flags & LC_SLEEPLOCK) != 0) 1896 instance = find_instance(curthread->td_sleeplocks, lock); 1897 else if ((class->lc_flags & LC_SPINLOCK) != 0) 1898 instance = find_instance(PCPU_GET(spinlocks), lock); 1899 else { 1900 panic("Lock (%s) %s is not sleep or spin!", 1901 class->lc_name, lock->lo_name); 1902 } 1903 file = fixup_filename(file); 1904 switch (flags) { 1905 case LA_UNLOCKED: 1906 if (instance != NULL) 1907 panic("Lock (%s) %s locked @ %s:%d.", 1908 class->lc_name, lock->lo_name, file, line); 1909 break; 1910 case LA_LOCKED: 1911 case LA_LOCKED | LA_RECURSED: 1912 case LA_LOCKED | LA_NOTRECURSED: 1913 case LA_SLOCKED: 1914 case LA_SLOCKED | LA_RECURSED: 1915 case LA_SLOCKED | LA_NOTRECURSED: 1916 case LA_XLOCKED: 1917 case LA_XLOCKED | LA_RECURSED: 1918 case LA_XLOCKED | LA_NOTRECURSED: 1919 if (instance == NULL) { 1920 panic("Lock (%s) %s not locked @ %s:%d.", 1921 class->lc_name, lock->lo_name, file, line); 1922 break; 1923 } 1924 if ((flags & LA_XLOCKED) != 0 && 1925 (instance->li_flags & LI_EXCLUSIVE) == 0) 1926 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1927 class->lc_name, lock->lo_name, file, line); 1928 if ((flags & LA_SLOCKED) != 0 && 1929 (instance->li_flags & LI_EXCLUSIVE) != 0) 1930 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1931 class->lc_name, lock->lo_name, file, line); 1932 if ((flags & LA_RECURSED) != 0 && 1933 (instance->li_flags & LI_RECURSEMASK) == 0) 1934 panic("Lock (%s) %s not recursed @ %s:%d.", 1935 class->lc_name, lock->lo_name, file, line); 1936 if ((flags & LA_NOTRECURSED) != 0 && 1937 (instance->li_flags & LI_RECURSEMASK) != 0) 1938 panic("Lock (%s) %s recursed @ %s:%d.", 1939 class->lc_name, lock->lo_name, file, line); 1940 break; 1941 default: 1942 panic("Invalid lock assertion at %s:%d.", file, line); 1943 1944 } 1945#endif /* INVARIANT_SUPPORT */ 1946} 1947 1948#ifdef DDB 1949static void 1950witness_list(struct thread *td) 1951{ 1952 1953 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1954 KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 1955 1956 if (witness_watch == 0) 1957 return; 1958 1959 witness_list_locks(&td->td_sleeplocks); 1960 1961 /* 1962 * We only handle spinlocks if td == curthread. This is somewhat broken 1963 * if td is currently executing on some other CPU and holds spin locks 1964 * as we won't display those locks. If we had a MI way of getting 1965 * the per-cpu data for a given cpu then we could use 1966 * td->td_oncpu to get the list of spinlocks for this thread 1967 * and "fix" this. 1968 * 1969 * That still wouldn't really fix this unless we locked the scheduler 1970 * lock or stopped the other CPU to make sure it wasn't changing the 1971 * list out from under us. It is probably best to just not try to 1972 * handle threads on other CPU's for now. 1973 */ 1974 if (td == curthread && PCPU_GET(spinlocks) != NULL) 1975 witness_list_locks(PCPU_PTR(spinlocks)); 1976} 1977 1978DB_SHOW_COMMAND(locks, db_witness_list) 1979{ 1980 struct thread *td; 1981 1982 if (have_addr) 1983 td = db_lookup_thread(addr, TRUE); 1984 else 1985 td = kdb_thread; 1986 witness_list(td); 1987} 1988 1989DB_SHOW_COMMAND(alllocks, db_witness_list_all) 1990{ 1991 struct thread *td; 1992 struct proc *p; 1993 1994 /* 1995 * It would be nice to list only threads and processes that actually 1996 * held sleep locks, but that information is currently not exported 1997 * by WITNESS. 1998 */ 1999 FOREACH_PROC_IN_SYSTEM(p) { 2000 if (!witness_proc_has_locks(p)) 2001 continue; 2002 FOREACH_THREAD_IN_PROC(p, td) { 2003 if (!witness_thread_has_locks(td)) 2004 continue; 2005 db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2006 p->p_comm, td, td->td_tid); 2007 witness_list(td); 2008 } 2009 } 2010} 2011 2012DB_SHOW_COMMAND(witness, db_witness_display) 2013{ 2014 2015 witness_display(db_printf); 2016} 2017#endif 2018