subr_witness.c revision 95473
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 95473 2002-04-25 22:48:40Z des $ 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/* 7883798Sjhb * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 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 blessed(struct witness *, struct witness *); 12874912Sjhbstatic void witness_display_list(void(*prnt)(const char *fmt, ...), 12974912Sjhb struct witness_list *list); 13074912Sjhbstatic void witness_displaydescendants(void(*)(const char *fmt, ...), 13174912Sjhb struct witness *); 13274912Sjhbstatic void witness_leveldescendents(struct witness *parent, int level); 13374912Sjhbstatic void witness_levelall(void); 13474912Sjhbstatic struct witness *witness_get(void); 13574912Sjhbstatic void witness_free(struct witness *m); 13674912Sjhbstatic struct witness_child_list_entry *witness_child_get(void); 13774912Sjhbstatic void witness_child_free(struct witness_child_list_entry *wcl); 13874912Sjhbstatic struct lock_list_entry *witness_lock_list_get(void); 13974912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 14074912Sjhbstatic void witness_display(void(*)(const char *fmt, ...)); 14176272Sjhbstatic struct lock_instance *find_instance(struct lock_list_entry *lock_list, 14276272Sjhb struct lock_object *lock); 14372200Sbmilekic 14474912SjhbMALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 14572200Sbmilekic 14677843Speterstatic int witness_watch = 1; 14777900SpeterTUNABLE_INT("debug.witness_watch", &witness_watch); 14874912SjhbSYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 14971352Sjasone 15067352Sjhb#ifdef DDB 15172200Sbmilekic/* 15267676Sjhb * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 15365557Sjasone * drop into kdebug() when: 15465557Sjasone * - a lock heirarchy violation occurs 15565557Sjasone * - locks are held when going to sleep. 15665557Sjasone */ 15767676Sjhb#ifdef WITNESS_DDB 15877843Speterint witness_ddb = 1; 15967676Sjhb#else 16077843Speterint witness_ddb = 0; 16165557Sjasone#endif 16277900SpeterTUNABLE_INT("debug.witness_ddb", &witness_ddb); 16367676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 16467676Sjhb#endif /* DDB */ 16565557Sjasone 16667676Sjhb#ifdef WITNESS_SKIPSPIN 16777843Speterint witness_skipspin = 1; 16867676Sjhb#else 16977843Speterint witness_skipspin = 0; 17065557Sjasone#endif 17177900SpeterTUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 17267676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 17367676Sjhb ""); 17465557Sjasone 17574912Sjhbstatic struct mtx w_mtx; 17674912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 17774912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 17874912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 17974912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 18074912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL; 18174912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 18274912Sjhbstatic int witness_dead; /* fatal error, probably no memory */ 18365557Sjasone 18474912Sjhbstatic struct witness w_data[WITNESS_COUNT]; 18574912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 18674912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 18765557Sjasone 18874912Sjhbstatic struct witness_order_list_entry order_lists[] = { 18974912Sjhb { "Giant", &lock_class_mtx_sleep }, 19074912Sjhb { "proctree", &lock_class_sx }, 19174912Sjhb { "allproc", &lock_class_sx }, 19291140Stanimura { "process group", &lock_class_mtx_sleep }, 19374912Sjhb { "process lock", &lock_class_mtx_sleep }, 19491140Stanimura { "session", &lock_class_mtx_sleep }, 19574912Sjhb { "uidinfo hash", &lock_class_mtx_sleep }, 19674912Sjhb { "uidinfo struct", &lock_class_mtx_sleep }, 19774912Sjhb { NULL, NULL }, 19875464Sjhb /* 19975464Sjhb * spin locks 20075464Sjhb */ 20184331Sjhb#ifdef SMP 20284331Sjhb { "ap boot", &lock_class_mtx_spin }, 20384331Sjhb#ifdef __i386__ 20474912Sjhb { "com", &lock_class_mtx_spin }, 20572224Sjhb#endif 20684331Sjhb#endif 20774912Sjhb { "sio", &lock_class_mtx_spin }, 20872224Sjhb#ifdef __i386__ 20974912Sjhb { "cy", &lock_class_mtx_spin }, 21072224Sjhb#endif 21174912Sjhb { "ng_node", &lock_class_mtx_spin }, 21274912Sjhb { "ng_worklist", &lock_class_mtx_spin }, 21374912Sjhb { "ithread table lock", &lock_class_mtx_spin }, 21474912Sjhb { "sched lock", &lock_class_mtx_spin }, 21574912Sjhb { "callout", &lock_class_mtx_spin }, 21665557Sjasone /* 21765557Sjasone * leaf locks 21865557Sjasone */ 21990278Sjhb { "allpmaps", &lock_class_mtx_spin }, 22088322Sjhb { "icu", &lock_class_mtx_spin }, 22172224Sjhb#ifdef SMP 22274912Sjhb { "smp rendezvous", &lock_class_mtx_spin }, 22372224Sjhb#endif 22478785Sjhb { "clk", &lock_class_mtx_spin }, 22595473Sdes { "mutex profiling lock", &lock_class_mtx_spin }, 22674912Sjhb { NULL, NULL }, 22774912Sjhb { NULL, NULL } 22865557Sjasone}; 22965557Sjasone 23065557Sjasone/* 23165557Sjasone * Pairs of locks which have been blessed 23265557Sjasone * Don't complain about order problems with blessed locks 23365557Sjasone */ 23465856Sjhbstatic struct witness_blessed blessed_list[] = { 23565557Sjasone}; 23672200Sbmilekicstatic int blessed_count = 23772200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 23865557Sjasone 23974912Sjhb/* 24074912Sjhb * List of all locks in the system. 24174912Sjhb */ 24274912SjhbSTAILQ_HEAD(, lock_object) all_locks = STAILQ_HEAD_INITIALIZER(all_locks); 24374912Sjhb 24474912Sjhbstatic struct mtx all_mtx = { 24574912Sjhb { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 24674912Sjhb "All locks list", /* mtx_object.lo_name */ 24793811Sjhb "All locks list", /* mtx_object.lo_type */ 24874912Sjhb LO_INITIALIZED, /* mtx_object.lo_flags */ 24974912Sjhb { NULL }, /* mtx_object.lo_list */ 25074912Sjhb NULL }, /* mtx_object.lo_witness */ 25174912Sjhb MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 25274912Sjhb TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 25374912Sjhb { NULL, NULL } /* mtx_contested */ 25474912Sjhb}; 25574912Sjhb 25674912Sjhb/* 25774912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 25874912Sjhb */ 25974912Sjhbstatic int witness_cold = 1; 26074912Sjhb 26174912Sjhb/* 26274912Sjhb * Global variables for book keeping. 26374912Sjhb */ 26474912Sjhbstatic int lock_cur_cnt; 26574912Sjhbstatic int lock_max_cnt; 26674912Sjhb 26774912Sjhb/* 26874912Sjhb * The WITNESS-enabled diagnostic code. 26974912Sjhb */ 27071352Sjasonestatic void 27174912Sjhbwitness_initialize(void *dummy __unused) 27265557Sjasone{ 27374912Sjhb struct lock_object *lock; 27474912Sjhb struct witness_order_list_entry *order; 27574912Sjhb struct witness *w, *w1; 27674912Sjhb int i; 27765557Sjasone 27874912Sjhb /* 27974912Sjhb * We have to release Giant before initializing its witness 28074912Sjhb * structure so that WITNESS doesn't get confused. 28174912Sjhb */ 28274912Sjhb mtx_unlock(&Giant); 28374912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 28474912Sjhb 28587593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 28674912Sjhb STAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 28793811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 28893811Sjhb 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) 31693811Sjhb lock->lo_witness = enroll(lock->lo_type, 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) 33782284Sjhb panic("%s: lock (%s) %s is already initialized", __func__, 33874912Sjhb class->lc_name, lock->lo_name); 33974912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 34074912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 34182284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 34274912Sjhb class->lc_name, lock->lo_name); 34374912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 34474912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 34582284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 34674912Sjhb class->lc_name, lock->lo_name); 34782244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 34882244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 34982284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 35082244Sjhb class->lc_name, lock->lo_name); 35182244Sjhb 35274912Sjhb mtx_lock(&all_mtx); 35374912Sjhb STAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 35474912Sjhb lock->lo_flags |= LO_INITIALIZED; 35574912Sjhb lock_cur_cnt++; 35674912Sjhb if (lock_cur_cnt > lock_max_cnt) 35774912Sjhb lock_max_cnt = lock_cur_cnt; 35874912Sjhb mtx_unlock(&all_mtx); 35980747Sjhb if (!witness_cold && !witness_dead && panicstr == NULL && 36074912Sjhb (lock->lo_flags & LO_WITNESS) != 0) 36193811Sjhb lock->lo_witness = enroll(lock->lo_type, class); 36274912Sjhb else 36374912Sjhb lock->lo_witness = NULL; 36474912Sjhb} 36574912Sjhb 36674912Sjhbvoid 36774912Sjhbwitness_destroy(struct lock_object *lock) 36874912Sjhb{ 36975362Sjhb struct witness *w; 37074912Sjhb 37174912Sjhb if (witness_cold) 37274912Sjhb panic("lock (%s) %s destroyed while witness_cold", 37374912Sjhb lock->lo_class->lc_name, lock->lo_name); 37474912Sjhb if ((lock->lo_flags & LO_INITIALIZED) == 0) 37582284Sjhb panic("%s: lock (%s) %s is not initialized", __func__, 37674912Sjhb lock->lo_class->lc_name, lock->lo_name); 37774912Sjhb 37876272Sjhb /* XXX: need to verify that no one holds the lock */ 37975362Sjhb w = lock->lo_witness; 38075362Sjhb if (w != NULL) { 38175362Sjhb mtx_lock_spin(&w_mtx); 38275362Sjhb w->w_refcount--; 38375362Sjhb if (w->w_refcount == 0) { 38487593Sobrien CTR2(KTR_WITNESS, 38587593Sobrien "%s: marking witness %s as dead", __func__, w->w_name); 38675362Sjhb w->w_name = "(dead)"; 38775362Sjhb w->w_file = "(dead)"; 38875362Sjhb w->w_line = 0; 38975362Sjhb } 39075362Sjhb mtx_unlock_spin(&w_mtx); 39175362Sjhb } 39275362Sjhb 39374912Sjhb mtx_lock(&all_mtx); 39474912Sjhb lock_cur_cnt--; 39574912Sjhb STAILQ_REMOVE(&all_locks, lock, lock_object, lo_list); 39680055Sjhb lock->lo_flags &= ~LO_INITIALIZED; 39774912Sjhb mtx_unlock(&all_mtx); 39874912Sjhb} 39974912Sjhb 40071352Sjasonestatic void 40174912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...), 40274912Sjhb struct witness_list *list) 40371352Sjasone{ 40471352Sjasone struct witness *w, *w1; 40574912Sjhb int found; 40671352Sjasone 40774912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 40874912Sjhb if (w->w_file == NULL) 40971352Sjasone continue; 41074912Sjhb found = 0; 41174912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 41274912Sjhb if (isitmychild(w1, w)) { 41374912Sjhb found++; 41471352Sjasone break; 41574912Sjhb } 41671352Sjasone } 41774912Sjhb if (found) 41871352Sjasone continue; 41971352Sjasone /* 42071352Sjasone * This lock has no anscestors, display its descendants. 42171352Sjasone */ 42271352Sjasone witness_displaydescendants(prnt, w); 42371352Sjasone } 42474912Sjhb} 42572224Sjhb 42674912Sjhbstatic void 42774912Sjhbwitness_display(void(*prnt)(const char *fmt, ...)) 42874912Sjhb{ 42974912Sjhb struct witness *w; 43074912Sjhb 43182284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 43274912Sjhb witness_levelall(); 43374912Sjhb 43472224Sjhb /* 43574930Sjhb * First, handle sleep locks which have been acquired at least 43674912Sjhb * once. 43774912Sjhb */ 43874912Sjhb prnt("Sleep locks:\n"); 43974912Sjhb witness_display_list(prnt, &w_sleep); 44074912Sjhb 44174912Sjhb /* 44274930Sjhb * Now do spin locks which have been acquired at least once. 44372224Sjhb */ 44474912Sjhb prnt("\nSpin locks:\n"); 44574912Sjhb witness_display_list(prnt, &w_spin); 44672224Sjhb 44772224Sjhb /* 44874930Sjhb * Finally, any locks which have not been acquired yet. 44972224Sjhb */ 45074912Sjhb prnt("\nLocks which were never acquired:\n"); 45174912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 45271352Sjasone if (w->w_file != NULL) 45371352Sjasone continue; 45474912Sjhb prnt("%s\n", w->w_name); 45571352Sjasone } 45671352Sjasone} 45771352Sjasone 45865557Sjasonevoid 45974912Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 46065557Sjasone{ 46174912Sjhb struct lock_list_entry **lock_list, *lle; 46276272Sjhb struct lock_instance *lock1, *lock2; 46374912Sjhb struct lock_class *class; 46465856Sjhb struct witness *w, *w1; 46583366Sjulian struct thread *td; 46674912Sjhb int i, j; 46767676Sjhb#ifdef DDB 46867676Sjhb int go_into_ddb = 0; 46967676Sjhb#endif /* DDB */ 47065557Sjasone 47174912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 47280747Sjhb panicstr != NULL) 47371320Sjasone return; 47474912Sjhb w = lock->lo_witness; 47574912Sjhb class = lock->lo_class; 47683366Sjulian td = curthread; 47765557Sjasone 47874912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 47993676Sjhb /* 48093676Sjhb * Since spin locks include a critical section, this check 48193676Sjhb * impliclty enforces a lock order of all sleep locks before 48293676Sjhb * all spin locks. 48393676Sjhb */ 48488899Sjhb if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0) 48574912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 48674912Sjhb class->lc_name, lock->lo_name, file, line); 48783366Sjulian lock_list = &td->td_sleeplocks; 48888899Sjhb } else 48988899Sjhb lock_list = PCPU_PTR(spinlocks); 49065557Sjasone 49176772Sjhb /* 49276772Sjhb * Try locks do not block if they fail to acquire the lock, thus 49376772Sjhb * there is no danger of deadlocks or of switching while holding a 49476772Sjhb * spin lock if we acquire a lock via a try operation. 49576772Sjhb */ 49674912Sjhb if (flags & LOP_TRYLOCK) 49765557Sjasone goto out; 49865557Sjasone 49965557Sjasone /* 50074912Sjhb * Is this the first lock acquired? If so, then no order checking 50174912Sjhb * is needed. 50265557Sjasone */ 50374912Sjhb if (*lock_list == NULL) 50465557Sjasone goto out; 50565557Sjasone 50674912Sjhb /* 50776272Sjhb * Check to see if we are recursing on a lock we already own. 50876272Sjhb */ 50976272Sjhb lock1 = find_instance(*lock_list, lock); 51076272Sjhb if (lock1 != NULL) { 51176272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 51276272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 51376272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 51476272Sjhb class->lc_name, lock->lo_name, file, line); 51576272Sjhb printf("while exclusively locked from %s:%d\n", 51676272Sjhb lock1->li_file, lock1->li_line); 51776272Sjhb panic("share->excl"); 51876272Sjhb } 51976272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 52076272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 52176272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 52276272Sjhb class->lc_name, lock->lo_name, file, line); 52376272Sjhb printf("while share locked from %s:%d\n", 52476272Sjhb lock1->li_file, lock1->li_line); 52576272Sjhb panic("excl->share"); 52676272Sjhb } 52776272Sjhb lock1->li_flags++; 52876272Sjhb if ((lock->lo_flags & LO_RECURSABLE) == 0) { 52976272Sjhb printf( 53076272Sjhb "recursed on non-recursive lock (%s) %s @ %s:%d\n", 53176272Sjhb class->lc_name, lock->lo_name, file, line); 53276272Sjhb printf("first acquired @ %s:%d\n", lock1->li_file, 53376272Sjhb lock1->li_line); 53476272Sjhb panic("recurse"); 53576272Sjhb } 53687593Sobrien CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 53784680Sjhb td->td_proc->p_pid, lock->lo_name, 53878785Sjhb lock1->li_flags & LI_RECURSEMASK); 53976272Sjhb lock1->li_file = file; 54076272Sjhb lock1->li_line = line; 54176272Sjhb return; 54276272Sjhb } 54376272Sjhb 54476272Sjhb /* 54574912Sjhb * Check for duplicate locks of the same type. Note that we only 54674912Sjhb * have to check for this on the last lock we just acquired. Any 54774912Sjhb * other cases will be caught as lock order violations. 54874912Sjhb */ 54976272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 55076272Sjhb w1 = lock1->li_lock->lo_witness; 55174912Sjhb if (w1 == w) { 55293273Sjeff if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK)) 55365557Sjasone goto out; 55465557Sjasone w->w_same_squawked = 1; 55575755Sjhb printf("acquiring duplicate lock of same type: \"%s\"\n", 55693811Sjhb lock->lo_type); 55793811Sjhb printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 55893811Sjhb lock1->li_file, lock1->li_line); 55993811Sjhb printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 56067676Sjhb#ifdef DDB 56167676Sjhb go_into_ddb = 1; 56267676Sjhb#endif /* DDB */ 56365557Sjasone goto out; 56465557Sjasone } 56565557Sjasone MPASS(!mtx_owned(&w_mtx)); 56674912Sjhb mtx_lock_spin(&w_mtx); 56765557Sjasone /* 56865557Sjasone * If we have a known higher number just say ok 56965557Sjasone */ 57065557Sjasone if (witness_watch > 1 && w->w_level > w1->w_level) { 57174912Sjhb mtx_unlock_spin(&w_mtx); 57265557Sjasone goto out; 57365557Sjasone } 57474912Sjhb if (isitmydescendant(w1, w)) { 57574912Sjhb mtx_unlock_spin(&w_mtx); 57665557Sjasone goto out; 57765557Sjasone } 57874912Sjhb for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 57974912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 58065557Sjasone 58174912Sjhb MPASS(j < WITNESS_COUNT); 58276272Sjhb lock1 = &lle->ll_children[i]; 58376272Sjhb w1 = lock1->li_lock->lo_witness; 58474912Sjhb 58574912Sjhb /* 58674912Sjhb * If this lock doesn't undergo witness checking, 58774912Sjhb * then skip it. 58874912Sjhb */ 58974912Sjhb if (w1 == NULL) { 59076272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 59174912Sjhb ("lock missing witness structure")); 59274912Sjhb continue; 59374912Sjhb } 59476272Sjhb /* 59576272Sjhb * If we are locking Giant and we slept with this 59676272Sjhb * lock, then skip it. 59776272Sjhb */ 59876272Sjhb if ((lock1->li_flags & LI_SLEPT) != 0 && 59976272Sjhb lock == &Giant.mtx_object) 60076272Sjhb continue; 60193690Sjhb /* 60293690Sjhb * If we are locking a sleepable lock and this lock 60393690Sjhb * isn't sleepable and isn't Giant, we want to treat 60493690Sjhb * it as a lock order violation to enfore a general 60593690Sjhb * lock order of sleepable locks before non-sleepable 60693690Sjhb * locks. Thus, we only bother checking the lock 60793690Sjhb * order hierarchy if we pass the initial test. 60893690Sjhb */ 60993690Sjhb if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 61093690Sjhb ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 61193690Sjhb lock1->li_lock != &Giant.mtx_object)) && 61293690Sjhb !isitmydescendant(w, w1)) 61374912Sjhb continue; 61474912Sjhb /* 61574912Sjhb * We have a lock order violation, check to see if it 61674912Sjhb * is allowed or has already been yelled about. 61774912Sjhb */ 61874912Sjhb mtx_unlock_spin(&w_mtx); 61965557Sjasone if (blessed(w, w1)) 62065557Sjasone goto out; 62176272Sjhb if (lock1->li_lock == &Giant.mtx_object) { 62265557Sjasone if (w1->w_Giant_squawked) 62365557Sjasone goto out; 62465557Sjasone else 62565557Sjasone w1->w_Giant_squawked = 1; 62665557Sjasone } else { 62765557Sjasone if (w1->w_other_squawked) 62865557Sjasone goto out; 62965557Sjasone else 63065557Sjasone w1->w_other_squawked = 1; 63165557Sjasone } 63274912Sjhb /* 63374912Sjhb * Ok, yell about it. 63474912Sjhb */ 63565557Sjasone printf("lock order reversal\n"); 63674912Sjhb /* 63774912Sjhb * Try to locate an earlier lock with 63874912Sjhb * witness w in our list. 63974912Sjhb */ 64074912Sjhb do { 64176272Sjhb lock2 = &lle->ll_children[i]; 64276272Sjhb MPASS(lock2->li_lock != NULL); 64376272Sjhb if (lock2->li_lock->lo_witness == w) 64474912Sjhb break; 64574912Sjhb i--; 64674912Sjhb if (i == 0 && lle->ll_next != NULL) { 64774912Sjhb lle = lle->ll_next; 64874912Sjhb i = lle->ll_count - 1; 64974912Sjhb MPASS(i != 0); 65074912Sjhb } 65174912Sjhb } while (i >= 0); 65276272Sjhb if (i < 0) { 65393811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 65493811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 65593811Sjhb lock1->li_lock->lo_type, lock1->li_file, 65676272Sjhb lock1->li_line); 65793811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 65893811Sjhb lock->lo_name, lock->lo_type, file, line); 65976272Sjhb } else { 66093811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 66193811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 66293811Sjhb lock2->li_lock->lo_type, lock2->li_file, 66376272Sjhb lock2->li_line); 66493811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 66593811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 66693811Sjhb lock1->li_lock->lo_type, lock1->li_file, 66776272Sjhb lock1->li_line); 66893811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 66993811Sjhb lock->lo_name, lock->lo_type, file, line); 67076272Sjhb } 67167676Sjhb#ifdef DDB 67267676Sjhb go_into_ddb = 1; 67367676Sjhb#endif /* DDB */ 67465557Sjasone goto out; 67565557Sjasone } 67665557Sjasone } 67776272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 67878871Sjhb /* 67978871Sjhb * Don't build a new relationship if we are locking Giant just 68078871Sjhb * after waking up and the previous lock in the list was acquired 68178871Sjhb * prior to blocking. 68278871Sjhb */ 68378871Sjhb if (lock == &Giant.mtx_object && (lock1->li_flags & LI_SLEPT) != 0) 68474912Sjhb mtx_unlock_spin(&w_mtx); 68578871Sjhb else { 68687593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 68793811Sjhb lock->lo_type, lock1->li_lock->lo_type); 68878871Sjhb if (!itismychild(lock1->li_lock->lo_witness, w)) 68978871Sjhb mtx_unlock_spin(&w_mtx); 69078871Sjhb } 69165557Sjasone 69265557Sjasoneout: 69367676Sjhb#ifdef DDB 69467676Sjhb if (witness_ddb && go_into_ddb) 69575711Sjhb Debugger(__func__); 69667676Sjhb#endif /* DDB */ 69765557Sjasone w->w_file = file; 69865557Sjasone w->w_line = line; 69974912Sjhb 70074912Sjhb lle = *lock_list; 70176272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 70278785Sjhb lle = witness_lock_list_get(); 70378785Sjhb if (lle == NULL) 70465557Sjasone return; 70578785Sjhb lle->ll_next = *lock_list; 70687593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 70784680Sjhb td->td_proc->p_pid, lle); 70878785Sjhb *lock_list = lle; 70965557Sjasone } 71076272Sjhb lock1 = &lle->ll_children[lle->ll_count++]; 71176272Sjhb lock1->li_lock = lock; 71276272Sjhb lock1->li_line = line; 71376272Sjhb lock1->li_file = file; 71476272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 71576272Sjhb lock1->li_flags = LI_EXCLUSIVE; 71676272Sjhb else 71776272Sjhb lock1->li_flags = 0; 71887593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 71984680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 72065557Sjasone} 72165557Sjasone 72265557Sjasonevoid 72382244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 72482244Sjhb{ 72582244Sjhb struct lock_instance *instance; 72682244Sjhb struct lock_class *class; 72782244Sjhb 72882284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 72982244Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 73082244Sjhb return; 73182244Sjhb class = lock->lo_class; 73282244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 73382244Sjhb panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 73482244Sjhb class->lc_name, lock->lo_name, file, line); 73582244Sjhb if ((flags & LOP_TRYLOCK) == 0) 73682244Sjhb panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 73782244Sjhb lock->lo_name, file, line); 73882244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 73982244Sjhb panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 74082244Sjhb class->lc_name, lock->lo_name, file, line); 74183366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 74282244Sjhb if (instance == NULL) 74382244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 74482244Sjhb class->lc_name, lock->lo_name, file, line); 74582244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0) 74682244Sjhb panic("upgrade of exclusive lock (%s) %s @ %s:%d", 74782244Sjhb class->lc_name, lock->lo_name, file, line); 74882244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 74982244Sjhb panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 75082244Sjhb class->lc_name, lock->lo_name, 75182244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 75282244Sjhb instance->li_flags |= LI_EXCLUSIVE; 75382244Sjhb} 75482244Sjhb 75582244Sjhbvoid 75682244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 75782244Sjhb int line) 75882244Sjhb{ 75982244Sjhb struct lock_instance *instance; 76082244Sjhb struct lock_class *class; 76182244Sjhb 76282284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 76382244Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 76482244Sjhb return; 76582244Sjhb class = lock->lo_class; 76682244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 76782244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 76882244Sjhb class->lc_name, lock->lo_name, file, line); 76982244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 77082244Sjhb panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 77182244Sjhb class->lc_name, lock->lo_name, file, line); 77283366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 77382244Sjhb if (instance == NULL) 77482244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 77582244Sjhb class->lc_name, lock->lo_name, file, line); 77682244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0) 77782244Sjhb panic("downgrade of shared lock (%s) %s @ %s:%d", 77882244Sjhb class->lc_name, lock->lo_name, file, line); 77982244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 78082244Sjhb panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 78182244Sjhb class->lc_name, lock->lo_name, 78282244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 78382244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 78482244Sjhb} 78582244Sjhb 78682244Sjhbvoid 78774912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 78865557Sjasone{ 78974912Sjhb struct lock_list_entry **lock_list, *lle; 79076272Sjhb struct lock_instance *instance; 79174912Sjhb struct lock_class *class; 79283366Sjulian struct thread *td; 79392858Simp register_t s; 79474912Sjhb int i, j; 79565557Sjasone 79674912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 79780747Sjhb panicstr != NULL) 79871352Sjasone return; 79983366Sjulian td = curthread; 80074912Sjhb class = lock->lo_class; 80176272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 80283366Sjulian lock_list = &td->td_sleeplocks; 80376272Sjhb else 80474912Sjhb lock_list = PCPU_PTR(spinlocks); 80574912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 80676272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 80776272Sjhb instance = &(*lock_list)->ll_children[i]; 80876272Sjhb if (instance->li_lock == lock) { 80976272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 81076272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 81176272Sjhb printf( 81276272Sjhb "shared unlock of (%s) %s @ %s:%d\n", 81376272Sjhb class->lc_name, lock->lo_name, 81476272Sjhb file, line); 81576272Sjhb printf( 81676272Sjhb "while exclusively locked from %s:%d\n", 81776272Sjhb instance->li_file, 81876272Sjhb instance->li_line); 81976272Sjhb panic("excl->ushare"); 82076272Sjhb } 82176272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 82276272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 82376272Sjhb printf( 82476272Sjhb "exclusive unlock of (%s) %s @ %s:%d\n", 82576272Sjhb class->lc_name, lock->lo_name, 82676272Sjhb file, line); 82776272Sjhb printf( 82876272Sjhb "while share locked from %s:%d\n", 82976272Sjhb instance->li_file, 83076272Sjhb instance->li_line); 83176272Sjhb panic("share->uexcl"); 83276272Sjhb } 83376272Sjhb /* If we are recursed, unrecurse. */ 83476272Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 83587593Sobrien CTR4(KTR_WITNESS, 83687593Sobrien "%s: pid %d unrecursed on %s r=%d", __func__, 83784680Sjhb td->td_proc->p_pid, 83878785Sjhb instance->li_lock->lo_name, 83978785Sjhb instance->li_flags); 84076272Sjhb instance->li_flags--; 84188900Sjhb return; 84276272Sjhb } 84392858Simp s = intr_disable(); 84487593Sobrien CTR4(KTR_WITNESS, 84587593Sobrien "%s: pid %d removed %s from lle[%d]", __func__, 84684680Sjhb td->td_proc->p_pid, 84784680Sjhb instance->li_lock->lo_name, 84878785Sjhb (*lock_list)->ll_count - 1); 84974912Sjhb (*lock_list)->ll_count--; 85074912Sjhb for (j = i; j < (*lock_list)->ll_count; j++) 85174912Sjhb (*lock_list)->ll_children[j] = 85274912Sjhb (*lock_list)->ll_children[j + 1]; 85392858Simp intr_restore(s); 85474912Sjhb if ((*lock_list)->ll_count == 0) { 85574912Sjhb lle = *lock_list; 85674912Sjhb *lock_list = lle->ll_next; 85787593Sobrien CTR3(KTR_WITNESS, 85887593Sobrien "%s: pid %d removed lle %p", __func__, 85984680Sjhb td->td_proc->p_pid, lle); 86074912Sjhb witness_lock_list_free(lle); 86174912Sjhb } 86288900Sjhb return; 86374912Sjhb } 86476272Sjhb } 86576272Sjhb panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 86676272Sjhb file, line); 86765557Sjasone} 86865557Sjasone 86974912Sjhb/* 87074912Sjhb * Warn if any held locks are not sleepable. Note that Giant and the lock 87174912Sjhb * passed in are both special cases since they are both released during the 87283798Sjhb * sleep process and aren't actually held while the thread is asleep. 87374912Sjhb */ 87465557Sjasoneint 87574912Sjhbwitness_sleep(int check_only, struct lock_object *lock, const char *file, 87674912Sjhb int line) 87765557Sjasone{ 87874912Sjhb struct lock_list_entry **lock_list, *lle; 87976272Sjhb struct lock_instance *lock1; 88083366Sjulian struct thread *td; 88174912Sjhb int i, n; 88265557Sjasone 88380747Sjhb if (witness_dead || panicstr != NULL) 88474912Sjhb return (0); 88582284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 88674912Sjhb n = 0; 88774912Sjhb /* 88874912Sjhb * Preemption bad because we need PCPU_PTR(spinlocks) to not change. 88974912Sjhb */ 89091905Sjhb critical_enter(); 89183366Sjulian td = curthread; 89283366Sjulian lock_list = &td->td_sleeplocks; 89374912Sjhbagain: 89474912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 89574912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 89676272Sjhb lock1 = &lle->ll_children[i]; 89776272Sjhb if (lock1->li_lock == lock || 89876272Sjhb lock1->li_lock == &Giant.mtx_object) 89974912Sjhb continue; 90076272Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) { 90178785Sjhb if (check_only == 0) { 90278785Sjhb CTR3(KTR_WITNESS, 90378871Sjhb "pid %d: sleeping with lock (%s) %s held", 90484680Sjhb td->td_proc->p_pid, 90578785Sjhb lock1->li_lock->lo_class->lc_name, 90678785Sjhb lock1->li_lock->lo_name); 90776272Sjhb lock1->li_flags |= LI_SLEPT; 90878785Sjhb } 90976272Sjhb continue; 91076272Sjhb } 91174912Sjhb n++; 91274912Sjhb printf("%s:%d: %s with \"%s\" locked from %s:%d\n", 91374912Sjhb file, line, check_only ? "could sleep" : "sleeping", 91476272Sjhb lock1->li_lock->lo_name, lock1->li_file, 91576272Sjhb lock1->li_line); 91674912Sjhb } 91783366Sjulian if (lock_list == &td->td_sleeplocks) { 91874912Sjhb lock_list = PCPU_PTR(spinlocks); 91974912Sjhb goto again; 92065557Sjasone } 92167676Sjhb#ifdef DDB 92267676Sjhb if (witness_ddb && n) 92375711Sjhb Debugger(__func__); 92467676Sjhb#endif /* DDB */ 92591905Sjhb critical_exit(); 92665557Sjasone return (n); 92765557Sjasone} 92865557Sjasone 92965856Sjhbstatic struct witness * 93074912Sjhbenroll(const char *description, struct lock_class *lock_class) 93165557Sjasone{ 93274912Sjhb struct witness *w; 93365557Sjasone 93480747Sjhb if (!witness_watch || witness_dead || panicstr != NULL) 93565557Sjasone return (NULL); 93674912Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 93765557Sjasone return (NULL); 93874912Sjhb mtx_lock_spin(&w_mtx); 93974912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 94074912Sjhb if (strcmp(description, w->w_name) == 0) { 94175362Sjhb w->w_refcount++; 94274912Sjhb mtx_unlock_spin(&w_mtx); 94374912Sjhb if (lock_class != w->w_class) 94474912Sjhb panic( 94574912Sjhb "lock (%s) %s does not match earlier (%s) lock", 94674912Sjhb description, lock_class->lc_name, 94774912Sjhb w->w_class->lc_name); 94865557Sjasone return (w); 94965557Sjasone } 95065557Sjasone } 95174912Sjhb /* 95274912Sjhb * This isn't quite right, as witness_cold is still 0 while we 95374912Sjhb * enroll all the locks initialized before witness_initialize(). 95474912Sjhb */ 95575364Sbp if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 95675364Sbp mtx_unlock_spin(&w_mtx); 95774912Sjhb panic("spin lock %s not in order list", description); 95875364Sbp } 95965557Sjasone if ((w = witness_get()) == NULL) 96065557Sjasone return (NULL); 96174912Sjhb w->w_name = description; 96274912Sjhb w->w_class = lock_class; 96375362Sjhb w->w_refcount = 1; 96474912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 96574912Sjhb if (lock_class->lc_flags & LC_SPINLOCK) 96674912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 96774912Sjhb else if (lock_class->lc_flags & LC_SLEEPLOCK) 96874912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 96975364Sbp else { 97075364Sbp mtx_unlock_spin(&w_mtx); 97174912Sjhb panic("lock class %s is not sleep or spin", 97274912Sjhb lock_class->lc_name); 97375364Sbp } 97474912Sjhb mtx_unlock_spin(&w_mtx); 97565557Sjasone return (w); 97665557Sjasone} 97765557Sjasone 97865557Sjasonestatic int 97965856Sjhbitismychild(struct witness *parent, struct witness *child) 98065557Sjasone{ 98165557Sjasone static int recursed; 98274912Sjhb struct witness_child_list_entry **wcl; 98374912Sjhb struct witness_list *list; 98465557Sjasone 98574912Sjhb MPASS(child != NULL && parent != NULL); 98674912Sjhb if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 98774912Sjhb (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 98874912Sjhb panic( 98974912Sjhb "%s: parent (%s) and child (%s) are not the same lock type", 99074912Sjhb __func__, parent->w_class->lc_name, 99174912Sjhb child->w_class->lc_name); 99274912Sjhb 99365557Sjasone /* 99465557Sjasone * Insert "child" after "parent" 99565557Sjasone */ 99674912Sjhb wcl = &parent->w_children; 99774912Sjhb while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 99874912Sjhb wcl = &(*wcl)->wcl_next; 99974912Sjhb if (*wcl == NULL) { 100074912Sjhb *wcl = witness_child_get(); 100174912Sjhb if (*wcl == NULL) 100265557Sjasone return (1); 100365557Sjasone } 100474912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 100574912Sjhb 100665557Sjasone /* 100774912Sjhb * Now prune whole tree. We look for cases where a lock is now 100874912Sjhb * both a descendant and a direct child of a given lock. In that 100974912Sjhb * case, we want to remove the direct child link from the tree. 101065557Sjasone */ 101165557Sjasone if (recursed) 101265557Sjasone return (0); 101365557Sjasone recursed = 1; 101474912Sjhb if (parent->w_class->lc_flags & LC_SLEEPLOCK) 101574912Sjhb list = &w_sleep; 101674912Sjhb else 101774912Sjhb list = &w_spin; 101874912Sjhb STAILQ_FOREACH(child, list, w_typelist) { 101974912Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 102065557Sjasone if (!isitmychild(parent, child)) 102165557Sjasone continue; 102265557Sjasone removechild(parent, child); 102365557Sjasone if (isitmydescendant(parent, child)) 102465557Sjasone continue; 102565557Sjasone itismychild(parent, child); 102665557Sjasone } 102765557Sjasone } 102865557Sjasone recursed = 0; 102965557Sjasone witness_levelall(); 103065557Sjasone return (0); 103165557Sjasone} 103265557Sjasone 103365557Sjasonestatic void 103465856Sjhbremovechild(struct witness *parent, struct witness *child) 103565557Sjasone{ 103674912Sjhb struct witness_child_list_entry **wcl, *wcl1; 103765557Sjasone int i; 103865557Sjasone 103974912Sjhb for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 104074912Sjhb for (i = 0; i < (*wcl)->wcl_count; i++) 104174912Sjhb if ((*wcl)->wcl_children[i] == child) 104265557Sjasone goto found; 104365557Sjasone return; 104465557Sjasonefound: 104574912Sjhb (*wcl)->wcl_count--; 104674912Sjhb if ((*wcl)->wcl_count > i) 104774912Sjhb (*wcl)->wcl_children[i] = 104874912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count]; 104974912Sjhb MPASS((*wcl)->wcl_children[i] != NULL); 105074912Sjhb if ((*wcl)->wcl_count != 0) 105165557Sjasone return; 105274912Sjhb wcl1 = *wcl; 105374912Sjhb *wcl = wcl1->wcl_next; 105474912Sjhb witness_child_free(wcl1); 105565557Sjasone} 105665557Sjasone 105765557Sjasonestatic int 105865856Sjhbisitmychild(struct witness *parent, struct witness *child) 105965557Sjasone{ 106074912Sjhb struct witness_child_list_entry *wcl; 106165557Sjasone int i; 106265557Sjasone 106374912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 106474912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 106574912Sjhb if (wcl->wcl_children[i] == child) 106665557Sjasone return (1); 106765557Sjasone } 106865557Sjasone } 106965557Sjasone return (0); 107065557Sjasone} 107165557Sjasone 107265557Sjasonestatic int 107365856Sjhbisitmydescendant(struct witness *parent, struct witness *child) 107465557Sjasone{ 107574912Sjhb struct witness_child_list_entry *wcl; 107674912Sjhb int i, j; 107765557Sjasone 107874912Sjhb if (isitmychild(parent, child)) 107974912Sjhb return (1); 108074912Sjhb j = 0; 108174912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 108267352Sjhb MPASS(j < 1000); 108374912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 108474912Sjhb if (isitmydescendant(wcl->wcl_children[i], child)) 108565557Sjasone return (1); 108665557Sjasone } 108774912Sjhb j++; 108865557Sjasone } 108965557Sjasone return (0); 109065557Sjasone} 109165557Sjasone 109265557Sjasonevoid 109365557Sjasonewitness_levelall (void) 109465557Sjasone{ 109574912Sjhb struct witness_list *list; 109665856Sjhb struct witness *w, *w1; 109765557Sjasone 109874912Sjhb /* 109974912Sjhb * First clear all levels. 110074912Sjhb */ 110174912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 110274912Sjhb w->w_level = 0; 110374912Sjhb } 110474912Sjhb 110574912Sjhb /* 110674912Sjhb * Look for locks with no parent and level all their descendants. 110774912Sjhb */ 110874912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 110974912Sjhb /* 111074912Sjhb * This is just an optimization, technically we could get 111174912Sjhb * away just walking the all list each time. 111274912Sjhb */ 111374912Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 111474912Sjhb list = &w_sleep; 111574912Sjhb else 111674912Sjhb list = &w_spin; 111774912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 111865557Sjasone if (isitmychild(w1, w)) 111974912Sjhb goto skip; 112065557Sjasone } 112165557Sjasone witness_leveldescendents(w, 0); 112274912Sjhb skip: 112365557Sjasone } 112465557Sjasone} 112565557Sjasone 112665557Sjasonestatic void 112765856Sjhbwitness_leveldescendents(struct witness *parent, int level) 112865557Sjasone{ 112974912Sjhb struct witness_child_list_entry *wcl; 113065557Sjasone int i; 113165557Sjasone 113265557Sjasone if (parent->w_level < level) 113365557Sjasone parent->w_level = level; 113465557Sjasone level++; 113574912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 113674912Sjhb for (i = 0; i < wcl->wcl_count; i++) 113774912Sjhb witness_leveldescendents(wcl->wcl_children[i], level); 113865557Sjasone} 113965557Sjasone 114065557Sjasonestatic void 114165856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...), 114265856Sjhb struct witness *parent) 114365557Sjasone{ 114474912Sjhb struct witness_child_list_entry *wcl; 114574912Sjhb int i, level; 114665557Sjasone 114774912Sjhb level = parent->w_level; 114874912Sjhb prnt("%-2d", level); 114965557Sjasone for (i = 0; i < level; i++) 115065557Sjasone prnt(" "); 115174912Sjhb prnt("%s", parent->w_name); 115272224Sjhb if (parent->w_file != NULL) 115372224Sjhb prnt(" -- last acquired @ %s:%d\n", parent->w_file, 115472224Sjhb parent->w_line); 115574912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 115674912Sjhb for (i = 0; i < wcl->wcl_count; i++) 115774912Sjhb witness_displaydescendants(prnt, 115874912Sjhb wcl->wcl_children[i]); 115974912Sjhb} 116065557Sjasone 116165557Sjasonestatic int 116265856Sjhbblessed(struct witness *w1, struct witness *w2) 116365557Sjasone{ 116465557Sjasone int i; 116565856Sjhb struct witness_blessed *b; 116665557Sjasone 116765557Sjasone for (i = 0; i < blessed_count; i++) { 116865557Sjasone b = &blessed_list[i]; 116974912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 117074912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 117165557Sjasone return (1); 117265557Sjasone continue; 117365557Sjasone } 117474912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 117574912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 117665557Sjasone return (1); 117765557Sjasone } 117865557Sjasone return (0); 117965557Sjasone} 118065557Sjasone 118165856Sjhbstatic struct witness * 118274912Sjhbwitness_get(void) 118365557Sjasone{ 118465856Sjhb struct witness *w; 118565557Sjasone 118676481Sjhb if (witness_dead) { 118776481Sjhb mtx_unlock_spin(&w_mtx); 118876481Sjhb return (NULL); 118976481Sjhb } 119074912Sjhb if (STAILQ_EMPTY(&w_free)) { 119165557Sjasone witness_dead = 1; 119274912Sjhb mtx_unlock_spin(&w_mtx); 119374912Sjhb printf("%s: witness exhausted\n", __func__); 119465557Sjasone return (NULL); 119565557Sjasone } 119674912Sjhb w = STAILQ_FIRST(&w_free); 119774912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 119865856Sjhb bzero(w, sizeof(*w)); 119965557Sjasone return (w); 120065557Sjasone} 120165557Sjasone 120265557Sjasonestatic void 120365856Sjhbwitness_free(struct witness *w) 120465557Sjasone{ 120574912Sjhb 120674912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 120765557Sjasone} 120865557Sjasone 120974912Sjhbstatic struct witness_child_list_entry * 121074912Sjhbwitness_child_get(void) 121165557Sjasone{ 121274912Sjhb struct witness_child_list_entry *wcl; 121365557Sjasone 121476481Sjhb if (witness_dead) { 121576481Sjhb mtx_unlock_spin(&w_mtx); 121676481Sjhb return (NULL); 121776481Sjhb } 121874912Sjhb wcl = w_child_free; 121974912Sjhb if (wcl == NULL) { 122074912Sjhb witness_dead = 1; 122174912Sjhb mtx_unlock_spin(&w_mtx); 122274912Sjhb printf("%s: witness exhausted\n", __func__); 122374912Sjhb return (NULL); 122465557Sjasone } 122574912Sjhb w_child_free = wcl->wcl_next; 122674912Sjhb bzero(wcl, sizeof(*wcl)); 122774912Sjhb return (wcl); 122874912Sjhb} 122969881Sjake 123074912Sjhbstatic void 123174912Sjhbwitness_child_free(struct witness_child_list_entry *wcl) 123274912Sjhb{ 123374912Sjhb 123474912Sjhb wcl->wcl_next = w_child_free; 123574912Sjhb w_child_free = wcl; 123665557Sjasone} 123765557Sjasone 123874912Sjhbstatic struct lock_list_entry * 123974912Sjhbwitness_lock_list_get(void) 124074912Sjhb{ 124174912Sjhb struct lock_list_entry *lle; 124271709Sjhb 124376481Sjhb if (witness_dead) 124476481Sjhb return (NULL); 124574912Sjhb mtx_lock_spin(&w_mtx); 124674912Sjhb lle = w_lock_list_free; 124774912Sjhb if (lle == NULL) { 124874912Sjhb witness_dead = 1; 124974912Sjhb mtx_unlock_spin(&w_mtx); 125074912Sjhb printf("%s: witness exhausted\n", __func__); 125174912Sjhb return (NULL); 125274912Sjhb } 125374912Sjhb w_lock_list_free = lle->ll_next; 125474912Sjhb mtx_unlock_spin(&w_mtx); 125574912Sjhb bzero(lle, sizeof(*lle)); 125674912Sjhb return (lle); 125774912Sjhb} 125874912Sjhb 125974912Sjhbstatic void 126074912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 126171709Sjhb{ 126271709Sjhb 126374912Sjhb mtx_lock_spin(&w_mtx); 126474912Sjhb lle->ll_next = w_lock_list_free; 126574912Sjhb w_lock_list_free = lle; 126674912Sjhb mtx_unlock_spin(&w_mtx); 126771709Sjhb} 126871709Sjhb 126976272Sjhbstatic struct lock_instance * 127076272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 127176272Sjhb{ 127276272Sjhb struct lock_list_entry *lle; 127376272Sjhb struct lock_instance *instance; 127476272Sjhb int i; 127576272Sjhb 127676272Sjhb for (lle = lock_list; lle != NULL; lle = lle->ll_next) 127776272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 127876272Sjhb instance = &lle->ll_children[i]; 127976272Sjhb if (instance->li_lock == lock) 128076272Sjhb return (instance); 128176272Sjhb } 128276272Sjhb return (NULL); 128376272Sjhb} 128476272Sjhb 128574912Sjhbint 128675273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 128772224Sjhb{ 128875273Sjhb struct lock_list_entry *lle; 128976272Sjhb struct lock_instance *instance; 129074912Sjhb struct lock_object *lock; 129174912Sjhb int i, nheld; 129272224Sjhb 129374912Sjhb nheld = 0; 129474912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 129574912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 129676272Sjhb instance = &lle->ll_children[i]; 129776272Sjhb lock = instance->li_lock; 129893811Sjhb printf("%s %s %s", 129976272Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0 ? 130076272Sjhb "exclusive" : "shared", 130193811Sjhb lock->lo_class->lc_name, lock->lo_name); 130293811Sjhb if (lock->lo_type != lock->lo_name) 130394324Sjhb printf(" (%s)", lock->lo_type); 130494325Sjhb printf(" r = %d (%p) locked @ %s:%d\n", 130594325Sjhb instance->li_flags & LI_RECURSEMASK, lock, 130676272Sjhb instance->li_file, instance->li_line); 130774912Sjhb nheld++; 130874912Sjhb } 130975273Sjhb return (nheld); 131075273Sjhb} 131175273Sjhb 131275273Sjhb/* 131383366Sjulian * Calling this on td != curthread is bad unless we are in ddb. 131475273Sjhb */ 131575273Sjhbint 131683366Sjulianwitness_list(struct thread *td) 131775273Sjhb{ 131875273Sjhb int nheld; 131975273Sjhb 132081489Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 132181489Sjhb#ifdef DDB 132283366Sjulian KASSERT(td == curthread || db_active, 132383366Sjulian ("%s: td != curthread and we aren't in the debugger", __func__)); 132476481Sjhb if (!db_active && witness_dead) 132576481Sjhb return (0); 132681489Sjhb#else 132783366Sjulian KASSERT(td == curthread, ("%s: p != curthread", __func__)); 132881489Sjhb if (witness_dead) 132981489Sjhb return (0); 133081489Sjhb#endif 133183366Sjulian nheld = witness_list_locks(&td->td_sleeplocks); 133275273Sjhb 133374912Sjhb /* 133483366Sjulian * We only handle spinlocks if td == curthread. This is somewhat broken 133583798Sjhb * if td is currently executing on some other CPU and holds spin locks 133675273Sjhb * as we won't display those locks. If we had a MI way of getting 133783798Sjhb * the per-cpu data for a given cpu then we could use 133883798Sjhb * td->td_kse->ke_oncpu to get the list of spinlocks for this thread 133983798Sjhb * and "fix" this. 134091905Sjhb * 134191905Sjhb * That still wouldn't really fix this unless we locked sched_lock 134291905Sjhb * or stopped the other CPU to make sure it wasn't changing the list 134391905Sjhb * out from under us. It is probably best to just not try to handle 134491905Sjhb * threads on other CPU's for now. 134574912Sjhb */ 134683366Sjulian if (td == curthread) { 134775273Sjhb /* 134875273Sjhb * Preemption bad because we need PCPU_PTR(spinlocks) to not 134975273Sjhb * change. 135075273Sjhb */ 135191905Sjhb critical_enter(); 135275273Sjhb nheld += witness_list_locks(PCPU_PTR(spinlocks)); 135391905Sjhb critical_exit(); 135474912Sjhb } 135574912Sjhb return (nheld); 135672224Sjhb} 135771709Sjhb 135865557Sjasonevoid 135974912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 136065557Sjasone{ 136176272Sjhb struct lock_instance *instance; 136271320Sjasone 136382284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 136480747Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 136571352Sjasone return; 136682243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 136782243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 136882243Sjhb lock->lo_class->lc_name, lock->lo_name); 136983366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 137082243Sjhb if (instance == NULL) 137182243Sjhb panic("%s: lock (%s) %s not locked", __func__, 137282243Sjhb lock->lo_class->lc_name, lock->lo_name); 137376272Sjhb *filep = instance->li_file; 137476272Sjhb *linep = instance->li_line; 137565557Sjasone} 137665557Sjasone 137765557Sjasonevoid 137874912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 137965557Sjasone{ 138076272Sjhb struct lock_instance *instance; 138171320Sjasone 138282284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 138380747Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 138471352Sjasone return; 138582243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 138682243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 138782243Sjhb lock->lo_class->lc_name, lock->lo_name); 138883366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 138982243Sjhb if (instance == NULL) 139082243Sjhb panic("%s: lock (%s) %s not locked", __func__, 139182243Sjhb lock->lo_class->lc_name, lock->lo_name); 139274912Sjhb lock->lo_witness->w_file = file; 139374912Sjhb lock->lo_witness->w_line = line; 139476272Sjhb instance->li_file = file; 139576272Sjhb instance->li_line = line; 139665557Sjasone} 139765557Sjasone 139878871Sjhbvoid 139978871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 140078871Sjhb{ 140178871Sjhb#ifdef INVARIANT_SUPPORT 140278871Sjhb struct lock_instance *instance; 140378871Sjhb 140480747Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 140578941Sjhb return; 140678871Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 140783366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 140878871Sjhb else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 140978871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 141086422Sjhb else { 141178871Sjhb panic("Lock (%s) %s is not sleep or spin!", 141278871Sjhb lock->lo_class->lc_name, lock->lo_name); 141386422Sjhb return; 141486422Sjhb } 141578871Sjhb switch (flags) { 141678871Sjhb case LA_UNLOCKED: 141778871Sjhb if (instance != NULL) 141878871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 141978871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 142078871Sjhb break; 142178871Sjhb case LA_LOCKED: 142278871Sjhb case LA_LOCKED | LA_RECURSED: 142378871Sjhb case LA_LOCKED | LA_NOTRECURSED: 142478871Sjhb case LA_SLOCKED: 142578871Sjhb case LA_SLOCKED | LA_RECURSED: 142678871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 142778871Sjhb case LA_XLOCKED: 142878871Sjhb case LA_XLOCKED | LA_RECURSED: 142978871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 143086422Sjhb if (instance == NULL) { 143178871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 143278871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 143386422Sjhb break; 143486422Sjhb } 143578871Sjhb if ((flags & LA_XLOCKED) != 0 && 143678871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 143778871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 143878871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 143978871Sjhb if ((flags & LA_SLOCKED) != 0 && 144078871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 144178871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 144278871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 144378871Sjhb if ((flags & LA_RECURSED) != 0 && 144478871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 144578871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 144678871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 144778871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 144878871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 144978871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 145078871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 145178871Sjhb break; 145278871Sjhb default: 145378871Sjhb panic("Invalid lock assertion at %s:%d.", file, line); 145478871Sjhb 145578871Sjhb } 145678871Sjhb#endif /* INVARIANT_SUPPORT */ 145778871Sjhb} 145878871Sjhb 145974912Sjhb#ifdef DDB 146074912Sjhb 146174930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 146274912Sjhb{ 146383366Sjulian struct thread *td; 146483366Sjulian pid_t pid; 146575273Sjhb struct proc *p; 146674912Sjhb 146775273Sjhb if (have_addr) { 146875273Sjhb pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 146975273Sjhb ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 147075273Sjhb ((addr >> 16) % 16) * 10000; 147175273Sjhb /* sx_slock(&allproc_lock); */ 147283366Sjulian FOREACH_PROC_IN_SYSTEM(p) { 147375273Sjhb if (p->p_pid == pid) 147475273Sjhb break; 147575273Sjhb } 147675273Sjhb /* sx_sunlock(&allproc_lock); */ 147775273Sjhb if (p == NULL) { 147875273Sjhb db_printf("pid %d not found\n", pid); 147975273Sjhb return; 148075273Sjhb } 148190361Sjulian FOREACH_THREAD_IN_PROC(p, td) { 148290361Sjulian witness_list(td); 148390361Sjulian } 148483366Sjulian } else { 148583366Sjulian td = curthread; 148690361Sjulian witness_list(td); 148783366Sjulian } 148874912Sjhb} 148974912Sjhb 149074912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 149174912Sjhb{ 149274912Sjhb 149374912Sjhb witness_display(db_printf); 149474912Sjhb} 149574912Sjhb#endif 1496