subr_witness.c revision 184214
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 184214 2008-10-23 20:26:15Z des $"); 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. */ 130178165Sattilio 131105508Sphk/* Define this to check for blessed mutexes */ 132105508Sphk#undef BLESSING 133105508Sphk 134181695Sattilio#define WITNESS_COUNT 1024 135181695Sattilio#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 136181695Sattilio#define WITNESS_HASH_SIZE 251 /* Prime, gives load factor < 2 */ 137179025Sattilio#define WITNESS_PENDLIST 512 138181695Sattilio 139181695Sattilio/* Allocate 256 KB of stack data space */ 140181695Sattilio#define WITNESS_LO_DATA_COUNT 2048 141181695Sattilio 142181695Sattilio/* Prime, gives load factor of ~2 at full load */ 143181695Sattilio#define WITNESS_LO_HASH_SIZE 1021 144181695Sattilio 14565557Sjasone/* 146181695Sattilio * XXX: This is somewhat bogus, as we assume here that at most 2048 threads 147181695Sattilio * will hold LOCK_NCHILDREN locks. We handle failure ok, and we should 14874912Sjhb * probably be safe for the most part, but it's still a SWAG. 14967352Sjhb */ 150181695Sattilio#define LOCK_NCHILDREN 5 151181695Sattilio#define LOCK_CHILDCOUNT 2048 15271352Sjasone 153181695Sattilio#define MAX_W_NAME 64 15471352Sjasone 155181695Sattilio#define BADSTACK_SBUF_SIZE (256 * WITNESS_COUNT) 156181695Sattilio#define CYCLEGRAPH_SBUF_SIZE 8192 157181695Sattilio#define FULLGRAPH_SBUF_SIZE 32768 158178165Sattilio 159181695Sattilio/* 160181695Sattilio * These flags go in the witness relationship matrix and describe the 161181695Sattilio * relationship between any two struct witness objects. 162181695Sattilio */ 163181695Sattilio#define WITNESS_UNRELATED 0x00 /* No lock order relation. */ 164181695Sattilio#define WITNESS_PARENT 0x01 /* Parent, aka direct ancestor. */ 165181695Sattilio#define WITNESS_ANCESTOR 0x02 /* Direct or indirect ancestor. */ 166181695Sattilio#define WITNESS_CHILD 0x04 /* Child, aka direct descendant. */ 167181695Sattilio#define WITNESS_DESCENDANT 0x08 /* Direct or indirect descendant. */ 168181695Sattilio#define WITNESS_ANCESTOR_MASK (WITNESS_PARENT | WITNESS_ANCESTOR) 169181695Sattilio#define WITNESS_DESCENDANT_MASK (WITNESS_CHILD | WITNESS_DESCENDANT) 170181695Sattilio#define WITNESS_RELATED_MASK \ 171181695Sattilio (WITNESS_ANCESTOR_MASK | WITNESS_DESCENDANT_MASK) 172181695Sattilio#define WITNESS_REVERSAL 0x10 /* A lock order reversal has been 173181695Sattilio * observed. */ 174181695Sattilio#define WITNESS_RESERVED1 0x20 /* Unused flag, reserved. */ 175181695Sattilio#define WITNESS_RESERVED2 0x40 /* Unused flag, reserved. */ 176181695Sattilio#define WITNESS_LOCK_ORDER_KNOWN 0x80 /* This lock order is known. */ 17771352Sjasone 178181695Sattilio/* Descendant to ancestor flags */ 179181695Sattilio#define WITNESS_DTOA(x) (((x) & WITNESS_RELATED_MASK) >> 2) 180181695Sattilio 181181695Sattilio/* Ancestor to descendant flags */ 182181695Sattilio#define WITNESS_ATOD(x) (((x) & WITNESS_RELATED_MASK) << 2) 183181695Sattilio 184181695Sattilio#define WITNESS_INDEX_ASSERT(i) \ 185181695Sattilio MPASS((i) > 0 && (i) <= w_max_used_index && (i) < WITNESS_COUNT) 186181695Sattilio 187181695SattilioMALLOC_DEFINE(M_WITNESS, "Witness", "Witness"); 188181695Sattilio 189178165Sattilio/* 190178165Sattilio * Lock instances. A lock instance is the data associated with a lock while 191178165Sattilio * it is held by witness. For example, a lock instance will hold the 192178165Sattilio * recursion count of a lock. Lock instances are held in lists. Spin locks 193178165Sattilio * are held in a per-cpu list while sleep locks are held in per-thread list. 194178165Sattilio */ 195178165Sattiliostruct lock_instance { 196181695Sattilio struct lock_object *li_lock; 197181695Sattilio const char *li_file; 198181695Sattilio int li_line; 199181695Sattilio u_int li_flags; 200178165Sattilio}; 201178165Sattilio 202178165Sattilio/* 203178165Sattilio * A simple list type used to build the list of locks held by a thread 204178165Sattilio * or CPU. We can't simply embed the list in struct lock_object since a 205178165Sattilio * lock may be held by more than one thread if it is a shared lock. Locks 206178165Sattilio * are added to the head of the list, so we fill up each list entry from 207178165Sattilio * "the back" logically. To ease some of the arithmetic, we actually fill 208178165Sattilio * in each list entry the normal way (children[0] then children[1], etc.) but 209178165Sattilio * when we traverse the list we read children[count-1] as the first entry 210178165Sattilio * down to children[0] as the final entry. 211178165Sattilio */ 212178165Sattiliostruct lock_list_entry { 213178165Sattilio struct lock_list_entry *ll_next; 214178165Sattilio struct lock_instance ll_children[LOCK_NCHILDREN]; 215178165Sattilio u_int ll_count; 216178165Sattilio}; 217178165Sattilio 218181695Sattilio/* 219181695Sattilio * The main witness structure. One of these per named lock type in the system 220181695Sattilio * (for example, "vnode interlock"). 221181695Sattilio */ 22274912Sjhbstruct witness { 223181695Sattilio char w_name[MAX_W_NAME]; 224181695Sattilio uint32_t w_index; /* Index in the relationship matrix */ 225181695Sattilio struct lock_class *w_class; 226181695Sattilio STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 227181695Sattilio STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 228181695Sattilio struct witness *w_hash_next; /* Linked list in hash buckets. */ 229181695Sattilio const char *w_file; /* File where last acquired */ 230181695Sattilio uint32_t w_line; /* Line where last acquired */ 231181695Sattilio uint32_t w_refcount; 232181695Sattilio uint16_t w_num_ancestors; /* direct/indirect 233181695Sattilio * ancestor count */ 234181695Sattilio uint16_t w_num_descendants; /* direct/indirect 235181695Sattilio * descendant count */ 236181695Sattilio int16_t w_ddb_level; 237181695Sattilio int w_displayed:1; 238181695Sattilio int w_reversed:1; 23974912Sjhb}; 24071352Sjasone 241181695SattilioSTAILQ_HEAD(witness_list, witness); 242181695Sattilio 243181695Sattilio/* 244181695Sattilio * The witness hash table. Keys are witness names (const char *), elements are 245181695Sattilio * witness objects (struct witness *). 246181695Sattilio */ 247181695Sattiliostruct witness_hash { 248181695Sattilio struct witness *wh_array[WITNESS_HASH_SIZE]; 249181695Sattilio uint32_t wh_size; 250181695Sattilio uint32_t wh_count; 25174912Sjhb}; 25271352Sjasone 253181695Sattilio/* 254181695Sattilio * Key type for the lock order data hash table. 255181695Sattilio */ 256181695Sattiliostruct witness_lock_order_key { 257181695Sattilio uint16_t from; 258181695Sattilio uint16_t to; 259181695Sattilio}; 26071352Sjasone 261181695Sattiliostruct witness_lock_order_data { 262181695Sattilio struct stack wlod_stack; 263181695Sattilio struct witness_lock_order_key wlod_key; 264181695Sattilio struct witness_lock_order_data *wlod_next; 265181695Sattilio}; 266181695Sattilio 267181695Sattilio/* 268181695Sattilio * The witness lock order data hash table. Keys are witness index tuples 269181695Sattilio * (struct witness_lock_order_key), elements are lock order data objects 270181695Sattilio * (struct witness_lock_order_data). 271181695Sattilio */ 272181695Sattiliostruct witness_lock_order_hash { 273181695Sattilio struct witness_lock_order_data *wloh_array[WITNESS_LO_HASH_SIZE]; 274181695Sattilio u_int wloh_size; 275181695Sattilio u_int wloh_count; 276181695Sattilio}; 277181695Sattilio 278105508Sphk#ifdef BLESSING 27974912Sjhbstruct witness_blessed { 280181695Sattilio const char *b_lock1; 281181695Sattilio const char *b_lock2; 28274912Sjhb}; 283105508Sphk#endif 28471352Sjasone 285179025Sattiliostruct witness_pendhelp { 286179025Sattilio const char *wh_type; 287179025Sattilio struct lock_object *wh_lock; 288179025Sattilio}; 289179025Sattilio 290181695Sattiliostruct witness_order_list_entry { 291181695Sattilio const char *w_name; 292181695Sattilio struct lock_class *w_class; 293181695Sattilio}; 294181695Sattilio 295181695Sattilio/* 296181695Sattilio * Returns 0 if one of the locks is a spin lock and the other is not. 297181695Sattilio * Returns 1 otherwise. 298181695Sattilio */ 299181695Sattiliostatic __inline int 300181695Sattiliowitness_lock_type_equal(struct witness *w1, struct witness *w2) 301181695Sattilio{ 302181695Sattilio 303181695Sattilio return ((w1->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) == 304181695Sattilio (w2->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))); 305181695Sattilio} 306181695Sattilio 307181695Sattiliostatic __inline int 308181695Sattiliowitness_lock_order_key_empty(const struct witness_lock_order_key *key) 309181695Sattilio{ 310181695Sattilio 311181695Sattilio return (key->from == 0 && key->to == 0); 312181695Sattilio} 313181695Sattilio 314181695Sattiliostatic __inline int 315181695Sattiliowitness_lock_order_key_equal(const struct witness_lock_order_key *a, 316181695Sattilio const struct witness_lock_order_key *b) 317181695Sattilio{ 318181695Sattilio 319181695Sattilio return (a->from == b->from && a->to == b->to); 320181695Sattilio} 321181695Sattilio 322181695Sattiliostatic int _isitmyx(struct witness *w1, struct witness *w2, int rmask, 323181695Sattilio const char *fname); 324181695Sattilio#ifdef KDB 325181695Sattiliostatic void _witness_debugger(int cond, const char *msg); 326181695Sattilio#endif 327181695Sattiliostatic void adopt(struct witness *parent, struct witness *child); 328112117Sjhb#ifdef BLESSING 329112117Sjhbstatic int blessed(struct witness *, struct witness *); 330112117Sjhb#endif 331179025Sattiliostatic void depart(struct witness *w); 332181695Sattiliostatic struct witness *enroll(const char *description, 333181695Sattilio struct lock_class *lock_class); 334181695Sattiliostatic struct lock_instance *find_instance(struct lock_list_entry *list, 335181695Sattilio struct lock_object *lock); 336112117Sjhbstatic int isitmychild(struct witness *parent, struct witness *child); 337112117Sjhbstatic int isitmydescendant(struct witness *parent, struct witness *child); 338181695Sattiliostatic void itismychild(struct witness *parent, struct witness *child); 339181695Sattiliostatic int sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS); 340112562Sjhbstatic int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 341181695Sattiliostatic int sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS); 342181695Sattiliostatic void witness_add_fullgraph(struct sbuf *sb, struct witness *parent); 343181695Sattilio#ifdef DDB 344181695Sattiliostatic void witness_ddb_compute_levels(void); 345181695Sattiliostatic void witness_ddb_display(void(*)(const char *fmt, ...)); 346181695Sattiliostatic void witness_ddb_display_descendants(void(*)(const char *fmt, ...), 347181695Sattilio struct witness *, int indent); 348181695Sattiliostatic void witness_ddb_display_list(void(*prnt)(const char *fmt, ...), 349181695Sattilio struct witness_list *list); 350181695Sattiliostatic void witness_ddb_level_descendants(struct witness *parent, int l); 351181695Sattiliostatic void witness_ddb_list(struct thread *td); 352181695Sattilio#endif 35374912Sjhbstatic void witness_free(struct witness *m); 354181695Sattiliostatic struct witness *witness_get(void); 355181695Sattiliostatic uint32_t witness_hash_djb2(const uint8_t *key, uint32_t size); 356181695Sattiliostatic struct witness *witness_hash_get(const char *key); 357181695Sattiliostatic void witness_hash_put(struct witness *w); 358181695Sattiliostatic void witness_init_hash_tables(void); 359181695Sattiliostatic void witness_increment_graph_generation(void); 36074912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 361181695Sattiliostatic struct lock_list_entry *witness_lock_list_get(void); 362181695Sattiliostatic int witness_lock_order_add(struct witness *parent, 363181695Sattilio struct witness *child); 364181695Sattiliostatic int witness_lock_order_check(struct witness *parent, 365181695Sattilio struct witness *child); 366181695Sattiliostatic struct witness_lock_order_data *witness_lock_order_get( 367181695Sattilio struct witness *parent, 368181695Sattilio struct witness *child); 369111881Sjhbstatic void witness_list_lock(struct lock_instance *instance); 370181695Sattilio 371181695Sattilio#ifdef KDB 372181695Sattilio#define witness_debugger(c) _witness_debugger(c, __func__) 373181695Sattilio#else 374181695Sattilio#define witness_debugger(c) 375100011Smp#endif 37672200Sbmilekic 377134873SjmgSYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking"); 37872200Sbmilekic 379112562Sjhb/* 380183329Sjhb * If set to 0, lock order checking is disabled. If set to -1, 381183329Sjhb * witness is completely disabled. Otherwise witness performs full 382183329Sjhb * lock order checking for all locks. At runtime, lock order checking 383183329Sjhb * may be toggled. However, witness cannot be reenabled once it is 384183329Sjhb * completely disabled. 385112562Sjhb */ 38677843Speterstatic int witness_watch = 1; 387134873SjmgTUNABLE_INT("debug.witness.watch", &witness_watch); 388134873SjmgSYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 389112562Sjhb sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 39071352Sjasone 391131930Smarcel#ifdef KDB 39272200Sbmilekic/* 393181695Sattilio * When KDB is enabled and witness_kdb is 1, it will cause the system 394131930Smarcel * to drop into kdebug() when: 395151623Sjhb * - a lock hierarchy violation occurs 39665557Sjasone * - locks are held when going to sleep. 39765557Sjasone */ 398131930Smarcel#ifdef WITNESS_KDB 399131930Smarcelint witness_kdb = 1; 40067676Sjhb#else 401131930Smarcelint witness_kdb = 0; 40265557Sjasone#endif 403134873SjmgTUNABLE_INT("debug.witness.kdb", &witness_kdb); 404134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 405110779Speter 406110779Speter/* 407181695Sattilio * When KDB is enabled and witness_trace is 1, it will cause the system 408110779Speter * to print a stack trace: 409151623Sjhb * - a lock hierarchy violation occurs 410110779Speter * - locks are held when going to sleep. 411110779Speter */ 412110779Speterint witness_trace = 1; 413134873SjmgTUNABLE_INT("debug.witness.trace", &witness_trace); 414134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 415131930Smarcel#endif /* KDB */ 41665557Sjasone 41767676Sjhb#ifdef WITNESS_SKIPSPIN 41877843Speterint witness_skipspin = 1; 41967676Sjhb#else 42077843Speterint witness_skipspin = 0; 42165557Sjasone#endif 422134873SjmgTUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 423181695SattilioSYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, &witness_skipspin, 424181695Sattilio 0, ""); 42565557Sjasone 426181695Sattilio/* 427181695Sattilio * Call this to print out the relations between locks. 428181695Sattilio */ 429181695SattilioSYSCTL_PROC(_debug_witness, OID_AUTO, fullgraph, CTLTYPE_STRING | CTLFLAG_RD, 430181695Sattilio NULL, 0, sysctl_debug_witness_fullgraph, "A", "Show locks relation graphs"); 431181695Sattilio 432181695Sattilio/* 433181695Sattilio * Call this to print out the witness faulty stacks. 434181695Sattilio */ 435181695SattilioSYSCTL_PROC(_debug_witness, OID_AUTO, badstacks, CTLTYPE_STRING | CTLFLAG_RD, 436181695Sattilio NULL, 0, sysctl_debug_witness_badstacks, "A", "Show bad witness stacks"); 437181695Sattilio 43874912Sjhbstatic struct mtx w_mtx; 439181695Sattilio 440181695Sattilio/* w_list */ 44174912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 44274912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 443181695Sattilio 444181695Sattilio/* w_typelist */ 44574912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 44674912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 447181695Sattilio 448181695Sattilio/* lock list */ 44974912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 450179025Sattiliostatic struct witness_pendhelp pending_locks[WITNESS_PENDLIST]; 451179025Sattiliostatic u_int pending_cnt; 45265557Sjasone 453181695Sattiliostatic int w_free_cnt, w_spin_cnt, w_sleep_cnt; 454149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, ""); 455149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, ""); 456149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0, 457149441Struckman ""); 458149441Struckman 459181695Sattiliostatic struct witness *w_data; 460181695Sattiliostatic uint8_t w_rmatrix[WITNESS_COUNT+1][WITNESS_COUNT+1]; 46174912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 462181695Sattiliostatic struct witness_hash w_hash; /* The witness hash table. */ 46365557Sjasone 464181695Sattilio/* The lock order data hash */ 465181695Sattiliostatic struct witness_lock_order_data w_lodata[WITNESS_LO_DATA_COUNT]; 466181695Sattiliostatic struct witness_lock_order_data *w_lofree = NULL; 467181695Sattiliostatic struct witness_lock_order_hash w_lohash; 468181695Sattiliostatic int w_max_used_index = 0; 469181695Sattiliostatic unsigned int w_generation = 0; 470182446Sattiliostatic const char *w_notrunning = "Witness not running\n"; 471181695Sattiliostatic const char *w_stillcold = "Witness is still cold\n"; 472181695Sattilio 473181695Sattilio 47474912Sjhbstatic struct witness_order_list_entry order_lists[] = { 475149738Sjhb /* 476149738Sjhb * sx locks 477149738Sjhb */ 47874912Sjhb { "proctree", &lock_class_sx }, 47974912Sjhb { "allproc", &lock_class_sx }, 480168402Spjd { "allprison", &lock_class_sx }, 481149738Sjhb { NULL, NULL }, 482149738Sjhb /* 483149738Sjhb * Various mutexes 484149738Sjhb */ 485111951Sjhb { "Giant", &lock_class_mtx_sleep }, 486108184Skris { "pipe mutex", &lock_class_mtx_sleep }, 48796122Salfred { "sigio lock", &lock_class_mtx_sleep }, 48891140Stanimura { "process group", &lock_class_mtx_sleep }, 48974912Sjhb { "process lock", &lock_class_mtx_sleep }, 49091140Stanimura { "session", &lock_class_mtx_sleep }, 491177299Spjd { "uidinfo hash", &lock_class_rw }, 492168856Sjkoshy#ifdef HWPMC_HOOKS 493168856Sjkoshy { "pmc-sleep", &lock_class_mtx_sleep }, 494168856Sjkoshy#endif 49574912Sjhb { NULL, NULL }, 49675464Sjhb /* 497130022Srwatson * Sockets 498130022Srwatson */ 499130022Srwatson { "accept", &lock_class_mtx_sleep }, 500130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 501130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 502130022Srwatson { "sellck", &lock_class_mtx_sleep }, 503130022Srwatson { NULL, NULL }, 504130022Srwatson /* 505130022Srwatson * Routing 506130022Srwatson */ 507130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 508130022Srwatson { "radix node head", &lock_class_mtx_sleep }, 509130022Srwatson { "rtentry", &lock_class_mtx_sleep }, 510130022Srwatson { "ifaddr", &lock_class_mtx_sleep }, 511130022Srwatson { NULL, NULL }, 512130022Srwatson /* 513148896Srwatson * Multicast - protocol locks before interface locks, after UDP locks. 514148682Srwatson */ 515178285Srwatson { "udpinp", &lock_class_rw }, 516148682Srwatson { "in_multi_mtx", &lock_class_mtx_sleep }, 517148682Srwatson { "igmp_mtx", &lock_class_mtx_sleep }, 518148682Srwatson { "if_addr_mtx", &lock_class_mtx_sleep }, 519148682Srwatson { NULL, NULL }, 520148682Srwatson /* 521130022Srwatson * UNIX Domain Sockets 522130396Srwatson */ 523130396Srwatson { "unp", &lock_class_mtx_sleep }, 524130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 525130031Sjhb { NULL, NULL }, 526130022Srwatson /* 527130022Srwatson * UDP/IP 528130022Srwatson */ 529178285Srwatson { "udp", &lock_class_rw }, 530178285Srwatson { "udpinp", &lock_class_rw }, 531130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 532130022Srwatson { NULL, NULL }, 533130022Srwatson /* 534130022Srwatson * TCP/IP 535130022Srwatson */ 536178285Srwatson { "tcp", &lock_class_rw }, 537178285Srwatson { "tcpinp", &lock_class_rw }, 538130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 539130022Srwatson { NULL, NULL }, 540130022Srwatson /* 541130022Srwatson * SLIP 542130022Srwatson */ 543130022Srwatson { "slip_mtx", &lock_class_mtx_sleep }, 544130022Srwatson { "slip sc_mtx", &lock_class_mtx_sleep }, 545130031Sjhb { NULL, NULL }, 546130022Srwatson /* 547132639Srwatson * netatalk 548132639Srwatson */ 549132639Srwatson { "ddp_list_mtx", &lock_class_mtx_sleep }, 550132639Srwatson { "ddp_mtx", &lock_class_mtx_sleep }, 551132639Srwatson { NULL, NULL }, 552132639Srwatson /* 553134971Srwatson * BPF 554134971Srwatson */ 555134971Srwatson { "bpf global lock", &lock_class_mtx_sleep }, 556134971Srwatson { "bpf interface lock", &lock_class_mtx_sleep }, 557134971Srwatson { "bpf cdev lock", &lock_class_mtx_sleep }, 558144832Spjd { NULL, NULL }, 559143335Srwatson /* 560143335Srwatson * NFS server 561143335Srwatson */ 562143335Srwatson { "nfsd_mtx", &lock_class_mtx_sleep }, 563143335Srwatson { "so_snd", &lock_class_mtx_sleep }, 564134971Srwatson { NULL, NULL }, 565170530Ssam 566134971Srwatson /* 567170530Ssam * IEEE 802.11 568170530Ssam */ 569170530Ssam { "802.11 com lock", &lock_class_mtx_sleep}, 570170530Ssam { NULL, NULL }, 571170530Ssam /* 572170530Ssam * Network drivers 573170530Ssam */ 574170530Ssam { "network driver", &lock_class_mtx_sleep}, 575170530Ssam { NULL, NULL }, 576170530Ssam 577170530Ssam /* 578168217Swkoszek * Netgraph 579168217Swkoszek */ 580168217Swkoszek { "ng_node", &lock_class_mtx_sleep }, 581168217Swkoszek { "ng_worklist", &lock_class_mtx_sleep }, 582168217Swkoszek { NULL, NULL }, 583168217Swkoszek /* 584144836Spjd * CDEV 585144836Spjd */ 586145425Sjeff { "system map", &lock_class_mtx_sleep }, 587145425Sjeff { "vm page queue mutex", &lock_class_mtx_sleep }, 588145425Sjeff { "vnode interlock", &lock_class_mtx_sleep }, 589144836Spjd { "cdev", &lock_class_mtx_sleep }, 590144836Spjd { NULL, NULL }, 591144836Spjd /* 592166421Skib * kqueue/VFS interaction 593166421Skib */ 594166421Skib { "kqueue", &lock_class_mtx_sleep }, 595166421Skib { "struct mount mtx", &lock_class_mtx_sleep }, 596166421Skib { "vnode interlock", &lock_class_mtx_sleep }, 597166421Skib { NULL, NULL }, 598166421Skib /* 59975464Sjhb * spin locks 60075464Sjhb */ 60184331Sjhb#ifdef SMP 60284331Sjhb { "ap boot", &lock_class_mtx_spin }, 60372224Sjhb#endif 604150582Sjhb { "rm.mutex_mtx", &lock_class_mtx_spin }, 60574912Sjhb { "sio", &lock_class_mtx_spin }, 606173877Sattilio { "scrlock", &lock_class_mtx_spin }, 60772224Sjhb#ifdef __i386__ 60874912Sjhb { "cy", &lock_class_mtx_spin }, 60972224Sjhb#endif 610170848Smarius#ifdef __sparc64__ 611170848Smarius { "pcib_mtx", &lock_class_mtx_spin }, 612170848Smarius { "rtc_mtx", &lock_class_mtx_spin }, 613170848Smarius#endif 614157584Smarcel { "scc_hwmtx", &lock_class_mtx_spin }, 615124972Sru { "uart_hwmtx", &lock_class_mtx_spin }, 616161638Sssouhlal { "fast_taskqueue", &lock_class_mtx_spin }, 617122001Sjhb { "intr table", &lock_class_mtx_spin }, 618168856Sjkoshy#ifdef HWPMC_HOOKS 619168856Sjkoshy { "pmc-per-proc", &lock_class_mtx_spin }, 620168856Sjkoshy#endif 621170302Sjeff { "process slock", &lock_class_mtx_spin }, 622126324Sjhb { "sleepq chain", &lock_class_mtx_spin }, 623170302Sjeff { "umtx lock", &lock_class_mtx_spin }, 624173877Sattilio { "rm_spinlock", &lock_class_mtx_spin }, 625170302Sjeff { "turnstile chain", &lock_class_mtx_spin }, 626170302Sjeff { "turnstile lock", &lock_class_mtx_spin }, 62774912Sjhb { "sched lock", &lock_class_mtx_spin }, 628122514Sjhb { "td_contested", &lock_class_mtx_spin }, 62974912Sjhb { "callout", &lock_class_mtx_spin }, 630136374Srwatson { "entropy harvest mutex", &lock_class_mtx_spin }, 631162285Sscottl { "syscons video lock", &lock_class_mtx_spin }, 632169803Sjeff { "time lock", &lock_class_mtx_spin }, 633172256Sattilio#ifdef SMP 634172256Sattilio { "smp rendezvous", &lock_class_mtx_spin }, 635172256Sattilio#endif 636176771Sraj#ifdef __powerpc__ 637176771Sraj { "tlb0", &lock_class_mtx_spin }, 638176771Sraj#endif 63965557Sjasone /* 64065557Sjasone * leaf locks 64165557Sjasone */ 642178149Sattilio { "intrcnt", &lock_class_mtx_spin }, 64388322Sjhb { "icu", &lock_class_mtx_spin }, 644172256Sattilio#if defined(SMP) && defined(__sparc64__) 645108187Sjake { "ipi", &lock_class_mtx_spin }, 64699862Speter#endif 647172256Sattilio#ifdef __i386__ 648172256Sattilio { "allpmaps", &lock_class_mtx_spin }, 649172256Sattilio { "descriptor tables", &lock_class_mtx_spin }, 650108187Sjake#endif 65178785Sjhb { "clk", &lock_class_mtx_spin }, 652178149Sattilio { "cpuset", &lock_class_mtx_spin }, 653172256Sattilio { "mprof lock", &lock_class_mtx_spin }, 654170302Sjeff { "zombie lock", &lock_class_mtx_spin }, 655103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 656104951Speter#ifdef __ia64__ 657104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 658104951Speter#endif 659115425Speter#if defined(__i386__) || defined(__amd64__) 660111068Speter { "pcicfg", &lock_class_mtx_spin }, 661143204Swpaul { "NDIS thread lock", &lock_class_mtx_spin }, 662111068Speter#endif 663144966Svkashyap { "tw_osl_io_lock", &lock_class_mtx_spin }, 664144966Svkashyap { "tw_osl_q_lock", &lock_class_mtx_spin }, 665144966Svkashyap { "tw_cl_io_lock", &lock_class_mtx_spin }, 666144966Svkashyap { "tw_cl_intr_lock", &lock_class_mtx_spin }, 667144966Svkashyap { "tw_cl_gen_lock", &lock_class_mtx_spin }, 668168856Sjkoshy#ifdef HWPMC_HOOKS 669168856Sjkoshy { "pmc-leaf", &lock_class_mtx_spin }, 670168856Sjkoshy#endif 671170302Sjeff { "blocked lock", &lock_class_mtx_spin }, 67274912Sjhb { NULL, NULL }, 67374912Sjhb { NULL, NULL } 67465557Sjasone}; 67565557Sjasone 676105508Sphk#ifdef BLESSING 67765557Sjasone/* 67865557Sjasone * Pairs of locks which have been blessed 67965557Sjasone * Don't complain about order problems with blessed locks 68065557Sjasone */ 68165856Sjhbstatic struct witness_blessed blessed_list[] = { 68265557Sjasone}; 68372200Sbmilekicstatic int blessed_count = 68472200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 685105508Sphk#endif 68665557Sjasone 68774912Sjhb/* 68874912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 68974912Sjhb */ 69074912Sjhbstatic int witness_cold = 1; 69174912Sjhb 69274912Sjhb/* 693151629Sjhb * This global is set to 1 once the static lock orders have been enrolled 694151629Sjhb * so that a warning can be issued for any spin locks enrolled later. 695151629Sjhb */ 696151629Sjhbstatic int witness_spin_warn = 0; 697151629Sjhb 698151629Sjhb/* 699153133Sjhb * The WITNESS-enabled diagnostic code. Note that the witness code does 700153133Sjhb * assume that the early boot is single-threaded at least until after this 701153133Sjhb * routine is completed. 70274912Sjhb */ 70371352Sjasonestatic void 70474912Sjhbwitness_initialize(void *dummy __unused) 70565557Sjasone{ 70674912Sjhb struct lock_object *lock; 70774912Sjhb struct witness_order_list_entry *order; 70874912Sjhb struct witness *w, *w1; 70974912Sjhb int i; 71065557Sjasone 711184214Sdes w_data = malloc(sizeof (struct witness) * WITNESS_COUNT, M_WITNESS, 712181695Sattilio M_NOWAIT | M_ZERO); 713181695Sattilio 71474912Sjhb /* 71574912Sjhb * We have to release Giant before initializing its witness 71674912Sjhb * structure so that WITNESS doesn't get confused. 71774912Sjhb */ 71874912Sjhb mtx_unlock(&Giant); 71974912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 72074912Sjhb 72187593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 72293811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 723164159Skmacy MTX_NOWITNESS | MTX_NOPROFILE); 724181695Sattilio for (i = WITNESS_COUNT - 1; i >= 0; i--) { 725181695Sattilio w = &w_data[i]; 726181695Sattilio memset(w, 0, sizeof(*w)); 727181695Sattilio w_data[i].w_index = i; /* Witness index never changes. */ 728181695Sattilio witness_free(w); 729181695Sattilio } 730181695Sattilio KASSERT(STAILQ_FIRST(&w_free)->w_index == 0, 731181695Sattilio ("%s: Invalid list of free witness objects", __func__)); 732181695Sattilio 733181695Sattilio /* Witness with index 0 is not used to aid in debugging. */ 734181695Sattilio STAILQ_REMOVE_HEAD(&w_free, w_list); 735181695Sattilio w_free_cnt--; 736181695Sattilio 737181695Sattilio memset(w_rmatrix, 0, 738181695Sattilio (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1))); 739181695Sattilio 74074912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 74174912Sjhb witness_lock_list_free(&w_locklistdata[i]); 742181695Sattilio witness_init_hash_tables(); 74374912Sjhb 74474912Sjhb /* First add in all the specified order lists. */ 74574912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 74674912Sjhb w = enroll(order->w_name, order->w_class); 74775569Sjhb if (w == NULL) 74875569Sjhb continue; 74974912Sjhb w->w_file = "order list"; 75074912Sjhb for (order++; order->w_name != NULL; order++) { 75174912Sjhb w1 = enroll(order->w_name, order->w_class); 75275569Sjhb if (w1 == NULL) 75375569Sjhb continue; 75474912Sjhb w1->w_file = "order list"; 755181695Sattilio itismychild(w, w1); 75674912Sjhb w = w1; 75765557Sjasone } 75865557Sjasone } 759151629Sjhb witness_spin_warn = 1; 76065557Sjasone 76174912Sjhb /* Iterate through all locks and add them to witness. */ 762179025Sattilio for (i = 0; pending_locks[i].wh_lock != NULL; i++) { 763179025Sattilio lock = pending_locks[i].wh_lock; 764153133Sjhb KASSERT(lock->lo_flags & LO_WITNESS, 765153133Sjhb ("%s: lock %s is on pending list but not LO_WITNESS", 766153133Sjhb __func__, lock->lo_name)); 767179025Sattilio lock->lo_witness = enroll(pending_locks[i].wh_type, 768179025Sattilio LOCK_CLASS(lock)); 76974912Sjhb } 77074912Sjhb 77174912Sjhb /* Mark the witness code as being ready for use. */ 772151629Sjhb witness_cold = 0; 77374912Sjhb 77474912Sjhb mtx_lock(&Giant); 77565557Sjasone} 776177253SrwatsonSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, 777177253Srwatson NULL); 77865557Sjasone 77974912Sjhbvoid 780179025Sattiliowitness_init(struct lock_object *lock, const char *type) 78174912Sjhb{ 78274912Sjhb struct lock_class *class; 78374912Sjhb 784153133Sjhb /* Various sanity checks. */ 785154077Sjhb class = LOCK_CLASS(lock); 78674912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 78774912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 78882284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 78974912Sjhb class->lc_name, lock->lo_name); 79074912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 79174912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 79282284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 79374912Sjhb class->lc_name, lock->lo_name); 79482244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 79582244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 79682284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 79782244Sjhb class->lc_name, lock->lo_name); 79882244Sjhb 799153133Sjhb /* 800153133Sjhb * If we shouldn't watch this lock, then just clear lo_witness. 801153133Sjhb * Otherwise, if witness_cold is set, then it is too early to 802153133Sjhb * enroll this lock, so defer it to witness_initialize() by adding 803153133Sjhb * it to the pending_locks list. If it is not too early, then enroll 804153133Sjhb * the lock now. 805153133Sjhb */ 806182446Sattilio if (witness_watch < 1 || panicstr != NULL || 807153133Sjhb (lock->lo_flags & LO_WITNESS) == 0) 808153133Sjhb lock->lo_witness = NULL; 809153133Sjhb else if (witness_cold) { 810179025Sattilio pending_locks[pending_cnt].wh_lock = lock; 811179025Sattilio pending_locks[pending_cnt++].wh_type = type; 812179025Sattilio if (pending_cnt > WITNESS_PENDLIST) 813179025Sattilio panic("%s: pending locks list is too small, bump it\n", 814179025Sattilio __func__); 815153133Sjhb } else 816179025Sattilio lock->lo_witness = enroll(type, class); 81774912Sjhb} 81874912Sjhb 81974912Sjhbvoid 82074912Sjhbwitness_destroy(struct lock_object *lock) 82174912Sjhb{ 822154077Sjhb struct lock_class *class; 82375362Sjhb struct witness *w; 82474912Sjhb 825154077Sjhb class = LOCK_CLASS(lock); 826181695Sattilio 82774912Sjhb if (witness_cold) 82874912Sjhb panic("lock (%s) %s destroyed while witness_cold", 829154077Sjhb class->lc_name, lock->lo_name); 83074912Sjhb 83176272Sjhb /* XXX: need to verify that no one holds the lock */ 832181695Sattilio if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL) 833181695Sattilio return; 834181695Sattilio w = lock->lo_witness; 835112117Sjhb 836181695Sattilio mtx_lock_spin(&w_mtx); 837181695Sattilio MPASS(w->w_refcount > 0); 838181695Sattilio w->w_refcount--; 839181695Sattilio 840181695Sattilio if (w->w_refcount == 0) 841181695Sattilio depart(w); 842181695Sattilio mtx_unlock_spin(&w_mtx); 84374912Sjhb} 84474912Sjhb 845112115Sjhb#ifdef DDB 84671352Sjasonestatic void 847181695Sattiliowitness_ddb_compute_levels(void) 848149979Struckman{ 849181695Sattilio struct witness *w; 850149979Struckman 851149979Struckman /* 852149979Struckman * First clear all levels. 853149979Struckman */ 854181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 855181695Sattilio w->w_ddb_level = -1; 856149979Struckman 857149979Struckman /* 858181695Sattilio * Look for locks with no parents and level all their descendants. 859149979Struckman */ 860149979Struckman STAILQ_FOREACH(w, &w_all, w_list) { 861181695Sattilio 862181695Sattilio /* If the witness has ancestors (is not a root), skip it. */ 863181695Sattilio if (w->w_num_ancestors > 0) 864181695Sattilio continue; 865181695Sattilio witness_ddb_level_descendants(w, 0); 866149979Struckman } 867149979Struckman} 868149979Struckman 869149979Struckmanstatic void 870181695Sattiliowitness_ddb_level_descendants(struct witness *w, int l) 871149979Struckman{ 872149979Struckman int i; 873149979Struckman 874181695Sattilio if (w->w_ddb_level >= l) 875181695Sattilio return; 876181695Sattilio 877181695Sattilio w->w_ddb_level = l; 878181695Sattilio l++; 879181695Sattilio 880181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 881181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 882181695Sattilio witness_ddb_level_descendants(&w_data[i], l); 883181695Sattilio } 884149979Struckman} 885149979Struckman 886149979Struckmanstatic void 887181695Sattiliowitness_ddb_display_descendants(void(*prnt)(const char *fmt, ...), 888181695Sattilio struct witness *w, int indent) 889149979Struckman{ 890181695Sattilio int i; 891149979Struckman 892181695Sattilio for (i = 0; i < indent; i++) 893181695Sattilio prnt(" "); 894181695Sattilio prnt("%s (type: %s, depth: %d, active refs: %d)", 895181695Sattilio w->w_name, w->w_class->lc_name, 896181695Sattilio w->w_ddb_level, w->w_refcount); 897181695Sattilio if (w->w_displayed) { 898181695Sattilio prnt(" -- (already displayed)\n"); 899181695Sattilio return; 900181695Sattilio } 901181695Sattilio w->w_displayed = 1; 902181695Sattilio if (w->w_file != NULL && w->w_line != 0) 903181695Sattilio prnt(" -- last acquired @ %s:%d\n", w->w_file, 904181695Sattilio w->w_line); 905149979Struckman else 906181695Sattilio prnt(" -- never acquired\n"); 907181695Sattilio indent++; 908181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 909181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 910181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 911181695Sattilio witness_ddb_display_descendants(prnt, &w_data[i], 912181695Sattilio indent); 913149979Struckman } 914149979Struckman} 915149979Struckman 916149979Struckmanstatic void 917181695Sattiliowitness_ddb_display_list(void(*prnt)(const char *fmt, ...), 918181695Sattilio struct witness_list *list) 91971352Sjasone{ 920112118Sjhb struct witness *w; 92171352Sjasone 92274912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 923181695Sattilio if (w->w_file == NULL || w->w_ddb_level > 0) 92471352Sjasone continue; 925181695Sattilio 926181695Sattilio /* This lock has no anscestors - display its descendants. */ 927181695Sattilio witness_ddb_display_descendants(prnt, w, 0); 92871352Sjasone } 92974912Sjhb} 93072224Sjhb 93174912Sjhbstatic void 932181695Sattiliowitness_ddb_display(void(*prnt)(const char *fmt, ...)) 933178841Sattilio{ 93474912Sjhb struct witness *w; 93574912Sjhb 936181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 937181695Sattilio witness_ddb_compute_levels(); 93874912Sjhb 939112118Sjhb /* Clear all the displayed flags. */ 940181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 941112118Sjhb w->w_displayed = 0; 942112118Sjhb 94372224Sjhb /* 94474930Sjhb * First, handle sleep locks which have been acquired at least 94574912Sjhb * once. 94674912Sjhb */ 94774912Sjhb prnt("Sleep locks:\n"); 948181695Sattilio witness_ddb_display_list(prnt, &w_sleep); 94974912Sjhb 95074912Sjhb /* 95174930Sjhb * Now do spin locks which have been acquired at least once. 95272224Sjhb */ 95374912Sjhb prnt("\nSpin locks:\n"); 954181695Sattilio witness_ddb_display_list(prnt, &w_spin); 95572224Sjhb 95672224Sjhb /* 95774930Sjhb * Finally, any locks which have not been acquired yet. 95872224Sjhb */ 95974912Sjhb prnt("\nLocks which were never acquired:\n"); 96074912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 96197948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 96271352Sjasone continue; 963181695Sattilio prnt("%s (type: %s, depth: %d)\n", w->w_name, 964181695Sattilio w->w_class->lc_name, w->w_ddb_level); 96571352Sjasone } 96671352Sjasone} 967112115Sjhb#endif /* DDB */ 96871352Sjasone 969112116Sjhb/* Trim useless garbage from filenames. */ 970112116Sjhbstatic const char * 971112116Sjhbfixup_filename(const char *file) 972112116Sjhb{ 973112116Sjhb 974112116Sjhb if (file == NULL) 975112116Sjhb return (NULL); 976112116Sjhb while (strncmp(file, "../", 3) == 0) 977112116Sjhb file += 3; 978112116Sjhb return (file); 979112116Sjhb} 980112116Sjhb 981125160Sjhbint 982125160Sjhbwitness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 983125160Sjhb{ 984125160Sjhb 985182473Sattilio if (witness_watch == -1 || panicstr != NULL) 986125160Sjhb return (0); 987125160Sjhb 988125160Sjhb /* Require locks that witness knows about. */ 989125160Sjhb if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 990125160Sjhb lock2->lo_witness == NULL) 991125160Sjhb return (EINVAL); 992125160Sjhb 993181695Sattilio mtx_assert(&w_mtx, MA_NOTOWNED); 994125160Sjhb mtx_lock_spin(&w_mtx); 995125160Sjhb 996125160Sjhb /* 997125160Sjhb * If we already have either an explicit or implied lock order that 998125160Sjhb * is the other way around, then return an error. 999125160Sjhb */ 1000182473Sattilio if (witness_watch && 1001182473Sattilio isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 1002125160Sjhb mtx_unlock_spin(&w_mtx); 1003125160Sjhb return (EDOOFUS); 1004125160Sjhb } 1005125160Sjhb 1006125160Sjhb /* Try to add the new order. */ 1007125160Sjhb CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1008179025Sattilio lock2->lo_witness->w_name, lock1->lo_witness->w_name); 1009181695Sattilio itismychild(lock1->lo_witness, lock2->lo_witness); 1010125160Sjhb mtx_unlock_spin(&w_mtx); 1011125160Sjhb return (0); 1012125160Sjhb} 1013125160Sjhb 101465557Sjasonevoid 1015125160Sjhbwitness_checkorder(struct lock_object *lock, int flags, const char *file, 1016182914Sjhb int line, struct lock_object *interlock) 101765557Sjasone{ 1018183955Sattilio struct lock_list_entry *lock_list, *lle; 1019182914Sjhb struct lock_instance *lock1, *lock2, *plock; 102074912Sjhb struct lock_class *class; 102165856Sjhb struct witness *w, *w1; 102283366Sjulian struct thread *td; 102374912Sjhb int i, j; 102465557Sjasone 1025182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL || 102680747Sjhb panicstr != NULL) 102771320Sjasone return; 1028125160Sjhb 102974912Sjhb w = lock->lo_witness; 1030154077Sjhb class = LOCK_CLASS(lock); 103183366Sjulian td = curthread; 1032112116Sjhb file = fixup_filename(file); 103365557Sjasone 103474912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 1035181695Sattilio 103693676Sjhb /* 103793676Sjhb * Since spin locks include a critical section, this check 1038131884Sjhb * implicitly enforces a lock order of all sleep locks before 103993676Sjhb * all spin locks. 104093676Sjhb */ 1041136304Sgreen if (td->td_critnest != 0 && !kdb_active) 104274912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 104374912Sjhb class->lc_name, lock->lo_name, file, line); 1044131884Sjhb 1045131884Sjhb /* 1046131884Sjhb * If this is the first lock acquired then just return as 1047131884Sjhb * no order checking is needed. 1048131884Sjhb */ 1049183955Sattilio lock_list = td->td_sleeplocks; 1050183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) 1051131884Sjhb return; 1052131884Sjhb } else { 1053181695Sattilio 1054131884Sjhb /* 1055131884Sjhb * If this is the first lock, just return as no order 1056183955Sattilio * checking is needed. Avoid problems with thread 1057183955Sattilio * migration pinning the thread while checking if 1058183955Sattilio * spinlocks are held. If at least one spinlock is held 1059183955Sattilio * the thread is in a safe path and it is allowed to 1060183955Sattilio * unpin it. 1061131884Sjhb */ 1062183955Sattilio sched_pin(); 1063183955Sattilio lock_list = PCPU_GET(spinlocks); 1064183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) { 1065183955Sattilio sched_unpin(); 1066131884Sjhb return; 1067183955Sattilio } 1068183955Sattilio sched_unpin(); 1069131884Sjhb } 107065557Sjasone 107176772Sjhb /* 1072125160Sjhb * Check to see if we are recursing on a lock we already own. If 1073125160Sjhb * so, make sure that we don't mismatch exclusive and shared lock 1074125160Sjhb * acquires. 107576272Sjhb */ 1076183955Sattilio lock1 = find_instance(lock_list, lock); 107776272Sjhb if (lock1 != NULL) { 107876272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 107976272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 108076272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 108176272Sjhb class->lc_name, lock->lo_name, file, line); 108276272Sjhb printf("while exclusively locked from %s:%d\n", 108376272Sjhb lock1->li_file, lock1->li_line); 108476272Sjhb panic("share->excl"); 108576272Sjhb } 108676272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 108776272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 108876272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 108976272Sjhb class->lc_name, lock->lo_name, file, line); 109076272Sjhb printf("while share locked from %s:%d\n", 109176272Sjhb lock1->li_file, lock1->li_line); 109276272Sjhb panic("excl->share"); 109376272Sjhb } 109476272Sjhb return; 109576272Sjhb } 109676272Sjhb 109776272Sjhb /* 1098182914Sjhb * Find the previously acquired lock, but ignore interlocks. 1099182914Sjhb */ 1100183955Sattilio plock = &lock_list->ll_children[lock_list->ll_count - 1]; 1101182914Sjhb if (interlock != NULL && plock->li_lock == interlock) { 1102183955Sattilio if (lock_list->ll_count > 1) 1103183955Sattilio plock = 1104183955Sattilio &lock_list->ll_children[lock_list->ll_count - 2]; 1105183955Sattilio else { 1106183955Sattilio lle = lock_list->ll_next; 1107182984Sattilio 1108182914Sjhb /* 1109182914Sjhb * The interlock is the only lock we hold, so 1110183955Sattilio * simply return. 1111182914Sjhb */ 1112183955Sattilio if (lle == NULL) 1113183955Sattilio return; 1114183955Sattilio plock = &lle->ll_children[lle->ll_count - 1]; 1115182914Sjhb } 1116182914Sjhb } 1117182914Sjhb 1118182914Sjhb /* 1119181695Sattilio * Try to perform most checks without a lock. If this succeeds we 1120181695Sattilio * can skip acquiring the lock and return success. 1121181695Sattilio */ 1122182914Sjhb w1 = plock->li_lock->lo_witness; 1123181695Sattilio if (witness_lock_order_check(w1, w)) 1124181695Sattilio return; 1125181695Sattilio 1126181695Sattilio /* 112774912Sjhb * Check for duplicate locks of the same type. Note that we only 112874912Sjhb * have to check for this on the last lock we just acquired. Any 112974912Sjhb * other cases will be caught as lock order violations. 113074912Sjhb */ 1131181695Sattilio mtx_lock_spin(&w_mtx); 1132181695Sattilio witness_lock_order_add(w1, w); 113374912Sjhb if (w1 == w) { 1134181695Sattilio i = w->w_index; 1135181695Sattilio if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) && 1136181695Sattilio !(w_rmatrix[i][i] & WITNESS_REVERSAL)) { 1137181695Sattilio w_rmatrix[i][i] |= WITNESS_REVERSAL; 1138181695Sattilio w->w_reversed = 1; 1139181695Sattilio mtx_unlock_spin(&w_mtx); 1140183574Sjhb printf( 1141183574Sjhb "acquiring duplicate lock of same type: \"%s\"\n", 1142181695Sattilio w->w_name); 1143183574Sjhb printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name, 1144183574Sjhb plock->li_file, plock->li_line); 1145181695Sattilio printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 1146181695Sattilio witness_debugger(1); 1147181695Sattilio } else 1148181695Sattilio mtx_unlock_spin(&w_mtx); 1149125160Sjhb return; 115065557Sjasone } 1151181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1152181695Sattilio 115365557Sjasone /* 1154111881Sjhb * If we know that the the lock we are acquiring comes after 1155111881Sjhb * the lock we most recently acquired in the lock order tree, 1156111881Sjhb * then there is no need for any further checks. 1157111881Sjhb */ 1158181695Sattilio if (isitmychild(w1, w)) 1159181695Sattilio goto out; 1160181695Sattilio 1161183955Sattilio for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) { 116274912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 116365557Sjasone 116474912Sjhb MPASS(j < WITNESS_COUNT); 116576272Sjhb lock1 = &lle->ll_children[i]; 116674912Sjhb 116774912Sjhb /* 1168182914Sjhb * Ignore the interlock the first time we see it. 1169182914Sjhb */ 1170182914Sjhb if (interlock != NULL && interlock == lock1->li_lock) { 1171182914Sjhb interlock = NULL; 1172182914Sjhb continue; 1173182914Sjhb } 1174182914Sjhb 1175182914Sjhb /* 117674912Sjhb * If this lock doesn't undergo witness checking, 117774912Sjhb * then skip it. 117874912Sjhb */ 1179182914Sjhb w1 = lock1->li_lock->lo_witness; 118074912Sjhb if (w1 == NULL) { 118176272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 118274912Sjhb ("lock missing witness structure")); 118374912Sjhb continue; 118474912Sjhb } 1185181695Sattilio 118676272Sjhb /* 1187111881Sjhb * If we are locking Giant and this is a sleepable 118876272Sjhb * lock, then skip it. 118976272Sjhb */ 1190111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 1191167787Sjhb lock == &Giant.lock_object) 119276272Sjhb continue; 1193181695Sattilio 119493690Sjhb /* 119593690Sjhb * If we are locking a sleepable lock and this lock 1196111881Sjhb * is Giant, then skip it. 119793690Sjhb */ 1198111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 1199167787Sjhb lock1->li_lock == &Giant.lock_object) 1200111881Sjhb continue; 1201181695Sattilio 1202111881Sjhb /* 1203111881Sjhb * If we are locking a sleepable lock and this lock 1204111881Sjhb * isn't sleepable, we want to treat it as a lock 1205111881Sjhb * order violation to enfore a general lock order of 1206111881Sjhb * sleepable locks before non-sleepable locks. 1207111881Sjhb */ 1208149738Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1209111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1210149738Sjhb goto reversal; 1211181695Sattilio 1212149738Sjhb /* 1213150179Sjhb * If we are locking Giant and this is a non-sleepable 1214150179Sjhb * lock, then treat it as a reversal. 1215150179Sjhb */ 1216150179Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 1217167787Sjhb lock == &Giant.lock_object) 1218150179Sjhb goto reversal; 1219181695Sattilio 1220150179Sjhb /* 1221149738Sjhb * Check the lock order hierarchy for a reveresal. 1222149738Sjhb */ 1223149738Sjhb if (!isitmydescendant(w, w1)) 122474912Sjhb continue; 1225149738Sjhb reversal: 1226181695Sattilio 122774912Sjhb /* 122874912Sjhb * We have a lock order violation, check to see if it 122974912Sjhb * is allowed or has already been yelled about. 123074912Sjhb */ 1231105508Sphk#ifdef BLESSING 1232181695Sattilio 1233125160Sjhb /* 1234125160Sjhb * If the lock order is blessed, just bail. We don't 1235125160Sjhb * look for other lock order violations though, which 1236125160Sjhb * may be a bug. 1237125160Sjhb */ 123865557Sjasone if (blessed(w, w1)) 1239181695Sattilio goto out; 1240105508Sphk#endif 1241181695Sattilio 1242181695Sattilio /* Bail if this violation is known */ 1243181695Sattilio if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL) 1244181695Sattilio goto out; 1245181695Sattilio 1246181695Sattilio /* Record this as a violation */ 1247181695Sattilio w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL; 1248181695Sattilio w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL; 1249181695Sattilio w->w_reversed = w1->w_reversed = 1; 1250181695Sattilio witness_increment_graph_generation(); 1251181695Sattilio mtx_unlock_spin(&w_mtx); 1252181695Sattilio 125374912Sjhb /* 125474912Sjhb * Ok, yell about it. 125574912Sjhb */ 1256150179Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1257150179Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1258150179Sjhb printf( 1259150179Sjhb "lock order reversal: (sleepable after non-sleepable)\n"); 1260150179Sjhb else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1261167787Sjhb && lock == &Giant.lock_object) 1262150179Sjhb printf( 1263150179Sjhb "lock order reversal: (Giant after non-sleepable)\n"); 1264150179Sjhb else 1265150179Sjhb printf("lock order reversal:\n"); 1266181695Sattilio 126774912Sjhb /* 126874912Sjhb * Try to locate an earlier lock with 126974912Sjhb * witness w in our list. 127074912Sjhb */ 127174912Sjhb do { 127276272Sjhb lock2 = &lle->ll_children[i]; 127376272Sjhb MPASS(lock2->li_lock != NULL); 127476272Sjhb if (lock2->li_lock->lo_witness == w) 127574912Sjhb break; 127674912Sjhb if (i == 0 && lle->ll_next != NULL) { 127774912Sjhb lle = lle->ll_next; 127874912Sjhb i = lle->ll_count - 1; 1279106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 1280125160Sjhb } else 1281125160Sjhb i--; 128274912Sjhb } while (i >= 0); 128376272Sjhb if (i < 0) { 128493811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 128593811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 1286179025Sattilio w1->w_name, lock1->li_file, lock1->li_line); 128793811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 1288179025Sattilio lock->lo_name, w->w_name, file, line); 128976272Sjhb } else { 129093811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 129193811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 1292179025Sattilio lock2->li_lock->lo_witness->w_name, 1293179025Sattilio lock2->li_file, lock2->li_line); 129493811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 129593811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 1296179025Sattilio w1->w_name, lock1->li_file, lock1->li_line); 129793811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 1298179025Sattilio lock->lo_name, w->w_name, file, line); 129976272Sjhb } 1300181695Sattilio witness_debugger(1); 1301125160Sjhb return; 130265557Sjasone } 130365557Sjasone } 1304181695Sattilio 130578871Sjhb /* 1306125160Sjhb * If requested, build a new lock order. However, don't build a new 1307125160Sjhb * relationship between a sleepable lock and Giant if it is in the 1308125160Sjhb * wrong direction. The correct lock order is that sleepable locks 1309125160Sjhb * always come before Giant. 131078871Sjhb */ 1311125160Sjhb if (flags & LOP_NEWORDER && 1312182914Sjhb !(plock->li_lock == &Giant.lock_object && 1313112117Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0)) { 131487593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1315182914Sjhb w->w_name, plock->li_lock->lo_witness->w_name); 1316182914Sjhb itismychild(plock->li_lock->lo_witness, w); 1317181695Sattilio } 1318181695Sattilioout: 1319112117Sjhb mtx_unlock_spin(&w_mtx); 1320125160Sjhb} 1321125160Sjhb 1322125160Sjhbvoid 1323125160Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 1324125160Sjhb{ 1325125160Sjhb struct lock_list_entry **lock_list, *lle; 1326125160Sjhb struct lock_instance *instance; 1327125160Sjhb struct witness *w; 1328125160Sjhb struct thread *td; 1329125160Sjhb 1330182446Sattilio if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL || 1331125160Sjhb panicstr != NULL) 1332125160Sjhb return; 1333125160Sjhb w = lock->lo_witness; 1334125160Sjhb td = curthread; 1335125160Sjhb file = fixup_filename(file); 1336125160Sjhb 1337125160Sjhb /* Determine lock list for this lock. */ 1338154077Sjhb if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1339125160Sjhb lock_list = &td->td_sleeplocks; 1340125160Sjhb else 1341125160Sjhb lock_list = PCPU_PTR(spinlocks); 1342125160Sjhb 1343125160Sjhb /* Check to see if we are recursing on a lock we already own. */ 1344125160Sjhb instance = find_instance(*lock_list, lock); 1345125160Sjhb if (instance != NULL) { 1346125160Sjhb instance->li_flags++; 1347125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1348125160Sjhb td->td_proc->p_pid, lock->lo_name, 1349125160Sjhb instance->li_flags & LI_RECURSEMASK); 1350125160Sjhb instance->li_file = file; 1351125160Sjhb instance->li_line = line; 1352125160Sjhb return; 1353110779Speter } 1354125160Sjhb 1355125160Sjhb /* Update per-witness last file and line acquire. */ 135665557Sjasone w->w_file = file; 135765557Sjasone w->w_line = line; 1358125160Sjhb 1359125160Sjhb /* Find the next open lock instance in the list and fill it. */ 136074912Sjhb lle = *lock_list; 136176272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 136278785Sjhb lle = witness_lock_list_get(); 136378785Sjhb if (lle == NULL) 136465557Sjasone return; 136578785Sjhb lle->ll_next = *lock_list; 136687593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 136784680Sjhb td->td_proc->p_pid, lle); 136878785Sjhb *lock_list = lle; 136965557Sjasone } 1370125160Sjhb instance = &lle->ll_children[lle->ll_count++]; 1371125160Sjhb instance->li_lock = lock; 1372125160Sjhb instance->li_line = line; 1373125160Sjhb instance->li_file = file; 137476272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 1375125160Sjhb instance->li_flags = LI_EXCLUSIVE; 137676272Sjhb else 1377125160Sjhb instance->li_flags = 0; 137887593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 137984680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 138065557Sjasone} 138165557Sjasone 138265557Sjasonevoid 138382244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 138482244Sjhb{ 138582244Sjhb struct lock_instance *instance; 138682244Sjhb struct lock_class *class; 138782244Sjhb 1388181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1389182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 139082244Sjhb return; 1391154077Sjhb class = LOCK_CLASS(lock); 1392112116Sjhb file = fixup_filename(file); 1393182473Sattilio if (witness_watch) { 1394182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1395182473Sattilio panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 1396182473Sattilio class->lc_name, lock->lo_name, file, line); 1397182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1398182473Sattilio panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 1399182473Sattilio class->lc_name, lock->lo_name, file, line); 1400182473Sattilio } 140183366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 140282244Sjhb if (instance == NULL) 140382244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 140482244Sjhb class->lc_name, lock->lo_name, file, line); 1405182473Sattilio if (witness_watch) { 1406182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1407182473Sattilio panic("upgrade of exclusive lock (%s) %s @ %s:%d", 1408182473Sattilio class->lc_name, lock->lo_name, file, line); 1409182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1410182473Sattilio panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1411182473Sattilio class->lc_name, lock->lo_name, 1412182473Sattilio instance->li_flags & LI_RECURSEMASK, file, line); 1413182473Sattilio } 141482244Sjhb instance->li_flags |= LI_EXCLUSIVE; 141582244Sjhb} 141682244Sjhb 141782244Sjhbvoid 141882244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 141982244Sjhb int line) 142082244Sjhb{ 142182244Sjhb struct lock_instance *instance; 142282244Sjhb struct lock_class *class; 142382244Sjhb 1424181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1425182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 142682244Sjhb return; 1427154077Sjhb class = LOCK_CLASS(lock); 1428112116Sjhb file = fixup_filename(file); 1429182473Sattilio if (witness_watch) { 1430182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 143182244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 1432182473Sattilio class->lc_name, lock->lo_name, file, line); 1433182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1434182473Sattilio panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 1435182473Sattilio class->lc_name, lock->lo_name, file, line); 1436182473Sattilio } 143783366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 143882244Sjhb if (instance == NULL) 143982244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 144082244Sjhb class->lc_name, lock->lo_name, file, line); 1441182473Sattilio if (witness_watch) { 1442182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1443182473Sattilio panic("downgrade of shared lock (%s) %s @ %s:%d", 1444182473Sattilio class->lc_name, lock->lo_name, file, line); 1445182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1446182473Sattilio panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1447182473Sattilio class->lc_name, lock->lo_name, 1448182473Sattilio instance->li_flags & LI_RECURSEMASK, file, line); 1449182473Sattilio } 145082244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 145182244Sjhb} 145282244Sjhb 145382244Sjhbvoid 145474912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 145565557Sjasone{ 145674912Sjhb struct lock_list_entry **lock_list, *lle; 145776272Sjhb struct lock_instance *instance; 145874912Sjhb struct lock_class *class; 145983366Sjulian struct thread *td; 146092858Simp register_t s; 146174912Sjhb int i, j; 146265557Sjasone 1463182446Sattilio if (witness_cold || lock->lo_witness == NULL || panicstr != NULL) 146471352Sjasone return; 146583366Sjulian td = curthread; 1466154077Sjhb class = LOCK_CLASS(lock); 1467112116Sjhb file = fixup_filename(file); 1468125160Sjhb 1469125160Sjhb /* Find lock instance associated with this lock. */ 147076272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 147183366Sjulian lock_list = &td->td_sleeplocks; 147276272Sjhb else 147374912Sjhb lock_list = PCPU_PTR(spinlocks); 1474181695Sattilio lle = *lock_list; 147574912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 147676272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 147776272Sjhb instance = &(*lock_list)->ll_children[i]; 1478125160Sjhb if (instance->li_lock == lock) 1479125160Sjhb goto found; 148076272Sjhb } 1481182446Sattilio 1482182446Sattilio /* 1483182446Sattilio * When disabling WITNESS through witness_watch we could end up in 1484182473Sattilio * having registered locks in the td_sleeplocks queue. 1485182446Sattilio * We have to make sure we flush these queues, so just search for 1486182473Sattilio * eventual register locks and remove them. 1487182446Sattilio */ 1488182446Sattilio if (witness_watch > 0) 1489182446Sattilio panic("lock (%s) %s not locked @ %s:%d", class->lc_name, 1490182446Sattilio lock->lo_name, file, line); 1491182446Sattilio else 1492182446Sattilio return; 1493125160Sjhbfound: 1494125160Sjhb 1495125160Sjhb /* First, check for shared/exclusive mismatches. */ 1496182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 && 1497125160Sjhb (flags & LOP_EXCLUSIVE) == 0) { 1498125160Sjhb printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1499125160Sjhb lock->lo_name, file, line); 1500125160Sjhb printf("while exclusively locked from %s:%d\n", 1501125160Sjhb instance->li_file, instance->li_line); 1502125160Sjhb panic("excl->ushare"); 1503125160Sjhb } 1504182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 && 1505125160Sjhb (flags & LOP_EXCLUSIVE) != 0) { 1506125160Sjhb printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1507125160Sjhb lock->lo_name, file, line); 1508125160Sjhb printf("while share locked from %s:%d\n", instance->li_file, 1509125160Sjhb instance->li_line); 1510125160Sjhb panic("share->uexcl"); 1511125160Sjhb } 1512125160Sjhb 1513125160Sjhb /* If we are recursed, unrecurse. */ 1514125160Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1515125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1516125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1517125160Sjhb instance->li_flags); 1518125160Sjhb instance->li_flags--; 1519125160Sjhb return; 1520125160Sjhb } 1521125160Sjhb 1522125160Sjhb /* Otherwise, remove this item from the list. */ 1523125160Sjhb s = intr_disable(); 1524125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1525125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1526125160Sjhb (*lock_list)->ll_count - 1); 1527125160Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 1528125160Sjhb (*lock_list)->ll_children[j] = 1529125160Sjhb (*lock_list)->ll_children[j + 1]; 1530125160Sjhb (*lock_list)->ll_count--; 1531125160Sjhb intr_restore(s); 1532125160Sjhb 1533181695Sattilio /* 1534182984Sattilio * In order to reduce contention on w_mtx, we want to keep always an 1535182984Sattilio * head object into lists so that frequent allocation from the 1536182984Sattilio * free witness pool (and subsequent locking) is avoided. 1537182984Sattilio * In order to maintain the current code simple, when the head 1538182984Sattilio * object is totally unloaded it means also that we do not have 1539182984Sattilio * further objects in the list, so the list ownership needs to be 1540182984Sattilio * hand over to another object if the current head needs to be freed. 1541181695Sattilio */ 1542182984Sattilio if ((*lock_list)->ll_count == 0) { 1543182984Sattilio if (*lock_list == lle) { 1544182984Sattilio if (lle->ll_next == NULL) 1545182984Sattilio return; 1546182984Sattilio } else 1547182984Sattilio lle = *lock_list; 1548125160Sjhb *lock_list = lle->ll_next; 1549125160Sjhb CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1550125160Sjhb td->td_proc->p_pid, lle); 1551125160Sjhb witness_lock_list_free(lle); 1552125160Sjhb } 155365557Sjasone} 155465557Sjasone 1555181695Sattiliovoid 1556181695Sattiliowitness_thread_exit(struct thread *td) 1557181695Sattilio{ 1558181695Sattilio struct lock_list_entry *lle; 1559181695Sattilio int i, n; 1560181695Sattilio 1561181695Sattilio lle = td->td_sleeplocks; 1562181695Sattilio if (lle == NULL || panicstr != NULL) 1563181695Sattilio return; 1564181695Sattilio if (lle->ll_count != 0) { 1565181695Sattilio for (n = 0; lle != NULL; lle = lle->ll_next) 1566181695Sattilio for (i = lle->ll_count - 1; i >= 0; i--) { 1567181695Sattilio if (n == 0) 1568181695Sattilio printf("Thread %p exiting with the following locks held:\n", 1569181695Sattilio td); 1570181695Sattilio n++; 1571181695Sattilio witness_list_lock(&lle->ll_children[i]); 1572181695Sattilio 1573181695Sattilio } 1574181695Sattilio panic("Thread %p cannot exit while holding sleeplocks\n", td); 1575181695Sattilio } 1576181695Sattilio witness_lock_list_free(lle); 1577181695Sattilio} 1578181695Sattilio 157974912Sjhb/* 1580111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 1581111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 1582111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 1583111881Sjhb * console along with a list of the offending locks. If indicated in the 1584111881Sjhb * flags then a failure results in a panic as well. 158574912Sjhb */ 158665557Sjasoneint 1587111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 158865557Sjasone{ 1589183955Sattilio struct lock_list_entry *lock_list, *lle; 159076272Sjhb struct lock_instance *lock1; 159183366Sjulian struct thread *td; 1592111881Sjhb va_list ap; 159374912Sjhb int i, n; 159465557Sjasone 1595182446Sattilio if (witness_cold || witness_watch < 1 || panicstr != NULL) 159674912Sjhb return (0); 159774912Sjhb n = 0; 159883366Sjulian td = curthread; 1599111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 160074912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 160176272Sjhb lock1 = &lle->ll_children[i]; 1602111881Sjhb if (lock1->li_lock == lock) 1603111881Sjhb continue; 1604111881Sjhb if (flags & WARN_GIANTOK && 1605167787Sjhb lock1->li_lock == &Giant.lock_object) 160674912Sjhb continue; 1607111881Sjhb if (flags & WARN_SLEEPOK && 1608111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 160976272Sjhb continue; 1610111881Sjhb if (n == 0) { 1611111881Sjhb va_start(ap, fmt); 1612111881Sjhb vprintf(fmt, ap); 1613111881Sjhb va_end(ap); 1614111881Sjhb printf(" with the following"); 1615111881Sjhb if (flags & WARN_SLEEPOK) 1616111881Sjhb printf(" non-sleepable"); 1617118441Sjhb printf(" locks held:\n"); 161876272Sjhb } 161974912Sjhb n++; 1620111881Sjhb witness_list_lock(lock1); 162174912Sjhb } 1622181695Sattilio 1623183955Sattilio /* 1624183955Sattilio * Pin the thread in order to avoid problems with thread migration. 1625183955Sattilio * Once that all verifies are passed about spinlocks ownership, 1626183955Sattilio * the thread is in a safe path and it can be unpinned. 1627183955Sattilio */ 1628183955Sattilio sched_pin(); 1629183955Sattilio lock_list = PCPU_GET(spinlocks); 1630184098Sattilio if (lock_list != NULL && lock_list->ll_count != 0) { 1631183955Sattilio sched_unpin(); 1632181695Sattilio 163397006Sjhb /* 1634183955Sattilio * We should only have one spinlock and as long as 1635183955Sattilio * the flags cannot match for this locks class, 1636183955Sattilio * check if the first spinlock is the one curthread 1637183955Sattilio * should hold. 163897006Sjhb */ 1639183955Sattilio lock1 = &lock_list->ll_children[lock_list->ll_count - 1]; 1640184098Sattilio if (lock_list->ll_count == 1 && lock_list->ll_next == NULL && 1641184098Sattilio lock1->li_lock == lock && n == 0) 1642184098Sattilio return (0); 1643183955Sattilio 1644184098Sattilio va_start(ap, fmt); 1645184098Sattilio vprintf(fmt, ap); 1646184098Sattilio va_end(ap); 1647184098Sattilio printf(" with the following"); 1648184098Sattilio if (flags & WARN_SLEEPOK) 1649184098Sattilio printf(" non-sleepable"); 1650184098Sattilio printf(" locks held:\n"); 1651183955Sattilio n += witness_list_locks(&lock_list); 1652183955Sattilio } else 1653183955Sattilio sched_unpin(); 1654111881Sjhb if (flags & WARN_PANIC && n) 1655181695Sattilio panic("%s", __func__); 1656181695Sattilio else 1657181695Sattilio witness_debugger(n); 165865557Sjasone return (n); 165965557Sjasone} 166065557Sjasone 1661102448Siedowseconst char * 1662102448Siedowsewitness_file(struct lock_object *lock) 1663102448Siedowse{ 1664102448Siedowse struct witness *w; 1665102448Siedowse 1666182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1667102448Siedowse return ("?"); 1668102448Siedowse w = lock->lo_witness; 1669102448Siedowse return (w->w_file); 1670102448Siedowse} 1671102448Siedowse 1672102448Siedowseint 1673102448Siedowsewitness_line(struct lock_object *lock) 1674102448Siedowse{ 1675102448Siedowse struct witness *w; 1676102448Siedowse 1677182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1678102448Siedowse return (0); 1679102448Siedowse w = lock->lo_witness; 1680102448Siedowse return (w->w_line); 1681102448Siedowse} 1682102448Siedowse 168365856Sjhbstatic struct witness * 168474912Sjhbenroll(const char *description, struct lock_class *lock_class) 168565557Sjasone{ 168674912Sjhb struct witness *w; 1687181695Sattilio struct witness_list *typelist; 168865557Sjasone 1689181695Sattilio MPASS(description != NULL); 1690181695Sattilio 1691182473Sattilio if (witness_watch == -1 || panicstr != NULL) 169265557Sjasone return (NULL); 1693181695Sattilio if ((lock_class->lc_flags & LC_SPINLOCK)) { 1694181695Sattilio if (witness_skipspin) 1695181695Sattilio return (NULL); 1696181695Sattilio else 1697181695Sattilio typelist = &w_spin; 1698181695Sattilio } else if ((lock_class->lc_flags & LC_SLEEPLOCK)) 1699181695Sattilio typelist = &w_sleep; 1700181695Sattilio else 1701181695Sattilio panic("lock class %s is not sleep or spin", 1702181695Sattilio lock_class->lc_name); 1703181695Sattilio 1704181695Sattilio mtx_lock_spin(&w_mtx); 1705181695Sattilio w = witness_hash_get(description); 1706181695Sattilio if (w) 1707181695Sattilio goto found; 1708181695Sattilio if ((w = witness_get()) == NULL) 170965557Sjasone return (NULL); 1710181695Sattilio MPASS(strlen(description) < MAX_W_NAME); 1711181695Sattilio strcpy(w->w_name, description); 171274912Sjhb w->w_class = lock_class; 171375362Sjhb w->w_refcount = 1; 171474912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 1715149441Struckman if (lock_class->lc_flags & LC_SPINLOCK) { 171674912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1717149441Struckman w_spin_cnt++; 1718149441Struckman } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 171974912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1720149441Struckman w_sleep_cnt++; 172175364Sbp } 1722181695Sattilio 1723181695Sattilio /* Insert new witness into the hash */ 1724181695Sattilio witness_hash_put(w); 1725181695Sattilio witness_increment_graph_generation(); 172674912Sjhb mtx_unlock_spin(&w_mtx); 172765557Sjasone return (w); 1728181695Sattiliofound: 1729181695Sattilio w->w_refcount++; 1730181695Sattilio mtx_unlock_spin(&w_mtx); 1731181695Sattilio if (lock_class != w->w_class) 1732181695Sattilio panic( 1733181695Sattilio "lock (%s) %s does not match earlier (%s) lock", 1734181695Sattilio description, lock_class->lc_name, 1735181695Sattilio w->w_class->lc_name); 1736181695Sattilio return (w); 173765557Sjasone} 173865557Sjasone 1739179025Sattiliostatic void 1740112117Sjhbdepart(struct witness *w) 174165557Sjasone{ 174274912Sjhb struct witness_list *list; 174365557Sjasone 1744112117Sjhb MPASS(w->w_refcount == 0); 1745149441Struckman if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1746112117Sjhb list = &w_sleep; 1747149441Struckman w_sleep_cnt--; 1748149441Struckman } else { 1749112117Sjhb list = &w_spin; 1750149441Struckman w_spin_cnt--; 1751149441Struckman } 1752112117Sjhb /* 1753181695Sattilio * Set file to NULL as it may point into a loadable module. 1754112117Sjhb */ 1755181695Sattilio w->w_file = NULL; 1756181695Sattilio w->w_line = 0; 1757181695Sattilio witness_increment_graph_generation(); 1758181695Sattilio} 1759112117Sjhb 1760181695Sattilio 1761181695Sattiliostatic void 1762181695Sattilioadopt(struct witness *parent, struct witness *child) 1763181695Sattilio{ 1764181695Sattilio int pi, ci, i, j; 1765181695Sattilio 1766181695Sattilio if (witness_cold == 0) 1767181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1768181695Sattilio 1769181695Sattilio /* If the relationship is already known, there's no work to be done. */ 1770181695Sattilio if (isitmychild(parent, child)) 1771181695Sattilio return; 1772181695Sattilio 1773181695Sattilio /* When the structure of the graph changes, bump up the generation. */ 1774181695Sattilio witness_increment_graph_generation(); 1775181695Sattilio 1776112117Sjhb /* 1777181695Sattilio * The hard part ... create the direct relationship, then propagate all 1778181695Sattilio * indirect relationships. 1779112117Sjhb */ 1780181695Sattilio pi = parent->w_index; 1781181695Sattilio ci = child->w_index; 1782181695Sattilio WITNESS_INDEX_ASSERT(pi); 1783181695Sattilio WITNESS_INDEX_ASSERT(ci); 1784181695Sattilio MPASS(pi != ci); 1785181695Sattilio w_rmatrix[pi][ci] |= WITNESS_PARENT; 1786181695Sattilio w_rmatrix[ci][pi] |= WITNESS_CHILD; 1787112117Sjhb 1788112117Sjhb /* 1789181695Sattilio * If parent was not already an ancestor of child, 1790181695Sattilio * then we increment the descendant and ancestor counters. 1791112117Sjhb */ 1792181695Sattilio if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) { 1793181695Sattilio parent->w_num_descendants++; 1794181695Sattilio child->w_num_ancestors++; 1795181695Sattilio } 1796112117Sjhb 1797181695Sattilio /* 1798181695Sattilio * Find each ancestor of 'pi'. Note that 'pi' itself is counted as 1799181695Sattilio * an ancestor of 'pi' during this loop. 1800181695Sattilio */ 1801181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 1802181695Sattilio if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 && 1803181695Sattilio (i != pi)) 1804181695Sattilio continue; 1805112117Sjhb 1806181695Sattilio /* Find each descendant of 'i' and mark it as a descendant. */ 1807181695Sattilio for (j = 1; j <= w_max_used_index; j++) { 180874912Sjhb 1809181695Sattilio /* 1810181695Sattilio * Skip children that are already marked as 1811181695Sattilio * descendants of 'i'. 1812181695Sattilio */ 1813181695Sattilio if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) 1814181695Sattilio continue; 1815181695Sattilio 1816181695Sattilio /* 1817181695Sattilio * We are only interested in descendants of 'ci'. Note 1818181695Sattilio * that 'ci' itself is counted as a descendant of 'ci'. 1819181695Sattilio */ 1820181695Sattilio if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 && 1821181695Sattilio (j != ci)) 1822181695Sattilio continue; 1823181695Sattilio w_rmatrix[i][j] |= WITNESS_ANCESTOR; 1824181695Sattilio w_rmatrix[j][i] |= WITNESS_DESCENDANT; 1825181695Sattilio w_data[i].w_num_descendants++; 1826181695Sattilio w_data[j].w_num_ancestors++; 1827181695Sattilio 1828181695Sattilio /* 1829181695Sattilio * Make sure we aren't marking a node as both an 1830181695Sattilio * ancestor and descendant. We should have caught 1831181695Sattilio * this as a lock order reversal earlier. 1832181695Sattilio */ 1833181695Sattilio if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) && 1834181695Sattilio (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) { 1835181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1836181695Sattilio "both ancestor and descendant\n", 1837181695Sattilio i, j, w_rmatrix[i][j]); 1838181695Sattilio kdb_backtrace(); 1839181695Sattilio printf("Witness disabled.\n"); 1840182446Sattilio witness_watch = -1; 1841181695Sattilio } 1842181695Sattilio if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) && 1843181695Sattilio (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) { 1844181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1845181695Sattilio "both ancestor and descendant\n", 1846181695Sattilio j, i, w_rmatrix[j][i]); 1847181695Sattilio kdb_backtrace(); 1848181695Sattilio printf("Witness disabled.\n"); 1849182446Sattilio witness_watch = -1; 1850181695Sattilio } 1851181695Sattilio } 185265557Sjasone } 1853112117Sjhb} 1854112117Sjhb 1855181695Sattiliostatic void 1856112117Sjhbitismychild(struct witness *parent, struct witness *child) 1857112117Sjhb{ 1858112117Sjhb 1859112117Sjhb MPASS(child != NULL && parent != NULL); 1860181695Sattilio if (witness_cold == 0) 1861181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1862181695Sattilio 1863181695Sattilio if (!witness_lock_type_equal(parent, child)) { 1864181695Sattilio if (witness_cold == 0) 1865181695Sattilio mtx_unlock_spin(&w_mtx); 1866181695Sattilio panic("%s: parent \"%s\" (%s) and child \"%s\" (%s) are not " 1867181695Sattilio "the same lock type", __func__, parent->w_name, 1868181695Sattilio parent->w_class->lc_name, child->w_name, 1869112117Sjhb child->w_class->lc_name); 1870181695Sattilio } 1871181695Sattilio adopt(parent, child); 187265557Sjasone} 187365557Sjasone 1874181695Sattilio/* 1875181695Sattilio * Generic code for the isitmy*() functions. The rmask parameter is the 1876181695Sattilio * expected relationship of w1 to w2. 1877181695Sattilio */ 1878181695Sattiliostatic int 1879181695Sattilio_isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname) 188065557Sjasone{ 1881181695Sattilio unsigned char r1, r2; 1882181695Sattilio int i1, i2; 188365557Sjasone 1884181695Sattilio i1 = w1->w_index; 1885181695Sattilio i2 = w2->w_index; 1886181695Sattilio WITNESS_INDEX_ASSERT(i1); 1887181695Sattilio WITNESS_INDEX_ASSERT(i2); 1888181695Sattilio r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK; 1889181695Sattilio r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK; 1890181695Sattilio 1891181695Sattilio /* The flags on one better be the inverse of the flags on the other */ 1892181695Sattilio if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) || 1893181695Sattilio (WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) { 1894181695Sattilio printf("%s: rmatrix mismatch between %s (index %d) and %s " 1895181695Sattilio "(index %d): w_rmatrix[%d][%d] == %hhx but " 1896181695Sattilio "w_rmatrix[%d][%d] == %hhx\n", 1897181695Sattilio fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1, 1898181695Sattilio i2, i1, r2); 1899181695Sattilio kdb_backtrace(); 1900181695Sattilio printf("Witness disabled.\n"); 1901182446Sattilio witness_watch = -1; 1902181695Sattilio } 1903181695Sattilio return (r1 & rmask); 190465557Sjasone} 190565557Sjasone 1906181695Sattilio/* 1907181695Sattilio * Checks if @child is a direct child of @parent. 1908181695Sattilio */ 190965557Sjasonestatic int 191065856Sjhbisitmychild(struct witness *parent, struct witness *child) 191165557Sjasone{ 191265557Sjasone 1913181695Sattilio return (_isitmyx(parent, child, WITNESS_PARENT, __func__)); 191465557Sjasone} 191565557Sjasone 1916181695Sattilio/* 1917181695Sattilio * Checks if @descendant is a direct or inderect descendant of @ancestor. 1918181695Sattilio */ 191965557Sjasonestatic int 1920181695Sattilioisitmydescendant(struct witness *ancestor, struct witness *descendant) 192165557Sjasone{ 192265557Sjasone 1923181695Sattilio return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK, 1924181695Sattilio __func__)); 192565557Sjasone} 192665557Sjasone 1927105508Sphk#ifdef BLESSING 192865557Sjasonestatic int 192965856Sjhbblessed(struct witness *w1, struct witness *w2) 193065557Sjasone{ 193165557Sjasone int i; 193265856Sjhb struct witness_blessed *b; 193365557Sjasone 193465557Sjasone for (i = 0; i < blessed_count; i++) { 193565557Sjasone b = &blessed_list[i]; 193674912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 193774912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 193865557Sjasone return (1); 193965557Sjasone continue; 194065557Sjasone } 194174912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 194274912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 194365557Sjasone return (1); 194465557Sjasone } 194565557Sjasone return (0); 194665557Sjasone} 1947105508Sphk#endif 194865557Sjasone 194965856Sjhbstatic struct witness * 195074912Sjhbwitness_get(void) 195165557Sjasone{ 195265856Sjhb struct witness *w; 1953181695Sattilio int index; 195465557Sjasone 1955181695Sattilio if (witness_cold == 0) 1956181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1957181695Sattilio 1958182473Sattilio if (witness_watch == -1) { 195976481Sjhb mtx_unlock_spin(&w_mtx); 196076481Sjhb return (NULL); 196176481Sjhb } 196274912Sjhb if (STAILQ_EMPTY(&w_free)) { 1963182446Sattilio witness_watch = -1; 196474912Sjhb mtx_unlock_spin(&w_mtx); 1965181695Sattilio printf("WITNESS: unable to allocate a new witness object\n"); 196665557Sjasone return (NULL); 196765557Sjasone } 196874912Sjhb w = STAILQ_FIRST(&w_free); 196974912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 1970149441Struckman w_free_cnt--; 1971181695Sattilio index = w->w_index; 1972181695Sattilio MPASS(index > 0 && index == w_max_used_index+1 && 1973181695Sattilio index < WITNESS_COUNT); 197465856Sjhb bzero(w, sizeof(*w)); 1975181695Sattilio w->w_index = index; 1976181695Sattilio if (index > w_max_used_index) 1977181695Sattilio w_max_used_index = index; 197865557Sjasone return (w); 197965557Sjasone} 198065557Sjasone 198165557Sjasonestatic void 198265856Sjhbwitness_free(struct witness *w) 198365557Sjasone{ 198474912Sjhb 198574912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 1986149441Struckman w_free_cnt++; 198765557Sjasone} 198865557Sjasone 198974912Sjhbstatic struct lock_list_entry * 199074912Sjhbwitness_lock_list_get(void) 199174912Sjhb{ 199274912Sjhb struct lock_list_entry *lle; 199371709Sjhb 1994182446Sattilio if (witness_watch == -1) 199576481Sjhb return (NULL); 199674912Sjhb mtx_lock_spin(&w_mtx); 199774912Sjhb lle = w_lock_list_free; 199874912Sjhb if (lle == NULL) { 1999182446Sattilio witness_watch = -1; 200074912Sjhb mtx_unlock_spin(&w_mtx); 200174912Sjhb printf("%s: witness exhausted\n", __func__); 200274912Sjhb return (NULL); 200374912Sjhb } 200474912Sjhb w_lock_list_free = lle->ll_next; 200574912Sjhb mtx_unlock_spin(&w_mtx); 200674912Sjhb bzero(lle, sizeof(*lle)); 200774912Sjhb return (lle); 200874912Sjhb} 200974912Sjhb 201074912Sjhbstatic void 201174912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 201271709Sjhb{ 201371709Sjhb 201474912Sjhb mtx_lock_spin(&w_mtx); 201574912Sjhb lle->ll_next = w_lock_list_free; 201674912Sjhb w_lock_list_free = lle; 201774912Sjhb mtx_unlock_spin(&w_mtx); 201871709Sjhb} 201971709Sjhb 202076272Sjhbstatic struct lock_instance * 2021181695Sattiliofind_instance(struct lock_list_entry *list, struct lock_object *lock) 202276272Sjhb{ 202376272Sjhb struct lock_list_entry *lle; 202476272Sjhb struct lock_instance *instance; 202576272Sjhb int i; 202676272Sjhb 2027181695Sattilio for (lle = list; lle != NULL; lle = lle->ll_next) 202876272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 202976272Sjhb instance = &lle->ll_children[i]; 203076272Sjhb if (instance->li_lock == lock) 203176272Sjhb return (instance); 203276272Sjhb } 203376272Sjhb return (NULL); 203476272Sjhb} 203576272Sjhb 2036111881Sjhbstatic void 2037111881Sjhbwitness_list_lock(struct lock_instance *instance) 2038111881Sjhb{ 2039111881Sjhb struct lock_object *lock; 2040111881Sjhb 2041111881Sjhb lock = instance->li_lock; 2042111881Sjhb printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 2043154077Sjhb "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 2044179025Sattilio if (lock->lo_witness->w_name != lock->lo_name) 2045179025Sattilio printf(" (%s)", lock->lo_witness->w_name); 2046111881Sjhb printf(" r = %d (%p) locked @ %s:%d\n", 2047111881Sjhb instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 2048111881Sjhb instance->li_line); 2049111881Sjhb} 2050111881Sjhb 2051140637Srwatson#ifdef DDB 2052139333Srwatsonstatic int 2053139333Srwatsonwitness_thread_has_locks(struct thread *td) 2054139333Srwatson{ 2055139333Srwatson 2056182984Sattilio if (td->td_sleeplocks == NULL) 2057182984Sattilio return (0); 2058182984Sattilio return (td->td_sleeplocks->ll_count != 0); 2059139333Srwatson} 2060139333Srwatson 2061139333Srwatsonstatic int 2062139333Srwatsonwitness_proc_has_locks(struct proc *p) 2063139333Srwatson{ 2064139333Srwatson struct thread *td; 2065139333Srwatson 2066139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2067139333Srwatson if (witness_thread_has_locks(td)) 2068139333Srwatson return (1); 2069139333Srwatson } 2070139333Srwatson return (0); 2071139333Srwatson} 2072140637Srwatson#endif 2073139333Srwatson 207474912Sjhbint 207575273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 207672224Sjhb{ 207775273Sjhb struct lock_list_entry *lle; 207874912Sjhb int i, nheld; 207972224Sjhb 208074912Sjhb nheld = 0; 208174912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 208274912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 2083111881Sjhb witness_list_lock(&lle->ll_children[i]); 208474912Sjhb nheld++; 208574912Sjhb } 208675273Sjhb return (nheld); 208775273Sjhb} 208875273Sjhb 2089118271Sjhb/* 2090118271Sjhb * This is a bit risky at best. We call this function when we have timed 2091118271Sjhb * out acquiring a spin lock, and we assume that the other CPU is stuck 2092118271Sjhb * with this lock held. So, we go groveling around in the other CPU's 2093118271Sjhb * per-cpu data to try to find the lock instance for this spin lock to 2094118271Sjhb * see when it was last acquired. 2095118271Sjhb */ 209665557Sjasonevoid 2097118271Sjhbwitness_display_spinlock(struct lock_object *lock, struct thread *owner) 2098118271Sjhb{ 2099118271Sjhb struct lock_instance *instance; 2100118271Sjhb struct pcpu *pc; 2101118271Sjhb 2102118271Sjhb if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 2103118271Sjhb return; 2104118271Sjhb pc = pcpu_find(owner->td_oncpu); 2105118271Sjhb instance = find_instance(pc->pc_spinlocks, lock); 2106118271Sjhb if (instance != NULL) 2107118271Sjhb witness_list_lock(instance); 2108118271Sjhb} 2109118271Sjhb 2110118271Sjhbvoid 211174912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 211265557Sjasone{ 2113153854Sjhb struct lock_list_entry *lock_list; 211476272Sjhb struct lock_instance *instance; 2115154077Sjhb struct lock_class *class; 211671320Sjasone 2117181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2118182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 211971352Sjasone return; 2120154077Sjhb class = LOCK_CLASS(lock); 2121154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2122153854Sjhb lock_list = curthread->td_sleeplocks; 2123153854Sjhb else { 2124153854Sjhb if (witness_skipspin) 2125153854Sjhb return; 2126153854Sjhb lock_list = PCPU_GET(spinlocks); 2127153854Sjhb } 2128153854Sjhb instance = find_instance(lock_list, lock); 212982243Sjhb if (instance == NULL) 213082243Sjhb panic("%s: lock (%s) %s not locked", __func__, 2131154077Sjhb class->lc_name, lock->lo_name); 213276272Sjhb *filep = instance->li_file; 213376272Sjhb *linep = instance->li_line; 213465557Sjasone} 213565557Sjasone 213665557Sjasonevoid 213774912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 213865557Sjasone{ 2139153854Sjhb struct lock_list_entry *lock_list; 214076272Sjhb struct lock_instance *instance; 2141154077Sjhb struct lock_class *class; 214271320Sjasone 2143181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2144182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 214571352Sjasone return; 2146154077Sjhb class = LOCK_CLASS(lock); 2147154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2148153854Sjhb lock_list = curthread->td_sleeplocks; 2149153854Sjhb else { 2150153854Sjhb if (witness_skipspin) 2151153854Sjhb return; 2152153854Sjhb lock_list = PCPU_GET(spinlocks); 2153153854Sjhb } 2154153854Sjhb instance = find_instance(lock_list, lock); 215582243Sjhb if (instance == NULL) 215682243Sjhb panic("%s: lock (%s) %s not locked", __func__, 2157154077Sjhb class->lc_name, lock->lo_name); 215874912Sjhb lock->lo_witness->w_file = file; 215974912Sjhb lock->lo_witness->w_line = line; 216076272Sjhb instance->li_file = file; 216176272Sjhb instance->li_line = line; 216265557Sjasone} 216365557Sjasone 216478871Sjhbvoid 216578871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 216678871Sjhb{ 216778871Sjhb#ifdef INVARIANT_SUPPORT 216878871Sjhb struct lock_instance *instance; 2169154077Sjhb struct lock_class *class; 217078871Sjhb 2171182446Sattilio if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL) 217278941Sjhb return; 2173154077Sjhb class = LOCK_CLASS(lock); 2174154077Sjhb if ((class->lc_flags & LC_SLEEPLOCK) != 0) 217583366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 2176154077Sjhb else if ((class->lc_flags & LC_SPINLOCK) != 0) 217778871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 217886422Sjhb else { 217978871Sjhb panic("Lock (%s) %s is not sleep or spin!", 2180154077Sjhb class->lc_name, lock->lo_name); 218186422Sjhb } 2182112116Sjhb file = fixup_filename(file); 218378871Sjhb switch (flags) { 218478871Sjhb case LA_UNLOCKED: 218578871Sjhb if (instance != NULL) 218678871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 2187154077Sjhb class->lc_name, lock->lo_name, file, line); 218878871Sjhb break; 218978871Sjhb case LA_LOCKED: 219078871Sjhb case LA_LOCKED | LA_RECURSED: 219178871Sjhb case LA_LOCKED | LA_NOTRECURSED: 219278871Sjhb case LA_SLOCKED: 219378871Sjhb case LA_SLOCKED | LA_RECURSED: 219478871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 219578871Sjhb case LA_XLOCKED: 219678871Sjhb case LA_XLOCKED | LA_RECURSED: 219778871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 219886422Sjhb if (instance == NULL) { 219978871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 2200154077Sjhb class->lc_name, lock->lo_name, file, line); 220186422Sjhb break; 220286422Sjhb } 220378871Sjhb if ((flags & LA_XLOCKED) != 0 && 220478871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 220578871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 2206154077Sjhb class->lc_name, lock->lo_name, file, line); 220778871Sjhb if ((flags & LA_SLOCKED) != 0 && 220878871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 220978871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 2210154077Sjhb class->lc_name, lock->lo_name, file, line); 221178871Sjhb if ((flags & LA_RECURSED) != 0 && 221278871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 221378871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 2214154077Sjhb class->lc_name, lock->lo_name, file, line); 221578871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 221678871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 221778871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 2218154077Sjhb class->lc_name, lock->lo_name, file, line); 221978871Sjhb break; 222078871Sjhb default: 222178871Sjhb panic("Invalid lock assertion at %s:%d.", file, line); 222278871Sjhb 222378871Sjhb } 222478871Sjhb#endif /* INVARIANT_SUPPORT */ 222578871Sjhb} 222678871Sjhb 222774912Sjhb#ifdef DDB 2228112061Sjhbstatic void 2229181695Sattiliowitness_ddb_list(struct thread *td) 2230112061Sjhb{ 223174912Sjhb 2232181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2233131930Smarcel KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 2234112061Sjhb 2235182446Sattilio if (witness_watch < 1) 2236112061Sjhb return; 2237112061Sjhb 2238112061Sjhb witness_list_locks(&td->td_sleeplocks); 2239112061Sjhb 2240112061Sjhb /* 2241112061Sjhb * We only handle spinlocks if td == curthread. This is somewhat broken 2242112061Sjhb * if td is currently executing on some other CPU and holds spin locks 2243112061Sjhb * as we won't display those locks. If we had a MI way of getting 2244112061Sjhb * the per-cpu data for a given cpu then we could use 2245113339Sjulian * td->td_oncpu to get the list of spinlocks for this thread 2246112061Sjhb * and "fix" this. 2247112061Sjhb * 2248170302Sjeff * That still wouldn't really fix this unless we locked the scheduler 2249170302Sjeff * lock or stopped the other CPU to make sure it wasn't changing the 2250170302Sjeff * list out from under us. It is probably best to just not try to 2251170302Sjeff * handle threads on other CPU's for now. 2252112061Sjhb */ 2253112061Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 2254112061Sjhb witness_list_locks(PCPU_PTR(spinlocks)); 2255112061Sjhb} 2256112061Sjhb 225774930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 225874912Sjhb{ 225983366Sjulian struct thread *td; 226074912Sjhb 2261158030Sjhb if (have_addr) 2262158030Sjhb td = db_lookup_thread(addr, TRUE); 2263158030Sjhb else 2264158030Sjhb td = kdb_thread; 2265181695Sattilio witness_ddb_list(td); 226674912Sjhb} 226774912Sjhb 2268183054SsamDB_SHOW_ALL_COMMAND(locks, db_witness_list_all) 2269139333Srwatson{ 2270139333Srwatson struct thread *td; 2271139333Srwatson struct proc *p; 2272139333Srwatson 2273139333Srwatson /* 2274139333Srwatson * It would be nice to list only threads and processes that actually 2275139333Srwatson * held sleep locks, but that information is currently not exported 2276139333Srwatson * by WITNESS. 2277139333Srwatson */ 2278139333Srwatson FOREACH_PROC_IN_SYSTEM(p) { 2279139333Srwatson if (!witness_proc_has_locks(p)) 2280139333Srwatson continue; 2281139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2282139333Srwatson if (!witness_thread_has_locks(td)) 2283139333Srwatson continue; 2284153853Sjhb db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2285182984Sattilio p->p_comm, td, td->td_tid); 2286181695Sattilio witness_ddb_list(td); 2287139333Srwatson } 2288139333Srwatson } 2289139333Srwatson} 2290183054SsamDB_SHOW_ALIAS(alllocks, db_witness_list_all) 2291139333Srwatson 229274912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 229374912Sjhb{ 229474912Sjhb 2295181695Sattilio witness_ddb_display(db_printf); 229674912Sjhb} 229774912Sjhb#endif 2298181695Sattilio 2299181695Sattiliostatic int 2300181695Sattiliosysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS) 2301181695Sattilio{ 2302181695Sattilio struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2; 2303181695Sattilio struct witness *tmp_w1, *tmp_w2, *w1, *w2; 2304181695Sattilio struct sbuf *sb; 2305181695Sattilio u_int w_rmatrix1, w_rmatrix2; 2306181695Sattilio int error, generation, i, j; 2307181695Sattilio 2308181695Sattilio tmp_data1 = NULL; 2309181695Sattilio tmp_data2 = NULL; 2310181695Sattilio tmp_w1 = NULL; 2311181695Sattilio tmp_w2 = NULL; 2312182446Sattilio if (witness_watch < 1) { 2313181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2314181695Sattilio return (error); 2315181695Sattilio } 2316181695Sattilio if (witness_cold) { 2317181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2318181695Sattilio return (error); 2319181695Sattilio } 2320181695Sattilio error = 0; 2321181695Sattilio sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND); 2322181695Sattilio if (sb == NULL) 2323181695Sattilio return (ENOMEM); 2324181695Sattilio 2325181695Sattilio /* Allocate and init temporary storage space. */ 2326181695Sattilio tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2327181695Sattilio tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2328181695Sattilio tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2329181695Sattilio M_WAITOK | M_ZERO); 2330181695Sattilio tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2331181695Sattilio M_WAITOK | M_ZERO); 2332181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2333181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2334181695Sattilio 2335181695Sattiliorestart: 2336181695Sattilio mtx_lock_spin(&w_mtx); 2337181695Sattilio generation = w_generation; 2338181695Sattilio mtx_unlock_spin(&w_mtx); 2339181695Sattilio sbuf_printf(sb, "Number of known direct relationships is %d\n", 2340181695Sattilio w_lohash.wloh_count); 2341181695Sattilio for (i = 1; i < w_max_used_index; i++) { 2342181695Sattilio mtx_lock_spin(&w_mtx); 2343181695Sattilio if (generation != w_generation) { 2344181695Sattilio mtx_unlock_spin(&w_mtx); 2345181695Sattilio 2346181695Sattilio /* The graph has changed, try again. */ 2347181695Sattilio req->oldidx = 0; 2348181695Sattilio sbuf_clear(sb); 2349181695Sattilio goto restart; 2350181695Sattilio } 2351181695Sattilio 2352181695Sattilio w1 = &w_data[i]; 2353181695Sattilio if (w1->w_reversed == 0) { 2354181695Sattilio mtx_unlock_spin(&w_mtx); 2355181695Sattilio continue; 2356181695Sattilio } 2357181695Sattilio 2358181695Sattilio /* Copy w1 locally so we can release the spin lock. */ 2359181695Sattilio *tmp_w1 = *w1; 2360181695Sattilio mtx_unlock_spin(&w_mtx); 2361181695Sattilio 2362181695Sattilio if (tmp_w1->w_reversed == 0) 2363181695Sattilio continue; 2364181695Sattilio for (j = 1; j < w_max_used_index; j++) { 2365181695Sattilio if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j) 2366181695Sattilio continue; 2367181695Sattilio 2368181695Sattilio mtx_lock_spin(&w_mtx); 2369181695Sattilio if (generation != w_generation) { 2370181695Sattilio mtx_unlock_spin(&w_mtx); 2371181695Sattilio 2372181695Sattilio /* The graph has changed, try again. */ 2373181695Sattilio req->oldidx = 0; 2374181695Sattilio sbuf_clear(sb); 2375181695Sattilio goto restart; 2376181695Sattilio } 2377181695Sattilio 2378181695Sattilio w2 = &w_data[j]; 2379181695Sattilio data1 = witness_lock_order_get(w1, w2); 2380181695Sattilio data2 = witness_lock_order_get(w2, w1); 2381181695Sattilio 2382181695Sattilio /* 2383181695Sattilio * Copy information locally so we can release the 2384181695Sattilio * spin lock. 2385181695Sattilio */ 2386181695Sattilio *tmp_w2 = *w2; 2387181695Sattilio w_rmatrix1 = (unsigned int)w_rmatrix[i][j]; 2388181695Sattilio w_rmatrix2 = (unsigned int)w_rmatrix[j][i]; 2389181695Sattilio 2390181695Sattilio if (data1) { 2391181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2392181695Sattilio stack_copy(&data1->wlod_stack, 2393181695Sattilio &tmp_data1->wlod_stack); 2394181695Sattilio } 2395181695Sattilio if (data2 && data2 != data1) { 2396181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2397181695Sattilio stack_copy(&data2->wlod_stack, 2398181695Sattilio &tmp_data2->wlod_stack); 2399181695Sattilio } 2400181695Sattilio mtx_unlock_spin(&w_mtx); 2401181695Sattilio 2402181695Sattilio sbuf_printf(sb, 2403181695Sattilio "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n", 2404181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2405181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2406181695Sattilio#if 0 2407181695Sattilio sbuf_printf(sb, 2408181695Sattilio "w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n", 2409181695Sattilio tmp_w1->name, tmp_w2->w_name, w_rmatrix1, 2410181695Sattilio tmp_w2->name, tmp_w1->w_name, w_rmatrix2); 2411181695Sattilio#endif 2412181695Sattilio if (data1) { 2413181695Sattilio sbuf_printf(sb, 2414181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2415181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2416181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2417181695Sattilio stack_sbuf_print(sb, &tmp_data1->wlod_stack); 2418181695Sattilio sbuf_printf(sb, "\n"); 2419181695Sattilio } 2420181695Sattilio if (data2 && data2 != data1) { 2421181695Sattilio sbuf_printf(sb, 2422181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2423181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name, 2424181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name); 2425181695Sattilio stack_sbuf_print(sb, &tmp_data2->wlod_stack); 2426181695Sattilio sbuf_printf(sb, "\n"); 2427181695Sattilio } 2428181695Sattilio } 2429181695Sattilio } 2430181695Sattilio mtx_lock_spin(&w_mtx); 2431181695Sattilio if (generation != w_generation) { 2432181695Sattilio mtx_unlock_spin(&w_mtx); 2433181695Sattilio 2434181695Sattilio /* 2435181695Sattilio * The graph changed while we were printing stack data, 2436181695Sattilio * try again. 2437181695Sattilio */ 2438181695Sattilio req->oldidx = 0; 2439181695Sattilio sbuf_clear(sb); 2440181695Sattilio goto restart; 2441181695Sattilio } 2442181695Sattilio mtx_unlock_spin(&w_mtx); 2443181695Sattilio 2444181695Sattilio /* Free temporary storage space. */ 2445181695Sattilio free(tmp_data1, M_TEMP); 2446181695Sattilio free(tmp_data2, M_TEMP); 2447181695Sattilio free(tmp_w1, M_TEMP); 2448181695Sattilio free(tmp_w2, M_TEMP); 2449181695Sattilio 2450181695Sattilio sbuf_finish(sb); 2451181695Sattilio error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2452181695Sattilio sbuf_delete(sb); 2453181695Sattilio 2454181695Sattilio return (error); 2455181695Sattilio} 2456181695Sattilio 2457181695Sattiliostatic int 2458181695Sattiliosysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS) 2459181695Sattilio{ 2460181695Sattilio struct witness *w; 2461181695Sattilio struct sbuf *sb; 2462181695Sattilio int error; 2463181695Sattilio 2464182446Sattilio if (witness_watch < 1) { 2465181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2466181695Sattilio return (error); 2467181695Sattilio } 2468181695Sattilio if (witness_cold) { 2469181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2470181695Sattilio return (error); 2471181695Sattilio } 2472181695Sattilio error = 0; 2473181695Sattilio sb = sbuf_new(NULL, NULL, FULLGRAPH_SBUF_SIZE, SBUF_FIXEDLEN); 2474181695Sattilio if (sb == NULL) 2475181695Sattilio return (ENOMEM); 2476181695Sattilio sbuf_printf(sb, "\n"); 2477181695Sattilio 2478181695Sattilio mtx_lock_spin(&w_mtx); 2479181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2480181695Sattilio w->w_displayed = 0; 2481181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2482181695Sattilio witness_add_fullgraph(sb, w); 2483181695Sattilio mtx_unlock_spin(&w_mtx); 2484181695Sattilio 2485181695Sattilio /* 2486181695Sattilio * While using SBUF_FIXEDLEN, check if the sbuf overflowed. 2487181695Sattilio */ 2488181695Sattilio if (sbuf_overflowed(sb)) { 2489181695Sattilio sbuf_delete(sb); 2490181695Sattilio panic("%s: sbuf overflowed, bump FULLGRAPH_SBUF_SIZE value\n", 2491181695Sattilio __func__); 2492181695Sattilio } 2493181695Sattilio 2494181695Sattilio /* 2495181695Sattilio * Close the sbuf and return to userland. 2496181695Sattilio */ 2497181695Sattilio sbuf_finish(sb); 2498181695Sattilio error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2499181695Sattilio sbuf_delete(sb); 2500181695Sattilio 2501181695Sattilio return (error); 2502181695Sattilio} 2503181695Sattilio 2504181695Sattiliostatic int 2505181695Sattiliosysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 2506181695Sattilio{ 2507181695Sattilio int error, value; 2508181695Sattilio 2509182473Sattilio value = witness_watch; 2510181695Sattilio error = sysctl_handle_int(oidp, &value, 0, req); 2511181695Sattilio if (error != 0 || req->newptr == NULL) 2512181695Sattilio return (error); 2513182446Sattilio if (value > 1 || value < -1 || 2514182446Sattilio (witness_watch == -1 && value != witness_watch)) 2515181695Sattilio return (EINVAL); 2516182446Sattilio witness_watch = value; 2517181695Sattilio return (0); 2518181695Sattilio} 2519181695Sattilio 2520181695Sattiliostatic void 2521181695Sattiliowitness_add_fullgraph(struct sbuf *sb, struct witness *w) 2522181695Sattilio{ 2523181695Sattilio int i; 2524181695Sattilio 2525181695Sattilio if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0)) 2526181695Sattilio return; 2527181695Sattilio w->w_displayed = 1; 2528181695Sattilio 2529181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 2530181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 2531181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) { 2532181695Sattilio sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name, 2533181695Sattilio w_data[i].w_name); 2534181695Sattilio witness_add_fullgraph(sb, &w_data[i]); 2535181695Sattilio } 2536181695Sattilio } 2537181695Sattilio} 2538181695Sattilio 2539181695Sattilio/* 2540181695Sattilio * A simple hash function. Takes a key pointer and a key size. If size == 0, 2541181695Sattilio * interprets the key as a string and reads until the null 2542181695Sattilio * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit 2543181695Sattilio * hash value computed from the key. 2544181695Sattilio */ 2545181695Sattiliostatic uint32_t 2546181695Sattiliowitness_hash_djb2(const uint8_t *key, uint32_t size) 2547181695Sattilio{ 2548181695Sattilio unsigned int hash = 5381; 2549181695Sattilio int i; 2550181695Sattilio 2551181695Sattilio /* hash = hash * 33 + key[i] */ 2552181695Sattilio if (size) 2553181695Sattilio for (i = 0; i < size; i++) 2554181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2555181695Sattilio else 2556181695Sattilio for (i = 0; key[i] != 0; i++) 2557181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2558181695Sattilio 2559181695Sattilio return (hash); 2560181695Sattilio} 2561181695Sattilio 2562181695Sattilio 2563181695Sattilio/* 2564181695Sattilio * Initializes the two witness hash tables. Called exactly once from 2565181695Sattilio * witness_initialize(). 2566181695Sattilio */ 2567181695Sattiliostatic void 2568181695Sattiliowitness_init_hash_tables(void) 2569181695Sattilio{ 2570181695Sattilio int i; 2571181695Sattilio 2572181695Sattilio MPASS(witness_cold); 2573181695Sattilio 2574181695Sattilio /* Initialize the hash tables. */ 2575181695Sattilio for (i = 0; i < WITNESS_HASH_SIZE; i++) 2576181695Sattilio w_hash.wh_array[i] = NULL; 2577181695Sattilio 2578181695Sattilio w_hash.wh_size = WITNESS_HASH_SIZE; 2579181695Sattilio w_hash.wh_count = 0; 2580181695Sattilio 2581181695Sattilio /* Initialize the lock order data hash. */ 2582181695Sattilio w_lofree = NULL; 2583181695Sattilio for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) { 2584181695Sattilio memset(&w_lodata[i], 0, sizeof(w_lodata[i])); 2585181695Sattilio w_lodata[i].wlod_next = w_lofree; 2586181695Sattilio w_lofree = &w_lodata[i]; 2587181695Sattilio } 2588181695Sattilio w_lohash.wloh_size = WITNESS_LO_HASH_SIZE; 2589181695Sattilio w_lohash.wloh_count = 0; 2590181695Sattilio for (i = 0; i < WITNESS_LO_HASH_SIZE; i++) 2591181695Sattilio w_lohash.wloh_array[i] = NULL; 2592181695Sattilio} 2593181695Sattilio 2594181695Sattiliostatic struct witness * 2595181695Sattiliowitness_hash_get(const char *key) 2596181695Sattilio{ 2597181695Sattilio struct witness *w; 2598181695Sattilio uint32_t hash; 2599181695Sattilio 2600181695Sattilio MPASS(key != NULL); 2601181695Sattilio if (witness_cold == 0) 2602181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2603181695Sattilio hash = witness_hash_djb2(key, 0) % w_hash.wh_size; 2604181695Sattilio w = w_hash.wh_array[hash]; 2605181695Sattilio while (w != NULL) { 2606181695Sattilio if (strcmp(w->w_name, key) == 0) 2607181695Sattilio goto out; 2608181695Sattilio w = w->w_hash_next; 2609181695Sattilio } 2610181695Sattilio 2611181695Sattilioout: 2612181695Sattilio return (w); 2613181695Sattilio} 2614181695Sattilio 2615181695Sattiliostatic void 2616181695Sattiliowitness_hash_put(struct witness *w) 2617181695Sattilio{ 2618181695Sattilio uint32_t hash; 2619181695Sattilio 2620181695Sattilio MPASS(w != NULL); 2621181695Sattilio MPASS(w->w_name != NULL); 2622181695Sattilio if (witness_cold == 0) 2623181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2624181695Sattilio KASSERT(witness_hash_get(w->w_name) == NULL, 2625181695Sattilio ("%s: trying to add a hash entry that already exists!", __func__)); 2626181695Sattilio KASSERT(w->w_hash_next == NULL, 2627181695Sattilio ("%s: w->w_hash_next != NULL", __func__)); 2628181695Sattilio 2629181695Sattilio hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size; 2630181695Sattilio w->w_hash_next = w_hash.wh_array[hash]; 2631181695Sattilio w_hash.wh_array[hash] = w; 2632181695Sattilio w_hash.wh_count++; 2633181695Sattilio} 2634181695Sattilio 2635181695Sattilio 2636181695Sattiliostatic struct witness_lock_order_data * 2637181695Sattiliowitness_lock_order_get(struct witness *parent, struct witness *child) 2638181695Sattilio{ 2639181695Sattilio struct witness_lock_order_data *data = NULL; 2640181695Sattilio struct witness_lock_order_key key; 2641181695Sattilio unsigned int hash; 2642181695Sattilio 2643181695Sattilio MPASS(parent != NULL && child != NULL); 2644181695Sattilio key.from = parent->w_index; 2645181695Sattilio key.to = child->w_index; 2646181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2647181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2648181695Sattilio if ((w_rmatrix[parent->w_index][child->w_index] 2649181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) == 0) 2650181695Sattilio goto out; 2651181695Sattilio 2652181695Sattilio hash = witness_hash_djb2((const char*)&key, 2653181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2654181695Sattilio data = w_lohash.wloh_array[hash]; 2655181695Sattilio while (data != NULL) { 2656181695Sattilio if (witness_lock_order_key_equal(&data->wlod_key, &key)) 2657181695Sattilio break; 2658181695Sattilio data = data->wlod_next; 2659181695Sattilio } 2660181695Sattilio 2661181695Sattilioout: 2662181695Sattilio return (data); 2663181695Sattilio} 2664181695Sattilio 2665181695Sattilio/* 2666181695Sattilio * Verify that parent and child have a known relationship, are not the same, 2667181695Sattilio * and child is actually a child of parent. This is done without w_mtx 2668181695Sattilio * to avoid contention in the common case. 2669181695Sattilio */ 2670181695Sattiliostatic int 2671181695Sattiliowitness_lock_order_check(struct witness *parent, struct witness *child) 2672181695Sattilio{ 2673181695Sattilio 2674181695Sattilio if (parent != child && 2675181695Sattilio w_rmatrix[parent->w_index][child->w_index] 2676181695Sattilio & WITNESS_LOCK_ORDER_KNOWN && 2677181695Sattilio isitmychild(parent, child)) 2678181695Sattilio return (1); 2679181695Sattilio 2680181695Sattilio return (0); 2681181695Sattilio} 2682181695Sattilio 2683181695Sattiliostatic int 2684181695Sattiliowitness_lock_order_add(struct witness *parent, struct witness *child) 2685181695Sattilio{ 2686181695Sattilio struct witness_lock_order_data *data = NULL; 2687181695Sattilio struct witness_lock_order_key key; 2688181695Sattilio unsigned int hash; 2689181695Sattilio 2690181695Sattilio MPASS(parent != NULL && child != NULL); 2691181695Sattilio key.from = parent->w_index; 2692181695Sattilio key.to = child->w_index; 2693181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2694181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2695181695Sattilio if (w_rmatrix[parent->w_index][child->w_index] 2696181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) 2697181695Sattilio return (1); 2698181695Sattilio 2699181695Sattilio hash = witness_hash_djb2((const char*)&key, 2700181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2701181695Sattilio w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN; 2702181695Sattilio data = w_lofree; 2703181695Sattilio if (data == NULL) 2704181695Sattilio return (0); 2705181695Sattilio w_lofree = data->wlod_next; 2706181695Sattilio data->wlod_next = w_lohash.wloh_array[hash]; 2707181695Sattilio data->wlod_key = key; 2708181695Sattilio w_lohash.wloh_array[hash] = data; 2709181695Sattilio w_lohash.wloh_count++; 2710181695Sattilio stack_zero(&data->wlod_stack); 2711181695Sattilio stack_save(&data->wlod_stack); 2712181695Sattilio return (1); 2713181695Sattilio} 2714181695Sattilio 2715181695Sattilio/* Call this whenver the structure of the witness graph changes. */ 2716181695Sattiliostatic void 2717181695Sattiliowitness_increment_graph_generation(void) 2718181695Sattilio{ 2719181695Sattilio 2720181695Sattilio if (witness_cold == 0) 2721181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2722181695Sattilio w_generation++; 2723181695Sattilio} 2724181695Sattilio 2725181695Sattilio#ifdef KDB 2726181695Sattiliostatic void 2727181695Sattilio_witness_debugger(int cond, const char *msg) 2728181695Sattilio{ 2729181695Sattilio 2730181695Sattilio if (witness_trace && cond) 2731181695Sattilio kdb_backtrace(); 2732181695Sattilio if (witness_kdb && cond) 2733181695Sattilio kdb_enter(KDB_WHY_WITNESS, msg); 2734181695Sattilio} 2735181695Sattilio#endif 2736