165557Sjasone/*- 2181695Sattilio * Copyright (c) 2008 Isilon Systems, Inc. 3181695Sattilio * Copyright (c) 2008 Ilya Maykov <ivmaykov@gmail.com> 4181695Sattilio * Copyright (c) 1998 Berkeley Software Design, Inc. 5181695Sattilio * All rights reserved. 665557Sjasone * 765557Sjasone * Redistribution and use in source and binary forms, with or without 865557Sjasone * modification, are permitted provided that the following conditions 965557Sjasone * are met: 1065557Sjasone * 1. Redistributions of source code must retain the above copyright 1165557Sjasone * notice, this list of conditions and the following disclaimer. 1265557Sjasone * 2. Redistributions in binary form must reproduce the above copyright 1365557Sjasone * notice, this list of conditions and the following disclaimer in the 1465557Sjasone * documentation and/or other materials provided with the distribution. 1565557Sjasone * 3. Berkeley Software Design Inc's name may not be used to endorse or 1665557Sjasone * promote products derived from this software without specific prior 1765557Sjasone * written permission. 1865557Sjasone * 1965557Sjasone * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 2065557Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2165557Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2265557Sjasone * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 2365557Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2465557Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2565557Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2665557Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2765557Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2865557Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2965557Sjasone * SUCH DAMAGE. 3065557Sjasone * 3165557Sjasone * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 3267352Sjhb * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 3365557Sjasone */ 3465557Sjasone 3565557Sjasone/* 3674912Sjhb * Implementation of the `witness' lock verifier. Originally implemented for 3774912Sjhb * mutexes in BSD/OS. Extended to handle generic lock objects and lock 3874912Sjhb * classes in FreeBSD. 3972200Sbmilekic */ 4072200Sbmilekic 4172200Sbmilekic/* 4265557Sjasone * Main Entry: witness 4365557Sjasone * Pronunciation: 'wit-n&s 4465557Sjasone * Function: noun 4565557Sjasone * Etymology: Middle English witnesse, from Old English witnes knowledge, 4665557Sjasone * testimony, witness, from 2wit 4765557Sjasone * Date: before 12th century 4865557Sjasone * 1 : attestation of a fact or event : TESTIMONY 4965557Sjasone * 2 : one that gives evidence; specifically : one who testifies in 5065557Sjasone * a cause or before a judicial tribunal 5165557Sjasone * 3 : one asked to be present at a transaction so as to be able to 5265557Sjasone * testify to its having taken place 5365557Sjasone * 4 : one who has personal knowledge of something 5465557Sjasone * 5 a : something serving as evidence or proof : SIGN 5565557Sjasone * b : public affirmation by word or example of usually 5665557Sjasone * religious faith or conviction <the heroic witness to divine 5765557Sjasone * life -- Pilot> 5865557Sjasone * 6 capitalized : a member of the Jehovah's Witnesses 5965557Sjasone */ 6065557Sjasone 61111881Sjhb/* 62111881Sjhb * Special rules concerning Giant and lock orders: 63111881Sjhb * 64111881Sjhb * 1) Giant must be acquired before any other mutexes. Stated another way, 65111881Sjhb * no other mutex may be held when Giant is acquired. 66111881Sjhb * 67111881Sjhb * 2) Giant must be released when blocking on a sleepable lock. 68111881Sjhb * 69111881Sjhb * This rule is less obvious, but is a result of Giant providing the same 70111881Sjhb * semantics as spl(). Basically, when a thread sleeps, it must release 71111881Sjhb * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule 72111881Sjhb * 2). 73111881Sjhb * 74111881Sjhb * 3) Giant may be acquired before or after sleepable locks. 75111881Sjhb * 76111881Sjhb * This rule is also not quite as obvious. Giant may be acquired after 77111881Sjhb * a sleepable lock because it is a non-sleepable lock and non-sleepable 78111881Sjhb * locks may always be acquired while holding a sleepable lock. The second 79111881Sjhb * case, Giant before a sleepable lock, follows from rule 2) above. Suppose 80111881Sjhb * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1 81111881Sjhb * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and 82111881Sjhb * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to 83111881Sjhb * execute. Thus, acquiring Giant both before and after a sleepable lock 84111881Sjhb * will not result in a lock order reversal. 85111881Sjhb */ 86111881Sjhb 87116182Sobrien#include <sys/cdefs.h> 88116182Sobrien__FBSDID("$FreeBSD$"); 89116182Sobrien 9068790Sjhb#include "opt_ddb.h" 91168856Sjkoshy#include "opt_hwpmc_hooks.h" 92181695Sattilio#include "opt_stack.h" 9367676Sjhb#include "opt_witness.h" 9467676Sjhb 9565557Sjasone#include <sys/param.h> 9667352Sjhb#include <sys/bus.h> 97131930Smarcel#include <sys/kdb.h> 9867352Sjhb#include <sys/kernel.h> 9974912Sjhb#include <sys/ktr.h> 10074912Sjhb#include <sys/lock.h> 10167352Sjhb#include <sys/malloc.h> 10274912Sjhb#include <sys/mutex.h> 103164033Srwatson#include <sys/priv.h> 10465557Sjasone#include <sys/proc.h> 105178841Sattilio#include <sys/sbuf.h> 106183955Sattilio#include <sys/sched.h> 107181695Sattilio#include <sys/stack.h> 10867676Sjhb#include <sys/sysctl.h> 10965557Sjasone#include <sys/systm.h> 11065557Sjasone 111181695Sattilio#ifdef DDB 11268790Sjhb#include <ddb/ddb.h> 113181695Sattilio#endif 11468790Sjhb 115111881Sjhb#include <machine/stdarg.h> 116111881Sjhb 117181695Sattilio#if !defined(DDB) && !defined(STACK) 118181695Sattilio#error "DDB or STACK options are required for WITNESS" 119181695Sattilio#endif 120181695Sattilio 121154818Sjhb/* Note that these traces do not work with KTR_ALQ. */ 122154790Sjhb#if 0 123154790Sjhb#define KTR_WITNESS KTR_SUBSYS 124154790Sjhb#else 125154790Sjhb#define KTR_WITNESS 0 126154790Sjhb#endif 127154790Sjhb 128178165Sattilio#define LI_RECURSEMASK 0x0000ffff /* Recursion depth of lock instance. */ 129178165Sattilio#define LI_EXCLUSIVE 0x00010000 /* Exclusive lock instance. */ 130187511Sthompsa#define LI_NORELEASE 0x00020000 /* Lock not allowed to be released. */ 131178165Sattilio 132105508Sphk/* Define this to check for blessed mutexes */ 133105508Sphk#undef BLESSING 134105508Sphk 135181695Sattilio#define WITNESS_COUNT 1024 136181695Sattilio#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 137181695Sattilio#define WITNESS_HASH_SIZE 251 /* Prime, gives load factor < 2 */ 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 /* 494130022Srwatson * Sockets 495130022Srwatson */ 496130022Srwatson { "accept", &lock_class_mtx_sleep }, 497130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 498130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 499130022Srwatson { "sellck", &lock_class_mtx_sleep }, 500130022Srwatson { NULL, NULL }, 501130022Srwatson /* 502130022Srwatson * Routing 503130022Srwatson */ 504130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 505185747Skmacy { "radix node head", &lock_class_rw }, 506130022Srwatson { "rtentry", &lock_class_mtx_sleep }, 507130022Srwatson { "ifaddr", &lock_class_mtx_sleep }, 508130022Srwatson { NULL, NULL }, 509130022Srwatson /* 510191672Sbms * IPv4 multicast: 511191672Sbms * protocol locks before interface locks, after UDP locks. 512148682Srwatson */ 513178285Srwatson { "udpinp", &lock_class_rw }, 514148682Srwatson { "in_multi_mtx", &lock_class_mtx_sleep }, 515148682Srwatson { "igmp_mtx", &lock_class_mtx_sleep }, 516229873Sjhb { "if_addr_lock", &lock_class_rw }, 517148682Srwatson { NULL, NULL }, 518148682Srwatson /* 519191672Sbms * IPv6 multicast: 520191672Sbms * protocol locks before interface locks, after UDP locks. 521191672Sbms */ 522191672Sbms { "udpinp", &lock_class_rw }, 523191672Sbms { "in6_multi_mtx", &lock_class_mtx_sleep }, 524191672Sbms { "mld_mtx", &lock_class_mtx_sleep }, 525229873Sjhb { "if_addr_lock", &lock_class_rw }, 526191672Sbms { NULL, NULL }, 527191672Sbms /* 528130022Srwatson * UNIX Domain Sockets 529130396Srwatson */ 530189544Srwatson { "unp_global_rwlock", &lock_class_rw }, 531189544Srwatson { "unp_list_lock", &lock_class_mtx_sleep }, 532130396Srwatson { "unp", &lock_class_mtx_sleep }, 533130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 534130031Sjhb { NULL, NULL }, 535130022Srwatson /* 536130022Srwatson * UDP/IP 537130022Srwatson */ 538178285Srwatson { "udp", &lock_class_rw }, 539178285Srwatson { "udpinp", &lock_class_rw }, 540130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 541130022Srwatson { NULL, NULL }, 542130022Srwatson /* 543130022Srwatson * TCP/IP 544130022Srwatson */ 545178285Srwatson { "tcp", &lock_class_rw }, 546178285Srwatson { "tcpinp", &lock_class_rw }, 547130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 548130022Srwatson { NULL, NULL }, 549130022Srwatson /* 550132639Srwatson * netatalk 551132639Srwatson */ 552132639Srwatson { "ddp_list_mtx", &lock_class_mtx_sleep }, 553132639Srwatson { "ddp_mtx", &lock_class_mtx_sleep }, 554132639Srwatson { NULL, NULL }, 555132639Srwatson /* 556134971Srwatson * BPF 557134971Srwatson */ 558134971Srwatson { "bpf global lock", &lock_class_mtx_sleep }, 559233937Smelifaro { "bpf interface lock", &lock_class_rw }, 560235745Smelifaro { "bpf cdev lock", &lock_class_mtx_sleep }, 561144832Spjd { NULL, NULL }, 562143335Srwatson /* 563143335Srwatson * NFS server 564143335Srwatson */ 565143335Srwatson { "nfsd_mtx", &lock_class_mtx_sleep }, 566143335Srwatson { "so_snd", &lock_class_mtx_sleep }, 567134971Srwatson { NULL, NULL }, 568170530Ssam 569134971Srwatson /* 570170530Ssam * IEEE 802.11 571170530Ssam */ 572170530Ssam { "802.11 com lock", &lock_class_mtx_sleep}, 573170530Ssam { NULL, NULL }, 574170530Ssam /* 575170530Ssam * Network drivers 576170530Ssam */ 577170530Ssam { "network driver", &lock_class_mtx_sleep}, 578170530Ssam { NULL, NULL }, 579170530Ssam 580170530Ssam /* 581168217Swkoszek * Netgraph 582168217Swkoszek */ 583168217Swkoszek { "ng_node", &lock_class_mtx_sleep }, 584168217Swkoszek { "ng_worklist", &lock_class_mtx_sleep }, 585168217Swkoszek { NULL, NULL }, 586168217Swkoszek /* 587144836Spjd * CDEV 588144836Spjd */ 589237623Salc { "vm map (system)", &lock_class_mtx_sleep }, 590237623Salc { "vm page queue", &lock_class_mtx_sleep }, 591145425Sjeff { "vnode interlock", &lock_class_mtx_sleep }, 592144836Spjd { "cdev", &lock_class_mtx_sleep }, 593144836Spjd { NULL, NULL }, 594144836Spjd /* 595207410Skmacy * VM 596207410Skmacy */ 597237623Salc { "vm map (user)", &lock_class_sx }, 598248093Skib { "vm object", &lock_class_rw }, 599237623Salc { "vm page", &lock_class_mtx_sleep }, 600237623Salc { "vm page queue", &lock_class_mtx_sleep }, 601237623Salc { "pmap pv global", &lock_class_rw }, 602207410Skmacy { "pmap", &lock_class_mtx_sleep }, 603237623Salc { "pmap pv list", &lock_class_rw }, 604237623Salc { "vm page free queue", &lock_class_mtx_sleep }, 605207410Skmacy { NULL, NULL }, 606207410Skmacy /* 607166421Skib * kqueue/VFS interaction 608166421Skib */ 609166421Skib { "kqueue", &lock_class_mtx_sleep }, 610166421Skib { "struct mount mtx", &lock_class_mtx_sleep }, 611166421Skib { "vnode interlock", &lock_class_mtx_sleep }, 612166421Skib { NULL, NULL }, 613166421Skib /* 614192416Skmacy * ZFS locking 615192416Skmacy */ 616192416Skmacy { "dn->dn_mtx", &lock_class_sx }, 617192416Skmacy { "dr->dt.di.dr_mtx", &lock_class_sx }, 618192416Skmacy { "db->db_mtx", &lock_class_sx }, 619192416Skmacy { NULL, NULL }, 620192416Skmacy /* 62175464Sjhb * spin locks 62275464Sjhb */ 62384331Sjhb#ifdef SMP 62484331Sjhb { "ap boot", &lock_class_mtx_spin }, 62572224Sjhb#endif 626150582Sjhb { "rm.mutex_mtx", &lock_class_mtx_spin }, 62774912Sjhb { "sio", &lock_class_mtx_spin }, 628173877Sattilio { "scrlock", &lock_class_mtx_spin }, 62972224Sjhb#ifdef __i386__ 63074912Sjhb { "cy", &lock_class_mtx_spin }, 63172224Sjhb#endif 632170848Smarius#ifdef __sparc64__ 633170848Smarius { "pcib_mtx", &lock_class_mtx_spin }, 634170848Smarius { "rtc_mtx", &lock_class_mtx_spin }, 635170848Smarius#endif 636157584Smarcel { "scc_hwmtx", &lock_class_mtx_spin }, 637124972Sru { "uart_hwmtx", &lock_class_mtx_spin }, 638161638Sssouhlal { "fast_taskqueue", &lock_class_mtx_spin }, 639122001Sjhb { "intr table", &lock_class_mtx_spin }, 640168856Sjkoshy#ifdef HWPMC_HOOKS 641168856Sjkoshy { "pmc-per-proc", &lock_class_mtx_spin }, 642168856Sjkoshy#endif 643170302Sjeff { "process slock", &lock_class_mtx_spin }, 644126324Sjhb { "sleepq chain", &lock_class_mtx_spin }, 645170302Sjeff { "umtx lock", &lock_class_mtx_spin }, 646173877Sattilio { "rm_spinlock", &lock_class_mtx_spin }, 647170302Sjeff { "turnstile chain", &lock_class_mtx_spin }, 648170302Sjeff { "turnstile lock", &lock_class_mtx_spin }, 64974912Sjhb { "sched lock", &lock_class_mtx_spin }, 650122514Sjhb { "td_contested", &lock_class_mtx_spin }, 65174912Sjhb { "callout", &lock_class_mtx_spin }, 652136374Srwatson { "entropy harvest mutex", &lock_class_mtx_spin }, 653162285Sscottl { "syscons video lock", &lock_class_mtx_spin }, 654172256Sattilio#ifdef SMP 655172256Sattilio { "smp rendezvous", &lock_class_mtx_spin }, 656172256Sattilio#endif 657176771Sraj#ifdef __powerpc__ 658176771Sraj { "tlb0", &lock_class_mtx_spin }, 659176771Sraj#endif 66065557Sjasone /* 66165557Sjasone * leaf locks 66265557Sjasone */ 663178149Sattilio { "intrcnt", &lock_class_mtx_spin }, 66488322Sjhb { "icu", &lock_class_mtx_spin }, 665172256Sattilio#ifdef __i386__ 666172256Sattilio { "allpmaps", &lock_class_mtx_spin }, 667172256Sattilio { "descriptor tables", &lock_class_mtx_spin }, 668108187Sjake#endif 66978785Sjhb { "clk", &lock_class_mtx_spin }, 670178149Sattilio { "cpuset", &lock_class_mtx_spin }, 671172256Sattilio { "mprof lock", &lock_class_mtx_spin }, 672170302Sjeff { "zombie lock", &lock_class_mtx_spin }, 673103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 674104951Speter#ifdef __ia64__ 675104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 676104951Speter#endif 677115425Speter#if defined(__i386__) || defined(__amd64__) 678111068Speter { "pcicfg", &lock_class_mtx_spin }, 679143204Swpaul { "NDIS thread lock", &lock_class_mtx_spin }, 680111068Speter#endif 681144966Svkashyap { "tw_osl_io_lock", &lock_class_mtx_spin }, 682144966Svkashyap { "tw_osl_q_lock", &lock_class_mtx_spin }, 683144966Svkashyap { "tw_cl_io_lock", &lock_class_mtx_spin }, 684144966Svkashyap { "tw_cl_intr_lock", &lock_class_mtx_spin }, 685144966Svkashyap { "tw_cl_gen_lock", &lock_class_mtx_spin }, 686168856Sjkoshy#ifdef HWPMC_HOOKS 687168856Sjkoshy { "pmc-leaf", &lock_class_mtx_spin }, 688168856Sjkoshy#endif 689170302Sjeff { "blocked lock", &lock_class_mtx_spin }, 69074912Sjhb { NULL, NULL }, 69174912Sjhb { NULL, NULL } 69265557Sjasone}; 69365557Sjasone 694105508Sphk#ifdef BLESSING 69565557Sjasone/* 69665557Sjasone * Pairs of locks which have been blessed 69765557Sjasone * Don't complain about order problems with blessed locks 69865557Sjasone */ 69965856Sjhbstatic struct witness_blessed blessed_list[] = { 70065557Sjasone}; 70172200Sbmilekicstatic int blessed_count = 70272200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 703105508Sphk#endif 70465557Sjasone 70574912Sjhb/* 70674912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 70774912Sjhb */ 70874912Sjhbstatic int witness_cold = 1; 70974912Sjhb 71074912Sjhb/* 711151629Sjhb * This global is set to 1 once the static lock orders have been enrolled 712151629Sjhb * so that a warning can be issued for any spin locks enrolled later. 713151629Sjhb */ 714151629Sjhbstatic int witness_spin_warn = 0; 715151629Sjhb 716226793Sjhb/* Trim useless garbage from filenames. */ 717226793Sjhbstatic const char * 718226793Sjhbfixup_filename(const char *file) 719226793Sjhb{ 720226793Sjhb 721226793Sjhb if (file == NULL) 722226793Sjhb return (NULL); 723226793Sjhb while (strncmp(file, "../", 3) == 0) 724226793Sjhb file += 3; 725226793Sjhb return (file); 726226793Sjhb} 727226793Sjhb 728151629Sjhb/* 729153133Sjhb * The WITNESS-enabled diagnostic code. Note that the witness code does 730153133Sjhb * assume that the early boot is single-threaded at least until after this 731153133Sjhb * routine is completed. 73274912Sjhb */ 73371352Sjasonestatic void 73474912Sjhbwitness_initialize(void *dummy __unused) 73565557Sjasone{ 73674912Sjhb struct lock_object *lock; 73774912Sjhb struct witness_order_list_entry *order; 73874912Sjhb struct witness *w, *w1; 73974912Sjhb int i; 74065557Sjasone 741184214Sdes w_data = malloc(sizeof (struct witness) * WITNESS_COUNT, M_WITNESS, 742181695Sattilio M_NOWAIT | M_ZERO); 743181695Sattilio 74474912Sjhb /* 74574912Sjhb * We have to release Giant before initializing its witness 74674912Sjhb * structure so that WITNESS doesn't get confused. 74774912Sjhb */ 74874912Sjhb mtx_unlock(&Giant); 74974912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 75074912Sjhb 75187593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 75293811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 753164159Skmacy MTX_NOWITNESS | MTX_NOPROFILE); 754181695Sattilio for (i = WITNESS_COUNT - 1; i >= 0; i--) { 755181695Sattilio w = &w_data[i]; 756181695Sattilio memset(w, 0, sizeof(*w)); 757181695Sattilio w_data[i].w_index = i; /* Witness index never changes. */ 758181695Sattilio witness_free(w); 759181695Sattilio } 760181695Sattilio KASSERT(STAILQ_FIRST(&w_free)->w_index == 0, 761181695Sattilio ("%s: Invalid list of free witness objects", __func__)); 762181695Sattilio 763181695Sattilio /* Witness with index 0 is not used to aid in debugging. */ 764181695Sattilio STAILQ_REMOVE_HEAD(&w_free, w_list); 765181695Sattilio w_free_cnt--; 766181695Sattilio 767181695Sattilio memset(w_rmatrix, 0, 768181695Sattilio (sizeof(**w_rmatrix) * (WITNESS_COUNT+1) * (WITNESS_COUNT+1))); 769181695Sattilio 77074912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 77174912Sjhb witness_lock_list_free(&w_locklistdata[i]); 772181695Sattilio witness_init_hash_tables(); 77374912Sjhb 77474912Sjhb /* First add in all the specified order lists. */ 77574912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 77674912Sjhb w = enroll(order->w_name, order->w_class); 77775569Sjhb if (w == NULL) 77875569Sjhb continue; 77974912Sjhb w->w_file = "order list"; 78074912Sjhb for (order++; order->w_name != NULL; order++) { 78174912Sjhb w1 = enroll(order->w_name, order->w_class); 78275569Sjhb if (w1 == NULL) 78375569Sjhb continue; 78474912Sjhb w1->w_file = "order list"; 785181695Sattilio itismychild(w, w1); 78674912Sjhb w = w1; 78765557Sjasone } 78865557Sjasone } 789151629Sjhb witness_spin_warn = 1; 79065557Sjasone 79174912Sjhb /* Iterate through all locks and add them to witness. */ 792179025Sattilio for (i = 0; pending_locks[i].wh_lock != NULL; i++) { 793179025Sattilio lock = pending_locks[i].wh_lock; 794153133Sjhb KASSERT(lock->lo_flags & LO_WITNESS, 795153133Sjhb ("%s: lock %s is on pending list but not LO_WITNESS", 796153133Sjhb __func__, lock->lo_name)); 797179025Sattilio lock->lo_witness = enroll(pending_locks[i].wh_type, 798179025Sattilio LOCK_CLASS(lock)); 79974912Sjhb } 80074912Sjhb 80174912Sjhb /* Mark the witness code as being ready for use. */ 802151629Sjhb witness_cold = 0; 80374912Sjhb 80474912Sjhb mtx_lock(&Giant); 80565557Sjasone} 806177253SrwatsonSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, 807177253Srwatson NULL); 80865557Sjasone 80974912Sjhbvoid 810179025Sattiliowitness_init(struct lock_object *lock, const char *type) 81174912Sjhb{ 81274912Sjhb struct lock_class *class; 81374912Sjhb 814153133Sjhb /* Various sanity checks. */ 815154077Sjhb class = LOCK_CLASS(lock); 81674912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 81774912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 818244105Salfred kassert_panic("%s: lock (%s) %s can not be recursable", 819244105Salfred __func__, class->lc_name, lock->lo_name); 82074912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 82174912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 822244105Salfred kassert_panic("%s: lock (%s) %s can not be sleepable", 823244105Salfred __func__, class->lc_name, lock->lo_name); 82482244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 82582244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 826244105Salfred kassert_panic("%s: lock (%s) %s can not be upgradable", 827244105Salfred __func__, class->lc_name, lock->lo_name); 82882244Sjhb 829153133Sjhb /* 830153133Sjhb * If we shouldn't watch this lock, then just clear lo_witness. 831153133Sjhb * Otherwise, if witness_cold is set, then it is too early to 832153133Sjhb * enroll this lock, so defer it to witness_initialize() by adding 833153133Sjhb * it to the pending_locks list. If it is not too early, then enroll 834153133Sjhb * the lock now. 835153133Sjhb */ 836182446Sattilio if (witness_watch < 1 || panicstr != NULL || 837153133Sjhb (lock->lo_flags & LO_WITNESS) == 0) 838153133Sjhb lock->lo_witness = NULL; 839153133Sjhb else if (witness_cold) { 840179025Sattilio pending_locks[pending_cnt].wh_lock = lock; 841179025Sattilio pending_locks[pending_cnt++].wh_type = type; 842179025Sattilio if (pending_cnt > WITNESS_PENDLIST) 843244105Salfred panic("%s: pending locks list is too small, " 844244105Salfred "increase WITNESS_PENDLIST\n", 845179025Sattilio __func__); 846153133Sjhb } else 847179025Sattilio lock->lo_witness = enroll(type, class); 84874912Sjhb} 84974912Sjhb 85074912Sjhbvoid 85174912Sjhbwitness_destroy(struct lock_object *lock) 85274912Sjhb{ 853154077Sjhb struct lock_class *class; 85475362Sjhb struct witness *w; 85574912Sjhb 856154077Sjhb class = LOCK_CLASS(lock); 857181695Sattilio 85874912Sjhb if (witness_cold) 85974912Sjhb panic("lock (%s) %s destroyed while witness_cold", 860154077Sjhb class->lc_name, lock->lo_name); 86174912Sjhb 86276272Sjhb /* XXX: need to verify that no one holds the lock */ 863181695Sattilio if ((lock->lo_flags & LO_WITNESS) == 0 || lock->lo_witness == NULL) 864181695Sattilio return; 865181695Sattilio w = lock->lo_witness; 866112117Sjhb 867181695Sattilio mtx_lock_spin(&w_mtx); 868181695Sattilio MPASS(w->w_refcount > 0); 869181695Sattilio w->w_refcount--; 870181695Sattilio 871181695Sattilio if (w->w_refcount == 0) 872181695Sattilio depart(w); 873181695Sattilio mtx_unlock_spin(&w_mtx); 87474912Sjhb} 87574912Sjhb 876112115Sjhb#ifdef DDB 87771352Sjasonestatic void 878181695Sattiliowitness_ddb_compute_levels(void) 879149979Struckman{ 880181695Sattilio struct witness *w; 881149979Struckman 882149979Struckman /* 883149979Struckman * First clear all levels. 884149979Struckman */ 885181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 886181695Sattilio w->w_ddb_level = -1; 887149979Struckman 888149979Struckman /* 889181695Sattilio * Look for locks with no parents and level all their descendants. 890149979Struckman */ 891149979Struckman STAILQ_FOREACH(w, &w_all, w_list) { 892181695Sattilio 893181695Sattilio /* If the witness has ancestors (is not a root), skip it. */ 894181695Sattilio if (w->w_num_ancestors > 0) 895181695Sattilio continue; 896181695Sattilio witness_ddb_level_descendants(w, 0); 897149979Struckman } 898149979Struckman} 899149979Struckman 900149979Struckmanstatic void 901181695Sattiliowitness_ddb_level_descendants(struct witness *w, int l) 902149979Struckman{ 903149979Struckman int i; 904149979Struckman 905181695Sattilio if (w->w_ddb_level >= l) 906181695Sattilio return; 907181695Sattilio 908181695Sattilio w->w_ddb_level = l; 909181695Sattilio l++; 910181695Sattilio 911181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 912181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 913181695Sattilio witness_ddb_level_descendants(&w_data[i], l); 914181695Sattilio } 915149979Struckman} 916149979Struckman 917149979Struckmanstatic void 918207922Sattiliowitness_ddb_display_descendants(int(*prnt)(const char *fmt, ...), 919181695Sattilio struct witness *w, int indent) 920149979Struckman{ 921181695Sattilio int i; 922149979Struckman 923181695Sattilio for (i = 0; i < indent; i++) 924181695Sattilio prnt(" "); 925181695Sattilio prnt("%s (type: %s, depth: %d, active refs: %d)", 926181695Sattilio w->w_name, w->w_class->lc_name, 927181695Sattilio w->w_ddb_level, w->w_refcount); 928181695Sattilio if (w->w_displayed) { 929181695Sattilio prnt(" -- (already displayed)\n"); 930181695Sattilio return; 931181695Sattilio } 932181695Sattilio w->w_displayed = 1; 933181695Sattilio if (w->w_file != NULL && w->w_line != 0) 934226793Sjhb prnt(" -- last acquired @ %s:%d\n", fixup_filename(w->w_file), 935181695Sattilio w->w_line); 936149979Struckman else 937181695Sattilio prnt(" -- never acquired\n"); 938181695Sattilio indent++; 939181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 940181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 941239584Sjhb if (db_pager_quit) 942239584Sjhb return; 943181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) 944181695Sattilio witness_ddb_display_descendants(prnt, &w_data[i], 945181695Sattilio indent); 946149979Struckman } 947149979Struckman} 948149979Struckman 949149979Struckmanstatic void 950207922Sattiliowitness_ddb_display_list(int(*prnt)(const char *fmt, ...), 951181695Sattilio struct witness_list *list) 95271352Sjasone{ 953112118Sjhb struct witness *w; 95471352Sjasone 95574912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 956181695Sattilio if (w->w_file == NULL || w->w_ddb_level > 0) 95771352Sjasone continue; 958181695Sattilio 959181695Sattilio /* This lock has no anscestors - display its descendants. */ 960181695Sattilio witness_ddb_display_descendants(prnt, w, 0); 961239584Sjhb if (db_pager_quit) 962239584Sjhb return; 96371352Sjasone } 96474912Sjhb} 96572224Sjhb 96674912Sjhbstatic void 967207922Sattiliowitness_ddb_display(int(*prnt)(const char *fmt, ...)) 968178841Sattilio{ 96974912Sjhb struct witness *w; 97074912Sjhb 971181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 972181695Sattilio witness_ddb_compute_levels(); 97374912Sjhb 974112118Sjhb /* Clear all the displayed flags. */ 975181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 976112118Sjhb w->w_displayed = 0; 977112118Sjhb 97872224Sjhb /* 97974930Sjhb * First, handle sleep locks which have been acquired at least 98074912Sjhb * once. 98174912Sjhb */ 98274912Sjhb prnt("Sleep locks:\n"); 983181695Sattilio witness_ddb_display_list(prnt, &w_sleep); 984239584Sjhb if (db_pager_quit) 985239584Sjhb return; 98674912Sjhb 98774912Sjhb /* 98874930Sjhb * Now do spin locks which have been acquired at least once. 98972224Sjhb */ 99074912Sjhb prnt("\nSpin locks:\n"); 991181695Sattilio witness_ddb_display_list(prnt, &w_spin); 992239584Sjhb if (db_pager_quit) 993239584Sjhb return; 99472224Sjhb 99572224Sjhb /* 99674930Sjhb * Finally, any locks which have not been acquired yet. 99772224Sjhb */ 99874912Sjhb prnt("\nLocks which were never acquired:\n"); 99974912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 100097948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 100171352Sjasone continue; 1002181695Sattilio prnt("%s (type: %s, depth: %d)\n", w->w_name, 1003181695Sattilio w->w_class->lc_name, w->w_ddb_level); 1004239584Sjhb if (db_pager_quit) 1005239584Sjhb return; 100671352Sjasone } 100771352Sjasone} 1008112115Sjhb#endif /* DDB */ 100971352Sjasone 1010125160Sjhbint 1011125160Sjhbwitness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 1012125160Sjhb{ 1013125160Sjhb 1014182473Sattilio if (witness_watch == -1 || panicstr != NULL) 1015125160Sjhb return (0); 1016125160Sjhb 1017125160Sjhb /* Require locks that witness knows about. */ 1018125160Sjhb if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 1019125160Sjhb lock2->lo_witness == NULL) 1020125160Sjhb return (EINVAL); 1021125160Sjhb 1022181695Sattilio mtx_assert(&w_mtx, MA_NOTOWNED); 1023125160Sjhb mtx_lock_spin(&w_mtx); 1024125160Sjhb 1025125160Sjhb /* 1026125160Sjhb * If we already have either an explicit or implied lock order that 1027125160Sjhb * is the other way around, then return an error. 1028125160Sjhb */ 1029182473Sattilio if (witness_watch && 1030182473Sattilio isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 1031125160Sjhb mtx_unlock_spin(&w_mtx); 1032125160Sjhb return (EDOOFUS); 1033125160Sjhb } 1034125160Sjhb 1035125160Sjhb /* Try to add the new order. */ 1036125160Sjhb CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1037179025Sattilio lock2->lo_witness->w_name, lock1->lo_witness->w_name); 1038181695Sattilio itismychild(lock1->lo_witness, lock2->lo_witness); 1039125160Sjhb mtx_unlock_spin(&w_mtx); 1040125160Sjhb return (0); 1041125160Sjhb} 1042125160Sjhb 104365557Sjasonevoid 1044125160Sjhbwitness_checkorder(struct lock_object *lock, int flags, const char *file, 1045182914Sjhb int line, struct lock_object *interlock) 104665557Sjasone{ 1047183955Sattilio struct lock_list_entry *lock_list, *lle; 1048182914Sjhb struct lock_instance *lock1, *lock2, *plock; 1049251326Sjhb struct lock_class *class, *iclass; 105065856Sjhb struct witness *w, *w1; 105183366Sjulian struct thread *td; 105274912Sjhb int i, j; 105365557Sjasone 1054182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL || 105580747Sjhb panicstr != NULL) 105671320Sjasone return; 1057125160Sjhb 105874912Sjhb w = lock->lo_witness; 1059154077Sjhb class = LOCK_CLASS(lock); 106083366Sjulian td = curthread; 106165557Sjasone 106274912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 1063181695Sattilio 106493676Sjhb /* 106593676Sjhb * Since spin locks include a critical section, this check 1066131884Sjhb * implicitly enforces a lock order of all sleep locks before 106793676Sjhb * all spin locks. 106893676Sjhb */ 1069136304Sgreen if (td->td_critnest != 0 && !kdb_active) 1070244105Salfred kassert_panic("acquiring blockable sleep lock with " 1071244105Salfred "spinlock or critical section held (%s) %s @ %s:%d", 1072226294Sadrian class->lc_name, lock->lo_name, 1073226294Sadrian fixup_filename(file), line); 1074131884Sjhb 1075131884Sjhb /* 1076131884Sjhb * If this is the first lock acquired then just return as 1077131884Sjhb * no order checking is needed. 1078131884Sjhb */ 1079183955Sattilio lock_list = td->td_sleeplocks; 1080183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) 1081131884Sjhb return; 1082131884Sjhb } else { 1083181695Sattilio 1084131884Sjhb /* 1085131884Sjhb * If this is the first lock, just return as no order 1086183955Sattilio * checking is needed. Avoid problems with thread 1087183955Sattilio * migration pinning the thread while checking if 1088183955Sattilio * spinlocks are held. If at least one spinlock is held 1089183955Sattilio * the thread is in a safe path and it is allowed to 1090183955Sattilio * unpin it. 1091131884Sjhb */ 1092183955Sattilio sched_pin(); 1093183955Sattilio lock_list = PCPU_GET(spinlocks); 1094183955Sattilio if (lock_list == NULL || lock_list->ll_count == 0) { 1095183955Sattilio sched_unpin(); 1096131884Sjhb return; 1097183955Sattilio } 1098183955Sattilio sched_unpin(); 1099131884Sjhb } 110065557Sjasone 110176772Sjhb /* 1102125160Sjhb * Check to see if we are recursing on a lock we already own. If 1103125160Sjhb * so, make sure that we don't mismatch exclusive and shared lock 1104125160Sjhb * acquires. 110576272Sjhb */ 1106183955Sattilio lock1 = find_instance(lock_list, lock); 110776272Sjhb if (lock1 != NULL) { 110876272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 110976272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 111076272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 1111226294Sadrian class->lc_name, lock->lo_name, 1112226294Sadrian fixup_filename(file), line); 111376272Sjhb printf("while exclusively locked from %s:%d\n", 1114226294Sadrian fixup_filename(lock1->li_file), lock1->li_line); 1115251326Sjhb kassert_panic("excl->share"); 111676272Sjhb } 111776272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 111876272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 111976272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 1120226294Sadrian class->lc_name, lock->lo_name, 1121226294Sadrian fixup_filename(file), line); 112276272Sjhb printf("while share locked from %s:%d\n", 1123226294Sadrian fixup_filename(lock1->li_file), lock1->li_line); 1124251326Sjhb kassert_panic("share->excl"); 112576272Sjhb } 112676272Sjhb return; 112776272Sjhb } 112876272Sjhb 1129251326Sjhb /* Warn if the interlock is not locked exactly once. */ 1130251326Sjhb if (interlock != NULL) { 1131251326Sjhb iclass = LOCK_CLASS(interlock); 1132251326Sjhb lock1 = find_instance(lock_list, interlock); 1133251326Sjhb if (lock1 == NULL) 1134255205Sjhb kassert_panic("interlock (%s) %s not locked @ %s:%d", 1135251326Sjhb iclass->lc_name, interlock->lo_name, 1136251326Sjhb fixup_filename(file), line); 1137251326Sjhb else if ((lock1->li_flags & LI_RECURSEMASK) != 0) 1138255205Sjhb kassert_panic("interlock (%s) %s recursed @ %s:%d", 1139251326Sjhb iclass->lc_name, interlock->lo_name, 1140251326Sjhb fixup_filename(file), line); 1141251326Sjhb } 1142251326Sjhb 114376272Sjhb /* 1144182914Sjhb * Find the previously acquired lock, but ignore interlocks. 1145182914Sjhb */ 1146183955Sattilio plock = &lock_list->ll_children[lock_list->ll_count - 1]; 1147182914Sjhb if (interlock != NULL && plock->li_lock == interlock) { 1148183955Sattilio if (lock_list->ll_count > 1) 1149183955Sattilio plock = 1150183955Sattilio &lock_list->ll_children[lock_list->ll_count - 2]; 1151183955Sattilio else { 1152183955Sattilio lle = lock_list->ll_next; 1153182984Sattilio 1154182914Sjhb /* 1155182914Sjhb * The interlock is the only lock we hold, so 1156183955Sattilio * simply return. 1157182914Sjhb */ 1158183955Sattilio if (lle == NULL) 1159183955Sattilio return; 1160183955Sattilio plock = &lle->ll_children[lle->ll_count - 1]; 1161182914Sjhb } 1162182914Sjhb } 1163182914Sjhb 1164182914Sjhb /* 1165181695Sattilio * Try to perform most checks without a lock. If this succeeds we 1166181695Sattilio * can skip acquiring the lock and return success. 1167181695Sattilio */ 1168182914Sjhb w1 = plock->li_lock->lo_witness; 1169181695Sattilio if (witness_lock_order_check(w1, w)) 1170181695Sattilio return; 1171181695Sattilio 1172181695Sattilio /* 117374912Sjhb * Check for duplicate locks of the same type. Note that we only 117474912Sjhb * have to check for this on the last lock we just acquired. Any 117574912Sjhb * other cases will be caught as lock order violations. 117674912Sjhb */ 1177181695Sattilio mtx_lock_spin(&w_mtx); 1178181695Sattilio witness_lock_order_add(w1, w); 117974912Sjhb if (w1 == w) { 1180181695Sattilio i = w->w_index; 1181181695Sattilio if (!(lock->lo_flags & LO_DUPOK) && !(flags & LOP_DUPOK) && 1182181695Sattilio !(w_rmatrix[i][i] & WITNESS_REVERSAL)) { 1183181695Sattilio w_rmatrix[i][i] |= WITNESS_REVERSAL; 1184181695Sattilio w->w_reversed = 1; 1185181695Sattilio mtx_unlock_spin(&w_mtx); 1186183574Sjhb printf( 1187183574Sjhb "acquiring duplicate lock of same type: \"%s\"\n", 1188181695Sattilio w->w_name); 1189183574Sjhb printf(" 1st %s @ %s:%d\n", plock->li_lock->lo_name, 1190226793Sjhb fixup_filename(plock->li_file), plock->li_line); 1191226294Sadrian printf(" 2nd %s @ %s:%d\n", lock->lo_name, 1192226294Sadrian fixup_filename(file), line); 1193181695Sattilio witness_debugger(1); 1194226793Sjhb } else 1195226793Sjhb mtx_unlock_spin(&w_mtx); 1196125160Sjhb return; 119765557Sjasone } 1198181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1199181695Sattilio 120065557Sjasone /* 1201218909Sbrucec * If we know that the lock we are acquiring comes after 1202111881Sjhb * the lock we most recently acquired in the lock order tree, 1203111881Sjhb * then there is no need for any further checks. 1204111881Sjhb */ 1205181695Sattilio if (isitmychild(w1, w)) 1206181695Sattilio goto out; 1207181695Sattilio 1208183955Sattilio for (j = 0, lle = lock_list; lle != NULL; lle = lle->ll_next) { 120974912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 121065557Sjasone 121174912Sjhb MPASS(j < WITNESS_COUNT); 121276272Sjhb lock1 = &lle->ll_children[i]; 121374912Sjhb 121474912Sjhb /* 1215251326Sjhb * Ignore the interlock. 1216182914Sjhb */ 1217251326Sjhb if (interlock == lock1->li_lock) 1218182914Sjhb continue; 1219182914Sjhb 1220182914Sjhb /* 122174912Sjhb * If this lock doesn't undergo witness checking, 122274912Sjhb * then skip it. 122374912Sjhb */ 1224182914Sjhb w1 = lock1->li_lock->lo_witness; 122574912Sjhb if (w1 == NULL) { 122676272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 122774912Sjhb ("lock missing witness structure")); 122874912Sjhb continue; 122974912Sjhb } 1230181695Sattilio 123176272Sjhb /* 1232111881Sjhb * If we are locking Giant and this is a sleepable 123376272Sjhb * lock, then skip it. 123476272Sjhb */ 1235111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 1236167787Sjhb lock == &Giant.lock_object) 123776272Sjhb continue; 1238181695Sattilio 123993690Sjhb /* 124093690Sjhb * If we are locking a sleepable lock and this lock 1241111881Sjhb * is Giant, then skip it. 124293690Sjhb */ 1243111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 1244167787Sjhb lock1->li_lock == &Giant.lock_object) 1245111881Sjhb continue; 1246181695Sattilio 1247111881Sjhb /* 1248111881Sjhb * If we are locking a sleepable lock and this lock 1249111881Sjhb * isn't sleepable, we want to treat it as a lock 1250111881Sjhb * order violation to enfore a general lock order of 1251111881Sjhb * sleepable locks before non-sleepable locks. 1252111881Sjhb */ 1253149738Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1254111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1255149738Sjhb goto reversal; 1256181695Sattilio 1257149738Sjhb /* 1258150179Sjhb * If we are locking Giant and this is a non-sleepable 1259150179Sjhb * lock, then treat it as a reversal. 1260150179Sjhb */ 1261150179Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 1262167787Sjhb lock == &Giant.lock_object) 1263150179Sjhb goto reversal; 1264181695Sattilio 1265150179Sjhb /* 1266149738Sjhb * Check the lock order hierarchy for a reveresal. 1267149738Sjhb */ 1268149738Sjhb if (!isitmydescendant(w, w1)) 126974912Sjhb continue; 1270149738Sjhb reversal: 1271181695Sattilio 127274912Sjhb /* 127374912Sjhb * We have a lock order violation, check to see if it 127474912Sjhb * is allowed or has already been yelled about. 127574912Sjhb */ 1276105508Sphk#ifdef BLESSING 1277181695Sattilio 1278125160Sjhb /* 1279125160Sjhb * If the lock order is blessed, just bail. We don't 1280125160Sjhb * look for other lock order violations though, which 1281125160Sjhb * may be a bug. 1282125160Sjhb */ 128365557Sjasone if (blessed(w, w1)) 1284181695Sattilio goto out; 1285105508Sphk#endif 1286181695Sattilio 1287181695Sattilio /* Bail if this violation is known */ 1288181695Sattilio if (w_rmatrix[w1->w_index][w->w_index] & WITNESS_REVERSAL) 1289181695Sattilio goto out; 1290181695Sattilio 1291181695Sattilio /* Record this as a violation */ 1292181695Sattilio w_rmatrix[w1->w_index][w->w_index] |= WITNESS_REVERSAL; 1293181695Sattilio w_rmatrix[w->w_index][w1->w_index] |= WITNESS_REVERSAL; 1294181695Sattilio w->w_reversed = w1->w_reversed = 1; 1295181695Sattilio witness_increment_graph_generation(); 1296181695Sattilio mtx_unlock_spin(&w_mtx); 1297250411Smarcel 1298250411Smarcel#ifdef WITNESS_NO_VNODE 129974912Sjhb /* 1300250411Smarcel * There are known LORs between VNODE locks. They are 1301250411Smarcel * not an indication of a bug. VNODE locks are flagged 1302250411Smarcel * as such (LO_IS_VNODE) and we don't yell if the LOR 1303250411Smarcel * is between 2 VNODE locks. 1304250411Smarcel */ 1305250411Smarcel if ((lock->lo_flags & LO_IS_VNODE) != 0 && 1306250411Smarcel (lock1->li_lock->lo_flags & LO_IS_VNODE) != 0) 1307250411Smarcel return; 1308250411Smarcel#endif 1309250411Smarcel 1310250411Smarcel /* 131174912Sjhb * Ok, yell about it. 131274912Sjhb */ 1313150179Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1314150179Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1315150179Sjhb printf( 1316150179Sjhb "lock order reversal: (sleepable after non-sleepable)\n"); 1317150179Sjhb else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1318167787Sjhb && lock == &Giant.lock_object) 1319150179Sjhb printf( 1320150179Sjhb "lock order reversal: (Giant after non-sleepable)\n"); 1321150179Sjhb else 1322150179Sjhb printf("lock order reversal:\n"); 1323181695Sattilio 132474912Sjhb /* 132574912Sjhb * Try to locate an earlier lock with 132674912Sjhb * witness w in our list. 132774912Sjhb */ 132874912Sjhb do { 132976272Sjhb lock2 = &lle->ll_children[i]; 133076272Sjhb MPASS(lock2->li_lock != NULL); 133176272Sjhb if (lock2->li_lock->lo_witness == w) 133274912Sjhb break; 133374912Sjhb if (i == 0 && lle->ll_next != NULL) { 133474912Sjhb lle = lle->ll_next; 133574912Sjhb i = lle->ll_count - 1; 1336106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 1337125160Sjhb } else 1338125160Sjhb i--; 133974912Sjhb } while (i >= 0); 134076272Sjhb if (i < 0) { 134193811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 134293811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 1343226294Sadrian w1->w_name, fixup_filename(lock1->li_file), 1344226294Sadrian lock1->li_line); 134593811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 1346226294Sadrian lock->lo_name, w->w_name, 1347226294Sadrian fixup_filename(file), line); 134876272Sjhb } else { 134993811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 135093811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 1351179025Sattilio lock2->li_lock->lo_witness->w_name, 1352226294Sadrian fixup_filename(lock2->li_file), 1353226294Sadrian lock2->li_line); 135493811Sjhb printf(" 2nd %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(" 3rd %p %s (%s) @ %s:%d\n", lock, 1359226294Sadrian lock->lo_name, w->w_name, 1360226294Sadrian fixup_filename(file), line); 136176272Sjhb } 1362181695Sattilio witness_debugger(1); 1363125160Sjhb return; 136465557Sjasone } 136565557Sjasone } 1366181695Sattilio 136778871Sjhb /* 1368125160Sjhb * If requested, build a new lock order. However, don't build a new 1369125160Sjhb * relationship between a sleepable lock and Giant if it is in the 1370125160Sjhb * wrong direction. The correct lock order is that sleepable locks 1371125160Sjhb * always come before Giant. 137278871Sjhb */ 1373125160Sjhb if (flags & LOP_NEWORDER && 1374182914Sjhb !(plock->li_lock == &Giant.lock_object && 1375112117Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0)) { 137687593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 1377182914Sjhb w->w_name, plock->li_lock->lo_witness->w_name); 1378182914Sjhb itismychild(plock->li_lock->lo_witness, w); 1379181695Sattilio } 1380181695Sattilioout: 1381112117Sjhb mtx_unlock_spin(&w_mtx); 1382125160Sjhb} 1383125160Sjhb 1384125160Sjhbvoid 1385125160Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 1386125160Sjhb{ 1387125160Sjhb struct lock_list_entry **lock_list, *lle; 1388125160Sjhb struct lock_instance *instance; 1389125160Sjhb struct witness *w; 1390125160Sjhb struct thread *td; 1391125160Sjhb 1392182446Sattilio if (witness_cold || witness_watch == -1 || lock->lo_witness == NULL || 1393125160Sjhb panicstr != NULL) 1394125160Sjhb return; 1395125160Sjhb w = lock->lo_witness; 1396125160Sjhb td = curthread; 1397125160Sjhb 1398125160Sjhb /* Determine lock list for this lock. */ 1399154077Sjhb if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1400125160Sjhb lock_list = &td->td_sleeplocks; 1401125160Sjhb else 1402125160Sjhb lock_list = PCPU_PTR(spinlocks); 1403125160Sjhb 1404125160Sjhb /* Check to see if we are recursing on a lock we already own. */ 1405125160Sjhb instance = find_instance(*lock_list, lock); 1406125160Sjhb if (instance != NULL) { 1407125160Sjhb instance->li_flags++; 1408125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1409125160Sjhb td->td_proc->p_pid, lock->lo_name, 1410125160Sjhb instance->li_flags & LI_RECURSEMASK); 1411125160Sjhb instance->li_file = file; 1412125160Sjhb instance->li_line = line; 1413125160Sjhb return; 1414110779Speter } 1415125160Sjhb 1416125160Sjhb /* Update per-witness last file and line acquire. */ 141765557Sjasone w->w_file = file; 141865557Sjasone w->w_line = line; 1419125160Sjhb 1420125160Sjhb /* Find the next open lock instance in the list and fill it. */ 142174912Sjhb lle = *lock_list; 142276272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 142378785Sjhb lle = witness_lock_list_get(); 142478785Sjhb if (lle == NULL) 142565557Sjasone return; 142678785Sjhb lle->ll_next = *lock_list; 142787593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 142884680Sjhb td->td_proc->p_pid, lle); 142978785Sjhb *lock_list = lle; 143065557Sjasone } 1431125160Sjhb instance = &lle->ll_children[lle->ll_count++]; 1432125160Sjhb instance->li_lock = lock; 1433125160Sjhb instance->li_line = line; 1434125160Sjhb instance->li_file = file; 143576272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 1436125160Sjhb instance->li_flags = LI_EXCLUSIVE; 143776272Sjhb else 1438125160Sjhb instance->li_flags = 0; 143987593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 144084680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 144165557Sjasone} 144265557Sjasone 144365557Sjasonevoid 144482244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 144582244Sjhb{ 144682244Sjhb struct lock_instance *instance; 144782244Sjhb struct lock_class *class; 144882244Sjhb 1449181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1450182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 145182244Sjhb return; 1452154077Sjhb class = LOCK_CLASS(lock); 1453182473Sattilio if (witness_watch) { 1454182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1455244105Salfred kassert_panic( 1456244105Salfred "upgrade of non-upgradable lock (%s) %s @ %s:%d", 1457226294Sadrian class->lc_name, lock->lo_name, 1458226294Sadrian fixup_filename(file), line); 1459182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1460244105Salfred kassert_panic( 1461244105Salfred "upgrade of non-sleep lock (%s) %s @ %s:%d", 1462226294Sadrian class->lc_name, lock->lo_name, 1463226294Sadrian fixup_filename(file), line); 1464182473Sattilio } 146583366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 1466244112Salfred if (instance == NULL) { 1467244105Salfred kassert_panic("upgrade of unlocked lock (%s) %s @ %s:%d", 1468226294Sadrian class->lc_name, lock->lo_name, 1469226294Sadrian fixup_filename(file), line); 1470244112Salfred return; 1471244112Salfred } 1472182473Sattilio if (witness_watch) { 1473182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1474244105Salfred kassert_panic( 1475244105Salfred "upgrade of exclusive lock (%s) %s @ %s:%d", 1476226294Sadrian class->lc_name, lock->lo_name, 1477226294Sadrian fixup_filename(file), line); 1478182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1479244105Salfred kassert_panic( 1480244105Salfred "upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1481182473Sattilio class->lc_name, lock->lo_name, 1482226294Sadrian instance->li_flags & LI_RECURSEMASK, 1483226294Sadrian fixup_filename(file), line); 1484182473Sattilio } 148582244Sjhb instance->li_flags |= LI_EXCLUSIVE; 148682244Sjhb} 148782244Sjhb 148882244Sjhbvoid 148982244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 149082244Sjhb int line) 149182244Sjhb{ 149282244Sjhb struct lock_instance *instance; 149382244Sjhb struct lock_class *class; 149482244Sjhb 1495181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 1496182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 149782244Sjhb return; 1498154077Sjhb class = LOCK_CLASS(lock); 1499182473Sattilio if (witness_watch) { 1500182473Sattilio if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1501244105Salfred kassert_panic( 1502244105Salfred "downgrade of non-upgradable lock (%s) %s @ %s:%d", 1503226294Sadrian class->lc_name, lock->lo_name, 1504226294Sadrian fixup_filename(file), line); 1505182473Sattilio if ((class->lc_flags & LC_SLEEPLOCK) == 0) 1506244105Salfred kassert_panic( 1507244105Salfred "downgrade of non-sleep lock (%s) %s @ %s:%d", 1508226294Sadrian class->lc_name, lock->lo_name, 1509226294Sadrian fixup_filename(file), line); 1510182473Sattilio } 151183366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 1512244112Salfred if (instance == NULL) { 1513244105Salfred kassert_panic("downgrade of unlocked lock (%s) %s @ %s:%d", 1514226294Sadrian class->lc_name, lock->lo_name, 1515226294Sadrian fixup_filename(file), line); 1516244112Salfred return; 1517244112Salfred } 1518182473Sattilio if (witness_watch) { 1519182473Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1520244105Salfred kassert_panic( 1521244105Salfred "downgrade of shared lock (%s) %s @ %s:%d", 1522226294Sadrian class->lc_name, lock->lo_name, 1523226294Sadrian fixup_filename(file), line); 1524182473Sattilio if ((instance->li_flags & LI_RECURSEMASK) != 0) 1525244105Salfred kassert_panic( 1526244105Salfred "downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1527182473Sattilio class->lc_name, lock->lo_name, 1528226793Sjhb instance->li_flags & LI_RECURSEMASK, 1529226793Sjhb fixup_filename(file), line); 1530182473Sattilio } 153182244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 153282244Sjhb} 153382244Sjhb 153482244Sjhbvoid 153574912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 153665557Sjasone{ 153774912Sjhb struct lock_list_entry **lock_list, *lle; 153876272Sjhb struct lock_instance *instance; 153974912Sjhb struct lock_class *class; 154083366Sjulian struct thread *td; 154192858Simp register_t s; 154274912Sjhb int i, j; 154365557Sjasone 1544182446Sattilio if (witness_cold || lock->lo_witness == NULL || panicstr != NULL) 154571352Sjasone return; 154683366Sjulian td = curthread; 1547154077Sjhb class = LOCK_CLASS(lock); 1548125160Sjhb 1549125160Sjhb /* Find lock instance associated with this lock. */ 155076272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 155183366Sjulian lock_list = &td->td_sleeplocks; 155276272Sjhb else 155374912Sjhb lock_list = PCPU_PTR(spinlocks); 1554181695Sattilio lle = *lock_list; 155574912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 155676272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 155776272Sjhb instance = &(*lock_list)->ll_children[i]; 1558125160Sjhb if (instance->li_lock == lock) 1559125160Sjhb goto found; 156076272Sjhb } 1561182446Sattilio 1562182446Sattilio /* 1563182446Sattilio * When disabling WITNESS through witness_watch we could end up in 1564182473Sattilio * having registered locks in the td_sleeplocks queue. 1565182446Sattilio * We have to make sure we flush these queues, so just search for 1566182473Sattilio * eventual register locks and remove them. 1567182446Sattilio */ 1568244112Salfred if (witness_watch > 0) { 1569244105Salfred kassert_panic("lock (%s) %s not locked @ %s:%d", class->lc_name, 1570226294Sadrian lock->lo_name, fixup_filename(file), line); 1571182446Sattilio return; 1572244112Salfred } else { 1573244112Salfred return; 1574244112Salfred } 1575125160Sjhbfound: 1576125160Sjhb 1577125160Sjhb /* First, check for shared/exclusive mismatches. */ 1578182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) != 0 && witness_watch > 0 && 1579125160Sjhb (flags & LOP_EXCLUSIVE) == 0) { 1580125160Sjhb printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1581226793Sjhb lock->lo_name, fixup_filename(file), line); 1582125160Sjhb printf("while exclusively locked from %s:%d\n", 1583226294Sadrian fixup_filename(instance->li_file), instance->li_line); 1584244105Salfred kassert_panic("excl->ushare"); 1585125160Sjhb } 1586182446Sattilio if ((instance->li_flags & LI_EXCLUSIVE) == 0 && witness_watch > 0 && 1587125160Sjhb (flags & LOP_EXCLUSIVE) != 0) { 1588125160Sjhb printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1589226294Sadrian lock->lo_name, fixup_filename(file), line); 1590226294Sadrian printf("while share locked from %s:%d\n", 1591226294Sadrian fixup_filename(instance->li_file), 1592125160Sjhb instance->li_line); 1593244105Salfred kassert_panic("share->uexcl"); 1594125160Sjhb } 1595125160Sjhb /* If we are recursed, unrecurse. */ 1596125160Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1597125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1598125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1599125160Sjhb instance->li_flags); 1600125160Sjhb instance->li_flags--; 1601125160Sjhb return; 1602125160Sjhb } 1603189194Sthompsa /* The lock is now being dropped, check for NORELEASE flag */ 1604189194Sthompsa if ((instance->li_flags & LI_NORELEASE) != 0 && witness_watch > 0) { 1605189194Sthompsa printf("forbidden unlock of (%s) %s @ %s:%d\n", class->lc_name, 1606226294Sadrian lock->lo_name, fixup_filename(file), line); 1607244105Salfred kassert_panic("lock marked norelease"); 1608189194Sthompsa } 1609125160Sjhb 1610125160Sjhb /* Otherwise, remove this item from the list. */ 1611125160Sjhb s = intr_disable(); 1612125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1613125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1614125160Sjhb (*lock_list)->ll_count - 1); 1615125160Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 1616125160Sjhb (*lock_list)->ll_children[j] = 1617125160Sjhb (*lock_list)->ll_children[j + 1]; 1618125160Sjhb (*lock_list)->ll_count--; 1619125160Sjhb intr_restore(s); 1620125160Sjhb 1621181695Sattilio /* 1622182984Sattilio * In order to reduce contention on w_mtx, we want to keep always an 1623182984Sattilio * head object into lists so that frequent allocation from the 1624182984Sattilio * free witness pool (and subsequent locking) is avoided. 1625182984Sattilio * In order to maintain the current code simple, when the head 1626182984Sattilio * object is totally unloaded it means also that we do not have 1627182984Sattilio * further objects in the list, so the list ownership needs to be 1628182984Sattilio * hand over to another object if the current head needs to be freed. 1629181695Sattilio */ 1630182984Sattilio if ((*lock_list)->ll_count == 0) { 1631182984Sattilio if (*lock_list == lle) { 1632182984Sattilio if (lle->ll_next == NULL) 1633182984Sattilio return; 1634182984Sattilio } else 1635182984Sattilio lle = *lock_list; 1636125160Sjhb *lock_list = lle->ll_next; 1637125160Sjhb CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1638125160Sjhb td->td_proc->p_pid, lle); 1639125160Sjhb witness_lock_list_free(lle); 1640125160Sjhb } 164165557Sjasone} 164265557Sjasone 1643181695Sattiliovoid 1644181695Sattiliowitness_thread_exit(struct thread *td) 1645181695Sattilio{ 1646181695Sattilio struct lock_list_entry *lle; 1647181695Sattilio int i, n; 1648181695Sattilio 1649181695Sattilio lle = td->td_sleeplocks; 1650181695Sattilio if (lle == NULL || panicstr != NULL) 1651181695Sattilio return; 1652181695Sattilio if (lle->ll_count != 0) { 1653181695Sattilio for (n = 0; lle != NULL; lle = lle->ll_next) 1654181695Sattilio for (i = lle->ll_count - 1; i >= 0; i--) { 1655181695Sattilio if (n == 0) 1656181695Sattilio printf("Thread %p exiting with the following locks held:\n", 1657181695Sattilio td); 1658181695Sattilio n++; 1659207929Sattilio witness_list_lock(&lle->ll_children[i], printf); 1660181695Sattilio 1661181695Sattilio } 1662244105Salfred kassert_panic( 1663244105Salfred "Thread %p cannot exit while holding sleeplocks\n", td); 1664181695Sattilio } 1665181695Sattilio witness_lock_list_free(lle); 1666181695Sattilio} 1667181695Sattilio 166874912Sjhb/* 1669111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 1670111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 1671111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 1672111881Sjhb * console along with a list of the offending locks. If indicated in the 1673111881Sjhb * flags then a failure results in a panic as well. 167474912Sjhb */ 167565557Sjasoneint 1676111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 167765557Sjasone{ 1678183955Sattilio struct lock_list_entry *lock_list, *lle; 167976272Sjhb struct lock_instance *lock1; 168083366Sjulian struct thread *td; 1681111881Sjhb va_list ap; 168274912Sjhb int i, n; 168365557Sjasone 1684182446Sattilio if (witness_cold || witness_watch < 1 || panicstr != NULL) 168574912Sjhb return (0); 168674912Sjhb n = 0; 168783366Sjulian td = curthread; 1688111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 168974912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 169076272Sjhb lock1 = &lle->ll_children[i]; 1691111881Sjhb if (lock1->li_lock == lock) 1692111881Sjhb continue; 1693111881Sjhb if (flags & WARN_GIANTOK && 1694167787Sjhb lock1->li_lock == &Giant.lock_object) 169574912Sjhb continue; 1696111881Sjhb if (flags & WARN_SLEEPOK && 1697111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 169876272Sjhb continue; 1699111881Sjhb if (n == 0) { 1700111881Sjhb va_start(ap, fmt); 1701111881Sjhb vprintf(fmt, ap); 1702111881Sjhb va_end(ap); 1703111881Sjhb printf(" with the following"); 1704111881Sjhb if (flags & WARN_SLEEPOK) 1705111881Sjhb printf(" non-sleepable"); 1706118441Sjhb printf(" locks held:\n"); 170776272Sjhb } 170874912Sjhb n++; 1709207929Sattilio witness_list_lock(lock1, printf); 171074912Sjhb } 1711181695Sattilio 1712183955Sattilio /* 1713183955Sattilio * Pin the thread in order to avoid problems with thread migration. 1714183955Sattilio * Once that all verifies are passed about spinlocks ownership, 1715183955Sattilio * the thread is in a safe path and it can be unpinned. 1716183955Sattilio */ 1717183955Sattilio sched_pin(); 1718183955Sattilio lock_list = PCPU_GET(spinlocks); 1719184098Sattilio if (lock_list != NULL && lock_list->ll_count != 0) { 1720183955Sattilio sched_unpin(); 1721181695Sattilio 172297006Sjhb /* 1723183955Sattilio * We should only have one spinlock and as long as 1724183955Sattilio * the flags cannot match for this locks class, 1725183955Sattilio * check if the first spinlock is the one curthread 1726183955Sattilio * should hold. 172797006Sjhb */ 1728183955Sattilio lock1 = &lock_list->ll_children[lock_list->ll_count - 1]; 1729184098Sattilio if (lock_list->ll_count == 1 && lock_list->ll_next == NULL && 1730184098Sattilio lock1->li_lock == lock && n == 0) 1731184098Sattilio return (0); 1732183955Sattilio 1733184098Sattilio va_start(ap, fmt); 1734184098Sattilio vprintf(fmt, ap); 1735184098Sattilio va_end(ap); 1736184098Sattilio printf(" with the following"); 1737184098Sattilio if (flags & WARN_SLEEPOK) 1738184098Sattilio printf(" non-sleepable"); 1739184098Sattilio printf(" locks held:\n"); 1740207929Sattilio n += witness_list_locks(&lock_list, printf); 1741183955Sattilio } else 1742183955Sattilio sched_unpin(); 1743111881Sjhb if (flags & WARN_PANIC && n) 1744244105Salfred kassert_panic("%s", __func__); 1745181695Sattilio else 1746181695Sattilio witness_debugger(n); 174765557Sjasone return (n); 174865557Sjasone} 174965557Sjasone 1750102448Siedowseconst char * 1751102448Siedowsewitness_file(struct lock_object *lock) 1752102448Siedowse{ 1753102448Siedowse struct witness *w; 1754102448Siedowse 1755182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1756102448Siedowse return ("?"); 1757102448Siedowse w = lock->lo_witness; 1758102448Siedowse return (w->w_file); 1759102448Siedowse} 1760102448Siedowse 1761102448Siedowseint 1762102448Siedowsewitness_line(struct lock_object *lock) 1763102448Siedowse{ 1764102448Siedowse struct witness *w; 1765102448Siedowse 1766182446Sattilio if (witness_cold || witness_watch < 1 || lock->lo_witness == NULL) 1767102448Siedowse return (0); 1768102448Siedowse w = lock->lo_witness; 1769102448Siedowse return (w->w_line); 1770102448Siedowse} 1771102448Siedowse 177265856Sjhbstatic struct witness * 177374912Sjhbenroll(const char *description, struct lock_class *lock_class) 177465557Sjasone{ 177574912Sjhb struct witness *w; 1776181695Sattilio struct witness_list *typelist; 177765557Sjasone 1778181695Sattilio MPASS(description != NULL); 1779181695Sattilio 1780182473Sattilio if (witness_watch == -1 || panicstr != NULL) 178165557Sjasone return (NULL); 1782181695Sattilio if ((lock_class->lc_flags & LC_SPINLOCK)) { 1783181695Sattilio if (witness_skipspin) 1784181695Sattilio return (NULL); 1785181695Sattilio else 1786181695Sattilio typelist = &w_spin; 1787244112Salfred } else if ((lock_class->lc_flags & LC_SLEEPLOCK)) { 1788181695Sattilio typelist = &w_sleep; 1789244112Salfred } else { 1790244105Salfred kassert_panic("lock class %s is not sleep or spin", 1791181695Sattilio lock_class->lc_name); 1792244112Salfred return (NULL); 1793244112Salfred } 1794181695Sattilio 1795181695Sattilio mtx_lock_spin(&w_mtx); 1796181695Sattilio w = witness_hash_get(description); 1797181695Sattilio if (w) 1798181695Sattilio goto found; 1799181695Sattilio if ((w = witness_get()) == NULL) 180065557Sjasone return (NULL); 1801181695Sattilio MPASS(strlen(description) < MAX_W_NAME); 1802181695Sattilio strcpy(w->w_name, description); 180374912Sjhb w->w_class = lock_class; 180475362Sjhb w->w_refcount = 1; 180574912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 1806149441Struckman if (lock_class->lc_flags & LC_SPINLOCK) { 180774912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1808149441Struckman w_spin_cnt++; 1809149441Struckman } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 181074912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1811149441Struckman w_sleep_cnt++; 181275364Sbp } 1813181695Sattilio 1814181695Sattilio /* Insert new witness into the hash */ 1815181695Sattilio witness_hash_put(w); 1816181695Sattilio witness_increment_graph_generation(); 181774912Sjhb mtx_unlock_spin(&w_mtx); 181865557Sjasone return (w); 1819181695Sattiliofound: 1820181695Sattilio w->w_refcount++; 1821181695Sattilio mtx_unlock_spin(&w_mtx); 1822181695Sattilio if (lock_class != w->w_class) 1823244105Salfred kassert_panic( 1824181695Sattilio "lock (%s) %s does not match earlier (%s) lock", 1825181695Sattilio description, lock_class->lc_name, 1826181695Sattilio w->w_class->lc_name); 1827181695Sattilio return (w); 182865557Sjasone} 182965557Sjasone 1830179025Sattiliostatic void 1831112117Sjhbdepart(struct witness *w) 183265557Sjasone{ 183374912Sjhb struct witness_list *list; 183465557Sjasone 1835112117Sjhb MPASS(w->w_refcount == 0); 1836149441Struckman if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1837112117Sjhb list = &w_sleep; 1838149441Struckman w_sleep_cnt--; 1839149441Struckman } else { 1840112117Sjhb list = &w_spin; 1841149441Struckman w_spin_cnt--; 1842149441Struckman } 1843112117Sjhb /* 1844181695Sattilio * Set file to NULL as it may point into a loadable module. 1845112117Sjhb */ 1846181695Sattilio w->w_file = NULL; 1847181695Sattilio w->w_line = 0; 1848181695Sattilio witness_increment_graph_generation(); 1849181695Sattilio} 1850112117Sjhb 1851181695Sattilio 1852181695Sattiliostatic void 1853181695Sattilioadopt(struct witness *parent, struct witness *child) 1854181695Sattilio{ 1855181695Sattilio int pi, ci, i, j; 1856181695Sattilio 1857181695Sattilio if (witness_cold == 0) 1858181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1859181695Sattilio 1860181695Sattilio /* If the relationship is already known, there's no work to be done. */ 1861181695Sattilio if (isitmychild(parent, child)) 1862181695Sattilio return; 1863181695Sattilio 1864181695Sattilio /* When the structure of the graph changes, bump up the generation. */ 1865181695Sattilio witness_increment_graph_generation(); 1866181695Sattilio 1867112117Sjhb /* 1868181695Sattilio * The hard part ... create the direct relationship, then propagate all 1869181695Sattilio * indirect relationships. 1870112117Sjhb */ 1871181695Sattilio pi = parent->w_index; 1872181695Sattilio ci = child->w_index; 1873181695Sattilio WITNESS_INDEX_ASSERT(pi); 1874181695Sattilio WITNESS_INDEX_ASSERT(ci); 1875181695Sattilio MPASS(pi != ci); 1876181695Sattilio w_rmatrix[pi][ci] |= WITNESS_PARENT; 1877181695Sattilio w_rmatrix[ci][pi] |= WITNESS_CHILD; 1878112117Sjhb 1879112117Sjhb /* 1880181695Sattilio * If parent was not already an ancestor of child, 1881181695Sattilio * then we increment the descendant and ancestor counters. 1882112117Sjhb */ 1883181695Sattilio if ((w_rmatrix[pi][ci] & WITNESS_ANCESTOR) == 0) { 1884181695Sattilio parent->w_num_descendants++; 1885181695Sattilio child->w_num_ancestors++; 1886181695Sattilio } 1887112117Sjhb 1888181695Sattilio /* 1889181695Sattilio * Find each ancestor of 'pi'. Note that 'pi' itself is counted as 1890181695Sattilio * an ancestor of 'pi' during this loop. 1891181695Sattilio */ 1892181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 1893181695Sattilio if ((w_rmatrix[i][pi] & WITNESS_ANCESTOR_MASK) == 0 && 1894181695Sattilio (i != pi)) 1895181695Sattilio continue; 1896112117Sjhb 1897181695Sattilio /* Find each descendant of 'i' and mark it as a descendant. */ 1898181695Sattilio for (j = 1; j <= w_max_used_index; j++) { 189974912Sjhb 1900181695Sattilio /* 1901181695Sattilio * Skip children that are already marked as 1902181695Sattilio * descendants of 'i'. 1903181695Sattilio */ 1904181695Sattilio if (w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) 1905181695Sattilio continue; 1906181695Sattilio 1907181695Sattilio /* 1908181695Sattilio * We are only interested in descendants of 'ci'. Note 1909181695Sattilio * that 'ci' itself is counted as a descendant of 'ci'. 1910181695Sattilio */ 1911181695Sattilio if ((w_rmatrix[ci][j] & WITNESS_ANCESTOR_MASK) == 0 && 1912181695Sattilio (j != ci)) 1913181695Sattilio continue; 1914181695Sattilio w_rmatrix[i][j] |= WITNESS_ANCESTOR; 1915181695Sattilio w_rmatrix[j][i] |= WITNESS_DESCENDANT; 1916181695Sattilio w_data[i].w_num_descendants++; 1917181695Sattilio w_data[j].w_num_ancestors++; 1918181695Sattilio 1919181695Sattilio /* 1920181695Sattilio * Make sure we aren't marking a node as both an 1921181695Sattilio * ancestor and descendant. We should have caught 1922181695Sattilio * this as a lock order reversal earlier. 1923181695Sattilio */ 1924181695Sattilio if ((w_rmatrix[i][j] & WITNESS_ANCESTOR_MASK) && 1925181695Sattilio (w_rmatrix[i][j] & WITNESS_DESCENDANT_MASK)) { 1926181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1927181695Sattilio "both ancestor and descendant\n", 1928181695Sattilio i, j, w_rmatrix[i][j]); 1929181695Sattilio kdb_backtrace(); 1930181695Sattilio printf("Witness disabled.\n"); 1931182446Sattilio witness_watch = -1; 1932181695Sattilio } 1933181695Sattilio if ((w_rmatrix[j][i] & WITNESS_ANCESTOR_MASK) && 1934181695Sattilio (w_rmatrix[j][i] & WITNESS_DESCENDANT_MASK)) { 1935181695Sattilio printf("witness rmatrix paradox! [%d][%d]=%d " 1936181695Sattilio "both ancestor and descendant\n", 1937181695Sattilio j, i, w_rmatrix[j][i]); 1938181695Sattilio kdb_backtrace(); 1939181695Sattilio printf("Witness disabled.\n"); 1940182446Sattilio witness_watch = -1; 1941181695Sattilio } 1942181695Sattilio } 194365557Sjasone } 1944112117Sjhb} 1945112117Sjhb 1946181695Sattiliostatic void 1947112117Sjhbitismychild(struct witness *parent, struct witness *child) 1948112117Sjhb{ 1949244112Salfred int unlocked; 1950112117Sjhb 1951112117Sjhb MPASS(child != NULL && parent != NULL); 1952181695Sattilio if (witness_cold == 0) 1953181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 1954181695Sattilio 1955181695Sattilio if (!witness_lock_type_equal(parent, child)) { 1956244112Salfred if (witness_cold == 0) { 1957244112Salfred unlocked = 1; 1958181695Sattilio mtx_unlock_spin(&w_mtx); 1959244112Salfred } else { 1960244112Salfred unlocked = 0; 1961244112Salfred } 1962244105Salfred kassert_panic( 1963244105Salfred "%s: parent \"%s\" (%s) and child \"%s\" (%s) are not " 1964181695Sattilio "the same lock type", __func__, parent->w_name, 1965181695Sattilio parent->w_class->lc_name, child->w_name, 1966112117Sjhb child->w_class->lc_name); 1967244112Salfred if (unlocked) 1968244112Salfred mtx_lock_spin(&w_mtx); 1969181695Sattilio } 1970181695Sattilio adopt(parent, child); 197165557Sjasone} 197265557Sjasone 1973181695Sattilio/* 1974181695Sattilio * Generic code for the isitmy*() functions. The rmask parameter is the 1975181695Sattilio * expected relationship of w1 to w2. 1976181695Sattilio */ 1977181695Sattiliostatic int 1978181695Sattilio_isitmyx(struct witness *w1, struct witness *w2, int rmask, const char *fname) 197965557Sjasone{ 1980181695Sattilio unsigned char r1, r2; 1981181695Sattilio int i1, i2; 198265557Sjasone 1983181695Sattilio i1 = w1->w_index; 1984181695Sattilio i2 = w2->w_index; 1985181695Sattilio WITNESS_INDEX_ASSERT(i1); 1986181695Sattilio WITNESS_INDEX_ASSERT(i2); 1987181695Sattilio r1 = w_rmatrix[i1][i2] & WITNESS_RELATED_MASK; 1988181695Sattilio r2 = w_rmatrix[i2][i1] & WITNESS_RELATED_MASK; 1989181695Sattilio 1990181695Sattilio /* The flags on one better be the inverse of the flags on the other */ 1991181695Sattilio if (!((WITNESS_ATOD(r1) == r2 && WITNESS_DTOA(r2) == r1) || 1992181695Sattilio (WITNESS_DTOA(r1) == r2 && WITNESS_ATOD(r2) == r1))) { 1993181695Sattilio printf("%s: rmatrix mismatch between %s (index %d) and %s " 1994181695Sattilio "(index %d): w_rmatrix[%d][%d] == %hhx but " 1995181695Sattilio "w_rmatrix[%d][%d] == %hhx\n", 1996181695Sattilio fname, w1->w_name, i1, w2->w_name, i2, i1, i2, r1, 1997181695Sattilio i2, i1, r2); 1998181695Sattilio kdb_backtrace(); 1999181695Sattilio printf("Witness disabled.\n"); 2000182446Sattilio witness_watch = -1; 2001181695Sattilio } 2002181695Sattilio return (r1 & rmask); 200365557Sjasone} 200465557Sjasone 2005181695Sattilio/* 2006181695Sattilio * Checks if @child is a direct child of @parent. 2007181695Sattilio */ 200865557Sjasonestatic int 200965856Sjhbisitmychild(struct witness *parent, struct witness *child) 201065557Sjasone{ 201165557Sjasone 2012181695Sattilio return (_isitmyx(parent, child, WITNESS_PARENT, __func__)); 201365557Sjasone} 201465557Sjasone 2015181695Sattilio/* 2016181695Sattilio * Checks if @descendant is a direct or inderect descendant of @ancestor. 2017181695Sattilio */ 201865557Sjasonestatic int 2019181695Sattilioisitmydescendant(struct witness *ancestor, struct witness *descendant) 202065557Sjasone{ 202165557Sjasone 2022181695Sattilio return (_isitmyx(ancestor, descendant, WITNESS_ANCESTOR_MASK, 2023181695Sattilio __func__)); 202465557Sjasone} 202565557Sjasone 2026105508Sphk#ifdef BLESSING 202765557Sjasonestatic int 202865856Sjhbblessed(struct witness *w1, struct witness *w2) 202965557Sjasone{ 203065557Sjasone int i; 203165856Sjhb struct witness_blessed *b; 203265557Sjasone 203365557Sjasone for (i = 0; i < blessed_count; i++) { 203465557Sjasone b = &blessed_list[i]; 203574912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 203674912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 203765557Sjasone return (1); 203865557Sjasone continue; 203965557Sjasone } 204074912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 204174912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 204265557Sjasone return (1); 204365557Sjasone } 204465557Sjasone return (0); 204565557Sjasone} 2046105508Sphk#endif 204765557Sjasone 204865856Sjhbstatic struct witness * 204974912Sjhbwitness_get(void) 205065557Sjasone{ 205165856Sjhb struct witness *w; 2052181695Sattilio int index; 205365557Sjasone 2054181695Sattilio if (witness_cold == 0) 2055181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2056181695Sattilio 2057182473Sattilio if (witness_watch == -1) { 205876481Sjhb mtx_unlock_spin(&w_mtx); 205976481Sjhb return (NULL); 206076481Sjhb } 206174912Sjhb if (STAILQ_EMPTY(&w_free)) { 2062182446Sattilio witness_watch = -1; 206374912Sjhb mtx_unlock_spin(&w_mtx); 2064181695Sattilio printf("WITNESS: unable to allocate a new witness object\n"); 206565557Sjasone return (NULL); 206665557Sjasone } 206774912Sjhb w = STAILQ_FIRST(&w_free); 206874912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 2069149441Struckman w_free_cnt--; 2070181695Sattilio index = w->w_index; 2071181695Sattilio MPASS(index > 0 && index == w_max_used_index+1 && 2072181695Sattilio index < WITNESS_COUNT); 207365856Sjhb bzero(w, sizeof(*w)); 2074181695Sattilio w->w_index = index; 2075181695Sattilio if (index > w_max_used_index) 2076181695Sattilio w_max_used_index = index; 207765557Sjasone return (w); 207865557Sjasone} 207965557Sjasone 208065557Sjasonestatic void 208165856Sjhbwitness_free(struct witness *w) 208265557Sjasone{ 208374912Sjhb 208474912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 2085149441Struckman w_free_cnt++; 208665557Sjasone} 208765557Sjasone 208874912Sjhbstatic struct lock_list_entry * 208974912Sjhbwitness_lock_list_get(void) 209074912Sjhb{ 209174912Sjhb struct lock_list_entry *lle; 209271709Sjhb 2093182446Sattilio if (witness_watch == -1) 209476481Sjhb return (NULL); 209574912Sjhb mtx_lock_spin(&w_mtx); 209674912Sjhb lle = w_lock_list_free; 209774912Sjhb if (lle == NULL) { 2098182446Sattilio witness_watch = -1; 209974912Sjhb mtx_unlock_spin(&w_mtx); 210074912Sjhb printf("%s: witness exhausted\n", __func__); 210174912Sjhb return (NULL); 210274912Sjhb } 210374912Sjhb w_lock_list_free = lle->ll_next; 210474912Sjhb mtx_unlock_spin(&w_mtx); 210574912Sjhb bzero(lle, sizeof(*lle)); 210674912Sjhb return (lle); 210774912Sjhb} 210874912Sjhb 210974912Sjhbstatic void 211074912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 211171709Sjhb{ 211271709Sjhb 211374912Sjhb mtx_lock_spin(&w_mtx); 211474912Sjhb lle->ll_next = w_lock_list_free; 211574912Sjhb w_lock_list_free = lle; 211674912Sjhb mtx_unlock_spin(&w_mtx); 211771709Sjhb} 211871709Sjhb 211976272Sjhbstatic struct lock_instance * 2120227588Spjdfind_instance(struct lock_list_entry *list, const struct lock_object *lock) 212176272Sjhb{ 212276272Sjhb struct lock_list_entry *lle; 212376272Sjhb struct lock_instance *instance; 212476272Sjhb int i; 212576272Sjhb 2126181695Sattilio for (lle = list; lle != NULL; lle = lle->ll_next) 212776272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 212876272Sjhb instance = &lle->ll_children[i]; 212976272Sjhb if (instance->li_lock == lock) 213076272Sjhb return (instance); 213176272Sjhb } 213276272Sjhb return (NULL); 213376272Sjhb} 213476272Sjhb 2135111881Sjhbstatic void 2136207929Sattiliowitness_list_lock(struct lock_instance *instance, 2137207929Sattilio int (*prnt)(const char *fmt, ...)) 2138111881Sjhb{ 2139111881Sjhb struct lock_object *lock; 2140111881Sjhb 2141111881Sjhb lock = instance->li_lock; 2142207929Sattilio prnt("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 2143154077Sjhb "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 2144179025Sattilio if (lock->lo_witness->w_name != lock->lo_name) 2145207929Sattilio prnt(" (%s)", lock->lo_witness->w_name); 2146207929Sattilio prnt(" r = %d (%p) locked @ %s:%d\n", 2147226294Sadrian instance->li_flags & LI_RECURSEMASK, lock, 2148226793Sjhb fixup_filename(instance->li_file), instance->li_line); 2149111881Sjhb} 2150111881Sjhb 2151140637Srwatson#ifdef DDB 2152139333Srwatsonstatic int 2153139333Srwatsonwitness_thread_has_locks(struct thread *td) 2154139333Srwatson{ 2155139333Srwatson 2156182984Sattilio if (td->td_sleeplocks == NULL) 2157182984Sattilio return (0); 2158182984Sattilio return (td->td_sleeplocks->ll_count != 0); 2159139333Srwatson} 2160139333Srwatson 2161139333Srwatsonstatic int 2162139333Srwatsonwitness_proc_has_locks(struct proc *p) 2163139333Srwatson{ 2164139333Srwatson struct thread *td; 2165139333Srwatson 2166139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2167139333Srwatson if (witness_thread_has_locks(td)) 2168139333Srwatson return (1); 2169139333Srwatson } 2170139333Srwatson return (0); 2171139333Srwatson} 2172140637Srwatson#endif 2173139333Srwatson 217474912Sjhbint 2175207929Sattiliowitness_list_locks(struct lock_list_entry **lock_list, 2176207929Sattilio int (*prnt)(const char *fmt, ...)) 217772224Sjhb{ 217875273Sjhb struct lock_list_entry *lle; 217974912Sjhb int i, nheld; 218072224Sjhb 218174912Sjhb nheld = 0; 218274912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 218374912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 2184207929Sattilio witness_list_lock(&lle->ll_children[i], prnt); 218574912Sjhb nheld++; 218674912Sjhb } 218775273Sjhb return (nheld); 218875273Sjhb} 218975273Sjhb 2190118271Sjhb/* 2191118271Sjhb * This is a bit risky at best. We call this function when we have timed 2192118271Sjhb * out acquiring a spin lock, and we assume that the other CPU is stuck 2193118271Sjhb * with this lock held. So, we go groveling around in the other CPU's 2194118271Sjhb * per-cpu data to try to find the lock instance for this spin lock to 2195118271Sjhb * see when it was last acquired. 2196118271Sjhb */ 219765557Sjasonevoid 2198207929Sattiliowitness_display_spinlock(struct lock_object *lock, struct thread *owner, 2199207929Sattilio int (*prnt)(const char *fmt, ...)) 2200118271Sjhb{ 2201118271Sjhb struct lock_instance *instance; 2202118271Sjhb struct pcpu *pc; 2203118271Sjhb 2204118271Sjhb if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 2205118271Sjhb return; 2206118271Sjhb pc = pcpu_find(owner->td_oncpu); 2207118271Sjhb instance = find_instance(pc->pc_spinlocks, lock); 2208118271Sjhb if (instance != NULL) 2209207929Sattilio witness_list_lock(instance, prnt); 2210118271Sjhb} 2211118271Sjhb 2212118271Sjhbvoid 221374912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 221465557Sjasone{ 2215153854Sjhb struct lock_list_entry *lock_list; 221676272Sjhb struct lock_instance *instance; 2217154077Sjhb struct lock_class *class; 221871320Sjasone 2219228424Savg /* 2220228424Savg * This function is used independently in locking code to deal with 2221228424Savg * Giant, SCHEDULER_STOPPED() check can be removed here after Giant 2222228424Savg * is gone. 2223228424Savg */ 2224228424Savg if (SCHEDULER_STOPPED()) 2225228424Savg return; 2226181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2227182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 222871352Sjasone return; 2229154077Sjhb class = LOCK_CLASS(lock); 2230154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2231153854Sjhb lock_list = curthread->td_sleeplocks; 2232153854Sjhb else { 2233153854Sjhb if (witness_skipspin) 2234153854Sjhb return; 2235153854Sjhb lock_list = PCPU_GET(spinlocks); 2236153854Sjhb } 2237153854Sjhb instance = find_instance(lock_list, lock); 2238244112Salfred if (instance == NULL) { 2239244105Salfred kassert_panic("%s: lock (%s) %s not locked", __func__, 2240154077Sjhb class->lc_name, lock->lo_name); 2241244112Salfred return; 2242244112Salfred } 224376272Sjhb *filep = instance->li_file; 224476272Sjhb *linep = instance->li_line; 224565557Sjasone} 224665557Sjasone 224765557Sjasonevoid 224874912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 224965557Sjasone{ 2250153854Sjhb struct lock_list_entry *lock_list; 225176272Sjhb struct lock_instance *instance; 2252154077Sjhb struct lock_class *class; 225371320Sjasone 2254228424Savg /* 2255228424Savg * This function is used independently in locking code to deal with 2256228424Savg * Giant, SCHEDULER_STOPPED() check can be removed here after Giant 2257228424Savg * is gone. 2258228424Savg */ 2259228424Savg if (SCHEDULER_STOPPED()) 2260228424Savg return; 2261181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2262182473Sattilio if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 226371352Sjasone return; 2264154077Sjhb class = LOCK_CLASS(lock); 2265154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 2266153854Sjhb lock_list = curthread->td_sleeplocks; 2267153854Sjhb else { 2268153854Sjhb if (witness_skipspin) 2269153854Sjhb return; 2270153854Sjhb lock_list = PCPU_GET(spinlocks); 2271153854Sjhb } 2272153854Sjhb instance = find_instance(lock_list, lock); 227382243Sjhb if (instance == NULL) 2274244105Salfred kassert_panic("%s: lock (%s) %s not locked", __func__, 2275154077Sjhb class->lc_name, lock->lo_name); 227674912Sjhb lock->lo_witness->w_file = file; 227774912Sjhb lock->lo_witness->w_line = line; 2278244112Salfred if (instance == NULL) 2279244112Salfred return; 228076272Sjhb instance->li_file = file; 228176272Sjhb instance->li_line = line; 228265557Sjasone} 228365557Sjasone 228478871Sjhbvoid 2285227588Spjdwitness_assert(const struct lock_object *lock, int flags, const char *file, 2286227588Spjd int line) 228778871Sjhb{ 228878871Sjhb#ifdef INVARIANT_SUPPORT 228978871Sjhb struct lock_instance *instance; 2290154077Sjhb struct lock_class *class; 229178871Sjhb 2292182446Sattilio if (lock->lo_witness == NULL || witness_watch < 1 || panicstr != NULL) 229378941Sjhb return; 2294154077Sjhb class = LOCK_CLASS(lock); 2295154077Sjhb if ((class->lc_flags & LC_SLEEPLOCK) != 0) 229683366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 2297154077Sjhb else if ((class->lc_flags & LC_SPINLOCK) != 0) 229878871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 229986422Sjhb else { 2300244105Salfred kassert_panic("Lock (%s) %s is not sleep or spin!", 2301154077Sjhb class->lc_name, lock->lo_name); 2302244111Salfred return; 230386422Sjhb } 230478871Sjhb switch (flags) { 230578871Sjhb case LA_UNLOCKED: 230678871Sjhb if (instance != NULL) 2307244105Salfred kassert_panic("Lock (%s) %s locked @ %s:%d.", 2308226294Sadrian class->lc_name, lock->lo_name, 2309226294Sadrian fixup_filename(file), line); 231078871Sjhb break; 231178871Sjhb case LA_LOCKED: 231278871Sjhb case LA_LOCKED | LA_RECURSED: 231378871Sjhb case LA_LOCKED | LA_NOTRECURSED: 231478871Sjhb case LA_SLOCKED: 231578871Sjhb case LA_SLOCKED | LA_RECURSED: 231678871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 231778871Sjhb case LA_XLOCKED: 231878871Sjhb case LA_XLOCKED | LA_RECURSED: 231978871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 232086422Sjhb if (instance == NULL) { 2321244105Salfred kassert_panic("Lock (%s) %s not locked @ %s:%d.", 2322226294Sadrian class->lc_name, lock->lo_name, 2323226294Sadrian fixup_filename(file), line); 232486422Sjhb break; 232586422Sjhb } 232678871Sjhb if ((flags & LA_XLOCKED) != 0 && 232778871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 2328244105Salfred kassert_panic( 2329244105Salfred "Lock (%s) %s not exclusively locked @ %s:%d.", 2330226294Sadrian class->lc_name, lock->lo_name, 2331226294Sadrian fixup_filename(file), line); 233278871Sjhb if ((flags & LA_SLOCKED) != 0 && 233378871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 2334244105Salfred kassert_panic( 2335244105Salfred "Lock (%s) %s exclusively locked @ %s:%d.", 2336226294Sadrian class->lc_name, lock->lo_name, 2337226294Sadrian fixup_filename(file), line); 233878871Sjhb if ((flags & LA_RECURSED) != 0 && 233978871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 2340244105Salfred kassert_panic("Lock (%s) %s not recursed @ %s:%d.", 2341226294Sadrian class->lc_name, lock->lo_name, 2342226294Sadrian fixup_filename(file), line); 234378871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 234478871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 2345244105Salfred kassert_panic("Lock (%s) %s recursed @ %s:%d.", 2346226294Sadrian class->lc_name, lock->lo_name, 2347226294Sadrian fixup_filename(file), line); 234878871Sjhb break; 234978871Sjhb default: 2350244105Salfred kassert_panic("Invalid lock assertion at %s:%d.", 2351226294Sadrian fixup_filename(file), line); 235278871Sjhb 235378871Sjhb } 235478871Sjhb#endif /* INVARIANT_SUPPORT */ 235578871Sjhb} 235678871Sjhb 2357187511Sthompsastatic void 2358187511Sthompsawitness_setflag(struct lock_object *lock, int flag, int set) 2359187511Sthompsa{ 2360187511Sthompsa struct lock_list_entry *lock_list; 2361187511Sthompsa struct lock_instance *instance; 2362187511Sthompsa struct lock_class *class; 2363187511Sthompsa 2364187511Sthompsa if (lock->lo_witness == NULL || witness_watch == -1 || panicstr != NULL) 2365187511Sthompsa return; 2366187511Sthompsa class = LOCK_CLASS(lock); 2367187511Sthompsa if (class->lc_flags & LC_SLEEPLOCK) 2368187511Sthompsa lock_list = curthread->td_sleeplocks; 2369187511Sthompsa else { 2370187511Sthompsa if (witness_skipspin) 2371187511Sthompsa return; 2372187511Sthompsa lock_list = PCPU_GET(spinlocks); 2373187511Sthompsa } 2374187511Sthompsa instance = find_instance(lock_list, lock); 2375244112Salfred if (instance == NULL) { 2376244105Salfred kassert_panic("%s: lock (%s) %s not locked", __func__, 2377187511Sthompsa class->lc_name, lock->lo_name); 2378244112Salfred return; 2379244112Salfred } 2380187511Sthompsa 2381187511Sthompsa if (set) 2382187511Sthompsa instance->li_flags |= flag; 2383187511Sthompsa else 2384187511Sthompsa instance->li_flags &= ~flag; 2385187511Sthompsa} 2386187511Sthompsa 2387187511Sthompsavoid 2388187511Sthompsawitness_norelease(struct lock_object *lock) 2389187511Sthompsa{ 2390187511Sthompsa 2391187511Sthompsa witness_setflag(lock, LI_NORELEASE, 1); 2392187511Sthompsa} 2393187511Sthompsa 2394187511Sthompsavoid 2395187511Sthompsawitness_releaseok(struct lock_object *lock) 2396187511Sthompsa{ 2397187511Sthompsa 2398187511Sthompsa witness_setflag(lock, LI_NORELEASE, 0); 2399187511Sthompsa} 2400187511Sthompsa 240174912Sjhb#ifdef DDB 2402112061Sjhbstatic void 2403181695Sattiliowitness_ddb_list(struct thread *td) 2404112061Sjhb{ 240574912Sjhb 2406181695Sattilio KASSERT(witness_cold == 0, ("%s: witness_cold", __func__)); 2407131930Smarcel KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 2408112061Sjhb 2409182446Sattilio if (witness_watch < 1) 2410112061Sjhb return; 2411112061Sjhb 2412207929Sattilio witness_list_locks(&td->td_sleeplocks, db_printf); 2413112061Sjhb 2414112061Sjhb /* 2415112061Sjhb * We only handle spinlocks if td == curthread. This is somewhat broken 2416112061Sjhb * if td is currently executing on some other CPU and holds spin locks 2417112061Sjhb * as we won't display those locks. If we had a MI way of getting 2418112061Sjhb * the per-cpu data for a given cpu then we could use 2419113339Sjulian * td->td_oncpu to get the list of spinlocks for this thread 2420112061Sjhb * and "fix" this. 2421112061Sjhb * 2422170302Sjeff * That still wouldn't really fix this unless we locked the scheduler 2423170302Sjeff * lock or stopped the other CPU to make sure it wasn't changing the 2424170302Sjeff * list out from under us. It is probably best to just not try to 2425170302Sjeff * handle threads on other CPU's for now. 2426112061Sjhb */ 2427112061Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 2428207929Sattilio witness_list_locks(PCPU_PTR(spinlocks), db_printf); 2429112061Sjhb} 2430112061Sjhb 243174930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 243274912Sjhb{ 243383366Sjulian struct thread *td; 243474912Sjhb 2435158030Sjhb if (have_addr) 2436158030Sjhb td = db_lookup_thread(addr, TRUE); 2437158030Sjhb else 2438158030Sjhb td = kdb_thread; 2439181695Sattilio witness_ddb_list(td); 244074912Sjhb} 244174912Sjhb 2442183054SsamDB_SHOW_ALL_COMMAND(locks, db_witness_list_all) 2443139333Srwatson{ 2444139333Srwatson struct thread *td; 2445139333Srwatson struct proc *p; 2446139333Srwatson 2447139333Srwatson /* 2448139333Srwatson * It would be nice to list only threads and processes that actually 2449139333Srwatson * held sleep locks, but that information is currently not exported 2450139333Srwatson * by WITNESS. 2451139333Srwatson */ 2452139333Srwatson FOREACH_PROC_IN_SYSTEM(p) { 2453139333Srwatson if (!witness_proc_has_locks(p)) 2454139333Srwatson continue; 2455139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 2456139333Srwatson if (!witness_thread_has_locks(td)) 2457139333Srwatson continue; 2458153853Sjhb db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2459182984Sattilio p->p_comm, td, td->td_tid); 2460181695Sattilio witness_ddb_list(td); 2461239584Sjhb if (db_pager_quit) 2462239584Sjhb return; 2463139333Srwatson } 2464139333Srwatson } 2465139333Srwatson} 2466183054SsamDB_SHOW_ALIAS(alllocks, db_witness_list_all) 2467139333Srwatson 246874912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 246974912Sjhb{ 247074912Sjhb 2471181695Sattilio witness_ddb_display(db_printf); 247274912Sjhb} 247374912Sjhb#endif 2474181695Sattilio 2475181695Sattiliostatic int 2476181695Sattiliosysctl_debug_witness_badstacks(SYSCTL_HANDLER_ARGS) 2477181695Sattilio{ 2478181695Sattilio struct witness_lock_order_data *data1, *data2, *tmp_data1, *tmp_data2; 2479181695Sattilio struct witness *tmp_w1, *tmp_w2, *w1, *w2; 2480181695Sattilio struct sbuf *sb; 2481181695Sattilio u_int w_rmatrix1, w_rmatrix2; 2482181695Sattilio int error, generation, i, j; 2483181695Sattilio 2484181695Sattilio tmp_data1 = NULL; 2485181695Sattilio tmp_data2 = NULL; 2486181695Sattilio tmp_w1 = NULL; 2487181695Sattilio tmp_w2 = NULL; 2488182446Sattilio if (witness_watch < 1) { 2489181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2490181695Sattilio return (error); 2491181695Sattilio } 2492181695Sattilio if (witness_cold) { 2493181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2494181695Sattilio return (error); 2495181695Sattilio } 2496181695Sattilio error = 0; 2497181695Sattilio sb = sbuf_new(NULL, NULL, BADSTACK_SBUF_SIZE, SBUF_AUTOEXTEND); 2498181695Sattilio if (sb == NULL) 2499181695Sattilio return (ENOMEM); 2500181695Sattilio 2501181695Sattilio /* Allocate and init temporary storage space. */ 2502181695Sattilio tmp_w1 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2503181695Sattilio tmp_w2 = malloc(sizeof(struct witness), M_TEMP, M_WAITOK | M_ZERO); 2504181695Sattilio tmp_data1 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2505181695Sattilio M_WAITOK | M_ZERO); 2506181695Sattilio tmp_data2 = malloc(sizeof(struct witness_lock_order_data), M_TEMP, 2507181695Sattilio M_WAITOK | M_ZERO); 2508181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2509181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2510181695Sattilio 2511181695Sattiliorestart: 2512181695Sattilio mtx_lock_spin(&w_mtx); 2513181695Sattilio generation = w_generation; 2514181695Sattilio mtx_unlock_spin(&w_mtx); 2515181695Sattilio sbuf_printf(sb, "Number of known direct relationships is %d\n", 2516181695Sattilio w_lohash.wloh_count); 2517181695Sattilio for (i = 1; i < w_max_used_index; i++) { 2518181695Sattilio mtx_lock_spin(&w_mtx); 2519181695Sattilio if (generation != w_generation) { 2520181695Sattilio mtx_unlock_spin(&w_mtx); 2521181695Sattilio 2522181695Sattilio /* The graph has changed, try again. */ 2523181695Sattilio req->oldidx = 0; 2524181695Sattilio sbuf_clear(sb); 2525181695Sattilio goto restart; 2526181695Sattilio } 2527181695Sattilio 2528181695Sattilio w1 = &w_data[i]; 2529181695Sattilio if (w1->w_reversed == 0) { 2530181695Sattilio mtx_unlock_spin(&w_mtx); 2531181695Sattilio continue; 2532181695Sattilio } 2533181695Sattilio 2534181695Sattilio /* Copy w1 locally so we can release the spin lock. */ 2535181695Sattilio *tmp_w1 = *w1; 2536181695Sattilio mtx_unlock_spin(&w_mtx); 2537181695Sattilio 2538181695Sattilio if (tmp_w1->w_reversed == 0) 2539181695Sattilio continue; 2540181695Sattilio for (j = 1; j < w_max_used_index; j++) { 2541181695Sattilio if ((w_rmatrix[i][j] & WITNESS_REVERSAL) == 0 || i > j) 2542181695Sattilio continue; 2543181695Sattilio 2544181695Sattilio mtx_lock_spin(&w_mtx); 2545181695Sattilio if (generation != w_generation) { 2546181695Sattilio mtx_unlock_spin(&w_mtx); 2547181695Sattilio 2548181695Sattilio /* The graph has changed, try again. */ 2549181695Sattilio req->oldidx = 0; 2550181695Sattilio sbuf_clear(sb); 2551181695Sattilio goto restart; 2552181695Sattilio } 2553181695Sattilio 2554181695Sattilio w2 = &w_data[j]; 2555181695Sattilio data1 = witness_lock_order_get(w1, w2); 2556181695Sattilio data2 = witness_lock_order_get(w2, w1); 2557181695Sattilio 2558181695Sattilio /* 2559181695Sattilio * Copy information locally so we can release the 2560181695Sattilio * spin lock. 2561181695Sattilio */ 2562181695Sattilio *tmp_w2 = *w2; 2563181695Sattilio w_rmatrix1 = (unsigned int)w_rmatrix[i][j]; 2564181695Sattilio w_rmatrix2 = (unsigned int)w_rmatrix[j][i]; 2565181695Sattilio 2566181695Sattilio if (data1) { 2567181695Sattilio stack_zero(&tmp_data1->wlod_stack); 2568181695Sattilio stack_copy(&data1->wlod_stack, 2569181695Sattilio &tmp_data1->wlod_stack); 2570181695Sattilio } 2571181695Sattilio if (data2 && data2 != data1) { 2572181695Sattilio stack_zero(&tmp_data2->wlod_stack); 2573181695Sattilio stack_copy(&data2->wlod_stack, 2574181695Sattilio &tmp_data2->wlod_stack); 2575181695Sattilio } 2576181695Sattilio mtx_unlock_spin(&w_mtx); 2577181695Sattilio 2578181695Sattilio sbuf_printf(sb, 2579181695Sattilio "\nLock order reversal between \"%s\"(%s) and \"%s\"(%s)!\n", 2580181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2581181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2582181695Sattilio#if 0 2583181695Sattilio sbuf_printf(sb, 2584181695Sattilio "w_rmatrix[%s][%s] == %x, w_rmatrix[%s][%s] == %x\n", 2585181695Sattilio tmp_w1->name, tmp_w2->w_name, w_rmatrix1, 2586181695Sattilio tmp_w2->name, tmp_w1->w_name, w_rmatrix2); 2587181695Sattilio#endif 2588181695Sattilio if (data1) { 2589181695Sattilio sbuf_printf(sb, 2590181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2591181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name, 2592181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name); 2593181695Sattilio stack_sbuf_print(sb, &tmp_data1->wlod_stack); 2594181695Sattilio sbuf_printf(sb, "\n"); 2595181695Sattilio } 2596181695Sattilio if (data2 && data2 != data1) { 2597181695Sattilio sbuf_printf(sb, 2598181695Sattilio "Lock order \"%s\"(%s) -> \"%s\"(%s) first seen at:\n", 2599181695Sattilio tmp_w2->w_name, tmp_w2->w_class->lc_name, 2600181695Sattilio tmp_w1->w_name, tmp_w1->w_class->lc_name); 2601181695Sattilio stack_sbuf_print(sb, &tmp_data2->wlod_stack); 2602181695Sattilio sbuf_printf(sb, "\n"); 2603181695Sattilio } 2604181695Sattilio } 2605181695Sattilio } 2606181695Sattilio mtx_lock_spin(&w_mtx); 2607181695Sattilio if (generation != w_generation) { 2608181695Sattilio mtx_unlock_spin(&w_mtx); 2609181695Sattilio 2610181695Sattilio /* 2611181695Sattilio * The graph changed while we were printing stack data, 2612181695Sattilio * try again. 2613181695Sattilio */ 2614181695Sattilio req->oldidx = 0; 2615181695Sattilio sbuf_clear(sb); 2616181695Sattilio goto restart; 2617181695Sattilio } 2618181695Sattilio mtx_unlock_spin(&w_mtx); 2619181695Sattilio 2620181695Sattilio /* Free temporary storage space. */ 2621181695Sattilio free(tmp_data1, M_TEMP); 2622181695Sattilio free(tmp_data2, M_TEMP); 2623181695Sattilio free(tmp_w1, M_TEMP); 2624181695Sattilio free(tmp_w2, M_TEMP); 2625181695Sattilio 2626181695Sattilio sbuf_finish(sb); 2627181695Sattilio error = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb) + 1); 2628181695Sattilio sbuf_delete(sb); 2629181695Sattilio 2630181695Sattilio return (error); 2631181695Sattilio} 2632181695Sattilio 2633181695Sattiliostatic int 2634181695Sattiliosysctl_debug_witness_fullgraph(SYSCTL_HANDLER_ARGS) 2635181695Sattilio{ 2636181695Sattilio struct witness *w; 2637181695Sattilio struct sbuf *sb; 2638181695Sattilio int error; 2639181695Sattilio 2640182446Sattilio if (witness_watch < 1) { 2641181695Sattilio error = SYSCTL_OUT(req, w_notrunning, sizeof(w_notrunning)); 2642181695Sattilio return (error); 2643181695Sattilio } 2644181695Sattilio if (witness_cold) { 2645181695Sattilio error = SYSCTL_OUT(req, w_stillcold, sizeof(w_stillcold)); 2646181695Sattilio return (error); 2647181695Sattilio } 2648181695Sattilio error = 0; 2649217916Smdf 2650217916Smdf error = sysctl_wire_old_buffer(req, 0); 2651217916Smdf if (error != 0) 2652217916Smdf return (error); 2653212750Smdf sb = sbuf_new_for_sysctl(NULL, NULL, FULLGRAPH_SBUF_SIZE, req); 2654181695Sattilio if (sb == NULL) 2655181695Sattilio return (ENOMEM); 2656181695Sattilio sbuf_printf(sb, "\n"); 2657181695Sattilio 2658181695Sattilio mtx_lock_spin(&w_mtx); 2659181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2660181695Sattilio w->w_displayed = 0; 2661181695Sattilio STAILQ_FOREACH(w, &w_all, w_list) 2662181695Sattilio witness_add_fullgraph(sb, w); 2663181695Sattilio mtx_unlock_spin(&w_mtx); 2664181695Sattilio 2665181695Sattilio /* 2666181695Sattilio * Close the sbuf and return to userland. 2667181695Sattilio */ 2668212750Smdf error = sbuf_finish(sb); 2669181695Sattilio sbuf_delete(sb); 2670181695Sattilio 2671181695Sattilio return (error); 2672181695Sattilio} 2673181695Sattilio 2674181695Sattiliostatic int 2675181695Sattiliosysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 2676181695Sattilio{ 2677181695Sattilio int error, value; 2678181695Sattilio 2679182473Sattilio value = witness_watch; 2680181695Sattilio error = sysctl_handle_int(oidp, &value, 0, req); 2681181695Sattilio if (error != 0 || req->newptr == NULL) 2682181695Sattilio return (error); 2683182446Sattilio if (value > 1 || value < -1 || 2684182446Sattilio (witness_watch == -1 && value != witness_watch)) 2685181695Sattilio return (EINVAL); 2686182446Sattilio witness_watch = value; 2687181695Sattilio return (0); 2688181695Sattilio} 2689181695Sattilio 2690181695Sattiliostatic void 2691181695Sattiliowitness_add_fullgraph(struct sbuf *sb, struct witness *w) 2692181695Sattilio{ 2693181695Sattilio int i; 2694181695Sattilio 2695181695Sattilio if (w->w_displayed != 0 || (w->w_file == NULL && w->w_line == 0)) 2696181695Sattilio return; 2697181695Sattilio w->w_displayed = 1; 2698181695Sattilio 2699181695Sattilio WITNESS_INDEX_ASSERT(w->w_index); 2700181695Sattilio for (i = 1; i <= w_max_used_index; i++) { 2701181695Sattilio if (w_rmatrix[w->w_index][i] & WITNESS_PARENT) { 2702181695Sattilio sbuf_printf(sb, "\"%s\",\"%s\"\n", w->w_name, 2703181695Sattilio w_data[i].w_name); 2704181695Sattilio witness_add_fullgraph(sb, &w_data[i]); 2705181695Sattilio } 2706181695Sattilio } 2707181695Sattilio} 2708181695Sattilio 2709181695Sattilio/* 2710181695Sattilio * A simple hash function. Takes a key pointer and a key size. If size == 0, 2711181695Sattilio * interprets the key as a string and reads until the null 2712181695Sattilio * terminator. Otherwise, reads the first size bytes. Returns an unsigned 32-bit 2713181695Sattilio * hash value computed from the key. 2714181695Sattilio */ 2715181695Sattiliostatic uint32_t 2716181695Sattiliowitness_hash_djb2(const uint8_t *key, uint32_t size) 2717181695Sattilio{ 2718181695Sattilio unsigned int hash = 5381; 2719181695Sattilio int i; 2720181695Sattilio 2721181695Sattilio /* hash = hash * 33 + key[i] */ 2722181695Sattilio if (size) 2723181695Sattilio for (i = 0; i < size; i++) 2724181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2725181695Sattilio else 2726181695Sattilio for (i = 0; key[i] != 0; i++) 2727181695Sattilio hash = ((hash << 5) + hash) + (unsigned int)key[i]; 2728181695Sattilio 2729181695Sattilio return (hash); 2730181695Sattilio} 2731181695Sattilio 2732181695Sattilio 2733181695Sattilio/* 2734181695Sattilio * Initializes the two witness hash tables. Called exactly once from 2735181695Sattilio * witness_initialize(). 2736181695Sattilio */ 2737181695Sattiliostatic void 2738181695Sattiliowitness_init_hash_tables(void) 2739181695Sattilio{ 2740181695Sattilio int i; 2741181695Sattilio 2742181695Sattilio MPASS(witness_cold); 2743181695Sattilio 2744181695Sattilio /* Initialize the hash tables. */ 2745181695Sattilio for (i = 0; i < WITNESS_HASH_SIZE; i++) 2746181695Sattilio w_hash.wh_array[i] = NULL; 2747181695Sattilio 2748181695Sattilio w_hash.wh_size = WITNESS_HASH_SIZE; 2749181695Sattilio w_hash.wh_count = 0; 2750181695Sattilio 2751181695Sattilio /* Initialize the lock order data hash. */ 2752181695Sattilio w_lofree = NULL; 2753181695Sattilio for (i = 0; i < WITNESS_LO_DATA_COUNT; i++) { 2754181695Sattilio memset(&w_lodata[i], 0, sizeof(w_lodata[i])); 2755181695Sattilio w_lodata[i].wlod_next = w_lofree; 2756181695Sattilio w_lofree = &w_lodata[i]; 2757181695Sattilio } 2758181695Sattilio w_lohash.wloh_size = WITNESS_LO_HASH_SIZE; 2759181695Sattilio w_lohash.wloh_count = 0; 2760181695Sattilio for (i = 0; i < WITNESS_LO_HASH_SIZE; i++) 2761181695Sattilio w_lohash.wloh_array[i] = NULL; 2762181695Sattilio} 2763181695Sattilio 2764181695Sattiliostatic struct witness * 2765181695Sattiliowitness_hash_get(const char *key) 2766181695Sattilio{ 2767181695Sattilio struct witness *w; 2768181695Sattilio uint32_t hash; 2769181695Sattilio 2770181695Sattilio MPASS(key != NULL); 2771181695Sattilio if (witness_cold == 0) 2772181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2773181695Sattilio hash = witness_hash_djb2(key, 0) % w_hash.wh_size; 2774181695Sattilio w = w_hash.wh_array[hash]; 2775181695Sattilio while (w != NULL) { 2776181695Sattilio if (strcmp(w->w_name, key) == 0) 2777181695Sattilio goto out; 2778181695Sattilio w = w->w_hash_next; 2779181695Sattilio } 2780181695Sattilio 2781181695Sattilioout: 2782181695Sattilio return (w); 2783181695Sattilio} 2784181695Sattilio 2785181695Sattiliostatic void 2786181695Sattiliowitness_hash_put(struct witness *w) 2787181695Sattilio{ 2788181695Sattilio uint32_t hash; 2789181695Sattilio 2790181695Sattilio MPASS(w != NULL); 2791181695Sattilio MPASS(w->w_name != NULL); 2792181695Sattilio if (witness_cold == 0) 2793181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2794181695Sattilio KASSERT(witness_hash_get(w->w_name) == NULL, 2795181695Sattilio ("%s: trying to add a hash entry that already exists!", __func__)); 2796181695Sattilio KASSERT(w->w_hash_next == NULL, 2797181695Sattilio ("%s: w->w_hash_next != NULL", __func__)); 2798181695Sattilio 2799181695Sattilio hash = witness_hash_djb2(w->w_name, 0) % w_hash.wh_size; 2800181695Sattilio w->w_hash_next = w_hash.wh_array[hash]; 2801181695Sattilio w_hash.wh_array[hash] = w; 2802181695Sattilio w_hash.wh_count++; 2803181695Sattilio} 2804181695Sattilio 2805181695Sattilio 2806181695Sattiliostatic struct witness_lock_order_data * 2807181695Sattiliowitness_lock_order_get(struct witness *parent, struct witness *child) 2808181695Sattilio{ 2809181695Sattilio struct witness_lock_order_data *data = NULL; 2810181695Sattilio struct witness_lock_order_key key; 2811181695Sattilio unsigned int hash; 2812181695Sattilio 2813181695Sattilio MPASS(parent != NULL && child != NULL); 2814181695Sattilio key.from = parent->w_index; 2815181695Sattilio key.to = child->w_index; 2816181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2817181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2818181695Sattilio if ((w_rmatrix[parent->w_index][child->w_index] 2819181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) == 0) 2820181695Sattilio goto out; 2821181695Sattilio 2822181695Sattilio hash = witness_hash_djb2((const char*)&key, 2823181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2824181695Sattilio data = w_lohash.wloh_array[hash]; 2825181695Sattilio while (data != NULL) { 2826181695Sattilio if (witness_lock_order_key_equal(&data->wlod_key, &key)) 2827181695Sattilio break; 2828181695Sattilio data = data->wlod_next; 2829181695Sattilio } 2830181695Sattilio 2831181695Sattilioout: 2832181695Sattilio return (data); 2833181695Sattilio} 2834181695Sattilio 2835181695Sattilio/* 2836181695Sattilio * Verify that parent and child have a known relationship, are not the same, 2837181695Sattilio * and child is actually a child of parent. This is done without w_mtx 2838181695Sattilio * to avoid contention in the common case. 2839181695Sattilio */ 2840181695Sattiliostatic int 2841181695Sattiliowitness_lock_order_check(struct witness *parent, struct witness *child) 2842181695Sattilio{ 2843181695Sattilio 2844181695Sattilio if (parent != child && 2845181695Sattilio w_rmatrix[parent->w_index][child->w_index] 2846181695Sattilio & WITNESS_LOCK_ORDER_KNOWN && 2847181695Sattilio isitmychild(parent, child)) 2848181695Sattilio return (1); 2849181695Sattilio 2850181695Sattilio return (0); 2851181695Sattilio} 2852181695Sattilio 2853181695Sattiliostatic int 2854181695Sattiliowitness_lock_order_add(struct witness *parent, struct witness *child) 2855181695Sattilio{ 2856181695Sattilio struct witness_lock_order_data *data = NULL; 2857181695Sattilio struct witness_lock_order_key key; 2858181695Sattilio unsigned int hash; 2859181695Sattilio 2860181695Sattilio MPASS(parent != NULL && child != NULL); 2861181695Sattilio key.from = parent->w_index; 2862181695Sattilio key.to = child->w_index; 2863181695Sattilio WITNESS_INDEX_ASSERT(key.from); 2864181695Sattilio WITNESS_INDEX_ASSERT(key.to); 2865181695Sattilio if (w_rmatrix[parent->w_index][child->w_index] 2866181695Sattilio & WITNESS_LOCK_ORDER_KNOWN) 2867181695Sattilio return (1); 2868181695Sattilio 2869181695Sattilio hash = witness_hash_djb2((const char*)&key, 2870181695Sattilio sizeof(key)) % w_lohash.wloh_size; 2871181695Sattilio w_rmatrix[parent->w_index][child->w_index] |= WITNESS_LOCK_ORDER_KNOWN; 2872181695Sattilio data = w_lofree; 2873181695Sattilio if (data == NULL) 2874181695Sattilio return (0); 2875181695Sattilio w_lofree = data->wlod_next; 2876181695Sattilio data->wlod_next = w_lohash.wloh_array[hash]; 2877181695Sattilio data->wlod_key = key; 2878181695Sattilio w_lohash.wloh_array[hash] = data; 2879181695Sattilio w_lohash.wloh_count++; 2880181695Sattilio stack_zero(&data->wlod_stack); 2881181695Sattilio stack_save(&data->wlod_stack); 2882181695Sattilio return (1); 2883181695Sattilio} 2884181695Sattilio 2885181695Sattilio/* Call this whenver the structure of the witness graph changes. */ 2886181695Sattiliostatic void 2887181695Sattiliowitness_increment_graph_generation(void) 2888181695Sattilio{ 2889181695Sattilio 2890181695Sattilio if (witness_cold == 0) 2891181695Sattilio mtx_assert(&w_mtx, MA_OWNED); 2892181695Sattilio w_generation++; 2893181695Sattilio} 2894181695Sattilio 2895181695Sattilio#ifdef KDB 2896181695Sattiliostatic void 2897181695Sattilio_witness_debugger(int cond, const char *msg) 2898181695Sattilio{ 2899181695Sattilio 2900181695Sattilio if (witness_trace && cond) 2901181695Sattilio kdb_backtrace(); 2902181695Sattilio if (witness_kdb && cond) 2903181695Sattilio kdb_enter(KDB_WHY_WITNESS, msg); 2904181695Sattilio} 2905181695Sattilio#endif 2906