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