subr_witness.c revision 75711
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 75711 2001-04-19 15:49:54Z 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 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, ...)); 14272200Sbmilekic 14374912SjhbMALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 14472200Sbmilekic 14574912Sjhbstatic int witness_watch; 14674912SjhbTUNABLE_INT_DECL("debug.witness_watch", 1, witness_watch); 14774912SjhbSYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 14871352Sjasone 14967352Sjhb#ifdef DDB 15072200Sbmilekic/* 15167676Sjhb * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 15265557Sjasone * drop into kdebug() when: 15365557Sjasone * - a lock heirarchy violation occurs 15465557Sjasone * - locks are held when going to sleep. 15565557Sjasone */ 15671560Sjhbint witness_ddb; 15767676Sjhb#ifdef WITNESS_DDB 15871560SjhbTUNABLE_INT_DECL("debug.witness_ddb", 1, witness_ddb); 15967676Sjhb#else 16071560SjhbTUNABLE_INT_DECL("debug.witness_ddb", 0, witness_ddb); 16165557Sjasone#endif 16267676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 16367676Sjhb#endif /* DDB */ 16465557Sjasone 16571560Sjhbint witness_skipspin; 16667676Sjhb#ifdef WITNESS_SKIPSPIN 16771560SjhbTUNABLE_INT_DECL("debug.witness_skipspin", 1, witness_skipspin); 16867676Sjhb#else 16971560SjhbTUNABLE_INT_DECL("debug.witness_skipspin", 0, witness_skipspin); 17065557Sjasone#endif 17167676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 17267676Sjhb ""); 17365557Sjasone 17474912Sjhbstatic struct mtx w_mtx; 17574912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 17674912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 17774912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 17874912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 17974912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL; 18074912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 18174912Sjhbstatic int witness_dead; /* fatal error, probably no memory */ 18265557Sjasone 18374912Sjhbstatic struct witness w_data[WITNESS_COUNT]; 18474912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 18574912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 18665557Sjasone 18774912Sjhbstatic struct witness_order_list_entry order_lists[] = { 18874912Sjhb { "Giant", &lock_class_mtx_sleep }, 18974912Sjhb { "proctree", &lock_class_sx }, 19074912Sjhb { "allproc", &lock_class_sx }, 19174912Sjhb { "process lock", &lock_class_mtx_sleep }, 19274912Sjhb { "uidinfo hash", &lock_class_mtx_sleep }, 19374912Sjhb { "uidinfo struct", &lock_class_mtx_sleep }, 19474912Sjhb { NULL, NULL }, 19575464Sjhb /* 19675464Sjhb * spin locks 19775464Sjhb */ 19872224Sjhb#if defined(__i386__) && defined (SMP) 19974912Sjhb { "com", &lock_class_mtx_spin }, 20072224Sjhb#endif 20174912Sjhb { "sio", &lock_class_mtx_spin }, 20272224Sjhb#ifdef __i386__ 20374912Sjhb { "cy", &lock_class_mtx_spin }, 20472224Sjhb#endif 20574912Sjhb { "ng_node", &lock_class_mtx_spin }, 20674912Sjhb { "ng_worklist", &lock_class_mtx_spin }, 20774912Sjhb { "ithread table lock", &lock_class_mtx_spin }, 20874912Sjhb { "ithread list lock", &lock_class_mtx_spin }, 20974912Sjhb { "sched lock", &lock_class_mtx_spin }, 21074912Sjhb { "clk", &lock_class_mtx_spin }, 21174912Sjhb { "callout", &lock_class_mtx_spin }, 21265557Sjasone /* 21365557Sjasone * leaf locks 21465557Sjasone */ 21572224Sjhb#ifdef SMP 21675464Sjhb { "ap boot", &lock_class_mtx_spin }, 21771576Sjasone#ifdef __i386__ 21874912Sjhb { "imen", &lock_class_mtx_spin }, 21971576Sjasone#endif 22074912Sjhb { "smp rendezvous", &lock_class_mtx_spin }, 22172224Sjhb#endif 22274912Sjhb { NULL, NULL }, 22374912Sjhb { NULL, NULL } 22465557Sjasone}; 22565557Sjasone 22674912Sjhbstatic const char *dup_list[] = { 22773912Sjhb "process lock", 22865557Sjasone NULL 22965557Sjasone}; 23065557Sjasone 23165557Sjasone/* 23265557Sjasone * Pairs of locks which have been blessed 23365557Sjasone * Don't complain about order problems with blessed locks 23465557Sjasone */ 23565856Sjhbstatic struct witness_blessed blessed_list[] = { 23665557Sjasone}; 23772200Sbmilekicstatic int blessed_count = 23872200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 23965557Sjasone 24074912Sjhb/* 24174912Sjhb * List of all locks in the system. 24274912Sjhb */ 24374912SjhbSTAILQ_HEAD(, lock_object) all_locks = STAILQ_HEAD_INITIALIZER(all_locks); 24474912Sjhb 24574912Sjhbstatic struct mtx all_mtx = { 24674912Sjhb { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 24774912Sjhb "All locks list", /* mtx_object.lo_name */ 24874912Sjhb NULL, /* mtx_object.lo_file */ 24974912Sjhb 0, /* mtx_object.lo_line */ 25074912Sjhb LO_INITIALIZED, /* mtx_object.lo_flags */ 25174912Sjhb { NULL }, /* mtx_object.lo_list */ 25274912Sjhb NULL }, /* mtx_object.lo_witness */ 25374912Sjhb MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 25474912Sjhb 0, /* mtx_savecrit */ 25574912Sjhb TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 25674912Sjhb { NULL, NULL } /* mtx_contested */ 25774912Sjhb}; 25874912Sjhb 25974912Sjhb/* 26074912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 26174912Sjhb */ 26274912Sjhbstatic int witness_cold = 1; 26374912Sjhb 26474912Sjhb/* 26574912Sjhb * Global variables for book keeping. 26674912Sjhb */ 26774912Sjhbstatic int lock_cur_cnt; 26874912Sjhbstatic int lock_max_cnt; 26974912Sjhb 27074912Sjhb/* 27174912Sjhb * The WITNESS-enabled diagnostic code. 27274912Sjhb */ 27371352Sjasonestatic void 27474912Sjhbwitness_initialize(void *dummy __unused) 27565557Sjasone{ 27674912Sjhb struct lock_object *lock; 27774912Sjhb struct witness_order_list_entry *order; 27874912Sjhb struct witness *w, *w1; 27974912Sjhb int i; 28065557Sjasone 28174912Sjhb /* 28274912Sjhb * We have to release Giant before initializing its witness 28374912Sjhb * structure so that WITNESS doesn't get confused. 28474912Sjhb */ 28574912Sjhb mtx_unlock(&Giant); 28674912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 28774912Sjhb 28874912Sjhb STAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 28974912Sjhb mtx_init(&w_mtx, "witness lock", MTX_SPIN | MTX_QUIET | MTX_NOWITNESS); 29074912Sjhb for (i = 0; i < WITNESS_COUNT; i++) 29174912Sjhb witness_free(&w_data[i]); 29274912Sjhb for (i = 0; i < WITNESS_CHILDCOUNT; i++) 29374912Sjhb witness_child_free(&w_childdata[i]); 29474912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 29574912Sjhb witness_lock_list_free(&w_locklistdata[i]); 29674912Sjhb 29774912Sjhb /* First add in all the specified order lists. */ 29874912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 29974912Sjhb w = enroll(order->w_name, order->w_class); 30075569Sjhb if (w == NULL) 30175569Sjhb continue; 30274912Sjhb w->w_file = "order list"; 30374912Sjhb for (order++; order->w_name != NULL; order++) { 30474912Sjhb w1 = enroll(order->w_name, order->w_class); 30575569Sjhb if (w1 == NULL) 30675569Sjhb continue; 30774912Sjhb w1->w_file = "order list"; 30874912Sjhb itismychild(w, w1); 30974912Sjhb w = w1; 31065557Sjasone } 31165557Sjasone } 31265557Sjasone 31374912Sjhb /* Iterate through all locks and add them to witness. */ 31474912Sjhb mtx_lock(&all_mtx); 31574912Sjhb STAILQ_FOREACH(lock, &all_locks, lo_list) { 31674912Sjhb if (lock->lo_flags & LO_WITNESS) 31774912Sjhb lock->lo_witness = enroll(lock->lo_name, 31874912Sjhb lock->lo_class); 31974912Sjhb else 32074912Sjhb lock->lo_witness = NULL; 32174912Sjhb } 32274912Sjhb mtx_unlock(&all_mtx); 32374912Sjhb 32474912Sjhb /* Mark the witness code as being ready for use. */ 32574912Sjhb atomic_store_rel_int(&witness_cold, 0); 32674912Sjhb 32774912Sjhb mtx_lock(&Giant); 32865557Sjasone} 32974912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 33065557Sjasone 33174912Sjhbvoid 33274912Sjhbwitness_init(struct lock_object *lock) 33374912Sjhb{ 33474912Sjhb struct lock_class *class; 33574912Sjhb 33674912Sjhb class = lock->lo_class; 33774912Sjhb if (lock->lo_flags & LO_INITIALIZED) 33874912Sjhb panic("%s: lock (%s) %s is already initialized!\n", __func__, 33974912Sjhb class->lc_name, lock->lo_name); 34074912Sjhb 34174912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 34274912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 34374912Sjhb panic("%s: lock (%s) %s can not be recursable!\n", __func__, 34474912Sjhb class->lc_name, lock->lo_name); 34574912Sjhb 34674912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 34774912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 34874912Sjhb panic("%s: lock (%s) %s can not be sleepable!\n", __func__, 34974912Sjhb class->lc_name, lock->lo_name); 35074912Sjhb 35174912Sjhb mtx_lock(&all_mtx); 35274912Sjhb STAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 35374912Sjhb lock->lo_flags |= LO_INITIALIZED; 35474912Sjhb lock_cur_cnt++; 35574912Sjhb if (lock_cur_cnt > lock_max_cnt) 35674912Sjhb lock_max_cnt = lock_cur_cnt; 35774912Sjhb mtx_unlock(&all_mtx); 35874912Sjhb if (!witness_cold && !witness_dead && 35974912Sjhb (lock->lo_flags & LO_WITNESS) != 0) 36074912Sjhb lock->lo_witness = enroll(lock->lo_name, class); 36174912Sjhb else 36274912Sjhb lock->lo_witness = NULL; 36374912Sjhb} 36474912Sjhb 36574912Sjhbvoid 36674912Sjhbwitness_destroy(struct lock_object *lock) 36774912Sjhb{ 36875362Sjhb struct witness *w; 36974912Sjhb 37074912Sjhb if (witness_cold) 37174912Sjhb panic("lock (%s) %s destroyed while witness_cold", 37274912Sjhb lock->lo_class->lc_name, lock->lo_name); 37374912Sjhb 37474912Sjhb if ((lock->lo_flags & LO_INITIALIZED) == 0) 37574912Sjhb panic("%s: lock (%s) %s is not initialized!\n", __func__, 37674912Sjhb lock->lo_class->lc_name, lock->lo_name); 37774912Sjhb 37874912Sjhb if (lock->lo_flags & LO_LOCKED) 37974912Sjhb panic("lock (%s) %s destroyed while held", 38074912Sjhb lock->lo_class->lc_name, lock->lo_name); 38174912Sjhb 38275362Sjhb w = lock->lo_witness; 38375362Sjhb if (w != NULL) { 38475362Sjhb mtx_lock_spin(&w_mtx); 38575362Sjhb w->w_refcount--; 38675362Sjhb if (w->w_refcount == 0) { 38775362Sjhb w->w_name = "(dead)"; 38875362Sjhb w->w_file = "(dead)"; 38975362Sjhb w->w_line = 0; 39075362Sjhb } 39175362Sjhb mtx_unlock_spin(&w_mtx); 39275362Sjhb } 39375362Sjhb 39474912Sjhb mtx_lock(&all_mtx); 39574912Sjhb lock_cur_cnt--; 39674912Sjhb STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list); 39774912Sjhb lock->lo_flags &= LO_INITIALIZED; 39874912Sjhb mtx_unlock(&all_mtx); 39974912Sjhb} 40074912Sjhb 40171352Sjasonestatic void 40274912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...), 40374912Sjhb struct witness_list *list) 40471352Sjasone{ 40571352Sjasone struct witness *w, *w1; 40674912Sjhb int found; 40771352Sjasone 40874912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 40974912Sjhb if (w->w_file == NULL) 41071352Sjasone continue; 41174912Sjhb found = 0; 41274912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 41374912Sjhb if (isitmychild(w1, w)) { 41474912Sjhb found++; 41571352Sjasone break; 41674912Sjhb } 41771352Sjasone } 41874912Sjhb if (found) 41971352Sjasone continue; 42071352Sjasone /* 42171352Sjasone * This lock has no anscestors, display its descendants. 42271352Sjasone */ 42371352Sjasone witness_displaydescendants(prnt, w); 42471352Sjasone } 42574912Sjhb} 42672224Sjhb 42774912Sjhbstatic void 42874912Sjhbwitness_display(void(*prnt)(const char *fmt, ...)) 42974912Sjhb{ 43074912Sjhb struct witness *w; 43174912Sjhb 43274912Sjhb KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 43374912Sjhb witness_levelall(); 43474912Sjhb 43572224Sjhb /* 43674930Sjhb * First, handle sleep locks which have been acquired at least 43774912Sjhb * once. 43874912Sjhb */ 43974912Sjhb prnt("Sleep locks:\n"); 44074912Sjhb witness_display_list(prnt, &w_sleep); 44174912Sjhb 44274912Sjhb /* 44374930Sjhb * Now do spin locks which have been acquired at least once. 44472224Sjhb */ 44574912Sjhb prnt("\nSpin locks:\n"); 44674912Sjhb witness_display_list(prnt, &w_spin); 44772224Sjhb 44872224Sjhb /* 44974930Sjhb * Finally, any locks which have not been acquired yet. 45072224Sjhb */ 45174912Sjhb prnt("\nLocks which were never acquired:\n"); 45274912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 45371352Sjasone if (w->w_file != NULL) 45471352Sjasone continue; 45574912Sjhb prnt("%s\n", w->w_name); 45671352Sjasone } 45771352Sjasone} 45871352Sjasone 45965557Sjasonevoid 46074912Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 46165557Sjasone{ 46274912Sjhb struct lock_list_entry **lock_list, *lle; 46374912Sjhb struct lock_object *lock1, *lock2; 46474912Sjhb struct lock_class *class; 46565856Sjhb struct witness *w, *w1; 46665557Sjasone struct proc *p; 46774912Sjhb int i, j; 46867676Sjhb#ifdef DDB 46967676Sjhb int go_into_ddb = 0; 47067676Sjhb#endif /* DDB */ 47165557Sjasone 47274912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 47374912Sjhb panicstr) 47471320Sjasone return; 47574912Sjhb w = lock->lo_witness; 47674912Sjhb class = lock->lo_class; 47772393Sbmilekic p = curproc; 47865557Sjasone 47974912Sjhb if ((lock->lo_flags & LO_LOCKED) == 0) 48074912Sjhb panic("%s: lock (%s) %s is not locked @ %s:%d", __func__, 48174912Sjhb class->lc_name, lock->lo_name, file, line); 48274912Sjhb 48374912Sjhb if ((lock->lo_flags & LO_RECURSED) != 0) { 48474912Sjhb if ((lock->lo_flags & LO_RECURSABLE) == 0) 48574912Sjhb panic( 48674912Sjhb "%s: recursed on non-recursive lock (%s) %s @ %s:%d", 48774912Sjhb __func__, class->lc_name, lock->lo_name, file, 48874912Sjhb line); 48965557Sjasone return; 49065557Sjasone } 49174912Sjhb 49274944Sjhb /* 49374944Sjhb * We have to hold a spinlock to keep lock_list valid across the check 49474944Sjhb * in the LC_SLEEPLOCK case. In the LC_SPINLOCK case, it is already 49574944Sjhb * protected by the spinlock we are currently performing the witness 49674944Sjhb * checks on, so it is ok to release the lock after performing this 49774944Sjhb * check. All we have to protect is the LC_SLEEPLOCK case when no 49874944Sjhb * spinlocks are held as we may get preempted during this check and 49974944Sjhb * lock_list could end up pointing to some other CPU's spinlock list. 50074944Sjhb */ 50174944Sjhb mtx_lock_spin(&w_mtx); 50274912Sjhb lock_list = PCPU_PTR(spinlocks); 50374912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 50474944Sjhb if (*lock_list != NULL) { 50574944Sjhb mtx_unlock_spin(&w_mtx); 50674912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 50774912Sjhb class->lc_name, lock->lo_name, file, line); 50874944Sjhb } 50974912Sjhb lock_list = &p->p_sleeplocks; 51074912Sjhb } 51174944Sjhb mtx_unlock_spin(&w_mtx); 51265557Sjasone 51374912Sjhb if (flags & LOP_TRYLOCK) 51465557Sjasone goto out; 51565557Sjasone 51665557Sjasone /* 51774912Sjhb * Is this the first lock acquired? If so, then no order checking 51874912Sjhb * is needed. 51965557Sjasone */ 52074912Sjhb if (*lock_list == NULL) 52165557Sjasone goto out; 52265557Sjasone 52374912Sjhb /* 52474912Sjhb * Check for duplicate locks of the same type. Note that we only 52574912Sjhb * have to check for this on the last lock we just acquired. Any 52674912Sjhb * other cases will be caught as lock order violations. 52774912Sjhb */ 52874912Sjhb lock1 = (*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 52974912Sjhb w1 = lock1->lo_witness; 53074912Sjhb if (w1 == w) { 53165557Sjasone if (w->w_same_squawked || dup_ok(w)) 53265557Sjasone goto out; 53365557Sjasone w->w_same_squawked = 1; 53465557Sjasone printf("acquring duplicate lock of same type: \"%s\"\n", 53574912Sjhb lock->lo_name); 53665557Sjasone printf(" 1st @ %s:%d\n", w->w_file, w->w_line); 53765557Sjasone printf(" 2nd @ %s:%d\n", file, line); 53867676Sjhb#ifdef DDB 53967676Sjhb go_into_ddb = 1; 54067676Sjhb#endif /* DDB */ 54165557Sjasone goto out; 54265557Sjasone } 54365557Sjasone MPASS(!mtx_owned(&w_mtx)); 54474912Sjhb mtx_lock_spin(&w_mtx); 54565557Sjasone /* 54665557Sjasone * If we have a known higher number just say ok 54765557Sjasone */ 54865557Sjasone if (witness_watch > 1 && w->w_level > w1->w_level) { 54974912Sjhb mtx_unlock_spin(&w_mtx); 55065557Sjasone goto out; 55165557Sjasone } 55274912Sjhb if (isitmydescendant(w1, w)) { 55374912Sjhb mtx_unlock_spin(&w_mtx); 55465557Sjasone goto out; 55565557Sjasone } 55674912Sjhb for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 55774912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 55865557Sjasone 55974912Sjhb MPASS(j < WITNESS_COUNT); 56074912Sjhb lock1 = lle->ll_children[i]; 56174912Sjhb w1 = lock1->lo_witness; 56274912Sjhb 56374912Sjhb /* 56474912Sjhb * If this lock doesn't undergo witness checking, 56574912Sjhb * then skip it. 56674912Sjhb */ 56774912Sjhb if (w1 == NULL) { 56874912Sjhb KASSERT((lock1->lo_flags & LO_WITNESS) == 0, 56974912Sjhb ("lock missing witness structure")); 57074912Sjhb continue; 57174912Sjhb } 57274912Sjhb if (!isitmydescendant(w, w1)) 57374912Sjhb continue; 57474912Sjhb /* 57574912Sjhb * We have a lock order violation, check to see if it 57674912Sjhb * is allowed or has already been yelled about. 57774912Sjhb */ 57874912Sjhb mtx_unlock_spin(&w_mtx); 57965557Sjasone if (blessed(w, w1)) 58065557Sjasone goto out; 58174912Sjhb if (lock1 == &Giant.mtx_object) { 58265557Sjasone if (w1->w_Giant_squawked) 58365557Sjasone goto out; 58465557Sjasone else 58565557Sjasone w1->w_Giant_squawked = 1; 58665557Sjasone } else { 58765557Sjasone if (w1->w_other_squawked) 58865557Sjasone goto out; 58965557Sjasone else 59065557Sjasone w1->w_other_squawked = 1; 59165557Sjasone } 59274912Sjhb /* 59374912Sjhb * Ok, yell about it. 59474912Sjhb */ 59565557Sjasone printf("lock order reversal\n"); 59674912Sjhb /* 59774912Sjhb * Try to locate an earlier lock with 59874912Sjhb * witness w in our list. 59974912Sjhb */ 60074912Sjhb do { 60174912Sjhb lock2 = lle->ll_children[i]; 60274912Sjhb MPASS(lock2 != NULL); 60374912Sjhb if (lock2->lo_witness == w) 60474912Sjhb break; 60574912Sjhb i--; 60674912Sjhb if (i == 0 && lle->ll_next != NULL) { 60774912Sjhb lle = lle->ll_next; 60874912Sjhb i = lle->ll_count - 1; 60974912Sjhb MPASS(i != 0); 61074912Sjhb } 61174912Sjhb } while (i >= 0); 61274912Sjhb if (i < 0) 61374912Sjhb /* 61474912Sjhb * We are very likely bogus in this case. 61574912Sjhb */ 61674912Sjhb printf(" 1st %s last acquired @ %s:%d\n", 61774912Sjhb w->w_name, w->w_file, w->w_line); 61874912Sjhb else 61974912Sjhb printf(" 1st %p %s @ %s:%d\n", lock2, 62074912Sjhb lock2->lo_name, lock2->lo_file, 62174912Sjhb lock2->lo_line); 62265557Sjasone printf(" 2nd %p %s @ %s:%d\n", 62374912Sjhb lock1, lock1->lo_name, lock1->lo_file, 62474912Sjhb lock1->lo_line); 62565557Sjasone printf(" 3rd %p %s @ %s:%d\n", 62674912Sjhb lock, lock->lo_name, file, line); 62767676Sjhb#ifdef DDB 62867676Sjhb go_into_ddb = 1; 62967676Sjhb#endif /* DDB */ 63065557Sjasone goto out; 63165557Sjasone } 63265557Sjasone } 63374912Sjhb lock1 = (*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 63474912Sjhb if (!itismychild(lock1->lo_witness, w)) 63574912Sjhb mtx_unlock_spin(&w_mtx); 63665557Sjasone 63765557Sjasoneout: 63867676Sjhb#ifdef DDB 63967676Sjhb if (witness_ddb && go_into_ddb) 64075711Sjhb Debugger(__func__); 64167676Sjhb#endif /* DDB */ 64265557Sjasone w->w_file = file; 64365557Sjasone w->w_line = line; 64474912Sjhb lock->lo_line = line; 64574912Sjhb lock->lo_file = file; 64674912Sjhb 64774912Sjhb lle = *lock_list; 64874912Sjhb if (lle == NULL || lle->ll_count == LOCK_CHILDCOUNT) { 64974912Sjhb *lock_list = witness_lock_list_get(); 65074912Sjhb if (*lock_list == NULL) 65165557Sjasone return; 65274912Sjhb (*lock_list)->ll_next = lle; 65374912Sjhb lle = *lock_list; 65465557Sjasone } 65574912Sjhb lle->ll_children[lle->ll_count++] = lock; 65665557Sjasone} 65765557Sjasone 65865557Sjasonevoid 65974912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 66065557Sjasone{ 66174912Sjhb struct lock_list_entry **lock_list, *lle; 66274912Sjhb struct lock_class *class; 66374016Sjhb struct proc *p; 66474912Sjhb int i, j; 66565557Sjasone 66674912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 66774912Sjhb panicstr) 66871352Sjasone return; 66974016Sjhb p = curproc; 67074912Sjhb class = lock->lo_class; 67165557Sjasone 67274912Sjhb if (lock->lo_flags & LO_RECURSED) { 67374912Sjhb if ((lock->lo_flags & LO_LOCKED) == 0) 67474912Sjhb panic("%s: recursed lock (%s) %s is not locked @ %s:%d", 67574912Sjhb __func__, class->lc_name, lock->lo_name, file, 67674912Sjhb line); 67771352Sjasone return; 67865557Sjasone } 67971352Sjasone 68074912Sjhb /* 68174912Sjhb * We don't need to protect this PCPU_GET() here against preemption 68274912Sjhb * because if we hold any spinlocks then we are already protected, 68374912Sjhb * and if we don't we will get NULL if we hold no spinlocks even if 68474912Sjhb * we switch CPU's while reading it. 68574912Sjhb */ 68674912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 68774912Sjhb if ((flags & LOP_NOSWITCH) == 0 && PCPU_GET(spinlocks) != NULL) 68874912Sjhb panic("switchable sleep unlock (%s) %s @ %s:%d", 68974912Sjhb class->lc_name, lock->lo_name, file, line); 69074912Sjhb lock_list = &p->p_sleeplocks; 69174912Sjhb } else 69274912Sjhb lock_list = PCPU_PTR(spinlocks); 69371352Sjasone 69474912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 69574912Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) 69674912Sjhb if ((*lock_list)->ll_children[i] == lock) { 69774912Sjhb (*lock_list)->ll_count--; 69874912Sjhb for (j = i; j < (*lock_list)->ll_count; j++) 69974912Sjhb (*lock_list)->ll_children[j] = 70074912Sjhb (*lock_list)->ll_children[j + 1]; 70174912Sjhb if ((*lock_list)->ll_count == 0) { 70274912Sjhb lle = *lock_list; 70374912Sjhb *lock_list = lle->ll_next; 70474912Sjhb witness_lock_list_free(lle); 70574912Sjhb } 70674912Sjhb return; 70774912Sjhb } 70865557Sjasone} 70965557Sjasone 71074912Sjhb/* 71174912Sjhb * Warn if any held locks are not sleepable. Note that Giant and the lock 71274912Sjhb * passed in are both special cases since they are both released during the 71374912Sjhb * sleep process and aren't actually held while the process is asleep. 71474912Sjhb */ 71565557Sjasoneint 71674912Sjhbwitness_sleep(int check_only, struct lock_object *lock, const char *file, 71774912Sjhb int line) 71865557Sjasone{ 71974912Sjhb struct lock_list_entry **lock_list, *lle; 72074912Sjhb struct lock_object *lock1; 72165557Sjasone struct proc *p; 72274912Sjhb critical_t savecrit; 72374912Sjhb int i, n; 72465557Sjasone 72574912Sjhb if (witness_dead || panicstr) 72674912Sjhb return (0); 72774912Sjhb KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 72874912Sjhb n = 0; 72974912Sjhb /* 73074912Sjhb * Preemption bad because we need PCPU_PTR(spinlocks) to not change. 73174912Sjhb */ 73274912Sjhb savecrit = critical_enter(); 73372393Sbmilekic p = curproc; 73474912Sjhb lock_list = &p->p_sleeplocks; 73574912Sjhbagain: 73674912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 73774912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 73874912Sjhb lock1 = lle->ll_children[i]; 73974912Sjhb if (lock1 == lock || lock1 == &Giant.mtx_object || 74074912Sjhb (lock1->lo_flags & LO_SLEEPABLE)) 74174912Sjhb continue; 74274912Sjhb n++; 74374912Sjhb printf("%s:%d: %s with \"%s\" locked from %s:%d\n", 74474912Sjhb file, line, check_only ? "could sleep" : "sleeping", 74574912Sjhb lock1->lo_name, lock1->lo_file, lock1->lo_line); 74674912Sjhb } 74774912Sjhb if (lock_list == &p->p_sleeplocks) { 74874912Sjhb lock_list = PCPU_PTR(spinlocks); 74974912Sjhb goto again; 75065557Sjasone } 75167676Sjhb#ifdef DDB 75267676Sjhb if (witness_ddb && n) 75375711Sjhb Debugger(__func__); 75467676Sjhb#endif /* DDB */ 75574912Sjhb critical_exit(savecrit); 75665557Sjasone return (n); 75765557Sjasone} 75865557Sjasone 75965856Sjhbstatic struct witness * 76074912Sjhbenroll(const char *description, struct lock_class *lock_class) 76165557Sjasone{ 76274912Sjhb struct witness *w; 76365557Sjasone 76465557Sjasone if (!witness_watch) 76565557Sjasone return (NULL); 76665557Sjasone 76774912Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 76865557Sjasone return (NULL); 76974912Sjhb mtx_lock_spin(&w_mtx); 77074912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 77174912Sjhb if (strcmp(description, w->w_name) == 0) { 77275362Sjhb w->w_refcount++; 77374912Sjhb mtx_unlock_spin(&w_mtx); 77474912Sjhb if (lock_class != w->w_class) 77574912Sjhb panic( 77674912Sjhb "lock (%s) %s does not match earlier (%s) lock", 77774912Sjhb description, lock_class->lc_name, 77874912Sjhb w->w_class->lc_name); 77965557Sjasone return (w); 78065557Sjasone } 78165557Sjasone } 78274912Sjhb /* 78374912Sjhb * This isn't quite right, as witness_cold is still 0 while we 78474912Sjhb * enroll all the locks initialized before witness_initialize(). 78574912Sjhb */ 78675364Sbp if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 78775364Sbp mtx_unlock_spin(&w_mtx); 78874912Sjhb panic("spin lock %s not in order list", description); 78975364Sbp } 79065557Sjasone if ((w = witness_get()) == NULL) 79165557Sjasone return (NULL); 79274912Sjhb w->w_name = description; 79374912Sjhb w->w_class = lock_class; 79475362Sjhb w->w_refcount = 1; 79574912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 79674912Sjhb if (lock_class->lc_flags & LC_SPINLOCK) 79774912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 79874912Sjhb else if (lock_class->lc_flags & LC_SLEEPLOCK) 79974912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 80075364Sbp else { 80175364Sbp mtx_unlock_spin(&w_mtx); 80274912Sjhb panic("lock class %s is not sleep or spin", 80374912Sjhb lock_class->lc_name); 80475364Sbp } 80574912Sjhb mtx_unlock_spin(&w_mtx); 80671228Sbmilekic 80765557Sjasone return (w); 80865557Sjasone} 80965557Sjasone 81065557Sjasonestatic int 81165856Sjhbitismychild(struct witness *parent, struct witness *child) 81265557Sjasone{ 81365557Sjasone static int recursed; 81474912Sjhb struct witness_child_list_entry **wcl; 81574912Sjhb struct witness_list *list; 81665557Sjasone 81774912Sjhb MPASS(child != NULL && parent != NULL); 81874912Sjhb if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 81974912Sjhb (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 82074912Sjhb panic( 82174912Sjhb "%s: parent (%s) and child (%s) are not the same lock type", 82274912Sjhb __func__, parent->w_class->lc_name, 82374912Sjhb child->w_class->lc_name); 82474912Sjhb 82565557Sjasone /* 82665557Sjasone * Insert "child" after "parent" 82765557Sjasone */ 82874912Sjhb wcl = &parent->w_children; 82974912Sjhb while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 83074912Sjhb wcl = &(*wcl)->wcl_next; 83165557Sjasone 83274912Sjhb if (*wcl == NULL) { 83374912Sjhb *wcl = witness_child_get(); 83474912Sjhb if (*wcl == NULL) 83565557Sjasone return (1); 83665557Sjasone } 83774912Sjhb 83874912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 83974912Sjhb 84065557Sjasone /* 84174912Sjhb * Now prune whole tree. We look for cases where a lock is now 84274912Sjhb * both a descendant and a direct child of a given lock. In that 84374912Sjhb * case, we want to remove the direct child link from the tree. 84465557Sjasone */ 84565557Sjasone if (recursed) 84665557Sjasone return (0); 84765557Sjasone recursed = 1; 84874912Sjhb if (parent->w_class->lc_flags & LC_SLEEPLOCK) 84974912Sjhb list = &w_sleep; 85074912Sjhb else 85174912Sjhb list = &w_spin; 85274912Sjhb STAILQ_FOREACH(child, list, w_typelist) { 85374912Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 85465557Sjasone if (!isitmychild(parent, child)) 85565557Sjasone continue; 85665557Sjasone removechild(parent, child); 85765557Sjasone if (isitmydescendant(parent, child)) 85865557Sjasone continue; 85965557Sjasone itismychild(parent, child); 86065557Sjasone } 86165557Sjasone } 86265557Sjasone recursed = 0; 86365557Sjasone witness_levelall(); 86465557Sjasone return (0); 86565557Sjasone} 86665557Sjasone 86765557Sjasonestatic void 86865856Sjhbremovechild(struct witness *parent, struct witness *child) 86965557Sjasone{ 87074912Sjhb struct witness_child_list_entry **wcl, *wcl1; 87165557Sjasone int i; 87265557Sjasone 87374912Sjhb for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 87474912Sjhb for (i = 0; i < (*wcl)->wcl_count; i++) 87574912Sjhb if ((*wcl)->wcl_children[i] == child) 87665557Sjasone goto found; 87765557Sjasone return; 87865557Sjasonefound: 87974912Sjhb (*wcl)->wcl_count--; 88074912Sjhb if ((*wcl)->wcl_count > i) 88174912Sjhb (*wcl)->wcl_children[i] = 88274912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count]; 88374912Sjhb MPASS((*wcl)->wcl_children[i] != NULL); 88465557Sjasone 88574912Sjhb if ((*wcl)->wcl_count != 0) 88665557Sjasone return; 88765557Sjasone 88874912Sjhb wcl1 = *wcl; 88974912Sjhb *wcl = wcl1->wcl_next; 89074912Sjhb witness_child_free(wcl1); 89165557Sjasone} 89265557Sjasone 89365557Sjasonestatic int 89465856Sjhbisitmychild(struct witness *parent, struct witness *child) 89565557Sjasone{ 89674912Sjhb struct witness_child_list_entry *wcl; 89765557Sjasone int i; 89865557Sjasone 89974912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 90074912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 90174912Sjhb if (wcl->wcl_children[i] == child) 90265557Sjasone return (1); 90365557Sjasone } 90465557Sjasone } 90565557Sjasone return (0); 90665557Sjasone} 90765557Sjasone 90865557Sjasonestatic int 90965856Sjhbisitmydescendant(struct witness *parent, struct witness *child) 91065557Sjasone{ 91174912Sjhb struct witness_child_list_entry *wcl; 91274912Sjhb int i, j; 91365557Sjasone 91474912Sjhb if (isitmychild(parent, child)) 91574912Sjhb return (1); 91674912Sjhb j = 0; 91774912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 91867352Sjhb MPASS(j < 1000); 91974912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 92074912Sjhb if (isitmydescendant(wcl->wcl_children[i], child)) 92165557Sjasone return (1); 92265557Sjasone } 92374912Sjhb j++; 92465557Sjasone } 92565557Sjasone return (0); 92665557Sjasone} 92765557Sjasone 92865557Sjasonevoid 92965557Sjasonewitness_levelall (void) 93065557Sjasone{ 93174912Sjhb struct witness_list *list; 93265856Sjhb struct witness *w, *w1; 93365557Sjasone 93474912Sjhb /* 93574912Sjhb * First clear all levels. 93674912Sjhb */ 93774912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 93874912Sjhb w->w_level = 0; 93974912Sjhb } 94074912Sjhb 94174912Sjhb /* 94274912Sjhb * Look for locks with no parent and level all their descendants. 94374912Sjhb */ 94474912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 94574912Sjhb /* 94674912Sjhb * This is just an optimization, technically we could get 94774912Sjhb * away just walking the all list each time. 94874912Sjhb */ 94974912Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 95074912Sjhb list = &w_sleep; 95174912Sjhb else 95274912Sjhb list = &w_spin; 95374912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 95465557Sjasone if (isitmychild(w1, w)) 95574912Sjhb goto skip; 95665557Sjasone } 95765557Sjasone witness_leveldescendents(w, 0); 95874912Sjhb skip: 95965557Sjasone } 96065557Sjasone} 96165557Sjasone 96265557Sjasonestatic void 96365856Sjhbwitness_leveldescendents(struct witness *parent, int level) 96465557Sjasone{ 96574912Sjhb struct witness_child_list_entry *wcl; 96665557Sjasone int i; 96765557Sjasone 96865557Sjasone if (parent->w_level < level) 96965557Sjasone parent->w_level = level; 97065557Sjasone level++; 97174912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 97274912Sjhb for (i = 0; i < wcl->wcl_count; i++) 97374912Sjhb witness_leveldescendents(wcl->wcl_children[i], level); 97465557Sjasone} 97565557Sjasone 97665557Sjasonestatic void 97765856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...), 97865856Sjhb struct witness *parent) 97965557Sjasone{ 98074912Sjhb struct witness_child_list_entry *wcl; 98174912Sjhb int i, level; 98265557Sjasone 98374912Sjhb level = parent->w_level; 98472224Sjhb 98574912Sjhb prnt("%-2d", level); 98665557Sjasone for (i = 0; i < level; i++) 98765557Sjasone prnt(" "); 98874912Sjhb prnt("%s", parent->w_name); 98972224Sjhb if (parent->w_file != NULL) 99072224Sjhb prnt(" -- last acquired @ %s:%d\n", parent->w_file, 99172224Sjhb parent->w_line); 99265557Sjasone 99374912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 99474912Sjhb for (i = 0; i < wcl->wcl_count; i++) 99574912Sjhb witness_displaydescendants(prnt, 99674912Sjhb wcl->wcl_children[i]); 99774912Sjhb} 99865557Sjasone 99965557Sjasonestatic int 100065856Sjhbdup_ok(struct witness *w) 100165557Sjasone{ 100274912Sjhb const char **dup; 100365557Sjasone 100474912Sjhb for (dup = dup_list; *dup != NULL; dup++) 100574912Sjhb if (strcmp(w->w_name, *dup) == 0) 100665557Sjasone return (1); 100765557Sjasone return (0); 100865557Sjasone} 100965557Sjasone 101065557Sjasonestatic int 101165856Sjhbblessed(struct witness *w1, struct witness *w2) 101265557Sjasone{ 101365557Sjasone int i; 101465856Sjhb struct witness_blessed *b; 101565557Sjasone 101665557Sjasone for (i = 0; i < blessed_count; i++) { 101765557Sjasone b = &blessed_list[i]; 101874912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 101974912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 102065557Sjasone return (1); 102165557Sjasone continue; 102265557Sjasone } 102374912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 102474912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 102565557Sjasone return (1); 102665557Sjasone } 102765557Sjasone return (0); 102865557Sjasone} 102965557Sjasone 103065856Sjhbstatic struct witness * 103174912Sjhbwitness_get(void) 103265557Sjasone{ 103365856Sjhb struct witness *w; 103465557Sjasone 103574912Sjhb if (STAILQ_EMPTY(&w_free)) { 103665557Sjasone witness_dead = 1; 103774912Sjhb mtx_unlock_spin(&w_mtx); 103874912Sjhb printf("%s: witness exhausted\n", __func__); 103965557Sjasone return (NULL); 104065557Sjasone } 104174912Sjhb w = STAILQ_FIRST(&w_free); 104274912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 104365856Sjhb bzero(w, sizeof(*w)); 104465557Sjasone return (w); 104565557Sjasone} 104665557Sjasone 104765557Sjasonestatic void 104865856Sjhbwitness_free(struct witness *w) 104965557Sjasone{ 105074912Sjhb 105174912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 105265557Sjasone} 105365557Sjasone 105474912Sjhbstatic struct witness_child_list_entry * 105574912Sjhbwitness_child_get(void) 105665557Sjasone{ 105774912Sjhb struct witness_child_list_entry *wcl; 105865557Sjasone 105974912Sjhb wcl = w_child_free; 106074912Sjhb if (wcl == NULL) { 106174912Sjhb witness_dead = 1; 106274912Sjhb mtx_unlock_spin(&w_mtx); 106374912Sjhb printf("%s: witness exhausted\n", __func__); 106474912Sjhb return (NULL); 106565557Sjasone } 106674912Sjhb w_child_free = wcl->wcl_next; 106774912Sjhb bzero(wcl, sizeof(*wcl)); 106874912Sjhb return (wcl); 106974912Sjhb} 107069881Sjake 107174912Sjhbstatic void 107274912Sjhbwitness_child_free(struct witness_child_list_entry *wcl) 107374912Sjhb{ 107474912Sjhb 107574912Sjhb wcl->wcl_next = w_child_free; 107674912Sjhb w_child_free = wcl; 107765557Sjasone} 107865557Sjasone 107974912Sjhbstatic struct lock_list_entry * 108074912Sjhbwitness_lock_list_get(void) 108174912Sjhb{ 108274912Sjhb struct lock_list_entry *lle; 108371709Sjhb 108474912Sjhb mtx_lock_spin(&w_mtx); 108574912Sjhb lle = w_lock_list_free; 108674912Sjhb if (lle == NULL) { 108774912Sjhb witness_dead = 1; 108874912Sjhb mtx_unlock_spin(&w_mtx); 108974912Sjhb printf("%s: witness exhausted\n", __func__); 109074912Sjhb return (NULL); 109174912Sjhb } 109274912Sjhb w_lock_list_free = lle->ll_next; 109374912Sjhb mtx_unlock_spin(&w_mtx); 109474912Sjhb bzero(lle, sizeof(*lle)); 109574912Sjhb return (lle); 109674912Sjhb} 109774912Sjhb 109874912Sjhbstatic void 109974912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 110071709Sjhb{ 110171709Sjhb 110274912Sjhb mtx_lock_spin(&w_mtx); 110374912Sjhb lle->ll_next = w_lock_list_free; 110474912Sjhb w_lock_list_free = lle; 110574912Sjhb mtx_unlock_spin(&w_mtx); 110671709Sjhb} 110771709Sjhb 110874912Sjhbint 110975273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 111072224Sjhb{ 111175273Sjhb struct lock_list_entry *lle; 111274912Sjhb struct lock_object *lock; 111374912Sjhb int i, nheld; 111472224Sjhb 111574912Sjhb nheld = 0; 111674912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 111774912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 111874912Sjhb lock = lle->ll_children[i]; 111974912Sjhb printf("\t(%s) %s (%p) locked at %s:%d\n", 112074912Sjhb lock->lo_class->lc_name, lock->lo_name, lock, 112174912Sjhb lock->lo_file, lock->lo_line); 112274912Sjhb nheld++; 112374912Sjhb } 112475273Sjhb return (nheld); 112575273Sjhb} 112675273Sjhb 112775273Sjhb/* 112875273Sjhb * Calling this on p != curproc is bad unless we are in ddb. 112975273Sjhb */ 113075273Sjhbint 113175273Sjhbwitness_list(struct proc *p) 113275273Sjhb{ 113375273Sjhb critical_t savecrit; 113475273Sjhb int nheld; 113575273Sjhb 113675273Sjhb KASSERT(p == curproc || db_active, 113775273Sjhb ("%s: p != curproc and we aren't in the debugger", __func__)); 113875273Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 113975273Sjhb 114075273Sjhb nheld = witness_list_locks(&p->p_sleeplocks); 114175273Sjhb 114274912Sjhb /* 114374912Sjhb * We only handle spinlocks if p == curproc. This is somewhat broken 114474912Sjhb * if p is currently executing on some other CPU and holds spin locks 114575273Sjhb * as we won't display those locks. If we had a MI way of getting 114675273Sjhb * the per-cpu data for a given cpu then we could use p->p_oncpu to 114775273Sjhb * get the list of spinlocks for this process and "fix" this. 114874912Sjhb */ 114975273Sjhb if (p == curproc) { 115075273Sjhb /* 115175273Sjhb * Preemption bad because we need PCPU_PTR(spinlocks) to not 115275273Sjhb * change. 115375273Sjhb */ 115475273Sjhb savecrit = critical_enter(); 115575273Sjhb nheld += witness_list_locks(PCPU_PTR(spinlocks)); 115675273Sjhb critical_exit(savecrit); 115774912Sjhb } 115874912Sjhb 115974912Sjhb return (nheld); 116072224Sjhb} 116171709Sjhb 116265557Sjasonevoid 116374912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 116465557Sjasone{ 116571320Sjasone 116674912Sjhb KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 116774912Sjhb if (lock->lo_witness == NULL) 116871352Sjasone return; 116971352Sjasone 117074912Sjhb *filep = lock->lo_file; 117174912Sjhb *linep = lock->lo_line; 117265557Sjasone} 117365557Sjasone 117465557Sjasonevoid 117574912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 117665557Sjasone{ 117771320Sjasone 117874912Sjhb KASSERT(!witness_cold, ("%s: witness_cold\n", __func__)); 117974912Sjhb if (lock->lo_witness == NULL) 118071352Sjasone return; 118171352Sjasone 118274912Sjhb lock->lo_witness->w_file = file; 118374912Sjhb lock->lo_witness->w_line = line; 118474912Sjhb lock->lo_file = file; 118574912Sjhb lock->lo_line = line; 118665557Sjasone} 118765557Sjasone 118874912Sjhb#ifdef DDB 118974912Sjhb 119074930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 119174912Sjhb{ 119275273Sjhb struct proc *p; 119375273Sjhb pid_t pid; 119474912Sjhb 119575273Sjhb if (have_addr) { 119675273Sjhb pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 119775273Sjhb ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 119875273Sjhb ((addr >> 16) % 16) * 10000; 119975273Sjhb 120075273Sjhb /* sx_slock(&allproc_lock); */ 120175273Sjhb LIST_FOREACH(p, &allproc, p_list) { 120275273Sjhb if (p->p_pid == pid) 120375273Sjhb break; 120475273Sjhb } 120575273Sjhb /* sx_sunlock(&allproc_lock); */ 120675273Sjhb if (p == NULL) { 120775273Sjhb db_printf("pid %d not found\n", pid); 120875273Sjhb return; 120975273Sjhb } 121075273Sjhb } else 121175273Sjhb p = curproc; 121275273Sjhb 121375273Sjhb witness_list(p); 121474912Sjhb} 121574912Sjhb 121674912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 121774912Sjhb{ 121874912Sjhb 121974912Sjhb witness_display(db_printf); 122074912Sjhb} 122174912Sjhb#endif 1222