subr_witness.c revision 112993
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 * $FreeBSD: head/sys/kern/subr_witness.c 112993 2003-04-02 23:53:30Z peter $ 3165557Sjasone */ 3265557Sjasone 3365557Sjasone/* 3474912Sjhb * Implementation of the `witness' lock verifier. Originally implemented for 3574912Sjhb * mutexes in BSD/OS. Extended to handle generic lock objects and lock 3674912Sjhb * classes in FreeBSD. 3772200Sbmilekic */ 3872200Sbmilekic 3972200Sbmilekic/* 4065557Sjasone * Main Entry: witness 4165557Sjasone * Pronunciation: 'wit-n&s 4265557Sjasone * Function: noun 4365557Sjasone * Etymology: Middle English witnesse, from Old English witnes knowledge, 4465557Sjasone * testimony, witness, from 2wit 4565557Sjasone * Date: before 12th century 4665557Sjasone * 1 : attestation of a fact or event : TESTIMONY 4765557Sjasone * 2 : one that gives evidence; specifically : one who testifies in 4865557Sjasone * a cause or before a judicial tribunal 4965557Sjasone * 3 : one asked to be present at a transaction so as to be able to 5065557Sjasone * testify to its having taken place 5165557Sjasone * 4 : one who has personal knowledge of something 5265557Sjasone * 5 a : something serving as evidence or proof : SIGN 5365557Sjasone * b : public affirmation by word or example of usually 5465557Sjasone * religious faith or conviction <the heroic witness to divine 5565557Sjasone * life -- Pilot> 5665557Sjasone * 6 capitalized : a member of the Jehovah's Witnesses 5765557Sjasone */ 5865557Sjasone 59111881Sjhb/* 60111881Sjhb * Special rules concerning Giant and lock orders: 61111881Sjhb * 62111881Sjhb * 1) Giant must be acquired before any other mutexes. Stated another way, 63111881Sjhb * no other mutex may be held when Giant is acquired. 64111881Sjhb * 65111881Sjhb * 2) Giant must be released when blocking on a sleepable lock. 66111881Sjhb * 67111881Sjhb * This rule is less obvious, but is a result of Giant providing the same 68111881Sjhb * semantics as spl(). Basically, when a thread sleeps, it must release 69111881Sjhb * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule 70111881Sjhb * 2). 71111881Sjhb * 72111881Sjhb * 3) Giant may be acquired before or after sleepable locks. 73111881Sjhb * 74111881Sjhb * This rule is also not quite as obvious. Giant may be acquired after 75111881Sjhb * a sleepable lock because it is a non-sleepable lock and non-sleepable 76111881Sjhb * locks may always be acquired while holding a sleepable lock. The second 77111881Sjhb * case, Giant before a sleepable lock, follows from rule 2) above. Suppose 78111881Sjhb * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1 79111881Sjhb * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and 80111881Sjhb * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to 81111881Sjhb * execute. Thus, acquiring Giant both before and after a sleepable lock 82111881Sjhb * will not result in a lock order reversal. 83111881Sjhb */ 84111881Sjhb 8568790Sjhb#include "opt_ddb.h" 8667676Sjhb#include "opt_witness.h" 87112993Speter#ifdef __i386__ 88112993Speter#include "opt_swtch.h" 89112993Speter#endif 9067676Sjhb 9165557Sjasone#include <sys/param.h> 9267352Sjhb#include <sys/bus.h> 9367352Sjhb#include <sys/kernel.h> 9474912Sjhb#include <sys/ktr.h> 9574912Sjhb#include <sys/lock.h> 9667352Sjhb#include <sys/malloc.h> 9774912Sjhb#include <sys/mutex.h> 9865557Sjasone#include <sys/proc.h> 9967676Sjhb#include <sys/sysctl.h> 10065557Sjasone#include <sys/systm.h> 10165557Sjasone 10268790Sjhb#include <ddb/ddb.h> 10368790Sjhb 104111881Sjhb#include <machine/stdarg.h> 105111881Sjhb 106105508Sphk/* Define this to check for blessed mutexes */ 107105508Sphk#undef BLESSING 108105508Sphk 10974912Sjhb#define WITNESS_COUNT 200 11074912Sjhb#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 11165557Sjasone/* 11283798Sjhb * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 11374912Sjhb * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 11474912Sjhb * probably be safe for the most part, but it's still a SWAG. 11567352Sjhb */ 11674912Sjhb#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 11771352Sjasone 11874912Sjhb#define WITNESS_NCHILDREN 6 11971352Sjasone 12074912Sjhbstruct witness_child_list_entry; 12171352Sjasone 12274912Sjhbstruct witness { 12374912Sjhb const char *w_name; 12474912Sjhb struct lock_class *w_class; 12574912Sjhb STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 12674912Sjhb STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 12774912Sjhb struct witness_child_list_entry *w_children; /* Great evilness... */ 12874912Sjhb const char *w_file; 12974912Sjhb int w_line; 13074912Sjhb u_int w_level; 13174912Sjhb u_int w_refcount; 13274912Sjhb u_char w_Giant_squawked:1; 13374912Sjhb u_char w_other_squawked:1; 13474912Sjhb u_char w_same_squawked:1; 135112118Sjhb u_char w_displayed:1; 13674912Sjhb}; 13771352Sjasone 13874912Sjhbstruct witness_child_list_entry { 13974912Sjhb struct witness_child_list_entry *wcl_next; 14074912Sjhb struct witness *wcl_children[WITNESS_NCHILDREN]; 14174912Sjhb u_int wcl_count; 14274912Sjhb}; 14371352Sjasone 14474912SjhbSTAILQ_HEAD(witness_list, witness); 14571352Sjasone 146105508Sphk#ifdef BLESSING 14774912Sjhbstruct witness_blessed { 14874912Sjhb const char *b_lock1; 14974912Sjhb const char *b_lock2; 15074912Sjhb}; 151105508Sphk#endif 15271352Sjasone 15374912Sjhbstruct witness_order_list_entry { 15474912Sjhb const char *w_name; 15574912Sjhb struct lock_class *w_class; 15674912Sjhb}; 15771352Sjasone 158112117Sjhb#ifdef BLESSING 159112117Sjhbstatic int blessed(struct witness *, struct witness *); 160112117Sjhb#endif 161112117Sjhbstatic int depart(struct witness *w); 16274912Sjhbstatic struct witness *enroll(const char *description, 16374912Sjhb struct lock_class *lock_class); 164112117Sjhbstatic int insertchild(struct witness *parent, struct witness *child); 165112117Sjhbstatic int isitmychild(struct witness *parent, struct witness *child); 166112117Sjhbstatic int isitmydescendant(struct witness *parent, struct witness *child); 16774912Sjhbstatic int itismychild(struct witness *parent, struct witness *child); 168112117Sjhbstatic int rebalancetree(struct witness_list *list); 16974912Sjhbstatic void removechild(struct witness *parent, struct witness *child); 170112117Sjhbstatic int reparentchildren(struct witness *newparent, 171112117Sjhb struct witness *oldparent); 172112562Sjhbstatic int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 17374912Sjhbstatic void witness_displaydescendants(void(*)(const char *fmt, ...), 174112118Sjhb struct witness *, int indent); 175112116Sjhbstatic const char *fixup_filename(const char *file); 17674912Sjhbstatic void witness_leveldescendents(struct witness *parent, int level); 17774912Sjhbstatic void witness_levelall(void); 17874912Sjhbstatic struct witness *witness_get(void); 17974912Sjhbstatic void witness_free(struct witness *m); 18074912Sjhbstatic struct witness_child_list_entry *witness_child_get(void); 18174912Sjhbstatic void witness_child_free(struct witness_child_list_entry *wcl); 18274912Sjhbstatic struct lock_list_entry *witness_lock_list_get(void); 18374912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 18476272Sjhbstatic struct lock_instance *find_instance(struct lock_list_entry *lock_list, 18576272Sjhb struct lock_object *lock); 186111881Sjhbstatic void witness_list_lock(struct lock_instance *instance); 187112115Sjhb#ifdef DDB 188112061Sjhbstatic void witness_list(struct thread *td); 189100011Smpstatic void witness_display_list(void(*prnt)(const char *fmt, ...), 190100011Smp struct witness_list *list); 191100011Smpstatic void witness_display(void(*)(const char *fmt, ...)); 192100011Smp#endif 19372200Sbmilekic 19474912SjhbMALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 19572200Sbmilekic 196112562Sjhb/* 197112562Sjhb * If set to 0, witness is disabled. If set to 1, witness performs full lock 198112562Sjhb * order checking for all locks. If set to 2 or higher, then witness skips 199112562Sjhb * the full lock order check if the lock being acquired is at a higher level 200112562Sjhb * (i.e. farther down in the tree) than the current lock. This last mode is 201112562Sjhb * somewhat experimental and not considered fully safe. At runtime, this 202112562Sjhb * value may be set to 0 to turn off witness. witness is not allowed be 203112562Sjhb * turned on once it is turned off, however. 204112562Sjhb */ 20577843Speterstatic int witness_watch = 1; 20677900SpeterTUNABLE_INT("debug.witness_watch", &witness_watch); 207112562SjhbSYSCTL_PROC(_debug, OID_AUTO, witness_watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 208112562Sjhb sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 20971352Sjasone 21067352Sjhb#ifdef DDB 21172200Sbmilekic/* 21267676Sjhb * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 21365557Sjasone * drop into kdebug() when: 21465557Sjasone * - a lock heirarchy violation occurs 21565557Sjasone * - locks are held when going to sleep. 21665557Sjasone */ 21767676Sjhb#ifdef WITNESS_DDB 21877843Speterint witness_ddb = 1; 21967676Sjhb#else 22077843Speterint witness_ddb = 0; 22165557Sjasone#endif 22277900SpeterTUNABLE_INT("debug.witness_ddb", &witness_ddb); 22367676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 224110779Speter 225110779Speter/* 226110779Speter * When DDB is enabled and witness_trace is set to 1, it will cause the system 227110779Speter * to print a stack trace: 228110779Speter * - a lock heirarchy violation occurs 229110779Speter * - locks are held when going to sleep. 230110779Speter */ 231110779Speterint witness_trace = 1; 232110779SpeterTUNABLE_INT("debug.witness_trace", &witness_trace); 233110779SpeterSYSCTL_INT(_debug, OID_AUTO, witness_trace, CTLFLAG_RW, &witness_trace, 0, ""); 23467676Sjhb#endif /* DDB */ 23565557Sjasone 23667676Sjhb#ifdef WITNESS_SKIPSPIN 23777843Speterint witness_skipspin = 1; 23867676Sjhb#else 23977843Speterint witness_skipspin = 0; 24065557Sjasone#endif 24177900SpeterTUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 24267676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 24367676Sjhb ""); 24465557Sjasone 24574912Sjhbstatic struct mtx w_mtx; 24674912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 24774912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 24874912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 24974912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 25074912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL; 25174912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 25265557Sjasone 25374912Sjhbstatic struct witness w_data[WITNESS_COUNT]; 25474912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 25574912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 25665557Sjasone 25774912Sjhbstatic struct witness_order_list_entry order_lists[] = { 25874912Sjhb { "proctree", &lock_class_sx }, 25974912Sjhb { "allproc", &lock_class_sx }, 260111951Sjhb { "Giant", &lock_class_mtx_sleep }, 261108184Skris { "filedesc structure", &lock_class_mtx_sleep }, 262108184Skris { "pipe mutex", &lock_class_mtx_sleep }, 26396122Salfred { "sigio lock", &lock_class_mtx_sleep }, 26491140Stanimura { "process group", &lock_class_mtx_sleep }, 26574912Sjhb { "process lock", &lock_class_mtx_sleep }, 26691140Stanimura { "session", &lock_class_mtx_sleep }, 26774912Sjhb { "uidinfo hash", &lock_class_mtx_sleep }, 26874912Sjhb { "uidinfo struct", &lock_class_mtx_sleep }, 26974912Sjhb { NULL, NULL }, 27075464Sjhb /* 27175464Sjhb * spin locks 27275464Sjhb */ 27384331Sjhb#ifdef SMP 27484331Sjhb { "ap boot", &lock_class_mtx_spin }, 27584331Sjhb#ifdef __i386__ 27674912Sjhb { "com", &lock_class_mtx_spin }, 27772224Sjhb#endif 27884331Sjhb#endif 27974912Sjhb { "sio", &lock_class_mtx_spin }, 28072224Sjhb#ifdef __i386__ 28174912Sjhb { "cy", &lock_class_mtx_spin }, 28272224Sjhb#endif 283103091Sjake { "sabtty", &lock_class_mtx_spin }, 284109015Sjake { "zstty", &lock_class_mtx_spin }, 28574912Sjhb { "ng_node", &lock_class_mtx_spin }, 28674912Sjhb { "ng_worklist", &lock_class_mtx_spin }, 28774912Sjhb { "ithread table lock", &lock_class_mtx_spin }, 28874912Sjhb { "sched lock", &lock_class_mtx_spin }, 28974912Sjhb { "callout", &lock_class_mtx_spin }, 29065557Sjasone /* 29165557Sjasone * leaf locks 29265557Sjasone */ 29390278Sjhb { "allpmaps", &lock_class_mtx_spin }, 29499416Salc { "vm page queue free mutex", &lock_class_mtx_spin }, 29588322Sjhb { "icu", &lock_class_mtx_spin }, 29672224Sjhb#ifdef SMP 29774912Sjhb { "smp rendezvous", &lock_class_mtx_spin }, 29899862Speter#if defined(__i386__) && defined(APIC_IO) 29999862Speter { "tlb", &lock_class_mtx_spin }, 30072224Sjhb#endif 301112993Speter#if defined(__i386__) && defined(LAZY_SWITCH) 302112993Speter { "lazypmap", &lock_class_mtx_spin }, 303112993Speter#endif 304108187Sjake#ifdef __sparc64__ 305108187Sjake { "ipi", &lock_class_mtx_spin }, 30699862Speter#endif 307108187Sjake#endif 30878785Sjhb { "clk", &lock_class_mtx_spin }, 30995473Sdes { "mutex profiling lock", &lock_class_mtx_spin }, 310111028Sjeff { "kse zombie lock", &lock_class_mtx_spin }, 311103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 312104951Speter#ifdef __ia64__ 313104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 314104951Speter#endif 315111068Speter#ifdef __i386__ 316111068Speter { "pcicfg", &lock_class_mtx_spin }, 317111068Speter#endif 31874912Sjhb { NULL, NULL }, 31974912Sjhb { NULL, NULL } 32065557Sjasone}; 32165557Sjasone 322105508Sphk#ifdef BLESSING 32365557Sjasone/* 32465557Sjasone * Pairs of locks which have been blessed 32565557Sjasone * Don't complain about order problems with blessed locks 32665557Sjasone */ 32765856Sjhbstatic struct witness_blessed blessed_list[] = { 32865557Sjasone}; 32972200Sbmilekicstatic int blessed_count = 33072200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 331105508Sphk#endif 33265557Sjasone 33374912Sjhb/* 33474912Sjhb * List of all locks in the system. 33574912Sjhb */ 33697963SjhbTAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 33774912Sjhb 33874912Sjhbstatic struct mtx all_mtx = { 33974912Sjhb { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 34074912Sjhb "All locks list", /* mtx_object.lo_name */ 34193811Sjhb "All locks list", /* mtx_object.lo_type */ 34274912Sjhb LO_INITIALIZED, /* mtx_object.lo_flags */ 34397963Sjhb { NULL, NULL }, /* mtx_object.lo_list */ 34474912Sjhb NULL }, /* mtx_object.lo_witness */ 34574912Sjhb MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 34674912Sjhb TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 34774912Sjhb { NULL, NULL } /* mtx_contested */ 34874912Sjhb}; 34974912Sjhb 35074912Sjhb/* 35174912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 35274912Sjhb */ 35374912Sjhbstatic int witness_cold = 1; 35474912Sjhb 35574912Sjhb/* 35674912Sjhb * Global variables for book keeping. 35774912Sjhb */ 35874912Sjhbstatic int lock_cur_cnt; 35974912Sjhbstatic int lock_max_cnt; 36074912Sjhb 36174912Sjhb/* 36274912Sjhb * The WITNESS-enabled diagnostic code. 36374912Sjhb */ 36471352Sjasonestatic void 36574912Sjhbwitness_initialize(void *dummy __unused) 36665557Sjasone{ 36774912Sjhb struct lock_object *lock; 36874912Sjhb struct witness_order_list_entry *order; 36974912Sjhb struct witness *w, *w1; 37074912Sjhb int i; 37165557Sjasone 37274912Sjhb /* 37374912Sjhb * We have to release Giant before initializing its witness 37474912Sjhb * structure so that WITNESS doesn't get confused. 37574912Sjhb */ 37674912Sjhb mtx_unlock(&Giant); 37774912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 37874912Sjhb 37987593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 38097963Sjhb TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 38193811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 38293811Sjhb MTX_NOWITNESS); 38374912Sjhb for (i = 0; i < WITNESS_COUNT; i++) 38474912Sjhb witness_free(&w_data[i]); 38574912Sjhb for (i = 0; i < WITNESS_CHILDCOUNT; i++) 38674912Sjhb witness_child_free(&w_childdata[i]); 38774912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 38874912Sjhb witness_lock_list_free(&w_locklistdata[i]); 38974912Sjhb 39074912Sjhb /* First add in all the specified order lists. */ 39174912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 39274912Sjhb w = enroll(order->w_name, order->w_class); 39375569Sjhb if (w == NULL) 39475569Sjhb continue; 39574912Sjhb w->w_file = "order list"; 39674912Sjhb for (order++; order->w_name != NULL; order++) { 39774912Sjhb w1 = enroll(order->w_name, order->w_class); 39875569Sjhb if (w1 == NULL) 39975569Sjhb continue; 40074912Sjhb w1->w_file = "order list"; 401112117Sjhb if (!itismychild(w, w1)) 402112117Sjhb panic("Not enough memory for static orders!"); 40374912Sjhb w = w1; 40465557Sjasone } 40565557Sjasone } 40665557Sjasone 40774912Sjhb /* Iterate through all locks and add them to witness. */ 40874912Sjhb mtx_lock(&all_mtx); 40997963Sjhb TAILQ_FOREACH(lock, &all_locks, lo_list) { 41074912Sjhb if (lock->lo_flags & LO_WITNESS) 41193811Sjhb lock->lo_witness = enroll(lock->lo_type, 41274912Sjhb lock->lo_class); 41374912Sjhb else 41474912Sjhb lock->lo_witness = NULL; 41574912Sjhb } 41674912Sjhb mtx_unlock(&all_mtx); 41774912Sjhb 41874912Sjhb /* Mark the witness code as being ready for use. */ 41974912Sjhb atomic_store_rel_int(&witness_cold, 0); 42074912Sjhb 42174912Sjhb mtx_lock(&Giant); 42265557Sjasone} 42374912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 42465557Sjasone 425112562Sjhbstatic int 426112562Sjhbsysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 427112562Sjhb{ 428112562Sjhb int error, value; 429112562Sjhb 430112562Sjhb value = witness_watch; 431112562Sjhb error = sysctl_handle_int(oidp, &value, 0, req); 432112562Sjhb if (error != 0 || req->newptr == NULL) 433112562Sjhb return (error); 434112562Sjhb error = suser(req->td); 435112562Sjhb if (error != 0) 436112562Sjhb return (error); 437112562Sjhb if (value == witness_watch) 438112562Sjhb return (0); 439112562Sjhb if (value != 0) 440112562Sjhb return (EINVAL); 441112562Sjhb witness_watch = 0; 442112562Sjhb return (0); 443112562Sjhb} 444112562Sjhb 44574912Sjhbvoid 44674912Sjhbwitness_init(struct lock_object *lock) 44774912Sjhb{ 44874912Sjhb struct lock_class *class; 44974912Sjhb 45074912Sjhb class = lock->lo_class; 45174912Sjhb if (lock->lo_flags & LO_INITIALIZED) 45282284Sjhb panic("%s: lock (%s) %s is already initialized", __func__, 45374912Sjhb class->lc_name, lock->lo_name); 45474912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 45574912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 45682284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 45774912Sjhb class->lc_name, lock->lo_name); 45874912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 45974912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 46082284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 46174912Sjhb class->lc_name, lock->lo_name); 46282244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 46382244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 46482284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 46582244Sjhb class->lc_name, lock->lo_name); 46682244Sjhb 46774912Sjhb mtx_lock(&all_mtx); 46897963Sjhb TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 46974912Sjhb lock->lo_flags |= LO_INITIALIZED; 47074912Sjhb lock_cur_cnt++; 47174912Sjhb if (lock_cur_cnt > lock_max_cnt) 47274912Sjhb lock_max_cnt = lock_cur_cnt; 47374912Sjhb mtx_unlock(&all_mtx); 474112562Sjhb if (!witness_cold && witness_watch != 0 && panicstr == NULL && 47574912Sjhb (lock->lo_flags & LO_WITNESS) != 0) 47693811Sjhb lock->lo_witness = enroll(lock->lo_type, class); 47774912Sjhb else 47874912Sjhb lock->lo_witness = NULL; 47974912Sjhb} 48074912Sjhb 48174912Sjhbvoid 48274912Sjhbwitness_destroy(struct lock_object *lock) 48374912Sjhb{ 48475362Sjhb struct witness *w; 48574912Sjhb 48674912Sjhb if (witness_cold) 48774912Sjhb panic("lock (%s) %s destroyed while witness_cold", 48874912Sjhb lock->lo_class->lc_name, lock->lo_name); 48974912Sjhb if ((lock->lo_flags & LO_INITIALIZED) == 0) 49082284Sjhb panic("%s: lock (%s) %s is not initialized", __func__, 49174912Sjhb lock->lo_class->lc_name, lock->lo_name); 49274912Sjhb 49376272Sjhb /* XXX: need to verify that no one holds the lock */ 49475362Sjhb w = lock->lo_witness; 49575362Sjhb if (w != NULL) { 49675362Sjhb mtx_lock_spin(&w_mtx); 49797948Sjhb MPASS(w->w_refcount > 0); 49875362Sjhb w->w_refcount--; 499112117Sjhb 500112117Sjhb /* 501112117Sjhb * Lock is already released if we have an allocation failure 502112117Sjhb * and depart() fails. 503112117Sjhb */ 504112117Sjhb if (w->w_refcount != 0 || depart(w)) 505112117Sjhb mtx_unlock_spin(&w_mtx); 50675362Sjhb } 50775362Sjhb 50874912Sjhb mtx_lock(&all_mtx); 50974912Sjhb lock_cur_cnt--; 51097963Sjhb TAILQ_REMOVE(&all_locks, lock, lo_list); 51180055Sjhb lock->lo_flags &= ~LO_INITIALIZED; 51274912Sjhb mtx_unlock(&all_mtx); 51374912Sjhb} 51474912Sjhb 515112115Sjhb#ifdef DDB 51671352Sjasonestatic void 51774912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...), 51874912Sjhb struct witness_list *list) 51971352Sjasone{ 520112118Sjhb struct witness *w; 52171352Sjasone 52274912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 523112118Sjhb if (w->w_file == NULL || w->w_level > 0) 52471352Sjasone continue; 52571352Sjasone /* 52671352Sjasone * This lock has no anscestors, display its descendants. 52771352Sjasone */ 528112118Sjhb witness_displaydescendants(prnt, w, 0); 52971352Sjasone } 53074912Sjhb} 53172224Sjhb 53274912Sjhbstatic void 53374912Sjhbwitness_display(void(*prnt)(const char *fmt, ...)) 53474912Sjhb{ 53574912Sjhb struct witness *w; 53674912Sjhb 53782284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 53874912Sjhb witness_levelall(); 53974912Sjhb 540112118Sjhb /* Clear all the displayed flags. */ 541112118Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 542112118Sjhb w->w_displayed = 0; 543112118Sjhb } 544112118Sjhb 54572224Sjhb /* 54674930Sjhb * First, handle sleep locks which have been acquired at least 54774912Sjhb * once. 54874912Sjhb */ 54974912Sjhb prnt("Sleep locks:\n"); 55074912Sjhb witness_display_list(prnt, &w_sleep); 55174912Sjhb 55274912Sjhb /* 55374930Sjhb * Now do spin locks which have been acquired at least once. 55472224Sjhb */ 55574912Sjhb prnt("\nSpin locks:\n"); 55674912Sjhb witness_display_list(prnt, &w_spin); 55772224Sjhb 55872224Sjhb /* 55974930Sjhb * Finally, any locks which have not been acquired yet. 56072224Sjhb */ 56174912Sjhb prnt("\nLocks which were never acquired:\n"); 56274912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 56397948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 56471352Sjasone continue; 56574912Sjhb prnt("%s\n", w->w_name); 56671352Sjasone } 56771352Sjasone} 568112115Sjhb#endif /* DDB */ 56971352Sjasone 570112116Sjhb/* Trim useless garbage from filenames. */ 571112116Sjhbstatic const char * 572112116Sjhbfixup_filename(const char *file) 573112116Sjhb{ 574112116Sjhb 575112116Sjhb if (file == NULL) 576112116Sjhb return (NULL); 577112116Sjhb while (strncmp(file, "../", 3) == 0) 578112116Sjhb file += 3; 579112116Sjhb return (file); 580112116Sjhb} 581112116Sjhb 58265557Sjasonevoid 58374912Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 58465557Sjasone{ 58574912Sjhb struct lock_list_entry **lock_list, *lle; 58676272Sjhb struct lock_instance *lock1, *lock2; 58774912Sjhb struct lock_class *class; 58865856Sjhb struct witness *w, *w1; 58983366Sjulian struct thread *td; 59074912Sjhb int i, j; 59167676Sjhb#ifdef DDB 59267676Sjhb int go_into_ddb = 0; 593112115Sjhb#endif 59465557Sjasone 595112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 59680747Sjhb panicstr != NULL) 59771320Sjasone return; 59874912Sjhb w = lock->lo_witness; 59974912Sjhb class = lock->lo_class; 60083366Sjulian td = curthread; 601112116Sjhb file = fixup_filename(file); 60265557Sjasone 60374912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 60493676Sjhb /* 60593676Sjhb * Since spin locks include a critical section, this check 60693676Sjhb * impliclty enforces a lock order of all sleep locks before 60793676Sjhb * all spin locks. 60893676Sjhb */ 60988899Sjhb if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0) 61074912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 61174912Sjhb class->lc_name, lock->lo_name, file, line); 61283366Sjulian lock_list = &td->td_sleeplocks; 61388899Sjhb } else 61488899Sjhb lock_list = PCPU_PTR(spinlocks); 61565557Sjasone 61676772Sjhb /* 61774912Sjhb * Is this the first lock acquired? If so, then no order checking 61874912Sjhb * is needed. 61965557Sjasone */ 62074912Sjhb if (*lock_list == NULL) 62165557Sjasone goto out; 62265557Sjasone 62374912Sjhb /* 62476272Sjhb * Check to see if we are recursing on a lock we already own. 62576272Sjhb */ 62676272Sjhb lock1 = find_instance(*lock_list, lock); 62776272Sjhb if (lock1 != NULL) { 62876272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 62976272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 63076272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 63176272Sjhb class->lc_name, lock->lo_name, file, line); 63276272Sjhb printf("while exclusively locked from %s:%d\n", 63376272Sjhb lock1->li_file, lock1->li_line); 63476272Sjhb panic("share->excl"); 63576272Sjhb } 63676272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 63776272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 63876272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 63976272Sjhb class->lc_name, lock->lo_name, file, line); 64076272Sjhb printf("while share locked from %s:%d\n", 64176272Sjhb lock1->li_file, lock1->li_line); 64276272Sjhb panic("excl->share"); 64376272Sjhb } 64476272Sjhb lock1->li_flags++; 64576272Sjhb if ((lock->lo_flags & LO_RECURSABLE) == 0) { 64676272Sjhb printf( 64776272Sjhb "recursed on non-recursive lock (%s) %s @ %s:%d\n", 64876272Sjhb class->lc_name, lock->lo_name, file, line); 64976272Sjhb printf("first acquired @ %s:%d\n", lock1->li_file, 65076272Sjhb lock1->li_line); 65176272Sjhb panic("recurse"); 65276272Sjhb } 65387593Sobrien CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 65484680Sjhb td->td_proc->p_pid, lock->lo_name, 65578785Sjhb lock1->li_flags & LI_RECURSEMASK); 65676272Sjhb lock1->li_file = file; 65776272Sjhb lock1->li_line = line; 65876272Sjhb return; 65976272Sjhb } 66076272Sjhb 66176272Sjhb /* 662112112Sjhb * Try locks do not block if they fail to acquire the lock, thus 663112112Sjhb * there is no danger of deadlocks or of switching while holding a 664112112Sjhb * spin lock if we acquire a lock via a try operation. 665112112Sjhb */ 666112112Sjhb if (flags & LOP_TRYLOCK) 667112112Sjhb goto out; 668112112Sjhb 669112112Sjhb /* 67074912Sjhb * Check for duplicate locks of the same type. Note that we only 67174912Sjhb * have to check for this on the last lock we just acquired. Any 67274912Sjhb * other cases will be caught as lock order violations. 67374912Sjhb */ 67476272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 67576272Sjhb w1 = lock1->li_lock->lo_witness; 67674912Sjhb if (w1 == w) { 67793273Sjeff if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK)) 67865557Sjasone goto out; 67965557Sjasone w->w_same_squawked = 1; 68075755Sjhb printf("acquiring duplicate lock of same type: \"%s\"\n", 68193811Sjhb lock->lo_type); 68293811Sjhb printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 68393811Sjhb lock1->li_file, lock1->li_line); 68493811Sjhb printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 68567676Sjhb#ifdef DDB 68667676Sjhb go_into_ddb = 1; 687112115Sjhb#endif 68865557Sjasone goto out; 68965557Sjasone } 69065557Sjasone MPASS(!mtx_owned(&w_mtx)); 69174912Sjhb mtx_lock_spin(&w_mtx); 69265557Sjasone /* 69365557Sjasone * If we have a known higher number just say ok 69465557Sjasone */ 69565557Sjasone if (witness_watch > 1 && w->w_level > w1->w_level) { 69674912Sjhb mtx_unlock_spin(&w_mtx); 69765557Sjasone goto out; 69865557Sjasone } 699111881Sjhb /* 700111881Sjhb * If we know that the the lock we are acquiring comes after 701111881Sjhb * the lock we most recently acquired in the lock order tree, 702111881Sjhb * then there is no need for any further checks. 703111881Sjhb */ 70474912Sjhb if (isitmydescendant(w1, w)) { 70574912Sjhb mtx_unlock_spin(&w_mtx); 70665557Sjasone goto out; 70765557Sjasone } 70874912Sjhb for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 70974912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 71065557Sjasone 71174912Sjhb MPASS(j < WITNESS_COUNT); 71276272Sjhb lock1 = &lle->ll_children[i]; 71376272Sjhb w1 = lock1->li_lock->lo_witness; 71474912Sjhb 71574912Sjhb /* 71674912Sjhb * If this lock doesn't undergo witness checking, 71774912Sjhb * then skip it. 71874912Sjhb */ 71974912Sjhb if (w1 == NULL) { 72076272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 72174912Sjhb ("lock missing witness structure")); 72274912Sjhb continue; 72374912Sjhb } 72476272Sjhb /* 725111881Sjhb * If we are locking Giant and this is a sleepable 72676272Sjhb * lock, then skip it. 72776272Sjhb */ 728111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 72976272Sjhb lock == &Giant.mtx_object) 73076272Sjhb continue; 73193690Sjhb /* 73293690Sjhb * If we are locking a sleepable lock and this lock 733111881Sjhb * is Giant, then skip it. 73493690Sjhb */ 735111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 736111887Sjhb lock1->li_lock == &Giant.mtx_object) 737111881Sjhb continue; 738111881Sjhb /* 739111881Sjhb * If we are locking a sleepable lock and this lock 740111881Sjhb * isn't sleepable, we want to treat it as a lock 741111881Sjhb * order violation to enfore a general lock order of 742111881Sjhb * sleepable locks before non-sleepable locks. 743111881Sjhb */ 74493690Sjhb if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 745111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 746111881Sjhb /* 747111881Sjhb * Check the lock order hierarchy for a reveresal. 748111881Sjhb */ 749111881Sjhb if (!isitmydescendant(w, w1)) 75074912Sjhb continue; 75174912Sjhb /* 75274912Sjhb * We have a lock order violation, check to see if it 75374912Sjhb * is allowed or has already been yelled about. 75474912Sjhb */ 75574912Sjhb mtx_unlock_spin(&w_mtx); 756105508Sphk#ifdef BLESSING 75765557Sjasone if (blessed(w, w1)) 75865557Sjasone goto out; 759105508Sphk#endif 76076272Sjhb if (lock1->li_lock == &Giant.mtx_object) { 76165557Sjasone if (w1->w_Giant_squawked) 76265557Sjasone goto out; 76365557Sjasone else 76465557Sjasone w1->w_Giant_squawked = 1; 76565557Sjasone } else { 76665557Sjasone if (w1->w_other_squawked) 76765557Sjasone goto out; 76865557Sjasone else 76965557Sjasone w1->w_other_squawked = 1; 77065557Sjasone } 77174912Sjhb /* 77274912Sjhb * Ok, yell about it. 77374912Sjhb */ 77465557Sjasone printf("lock order reversal\n"); 77574912Sjhb /* 77674912Sjhb * Try to locate an earlier lock with 77774912Sjhb * witness w in our list. 77874912Sjhb */ 77974912Sjhb do { 78076272Sjhb lock2 = &lle->ll_children[i]; 78176272Sjhb MPASS(lock2->li_lock != NULL); 78276272Sjhb if (lock2->li_lock->lo_witness == w) 78374912Sjhb break; 78474912Sjhb i--; 78574912Sjhb if (i == 0 && lle->ll_next != NULL) { 78674912Sjhb lle = lle->ll_next; 78774912Sjhb i = lle->ll_count - 1; 788106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 78974912Sjhb } 79074912Sjhb } while (i >= 0); 79176272Sjhb if (i < 0) { 79293811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 79393811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 79493811Sjhb lock1->li_lock->lo_type, lock1->li_file, 79576272Sjhb lock1->li_line); 79693811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 79793811Sjhb lock->lo_name, lock->lo_type, file, line); 79876272Sjhb } else { 79993811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 80093811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 80193811Sjhb lock2->li_lock->lo_type, lock2->li_file, 80276272Sjhb lock2->li_line); 80393811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 80493811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 80593811Sjhb lock1->li_lock->lo_type, lock1->li_file, 80676272Sjhb lock1->li_line); 80793811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 80893811Sjhb lock->lo_name, lock->lo_type, file, line); 80976272Sjhb } 81067676Sjhb#ifdef DDB 81167676Sjhb go_into_ddb = 1; 812112115Sjhb#endif 81365557Sjasone goto out; 81465557Sjasone } 81565557Sjasone } 81676272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 81778871Sjhb /* 818111881Sjhb * Don't build a new relationship between a sleepable lock and 819111881Sjhb * Giant if it is the wrong direction. The real lock order is that 820111881Sjhb * sleepable locks come before Giant. 82178871Sjhb */ 822112117Sjhb if (!(lock1->li_lock == &Giant.mtx_object && 823112117Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0)) { 82487593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 82593811Sjhb lock->lo_type, lock1->li_lock->lo_type); 82678871Sjhb if (!itismychild(lock1->li_lock->lo_witness, w)) 827112117Sjhb /* Witness is dead. */ 828112117Sjhb return; 82978871Sjhb } 830112117Sjhb mtx_unlock_spin(&w_mtx); 83165557Sjasone 83265557Sjasoneout: 83367676Sjhb#ifdef DDB 834110779Speter if (go_into_ddb) { 835110779Speter if (witness_trace) 836110779Speter backtrace(); 837110779Speter if (witness_ddb) 838110779Speter Debugger(__func__); 839110779Speter } 840112115Sjhb#endif 84165557Sjasone w->w_file = file; 84265557Sjasone w->w_line = line; 84374912Sjhb 84474912Sjhb lle = *lock_list; 84576272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 84678785Sjhb lle = witness_lock_list_get(); 84778785Sjhb if (lle == NULL) 84865557Sjasone return; 84978785Sjhb lle->ll_next = *lock_list; 85087593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 85184680Sjhb td->td_proc->p_pid, lle); 85278785Sjhb *lock_list = lle; 85365557Sjasone } 85476272Sjhb lock1 = &lle->ll_children[lle->ll_count++]; 85576272Sjhb lock1->li_lock = lock; 85676272Sjhb lock1->li_line = line; 85776272Sjhb lock1->li_file = file; 85876272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 85976272Sjhb lock1->li_flags = LI_EXCLUSIVE; 86076272Sjhb else 86176272Sjhb lock1->li_flags = 0; 86287593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 86384680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 86465557Sjasone} 86565557Sjasone 86665557Sjasonevoid 86782244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 86882244Sjhb{ 86982244Sjhb struct lock_instance *instance; 87082244Sjhb struct lock_class *class; 87182244Sjhb 87282284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 873112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 87482244Sjhb return; 87582244Sjhb class = lock->lo_class; 876112116Sjhb file = fixup_filename(file); 87782244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 87882244Sjhb panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 87982244Sjhb class->lc_name, lock->lo_name, file, line); 88082244Sjhb if ((flags & LOP_TRYLOCK) == 0) 88182244Sjhb panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 88282244Sjhb lock->lo_name, file, line); 88382244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 88482244Sjhb panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 88582244Sjhb class->lc_name, lock->lo_name, file, line); 88683366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 88782244Sjhb if (instance == NULL) 88882244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 88982244Sjhb class->lc_name, lock->lo_name, file, line); 89082244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0) 89182244Sjhb panic("upgrade of exclusive lock (%s) %s @ %s:%d", 89282244Sjhb class->lc_name, lock->lo_name, file, line); 89382244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 89482244Sjhb panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 89582244Sjhb class->lc_name, lock->lo_name, 89682244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 89782244Sjhb instance->li_flags |= LI_EXCLUSIVE; 89882244Sjhb} 89982244Sjhb 90082244Sjhbvoid 90182244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 90282244Sjhb int line) 90382244Sjhb{ 90482244Sjhb struct lock_instance *instance; 90582244Sjhb struct lock_class *class; 90682244Sjhb 90782284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 908112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 90982244Sjhb return; 91082244Sjhb class = lock->lo_class; 911112116Sjhb file = fixup_filename(file); 91282244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 91382244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 91482244Sjhb class->lc_name, lock->lo_name, file, line); 91582244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 91682244Sjhb panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 91782244Sjhb class->lc_name, lock->lo_name, file, line); 91883366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 91982244Sjhb if (instance == NULL) 92082244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 92182244Sjhb class->lc_name, lock->lo_name, file, line); 92282244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0) 92382244Sjhb panic("downgrade of shared lock (%s) %s @ %s:%d", 92482244Sjhb class->lc_name, lock->lo_name, file, line); 92582244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 92682244Sjhb panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 92782244Sjhb class->lc_name, lock->lo_name, 92882244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 92982244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 93082244Sjhb} 93182244Sjhb 93282244Sjhbvoid 93374912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 93465557Sjasone{ 93574912Sjhb struct lock_list_entry **lock_list, *lle; 93676272Sjhb struct lock_instance *instance; 93774912Sjhb struct lock_class *class; 93883366Sjulian struct thread *td; 93992858Simp register_t s; 94074912Sjhb int i, j; 94165557Sjasone 942112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 94380747Sjhb panicstr != NULL) 94471352Sjasone return; 94583366Sjulian td = curthread; 94674912Sjhb class = lock->lo_class; 947112116Sjhb file = fixup_filename(file); 94876272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 94983366Sjulian lock_list = &td->td_sleeplocks; 95076272Sjhb else 95174912Sjhb lock_list = PCPU_PTR(spinlocks); 95274912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 95376272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 95476272Sjhb instance = &(*lock_list)->ll_children[i]; 95576272Sjhb if (instance->li_lock == lock) { 95676272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 95776272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 95876272Sjhb printf( 95976272Sjhb "shared unlock of (%s) %s @ %s:%d\n", 96076272Sjhb class->lc_name, lock->lo_name, 96176272Sjhb file, line); 96276272Sjhb printf( 96376272Sjhb "while exclusively locked from %s:%d\n", 96476272Sjhb instance->li_file, 96576272Sjhb instance->li_line); 96676272Sjhb panic("excl->ushare"); 96776272Sjhb } 96876272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 96976272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 97076272Sjhb printf( 97176272Sjhb "exclusive unlock of (%s) %s @ %s:%d\n", 97276272Sjhb class->lc_name, lock->lo_name, 97376272Sjhb file, line); 97476272Sjhb printf( 97576272Sjhb "while share locked from %s:%d\n", 97676272Sjhb instance->li_file, 97776272Sjhb instance->li_line); 97876272Sjhb panic("share->uexcl"); 97976272Sjhb } 98076272Sjhb /* If we are recursed, unrecurse. */ 98176272Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 98287593Sobrien CTR4(KTR_WITNESS, 98387593Sobrien "%s: pid %d unrecursed on %s r=%d", __func__, 98484680Sjhb td->td_proc->p_pid, 98578785Sjhb instance->li_lock->lo_name, 98678785Sjhb instance->li_flags); 98776272Sjhb instance->li_flags--; 98888900Sjhb return; 98976272Sjhb } 99092858Simp s = intr_disable(); 99187593Sobrien CTR4(KTR_WITNESS, 99287593Sobrien "%s: pid %d removed %s from lle[%d]", __func__, 99384680Sjhb td->td_proc->p_pid, 99484680Sjhb instance->li_lock->lo_name, 99578785Sjhb (*lock_list)->ll_count - 1); 99697014Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 99774912Sjhb (*lock_list)->ll_children[j] = 99874912Sjhb (*lock_list)->ll_children[j + 1]; 99997014Sjhb (*lock_list)->ll_count--; 100092858Simp intr_restore(s); 100174912Sjhb if ((*lock_list)->ll_count == 0) { 100274912Sjhb lle = *lock_list; 100374912Sjhb *lock_list = lle->ll_next; 100487593Sobrien CTR3(KTR_WITNESS, 100587593Sobrien "%s: pid %d removed lle %p", __func__, 100684680Sjhb td->td_proc->p_pid, lle); 100774912Sjhb witness_lock_list_free(lle); 100874912Sjhb } 100988900Sjhb return; 101074912Sjhb } 101176272Sjhb } 101276272Sjhb panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 101376272Sjhb file, line); 101465557Sjasone} 101565557Sjasone 101674912Sjhb/* 1017111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 1018111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 1019111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 1020111881Sjhb * console along with a list of the offending locks. If indicated in the 1021111881Sjhb * flags then a failure results in a panic as well. 102274912Sjhb */ 102365557Sjasoneint 1024111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 102565557Sjasone{ 1026111881Sjhb struct lock_list_entry *lle; 102776272Sjhb struct lock_instance *lock1; 102883366Sjulian struct thread *td; 1029111881Sjhb va_list ap; 103074912Sjhb int i, n; 103165557Sjasone 1032112562Sjhb if (witness_cold || witness_watch == 0 || panicstr != NULL) 103374912Sjhb return (0); 103474912Sjhb n = 0; 103583366Sjulian td = curthread; 1036111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 103774912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 103876272Sjhb lock1 = &lle->ll_children[i]; 1039111881Sjhb if (lock1->li_lock == lock) 1040111881Sjhb continue; 1041111881Sjhb if (flags & WARN_GIANTOK && 104276272Sjhb lock1->li_lock == &Giant.mtx_object) 104374912Sjhb continue; 1044111881Sjhb if (flags & WARN_SLEEPOK && 1045111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 104676272Sjhb continue; 1047111881Sjhb if (n == 0) { 1048111881Sjhb va_start(ap, fmt); 1049111881Sjhb vprintf(fmt, ap); 1050111881Sjhb va_end(ap); 1051111881Sjhb printf(" with the following"); 1052111881Sjhb if (flags & WARN_SLEEPOK) 1053111881Sjhb printf(" non-sleepable"); 1054111881Sjhb printf("locks held:\n"); 105576272Sjhb } 105674912Sjhb n++; 1057111881Sjhb witness_list_lock(lock1); 105874912Sjhb } 1059111881Sjhb if (PCPU_GET(spinlocks) != NULL) { 106097006Sjhb /* 106197006Sjhb * Since we already hold a spinlock preemption is 106297006Sjhb * already blocked. 106397006Sjhb */ 1064111881Sjhb if (n == 0) { 1065111881Sjhb va_start(ap, fmt); 1066111881Sjhb vprintf(fmt, ap); 1067111881Sjhb va_end(ap); 1068111881Sjhb printf(" with the following"); 1069111881Sjhb if (flags & WARN_SLEEPOK) 1070111881Sjhb printf(" non-sleepable"); 1071111881Sjhb printf("locks held:\n"); 1072111881Sjhb } 1073111881Sjhb n += witness_list_locks(PCPU_PTR(spinlocks)); 107465557Sjasone } 1075111881Sjhb if (flags & WARN_PANIC && n) 1076111881Sjhb panic("witness_warn"); 107767676Sjhb#ifdef DDB 1078111881Sjhb else if (witness_ddb && n) 107975711Sjhb Debugger(__func__); 1080111881Sjhb#endif 108165557Sjasone return (n); 108265557Sjasone} 108365557Sjasone 1084102448Siedowseconst char * 1085102448Siedowsewitness_file(struct lock_object *lock) 1086102448Siedowse{ 1087102448Siedowse struct witness *w; 1088102448Siedowse 1089112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1090102448Siedowse return ("?"); 1091102448Siedowse w = lock->lo_witness; 1092102448Siedowse return (w->w_file); 1093102448Siedowse} 1094102448Siedowse 1095102448Siedowseint 1096102448Siedowsewitness_line(struct lock_object *lock) 1097102448Siedowse{ 1098102448Siedowse struct witness *w; 1099102448Siedowse 1100112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1101102448Siedowse return (0); 1102102448Siedowse w = lock->lo_witness; 1103102448Siedowse return (w->w_line); 1104102448Siedowse} 1105102448Siedowse 110665856Sjhbstatic struct witness * 110774912Sjhbenroll(const char *description, struct lock_class *lock_class) 110865557Sjasone{ 110974912Sjhb struct witness *w; 111065557Sjasone 1111112562Sjhb if (!witness_watch || witness_watch == 0 || panicstr != NULL) 111265557Sjasone return (NULL); 111374912Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 111465557Sjasone return (NULL); 111574912Sjhb mtx_lock_spin(&w_mtx); 111674912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 111797948Sjhb if (w->w_name == description || (w->w_refcount > 0 && 111897948Sjhb strcmp(description, w->w_name) == 0)) { 111975362Sjhb w->w_refcount++; 112074912Sjhb mtx_unlock_spin(&w_mtx); 112174912Sjhb if (lock_class != w->w_class) 112274912Sjhb panic( 112374912Sjhb "lock (%s) %s does not match earlier (%s) lock", 112474912Sjhb description, lock_class->lc_name, 112574912Sjhb w->w_class->lc_name); 112665557Sjasone return (w); 112765557Sjasone } 112865557Sjasone } 112974912Sjhb /* 113074912Sjhb * This isn't quite right, as witness_cold is still 0 while we 113174912Sjhb * enroll all the locks initialized before witness_initialize(). 113274912Sjhb */ 113375364Sbp if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 113475364Sbp mtx_unlock_spin(&w_mtx); 113574912Sjhb panic("spin lock %s not in order list", description); 113675364Sbp } 113765557Sjasone if ((w = witness_get()) == NULL) 113865557Sjasone return (NULL); 113974912Sjhb w->w_name = description; 114074912Sjhb w->w_class = lock_class; 114175362Sjhb w->w_refcount = 1; 114274912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 114374912Sjhb if (lock_class->lc_flags & LC_SPINLOCK) 114474912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 114574912Sjhb else if (lock_class->lc_flags & LC_SLEEPLOCK) 114674912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 114775364Sbp else { 114875364Sbp mtx_unlock_spin(&w_mtx); 114974912Sjhb panic("lock class %s is not sleep or spin", 115074912Sjhb lock_class->lc_name); 115175364Sbp } 115274912Sjhb mtx_unlock_spin(&w_mtx); 115365557Sjasone return (w); 115465557Sjasone} 115565557Sjasone 1156112117Sjhb/* Don't let the door bang you on the way out... */ 115765557Sjasonestatic int 1158112117Sjhbdepart(struct witness *w) 115965557Sjasone{ 1160112117Sjhb struct witness_child_list_entry *wcl, *nwcl; 116174912Sjhb struct witness_list *list; 1162112117Sjhb struct witness *parent; 116365557Sjasone 1164112117Sjhb MPASS(w->w_refcount == 0); 1165112117Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 1166112117Sjhb list = &w_sleep; 1167112117Sjhb else 1168112117Sjhb list = &w_spin; 1169112117Sjhb /* 1170112117Sjhb * First, we run through the entire tree looking for any 1171112117Sjhb * witnesses that the outgoing witness is a child of. For 1172112117Sjhb * each parent that we find, we reparent all the direct 1173112117Sjhb * children of the outgoing witness to its parent. 1174112117Sjhb */ 1175112117Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 1176112117Sjhb if (!isitmychild(parent, w)) 1177112117Sjhb continue; 1178112117Sjhb removechild(parent, w); 1179112117Sjhb if (!reparentchildren(parent, w)) 1180112117Sjhb return (0); 1181112117Sjhb } 1182112117Sjhb 1183112117Sjhb /* 1184112117Sjhb * Now we go through and free up the child list of the 1185112117Sjhb * outgoing witness. 1186112117Sjhb */ 1187112117Sjhb for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1188112117Sjhb nwcl = wcl->wcl_next; 1189112117Sjhb witness_child_free(wcl); 1190112117Sjhb } 1191112117Sjhb 1192112117Sjhb /* 1193112117Sjhb * Detach from various lists and free. 1194112117Sjhb */ 1195112117Sjhb STAILQ_REMOVE(list, w, witness, w_typelist); 1196112117Sjhb STAILQ_REMOVE(&w_all, w, witness, w_list); 1197112117Sjhb witness_free(w); 1198112117Sjhb 1199112117Sjhb /* Finally, fixup the tree. */ 1200112117Sjhb return (rebalancetree(list)); 1201112117Sjhb} 1202112117Sjhb 1203112117Sjhb/* 1204112117Sjhb * Prune an entire lock order tree. We look for cases where a lock 1205112117Sjhb * is now both a descendant and a direct child of a given lock. In 1206112117Sjhb * that case, we want to remove the direct child link from the tree. 1207112117Sjhb * 1208112117Sjhb * Returns false if insertchild() fails. 1209112117Sjhb */ 1210112117Sjhbstatic int 1211112117Sjhbrebalancetree(struct witness_list *list) 1212112117Sjhb{ 1213112117Sjhb struct witness *child, *parent; 1214112117Sjhb 1215112117Sjhb STAILQ_FOREACH(child, list, w_typelist) { 1216112117Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 1217112117Sjhb if (!isitmychild(parent, child)) 1218112117Sjhb continue; 1219112117Sjhb removechild(parent, child); 1220112117Sjhb if (isitmydescendant(parent, child)) 1221112117Sjhb continue; 1222112117Sjhb if (!insertchild(parent, child)) 1223112117Sjhb return (0); 1224112117Sjhb } 1225112117Sjhb } 1226112117Sjhb witness_levelall(); 1227112117Sjhb return (1); 1228112117Sjhb} 1229112117Sjhb 1230112117Sjhb/* 1231112117Sjhb * Add "child" as a direct child of "parent". Returns false if 1232112117Sjhb * we fail due to out of memory. 1233112117Sjhb */ 1234112117Sjhbstatic int 1235112117Sjhbinsertchild(struct witness *parent, struct witness *child) 1236112117Sjhb{ 1237112117Sjhb struct witness_child_list_entry **wcl; 1238112117Sjhb 123974912Sjhb MPASS(child != NULL && parent != NULL); 124074912Sjhb 124165557Sjasone /* 124265557Sjasone * Insert "child" after "parent" 124365557Sjasone */ 124474912Sjhb wcl = &parent->w_children; 124574912Sjhb while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 124674912Sjhb wcl = &(*wcl)->wcl_next; 124774912Sjhb if (*wcl == NULL) { 124874912Sjhb *wcl = witness_child_get(); 124974912Sjhb if (*wcl == NULL) 1250112117Sjhb return (0); 125165557Sjasone } 125274912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 125374912Sjhb 1254112117Sjhb return (1); 1255112117Sjhb} 1256112117Sjhb 1257112117Sjhb/* 1258112117Sjhb * Make all the direct descendants of oldparent be direct descendants 1259112117Sjhb * of newparent. 1260112117Sjhb */ 1261112117Sjhbstatic int 1262112117Sjhbreparentchildren(struct witness *newparent, struct witness *oldparent) 1263112117Sjhb{ 1264112117Sjhb struct witness_child_list_entry *wcl; 1265112117Sjhb int i; 1266112117Sjhb 1267112117Sjhb /* Avoid making a witness a child of itself. */ 1268112117Sjhb MPASS(!isitmychild(oldparent, newparent)); 1269112117Sjhb 1270112117Sjhb for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1271112117Sjhb for (i = 0; i < wcl->wcl_count; i++) 1272112117Sjhb if (!insertchild(newparent, wcl->wcl_children[i])) 1273112117Sjhb return (0); 1274112117Sjhb return (1); 1275112117Sjhb} 1276112117Sjhb 1277112117Sjhbstatic int 1278112117Sjhbitismychild(struct witness *parent, struct witness *child) 1279112117Sjhb{ 1280112117Sjhb struct witness_list *list; 1281112117Sjhb 1282112117Sjhb MPASS(child != NULL && parent != NULL); 1283112117Sjhb if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1284112117Sjhb (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1285112117Sjhb panic( 1286112117Sjhb "%s: parent (%s) and child (%s) are not the same lock type", 1287112117Sjhb __func__, parent->w_class->lc_name, 1288112117Sjhb child->w_class->lc_name); 1289112117Sjhb 1290112117Sjhb if (!insertchild(parent, child)) 129165557Sjasone return (0); 1292112117Sjhb 129374912Sjhb if (parent->w_class->lc_flags & LC_SLEEPLOCK) 129474912Sjhb list = &w_sleep; 129574912Sjhb else 129674912Sjhb list = &w_spin; 1297112117Sjhb return (rebalancetree(list)); 129865557Sjasone} 129965557Sjasone 130065557Sjasonestatic void 130165856Sjhbremovechild(struct witness *parent, struct witness *child) 130265557Sjasone{ 130374912Sjhb struct witness_child_list_entry **wcl, *wcl1; 130465557Sjasone int i; 130565557Sjasone 130674912Sjhb for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 130774912Sjhb for (i = 0; i < (*wcl)->wcl_count; i++) 130874912Sjhb if ((*wcl)->wcl_children[i] == child) 130965557Sjasone goto found; 131065557Sjasone return; 131165557Sjasonefound: 131274912Sjhb (*wcl)->wcl_count--; 131374912Sjhb if ((*wcl)->wcl_count > i) 131474912Sjhb (*wcl)->wcl_children[i] = 131574912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count]; 131674912Sjhb MPASS((*wcl)->wcl_children[i] != NULL); 131774912Sjhb if ((*wcl)->wcl_count != 0) 131865557Sjasone return; 131974912Sjhb wcl1 = *wcl; 132074912Sjhb *wcl = wcl1->wcl_next; 132174912Sjhb witness_child_free(wcl1); 132265557Sjasone} 132365557Sjasone 132465557Sjasonestatic int 132565856Sjhbisitmychild(struct witness *parent, struct witness *child) 132665557Sjasone{ 132774912Sjhb struct witness_child_list_entry *wcl; 132865557Sjasone int i; 132965557Sjasone 133074912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 133174912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 133274912Sjhb if (wcl->wcl_children[i] == child) 133365557Sjasone return (1); 133465557Sjasone } 133565557Sjasone } 133665557Sjasone return (0); 133765557Sjasone} 133865557Sjasone 133965557Sjasonestatic int 134065856Sjhbisitmydescendant(struct witness *parent, struct witness *child) 134165557Sjasone{ 134274912Sjhb struct witness_child_list_entry *wcl; 134374912Sjhb int i, j; 134465557Sjasone 134574912Sjhb if (isitmychild(parent, child)) 134674912Sjhb return (1); 134774912Sjhb j = 0; 134874912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 134967352Sjhb MPASS(j < 1000); 135074912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 135174912Sjhb if (isitmydescendant(wcl->wcl_children[i], child)) 135265557Sjasone return (1); 135365557Sjasone } 135474912Sjhb j++; 135565557Sjasone } 135665557Sjasone return (0); 135765557Sjasone} 135865557Sjasone 1359104094Sphkstatic void 136065557Sjasonewitness_levelall (void) 136165557Sjasone{ 136274912Sjhb struct witness_list *list; 136365856Sjhb struct witness *w, *w1; 136465557Sjasone 136574912Sjhb /* 136674912Sjhb * First clear all levels. 136774912Sjhb */ 136874912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 136974912Sjhb w->w_level = 0; 137074912Sjhb } 137174912Sjhb 137274912Sjhb /* 137374912Sjhb * Look for locks with no parent and level all their descendants. 137474912Sjhb */ 137574912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 137674912Sjhb /* 137774912Sjhb * This is just an optimization, technically we could get 137874912Sjhb * away just walking the all list each time. 137974912Sjhb */ 138074912Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 138174912Sjhb list = &w_sleep; 138274912Sjhb else 138374912Sjhb list = &w_spin; 138474912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 138565557Sjasone if (isitmychild(w1, w)) 138674912Sjhb goto skip; 138765557Sjasone } 138865557Sjasone witness_leveldescendents(w, 0); 138974912Sjhb skip: 139095541Smarcel ; /* silence GCC 3.x */ 139165557Sjasone } 139265557Sjasone} 139365557Sjasone 139465557Sjasonestatic void 139565856Sjhbwitness_leveldescendents(struct witness *parent, int level) 139665557Sjasone{ 139774912Sjhb struct witness_child_list_entry *wcl; 139865557Sjasone int i; 139965557Sjasone 140065557Sjasone if (parent->w_level < level) 140165557Sjasone parent->w_level = level; 140265557Sjasone level++; 140374912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 140474912Sjhb for (i = 0; i < wcl->wcl_count; i++) 140574912Sjhb witness_leveldescendents(wcl->wcl_children[i], level); 140665557Sjasone} 140765557Sjasone 140865557Sjasonestatic void 140965856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...), 1410112118Sjhb struct witness *parent, int indent) 141165557Sjasone{ 141274912Sjhb struct witness_child_list_entry *wcl; 141374912Sjhb int i, level; 141465557Sjasone 141595543Sjhb level = parent->w_level; 141674912Sjhb prnt("%-2d", level); 1417112118Sjhb for (i = 0; i < indent; i++) 141865557Sjasone prnt(" "); 1419112118Sjhb if (parent->w_refcount > 0) 1420112118Sjhb prnt("%s", parent->w_name); 1421112118Sjhb else 1422112118Sjhb prnt("(dead)"); 1423112118Sjhb if (parent->w_displayed) { 1424112118Sjhb prnt(" -- (already displayed)\n"); 1425112118Sjhb return; 1426112118Sjhb } 1427112118Sjhb parent->w_displayed = 1; 142897948Sjhb if (parent->w_refcount > 0) { 142997948Sjhb if (parent->w_file != NULL) 1430112118Sjhb prnt(" -- last acquired @ %s:%d", parent->w_file, 143197948Sjhb parent->w_line); 1432112118Sjhb } 1433112118Sjhb prnt("\n"); 143474912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 143574912Sjhb for (i = 0; i < wcl->wcl_count; i++) 143674912Sjhb witness_displaydescendants(prnt, 1437112118Sjhb wcl->wcl_children[i], indent + 1); 143874912Sjhb} 143965557Sjasone 1440105508Sphk#ifdef BLESSING 144165557Sjasonestatic int 144265856Sjhbblessed(struct witness *w1, struct witness *w2) 144365557Sjasone{ 144465557Sjasone int i; 144565856Sjhb struct witness_blessed *b; 144665557Sjasone 144765557Sjasone for (i = 0; i < blessed_count; i++) { 144865557Sjasone b = &blessed_list[i]; 144974912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 145074912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 145165557Sjasone return (1); 145265557Sjasone continue; 145365557Sjasone } 145474912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 145574912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 145665557Sjasone return (1); 145765557Sjasone } 145865557Sjasone return (0); 145965557Sjasone} 1460105508Sphk#endif 146165557Sjasone 146265856Sjhbstatic struct witness * 146374912Sjhbwitness_get(void) 146465557Sjasone{ 146565856Sjhb struct witness *w; 146665557Sjasone 1467112562Sjhb if (witness_watch == 0) { 146876481Sjhb mtx_unlock_spin(&w_mtx); 146976481Sjhb return (NULL); 147076481Sjhb } 147174912Sjhb if (STAILQ_EMPTY(&w_free)) { 1472112562Sjhb witness_watch = 0; 147374912Sjhb mtx_unlock_spin(&w_mtx); 147474912Sjhb printf("%s: witness exhausted\n", __func__); 147565557Sjasone return (NULL); 147665557Sjasone } 147774912Sjhb w = STAILQ_FIRST(&w_free); 147874912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 147965856Sjhb bzero(w, sizeof(*w)); 148065557Sjasone return (w); 148165557Sjasone} 148265557Sjasone 148365557Sjasonestatic void 148465856Sjhbwitness_free(struct witness *w) 148565557Sjasone{ 148674912Sjhb 148774912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 148865557Sjasone} 148965557Sjasone 149074912Sjhbstatic struct witness_child_list_entry * 149174912Sjhbwitness_child_get(void) 149265557Sjasone{ 149374912Sjhb struct witness_child_list_entry *wcl; 149465557Sjasone 1495112562Sjhb if (witness_watch == 0) { 149676481Sjhb mtx_unlock_spin(&w_mtx); 149776481Sjhb return (NULL); 149876481Sjhb } 149974912Sjhb wcl = w_child_free; 150074912Sjhb if (wcl == NULL) { 1501112562Sjhb witness_watch = 0; 150274912Sjhb mtx_unlock_spin(&w_mtx); 150374912Sjhb printf("%s: witness exhausted\n", __func__); 150474912Sjhb return (NULL); 150565557Sjasone } 150674912Sjhb w_child_free = wcl->wcl_next; 150774912Sjhb bzero(wcl, sizeof(*wcl)); 150874912Sjhb return (wcl); 150974912Sjhb} 151069881Sjake 151174912Sjhbstatic void 151274912Sjhbwitness_child_free(struct witness_child_list_entry *wcl) 151374912Sjhb{ 151474912Sjhb 151574912Sjhb wcl->wcl_next = w_child_free; 151674912Sjhb w_child_free = wcl; 151765557Sjasone} 151865557Sjasone 151974912Sjhbstatic struct lock_list_entry * 152074912Sjhbwitness_lock_list_get(void) 152174912Sjhb{ 152274912Sjhb struct lock_list_entry *lle; 152371709Sjhb 1524112562Sjhb if (witness_watch == 0) 152576481Sjhb return (NULL); 152674912Sjhb mtx_lock_spin(&w_mtx); 152774912Sjhb lle = w_lock_list_free; 152874912Sjhb if (lle == NULL) { 1529112562Sjhb witness_watch = 0; 153074912Sjhb mtx_unlock_spin(&w_mtx); 153174912Sjhb printf("%s: witness exhausted\n", __func__); 153274912Sjhb return (NULL); 153374912Sjhb } 153474912Sjhb w_lock_list_free = lle->ll_next; 153574912Sjhb mtx_unlock_spin(&w_mtx); 153674912Sjhb bzero(lle, sizeof(*lle)); 153774912Sjhb return (lle); 153874912Sjhb} 153974912Sjhb 154074912Sjhbstatic void 154174912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 154271709Sjhb{ 154371709Sjhb 154474912Sjhb mtx_lock_spin(&w_mtx); 154574912Sjhb lle->ll_next = w_lock_list_free; 154674912Sjhb w_lock_list_free = lle; 154774912Sjhb mtx_unlock_spin(&w_mtx); 154871709Sjhb} 154971709Sjhb 155076272Sjhbstatic struct lock_instance * 155176272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 155276272Sjhb{ 155376272Sjhb struct lock_list_entry *lle; 155476272Sjhb struct lock_instance *instance; 155576272Sjhb int i; 155676272Sjhb 155776272Sjhb for (lle = lock_list; lle != NULL; lle = lle->ll_next) 155876272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 155976272Sjhb instance = &lle->ll_children[i]; 156076272Sjhb if (instance->li_lock == lock) 156176272Sjhb return (instance); 156276272Sjhb } 156376272Sjhb return (NULL); 156476272Sjhb} 156576272Sjhb 1566111881Sjhbstatic void 1567111881Sjhbwitness_list_lock(struct lock_instance *instance) 1568111881Sjhb{ 1569111881Sjhb struct lock_object *lock; 1570111881Sjhb 1571111881Sjhb lock = instance->li_lock; 1572111881Sjhb printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1573111881Sjhb "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name); 1574111881Sjhb if (lock->lo_type != lock->lo_name) 1575111881Sjhb printf(" (%s)", lock->lo_type); 1576111881Sjhb printf(" r = %d (%p) locked @ %s:%d\n", 1577111881Sjhb instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1578111881Sjhb instance->li_line); 1579111881Sjhb} 1580111881Sjhb 158174912Sjhbint 158275273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 158372224Sjhb{ 158475273Sjhb struct lock_list_entry *lle; 158574912Sjhb int i, nheld; 158672224Sjhb 158774912Sjhb nheld = 0; 158874912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 158974912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 1590111881Sjhb witness_list_lock(&lle->ll_children[i]); 159174912Sjhb nheld++; 159274912Sjhb } 159375273Sjhb return (nheld); 159475273Sjhb} 159575273Sjhb 159665557Sjasonevoid 159774912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 159865557Sjasone{ 159976272Sjhb struct lock_instance *instance; 160071320Sjasone 160182284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1602112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 160371352Sjasone return; 160482243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 160582243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 160682243Sjhb lock->lo_class->lc_name, lock->lo_name); 160783366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 160882243Sjhb if (instance == NULL) 160982243Sjhb panic("%s: lock (%s) %s not locked", __func__, 161082243Sjhb lock->lo_class->lc_name, lock->lo_name); 161176272Sjhb *filep = instance->li_file; 161276272Sjhb *linep = instance->li_line; 161365557Sjasone} 161465557Sjasone 161565557Sjasonevoid 161674912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 161765557Sjasone{ 161876272Sjhb struct lock_instance *instance; 161971320Sjasone 162082284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1621112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 162271352Sjasone return; 162382243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 162482243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 162582243Sjhb lock->lo_class->lc_name, lock->lo_name); 162683366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 162782243Sjhb if (instance == NULL) 162882243Sjhb panic("%s: lock (%s) %s not locked", __func__, 162982243Sjhb lock->lo_class->lc_name, lock->lo_name); 163074912Sjhb lock->lo_witness->w_file = file; 163174912Sjhb lock->lo_witness->w_line = line; 163276272Sjhb instance->li_file = file; 163376272Sjhb instance->li_line = line; 163465557Sjasone} 163565557Sjasone 163678871Sjhbvoid 163778871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 163878871Sjhb{ 163978871Sjhb#ifdef INVARIANT_SUPPORT 164078871Sjhb struct lock_instance *instance; 164178871Sjhb 1642112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 164378941Sjhb return; 164478871Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 164583366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 164678871Sjhb else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 164778871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 164886422Sjhb else { 164978871Sjhb panic("Lock (%s) %s is not sleep or spin!", 165078871Sjhb lock->lo_class->lc_name, lock->lo_name); 165186422Sjhb return; 165286422Sjhb } 1653112116Sjhb file = fixup_filename(file); 165478871Sjhb switch (flags) { 165578871Sjhb case LA_UNLOCKED: 165678871Sjhb if (instance != NULL) 165778871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 165878871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 165978871Sjhb break; 166078871Sjhb case LA_LOCKED: 166178871Sjhb case LA_LOCKED | LA_RECURSED: 166278871Sjhb case LA_LOCKED | LA_NOTRECURSED: 166378871Sjhb case LA_SLOCKED: 166478871Sjhb case LA_SLOCKED | LA_RECURSED: 166578871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 166678871Sjhb case LA_XLOCKED: 166778871Sjhb case LA_XLOCKED | LA_RECURSED: 166878871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 166986422Sjhb if (instance == NULL) { 167078871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 167178871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 167286422Sjhb break; 167386422Sjhb } 167478871Sjhb if ((flags & LA_XLOCKED) != 0 && 167578871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 167678871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 167778871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 167878871Sjhb if ((flags & LA_SLOCKED) != 0 && 167978871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 168078871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 168178871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 168278871Sjhb if ((flags & LA_RECURSED) != 0 && 168378871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 168478871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 168578871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 168678871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 168778871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 168878871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 168978871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 169078871Sjhb break; 169178871Sjhb default: 169278871Sjhb panic("Invalid lock assertion at %s:%d.", file, line); 169378871Sjhb 169478871Sjhb } 169578871Sjhb#endif /* INVARIANT_SUPPORT */ 169678871Sjhb} 169778871Sjhb 169874912Sjhb#ifdef DDB 1699112061Sjhbstatic void 1700112061Sjhbwitness_list(struct thread *td) 1701112061Sjhb{ 170274912Sjhb 1703112061Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1704112061Sjhb KASSERT(db_active, ("%s: not in the debugger", __func__)); 1705112061Sjhb 1706112562Sjhb if (witness_watch == 0) 1707112061Sjhb return; 1708112061Sjhb 1709112061Sjhb witness_list_locks(&td->td_sleeplocks); 1710112061Sjhb 1711112061Sjhb /* 1712112061Sjhb * We only handle spinlocks if td == curthread. This is somewhat broken 1713112061Sjhb * if td is currently executing on some other CPU and holds spin locks 1714112061Sjhb * as we won't display those locks. If we had a MI way of getting 1715112061Sjhb * the per-cpu data for a given cpu then we could use 1716112061Sjhb * td->td_kse->ke_oncpu to get the list of spinlocks for this thread 1717112061Sjhb * and "fix" this. 1718112061Sjhb * 1719112061Sjhb * That still wouldn't really fix this unless we locked sched_lock 1720112061Sjhb * or stopped the other CPU to make sure it wasn't changing the list 1721112061Sjhb * out from under us. It is probably best to just not try to handle 1722112061Sjhb * threads on other CPU's for now. 1723112061Sjhb */ 1724112061Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 1725112061Sjhb witness_list_locks(PCPU_PTR(spinlocks)); 1726112061Sjhb} 1727112061Sjhb 172874930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 172974912Sjhb{ 173083366Sjulian struct thread *td; 173183366Sjulian pid_t pid; 173275273Sjhb struct proc *p; 173374912Sjhb 173475273Sjhb if (have_addr) { 173575273Sjhb pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 173675273Sjhb ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 173775273Sjhb ((addr >> 16) % 16) * 10000; 173875273Sjhb /* sx_slock(&allproc_lock); */ 173983366Sjulian FOREACH_PROC_IN_SYSTEM(p) { 174075273Sjhb if (p->p_pid == pid) 174175273Sjhb break; 174275273Sjhb } 174375273Sjhb /* sx_sunlock(&allproc_lock); */ 174475273Sjhb if (p == NULL) { 174575273Sjhb db_printf("pid %d not found\n", pid); 174675273Sjhb return; 174775273Sjhb } 174890361Sjulian FOREACH_THREAD_IN_PROC(p, td) { 174990361Sjulian witness_list(td); 175090361Sjulian } 175183366Sjulian } else { 175283366Sjulian td = curthread; 175390361Sjulian witness_list(td); 175483366Sjulian } 175574912Sjhb} 175674912Sjhb 175774912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 175874912Sjhb{ 175974912Sjhb 176074912Sjhb witness_display(db_printf); 176174912Sjhb} 176274912Sjhb#endif 1763