subr_witness.c revision 169803
165557Sjasone/*- 265557Sjasone * Copyright (c) 1998 Berkeley Software Design, Inc. All rights reserved. 365557Sjasone * 465557Sjasone * Redistribution and use in source and binary forms, with or without 565557Sjasone * modification, are permitted provided that the following conditions 665557Sjasone * are met: 765557Sjasone * 1. Redistributions of source code must retain the above copyright 865557Sjasone * notice, this list of conditions and the following disclaimer. 965557Sjasone * 2. Redistributions in binary form must reproduce the above copyright 1065557Sjasone * notice, this list of conditions and the following disclaimer in the 1165557Sjasone * documentation and/or other materials provided with the distribution. 1265557Sjasone * 3. Berkeley Software Design Inc's name may not be used to endorse or 1365557Sjasone * promote products derived from this software without specific prior 1465557Sjasone * written permission. 1565557Sjasone * 1665557Sjasone * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 1765557Sjasone * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1865557Sjasone * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1965557Sjasone * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 2065557Sjasone * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2165557Sjasone * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2265557Sjasone * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2365557Sjasone * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2465557Sjasone * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2565557Sjasone * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2665557Sjasone * SUCH DAMAGE. 2765557Sjasone * 2865557Sjasone * from BSDI $Id: mutex_witness.c,v 1.1.2.20 2000/04/27 03:10:27 cp Exp $ 2967352Sjhb * and BSDI $Id: synch_machdep.c,v 2.3.2.39 2000/04/27 03:10:25 cp Exp $ 3065557Sjasone */ 3165557Sjasone 3265557Sjasone/* 3374912Sjhb * Implementation of the `witness' lock verifier. Originally implemented for 3474912Sjhb * mutexes in BSD/OS. Extended to handle generic lock objects and lock 3574912Sjhb * classes in FreeBSD. 3672200Sbmilekic */ 3772200Sbmilekic 3872200Sbmilekic/* 3965557Sjasone * Main Entry: witness 4065557Sjasone * Pronunciation: 'wit-n&s 4165557Sjasone * Function: noun 4265557Sjasone * Etymology: Middle English witnesse, from Old English witnes knowledge, 4365557Sjasone * testimony, witness, from 2wit 4465557Sjasone * Date: before 12th century 4565557Sjasone * 1 : attestation of a fact or event : TESTIMONY 4665557Sjasone * 2 : one that gives evidence; specifically : one who testifies in 4765557Sjasone * a cause or before a judicial tribunal 4865557Sjasone * 3 : one asked to be present at a transaction so as to be able to 4965557Sjasone * testify to its having taken place 5065557Sjasone * 4 : one who has personal knowledge of something 5165557Sjasone * 5 a : something serving as evidence or proof : SIGN 5265557Sjasone * b : public affirmation by word or example of usually 5365557Sjasone * religious faith or conviction <the heroic witness to divine 5465557Sjasone * life -- Pilot> 5565557Sjasone * 6 capitalized : a member of the Jehovah's Witnesses 5665557Sjasone */ 5765557Sjasone 58111881Sjhb/* 59111881Sjhb * Special rules concerning Giant and lock orders: 60111881Sjhb * 61111881Sjhb * 1) Giant must be acquired before any other mutexes. Stated another way, 62111881Sjhb * no other mutex may be held when Giant is acquired. 63111881Sjhb * 64111881Sjhb * 2) Giant must be released when blocking on a sleepable lock. 65111881Sjhb * 66111881Sjhb * This rule is less obvious, but is a result of Giant providing the same 67111881Sjhb * semantics as spl(). Basically, when a thread sleeps, it must release 68111881Sjhb * Giant. When a thread blocks on a sleepable lock, it sleeps. Hence rule 69111881Sjhb * 2). 70111881Sjhb * 71111881Sjhb * 3) Giant may be acquired before or after sleepable locks. 72111881Sjhb * 73111881Sjhb * This rule is also not quite as obvious. Giant may be acquired after 74111881Sjhb * a sleepable lock because it is a non-sleepable lock and non-sleepable 75111881Sjhb * locks may always be acquired while holding a sleepable lock. The second 76111881Sjhb * case, Giant before a sleepable lock, follows from rule 2) above. Suppose 77111881Sjhb * you have two threads T1 and T2 and a sleepable lock X. Suppose that T1 78111881Sjhb * acquires X and blocks on Giant. Then suppose that T2 acquires Giant and 79111881Sjhb * blocks on X. When T2 blocks on X, T2 will release Giant allowing T1 to 80111881Sjhb * execute. Thus, acquiring Giant both before and after a sleepable lock 81111881Sjhb * will not result in a lock order reversal. 82111881Sjhb */ 83111881Sjhb 84116182Sobrien#include <sys/cdefs.h> 85116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/subr_witness.c 169803 2007-05-20 22:11:50Z jeff $"); 86116182Sobrien 8768790Sjhb#include "opt_ddb.h" 88168856Sjkoshy#include "opt_hwpmc_hooks.h" 8967676Sjhb#include "opt_witness.h" 9067676Sjhb 9165557Sjasone#include <sys/param.h> 9267352Sjhb#include <sys/bus.h> 93131930Smarcel#include <sys/kdb.h> 9467352Sjhb#include <sys/kernel.h> 9574912Sjhb#include <sys/ktr.h> 9674912Sjhb#include <sys/lock.h> 9767352Sjhb#include <sys/malloc.h> 9874912Sjhb#include <sys/mutex.h> 99164033Srwatson#include <sys/priv.h> 10065557Sjasone#include <sys/proc.h> 10167676Sjhb#include <sys/sysctl.h> 10265557Sjasone#include <sys/systm.h> 10365557Sjasone 10468790Sjhb#include <ddb/ddb.h> 10568790Sjhb 106111881Sjhb#include <machine/stdarg.h> 107111881Sjhb 108154818Sjhb/* Note that these traces do not work with KTR_ALQ. */ 109154790Sjhb#if 0 110154790Sjhb#define KTR_WITNESS KTR_SUBSYS 111154790Sjhb#else 112154790Sjhb#define KTR_WITNESS 0 113154790Sjhb#endif 114154790Sjhb 115153133Sjhb/* Easier to stay with the old names. */ 116153133Sjhb#define lo_list lo_witness_data.lod_list 117153133Sjhb#define lo_witness lo_witness_data.lod_witness 118153133Sjhb 119105508Sphk/* Define this to check for blessed mutexes */ 120105508Sphk#undef BLESSING 121105508Sphk 122139378Sjhb#define WITNESS_COUNT 1024 12374912Sjhb#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 12465557Sjasone/* 12583798Sjhb * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 12674912Sjhb * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 12774912Sjhb * probably be safe for the most part, but it's still a SWAG. 12867352Sjhb */ 12974912Sjhb#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 13071352Sjasone 13174912Sjhb#define WITNESS_NCHILDREN 6 13271352Sjasone 13374912Sjhbstruct witness_child_list_entry; 13471352Sjasone 13574912Sjhbstruct witness { 13674912Sjhb const char *w_name; 13774912Sjhb struct lock_class *w_class; 13874912Sjhb STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 13974912Sjhb STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 14074912Sjhb struct witness_child_list_entry *w_children; /* Great evilness... */ 14174912Sjhb const char *w_file; 14274912Sjhb int w_line; 14374912Sjhb u_int w_level; 14474912Sjhb u_int w_refcount; 14574912Sjhb u_char w_Giant_squawked:1; 14674912Sjhb u_char w_other_squawked:1; 14774912Sjhb u_char w_same_squawked:1; 148112118Sjhb u_char w_displayed:1; 14974912Sjhb}; 15071352Sjasone 15174912Sjhbstruct witness_child_list_entry { 15274912Sjhb struct witness_child_list_entry *wcl_next; 15374912Sjhb struct witness *wcl_children[WITNESS_NCHILDREN]; 15474912Sjhb u_int wcl_count; 15574912Sjhb}; 15671352Sjasone 15774912SjhbSTAILQ_HEAD(witness_list, witness); 15871352Sjasone 159105508Sphk#ifdef BLESSING 16074912Sjhbstruct witness_blessed { 16174912Sjhb const char *b_lock1; 16274912Sjhb const char *b_lock2; 16374912Sjhb}; 164105508Sphk#endif 16571352Sjasone 16674912Sjhbstruct witness_order_list_entry { 16774912Sjhb const char *w_name; 16874912Sjhb struct lock_class *w_class; 16974912Sjhb}; 17071352Sjasone 171112117Sjhb#ifdef BLESSING 172112117Sjhbstatic int blessed(struct witness *, struct witness *); 173112117Sjhb#endif 174112117Sjhbstatic int depart(struct witness *w); 17574912Sjhbstatic struct witness *enroll(const char *description, 17674912Sjhb struct lock_class *lock_class); 177112117Sjhbstatic int insertchild(struct witness *parent, struct witness *child); 178112117Sjhbstatic int isitmychild(struct witness *parent, struct witness *child); 179112117Sjhbstatic int isitmydescendant(struct witness *parent, struct witness *child); 18074912Sjhbstatic int itismychild(struct witness *parent, struct witness *child); 18174912Sjhbstatic void removechild(struct witness *parent, struct witness *child); 182112562Sjhbstatic int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 183112116Sjhbstatic const char *fixup_filename(const char *file); 18474912Sjhbstatic struct witness *witness_get(void); 18574912Sjhbstatic void witness_free(struct witness *m); 18674912Sjhbstatic struct witness_child_list_entry *witness_child_get(void); 18774912Sjhbstatic void witness_child_free(struct witness_child_list_entry *wcl); 18874912Sjhbstatic struct lock_list_entry *witness_lock_list_get(void); 18974912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 19076272Sjhbstatic struct lock_instance *find_instance(struct lock_list_entry *lock_list, 19176272Sjhb struct lock_object *lock); 192111881Sjhbstatic void witness_list_lock(struct lock_instance *instance); 193112115Sjhb#ifdef DDB 194149979Struckmanstatic void witness_leveldescendents(struct witness *parent, int level); 195149979Struckmanstatic void witness_levelall(void); 196149979Struckmanstatic void witness_displaydescendants(void(*)(const char *fmt, ...), 197149979Struckman struct witness *, int indent); 198100011Smpstatic void witness_display_list(void(*prnt)(const char *fmt, ...), 199100011Smp struct witness_list *list); 200100011Smpstatic void witness_display(void(*)(const char *fmt, ...)); 201149979Struckmanstatic void witness_list(struct thread *td); 202100011Smp#endif 20372200Sbmilekic 204134873SjmgSYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking"); 20572200Sbmilekic 206112562Sjhb/* 207149441Struckman * If set to 0, witness is disabled. If set to a non-zero value, witness 208149441Struckman * performs full lock order checking for all locks. At runtime, this 209112562Sjhb * value may be set to 0 to turn off witness. witness is not allowed be 210112562Sjhb * turned on once it is turned off, however. 211112562Sjhb */ 21277843Speterstatic int witness_watch = 1; 213134873SjmgTUNABLE_INT("debug.witness.watch", &witness_watch); 214134873SjmgSYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 215112562Sjhb sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 21671352Sjasone 217131930Smarcel#ifdef KDB 21872200Sbmilekic/* 219131930Smarcel * When KDB is enabled and witness_kdb is set to 1, it will cause the system 220131930Smarcel * to drop into kdebug() when: 221151623Sjhb * - a lock hierarchy violation occurs 22265557Sjasone * - locks are held when going to sleep. 22365557Sjasone */ 224131930Smarcel#ifdef WITNESS_KDB 225131930Smarcelint witness_kdb = 1; 22667676Sjhb#else 227131930Smarcelint witness_kdb = 0; 22865557Sjasone#endif 229134873SjmgTUNABLE_INT("debug.witness.kdb", &witness_kdb); 230134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 231110779Speter 232110779Speter/* 233131930Smarcel * When KDB is enabled and witness_trace is set to 1, it will cause the system 234110779Speter * to print a stack trace: 235151623Sjhb * - a lock hierarchy violation occurs 236110779Speter * - locks are held when going to sleep. 237110779Speter */ 238110779Speterint witness_trace = 1; 239134873SjmgTUNABLE_INT("debug.witness.trace", &witness_trace); 240134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 241131930Smarcel#endif /* KDB */ 24265557Sjasone 24367676Sjhb#ifdef WITNESS_SKIPSPIN 24477843Speterint witness_skipspin = 1; 24567676Sjhb#else 24677843Speterint witness_skipspin = 0; 24765557Sjasone#endif 248134873SjmgTUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 249134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, 250134873Sjmg &witness_skipspin, 0, ""); 25165557Sjasone 25274912Sjhbstatic struct mtx w_mtx; 25374912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 25474912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 25574912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 25674912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 25774912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL; 25874912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 25965557Sjasone 260149441Struckmanstatic int w_free_cnt, w_spin_cnt, w_sleep_cnt, w_child_free_cnt, w_child_cnt; 261149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, ""); 262149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, ""); 263149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0, 264149441Struckman ""); 265149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, child_free_cnt, CTLFLAG_RD, 266149441Struckman &w_child_free_cnt, 0, ""); 267149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, child_cnt, CTLFLAG_RD, &w_child_cnt, 0, 268149441Struckman ""); 269149441Struckman 27074912Sjhbstatic struct witness w_data[WITNESS_COUNT]; 27174912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 27274912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 27365557Sjasone 27474912Sjhbstatic struct witness_order_list_entry order_lists[] = { 275149738Sjhb /* 276149738Sjhb * sx locks 277149738Sjhb */ 27874912Sjhb { "proctree", &lock_class_sx }, 27974912Sjhb { "allproc", &lock_class_sx }, 280168402Spjd { "allprison", &lock_class_sx }, 281149738Sjhb { NULL, NULL }, 282149738Sjhb /* 283149738Sjhb * Various mutexes 284149738Sjhb */ 285111951Sjhb { "Giant", &lock_class_mtx_sleep }, 286108184Skris { "pipe mutex", &lock_class_mtx_sleep }, 28796122Salfred { "sigio lock", &lock_class_mtx_sleep }, 28891140Stanimura { "process group", &lock_class_mtx_sleep }, 28974912Sjhb { "process lock", &lock_class_mtx_sleep }, 29091140Stanimura { "session", &lock_class_mtx_sleep }, 29174912Sjhb { "uidinfo hash", &lock_class_mtx_sleep }, 29274912Sjhb { "uidinfo struct", &lock_class_mtx_sleep }, 293168856Sjkoshy#ifdef HWPMC_HOOKS 294168856Sjkoshy { "pmc-sleep", &lock_class_mtx_sleep }, 295168856Sjkoshy#endif 29674912Sjhb { NULL, NULL }, 29775464Sjhb /* 298130022Srwatson * Sockets 299130022Srwatson */ 300130022Srwatson { "accept", &lock_class_mtx_sleep }, 301130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 302130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 303130022Srwatson { "sellck", &lock_class_mtx_sleep }, 304130022Srwatson { NULL, NULL }, 305130022Srwatson /* 306130022Srwatson * Routing 307130022Srwatson */ 308130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 309130022Srwatson { "radix node head", &lock_class_mtx_sleep }, 310130022Srwatson { "rtentry", &lock_class_mtx_sleep }, 311130022Srwatson { "ifaddr", &lock_class_mtx_sleep }, 312130022Srwatson { NULL, NULL }, 313130022Srwatson /* 314148896Srwatson * Multicast - protocol locks before interface locks, after UDP locks. 315148682Srwatson */ 316148896Srwatson { "udpinp", &lock_class_mtx_sleep }, 317148682Srwatson { "in_multi_mtx", &lock_class_mtx_sleep }, 318148682Srwatson { "igmp_mtx", &lock_class_mtx_sleep }, 319148682Srwatson { "if_addr_mtx", &lock_class_mtx_sleep }, 320148682Srwatson { NULL, NULL }, 321148682Srwatson /* 322130022Srwatson * UNIX Domain Sockets 323130396Srwatson */ 324130396Srwatson { "unp", &lock_class_mtx_sleep }, 325130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 326130031Sjhb { NULL, NULL }, 327130022Srwatson /* 328130022Srwatson * UDP/IP 329130022Srwatson */ 330130022Srwatson { "udp", &lock_class_mtx_sleep }, 331130022Srwatson { "udpinp", &lock_class_mtx_sleep }, 332130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 333130022Srwatson { NULL, NULL }, 334130022Srwatson /* 335130022Srwatson * TCP/IP 336130022Srwatson */ 337130022Srwatson { "tcp", &lock_class_mtx_sleep }, 338130022Srwatson { "tcpinp", &lock_class_mtx_sleep }, 339130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 340130022Srwatson { NULL, NULL }, 341130022Srwatson /* 342130022Srwatson * SLIP 343130022Srwatson */ 344130022Srwatson { "slip_mtx", &lock_class_mtx_sleep }, 345130022Srwatson { "slip sc_mtx", &lock_class_mtx_sleep }, 346130031Sjhb { NULL, NULL }, 347130022Srwatson /* 348132639Srwatson * netatalk 349132639Srwatson */ 350132639Srwatson { "ddp_list_mtx", &lock_class_mtx_sleep }, 351132639Srwatson { "ddp_mtx", &lock_class_mtx_sleep }, 352132639Srwatson { NULL, NULL }, 353132639Srwatson /* 354134971Srwatson * BPF 355134971Srwatson */ 356134971Srwatson { "bpf global lock", &lock_class_mtx_sleep }, 357134971Srwatson { "bpf interface lock", &lock_class_mtx_sleep }, 358134971Srwatson { "bpf cdev lock", &lock_class_mtx_sleep }, 359144832Spjd { NULL, NULL }, 360143335Srwatson /* 361143335Srwatson * NFS server 362143335Srwatson */ 363143335Srwatson { "nfsd_mtx", &lock_class_mtx_sleep }, 364143335Srwatson { "so_snd", &lock_class_mtx_sleep }, 365134971Srwatson { NULL, NULL }, 366134971Srwatson /* 367168217Swkoszek * Netgraph 368168217Swkoszek */ 369168217Swkoszek { "ng_node", &lock_class_mtx_sleep }, 370168217Swkoszek { "ng_worklist", &lock_class_mtx_sleep }, 371168217Swkoszek { NULL, NULL }, 372168217Swkoszek /* 373144836Spjd * CDEV 374144836Spjd */ 375145425Sjeff { "system map", &lock_class_mtx_sleep }, 376145425Sjeff { "vm page queue mutex", &lock_class_mtx_sleep }, 377145425Sjeff { "vnode interlock", &lock_class_mtx_sleep }, 378144836Spjd { "cdev", &lock_class_mtx_sleep }, 379144836Spjd { NULL, NULL }, 380144836Spjd /* 381166421Skib * kqueue/VFS interaction 382166421Skib */ 383166421Skib { "kqueue", &lock_class_mtx_sleep }, 384166421Skib { "struct mount mtx", &lock_class_mtx_sleep }, 385166421Skib { "vnode interlock", &lock_class_mtx_sleep }, 386166421Skib { NULL, NULL }, 387166421Skib /* 38875464Sjhb * spin locks 38975464Sjhb */ 39084331Sjhb#ifdef SMP 39184331Sjhb { "ap boot", &lock_class_mtx_spin }, 39272224Sjhb#endif 393150582Sjhb { "rm.mutex_mtx", &lock_class_mtx_spin }, 39474912Sjhb { "sio", &lock_class_mtx_spin }, 39572224Sjhb#ifdef __i386__ 39674912Sjhb { "cy", &lock_class_mtx_spin }, 39772224Sjhb#endif 398157584Smarcel { "scc_hwmtx", &lock_class_mtx_spin }, 399124972Sru { "uart_hwmtx", &lock_class_mtx_spin }, 400109015Sjake { "zstty", &lock_class_mtx_spin }, 401161638Sssouhlal { "fast_taskqueue", &lock_class_mtx_spin }, 402122001Sjhb { "intr table", &lock_class_mtx_spin }, 403168856Sjkoshy#ifdef HWPMC_HOOKS 404168856Sjkoshy { "pmc-per-proc", &lock_class_mtx_spin }, 405168856Sjkoshy#endif 406126324Sjhb { "sleepq chain", &lock_class_mtx_spin }, 40774912Sjhb { "sched lock", &lock_class_mtx_spin }, 408122514Sjhb { "turnstile chain", &lock_class_mtx_spin }, 409122514Sjhb { "td_contested", &lock_class_mtx_spin }, 41074912Sjhb { "callout", &lock_class_mtx_spin }, 411136374Srwatson { "entropy harvest mutex", &lock_class_mtx_spin }, 412162285Sscottl { "syscons video lock", &lock_class_mtx_spin }, 413169803Sjeff { "time lock", &lock_class_mtx_spin }, 41465557Sjasone /* 41565557Sjasone * leaf locks 41665557Sjasone */ 41790278Sjhb { "allpmaps", &lock_class_mtx_spin }, 41888322Sjhb { "icu", &lock_class_mtx_spin }, 41972224Sjhb#ifdef SMP 42074912Sjhb { "smp rendezvous", &lock_class_mtx_spin }, 421122849Speter#if defined(__i386__) || defined(__amd64__) 42299862Speter { "tlb", &lock_class_mtx_spin }, 423112993Speter#endif 424108187Sjake#ifdef __sparc64__ 425108187Sjake { "ipi", &lock_class_mtx_spin }, 426146982Smarius { "rtc_mtx", &lock_class_mtx_spin }, 42799862Speter#endif 428108187Sjake#endif 42978785Sjhb { "clk", &lock_class_mtx_spin }, 43095473Sdes { "mutex profiling lock", &lock_class_mtx_spin }, 431111028Sjeff { "kse zombie lock", &lock_class_mtx_spin }, 432103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 433104951Speter#ifdef __ia64__ 434104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 435104951Speter#endif 436115425Speter#if defined(__i386__) || defined(__amd64__) 437111068Speter { "pcicfg", &lock_class_mtx_spin }, 438143204Swpaul { "NDIS thread lock", &lock_class_mtx_spin }, 439111068Speter#endif 440144966Svkashyap { "tw_osl_io_lock", &lock_class_mtx_spin }, 441144966Svkashyap { "tw_osl_q_lock", &lock_class_mtx_spin }, 442144966Svkashyap { "tw_cl_io_lock", &lock_class_mtx_spin }, 443144966Svkashyap { "tw_cl_intr_lock", &lock_class_mtx_spin }, 444144966Svkashyap { "tw_cl_gen_lock", &lock_class_mtx_spin }, 445168856Sjkoshy#ifdef HWPMC_HOOKS 446168856Sjkoshy { "pmc-leaf", &lock_class_mtx_spin }, 447168856Sjkoshy#endif 44874912Sjhb { NULL, NULL }, 44974912Sjhb { NULL, NULL } 45065557Sjasone}; 45165557Sjasone 452105508Sphk#ifdef BLESSING 45365557Sjasone/* 45465557Sjasone * Pairs of locks which have been blessed 45565557Sjasone * Don't complain about order problems with blessed locks 45665557Sjasone */ 45765856Sjhbstatic struct witness_blessed blessed_list[] = { 45865557Sjasone}; 45972200Sbmilekicstatic int blessed_count = 46072200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 461105508Sphk#endif 46265557Sjasone 46374912Sjhb/* 464153133Sjhb * List of locks initialized prior to witness being initialized whose 465153133Sjhb * enrollment is currently deferred. 46674912Sjhb */ 467153133SjhbSTAILQ_HEAD(, lock_object) pending_locks = 468153133Sjhb STAILQ_HEAD_INITIALIZER(pending_locks); 46974912Sjhb 47074912Sjhb/* 47174912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 47274912Sjhb */ 47374912Sjhbstatic int witness_cold = 1; 47474912Sjhb 47574912Sjhb/* 476151629Sjhb * This global is set to 1 once the static lock orders have been enrolled 477151629Sjhb * so that a warning can be issued for any spin locks enrolled later. 478151629Sjhb */ 479151629Sjhbstatic int witness_spin_warn = 0; 480151629Sjhb 481151629Sjhb/* 482153133Sjhb * The WITNESS-enabled diagnostic code. Note that the witness code does 483153133Sjhb * assume that the early boot is single-threaded at least until after this 484153133Sjhb * routine is completed. 48574912Sjhb */ 48671352Sjasonestatic void 48774912Sjhbwitness_initialize(void *dummy __unused) 48865557Sjasone{ 48974912Sjhb struct lock_object *lock; 49074912Sjhb struct witness_order_list_entry *order; 49174912Sjhb struct witness *w, *w1; 49274912Sjhb int i; 49365557Sjasone 49474912Sjhb /* 49574912Sjhb * We have to release Giant before initializing its witness 49674912Sjhb * structure so that WITNESS doesn't get confused. 49774912Sjhb */ 49874912Sjhb mtx_unlock(&Giant); 49974912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 50074912Sjhb 50187593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 50293811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 503164159Skmacy MTX_NOWITNESS | MTX_NOPROFILE); 50474912Sjhb for (i = 0; i < WITNESS_COUNT; i++) 50574912Sjhb witness_free(&w_data[i]); 50674912Sjhb for (i = 0; i < WITNESS_CHILDCOUNT; i++) 50774912Sjhb witness_child_free(&w_childdata[i]); 50874912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 50974912Sjhb witness_lock_list_free(&w_locklistdata[i]); 51074912Sjhb 51174912Sjhb /* First add in all the specified order lists. */ 51274912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 51374912Sjhb w = enroll(order->w_name, order->w_class); 51475569Sjhb if (w == NULL) 51575569Sjhb continue; 51674912Sjhb w->w_file = "order list"; 51774912Sjhb for (order++; order->w_name != NULL; order++) { 51874912Sjhb w1 = enroll(order->w_name, order->w_class); 51975569Sjhb if (w1 == NULL) 52075569Sjhb continue; 52174912Sjhb w1->w_file = "order list"; 522112117Sjhb if (!itismychild(w, w1)) 523112117Sjhb panic("Not enough memory for static orders!"); 52474912Sjhb w = w1; 52565557Sjasone } 52665557Sjasone } 527151629Sjhb witness_spin_warn = 1; 52865557Sjasone 52974912Sjhb /* Iterate through all locks and add them to witness. */ 530153133Sjhb while (!STAILQ_EMPTY(&pending_locks)) { 531153133Sjhb lock = STAILQ_FIRST(&pending_locks); 532153133Sjhb STAILQ_REMOVE_HEAD(&pending_locks, lo_list); 533153133Sjhb KASSERT(lock->lo_flags & LO_WITNESS, 534153133Sjhb ("%s: lock %s is on pending list but not LO_WITNESS", 535153133Sjhb __func__, lock->lo_name)); 536154077Sjhb lock->lo_witness = enroll(lock->lo_type, LOCK_CLASS(lock)); 53774912Sjhb } 53874912Sjhb 53974912Sjhb /* Mark the witness code as being ready for use. */ 540151629Sjhb witness_cold = 0; 54174912Sjhb 54274912Sjhb mtx_lock(&Giant); 54365557Sjasone} 54474912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 54565557Sjasone 546112562Sjhbstatic int 547112562Sjhbsysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 548112562Sjhb{ 549112562Sjhb int error, value; 550112562Sjhb 551112562Sjhb value = witness_watch; 552112562Sjhb error = sysctl_handle_int(oidp, &value, 0, req); 553112562Sjhb if (error != 0 || req->newptr == NULL) 554112562Sjhb return (error); 555112562Sjhb if (value == witness_watch) 556112562Sjhb return (0); 557112562Sjhb if (value != 0) 558112562Sjhb return (EINVAL); 559112562Sjhb witness_watch = 0; 560112562Sjhb return (0); 561112562Sjhb} 562112562Sjhb 56374912Sjhbvoid 56474912Sjhbwitness_init(struct lock_object *lock) 56574912Sjhb{ 56674912Sjhb struct lock_class *class; 56774912Sjhb 568153133Sjhb /* Various sanity checks. */ 569154077Sjhb class = LOCK_CLASS(lock); 57074912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 57174912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 57282284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 57374912Sjhb class->lc_name, lock->lo_name); 57474912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 57574912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 57682284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 57774912Sjhb class->lc_name, lock->lo_name); 57882244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 57982244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 58082284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 58182244Sjhb class->lc_name, lock->lo_name); 58282244Sjhb 583153133Sjhb /* 584153133Sjhb * If we shouldn't watch this lock, then just clear lo_witness. 585153133Sjhb * Otherwise, if witness_cold is set, then it is too early to 586153133Sjhb * enroll this lock, so defer it to witness_initialize() by adding 587153133Sjhb * it to the pending_locks list. If it is not too early, then enroll 588153133Sjhb * the lock now. 589153133Sjhb */ 590153133Sjhb if (witness_watch == 0 || panicstr != NULL || 591153133Sjhb (lock->lo_flags & LO_WITNESS) == 0) 592153133Sjhb lock->lo_witness = NULL; 593153133Sjhb else if (witness_cold) { 594153133Sjhb STAILQ_INSERT_TAIL(&pending_locks, lock, lo_list); 595153133Sjhb lock->lo_flags |= LO_ENROLLPEND; 596153133Sjhb } else 59793811Sjhb lock->lo_witness = enroll(lock->lo_type, class); 59874912Sjhb} 59974912Sjhb 60074912Sjhbvoid 60174912Sjhbwitness_destroy(struct lock_object *lock) 60274912Sjhb{ 603154077Sjhb struct lock_class *class; 60475362Sjhb struct witness *w; 60574912Sjhb 606154077Sjhb class = LOCK_CLASS(lock); 60774912Sjhb if (witness_cold) 60874912Sjhb panic("lock (%s) %s destroyed while witness_cold", 609154077Sjhb class->lc_name, lock->lo_name); 61074912Sjhb 61176272Sjhb /* XXX: need to verify that no one holds the lock */ 612153133Sjhb if ((lock->lo_flags & (LO_WITNESS | LO_ENROLLPEND)) == LO_WITNESS && 613153133Sjhb lock->lo_witness != NULL) { 614153133Sjhb w = lock->lo_witness; 61575362Sjhb mtx_lock_spin(&w_mtx); 61697948Sjhb MPASS(w->w_refcount > 0); 61775362Sjhb w->w_refcount--; 618112117Sjhb 619112117Sjhb /* 620112117Sjhb * Lock is already released if we have an allocation failure 621112117Sjhb * and depart() fails. 622112117Sjhb */ 623112117Sjhb if (w->w_refcount != 0 || depart(w)) 624112117Sjhb mtx_unlock_spin(&w_mtx); 62575362Sjhb } 62675362Sjhb 627153133Sjhb /* 628153133Sjhb * If this lock is destroyed before witness is up and running, 629153133Sjhb * remove it from the pending list. 630153133Sjhb */ 631153133Sjhb if (lock->lo_flags & LO_ENROLLPEND) { 632153133Sjhb STAILQ_REMOVE(&pending_locks, lock, lock_object, lo_list); 633153133Sjhb lock->lo_flags &= ~LO_ENROLLPEND; 634153133Sjhb } 63574912Sjhb} 63674912Sjhb 637112115Sjhb#ifdef DDB 63871352Sjasonestatic void 639149979Struckmanwitness_levelall (void) 640149979Struckman{ 641149979Struckman struct witness_list *list; 642149979Struckman struct witness *w, *w1; 643149979Struckman 644149979Struckman /* 645149979Struckman * First clear all levels. 646149979Struckman */ 647149979Struckman STAILQ_FOREACH(w, &w_all, w_list) { 648149979Struckman w->w_level = 0; 649149979Struckman } 650149979Struckman 651149979Struckman /* 652149979Struckman * Look for locks with no parent and level all their descendants. 653149979Struckman */ 654149979Struckman STAILQ_FOREACH(w, &w_all, w_list) { 655149979Struckman /* 656149979Struckman * This is just an optimization, technically we could get 657149979Struckman * away just walking the all list each time. 658149979Struckman */ 659149979Struckman if (w->w_class->lc_flags & LC_SLEEPLOCK) 660149979Struckman list = &w_sleep; 661149979Struckman else 662149979Struckman list = &w_spin; 663149979Struckman STAILQ_FOREACH(w1, list, w_typelist) { 664149979Struckman if (isitmychild(w1, w)) 665149979Struckman goto skip; 666149979Struckman } 667149979Struckman witness_leveldescendents(w, 0); 668149979Struckman skip: 669149979Struckman ; /* silence GCC 3.x */ 670149979Struckman } 671149979Struckman} 672149979Struckman 673149979Struckmanstatic void 674149979Struckmanwitness_leveldescendents(struct witness *parent, int level) 675149979Struckman{ 676149979Struckman struct witness_child_list_entry *wcl; 677149979Struckman int i; 678149979Struckman 679149979Struckman if (parent->w_level < level) 680149979Struckman parent->w_level = level; 681149979Struckman level++; 682149979Struckman for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 683149979Struckman for (i = 0; i < wcl->wcl_count; i++) 684149979Struckman witness_leveldescendents(wcl->wcl_children[i], level); 685149979Struckman} 686149979Struckman 687149979Struckmanstatic void 688149979Struckmanwitness_displaydescendants(void(*prnt)(const char *fmt, ...), 689149979Struckman struct witness *parent, int indent) 690149979Struckman{ 691149979Struckman struct witness_child_list_entry *wcl; 692149979Struckman int i, level; 693149979Struckman 694149979Struckman level = parent->w_level; 695149979Struckman prnt("%-2d", level); 696149979Struckman for (i = 0; i < indent; i++) 697149979Struckman prnt(" "); 698149979Struckman if (parent->w_refcount > 0) 699149979Struckman prnt("%s", parent->w_name); 700149979Struckman else 701149979Struckman prnt("(dead)"); 702149979Struckman if (parent->w_displayed) { 703149979Struckman prnt(" -- (already displayed)\n"); 704149979Struckman return; 705149979Struckman } 706149979Struckman parent->w_displayed = 1; 707149979Struckman if (parent->w_refcount > 0) { 708149979Struckman if (parent->w_file != NULL) 709149979Struckman prnt(" -- last acquired @ %s:%d", parent->w_file, 710149979Struckman parent->w_line); 711149979Struckman } 712149979Struckman prnt("\n"); 713149979Struckman for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 714149979Struckman for (i = 0; i < wcl->wcl_count; i++) 715149979Struckman witness_displaydescendants(prnt, 716149979Struckman wcl->wcl_children[i], indent + 1); 717149979Struckman} 718149979Struckman 719149979Struckmanstatic void 72074912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...), 72174912Sjhb struct witness_list *list) 72271352Sjasone{ 723112118Sjhb struct witness *w; 72471352Sjasone 72574912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 726112118Sjhb if (w->w_file == NULL || w->w_level > 0) 72771352Sjasone continue; 72871352Sjasone /* 72971352Sjasone * This lock has no anscestors, display its descendants. 73071352Sjasone */ 731112118Sjhb witness_displaydescendants(prnt, w, 0); 73271352Sjasone } 73374912Sjhb} 73472224Sjhb 73574912Sjhbstatic void 73674912Sjhbwitness_display(void(*prnt)(const char *fmt, ...)) 73774912Sjhb{ 73874912Sjhb struct witness *w; 73974912Sjhb 74082284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 74174912Sjhb witness_levelall(); 74274912Sjhb 743112118Sjhb /* Clear all the displayed flags. */ 744112118Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 745112118Sjhb w->w_displayed = 0; 746112118Sjhb } 747112118Sjhb 74872224Sjhb /* 74974930Sjhb * First, handle sleep locks which have been acquired at least 75074912Sjhb * once. 75174912Sjhb */ 75274912Sjhb prnt("Sleep locks:\n"); 75374912Sjhb witness_display_list(prnt, &w_sleep); 75474912Sjhb 75574912Sjhb /* 75674930Sjhb * Now do spin locks which have been acquired at least once. 75772224Sjhb */ 75874912Sjhb prnt("\nSpin locks:\n"); 75974912Sjhb witness_display_list(prnt, &w_spin); 76072224Sjhb 76172224Sjhb /* 76274930Sjhb * Finally, any locks which have not been acquired yet. 76372224Sjhb */ 76474912Sjhb prnt("\nLocks which were never acquired:\n"); 76574912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 76697948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 76771352Sjasone continue; 76874912Sjhb prnt("%s\n", w->w_name); 76971352Sjasone } 77071352Sjasone} 771112115Sjhb#endif /* DDB */ 77271352Sjasone 773112116Sjhb/* Trim useless garbage from filenames. */ 774112116Sjhbstatic const char * 775112116Sjhbfixup_filename(const char *file) 776112116Sjhb{ 777112116Sjhb 778112116Sjhb if (file == NULL) 779112116Sjhb return (NULL); 780112116Sjhb while (strncmp(file, "../", 3) == 0) 781112116Sjhb file += 3; 782112116Sjhb return (file); 783112116Sjhb} 784112116Sjhb 785125160Sjhbint 786125160Sjhbwitness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 787125160Sjhb{ 788125160Sjhb 789125160Sjhb if (witness_watch == 0 || panicstr != NULL) 790125160Sjhb return (0); 791125160Sjhb 792125160Sjhb /* Require locks that witness knows about. */ 793125160Sjhb if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 794125160Sjhb lock2->lo_witness == NULL) 795125160Sjhb return (EINVAL); 796125160Sjhb 797125160Sjhb MPASS(!mtx_owned(&w_mtx)); 798125160Sjhb mtx_lock_spin(&w_mtx); 799125160Sjhb 800125160Sjhb /* 801125160Sjhb * If we already have either an explicit or implied lock order that 802125160Sjhb * is the other way around, then return an error. 803125160Sjhb */ 804125160Sjhb if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 805125160Sjhb mtx_unlock_spin(&w_mtx); 806125160Sjhb return (EDOOFUS); 807125160Sjhb } 808125160Sjhb 809125160Sjhb /* Try to add the new order. */ 810125160Sjhb CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 811125160Sjhb lock2->lo_type, lock1->lo_type); 812125160Sjhb if (!itismychild(lock1->lo_witness, lock2->lo_witness)) 813125160Sjhb return (ENOMEM); 814125160Sjhb mtx_unlock_spin(&w_mtx); 815125160Sjhb return (0); 816125160Sjhb} 817125160Sjhb 81865557Sjasonevoid 819125160Sjhbwitness_checkorder(struct lock_object *lock, int flags, const char *file, 820125160Sjhb int line) 82165557Sjasone{ 82274912Sjhb struct lock_list_entry **lock_list, *lle; 82376272Sjhb struct lock_instance *lock1, *lock2; 82474912Sjhb struct lock_class *class; 82565856Sjhb struct witness *w, *w1; 82683366Sjulian struct thread *td; 82774912Sjhb int i, j; 82865557Sjasone 829112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 83080747Sjhb panicstr != NULL) 83171320Sjasone return; 832125160Sjhb 833125160Sjhb /* 834125160Sjhb * Try locks do not block if they fail to acquire the lock, thus 835125160Sjhb * there is no danger of deadlocks or of switching while holding a 836125160Sjhb * spin lock if we acquire a lock via a try operation. This 837125160Sjhb * function shouldn't even be called for try locks, so panic if 838125160Sjhb * that happens. 839125160Sjhb */ 840125160Sjhb if (flags & LOP_TRYLOCK) 841125160Sjhb panic("%s should not be called for try lock operations", 842125160Sjhb __func__); 843125160Sjhb 84474912Sjhb w = lock->lo_witness; 845154077Sjhb class = LOCK_CLASS(lock); 84683366Sjulian td = curthread; 847112116Sjhb file = fixup_filename(file); 84865557Sjasone 84974912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 85093676Sjhb /* 85193676Sjhb * Since spin locks include a critical section, this check 852131884Sjhb * implicitly enforces a lock order of all sleep locks before 85393676Sjhb * all spin locks. 85493676Sjhb */ 855136304Sgreen if (td->td_critnest != 0 && !kdb_active) 85674912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 85774912Sjhb class->lc_name, lock->lo_name, file, line); 858131884Sjhb 859131884Sjhb /* 860131884Sjhb * If this is the first lock acquired then just return as 861131884Sjhb * no order checking is needed. 862131884Sjhb */ 863131884Sjhb if (td->td_sleeplocks == NULL) 864131884Sjhb return; 86583366Sjulian lock_list = &td->td_sleeplocks; 866131884Sjhb } else { 867131884Sjhb /* 868131884Sjhb * If this is the first lock, just return as no order 869131884Sjhb * checking is needed. We check this in both if clauses 870131884Sjhb * here as unifying the check would require us to use a 871131884Sjhb * critical section to ensure we don't migrate while doing 872131884Sjhb * the check. Note that if this is not the first lock, we 873131884Sjhb * are already in a critical section and are safe for the 874131884Sjhb * rest of the check. 875131884Sjhb */ 876131884Sjhb if (PCPU_GET(spinlocks) == NULL) 877131884Sjhb return; 87888899Sjhb lock_list = PCPU_PTR(spinlocks); 879131884Sjhb } 88065557Sjasone 88176772Sjhb /* 882125160Sjhb * Check to see if we are recursing on a lock we already own. If 883125160Sjhb * so, make sure that we don't mismatch exclusive and shared lock 884125160Sjhb * acquires. 88576272Sjhb */ 88676272Sjhb lock1 = find_instance(*lock_list, lock); 88776272Sjhb if (lock1 != NULL) { 88876272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 88976272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 89076272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 89176272Sjhb class->lc_name, lock->lo_name, file, line); 89276272Sjhb printf("while exclusively locked from %s:%d\n", 89376272Sjhb lock1->li_file, lock1->li_line); 89476272Sjhb panic("share->excl"); 89576272Sjhb } 89676272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 89776272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 89876272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 89976272Sjhb class->lc_name, lock->lo_name, file, line); 90076272Sjhb printf("while share locked from %s:%d\n", 90176272Sjhb lock1->li_file, lock1->li_line); 90276272Sjhb panic("excl->share"); 90376272Sjhb } 90476272Sjhb return; 90576272Sjhb } 90676272Sjhb 90776272Sjhb /* 908112112Sjhb * Try locks do not block if they fail to acquire the lock, thus 909112112Sjhb * there is no danger of deadlocks or of switching while holding a 910112112Sjhb * spin lock if we acquire a lock via a try operation. 911112112Sjhb */ 912112112Sjhb if (flags & LOP_TRYLOCK) 913125160Sjhb return; 914112112Sjhb 915112112Sjhb /* 91674912Sjhb * Check for duplicate locks of the same type. Note that we only 91774912Sjhb * have to check for this on the last lock we just acquired. Any 91874912Sjhb * other cases will be caught as lock order violations. 91974912Sjhb */ 92076272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 92176272Sjhb w1 = lock1->li_lock->lo_witness; 92274912Sjhb if (w1 == w) { 923145422Sjeff if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) || 924145422Sjeff (flags & LOP_DUPOK)) 925125160Sjhb return; 92665557Sjasone w->w_same_squawked = 1; 92775755Sjhb printf("acquiring duplicate lock of same type: \"%s\"\n", 92893811Sjhb lock->lo_type); 92993811Sjhb printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 93093811Sjhb lock1->li_file, lock1->li_line); 93193811Sjhb printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 932131930Smarcel#ifdef KDB 933125160Sjhb goto debugger; 934125160Sjhb#else 935125160Sjhb return; 936112115Sjhb#endif 93765557Sjasone } 93865557Sjasone MPASS(!mtx_owned(&w_mtx)); 93974912Sjhb mtx_lock_spin(&w_mtx); 94065557Sjasone /* 941111881Sjhb * If we know that the the lock we are acquiring comes after 942111881Sjhb * the lock we most recently acquired in the lock order tree, 943111881Sjhb * then there is no need for any further checks. 944111881Sjhb */ 945149441Struckman if (isitmychild(w1, w)) { 94674912Sjhb mtx_unlock_spin(&w_mtx); 947125160Sjhb return; 94865557Sjasone } 94974912Sjhb for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 95074912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 95165557Sjasone 95274912Sjhb MPASS(j < WITNESS_COUNT); 95376272Sjhb lock1 = &lle->ll_children[i]; 95476272Sjhb w1 = lock1->li_lock->lo_witness; 95574912Sjhb 95674912Sjhb /* 95774912Sjhb * If this lock doesn't undergo witness checking, 95874912Sjhb * then skip it. 95974912Sjhb */ 96074912Sjhb if (w1 == NULL) { 96176272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 96274912Sjhb ("lock missing witness structure")); 96374912Sjhb continue; 96474912Sjhb } 96576272Sjhb /* 966111881Sjhb * If we are locking Giant and this is a sleepable 96776272Sjhb * lock, then skip it. 96876272Sjhb */ 969111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 970167787Sjhb lock == &Giant.lock_object) 97176272Sjhb continue; 97293690Sjhb /* 97393690Sjhb * If we are locking a sleepable lock and this lock 974111881Sjhb * is Giant, then skip it. 97593690Sjhb */ 976111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 977167787Sjhb lock1->li_lock == &Giant.lock_object) 978111881Sjhb continue; 979111881Sjhb /* 980111881Sjhb * If we are locking a sleepable lock and this lock 981111881Sjhb * isn't sleepable, we want to treat it as a lock 982111881Sjhb * order violation to enfore a general lock order of 983111881Sjhb * sleepable locks before non-sleepable locks. 984111881Sjhb */ 985149738Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 986111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 987149738Sjhb goto reversal; 988149738Sjhb /* 989150179Sjhb * If we are locking Giant and this is a non-sleepable 990150179Sjhb * lock, then treat it as a reversal. 991150179Sjhb */ 992150179Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 && 993167787Sjhb lock == &Giant.lock_object) 994150179Sjhb goto reversal; 995150179Sjhb /* 996149738Sjhb * Check the lock order hierarchy for a reveresal. 997149738Sjhb */ 998149738Sjhb if (!isitmydescendant(w, w1)) 99974912Sjhb continue; 1000149738Sjhb reversal: 100174912Sjhb /* 100274912Sjhb * We have a lock order violation, check to see if it 100374912Sjhb * is allowed or has already been yelled about. 100474912Sjhb */ 100574912Sjhb mtx_unlock_spin(&w_mtx); 1006105508Sphk#ifdef BLESSING 1007125160Sjhb /* 1008125160Sjhb * If the lock order is blessed, just bail. We don't 1009125160Sjhb * look for other lock order violations though, which 1010125160Sjhb * may be a bug. 1011125160Sjhb */ 101265557Sjasone if (blessed(w, w1)) 1013125160Sjhb return; 1014105508Sphk#endif 1015167787Sjhb if (lock1->li_lock == &Giant.lock_object) { 101665557Sjasone if (w1->w_Giant_squawked) 1017125160Sjhb return; 101865557Sjasone else 101965557Sjasone w1->w_Giant_squawked = 1; 102065557Sjasone } else { 102165557Sjasone if (w1->w_other_squawked) 1022125160Sjhb return; 102365557Sjasone else 102465557Sjasone w1->w_other_squawked = 1; 102565557Sjasone } 102674912Sjhb /* 102774912Sjhb * Ok, yell about it. 102874912Sjhb */ 1029150179Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 1030150179Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 1031150179Sjhb printf( 1032150179Sjhb "lock order reversal: (sleepable after non-sleepable)\n"); 1033150179Sjhb else if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0 1034167787Sjhb && lock == &Giant.lock_object) 1035150179Sjhb printf( 1036150179Sjhb "lock order reversal: (Giant after non-sleepable)\n"); 1037150179Sjhb else 1038150179Sjhb printf("lock order reversal:\n"); 103974912Sjhb /* 104074912Sjhb * Try to locate an earlier lock with 104174912Sjhb * witness w in our list. 104274912Sjhb */ 104374912Sjhb do { 104476272Sjhb lock2 = &lle->ll_children[i]; 104576272Sjhb MPASS(lock2->li_lock != NULL); 104676272Sjhb if (lock2->li_lock->lo_witness == w) 104774912Sjhb break; 104874912Sjhb if (i == 0 && lle->ll_next != NULL) { 104974912Sjhb lle = lle->ll_next; 105074912Sjhb i = lle->ll_count - 1; 1051106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 1052125160Sjhb } else 1053125160Sjhb i--; 105474912Sjhb } while (i >= 0); 105576272Sjhb if (i < 0) { 105693811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 105793811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 105893811Sjhb lock1->li_lock->lo_type, lock1->li_file, 105976272Sjhb lock1->li_line); 106093811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 106193811Sjhb lock->lo_name, lock->lo_type, file, line); 106276272Sjhb } else { 106393811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 106493811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 106593811Sjhb lock2->li_lock->lo_type, lock2->li_file, 106676272Sjhb lock2->li_line); 106793811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 106893811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 106993811Sjhb lock1->li_lock->lo_type, lock1->li_file, 107076272Sjhb lock1->li_line); 107193811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 107293811Sjhb lock->lo_name, lock->lo_type, file, line); 107376272Sjhb } 1074131930Smarcel#ifdef KDB 1075125160Sjhb goto debugger; 1076125160Sjhb#else 1077125160Sjhb return; 1078112115Sjhb#endif 107965557Sjasone } 108065557Sjasone } 108176272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 108278871Sjhb /* 1083125160Sjhb * If requested, build a new lock order. However, don't build a new 1084125160Sjhb * relationship between a sleepable lock and Giant if it is in the 1085125160Sjhb * wrong direction. The correct lock order is that sleepable locks 1086125160Sjhb * always come before Giant. 108778871Sjhb */ 1088125160Sjhb if (flags & LOP_NEWORDER && 1089167787Sjhb !(lock1->li_lock == &Giant.lock_object && 1090112117Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0)) { 109187593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 109293811Sjhb lock->lo_type, lock1->li_lock->lo_type); 109378871Sjhb if (!itismychild(lock1->li_lock->lo_witness, w)) 1094112117Sjhb /* Witness is dead. */ 1095112117Sjhb return; 109678871Sjhb } 1097112117Sjhb mtx_unlock_spin(&w_mtx); 1098125160Sjhb return; 109965557Sjasone 1100131930Smarcel#ifdef KDB 1101125160Sjhbdebugger: 1102125160Sjhb if (witness_trace) 1103131930Smarcel kdb_backtrace(); 1104131930Smarcel if (witness_kdb) 1105131930Smarcel kdb_enter(__func__); 1106125160Sjhb#endif 1107125160Sjhb} 1108125160Sjhb 1109125160Sjhbvoid 1110125160Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 1111125160Sjhb{ 1112125160Sjhb struct lock_list_entry **lock_list, *lle; 1113125160Sjhb struct lock_instance *instance; 1114125160Sjhb struct witness *w; 1115125160Sjhb struct thread *td; 1116125160Sjhb 1117125160Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1118125160Sjhb panicstr != NULL) 1119125160Sjhb return; 1120125160Sjhb w = lock->lo_witness; 1121125160Sjhb td = curthread; 1122125160Sjhb file = fixup_filename(file); 1123125160Sjhb 1124125160Sjhb /* Determine lock list for this lock. */ 1125154077Sjhb if (LOCK_CLASS(lock)->lc_flags & LC_SLEEPLOCK) 1126125160Sjhb lock_list = &td->td_sleeplocks; 1127125160Sjhb else 1128125160Sjhb lock_list = PCPU_PTR(spinlocks); 1129125160Sjhb 1130125160Sjhb /* Check to see if we are recursing on a lock we already own. */ 1131125160Sjhb instance = find_instance(*lock_list, lock); 1132125160Sjhb if (instance != NULL) { 1133125160Sjhb instance->li_flags++; 1134125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1135125160Sjhb td->td_proc->p_pid, lock->lo_name, 1136125160Sjhb instance->li_flags & LI_RECURSEMASK); 1137125160Sjhb instance->li_file = file; 1138125160Sjhb instance->li_line = line; 1139125160Sjhb return; 1140110779Speter } 1141125160Sjhb 1142125160Sjhb /* Update per-witness last file and line acquire. */ 114365557Sjasone w->w_file = file; 114465557Sjasone w->w_line = line; 1145125160Sjhb 1146125160Sjhb /* Find the next open lock instance in the list and fill it. */ 114774912Sjhb lle = *lock_list; 114876272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 114978785Sjhb lle = witness_lock_list_get(); 115078785Sjhb if (lle == NULL) 115165557Sjasone return; 115278785Sjhb lle->ll_next = *lock_list; 115387593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 115484680Sjhb td->td_proc->p_pid, lle); 115578785Sjhb *lock_list = lle; 115665557Sjasone } 1157125160Sjhb instance = &lle->ll_children[lle->ll_count++]; 1158125160Sjhb instance->li_lock = lock; 1159125160Sjhb instance->li_line = line; 1160125160Sjhb instance->li_file = file; 116176272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 1162125160Sjhb instance->li_flags = LI_EXCLUSIVE; 116376272Sjhb else 1164125160Sjhb instance->li_flags = 0; 116587593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 116684680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 116765557Sjasone} 116865557Sjasone 116965557Sjasonevoid 117082244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 117182244Sjhb{ 117282244Sjhb struct lock_instance *instance; 117382244Sjhb struct lock_class *class; 117482244Sjhb 117582284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1176112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 117782244Sjhb return; 1178154077Sjhb class = LOCK_CLASS(lock); 1179112116Sjhb file = fixup_filename(file); 118082244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 118182244Sjhb panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 118282244Sjhb class->lc_name, lock->lo_name, file, line); 118382244Sjhb if ((flags & LOP_TRYLOCK) == 0) 118482244Sjhb panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 118582244Sjhb lock->lo_name, file, line); 1186154077Sjhb if ((class->lc_flags & LC_SLEEPLOCK) == 0) 118782244Sjhb panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 118882244Sjhb class->lc_name, lock->lo_name, file, line); 118983366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 119082244Sjhb if (instance == NULL) 119182244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 119282244Sjhb class->lc_name, lock->lo_name, file, line); 119382244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0) 119482244Sjhb panic("upgrade of exclusive lock (%s) %s @ %s:%d", 119582244Sjhb class->lc_name, lock->lo_name, file, line); 119682244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 119782244Sjhb panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 119882244Sjhb class->lc_name, lock->lo_name, 119982244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 120082244Sjhb instance->li_flags |= LI_EXCLUSIVE; 120182244Sjhb} 120282244Sjhb 120382244Sjhbvoid 120482244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 120582244Sjhb int line) 120682244Sjhb{ 120782244Sjhb struct lock_instance *instance; 120882244Sjhb struct lock_class *class; 120982244Sjhb 121082284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1211112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 121282244Sjhb return; 1213154077Sjhb class = LOCK_CLASS(lock); 1214112116Sjhb file = fixup_filename(file); 121582244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 121682244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 121782244Sjhb class->lc_name, lock->lo_name, file, line); 1218154077Sjhb if ((class->lc_flags & LC_SLEEPLOCK) == 0) 121982244Sjhb panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 122082244Sjhb class->lc_name, lock->lo_name, file, line); 122183366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 122282244Sjhb if (instance == NULL) 122382244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 122482244Sjhb class->lc_name, lock->lo_name, file, line); 122582244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0) 122682244Sjhb panic("downgrade of shared lock (%s) %s @ %s:%d", 122782244Sjhb class->lc_name, lock->lo_name, file, line); 122882244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 122982244Sjhb panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 123082244Sjhb class->lc_name, lock->lo_name, 123182244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 123282244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 123382244Sjhb} 123482244Sjhb 123582244Sjhbvoid 123674912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 123765557Sjasone{ 123874912Sjhb struct lock_list_entry **lock_list, *lle; 123976272Sjhb struct lock_instance *instance; 124074912Sjhb struct lock_class *class; 124183366Sjulian struct thread *td; 124292858Simp register_t s; 124374912Sjhb int i, j; 124465557Sjasone 1245112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 124680747Sjhb panicstr != NULL) 124771352Sjasone return; 124883366Sjulian td = curthread; 1249154077Sjhb class = LOCK_CLASS(lock); 1250112116Sjhb file = fixup_filename(file); 1251125160Sjhb 1252125160Sjhb /* Find lock instance associated with this lock. */ 125376272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 125483366Sjulian lock_list = &td->td_sleeplocks; 125576272Sjhb else 125674912Sjhb lock_list = PCPU_PTR(spinlocks); 125774912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 125876272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 125976272Sjhb instance = &(*lock_list)->ll_children[i]; 1260125160Sjhb if (instance->li_lock == lock) 1261125160Sjhb goto found; 126276272Sjhb } 126376272Sjhb panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 126476272Sjhb file, line); 1265125160Sjhbfound: 1266125160Sjhb 1267125160Sjhb /* First, check for shared/exclusive mismatches. */ 1268125160Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 1269125160Sjhb (flags & LOP_EXCLUSIVE) == 0) { 1270125160Sjhb printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1271125160Sjhb lock->lo_name, file, line); 1272125160Sjhb printf("while exclusively locked from %s:%d\n", 1273125160Sjhb instance->li_file, instance->li_line); 1274125160Sjhb panic("excl->ushare"); 1275125160Sjhb } 1276125160Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 1277125160Sjhb (flags & LOP_EXCLUSIVE) != 0) { 1278125160Sjhb printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1279125160Sjhb lock->lo_name, file, line); 1280125160Sjhb printf("while share locked from %s:%d\n", instance->li_file, 1281125160Sjhb instance->li_line); 1282125160Sjhb panic("share->uexcl"); 1283125160Sjhb } 1284125160Sjhb 1285125160Sjhb /* If we are recursed, unrecurse. */ 1286125160Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1287125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1288125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1289125160Sjhb instance->li_flags); 1290125160Sjhb instance->li_flags--; 1291125160Sjhb return; 1292125160Sjhb } 1293125160Sjhb 1294125160Sjhb /* Otherwise, remove this item from the list. */ 1295125160Sjhb s = intr_disable(); 1296125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1297125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1298125160Sjhb (*lock_list)->ll_count - 1); 1299125160Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 1300125160Sjhb (*lock_list)->ll_children[j] = 1301125160Sjhb (*lock_list)->ll_children[j + 1]; 1302125160Sjhb (*lock_list)->ll_count--; 1303125160Sjhb intr_restore(s); 1304125160Sjhb 1305125160Sjhb /* If this lock list entry is now empty, free it. */ 1306125160Sjhb if ((*lock_list)->ll_count == 0) { 1307125160Sjhb lle = *lock_list; 1308125160Sjhb *lock_list = lle->ll_next; 1309125160Sjhb CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1310125160Sjhb td->td_proc->p_pid, lle); 1311125160Sjhb witness_lock_list_free(lle); 1312125160Sjhb } 131365557Sjasone} 131465557Sjasone 131574912Sjhb/* 1316111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 1317111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 1318111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 1319111881Sjhb * console along with a list of the offending locks. If indicated in the 1320111881Sjhb * flags then a failure results in a panic as well. 132174912Sjhb */ 132265557Sjasoneint 1323111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 132465557Sjasone{ 1325111881Sjhb struct lock_list_entry *lle; 132676272Sjhb struct lock_instance *lock1; 132783366Sjulian struct thread *td; 1328111881Sjhb va_list ap; 132974912Sjhb int i, n; 133065557Sjasone 1331112562Sjhb if (witness_cold || witness_watch == 0 || panicstr != NULL) 133274912Sjhb return (0); 133374912Sjhb n = 0; 133483366Sjulian td = curthread; 1335111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 133674912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 133776272Sjhb lock1 = &lle->ll_children[i]; 1338111881Sjhb if (lock1->li_lock == lock) 1339111881Sjhb continue; 1340111881Sjhb if (flags & WARN_GIANTOK && 1341167787Sjhb lock1->li_lock == &Giant.lock_object) 134274912Sjhb continue; 1343111881Sjhb if (flags & WARN_SLEEPOK && 1344111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 134576272Sjhb continue; 1346111881Sjhb if (n == 0) { 1347111881Sjhb va_start(ap, fmt); 1348111881Sjhb vprintf(fmt, ap); 1349111881Sjhb va_end(ap); 1350111881Sjhb printf(" with the following"); 1351111881Sjhb if (flags & WARN_SLEEPOK) 1352111881Sjhb printf(" non-sleepable"); 1353118441Sjhb printf(" locks held:\n"); 135476272Sjhb } 135574912Sjhb n++; 1356111881Sjhb witness_list_lock(lock1); 135774912Sjhb } 1358111881Sjhb if (PCPU_GET(spinlocks) != NULL) { 135997006Sjhb /* 136097006Sjhb * Since we already hold a spinlock preemption is 136197006Sjhb * already blocked. 136297006Sjhb */ 1363111881Sjhb if (n == 0) { 1364111881Sjhb va_start(ap, fmt); 1365111881Sjhb vprintf(fmt, ap); 1366111881Sjhb va_end(ap); 1367111881Sjhb printf(" with the following"); 1368111881Sjhb if (flags & WARN_SLEEPOK) 1369111881Sjhb printf(" non-sleepable"); 1370118441Sjhb printf(" locks held:\n"); 1371111881Sjhb } 1372111881Sjhb n += witness_list_locks(PCPU_PTR(spinlocks)); 137365557Sjasone } 1374111881Sjhb if (flags & WARN_PANIC && n) 1375111881Sjhb panic("witness_warn"); 1376131930Smarcel#ifdef KDB 1377131930Smarcel else if (witness_kdb && n) 1378131930Smarcel kdb_enter(__func__); 1379127323Salfred else if (witness_trace && n) 1380131930Smarcel kdb_backtrace(); 1381111881Sjhb#endif 138265557Sjasone return (n); 138365557Sjasone} 138465557Sjasone 1385102448Siedowseconst char * 1386102448Siedowsewitness_file(struct lock_object *lock) 1387102448Siedowse{ 1388102448Siedowse struct witness *w; 1389102448Siedowse 1390112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1391102448Siedowse return ("?"); 1392102448Siedowse w = lock->lo_witness; 1393102448Siedowse return (w->w_file); 1394102448Siedowse} 1395102448Siedowse 1396102448Siedowseint 1397102448Siedowsewitness_line(struct lock_object *lock) 1398102448Siedowse{ 1399102448Siedowse struct witness *w; 1400102448Siedowse 1401112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1402102448Siedowse return (0); 1403102448Siedowse w = lock->lo_witness; 1404102448Siedowse return (w->w_line); 1405102448Siedowse} 1406102448Siedowse 140765856Sjhbstatic struct witness * 140874912Sjhbenroll(const char *description, struct lock_class *lock_class) 140965557Sjasone{ 141074912Sjhb struct witness *w; 141165557Sjasone 1412125348Sjhb if (witness_watch == 0 || panicstr != NULL) 141365557Sjasone return (NULL); 141474912Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 141565557Sjasone return (NULL); 141674912Sjhb mtx_lock_spin(&w_mtx); 141774912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 141897948Sjhb if (w->w_name == description || (w->w_refcount > 0 && 141997948Sjhb strcmp(description, w->w_name) == 0)) { 142075362Sjhb w->w_refcount++; 142174912Sjhb mtx_unlock_spin(&w_mtx); 142274912Sjhb if (lock_class != w->w_class) 142374912Sjhb panic( 142474912Sjhb "lock (%s) %s does not match earlier (%s) lock", 142574912Sjhb description, lock_class->lc_name, 142674912Sjhb w->w_class->lc_name); 142765557Sjasone return (w); 142865557Sjasone } 142965557Sjasone } 143065557Sjasone if ((w = witness_get()) == NULL) 1431153853Sjhb goto out; 143274912Sjhb w->w_name = description; 143374912Sjhb w->w_class = lock_class; 143475362Sjhb w->w_refcount = 1; 143574912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 1436149441Struckman if (lock_class->lc_flags & LC_SPINLOCK) { 143774912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1438149441Struckman w_spin_cnt++; 1439149441Struckman } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 144074912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1441149441Struckman w_sleep_cnt++; 1442149441Struckman } else { 144375364Sbp mtx_unlock_spin(&w_mtx); 144474912Sjhb panic("lock class %s is not sleep or spin", 144574912Sjhb lock_class->lc_name); 144675364Sbp } 144774912Sjhb mtx_unlock_spin(&w_mtx); 1448153853Sjhbout: 1449153853Sjhb /* 1450153853Sjhb * We issue a warning for any spin locks not defined in the static 1451153853Sjhb * order list as a way to discourage their use (folks should really 1452153853Sjhb * be using non-spin mutexes most of the time). However, several 1453153853Sjhb * 3rd part device drivers use spin locks because that is all they 1454153853Sjhb * have available on Windows and Linux and they think that normal 1455153853Sjhb * mutexes are insufficient. 1456153853Sjhb */ 1457153853Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_spin_warn) 1458153853Sjhb printf("WITNESS: spin lock %s not in order list\n", 1459153853Sjhb description); 146065557Sjasone return (w); 146165557Sjasone} 146265557Sjasone 1463112117Sjhb/* Don't let the door bang you on the way out... */ 146465557Sjasonestatic int 1465112117Sjhbdepart(struct witness *w) 146665557Sjasone{ 1467112117Sjhb struct witness_child_list_entry *wcl, *nwcl; 146874912Sjhb struct witness_list *list; 1469112117Sjhb struct witness *parent; 147065557Sjasone 1471112117Sjhb MPASS(w->w_refcount == 0); 1472149441Struckman if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1473112117Sjhb list = &w_sleep; 1474149441Struckman w_sleep_cnt--; 1475149441Struckman } else { 1476112117Sjhb list = &w_spin; 1477149441Struckman w_spin_cnt--; 1478149441Struckman } 1479112117Sjhb /* 1480112117Sjhb * First, we run through the entire tree looking for any 1481112117Sjhb * witnesses that the outgoing witness is a child of. For 1482112117Sjhb * each parent that we find, we reparent all the direct 1483112117Sjhb * children of the outgoing witness to its parent. 1484112117Sjhb */ 1485112117Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 1486112117Sjhb if (!isitmychild(parent, w)) 1487112117Sjhb continue; 1488112117Sjhb removechild(parent, w); 1489112117Sjhb } 1490112117Sjhb 1491112117Sjhb /* 1492112117Sjhb * Now we go through and free up the child list of the 1493112117Sjhb * outgoing witness. 1494112117Sjhb */ 1495112117Sjhb for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1496112117Sjhb nwcl = wcl->wcl_next; 1497149441Struckman w_child_cnt--; 1498112117Sjhb witness_child_free(wcl); 1499112117Sjhb } 1500112117Sjhb 1501112117Sjhb /* 1502112117Sjhb * Detach from various lists and free. 1503112117Sjhb */ 1504112117Sjhb STAILQ_REMOVE(list, w, witness, w_typelist); 1505112117Sjhb STAILQ_REMOVE(&w_all, w, witness, w_list); 1506112117Sjhb witness_free(w); 1507112117Sjhb 1508112117Sjhb return (1); 1509112117Sjhb} 1510112117Sjhb 1511112117Sjhb/* 1512112117Sjhb * Add "child" as a direct child of "parent". Returns false if 1513112117Sjhb * we fail due to out of memory. 1514112117Sjhb */ 1515112117Sjhbstatic int 1516112117Sjhbinsertchild(struct witness *parent, struct witness *child) 1517112117Sjhb{ 1518112117Sjhb struct witness_child_list_entry **wcl; 1519112117Sjhb 152074912Sjhb MPASS(child != NULL && parent != NULL); 152174912Sjhb 152265557Sjasone /* 152365557Sjasone * Insert "child" after "parent" 152465557Sjasone */ 152574912Sjhb wcl = &parent->w_children; 152674912Sjhb while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 152774912Sjhb wcl = &(*wcl)->wcl_next; 152874912Sjhb if (*wcl == NULL) { 152974912Sjhb *wcl = witness_child_get(); 153074912Sjhb if (*wcl == NULL) 1531112117Sjhb return (0); 1532149441Struckman w_child_cnt++; 153365557Sjasone } 153474912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 153574912Sjhb 1536112117Sjhb return (1); 1537112117Sjhb} 1538112117Sjhb 1539112117Sjhb 1540112117Sjhbstatic int 1541112117Sjhbitismychild(struct witness *parent, struct witness *child) 1542112117Sjhb{ 1543112117Sjhb struct witness_list *list; 1544112117Sjhb 1545112117Sjhb MPASS(child != NULL && parent != NULL); 1546112117Sjhb if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1547112117Sjhb (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1548112117Sjhb panic( 1549112117Sjhb "%s: parent (%s) and child (%s) are not the same lock type", 1550112117Sjhb __func__, parent->w_class->lc_name, 1551112117Sjhb child->w_class->lc_name); 1552112117Sjhb 1553112117Sjhb if (!insertchild(parent, child)) 155465557Sjasone return (0); 1555112117Sjhb 155674912Sjhb if (parent->w_class->lc_flags & LC_SLEEPLOCK) 155774912Sjhb list = &w_sleep; 155874912Sjhb else 155974912Sjhb list = &w_spin; 1560149441Struckman return (1); 156165557Sjasone} 156265557Sjasone 156365557Sjasonestatic void 156465856Sjhbremovechild(struct witness *parent, struct witness *child) 156565557Sjasone{ 156674912Sjhb struct witness_child_list_entry **wcl, *wcl1; 156765557Sjasone int i; 156865557Sjasone 156974912Sjhb for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 157074912Sjhb for (i = 0; i < (*wcl)->wcl_count; i++) 157174912Sjhb if ((*wcl)->wcl_children[i] == child) 157265557Sjasone goto found; 157365557Sjasone return; 157465557Sjasonefound: 157574912Sjhb (*wcl)->wcl_count--; 157674912Sjhb if ((*wcl)->wcl_count > i) 157774912Sjhb (*wcl)->wcl_children[i] = 157874912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count]; 157974912Sjhb MPASS((*wcl)->wcl_children[i] != NULL); 158074912Sjhb if ((*wcl)->wcl_count != 0) 158165557Sjasone return; 158274912Sjhb wcl1 = *wcl; 158374912Sjhb *wcl = wcl1->wcl_next; 1584149441Struckman w_child_cnt--; 158574912Sjhb witness_child_free(wcl1); 158665557Sjasone} 158765557Sjasone 158865557Sjasonestatic int 158965856Sjhbisitmychild(struct witness *parent, struct witness *child) 159065557Sjasone{ 159174912Sjhb struct witness_child_list_entry *wcl; 159265557Sjasone int i; 159365557Sjasone 159474912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 159574912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 159674912Sjhb if (wcl->wcl_children[i] == child) 159765557Sjasone return (1); 159865557Sjasone } 159965557Sjasone } 160065557Sjasone return (0); 160165557Sjasone} 160265557Sjasone 160365557Sjasonestatic int 160465856Sjhbisitmydescendant(struct witness *parent, struct witness *child) 160565557Sjasone{ 160674912Sjhb struct witness_child_list_entry *wcl; 160774912Sjhb int i, j; 160865557Sjasone 160974912Sjhb if (isitmychild(parent, child)) 161074912Sjhb return (1); 161174912Sjhb j = 0; 161274912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 161367352Sjhb MPASS(j < 1000); 161474912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 161574912Sjhb if (isitmydescendant(wcl->wcl_children[i], child)) 161665557Sjasone return (1); 161765557Sjasone } 161874912Sjhb j++; 161965557Sjasone } 162065557Sjasone return (0); 162165557Sjasone} 162265557Sjasone 1623105508Sphk#ifdef BLESSING 162465557Sjasonestatic int 162565856Sjhbblessed(struct witness *w1, struct witness *w2) 162665557Sjasone{ 162765557Sjasone int i; 162865856Sjhb struct witness_blessed *b; 162965557Sjasone 163065557Sjasone for (i = 0; i < blessed_count; i++) { 163165557Sjasone b = &blessed_list[i]; 163274912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 163374912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 163465557Sjasone return (1); 163565557Sjasone continue; 163665557Sjasone } 163774912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 163874912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 163965557Sjasone return (1); 164065557Sjasone } 164165557Sjasone return (0); 164265557Sjasone} 1643105508Sphk#endif 164465557Sjasone 164565856Sjhbstatic struct witness * 164674912Sjhbwitness_get(void) 164765557Sjasone{ 164865856Sjhb struct witness *w; 164965557Sjasone 1650112562Sjhb if (witness_watch == 0) { 165176481Sjhb mtx_unlock_spin(&w_mtx); 165276481Sjhb return (NULL); 165376481Sjhb } 165474912Sjhb if (STAILQ_EMPTY(&w_free)) { 1655112562Sjhb witness_watch = 0; 165674912Sjhb mtx_unlock_spin(&w_mtx); 165774912Sjhb printf("%s: witness exhausted\n", __func__); 165865557Sjasone return (NULL); 165965557Sjasone } 166074912Sjhb w = STAILQ_FIRST(&w_free); 166174912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 1662149441Struckman w_free_cnt--; 166365856Sjhb bzero(w, sizeof(*w)); 166465557Sjasone return (w); 166565557Sjasone} 166665557Sjasone 166765557Sjasonestatic void 166865856Sjhbwitness_free(struct witness *w) 166965557Sjasone{ 167074912Sjhb 167174912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 1672149441Struckman w_free_cnt++; 167365557Sjasone} 167465557Sjasone 167574912Sjhbstatic struct witness_child_list_entry * 167674912Sjhbwitness_child_get(void) 167765557Sjasone{ 167874912Sjhb struct witness_child_list_entry *wcl; 167965557Sjasone 1680112562Sjhb if (witness_watch == 0) { 168176481Sjhb mtx_unlock_spin(&w_mtx); 168276481Sjhb return (NULL); 168376481Sjhb } 168474912Sjhb wcl = w_child_free; 168574912Sjhb if (wcl == NULL) { 1686112562Sjhb witness_watch = 0; 168774912Sjhb mtx_unlock_spin(&w_mtx); 168874912Sjhb printf("%s: witness exhausted\n", __func__); 168974912Sjhb return (NULL); 169065557Sjasone } 169174912Sjhb w_child_free = wcl->wcl_next; 1692149441Struckman w_child_free_cnt--; 169374912Sjhb bzero(wcl, sizeof(*wcl)); 169474912Sjhb return (wcl); 169574912Sjhb} 169669881Sjake 169774912Sjhbstatic void 169874912Sjhbwitness_child_free(struct witness_child_list_entry *wcl) 169974912Sjhb{ 170074912Sjhb 170174912Sjhb wcl->wcl_next = w_child_free; 170274912Sjhb w_child_free = wcl; 1703149441Struckman w_child_free_cnt++; 170465557Sjasone} 170565557Sjasone 170674912Sjhbstatic struct lock_list_entry * 170774912Sjhbwitness_lock_list_get(void) 170874912Sjhb{ 170974912Sjhb struct lock_list_entry *lle; 171071709Sjhb 1711112562Sjhb if (witness_watch == 0) 171276481Sjhb return (NULL); 171374912Sjhb mtx_lock_spin(&w_mtx); 171474912Sjhb lle = w_lock_list_free; 171574912Sjhb if (lle == NULL) { 1716112562Sjhb witness_watch = 0; 171774912Sjhb mtx_unlock_spin(&w_mtx); 171874912Sjhb printf("%s: witness exhausted\n", __func__); 171974912Sjhb return (NULL); 172074912Sjhb } 172174912Sjhb w_lock_list_free = lle->ll_next; 172274912Sjhb mtx_unlock_spin(&w_mtx); 172374912Sjhb bzero(lle, sizeof(*lle)); 172474912Sjhb return (lle); 172574912Sjhb} 172674912Sjhb 172774912Sjhbstatic void 172874912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 172971709Sjhb{ 173071709Sjhb 173174912Sjhb mtx_lock_spin(&w_mtx); 173274912Sjhb lle->ll_next = w_lock_list_free; 173374912Sjhb w_lock_list_free = lle; 173474912Sjhb mtx_unlock_spin(&w_mtx); 173571709Sjhb} 173671709Sjhb 173776272Sjhbstatic struct lock_instance * 173876272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 173976272Sjhb{ 174076272Sjhb struct lock_list_entry *lle; 174176272Sjhb struct lock_instance *instance; 174276272Sjhb int i; 174376272Sjhb 174476272Sjhb for (lle = lock_list; lle != NULL; lle = lle->ll_next) 174576272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 174676272Sjhb instance = &lle->ll_children[i]; 174776272Sjhb if (instance->li_lock == lock) 174876272Sjhb return (instance); 174976272Sjhb } 175076272Sjhb return (NULL); 175176272Sjhb} 175276272Sjhb 1753111881Sjhbstatic void 1754111881Sjhbwitness_list_lock(struct lock_instance *instance) 1755111881Sjhb{ 1756111881Sjhb struct lock_object *lock; 1757111881Sjhb 1758111881Sjhb lock = instance->li_lock; 1759111881Sjhb printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1760154077Sjhb "exclusive" : "shared", LOCK_CLASS(lock)->lc_name, lock->lo_name); 1761111881Sjhb if (lock->lo_type != lock->lo_name) 1762111881Sjhb printf(" (%s)", lock->lo_type); 1763111881Sjhb printf(" r = %d (%p) locked @ %s:%d\n", 1764111881Sjhb instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1765111881Sjhb instance->li_line); 1766111881Sjhb} 1767111881Sjhb 1768140637Srwatson#ifdef DDB 1769139333Srwatsonstatic int 1770139333Srwatsonwitness_thread_has_locks(struct thread *td) 1771139333Srwatson{ 1772139333Srwatson 1773139333Srwatson return (td->td_sleeplocks != NULL); 1774139333Srwatson} 1775139333Srwatson 1776139333Srwatsonstatic int 1777139333Srwatsonwitness_proc_has_locks(struct proc *p) 1778139333Srwatson{ 1779139333Srwatson struct thread *td; 1780139333Srwatson 1781139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 1782139333Srwatson if (witness_thread_has_locks(td)) 1783139333Srwatson return (1); 1784139333Srwatson } 1785139333Srwatson return (0); 1786139333Srwatson} 1787140637Srwatson#endif 1788139333Srwatson 178974912Sjhbint 179075273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 179172224Sjhb{ 179275273Sjhb struct lock_list_entry *lle; 179374912Sjhb int i, nheld; 179472224Sjhb 179574912Sjhb nheld = 0; 179674912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 179774912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 1798111881Sjhb witness_list_lock(&lle->ll_children[i]); 179974912Sjhb nheld++; 180074912Sjhb } 180175273Sjhb return (nheld); 180275273Sjhb} 180375273Sjhb 1804118271Sjhb/* 1805118271Sjhb * This is a bit risky at best. We call this function when we have timed 1806118271Sjhb * out acquiring a spin lock, and we assume that the other CPU is stuck 1807118271Sjhb * with this lock held. So, we go groveling around in the other CPU's 1808118271Sjhb * per-cpu data to try to find the lock instance for this spin lock to 1809118271Sjhb * see when it was last acquired. 1810118271Sjhb */ 181165557Sjasonevoid 1812118271Sjhbwitness_display_spinlock(struct lock_object *lock, struct thread *owner) 1813118271Sjhb{ 1814118271Sjhb struct lock_instance *instance; 1815118271Sjhb struct pcpu *pc; 1816118271Sjhb 1817118271Sjhb if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 1818118271Sjhb return; 1819118271Sjhb pc = pcpu_find(owner->td_oncpu); 1820118271Sjhb instance = find_instance(pc->pc_spinlocks, lock); 1821118271Sjhb if (instance != NULL) 1822118271Sjhb witness_list_lock(instance); 1823118271Sjhb} 1824118271Sjhb 1825118271Sjhbvoid 182674912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 182765557Sjasone{ 1828153854Sjhb struct lock_list_entry *lock_list; 182976272Sjhb struct lock_instance *instance; 1830154077Sjhb struct lock_class *class; 183171320Sjasone 183282284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1833112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 183471352Sjasone return; 1835154077Sjhb class = LOCK_CLASS(lock); 1836154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 1837153854Sjhb lock_list = curthread->td_sleeplocks; 1838153854Sjhb else { 1839153854Sjhb if (witness_skipspin) 1840153854Sjhb return; 1841153854Sjhb lock_list = PCPU_GET(spinlocks); 1842153854Sjhb } 1843153854Sjhb instance = find_instance(lock_list, lock); 184482243Sjhb if (instance == NULL) 184582243Sjhb panic("%s: lock (%s) %s not locked", __func__, 1846154077Sjhb class->lc_name, lock->lo_name); 184776272Sjhb *filep = instance->li_file; 184876272Sjhb *linep = instance->li_line; 184965557Sjasone} 185065557Sjasone 185165557Sjasonevoid 185274912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 185365557Sjasone{ 1854153854Sjhb struct lock_list_entry *lock_list; 185576272Sjhb struct lock_instance *instance; 1856154077Sjhb struct lock_class *class; 185771320Sjasone 185882284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1859112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 186071352Sjasone return; 1861154077Sjhb class = LOCK_CLASS(lock); 1862154077Sjhb if (class->lc_flags & LC_SLEEPLOCK) 1863153854Sjhb lock_list = curthread->td_sleeplocks; 1864153854Sjhb else { 1865153854Sjhb if (witness_skipspin) 1866153854Sjhb return; 1867153854Sjhb lock_list = PCPU_GET(spinlocks); 1868153854Sjhb } 1869153854Sjhb instance = find_instance(lock_list, lock); 187082243Sjhb if (instance == NULL) 187182243Sjhb panic("%s: lock (%s) %s not locked", __func__, 1872154077Sjhb class->lc_name, lock->lo_name); 187374912Sjhb lock->lo_witness->w_file = file; 187474912Sjhb lock->lo_witness->w_line = line; 187576272Sjhb instance->li_file = file; 187676272Sjhb instance->li_line = line; 187765557Sjasone} 187865557Sjasone 187978871Sjhbvoid 188078871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 188178871Sjhb{ 188278871Sjhb#ifdef INVARIANT_SUPPORT 188378871Sjhb struct lock_instance *instance; 1884154077Sjhb struct lock_class *class; 188578871Sjhb 1886112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 188778941Sjhb return; 1888154077Sjhb class = LOCK_CLASS(lock); 1889154077Sjhb if ((class->lc_flags & LC_SLEEPLOCK) != 0) 189083366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 1891154077Sjhb else if ((class->lc_flags & LC_SPINLOCK) != 0) 189278871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 189386422Sjhb else { 189478871Sjhb panic("Lock (%s) %s is not sleep or spin!", 1895154077Sjhb class->lc_name, lock->lo_name); 189686422Sjhb } 1897112116Sjhb file = fixup_filename(file); 189878871Sjhb switch (flags) { 189978871Sjhb case LA_UNLOCKED: 190078871Sjhb if (instance != NULL) 190178871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 1902154077Sjhb class->lc_name, lock->lo_name, file, line); 190378871Sjhb break; 190478871Sjhb case LA_LOCKED: 190578871Sjhb case LA_LOCKED | LA_RECURSED: 190678871Sjhb case LA_LOCKED | LA_NOTRECURSED: 190778871Sjhb case LA_SLOCKED: 190878871Sjhb case LA_SLOCKED | LA_RECURSED: 190978871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 191078871Sjhb case LA_XLOCKED: 191178871Sjhb case LA_XLOCKED | LA_RECURSED: 191278871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 191386422Sjhb if (instance == NULL) { 191478871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 1915154077Sjhb class->lc_name, lock->lo_name, file, line); 191686422Sjhb break; 191786422Sjhb } 191878871Sjhb if ((flags & LA_XLOCKED) != 0 && 191978871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 192078871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1921154077Sjhb class->lc_name, lock->lo_name, file, line); 192278871Sjhb if ((flags & LA_SLOCKED) != 0 && 192378871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 192478871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 1925154077Sjhb class->lc_name, lock->lo_name, file, line); 192678871Sjhb if ((flags & LA_RECURSED) != 0 && 192778871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 192878871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 1929154077Sjhb class->lc_name, lock->lo_name, file, line); 193078871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 193178871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 193278871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 1933154077Sjhb class->lc_name, lock->lo_name, file, line); 193478871Sjhb break; 193578871Sjhb default: 193678871Sjhb panic("Invalid lock assertion at %s:%d.", file, line); 193778871Sjhb 193878871Sjhb } 193978871Sjhb#endif /* INVARIANT_SUPPORT */ 194078871Sjhb} 194178871Sjhb 194274912Sjhb#ifdef DDB 1943112061Sjhbstatic void 1944112061Sjhbwitness_list(struct thread *td) 1945112061Sjhb{ 194674912Sjhb 1947112061Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1948131930Smarcel KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 1949112061Sjhb 1950112562Sjhb if (witness_watch == 0) 1951112061Sjhb return; 1952112061Sjhb 1953112061Sjhb witness_list_locks(&td->td_sleeplocks); 1954112061Sjhb 1955112061Sjhb /* 1956112061Sjhb * We only handle spinlocks if td == curthread. This is somewhat broken 1957112061Sjhb * if td is currently executing on some other CPU and holds spin locks 1958112061Sjhb * as we won't display those locks. If we had a MI way of getting 1959112061Sjhb * the per-cpu data for a given cpu then we could use 1960113339Sjulian * td->td_oncpu to get the list of spinlocks for this thread 1961112061Sjhb * and "fix" this. 1962112061Sjhb * 1963112061Sjhb * That still wouldn't really fix this unless we locked sched_lock 1964112061Sjhb * or stopped the other CPU to make sure it wasn't changing the list 1965112061Sjhb * out from under us. It is probably best to just not try to handle 1966112061Sjhb * threads on other CPU's for now. 1967112061Sjhb */ 1968112061Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 1969112061Sjhb witness_list_locks(PCPU_PTR(spinlocks)); 1970112061Sjhb} 1971112061Sjhb 197274930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 197374912Sjhb{ 197483366Sjulian struct thread *td; 197574912Sjhb 1976158030Sjhb if (have_addr) 1977158030Sjhb td = db_lookup_thread(addr, TRUE); 1978158030Sjhb else 1979158030Sjhb td = kdb_thread; 1980158030Sjhb witness_list(td); 198174912Sjhb} 198274912Sjhb 1983139333SrwatsonDB_SHOW_COMMAND(alllocks, db_witness_list_all) 1984139333Srwatson{ 1985139333Srwatson struct thread *td; 1986139333Srwatson struct proc *p; 1987139333Srwatson 1988139333Srwatson /* 1989139333Srwatson * It would be nice to list only threads and processes that actually 1990139333Srwatson * held sleep locks, but that information is currently not exported 1991139333Srwatson * by WITNESS. 1992139333Srwatson */ 1993139333Srwatson FOREACH_PROC_IN_SYSTEM(p) { 1994139333Srwatson if (!witness_proc_has_locks(p)) 1995139333Srwatson continue; 1996139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 1997139333Srwatson if (!witness_thread_has_locks(td)) 1998139333Srwatson continue; 1999153853Sjhb db_printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 2000139348Srwatson p->p_comm, td, td->td_tid); 2001139333Srwatson witness_list(td); 2002139333Srwatson } 2003139333Srwatson } 2004139333Srwatson} 2005139333Srwatson 200674912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 200774912Sjhb{ 200874912Sjhb 200974912Sjhb witness_display(db_printf); 201074912Sjhb} 201174912Sjhb#endif 2012