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$"); 89116182Sobrien 9068790Sjhb#include "opt_ddb.h" 91168856Sjkoshy#include "opt_hwpmc_hooks.h" 92181695Sattilio#include "opt_stack.h" 9367676Sjhb#include "opt_witness.h" 9467676Sjhb 9565557Sjasone#include <sys/param.h> 9667352Sjhb#include <sys/bus.h> 97131930Smarcel#include <sys/kdb.h> 9867352Sjhb#include <sys/kernel.h> 9974912Sjhb#include <sys/ktr.h> 10074912Sjhb#include <sys/lock.h> 10167352Sjhb#include <sys/malloc.h> 10274912Sjhb#include <sys/mutex.h> 103164033Srwatson#include <sys/priv.h> 10465557Sjasone#include <sys/proc.h> 105178841Sattilio#include <sys/sbuf.h> 106183955Sattilio#include <sys/sched.h> 107181695Sattilio#include <sys/stack.h> 10867676Sjhb#include <sys/sysctl.h> 10965557Sjasone#include <sys/systm.h> 11065557Sjasone 111181695Sattilio#ifdef DDB 11268790Sjhb#include <ddb/ddb.h> 113181695Sattilio#endif 11468790Sjhb 115111881Sjhb#include <machine/stdarg.h> 116111881Sjhb 117181695Sattilio#if !defined(DDB) && !defined(STACK) 118181695Sattilio#error "DDB or STACK options are required for WITNESS" 119181695Sattilio#endif 120181695Sattilio 121154818Sjhb/* Note that these traces do not work with KTR_ALQ. */ 122154790Sjhb#if 0 123154790Sjhb#define KTR_WITNESS KTR_SUBSYS 124154790Sjhb#else 125154790Sjhb#define KTR_WITNESS 0 126154790Sjhb#endif 127154790Sjhb 128178165Sattilio#define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */ 129178165Sattilio#define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */ 130187511Sthompsa#define LI_NORELEASE 0x00020000 /* Lock not allowed to be released. */ 131178165Sattilio 132105508Sphk/* Define this to check for blessed mutexes */ 133105508Sphk#undef BLESSING 134105508Sphk 135181695Sattilio#define WITNESS_COUNT 1024 136181695Sattilio#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 137181695Sattilio#define WITNESS_HASH_SIZE 251 /* Prime, gives load factor < 2 */ 138210611Srpaulo#define WITNESS_PENDLIST 768 139181695Sattilio 140181695Sattilio/* Allocate 256 KB of stack data space */ 141181695Sattilio#define WITNESS_LO_DATA_COUNT 2048 142181695Sattilio 143181695Sattilio/* Prime, gives load factor of ~2 at full load */ 144181695Sattilio#define WITNESS_LO_HASH_SIZE 1021 145181695Sattilio 14665557Sjasone/* 147181695Sattilio * XXX: This is somewhat bogus, as we assume here that at most 2048 threads 148181695Sattilio * will hold LOCK_NCHILDREN locks. We handle failure ok, and we should 14974912Sjhb * probably be safe for the most part, but it's still a SWAG. 15067352Sjhb */ 151181695Sattilio#define LOCK_NCHILDREN 5 152181695Sattilio#define LOCK_CHILDCOUNT 2048 15371352Sjasone 154181695Sattilio#define MAX_W_NAME 64 15571352Sjasone 156181695Sattilio#define BADSTACK_SBUF_SIZE (256 * WITNESS_COUNT) 157212750Smdf#define FULLGRAPH_SBUF_SIZE 512 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 187249132Smavstatic MALLOC_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; 237188056Simp unsigned w_displayed:1; 238188056Simp unsigned 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_equal(const struct witness_lock_order_key *a, 309181695Sattilio const struct witness_lock_order_key *b) 310181695Sattilio{ 311181695Sattilio 312181695Sattilio return (a->from == b->from && a->to == b->to); 313181695Sattilio} 314181695Sattilio 315181695Sattiliostatic int _isitmyx(struct witness *w1, struct witness *w2, int rmask, 316181695Sattilio const char *fname); 317181695Sattilio#ifdef KDB 318181695Sattiliostatic void _witness_debugger(int cond, const char *msg); 319181695Sattilio#endif 320181695Sattiliostatic void adopt(struct witness *parent, struct witness *child); 321112117Sjhb#ifdef BLESSING 322112117Sjhbstatic int blessed(struct witness *, struct witness *); 323112117Sjhb#endif 324179025Sattiliostatic void depart(struct witness *w); 325181695Sattiliostatic struct witness *enroll(const char *description, 326181695Sattilio struct lock_class *lock_class); 327181695Sattiliostatic struct lock_instance *find_instance(struct lock_list_entry *list, 328181695Sattilio struct lock_object *lock); 329112117Sjhbstatic int isitmychild(struct witness *parent, struct witness *child); 330112117Sjhbstatic int isitmydescendant(struct witness *parent, struct witness *child); 331181695Sattiliostatic void itismychild(struct witness *parent, struct witness *child); 332181695Sattiliostatic int sysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS); 333112562Sjhbstatic int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 334181695Sattiliostatic int sysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS); 335181695Sattiliostatic void witness_add_fullgraph(struct sbuf *sb, struct witness *parent); 336181695Sattilio#ifdef DDB 337181695Sattiliostatic void witness_ddb_compute_levels(void); 338207922Sattiliostatic void witness_ddb_display(int(*)(const char *fmt, ...)); 339207922Sattiliostatic void witness_ddb_display_descendants(int(*)(const char *fmt, ...), 340181695Sattilio struct witness *, int indent); 341207922Sattiliostatic void witness_ddb_display_list(int(*prnt)(const char *fmt, ...), 342181695Sattilio struct witness_list *list); 343181695Sattiliostatic void witness_ddb_level_descendants(struct witness *parent, int l); 344181695Sattiliostatic void witness_ddb_list(struct thread *td); 345181695Sattilio#endif 34674912Sjhbstatic void witness_free(struct witness *m); 347181695Sattiliostatic struct witness *witness_get(void); 348181695Sattiliostatic uint32_t witness_hash_djb2(const uint8_t *key, uint32_t size); 349181695Sattiliostatic struct witness *witness_hash_get(const char *key); 350181695Sattiliostatic void witness_hash_put(struct witness *w); 351181695Sattiliostatic void witness_init_hash_tables(void); 352181695Sattiliostatic void witness_increment_graph_generation(void); 35374912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 354181695Sattiliostatic struct lock_list_entry *witness_lock_list_get(void); 355181695Sattiliostatic int witness_lock_order_add(struct witness *parent, 356181695Sattilio struct witness *child); 357181695Sattiliostatic int witness_lock_order_check(struct witness *parent, 358181695Sattilio struct witness *child); 359181695Sattiliostatic struct witness_lock_order_data *witness_lock_order_get( 360181695Sattilio struct witness *parent, 361181695Sattilio struct witness *child); 362207929Sattiliostatic void witness_list_lock(struct lock_instance *instance, 363207929Sattilio int (*prnt)(const char *fmt, ...)); 364187511Sthompsastatic void witness_setflag(struct lock_object *lock, int flag, int set); 365181695Sattilio 366181695Sattilio#ifdef KDB 367181695Sattilio#define witness_debugger(c) _witness_debugger(c, __func__) 368181695Sattilio#else 369181695Sattilio#define witness_debugger(c) 370100011Smp#endif 37172200Sbmilekic 372248085Smariusstatic SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, NULL, 373248085Smarius "Witness Locking"); 37472200Sbmilekic 375112562Sjhb/* 376183329Sjhb * If set to 0, lock order checking is disabled. If set to -1, 377183329Sjhb * witness is completely disabled. Otherwise witness performs full 378183329Sjhb * lock order checking for all locks. At runtime, lock order checking 379183329Sjhb * may be toggled. However, witness cannot be reenabled once it is 380183329Sjhb * completely disabled. 381112562Sjhb */ 38277843Speterstatic int witness_watch = 1; 383134873SjmgTUNABLE_INT("debug.witness.watch", &witness_watch); 384134873SjmgSYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 385112562Sjhb sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 38671352Sjasone 387131930Smarcel#ifdef KDB 38872200Sbmilekic/* 389181695Sattilio * When KDB is enabled and witness_kdb is 1, it will cause the system 390131930Smarcel * to drop into kdebug() when: 391151623Sjhb * - a lock hierarchy violation occurs 39265557Sjasone * - locks are held when going to sleep. 39365557Sjasone */ 394131930Smarcel#ifdef WITNESS_KDB 395131930Smarcelint witness_kdb = 1; 39667676Sjhb#else 397131930Smarcelint witness_kdb = 0; 39865557Sjasone#endif 399134873SjmgTUNABLE_INT("debug.witness.kdb", &witness_kdb); 400134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 401110779Speter 402110779Speter/* 403181695Sattilio * When KDB is enabled and witness_trace is 1, it will cause the system 404110779Speter * to print a stack trace: 405151623Sjhb * - a lock hierarchy violation occurs 406110779Speter * - locks are held when going to sleep. 407110779Speter */ 408110779Speterint witness_trace = 1; 409134873SjmgTUNABLE_INT("debug.witness.trace", &witness_trace); 410134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 411131930Smarcel#endif /* KDB */ 41265557Sjasone 41367676Sjhb#ifdef WITNESS_SKIPSPIN 41477843Speterint witness_skipspin = 1; 41567676Sjhb#else 41677843Speterint witness_skipspin = 0; 41765557Sjasone#endif 418134873SjmgTUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 419181695SattilioSYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, &witness_skipspin, 420181695Sattilio 0, ""); 42165557Sjasone 422181695Sattilio/* 423181695Sattilio * Call this to print out the relations between locks. 424181695Sattilio */ 425181695SattilioSYSCTL_PROC(_debug_witness, OID_AUTO, fullgraph, CTLTYPE_STRING | CTLFLAG_RD, 426181695Sattilio NULL, 0, sysctl_debug_witness_fullgraph, "A", "Show locks relation graphs"); 427181695Sattilio 428181695Sattilio/* 429181695Sattilio * Call this to print out the witness faulty stacks. 430181695Sattilio */ 431181695SattilioSYSCTL_PROC(_debug_witness, OID_AUTO, badstacks, CTLTYPE_STRING | CTLFLAG_RD, 432181695Sattilio NULL, 0, sysctl_debug_witness_badstacks, "A", "Show bad witness stacks"); 433181695Sattilio 43474912Sjhbstatic struct mtx w_mtx; 435181695Sattilio 436181695Sattilio/* w_list */ 43774912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 43874912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 439181695Sattilio 440181695Sattilio/* w_typelist */ 44174912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 44274912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 443181695Sattilio 444181695Sattilio/* lock list */ 44574912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 446179025Sattiliostatic struct witness_pendhelp pending_locks[WITNESS_PENDLIST]; 447179025Sattiliostatic u_int pending_cnt; 44865557Sjasone 449181695Sattiliostatic int w_free_cnt, w_spin_cnt, w_sleep_cnt; 450149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, ""); 451149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, ""); 452149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0, 453149441Struckman ""); 454149441Struckman 455181695Sattiliostatic struct witness *w_data; 456181695Sattiliostatic uint8_t w_rmatrix[WITNESS_COUNT+1][WITNESS_COUNT+1]; 45774912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 458181695Sattiliostatic struct witness_hash w_hash; /* The witness hash table. */ 45965557Sjasone 460181695Sattilio/* The lock order data hash */ 461181695Sattiliostatic struct witness_lock_order_data w_lodata[WITNESS_LO_DATA_COUNT]; 462181695Sattiliostatic struct witness_lock_order_data *w_lofree = NULL; 463181695Sattiliostatic struct witness_lock_order_hash w_lohash; 464181695Sattiliostatic int w_max_used_index = 0; 465181695Sattiliostatic unsigned int w_generation = 0; 466196891Santoinestatic const char w_notrunning[] = "Witness not running\n"; 467196891Santoinestatic const char w_stillcold[] = "Witness is still cold\n"; 468181695Sattilio 469181695Sattilio 47074912Sjhbstatic struct witness_order_list_entry order_lists[] = { 471149738Sjhb /* 472149738Sjhb * sx locks 473149738Sjhb */ 47474912Sjhb { "proctree", &lock_class_sx }, 47574912Sjhb { "allproc", &lock_class_sx }, 476168402Spjd { "allprison", &lock_class_sx }, 477149738Sjhb { NULL, NULL }, 478149738Sjhb /* 479149738Sjhb * Various mutexes 480149738Sjhb */ 481111951Sjhb { "Giant", &lock_class_mtx_sleep }, 482108184Skris { "pipe mutex", &lock_class_mtx_sleep }, 48396122Salfred { "sigio lock", &lock_class_mtx_sleep }, 48491140Stanimura { "process group", &lock_class_mtx_sleep }, 48574912Sjhb { "process lock", &lock_class_mtx_sleep }, 48691140Stanimura { "session", &lock_class_mtx_sleep }, 487177299Spjd { "uidinfo hash", &lock_class_rw }, 488168856Sjkoshy#ifdef HWPMC_HOOKS 489168856Sjkoshy { "pmc-sleep", &lock_class_mtx_sleep }, 490168856Sjkoshy#endif 491209403Smav { "time lock", &lock_class_mtx_sleep }, 49274912Sjhb { NULL, NULL }, 49375464Sjhb /* 494130022Srwatson * Sockets 495130022Srwatson */ 496130022Srwatson { "accept", &lock_class_mtx_sleep }, 497130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 498130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 499130022Srwatson { "sellck", &lock_class_mtx_sleep }, 500130022Srwatson { NULL, NULL }, 501130022Srwatson /* 502130022Srwatson * Routing 503130022Srwatson */ 504130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 505185747Skmacy { "radix node head", &lock_class_rw }, 506130022Srwatson { "rtentry", &lock_class_mtx_sleep }, 507130022Srwatson { "ifaddr", &lock_class_mtx_sleep }, 508130022Srwatson { NULL, NULL }, 509130022Srwatson /* 510191672Sbms * IPv4 multicast: 511191672Sbms * protocol locks before interface locks, after UDP locks. 512148682Srwatson */ 513178285Srwatson { "udpinp", &lock_class_rw }, 514148682Srwatson { "in_multi_mtx", &lock_class_mtx_sleep }, 515148682Srwatson { "igmp_mtx", &lock_class_mtx_sleep }, 516148682Srwatson { "if_addr_mtx", &lock_class_mtx_sleep }, 517148682Srwatson { NULL, NULL }, 518148682Srwatson /* 519191672Sbms * IPv6 multicast: 520191672Sbms * protocol locks before interface locks, after UDP locks. 521191672Sbms */ 522191672Sbms { "udpinp", &lock_class_rw }, 523191672Sbms { "in6_multi_mtx", &lock_class_mtx_sleep }, 524191672Sbms { "mld_mtx", &lock_class_mtx_sleep }, 525191672Sbms { "if_addr_mtx", &lock_class_mtx_sleep }, 526191672Sbms { NULL, NULL }, 527191672Sbms /* 528130022Srwatson * UNIX Domain Sockets 529130396Srwatson */ 530189544Srwatson { "unp_global_rwlock", &lock_class_rw }, 531189544Srwatson { "unp_list_lock", &lock_class_mtx_sleep }, 532130396Srwatson { "unp", &lock_class_mtx_sleep }, 533130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 534130031Sjhb { NULL, NULL }, 535130022Srwatson /* 536130022Srwatson * UDP/IP 537130022Srwatson */ 538178285Srwatson { "udp", &lock_class_rw }, 539178285Srwatson { "udpinp", &lock_class_rw }, 540130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 541130022Srwatson { NULL, NULL }, 542130022Srwatson /* 543130022Srwatson * TCP/IP 544130022Srwatson */ 545178285Srwatson { "tcp", &lock_class_rw }, 546178285Srwatson { "tcpinp", &lock_class_rw }, 547130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 548130022Srwatson { NULL, NULL }, 549130022Srwatson /* 550132639Srwatson * netatalk 551132639Srwatson */ 552132639Srwatson { "ddp_list_mtx", &lock_class_mtx_sleep }, 553132639Srwatson { "ddp_mtx", &lock_class_mtx_sleep }, 554132639Srwatson { NULL, NULL }, 555132639Srwatson /* 556134971Srwatson * BPF 557134971Srwatson */ 558134971Srwatson { "bpf global lock", &lock_class_mtx_sleep }, 559247629Smelifaro { "bpf interface lock", &lock_class_rw }, 560134971Srwatson { "bpf cdev lock", &lock_class_mtx_sleep }, 561144832Spjd { NULL, NULL }, 562143335Srwatson /* 563143335Srwatson * NFS server 564143335Srwatson */ 565143335Srwatson { "nfsd_mtx", &lock_class_mtx_sleep }, 566143335Srwatson { "so_snd", &lock_class_mtx_sleep }, 567134971Srwatson { NULL, NULL }, 568170530Ssam 569134971Srwatson /* 570170530Ssam * IEEE 802.11 571170530Ssam */ 572170530Ssam { "802.11 com lock", &lock_class_mtx_sleep}, 573170530Ssam { NULL, NULL }, 574170530Ssam /* 575170530Ssam * Network drivers 576170530Ssam */ 577170530Ssam { "network driver", &lock_class_mtx_sleep}, 578170530Ssam { NULL, NULL }, 579170530Ssam 580170530Ssam /* 581168217Swkoszek * Netgraph 582168217Swkoszek */ 583168217Swkoszek { "ng_node", &lock_class_mtx_sleep }, 584168217Swkoszek { "ng_worklist", &lock_class_mtx_sleep }, 585168217Swkoszek { NULL, NULL }, 586168217Swkoszek /* 587144836Spjd * CDEV 588144836Spjd */ 589240151Skib { "vm map (system)", &lock_class_mtx_sleep }, 590240151Skib { "vm page queue", &lock_class_mtx_sleep }, 591145425Sjeff { "vnode interlock", &lock_class_mtx_sleep }, 592144836Spjd { "cdev", &lock_class_mtx_sleep }, 593144836Spjd { NULL, NULL }, 594144836Spjd /* 595207410Skmacy * VM 596207410Skmacy */ 597240151Skib { "vm map (user)", &lock_class_sx }, 598207410Skmacy { "vm object", &lock_class_mtx_sleep }, 599240151Skib { "vm page", &lock_class_mtx_sleep }, 600240151Skib { "vm page queue", &lock_class_mtx_sleep }, 601240151Skib { "pmap pv global", &lock_class_rw }, 602207410Skmacy { "pmap", &lock_class_mtx_sleep }, 603240151Skib { "pmap pv list", &lock_class_rw }, 604240151Skib { "vm page free queue", &lock_class_mtx_sleep }, 605207410Skmacy { NULL, NULL }, 606207410Skmacy /* 607166421Skib * kqueue/VFS interaction 608166421Skib */ 609166421Skib { "kqueue", &lock_class_mtx_sleep }, 610166421Skib { "struct mount mtx", &lock_class_mtx_sleep }, 611166421Skib { "vnode interlock", &lock_class_mtx_sleep }, 612166421Skib { NULL, NULL }, 613166421Skib /* 614192416Skmacy * ZFS locking 615192416Skmacy */ 616192416Skmacy { "dn->dn_mtx", &lock_class_sx }, 617192416Skmacy { "dr->dt.di.dr_mtx", &lock_class_sx }, 618192416Skmacy { "db->db_mtx", &lock_class_sx }, 619192416Skmacy { NULL, NULL }, 620192416Skmacy /* 62175464Sjhb * spin locks 62275464Sjhb */ 62384331Sjhb#ifdef SMP 62484331Sjhb { "ap boot", &lock_class_mtx_spin }, 62572224Sjhb#endif 626150582Sjhb { "rm.mutex_mtx", &lock_class_mtx_spin }, 62774912Sjhb { "sio", &lock_class_mtx_spin }, 628173877Sattilio { "scrlock", &lock_class_mtx_spin }, 62972224Sjhb#ifdef __i386__ 63074912Sjhb { "cy", &lock_class_mtx_spin }, 63172224Sjhb#endif 632170848Smarius#ifdef __sparc64__ 633170848Smarius { "pcib_mtx", &lock_class_mtx_spin }, 634170848Smarius { "rtc_mtx", &lock_class_mtx_spin }, 635170848Smarius#endif 636157584Smarcel { "scc_hwmtx", &lock_class_mtx_spin }, 637124972Sru { "uart_hwmtx", &lock_class_mtx_spin }, 638161638Sssouhlal { "fast_taskqueue", &lock_class_mtx_spin }, 639122001Sjhb { "intr table", &lock_class_mtx_spin }, 640168856Sjkoshy#ifdef HWPMC_HOOKS 641168856Sjkoshy { "pmc-per-proc", &lock_class_mtx_spin }, 642168856Sjkoshy#endif 643170302Sjeff { "process slock", &lock_class_mtx_spin }, 644126324Sjhb { "sleepq chain", &lock_class_mtx_spin }, 645170302Sjeff { "umtx lock", &lock_class_mtx_spin }, 646173877Sattilio { "rm_spinlock", &lock_class_mtx_spin }, 647170302Sjeff { "turnstile chain", &lock_class_mtx_spin }, 648170302Sjeff { "turnstile lock", &lock_class_mtx_spin }, 64974912Sjhb { "sched lock", &lock_class_mtx_spin }, 650122514Sjhb { "td_contested", &lock_class_mtx_spin }, 65174912Sjhb { "callout", &lock_class_mtx_spin }, 652136374Srwatson { "entropy harvest mutex", &lock_class_mtx_spin }, 653162285Sscottl { "syscons video lock", &lock_class_mtx_spin }, 654172256Sattilio#ifdef SMP 655172256Sattilio { "smp rendezvous", &lock_class_mtx_spin }, 656172256Sattilio#endif 657176771Sraj#ifdef __powerpc__ 658176771Sraj { "tlb0", &lock_class_mtx_spin }, 659176771Sraj#endif 66065557Sjasone /* 66165557Sjasone * leaf locks 66265557Sjasone */ 663178149Sattilio { "intrcnt", &lock_class_mtx_spin }, 66488322Sjhb { "icu", &lock_class_mtx_spin }, 665172256Sattilio#ifdef __i386__ 666172256Sattilio { "allpmaps", &lock_class_mtx_spin }, 667172256Sattilio { "descriptor tables", &lock_class_mtx_spin }, 668108187Sjake#endif 66978785Sjhb { "clk", &lock_class_mtx_spin }, 670178149Sattilio { "cpuset", &lock_class_mtx_spin }, 671172256Sattilio { "mprof lock", &lock_class_mtx_spin }, 672170302Sjeff { "zombie lock", &lock_class_mtx_spin }, 673103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 674104951Speter#ifdef __ia64__ 675104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 676104951Speter#endif 677115425Speter#if defined(__i386__) || defined(__amd64__) 678111068Speter { "pcicfg", &lock_class_mtx_spin }, 679143204Swpaul { "NDIS thread lock", &lock_class_mtx_spin }, 680111068Speter#endif 681144966Svkashyap { "tw_osl_io_lock", &lock_class_mtx_spin }, 682144966Svkashyap { "tw_osl_q_lock", &lock_class_mtx_spin }, 683144966Svkashyap { "tw_cl_io_lock", &lock_class_mtx_spin }, 684144966Svkashyap { "tw_cl_intr_lock", &lock_class_mtx_spin }, 685144966Svkashyap { "tw_cl_gen_lock", &lock_class_mtx_spin }, 686168856Sjkoshy#ifdef HWPMC_HOOKS 687168856Sjkoshy { "pmc-leaf", &lock_class_mtx_spin }, 688168856Sjkoshy#endif 689170302Sjeff { "blocked lock", &lock_class_mtx_spin }, 69074912Sjhb { NULL, NULL }, 69174912Sjhb { NULL, NULL } 69265557Sjasone}; 69365557Sjasone 694105508Sphk#ifdef BLESSING 69565557Sjasone/* 69665557Sjasone * Pairs of locks which have been blessed 69765557Sjasone * Don't complain about order problems with blessed locks 69865557Sjasone */ 69965856Sjhbstatic struct witness_blessed blessed_list[] = { 70065557Sjasone}; 70172200Sbmilekicstatic int blessed_count = 70272200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 703105508Sphk#endif 70465557Sjasone 70574912Sjhb/* 70674912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 70774912Sjhb */ 70874912Sjhbstatic int witness_cold = 1; 70974912Sjhb 71074912Sjhb/* 711151629Sjhb * This global is set to 1 once the static lock orders have been enrolled 712151629Sjhb * so that a warning can be issued for any spin locks enrolled later. 713151629Sjhb */ 714151629Sjhbstatic int witness_spin_warn = 0; 715151629Sjhb 716234111Sadrian/* Trim useless garbage from filenames. */ 717234111Sadrianstatic const char * 718234111Sadrianfixup_filename(const char *file) 719234111Sadrian{ 720234111Sadrian 721234111Sadrian if (file == NULL) 722234111Sadrian return (NULL); 723234111Sadrian while (strncmp(file, "../", 3) == 0) 724234111Sadrian file += 3; 725234111Sadrian return (file); 726234111Sadrian} 727234111Sadrian 728151629Sjhb/* 729153133Sjhb * The WITNESS-enabled diagnostic code. Note that the witness code does 730153133Sjhb * assume that the early boot is single-threaded at least until after this 731153133Sjhb * routine is completed. 73274912Sjhb */ 73371352Sjasonestatic void 73474912Sjhbwitness_initialize(void *dummy __unused) 73565557Sjasone{ 73674912Sjhb struct lock_object *lock; 73774912Sjhb struct witness_order_list_entry *order; 73874912Sjhb struct witness *w, *w1; 73974912Sjhb int i; 74065557Sjasone 741184214Sdes w_data = malloc(sizeof (struct witness) * WITNESS_COUNT, M_WITNESS, 742181695Sattilio M_NOWAIT | M_ZERO); 743181695Sattilio 74474912Sjhb /* 74574912Sjhb * We have to release Giant before initializing its witness 74674912Sjhb * structure so that WITNESS doesn't get confused. 74774912Sjhb */ 74874912Sjhb mtx_unlock(&Giant); 74974912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 75074912Sjhb 75187593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 75293811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 753164159Skmacy MTX_NOWITNESS | MTX_NOPROFILE); 754181695Sattilio for (i = WITNESS_COUNT - 1; i >= 0; i--) { 755181695Sattilio w = &w_data[i]; 756181695Sattilio memset(w, 0, sizeof(*w)); 757181695Sattilio w_data[i].w_index = i; /* Witness index never changes. */ 758181695Sattilio witness_free(w); 759181695Sattilio } 760181695Sattilio KASSERT(STAILQ_FIRST(&w_free)->w_index == 0, 761181695Sattilio ("%s: Invalid list of free witness objects", __func__)); 762181695Sattilio 763181695Sattilio /* Witness with index 0 is not used to aid in debugging. */ 764181695Sattilio STAILQ_REMOVE_HEAD(&w_free, w_list); 765181695Sattilio w_free_cnt--; 766181695Sattilio 767181695Sattilio memset(w_rmatrix, 0, 768181695Sattilio (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1))); 769181695Sattilio 77074912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 77174912Sjhb witness_lock_list_free(&w_locklistdata[i]); 772181695Sattilio witness_init_hash_tables(); 77374912Sjhb 77474912Sjhb /* First add in all the specified order lists. */ 77574912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 77674912Sjhb w = enroll(order->w_name, order->w_class); 77775569Sjhb if (w == NULL) 77875569Sjhb continue; 77974912Sjhb w->w_file = "order list"; 78074912Sjhb for (order++; order->w_name != NULL; order++) { 78174912Sjhb w1 = enroll(order->w_name, order->w_class); 78275569Sjhb if (w1 == NULL) 78375569Sjhb continue; 78474912Sjhb w1->w_file = "order list"; 785181695Sattilio itismychild(w, w1); 78674912Sjhb w = w1; 78765557Sjasone } 78865557Sjasone } 789151629Sjhb witness_spin_warn = 1; 79065557Sjasone 79174912Sjhb /* Iterate through all locks and add them to witness. */ 792179025Sattilio for (i = 0; pending_locks[i].wh_lock != NULL; i++) { 793179025Sattilio lock = pending_locks[i].wh_lock; 794153133Sjhb KASSERT(lock->lo_flags & LO_WITNESS, 795153133Sjhb ("%s: lock %s is on pending list but not LO_WITNESS", 796153133Sjhb __func__, lock->lo_name)); 797179025Sattilio lock->lo_witness = enroll(pending_locks[i].wh_type, 798179025Sattilio LOCK_CLASS(lock)); 79974912Sjhb } 80074912Sjhb 80174912Sjhb /* Mark the witness code as being ready for use. */ 802151629Sjhb witness_cold = 0; 80374912Sjhb 80474912Sjhb mtx_lock(&Giant); 80565557Sjasone} 806177253SrwatsonSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, 807177253Srwatson NULL); 80865557Sjasone 80974912Sjhbvoid 810179025Sattiliowitness_init(struct lock_object *lock, const char *type) 81174912Sjhb{ 81274912Sjhb struct lock_class *class; 81374912Sjhb 814153133Sjhb /* Various sanity checks. */ 815154077Sjhb class = LOCK_CLASS(lock); 81674912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 81774912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 81882284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 81974912Sjhb class->lc_name, lock->lo_name); 82074912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 82174912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 82282284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 82374912Sjhb class->lc_name, lock->lo_name); 82482244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 82582244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 82682284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 82782244Sjhb class->lc_name, lock->lo_name); 82882244Sjhb 829153133Sjhb /* 830153133Sjhb * If we shouldn't watch this lock, then just clear lo_witness. 831153133Sjhb * Otherwise, if witness_cold is set, then it is too early to 832153133Sjhb * enroll this lock, so defer it to witness_initialize() by adding 833153133Sjhb * it to the pending_locks list. If it is not too early, then enroll 834153133Sjhb * the lock now. 835153133Sjhb */ 836182446Sattilio if (witness_watch < 1 || panicstr != NULL || 837153133Sjhb (lock->lo_flags & LO_WITNESS) == 0) 838153133Sjhb lock->lo_witness = NULL; 839153133Sjhb else if (witness_cold) { 840179025Sattilio pending_locks[pending_cnt].wh_lock = lock; 841179025Sattilio pending_locks[pending_cnt++].wh_type = type; 842179025Sattilio if (pending_cnt > WITNESS_PENDLIST) 843179025Sattilio panic("%s: pending locks list is too small, bump it\n", 844179025Sattilio __func__); 845153133Sjhb } else 846179025Sattilio lock->lo_witness = enroll(type, class); 84774912Sjhb} 84874912Sjhb 84974912Sjhbvoid 85074912Sjhbwitness_destroy(struct lock_object *lock) 85174912Sjhb{ 852154077Sjhb struct lock_class *class; 85375362Sjhb struct witness *w; 85474912Sjhb 855154077Sjhb class = LOCK_CLASS(lock); 856181695Sattilio 85774912Sjhb if (witness_cold) 85874912Sjhb panic("lock (%s) %s destroyed while witness_cold", 859154077Sjhb class->lc_name, lock->lo_name); 86074912Sjhb 86176272Sjhb /* XXX: need to verify that no one holds the lock */ 862181695Sattilio if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL) 863181695Sattilio return; 864181695Sattilio w = lock->lo_witness; 865112117Sjhb 866181695Sattilio mtx_lock_spin(&w_mtx); 867181695Sattilio MPASS(w->w_refcount > 0); 868181695Sattilio w->w_refcount--; 869181695Sattilio 870181695Sattilio if (w->w_refcount == 0) 871181695Sattilio depart(w); 872181695Sattilio mtx_unlock_spin(&w_mtx); 87374912Sjhb} 87474912Sjhb 875112115Sjhb#ifdef DDB 87671352Sjasonestatic void 877181695Sattiliowitness_ddb_compute_levels(void) 878149979Struckman{ 879181695Sattilio struct witness *w; 880149979Struckman 881149979Struckman /* 882149979Struckman * First clear all levels. 883149979Struckman */ 884181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 885181695Sattilio w->w_ddb_level = -1; 886149979Struckman 887149979Struckman /* 888181695Sattilio * Look for locks with no parents and level all their descendants. 889149979Struckman */ 890149979Struckman STAILQ_FOREACH(w, &w_all, w_list) { 891181695Sattilio 892181695Sattilio /* If the witness has ancestors (is not a root), skip it. */ 893181695Sattilio if (w->w_num_ancestors > 0) 894181695Sattilio continue; 895181695Sattilio witness_ddb_level_descendants(w, 0); 896149979Struckman } 897149979Struckman} 898149979Struckman 899149979Struckmanstatic void 900181695Sattiliowitness_ddb_level_descendants(struct witness *w, int l) 901149979Struckman{ 902149979Struckman int i; 903149979Struckman 904181695Sattilio if (w->w_ddb_level >= l) 905181695Sattilio return; 906181695Sattilio 907181695Sattilio w->w_ddb_level = l; 908181695Sattilio l++; 909181695Sattilio 910181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 911181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 912181695Sattilio witness_ddb_level_descendants(&w_data[i], l); 913181695Sattilio } 914149979Struckman} 915149979Struckman 916149979Struckmanstatic void 917207922Sattiliowitness_ddb_display_descendants(int(*prnt)(const char *fmt, ...), 918181695Sattilio struct witness *w, int indent) 919149979Struckman{ 920181695Sattilio int i; 921149979Struckman 922181695Sattilio for (i = 0; i < indent; i++) 923181695Sattilio prnt(" "); 924181695Sattilio prnt("%s (type: %s, depth: %d, active refs: %d)", 925181695Sattilio w->w_name, w->w_class->lc_name, 926181695Sattilio w->w_ddb_level, w->w_refcount); 927181695Sattilio if (w->w_displayed) { 928181695Sattilio prnt(" -- (already displayed)\n"); 929181695Sattilio return; 930181695Sattilio } 931181695Sattilio w->w_displayed = 1; 932181695Sattilio if (w->w_file != NULL && w->w_line != 0) 933234111Sadrian prnt(" -- last acquired @ %s:%d\n", fixup_filename(w->w_file), 934181695Sattilio w->w_line); 935149979Struckman else 936181695Sattilio prnt(" -- never acquired\n"); 937181695Sattilio indent++; 938181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 939181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 940239874Sjhb if (db_pager_quit) 941239874Sjhb return; 942181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 943181695Sattilio witness_ddb_display_descendants(prnt, &w_data[i], 944181695Sattilio indent); 945149979Struckman } 946149979Struckman} 947149979Struckman 948149979Struckmanstatic void 949207922Sattiliowitness_ddb_display_list(int(*prnt)(const char *fmt, ...), 950181695Sattilio struct witness_list *list) 95171352Sjasone{ 952112118Sjhb struct witness *w; 95371352Sjasone 95474912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 955181695Sattilio if (w->w_file == NULL || w->w_ddb_level > 0) 95671352Sjasone continue; 957181695Sattilio 958181695Sattilio /* This lock has no anscestors - display its descendants. */ 959181695Sattilio witness_ddb_display_descendants(prnt, w, 0); 960239874Sjhb if (db_pager_quit) 961239874Sjhb return; 96271352Sjasone } 96374912Sjhb} 96472224Sjhb 96574912Sjhbstatic void 966207922Sattiliowitness_ddb_display(int(*prnt)(const char *fmt, ...)) 967178841Sattilio{ 96874912Sjhb struct witness *w; 96974912Sjhb 970181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 971181695Sattilio witness_ddb_compute_levels(); 97274912Sjhb 973112118Sjhb /* Clear all the displayed flags. */ 974181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 975112118Sjhb w->w_displayed = 0; 976112118Sjhb 97772224Sjhb /* 97874930Sjhb * First, handle sleep locks which have been acquired at least 97974912Sjhb * once. 98074912Sjhb */ 98174912Sjhb prnt("Sleep locks:\n"); 982181695Sattilio witness_ddb_display_list(prnt, &w_sleep); 983239874Sjhb if (db_pager_quit) 984239874Sjhb return; 98574912Sjhb 98674912Sjhb /* 98774930Sjhb * Now do spin locks which have been acquired at least once. 98872224Sjhb */ 98974912Sjhb prnt("\nSpin locks:\n"); 990181695Sattilio witness_ddb_display_list(prnt, &w_spin); 991239874Sjhb if (db_pager_quit) 992239874Sjhb return; 99372224Sjhb 99472224Sjhb /* 99574930Sjhb * Finally, any locks which have not been acquired yet. 99672224Sjhb */ 99774912Sjhb prnt("\nLocks which were never acquired:\n"); 99874912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 99997948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 100071352Sjasone continue; 1001181695Sattilio prnt("%s (type: %s, depth: %d)\n", w->w_name, 1002181695Sattilio w->w_class->lc_name, w->w_ddb_level); 1003239874Sjhb if (db_pager_quit) 1004239874Sjhb return; 100571352Sjasone } 100671352Sjasone} 1007112115Sjhb#endif /* DDB */ 100871352Sjasone 1009125160Sjhbint 1010125160Sjhbwitness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 1011125160Sjhb{ 1012125160Sjhb 1013182473Sattilio if (witness_watch == -1 || panicstr != NULL) 1014125160Sjhb return (0); 1015125160Sjhb 1016125160Sjhb /* Require locks that witness knows about. */ 1017125160Sjhb if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 1018125160Sjhb lock2->lo_witness == NULL) 1019125160Sjhb return (EINVAL); 1020125160Sjhb 1021181695Sattilio mtx_assert(&w_mtx, MA_NOTOWNED); 1022125160Sjhb mtx_lock_spin(&w_mtx); 1023125160Sjhb 1024125160Sjhb /* 1025125160Sjhb * If we already have either an explicit or implied lock order that 1026125160Sjhb * is the other way around, then return an error. 1027125160Sjhb */ 1028182473Sattilio if (witness_watch && 1029182473Sattilio isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 1030125160Sjhb mtx_unlock_spin(&w_mtx); 1031125160Sjhb return (EDOOFUS); 1032125160Sjhb } 1033125160Sjhb 1034125160Sjhb /* Try to add the new order. */ 1035125160Sjhb CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1036179025Sattilio lock2->lo_witness->w_name, lock1->lo_witness->w_name); 1037181695Sattilio itismychild(lock1->lo_witness, lock2->lo_witness); 1038125160Sjhb mtx_unlock_spin(&w_mtx); 1039125160Sjhb return (0); 1040125160Sjhb} 1041125160Sjhb 104265557Sjasonevoid 1043125160Sjhbwitness_checkorder(struct lock_object *lock, int flags, const char *file, 1044182914Sjhb int line, struct lock_object *interlock) 104565557Sjasone{ 1046183955Sattilio struct lock_list_entry *lock_list, *lle; 1047182914Sjhb struct lock_instance *lock1, *lock2, *plock; 104874912Sjhb struct lock_class *class; 104965856Sjhb struct witness *w, *w1; 105083366Sjulian struct thread *td; 105174912Sjhb int i, j; 105265557Sjasone 1053182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL || 105480747Sjhb panicstr != NULL) 105571320Sjasone return; 1056125160Sjhb 105774912Sjhb w = lock->lo_witness; 1058154077Sjhb class = LOCK_CLASS(lock); 105983366Sjulian td = curthread; 106065557Sjasone 106174912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 1062181695Sattilio 106393676Sjhb /* 106493676Sjhb * Since spin locks include a critical section, this check 1065131884Sjhb * implicitly enforces a lock order of all sleep locks before 106693676Sjhb * all spin locks. 106793676Sjhb */ 1068136304Sgreen if (td->td_critnest != 0 && !kdb_active) 106974912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 1070234111Sadrian class->lc_name, lock->lo_name, 1071234111Sadrian fixup_filename(file), line); 1072131884Sjhb 1073131884Sjhb /* 1074131884Sjhb * If this is the first lock acquired then just return as 1075131884Sjhb * no order checking is needed. 1076131884Sjhb */ 1077183955Sattilio lock_list = td->td_sleeplocks; 1078183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) 1079131884Sjhb return; 1080131884Sjhb } else { 1081181695Sattilio 1082131884Sjhb /* 1083131884Sjhb * If this is the first lock, just return as no order 1084183955Sattilio * checking is needed. Avoid problems with thread 1085183955Sattilio * migration pinning the thread while checking if 1086183955Sattilio * spinlocks are held. If at least one spinlock is held 1087183955Sattilio * the thread is in a safe path and it is allowed to 1088183955Sattilio * unpin it. 1089131884Sjhb */ 1090183955Sattilio sched_pin(); 1091183955Sattilio lock_list = PCPU_GET(spinlocks); 1092183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) { 1093183955Sattilio sched_unpin(); 1094131884Sjhb return; 1095183955Sattilio } 1096183955Sattilio sched_unpin(); 1097131884Sjhb } 109865557Sjasone 109976772Sjhb /* 1100125160Sjhb * Check to see if we are recursing on a lock we already own. If 1101125160Sjhb * so, make sure that we don't mismatch exclusive and shared lock 1102125160Sjhb * acquires. 110376272Sjhb */ 1104183955Sattilio lock1 = find_instance(lock_list, lock); 110576272Sjhb if (lock1 != NULL) { 110676272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 110776272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 110876272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 1109234111Sadrian class->lc_name, lock->lo_name, 1110234111Sadrian fixup_filename(file), line); 111176272Sjhb printf("while exclusively locked from %s:%d\n", 1112234111Sadrian fixup_filename(lock1->li_file), lock1->li_line); 111376272Sjhb panic("share->excl"); 111476272Sjhb } 111576272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 111676272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 111776272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 1118234111Sadrian class->lc_name, lock->lo_name, 1119234111Sadrian fixup_filename(file), line); 112076272Sjhb printf("while share locked from %s:%d\n", 1121234111Sadrian fixup_filename(lock1->li_file), lock1->li_line); 112276272Sjhb panic("excl->share"); 112376272Sjhb } 112476272Sjhb return; 112576272Sjhb } 112676272Sjhb 112776272Sjhb /* 1128182914Sjhb * Find the previously acquired lock, but ignore interlocks. 1129182914Sjhb */ 1130183955Sattilio plock = &lock_list->ll_children[lock_list->ll_count - 1]; 1131182914Sjhb if (interlock != NULL && plock->li_lock == interlock) { 1132183955Sattilio if (lock_list->ll_count > 1) 1133183955Sattilio plock = 1134183955Sattilio &lock_list->ll_children[lock_list->ll_count - 2]; 1135183955Sattilio else { 1136183955Sattilio lle = lock_list->ll_next; 1137182984Sattilio 1138182914Sjhb /* 1139182914Sjhb * The interlock is the only lock we hold, so 1140183955Sattilio * simply return. 1141182914Sjhb */ 1142183955Sattilio if (lle == NULL) 1143183955Sattilio return; 1144183955Sattilio plock = &lle->ll_children[lle->ll_count - 1]; 1145182914Sjhb } 1146182914Sjhb } 1147182914Sjhb 1148182914Sjhb /* 1149181695Sattilio * Try to perform most checks without a lock. If this succeeds we 1150181695Sattilio * can skip acquiring the lock and return success. 1151181695Sattilio */ 1152182914Sjhb w1 = plock->li_lock->lo_witness; 1153181695Sattilio if (witness_lock_order_check(w1, w)) 1154181695Sattilio return; 1155181695Sattilio 1156181695Sattilio /* 115774912Sjhb * Check for duplicate locks of the same type. Note that we only 115874912Sjhb * have to check for this on the last lock we just acquired. Any 115974912Sjhb * other cases will be caught as lock order violations. 116074912Sjhb */ 1161181695Sattilio mtx_lock_spin(&w_mtx); 1162181695Sattilio witness_lock_order_add(w1, w); 116374912Sjhb if (w1 == w) { 1164181695Sattilio i = w->w_index; 1165181695Sattilio if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) && 1166181695Sattilio !(w_rmatrix[i][i] & WITNESS_REVERSAL)) { 1167181695Sattilio w_rmatrix[i][i] |= WITNESS_REVERSAL; 1168181695Sattilio w->w_reversed = 1; 1169181695Sattilio mtx_unlock_spin(&w_mtx); 1170183574Sjhb printf( 1171183574Sjhb "acquiring duplicate lock of same type: \"%s\"\n", 1172181695Sattilio w->w_name); 1173183574Sjhb printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name, 1174234111Sadrian fixup_filename(plock->li_file), plock->li_line); 1175234111Sadrian printf(" 2nd %s @ %s:%d\n", lock->lo_name, 1176234111Sadrian fixup_filename(file), line); 1177181695Sattilio witness_debugger(1); 1178234111Sadrian } else 1179234111Sadrian mtx_unlock_spin(&w_mtx); 1180125160Sjhb return; 118165557Sjasone } 1182181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1183181695Sattilio 118465557Sjasone /* 1185218909Sbrucec * If we know that the lock we are acquiring comes after 1186111881Sjhb * the lock we most recently acquired in the lock order tree, 1187111881Sjhb * then there is no need for any further checks. 1188111881Sjhb */ 1189181695Sattilio if (isitmychild(w1, w)) 1190181695Sattilio goto out; 1191181695Sattilio 1192183955Sattilio for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) { 119374912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 119465557Sjasone 119574912Sjhb MPASS(j < WITNESS_COUNT); 119676272Sjhb lock1 = &lle->ll_children[i]; 119774912Sjhb 119874912Sjhb /* 1199182914Sjhb * Ignore the interlock the first time we see it. 1200182914Sjhb */ 1201182914Sjhb if (interlock != NULL && interlock == lock1->li_lock) { 1202182914Sjhb interlock = NULL; 1203182914Sjhb continue; 1204182914Sjhb } 1205182914Sjhb 1206182914Sjhb /* 120774912Sjhb * If this lock doesn't undergo witness checking, 120874912Sjhb * then skip it. 120974912Sjhb */ 1210182914Sjhb w1 = lock1->li_lock->lo_witness; 121174912Sjhb if (w1 == NULL) { 121276272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 121374912Sjhb ("lock missing witness structure")); 121474912Sjhb continue; 121574912Sjhb } 1216181695Sattilio 121776272Sjhb /* 1218111881Sjhb * If we are locking Giant and this is a sleepable 121976272Sjhb * lock, then skip it. 122076272Sjhb */ 1221111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 1222167787Sjhb lock == &Giant.lock_object) 122376272Sjhb continue; 1224181695Sattilio 122593690Sjhb /* 122693690Sjhb * If we are locking a sleepable lock and this lock 1227111881Sjhb * is Giant, then skip it. 122893690Sjhb */ 1229111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 1230167787Sjhb lock1->li_lock == &Giant.lock_object) 1231111881Sjhb continue; 1232181695Sattilio 1233111881Sjhb /* 1234111881Sjhb * If we are locking a sleepable lock and this lock 1235111881Sjhb * isn't sleepable, we want to treat it as a lock 1236111881Sjhb * order violation to enfore a general lock order of 1237111881Sjhb * sleepable locks before non-sleepable locks. 1238111881Sjhb */ 1239149738Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1240111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1241149738Sjhb goto reversal; 1242181695Sattilio 1243149738Sjhb /* 1244150179Sjhb * If we are locking Giant and this is a non-sleepable 1245150179Sjhb * lock, then treat it as a reversal. 1246150179Sjhb */ 1247150179Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 1248167787Sjhb lock == &Giant.lock_object) 1249150179Sjhb goto reversal; 1250181695Sattilio 1251150179Sjhb /* 1252149738Sjhb * Check the lock order hierarchy for a reveresal. 1253149738Sjhb */ 1254149738Sjhb if (!isitmydescendant(w, w1)) 125574912Sjhb continue; 1256149738Sjhb reversal: 1257181695Sattilio 125874912Sjhb /* 125974912Sjhb * We have a lock order violation, check to see if it 126074912Sjhb * is allowed or has already been yelled about. 126174912Sjhb */ 1262105508Sphk#ifdef BLESSING 1263181695Sattilio 1264125160Sjhb /* 1265125160Sjhb * If the lock order is blessed, just bail. We don't 1266125160Sjhb * look for other lock order violations though, which 1267125160Sjhb * may be a bug. 1268125160Sjhb */ 126965557Sjasone if (blessed(w, w1)) 1270181695Sattilio goto out; 1271105508Sphk#endif 1272181695Sattilio 1273181695Sattilio /* Bail if this violation is known */ 1274181695Sattilio if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL) 1275181695Sattilio goto out; 1276181695Sattilio 1277181695Sattilio /* Record this as a violation */ 1278181695Sattilio w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL; 1279181695Sattilio w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL; 1280181695Sattilio w->w_reversed = w1->w_reversed = 1; 1281181695Sattilio witness_increment_graph_generation(); 1282181695Sattilio mtx_unlock_spin(&w_mtx); 1283181695Sattilio 128474912Sjhb /* 128574912Sjhb * Ok, yell about it. 128674912Sjhb */ 1287150179Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1288150179Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1289150179Sjhb printf( 1290150179Sjhb "lock order reversal: (sleepable after non-sleepable)\n"); 1291150179Sjhb else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1292167787Sjhb && lock == &Giant.lock_object) 1293150179Sjhb printf( 1294150179Sjhb "lock order reversal: (Giant after non-sleepable)\n"); 1295150179Sjhb else 1296150179Sjhb printf("lock order reversal:\n"); 1297181695Sattilio 129874912Sjhb /* 129974912Sjhb * Try to locate an earlier lock with 130074912Sjhb * witness w in our list. 130174912Sjhb */ 130274912Sjhb do { 130376272Sjhb lock2 = &lle->ll_children[i]; 130476272Sjhb MPASS(lock2->li_lock != NULL); 130576272Sjhb if (lock2->li_lock->lo_witness == w) 130674912Sjhb break; 130774912Sjhb if (i == 0 && lle->ll_next != NULL) { 130874912Sjhb lle = lle->ll_next; 130974912Sjhb i = lle->ll_count - 1; 1310106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 1311125160Sjhb } else 1312125160Sjhb i--; 131374912Sjhb } while (i >= 0); 131476272Sjhb if (i < 0) { 131593811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 131693811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 1317234111Sadrian w1->w_name, fixup_filename(lock1->li_file), 1318234111Sadrian lock1->li_line); 131993811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 1320234111Sadrian lock->lo_name, w->w_name, 1321234111Sadrian fixup_filename(file), line); 132276272Sjhb } else { 132393811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 132493811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 1325179025Sattilio lock2->li_lock->lo_witness->w_name, 1326234111Sadrian fixup_filename(lock2->li_file), 1327234111Sadrian lock2->li_line); 132893811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 132993811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 1330234111Sadrian w1->w_name, fixup_filename(lock1->li_file), 1331234111Sadrian lock1->li_line); 133293811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 1333234111Sadrian lock->lo_name, w->w_name, 1334234111Sadrian fixup_filename(file), line); 133576272Sjhb } 1336181695Sattilio witness_debugger(1); 1337125160Sjhb return; 133865557Sjasone } 133965557Sjasone } 1340181695Sattilio 134178871Sjhb /* 1342125160Sjhb * If requested, build a new lock order. However, don't build a new 1343125160Sjhb * relationship between a sleepable lock and Giant if it is in the 1344125160Sjhb * wrong direction. The correct lock order is that sleepable locks 1345125160Sjhb * always come before Giant. 134678871Sjhb */ 1347125160Sjhb if (flags & LOP_NEWORDER && 1348182914Sjhb !(plock->li_lock == &Giant.lock_object && 1349112117Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0)) { 135087593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1351182914Sjhb w->w_name, plock->li_lock->lo_witness->w_name); 1352182914Sjhb itismychild(plock->li_lock->lo_witness, w); 1353181695Sattilio } 1354181695Sattilioout: 1355112117Sjhb mtx_unlock_spin(&w_mtx); 1356125160Sjhb} 1357125160Sjhb 1358125160Sjhbvoid 1359125160Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 1360125160Sjhb{ 1361125160Sjhb struct lock_list_entry **lock_list, *lle; 1362125160Sjhb struct lock_instance *instance; 1363125160Sjhb struct witness *w; 1364125160Sjhb struct thread *td; 1365125160Sjhb 1366182446Sattilio if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL || 1367125160Sjhb panicstr != NULL) 1368125160Sjhb return; 1369125160Sjhb w = lock->lo_witness; 1370125160Sjhb td = curthread; 1371125160Sjhb 1372125160Sjhb /* Determine lock list for this lock. */ 1373154077Sjhb if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1374125160Sjhb lock_list = &td->td_sleeplocks; 1375125160Sjhb else 1376125160Sjhb lock_list = PCPU_PTR(spinlocks); 1377125160Sjhb 1378125160Sjhb /* Check to see if we are recursing on a lock we already own. */ 1379125160Sjhb instance = find_instance(*lock_list, lock); 1380125160Sjhb if (instance != NULL) { 1381125160Sjhb instance->li_flags++; 1382125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1383125160Sjhb td->td_proc->p_pid, lock->lo_name, 1384125160Sjhb instance->li_flags & LI_RECURSEMASK); 1385125160Sjhb instance->li_file = file; 1386125160Sjhb instance->li_line = line; 1387125160Sjhb return; 1388110779Speter } 1389125160Sjhb 1390125160Sjhb /* Update per-witness last file and line acquire. */ 139165557Sjasone w->w_file = file; 139265557Sjasone w->w_line = line; 1393125160Sjhb 1394125160Sjhb /* Find the next open lock instance in the list and fill it. */ 139574912Sjhb lle = *lock_list; 139676272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 139778785Sjhb lle = witness_lock_list_get(); 139878785Sjhb if (lle == NULL) 139965557Sjasone return; 140078785Sjhb lle->ll_next = *lock_list; 140187593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 140284680Sjhb td->td_proc->p_pid, lle); 140378785Sjhb *lock_list = lle; 140465557Sjasone } 1405125160Sjhb instance = &lle->ll_children[lle->ll_count++]; 1406125160Sjhb instance->li_lock = lock; 1407125160Sjhb instance->li_line = line; 1408125160Sjhb instance->li_file = file; 140976272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 1410125160Sjhb instance->li_flags = LI_EXCLUSIVE; 141176272Sjhb else 1412125160Sjhb instance->li_flags = 0; 141387593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 141484680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 141565557Sjasone} 141665557Sjasone 141765557Sjasonevoid 141882244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 141982244Sjhb{ 142082244Sjhb struct lock_instance *instance; 142182244Sjhb struct lock_class *class; 142282244Sjhb 1423181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1424182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 142582244Sjhb return; 1426154077Sjhb class = LOCK_CLASS(lock); 1427182473Sattilio if (witness_watch) { 1428182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1429182473Sattilio panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 1430234111Sadrian class->lc_name, lock->lo_name, 1431234111Sadrian fixup_filename(file), line); 1432182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1433182473Sattilio panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 1434234111Sadrian class->lc_name, lock->lo_name, 1435234111Sadrian fixup_filename(file), line); 1436182473Sattilio } 143783366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 143882244Sjhb if (instance == NULL) 143982244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 1440234111Sadrian class->lc_name, lock->lo_name, 1441234111Sadrian fixup_filename(file), line); 1442182473Sattilio if (witness_watch) { 1443182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1444182473Sattilio panic("upgrade of exclusive lock (%s) %s @ %s:%d", 1445234111Sadrian class->lc_name, lock->lo_name, 1446234111Sadrian fixup_filename(file), line); 1447182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1448182473Sattilio panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1449182473Sattilio class->lc_name, lock->lo_name, 1450234111Sadrian instance->li_flags & LI_RECURSEMASK, 1451234111Sadrian fixup_filename(file), line); 1452182473Sattilio } 145382244Sjhb instance->li_flags |= LI_EXCLUSIVE; 145482244Sjhb} 145582244Sjhb 145682244Sjhbvoid 145782244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 145882244Sjhb int line) 145982244Sjhb{ 146082244Sjhb struct lock_instance *instance; 146182244Sjhb struct lock_class *class; 146282244Sjhb 1463181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1464182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 146582244Sjhb return; 1466154077Sjhb class = LOCK_CLASS(lock); 1467182473Sattilio if (witness_watch) { 1468182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 146982244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 1470234111Sadrian class->lc_name, lock->lo_name, 1471234111Sadrian fixup_filename(file), line); 1472182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1473182473Sattilio panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 1474234111Sadrian class->lc_name, lock->lo_name, 1475234111Sadrian fixup_filename(file), line); 1476182473Sattilio } 147783366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 147882244Sjhb if (instance == NULL) 147982244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 1480234111Sadrian class->lc_name, lock->lo_name, 1481234111Sadrian fixup_filename(file), line); 1482182473Sattilio if (witness_watch) { 1483182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1484182473Sattilio panic("downgrade of shared lock (%s) %s @ %s:%d", 1485234111Sadrian class->lc_name, lock->lo_name, 1486234111Sadrian fixup_filename(file), line); 1487182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1488182473Sattilio panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1489182473Sattilio class->lc_name, lock->lo_name, 1490234111Sadrian instance->li_flags & LI_RECURSEMASK, 1491234111Sadrian fixup_filename(file), line); 1492182473Sattilio } 149382244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 149482244Sjhb} 149582244Sjhb 149682244Sjhbvoid 149774912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 149865557Sjasone{ 149974912Sjhb struct lock_list_entry **lock_list, *lle; 150076272Sjhb struct lock_instance *instance; 150174912Sjhb struct lock_class *class; 150283366Sjulian struct thread *td; 150392858Simp register_t s; 150474912Sjhb int i, j; 150565557Sjasone 1506182446Sattilio if (witness_cold || lock->lo_witness == NULL || panicstr != NULL) 150771352Sjasone return; 150883366Sjulian td = curthread; 1509154077Sjhb class = LOCK_CLASS(lock); 1510125160Sjhb 1511125160Sjhb /* Find lock instance associated with this lock. */ 151276272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 151383366Sjulian lock_list = &td->td_sleeplocks; 151476272Sjhb else 151574912Sjhb lock_list = PCPU_PTR(spinlocks); 1516181695Sattilio lle = *lock_list; 151774912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 151876272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 151976272Sjhb instance = &(*lock_list)->ll_children[i]; 1520125160Sjhb if (instance->li_lock == lock) 1521125160Sjhb goto found; 152276272Sjhb } 1523182446Sattilio 1524182446Sattilio /* 1525182446Sattilio * When disabling WITNESS through witness_watch we could end up in 1526182473Sattilio * having registered locks in the td_sleeplocks queue. 1527182446Sattilio * We have to make sure we flush these queues, so just search for 1528182473Sattilio * eventual register locks and remove them. 1529182446Sattilio */ 1530182446Sattilio if (witness_watch > 0) 1531182446Sattilio panic("lock (%s) %s not locked @ %s:%d", class->lc_name, 1532234111Sadrian lock->lo_name, fixup_filename(file), line); 1533182446Sattilio else 1534182446Sattilio return; 1535125160Sjhbfound: 1536125160Sjhb 1537125160Sjhb /* First, check for shared/exclusive mismatches. */ 1538182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 && 1539125160Sjhb (flags & LOP_EXCLUSIVE) == 0) { 1540125160Sjhb printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1541234111Sadrian lock->lo_name, fixup_filename(file), line); 1542125160Sjhb printf("while exclusively locked from %s:%d\n", 1543234111Sadrian fixup_filename(instance->li_file), instance->li_line); 1544125160Sjhb panic("excl->ushare"); 1545125160Sjhb } 1546182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 && 1547125160Sjhb (flags & LOP_EXCLUSIVE) != 0) { 1548125160Sjhb printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1549234111Sadrian lock->lo_name, fixup_filename(file), line); 1550234111Sadrian printf("while share locked from %s:%d\n", 1551234111Sadrian fixup_filename(instance->li_file), 1552125160Sjhb instance->li_line); 1553125160Sjhb panic("share->uexcl"); 1554125160Sjhb } 1555125160Sjhb /* If we are recursed, unrecurse. */ 1556125160Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1557125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1558125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1559125160Sjhb instance->li_flags); 1560125160Sjhb instance->li_flags--; 1561125160Sjhb return; 1562125160Sjhb } 1563189194Sthompsa /* The lock is now being dropped, check for NORELEASE flag */ 1564189194Sthompsa if ((instance->li_flags & LI_NORELEASE) != 0 && witness_watch > 0) { 1565189194Sthompsa printf("forbidden unlock of (%s) %s @ %s:%d\n", class->lc_name, 1566234111Sadrian lock->lo_name, fixup_filename(file), line); 1567189194Sthompsa panic("lock marked norelease"); 1568189194Sthompsa } 1569125160Sjhb 1570125160Sjhb /* Otherwise, remove this item from the list. */ 1571125160Sjhb s = intr_disable(); 1572125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1573125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1574125160Sjhb (*lock_list)->ll_count - 1); 1575125160Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 1576125160Sjhb (*lock_list)->ll_children[j] = 1577125160Sjhb (*lock_list)->ll_children[j + 1]; 1578125160Sjhb (*lock_list)->ll_count--; 1579125160Sjhb intr_restore(s); 1580125160Sjhb 1581181695Sattilio /* 1582182984Sattilio * In order to reduce contention on w_mtx, we want to keep always an 1583182984Sattilio * head object into lists so that frequent allocation from the 1584182984Sattilio * free witness pool (and subsequent locking) is avoided. 1585182984Sattilio * In order to maintain the current code simple, when the head 1586182984Sattilio * object is totally unloaded it means also that we do not have 1587182984Sattilio * further objects in the list, so the list ownership needs to be 1588182984Sattilio * hand over to another object if the current head needs to be freed. 1589181695Sattilio */ 1590182984Sattilio if ((*lock_list)->ll_count == 0) { 1591182984Sattilio if (*lock_list == lle) { 1592182984Sattilio if (lle->ll_next == NULL) 1593182984Sattilio return; 1594182984Sattilio } else 1595182984Sattilio lle = *lock_list; 1596125160Sjhb *lock_list = lle->ll_next; 1597125160Sjhb CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1598125160Sjhb td->td_proc->p_pid, lle); 1599125160Sjhb witness_lock_list_free(lle); 1600125160Sjhb } 160165557Sjasone} 160265557Sjasone 1603181695Sattiliovoid 1604181695Sattiliowitness_thread_exit(struct thread *td) 1605181695Sattilio{ 1606181695Sattilio struct lock_list_entry *lle; 1607181695Sattilio int i, n; 1608181695Sattilio 1609181695Sattilio lle = td->td_sleeplocks; 1610181695Sattilio if (lle == NULL || panicstr != NULL) 1611181695Sattilio return; 1612181695Sattilio if (lle->ll_count != 0) { 1613181695Sattilio for (n = 0; lle != NULL; lle = lle->ll_next) 1614181695Sattilio for (i = lle->ll_count - 1; i >= 0; i--) { 1615181695Sattilio if (n == 0) 1616181695Sattilio printf("Thread %p exiting with the following locks held:\n", 1617181695Sattilio td); 1618181695Sattilio n++; 1619207929Sattilio witness_list_lock(&lle->ll_children[i], printf); 1620181695Sattilio 1621181695Sattilio } 1622181695Sattilio panic("Thread %p cannot exit while holding sleeplocks\n", td); 1623181695Sattilio } 1624181695Sattilio witness_lock_list_free(lle); 1625181695Sattilio} 1626181695Sattilio 162774912Sjhb/* 1628111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 1629111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 1630111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 1631111881Sjhb * console along with a list of the offending locks. If indicated in the 1632111881Sjhb * flags then a failure results in a panic as well. 163374912Sjhb */ 163465557Sjasoneint 1635111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 163665557Sjasone{ 1637183955Sattilio struct lock_list_entry *lock_list, *lle; 163876272Sjhb struct lock_instance *lock1; 163983366Sjulian struct thread *td; 1640111881Sjhb va_list ap; 164174912Sjhb int i, n; 164265557Sjasone 1643182446Sattilio if (witness_cold || witness_watch < 1 || panicstr != NULL) 164474912Sjhb return (0); 164574912Sjhb n = 0; 164683366Sjulian td = curthread; 1647111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 164874912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 164976272Sjhb lock1 = &lle->ll_children[i]; 1650111881Sjhb if (lock1->li_lock == lock) 1651111881Sjhb continue; 1652111881Sjhb if (flags & WARN_GIANTOK && 1653167787Sjhb lock1->li_lock == &Giant.lock_object) 165474912Sjhb continue; 1655111881Sjhb if (flags & WARN_SLEEPOK && 1656111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 165776272Sjhb continue; 1658111881Sjhb if (n == 0) { 1659111881Sjhb va_start(ap, fmt); 1660111881Sjhb vprintf(fmt, ap); 1661111881Sjhb va_end(ap); 1662111881Sjhb printf(" with the following"); 1663111881Sjhb if (flags & WARN_SLEEPOK) 1664111881Sjhb printf(" non-sleepable"); 1665118441Sjhb printf(" locks held:\n"); 166676272Sjhb } 166774912Sjhb n++; 1668207929Sattilio witness_list_lock(lock1, printf); 166974912Sjhb } 1670181695Sattilio 1671183955Sattilio /* 1672183955Sattilio * Pin the thread in order to avoid problems with thread migration. 1673183955Sattilio * Once that all verifies are passed about spinlocks ownership, 1674183955Sattilio * the thread is in a safe path and it can be unpinned. 1675183955Sattilio */ 1676183955Sattilio sched_pin(); 1677183955Sattilio lock_list = PCPU_GET(spinlocks); 1678184098Sattilio if (lock_list != NULL && lock_list->ll_count != 0) { 1679183955Sattilio sched_unpin(); 1680181695Sattilio 168197006Sjhb /* 1682183955Sattilio * We should only have one spinlock and as long as 1683183955Sattilio * the flags cannot match for this locks class, 1684183955Sattilio * check if the first spinlock is the one curthread 1685183955Sattilio * should hold. 168697006Sjhb */ 1687183955Sattilio lock1 = &lock_list->ll_children[lock_list->ll_count - 1]; 1688184098Sattilio if (lock_list->ll_count == 1 && lock_list->ll_next == NULL && 1689184098Sattilio lock1->li_lock == lock && n == 0) 1690184098Sattilio return (0); 1691183955Sattilio 1692184098Sattilio va_start(ap, fmt); 1693184098Sattilio vprintf(fmt, ap); 1694184098Sattilio va_end(ap); 1695184098Sattilio printf(" with the following"); 1696184098Sattilio if (flags & WARN_SLEEPOK) 1697184098Sattilio printf(" non-sleepable"); 1698184098Sattilio printf(" locks held:\n"); 1699207929Sattilio n += witness_list_locks(&lock_list, printf); 1700183955Sattilio } else 1701183955Sattilio sched_unpin(); 1702111881Sjhb if (flags & WARN_PANIC && n) 1703181695Sattilio panic("%s", __func__); 1704181695Sattilio else 1705181695Sattilio witness_debugger(n); 170665557Sjasone return (n); 170765557Sjasone} 170865557Sjasone 1709102448Siedowseconst char * 1710102448Siedowsewitness_file(struct lock_object *lock) 1711102448Siedowse{ 1712102448Siedowse struct witness *w; 1713102448Siedowse 1714182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1715102448Siedowse return ("?"); 1716102448Siedowse w = lock->lo_witness; 1717102448Siedowse return (w->w_file); 1718102448Siedowse} 1719102448Siedowse 1720102448Siedowseint 1721102448Siedowsewitness_line(struct lock_object *lock) 1722102448Siedowse{ 1723102448Siedowse struct witness *w; 1724102448Siedowse 1725182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1726102448Siedowse return (0); 1727102448Siedowse w = lock->lo_witness; 1728102448Siedowse return (w->w_line); 1729102448Siedowse} 1730102448Siedowse 173165856Sjhbstatic struct witness * 173274912Sjhbenroll(const char *description, struct lock_class *lock_class) 173365557Sjasone{ 173474912Sjhb struct witness *w; 1735181695Sattilio struct witness_list *typelist; 173665557Sjasone 1737181695Sattilio MPASS(description != NULL); 1738181695Sattilio 1739182473Sattilio if (witness_watch == -1 || panicstr != NULL) 174065557Sjasone return (NULL); 1741181695Sattilio if ((lock_class->lc_flags & LC_SPINLOCK)) { 1742181695Sattilio if (witness_skipspin) 1743181695Sattilio return (NULL); 1744181695Sattilio else 1745181695Sattilio typelist = &w_spin; 1746181695Sattilio } else if ((lock_class->lc_flags & LC_SLEEPLOCK)) 1747181695Sattilio typelist = &w_sleep; 1748181695Sattilio else 1749181695Sattilio panic("lock class %s is not sleep or spin", 1750181695Sattilio lock_class->lc_name); 1751181695Sattilio 1752181695Sattilio mtx_lock_spin(&w_mtx); 1753181695Sattilio w = witness_hash_get(description); 1754181695Sattilio if (w) 1755181695Sattilio goto found; 1756181695Sattilio if ((w = witness_get()) == NULL) 175765557Sjasone return (NULL); 1758181695Sattilio MPASS(strlen(description) < MAX_W_NAME); 1759181695Sattilio strcpy(w->w_name, description); 176074912Sjhb w->w_class = lock_class; 176175362Sjhb w->w_refcount = 1; 176274912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 1763149441Struckman if (lock_class->lc_flags & LC_SPINLOCK) { 176474912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1765149441Struckman w_spin_cnt++; 1766149441Struckman } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 176774912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1768149441Struckman w_sleep_cnt++; 176975364Sbp } 1770181695Sattilio 1771181695Sattilio /* Insert new witness into the hash */ 1772181695Sattilio witness_hash_put(w); 1773181695Sattilio witness_increment_graph_generation(); 177474912Sjhb mtx_unlock_spin(&w_mtx); 177565557Sjasone return (w); 1776181695Sattiliofound: 1777181695Sattilio w->w_refcount++; 1778181695Sattilio mtx_unlock_spin(&w_mtx); 1779181695Sattilio if (lock_class != w->w_class) 1780181695Sattilio panic( 1781181695Sattilio "lock (%s) %s does not match earlier (%s) lock", 1782181695Sattilio description, lock_class->lc_name, 1783181695Sattilio w->w_class->lc_name); 1784181695Sattilio return (w); 178565557Sjasone} 178665557Sjasone 1787179025Sattiliostatic void 1788112117Sjhbdepart(struct witness *w) 178965557Sjasone{ 179074912Sjhb struct witness_list *list; 179165557Sjasone 1792112117Sjhb MPASS(w->w_refcount == 0); 1793149441Struckman if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1794112117Sjhb list = &w_sleep; 1795149441Struckman w_sleep_cnt--; 1796149441Struckman } else { 1797112117Sjhb list = &w_spin; 1798149441Struckman w_spin_cnt--; 1799149441Struckman } 1800112117Sjhb /* 1801181695Sattilio * Set file to NULL as it may point into a loadable module. 1802112117Sjhb */ 1803181695Sattilio w->w_file = NULL; 1804181695Sattilio w->w_line = 0; 1805181695Sattilio witness_increment_graph_generation(); 1806181695Sattilio} 1807112117Sjhb 1808181695Sattilio 1809181695Sattiliostatic void 1810181695Sattilioadopt(struct witness *parent, struct witness *child) 1811181695Sattilio{ 1812181695Sattilio int pi, ci, i, j; 1813181695Sattilio 1814181695Sattilio if (witness_cold == 0) 1815181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1816181695Sattilio 1817181695Sattilio /* If the relationship is already known, there's no work to be done. */ 1818181695Sattilio if (isitmychild(parent, child)) 1819181695Sattilio return; 1820181695Sattilio 1821181695Sattilio /* When the structure of the graph changes, bump up the generation. */ 1822181695Sattilio witness_increment_graph_generation(); 1823181695Sattilio 1824112117Sjhb /* 1825181695Sattilio * The hard part ... create the direct relationship, then propagate all 1826181695Sattilio * indirect relationships. 1827112117Sjhb */ 1828181695Sattilio pi = parent->w_index; 1829181695Sattilio ci = child->w_index; 1830181695Sattilio WITNESS_INDEX_ASSERT(pi); 1831181695Sattilio WITNESS_INDEX_ASSERT(ci); 1832181695Sattilio MPASS(pi != ci); 1833181695Sattilio w_rmatrix[pi][ci] |= WITNESS_PARENT; 1834181695Sattilio w_rmatrix[ci][pi] |= WITNESS_CHILD; 1835112117Sjhb 1836112117Sjhb /* 1837181695Sattilio * If parent was not already an ancestor of child, 1838181695Sattilio * then we increment the descendant and ancestor counters. 1839112117Sjhb */ 1840181695Sattilio if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) { 1841181695Sattilio parent->w_num_descendants++; 1842181695Sattilio child->w_num_ancestors++; 1843181695Sattilio } 1844112117Sjhb 1845181695Sattilio /* 1846181695Sattilio * Find each ancestor of 'pi'. Note that 'pi' itself is counted as 1847181695Sattilio * an ancestor of 'pi' during this loop. 1848181695Sattilio */ 1849181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 1850181695Sattilio if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 && 1851181695Sattilio (i != pi)) 1852181695Sattilio continue; 1853112117Sjhb 1854181695Sattilio /* Find each descendant of 'i' and mark it as a descendant. */ 1855181695Sattilio for (j = 1; j <= w_max_used_index; j++) { 185674912Sjhb 1857181695Sattilio /* 1858181695Sattilio * Skip children that are already marked as 1859181695Sattilio * descendants of 'i'. 1860181695Sattilio */ 1861181695Sattilio if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) 1862181695Sattilio continue; 1863181695Sattilio 1864181695Sattilio /* 1865181695Sattilio * We are only interested in descendants of 'ci'. Note 1866181695Sattilio * that 'ci' itself is counted as a descendant of 'ci'. 1867181695Sattilio */ 1868181695Sattilio if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 && 1869181695Sattilio (j != ci)) 1870181695Sattilio continue; 1871181695Sattilio w_rmatrix[i][j] |= WITNESS_ANCESTOR; 1872181695Sattilio w_rmatrix[j][i] |= WITNESS_DESCENDANT; 1873181695Sattilio w_data[i].w_num_descendants++; 1874181695Sattilio w_data[j].w_num_ancestors++; 1875181695Sattilio 1876181695Sattilio /* 1877181695Sattilio * Make sure we aren't marking a node as both an 1878181695Sattilio * ancestor and descendant. We should have caught 1879181695Sattilio * this as a lock order reversal earlier. 1880181695Sattilio */ 1881181695Sattilio if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) && 1882181695Sattilio (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) { 1883181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1884181695Sattilio "both ancestor and descendant\n", 1885181695Sattilio i, j, w_rmatrix[i][j]); 1886181695Sattilio kdb_backtrace(); 1887181695Sattilio printf("Witness disabled.\n"); 1888182446Sattilio witness_watch = -1; 1889181695Sattilio } 1890181695Sattilio if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) && 1891181695Sattilio (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) { 1892181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1893181695Sattilio "both ancestor and descendant\n", 1894181695Sattilio j, i, w_rmatrix[j][i]); 1895181695Sattilio kdb_backtrace(); 1896181695Sattilio printf("Witness disabled.\n"); 1897182446Sattilio witness_watch = -1; 1898181695Sattilio } 1899181695Sattilio } 190065557Sjasone } 1901112117Sjhb} 1902112117Sjhb 1903181695Sattiliostatic void 1904112117Sjhbitismychild(struct witness *parent, struct witness *child) 1905112117Sjhb{ 1906112117Sjhb 1907112117Sjhb MPASS(child != NULL && parent != NULL); 1908181695Sattilio if (witness_cold == 0) 1909181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1910181695Sattilio 1911181695Sattilio if (!witness_lock_type_equal(parent, child)) { 1912181695Sattilio if (witness_cold == 0) 1913181695Sattilio mtx_unlock_spin(&w_mtx); 1914181695Sattilio panic("%s: parent \"%s\" (%s) and child \"%s\" (%s) are not " 1915181695Sattilio "the same lock type", __func__, parent->w_name, 1916181695Sattilio parent->w_class->lc_name, child->w_name, 1917112117Sjhb child->w_class->lc_name); 1918181695Sattilio } 1919181695Sattilio adopt(parent, child); 192065557Sjasone} 192165557Sjasone 1922181695Sattilio/* 1923181695Sattilio * Generic code for the isitmy*() functions. The rmask parameter is the 1924181695Sattilio * expected relationship of w1 to w2. 1925181695Sattilio */ 1926181695Sattiliostatic int 1927181695Sattilio_isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname) 192865557Sjasone{ 1929181695Sattilio unsigned char r1, r2; 1930181695Sattilio int i1, i2; 193165557Sjasone 1932181695Sattilio i1 = w1->w_index; 1933181695Sattilio i2 = w2->w_index; 1934181695Sattilio WITNESS_INDEX_ASSERT(i1); 1935181695Sattilio WITNESS_INDEX_ASSERT(i2); 1936181695Sattilio r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK; 1937181695Sattilio r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK; 1938181695Sattilio 1939181695Sattilio /* The flags on one better be the inverse of the flags on the other */ 1940181695Sattilio if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) || 1941181695Sattilio (WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) { 1942181695Sattilio printf("%s: rmatrix mismatch between %s (index %d) and %s " 1943181695Sattilio "(index %d): w_rmatrix[%d][%d] == %hhx but " 1944181695Sattilio "w_rmatrix[%d][%d] == %hhx\n", 1945181695Sattilio fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1, 1946181695Sattilio i2, i1, r2); 1947181695Sattilio kdb_backtrace(); 1948181695Sattilio printf("Witness disabled.\n"); 1949182446Sattilio witness_watch = -1; 1950181695Sattilio } 1951181695Sattilio return (r1 & rmask); 195265557Sjasone} 195365557Sjasone 1954181695Sattilio/* 1955181695Sattilio * Checks if @child is a direct child of @parent. 1956181695Sattilio */ 195765557Sjasonestatic int 195865856Sjhbisitmychild(struct witness *parent, struct witness *child) 195965557Sjasone{ 196065557Sjasone 1961181695Sattilio return (_isitmyx(parent, child, WITNESS_PARENT, __func__)); 196265557Sjasone} 196365557Sjasone 1964181695Sattilio/* 1965181695Sattilio * Checks if @descendant is a direct or inderect descendant of @ancestor. 1966181695Sattilio */ 196765557Sjasonestatic int 1968181695Sattilioisitmydescendant(struct witness *ancestor, struct witness *descendant) 196965557Sjasone{ 197065557Sjasone 1971181695Sattilio return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK, 1972181695Sattilio __func__)); 197365557Sjasone} 197465557Sjasone 1975105508Sphk#ifdef BLESSING 197665557Sjasonestatic int 197765856Sjhbblessed(struct witness *w1, struct witness *w2) 197865557Sjasone{ 197965557Sjasone int i; 198065856Sjhb struct witness_blessed *b; 198165557Sjasone 198265557Sjasone for (i = 0; i < blessed_count; i++) { 198365557Sjasone b = &blessed_list[i]; 198474912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 198574912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 198665557Sjasone return (1); 198765557Sjasone continue; 198865557Sjasone } 198974912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 199074912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 199165557Sjasone return (1); 199265557Sjasone } 199365557Sjasone return (0); 199465557Sjasone} 1995105508Sphk#endif 199665557Sjasone 199765856Sjhbstatic struct witness * 199874912Sjhbwitness_get(void) 199965557Sjasone{ 200065856Sjhb struct witness *w; 2001181695Sattilio int index; 200265557Sjasone 2003181695Sattilio if (witness_cold == 0) 2004181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2005181695Sattilio 2006182473Sattilio if (witness_watch == -1) { 200776481Sjhb mtx_unlock_spin(&w_mtx); 200876481Sjhb return (NULL); 200976481Sjhb } 201074912Sjhb if (STAILQ_EMPTY(&w_free)) { 2011182446Sattilio witness_watch = -1; 201274912Sjhb mtx_unlock_spin(&w_mtx); 2013181695Sattilio printf("WITNESS: unable to allocate a new witness object\n"); 201465557Sjasone return (NULL); 201565557Sjasone } 201674912Sjhb w = STAILQ_FIRST(&w_free); 201774912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 2018149441Struckman w_free_cnt--; 2019181695Sattilio index = w->w_index; 2020181695Sattilio MPASS(index > 0 && index == w_max_used_index+1 && 2021181695Sattilio index < WITNESS_COUNT); 202265856Sjhb bzero(w, sizeof(*w)); 2023181695Sattilio w->w_index = index; 2024181695Sattilio if (index > w_max_used_index) 2025181695Sattilio w_max_used_index = index; 202665557Sjasone return (w); 202765557Sjasone} 202865557Sjasone 202965557Sjasonestatic void 203065856Sjhbwitness_free(struct witness *w) 203165557Sjasone{ 203274912Sjhb 203374912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 2034149441Struckman w_free_cnt++; 203565557Sjasone} 203665557Sjasone 203774912Sjhbstatic struct lock_list_entry * 203874912Sjhbwitness_lock_list_get(void) 203974912Sjhb{ 204074912Sjhb struct lock_list_entry *lle; 204171709Sjhb 2042182446Sattilio if (witness_watch == -1) 204376481Sjhb return (NULL); 204474912Sjhb mtx_lock_spin(&w_mtx); 204574912Sjhb lle = w_lock_list_free; 204674912Sjhb if (lle == NULL) { 2047182446Sattilio witness_watch = -1; 204874912Sjhb mtx_unlock_spin(&w_mtx); 204974912Sjhb printf("%s: witness exhausted\n", __func__); 205074912Sjhb return (NULL); 205174912Sjhb } 205274912Sjhb w_lock_list_free = lle->ll_next; 205374912Sjhb mtx_unlock_spin(&w_mtx); 205474912Sjhb bzero(lle, sizeof(*lle)); 205574912Sjhb return (lle); 205674912Sjhb} 205774912Sjhb 205874912Sjhbstatic void 205974912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 206071709Sjhb{ 206171709Sjhb 206274912Sjhb mtx_lock_spin(&w_mtx); 206374912Sjhb lle->ll_next = w_lock_list_free; 206474912Sjhb w_lock_list_free = lle; 206574912Sjhb mtx_unlock_spin(&w_mtx); 206671709Sjhb} 206771709Sjhb 206876272Sjhbstatic struct lock_instance * 2069181695Sattiliofind_instance(struct lock_list_entry *list, struct lock_object *lock) 207076272Sjhb{ 207176272Sjhb struct lock_list_entry *lle; 207276272Sjhb struct lock_instance *instance; 207376272Sjhb int i; 207476272Sjhb 2075181695Sattilio for (lle = list; lle != NULL; lle = lle->ll_next) 207676272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 207776272Sjhb instance = &lle->ll_children[i]; 207876272Sjhb if (instance->li_lock == lock) 207976272Sjhb return (instance); 208076272Sjhb } 208176272Sjhb return (NULL); 208276272Sjhb} 208376272Sjhb 2084111881Sjhbstatic void 2085207929Sattiliowitness_list_lock(struct lock_instance *instance, 2086207929Sattilio int (*prnt)(const char *fmt, ...)) 2087111881Sjhb{ 2088111881Sjhb struct lock_object *lock; 2089111881Sjhb 2090111881Sjhb lock = instance->li_lock; 2091207929Sattilio prnt("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 2092154077Sjhb "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 2093179025Sattilio if (lock->lo_witness->w_name != lock->lo_name) 2094207929Sattilio prnt(" (%s)", lock->lo_witness->w_name); 2095207929Sattilio prnt(" r = %d (%p) locked @ %s:%d\n", 2096234111Sadrian instance->li_flags & LI_RECURSEMASK, lock, 2097234111Sadrian fixup_filename(instance->li_file), instance->li_line); 2098111881Sjhb} 2099111881Sjhb 2100140637Srwatson#ifdef DDB 2101139333Srwatsonstatic int 2102139333Srwatsonwitness_thread_has_locks(struct thread *td) 2103139333Srwatson{ 2104139333Srwatson 2105182984Sattilio if (td->td_sleeplocks == NULL) 2106182984Sattilio return (0); 2107182984Sattilio return (td->td_sleeplocks->ll_count != 0); 2108139333Srwatson} 2109139333Srwatson 2110139333Srwatsonstatic int 2111139333Srwatsonwitness_proc_has_locks(struct proc *p) 2112139333Srwatson{ 2113139333Srwatson struct thread *td; 2114139333Srwatson 2115139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2116139333Srwatson if (witness_thread_has_locks(td)) 2117139333Srwatson return (1); 2118139333Srwatson } 2119139333Srwatson return (0); 2120139333Srwatson} 2121140637Srwatson#endif 2122139333Srwatson 212374912Sjhbint 2124207929Sattiliowitness_list_locks(struct lock_list_entry **lock_list, 2125207929Sattilio int (*prnt)(const char *fmt, ...)) 212672224Sjhb{ 212775273Sjhb struct lock_list_entry *lle; 212874912Sjhb int i, nheld; 212972224Sjhb 213074912Sjhb nheld = 0; 213174912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 213274912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 2133207929Sattilio witness_list_lock(&lle->ll_children[i], prnt); 213474912Sjhb nheld++; 213574912Sjhb } 213675273Sjhb return (nheld); 213775273Sjhb} 213875273Sjhb 2139118271Sjhb/* 2140118271Sjhb * This is a bit risky at best. We call this function when we have timed 2141118271Sjhb * out acquiring a spin lock, and we assume that the other CPU is stuck 2142118271Sjhb * with this lock held. So, we go groveling around in the other CPU's 2143118271Sjhb * per-cpu data to try to find the lock instance for this spin lock to 2144118271Sjhb * see when it was last acquired. 2145118271Sjhb */ 214665557Sjasonevoid 2147207929Sattiliowitness_display_spinlock(struct lock_object *lock, struct thread *owner, 2148207929Sattilio int (*prnt)(const char *fmt, ...)) 2149118271Sjhb{ 2150118271Sjhb struct lock_instance *instance; 2151118271Sjhb struct pcpu *pc; 2152118271Sjhb 2153118271Sjhb if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 2154118271Sjhb return; 2155118271Sjhb pc = pcpu_find(owner->td_oncpu); 2156118271Sjhb instance = find_instance(pc->pc_spinlocks, lock); 2157118271Sjhb if (instance != NULL) 2158207929Sattilio witness_list_lock(instance, prnt); 2159118271Sjhb} 2160118271Sjhb 2161118271Sjhbvoid 216274912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 216365557Sjasone{ 2164153854Sjhb struct lock_list_entry *lock_list; 216576272Sjhb struct lock_instance *instance; 2166154077Sjhb struct lock_class *class; 216771320Sjasone 2168235404Savg /* 2169235404Savg * This function is used independently in locking code to deal with 2170235404Savg * Giant, SCHEDULER_STOPPED() check can be removed here after Giant 2171235404Savg * is gone. 2172235404Savg */ 2173235404Savg if (SCHEDULER_STOPPED()) 2174235404Savg return; 2175181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2176182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 217771352Sjasone return; 2178154077Sjhb class = LOCK_CLASS(lock); 2179154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2180153854Sjhb lock_list = curthread->td_sleeplocks; 2181153854Sjhb else { 2182153854Sjhb if (witness_skipspin) 2183153854Sjhb return; 2184153854Sjhb lock_list = PCPU_GET(spinlocks); 2185153854Sjhb } 2186153854Sjhb instance = find_instance(lock_list, lock); 218782243Sjhb if (instance == NULL) 218882243Sjhb panic("%s: lock (%s) %s not locked", __func__, 2189154077Sjhb class->lc_name, lock->lo_name); 219076272Sjhb *filep = instance->li_file; 219176272Sjhb *linep = instance->li_line; 219265557Sjasone} 219365557Sjasone 219465557Sjasonevoid 219574912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 219665557Sjasone{ 2197153854Sjhb struct lock_list_entry *lock_list; 219876272Sjhb struct lock_instance *instance; 2199154077Sjhb struct lock_class *class; 220071320Sjasone 2201235404Savg /* 2202235404Savg * This function is used independently in locking code to deal with 2203235404Savg * Giant, SCHEDULER_STOPPED() check can be removed here after Giant 2204235404Savg * is gone. 2205235404Savg */ 2206235404Savg if (SCHEDULER_STOPPED()) 2207235404Savg return; 2208181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2209182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 221071352Sjasone return; 2211154077Sjhb class = LOCK_CLASS(lock); 2212154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2213153854Sjhb lock_list = curthread->td_sleeplocks; 2214153854Sjhb else { 2215153854Sjhb if (witness_skipspin) 2216153854Sjhb return; 2217153854Sjhb lock_list = PCPU_GET(spinlocks); 2218153854Sjhb } 2219153854Sjhb instance = find_instance(lock_list, lock); 222082243Sjhb if (instance == NULL) 222182243Sjhb panic("%s: lock (%s) %s not locked", __func__, 2222154077Sjhb class->lc_name, lock->lo_name); 222374912Sjhb lock->lo_witness->w_file = file; 222474912Sjhb lock->lo_witness->w_line = line; 222576272Sjhb instance->li_file = file; 222676272Sjhb instance->li_line = line; 222765557Sjasone} 222865557Sjasone 222978871Sjhbvoid 223078871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 223178871Sjhb{ 223278871Sjhb#ifdef INVARIANT_SUPPORT 223378871Sjhb struct lock_instance *instance; 2234154077Sjhb struct lock_class *class; 223578871Sjhb 2236182446Sattilio if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL) 223778941Sjhb return; 2238154077Sjhb class = LOCK_CLASS(lock); 2239154077Sjhb if ((class->lc_flags & LC_SLEEPLOCK) != 0) 224083366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 2241154077Sjhb else if ((class->lc_flags & LC_SPINLOCK) != 0) 224278871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 224386422Sjhb else { 224478871Sjhb panic("Lock (%s) %s is not sleep or spin!", 2245154077Sjhb class->lc_name, lock->lo_name); 224686422Sjhb } 224778871Sjhb switch (flags) { 224878871Sjhb case LA_UNLOCKED: 224978871Sjhb if (instance != NULL) 225078871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 2251234111Sadrian class->lc_name, lock->lo_name, 2252234111Sadrian fixup_filename(file), line); 225378871Sjhb break; 225478871Sjhb case LA_LOCKED: 225578871Sjhb case LA_LOCKED | LA_RECURSED: 225678871Sjhb case LA_LOCKED | LA_NOTRECURSED: 225778871Sjhb case LA_SLOCKED: 225878871Sjhb case LA_SLOCKED | LA_RECURSED: 225978871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 226078871Sjhb case LA_XLOCKED: 226178871Sjhb case LA_XLOCKED | LA_RECURSED: 226278871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 226386422Sjhb if (instance == NULL) { 226478871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 2265234111Sadrian class->lc_name, lock->lo_name, 2266234111Sadrian fixup_filename(file), line); 226786422Sjhb break; 226886422Sjhb } 226978871Sjhb if ((flags & LA_XLOCKED) != 0 && 227078871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 227178871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 2272234111Sadrian class->lc_name, lock->lo_name, 2273234111Sadrian fixup_filename(file), line); 227478871Sjhb if ((flags & LA_SLOCKED) != 0 && 227578871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 227678871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 2277234111Sadrian class->lc_name, lock->lo_name, 2278234111Sadrian fixup_filename(file), line); 227978871Sjhb if ((flags & LA_RECURSED) != 0 && 228078871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 228178871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 2282234111Sadrian class->lc_name, lock->lo_name, 2283234111Sadrian fixup_filename(file), line); 228478871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 228578871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 228678871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 2287234111Sadrian class->lc_name, lock->lo_name, 2288234111Sadrian fixup_filename(file), line); 228978871Sjhb break; 229078871Sjhb default: 2291234111Sadrian panic("Invalid lock assertion at %s:%d.", 2292234111Sadrian fixup_filename(file), line); 229378871Sjhb 229478871Sjhb } 229578871Sjhb#endif /* INVARIANT_SUPPORT */ 229678871Sjhb} 229778871Sjhb 2298187511Sthompsastatic void 2299187511Sthompsawitness_setflag(struct lock_object *lock, int flag, int set) 2300187511Sthompsa{ 2301187511Sthompsa struct lock_list_entry *lock_list; 2302187511Sthompsa struct lock_instance *instance; 2303187511Sthompsa struct lock_class *class; 2304187511Sthompsa 2305187511Sthompsa if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 2306187511Sthompsa return; 2307187511Sthompsa class = LOCK_CLASS(lock); 2308187511Sthompsa if (class->lc_flags & LC_SLEEPLOCK) 2309187511Sthompsa lock_list = curthread->td_sleeplocks; 2310187511Sthompsa else { 2311187511Sthompsa if (witness_skipspin) 2312187511Sthompsa return; 2313187511Sthompsa lock_list = PCPU_GET(spinlocks); 2314187511Sthompsa } 2315187511Sthompsa instance = find_instance(lock_list, lock); 2316187511Sthompsa if (instance == NULL) 2317187511Sthompsa panic("%s: lock (%s) %s not locked", __func__, 2318187511Sthompsa class->lc_name, lock->lo_name); 2319187511Sthompsa 2320187511Sthompsa if (set) 2321187511Sthompsa instance->li_flags |= flag; 2322187511Sthompsa else 2323187511Sthompsa instance->li_flags &= ~flag; 2324187511Sthompsa} 2325187511Sthompsa 2326187511Sthompsavoid 2327187511Sthompsawitness_norelease(struct lock_object *lock) 2328187511Sthompsa{ 2329187511Sthompsa 2330187511Sthompsa witness_setflag(lock, LI_NORELEASE, 1); 2331187511Sthompsa} 2332187511Sthompsa 2333187511Sthompsavoid 2334187511Sthompsawitness_releaseok(struct lock_object *lock) 2335187511Sthompsa{ 2336187511Sthompsa 2337187511Sthompsa witness_setflag(lock, LI_NORELEASE, 0); 2338187511Sthompsa} 2339187511Sthompsa 234074912Sjhb#ifdef DDB 2341112061Sjhbstatic void 2342181695Sattiliowitness_ddb_list(struct thread *td) 2343112061Sjhb{ 234474912Sjhb 2345181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2346131930Smarcel KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 2347112061Sjhb 2348182446Sattilio if (witness_watch < 1) 2349112061Sjhb return; 2350112061Sjhb 2351207929Sattilio witness_list_locks(&td->td_sleeplocks, db_printf); 2352112061Sjhb 2353112061Sjhb /* 2354112061Sjhb * We only handle spinlocks if td == curthread. This is somewhat broken 2355112061Sjhb * if td is currently executing on some other CPU and holds spin locks 2356112061Sjhb * as we won't display those locks. If we had a MI way of getting 2357112061Sjhb * the per-cpu data for a given cpu then we could use 2358113339Sjulian * td->td_oncpu to get the list of spinlocks for this thread 2359112061Sjhb * and "fix" this. 2360112061Sjhb * 2361170302Sjeff * That still wouldn't really fix this unless we locked the scheduler 2362170302Sjeff * lock or stopped the other CPU to make sure it wasn't changing the 2363170302Sjeff * list out from under us. It is probably best to just not try to 2364170302Sjeff * handle threads on other CPU's for now. 2365112061Sjhb */ 2366112061Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 2367207929Sattilio witness_list_locks(PCPU_PTR(spinlocks), db_printf); 2368112061Sjhb} 2369112061Sjhb 237074930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 237174912Sjhb{ 237283366Sjulian struct thread *td; 237374912Sjhb 2374158030Sjhb if (have_addr) 2375158030Sjhb td = db_lookup_thread(addr, TRUE); 2376158030Sjhb else 2377158030Sjhb td = kdb_thread; 2378181695Sattilio witness_ddb_list(td); 237974912Sjhb} 238074912Sjhb 2381183054SsamDB_SHOW_ALL_COMMAND(locks, db_witness_list_all) 2382139333Srwatson{ 2383139333Srwatson struct thread *td; 2384139333Srwatson struct proc *p; 2385139333Srwatson 2386139333Srwatson /* 2387139333Srwatson * It would be nice to list only threads and processes that actually 2388139333Srwatson * held sleep locks, but that information is currently not exported 2389139333Srwatson * by WITNESS. 2390139333Srwatson */ 2391139333Srwatson FOREACH_PROC_IN_SYSTEM(p) { 2392139333Srwatson if (!witness_proc_has_locks(p)) 2393139333Srwatson continue; 2394139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2395139333Srwatson if (!witness_thread_has_locks(td)) 2396139333Srwatson continue; 2397153853Sjhb db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2398182984Sattilio p->p_comm, td, td->td_tid); 2399181695Sattilio witness_ddb_list(td); 2400239874Sjhb if (db_pager_quit) 2401239874Sjhb return; 2402139333Srwatson } 2403139333Srwatson } 2404139333Srwatson} 2405183054SsamDB_SHOW_ALIAS(alllocks, db_witness_list_all) 2406139333Srwatson 240774912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 240874912Sjhb{ 240974912Sjhb 2410181695Sattilio witness_ddb_display(db_printf); 241174912Sjhb} 241274912Sjhb#endif 2413181695Sattilio 2414181695Sattiliostatic int 2415181695Sattiliosysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS) 2416181695Sattilio{ 2417181695Sattilio struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2; 2418181695Sattilio struct witness *tmp_w1, *tmp_w2, *w1, *w2; 2419181695Sattilio struct sbuf *sb; 2420181695Sattilio u_int w_rmatrix1, w_rmatrix2; 2421181695Sattilio int error, generation, i, j; 2422181695Sattilio 2423181695Sattilio tmp_data1 = NULL; 2424181695Sattilio tmp_data2 = NULL; 2425181695Sattilio tmp_w1 = NULL; 2426181695Sattilio tmp_w2 = NULL; 2427182446Sattilio if (witness_watch < 1) { 2428181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2429181695Sattilio return (error); 2430181695Sattilio } 2431181695Sattilio if (witness_cold) { 2432181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2433181695Sattilio return (error); 2434181695Sattilio } 2435181695Sattilio error = 0; 2436181695Sattilio sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND); 2437181695Sattilio if (sb == NULL) 2438181695Sattilio return (ENOMEM); 2439181695Sattilio 2440181695Sattilio /* Allocate and init temporary storage space. */ 2441181695Sattilio tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2442181695Sattilio tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2443181695Sattilio tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2444181695Sattilio M_WAITOK | M_ZERO); 2445181695Sattilio tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2446181695Sattilio M_WAITOK | M_ZERO); 2447181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2448181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2449181695Sattilio 2450181695Sattiliorestart: 2451181695Sattilio mtx_lock_spin(&w_mtx); 2452181695Sattilio generation = w_generation; 2453181695Sattilio mtx_unlock_spin(&w_mtx); 2454181695Sattilio sbuf_printf(sb, "Number of known direct relationships is %d\n", 2455181695Sattilio w_lohash.wloh_count); 2456181695Sattilio for (i = 1; i < w_max_used_index; i++) { 2457181695Sattilio mtx_lock_spin(&w_mtx); 2458181695Sattilio if (generation != w_generation) { 2459181695Sattilio mtx_unlock_spin(&w_mtx); 2460181695Sattilio 2461181695Sattilio /* The graph has changed, try again. */ 2462181695Sattilio req->oldidx = 0; 2463181695Sattilio sbuf_clear(sb); 2464181695Sattilio goto restart; 2465181695Sattilio } 2466181695Sattilio 2467181695Sattilio w1 = &w_data[i]; 2468181695Sattilio if (w1->w_reversed == 0) { 2469181695Sattilio mtx_unlock_spin(&w_mtx); 2470181695Sattilio continue; 2471181695Sattilio } 2472181695Sattilio 2473181695Sattilio /* Copy w1 locally so we can release the spin lock. */ 2474181695Sattilio *tmp_w1 = *w1; 2475181695Sattilio mtx_unlock_spin(&w_mtx); 2476181695Sattilio 2477181695Sattilio if (tmp_w1->w_reversed == 0) 2478181695Sattilio continue; 2479181695Sattilio for (j = 1; j < w_max_used_index; j++) { 2480181695Sattilio if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j) 2481181695Sattilio continue; 2482181695Sattilio 2483181695Sattilio mtx_lock_spin(&w_mtx); 2484181695Sattilio if (generation != w_generation) { 2485181695Sattilio mtx_unlock_spin(&w_mtx); 2486181695Sattilio 2487181695Sattilio /* The graph has changed, try again. */ 2488181695Sattilio req->oldidx = 0; 2489181695Sattilio sbuf_clear(sb); 2490181695Sattilio goto restart; 2491181695Sattilio } 2492181695Sattilio 2493181695Sattilio w2 = &w_data[j]; 2494181695Sattilio data1 = witness_lock_order_get(w1, w2); 2495181695Sattilio data2 = witness_lock_order_get(w2, w1); 2496181695Sattilio 2497181695Sattilio /* 2498181695Sattilio * Copy information locally so we can release the 2499181695Sattilio * spin lock. 2500181695Sattilio */ 2501181695Sattilio *tmp_w2 = *w2; 2502181695Sattilio w_rmatrix1 = (unsigned int)w_rmatrix[i][j]; 2503181695Sattilio w_rmatrix2 = (unsigned int)w_rmatrix[j][i]; 2504181695Sattilio 2505181695Sattilio if (data1) { 2506181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2507181695Sattilio stack_copy(&data1->wlod_stack, 2508181695Sattilio &tmp_data1->wlod_stack); 2509181695Sattilio } 2510181695Sattilio if (data2 && data2 != data1) { 2511181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2512181695Sattilio stack_copy(&data2->wlod_stack, 2513181695Sattilio &tmp_data2->wlod_stack); 2514181695Sattilio } 2515181695Sattilio mtx_unlock_spin(&w_mtx); 2516181695Sattilio 2517181695Sattilio sbuf_printf(sb, 2518181695Sattilio "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n", 2519181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2520181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2521181695Sattilio#if 0 2522181695Sattilio sbuf_printf(sb, 2523181695Sattilio "w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n", 2524181695Sattilio tmp_w1->name, tmp_w2->w_name, w_rmatrix1, 2525181695Sattilio tmp_w2->name, tmp_w1->w_name, w_rmatrix2); 2526181695Sattilio#endif 2527181695Sattilio if (data1) { 2528181695Sattilio sbuf_printf(sb, 2529181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2530181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2531181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2532181695Sattilio stack_sbuf_print(sb, &tmp_data1->wlod_stack); 2533181695Sattilio sbuf_printf(sb, "\n"); 2534181695Sattilio } 2535181695Sattilio if (data2 && data2 != data1) { 2536181695Sattilio sbuf_printf(sb, 2537181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2538181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name, 2539181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name); 2540181695Sattilio stack_sbuf_print(sb, &tmp_data2->wlod_stack); 2541181695Sattilio sbuf_printf(sb, "\n"); 2542181695Sattilio } 2543181695Sattilio } 2544181695Sattilio } 2545181695Sattilio mtx_lock_spin(&w_mtx); 2546181695Sattilio if (generation != w_generation) { 2547181695Sattilio mtx_unlock_spin(&w_mtx); 2548181695Sattilio 2549181695Sattilio /* 2550181695Sattilio * The graph changed while we were printing stack data, 2551181695Sattilio * try again. 2552181695Sattilio */ 2553181695Sattilio req->oldidx = 0; 2554181695Sattilio sbuf_clear(sb); 2555181695Sattilio goto restart; 2556181695Sattilio } 2557181695Sattilio mtx_unlock_spin(&w_mtx); 2558181695Sattilio 2559181695Sattilio /* Free temporary storage space. */ 2560181695Sattilio free(tmp_data1, M_TEMP); 2561181695Sattilio free(tmp_data2, M_TEMP); 2562181695Sattilio free(tmp_w1, M_TEMP); 2563181695Sattilio free(tmp_w2, M_TEMP); 2564181695Sattilio 2565181695Sattilio sbuf_finish(sb); 2566181695Sattilio error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2567181695Sattilio sbuf_delete(sb); 2568181695Sattilio 2569181695Sattilio return (error); 2570181695Sattilio} 2571181695Sattilio 2572181695Sattiliostatic int 2573181695Sattiliosysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS) 2574181695Sattilio{ 2575181695Sattilio struct witness *w; 2576181695Sattilio struct sbuf *sb; 2577181695Sattilio int error; 2578181695Sattilio 2579182446Sattilio if (witness_watch < 1) { 2580181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2581181695Sattilio return (error); 2582181695Sattilio } 2583181695Sattilio if (witness_cold) { 2584181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2585181695Sattilio return (error); 2586181695Sattilio } 2587181695Sattilio error = 0; 2588217916Smdf 2589217916Smdf error = sysctl_wire_old_buffer(req, 0); 2590217916Smdf if (error != 0) 2591217916Smdf return (error); 2592212750Smdf sb = sbuf_new_for_sysctl(NULL, NULL, FULLGRAPH_SBUF_SIZE, req); 2593181695Sattilio if (sb == NULL) 2594181695Sattilio return (ENOMEM); 2595181695Sattilio sbuf_printf(sb, "\n"); 2596181695Sattilio 2597181695Sattilio mtx_lock_spin(&w_mtx); 2598181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2599181695Sattilio w->w_displayed = 0; 2600181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2601181695Sattilio witness_add_fullgraph(sb, w); 2602181695Sattilio mtx_unlock_spin(&w_mtx); 2603181695Sattilio 2604181695Sattilio /* 2605181695Sattilio * Close the sbuf and return to userland. 2606181695Sattilio */ 2607212750Smdf error = sbuf_finish(sb); 2608181695Sattilio sbuf_delete(sb); 2609181695Sattilio 2610181695Sattilio return (error); 2611181695Sattilio} 2612181695Sattilio 2613181695Sattiliostatic int 2614181695Sattiliosysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 2615181695Sattilio{ 2616181695Sattilio int error, value; 2617181695Sattilio 2618182473Sattilio value = witness_watch; 2619181695Sattilio error = sysctl_handle_int(oidp, &value, 0, req); 2620181695Sattilio if (error != 0 || req->newptr == NULL) 2621181695Sattilio return (error); 2622182446Sattilio if (value > 1 || value < -1 || 2623182446Sattilio (witness_watch == -1 && value != witness_watch)) 2624181695Sattilio return (EINVAL); 2625182446Sattilio witness_watch = value; 2626181695Sattilio return (0); 2627181695Sattilio} 2628181695Sattilio 2629181695Sattiliostatic void 2630181695Sattiliowitness_add_fullgraph(struct sbuf *sb, struct witness *w) 2631181695Sattilio{ 2632181695Sattilio int i; 2633181695Sattilio 2634181695Sattilio if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0)) 2635181695Sattilio return; 2636181695Sattilio w->w_displayed = 1; 2637181695Sattilio 2638181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 2639181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 2640181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) { 2641181695Sattilio sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name, 2642181695Sattilio w_data[i].w_name); 2643181695Sattilio witness_add_fullgraph(sb, &w_data[i]); 2644181695Sattilio } 2645181695Sattilio } 2646181695Sattilio} 2647181695Sattilio 2648181695Sattilio/* 2649181695Sattilio * A simple hash function. Takes a key pointer and a key size. If size == 0, 2650181695Sattilio * interprets the key as a string and reads until the null 2651181695Sattilio * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit 2652181695Sattilio * hash value computed from the key. 2653181695Sattilio */ 2654181695Sattiliostatic uint32_t 2655181695Sattiliowitness_hash_djb2(const uint8_t *key, uint32_t size) 2656181695Sattilio{ 2657181695Sattilio unsigned int hash = 5381; 2658181695Sattilio int i; 2659181695Sattilio 2660181695Sattilio /* hash = hash * 33 + key[i] */ 2661181695Sattilio if (size) 2662181695Sattilio for (i = 0; i < size; i++) 2663181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2664181695Sattilio else 2665181695Sattilio for (i = 0; key[i] != 0; i++) 2666181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2667181695Sattilio 2668181695Sattilio return (hash); 2669181695Sattilio} 2670181695Sattilio 2671181695Sattilio 2672181695Sattilio/* 2673181695Sattilio * Initializes the two witness hash tables. Called exactly once from 2674181695Sattilio * witness_initialize(). 2675181695Sattilio */ 2676181695Sattiliostatic void 2677181695Sattiliowitness_init_hash_tables(void) 2678181695Sattilio{ 2679181695Sattilio int i; 2680181695Sattilio 2681181695Sattilio MPASS(witness_cold); 2682181695Sattilio 2683181695Sattilio /* Initialize the hash tables. */ 2684181695Sattilio for (i = 0; i < WITNESS_HASH_SIZE; i++) 2685181695Sattilio w_hash.wh_array[i] = NULL; 2686181695Sattilio 2687181695Sattilio w_hash.wh_size = WITNESS_HASH_SIZE; 2688181695Sattilio w_hash.wh_count = 0; 2689181695Sattilio 2690181695Sattilio /* Initialize the lock order data hash. */ 2691181695Sattilio w_lofree = NULL; 2692181695Sattilio for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) { 2693181695Sattilio memset(&w_lodata[i], 0, sizeof(w_lodata[i])); 2694181695Sattilio w_lodata[i].wlod_next = w_lofree; 2695181695Sattilio w_lofree = &w_lodata[i]; 2696181695Sattilio } 2697181695Sattilio w_lohash.wloh_size = WITNESS_LO_HASH_SIZE; 2698181695Sattilio w_lohash.wloh_count = 0; 2699181695Sattilio for (i = 0; i < WITNESS_LO_HASH_SIZE; i++) 2700181695Sattilio w_lohash.wloh_array[i] = NULL; 2701181695Sattilio} 2702181695Sattilio 2703181695Sattiliostatic struct witness * 2704181695Sattiliowitness_hash_get(const char *key) 2705181695Sattilio{ 2706181695Sattilio struct witness *w; 2707181695Sattilio uint32_t hash; 2708181695Sattilio 2709181695Sattilio MPASS(key != NULL); 2710181695Sattilio if (witness_cold == 0) 2711181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2712181695Sattilio hash = witness_hash_djb2(key, 0) % w_hash.wh_size; 2713181695Sattilio w = w_hash.wh_array[hash]; 2714181695Sattilio while (w != NULL) { 2715181695Sattilio if (strcmp(w->w_name, key) == 0) 2716181695Sattilio goto out; 2717181695Sattilio w = w->w_hash_next; 2718181695Sattilio } 2719181695Sattilio 2720181695Sattilioout: 2721181695Sattilio return (w); 2722181695Sattilio} 2723181695Sattilio 2724181695Sattiliostatic void 2725181695Sattiliowitness_hash_put(struct witness *w) 2726181695Sattilio{ 2727181695Sattilio uint32_t hash; 2728181695Sattilio 2729181695Sattilio MPASS(w != NULL); 2730181695Sattilio MPASS(w->w_name != NULL); 2731181695Sattilio if (witness_cold == 0) 2732181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2733181695Sattilio KASSERT(witness_hash_get(w->w_name) == NULL, 2734181695Sattilio ("%s: trying to add a hash entry that already exists!", __func__)); 2735181695Sattilio KASSERT(w->w_hash_next == NULL, 2736181695Sattilio ("%s: w->w_hash_next != NULL", __func__)); 2737181695Sattilio 2738181695Sattilio hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size; 2739181695Sattilio w->w_hash_next = w_hash.wh_array[hash]; 2740181695Sattilio w_hash.wh_array[hash] = w; 2741181695Sattilio w_hash.wh_count++; 2742181695Sattilio} 2743181695Sattilio 2744181695Sattilio 2745181695Sattiliostatic struct witness_lock_order_data * 2746181695Sattiliowitness_lock_order_get(struct witness *parent, struct witness *child) 2747181695Sattilio{ 2748181695Sattilio struct witness_lock_order_data *data = NULL; 2749181695Sattilio struct witness_lock_order_key key; 2750181695Sattilio unsigned int hash; 2751181695Sattilio 2752181695Sattilio MPASS(parent != NULL && child != NULL); 2753181695Sattilio key.from = parent->w_index; 2754181695Sattilio key.to = child->w_index; 2755181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2756181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2757181695Sattilio if ((w_rmatrix[parent->w_index][child->w_index] 2758181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) == 0) 2759181695Sattilio goto out; 2760181695Sattilio 2761181695Sattilio hash = witness_hash_djb2((const char*)&key, 2762181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2763181695Sattilio data = w_lohash.wloh_array[hash]; 2764181695Sattilio while (data != NULL) { 2765181695Sattilio if (witness_lock_order_key_equal(&data->wlod_key, &key)) 2766181695Sattilio break; 2767181695Sattilio data = data->wlod_next; 2768181695Sattilio } 2769181695Sattilio 2770181695Sattilioout: 2771181695Sattilio return (data); 2772181695Sattilio} 2773181695Sattilio 2774181695Sattilio/* 2775181695Sattilio * Verify that parent and child have a known relationship, are not the same, 2776181695Sattilio * and child is actually a child of parent. This is done without w_mtx 2777181695Sattilio * to avoid contention in the common case. 2778181695Sattilio */ 2779181695Sattiliostatic int 2780181695Sattiliowitness_lock_order_check(struct witness *parent, struct witness *child) 2781181695Sattilio{ 2782181695Sattilio 2783181695Sattilio if (parent != child && 2784181695Sattilio w_rmatrix[parent->w_index][child->w_index] 2785181695Sattilio & WITNESS_LOCK_ORDER_KNOWN && 2786181695Sattilio isitmychild(parent, child)) 2787181695Sattilio return (1); 2788181695Sattilio 2789181695Sattilio return (0); 2790181695Sattilio} 2791181695Sattilio 2792181695Sattiliostatic int 2793181695Sattiliowitness_lock_order_add(struct witness *parent, struct witness *child) 2794181695Sattilio{ 2795181695Sattilio struct witness_lock_order_data *data = NULL; 2796181695Sattilio struct witness_lock_order_key key; 2797181695Sattilio unsigned int hash; 2798181695Sattilio 2799181695Sattilio MPASS(parent != NULL && child != NULL); 2800181695Sattilio key.from = parent->w_index; 2801181695Sattilio key.to = child->w_index; 2802181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2803181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2804181695Sattilio if (w_rmatrix[parent->w_index][child->w_index] 2805181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) 2806181695Sattilio return (1); 2807181695Sattilio 2808181695Sattilio hash = witness_hash_djb2((const char*)&key, 2809181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2810181695Sattilio w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN; 2811181695Sattilio data = w_lofree; 2812181695Sattilio if (data == NULL) 2813181695Sattilio return (0); 2814181695Sattilio w_lofree = data->wlod_next; 2815181695Sattilio data->wlod_next = w_lohash.wloh_array[hash]; 2816181695Sattilio data->wlod_key = key; 2817181695Sattilio w_lohash.wloh_array[hash] = data; 2818181695Sattilio w_lohash.wloh_count++; 2819181695Sattilio stack_zero(&data->wlod_stack); 2820181695Sattilio stack_save(&data->wlod_stack); 2821181695Sattilio return (1); 2822181695Sattilio} 2823181695Sattilio 2824181695Sattilio/* Call this whenver the structure of the witness graph changes. */ 2825181695Sattiliostatic void 2826181695Sattiliowitness_increment_graph_generation(void) 2827181695Sattilio{ 2828181695Sattilio 2829181695Sattilio if (witness_cold == 0) 2830181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2831181695Sattilio w_generation++; 2832181695Sattilio} 2833181695Sattilio 2834181695Sattilio#ifdef KDB 2835181695Sattiliostatic void 2836181695Sattilio_witness_debugger(int cond, const char *msg) 2837181695Sattilio{ 2838181695Sattilio 2839181695Sattilio if (witness_trace && cond) 2840181695Sattilio kdb_backtrace(); 2841181695Sattilio if (witness_kdb && cond) 2842181695Sattilio kdb_enter(KDB_WHY_WITNESS, msg); 2843181695Sattilio} 2844181695Sattilio#endif 2845