subr_witness.c revision 122849
165557Sjasone/*- 265557Sjasone * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 365557Sjasone * 465557Sjasone * Redistribution and use in source and binary forms, with or without 565557Sjasone * modification, are permitted provided that the following conditions 665557Sjasone * are met: 765557Sjasone * 1. Redistributions of source code must retain the above copyright 865557Sjasone * notice, this list of conditions and the following disclaimer. 965557Sjasone * 2. Redistributions in binary form must reproduce the above copyright 1065557Sjasone * notice, this list of conditions and the following disclaimer in the 1165557Sjasone * documentation and/or other materials provided with the distribution. 1265557Sjasone * 3. Berkeley Software Design Inc's name may not be used to endorse or 1365557Sjasone * promote products derived from this software without specific prior 1465557Sjasone * written permission. 1565557Sjasone * 1665557Sjasone * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 1765557Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1865557Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1965557Sjasone * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 2065557Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2165557Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2265557Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2365557Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2465557Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2565557Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2665557Sjasone * SUCH DAMAGE. 2765557Sjasone * 2865557Sjasone * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 2967352Sjhb * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 3065557Sjasone */ 3165557Sjasone 3265557Sjasone/* 3374912Sjhb * Implementation of the `witness' lock verifier. Originally implemented for 3474912Sjhb * mutexes in BSD/OS. Extended to handle generic lock objects and lock 3574912Sjhb * classes in FreeBSD. 3672200Sbmilekic */ 3772200Sbmilekic 3872200Sbmilekic/* 3965557Sjasone * Main Entry: witness 4065557Sjasone * Pronunciation: 'wit-n&s 4165557Sjasone * Function: noun 4265557Sjasone * Etymology: Middle English witnesse, from Old English witnes knowledge, 4365557Sjasone * testimony, witness, from 2wit 4465557Sjasone * Date: before 12th century 4565557Sjasone * 1 : attestation of a fact or event : TESTIMONY 4665557Sjasone * 2 : one that gives evidence; specifically : one who testifies in 4765557Sjasone * a cause or before a judicial tribunal 4865557Sjasone * 3 : one asked to be present at a transaction so as to be able to 4965557Sjasone * testify to its having taken place 5065557Sjasone * 4 : one who has personal knowledge of something 5165557Sjasone * 5 a : something serving as evidence or proof : SIGN 5265557Sjasone * b : public affirmation by word or example of usually 5365557Sjasone * religious faith or conviction <the heroic witness to divine 5465557Sjasone * life -- Pilot> 5565557Sjasone * 6 capitalized : a member of the Jehovah's Witnesses 5665557Sjasone */ 5765557Sjasone 58111881Sjhb/* 59111881Sjhb * Special rules concerning Giant and lock orders: 60111881Sjhb * 61111881Sjhb * 1) Giant must be acquired before any other mutexes. Stated another way, 62111881Sjhb * no other mutex may be held when Giant is acquired. 63111881Sjhb * 64111881Sjhb * 2) Giant must be released when blocking on a sleepable lock. 65111881Sjhb * 66111881Sjhb * This rule is less obvious, but is a result of Giant providing the same 67111881Sjhb * semantics as spl(). Basically, when a thread sleeps, it must release 68111881Sjhb * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule 69111881Sjhb * 2). 70111881Sjhb * 71111881Sjhb * 3) Giant may be acquired before or after sleepable locks. 72111881Sjhb * 73111881Sjhb * This rule is also not quite as obvious. Giant may be acquired after 74111881Sjhb * a sleepable lock because it is a non-sleepable lock and non-sleepable 75111881Sjhb * locks may always be acquired while holding a sleepable lock. The second 76111881Sjhb * case, Giant before a sleepable lock, follows from rule 2) above. Suppose 77111881Sjhb * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1 78111881Sjhb * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and 79111881Sjhb * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to 80111881Sjhb * execute. Thus, acquiring Giant both before and after a sleepable lock 81111881Sjhb * will not result in a lock order reversal. 82111881Sjhb */ 83111881Sjhb 84116182Sobrien#include <sys/cdefs.h> 85116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_witness.c 122849 2003-11-17 08:58:16Z peter $"); 86116182Sobrien 8768790Sjhb#include "opt_ddb.h" 8867676Sjhb#include "opt_witness.h" 8967676Sjhb 9065557Sjasone#include <sys/param.h> 9167352Sjhb#include <sys/bus.h> 9267352Sjhb#include <sys/kernel.h> 9374912Sjhb#include <sys/ktr.h> 9474912Sjhb#include <sys/lock.h> 9567352Sjhb#include <sys/malloc.h> 9674912Sjhb#include <sys/mutex.h> 9765557Sjasone#include <sys/proc.h> 9867676Sjhb#include <sys/sysctl.h> 9965557Sjasone#include <sys/systm.h> 10065557Sjasone 10168790Sjhb#include <ddb/ddb.h> 10268790Sjhb 103111881Sjhb#include <machine/stdarg.h> 104111881Sjhb 105105508Sphk/* Define this to check for blessed mutexes */ 106105508Sphk#undef BLESSING 107105508Sphk 10874912Sjhb#define WITNESS_COUNT 200 10974912Sjhb#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 11065557Sjasone/* 11183798Sjhb * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 11274912Sjhb * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 11374912Sjhb * probably be safe for the most part, but it's still a SWAG. 11467352Sjhb */ 11574912Sjhb#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 11671352Sjasone 11774912Sjhb#define WITNESS_NCHILDREN 6 11871352Sjasone 11974912Sjhbstruct witness_child_list_entry; 12071352Sjasone 12174912Sjhbstruct witness { 12274912Sjhb const char *w_name; 12374912Sjhb struct lock_class *w_class; 12474912Sjhb STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 12574912Sjhb STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 12674912Sjhb struct witness_child_list_entry *w_children; /* Great evilness... */ 12774912Sjhb const char *w_file; 12874912Sjhb int w_line; 12974912Sjhb u_int w_level; 13074912Sjhb u_int w_refcount; 13174912Sjhb u_char w_Giant_squawked:1; 13274912Sjhb u_char w_other_squawked:1; 13374912Sjhb u_char w_same_squawked:1; 134112118Sjhb u_char w_displayed:1; 13574912Sjhb}; 13671352Sjasone 13774912Sjhbstruct witness_child_list_entry { 13874912Sjhb struct witness_child_list_entry *wcl_next; 13974912Sjhb struct witness *wcl_children[WITNESS_NCHILDREN]; 14074912Sjhb u_int wcl_count; 14174912Sjhb}; 14271352Sjasone 14374912SjhbSTAILQ_HEAD(witness_list, witness); 14471352Sjasone 145105508Sphk#ifdef BLESSING 14674912Sjhbstruct witness_blessed { 14774912Sjhb const char *b_lock1; 14874912Sjhb const char *b_lock2; 14974912Sjhb}; 150105508Sphk#endif 15171352Sjasone 15274912Sjhbstruct witness_order_list_entry { 15374912Sjhb const char *w_name; 15474912Sjhb struct lock_class *w_class; 15574912Sjhb}; 15671352Sjasone 157112117Sjhb#ifdef BLESSING 158112117Sjhbstatic int blessed(struct witness *, struct witness *); 159112117Sjhb#endif 160112117Sjhbstatic int depart(struct witness *w); 16174912Sjhbstatic struct witness *enroll(const char *description, 16274912Sjhb struct lock_class *lock_class); 163112117Sjhbstatic int insertchild(struct witness *parent, struct witness *child); 164112117Sjhbstatic int isitmychild(struct witness *parent, struct witness *child); 165112117Sjhbstatic int isitmydescendant(struct witness *parent, struct witness *child); 16674912Sjhbstatic int itismychild(struct witness *parent, struct witness *child); 167112117Sjhbstatic int rebalancetree(struct witness_list *list); 16874912Sjhbstatic void removechild(struct witness *parent, struct witness *child); 169112117Sjhbstatic int reparentchildren(struct witness *newparent, 170112117Sjhb struct witness *oldparent); 171112562Sjhbstatic int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 17274912Sjhbstatic void witness_displaydescendants(void(*)(const char *fmt, ...), 173112118Sjhb struct witness *, int indent); 174112116Sjhbstatic const char *fixup_filename(const char *file); 17574912Sjhbstatic void witness_leveldescendents(struct witness *parent, int level); 17674912Sjhbstatic void witness_levelall(void); 17774912Sjhbstatic struct witness *witness_get(void); 17874912Sjhbstatic void witness_free(struct witness *m); 17974912Sjhbstatic struct witness_child_list_entry *witness_child_get(void); 18074912Sjhbstatic void witness_child_free(struct witness_child_list_entry *wcl); 18174912Sjhbstatic struct lock_list_entry *witness_lock_list_get(void); 18274912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 18376272Sjhbstatic struct lock_instance *find_instance(struct lock_list_entry *lock_list, 18476272Sjhb struct lock_object *lock); 185111881Sjhbstatic void witness_list_lock(struct lock_instance *instance); 186112115Sjhb#ifdef DDB 187112061Sjhbstatic void witness_list(struct thread *td); 188100011Smpstatic void witness_display_list(void(*prnt)(const char *fmt, ...), 189100011Smp struct witness_list *list); 190100011Smpstatic void witness_display(void(*)(const char *fmt, ...)); 191100011Smp#endif 19272200Sbmilekic 19374912SjhbMALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 19472200Sbmilekic 195112562Sjhb/* 196112562Sjhb * If set to 0, witness is disabled. If set to 1, witness performs full lock 197112562Sjhb * order checking for all locks. If set to 2 or higher, then witness skips 198112562Sjhb * the full lock order check if the lock being acquired is at a higher level 199112562Sjhb * (i.e. farther down in the tree) than the current lock. This last mode is 200112562Sjhb * somewhat experimental and not considered fully safe. At runtime, this 201112562Sjhb * value may be set to 0 to turn off witness. witness is not allowed be 202112562Sjhb * turned on once it is turned off, however. 203112562Sjhb */ 20477843Speterstatic int witness_watch = 1; 20577900SpeterTUNABLE_INT("debug.witness_watch", &witness_watch); 206112562SjhbSYSCTL_PROC(_debug, OID_AUTO, witness_watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 207112562Sjhb sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 20871352Sjasone 20967352Sjhb#ifdef DDB 21072200Sbmilekic/* 21167676Sjhb * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 21265557Sjasone * drop into kdebug() when: 21365557Sjasone * - a lock heirarchy violation occurs 21465557Sjasone * - locks are held when going to sleep. 21565557Sjasone */ 21667676Sjhb#ifdef WITNESS_DDB 21777843Speterint witness_ddb = 1; 21867676Sjhb#else 21977843Speterint witness_ddb = 0; 22065557Sjasone#endif 22177900SpeterTUNABLE_INT("debug.witness_ddb", &witness_ddb); 22267676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 223110779Speter 224110779Speter/* 225110779Speter * When DDB is enabled and witness_trace is set to 1, it will cause the system 226110779Speter * to print a stack trace: 227110779Speter * - a lock heirarchy violation occurs 228110779Speter * - locks are held when going to sleep. 229110779Speter */ 230110779Speterint witness_trace = 1; 231110779SpeterTUNABLE_INT("debug.witness_trace", &witness_trace); 232110779SpeterSYSCTL_INT(_debug, OID_AUTO, witness_trace, CTLFLAG_RW, &witness_trace, 0, ""); 23367676Sjhb#endif /* DDB */ 23465557Sjasone 23567676Sjhb#ifdef WITNESS_SKIPSPIN 23677843Speterint witness_skipspin = 1; 23767676Sjhb#else 23877843Speterint witness_skipspin = 0; 23965557Sjasone#endif 24077900SpeterTUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 241121307SsilbySYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RDTUN, &witness_skipspin, 0, 24267676Sjhb ""); 24365557Sjasone 24474912Sjhbstatic struct mtx w_mtx; 24574912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 24674912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 24774912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 24874912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 24974912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL; 25074912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 25165557Sjasone 25274912Sjhbstatic struct witness w_data[WITNESS_COUNT]; 25374912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 25474912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 25565557Sjasone 25674912Sjhbstatic struct witness_order_list_entry order_lists[] = { 25774912Sjhb { "proctree", &lock_class_sx }, 25874912Sjhb { "allproc", &lock_class_sx }, 259111951Sjhb { "Giant", &lock_class_mtx_sleep }, 260108184Skris { "filedesc structure", &lock_class_mtx_sleep }, 261108184Skris { "pipe mutex", &lock_class_mtx_sleep }, 26296122Salfred { "sigio lock", &lock_class_mtx_sleep }, 26391140Stanimura { "process group", &lock_class_mtx_sleep }, 26474912Sjhb { "process lock", &lock_class_mtx_sleep }, 26591140Stanimura { "session", &lock_class_mtx_sleep }, 26674912Sjhb { "uidinfo hash", &lock_class_mtx_sleep }, 26774912Sjhb { "uidinfo struct", &lock_class_mtx_sleep }, 268113275Smike { "allprison", &lock_class_mtx_sleep }, 26974912Sjhb { NULL, NULL }, 27075464Sjhb /* 27175464Sjhb * spin locks 27275464Sjhb */ 27384331Sjhb#ifdef SMP 27484331Sjhb { "ap boot", &lock_class_mtx_spin }, 27572224Sjhb#endif 27674912Sjhb { "sio", &lock_class_mtx_spin }, 27772224Sjhb#ifdef __i386__ 27874912Sjhb { "cy", &lock_class_mtx_spin }, 27972224Sjhb#endif 280103091Sjake { "sabtty", &lock_class_mtx_spin }, 281109015Sjake { "zstty", &lock_class_mtx_spin }, 28274912Sjhb { "ng_node", &lock_class_mtx_spin }, 28374912Sjhb { "ng_worklist", &lock_class_mtx_spin }, 284119813Ssam { "taskqueue_fast", &lock_class_mtx_spin }, 285122001Sjhb { "intr table", &lock_class_mtx_spin }, 28674912Sjhb { "ithread table lock", &lock_class_mtx_spin }, 28774912Sjhb { "sched lock", &lock_class_mtx_spin }, 288122514Sjhb { "turnstile chain", &lock_class_mtx_spin }, 289122514Sjhb { "td_contested", &lock_class_mtx_spin }, 29074912Sjhb { "callout", &lock_class_mtx_spin }, 29165557Sjasone /* 29265557Sjasone * leaf locks 29365557Sjasone */ 29490278Sjhb { "allpmaps", &lock_class_mtx_spin }, 29599416Salc { "vm page queue free mutex", &lock_class_mtx_spin }, 29688322Sjhb { "icu", &lock_class_mtx_spin }, 29772224Sjhb#ifdef SMP 29874912Sjhb { "smp rendezvous", &lock_class_mtx_spin }, 299122849Speter#if defined(__i386__) || defined(__amd64__) 30099862Speter { "tlb", &lock_class_mtx_spin }, 301112993Speter { "lazypmap", &lock_class_mtx_spin }, 302112993Speter#endif 303108187Sjake#ifdef __sparc64__ 304108187Sjake { "ipi", &lock_class_mtx_spin }, 30599862Speter#endif 306108187Sjake#endif 30778785Sjhb { "clk", &lock_class_mtx_spin }, 30895473Sdes { "mutex profiling lock", &lock_class_mtx_spin }, 309111028Sjeff { "kse zombie lock", &lock_class_mtx_spin }, 310103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 311104951Speter#ifdef __ia64__ 312104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 313104951Speter#endif 314115425Speter#if defined(__i386__) || defined(__amd64__) 315111068Speter { "pcicfg", &lock_class_mtx_spin }, 316111068Speter#endif 31774912Sjhb { NULL, NULL }, 31874912Sjhb { NULL, NULL } 31965557Sjasone}; 32065557Sjasone 321105508Sphk#ifdef BLESSING 32265557Sjasone/* 32365557Sjasone * Pairs of locks which have been blessed 32465557Sjasone * Don't complain about order problems with blessed locks 32565557Sjasone */ 32665856Sjhbstatic struct witness_blessed blessed_list[] = { 32765557Sjasone}; 32872200Sbmilekicstatic int blessed_count = 32972200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 330105508Sphk#endif 33165557Sjasone 33274912Sjhb/* 33374912Sjhb * List of all locks in the system. 33474912Sjhb */ 33597963SjhbTAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 33674912Sjhb 33774912Sjhbstatic struct mtx all_mtx = { 33874912Sjhb { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 33974912Sjhb "All locks list", /* mtx_object.lo_name */ 34093811Sjhb "All locks list", /* mtx_object.lo_type */ 34174912Sjhb LO_INITIALIZED, /* mtx_object.lo_flags */ 34297963Sjhb { NULL, NULL }, /* mtx_object.lo_list */ 34374912Sjhb NULL }, /* mtx_object.lo_witness */ 344122514Sjhb MTX_UNOWNED, 0 /* mtx_lock, mtx_recurse */ 34574912Sjhb}; 34674912Sjhb 34774912Sjhb/* 34874912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 34974912Sjhb */ 35074912Sjhbstatic int witness_cold = 1; 35174912Sjhb 35274912Sjhb/* 35374912Sjhb * Global variables for book keeping. 35474912Sjhb */ 35574912Sjhbstatic int lock_cur_cnt; 35674912Sjhbstatic int lock_max_cnt; 35774912Sjhb 35874912Sjhb/* 35974912Sjhb * The WITNESS-enabled diagnostic code. 36074912Sjhb */ 36171352Sjasonestatic void 36274912Sjhbwitness_initialize(void *dummy __unused) 36365557Sjasone{ 36474912Sjhb struct lock_object *lock; 36574912Sjhb struct witness_order_list_entry *order; 36674912Sjhb struct witness *w, *w1; 36774912Sjhb int i; 36865557Sjasone 36974912Sjhb /* 37074912Sjhb * We have to release Giant before initializing its witness 37174912Sjhb * structure so that WITNESS doesn't get confused. 37274912Sjhb */ 37374912Sjhb mtx_unlock(&Giant); 37474912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 37574912Sjhb 37687593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 37797963Sjhb TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 37893811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 37993811Sjhb MTX_NOWITNESS); 38074912Sjhb for (i = 0; i < WITNESS_COUNT; i++) 38174912Sjhb witness_free(&w_data[i]); 38274912Sjhb for (i = 0; i < WITNESS_CHILDCOUNT; i++) 38374912Sjhb witness_child_free(&w_childdata[i]); 38474912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 38574912Sjhb witness_lock_list_free(&w_locklistdata[i]); 38674912Sjhb 38774912Sjhb /* First add in all the specified order lists. */ 38874912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 38974912Sjhb w = enroll(order->w_name, order->w_class); 39075569Sjhb if (w == NULL) 39175569Sjhb continue; 39274912Sjhb w->w_file = "order list"; 39374912Sjhb for (order++; order->w_name != NULL; order++) { 39474912Sjhb w1 = enroll(order->w_name, order->w_class); 39575569Sjhb if (w1 == NULL) 39675569Sjhb continue; 39774912Sjhb w1->w_file = "order list"; 398112117Sjhb if (!itismychild(w, w1)) 399112117Sjhb panic("Not enough memory for static orders!"); 40074912Sjhb w = w1; 40165557Sjasone } 40265557Sjasone } 40365557Sjasone 40474912Sjhb /* Iterate through all locks and add them to witness. */ 40574912Sjhb mtx_lock(&all_mtx); 40697963Sjhb TAILQ_FOREACH(lock, &all_locks, lo_list) { 40774912Sjhb if (lock->lo_flags & LO_WITNESS) 40893811Sjhb lock->lo_witness = enroll(lock->lo_type, 40974912Sjhb lock->lo_class); 41074912Sjhb else 41174912Sjhb lock->lo_witness = NULL; 41274912Sjhb } 41374912Sjhb mtx_unlock(&all_mtx); 41474912Sjhb 41574912Sjhb /* Mark the witness code as being ready for use. */ 41674912Sjhb atomic_store_rel_int(&witness_cold, 0); 41774912Sjhb 41874912Sjhb mtx_lock(&Giant); 41965557Sjasone} 42074912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 42165557Sjasone 422112562Sjhbstatic int 423112562Sjhbsysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 424112562Sjhb{ 425112562Sjhb int error, value; 426112562Sjhb 427112562Sjhb value = witness_watch; 428112562Sjhb error = sysctl_handle_int(oidp, &value, 0, req); 429112562Sjhb if (error != 0 || req->newptr == NULL) 430112562Sjhb return (error); 431112562Sjhb error = suser(req->td); 432112562Sjhb if (error != 0) 433112562Sjhb return (error); 434112562Sjhb if (value == witness_watch) 435112562Sjhb return (0); 436112562Sjhb if (value != 0) 437112562Sjhb return (EINVAL); 438112562Sjhb witness_watch = 0; 439112562Sjhb return (0); 440112562Sjhb} 441112562Sjhb 44274912Sjhbvoid 44374912Sjhbwitness_init(struct lock_object *lock) 44474912Sjhb{ 44574912Sjhb struct lock_class *class; 44674912Sjhb 44774912Sjhb class = lock->lo_class; 44874912Sjhb if (lock->lo_flags & LO_INITIALIZED) 44982284Sjhb panic("%s: lock (%s) %s is already initialized", __func__, 45074912Sjhb class->lc_name, lock->lo_name); 45174912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 45274912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 45382284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 45474912Sjhb class->lc_name, lock->lo_name); 45574912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 45674912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 45782284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 45874912Sjhb class->lc_name, lock->lo_name); 45982244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 46082244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 46182284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 46282244Sjhb class->lc_name, lock->lo_name); 46382244Sjhb 46474912Sjhb mtx_lock(&all_mtx); 46597963Sjhb TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 46674912Sjhb lock->lo_flags |= LO_INITIALIZED; 46774912Sjhb lock_cur_cnt++; 46874912Sjhb if (lock_cur_cnt > lock_max_cnt) 46974912Sjhb lock_max_cnt = lock_cur_cnt; 47074912Sjhb mtx_unlock(&all_mtx); 471112562Sjhb if (!witness_cold && witness_watch != 0 && panicstr == NULL && 47274912Sjhb (lock->lo_flags & LO_WITNESS) != 0) 47393811Sjhb lock->lo_witness = enroll(lock->lo_type, class); 47474912Sjhb else 47574912Sjhb lock->lo_witness = NULL; 47674912Sjhb} 47774912Sjhb 47874912Sjhbvoid 47974912Sjhbwitness_destroy(struct lock_object *lock) 48074912Sjhb{ 48175362Sjhb struct witness *w; 48274912Sjhb 48374912Sjhb if (witness_cold) 48474912Sjhb panic("lock (%s) %s destroyed while witness_cold", 48574912Sjhb lock->lo_class->lc_name, lock->lo_name); 48674912Sjhb if ((lock->lo_flags & LO_INITIALIZED) == 0) 48782284Sjhb panic("%s: lock (%s) %s is not initialized", __func__, 48874912Sjhb lock->lo_class->lc_name, lock->lo_name); 48974912Sjhb 49076272Sjhb /* XXX: need to verify that no one holds the lock */ 49175362Sjhb w = lock->lo_witness; 49275362Sjhb if (w != NULL) { 49375362Sjhb mtx_lock_spin(&w_mtx); 49497948Sjhb MPASS(w->w_refcount > 0); 49575362Sjhb w->w_refcount--; 496112117Sjhb 497112117Sjhb /* 498112117Sjhb * Lock is already released if we have an allocation failure 499112117Sjhb * and depart() fails. 500112117Sjhb */ 501112117Sjhb if (w->w_refcount != 0 || depart(w)) 502112117Sjhb mtx_unlock_spin(&w_mtx); 50375362Sjhb } 50475362Sjhb 50574912Sjhb mtx_lock(&all_mtx); 50674912Sjhb lock_cur_cnt--; 50797963Sjhb TAILQ_REMOVE(&all_locks, lock, lo_list); 50880055Sjhb lock->lo_flags &= ~LO_INITIALIZED; 50974912Sjhb mtx_unlock(&all_mtx); 51074912Sjhb} 51174912Sjhb 512112115Sjhb#ifdef DDB 51371352Sjasonestatic void 51474912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...), 51574912Sjhb struct witness_list *list) 51671352Sjasone{ 517112118Sjhb struct witness *w; 51871352Sjasone 51974912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 520112118Sjhb if (w->w_file == NULL || w->w_level > 0) 52171352Sjasone continue; 52271352Sjasone /* 52371352Sjasone * This lock has no anscestors, display its descendants. 52471352Sjasone */ 525112118Sjhb witness_displaydescendants(prnt, w, 0); 52671352Sjasone } 52774912Sjhb} 52872224Sjhb 52974912Sjhbstatic void 53074912Sjhbwitness_display(void(*prnt)(const char *fmt, ...)) 53174912Sjhb{ 53274912Sjhb struct witness *w; 53374912Sjhb 53482284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 53574912Sjhb witness_levelall(); 53674912Sjhb 537112118Sjhb /* Clear all the displayed flags. */ 538112118Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 539112118Sjhb w->w_displayed = 0; 540112118Sjhb } 541112118Sjhb 54272224Sjhb /* 54374930Sjhb * First, handle sleep locks which have been acquired at least 54474912Sjhb * once. 54574912Sjhb */ 54674912Sjhb prnt("Sleep locks:\n"); 54774912Sjhb witness_display_list(prnt, &w_sleep); 54874912Sjhb 54974912Sjhb /* 55074930Sjhb * Now do spin locks which have been acquired at least once. 55172224Sjhb */ 55274912Sjhb prnt("\nSpin locks:\n"); 55374912Sjhb witness_display_list(prnt, &w_spin); 55472224Sjhb 55572224Sjhb /* 55674930Sjhb * Finally, any locks which have not been acquired yet. 55772224Sjhb */ 55874912Sjhb prnt("\nLocks which were never acquired:\n"); 55974912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 56097948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 56171352Sjasone continue; 56274912Sjhb prnt("%s\n", w->w_name); 56371352Sjasone } 56471352Sjasone} 565112115Sjhb#endif /* DDB */ 56671352Sjasone 567112116Sjhb/* Trim useless garbage from filenames. */ 568112116Sjhbstatic const char * 569112116Sjhbfixup_filename(const char *file) 570112116Sjhb{ 571112116Sjhb 572112116Sjhb if (file == NULL) 573112116Sjhb return (NULL); 574112116Sjhb while (strncmp(file, "../", 3) == 0) 575112116Sjhb file += 3; 576112116Sjhb return (file); 577112116Sjhb} 578112116Sjhb 57965557Sjasonevoid 58074912Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 58165557Sjasone{ 58274912Sjhb struct lock_list_entry **lock_list, *lle; 58376272Sjhb struct lock_instance *lock1, *lock2; 58474912Sjhb struct lock_class *class; 58565856Sjhb struct witness *w, *w1; 58683366Sjulian struct thread *td; 58774912Sjhb int i, j; 58867676Sjhb#ifdef DDB 58967676Sjhb int go_into_ddb = 0; 590112115Sjhb#endif 59165557Sjasone 592112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 59380747Sjhb panicstr != NULL) 59471320Sjasone return; 59574912Sjhb w = lock->lo_witness; 59674912Sjhb class = lock->lo_class; 59783366Sjulian td = curthread; 598112116Sjhb file = fixup_filename(file); 59965557Sjasone 60074912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 60193676Sjhb /* 60293676Sjhb * Since spin locks include a critical section, this check 60393676Sjhb * impliclty enforces a lock order of all sleep locks before 60493676Sjhb * all spin locks. 60593676Sjhb */ 60688899Sjhb if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0) 60774912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 60874912Sjhb class->lc_name, lock->lo_name, file, line); 60983366Sjulian lock_list = &td->td_sleeplocks; 61088899Sjhb } else 61188899Sjhb lock_list = PCPU_PTR(spinlocks); 61265557Sjasone 61376772Sjhb /* 61474912Sjhb * Is this the first lock acquired? If so, then no order checking 61574912Sjhb * is needed. 61665557Sjasone */ 61774912Sjhb if (*lock_list == NULL) 61865557Sjasone goto out; 61965557Sjasone 62074912Sjhb /* 62176272Sjhb * Check to see if we are recursing on a lock we already own. 62276272Sjhb */ 62376272Sjhb lock1 = find_instance(*lock_list, lock); 62476272Sjhb if (lock1 != NULL) { 62576272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 62676272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 62776272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 62876272Sjhb class->lc_name, lock->lo_name, file, line); 62976272Sjhb printf("while exclusively locked from %s:%d\n", 63076272Sjhb lock1->li_file, lock1->li_line); 63176272Sjhb panic("share->excl"); 63276272Sjhb } 63376272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 63476272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 63576272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 63676272Sjhb class->lc_name, lock->lo_name, file, line); 63776272Sjhb printf("while share locked from %s:%d\n", 63876272Sjhb lock1->li_file, lock1->li_line); 63976272Sjhb panic("excl->share"); 64076272Sjhb } 64176272Sjhb lock1->li_flags++; 64276272Sjhb if ((lock->lo_flags & LO_RECURSABLE) == 0) { 64376272Sjhb printf( 64476272Sjhb "recursed on non-recursive lock (%s) %s @ %s:%d\n", 64576272Sjhb class->lc_name, lock->lo_name, file, line); 64676272Sjhb printf("first acquired @ %s:%d\n", lock1->li_file, 64776272Sjhb lock1->li_line); 64876272Sjhb panic("recurse"); 64976272Sjhb } 65087593Sobrien CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 65184680Sjhb td->td_proc->p_pid, lock->lo_name, 65278785Sjhb lock1->li_flags & LI_RECURSEMASK); 65376272Sjhb lock1->li_file = file; 65476272Sjhb lock1->li_line = line; 65576272Sjhb return; 65676272Sjhb } 65776272Sjhb 65876272Sjhb /* 659112112Sjhb * Try locks do not block if they fail to acquire the lock, thus 660112112Sjhb * there is no danger of deadlocks or of switching while holding a 661112112Sjhb * spin lock if we acquire a lock via a try operation. 662112112Sjhb */ 663112112Sjhb if (flags & LOP_TRYLOCK) 664112112Sjhb goto out; 665112112Sjhb 666112112Sjhb /* 66774912Sjhb * Check for duplicate locks of the same type. Note that we only 66874912Sjhb * have to check for this on the last lock we just acquired. Any 66974912Sjhb * other cases will be caught as lock order violations. 67074912Sjhb */ 67176272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 67276272Sjhb w1 = lock1->li_lock->lo_witness; 67374912Sjhb if (w1 == w) { 67493273Sjeff if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK)) 67565557Sjasone goto out; 67665557Sjasone w->w_same_squawked = 1; 67775755Sjhb printf("acquiring duplicate lock of same type: \"%s\"\n", 67893811Sjhb lock->lo_type); 67993811Sjhb printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 68093811Sjhb lock1->li_file, lock1->li_line); 68193811Sjhb printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 68267676Sjhb#ifdef DDB 68367676Sjhb go_into_ddb = 1; 684112115Sjhb#endif 68565557Sjasone goto out; 68665557Sjasone } 68765557Sjasone MPASS(!mtx_owned(&w_mtx)); 68874912Sjhb mtx_lock_spin(&w_mtx); 68965557Sjasone /* 69065557Sjasone * If we have a known higher number just say ok 69165557Sjasone */ 69265557Sjasone if (witness_watch > 1 && w->w_level > w1->w_level) { 69374912Sjhb mtx_unlock_spin(&w_mtx); 69465557Sjasone goto out; 69565557Sjasone } 696111881Sjhb /* 697111881Sjhb * If we know that the the lock we are acquiring comes after 698111881Sjhb * the lock we most recently acquired in the lock order tree, 699111881Sjhb * then there is no need for any further checks. 700111881Sjhb */ 70174912Sjhb if (isitmydescendant(w1, w)) { 70274912Sjhb mtx_unlock_spin(&w_mtx); 70365557Sjasone goto out; 70465557Sjasone } 70574912Sjhb for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 70674912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 70765557Sjasone 70874912Sjhb MPASS(j < WITNESS_COUNT); 70976272Sjhb lock1 = &lle->ll_children[i]; 71076272Sjhb w1 = lock1->li_lock->lo_witness; 71174912Sjhb 71274912Sjhb /* 71374912Sjhb * If this lock doesn't undergo witness checking, 71474912Sjhb * then skip it. 71574912Sjhb */ 71674912Sjhb if (w1 == NULL) { 71776272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 71874912Sjhb ("lock missing witness structure")); 71974912Sjhb continue; 72074912Sjhb } 72176272Sjhb /* 722111881Sjhb * If we are locking Giant and this is a sleepable 72376272Sjhb * lock, then skip it. 72476272Sjhb */ 725111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 72676272Sjhb lock == &Giant.mtx_object) 72776272Sjhb continue; 72893690Sjhb /* 72993690Sjhb * If we are locking a sleepable lock and this lock 730111881Sjhb * is Giant, then skip it. 73193690Sjhb */ 732111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 733111887Sjhb lock1->li_lock == &Giant.mtx_object) 734111881Sjhb continue; 735111881Sjhb /* 736111881Sjhb * If we are locking a sleepable lock and this lock 737111881Sjhb * isn't sleepable, we want to treat it as a lock 738111881Sjhb * order violation to enfore a general lock order of 739111881Sjhb * sleepable locks before non-sleepable locks. 740111881Sjhb */ 74193690Sjhb if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 742111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 743111881Sjhb /* 744111881Sjhb * Check the lock order hierarchy for a reveresal. 745111881Sjhb */ 746111881Sjhb if (!isitmydescendant(w, w1)) 74774912Sjhb continue; 74874912Sjhb /* 74974912Sjhb * We have a lock order violation, check to see if it 75074912Sjhb * is allowed or has already been yelled about. 75174912Sjhb */ 75274912Sjhb mtx_unlock_spin(&w_mtx); 753105508Sphk#ifdef BLESSING 75465557Sjasone if (blessed(w, w1)) 75565557Sjasone goto out; 756105508Sphk#endif 75776272Sjhb if (lock1->li_lock == &Giant.mtx_object) { 75865557Sjasone if (w1->w_Giant_squawked) 75965557Sjasone goto out; 76065557Sjasone else 76165557Sjasone w1->w_Giant_squawked = 1; 76265557Sjasone } else { 76365557Sjasone if (w1->w_other_squawked) 76465557Sjasone goto out; 76565557Sjasone else 76665557Sjasone w1->w_other_squawked = 1; 76765557Sjasone } 76874912Sjhb /* 76974912Sjhb * Ok, yell about it. 77074912Sjhb */ 77165557Sjasone printf("lock order reversal\n"); 77274912Sjhb /* 77374912Sjhb * Try to locate an earlier lock with 77474912Sjhb * witness w in our list. 77574912Sjhb */ 77674912Sjhb do { 77776272Sjhb lock2 = &lle->ll_children[i]; 77876272Sjhb MPASS(lock2->li_lock != NULL); 77976272Sjhb if (lock2->li_lock->lo_witness == w) 78074912Sjhb break; 78174912Sjhb i--; 78274912Sjhb if (i == 0 && lle->ll_next != NULL) { 78374912Sjhb lle = lle->ll_next; 78474912Sjhb i = lle->ll_count - 1; 785106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 78674912Sjhb } 78774912Sjhb } while (i >= 0); 78876272Sjhb if (i < 0) { 78993811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 79093811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 79193811Sjhb lock1->li_lock->lo_type, lock1->li_file, 79276272Sjhb lock1->li_line); 79393811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 79493811Sjhb lock->lo_name, lock->lo_type, file, line); 79576272Sjhb } else { 79693811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 79793811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 79893811Sjhb lock2->li_lock->lo_type, lock2->li_file, 79976272Sjhb lock2->li_line); 80093811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 80193811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 80293811Sjhb lock1->li_lock->lo_type, lock1->li_file, 80376272Sjhb lock1->li_line); 80493811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 80593811Sjhb lock->lo_name, lock->lo_type, file, line); 80676272Sjhb } 80767676Sjhb#ifdef DDB 80867676Sjhb go_into_ddb = 1; 809112115Sjhb#endif 81065557Sjasone goto out; 81165557Sjasone } 81265557Sjasone } 81376272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 81478871Sjhb /* 815111881Sjhb * Don't build a new relationship between a sleepable lock and 816111881Sjhb * Giant if it is the wrong direction. The real lock order is that 817111881Sjhb * sleepable locks come before Giant. 81878871Sjhb */ 819112117Sjhb if (!(lock1->li_lock == &Giant.mtx_object && 820112117Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0)) { 82187593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 82293811Sjhb lock->lo_type, lock1->li_lock->lo_type); 82378871Sjhb if (!itismychild(lock1->li_lock->lo_witness, w)) 824112117Sjhb /* Witness is dead. */ 825112117Sjhb return; 82678871Sjhb } 827112117Sjhb mtx_unlock_spin(&w_mtx); 82865557Sjasone 82965557Sjasoneout: 83067676Sjhb#ifdef DDB 831110779Speter if (go_into_ddb) { 832110779Speter if (witness_trace) 833110779Speter backtrace(); 834110779Speter if (witness_ddb) 835110779Speter Debugger(__func__); 836110779Speter } 837112115Sjhb#endif 83865557Sjasone w->w_file = file; 83965557Sjasone w->w_line = line; 84074912Sjhb 84174912Sjhb lle = *lock_list; 84276272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 84378785Sjhb lle = witness_lock_list_get(); 84478785Sjhb if (lle == NULL) 84565557Sjasone return; 84678785Sjhb lle->ll_next = *lock_list; 84787593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 84884680Sjhb td->td_proc->p_pid, lle); 84978785Sjhb *lock_list = lle; 85065557Sjasone } 85176272Sjhb lock1 = &lle->ll_children[lle->ll_count++]; 85276272Sjhb lock1->li_lock = lock; 85376272Sjhb lock1->li_line = line; 85476272Sjhb lock1->li_file = file; 85576272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 85676272Sjhb lock1->li_flags = LI_EXCLUSIVE; 85776272Sjhb else 85876272Sjhb lock1->li_flags = 0; 85987593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 86084680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 86165557Sjasone} 86265557Sjasone 86365557Sjasonevoid 86482244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 86582244Sjhb{ 86682244Sjhb struct lock_instance *instance; 86782244Sjhb struct lock_class *class; 86882244Sjhb 86982284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 870112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 87182244Sjhb return; 87282244Sjhb class = lock->lo_class; 873112116Sjhb file = fixup_filename(file); 87482244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 87582244Sjhb panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 87682244Sjhb class->lc_name, lock->lo_name, file, line); 87782244Sjhb if ((flags & LOP_TRYLOCK) == 0) 87882244Sjhb panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 87982244Sjhb lock->lo_name, file, line); 88082244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 88182244Sjhb panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 88282244Sjhb class->lc_name, lock->lo_name, file, line); 88383366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 88482244Sjhb if (instance == NULL) 88582244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 88682244Sjhb class->lc_name, lock->lo_name, file, line); 88782244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0) 88882244Sjhb panic("upgrade of exclusive lock (%s) %s @ %s:%d", 88982244Sjhb class->lc_name, lock->lo_name, file, line); 89082244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 89182244Sjhb panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 89282244Sjhb class->lc_name, lock->lo_name, 89382244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 89482244Sjhb instance->li_flags |= LI_EXCLUSIVE; 89582244Sjhb} 89682244Sjhb 89782244Sjhbvoid 89882244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 89982244Sjhb int line) 90082244Sjhb{ 90182244Sjhb struct lock_instance *instance; 90282244Sjhb struct lock_class *class; 90382244Sjhb 90482284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 905112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 90682244Sjhb return; 90782244Sjhb class = lock->lo_class; 908112116Sjhb file = fixup_filename(file); 90982244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 91082244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 91182244Sjhb class->lc_name, lock->lo_name, file, line); 91282244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 91382244Sjhb panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 91482244Sjhb class->lc_name, lock->lo_name, file, line); 91583366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 91682244Sjhb if (instance == NULL) 91782244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 91882244Sjhb class->lc_name, lock->lo_name, file, line); 91982244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0) 92082244Sjhb panic("downgrade of shared lock (%s) %s @ %s:%d", 92182244Sjhb class->lc_name, lock->lo_name, file, line); 92282244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 92382244Sjhb panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 92482244Sjhb class->lc_name, lock->lo_name, 92582244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 92682244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 92782244Sjhb} 92882244Sjhb 92982244Sjhbvoid 93074912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 93165557Sjasone{ 93274912Sjhb struct lock_list_entry **lock_list, *lle; 93376272Sjhb struct lock_instance *instance; 93474912Sjhb struct lock_class *class; 93583366Sjulian struct thread *td; 93692858Simp register_t s; 93774912Sjhb int i, j; 93865557Sjasone 939112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 94080747Sjhb panicstr != NULL) 94171352Sjasone return; 94283366Sjulian td = curthread; 94374912Sjhb class = lock->lo_class; 944112116Sjhb file = fixup_filename(file); 94576272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 94683366Sjulian lock_list = &td->td_sleeplocks; 94776272Sjhb else 94874912Sjhb lock_list = PCPU_PTR(spinlocks); 94974912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 95076272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 95176272Sjhb instance = &(*lock_list)->ll_children[i]; 95276272Sjhb if (instance->li_lock == lock) { 95376272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 95476272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 95576272Sjhb printf( 95676272Sjhb "shared unlock of (%s) %s @ %s:%d\n", 95776272Sjhb class->lc_name, lock->lo_name, 95876272Sjhb file, line); 95976272Sjhb printf( 96076272Sjhb "while exclusively locked from %s:%d\n", 96176272Sjhb instance->li_file, 96276272Sjhb instance->li_line); 96376272Sjhb panic("excl->ushare"); 96476272Sjhb } 96576272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 96676272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 96776272Sjhb printf( 96876272Sjhb "exclusive unlock of (%s) %s @ %s:%d\n", 96976272Sjhb class->lc_name, lock->lo_name, 97076272Sjhb file, line); 97176272Sjhb printf( 97276272Sjhb "while share locked from %s:%d\n", 97376272Sjhb instance->li_file, 97476272Sjhb instance->li_line); 97576272Sjhb panic("share->uexcl"); 97676272Sjhb } 97776272Sjhb /* If we are recursed, unrecurse. */ 97876272Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 97987593Sobrien CTR4(KTR_WITNESS, 98087593Sobrien "%s: pid %d unrecursed on %s r=%d", __func__, 98184680Sjhb td->td_proc->p_pid, 98278785Sjhb instance->li_lock->lo_name, 98378785Sjhb instance->li_flags); 98476272Sjhb instance->li_flags--; 98588900Sjhb return; 98676272Sjhb } 98792858Simp s = intr_disable(); 98887593Sobrien CTR4(KTR_WITNESS, 98987593Sobrien "%s: pid %d removed %s from lle[%d]", __func__, 99084680Sjhb td->td_proc->p_pid, 99184680Sjhb instance->li_lock->lo_name, 99278785Sjhb (*lock_list)->ll_count - 1); 99397014Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 99474912Sjhb (*lock_list)->ll_children[j] = 99574912Sjhb (*lock_list)->ll_children[j + 1]; 99697014Sjhb (*lock_list)->ll_count--; 99792858Simp intr_restore(s); 99874912Sjhb if ((*lock_list)->ll_count == 0) { 99974912Sjhb lle = *lock_list; 100074912Sjhb *lock_list = lle->ll_next; 100187593Sobrien CTR3(KTR_WITNESS, 100287593Sobrien "%s: pid %d removed lle %p", __func__, 100384680Sjhb td->td_proc->p_pid, lle); 100474912Sjhb witness_lock_list_free(lle); 100574912Sjhb } 100688900Sjhb return; 100774912Sjhb } 100876272Sjhb } 100976272Sjhb panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 101076272Sjhb file, line); 101165557Sjasone} 101265557Sjasone 101374912Sjhb/* 1014111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 1015111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 1016111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 1017111881Sjhb * console along with a list of the offending locks. If indicated in the 1018111881Sjhb * flags then a failure results in a panic as well. 101974912Sjhb */ 102065557Sjasoneint 1021111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 102265557Sjasone{ 1023111881Sjhb struct lock_list_entry *lle; 102476272Sjhb struct lock_instance *lock1; 102583366Sjulian struct thread *td; 1026111881Sjhb va_list ap; 102774912Sjhb int i, n; 102865557Sjasone 1029112562Sjhb if (witness_cold || witness_watch == 0 || panicstr != NULL) 103074912Sjhb return (0); 103174912Sjhb n = 0; 103283366Sjulian td = curthread; 1033111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 103474912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 103576272Sjhb lock1 = &lle->ll_children[i]; 1036111881Sjhb if (lock1->li_lock == lock) 1037111881Sjhb continue; 1038111881Sjhb if (flags & WARN_GIANTOK && 103976272Sjhb lock1->li_lock == &Giant.mtx_object) 104074912Sjhb continue; 1041111881Sjhb if (flags & WARN_SLEEPOK && 1042111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 104376272Sjhb continue; 1044111881Sjhb if (n == 0) { 1045111881Sjhb va_start(ap, fmt); 1046111881Sjhb vprintf(fmt, ap); 1047111881Sjhb va_end(ap); 1048111881Sjhb printf(" with the following"); 1049111881Sjhb if (flags & WARN_SLEEPOK) 1050111881Sjhb printf(" non-sleepable"); 1051118441Sjhb printf(" locks held:\n"); 105276272Sjhb } 105374912Sjhb n++; 1054111881Sjhb witness_list_lock(lock1); 105574912Sjhb } 1056111881Sjhb if (PCPU_GET(spinlocks) != NULL) { 105797006Sjhb /* 105897006Sjhb * Since we already hold a spinlock preemption is 105997006Sjhb * already blocked. 106097006Sjhb */ 1061111881Sjhb if (n == 0) { 1062111881Sjhb va_start(ap, fmt); 1063111881Sjhb vprintf(fmt, ap); 1064111881Sjhb va_end(ap); 1065111881Sjhb printf(" with the following"); 1066111881Sjhb if (flags & WARN_SLEEPOK) 1067111881Sjhb printf(" non-sleepable"); 1068118441Sjhb printf(" locks held:\n"); 1069111881Sjhb } 1070111881Sjhb n += witness_list_locks(PCPU_PTR(spinlocks)); 107165557Sjasone } 1072111881Sjhb if (flags & WARN_PANIC && n) 1073111881Sjhb panic("witness_warn"); 107467676Sjhb#ifdef DDB 1075111881Sjhb else if (witness_ddb && n) 107675711Sjhb Debugger(__func__); 1077111881Sjhb#endif 107865557Sjasone return (n); 107965557Sjasone} 108065557Sjasone 1081102448Siedowseconst char * 1082102448Siedowsewitness_file(struct lock_object *lock) 1083102448Siedowse{ 1084102448Siedowse struct witness *w; 1085102448Siedowse 1086112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1087102448Siedowse return ("?"); 1088102448Siedowse w = lock->lo_witness; 1089102448Siedowse return (w->w_file); 1090102448Siedowse} 1091102448Siedowse 1092102448Siedowseint 1093102448Siedowsewitness_line(struct lock_object *lock) 1094102448Siedowse{ 1095102448Siedowse struct witness *w; 1096102448Siedowse 1097112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1098102448Siedowse return (0); 1099102448Siedowse w = lock->lo_witness; 1100102448Siedowse return (w->w_line); 1101102448Siedowse} 1102102448Siedowse 110365856Sjhbstatic struct witness * 110474912Sjhbenroll(const char *description, struct lock_class *lock_class) 110565557Sjasone{ 110674912Sjhb struct witness *w; 110765557Sjasone 1108112562Sjhb if (!witness_watch || witness_watch == 0 || panicstr != NULL) 110965557Sjasone return (NULL); 111074912Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 111165557Sjasone return (NULL); 111274912Sjhb mtx_lock_spin(&w_mtx); 111374912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 111497948Sjhb if (w->w_name == description || (w->w_refcount > 0 && 111597948Sjhb strcmp(description, w->w_name) == 0)) { 111675362Sjhb w->w_refcount++; 111774912Sjhb mtx_unlock_spin(&w_mtx); 111874912Sjhb if (lock_class != w->w_class) 111974912Sjhb panic( 112074912Sjhb "lock (%s) %s does not match earlier (%s) lock", 112174912Sjhb description, lock_class->lc_name, 112274912Sjhb w->w_class->lc_name); 112365557Sjasone return (w); 112465557Sjasone } 112565557Sjasone } 112674912Sjhb /* 112774912Sjhb * This isn't quite right, as witness_cold is still 0 while we 112874912Sjhb * enroll all the locks initialized before witness_initialize(). 112974912Sjhb */ 113075364Sbp if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 113175364Sbp mtx_unlock_spin(&w_mtx); 113274912Sjhb panic("spin lock %s not in order list", description); 113375364Sbp } 113465557Sjasone if ((w = witness_get()) == NULL) 113565557Sjasone return (NULL); 113674912Sjhb w->w_name = description; 113774912Sjhb w->w_class = lock_class; 113875362Sjhb w->w_refcount = 1; 113974912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 114074912Sjhb if (lock_class->lc_flags & LC_SPINLOCK) 114174912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 114274912Sjhb else if (lock_class->lc_flags & LC_SLEEPLOCK) 114374912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 114475364Sbp else { 114575364Sbp mtx_unlock_spin(&w_mtx); 114674912Sjhb panic("lock class %s is not sleep or spin", 114774912Sjhb lock_class->lc_name); 114875364Sbp } 114974912Sjhb mtx_unlock_spin(&w_mtx); 115065557Sjasone return (w); 115165557Sjasone} 115265557Sjasone 1153112117Sjhb/* Don't let the door bang you on the way out... */ 115465557Sjasonestatic int 1155112117Sjhbdepart(struct witness *w) 115665557Sjasone{ 1157112117Sjhb struct witness_child_list_entry *wcl, *nwcl; 115874912Sjhb struct witness_list *list; 1159112117Sjhb struct witness *parent; 116065557Sjasone 1161112117Sjhb MPASS(w->w_refcount == 0); 1162112117Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 1163112117Sjhb list = &w_sleep; 1164112117Sjhb else 1165112117Sjhb list = &w_spin; 1166112117Sjhb /* 1167112117Sjhb * First, we run through the entire tree looking for any 1168112117Sjhb * witnesses that the outgoing witness is a child of. For 1169112117Sjhb * each parent that we find, we reparent all the direct 1170112117Sjhb * children of the outgoing witness to its parent. 1171112117Sjhb */ 1172112117Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 1173112117Sjhb if (!isitmychild(parent, w)) 1174112117Sjhb continue; 1175112117Sjhb removechild(parent, w); 1176112117Sjhb if (!reparentchildren(parent, w)) 1177112117Sjhb return (0); 1178112117Sjhb } 1179112117Sjhb 1180112117Sjhb /* 1181112117Sjhb * Now we go through and free up the child list of the 1182112117Sjhb * outgoing witness. 1183112117Sjhb */ 1184112117Sjhb for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1185112117Sjhb nwcl = wcl->wcl_next; 1186112117Sjhb witness_child_free(wcl); 1187112117Sjhb } 1188112117Sjhb 1189112117Sjhb /* 1190112117Sjhb * Detach from various lists and free. 1191112117Sjhb */ 1192112117Sjhb STAILQ_REMOVE(list, w, witness, w_typelist); 1193112117Sjhb STAILQ_REMOVE(&w_all, w, witness, w_list); 1194112117Sjhb witness_free(w); 1195112117Sjhb 1196112117Sjhb /* Finally, fixup the tree. */ 1197112117Sjhb return (rebalancetree(list)); 1198112117Sjhb} 1199112117Sjhb 1200112117Sjhb/* 1201112117Sjhb * Prune an entire lock order tree. We look for cases where a lock 1202112117Sjhb * is now both a descendant and a direct child of a given lock. In 1203112117Sjhb * that case, we want to remove the direct child link from the tree. 1204112117Sjhb * 1205112117Sjhb * Returns false if insertchild() fails. 1206112117Sjhb */ 1207112117Sjhbstatic int 1208112117Sjhbrebalancetree(struct witness_list *list) 1209112117Sjhb{ 1210112117Sjhb struct witness *child, *parent; 1211112117Sjhb 1212112117Sjhb STAILQ_FOREACH(child, list, w_typelist) { 1213112117Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 1214112117Sjhb if (!isitmychild(parent, child)) 1215112117Sjhb continue; 1216112117Sjhb removechild(parent, child); 1217112117Sjhb if (isitmydescendant(parent, child)) 1218112117Sjhb continue; 1219112117Sjhb if (!insertchild(parent, child)) 1220112117Sjhb return (0); 1221112117Sjhb } 1222112117Sjhb } 1223112117Sjhb witness_levelall(); 1224112117Sjhb return (1); 1225112117Sjhb} 1226112117Sjhb 1227112117Sjhb/* 1228112117Sjhb * Add "child" as a direct child of "parent". Returns false if 1229112117Sjhb * we fail due to out of memory. 1230112117Sjhb */ 1231112117Sjhbstatic int 1232112117Sjhbinsertchild(struct witness *parent, struct witness *child) 1233112117Sjhb{ 1234112117Sjhb struct witness_child_list_entry **wcl; 1235112117Sjhb 123674912Sjhb MPASS(child != NULL && parent != NULL); 123774912Sjhb 123865557Sjasone /* 123965557Sjasone * Insert "child" after "parent" 124065557Sjasone */ 124174912Sjhb wcl = &parent->w_children; 124274912Sjhb while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 124374912Sjhb wcl = &(*wcl)->wcl_next; 124474912Sjhb if (*wcl == NULL) { 124574912Sjhb *wcl = witness_child_get(); 124674912Sjhb if (*wcl == NULL) 1247112117Sjhb return (0); 124865557Sjasone } 124974912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 125074912Sjhb 1251112117Sjhb return (1); 1252112117Sjhb} 1253112117Sjhb 1254112117Sjhb/* 1255112117Sjhb * Make all the direct descendants of oldparent be direct descendants 1256112117Sjhb * of newparent. 1257112117Sjhb */ 1258112117Sjhbstatic int 1259112117Sjhbreparentchildren(struct witness *newparent, struct witness *oldparent) 1260112117Sjhb{ 1261112117Sjhb struct witness_child_list_entry *wcl; 1262112117Sjhb int i; 1263112117Sjhb 1264112117Sjhb /* Avoid making a witness a child of itself. */ 1265112117Sjhb MPASS(!isitmychild(oldparent, newparent)); 1266112117Sjhb 1267112117Sjhb for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1268112117Sjhb for (i = 0; i < wcl->wcl_count; i++) 1269112117Sjhb if (!insertchild(newparent, wcl->wcl_children[i])) 1270112117Sjhb return (0); 1271112117Sjhb return (1); 1272112117Sjhb} 1273112117Sjhb 1274112117Sjhbstatic int 1275112117Sjhbitismychild(struct witness *parent, struct witness *child) 1276112117Sjhb{ 1277112117Sjhb struct witness_list *list; 1278112117Sjhb 1279112117Sjhb MPASS(child != NULL && parent != NULL); 1280112117Sjhb if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1281112117Sjhb (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1282112117Sjhb panic( 1283112117Sjhb "%s: parent (%s) and child (%s) are not the same lock type", 1284112117Sjhb __func__, parent->w_class->lc_name, 1285112117Sjhb child->w_class->lc_name); 1286112117Sjhb 1287112117Sjhb if (!insertchild(parent, child)) 128865557Sjasone return (0); 1289112117Sjhb 129074912Sjhb if (parent->w_class->lc_flags & LC_SLEEPLOCK) 129174912Sjhb list = &w_sleep; 129274912Sjhb else 129374912Sjhb list = &w_spin; 1294112117Sjhb return (rebalancetree(list)); 129565557Sjasone} 129665557Sjasone 129765557Sjasonestatic void 129865856Sjhbremovechild(struct witness *parent, struct witness *child) 129965557Sjasone{ 130074912Sjhb struct witness_child_list_entry **wcl, *wcl1; 130165557Sjasone int i; 130265557Sjasone 130374912Sjhb for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 130474912Sjhb for (i = 0; i < (*wcl)->wcl_count; i++) 130574912Sjhb if ((*wcl)->wcl_children[i] == child) 130665557Sjasone goto found; 130765557Sjasone return; 130865557Sjasonefound: 130974912Sjhb (*wcl)->wcl_count--; 131074912Sjhb if ((*wcl)->wcl_count > i) 131174912Sjhb (*wcl)->wcl_children[i] = 131274912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count]; 131374912Sjhb MPASS((*wcl)->wcl_children[i] != NULL); 131474912Sjhb if ((*wcl)->wcl_count != 0) 131565557Sjasone return; 131674912Sjhb wcl1 = *wcl; 131774912Sjhb *wcl = wcl1->wcl_next; 131874912Sjhb witness_child_free(wcl1); 131965557Sjasone} 132065557Sjasone 132165557Sjasonestatic int 132265856Sjhbisitmychild(struct witness *parent, struct witness *child) 132365557Sjasone{ 132474912Sjhb struct witness_child_list_entry *wcl; 132565557Sjasone int i; 132665557Sjasone 132774912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 132874912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 132974912Sjhb if (wcl->wcl_children[i] == child) 133065557Sjasone return (1); 133165557Sjasone } 133265557Sjasone } 133365557Sjasone return (0); 133465557Sjasone} 133565557Sjasone 133665557Sjasonestatic int 133765856Sjhbisitmydescendant(struct witness *parent, struct witness *child) 133865557Sjasone{ 133974912Sjhb struct witness_child_list_entry *wcl; 134074912Sjhb int i, j; 134165557Sjasone 134274912Sjhb if (isitmychild(parent, child)) 134374912Sjhb return (1); 134474912Sjhb j = 0; 134574912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 134667352Sjhb MPASS(j < 1000); 134774912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 134874912Sjhb if (isitmydescendant(wcl->wcl_children[i], child)) 134965557Sjasone return (1); 135065557Sjasone } 135174912Sjhb j++; 135265557Sjasone } 135365557Sjasone return (0); 135465557Sjasone} 135565557Sjasone 1356104094Sphkstatic void 135765557Sjasonewitness_levelall (void) 135865557Sjasone{ 135974912Sjhb struct witness_list *list; 136065856Sjhb struct witness *w, *w1; 136165557Sjasone 136274912Sjhb /* 136374912Sjhb * First clear all levels. 136474912Sjhb */ 136574912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 136674912Sjhb w->w_level = 0; 136774912Sjhb } 136874912Sjhb 136974912Sjhb /* 137074912Sjhb * Look for locks with no parent and level all their descendants. 137174912Sjhb */ 137274912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 137374912Sjhb /* 137474912Sjhb * This is just an optimization, technically we could get 137574912Sjhb * away just walking the all list each time. 137674912Sjhb */ 137774912Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 137874912Sjhb list = &w_sleep; 137974912Sjhb else 138074912Sjhb list = &w_spin; 138174912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 138265557Sjasone if (isitmychild(w1, w)) 138374912Sjhb goto skip; 138465557Sjasone } 138565557Sjasone witness_leveldescendents(w, 0); 138674912Sjhb skip: 138795541Smarcel ; /* silence GCC 3.x */ 138865557Sjasone } 138965557Sjasone} 139065557Sjasone 139165557Sjasonestatic void 139265856Sjhbwitness_leveldescendents(struct witness *parent, int level) 139365557Sjasone{ 139474912Sjhb struct witness_child_list_entry *wcl; 139565557Sjasone int i; 139665557Sjasone 139765557Sjasone if (parent->w_level < level) 139865557Sjasone parent->w_level = level; 139965557Sjasone level++; 140074912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 140174912Sjhb for (i = 0; i < wcl->wcl_count; i++) 140274912Sjhb witness_leveldescendents(wcl->wcl_children[i], level); 140365557Sjasone} 140465557Sjasone 140565557Sjasonestatic void 140665856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...), 1407112118Sjhb struct witness *parent, int indent) 140865557Sjasone{ 140974912Sjhb struct witness_child_list_entry *wcl; 141074912Sjhb int i, level; 141165557Sjasone 141295543Sjhb level = parent->w_level; 141374912Sjhb prnt("%-2d", level); 1414112118Sjhb for (i = 0; i < indent; i++) 141565557Sjasone prnt(" "); 1416112118Sjhb if (parent->w_refcount > 0) 1417112118Sjhb prnt("%s", parent->w_name); 1418112118Sjhb else 1419112118Sjhb prnt("(dead)"); 1420112118Sjhb if (parent->w_displayed) { 1421112118Sjhb prnt(" -- (already displayed)\n"); 1422112118Sjhb return; 1423112118Sjhb } 1424112118Sjhb parent->w_displayed = 1; 142597948Sjhb if (parent->w_refcount > 0) { 142697948Sjhb if (parent->w_file != NULL) 1427112118Sjhb prnt(" -- last acquired @ %s:%d", parent->w_file, 142897948Sjhb parent->w_line); 1429112118Sjhb } 1430112118Sjhb prnt("\n"); 143174912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 143274912Sjhb for (i = 0; i < wcl->wcl_count; i++) 143374912Sjhb witness_displaydescendants(prnt, 1434112118Sjhb wcl->wcl_children[i], indent + 1); 143574912Sjhb} 143665557Sjasone 1437105508Sphk#ifdef BLESSING 143865557Sjasonestatic int 143965856Sjhbblessed(struct witness *w1, struct witness *w2) 144065557Sjasone{ 144165557Sjasone int i; 144265856Sjhb struct witness_blessed *b; 144365557Sjasone 144465557Sjasone for (i = 0; i < blessed_count; i++) { 144565557Sjasone b = &blessed_list[i]; 144674912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 144774912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 144865557Sjasone return (1); 144965557Sjasone continue; 145065557Sjasone } 145174912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 145274912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 145365557Sjasone return (1); 145465557Sjasone } 145565557Sjasone return (0); 145665557Sjasone} 1457105508Sphk#endif 145865557Sjasone 145965856Sjhbstatic struct witness * 146074912Sjhbwitness_get(void) 146165557Sjasone{ 146265856Sjhb struct witness *w; 146365557Sjasone 1464112562Sjhb if (witness_watch == 0) { 146576481Sjhb mtx_unlock_spin(&w_mtx); 146676481Sjhb return (NULL); 146776481Sjhb } 146874912Sjhb if (STAILQ_EMPTY(&w_free)) { 1469112562Sjhb witness_watch = 0; 147074912Sjhb mtx_unlock_spin(&w_mtx); 147174912Sjhb printf("%s: witness exhausted\n", __func__); 147265557Sjasone return (NULL); 147365557Sjasone } 147474912Sjhb w = STAILQ_FIRST(&w_free); 147574912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 147665856Sjhb bzero(w, sizeof(*w)); 147765557Sjasone return (w); 147865557Sjasone} 147965557Sjasone 148065557Sjasonestatic void 148165856Sjhbwitness_free(struct witness *w) 148265557Sjasone{ 148374912Sjhb 148474912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 148565557Sjasone} 148665557Sjasone 148774912Sjhbstatic struct witness_child_list_entry * 148874912Sjhbwitness_child_get(void) 148965557Sjasone{ 149074912Sjhb struct witness_child_list_entry *wcl; 149165557Sjasone 1492112562Sjhb if (witness_watch == 0) { 149376481Sjhb mtx_unlock_spin(&w_mtx); 149476481Sjhb return (NULL); 149576481Sjhb } 149674912Sjhb wcl = w_child_free; 149774912Sjhb if (wcl == NULL) { 1498112562Sjhb witness_watch = 0; 149974912Sjhb mtx_unlock_spin(&w_mtx); 150074912Sjhb printf("%s: witness exhausted\n", __func__); 150174912Sjhb return (NULL); 150265557Sjasone } 150374912Sjhb w_child_free = wcl->wcl_next; 150474912Sjhb bzero(wcl, sizeof(*wcl)); 150574912Sjhb return (wcl); 150674912Sjhb} 150769881Sjake 150874912Sjhbstatic void 150974912Sjhbwitness_child_free(struct witness_child_list_entry *wcl) 151074912Sjhb{ 151174912Sjhb 151274912Sjhb wcl->wcl_next = w_child_free; 151374912Sjhb w_child_free = wcl; 151465557Sjasone} 151565557Sjasone 151674912Sjhbstatic struct lock_list_entry * 151774912Sjhbwitness_lock_list_get(void) 151874912Sjhb{ 151974912Sjhb struct lock_list_entry *lle; 152071709Sjhb 1521112562Sjhb if (witness_watch == 0) 152276481Sjhb return (NULL); 152374912Sjhb mtx_lock_spin(&w_mtx); 152474912Sjhb lle = w_lock_list_free; 152574912Sjhb if (lle == NULL) { 1526112562Sjhb witness_watch = 0; 152774912Sjhb mtx_unlock_spin(&w_mtx); 152874912Sjhb printf("%s: witness exhausted\n", __func__); 152974912Sjhb return (NULL); 153074912Sjhb } 153174912Sjhb w_lock_list_free = lle->ll_next; 153274912Sjhb mtx_unlock_spin(&w_mtx); 153374912Sjhb bzero(lle, sizeof(*lle)); 153474912Sjhb return (lle); 153574912Sjhb} 153674912Sjhb 153774912Sjhbstatic void 153874912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 153971709Sjhb{ 154071709Sjhb 154174912Sjhb mtx_lock_spin(&w_mtx); 154274912Sjhb lle->ll_next = w_lock_list_free; 154374912Sjhb w_lock_list_free = lle; 154474912Sjhb mtx_unlock_spin(&w_mtx); 154571709Sjhb} 154671709Sjhb 154776272Sjhbstatic struct lock_instance * 154876272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 154976272Sjhb{ 155076272Sjhb struct lock_list_entry *lle; 155176272Sjhb struct lock_instance *instance; 155276272Sjhb int i; 155376272Sjhb 155476272Sjhb for (lle = lock_list; lle != NULL; lle = lle->ll_next) 155576272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 155676272Sjhb instance = &lle->ll_children[i]; 155776272Sjhb if (instance->li_lock == lock) 155876272Sjhb return (instance); 155976272Sjhb } 156076272Sjhb return (NULL); 156176272Sjhb} 156276272Sjhb 1563111881Sjhbstatic void 1564111881Sjhbwitness_list_lock(struct lock_instance *instance) 1565111881Sjhb{ 1566111881Sjhb struct lock_object *lock; 1567111881Sjhb 1568111881Sjhb lock = instance->li_lock; 1569111881Sjhb printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1570111881Sjhb "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name); 1571111881Sjhb if (lock->lo_type != lock->lo_name) 1572111881Sjhb printf(" (%s)", lock->lo_type); 1573111881Sjhb printf(" r = %d (%p) locked @ %s:%d\n", 1574111881Sjhb instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1575111881Sjhb instance->li_line); 1576111881Sjhb} 1577111881Sjhb 157874912Sjhbint 157975273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 158072224Sjhb{ 158175273Sjhb struct lock_list_entry *lle; 158274912Sjhb int i, nheld; 158372224Sjhb 158474912Sjhb nheld = 0; 158574912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 158674912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 1587111881Sjhb witness_list_lock(&lle->ll_children[i]); 158874912Sjhb nheld++; 158974912Sjhb } 159075273Sjhb return (nheld); 159175273Sjhb} 159275273Sjhb 1593118271Sjhb/* 1594118271Sjhb * This is a bit risky at best. We call this function when we have timed 1595118271Sjhb * out acquiring a spin lock, and we assume that the other CPU is stuck 1596118271Sjhb * with this lock held. So, we go groveling around in the other CPU's 1597118271Sjhb * per-cpu data to try to find the lock instance for this spin lock to 1598118271Sjhb * see when it was last acquired. 1599118271Sjhb */ 160065557Sjasonevoid 1601118271Sjhbwitness_display_spinlock(struct lock_object *lock, struct thread *owner) 1602118271Sjhb{ 1603118271Sjhb struct lock_instance *instance; 1604118271Sjhb struct pcpu *pc; 1605118271Sjhb 1606118271Sjhb if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 1607118271Sjhb return; 1608118271Sjhb pc = pcpu_find(owner->td_oncpu); 1609118271Sjhb instance = find_instance(pc->pc_spinlocks, lock); 1610118271Sjhb if (instance != NULL) 1611118271Sjhb witness_list_lock(instance); 1612118271Sjhb} 1613118271Sjhb 1614118271Sjhbvoid 161574912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 161665557Sjasone{ 161776272Sjhb struct lock_instance *instance; 161871320Sjasone 161982284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1620112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 162171352Sjasone return; 162282243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 162382243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 162482243Sjhb lock->lo_class->lc_name, lock->lo_name); 162583366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 162682243Sjhb if (instance == NULL) 162782243Sjhb panic("%s: lock (%s) %s not locked", __func__, 162882243Sjhb lock->lo_class->lc_name, lock->lo_name); 162976272Sjhb *filep = instance->li_file; 163076272Sjhb *linep = instance->li_line; 163165557Sjasone} 163265557Sjasone 163365557Sjasonevoid 163474912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 163565557Sjasone{ 163676272Sjhb struct lock_instance *instance; 163771320Sjasone 163882284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1639112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 164071352Sjasone return; 164182243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 164282243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 164382243Sjhb lock->lo_class->lc_name, lock->lo_name); 164483366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 164582243Sjhb if (instance == NULL) 164682243Sjhb panic("%s: lock (%s) %s not locked", __func__, 164782243Sjhb lock->lo_class->lc_name, lock->lo_name); 164874912Sjhb lock->lo_witness->w_file = file; 164974912Sjhb lock->lo_witness->w_line = line; 165076272Sjhb instance->li_file = file; 165176272Sjhb instance->li_line = line; 165265557Sjasone} 165365557Sjasone 165478871Sjhbvoid 165578871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 165678871Sjhb{ 165778871Sjhb#ifdef INVARIANT_SUPPORT 165878871Sjhb struct lock_instance *instance; 165978871Sjhb 1660112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 166178941Sjhb return; 166278871Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 166383366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 166478871Sjhb else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 166578871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 166686422Sjhb else { 166778871Sjhb panic("Lock (%s) %s is not sleep or spin!", 166878871Sjhb lock->lo_class->lc_name, lock->lo_name); 166986422Sjhb } 1670112116Sjhb file = fixup_filename(file); 167178871Sjhb switch (flags) { 167278871Sjhb case LA_UNLOCKED: 167378871Sjhb if (instance != NULL) 167478871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 167578871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 167678871Sjhb break; 167778871Sjhb case LA_LOCKED: 167878871Sjhb case LA_LOCKED | LA_RECURSED: 167978871Sjhb case LA_LOCKED | LA_NOTRECURSED: 168078871Sjhb case LA_SLOCKED: 168178871Sjhb case LA_SLOCKED | LA_RECURSED: 168278871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 168378871Sjhb case LA_XLOCKED: 168478871Sjhb case LA_XLOCKED | LA_RECURSED: 168578871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 168686422Sjhb if (instance == NULL) { 168778871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 168878871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 168986422Sjhb break; 169086422Sjhb } 169178871Sjhb if ((flags & LA_XLOCKED) != 0 && 169278871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 169378871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 169478871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 169578871Sjhb if ((flags & LA_SLOCKED) != 0 && 169678871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 169778871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 169878871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 169978871Sjhb if ((flags & LA_RECURSED) != 0 && 170078871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 170178871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 170278871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 170378871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 170478871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 170578871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 170678871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 170778871Sjhb break; 170878871Sjhb default: 170978871Sjhb panic("Invalid lock assertion at %s:%d.", file, line); 171078871Sjhb 171178871Sjhb } 171278871Sjhb#endif /* INVARIANT_SUPPORT */ 171378871Sjhb} 171478871Sjhb 171574912Sjhb#ifdef DDB 1716112061Sjhbstatic void 1717112061Sjhbwitness_list(struct thread *td) 1718112061Sjhb{ 171974912Sjhb 1720112061Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1721112061Sjhb KASSERT(db_active, ("%s: not in the debugger", __func__)); 1722112061Sjhb 1723112562Sjhb if (witness_watch == 0) 1724112061Sjhb return; 1725112061Sjhb 1726112061Sjhb witness_list_locks(&td->td_sleeplocks); 1727112061Sjhb 1728112061Sjhb /* 1729112061Sjhb * We only handle spinlocks if td == curthread. This is somewhat broken 1730112061Sjhb * if td is currently executing on some other CPU and holds spin locks 1731112061Sjhb * as we won't display those locks. If we had a MI way of getting 1732112061Sjhb * the per-cpu data for a given cpu then we could use 1733113339Sjulian * td->td_oncpu to get the list of spinlocks for this thread 1734112061Sjhb * and "fix" this. 1735112061Sjhb * 1736112061Sjhb * That still wouldn't really fix this unless we locked sched_lock 1737112061Sjhb * or stopped the other CPU to make sure it wasn't changing the list 1738112061Sjhb * out from under us. It is probably best to just not try to handle 1739112061Sjhb * threads on other CPU's for now. 1740112061Sjhb */ 1741112061Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 1742112061Sjhb witness_list_locks(PCPU_PTR(spinlocks)); 1743112061Sjhb} 1744112061Sjhb 174574930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 174674912Sjhb{ 174783366Sjulian struct thread *td; 174883366Sjulian pid_t pid; 174975273Sjhb struct proc *p; 175074912Sjhb 175175273Sjhb if (have_addr) { 175275273Sjhb pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 175375273Sjhb ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 175475273Sjhb ((addr >> 16) % 16) * 10000; 175575273Sjhb /* sx_slock(&allproc_lock); */ 175683366Sjulian FOREACH_PROC_IN_SYSTEM(p) { 175775273Sjhb if (p->p_pid == pid) 175875273Sjhb break; 175975273Sjhb } 176075273Sjhb /* sx_sunlock(&allproc_lock); */ 176175273Sjhb if (p == NULL) { 176275273Sjhb db_printf("pid %d not found\n", pid); 176375273Sjhb return; 176475273Sjhb } 176590361Sjulian FOREACH_THREAD_IN_PROC(p, td) { 176690361Sjulian witness_list(td); 176790361Sjulian } 176883366Sjulian } else { 176983366Sjulian td = curthread; 177090361Sjulian witness_list(td); 177183366Sjulian } 177274912Sjhb} 177374912Sjhb 177474912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 177574912Sjhb{ 177674912Sjhb 177774912Sjhb witness_display(db_printf); 177874912Sjhb} 177974912Sjhb#endif 1780