subr_witness.c revision 112123
1178825Sdfr/*- 2233294Sstas * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 3233294Sstas * 4233294Sstas * Redistribution and use in source and binary forms, with or without 5178825Sdfr * modification, are permitted provided that the following conditions 6233294Sstas * are met: 7233294Sstas * 1. Redistributions of source code must retain the above copyright 8233294Sstas * notice, this list of conditions and the following disclaimer. 9178825Sdfr * 2. Redistributions in binary form must reproduce the above copyright 10233294Sstas * notice, this list of conditions and the following disclaimer in the 11233294Sstas * documentation and/or other materials provided with the distribution. 12178825Sdfr * 3. Berkeley Software Design Inc's name may not be used to endorse or 13233294Sstas * promote products derived from this software without specific prior 14233294Sstas * written permission. 15233294Sstas * 16178825Sdfr * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 17233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 20178825Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26233294Sstas * SUCH DAMAGE. 27233294Sstas * 28233294Sstas * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 29233294Sstas * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 30233294Sstas * $FreeBSD: head/sys/kern/subr_witness.c 112123 2003-03-11 22:33:42Z jhb $ 31233294Sstas */ 32178825Sdfr 33178825Sdfr/* 34233294Sstas * Implementation of the `witness' lock verifier. Originally implemented for 35178825Sdfr * mutexes in BSD/OS. Extended to handle generic lock objects and lock 36178825Sdfr * classes in FreeBSD. 37178825Sdfr */ 38178825Sdfr 39178825Sdfr/* 40178825Sdfr * Main Entry: witness 41178825Sdfr * Pronunciation: 'wit-n&s 42178825Sdfr * Function: noun 43233294Sstas * Etymology: Middle English witnesse, from Old English witnes knowledge, 44178825Sdfr * testimony, witness, from 2wit 45178825Sdfr * Date: before 12th century 46178825Sdfr * 1 : attestation of a fact or event : TESTIMONY 47178825Sdfr * 2 : one that gives evidence; specifically : one who testifies in 48178825Sdfr * a cause or before a judicial tribunal 49233294Sstas * 3 : one asked to be present at a transaction so as to be able to 50178825Sdfr * testify to its having taken place 51178825Sdfr * 4 : one who has personal knowledge of something 52178825Sdfr * 5 a : something serving as evidence or proof : SIGN 53178825Sdfr * b : public affirmation by word or example of usually 54178825Sdfr * religious faith or conviction <the heroic witness to divine 55178825Sdfr * life -- Pilot> 56178825Sdfr * 6 capitalized : a member of the Jehovah's Witnesses 57233294Sstas */ 58178825Sdfr 59178825Sdfr/* 60178825Sdfr * Special rules concerning Giant and lock orders: 61178825Sdfr * 62178825Sdfr * 1) Giant must be acquired before any other mutexes. Stated another way, 63178825Sdfr * no other mutex may be held when Giant is acquired. 64233294Sstas * 65178825Sdfr * 2) Giant must be released when blocking on a sleepable lock. 66178825Sdfr * 67233294Sstas * This rule is less obvious, but is a result of Giant providing the same 68233294Sstas * semantics as spl(). Basically, when a thread sleeps, it must release 69233294Sstas * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule 70178825Sdfr * 2). 71178825Sdfr * 72233294Sstas * 3) Giant may be acquired before or after sleepable locks. 73233294Sstas * 74178825Sdfr * This rule is also not quite as obvious. Giant may be acquired after 75178825Sdfr * a sleepable lock because it is a non-sleepable lock and non-sleepable 76178825Sdfr * locks may always be acquired while holding a sleepable lock. The second 77178825Sdfr * case, Giant before a sleepable lock, follows from rule 2) above. Suppose 78178825Sdfr * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1 79178825Sdfr * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and 80178825Sdfr * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to 81178825Sdfr * execute. Thus, acquiring Giant both before and after a sleepable lock 82178825Sdfr * will not result in a lock order reversal. 83178825Sdfr */ 84178825Sdfr 85178825Sdfr#include "opt_ddb.h" 86178825Sdfr#include "opt_witness.h" 87178825Sdfr 88178825Sdfr#include <sys/param.h> 89233294Sstas#include <sys/bus.h> 90233294Sstas#include <sys/kernel.h> 91233294Sstas#include <sys/ktr.h> 92233294Sstas#include <sys/lock.h> 93233294Sstas#include <sys/malloc.h> 94233294Sstas#include <sys/mutex.h> 95233294Sstas#include <sys/proc.h> 96178825Sdfr#include <sys/sysctl.h> 97178825Sdfr#include <sys/systm.h> 98178825Sdfr 99178825Sdfr#include <ddb/ddb.h> 100178825Sdfr 101178825Sdfr#include <machine/stdarg.h> 102178825Sdfr 103178825Sdfr/* Define this to check for blessed mutexes */ 104178825Sdfr#undef BLESSING 105178825Sdfr 106178825Sdfr#define WITNESS_COUNT 200 107233294Sstas#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 108233294Sstas/* 109178825Sdfr * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 110178825Sdfr * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 111233294Sstas * probably be safe for the most part, but it's still a SWAG. 112178825Sdfr */ 113233294Sstas#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 114178825Sdfr 115178825Sdfr#define WITNESS_NCHILDREN 6 116178825Sdfr 117178825Sdfrstruct witness_child_list_entry; 118178825Sdfr 119178825Sdfrstruct witness { 120178825Sdfr const char *w_name; 121178825Sdfr struct lock_class *w_class; 122178825Sdfr STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 123178825Sdfr STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 124178825Sdfr struct witness_child_list_entry *w_children; /* Great evilness... */ 125178825Sdfr const char *w_file; 126178825Sdfr int w_line; 127178825Sdfr u_int w_level; 128178825Sdfr u_int w_refcount; 129178825Sdfr u_char w_Giant_squawked:1; 130233294Sstas u_char w_other_squawked:1; 131233294Sstas u_char w_same_squawked:1; 132233294Sstas u_char w_displayed:1; 133233294Sstas}; 134233294Sstas 135233294Sstasstruct witness_child_list_entry { 136233294Sstas struct witness_child_list_entry *wcl_next; 137233294Sstas struct witness *wcl_children[WITNESS_NCHILDREN]; 138233294Sstas u_int wcl_count; 139233294Sstas}; 140233294Sstas 141233294SstasSTAILQ_HEAD(witness_list, witness); 142233294Sstas 143178825Sdfr#ifdef BLESSING 144178825Sdfrstruct witness_blessed { 145178825Sdfr const char *b_lock1; 146233294Sstas const char *b_lock2; 147233294Sstas}; 148233294Sstas#endif 149233294Sstas 150233294Sstasstruct witness_order_list_entry { 151233294Sstas const char *w_name; 152178825Sdfr struct lock_class *w_class; 153178825Sdfr}; 154178825Sdfr 155178825Sdfr#ifdef BLESSING 156178825Sdfrstatic int blessed(struct witness *, struct witness *); 157233294Sstas#endif 158178825Sdfrstatic int depart(struct witness *w); 159178825Sdfrstatic struct witness *enroll(const char *description, 160233294Sstas struct lock_class *lock_class); 161233294Sstasstatic int insertchild(struct witness *parent, struct witness *child); 162178825Sdfrstatic int isitmychild(struct witness *parent, struct witness *child); 163178825Sdfrstatic int isitmydescendant(struct witness *parent, struct witness *child); 164233294Sstasstatic int itismychild(struct witness *parent, struct witness *child); 165178825Sdfrstatic int rebalancetree(struct witness_list *list); 166178825Sdfrstatic void removechild(struct witness *parent, struct witness *child); 167178825Sdfrstatic int reparentchildren(struct witness *newparent, 168233294Sstas struct witness *oldparent); 169233294Sstasstatic void witness_displaydescendants(void(*)(const char *fmt, ...), 170178825Sdfr struct witness *, int indent); 171178825Sdfrstatic const char *fixup_filename(const char *file); 172233294Sstasstatic void witness_leveldescendents(struct witness *parent, int level); 173233294Sstasstatic void witness_levelall(void); 174178825Sdfrstatic struct witness *witness_get(void); 175178825Sdfrstatic void witness_free(struct witness *m); 176178825Sdfrstatic struct witness_child_list_entry *witness_child_get(void); 177178825Sdfrstatic void witness_child_free(struct witness_child_list_entry *wcl); 178178825Sdfrstatic struct lock_list_entry *witness_lock_list_get(void); 179178825Sdfrstatic void witness_lock_list_free(struct lock_list_entry *lle); 180233294Sstasstatic struct lock_instance *find_instance(struct lock_list_entry *lock_list, 181178825Sdfr struct lock_object *lock); 182178825Sdfrstatic void witness_list_lock(struct lock_instance *instance); 183178825Sdfr#ifdef DDB 184178825Sdfrstatic void witness_list(struct thread *td); 185178825Sdfrstatic void witness_display_list(void(*prnt)(const char *fmt, ...), 186178825Sdfr struct witness_list *list); 187233294Sstasstatic void witness_display(void(*)(const char *fmt, ...)); 188233294Sstas#endif 189233294Sstas 190233294SstasMALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 191233294Sstas 192233294Sstasstatic int witness_watch = 1; 193233294SstasTUNABLE_INT("debug.witness_watch", &witness_watch); 194233294SstasSYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 195233294Sstas 196233294Sstas#ifdef DDB 197233294Sstas/* 198233294Sstas * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 199233294Sstas * drop into kdebug() when: 200233294Sstas * - a lock heirarchy violation occurs 201233294Sstas * - locks are held when going to sleep. 202233294Sstas */ 203233294Sstas#ifdef WITNESS_DDB 204233294Sstasint witness_ddb = 1; 205233294Sstas#else 206233294Sstasint witness_ddb = 0; 207178825Sdfr#endif 208178825SdfrTUNABLE_INT("debug.witness_ddb", &witness_ddb); 209178825SdfrSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 210233294Sstas 211233294Sstas/* 212178825Sdfr * When DDB is enabled and witness_trace is set to 1, it will cause the system 213178825Sdfr * to print a stack trace: 214178825Sdfr * - a lock heirarchy violation occurs 215233294Sstas * - locks are held when going to sleep. 216233294Sstas */ 217178825Sdfrint witness_trace = 1; 218233294SstasTUNABLE_INT("debug.witness_trace", &witness_trace); 219178825SdfrSYSCTL_INT(_debug, OID_AUTO, witness_trace, CTLFLAG_RW, &witness_trace, 0, ""); 220233294Sstas#endif /* DDB */ 221233294Sstas 222178825Sdfr#ifdef WITNESS_SKIPSPIN 223233294Sstasint witness_skipspin = 1; 224178825Sdfr#else 225178825Sdfrint witness_skipspin = 0; 226178825Sdfr#endif 227178825SdfrTUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 228178825SdfrSYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 229178825Sdfr ""); 230178825Sdfr 231178825Sdfrstatic struct mtx w_mtx; 232178825Sdfrstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 233233294Sstasstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 234233294Sstasstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 235178825Sdfrstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 236233294Sstasstatic struct witness_child_list_entry *w_child_free = NULL; 237178825Sdfrstatic struct lock_list_entry *w_lock_list_free = NULL; 238178825Sdfrstatic int witness_dead; /* fatal error, probably no memory */ 239233294Sstas 240178825Sdfrstatic struct witness w_data[WITNESS_COUNT]; 241178825Sdfrstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 242178825Sdfrstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 243178825Sdfr 244178825Sdfrstatic struct witness_order_list_entry order_lists[] = { 245178825Sdfr { "proctree", &lock_class_sx }, 246178825Sdfr { "allproc", &lock_class_sx }, 247178825Sdfr { "Giant", &lock_class_mtx_sleep }, 248178825Sdfr { "filedesc structure", &lock_class_mtx_sleep }, 249178825Sdfr { "pipe mutex", &lock_class_mtx_sleep }, 250233294Sstas { "sigio lock", &lock_class_mtx_sleep }, 251233294Sstas { "process group", &lock_class_mtx_sleep }, 252178825Sdfr { "process lock", &lock_class_mtx_sleep }, 253178825Sdfr { "session", &lock_class_mtx_sleep }, 254178825Sdfr { "uidinfo hash", &lock_class_mtx_sleep }, 255178825Sdfr { "uidinfo struct", &lock_class_mtx_sleep }, 256178825Sdfr { NULL, NULL }, 257178825Sdfr /* 258233294Sstas * spin locks 259233294Sstas */ 260178825Sdfr#ifdef SMP 261178825Sdfr { "ap boot", &lock_class_mtx_spin }, 262233294Sstas#ifdef __i386__ 263178825Sdfr { "com", &lock_class_mtx_spin }, 264233294Sstas#endif 265178825Sdfr#endif 266178825Sdfr { "sio", &lock_class_mtx_spin }, 267178825Sdfr#ifdef __i386__ 268178825Sdfr { "cy", &lock_class_mtx_spin }, 269178825Sdfr#endif 270178825Sdfr { "sabtty", &lock_class_mtx_spin }, 271233294Sstas { "zstty", &lock_class_mtx_spin }, 272233294Sstas { "ng_node", &lock_class_mtx_spin }, 273233294Sstas { "ng_worklist", &lock_class_mtx_spin }, 274233294Sstas { "ithread table lock", &lock_class_mtx_spin }, 275233294Sstas { "sched lock", &lock_class_mtx_spin }, 276233294Sstas { "callout", &lock_class_mtx_spin }, 277178825Sdfr /* 278178825Sdfr * leaf locks 279178825Sdfr */ 280233294Sstas { "allpmaps", &lock_class_mtx_spin }, 281178825Sdfr { "vm page queue free mutex", &lock_class_mtx_spin }, 282178825Sdfr { "icu", &lock_class_mtx_spin }, 283178825Sdfr#ifdef SMP 284178825Sdfr { "smp rendezvous", &lock_class_mtx_spin }, 285233294Sstas#if defined(__i386__) && defined(APIC_IO) 286178825Sdfr { "tlb", &lock_class_mtx_spin }, 287178825Sdfr#endif 288178825Sdfr#ifdef __sparc64__ 289178825Sdfr { "ipi", &lock_class_mtx_spin }, 290178825Sdfr#endif 291178825Sdfr#endif 292233294Sstas { "clk", &lock_class_mtx_spin }, 293178825Sdfr { "mutex profiling lock", &lock_class_mtx_spin }, 294178825Sdfr { "kse zombie lock", &lock_class_mtx_spin }, 295178825Sdfr { "ALD Queue", &lock_class_mtx_spin }, 296178825Sdfr#ifdef __ia64__ 297178825Sdfr { "MCA spin lock", &lock_class_mtx_spin }, 298178825Sdfr#endif 299178825Sdfr#ifdef __i386__ 300178825Sdfr { "pcicfg", &lock_class_mtx_spin }, 301178825Sdfr#endif 302178825Sdfr { NULL, NULL }, 303178825Sdfr { NULL, NULL } 304178825Sdfr}; 305178825Sdfr 306178825Sdfr#ifdef BLESSING 307233294Sstas/* 308178825Sdfr * Pairs of locks which have been blessed 309178825Sdfr * Don't complain about order problems with blessed locks 310178825Sdfr */ 311178825Sdfrstatic struct witness_blessed blessed_list[] = { 312178825Sdfr}; 313178825Sdfrstatic int blessed_count = 314178825Sdfr sizeof(blessed_list) / sizeof(struct witness_blessed); 315178825Sdfr#endif 316178825Sdfr 317178825Sdfr/* 318178825Sdfr * List of all locks in the system. 319233294Sstas */ 320178825SdfrTAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 321178825Sdfr 322178825Sdfrstatic struct mtx all_mtx = { 323178825Sdfr { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 324178825Sdfr "All locks list", /* mtx_object.lo_name */ 325178825Sdfr "All locks list", /* mtx_object.lo_type */ 326178825Sdfr LO_INITIALIZED, /* mtx_object.lo_flags */ 327178825Sdfr { NULL, NULL }, /* mtx_object.lo_list */ 328178825Sdfr NULL }, /* mtx_object.lo_witness */ 329178825Sdfr MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 330178825Sdfr TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 331178825Sdfr { NULL, NULL } /* mtx_contested */ 332178825Sdfr}; 333178825Sdfr 334178825Sdfr/* 335178825Sdfr * This global is set to 0 once it becomes safe to use the witness code. 336233294Sstas */ 337178825Sdfrstatic int witness_cold = 1; 338178825Sdfr 339178825Sdfr/* 340178825Sdfr * Global variables for book keeping. 341178825Sdfr */ 342178825Sdfrstatic int lock_cur_cnt; 343178825Sdfrstatic int lock_max_cnt; 344178825Sdfr 345233294Sstas/* 346233294Sstas * The WITNESS-enabled diagnostic code. 347233294Sstas */ 348233294Sstasstatic void 349233294Sstaswitness_initialize(void *dummy __unused) 350233294Sstas{ 351233294Sstas struct lock_object *lock; 352233294Sstas struct witness_order_list_entry *order; 353233294Sstas struct witness *w, *w1; 354233294Sstas int i; 355233294Sstas 356233294Sstas /* 357233294Sstas * We have to release Giant before initializing its witness 358233294Sstas * structure so that WITNESS doesn't get confused. 359233294Sstas */ 360233294Sstas mtx_unlock(&Giant); 361233294Sstas mtx_assert(&Giant, MA_NOTOWNED); 362233294Sstas 363233294Sstas CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 364233294Sstas TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 365233294Sstas mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 366233294Sstas MTX_NOWITNESS); 367233294Sstas for (i = 0; i < WITNESS_COUNT; i++) 368233294Sstas witness_free(&w_data[i]); 369233294Sstas for (i = 0; i < WITNESS_CHILDCOUNT; i++) 370233294Sstas witness_child_free(&w_childdata[i]); 371233294Sstas for (i = 0; i < LOCK_CHILDCOUNT; i++) 372233294Sstas witness_lock_list_free(&w_locklistdata[i]); 373233294Sstas 374233294Sstas /* First add in all the specified order lists. */ 375233294Sstas for (order = order_lists; order->w_name != NULL; order++) { 376233294Sstas w = enroll(order->w_name, order->w_class); 377233294Sstas if (w == NULL) 378233294Sstas continue; 379233294Sstas w->w_file = "order list"; 380233294Sstas for (order++; order->w_name != NULL; order++) { 381233294Sstas w1 = enroll(order->w_name, order->w_class); 382233294Sstas if (w1 == NULL) 383233294Sstas continue; 384233294Sstas w1->w_file = "order list"; 385233294Sstas if (!itismychild(w, w1)) 386233294Sstas panic("Not enough memory for static orders!"); 387233294Sstas w = w1; 388233294Sstas } 389233294Sstas } 390233294Sstas 391233294Sstas /* Iterate through all locks and add them to witness. */ 392233294Sstas mtx_lock(&all_mtx); 393178825Sdfr TAILQ_FOREACH(lock, &all_locks, lo_list) { 394178825Sdfr if (lock->lo_flags & LO_WITNESS) 395178825Sdfr lock->lo_witness = enroll(lock->lo_type, 396178825Sdfr lock->lo_class); 397178825Sdfr else 398178825Sdfr lock->lo_witness = NULL; 399178825Sdfr } 400178825Sdfr mtx_unlock(&all_mtx); 401178825Sdfr 402233294Sstas /* Mark the witness code as being ready for use. */ 403233294Sstas atomic_store_rel_int(&witness_cold, 0); 404178825Sdfr 405178825Sdfr mtx_lock(&Giant); 406178825Sdfr} 407233294SstasSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 408178825Sdfr 409178825Sdfrvoid 410178825Sdfrwitness_init(struct lock_object *lock) 411178825Sdfr{ 412233294Sstas struct lock_class *class; 413178825Sdfr 414233294Sstas class = lock->lo_class; 415178825Sdfr if (lock->lo_flags & LO_INITIALIZED) 416178825Sdfr panic("%s: lock (%s) %s is already initialized", __func__, 417178825Sdfr class->lc_name, lock->lo_name); 418178825Sdfr if ((lock->lo_flags & LO_RECURSABLE) != 0 && 419178825Sdfr (class->lc_flags & LC_RECURSABLE) == 0) 420178825Sdfr panic("%s: lock (%s) %s can not be recursable", __func__, 421178825Sdfr class->lc_name, lock->lo_name); 422178825Sdfr if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 423178825Sdfr (class->lc_flags & LC_SLEEPABLE) == 0) 424233294Sstas panic("%s: lock (%s) %s can not be sleepable", __func__, 425178825Sdfr class->lc_name, lock->lo_name); 426233294Sstas if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 427178825Sdfr (class->lc_flags & LC_UPGRADABLE) == 0) 428178825Sdfr panic("%s: lock (%s) %s can not be upgradable", __func__, 429178825Sdfr class->lc_name, lock->lo_name); 430178825Sdfr 431178825Sdfr mtx_lock(&all_mtx); 432178825Sdfr TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 433178825Sdfr lock->lo_flags |= LO_INITIALIZED; 434178825Sdfr lock_cur_cnt++; 435178825Sdfr if (lock_cur_cnt > lock_max_cnt) 436178825Sdfr lock_max_cnt = lock_cur_cnt; 437178825Sdfr mtx_unlock(&all_mtx); 438178825Sdfr if (!witness_cold && !witness_dead && panicstr == NULL && 439178825Sdfr (lock->lo_flags & LO_WITNESS) != 0) 440178825Sdfr lock->lo_witness = enroll(lock->lo_type, class); 441178825Sdfr else 442178825Sdfr lock->lo_witness = NULL; 443178825Sdfr} 444178825Sdfr 445178825Sdfrvoid 446178825Sdfrwitness_destroy(struct lock_object *lock) 447233294Sstas{ 448178825Sdfr struct witness *w; 449178825Sdfr 450178825Sdfr if (witness_cold) 451 panic("lock (%s) %s destroyed while witness_cold", 452 lock->lo_class->lc_name, lock->lo_name); 453 if ((lock->lo_flags & LO_INITIALIZED) == 0) 454 panic("%s: lock (%s) %s is not initialized", __func__, 455 lock->lo_class->lc_name, lock->lo_name); 456 457 /* XXX: need to verify that no one holds the lock */ 458 w = lock->lo_witness; 459 if (w != NULL) { 460 mtx_lock_spin(&w_mtx); 461 MPASS(w->w_refcount > 0); 462 w->w_refcount--; 463 464 /* 465 * Lock is already released if we have an allocation failure 466 * and depart() fails. 467 */ 468 if (w->w_refcount != 0 || depart(w)) 469 mtx_unlock_spin(&w_mtx); 470 } 471 472 mtx_lock(&all_mtx); 473 lock_cur_cnt--; 474 TAILQ_REMOVE(&all_locks, lock, lo_list); 475 lock->lo_flags &= ~LO_INITIALIZED; 476 mtx_unlock(&all_mtx); 477} 478 479#ifdef DDB 480static void 481witness_display_list(void(*prnt)(const char *fmt, ...), 482 struct witness_list *list) 483{ 484 struct witness *w; 485 486 STAILQ_FOREACH(w, list, w_typelist) { 487 if (w->w_file == NULL || w->w_level > 0) 488 continue; 489 /* 490 * This lock has no anscestors, display its descendants. 491 */ 492 witness_displaydescendants(prnt, w, 0); 493 } 494} 495 496static void 497witness_display(void(*prnt)(const char *fmt, ...)) 498{ 499 struct witness *w; 500 501 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 502 witness_levelall(); 503 504 /* Clear all the displayed flags. */ 505 STAILQ_FOREACH(w, &w_all, w_list) { 506 w->w_displayed = 0; 507 } 508 509 /* 510 * First, handle sleep locks which have been acquired at least 511 * once. 512 */ 513 prnt("Sleep locks:\n"); 514 witness_display_list(prnt, &w_sleep); 515 516 /* 517 * Now do spin locks which have been acquired at least once. 518 */ 519 prnt("\nSpin locks:\n"); 520 witness_display_list(prnt, &w_spin); 521 522 /* 523 * Finally, any locks which have not been acquired yet. 524 */ 525 prnt("\nLocks which were never acquired:\n"); 526 STAILQ_FOREACH(w, &w_all, w_list) { 527 if (w->w_file != NULL || w->w_refcount == 0) 528 continue; 529 prnt("%s\n", w->w_name); 530 } 531} 532#endif /* DDB */ 533 534/* Trim useless garbage from filenames. */ 535static const char * 536fixup_filename(const char *file) 537{ 538 539 if (file == NULL) 540 return (NULL); 541 while (strncmp(file, "../", 3) == 0) 542 file += 3; 543 return (file); 544} 545 546void 547witness_lock(struct lock_object *lock, int flags, const char *file, int line) 548{ 549 struct lock_list_entry **lock_list, *lle; 550 struct lock_instance *lock1, *lock2; 551 struct lock_class *class; 552 struct witness *w, *w1; 553 struct thread *td; 554 int i, j; 555#ifdef DDB 556 int go_into_ddb = 0; 557#endif 558 559 if (witness_cold || witness_dead || lock->lo_witness == NULL || 560 panicstr != NULL) 561 return; 562 w = lock->lo_witness; 563 class = lock->lo_class; 564 td = curthread; 565 file = fixup_filename(file); 566 567 if (class->lc_flags & LC_SLEEPLOCK) { 568 /* 569 * Since spin locks include a critical section, this check 570 * impliclty enforces a lock order of all sleep locks before 571 * all spin locks. 572 */ 573 if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0) 574 panic("blockable sleep lock (%s) %s @ %s:%d", 575 class->lc_name, lock->lo_name, file, line); 576 lock_list = &td->td_sleeplocks; 577 } else 578 lock_list = PCPU_PTR(spinlocks); 579 580 /* 581 * Is this the first lock acquired? If so, then no order checking 582 * is needed. 583 */ 584 if (*lock_list == NULL) 585 goto out; 586 587 /* 588 * Check to see if we are recursing on a lock we already own. 589 */ 590 lock1 = find_instance(*lock_list, lock); 591 if (lock1 != NULL) { 592 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 593 (flags & LOP_EXCLUSIVE) == 0) { 594 printf("shared lock of (%s) %s @ %s:%d\n", 595 class->lc_name, lock->lo_name, file, line); 596 printf("while exclusively locked from %s:%d\n", 597 lock1->li_file, lock1->li_line); 598 panic("share->excl"); 599 } 600 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 601 (flags & LOP_EXCLUSIVE) != 0) { 602 printf("exclusive lock of (%s) %s @ %s:%d\n", 603 class->lc_name, lock->lo_name, file, line); 604 printf("while share locked from %s:%d\n", 605 lock1->li_file, lock1->li_line); 606 panic("excl->share"); 607 } 608 lock1->li_flags++; 609 if ((lock->lo_flags & LO_RECURSABLE) == 0) { 610 printf( 611 "recursed on non-recursive lock (%s) %s @ %s:%d\n", 612 class->lc_name, lock->lo_name, file, line); 613 printf("first acquired @ %s:%d\n", lock1->li_file, 614 lock1->li_line); 615 panic("recurse"); 616 } 617 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 618 td->td_proc->p_pid, lock->lo_name, 619 lock1->li_flags & LI_RECURSEMASK); 620 lock1->li_file = file; 621 lock1->li_line = line; 622 return; 623 } 624 625 /* 626 * Try locks do not block if they fail to acquire the lock, thus 627 * there is no danger of deadlocks or of switching while holding a 628 * spin lock if we acquire a lock via a try operation. 629 */ 630 if (flags & LOP_TRYLOCK) 631 goto out; 632 633 /* 634 * Check for duplicate locks of the same type. Note that we only 635 * have to check for this on the last lock we just acquired. Any 636 * other cases will be caught as lock order violations. 637 */ 638 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 639 w1 = lock1->li_lock->lo_witness; 640 if (w1 == w) { 641 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK)) 642 goto out; 643 w->w_same_squawked = 1; 644 printf("acquiring duplicate lock of same type: \"%s\"\n", 645 lock->lo_type); 646 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 647 lock1->li_file, lock1->li_line); 648 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 649#ifdef DDB 650 go_into_ddb = 1; 651#endif 652 goto out; 653 } 654 MPASS(!mtx_owned(&w_mtx)); 655 mtx_lock_spin(&w_mtx); 656 /* 657 * If we have a known higher number just say ok 658 */ 659 if (witness_watch > 1 && w->w_level > w1->w_level) { 660 mtx_unlock_spin(&w_mtx); 661 goto out; 662 } 663 /* 664 * If we know that the the lock we are acquiring comes after 665 * the lock we most recently acquired in the lock order tree, 666 * then there is no need for any further checks. 667 */ 668 if (isitmydescendant(w1, w)) { 669 mtx_unlock_spin(&w_mtx); 670 goto out; 671 } 672 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 673 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 674 675 MPASS(j < WITNESS_COUNT); 676 lock1 = &lle->ll_children[i]; 677 w1 = lock1->li_lock->lo_witness; 678 679 /* 680 * If this lock doesn't undergo witness checking, 681 * then skip it. 682 */ 683 if (w1 == NULL) { 684 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 685 ("lock missing witness structure")); 686 continue; 687 } 688 /* 689 * If we are locking Giant and this is a sleepable 690 * lock, then skip it. 691 */ 692 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 693 lock == &Giant.mtx_object) 694 continue; 695 /* 696 * If we are locking a sleepable lock and this lock 697 * is Giant, then skip it. 698 */ 699 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 700 lock1->li_lock == &Giant.mtx_object) 701 continue; 702 /* 703 * If we are locking a sleepable lock and this lock 704 * isn't sleepable, we want to treat it as a lock 705 * order violation to enfore a general lock order of 706 * sleepable locks before non-sleepable locks. 707 */ 708 if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 709 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 710 /* 711 * Check the lock order hierarchy for a reveresal. 712 */ 713 if (!isitmydescendant(w, w1)) 714 continue; 715 /* 716 * We have a lock order violation, check to see if it 717 * is allowed or has already been yelled about. 718 */ 719 mtx_unlock_spin(&w_mtx); 720#ifdef BLESSING 721 if (blessed(w, w1)) 722 goto out; 723#endif 724 if (lock1->li_lock == &Giant.mtx_object) { 725 if (w1->w_Giant_squawked) 726 goto out; 727 else 728 w1->w_Giant_squawked = 1; 729 } else { 730 if (w1->w_other_squawked) 731 goto out; 732 else 733 w1->w_other_squawked = 1; 734 } 735 /* 736 * Ok, yell about it. 737 */ 738 printf("lock order reversal\n"); 739 /* 740 * Try to locate an earlier lock with 741 * witness w in our list. 742 */ 743 do { 744 lock2 = &lle->ll_children[i]; 745 MPASS(lock2->li_lock != NULL); 746 if (lock2->li_lock->lo_witness == w) 747 break; 748 i--; 749 if (i == 0 && lle->ll_next != NULL) { 750 lle = lle->ll_next; 751 i = lle->ll_count - 1; 752 MPASS(i >= 0 && i < LOCK_NCHILDREN); 753 } 754 } while (i >= 0); 755 if (i < 0) { 756 printf(" 1st %p %s (%s) @ %s:%d\n", 757 lock1->li_lock, lock1->li_lock->lo_name, 758 lock1->li_lock->lo_type, lock1->li_file, 759 lock1->li_line); 760 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 761 lock->lo_name, lock->lo_type, file, line); 762 } else { 763 printf(" 1st %p %s (%s) @ %s:%d\n", 764 lock2->li_lock, lock2->li_lock->lo_name, 765 lock2->li_lock->lo_type, lock2->li_file, 766 lock2->li_line); 767 printf(" 2nd %p %s (%s) @ %s:%d\n", 768 lock1->li_lock, lock1->li_lock->lo_name, 769 lock1->li_lock->lo_type, lock1->li_file, 770 lock1->li_line); 771 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 772 lock->lo_name, lock->lo_type, file, line); 773 } 774#ifdef DDB 775 go_into_ddb = 1; 776#endif 777 goto out; 778 } 779 } 780 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 781 /* 782 * Don't build a new relationship between a sleepable lock and 783 * Giant if it is the wrong direction. The real lock order is that 784 * sleepable locks come before Giant. 785 */ 786 if (!(lock1->li_lock == &Giant.mtx_object && 787 (lock->lo_flags & LO_SLEEPABLE) != 0)) { 788 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 789 lock->lo_type, lock1->li_lock->lo_type); 790 if (!itismychild(lock1->li_lock->lo_witness, w)) 791 /* Witness is dead. */ 792 return; 793 } 794 mtx_unlock_spin(&w_mtx); 795 796out: 797#ifdef DDB 798 if (go_into_ddb) { 799 if (witness_trace) 800 backtrace(); 801 if (witness_ddb) 802 Debugger(__func__); 803 } 804#endif 805 w->w_file = file; 806 w->w_line = line; 807 808 lle = *lock_list; 809 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 810 lle = witness_lock_list_get(); 811 if (lle == NULL) 812 return; 813 lle->ll_next = *lock_list; 814 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 815 td->td_proc->p_pid, lle); 816 *lock_list = lle; 817 } 818 lock1 = &lle->ll_children[lle->ll_count++]; 819 lock1->li_lock = lock; 820 lock1->li_line = line; 821 lock1->li_file = file; 822 if ((flags & LOP_EXCLUSIVE) != 0) 823 lock1->li_flags = LI_EXCLUSIVE; 824 else 825 lock1->li_flags = 0; 826 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 827 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 828} 829 830void 831witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 832{ 833 struct lock_instance *instance; 834 struct lock_class *class; 835 836 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 837 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 838 return; 839 class = lock->lo_class; 840 file = fixup_filename(file); 841 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 842 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 843 class->lc_name, lock->lo_name, file, line); 844 if ((flags & LOP_TRYLOCK) == 0) 845 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 846 lock->lo_name, file, line); 847 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 848 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 849 class->lc_name, lock->lo_name, file, line); 850 instance = find_instance(curthread->td_sleeplocks, lock); 851 if (instance == NULL) 852 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 853 class->lc_name, lock->lo_name, file, line); 854 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 855 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 856 class->lc_name, lock->lo_name, file, line); 857 if ((instance->li_flags & LI_RECURSEMASK) != 0) 858 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 859 class->lc_name, lock->lo_name, 860 instance->li_flags & LI_RECURSEMASK, file, line); 861 instance->li_flags |= LI_EXCLUSIVE; 862} 863 864void 865witness_downgrade(struct lock_object *lock, int flags, const char *file, 866 int line) 867{ 868 struct lock_instance *instance; 869 struct lock_class *class; 870 871 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 872 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 873 return; 874 class = lock->lo_class; 875 file = fixup_filename(file); 876 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 877 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 878 class->lc_name, lock->lo_name, file, line); 879 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 880 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 881 class->lc_name, lock->lo_name, file, line); 882 instance = find_instance(curthread->td_sleeplocks, lock); 883 if (instance == NULL) 884 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 885 class->lc_name, lock->lo_name, file, line); 886 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 887 panic("downgrade of shared lock (%s) %s @ %s:%d", 888 class->lc_name, lock->lo_name, file, line); 889 if ((instance->li_flags & LI_RECURSEMASK) != 0) 890 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 891 class->lc_name, lock->lo_name, 892 instance->li_flags & LI_RECURSEMASK, file, line); 893 instance->li_flags &= ~LI_EXCLUSIVE; 894} 895 896void 897witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 898{ 899 struct lock_list_entry **lock_list, *lle; 900 struct lock_instance *instance; 901 struct lock_class *class; 902 struct thread *td; 903 register_t s; 904 int i, j; 905 906 if (witness_cold || witness_dead || lock->lo_witness == NULL || 907 panicstr != NULL) 908 return; 909 td = curthread; 910 class = lock->lo_class; 911 file = fixup_filename(file); 912 if (class->lc_flags & LC_SLEEPLOCK) 913 lock_list = &td->td_sleeplocks; 914 else 915 lock_list = PCPU_PTR(spinlocks); 916 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 917 for (i = 0; i < (*lock_list)->ll_count; i++) { 918 instance = &(*lock_list)->ll_children[i]; 919 if (instance->li_lock == lock) { 920 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 921 (flags & LOP_EXCLUSIVE) == 0) { 922 printf( 923 "shared unlock of (%s) %s @ %s:%d\n", 924 class->lc_name, lock->lo_name, 925 file, line); 926 printf( 927 "while exclusively locked from %s:%d\n", 928 instance->li_file, 929 instance->li_line); 930 panic("excl->ushare"); 931 } 932 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 933 (flags & LOP_EXCLUSIVE) != 0) { 934 printf( 935 "exclusive unlock of (%s) %s @ %s:%d\n", 936 class->lc_name, lock->lo_name, 937 file, line); 938 printf( 939 "while share locked from %s:%d\n", 940 instance->li_file, 941 instance->li_line); 942 panic("share->uexcl"); 943 } 944 /* If we are recursed, unrecurse. */ 945 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 946 CTR4(KTR_WITNESS, 947 "%s: pid %d unrecursed on %s r=%d", __func__, 948 td->td_proc->p_pid, 949 instance->li_lock->lo_name, 950 instance->li_flags); 951 instance->li_flags--; 952 return; 953 } 954 s = intr_disable(); 955 CTR4(KTR_WITNESS, 956 "%s: pid %d removed %s from lle[%d]", __func__, 957 td->td_proc->p_pid, 958 instance->li_lock->lo_name, 959 (*lock_list)->ll_count - 1); 960 for (j = i; j < (*lock_list)->ll_count - 1; j++) 961 (*lock_list)->ll_children[j] = 962 (*lock_list)->ll_children[j + 1]; 963 (*lock_list)->ll_count--; 964 intr_restore(s); 965 if ((*lock_list)->ll_count == 0) { 966 lle = *lock_list; 967 *lock_list = lle->ll_next; 968 CTR3(KTR_WITNESS, 969 "%s: pid %d removed lle %p", __func__, 970 td->td_proc->p_pid, lle); 971 witness_lock_list_free(lle); 972 } 973 return; 974 } 975 } 976 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 977 file, line); 978} 979 980/* 981 * Warn if any locks other than 'lock' are held. Flags can be passed in to 982 * exempt Giant and sleepable locks from the checks as well. If any 983 * non-exempt locks are held, then a supplied message is printed to the 984 * console along with a list of the offending locks. If indicated in the 985 * flags then a failure results in a panic as well. 986 */ 987int 988witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 989{ 990 struct lock_list_entry *lle; 991 struct lock_instance *lock1; 992 struct thread *td; 993 va_list ap; 994 int i, n; 995 996 if (witness_cold || witness_dead || panicstr != NULL) 997 return (0); 998 n = 0; 999 td = curthread; 1000 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 1001 for (i = lle->ll_count - 1; i >= 0; i--) { 1002 lock1 = &lle->ll_children[i]; 1003 if (lock1->li_lock == lock) 1004 continue; 1005 if (flags & WARN_GIANTOK && 1006 lock1->li_lock == &Giant.mtx_object) 1007 continue; 1008 if (flags & WARN_SLEEPOK && 1009 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 1010 continue; 1011 if (n == 0) { 1012 va_start(ap, fmt); 1013 vprintf(fmt, ap); 1014 va_end(ap); 1015 printf(" with the following"); 1016 if (flags & WARN_SLEEPOK) 1017 printf(" non-sleepable"); 1018 printf("locks held:\n"); 1019 } 1020 n++; 1021 witness_list_lock(lock1); 1022 } 1023 if (PCPU_GET(spinlocks) != NULL) { 1024 /* 1025 * Since we already hold a spinlock preemption is 1026 * already blocked. 1027 */ 1028 if (n == 0) { 1029 va_start(ap, fmt); 1030 vprintf(fmt, ap); 1031 va_end(ap); 1032 printf(" with the following"); 1033 if (flags & WARN_SLEEPOK) 1034 printf(" non-sleepable"); 1035 printf("locks held:\n"); 1036 } 1037 n += witness_list_locks(PCPU_PTR(spinlocks)); 1038 } 1039 if (flags & WARN_PANIC && n) 1040 panic("witness_warn"); 1041#ifdef DDB 1042 else if (witness_ddb && n) 1043 Debugger(__func__); 1044#endif 1045 return (n); 1046} 1047 1048const char * 1049witness_file(struct lock_object *lock) 1050{ 1051 struct witness *w; 1052 1053 if (witness_cold || witness_dead || lock->lo_witness == NULL) 1054 return ("?"); 1055 w = lock->lo_witness; 1056 return (w->w_file); 1057} 1058 1059int 1060witness_line(struct lock_object *lock) 1061{ 1062 struct witness *w; 1063 1064 if (witness_cold || witness_dead || lock->lo_witness == NULL) 1065 return (0); 1066 w = lock->lo_witness; 1067 return (w->w_line); 1068} 1069 1070static struct witness * 1071enroll(const char *description, struct lock_class *lock_class) 1072{ 1073 struct witness *w; 1074 1075 if (!witness_watch || witness_dead || panicstr != NULL) 1076 return (NULL); 1077 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 1078 return (NULL); 1079 mtx_lock_spin(&w_mtx); 1080 STAILQ_FOREACH(w, &w_all, w_list) { 1081 if (w->w_name == description || (w->w_refcount > 0 && 1082 strcmp(description, w->w_name) == 0)) { 1083 w->w_refcount++; 1084 mtx_unlock_spin(&w_mtx); 1085 if (lock_class != w->w_class) 1086 panic( 1087 "lock (%s) %s does not match earlier (%s) lock", 1088 description, lock_class->lc_name, 1089 w->w_class->lc_name); 1090 return (w); 1091 } 1092 } 1093 /* 1094 * This isn't quite right, as witness_cold is still 0 while we 1095 * enroll all the locks initialized before witness_initialize(). 1096 */ 1097 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 1098 mtx_unlock_spin(&w_mtx); 1099 panic("spin lock %s not in order list", description); 1100 } 1101 if ((w = witness_get()) == NULL) 1102 return (NULL); 1103 w->w_name = description; 1104 w->w_class = lock_class; 1105 w->w_refcount = 1; 1106 STAILQ_INSERT_HEAD(&w_all, w, w_list); 1107 if (lock_class->lc_flags & LC_SPINLOCK) 1108 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1109 else if (lock_class->lc_flags & LC_SLEEPLOCK) 1110 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1111 else { 1112 mtx_unlock_spin(&w_mtx); 1113 panic("lock class %s is not sleep or spin", 1114 lock_class->lc_name); 1115 } 1116 mtx_unlock_spin(&w_mtx); 1117 return (w); 1118} 1119 1120/* Don't let the door bang you on the way out... */ 1121static int 1122depart(struct witness *w) 1123{ 1124 struct witness_child_list_entry *wcl, *nwcl; 1125 struct witness_list *list; 1126 struct witness *parent; 1127 1128 MPASS(w->w_refcount == 0); 1129 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1130 list = &w_sleep; 1131 else 1132 list = &w_spin; 1133 /* 1134 * First, we run through the entire tree looking for any 1135 * witnesses that the outgoing witness is a child of. For 1136 * each parent that we find, we reparent all the direct 1137 * children of the outgoing witness to its parent. 1138 */ 1139 STAILQ_FOREACH(parent, list, w_typelist) { 1140 if (!isitmychild(parent, w)) 1141 continue; 1142 removechild(parent, w); 1143 if (!reparentchildren(parent, w)) 1144 return (0); 1145 } 1146 1147 /* 1148 * Now we go through and free up the child list of the 1149 * outgoing witness. 1150 */ 1151 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1152 nwcl = wcl->wcl_next; 1153 witness_child_free(wcl); 1154 } 1155 1156 /* 1157 * Detach from various lists and free. 1158 */ 1159 STAILQ_REMOVE(list, w, witness, w_typelist); 1160 STAILQ_REMOVE(&w_all, w, witness, w_list); 1161 witness_free(w); 1162 1163 /* Finally, fixup the tree. */ 1164 return (rebalancetree(list)); 1165} 1166 1167/* 1168 * Prune an entire lock order tree. We look for cases where a lock 1169 * is now both a descendant and a direct child of a given lock. In 1170 * that case, we want to remove the direct child link from the tree. 1171 * 1172 * Returns false if insertchild() fails. 1173 */ 1174static int 1175rebalancetree(struct witness_list *list) 1176{ 1177 struct witness *child, *parent; 1178 1179 STAILQ_FOREACH(child, list, w_typelist) { 1180 STAILQ_FOREACH(parent, list, w_typelist) { 1181 if (!isitmychild(parent, child)) 1182 continue; 1183 removechild(parent, child); 1184 if (isitmydescendant(parent, child)) 1185 continue; 1186 if (!insertchild(parent, child)) 1187 return (0); 1188 } 1189 } 1190 witness_levelall(); 1191 return (1); 1192} 1193 1194/* 1195 * Add "child" as a direct child of "parent". Returns false if 1196 * we fail due to out of memory. 1197 */ 1198static int 1199insertchild(struct witness *parent, struct witness *child) 1200{ 1201 struct witness_child_list_entry **wcl; 1202 1203 MPASS(child != NULL && parent != NULL); 1204 1205 /* 1206 * Insert "child" after "parent" 1207 */ 1208 wcl = &parent->w_children; 1209 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 1210 wcl = &(*wcl)->wcl_next; 1211 if (*wcl == NULL) { 1212 *wcl = witness_child_get(); 1213 if (*wcl == NULL) 1214 return (0); 1215 } 1216 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1217 1218 return (1); 1219} 1220 1221/* 1222 * Make all the direct descendants of oldparent be direct descendants 1223 * of newparent. 1224 */ 1225static int 1226reparentchildren(struct witness *newparent, struct witness *oldparent) 1227{ 1228 struct witness_child_list_entry *wcl; 1229 int i; 1230 1231 /* Avoid making a witness a child of itself. */ 1232 MPASS(!isitmychild(oldparent, newparent)); 1233 1234 for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1235 for (i = 0; i < wcl->wcl_count; i++) 1236 if (!insertchild(newparent, wcl->wcl_children[i])) 1237 return (0); 1238 return (1); 1239} 1240 1241static int 1242itismychild(struct witness *parent, struct witness *child) 1243{ 1244 struct witness_list *list; 1245 1246 MPASS(child != NULL && parent != NULL); 1247 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1248 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1249 panic( 1250 "%s: parent (%s) and child (%s) are not the same lock type", 1251 __func__, parent->w_class->lc_name, 1252 child->w_class->lc_name); 1253 1254 if (!insertchild(parent, child)) 1255 return (0); 1256 1257 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1258 list = &w_sleep; 1259 else 1260 list = &w_spin; 1261 return (rebalancetree(list)); 1262} 1263 1264static void 1265removechild(struct witness *parent, struct witness *child) 1266{ 1267 struct witness_child_list_entry **wcl, *wcl1; 1268 int i; 1269 1270 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1271 for (i = 0; i < (*wcl)->wcl_count; i++) 1272 if ((*wcl)->wcl_children[i] == child) 1273 goto found; 1274 return; 1275found: 1276 (*wcl)->wcl_count--; 1277 if ((*wcl)->wcl_count > i) 1278 (*wcl)->wcl_children[i] = 1279 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1280 MPASS((*wcl)->wcl_children[i] != NULL); 1281 if ((*wcl)->wcl_count != 0) 1282 return; 1283 wcl1 = *wcl; 1284 *wcl = wcl1->wcl_next; 1285 witness_child_free(wcl1); 1286} 1287 1288static int 1289isitmychild(struct witness *parent, struct witness *child) 1290{ 1291 struct witness_child_list_entry *wcl; 1292 int i; 1293 1294 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1295 for (i = 0; i < wcl->wcl_count; i++) { 1296 if (wcl->wcl_children[i] == child) 1297 return (1); 1298 } 1299 } 1300 return (0); 1301} 1302 1303static int 1304isitmydescendant(struct witness *parent, struct witness *child) 1305{ 1306 struct witness_child_list_entry *wcl; 1307 int i, j; 1308 1309 if (isitmychild(parent, child)) 1310 return (1); 1311 j = 0; 1312 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1313 MPASS(j < 1000); 1314 for (i = 0; i < wcl->wcl_count; i++) { 1315 if (isitmydescendant(wcl->wcl_children[i], child)) 1316 return (1); 1317 } 1318 j++; 1319 } 1320 return (0); 1321} 1322 1323static void 1324witness_levelall (void) 1325{ 1326 struct witness_list *list; 1327 struct witness *w, *w1; 1328 1329 /* 1330 * First clear all levels. 1331 */ 1332 STAILQ_FOREACH(w, &w_all, w_list) { 1333 w->w_level = 0; 1334 } 1335 1336 /* 1337 * Look for locks with no parent and level all their descendants. 1338 */ 1339 STAILQ_FOREACH(w, &w_all, w_list) { 1340 /* 1341 * This is just an optimization, technically we could get 1342 * away just walking the all list each time. 1343 */ 1344 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1345 list = &w_sleep; 1346 else 1347 list = &w_spin; 1348 STAILQ_FOREACH(w1, list, w_typelist) { 1349 if (isitmychild(w1, w)) 1350 goto skip; 1351 } 1352 witness_leveldescendents(w, 0); 1353 skip: 1354 ; /* silence GCC 3.x */ 1355 } 1356} 1357 1358static void 1359witness_leveldescendents(struct witness *parent, int level) 1360{ 1361 struct witness_child_list_entry *wcl; 1362 int i; 1363 1364 if (parent->w_level < level) 1365 parent->w_level = level; 1366 level++; 1367 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1368 for (i = 0; i < wcl->wcl_count; i++) 1369 witness_leveldescendents(wcl->wcl_children[i], level); 1370} 1371 1372static void 1373witness_displaydescendants(void(*prnt)(const char *fmt, ...), 1374 struct witness *parent, int indent) 1375{ 1376 struct witness_child_list_entry *wcl; 1377 int i, level; 1378 1379 level = parent->w_level; 1380 prnt("%-2d", level); 1381 for (i = 0; i < indent; i++) 1382 prnt(" "); 1383 if (parent->w_refcount > 0) 1384 prnt("%s", parent->w_name); 1385 else 1386 prnt("(dead)"); 1387 if (parent->w_displayed) { 1388 prnt(" -- (already displayed)\n"); 1389 return; 1390 } 1391 parent->w_displayed = 1; 1392 if (parent->w_refcount > 0) { 1393 if (parent->w_file != NULL) 1394 prnt(" -- last acquired @ %s:%d", parent->w_file, 1395 parent->w_line); 1396 } 1397 prnt("\n"); 1398 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1399 for (i = 0; i < wcl->wcl_count; i++) 1400 witness_displaydescendants(prnt, 1401 wcl->wcl_children[i], indent + 1); 1402} 1403 1404#ifdef BLESSING 1405static int 1406blessed(struct witness *w1, struct witness *w2) 1407{ 1408 int i; 1409 struct witness_blessed *b; 1410 1411 for (i = 0; i < blessed_count; i++) { 1412 b = &blessed_list[i]; 1413 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1414 if (strcmp(w2->w_name, b->b_lock2) == 0) 1415 return (1); 1416 continue; 1417 } 1418 if (strcmp(w1->w_name, b->b_lock2) == 0) 1419 if (strcmp(w2->w_name, b->b_lock1) == 0) 1420 return (1); 1421 } 1422 return (0); 1423} 1424#endif 1425 1426static struct witness * 1427witness_get(void) 1428{ 1429 struct witness *w; 1430 1431 if (witness_dead) { 1432 mtx_unlock_spin(&w_mtx); 1433 return (NULL); 1434 } 1435 if (STAILQ_EMPTY(&w_free)) { 1436 witness_dead = 1; 1437 mtx_unlock_spin(&w_mtx); 1438 printf("%s: witness exhausted\n", __func__); 1439 return (NULL); 1440 } 1441 w = STAILQ_FIRST(&w_free); 1442 STAILQ_REMOVE_HEAD(&w_free, w_list); 1443 bzero(w, sizeof(*w)); 1444 return (w); 1445} 1446 1447static void 1448witness_free(struct witness *w) 1449{ 1450 1451 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1452} 1453 1454static struct witness_child_list_entry * 1455witness_child_get(void) 1456{ 1457 struct witness_child_list_entry *wcl; 1458 1459 if (witness_dead) { 1460 mtx_unlock_spin(&w_mtx); 1461 return (NULL); 1462 } 1463 wcl = w_child_free; 1464 if (wcl == NULL) { 1465 witness_dead = 1; 1466 mtx_unlock_spin(&w_mtx); 1467 printf("%s: witness exhausted\n", __func__); 1468 return (NULL); 1469 } 1470 w_child_free = wcl->wcl_next; 1471 bzero(wcl, sizeof(*wcl)); 1472 return (wcl); 1473} 1474 1475static void 1476witness_child_free(struct witness_child_list_entry *wcl) 1477{ 1478 1479 wcl->wcl_next = w_child_free; 1480 w_child_free = wcl; 1481} 1482 1483static struct lock_list_entry * 1484witness_lock_list_get(void) 1485{ 1486 struct lock_list_entry *lle; 1487 1488 if (witness_dead) 1489 return (NULL); 1490 mtx_lock_spin(&w_mtx); 1491 lle = w_lock_list_free; 1492 if (lle == NULL) { 1493 witness_dead = 1; 1494 mtx_unlock_spin(&w_mtx); 1495 printf("%s: witness exhausted\n", __func__); 1496 return (NULL); 1497 } 1498 w_lock_list_free = lle->ll_next; 1499 mtx_unlock_spin(&w_mtx); 1500 bzero(lle, sizeof(*lle)); 1501 return (lle); 1502} 1503 1504static void 1505witness_lock_list_free(struct lock_list_entry *lle) 1506{ 1507 1508 mtx_lock_spin(&w_mtx); 1509 lle->ll_next = w_lock_list_free; 1510 w_lock_list_free = lle; 1511 mtx_unlock_spin(&w_mtx); 1512} 1513 1514static struct lock_instance * 1515find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1516{ 1517 struct lock_list_entry *lle; 1518 struct lock_instance *instance; 1519 int i; 1520 1521 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1522 for (i = lle->ll_count - 1; i >= 0; i--) { 1523 instance = &lle->ll_children[i]; 1524 if (instance->li_lock == lock) 1525 return (instance); 1526 } 1527 return (NULL); 1528} 1529 1530static void 1531witness_list_lock(struct lock_instance *instance) 1532{ 1533 struct lock_object *lock; 1534 1535 lock = instance->li_lock; 1536 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1537 "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name); 1538 if (lock->lo_type != lock->lo_name) 1539 printf(" (%s)", lock->lo_type); 1540 printf(" r = %d (%p) locked @ %s:%d\n", 1541 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1542 instance->li_line); 1543} 1544 1545int 1546witness_list_locks(struct lock_list_entry **lock_list) 1547{ 1548 struct lock_list_entry *lle; 1549 int i, nheld; 1550 1551 nheld = 0; 1552 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1553 for (i = lle->ll_count - 1; i >= 0; i--) { 1554 witness_list_lock(&lle->ll_children[i]); 1555 nheld++; 1556 } 1557 return (nheld); 1558} 1559 1560void 1561witness_save(struct lock_object *lock, const char **filep, int *linep) 1562{ 1563 struct lock_instance *instance; 1564 1565 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1566 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1567 return; 1568 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1569 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1570 lock->lo_class->lc_name, lock->lo_name); 1571 instance = find_instance(curthread->td_sleeplocks, lock); 1572 if (instance == NULL) 1573 panic("%s: lock (%s) %s not locked", __func__, 1574 lock->lo_class->lc_name, lock->lo_name); 1575 *filep = instance->li_file; 1576 *linep = instance->li_line; 1577} 1578 1579void 1580witness_restore(struct lock_object *lock, const char *file, int line) 1581{ 1582 struct lock_instance *instance; 1583 1584 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1585 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1586 return; 1587 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1588 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1589 lock->lo_class->lc_name, lock->lo_name); 1590 instance = find_instance(curthread->td_sleeplocks, lock); 1591 if (instance == NULL) 1592 panic("%s: lock (%s) %s not locked", __func__, 1593 lock->lo_class->lc_name, lock->lo_name); 1594 lock->lo_witness->w_file = file; 1595 lock->lo_witness->w_line = line; 1596 instance->li_file = file; 1597 instance->li_line = line; 1598} 1599 1600void 1601witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1602{ 1603#ifdef INVARIANT_SUPPORT 1604 struct lock_instance *instance; 1605 1606 if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 1607 return; 1608 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 1609 instance = find_instance(curthread->td_sleeplocks, lock); 1610 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 1611 instance = find_instance(PCPU_GET(spinlocks), lock); 1612 else { 1613 panic("Lock (%s) %s is not sleep or spin!", 1614 lock->lo_class->lc_name, lock->lo_name); 1615 return; 1616 } 1617 file = fixup_filename(file); 1618 switch (flags) { 1619 case LA_UNLOCKED: 1620 if (instance != NULL) 1621 panic("Lock (%s) %s locked @ %s:%d.", 1622 lock->lo_class->lc_name, lock->lo_name, file, line); 1623 break; 1624 case LA_LOCKED: 1625 case LA_LOCKED | LA_RECURSED: 1626 case LA_LOCKED | LA_NOTRECURSED: 1627 case LA_SLOCKED: 1628 case LA_SLOCKED | LA_RECURSED: 1629 case LA_SLOCKED | LA_NOTRECURSED: 1630 case LA_XLOCKED: 1631 case LA_XLOCKED | LA_RECURSED: 1632 case LA_XLOCKED | LA_NOTRECURSED: 1633 if (instance == NULL) { 1634 panic("Lock (%s) %s not locked @ %s:%d.", 1635 lock->lo_class->lc_name, lock->lo_name, file, line); 1636 break; 1637 } 1638 if ((flags & LA_XLOCKED) != 0 && 1639 (instance->li_flags & LI_EXCLUSIVE) == 0) 1640 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1641 lock->lo_class->lc_name, lock->lo_name, file, line); 1642 if ((flags & LA_SLOCKED) != 0 && 1643 (instance->li_flags & LI_EXCLUSIVE) != 0) 1644 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1645 lock->lo_class->lc_name, lock->lo_name, file, line); 1646 if ((flags & LA_RECURSED) != 0 && 1647 (instance->li_flags & LI_RECURSEMASK) == 0) 1648 panic("Lock (%s) %s not recursed @ %s:%d.", 1649 lock->lo_class->lc_name, lock->lo_name, file, line); 1650 if ((flags & LA_NOTRECURSED) != 0 && 1651 (instance->li_flags & LI_RECURSEMASK) != 0) 1652 panic("Lock (%s) %s recursed @ %s:%d.", 1653 lock->lo_class->lc_name, lock->lo_name, file, line); 1654 break; 1655 default: 1656 panic("Invalid lock assertion at %s:%d.", file, line); 1657 1658 } 1659#endif /* INVARIANT_SUPPORT */ 1660} 1661 1662#ifdef DDB 1663static void 1664witness_list(struct thread *td) 1665{ 1666 1667 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1668 KASSERT(db_active, ("%s: not in the debugger", __func__)); 1669 1670 if (witness_dead) 1671 return; 1672 1673 witness_list_locks(&td->td_sleeplocks); 1674 1675 /* 1676 * We only handle spinlocks if td == curthread. This is somewhat broken 1677 * if td is currently executing on some other CPU and holds spin locks 1678 * as we won't display those locks. If we had a MI way of getting 1679 * the per-cpu data for a given cpu then we could use 1680 * td->td_kse->ke_oncpu to get the list of spinlocks for this thread 1681 * and "fix" this. 1682 * 1683 * That still wouldn't really fix this unless we locked sched_lock 1684 * or stopped the other CPU to make sure it wasn't changing the list 1685 * out from under us. It is probably best to just not try to handle 1686 * threads on other CPU's for now. 1687 */ 1688 if (td == curthread && PCPU_GET(spinlocks) != NULL) 1689 witness_list_locks(PCPU_PTR(spinlocks)); 1690} 1691 1692DB_SHOW_COMMAND(locks, db_witness_list) 1693{ 1694 struct thread *td; 1695 pid_t pid; 1696 struct proc *p; 1697 1698 if (have_addr) { 1699 pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 1700 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 1701 ((addr >> 16) % 16) * 10000; 1702 /* sx_slock(&allproc_lock); */ 1703 FOREACH_PROC_IN_SYSTEM(p) { 1704 if (p->p_pid == pid) 1705 break; 1706 } 1707 /* sx_sunlock(&allproc_lock); */ 1708 if (p == NULL) { 1709 db_printf("pid %d not found\n", pid); 1710 return; 1711 } 1712 FOREACH_THREAD_IN_PROC(p, td) { 1713 witness_list(td); 1714 } 1715 } else { 1716 td = curthread; 1717 witness_list(td); 1718 } 1719} 1720 1721DB_SHOW_COMMAND(witness, db_witness_display) 1722{ 1723 1724 witness_display(db_printf); 1725} 1726#endif 1727