subr_witness.c revision 207410
165557Sjasone/*- 2181695Sattilio * Copyright (c) 2008 Isilon Systems, Inc. 3181695Sattilio * Copyright (c) 2008 Ilya Maykov <ivmaykov@gmail.com> 4181695Sattilio * Copyright (c) 1998 Berkeley Software Design, Inc. 5181695Sattilio * All rights reserved. 665557Sjasone * 765557Sjasone * Redistribution and use in source and binary forms, with or without 865557Sjasone * modification, are permitted provided that the following conditions 965557Sjasone * are met: 1065557Sjasone * 1. Redistributions of source code must retain the above copyright 1165557Sjasone * notice, this list of conditions and the following disclaimer. 1265557Sjasone * 2. Redistributions in binary form must reproduce the above copyright 1365557Sjasone * notice, this list of conditions and the following disclaimer in the 1465557Sjasone * documentation and/or other materials provided with the distribution. 1565557Sjasone * 3. Berkeley Software Design Inc's name may not be used to endorse or 1665557Sjasone * promote products derived from this software without specific prior 1765557Sjasone * written permission. 1865557Sjasone * 1965557Sjasone * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 2065557Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2165557Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2265557Sjasone * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 2365557Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2465557Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2565557Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2665557Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2765557Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2865557Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2965557Sjasone * SUCH DAMAGE. 3065557Sjasone * 3165557Sjasone * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 3267352Sjhb * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 3365557Sjasone */ 3465557Sjasone 3565557Sjasone/* 3674912Sjhb * Implementation of the `witness' lock verifier. Originally implemented for 3774912Sjhb * mutexes in BSD/OS. Extended to handle generic lock objects and lock 3874912Sjhb * classes in FreeBSD. 3972200Sbmilekic */ 4072200Sbmilekic 4172200Sbmilekic/* 4265557Sjasone * Main Entry: witness 4365557Sjasone * Pronunciation: 'wit-n&s 4465557Sjasone * Function: noun 4565557Sjasone * Etymology: Middle English witnesse, from Old English witnes knowledge, 4665557Sjasone * testimony, witness, from 2wit 4765557Sjasone * Date: before 12th century 4865557Sjasone * 1 : attestation of a fact or event : TESTIMONY 4965557Sjasone * 2 : one that gives evidence; specifically : one who testifies in 5065557Sjasone * a cause or before a judicial tribunal 5165557Sjasone * 3 : one asked to be present at a transaction so as to be able to 5265557Sjasone * testify to its having taken place 5365557Sjasone * 4 : one who has personal knowledge of something 5465557Sjasone * 5 a : something serving as evidence or proof : SIGN 5565557Sjasone * b : public affirmation by word or example of usually 5665557Sjasone * religious faith or conviction <the heroic witness to divine 5765557Sjasone * life -- Pilot> 5865557Sjasone * 6 capitalized : a member of the Jehovah's Witnesses 5965557Sjasone */ 6065557Sjasone 61111881Sjhb/* 62111881Sjhb * Special rules concerning Giant and lock orders: 63111881Sjhb * 64111881Sjhb * 1) Giant must be acquired before any other mutexes. Stated another way, 65111881Sjhb * no other mutex may be held when Giant is acquired. 66111881Sjhb * 67111881Sjhb * 2) Giant must be released when blocking on a sleepable lock. 68111881Sjhb * 69111881Sjhb * This rule is less obvious, but is a result of Giant providing the same 70111881Sjhb * semantics as spl(). Basically, when a thread sleeps, it must release 71111881Sjhb * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule 72111881Sjhb * 2). 73111881Sjhb * 74111881Sjhb * 3) Giant may be acquired before or after sleepable locks. 75111881Sjhb * 76111881Sjhb * This rule is also not quite as obvious. Giant may be acquired after 77111881Sjhb * a sleepable lock because it is a non-sleepable lock and non-sleepable 78111881Sjhb * locks may always be acquired while holding a sleepable lock. The second 79111881Sjhb * case, Giant before a sleepable lock, follows from rule 2) above. Suppose 80111881Sjhb * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1 81111881Sjhb * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and 82111881Sjhb * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to 83111881Sjhb * execute. Thus, acquiring Giant both before and after a sleepable lock 84111881Sjhb * will not result in a lock order reversal. 85111881Sjhb */ 86111881Sjhb 87116182Sobrien#include <sys/cdefs.h> 88116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_witness.c 207410 2010-04-30 00:46:43Z kmacy $"); 89116182Sobrien 9068790Sjhb#include "opt_ddb.h" 91168856Sjkoshy#include "opt_hwpmc_hooks.h" 92181695Sattilio#include "opt_stack.h" 9367676Sjhb#include "opt_witness.h" 9467676Sjhb 9565557Sjasone#include <sys/param.h> 9667352Sjhb#include <sys/bus.h> 97131930Smarcel#include <sys/kdb.h> 9867352Sjhb#include <sys/kernel.h> 9974912Sjhb#include <sys/ktr.h> 10074912Sjhb#include <sys/lock.h> 10167352Sjhb#include <sys/malloc.h> 10274912Sjhb#include <sys/mutex.h> 103164033Srwatson#include <sys/priv.h> 10465557Sjasone#include <sys/proc.h> 105178841Sattilio#include <sys/sbuf.h> 106183955Sattilio#include <sys/sched.h> 107181695Sattilio#include <sys/stack.h> 10867676Sjhb#include <sys/sysctl.h> 10965557Sjasone#include <sys/systm.h> 11065557Sjasone 111181695Sattilio#ifdef DDB 11268790Sjhb#include <ddb/ddb.h> 113181695Sattilio#endif 11468790Sjhb 115111881Sjhb#include <machine/stdarg.h> 116111881Sjhb 117181695Sattilio#if !defined(DDB) && !defined(STACK) 118181695Sattilio#error "DDB or STACK options are required for WITNESS" 119181695Sattilio#endif 120181695Sattilio 121154818Sjhb/* Note that these traces do not work with KTR_ALQ. */ 122154790Sjhb#if 0 123154790Sjhb#define KTR_WITNESS KTR_SUBSYS 124154790Sjhb#else 125154790Sjhb#define KTR_WITNESS 0 126154790Sjhb#endif 127154790Sjhb 128178165Sattilio#define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */ 129178165Sattilio#define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */ 130187511Sthompsa#define LI_NORELEASE 0x00020000 /* Lock not allowed to be released. */ 131178165Sattilio 132105508Sphk/* Define this to check for blessed mutexes */ 133105508Sphk#undef BLESSING 134105508Sphk 135181695Sattilio#define WITNESS_COUNT 1024 136181695Sattilio#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 137181695Sattilio#define WITNESS_HASH_SIZE 251 /* Prime, gives load factor < 2 */ 138179025Sattilio#define WITNESS_PENDLIST 512 139181695Sattilio 140181695Sattilio/* Allocate 256 KB of stack data space */ 141181695Sattilio#define WITNESS_LO_DATA_COUNT 2048 142181695Sattilio 143181695Sattilio/* Prime, gives load factor of ~2 at full load */ 144181695Sattilio#define WITNESS_LO_HASH_SIZE 1021 145181695Sattilio 14665557Sjasone/* 147181695Sattilio * XXX: This is somewhat bogus, as we assume here that at most 2048 threads 148181695Sattilio * will hold LOCK_NCHILDREN locks. We handle failure ok, and we should 14974912Sjhb * probably be safe for the most part, but it's still a SWAG. 15067352Sjhb */ 151181695Sattilio#define LOCK_NCHILDREN 5 152181695Sattilio#define LOCK_CHILDCOUNT 2048 15371352Sjasone 154181695Sattilio#define MAX_W_NAME 64 15571352Sjasone 156181695Sattilio#define BADSTACK_SBUF_SIZE (256 * WITNESS_COUNT) 157181695Sattilio#define CYCLEGRAPH_SBUF_SIZE 8192 158181695Sattilio#define FULLGRAPH_SBUF_SIZE 32768 159178165Sattilio 160181695Sattilio/* 161181695Sattilio * These flags go in the witness relationship matrix and describe the 162181695Sattilio * relationship between any two struct witness objects. 163181695Sattilio */ 164181695Sattilio#define WITNESS_UNRELATED 0x00 /* No lock order relation. */ 165181695Sattilio#define WITNESS_PARENT 0x01 /* Parent, aka direct ancestor. */ 166181695Sattilio#define WITNESS_ANCESTOR 0x02 /* Direct or indirect ancestor. */ 167181695Sattilio#define WITNESS_CHILD 0x04 /* Child, aka direct descendant. */ 168181695Sattilio#define WITNESS_DESCENDANT 0x08 /* Direct or indirect descendant. */ 169181695Sattilio#define WITNESS_ANCESTOR_MASK (WITNESS_PARENT | WITNESS_ANCESTOR) 170181695Sattilio#define WITNESS_DESCENDANT_MASK (WITNESS_CHILD | WITNESS_DESCENDANT) 171181695Sattilio#define WITNESS_RELATED_MASK \ 172181695Sattilio (WITNESS_ANCESTOR_MASK | WITNESS_DESCENDANT_MASK) 173181695Sattilio#define WITNESS_REVERSAL 0x10 /* A lock order reversal has been 174181695Sattilio * observed. */ 175181695Sattilio#define WITNESS_RESERVED1 0x20 /* Unused flag, reserved. */ 176181695Sattilio#define WITNESS_RESERVED2 0x40 /* Unused flag, reserved. */ 177181695Sattilio#define WITNESS_LOCK_ORDER_KNOWN 0x80 /* This lock order is known. */ 17871352Sjasone 179181695Sattilio/* Descendant to ancestor flags */ 180181695Sattilio#define WITNESS_DTOA(x) (((x) & WITNESS_RELATED_MASK) >> 2) 181181695Sattilio 182181695Sattilio/* Ancestor to descendant flags */ 183181695Sattilio#define WITNESS_ATOD(x) (((x) & WITNESS_RELATED_MASK) << 2) 184181695Sattilio 185181695Sattilio#define WITNESS_INDEX_ASSERT(i) \ 186181695Sattilio MPASS((i) > 0 && (i) <= w_max_used_index && (i) < WITNESS_COUNT) 187181695Sattilio 188181695SattilioMALLOC_DEFINE(M_WITNESS, "Witness", "Witness"); 189181695Sattilio 190178165Sattilio/* 191178165Sattilio * Lock instances. A lock instance is the data associated with a lock while 192178165Sattilio * it is held by witness. For example, a lock instance will hold the 193178165Sattilio * recursion count of a lock. Lock instances are held in lists. Spin locks 194178165Sattilio * are held in a per-cpu list while sleep locks are held in per-thread list. 195178165Sattilio */ 196178165Sattiliostruct lock_instance { 197181695Sattilio struct lock_object *li_lock; 198181695Sattilio const char *li_file; 199181695Sattilio int li_line; 200181695Sattilio u_int li_flags; 201178165Sattilio}; 202178165Sattilio 203178165Sattilio/* 204178165Sattilio * A simple list type used to build the list of locks held by a thread 205178165Sattilio * or CPU. We can't simply embed the list in struct lock_object since a 206178165Sattilio * lock may be held by more than one thread if it is a shared lock. Locks 207178165Sattilio * are added to the head of the list, so we fill up each list entry from 208178165Sattilio * "the back" logically. To ease some of the arithmetic, we actually fill 209178165Sattilio * in each list entry the normal way (children[0] then children[1], etc.) but 210178165Sattilio * when we traverse the list we read children[count-1] as the first entry 211178165Sattilio * down to children[0] as the final entry. 212178165Sattilio */ 213178165Sattiliostruct lock_list_entry { 214178165Sattilio struct lock_list_entry *ll_next; 215178165Sattilio struct lock_instance ll_children[LOCK_NCHILDREN]; 216178165Sattilio u_int ll_count; 217178165Sattilio}; 218178165Sattilio 219181695Sattilio/* 220181695Sattilio * The main witness structure. One of these per named lock type in the system 221181695Sattilio * (for example, "vnode interlock"). 222181695Sattilio */ 22374912Sjhbstruct witness { 224181695Sattilio char w_name[MAX_W_NAME]; 225181695Sattilio uint32_t w_index; /* Index in the relationship matrix */ 226181695Sattilio struct lock_class *w_class; 227181695Sattilio STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 228181695Sattilio STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 229181695Sattilio struct witness *w_hash_next; /* Linked list in hash buckets. */ 230181695Sattilio const char *w_file; /* File where last acquired */ 231181695Sattilio uint32_t w_line; /* Line where last acquired */ 232181695Sattilio uint32_t w_refcount; 233181695Sattilio uint16_t w_num_ancestors; /* direct/indirect 234181695Sattilio * ancestor count */ 235181695Sattilio uint16_t w_num_descendants; /* direct/indirect 236181695Sattilio * descendant count */ 237181695Sattilio int16_t w_ddb_level; 238188056Simp unsigned w_displayed:1; 239188056Simp unsigned w_reversed:1; 24074912Sjhb}; 24171352Sjasone 242181695SattilioSTAILQ_HEAD(witness_list, witness); 243181695Sattilio 244181695Sattilio/* 245181695Sattilio * The witness hash table. Keys are witness names (const char *), elements are 246181695Sattilio * witness objects (struct witness *). 247181695Sattilio */ 248181695Sattiliostruct witness_hash { 249181695Sattilio struct witness *wh_array[WITNESS_HASH_SIZE]; 250181695Sattilio uint32_t wh_size; 251181695Sattilio uint32_t wh_count; 25274912Sjhb}; 25371352Sjasone 254181695Sattilio/* 255181695Sattilio * Key type for the lock order data hash table. 256181695Sattilio */ 257181695Sattiliostruct witness_lock_order_key { 258181695Sattilio uint16_t from; 259181695Sattilio uint16_t to; 260181695Sattilio}; 26171352Sjasone 262181695Sattiliostruct witness_lock_order_data { 263181695Sattilio struct stack wlod_stack; 264181695Sattilio struct witness_lock_order_key wlod_key; 265181695Sattilio struct witness_lock_order_data *wlod_next; 266181695Sattilio}; 267181695Sattilio 268181695Sattilio/* 269181695Sattilio * The witness lock order data hash table. Keys are witness index tuples 270181695Sattilio * (struct witness_lock_order_key), elements are lock order data objects 271181695Sattilio * (struct witness_lock_order_data). 272181695Sattilio */ 273181695Sattiliostruct witness_lock_order_hash { 274181695Sattilio struct witness_lock_order_data *wloh_array[WITNESS_LO_HASH_SIZE]; 275181695Sattilio u_int wloh_size; 276181695Sattilio u_int wloh_count; 277181695Sattilio}; 278181695Sattilio 279105508Sphk#ifdef BLESSING 28074912Sjhbstruct witness_blessed { 281181695Sattilio const char *b_lock1; 282181695Sattilio const char *b_lock2; 28374912Sjhb}; 284105508Sphk#endif 28571352Sjasone 286179025Sattiliostruct witness_pendhelp { 287179025Sattilio const char *wh_type; 288179025Sattilio struct lock_object *wh_lock; 289179025Sattilio}; 290179025Sattilio 291181695Sattiliostruct witness_order_list_entry { 292181695Sattilio const char *w_name; 293181695Sattilio struct lock_class *w_class; 294181695Sattilio}; 295181695Sattilio 296181695Sattilio/* 297181695Sattilio * Returns 0 if one of the locks is a spin lock and the other is not. 298181695Sattilio * Returns 1 otherwise. 299181695Sattilio */ 300181695Sattiliostatic __inline int 301181695Sattiliowitness_lock_type_equal(struct witness *w1, struct witness *w2) 302181695Sattilio{ 303181695Sattilio 304181695Sattilio return ((w1->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) == 305181695Sattilio (w2->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))); 306181695Sattilio} 307181695Sattilio 308181695Sattiliostatic __inline int 309181695Sattiliowitness_lock_order_key_empty(const struct witness_lock_order_key *key) 310181695Sattilio{ 311181695Sattilio 312181695Sattilio return (key->from == 0 && key->to == 0); 313181695Sattilio} 314181695Sattilio 315181695Sattiliostatic __inline int 316181695Sattiliowitness_lock_order_key_equal(const struct witness_lock_order_key *a, 317181695Sattilio const struct witness_lock_order_key *b) 318181695Sattilio{ 319181695Sattilio 320181695Sattilio return (a->from == b->from && a->to == b->to); 321181695Sattilio} 322181695Sattilio 323181695Sattiliostatic int _isitmyx(struct witness *w1, struct witness *w2, int rmask, 324181695Sattilio const char *fname); 325181695Sattilio#ifdef KDB 326181695Sattiliostatic void _witness_debugger(int cond, const char *msg); 327181695Sattilio#endif 328181695Sattiliostatic void adopt(struct witness *parent, struct witness *child); 329112117Sjhb#ifdef BLESSING 330112117Sjhbstatic int blessed(struct witness *, struct witness *); 331112117Sjhb#endif 332179025Sattiliostatic void depart(struct witness *w); 333181695Sattiliostatic struct witness *enroll(const char *description, 334181695Sattilio struct lock_class *lock_class); 335181695Sattiliostatic struct lock_instance *find_instance(struct lock_list_entry *list, 336181695Sattilio struct lock_object *lock); 337112117Sjhbstatic int isitmychild(struct witness *parent, struct witness *child); 338112117Sjhbstatic int isitmydescendant(struct witness *parent, struct witness *child); 339181695Sattiliostatic void itismychild(struct witness *parent, struct witness *child); 340181695Sattiliostatic int sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS); 341112562Sjhbstatic int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 342181695Sattiliostatic int sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS); 343181695Sattiliostatic void witness_add_fullgraph(struct sbuf *sb, struct witness *parent); 344181695Sattilio#ifdef DDB 345181695Sattiliostatic void witness_ddb_compute_levels(void); 346181695Sattiliostatic void witness_ddb_display(void(*)(const char *fmt, ...)); 347181695Sattiliostatic void witness_ddb_display_descendants(void(*)(const char *fmt, ...), 348181695Sattilio struct witness *, int indent); 349181695Sattiliostatic void witness_ddb_display_list(void(*prnt)(const char *fmt, ...), 350181695Sattilio struct witness_list *list); 351181695Sattiliostatic void witness_ddb_level_descendants(struct witness *parent, int l); 352181695Sattiliostatic void witness_ddb_list(struct thread *td); 353181695Sattilio#endif 35474912Sjhbstatic void witness_free(struct witness *m); 355181695Sattiliostatic struct witness *witness_get(void); 356181695Sattiliostatic uint32_t witness_hash_djb2(const uint8_t *key, uint32_t size); 357181695Sattiliostatic struct witness *witness_hash_get(const char *key); 358181695Sattiliostatic void witness_hash_put(struct witness *w); 359181695Sattiliostatic void witness_init_hash_tables(void); 360181695Sattiliostatic void witness_increment_graph_generation(void); 36174912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 362181695Sattiliostatic struct lock_list_entry *witness_lock_list_get(void); 363181695Sattiliostatic int witness_lock_order_add(struct witness *parent, 364181695Sattilio struct witness *child); 365181695Sattiliostatic int witness_lock_order_check(struct witness *parent, 366181695Sattilio struct witness *child); 367181695Sattiliostatic struct witness_lock_order_data *witness_lock_order_get( 368181695Sattilio struct witness *parent, 369181695Sattilio struct witness *child); 370111881Sjhbstatic void witness_list_lock(struct lock_instance *instance); 371187511Sthompsastatic void witness_setflag(struct lock_object *lock, int flag, int set); 372181695Sattilio 373181695Sattilio#ifdef KDB 374181695Sattilio#define witness_debugger(c) _witness_debugger(c, __func__) 375181695Sattilio#else 376181695Sattilio#define witness_debugger(c) 377100011Smp#endif 37872200Sbmilekic 379188056SimpSYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, NULL, "Witness Locking"); 38072200Sbmilekic 381112562Sjhb/* 382183329Sjhb * If set to 0, lock order checking is disabled. If set to -1, 383183329Sjhb * witness is completely disabled. Otherwise witness performs full 384183329Sjhb * lock order checking for all locks. At runtime, lock order checking 385183329Sjhb * may be toggled. However, witness cannot be reenabled once it is 386183329Sjhb * completely disabled. 387112562Sjhb */ 38877843Speterstatic int witness_watch = 1; 389134873SjmgTUNABLE_INT("debug.witness.watch", &witness_watch); 390134873SjmgSYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 391112562Sjhb sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 39271352Sjasone 393131930Smarcel#ifdef KDB 39472200Sbmilekic/* 395181695Sattilio * When KDB is enabled and witness_kdb is 1, it will cause the system 396131930Smarcel * to drop into kdebug() when: 397151623Sjhb * - a lock hierarchy violation occurs 39865557Sjasone * - locks are held when going to sleep. 39965557Sjasone */ 400131930Smarcel#ifdef WITNESS_KDB 401131930Smarcelint witness_kdb = 1; 40267676Sjhb#else 403131930Smarcelint witness_kdb = 0; 40465557Sjasone#endif 405134873SjmgTUNABLE_INT("debug.witness.kdb", &witness_kdb); 406134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 407110779Speter 408110779Speter/* 409181695Sattilio * When KDB is enabled and witness_trace is 1, it will cause the system 410110779Speter * to print a stack trace: 411151623Sjhb * - a lock hierarchy violation occurs 412110779Speter * - locks are held when going to sleep. 413110779Speter */ 414110779Speterint witness_trace = 1; 415134873SjmgTUNABLE_INT("debug.witness.trace", &witness_trace); 416134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 417131930Smarcel#endif /* KDB */ 41865557Sjasone 41967676Sjhb#ifdef WITNESS_SKIPSPIN 42077843Speterint witness_skipspin = 1; 42167676Sjhb#else 42277843Speterint witness_skipspin = 0; 42365557Sjasone#endif 424134873SjmgTUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 425181695SattilioSYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, &witness_skipspin, 426181695Sattilio 0, ""); 42765557Sjasone 428181695Sattilio/* 429181695Sattilio * Call this to print out the relations between locks. 430181695Sattilio */ 431181695SattilioSYSCTL_PROC(_debug_witness, OID_AUTO, fullgraph, CTLTYPE_STRING | CTLFLAG_RD, 432181695Sattilio NULL, 0, sysctl_debug_witness_fullgraph, "A", "Show locks relation graphs"); 433181695Sattilio 434181695Sattilio/* 435181695Sattilio * Call this to print out the witness faulty stacks. 436181695Sattilio */ 437181695SattilioSYSCTL_PROC(_debug_witness, OID_AUTO, badstacks, CTLTYPE_STRING | CTLFLAG_RD, 438181695Sattilio NULL, 0, sysctl_debug_witness_badstacks, "A", "Show bad witness stacks"); 439181695Sattilio 44074912Sjhbstatic struct mtx w_mtx; 441181695Sattilio 442181695Sattilio/* w_list */ 44374912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 44474912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 445181695Sattilio 446181695Sattilio/* w_typelist */ 44774912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 44874912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 449181695Sattilio 450181695Sattilio/* lock list */ 45174912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 452179025Sattiliostatic struct witness_pendhelp pending_locks[WITNESS_PENDLIST]; 453179025Sattiliostatic u_int pending_cnt; 45465557Sjasone 455181695Sattiliostatic int w_free_cnt, w_spin_cnt, w_sleep_cnt; 456149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, ""); 457149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, ""); 458149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0, 459149441Struckman ""); 460149441Struckman 461181695Sattiliostatic struct witness *w_data; 462181695Sattiliostatic uint8_t w_rmatrix[WITNESS_COUNT+1][WITNESS_COUNT+1]; 46374912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 464181695Sattiliostatic struct witness_hash w_hash; /* The witness hash table. */ 46565557Sjasone 466181695Sattilio/* The lock order data hash */ 467181695Sattiliostatic struct witness_lock_order_data w_lodata[WITNESS_LO_DATA_COUNT]; 468181695Sattiliostatic struct witness_lock_order_data *w_lofree = NULL; 469181695Sattiliostatic struct witness_lock_order_hash w_lohash; 470181695Sattiliostatic int w_max_used_index = 0; 471181695Sattiliostatic unsigned int w_generation = 0; 472196891Santoinestatic const char w_notrunning[] = "Witness not running\n"; 473196891Santoinestatic const char w_stillcold[] = "Witness is still cold\n"; 474181695Sattilio 475181695Sattilio 47674912Sjhbstatic struct witness_order_list_entry order_lists[] = { 477149738Sjhb /* 478149738Sjhb * sx locks 479149738Sjhb */ 48074912Sjhb { "proctree", &lock_class_sx }, 48174912Sjhb { "allproc", &lock_class_sx }, 482168402Spjd { "allprison", &lock_class_sx }, 483149738Sjhb { NULL, NULL }, 484149738Sjhb /* 485149738Sjhb * Various mutexes 486149738Sjhb */ 487111951Sjhb { "Giant", &lock_class_mtx_sleep }, 488108184Skris { "pipe mutex", &lock_class_mtx_sleep }, 48996122Salfred { "sigio lock", &lock_class_mtx_sleep }, 49091140Stanimura { "process group", &lock_class_mtx_sleep }, 49174912Sjhb { "process lock", &lock_class_mtx_sleep }, 49291140Stanimura { "session", &lock_class_mtx_sleep }, 493177299Spjd { "uidinfo hash", &lock_class_rw }, 494168856Sjkoshy#ifdef HWPMC_HOOKS 495168856Sjkoshy { "pmc-sleep", &lock_class_mtx_sleep }, 496168856Sjkoshy#endif 49774912Sjhb { NULL, NULL }, 49875464Sjhb /* 499130022Srwatson * Sockets 500130022Srwatson */ 501130022Srwatson { "accept", &lock_class_mtx_sleep }, 502130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 503130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 504130022Srwatson { "sellck", &lock_class_mtx_sleep }, 505130022Srwatson { NULL, NULL }, 506130022Srwatson /* 507130022Srwatson * Routing 508130022Srwatson */ 509130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 510185747Skmacy { "radix node head", &lock_class_rw }, 511130022Srwatson { "rtentry", &lock_class_mtx_sleep }, 512130022Srwatson { "ifaddr", &lock_class_mtx_sleep }, 513130022Srwatson { NULL, NULL }, 514130022Srwatson /* 515191672Sbms * IPv4 multicast: 516191672Sbms * protocol locks before interface locks, after UDP locks. 517148682Srwatson */ 518178285Srwatson { "udpinp", &lock_class_rw }, 519148682Srwatson { "in_multi_mtx", &lock_class_mtx_sleep }, 520148682Srwatson { "igmp_mtx", &lock_class_mtx_sleep }, 521148682Srwatson { "if_addr_mtx", &lock_class_mtx_sleep }, 522148682Srwatson { NULL, NULL }, 523148682Srwatson /* 524191672Sbms * IPv6 multicast: 525191672Sbms * protocol locks before interface locks, after UDP locks. 526191672Sbms */ 527191672Sbms { "udpinp", &lock_class_rw }, 528191672Sbms { "in6_multi_mtx", &lock_class_mtx_sleep }, 529191672Sbms { "mld_mtx", &lock_class_mtx_sleep }, 530191672Sbms { "if_addr_mtx", &lock_class_mtx_sleep }, 531191672Sbms { NULL, NULL }, 532191672Sbms /* 533130022Srwatson * UNIX Domain Sockets 534130396Srwatson */ 535189544Srwatson { "unp_global_rwlock", &lock_class_rw }, 536189544Srwatson { "unp_list_lock", &lock_class_mtx_sleep }, 537130396Srwatson { "unp", &lock_class_mtx_sleep }, 538130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 539130031Sjhb { NULL, NULL }, 540130022Srwatson /* 541130022Srwatson * UDP/IP 542130022Srwatson */ 543178285Srwatson { "udp", &lock_class_rw }, 544178285Srwatson { "udpinp", &lock_class_rw }, 545130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 546130022Srwatson { NULL, NULL }, 547130022Srwatson /* 548130022Srwatson * TCP/IP 549130022Srwatson */ 550178285Srwatson { "tcp", &lock_class_rw }, 551178285Srwatson { "tcpinp", &lock_class_rw }, 552130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 553130022Srwatson { NULL, NULL }, 554130022Srwatson /* 555132639Srwatson * netatalk 556132639Srwatson */ 557132639Srwatson { "ddp_list_mtx", &lock_class_mtx_sleep }, 558132639Srwatson { "ddp_mtx", &lock_class_mtx_sleep }, 559132639Srwatson { NULL, NULL }, 560132639Srwatson /* 561134971Srwatson * BPF 562134971Srwatson */ 563134971Srwatson { "bpf global lock", &lock_class_mtx_sleep }, 564134971Srwatson { "bpf interface lock", &lock_class_mtx_sleep }, 565134971Srwatson { "bpf cdev lock", &lock_class_mtx_sleep }, 566144832Spjd { NULL, NULL }, 567143335Srwatson /* 568143335Srwatson * NFS server 569143335Srwatson */ 570143335Srwatson { "nfsd_mtx", &lock_class_mtx_sleep }, 571143335Srwatson { "so_snd", &lock_class_mtx_sleep }, 572134971Srwatson { NULL, NULL }, 573170530Ssam 574134971Srwatson /* 575170530Ssam * IEEE 802.11 576170530Ssam */ 577170530Ssam { "802.11 com lock", &lock_class_mtx_sleep}, 578170530Ssam { NULL, NULL }, 579170530Ssam /* 580170530Ssam * Network drivers 581170530Ssam */ 582170530Ssam { "network driver", &lock_class_mtx_sleep}, 583170530Ssam { NULL, NULL }, 584170530Ssam 585170530Ssam /* 586168217Swkoszek * Netgraph 587168217Swkoszek */ 588168217Swkoszek { "ng_node", &lock_class_mtx_sleep }, 589168217Swkoszek { "ng_worklist", &lock_class_mtx_sleep }, 590168217Swkoszek { NULL, NULL }, 591168217Swkoszek /* 592144836Spjd * CDEV 593144836Spjd */ 594145425Sjeff { "system map", &lock_class_mtx_sleep }, 595145425Sjeff { "vm page queue mutex", &lock_class_mtx_sleep }, 596145425Sjeff { "vnode interlock", &lock_class_mtx_sleep }, 597144836Spjd { "cdev", &lock_class_mtx_sleep }, 598144836Spjd { NULL, NULL }, 599144836Spjd /* 600207410Skmacy * VM 601207410Skmacy * 602207410Skmacy */ 603207410Skmacy { "vm object", &lock_class_mtx_sleep }, 604207410Skmacy { "page lock", &lock_class_mtx_sleep }, 605207410Skmacy { "vm page queue mutex", &lock_class_mtx_sleep }, 606207410Skmacy { "pmap", &lock_class_mtx_sleep }, 607207410Skmacy { NULL, NULL }, 608207410Skmacy /* 609166421Skib * kqueue/VFS interaction 610166421Skib */ 611166421Skib { "kqueue", &lock_class_mtx_sleep }, 612166421Skib { "struct mount mtx", &lock_class_mtx_sleep }, 613166421Skib { "vnode interlock", &lock_class_mtx_sleep }, 614166421Skib { NULL, NULL }, 615166421Skib /* 616192416Skmacy * ZFS locking 617192416Skmacy */ 618192416Skmacy { "dn->dn_mtx", &lock_class_sx }, 619192416Skmacy { "dr->dt.di.dr_mtx", &lock_class_sx }, 620192416Skmacy { "db->db_mtx", &lock_class_sx }, 621192416Skmacy { NULL, NULL }, 622192416Skmacy /* 62375464Sjhb * spin locks 62475464Sjhb */ 62584331Sjhb#ifdef SMP 62684331Sjhb { "ap boot", &lock_class_mtx_spin }, 62772224Sjhb#endif 628150582Sjhb { "rm.mutex_mtx", &lock_class_mtx_spin }, 62974912Sjhb { "sio", &lock_class_mtx_spin }, 630173877Sattilio { "scrlock", &lock_class_mtx_spin }, 63172224Sjhb#ifdef __i386__ 63274912Sjhb { "cy", &lock_class_mtx_spin }, 63372224Sjhb#endif 634170848Smarius#ifdef __sparc64__ 635170848Smarius { "pcib_mtx", &lock_class_mtx_spin }, 636170848Smarius { "rtc_mtx", &lock_class_mtx_spin }, 637170848Smarius#endif 638157584Smarcel { "scc_hwmtx", &lock_class_mtx_spin }, 639124972Sru { "uart_hwmtx", &lock_class_mtx_spin }, 640161638Sssouhlal { "fast_taskqueue", &lock_class_mtx_spin }, 641122001Sjhb { "intr table", &lock_class_mtx_spin }, 642168856Sjkoshy#ifdef HWPMC_HOOKS 643168856Sjkoshy { "pmc-per-proc", &lock_class_mtx_spin }, 644168856Sjkoshy#endif 645170302Sjeff { "process slock", &lock_class_mtx_spin }, 646126324Sjhb { "sleepq chain", &lock_class_mtx_spin }, 647170302Sjeff { "umtx lock", &lock_class_mtx_spin }, 648173877Sattilio { "rm_spinlock", &lock_class_mtx_spin }, 649170302Sjeff { "turnstile chain", &lock_class_mtx_spin }, 650170302Sjeff { "turnstile lock", &lock_class_mtx_spin }, 65174912Sjhb { "sched lock", &lock_class_mtx_spin }, 652122514Sjhb { "td_contested", &lock_class_mtx_spin }, 65374912Sjhb { "callout", &lock_class_mtx_spin }, 654136374Srwatson { "entropy harvest mutex", &lock_class_mtx_spin }, 655162285Sscottl { "syscons video lock", &lock_class_mtx_spin }, 656169803Sjeff { "time lock", &lock_class_mtx_spin }, 657172256Sattilio#ifdef SMP 658172256Sattilio { "smp rendezvous", &lock_class_mtx_spin }, 659172256Sattilio#endif 660176771Sraj#ifdef __powerpc__ 661176771Sraj { "tlb0", &lock_class_mtx_spin }, 662176771Sraj#endif 66365557Sjasone /* 66465557Sjasone * leaf locks 66565557Sjasone */ 666178149Sattilio { "intrcnt", &lock_class_mtx_spin }, 66788322Sjhb { "icu", &lock_class_mtx_spin }, 668172256Sattilio#if defined(SMP) && defined(__sparc64__) 669108187Sjake { "ipi", &lock_class_mtx_spin }, 67099862Speter#endif 671172256Sattilio#ifdef __i386__ 672172256Sattilio { "allpmaps", &lock_class_mtx_spin }, 673172256Sattilio { "descriptor tables", &lock_class_mtx_spin }, 674108187Sjake#endif 67578785Sjhb { "clk", &lock_class_mtx_spin }, 676178149Sattilio { "cpuset", &lock_class_mtx_spin }, 677172256Sattilio { "mprof lock", &lock_class_mtx_spin }, 678170302Sjeff { "zombie lock", &lock_class_mtx_spin }, 679103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 680104951Speter#ifdef __ia64__ 681104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 682104951Speter#endif 683115425Speter#if defined(__i386__) || defined(__amd64__) 684111068Speter { "pcicfg", &lock_class_mtx_spin }, 685143204Swpaul { "NDIS thread lock", &lock_class_mtx_spin }, 686111068Speter#endif 687144966Svkashyap { "tw_osl_io_lock", &lock_class_mtx_spin }, 688144966Svkashyap { "tw_osl_q_lock", &lock_class_mtx_spin }, 689144966Svkashyap { "tw_cl_io_lock", &lock_class_mtx_spin }, 690144966Svkashyap { "tw_cl_intr_lock", &lock_class_mtx_spin }, 691144966Svkashyap { "tw_cl_gen_lock", &lock_class_mtx_spin }, 692168856Sjkoshy#ifdef HWPMC_HOOKS 693168856Sjkoshy { "pmc-leaf", &lock_class_mtx_spin }, 694168856Sjkoshy#endif 695170302Sjeff { "blocked lock", &lock_class_mtx_spin }, 69674912Sjhb { NULL, NULL }, 69774912Sjhb { NULL, NULL } 69865557Sjasone}; 69965557Sjasone 700105508Sphk#ifdef BLESSING 70165557Sjasone/* 70265557Sjasone * Pairs of locks which have been blessed 70365557Sjasone * Don't complain about order problems with blessed locks 70465557Sjasone */ 70565856Sjhbstatic struct witness_blessed blessed_list[] = { 70665557Sjasone}; 70772200Sbmilekicstatic int blessed_count = 70872200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 709105508Sphk#endif 71065557Sjasone 71174912Sjhb/* 71274912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 71374912Sjhb */ 71474912Sjhbstatic int witness_cold = 1; 71574912Sjhb 71674912Sjhb/* 717151629Sjhb * This global is set to 1 once the static lock orders have been enrolled 718151629Sjhb * so that a warning can be issued for any spin locks enrolled later. 719151629Sjhb */ 720151629Sjhbstatic int witness_spin_warn = 0; 721151629Sjhb 722151629Sjhb/* 723153133Sjhb * The WITNESS-enabled diagnostic code. Note that the witness code does 724153133Sjhb * assume that the early boot is single-threaded at least until after this 725153133Sjhb * routine is completed. 72674912Sjhb */ 72771352Sjasonestatic void 72874912Sjhbwitness_initialize(void *dummy __unused) 72965557Sjasone{ 73074912Sjhb struct lock_object *lock; 73174912Sjhb struct witness_order_list_entry *order; 73274912Sjhb struct witness *w, *w1; 73374912Sjhb int i; 73465557Sjasone 735184214Sdes w_data = malloc(sizeof (struct witness) * WITNESS_COUNT, M_WITNESS, 736181695Sattilio M_NOWAIT | M_ZERO); 737181695Sattilio 73874912Sjhb /* 73974912Sjhb * We have to release Giant before initializing its witness 74074912Sjhb * structure so that WITNESS doesn't get confused. 74174912Sjhb */ 74274912Sjhb mtx_unlock(&Giant); 74374912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 74474912Sjhb 74587593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 74693811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 747164159Skmacy MTX_NOWITNESS | MTX_NOPROFILE); 748181695Sattilio for (i = WITNESS_COUNT - 1; i >= 0; i--) { 749181695Sattilio w = &w_data[i]; 750181695Sattilio memset(w, 0, sizeof(*w)); 751181695Sattilio w_data[i].w_index = i; /* Witness index never changes. */ 752181695Sattilio witness_free(w); 753181695Sattilio } 754181695Sattilio KASSERT(STAILQ_FIRST(&w_free)->w_index == 0, 755181695Sattilio ("%s: Invalid list of free witness objects", __func__)); 756181695Sattilio 757181695Sattilio /* Witness with index 0 is not used to aid in debugging. */ 758181695Sattilio STAILQ_REMOVE_HEAD(&w_free, w_list); 759181695Sattilio w_free_cnt--; 760181695Sattilio 761181695Sattilio memset(w_rmatrix, 0, 762181695Sattilio (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1))); 763181695Sattilio 76474912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 76574912Sjhb witness_lock_list_free(&w_locklistdata[i]); 766181695Sattilio witness_init_hash_tables(); 76774912Sjhb 76874912Sjhb /* First add in all the specified order lists. */ 76974912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 77074912Sjhb w = enroll(order->w_name, order->w_class); 77175569Sjhb if (w == NULL) 77275569Sjhb continue; 77374912Sjhb w->w_file = "order list"; 77474912Sjhb for (order++; order->w_name != NULL; order++) { 77574912Sjhb w1 = enroll(order->w_name, order->w_class); 77675569Sjhb if (w1 == NULL) 77775569Sjhb continue; 77874912Sjhb w1->w_file = "order list"; 779181695Sattilio itismychild(w, w1); 78074912Sjhb w = w1; 78165557Sjasone } 78265557Sjasone } 783151629Sjhb witness_spin_warn = 1; 78465557Sjasone 78574912Sjhb /* Iterate through all locks and add them to witness. */ 786179025Sattilio for (i = 0; pending_locks[i].wh_lock != NULL; i++) { 787179025Sattilio lock = pending_locks[i].wh_lock; 788153133Sjhb KASSERT(lock->lo_flags & LO_WITNESS, 789153133Sjhb ("%s: lock %s is on pending list but not LO_WITNESS", 790153133Sjhb __func__, lock->lo_name)); 791179025Sattilio lock->lo_witness = enroll(pending_locks[i].wh_type, 792179025Sattilio LOCK_CLASS(lock)); 79374912Sjhb } 79474912Sjhb 79574912Sjhb /* Mark the witness code as being ready for use. */ 796151629Sjhb witness_cold = 0; 79774912Sjhb 79874912Sjhb mtx_lock(&Giant); 79965557Sjasone} 800177253SrwatsonSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, 801177253Srwatson NULL); 80265557Sjasone 80374912Sjhbvoid 804179025Sattiliowitness_init(struct lock_object *lock, const char *type) 80574912Sjhb{ 80674912Sjhb struct lock_class *class; 80774912Sjhb 808153133Sjhb /* Various sanity checks. */ 809154077Sjhb class = LOCK_CLASS(lock); 81074912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 81174912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 81282284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 81374912Sjhb class->lc_name, lock->lo_name); 81474912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 81574912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 81682284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 81774912Sjhb class->lc_name, lock->lo_name); 81882244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 81982244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 82082284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 82182244Sjhb class->lc_name, lock->lo_name); 82282244Sjhb 823153133Sjhb /* 824153133Sjhb * If we shouldn't watch this lock, then just clear lo_witness. 825153133Sjhb * Otherwise, if witness_cold is set, then it is too early to 826153133Sjhb * enroll this lock, so defer it to witness_initialize() by adding 827153133Sjhb * it to the pending_locks list. If it is not too early, then enroll 828153133Sjhb * the lock now. 829153133Sjhb */ 830182446Sattilio if (witness_watch < 1 || panicstr != NULL || 831153133Sjhb (lock->lo_flags & LO_WITNESS) == 0) 832153133Sjhb lock->lo_witness = NULL; 833153133Sjhb else if (witness_cold) { 834179025Sattilio pending_locks[pending_cnt].wh_lock = lock; 835179025Sattilio pending_locks[pending_cnt++].wh_type = type; 836179025Sattilio if (pending_cnt > WITNESS_PENDLIST) 837179025Sattilio panic("%s: pending locks list is too small, bump it\n", 838179025Sattilio __func__); 839153133Sjhb } else 840179025Sattilio lock->lo_witness = enroll(type, class); 84174912Sjhb} 84274912Sjhb 84374912Sjhbvoid 84474912Sjhbwitness_destroy(struct lock_object *lock) 84574912Sjhb{ 846154077Sjhb struct lock_class *class; 84775362Sjhb struct witness *w; 84874912Sjhb 849154077Sjhb class = LOCK_CLASS(lock); 850181695Sattilio 85174912Sjhb if (witness_cold) 85274912Sjhb panic("lock (%s) %s destroyed while witness_cold", 853154077Sjhb class->lc_name, lock->lo_name); 85474912Sjhb 85576272Sjhb /* XXX: need to verify that no one holds the lock */ 856181695Sattilio if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL) 857181695Sattilio return; 858181695Sattilio w = lock->lo_witness; 859112117Sjhb 860181695Sattilio mtx_lock_spin(&w_mtx); 861181695Sattilio MPASS(w->w_refcount > 0); 862181695Sattilio w->w_refcount--; 863181695Sattilio 864181695Sattilio if (w->w_refcount == 0) 865181695Sattilio depart(w); 866181695Sattilio mtx_unlock_spin(&w_mtx); 86774912Sjhb} 86874912Sjhb 869112115Sjhb#ifdef DDB 87071352Sjasonestatic void 871181695Sattiliowitness_ddb_compute_levels(void) 872149979Struckman{ 873181695Sattilio struct witness *w; 874149979Struckman 875149979Struckman /* 876149979Struckman * First clear all levels. 877149979Struckman */ 878181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 879181695Sattilio w->w_ddb_level = -1; 880149979Struckman 881149979Struckman /* 882181695Sattilio * Look for locks with no parents and level all their descendants. 883149979Struckman */ 884149979Struckman STAILQ_FOREACH(w, &w_all, w_list) { 885181695Sattilio 886181695Sattilio /* If the witness has ancestors (is not a root), skip it. */ 887181695Sattilio if (w->w_num_ancestors > 0) 888181695Sattilio continue; 889181695Sattilio witness_ddb_level_descendants(w, 0); 890149979Struckman } 891149979Struckman} 892149979Struckman 893149979Struckmanstatic void 894181695Sattiliowitness_ddb_level_descendants(struct witness *w, int l) 895149979Struckman{ 896149979Struckman int i; 897149979Struckman 898181695Sattilio if (w->w_ddb_level >= l) 899181695Sattilio return; 900181695Sattilio 901181695Sattilio w->w_ddb_level = l; 902181695Sattilio l++; 903181695Sattilio 904181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 905181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 906181695Sattilio witness_ddb_level_descendants(&w_data[i], l); 907181695Sattilio } 908149979Struckman} 909149979Struckman 910149979Struckmanstatic void 911181695Sattiliowitness_ddb_display_descendants(void(*prnt)(const char *fmt, ...), 912181695Sattilio struct witness *w, int indent) 913149979Struckman{ 914181695Sattilio int i; 915149979Struckman 916181695Sattilio for (i = 0; i < indent; i++) 917181695Sattilio prnt(" "); 918181695Sattilio prnt("%s (type: %s, depth: %d, active refs: %d)", 919181695Sattilio w->w_name, w->w_class->lc_name, 920181695Sattilio w->w_ddb_level, w->w_refcount); 921181695Sattilio if (w->w_displayed) { 922181695Sattilio prnt(" -- (already displayed)\n"); 923181695Sattilio return; 924181695Sattilio } 925181695Sattilio w->w_displayed = 1; 926181695Sattilio if (w->w_file != NULL && w->w_line != 0) 927181695Sattilio prnt(" -- last acquired @ %s:%d\n", w->w_file, 928181695Sattilio w->w_line); 929149979Struckman else 930181695Sattilio prnt(" -- never acquired\n"); 931181695Sattilio indent++; 932181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 933181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 934181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 935181695Sattilio witness_ddb_display_descendants(prnt, &w_data[i], 936181695Sattilio indent); 937149979Struckman } 938149979Struckman} 939149979Struckman 940149979Struckmanstatic void 941181695Sattiliowitness_ddb_display_list(void(*prnt)(const char *fmt, ...), 942181695Sattilio struct witness_list *list) 94371352Sjasone{ 944112118Sjhb struct witness *w; 94571352Sjasone 94674912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 947181695Sattilio if (w->w_file == NULL || w->w_ddb_level > 0) 94871352Sjasone continue; 949181695Sattilio 950181695Sattilio /* This lock has no anscestors - display its descendants. */ 951181695Sattilio witness_ddb_display_descendants(prnt, w, 0); 95271352Sjasone } 95374912Sjhb} 95472224Sjhb 95574912Sjhbstatic void 956181695Sattiliowitness_ddb_display(void(*prnt)(const char *fmt, ...)) 957178841Sattilio{ 95874912Sjhb struct witness *w; 95974912Sjhb 960181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 961181695Sattilio witness_ddb_compute_levels(); 96274912Sjhb 963112118Sjhb /* Clear all the displayed flags. */ 964181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 965112118Sjhb w->w_displayed = 0; 966112118Sjhb 96772224Sjhb /* 96874930Sjhb * First, handle sleep locks which have been acquired at least 96974912Sjhb * once. 97074912Sjhb */ 97174912Sjhb prnt("Sleep locks:\n"); 972181695Sattilio witness_ddb_display_list(prnt, &w_sleep); 97374912Sjhb 97474912Sjhb /* 97574930Sjhb * Now do spin locks which have been acquired at least once. 97672224Sjhb */ 97774912Sjhb prnt("\nSpin locks:\n"); 978181695Sattilio witness_ddb_display_list(prnt, &w_spin); 97972224Sjhb 98072224Sjhb /* 98174930Sjhb * Finally, any locks which have not been acquired yet. 98272224Sjhb */ 98374912Sjhb prnt("\nLocks which were never acquired:\n"); 98474912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 98597948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 98671352Sjasone continue; 987181695Sattilio prnt("%s (type: %s, depth: %d)\n", w->w_name, 988181695Sattilio w->w_class->lc_name, w->w_ddb_level); 98971352Sjasone } 99071352Sjasone} 991112115Sjhb#endif /* DDB */ 99271352Sjasone 993112116Sjhb/* Trim useless garbage from filenames. */ 994112116Sjhbstatic const char * 995112116Sjhbfixup_filename(const char *file) 996112116Sjhb{ 997112116Sjhb 998112116Sjhb if (file == NULL) 999112116Sjhb return (NULL); 1000112116Sjhb while (strncmp(file, "../", 3) == 0) 1001112116Sjhb file += 3; 1002112116Sjhb return (file); 1003112116Sjhb} 1004112116Sjhb 1005125160Sjhbint 1006125160Sjhbwitness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 1007125160Sjhb{ 1008125160Sjhb 1009182473Sattilio if (witness_watch == -1 || panicstr != NULL) 1010125160Sjhb return (0); 1011125160Sjhb 1012125160Sjhb /* Require locks that witness knows about. */ 1013125160Sjhb if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 1014125160Sjhb lock2->lo_witness == NULL) 1015125160Sjhb return (EINVAL); 1016125160Sjhb 1017181695Sattilio mtx_assert(&w_mtx, MA_NOTOWNED); 1018125160Sjhb mtx_lock_spin(&w_mtx); 1019125160Sjhb 1020125160Sjhb /* 1021125160Sjhb * If we already have either an explicit or implied lock order that 1022125160Sjhb * is the other way around, then return an error. 1023125160Sjhb */ 1024182473Sattilio if (witness_watch && 1025182473Sattilio isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 1026125160Sjhb mtx_unlock_spin(&w_mtx); 1027125160Sjhb return (EDOOFUS); 1028125160Sjhb } 1029125160Sjhb 1030125160Sjhb /* Try to add the new order. */ 1031125160Sjhb CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1032179025Sattilio lock2->lo_witness->w_name, lock1->lo_witness->w_name); 1033181695Sattilio itismychild(lock1->lo_witness, lock2->lo_witness); 1034125160Sjhb mtx_unlock_spin(&w_mtx); 1035125160Sjhb return (0); 1036125160Sjhb} 1037125160Sjhb 103865557Sjasonevoid 1039125160Sjhbwitness_checkorder(struct lock_object *lock, int flags, const char *file, 1040182914Sjhb int line, struct lock_object *interlock) 104165557Sjasone{ 1042183955Sattilio struct lock_list_entry *lock_list, *lle; 1043182914Sjhb struct lock_instance *lock1, *lock2, *plock; 104474912Sjhb struct lock_class *class; 104565856Sjhb struct witness *w, *w1; 104683366Sjulian struct thread *td; 104774912Sjhb int i, j; 104865557Sjasone 1049182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL || 105080747Sjhb panicstr != NULL) 105171320Sjasone return; 1052125160Sjhb 105374912Sjhb w = lock->lo_witness; 1054154077Sjhb class = LOCK_CLASS(lock); 105583366Sjulian td = curthread; 1056112116Sjhb file = fixup_filename(file); 105765557Sjasone 105874912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 1059181695Sattilio 106093676Sjhb /* 106193676Sjhb * Since spin locks include a critical section, this check 1062131884Sjhb * implicitly enforces a lock order of all sleep locks before 106393676Sjhb * all spin locks. 106493676Sjhb */ 1065136304Sgreen if (td->td_critnest != 0 && !kdb_active) 106674912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 106774912Sjhb class->lc_name, lock->lo_name, file, line); 1068131884Sjhb 1069131884Sjhb /* 1070131884Sjhb * If this is the first lock acquired then just return as 1071131884Sjhb * no order checking is needed. 1072131884Sjhb */ 1073183955Sattilio lock_list = td->td_sleeplocks; 1074183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) 1075131884Sjhb return; 1076131884Sjhb } else { 1077181695Sattilio 1078131884Sjhb /* 1079131884Sjhb * If this is the first lock, just return as no order 1080183955Sattilio * checking is needed. Avoid problems with thread 1081183955Sattilio * migration pinning the thread while checking if 1082183955Sattilio * spinlocks are held. If at least one spinlock is held 1083183955Sattilio * the thread is in a safe path and it is allowed to 1084183955Sattilio * unpin it. 1085131884Sjhb */ 1086183955Sattilio sched_pin(); 1087183955Sattilio lock_list = PCPU_GET(spinlocks); 1088183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) { 1089183955Sattilio sched_unpin(); 1090131884Sjhb return; 1091183955Sattilio } 1092183955Sattilio sched_unpin(); 1093131884Sjhb } 109465557Sjasone 109576772Sjhb /* 1096125160Sjhb * Check to see if we are recursing on a lock we already own. If 1097125160Sjhb * so, make sure that we don't mismatch exclusive and shared lock 1098125160Sjhb * acquires. 109976272Sjhb */ 1100183955Sattilio lock1 = find_instance(lock_list, lock); 110176272Sjhb if (lock1 != NULL) { 110276272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 110376272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 110476272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 110576272Sjhb class->lc_name, lock->lo_name, file, line); 110676272Sjhb printf("while exclusively locked from %s:%d\n", 110776272Sjhb lock1->li_file, lock1->li_line); 110876272Sjhb panic("share->excl"); 110976272Sjhb } 111076272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 111176272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 111276272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 111376272Sjhb class->lc_name, lock->lo_name, file, line); 111476272Sjhb printf("while share locked from %s:%d\n", 111576272Sjhb lock1->li_file, lock1->li_line); 111676272Sjhb panic("excl->share"); 111776272Sjhb } 111876272Sjhb return; 111976272Sjhb } 112076272Sjhb 112176272Sjhb /* 1122182914Sjhb * Find the previously acquired lock, but ignore interlocks. 1123182914Sjhb */ 1124183955Sattilio plock = &lock_list->ll_children[lock_list->ll_count - 1]; 1125182914Sjhb if (interlock != NULL && plock->li_lock == interlock) { 1126183955Sattilio if (lock_list->ll_count > 1) 1127183955Sattilio plock = 1128183955Sattilio &lock_list->ll_children[lock_list->ll_count - 2]; 1129183955Sattilio else { 1130183955Sattilio lle = lock_list->ll_next; 1131182984Sattilio 1132182914Sjhb /* 1133182914Sjhb * The interlock is the only lock we hold, so 1134183955Sattilio * simply return. 1135182914Sjhb */ 1136183955Sattilio if (lle == NULL) 1137183955Sattilio return; 1138183955Sattilio plock = &lle->ll_children[lle->ll_count - 1]; 1139182914Sjhb } 1140182914Sjhb } 1141182914Sjhb 1142182914Sjhb /* 1143181695Sattilio * Try to perform most checks without a lock. If this succeeds we 1144181695Sattilio * can skip acquiring the lock and return success. 1145181695Sattilio */ 1146182914Sjhb w1 = plock->li_lock->lo_witness; 1147181695Sattilio if (witness_lock_order_check(w1, w)) 1148181695Sattilio return; 1149181695Sattilio 1150181695Sattilio /* 115174912Sjhb * Check for duplicate locks of the same type. Note that we only 115274912Sjhb * have to check for this on the last lock we just acquired. Any 115374912Sjhb * other cases will be caught as lock order violations. 115474912Sjhb */ 1155181695Sattilio mtx_lock_spin(&w_mtx); 1156181695Sattilio witness_lock_order_add(w1, w); 115774912Sjhb if (w1 == w) { 1158181695Sattilio i = w->w_index; 1159181695Sattilio if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) && 1160181695Sattilio !(w_rmatrix[i][i] & WITNESS_REVERSAL)) { 1161181695Sattilio w_rmatrix[i][i] |= WITNESS_REVERSAL; 1162181695Sattilio w->w_reversed = 1; 1163181695Sattilio mtx_unlock_spin(&w_mtx); 1164183574Sjhb printf( 1165183574Sjhb "acquiring duplicate lock of same type: \"%s\"\n", 1166181695Sattilio w->w_name); 1167183574Sjhb printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name, 1168183574Sjhb plock->li_file, plock->li_line); 1169181695Sattilio printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 1170181695Sattilio witness_debugger(1); 1171181695Sattilio } else 1172181695Sattilio mtx_unlock_spin(&w_mtx); 1173125160Sjhb return; 117465557Sjasone } 1175181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1176181695Sattilio 117765557Sjasone /* 1178111881Sjhb * If we know that the the lock we are acquiring comes after 1179111881Sjhb * the lock we most recently acquired in the lock order tree, 1180111881Sjhb * then there is no need for any further checks. 1181111881Sjhb */ 1182181695Sattilio if (isitmychild(w1, w)) 1183181695Sattilio goto out; 1184181695Sattilio 1185183955Sattilio for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) { 118674912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 118765557Sjasone 118874912Sjhb MPASS(j < WITNESS_COUNT); 118976272Sjhb lock1 = &lle->ll_children[i]; 119074912Sjhb 119174912Sjhb /* 1192182914Sjhb * Ignore the interlock the first time we see it. 1193182914Sjhb */ 1194182914Sjhb if (interlock != NULL && interlock == lock1->li_lock) { 1195182914Sjhb interlock = NULL; 1196182914Sjhb continue; 1197182914Sjhb } 1198182914Sjhb 1199182914Sjhb /* 120074912Sjhb * If this lock doesn't undergo witness checking, 120174912Sjhb * then skip it. 120274912Sjhb */ 1203182914Sjhb w1 = lock1->li_lock->lo_witness; 120474912Sjhb if (w1 == NULL) { 120576272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 120674912Sjhb ("lock missing witness structure")); 120774912Sjhb continue; 120874912Sjhb } 1209181695Sattilio 121076272Sjhb /* 1211111881Sjhb * If we are locking Giant and this is a sleepable 121276272Sjhb * lock, then skip it. 121376272Sjhb */ 1214111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 1215167787Sjhb lock == &Giant.lock_object) 121676272Sjhb continue; 1217181695Sattilio 121893690Sjhb /* 121993690Sjhb * If we are locking a sleepable lock and this lock 1220111881Sjhb * is Giant, then skip it. 122193690Sjhb */ 1222111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 1223167787Sjhb lock1->li_lock == &Giant.lock_object) 1224111881Sjhb continue; 1225181695Sattilio 1226111881Sjhb /* 1227111881Sjhb * If we are locking a sleepable lock and this lock 1228111881Sjhb * isn't sleepable, we want to treat it as a lock 1229111881Sjhb * order violation to enfore a general lock order of 1230111881Sjhb * sleepable locks before non-sleepable locks. 1231111881Sjhb */ 1232149738Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1233111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1234149738Sjhb goto reversal; 1235181695Sattilio 1236149738Sjhb /* 1237150179Sjhb * If we are locking Giant and this is a non-sleepable 1238150179Sjhb * lock, then treat it as a reversal. 1239150179Sjhb */ 1240150179Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 1241167787Sjhb lock == &Giant.lock_object) 1242150179Sjhb goto reversal; 1243181695Sattilio 1244150179Sjhb /* 1245149738Sjhb * Check the lock order hierarchy for a reveresal. 1246149738Sjhb */ 1247149738Sjhb if (!isitmydescendant(w, w1)) 124874912Sjhb continue; 1249149738Sjhb reversal: 1250181695Sattilio 125174912Sjhb /* 125274912Sjhb * We have a lock order violation, check to see if it 125374912Sjhb * is allowed or has already been yelled about. 125474912Sjhb */ 1255105508Sphk#ifdef BLESSING 1256181695Sattilio 1257125160Sjhb /* 1258125160Sjhb * If the lock order is blessed, just bail. We don't 1259125160Sjhb * look for other lock order violations though, which 1260125160Sjhb * may be a bug. 1261125160Sjhb */ 126265557Sjasone if (blessed(w, w1)) 1263181695Sattilio goto out; 1264105508Sphk#endif 1265181695Sattilio 1266181695Sattilio /* Bail if this violation is known */ 1267181695Sattilio if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL) 1268181695Sattilio goto out; 1269181695Sattilio 1270181695Sattilio /* Record this as a violation */ 1271181695Sattilio w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL; 1272181695Sattilio w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL; 1273181695Sattilio w->w_reversed = w1->w_reversed = 1; 1274181695Sattilio witness_increment_graph_generation(); 1275181695Sattilio mtx_unlock_spin(&w_mtx); 1276181695Sattilio 127774912Sjhb /* 127874912Sjhb * Ok, yell about it. 127974912Sjhb */ 1280150179Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1281150179Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1282150179Sjhb printf( 1283150179Sjhb "lock order reversal: (sleepable after non-sleepable)\n"); 1284150179Sjhb else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1285167787Sjhb && lock == &Giant.lock_object) 1286150179Sjhb printf( 1287150179Sjhb "lock order reversal: (Giant after non-sleepable)\n"); 1288150179Sjhb else 1289150179Sjhb printf("lock order reversal:\n"); 1290181695Sattilio 129174912Sjhb /* 129274912Sjhb * Try to locate an earlier lock with 129374912Sjhb * witness w in our list. 129474912Sjhb */ 129574912Sjhb do { 129676272Sjhb lock2 = &lle->ll_children[i]; 129776272Sjhb MPASS(lock2->li_lock != NULL); 129876272Sjhb if (lock2->li_lock->lo_witness == w) 129974912Sjhb break; 130074912Sjhb if (i == 0 && lle->ll_next != NULL) { 130174912Sjhb lle = lle->ll_next; 130274912Sjhb i = lle->ll_count - 1; 1303106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 1304125160Sjhb } else 1305125160Sjhb i--; 130674912Sjhb } while (i >= 0); 130776272Sjhb if (i < 0) { 130893811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 130993811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 1310179025Sattilio w1->w_name, lock1->li_file, lock1->li_line); 131193811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 1312179025Sattilio lock->lo_name, w->w_name, file, line); 131376272Sjhb } else { 131493811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 131593811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 1316179025Sattilio lock2->li_lock->lo_witness->w_name, 1317179025Sattilio lock2->li_file, lock2->li_line); 131893811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 131993811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 1320179025Sattilio w1->w_name, lock1->li_file, lock1->li_line); 132193811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 1322179025Sattilio lock->lo_name, w->w_name, file, line); 132376272Sjhb } 1324181695Sattilio witness_debugger(1); 1325125160Sjhb return; 132665557Sjasone } 132765557Sjasone } 1328181695Sattilio 132978871Sjhb /* 1330125160Sjhb * If requested, build a new lock order. However, don't build a new 1331125160Sjhb * relationship between a sleepable lock and Giant if it is in the 1332125160Sjhb * wrong direction. The correct lock order is that sleepable locks 1333125160Sjhb * always come before Giant. 133478871Sjhb */ 1335125160Sjhb if (flags & LOP_NEWORDER && 1336182914Sjhb !(plock->li_lock == &Giant.lock_object && 1337112117Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0)) { 133887593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1339182914Sjhb w->w_name, plock->li_lock->lo_witness->w_name); 1340182914Sjhb itismychild(plock->li_lock->lo_witness, w); 1341181695Sattilio } 1342181695Sattilioout: 1343112117Sjhb mtx_unlock_spin(&w_mtx); 1344125160Sjhb} 1345125160Sjhb 1346125160Sjhbvoid 1347125160Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 1348125160Sjhb{ 1349125160Sjhb struct lock_list_entry **lock_list, *lle; 1350125160Sjhb struct lock_instance *instance; 1351125160Sjhb struct witness *w; 1352125160Sjhb struct thread *td; 1353125160Sjhb 1354182446Sattilio if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL || 1355125160Sjhb panicstr != NULL) 1356125160Sjhb return; 1357125160Sjhb w = lock->lo_witness; 1358125160Sjhb td = curthread; 1359125160Sjhb file = fixup_filename(file); 1360125160Sjhb 1361125160Sjhb /* Determine lock list for this lock. */ 1362154077Sjhb if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1363125160Sjhb lock_list = &td->td_sleeplocks; 1364125160Sjhb else 1365125160Sjhb lock_list = PCPU_PTR(spinlocks); 1366125160Sjhb 1367125160Sjhb /* Check to see if we are recursing on a lock we already own. */ 1368125160Sjhb instance = find_instance(*lock_list, lock); 1369125160Sjhb if (instance != NULL) { 1370125160Sjhb instance->li_flags++; 1371125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1372125160Sjhb td->td_proc->p_pid, lock->lo_name, 1373125160Sjhb instance->li_flags & LI_RECURSEMASK); 1374125160Sjhb instance->li_file = file; 1375125160Sjhb instance->li_line = line; 1376125160Sjhb return; 1377110779Speter } 1378125160Sjhb 1379125160Sjhb /* Update per-witness last file and line acquire. */ 138065557Sjasone w->w_file = file; 138165557Sjasone w->w_line = line; 1382125160Sjhb 1383125160Sjhb /* Find the next open lock instance in the list and fill it. */ 138474912Sjhb lle = *lock_list; 138576272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 138678785Sjhb lle = witness_lock_list_get(); 138778785Sjhb if (lle == NULL) 138865557Sjasone return; 138978785Sjhb lle->ll_next = *lock_list; 139087593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 139184680Sjhb td->td_proc->p_pid, lle); 139278785Sjhb *lock_list = lle; 139365557Sjasone } 1394125160Sjhb instance = &lle->ll_children[lle->ll_count++]; 1395125160Sjhb instance->li_lock = lock; 1396125160Sjhb instance->li_line = line; 1397125160Sjhb instance->li_file = file; 139876272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 1399125160Sjhb instance->li_flags = LI_EXCLUSIVE; 140076272Sjhb else 1401125160Sjhb instance->li_flags = 0; 140287593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 140384680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 140465557Sjasone} 140565557Sjasone 140665557Sjasonevoid 140782244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 140882244Sjhb{ 140982244Sjhb struct lock_instance *instance; 141082244Sjhb struct lock_class *class; 141182244Sjhb 1412181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1413182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 141482244Sjhb return; 1415154077Sjhb class = LOCK_CLASS(lock); 1416112116Sjhb file = fixup_filename(file); 1417182473Sattilio if (witness_watch) { 1418182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1419182473Sattilio panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 1420182473Sattilio class->lc_name, lock->lo_name, file, line); 1421182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1422182473Sattilio panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 1423182473Sattilio class->lc_name, lock->lo_name, file, line); 1424182473Sattilio } 142583366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 142682244Sjhb if (instance == NULL) 142782244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 142882244Sjhb class->lc_name, lock->lo_name, file, line); 1429182473Sattilio if (witness_watch) { 1430182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1431182473Sattilio panic("upgrade of exclusive lock (%s) %s @ %s:%d", 1432182473Sattilio class->lc_name, lock->lo_name, file, line); 1433182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1434182473Sattilio panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1435182473Sattilio class->lc_name, lock->lo_name, 1436182473Sattilio instance->li_flags & LI_RECURSEMASK, file, line); 1437182473Sattilio } 143882244Sjhb instance->li_flags |= LI_EXCLUSIVE; 143982244Sjhb} 144082244Sjhb 144182244Sjhbvoid 144282244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 144382244Sjhb int line) 144482244Sjhb{ 144582244Sjhb struct lock_instance *instance; 144682244Sjhb struct lock_class *class; 144782244Sjhb 1448181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1449182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 145082244Sjhb return; 1451154077Sjhb class = LOCK_CLASS(lock); 1452112116Sjhb file = fixup_filename(file); 1453182473Sattilio if (witness_watch) { 1454182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 145582244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 1456182473Sattilio class->lc_name, lock->lo_name, file, line); 1457182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1458182473Sattilio panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 1459182473Sattilio class->lc_name, lock->lo_name, file, line); 1460182473Sattilio } 146183366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 146282244Sjhb if (instance == NULL) 146382244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 146482244Sjhb class->lc_name, lock->lo_name, file, line); 1465182473Sattilio if (witness_watch) { 1466182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1467182473Sattilio panic("downgrade of shared lock (%s) %s @ %s:%d", 1468182473Sattilio class->lc_name, lock->lo_name, file, line); 1469182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1470182473Sattilio panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1471182473Sattilio class->lc_name, lock->lo_name, 1472182473Sattilio instance->li_flags & LI_RECURSEMASK, file, line); 1473182473Sattilio } 147482244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 147582244Sjhb} 147682244Sjhb 147782244Sjhbvoid 147874912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 147965557Sjasone{ 148074912Sjhb struct lock_list_entry **lock_list, *lle; 148176272Sjhb struct lock_instance *instance; 148274912Sjhb struct lock_class *class; 148383366Sjulian struct thread *td; 148492858Simp register_t s; 148574912Sjhb int i, j; 148665557Sjasone 1487182446Sattilio if (witness_cold || lock->lo_witness == NULL || panicstr != NULL) 148871352Sjasone return; 148983366Sjulian td = curthread; 1490154077Sjhb class = LOCK_CLASS(lock); 1491112116Sjhb file = fixup_filename(file); 1492125160Sjhb 1493125160Sjhb /* Find lock instance associated with this lock. */ 149476272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 149583366Sjulian lock_list = &td->td_sleeplocks; 149676272Sjhb else 149774912Sjhb lock_list = PCPU_PTR(spinlocks); 1498181695Sattilio lle = *lock_list; 149974912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 150076272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 150176272Sjhb instance = &(*lock_list)->ll_children[i]; 1502125160Sjhb if (instance->li_lock == lock) 1503125160Sjhb goto found; 150476272Sjhb } 1505182446Sattilio 1506182446Sattilio /* 1507182446Sattilio * When disabling WITNESS through witness_watch we could end up in 1508182473Sattilio * having registered locks in the td_sleeplocks queue. 1509182446Sattilio * We have to make sure we flush these queues, so just search for 1510182473Sattilio * eventual register locks and remove them. 1511182446Sattilio */ 1512182446Sattilio if (witness_watch > 0) 1513182446Sattilio panic("lock (%s) %s not locked @ %s:%d", class->lc_name, 1514182446Sattilio lock->lo_name, file, line); 1515182446Sattilio else 1516182446Sattilio return; 1517125160Sjhbfound: 1518125160Sjhb 1519125160Sjhb /* First, check for shared/exclusive mismatches. */ 1520182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 && 1521125160Sjhb (flags & LOP_EXCLUSIVE) == 0) { 1522125160Sjhb printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1523125160Sjhb lock->lo_name, file, line); 1524125160Sjhb printf("while exclusively locked from %s:%d\n", 1525125160Sjhb instance->li_file, instance->li_line); 1526125160Sjhb panic("excl->ushare"); 1527125160Sjhb } 1528182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 && 1529125160Sjhb (flags & LOP_EXCLUSIVE) != 0) { 1530125160Sjhb printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1531125160Sjhb lock->lo_name, file, line); 1532125160Sjhb printf("while share locked from %s:%d\n", instance->li_file, 1533125160Sjhb instance->li_line); 1534125160Sjhb panic("share->uexcl"); 1535125160Sjhb } 1536125160Sjhb /* If we are recursed, unrecurse. */ 1537125160Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1538125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1539125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1540125160Sjhb instance->li_flags); 1541125160Sjhb instance->li_flags--; 1542125160Sjhb return; 1543125160Sjhb } 1544189194Sthompsa /* The lock is now being dropped, check for NORELEASE flag */ 1545189194Sthompsa if ((instance->li_flags & LI_NORELEASE) != 0 && witness_watch > 0) { 1546189194Sthompsa printf("forbidden unlock of (%s) %s @ %s:%d\n", class->lc_name, 1547189194Sthompsa lock->lo_name, file, line); 1548189194Sthompsa panic("lock marked norelease"); 1549189194Sthompsa } 1550125160Sjhb 1551125160Sjhb /* Otherwise, remove this item from the list. */ 1552125160Sjhb s = intr_disable(); 1553125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1554125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1555125160Sjhb (*lock_list)->ll_count - 1); 1556125160Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 1557125160Sjhb (*lock_list)->ll_children[j] = 1558125160Sjhb (*lock_list)->ll_children[j + 1]; 1559125160Sjhb (*lock_list)->ll_count--; 1560125160Sjhb intr_restore(s); 1561125160Sjhb 1562181695Sattilio /* 1563182984Sattilio * In order to reduce contention on w_mtx, we want to keep always an 1564182984Sattilio * head object into lists so that frequent allocation from the 1565182984Sattilio * free witness pool (and subsequent locking) is avoided. 1566182984Sattilio * In order to maintain the current code simple, when the head 1567182984Sattilio * object is totally unloaded it means also that we do not have 1568182984Sattilio * further objects in the list, so the list ownership needs to be 1569182984Sattilio * hand over to another object if the current head needs to be freed. 1570181695Sattilio */ 1571182984Sattilio if ((*lock_list)->ll_count == 0) { 1572182984Sattilio if (*lock_list == lle) { 1573182984Sattilio if (lle->ll_next == NULL) 1574182984Sattilio return; 1575182984Sattilio } else 1576182984Sattilio lle = *lock_list; 1577125160Sjhb *lock_list = lle->ll_next; 1578125160Sjhb CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1579125160Sjhb td->td_proc->p_pid, lle); 1580125160Sjhb witness_lock_list_free(lle); 1581125160Sjhb } 158265557Sjasone} 158365557Sjasone 1584181695Sattiliovoid 1585181695Sattiliowitness_thread_exit(struct thread *td) 1586181695Sattilio{ 1587181695Sattilio struct lock_list_entry *lle; 1588181695Sattilio int i, n; 1589181695Sattilio 1590181695Sattilio lle = td->td_sleeplocks; 1591181695Sattilio if (lle == NULL || panicstr != NULL) 1592181695Sattilio return; 1593181695Sattilio if (lle->ll_count != 0) { 1594181695Sattilio for (n = 0; lle != NULL; lle = lle->ll_next) 1595181695Sattilio for (i = lle->ll_count - 1; i >= 0; i--) { 1596181695Sattilio if (n == 0) 1597181695Sattilio printf("Thread %p exiting with the following locks held:\n", 1598181695Sattilio td); 1599181695Sattilio n++; 1600181695Sattilio witness_list_lock(&lle->ll_children[i]); 1601181695Sattilio 1602181695Sattilio } 1603181695Sattilio panic("Thread %p cannot exit while holding sleeplocks\n", td); 1604181695Sattilio } 1605181695Sattilio witness_lock_list_free(lle); 1606181695Sattilio} 1607181695Sattilio 160874912Sjhb/* 1609111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 1610111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 1611111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 1612111881Sjhb * console along with a list of the offending locks. If indicated in the 1613111881Sjhb * flags then a failure results in a panic as well. 161474912Sjhb */ 161565557Sjasoneint 1616111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 161765557Sjasone{ 1618183955Sattilio struct lock_list_entry *lock_list, *lle; 161976272Sjhb struct lock_instance *lock1; 162083366Sjulian struct thread *td; 1621111881Sjhb va_list ap; 162274912Sjhb int i, n; 162365557Sjasone 1624182446Sattilio if (witness_cold || witness_watch < 1 || panicstr != NULL) 162574912Sjhb return (0); 162674912Sjhb n = 0; 162783366Sjulian td = curthread; 1628111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 162974912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 163076272Sjhb lock1 = &lle->ll_children[i]; 1631111881Sjhb if (lock1->li_lock == lock) 1632111881Sjhb continue; 1633111881Sjhb if (flags & WARN_GIANTOK && 1634167787Sjhb lock1->li_lock == &Giant.lock_object) 163574912Sjhb continue; 1636111881Sjhb if (flags & WARN_SLEEPOK && 1637111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 163876272Sjhb continue; 1639111881Sjhb if (n == 0) { 1640111881Sjhb va_start(ap, fmt); 1641111881Sjhb vprintf(fmt, ap); 1642111881Sjhb va_end(ap); 1643111881Sjhb printf(" with the following"); 1644111881Sjhb if (flags & WARN_SLEEPOK) 1645111881Sjhb printf(" non-sleepable"); 1646118441Sjhb printf(" locks held:\n"); 164776272Sjhb } 164874912Sjhb n++; 1649111881Sjhb witness_list_lock(lock1); 165074912Sjhb } 1651181695Sattilio 1652183955Sattilio /* 1653183955Sattilio * Pin the thread in order to avoid problems with thread migration. 1654183955Sattilio * Once that all verifies are passed about spinlocks ownership, 1655183955Sattilio * the thread is in a safe path and it can be unpinned. 1656183955Sattilio */ 1657183955Sattilio sched_pin(); 1658183955Sattilio lock_list = PCPU_GET(spinlocks); 1659184098Sattilio if (lock_list != NULL && lock_list->ll_count != 0) { 1660183955Sattilio sched_unpin(); 1661181695Sattilio 166297006Sjhb /* 1663183955Sattilio * We should only have one spinlock and as long as 1664183955Sattilio * the flags cannot match for this locks class, 1665183955Sattilio * check if the first spinlock is the one curthread 1666183955Sattilio * should hold. 166797006Sjhb */ 1668183955Sattilio lock1 = &lock_list->ll_children[lock_list->ll_count - 1]; 1669184098Sattilio if (lock_list->ll_count == 1 && lock_list->ll_next == NULL && 1670184098Sattilio lock1->li_lock == lock && n == 0) 1671184098Sattilio return (0); 1672183955Sattilio 1673184098Sattilio va_start(ap, fmt); 1674184098Sattilio vprintf(fmt, ap); 1675184098Sattilio va_end(ap); 1676184098Sattilio printf(" with the following"); 1677184098Sattilio if (flags & WARN_SLEEPOK) 1678184098Sattilio printf(" non-sleepable"); 1679184098Sattilio printf(" locks held:\n"); 1680183955Sattilio n += witness_list_locks(&lock_list); 1681183955Sattilio } else 1682183955Sattilio sched_unpin(); 1683111881Sjhb if (flags & WARN_PANIC && n) 1684181695Sattilio panic("%s", __func__); 1685181695Sattilio else 1686181695Sattilio witness_debugger(n); 168765557Sjasone return (n); 168865557Sjasone} 168965557Sjasone 1690102448Siedowseconst char * 1691102448Siedowsewitness_file(struct lock_object *lock) 1692102448Siedowse{ 1693102448Siedowse struct witness *w; 1694102448Siedowse 1695182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1696102448Siedowse return ("?"); 1697102448Siedowse w = lock->lo_witness; 1698102448Siedowse return (w->w_file); 1699102448Siedowse} 1700102448Siedowse 1701102448Siedowseint 1702102448Siedowsewitness_line(struct lock_object *lock) 1703102448Siedowse{ 1704102448Siedowse struct witness *w; 1705102448Siedowse 1706182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1707102448Siedowse return (0); 1708102448Siedowse w = lock->lo_witness; 1709102448Siedowse return (w->w_line); 1710102448Siedowse} 1711102448Siedowse 171265856Sjhbstatic struct witness * 171374912Sjhbenroll(const char *description, struct lock_class *lock_class) 171465557Sjasone{ 171574912Sjhb struct witness *w; 1716181695Sattilio struct witness_list *typelist; 171765557Sjasone 1718181695Sattilio MPASS(description != NULL); 1719181695Sattilio 1720182473Sattilio if (witness_watch == -1 || panicstr != NULL) 172165557Sjasone return (NULL); 1722181695Sattilio if ((lock_class->lc_flags & LC_SPINLOCK)) { 1723181695Sattilio if (witness_skipspin) 1724181695Sattilio return (NULL); 1725181695Sattilio else 1726181695Sattilio typelist = &w_spin; 1727181695Sattilio } else if ((lock_class->lc_flags & LC_SLEEPLOCK)) 1728181695Sattilio typelist = &w_sleep; 1729181695Sattilio else 1730181695Sattilio panic("lock class %s is not sleep or spin", 1731181695Sattilio lock_class->lc_name); 1732181695Sattilio 1733181695Sattilio mtx_lock_spin(&w_mtx); 1734181695Sattilio w = witness_hash_get(description); 1735181695Sattilio if (w) 1736181695Sattilio goto found; 1737181695Sattilio if ((w = witness_get()) == NULL) 173865557Sjasone return (NULL); 1739181695Sattilio MPASS(strlen(description) < MAX_W_NAME); 1740181695Sattilio strcpy(w->w_name, description); 174174912Sjhb w->w_class = lock_class; 174275362Sjhb w->w_refcount = 1; 174374912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 1744149441Struckman if (lock_class->lc_flags & LC_SPINLOCK) { 174574912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1746149441Struckman w_spin_cnt++; 1747149441Struckman } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 174874912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1749149441Struckman w_sleep_cnt++; 175075364Sbp } 1751181695Sattilio 1752181695Sattilio /* Insert new witness into the hash */ 1753181695Sattilio witness_hash_put(w); 1754181695Sattilio witness_increment_graph_generation(); 175574912Sjhb mtx_unlock_spin(&w_mtx); 175665557Sjasone return (w); 1757181695Sattiliofound: 1758181695Sattilio w->w_refcount++; 1759181695Sattilio mtx_unlock_spin(&w_mtx); 1760181695Sattilio if (lock_class != w->w_class) 1761181695Sattilio panic( 1762181695Sattilio "lock (%s) %s does not match earlier (%s) lock", 1763181695Sattilio description, lock_class->lc_name, 1764181695Sattilio w->w_class->lc_name); 1765181695Sattilio return (w); 176665557Sjasone} 176765557Sjasone 1768179025Sattiliostatic void 1769112117Sjhbdepart(struct witness *w) 177065557Sjasone{ 177174912Sjhb struct witness_list *list; 177265557Sjasone 1773112117Sjhb MPASS(w->w_refcount == 0); 1774149441Struckman if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1775112117Sjhb list = &w_sleep; 1776149441Struckman w_sleep_cnt--; 1777149441Struckman } else { 1778112117Sjhb list = &w_spin; 1779149441Struckman w_spin_cnt--; 1780149441Struckman } 1781112117Sjhb /* 1782181695Sattilio * Set file to NULL as it may point into a loadable module. 1783112117Sjhb */ 1784181695Sattilio w->w_file = NULL; 1785181695Sattilio w->w_line = 0; 1786181695Sattilio witness_increment_graph_generation(); 1787181695Sattilio} 1788112117Sjhb 1789181695Sattilio 1790181695Sattiliostatic void 1791181695Sattilioadopt(struct witness *parent, struct witness *child) 1792181695Sattilio{ 1793181695Sattilio int pi, ci, i, j; 1794181695Sattilio 1795181695Sattilio if (witness_cold == 0) 1796181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1797181695Sattilio 1798181695Sattilio /* If the relationship is already known, there's no work to be done. */ 1799181695Sattilio if (isitmychild(parent, child)) 1800181695Sattilio return; 1801181695Sattilio 1802181695Sattilio /* When the structure of the graph changes, bump up the generation. */ 1803181695Sattilio witness_increment_graph_generation(); 1804181695Sattilio 1805112117Sjhb /* 1806181695Sattilio * The hard part ... create the direct relationship, then propagate all 1807181695Sattilio * indirect relationships. 1808112117Sjhb */ 1809181695Sattilio pi = parent->w_index; 1810181695Sattilio ci = child->w_index; 1811181695Sattilio WITNESS_INDEX_ASSERT(pi); 1812181695Sattilio WITNESS_INDEX_ASSERT(ci); 1813181695Sattilio MPASS(pi != ci); 1814181695Sattilio w_rmatrix[pi][ci] |= WITNESS_PARENT; 1815181695Sattilio w_rmatrix[ci][pi] |= WITNESS_CHILD; 1816112117Sjhb 1817112117Sjhb /* 1818181695Sattilio * If parent was not already an ancestor of child, 1819181695Sattilio * then we increment the descendant and ancestor counters. 1820112117Sjhb */ 1821181695Sattilio if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) { 1822181695Sattilio parent->w_num_descendants++; 1823181695Sattilio child->w_num_ancestors++; 1824181695Sattilio } 1825112117Sjhb 1826181695Sattilio /* 1827181695Sattilio * Find each ancestor of 'pi'. Note that 'pi' itself is counted as 1828181695Sattilio * an ancestor of 'pi' during this loop. 1829181695Sattilio */ 1830181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 1831181695Sattilio if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 && 1832181695Sattilio (i != pi)) 1833181695Sattilio continue; 1834112117Sjhb 1835181695Sattilio /* Find each descendant of 'i' and mark it as a descendant. */ 1836181695Sattilio for (j = 1; j <= w_max_used_index; j++) { 183774912Sjhb 1838181695Sattilio /* 1839181695Sattilio * Skip children that are already marked as 1840181695Sattilio * descendants of 'i'. 1841181695Sattilio */ 1842181695Sattilio if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) 1843181695Sattilio continue; 1844181695Sattilio 1845181695Sattilio /* 1846181695Sattilio * We are only interested in descendants of 'ci'. Note 1847181695Sattilio * that 'ci' itself is counted as a descendant of 'ci'. 1848181695Sattilio */ 1849181695Sattilio if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 && 1850181695Sattilio (j != ci)) 1851181695Sattilio continue; 1852181695Sattilio w_rmatrix[i][j] |= WITNESS_ANCESTOR; 1853181695Sattilio w_rmatrix[j][i] |= WITNESS_DESCENDANT; 1854181695Sattilio w_data[i].w_num_descendants++; 1855181695Sattilio w_data[j].w_num_ancestors++; 1856181695Sattilio 1857181695Sattilio /* 1858181695Sattilio * Make sure we aren't marking a node as both an 1859181695Sattilio * ancestor and descendant. We should have caught 1860181695Sattilio * this as a lock order reversal earlier. 1861181695Sattilio */ 1862181695Sattilio if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) && 1863181695Sattilio (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) { 1864181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1865181695Sattilio "both ancestor and descendant\n", 1866181695Sattilio i, j, w_rmatrix[i][j]); 1867181695Sattilio kdb_backtrace(); 1868181695Sattilio printf("Witness disabled.\n"); 1869182446Sattilio witness_watch = -1; 1870181695Sattilio } 1871181695Sattilio if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) && 1872181695Sattilio (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) { 1873181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1874181695Sattilio "both ancestor and descendant\n", 1875181695Sattilio j, i, w_rmatrix[j][i]); 1876181695Sattilio kdb_backtrace(); 1877181695Sattilio printf("Witness disabled.\n"); 1878182446Sattilio witness_watch = -1; 1879181695Sattilio } 1880181695Sattilio } 188165557Sjasone } 1882112117Sjhb} 1883112117Sjhb 1884181695Sattiliostatic void 1885112117Sjhbitismychild(struct witness *parent, struct witness *child) 1886112117Sjhb{ 1887112117Sjhb 1888112117Sjhb MPASS(child != NULL && parent != NULL); 1889181695Sattilio if (witness_cold == 0) 1890181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1891181695Sattilio 1892181695Sattilio if (!witness_lock_type_equal(parent, child)) { 1893181695Sattilio if (witness_cold == 0) 1894181695Sattilio mtx_unlock_spin(&w_mtx); 1895181695Sattilio panic("%s: parent \"%s\" (%s) and child \"%s\" (%s) are not " 1896181695Sattilio "the same lock type", __func__, parent->w_name, 1897181695Sattilio parent->w_class->lc_name, child->w_name, 1898112117Sjhb child->w_class->lc_name); 1899181695Sattilio } 1900181695Sattilio adopt(parent, child); 190165557Sjasone} 190265557Sjasone 1903181695Sattilio/* 1904181695Sattilio * Generic code for the isitmy*() functions. The rmask parameter is the 1905181695Sattilio * expected relationship of w1 to w2. 1906181695Sattilio */ 1907181695Sattiliostatic int 1908181695Sattilio_isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname) 190965557Sjasone{ 1910181695Sattilio unsigned char r1, r2; 1911181695Sattilio int i1, i2; 191265557Sjasone 1913181695Sattilio i1 = w1->w_index; 1914181695Sattilio i2 = w2->w_index; 1915181695Sattilio WITNESS_INDEX_ASSERT(i1); 1916181695Sattilio WITNESS_INDEX_ASSERT(i2); 1917181695Sattilio r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK; 1918181695Sattilio r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK; 1919181695Sattilio 1920181695Sattilio /* The flags on one better be the inverse of the flags on the other */ 1921181695Sattilio if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) || 1922181695Sattilio (WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) { 1923181695Sattilio printf("%s: rmatrix mismatch between %s (index %d) and %s " 1924181695Sattilio "(index %d): w_rmatrix[%d][%d] == %hhx but " 1925181695Sattilio "w_rmatrix[%d][%d] == %hhx\n", 1926181695Sattilio fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1, 1927181695Sattilio i2, i1, r2); 1928181695Sattilio kdb_backtrace(); 1929181695Sattilio printf("Witness disabled.\n"); 1930182446Sattilio witness_watch = -1; 1931181695Sattilio } 1932181695Sattilio return (r1 & rmask); 193365557Sjasone} 193465557Sjasone 1935181695Sattilio/* 1936181695Sattilio * Checks if @child is a direct child of @parent. 1937181695Sattilio */ 193865557Sjasonestatic int 193965856Sjhbisitmychild(struct witness *parent, struct witness *child) 194065557Sjasone{ 194165557Sjasone 1942181695Sattilio return (_isitmyx(parent, child, WITNESS_PARENT, __func__)); 194365557Sjasone} 194465557Sjasone 1945181695Sattilio/* 1946181695Sattilio * Checks if @descendant is a direct or inderect descendant of @ancestor. 1947181695Sattilio */ 194865557Sjasonestatic int 1949181695Sattilioisitmydescendant(struct witness *ancestor, struct witness *descendant) 195065557Sjasone{ 195165557Sjasone 1952181695Sattilio return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK, 1953181695Sattilio __func__)); 195465557Sjasone} 195565557Sjasone 1956105508Sphk#ifdef BLESSING 195765557Sjasonestatic int 195865856Sjhbblessed(struct witness *w1, struct witness *w2) 195965557Sjasone{ 196065557Sjasone int i; 196165856Sjhb struct witness_blessed *b; 196265557Sjasone 196365557Sjasone for (i = 0; i < blessed_count; i++) { 196465557Sjasone b = &blessed_list[i]; 196574912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 196674912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 196765557Sjasone return (1); 196865557Sjasone continue; 196965557Sjasone } 197074912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 197174912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 197265557Sjasone return (1); 197365557Sjasone } 197465557Sjasone return (0); 197565557Sjasone} 1976105508Sphk#endif 197765557Sjasone 197865856Sjhbstatic struct witness * 197974912Sjhbwitness_get(void) 198065557Sjasone{ 198165856Sjhb struct witness *w; 1982181695Sattilio int index; 198365557Sjasone 1984181695Sattilio if (witness_cold == 0) 1985181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1986181695Sattilio 1987182473Sattilio if (witness_watch == -1) { 198876481Sjhb mtx_unlock_spin(&w_mtx); 198976481Sjhb return (NULL); 199076481Sjhb } 199174912Sjhb if (STAILQ_EMPTY(&w_free)) { 1992182446Sattilio witness_watch = -1; 199374912Sjhb mtx_unlock_spin(&w_mtx); 1994181695Sattilio printf("WITNESS: unable to allocate a new witness object\n"); 199565557Sjasone return (NULL); 199665557Sjasone } 199774912Sjhb w = STAILQ_FIRST(&w_free); 199874912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 1999149441Struckman w_free_cnt--; 2000181695Sattilio index = w->w_index; 2001181695Sattilio MPASS(index > 0 && index == w_max_used_index+1 && 2002181695Sattilio index < WITNESS_COUNT); 200365856Sjhb bzero(w, sizeof(*w)); 2004181695Sattilio w->w_index = index; 2005181695Sattilio if (index > w_max_used_index) 2006181695Sattilio w_max_used_index = index; 200765557Sjasone return (w); 200865557Sjasone} 200965557Sjasone 201065557Sjasonestatic void 201165856Sjhbwitness_free(struct witness *w) 201265557Sjasone{ 201374912Sjhb 201474912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 2015149441Struckman w_free_cnt++; 201665557Sjasone} 201765557Sjasone 201874912Sjhbstatic struct lock_list_entry * 201974912Sjhbwitness_lock_list_get(void) 202074912Sjhb{ 202174912Sjhb struct lock_list_entry *lle; 202271709Sjhb 2023182446Sattilio if (witness_watch == -1) 202476481Sjhb return (NULL); 202574912Sjhb mtx_lock_spin(&w_mtx); 202674912Sjhb lle = w_lock_list_free; 202774912Sjhb if (lle == NULL) { 2028182446Sattilio witness_watch = -1; 202974912Sjhb mtx_unlock_spin(&w_mtx); 203074912Sjhb printf("%s: witness exhausted\n", __func__); 203174912Sjhb return (NULL); 203274912Sjhb } 203374912Sjhb w_lock_list_free = lle->ll_next; 203474912Sjhb mtx_unlock_spin(&w_mtx); 203574912Sjhb bzero(lle, sizeof(*lle)); 203674912Sjhb return (lle); 203774912Sjhb} 203874912Sjhb 203974912Sjhbstatic void 204074912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 204171709Sjhb{ 204271709Sjhb 204374912Sjhb mtx_lock_spin(&w_mtx); 204474912Sjhb lle->ll_next = w_lock_list_free; 204574912Sjhb w_lock_list_free = lle; 204674912Sjhb mtx_unlock_spin(&w_mtx); 204771709Sjhb} 204871709Sjhb 204976272Sjhbstatic struct lock_instance * 2050181695Sattiliofind_instance(struct lock_list_entry *list, struct lock_object *lock) 205176272Sjhb{ 205276272Sjhb struct lock_list_entry *lle; 205376272Sjhb struct lock_instance *instance; 205476272Sjhb int i; 205576272Sjhb 2056181695Sattilio for (lle = list; lle != NULL; lle = lle->ll_next) 205776272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 205876272Sjhb instance = &lle->ll_children[i]; 205976272Sjhb if (instance->li_lock == lock) 206076272Sjhb return (instance); 206176272Sjhb } 206276272Sjhb return (NULL); 206376272Sjhb} 206476272Sjhb 2065111881Sjhbstatic void 2066111881Sjhbwitness_list_lock(struct lock_instance *instance) 2067111881Sjhb{ 2068111881Sjhb struct lock_object *lock; 2069111881Sjhb 2070111881Sjhb lock = instance->li_lock; 2071111881Sjhb printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 2072154077Sjhb "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 2073179025Sattilio if (lock->lo_witness->w_name != lock->lo_name) 2074179025Sattilio printf(" (%s)", lock->lo_witness->w_name); 2075111881Sjhb printf(" r = %d (%p) locked @ %s:%d\n", 2076111881Sjhb instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 2077111881Sjhb instance->li_line); 2078111881Sjhb} 2079111881Sjhb 2080140637Srwatson#ifdef DDB 2081139333Srwatsonstatic int 2082139333Srwatsonwitness_thread_has_locks(struct thread *td) 2083139333Srwatson{ 2084139333Srwatson 2085182984Sattilio if (td->td_sleeplocks == NULL) 2086182984Sattilio return (0); 2087182984Sattilio return (td->td_sleeplocks->ll_count != 0); 2088139333Srwatson} 2089139333Srwatson 2090139333Srwatsonstatic int 2091139333Srwatsonwitness_proc_has_locks(struct proc *p) 2092139333Srwatson{ 2093139333Srwatson struct thread *td; 2094139333Srwatson 2095139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2096139333Srwatson if (witness_thread_has_locks(td)) 2097139333Srwatson return (1); 2098139333Srwatson } 2099139333Srwatson return (0); 2100139333Srwatson} 2101140637Srwatson#endif 2102139333Srwatson 210374912Sjhbint 210475273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 210572224Sjhb{ 210675273Sjhb struct lock_list_entry *lle; 210774912Sjhb int i, nheld; 210872224Sjhb 210974912Sjhb nheld = 0; 211074912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 211174912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 2112111881Sjhb witness_list_lock(&lle->ll_children[i]); 211374912Sjhb nheld++; 211474912Sjhb } 211575273Sjhb return (nheld); 211675273Sjhb} 211775273Sjhb 2118118271Sjhb/* 2119118271Sjhb * This is a bit risky at best. We call this function when we have timed 2120118271Sjhb * out acquiring a spin lock, and we assume that the other CPU is stuck 2121118271Sjhb * with this lock held. So, we go groveling around in the other CPU's 2122118271Sjhb * per-cpu data to try to find the lock instance for this spin lock to 2123118271Sjhb * see when it was last acquired. 2124118271Sjhb */ 212565557Sjasonevoid 2126118271Sjhbwitness_display_spinlock(struct lock_object *lock, struct thread *owner) 2127118271Sjhb{ 2128118271Sjhb struct lock_instance *instance; 2129118271Sjhb struct pcpu *pc; 2130118271Sjhb 2131118271Sjhb if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 2132118271Sjhb return; 2133118271Sjhb pc = pcpu_find(owner->td_oncpu); 2134118271Sjhb instance = find_instance(pc->pc_spinlocks, lock); 2135118271Sjhb if (instance != NULL) 2136118271Sjhb witness_list_lock(instance); 2137118271Sjhb} 2138118271Sjhb 2139118271Sjhbvoid 214074912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 214165557Sjasone{ 2142153854Sjhb struct lock_list_entry *lock_list; 214376272Sjhb struct lock_instance *instance; 2144154077Sjhb struct lock_class *class; 214571320Sjasone 2146181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2147182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 214871352Sjasone return; 2149154077Sjhb class = LOCK_CLASS(lock); 2150154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2151153854Sjhb lock_list = curthread->td_sleeplocks; 2152153854Sjhb else { 2153153854Sjhb if (witness_skipspin) 2154153854Sjhb return; 2155153854Sjhb lock_list = PCPU_GET(spinlocks); 2156153854Sjhb } 2157153854Sjhb instance = find_instance(lock_list, lock); 215882243Sjhb if (instance == NULL) 215982243Sjhb panic("%s: lock (%s) %s not locked", __func__, 2160154077Sjhb class->lc_name, lock->lo_name); 216176272Sjhb *filep = instance->li_file; 216276272Sjhb *linep = instance->li_line; 216365557Sjasone} 216465557Sjasone 216565557Sjasonevoid 216674912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 216765557Sjasone{ 2168153854Sjhb struct lock_list_entry *lock_list; 216976272Sjhb struct lock_instance *instance; 2170154077Sjhb struct lock_class *class; 217171320Sjasone 2172181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2173182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 217471352Sjasone return; 2175154077Sjhb class = LOCK_CLASS(lock); 2176154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2177153854Sjhb lock_list = curthread->td_sleeplocks; 2178153854Sjhb else { 2179153854Sjhb if (witness_skipspin) 2180153854Sjhb return; 2181153854Sjhb lock_list = PCPU_GET(spinlocks); 2182153854Sjhb } 2183153854Sjhb instance = find_instance(lock_list, lock); 218482243Sjhb if (instance == NULL) 218582243Sjhb panic("%s: lock (%s) %s not locked", __func__, 2186154077Sjhb class->lc_name, lock->lo_name); 218774912Sjhb lock->lo_witness->w_file = file; 218874912Sjhb lock->lo_witness->w_line = line; 218976272Sjhb instance->li_file = file; 219076272Sjhb instance->li_line = line; 219165557Sjasone} 219265557Sjasone 219378871Sjhbvoid 219478871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 219578871Sjhb{ 219678871Sjhb#ifdef INVARIANT_SUPPORT 219778871Sjhb struct lock_instance *instance; 2198154077Sjhb struct lock_class *class; 219978871Sjhb 2200182446Sattilio if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL) 220178941Sjhb return; 2202154077Sjhb class = LOCK_CLASS(lock); 2203154077Sjhb if ((class->lc_flags & LC_SLEEPLOCK) != 0) 220483366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 2205154077Sjhb else if ((class->lc_flags & LC_SPINLOCK) != 0) 220678871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 220786422Sjhb else { 220878871Sjhb panic("Lock (%s) %s is not sleep or spin!", 2209154077Sjhb class->lc_name, lock->lo_name); 221086422Sjhb } 2211112116Sjhb file = fixup_filename(file); 221278871Sjhb switch (flags) { 221378871Sjhb case LA_UNLOCKED: 221478871Sjhb if (instance != NULL) 221578871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 2216154077Sjhb class->lc_name, lock->lo_name, file, line); 221778871Sjhb break; 221878871Sjhb case LA_LOCKED: 221978871Sjhb case LA_LOCKED | LA_RECURSED: 222078871Sjhb case LA_LOCKED | LA_NOTRECURSED: 222178871Sjhb case LA_SLOCKED: 222278871Sjhb case LA_SLOCKED | LA_RECURSED: 222378871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 222478871Sjhb case LA_XLOCKED: 222578871Sjhb case LA_XLOCKED | LA_RECURSED: 222678871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 222786422Sjhb if (instance == NULL) { 222878871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 2229154077Sjhb class->lc_name, lock->lo_name, file, line); 223086422Sjhb break; 223186422Sjhb } 223278871Sjhb if ((flags & LA_XLOCKED) != 0 && 223378871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 223478871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 2235154077Sjhb class->lc_name, lock->lo_name, file, line); 223678871Sjhb if ((flags & LA_SLOCKED) != 0 && 223778871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 223878871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 2239154077Sjhb class->lc_name, lock->lo_name, file, line); 224078871Sjhb if ((flags & LA_RECURSED) != 0 && 224178871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 224278871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 2243154077Sjhb class->lc_name, lock->lo_name, file, line); 224478871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 224578871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 224678871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 2247154077Sjhb class->lc_name, lock->lo_name, file, line); 224878871Sjhb break; 224978871Sjhb default: 225078871Sjhb panic("Invalid lock assertion at %s:%d.", file, line); 225178871Sjhb 225278871Sjhb } 225378871Sjhb#endif /* INVARIANT_SUPPORT */ 225478871Sjhb} 225578871Sjhb 2256187511Sthompsastatic void 2257187511Sthompsawitness_setflag(struct lock_object *lock, int flag, int set) 2258187511Sthompsa{ 2259187511Sthompsa struct lock_list_entry *lock_list; 2260187511Sthompsa struct lock_instance *instance; 2261187511Sthompsa struct lock_class *class; 2262187511Sthompsa 2263187511Sthompsa if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 2264187511Sthompsa return; 2265187511Sthompsa class = LOCK_CLASS(lock); 2266187511Sthompsa if (class->lc_flags & LC_SLEEPLOCK) 2267187511Sthompsa lock_list = curthread->td_sleeplocks; 2268187511Sthompsa else { 2269187511Sthompsa if (witness_skipspin) 2270187511Sthompsa return; 2271187511Sthompsa lock_list = PCPU_GET(spinlocks); 2272187511Sthompsa } 2273187511Sthompsa instance = find_instance(lock_list, lock); 2274187511Sthompsa if (instance == NULL) 2275187511Sthompsa panic("%s: lock (%s) %s not locked", __func__, 2276187511Sthompsa class->lc_name, lock->lo_name); 2277187511Sthompsa 2278187511Sthompsa if (set) 2279187511Sthompsa instance->li_flags |= flag; 2280187511Sthompsa else 2281187511Sthompsa instance->li_flags &= ~flag; 2282187511Sthompsa} 2283187511Sthompsa 2284187511Sthompsavoid 2285187511Sthompsawitness_norelease(struct lock_object *lock) 2286187511Sthompsa{ 2287187511Sthompsa 2288187511Sthompsa witness_setflag(lock, LI_NORELEASE, 1); 2289187511Sthompsa} 2290187511Sthompsa 2291187511Sthompsavoid 2292187511Sthompsawitness_releaseok(struct lock_object *lock) 2293187511Sthompsa{ 2294187511Sthompsa 2295187511Sthompsa witness_setflag(lock, LI_NORELEASE, 0); 2296187511Sthompsa} 2297187511Sthompsa 229874912Sjhb#ifdef DDB 2299112061Sjhbstatic void 2300181695Sattiliowitness_ddb_list(struct thread *td) 2301112061Sjhb{ 230274912Sjhb 2303181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2304131930Smarcel KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 2305112061Sjhb 2306182446Sattilio if (witness_watch < 1) 2307112061Sjhb return; 2308112061Sjhb 2309112061Sjhb witness_list_locks(&td->td_sleeplocks); 2310112061Sjhb 2311112061Sjhb /* 2312112061Sjhb * We only handle spinlocks if td == curthread. This is somewhat broken 2313112061Sjhb * if td is currently executing on some other CPU and holds spin locks 2314112061Sjhb * as we won't display those locks. If we had a MI way of getting 2315112061Sjhb * the per-cpu data for a given cpu then we could use 2316113339Sjulian * td->td_oncpu to get the list of spinlocks for this thread 2317112061Sjhb * and "fix" this. 2318112061Sjhb * 2319170302Sjeff * That still wouldn't really fix this unless we locked the scheduler 2320170302Sjeff * lock or stopped the other CPU to make sure it wasn't changing the 2321170302Sjeff * list out from under us. It is probably best to just not try to 2322170302Sjeff * handle threads on other CPU's for now. 2323112061Sjhb */ 2324112061Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 2325112061Sjhb witness_list_locks(PCPU_PTR(spinlocks)); 2326112061Sjhb} 2327112061Sjhb 232874930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 232974912Sjhb{ 233083366Sjulian struct thread *td; 233174912Sjhb 2332158030Sjhb if (have_addr) 2333158030Sjhb td = db_lookup_thread(addr, TRUE); 2334158030Sjhb else 2335158030Sjhb td = kdb_thread; 2336181695Sattilio witness_ddb_list(td); 233774912Sjhb} 233874912Sjhb 2339183054SsamDB_SHOW_ALL_COMMAND(locks, db_witness_list_all) 2340139333Srwatson{ 2341139333Srwatson struct thread *td; 2342139333Srwatson struct proc *p; 2343139333Srwatson 2344139333Srwatson /* 2345139333Srwatson * It would be nice to list only threads and processes that actually 2346139333Srwatson * held sleep locks, but that information is currently not exported 2347139333Srwatson * by WITNESS. 2348139333Srwatson */ 2349139333Srwatson FOREACH_PROC_IN_SYSTEM(p) { 2350139333Srwatson if (!witness_proc_has_locks(p)) 2351139333Srwatson continue; 2352139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2353139333Srwatson if (!witness_thread_has_locks(td)) 2354139333Srwatson continue; 2355153853Sjhb db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2356182984Sattilio p->p_comm, td, td->td_tid); 2357181695Sattilio witness_ddb_list(td); 2358139333Srwatson } 2359139333Srwatson } 2360139333Srwatson} 2361183054SsamDB_SHOW_ALIAS(alllocks, db_witness_list_all) 2362139333Srwatson 236374912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 236474912Sjhb{ 236574912Sjhb 2366181695Sattilio witness_ddb_display(db_printf); 236774912Sjhb} 236874912Sjhb#endif 2369181695Sattilio 2370181695Sattiliostatic int 2371181695Sattiliosysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS) 2372181695Sattilio{ 2373181695Sattilio struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2; 2374181695Sattilio struct witness *tmp_w1, *tmp_w2, *w1, *w2; 2375181695Sattilio struct sbuf *sb; 2376181695Sattilio u_int w_rmatrix1, w_rmatrix2; 2377181695Sattilio int error, generation, i, j; 2378181695Sattilio 2379181695Sattilio tmp_data1 = NULL; 2380181695Sattilio tmp_data2 = NULL; 2381181695Sattilio tmp_w1 = NULL; 2382181695Sattilio tmp_w2 = NULL; 2383182446Sattilio if (witness_watch < 1) { 2384181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2385181695Sattilio return (error); 2386181695Sattilio } 2387181695Sattilio if (witness_cold) { 2388181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2389181695Sattilio return (error); 2390181695Sattilio } 2391181695Sattilio error = 0; 2392181695Sattilio sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND); 2393181695Sattilio if (sb == NULL) 2394181695Sattilio return (ENOMEM); 2395181695Sattilio 2396181695Sattilio /* Allocate and init temporary storage space. */ 2397181695Sattilio tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2398181695Sattilio tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2399181695Sattilio tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2400181695Sattilio M_WAITOK | M_ZERO); 2401181695Sattilio tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2402181695Sattilio M_WAITOK | M_ZERO); 2403181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2404181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2405181695Sattilio 2406181695Sattiliorestart: 2407181695Sattilio mtx_lock_spin(&w_mtx); 2408181695Sattilio generation = w_generation; 2409181695Sattilio mtx_unlock_spin(&w_mtx); 2410181695Sattilio sbuf_printf(sb, "Number of known direct relationships is %d\n", 2411181695Sattilio w_lohash.wloh_count); 2412181695Sattilio for (i = 1; i < w_max_used_index; i++) { 2413181695Sattilio mtx_lock_spin(&w_mtx); 2414181695Sattilio if (generation != w_generation) { 2415181695Sattilio mtx_unlock_spin(&w_mtx); 2416181695Sattilio 2417181695Sattilio /* The graph has changed, try again. */ 2418181695Sattilio req->oldidx = 0; 2419181695Sattilio sbuf_clear(sb); 2420181695Sattilio goto restart; 2421181695Sattilio } 2422181695Sattilio 2423181695Sattilio w1 = &w_data[i]; 2424181695Sattilio if (w1->w_reversed == 0) { 2425181695Sattilio mtx_unlock_spin(&w_mtx); 2426181695Sattilio continue; 2427181695Sattilio } 2428181695Sattilio 2429181695Sattilio /* Copy w1 locally so we can release the spin lock. */ 2430181695Sattilio *tmp_w1 = *w1; 2431181695Sattilio mtx_unlock_spin(&w_mtx); 2432181695Sattilio 2433181695Sattilio if (tmp_w1->w_reversed == 0) 2434181695Sattilio continue; 2435181695Sattilio for (j = 1; j < w_max_used_index; j++) { 2436181695Sattilio if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j) 2437181695Sattilio continue; 2438181695Sattilio 2439181695Sattilio mtx_lock_spin(&w_mtx); 2440181695Sattilio if (generation != w_generation) { 2441181695Sattilio mtx_unlock_spin(&w_mtx); 2442181695Sattilio 2443181695Sattilio /* The graph has changed, try again. */ 2444181695Sattilio req->oldidx = 0; 2445181695Sattilio sbuf_clear(sb); 2446181695Sattilio goto restart; 2447181695Sattilio } 2448181695Sattilio 2449181695Sattilio w2 = &w_data[j]; 2450181695Sattilio data1 = witness_lock_order_get(w1, w2); 2451181695Sattilio data2 = witness_lock_order_get(w2, w1); 2452181695Sattilio 2453181695Sattilio /* 2454181695Sattilio * Copy information locally so we can release the 2455181695Sattilio * spin lock. 2456181695Sattilio */ 2457181695Sattilio *tmp_w2 = *w2; 2458181695Sattilio w_rmatrix1 = (unsigned int)w_rmatrix[i][j]; 2459181695Sattilio w_rmatrix2 = (unsigned int)w_rmatrix[j][i]; 2460181695Sattilio 2461181695Sattilio if (data1) { 2462181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2463181695Sattilio stack_copy(&data1->wlod_stack, 2464181695Sattilio &tmp_data1->wlod_stack); 2465181695Sattilio } 2466181695Sattilio if (data2 && data2 != data1) { 2467181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2468181695Sattilio stack_copy(&data2->wlod_stack, 2469181695Sattilio &tmp_data2->wlod_stack); 2470181695Sattilio } 2471181695Sattilio mtx_unlock_spin(&w_mtx); 2472181695Sattilio 2473181695Sattilio sbuf_printf(sb, 2474181695Sattilio "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n", 2475181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2476181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2477181695Sattilio#if 0 2478181695Sattilio sbuf_printf(sb, 2479181695Sattilio "w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n", 2480181695Sattilio tmp_w1->name, tmp_w2->w_name, w_rmatrix1, 2481181695Sattilio tmp_w2->name, tmp_w1->w_name, w_rmatrix2); 2482181695Sattilio#endif 2483181695Sattilio if (data1) { 2484181695Sattilio sbuf_printf(sb, 2485181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2486181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2487181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2488181695Sattilio stack_sbuf_print(sb, &tmp_data1->wlod_stack); 2489181695Sattilio sbuf_printf(sb, "\n"); 2490181695Sattilio } 2491181695Sattilio if (data2 && data2 != data1) { 2492181695Sattilio sbuf_printf(sb, 2493181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2494181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name, 2495181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name); 2496181695Sattilio stack_sbuf_print(sb, &tmp_data2->wlod_stack); 2497181695Sattilio sbuf_printf(sb, "\n"); 2498181695Sattilio } 2499181695Sattilio } 2500181695Sattilio } 2501181695Sattilio mtx_lock_spin(&w_mtx); 2502181695Sattilio if (generation != w_generation) { 2503181695Sattilio mtx_unlock_spin(&w_mtx); 2504181695Sattilio 2505181695Sattilio /* 2506181695Sattilio * The graph changed while we were printing stack data, 2507181695Sattilio * try again. 2508181695Sattilio */ 2509181695Sattilio req->oldidx = 0; 2510181695Sattilio sbuf_clear(sb); 2511181695Sattilio goto restart; 2512181695Sattilio } 2513181695Sattilio mtx_unlock_spin(&w_mtx); 2514181695Sattilio 2515181695Sattilio /* Free temporary storage space. */ 2516181695Sattilio free(tmp_data1, M_TEMP); 2517181695Sattilio free(tmp_data2, M_TEMP); 2518181695Sattilio free(tmp_w1, M_TEMP); 2519181695Sattilio free(tmp_w2, M_TEMP); 2520181695Sattilio 2521181695Sattilio sbuf_finish(sb); 2522181695Sattilio error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2523181695Sattilio sbuf_delete(sb); 2524181695Sattilio 2525181695Sattilio return (error); 2526181695Sattilio} 2527181695Sattilio 2528181695Sattiliostatic int 2529181695Sattiliosysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS) 2530181695Sattilio{ 2531181695Sattilio struct witness *w; 2532181695Sattilio struct sbuf *sb; 2533181695Sattilio int error; 2534181695Sattilio 2535182446Sattilio if (witness_watch < 1) { 2536181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2537181695Sattilio return (error); 2538181695Sattilio } 2539181695Sattilio if (witness_cold) { 2540181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2541181695Sattilio return (error); 2542181695Sattilio } 2543181695Sattilio error = 0; 2544181695Sattilio sb = sbuf_new(NULL, NULL, FULLGRAPH_SBUF_SIZE, SBUF_FIXEDLEN); 2545181695Sattilio if (sb == NULL) 2546181695Sattilio return (ENOMEM); 2547181695Sattilio sbuf_printf(sb, "\n"); 2548181695Sattilio 2549181695Sattilio mtx_lock_spin(&w_mtx); 2550181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2551181695Sattilio w->w_displayed = 0; 2552181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2553181695Sattilio witness_add_fullgraph(sb, w); 2554181695Sattilio mtx_unlock_spin(&w_mtx); 2555181695Sattilio 2556181695Sattilio /* 2557181695Sattilio * While using SBUF_FIXEDLEN, check if the sbuf overflowed. 2558181695Sattilio */ 2559181695Sattilio if (sbuf_overflowed(sb)) { 2560181695Sattilio sbuf_delete(sb); 2561181695Sattilio panic("%s: sbuf overflowed, bump FULLGRAPH_SBUF_SIZE value\n", 2562181695Sattilio __func__); 2563181695Sattilio } 2564181695Sattilio 2565181695Sattilio /* 2566181695Sattilio * Close the sbuf and return to userland. 2567181695Sattilio */ 2568181695Sattilio sbuf_finish(sb); 2569181695Sattilio error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2570181695Sattilio sbuf_delete(sb); 2571181695Sattilio 2572181695Sattilio return (error); 2573181695Sattilio} 2574181695Sattilio 2575181695Sattiliostatic int 2576181695Sattiliosysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 2577181695Sattilio{ 2578181695Sattilio int error, value; 2579181695Sattilio 2580182473Sattilio value = witness_watch; 2581181695Sattilio error = sysctl_handle_int(oidp, &value, 0, req); 2582181695Sattilio if (error != 0 || req->newptr == NULL) 2583181695Sattilio return (error); 2584182446Sattilio if (value > 1 || value < -1 || 2585182446Sattilio (witness_watch == -1 && value != witness_watch)) 2586181695Sattilio return (EINVAL); 2587182446Sattilio witness_watch = value; 2588181695Sattilio return (0); 2589181695Sattilio} 2590181695Sattilio 2591181695Sattiliostatic void 2592181695Sattiliowitness_add_fullgraph(struct sbuf *sb, struct witness *w) 2593181695Sattilio{ 2594181695Sattilio int i; 2595181695Sattilio 2596181695Sattilio if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0)) 2597181695Sattilio return; 2598181695Sattilio w->w_displayed = 1; 2599181695Sattilio 2600181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 2601181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 2602181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) { 2603181695Sattilio sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name, 2604181695Sattilio w_data[i].w_name); 2605181695Sattilio witness_add_fullgraph(sb, &w_data[i]); 2606181695Sattilio } 2607181695Sattilio } 2608181695Sattilio} 2609181695Sattilio 2610181695Sattilio/* 2611181695Sattilio * A simple hash function. Takes a key pointer and a key size. If size == 0, 2612181695Sattilio * interprets the key as a string and reads until the null 2613181695Sattilio * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit 2614181695Sattilio * hash value computed from the key. 2615181695Sattilio */ 2616181695Sattiliostatic uint32_t 2617181695Sattiliowitness_hash_djb2(const uint8_t *key, uint32_t size) 2618181695Sattilio{ 2619181695Sattilio unsigned int hash = 5381; 2620181695Sattilio int i; 2621181695Sattilio 2622181695Sattilio /* hash = hash * 33 + key[i] */ 2623181695Sattilio if (size) 2624181695Sattilio for (i = 0; i < size; i++) 2625181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2626181695Sattilio else 2627181695Sattilio for (i = 0; key[i] != 0; i++) 2628181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2629181695Sattilio 2630181695Sattilio return (hash); 2631181695Sattilio} 2632181695Sattilio 2633181695Sattilio 2634181695Sattilio/* 2635181695Sattilio * Initializes the two witness hash tables. Called exactly once from 2636181695Sattilio * witness_initialize(). 2637181695Sattilio */ 2638181695Sattiliostatic void 2639181695Sattiliowitness_init_hash_tables(void) 2640181695Sattilio{ 2641181695Sattilio int i; 2642181695Sattilio 2643181695Sattilio MPASS(witness_cold); 2644181695Sattilio 2645181695Sattilio /* Initialize the hash tables. */ 2646181695Sattilio for (i = 0; i < WITNESS_HASH_SIZE; i++) 2647181695Sattilio w_hash.wh_array[i] = NULL; 2648181695Sattilio 2649181695Sattilio w_hash.wh_size = WITNESS_HASH_SIZE; 2650181695Sattilio w_hash.wh_count = 0; 2651181695Sattilio 2652181695Sattilio /* Initialize the lock order data hash. */ 2653181695Sattilio w_lofree = NULL; 2654181695Sattilio for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) { 2655181695Sattilio memset(&w_lodata[i], 0, sizeof(w_lodata[i])); 2656181695Sattilio w_lodata[i].wlod_next = w_lofree; 2657181695Sattilio w_lofree = &w_lodata[i]; 2658181695Sattilio } 2659181695Sattilio w_lohash.wloh_size = WITNESS_LO_HASH_SIZE; 2660181695Sattilio w_lohash.wloh_count = 0; 2661181695Sattilio for (i = 0; i < WITNESS_LO_HASH_SIZE; i++) 2662181695Sattilio w_lohash.wloh_array[i] = NULL; 2663181695Sattilio} 2664181695Sattilio 2665181695Sattiliostatic struct witness * 2666181695Sattiliowitness_hash_get(const char *key) 2667181695Sattilio{ 2668181695Sattilio struct witness *w; 2669181695Sattilio uint32_t hash; 2670181695Sattilio 2671181695Sattilio MPASS(key != NULL); 2672181695Sattilio if (witness_cold == 0) 2673181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2674181695Sattilio hash = witness_hash_djb2(key, 0) % w_hash.wh_size; 2675181695Sattilio w = w_hash.wh_array[hash]; 2676181695Sattilio while (w != NULL) { 2677181695Sattilio if (strcmp(w->w_name, key) == 0) 2678181695Sattilio goto out; 2679181695Sattilio w = w->w_hash_next; 2680181695Sattilio } 2681181695Sattilio 2682181695Sattilioout: 2683181695Sattilio return (w); 2684181695Sattilio} 2685181695Sattilio 2686181695Sattiliostatic void 2687181695Sattiliowitness_hash_put(struct witness *w) 2688181695Sattilio{ 2689181695Sattilio uint32_t hash; 2690181695Sattilio 2691181695Sattilio MPASS(w != NULL); 2692181695Sattilio MPASS(w->w_name != NULL); 2693181695Sattilio if (witness_cold == 0) 2694181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2695181695Sattilio KASSERT(witness_hash_get(w->w_name) == NULL, 2696181695Sattilio ("%s: trying to add a hash entry that already exists!", __func__)); 2697181695Sattilio KASSERT(w->w_hash_next == NULL, 2698181695Sattilio ("%s: w->w_hash_next != NULL", __func__)); 2699181695Sattilio 2700181695Sattilio hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size; 2701181695Sattilio w->w_hash_next = w_hash.wh_array[hash]; 2702181695Sattilio w_hash.wh_array[hash] = w; 2703181695Sattilio w_hash.wh_count++; 2704181695Sattilio} 2705181695Sattilio 2706181695Sattilio 2707181695Sattiliostatic struct witness_lock_order_data * 2708181695Sattiliowitness_lock_order_get(struct witness *parent, struct witness *child) 2709181695Sattilio{ 2710181695Sattilio struct witness_lock_order_data *data = NULL; 2711181695Sattilio struct witness_lock_order_key key; 2712181695Sattilio unsigned int hash; 2713181695Sattilio 2714181695Sattilio MPASS(parent != NULL && child != NULL); 2715181695Sattilio key.from = parent->w_index; 2716181695Sattilio key.to = child->w_index; 2717181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2718181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2719181695Sattilio if ((w_rmatrix[parent->w_index][child->w_index] 2720181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) == 0) 2721181695Sattilio goto out; 2722181695Sattilio 2723181695Sattilio hash = witness_hash_djb2((const char*)&key, 2724181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2725181695Sattilio data = w_lohash.wloh_array[hash]; 2726181695Sattilio while (data != NULL) { 2727181695Sattilio if (witness_lock_order_key_equal(&data->wlod_key, &key)) 2728181695Sattilio break; 2729181695Sattilio data = data->wlod_next; 2730181695Sattilio } 2731181695Sattilio 2732181695Sattilioout: 2733181695Sattilio return (data); 2734181695Sattilio} 2735181695Sattilio 2736181695Sattilio/* 2737181695Sattilio * Verify that parent and child have a known relationship, are not the same, 2738181695Sattilio * and child is actually a child of parent. This is done without w_mtx 2739181695Sattilio * to avoid contention in the common case. 2740181695Sattilio */ 2741181695Sattiliostatic int 2742181695Sattiliowitness_lock_order_check(struct witness *parent, struct witness *child) 2743181695Sattilio{ 2744181695Sattilio 2745181695Sattilio if (parent != child && 2746181695Sattilio w_rmatrix[parent->w_index][child->w_index] 2747181695Sattilio & WITNESS_LOCK_ORDER_KNOWN && 2748181695Sattilio isitmychild(parent, child)) 2749181695Sattilio return (1); 2750181695Sattilio 2751181695Sattilio return (0); 2752181695Sattilio} 2753181695Sattilio 2754181695Sattiliostatic int 2755181695Sattiliowitness_lock_order_add(struct witness *parent, struct witness *child) 2756181695Sattilio{ 2757181695Sattilio struct witness_lock_order_data *data = NULL; 2758181695Sattilio struct witness_lock_order_key key; 2759181695Sattilio unsigned int hash; 2760181695Sattilio 2761181695Sattilio MPASS(parent != NULL && child != NULL); 2762181695Sattilio key.from = parent->w_index; 2763181695Sattilio key.to = child->w_index; 2764181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2765181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2766181695Sattilio if (w_rmatrix[parent->w_index][child->w_index] 2767181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) 2768181695Sattilio return (1); 2769181695Sattilio 2770181695Sattilio hash = witness_hash_djb2((const char*)&key, 2771181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2772181695Sattilio w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN; 2773181695Sattilio data = w_lofree; 2774181695Sattilio if (data == NULL) 2775181695Sattilio return (0); 2776181695Sattilio w_lofree = data->wlod_next; 2777181695Sattilio data->wlod_next = w_lohash.wloh_array[hash]; 2778181695Sattilio data->wlod_key = key; 2779181695Sattilio w_lohash.wloh_array[hash] = data; 2780181695Sattilio w_lohash.wloh_count++; 2781181695Sattilio stack_zero(&data->wlod_stack); 2782181695Sattilio stack_save(&data->wlod_stack); 2783181695Sattilio return (1); 2784181695Sattilio} 2785181695Sattilio 2786181695Sattilio/* Call this whenver the structure of the witness graph changes. */ 2787181695Sattiliostatic void 2788181695Sattiliowitness_increment_graph_generation(void) 2789181695Sattilio{ 2790181695Sattilio 2791181695Sattilio if (witness_cold == 0) 2792181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2793181695Sattilio w_generation++; 2794181695Sattilio} 2795181695Sattilio 2796181695Sattilio#ifdef KDB 2797181695Sattiliostatic void 2798181695Sattilio_witness_debugger(int cond, const char *msg) 2799181695Sattilio{ 2800181695Sattilio 2801181695Sattilio if (witness_trace && cond) 2802181695Sattilio kdb_backtrace(); 2803181695Sattilio if (witness_kdb && cond) 2804181695Sattilio kdb_enter(KDB_WHY_WITNESS, msg); 2805181695Sattilio} 2806181695Sattilio#endif 2807