subr_witness.c revision 77900
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 77900 2001-06-08 05:24:21Z 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 5968790Sjhb#include "opt_ddb.h" 6067676Sjhb#include "opt_witness.h" 6167676Sjhb 6265557Sjasone#include <sys/param.h> 6367352Sjhb#include <sys/bus.h> 6467352Sjhb#include <sys/kernel.h> 6574912Sjhb#include <sys/ktr.h> 6674912Sjhb#include <sys/lock.h> 6767352Sjhb#include <sys/malloc.h> 6874912Sjhb#include <sys/mutex.h> 6965557Sjasone#include <sys/proc.h> 7067676Sjhb#include <sys/sysctl.h> 7165557Sjasone#include <sys/systm.h> 7265557Sjasone 7368790Sjhb#include <ddb/ddb.h> 7468790Sjhb 7574912Sjhb#define WITNESS_COUNT 200 7674912Sjhb#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 7765557Sjasone/* 7874912Sjhb * XXX: This is somewhat bogus, as we assume here that at most 1024 processes 7974912Sjhb * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 8074912Sjhb * probably be safe for the most part, but it's still a SWAG. 8167352Sjhb */ 8274912Sjhb#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 8371352Sjasone 8474912Sjhb#define WITNESS_NCHILDREN 6 8571352Sjasone 8674912Sjhbstruct witness_child_list_entry; 8771352Sjasone 8874912Sjhbstruct witness { 8974912Sjhb const char *w_name; 9074912Sjhb struct lock_class *w_class; 9174912Sjhb STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 9274912Sjhb STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 9374912Sjhb struct witness_child_list_entry *w_children; /* Great evilness... */ 9474912Sjhb const char *w_file; 9574912Sjhb int w_line; 9674912Sjhb u_int w_level; 9774912Sjhb u_int w_refcount; 9874912Sjhb u_char w_Giant_squawked:1; 9974912Sjhb u_char w_other_squawked:1; 10074912Sjhb u_char w_same_squawked:1; 10174912Sjhb}; 10271352Sjasone 10374912Sjhbstruct witness_child_list_entry { 10474912Sjhb struct witness_child_list_entry *wcl_next; 10574912Sjhb struct witness *wcl_children[WITNESS_NCHILDREN]; 10674912Sjhb u_int wcl_count; 10774912Sjhb}; 10871352Sjasone 10974912SjhbSTAILQ_HEAD(witness_list, witness); 11071352Sjasone 11174912Sjhbstruct witness_blessed { 11274912Sjhb const char *b_lock1; 11374912Sjhb const char *b_lock2; 11474912Sjhb}; 11571352Sjasone 11674912Sjhbstruct witness_order_list_entry { 11774912Sjhb const char *w_name; 11874912Sjhb struct lock_class *w_class; 11974912Sjhb}; 12071352Sjasone 12174912Sjhbstatic struct witness *enroll(const char *description, 12274912Sjhb struct lock_class *lock_class); 12374912Sjhbstatic int itismychild(struct witness *parent, struct witness *child); 12474912Sjhbstatic void removechild(struct witness *parent, struct witness *child); 12574912Sjhbstatic int isitmychild(struct witness *parent, struct witness *child); 12674912Sjhbstatic int isitmydescendant(struct witness *parent, struct witness *child); 12774912Sjhbstatic int dup_ok(struct witness *); 12874912Sjhbstatic int blessed(struct witness *, struct witness *); 12974912Sjhbstatic void witness_display_list(void(*prnt)(const char *fmt, ...), 13074912Sjhb struct witness_list *list); 13174912Sjhbstatic void witness_displaydescendants(void(*)(const char *fmt, ...), 13274912Sjhb struct witness *); 13374912Sjhbstatic void witness_leveldescendents(struct witness *parent, int level); 13474912Sjhbstatic void witness_levelall(void); 13574912Sjhbstatic struct witness *witness_get(void); 13674912Sjhbstatic void witness_free(struct witness *m); 13774912Sjhbstatic struct witness_child_list_entry *witness_child_get(void); 13874912Sjhbstatic void witness_child_free(struct witness_child_list_entry *wcl); 13974912Sjhbstatic struct lock_list_entry *witness_lock_list_get(void); 14074912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 14174912Sjhbstatic void witness_display(void(*)(const char *fmt, ...)); 14276272Sjhbstatic struct lock_instance *find_instance(struct lock_list_entry *lock_list, 14376272Sjhb struct lock_object *lock); 14472200Sbmilekic 14574912SjhbMALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 14672200Sbmilekic 14777843Speterstatic int witness_watch = 1; 14877900SpeterTUNABLE_INT("debug.witness_watch", &witness_watch); 14974912SjhbSYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 15071352Sjasone 15167352Sjhb#ifdef DDB 15272200Sbmilekic/* 15367676Sjhb * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 15465557Sjasone * drop into kdebug() when: 15565557Sjasone * - a lock heirarchy violation occurs 15665557Sjasone * - locks are held when going to sleep. 15765557Sjasone */ 15867676Sjhb#ifdef WITNESS_DDB 15977843Speterint witness_ddb = 1; 16067676Sjhb#else 16177843Speterint witness_ddb = 0; 16265557Sjasone#endif 16377900SpeterTUNABLE_INT("debug.witness_ddb", &witness_ddb); 16467676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 16567676Sjhb#endif /* DDB */ 16665557Sjasone 16767676Sjhb#ifdef WITNESS_SKIPSPIN 16877843Speterint witness_skipspin = 1; 16967676Sjhb#else 17077843Speterint witness_skipspin = 0; 17165557Sjasone#endif 17277900SpeterTUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 17367676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 17467676Sjhb ""); 17565557Sjasone 17674912Sjhbstatic struct mtx w_mtx; 17774912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 17874912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 17974912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 18074912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 18174912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL; 18274912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 18374912Sjhbstatic int witness_dead; /* fatal error, probably no memory */ 18465557Sjasone 18574912Sjhbstatic struct witness w_data[WITNESS_COUNT]; 18674912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 18774912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 18865557Sjasone 18974912Sjhbstatic struct witness_order_list_entry order_lists[] = { 19074912Sjhb { "Giant", &lock_class_mtx_sleep }, 19174912Sjhb { "proctree", &lock_class_sx }, 19274912Sjhb { "allproc", &lock_class_sx }, 19374912Sjhb { "process lock", &lock_class_mtx_sleep }, 19474912Sjhb { "uidinfo hash", &lock_class_mtx_sleep }, 19574912Sjhb { "uidinfo struct", &lock_class_mtx_sleep }, 19674912Sjhb { NULL, NULL }, 19775464Sjhb /* 19875464Sjhb * spin locks 19975464Sjhb */ 20072224Sjhb#if defined(__i386__) && defined (SMP) 20174912Sjhb { "com", &lock_class_mtx_spin }, 20272224Sjhb#endif 20374912Sjhb { "sio", &lock_class_mtx_spin }, 20472224Sjhb#ifdef __i386__ 20574912Sjhb { "cy", &lock_class_mtx_spin }, 20672224Sjhb#endif 20774912Sjhb { "ng_node", &lock_class_mtx_spin }, 20874912Sjhb { "ng_worklist", &lock_class_mtx_spin }, 20974912Sjhb { "ithread table lock", &lock_class_mtx_spin }, 21074912Sjhb { "sched lock", &lock_class_mtx_spin }, 21174912Sjhb { "clk", &lock_class_mtx_spin }, 21274912Sjhb { "callout", &lock_class_mtx_spin }, 21365557Sjasone /* 21465557Sjasone * leaf locks 21565557Sjasone */ 21672224Sjhb#ifdef SMP 21775464Sjhb { "ap boot", &lock_class_mtx_spin }, 21871576Sjasone#ifdef __i386__ 21974912Sjhb { "imen", &lock_class_mtx_spin }, 22071576Sjasone#endif 22174912Sjhb { "smp rendezvous", &lock_class_mtx_spin }, 22272224Sjhb#endif 22374912Sjhb { NULL, NULL }, 22474912Sjhb { NULL, NULL } 22565557Sjasone}; 22665557Sjasone 22774912Sjhbstatic const char *dup_list[] = { 22873912Sjhb "process lock", 22965557Sjasone NULL 23065557Sjasone}; 23165557Sjasone 23265557Sjasone/* 23365557Sjasone * Pairs of locks which have been blessed 23465557Sjasone * Don't complain about order problems with blessed locks 23565557Sjasone */ 23665856Sjhbstatic struct witness_blessed blessed_list[] = { 23765557Sjasone}; 23872200Sbmilekicstatic int blessed_count = 23972200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 24065557Sjasone 24174912Sjhb/* 24274912Sjhb * List of all locks in the system. 24374912Sjhb */ 24474912SjhbSTAILQ_HEAD(, lock_object) all_locks = STAILQ_HEAD_INITIALIZER(all_locks); 24574912Sjhb 24674912Sjhbstatic struct mtx all_mtx = { 24774912Sjhb { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 24874912Sjhb "All locks list", /* mtx_object.lo_name */ 24974912Sjhb LO_INITIALIZED, /* mtx_object.lo_flags */ 25074912Sjhb { NULL }, /* mtx_object.lo_list */ 25174912Sjhb NULL }, /* mtx_object.lo_witness */ 25274912Sjhb MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 25374912Sjhb 0, /* mtx_savecrit */ 25474912Sjhb TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 25574912Sjhb { NULL, NULL } /* mtx_contested */ 25674912Sjhb}; 25774912Sjhb 25874912Sjhb/* 25974912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 26074912Sjhb */ 26174912Sjhbstatic int witness_cold = 1; 26274912Sjhb 26374912Sjhb/* 26474912Sjhb * Global variables for book keeping. 26574912Sjhb */ 26674912Sjhbstatic int lock_cur_cnt; 26774912Sjhbstatic int lock_max_cnt; 26874912Sjhb 26974912Sjhb/* 27074912Sjhb * The WITNESS-enabled diagnostic code. 27174912Sjhb */ 27271352Sjasonestatic void 27374912Sjhbwitness_initialize(void *dummy __unused) 27465557Sjasone{ 27574912Sjhb struct lock_object *lock; 27674912Sjhb struct witness_order_list_entry *order; 27774912Sjhb struct witness *w, *w1; 27874912Sjhb int i; 27965557Sjasone 28074912Sjhb /* 28174912Sjhb * We have to release Giant before initializing its witness 28274912Sjhb * structure so that WITNESS doesn't get confused. 28374912Sjhb */ 28474912Sjhb mtx_unlock(&Giant); 28574912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 28674912Sjhb 28774912Sjhb STAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 28874912Sjhb mtx_init(&w_mtx, "witness lock", MTX_SPIN | MTX_QUIET | MTX_NOWITNESS); 28974912Sjhb for (i = 0; i < WITNESS_COUNT; i++) 29074912Sjhb witness_free(&w_data[i]); 29174912Sjhb for (i = 0; i < WITNESS_CHILDCOUNT; i++) 29274912Sjhb witness_child_free(&w_childdata[i]); 29374912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 29474912Sjhb witness_lock_list_free(&w_locklistdata[i]); 29574912Sjhb 29674912Sjhb /* First add in all the specified order lists. */ 29774912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 29874912Sjhb w = enroll(order->w_name, order->w_class); 29975569Sjhb if (w == NULL) 30075569Sjhb continue; 30174912Sjhb w->w_file = "order list"; 30274912Sjhb for (order++; order->w_name != NULL; order++) { 30374912Sjhb w1 = enroll(order->w_name, order->w_class); 30475569Sjhb if (w1 == NULL) 30575569Sjhb continue; 30674912Sjhb w1->w_file = "order list"; 30774912Sjhb itismychild(w, w1); 30874912Sjhb w = w1; 30965557Sjasone } 31065557Sjasone } 31165557Sjasone 31274912Sjhb /* Iterate through all locks and add them to witness. */ 31374912Sjhb mtx_lock(&all_mtx); 31474912Sjhb STAILQ_FOREACH(lock, &all_locks, lo_list) { 31574912Sjhb if (lock->lo_flags & LO_WITNESS) 31674912Sjhb lock->lo_witness = enroll(lock->lo_name, 31774912Sjhb lock->lo_class); 31874912Sjhb else 31974912Sjhb lock->lo_witness = NULL; 32074912Sjhb } 32174912Sjhb mtx_unlock(&all_mtx); 32274912Sjhb 32374912Sjhb /* Mark the witness code as being ready for use. */ 32474912Sjhb atomic_store_rel_int(&witness_cold, 0); 32574912Sjhb 32674912Sjhb mtx_lock(&Giant); 32765557Sjasone} 32874912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 32965557Sjasone 33074912Sjhbvoid 33174912Sjhbwitness_init(struct lock_object *lock) 33274912Sjhb{ 33374912Sjhb struct lock_class *class; 33474912Sjhb 33574912Sjhb class = lock->lo_class; 33674912Sjhb if (lock->lo_flags & LO_INITIALIZED) 33774912Sjhb panic("%s: lock (%s) %s is already initialized!\n", __func__, 33874912Sjhb class->lc_name, lock->lo_name); 33974912Sjhb 34074912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 34174912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 34274912Sjhb panic("%s: lock (%s) %s can not be recursable!\n", __func__, 34374912Sjhb class->lc_name, lock->lo_name); 34476272Sjhb 34574912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 34674912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 34774912Sjhb panic("%s: lock (%s) %s can not be sleepable!\n", __func__, 34874912Sjhb class->lc_name, lock->lo_name); 34976272Sjhb 35074912Sjhb mtx_lock(&all_mtx); 35174912Sjhb STAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 35274912Sjhb lock->lo_flags |= LO_INITIALIZED; 35374912Sjhb lock_cur_cnt++; 35474912Sjhb if (lock_cur_cnt > lock_max_cnt) 35574912Sjhb lock_max_cnt = lock_cur_cnt; 35674912Sjhb mtx_unlock(&all_mtx); 35774912Sjhb if (!witness_cold && !witness_dead && 35874912Sjhb (lock->lo_flags & LO_WITNESS) != 0) 35974912Sjhb lock->lo_witness = enroll(lock->lo_name, class); 36074912Sjhb else 36174912Sjhb lock->lo_witness = NULL; 36274912Sjhb} 36374912Sjhb 36474912Sjhbvoid 36574912Sjhbwitness_destroy(struct lock_object *lock) 36674912Sjhb{ 36775362Sjhb struct witness *w; 36874912Sjhb 36974912Sjhb if (witness_cold) 37074912Sjhb panic("lock (%s) %s destroyed while witness_cold", 37174912Sjhb lock->lo_class->lc_name, lock->lo_name); 37274912Sjhb 37374912Sjhb if ((lock->lo_flags & LO_INITIALIZED) == 0) 37474912Sjhb panic("%s: lock (%s) %s is not initialized!\n", __func__, 37574912Sjhb lock->lo_class->lc_name, lock->lo_name); 37674912Sjhb 37776272Sjhb /* XXX: need to verify that no one holds the lock */ 37875362Sjhb w = lock->lo_witness; 37975362Sjhb if (w != NULL) { 38075362Sjhb mtx_lock_spin(&w_mtx); 38175362Sjhb w->w_refcount--; 38275362Sjhb if (w->w_refcount == 0) { 38375362Sjhb w->w_name = "(dead)"; 38475362Sjhb w->w_file = "(dead)"; 38575362Sjhb w->w_line = 0; 38675362Sjhb } 38775362Sjhb mtx_unlock_spin(&w_mtx); 38875362Sjhb } 38975362Sjhb 39074912Sjhb mtx_lock(&all_mtx); 39174912Sjhb lock_cur_cnt--; 39274912Sjhb STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list); 39374912Sjhb lock->lo_flags &= LO_INITIALIZED; 39474912Sjhb mtx_unlock(&all_mtx); 39574912Sjhb} 39674912Sjhb 39771352Sjasonestatic void 39874912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...), 39974912Sjhb struct witness_list *list) 40071352Sjasone{ 40171352Sjasone struct witness *w, *w1; 40274912Sjhb int found; 40371352Sjasone 40474912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 40574912Sjhb if (w->w_file == NULL) 40671352Sjasone continue; 40774912Sjhb found = 0; 40874912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 40974912Sjhb if (isitmychild(w1, w)) { 41074912Sjhb found++; 41171352Sjasone break; 41274912Sjhb } 41371352Sjasone } 41474912Sjhb if (found) 41571352Sjasone continue; 41671352Sjasone /* 41771352Sjasone * This lock has no anscestors, display its descendants. 41871352Sjasone */ 41971352Sjasone witness_displaydescendants(prnt, w); 42071352Sjasone } 42174912Sjhb} 42272224Sjhb 42374912Sjhbstatic void 42474912Sjhbwitness_display(void(*prnt)(const char *fmt, ...)) 42574912Sjhb{ 42674912Sjhb struct witness *w; 42774912Sjhb 42874912Sjhb KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 42974912Sjhb witness_levelall(); 43074912Sjhb 43172224Sjhb /* 43274930Sjhb * First, handle sleep locks which have been acquired at least 43374912Sjhb * once. 43474912Sjhb */ 43574912Sjhb prnt("Sleep locks:\n"); 43674912Sjhb witness_display_list(prnt, &w_sleep); 43774912Sjhb 43874912Sjhb /* 43974930Sjhb * Now do spin locks which have been acquired at least once. 44072224Sjhb */ 44174912Sjhb prnt("\nSpin locks:\n"); 44274912Sjhb witness_display_list(prnt, &w_spin); 44372224Sjhb 44472224Sjhb /* 44574930Sjhb * Finally, any locks which have not been acquired yet. 44672224Sjhb */ 44774912Sjhb prnt("\nLocks which were never acquired:\n"); 44874912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 44971352Sjasone if (w->w_file != NULL) 45071352Sjasone continue; 45174912Sjhb prnt("%s\n", w->w_name); 45271352Sjasone } 45371352Sjasone} 45471352Sjasone 45565557Sjasonevoid 45674912Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 45765557Sjasone{ 45874912Sjhb struct lock_list_entry **lock_list, *lle; 45976272Sjhb struct lock_instance *lock1, *lock2; 46074912Sjhb struct lock_class *class; 46165856Sjhb struct witness *w, *w1; 46265557Sjasone struct proc *p; 46374912Sjhb int i, j; 46467676Sjhb#ifdef DDB 46567676Sjhb int go_into_ddb = 0; 46667676Sjhb#endif /* DDB */ 46765557Sjasone 46874912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 46974912Sjhb panicstr) 47071320Sjasone return; 47174912Sjhb w = lock->lo_witness; 47274912Sjhb class = lock->lo_class; 47372393Sbmilekic p = curproc; 47465557Sjasone 47574944Sjhb /* 47674944Sjhb * We have to hold a spinlock to keep lock_list valid across the check 47774944Sjhb * in the LC_SLEEPLOCK case. In the LC_SPINLOCK case, it is already 47874944Sjhb * protected by the spinlock we are currently performing the witness 47974944Sjhb * checks on, so it is ok to release the lock after performing this 48074944Sjhb * check. All we have to protect is the LC_SLEEPLOCK case when no 48174944Sjhb * spinlocks are held as we may get preempted during this check and 48274944Sjhb * lock_list could end up pointing to some other CPU's spinlock list. 48374944Sjhb */ 48474944Sjhb mtx_lock_spin(&w_mtx); 48574912Sjhb lock_list = PCPU_PTR(spinlocks); 48674912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 48776772Sjhb if (*lock_list != NULL && (flags & LOP_TRYLOCK) == 0) { 48874944Sjhb mtx_unlock_spin(&w_mtx); 48974912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 49074912Sjhb class->lc_name, lock->lo_name, file, line); 49174944Sjhb } 49274912Sjhb lock_list = &p->p_sleeplocks; 49374912Sjhb } 49474944Sjhb mtx_unlock_spin(&w_mtx); 49565557Sjasone 49676772Sjhb /* 49776772Sjhb * Try locks do not block if they fail to acquire the lock, thus 49876772Sjhb * there is no danger of deadlocks or of switching while holding a 49976772Sjhb * spin lock if we acquire a lock via a try operation. 50076772Sjhb */ 50174912Sjhb if (flags & LOP_TRYLOCK) 50265557Sjasone goto out; 50365557Sjasone 50465557Sjasone /* 50574912Sjhb * Is this the first lock acquired? If so, then no order checking 50674912Sjhb * is needed. 50765557Sjasone */ 50874912Sjhb if (*lock_list == NULL) 50965557Sjasone goto out; 51065557Sjasone 51174912Sjhb /* 51276272Sjhb * Check to see if we are recursing on a lock we already own. 51376272Sjhb */ 51476272Sjhb lock1 = find_instance(*lock_list, lock); 51576272Sjhb if (lock1 != NULL) { 51676272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 51776272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 51876272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 51976272Sjhb class->lc_name, lock->lo_name, file, line); 52076272Sjhb printf("while exclusively locked from %s:%d\n", 52176272Sjhb lock1->li_file, lock1->li_line); 52276272Sjhb panic("share->excl"); 52376272Sjhb } 52476272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 52576272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 52676272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 52776272Sjhb class->lc_name, lock->lo_name, file, line); 52876272Sjhb printf("while share locked from %s:%d\n", 52976272Sjhb lock1->li_file, lock1->li_line); 53076272Sjhb panic("excl->share"); 53176272Sjhb } 53276272Sjhb lock1->li_flags++; 53376272Sjhb if ((lock->lo_flags & LO_RECURSABLE) == 0) { 53476272Sjhb printf( 53576272Sjhb "recursed on non-recursive lock (%s) %s @ %s:%d\n", 53676272Sjhb class->lc_name, lock->lo_name, file, line); 53776272Sjhb printf("first acquired @ %s:%d\n", lock1->li_file, 53876272Sjhb lock1->li_line); 53976272Sjhb panic("recurse"); 54076272Sjhb } 54176272Sjhb lock1->li_file = file; 54276272Sjhb lock1->li_line = line; 54376272Sjhb return; 54476272Sjhb } 54576272Sjhb 54676272Sjhb /* 54774912Sjhb * Check for duplicate locks of the same type. Note that we only 54874912Sjhb * have to check for this on the last lock we just acquired. Any 54974912Sjhb * other cases will be caught as lock order violations. 55074912Sjhb */ 55176272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 55276272Sjhb w1 = lock1->li_lock->lo_witness; 55374912Sjhb if (w1 == w) { 55465557Sjasone if (w->w_same_squawked || dup_ok(w)) 55565557Sjasone goto out; 55665557Sjasone w->w_same_squawked = 1; 55775755Sjhb printf("acquiring duplicate lock of same type: \"%s\"\n", 55874912Sjhb lock->lo_name); 55976272Sjhb printf(" 1st @ %s:%d\n", lock1->li_file, lock1->li_line); 56065557Sjasone printf(" 2nd @ %s:%d\n", file, line); 56167676Sjhb#ifdef DDB 56267676Sjhb go_into_ddb = 1; 56367676Sjhb#endif /* DDB */ 56465557Sjasone goto out; 56565557Sjasone } 56665557Sjasone MPASS(!mtx_owned(&w_mtx)); 56774912Sjhb mtx_lock_spin(&w_mtx); 56865557Sjasone /* 56965557Sjasone * If we have a known higher number just say ok 57065557Sjasone */ 57165557Sjasone if (witness_watch > 1 && w->w_level > w1->w_level) { 57274912Sjhb mtx_unlock_spin(&w_mtx); 57365557Sjasone goto out; 57465557Sjasone } 57574912Sjhb if (isitmydescendant(w1, w)) { 57674912Sjhb mtx_unlock_spin(&w_mtx); 57765557Sjasone goto out; 57865557Sjasone } 57974912Sjhb for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 58074912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 58165557Sjasone 58274912Sjhb MPASS(j < WITNESS_COUNT); 58376272Sjhb lock1 = &lle->ll_children[i]; 58476272Sjhb w1 = lock1->li_lock->lo_witness; 58574912Sjhb 58674912Sjhb /* 58774912Sjhb * If this lock doesn't undergo witness checking, 58874912Sjhb * then skip it. 58974912Sjhb */ 59074912Sjhb if (w1 == NULL) { 59176272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 59274912Sjhb ("lock missing witness structure")); 59374912Sjhb continue; 59474912Sjhb } 59576272Sjhb /* 59676272Sjhb * If we are locking Giant and we slept with this 59776272Sjhb * lock, then skip it. 59876272Sjhb */ 59976272Sjhb if ((lock1->li_flags & LI_SLEPT) != 0 && 60076272Sjhb lock == &Giant.mtx_object) 60176272Sjhb continue; 60274912Sjhb if (!isitmydescendant(w, w1)) 60374912Sjhb continue; 60474912Sjhb /* 60574912Sjhb * We have a lock order violation, check to see if it 60674912Sjhb * is allowed or has already been yelled about. 60774912Sjhb */ 60874912Sjhb mtx_unlock_spin(&w_mtx); 60965557Sjasone if (blessed(w, w1)) 61065557Sjasone goto out; 61176272Sjhb if (lock1->li_lock == &Giant.mtx_object) { 61265557Sjasone if (w1->w_Giant_squawked) 61365557Sjasone goto out; 61465557Sjasone else 61565557Sjasone w1->w_Giant_squawked = 1; 61665557Sjasone } else { 61765557Sjasone if (w1->w_other_squawked) 61865557Sjasone goto out; 61965557Sjasone else 62065557Sjasone w1->w_other_squawked = 1; 62165557Sjasone } 62274912Sjhb /* 62374912Sjhb * Ok, yell about it. 62474912Sjhb */ 62565557Sjasone printf("lock order reversal\n"); 62674912Sjhb /* 62774912Sjhb * Try to locate an earlier lock with 62874912Sjhb * witness w in our list. 62974912Sjhb */ 63074912Sjhb do { 63176272Sjhb lock2 = &lle->ll_children[i]; 63276272Sjhb MPASS(lock2->li_lock != NULL); 63376272Sjhb if (lock2->li_lock->lo_witness == w) 63474912Sjhb break; 63574912Sjhb i--; 63674912Sjhb if (i == 0 && lle->ll_next != NULL) { 63774912Sjhb lle = lle->ll_next; 63874912Sjhb i = lle->ll_count - 1; 63974912Sjhb MPASS(i != 0); 64074912Sjhb } 64174912Sjhb } while (i >= 0); 64276272Sjhb if (i < 0) { 64376272Sjhb printf(" 1st %p %s @ %s:%d\n", lock1->li_lock, 64476272Sjhb lock1->li_lock->lo_name, lock1->li_file, 64576272Sjhb lock1->li_line); 64676272Sjhb printf(" 2nd %p %s @ %s:%d\n", lock, 64776272Sjhb lock->lo_name, file, line); 64876272Sjhb } else { 64976272Sjhb printf(" 1st %p %s @ %s:%d\n", lock2->li_lock, 65076272Sjhb lock2->li_lock->lo_name, lock2->li_file, 65176272Sjhb lock2->li_line); 65276272Sjhb printf(" 2nd %p %s @ %s:%d\n", lock1->li_lock, 65376272Sjhb lock1->li_lock->lo_name, lock1->li_file, 65476272Sjhb lock1->li_line); 65576272Sjhb printf(" 3rd %p %s @ %s:%d\n", lock, 65676272Sjhb lock->lo_name, file, line); 65776272Sjhb } 65867676Sjhb#ifdef DDB 65967676Sjhb go_into_ddb = 1; 66067676Sjhb#endif /* DDB */ 66165557Sjasone goto out; 66265557Sjasone } 66365557Sjasone } 66476272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 66576272Sjhb if (!itismychild(lock1->li_lock->lo_witness, w)) 66674912Sjhb mtx_unlock_spin(&w_mtx); 66765557Sjasone 66865557Sjasoneout: 66967676Sjhb#ifdef DDB 67067676Sjhb if (witness_ddb && go_into_ddb) 67175711Sjhb Debugger(__func__); 67267676Sjhb#endif /* DDB */ 67365557Sjasone w->w_file = file; 67465557Sjasone w->w_line = line; 67574912Sjhb 67674912Sjhb lle = *lock_list; 67776272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 67874912Sjhb *lock_list = witness_lock_list_get(); 67974912Sjhb if (*lock_list == NULL) 68065557Sjasone return; 68174912Sjhb (*lock_list)->ll_next = lle; 68274912Sjhb lle = *lock_list; 68365557Sjasone } 68476272Sjhb lock1 = &lle->ll_children[lle->ll_count++]; 68576272Sjhb lock1->li_lock = lock; 68676272Sjhb lock1->li_line = line; 68776272Sjhb lock1->li_file = file; 68876272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 68976272Sjhb lock1->li_flags = LI_EXCLUSIVE; 69076272Sjhb else 69176272Sjhb lock1->li_flags = 0; 69265557Sjasone} 69365557Sjasone 69465557Sjasonevoid 69574912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 69665557Sjasone{ 69774912Sjhb struct lock_list_entry **lock_list, *lle; 69876272Sjhb struct lock_instance *instance; 69974912Sjhb struct lock_class *class; 70074016Sjhb struct proc *p; 70174912Sjhb int i, j; 70265557Sjasone 70374912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 70474912Sjhb panicstr) 70571352Sjasone return; 70674016Sjhb p = curproc; 70774912Sjhb class = lock->lo_class; 70876272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 70974912Sjhb lock_list = &p->p_sleeplocks; 71076272Sjhb else 71174912Sjhb lock_list = PCPU_PTR(spinlocks); 71274912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 71376272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 71476272Sjhb instance = &(*lock_list)->ll_children[i]; 71576272Sjhb if (instance->li_lock == lock) { 71676272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 71776272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 71876272Sjhb printf( 71976272Sjhb "shared unlock of (%s) %s @ %s:%d\n", 72076272Sjhb class->lc_name, lock->lo_name, 72176272Sjhb file, line); 72276272Sjhb printf( 72376272Sjhb "while exclusively locked from %s:%d\n", 72476272Sjhb instance->li_file, 72576272Sjhb instance->li_line); 72676272Sjhb panic("excl->ushare"); 72776272Sjhb } 72876272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 72976272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 73076272Sjhb printf( 73176272Sjhb "exclusive unlock of (%s) %s @ %s:%d\n", 73276272Sjhb class->lc_name, lock->lo_name, 73376272Sjhb file, line); 73476272Sjhb printf( 73576272Sjhb "while share locked from %s:%d\n", 73676272Sjhb instance->li_file, 73776272Sjhb instance->li_line); 73876272Sjhb panic("share->uexcl"); 73976272Sjhb } 74076272Sjhb /* If we are recursed, unrecurse. */ 74176272Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 74276272Sjhb instance->li_flags--; 74376272Sjhb goto out; 74476272Sjhb } 74574912Sjhb (*lock_list)->ll_count--; 74674912Sjhb for (j = i; j < (*lock_list)->ll_count; j++) 74774912Sjhb (*lock_list)->ll_children[j] = 74874912Sjhb (*lock_list)->ll_children[j + 1]; 74974912Sjhb if ((*lock_list)->ll_count == 0) { 75074912Sjhb lle = *lock_list; 75174912Sjhb *lock_list = lle->ll_next; 75274912Sjhb witness_lock_list_free(lle); 75374912Sjhb } 75476272Sjhb goto out; 75574912Sjhb } 75676272Sjhb } 75776272Sjhb panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 75876272Sjhb file, line); 75976272Sjhbout: 76076272Sjhb /* 76176272Sjhb * We don't need to protect this PCPU_GET() here against preemption 76276272Sjhb * because if we hold any spinlocks then we are already protected, 76376272Sjhb * and if we don't we will get NULL if we hold no spinlocks even if 76476272Sjhb * we switch CPU's while reading it. 76576272Sjhb */ 76676272Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 76776272Sjhb if ((flags & LOP_NOSWITCH) == 0 && PCPU_GET(spinlocks) != NULL) 76876272Sjhb panic("switchable sleep unlock (%s) %s @ %s:%d", 76976272Sjhb class->lc_name, lock->lo_name, file, line); 77076272Sjhb } 77165557Sjasone} 77265557Sjasone 77374912Sjhb/* 77474912Sjhb * Warn if any held locks are not sleepable. Note that Giant and the lock 77574912Sjhb * passed in are both special cases since they are both released during the 77674912Sjhb * sleep process and aren't actually held while the process is asleep. 77774912Sjhb */ 77865557Sjasoneint 77974912Sjhbwitness_sleep(int check_only, struct lock_object *lock, const char *file, 78074912Sjhb int line) 78165557Sjasone{ 78274912Sjhb struct lock_list_entry **lock_list, *lle; 78376272Sjhb struct lock_instance *lock1; 78465557Sjasone struct proc *p; 78574912Sjhb critical_t savecrit; 78674912Sjhb int i, n; 78765557Sjasone 78874912Sjhb if (witness_dead || panicstr) 78974912Sjhb return (0); 79074912Sjhb KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 79174912Sjhb n = 0; 79274912Sjhb /* 79374912Sjhb * Preemption bad because we need PCPU_PTR(spinlocks) to not change. 79474912Sjhb */ 79574912Sjhb savecrit = critical_enter(); 79672393Sbmilekic p = curproc; 79774912Sjhb lock_list = &p->p_sleeplocks; 79874912Sjhbagain: 79974912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 80074912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 80176272Sjhb lock1 = &lle->ll_children[i]; 80276272Sjhb if (lock1->li_lock == lock || 80376272Sjhb lock1->li_lock == &Giant.mtx_object) 80474912Sjhb continue; 80576272Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) { 80676272Sjhb if (check_only == 0) 80776272Sjhb lock1->li_flags |= LI_SLEPT; 80876272Sjhb continue; 80976272Sjhb } 81074912Sjhb n++; 81174912Sjhb printf("%s:%d: %s with \"%s\" locked from %s:%d\n", 81274912Sjhb file, line, check_only ? "could sleep" : "sleeping", 81376272Sjhb lock1->li_lock->lo_name, lock1->li_file, 81476272Sjhb lock1->li_line); 81574912Sjhb } 81674912Sjhb if (lock_list == &p->p_sleeplocks) { 81774912Sjhb lock_list = PCPU_PTR(spinlocks); 81874912Sjhb goto again; 81965557Sjasone } 82067676Sjhb#ifdef DDB 82167676Sjhb if (witness_ddb && n) 82275711Sjhb Debugger(__func__); 82367676Sjhb#endif /* DDB */ 82474912Sjhb critical_exit(savecrit); 82565557Sjasone return (n); 82665557Sjasone} 82765557Sjasone 82865856Sjhbstatic struct witness * 82974912Sjhbenroll(const char *description, struct lock_class *lock_class) 83065557Sjasone{ 83174912Sjhb struct witness *w; 83265557Sjasone 83376481Sjhb if (!witness_watch || witness_dead) 83465557Sjasone return (NULL); 83565557Sjasone 83674912Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 83765557Sjasone return (NULL); 83874912Sjhb mtx_lock_spin(&w_mtx); 83974912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 84074912Sjhb if (strcmp(description, w->w_name) == 0) { 84175362Sjhb w->w_refcount++; 84274912Sjhb mtx_unlock_spin(&w_mtx); 84374912Sjhb if (lock_class != w->w_class) 84474912Sjhb panic( 84574912Sjhb "lock (%s) %s does not match earlier (%s) lock", 84674912Sjhb description, lock_class->lc_name, 84774912Sjhb w->w_class->lc_name); 84865557Sjasone return (w); 84965557Sjasone } 85065557Sjasone } 85174912Sjhb /* 85274912Sjhb * This isn't quite right, as witness_cold is still 0 while we 85374912Sjhb * enroll all the locks initialized before witness_initialize(). 85474912Sjhb */ 85575364Sbp if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 85675364Sbp mtx_unlock_spin(&w_mtx); 85774912Sjhb panic("spin lock %s not in order list", description); 85875364Sbp } 85965557Sjasone if ((w = witness_get()) == NULL) 86065557Sjasone return (NULL); 86174912Sjhb w->w_name = description; 86274912Sjhb w->w_class = lock_class; 86375362Sjhb w->w_refcount = 1; 86474912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 86574912Sjhb if (lock_class->lc_flags & LC_SPINLOCK) 86674912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 86774912Sjhb else if (lock_class->lc_flags & LC_SLEEPLOCK) 86874912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 86975364Sbp else { 87075364Sbp mtx_unlock_spin(&w_mtx); 87174912Sjhb panic("lock class %s is not sleep or spin", 87274912Sjhb lock_class->lc_name); 87375364Sbp } 87474912Sjhb mtx_unlock_spin(&w_mtx); 87571228Sbmilekic 87665557Sjasone return (w); 87765557Sjasone} 87865557Sjasone 87965557Sjasonestatic int 88065856Sjhbitismychild(struct witness *parent, struct witness *child) 88165557Sjasone{ 88265557Sjasone static int recursed; 88374912Sjhb struct witness_child_list_entry **wcl; 88474912Sjhb struct witness_list *list; 88565557Sjasone 88674912Sjhb MPASS(child != NULL && parent != NULL); 88774912Sjhb if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 88874912Sjhb (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 88974912Sjhb panic( 89074912Sjhb "%s: parent (%s) and child (%s) are not the same lock type", 89174912Sjhb __func__, parent->w_class->lc_name, 89274912Sjhb child->w_class->lc_name); 89374912Sjhb 89465557Sjasone /* 89565557Sjasone * Insert "child" after "parent" 89665557Sjasone */ 89774912Sjhb wcl = &parent->w_children; 89874912Sjhb while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 89974912Sjhb wcl = &(*wcl)->wcl_next; 90065557Sjasone 90174912Sjhb if (*wcl == NULL) { 90274912Sjhb *wcl = witness_child_get(); 90374912Sjhb if (*wcl == NULL) 90465557Sjasone return (1); 90565557Sjasone } 90674912Sjhb 90774912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 90874912Sjhb 90965557Sjasone /* 91074912Sjhb * Now prune whole tree. We look for cases where a lock is now 91174912Sjhb * both a descendant and a direct child of a given lock. In that 91274912Sjhb * case, we want to remove the direct child link from the tree. 91365557Sjasone */ 91465557Sjasone if (recursed) 91565557Sjasone return (0); 91665557Sjasone recursed = 1; 91774912Sjhb if (parent->w_class->lc_flags & LC_SLEEPLOCK) 91874912Sjhb list = &w_sleep; 91974912Sjhb else 92074912Sjhb list = &w_spin; 92174912Sjhb STAILQ_FOREACH(child, list, w_typelist) { 92274912Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 92365557Sjasone if (!isitmychild(parent, child)) 92465557Sjasone continue; 92565557Sjasone removechild(parent, child); 92665557Sjasone if (isitmydescendant(parent, child)) 92765557Sjasone continue; 92865557Sjasone itismychild(parent, child); 92965557Sjasone } 93065557Sjasone } 93165557Sjasone recursed = 0; 93265557Sjasone witness_levelall(); 93365557Sjasone return (0); 93465557Sjasone} 93565557Sjasone 93665557Sjasonestatic void 93765856Sjhbremovechild(struct witness *parent, struct witness *child) 93865557Sjasone{ 93974912Sjhb struct witness_child_list_entry **wcl, *wcl1; 94065557Sjasone int i; 94165557Sjasone 94274912Sjhb for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 94374912Sjhb for (i = 0; i < (*wcl)->wcl_count; i++) 94474912Sjhb if ((*wcl)->wcl_children[i] == child) 94565557Sjasone goto found; 94665557Sjasone return; 94765557Sjasonefound: 94874912Sjhb (*wcl)->wcl_count--; 94974912Sjhb if ((*wcl)->wcl_count > i) 95074912Sjhb (*wcl)->wcl_children[i] = 95174912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count]; 95274912Sjhb MPASS((*wcl)->wcl_children[i] != NULL); 95365557Sjasone 95474912Sjhb if ((*wcl)->wcl_count != 0) 95565557Sjasone return; 95665557Sjasone 95774912Sjhb wcl1 = *wcl; 95874912Sjhb *wcl = wcl1->wcl_next; 95974912Sjhb witness_child_free(wcl1); 96065557Sjasone} 96165557Sjasone 96265557Sjasonestatic int 96365856Sjhbisitmychild(struct witness *parent, struct witness *child) 96465557Sjasone{ 96574912Sjhb struct witness_child_list_entry *wcl; 96665557Sjasone int i; 96765557Sjasone 96874912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 96974912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 97074912Sjhb if (wcl->wcl_children[i] == child) 97165557Sjasone return (1); 97265557Sjasone } 97365557Sjasone } 97465557Sjasone return (0); 97565557Sjasone} 97665557Sjasone 97765557Sjasonestatic int 97865856Sjhbisitmydescendant(struct witness *parent, struct witness *child) 97965557Sjasone{ 98074912Sjhb struct witness_child_list_entry *wcl; 98174912Sjhb int i, j; 98265557Sjasone 98374912Sjhb if (isitmychild(parent, child)) 98474912Sjhb return (1); 98574912Sjhb j = 0; 98674912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 98767352Sjhb MPASS(j < 1000); 98874912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 98974912Sjhb if (isitmydescendant(wcl->wcl_children[i], child)) 99065557Sjasone return (1); 99165557Sjasone } 99274912Sjhb j++; 99365557Sjasone } 99465557Sjasone return (0); 99565557Sjasone} 99665557Sjasone 99765557Sjasonevoid 99865557Sjasonewitness_levelall (void) 99965557Sjasone{ 100074912Sjhb struct witness_list *list; 100165856Sjhb struct witness *w, *w1; 100265557Sjasone 100374912Sjhb /* 100474912Sjhb * First clear all levels. 100574912Sjhb */ 100674912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 100774912Sjhb w->w_level = 0; 100874912Sjhb } 100974912Sjhb 101074912Sjhb /* 101174912Sjhb * Look for locks with no parent and level all their descendants. 101274912Sjhb */ 101374912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 101474912Sjhb /* 101574912Sjhb * This is just an optimization, technically we could get 101674912Sjhb * away just walking the all list each time. 101774912Sjhb */ 101874912Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 101974912Sjhb list = &w_sleep; 102074912Sjhb else 102174912Sjhb list = &w_spin; 102274912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 102365557Sjasone if (isitmychild(w1, w)) 102474912Sjhb goto skip; 102565557Sjasone } 102665557Sjasone witness_leveldescendents(w, 0); 102774912Sjhb skip: 102865557Sjasone } 102965557Sjasone} 103065557Sjasone 103165557Sjasonestatic void 103265856Sjhbwitness_leveldescendents(struct witness *parent, int level) 103365557Sjasone{ 103474912Sjhb struct witness_child_list_entry *wcl; 103565557Sjasone int i; 103665557Sjasone 103765557Sjasone if (parent->w_level < level) 103865557Sjasone parent->w_level = level; 103965557Sjasone level++; 104074912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 104174912Sjhb for (i = 0; i < wcl->wcl_count; i++) 104274912Sjhb witness_leveldescendents(wcl->wcl_children[i], level); 104365557Sjasone} 104465557Sjasone 104565557Sjasonestatic void 104665856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...), 104765856Sjhb struct witness *parent) 104865557Sjasone{ 104974912Sjhb struct witness_child_list_entry *wcl; 105074912Sjhb int i, level; 105165557Sjasone 105274912Sjhb level = parent->w_level; 105372224Sjhb 105474912Sjhb prnt("%-2d", level); 105565557Sjasone for (i = 0; i < level; i++) 105665557Sjasone prnt(" "); 105774912Sjhb prnt("%s", parent->w_name); 105872224Sjhb if (parent->w_file != NULL) 105972224Sjhb prnt(" -- last acquired @ %s:%d\n", parent->w_file, 106072224Sjhb parent->w_line); 106165557Sjasone 106274912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 106374912Sjhb for (i = 0; i < wcl->wcl_count; i++) 106474912Sjhb witness_displaydescendants(prnt, 106574912Sjhb wcl->wcl_children[i]); 106674912Sjhb} 106765557Sjasone 106865557Sjasonestatic int 106965856Sjhbdup_ok(struct witness *w) 107065557Sjasone{ 107174912Sjhb const char **dup; 107265557Sjasone 107374912Sjhb for (dup = dup_list; *dup != NULL; dup++) 107474912Sjhb if (strcmp(w->w_name, *dup) == 0) 107565557Sjasone return (1); 107665557Sjasone return (0); 107765557Sjasone} 107865557Sjasone 107965557Sjasonestatic int 108065856Sjhbblessed(struct witness *w1, struct witness *w2) 108165557Sjasone{ 108265557Sjasone int i; 108365856Sjhb struct witness_blessed *b; 108465557Sjasone 108565557Sjasone for (i = 0; i < blessed_count; i++) { 108665557Sjasone b = &blessed_list[i]; 108774912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 108874912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 108965557Sjasone return (1); 109065557Sjasone continue; 109165557Sjasone } 109274912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 109374912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 109465557Sjasone return (1); 109565557Sjasone } 109665557Sjasone return (0); 109765557Sjasone} 109865557Sjasone 109965856Sjhbstatic struct witness * 110074912Sjhbwitness_get(void) 110165557Sjasone{ 110265856Sjhb struct witness *w; 110365557Sjasone 110476481Sjhb if (witness_dead) { 110576481Sjhb mtx_unlock_spin(&w_mtx); 110676481Sjhb return (NULL); 110776481Sjhb } 110874912Sjhb if (STAILQ_EMPTY(&w_free)) { 110965557Sjasone witness_dead = 1; 111074912Sjhb mtx_unlock_spin(&w_mtx); 111174912Sjhb printf("%s: witness exhausted\n", __func__); 111265557Sjasone return (NULL); 111365557Sjasone } 111474912Sjhb w = STAILQ_FIRST(&w_free); 111574912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 111665856Sjhb bzero(w, sizeof(*w)); 111765557Sjasone return (w); 111865557Sjasone} 111965557Sjasone 112065557Sjasonestatic void 112165856Sjhbwitness_free(struct witness *w) 112265557Sjasone{ 112374912Sjhb 112474912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 112565557Sjasone} 112665557Sjasone 112774912Sjhbstatic struct witness_child_list_entry * 112874912Sjhbwitness_child_get(void) 112965557Sjasone{ 113074912Sjhb struct witness_child_list_entry *wcl; 113165557Sjasone 113276481Sjhb if (witness_dead) { 113376481Sjhb mtx_unlock_spin(&w_mtx); 113476481Sjhb return (NULL); 113576481Sjhb } 113674912Sjhb wcl = w_child_free; 113774912Sjhb if (wcl == NULL) { 113874912Sjhb witness_dead = 1; 113974912Sjhb mtx_unlock_spin(&w_mtx); 114074912Sjhb printf("%s: witness exhausted\n", __func__); 114174912Sjhb return (NULL); 114265557Sjasone } 114374912Sjhb w_child_free = wcl->wcl_next; 114474912Sjhb bzero(wcl, sizeof(*wcl)); 114574912Sjhb return (wcl); 114674912Sjhb} 114769881Sjake 114874912Sjhbstatic void 114974912Sjhbwitness_child_free(struct witness_child_list_entry *wcl) 115074912Sjhb{ 115174912Sjhb 115274912Sjhb wcl->wcl_next = w_child_free; 115374912Sjhb w_child_free = wcl; 115465557Sjasone} 115565557Sjasone 115674912Sjhbstatic struct lock_list_entry * 115774912Sjhbwitness_lock_list_get(void) 115874912Sjhb{ 115974912Sjhb struct lock_list_entry *lle; 116071709Sjhb 116176481Sjhb if (witness_dead) 116276481Sjhb return (NULL); 116374912Sjhb mtx_lock_spin(&w_mtx); 116474912Sjhb lle = w_lock_list_free; 116574912Sjhb if (lle == NULL) { 116674912Sjhb witness_dead = 1; 116774912Sjhb mtx_unlock_spin(&w_mtx); 116874912Sjhb printf("%s: witness exhausted\n", __func__); 116974912Sjhb return (NULL); 117074912Sjhb } 117174912Sjhb w_lock_list_free = lle->ll_next; 117274912Sjhb mtx_unlock_spin(&w_mtx); 117374912Sjhb bzero(lle, sizeof(*lle)); 117474912Sjhb return (lle); 117574912Sjhb} 117674912Sjhb 117774912Sjhbstatic void 117874912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 117971709Sjhb{ 118071709Sjhb 118174912Sjhb mtx_lock_spin(&w_mtx); 118274912Sjhb lle->ll_next = w_lock_list_free; 118374912Sjhb w_lock_list_free = lle; 118474912Sjhb mtx_unlock_spin(&w_mtx); 118571709Sjhb} 118671709Sjhb 118776272Sjhbstatic struct lock_instance * 118876272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 118976272Sjhb{ 119076272Sjhb struct lock_list_entry *lle; 119176272Sjhb struct lock_instance *instance; 119276272Sjhb int i; 119376272Sjhb 119476272Sjhb for (lle = lock_list; lle != NULL; lle = lle->ll_next) 119576272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 119676272Sjhb instance = &lle->ll_children[i]; 119776272Sjhb if (instance->li_lock == lock) 119876272Sjhb return (instance); 119976272Sjhb } 120076272Sjhb return (NULL); 120176272Sjhb} 120276272Sjhb 120374912Sjhbint 120475273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 120572224Sjhb{ 120675273Sjhb struct lock_list_entry *lle; 120776272Sjhb struct lock_instance *instance; 120874912Sjhb struct lock_object *lock; 120974912Sjhb int i, nheld; 121072224Sjhb 121174912Sjhb nheld = 0; 121274912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 121374912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 121476272Sjhb instance = &lle->ll_children[i]; 121576272Sjhb lock = instance->li_lock; 121676272Sjhb printf("%s (%s) %s (%p) locked @ %s:%d\n", 121776272Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0 ? 121876272Sjhb "exclusive" : "shared", 121974912Sjhb lock->lo_class->lc_name, lock->lo_name, lock, 122076272Sjhb instance->li_file, instance->li_line); 122174912Sjhb nheld++; 122274912Sjhb } 122375273Sjhb return (nheld); 122475273Sjhb} 122575273Sjhb 122675273Sjhb/* 122775273Sjhb * Calling this on p != curproc is bad unless we are in ddb. 122875273Sjhb */ 122975273Sjhbint 123075273Sjhbwitness_list(struct proc *p) 123175273Sjhb{ 123275273Sjhb critical_t savecrit; 123375273Sjhb int nheld; 123475273Sjhb 123575273Sjhb KASSERT(p == curproc || db_active, 123675273Sjhb ("%s: p != curproc and we aren't in the debugger", __func__)); 123775273Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 123875273Sjhb 123976481Sjhb if (!db_active && witness_dead) 124076481Sjhb return (0); 124176481Sjhb 124275273Sjhb nheld = witness_list_locks(&p->p_sleeplocks); 124375273Sjhb 124474912Sjhb /* 124574912Sjhb * We only handle spinlocks if p == curproc. This is somewhat broken 124674912Sjhb * if p is currently executing on some other CPU and holds spin locks 124775273Sjhb * as we won't display those locks. If we had a MI way of getting 124875273Sjhb * the per-cpu data for a given cpu then we could use p->p_oncpu to 124975273Sjhb * get the list of spinlocks for this process and "fix" this. 125074912Sjhb */ 125175273Sjhb if (p == curproc) { 125275273Sjhb /* 125375273Sjhb * Preemption bad because we need PCPU_PTR(spinlocks) to not 125475273Sjhb * change. 125575273Sjhb */ 125675273Sjhb savecrit = critical_enter(); 125775273Sjhb nheld += witness_list_locks(PCPU_PTR(spinlocks)); 125875273Sjhb critical_exit(savecrit); 125974912Sjhb } 126074912Sjhb 126174912Sjhb return (nheld); 126272224Sjhb} 126371709Sjhb 126465557Sjasonevoid 126574912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 126665557Sjasone{ 126776272Sjhb struct lock_instance *instance; 126871320Sjasone 126974912Sjhb KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 127076481Sjhb if (lock->lo_witness == NULL || witness_dead) 127171352Sjasone return; 127271352Sjasone 127376272Sjhb KASSERT(lock->lo_class->lc_flags & LC_SLEEPLOCK, 127476272Sjhb ("%s: lock (%s) %s is not a sleep lock", __func__, 127576272Sjhb lock->lo_class->lc_name, lock->lo_name)); 127676272Sjhb instance = find_instance(curproc->p_sleeplocks, lock); 127776272Sjhb KASSERT(instance != NULL, ("%s: lock (%s) %s not locked", __func__, 127876272Sjhb lock->lo_class->lc_name, lock->lo_name)); 127976272Sjhb 128076272Sjhb *filep = instance->li_file; 128176272Sjhb *linep = instance->li_line; 128265557Sjasone} 128365557Sjasone 128465557Sjasonevoid 128574912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 128665557Sjasone{ 128776272Sjhb struct lock_instance *instance; 128871320Sjasone 128974912Sjhb KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 129076481Sjhb if (lock->lo_witness == NULL || witness_dead) 129171352Sjasone return; 129271352Sjasone 129376272Sjhb KASSERT(lock->lo_class->lc_flags & LC_SLEEPLOCK, 129476272Sjhb ("%s: lock (%s) %s is not a sleep lock", __func__, 129576272Sjhb lock->lo_class->lc_name, lock->lo_name)); 129676272Sjhb instance = find_instance(curproc->p_sleeplocks, lock); 129776272Sjhb KASSERT(instance != NULL, ("%s: lock (%s) %s not locked", __func__, 129876272Sjhb lock->lo_class->lc_name, lock->lo_name)); 129976272Sjhb 130074912Sjhb lock->lo_witness->w_file = file; 130174912Sjhb lock->lo_witness->w_line = line; 130276272Sjhb instance->li_file = file; 130376272Sjhb instance->li_line = line; 130465557Sjasone} 130565557Sjasone 130674912Sjhb#ifdef DDB 130774912Sjhb 130874930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 130974912Sjhb{ 131075273Sjhb struct proc *p; 131175273Sjhb pid_t pid; 131274912Sjhb 131375273Sjhb if (have_addr) { 131475273Sjhb pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 131575273Sjhb ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 131675273Sjhb ((addr >> 16) % 16) * 10000; 131775273Sjhb 131875273Sjhb /* sx_slock(&allproc_lock); */ 131975273Sjhb LIST_FOREACH(p, &allproc, p_list) { 132075273Sjhb if (p->p_pid == pid) 132175273Sjhb break; 132275273Sjhb } 132375273Sjhb /* sx_sunlock(&allproc_lock); */ 132475273Sjhb if (p == NULL) { 132575273Sjhb db_printf("pid %d not found\n", pid); 132675273Sjhb return; 132775273Sjhb } 132875273Sjhb } else 132975273Sjhb p = curproc; 133075273Sjhb 133175273Sjhb witness_list(p); 133274912Sjhb} 133374912Sjhb 133474912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 133574912Sjhb{ 133674912Sjhb 133774912Sjhb witness_display(db_printf); 133874912Sjhb} 133974912Sjhb#endif 1340