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: stable/10/sys/kern/subr_witness.c 309871 2016-12-12 02:24:46Z markj $"); 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 */ 138270185Sgrehan#define WITNESS_PENDLIST (1024 + MAXCPU) 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 187227293Sedstatic 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, 328227588Spjd const 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 372227309Sedstatic SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, NULL, 373227309Sed "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 /* 494280309Skib * umtx 495280309Skib */ 496280309Skib { "umtx lock", &lock_class_mtx_sleep }, 497280309Skib { NULL, NULL }, 498280309Skib /* 499130022Srwatson * Sockets 500130022Srwatson */ 501130022Srwatson { "accept", &lock_class_mtx_sleep }, 502130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 503130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 504130022Srwatson { "sellck", &lock_class_mtx_sleep }, 505130022Srwatson { NULL, NULL }, 506130022Srwatson /* 507130022Srwatson * Routing 508130022Srwatson */ 509130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 510185747Skmacy { "radix node head", &lock_class_rw }, 511130022Srwatson { "rtentry", &lock_class_mtx_sleep }, 512130022Srwatson { "ifaddr", &lock_class_mtx_sleep }, 513130022Srwatson { NULL, NULL }, 514130022Srwatson /* 515191672Sbms * IPv4 multicast: 516191672Sbms * protocol locks before interface locks, after UDP locks. 517148682Srwatson */ 518178285Srwatson { "udpinp", &lock_class_rw }, 519148682Srwatson { "in_multi_mtx", &lock_class_mtx_sleep }, 520148682Srwatson { "igmp_mtx", &lock_class_mtx_sleep }, 521229873Sjhb { "if_addr_lock", &lock_class_rw }, 522148682Srwatson { NULL, NULL }, 523148682Srwatson /* 524191672Sbms * IPv6 multicast: 525191672Sbms * protocol locks before interface locks, after UDP locks. 526191672Sbms */ 527191672Sbms { "udpinp", &lock_class_rw }, 528191672Sbms { "in6_multi_mtx", &lock_class_mtx_sleep }, 529191672Sbms { "mld_mtx", &lock_class_mtx_sleep }, 530229873Sjhb { "if_addr_lock", &lock_class_rw }, 531191672Sbms { NULL, NULL }, 532191672Sbms /* 533130022Srwatson * UNIX Domain Sockets 534130396Srwatson */ 535274157Smarkj { "unp_link_rwlock", &lock_class_rw }, 536189544Srwatson { "unp_list_lock", &lock_class_mtx_sleep }, 537130396Srwatson { "unp", &lock_class_mtx_sleep }, 538130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 539130031Sjhb { NULL, NULL }, 540130022Srwatson /* 541130022Srwatson * UDP/IP 542130022Srwatson */ 543178285Srwatson { "udp", &lock_class_rw }, 544178285Srwatson { "udpinp", &lock_class_rw }, 545130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 546130022Srwatson { NULL, NULL }, 547130022Srwatson /* 548130022Srwatson * TCP/IP 549130022Srwatson */ 550178285Srwatson { "tcp", &lock_class_rw }, 551178285Srwatson { "tcpinp", &lock_class_rw }, 552130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 553130022Srwatson { NULL, NULL }, 554130022Srwatson /* 555132639Srwatson * netatalk 556132639Srwatson */ 557132639Srwatson { "ddp_list_mtx", &lock_class_mtx_sleep }, 558132639Srwatson { "ddp_mtx", &lock_class_mtx_sleep }, 559132639Srwatson { NULL, NULL }, 560132639Srwatson /* 561134971Srwatson * BPF 562134971Srwatson */ 563134971Srwatson { "bpf global lock", &lock_class_mtx_sleep }, 564233937Smelifaro { "bpf interface lock", &lock_class_rw }, 565235745Smelifaro { "bpf cdev lock", &lock_class_mtx_sleep }, 566144832Spjd { NULL, NULL }, 567143335Srwatson /* 568143335Srwatson * NFS server 569143335Srwatson */ 570143335Srwatson { "nfsd_mtx", &lock_class_mtx_sleep }, 571143335Srwatson { "so_snd", &lock_class_mtx_sleep }, 572134971Srwatson { NULL, NULL }, 573170530Ssam 574134971Srwatson /* 575170530Ssam * IEEE 802.11 576170530Ssam */ 577170530Ssam { "802.11 com lock", &lock_class_mtx_sleep}, 578170530Ssam { NULL, NULL }, 579170530Ssam /* 580170530Ssam * Network drivers 581170530Ssam */ 582170530Ssam { "network driver", &lock_class_mtx_sleep}, 583170530Ssam { NULL, NULL }, 584170530Ssam 585170530Ssam /* 586168217Swkoszek * Netgraph 587168217Swkoszek */ 588168217Swkoszek { "ng_node", &lock_class_mtx_sleep }, 589168217Swkoszek { "ng_worklist", &lock_class_mtx_sleep }, 590168217Swkoszek { NULL, NULL }, 591168217Swkoszek /* 592144836Spjd * CDEV 593144836Spjd */ 594237623Salc { "vm map (system)", &lock_class_mtx_sleep }, 595309871Smarkj { "vm pagequeue", &lock_class_mtx_sleep }, 596145425Sjeff { "vnode interlock", &lock_class_mtx_sleep }, 597144836Spjd { "cdev", &lock_class_mtx_sleep }, 598144836Spjd { NULL, NULL }, 599144836Spjd /* 600207410Skmacy * VM 601207410Skmacy */ 602237623Salc { "vm map (user)", &lock_class_sx }, 603248093Skib { "vm object", &lock_class_rw }, 604237623Salc { "vm page", &lock_class_mtx_sleep }, 605309871Smarkj { "vm pagequeue", &lock_class_mtx_sleep }, 606237623Salc { "pmap pv global", &lock_class_rw }, 607207410Skmacy { "pmap", &lock_class_mtx_sleep }, 608237623Salc { "pmap pv list", &lock_class_rw }, 609237623Salc { "vm page free queue", &lock_class_mtx_sleep }, 610207410Skmacy { NULL, NULL }, 611207410Skmacy /* 612166421Skib * kqueue/VFS interaction 613166421Skib */ 614166421Skib { "kqueue", &lock_class_mtx_sleep }, 615166421Skib { "struct mount mtx", &lock_class_mtx_sleep }, 616166421Skib { "vnode interlock", &lock_class_mtx_sleep }, 617166421Skib { NULL, NULL }, 618166421Skib /* 619192416Skmacy * ZFS locking 620192416Skmacy */ 621192416Skmacy { "dn->dn_mtx", &lock_class_sx }, 622192416Skmacy { "dr->dt.di.dr_mtx", &lock_class_sx }, 623192416Skmacy { "db->db_mtx", &lock_class_sx }, 624192416Skmacy { NULL, NULL }, 625192416Skmacy /* 62675464Sjhb * spin locks 62775464Sjhb */ 62884331Sjhb#ifdef SMP 62984331Sjhb { "ap boot", &lock_class_mtx_spin }, 63072224Sjhb#endif 631150582Sjhb { "rm.mutex_mtx", &lock_class_mtx_spin }, 63274912Sjhb { "sio", &lock_class_mtx_spin }, 633173877Sattilio { "scrlock", &lock_class_mtx_spin }, 63472224Sjhb#ifdef __i386__ 63574912Sjhb { "cy", &lock_class_mtx_spin }, 63672224Sjhb#endif 637170848Smarius#ifdef __sparc64__ 638170848Smarius { "pcib_mtx", &lock_class_mtx_spin }, 639170848Smarius { "rtc_mtx", &lock_class_mtx_spin }, 640170848Smarius#endif 641157584Smarcel { "scc_hwmtx", &lock_class_mtx_spin }, 642124972Sru { "uart_hwmtx", &lock_class_mtx_spin }, 643161638Sssouhlal { "fast_taskqueue", &lock_class_mtx_spin }, 644122001Sjhb { "intr table", &lock_class_mtx_spin }, 645168856Sjkoshy#ifdef HWPMC_HOOKS 646168856Sjkoshy { "pmc-per-proc", &lock_class_mtx_spin }, 647168856Sjkoshy#endif 648170302Sjeff { "process slock", &lock_class_mtx_spin }, 649126324Sjhb { "sleepq chain", &lock_class_mtx_spin }, 650173877Sattilio { "rm_spinlock", &lock_class_mtx_spin }, 651170302Sjeff { "turnstile chain", &lock_class_mtx_spin }, 652170302Sjeff { "turnstile lock", &lock_class_mtx_spin }, 65374912Sjhb { "sched lock", &lock_class_mtx_spin }, 654122514Sjhb { "td_contested", &lock_class_mtx_spin }, 65574912Sjhb { "callout", &lock_class_mtx_spin }, 656136374Srwatson { "entropy harvest mutex", &lock_class_mtx_spin }, 657162285Sscottl { "syscons video lock", &lock_class_mtx_spin }, 658172256Sattilio#ifdef SMP 659172256Sattilio { "smp rendezvous", &lock_class_mtx_spin }, 660172256Sattilio#endif 661176771Sraj#ifdef __powerpc__ 662176771Sraj { "tlb0", &lock_class_mtx_spin }, 663176771Sraj#endif 66465557Sjasone /* 66565557Sjasone * leaf locks 66665557Sjasone */ 667178149Sattilio { "intrcnt", &lock_class_mtx_spin }, 66888322Sjhb { "icu", &lock_class_mtx_spin }, 669286055Smarius#if defined(SMP) && defined(__sparc64__) 670286055Smarius { "ipi", &lock_class_mtx_spin }, 671286055Smarius#endif 672172256Sattilio#ifdef __i386__ 673172256Sattilio { "allpmaps", &lock_class_mtx_spin }, 674172256Sattilio { "descriptor tables", &lock_class_mtx_spin }, 675108187Sjake#endif 67678785Sjhb { "clk", &lock_class_mtx_spin }, 677178149Sattilio { "cpuset", &lock_class_mtx_spin }, 678172256Sattilio { "mprof lock", &lock_class_mtx_spin }, 679170302Sjeff { "zombie lock", &lock_class_mtx_spin }, 680103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 681104951Speter#ifdef __ia64__ 682104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 683104951Speter#endif 684115425Speter#if defined(__i386__) || defined(__amd64__) 685111068Speter { "pcicfg", &lock_class_mtx_spin }, 686143204Swpaul { "NDIS thread lock", &lock_class_mtx_spin }, 687111068Speter#endif 688144966Svkashyap { "tw_osl_io_lock", &lock_class_mtx_spin }, 689144966Svkashyap { "tw_osl_q_lock", &lock_class_mtx_spin }, 690144966Svkashyap { "tw_cl_io_lock", &lock_class_mtx_spin }, 691144966Svkashyap { "tw_cl_intr_lock", &lock_class_mtx_spin }, 692144966Svkashyap { "tw_cl_gen_lock", &lock_class_mtx_spin }, 693168856Sjkoshy#ifdef HWPMC_HOOKS 694168856Sjkoshy { "pmc-leaf", &lock_class_mtx_spin }, 695168856Sjkoshy#endif 696170302Sjeff { "blocked lock", &lock_class_mtx_spin }, 69774912Sjhb { NULL, NULL }, 69874912Sjhb { NULL, NULL } 69965557Sjasone}; 70065557Sjasone 701105508Sphk#ifdef BLESSING 70265557Sjasone/* 70365557Sjasone * Pairs of locks which have been blessed 70465557Sjasone * Don't complain about order problems with blessed locks 70565557Sjasone */ 70665856Sjhbstatic struct witness_blessed blessed_list[] = { 70765557Sjasone}; 70872200Sbmilekicstatic int blessed_count = 70972200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 710105508Sphk#endif 71165557Sjasone 71274912Sjhb/* 71374912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 71474912Sjhb */ 71574912Sjhbstatic int witness_cold = 1; 71674912Sjhb 71774912Sjhb/* 718151629Sjhb * This global is set to 1 once the static lock orders have been enrolled 719151629Sjhb * so that a warning can be issued for any spin locks enrolled later. 720151629Sjhb */ 721151629Sjhbstatic int witness_spin_warn = 0; 722151629Sjhb 723226793Sjhb/* Trim useless garbage from filenames. */ 724226793Sjhbstatic const char * 725226793Sjhbfixup_filename(const char *file) 726226793Sjhb{ 727226793Sjhb 728226793Sjhb if (file == NULL) 729226793Sjhb return (NULL); 730226793Sjhb while (strncmp(file, "../", 3) == 0) 731226793Sjhb file += 3; 732226793Sjhb return (file); 733226793Sjhb} 734226793Sjhb 735151629Sjhb/* 736153133Sjhb * The WITNESS-enabled diagnostic code. Note that the witness code does 737153133Sjhb * assume that the early boot is single-threaded at least until after this 738153133Sjhb * routine is completed. 73974912Sjhb */ 74071352Sjasonestatic void 74174912Sjhbwitness_initialize(void *dummy __unused) 74265557Sjasone{ 74374912Sjhb struct lock_object *lock; 74474912Sjhb struct witness_order_list_entry *order; 74574912Sjhb struct witness *w, *w1; 74674912Sjhb int i; 74765557Sjasone 748184214Sdes w_data = malloc(sizeof (struct witness) * WITNESS_COUNT, M_WITNESS, 749181695Sattilio M_NOWAIT | M_ZERO); 750181695Sattilio 75174912Sjhb /* 75274912Sjhb * We have to release Giant before initializing its witness 75374912Sjhb * structure so that WITNESS doesn't get confused. 75474912Sjhb */ 75574912Sjhb mtx_unlock(&Giant); 75674912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 75774912Sjhb 75887593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 75993811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 760164159Skmacy MTX_NOWITNESS | MTX_NOPROFILE); 761181695Sattilio for (i = WITNESS_COUNT - 1; i >= 0; i--) { 762181695Sattilio w = &w_data[i]; 763181695Sattilio memset(w, 0, sizeof(*w)); 764181695Sattilio w_data[i].w_index = i; /* Witness index never changes. */ 765181695Sattilio witness_free(w); 766181695Sattilio } 767181695Sattilio KASSERT(STAILQ_FIRST(&w_free)->w_index == 0, 768181695Sattilio ("%s: Invalid list of free witness objects", __func__)); 769181695Sattilio 770181695Sattilio /* Witness with index 0 is not used to aid in debugging. */ 771181695Sattilio STAILQ_REMOVE_HEAD(&w_free, w_list); 772181695Sattilio w_free_cnt--; 773181695Sattilio 774181695Sattilio memset(w_rmatrix, 0, 775181695Sattilio (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1))); 776181695Sattilio 77774912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 77874912Sjhb witness_lock_list_free(&w_locklistdata[i]); 779181695Sattilio witness_init_hash_tables(); 78074912Sjhb 78174912Sjhb /* First add in all the specified order lists. */ 78274912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 78374912Sjhb w = enroll(order->w_name, order->w_class); 78475569Sjhb if (w == NULL) 78575569Sjhb continue; 78674912Sjhb w->w_file = "order list"; 78774912Sjhb for (order++; order->w_name != NULL; order++) { 78874912Sjhb w1 = enroll(order->w_name, order->w_class); 78975569Sjhb if (w1 == NULL) 79075569Sjhb continue; 79174912Sjhb w1->w_file = "order list"; 792181695Sattilio itismychild(w, w1); 79374912Sjhb w = w1; 79465557Sjasone } 79565557Sjasone } 796151629Sjhb witness_spin_warn = 1; 79765557Sjasone 79874912Sjhb /* Iterate through all locks and add them to witness. */ 799179025Sattilio for (i = 0; pending_locks[i].wh_lock != NULL; i++) { 800179025Sattilio lock = pending_locks[i].wh_lock; 801153133Sjhb KASSERT(lock->lo_flags & LO_WITNESS, 802153133Sjhb ("%s: lock %s is on pending list but not LO_WITNESS", 803153133Sjhb __func__, lock->lo_name)); 804179025Sattilio lock->lo_witness = enroll(pending_locks[i].wh_type, 805179025Sattilio LOCK_CLASS(lock)); 80674912Sjhb } 80774912Sjhb 80874912Sjhb /* Mark the witness code as being ready for use. */ 809151629Sjhb witness_cold = 0; 81074912Sjhb 81174912Sjhb mtx_lock(&Giant); 81265557Sjasone} 813177253SrwatsonSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, 814177253Srwatson NULL); 81565557Sjasone 81674912Sjhbvoid 817179025Sattiliowitness_init(struct lock_object *lock, const char *type) 81874912Sjhb{ 81974912Sjhb struct lock_class *class; 82074912Sjhb 821153133Sjhb /* Various sanity checks. */ 822154077Sjhb class = LOCK_CLASS(lock); 82374912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 82474912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 825244105Salfred kassert_panic("%s: lock (%s) %s can not be recursable", 826244105Salfred __func__, class->lc_name, lock->lo_name); 82774912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 82874912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 829244105Salfred kassert_panic("%s: lock (%s) %s can not be sleepable", 830244105Salfred __func__, class->lc_name, lock->lo_name); 83182244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 83282244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 833244105Salfred kassert_panic("%s: lock (%s) %s can not be upgradable", 834244105Salfred __func__, class->lc_name, lock->lo_name); 83582244Sjhb 836153133Sjhb /* 837153133Sjhb * If we shouldn't watch this lock, then just clear lo_witness. 838153133Sjhb * Otherwise, if witness_cold is set, then it is too early to 839153133Sjhb * enroll this lock, so defer it to witness_initialize() by adding 840153133Sjhb * it to the pending_locks list. If it is not too early, then enroll 841153133Sjhb * the lock now. 842153133Sjhb */ 843182446Sattilio if (witness_watch < 1 || panicstr != NULL || 844153133Sjhb (lock->lo_flags & LO_WITNESS) == 0) 845153133Sjhb lock->lo_witness = NULL; 846153133Sjhb else if (witness_cold) { 847179025Sattilio pending_locks[pending_cnt].wh_lock = lock; 848179025Sattilio pending_locks[pending_cnt++].wh_type = type; 849179025Sattilio if (pending_cnt > WITNESS_PENDLIST) 850244105Salfred panic("%s: pending locks list is too small, " 851244105Salfred "increase WITNESS_PENDLIST\n", 852179025Sattilio __func__); 853153133Sjhb } else 854179025Sattilio lock->lo_witness = enroll(type, class); 85574912Sjhb} 85674912Sjhb 85774912Sjhbvoid 85874912Sjhbwitness_destroy(struct lock_object *lock) 85974912Sjhb{ 860154077Sjhb struct lock_class *class; 86175362Sjhb struct witness *w; 86274912Sjhb 863154077Sjhb class = LOCK_CLASS(lock); 864181695Sattilio 86574912Sjhb if (witness_cold) 86674912Sjhb panic("lock (%s) %s destroyed while witness_cold", 867154077Sjhb class->lc_name, lock->lo_name); 86874912Sjhb 86976272Sjhb /* XXX: need to verify that no one holds the lock */ 870181695Sattilio if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL) 871181695Sattilio return; 872181695Sattilio w = lock->lo_witness; 873112117Sjhb 874181695Sattilio mtx_lock_spin(&w_mtx); 875181695Sattilio MPASS(w->w_refcount > 0); 876181695Sattilio w->w_refcount--; 877181695Sattilio 878181695Sattilio if (w->w_refcount == 0) 879181695Sattilio depart(w); 880181695Sattilio mtx_unlock_spin(&w_mtx); 88174912Sjhb} 88274912Sjhb 883112115Sjhb#ifdef DDB 88471352Sjasonestatic void 885181695Sattiliowitness_ddb_compute_levels(void) 886149979Struckman{ 887181695Sattilio struct witness *w; 888149979Struckman 889149979Struckman /* 890149979Struckman * First clear all levels. 891149979Struckman */ 892181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 893181695Sattilio w->w_ddb_level = -1; 894149979Struckman 895149979Struckman /* 896181695Sattilio * Look for locks with no parents and level all their descendants. 897149979Struckman */ 898149979Struckman STAILQ_FOREACH(w, &w_all, w_list) { 899181695Sattilio 900181695Sattilio /* If the witness has ancestors (is not a root), skip it. */ 901181695Sattilio if (w->w_num_ancestors > 0) 902181695Sattilio continue; 903181695Sattilio witness_ddb_level_descendants(w, 0); 904149979Struckman } 905149979Struckman} 906149979Struckman 907149979Struckmanstatic void 908181695Sattiliowitness_ddb_level_descendants(struct witness *w, int l) 909149979Struckman{ 910149979Struckman int i; 911149979Struckman 912181695Sattilio if (w->w_ddb_level >= l) 913181695Sattilio return; 914181695Sattilio 915181695Sattilio w->w_ddb_level = l; 916181695Sattilio l++; 917181695Sattilio 918181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 919181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 920181695Sattilio witness_ddb_level_descendants(&w_data[i], l); 921181695Sattilio } 922149979Struckman} 923149979Struckman 924149979Struckmanstatic void 925207922Sattiliowitness_ddb_display_descendants(int(*prnt)(const char *fmt, ...), 926181695Sattilio struct witness *w, int indent) 927149979Struckman{ 928181695Sattilio int i; 929149979Struckman 930181695Sattilio for (i = 0; i < indent; i++) 931181695Sattilio prnt(" "); 932181695Sattilio prnt("%s (type: %s, depth: %d, active refs: %d)", 933181695Sattilio w->w_name, w->w_class->lc_name, 934181695Sattilio w->w_ddb_level, w->w_refcount); 935181695Sattilio if (w->w_displayed) { 936181695Sattilio prnt(" -- (already displayed)\n"); 937181695Sattilio return; 938181695Sattilio } 939181695Sattilio w->w_displayed = 1; 940181695Sattilio if (w->w_file != NULL && w->w_line != 0) 941226793Sjhb prnt(" -- last acquired @ %s:%d\n", fixup_filename(w->w_file), 942181695Sattilio w->w_line); 943149979Struckman else 944181695Sattilio prnt(" -- never acquired\n"); 945181695Sattilio indent++; 946181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 947181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 948239584Sjhb if (db_pager_quit) 949239584Sjhb return; 950181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 951181695Sattilio witness_ddb_display_descendants(prnt, &w_data[i], 952181695Sattilio indent); 953149979Struckman } 954149979Struckman} 955149979Struckman 956149979Struckmanstatic void 957207922Sattiliowitness_ddb_display_list(int(*prnt)(const char *fmt, ...), 958181695Sattilio struct witness_list *list) 95971352Sjasone{ 960112118Sjhb struct witness *w; 96171352Sjasone 96274912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 963181695Sattilio if (w->w_file == NULL || w->w_ddb_level > 0) 96471352Sjasone continue; 965181695Sattilio 966181695Sattilio /* This lock has no anscestors - display its descendants. */ 967181695Sattilio witness_ddb_display_descendants(prnt, w, 0); 968239584Sjhb if (db_pager_quit) 969239584Sjhb return; 97071352Sjasone } 97174912Sjhb} 97272224Sjhb 97374912Sjhbstatic void 974207922Sattiliowitness_ddb_display(int(*prnt)(const char *fmt, ...)) 975178841Sattilio{ 97674912Sjhb struct witness *w; 97774912Sjhb 978181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 979181695Sattilio witness_ddb_compute_levels(); 98074912Sjhb 981112118Sjhb /* Clear all the displayed flags. */ 982181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 983112118Sjhb w->w_displayed = 0; 984112118Sjhb 98572224Sjhb /* 98674930Sjhb * First, handle sleep locks which have been acquired at least 98774912Sjhb * once. 98874912Sjhb */ 98974912Sjhb prnt("Sleep locks:\n"); 990181695Sattilio witness_ddb_display_list(prnt, &w_sleep); 991239584Sjhb if (db_pager_quit) 992239584Sjhb return; 99374912Sjhb 99474912Sjhb /* 99574930Sjhb * Now do spin locks which have been acquired at least once. 99672224Sjhb */ 99774912Sjhb prnt("\nSpin locks:\n"); 998181695Sattilio witness_ddb_display_list(prnt, &w_spin); 999239584Sjhb if (db_pager_quit) 1000239584Sjhb return; 100172224Sjhb 100272224Sjhb /* 100374930Sjhb * Finally, any locks which have not been acquired yet. 100472224Sjhb */ 100574912Sjhb prnt("\nLocks which were never acquired:\n"); 100674912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 100797948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 100871352Sjasone continue; 1009181695Sattilio prnt("%s (type: %s, depth: %d)\n", w->w_name, 1010181695Sattilio w->w_class->lc_name, w->w_ddb_level); 1011239584Sjhb if (db_pager_quit) 1012239584Sjhb return; 101371352Sjasone } 101471352Sjasone} 1015112115Sjhb#endif /* DDB */ 101671352Sjasone 1017125160Sjhbint 1018125160Sjhbwitness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 1019125160Sjhb{ 1020125160Sjhb 1021182473Sattilio if (witness_watch == -1 || panicstr != NULL) 1022125160Sjhb return (0); 1023125160Sjhb 1024125160Sjhb /* Require locks that witness knows about. */ 1025125160Sjhb if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 1026125160Sjhb lock2->lo_witness == NULL) 1027125160Sjhb return (EINVAL); 1028125160Sjhb 1029181695Sattilio mtx_assert(&w_mtx, MA_NOTOWNED); 1030125160Sjhb mtx_lock_spin(&w_mtx); 1031125160Sjhb 1032125160Sjhb /* 1033125160Sjhb * If we already have either an explicit or implied lock order that 1034125160Sjhb * is the other way around, then return an error. 1035125160Sjhb */ 1036182473Sattilio if (witness_watch && 1037182473Sattilio isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 1038125160Sjhb mtx_unlock_spin(&w_mtx); 1039125160Sjhb return (EDOOFUS); 1040125160Sjhb } 1041125160Sjhb 1042125160Sjhb /* Try to add the new order. */ 1043125160Sjhb CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1044179025Sattilio lock2->lo_witness->w_name, lock1->lo_witness->w_name); 1045181695Sattilio itismychild(lock1->lo_witness, lock2->lo_witness); 1046125160Sjhb mtx_unlock_spin(&w_mtx); 1047125160Sjhb return (0); 1048125160Sjhb} 1049125160Sjhb 105065557Sjasonevoid 1051125160Sjhbwitness_checkorder(struct lock_object *lock, int flags, const char *file, 1052182914Sjhb int line, struct lock_object *interlock) 105365557Sjasone{ 1054183955Sattilio struct lock_list_entry *lock_list, *lle; 1055182914Sjhb struct lock_instance *lock1, *lock2, *plock; 1056251326Sjhb struct lock_class *class, *iclass; 105765856Sjhb struct witness *w, *w1; 105883366Sjulian struct thread *td; 105974912Sjhb int i, j; 106065557Sjasone 1061182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL || 106280747Sjhb panicstr != NULL) 106371320Sjasone return; 1064125160Sjhb 106574912Sjhb w = lock->lo_witness; 1066154077Sjhb class = LOCK_CLASS(lock); 106783366Sjulian td = curthread; 106865557Sjasone 106974912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 1070181695Sattilio 107193676Sjhb /* 107293676Sjhb * Since spin locks include a critical section, this check 1073131884Sjhb * implicitly enforces a lock order of all sleep locks before 107493676Sjhb * all spin locks. 107593676Sjhb */ 1076136304Sgreen if (td->td_critnest != 0 && !kdb_active) 1077244105Salfred kassert_panic("acquiring blockable sleep lock with " 1078244105Salfred "spinlock or critical section held (%s) %s @ %s:%d", 1079226294Sadrian class->lc_name, lock->lo_name, 1080226294Sadrian fixup_filename(file), line); 1081131884Sjhb 1082131884Sjhb /* 1083131884Sjhb * If this is the first lock acquired then just return as 1084131884Sjhb * no order checking is needed. 1085131884Sjhb */ 1086183955Sattilio lock_list = td->td_sleeplocks; 1087183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) 1088131884Sjhb return; 1089131884Sjhb } else { 1090181695Sattilio 1091131884Sjhb /* 1092131884Sjhb * If this is the first lock, just return as no order 1093183955Sattilio * checking is needed. Avoid problems with thread 1094183955Sattilio * migration pinning the thread while checking if 1095183955Sattilio * spinlocks are held. If at least one spinlock is held 1096183955Sattilio * the thread is in a safe path and it is allowed to 1097183955Sattilio * unpin it. 1098131884Sjhb */ 1099183955Sattilio sched_pin(); 1100183955Sattilio lock_list = PCPU_GET(spinlocks); 1101183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) { 1102183955Sattilio sched_unpin(); 1103131884Sjhb return; 1104183955Sattilio } 1105183955Sattilio sched_unpin(); 1106131884Sjhb } 110765557Sjasone 110876772Sjhb /* 1109125160Sjhb * Check to see if we are recursing on a lock we already own. If 1110125160Sjhb * so, make sure that we don't mismatch exclusive and shared lock 1111125160Sjhb * acquires. 111276272Sjhb */ 1113183955Sattilio lock1 = find_instance(lock_list, lock); 111476272Sjhb if (lock1 != NULL) { 111576272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 111676272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 111776272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 1118226294Sadrian class->lc_name, lock->lo_name, 1119226294Sadrian fixup_filename(file), line); 112076272Sjhb printf("while exclusively locked from %s:%d\n", 1121226294Sadrian fixup_filename(lock1->li_file), lock1->li_line); 1122251326Sjhb kassert_panic("excl->share"); 112376272Sjhb } 112476272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 112576272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 112676272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 1127226294Sadrian class->lc_name, lock->lo_name, 1128226294Sadrian fixup_filename(file), line); 112976272Sjhb printf("while share locked from %s:%d\n", 1130226294Sadrian fixup_filename(lock1->li_file), lock1->li_line); 1131251326Sjhb kassert_panic("share->excl"); 113276272Sjhb } 113376272Sjhb return; 113476272Sjhb } 113576272Sjhb 1136251326Sjhb /* Warn if the interlock is not locked exactly once. */ 1137251326Sjhb if (interlock != NULL) { 1138251326Sjhb iclass = LOCK_CLASS(interlock); 1139251326Sjhb lock1 = find_instance(lock_list, interlock); 1140251326Sjhb if (lock1 == NULL) 1141255205Sjhb kassert_panic("interlock (%s) %s not locked @ %s:%d", 1142251326Sjhb iclass->lc_name, interlock->lo_name, 1143251326Sjhb fixup_filename(file), line); 1144251326Sjhb else if ((lock1->li_flags & LI_RECURSEMASK) != 0) 1145255205Sjhb kassert_panic("interlock (%s) %s recursed @ %s:%d", 1146251326Sjhb iclass->lc_name, interlock->lo_name, 1147251326Sjhb fixup_filename(file), line); 1148251326Sjhb } 1149251326Sjhb 115076272Sjhb /* 1151182914Sjhb * Find the previously acquired lock, but ignore interlocks. 1152182914Sjhb */ 1153183955Sattilio plock = &lock_list->ll_children[lock_list->ll_count - 1]; 1154182914Sjhb if (interlock != NULL && plock->li_lock == interlock) { 1155183955Sattilio if (lock_list->ll_count > 1) 1156183955Sattilio plock = 1157183955Sattilio &lock_list->ll_children[lock_list->ll_count - 2]; 1158183955Sattilio else { 1159183955Sattilio lle = lock_list->ll_next; 1160182984Sattilio 1161182914Sjhb /* 1162182914Sjhb * The interlock is the only lock we hold, so 1163183955Sattilio * simply return. 1164182914Sjhb */ 1165183955Sattilio if (lle == NULL) 1166183955Sattilio return; 1167183955Sattilio plock = &lle->ll_children[lle->ll_count - 1]; 1168182914Sjhb } 1169182914Sjhb } 1170182914Sjhb 1171182914Sjhb /* 1172181695Sattilio * Try to perform most checks without a lock. If this succeeds we 1173284653Smarkj * can skip acquiring the lock and return success. Otherwise we redo 1174284653Smarkj * the check with the lock held to handle races with concurrent updates. 1175181695Sattilio */ 1176182914Sjhb w1 = plock->li_lock->lo_witness; 1177181695Sattilio if (witness_lock_order_check(w1, w)) 1178181695Sattilio return; 1179181695Sattilio 1180284653Smarkj mtx_lock_spin(&w_mtx); 1181284653Smarkj if (witness_lock_order_check(w1, w)) { 1182284653Smarkj mtx_unlock_spin(&w_mtx); 1183284653Smarkj return; 1184284653Smarkj } 1185284653Smarkj witness_lock_order_add(w1, w); 1186284653Smarkj 1187181695Sattilio /* 118874912Sjhb * Check for duplicate locks of the same type. Note that we only 118974912Sjhb * have to check for this on the last lock we just acquired. Any 119074912Sjhb * other cases will be caught as lock order violations. 119174912Sjhb */ 119274912Sjhb if (w1 == w) { 1193181695Sattilio i = w->w_index; 1194181695Sattilio if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) && 1195181695Sattilio !(w_rmatrix[i][i] & WITNESS_REVERSAL)) { 1196181695Sattilio w_rmatrix[i][i] |= WITNESS_REVERSAL; 1197181695Sattilio w->w_reversed = 1; 1198181695Sattilio mtx_unlock_spin(&w_mtx); 1199183574Sjhb printf( 1200183574Sjhb "acquiring duplicate lock of same type: \"%s\"\n", 1201181695Sattilio w->w_name); 1202183574Sjhb printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name, 1203226793Sjhb fixup_filename(plock->li_file), plock->li_line); 1204226294Sadrian printf(" 2nd %s @ %s:%d\n", lock->lo_name, 1205226294Sadrian fixup_filename(file), line); 1206181695Sattilio witness_debugger(1); 1207226793Sjhb } else 1208226793Sjhb mtx_unlock_spin(&w_mtx); 1209125160Sjhb return; 121065557Sjasone } 1211181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1212181695Sattilio 121365557Sjasone /* 1214218909Sbrucec * If we know that the lock we are acquiring comes after 1215111881Sjhb * the lock we most recently acquired in the lock order tree, 1216111881Sjhb * then there is no need for any further checks. 1217111881Sjhb */ 1218181695Sattilio if (isitmychild(w1, w)) 1219181695Sattilio goto out; 1220181695Sattilio 1221183955Sattilio for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) { 122274912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 122365557Sjasone 1224287231Smarkj MPASS(j < LOCK_CHILDCOUNT * LOCK_NCHILDREN); 122576272Sjhb lock1 = &lle->ll_children[i]; 122674912Sjhb 122774912Sjhb /* 1228251326Sjhb * Ignore the interlock. 1229182914Sjhb */ 1230251326Sjhb if (interlock == lock1->li_lock) 1231182914Sjhb continue; 1232182914Sjhb 1233182914Sjhb /* 123474912Sjhb * If this lock doesn't undergo witness checking, 123574912Sjhb * then skip it. 123674912Sjhb */ 1237182914Sjhb w1 = lock1->li_lock->lo_witness; 123874912Sjhb if (w1 == NULL) { 123976272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 124074912Sjhb ("lock missing witness structure")); 124174912Sjhb continue; 124274912Sjhb } 1243181695Sattilio 124476272Sjhb /* 1245111881Sjhb * If we are locking Giant and this is a sleepable 124676272Sjhb * lock, then skip it. 124776272Sjhb */ 1248111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 1249167787Sjhb lock == &Giant.lock_object) 125076272Sjhb continue; 1251181695Sattilio 125293690Sjhb /* 125393690Sjhb * If we are locking a sleepable lock and this lock 1254111881Sjhb * is Giant, then skip it. 125593690Sjhb */ 1256111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 1257167787Sjhb lock1->li_lock == &Giant.lock_object) 1258111881Sjhb continue; 1259181695Sattilio 1260111881Sjhb /* 1261111881Sjhb * If we are locking a sleepable lock and this lock 1262111881Sjhb * isn't sleepable, we want to treat it as a lock 1263111881Sjhb * order violation to enfore a general lock order of 1264111881Sjhb * sleepable locks before non-sleepable locks. 1265111881Sjhb */ 1266149738Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1267111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1268149738Sjhb goto reversal; 1269181695Sattilio 1270149738Sjhb /* 1271150179Sjhb * If we are locking Giant and this is a non-sleepable 1272150179Sjhb * lock, then treat it as a reversal. 1273150179Sjhb */ 1274150179Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 1275167787Sjhb lock == &Giant.lock_object) 1276150179Sjhb goto reversal; 1277181695Sattilio 1278150179Sjhb /* 1279149738Sjhb * Check the lock order hierarchy for a reveresal. 1280149738Sjhb */ 1281149738Sjhb if (!isitmydescendant(w, w1)) 128274912Sjhb continue; 1283149738Sjhb reversal: 1284181695Sattilio 128574912Sjhb /* 128674912Sjhb * We have a lock order violation, check to see if it 128774912Sjhb * is allowed or has already been yelled about. 128874912Sjhb */ 1289105508Sphk#ifdef BLESSING 1290181695Sattilio 1291125160Sjhb /* 1292125160Sjhb * If the lock order is blessed, just bail. We don't 1293125160Sjhb * look for other lock order violations though, which 1294125160Sjhb * may be a bug. 1295125160Sjhb */ 129665557Sjasone if (blessed(w, w1)) 1297181695Sattilio goto out; 1298105508Sphk#endif 1299181695Sattilio 1300181695Sattilio /* Bail if this violation is known */ 1301181695Sattilio if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL) 1302181695Sattilio goto out; 1303181695Sattilio 1304181695Sattilio /* Record this as a violation */ 1305181695Sattilio w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL; 1306181695Sattilio w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL; 1307181695Sattilio w->w_reversed = w1->w_reversed = 1; 1308181695Sattilio witness_increment_graph_generation(); 1309181695Sattilio mtx_unlock_spin(&w_mtx); 1310250411Smarcel 1311250411Smarcel#ifdef WITNESS_NO_VNODE 131274912Sjhb /* 1313250411Smarcel * There are known LORs between VNODE locks. They are 1314250411Smarcel * not an indication of a bug. VNODE locks are flagged 1315250411Smarcel * as such (LO_IS_VNODE) and we don't yell if the LOR 1316250411Smarcel * is between 2 VNODE locks. 1317250411Smarcel */ 1318250411Smarcel if ((lock->lo_flags & LO_IS_VNODE) != 0 && 1319250411Smarcel (lock1->li_lock->lo_flags & LO_IS_VNODE) != 0) 1320250411Smarcel return; 1321250411Smarcel#endif 1322250411Smarcel 1323250411Smarcel /* 132474912Sjhb * Ok, yell about it. 132574912Sjhb */ 1326150179Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1327150179Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1328150179Sjhb printf( 1329150179Sjhb "lock order reversal: (sleepable after non-sleepable)\n"); 1330150179Sjhb else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1331167787Sjhb && lock == &Giant.lock_object) 1332150179Sjhb printf( 1333150179Sjhb "lock order reversal: (Giant after non-sleepable)\n"); 1334150179Sjhb else 1335150179Sjhb printf("lock order reversal:\n"); 1336181695Sattilio 133774912Sjhb /* 133874912Sjhb * Try to locate an earlier lock with 133974912Sjhb * witness w in our list. 134074912Sjhb */ 134174912Sjhb do { 134276272Sjhb lock2 = &lle->ll_children[i]; 134376272Sjhb MPASS(lock2->li_lock != NULL); 134476272Sjhb if (lock2->li_lock->lo_witness == w) 134574912Sjhb break; 134674912Sjhb if (i == 0 && lle->ll_next != NULL) { 134774912Sjhb lle = lle->ll_next; 134874912Sjhb i = lle->ll_count - 1; 1349106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 1350125160Sjhb } else 1351125160Sjhb i--; 135274912Sjhb } while (i >= 0); 135376272Sjhb if (i < 0) { 135493811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 135593811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 1356226294Sadrian w1->w_name, fixup_filename(lock1->li_file), 1357226294Sadrian lock1->li_line); 135893811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 1359226294Sadrian lock->lo_name, w->w_name, 1360226294Sadrian fixup_filename(file), line); 136176272Sjhb } else { 136293811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 136393811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 1364179025Sattilio lock2->li_lock->lo_witness->w_name, 1365226294Sadrian fixup_filename(lock2->li_file), 1366226294Sadrian lock2->li_line); 136793811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 136893811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 1369226294Sadrian w1->w_name, fixup_filename(lock1->li_file), 1370226294Sadrian lock1->li_line); 137193811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 1372226294Sadrian lock->lo_name, w->w_name, 1373226294Sadrian fixup_filename(file), line); 137476272Sjhb } 1375181695Sattilio witness_debugger(1); 1376125160Sjhb return; 137765557Sjasone } 137865557Sjasone } 1379181695Sattilio 138078871Sjhb /* 1381125160Sjhb * If requested, build a new lock order. However, don't build a new 1382125160Sjhb * relationship between a sleepable lock and Giant if it is in the 1383125160Sjhb * wrong direction. The correct lock order is that sleepable locks 1384125160Sjhb * always come before Giant. 138578871Sjhb */ 1386125160Sjhb if (flags & LOP_NEWORDER && 1387182914Sjhb !(plock->li_lock == &Giant.lock_object && 1388112117Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0)) { 138987593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1390182914Sjhb w->w_name, plock->li_lock->lo_witness->w_name); 1391182914Sjhb itismychild(plock->li_lock->lo_witness, w); 1392181695Sattilio } 1393181695Sattilioout: 1394112117Sjhb mtx_unlock_spin(&w_mtx); 1395125160Sjhb} 1396125160Sjhb 1397125160Sjhbvoid 1398125160Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 1399125160Sjhb{ 1400125160Sjhb struct lock_list_entry **lock_list, *lle; 1401125160Sjhb struct lock_instance *instance; 1402125160Sjhb struct witness *w; 1403125160Sjhb struct thread *td; 1404125160Sjhb 1405182446Sattilio if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL || 1406125160Sjhb panicstr != NULL) 1407125160Sjhb return; 1408125160Sjhb w = lock->lo_witness; 1409125160Sjhb td = curthread; 1410125160Sjhb 1411125160Sjhb /* Determine lock list for this lock. */ 1412154077Sjhb if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1413125160Sjhb lock_list = &td->td_sleeplocks; 1414125160Sjhb else 1415125160Sjhb lock_list = PCPU_PTR(spinlocks); 1416125160Sjhb 1417125160Sjhb /* Check to see if we are recursing on a lock we already own. */ 1418125160Sjhb instance = find_instance(*lock_list, lock); 1419125160Sjhb if (instance != NULL) { 1420125160Sjhb instance->li_flags++; 1421125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1422125160Sjhb td->td_proc->p_pid, lock->lo_name, 1423125160Sjhb instance->li_flags & LI_RECURSEMASK); 1424125160Sjhb instance->li_file = file; 1425125160Sjhb instance->li_line = line; 1426125160Sjhb return; 1427110779Speter } 1428125160Sjhb 1429125160Sjhb /* Update per-witness last file and line acquire. */ 143065557Sjasone w->w_file = file; 143165557Sjasone w->w_line = line; 1432125160Sjhb 1433125160Sjhb /* Find the next open lock instance in the list and fill it. */ 143474912Sjhb lle = *lock_list; 143576272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 143678785Sjhb lle = witness_lock_list_get(); 143778785Sjhb if (lle == NULL) 143865557Sjasone return; 143978785Sjhb lle->ll_next = *lock_list; 144087593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 144184680Sjhb td->td_proc->p_pid, lle); 144278785Sjhb *lock_list = lle; 144365557Sjasone } 1444125160Sjhb instance = &lle->ll_children[lle->ll_count++]; 1445125160Sjhb instance->li_lock = lock; 1446125160Sjhb instance->li_line = line; 1447125160Sjhb instance->li_file = file; 144876272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 1449125160Sjhb instance->li_flags = LI_EXCLUSIVE; 145076272Sjhb else 1451125160Sjhb instance->li_flags = 0; 145287593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 145384680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 145465557Sjasone} 145565557Sjasone 145665557Sjasonevoid 145782244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 145882244Sjhb{ 145982244Sjhb struct lock_instance *instance; 146082244Sjhb struct lock_class *class; 146182244Sjhb 1462181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1463182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 146482244Sjhb return; 1465154077Sjhb class = LOCK_CLASS(lock); 1466182473Sattilio if (witness_watch) { 1467182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1468244105Salfred kassert_panic( 1469244105Salfred "upgrade of non-upgradable lock (%s) %s @ %s:%d", 1470226294Sadrian class->lc_name, lock->lo_name, 1471226294Sadrian fixup_filename(file), line); 1472182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1473244105Salfred kassert_panic( 1474244105Salfred "upgrade of non-sleep lock (%s) %s @ %s:%d", 1475226294Sadrian class->lc_name, lock->lo_name, 1476226294Sadrian fixup_filename(file), line); 1477182473Sattilio } 147883366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 1479244112Salfred if (instance == NULL) { 1480244105Salfred kassert_panic("upgrade of unlocked lock (%s) %s @ %s:%d", 1481226294Sadrian class->lc_name, lock->lo_name, 1482226294Sadrian fixup_filename(file), line); 1483244112Salfred return; 1484244112Salfred } 1485182473Sattilio if (witness_watch) { 1486182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1487244105Salfred kassert_panic( 1488244105Salfred "upgrade of exclusive lock (%s) %s @ %s:%d", 1489226294Sadrian class->lc_name, lock->lo_name, 1490226294Sadrian fixup_filename(file), line); 1491182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1492244105Salfred kassert_panic( 1493244105Salfred "upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1494182473Sattilio class->lc_name, lock->lo_name, 1495226294Sadrian instance->li_flags & LI_RECURSEMASK, 1496226294Sadrian fixup_filename(file), line); 1497182473Sattilio } 149882244Sjhb instance->li_flags |= LI_EXCLUSIVE; 149982244Sjhb} 150082244Sjhb 150182244Sjhbvoid 150282244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 150382244Sjhb int line) 150482244Sjhb{ 150582244Sjhb struct lock_instance *instance; 150682244Sjhb struct lock_class *class; 150782244Sjhb 1508181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1509182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 151082244Sjhb return; 1511154077Sjhb class = LOCK_CLASS(lock); 1512182473Sattilio if (witness_watch) { 1513182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1514244105Salfred kassert_panic( 1515244105Salfred "downgrade of non-upgradable lock (%s) %s @ %s:%d", 1516226294Sadrian class->lc_name, lock->lo_name, 1517226294Sadrian fixup_filename(file), line); 1518182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1519244105Salfred kassert_panic( 1520244105Salfred "downgrade of non-sleep lock (%s) %s @ %s:%d", 1521226294Sadrian class->lc_name, lock->lo_name, 1522226294Sadrian fixup_filename(file), line); 1523182473Sattilio } 152483366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 1525244112Salfred if (instance == NULL) { 1526244105Salfred kassert_panic("downgrade of unlocked lock (%s) %s @ %s:%d", 1527226294Sadrian class->lc_name, lock->lo_name, 1528226294Sadrian fixup_filename(file), line); 1529244112Salfred return; 1530244112Salfred } 1531182473Sattilio if (witness_watch) { 1532182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1533244105Salfred kassert_panic( 1534244105Salfred "downgrade of shared lock (%s) %s @ %s:%d", 1535226294Sadrian class->lc_name, lock->lo_name, 1536226294Sadrian fixup_filename(file), line); 1537182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1538244105Salfred kassert_panic( 1539244105Salfred "downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1540182473Sattilio class->lc_name, lock->lo_name, 1541226793Sjhb instance->li_flags & LI_RECURSEMASK, 1542226793Sjhb fixup_filename(file), line); 1543182473Sattilio } 154482244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 154582244Sjhb} 154682244Sjhb 154782244Sjhbvoid 154874912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 154965557Sjasone{ 155074912Sjhb struct lock_list_entry **lock_list, *lle; 155176272Sjhb struct lock_instance *instance; 155274912Sjhb struct lock_class *class; 155383366Sjulian struct thread *td; 155492858Simp register_t s; 155574912Sjhb int i, j; 155665557Sjasone 1557182446Sattilio if (witness_cold || lock->lo_witness == NULL || panicstr != NULL) 155871352Sjasone return; 155983366Sjulian td = curthread; 1560154077Sjhb class = LOCK_CLASS(lock); 1561125160Sjhb 1562125160Sjhb /* Find lock instance associated with this lock. */ 156376272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 156483366Sjulian lock_list = &td->td_sleeplocks; 156576272Sjhb else 156674912Sjhb lock_list = PCPU_PTR(spinlocks); 1567181695Sattilio lle = *lock_list; 156874912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 156976272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 157076272Sjhb instance = &(*lock_list)->ll_children[i]; 1571125160Sjhb if (instance->li_lock == lock) 1572125160Sjhb goto found; 157376272Sjhb } 1574182446Sattilio 1575182446Sattilio /* 1576182446Sattilio * When disabling WITNESS through witness_watch we could end up in 1577182473Sattilio * having registered locks in the td_sleeplocks queue. 1578182446Sattilio * We have to make sure we flush these queues, so just search for 1579182473Sattilio * eventual register locks and remove them. 1580182446Sattilio */ 1581244112Salfred if (witness_watch > 0) { 1582244105Salfred kassert_panic("lock (%s) %s not locked @ %s:%d", class->lc_name, 1583226294Sadrian lock->lo_name, fixup_filename(file), line); 1584182446Sattilio return; 1585244112Salfred } else { 1586244112Salfred return; 1587244112Salfred } 1588125160Sjhbfound: 1589125160Sjhb 1590125160Sjhb /* First, check for shared/exclusive mismatches. */ 1591182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 && 1592125160Sjhb (flags & LOP_EXCLUSIVE) == 0) { 1593125160Sjhb printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1594226793Sjhb lock->lo_name, fixup_filename(file), line); 1595125160Sjhb printf("while exclusively locked from %s:%d\n", 1596226294Sadrian fixup_filename(instance->li_file), instance->li_line); 1597244105Salfred kassert_panic("excl->ushare"); 1598125160Sjhb } 1599182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 && 1600125160Sjhb (flags & LOP_EXCLUSIVE) != 0) { 1601125160Sjhb printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1602226294Sadrian lock->lo_name, fixup_filename(file), line); 1603226294Sadrian printf("while share locked from %s:%d\n", 1604226294Sadrian fixup_filename(instance->li_file), 1605125160Sjhb instance->li_line); 1606244105Salfred kassert_panic("share->uexcl"); 1607125160Sjhb } 1608125160Sjhb /* If we are recursed, unrecurse. */ 1609125160Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1610125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1611125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1612125160Sjhb instance->li_flags); 1613125160Sjhb instance->li_flags--; 1614125160Sjhb return; 1615125160Sjhb } 1616189194Sthompsa /* The lock is now being dropped, check for NORELEASE flag */ 1617189194Sthompsa if ((instance->li_flags & LI_NORELEASE) != 0 && witness_watch > 0) { 1618189194Sthompsa printf("forbidden unlock of (%s) %s @ %s:%d\n", class->lc_name, 1619226294Sadrian lock->lo_name, fixup_filename(file), line); 1620244105Salfred kassert_panic("lock marked norelease"); 1621189194Sthompsa } 1622125160Sjhb 1623125160Sjhb /* Otherwise, remove this item from the list. */ 1624125160Sjhb s = intr_disable(); 1625125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1626125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1627125160Sjhb (*lock_list)->ll_count - 1); 1628125160Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 1629125160Sjhb (*lock_list)->ll_children[j] = 1630125160Sjhb (*lock_list)->ll_children[j + 1]; 1631125160Sjhb (*lock_list)->ll_count--; 1632125160Sjhb intr_restore(s); 1633125160Sjhb 1634181695Sattilio /* 1635182984Sattilio * In order to reduce contention on w_mtx, we want to keep always an 1636182984Sattilio * head object into lists so that frequent allocation from the 1637182984Sattilio * free witness pool (and subsequent locking) is avoided. 1638182984Sattilio * In order to maintain the current code simple, when the head 1639182984Sattilio * object is totally unloaded it means also that we do not have 1640182984Sattilio * further objects in the list, so the list ownership needs to be 1641182984Sattilio * hand over to another object if the current head needs to be freed. 1642181695Sattilio */ 1643182984Sattilio if ((*lock_list)->ll_count == 0) { 1644182984Sattilio if (*lock_list == lle) { 1645182984Sattilio if (lle->ll_next == NULL) 1646182984Sattilio return; 1647182984Sattilio } else 1648182984Sattilio lle = *lock_list; 1649125160Sjhb *lock_list = lle->ll_next; 1650125160Sjhb CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1651125160Sjhb td->td_proc->p_pid, lle); 1652125160Sjhb witness_lock_list_free(lle); 1653125160Sjhb } 165465557Sjasone} 165565557Sjasone 1656181695Sattiliovoid 1657181695Sattiliowitness_thread_exit(struct thread *td) 1658181695Sattilio{ 1659181695Sattilio struct lock_list_entry *lle; 1660181695Sattilio int i, n; 1661181695Sattilio 1662181695Sattilio lle = td->td_sleeplocks; 1663181695Sattilio if (lle == NULL || panicstr != NULL) 1664181695Sattilio return; 1665181695Sattilio if (lle->ll_count != 0) { 1666181695Sattilio for (n = 0; lle != NULL; lle = lle->ll_next) 1667181695Sattilio for (i = lle->ll_count - 1; i >= 0; i--) { 1668181695Sattilio if (n == 0) 1669181695Sattilio printf("Thread %p exiting with the following locks held:\n", 1670181695Sattilio td); 1671181695Sattilio n++; 1672207929Sattilio witness_list_lock(&lle->ll_children[i], printf); 1673181695Sattilio 1674181695Sattilio } 1675244105Salfred kassert_panic( 1676244105Salfred "Thread %p cannot exit while holding sleeplocks\n", td); 1677181695Sattilio } 1678181695Sattilio witness_lock_list_free(lle); 1679181695Sattilio} 1680181695Sattilio 168174912Sjhb/* 1682111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 1683111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 1684111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 1685111881Sjhb * console along with a list of the offending locks. If indicated in the 1686111881Sjhb * flags then a failure results in a panic as well. 168774912Sjhb */ 168865557Sjasoneint 1689111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 169065557Sjasone{ 1691183955Sattilio struct lock_list_entry *lock_list, *lle; 169276272Sjhb struct lock_instance *lock1; 169383366Sjulian struct thread *td; 1694111881Sjhb va_list ap; 169574912Sjhb int i, n; 169665557Sjasone 1697182446Sattilio if (witness_cold || witness_watch < 1 || panicstr != NULL) 169874912Sjhb return (0); 169974912Sjhb n = 0; 170083366Sjulian td = curthread; 1701111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 170274912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 170376272Sjhb lock1 = &lle->ll_children[i]; 1704111881Sjhb if (lock1->li_lock == lock) 1705111881Sjhb continue; 1706111881Sjhb if (flags & WARN_GIANTOK && 1707167787Sjhb lock1->li_lock == &Giant.lock_object) 170874912Sjhb continue; 1709111881Sjhb if (flags & WARN_SLEEPOK && 1710111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 171176272Sjhb continue; 1712111881Sjhb if (n == 0) { 1713111881Sjhb va_start(ap, fmt); 1714111881Sjhb vprintf(fmt, ap); 1715111881Sjhb va_end(ap); 1716111881Sjhb printf(" with the following"); 1717111881Sjhb if (flags & WARN_SLEEPOK) 1718111881Sjhb printf(" non-sleepable"); 1719118441Sjhb printf(" locks held:\n"); 172076272Sjhb } 172174912Sjhb n++; 1722207929Sattilio witness_list_lock(lock1, printf); 172374912Sjhb } 1724181695Sattilio 1725183955Sattilio /* 1726183955Sattilio * Pin the thread in order to avoid problems with thread migration. 1727183955Sattilio * Once that all verifies are passed about spinlocks ownership, 1728183955Sattilio * the thread is in a safe path and it can be unpinned. 1729183955Sattilio */ 1730183955Sattilio sched_pin(); 1731183955Sattilio lock_list = PCPU_GET(spinlocks); 1732184098Sattilio if (lock_list != NULL && lock_list->ll_count != 0) { 1733183955Sattilio sched_unpin(); 1734181695Sattilio 173597006Sjhb /* 1736183955Sattilio * We should only have one spinlock and as long as 1737183955Sattilio * the flags cannot match for this locks class, 1738183955Sattilio * check if the first spinlock is the one curthread 1739183955Sattilio * should hold. 174097006Sjhb */ 1741183955Sattilio lock1 = &lock_list->ll_children[lock_list->ll_count - 1]; 1742184098Sattilio if (lock_list->ll_count == 1 && lock_list->ll_next == NULL && 1743184098Sattilio lock1->li_lock == lock && n == 0) 1744184098Sattilio return (0); 1745183955Sattilio 1746184098Sattilio va_start(ap, fmt); 1747184098Sattilio vprintf(fmt, ap); 1748184098Sattilio va_end(ap); 1749184098Sattilio printf(" with the following"); 1750184098Sattilio if (flags & WARN_SLEEPOK) 1751184098Sattilio printf(" non-sleepable"); 1752184098Sattilio printf(" locks held:\n"); 1753207929Sattilio n += witness_list_locks(&lock_list, printf); 1754183955Sattilio } else 1755183955Sattilio sched_unpin(); 1756111881Sjhb if (flags & WARN_PANIC && n) 1757244105Salfred kassert_panic("%s", __func__); 1758181695Sattilio else 1759181695Sattilio witness_debugger(n); 176065557Sjasone return (n); 176165557Sjasone} 176265557Sjasone 1763102448Siedowseconst char * 1764102448Siedowsewitness_file(struct lock_object *lock) 1765102448Siedowse{ 1766102448Siedowse struct witness *w; 1767102448Siedowse 1768182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1769102448Siedowse return ("?"); 1770102448Siedowse w = lock->lo_witness; 1771102448Siedowse return (w->w_file); 1772102448Siedowse} 1773102448Siedowse 1774102448Siedowseint 1775102448Siedowsewitness_line(struct lock_object *lock) 1776102448Siedowse{ 1777102448Siedowse struct witness *w; 1778102448Siedowse 1779182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1780102448Siedowse return (0); 1781102448Siedowse w = lock->lo_witness; 1782102448Siedowse return (w->w_line); 1783102448Siedowse} 1784102448Siedowse 178565856Sjhbstatic struct witness * 178674912Sjhbenroll(const char *description, struct lock_class *lock_class) 178765557Sjasone{ 178874912Sjhb struct witness *w; 1789181695Sattilio struct witness_list *typelist; 179065557Sjasone 1791181695Sattilio MPASS(description != NULL); 1792181695Sattilio 1793182473Sattilio if (witness_watch == -1 || panicstr != NULL) 179465557Sjasone return (NULL); 1795181695Sattilio if ((lock_class->lc_flags & LC_SPINLOCK)) { 1796181695Sattilio if (witness_skipspin) 1797181695Sattilio return (NULL); 1798181695Sattilio else 1799181695Sattilio typelist = &w_spin; 1800244112Salfred } else if ((lock_class->lc_flags & LC_SLEEPLOCK)) { 1801181695Sattilio typelist = &w_sleep; 1802244112Salfred } else { 1803244105Salfred kassert_panic("lock class %s is not sleep or spin", 1804181695Sattilio lock_class->lc_name); 1805244112Salfred return (NULL); 1806244112Salfred } 1807181695Sattilio 1808181695Sattilio mtx_lock_spin(&w_mtx); 1809181695Sattilio w = witness_hash_get(description); 1810181695Sattilio if (w) 1811181695Sattilio goto found; 1812181695Sattilio if ((w = witness_get()) == NULL) 181365557Sjasone return (NULL); 1814181695Sattilio MPASS(strlen(description) < MAX_W_NAME); 1815181695Sattilio strcpy(w->w_name, description); 181674912Sjhb w->w_class = lock_class; 181775362Sjhb w->w_refcount = 1; 181874912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 1819149441Struckman if (lock_class->lc_flags & LC_SPINLOCK) { 182074912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1821149441Struckman w_spin_cnt++; 1822149441Struckman } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 182374912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1824149441Struckman w_sleep_cnt++; 182575364Sbp } 1826181695Sattilio 1827181695Sattilio /* Insert new witness into the hash */ 1828181695Sattilio witness_hash_put(w); 1829181695Sattilio witness_increment_graph_generation(); 183074912Sjhb mtx_unlock_spin(&w_mtx); 183165557Sjasone return (w); 1832181695Sattiliofound: 1833181695Sattilio w->w_refcount++; 1834181695Sattilio mtx_unlock_spin(&w_mtx); 1835181695Sattilio if (lock_class != w->w_class) 1836244105Salfred kassert_panic( 1837181695Sattilio "lock (%s) %s does not match earlier (%s) lock", 1838181695Sattilio description, lock_class->lc_name, 1839181695Sattilio w->w_class->lc_name); 1840181695Sattilio return (w); 184165557Sjasone} 184265557Sjasone 1843179025Sattiliostatic void 1844112117Sjhbdepart(struct witness *w) 184565557Sjasone{ 184674912Sjhb struct witness_list *list; 184765557Sjasone 1848112117Sjhb MPASS(w->w_refcount == 0); 1849149441Struckman if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1850112117Sjhb list = &w_sleep; 1851149441Struckman w_sleep_cnt--; 1852149441Struckman } else { 1853112117Sjhb list = &w_spin; 1854149441Struckman w_spin_cnt--; 1855149441Struckman } 1856112117Sjhb /* 1857181695Sattilio * Set file to NULL as it may point into a loadable module. 1858112117Sjhb */ 1859181695Sattilio w->w_file = NULL; 1860181695Sattilio w->w_line = 0; 1861181695Sattilio witness_increment_graph_generation(); 1862181695Sattilio} 1863112117Sjhb 1864181695Sattilio 1865181695Sattiliostatic void 1866181695Sattilioadopt(struct witness *parent, struct witness *child) 1867181695Sattilio{ 1868181695Sattilio int pi, ci, i, j; 1869181695Sattilio 1870181695Sattilio if (witness_cold == 0) 1871181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1872181695Sattilio 1873181695Sattilio /* If the relationship is already known, there's no work to be done. */ 1874181695Sattilio if (isitmychild(parent, child)) 1875181695Sattilio return; 1876181695Sattilio 1877181695Sattilio /* When the structure of the graph changes, bump up the generation. */ 1878181695Sattilio witness_increment_graph_generation(); 1879181695Sattilio 1880112117Sjhb /* 1881181695Sattilio * The hard part ... create the direct relationship, then propagate all 1882181695Sattilio * indirect relationships. 1883112117Sjhb */ 1884181695Sattilio pi = parent->w_index; 1885181695Sattilio ci = child->w_index; 1886181695Sattilio WITNESS_INDEX_ASSERT(pi); 1887181695Sattilio WITNESS_INDEX_ASSERT(ci); 1888181695Sattilio MPASS(pi != ci); 1889181695Sattilio w_rmatrix[pi][ci] |= WITNESS_PARENT; 1890181695Sattilio w_rmatrix[ci][pi] |= WITNESS_CHILD; 1891112117Sjhb 1892112117Sjhb /* 1893181695Sattilio * If parent was not already an ancestor of child, 1894181695Sattilio * then we increment the descendant and ancestor counters. 1895112117Sjhb */ 1896181695Sattilio if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) { 1897181695Sattilio parent->w_num_descendants++; 1898181695Sattilio child->w_num_ancestors++; 1899181695Sattilio } 1900112117Sjhb 1901181695Sattilio /* 1902181695Sattilio * Find each ancestor of 'pi'. Note that 'pi' itself is counted as 1903181695Sattilio * an ancestor of 'pi' during this loop. 1904181695Sattilio */ 1905181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 1906181695Sattilio if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 && 1907181695Sattilio (i != pi)) 1908181695Sattilio continue; 1909112117Sjhb 1910181695Sattilio /* Find each descendant of 'i' and mark it as a descendant. */ 1911181695Sattilio for (j = 1; j <= w_max_used_index; j++) { 191274912Sjhb 1913181695Sattilio /* 1914181695Sattilio * Skip children that are already marked as 1915181695Sattilio * descendants of 'i'. 1916181695Sattilio */ 1917181695Sattilio if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) 1918181695Sattilio continue; 1919181695Sattilio 1920181695Sattilio /* 1921181695Sattilio * We are only interested in descendants of 'ci'. Note 1922181695Sattilio * that 'ci' itself is counted as a descendant of 'ci'. 1923181695Sattilio */ 1924181695Sattilio if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 && 1925181695Sattilio (j != ci)) 1926181695Sattilio continue; 1927181695Sattilio w_rmatrix[i][j] |= WITNESS_ANCESTOR; 1928181695Sattilio w_rmatrix[j][i] |= WITNESS_DESCENDANT; 1929181695Sattilio w_data[i].w_num_descendants++; 1930181695Sattilio w_data[j].w_num_ancestors++; 1931181695Sattilio 1932181695Sattilio /* 1933181695Sattilio * Make sure we aren't marking a node as both an 1934181695Sattilio * ancestor and descendant. We should have caught 1935181695Sattilio * this as a lock order reversal earlier. 1936181695Sattilio */ 1937181695Sattilio if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) && 1938181695Sattilio (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) { 1939181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1940181695Sattilio "both ancestor and descendant\n", 1941181695Sattilio i, j, w_rmatrix[i][j]); 1942181695Sattilio kdb_backtrace(); 1943181695Sattilio printf("Witness disabled.\n"); 1944182446Sattilio witness_watch = -1; 1945181695Sattilio } 1946181695Sattilio if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) && 1947181695Sattilio (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) { 1948181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1949181695Sattilio "both ancestor and descendant\n", 1950181695Sattilio j, i, w_rmatrix[j][i]); 1951181695Sattilio kdb_backtrace(); 1952181695Sattilio printf("Witness disabled.\n"); 1953182446Sattilio witness_watch = -1; 1954181695Sattilio } 1955181695Sattilio } 195665557Sjasone } 1957112117Sjhb} 1958112117Sjhb 1959181695Sattiliostatic void 1960112117Sjhbitismychild(struct witness *parent, struct witness *child) 1961112117Sjhb{ 1962244112Salfred int unlocked; 1963112117Sjhb 1964112117Sjhb MPASS(child != NULL && parent != NULL); 1965181695Sattilio if (witness_cold == 0) 1966181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1967181695Sattilio 1968181695Sattilio if (!witness_lock_type_equal(parent, child)) { 1969244112Salfred if (witness_cold == 0) { 1970244112Salfred unlocked = 1; 1971181695Sattilio mtx_unlock_spin(&w_mtx); 1972244112Salfred } else { 1973244112Salfred unlocked = 0; 1974244112Salfred } 1975244105Salfred kassert_panic( 1976244105Salfred "%s: parent \"%s\" (%s) and child \"%s\" (%s) are not " 1977181695Sattilio "the same lock type", __func__, parent->w_name, 1978181695Sattilio parent->w_class->lc_name, child->w_name, 1979112117Sjhb child->w_class->lc_name); 1980244112Salfred if (unlocked) 1981244112Salfred mtx_lock_spin(&w_mtx); 1982181695Sattilio } 1983181695Sattilio adopt(parent, child); 198465557Sjasone} 198565557Sjasone 1986181695Sattilio/* 1987181695Sattilio * Generic code for the isitmy*() functions. The rmask parameter is the 1988181695Sattilio * expected relationship of w1 to w2. 1989181695Sattilio */ 1990181695Sattiliostatic int 1991181695Sattilio_isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname) 199265557Sjasone{ 1993181695Sattilio unsigned char r1, r2; 1994181695Sattilio int i1, i2; 199565557Sjasone 1996181695Sattilio i1 = w1->w_index; 1997181695Sattilio i2 = w2->w_index; 1998181695Sattilio WITNESS_INDEX_ASSERT(i1); 1999181695Sattilio WITNESS_INDEX_ASSERT(i2); 2000181695Sattilio r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK; 2001181695Sattilio r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK; 2002181695Sattilio 2003181695Sattilio /* The flags on one better be the inverse of the flags on the other */ 2004181695Sattilio if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) || 2005284653Smarkj (WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) { 2006284653Smarkj /* Don't squawk if we're potentially racing with an update. */ 2007284653Smarkj if (!mtx_owned(&w_mtx)) 2008284653Smarkj return (0); 2009181695Sattilio printf("%s: rmatrix mismatch between %s (index %d) and %s " 2010181695Sattilio "(index %d): w_rmatrix[%d][%d] == %hhx but " 2011181695Sattilio "w_rmatrix[%d][%d] == %hhx\n", 2012181695Sattilio fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1, 2013181695Sattilio i2, i1, r2); 2014181695Sattilio kdb_backtrace(); 2015181695Sattilio printf("Witness disabled.\n"); 2016182446Sattilio witness_watch = -1; 2017181695Sattilio } 2018181695Sattilio return (r1 & rmask); 201965557Sjasone} 202065557Sjasone 2021181695Sattilio/* 2022181695Sattilio * Checks if @child is a direct child of @parent. 2023181695Sattilio */ 202465557Sjasonestatic int 202565856Sjhbisitmychild(struct witness *parent, struct witness *child) 202665557Sjasone{ 202765557Sjasone 2028181695Sattilio return (_isitmyx(parent, child, WITNESS_PARENT, __func__)); 202965557Sjasone} 203065557Sjasone 2031181695Sattilio/* 2032181695Sattilio * Checks if @descendant is a direct or inderect descendant of @ancestor. 2033181695Sattilio */ 203465557Sjasonestatic int 2035181695Sattilioisitmydescendant(struct witness *ancestor, struct witness *descendant) 203665557Sjasone{ 203765557Sjasone 2038181695Sattilio return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK, 2039181695Sattilio __func__)); 204065557Sjasone} 204165557Sjasone 2042105508Sphk#ifdef BLESSING 204365557Sjasonestatic int 204465856Sjhbblessed(struct witness *w1, struct witness *w2) 204565557Sjasone{ 204665557Sjasone int i; 204765856Sjhb struct witness_blessed *b; 204865557Sjasone 204965557Sjasone for (i = 0; i < blessed_count; i++) { 205065557Sjasone b = &blessed_list[i]; 205174912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 205274912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 205365557Sjasone return (1); 205465557Sjasone continue; 205565557Sjasone } 205674912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 205774912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 205865557Sjasone return (1); 205965557Sjasone } 206065557Sjasone return (0); 206165557Sjasone} 2062105508Sphk#endif 206365557Sjasone 206465856Sjhbstatic struct witness * 206574912Sjhbwitness_get(void) 206665557Sjasone{ 206765856Sjhb struct witness *w; 2068181695Sattilio int index; 206965557Sjasone 2070181695Sattilio if (witness_cold == 0) 2071181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2072181695Sattilio 2073182473Sattilio if (witness_watch == -1) { 207476481Sjhb mtx_unlock_spin(&w_mtx); 207576481Sjhb return (NULL); 207676481Sjhb } 207774912Sjhb if (STAILQ_EMPTY(&w_free)) { 2078182446Sattilio witness_watch = -1; 207974912Sjhb mtx_unlock_spin(&w_mtx); 2080181695Sattilio printf("WITNESS: unable to allocate a new witness object\n"); 208165557Sjasone return (NULL); 208265557Sjasone } 208374912Sjhb w = STAILQ_FIRST(&w_free); 208474912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 2085149441Struckman w_free_cnt--; 2086181695Sattilio index = w->w_index; 2087181695Sattilio MPASS(index > 0 && index == w_max_used_index+1 && 2088181695Sattilio index < WITNESS_COUNT); 208965856Sjhb bzero(w, sizeof(*w)); 2090181695Sattilio w->w_index = index; 2091181695Sattilio if (index > w_max_used_index) 2092181695Sattilio w_max_used_index = index; 209365557Sjasone return (w); 209465557Sjasone} 209565557Sjasone 209665557Sjasonestatic void 209765856Sjhbwitness_free(struct witness *w) 209865557Sjasone{ 209974912Sjhb 210074912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 2101149441Struckman w_free_cnt++; 210265557Sjasone} 210365557Sjasone 210474912Sjhbstatic struct lock_list_entry * 210574912Sjhbwitness_lock_list_get(void) 210674912Sjhb{ 210774912Sjhb struct lock_list_entry *lle; 210871709Sjhb 2109182446Sattilio if (witness_watch == -1) 211076481Sjhb return (NULL); 211174912Sjhb mtx_lock_spin(&w_mtx); 211274912Sjhb lle = w_lock_list_free; 211374912Sjhb if (lle == NULL) { 2114182446Sattilio witness_watch = -1; 211574912Sjhb mtx_unlock_spin(&w_mtx); 211674912Sjhb printf("%s: witness exhausted\n", __func__); 211774912Sjhb return (NULL); 211874912Sjhb } 211974912Sjhb w_lock_list_free = lle->ll_next; 212074912Sjhb mtx_unlock_spin(&w_mtx); 212174912Sjhb bzero(lle, sizeof(*lle)); 212274912Sjhb return (lle); 212374912Sjhb} 212474912Sjhb 212574912Sjhbstatic void 212674912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 212771709Sjhb{ 212871709Sjhb 212974912Sjhb mtx_lock_spin(&w_mtx); 213074912Sjhb lle->ll_next = w_lock_list_free; 213174912Sjhb w_lock_list_free = lle; 213274912Sjhb mtx_unlock_spin(&w_mtx); 213371709Sjhb} 213471709Sjhb 213576272Sjhbstatic struct lock_instance * 2136227588Spjdfind_instance(struct lock_list_entry *list, const struct lock_object *lock) 213776272Sjhb{ 213876272Sjhb struct lock_list_entry *lle; 213976272Sjhb struct lock_instance *instance; 214076272Sjhb int i; 214176272Sjhb 2142181695Sattilio for (lle = list; lle != NULL; lle = lle->ll_next) 214376272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 214476272Sjhb instance = &lle->ll_children[i]; 214576272Sjhb if (instance->li_lock == lock) 214676272Sjhb return (instance); 214776272Sjhb } 214876272Sjhb return (NULL); 214976272Sjhb} 215076272Sjhb 2151111881Sjhbstatic void 2152207929Sattiliowitness_list_lock(struct lock_instance *instance, 2153207929Sattilio int (*prnt)(const char *fmt, ...)) 2154111881Sjhb{ 2155111881Sjhb struct lock_object *lock; 2156111881Sjhb 2157111881Sjhb lock = instance->li_lock; 2158207929Sattilio prnt("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 2159154077Sjhb "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 2160179025Sattilio if (lock->lo_witness->w_name != lock->lo_name) 2161207929Sattilio prnt(" (%s)", lock->lo_witness->w_name); 2162207929Sattilio prnt(" r = %d (%p) locked @ %s:%d\n", 2163226294Sadrian instance->li_flags & LI_RECURSEMASK, lock, 2164226793Sjhb fixup_filename(instance->li_file), instance->li_line); 2165111881Sjhb} 2166111881Sjhb 2167140637Srwatson#ifdef DDB 2168139333Srwatsonstatic int 2169139333Srwatsonwitness_thread_has_locks(struct thread *td) 2170139333Srwatson{ 2171139333Srwatson 2172182984Sattilio if (td->td_sleeplocks == NULL) 2173182984Sattilio return (0); 2174182984Sattilio return (td->td_sleeplocks->ll_count != 0); 2175139333Srwatson} 2176139333Srwatson 2177139333Srwatsonstatic int 2178139333Srwatsonwitness_proc_has_locks(struct proc *p) 2179139333Srwatson{ 2180139333Srwatson struct thread *td; 2181139333Srwatson 2182139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2183139333Srwatson if (witness_thread_has_locks(td)) 2184139333Srwatson return (1); 2185139333Srwatson } 2186139333Srwatson return (0); 2187139333Srwatson} 2188140637Srwatson#endif 2189139333Srwatson 219074912Sjhbint 2191207929Sattiliowitness_list_locks(struct lock_list_entry **lock_list, 2192207929Sattilio int (*prnt)(const char *fmt, ...)) 219372224Sjhb{ 219475273Sjhb struct lock_list_entry *lle; 219574912Sjhb int i, nheld; 219672224Sjhb 219774912Sjhb nheld = 0; 219874912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 219974912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 2200207929Sattilio witness_list_lock(&lle->ll_children[i], prnt); 220174912Sjhb nheld++; 220274912Sjhb } 220375273Sjhb return (nheld); 220475273Sjhb} 220575273Sjhb 2206118271Sjhb/* 2207118271Sjhb * This is a bit risky at best. We call this function when we have timed 2208118271Sjhb * out acquiring a spin lock, and we assume that the other CPU is stuck 2209118271Sjhb * with this lock held. So, we go groveling around in the other CPU's 2210118271Sjhb * per-cpu data to try to find the lock instance for this spin lock to 2211118271Sjhb * see when it was last acquired. 2212118271Sjhb */ 221365557Sjasonevoid 2214207929Sattiliowitness_display_spinlock(struct lock_object *lock, struct thread *owner, 2215207929Sattilio int (*prnt)(const char *fmt, ...)) 2216118271Sjhb{ 2217118271Sjhb struct lock_instance *instance; 2218118271Sjhb struct pcpu *pc; 2219118271Sjhb 2220118271Sjhb if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 2221118271Sjhb return; 2222118271Sjhb pc = pcpu_find(owner->td_oncpu); 2223118271Sjhb instance = find_instance(pc->pc_spinlocks, lock); 2224118271Sjhb if (instance != NULL) 2225207929Sattilio witness_list_lock(instance, prnt); 2226118271Sjhb} 2227118271Sjhb 2228118271Sjhbvoid 222974912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 223065557Sjasone{ 2231153854Sjhb struct lock_list_entry *lock_list; 223276272Sjhb struct lock_instance *instance; 2233154077Sjhb struct lock_class *class; 223471320Sjasone 2235228424Savg /* 2236228424Savg * This function is used independently in locking code to deal with 2237228424Savg * Giant, SCHEDULER_STOPPED() check can be removed here after Giant 2238228424Savg * is gone. 2239228424Savg */ 2240228424Savg if (SCHEDULER_STOPPED()) 2241228424Savg return; 2242181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2243182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 224471352Sjasone return; 2245154077Sjhb class = LOCK_CLASS(lock); 2246154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2247153854Sjhb lock_list = curthread->td_sleeplocks; 2248153854Sjhb else { 2249153854Sjhb if (witness_skipspin) 2250153854Sjhb return; 2251153854Sjhb lock_list = PCPU_GET(spinlocks); 2252153854Sjhb } 2253153854Sjhb instance = find_instance(lock_list, lock); 2254244112Salfred if (instance == NULL) { 2255244105Salfred kassert_panic("%s: lock (%s) %s not locked", __func__, 2256154077Sjhb class->lc_name, lock->lo_name); 2257244112Salfred return; 2258244112Salfred } 225976272Sjhb *filep = instance->li_file; 226076272Sjhb *linep = instance->li_line; 226165557Sjasone} 226265557Sjasone 226365557Sjasonevoid 226474912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 226565557Sjasone{ 2266153854Sjhb struct lock_list_entry *lock_list; 226776272Sjhb struct lock_instance *instance; 2268154077Sjhb struct lock_class *class; 226971320Sjasone 2270228424Savg /* 2271228424Savg * This function is used independently in locking code to deal with 2272228424Savg * Giant, SCHEDULER_STOPPED() check can be removed here after Giant 2273228424Savg * is gone. 2274228424Savg */ 2275228424Savg if (SCHEDULER_STOPPED()) 2276228424Savg return; 2277181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2278182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 227971352Sjasone return; 2280154077Sjhb class = LOCK_CLASS(lock); 2281154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2282153854Sjhb lock_list = curthread->td_sleeplocks; 2283153854Sjhb else { 2284153854Sjhb if (witness_skipspin) 2285153854Sjhb return; 2286153854Sjhb lock_list = PCPU_GET(spinlocks); 2287153854Sjhb } 2288153854Sjhb instance = find_instance(lock_list, lock); 228982243Sjhb if (instance == NULL) 2290244105Salfred kassert_panic("%s: lock (%s) %s not locked", __func__, 2291154077Sjhb class->lc_name, lock->lo_name); 229274912Sjhb lock->lo_witness->w_file = file; 229374912Sjhb lock->lo_witness->w_line = line; 2294244112Salfred if (instance == NULL) 2295244112Salfred return; 229676272Sjhb instance->li_file = file; 229776272Sjhb instance->li_line = line; 229865557Sjasone} 229965557Sjasone 230078871Sjhbvoid 2301227588Spjdwitness_assert(const struct lock_object *lock, int flags, const char *file, 2302227588Spjd int line) 230378871Sjhb{ 230478871Sjhb#ifdef INVARIANT_SUPPORT 230578871Sjhb struct lock_instance *instance; 2306154077Sjhb struct lock_class *class; 230778871Sjhb 2308182446Sattilio if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL) 230978941Sjhb return; 2310154077Sjhb class = LOCK_CLASS(lock); 2311154077Sjhb if ((class->lc_flags & LC_SLEEPLOCK) != 0) 231283366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 2313154077Sjhb else if ((class->lc_flags & LC_SPINLOCK) != 0) 231478871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 231586422Sjhb else { 2316244105Salfred kassert_panic("Lock (%s) %s is not sleep or spin!", 2317154077Sjhb class->lc_name, lock->lo_name); 2318244111Salfred return; 231986422Sjhb } 232078871Sjhb switch (flags) { 232178871Sjhb case LA_UNLOCKED: 232278871Sjhb if (instance != NULL) 2323244105Salfred kassert_panic("Lock (%s) %s locked @ %s:%d.", 2324226294Sadrian class->lc_name, lock->lo_name, 2325226294Sadrian fixup_filename(file), line); 232678871Sjhb break; 232778871Sjhb case LA_LOCKED: 232878871Sjhb case LA_LOCKED | LA_RECURSED: 232978871Sjhb case LA_LOCKED | LA_NOTRECURSED: 233078871Sjhb case LA_SLOCKED: 233178871Sjhb case LA_SLOCKED | LA_RECURSED: 233278871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 233378871Sjhb case LA_XLOCKED: 233478871Sjhb case LA_XLOCKED | LA_RECURSED: 233578871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 233686422Sjhb if (instance == NULL) { 2337244105Salfred kassert_panic("Lock (%s) %s not locked @ %s:%d.", 2338226294Sadrian class->lc_name, lock->lo_name, 2339226294Sadrian fixup_filename(file), line); 234086422Sjhb break; 234186422Sjhb } 234278871Sjhb if ((flags & LA_XLOCKED) != 0 && 234378871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 2344244105Salfred kassert_panic( 2345244105Salfred "Lock (%s) %s not exclusively locked @ %s:%d.", 2346226294Sadrian class->lc_name, lock->lo_name, 2347226294Sadrian fixup_filename(file), line); 234878871Sjhb if ((flags & LA_SLOCKED) != 0 && 234978871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 2350244105Salfred kassert_panic( 2351244105Salfred "Lock (%s) %s exclusively locked @ %s:%d.", 2352226294Sadrian class->lc_name, lock->lo_name, 2353226294Sadrian fixup_filename(file), line); 235478871Sjhb if ((flags & LA_RECURSED) != 0 && 235578871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 2356244105Salfred kassert_panic("Lock (%s) %s not recursed @ %s:%d.", 2357226294Sadrian class->lc_name, lock->lo_name, 2358226294Sadrian fixup_filename(file), line); 235978871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 236078871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 2361244105Salfred kassert_panic("Lock (%s) %s recursed @ %s:%d.", 2362226294Sadrian class->lc_name, lock->lo_name, 2363226294Sadrian fixup_filename(file), line); 236478871Sjhb break; 236578871Sjhb default: 2366244105Salfred kassert_panic("Invalid lock assertion at %s:%d.", 2367226294Sadrian fixup_filename(file), line); 236878871Sjhb 236978871Sjhb } 237078871Sjhb#endif /* INVARIANT_SUPPORT */ 237178871Sjhb} 237278871Sjhb 2373187511Sthompsastatic void 2374187511Sthompsawitness_setflag(struct lock_object *lock, int flag, int set) 2375187511Sthompsa{ 2376187511Sthompsa struct lock_list_entry *lock_list; 2377187511Sthompsa struct lock_instance *instance; 2378187511Sthompsa struct lock_class *class; 2379187511Sthompsa 2380187511Sthompsa if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 2381187511Sthompsa return; 2382187511Sthompsa class = LOCK_CLASS(lock); 2383187511Sthompsa if (class->lc_flags & LC_SLEEPLOCK) 2384187511Sthompsa lock_list = curthread->td_sleeplocks; 2385187511Sthompsa else { 2386187511Sthompsa if (witness_skipspin) 2387187511Sthompsa return; 2388187511Sthompsa lock_list = PCPU_GET(spinlocks); 2389187511Sthompsa } 2390187511Sthompsa instance = find_instance(lock_list, lock); 2391244112Salfred if (instance == NULL) { 2392244105Salfred kassert_panic("%s: lock (%s) %s not locked", __func__, 2393187511Sthompsa class->lc_name, lock->lo_name); 2394244112Salfred return; 2395244112Salfred } 2396187511Sthompsa 2397187511Sthompsa if (set) 2398187511Sthompsa instance->li_flags |= flag; 2399187511Sthompsa else 2400187511Sthompsa instance->li_flags &= ~flag; 2401187511Sthompsa} 2402187511Sthompsa 2403187511Sthompsavoid 2404187511Sthompsawitness_norelease(struct lock_object *lock) 2405187511Sthompsa{ 2406187511Sthompsa 2407187511Sthompsa witness_setflag(lock, LI_NORELEASE, 1); 2408187511Sthompsa} 2409187511Sthompsa 2410187511Sthompsavoid 2411187511Sthompsawitness_releaseok(struct lock_object *lock) 2412187511Sthompsa{ 2413187511Sthompsa 2414187511Sthompsa witness_setflag(lock, LI_NORELEASE, 0); 2415187511Sthompsa} 2416187511Sthompsa 241774912Sjhb#ifdef DDB 2418112061Sjhbstatic void 2419181695Sattiliowitness_ddb_list(struct thread *td) 2420112061Sjhb{ 242174912Sjhb 2422181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2423131930Smarcel KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 2424112061Sjhb 2425182446Sattilio if (witness_watch < 1) 2426112061Sjhb return; 2427112061Sjhb 2428207929Sattilio witness_list_locks(&td->td_sleeplocks, db_printf); 2429112061Sjhb 2430112061Sjhb /* 2431112061Sjhb * We only handle spinlocks if td == curthread. This is somewhat broken 2432112061Sjhb * if td is currently executing on some other CPU and holds spin locks 2433112061Sjhb * as we won't display those locks. If we had a MI way of getting 2434112061Sjhb * the per-cpu data for a given cpu then we could use 2435113339Sjulian * td->td_oncpu to get the list of spinlocks for this thread 2436112061Sjhb * and "fix" this. 2437112061Sjhb * 2438170302Sjeff * That still wouldn't really fix this unless we locked the scheduler 2439170302Sjeff * lock or stopped the other CPU to make sure it wasn't changing the 2440170302Sjeff * list out from under us. It is probably best to just not try to 2441170302Sjeff * handle threads on other CPU's for now. 2442112061Sjhb */ 2443112061Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 2444207929Sattilio witness_list_locks(PCPU_PTR(spinlocks), db_printf); 2445112061Sjhb} 2446112061Sjhb 244774930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 244874912Sjhb{ 244983366Sjulian struct thread *td; 245074912Sjhb 2451158030Sjhb if (have_addr) 2452158030Sjhb td = db_lookup_thread(addr, TRUE); 2453158030Sjhb else 2454158030Sjhb td = kdb_thread; 2455181695Sattilio witness_ddb_list(td); 245674912Sjhb} 245774912Sjhb 2458183054SsamDB_SHOW_ALL_COMMAND(locks, db_witness_list_all) 2459139333Srwatson{ 2460139333Srwatson struct thread *td; 2461139333Srwatson struct proc *p; 2462139333Srwatson 2463139333Srwatson /* 2464139333Srwatson * It would be nice to list only threads and processes that actually 2465139333Srwatson * held sleep locks, but that information is currently not exported 2466139333Srwatson * by WITNESS. 2467139333Srwatson */ 2468139333Srwatson FOREACH_PROC_IN_SYSTEM(p) { 2469139333Srwatson if (!witness_proc_has_locks(p)) 2470139333Srwatson continue; 2471139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2472139333Srwatson if (!witness_thread_has_locks(td)) 2473139333Srwatson continue; 2474153853Sjhb db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2475182984Sattilio p->p_comm, td, td->td_tid); 2476181695Sattilio witness_ddb_list(td); 2477239584Sjhb if (db_pager_quit) 2478239584Sjhb return; 2479139333Srwatson } 2480139333Srwatson } 2481139333Srwatson} 2482183054SsamDB_SHOW_ALIAS(alllocks, db_witness_list_all) 2483139333Srwatson 248474912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 248574912Sjhb{ 248674912Sjhb 2487181695Sattilio witness_ddb_display(db_printf); 248874912Sjhb} 248974912Sjhb#endif 2490181695Sattilio 2491181695Sattiliostatic int 2492181695Sattiliosysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS) 2493181695Sattilio{ 2494181695Sattilio struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2; 2495181695Sattilio struct witness *tmp_w1, *tmp_w2, *w1, *w2; 2496181695Sattilio struct sbuf *sb; 2497181695Sattilio u_int w_rmatrix1, w_rmatrix2; 2498181695Sattilio int error, generation, i, j; 2499181695Sattilio 2500181695Sattilio tmp_data1 = NULL; 2501181695Sattilio tmp_data2 = NULL; 2502181695Sattilio tmp_w1 = NULL; 2503181695Sattilio tmp_w2 = NULL; 2504182446Sattilio if (witness_watch < 1) { 2505181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2506181695Sattilio return (error); 2507181695Sattilio } 2508181695Sattilio if (witness_cold) { 2509181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2510181695Sattilio return (error); 2511181695Sattilio } 2512181695Sattilio error = 0; 2513181695Sattilio sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND); 2514181695Sattilio if (sb == NULL) 2515181695Sattilio return (ENOMEM); 2516181695Sattilio 2517181695Sattilio /* Allocate and init temporary storage space. */ 2518181695Sattilio tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2519181695Sattilio tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2520181695Sattilio tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2521181695Sattilio M_WAITOK | M_ZERO); 2522181695Sattilio tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2523181695Sattilio M_WAITOK | M_ZERO); 2524181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2525181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2526181695Sattilio 2527181695Sattiliorestart: 2528181695Sattilio mtx_lock_spin(&w_mtx); 2529181695Sattilio generation = w_generation; 2530181695Sattilio mtx_unlock_spin(&w_mtx); 2531181695Sattilio sbuf_printf(sb, "Number of known direct relationships is %d\n", 2532181695Sattilio w_lohash.wloh_count); 2533181695Sattilio for (i = 1; i < w_max_used_index; i++) { 2534181695Sattilio mtx_lock_spin(&w_mtx); 2535181695Sattilio if (generation != w_generation) { 2536181695Sattilio mtx_unlock_spin(&w_mtx); 2537181695Sattilio 2538181695Sattilio /* The graph has changed, try again. */ 2539181695Sattilio req->oldidx = 0; 2540181695Sattilio sbuf_clear(sb); 2541181695Sattilio goto restart; 2542181695Sattilio } 2543181695Sattilio 2544181695Sattilio w1 = &w_data[i]; 2545181695Sattilio if (w1->w_reversed == 0) { 2546181695Sattilio mtx_unlock_spin(&w_mtx); 2547181695Sattilio continue; 2548181695Sattilio } 2549181695Sattilio 2550181695Sattilio /* Copy w1 locally so we can release the spin lock. */ 2551181695Sattilio *tmp_w1 = *w1; 2552181695Sattilio mtx_unlock_spin(&w_mtx); 2553181695Sattilio 2554181695Sattilio if (tmp_w1->w_reversed == 0) 2555181695Sattilio continue; 2556181695Sattilio for (j = 1; j < w_max_used_index; j++) { 2557181695Sattilio if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j) 2558181695Sattilio continue; 2559181695Sattilio 2560181695Sattilio mtx_lock_spin(&w_mtx); 2561181695Sattilio if (generation != w_generation) { 2562181695Sattilio mtx_unlock_spin(&w_mtx); 2563181695Sattilio 2564181695Sattilio /* The graph has changed, try again. */ 2565181695Sattilio req->oldidx = 0; 2566181695Sattilio sbuf_clear(sb); 2567181695Sattilio goto restart; 2568181695Sattilio } 2569181695Sattilio 2570181695Sattilio w2 = &w_data[j]; 2571181695Sattilio data1 = witness_lock_order_get(w1, w2); 2572181695Sattilio data2 = witness_lock_order_get(w2, w1); 2573181695Sattilio 2574181695Sattilio /* 2575181695Sattilio * Copy information locally so we can release the 2576181695Sattilio * spin lock. 2577181695Sattilio */ 2578181695Sattilio *tmp_w2 = *w2; 2579181695Sattilio w_rmatrix1 = (unsigned int)w_rmatrix[i][j]; 2580181695Sattilio w_rmatrix2 = (unsigned int)w_rmatrix[j][i]; 2581181695Sattilio 2582181695Sattilio if (data1) { 2583181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2584181695Sattilio stack_copy(&data1->wlod_stack, 2585181695Sattilio &tmp_data1->wlod_stack); 2586181695Sattilio } 2587181695Sattilio if (data2 && data2 != data1) { 2588181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2589181695Sattilio stack_copy(&data2->wlod_stack, 2590181695Sattilio &tmp_data2->wlod_stack); 2591181695Sattilio } 2592181695Sattilio mtx_unlock_spin(&w_mtx); 2593181695Sattilio 2594181695Sattilio sbuf_printf(sb, 2595181695Sattilio "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n", 2596181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2597181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2598181695Sattilio#if 0 2599181695Sattilio sbuf_printf(sb, 2600181695Sattilio "w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n", 2601181695Sattilio tmp_w1->name, tmp_w2->w_name, w_rmatrix1, 2602181695Sattilio tmp_w2->name, tmp_w1->w_name, w_rmatrix2); 2603181695Sattilio#endif 2604181695Sattilio if (data1) { 2605181695Sattilio sbuf_printf(sb, 2606181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2607181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2608181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2609181695Sattilio stack_sbuf_print(sb, &tmp_data1->wlod_stack); 2610181695Sattilio sbuf_printf(sb, "\n"); 2611181695Sattilio } 2612181695Sattilio if (data2 && data2 != data1) { 2613181695Sattilio sbuf_printf(sb, 2614181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2615181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name, 2616181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name); 2617181695Sattilio stack_sbuf_print(sb, &tmp_data2->wlod_stack); 2618181695Sattilio sbuf_printf(sb, "\n"); 2619181695Sattilio } 2620181695Sattilio } 2621181695Sattilio } 2622181695Sattilio mtx_lock_spin(&w_mtx); 2623181695Sattilio if (generation != w_generation) { 2624181695Sattilio mtx_unlock_spin(&w_mtx); 2625181695Sattilio 2626181695Sattilio /* 2627181695Sattilio * The graph changed while we were printing stack data, 2628181695Sattilio * try again. 2629181695Sattilio */ 2630181695Sattilio req->oldidx = 0; 2631181695Sattilio sbuf_clear(sb); 2632181695Sattilio goto restart; 2633181695Sattilio } 2634181695Sattilio mtx_unlock_spin(&w_mtx); 2635181695Sattilio 2636181695Sattilio /* Free temporary storage space. */ 2637181695Sattilio free(tmp_data1, M_TEMP); 2638181695Sattilio free(tmp_data2, M_TEMP); 2639181695Sattilio free(tmp_w1, M_TEMP); 2640181695Sattilio free(tmp_w2, M_TEMP); 2641181695Sattilio 2642181695Sattilio sbuf_finish(sb); 2643181695Sattilio error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2644181695Sattilio sbuf_delete(sb); 2645181695Sattilio 2646181695Sattilio return (error); 2647181695Sattilio} 2648181695Sattilio 2649181695Sattiliostatic int 2650181695Sattiliosysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS) 2651181695Sattilio{ 2652181695Sattilio struct witness *w; 2653181695Sattilio struct sbuf *sb; 2654181695Sattilio int error; 2655181695Sattilio 2656182446Sattilio if (witness_watch < 1) { 2657181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2658181695Sattilio return (error); 2659181695Sattilio } 2660181695Sattilio if (witness_cold) { 2661181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2662181695Sattilio return (error); 2663181695Sattilio } 2664181695Sattilio error = 0; 2665217916Smdf 2666217916Smdf error = sysctl_wire_old_buffer(req, 0); 2667217916Smdf if (error != 0) 2668217916Smdf return (error); 2669212750Smdf sb = sbuf_new_for_sysctl(NULL, NULL, FULLGRAPH_SBUF_SIZE, req); 2670181695Sattilio if (sb == NULL) 2671181695Sattilio return (ENOMEM); 2672181695Sattilio sbuf_printf(sb, "\n"); 2673181695Sattilio 2674181695Sattilio mtx_lock_spin(&w_mtx); 2675181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2676181695Sattilio w->w_displayed = 0; 2677181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2678181695Sattilio witness_add_fullgraph(sb, w); 2679181695Sattilio mtx_unlock_spin(&w_mtx); 2680181695Sattilio 2681181695Sattilio /* 2682181695Sattilio * Close the sbuf and return to userland. 2683181695Sattilio */ 2684212750Smdf error = sbuf_finish(sb); 2685181695Sattilio sbuf_delete(sb); 2686181695Sattilio 2687181695Sattilio return (error); 2688181695Sattilio} 2689181695Sattilio 2690181695Sattiliostatic int 2691181695Sattiliosysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 2692181695Sattilio{ 2693181695Sattilio int error, value; 2694181695Sattilio 2695182473Sattilio value = witness_watch; 2696181695Sattilio error = sysctl_handle_int(oidp, &value, 0, req); 2697181695Sattilio if (error != 0 || req->newptr == NULL) 2698181695Sattilio return (error); 2699182446Sattilio if (value > 1 || value < -1 || 2700182446Sattilio (witness_watch == -1 && value != witness_watch)) 2701181695Sattilio return (EINVAL); 2702182446Sattilio witness_watch = value; 2703181695Sattilio return (0); 2704181695Sattilio} 2705181695Sattilio 2706181695Sattiliostatic void 2707181695Sattiliowitness_add_fullgraph(struct sbuf *sb, struct witness *w) 2708181695Sattilio{ 2709181695Sattilio int i; 2710181695Sattilio 2711181695Sattilio if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0)) 2712181695Sattilio return; 2713181695Sattilio w->w_displayed = 1; 2714181695Sattilio 2715181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 2716181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 2717181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) { 2718181695Sattilio sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name, 2719181695Sattilio w_data[i].w_name); 2720181695Sattilio witness_add_fullgraph(sb, &w_data[i]); 2721181695Sattilio } 2722181695Sattilio } 2723181695Sattilio} 2724181695Sattilio 2725181695Sattilio/* 2726181695Sattilio * A simple hash function. Takes a key pointer and a key size. If size == 0, 2727181695Sattilio * interprets the key as a string and reads until the null 2728181695Sattilio * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit 2729181695Sattilio * hash value computed from the key. 2730181695Sattilio */ 2731181695Sattiliostatic uint32_t 2732181695Sattiliowitness_hash_djb2(const uint8_t *key, uint32_t size) 2733181695Sattilio{ 2734181695Sattilio unsigned int hash = 5381; 2735181695Sattilio int i; 2736181695Sattilio 2737181695Sattilio /* hash = hash * 33 + key[i] */ 2738181695Sattilio if (size) 2739181695Sattilio for (i = 0; i < size; i++) 2740181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2741181695Sattilio else 2742181695Sattilio for (i = 0; key[i] != 0; i++) 2743181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2744181695Sattilio 2745181695Sattilio return (hash); 2746181695Sattilio} 2747181695Sattilio 2748181695Sattilio 2749181695Sattilio/* 2750181695Sattilio * Initializes the two witness hash tables. Called exactly once from 2751181695Sattilio * witness_initialize(). 2752181695Sattilio */ 2753181695Sattiliostatic void 2754181695Sattiliowitness_init_hash_tables(void) 2755181695Sattilio{ 2756181695Sattilio int i; 2757181695Sattilio 2758181695Sattilio MPASS(witness_cold); 2759181695Sattilio 2760181695Sattilio /* Initialize the hash tables. */ 2761181695Sattilio for (i = 0; i < WITNESS_HASH_SIZE; i++) 2762181695Sattilio w_hash.wh_array[i] = NULL; 2763181695Sattilio 2764181695Sattilio w_hash.wh_size = WITNESS_HASH_SIZE; 2765181695Sattilio w_hash.wh_count = 0; 2766181695Sattilio 2767181695Sattilio /* Initialize the lock order data hash. */ 2768181695Sattilio w_lofree = NULL; 2769181695Sattilio for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) { 2770181695Sattilio memset(&w_lodata[i], 0, sizeof(w_lodata[i])); 2771181695Sattilio w_lodata[i].wlod_next = w_lofree; 2772181695Sattilio w_lofree = &w_lodata[i]; 2773181695Sattilio } 2774181695Sattilio w_lohash.wloh_size = WITNESS_LO_HASH_SIZE; 2775181695Sattilio w_lohash.wloh_count = 0; 2776181695Sattilio for (i = 0; i < WITNESS_LO_HASH_SIZE; i++) 2777181695Sattilio w_lohash.wloh_array[i] = NULL; 2778181695Sattilio} 2779181695Sattilio 2780181695Sattiliostatic struct witness * 2781181695Sattiliowitness_hash_get(const char *key) 2782181695Sattilio{ 2783181695Sattilio struct witness *w; 2784181695Sattilio uint32_t hash; 2785181695Sattilio 2786181695Sattilio MPASS(key != NULL); 2787181695Sattilio if (witness_cold == 0) 2788181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2789181695Sattilio hash = witness_hash_djb2(key, 0) % w_hash.wh_size; 2790181695Sattilio w = w_hash.wh_array[hash]; 2791181695Sattilio while (w != NULL) { 2792181695Sattilio if (strcmp(w->w_name, key) == 0) 2793181695Sattilio goto out; 2794181695Sattilio w = w->w_hash_next; 2795181695Sattilio } 2796181695Sattilio 2797181695Sattilioout: 2798181695Sattilio return (w); 2799181695Sattilio} 2800181695Sattilio 2801181695Sattiliostatic void 2802181695Sattiliowitness_hash_put(struct witness *w) 2803181695Sattilio{ 2804181695Sattilio uint32_t hash; 2805181695Sattilio 2806181695Sattilio MPASS(w != NULL); 2807181695Sattilio MPASS(w->w_name != NULL); 2808181695Sattilio if (witness_cold == 0) 2809181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2810181695Sattilio KASSERT(witness_hash_get(w->w_name) == NULL, 2811181695Sattilio ("%s: trying to add a hash entry that already exists!", __func__)); 2812181695Sattilio KASSERT(w->w_hash_next == NULL, 2813181695Sattilio ("%s: w->w_hash_next != NULL", __func__)); 2814181695Sattilio 2815181695Sattilio hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size; 2816181695Sattilio w->w_hash_next = w_hash.wh_array[hash]; 2817181695Sattilio w_hash.wh_array[hash] = w; 2818181695Sattilio w_hash.wh_count++; 2819181695Sattilio} 2820181695Sattilio 2821181695Sattilio 2822181695Sattiliostatic struct witness_lock_order_data * 2823181695Sattiliowitness_lock_order_get(struct witness *parent, struct witness *child) 2824181695Sattilio{ 2825181695Sattilio struct witness_lock_order_data *data = NULL; 2826181695Sattilio struct witness_lock_order_key key; 2827181695Sattilio unsigned int hash; 2828181695Sattilio 2829181695Sattilio MPASS(parent != NULL && child != NULL); 2830181695Sattilio key.from = parent->w_index; 2831181695Sattilio key.to = child->w_index; 2832181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2833181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2834181695Sattilio if ((w_rmatrix[parent->w_index][child->w_index] 2835181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) == 0) 2836181695Sattilio goto out; 2837181695Sattilio 2838181695Sattilio hash = witness_hash_djb2((const char*)&key, 2839181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2840181695Sattilio data = w_lohash.wloh_array[hash]; 2841181695Sattilio while (data != NULL) { 2842181695Sattilio if (witness_lock_order_key_equal(&data->wlod_key, &key)) 2843181695Sattilio break; 2844181695Sattilio data = data->wlod_next; 2845181695Sattilio } 2846181695Sattilio 2847181695Sattilioout: 2848181695Sattilio return (data); 2849181695Sattilio} 2850181695Sattilio 2851181695Sattilio/* 2852181695Sattilio * Verify that parent and child have a known relationship, are not the same, 2853181695Sattilio * and child is actually a child of parent. This is done without w_mtx 2854181695Sattilio * to avoid contention in the common case. 2855181695Sattilio */ 2856181695Sattiliostatic int 2857181695Sattiliowitness_lock_order_check(struct witness *parent, struct witness *child) 2858181695Sattilio{ 2859181695Sattilio 2860181695Sattilio if (parent != child && 2861181695Sattilio w_rmatrix[parent->w_index][child->w_index] 2862181695Sattilio & WITNESS_LOCK_ORDER_KNOWN && 2863181695Sattilio isitmychild(parent, child)) 2864181695Sattilio return (1); 2865181695Sattilio 2866181695Sattilio return (0); 2867181695Sattilio} 2868181695Sattilio 2869181695Sattiliostatic int 2870181695Sattiliowitness_lock_order_add(struct witness *parent, struct witness *child) 2871181695Sattilio{ 2872181695Sattilio struct witness_lock_order_data *data = NULL; 2873181695Sattilio struct witness_lock_order_key key; 2874181695Sattilio unsigned int hash; 2875181695Sattilio 2876181695Sattilio MPASS(parent != NULL && child != NULL); 2877181695Sattilio key.from = parent->w_index; 2878181695Sattilio key.to = child->w_index; 2879181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2880181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2881181695Sattilio if (w_rmatrix[parent->w_index][child->w_index] 2882181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) 2883181695Sattilio return (1); 2884181695Sattilio 2885181695Sattilio hash = witness_hash_djb2((const char*)&key, 2886181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2887181695Sattilio w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN; 2888181695Sattilio data = w_lofree; 2889181695Sattilio if (data == NULL) 2890181695Sattilio return (0); 2891181695Sattilio w_lofree = data->wlod_next; 2892181695Sattilio data->wlod_next = w_lohash.wloh_array[hash]; 2893181695Sattilio data->wlod_key = key; 2894181695Sattilio w_lohash.wloh_array[hash] = data; 2895181695Sattilio w_lohash.wloh_count++; 2896181695Sattilio stack_zero(&data->wlod_stack); 2897181695Sattilio stack_save(&data->wlod_stack); 2898181695Sattilio return (1); 2899181695Sattilio} 2900181695Sattilio 2901302234Sbdrewery/* Call this whenever the structure of the witness graph changes. */ 2902181695Sattiliostatic void 2903181695Sattiliowitness_increment_graph_generation(void) 2904181695Sattilio{ 2905181695Sattilio 2906181695Sattilio if (witness_cold == 0) 2907181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2908181695Sattilio w_generation++; 2909181695Sattilio} 2910181695Sattilio 2911181695Sattilio#ifdef KDB 2912181695Sattiliostatic void 2913181695Sattilio_witness_debugger(int cond, const char *msg) 2914181695Sattilio{ 2915181695Sattilio 2916181695Sattilio if (witness_trace && cond) 2917181695Sattilio kdb_backtrace(); 2918181695Sattilio if (witness_kdb && cond) 2919181695Sattilio kdb_enter(KDB_WHY_WITNESS, msg); 2920181695Sattilio} 2921181695Sattilio#endif 2922