subr_witness.c revision 103091
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 103091 2002-09-08 04:45:16Z jake $ 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_displaydescendants(void(*)(const char *fmt, ...), 12974912Sjhb struct witness *); 13074912Sjhbstatic void witness_leveldescendents(struct witness *parent, int level); 13174912Sjhbstatic void witness_levelall(void); 13274912Sjhbstatic struct witness *witness_get(void); 13374912Sjhbstatic void witness_free(struct witness *m); 13474912Sjhbstatic struct witness_child_list_entry *witness_child_get(void); 13574912Sjhbstatic void witness_child_free(struct witness_child_list_entry *wcl); 13674912Sjhbstatic struct lock_list_entry *witness_lock_list_get(void); 13774912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 13876272Sjhbstatic struct lock_instance *find_instance(struct lock_list_entry *lock_list, 13976272Sjhb struct lock_object *lock); 140100011Smp#if defined(DDB) 141100011Smpstatic void witness_display_list(void(*prnt)(const char *fmt, ...), 142100011Smp struct witness_list *list); 143100011Smpstatic void witness_display(void(*)(const char *fmt, ...)); 144100011Smp#endif 14572200Sbmilekic 14674912SjhbMALLOC_DEFINE(M_WITNESS, "witness", "witness structure"); 14772200Sbmilekic 14877843Speterstatic int witness_watch = 1; 14977900SpeterTUNABLE_INT("debug.witness_watch", &witness_watch); 15074912SjhbSYSCTL_INT(_debug, OID_AUTO, witness_watch, CTLFLAG_RD, &witness_watch, 0, ""); 15171352Sjasone 15267352Sjhb#ifdef DDB 15372200Sbmilekic/* 15467676Sjhb * When DDB is enabled and witness_ddb is set to 1, it will cause the system to 15565557Sjasone * drop into kdebug() when: 15665557Sjasone * - a lock heirarchy violation occurs 15765557Sjasone * - locks are held when going to sleep. 15865557Sjasone */ 15967676Sjhb#ifdef WITNESS_DDB 16077843Speterint witness_ddb = 1; 16167676Sjhb#else 16277843Speterint witness_ddb = 0; 16365557Sjasone#endif 16477900SpeterTUNABLE_INT("debug.witness_ddb", &witness_ddb); 16567676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_ddb, CTLFLAG_RW, &witness_ddb, 0, ""); 16667676Sjhb#endif /* DDB */ 16765557Sjasone 16867676Sjhb#ifdef WITNESS_SKIPSPIN 16977843Speterint witness_skipspin = 1; 17067676Sjhb#else 17177843Speterint witness_skipspin = 0; 17265557Sjasone#endif 17377900SpeterTUNABLE_INT("debug.witness_skipspin", &witness_skipspin); 17467676SjhbSYSCTL_INT(_debug, OID_AUTO, witness_skipspin, CTLFLAG_RD, &witness_skipspin, 0, 17567676Sjhb ""); 17665557Sjasone 17774912Sjhbstatic struct mtx w_mtx; 17874912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 17974912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 18074912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 18174912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 18274912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL; 18374912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 18474912Sjhbstatic int witness_dead; /* fatal error, probably no memory */ 18565557Sjasone 18674912Sjhbstatic struct witness w_data[WITNESS_COUNT]; 18774912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 18874912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 18965557Sjasone 19074912Sjhbstatic struct witness_order_list_entry order_lists[] = { 19174912Sjhb { "Giant", &lock_class_mtx_sleep }, 19274912Sjhb { "proctree", &lock_class_sx }, 19374912Sjhb { "allproc", &lock_class_sx }, 19496122Salfred { "sigio lock", &lock_class_mtx_sleep }, 19591140Stanimura { "process group", &lock_class_mtx_sleep }, 19674912Sjhb { "process lock", &lock_class_mtx_sleep }, 19791140Stanimura { "session", &lock_class_mtx_sleep }, 19874912Sjhb { "uidinfo hash", &lock_class_mtx_sleep }, 19974912Sjhb { "uidinfo struct", &lock_class_mtx_sleep }, 20074912Sjhb { NULL, NULL }, 20175464Sjhb /* 20275464Sjhb * spin locks 20375464Sjhb */ 20484331Sjhb#ifdef SMP 20584331Sjhb { "ap boot", &lock_class_mtx_spin }, 20684331Sjhb#ifdef __i386__ 20774912Sjhb { "com", &lock_class_mtx_spin }, 20872224Sjhb#endif 20984331Sjhb#endif 21074912Sjhb { "sio", &lock_class_mtx_spin }, 21172224Sjhb#ifdef __i386__ 21274912Sjhb { "cy", &lock_class_mtx_spin }, 21372224Sjhb#endif 214103091Sjake { "sabtty", &lock_class_mtx_spin }, 21574912Sjhb { "ng_node", &lock_class_mtx_spin }, 21674912Sjhb { "ng_worklist", &lock_class_mtx_spin }, 21774912Sjhb { "ithread table lock", &lock_class_mtx_spin }, 21874912Sjhb { "sched lock", &lock_class_mtx_spin }, 21974912Sjhb { "callout", &lock_class_mtx_spin }, 22065557Sjasone /* 22165557Sjasone * leaf locks 22265557Sjasone */ 22390278Sjhb { "allpmaps", &lock_class_mtx_spin }, 22495823Salc { "vm page buckets mutex", &lock_class_mtx_spin }, 22599416Salc { "vm page queue free mutex", &lock_class_mtx_spin }, 22688322Sjhb { "icu", &lock_class_mtx_spin }, 22772224Sjhb#ifdef SMP 22874912Sjhb { "smp rendezvous", &lock_class_mtx_spin }, 22999862Speter#if defined(__i386__) && defined(APIC_IO) 23099862Speter { "tlb", &lock_class_mtx_spin }, 23172224Sjhb#endif 23299862Speter#endif 23378785Sjhb { "clk", &lock_class_mtx_spin }, 23495473Sdes { "mutex profiling lock", &lock_class_mtx_spin }, 23599072Sjulian { "zombie_thread_lock", &lock_class_mtx_spin }, 23674912Sjhb { NULL, NULL }, 23774912Sjhb { NULL, NULL } 23865557Sjasone}; 23965557Sjasone 24065557Sjasone/* 24165557Sjasone * Pairs of locks which have been blessed 24265557Sjasone * Don't complain about order problems with blessed locks 24365557Sjasone */ 24465856Sjhbstatic struct witness_blessed blessed_list[] = { 24565557Sjasone}; 24672200Sbmilekicstatic int blessed_count = 24772200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 24865557Sjasone 24974912Sjhb/* 25074912Sjhb * List of all locks in the system. 25174912Sjhb */ 25297963SjhbTAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 25374912Sjhb 25474912Sjhbstatic struct mtx all_mtx = { 25574912Sjhb { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 25674912Sjhb "All locks list", /* mtx_object.lo_name */ 25793811Sjhb "All locks list", /* mtx_object.lo_type */ 25874912Sjhb LO_INITIALIZED, /* mtx_object.lo_flags */ 25997963Sjhb { NULL, NULL }, /* mtx_object.lo_list */ 26074912Sjhb NULL }, /* mtx_object.lo_witness */ 26174912Sjhb MTX_UNOWNED, 0, /* mtx_lock, mtx_recurse */ 26274912Sjhb TAILQ_HEAD_INITIALIZER(all_mtx.mtx_blocked), 26374912Sjhb { NULL, NULL } /* mtx_contested */ 26474912Sjhb}; 26574912Sjhb 26674912Sjhb/* 26774912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 26874912Sjhb */ 26974912Sjhbstatic int witness_cold = 1; 27074912Sjhb 27174912Sjhb/* 27274912Sjhb * Global variables for book keeping. 27374912Sjhb */ 27474912Sjhbstatic int lock_cur_cnt; 27574912Sjhbstatic int lock_max_cnt; 27674912Sjhb 27774912Sjhb/* 27874912Sjhb * The WITNESS-enabled diagnostic code. 27974912Sjhb */ 28071352Sjasonestatic void 28174912Sjhbwitness_initialize(void *dummy __unused) 28265557Sjasone{ 28374912Sjhb struct lock_object *lock; 28474912Sjhb struct witness_order_list_entry *order; 28574912Sjhb struct witness *w, *w1; 28674912Sjhb int i; 28765557Sjasone 28874912Sjhb /* 28974912Sjhb * We have to release Giant before initializing its witness 29074912Sjhb * structure so that WITNESS doesn't get confused. 29174912Sjhb */ 29274912Sjhb mtx_unlock(&Giant); 29374912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 29474912Sjhb 29587593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 29697963Sjhb TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 29793811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 29893811Sjhb MTX_NOWITNESS); 29974912Sjhb for (i = 0; i < WITNESS_COUNT; i++) 30074912Sjhb witness_free(&w_data[i]); 30174912Sjhb for (i = 0; i < WITNESS_CHILDCOUNT; i++) 30274912Sjhb witness_child_free(&w_childdata[i]); 30374912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 30474912Sjhb witness_lock_list_free(&w_locklistdata[i]); 30574912Sjhb 30674912Sjhb /* First add in all the specified order lists. */ 30774912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 30874912Sjhb w = enroll(order->w_name, order->w_class); 30975569Sjhb if (w == NULL) 31075569Sjhb continue; 31174912Sjhb w->w_file = "order list"; 31274912Sjhb for (order++; order->w_name != NULL; order++) { 31374912Sjhb w1 = enroll(order->w_name, order->w_class); 31475569Sjhb if (w1 == NULL) 31575569Sjhb continue; 31674912Sjhb w1->w_file = "order list"; 31774912Sjhb itismychild(w, w1); 31874912Sjhb w = w1; 31965557Sjasone } 32065557Sjasone } 32165557Sjasone 32274912Sjhb /* Iterate through all locks and add them to witness. */ 32374912Sjhb mtx_lock(&all_mtx); 32497963Sjhb TAILQ_FOREACH(lock, &all_locks, lo_list) { 32574912Sjhb if (lock->lo_flags & LO_WITNESS) 32693811Sjhb lock->lo_witness = enroll(lock->lo_type, 32774912Sjhb lock->lo_class); 32874912Sjhb else 32974912Sjhb lock->lo_witness = NULL; 33074912Sjhb } 33174912Sjhb mtx_unlock(&all_mtx); 33274912Sjhb 33374912Sjhb /* Mark the witness code as being ready for use. */ 33474912Sjhb atomic_store_rel_int(&witness_cold, 0); 33574912Sjhb 33674912Sjhb mtx_lock(&Giant); 33765557Sjasone} 33874912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 33965557Sjasone 34074912Sjhbvoid 34174912Sjhbwitness_init(struct lock_object *lock) 34274912Sjhb{ 34374912Sjhb struct lock_class *class; 34474912Sjhb 34574912Sjhb class = lock->lo_class; 34674912Sjhb if (lock->lo_flags & LO_INITIALIZED) 34782284Sjhb panic("%s: lock (%s) %s is already initialized", __func__, 34874912Sjhb class->lc_name, lock->lo_name); 34974912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 35074912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 35182284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 35274912Sjhb class->lc_name, lock->lo_name); 35374912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 35474912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 35582284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 35674912Sjhb class->lc_name, lock->lo_name); 35782244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 35882244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 35982284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 36082244Sjhb class->lc_name, lock->lo_name); 36182244Sjhb 36274912Sjhb mtx_lock(&all_mtx); 36397963Sjhb TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 36474912Sjhb lock->lo_flags |= LO_INITIALIZED; 36574912Sjhb lock_cur_cnt++; 36674912Sjhb if (lock_cur_cnt > lock_max_cnt) 36774912Sjhb lock_max_cnt = lock_cur_cnt; 36874912Sjhb mtx_unlock(&all_mtx); 36980747Sjhb if (!witness_cold && !witness_dead && panicstr == NULL && 37074912Sjhb (lock->lo_flags & LO_WITNESS) != 0) 37193811Sjhb lock->lo_witness = enroll(lock->lo_type, class); 37274912Sjhb else 37374912Sjhb lock->lo_witness = NULL; 37474912Sjhb} 37574912Sjhb 37674912Sjhbvoid 37774912Sjhbwitness_destroy(struct lock_object *lock) 37874912Sjhb{ 37975362Sjhb struct witness *w; 38074912Sjhb 38174912Sjhb if (witness_cold) 38274912Sjhb panic("lock (%s) %s destroyed while witness_cold", 38374912Sjhb lock->lo_class->lc_name, lock->lo_name); 38474912Sjhb if ((lock->lo_flags & LO_INITIALIZED) == 0) 38582284Sjhb panic("%s: lock (%s) %s is not initialized", __func__, 38674912Sjhb lock->lo_class->lc_name, lock->lo_name); 38774912Sjhb 38876272Sjhb /* XXX: need to verify that no one holds the lock */ 38975362Sjhb w = lock->lo_witness; 39075362Sjhb if (w != NULL) { 39175362Sjhb mtx_lock_spin(&w_mtx); 39297948Sjhb MPASS(w->w_refcount > 0); 39375362Sjhb w->w_refcount--; 39475362Sjhb mtx_unlock_spin(&w_mtx); 39575362Sjhb } 39675362Sjhb 39774912Sjhb mtx_lock(&all_mtx); 39874912Sjhb lock_cur_cnt--; 39997963Sjhb TAILQ_REMOVE(&all_locks, lock, lo_list); 40080055Sjhb lock->lo_flags &= ~LO_INITIALIZED; 40174912Sjhb mtx_unlock(&all_mtx); 40274912Sjhb} 40374912Sjhb 404100011Smp#if defined(DDB) 40571352Sjasonestatic void 40674912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...), 40774912Sjhb struct witness_list *list) 40871352Sjasone{ 40971352Sjasone struct witness *w, *w1; 41074912Sjhb int found; 41171352Sjasone 41274912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 41374912Sjhb if (w->w_file == NULL) 41471352Sjasone continue; 41574912Sjhb found = 0; 41674912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 41774912Sjhb if (isitmychild(w1, w)) { 41874912Sjhb found++; 41971352Sjasone break; 42074912Sjhb } 42171352Sjasone } 42274912Sjhb if (found) 42371352Sjasone continue; 42471352Sjasone /* 42571352Sjasone * This lock has no anscestors, display its descendants. 42671352Sjasone */ 42771352Sjasone witness_displaydescendants(prnt, w); 42871352Sjasone } 42974912Sjhb} 43072224Sjhb 43174912Sjhbstatic void 43274912Sjhbwitness_display(void(*prnt)(const char *fmt, ...)) 43374912Sjhb{ 43474912Sjhb struct witness *w; 43574912Sjhb 43682284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 43774912Sjhb witness_levelall(); 43874912Sjhb 43972224Sjhb /* 44074930Sjhb * First, handle sleep locks which have been acquired at least 44174912Sjhb * once. 44274912Sjhb */ 44374912Sjhb prnt("Sleep locks:\n"); 44474912Sjhb witness_display_list(prnt, &w_sleep); 44574912Sjhb 44674912Sjhb /* 44774930Sjhb * Now do spin locks which have been acquired at least once. 44872224Sjhb */ 44974912Sjhb prnt("\nSpin locks:\n"); 45074912Sjhb witness_display_list(prnt, &w_spin); 45172224Sjhb 45272224Sjhb /* 45374930Sjhb * Finally, any locks which have not been acquired yet. 45472224Sjhb */ 45574912Sjhb prnt("\nLocks which were never acquired:\n"); 45674912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 45797948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 45871352Sjasone continue; 45974912Sjhb prnt("%s\n", w->w_name); 46071352Sjasone } 46171352Sjasone} 462100011Smp#endif 46371352Sjasone 46465557Sjasonevoid 46574912Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 46665557Sjasone{ 46774912Sjhb struct lock_list_entry **lock_list, *lle; 46876272Sjhb struct lock_instance *lock1, *lock2; 46974912Sjhb struct lock_class *class; 47065856Sjhb struct witness *w, *w1; 47183366Sjulian struct thread *td; 47274912Sjhb int i, j; 47367676Sjhb#ifdef DDB 47467676Sjhb int go_into_ddb = 0; 47567676Sjhb#endif /* DDB */ 47665557Sjasone 47774912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 47880747Sjhb panicstr != NULL) 47971320Sjasone return; 48074912Sjhb w = lock->lo_witness; 48174912Sjhb class = lock->lo_class; 48283366Sjulian td = curthread; 48365557Sjasone 48474912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 48593676Sjhb /* 48693676Sjhb * Since spin locks include a critical section, this check 48793676Sjhb * impliclty enforces a lock order of all sleep locks before 48893676Sjhb * all spin locks. 48993676Sjhb */ 49088899Sjhb if (td->td_critnest != 0 && (flags & LOP_TRYLOCK) == 0) 49174912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 49274912Sjhb class->lc_name, lock->lo_name, file, line); 49383366Sjulian lock_list = &td->td_sleeplocks; 49488899Sjhb } else 49588899Sjhb lock_list = PCPU_PTR(spinlocks); 49665557Sjasone 49776772Sjhb /* 49876772Sjhb * Try locks do not block if they fail to acquire the lock, thus 49976772Sjhb * there is no danger of deadlocks or of switching while holding a 50076772Sjhb * spin lock if we acquire a lock via a try operation. 50176772Sjhb */ 50274912Sjhb if (flags & LOP_TRYLOCK) 50365557Sjasone goto out; 50465557Sjasone 50565557Sjasone /* 50674912Sjhb * Is this the first lock acquired? If so, then no order checking 50774912Sjhb * is needed. 50865557Sjasone */ 50974912Sjhb if (*lock_list == NULL) 51065557Sjasone goto out; 51165557Sjasone 51274912Sjhb /* 51376272Sjhb * Check to see if we are recursing on a lock we already own. 51476272Sjhb */ 51576272Sjhb lock1 = find_instance(*lock_list, lock); 51676272Sjhb if (lock1 != NULL) { 51776272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 51876272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 51976272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 52076272Sjhb class->lc_name, lock->lo_name, file, line); 52176272Sjhb printf("while exclusively locked from %s:%d\n", 52276272Sjhb lock1->li_file, lock1->li_line); 52376272Sjhb panic("share->excl"); 52476272Sjhb } 52576272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 52676272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 52776272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 52876272Sjhb class->lc_name, lock->lo_name, file, line); 52976272Sjhb printf("while share locked from %s:%d\n", 53076272Sjhb lock1->li_file, lock1->li_line); 53176272Sjhb panic("excl->share"); 53276272Sjhb } 53376272Sjhb lock1->li_flags++; 53476272Sjhb if ((lock->lo_flags & LO_RECURSABLE) == 0) { 53576272Sjhb printf( 53676272Sjhb "recursed on non-recursive lock (%s) %s @ %s:%d\n", 53776272Sjhb class->lc_name, lock->lo_name, file, line); 53876272Sjhb printf("first acquired @ %s:%d\n", lock1->li_file, 53976272Sjhb lock1->li_line); 54076272Sjhb panic("recurse"); 54176272Sjhb } 54287593Sobrien CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 54384680Sjhb td->td_proc->p_pid, lock->lo_name, 54478785Sjhb lock1->li_flags & LI_RECURSEMASK); 54576272Sjhb lock1->li_file = file; 54676272Sjhb lock1->li_line = line; 54776272Sjhb return; 54876272Sjhb } 54976272Sjhb 55076272Sjhb /* 55174912Sjhb * Check for duplicate locks of the same type. Note that we only 55274912Sjhb * have to check for this on the last lock we just acquired. Any 55374912Sjhb * other cases will be caught as lock order violations. 55474912Sjhb */ 55576272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 55676272Sjhb w1 = lock1->li_lock->lo_witness; 55774912Sjhb if (w1 == w) { 55893273Sjeff if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK)) 55965557Sjasone goto out; 56065557Sjasone w->w_same_squawked = 1; 56175755Sjhb printf("acquiring duplicate lock of same type: \"%s\"\n", 56293811Sjhb lock->lo_type); 56393811Sjhb printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 56493811Sjhb lock1->li_file, lock1->li_line); 56593811Sjhb printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 56667676Sjhb#ifdef DDB 56767676Sjhb go_into_ddb = 1; 56867676Sjhb#endif /* DDB */ 56965557Sjasone goto out; 57065557Sjasone } 57165557Sjasone MPASS(!mtx_owned(&w_mtx)); 57274912Sjhb mtx_lock_spin(&w_mtx); 57365557Sjasone /* 57465557Sjasone * If we have a known higher number just say ok 57565557Sjasone */ 57665557Sjasone if (witness_watch > 1 && w->w_level > w1->w_level) { 57774912Sjhb mtx_unlock_spin(&w_mtx); 57865557Sjasone goto out; 57965557Sjasone } 58074912Sjhb if (isitmydescendant(w1, w)) { 58174912Sjhb mtx_unlock_spin(&w_mtx); 58265557Sjasone goto out; 58365557Sjasone } 58474912Sjhb for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 58574912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 58665557Sjasone 58774912Sjhb MPASS(j < WITNESS_COUNT); 58876272Sjhb lock1 = &lle->ll_children[i]; 58976272Sjhb w1 = lock1->li_lock->lo_witness; 59074912Sjhb 59174912Sjhb /* 59274912Sjhb * If this lock doesn't undergo witness checking, 59374912Sjhb * then skip it. 59474912Sjhb */ 59574912Sjhb if (w1 == NULL) { 59676272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 59774912Sjhb ("lock missing witness structure")); 59874912Sjhb continue; 59974912Sjhb } 60076272Sjhb /* 60176272Sjhb * If we are locking Giant and we slept with this 60276272Sjhb * lock, then skip it. 60376272Sjhb */ 60476272Sjhb if ((lock1->li_flags & LI_SLEPT) != 0 && 60576272Sjhb lock == &Giant.mtx_object) 60676272Sjhb continue; 60793690Sjhb /* 60893690Sjhb * If we are locking a sleepable lock and this lock 60993690Sjhb * isn't sleepable and isn't Giant, we want to treat 61093690Sjhb * it as a lock order violation to enfore a general 61193690Sjhb * lock order of sleepable locks before non-sleepable 61293690Sjhb * locks. Thus, we only bother checking the lock 61393690Sjhb * order hierarchy if we pass the initial test. 61493690Sjhb */ 61593690Sjhb if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 61693690Sjhb ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 61793690Sjhb lock1->li_lock != &Giant.mtx_object)) && 61893690Sjhb !isitmydescendant(w, w1)) 61974912Sjhb continue; 62074912Sjhb /* 62174912Sjhb * We have a lock order violation, check to see if it 62274912Sjhb * is allowed or has already been yelled about. 62374912Sjhb */ 62474912Sjhb mtx_unlock_spin(&w_mtx); 62565557Sjasone if (blessed(w, w1)) 62665557Sjasone goto out; 62776272Sjhb if (lock1->li_lock == &Giant.mtx_object) { 62865557Sjasone if (w1->w_Giant_squawked) 62965557Sjasone goto out; 63065557Sjasone else 63165557Sjasone w1->w_Giant_squawked = 1; 63265557Sjasone } else { 63365557Sjasone if (w1->w_other_squawked) 63465557Sjasone goto out; 63565557Sjasone else 63665557Sjasone w1->w_other_squawked = 1; 63765557Sjasone } 63874912Sjhb /* 63974912Sjhb * Ok, yell about it. 64074912Sjhb */ 64165557Sjasone printf("lock order reversal\n"); 64274912Sjhb /* 64374912Sjhb * Try to locate an earlier lock with 64474912Sjhb * witness w in our list. 64574912Sjhb */ 64674912Sjhb do { 64776272Sjhb lock2 = &lle->ll_children[i]; 64876272Sjhb MPASS(lock2->li_lock != NULL); 64976272Sjhb if (lock2->li_lock->lo_witness == w) 65074912Sjhb break; 65174912Sjhb i--; 65274912Sjhb if (i == 0 && lle->ll_next != NULL) { 65374912Sjhb lle = lle->ll_next; 65474912Sjhb i = lle->ll_count - 1; 65574912Sjhb MPASS(i != 0); 65674912Sjhb } 65774912Sjhb } while (i >= 0); 65876272Sjhb if (i < 0) { 65993811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 66093811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 66193811Sjhb lock1->li_lock->lo_type, lock1->li_file, 66276272Sjhb lock1->li_line); 66393811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 66493811Sjhb lock->lo_name, lock->lo_type, file, line); 66576272Sjhb } else { 66693811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 66793811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 66893811Sjhb lock2->li_lock->lo_type, lock2->li_file, 66976272Sjhb lock2->li_line); 67093811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 67193811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 67293811Sjhb lock1->li_lock->lo_type, lock1->li_file, 67376272Sjhb lock1->li_line); 67493811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 67593811Sjhb lock->lo_name, lock->lo_type, file, line); 67676272Sjhb } 67767676Sjhb#ifdef DDB 67867676Sjhb go_into_ddb = 1; 67967676Sjhb#endif /* DDB */ 68065557Sjasone goto out; 68165557Sjasone } 68265557Sjasone } 68376272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 68478871Sjhb /* 68578871Sjhb * Don't build a new relationship if we are locking Giant just 68678871Sjhb * after waking up and the previous lock in the list was acquired 68778871Sjhb * prior to blocking. 68878871Sjhb */ 68978871Sjhb if (lock == &Giant.mtx_object && (lock1->li_flags & LI_SLEPT) != 0) 69074912Sjhb mtx_unlock_spin(&w_mtx); 69178871Sjhb else { 69287593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 69393811Sjhb lock->lo_type, lock1->li_lock->lo_type); 69478871Sjhb if (!itismychild(lock1->li_lock->lo_witness, w)) 69578871Sjhb mtx_unlock_spin(&w_mtx); 69678871Sjhb } 69765557Sjasone 69865557Sjasoneout: 69967676Sjhb#ifdef DDB 70067676Sjhb if (witness_ddb && go_into_ddb) 70175711Sjhb Debugger(__func__); 70267676Sjhb#endif /* DDB */ 70365557Sjasone w->w_file = file; 70465557Sjasone w->w_line = line; 70574912Sjhb 70674912Sjhb lle = *lock_list; 70776272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 70878785Sjhb lle = witness_lock_list_get(); 70978785Sjhb if (lle == NULL) 71065557Sjasone return; 71178785Sjhb lle->ll_next = *lock_list; 71287593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 71384680Sjhb td->td_proc->p_pid, lle); 71478785Sjhb *lock_list = lle; 71565557Sjasone } 71676272Sjhb lock1 = &lle->ll_children[lle->ll_count++]; 71776272Sjhb lock1->li_lock = lock; 71876272Sjhb lock1->li_line = line; 71976272Sjhb lock1->li_file = file; 72076272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 72176272Sjhb lock1->li_flags = LI_EXCLUSIVE; 72276272Sjhb else 72376272Sjhb lock1->li_flags = 0; 72487593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 72584680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 72665557Sjasone} 72765557Sjasone 72865557Sjasonevoid 72982244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 73082244Sjhb{ 73182244Sjhb struct lock_instance *instance; 73282244Sjhb struct lock_class *class; 73382244Sjhb 73482284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 73582244Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 73682244Sjhb return; 73782244Sjhb class = lock->lo_class; 73882244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 73982244Sjhb panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 74082244Sjhb class->lc_name, lock->lo_name, file, line); 74182244Sjhb if ((flags & LOP_TRYLOCK) == 0) 74282244Sjhb panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 74382244Sjhb lock->lo_name, file, line); 74482244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 74582244Sjhb panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 74682244Sjhb class->lc_name, lock->lo_name, file, line); 74783366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 74882244Sjhb if (instance == NULL) 74982244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 75082244Sjhb class->lc_name, lock->lo_name, file, line); 75182244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0) 75282244Sjhb panic("upgrade of exclusive lock (%s) %s @ %s:%d", 75382244Sjhb class->lc_name, lock->lo_name, file, line); 75482244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 75582244Sjhb panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 75682244Sjhb class->lc_name, lock->lo_name, 75782244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 75882244Sjhb instance->li_flags |= LI_EXCLUSIVE; 75982244Sjhb} 76082244Sjhb 76182244Sjhbvoid 76282244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 76382244Sjhb int line) 76482244Sjhb{ 76582244Sjhb struct lock_instance *instance; 76682244Sjhb struct lock_class *class; 76782244Sjhb 76882284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 76982244Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 77082244Sjhb return; 77182244Sjhb class = lock->lo_class; 77282244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 77382244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 77482244Sjhb class->lc_name, lock->lo_name, file, line); 77582244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 77682244Sjhb panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 77782244Sjhb class->lc_name, lock->lo_name, file, line); 77883366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 77982244Sjhb if (instance == NULL) 78082244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 78182244Sjhb class->lc_name, lock->lo_name, file, line); 78282244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0) 78382244Sjhb panic("downgrade of shared lock (%s) %s @ %s:%d", 78482244Sjhb class->lc_name, lock->lo_name, file, line); 78582244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 78682244Sjhb panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 78782244Sjhb class->lc_name, lock->lo_name, 78882244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 78982244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 79082244Sjhb} 79182244Sjhb 79282244Sjhbvoid 79374912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 79465557Sjasone{ 79574912Sjhb struct lock_list_entry **lock_list, *lle; 79676272Sjhb struct lock_instance *instance; 79774912Sjhb struct lock_class *class; 79883366Sjulian struct thread *td; 79992858Simp register_t s; 80074912Sjhb int i, j; 80165557Sjasone 80274912Sjhb if (witness_cold || witness_dead || lock->lo_witness == NULL || 80380747Sjhb panicstr != NULL) 80471352Sjasone return; 80583366Sjulian td = curthread; 80674912Sjhb class = lock->lo_class; 80776272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 80883366Sjulian lock_list = &td->td_sleeplocks; 80976272Sjhb else 81074912Sjhb lock_list = PCPU_PTR(spinlocks); 81174912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 81276272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 81376272Sjhb instance = &(*lock_list)->ll_children[i]; 81476272Sjhb if (instance->li_lock == lock) { 81576272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 81676272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 81776272Sjhb printf( 81876272Sjhb "shared unlock of (%s) %s @ %s:%d\n", 81976272Sjhb class->lc_name, lock->lo_name, 82076272Sjhb file, line); 82176272Sjhb printf( 82276272Sjhb "while exclusively locked from %s:%d\n", 82376272Sjhb instance->li_file, 82476272Sjhb instance->li_line); 82576272Sjhb panic("excl->ushare"); 82676272Sjhb } 82776272Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 82876272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 82976272Sjhb printf( 83076272Sjhb "exclusive unlock of (%s) %s @ %s:%d\n", 83176272Sjhb class->lc_name, lock->lo_name, 83276272Sjhb file, line); 83376272Sjhb printf( 83476272Sjhb "while share locked from %s:%d\n", 83576272Sjhb instance->li_file, 83676272Sjhb instance->li_line); 83776272Sjhb panic("share->uexcl"); 83876272Sjhb } 83976272Sjhb /* If we are recursed, unrecurse. */ 84076272Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 84187593Sobrien CTR4(KTR_WITNESS, 84287593Sobrien "%s: pid %d unrecursed on %s r=%d", __func__, 84384680Sjhb td->td_proc->p_pid, 84478785Sjhb instance->li_lock->lo_name, 84578785Sjhb instance->li_flags); 84676272Sjhb instance->li_flags--; 84788900Sjhb return; 84876272Sjhb } 84992858Simp s = intr_disable(); 85087593Sobrien CTR4(KTR_WITNESS, 85187593Sobrien "%s: pid %d removed %s from lle[%d]", __func__, 85284680Sjhb td->td_proc->p_pid, 85384680Sjhb instance->li_lock->lo_name, 85478785Sjhb (*lock_list)->ll_count - 1); 85597014Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 85674912Sjhb (*lock_list)->ll_children[j] = 85774912Sjhb (*lock_list)->ll_children[j + 1]; 85897014Sjhb (*lock_list)->ll_count--; 85992858Simp intr_restore(s); 86074912Sjhb if ((*lock_list)->ll_count == 0) { 86174912Sjhb lle = *lock_list; 86274912Sjhb *lock_list = lle->ll_next; 86387593Sobrien CTR3(KTR_WITNESS, 86487593Sobrien "%s: pid %d removed lle %p", __func__, 86584680Sjhb td->td_proc->p_pid, lle); 86674912Sjhb witness_lock_list_free(lle); 86774912Sjhb } 86888900Sjhb return; 86974912Sjhb } 87076272Sjhb } 87176272Sjhb panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 87276272Sjhb file, line); 87365557Sjasone} 87465557Sjasone 87574912Sjhb/* 87674912Sjhb * Warn if any held locks are not sleepable. Note that Giant and the lock 87774912Sjhb * passed in are both special cases since they are both released during the 87883798Sjhb * sleep process and aren't actually held while the thread is asleep. 87974912Sjhb */ 88065557Sjasoneint 88174912Sjhbwitness_sleep(int check_only, struct lock_object *lock, const char *file, 88274912Sjhb int line) 88365557Sjasone{ 88474912Sjhb struct lock_list_entry **lock_list, *lle; 88576272Sjhb struct lock_instance *lock1; 88683366Sjulian struct thread *td; 88774912Sjhb int i, n; 88865557Sjasone 88997006Sjhb if (witness_cold || witness_dead || panicstr != NULL) 89074912Sjhb return (0); 89174912Sjhb n = 0; 89283366Sjulian td = curthread; 89383366Sjulian lock_list = &td->td_sleeplocks; 89474912Sjhbagain: 89574912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 89674912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 89776272Sjhb lock1 = &lle->ll_children[i]; 89876272Sjhb if (lock1->li_lock == lock || 89976272Sjhb lock1->li_lock == &Giant.mtx_object) 90074912Sjhb continue; 90176272Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) { 90278785Sjhb if (check_only == 0) { 90378785Sjhb CTR3(KTR_WITNESS, 90478871Sjhb "pid %d: sleeping with lock (%s) %s held", 90584680Sjhb td->td_proc->p_pid, 90678785Sjhb lock1->li_lock->lo_class->lc_name, 90778785Sjhb lock1->li_lock->lo_name); 90876272Sjhb lock1->li_flags |= LI_SLEPT; 90978785Sjhb } 91076272Sjhb continue; 91176272Sjhb } 91274912Sjhb n++; 91374912Sjhb printf("%s:%d: %s with \"%s\" locked from %s:%d\n", 91474912Sjhb file, line, check_only ? "could sleep" : "sleeping", 91576272Sjhb lock1->li_lock->lo_name, lock1->li_file, 91676272Sjhb lock1->li_line); 91774912Sjhb } 91897006Sjhb if (lock_list == &td->td_sleeplocks && PCPU_GET(spinlocks) != NULL) { 91997006Sjhb /* 92097006Sjhb * Since we already hold a spinlock preemption is 92197006Sjhb * already blocked. 92297006Sjhb */ 92374912Sjhb lock_list = PCPU_PTR(spinlocks); 92474912Sjhb goto again; 92565557Sjasone } 92667676Sjhb#ifdef DDB 92767676Sjhb if (witness_ddb && n) 92875711Sjhb Debugger(__func__); 92967676Sjhb#endif /* DDB */ 93065557Sjasone return (n); 93165557Sjasone} 93265557Sjasone 933102448Siedowseconst char * 934102448Siedowsewitness_file(struct lock_object *lock) 935102448Siedowse{ 936102448Siedowse struct witness *w; 937102448Siedowse 938102448Siedowse if (witness_cold || witness_dead || lock->lo_witness == NULL) 939102448Siedowse return ("?"); 940102448Siedowse w = lock->lo_witness; 941102448Siedowse return (w->w_file); 942102448Siedowse} 943102448Siedowse 944102448Siedowseint 945102448Siedowsewitness_line(struct lock_object *lock) 946102448Siedowse{ 947102448Siedowse struct witness *w; 948102448Siedowse 949102448Siedowse if (witness_cold || witness_dead || lock->lo_witness == NULL) 950102448Siedowse return (0); 951102448Siedowse w = lock->lo_witness; 952102448Siedowse return (w->w_line); 953102448Siedowse} 954102448Siedowse 95565856Sjhbstatic struct witness * 95674912Sjhbenroll(const char *description, struct lock_class *lock_class) 95765557Sjasone{ 95874912Sjhb struct witness *w; 95965557Sjasone 96080747Sjhb if (!witness_watch || witness_dead || panicstr != NULL) 96165557Sjasone return (NULL); 96274912Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 96365557Sjasone return (NULL); 96474912Sjhb mtx_lock_spin(&w_mtx); 96574912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 96697948Sjhb if (w->w_name == description || (w->w_refcount > 0 && 96797948Sjhb strcmp(description, w->w_name) == 0)) { 96875362Sjhb w->w_refcount++; 96974912Sjhb mtx_unlock_spin(&w_mtx); 97074912Sjhb if (lock_class != w->w_class) 97174912Sjhb panic( 97274912Sjhb "lock (%s) %s does not match earlier (%s) lock", 97374912Sjhb description, lock_class->lc_name, 97474912Sjhb w->w_class->lc_name); 97565557Sjasone return (w); 97665557Sjasone } 97765557Sjasone } 97874912Sjhb /* 97974912Sjhb * This isn't quite right, as witness_cold is still 0 while we 98074912Sjhb * enroll all the locks initialized before witness_initialize(). 98174912Sjhb */ 98275364Sbp if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 98375364Sbp mtx_unlock_spin(&w_mtx); 98474912Sjhb panic("spin lock %s not in order list", description); 98575364Sbp } 98665557Sjasone if ((w = witness_get()) == NULL) 98765557Sjasone return (NULL); 98874912Sjhb w->w_name = description; 98974912Sjhb w->w_class = lock_class; 99075362Sjhb w->w_refcount = 1; 99174912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 99274912Sjhb if (lock_class->lc_flags & LC_SPINLOCK) 99374912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 99474912Sjhb else if (lock_class->lc_flags & LC_SLEEPLOCK) 99574912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 99675364Sbp else { 99775364Sbp mtx_unlock_spin(&w_mtx); 99874912Sjhb panic("lock class %s is not sleep or spin", 99974912Sjhb lock_class->lc_name); 100075364Sbp } 100174912Sjhb mtx_unlock_spin(&w_mtx); 100265557Sjasone return (w); 100365557Sjasone} 100465557Sjasone 100565557Sjasonestatic int 100665856Sjhbitismychild(struct witness *parent, struct witness *child) 100765557Sjasone{ 100865557Sjasone static int recursed; 100974912Sjhb struct witness_child_list_entry **wcl; 101074912Sjhb struct witness_list *list; 101165557Sjasone 101274912Sjhb MPASS(child != NULL && parent != NULL); 101374912Sjhb if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 101474912Sjhb (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 101574912Sjhb panic( 101674912Sjhb "%s: parent (%s) and child (%s) are not the same lock type", 101774912Sjhb __func__, parent->w_class->lc_name, 101874912Sjhb child->w_class->lc_name); 101974912Sjhb 102065557Sjasone /* 102165557Sjasone * Insert "child" after "parent" 102265557Sjasone */ 102374912Sjhb wcl = &parent->w_children; 102474912Sjhb while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 102574912Sjhb wcl = &(*wcl)->wcl_next; 102674912Sjhb if (*wcl == NULL) { 102774912Sjhb *wcl = witness_child_get(); 102874912Sjhb if (*wcl == NULL) 102965557Sjasone return (1); 103065557Sjasone } 103174912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 103274912Sjhb 103365557Sjasone /* 103474912Sjhb * Now prune whole tree. We look for cases where a lock is now 103574912Sjhb * both a descendant and a direct child of a given lock. In that 103674912Sjhb * case, we want to remove the direct child link from the tree. 103765557Sjasone */ 103865557Sjasone if (recursed) 103965557Sjasone return (0); 104065557Sjasone recursed = 1; 104174912Sjhb if (parent->w_class->lc_flags & LC_SLEEPLOCK) 104274912Sjhb list = &w_sleep; 104374912Sjhb else 104474912Sjhb list = &w_spin; 104574912Sjhb STAILQ_FOREACH(child, list, w_typelist) { 104674912Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 104765557Sjasone if (!isitmychild(parent, child)) 104865557Sjasone continue; 104965557Sjasone removechild(parent, child); 105065557Sjasone if (isitmydescendant(parent, child)) 105165557Sjasone continue; 105265557Sjasone itismychild(parent, child); 105365557Sjasone } 105465557Sjasone } 105565557Sjasone recursed = 0; 105665557Sjasone witness_levelall(); 105765557Sjasone return (0); 105865557Sjasone} 105965557Sjasone 106065557Sjasonestatic void 106165856Sjhbremovechild(struct witness *parent, struct witness *child) 106265557Sjasone{ 106374912Sjhb struct witness_child_list_entry **wcl, *wcl1; 106465557Sjasone int i; 106565557Sjasone 106674912Sjhb for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 106774912Sjhb for (i = 0; i < (*wcl)->wcl_count; i++) 106874912Sjhb if ((*wcl)->wcl_children[i] == child) 106965557Sjasone goto found; 107065557Sjasone return; 107165557Sjasonefound: 107274912Sjhb (*wcl)->wcl_count--; 107374912Sjhb if ((*wcl)->wcl_count > i) 107474912Sjhb (*wcl)->wcl_children[i] = 107574912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count]; 107674912Sjhb MPASS((*wcl)->wcl_children[i] != NULL); 107774912Sjhb if ((*wcl)->wcl_count != 0) 107865557Sjasone return; 107974912Sjhb wcl1 = *wcl; 108074912Sjhb *wcl = wcl1->wcl_next; 108174912Sjhb witness_child_free(wcl1); 108265557Sjasone} 108365557Sjasone 108465557Sjasonestatic int 108565856Sjhbisitmychild(struct witness *parent, struct witness *child) 108665557Sjasone{ 108774912Sjhb struct witness_child_list_entry *wcl; 108865557Sjasone int i; 108965557Sjasone 109074912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 109174912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 109274912Sjhb if (wcl->wcl_children[i] == child) 109365557Sjasone return (1); 109465557Sjasone } 109565557Sjasone } 109665557Sjasone return (0); 109765557Sjasone} 109865557Sjasone 109965557Sjasonestatic int 110065856Sjhbisitmydescendant(struct witness *parent, struct witness *child) 110165557Sjasone{ 110274912Sjhb struct witness_child_list_entry *wcl; 110374912Sjhb int i, j; 110465557Sjasone 110574912Sjhb if (isitmychild(parent, child)) 110674912Sjhb return (1); 110774912Sjhb j = 0; 110874912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 110967352Sjhb MPASS(j < 1000); 111074912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 111174912Sjhb if (isitmydescendant(wcl->wcl_children[i], child)) 111265557Sjasone return (1); 111365557Sjasone } 111474912Sjhb j++; 111565557Sjasone } 111665557Sjasone return (0); 111765557Sjasone} 111865557Sjasone 111965557Sjasonevoid 112065557Sjasonewitness_levelall (void) 112165557Sjasone{ 112274912Sjhb struct witness_list *list; 112365856Sjhb struct witness *w, *w1; 112465557Sjasone 112574912Sjhb /* 112674912Sjhb * First clear all levels. 112774912Sjhb */ 112874912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 112974912Sjhb w->w_level = 0; 113074912Sjhb } 113174912Sjhb 113274912Sjhb /* 113374912Sjhb * Look for locks with no parent and level all their descendants. 113474912Sjhb */ 113574912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 113674912Sjhb /* 113774912Sjhb * This is just an optimization, technically we could get 113874912Sjhb * away just walking the all list each time. 113974912Sjhb */ 114074912Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 114174912Sjhb list = &w_sleep; 114274912Sjhb else 114374912Sjhb list = &w_spin; 114474912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 114565557Sjasone if (isitmychild(w1, w)) 114674912Sjhb goto skip; 114765557Sjasone } 114865557Sjasone witness_leveldescendents(w, 0); 114974912Sjhb skip: 115095541Smarcel ; /* silence GCC 3.x */ 115165557Sjasone } 115265557Sjasone} 115365557Sjasone 115465557Sjasonestatic void 115565856Sjhbwitness_leveldescendents(struct witness *parent, int level) 115665557Sjasone{ 115774912Sjhb struct witness_child_list_entry *wcl; 115865557Sjasone int i; 115965557Sjasone 116065557Sjasone if (parent->w_level < level) 116165557Sjasone parent->w_level = level; 116265557Sjasone level++; 116374912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 116474912Sjhb for (i = 0; i < wcl->wcl_count; i++) 116574912Sjhb witness_leveldescendents(wcl->wcl_children[i], level); 116665557Sjasone} 116765557Sjasone 116865557Sjasonestatic void 116965856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...), 117065856Sjhb struct witness *parent) 117165557Sjasone{ 117274912Sjhb struct witness_child_list_entry *wcl; 117374912Sjhb int i, level; 117465557Sjasone 117595543Sjhb level = parent->w_level; 117674912Sjhb prnt("%-2d", level); 117765557Sjasone for (i = 0; i < level; i++) 117865557Sjasone prnt(" "); 117997948Sjhb if (parent->w_refcount > 0) { 118097948Sjhb prnt("%s", parent->w_name); 118197948Sjhb if (parent->w_file != NULL) 118297948Sjhb prnt(" -- last acquired @ %s:%d\n", parent->w_file, 118397948Sjhb parent->w_line); 118497948Sjhb } else 118597948Sjhb prnt("(dead)\n"); 118674912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 118774912Sjhb for (i = 0; i < wcl->wcl_count; i++) 118874912Sjhb witness_displaydescendants(prnt, 118974912Sjhb wcl->wcl_children[i]); 119074912Sjhb} 119165557Sjasone 119265557Sjasonestatic int 119365856Sjhbblessed(struct witness *w1, struct witness *w2) 119465557Sjasone{ 119565557Sjasone int i; 119665856Sjhb struct witness_blessed *b; 119765557Sjasone 119865557Sjasone for (i = 0; i < blessed_count; i++) { 119965557Sjasone b = &blessed_list[i]; 120074912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 120174912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 120265557Sjasone return (1); 120365557Sjasone continue; 120465557Sjasone } 120574912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 120674912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 120765557Sjasone return (1); 120865557Sjasone } 120965557Sjasone return (0); 121065557Sjasone} 121165557Sjasone 121265856Sjhbstatic struct witness * 121374912Sjhbwitness_get(void) 121465557Sjasone{ 121565856Sjhb struct witness *w; 121665557Sjasone 121776481Sjhb if (witness_dead) { 121876481Sjhb mtx_unlock_spin(&w_mtx); 121976481Sjhb return (NULL); 122076481Sjhb } 122174912Sjhb if (STAILQ_EMPTY(&w_free)) { 122265557Sjasone witness_dead = 1; 122374912Sjhb mtx_unlock_spin(&w_mtx); 122474912Sjhb printf("%s: witness exhausted\n", __func__); 122565557Sjasone return (NULL); 122665557Sjasone } 122774912Sjhb w = STAILQ_FIRST(&w_free); 122874912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 122965856Sjhb bzero(w, sizeof(*w)); 123065557Sjasone return (w); 123165557Sjasone} 123265557Sjasone 123365557Sjasonestatic void 123465856Sjhbwitness_free(struct witness *w) 123565557Sjasone{ 123674912Sjhb 123774912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 123865557Sjasone} 123965557Sjasone 124074912Sjhbstatic struct witness_child_list_entry * 124174912Sjhbwitness_child_get(void) 124265557Sjasone{ 124374912Sjhb struct witness_child_list_entry *wcl; 124465557Sjasone 124576481Sjhb if (witness_dead) { 124676481Sjhb mtx_unlock_spin(&w_mtx); 124776481Sjhb return (NULL); 124876481Sjhb } 124974912Sjhb wcl = w_child_free; 125074912Sjhb if (wcl == NULL) { 125174912Sjhb witness_dead = 1; 125274912Sjhb mtx_unlock_spin(&w_mtx); 125374912Sjhb printf("%s: witness exhausted\n", __func__); 125474912Sjhb return (NULL); 125565557Sjasone } 125674912Sjhb w_child_free = wcl->wcl_next; 125774912Sjhb bzero(wcl, sizeof(*wcl)); 125874912Sjhb return (wcl); 125974912Sjhb} 126069881Sjake 126174912Sjhbstatic void 126274912Sjhbwitness_child_free(struct witness_child_list_entry *wcl) 126374912Sjhb{ 126474912Sjhb 126574912Sjhb wcl->wcl_next = w_child_free; 126674912Sjhb w_child_free = wcl; 126765557Sjasone} 126865557Sjasone 126974912Sjhbstatic struct lock_list_entry * 127074912Sjhbwitness_lock_list_get(void) 127174912Sjhb{ 127274912Sjhb struct lock_list_entry *lle; 127371709Sjhb 127476481Sjhb if (witness_dead) 127576481Sjhb return (NULL); 127674912Sjhb mtx_lock_spin(&w_mtx); 127774912Sjhb lle = w_lock_list_free; 127874912Sjhb if (lle == NULL) { 127974912Sjhb witness_dead = 1; 128074912Sjhb mtx_unlock_spin(&w_mtx); 128174912Sjhb printf("%s: witness exhausted\n", __func__); 128274912Sjhb return (NULL); 128374912Sjhb } 128474912Sjhb w_lock_list_free = lle->ll_next; 128574912Sjhb mtx_unlock_spin(&w_mtx); 128674912Sjhb bzero(lle, sizeof(*lle)); 128774912Sjhb return (lle); 128874912Sjhb} 128974912Sjhb 129074912Sjhbstatic void 129174912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 129271709Sjhb{ 129371709Sjhb 129474912Sjhb mtx_lock_spin(&w_mtx); 129574912Sjhb lle->ll_next = w_lock_list_free; 129674912Sjhb w_lock_list_free = lle; 129774912Sjhb mtx_unlock_spin(&w_mtx); 129871709Sjhb} 129971709Sjhb 130076272Sjhbstatic struct lock_instance * 130176272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 130276272Sjhb{ 130376272Sjhb struct lock_list_entry *lle; 130476272Sjhb struct lock_instance *instance; 130576272Sjhb int i; 130676272Sjhb 130776272Sjhb for (lle = lock_list; lle != NULL; lle = lle->ll_next) 130876272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 130976272Sjhb instance = &lle->ll_children[i]; 131076272Sjhb if (instance->li_lock == lock) 131176272Sjhb return (instance); 131276272Sjhb } 131376272Sjhb return (NULL); 131476272Sjhb} 131576272Sjhb 131674912Sjhbint 131775273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 131872224Sjhb{ 131975273Sjhb struct lock_list_entry *lle; 132076272Sjhb struct lock_instance *instance; 132174912Sjhb struct lock_object *lock; 132274912Sjhb int i, nheld; 132372224Sjhb 132474912Sjhb nheld = 0; 132574912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 132674912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 132776272Sjhb instance = &lle->ll_children[i]; 132876272Sjhb lock = instance->li_lock; 132993811Sjhb printf("%s %s %s", 133076272Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0 ? 133176272Sjhb "exclusive" : "shared", 133293811Sjhb lock->lo_class->lc_name, lock->lo_name); 133393811Sjhb if (lock->lo_type != lock->lo_name) 133494324Sjhb printf(" (%s)", lock->lo_type); 133594325Sjhb printf(" r = %d (%p) locked @ %s:%d\n", 133694325Sjhb instance->li_flags & LI_RECURSEMASK, lock, 133776272Sjhb instance->li_file, instance->li_line); 133874912Sjhb nheld++; 133974912Sjhb } 134075273Sjhb return (nheld); 134175273Sjhb} 134275273Sjhb 134375273Sjhb/* 134483366Sjulian * Calling this on td != curthread is bad unless we are in ddb. 134575273Sjhb */ 134675273Sjhbint 134783366Sjulianwitness_list(struct thread *td) 134875273Sjhb{ 134975273Sjhb int nheld; 135075273Sjhb 135181489Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 135281489Sjhb#ifdef DDB 135383366Sjulian KASSERT(td == curthread || db_active, 135483366Sjulian ("%s: td != curthread and we aren't in the debugger", __func__)); 135576481Sjhb if (!db_active && witness_dead) 135676481Sjhb return (0); 135781489Sjhb#else 135883366Sjulian KASSERT(td == curthread, ("%s: p != curthread", __func__)); 135981489Sjhb if (witness_dead) 136081489Sjhb return (0); 136181489Sjhb#endif 136283366Sjulian nheld = witness_list_locks(&td->td_sleeplocks); 136375273Sjhb 136474912Sjhb /* 136583366Sjulian * We only handle spinlocks if td == curthread. This is somewhat broken 136683798Sjhb * if td is currently executing on some other CPU and holds spin locks 136775273Sjhb * as we won't display those locks. If we had a MI way of getting 136883798Sjhb * the per-cpu data for a given cpu then we could use 136983798Sjhb * td->td_kse->ke_oncpu to get the list of spinlocks for this thread 137083798Sjhb * and "fix" this. 137191905Sjhb * 137291905Sjhb * That still wouldn't really fix this unless we locked sched_lock 137391905Sjhb * or stopped the other CPU to make sure it wasn't changing the list 137491905Sjhb * out from under us. It is probably best to just not try to handle 137591905Sjhb * threads on other CPU's for now. 137674912Sjhb */ 137797006Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 137875273Sjhb nheld += witness_list_locks(PCPU_PTR(spinlocks)); 137997006Sjhb 138074912Sjhb return (nheld); 138172224Sjhb} 138271709Sjhb 138365557Sjasonevoid 138474912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 138565557Sjasone{ 138676272Sjhb struct lock_instance *instance; 138771320Sjasone 138882284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 138980747Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 139071352Sjasone return; 139182243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 139282243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 139382243Sjhb lock->lo_class->lc_name, lock->lo_name); 139483366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 139582243Sjhb if (instance == NULL) 139682243Sjhb panic("%s: lock (%s) %s not locked", __func__, 139782243Sjhb lock->lo_class->lc_name, lock->lo_name); 139876272Sjhb *filep = instance->li_file; 139976272Sjhb *linep = instance->li_line; 140065557Sjasone} 140165557Sjasone 140265557Sjasonevoid 140374912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 140465557Sjasone{ 140576272Sjhb struct lock_instance *instance; 140671320Sjasone 140782284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 140880747Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 140971352Sjasone return; 141082243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 141182243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 141282243Sjhb lock->lo_class->lc_name, lock->lo_name); 141383366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 141482243Sjhb if (instance == NULL) 141582243Sjhb panic("%s: lock (%s) %s not locked", __func__, 141682243Sjhb lock->lo_class->lc_name, lock->lo_name); 141774912Sjhb lock->lo_witness->w_file = file; 141874912Sjhb lock->lo_witness->w_line = line; 141976272Sjhb instance->li_file = file; 142076272Sjhb instance->li_line = line; 142165557Sjasone} 142265557Sjasone 142378871Sjhbvoid 142478871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 142578871Sjhb{ 142678871Sjhb#ifdef INVARIANT_SUPPORT 142778871Sjhb struct lock_instance *instance; 142878871Sjhb 142980747Sjhb if (lock->lo_witness == NULL || witness_dead || panicstr != NULL) 143078941Sjhb return; 143178871Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 143283366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 143378871Sjhb else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 143478871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 143586422Sjhb else { 143678871Sjhb panic("Lock (%s) %s is not sleep or spin!", 143778871Sjhb lock->lo_class->lc_name, lock->lo_name); 143886422Sjhb return; 143986422Sjhb } 144078871Sjhb switch (flags) { 144178871Sjhb case LA_UNLOCKED: 144278871Sjhb if (instance != NULL) 144378871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 144478871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 144578871Sjhb break; 144678871Sjhb case LA_LOCKED: 144778871Sjhb case LA_LOCKED | LA_RECURSED: 144878871Sjhb case LA_LOCKED | LA_NOTRECURSED: 144978871Sjhb case LA_SLOCKED: 145078871Sjhb case LA_SLOCKED | LA_RECURSED: 145178871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 145278871Sjhb case LA_XLOCKED: 145378871Sjhb case LA_XLOCKED | LA_RECURSED: 145478871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 145586422Sjhb if (instance == NULL) { 145678871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 145778871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 145886422Sjhb break; 145986422Sjhb } 146078871Sjhb if ((flags & LA_XLOCKED) != 0 && 146178871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 146278871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 146378871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 146478871Sjhb if ((flags & LA_SLOCKED) != 0 && 146578871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 146678871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 146778871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 146878871Sjhb if ((flags & LA_RECURSED) != 0 && 146978871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 147078871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 147178871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 147278871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 147378871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 147478871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 147578871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 147678871Sjhb break; 147778871Sjhb default: 147878871Sjhb panic("Invalid lock assertion at %s:%d.", file, line); 147978871Sjhb 148078871Sjhb } 148178871Sjhb#endif /* INVARIANT_SUPPORT */ 148278871Sjhb} 148378871Sjhb 148474912Sjhb#ifdef DDB 148574912Sjhb 148674930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 148774912Sjhb{ 148883366Sjulian struct thread *td; 148983366Sjulian pid_t pid; 149075273Sjhb struct proc *p; 149174912Sjhb 149275273Sjhb if (have_addr) { 149375273Sjhb pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 149475273Sjhb ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 149575273Sjhb ((addr >> 16) % 16) * 10000; 149675273Sjhb /* sx_slock(&allproc_lock); */ 149783366Sjulian FOREACH_PROC_IN_SYSTEM(p) { 149875273Sjhb if (p->p_pid == pid) 149975273Sjhb break; 150075273Sjhb } 150175273Sjhb /* sx_sunlock(&allproc_lock); */ 150275273Sjhb if (p == NULL) { 150375273Sjhb db_printf("pid %d not found\n", pid); 150475273Sjhb return; 150575273Sjhb } 150690361Sjulian FOREACH_THREAD_IN_PROC(p, td) { 150790361Sjulian witness_list(td); 150890361Sjulian } 150983366Sjulian } else { 151083366Sjulian td = curthread; 151190361Sjulian witness_list(td); 151283366Sjulian } 151374912Sjhb} 151474912Sjhb 151574912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 151674912Sjhb{ 151774912Sjhb 151874912Sjhb witness_display(db_printf); 151974912Sjhb} 152074912Sjhb#endif 1521