subr_witness.c revision 111887
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 111887 2003-03-04 22:34:07Z jhb $ 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" 8767676Sjhb 8865557Sjasone#include <sys/param.h> 8967352Sjhb#include <sys/bus.h> 9067352Sjhb#include <sys/kernel.h> 9174912Sjhb#include <sys/ktr.h> 9274912Sjhb#include <sys/lock.h> 9367352Sjhb#include <sys/malloc.h> 9474912Sjhb#include <sys/mutex.h> 9565557Sjasone#include <sys/proc.h> 9667676Sjhb#include <sys/sysctl.h> 9765557Sjasone#include <sys/systm.h> 9865557Sjasone 9968790Sjhb#include <ddb/ddb.h> 10068790Sjhb 101111881Sjhb#include <machine/stdarg.h> 102111881Sjhb 103105508Sphk/* Define this to check for blessed mutexes */ 104105508Sphk#undef BLESSING 105105508Sphk 10674912Sjhb#define WITNESS_COUNT 200 10774912Sjhb#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 10865557Sjasone/* 10983798Sjhb * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 11074912Sjhb * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 11174912Sjhb * probably be safe for the most part, but it's still a SWAG. 11267352Sjhb */ 11374912Sjhb#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 11471352Sjasone 11574912Sjhb#define WITNESS_NCHILDREN 6 11671352Sjasone 11774912Sjhbstruct witness_child_list_entry; 11871352Sjasone 11974912Sjhbstruct witness { 12074912Sjhb const char *w_name; 12174912Sjhb struct lock_class *w_class; 12274912Sjhb STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 12374912Sjhb STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 12474912Sjhb struct witness_child_list_entry *w_children; /* Great evilness... */ 12574912Sjhb const char *w_file; 12674912Sjhb int w_line; 12774912Sjhb u_int w_level; 12874912Sjhb u_int w_refcount; 12974912Sjhb u_char w_Giant_squawked:1; 13074912Sjhb u_char w_other_squawked:1; 13174912Sjhb u_char w_same_squawked:1; 13274912Sjhb}; 13371352Sjasone 13474912Sjhbstruct witness_child_list_entry { 13574912Sjhb struct witness_child_list_entry *wcl_next; 13674912Sjhb struct witness *wcl_children[WITNESS_NCHILDREN]; 13774912Sjhb u_int wcl_count; 13874912Sjhb}; 13971352Sjasone 14074912SjhbSTAILQ_HEAD(witness_list, witness); 14171352Sjasone 142105508Sphk#ifdef BLESSING 14374912Sjhbstruct witness_blessed { 14474912Sjhb const char *b_lock1; 14574912Sjhb const char *b_lock2; 14674912Sjhb}; 147105508Sphk#endif 14871352Sjasone 14974912Sjhbstruct witness_order_list_entry { 15074912Sjhb const char *w_name; 15174912Sjhb struct lock_class *w_class; 15274912Sjhb}; 15371352Sjasone 15474912Sjhbstatic struct witness *enroll(const char *description, 15574912Sjhb struct lock_class *lock_class); 15674912Sjhbstatic int itismychild(struct witness *parent, struct witness *child); 15774912Sjhbstatic void removechild(struct witness *parent, struct witness *child); 15874912Sjhbstatic int isitmychild(struct witness *parent, struct witness *child); 15974912Sjhbstatic int isitmydescendant(struct witness *parent, struct witness *child); 160105508Sphk#ifdef BLESSING 16174912Sjhbstatic int blessed(struct witness *, struct witness *); 162105508Sphk#endif 16374912Sjhbstatic void witness_displaydescendants(void(*)(const char *fmt, ...), 16474912Sjhb struct witness *); 16574912Sjhbstatic void witness_leveldescendents(struct witness *parent, int level); 16674912Sjhbstatic void witness_levelall(void); 16774912Sjhbstatic struct witness *witness_get(void); 16874912Sjhbstatic void witness_free(struct witness *m); 16974912Sjhbstatic struct witness_child_list_entry *witness_child_get(void); 17074912Sjhbstatic void witness_child_free(struct witness_child_list_entry *wcl); 17174912Sjhbstatic struct lock_list_entry *witness_lock_list_get(void); 17274912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 17376272Sjhbstatic struct lock_instance *find_instance(struct lock_list_entry *lock_list, 17476272Sjhb struct lock_object *lock); 175111881Sjhbstatic int witness_list(struct thread *td); 176111881Sjhbstatic void witness_list_lock(struct lock_instance *instance); 177100011Smp#if defined(DDB) 178100011Smpstatic void witness_display_list(void(*prnt)(const char *fmt, ...), 179100011Smp struct witness_list *list); 180100011Smpstatic void witness_display(void(*)(const char *fmt, ...)); 181100011Smp#endif 18272200Sbmilekic 18374912SjhbMALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 18472200Sbmilekic 18577843Speterstatic int witness_watch = 1; 18677900SpeterTUNABLE_INT("debug.witness_watch", &witness_watch); 18774912SjhbSYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 18871352Sjasone 18967352Sjhb#ifdef DDB 19072200Sbmilekic/* 19167676Sjhb * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 19265557Sjasone * drop into kdebug() when: 19365557Sjasone * - a lock heirarchy violation occurs 19465557Sjasone * - locks are held when going to sleep. 19565557Sjasone */ 19667676Sjhb#ifdef WITNESS_DDB 19777843Speterint witness_ddb = 1; 19867676Sjhb#else 19977843Speterint witness_ddb = 0; 20065557Sjasone#endif 20177900SpeterTUNABLE_INT("debug.witness_ddb", &witness_ddb); 20267676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 203110779Speter 204110779Speter/* 205110779Speter * When DDB is enabled and witness_trace is set to 1, it will cause the system 206110779Speter * to print a stack trace: 207110779Speter * - a lock heirarchy violation occurs 208110779Speter * - locks are held when going to sleep. 209110779Speter */ 210110779Speterint witness_trace = 1; 211110779SpeterTUNABLE_INT("debug.witness_trace", &witness_trace); 212110779SpeterSYSCTL_INT(_debug, OID_AUTO, witness_trace, CTLFLAG_RW, &witness_trace, 0, ""); 21367676Sjhb#endif /* DDB */ 21465557Sjasone 21567676Sjhb#ifdef WITNESS_SKIPSPIN 21677843Speterint witness_skipspin = 1; 21767676Sjhb#else 21877843Speterint witness_skipspin = 0; 21965557Sjasone#endif 22077900SpeterTUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 22167676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 22267676Sjhb ""); 22365557Sjasone 22474912Sjhbstatic struct mtx w_mtx; 22574912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 22674912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 22774912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 22874912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 22974912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL; 23074912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 23174912Sjhbstatic int witness_dead; /* fatal error, probably no memory */ 23265557Sjasone 23374912Sjhbstatic struct witness w_data[WITNESS_COUNT]; 23474912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 23574912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 23665557Sjasone 23774912Sjhbstatic struct witness_order_list_entry order_lists[] = { 23874912Sjhb { "Giant", &lock_class_mtx_sleep }, 23974912Sjhb { "proctree", &lock_class_sx }, 24074912Sjhb { "allproc", &lock_class_sx }, 241108184Skris { "filedesc structure", &lock_class_mtx_sleep }, 242108184Skris { "pipe mutex", &lock_class_mtx_sleep }, 24396122Salfred { "sigio lock", &lock_class_mtx_sleep }, 24491140Stanimura { "process group", &lock_class_mtx_sleep }, 24574912Sjhb { "process lock", &lock_class_mtx_sleep }, 24691140Stanimura { "session", &lock_class_mtx_sleep }, 24774912Sjhb { "uidinfo hash", &lock_class_mtx_sleep }, 24874912Sjhb { "uidinfo struct", &lock_class_mtx_sleep }, 24974912Sjhb { NULL, NULL }, 25075464Sjhb /* 25175464Sjhb * spin locks 25275464Sjhb */ 25384331Sjhb#ifdef SMP 25484331Sjhb { "ap boot", &lock_class_mtx_spin }, 25584331Sjhb#ifdef __i386__ 25674912Sjhb { "com", &lock_class_mtx_spin }, 25772224Sjhb#endif 25884331Sjhb#endif 25974912Sjhb { "sio", &lock_class_mtx_spin }, 26072224Sjhb#ifdef __i386__ 26174912Sjhb { "cy", &lock_class_mtx_spin }, 26272224Sjhb#endif 263103091Sjake { "sabtty", &lock_class_mtx_spin }, 264109015Sjake { "zstty", &lock_class_mtx_spin }, 26574912Sjhb { "ng_node", &lock_class_mtx_spin }, 26674912Sjhb { "ng_worklist", &lock_class_mtx_spin }, 26774912Sjhb { "ithread table lock", &lock_class_mtx_spin }, 26874912Sjhb { "sched lock", &lock_class_mtx_spin }, 26974912Sjhb { "callout", &lock_class_mtx_spin }, 27065557Sjasone /* 27165557Sjasone * leaf locks 27265557Sjasone */ 27390278Sjhb { "allpmaps", &lock_class_mtx_spin }, 27499416Salc { "vm page queue free mutex", &lock_class_mtx_spin }, 27588322Sjhb { "icu", &lock_class_mtx_spin }, 27672224Sjhb#ifdef SMP 27774912Sjhb { "smp rendezvous", &lock_class_mtx_spin }, 27899862Speter#if defined(__i386__) && defined(APIC_IO) 27999862Speter { "tlb", &lock_class_mtx_spin }, 28072224Sjhb#endif 281108187Sjake#ifdef __sparc64__ 282108187Sjake { "ipi", &lock_class_mtx_spin }, 28399862Speter#endif 284108187Sjake#endif 28578785Sjhb { "clk", &lock_class_mtx_spin }, 28695473Sdes { "mutex profiling lock", &lock_class_mtx_spin }, 287111028Sjeff { "kse zombie lock", &lock_class_mtx_spin }, 288103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 289104951Speter#ifdef __ia64__ 290104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 291104951Speter#endif 292111068Speter#ifdef __i386__ 293111068Speter { "pcicfg", &lock_class_mtx_spin }, 294111068Speter#endif 29574912Sjhb { NULL, NULL }, 29674912Sjhb { NULL, NULL } 29765557Sjasone}; 29865557Sjasone 299105508Sphk#ifdef BLESSING 30065557Sjasone/* 30165557Sjasone * Pairs of locks which have been blessed 30265557Sjasone * Don't complain about order problems with blessed locks 30365557Sjasone */ 30465856Sjhbstatic struct witness_blessed blessed_list[] = { 30565557Sjasone}; 30672200Sbmilekicstatic int blessed_count = 30772200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 308105508Sphk#endif 30965557Sjasone 31074912Sjhb/* 31174912Sjhb * List of all locks in the system. 31274912Sjhb */ 31397963SjhbTAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 31474912Sjhb 31574912Sjhbstatic struct mtx all_mtx = { 31674912Sjhb { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 31774912Sjhb "All locks list", /* mtx_object.lo_name */ 31893811Sjhb "All locks list", /* mtx_object.lo_type */ 31974912Sjhb LO_INITIALIZED, /* mtx_object.lo_flags */ 32097963Sjhb { NULL, NULL }, /* mtx_object.lo_list */ 32174912Sjhb NULL }, /* mtx_object.lo_witness */ 32274912Sjhb MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 32374912Sjhb TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 32474912Sjhb { NULL, NULL } /* mtx_contested */ 32574912Sjhb}; 32674912Sjhb 32774912Sjhb/* 32874912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 32974912Sjhb */ 33074912Sjhbstatic int witness_cold = 1; 33174912Sjhb 33274912Sjhb/* 33374912Sjhb * Global variables for book keeping. 33474912Sjhb */ 33574912Sjhbstatic int lock_cur_cnt; 33674912Sjhbstatic int lock_max_cnt; 33774912Sjhb 33874912Sjhb/* 33974912Sjhb * The WITNESS-enabled diagnostic code. 34074912Sjhb */ 34171352Sjasonestatic void 34274912Sjhbwitness_initialize(void *dummy __unused) 34365557Sjasone{ 34474912Sjhb struct lock_object *lock; 34574912Sjhb struct witness_order_list_entry *order; 34674912Sjhb struct witness *w, *w1; 34774912Sjhb int i; 34865557Sjasone 34974912Sjhb /* 35074912Sjhb * We have to release Giant before initializing its witness 35174912Sjhb * structure so that WITNESS doesn't get confused. 35274912Sjhb */ 35374912Sjhb mtx_unlock(&Giant); 35474912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 35574912Sjhb 35687593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 35797963Sjhb TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 35893811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 35993811Sjhb MTX_NOWITNESS); 36074912Sjhb for (i = 0; i < WITNESS_COUNT; i++) 36174912Sjhb witness_free(&w_data[i]); 36274912Sjhb for (i = 0; i < WITNESS_CHILDCOUNT; i++) 36374912Sjhb witness_child_free(&w_childdata[i]); 36474912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 36574912Sjhb witness_lock_list_free(&w_locklistdata[i]); 36674912Sjhb 36774912Sjhb /* First add in all the specified order lists. */ 36874912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 36974912Sjhb w = enroll(order->w_name, order->w_class); 37075569Sjhb if (w == NULL) 37175569Sjhb continue; 37274912Sjhb w->w_file = "order list"; 37374912Sjhb for (order++; order->w_name != NULL; order++) { 37474912Sjhb w1 = enroll(order->w_name, order->w_class); 37575569Sjhb if (w1 == NULL) 37675569Sjhb continue; 37774912Sjhb w1->w_file = "order list"; 37874912Sjhb itismychild(w, w1); 37974912Sjhb w = w1; 38065557Sjasone } 38165557Sjasone } 38265557Sjasone 38374912Sjhb /* Iterate through all locks and add them to witness. */ 38474912Sjhb mtx_lock(&all_mtx); 38597963Sjhb TAILQ_FOREACH(lock, &all_locks, lo_list) { 38674912Sjhb if (lock->lo_flags & LO_WITNESS) 38793811Sjhb lock->lo_witness = enroll(lock->lo_type, 38874912Sjhb lock->lo_class); 38974912Sjhb else 39074912Sjhb lock->lo_witness = NULL; 39174912Sjhb } 39274912Sjhb mtx_unlock(&all_mtx); 39374912Sjhb 39474912Sjhb /* Mark the witness code as being ready for use. */ 39574912Sjhb atomic_store_rel_int(&witness_cold, 0); 39674912Sjhb 39774912Sjhb mtx_lock(&Giant); 39865557Sjasone} 39974912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 40065557Sjasone 40174912Sjhbvoid 40274912Sjhbwitness_init(struct lock_object *lock) 40374912Sjhb{ 40474912Sjhb struct lock_class *class; 40574912Sjhb 40674912Sjhb class = lock->lo_class; 40774912Sjhb if (lock->lo_flags & LO_INITIALIZED) 40882284Sjhb panic("%s: lock (%s) %s is already initialized", __func__, 40974912Sjhb class->lc_name, lock->lo_name); 41074912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 41174912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 41282284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 41374912Sjhb class->lc_name, lock->lo_name); 41474912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 41574912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 41682284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 41774912Sjhb class->lc_name, lock->lo_name); 41882244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 41982244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 42082284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 42182244Sjhb class->lc_name, lock->lo_name); 42282244Sjhb 42374912Sjhb mtx_lock(&all_mtx); 42497963Sjhb TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 42574912Sjhb lock->lo_flags |= LO_INITIALIZED; 42674912Sjhb lock_cur_cnt++; 42774912Sjhb if (lock_cur_cnt > lock_max_cnt) 42874912Sjhb lock_max_cnt = lock_cur_cnt; 42974912Sjhb mtx_unlock(&all_mtx); 43080747Sjhb if (!witness_cold && !witness_dead && panicstr == NULL && 43174912Sjhb (lock->lo_flags & LO_WITNESS) != 0) 43293811Sjhb lock->lo_witness = enroll(lock->lo_type, class); 43374912Sjhb else 43474912Sjhb lock->lo_witness = NULL; 43574912Sjhb} 43674912Sjhb 43774912Sjhbvoid 43874912Sjhbwitness_destroy(struct lock_object *lock) 43974912Sjhb{ 44075362Sjhb struct witness *w; 44174912Sjhb 44274912Sjhb if (witness_cold) 44374912Sjhb panic("lock (%s) %s destroyed while witness_cold", 44474912Sjhb lock->lo_class->lc_name, lock->lo_name); 44574912Sjhb if ((lock->lo_flags & LO_INITIALIZED) == 0) 44682284Sjhb panic("%s: lock (%s) %s is not initialized", __func__, 44774912Sjhb lock->lo_class->lc_name, lock->lo_name); 44874912Sjhb 44976272Sjhb /* XXX: need to verify that no one holds the lock */ 45075362Sjhb w = lock->lo_witness; 45175362Sjhb if (w != NULL) { 45275362Sjhb mtx_lock_spin(&w_mtx); 45397948Sjhb MPASS(w->w_refcount > 0); 45475362Sjhb w->w_refcount--; 45575362Sjhb mtx_unlock_spin(&w_mtx); 45675362Sjhb } 45775362Sjhb 45874912Sjhb mtx_lock(&all_mtx); 45974912Sjhb lock_cur_cnt--; 46097963Sjhb TAILQ_REMOVE(&all_locks, lock, lo_list); 46180055Sjhb lock->lo_flags &= ~LO_INITIALIZED; 46274912Sjhb mtx_unlock(&all_mtx); 46374912Sjhb} 46474912Sjhb 465100011Smp#if defined(DDB) 46671352Sjasonestatic void 46774912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...), 46874912Sjhb struct witness_list *list) 46971352Sjasone{ 47071352Sjasone struct witness *w, *w1; 47174912Sjhb int found; 47271352Sjasone 47374912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 47474912Sjhb if (w->w_file == NULL) 47571352Sjasone continue; 47674912Sjhb found = 0; 47774912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 47874912Sjhb if (isitmychild(w1, w)) { 47974912Sjhb found++; 48071352Sjasone break; 48174912Sjhb } 48271352Sjasone } 48374912Sjhb if (found) 48471352Sjasone continue; 48571352Sjasone /* 48671352Sjasone * This lock has no anscestors, display its descendants. 48771352Sjasone */ 48871352Sjasone witness_displaydescendants(prnt, w); 48971352Sjasone } 49074912Sjhb} 49172224Sjhb 49274912Sjhbstatic void 49374912Sjhbwitness_display(void(*prnt)(const char *fmt, ...)) 49474912Sjhb{ 49574912Sjhb struct witness *w; 49674912Sjhb 49782284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 49874912Sjhb witness_levelall(); 49974912Sjhb 50072224Sjhb /* 50174930Sjhb * First, handle sleep locks which have been acquired at least 50274912Sjhb * once. 50374912Sjhb */ 50474912Sjhb prnt("Sleep locks:\n"); 50574912Sjhb witness_display_list(prnt, &w_sleep); 50674912Sjhb 50774912Sjhb /* 50874930Sjhb * Now do spin locks which have been acquired at least once. 50972224Sjhb */ 51074912Sjhb prnt("\nSpin locks:\n"); 51174912Sjhb witness_display_list(prnt, &w_spin); 51272224Sjhb 51372224Sjhb /* 51474930Sjhb * Finally, any locks which have not been acquired yet. 51572224Sjhb */ 51674912Sjhb prnt("\nLocks which were never acquired:\n"); 51774912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 51897948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 51971352Sjasone continue; 52074912Sjhb prnt("%s\n", w->w_name); 52171352Sjasone } 52271352Sjasone} 523100011Smp#endif 52471352Sjasone 52565557Sjasonevoid 52674912Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 52765557Sjasone{ 52874912Sjhb struct lock_list_entry **lock_list, *lle; 52976272Sjhb struct lock_instance *lock1, *lock2; 53074912Sjhb struct lock_class *class; 53165856Sjhb struct witness *w, *w1; 53283366Sjulian struct thread *td; 53374912Sjhb int i, j; 53467676Sjhb#ifdef DDB 53567676Sjhb int go_into_ddb = 0; 53667676Sjhb#endif /* DDB */ 53765557Sjasone 53874912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 53980747Sjhb panicstr != NULL) 54071320Sjasone return; 54174912Sjhb w = lock->lo_witness; 54274912Sjhb class = lock->lo_class; 54383366Sjulian td = curthread; 54465557Sjasone 54574912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 54693676Sjhb /* 54793676Sjhb * Since spin locks include a critical section, this check 54893676Sjhb * impliclty enforces a lock order of all sleep locks before 54993676Sjhb * all spin locks. 55093676Sjhb */ 55188899Sjhb if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0) 55274912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 55374912Sjhb class->lc_name, lock->lo_name, file, line); 55483366Sjulian lock_list = &td->td_sleeplocks; 55588899Sjhb } else 55688899Sjhb lock_list = PCPU_PTR(spinlocks); 55765557Sjasone 55876772Sjhb /* 55976772Sjhb * Try locks do not block if they fail to acquire the lock, thus 56076772Sjhb * there is no danger of deadlocks or of switching while holding a 56176772Sjhb * spin lock if we acquire a lock via a try operation. 56276772Sjhb */ 56374912Sjhb if (flags & LOP_TRYLOCK) 56465557Sjasone goto out; 56565557Sjasone 56665557Sjasone /* 56774912Sjhb * Is this the first lock acquired? If so, then no order checking 56874912Sjhb * is needed. 56965557Sjasone */ 57074912Sjhb if (*lock_list == NULL) 57165557Sjasone goto out; 57265557Sjasone 57374912Sjhb /* 57476272Sjhb * Check to see if we are recursing on a lock we already own. 57576272Sjhb */ 57676272Sjhb lock1 = find_instance(*lock_list, lock); 57776272Sjhb if (lock1 != NULL) { 57876272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 57976272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 58076272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 58176272Sjhb class->lc_name, lock->lo_name, file, line); 58276272Sjhb printf("while exclusively locked from %s:%d\n", 58376272Sjhb lock1->li_file, lock1->li_line); 58476272Sjhb panic("share->excl"); 58576272Sjhb } 58676272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 58776272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 58876272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 58976272Sjhb class->lc_name, lock->lo_name, file, line); 59076272Sjhb printf("while share locked from %s:%d\n", 59176272Sjhb lock1->li_file, lock1->li_line); 59276272Sjhb panic("excl->share"); 59376272Sjhb } 59476272Sjhb lock1->li_flags++; 59576272Sjhb if ((lock->lo_flags & LO_RECURSABLE) == 0) { 59676272Sjhb printf( 59776272Sjhb "recursed on non-recursive lock (%s) %s @ %s:%d\n", 59876272Sjhb class->lc_name, lock->lo_name, file, line); 59976272Sjhb printf("first acquired @ %s:%d\n", lock1->li_file, 60076272Sjhb lock1->li_line); 60176272Sjhb panic("recurse"); 60276272Sjhb } 60387593Sobrien CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 60484680Sjhb td->td_proc->p_pid, lock->lo_name, 60578785Sjhb lock1->li_flags & LI_RECURSEMASK); 60676272Sjhb lock1->li_file = file; 60776272Sjhb lock1->li_line = line; 60876272Sjhb return; 60976272Sjhb } 61076272Sjhb 61176272Sjhb /* 61274912Sjhb * Check for duplicate locks of the same type. Note that we only 61374912Sjhb * have to check for this on the last lock we just acquired. Any 61474912Sjhb * other cases will be caught as lock order violations. 61574912Sjhb */ 61676272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 61776272Sjhb w1 = lock1->li_lock->lo_witness; 61874912Sjhb if (w1 == w) { 61993273Sjeff if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK)) 62065557Sjasone goto out; 62165557Sjasone w->w_same_squawked = 1; 62275755Sjhb printf("acquiring duplicate lock of same type: \"%s\"\n", 62393811Sjhb lock->lo_type); 62493811Sjhb printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 62593811Sjhb lock1->li_file, lock1->li_line); 62693811Sjhb printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 62767676Sjhb#ifdef DDB 62867676Sjhb go_into_ddb = 1; 62967676Sjhb#endif /* DDB */ 63065557Sjasone goto out; 63165557Sjasone } 63265557Sjasone MPASS(!mtx_owned(&w_mtx)); 63374912Sjhb mtx_lock_spin(&w_mtx); 63465557Sjasone /* 63565557Sjasone * If we have a known higher number just say ok 63665557Sjasone */ 63765557Sjasone if (witness_watch > 1 && w->w_level > w1->w_level) { 63874912Sjhb mtx_unlock_spin(&w_mtx); 63965557Sjasone goto out; 64065557Sjasone } 641111881Sjhb /* 642111881Sjhb * If we know that the the lock we are acquiring comes after 643111881Sjhb * the lock we most recently acquired in the lock order tree, 644111881Sjhb * then there is no need for any further checks. 645111881Sjhb */ 64674912Sjhb if (isitmydescendant(w1, w)) { 64774912Sjhb mtx_unlock_spin(&w_mtx); 64865557Sjasone goto out; 64965557Sjasone } 65074912Sjhb for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 65174912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 65265557Sjasone 65374912Sjhb MPASS(j < WITNESS_COUNT); 65476272Sjhb lock1 = &lle->ll_children[i]; 65576272Sjhb w1 = lock1->li_lock->lo_witness; 65674912Sjhb 65774912Sjhb /* 65874912Sjhb * If this lock doesn't undergo witness checking, 65974912Sjhb * then skip it. 66074912Sjhb */ 66174912Sjhb if (w1 == NULL) { 66276272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 66374912Sjhb ("lock missing witness structure")); 66474912Sjhb continue; 66574912Sjhb } 66676272Sjhb /* 667111881Sjhb * If we are locking Giant and this is a sleepable 66876272Sjhb * lock, then skip it. 66976272Sjhb */ 670111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 67176272Sjhb lock == &Giant.mtx_object) 67276272Sjhb continue; 67393690Sjhb /* 67493690Sjhb * If we are locking a sleepable lock and this lock 675111881Sjhb * is Giant, then skip it. 67693690Sjhb */ 677111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 678111887Sjhb lock1->li_lock == &Giant.mtx_object) 679111881Sjhb continue; 680111881Sjhb /* 681111881Sjhb * If we are locking a sleepable lock and this lock 682111881Sjhb * isn't sleepable, we want to treat it as a lock 683111881Sjhb * order violation to enfore a general lock order of 684111881Sjhb * sleepable locks before non-sleepable locks. 685111881Sjhb */ 68693690Sjhb if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 687111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 688111881Sjhb /* 689111881Sjhb * Check the lock order hierarchy for a reveresal. 690111881Sjhb */ 691111881Sjhb if (!isitmydescendant(w, w1)) 69274912Sjhb continue; 69374912Sjhb /* 69474912Sjhb * We have a lock order violation, check to see if it 69574912Sjhb * is allowed or has already been yelled about. 69674912Sjhb */ 69774912Sjhb mtx_unlock_spin(&w_mtx); 698105508Sphk#ifdef BLESSING 69965557Sjasone if (blessed(w, w1)) 70065557Sjasone goto out; 701105508Sphk#endif 70276272Sjhb if (lock1->li_lock == &Giant.mtx_object) { 70365557Sjasone if (w1->w_Giant_squawked) 70465557Sjasone goto out; 70565557Sjasone else 70665557Sjasone w1->w_Giant_squawked = 1; 70765557Sjasone } else { 70865557Sjasone if (w1->w_other_squawked) 70965557Sjasone goto out; 71065557Sjasone else 71165557Sjasone w1->w_other_squawked = 1; 71265557Sjasone } 71374912Sjhb /* 71474912Sjhb * Ok, yell about it. 71574912Sjhb */ 71665557Sjasone printf("lock order reversal\n"); 71774912Sjhb /* 71874912Sjhb * Try to locate an earlier lock with 71974912Sjhb * witness w in our list. 72074912Sjhb */ 72174912Sjhb do { 72276272Sjhb lock2 = &lle->ll_children[i]; 72376272Sjhb MPASS(lock2->li_lock != NULL); 72476272Sjhb if (lock2->li_lock->lo_witness == w) 72574912Sjhb break; 72674912Sjhb i--; 72774912Sjhb if (i == 0 && lle->ll_next != NULL) { 72874912Sjhb lle = lle->ll_next; 72974912Sjhb i = lle->ll_count - 1; 730106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 73174912Sjhb } 73274912Sjhb } while (i >= 0); 73376272Sjhb if (i < 0) { 73493811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 73593811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 73693811Sjhb lock1->li_lock->lo_type, lock1->li_file, 73776272Sjhb lock1->li_line); 73893811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 73993811Sjhb lock->lo_name, lock->lo_type, file, line); 74076272Sjhb } else { 74193811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 74293811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 74393811Sjhb lock2->li_lock->lo_type, lock2->li_file, 74476272Sjhb lock2->li_line); 74593811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 74693811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 74793811Sjhb lock1->li_lock->lo_type, lock1->li_file, 74876272Sjhb lock1->li_line); 74993811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 75093811Sjhb lock->lo_name, lock->lo_type, file, line); 75176272Sjhb } 75267676Sjhb#ifdef DDB 75367676Sjhb go_into_ddb = 1; 75467676Sjhb#endif /* DDB */ 75565557Sjasone goto out; 75665557Sjasone } 75765557Sjasone } 75876272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 75978871Sjhb /* 760111881Sjhb * Don't build a new relationship between a sleepable lock and 761111881Sjhb * Giant if it is the wrong direction. The real lock order is that 762111881Sjhb * sleepable locks come before Giant. 76378871Sjhb */ 764111881Sjhb if (lock1->li_lock == &Giant.mtx_object && 765111881Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0) 76674912Sjhb mtx_unlock_spin(&w_mtx); 76778871Sjhb else { 76887593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 76993811Sjhb lock->lo_type, lock1->li_lock->lo_type); 77078871Sjhb if (!itismychild(lock1->li_lock->lo_witness, w)) 77178871Sjhb mtx_unlock_spin(&w_mtx); 77278871Sjhb } 77365557Sjasone 77465557Sjasoneout: 77567676Sjhb#ifdef DDB 776110779Speter if (go_into_ddb) { 777110779Speter if (witness_trace) 778110779Speter backtrace(); 779110779Speter if (witness_ddb) 780110779Speter Debugger(__func__); 781110779Speter } 78267676Sjhb#endif /* DDB */ 78365557Sjasone w->w_file = file; 78465557Sjasone w->w_line = line; 78574912Sjhb 78674912Sjhb lle = *lock_list; 78776272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 78878785Sjhb lle = witness_lock_list_get(); 78978785Sjhb if (lle == NULL) 79065557Sjasone return; 79178785Sjhb lle->ll_next = *lock_list; 79287593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 79384680Sjhb td->td_proc->p_pid, lle); 79478785Sjhb *lock_list = lle; 79565557Sjasone } 79676272Sjhb lock1 = &lle->ll_children[lle->ll_count++]; 79776272Sjhb lock1->li_lock = lock; 79876272Sjhb lock1->li_line = line; 79976272Sjhb lock1->li_file = file; 80076272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 80176272Sjhb lock1->li_flags = LI_EXCLUSIVE; 80276272Sjhb else 80376272Sjhb lock1->li_flags = 0; 80487593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 80584680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 80665557Sjasone} 80765557Sjasone 80865557Sjasonevoid 80982244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 81082244Sjhb{ 81182244Sjhb struct lock_instance *instance; 81282244Sjhb struct lock_class *class; 81382244Sjhb 81482284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 81582244Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 81682244Sjhb return; 81782244Sjhb class = lock->lo_class; 81882244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 81982244Sjhb panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 82082244Sjhb class->lc_name, lock->lo_name, file, line); 82182244Sjhb if ((flags & LOP_TRYLOCK) == 0) 82282244Sjhb panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 82382244Sjhb lock->lo_name, file, line); 82482244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 82582244Sjhb panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 82682244Sjhb class->lc_name, lock->lo_name, file, line); 82783366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 82882244Sjhb if (instance == NULL) 82982244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 83082244Sjhb class->lc_name, lock->lo_name, file, line); 83182244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0) 83282244Sjhb panic("upgrade of exclusive lock (%s) %s @ %s:%d", 83382244Sjhb class->lc_name, lock->lo_name, file, line); 83482244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 83582244Sjhb panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 83682244Sjhb class->lc_name, lock->lo_name, 83782244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 83882244Sjhb instance->li_flags |= LI_EXCLUSIVE; 83982244Sjhb} 84082244Sjhb 84182244Sjhbvoid 84282244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 84382244Sjhb int line) 84482244Sjhb{ 84582244Sjhb struct lock_instance *instance; 84682244Sjhb struct lock_class *class; 84782244Sjhb 84882284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 84982244Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 85082244Sjhb return; 85182244Sjhb class = lock->lo_class; 85282244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 85382244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 85482244Sjhb class->lc_name, lock->lo_name, file, line); 85582244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 85682244Sjhb panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 85782244Sjhb class->lc_name, lock->lo_name, file, line); 85883366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 85982244Sjhb if (instance == NULL) 86082244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 86182244Sjhb class->lc_name, lock->lo_name, file, line); 86282244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0) 86382244Sjhb panic("downgrade of shared lock (%s) %s @ %s:%d", 86482244Sjhb class->lc_name, lock->lo_name, file, line); 86582244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 86682244Sjhb panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 86782244Sjhb class->lc_name, lock->lo_name, 86882244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 86982244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 87082244Sjhb} 87182244Sjhb 87282244Sjhbvoid 87374912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 87465557Sjasone{ 87574912Sjhb struct lock_list_entry **lock_list, *lle; 87676272Sjhb struct lock_instance *instance; 87774912Sjhb struct lock_class *class; 87883366Sjulian struct thread *td; 87992858Simp register_t s; 88074912Sjhb int i, j; 88165557Sjasone 88274912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 88380747Sjhb panicstr != NULL) 88471352Sjasone return; 88583366Sjulian td = curthread; 88674912Sjhb class = lock->lo_class; 88776272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 88883366Sjulian lock_list = &td->td_sleeplocks; 88976272Sjhb else 89074912Sjhb lock_list = PCPU_PTR(spinlocks); 89174912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 89276272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 89376272Sjhb instance = &(*lock_list)->ll_children[i]; 89476272Sjhb if (instance->li_lock == lock) { 89576272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 89676272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 89776272Sjhb printf( 89876272Sjhb "shared unlock of (%s) %s @ %s:%d\n", 89976272Sjhb class->lc_name, lock->lo_name, 90076272Sjhb file, line); 90176272Sjhb printf( 90276272Sjhb "while exclusively locked from %s:%d\n", 90376272Sjhb instance->li_file, 90476272Sjhb instance->li_line); 90576272Sjhb panic("excl->ushare"); 90676272Sjhb } 90776272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 90876272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 90976272Sjhb printf( 91076272Sjhb "exclusive unlock of (%s) %s @ %s:%d\n", 91176272Sjhb class->lc_name, lock->lo_name, 91276272Sjhb file, line); 91376272Sjhb printf( 91476272Sjhb "while share locked from %s:%d\n", 91576272Sjhb instance->li_file, 91676272Sjhb instance->li_line); 91776272Sjhb panic("share->uexcl"); 91876272Sjhb } 91976272Sjhb /* If we are recursed, unrecurse. */ 92076272Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 92187593Sobrien CTR4(KTR_WITNESS, 92287593Sobrien "%s: pid %d unrecursed on %s r=%d", __func__, 92384680Sjhb td->td_proc->p_pid, 92478785Sjhb instance->li_lock->lo_name, 92578785Sjhb instance->li_flags); 92676272Sjhb instance->li_flags--; 92788900Sjhb return; 92876272Sjhb } 92992858Simp s = intr_disable(); 93087593Sobrien CTR4(KTR_WITNESS, 93187593Sobrien "%s: pid %d removed %s from lle[%d]", __func__, 93284680Sjhb td->td_proc->p_pid, 93384680Sjhb instance->li_lock->lo_name, 93478785Sjhb (*lock_list)->ll_count - 1); 93597014Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 93674912Sjhb (*lock_list)->ll_children[j] = 93774912Sjhb (*lock_list)->ll_children[j + 1]; 93897014Sjhb (*lock_list)->ll_count--; 93992858Simp intr_restore(s); 94074912Sjhb if ((*lock_list)->ll_count == 0) { 94174912Sjhb lle = *lock_list; 94274912Sjhb *lock_list = lle->ll_next; 94387593Sobrien CTR3(KTR_WITNESS, 94487593Sobrien "%s: pid %d removed lle %p", __func__, 94584680Sjhb td->td_proc->p_pid, lle); 94674912Sjhb witness_lock_list_free(lle); 94774912Sjhb } 94888900Sjhb return; 94974912Sjhb } 95076272Sjhb } 95176272Sjhb panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 95276272Sjhb file, line); 95365557Sjasone} 95465557Sjasone 95574912Sjhb/* 956111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 957111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 958111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 959111881Sjhb * console along with a list of the offending locks. If indicated in the 960111881Sjhb * flags then a failure results in a panic as well. 96174912Sjhb */ 96265557Sjasoneint 963111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 96465557Sjasone{ 965111881Sjhb struct lock_list_entry *lle; 96676272Sjhb struct lock_instance *lock1; 96783366Sjulian struct thread *td; 968111881Sjhb va_list ap; 96974912Sjhb int i, n; 97065557Sjasone 97197006Sjhb if (witness_cold || witness_dead || panicstr != NULL) 97274912Sjhb return (0); 97374912Sjhb n = 0; 97483366Sjulian td = curthread; 975111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 97674912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 97776272Sjhb lock1 = &lle->ll_children[i]; 978111881Sjhb if (lock1->li_lock == lock) 979111881Sjhb continue; 980111881Sjhb if (flags & WARN_GIANTOK && 98176272Sjhb lock1->li_lock == &Giant.mtx_object) 98274912Sjhb continue; 983111881Sjhb if (flags & WARN_SLEEPOK && 984111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 98576272Sjhb continue; 986111881Sjhb if (n == 0) { 987111881Sjhb va_start(ap, fmt); 988111881Sjhb vprintf(fmt, ap); 989111881Sjhb va_end(ap); 990111881Sjhb printf(" with the following"); 991111881Sjhb if (flags & WARN_SLEEPOK) 992111881Sjhb printf(" non-sleepable"); 993111881Sjhb printf("locks held:\n"); 99476272Sjhb } 99574912Sjhb n++; 996111881Sjhb witness_list_lock(lock1); 99774912Sjhb } 998111881Sjhb if (PCPU_GET(spinlocks) != NULL) { 99997006Sjhb /* 100097006Sjhb * Since we already hold a spinlock preemption is 100197006Sjhb * already blocked. 100297006Sjhb */ 1003111881Sjhb if (n == 0) { 1004111881Sjhb va_start(ap, fmt); 1005111881Sjhb vprintf(fmt, ap); 1006111881Sjhb va_end(ap); 1007111881Sjhb printf(" with the following"); 1008111881Sjhb if (flags & WARN_SLEEPOK) 1009111881Sjhb printf(" non-sleepable"); 1010111881Sjhb printf("locks held:\n"); 1011111881Sjhb } 1012111881Sjhb n += witness_list_locks(PCPU_PTR(spinlocks)); 101365557Sjasone } 1014111881Sjhb if (flags & WARN_PANIC && n) 1015111881Sjhb panic("witness_warn"); 101667676Sjhb#ifdef DDB 1017111881Sjhb else if (witness_ddb && n) 101875711Sjhb Debugger(__func__); 1019111881Sjhb#endif 102065557Sjasone return (n); 102165557Sjasone} 102265557Sjasone 1023102448Siedowseconst char * 1024102448Siedowsewitness_file(struct lock_object *lock) 1025102448Siedowse{ 1026102448Siedowse struct witness *w; 1027102448Siedowse 1028102448Siedowse if (witness_cold || witness_dead || lock->lo_witness == NULL) 1029102448Siedowse return ("?"); 1030102448Siedowse w = lock->lo_witness; 1031102448Siedowse return (w->w_file); 1032102448Siedowse} 1033102448Siedowse 1034102448Siedowseint 1035102448Siedowsewitness_line(struct lock_object *lock) 1036102448Siedowse{ 1037102448Siedowse struct witness *w; 1038102448Siedowse 1039102448Siedowse if (witness_cold || witness_dead || lock->lo_witness == NULL) 1040102448Siedowse return (0); 1041102448Siedowse w = lock->lo_witness; 1042102448Siedowse return (w->w_line); 1043102448Siedowse} 1044102448Siedowse 104565856Sjhbstatic struct witness * 104674912Sjhbenroll(const char *description, struct lock_class *lock_class) 104765557Sjasone{ 104874912Sjhb struct witness *w; 104965557Sjasone 105080747Sjhb if (!witness_watch || witness_dead || panicstr != NULL) 105165557Sjasone return (NULL); 105274912Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 105365557Sjasone return (NULL); 105474912Sjhb mtx_lock_spin(&w_mtx); 105574912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 105697948Sjhb if (w->w_name == description || (w->w_refcount > 0 && 105797948Sjhb strcmp(description, w->w_name) == 0)) { 105875362Sjhb w->w_refcount++; 105974912Sjhb mtx_unlock_spin(&w_mtx); 106074912Sjhb if (lock_class != w->w_class) 106174912Sjhb panic( 106274912Sjhb "lock (%s) %s does not match earlier (%s) lock", 106374912Sjhb description, lock_class->lc_name, 106474912Sjhb w->w_class->lc_name); 106565557Sjasone return (w); 106665557Sjasone } 106765557Sjasone } 106874912Sjhb /* 106974912Sjhb * This isn't quite right, as witness_cold is still 0 while we 107074912Sjhb * enroll all the locks initialized before witness_initialize(). 107174912Sjhb */ 107275364Sbp if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 107375364Sbp mtx_unlock_spin(&w_mtx); 107474912Sjhb panic("spin lock %s not in order list", description); 107575364Sbp } 107665557Sjasone if ((w = witness_get()) == NULL) 107765557Sjasone return (NULL); 107874912Sjhb w->w_name = description; 107974912Sjhb w->w_class = lock_class; 108075362Sjhb w->w_refcount = 1; 108174912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 108274912Sjhb if (lock_class->lc_flags & LC_SPINLOCK) 108374912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 108474912Sjhb else if (lock_class->lc_flags & LC_SLEEPLOCK) 108574912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 108675364Sbp else { 108775364Sbp mtx_unlock_spin(&w_mtx); 108874912Sjhb panic("lock class %s is not sleep or spin", 108974912Sjhb lock_class->lc_name); 109075364Sbp } 109174912Sjhb mtx_unlock_spin(&w_mtx); 109265557Sjasone return (w); 109365557Sjasone} 109465557Sjasone 109565557Sjasonestatic int 109665856Sjhbitismychild(struct witness *parent, struct witness *child) 109765557Sjasone{ 109865557Sjasone static int recursed; 109974912Sjhb struct witness_child_list_entry **wcl; 110074912Sjhb struct witness_list *list; 110165557Sjasone 110274912Sjhb MPASS(child != NULL && parent != NULL); 110374912Sjhb if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 110474912Sjhb (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 110574912Sjhb panic( 110674912Sjhb "%s: parent (%s) and child (%s) are not the same lock type", 110774912Sjhb __func__, parent->w_class->lc_name, 110874912Sjhb child->w_class->lc_name); 110974912Sjhb 111065557Sjasone /* 111165557Sjasone * Insert "child" after "parent" 111265557Sjasone */ 111374912Sjhb wcl = &parent->w_children; 111474912Sjhb while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 111574912Sjhb wcl = &(*wcl)->wcl_next; 111674912Sjhb if (*wcl == NULL) { 111774912Sjhb *wcl = witness_child_get(); 111874912Sjhb if (*wcl == NULL) 111965557Sjasone return (1); 112065557Sjasone } 112174912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 112274912Sjhb 112365557Sjasone /* 112474912Sjhb * Now prune whole tree. We look for cases where a lock is now 112574912Sjhb * both a descendant and a direct child of a given lock. In that 112674912Sjhb * case, we want to remove the direct child link from the tree. 112765557Sjasone */ 112865557Sjasone if (recursed) 112965557Sjasone return (0); 113065557Sjasone recursed = 1; 113174912Sjhb if (parent->w_class->lc_flags & LC_SLEEPLOCK) 113274912Sjhb list = &w_sleep; 113374912Sjhb else 113474912Sjhb list = &w_spin; 113574912Sjhb STAILQ_FOREACH(child, list, w_typelist) { 113674912Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 113765557Sjasone if (!isitmychild(parent, child)) 113865557Sjasone continue; 113965557Sjasone removechild(parent, child); 114065557Sjasone if (isitmydescendant(parent, child)) 114165557Sjasone continue; 114265557Sjasone itismychild(parent, child); 114365557Sjasone } 114465557Sjasone } 114565557Sjasone recursed = 0; 114665557Sjasone witness_levelall(); 114765557Sjasone return (0); 114865557Sjasone} 114965557Sjasone 115065557Sjasonestatic void 115165856Sjhbremovechild(struct witness *parent, struct witness *child) 115265557Sjasone{ 115374912Sjhb struct witness_child_list_entry **wcl, *wcl1; 115465557Sjasone int i; 115565557Sjasone 115674912Sjhb for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 115774912Sjhb for (i = 0; i < (*wcl)->wcl_count; i++) 115874912Sjhb if ((*wcl)->wcl_children[i] == child) 115965557Sjasone goto found; 116065557Sjasone return; 116165557Sjasonefound: 116274912Sjhb (*wcl)->wcl_count--; 116374912Sjhb if ((*wcl)->wcl_count > i) 116474912Sjhb (*wcl)->wcl_children[i] = 116574912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count]; 116674912Sjhb MPASS((*wcl)->wcl_children[i] != NULL); 116774912Sjhb if ((*wcl)->wcl_count != 0) 116865557Sjasone return; 116974912Sjhb wcl1 = *wcl; 117074912Sjhb *wcl = wcl1->wcl_next; 117174912Sjhb witness_child_free(wcl1); 117265557Sjasone} 117365557Sjasone 117465557Sjasonestatic int 117565856Sjhbisitmychild(struct witness *parent, struct witness *child) 117665557Sjasone{ 117774912Sjhb struct witness_child_list_entry *wcl; 117865557Sjasone int i; 117965557Sjasone 118074912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 118174912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 118274912Sjhb if (wcl->wcl_children[i] == child) 118365557Sjasone return (1); 118465557Sjasone } 118565557Sjasone } 118665557Sjasone return (0); 118765557Sjasone} 118865557Sjasone 118965557Sjasonestatic int 119065856Sjhbisitmydescendant(struct witness *parent, struct witness *child) 119165557Sjasone{ 119274912Sjhb struct witness_child_list_entry *wcl; 119374912Sjhb int i, j; 119465557Sjasone 119574912Sjhb if (isitmychild(parent, child)) 119674912Sjhb return (1); 119774912Sjhb j = 0; 119874912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 119967352Sjhb MPASS(j < 1000); 120074912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 120174912Sjhb if (isitmydescendant(wcl->wcl_children[i], child)) 120265557Sjasone return (1); 120365557Sjasone } 120474912Sjhb j++; 120565557Sjasone } 120665557Sjasone return (0); 120765557Sjasone} 120865557Sjasone 1209104094Sphkstatic void 121065557Sjasonewitness_levelall (void) 121165557Sjasone{ 121274912Sjhb struct witness_list *list; 121365856Sjhb struct witness *w, *w1; 121465557Sjasone 121574912Sjhb /* 121674912Sjhb * First clear all levels. 121774912Sjhb */ 121874912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 121974912Sjhb w->w_level = 0; 122074912Sjhb } 122174912Sjhb 122274912Sjhb /* 122374912Sjhb * Look for locks with no parent and level all their descendants. 122474912Sjhb */ 122574912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 122674912Sjhb /* 122774912Sjhb * This is just an optimization, technically we could get 122874912Sjhb * away just walking the all list each time. 122974912Sjhb */ 123074912Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 123174912Sjhb list = &w_sleep; 123274912Sjhb else 123374912Sjhb list = &w_spin; 123474912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 123565557Sjasone if (isitmychild(w1, w)) 123674912Sjhb goto skip; 123765557Sjasone } 123865557Sjasone witness_leveldescendents(w, 0); 123974912Sjhb skip: 124095541Smarcel ; /* silence GCC 3.x */ 124165557Sjasone } 124265557Sjasone} 124365557Sjasone 124465557Sjasonestatic void 124565856Sjhbwitness_leveldescendents(struct witness *parent, int level) 124665557Sjasone{ 124774912Sjhb struct witness_child_list_entry *wcl; 124865557Sjasone int i; 124965557Sjasone 125065557Sjasone if (parent->w_level < level) 125165557Sjasone parent->w_level = level; 125265557Sjasone level++; 125374912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 125474912Sjhb for (i = 0; i < wcl->wcl_count; i++) 125574912Sjhb witness_leveldescendents(wcl->wcl_children[i], level); 125665557Sjasone} 125765557Sjasone 125865557Sjasonestatic void 125965856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...), 126065856Sjhb struct witness *parent) 126165557Sjasone{ 126274912Sjhb struct witness_child_list_entry *wcl; 126374912Sjhb int i, level; 126465557Sjasone 126595543Sjhb level = parent->w_level; 126674912Sjhb prnt("%-2d", level); 126765557Sjasone for (i = 0; i < level; i++) 126865557Sjasone prnt(" "); 126997948Sjhb if (parent->w_refcount > 0) { 127097948Sjhb prnt("%s", parent->w_name); 127197948Sjhb if (parent->w_file != NULL) 127297948Sjhb prnt(" -- last acquired @ %s:%d\n", parent->w_file, 127397948Sjhb parent->w_line); 127497948Sjhb } else 127597948Sjhb prnt("(dead)\n"); 127674912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 127774912Sjhb for (i = 0; i < wcl->wcl_count; i++) 127874912Sjhb witness_displaydescendants(prnt, 127974912Sjhb wcl->wcl_children[i]); 128074912Sjhb} 128165557Sjasone 1282105508Sphk#ifdef BLESSING 128365557Sjasonestatic int 128465856Sjhbblessed(struct witness *w1, struct witness *w2) 128565557Sjasone{ 128665557Sjasone int i; 128765856Sjhb struct witness_blessed *b; 128865557Sjasone 128965557Sjasone for (i = 0; i < blessed_count; i++) { 129065557Sjasone b = &blessed_list[i]; 129174912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 129274912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 129365557Sjasone return (1); 129465557Sjasone continue; 129565557Sjasone } 129674912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 129774912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 129865557Sjasone return (1); 129965557Sjasone } 130065557Sjasone return (0); 130165557Sjasone} 1302105508Sphk#endif 130365557Sjasone 130465856Sjhbstatic struct witness * 130574912Sjhbwitness_get(void) 130665557Sjasone{ 130765856Sjhb struct witness *w; 130865557Sjasone 130976481Sjhb if (witness_dead) { 131076481Sjhb mtx_unlock_spin(&w_mtx); 131176481Sjhb return (NULL); 131276481Sjhb } 131374912Sjhb if (STAILQ_EMPTY(&w_free)) { 131465557Sjasone witness_dead = 1; 131574912Sjhb mtx_unlock_spin(&w_mtx); 131674912Sjhb printf("%s: witness exhausted\n", __func__); 131765557Sjasone return (NULL); 131865557Sjasone } 131974912Sjhb w = STAILQ_FIRST(&w_free); 132074912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 132165856Sjhb bzero(w, sizeof(*w)); 132265557Sjasone return (w); 132365557Sjasone} 132465557Sjasone 132565557Sjasonestatic void 132665856Sjhbwitness_free(struct witness *w) 132765557Sjasone{ 132874912Sjhb 132974912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 133065557Sjasone} 133165557Sjasone 133274912Sjhbstatic struct witness_child_list_entry * 133374912Sjhbwitness_child_get(void) 133465557Sjasone{ 133574912Sjhb struct witness_child_list_entry *wcl; 133665557Sjasone 133776481Sjhb if (witness_dead) { 133876481Sjhb mtx_unlock_spin(&w_mtx); 133976481Sjhb return (NULL); 134076481Sjhb } 134174912Sjhb wcl = w_child_free; 134274912Sjhb if (wcl == NULL) { 134374912Sjhb witness_dead = 1; 134474912Sjhb mtx_unlock_spin(&w_mtx); 134574912Sjhb printf("%s: witness exhausted\n", __func__); 134674912Sjhb return (NULL); 134765557Sjasone } 134874912Sjhb w_child_free = wcl->wcl_next; 134974912Sjhb bzero(wcl, sizeof(*wcl)); 135074912Sjhb return (wcl); 135174912Sjhb} 135269881Sjake 135374912Sjhbstatic void 135474912Sjhbwitness_child_free(struct witness_child_list_entry *wcl) 135574912Sjhb{ 135674912Sjhb 135774912Sjhb wcl->wcl_next = w_child_free; 135874912Sjhb w_child_free = wcl; 135965557Sjasone} 136065557Sjasone 136174912Sjhbstatic struct lock_list_entry * 136274912Sjhbwitness_lock_list_get(void) 136374912Sjhb{ 136474912Sjhb struct lock_list_entry *lle; 136571709Sjhb 136676481Sjhb if (witness_dead) 136776481Sjhb return (NULL); 136874912Sjhb mtx_lock_spin(&w_mtx); 136974912Sjhb lle = w_lock_list_free; 137074912Sjhb if (lle == NULL) { 137174912Sjhb witness_dead = 1; 137274912Sjhb mtx_unlock_spin(&w_mtx); 137374912Sjhb printf("%s: witness exhausted\n", __func__); 137474912Sjhb return (NULL); 137574912Sjhb } 137674912Sjhb w_lock_list_free = lle->ll_next; 137774912Sjhb mtx_unlock_spin(&w_mtx); 137874912Sjhb bzero(lle, sizeof(*lle)); 137974912Sjhb return (lle); 138074912Sjhb} 138174912Sjhb 138274912Sjhbstatic void 138374912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 138471709Sjhb{ 138571709Sjhb 138674912Sjhb mtx_lock_spin(&w_mtx); 138774912Sjhb lle->ll_next = w_lock_list_free; 138874912Sjhb w_lock_list_free = lle; 138974912Sjhb mtx_unlock_spin(&w_mtx); 139071709Sjhb} 139171709Sjhb 139276272Sjhbstatic struct lock_instance * 139376272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 139476272Sjhb{ 139576272Sjhb struct lock_list_entry *lle; 139676272Sjhb struct lock_instance *instance; 139776272Sjhb int i; 139876272Sjhb 139976272Sjhb for (lle = lock_list; lle != NULL; lle = lle->ll_next) 140076272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 140176272Sjhb instance = &lle->ll_children[i]; 140276272Sjhb if (instance->li_lock == lock) 140376272Sjhb return (instance); 140476272Sjhb } 140576272Sjhb return (NULL); 140676272Sjhb} 140776272Sjhb 1408111881Sjhbstatic void 1409111881Sjhbwitness_list_lock(struct lock_instance *instance) 1410111881Sjhb{ 1411111881Sjhb struct lock_object *lock; 1412111881Sjhb 1413111881Sjhb lock = instance->li_lock; 1414111881Sjhb printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1415111881Sjhb "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name); 1416111881Sjhb if (lock->lo_type != lock->lo_name) 1417111881Sjhb printf(" (%s)", lock->lo_type); 1418111881Sjhb printf(" r = %d (%p) locked @ %s:%d\n", 1419111881Sjhb instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1420111881Sjhb instance->li_line); 1421111881Sjhb} 1422111881Sjhb 142374912Sjhbint 142475273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 142572224Sjhb{ 142675273Sjhb struct lock_list_entry *lle; 142774912Sjhb int i, nheld; 142872224Sjhb 142974912Sjhb nheld = 0; 143074912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 143174912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 1432111881Sjhb witness_list_lock(&lle->ll_children[i]); 143374912Sjhb nheld++; 143474912Sjhb } 143575273Sjhb return (nheld); 143675273Sjhb} 143775273Sjhb 143875273Sjhb/* 143983366Sjulian * Calling this on td != curthread is bad unless we are in ddb. 144075273Sjhb */ 1441111881Sjhbstatic int 144283366Sjulianwitness_list(struct thread *td) 144375273Sjhb{ 144475273Sjhb int nheld; 144575273Sjhb 144681489Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 144781489Sjhb#ifdef DDB 144883366Sjulian KASSERT(td == curthread || db_active, 144983366Sjulian ("%s: td != curthread and we aren't in the debugger", __func__)); 145076481Sjhb if (!db_active && witness_dead) 145176481Sjhb return (0); 145281489Sjhb#else 145383366Sjulian KASSERT(td == curthread, ("%s: p != curthread", __func__)); 145481489Sjhb if (witness_dead) 145581489Sjhb return (0); 145681489Sjhb#endif 145783366Sjulian nheld = witness_list_locks(&td->td_sleeplocks); 145875273Sjhb 145974912Sjhb /* 146083366Sjulian * We only handle spinlocks if td == curthread. This is somewhat broken 146183798Sjhb * if td is currently executing on some other CPU and holds spin locks 146275273Sjhb * as we won't display those locks. If we had a MI way of getting 146383798Sjhb * the per-cpu data for a given cpu then we could use 146483798Sjhb * td->td_kse->ke_oncpu to get the list of spinlocks for this thread 146583798Sjhb * and "fix" this. 146691905Sjhb * 146791905Sjhb * That still wouldn't really fix this unless we locked sched_lock 146891905Sjhb * or stopped the other CPU to make sure it wasn't changing the list 146991905Sjhb * out from under us. It is probably best to just not try to handle 147091905Sjhb * threads on other CPU's for now. 147174912Sjhb */ 147297006Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 147375273Sjhb nheld += witness_list_locks(PCPU_PTR(spinlocks)); 147497006Sjhb 147574912Sjhb return (nheld); 147672224Sjhb} 147771709Sjhb 147865557Sjasonevoid 147974912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 148065557Sjasone{ 148176272Sjhb struct lock_instance *instance; 148271320Sjasone 148382284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 148480747Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 148571352Sjasone return; 148682243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 148782243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 148882243Sjhb lock->lo_class->lc_name, lock->lo_name); 148983366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 149082243Sjhb if (instance == NULL) 149182243Sjhb panic("%s: lock (%s) %s not locked", __func__, 149282243Sjhb lock->lo_class->lc_name, lock->lo_name); 149376272Sjhb *filep = instance->li_file; 149476272Sjhb *linep = instance->li_line; 149565557Sjasone} 149665557Sjasone 149765557Sjasonevoid 149874912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 149965557Sjasone{ 150076272Sjhb struct lock_instance *instance; 150171320Sjasone 150282284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 150380747Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 150471352Sjasone return; 150582243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 150682243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 150782243Sjhb lock->lo_class->lc_name, lock->lo_name); 150883366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 150982243Sjhb if (instance == NULL) 151082243Sjhb panic("%s: lock (%s) %s not locked", __func__, 151182243Sjhb lock->lo_class->lc_name, lock->lo_name); 151274912Sjhb lock->lo_witness->w_file = file; 151374912Sjhb lock->lo_witness->w_line = line; 151476272Sjhb instance->li_file = file; 151576272Sjhb instance->li_line = line; 151665557Sjasone} 151765557Sjasone 151878871Sjhbvoid 151978871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 152078871Sjhb{ 152178871Sjhb#ifdef INVARIANT_SUPPORT 152278871Sjhb struct lock_instance *instance; 152378871Sjhb 152480747Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 152578941Sjhb return; 152678871Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 152783366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 152878871Sjhb else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 152978871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 153086422Sjhb else { 153178871Sjhb panic("Lock (%s) %s is not sleep or spin!", 153278871Sjhb lock->lo_class->lc_name, lock->lo_name); 153386422Sjhb return; 153486422Sjhb } 153578871Sjhb switch (flags) { 153678871Sjhb case LA_UNLOCKED: 153778871Sjhb if (instance != NULL) 153878871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 153978871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 154078871Sjhb break; 154178871Sjhb case LA_LOCKED: 154278871Sjhb case LA_LOCKED | LA_RECURSED: 154378871Sjhb case LA_LOCKED | LA_NOTRECURSED: 154478871Sjhb case LA_SLOCKED: 154578871Sjhb case LA_SLOCKED | LA_RECURSED: 154678871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 154778871Sjhb case LA_XLOCKED: 154878871Sjhb case LA_XLOCKED | LA_RECURSED: 154978871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 155086422Sjhb if (instance == NULL) { 155178871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 155278871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 155386422Sjhb break; 155486422Sjhb } 155578871Sjhb if ((flags & LA_XLOCKED) != 0 && 155678871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 155778871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 155878871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 155978871Sjhb if ((flags & LA_SLOCKED) != 0 && 156078871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 156178871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 156278871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 156378871Sjhb if ((flags & LA_RECURSED) != 0 && 156478871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 156578871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 156678871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 156778871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 156878871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 156978871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 157078871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 157178871Sjhb break; 157278871Sjhb default: 157378871Sjhb panic("Invalid lock assertion at %s:%d.", file, line); 157478871Sjhb 157578871Sjhb } 157678871Sjhb#endif /* INVARIANT_SUPPORT */ 157778871Sjhb} 157878871Sjhb 157974912Sjhb#ifdef DDB 158074912Sjhb 158174930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 158274912Sjhb{ 158383366Sjulian struct thread *td; 158483366Sjulian pid_t pid; 158575273Sjhb struct proc *p; 158674912Sjhb 158775273Sjhb if (have_addr) { 158875273Sjhb pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 158975273Sjhb ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 159075273Sjhb ((addr >> 16) % 16) * 10000; 159175273Sjhb /* sx_slock(&allproc_lock); */ 159283366Sjulian FOREACH_PROC_IN_SYSTEM(p) { 159375273Sjhb if (p->p_pid == pid) 159475273Sjhb break; 159575273Sjhb } 159675273Sjhb /* sx_sunlock(&allproc_lock); */ 159775273Sjhb if (p == NULL) { 159875273Sjhb db_printf("pid %d not found\n", pid); 159975273Sjhb return; 160075273Sjhb } 160190361Sjulian FOREACH_THREAD_IN_PROC(p, td) { 160290361Sjulian witness_list(td); 160390361Sjulian } 160483366Sjulian } else { 160583366Sjulian td = curthread; 160690361Sjulian witness_list(td); 160783366Sjulian } 160874912Sjhb} 160974912Sjhb 161074912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 161174912Sjhb{ 161274912Sjhb 161374912Sjhb witness_display(db_printf); 161474912Sjhb} 161574912Sjhb#endif 1616