86 87#include "opt_ddb.h" 88#include "opt_witness.h" 89 90#include <sys/param.h> 91#include <sys/bus.h> 92#include <sys/kdb.h> 93#include <sys/kernel.h> 94#include <sys/ktr.h> 95#include <sys/lock.h> 96#include <sys/malloc.h> 97#include <sys/mutex.h> 98#include <sys/proc.h> 99#include <sys/sysctl.h> 100#include <sys/systm.h> 101 102#include <ddb/ddb.h> 103 104#include <machine/stdarg.h> 105 106/* Define this to check for blessed mutexes */ 107#undef BLESSING 108 109#define WITNESS_COUNT 1024 110#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 111/* 112 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 113 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 114 * probably be safe for the most part, but it's still a SWAG. 115 */ 116#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 117 118#define WITNESS_NCHILDREN 6 119 120struct witness_child_list_entry; 121 122struct witness { 123 const char *w_name; 124 struct lock_class *w_class; 125 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 126 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 127 struct witness_child_list_entry *w_children; /* Great evilness... */ 128 const char *w_file; 129 int w_line; 130 u_int w_level; 131 u_int w_refcount; 132 u_char w_Giant_squawked:1; 133 u_char w_other_squawked:1; 134 u_char w_same_squawked:1; 135 u_char w_displayed:1; 136}; 137 138struct witness_child_list_entry { 139 struct witness_child_list_entry *wcl_next; 140 struct witness *wcl_children[WITNESS_NCHILDREN]; 141 u_int wcl_count; 142}; 143 144STAILQ_HEAD(witness_list, witness); 145 146#ifdef BLESSING 147struct witness_blessed { 148 const char *b_lock1; 149 const char *b_lock2; 150}; 151#endif 152 153struct witness_order_list_entry { 154 const char *w_name; 155 struct lock_class *w_class; 156}; 157 158#ifdef BLESSING 159static int blessed(struct witness *, struct witness *); 160#endif 161static int depart(struct witness *w); 162static struct witness *enroll(const char *description, 163 struct lock_class *lock_class); 164static int insertchild(struct witness *parent, struct witness *child); 165static int isitmychild(struct witness *parent, struct witness *child); 166static int isitmydescendant(struct witness *parent, struct witness *child); 167static int itismychild(struct witness *parent, struct witness *child); 168static int rebalancetree(struct witness_list *list); 169static void removechild(struct witness *parent, struct witness *child); 170static int reparentchildren(struct witness *newparent, 171 struct witness *oldparent); 172static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 173static void witness_displaydescendants(void(*)(const char *fmt, ...), 174 struct witness *, int indent); 175static const char *fixup_filename(const char *file); 176static void witness_leveldescendents(struct witness *parent, int level); 177static void witness_levelall(void); 178static struct witness *witness_get(void); 179static void witness_free(struct witness *m); 180static struct witness_child_list_entry *witness_child_get(void); 181static void witness_child_free(struct witness_child_list_entry *wcl); 182static struct lock_list_entry *witness_lock_list_get(void); 183static void witness_lock_list_free(struct lock_list_entry *lle); 184static struct lock_instance *find_instance(struct lock_list_entry *lock_list, 185 struct lock_object *lock); 186static void witness_list_lock(struct lock_instance *instance); 187#ifdef DDB 188static void witness_list(struct thread *td); 189static void witness_display_list(void(*prnt)(const char *fmt, ...), 190 struct witness_list *list); 191static void witness_display(void(*)(const char *fmt, ...)); 192#endif 193 194SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking"); 195 196/* 197 * If set to 0, witness is disabled. If set to 1, witness performs full lock 198 * order checking for all locks. If set to 2 or higher, then witness skips 199 * the full lock order check if the lock being acquired is at a higher level 200 * (i.e. farther down in the tree) than the current lock. This last mode is 201 * somewhat experimental and not considered fully safe. At runtime, this 202 * value may be set to 0 to turn off witness. witness is not allowed be 203 * turned on once it is turned off, however. 204 */ 205static int witness_watch = 1; 206TUNABLE_INT("debug.witness.watch", &witness_watch); 207SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 208 sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 209 210#ifdef KDB 211/* 212 * When KDB is enabled and witness_kdb is set to 1, it will cause the system 213 * to drop into kdebug() when: 214 * - a lock heirarchy violation occurs 215 * - locks are held when going to sleep. 216 */ 217#ifdef WITNESS_KDB 218int witness_kdb = 1; 219#else 220int witness_kdb = 0; 221#endif 222TUNABLE_INT("debug.witness.kdb", &witness_kdb); 223SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 224 225/* 226 * When KDB is enabled and witness_trace is set to 1, it will cause the system 227 * to print a stack trace: 228 * - a lock heirarchy violation occurs 229 * - locks are held when going to sleep. 230 */ 231int witness_trace = 1; 232TUNABLE_INT("debug.witness.trace", &witness_trace); 233SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 234#endif /* KDB */ 235 236#ifdef WITNESS_SKIPSPIN 237int witness_skipspin = 1; 238#else 239int witness_skipspin = 0; 240#endif 241TUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 242SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, 243 &witness_skipspin, 0, ""); 244 245static struct mtx w_mtx; 246static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 247static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 248static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 249static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 250static struct witness_child_list_entry *w_child_free = NULL; 251static struct lock_list_entry *w_lock_list_free = NULL; 252 253static struct witness w_data[WITNESS_COUNT]; 254static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 255static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 256 257static struct witness_order_list_entry order_lists[] = { 258 { "proctree", &lock_class_sx }, 259 { "allproc", &lock_class_sx }, 260 { "Giant", &lock_class_mtx_sleep }, 261 { "filedesc structure", &lock_class_mtx_sleep }, 262 { "pipe mutex", &lock_class_mtx_sleep }, 263 { "sigio lock", &lock_class_mtx_sleep }, 264 { "process group", &lock_class_mtx_sleep }, 265 { "process lock", &lock_class_mtx_sleep }, 266 { "session", &lock_class_mtx_sleep }, 267 { "uidinfo hash", &lock_class_mtx_sleep }, 268 { "uidinfo struct", &lock_class_mtx_sleep }, 269 { "allprison", &lock_class_mtx_sleep }, 270 { NULL, NULL }, 271 /* 272 * Sockets 273 */ 274 { "filedesc structure", &lock_class_mtx_sleep }, 275 { "accept", &lock_class_mtx_sleep }, 276 { "so_snd", &lock_class_mtx_sleep }, 277 { "so_rcv", &lock_class_mtx_sleep }, 278 { "sellck", &lock_class_mtx_sleep }, 279 { NULL, NULL }, 280 /* 281 * Routing 282 */ 283 { "so_rcv", &lock_class_mtx_sleep }, 284 { "radix node head", &lock_class_mtx_sleep }, 285 { "rtentry", &lock_class_mtx_sleep }, 286 { "ifaddr", &lock_class_mtx_sleep }, 287 { NULL, NULL }, 288 /* 289 * UNIX Domain Sockets 290 */ 291 { "unp", &lock_class_mtx_sleep }, 292 { "so_snd", &lock_class_mtx_sleep }, 293 { NULL, NULL }, 294 /* 295 * UDP/IP 296 */ 297 { "udp", &lock_class_mtx_sleep }, 298 { "udpinp", &lock_class_mtx_sleep }, 299 { "so_snd", &lock_class_mtx_sleep }, 300 { NULL, NULL }, 301 /* 302 * TCP/IP 303 */ 304 { "tcp", &lock_class_mtx_sleep }, 305 { "tcpinp", &lock_class_mtx_sleep }, 306 { "so_snd", &lock_class_mtx_sleep }, 307 { NULL, NULL }, 308 /* 309 * SLIP 310 */ 311 { "slip_mtx", &lock_class_mtx_sleep }, 312 { "slip sc_mtx", &lock_class_mtx_sleep }, 313 { NULL, NULL }, 314 /* 315 * netatalk 316 */ 317 { "ddp_list_mtx", &lock_class_mtx_sleep }, 318 { "ddp_mtx", &lock_class_mtx_sleep }, 319 { NULL, NULL }, 320 /* 321 * BPF 322 */ 323 { "bpf global lock", &lock_class_mtx_sleep }, 324 { "bpf interface lock", &lock_class_mtx_sleep }, 325 { "bpf cdev lock", &lock_class_mtx_sleep }, 326 { NULL, NULL }, 327 /* 328 * NFS server 329 */ 330 { "nfsd_mtx", &lock_class_mtx_sleep }, 331 { "so_snd", &lock_class_mtx_sleep }, 332 { NULL, NULL }, 333 /* 334 * CDEV 335 */ 336 { "system map", &lock_class_mtx_sleep }, 337 { "vm page queue mutex", &lock_class_mtx_sleep }, 338 { "vnode interlock", &lock_class_mtx_sleep }, 339 { "cdev", &lock_class_mtx_sleep }, 340 { NULL, NULL }, 341 /* 342 * spin locks 343 */ 344#ifdef SMP 345 { "ap boot", &lock_class_mtx_spin }, 346#endif 347 { "sio", &lock_class_mtx_spin }, 348#ifdef __i386__ 349 { "cy", &lock_class_mtx_spin }, 350#endif 351 { "uart_hwmtx", &lock_class_mtx_spin }, 352 { "sabtty", &lock_class_mtx_spin }, 353 { "zstty", &lock_class_mtx_spin }, 354 { "ng_node", &lock_class_mtx_spin }, 355 { "ng_worklist", &lock_class_mtx_spin }, 356 { "taskqueue_fast", &lock_class_mtx_spin }, 357 { "intr table", &lock_class_mtx_spin }, 358 { "ithread table lock", &lock_class_mtx_spin }, 359 { "sleepq chain", &lock_class_mtx_spin }, 360 { "sched lock", &lock_class_mtx_spin }, 361 { "turnstile chain", &lock_class_mtx_spin }, 362 { "td_contested", &lock_class_mtx_spin }, 363 { "callout", &lock_class_mtx_spin }, 364 { "entropy harvest mutex", &lock_class_mtx_spin }, 365 /* 366 * leaf locks 367 */ 368 { "allpmaps", &lock_class_mtx_spin }, 369 { "vm page queue free mutex", &lock_class_mtx_spin }, 370 { "icu", &lock_class_mtx_spin }, 371#ifdef SMP 372 { "smp rendezvous", &lock_class_mtx_spin }, 373#if defined(__i386__) || defined(__amd64__) 374 { "tlb", &lock_class_mtx_spin }, 375#endif 376#ifdef __sparc64__ 377 { "ipi", &lock_class_mtx_spin },
| 86 87#include "opt_ddb.h" 88#include "opt_witness.h" 89 90#include <sys/param.h> 91#include <sys/bus.h> 92#include <sys/kdb.h> 93#include <sys/kernel.h> 94#include <sys/ktr.h> 95#include <sys/lock.h> 96#include <sys/malloc.h> 97#include <sys/mutex.h> 98#include <sys/proc.h> 99#include <sys/sysctl.h> 100#include <sys/systm.h> 101 102#include <ddb/ddb.h> 103 104#include <machine/stdarg.h> 105 106/* Define this to check for blessed mutexes */ 107#undef BLESSING 108 109#define WITNESS_COUNT 1024 110#define WITNESS_CHILDCOUNT (WITNESS_COUNT * 4) 111/* 112 * XXX: This is somewhat bogus, as we assume here that at most 1024 threads 113 * will hold LOCK_NCHILDREN * 2 locks. We handle failure ok, and we should 114 * probably be safe for the most part, but it's still a SWAG. 115 */ 116#define LOCK_CHILDCOUNT (MAXCPU + 1024) * 2 117 118#define WITNESS_NCHILDREN 6 119 120struct witness_child_list_entry; 121 122struct witness { 123 const char *w_name; 124 struct lock_class *w_class; 125 STAILQ_ENTRY(witness) w_list; /* List of all witnesses. */ 126 STAILQ_ENTRY(witness) w_typelist; /* Witnesses of a type. */ 127 struct witness_child_list_entry *w_children; /* Great evilness... */ 128 const char *w_file; 129 int w_line; 130 u_int w_level; 131 u_int w_refcount; 132 u_char w_Giant_squawked:1; 133 u_char w_other_squawked:1; 134 u_char w_same_squawked:1; 135 u_char w_displayed:1; 136}; 137 138struct witness_child_list_entry { 139 struct witness_child_list_entry *wcl_next; 140 struct witness *wcl_children[WITNESS_NCHILDREN]; 141 u_int wcl_count; 142}; 143 144STAILQ_HEAD(witness_list, witness); 145 146#ifdef BLESSING 147struct witness_blessed { 148 const char *b_lock1; 149 const char *b_lock2; 150}; 151#endif 152 153struct witness_order_list_entry { 154 const char *w_name; 155 struct lock_class *w_class; 156}; 157 158#ifdef BLESSING 159static int blessed(struct witness *, struct witness *); 160#endif 161static int depart(struct witness *w); 162static struct witness *enroll(const char *description, 163 struct lock_class *lock_class); 164static int insertchild(struct witness *parent, struct witness *child); 165static int isitmychild(struct witness *parent, struct witness *child); 166static int isitmydescendant(struct witness *parent, struct witness *child); 167static int itismychild(struct witness *parent, struct witness *child); 168static int rebalancetree(struct witness_list *list); 169static void removechild(struct witness *parent, struct witness *child); 170static int reparentchildren(struct witness *newparent, 171 struct witness *oldparent); 172static int sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS); 173static void witness_displaydescendants(void(*)(const char *fmt, ...), 174 struct witness *, int indent); 175static const char *fixup_filename(const char *file); 176static void witness_leveldescendents(struct witness *parent, int level); 177static void witness_levelall(void); 178static struct witness *witness_get(void); 179static void witness_free(struct witness *m); 180static struct witness_child_list_entry *witness_child_get(void); 181static void witness_child_free(struct witness_child_list_entry *wcl); 182static struct lock_list_entry *witness_lock_list_get(void); 183static void witness_lock_list_free(struct lock_list_entry *lle); 184static struct lock_instance *find_instance(struct lock_list_entry *lock_list, 185 struct lock_object *lock); 186static void witness_list_lock(struct lock_instance *instance); 187#ifdef DDB 188static void witness_list(struct thread *td); 189static void witness_display_list(void(*prnt)(const char *fmt, ...), 190 struct witness_list *list); 191static void witness_display(void(*)(const char *fmt, ...)); 192#endif 193 194SYSCTL_NODE(_debug, OID_AUTO, witness, CTLFLAG_RW, 0, "Witness Locking"); 195 196/* 197 * If set to 0, witness is disabled. If set to 1, witness performs full lock 198 * order checking for all locks. If set to 2 or higher, then witness skips 199 * the full lock order check if the lock being acquired is at a higher level 200 * (i.e. farther down in the tree) than the current lock. This last mode is 201 * somewhat experimental and not considered fully safe. At runtime, this 202 * value may be set to 0 to turn off witness. witness is not allowed be 203 * turned on once it is turned off, however. 204 */ 205static int witness_watch = 1; 206TUNABLE_INT("debug.witness.watch", &witness_watch); 207SYSCTL_PROC(_debug_witness, OID_AUTO, watch, CTLFLAG_RW | CTLTYPE_INT, NULL, 0, 208 sysctl_debug_witness_watch, "I", "witness is watching lock operations"); 209 210#ifdef KDB 211/* 212 * When KDB is enabled and witness_kdb is set to 1, it will cause the system 213 * to drop into kdebug() when: 214 * - a lock heirarchy violation occurs 215 * - locks are held when going to sleep. 216 */ 217#ifdef WITNESS_KDB 218int witness_kdb = 1; 219#else 220int witness_kdb = 0; 221#endif 222TUNABLE_INT("debug.witness.kdb", &witness_kdb); 223SYSCTL_INT(_debug_witness, OID_AUTO, kdb, CTLFLAG_RW, &witness_kdb, 0, ""); 224 225/* 226 * When KDB is enabled and witness_trace is set to 1, it will cause the system 227 * to print a stack trace: 228 * - a lock heirarchy violation occurs 229 * - locks are held when going to sleep. 230 */ 231int witness_trace = 1; 232TUNABLE_INT("debug.witness.trace", &witness_trace); 233SYSCTL_INT(_debug_witness, OID_AUTO, trace, CTLFLAG_RW, &witness_trace, 0, ""); 234#endif /* KDB */ 235 236#ifdef WITNESS_SKIPSPIN 237int witness_skipspin = 1; 238#else 239int witness_skipspin = 0; 240#endif 241TUNABLE_INT("debug.witness.skipspin", &witness_skipspin); 242SYSCTL_INT(_debug_witness, OID_AUTO, skipspin, CTLFLAG_RDTUN, 243 &witness_skipspin, 0, ""); 244 245static struct mtx w_mtx; 246static struct witness_list w_free = STAILQ_HEAD_INITIALIZER(w_free); 247static struct witness_list w_all = STAILQ_HEAD_INITIALIZER(w_all); 248static struct witness_list w_spin = STAILQ_HEAD_INITIALIZER(w_spin); 249static struct witness_list w_sleep = STAILQ_HEAD_INITIALIZER(w_sleep); 250static struct witness_child_list_entry *w_child_free = NULL; 251static struct lock_list_entry *w_lock_list_free = NULL; 252 253static struct witness w_data[WITNESS_COUNT]; 254static struct witness_child_list_entry w_childdata[WITNESS_CHILDCOUNT]; 255static struct lock_list_entry w_locklistdata[LOCK_CHILDCOUNT]; 256 257static struct witness_order_list_entry order_lists[] = { 258 { "proctree", &lock_class_sx }, 259 { "allproc", &lock_class_sx }, 260 { "Giant", &lock_class_mtx_sleep }, 261 { "filedesc structure", &lock_class_mtx_sleep }, 262 { "pipe mutex", &lock_class_mtx_sleep }, 263 { "sigio lock", &lock_class_mtx_sleep }, 264 { "process group", &lock_class_mtx_sleep }, 265 { "process lock", &lock_class_mtx_sleep }, 266 { "session", &lock_class_mtx_sleep }, 267 { "uidinfo hash", &lock_class_mtx_sleep }, 268 { "uidinfo struct", &lock_class_mtx_sleep }, 269 { "allprison", &lock_class_mtx_sleep }, 270 { NULL, NULL }, 271 /* 272 * Sockets 273 */ 274 { "filedesc structure", &lock_class_mtx_sleep }, 275 { "accept", &lock_class_mtx_sleep }, 276 { "so_snd", &lock_class_mtx_sleep }, 277 { "so_rcv", &lock_class_mtx_sleep }, 278 { "sellck", &lock_class_mtx_sleep }, 279 { NULL, NULL }, 280 /* 281 * Routing 282 */ 283 { "so_rcv", &lock_class_mtx_sleep }, 284 { "radix node head", &lock_class_mtx_sleep }, 285 { "rtentry", &lock_class_mtx_sleep }, 286 { "ifaddr", &lock_class_mtx_sleep }, 287 { NULL, NULL }, 288 /* 289 * UNIX Domain Sockets 290 */ 291 { "unp", &lock_class_mtx_sleep }, 292 { "so_snd", &lock_class_mtx_sleep }, 293 { NULL, NULL }, 294 /* 295 * UDP/IP 296 */ 297 { "udp", &lock_class_mtx_sleep }, 298 { "udpinp", &lock_class_mtx_sleep }, 299 { "so_snd", &lock_class_mtx_sleep }, 300 { NULL, NULL }, 301 /* 302 * TCP/IP 303 */ 304 { "tcp", &lock_class_mtx_sleep }, 305 { "tcpinp", &lock_class_mtx_sleep }, 306 { "so_snd", &lock_class_mtx_sleep }, 307 { NULL, NULL }, 308 /* 309 * SLIP 310 */ 311 { "slip_mtx", &lock_class_mtx_sleep }, 312 { "slip sc_mtx", &lock_class_mtx_sleep }, 313 { NULL, NULL }, 314 /* 315 * netatalk 316 */ 317 { "ddp_list_mtx", &lock_class_mtx_sleep }, 318 { "ddp_mtx", &lock_class_mtx_sleep }, 319 { NULL, NULL }, 320 /* 321 * BPF 322 */ 323 { "bpf global lock", &lock_class_mtx_sleep }, 324 { "bpf interface lock", &lock_class_mtx_sleep }, 325 { "bpf cdev lock", &lock_class_mtx_sleep }, 326 { NULL, NULL }, 327 /* 328 * NFS server 329 */ 330 { "nfsd_mtx", &lock_class_mtx_sleep }, 331 { "so_snd", &lock_class_mtx_sleep }, 332 { NULL, NULL }, 333 /* 334 * CDEV 335 */ 336 { "system map", &lock_class_mtx_sleep }, 337 { "vm page queue mutex", &lock_class_mtx_sleep }, 338 { "vnode interlock", &lock_class_mtx_sleep }, 339 { "cdev", &lock_class_mtx_sleep }, 340 { NULL, NULL }, 341 /* 342 * spin locks 343 */ 344#ifdef SMP 345 { "ap boot", &lock_class_mtx_spin }, 346#endif 347 { "sio", &lock_class_mtx_spin }, 348#ifdef __i386__ 349 { "cy", &lock_class_mtx_spin }, 350#endif 351 { "uart_hwmtx", &lock_class_mtx_spin }, 352 { "sabtty", &lock_class_mtx_spin }, 353 { "zstty", &lock_class_mtx_spin }, 354 { "ng_node", &lock_class_mtx_spin }, 355 { "ng_worklist", &lock_class_mtx_spin }, 356 { "taskqueue_fast", &lock_class_mtx_spin }, 357 { "intr table", &lock_class_mtx_spin }, 358 { "ithread table lock", &lock_class_mtx_spin }, 359 { "sleepq chain", &lock_class_mtx_spin }, 360 { "sched lock", &lock_class_mtx_spin }, 361 { "turnstile chain", &lock_class_mtx_spin }, 362 { "td_contested", &lock_class_mtx_spin }, 363 { "callout", &lock_class_mtx_spin }, 364 { "entropy harvest mutex", &lock_class_mtx_spin }, 365 /* 366 * leaf locks 367 */ 368 { "allpmaps", &lock_class_mtx_spin }, 369 { "vm page queue free mutex", &lock_class_mtx_spin }, 370 { "icu", &lock_class_mtx_spin }, 371#ifdef SMP 372 { "smp rendezvous", &lock_class_mtx_spin }, 373#if defined(__i386__) || defined(__amd64__) 374 { "tlb", &lock_class_mtx_spin }, 375#endif 376#ifdef __sparc64__ 377 { "ipi", &lock_class_mtx_spin },
|
378#endif 379#endif 380 { "clk", &lock_class_mtx_spin }, 381 { "mutex profiling lock", &lock_class_mtx_spin }, 382 { "kse zombie lock", &lock_class_mtx_spin }, 383 { "ALD Queue", &lock_class_mtx_spin }, 384#ifdef __ia64__ 385 { "MCA spin lock", &lock_class_mtx_spin }, 386#endif 387#if defined(__i386__) || defined(__amd64__) 388 { "pcicfg", &lock_class_mtx_spin }, 389 { "NDIS thread lock", &lock_class_mtx_spin }, 390#endif 391 { "tw_osl_io_lock", &lock_class_mtx_spin }, 392 { "tw_osl_q_lock", &lock_class_mtx_spin }, 393 { "tw_cl_io_lock", &lock_class_mtx_spin }, 394 { "tw_cl_intr_lock", &lock_class_mtx_spin }, 395 { "tw_cl_gen_lock", &lock_class_mtx_spin }, 396 { NULL, NULL }, 397 { NULL, NULL } 398}; 399 400#ifdef BLESSING 401/* 402 * Pairs of locks which have been blessed 403 * Don't complain about order problems with blessed locks 404 */ 405static struct witness_blessed blessed_list[] = { 406}; 407static int blessed_count = 408 sizeof(blessed_list) / sizeof(struct witness_blessed); 409#endif 410 411/* 412 * List of all locks in the system. 413 */ 414TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 415 416static struct mtx all_mtx = { 417 { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 418 "All locks list", /* mtx_object.lo_name */ 419 "All locks list", /* mtx_object.lo_type */ 420 LO_INITIALIZED, /* mtx_object.lo_flags */ 421 { NULL, NULL }, /* mtx_object.lo_list */ 422 NULL }, /* mtx_object.lo_witness */ 423 MTX_UNOWNED, 0 /* mtx_lock, mtx_recurse */ 424}; 425 426/* 427 * This global is set to 0 once it becomes safe to use the witness code. 428 */ 429static int witness_cold = 1; 430 431/* 432 * Global variables for book keeping. 433 */ 434static int lock_cur_cnt; 435static int lock_max_cnt; 436 437/* 438 * The WITNESS-enabled diagnostic code. 439 */ 440static void 441witness_initialize(void *dummy __unused) 442{ 443 struct lock_object *lock; 444 struct witness_order_list_entry *order; 445 struct witness *w, *w1; 446 int i; 447 448 /* 449 * We have to release Giant before initializing its witness 450 * structure so that WITNESS doesn't get confused. 451 */ 452 mtx_unlock(&Giant); 453 mtx_assert(&Giant, MA_NOTOWNED); 454 455 CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 456 TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 457 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 458 MTX_NOWITNESS); 459 for (i = 0; i < WITNESS_COUNT; i++) 460 witness_free(&w_data[i]); 461 for (i = 0; i < WITNESS_CHILDCOUNT; i++) 462 witness_child_free(&w_childdata[i]); 463 for (i = 0; i < LOCK_CHILDCOUNT; i++) 464 witness_lock_list_free(&w_locklistdata[i]); 465 466 /* First add in all the specified order lists. */ 467 for (order = order_lists; order->w_name != NULL; order++) { 468 w = enroll(order->w_name, order->w_class); 469 if (w == NULL) 470 continue; 471 w->w_file = "order list"; 472 for (order++; order->w_name != NULL; order++) { 473 w1 = enroll(order->w_name, order->w_class); 474 if (w1 == NULL) 475 continue; 476 w1->w_file = "order list"; 477 if (!itismychild(w, w1)) 478 panic("Not enough memory for static orders!"); 479 w = w1; 480 } 481 } 482 483 /* Iterate through all locks and add them to witness. */ 484 mtx_lock(&all_mtx); 485 TAILQ_FOREACH(lock, &all_locks, lo_list) { 486 if (lock->lo_flags & LO_WITNESS) 487 lock->lo_witness = enroll(lock->lo_type, 488 lock->lo_class); 489 else 490 lock->lo_witness = NULL; 491 } 492 mtx_unlock(&all_mtx); 493 494 /* Mark the witness code as being ready for use. */ 495 atomic_store_rel_int(&witness_cold, 0); 496 497 mtx_lock(&Giant); 498} 499SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 500 501static int 502sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 503{ 504 int error, value; 505 506 value = witness_watch; 507 error = sysctl_handle_int(oidp, &value, 0, req); 508 if (error != 0 || req->newptr == NULL) 509 return (error); 510 error = suser(req->td); 511 if (error != 0) 512 return (error); 513 if (value == witness_watch) 514 return (0); 515 if (value != 0) 516 return (EINVAL); 517 witness_watch = 0; 518 return (0); 519} 520 521void 522witness_init(struct lock_object *lock) 523{ 524 struct lock_class *class; 525 526 class = lock->lo_class; 527 if (lock->lo_flags & LO_INITIALIZED) 528 panic("%s: lock (%s) %s is already initialized", __func__, 529 class->lc_name, lock->lo_name); 530 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 531 (class->lc_flags & LC_RECURSABLE) == 0) 532 panic("%s: lock (%s) %s can not be recursable", __func__, 533 class->lc_name, lock->lo_name); 534 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 535 (class->lc_flags & LC_SLEEPABLE) == 0) 536 panic("%s: lock (%s) %s can not be sleepable", __func__, 537 class->lc_name, lock->lo_name); 538 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 539 (class->lc_flags & LC_UPGRADABLE) == 0) 540 panic("%s: lock (%s) %s can not be upgradable", __func__, 541 class->lc_name, lock->lo_name); 542 543 mtx_lock(&all_mtx); 544 TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 545 lock->lo_flags |= LO_INITIALIZED; 546 lock_cur_cnt++; 547 if (lock_cur_cnt > lock_max_cnt) 548 lock_max_cnt = lock_cur_cnt; 549 mtx_unlock(&all_mtx); 550 if (!witness_cold && witness_watch != 0 && panicstr == NULL && 551 (lock->lo_flags & LO_WITNESS) != 0) 552 lock->lo_witness = enroll(lock->lo_type, class); 553 else 554 lock->lo_witness = NULL; 555} 556 557void 558witness_destroy(struct lock_object *lock) 559{ 560 struct witness *w; 561 562 if (witness_cold) 563 panic("lock (%s) %s destroyed while witness_cold", 564 lock->lo_class->lc_name, lock->lo_name); 565 if ((lock->lo_flags & LO_INITIALIZED) == 0) 566 panic("%s: lock (%s) %s is not initialized", __func__, 567 lock->lo_class->lc_name, lock->lo_name); 568 569 /* XXX: need to verify that no one holds the lock */ 570 w = lock->lo_witness; 571 if (w != NULL) { 572 mtx_lock_spin(&w_mtx); 573 MPASS(w->w_refcount > 0); 574 w->w_refcount--; 575 576 /* 577 * Lock is already released if we have an allocation failure 578 * and depart() fails. 579 */ 580 if (w->w_refcount != 0 || depart(w)) 581 mtx_unlock_spin(&w_mtx); 582 } 583 584 mtx_lock(&all_mtx); 585 lock_cur_cnt--; 586 TAILQ_REMOVE(&all_locks, lock, lo_list); 587 lock->lo_flags &= ~LO_INITIALIZED; 588 mtx_unlock(&all_mtx); 589} 590 591#ifdef DDB 592static void 593witness_display_list(void(*prnt)(const char *fmt, ...), 594 struct witness_list *list) 595{ 596 struct witness *w; 597 598 STAILQ_FOREACH(w, list, w_typelist) { 599 if (w->w_file == NULL || w->w_level > 0) 600 continue; 601 /* 602 * This lock has no anscestors, display its descendants. 603 */ 604 witness_displaydescendants(prnt, w, 0); 605 } 606} 607 608static void 609witness_display(void(*prnt)(const char *fmt, ...)) 610{ 611 struct witness *w; 612 613 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 614 witness_levelall(); 615 616 /* Clear all the displayed flags. */ 617 STAILQ_FOREACH(w, &w_all, w_list) { 618 w->w_displayed = 0; 619 } 620 621 /* 622 * First, handle sleep locks which have been acquired at least 623 * once. 624 */ 625 prnt("Sleep locks:\n"); 626 witness_display_list(prnt, &w_sleep); 627 628 /* 629 * Now do spin locks which have been acquired at least once. 630 */ 631 prnt("\nSpin locks:\n"); 632 witness_display_list(prnt, &w_spin); 633 634 /* 635 * Finally, any locks which have not been acquired yet. 636 */ 637 prnt("\nLocks which were never acquired:\n"); 638 STAILQ_FOREACH(w, &w_all, w_list) { 639 if (w->w_file != NULL || w->w_refcount == 0) 640 continue; 641 prnt("%s\n", w->w_name); 642 } 643} 644#endif /* DDB */ 645 646/* Trim useless garbage from filenames. */ 647static const char * 648fixup_filename(const char *file) 649{ 650 651 if (file == NULL) 652 return (NULL); 653 while (strncmp(file, "../", 3) == 0) 654 file += 3; 655 return (file); 656} 657 658int 659witness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 660{ 661 662 if (witness_watch == 0 || panicstr != NULL) 663 return (0); 664 665 /* Require locks that witness knows about. */ 666 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 667 lock2->lo_witness == NULL) 668 return (EINVAL); 669 670 MPASS(!mtx_owned(&w_mtx)); 671 mtx_lock_spin(&w_mtx); 672 673 /* 674 * If we already have either an explicit or implied lock order that 675 * is the other way around, then return an error. 676 */ 677 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 678 mtx_unlock_spin(&w_mtx); 679 return (EDOOFUS); 680 } 681 682 /* Try to add the new order. */ 683 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 684 lock2->lo_type, lock1->lo_type); 685 if (!itismychild(lock1->lo_witness, lock2->lo_witness)) 686 return (ENOMEM); 687 mtx_unlock_spin(&w_mtx); 688 return (0); 689} 690 691void 692witness_checkorder(struct lock_object *lock, int flags, const char *file, 693 int line) 694{ 695 struct lock_list_entry **lock_list, *lle; 696 struct lock_instance *lock1, *lock2; 697 struct lock_class *class; 698 struct witness *w, *w1; 699 struct thread *td; 700 int i, j; 701 702 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 703 panicstr != NULL) 704 return; 705 706 /* 707 * Try locks do not block if they fail to acquire the lock, thus 708 * there is no danger of deadlocks or of switching while holding a 709 * spin lock if we acquire a lock via a try operation. This 710 * function shouldn't even be called for try locks, so panic if 711 * that happens. 712 */ 713 if (flags & LOP_TRYLOCK) 714 panic("%s should not be called for try lock operations", 715 __func__); 716 717 w = lock->lo_witness; 718 class = lock->lo_class; 719 td = curthread; 720 file = fixup_filename(file); 721 722 if (class->lc_flags & LC_SLEEPLOCK) { 723 /* 724 * Since spin locks include a critical section, this check 725 * implicitly enforces a lock order of all sleep locks before 726 * all spin locks. 727 */ 728 if (td->td_critnest != 0 && !kdb_active) 729 panic("blockable sleep lock (%s) %s @ %s:%d", 730 class->lc_name, lock->lo_name, file, line); 731 732 /* 733 * If this is the first lock acquired then just return as 734 * no order checking is needed. 735 */ 736 if (td->td_sleeplocks == NULL) 737 return; 738 lock_list = &td->td_sleeplocks; 739 } else { 740 /* 741 * If this is the first lock, just return as no order 742 * checking is needed. We check this in both if clauses 743 * here as unifying the check would require us to use a 744 * critical section to ensure we don't migrate while doing 745 * the check. Note that if this is not the first lock, we 746 * are already in a critical section and are safe for the 747 * rest of the check. 748 */ 749 if (PCPU_GET(spinlocks) == NULL) 750 return; 751 lock_list = PCPU_PTR(spinlocks); 752 } 753 754 /* 755 * Check to see if we are recursing on a lock we already own. If 756 * so, make sure that we don't mismatch exclusive and shared lock 757 * acquires. 758 */ 759 lock1 = find_instance(*lock_list, lock); 760 if (lock1 != NULL) { 761 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 762 (flags & LOP_EXCLUSIVE) == 0) { 763 printf("shared lock of (%s) %s @ %s:%d\n", 764 class->lc_name, lock->lo_name, file, line); 765 printf("while exclusively locked from %s:%d\n", 766 lock1->li_file, lock1->li_line); 767 panic("share->excl"); 768 } 769 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 770 (flags & LOP_EXCLUSIVE) != 0) { 771 printf("exclusive lock of (%s) %s @ %s:%d\n", 772 class->lc_name, lock->lo_name, file, line); 773 printf("while share locked from %s:%d\n", 774 lock1->li_file, lock1->li_line); 775 panic("excl->share"); 776 } 777 return; 778 } 779 780 /* 781 * Try locks do not block if they fail to acquire the lock, thus 782 * there is no danger of deadlocks or of switching while holding a 783 * spin lock if we acquire a lock via a try operation. 784 */ 785 if (flags & LOP_TRYLOCK) 786 return; 787 788 /* 789 * Check for duplicate locks of the same type. Note that we only 790 * have to check for this on the last lock we just acquired. Any 791 * other cases will be caught as lock order violations. 792 */ 793 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 794 w1 = lock1->li_lock->lo_witness; 795 if (w1 == w) { 796 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) || 797 (flags & LOP_DUPOK)) 798 return; 799 w->w_same_squawked = 1; 800 printf("acquiring duplicate lock of same type: \"%s\"\n", 801 lock->lo_type); 802 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 803 lock1->li_file, lock1->li_line); 804 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 805#ifdef KDB 806 goto debugger; 807#else 808 return; 809#endif 810 } 811 MPASS(!mtx_owned(&w_mtx)); 812 mtx_lock_spin(&w_mtx); 813 /* 814 * If we have a known higher number just say ok 815 */ 816 if (witness_watch > 1 && w->w_level > w1->w_level) { 817 mtx_unlock_spin(&w_mtx); 818 return; 819 } 820 /* 821 * If we know that the the lock we are acquiring comes after 822 * the lock we most recently acquired in the lock order tree, 823 * then there is no need for any further checks. 824 */ 825 if (isitmydescendant(w1, w)) { 826 mtx_unlock_spin(&w_mtx); 827 return; 828 } 829 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 830 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 831 832 MPASS(j < WITNESS_COUNT); 833 lock1 = &lle->ll_children[i]; 834 w1 = lock1->li_lock->lo_witness; 835 836 /* 837 * If this lock doesn't undergo witness checking, 838 * then skip it. 839 */ 840 if (w1 == NULL) { 841 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 842 ("lock missing witness structure")); 843 continue; 844 } 845 /* 846 * If we are locking Giant and this is a sleepable 847 * lock, then skip it. 848 */ 849 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 850 lock == &Giant.mtx_object) 851 continue; 852 /* 853 * If we are locking a sleepable lock and this lock 854 * is Giant, then skip it. 855 */ 856 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 857 lock1->li_lock == &Giant.mtx_object) 858 continue; 859 /* 860 * If we are locking a sleepable lock and this lock 861 * isn't sleepable, we want to treat it as a lock 862 * order violation to enfore a general lock order of 863 * sleepable locks before non-sleepable locks. 864 */ 865 if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 866 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 867 /* 868 * Check the lock order hierarchy for a reveresal. 869 */ 870 if (!isitmydescendant(w, w1)) 871 continue; 872 /* 873 * We have a lock order violation, check to see if it 874 * is allowed or has already been yelled about. 875 */ 876 mtx_unlock_spin(&w_mtx); 877#ifdef BLESSING 878 /* 879 * If the lock order is blessed, just bail. We don't 880 * look for other lock order violations though, which 881 * may be a bug. 882 */ 883 if (blessed(w, w1)) 884 return; 885#endif 886 if (lock1->li_lock == &Giant.mtx_object) { 887 if (w1->w_Giant_squawked) 888 return; 889 else 890 w1->w_Giant_squawked = 1; 891 } else { 892 if (w1->w_other_squawked) 893 return; 894 else 895 w1->w_other_squawked = 1; 896 } 897 /* 898 * Ok, yell about it. 899 */ 900 printf("lock order reversal\n"); 901 /* 902 * Try to locate an earlier lock with 903 * witness w in our list. 904 */ 905 do { 906 lock2 = &lle->ll_children[i]; 907 MPASS(lock2->li_lock != NULL); 908 if (lock2->li_lock->lo_witness == w) 909 break; 910 if (i == 0 && lle->ll_next != NULL) { 911 lle = lle->ll_next; 912 i = lle->ll_count - 1; 913 MPASS(i >= 0 && i < LOCK_NCHILDREN); 914 } else 915 i--; 916 } while (i >= 0); 917 if (i < 0) { 918 printf(" 1st %p %s (%s) @ %s:%d\n", 919 lock1->li_lock, lock1->li_lock->lo_name, 920 lock1->li_lock->lo_type, lock1->li_file, 921 lock1->li_line); 922 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 923 lock->lo_name, lock->lo_type, file, line); 924 } else { 925 printf(" 1st %p %s (%s) @ %s:%d\n", 926 lock2->li_lock, lock2->li_lock->lo_name, 927 lock2->li_lock->lo_type, lock2->li_file, 928 lock2->li_line); 929 printf(" 2nd %p %s (%s) @ %s:%d\n", 930 lock1->li_lock, lock1->li_lock->lo_name, 931 lock1->li_lock->lo_type, lock1->li_file, 932 lock1->li_line); 933 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 934 lock->lo_name, lock->lo_type, file, line); 935 } 936#ifdef KDB 937 goto debugger; 938#else 939 return; 940#endif 941 } 942 } 943 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 944 /* 945 * If requested, build a new lock order. However, don't build a new 946 * relationship between a sleepable lock and Giant if it is in the 947 * wrong direction. The correct lock order is that sleepable locks 948 * always come before Giant. 949 */ 950 if (flags & LOP_NEWORDER && 951 !(lock1->li_lock == &Giant.mtx_object && 952 (lock->lo_flags & LO_SLEEPABLE) != 0)) { 953 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 954 lock->lo_type, lock1->li_lock->lo_type); 955 if (!itismychild(lock1->li_lock->lo_witness, w)) 956 /* Witness is dead. */ 957 return; 958 } 959 mtx_unlock_spin(&w_mtx); 960 return; 961 962#ifdef KDB 963debugger: 964 if (witness_trace) 965 kdb_backtrace(); 966 if (witness_kdb) 967 kdb_enter(__func__); 968#endif 969} 970 971void 972witness_lock(struct lock_object *lock, int flags, const char *file, int line) 973{ 974 struct lock_list_entry **lock_list, *lle; 975 struct lock_instance *instance; 976 struct witness *w; 977 struct thread *td; 978 979 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 980 panicstr != NULL) 981 return; 982 w = lock->lo_witness; 983 td = curthread; 984 file = fixup_filename(file); 985 986 /* Determine lock list for this lock. */ 987 if (lock->lo_class->lc_flags & LC_SLEEPLOCK) 988 lock_list = &td->td_sleeplocks; 989 else 990 lock_list = PCPU_PTR(spinlocks); 991 992 /* Check to see if we are recursing on a lock we already own. */ 993 instance = find_instance(*lock_list, lock); 994 if (instance != NULL) { 995 instance->li_flags++; 996 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 997 td->td_proc->p_pid, lock->lo_name, 998 instance->li_flags & LI_RECURSEMASK); 999 instance->li_file = file; 1000 instance->li_line = line; 1001 return; 1002 } 1003 1004 /* Update per-witness last file and line acquire. */ 1005 w->w_file = file; 1006 w->w_line = line; 1007 1008 /* Find the next open lock instance in the list and fill it. */ 1009 lle = *lock_list; 1010 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 1011 lle = witness_lock_list_get(); 1012 if (lle == NULL) 1013 return; 1014 lle->ll_next = *lock_list; 1015 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 1016 td->td_proc->p_pid, lle); 1017 *lock_list = lle; 1018 } 1019 instance = &lle->ll_children[lle->ll_count++]; 1020 instance->li_lock = lock; 1021 instance->li_line = line; 1022 instance->li_file = file; 1023 if ((flags & LOP_EXCLUSIVE) != 0) 1024 instance->li_flags = LI_EXCLUSIVE; 1025 else 1026 instance->li_flags = 0; 1027 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 1028 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 1029} 1030 1031void 1032witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 1033{ 1034 struct lock_instance *instance; 1035 struct lock_class *class; 1036 1037 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1038 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1039 return; 1040 class = lock->lo_class; 1041 file = fixup_filename(file); 1042 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1043 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 1044 class->lc_name, lock->lo_name, file, line); 1045 if ((flags & LOP_TRYLOCK) == 0) 1046 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 1047 lock->lo_name, file, line); 1048 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1049 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 1050 class->lc_name, lock->lo_name, file, line); 1051 instance = find_instance(curthread->td_sleeplocks, lock); 1052 if (instance == NULL) 1053 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 1054 class->lc_name, lock->lo_name, file, line); 1055 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1056 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 1057 class->lc_name, lock->lo_name, file, line); 1058 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1059 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1060 class->lc_name, lock->lo_name, 1061 instance->li_flags & LI_RECURSEMASK, file, line); 1062 instance->li_flags |= LI_EXCLUSIVE; 1063} 1064 1065void 1066witness_downgrade(struct lock_object *lock, int flags, const char *file, 1067 int line) 1068{ 1069 struct lock_instance *instance; 1070 struct lock_class *class; 1071 1072 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1073 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1074 return; 1075 class = lock->lo_class; 1076 file = fixup_filename(file); 1077 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1078 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 1079 class->lc_name, lock->lo_name, file, line); 1080 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1081 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 1082 class->lc_name, lock->lo_name, file, line); 1083 instance = find_instance(curthread->td_sleeplocks, lock); 1084 if (instance == NULL) 1085 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 1086 class->lc_name, lock->lo_name, file, line); 1087 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1088 panic("downgrade of shared lock (%s) %s @ %s:%d", 1089 class->lc_name, lock->lo_name, file, line); 1090 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1091 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1092 class->lc_name, lock->lo_name, 1093 instance->li_flags & LI_RECURSEMASK, file, line); 1094 instance->li_flags &= ~LI_EXCLUSIVE; 1095} 1096 1097void 1098witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 1099{ 1100 struct lock_list_entry **lock_list, *lle; 1101 struct lock_instance *instance; 1102 struct lock_class *class; 1103 struct thread *td; 1104 register_t s; 1105 int i, j; 1106 1107 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1108 panicstr != NULL) 1109 return; 1110 td = curthread; 1111 class = lock->lo_class; 1112 file = fixup_filename(file); 1113 1114 /* Find lock instance associated with this lock. */ 1115 if (class->lc_flags & LC_SLEEPLOCK) 1116 lock_list = &td->td_sleeplocks; 1117 else 1118 lock_list = PCPU_PTR(spinlocks); 1119 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 1120 for (i = 0; i < (*lock_list)->ll_count; i++) { 1121 instance = &(*lock_list)->ll_children[i]; 1122 if (instance->li_lock == lock) 1123 goto found; 1124 } 1125 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 1126 file, line); 1127found: 1128 1129 /* First, check for shared/exclusive mismatches. */ 1130 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 1131 (flags & LOP_EXCLUSIVE) == 0) { 1132 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1133 lock->lo_name, file, line); 1134 printf("while exclusively locked from %s:%d\n", 1135 instance->li_file, instance->li_line); 1136 panic("excl->ushare"); 1137 } 1138 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 1139 (flags & LOP_EXCLUSIVE) != 0) { 1140 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1141 lock->lo_name, file, line); 1142 printf("while share locked from %s:%d\n", instance->li_file, 1143 instance->li_line); 1144 panic("share->uexcl"); 1145 } 1146 1147 /* If we are recursed, unrecurse. */ 1148 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1149 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1150 td->td_proc->p_pid, instance->li_lock->lo_name, 1151 instance->li_flags); 1152 instance->li_flags--; 1153 return; 1154 } 1155 1156 /* Otherwise, remove this item from the list. */ 1157 s = intr_disable(); 1158 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1159 td->td_proc->p_pid, instance->li_lock->lo_name, 1160 (*lock_list)->ll_count - 1); 1161 for (j = i; j < (*lock_list)->ll_count - 1; j++) 1162 (*lock_list)->ll_children[j] = 1163 (*lock_list)->ll_children[j + 1]; 1164 (*lock_list)->ll_count--; 1165 intr_restore(s); 1166 1167 /* If this lock list entry is now empty, free it. */ 1168 if ((*lock_list)->ll_count == 0) { 1169 lle = *lock_list; 1170 *lock_list = lle->ll_next; 1171 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1172 td->td_proc->p_pid, lle); 1173 witness_lock_list_free(lle); 1174 } 1175} 1176 1177/* 1178 * Warn if any locks other than 'lock' are held. Flags can be passed in to 1179 * exempt Giant and sleepable locks from the checks as well. If any 1180 * non-exempt locks are held, then a supplied message is printed to the 1181 * console along with a list of the offending locks. If indicated in the 1182 * flags then a failure results in a panic as well. 1183 */ 1184int 1185witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 1186{ 1187 struct lock_list_entry *lle; 1188 struct lock_instance *lock1; 1189 struct thread *td; 1190 va_list ap; 1191 int i, n; 1192 1193 if (witness_cold || witness_watch == 0 || panicstr != NULL) 1194 return (0); 1195 n = 0; 1196 td = curthread; 1197 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 1198 for (i = lle->ll_count - 1; i >= 0; i--) { 1199 lock1 = &lle->ll_children[i]; 1200 if (lock1->li_lock == lock) 1201 continue; 1202 if (flags & WARN_GIANTOK && 1203 lock1->li_lock == &Giant.mtx_object) 1204 continue; 1205 if (flags & WARN_SLEEPOK && 1206 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 1207 continue; 1208 if (n == 0) { 1209 va_start(ap, fmt); 1210 vprintf(fmt, ap); 1211 va_end(ap); 1212 printf(" with the following"); 1213 if (flags & WARN_SLEEPOK) 1214 printf(" non-sleepable"); 1215 printf(" locks held:\n"); 1216 } 1217 n++; 1218 witness_list_lock(lock1); 1219 } 1220 if (PCPU_GET(spinlocks) != NULL) { 1221 /* 1222 * Since we already hold a spinlock preemption is 1223 * already blocked. 1224 */ 1225 if (n == 0) { 1226 va_start(ap, fmt); 1227 vprintf(fmt, ap); 1228 va_end(ap); 1229 printf(" with the following"); 1230 if (flags & WARN_SLEEPOK) 1231 printf(" non-sleepable"); 1232 printf(" locks held:\n"); 1233 } 1234 n += witness_list_locks(PCPU_PTR(spinlocks)); 1235 } 1236 if (flags & WARN_PANIC && n) 1237 panic("witness_warn"); 1238#ifdef KDB 1239 else if (witness_kdb && n) 1240 kdb_enter(__func__); 1241 else if (witness_trace && n) 1242 kdb_backtrace(); 1243#endif 1244 return (n); 1245} 1246 1247const char * 1248witness_file(struct lock_object *lock) 1249{ 1250 struct witness *w; 1251 1252 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1253 return ("?"); 1254 w = lock->lo_witness; 1255 return (w->w_file); 1256} 1257 1258int 1259witness_line(struct lock_object *lock) 1260{ 1261 struct witness *w; 1262 1263 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1264 return (0); 1265 w = lock->lo_witness; 1266 return (w->w_line); 1267} 1268 1269static struct witness * 1270enroll(const char *description, struct lock_class *lock_class) 1271{ 1272 struct witness *w; 1273 1274 if (witness_watch == 0 || panicstr != NULL) 1275 return (NULL); 1276 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 1277 return (NULL); 1278 mtx_lock_spin(&w_mtx); 1279 STAILQ_FOREACH(w, &w_all, w_list) { 1280 if (w->w_name == description || (w->w_refcount > 0 && 1281 strcmp(description, w->w_name) == 0)) { 1282 w->w_refcount++; 1283 mtx_unlock_spin(&w_mtx); 1284 if (lock_class != w->w_class) 1285 panic( 1286 "lock (%s) %s does not match earlier (%s) lock", 1287 description, lock_class->lc_name, 1288 w->w_class->lc_name); 1289 return (w); 1290 } 1291 } 1292 /* 1293 * This isn't quite right, as witness_cold is still 0 while we 1294 * enroll all the locks initialized before witness_initialize(). 1295 */ 1296 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 1297 mtx_unlock_spin(&w_mtx); 1298 panic("spin lock %s not in order list", description); 1299 } 1300 if ((w = witness_get()) == NULL) 1301 return (NULL); 1302 w->w_name = description; 1303 w->w_class = lock_class; 1304 w->w_refcount = 1; 1305 STAILQ_INSERT_HEAD(&w_all, w, w_list); 1306 if (lock_class->lc_flags & LC_SPINLOCK) 1307 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1308 else if (lock_class->lc_flags & LC_SLEEPLOCK) 1309 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1310 else { 1311 mtx_unlock_spin(&w_mtx); 1312 panic("lock class %s is not sleep or spin", 1313 lock_class->lc_name); 1314 } 1315 mtx_unlock_spin(&w_mtx); 1316 return (w); 1317} 1318 1319/* Don't let the door bang you on the way out... */ 1320static int 1321depart(struct witness *w) 1322{ 1323 struct witness_child_list_entry *wcl, *nwcl; 1324 struct witness_list *list; 1325 struct witness *parent; 1326 1327 MPASS(w->w_refcount == 0); 1328 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1329 list = &w_sleep; 1330 else 1331 list = &w_spin; 1332 /* 1333 * First, we run through the entire tree looking for any 1334 * witnesses that the outgoing witness is a child of. For 1335 * each parent that we find, we reparent all the direct 1336 * children of the outgoing witness to its parent. 1337 */ 1338 STAILQ_FOREACH(parent, list, w_typelist) { 1339 if (!isitmychild(parent, w)) 1340 continue; 1341 removechild(parent, w); 1342 if (!reparentchildren(parent, w)) 1343 return (0); 1344 } 1345 1346 /* 1347 * Now we go through and free up the child list of the 1348 * outgoing witness. 1349 */ 1350 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1351 nwcl = wcl->wcl_next; 1352 witness_child_free(wcl); 1353 } 1354 1355 /* 1356 * Detach from various lists and free. 1357 */ 1358 STAILQ_REMOVE(list, w, witness, w_typelist); 1359 STAILQ_REMOVE(&w_all, w, witness, w_list); 1360 witness_free(w); 1361 1362 /* Finally, fixup the tree. */ 1363 return (rebalancetree(list)); 1364} 1365 1366/* 1367 * Prune an entire lock order tree. We look for cases where a lock 1368 * is now both a descendant and a direct child of a given lock. In 1369 * that case, we want to remove the direct child link from the tree. 1370 * 1371 * Returns false if insertchild() fails. 1372 */ 1373static int 1374rebalancetree(struct witness_list *list) 1375{ 1376 struct witness *child, *parent; 1377 1378 STAILQ_FOREACH(child, list, w_typelist) { 1379 STAILQ_FOREACH(parent, list, w_typelist) { 1380 if (!isitmychild(parent, child)) 1381 continue; 1382 removechild(parent, child); 1383 if (isitmydescendant(parent, child)) 1384 continue; 1385 if (!insertchild(parent, child)) 1386 return (0); 1387 } 1388 } 1389 witness_levelall(); 1390 return (1); 1391} 1392 1393/* 1394 * Add "child" as a direct child of "parent". Returns false if 1395 * we fail due to out of memory. 1396 */ 1397static int 1398insertchild(struct witness *parent, struct witness *child) 1399{ 1400 struct witness_child_list_entry **wcl; 1401 1402 MPASS(child != NULL && parent != NULL); 1403 1404 /* 1405 * Insert "child" after "parent" 1406 */ 1407 wcl = &parent->w_children; 1408 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 1409 wcl = &(*wcl)->wcl_next; 1410 if (*wcl == NULL) { 1411 *wcl = witness_child_get(); 1412 if (*wcl == NULL) 1413 return (0); 1414 } 1415 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1416 1417 return (1); 1418} 1419 1420/* 1421 * Make all the direct descendants of oldparent be direct descendants 1422 * of newparent. 1423 */ 1424static int 1425reparentchildren(struct witness *newparent, struct witness *oldparent) 1426{ 1427 struct witness_child_list_entry *wcl; 1428 int i; 1429 1430 /* Avoid making a witness a child of itself. */ 1431 MPASS(!isitmychild(oldparent, newparent)); 1432 1433 for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1434 for (i = 0; i < wcl->wcl_count; i++) 1435 if (!insertchild(newparent, wcl->wcl_children[i])) 1436 return (0); 1437 return (1); 1438} 1439 1440static int 1441itismychild(struct witness *parent, struct witness *child) 1442{ 1443 struct witness_list *list; 1444 1445 MPASS(child != NULL && parent != NULL); 1446 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1447 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1448 panic( 1449 "%s: parent (%s) and child (%s) are not the same lock type", 1450 __func__, parent->w_class->lc_name, 1451 child->w_class->lc_name); 1452 1453 if (!insertchild(parent, child)) 1454 return (0); 1455 1456 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1457 list = &w_sleep; 1458 else 1459 list = &w_spin; 1460 return (rebalancetree(list)); 1461} 1462 1463static void 1464removechild(struct witness *parent, struct witness *child) 1465{ 1466 struct witness_child_list_entry **wcl, *wcl1; 1467 int i; 1468 1469 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1470 for (i = 0; i < (*wcl)->wcl_count; i++) 1471 if ((*wcl)->wcl_children[i] == child) 1472 goto found; 1473 return; 1474found: 1475 (*wcl)->wcl_count--; 1476 if ((*wcl)->wcl_count > i) 1477 (*wcl)->wcl_children[i] = 1478 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1479 MPASS((*wcl)->wcl_children[i] != NULL); 1480 if ((*wcl)->wcl_count != 0) 1481 return; 1482 wcl1 = *wcl; 1483 *wcl = wcl1->wcl_next; 1484 witness_child_free(wcl1); 1485} 1486 1487static int 1488isitmychild(struct witness *parent, struct witness *child) 1489{ 1490 struct witness_child_list_entry *wcl; 1491 int i; 1492 1493 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1494 for (i = 0; i < wcl->wcl_count; i++) { 1495 if (wcl->wcl_children[i] == child) 1496 return (1); 1497 } 1498 } 1499 return (0); 1500} 1501 1502static int 1503isitmydescendant(struct witness *parent, struct witness *child) 1504{ 1505 struct witness_child_list_entry *wcl; 1506 int i, j; 1507 1508 if (isitmychild(parent, child)) 1509 return (1); 1510 j = 0; 1511 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1512 MPASS(j < 1000); 1513 for (i = 0; i < wcl->wcl_count; i++) { 1514 if (isitmydescendant(wcl->wcl_children[i], child)) 1515 return (1); 1516 } 1517 j++; 1518 } 1519 return (0); 1520} 1521 1522static void 1523witness_levelall (void) 1524{ 1525 struct witness_list *list; 1526 struct witness *w, *w1; 1527 1528 /* 1529 * First clear all levels. 1530 */ 1531 STAILQ_FOREACH(w, &w_all, w_list) { 1532 w->w_level = 0; 1533 } 1534 1535 /* 1536 * Look for locks with no parent and level all their descendants. 1537 */ 1538 STAILQ_FOREACH(w, &w_all, w_list) { 1539 /* 1540 * This is just an optimization, technically we could get 1541 * away just walking the all list each time. 1542 */ 1543 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1544 list = &w_sleep; 1545 else 1546 list = &w_spin; 1547 STAILQ_FOREACH(w1, list, w_typelist) { 1548 if (isitmychild(w1, w)) 1549 goto skip; 1550 } 1551 witness_leveldescendents(w, 0); 1552 skip: 1553 ; /* silence GCC 3.x */ 1554 } 1555} 1556 1557static void 1558witness_leveldescendents(struct witness *parent, int level) 1559{ 1560 struct witness_child_list_entry *wcl; 1561 int i; 1562 1563 if (parent->w_level < level) 1564 parent->w_level = level; 1565 level++; 1566 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1567 for (i = 0; i < wcl->wcl_count; i++) 1568 witness_leveldescendents(wcl->wcl_children[i], level); 1569} 1570 1571static void 1572witness_displaydescendants(void(*prnt)(const char *fmt, ...), 1573 struct witness *parent, int indent) 1574{ 1575 struct witness_child_list_entry *wcl; 1576 int i, level; 1577 1578 level = parent->w_level; 1579 prnt("%-2d", level); 1580 for (i = 0; i < indent; i++) 1581 prnt(" "); 1582 if (parent->w_refcount > 0) 1583 prnt("%s", parent->w_name); 1584 else 1585 prnt("(dead)"); 1586 if (parent->w_displayed) { 1587 prnt(" -- (already displayed)\n"); 1588 return; 1589 } 1590 parent->w_displayed = 1; 1591 if (parent->w_refcount > 0) { 1592 if (parent->w_file != NULL) 1593 prnt(" -- last acquired @ %s:%d", parent->w_file, 1594 parent->w_line); 1595 } 1596 prnt("\n"); 1597 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1598 for (i = 0; i < wcl->wcl_count; i++) 1599 witness_displaydescendants(prnt, 1600 wcl->wcl_children[i], indent + 1); 1601} 1602 1603#ifdef BLESSING 1604static int 1605blessed(struct witness *w1, struct witness *w2) 1606{ 1607 int i; 1608 struct witness_blessed *b; 1609 1610 for (i = 0; i < blessed_count; i++) { 1611 b = &blessed_list[i]; 1612 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1613 if (strcmp(w2->w_name, b->b_lock2) == 0) 1614 return (1); 1615 continue; 1616 } 1617 if (strcmp(w1->w_name, b->b_lock2) == 0) 1618 if (strcmp(w2->w_name, b->b_lock1) == 0) 1619 return (1); 1620 } 1621 return (0); 1622} 1623#endif 1624 1625static struct witness * 1626witness_get(void) 1627{ 1628 struct witness *w; 1629 1630 if (witness_watch == 0) { 1631 mtx_unlock_spin(&w_mtx); 1632 return (NULL); 1633 } 1634 if (STAILQ_EMPTY(&w_free)) { 1635 witness_watch = 0; 1636 mtx_unlock_spin(&w_mtx); 1637 printf("%s: witness exhausted\n", __func__); 1638 return (NULL); 1639 } 1640 w = STAILQ_FIRST(&w_free); 1641 STAILQ_REMOVE_HEAD(&w_free, w_list); 1642 bzero(w, sizeof(*w)); 1643 return (w); 1644} 1645 1646static void 1647witness_free(struct witness *w) 1648{ 1649 1650 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1651} 1652 1653static struct witness_child_list_entry * 1654witness_child_get(void) 1655{ 1656 struct witness_child_list_entry *wcl; 1657 1658 if (witness_watch == 0) { 1659 mtx_unlock_spin(&w_mtx); 1660 return (NULL); 1661 } 1662 wcl = w_child_free; 1663 if (wcl == NULL) { 1664 witness_watch = 0; 1665 mtx_unlock_spin(&w_mtx); 1666 printf("%s: witness exhausted\n", __func__); 1667 return (NULL); 1668 } 1669 w_child_free = wcl->wcl_next; 1670 bzero(wcl, sizeof(*wcl)); 1671 return (wcl); 1672} 1673 1674static void 1675witness_child_free(struct witness_child_list_entry *wcl) 1676{ 1677 1678 wcl->wcl_next = w_child_free; 1679 w_child_free = wcl; 1680} 1681 1682static struct lock_list_entry * 1683witness_lock_list_get(void) 1684{ 1685 struct lock_list_entry *lle; 1686 1687 if (witness_watch == 0) 1688 return (NULL); 1689 mtx_lock_spin(&w_mtx); 1690 lle = w_lock_list_free; 1691 if (lle == NULL) { 1692 witness_watch = 0; 1693 mtx_unlock_spin(&w_mtx); 1694 printf("%s: witness exhausted\n", __func__); 1695 return (NULL); 1696 } 1697 w_lock_list_free = lle->ll_next; 1698 mtx_unlock_spin(&w_mtx); 1699 bzero(lle, sizeof(*lle)); 1700 return (lle); 1701} 1702 1703static void 1704witness_lock_list_free(struct lock_list_entry *lle) 1705{ 1706 1707 mtx_lock_spin(&w_mtx); 1708 lle->ll_next = w_lock_list_free; 1709 w_lock_list_free = lle; 1710 mtx_unlock_spin(&w_mtx); 1711} 1712 1713static struct lock_instance * 1714find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1715{ 1716 struct lock_list_entry *lle; 1717 struct lock_instance *instance; 1718 int i; 1719 1720 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1721 for (i = lle->ll_count - 1; i >= 0; i--) { 1722 instance = &lle->ll_children[i]; 1723 if (instance->li_lock == lock) 1724 return (instance); 1725 } 1726 return (NULL); 1727} 1728 1729static void 1730witness_list_lock(struct lock_instance *instance) 1731{ 1732 struct lock_object *lock; 1733 1734 lock = instance->li_lock; 1735 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1736 "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name); 1737 if (lock->lo_type != lock->lo_name) 1738 printf(" (%s)", lock->lo_type); 1739 printf(" r = %d (%p) locked @ %s:%d\n", 1740 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1741 instance->li_line); 1742} 1743 1744#ifdef DDB 1745static int 1746witness_thread_has_locks(struct thread *td) 1747{ 1748 1749 return (td->td_sleeplocks != NULL); 1750} 1751 1752static int 1753witness_proc_has_locks(struct proc *p) 1754{ 1755 struct thread *td; 1756 1757 FOREACH_THREAD_IN_PROC(p, td) { 1758 if (witness_thread_has_locks(td)) 1759 return (1); 1760 } 1761 return (0); 1762} 1763#endif 1764 1765int 1766witness_list_locks(struct lock_list_entry **lock_list) 1767{ 1768 struct lock_list_entry *lle; 1769 int i, nheld; 1770 1771 nheld = 0; 1772 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1773 for (i = lle->ll_count - 1; i >= 0; i--) { 1774 witness_list_lock(&lle->ll_children[i]); 1775 nheld++; 1776 } 1777 return (nheld); 1778} 1779 1780/* 1781 * This is a bit risky at best. We call this function when we have timed 1782 * out acquiring a spin lock, and we assume that the other CPU is stuck 1783 * with this lock held. So, we go groveling around in the other CPU's 1784 * per-cpu data to try to find the lock instance for this spin lock to 1785 * see when it was last acquired. 1786 */ 1787void 1788witness_display_spinlock(struct lock_object *lock, struct thread *owner) 1789{ 1790 struct lock_instance *instance; 1791 struct pcpu *pc; 1792 1793 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 1794 return; 1795 pc = pcpu_find(owner->td_oncpu); 1796 instance = find_instance(pc->pc_spinlocks, lock); 1797 if (instance != NULL) 1798 witness_list_lock(instance); 1799} 1800 1801void 1802witness_save(struct lock_object *lock, const char **filep, int *linep) 1803{ 1804 struct lock_instance *instance; 1805 1806 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1807 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1808 return; 1809 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1810 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1811 lock->lo_class->lc_name, lock->lo_name); 1812 instance = find_instance(curthread->td_sleeplocks, lock); 1813 if (instance == NULL) 1814 panic("%s: lock (%s) %s not locked", __func__, 1815 lock->lo_class->lc_name, lock->lo_name); 1816 *filep = instance->li_file; 1817 *linep = instance->li_line; 1818} 1819 1820void 1821witness_restore(struct lock_object *lock, const char *file, int line) 1822{ 1823 struct lock_instance *instance; 1824 1825 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1826 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1827 return; 1828 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1829 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1830 lock->lo_class->lc_name, lock->lo_name); 1831 instance = find_instance(curthread->td_sleeplocks, lock); 1832 if (instance == NULL) 1833 panic("%s: lock (%s) %s not locked", __func__, 1834 lock->lo_class->lc_name, lock->lo_name); 1835 lock->lo_witness->w_file = file; 1836 lock->lo_witness->w_line = line; 1837 instance->li_file = file; 1838 instance->li_line = line; 1839} 1840 1841void 1842witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1843{ 1844#ifdef INVARIANT_SUPPORT 1845 struct lock_instance *instance; 1846 1847 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1848 return; 1849 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 1850 instance = find_instance(curthread->td_sleeplocks, lock); 1851 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 1852 instance = find_instance(PCPU_GET(spinlocks), lock); 1853 else { 1854 panic("Lock (%s) %s is not sleep or spin!", 1855 lock->lo_class->lc_name, lock->lo_name); 1856 } 1857 file = fixup_filename(file); 1858 switch (flags) { 1859 case LA_UNLOCKED: 1860 if (instance != NULL) 1861 panic("Lock (%s) %s locked @ %s:%d.", 1862 lock->lo_class->lc_name, lock->lo_name, file, line); 1863 break; 1864 case LA_LOCKED: 1865 case LA_LOCKED | LA_RECURSED: 1866 case LA_LOCKED | LA_NOTRECURSED: 1867 case LA_SLOCKED: 1868 case LA_SLOCKED | LA_RECURSED: 1869 case LA_SLOCKED | LA_NOTRECURSED: 1870 case LA_XLOCKED: 1871 case LA_XLOCKED | LA_RECURSED: 1872 case LA_XLOCKED | LA_NOTRECURSED: 1873 if (instance == NULL) { 1874 panic("Lock (%s) %s not locked @ %s:%d.", 1875 lock->lo_class->lc_name, lock->lo_name, file, line); 1876 break; 1877 } 1878 if ((flags & LA_XLOCKED) != 0 && 1879 (instance->li_flags & LI_EXCLUSIVE) == 0) 1880 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1881 lock->lo_class->lc_name, lock->lo_name, file, line); 1882 if ((flags & LA_SLOCKED) != 0 && 1883 (instance->li_flags & LI_EXCLUSIVE) != 0) 1884 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1885 lock->lo_class->lc_name, lock->lo_name, file, line); 1886 if ((flags & LA_RECURSED) != 0 && 1887 (instance->li_flags & LI_RECURSEMASK) == 0) 1888 panic("Lock (%s) %s not recursed @ %s:%d.", 1889 lock->lo_class->lc_name, lock->lo_name, file, line); 1890 if ((flags & LA_NOTRECURSED) != 0 && 1891 (instance->li_flags & LI_RECURSEMASK) != 0) 1892 panic("Lock (%s) %s recursed @ %s:%d.", 1893 lock->lo_class->lc_name, lock->lo_name, file, line); 1894 break; 1895 default: 1896 panic("Invalid lock assertion at %s:%d.", file, line); 1897 1898 } 1899#endif /* INVARIANT_SUPPORT */ 1900} 1901 1902#ifdef DDB 1903static void 1904witness_list(struct thread *td) 1905{ 1906 1907 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1908 KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 1909 1910 if (witness_watch == 0) 1911 return; 1912 1913 witness_list_locks(&td->td_sleeplocks); 1914 1915 /* 1916 * We only handle spinlocks if td == curthread. This is somewhat broken 1917 * if td is currently executing on some other CPU and holds spin locks 1918 * as we won't display those locks. If we had a MI way of getting 1919 * the per-cpu data for a given cpu then we could use 1920 * td->td_oncpu to get the list of spinlocks for this thread 1921 * and "fix" this. 1922 * 1923 * That still wouldn't really fix this unless we locked sched_lock 1924 * or stopped the other CPU to make sure it wasn't changing the list 1925 * out from under us. It is probably best to just not try to handle 1926 * threads on other CPU's for now. 1927 */ 1928 if (td == curthread && PCPU_GET(spinlocks) != NULL) 1929 witness_list_locks(PCPU_PTR(spinlocks)); 1930} 1931 1932DB_SHOW_COMMAND(locks, db_witness_list) 1933{ 1934 struct thread *td; 1935 pid_t pid; 1936 struct proc *p; 1937 1938 if (have_addr) { 1939 pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 1940 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 1941 ((addr >> 16) % 16) * 10000; 1942 /* sx_slock(&allproc_lock); */ 1943 FOREACH_PROC_IN_SYSTEM(p) { 1944 if (p->p_pid == pid) 1945 break; 1946 } 1947 /* sx_sunlock(&allproc_lock); */ 1948 if (p == NULL) { 1949 db_printf("pid %d not found\n", pid); 1950 return; 1951 } 1952 FOREACH_THREAD_IN_PROC(p, td) { 1953 witness_list(td); 1954 } 1955 } else { 1956 td = curthread; 1957 witness_list(td); 1958 } 1959} 1960 1961DB_SHOW_COMMAND(alllocks, db_witness_list_all) 1962{ 1963 struct thread *td; 1964 struct proc *p; 1965 1966 /* 1967 * It would be nice to list only threads and processes that actually 1968 * held sleep locks, but that information is currently not exported 1969 * by WITNESS. 1970 */ 1971 FOREACH_PROC_IN_SYSTEM(p) { 1972 if (!witness_proc_has_locks(p)) 1973 continue; 1974 FOREACH_THREAD_IN_PROC(p, td) { 1975 if (!witness_thread_has_locks(td)) 1976 continue; 1977 printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 1978 p->p_comm, td, td->td_tid); 1979 witness_list(td); 1980 } 1981 } 1982} 1983 1984DB_SHOW_COMMAND(witness, db_witness_display) 1985{ 1986 1987 witness_display(db_printf); 1988} 1989#endif
| 379#endif 380#endif 381 { "clk", &lock_class_mtx_spin }, 382 { "mutex profiling lock", &lock_class_mtx_spin }, 383 { "kse zombie lock", &lock_class_mtx_spin }, 384 { "ALD Queue", &lock_class_mtx_spin }, 385#ifdef __ia64__ 386 { "MCA spin lock", &lock_class_mtx_spin }, 387#endif 388#if defined(__i386__) || defined(__amd64__) 389 { "pcicfg", &lock_class_mtx_spin }, 390 { "NDIS thread lock", &lock_class_mtx_spin }, 391#endif 392 { "tw_osl_io_lock", &lock_class_mtx_spin }, 393 { "tw_osl_q_lock", &lock_class_mtx_spin }, 394 { "tw_cl_io_lock", &lock_class_mtx_spin }, 395 { "tw_cl_intr_lock", &lock_class_mtx_spin }, 396 { "tw_cl_gen_lock", &lock_class_mtx_spin }, 397 { NULL, NULL }, 398 { NULL, NULL } 399}; 400 401#ifdef BLESSING 402/* 403 * Pairs of locks which have been blessed 404 * Don't complain about order problems with blessed locks 405 */ 406static struct witness_blessed blessed_list[] = { 407}; 408static int blessed_count = 409 sizeof(blessed_list) / sizeof(struct witness_blessed); 410#endif 411 412/* 413 * List of all locks in the system. 414 */ 415TAILQ_HEAD(, lock_object) all_locks = TAILQ_HEAD_INITIALIZER(all_locks); 416 417static struct mtx all_mtx = { 418 { &lock_class_mtx_sleep, /* mtx_object.lo_class */ 419 "All locks list", /* mtx_object.lo_name */ 420 "All locks list", /* mtx_object.lo_type */ 421 LO_INITIALIZED, /* mtx_object.lo_flags */ 422 { NULL, NULL }, /* mtx_object.lo_list */ 423 NULL }, /* mtx_object.lo_witness */ 424 MTX_UNOWNED, 0 /* mtx_lock, mtx_recurse */ 425}; 426 427/* 428 * This global is set to 0 once it becomes safe to use the witness code. 429 */ 430static int witness_cold = 1; 431 432/* 433 * Global variables for book keeping. 434 */ 435static int lock_cur_cnt; 436static int lock_max_cnt; 437 438/* 439 * The WITNESS-enabled diagnostic code. 440 */ 441static void 442witness_initialize(void *dummy __unused) 443{ 444 struct lock_object *lock; 445 struct witness_order_list_entry *order; 446 struct witness *w, *w1; 447 int i; 448 449 /* 450 * We have to release Giant before initializing its witness 451 * structure so that WITNESS doesn't get confused. 452 */ 453 mtx_unlock(&Giant); 454 mtx_assert(&Giant, MA_NOTOWNED); 455 456 CTR1(KTR_WITNESS, "%s: initializing witness", __func__); 457 TAILQ_INSERT_HEAD(&all_locks, &all_mtx.mtx_object, lo_list); 458 mtx_init(&w_mtx, "witness lock", NULL, MTX_SPIN | MTX_QUIET | 459 MTX_NOWITNESS); 460 for (i = 0; i < WITNESS_COUNT; i++) 461 witness_free(&w_data[i]); 462 for (i = 0; i < WITNESS_CHILDCOUNT; i++) 463 witness_child_free(&w_childdata[i]); 464 for (i = 0; i < LOCK_CHILDCOUNT; i++) 465 witness_lock_list_free(&w_locklistdata[i]); 466 467 /* First add in all the specified order lists. */ 468 for (order = order_lists; order->w_name != NULL; order++) { 469 w = enroll(order->w_name, order->w_class); 470 if (w == NULL) 471 continue; 472 w->w_file = "order list"; 473 for (order++; order->w_name != NULL; order++) { 474 w1 = enroll(order->w_name, order->w_class); 475 if (w1 == NULL) 476 continue; 477 w1->w_file = "order list"; 478 if (!itismychild(w, w1)) 479 panic("Not enough memory for static orders!"); 480 w = w1; 481 } 482 } 483 484 /* Iterate through all locks and add them to witness. */ 485 mtx_lock(&all_mtx); 486 TAILQ_FOREACH(lock, &all_locks, lo_list) { 487 if (lock->lo_flags & LO_WITNESS) 488 lock->lo_witness = enroll(lock->lo_type, 489 lock->lo_class); 490 else 491 lock->lo_witness = NULL; 492 } 493 mtx_unlock(&all_mtx); 494 495 /* Mark the witness code as being ready for use. */ 496 atomic_store_rel_int(&witness_cold, 0); 497 498 mtx_lock(&Giant); 499} 500SYSINIT(witness_init, SI_SUB_WITNESS, SI_ORDER_FIRST, witness_initialize, NULL) 501 502static int 503sysctl_debug_witness_watch(SYSCTL_HANDLER_ARGS) 504{ 505 int error, value; 506 507 value = witness_watch; 508 error = sysctl_handle_int(oidp, &value, 0, req); 509 if (error != 0 || req->newptr == NULL) 510 return (error); 511 error = suser(req->td); 512 if (error != 0) 513 return (error); 514 if (value == witness_watch) 515 return (0); 516 if (value != 0) 517 return (EINVAL); 518 witness_watch = 0; 519 return (0); 520} 521 522void 523witness_init(struct lock_object *lock) 524{ 525 struct lock_class *class; 526 527 class = lock->lo_class; 528 if (lock->lo_flags & LO_INITIALIZED) 529 panic("%s: lock (%s) %s is already initialized", __func__, 530 class->lc_name, lock->lo_name); 531 if ((lock->lo_flags & LO_RECURSABLE) != 0 && 532 (class->lc_flags & LC_RECURSABLE) == 0) 533 panic("%s: lock (%s) %s can not be recursable", __func__, 534 class->lc_name, lock->lo_name); 535 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 536 (class->lc_flags & LC_SLEEPABLE) == 0) 537 panic("%s: lock (%s) %s can not be sleepable", __func__, 538 class->lc_name, lock->lo_name); 539 if ((lock->lo_flags & LO_UPGRADABLE) != 0 && 540 (class->lc_flags & LC_UPGRADABLE) == 0) 541 panic("%s: lock (%s) %s can not be upgradable", __func__, 542 class->lc_name, lock->lo_name); 543 544 mtx_lock(&all_mtx); 545 TAILQ_INSERT_TAIL(&all_locks, lock, lo_list); 546 lock->lo_flags |= LO_INITIALIZED; 547 lock_cur_cnt++; 548 if (lock_cur_cnt > lock_max_cnt) 549 lock_max_cnt = lock_cur_cnt; 550 mtx_unlock(&all_mtx); 551 if (!witness_cold && witness_watch != 0 && panicstr == NULL && 552 (lock->lo_flags & LO_WITNESS) != 0) 553 lock->lo_witness = enroll(lock->lo_type, class); 554 else 555 lock->lo_witness = NULL; 556} 557 558void 559witness_destroy(struct lock_object *lock) 560{ 561 struct witness *w; 562 563 if (witness_cold) 564 panic("lock (%s) %s destroyed while witness_cold", 565 lock->lo_class->lc_name, lock->lo_name); 566 if ((lock->lo_flags & LO_INITIALIZED) == 0) 567 panic("%s: lock (%s) %s is not initialized", __func__, 568 lock->lo_class->lc_name, lock->lo_name); 569 570 /* XXX: need to verify that no one holds the lock */ 571 w = lock->lo_witness; 572 if (w != NULL) { 573 mtx_lock_spin(&w_mtx); 574 MPASS(w->w_refcount > 0); 575 w->w_refcount--; 576 577 /* 578 * Lock is already released if we have an allocation failure 579 * and depart() fails. 580 */ 581 if (w->w_refcount != 0 || depart(w)) 582 mtx_unlock_spin(&w_mtx); 583 } 584 585 mtx_lock(&all_mtx); 586 lock_cur_cnt--; 587 TAILQ_REMOVE(&all_locks, lock, lo_list); 588 lock->lo_flags &= ~LO_INITIALIZED; 589 mtx_unlock(&all_mtx); 590} 591 592#ifdef DDB 593static void 594witness_display_list(void(*prnt)(const char *fmt, ...), 595 struct witness_list *list) 596{ 597 struct witness *w; 598 599 STAILQ_FOREACH(w, list, w_typelist) { 600 if (w->w_file == NULL || w->w_level > 0) 601 continue; 602 /* 603 * This lock has no anscestors, display its descendants. 604 */ 605 witness_displaydescendants(prnt, w, 0); 606 } 607} 608 609static void 610witness_display(void(*prnt)(const char *fmt, ...)) 611{ 612 struct witness *w; 613 614 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 615 witness_levelall(); 616 617 /* Clear all the displayed flags. */ 618 STAILQ_FOREACH(w, &w_all, w_list) { 619 w->w_displayed = 0; 620 } 621 622 /* 623 * First, handle sleep locks which have been acquired at least 624 * once. 625 */ 626 prnt("Sleep locks:\n"); 627 witness_display_list(prnt, &w_sleep); 628 629 /* 630 * Now do spin locks which have been acquired at least once. 631 */ 632 prnt("\nSpin locks:\n"); 633 witness_display_list(prnt, &w_spin); 634 635 /* 636 * Finally, any locks which have not been acquired yet. 637 */ 638 prnt("\nLocks which were never acquired:\n"); 639 STAILQ_FOREACH(w, &w_all, w_list) { 640 if (w->w_file != NULL || w->w_refcount == 0) 641 continue; 642 prnt("%s\n", w->w_name); 643 } 644} 645#endif /* DDB */ 646 647/* Trim useless garbage from filenames. */ 648static const char * 649fixup_filename(const char *file) 650{ 651 652 if (file == NULL) 653 return (NULL); 654 while (strncmp(file, "../", 3) == 0) 655 file += 3; 656 return (file); 657} 658 659int 660witness_defineorder(struct lock_object *lock1, struct lock_object *lock2) 661{ 662 663 if (witness_watch == 0 || panicstr != NULL) 664 return (0); 665 666 /* Require locks that witness knows about. */ 667 if (lock1 == NULL || lock1->lo_witness == NULL || lock2 == NULL || 668 lock2->lo_witness == NULL) 669 return (EINVAL); 670 671 MPASS(!mtx_owned(&w_mtx)); 672 mtx_lock_spin(&w_mtx); 673 674 /* 675 * If we already have either an explicit or implied lock order that 676 * is the other way around, then return an error. 677 */ 678 if (isitmydescendant(lock2->lo_witness, lock1->lo_witness)) { 679 mtx_unlock_spin(&w_mtx); 680 return (EDOOFUS); 681 } 682 683 /* Try to add the new order. */ 684 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 685 lock2->lo_type, lock1->lo_type); 686 if (!itismychild(lock1->lo_witness, lock2->lo_witness)) 687 return (ENOMEM); 688 mtx_unlock_spin(&w_mtx); 689 return (0); 690} 691 692void 693witness_checkorder(struct lock_object *lock, int flags, const char *file, 694 int line) 695{ 696 struct lock_list_entry **lock_list, *lle; 697 struct lock_instance *lock1, *lock2; 698 struct lock_class *class; 699 struct witness *w, *w1; 700 struct thread *td; 701 int i, j; 702 703 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 704 panicstr != NULL) 705 return; 706 707 /* 708 * Try locks do not block if they fail to acquire the lock, thus 709 * there is no danger of deadlocks or of switching while holding a 710 * spin lock if we acquire a lock via a try operation. This 711 * function shouldn't even be called for try locks, so panic if 712 * that happens. 713 */ 714 if (flags & LOP_TRYLOCK) 715 panic("%s should not be called for try lock operations", 716 __func__); 717 718 w = lock->lo_witness; 719 class = lock->lo_class; 720 td = curthread; 721 file = fixup_filename(file); 722 723 if (class->lc_flags & LC_SLEEPLOCK) { 724 /* 725 * Since spin locks include a critical section, this check 726 * implicitly enforces a lock order of all sleep locks before 727 * all spin locks. 728 */ 729 if (td->td_critnest != 0 && !kdb_active) 730 panic("blockable sleep lock (%s) %s @ %s:%d", 731 class->lc_name, lock->lo_name, file, line); 732 733 /* 734 * If this is the first lock acquired then just return as 735 * no order checking is needed. 736 */ 737 if (td->td_sleeplocks == NULL) 738 return; 739 lock_list = &td->td_sleeplocks; 740 } else { 741 /* 742 * If this is the first lock, just return as no order 743 * checking is needed. We check this in both if clauses 744 * here as unifying the check would require us to use a 745 * critical section to ensure we don't migrate while doing 746 * the check. Note that if this is not the first lock, we 747 * are already in a critical section and are safe for the 748 * rest of the check. 749 */ 750 if (PCPU_GET(spinlocks) == NULL) 751 return; 752 lock_list = PCPU_PTR(spinlocks); 753 } 754 755 /* 756 * Check to see if we are recursing on a lock we already own. If 757 * so, make sure that we don't mismatch exclusive and shared lock 758 * acquires. 759 */ 760 lock1 = find_instance(*lock_list, lock); 761 if (lock1 != NULL) { 762 if ((lock1->li_flags & LI_EXCLUSIVE) != 0 && 763 (flags & LOP_EXCLUSIVE) == 0) { 764 printf("shared lock of (%s) %s @ %s:%d\n", 765 class->lc_name, lock->lo_name, file, line); 766 printf("while exclusively locked from %s:%d\n", 767 lock1->li_file, lock1->li_line); 768 panic("share->excl"); 769 } 770 if ((lock1->li_flags & LI_EXCLUSIVE) == 0 && 771 (flags & LOP_EXCLUSIVE) != 0) { 772 printf("exclusive lock of (%s) %s @ %s:%d\n", 773 class->lc_name, lock->lo_name, file, line); 774 printf("while share locked from %s:%d\n", 775 lock1->li_file, lock1->li_line); 776 panic("excl->share"); 777 } 778 return; 779 } 780 781 /* 782 * Try locks do not block if they fail to acquire the lock, thus 783 * there is no danger of deadlocks or of switching while holding a 784 * spin lock if we acquire a lock via a try operation. 785 */ 786 if (flags & LOP_TRYLOCK) 787 return; 788 789 /* 790 * Check for duplicate locks of the same type. Note that we only 791 * have to check for this on the last lock we just acquired. Any 792 * other cases will be caught as lock order violations. 793 */ 794 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 795 w1 = lock1->li_lock->lo_witness; 796 if (w1 == w) { 797 if (w->w_same_squawked || (lock->lo_flags & LO_DUPOK) || 798 (flags & LOP_DUPOK)) 799 return; 800 w->w_same_squawked = 1; 801 printf("acquiring duplicate lock of same type: \"%s\"\n", 802 lock->lo_type); 803 printf(" 1st %s @ %s:%d\n", lock1->li_lock->lo_name, 804 lock1->li_file, lock1->li_line); 805 printf(" 2nd %s @ %s:%d\n", lock->lo_name, file, line); 806#ifdef KDB 807 goto debugger; 808#else 809 return; 810#endif 811 } 812 MPASS(!mtx_owned(&w_mtx)); 813 mtx_lock_spin(&w_mtx); 814 /* 815 * If we have a known higher number just say ok 816 */ 817 if (witness_watch > 1 && w->w_level > w1->w_level) { 818 mtx_unlock_spin(&w_mtx); 819 return; 820 } 821 /* 822 * If we know that the the lock we are acquiring comes after 823 * the lock we most recently acquired in the lock order tree, 824 * then there is no need for any further checks. 825 */ 826 if (isitmydescendant(w1, w)) { 827 mtx_unlock_spin(&w_mtx); 828 return; 829 } 830 for (j = 0, lle = *lock_list; lle != NULL; lle = lle->ll_next) { 831 for (i = lle->ll_count - 1; i >= 0; i--, j++) { 832 833 MPASS(j < WITNESS_COUNT); 834 lock1 = &lle->ll_children[i]; 835 w1 = lock1->li_lock->lo_witness; 836 837 /* 838 * If this lock doesn't undergo witness checking, 839 * then skip it. 840 */ 841 if (w1 == NULL) { 842 KASSERT((lock1->li_lock->lo_flags & LO_WITNESS) == 0, 843 ("lock missing witness structure")); 844 continue; 845 } 846 /* 847 * If we are locking Giant and this is a sleepable 848 * lock, then skip it. 849 */ 850 if ((lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0 && 851 lock == &Giant.mtx_object) 852 continue; 853 /* 854 * If we are locking a sleepable lock and this lock 855 * is Giant, then skip it. 856 */ 857 if ((lock->lo_flags & LO_SLEEPABLE) != 0 && 858 lock1->li_lock == &Giant.mtx_object) 859 continue; 860 /* 861 * If we are locking a sleepable lock and this lock 862 * isn't sleepable, we want to treat it as a lock 863 * order violation to enfore a general lock order of 864 * sleepable locks before non-sleepable locks. 865 */ 866 if (!((lock->lo_flags & LO_SLEEPABLE) != 0 && 867 (lock1->li_lock->lo_flags & LO_SLEEPABLE) == 0)) 868 /* 869 * Check the lock order hierarchy for a reveresal. 870 */ 871 if (!isitmydescendant(w, w1)) 872 continue; 873 /* 874 * We have a lock order violation, check to see if it 875 * is allowed or has already been yelled about. 876 */ 877 mtx_unlock_spin(&w_mtx); 878#ifdef BLESSING 879 /* 880 * If the lock order is blessed, just bail. We don't 881 * look for other lock order violations though, which 882 * may be a bug. 883 */ 884 if (blessed(w, w1)) 885 return; 886#endif 887 if (lock1->li_lock == &Giant.mtx_object) { 888 if (w1->w_Giant_squawked) 889 return; 890 else 891 w1->w_Giant_squawked = 1; 892 } else { 893 if (w1->w_other_squawked) 894 return; 895 else 896 w1->w_other_squawked = 1; 897 } 898 /* 899 * Ok, yell about it. 900 */ 901 printf("lock order reversal\n"); 902 /* 903 * Try to locate an earlier lock with 904 * witness w in our list. 905 */ 906 do { 907 lock2 = &lle->ll_children[i]; 908 MPASS(lock2->li_lock != NULL); 909 if (lock2->li_lock->lo_witness == w) 910 break; 911 if (i == 0 && lle->ll_next != NULL) { 912 lle = lle->ll_next; 913 i = lle->ll_count - 1; 914 MPASS(i >= 0 && i < LOCK_NCHILDREN); 915 } else 916 i--; 917 } while (i >= 0); 918 if (i < 0) { 919 printf(" 1st %p %s (%s) @ %s:%d\n", 920 lock1->li_lock, lock1->li_lock->lo_name, 921 lock1->li_lock->lo_type, lock1->li_file, 922 lock1->li_line); 923 printf(" 2nd %p %s (%s) @ %s:%d\n", lock, 924 lock->lo_name, lock->lo_type, file, line); 925 } else { 926 printf(" 1st %p %s (%s) @ %s:%d\n", 927 lock2->li_lock, lock2->li_lock->lo_name, 928 lock2->li_lock->lo_type, lock2->li_file, 929 lock2->li_line); 930 printf(" 2nd %p %s (%s) @ %s:%d\n", 931 lock1->li_lock, lock1->li_lock->lo_name, 932 lock1->li_lock->lo_type, lock1->li_file, 933 lock1->li_line); 934 printf(" 3rd %p %s (%s) @ %s:%d\n", lock, 935 lock->lo_name, lock->lo_type, file, line); 936 } 937#ifdef KDB 938 goto debugger; 939#else 940 return; 941#endif 942 } 943 } 944 lock1 = &(*lock_list)->ll_children[(*lock_list)->ll_count - 1]; 945 /* 946 * If requested, build a new lock order. However, don't build a new 947 * relationship between a sleepable lock and Giant if it is in the 948 * wrong direction. The correct lock order is that sleepable locks 949 * always come before Giant. 950 */ 951 if (flags & LOP_NEWORDER && 952 !(lock1->li_lock == &Giant.mtx_object && 953 (lock->lo_flags & LO_SLEEPABLE) != 0)) { 954 CTR3(KTR_WITNESS, "%s: adding %s as a child of %s", __func__, 955 lock->lo_type, lock1->li_lock->lo_type); 956 if (!itismychild(lock1->li_lock->lo_witness, w)) 957 /* Witness is dead. */ 958 return; 959 } 960 mtx_unlock_spin(&w_mtx); 961 return; 962 963#ifdef KDB 964debugger: 965 if (witness_trace) 966 kdb_backtrace(); 967 if (witness_kdb) 968 kdb_enter(__func__); 969#endif 970} 971 972void 973witness_lock(struct lock_object *lock, int flags, const char *file, int line) 974{ 975 struct lock_list_entry **lock_list, *lle; 976 struct lock_instance *instance; 977 struct witness *w; 978 struct thread *td; 979 980 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 981 panicstr != NULL) 982 return; 983 w = lock->lo_witness; 984 td = curthread; 985 file = fixup_filename(file); 986 987 /* Determine lock list for this lock. */ 988 if (lock->lo_class->lc_flags & LC_SLEEPLOCK) 989 lock_list = &td->td_sleeplocks; 990 else 991 lock_list = PCPU_PTR(spinlocks); 992 993 /* Check to see if we are recursing on a lock we already own. */ 994 instance = find_instance(*lock_list, lock); 995 if (instance != NULL) { 996 instance->li_flags++; 997 CTR4(KTR_WITNESS, "%s: pid %d recursed on %s r=%d", __func__, 998 td->td_proc->p_pid, lock->lo_name, 999 instance->li_flags & LI_RECURSEMASK); 1000 instance->li_file = file; 1001 instance->li_line = line; 1002 return; 1003 } 1004 1005 /* Update per-witness last file and line acquire. */ 1006 w->w_file = file; 1007 w->w_line = line; 1008 1009 /* Find the next open lock instance in the list and fill it. */ 1010 lle = *lock_list; 1011 if (lle == NULL || lle->ll_count == LOCK_NCHILDREN) { 1012 lle = witness_lock_list_get(); 1013 if (lle == NULL) 1014 return; 1015 lle->ll_next = *lock_list; 1016 CTR3(KTR_WITNESS, "%s: pid %d added lle %p", __func__, 1017 td->td_proc->p_pid, lle); 1018 *lock_list = lle; 1019 } 1020 instance = &lle->ll_children[lle->ll_count++]; 1021 instance->li_lock = lock; 1022 instance->li_line = line; 1023 instance->li_file = file; 1024 if ((flags & LOP_EXCLUSIVE) != 0) 1025 instance->li_flags = LI_EXCLUSIVE; 1026 else 1027 instance->li_flags = 0; 1028 CTR4(KTR_WITNESS, "%s: pid %d added %s as lle[%d]", __func__, 1029 td->td_proc->p_pid, lock->lo_name, lle->ll_count - 1); 1030} 1031 1032void 1033witness_upgrade(struct lock_object *lock, int flags, const char *file, int line) 1034{ 1035 struct lock_instance *instance; 1036 struct lock_class *class; 1037 1038 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1039 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1040 return; 1041 class = lock->lo_class; 1042 file = fixup_filename(file); 1043 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1044 panic("upgrade of non-upgradable lock (%s) %s @ %s:%d", 1045 class->lc_name, lock->lo_name, file, line); 1046 if ((flags & LOP_TRYLOCK) == 0) 1047 panic("non-try upgrade of lock (%s) %s @ %s:%d", class->lc_name, 1048 lock->lo_name, file, line); 1049 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1050 panic("upgrade of non-sleep lock (%s) %s @ %s:%d", 1051 class->lc_name, lock->lo_name, file, line); 1052 instance = find_instance(curthread->td_sleeplocks, lock); 1053 if (instance == NULL) 1054 panic("upgrade of unlocked lock (%s) %s @ %s:%d", 1055 class->lc_name, lock->lo_name, file, line); 1056 if ((instance->li_flags & LI_EXCLUSIVE) != 0) 1057 panic("upgrade of exclusive lock (%s) %s @ %s:%d", 1058 class->lc_name, lock->lo_name, file, line); 1059 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1060 panic("upgrade of recursed lock (%s) %s r=%d @ %s:%d", 1061 class->lc_name, lock->lo_name, 1062 instance->li_flags & LI_RECURSEMASK, file, line); 1063 instance->li_flags |= LI_EXCLUSIVE; 1064} 1065 1066void 1067witness_downgrade(struct lock_object *lock, int flags, const char *file, 1068 int line) 1069{ 1070 struct lock_instance *instance; 1071 struct lock_class *class; 1072 1073 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1074 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1075 return; 1076 class = lock->lo_class; 1077 file = fixup_filename(file); 1078 if ((lock->lo_flags & LO_UPGRADABLE) == 0) 1079 panic("downgrade of non-upgradable lock (%s) %s @ %s:%d", 1080 class->lc_name, lock->lo_name, file, line); 1081 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1082 panic("downgrade of non-sleep lock (%s) %s @ %s:%d", 1083 class->lc_name, lock->lo_name, file, line); 1084 instance = find_instance(curthread->td_sleeplocks, lock); 1085 if (instance == NULL) 1086 panic("downgrade of unlocked lock (%s) %s @ %s:%d", 1087 class->lc_name, lock->lo_name, file, line); 1088 if ((instance->li_flags & LI_EXCLUSIVE) == 0) 1089 panic("downgrade of shared lock (%s) %s @ %s:%d", 1090 class->lc_name, lock->lo_name, file, line); 1091 if ((instance->li_flags & LI_RECURSEMASK) != 0) 1092 panic("downgrade of recursed lock (%s) %s r=%d @ %s:%d", 1093 class->lc_name, lock->lo_name, 1094 instance->li_flags & LI_RECURSEMASK, file, line); 1095 instance->li_flags &= ~LI_EXCLUSIVE; 1096} 1097 1098void 1099witness_unlock(struct lock_object *lock, int flags, const char *file, int line) 1100{ 1101 struct lock_list_entry **lock_list, *lle; 1102 struct lock_instance *instance; 1103 struct lock_class *class; 1104 struct thread *td; 1105 register_t s; 1106 int i, j; 1107 1108 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL || 1109 panicstr != NULL) 1110 return; 1111 td = curthread; 1112 class = lock->lo_class; 1113 file = fixup_filename(file); 1114 1115 /* Find lock instance associated with this lock. */ 1116 if (class->lc_flags & LC_SLEEPLOCK) 1117 lock_list = &td->td_sleeplocks; 1118 else 1119 lock_list = PCPU_PTR(spinlocks); 1120 for (; *lock_list != NULL; lock_list = &(*lock_list)->ll_next) 1121 for (i = 0; i < (*lock_list)->ll_count; i++) { 1122 instance = &(*lock_list)->ll_children[i]; 1123 if (instance->li_lock == lock) 1124 goto found; 1125 } 1126 panic("lock (%s) %s not locked @ %s:%d", class->lc_name, lock->lo_name, 1127 file, line); 1128found: 1129 1130 /* First, check for shared/exclusive mismatches. */ 1131 if ((instance->li_flags & LI_EXCLUSIVE) != 0 && 1132 (flags & LOP_EXCLUSIVE) == 0) { 1133 printf("shared unlock of (%s) %s @ %s:%d\n", class->lc_name, 1134 lock->lo_name, file, line); 1135 printf("while exclusively locked from %s:%d\n", 1136 instance->li_file, instance->li_line); 1137 panic("excl->ushare"); 1138 } 1139 if ((instance->li_flags & LI_EXCLUSIVE) == 0 && 1140 (flags & LOP_EXCLUSIVE) != 0) { 1141 printf("exclusive unlock of (%s) %s @ %s:%d\n", class->lc_name, 1142 lock->lo_name, file, line); 1143 printf("while share locked from %s:%d\n", instance->li_file, 1144 instance->li_line); 1145 panic("share->uexcl"); 1146 } 1147 1148 /* If we are recursed, unrecurse. */ 1149 if ((instance->li_flags & LI_RECURSEMASK) > 0) { 1150 CTR4(KTR_WITNESS, "%s: pid %d unrecursed on %s r=%d", __func__, 1151 td->td_proc->p_pid, instance->li_lock->lo_name, 1152 instance->li_flags); 1153 instance->li_flags--; 1154 return; 1155 } 1156 1157 /* Otherwise, remove this item from the list. */ 1158 s = intr_disable(); 1159 CTR4(KTR_WITNESS, "%s: pid %d removed %s from lle[%d]", __func__, 1160 td->td_proc->p_pid, instance->li_lock->lo_name, 1161 (*lock_list)->ll_count - 1); 1162 for (j = i; j < (*lock_list)->ll_count - 1; j++) 1163 (*lock_list)->ll_children[j] = 1164 (*lock_list)->ll_children[j + 1]; 1165 (*lock_list)->ll_count--; 1166 intr_restore(s); 1167 1168 /* If this lock list entry is now empty, free it. */ 1169 if ((*lock_list)->ll_count == 0) { 1170 lle = *lock_list; 1171 *lock_list = lle->ll_next; 1172 CTR3(KTR_WITNESS, "%s: pid %d removed lle %p", __func__, 1173 td->td_proc->p_pid, lle); 1174 witness_lock_list_free(lle); 1175 } 1176} 1177 1178/* 1179 * Warn if any locks other than 'lock' are held. Flags can be passed in to 1180 * exempt Giant and sleepable locks from the checks as well. If any 1181 * non-exempt locks are held, then a supplied message is printed to the 1182 * console along with a list of the offending locks. If indicated in the 1183 * flags then a failure results in a panic as well. 1184 */ 1185int 1186witness_warn(int flags, struct lock_object *lock, const char *fmt, ...) 1187{ 1188 struct lock_list_entry *lle; 1189 struct lock_instance *lock1; 1190 struct thread *td; 1191 va_list ap; 1192 int i, n; 1193 1194 if (witness_cold || witness_watch == 0 || panicstr != NULL) 1195 return (0); 1196 n = 0; 1197 td = curthread; 1198 for (lle = td->td_sleeplocks; lle != NULL; lle = lle->ll_next) 1199 for (i = lle->ll_count - 1; i >= 0; i--) { 1200 lock1 = &lle->ll_children[i]; 1201 if (lock1->li_lock == lock) 1202 continue; 1203 if (flags & WARN_GIANTOK && 1204 lock1->li_lock == &Giant.mtx_object) 1205 continue; 1206 if (flags & WARN_SLEEPOK && 1207 (lock1->li_lock->lo_flags & LO_SLEEPABLE) != 0) 1208 continue; 1209 if (n == 0) { 1210 va_start(ap, fmt); 1211 vprintf(fmt, ap); 1212 va_end(ap); 1213 printf(" with the following"); 1214 if (flags & WARN_SLEEPOK) 1215 printf(" non-sleepable"); 1216 printf(" locks held:\n"); 1217 } 1218 n++; 1219 witness_list_lock(lock1); 1220 } 1221 if (PCPU_GET(spinlocks) != NULL) { 1222 /* 1223 * Since we already hold a spinlock preemption is 1224 * already blocked. 1225 */ 1226 if (n == 0) { 1227 va_start(ap, fmt); 1228 vprintf(fmt, ap); 1229 va_end(ap); 1230 printf(" with the following"); 1231 if (flags & WARN_SLEEPOK) 1232 printf(" non-sleepable"); 1233 printf(" locks held:\n"); 1234 } 1235 n += witness_list_locks(PCPU_PTR(spinlocks)); 1236 } 1237 if (flags & WARN_PANIC && n) 1238 panic("witness_warn"); 1239#ifdef KDB 1240 else if (witness_kdb && n) 1241 kdb_enter(__func__); 1242 else if (witness_trace && n) 1243 kdb_backtrace(); 1244#endif 1245 return (n); 1246} 1247 1248const char * 1249witness_file(struct lock_object *lock) 1250{ 1251 struct witness *w; 1252 1253 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1254 return ("?"); 1255 w = lock->lo_witness; 1256 return (w->w_file); 1257} 1258 1259int 1260witness_line(struct lock_object *lock) 1261{ 1262 struct witness *w; 1263 1264 if (witness_cold || witness_watch == 0 || lock->lo_witness == NULL) 1265 return (0); 1266 w = lock->lo_witness; 1267 return (w->w_line); 1268} 1269 1270static struct witness * 1271enroll(const char *description, struct lock_class *lock_class) 1272{ 1273 struct witness *w; 1274 1275 if (witness_watch == 0 || panicstr != NULL) 1276 return (NULL); 1277 if ((lock_class->lc_flags & LC_SPINLOCK) && witness_skipspin) 1278 return (NULL); 1279 mtx_lock_spin(&w_mtx); 1280 STAILQ_FOREACH(w, &w_all, w_list) { 1281 if (w->w_name == description || (w->w_refcount > 0 && 1282 strcmp(description, w->w_name) == 0)) { 1283 w->w_refcount++; 1284 mtx_unlock_spin(&w_mtx); 1285 if (lock_class != w->w_class) 1286 panic( 1287 "lock (%s) %s does not match earlier (%s) lock", 1288 description, lock_class->lc_name, 1289 w->w_class->lc_name); 1290 return (w); 1291 } 1292 } 1293 /* 1294 * This isn't quite right, as witness_cold is still 0 while we 1295 * enroll all the locks initialized before witness_initialize(). 1296 */ 1297 if ((lock_class->lc_flags & LC_SPINLOCK) && !witness_cold) { 1298 mtx_unlock_spin(&w_mtx); 1299 panic("spin lock %s not in order list", description); 1300 } 1301 if ((w = witness_get()) == NULL) 1302 return (NULL); 1303 w->w_name = description; 1304 w->w_class = lock_class; 1305 w->w_refcount = 1; 1306 STAILQ_INSERT_HEAD(&w_all, w, w_list); 1307 if (lock_class->lc_flags & LC_SPINLOCK) 1308 STAILQ_INSERT_HEAD(&w_spin, w, w_typelist); 1309 else if (lock_class->lc_flags & LC_SLEEPLOCK) 1310 STAILQ_INSERT_HEAD(&w_sleep, w, w_typelist); 1311 else { 1312 mtx_unlock_spin(&w_mtx); 1313 panic("lock class %s is not sleep or spin", 1314 lock_class->lc_name); 1315 } 1316 mtx_unlock_spin(&w_mtx); 1317 return (w); 1318} 1319 1320/* Don't let the door bang you on the way out... */ 1321static int 1322depart(struct witness *w) 1323{ 1324 struct witness_child_list_entry *wcl, *nwcl; 1325 struct witness_list *list; 1326 struct witness *parent; 1327 1328 MPASS(w->w_refcount == 0); 1329 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1330 list = &w_sleep; 1331 else 1332 list = &w_spin; 1333 /* 1334 * First, we run through the entire tree looking for any 1335 * witnesses that the outgoing witness is a child of. For 1336 * each parent that we find, we reparent all the direct 1337 * children of the outgoing witness to its parent. 1338 */ 1339 STAILQ_FOREACH(parent, list, w_typelist) { 1340 if (!isitmychild(parent, w)) 1341 continue; 1342 removechild(parent, w); 1343 if (!reparentchildren(parent, w)) 1344 return (0); 1345 } 1346 1347 /* 1348 * Now we go through and free up the child list of the 1349 * outgoing witness. 1350 */ 1351 for (wcl = w->w_children; wcl != NULL; wcl = nwcl) { 1352 nwcl = wcl->wcl_next; 1353 witness_child_free(wcl); 1354 } 1355 1356 /* 1357 * Detach from various lists and free. 1358 */ 1359 STAILQ_REMOVE(list, w, witness, w_typelist); 1360 STAILQ_REMOVE(&w_all, w, witness, w_list); 1361 witness_free(w); 1362 1363 /* Finally, fixup the tree. */ 1364 return (rebalancetree(list)); 1365} 1366 1367/* 1368 * Prune an entire lock order tree. We look for cases where a lock 1369 * is now both a descendant and a direct child of a given lock. In 1370 * that case, we want to remove the direct child link from the tree. 1371 * 1372 * Returns false if insertchild() fails. 1373 */ 1374static int 1375rebalancetree(struct witness_list *list) 1376{ 1377 struct witness *child, *parent; 1378 1379 STAILQ_FOREACH(child, list, w_typelist) { 1380 STAILQ_FOREACH(parent, list, w_typelist) { 1381 if (!isitmychild(parent, child)) 1382 continue; 1383 removechild(parent, child); 1384 if (isitmydescendant(parent, child)) 1385 continue; 1386 if (!insertchild(parent, child)) 1387 return (0); 1388 } 1389 } 1390 witness_levelall(); 1391 return (1); 1392} 1393 1394/* 1395 * Add "child" as a direct child of "parent". Returns false if 1396 * we fail due to out of memory. 1397 */ 1398static int 1399insertchild(struct witness *parent, struct witness *child) 1400{ 1401 struct witness_child_list_entry **wcl; 1402 1403 MPASS(child != NULL && parent != NULL); 1404 1405 /* 1406 * Insert "child" after "parent" 1407 */ 1408 wcl = &parent->w_children; 1409 while (*wcl != NULL && (*wcl)->wcl_count == WITNESS_NCHILDREN) 1410 wcl = &(*wcl)->wcl_next; 1411 if (*wcl == NULL) { 1412 *wcl = witness_child_get(); 1413 if (*wcl == NULL) 1414 return (0); 1415 } 1416 (*wcl)->wcl_children[(*wcl)->wcl_count++] = child; 1417 1418 return (1); 1419} 1420 1421/* 1422 * Make all the direct descendants of oldparent be direct descendants 1423 * of newparent. 1424 */ 1425static int 1426reparentchildren(struct witness *newparent, struct witness *oldparent) 1427{ 1428 struct witness_child_list_entry *wcl; 1429 int i; 1430 1431 /* Avoid making a witness a child of itself. */ 1432 MPASS(!isitmychild(oldparent, newparent)); 1433 1434 for (wcl = oldparent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1435 for (i = 0; i < wcl->wcl_count; i++) 1436 if (!insertchild(newparent, wcl->wcl_children[i])) 1437 return (0); 1438 return (1); 1439} 1440 1441static int 1442itismychild(struct witness *parent, struct witness *child) 1443{ 1444 struct witness_list *list; 1445 1446 MPASS(child != NULL && parent != NULL); 1447 if ((parent->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK)) != 1448 (child->w_class->lc_flags & (LC_SLEEPLOCK | LC_SPINLOCK))) 1449 panic( 1450 "%s: parent (%s) and child (%s) are not the same lock type", 1451 __func__, parent->w_class->lc_name, 1452 child->w_class->lc_name); 1453 1454 if (!insertchild(parent, child)) 1455 return (0); 1456 1457 if (parent->w_class->lc_flags & LC_SLEEPLOCK) 1458 list = &w_sleep; 1459 else 1460 list = &w_spin; 1461 return (rebalancetree(list)); 1462} 1463 1464static void 1465removechild(struct witness *parent, struct witness *child) 1466{ 1467 struct witness_child_list_entry **wcl, *wcl1; 1468 int i; 1469 1470 for (wcl = &parent->w_children; *wcl != NULL; wcl = &(*wcl)->wcl_next) 1471 for (i = 0; i < (*wcl)->wcl_count; i++) 1472 if ((*wcl)->wcl_children[i] == child) 1473 goto found; 1474 return; 1475found: 1476 (*wcl)->wcl_count--; 1477 if ((*wcl)->wcl_count > i) 1478 (*wcl)->wcl_children[i] = 1479 (*wcl)->wcl_children[(*wcl)->wcl_count]; 1480 MPASS((*wcl)->wcl_children[i] != NULL); 1481 if ((*wcl)->wcl_count != 0) 1482 return; 1483 wcl1 = *wcl; 1484 *wcl = wcl1->wcl_next; 1485 witness_child_free(wcl1); 1486} 1487 1488static int 1489isitmychild(struct witness *parent, struct witness *child) 1490{ 1491 struct witness_child_list_entry *wcl; 1492 int i; 1493 1494 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1495 for (i = 0; i < wcl->wcl_count; i++) { 1496 if (wcl->wcl_children[i] == child) 1497 return (1); 1498 } 1499 } 1500 return (0); 1501} 1502 1503static int 1504isitmydescendant(struct witness *parent, struct witness *child) 1505{ 1506 struct witness_child_list_entry *wcl; 1507 int i, j; 1508 1509 if (isitmychild(parent, child)) 1510 return (1); 1511 j = 0; 1512 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) { 1513 MPASS(j < 1000); 1514 for (i = 0; i < wcl->wcl_count; i++) { 1515 if (isitmydescendant(wcl->wcl_children[i], child)) 1516 return (1); 1517 } 1518 j++; 1519 } 1520 return (0); 1521} 1522 1523static void 1524witness_levelall (void) 1525{ 1526 struct witness_list *list; 1527 struct witness *w, *w1; 1528 1529 /* 1530 * First clear all levels. 1531 */ 1532 STAILQ_FOREACH(w, &w_all, w_list) { 1533 w->w_level = 0; 1534 } 1535 1536 /* 1537 * Look for locks with no parent and level all their descendants. 1538 */ 1539 STAILQ_FOREACH(w, &w_all, w_list) { 1540 /* 1541 * This is just an optimization, technically we could get 1542 * away just walking the all list each time. 1543 */ 1544 if (w->w_class->lc_flags & LC_SLEEPLOCK) 1545 list = &w_sleep; 1546 else 1547 list = &w_spin; 1548 STAILQ_FOREACH(w1, list, w_typelist) { 1549 if (isitmychild(w1, w)) 1550 goto skip; 1551 } 1552 witness_leveldescendents(w, 0); 1553 skip: 1554 ; /* silence GCC 3.x */ 1555 } 1556} 1557 1558static void 1559witness_leveldescendents(struct witness *parent, int level) 1560{ 1561 struct witness_child_list_entry *wcl; 1562 int i; 1563 1564 if (parent->w_level < level) 1565 parent->w_level = level; 1566 level++; 1567 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1568 for (i = 0; i < wcl->wcl_count; i++) 1569 witness_leveldescendents(wcl->wcl_children[i], level); 1570} 1571 1572static void 1573witness_displaydescendants(void(*prnt)(const char *fmt, ...), 1574 struct witness *parent, int indent) 1575{ 1576 struct witness_child_list_entry *wcl; 1577 int i, level; 1578 1579 level = parent->w_level; 1580 prnt("%-2d", level); 1581 for (i = 0; i < indent; i++) 1582 prnt(" "); 1583 if (parent->w_refcount > 0) 1584 prnt("%s", parent->w_name); 1585 else 1586 prnt("(dead)"); 1587 if (parent->w_displayed) { 1588 prnt(" -- (already displayed)\n"); 1589 return; 1590 } 1591 parent->w_displayed = 1; 1592 if (parent->w_refcount > 0) { 1593 if (parent->w_file != NULL) 1594 prnt(" -- last acquired @ %s:%d", parent->w_file, 1595 parent->w_line); 1596 } 1597 prnt("\n"); 1598 for (wcl = parent->w_children; wcl != NULL; wcl = wcl->wcl_next) 1599 for (i = 0; i < wcl->wcl_count; i++) 1600 witness_displaydescendants(prnt, 1601 wcl->wcl_children[i], indent + 1); 1602} 1603 1604#ifdef BLESSING 1605static int 1606blessed(struct witness *w1, struct witness *w2) 1607{ 1608 int i; 1609 struct witness_blessed *b; 1610 1611 for (i = 0; i < blessed_count; i++) { 1612 b = &blessed_list[i]; 1613 if (strcmp(w1->w_name, b->b_lock1) == 0) { 1614 if (strcmp(w2->w_name, b->b_lock2) == 0) 1615 return (1); 1616 continue; 1617 } 1618 if (strcmp(w1->w_name, b->b_lock2) == 0) 1619 if (strcmp(w2->w_name, b->b_lock1) == 0) 1620 return (1); 1621 } 1622 return (0); 1623} 1624#endif 1625 1626static struct witness * 1627witness_get(void) 1628{ 1629 struct witness *w; 1630 1631 if (witness_watch == 0) { 1632 mtx_unlock_spin(&w_mtx); 1633 return (NULL); 1634 } 1635 if (STAILQ_EMPTY(&w_free)) { 1636 witness_watch = 0; 1637 mtx_unlock_spin(&w_mtx); 1638 printf("%s: witness exhausted\n", __func__); 1639 return (NULL); 1640 } 1641 w = STAILQ_FIRST(&w_free); 1642 STAILQ_REMOVE_HEAD(&w_free, w_list); 1643 bzero(w, sizeof(*w)); 1644 return (w); 1645} 1646 1647static void 1648witness_free(struct witness *w) 1649{ 1650 1651 STAILQ_INSERT_HEAD(&w_free, w, w_list); 1652} 1653 1654static struct witness_child_list_entry * 1655witness_child_get(void) 1656{ 1657 struct witness_child_list_entry *wcl; 1658 1659 if (witness_watch == 0) { 1660 mtx_unlock_spin(&w_mtx); 1661 return (NULL); 1662 } 1663 wcl = w_child_free; 1664 if (wcl == NULL) { 1665 witness_watch = 0; 1666 mtx_unlock_spin(&w_mtx); 1667 printf("%s: witness exhausted\n", __func__); 1668 return (NULL); 1669 } 1670 w_child_free = wcl->wcl_next; 1671 bzero(wcl, sizeof(*wcl)); 1672 return (wcl); 1673} 1674 1675static void 1676witness_child_free(struct witness_child_list_entry *wcl) 1677{ 1678 1679 wcl->wcl_next = w_child_free; 1680 w_child_free = wcl; 1681} 1682 1683static struct lock_list_entry * 1684witness_lock_list_get(void) 1685{ 1686 struct lock_list_entry *lle; 1687 1688 if (witness_watch == 0) 1689 return (NULL); 1690 mtx_lock_spin(&w_mtx); 1691 lle = w_lock_list_free; 1692 if (lle == NULL) { 1693 witness_watch = 0; 1694 mtx_unlock_spin(&w_mtx); 1695 printf("%s: witness exhausted\n", __func__); 1696 return (NULL); 1697 } 1698 w_lock_list_free = lle->ll_next; 1699 mtx_unlock_spin(&w_mtx); 1700 bzero(lle, sizeof(*lle)); 1701 return (lle); 1702} 1703 1704static void 1705witness_lock_list_free(struct lock_list_entry *lle) 1706{ 1707 1708 mtx_lock_spin(&w_mtx); 1709 lle->ll_next = w_lock_list_free; 1710 w_lock_list_free = lle; 1711 mtx_unlock_spin(&w_mtx); 1712} 1713 1714static struct lock_instance * 1715find_instance(struct lock_list_entry *lock_list, struct lock_object *lock) 1716{ 1717 struct lock_list_entry *lle; 1718 struct lock_instance *instance; 1719 int i; 1720 1721 for (lle = lock_list; lle != NULL; lle = lle->ll_next) 1722 for (i = lle->ll_count - 1; i >= 0; i--) { 1723 instance = &lle->ll_children[i]; 1724 if (instance->li_lock == lock) 1725 return (instance); 1726 } 1727 return (NULL); 1728} 1729 1730static void 1731witness_list_lock(struct lock_instance *instance) 1732{ 1733 struct lock_object *lock; 1734 1735 lock = instance->li_lock; 1736 printf("%s %s %s", (instance->li_flags & LI_EXCLUSIVE) != 0 ? 1737 "exclusive" : "shared", lock->lo_class->lc_name, lock->lo_name); 1738 if (lock->lo_type != lock->lo_name) 1739 printf(" (%s)", lock->lo_type); 1740 printf(" r = %d (%p) locked @ %s:%d\n", 1741 instance->li_flags & LI_RECURSEMASK, lock, instance->li_file, 1742 instance->li_line); 1743} 1744 1745#ifdef DDB 1746static int 1747witness_thread_has_locks(struct thread *td) 1748{ 1749 1750 return (td->td_sleeplocks != NULL); 1751} 1752 1753static int 1754witness_proc_has_locks(struct proc *p) 1755{ 1756 struct thread *td; 1757 1758 FOREACH_THREAD_IN_PROC(p, td) { 1759 if (witness_thread_has_locks(td)) 1760 return (1); 1761 } 1762 return (0); 1763} 1764#endif 1765 1766int 1767witness_list_locks(struct lock_list_entry **lock_list) 1768{ 1769 struct lock_list_entry *lle; 1770 int i, nheld; 1771 1772 nheld = 0; 1773 for (lle = *lock_list; lle != NULL; lle = lle->ll_next) 1774 for (i = lle->ll_count - 1; i >= 0; i--) { 1775 witness_list_lock(&lle->ll_children[i]); 1776 nheld++; 1777 } 1778 return (nheld); 1779} 1780 1781/* 1782 * This is a bit risky at best. We call this function when we have timed 1783 * out acquiring a spin lock, and we assume that the other CPU is stuck 1784 * with this lock held. So, we go groveling around in the other CPU's 1785 * per-cpu data to try to find the lock instance for this spin lock to 1786 * see when it was last acquired. 1787 */ 1788void 1789witness_display_spinlock(struct lock_object *lock, struct thread *owner) 1790{ 1791 struct lock_instance *instance; 1792 struct pcpu *pc; 1793 1794 if (owner->td_critnest == 0 || owner->td_oncpu == NOCPU) 1795 return; 1796 pc = pcpu_find(owner->td_oncpu); 1797 instance = find_instance(pc->pc_spinlocks, lock); 1798 if (instance != NULL) 1799 witness_list_lock(instance); 1800} 1801 1802void 1803witness_save(struct lock_object *lock, const char **filep, int *linep) 1804{ 1805 struct lock_instance *instance; 1806 1807 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1808 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1809 return; 1810 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1811 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1812 lock->lo_class->lc_name, lock->lo_name); 1813 instance = find_instance(curthread->td_sleeplocks, lock); 1814 if (instance == NULL) 1815 panic("%s: lock (%s) %s not locked", __func__, 1816 lock->lo_class->lc_name, lock->lo_name); 1817 *filep = instance->li_file; 1818 *linep = instance->li_line; 1819} 1820 1821void 1822witness_restore(struct lock_object *lock, const char *file, int line) 1823{ 1824 struct lock_instance *instance; 1825 1826 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1827 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1828 return; 1829 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) == 0) 1830 panic("%s: lock (%s) %s is not a sleep lock", __func__, 1831 lock->lo_class->lc_name, lock->lo_name); 1832 instance = find_instance(curthread->td_sleeplocks, lock); 1833 if (instance == NULL) 1834 panic("%s: lock (%s) %s not locked", __func__, 1835 lock->lo_class->lc_name, lock->lo_name); 1836 lock->lo_witness->w_file = file; 1837 lock->lo_witness->w_line = line; 1838 instance->li_file = file; 1839 instance->li_line = line; 1840} 1841 1842void 1843witness_assert(struct lock_object *lock, int flags, const char *file, int line) 1844{ 1845#ifdef INVARIANT_SUPPORT 1846 struct lock_instance *instance; 1847 1848 if (lock->lo_witness == NULL || witness_watch == 0 || panicstr != NULL) 1849 return; 1850 if ((lock->lo_class->lc_flags & LC_SLEEPLOCK) != 0) 1851 instance = find_instance(curthread->td_sleeplocks, lock); 1852 else if ((lock->lo_class->lc_flags & LC_SPINLOCK) != 0) 1853 instance = find_instance(PCPU_GET(spinlocks), lock); 1854 else { 1855 panic("Lock (%s) %s is not sleep or spin!", 1856 lock->lo_class->lc_name, lock->lo_name); 1857 } 1858 file = fixup_filename(file); 1859 switch (flags) { 1860 case LA_UNLOCKED: 1861 if (instance != NULL) 1862 panic("Lock (%s) %s locked @ %s:%d.", 1863 lock->lo_class->lc_name, lock->lo_name, file, line); 1864 break; 1865 case LA_LOCKED: 1866 case LA_LOCKED | LA_RECURSED: 1867 case LA_LOCKED | LA_NOTRECURSED: 1868 case LA_SLOCKED: 1869 case LA_SLOCKED | LA_RECURSED: 1870 case LA_SLOCKED | LA_NOTRECURSED: 1871 case LA_XLOCKED: 1872 case LA_XLOCKED | LA_RECURSED: 1873 case LA_XLOCKED | LA_NOTRECURSED: 1874 if (instance == NULL) { 1875 panic("Lock (%s) %s not locked @ %s:%d.", 1876 lock->lo_class->lc_name, lock->lo_name, file, line); 1877 break; 1878 } 1879 if ((flags & LA_XLOCKED) != 0 && 1880 (instance->li_flags & LI_EXCLUSIVE) == 0) 1881 panic("Lock (%s) %s not exclusively locked @ %s:%d.", 1882 lock->lo_class->lc_name, lock->lo_name, file, line); 1883 if ((flags & LA_SLOCKED) != 0 && 1884 (instance->li_flags & LI_EXCLUSIVE) != 0) 1885 panic("Lock (%s) %s exclusively locked @ %s:%d.", 1886 lock->lo_class->lc_name, lock->lo_name, file, line); 1887 if ((flags & LA_RECURSED) != 0 && 1888 (instance->li_flags & LI_RECURSEMASK) == 0) 1889 panic("Lock (%s) %s not recursed @ %s:%d.", 1890 lock->lo_class->lc_name, lock->lo_name, file, line); 1891 if ((flags & LA_NOTRECURSED) != 0 && 1892 (instance->li_flags & LI_RECURSEMASK) != 0) 1893 panic("Lock (%s) %s recursed @ %s:%d.", 1894 lock->lo_class->lc_name, lock->lo_name, file, line); 1895 break; 1896 default: 1897 panic("Invalid lock assertion at %s:%d.", file, line); 1898 1899 } 1900#endif /* INVARIANT_SUPPORT */ 1901} 1902 1903#ifdef DDB 1904static void 1905witness_list(struct thread *td) 1906{ 1907 1908 KASSERT(!witness_cold, ("%s: witness_cold", __func__)); 1909 KASSERT(kdb_active, ("%s: not in the debugger", __func__)); 1910 1911 if (witness_watch == 0) 1912 return; 1913 1914 witness_list_locks(&td->td_sleeplocks); 1915 1916 /* 1917 * We only handle spinlocks if td == curthread. This is somewhat broken 1918 * if td is currently executing on some other CPU and holds spin locks 1919 * as we won't display those locks. If we had a MI way of getting 1920 * the per-cpu data for a given cpu then we could use 1921 * td->td_oncpu to get the list of spinlocks for this thread 1922 * and "fix" this. 1923 * 1924 * That still wouldn't really fix this unless we locked sched_lock 1925 * or stopped the other CPU to make sure it wasn't changing the list 1926 * out from under us. It is probably best to just not try to handle 1927 * threads on other CPU's for now. 1928 */ 1929 if (td == curthread && PCPU_GET(spinlocks) != NULL) 1930 witness_list_locks(PCPU_PTR(spinlocks)); 1931} 1932 1933DB_SHOW_COMMAND(locks, db_witness_list) 1934{ 1935 struct thread *td; 1936 pid_t pid; 1937 struct proc *p; 1938 1939 if (have_addr) { 1940 pid = (addr % 16) + ((addr >> 4) % 16) * 10 + 1941 ((addr >> 8) % 16) * 100 + ((addr >> 12) % 16) * 1000 + 1942 ((addr >> 16) % 16) * 10000; 1943 /* sx_slock(&allproc_lock); */ 1944 FOREACH_PROC_IN_SYSTEM(p) { 1945 if (p->p_pid == pid) 1946 break; 1947 } 1948 /* sx_sunlock(&allproc_lock); */ 1949 if (p == NULL) { 1950 db_printf("pid %d not found\n", pid); 1951 return; 1952 } 1953 FOREACH_THREAD_IN_PROC(p, td) { 1954 witness_list(td); 1955 } 1956 } else { 1957 td = curthread; 1958 witness_list(td); 1959 } 1960} 1961 1962DB_SHOW_COMMAND(alllocks, db_witness_list_all) 1963{ 1964 struct thread *td; 1965 struct proc *p; 1966 1967 /* 1968 * It would be nice to list only threads and processes that actually 1969 * held sleep locks, but that information is currently not exported 1970 * by WITNESS. 1971 */ 1972 FOREACH_PROC_IN_SYSTEM(p) { 1973 if (!witness_proc_has_locks(p)) 1974 continue; 1975 FOREACH_THREAD_IN_PROC(p, td) { 1976 if (!witness_thread_has_locks(td)) 1977 continue; 1978 printf("Process %d (%s) thread %p (%d)\n", p->p_pid, 1979 p->p_comm, td, td->td_tid); 1980 witness_list(td); 1981 } 1982 } 1983} 1984 1985DB_SHOW_COMMAND(witness, db_witness_display) 1986{ 1987 1988 witness_display(db_printf); 1989} 1990#endif
|