subr_witness.c revision 149738
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 149738 2005-09-02 20:23:49Z jhb $"); 86116182Sobrien 8768790Sjhb#include "opt_ddb.h" 8867676Sjhb#include "opt_witness.h" 8967676Sjhb 9065557Sjasone#include <sys/param.h> 9167352Sjhb#include <sys/bus.h> 92131930Smarcel#include <sys/kdb.h> 9367352Sjhb#include <sys/kernel.h> 9474912Sjhb#include <sys/ktr.h> 9574912Sjhb#include <sys/lock.h> 9667352Sjhb#include <sys/malloc.h> 9774912Sjhb#include <sys/mutex.h> 9865557Sjasone#include <sys/proc.h> 9967676Sjhb#include <sys/sysctl.h> 10065557Sjasone#include <sys/systm.h> 10165557Sjasone 10268790Sjhb#include <ddb/ddb.h> 10368790Sjhb 104111881Sjhb#include <machine/stdarg.h> 105111881Sjhb 106105508Sphk/* Define this to check for blessed mutexes */ 107105508Sphk#undef BLESSING 108105508Sphk 109139378Sjhb#define WITNESS_COUNT 1024 11074912Sjhb#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 11165557Sjasone/* 11283798Sjhb * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 11374912Sjhb * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 11474912Sjhb * probably be safe for the most part, but it's still a SWAG. 11567352Sjhb */ 11674912Sjhb#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 11771352Sjasone 11874912Sjhb#define WITNESS_NCHILDREN 6 11971352Sjasone 12074912Sjhbstruct witness_child_list_entry; 12171352Sjasone 12274912Sjhbstruct witness { 12374912Sjhb const char *w_name; 12474912Sjhb struct lock_class *w_class; 12574912Sjhb STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 12674912Sjhb STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 12774912Sjhb struct witness_child_list_entry *w_children; /* Great evilness... */ 12874912Sjhb const char *w_file; 12974912Sjhb int w_line; 13074912Sjhb u_int w_level; 13174912Sjhb u_int w_refcount; 13274912Sjhb u_char w_Giant_squawked:1; 13374912Sjhb u_char w_other_squawked:1; 13474912Sjhb u_char w_same_squawked:1; 135112118Sjhb u_char w_displayed:1; 13674912Sjhb}; 13771352Sjasone 13874912Sjhbstruct witness_child_list_entry { 13974912Sjhb struct witness_child_list_entry *wcl_next; 14074912Sjhb struct witness *wcl_children[WITNESS_NCHILDREN]; 14174912Sjhb u_int wcl_count; 14274912Sjhb}; 14371352Sjasone 14474912SjhbSTAILQ_HEAD(witness_list, witness); 14571352Sjasone 146105508Sphk#ifdef BLESSING 14774912Sjhbstruct witness_blessed { 14874912Sjhb const char *b_lock1; 14974912Sjhb const char *b_lock2; 15074912Sjhb}; 151105508Sphk#endif 15271352Sjasone 15374912Sjhbstruct witness_order_list_entry { 15474912Sjhb const char *w_name; 15574912Sjhb struct lock_class *w_class; 15674912Sjhb}; 15771352Sjasone 158112117Sjhb#ifdef BLESSING 159112117Sjhbstatic int blessed(struct witness *, struct witness *); 160112117Sjhb#endif 161112117Sjhbstatic int depart(struct witness *w); 16274912Sjhbstatic struct witness *enroll(const char *description, 16374912Sjhb struct lock_class *lock_class); 164112117Sjhbstatic int insertchild(struct witness *parent, struct witness *child); 165112117Sjhbstatic int isitmychild(struct witness *parent, struct witness *child); 166112117Sjhbstatic int isitmydescendant(struct witness *parent, struct witness *child); 16774912Sjhbstatic int itismychild(struct witness *parent, struct witness *child); 16874912Sjhbstatic void removechild(struct witness *parent, struct witness *child); 169112562Sjhbstatic int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 17074912Sjhbstatic void witness_displaydescendants(void(*)(const char *fmt, ...), 171112118Sjhb struct witness *, int indent); 172112116Sjhbstatic const char *fixup_filename(const char *file); 17374912Sjhbstatic void witness_leveldescendents(struct witness *parent, int level); 17474912Sjhbstatic void witness_levelall(void); 17574912Sjhbstatic struct witness *witness_get(void); 17674912Sjhbstatic void witness_free(struct witness *m); 17774912Sjhbstatic struct witness_child_list_entry *witness_child_get(void); 17874912Sjhbstatic void witness_child_free(struct witness_child_list_entry *wcl); 17974912Sjhbstatic struct lock_list_entry *witness_lock_list_get(void); 18074912Sjhbstatic void witness_lock_list_free(struct lock_list_entry *lle); 18176272Sjhbstatic struct lock_instance *find_instance(struct lock_list_entry *lock_list, 18276272Sjhb struct lock_object *lock); 183111881Sjhbstatic void witness_list_lock(struct lock_instance *instance); 184112115Sjhb#ifdef DDB 185112061Sjhbstatic void witness_list(struct thread *td); 186100011Smpstatic void witness_display_list(void(*prnt)(const char *fmt, ...), 187100011Smp struct witness_list *list); 188100011Smpstatic void witness_display(void(*)(const char *fmt, ...)); 189100011Smp#endif 19072200Sbmilekic 191134873SjmgSYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking"); 19272200Sbmilekic 193112562Sjhb/* 194149441Struckman * If set to 0, witness is disabled. If set to a non-zero value, witness 195149441Struckman * performs full lock order checking for all locks. At runtime, this 196112562Sjhb * value may be set to 0 to turn off witness. witness is not allowed be 197112562Sjhb * turned on once it is turned off, however. 198112562Sjhb */ 19977843Speterstatic int witness_watch = 1; 200134873SjmgTUNABLE_INT("debug.witness.watch", &witness_watch); 201134873SjmgSYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 202112562Sjhb sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 20371352Sjasone 204131930Smarcel#ifdef KDB 20572200Sbmilekic/* 206131930Smarcel * When KDB is enabled and witness_kdb is set to 1, it will cause the system 207131930Smarcel * to drop into kdebug() when: 20865557Sjasone * - a lock heirarchy violation occurs 20965557Sjasone * - locks are held when going to sleep. 21065557Sjasone */ 211131930Smarcel#ifdef WITNESS_KDB 212131930Smarcelint witness_kdb = 1; 21367676Sjhb#else 214131930Smarcelint witness_kdb = 0; 21565557Sjasone#endif 216134873SjmgTUNABLE_INT("debug.witness.kdb", &witness_kdb); 217134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 218110779Speter 219110779Speter/* 220131930Smarcel * When KDB is enabled and witness_trace is set to 1, it will cause the system 221110779Speter * to print a stack trace: 222110779Speter * - a lock heirarchy violation occurs 223110779Speter * - locks are held when going to sleep. 224110779Speter */ 225110779Speterint witness_trace = 1; 226134873SjmgTUNABLE_INT("debug.witness.trace", &witness_trace); 227134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 228131930Smarcel#endif /* KDB */ 22965557Sjasone 23067676Sjhb#ifdef WITNESS_SKIPSPIN 23177843Speterint witness_skipspin = 1; 23267676Sjhb#else 23377843Speterint witness_skipspin = 0; 23465557Sjasone#endif 235134873SjmgTUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 236134873SjmgSYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, 237134873Sjmg &witness_skipspin, 0, ""); 23865557Sjasone 23974912Sjhbstatic struct mtx w_mtx; 24074912Sjhbstatic struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 24174912Sjhbstatic struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 24274912Sjhbstatic struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 24374912Sjhbstatic struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 24474912Sjhbstatic struct witness_child_list_entry *w_child_free = NULL; 24574912Sjhbstatic struct lock_list_entry *w_lock_list_free = NULL; 24665557Sjasone 247149441Struckmanstatic int w_free_cnt, w_spin_cnt, w_sleep_cnt, w_child_free_cnt, w_child_cnt; 248149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, free_cnt, CTLFLAG_RD, &w_free_cnt, 0, ""); 249149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, spin_cnt, CTLFLAG_RD, &w_spin_cnt, 0, ""); 250149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, sleep_cnt, CTLFLAG_RD, &w_sleep_cnt, 0, 251149441Struckman ""); 252149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, child_free_cnt, CTLFLAG_RD, 253149441Struckman &w_child_free_cnt, 0, ""); 254149441StruckmanSYSCTL_INT(_debug_witness, OID_AUTO, child_cnt, CTLFLAG_RD, &w_child_cnt, 0, 255149441Struckman ""); 256149441Struckman 25774912Sjhbstatic struct witness w_data[WITNESS_COUNT]; 25874912Sjhbstatic struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 25974912Sjhbstatic struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 26065557Sjasone 26174912Sjhbstatic struct witness_order_list_entry order_lists[] = { 262149738Sjhb /* 263149738Sjhb * sx locks 264149738Sjhb */ 26574912Sjhb { "proctree", &lock_class_sx }, 26674912Sjhb { "allproc", &lock_class_sx }, 267149738Sjhb { NULL, NULL }, 268149738Sjhb /* 269149738Sjhb * Various mutexes 270149738Sjhb */ 271111951Sjhb { "Giant", &lock_class_mtx_sleep }, 272108184Skris { "filedesc structure", &lock_class_mtx_sleep }, 273108184Skris { "pipe mutex", &lock_class_mtx_sleep }, 27496122Salfred { "sigio lock", &lock_class_mtx_sleep }, 27591140Stanimura { "process group", &lock_class_mtx_sleep }, 27674912Sjhb { "process lock", &lock_class_mtx_sleep }, 27791140Stanimura { "session", &lock_class_mtx_sleep }, 27874912Sjhb { "uidinfo hash", &lock_class_mtx_sleep }, 27974912Sjhb { "uidinfo struct", &lock_class_mtx_sleep }, 280113275Smike { "allprison", &lock_class_mtx_sleep }, 28174912Sjhb { NULL, NULL }, 28275464Sjhb /* 283130022Srwatson * Sockets 284130022Srwatson */ 285130022Srwatson { "filedesc structure", &lock_class_mtx_sleep }, 286130022Srwatson { "accept", &lock_class_mtx_sleep }, 287130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 288130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 289130022Srwatson { "sellck", &lock_class_mtx_sleep }, 290130022Srwatson { NULL, NULL }, 291130022Srwatson /* 292130022Srwatson * Routing 293130022Srwatson */ 294130396Srwatson { "so_rcv", &lock_class_mtx_sleep }, 295130022Srwatson { "radix node head", &lock_class_mtx_sleep }, 296130022Srwatson { "rtentry", &lock_class_mtx_sleep }, 297130022Srwatson { "ifaddr", &lock_class_mtx_sleep }, 298130022Srwatson { NULL, NULL }, 299130022Srwatson /* 300148896Srwatson * Multicast - protocol locks before interface locks, after UDP locks. 301148682Srwatson */ 302148896Srwatson { "udpinp", &lock_class_mtx_sleep }, 303148682Srwatson { "in_multi_mtx", &lock_class_mtx_sleep }, 304148682Srwatson { "igmp_mtx", &lock_class_mtx_sleep }, 305148682Srwatson { "if_addr_mtx", &lock_class_mtx_sleep }, 306148682Srwatson { NULL, NULL }, 307148682Srwatson /* 308130022Srwatson * UNIX Domain Sockets 309130396Srwatson */ 310130396Srwatson { "unp", &lock_class_mtx_sleep }, 311130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 312130031Sjhb { NULL, NULL }, 313130022Srwatson /* 314130022Srwatson * UDP/IP 315130022Srwatson */ 316130022Srwatson { "udp", &lock_class_mtx_sleep }, 317130022Srwatson { "udpinp", &lock_class_mtx_sleep }, 318130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 319130022Srwatson { NULL, NULL }, 320130022Srwatson /* 321130022Srwatson * TCP/IP 322130022Srwatson */ 323130022Srwatson { "tcp", &lock_class_mtx_sleep }, 324130022Srwatson { "tcpinp", &lock_class_mtx_sleep }, 325130396Srwatson { "so_snd", &lock_class_mtx_sleep }, 326130022Srwatson { NULL, NULL }, 327130022Srwatson /* 328130022Srwatson * SLIP 329130022Srwatson */ 330130022Srwatson { "slip_mtx", &lock_class_mtx_sleep }, 331130022Srwatson { "slip sc_mtx", &lock_class_mtx_sleep }, 332130031Sjhb { NULL, NULL }, 333130022Srwatson /* 334132639Srwatson * netatalk 335132639Srwatson */ 336132639Srwatson { "ddp_list_mtx", &lock_class_mtx_sleep }, 337132639Srwatson { "ddp_mtx", &lock_class_mtx_sleep }, 338132639Srwatson { NULL, NULL }, 339132639Srwatson /* 340134971Srwatson * BPF 341134971Srwatson */ 342134971Srwatson { "bpf global lock", &lock_class_mtx_sleep }, 343134971Srwatson { "bpf interface lock", &lock_class_mtx_sleep }, 344134971Srwatson { "bpf cdev lock", &lock_class_mtx_sleep }, 345144832Spjd { NULL, NULL }, 346143335Srwatson /* 347143335Srwatson * NFS server 348143335Srwatson */ 349143335Srwatson { "nfsd_mtx", &lock_class_mtx_sleep }, 350143335Srwatson { "so_snd", &lock_class_mtx_sleep }, 351134971Srwatson { NULL, NULL }, 352134971Srwatson /* 353144836Spjd * CDEV 354144836Spjd */ 355145425Sjeff { "system map", &lock_class_mtx_sleep }, 356145425Sjeff { "vm page queue mutex", &lock_class_mtx_sleep }, 357145425Sjeff { "vnode interlock", &lock_class_mtx_sleep }, 358144836Spjd { "cdev", &lock_class_mtx_sleep }, 359144836Spjd { NULL, NULL }, 360144836Spjd /* 36175464Sjhb * spin locks 36275464Sjhb */ 36384331Sjhb#ifdef SMP 36484331Sjhb { "ap boot", &lock_class_mtx_spin }, 36572224Sjhb#endif 36674912Sjhb { "sio", &lock_class_mtx_spin }, 36772224Sjhb#ifdef __i386__ 36874912Sjhb { "cy", &lock_class_mtx_spin }, 36972224Sjhb#endif 370124972Sru { "uart_hwmtx", &lock_class_mtx_spin }, 371103091Sjake { "sabtty", &lock_class_mtx_spin }, 372109015Sjake { "zstty", &lock_class_mtx_spin }, 37374912Sjhb { "ng_node", &lock_class_mtx_spin }, 37474912Sjhb { "ng_worklist", &lock_class_mtx_spin }, 375119813Ssam { "taskqueue_fast", &lock_class_mtx_spin }, 376122001Sjhb { "intr table", &lock_class_mtx_spin }, 37774912Sjhb { "ithread table lock", &lock_class_mtx_spin }, 378126324Sjhb { "sleepq chain", &lock_class_mtx_spin }, 37974912Sjhb { "sched lock", &lock_class_mtx_spin }, 380122514Sjhb { "turnstile chain", &lock_class_mtx_spin }, 381122514Sjhb { "td_contested", &lock_class_mtx_spin }, 38274912Sjhb { "callout", &lock_class_mtx_spin }, 383136374Srwatson { "entropy harvest mutex", &lock_class_mtx_spin }, 38465557Sjasone /* 38565557Sjasone * leaf locks 38665557Sjasone */ 38790278Sjhb { "allpmaps", &lock_class_mtx_spin }, 38899416Salc { "vm page queue free mutex", &lock_class_mtx_spin }, 38988322Sjhb { "icu", &lock_class_mtx_spin }, 39072224Sjhb#ifdef SMP 39174912Sjhb { "smp rendezvous", &lock_class_mtx_spin }, 392122849Speter#if defined(__i386__) || defined(__amd64__) 39399862Speter { "tlb", &lock_class_mtx_spin }, 394112993Speter#endif 395108187Sjake#ifdef __sparc64__ 396108187Sjake { "ipi", &lock_class_mtx_spin }, 397146982Smarius { "rtc_mtx", &lock_class_mtx_spin }, 39899862Speter#endif 399108187Sjake#endif 40078785Sjhb { "clk", &lock_class_mtx_spin }, 40195473Sdes { "mutex profiling lock", &lock_class_mtx_spin }, 402111028Sjeff { "kse zombie lock", &lock_class_mtx_spin }, 403103786Sjeff { "ALD Queue", &lock_class_mtx_spin }, 404104951Speter#ifdef __ia64__ 405104951Speter { "MCA spin lock", &lock_class_mtx_spin }, 406104951Speter#endif 407115425Speter#if defined(__i386__) || defined(__amd64__) 408111068Speter { "pcicfg", &lock_class_mtx_spin }, 409143204Swpaul { "NDIS thread lock", &lock_class_mtx_spin }, 410111068Speter#endif 411144966Svkashyap { "tw_osl_io_lock", &lock_class_mtx_spin }, 412144966Svkashyap { "tw_osl_q_lock", &lock_class_mtx_spin }, 413144966Svkashyap { "tw_cl_io_lock", &lock_class_mtx_spin }, 414144966Svkashyap { "tw_cl_intr_lock", &lock_class_mtx_spin }, 415144966Svkashyap { "tw_cl_gen_lock", &lock_class_mtx_spin }, 41674912Sjhb { NULL, NULL }, 41774912Sjhb { NULL, NULL } 41865557Sjasone}; 41965557Sjasone 420105508Sphk#ifdef BLESSING 42165557Sjasone/* 42265557Sjasone * Pairs of locks which have been blessed 42365557Sjasone * Don't complain about order problems with blessed locks 42465557Sjasone */ 42565856Sjhbstatic struct witness_blessed blessed_list[] = { 42665557Sjasone}; 42772200Sbmilekicstatic int blessed_count = 42872200Sbmilekic sizeof(blessed_list) / sizeof(struct witness_blessed); 429105508Sphk#endif 43065557Sjasone 43174912Sjhb/* 43274912Sjhb * List of all locks in the system. 43374912Sjhb */ 43497963SjhbTAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 43574912Sjhb 43674912Sjhbstatic struct mtx all_mtx = { 43774912Sjhb { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 43874912Sjhb "All locks list", /* mtx_object.lo_name */ 43993811Sjhb "All locks list", /* mtx_object.lo_type */ 44074912Sjhb LO_INITIALIZED, /* mtx_object.lo_flags */ 44197963Sjhb { NULL, NULL }, /* mtx_object.lo_list */ 44274912Sjhb NULL }, /* mtx_object.lo_witness */ 443122514Sjhb MTX_UNOWNED, 0 /* mtx_lock, mtx_recurse */ 44474912Sjhb}; 44574912Sjhb 44674912Sjhb/* 44774912Sjhb * This global is set to 0 once it becomes safe to use the witness code. 44874912Sjhb */ 44974912Sjhbstatic int witness_cold = 1; 45074912Sjhb 45174912Sjhb/* 45274912Sjhb * Global variables for book keeping. 45374912Sjhb */ 45474912Sjhbstatic int lock_cur_cnt; 45574912Sjhbstatic int lock_max_cnt; 45674912Sjhb 45774912Sjhb/* 45874912Sjhb * The WITNESS-enabled diagnostic code. 45974912Sjhb */ 46071352Sjasonestatic void 46174912Sjhbwitness_initialize(void *dummy __unused) 46265557Sjasone{ 46374912Sjhb struct lock_object *lock; 46474912Sjhb struct witness_order_list_entry *order; 46574912Sjhb struct witness *w, *w1; 46674912Sjhb int i; 46765557Sjasone 46874912Sjhb /* 46974912Sjhb * We have to release Giant before initializing its witness 47074912Sjhb * structure so that WITNESS doesn't get confused. 47174912Sjhb */ 47274912Sjhb mtx_unlock(&Giant); 47374912Sjhb mtx_assert(&Giant, MA_NOTOWNED); 47474912Sjhb 47587593Sobrien CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 47697963Sjhb TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 47793811Sjhb mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 47893811Sjhb MTX_NOWITNESS); 47974912Sjhb for (i = 0; i < WITNESS_COUNT; i++) 48074912Sjhb witness_free(&w_data[i]); 48174912Sjhb for (i = 0; i < WITNESS_CHILDCOUNT; i++) 48274912Sjhb witness_child_free(&w_childdata[i]); 48374912Sjhb for (i = 0; i < LOCK_CHILDCOUNT; i++) 48474912Sjhb witness_lock_list_free(&w_locklistdata[i]); 48574912Sjhb 48674912Sjhb /* First add in all the specified order lists. */ 48774912Sjhb for (order = order_lists; order->w_name != NULL; order++) { 48874912Sjhb w = enroll(order->w_name, order->w_class); 48975569Sjhb if (w == NULL) 49075569Sjhb continue; 49174912Sjhb w->w_file = "order list"; 49274912Sjhb for (order++; order->w_name != NULL; order++) { 49374912Sjhb w1 = enroll(order->w_name, order->w_class); 49475569Sjhb if (w1 == NULL) 49575569Sjhb continue; 49674912Sjhb w1->w_file = "order list"; 497112117Sjhb if (!itismychild(w, w1)) 498112117Sjhb panic("Not enough memory for static orders!"); 49974912Sjhb w = w1; 50065557Sjasone } 50165557Sjasone } 50265557Sjasone 50374912Sjhb /* Iterate through all locks and add them to witness. */ 50474912Sjhb mtx_lock(&all_mtx); 50597963Sjhb TAILQ_FOREACH(lock, &all_locks, lo_list) { 50674912Sjhb if (lock->lo_flags & LO_WITNESS) 50793811Sjhb lock->lo_witness = enroll(lock->lo_type, 50874912Sjhb lock->lo_class); 50974912Sjhb else 51074912Sjhb lock->lo_witness = NULL; 51174912Sjhb } 51274912Sjhb mtx_unlock(&all_mtx); 51374912Sjhb 51474912Sjhb /* Mark the witness code as being ready for use. */ 51574912Sjhb atomic_store_rel_int(&witness_cold, 0); 51674912Sjhb 51774912Sjhb mtx_lock(&Giant); 51865557Sjasone} 51974912SjhbSYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 52065557Sjasone 521112562Sjhbstatic int 522112562Sjhbsysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 523112562Sjhb{ 524112562Sjhb int error, value; 525112562Sjhb 526112562Sjhb value = witness_watch; 527112562Sjhb error = sysctl_handle_int(oidp, &value, 0, req); 528112562Sjhb if (error != 0 || req->newptr == NULL) 529112562Sjhb return (error); 530112562Sjhb error = suser(req->td); 531112562Sjhb if (error != 0) 532112562Sjhb return (error); 533112562Sjhb if (value == witness_watch) 534112562Sjhb return (0); 535112562Sjhb if (value != 0) 536112562Sjhb return (EINVAL); 537112562Sjhb witness_watch = 0; 538112562Sjhb return (0); 539112562Sjhb} 540112562Sjhb 54174912Sjhbvoid 54274912Sjhbwitness_init(struct lock_object *lock) 54374912Sjhb{ 54474912Sjhb struct lock_class *class; 54574912Sjhb 54674912Sjhb class = lock->lo_class; 54774912Sjhb if (lock->lo_flags & LO_INITIALIZED) 54882284Sjhb panic("%s: lock (%s) %s is already initialized", __func__, 54974912Sjhb class->lc_name, lock->lo_name); 55074912Sjhb if ((lock->lo_flags & LO_RECURSABLE) != 0 && 55174912Sjhb (class->lc_flags & LC_RECURSABLE) == 0) 55282284Sjhb panic("%s: lock (%s) %s can not be recursable", __func__, 55374912Sjhb class->lc_name, lock->lo_name); 55474912Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 55574912Sjhb (class->lc_flags & LC_SLEEPABLE) == 0) 55682284Sjhb panic("%s: lock (%s) %s can not be sleepable", __func__, 55774912Sjhb class->lc_name, lock->lo_name); 55882244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 55982244Sjhb (class->lc_flags & LC_UPGRADABLE) == 0) 56082284Sjhb panic("%s: lock (%s) %s can not be upgradable", __func__, 56182244Sjhb class->lc_name, lock->lo_name); 56282244Sjhb 56374912Sjhb mtx_lock(&all_mtx); 56497963Sjhb TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 56574912Sjhb lock->lo_flags |= LO_INITIALIZED; 56674912Sjhb lock_cur_cnt++; 56774912Sjhb if (lock_cur_cnt > lock_max_cnt) 56874912Sjhb lock_max_cnt = lock_cur_cnt; 56974912Sjhb mtx_unlock(&all_mtx); 570112562Sjhb if (!witness_cold && witness_watch != 0 && panicstr == NULL && 57174912Sjhb (lock->lo_flags & LO_WITNESS) != 0) 57293811Sjhb lock->lo_witness = enroll(lock->lo_type, class); 57374912Sjhb else 57474912Sjhb lock->lo_witness = NULL; 57574912Sjhb} 57674912Sjhb 57774912Sjhbvoid 57874912Sjhbwitness_destroy(struct lock_object *lock) 57974912Sjhb{ 58075362Sjhb struct witness *w; 58174912Sjhb 58274912Sjhb if (witness_cold) 58374912Sjhb panic("lock (%s) %s destroyed while witness_cold", 58474912Sjhb lock->lo_class->lc_name, lock->lo_name); 58574912Sjhb if ((lock->lo_flags & LO_INITIALIZED) == 0) 58682284Sjhb panic("%s: lock (%s) %s is not initialized", __func__, 58774912Sjhb lock->lo_class->lc_name, lock->lo_name); 58874912Sjhb 58976272Sjhb /* XXX: need to verify that no one holds the lock */ 59075362Sjhb w = lock->lo_witness; 59175362Sjhb if (w != NULL) { 59275362Sjhb mtx_lock_spin(&w_mtx); 59397948Sjhb MPASS(w->w_refcount > 0); 59475362Sjhb w->w_refcount--; 595112117Sjhb 596112117Sjhb /* 597112117Sjhb * Lock is already released if we have an allocation failure 598112117Sjhb * and depart() fails. 599112117Sjhb */ 600112117Sjhb if (w->w_refcount != 0 || depart(w)) 601112117Sjhb mtx_unlock_spin(&w_mtx); 60275362Sjhb } 60375362Sjhb 60474912Sjhb mtx_lock(&all_mtx); 60574912Sjhb lock_cur_cnt--; 60697963Sjhb TAILQ_REMOVE(&all_locks, lock, lo_list); 60780055Sjhb lock->lo_flags &= ~LO_INITIALIZED; 60874912Sjhb mtx_unlock(&all_mtx); 60974912Sjhb} 61074912Sjhb 611112115Sjhb#ifdef DDB 61271352Sjasonestatic void 61374912Sjhbwitness_display_list(void(*prnt)(const char *fmt, ...), 61474912Sjhb struct witness_list *list) 61571352Sjasone{ 616112118Sjhb struct witness *w; 61771352Sjasone 61874912Sjhb STAILQ_FOREACH(w, list, w_typelist) { 619112118Sjhb if (w->w_file == NULL || w->w_level > 0) 62071352Sjasone continue; 62171352Sjasone /* 62271352Sjasone * This lock has no anscestors, display its descendants. 62371352Sjasone */ 624112118Sjhb witness_displaydescendants(prnt, w, 0); 62571352Sjasone } 62674912Sjhb} 62772224Sjhb 62874912Sjhbstatic void 62974912Sjhbwitness_display(void(*prnt)(const char *fmt, ...)) 63074912Sjhb{ 63174912Sjhb struct witness *w; 63274912Sjhb 63382284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 63474912Sjhb witness_levelall(); 63574912Sjhb 636112118Sjhb /* Clear all the displayed flags. */ 637112118Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 638112118Sjhb w->w_displayed = 0; 639112118Sjhb } 640112118Sjhb 64172224Sjhb /* 64274930Sjhb * First, handle sleep locks which have been acquired at least 64374912Sjhb * once. 64474912Sjhb */ 64574912Sjhb prnt("Sleep locks:\n"); 64674912Sjhb witness_display_list(prnt, &w_sleep); 64774912Sjhb 64874912Sjhb /* 64974930Sjhb * Now do spin locks which have been acquired at least once. 65072224Sjhb */ 65174912Sjhb prnt("\nSpin locks:\n"); 65274912Sjhb witness_display_list(prnt, &w_spin); 65372224Sjhb 65472224Sjhb /* 65574930Sjhb * Finally, any locks which have not been acquired yet. 65672224Sjhb */ 65774912Sjhb prnt("\nLocks which were never acquired:\n"); 65874912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 65997948Sjhb if (w->w_file != NULL || w->w_refcount == 0) 66071352Sjasone continue; 66174912Sjhb prnt("%s\n", w->w_name); 66271352Sjasone } 66371352Sjasone} 664112115Sjhb#endif /* DDB */ 66571352Sjasone 666112116Sjhb/* Trim useless garbage from filenames. */ 667112116Sjhbstatic const char * 668112116Sjhbfixup_filename(const char *file) 669112116Sjhb{ 670112116Sjhb 671112116Sjhb if (file == NULL) 672112116Sjhb return (NULL); 673112116Sjhb while (strncmp(file, "../", 3) == 0) 674112116Sjhb file += 3; 675112116Sjhb return (file); 676112116Sjhb} 677112116Sjhb 678125160Sjhbint 679125160Sjhbwitness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 680125160Sjhb{ 681125160Sjhb 682125160Sjhb if (witness_watch == 0 || panicstr != NULL) 683125160Sjhb return (0); 684125160Sjhb 685125160Sjhb /* Require locks that witness knows about. */ 686125160Sjhb if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 687125160Sjhb lock2->lo_witness == NULL) 688125160Sjhb return (EINVAL); 689125160Sjhb 690125160Sjhb MPASS(!mtx_owned(&w_mtx)); 691125160Sjhb mtx_lock_spin(&w_mtx); 692125160Sjhb 693125160Sjhb /* 694125160Sjhb * If we already have either an explicit or implied lock order that 695125160Sjhb * is the other way around, then return an error. 696125160Sjhb */ 697125160Sjhb if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 698125160Sjhb mtx_unlock_spin(&w_mtx); 699125160Sjhb return (EDOOFUS); 700125160Sjhb } 701125160Sjhb 702125160Sjhb /* Try to add the new order. */ 703125160Sjhb CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 704125160Sjhb lock2->lo_type, lock1->lo_type); 705125160Sjhb if (!itismychild(lock1->lo_witness, lock2->lo_witness)) 706125160Sjhb return (ENOMEM); 707125160Sjhb mtx_unlock_spin(&w_mtx); 708125160Sjhb return (0); 709125160Sjhb} 710125160Sjhb 71165557Sjasonevoid 712125160Sjhbwitness_checkorder(struct lock_object *lock, int flags, const char *file, 713125160Sjhb int line) 71465557Sjasone{ 71574912Sjhb struct lock_list_entry **lock_list, *lle; 71676272Sjhb struct lock_instance *lock1, *lock2; 71774912Sjhb struct lock_class *class; 71865856Sjhb struct witness *w, *w1; 71983366Sjulian struct thread *td; 72074912Sjhb int i, j; 72165557Sjasone 722112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 72380747Sjhb panicstr != NULL) 72471320Sjasone return; 725125160Sjhb 726125160Sjhb /* 727125160Sjhb * Try locks do not block if they fail to acquire the lock, thus 728125160Sjhb * there is no danger of deadlocks or of switching while holding a 729125160Sjhb * spin lock if we acquire a lock via a try operation. This 730125160Sjhb * function shouldn't even be called for try locks, so panic if 731125160Sjhb * that happens. 732125160Sjhb */ 733125160Sjhb if (flags & LOP_TRYLOCK) 734125160Sjhb panic("%s should not be called for try lock operations", 735125160Sjhb __func__); 736125160Sjhb 73774912Sjhb w = lock->lo_witness; 73874912Sjhb class = lock->lo_class; 73983366Sjulian td = curthread; 740112116Sjhb file = fixup_filename(file); 74165557Sjasone 74274912Sjhb if (class->lc_flags & LC_SLEEPLOCK) { 74393676Sjhb /* 74493676Sjhb * Since spin locks include a critical section, this check 745131884Sjhb * implicitly enforces a lock order of all sleep locks before 74693676Sjhb * all spin locks. 74793676Sjhb */ 748136304Sgreen if (td->td_critnest != 0 && !kdb_active) 74974912Sjhb panic("blockable sleep lock (%s) %s @ %s:%d", 75074912Sjhb class->lc_name, lock->lo_name, file, line); 751131884Sjhb 752131884Sjhb /* 753131884Sjhb * If this is the first lock acquired then just return as 754131884Sjhb * no order checking is needed. 755131884Sjhb */ 756131884Sjhb if (td->td_sleeplocks == NULL) 757131884Sjhb return; 75883366Sjulian lock_list = &td->td_sleeplocks; 759131884Sjhb } else { 760131884Sjhb /* 761131884Sjhb * If this is the first lock, just return as no order 762131884Sjhb * checking is needed. We check this in both if clauses 763131884Sjhb * here as unifying the check would require us to use a 764131884Sjhb * critical section to ensure we don't migrate while doing 765131884Sjhb * the check. Note that if this is not the first lock, we 766131884Sjhb * are already in a critical section and are safe for the 767131884Sjhb * rest of the check. 768131884Sjhb */ 769131884Sjhb if (PCPU_GET(spinlocks) == NULL) 770131884Sjhb return; 77188899Sjhb lock_list = PCPU_PTR(spinlocks); 772131884Sjhb } 77365557Sjasone 77476772Sjhb /* 775125160Sjhb * Check to see if we are recursing on a lock we already own. If 776125160Sjhb * so, make sure that we don't mismatch exclusive and shared lock 777125160Sjhb * acquires. 77876272Sjhb */ 77976272Sjhb lock1 = find_instance(*lock_list, lock); 78076272Sjhb if (lock1 != NULL) { 78176272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 78276272Sjhb (flags & LOP_EXCLUSIVE) == 0) { 78376272Sjhb printf("shared lock of (%s) %s @ %s:%d\n", 78476272Sjhb class->lc_name, lock->lo_name, file, line); 78576272Sjhb printf("while exclusively locked from %s:%d\n", 78676272Sjhb lock1->li_file, lock1->li_line); 78776272Sjhb panic("share->excl"); 78876272Sjhb } 78976272Sjhb if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 79076272Sjhb (flags & LOP_EXCLUSIVE) != 0) { 79176272Sjhb printf("exclusive lock of (%s) %s @ %s:%d\n", 79276272Sjhb class->lc_name, lock->lo_name, file, line); 79376272Sjhb printf("while share locked from %s:%d\n", 79476272Sjhb lock1->li_file, lock1->li_line); 79576272Sjhb panic("excl->share"); 79676272Sjhb } 79776272Sjhb return; 79876272Sjhb } 79976272Sjhb 80076272Sjhb /* 801112112Sjhb * Try locks do not block if they fail to acquire the lock, thus 802112112Sjhb * there is no danger of deadlocks or of switching while holding a 803112112Sjhb * spin lock if we acquire a lock via a try operation. 804112112Sjhb */ 805112112Sjhb if (flags & LOP_TRYLOCK) 806125160Sjhb return; 807112112Sjhb 808112112Sjhb /* 80974912Sjhb * Check for duplicate locks of the same type. Note that we only 81074912Sjhb * have to check for this on the last lock we just acquired. Any 81174912Sjhb * other cases will be caught as lock order violations. 81274912Sjhb */ 81376272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 81476272Sjhb w1 = lock1->li_lock->lo_witness; 81574912Sjhb if (w1 == w) { 816145422Sjeff if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) || 817145422Sjeff (flags & LOP_DUPOK)) 818125160Sjhb return; 81965557Sjasone w->w_same_squawked = 1; 82075755Sjhb printf("acquiring duplicate lock of same type: \"%s\"\n", 82193811Sjhb lock->lo_type); 82293811Sjhb printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 82393811Sjhb lock1->li_file, lock1->li_line); 82493811Sjhb printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 825131930Smarcel#ifdef KDB 826125160Sjhb goto debugger; 827125160Sjhb#else 828125160Sjhb return; 829112115Sjhb#endif 83065557Sjasone } 83165557Sjasone MPASS(!mtx_owned(&w_mtx)); 83274912Sjhb mtx_lock_spin(&w_mtx); 83365557Sjasone /* 834111881Sjhb * If we know that the the lock we are acquiring comes after 835111881Sjhb * the lock we most recently acquired in the lock order tree, 836111881Sjhb * then there is no need for any further checks. 837111881Sjhb */ 838149441Struckman if (isitmychild(w1, w)) { 83974912Sjhb mtx_unlock_spin(&w_mtx); 840125160Sjhb return; 84165557Sjasone } 84274912Sjhb for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 84374912Sjhb for (i = lle->ll_count - 1; i >= 0; i--, j++) { 84465557Sjasone 84574912Sjhb MPASS(j < WITNESS_COUNT); 84676272Sjhb lock1 = &lle->ll_children[i]; 84776272Sjhb w1 = lock1->li_lock->lo_witness; 84874912Sjhb 84974912Sjhb /* 85074912Sjhb * If this lock doesn't undergo witness checking, 85174912Sjhb * then skip it. 85274912Sjhb */ 85374912Sjhb if (w1 == NULL) { 85476272Sjhb KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 85574912Sjhb ("lock missing witness structure")); 85674912Sjhb continue; 85774912Sjhb } 85876272Sjhb /* 859111881Sjhb * If we are locking Giant and this is a sleepable 86076272Sjhb * lock, then skip it. 86176272Sjhb */ 862111881Sjhb if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 86376272Sjhb lock == &Giant.mtx_object) 86476272Sjhb continue; 86593690Sjhb /* 86693690Sjhb * If we are locking a sleepable lock and this lock 867111881Sjhb * is Giant, then skip it. 86893690Sjhb */ 869111881Sjhb if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 870111887Sjhb lock1->li_lock == &Giant.mtx_object) 871111881Sjhb continue; 872111881Sjhb /* 873111881Sjhb * If we are locking a sleepable lock and this lock 874111881Sjhb * isn't sleepable, we want to treat it as a lock 875111881Sjhb * order violation to enfore a general lock order of 876111881Sjhb * sleepable locks before non-sleepable locks. 877111881Sjhb */ 878149738Sjhb if (((lock->lo_flags & LO_SLEEPABLE) != 0 && 879111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 880149738Sjhb goto reversal; 881149738Sjhb /* 882149738Sjhb * Check the lock order hierarchy for a reveresal. 883149738Sjhb */ 884149738Sjhb if (!isitmydescendant(w, w1)) 88574912Sjhb continue; 886149738Sjhb reversal: 88774912Sjhb /* 88874912Sjhb * We have a lock order violation, check to see if it 88974912Sjhb * is allowed or has already been yelled about. 89074912Sjhb */ 89174912Sjhb mtx_unlock_spin(&w_mtx); 892105508Sphk#ifdef BLESSING 893125160Sjhb /* 894125160Sjhb * If the lock order is blessed, just bail. We don't 895125160Sjhb * look for other lock order violations though, which 896125160Sjhb * may be a bug. 897125160Sjhb */ 89865557Sjasone if (blessed(w, w1)) 899125160Sjhb return; 900105508Sphk#endif 90176272Sjhb if (lock1->li_lock == &Giant.mtx_object) { 90265557Sjasone if (w1->w_Giant_squawked) 903125160Sjhb return; 90465557Sjasone else 90565557Sjasone w1->w_Giant_squawked = 1; 90665557Sjasone } else { 90765557Sjasone if (w1->w_other_squawked) 908125160Sjhb return; 90965557Sjasone else 91065557Sjasone w1->w_other_squawked = 1; 91165557Sjasone } 91274912Sjhb /* 91374912Sjhb * Ok, yell about it. 91474912Sjhb */ 91565557Sjasone printf("lock order reversal\n"); 91674912Sjhb /* 91774912Sjhb * Try to locate an earlier lock with 91874912Sjhb * witness w in our list. 91974912Sjhb */ 92074912Sjhb do { 92176272Sjhb lock2 = &lle->ll_children[i]; 92276272Sjhb MPASS(lock2->li_lock != NULL); 92376272Sjhb if (lock2->li_lock->lo_witness == w) 92474912Sjhb break; 92574912Sjhb if (i == 0 && lle->ll_next != NULL) { 92674912Sjhb lle = lle->ll_next; 92774912Sjhb i = lle->ll_count - 1; 928106781Sjhb MPASS(i >= 0 && i < LOCK_NCHILDREN); 929125160Sjhb } else 930125160Sjhb i--; 93174912Sjhb } while (i >= 0); 93276272Sjhb if (i < 0) { 93393811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 93493811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 93593811Sjhb lock1->li_lock->lo_type, lock1->li_file, 93676272Sjhb lock1->li_line); 93793811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 93893811Sjhb lock->lo_name, lock->lo_type, file, line); 93976272Sjhb } else { 94093811Sjhb printf(" 1st %p %s (%s) @ %s:%d\n", 94193811Sjhb lock2->li_lock, lock2->li_lock->lo_name, 94293811Sjhb lock2->li_lock->lo_type, lock2->li_file, 94376272Sjhb lock2->li_line); 94493811Sjhb printf(" 2nd %p %s (%s) @ %s:%d\n", 94593811Sjhb lock1->li_lock, lock1->li_lock->lo_name, 94693811Sjhb lock1->li_lock->lo_type, lock1->li_file, 94776272Sjhb lock1->li_line); 94893811Sjhb printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 94993811Sjhb lock->lo_name, lock->lo_type, file, line); 95076272Sjhb } 951131930Smarcel#ifdef KDB 952125160Sjhb goto debugger; 953125160Sjhb#else 954125160Sjhb return; 955112115Sjhb#endif 95665557Sjasone } 95765557Sjasone } 95876272Sjhb lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 95978871Sjhb /* 960125160Sjhb * If requested, build a new lock order. However, don't build a new 961125160Sjhb * relationship between a sleepable lock and Giant if it is in the 962125160Sjhb * wrong direction. The correct lock order is that sleepable locks 963125160Sjhb * always come before Giant. 96478871Sjhb */ 965125160Sjhb if (flags & LOP_NEWORDER && 966125160Sjhb !(lock1->li_lock == &Giant.mtx_object && 967112117Sjhb (lock->lo_flags & LO_SLEEPABLE) != 0)) { 96887593Sobrien CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 96993811Sjhb lock->lo_type, lock1->li_lock->lo_type); 97078871Sjhb if (!itismychild(lock1->li_lock->lo_witness, w)) 971112117Sjhb /* Witness is dead. */ 972112117Sjhb return; 97378871Sjhb } 974112117Sjhb mtx_unlock_spin(&w_mtx); 975125160Sjhb return; 97665557Sjasone 977131930Smarcel#ifdef KDB 978125160Sjhbdebugger: 979125160Sjhb if (witness_trace) 980131930Smarcel kdb_backtrace(); 981131930Smarcel if (witness_kdb) 982131930Smarcel kdb_enter(__func__); 983125160Sjhb#endif 984125160Sjhb} 985125160Sjhb 986125160Sjhbvoid 987125160Sjhbwitness_lock(struct lock_object *lock, int flags, const char *file, int line) 988125160Sjhb{ 989125160Sjhb struct lock_list_entry **lock_list, *lle; 990125160Sjhb struct lock_instance *instance; 991125160Sjhb struct witness *w; 992125160Sjhb struct thread *td; 993125160Sjhb 994125160Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 995125160Sjhb panicstr != NULL) 996125160Sjhb return; 997125160Sjhb w = lock->lo_witness; 998125160Sjhb td = curthread; 999125160Sjhb file = fixup_filename(file); 1000125160Sjhb 1001125160Sjhb /* Determine lock list for this lock. */ 1002125160Sjhb if (lock->lo_class->lc_flags & LC_SLEEPLOCK) 1003125160Sjhb lock_list = &td->td_sleeplocks; 1004125160Sjhb else 1005125160Sjhb lock_list = PCPU_PTR(spinlocks); 1006125160Sjhb 1007125160Sjhb /* Check to see if we are recursing on a lock we already own. */ 1008125160Sjhb instance = find_instance(*lock_list, lock); 1009125160Sjhb if (instance != NULL) { 1010125160Sjhb instance->li_flags++; 1011125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 1012125160Sjhb td->td_proc->p_pid, lock->lo_name, 1013125160Sjhb instance->li_flags & LI_RECURSEMASK); 1014125160Sjhb instance->li_file = file; 1015125160Sjhb instance->li_line = line; 1016125160Sjhb return; 1017110779Speter } 1018125160Sjhb 1019125160Sjhb /* Update per-witness last file and line acquire. */ 102065557Sjasone w->w_file = file; 102165557Sjasone w->w_line = line; 1022125160Sjhb 1023125160Sjhb /* Find the next open lock instance in the list and fill it. */ 102474912Sjhb lle = *lock_list; 102576272Sjhb if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 102678785Sjhb lle = witness_lock_list_get(); 102778785Sjhb if (lle == NULL) 102865557Sjasone return; 102978785Sjhb lle->ll_next = *lock_list; 103087593Sobrien CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 103184680Sjhb td->td_proc->p_pid, lle); 103278785Sjhb *lock_list = lle; 103365557Sjasone } 1034125160Sjhb instance = &lle->ll_children[lle->ll_count++]; 1035125160Sjhb instance->li_lock = lock; 1036125160Sjhb instance->li_line = line; 1037125160Sjhb instance->li_file = file; 103876272Sjhb if ((flags & LOP_EXCLUSIVE) != 0) 1039125160Sjhb instance->li_flags = LI_EXCLUSIVE; 104076272Sjhb else 1041125160Sjhb instance->li_flags = 0; 104287593Sobrien CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 104384680Sjhb td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 104465557Sjasone} 104565557Sjasone 104665557Sjasonevoid 104782244Sjhbwitness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 104882244Sjhb{ 104982244Sjhb struct lock_instance *instance; 105082244Sjhb struct lock_class *class; 105182244Sjhb 105282284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1053112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 105482244Sjhb return; 105582244Sjhb class = lock->lo_class; 1056112116Sjhb file = fixup_filename(file); 105782244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 105882244Sjhb panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 105982244Sjhb class->lc_name, lock->lo_name, file, line); 106082244Sjhb if ((flags & LOP_TRYLOCK) == 0) 106182244Sjhb panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 106282244Sjhb lock->lo_name, file, line); 106382244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 106482244Sjhb panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 106582244Sjhb class->lc_name, lock->lo_name, file, line); 106683366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 106782244Sjhb if (instance == NULL) 106882244Sjhb panic("upgrade of unlocked lock (%s) %s @ %s:%d", 106982244Sjhb class->lc_name, lock->lo_name, file, line); 107082244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0) 107182244Sjhb panic("upgrade of exclusive lock (%s) %s @ %s:%d", 107282244Sjhb class->lc_name, lock->lo_name, file, line); 107382244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 107482244Sjhb panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 107582244Sjhb class->lc_name, lock->lo_name, 107682244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 107782244Sjhb instance->li_flags |= LI_EXCLUSIVE; 107882244Sjhb} 107982244Sjhb 108082244Sjhbvoid 108182244Sjhbwitness_downgrade(struct lock_object *lock, int flags, const char *file, 108282244Sjhb int line) 108382244Sjhb{ 108482244Sjhb struct lock_instance *instance; 108582244Sjhb struct lock_class *class; 108682244Sjhb 108782284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1088112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 108982244Sjhb return; 109082244Sjhb class = lock->lo_class; 1091112116Sjhb file = fixup_filename(file); 109282244Sjhb if ((lock->lo_flags & LO_UPGRADABLE) == 0) 109382244Sjhb panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 109482244Sjhb class->lc_name, lock->lo_name, file, line); 109582244Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 109682244Sjhb panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 109782244Sjhb class->lc_name, lock->lo_name, file, line); 109883366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 109982244Sjhb if (instance == NULL) 110082244Sjhb panic("downgrade of unlocked lock (%s) %s @ %s:%d", 110182244Sjhb class->lc_name, lock->lo_name, file, line); 110282244Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0) 110382244Sjhb panic("downgrade of shared lock (%s) %s @ %s:%d", 110482244Sjhb class->lc_name, lock->lo_name, file, line); 110582244Sjhb if ((instance->li_flags & LI_RECURSEMASK) != 0) 110682244Sjhb panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 110782244Sjhb class->lc_name, lock->lo_name, 110882244Sjhb instance->li_flags & LI_RECURSEMASK, file, line); 110982244Sjhb instance->li_flags &= ~LI_EXCLUSIVE; 111082244Sjhb} 111182244Sjhb 111282244Sjhbvoid 111374912Sjhbwitness_unlock(struct lock_object *lock, int flags, const char *file, int line) 111465557Sjasone{ 111574912Sjhb struct lock_list_entry **lock_list, *lle; 111676272Sjhb struct lock_instance *instance; 111774912Sjhb struct lock_class *class; 111883366Sjulian struct thread *td; 111992858Simp register_t s; 112074912Sjhb int i, j; 112165557Sjasone 1122112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 112380747Sjhb panicstr != NULL) 112471352Sjasone return; 112583366Sjulian td = curthread; 112674912Sjhb class = lock->lo_class; 1127112116Sjhb file = fixup_filename(file); 1128125160Sjhb 1129125160Sjhb /* Find lock instance associated with this lock. */ 113076272Sjhb if (class->lc_flags & LC_SLEEPLOCK) 113183366Sjulian lock_list = &td->td_sleeplocks; 113276272Sjhb else 113374912Sjhb lock_list = PCPU_PTR(spinlocks); 113474912Sjhb for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 113576272Sjhb for (i = 0; i < (*lock_list)->ll_count; i++) { 113676272Sjhb instance = &(*lock_list)->ll_children[i]; 1137125160Sjhb if (instance->li_lock == lock) 1138125160Sjhb goto found; 113976272Sjhb } 114076272Sjhb panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 114176272Sjhb file, line); 1142125160Sjhbfound: 1143125160Sjhb 1144125160Sjhb /* First, check for shared/exclusive mismatches. */ 1145125160Sjhb if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 1146125160Sjhb (flags & LOP_EXCLUSIVE) == 0) { 1147125160Sjhb printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1148125160Sjhb lock->lo_name, file, line); 1149125160Sjhb printf("while exclusively locked from %s:%d\n", 1150125160Sjhb instance->li_file, instance->li_line); 1151125160Sjhb panic("excl->ushare"); 1152125160Sjhb } 1153125160Sjhb if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 1154125160Sjhb (flags & LOP_EXCLUSIVE) != 0) { 1155125160Sjhb printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1156125160Sjhb lock->lo_name, file, line); 1157125160Sjhb printf("while share locked from %s:%d\n", instance->li_file, 1158125160Sjhb instance->li_line); 1159125160Sjhb panic("share->uexcl"); 1160125160Sjhb } 1161125160Sjhb 1162125160Sjhb /* If we are recursed, unrecurse. */ 1163125160Sjhb if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1164125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1165125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1166125160Sjhb instance->li_flags); 1167125160Sjhb instance->li_flags--; 1168125160Sjhb return; 1169125160Sjhb } 1170125160Sjhb 1171125160Sjhb /* Otherwise, remove this item from the list. */ 1172125160Sjhb s = intr_disable(); 1173125160Sjhb CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1174125160Sjhb td->td_proc->p_pid, instance->li_lock->lo_name, 1175125160Sjhb (*lock_list)->ll_count - 1); 1176125160Sjhb for (j = i; j < (*lock_list)->ll_count - 1; j++) 1177125160Sjhb (*lock_list)->ll_children[j] = 1178125160Sjhb (*lock_list)->ll_children[j + 1]; 1179125160Sjhb (*lock_list)->ll_count--; 1180125160Sjhb intr_restore(s); 1181125160Sjhb 1182125160Sjhb /* If this lock list entry is now empty, free it. */ 1183125160Sjhb if ((*lock_list)->ll_count == 0) { 1184125160Sjhb lle = *lock_list; 1185125160Sjhb *lock_list = lle->ll_next; 1186125160Sjhb CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1187125160Sjhb td->td_proc->p_pid, lle); 1188125160Sjhb witness_lock_list_free(lle); 1189125160Sjhb } 119065557Sjasone} 119165557Sjasone 119274912Sjhb/* 1193111881Sjhb * Warn if any locks other than 'lock' are held. Flags can be passed in to 1194111881Sjhb * exempt Giant and sleepable locks from the checks as well. If any 1195111881Sjhb * non-exempt locks are held, then a supplied message is printed to the 1196111881Sjhb * console along with a list of the offending locks. If indicated in the 1197111881Sjhb * flags then a failure results in a panic as well. 119874912Sjhb */ 119965557Sjasoneint 1200111881Sjhbwitness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 120165557Sjasone{ 1202111881Sjhb struct lock_list_entry *lle; 120376272Sjhb struct lock_instance *lock1; 120483366Sjulian struct thread *td; 1205111881Sjhb va_list ap; 120674912Sjhb int i, n; 120765557Sjasone 1208112562Sjhb if (witness_cold || witness_watch == 0 || panicstr != NULL) 120974912Sjhb return (0); 121074912Sjhb n = 0; 121183366Sjulian td = curthread; 1212111881Sjhb for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 121374912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 121476272Sjhb lock1 = &lle->ll_children[i]; 1215111881Sjhb if (lock1->li_lock == lock) 1216111881Sjhb continue; 1217111881Sjhb if (flags & WARN_GIANTOK && 121876272Sjhb lock1->li_lock == &Giant.mtx_object) 121974912Sjhb continue; 1220111881Sjhb if (flags & WARN_SLEEPOK && 1221111881Sjhb (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 122276272Sjhb continue; 1223111881Sjhb if (n == 0) { 1224111881Sjhb va_start(ap, fmt); 1225111881Sjhb vprintf(fmt, ap); 1226111881Sjhb va_end(ap); 1227111881Sjhb printf(" with the following"); 1228111881Sjhb if (flags & WARN_SLEEPOK) 1229111881Sjhb printf(" non-sleepable"); 1230118441Sjhb printf(" locks held:\n"); 123176272Sjhb } 123274912Sjhb n++; 1233111881Sjhb witness_list_lock(lock1); 123474912Sjhb } 1235111881Sjhb if (PCPU_GET(spinlocks) != NULL) { 123697006Sjhb /* 123797006Sjhb * Since we already hold a spinlock preemption is 123897006Sjhb * already blocked. 123997006Sjhb */ 1240111881Sjhb if (n == 0) { 1241111881Sjhb va_start(ap, fmt); 1242111881Sjhb vprintf(fmt, ap); 1243111881Sjhb va_end(ap); 1244111881Sjhb printf(" with the following"); 1245111881Sjhb if (flags & WARN_SLEEPOK) 1246111881Sjhb printf(" non-sleepable"); 1247118441Sjhb printf(" locks held:\n"); 1248111881Sjhb } 1249111881Sjhb n += witness_list_locks(PCPU_PTR(spinlocks)); 125065557Sjasone } 1251111881Sjhb if (flags & WARN_PANIC && n) 1252111881Sjhb panic("witness_warn"); 1253131930Smarcel#ifdef KDB 1254131930Smarcel else if (witness_kdb && n) 1255131930Smarcel kdb_enter(__func__); 1256127323Salfred else if (witness_trace && n) 1257131930Smarcel kdb_backtrace(); 1258111881Sjhb#endif 125965557Sjasone return (n); 126065557Sjasone} 126165557Sjasone 1262102448Siedowseconst char * 1263102448Siedowsewitness_file(struct lock_object *lock) 1264102448Siedowse{ 1265102448Siedowse struct witness *w; 1266102448Siedowse 1267112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1268102448Siedowse return ("?"); 1269102448Siedowse w = lock->lo_witness; 1270102448Siedowse return (w->w_file); 1271102448Siedowse} 1272102448Siedowse 1273102448Siedowseint 1274102448Siedowsewitness_line(struct lock_object *lock) 1275102448Siedowse{ 1276102448Siedowse struct witness *w; 1277102448Siedowse 1278112562Sjhb if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1279102448Siedowse return (0); 1280102448Siedowse w = lock->lo_witness; 1281102448Siedowse return (w->w_line); 1282102448Siedowse} 1283102448Siedowse 128465856Sjhbstatic struct witness * 128574912Sjhbenroll(const char *description, struct lock_class *lock_class) 128665557Sjasone{ 128774912Sjhb struct witness *w; 128865557Sjasone 1289125348Sjhb if (witness_watch == 0 || panicstr != NULL) 129065557Sjasone return (NULL); 129174912Sjhb if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 129265557Sjasone return (NULL); 129374912Sjhb mtx_lock_spin(&w_mtx); 129474912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 129597948Sjhb if (w->w_name == description || (w->w_refcount > 0 && 129697948Sjhb strcmp(description, w->w_name) == 0)) { 129775362Sjhb w->w_refcount++; 129874912Sjhb mtx_unlock_spin(&w_mtx); 129974912Sjhb if (lock_class != w->w_class) 130074912Sjhb panic( 130174912Sjhb "lock (%s) %s does not match earlier (%s) lock", 130274912Sjhb description, lock_class->lc_name, 130374912Sjhb w->w_class->lc_name); 130465557Sjasone return (w); 130565557Sjasone } 130665557Sjasone } 130774912Sjhb /* 130874912Sjhb * This isn't quite right, as witness_cold is still 0 while we 130974912Sjhb * enroll all the locks initialized before witness_initialize(). 131074912Sjhb */ 131175364Sbp if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 131275364Sbp mtx_unlock_spin(&w_mtx); 131374912Sjhb panic("spin lock %s not in order list", description); 131475364Sbp } 131565557Sjasone if ((w = witness_get()) == NULL) 131665557Sjasone return (NULL); 131774912Sjhb w->w_name = description; 131874912Sjhb w->w_class = lock_class; 131975362Sjhb w->w_refcount = 1; 132074912Sjhb STAILQ_INSERT_HEAD(&w_all, w, w_list); 1321149441Struckman if (lock_class->lc_flags & LC_SPINLOCK) { 132274912Sjhb STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1323149441Struckman w_spin_cnt++; 1324149441Struckman } else if (lock_class->lc_flags & LC_SLEEPLOCK) { 132574912Sjhb STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1326149441Struckman w_sleep_cnt++; 1327149441Struckman } else { 132875364Sbp mtx_unlock_spin(&w_mtx); 132974912Sjhb panic("lock class %s is not sleep or spin", 133074912Sjhb lock_class->lc_name); 133175364Sbp } 133274912Sjhb mtx_unlock_spin(&w_mtx); 133365557Sjasone return (w); 133465557Sjasone} 133565557Sjasone 1336112117Sjhb/* Don't let the door bang you on the way out... */ 133765557Sjasonestatic int 1338112117Sjhbdepart(struct witness *w) 133965557Sjasone{ 1340112117Sjhb struct witness_child_list_entry *wcl, *nwcl; 134174912Sjhb struct witness_list *list; 1342112117Sjhb struct witness *parent; 134365557Sjasone 1344112117Sjhb MPASS(w->w_refcount == 0); 1345149441Struckman if (w->w_class->lc_flags & LC_SLEEPLOCK) { 1346112117Sjhb list = &w_sleep; 1347149441Struckman w_sleep_cnt--; 1348149441Struckman } else { 1349112117Sjhb list = &w_spin; 1350149441Struckman w_spin_cnt--; 1351149441Struckman } 1352112117Sjhb /* 1353112117Sjhb * First, we run through the entire tree looking for any 1354112117Sjhb * witnesses that the outgoing witness is a child of. For 1355112117Sjhb * each parent that we find, we reparent all the direct 1356112117Sjhb * children of the outgoing witness to its parent. 1357112117Sjhb */ 1358112117Sjhb STAILQ_FOREACH(parent, list, w_typelist) { 1359112117Sjhb if (!isitmychild(parent, w)) 1360112117Sjhb continue; 1361112117Sjhb removechild(parent, w); 1362112117Sjhb } 1363112117Sjhb 1364112117Sjhb /* 1365112117Sjhb * Now we go through and free up the child list of the 1366112117Sjhb * outgoing witness. 1367112117Sjhb */ 1368112117Sjhb for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1369112117Sjhb nwcl = wcl->wcl_next; 1370149441Struckman w_child_cnt--; 1371112117Sjhb witness_child_free(wcl); 1372112117Sjhb } 1373112117Sjhb 1374112117Sjhb /* 1375112117Sjhb * Detach from various lists and free. 1376112117Sjhb */ 1377112117Sjhb STAILQ_REMOVE(list, w, witness, w_typelist); 1378112117Sjhb STAILQ_REMOVE(&w_all, w, witness, w_list); 1379112117Sjhb witness_free(w); 1380112117Sjhb 1381112117Sjhb return (1); 1382112117Sjhb} 1383112117Sjhb 1384112117Sjhb/* 1385112117Sjhb * Add "child" as a direct child of "parent". Returns false if 1386112117Sjhb * we fail due to out of memory. 1387112117Sjhb */ 1388112117Sjhbstatic int 1389112117Sjhbinsertchild(struct witness *parent, struct witness *child) 1390112117Sjhb{ 1391112117Sjhb struct witness_child_list_entry **wcl; 1392112117Sjhb 139374912Sjhb MPASS(child != NULL && parent != NULL); 139474912Sjhb 139565557Sjasone /* 139665557Sjasone * Insert "child" after "parent" 139765557Sjasone */ 139874912Sjhb wcl = &parent->w_children; 139974912Sjhb while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 140074912Sjhb wcl = &(*wcl)->wcl_next; 140174912Sjhb if (*wcl == NULL) { 140274912Sjhb *wcl = witness_child_get(); 140374912Sjhb if (*wcl == NULL) 1404112117Sjhb return (0); 1405149441Struckman w_child_cnt++; 140665557Sjasone } 140774912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 140874912Sjhb 1409112117Sjhb return (1); 1410112117Sjhb} 1411112117Sjhb 1412112117Sjhb 1413112117Sjhbstatic int 1414112117Sjhbitismychild(struct witness *parent, struct witness *child) 1415112117Sjhb{ 1416112117Sjhb struct witness_list *list; 1417112117Sjhb 1418112117Sjhb MPASS(child != NULL && parent != NULL); 1419112117Sjhb if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1420112117Sjhb (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1421112117Sjhb panic( 1422112117Sjhb "%s: parent (%s) and child (%s) are not the same lock type", 1423112117Sjhb __func__, parent->w_class->lc_name, 1424112117Sjhb child->w_class->lc_name); 1425112117Sjhb 1426112117Sjhb if (!insertchild(parent, child)) 142765557Sjasone return (0); 1428112117Sjhb 142974912Sjhb if (parent->w_class->lc_flags & LC_SLEEPLOCK) 143074912Sjhb list = &w_sleep; 143174912Sjhb else 143274912Sjhb list = &w_spin; 1433149441Struckman return (1); 143465557Sjasone} 143565557Sjasone 143665557Sjasonestatic void 143765856Sjhbremovechild(struct witness *parent, struct witness *child) 143865557Sjasone{ 143974912Sjhb struct witness_child_list_entry **wcl, *wcl1; 144065557Sjasone int i; 144165557Sjasone 144274912Sjhb for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 144374912Sjhb for (i = 0; i < (*wcl)->wcl_count; i++) 144474912Sjhb if ((*wcl)->wcl_children[i] == child) 144565557Sjasone goto found; 144665557Sjasone return; 144765557Sjasonefound: 144874912Sjhb (*wcl)->wcl_count--; 144974912Sjhb if ((*wcl)->wcl_count > i) 145074912Sjhb (*wcl)->wcl_children[i] = 145174912Sjhb (*wcl)->wcl_children[(*wcl)->wcl_count]; 145274912Sjhb MPASS((*wcl)->wcl_children[i] != NULL); 145374912Sjhb if ((*wcl)->wcl_count != 0) 145465557Sjasone return; 145574912Sjhb wcl1 = *wcl; 145674912Sjhb *wcl = wcl1->wcl_next; 1457149441Struckman w_child_cnt--; 145874912Sjhb witness_child_free(wcl1); 145965557Sjasone} 146065557Sjasone 146165557Sjasonestatic int 146265856Sjhbisitmychild(struct witness *parent, struct witness *child) 146365557Sjasone{ 146474912Sjhb struct witness_child_list_entry *wcl; 146565557Sjasone int i; 146665557Sjasone 146774912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 146874912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 146974912Sjhb if (wcl->wcl_children[i] == child) 147065557Sjasone return (1); 147165557Sjasone } 147265557Sjasone } 147365557Sjasone return (0); 147465557Sjasone} 147565557Sjasone 147665557Sjasonestatic int 147765856Sjhbisitmydescendant(struct witness *parent, struct witness *child) 147865557Sjasone{ 147974912Sjhb struct witness_child_list_entry *wcl; 148074912Sjhb int i, j; 148165557Sjasone 148274912Sjhb if (isitmychild(parent, child)) 148374912Sjhb return (1); 148474912Sjhb j = 0; 148574912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 148667352Sjhb MPASS(j < 1000); 148774912Sjhb for (i = 0; i < wcl->wcl_count; i++) { 148874912Sjhb if (isitmydescendant(wcl->wcl_children[i], child)) 148965557Sjasone return (1); 149065557Sjasone } 149174912Sjhb j++; 149265557Sjasone } 149365557Sjasone return (0); 149465557Sjasone} 149565557Sjasone 1496104094Sphkstatic void 149765557Sjasonewitness_levelall (void) 149865557Sjasone{ 149974912Sjhb struct witness_list *list; 150065856Sjhb struct witness *w, *w1; 150165557Sjasone 150274912Sjhb /* 150374912Sjhb * First clear all levels. 150474912Sjhb */ 150574912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 150674912Sjhb w->w_level = 0; 150774912Sjhb } 150874912Sjhb 150974912Sjhb /* 151074912Sjhb * Look for locks with no parent and level all their descendants. 151174912Sjhb */ 151274912Sjhb STAILQ_FOREACH(w, &w_all, w_list) { 151374912Sjhb /* 151474912Sjhb * This is just an optimization, technically we could get 151574912Sjhb * away just walking the all list each time. 151674912Sjhb */ 151774912Sjhb if (w->w_class->lc_flags & LC_SLEEPLOCK) 151874912Sjhb list = &w_sleep; 151974912Sjhb else 152074912Sjhb list = &w_spin; 152174912Sjhb STAILQ_FOREACH(w1, list, w_typelist) { 152265557Sjasone if (isitmychild(w1, w)) 152374912Sjhb goto skip; 152465557Sjasone } 152565557Sjasone witness_leveldescendents(w, 0); 152674912Sjhb skip: 152795541Smarcel ; /* silence GCC 3.x */ 152865557Sjasone } 152965557Sjasone} 153065557Sjasone 153165557Sjasonestatic void 153265856Sjhbwitness_leveldescendents(struct witness *parent, int level) 153365557Sjasone{ 153474912Sjhb struct witness_child_list_entry *wcl; 153565557Sjasone int i; 153665557Sjasone 153765557Sjasone if (parent->w_level < level) 153865557Sjasone parent->w_level = level; 153965557Sjasone level++; 154074912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 154174912Sjhb for (i = 0; i < wcl->wcl_count; i++) 154274912Sjhb witness_leveldescendents(wcl->wcl_children[i], level); 154365557Sjasone} 154465557Sjasone 154565557Sjasonestatic void 154665856Sjhbwitness_displaydescendants(void(*prnt)(const char *fmt, ...), 1547112118Sjhb struct witness *parent, int indent) 154865557Sjasone{ 154974912Sjhb struct witness_child_list_entry *wcl; 155074912Sjhb int i, level; 155165557Sjasone 155295543Sjhb level = parent->w_level; 155374912Sjhb prnt("%-2d", level); 1554112118Sjhb for (i = 0; i < indent; i++) 155565557Sjasone prnt(" "); 1556112118Sjhb if (parent->w_refcount > 0) 1557112118Sjhb prnt("%s", parent->w_name); 1558112118Sjhb else 1559112118Sjhb prnt("(dead)"); 1560112118Sjhb if (parent->w_displayed) { 1561112118Sjhb prnt(" -- (already displayed)\n"); 1562112118Sjhb return; 1563112118Sjhb } 1564112118Sjhb parent->w_displayed = 1; 156597948Sjhb if (parent->w_refcount > 0) { 156697948Sjhb if (parent->w_file != NULL) 1567112118Sjhb prnt(" -- last acquired @ %s:%d", parent->w_file, 156897948Sjhb parent->w_line); 1569112118Sjhb } 1570112118Sjhb prnt("\n"); 157174912Sjhb for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 157274912Sjhb for (i = 0; i < wcl->wcl_count; i++) 157374912Sjhb witness_displaydescendants(prnt, 1574112118Sjhb wcl->wcl_children[i], indent + 1); 157574912Sjhb} 157665557Sjasone 1577105508Sphk#ifdef BLESSING 157865557Sjasonestatic int 157965856Sjhbblessed(struct witness *w1, struct witness *w2) 158065557Sjasone{ 158165557Sjasone int i; 158265856Sjhb struct witness_blessed *b; 158365557Sjasone 158465557Sjasone for (i = 0; i < blessed_count; i++) { 158565557Sjasone b = &blessed_list[i]; 158674912Sjhb if (strcmp(w1->w_name, b->b_lock1) == 0) { 158774912Sjhb if (strcmp(w2->w_name, b->b_lock2) == 0) 158865557Sjasone return (1); 158965557Sjasone continue; 159065557Sjasone } 159174912Sjhb if (strcmp(w1->w_name, b->b_lock2) == 0) 159274912Sjhb if (strcmp(w2->w_name, b->b_lock1) == 0) 159365557Sjasone return (1); 159465557Sjasone } 159565557Sjasone return (0); 159665557Sjasone} 1597105508Sphk#endif 159865557Sjasone 159965856Sjhbstatic struct witness * 160074912Sjhbwitness_get(void) 160165557Sjasone{ 160265856Sjhb struct witness *w; 160365557Sjasone 1604112562Sjhb if (witness_watch == 0) { 160576481Sjhb mtx_unlock_spin(&w_mtx); 160676481Sjhb return (NULL); 160776481Sjhb } 160874912Sjhb if (STAILQ_EMPTY(&w_free)) { 1609112562Sjhb witness_watch = 0; 161074912Sjhb mtx_unlock_spin(&w_mtx); 161174912Sjhb printf("%s: witness exhausted\n", __func__); 161265557Sjasone return (NULL); 161365557Sjasone } 161474912Sjhb w = STAILQ_FIRST(&w_free); 161574912Sjhb STAILQ_REMOVE_HEAD(&w_free, w_list); 1616149441Struckman w_free_cnt--; 161765856Sjhb bzero(w, sizeof(*w)); 161865557Sjasone return (w); 161965557Sjasone} 162065557Sjasone 162165557Sjasonestatic void 162265856Sjhbwitness_free(struct witness *w) 162365557Sjasone{ 162474912Sjhb 162574912Sjhb STAILQ_INSERT_HEAD(&w_free, w, w_list); 1626149441Struckman w_free_cnt++; 162765557Sjasone} 162865557Sjasone 162974912Sjhbstatic struct witness_child_list_entry * 163074912Sjhbwitness_child_get(void) 163165557Sjasone{ 163274912Sjhb struct witness_child_list_entry *wcl; 163365557Sjasone 1634112562Sjhb if (witness_watch == 0) { 163576481Sjhb mtx_unlock_spin(&w_mtx); 163676481Sjhb return (NULL); 163776481Sjhb } 163874912Sjhb wcl = w_child_free; 163974912Sjhb if (wcl == NULL) { 1640112562Sjhb witness_watch = 0; 164174912Sjhb mtx_unlock_spin(&w_mtx); 164274912Sjhb printf("%s: witness exhausted\n", __func__); 164374912Sjhb return (NULL); 164465557Sjasone } 164574912Sjhb w_child_free = wcl->wcl_next; 1646149441Struckman w_child_free_cnt--; 164774912Sjhb bzero(wcl, sizeof(*wcl)); 164874912Sjhb return (wcl); 164974912Sjhb} 165069881Sjake 165174912Sjhbstatic void 165274912Sjhbwitness_child_free(struct witness_child_list_entry *wcl) 165374912Sjhb{ 165474912Sjhb 165574912Sjhb wcl->wcl_next = w_child_free; 165674912Sjhb w_child_free = wcl; 1657149441Struckman w_child_free_cnt++; 165865557Sjasone} 165965557Sjasone 166074912Sjhbstatic struct lock_list_entry * 166174912Sjhbwitness_lock_list_get(void) 166274912Sjhb{ 166374912Sjhb struct lock_list_entry *lle; 166471709Sjhb 1665112562Sjhb if (witness_watch == 0) 166676481Sjhb return (NULL); 166774912Sjhb mtx_lock_spin(&w_mtx); 166874912Sjhb lle = w_lock_list_free; 166974912Sjhb if (lle == NULL) { 1670112562Sjhb witness_watch = 0; 167174912Sjhb mtx_unlock_spin(&w_mtx); 167274912Sjhb printf("%s: witness exhausted\n", __func__); 167374912Sjhb return (NULL); 167474912Sjhb } 167574912Sjhb w_lock_list_free = lle->ll_next; 167674912Sjhb mtx_unlock_spin(&w_mtx); 167774912Sjhb bzero(lle, sizeof(*lle)); 167874912Sjhb return (lle); 167974912Sjhb} 168074912Sjhb 168174912Sjhbstatic void 168274912Sjhbwitness_lock_list_free(struct lock_list_entry *lle) 168371709Sjhb{ 168471709Sjhb 168574912Sjhb mtx_lock_spin(&w_mtx); 168674912Sjhb lle->ll_next = w_lock_list_free; 168774912Sjhb w_lock_list_free = lle; 168874912Sjhb mtx_unlock_spin(&w_mtx); 168971709Sjhb} 169071709Sjhb 169176272Sjhbstatic struct lock_instance * 169276272Sjhbfind_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 169376272Sjhb{ 169476272Sjhb struct lock_list_entry *lle; 169576272Sjhb struct lock_instance *instance; 169676272Sjhb int i; 169776272Sjhb 169876272Sjhb for (lle = lock_list; lle != NULL; lle = lle->ll_next) 169976272Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 170076272Sjhb instance = &lle->ll_children[i]; 170176272Sjhb if (instance->li_lock == lock) 170276272Sjhb return (instance); 170376272Sjhb } 170476272Sjhb return (NULL); 170576272Sjhb} 170676272Sjhb 1707111881Sjhbstatic void 1708111881Sjhbwitness_list_lock(struct lock_instance *instance) 1709111881Sjhb{ 1710111881Sjhb struct lock_object *lock; 1711111881Sjhb 1712111881Sjhb lock = instance->li_lock; 1713111881Sjhb printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1714111881Sjhb "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name); 1715111881Sjhb if (lock->lo_type != lock->lo_name) 1716111881Sjhb printf(" (%s)", lock->lo_type); 1717111881Sjhb printf(" r = %d (%p) locked @ %s:%d\n", 1718111881Sjhb instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1719111881Sjhb instance->li_line); 1720111881Sjhb} 1721111881Sjhb 1722140637Srwatson#ifdef DDB 1723139333Srwatsonstatic int 1724139333Srwatsonwitness_thread_has_locks(struct thread *td) 1725139333Srwatson{ 1726139333Srwatson 1727139333Srwatson return (td->td_sleeplocks != NULL); 1728139333Srwatson} 1729139333Srwatson 1730139333Srwatsonstatic int 1731139333Srwatsonwitness_proc_has_locks(struct proc *p) 1732139333Srwatson{ 1733139333Srwatson struct thread *td; 1734139333Srwatson 1735139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 1736139333Srwatson if (witness_thread_has_locks(td)) 1737139333Srwatson return (1); 1738139333Srwatson } 1739139333Srwatson return (0); 1740139333Srwatson} 1741140637Srwatson#endif 1742139333Srwatson 174374912Sjhbint 174475273Sjhbwitness_list_locks(struct lock_list_entry **lock_list) 174572224Sjhb{ 174675273Sjhb struct lock_list_entry *lle; 174774912Sjhb int i, nheld; 174872224Sjhb 174974912Sjhb nheld = 0; 175074912Sjhb for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 175174912Sjhb for (i = lle->ll_count - 1; i >= 0; i--) { 1752111881Sjhb witness_list_lock(&lle->ll_children[i]); 175374912Sjhb nheld++; 175474912Sjhb } 175575273Sjhb return (nheld); 175675273Sjhb} 175775273Sjhb 1758118271Sjhb/* 1759118271Sjhb * This is a bit risky at best. We call this function when we have timed 1760118271Sjhb * out acquiring a spin lock, and we assume that the other CPU is stuck 1761118271Sjhb * with this lock held. So, we go groveling around in the other CPU's 1762118271Sjhb * per-cpu data to try to find the lock instance for this spin lock to 1763118271Sjhb * see when it was last acquired. 1764118271Sjhb */ 176565557Sjasonevoid 1766118271Sjhbwitness_display_spinlock(struct lock_object *lock, struct thread *owner) 1767118271Sjhb{ 1768118271Sjhb struct lock_instance *instance; 1769118271Sjhb struct pcpu *pc; 1770118271Sjhb 1771118271Sjhb if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 1772118271Sjhb return; 1773118271Sjhb pc = pcpu_find(owner->td_oncpu); 1774118271Sjhb instance = find_instance(pc->pc_spinlocks, lock); 1775118271Sjhb if (instance != NULL) 1776118271Sjhb witness_list_lock(instance); 1777118271Sjhb} 1778118271Sjhb 1779118271Sjhbvoid 178074912Sjhbwitness_save(struct lock_object *lock, const char **filep, int *linep) 178165557Sjasone{ 178276272Sjhb struct lock_instance *instance; 178371320Sjasone 178482284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1785112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 178671352Sjasone return; 178782243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 178882243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 178982243Sjhb lock->lo_class->lc_name, lock->lo_name); 179083366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 179182243Sjhb if (instance == NULL) 179282243Sjhb panic("%s: lock (%s) %s not locked", __func__, 179382243Sjhb lock->lo_class->lc_name, lock->lo_name); 179476272Sjhb *filep = instance->li_file; 179576272Sjhb *linep = instance->li_line; 179665557Sjasone} 179765557Sjasone 179865557Sjasonevoid 179974912Sjhbwitness_restore(struct lock_object *lock, const char *file, int line) 180065557Sjasone{ 180176272Sjhb struct lock_instance *instance; 180271320Sjasone 180382284Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1804112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 180571352Sjasone return; 180682243Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 180782243Sjhb panic("%s: lock (%s) %s is not a sleep lock", __func__, 180882243Sjhb lock->lo_class->lc_name, lock->lo_name); 180983366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 181082243Sjhb if (instance == NULL) 181182243Sjhb panic("%s: lock (%s) %s not locked", __func__, 181282243Sjhb lock->lo_class->lc_name, lock->lo_name); 181374912Sjhb lock->lo_witness->w_file = file; 181474912Sjhb lock->lo_witness->w_line = line; 181576272Sjhb instance->li_file = file; 181676272Sjhb instance->li_line = line; 181765557Sjasone} 181865557Sjasone 181978871Sjhbvoid 182078871Sjhbwitness_assert(struct lock_object *lock, int flags, const char *file, int line) 182178871Sjhb{ 182278871Sjhb#ifdef INVARIANT_SUPPORT 182378871Sjhb struct lock_instance *instance; 182478871Sjhb 1825112562Sjhb if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 182678941Sjhb return; 182778871Sjhb if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 182883366Sjulian instance = find_instance(curthread->td_sleeplocks, lock); 182978871Sjhb else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 183078871Sjhb instance = find_instance(PCPU_GET(spinlocks), lock); 183186422Sjhb else { 183278871Sjhb panic("Lock (%s) %s is not sleep or spin!", 183378871Sjhb lock->lo_class->lc_name, lock->lo_name); 183486422Sjhb } 1835112116Sjhb file = fixup_filename(file); 183678871Sjhb switch (flags) { 183778871Sjhb case LA_UNLOCKED: 183878871Sjhb if (instance != NULL) 183978871Sjhb panic("Lock (%s) %s locked @ %s:%d.", 184078871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 184178871Sjhb break; 184278871Sjhb case LA_LOCKED: 184378871Sjhb case LA_LOCKED | LA_RECURSED: 184478871Sjhb case LA_LOCKED | LA_NOTRECURSED: 184578871Sjhb case LA_SLOCKED: 184678871Sjhb case LA_SLOCKED | LA_RECURSED: 184778871Sjhb case LA_SLOCKED | LA_NOTRECURSED: 184878871Sjhb case LA_XLOCKED: 184978871Sjhb case LA_XLOCKED | LA_RECURSED: 185078871Sjhb case LA_XLOCKED | LA_NOTRECURSED: 185186422Sjhb if (instance == NULL) { 185278871Sjhb panic("Lock (%s) %s not locked @ %s:%d.", 185378871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 185486422Sjhb break; 185586422Sjhb } 185678871Sjhb if ((flags & LA_XLOCKED) != 0 && 185778871Sjhb (instance->li_flags & LI_EXCLUSIVE) == 0) 185878871Sjhb panic("Lock (%s) %s not exclusively locked @ %s:%d.", 185978871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 186078871Sjhb if ((flags & LA_SLOCKED) != 0 && 186178871Sjhb (instance->li_flags & LI_EXCLUSIVE) != 0) 186278871Sjhb panic("Lock (%s) %s exclusively locked @ %s:%d.", 186378871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 186478871Sjhb if ((flags & LA_RECURSED) != 0 && 186578871Sjhb (instance->li_flags & LI_RECURSEMASK) == 0) 186678871Sjhb panic("Lock (%s) %s not recursed @ %s:%d.", 186778871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 186878871Sjhb if ((flags & LA_NOTRECURSED) != 0 && 186978871Sjhb (instance->li_flags & LI_RECURSEMASK) != 0) 187078871Sjhb panic("Lock (%s) %s recursed @ %s:%d.", 187178871Sjhb lock->lo_class->lc_name, lock->lo_name, file, line); 187278871Sjhb break; 187378871Sjhb default: 187478871Sjhb panic("Invalid lock assertion at %s:%d.", file, line); 187578871Sjhb 187678871Sjhb } 187778871Sjhb#endif /* INVARIANT_SUPPORT */ 187878871Sjhb} 187978871Sjhb 188074912Sjhb#ifdef DDB 1881112061Sjhbstatic void 1882112061Sjhbwitness_list(struct thread *td) 1883112061Sjhb{ 188474912Sjhb 1885112061Sjhb KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1886131930Smarcel KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 1887112061Sjhb 1888112562Sjhb if (witness_watch == 0) 1889112061Sjhb return; 1890112061Sjhb 1891112061Sjhb witness_list_locks(&td->td_sleeplocks); 1892112061Sjhb 1893112061Sjhb /* 1894112061Sjhb * We only handle spinlocks if td == curthread. This is somewhat broken 1895112061Sjhb * if td is currently executing on some other CPU and holds spin locks 1896112061Sjhb * as we won't display those locks. If we had a MI way of getting 1897112061Sjhb * the per-cpu data for a given cpu then we could use 1898113339Sjulian * td->td_oncpu to get the list of spinlocks for this thread 1899112061Sjhb * and "fix" this. 1900112061Sjhb * 1901112061Sjhb * That still wouldn't really fix this unless we locked sched_lock 1902112061Sjhb * or stopped the other CPU to make sure it wasn't changing the list 1903112061Sjhb * out from under us. It is probably best to just not try to handle 1904112061Sjhb * threads on other CPU's for now. 1905112061Sjhb */ 1906112061Sjhb if (td == curthread && PCPU_GET(spinlocks) != NULL) 1907112061Sjhb witness_list_locks(PCPU_PTR(spinlocks)); 1908112061Sjhb} 1909112061Sjhb 191074930SjhbDB_SHOW_COMMAND(locks, db_witness_list) 191174912Sjhb{ 191283366Sjulian struct thread *td; 191383366Sjulian pid_t pid; 191475273Sjhb struct proc *p; 191574912Sjhb 191675273Sjhb if (have_addr) { 191775273Sjhb pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 191875273Sjhb ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 191975273Sjhb ((addr >> 16) % 16) * 10000; 192075273Sjhb /* sx_slock(&allproc_lock); */ 192183366Sjulian FOREACH_PROC_IN_SYSTEM(p) { 192275273Sjhb if (p->p_pid == pid) 192375273Sjhb break; 192475273Sjhb } 192575273Sjhb /* sx_sunlock(&allproc_lock); */ 192675273Sjhb if (p == NULL) { 192775273Sjhb db_printf("pid %d not found\n", pid); 192875273Sjhb return; 192975273Sjhb } 193090361Sjulian FOREACH_THREAD_IN_PROC(p, td) { 193190361Sjulian witness_list(td); 193290361Sjulian } 193383366Sjulian } else { 193483366Sjulian td = curthread; 193590361Sjulian witness_list(td); 193683366Sjulian } 193774912Sjhb} 193874912Sjhb 1939139333SrwatsonDB_SHOW_COMMAND(alllocks, db_witness_list_all) 1940139333Srwatson{ 1941139333Srwatson struct thread *td; 1942139333Srwatson struct proc *p; 1943139333Srwatson 1944139333Srwatson /* 1945139333Srwatson * It would be nice to list only threads and processes that actually 1946139333Srwatson * held sleep locks, but that information is currently not exported 1947139333Srwatson * by WITNESS. 1948139333Srwatson */ 1949139333Srwatson FOREACH_PROC_IN_SYSTEM(p) { 1950139333Srwatson if (!witness_proc_has_locks(p)) 1951139333Srwatson continue; 1952139333Srwatson FOREACH_THREAD_IN_PROC(p, td) { 1953139333Srwatson if (!witness_thread_has_locks(td)) 1954139333Srwatson continue; 1955139348Srwatson printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 1956139348Srwatson p->p_comm, td, td->td_tid); 1957139333Srwatson witness_list(td); 1958139333Srwatson } 1959139333Srwatson } 1960139333Srwatson} 1961139333Srwatson 196274912SjhbDB_SHOW_COMMAND(witness, db_witness_display) 196374912Sjhb{ 196474912Sjhb 196574912Sjhb witness_display(db_printf); 196674912Sjhb} 196774912Sjhb#endif 1968