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